blob: e5b44dd2724eb141b227c60a596c0cec38201732 [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 *
18 * You should have received a copy of the GNU General Public Licens
19 * 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>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040042#include "ext4_jbd2.h"
Theodore Ts'o4a092d72012-11-28 13:03:30 -050043#include "ext4_extents.h"
Tao Maf19d5872012-12-10 14:05:51 -050044#include "xattr.h"
Alex Tomasa86c6182006-10-11 01:21:03 -070045
Jiaying Zhang0562e0b2011-03-21 21:38:05 -040046#include <trace/events/ext4.h>
47
Lukas Czerner5f95d212012-03-19 23:03:19 -040048/*
49 * used by extent splitting.
50 */
51#define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
52 due to ENOSPC */
Lukas Czerner556615d2014-04-20 23:45:47 -040053#define EXT4_EXT_MARK_UNWRIT1 0x2 /* mark first half unwritten */
54#define EXT4_EXT_MARK_UNWRIT2 0x4 /* mark second half unwritten */
Lukas Czerner5f95d212012-03-19 23:03:19 -040055
Dmitry Monakhovdee1f972012-10-10 01:04:58 -040056#define EXT4_EXT_DATA_VALID1 0x8 /* first half contains valid data */
57#define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */
58
Darrick J. Wong7ac59902012-04-29 18:37:10 -040059static __le32 ext4_extent_block_csum(struct inode *inode,
60 struct ext4_extent_header *eh)
61{
62 struct ext4_inode_info *ei = EXT4_I(inode);
63 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
64 __u32 csum;
65
66 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)eh,
67 EXT4_EXTENT_TAIL_OFFSET(eh));
68 return cpu_to_le32(csum);
69}
70
71static int ext4_extent_block_csum_verify(struct inode *inode,
72 struct ext4_extent_header *eh)
73{
74 struct ext4_extent_tail *et;
75
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -040076 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong7ac59902012-04-29 18:37:10 -040077 return 1;
78
79 et = find_ext4_extent_tail(eh);
80 if (et->et_checksum != ext4_extent_block_csum(inode, eh))
81 return 0;
82 return 1;
83}
84
85static void ext4_extent_block_csum_set(struct inode *inode,
86 struct ext4_extent_header *eh)
87{
88 struct ext4_extent_tail *et;
89
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -040090 if (!ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong7ac59902012-04-29 18:37:10 -040091 return;
92
93 et = find_ext4_extent_tail(eh);
94 et->et_checksum = ext4_extent_block_csum(inode, eh);
95}
96
Allison Hendersond583fb82011-05-25 07:41:43 -040097static int ext4_split_extent(handle_t *handle,
98 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -040099 struct ext4_ext_path **ppath,
Allison Hendersond583fb82011-05-25 07:41:43 -0400100 struct ext4_map_blocks *map,
101 int split_flag,
102 int flags);
103
Lukas Czerner5f95d212012-03-19 23:03:19 -0400104static int ext4_split_extent_at(handle_t *handle,
105 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -0400106 struct ext4_ext_path **ppath,
Lukas Czerner5f95d212012-03-19 23:03:19 -0400107 ext4_lblk_t split,
108 int split_flag,
109 int flags);
110
Lukas Czerner91dd8c12012-11-28 12:32:26 -0500111static int ext4_find_delayed_extent(struct inode *inode,
Zheng Liu69eb33d2013-02-18 00:31:07 -0500112 struct extent_status *newes);
Lukas Czerner91dd8c12012-11-28 12:32:26 -0500113
Jan Kara487caee2009-08-17 22:17:20 -0400114static int ext4_ext_truncate_extend_restart(handle_t *handle,
115 struct inode *inode,
116 int needed)
Alex Tomasa86c6182006-10-11 01:21:03 -0700117{
118 int err;
119
Frank Mayhar03901312009-01-07 00:06:22 -0500120 if (!ext4_handle_valid(handle))
121 return 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700122 if (handle->h_buffer_credits > needed)
Shen Feng9102e4f2008-07-11 19:27:31 -0400123 return 0;
124 err = ext4_journal_extend(handle, needed);
Theodore Ts'o0123c932008-08-01 20:57:54 -0400125 if (err <= 0)
Shen Feng9102e4f2008-07-11 19:27:31 -0400126 return err;
Jan Kara487caee2009-08-17 22:17:20 -0400127 err = ext4_truncate_restart_trans(handle, inode, needed);
Dmitry Monakhov0617b832010-05-17 01:00:00 -0400128 if (err == 0)
129 err = -EAGAIN;
Jan Kara487caee2009-08-17 22:17:20 -0400130
131 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -0700132}
133
134/*
135 * could return:
136 * - EROFS
137 * - ENOMEM
138 */
139static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
140 struct ext4_ext_path *path)
141{
142 if (path->p_bh) {
143 /* path points to block */
liang xie5d601252014-05-12 22:06:43 -0400144 BUFFER_TRACE(path->p_bh, "get_write_access");
Alex Tomasa86c6182006-10-11 01:21:03 -0700145 return ext4_journal_get_write_access(handle, path->p_bh);
146 }
147 /* path points to leaf/index in inode body */
148 /* we use in-core data, no need to protect them */
149 return 0;
150}
151
152/*
153 * could return:
154 * - EROFS
155 * - ENOMEM
156 * - EIO
157 */
Darrick J. Wong26564972013-04-19 14:04:12 -0400158int __ext4_ext_dirty(const char *where, unsigned int line, handle_t *handle,
159 struct inode *inode, struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700160{
161 int err;
Dmitry Monakhov4b1f1662014-07-27 22:28:15 -0400162
163 WARN_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
Alex Tomasa86c6182006-10-11 01:21:03 -0700164 if (path->p_bh) {
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400165 ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh));
Alex Tomasa86c6182006-10-11 01:21:03 -0700166 /* path points to block */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -0400167 err = __ext4_handle_dirty_metadata(where, line, handle,
168 inode, path->p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700169 } else {
170 /* path points to leaf/index in inode body */
171 err = ext4_mark_inode_dirty(handle, inode);
172 }
173 return err;
174}
175
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700176static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700177 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500178 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700179{
Alex Tomasa86c6182006-10-11 01:21:03 -0700180 if (path) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400181 int depth = path->p_depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700182 struct ext4_extent *ex;
Alex Tomasa86c6182006-10-11 01:21:03 -0700183
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500184 /*
185 * Try to predict block placement assuming that we are
186 * filling in a file which will eventually be
187 * non-sparse --- i.e., in the case of libbfd writing
188 * an ELF object sections out-of-order but in a way
189 * the eventually results in a contiguous object or
190 * executable file, or some database extending a table
191 * space file. However, this is actually somewhat
192 * non-ideal if we are writing a sparse file such as
193 * qemu or KVM writing a raw image file that is going
194 * to stay fairly sparse, since it will end up
195 * fragmenting the file system's free space. Maybe we
196 * should have some hueristics or some way to allow
197 * userspace to pass a hint to file system,
Tao Mab8d65682011-01-21 23:21:31 +0800198 * especially if the latter case turns out to be
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500199 * common.
200 */
Avantika Mathur7e028972006-12-06 20:41:33 -0800201 ex = path[depth].p_ext;
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500202 if (ex) {
203 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
204 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
205
206 if (block > ext_block)
207 return ext_pblk + (block - ext_block);
208 else
209 return ext_pblk - (ext_block - block);
210 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700211
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700212 /* it looks like index is empty;
213 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700214 if (path[depth].p_bh)
215 return path[depth].p_bh->b_blocknr;
216 }
217
218 /* OK. use inode's group */
Eric Sandeenf86186b2011-06-28 10:01:31 -0400219 return ext4_inode_to_goal_block(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700220}
221
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400222/*
223 * Allocation for a meta data block
224 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700225static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400226ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700227 struct ext4_ext_path *path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400228 struct ext4_extent *ex, int *err, unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700229{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700230 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700231
232 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Allison Henderson55f020d2011-05-25 07:41:26 -0400233 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
234 NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700235 return newblock;
236}
237
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400238static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700239{
240 int size;
241
242 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
243 / sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100244#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400245 if (!check && size > 6)
246 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700247#endif
248 return size;
249}
250
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400251static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700252{
253 int size;
254
255 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
256 / sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100257#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400258 if (!check && size > 5)
259 size = 5;
Alex Tomasa86c6182006-10-11 01:21:03 -0700260#endif
261 return size;
262}
263
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400264static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700265{
266 int size;
267
268 size = sizeof(EXT4_I(inode)->i_data);
269 size -= sizeof(struct ext4_extent_header);
270 size /= sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100271#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400272 if (!check && size > 3)
273 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700274#endif
275 return size;
276}
277
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400278static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700279{
280 int size;
281
282 size = sizeof(EXT4_I(inode)->i_data);
283 size -= sizeof(struct ext4_extent_header);
284 size /= sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100285#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400286 if (!check && size > 4)
287 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700288#endif
289 return size;
290}
291
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400292static inline int
293ext4_force_split_extent_at(handle_t *handle, struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -0400294 struct ext4_ext_path **ppath, ext4_lblk_t lblk,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400295 int nofail)
296{
Theodore Ts'odfe50802014-09-01 14:37:09 -0400297 struct ext4_ext_path *path = *ppath;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400298 int unwritten = ext4_ext_is_unwritten(path[path->p_depth].p_ext);
299
Theodore Ts'odfe50802014-09-01 14:37:09 -0400300 return ext4_split_extent_at(handle, inode, ppath, lblk, unwritten ?
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -0400301 EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0,
302 EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO |
303 (nofail ? EXT4_GET_BLOCKS_METADATA_NOFAIL:0));
304}
305
Mingming Caod2a17632008-07-14 17:52:37 -0400306/*
307 * Calculate the number of metadata blocks needed
308 * to allocate @blocks
309 * Worse case is one block per extent
310 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -0500311int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -0400312{
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500313 struct ext4_inode_info *ei = EXT4_I(inode);
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400314 int idxs;
Mingming Caod2a17632008-07-14 17:52:37 -0400315
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500316 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
317 / sizeof(struct ext4_extent_idx));
Mingming Caod2a17632008-07-14 17:52:37 -0400318
319 /*
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500320 * If the new delayed allocation block is contiguous with the
321 * previous da block, it can share index blocks with the
322 * previous block, so we only need to allocate a new index
323 * block every idxs leaf blocks. At ldxs**2 blocks, we need
324 * an additional index block, and at ldxs**3 blocks, yet
325 * another index blocks.
Mingming Caod2a17632008-07-14 17:52:37 -0400326 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500327 if (ei->i_da_metadata_calc_len &&
328 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400329 int num = 0;
330
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500331 if ((ei->i_da_metadata_calc_len % idxs) == 0)
332 num++;
333 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
334 num++;
335 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
336 num++;
337 ei->i_da_metadata_calc_len = 0;
338 } else
339 ei->i_da_metadata_calc_len++;
340 ei->i_da_metadata_calc_last_lblock++;
341 return num;
342 }
Mingming Caod2a17632008-07-14 17:52:37 -0400343
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500344 /*
345 * In the worst case we need a new set of index blocks at
346 * every level of the inode's extent tree.
347 */
348 ei->i_da_metadata_calc_len = 1;
349 ei->i_da_metadata_calc_last_lblock = lblock;
350 return ext_depth(inode) + 1;
Mingming Caod2a17632008-07-14 17:52:37 -0400351}
352
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400353static int
354ext4_ext_max_entries(struct inode *inode, int depth)
355{
356 int max;
357
358 if (depth == ext_depth(inode)) {
359 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400360 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400361 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400362 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400363 } else {
364 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400365 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400366 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400367 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400368 }
369
370 return max;
371}
372
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400373static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
374{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400375 ext4_fsblk_t block = ext4_ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400376 int len = ext4_ext_get_actual_len(ext);
Eryu Guan5946d082013-12-03 21:22:21 -0500377 ext4_lblk_t lblock = le32_to_cpu(ext->ee_block);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400378
Vegard Nossumcbe04d22016-06-30 11:53:46 -0400379 /*
380 * We allow neither:
381 * - zero length
382 * - overflow/wrap-around
383 */
384 if (lblock + len <= lblock)
Theodore Ts'o31d4f3a2012-03-11 23:30:16 -0400385 return 0;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400386 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400387}
388
389static int ext4_valid_extent_idx(struct inode *inode,
390 struct ext4_extent_idx *ext_idx)
391{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400392 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400393
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400394 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400395}
396
397static int ext4_valid_extent_entries(struct inode *inode,
398 struct ext4_extent_header *eh,
399 int depth)
400{
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400401 unsigned short entries;
402 if (eh->eh_entries == 0)
403 return 1;
404
405 entries = le16_to_cpu(eh->eh_entries);
406
407 if (depth == 0) {
408 /* leaf entries */
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400409 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
Eryu Guan5946d082013-12-03 21:22:21 -0500410 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
411 ext4_fsblk_t pblock = 0;
412 ext4_lblk_t lblock = 0;
413 ext4_lblk_t prev = 0;
414 int len = 0;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400415 while (entries) {
416 if (!ext4_valid_extent(inode, ext))
417 return 0;
Eryu Guan5946d082013-12-03 21:22:21 -0500418
419 /* Check for overlapping extents */
420 lblock = le32_to_cpu(ext->ee_block);
421 len = ext4_ext_get_actual_len(ext);
422 if ((lblock <= prev) && prev) {
423 pblock = ext4_ext_pblock(ext);
424 es->s_last_error_block = cpu_to_le64(pblock);
425 return 0;
426 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400427 ext++;
428 entries--;
Eryu Guan5946d082013-12-03 21:22:21 -0500429 prev = lblock + len - 1;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400430 }
431 } else {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400432 struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400433 while (entries) {
434 if (!ext4_valid_extent_idx(inode, ext_idx))
435 return 0;
436 ext_idx++;
437 entries--;
438 }
439 }
440 return 1;
441}
442
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400443static int __ext4_ext_check(const char *function, unsigned int line,
444 struct inode *inode, struct ext4_extent_header *eh,
Theodore Ts'oc3491792013-08-16 21:21:41 -0400445 int depth, ext4_fsblk_t pblk)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400446{
447 const char *error_msg;
448 int max = 0;
449
450 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
451 error_msg = "invalid magic";
452 goto corrupted;
453 }
454 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
455 error_msg = "unexpected eh_depth";
456 goto corrupted;
457 }
458 if (unlikely(eh->eh_max == 0)) {
459 error_msg = "invalid eh_max";
460 goto corrupted;
461 }
462 max = ext4_ext_max_entries(inode, depth);
463 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
464 error_msg = "too large eh_max";
465 goto corrupted;
466 }
467 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
468 error_msg = "invalid eh_entries";
469 goto corrupted;
470 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400471 if (!ext4_valid_extent_entries(inode, eh, depth)) {
472 error_msg = "invalid extent entries";
473 goto corrupted;
474 }
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400475 /* Verify checksum on non-root extent tree nodes */
476 if (ext_depth(inode) != depth &&
477 !ext4_extent_block_csum_verify(inode, eh)) {
478 error_msg = "extent tree corrupted";
479 goto corrupted;
480 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400481 return 0;
482
483corrupted:
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400484 ext4_error_inode(inode, function, line, 0,
Theodore Ts'oc3491792013-08-16 21:21:41 -0400485 "pblk %llu bad header/extent: %s - magic %x, "
486 "entries %u, max %u(%u), depth %u(%u)",
487 (unsigned long long) pblk, error_msg,
488 le16_to_cpu(eh->eh_magic),
489 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
490 max, le16_to_cpu(eh->eh_depth), depth);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400491 return -EIO;
492}
493
Theodore Ts'oc3491792013-08-16 21:21:41 -0400494#define ext4_ext_check(inode, eh, depth, pblk) \
495 __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk))
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400496
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400497int ext4_ext_check_inode(struct inode *inode)
498{
Theodore Ts'oc3491792013-08-16 21:21:41 -0400499 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode), 0);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400500}
501
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400502static struct buffer_head *
503__read_extent_tree_block(const char *function, unsigned int line,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400504 struct inode *inode, ext4_fsblk_t pblk, int depth,
505 int flags)
Darrick J. Wongf8489122012-04-29 18:21:10 -0400506{
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400507 struct buffer_head *bh;
508 int err;
Darrick J. Wongf8489122012-04-29 18:21:10 -0400509
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400510 bh = sb_getblk(inode->i_sb, pblk);
511 if (unlikely(!bh))
512 return ERR_PTR(-ENOMEM);
513
514 if (!bh_uptodate_or_lock(bh)) {
515 trace_ext4_ext_load_extent(inode, pblk, _RET_IP_);
516 err = bh_submit_read(bh);
517 if (err < 0)
518 goto errout;
519 }
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400520 if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE))
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400521 return bh;
522 err = __ext4_ext_check(function, line, inode,
Theodore Ts'oc3491792013-08-16 21:21:41 -0400523 ext_block_hdr(bh), depth, pblk);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400524 if (err)
525 goto errout;
Darrick J. Wongf8489122012-04-29 18:21:10 -0400526 set_buffer_verified(bh);
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400527 /*
528 * If this is a leaf block, cache all of its entries
529 */
530 if (!(flags & EXT4_EX_NOCACHE) && depth == 0) {
531 struct ext4_extent_header *eh = ext_block_hdr(bh);
532 struct ext4_extent *ex = EXT_FIRST_EXTENT(eh);
533 ext4_lblk_t prev = 0;
534 int i;
535
536 for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) {
537 unsigned int status = EXTENT_STATUS_WRITTEN;
538 ext4_lblk_t lblk = le32_to_cpu(ex->ee_block);
539 int len = ext4_ext_get_actual_len(ex);
540
541 if (prev && (prev != lblk))
542 ext4_es_cache_extent(inode, prev,
543 lblk - prev, ~0,
544 EXTENT_STATUS_HOLE);
545
Lukas Czerner556615d2014-04-20 23:45:47 -0400546 if (ext4_ext_is_unwritten(ex))
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400547 status = EXTENT_STATUS_UNWRITTEN;
548 ext4_es_cache_extent(inode, lblk, len,
549 ext4_ext_pblock(ex), status);
550 prev = lblk + len;
551 }
552 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400553 return bh;
554errout:
555 put_bh(bh);
556 return ERR_PTR(err);
557
Darrick J. Wongf8489122012-04-29 18:21:10 -0400558}
559
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400560#define read_extent_tree_block(inode, pblk, depth, flags) \
561 __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \
562 (depth), (flags))
Darrick J. Wongf8489122012-04-29 18:21:10 -0400563
Theodore Ts'o7869a4a2013-08-16 22:05:14 -0400564/*
565 * This function is called to cache a file's extent information in the
566 * extent status tree
567 */
568int ext4_ext_precache(struct inode *inode)
569{
570 struct ext4_inode_info *ei = EXT4_I(inode);
571 struct ext4_ext_path *path = NULL;
572 struct buffer_head *bh;
573 int i = 0, depth, ret = 0;
574
575 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
576 return 0; /* not an extent-mapped inode */
577
578 down_read(&ei->i_data_sem);
579 depth = ext_depth(inode);
580
581 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1),
582 GFP_NOFS);
583 if (path == NULL) {
584 up_read(&ei->i_data_sem);
585 return -ENOMEM;
586 }
587
588 /* Don't cache anything if there are no external extent blocks */
589 if (depth == 0)
590 goto out;
591 path[0].p_hdr = ext_inode_hdr(inode);
592 ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0);
593 if (ret)
594 goto out;
595 path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr);
596 while (i >= 0) {
597 /*
598 * If this is a leaf block or we've reached the end of
599 * the index block, go up
600 */
601 if ((i == depth) ||
602 path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) {
603 brelse(path[i].p_bh);
604 path[i].p_bh = NULL;
605 i--;
606 continue;
607 }
608 bh = read_extent_tree_block(inode,
609 ext4_idx_pblock(path[i].p_idx++),
610 depth - i - 1,
611 EXT4_EX_FORCE_CACHE);
612 if (IS_ERR(bh)) {
613 ret = PTR_ERR(bh);
614 break;
615 }
616 i++;
617 path[i].p_bh = bh;
618 path[i].p_hdr = ext_block_hdr(bh);
619 path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr);
620 }
621 ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
622out:
623 up_read(&ei->i_data_sem);
624 ext4_ext_drop_refs(path);
625 kfree(path);
626 return ret;
627}
628
Alex Tomasa86c6182006-10-11 01:21:03 -0700629#ifdef EXT_DEBUG
630static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
631{
632 int k, l = path->p_depth;
633
634 ext_debug("path:");
635 for (k = 0; k <= l; k++, path++) {
636 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700637 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400638 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700639 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400640 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700641 le32_to_cpu(path->p_ext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400642 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400643 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400644 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700645 } else
646 ext_debug(" []");
647 }
648 ext_debug("\n");
649}
650
651static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
652{
653 int depth = ext_depth(inode);
654 struct ext4_extent_header *eh;
655 struct ext4_extent *ex;
656 int i;
657
658 if (!path)
659 return;
660
661 eh = path[depth].p_hdr;
662 ex = EXT_FIRST_EXTENT(eh);
663
Mingming553f9002009-09-18 13:34:55 -0400664 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
665
Alex Tomasa86c6182006-10-11 01:21:03 -0700666 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400667 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -0400668 ext4_ext_is_unwritten(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400669 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700670 }
671 ext_debug("\n");
672}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400673
674static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
675 ext4_fsblk_t newblock, int level)
676{
677 int depth = ext_depth(inode);
678 struct ext4_extent *ex;
679
680 if (depth != level) {
681 struct ext4_extent_idx *idx;
682 idx = path[level].p_idx;
683 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
684 ext_debug("%d: move %d:%llu in new index %llu\n", level,
685 le32_to_cpu(idx->ei_block),
686 ext4_idx_pblock(idx),
687 newblock);
688 idx++;
689 }
690
691 return;
692 }
693
694 ex = path[depth].p_ext;
695 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
696 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
697 le32_to_cpu(ex->ee_block),
698 ext4_ext_pblock(ex),
Lukas Czerner556615d2014-04-20 23:45:47 -0400699 ext4_ext_is_unwritten(ex),
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400700 ext4_ext_get_actual_len(ex),
701 newblock);
702 ex++;
703 }
704}
705
Alex Tomasa86c6182006-10-11 01:21:03 -0700706#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400707#define ext4_ext_show_path(inode, path)
708#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400709#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700710#endif
711
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500712void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700713{
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400714 int depth, i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700715
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -0400716 if (!path)
717 return;
718 depth = path->p_depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700719 for (i = 0; i <= depth; i++, path++)
720 if (path->p_bh) {
721 brelse(path->p_bh);
722 path->p_bh = NULL;
723 }
724}
725
726/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700727 * ext4_ext_binsearch_idx:
728 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400729 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700730 */
731static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500732ext4_ext_binsearch_idx(struct inode *inode,
733 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700734{
735 struct ext4_extent_header *eh = path->p_hdr;
736 struct ext4_extent_idx *r, *l, *m;
737
Alex Tomasa86c6182006-10-11 01:21:03 -0700738
Eric Sandeenbba90742008-01-28 23:58:27 -0500739 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700740
741 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400742 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700743 while (l <= r) {
744 m = l + (r - l) / 2;
745 if (block < le32_to_cpu(m->ei_block))
746 r = m - 1;
747 else
748 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400749 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
750 m, le32_to_cpu(m->ei_block),
751 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700752 }
753
754 path->p_idx = l - 1;
Zheng Liu4a3c3a52012-05-28 17:55:16 -0400755 ext_debug(" -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400756 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700757
758#ifdef CHECK_BINSEARCH
759 {
760 struct ext4_extent_idx *chix, *ix;
761 int k;
762
763 chix = ix = EXT_FIRST_INDEX(eh);
764 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
765 if (k != 0 &&
766 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400767 printk(KERN_DEBUG "k=%d, ix=0x%p, "
768 "first=0x%p\n", k,
769 ix, EXT_FIRST_INDEX(eh));
770 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700771 le32_to_cpu(ix->ei_block),
772 le32_to_cpu(ix[-1].ei_block));
773 }
774 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400775 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700776 if (block < le32_to_cpu(ix->ei_block))
777 break;
778 chix = ix;
779 }
780 BUG_ON(chix != path->p_idx);
781 }
782#endif
783
784}
785
786/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700787 * ext4_ext_binsearch:
788 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400789 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700790 */
791static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500792ext4_ext_binsearch(struct inode *inode,
793 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700794{
795 struct ext4_extent_header *eh = path->p_hdr;
796 struct ext4_extent *r, *l, *m;
797
Alex Tomasa86c6182006-10-11 01:21:03 -0700798 if (eh->eh_entries == 0) {
799 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700800 * this leaf is empty:
801 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700802 */
803 return;
804 }
805
Eric Sandeenbba90742008-01-28 23:58:27 -0500806 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700807
808 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400809 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700810
811 while (l <= r) {
812 m = l + (r - l) / 2;
813 if (block < le32_to_cpu(m->ee_block))
814 r = m - 1;
815 else
816 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400817 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
818 m, le32_to_cpu(m->ee_block),
819 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700820 }
821
822 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400823 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400824 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400825 ext4_ext_pblock(path->p_ext),
Lukas Czerner556615d2014-04-20 23:45:47 -0400826 ext4_ext_is_unwritten(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400827 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700828
829#ifdef CHECK_BINSEARCH
830 {
831 struct ext4_extent *chex, *ex;
832 int k;
833
834 chex = ex = EXT_FIRST_EXTENT(eh);
835 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
836 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400837 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700838 if (block < le32_to_cpu(ex->ee_block))
839 break;
840 chex = ex;
841 }
842 BUG_ON(chex != path->p_ext);
843 }
844#endif
845
846}
847
848int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
849{
850 struct ext4_extent_header *eh;
851
852 eh = ext_inode_hdr(inode);
853 eh->eh_depth = 0;
854 eh->eh_entries = 0;
855 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400856 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700857 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700858 return 0;
859}
860
861struct ext4_ext_path *
Theodore Ts'oed8a1a72014-09-01 14:43:09 -0400862ext4_find_extent(struct inode *inode, ext4_lblk_t block,
863 struct ext4_ext_path **orig_path, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700864{
865 struct ext4_extent_header *eh;
866 struct buffer_head *bh;
Theodore Ts'o705912c2014-09-01 14:34:09 -0400867 struct ext4_ext_path *path = orig_path ? *orig_path : NULL;
868 short int depth, i, ppos = 0;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500869 int ret;
Alex Tomasa86c6182006-10-11 01:21:03 -0700870
871 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400872 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700873
Theodore Ts'o10809df82014-09-01 14:40:09 -0400874 if (path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400875 ext4_ext_drop_refs(path);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400876 if (depth > path[0].p_maxdepth) {
877 kfree(path);
878 *orig_path = path = NULL;
879 }
880 }
881 if (!path) {
Theodore Ts'o523f4312014-09-01 14:38:09 -0400882 /* account possible depth increase */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800883 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700884 GFP_NOFS);
Theodore Ts'o19008f62014-08-31 15:03:14 -0400885 if (unlikely(!path))
Alex Tomasa86c6182006-10-11 01:21:03 -0700886 return ERR_PTR(-ENOMEM);
Theodore Ts'o10809df82014-09-01 14:40:09 -0400887 path[0].p_maxdepth = depth + 1;
Alex Tomasa86c6182006-10-11 01:21:03 -0700888 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700889 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400890 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700891
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400892 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700893 /* walk through the tree */
894 while (i) {
895 ext_debug("depth %d: num %d, max %d\n",
896 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400897
Alex Tomasa86c6182006-10-11 01:21:03 -0700898 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400899 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700900 path[ppos].p_depth = i;
901 path[ppos].p_ext = NULL;
902
Theodore Ts'o107a7bd2013-08-16 21:23:41 -0400903 bh = read_extent_tree_block(inode, path[ppos].p_block, --i,
904 flags);
Theodore Ts'o19008f62014-08-31 15:03:14 -0400905 if (unlikely(IS_ERR(bh))) {
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400906 ret = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700907 goto err;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500908 }
Theodore Ts'o7d7ea892013-08-16 21:20:41 -0400909
Alex Tomasa86c6182006-10-11 01:21:03 -0700910 eh = ext_block_hdr(bh);
911 ppos++;
Frank Mayhar273df552010-03-02 11:46:09 -0500912 if (unlikely(ppos > depth)) {
913 put_bh(bh);
914 EXT4_ERROR_INODE(inode,
915 "ppos %d > depth %d", ppos, depth);
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500916 ret = -EIO;
Frank Mayhar273df552010-03-02 11:46:09 -0500917 goto err;
918 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700919 path[ppos].p_bh = bh;
920 path[ppos].p_hdr = eh;
Alex Tomasa86c6182006-10-11 01:21:03 -0700921 }
922
923 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700924 path[ppos].p_ext = NULL;
925 path[ppos].p_idx = NULL;
926
Alex Tomasa86c6182006-10-11 01:21:03 -0700927 /* find extent */
928 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400929 /* if not an empty leaf */
930 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400931 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700932
933 ext4_ext_show_path(inode, path);
934
935 return path;
936
937err:
938 ext4_ext_drop_refs(path);
Theodore Ts'odfe50802014-09-01 14:37:09 -0400939 kfree(path);
940 if (orig_path)
941 *orig_path = NULL;
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500942 return ERR_PTR(ret);
Alex Tomasa86c6182006-10-11 01:21:03 -0700943}
944
945/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700946 * ext4_ext_insert_index:
947 * insert new index [@logical;@ptr] into the block at @curp;
948 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700949 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400950static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
951 struct ext4_ext_path *curp,
952 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700953{
954 struct ext4_extent_idx *ix;
955 int len, err;
956
Avantika Mathur7e028972006-12-06 20:41:33 -0800957 err = ext4_ext_get_access(handle, inode, curp);
958 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700959 return err;
960
Frank Mayhar273df552010-03-02 11:46:09 -0500961 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
962 EXT4_ERROR_INODE(inode,
963 "logical %d == ei_block %d!",
964 logical, le32_to_cpu(curp->p_idx->ei_block));
965 return -EIO;
966 }
Robin Dongd4620312011-07-17 23:43:42 -0400967
968 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
969 >= le16_to_cpu(curp->p_hdr->eh_max))) {
970 EXT4_ERROR_INODE(inode,
971 "eh_entries %d >= eh_max %d!",
972 le16_to_cpu(curp->p_hdr->eh_entries),
973 le16_to_cpu(curp->p_hdr->eh_max));
974 return -EIO;
975 }
976
Alex Tomasa86c6182006-10-11 01:21:03 -0700977 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
978 /* insert after */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400979 ext_debug("insert new index %d after: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700980 ix = curp->p_idx + 1;
981 } else {
982 /* insert before */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400983 ext_debug("insert new index %d before: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700984 ix = curp->p_idx;
985 }
986
Eric Gouriou80e675f2011-10-27 11:52:18 -0400987 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
988 BUG_ON(len < 0);
989 if (len > 0) {
990 ext_debug("insert new index %d: "
991 "move %d indices from 0x%p to 0x%p\n",
992 logical, len, ix, ix + 1);
993 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
994 }
995
Tao Maf472e022011-10-17 10:13:46 -0400996 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
997 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
998 return -EIO;
999 }
1000
Alex Tomasa86c6182006-10-11 01:21:03 -07001001 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001002 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001003 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001004
Frank Mayhar273df552010-03-02 11:46:09 -05001005 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
1006 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
1007 return -EIO;
1008 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001009
1010 err = ext4_ext_dirty(handle, inode, curp);
1011 ext4_std_error(inode->i_sb, err);
1012
1013 return err;
1014}
1015
1016/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001017 * ext4_ext_split:
1018 * inserts new subtree into the path, using free index entry
1019 * at depth @at:
1020 * - allocates all needed blocks (new leaf and all intermediate index blocks)
1021 * - makes decision where to split
1022 * - moves remaining extents and index entries (right to the split point)
1023 * into the newly allocated blocks
1024 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -07001025 */
1026static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001027 unsigned int flags,
1028 struct ext4_ext_path *path,
1029 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -07001030{
1031 struct buffer_head *bh = NULL;
1032 int depth = ext_depth(inode);
1033 struct ext4_extent_header *neh;
1034 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -07001035 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001036 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001037 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001038 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -07001039 int err = 0;
1040
1041 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001042 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07001043
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001044 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -07001045 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -05001046 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
1047 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
1048 return -EIO;
1049 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001050 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
1051 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001052 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -07001053 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001054 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001055 } else {
1056 border = newext->ee_block;
1057 ext_debug("leaf will be added."
1058 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001059 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -07001060 }
1061
1062 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001063 * If error occurs, then we break processing
1064 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -07001065 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001066 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -07001067 */
1068
1069 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001070 * Get array to track all allocated blocks.
1071 * We need this to handle errors and free blocks
1072 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -07001073 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -08001074 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07001075 if (!ablocks)
1076 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001077
1078 /* allocate all needed blocks */
1079 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
1080 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -04001081 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -04001082 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001083 if (newblock == 0)
1084 goto cleanup;
1085 ablocks[a] = newblock;
1086 }
1087
1088 /* initialize new leaf */
1089 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -05001090 if (unlikely(newblock == 0)) {
1091 EXT4_ERROR_INODE(inode, "newblock == 0!");
1092 err = -EIO;
1093 goto cleanup;
1094 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001095 bh = sb_getblk(inode->i_sb, newblock);
Wang Shilongaebf0242013-01-12 16:28:47 -05001096 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001097 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001098 goto cleanup;
1099 }
1100 lock_buffer(bh);
1101
Avantika Mathur7e028972006-12-06 20:41:33 -08001102 err = ext4_journal_get_create_access(handle, bh);
1103 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001104 goto cleanup;
1105
1106 neh = ext_block_hdr(bh);
1107 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001108 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001109 neh->eh_magic = EXT4_EXT_MAGIC;
1110 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001111
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001112 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -05001113 if (unlikely(path[depth].p_hdr->eh_entries !=
1114 path[depth].p_hdr->eh_max)) {
1115 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
1116 path[depth].p_hdr->eh_entries,
1117 path[depth].p_hdr->eh_max);
1118 err = -EIO;
1119 goto cleanup;
1120 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001121 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001122 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
1123 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07001124 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001125 struct ext4_extent *ex;
1126 ex = EXT_FIRST_EXTENT(neh);
1127 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001128 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001129 }
1130
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001131 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001132 set_buffer_uptodate(bh);
1133 unlock_buffer(bh);
1134
Frank Mayhar03901312009-01-07 00:06:22 -05001135 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001136 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001137 goto cleanup;
1138 brelse(bh);
1139 bh = NULL;
1140
1141 /* correct old leaf */
1142 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -08001143 err = ext4_ext_get_access(handle, inode, path + depth);
1144 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001145 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001146 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -08001147 err = ext4_ext_dirty(handle, inode, path + depth);
1148 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001149 goto cleanup;
1150
1151 }
1152
1153 /* create intermediate indexes */
1154 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -05001155 if (unlikely(k < 0)) {
1156 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
1157 err = -EIO;
1158 goto cleanup;
1159 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001160 if (k)
1161 ext_debug("create %d intermediate indices\n", k);
1162 /* insert new index into current index block */
1163 /* current depth stored in i var */
1164 i = depth - 1;
1165 while (k--) {
1166 oldblock = newblock;
1167 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -05001168 bh = sb_getblk(inode->i_sb, newblock);
Wang Shilongaebf0242013-01-12 16:28:47 -05001169 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001170 err = -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001171 goto cleanup;
1172 }
1173 lock_buffer(bh);
1174
Avantika Mathur7e028972006-12-06 20:41:33 -08001175 err = ext4_journal_get_create_access(handle, bh);
1176 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001177 goto cleanup;
1178
1179 neh = ext_block_hdr(bh);
1180 neh->eh_entries = cpu_to_le16(1);
1181 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001182 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001183 neh->eh_depth = cpu_to_le16(depth - i);
1184 fidx = EXT_FIRST_INDEX(neh);
1185 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001186 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001187
Eric Sandeenbba90742008-01-28 23:58:27 -05001188 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
1189 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001190
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001191 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -05001192 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
1193 EXT_LAST_INDEX(path[i].p_hdr))) {
1194 EXT4_ERROR_INODE(inode,
1195 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1196 le32_to_cpu(path[i].p_ext->ee_block));
1197 err = -EIO;
1198 goto cleanup;
1199 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001200 /* start copy indexes */
1201 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
1202 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
1203 EXT_MAX_INDEX(path[i].p_hdr));
1204 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07001205 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001206 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -07001207 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001208 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001209 }
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001210 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001211 set_buffer_uptodate(bh);
1212 unlock_buffer(bh);
1213
Frank Mayhar03901312009-01-07 00:06:22 -05001214 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001215 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001216 goto cleanup;
1217 brelse(bh);
1218 bh = NULL;
1219
1220 /* correct old index */
1221 if (m) {
1222 err = ext4_ext_get_access(handle, inode, path + i);
1223 if (err)
1224 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001225 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001226 err = ext4_ext_dirty(handle, inode, path + i);
1227 if (err)
1228 goto cleanup;
1229 }
1230
1231 i--;
1232 }
1233
1234 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001235 err = ext4_ext_insert_index(handle, inode, path + at,
1236 le32_to_cpu(border), newblock);
1237
1238cleanup:
1239 if (bh) {
1240 if (buffer_locked(bh))
1241 unlock_buffer(bh);
1242 brelse(bh);
1243 }
1244
1245 if (err) {
1246 /* free all allocated blocks in error case */
1247 for (i = 0; i < depth; i++) {
1248 if (!ablocks[i])
1249 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001250 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001251 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001252 }
1253 }
1254 kfree(ablocks);
1255
1256 return err;
1257}
1258
1259/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001260 * ext4_ext_grow_indepth:
1261 * implements tree growing procedure:
1262 * - allocates new block
1263 * - moves top-level data (index block or leaf) into the new block
1264 * - initializes new top-level, creating index that points to the
1265 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001266 */
1267static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001268 unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001269{
Alex Tomasa86c6182006-10-11 01:21:03 -07001270 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001271 struct buffer_head *bh;
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001272 ext4_fsblk_t newblock, goal = 0;
1273 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
Alex Tomasa86c6182006-10-11 01:21:03 -07001274 int err = 0;
1275
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001276 /* Try to prepend new index to old one */
1277 if (ext_depth(inode))
1278 goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode)));
1279 if (goal > le32_to_cpu(es->s_first_data_block)) {
1280 flags |= EXT4_MB_HINT_TRY_GOAL;
1281 goal--;
1282 } else
1283 goal = ext4_inode_to_goal_block(inode);
1284 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
1285 NULL, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07001286 if (newblock == 0)
1287 return err;
1288
1289 bh = sb_getblk(inode->i_sb, newblock);
Wang Shilongaebf0242013-01-12 16:28:47 -05001290 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -05001291 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -07001292 lock_buffer(bh);
1293
Avantika Mathur7e028972006-12-06 20:41:33 -08001294 err = ext4_journal_get_create_access(handle, bh);
1295 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001296 unlock_buffer(bh);
1297 goto out;
1298 }
1299
1300 /* move top-level index/leaf into new block */
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001301 memmove(bh->b_data, EXT4_I(inode)->i_data,
1302 sizeof(EXT4_I(inode)->i_data));
Alex Tomasa86c6182006-10-11 01:21:03 -07001303
1304 /* set size of new block */
1305 neh = ext_block_hdr(bh);
1306 /* old root could have indexes or leaves
1307 * so calculate e_max right way */
1308 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001309 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001310 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001311 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001312 neh->eh_magic = EXT4_EXT_MAGIC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001313 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001314 set_buffer_uptodate(bh);
1315 unlock_buffer(bh);
1316
Frank Mayhar03901312009-01-07 00:06:22 -05001317 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001318 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001319 goto out;
1320
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001321 /* Update top-level index: num,max,pointer */
Alex Tomasa86c6182006-10-11 01:21:03 -07001322 neh = ext_inode_hdr(inode);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001323 neh->eh_entries = cpu_to_le16(1);
1324 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1325 if (neh->eh_depth == 0) {
1326 /* Root extent block becomes index block */
1327 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1328 EXT_FIRST_INDEX(neh)->ei_block =
1329 EXT_FIRST_EXTENT(neh)->ee_block;
1330 }
Mingming Cao2ae02102006-10-11 01:21:11 -07001331 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001332 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001333 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001334 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001335
Wei Yongjunba39ebb2012-09-27 09:37:53 -04001336 le16_add_cpu(&neh->eh_depth, 1);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001337 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07001338out:
1339 brelse(bh);
1340
1341 return err;
1342}
1343
1344/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001345 * ext4_ext_create_new_leaf:
1346 * finds empty index and adds new leaf.
1347 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001348 */
1349static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001350 unsigned int mb_flags,
1351 unsigned int gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001352 struct ext4_ext_path **ppath,
Allison Henderson55f020d2011-05-25 07:41:26 -04001353 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001354{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001355 struct ext4_ext_path *path = *ppath;
Alex Tomasa86c6182006-10-11 01:21:03 -07001356 struct ext4_ext_path *curp;
1357 int depth, i, err = 0;
1358
1359repeat:
1360 i = depth = ext_depth(inode);
1361
1362 /* walk up to the tree and look for free index entry */
1363 curp = path + depth;
1364 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1365 i--;
1366 curp--;
1367 }
1368
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001369 /* we use already allocated block for index block,
1370 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001371 if (EXT_HAS_FREE_INDEX(curp)) {
1372 /* if we found index with free entry, then use that
1373 * entry: create all needed subtree and add new leaf */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001374 err = ext4_ext_split(handle, inode, mb_flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001375 if (err)
1376 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001377
1378 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001379 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001380 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001381 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001382 if (IS_ERR(path))
1383 err = PTR_ERR(path);
1384 } else {
1385 /* tree is full, time to grow in depth */
Dmitry Monakhovbe5cd902014-10-01 22:57:09 -04001386 err = ext4_ext_grow_indepth(handle, inode, mb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001387 if (err)
1388 goto out;
1389
1390 /* refill path */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001391 path = ext4_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001392 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
Theodore Ts'odfe50802014-09-01 14:37:09 -04001393 ppath, gb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001394 if (IS_ERR(path)) {
1395 err = PTR_ERR(path);
1396 goto out;
1397 }
1398
1399 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001400 * only first (depth 0 -> 1) produces free space;
1401 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001402 */
1403 depth = ext_depth(inode);
1404 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001405 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001406 goto repeat;
1407 }
1408 }
1409
1410out:
1411 return err;
1412}
1413
1414/*
Alex Tomas1988b512008-01-28 23:58:27 -05001415 * search the closest allocated block to the left for *logical
1416 * and returns it at @logical + it's physical address at @phys
1417 * if *logical is the smallest allocated block, the function
1418 * returns 0 at @phys
1419 * return value contains 0 (success) or error code
1420 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001421static int ext4_ext_search_left(struct inode *inode,
1422 struct ext4_ext_path *path,
1423 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001424{
1425 struct ext4_extent_idx *ix;
1426 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001427 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001428
Frank Mayhar273df552010-03-02 11:46:09 -05001429 if (unlikely(path == NULL)) {
1430 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1431 return -EIO;
1432 }
Alex Tomas1988b512008-01-28 23:58:27 -05001433 depth = path->p_depth;
1434 *phys = 0;
1435
1436 if (depth == 0 && path->p_ext == NULL)
1437 return 0;
1438
1439 /* usually extent in the path covers blocks smaller
1440 * then *logical, but it can be that extent is the
1441 * first one in the file */
1442
1443 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001444 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001445 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001446 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1447 EXT4_ERROR_INODE(inode,
1448 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1449 *logical, le32_to_cpu(ex->ee_block));
1450 return -EIO;
1451 }
Alex Tomas1988b512008-01-28 23:58:27 -05001452 while (--depth >= 0) {
1453 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001454 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1455 EXT4_ERROR_INODE(inode,
1456 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
Tao Ma6ee3b212011-10-08 16:08:34 -04001457 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001458 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
Tao Ma6ee3b212011-10-08 16:08:34 -04001459 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001460 depth);
1461 return -EIO;
1462 }
Alex Tomas1988b512008-01-28 23:58:27 -05001463 }
1464 return 0;
1465 }
1466
Frank Mayhar273df552010-03-02 11:46:09 -05001467 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1468 EXT4_ERROR_INODE(inode,
1469 "logical %d < ee_block %d + ee_len %d!",
1470 *logical, le32_to_cpu(ex->ee_block), ee_len);
1471 return -EIO;
1472 }
Alex Tomas1988b512008-01-28 23:58:27 -05001473
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001474 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001475 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001476 return 0;
1477}
1478
1479/*
1480 * search the closest allocated block to the right for *logical
1481 * and returns it at @logical + it's physical address at @phys
Tao Madf3ab172011-10-08 15:53:49 -04001482 * if *logical is the largest allocated block, the function
Alex Tomas1988b512008-01-28 23:58:27 -05001483 * returns 0 at @phys
1484 * return value contains 0 (success) or error code
1485 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001486static int ext4_ext_search_right(struct inode *inode,
1487 struct ext4_ext_path *path,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001488 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1489 struct ext4_extent **ret_ex)
Alex Tomas1988b512008-01-28 23:58:27 -05001490{
1491 struct buffer_head *bh = NULL;
1492 struct ext4_extent_header *eh;
1493 struct ext4_extent_idx *ix;
1494 struct ext4_extent *ex;
1495 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001496 int depth; /* Note, NOT eh_depth; depth from top of tree */
1497 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001498
Frank Mayhar273df552010-03-02 11:46:09 -05001499 if (unlikely(path == NULL)) {
1500 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1501 return -EIO;
1502 }
Alex Tomas1988b512008-01-28 23:58:27 -05001503 depth = path->p_depth;
1504 *phys = 0;
1505
1506 if (depth == 0 && path->p_ext == NULL)
1507 return 0;
1508
1509 /* usually extent in the path covers blocks smaller
1510 * then *logical, but it can be that extent is the
1511 * first one in the file */
1512
1513 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001514 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001515 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001516 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1517 EXT4_ERROR_INODE(inode,
1518 "first_extent(path[%d].p_hdr) != ex",
1519 depth);
1520 return -EIO;
1521 }
Alex Tomas1988b512008-01-28 23:58:27 -05001522 while (--depth >= 0) {
1523 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001524 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1525 EXT4_ERROR_INODE(inode,
1526 "ix != EXT_FIRST_INDEX *logical %d!",
1527 *logical);
1528 return -EIO;
1529 }
Alex Tomas1988b512008-01-28 23:58:27 -05001530 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001531 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001532 }
1533
Frank Mayhar273df552010-03-02 11:46:09 -05001534 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1535 EXT4_ERROR_INODE(inode,
1536 "logical %d < ee_block %d + ee_len %d!",
1537 *logical, le32_to_cpu(ex->ee_block), ee_len);
1538 return -EIO;
1539 }
Alex Tomas1988b512008-01-28 23:58:27 -05001540
1541 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1542 /* next allocated block in this leaf */
1543 ex++;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001544 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001545 }
1546
1547 /* go up and search for index to the right */
1548 while (--depth >= 0) {
1549 ix = path[depth].p_idx;
1550 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001551 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001552 }
1553
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001554 /* we've gone up to the root and found no index to the right */
1555 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001556
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001557got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001558 /* we've found index to the right, let's
1559 * follow it and find the closest allocated
1560 * block to the right */
1561 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001562 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001563 while (++depth < path->p_depth) {
Eric Sandeen395a87b2009-03-10 18:18:47 -04001564 /* subtract from p_depth to get proper eh_depth */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001565 bh = read_extent_tree_block(inode, block,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001566 path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001567 if (IS_ERR(bh))
1568 return PTR_ERR(bh);
1569 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001570 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001571 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001572 put_bh(bh);
1573 }
1574
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001575 bh = read_extent_tree_block(inode, block, path->p_depth - depth, 0);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04001576 if (IS_ERR(bh))
1577 return PTR_ERR(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001578 eh = ext_block_hdr(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001579 ex = EXT_FIRST_EXTENT(eh);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001580found_extent:
Alex Tomas1988b512008-01-28 23:58:27 -05001581 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001582 *phys = ext4_ext_pblock(ex);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001583 *ret_ex = ex;
1584 if (bh)
1585 put_bh(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001586 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001587}
1588
1589/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001590 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001591 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001592 * NOTE: it considers block number from index entry as
1593 * allocated block. Thus, index entries have to be consistent
1594 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001595 */
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04001596ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001597ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1598{
1599 int depth;
1600
1601 BUG_ON(path == NULL);
1602 depth = path->p_depth;
1603
1604 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001605 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001606
1607 while (depth >= 0) {
1608 if (depth == path->p_depth) {
1609 /* leaf */
Curt Wohlgemuth6f8ff532011-10-26 04:38:59 -04001610 if (path[depth].p_ext &&
1611 path[depth].p_ext !=
Alex Tomasa86c6182006-10-11 01:21:03 -07001612 EXT_LAST_EXTENT(path[depth].p_hdr))
1613 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1614 } else {
1615 /* index */
1616 if (path[depth].p_idx !=
1617 EXT_LAST_INDEX(path[depth].p_hdr))
1618 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1619 }
1620 depth--;
1621 }
1622
Lukas Czernerf17722f2011-06-06 00:05:17 -04001623 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001624}
1625
1626/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001627 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001628 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001629 */
Robin Dong57187892011-07-23 21:49:07 -04001630static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001631{
1632 int depth;
1633
1634 BUG_ON(path == NULL);
1635 depth = path->p_depth;
1636
1637 /* zero-tree has no leaf blocks at all */
1638 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001639 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001640
1641 /* go to index block */
1642 depth--;
1643
1644 while (depth >= 0) {
1645 if (path[depth].p_idx !=
1646 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001647 return (ext4_lblk_t)
1648 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001649 depth--;
1650 }
1651
Lukas Czernerf17722f2011-06-06 00:05:17 -04001652 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001653}
1654
1655/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001656 * ext4_ext_correct_indexes:
1657 * if leaf gets modified and modified extent is first in the leaf,
1658 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001659 * TODO: do we need to correct tree in all cases?
1660 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001661static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001662 struct ext4_ext_path *path)
1663{
1664 struct ext4_extent_header *eh;
1665 int depth = ext_depth(inode);
1666 struct ext4_extent *ex;
1667 __le32 border;
1668 int k, err = 0;
1669
1670 eh = path[depth].p_hdr;
1671 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001672
1673 if (unlikely(ex == NULL || eh == NULL)) {
1674 EXT4_ERROR_INODE(inode,
1675 "ex %p == NULL or eh %p == NULL", ex, eh);
1676 return -EIO;
1677 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001678
1679 if (depth == 0) {
1680 /* there is no tree at all */
1681 return 0;
1682 }
1683
1684 if (ex != EXT_FIRST_EXTENT(eh)) {
1685 /* we correct tree if first leaf got modified only */
1686 return 0;
1687 }
1688
1689 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001690 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001691 */
1692 k = depth - 1;
1693 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001694 err = ext4_ext_get_access(handle, inode, path + k);
1695 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001696 return err;
1697 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001698 err = ext4_ext_dirty(handle, inode, path + k);
1699 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001700 return err;
1701
1702 while (k--) {
1703 /* change all left-side indexes */
1704 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1705 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001706 err = ext4_ext_get_access(handle, inode, path + k);
1707 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001708 break;
1709 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001710 err = ext4_ext_dirty(handle, inode, path + k);
1711 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001712 break;
1713 }
1714
1715 return err;
1716}
1717
Akira Fujita748de672009-06-17 19:24:03 -04001718int
Alex Tomasa86c6182006-10-11 01:21:03 -07001719ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1720 struct ext4_extent *ex2)
1721{
Eric Sandeenda0169b2013-11-04 09:58:26 -05001722 unsigned short ext1_ee_len, ext2_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001723
1724 /*
Dmitry Monakhovec22ba82013-03-04 00:36:06 -05001725 * Make sure that both extents are initialized. We don't merge
Lukas Czerner556615d2014-04-20 23:45:47 -04001726 * unwritten extents so that we can be sure that end_io code has
Dmitry Monakhovec22ba82013-03-04 00:36:06 -05001727 * the extent that was written properly split out and conversion to
1728 * initialized is trivial.
Amit Aroraa2df2a62007-07-17 21:42:41 -04001729 */
Lukas Czerner556615d2014-04-20 23:45:47 -04001730 if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2))
Amit Aroraa2df2a62007-07-17 21:42:41 -04001731 return 0;
1732
1733 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1734 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1735
1736 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001737 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001738 return 0;
1739
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001740 /*
1741 * To allow future support for preallocated extents to be added
1742 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001743 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001744 */
Eric Sandeenda0169b2013-11-04 09:58:26 -05001745 if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001746 return 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04001747 if (ext4_ext_is_unwritten(ex1) &&
Darrick J. Wonga9b82412014-02-20 21:17:35 -05001748 (ext4_test_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN) ||
1749 atomic_read(&EXT4_I(inode)->i_unwritten) ||
Lukas Czerner556615d2014-04-20 23:45:47 -04001750 (ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN)))
Darrick J. Wonga9b82412014-02-20 21:17:35 -05001751 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001752#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001753 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001754 return 0;
1755#endif
1756
Theodore Ts'obf89d162010-10-27 21:30:14 -04001757 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001758 return 1;
1759 return 0;
1760}
1761
1762/*
Amit Arora56055d32007-07-17 21:42:38 -04001763 * This function tries to merge the "ex" extent to the next extent in the tree.
1764 * It always tries to merge towards right. If you want to merge towards
1765 * left, pass "ex - 1" as argument instead of "ex".
1766 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1767 * 1 if they got merged.
1768 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001769static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001770 struct ext4_ext_path *path,
1771 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001772{
1773 struct ext4_extent_header *eh;
1774 unsigned int depth, len;
Lukas Czerner556615d2014-04-20 23:45:47 -04001775 int merge_done = 0, unwritten;
Amit Arora56055d32007-07-17 21:42:38 -04001776
1777 depth = ext_depth(inode);
1778 BUG_ON(path[depth].p_hdr == NULL);
1779 eh = path[depth].p_hdr;
1780
1781 while (ex < EXT_LAST_EXTENT(eh)) {
1782 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1783 break;
1784 /* merge with next extent! */
Lukas Czerner556615d2014-04-20 23:45:47 -04001785 unwritten = ext4_ext_is_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001786 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1787 + ext4_ext_get_actual_len(ex + 1));
Lukas Czerner556615d2014-04-20 23:45:47 -04001788 if (unwritten)
1789 ext4_ext_mark_unwritten(ex);
Amit Arora56055d32007-07-17 21:42:38 -04001790
1791 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1792 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1793 * sizeof(struct ext4_extent);
1794 memmove(ex + 1, ex + 2, len);
1795 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001796 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001797 merge_done = 1;
1798 WARN_ON(eh->eh_entries == 0);
1799 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001800 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001801 }
1802
1803 return merge_done;
1804}
1805
1806/*
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001807 * This function does a very simple check to see if we can collapse
1808 * an extent tree with a single extent tree leaf block into the inode.
1809 */
1810static void ext4_ext_try_to_merge_up(handle_t *handle,
1811 struct inode *inode,
1812 struct ext4_ext_path *path)
1813{
1814 size_t s;
1815 unsigned max_root = ext4_ext_space_root(inode, 0);
1816 ext4_fsblk_t blk;
1817
1818 if ((path[0].p_depth != 1) ||
1819 (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) ||
1820 (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root))
1821 return;
1822
1823 /*
1824 * We need to modify the block allocation bitmap and the block
1825 * group descriptor to release the extent tree block. If we
1826 * can't get the journal credits, give up.
1827 */
1828 if (ext4_journal_extend(handle, 2))
1829 return;
1830
1831 /*
1832 * Copy the extent data up to the inode
1833 */
1834 blk = ext4_idx_pblock(path[0].p_idx);
1835 s = le16_to_cpu(path[1].p_hdr->eh_entries) *
1836 sizeof(struct ext4_extent_idx);
1837 s += sizeof(struct ext4_extent_header);
1838
Theodore Ts'o10809df82014-09-01 14:40:09 -04001839 path[1].p_maxdepth = path[0].p_maxdepth;
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001840 memcpy(path[0].p_hdr, path[1].p_hdr, s);
1841 path[0].p_depth = 0;
1842 path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) +
1843 (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr));
1844 path[0].p_hdr->eh_max = cpu_to_le16(max_root);
1845
1846 brelse(path[1].p_bh);
1847 ext4_free_blocks(handle, inode, NULL, blk, 1,
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04001848 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001849}
1850
1851/*
Yongqiang Yang197217a2011-05-03 11:45:29 -04001852 * This function tries to merge the @ex extent to neighbours in the tree.
1853 * return 1 if merge left else 0.
1854 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001855static void ext4_ext_try_to_merge(handle_t *handle,
1856 struct inode *inode,
Yongqiang Yang197217a2011-05-03 11:45:29 -04001857 struct ext4_ext_path *path,
1858 struct ext4_extent *ex) {
1859 struct ext4_extent_header *eh;
1860 unsigned int depth;
1861 int merge_done = 0;
Yongqiang Yang197217a2011-05-03 11:45:29 -04001862
1863 depth = ext_depth(inode);
1864 BUG_ON(path[depth].p_hdr == NULL);
1865 eh = path[depth].p_hdr;
1866
1867 if (ex > EXT_FIRST_EXTENT(eh))
1868 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1869
1870 if (!merge_done)
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001871 (void) ext4_ext_try_to_merge_right(inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001872
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001873 ext4_ext_try_to_merge_up(handle, inode, path);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001874}
1875
1876/*
Amit Arora25d14f92007-05-24 13:04:13 -04001877 * check if a portion of the "newext" extent overlaps with an
1878 * existing extent.
1879 *
1880 * If there is an overlap discovered, it updates the length of the newext
1881 * such that there will be no overlap, and then returns 1.
1882 * If there is no overlap found, it returns 0.
1883 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001884static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1885 struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001886 struct ext4_extent *newext,
1887 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001888{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001889 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001890 unsigned int depth, len1;
1891 unsigned int ret = 0;
1892
1893 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001894 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001895 depth = ext_depth(inode);
1896 if (!path[depth].p_ext)
1897 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001898 b2 = EXT4_LBLK_CMASK(sbi, le32_to_cpu(path[depth].p_ext->ee_block));
Amit Arora25d14f92007-05-24 13:04:13 -04001899
1900 /*
1901 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001902 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001903 */
1904 if (b2 < b1) {
1905 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001906 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001907 goto out;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05001908 b2 = EXT4_LBLK_CMASK(sbi, b2);
Amit Arora25d14f92007-05-24 13:04:13 -04001909 }
1910
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001911 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001912 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001913 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001914 newext->ee_len = cpu_to_le16(len1);
1915 ret = 1;
1916 }
1917
1918 /* check for overlap */
1919 if (b1 + len1 > b2) {
1920 newext->ee_len = cpu_to_le16(b2 - b1);
1921 ret = 1;
1922 }
1923out:
1924 return ret;
1925}
1926
1927/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001928 * ext4_ext_insert_extent:
1929 * tries to merge requsted extent into the existing extent or
1930 * inserts requested extent as new one into the tree,
1931 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001932 */
1933int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04001934 struct ext4_ext_path **ppath,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001935 struct ext4_extent *newext, int gb_flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07001936{
Theodore Ts'odfe50802014-09-01 14:37:09 -04001937 struct ext4_ext_path *path = *ppath;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001938 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001939 struct ext4_extent *ex, *fex;
1940 struct ext4_extent *nearex; /* nearest extent */
1941 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001942 int depth, len, err;
1943 ext4_lblk_t next;
Lukas Czerner556615d2014-04-20 23:45:47 -04001944 int mb_flags = 0, unwritten;
Alex Tomasa86c6182006-10-11 01:21:03 -07001945
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04001946 if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
1947 mb_flags |= EXT4_MB_DELALLOC_RESERVED;
Frank Mayhar273df552010-03-02 11:46:09 -05001948 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1949 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1950 return -EIO;
1951 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001952 depth = ext_depth(inode);
1953 ex = path[depth].p_ext;
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001954 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05001955 if (unlikely(path[depth].p_hdr == NULL)) {
1956 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1957 return -EIO;
1958 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001959
1960 /* try to insert block into found extent and return */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04001961 if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04001962
1963 /*
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001964 * Try to see whether we should rather test the extent on
1965 * right from ex, or from the left of ex. This is because
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04001966 * ext4_find_extent() can return either extent on the
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001967 * left, or on the right from the searched position. This
1968 * will make merging more effective.
Amit Aroraa2df2a62007-07-17 21:42:41 -04001969 */
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001970 if (ex < EXT_LAST_EXTENT(eh) &&
1971 (le32_to_cpu(ex->ee_block) +
1972 ext4_ext_get_actual_len(ex) <
1973 le32_to_cpu(newext->ee_block))) {
1974 ex += 1;
1975 goto prepend;
1976 } else if ((ex > EXT_FIRST_EXTENT(eh)) &&
1977 (le32_to_cpu(newext->ee_block) +
1978 ext4_ext_get_actual_len(newext) <
1979 le32_to_cpu(ex->ee_block)))
1980 ex -= 1;
1981
1982 /* Try to append newex to the ex */
1983 if (ext4_can_extents_be_merged(inode, ex, newext)) {
1984 ext_debug("append [%d]%d block to %u:[%d]%d"
1985 "(from %llu)\n",
Lukas Czerner556615d2014-04-20 23:45:47 -04001986 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001987 ext4_ext_get_actual_len(newext),
1988 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04001989 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001990 ext4_ext_get_actual_len(ex),
1991 ext4_ext_pblock(ex));
1992 err = ext4_ext_get_access(handle, inode,
1993 path + depth);
1994 if (err)
1995 return err;
Lukas Czerner556615d2014-04-20 23:45:47 -04001996 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04001997 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001998 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04001999 if (unwritten)
2000 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002001 eh = path[depth].p_hdr;
2002 nearex = ex;
2003 goto merge;
2004 }
2005
2006prepend:
2007 /* Try to prepend newex to the ex */
2008 if (ext4_can_extents_be_merged(inode, newext, ex)) {
2009 ext_debug("prepend %u[%d]%d block to %u:[%d]%d"
2010 "(from %llu)\n",
2011 le32_to_cpu(newext->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04002012 ext4_ext_is_unwritten(newext),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002013 ext4_ext_get_actual_len(newext),
2014 le32_to_cpu(ex->ee_block),
Lukas Czerner556615d2014-04-20 23:45:47 -04002015 ext4_ext_is_unwritten(ex),
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002016 ext4_ext_get_actual_len(ex),
2017 ext4_ext_pblock(ex));
2018 err = ext4_ext_get_access(handle, inode,
2019 path + depth);
2020 if (err)
2021 return err;
2022
Lukas Czerner556615d2014-04-20 23:45:47 -04002023 unwritten = ext4_ext_is_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002024 ex->ee_block = newext->ee_block;
2025 ext4_ext_store_pblock(ex, ext4_ext_pblock(newext));
2026 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
2027 + ext4_ext_get_actual_len(newext));
Lukas Czerner556615d2014-04-20 23:45:47 -04002028 if (unwritten)
2029 ext4_ext_mark_unwritten(ex);
Lukas Czernerbe8981b2013-04-03 23:33:28 -04002030 eh = path[depth].p_hdr;
2031 nearex = ex;
2032 goto merge;
2033 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002034 }
2035
Alex Tomasa86c6182006-10-11 01:21:03 -07002036 depth = ext_depth(inode);
2037 eh = path[depth].p_hdr;
2038 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
2039 goto has_space;
2040
2041 /* probably next leaf has space for us? */
2042 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04002043 next = EXT_MAX_BLOCKS;
2044 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
Robin Dong57187892011-07-23 21:49:07 -04002045 next = ext4_ext_next_leaf_block(path);
Robin Dong598dbdf2011-07-11 18:24:01 -04002046 if (next != EXT_MAX_BLOCKS) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04002047 ext_debug("next leaf block - %u\n", next);
Alex Tomasa86c6182006-10-11 01:21:03 -07002048 BUG_ON(npath != NULL);
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002049 npath = ext4_find_extent(inode, next, NULL, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002050 if (IS_ERR(npath))
2051 return PTR_ERR(npath);
2052 BUG_ON(npath->p_depth != path->p_depth);
2053 eh = npath[depth].p_hdr;
2054 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002055 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002056 le16_to_cpu(eh->eh_entries));
2057 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04002058 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07002059 }
2060 ext_debug("next leaf has no free space(%d,%d)\n",
2061 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
2062 }
2063
2064 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002065 * There is no free space in the found leaf.
2066 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07002067 */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002068 if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04002069 mb_flags |= EXT4_MB_USE_RESERVED;
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002070 err = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags,
Theodore Ts'odfe50802014-09-01 14:37:09 -04002071 ppath, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07002072 if (err)
2073 goto cleanup;
2074 depth = ext_depth(inode);
2075 eh = path[depth].p_hdr;
2076
2077has_space:
2078 nearex = path[depth].p_ext;
2079
Avantika Mathur7e028972006-12-06 20:41:33 -08002080 err = ext4_ext_get_access(handle, inode, path + depth);
2081 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002082 goto cleanup;
2083
2084 if (!nearex) {
2085 /* there is no extent in this leaf, create first one */
Yongqiang Yang32de6752011-11-01 18:56:41 -04002086 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002087 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002088 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002089 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002090 ext4_ext_get_actual_len(newext));
Eric Gouriou80e675f2011-10-27 11:52:18 -04002091 nearex = EXT_FIRST_EXTENT(eh);
2092 } else {
2093 if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002094 > le32_to_cpu(nearex->ee_block)) {
Eric Gouriou80e675f2011-10-27 11:52:18 -04002095 /* Insert after */
Yongqiang Yang32de6752011-11-01 18:56:41 -04002096 ext_debug("insert %u:%llu:[%d]%d before: "
2097 "nearest %p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002098 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04002099 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002100 ext4_ext_is_unwritten(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04002101 ext4_ext_get_actual_len(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002102 nearex);
2103 nearex++;
2104 } else {
2105 /* Insert before */
2106 BUG_ON(newext->ee_block == nearex->ee_block);
Yongqiang Yang32de6752011-11-01 18:56:41 -04002107 ext_debug("insert %u:%llu:[%d]%d after: "
2108 "nearest %p\n",
Eric Gouriou80e675f2011-10-27 11:52:18 -04002109 le32_to_cpu(newext->ee_block),
2110 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002111 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002112 ext4_ext_get_actual_len(newext),
2113 nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002114 }
Eric Gouriou80e675f2011-10-27 11:52:18 -04002115 len = EXT_LAST_EXTENT(eh) - nearex + 1;
2116 if (len > 0) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04002117 ext_debug("insert %u:%llu:[%d]%d: "
Eric Gouriou80e675f2011-10-27 11:52:18 -04002118 "move %d extents from 0x%p to 0x%p\n",
2119 le32_to_cpu(newext->ee_block),
2120 ext4_ext_pblock(newext),
Lukas Czerner556615d2014-04-20 23:45:47 -04002121 ext4_ext_is_unwritten(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04002122 ext4_ext_get_actual_len(newext),
2123 len, nearex, nearex + 1);
2124 memmove(nearex + 1, nearex,
2125 len * sizeof(struct ext4_extent));
2126 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002127 }
2128
Marcin Slusarze8546d02008-04-17 10:38:59 -04002129 le16_add_cpu(&eh->eh_entries, 1);
Eric Gouriou80e675f2011-10-27 11:52:18 -04002130 path[depth].p_ext = nearex;
Alex Tomasa86c6182006-10-11 01:21:03 -07002131 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002132 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07002133 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07002134
2135merge:
HaiboLiue7bcf822012-07-09 16:29:28 -04002136 /* try to merge extents */
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002137 if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002138 ext4_ext_try_to_merge(handle, inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002139
Alex Tomasa86c6182006-10-11 01:21:03 -07002140
2141 /* time to correct all indexes above */
2142 err = ext4_ext_correct_indexes(handle, inode, path);
2143 if (err)
2144 goto cleanup;
2145
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002146 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002147
2148cleanup:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04002149 ext4_ext_drop_refs(npath);
2150 kfree(npath);
Alex Tomasa86c6182006-10-11 01:21:03 -07002151 return err;
2152}
2153
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002154static int ext4_fill_fiemap_extents(struct inode *inode,
2155 ext4_lblk_t block, ext4_lblk_t num,
2156 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04002157{
2158 struct ext4_ext_path *path = NULL;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002159 struct ext4_extent *ex;
Zheng Liu69eb33d2013-02-18 00:31:07 -05002160 struct extent_status es;
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002161 ext4_lblk_t next, next_del, start = 0, end = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002162 ext4_lblk_t last = block + num;
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002163 int exists, depth = 0, err = 0;
2164 unsigned int flags = 0;
2165 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002166
Lukas Czernerf17722f2011-06-06 00:05:17 -04002167 while (block < last && block != EXT_MAX_BLOCKS) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04002168 num = last - block;
2169 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05002170 down_read(&EXT4_I(inode)->i_data_sem);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002171
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002172 path = ext4_find_extent(inode, block, &path, 0);
Eric Sandeen6873fa02008-10-07 00:46:36 -04002173 if (IS_ERR(path)) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002174 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04002175 err = PTR_ERR(path);
2176 path = NULL;
2177 break;
2178 }
2179
2180 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05002181 if (unlikely(path[depth].p_hdr == NULL)) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002182 up_read(&EXT4_I(inode)->i_data_sem);
Frank Mayhar273df552010-03-02 11:46:09 -05002183 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2184 err = -EIO;
2185 break;
2186 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002187 ex = path[depth].p_ext;
2188 next = ext4_ext_next_allocated_block(path);
2189
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002190 flags = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002191 exists = 0;
2192 if (!ex) {
2193 /* there is no extent yet, so try to allocate
2194 * all requested space */
2195 start = block;
2196 end = block + num;
2197 } else if (le32_to_cpu(ex->ee_block) > block) {
2198 /* need to allocate space before found extent */
2199 start = block;
2200 end = le32_to_cpu(ex->ee_block);
2201 if (block + num < end)
2202 end = block + num;
2203 } else if (block >= le32_to_cpu(ex->ee_block)
2204 + ext4_ext_get_actual_len(ex)) {
2205 /* need to allocate space after found extent */
2206 start = block;
2207 end = block + num;
2208 if (end >= next)
2209 end = next;
2210 } else if (block >= le32_to_cpu(ex->ee_block)) {
2211 /*
2212 * some part of requested space is covered
2213 * by found extent
2214 */
2215 start = block;
2216 end = le32_to_cpu(ex->ee_block)
2217 + ext4_ext_get_actual_len(ex);
2218 if (block + num < end)
2219 end = block + num;
2220 exists = 1;
2221 } else {
2222 BUG();
2223 }
2224 BUG_ON(end <= start);
2225
2226 if (!exists) {
Zheng Liu69eb33d2013-02-18 00:31:07 -05002227 es.es_lblk = start;
2228 es.es_len = end - start;
2229 es.es_pblk = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002230 } else {
Zheng Liu69eb33d2013-02-18 00:31:07 -05002231 es.es_lblk = le32_to_cpu(ex->ee_block);
2232 es.es_len = ext4_ext_get_actual_len(ex);
2233 es.es_pblk = ext4_ext_pblock(ex);
Lukas Czerner556615d2014-04-20 23:45:47 -04002234 if (ext4_ext_is_unwritten(ex))
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002235 flags |= FIEMAP_EXTENT_UNWRITTEN;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002236 }
2237
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002238 /*
Zheng Liu69eb33d2013-02-18 00:31:07 -05002239 * Find delayed extent and update es accordingly. We call
2240 * it even in !exists case to find out whether es is the
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002241 * last existing extent or not.
2242 */
Zheng Liu69eb33d2013-02-18 00:31:07 -05002243 next_del = ext4_find_delayed_extent(inode, &es);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002244 if (!exists && next_del) {
2245 exists = 1;
Jie Liu72dac952013-06-12 23:13:59 -04002246 flags |= (FIEMAP_EXTENT_DELALLOC |
2247 FIEMAP_EXTENT_UNKNOWN);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002248 }
2249 up_read(&EXT4_I(inode)->i_data_sem);
2250
Zheng Liu69eb33d2013-02-18 00:31:07 -05002251 if (unlikely(es.es_len == 0)) {
2252 EXT4_ERROR_INODE(inode, "es.es_len == 0");
Frank Mayhar273df552010-03-02 11:46:09 -05002253 err = -EIO;
2254 break;
2255 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002256
Zheng Liuf7fec032013-02-18 00:28:47 -05002257 /*
2258 * This is possible iff next == next_del == EXT_MAX_BLOCKS.
2259 * we need to check next == EXT_MAX_BLOCKS because it is
2260 * possible that an extent is with unwritten and delayed
2261 * status due to when an extent is delayed allocated and
2262 * is allocated by fallocate status tree will track both of
2263 * them in a extent.
2264 *
2265 * So we could return a unwritten and delayed extent, and
2266 * its block is equal to 'next'.
2267 */
2268 if (next == next_del && next == EXT_MAX_BLOCKS) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002269 flags |= FIEMAP_EXTENT_LAST;
2270 if (unlikely(next_del != EXT_MAX_BLOCKS ||
2271 next != EXT_MAX_BLOCKS)) {
2272 EXT4_ERROR_INODE(inode,
2273 "next extent == %u, next "
2274 "delalloc extent = %u",
2275 next, next_del);
2276 err = -EIO;
2277 break;
2278 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002279 }
2280
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002281 if (exists) {
2282 err = fiemap_fill_next_extent(fieinfo,
Zheng Liu69eb33d2013-02-18 00:31:07 -05002283 (__u64)es.es_lblk << blksize_bits,
2284 (__u64)es.es_pblk << blksize_bits,
2285 (__u64)es.es_len << blksize_bits,
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002286 flags);
2287 if (err < 0)
2288 break;
2289 if (err == 1) {
2290 err = 0;
2291 break;
2292 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002293 }
2294
Zheng Liu69eb33d2013-02-18 00:31:07 -05002295 block = es.es_lblk + es.es_len;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002296 }
2297
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04002298 ext4_ext_drop_refs(path);
2299 kfree(path);
Eric Sandeen6873fa02008-10-07 00:46:36 -04002300 return err;
2301}
2302
Alex Tomasa86c6182006-10-11 01:21:03 -07002303/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002304 * ext4_ext_put_gap_in_cache:
2305 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07002306 * and cache this gap
2307 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002308static void
Alex Tomasa86c6182006-10-11 01:21:03 -07002309ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002310 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07002311{
2312 int depth = ext_depth(inode);
Andi Shyti27b1b222013-08-28 14:00:00 -04002313 unsigned long len = 0;
2314 ext4_lblk_t lblock = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002315 struct ext4_extent *ex;
2316
2317 ex = path[depth].p_ext;
2318 if (ex == NULL) {
Zheng Liu69eb33d2013-02-18 00:31:07 -05002319 /*
2320 * there is no extent yet, so gap is [0;-] and we
2321 * don't cache it
2322 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002323 ext_debug("cache gap(whole file):");
2324 } else if (block < le32_to_cpu(ex->ee_block)) {
2325 lblock = block;
2326 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05002327 ext_debug("cache gap(before): %u [%u:%u]",
2328 block,
2329 le32_to_cpu(ex->ee_block),
2330 ext4_ext_get_actual_len(ex));
Zheng Liud100eef2013-02-18 00:29:59 -05002331 if (!ext4_find_delalloc_range(inode, lblock, lblock + len - 1))
2332 ext4_es_insert_extent(inode, lblock, len, ~0,
2333 EXTENT_STATUS_HOLE);
Alex Tomasa86c6182006-10-11 01:21:03 -07002334 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002335 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002336 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002337 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002338 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002339
2340 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05002341 ext_debug("cache gap(after): [%u:%u] %u",
2342 le32_to_cpu(ex->ee_block),
2343 ext4_ext_get_actual_len(ex),
2344 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002345 BUG_ON(next == lblock);
2346 len = next - lblock;
Zheng Liud100eef2013-02-18 00:29:59 -05002347 if (!ext4_find_delalloc_range(inode, lblock, lblock + len - 1))
2348 ext4_es_insert_extent(inode, lblock, len, ~0,
2349 EXTENT_STATUS_HOLE);
Alex Tomasa86c6182006-10-11 01:21:03 -07002350 } else {
Alex Tomasa86c6182006-10-11 01:21:03 -07002351 BUG();
2352 }
2353
Eric Sandeenbba90742008-01-28 23:58:27 -05002354 ext_debug(" -> %u:%lu\n", lblock, len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002355}
2356
2357/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002358 * ext4_ext_rm_idx:
2359 * removes index from the index block.
Alex Tomasa86c6182006-10-11 01:21:03 -07002360 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002361static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Forrest Liuc36575e2012-12-17 09:55:39 -05002362 struct ext4_ext_path *path, int depth)
Alex Tomasa86c6182006-10-11 01:21:03 -07002363{
Alex Tomasa86c6182006-10-11 01:21:03 -07002364 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002365 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002366
2367 /* free index block */
Forrest Liuc36575e2012-12-17 09:55:39 -05002368 depth--;
2369 path = path + depth;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002370 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002371 if (unlikely(path->p_hdr->eh_entries == 0)) {
2372 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2373 return -EIO;
2374 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002375 err = ext4_ext_get_access(handle, inode, path);
2376 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002377 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002378
2379 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2380 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2381 len *= sizeof(struct ext4_extent_idx);
2382 memmove(path->p_idx, path->p_idx + 1, len);
2383 }
2384
Marcin Slusarze8546d02008-04-17 10:38:59 -04002385 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002386 err = ext4_ext_dirty(handle, inode, path);
2387 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002388 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002389 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Aditya Kalid8990242011-09-09 19:18:51 -04002390 trace_ext4_ext_rm_idx(inode, leaf);
2391
Peter Huewe7dc57612011-02-21 21:01:42 -05002392 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002393 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Forrest Liuc36575e2012-12-17 09:55:39 -05002394
2395 while (--depth >= 0) {
2396 if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr))
2397 break;
2398 path--;
2399 err = ext4_ext_get_access(handle, inode, path);
2400 if (err)
2401 break;
2402 path->p_idx->ei_block = (path+1)->p_idx->ei_block;
2403 err = ext4_ext_dirty(handle, inode, path);
2404 if (err)
2405 break;
2406 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002407 return err;
2408}
2409
2410/*
Mingming Caoee12b632008-08-19 22:16:05 -04002411 * ext4_ext_calc_credits_for_single_extent:
2412 * This routine returns max. credits that needed to insert an extent
2413 * to the extent tree.
2414 * When pass the actual path, the caller should calculate credits
2415 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002416 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002417int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002418 struct ext4_ext_path *path)
2419{
Alex Tomasa86c6182006-10-11 01:21:03 -07002420 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002421 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002422 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002423
Alex Tomasa86c6182006-10-11 01:21:03 -07002424 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002425 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002426 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2427
2428 /*
2429 * There are some space in the leaf tree, no
2430 * need to account for leaf block credit
2431 *
2432 * bitmaps and block group descriptor blocks
Tao Madf3ab172011-10-08 15:53:49 -04002433 * and other metadata blocks still need to be
Mingming Caoee12b632008-08-19 22:16:05 -04002434 * accounted.
2435 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002436 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002437 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002438 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002439 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002440 }
2441
Mingming Cao525f4ed2008-08-19 22:15:58 -04002442 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002443}
Alex Tomasa86c6182006-10-11 01:21:03 -07002444
Mingming Caoee12b632008-08-19 22:16:05 -04002445/*
Jan Karafffb2732013-06-04 13:01:11 -04002446 * How many index/leaf blocks need to change/allocate to add @extents extents?
Mingming Caoee12b632008-08-19 22:16:05 -04002447 *
Jan Karafffb2732013-06-04 13:01:11 -04002448 * If we add a single extent, then in the worse case, each tree level
2449 * index/leaf need to be changed in case of the tree split.
Mingming Caoee12b632008-08-19 22:16:05 -04002450 *
Jan Karafffb2732013-06-04 13:01:11 -04002451 * If more extents are inserted, they could cause the whole tree split more
2452 * than once, but this is really rare.
Mingming Caoee12b632008-08-19 22:16:05 -04002453 */
Jan Karafffb2732013-06-04 13:01:11 -04002454int ext4_ext_index_trans_blocks(struct inode *inode, int extents)
Mingming Caoee12b632008-08-19 22:16:05 -04002455{
2456 int index;
Tao Maf19d5872012-12-10 14:05:51 -05002457 int depth;
2458
2459 /* If we are converting the inline data, only one is needed here. */
2460 if (ext4_has_inline_data(inode))
2461 return 1;
2462
2463 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002464
Jan Karafffb2732013-06-04 13:01:11 -04002465 if (extents <= 1)
Mingming Caoee12b632008-08-19 22:16:05 -04002466 index = depth * 2;
2467 else
2468 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002469
Mingming Caoee12b632008-08-19 22:16:05 -04002470 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002471}
2472
Theodore Ts'o981250c2013-06-12 11:48:29 -04002473static inline int get_default_free_blocks_flags(struct inode *inode)
2474{
2475 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2476 return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET;
2477 else if (ext4_should_journal_data(inode))
2478 return EXT4_FREE_BLOCKS_FORGET;
2479 return 0;
2480}
2481
Alex Tomasa86c6182006-10-11 01:21:03 -07002482static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002483 struct ext4_extent *ex,
Lukas Czernerd23142c2013-05-27 23:33:35 -04002484 long long *partial_cluster,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002485 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002486{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002487 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002488 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002489 ext4_fsblk_t pblk;
Theodore Ts'o981250c2013-06-12 11:48:29 -04002490 int flags = get_default_free_blocks_flags(inode);
Andrey Sidorov18888cf2012-09-19 14:14:53 -04002491
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002492 /*
2493 * For bigalloc file systems, we never free a partial cluster
2494 * at the beginning of the extent. Instead, we make a note
2495 * that we tried freeing the cluster, and check to see if we
2496 * need to free it on a subsequent call to ext4_remove_blocks,
2497 * or at the end of the ext4_truncate() operation.
2498 */
2499 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
2500
Aditya Kalid8990242011-09-09 19:18:51 -04002501 trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002502 /*
2503 * If we have a partial cluster, and it's different from the
2504 * cluster of the last block, we need to explicitly free the
2505 * partial cluster here.
2506 */
2507 pblk = ext4_ext_pblock(ex) + ee_len - 1;
Lukas Czernerd23142c2013-05-27 23:33:35 -04002508 if ((*partial_cluster > 0) &&
2509 (EXT4_B2C(sbi, pblk) != *partial_cluster)) {
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002510 ext4_free_blocks(handle, inode, NULL,
2511 EXT4_C2B(sbi, *partial_cluster),
2512 sbi->s_cluster_ratio, flags);
2513 *partial_cluster = 0;
2514 }
2515
Alex Tomasa86c6182006-10-11 01:21:03 -07002516#ifdef EXTENTS_STATS
2517 {
2518 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002519 spin_lock(&sbi->s_ext_stats_lock);
2520 sbi->s_ext_blocks += ee_len;
2521 sbi->s_ext_extents++;
2522 if (ee_len < sbi->s_ext_min)
2523 sbi->s_ext_min = ee_len;
2524 if (ee_len > sbi->s_ext_max)
2525 sbi->s_ext_max = ee_len;
2526 if (ext_depth(inode) > sbi->s_depth_max)
2527 sbi->s_depth_max = ext_depth(inode);
2528 spin_unlock(&sbi->s_ext_stats_lock);
2529 }
2530#endif
2531 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002532 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002533 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002534 ext4_lblk_t num;
Lukas Czernerd23142c2013-05-27 23:33:35 -04002535 unsigned int unaligned;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002536
Amit Aroraa2df2a62007-07-17 21:42:41 -04002537 num = le32_to_cpu(ex->ee_block) + ee_len - from;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002538 pblk = ext4_ext_pblock(ex) + ee_len - num;
Lukas Czernerd23142c2013-05-27 23:33:35 -04002539 /*
2540 * Usually we want to free partial cluster at the end of the
2541 * extent, except for the situation when the cluster is still
2542 * used by any other extent (partial_cluster is negative).
2543 */
2544 if (*partial_cluster < 0 &&
2545 -(*partial_cluster) == EXT4_B2C(sbi, pblk + num - 1))
2546 flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER;
2547
2548 ext_debug("free last %u blocks starting %llu partial %lld\n",
2549 num, pblk, *partial_cluster);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002550 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
2551 /*
2552 * If the block range to be freed didn't start at the
2553 * beginning of a cluster, and we removed the entire
Lukas Czernerd23142c2013-05-27 23:33:35 -04002554 * extent and the cluster is not used by any other extent,
2555 * save the partial cluster here, since we might need to
2556 * delete if we determine that the truncate operation has
2557 * removed all of the blocks in the cluster.
2558 *
2559 * On the other hand, if we did not manage to free the whole
2560 * extent, we have to mark the cluster as used (store negative
2561 * cluster number in partial_cluster).
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002562 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05002563 unaligned = EXT4_PBLK_COFF(sbi, pblk);
Lukas Czernerd23142c2013-05-27 23:33:35 -04002564 if (unaligned && (ee_len == num) &&
2565 (*partial_cluster != -((long long)EXT4_B2C(sbi, pblk))))
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002566 *partial_cluster = EXT4_B2C(sbi, pblk);
Lukas Czernerd23142c2013-05-27 23:33:35 -04002567 else if (unaligned)
2568 *partial_cluster = -((long long)EXT4_B2C(sbi, pblk));
2569 else if (*partial_cluster > 0)
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002570 *partial_cluster = 0;
Lukas Czerner78fb9cd2013-05-27 23:32:35 -04002571 } else
2572 ext4_error(sbi->s_sb, "strange request: removal(2) "
2573 "%u-%u from %u:%u\n",
2574 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002575 return 0;
2576}
2577
Allison Hendersond583fb82011-05-25 07:41:43 -04002578
2579/*
2580 * ext4_ext_rm_leaf() Removes the extents associated with the
2581 * blocks appearing between "start" and "end", and splits the extents
2582 * if "start" and "end" appear in the same extent
2583 *
2584 * @handle: The journal handle
2585 * @inode: The files inode
2586 * @path: The path to the leaf
Lukas Czernerd23142c2013-05-27 23:33:35 -04002587 * @partial_cluster: The cluster which we'll have to free if all extents
2588 * has been released from it. It gets negative in case
2589 * that the cluster is still used.
Allison Hendersond583fb82011-05-25 07:41:43 -04002590 * @start: The first block to remove
2591 * @end: The last block to remove
2592 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002593static int
2594ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Lukas Czernerd23142c2013-05-27 23:33:35 -04002595 struct ext4_ext_path *path,
2596 long long *partial_cluster,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002597 ext4_lblk_t start, ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002598{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002599 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002600 int err = 0, correct_index = 0;
2601 int depth = ext_depth(inode), credits;
2602 struct ext4_extent_header *eh;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002603 ext4_lblk_t a, b;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002604 unsigned num;
2605 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002606 unsigned short ex_ee_len;
Lukas Czerner556615d2014-04-20 23:45:47 -04002607 unsigned unwritten = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002608 struct ext4_extent *ex;
Lukas Czernerd23142c2013-05-27 23:33:35 -04002609 ext4_fsblk_t pblk;
Alex Tomasa86c6182006-10-11 01:21:03 -07002610
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002611 /* the header must be checked already in ext4_ext_remove_space() */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002612 ext_debug("truncate since %u in leaf to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002613 if (!path[depth].p_hdr)
2614 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2615 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002616 if (unlikely(path[depth].p_hdr == NULL)) {
2617 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2618 return -EIO;
2619 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002620 /* find where to start removing */
Ashish Sangwan6ae06ff2013-07-01 08:12:41 -04002621 ex = path[depth].p_ext;
2622 if (!ex)
2623 ex = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002624
2625 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002626 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002627
Eric Whitneyc0634492014-03-13 23:34:16 -04002628 /*
2629 * If we're starting with an extent other than the last one in the
2630 * node, we need to see if it shares a cluster with the extent to
2631 * the right (towards the end of the file). If its leftmost cluster
2632 * is this extent's rightmost cluster and it is not cluster aligned,
2633 * we'll mark it as a partial that is not to be deallocated.
2634 */
2635
2636 if (ex != EXT_LAST_EXTENT(eh)) {
2637 ext4_fsblk_t current_pblk, right_pblk;
2638 long long current_cluster, right_cluster;
2639
2640 current_pblk = ext4_ext_pblock(ex) + ex_ee_len - 1;
2641 current_cluster = (long long)EXT4_B2C(sbi, current_pblk);
2642 right_pblk = ext4_ext_pblock(ex + 1);
2643 right_cluster = (long long)EXT4_B2C(sbi, right_pblk);
2644 if (current_cluster == right_cluster &&
2645 EXT4_PBLK_COFF(sbi, right_pblk))
2646 *partial_cluster = -right_cluster;
2647 }
2648
Aditya Kalid8990242011-09-09 19:18:51 -04002649 trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster);
2650
Alex Tomasa86c6182006-10-11 01:21:03 -07002651 while (ex >= EXT_FIRST_EXTENT(eh) &&
2652 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002653
Lukas Czerner556615d2014-04-20 23:45:47 -04002654 if (ext4_ext_is_unwritten(ex))
2655 unwritten = 1;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002656 else
Lukas Czerner556615d2014-04-20 23:45:47 -04002657 unwritten = 0;
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002658
Mingming553f9002009-09-18 13:34:55 -04002659 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
Lukas Czerner556615d2014-04-20 23:45:47 -04002660 unwritten, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002661 path[depth].p_ext = ex;
2662
2663 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002664 b = ex_ee_block+ex_ee_len - 1 < end ?
2665 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002666
2667 ext_debug(" border %u:%u\n", a, b);
2668
Allison Hendersond583fb82011-05-25 07:41:43 -04002669 /* If this extent is beyond the end of the hole, skip it */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002670 if (end < ex_ee_block) {
Lukas Czernerd23142c2013-05-27 23:33:35 -04002671 /*
2672 * We're going to skip this extent and move to another,
2673 * so if this extent is not cluster aligned we have
2674 * to mark the current cluster as used to avoid
2675 * accidentally freeing it later on
2676 */
2677 pblk = ext4_ext_pblock(ex);
Theodore Ts'of5a44db2013-12-20 09:29:35 -05002678 if (EXT4_PBLK_COFF(sbi, pblk))
Lukas Czernerd23142c2013-05-27 23:33:35 -04002679 *partial_cluster =
2680 -((long long)EXT4_B2C(sbi, pblk));
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);
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002691 err = -EIO;
2692 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);
Lukas Czernerd23142c2013-05-27 23:33:35 -04002756 } else if (*partial_cluster > 0)
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002757 *partial_cluster = 0;
Allison Hendersond583fb82011-05-25 07:41:43 -04002758
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002759 err = ext4_ext_dirty(handle, inode, path + depth);
2760 if (err)
2761 goto out;
2762
Yongqiang Yangbf52c6f2011-11-01 18:59:26 -04002763 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002764 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002765 ex--;
2766 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002767 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002768 }
2769
2770 if (correct_index && eh->eh_entries)
2771 err = ext4_ext_correct_indexes(handle, inode, path);
2772
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002773 /*
Eric Whitneyad6599a2014-04-01 19:49:30 -04002774 * If there's a partial cluster and at least one extent remains in
2775 * the leaf, free the partial cluster if it isn't shared with the
2776 * current extent. If there's a partial cluster and no extents
2777 * remain in the leaf, it can't be freed here. It can only be
2778 * freed when it's possible to determine if it's not shared with
2779 * any other extent - when the next leaf is processed or when space
2780 * removal is complete.
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002781 */
Eric Whitneyad6599a2014-04-01 19:49:30 -04002782 if (*partial_cluster > 0 && eh->eh_entries &&
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002783 (EXT4_B2C(sbi, ext4_ext_pblock(ex) + ex_ee_len - 1) !=
2784 *partial_cluster)) {
Theodore Ts'o981250c2013-06-12 11:48:29 -04002785 int flags = get_default_free_blocks_flags(inode);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002786
2787 ext4_free_blocks(handle, inode, NULL,
2788 EXT4_C2B(sbi, *partial_cluster),
2789 sbi->s_cluster_ratio, flags);
2790 *partial_cluster = 0;
2791 }
2792
Alex Tomasa86c6182006-10-11 01:21:03 -07002793 /* if this leaf is free, then we should
2794 * remove it from index block above */
2795 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
Forrest Liuc36575e2012-12-17 09:55:39 -05002796 err = ext4_ext_rm_idx(handle, inode, path, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002797
2798out:
2799 return err;
2800}
2801
2802/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002803 * ext4_ext_more_to_rm:
2804 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002805 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002806static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002807ext4_ext_more_to_rm(struct ext4_ext_path *path)
2808{
2809 BUG_ON(path->p_idx == NULL);
2810
2811 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2812 return 0;
2813
2814 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002815 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002816 * so we have to consider current index for truncation
2817 */
2818 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2819 return 0;
2820 return 1;
2821}
2822
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04002823int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2824 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002825{
2826 struct super_block *sb = inode->i_sb;
2827 int depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002828 struct ext4_ext_path *path = NULL;
Lukas Czernerd23142c2013-05-27 23:33:35 -04002829 long long partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002830 handle_t *handle;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002831 int i = 0, err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002832
Lukas Czerner5f95d212012-03-19 23:03:19 -04002833 ext_debug("truncate since %u to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002834
2835 /* probably first extent we're gonna free will be last in block */
Theodore Ts'o9924a922013-02-08 21:59:22 -05002836 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, depth + 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07002837 if (IS_ERR(handle))
2838 return PTR_ERR(handle);
2839
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002840again:
Lukas Czerner61801322013-05-27 23:32:35 -04002841 trace_ext4_ext_remove_space(inode, start, end, depth);
Aditya Kalid8990242011-09-09 19:18:51 -04002842
Alex Tomasa86c6182006-10-11 01:21:03 -07002843 /*
Lukas Czerner5f95d212012-03-19 23:03:19 -04002844 * Check if we are removing extents inside the extent tree. If that
2845 * is the case, we are going to punch a hole inside the extent tree
2846 * so we have to check whether we need to split the extent covering
2847 * the last block to remove so we can easily remove the part of it
2848 * in ext4_ext_rm_leaf().
2849 */
2850 if (end < EXT_MAX_BLOCKS - 1) {
2851 struct ext4_extent *ex;
2852 ext4_lblk_t ee_block;
2853
2854 /* find extent for this block */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04002855 path = ext4_find_extent(inode, end, NULL, EXT4_EX_NOCACHE);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002856 if (IS_ERR(path)) {
2857 ext4_journal_stop(handle);
2858 return PTR_ERR(path);
2859 }
2860 depth = ext_depth(inode);
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002861 /* Leaf not may not exist only if inode has no blocks at all */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002862 ex = path[depth].p_ext;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002863 if (!ex) {
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002864 if (depth) {
2865 EXT4_ERROR_INODE(inode,
2866 "path[%d].p_hdr == NULL",
2867 depth);
2868 err = -EIO;
2869 }
2870 goto out;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002871 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002872
2873 ee_block = le32_to_cpu(ex->ee_block);
2874
2875 /*
2876 * See if the last block is inside the extent, if so split
2877 * the extent at 'end' block so we can easily remove the
2878 * tail of the first part of the split extent in
2879 * ext4_ext_rm_leaf().
2880 */
2881 if (end >= ee_block &&
2882 end < ee_block + ext4_ext_get_actual_len(ex) - 1) {
Lukas Czerner5f95d212012-03-19 23:03:19 -04002883 /*
2884 * Split the extent in two so that 'end' is the last
Lukas Czerner27dd4382013-04-09 22:11:22 -04002885 * block in the first new extent. Also we should not
2886 * fail removing space due to ENOSPC so try to use
2887 * reserved block if that happens.
Lukas Czerner5f95d212012-03-19 23:03:19 -04002888 */
Theodore Ts'odfe50802014-09-01 14:37:09 -04002889 err = ext4_force_split_extent_at(handle, inode, &path,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04002890 end + 1, 1);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002891 if (err < 0)
2892 goto out;
2893 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002894 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002895 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002896 * We start scanning from right side, freeing all the blocks
2897 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002898 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002899 depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002900 if (path) {
2901 int k = i = depth;
2902 while (--k > 0)
2903 path[k].p_block =
2904 le16_to_cpu(path[k].p_hdr->eh_entries)+1;
2905 } else {
2906 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1),
2907 GFP_NOFS);
2908 if (path == NULL) {
2909 ext4_journal_stop(handle);
2910 return -ENOMEM;
2911 }
Theodore Ts'o10809df82014-09-01 14:40:09 -04002912 path[0].p_maxdepth = path[0].p_depth = depth;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002913 path[0].p_hdr = ext_inode_hdr(inode);
Theodore Ts'o89a4e482012-08-17 08:54:52 -04002914 i = 0;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002915
Theodore Ts'oc3491792013-08-16 21:21:41 -04002916 if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) {
Ashish Sangwan968dee72012-07-22 22:49:08 -04002917 err = -EIO;
2918 goto out;
2919 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002920 }
Ashish Sangwan968dee72012-07-22 22:49:08 -04002921 err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002922
2923 while (i >= 0 && err == 0) {
2924 if (i == depth) {
2925 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04002926 err = ext4_ext_rm_leaf(handle, inode, path,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002927 &partial_cluster, start,
Lukas Czerner5f95d212012-03-19 23:03:19 -04002928 end);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002929 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002930 brelse(path[i].p_bh);
2931 path[i].p_bh = NULL;
2932 i--;
2933 continue;
2934 }
2935
2936 /* this is index block */
2937 if (!path[i].p_hdr) {
2938 ext_debug("initialize header\n");
2939 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002940 }
2941
Alex Tomasa86c6182006-10-11 01:21:03 -07002942 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002943 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002944 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2945 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2946 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2947 path[i].p_hdr,
2948 le16_to_cpu(path[i].p_hdr->eh_entries));
2949 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002950 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002951 path[i].p_idx--;
2952 }
2953
2954 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2955 i, EXT_FIRST_INDEX(path[i].p_hdr),
2956 path[i].p_idx);
2957 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002958 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002959 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002960 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04002961 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002962 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04002963 bh = read_extent_tree_block(inode,
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04002964 ext4_idx_pblock(path[i].p_idx), depth - i - 1,
2965 EXT4_EX_NOCACHE);
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04002966 if (IS_ERR(bh)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002967 /* should we reset i_size? */
Theodore Ts'o7d7ea892013-08-16 21:20:41 -04002968 err = PTR_ERR(bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002969 break;
2970 }
Theodore Ts'o76828c82013-07-15 12:27:47 -04002971 /* Yield here to deal with large extent trees.
2972 * Should be a no-op if we did IO above. */
2973 cond_resched();
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002974 if (WARN_ON(i + 1 > depth)) {
2975 err = -EIO;
2976 break;
2977 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002978 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002979
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002980 /* save actual number of indexes since this
2981 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002982 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2983 i++;
2984 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002985 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002986 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002987 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002988 * handle must be already prepared by the
2989 * truncatei_leaf() */
Forrest Liuc36575e2012-12-17 09:55:39 -05002990 err = ext4_ext_rm_idx(handle, inode, path, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07002991 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002992 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002993 brelse(path[i].p_bh);
2994 path[i].p_bh = NULL;
2995 i--;
2996 ext_debug("return to level %d\n", i);
2997 }
2998 }
2999
Lukas Czerner61801322013-05-27 23:32:35 -04003000 trace_ext4_ext_remove_space_done(inode, start, end, depth,
3001 partial_cluster, path->p_hdr->eh_entries);
Aditya Kalid8990242011-09-09 19:18:51 -04003002
Aditya Kali7b415bf2011-09-09 19:04:51 -04003003 /* If we still have something in the partial cluster and we have removed
3004 * even the first extent, then we should free the blocks in the partial
3005 * cluster as well. */
Lukas Czernerd23142c2013-05-27 23:33:35 -04003006 if (partial_cluster > 0 && path->p_hdr->eh_entries == 0) {
Theodore Ts'o981250c2013-06-12 11:48:29 -04003007 int flags = get_default_free_blocks_flags(inode);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003008
3009 ext4_free_blocks(handle, inode, NULL,
3010 EXT4_C2B(EXT4_SB(sb), partial_cluster),
3011 EXT4_SB(sb)->s_cluster_ratio, flags);
3012 partial_cluster = 0;
3013 }
3014
Alex Tomasa86c6182006-10-11 01:21:03 -07003015 /* TODO: flexible tree reduction should be here */
3016 if (path->p_hdr->eh_entries == 0) {
3017 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003018 * truncate to zero freed all the tree,
3019 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07003020 */
3021 err = ext4_ext_get_access(handle, inode, path);
3022 if (err == 0) {
3023 ext_inode_hdr(inode)->eh_depth = 0;
3024 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04003025 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07003026 err = ext4_ext_dirty(handle, inode, path);
3027 }
3028 }
3029out:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04003030 ext4_ext_drop_refs(path);
3031 kfree(path);
3032 path = NULL;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003033 if (err == -EAGAIN)
3034 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07003035 ext4_journal_stop(handle);
3036
3037 return err;
3038}
3039
3040/*
3041 * called at mount time
3042 */
3043void ext4_ext_init(struct super_block *sb)
3044{
3045 /*
3046 * possible initialization would be here
3047 */
3048
Theodore Ts'o83982b62009-01-06 14:53:16 -05003049 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04003050#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o92b97812012-03-19 23:41:49 -04003051 printk(KERN_INFO "EXT4-fs: file extents enabled"
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01003052#ifdef AGGRESSIVE_TEST
Theodore Ts'o92b97812012-03-19 23:41:49 -04003053 ", aggressive tests"
Alex Tomasa86c6182006-10-11 01:21:03 -07003054#endif
3055#ifdef CHECK_BINSEARCH
Theodore Ts'o92b97812012-03-19 23:41:49 -04003056 ", check binsearch"
Alex Tomasa86c6182006-10-11 01:21:03 -07003057#endif
3058#ifdef EXTENTS_STATS
Theodore Ts'o92b97812012-03-19 23:41:49 -04003059 ", stats"
Alex Tomasa86c6182006-10-11 01:21:03 -07003060#endif
Theodore Ts'o92b97812012-03-19 23:41:49 -04003061 "\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04003062#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07003063#ifdef EXTENTS_STATS
3064 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
3065 EXT4_SB(sb)->s_ext_min = 1 << 30;
3066 EXT4_SB(sb)->s_ext_max = 0;
3067#endif
3068 }
3069}
3070
3071/*
3072 * called at umount time
3073 */
3074void ext4_ext_release(struct super_block *sb)
3075{
Theodore Ts'o83982b62009-01-06 14:53:16 -05003076 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
Alex Tomasa86c6182006-10-11 01:21:03 -07003077 return;
3078
3079#ifdef EXTENTS_STATS
3080 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
3081 struct ext4_sb_info *sbi = EXT4_SB(sb);
3082 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
3083 sbi->s_ext_blocks, sbi->s_ext_extents,
3084 sbi->s_ext_blocks / sbi->s_ext_extents);
3085 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
3086 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
3087 }
3088#endif
3089}
3090
Zheng Liud7b2a002013-08-28 14:47:06 -04003091static int ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex)
3092{
3093 ext4_lblk_t ee_block;
3094 ext4_fsblk_t ee_pblock;
3095 unsigned int ee_len;
3096
3097 ee_block = le32_to_cpu(ex->ee_block);
3098 ee_len = ext4_ext_get_actual_len(ex);
3099 ee_pblock = ext4_ext_pblock(ex);
3100
3101 if (ee_len == 0)
3102 return 0;
3103
3104 return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock,
3105 EXTENT_STATUS_WRITTEN);
3106}
3107
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003108/* FIXME!! we need to try to merge to left or right after zero-out */
3109static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
3110{
Lukas Czerner24075182010-10-27 21:30:06 -04003111 ext4_fsblk_t ee_pblock;
3112 unsigned int ee_len;
Jing Zhangb7203032010-05-12 00:00:00 -04003113 int ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003114
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003115 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003116 ee_pblock = ext4_ext_pblock(ex);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003117
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04003118 ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
Lukas Czerner24075182010-10-27 21:30:06 -04003119 if (ret > 0)
3120 ret = 0;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003121
Lukas Czerner24075182010-10-27 21:30:06 -04003122 return ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04003123}
3124
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003125/*
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003126 * ext4_split_extent_at() splits an extent at given block.
3127 *
3128 * @handle: the journal handle
3129 * @inode: the file inode
3130 * @path: the path to the extent
3131 * @split: the logical block where the extent is splitted.
3132 * @split_flags: indicates if the extent could be zeroout if split fails, and
Lukas Czerner556615d2014-04-20 23:45:47 -04003133 * the states(init or unwritten) of new extents.
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003134 * @flags: flags used to insert new extent to extent tree.
3135 *
3136 *
3137 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
3138 * of which are deterimined by split_flag.
3139 *
3140 * There are two cases:
3141 * a> the extent are splitted into two extent.
3142 * b> split is not needed, and just mark the extent.
3143 *
3144 * return 0 on success.
3145 */
3146static int ext4_split_extent_at(handle_t *handle,
3147 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003148 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003149 ext4_lblk_t split,
3150 int split_flag,
3151 int flags)
3152{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003153 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003154 ext4_fsblk_t newblock;
3155 ext4_lblk_t ee_block;
Zheng Liuadb23552013-03-10 21:13:05 -04003156 struct ext4_extent *ex, newex, orig_ex, zero_ex;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003157 struct ext4_extent *ex2 = NULL;
3158 unsigned int ee_len, depth;
3159 int err = 0;
3160
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003161 BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) ==
3162 (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2));
3163
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003164 ext_debug("ext4_split_extents_at: inode %lu, logical"
3165 "block %llu\n", inode->i_ino, (unsigned long long)split);
3166
3167 ext4_ext_show_leaf(inode, path);
3168
3169 depth = ext_depth(inode);
3170 ex = path[depth].p_ext;
3171 ee_block = le32_to_cpu(ex->ee_block);
3172 ee_len = ext4_ext_get_actual_len(ex);
3173 newblock = split - ee_block + ext4_ext_pblock(ex);
3174
3175 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
Lukas Czerner556615d2014-04-20 23:45:47 -04003176 BUG_ON(!ext4_ext_is_unwritten(ex) &&
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003177 split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003178 EXT4_EXT_MARK_UNWRIT1 |
3179 EXT4_EXT_MARK_UNWRIT2));
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003180
3181 err = ext4_ext_get_access(handle, inode, path + depth);
3182 if (err)
3183 goto out;
3184
3185 if (split == ee_block) {
3186 /*
3187 * case b: block @split is the block that the extent begins with
3188 * then we just change the state of the extent, and splitting
3189 * is not needed.
3190 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003191 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3192 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003193 else
3194 ext4_ext_mark_initialized(ex);
3195
3196 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003197 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003198
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003199 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003200 goto out;
3201 }
3202
3203 /* case a */
3204 memcpy(&orig_ex, ex, sizeof(orig_ex));
3205 ex->ee_len = cpu_to_le16(split - ee_block);
Lukas Czerner556615d2014-04-20 23:45:47 -04003206 if (split_flag & EXT4_EXT_MARK_UNWRIT1)
3207 ext4_ext_mark_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003208
3209 /*
3210 * path may lead to new leaf, not to original leaf any more
3211 * after ext4_ext_insert_extent() returns,
3212 */
3213 err = ext4_ext_dirty(handle, inode, path + depth);
3214 if (err)
3215 goto fix_extent_len;
3216
3217 ex2 = &newex;
3218 ex2->ee_block = cpu_to_le32(split);
3219 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
3220 ext4_ext_store_pblock(ex2, newblock);
Lukas Czerner556615d2014-04-20 23:45:47 -04003221 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3222 ext4_ext_mark_unwritten(ex2);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003223
Theodore Ts'odfe50802014-09-01 14:37:09 -04003224 err = ext4_ext_insert_extent(handle, inode, ppath, &newex, flags);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003225 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003226 if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) {
Zheng Liuadb23552013-03-10 21:13:05 -04003227 if (split_flag & EXT4_EXT_DATA_VALID1) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003228 err = ext4_ext_zeroout(inode, ex2);
Zheng Liuadb23552013-03-10 21:13:05 -04003229 zero_ex.ee_block = ex2->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003230 zero_ex.ee_len = cpu_to_le16(
3231 ext4_ext_get_actual_len(ex2));
Zheng Liuadb23552013-03-10 21:13:05 -04003232 ext4_ext_store_pblock(&zero_ex,
3233 ext4_ext_pblock(ex2));
3234 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003235 err = ext4_ext_zeroout(inode, ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003236 zero_ex.ee_block = ex->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003237 zero_ex.ee_len = cpu_to_le16(
3238 ext4_ext_get_actual_len(ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003239 ext4_ext_store_pblock(&zero_ex,
3240 ext4_ext_pblock(ex));
3241 }
3242 } else {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003243 err = ext4_ext_zeroout(inode, &orig_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003244 zero_ex.ee_block = orig_ex.ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003245 zero_ex.ee_len = cpu_to_le16(
3246 ext4_ext_get_actual_len(&orig_ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003247 ext4_ext_store_pblock(&zero_ex,
3248 ext4_ext_pblock(&orig_ex));
3249 }
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003250
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003251 if (err)
3252 goto fix_extent_len;
3253 /* update the extent length and mark as initialized */
Al Viroaf1584f2012-04-12 20:32:25 -04003254 ex->ee_len = cpu_to_le16(ee_len);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003255 ext4_ext_try_to_merge(handle, inode, path, ex);
3256 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Zheng Liuadb23552013-03-10 21:13:05 -04003257 if (err)
3258 goto fix_extent_len;
3259
3260 /* update extent status tree */
Zheng Liud7b2a002013-08-28 14:47:06 -04003261 err = ext4_zeroout_es(inode, &zero_ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003262
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003263 goto out;
3264 } else if (err)
3265 goto fix_extent_len;
3266
3267out:
3268 ext4_ext_show_leaf(inode, path);
3269 return err;
3270
3271fix_extent_len:
3272 ex->ee_len = orig_ex.ee_len;
Dmitry Monakhov29faed12014-07-27 22:30:29 -04003273 ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003274 return err;
3275}
3276
3277/*
3278 * ext4_split_extents() splits an extent and mark extent which is covered
3279 * by @map as split_flags indicates
3280 *
Anatol Pomozov70261f52013-08-28 14:40:12 -04003281 * It may result in splitting the extent into multiple extents (up to three)
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003282 * There are three possibilities:
3283 * a> There is no split required
3284 * b> Splits in two extents: Split is happening at either end of the extent
3285 * c> Splits in three extents: Somone is splitting in middle of the extent
3286 *
3287 */
3288static int ext4_split_extent(handle_t *handle,
3289 struct inode *inode,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003290 struct ext4_ext_path **ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003291 struct ext4_map_blocks *map,
3292 int split_flag,
3293 int flags)
3294{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003295 struct ext4_ext_path *path = *ppath;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003296 ext4_lblk_t ee_block;
3297 struct ext4_extent *ex;
3298 unsigned int ee_len, depth;
3299 int err = 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003300 int unwritten;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003301 int split_flag1, flags1;
Zheng Liu3a225672013-03-10 21:20:23 -04003302 int allocated = map->m_len;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003303
3304 depth = ext_depth(inode);
3305 ex = path[depth].p_ext;
3306 ee_block = le32_to_cpu(ex->ee_block);
3307 ee_len = ext4_ext_get_actual_len(ex);
Lukas Czerner556615d2014-04-20 23:45:47 -04003308 unwritten = ext4_ext_is_unwritten(ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003309
3310 if (map->m_lblk + map->m_len < ee_block + ee_len) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003311 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003312 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
Lukas Czerner556615d2014-04-20 23:45:47 -04003313 if (unwritten)
3314 split_flag1 |= EXT4_EXT_MARK_UNWRIT1 |
3315 EXT4_EXT_MARK_UNWRIT2;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003316 if (split_flag & EXT4_EXT_DATA_VALID2)
3317 split_flag1 |= EXT4_EXT_DATA_VALID1;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003318 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003319 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04003320 if (err)
3321 goto out;
Zheng Liu3a225672013-03-10 21:20:23 -04003322 } else {
3323 allocated = ee_len - (map->m_lblk - ee_block);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003324 }
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003325 /*
3326 * Update path is required because previous ext4_split_extent_at() may
3327 * result in split of original leaf or extent zeroout.
3328 */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003329 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003330 if (IS_ERR(path))
3331 return PTR_ERR(path);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003332 depth = ext_depth(inode);
3333 ex = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04003334 if (!ex) {
3335 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3336 (unsigned long) map->m_lblk);
3337 return -EIO;
3338 }
Lukas Czerner556615d2014-04-20 23:45:47 -04003339 unwritten = ext4_ext_is_unwritten(ex);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003340 split_flag1 = 0;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003341
3342 if (map->m_lblk >= ee_block) {
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003343 split_flag1 = split_flag & EXT4_EXT_DATA_VALID2;
Lukas Czerner556615d2014-04-20 23:45:47 -04003344 if (unwritten) {
3345 split_flag1 |= EXT4_EXT_MARK_UNWRIT1;
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003346 split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT |
Lukas Czerner556615d2014-04-20 23:45:47 -04003347 EXT4_EXT_MARK_UNWRIT2);
Dmitry Monakhov357b66f2013-03-04 00:34:34 -05003348 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04003349 err = ext4_split_extent_at(handle, inode, ppath,
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003350 map->m_lblk, split_flag1, flags);
3351 if (err)
3352 goto out;
3353 }
3354
3355 ext4_ext_show_leaf(inode, path);
3356out:
Zheng Liu3a225672013-03-10 21:20:23 -04003357 return err ? err : allocated;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003358}
3359
Amit Arora56055d32007-07-17 21:42:38 -04003360/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003361 * This function is called by ext4_ext_map_blocks() if someone tries to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003362 * to an unwritten extent. It may result in splitting the unwritten
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003363 * extent into multiple extents (up to three - one initialized and two
Lukas Czerner556615d2014-04-20 23:45:47 -04003364 * unwritten).
Amit Arora56055d32007-07-17 21:42:38 -04003365 * There are three possibilities:
3366 * a> There is no split required: Entire extent should be initialized
3367 * b> Splits in two extents: Write is happening at either end of the extent
3368 * c> Splits in three extents: Somone is writing in middle of the extent
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003369 *
3370 * Pre-conditions:
Lukas Czerner556615d2014-04-20 23:45:47 -04003371 * - The extent pointed to by 'path' is unwritten.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003372 * - The extent pointed to by 'path' contains a superset
3373 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
3374 *
3375 * Post-conditions on success:
3376 * - the returned value is the number of blocks beyond map->l_lblk
3377 * that are allocated and initialized.
3378 * It is guaranteed to be >= map->m_len.
Amit Arora56055d32007-07-17 21:42:38 -04003379 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003380static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003381 struct inode *inode,
3382 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003383 struct ext4_ext_path **ppath,
Lukas Czerner27dd4382013-04-09 22:11:22 -04003384 int flags)
Amit Arora56055d32007-07-17 21:42:38 -04003385{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003386 struct ext4_ext_path *path = *ppath;
Zheng Liu67a5da52012-08-17 09:54:17 -04003387 struct ext4_sb_info *sbi;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003388 struct ext4_extent_header *eh;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003389 struct ext4_map_blocks split_map;
3390 struct ext4_extent zero_ex;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003391 struct ext4_extent *ex, *abut_ex;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003392 ext4_lblk_t ee_block, eof_block;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003393 unsigned int ee_len, depth, map_len = map->m_len;
3394 int allocated = 0, max_zeroout = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003395 int err = 0;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003396 int split_flag = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003397
3398 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3399 "block %llu, max_blocks %u\n", inode->i_ino,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003400 (unsigned long long)map->m_lblk, map_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003401
Zheng Liu67a5da52012-08-17 09:54:17 -04003402 sbi = EXT4_SB(inode->i_sb);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003403 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3404 inode->i_sb->s_blocksize_bits;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003405 if (eof_block < map->m_lblk + map_len)
3406 eof_block = map->m_lblk + map_len;
Amit Arora56055d32007-07-17 21:42:38 -04003407
3408 depth = ext_depth(inode);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003409 eh = path[depth].p_hdr;
Amit Arora56055d32007-07-17 21:42:38 -04003410 ex = path[depth].p_ext;
3411 ee_block = le32_to_cpu(ex->ee_block);
3412 ee_len = ext4_ext_get_actual_len(ex);
Zheng Liuadb23552013-03-10 21:13:05 -04003413 zero_ex.ee_len = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003414
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003415 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
3416
3417 /* Pre-conditions */
Lukas Czerner556615d2014-04-20 23:45:47 -04003418 BUG_ON(!ext4_ext_is_unwritten(ex));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003419 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003420
3421 /*
3422 * Attempt to transfer newly initialized blocks from the currently
Lukas Czerner556615d2014-04-20 23:45:47 -04003423 * unwritten extent to its neighbor. This is much cheaper
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003424 * than an insertion followed by a merge as those involve costly
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003425 * memmove() calls. Transferring to the left is the common case in
3426 * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE)
3427 * followed by append writes.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003428 *
3429 * Limitations of the current logic:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003430 * - L1: we do not deal with writes covering the whole extent.
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003431 * This would require removing the extent if the transfer
3432 * is possible.
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003433 * - L2: we only attempt to merge with an extent stored in the
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003434 * same extent tree node.
3435 */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003436 if ((map->m_lblk == ee_block) &&
3437 /* See if we can merge left */
3438 (map_len < ee_len) && /*L1*/
3439 (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003440 ext4_lblk_t prev_lblk;
3441 ext4_fsblk_t prev_pblk, ee_pblk;
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003442 unsigned int prev_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003443
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003444 abut_ex = ex - 1;
3445 prev_lblk = le32_to_cpu(abut_ex->ee_block);
3446 prev_len = ext4_ext_get_actual_len(abut_ex);
3447 prev_pblk = ext4_ext_pblock(abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003448 ee_pblk = ext4_ext_pblock(ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003449
3450 /*
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003451 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003452 * upon those conditions:
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003453 * - C1: abut_ex is initialized,
3454 * - C2: abut_ex is logically abutting ex,
3455 * - C3: abut_ex is physically abutting ex,
3456 * - C4: abut_ex can receive the additional blocks without
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003457 * overflowing the (initialized) length limit.
3458 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003459 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003460 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3461 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003462 (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003463 err = ext4_ext_get_access(handle, inode, path + depth);
3464 if (err)
3465 goto out;
3466
3467 trace_ext4_ext_convert_to_initialized_fastpath(inode,
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003468 map, ex, abut_ex);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003469
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003470 /* Shift the start of ex by 'map_len' blocks */
3471 ex->ee_block = cpu_to_le32(ee_block + map_len);
3472 ext4_ext_store_pblock(ex, ee_pblk + map_len);
3473 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003474 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003475
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003476 /* Extend abut_ex by 'map_len' blocks */
3477 abut_ex->ee_len = cpu_to_le16(prev_len + map_len);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003478
3479 /* Result: number of initialized blocks past m_lblk */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003480 allocated = map_len;
3481 }
3482 } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) &&
3483 (map_len < ee_len) && /*L1*/
3484 ex < EXT_LAST_EXTENT(eh)) { /*L2*/
3485 /* See if we can merge right */
3486 ext4_lblk_t next_lblk;
3487 ext4_fsblk_t next_pblk, ee_pblk;
3488 unsigned int next_len;
3489
3490 abut_ex = ex + 1;
3491 next_lblk = le32_to_cpu(abut_ex->ee_block);
3492 next_len = ext4_ext_get_actual_len(abut_ex);
3493 next_pblk = ext4_ext_pblock(abut_ex);
3494 ee_pblk = ext4_ext_pblock(ex);
3495
3496 /*
3497 * A transfer of blocks from 'ex' to 'abut_ex' is allowed
3498 * upon those conditions:
3499 * - C1: abut_ex is initialized,
3500 * - C2: abut_ex is logically abutting ex,
3501 * - C3: abut_ex is physically abutting ex,
3502 * - C4: abut_ex can receive the additional blocks without
3503 * overflowing the (initialized) length limit.
3504 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003505 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003506 ((map->m_lblk + map_len) == next_lblk) && /*C2*/
3507 ((ee_pblk + ee_len) == next_pblk) && /*C3*/
3508 (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
3509 err = ext4_ext_get_access(handle, inode, path + depth);
3510 if (err)
3511 goto out;
3512
3513 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3514 map, ex, abut_ex);
3515
3516 /* Shift the start of abut_ex by 'map_len' blocks */
3517 abut_ex->ee_block = cpu_to_le32(next_lblk - map_len);
3518 ext4_ext_store_pblock(abut_ex, next_pblk - map_len);
3519 ex->ee_len = cpu_to_le16(ee_len - map_len);
Lukas Czerner556615d2014-04-20 23:45:47 -04003520 ext4_ext_mark_unwritten(ex); /* Restore the flag */
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003521
3522 /* Extend abut_ex by 'map_len' blocks */
3523 abut_ex->ee_len = cpu_to_le16(next_len + map_len);
3524
3525 /* Result: number of initialized blocks past m_lblk */
3526 allocated = map_len;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003527 }
3528 }
Lukas Czernerbc2d9db2013-04-03 23:33:27 -04003529 if (allocated) {
3530 /* Mark the block containing both extents as dirty */
3531 ext4_ext_dirty(handle, inode, path + depth);
3532
3533 /* Update path to point to the right extent */
3534 path[depth].p_ext = abut_ex;
3535 goto out;
3536 } else
3537 allocated = ee_len - (map->m_lblk - ee_block);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003538
Yongqiang Yang667eff32011-05-03 12:25:07 -04003539 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003540 /*
3541 * It is safe to convert extent to initialized via explicit
Yongqiang Yang9e740562014-01-06 14:05:23 -05003542 * zeroout only if extent is fully inside i_size or new_size.
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003543 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003544 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003545
Zheng Liu67a5da52012-08-17 09:54:17 -04003546 if (EXT4_EXT_MAY_ZEROOUT & split_flag)
3547 max_zeroout = sbi->s_extent_max_zeroout_kb >>
Lukas Czerner4f42f802013-03-12 12:40:04 -04003548 (inode->i_sb->s_blocksize_bits - 10);
Zheng Liu67a5da52012-08-17 09:54:17 -04003549
3550 /* If extent is less than s_max_zeroout_kb, zeroout directly */
3551 if (max_zeroout && (ee_len <= max_zeroout)) {
Yongqiang Yang667eff32011-05-03 12:25:07 -04003552 err = ext4_ext_zeroout(inode, ex);
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003553 if (err)
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003554 goto out;
Zheng Liuadb23552013-03-10 21:13:05 -04003555 zero_ex.ee_block = ex->ee_block;
Zheng Liu8cde7ad2013-04-03 12:27:18 -04003556 zero_ex.ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex));
Zheng Liuadb23552013-03-10 21:13:05 -04003557 ext4_ext_store_pblock(&zero_ex, ext4_ext_pblock(ex));
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003558
3559 err = ext4_ext_get_access(handle, inode, path + depth);
3560 if (err)
3561 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003562 ext4_ext_mark_initialized(ex);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003563 ext4_ext_try_to_merge(handle, inode, path, ex);
3564 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003565 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04003566 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003567
Amit Arora56055d32007-07-17 21:42:38 -04003568 /*
Yongqiang Yang667eff32011-05-03 12:25:07 -04003569 * four cases:
3570 * 1. split the extent into three extents.
3571 * 2. split the extent into two extents, zeroout the first half.
3572 * 3. split the extent into two extents, zeroout the second half.
3573 * 4. split the extent into two extents with out zeroout.
Amit Arora56055d32007-07-17 21:42:38 -04003574 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003575 split_map.m_lblk = map->m_lblk;
3576 split_map.m_len = map->m_len;
3577
Zheng Liu67a5da52012-08-17 09:54:17 -04003578 if (max_zeroout && (allocated > map->m_len)) {
3579 if (allocated <= max_zeroout) {
Yongqiang Yang667eff32011-05-03 12:25:07 -04003580 /* case 3 */
3581 zero_ex.ee_block =
Allison Henderson9b940f82011-05-16 10:11:09 -04003582 cpu_to_le32(map->m_lblk);
3583 zero_ex.ee_len = cpu_to_le16(allocated);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003584 ext4_ext_store_pblock(&zero_ex,
3585 ext4_ext_pblock(ex) + map->m_lblk - ee_block);
3586 err = ext4_ext_zeroout(inode, &zero_ex);
Amit Arora56055d32007-07-17 21:42:38 -04003587 if (err)
3588 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003589 split_map.m_lblk = map->m_lblk;
3590 split_map.m_len = allocated;
Zheng Liu67a5da52012-08-17 09:54:17 -04003591 } else if (map->m_lblk - ee_block + map->m_len < max_zeroout) {
Yongqiang Yang667eff32011-05-03 12:25:07 -04003592 /* case 2 */
3593 if (map->m_lblk != ee_block) {
3594 zero_ex.ee_block = ex->ee_block;
3595 zero_ex.ee_len = cpu_to_le16(map->m_lblk -
3596 ee_block);
3597 ext4_ext_store_pblock(&zero_ex,
3598 ext4_ext_pblock(ex));
3599 err = ext4_ext_zeroout(inode, &zero_ex);
3600 if (err)
3601 goto out;
3602 }
3603
Yongqiang Yang667eff32011-05-03 12:25:07 -04003604 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003605 split_map.m_len = map->m_lblk - ee_block + map->m_len;
3606 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003607 }
3608 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003609
Jan Karaae9e9c6a2014-10-30 10:53:17 -04003610 err = ext4_split_extent(handle, inode, ppath, &split_map, split_flag,
3611 flags);
3612 if (err > 0)
3613 err = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003614out:
Zheng Liuadb23552013-03-10 21:13:05 -04003615 /* If we have gotten a failure, don't zero out status tree */
3616 if (!err)
Zheng Liud7b2a002013-08-28 14:47:06 -04003617 err = ext4_zeroout_es(inode, &zero_ex);
Amit Arora56055d32007-07-17 21:42:38 -04003618 return err ? err : allocated;
3619}
3620
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003621/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003622 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003623 * ext4_get_blocks_dio_write() when DIO to write
Lukas Czerner556615d2014-04-20 23:45:47 -04003624 * to an unwritten extent.
Mingming Cao00314622009-09-28 15:49:08 -04003625 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003626 * Writing to an unwritten extent may result in splitting the unwritten
3627 * extent into multiple initialized/unwritten extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003628 * There are three possibilities:
Lukas Czerner556615d2014-04-20 23:45:47 -04003629 * a> There is no split required: Entire extent should be unwritten
Mingming Cao00314622009-09-28 15:49:08 -04003630 * b> Splits in two extents: Write is happening at either end of the extent
3631 * c> Splits in three extents: Somone is writing in middle of the extent
3632 *
Lukas Czernerb8a86842014-03-18 18:05:35 -04003633 * This works the same way in the case of initialized -> unwritten conversion.
3634 *
Mingming Cao00314622009-09-28 15:49:08 -04003635 * One of more index blocks maybe needed if the extent tree grow after
Lukas Czerner556615d2014-04-20 23:45:47 -04003636 * the unwritten extent split. To prevent ENOSPC occur at the IO
3637 * complete, we need to split the unwritten extent before DIO submit
3638 * the IO. The unwritten extent called at this time will be split
3639 * into three unwritten extent(at most). After IO complete, the part
Mingming Cao00314622009-09-28 15:49:08 -04003640 * being filled will be convert to initialized by the end_io callback function
3641 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003642 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003643 * Returns the size of unwritten extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003644 */
Lukas Czernerb8a86842014-03-18 18:05:35 -04003645static int ext4_split_convert_extents(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003646 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003647 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003648 struct ext4_ext_path **ppath,
Mingming Cao00314622009-09-28 15:49:08 -04003649 int flags)
3650{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003651 struct ext4_ext_path *path = *ppath;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003652 ext4_lblk_t eof_block;
3653 ext4_lblk_t ee_block;
3654 struct ext4_extent *ex;
3655 unsigned int ee_len;
3656 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003657
Lukas Czernerb8a86842014-03-18 18:05:35 -04003658 ext_debug("%s: inode %lu, logical block %llu, max_blocks %u\n",
3659 __func__, inode->i_ino,
3660 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003661
3662 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3663 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003664 if (eof_block < map->m_lblk + map->m_len)
3665 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003666 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003667 * It is safe to convert extent to initialized via explicit
3668 * zeroout only if extent is fully insde i_size or new_size.
3669 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003670 depth = ext_depth(inode);
3671 ex = path[depth].p_ext;
3672 ee_block = le32_to_cpu(ex->ee_block);
3673 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003674
Lukas Czernerb8a86842014-03-18 18:05:35 -04003675 /* Convert to unwritten */
3676 if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) {
3677 split_flag |= EXT4_EXT_DATA_VALID1;
3678 /* Convert to initialized */
3679 } else if (flags & EXT4_GET_BLOCKS_CONVERT) {
3680 split_flag |= ee_block + ee_len <= eof_block ?
3681 EXT4_EXT_MAY_ZEROOUT : 0;
Lukas Czerner556615d2014-04-20 23:45:47 -04003682 split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2);
Lukas Czernerb8a86842014-03-18 18:05:35 -04003683 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003684 flags |= EXT4_GET_BLOCKS_PRE_IO;
Theodore Ts'odfe50802014-09-01 14:37:09 -04003685 return ext4_split_extent(handle, inode, ppath, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003686}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003687
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003688static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003689 struct inode *inode,
3690 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003691 struct ext4_ext_path **ppath)
Mingming Cao00314622009-09-28 15:49:08 -04003692{
Theodore Ts'odfe50802014-09-01 14:37:09 -04003693 struct ext4_ext_path *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04003694 struct ext4_extent *ex;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003695 ext4_lblk_t ee_block;
3696 unsigned int ee_len;
Mingming Cao00314622009-09-28 15:49:08 -04003697 int depth;
3698 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003699
3700 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003701 ex = path[depth].p_ext;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003702 ee_block = le32_to_cpu(ex->ee_block);
3703 ee_len = ext4_ext_get_actual_len(ex);
Mingming Cao00314622009-09-28 15:49:08 -04003704
Yongqiang Yang197217a2011-05-03 11:45:29 -04003705 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3706 "block %llu, max_blocks %u\n", inode->i_ino,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003707 (unsigned long long)ee_block, ee_len);
3708
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003709 /* If extent is larger than requested it is a clear sign that we still
3710 * have some extent state machine issues left. So extent_split is still
3711 * required.
3712 * TODO: Once all related issues will be fixed this situation should be
3713 * illegal.
3714 */
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003715 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Dmitry Monakhovff95ec22013-03-04 00:41:05 -05003716#ifdef EXT4_DEBUG
3717 ext4_warning("Inode (%ld) finished: extent logical block %llu,"
3718 " len %u; IO logical block %llu, len %u\n",
3719 inode->i_ino, (unsigned long long)ee_block, ee_len,
3720 (unsigned long long)map->m_lblk, map->m_len);
3721#endif
Theodore Ts'odfe50802014-09-01 14:37:09 -04003722 err = ext4_split_convert_extents(handle, inode, map, ppath,
Lukas Czernerb8a86842014-03-18 18:05:35 -04003723 EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003724 if (err < 0)
Theodore Ts'odfe50802014-09-01 14:37:09 -04003725 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003726 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'odfe50802014-09-01 14:37:09 -04003727 if (IS_ERR(path))
3728 return PTR_ERR(path);
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003729 depth = ext_depth(inode);
3730 ex = path[depth].p_ext;
3731 }
Yongqiang Yang197217a2011-05-03 11:45:29 -04003732
Mingming Cao00314622009-09-28 15:49:08 -04003733 err = ext4_ext_get_access(handle, inode, path + depth);
3734 if (err)
3735 goto out;
3736 /* first mark the extent as initialized */
3737 ext4_ext_mark_initialized(ex);
3738
Yongqiang Yang197217a2011-05-03 11:45:29 -04003739 /* note: ext4_ext_correct_indexes() isn't needed here because
3740 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003741 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003742 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04003743
Mingming Cao00314622009-09-28 15:49:08 -04003744 /* Mark modified extent as dirty */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003745 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Mingming Cao00314622009-09-28 15:49:08 -04003746out:
3747 ext4_ext_show_leaf(inode, path);
3748 return err;
3749}
3750
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003751static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3752 sector_t block, int count)
3753{
3754 int i;
3755 for (i = 0; i < count; i++)
3756 unmap_underlying_metadata(bdev, block + i);
3757}
3758
Theodore Ts'o58590b02010-10-27 21:23:12 -04003759/*
3760 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3761 */
3762static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
Eric Sandeend002ebf2011-01-10 13:03:35 -05003763 ext4_lblk_t lblk,
Theodore Ts'o58590b02010-10-27 21:23:12 -04003764 struct ext4_ext_path *path,
3765 unsigned int len)
3766{
3767 int i, depth;
3768 struct ext4_extent_header *eh;
Sergey Senozhatsky65922cb2011-03-23 14:08:27 -04003769 struct ext4_extent *last_ex;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003770
3771 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3772 return 0;
3773
3774 depth = ext_depth(inode);
3775 eh = path[depth].p_hdr;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003776
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003777 /*
3778 * We're going to remove EOFBLOCKS_FL entirely in future so we
3779 * do not care for this case anymore. Simply remove the flag
3780 * if there are no extents.
3781 */
3782 if (unlikely(!eh->eh_entries))
3783 goto out;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003784 last_ex = EXT_LAST_EXTENT(eh);
3785 /*
3786 * We should clear the EOFBLOCKS_FL flag if we are writing the
3787 * last block in the last extent in the file. We test this by
3788 * first checking to see if the caller to
3789 * ext4_ext_get_blocks() was interested in the last block (or
3790 * a block beyond the last block) in the current extent. If
3791 * this turns out to be false, we can bail out from this
3792 * function immediately.
3793 */
Eric Sandeend002ebf2011-01-10 13:03:35 -05003794 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
Theodore Ts'o58590b02010-10-27 21:23:12 -04003795 ext4_ext_get_actual_len(last_ex))
3796 return 0;
3797 /*
3798 * If the caller does appear to be planning to write at or
3799 * beyond the end of the current extent, we then test to see
3800 * if the current extent is the last extent in the file, by
3801 * checking to make sure it was reached via the rightmost node
3802 * at each level of the tree.
3803 */
3804 for (i = depth-1; i >= 0; i--)
3805 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3806 return 0;
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003807out:
Theodore Ts'o58590b02010-10-27 21:23:12 -04003808 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3809 return ext4_mark_inode_dirty(handle, inode);
3810}
3811
Aditya Kali7b415bf2011-09-09 19:04:51 -04003812/**
3813 * ext4_find_delalloc_range: find delayed allocated block in the given range.
3814 *
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003815 * Return 1 if there is a delalloc block in the range, otherwise 0.
Aditya Kali7b415bf2011-09-09 19:04:51 -04003816 */
Zheng Liuf7fec032013-02-18 00:28:47 -05003817int ext4_find_delalloc_range(struct inode *inode,
3818 ext4_lblk_t lblk_start,
3819 ext4_lblk_t lblk_end)
Aditya Kali7b415bf2011-09-09 19:04:51 -04003820{
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003821 struct extent_status es;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003822
Yan, Zhenge30b5dc2013-05-03 02:15:52 -04003823 ext4_es_find_delayed_extent_range(inode, lblk_start, lblk_end, &es);
Zheng Liu06b0c882013-02-18 00:26:51 -05003824 if (es.es_len == 0)
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003825 return 0; /* there is no delay extent in this tree */
Zheng Liu06b0c882013-02-18 00:26:51 -05003826 else if (es.es_lblk <= lblk_start &&
3827 lblk_start < es.es_lblk + es.es_len)
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003828 return 1;
Zheng Liu06b0c882013-02-18 00:26:51 -05003829 else if (lblk_start <= es.es_lblk && es.es_lblk <= lblk_end)
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003830 return 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003831 else
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003832 return 0;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003833}
3834
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003835int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk)
Aditya Kali7b415bf2011-09-09 19:04:51 -04003836{
3837 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3838 ext4_lblk_t lblk_start, lblk_end;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003839 lblk_start = EXT4_LBLK_CMASK(sbi, lblk);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003840 lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
3841
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003842 return ext4_find_delalloc_range(inode, lblk_start, lblk_end);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003843}
3844
3845/**
3846 * Determines how many complete clusters (out of those specified by the 'map')
3847 * are under delalloc and were reserved quota for.
3848 * This function is called when we are writing out the blocks that were
3849 * originally written with their allocation delayed, but then the space was
3850 * allocated using fallocate() before the delayed allocation could be resolved.
3851 * The cases to look for are:
3852 * ('=' indicated delayed allocated blocks
3853 * '-' indicates non-delayed allocated blocks)
3854 * (a) partial clusters towards beginning and/or end outside of allocated range
3855 * are not delalloc'ed.
3856 * Ex:
3857 * |----c---=|====c====|====c====|===-c----|
3858 * |++++++ allocated ++++++|
3859 * ==> 4 complete clusters in above example
3860 *
3861 * (b) partial cluster (outside of allocated range) towards either end is
3862 * marked for delayed allocation. In this case, we will exclude that
3863 * cluster.
3864 * Ex:
3865 * |----====c========|========c========|
3866 * |++++++ allocated ++++++|
3867 * ==> 1 complete clusters in above example
3868 *
3869 * Ex:
3870 * |================c================|
3871 * |++++++ allocated ++++++|
3872 * ==> 0 complete clusters in above example
3873 *
3874 * The ext4_da_update_reserve_space will be called only if we
3875 * determine here that there were some "entire" clusters that span
3876 * this 'allocated' range.
3877 * In the non-bigalloc case, this function will just end up returning num_blks
3878 * without ever calling ext4_find_delalloc_range.
3879 */
3880static unsigned int
3881get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start,
3882 unsigned int num_blks)
3883{
3884 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3885 ext4_lblk_t alloc_cluster_start, alloc_cluster_end;
3886 ext4_lblk_t lblk_from, lblk_to, c_offset;
3887 unsigned int allocated_clusters = 0;
3888
3889 alloc_cluster_start = EXT4_B2C(sbi, lblk_start);
3890 alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1);
3891
3892 /* max possible clusters for this allocation */
3893 allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1;
3894
Aditya Kalid8990242011-09-09 19:18:51 -04003895 trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks);
3896
Aditya Kali7b415bf2011-09-09 19:04:51 -04003897 /* Check towards left side */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003898 c_offset = EXT4_LBLK_COFF(sbi, lblk_start);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003899 if (c_offset) {
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003900 lblk_from = EXT4_LBLK_CMASK(sbi, lblk_start);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003901 lblk_to = lblk_from + c_offset - 1;
3902
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003903 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to))
Aditya Kali7b415bf2011-09-09 19:04:51 -04003904 allocated_clusters--;
3905 }
3906
3907 /* Now check towards right. */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05003908 c_offset = EXT4_LBLK_COFF(sbi, lblk_start + num_blks);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003909 if (allocated_clusters && c_offset) {
3910 lblk_from = lblk_start + num_blks;
3911 lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1;
3912
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003913 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to))
Aditya Kali7b415bf2011-09-09 19:04:51 -04003914 allocated_clusters--;
3915 }
3916
3917 return allocated_clusters;
3918}
3919
Mingming Cao00314622009-09-28 15:49:08 -04003920static int
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003921convert_initialized_extent(handle_t *handle, struct inode *inode,
3922 struct ext4_map_blocks *map,
Theodore Ts'o4f224b82014-09-01 14:36:09 -04003923 struct ext4_ext_path **ppath, int flags,
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003924 unsigned int allocated, ext4_fsblk_t newblock)
Lukas Czernerb8a86842014-03-18 18:05:35 -04003925{
Theodore Ts'o4f224b82014-09-01 14:36:09 -04003926 struct ext4_ext_path *path = *ppath;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003927 struct ext4_extent *ex;
3928 ext4_lblk_t ee_block;
3929 unsigned int ee_len;
3930 int depth;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003931 int err = 0;
3932
3933 /*
3934 * Make sure that the extent is no bigger than we support with
Lukas Czerner556615d2014-04-20 23:45:47 -04003935 * unwritten extent
Lukas Czernerb8a86842014-03-18 18:05:35 -04003936 */
Lukas Czerner556615d2014-04-20 23:45:47 -04003937 if (map->m_len > EXT_UNWRITTEN_MAX_LEN)
3938 map->m_len = EXT_UNWRITTEN_MAX_LEN / 2;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003939
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003940 depth = ext_depth(inode);
3941 ex = path[depth].p_ext;
3942 ee_block = le32_to_cpu(ex->ee_block);
3943 ee_len = ext4_ext_get_actual_len(ex);
3944
3945 ext_debug("%s: inode %lu, logical"
3946 "block %llu, max_blocks %u\n", __func__, inode->i_ino,
3947 (unsigned long long)ee_block, ee_len);
3948
3949 if (ee_block != map->m_lblk || ee_len > map->m_len) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04003950 err = ext4_split_convert_extents(handle, inode, map, ppath,
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003951 EXT4_GET_BLOCKS_CONVERT_UNWRITTEN);
3952 if (err < 0)
3953 return err;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04003954 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003955 if (IS_ERR(path))
3956 return PTR_ERR(path);
3957 depth = ext_depth(inode);
3958 ex = path[depth].p_ext;
3959 if (!ex) {
3960 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3961 (unsigned long) map->m_lblk);
3962 return -EIO;
3963 }
3964 }
3965
3966 err = ext4_ext_get_access(handle, inode, path + depth);
3967 if (err)
3968 return err;
3969 /* first mark the extent as unwritten */
3970 ext4_ext_mark_unwritten(ex);
3971
3972 /* note: ext4_ext_correct_indexes() isn't needed here because
3973 * borders are not changed
3974 */
3975 ext4_ext_try_to_merge(handle, inode, path, ex);
3976
3977 /* Mark modified extent as dirty */
3978 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
3979 if (err)
3980 return err;
3981 ext4_ext_show_leaf(inode, path);
3982
3983 ext4_update_inode_fsync_trans(handle, inode, 1);
3984 err = check_eofblocks_fl(handle, inode, map->m_lblk, path, map->m_len);
3985 if (err)
3986 return err;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003987 map->m_flags |= EXT4_MAP_UNWRITTEN;
3988 if (allocated > map->m_len)
3989 allocated = map->m_len;
3990 map->m_len = allocated;
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04003991 return allocated;
Lukas Czernerb8a86842014-03-18 18:05:35 -04003992}
3993
3994static int
Lukas Czerner556615d2014-04-20 23:45:47 -04003995ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003996 struct ext4_map_blocks *map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04003997 struct ext4_ext_path **ppath, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003998 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003999{
Theodore Ts'odfe50802014-09-01 14:37:09 -04004000 struct ext4_ext_path *path = *ppath;
Mingming Cao00314622009-09-28 15:49:08 -04004001 int ret = 0;
4002 int err = 0;
Dmitry Monakhovf45ee3a2012-09-28 23:21:09 -04004003 ext4_io_end_t *io = ext4_inode_aio(inode);
Mingming Cao00314622009-09-28 15:49:08 -04004004
Lukas Czerner556615d2014-04-20 23:45:47 -04004005 ext_debug("ext4_ext_handle_unwritten_extents: inode %lu, logical "
Zheng Liu88635ca2011-12-28 19:00:25 -05004006 "block %llu, max_blocks %u, flags %x, allocated %u\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004007 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04004008 flags, allocated);
4009 ext4_ext_show_leaf(inode, path);
4010
Lukas Czerner27dd4382013-04-09 22:11:22 -04004011 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04004012 * When writing into unwritten space, we should not fail to
Lukas Czerner27dd4382013-04-09 22:11:22 -04004013 * allocate metadata blocks for the new extent block if needed.
4014 */
4015 flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL;
4016
Lukas Czerner556615d2014-04-20 23:45:47 -04004017 trace_ext4_ext_handle_unwritten_extents(inode, map, flags,
Zheng Liub5645532012-11-08 14:33:43 -05004018 allocated, newblock);
Aditya Kalid8990242011-09-09 19:18:51 -04004019
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004020 /* get_block() before submit the IO, split the extent */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04004021 if (flags & EXT4_GET_BLOCKS_PRE_IO) {
Theodore Ts'odfe50802014-09-01 14:37:09 -04004022 ret = ext4_split_convert_extents(handle, inode, map, ppath,
4023 flags | EXT4_GET_BLOCKS_CONVERT);
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004024 if (ret <= 0)
4025 goto out;
Mingming5f5249502009-11-10 10:48:04 -05004026 /*
4027 * Flag the inode(non aio case) or end_io struct (aio case)
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004028 * that this IO needs to conversion to written when IO is
Mingming5f5249502009-11-10 10:48:04 -05004029 * completed
4030 */
Tao Ma0edeb712011-10-31 17:30:44 -04004031 if (io)
4032 ext4_set_io_unwritten_flag(inode, io);
4033 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004034 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
Zheng Liua25a4e12013-02-18 00:28:04 -05004035 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004036 goto out;
4037 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004038 /* IO end_io complete, convert the filled extent to written */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04004039 if (flags & EXT4_GET_BLOCKS_CONVERT) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04004040 ret = ext4_convert_unwritten_extents_endio(handle, inode, map,
Theodore Ts'odfe50802014-09-01 14:37:09 -04004041 ppath);
Theodore Ts'o58590b02010-10-27 21:23:12 -04004042 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05004043 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05004044 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4045 path, map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04004046 } else
4047 err = ret;
Zheng Liucdee7842013-03-10 21:08:52 -04004048 map->m_flags |= EXT4_MAP_MAPPED;
Eric Whitney15cc1762014-02-12 10:42:45 -05004049 map->m_pblk = newblock;
Zheng Liucdee7842013-03-10 21:08:52 -04004050 if (allocated > map->m_len)
4051 allocated = map->m_len;
4052 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04004053 goto out2;
4054 }
4055 /* buffered IO case */
4056 /*
4057 * repeat fallocate creation request
4058 * we already have an unwritten extent
4059 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004060 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
Zheng Liua25a4e12013-02-18 00:28:04 -05004061 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004062 goto map_out;
Zheng Liua25a4e12013-02-18 00:28:04 -05004063 }
Mingming Cao00314622009-09-28 15:49:08 -04004064
4065 /* buffered READ or buffered write_begin() lookup */
4066 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
4067 /*
4068 * We have blocks reserved already. We
4069 * return allocated blocks so that delalloc
4070 * won't do block reservation for us. But
4071 * the buffer head will be unmapped so that
4072 * a read from the block returns 0s.
4073 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004074 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04004075 goto out1;
4076 }
4077
4078 /* buffered write, writepage time, convert*/
Theodore Ts'odfe50802014-09-01 14:37:09 -04004079 ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags);
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004080 if (ret >= 0)
Jan Karab436b9b2009-12-08 23:51:10 -05004081 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04004082out:
4083 if (ret <= 0) {
4084 err = ret;
4085 goto out2;
4086 } else
4087 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004088 map->m_flags |= EXT4_MAP_NEW;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05004089 /*
4090 * if we allocated more blocks than requested
4091 * we need to make sure we unmap the extra block
4092 * allocated. The actual needed block will get
4093 * unmapped later when we find the buffer_head marked
4094 * new.
4095 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004096 if (allocated > map->m_len) {
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05004097 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004098 newblock + map->m_len,
4099 allocated - map->m_len);
4100 allocated = map->m_len;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05004101 }
Zheng Liu3a225672013-03-10 21:20:23 -04004102 map->m_len = allocated;
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004103
4104 /*
4105 * If we have done fallocate with the offset that is already
4106 * delayed allocated, we would have block reservation
4107 * and quota reservation done in the delayed write path.
4108 * But fallocate would have already updated quota and block
4109 * count for this offset. So cancel these reservation
4110 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04004111 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
4112 unsigned int reserved_clusters;
4113 reserved_clusters = get_reserved_cluster_alloc(inode,
4114 map->m_lblk, map->m_len);
4115 if (reserved_clusters)
4116 ext4_da_update_reserve_space(inode,
4117 reserved_clusters,
4118 0);
4119 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004120
Mingming Cao00314622009-09-28 15:49:08 -04004121map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004122 map->m_flags |= EXT4_MAP_MAPPED;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004123 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
4124 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
4125 map->m_len);
4126 if (err < 0)
4127 goto out2;
4128 }
Mingming Cao00314622009-09-28 15:49:08 -04004129out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004130 if (allocated > map->m_len)
4131 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04004132 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004133 map->m_pblk = newblock;
4134 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04004135out2:
Mingming Cao00314622009-09-28 15:49:08 -04004136 return err ? err : allocated;
4137}
Theodore Ts'o58590b02010-10-27 21:23:12 -04004138
Mingming Cao00314622009-09-28 15:49:08 -04004139/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004140 * get_implied_cluster_alloc - check to see if the requested
4141 * allocation (in the map structure) overlaps with a cluster already
4142 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04004143 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004144 * @map The requested lblk->pblk mapping
4145 * @ex The extent structure which might contain an implied
4146 * cluster allocation
4147 *
4148 * This function is called by ext4_ext_map_blocks() after we failed to
4149 * find blocks that were already in the inode's extent tree. Hence,
4150 * we know that the beginning of the requested region cannot overlap
4151 * the extent from the inode's extent tree. There are three cases we
4152 * want to catch. The first is this case:
4153 *
4154 * |--- cluster # N--|
4155 * |--- extent ---| |---- requested region ---|
4156 * |==========|
4157 *
4158 * The second case that we need to test for is this one:
4159 *
4160 * |--------- cluster # N ----------------|
4161 * |--- requested region --| |------- extent ----|
4162 * |=======================|
4163 *
4164 * The third case is when the requested region lies between two extents
4165 * within the same cluster:
4166 * |------------- cluster # N-------------|
4167 * |----- ex -----| |---- ex_right ----|
4168 * |------ requested region ------|
4169 * |================|
4170 *
4171 * In each of the above cases, we need to set the map->m_pblk and
4172 * map->m_len so it corresponds to the return the extent labelled as
4173 * "|====|" from cluster #N, since it is already in use for data in
4174 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
4175 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
4176 * as a new "allocated" block region. Otherwise, we will return 0 and
4177 * ext4_ext_map_blocks() will then allocate one or more new clusters
4178 * by calling ext4_mb_new_blocks().
4179 */
Aditya Kalid8990242011-09-09 19:18:51 -04004180static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004181 struct ext4_map_blocks *map,
4182 struct ext4_extent *ex,
4183 struct ext4_ext_path *path)
4184{
Aditya Kalid8990242011-09-09 19:18:51 -04004185 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004186 ext4_lblk_t c_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004187 ext4_lblk_t ex_cluster_start, ex_cluster_end;
Curt Wohlgemuth14d7f3e2011-12-18 17:39:02 -05004188 ext4_lblk_t rr_cluster_start;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004189 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
4190 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
4191 unsigned short ee_len = ext4_ext_get_actual_len(ex);
4192
4193 /* The extent passed in that we are trying to match */
4194 ex_cluster_start = EXT4_B2C(sbi, ee_block);
4195 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
4196
4197 /* The requested region passed into ext4_map_blocks() */
4198 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004199
4200 if ((rr_cluster_start == ex_cluster_end) ||
4201 (rr_cluster_start == ex_cluster_start)) {
4202 if (rr_cluster_start == ex_cluster_end)
4203 ee_start += ee_len - 1;
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004204 map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004205 map->m_len = min(map->m_len,
4206 (unsigned) sbi->s_cluster_ratio - c_offset);
4207 /*
4208 * Check for and handle this case:
4209 *
4210 * |--------- cluster # N-------------|
4211 * |------- extent ----|
4212 * |--- requested region ---|
4213 * |===========|
4214 */
4215
4216 if (map->m_lblk < ee_block)
4217 map->m_len = min(map->m_len, ee_block - map->m_lblk);
4218
4219 /*
4220 * Check for the case where there is already another allocated
4221 * block to the right of 'ex' but before the end of the cluster.
4222 *
4223 * |------------- cluster # N-------------|
4224 * |----- ex -----| |---- ex_right ----|
4225 * |------ requested region ------|
4226 * |================|
4227 */
4228 if (map->m_lblk > ee_block) {
4229 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
4230 map->m_len = min(map->m_len, next - map->m_lblk);
4231 }
Aditya Kalid8990242011-09-09 19:18:51 -04004232
4233 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004234 return 1;
4235 }
Aditya Kalid8990242011-09-09 19:18:51 -04004236
4237 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004238 return 0;
4239}
4240
4241
4242/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05004243 * Block allocation/map/preallocation routine for extents based files
4244 *
4245 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004246 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004247 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
4248 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05004249 *
4250 * return > 0, number of of blocks already mapped/allocated
4251 * if create == 0 and these are pre-allocated blocks
4252 * buffer head is unmapped
4253 * otherwise blocks are mapped
4254 *
4255 * return = 0, if plain look up failed (blocks have not been allocated)
4256 * buffer head is unmapped
4257 *
4258 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05004259 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004260int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
4261 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07004262{
4263 struct ext4_ext_path *path = NULL;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004264 struct ext4_extent newex, *ex, *ex2;
4265 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004266 ext4_fsblk_t newblock = 0;
Eric Whitneyce37c422014-02-19 18:52:39 -05004267 int free_on_err = 0, err = 0, depth, ret;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004268 unsigned int allocated = 0, offset = 0;
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004269 unsigned int allocated_clusters = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05004270 struct ext4_allocation_request ar;
Dmitry Monakhovf45ee3a2012-09-28 23:21:09 -04004271 ext4_io_end_t *io = ext4_inode_aio(inode);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004272 ext4_lblk_t cluster_offset;
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004273 int set_unwritten = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07004274
Mingming84fe3be2009-09-01 08:44:37 -04004275 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004276 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004277 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004278
Alex Tomasa86c6182006-10-11 01:21:03 -07004279 /* find extent for this block */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004280 path = ext4_find_extent(inode, map->m_lblk, NULL, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004281 if (IS_ERR(path)) {
4282 err = PTR_ERR(path);
4283 path = NULL;
4284 goto out2;
4285 }
4286
4287 depth = ext_depth(inode);
4288
4289 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004290 * consistent leaf must not be empty;
4291 * this situation is possible, though, _during_ tree modification;
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004292 * this is why assert can't be put in ext4_find_extent()
Alex Tomasa86c6182006-10-11 01:21:03 -07004293 */
Frank Mayhar273df552010-03-02 11:46:09 -05004294 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
4295 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04004296 "lblock: %lu, depth: %d pblock %lld",
4297 (unsigned long) map->m_lblk, depth,
4298 path[depth].p_block);
Surbhi Palande034fb4c2009-12-14 09:53:52 -05004299 err = -EIO;
4300 goto out2;
4301 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004302
Avantika Mathur7e028972006-12-06 20:41:33 -08004303 ex = path[depth].p_ext;
4304 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004305 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04004306 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004307 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004308
Lukas Czernerb8a86842014-03-18 18:05:35 -04004309
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004310 /*
Lukas Czerner556615d2014-04-20 23:45:47 -04004311 * unwritten extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04004312 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07004313 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004314 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04004315
4316 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
4317
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004318 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004319 if (in_range(map->m_lblk, ee_block, ee_len)) {
4320 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004321 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004322 allocated = ee_len - (map->m_lblk - ee_block);
4323 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
4324 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04004325
Lukas Czernerb8a86842014-03-18 18:05:35 -04004326 /*
4327 * If the extent is initialized check whether the
4328 * caller wants to convert it to unwritten.
4329 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004330 if ((!ext4_ext_is_unwritten(ex)) &&
Lukas Czernerb8a86842014-03-18 18:05:35 -04004331 (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) {
Theodore Ts'oe8b83d932014-09-01 14:35:09 -04004332 allocated = convert_initialized_extent(
Theodore Ts'o4f224b82014-09-01 14:36:09 -04004333 handle, inode, map, &path,
4334 flags, allocated, newblock);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004335 goto out2;
Lukas Czerner556615d2014-04-20 23:45:47 -04004336 } else if (!ext4_ext_is_unwritten(ex))
Lukas Czerner78771912012-03-19 23:05:43 -04004337 goto out;
Zheng Liu69eb33d2013-02-18 00:31:07 -05004338
Lukas Czerner556615d2014-04-20 23:45:47 -04004339 ret = ext4_ext_handle_unwritten_extents(
Theodore Ts'odfe50802014-09-01 14:37:09 -04004340 handle, inode, map, &path, flags,
Lukas Czerner78771912012-03-19 23:05:43 -04004341 allocated, newblock);
Eric Whitneyce37c422014-02-19 18:52:39 -05004342 if (ret < 0)
4343 err = ret;
4344 else
4345 allocated = ret;
Eric Whitney31cf0f22014-03-13 23:14:46 -04004346 goto out2;
Alex Tomasa86c6182006-10-11 01:21:03 -07004347 }
4348 }
4349
Aditya Kali7b415bf2011-09-09 19:04:51 -04004350 if ((sbi->s_cluster_ratio > 1) &&
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05004351 ext4_find_delalloc_cluster(inode, map->m_lblk))
Aditya Kali7b415bf2011-09-09 19:04:51 -04004352 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
4353
Alex Tomasa86c6182006-10-11 01:21:03 -07004354 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004355 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07004356 * we couldn't try to create block if create flag is zero
4357 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04004358 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04004359 /*
4360 * put just found gap into cache to speed up
4361 * subsequent requests
4362 */
Zheng Liud100eef2013-02-18 00:29:59 -05004363 if ((flags & EXT4_GET_BLOCKS_NO_PUT_HOLE) == 0)
4364 ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
Alex Tomasa86c6182006-10-11 01:21:03 -07004365 goto out2;
4366 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004367
Alex Tomasa86c6182006-10-11 01:21:03 -07004368 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004369 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07004370 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04004371 map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004372 newex.ee_block = cpu_to_le32(map->m_lblk);
Eric Whitneyd0abafa2014-01-06 14:00:23 -05004373 cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004374
4375 /*
4376 * If we are doing bigalloc, check to see if the extent returned
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04004377 * by ext4_find_extent() implies a cluster we can use.
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004378 */
4379 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04004380 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004381 ar.len = allocated = map->m_len;
4382 newblock = map->m_pblk;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004383 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004384 goto got_allocated_blocks;
4385 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004386
Alex Tomasc9de5602008-01-29 00:19:52 -05004387 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004388 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05004389 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
4390 if (err)
4391 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004392 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004393 ex2 = NULL;
4394 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
Alex Tomasc9de5602008-01-29 00:19:52 -05004395 if (err)
4396 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04004397
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004398 /* Check if the extent after searching to the right implies a
4399 * cluster we can use. */
4400 if ((sbi->s_cluster_ratio > 1) && ex2 &&
Aditya Kalid8990242011-09-09 19:18:51 -04004401 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004402 ar.len = allocated = map->m_len;
4403 newblock = map->m_pblk;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004404 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004405 goto got_allocated_blocks;
4406 }
4407
Amit Arora749269f2007-07-18 09:02:56 -04004408 /*
4409 * See if request is beyond maximum number of blocks we can have in
4410 * a single extent. For an initialized extent this limit is
Lukas Czerner556615d2014-04-20 23:45:47 -04004411 * EXT_INIT_MAX_LEN and for an unwritten extent this limit is
4412 * EXT_UNWRITTEN_MAX_LEN.
Amit Arora749269f2007-07-18 09:02:56 -04004413 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004414 if (map->m_len > EXT_INIT_MAX_LEN &&
Lukas Czerner556615d2014-04-20 23:45:47 -04004415 !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004416 map->m_len = EXT_INIT_MAX_LEN;
Lukas Czerner556615d2014-04-20 23:45:47 -04004417 else if (map->m_len > EXT_UNWRITTEN_MAX_LEN &&
4418 (flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
4419 map->m_len = EXT_UNWRITTEN_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04004420
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004421 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004422 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004423 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04004424 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004425 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04004426 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004427 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004428
4429 /* allocate new block */
4430 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004431 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
4432 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004433 /*
4434 * We calculate the offset from the beginning of the cluster
4435 * for the logical block number, since when we allocate a
4436 * physical cluster, the physical block should start at the
4437 * same offset from the beginning of the cluster. This is
4438 * needed so that future calls to get_implied_cluster_alloc()
4439 * work correctly.
4440 */
Theodore Ts'of5a44db2013-12-20 09:29:35 -05004441 offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004442 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
4443 ar.goal -= offset;
4444 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05004445 if (S_ISREG(inode->i_mode))
4446 ar.flags = EXT4_MB_HINT_DATA;
4447 else
4448 /* disable in-core preallocation for non-regular files */
4449 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04004450 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4451 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Theodore Ts'oe3cf5d52014-09-04 18:07:25 -04004452 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
4453 ar.flags |= EXT4_MB_DELALLOC_RESERVED;
Alex Tomasc9de5602008-01-29 00:19:52 -05004454 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004455 if (!newblock)
4456 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04004457 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004458 ar.goal, newblock, allocated);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004459 free_on_err = 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004460 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004461 ar.len = EXT4_C2B(sbi, ar.len) - offset;
4462 if (ar.len > allocated)
4463 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004464
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004465got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07004466 /* try to insert new extent into found leaf and return */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004467 ext4_ext_store_pblock(&newex, newblock + offset);
Alex Tomasc9de5602008-01-29 00:19:52 -05004468 newex.ee_len = cpu_to_le16(ar.len);
Lukas Czerner556615d2014-04-20 23:45:47 -04004469 /* Mark unwritten */
4470 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT){
4471 ext4_ext_mark_unwritten(&newex);
Zheng Liua25a4e12013-02-18 00:28:04 -05004472 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004473 /*
Jiaying Zhang744692d2010-03-04 16:14:02 -05004474 * io_end structure was created for every IO write to an
Lukas Czerner556615d2014-04-20 23:45:47 -04004475 * unwritten extent. To avoid unnecessary conversion,
Jiaying Zhang744692d2010-03-04 16:14:02 -05004476 * here we flag the IO that really needs the conversion.
Mingming5f5249502009-11-10 10:48:04 -05004477 * For non asycn direct IO case, flag the inode state
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004478 * that we need to perform conversion when IO is done.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004479 */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04004480 if (flags & EXT4_GET_BLOCKS_PRE_IO)
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004481 set_unwritten = 1;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004482 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004483
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004484 err = 0;
4485 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
4486 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4487 path, ar.len);
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004488 if (!err)
Theodore Ts'odfe50802014-09-01 14:37:09 -04004489 err = ext4_ext_insert_extent(handle, inode, &path,
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004490 &newex, flags);
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004491
4492 if (!err && set_unwritten) {
4493 if (io)
4494 ext4_set_io_unwritten_flag(inode, io);
4495 else
4496 ext4_set_inode_state(inode,
4497 EXT4_STATE_DIO_UNWRITTEN);
4498 }
4499
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004500 if (err && free_on_err) {
Maxim Patlasov7132de72011-07-10 19:37:48 -04004501 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
4502 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
Alex Tomas315054f2007-05-24 13:04:25 -04004503 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05004504 /* not a good idea to call discard here directly,
4505 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004506 ext4_discard_preallocations(inode);
Theodore Ts'oc8e15132013-07-15 00:09:37 -04004507 ext4_free_blocks(handle, inode, NULL, newblock,
4508 EXT4_C2B(sbi, allocated_clusters), fb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004509 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04004510 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004511
Alex Tomasa86c6182006-10-11 01:21:03 -07004512 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04004513 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004514 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004515 if (allocated > map->m_len)
4516 allocated = map->m_len;
4517 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07004518
Jan Karab436b9b2009-12-08 23:51:10 -05004519 /*
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004520 * Update reserved blocks/metadata blocks after successful
4521 * block allocation which had been deferred till now.
4522 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04004523 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004524 unsigned int reserved_clusters;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004525 /*
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004526 * Check how many clusters we had reserved this allocated range
Aditya Kali7b415bf2011-09-09 19:04:51 -04004527 */
4528 reserved_clusters = get_reserved_cluster_alloc(inode,
4529 map->m_lblk, allocated);
4530 if (map->m_flags & EXT4_MAP_FROM_CLUSTER) {
4531 if (reserved_clusters) {
4532 /*
4533 * We have clusters reserved for this range.
4534 * But since we are not doing actual allocation
4535 * and are simply using blocks from previously
4536 * allocated cluster, we should release the
4537 * reservation and not claim quota.
4538 */
4539 ext4_da_update_reserve_space(inode,
4540 reserved_clusters, 0);
4541 }
4542 } else {
4543 BUG_ON(allocated_clusters < reserved_clusters);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004544 if (reserved_clusters < allocated_clusters) {
Aditya Kali5356f2612011-09-09 19:20:51 -04004545 struct ext4_inode_info *ei = EXT4_I(inode);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004546 int reservation = allocated_clusters -
4547 reserved_clusters;
4548 /*
4549 * It seems we claimed few clusters outside of
4550 * the range of this allocation. We should give
4551 * it back to the reservation pool. This can
4552 * happen in the following case:
4553 *
4554 * * Suppose s_cluster_ratio is 4 (i.e., each
4555 * cluster has 4 blocks. Thus, the clusters
4556 * are [0-3],[4-7],[8-11]...
4557 * * First comes delayed allocation write for
4558 * logical blocks 10 & 11. Since there were no
4559 * previous delayed allocated blocks in the
4560 * range [8-11], we would reserve 1 cluster
4561 * for this write.
4562 * * Next comes write for logical blocks 3 to 8.
4563 * In this case, we will reserve 2 clusters
4564 * (for [0-3] and [4-7]; and not for [8-11] as
4565 * that range has a delayed allocated blocks.
4566 * Thus total reserved clusters now becomes 3.
4567 * * Now, during the delayed allocation writeout
4568 * time, we will first write blocks [3-8] and
4569 * allocate 3 clusters for writing these
4570 * blocks. Also, we would claim all these
4571 * three clusters above.
4572 * * Now when we come here to writeout the
4573 * blocks [10-11], we would expect to claim
4574 * the reservation of 1 cluster we had made
4575 * (and we would claim it since there are no
4576 * more delayed allocated blocks in the range
4577 * [8-11]. But our reserved cluster count had
4578 * already gone to 0.
4579 *
4580 * Thus, at the step 4 above when we determine
4581 * that there are still some unwritten delayed
4582 * allocated blocks outside of our current
4583 * block range, we should increment the
4584 * reserved clusters count so that when the
4585 * remaining blocks finally gets written, we
4586 * could claim them.
4587 */
Aditya Kali5356f2612011-09-09 19:20:51 -04004588 dquot_reserve_block(inode,
4589 EXT4_C2B(sbi, reservation));
4590 spin_lock(&ei->i_block_reservation_lock);
4591 ei->i_reserved_data_blocks += reservation;
4592 spin_unlock(&ei->i_block_reservation_lock);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004593 }
Lukas Czerner232ec872013-03-10 22:46:30 -04004594 /*
4595 * We will claim quota for all newly allocated blocks.
4596 * We're updating the reserved space *after* the
4597 * correction above so we do not accidentally free
4598 * all the metadata reservation because we might
4599 * actually need it later on.
4600 */
4601 ext4_da_update_reserve_space(inode, allocated_clusters,
4602 1);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004603 }
4604 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004605
4606 /*
Jan Karab436b9b2009-12-08 23:51:10 -05004607 * Cache the extent and update transaction to commit on fdatasync only
Lukas Czerner556615d2014-04-20 23:45:47 -04004608 * when it is _not_ an unwritten extent.
Jan Karab436b9b2009-12-08 23:51:10 -05004609 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004610 if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0)
Jan Karab436b9b2009-12-08 23:51:10 -05004611 ext4_update_inode_fsync_trans(handle, inode, 1);
Zheng Liu69eb33d2013-02-18 00:31:07 -05004612 else
Jan Karab436b9b2009-12-08 23:51:10 -05004613 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004614out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004615 if (allocated > map->m_len)
4616 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004617 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004618 map->m_flags |= EXT4_MAP_MAPPED;
4619 map->m_pblk = newblock;
4620 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004621out2:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04004622 ext4_ext_drop_refs(path);
4623 kfree(path);
Allison Hendersone8613042011-05-25 07:41:46 -04004624
Theodore Ts'o63b99962013-07-16 10:28:47 -04004625 trace_ext4_ext_map_blocks_exit(inode, flags, map,
4626 err ? err : allocated);
4627 ext4_es_lru_add(inode);
Lukas Czerner78771912012-03-19 23:05:43 -04004628 return err ? err : allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004629}
4630
Theodore Ts'o819c4922013-04-03 12:47:17 -04004631void ext4_ext_truncate(handle_t *handle, struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004632{
Alex Tomasa86c6182006-10-11 01:21:03 -07004633 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004634 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004635 int err = 0;
4636
4637 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004638 * TODO: optimization is possible here.
4639 * Probably we need not scan at all,
4640 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004641 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004642
4643 /* we have to know where to truncate from in crash case */
4644 EXT4_I(inode)->i_disksize = inode->i_size;
4645 ext4_mark_inode_dirty(handle, inode);
4646
4647 last_block = (inode->i_size + sb->s_blocksize - 1)
4648 >> EXT4_BLOCK_SIZE_BITS(sb);
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004649retry:
Zheng Liu51865fd2012-11-08 21:57:32 -05004650 err = ext4_es_remove_extent(inode, last_block,
4651 EXT_MAX_BLOCKS - last_block);
Theodore Ts'o94eec0f2013-07-29 12:12:56 -04004652 if (err == -ENOMEM) {
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004653 cond_resched();
4654 congestion_wait(BLK_RW_ASYNC, HZ/50);
4655 goto retry;
4656 }
4657 if (err) {
4658 ext4_std_error(inode->i_sb, err);
4659 return;
4660 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04004661 err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
Theodore Ts'o8acd5e92013-07-15 00:09:19 -04004662 ext4_std_error(inode->i_sb, err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004663}
4664
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004665static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004666 ext4_lblk_t len, loff_t new_size,
4667 int flags, int mode)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004668{
Al Viro496ad9a2013-01-23 17:07:38 -05004669 struct inode *inode = file_inode(file);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004670 handle_t *handle;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004671 int ret = 0;
4672 int ret2 = 0;
4673 int retries = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004674 struct ext4_map_blocks map;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004675 unsigned int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004676 loff_t epos;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004677
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004678 map.m_lblk = offset;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004679 map.m_len = len;
Greg Harm3c6fe772011-10-31 18:41:47 -04004680 /*
4681 * Don't normalize the request if it can fit in one extent so
4682 * that it doesn't get unnecessarily split into multiple
4683 * extents.
4684 */
Lukas Czerner556615d2014-04-20 23:45:47 -04004685 if (len <= EXT_UNWRITTEN_MAX_LEN)
Greg Harm3c6fe772011-10-31 18:41:47 -04004686 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
Dmitry Monakhov60d46162012-10-05 11:32:02 -04004687
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004688 /*
4689 * credits to insert 1 extent into extent tree
4690 */
4691 credits = ext4_chunk_trans_blocks(inode, len);
4692
Amit Aroraa2df2a62007-07-17 21:42:41 -04004693retry:
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004694 while (ret >= 0 && len) {
Theodore Ts'o9924a922013-02-08 21:59:22 -05004695 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
4696 credits);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004697 if (IS_ERR(handle)) {
4698 ret = PTR_ERR(handle);
4699 break;
4700 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004701 ret = ext4_map_blocks(handle, inode, &map, flags);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05004702 if (ret <= 0) {
Lukas Czernerf282ac12014-03-18 17:44:35 -04004703 ext4_debug("inode #%lu: block %u: len %u: "
4704 "ext4_ext_map_blocks returned %d",
4705 inode->i_ino, map.m_lblk,
4706 map.m_len, ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004707 ext4_mark_inode_dirty(handle, inode);
4708 ret2 = ext4_journal_stop(handle);
4709 break;
4710 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004711 map.m_lblk += ret;
4712 map.m_len = len = len - ret;
4713 epos = (loff_t)map.m_lblk << inode->i_blkbits;
4714 inode->i_ctime = ext4_current_time(inode);
4715 if (new_size) {
4716 if (epos > new_size)
4717 epos = new_size;
4718 if (ext4_update_inode_size(inode, epos) & 0x1)
4719 inode->i_mtime = inode->i_ctime;
4720 } else {
4721 if (epos > inode->i_size)
4722 ext4_set_inode_flag(inode,
4723 EXT4_INODE_EOFBLOCKS);
4724 }
4725 ext4_mark_inode_dirty(handle, inode);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004726 ret2 = ext4_journal_stop(handle);
4727 if (ret2)
4728 break;
4729 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004730 if (ret == -ENOSPC &&
4731 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4732 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004733 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004734 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04004735
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004736 return ret > 0 ? ret2 : ret;
4737}
4738
Lukas Czernerb8a86842014-03-18 18:05:35 -04004739static long ext4_zero_range(struct file *file, loff_t offset,
4740 loff_t len, int mode)
4741{
4742 struct inode *inode = file_inode(file);
4743 handle_t *handle = NULL;
4744 unsigned int max_blocks;
4745 loff_t new_size = 0;
4746 int ret = 0;
4747 int flags;
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004748 int credits;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004749 int partial_begin, partial_end;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004750 loff_t start, end;
4751 ext4_lblk_t lblk;
4752 struct address_space *mapping = inode->i_mapping;
4753 unsigned int blkbits = inode->i_blkbits;
4754
4755 trace_ext4_zero_range(inode, offset, len, mode);
4756
jon ernst6c5e73d2014-04-18 11:50:35 -04004757 if (!S_ISREG(inode->i_mode))
4758 return -EINVAL;
4759
Namjae Jeone1ee60f2014-05-27 12:48:55 -04004760 /* Call ext4_force_commit to flush all data in case of data=journal. */
4761 if (ext4_should_journal_data(inode)) {
4762 ret = ext4_force_commit(inode->i_sb);
4763 if (ret)
4764 return ret;
4765 }
4766
Lukas Czernerb8a86842014-03-18 18:05:35 -04004767 /*
4768 * Write out all dirty pages to avoid race conditions
4769 * Then release them.
4770 */
4771 if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
4772 ret = filemap_write_and_wait_range(mapping, offset,
4773 offset + len - 1);
4774 if (ret)
4775 return ret;
4776 }
4777
4778 /*
4779 * Round up offset. This is not fallocate, we neet to zero out
4780 * blocks, so convert interior block aligned part of the range to
4781 * unwritten and possibly manually zero out unaligned parts of the
4782 * range.
4783 */
4784 start = round_up(offset, 1 << blkbits);
4785 end = round_down((offset + len), 1 << blkbits);
4786
4787 if (start < offset || end > offset + len)
4788 return -EINVAL;
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004789 partial_begin = offset & ((1 << blkbits) - 1);
4790 partial_end = (offset + len) & ((1 << blkbits) - 1);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004791
4792 lblk = start >> blkbits;
4793 max_blocks = (end >> blkbits);
4794 if (max_blocks < lblk)
4795 max_blocks = 0;
4796 else
4797 max_blocks -= lblk;
4798
Lukas Czernerb8a86842014-03-18 18:05:35 -04004799 mutex_lock(&inode->i_mutex);
4800
4801 /*
4802 * Indirect files do not support unwritten extnets
4803 */
4804 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4805 ret = -EOPNOTSUPP;
4806 goto out_mutex;
4807 }
4808
4809 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
4810 offset + len > i_size_read(inode)) {
4811 new_size = offset + len;
4812 ret = inode_newsize_ok(inode, new_size);
4813 if (ret)
4814 goto out_mutex;
Lukas Czernerb8a86842014-03-18 18:05:35 -04004815 }
4816
Lukas Czernerb9fca5c2015-04-03 00:09:13 -04004817 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
4818 if (mode & FALLOC_FL_KEEP_SIZE)
4819 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4820
4821 /* Preallocate the range including the unaligned edges */
4822 if (partial_begin || partial_end) {
4823 ret = ext4_alloc_file_blocks(file,
4824 round_down(offset, 1 << blkbits) >> blkbits,
4825 (round_up((offset + len), 1 << blkbits) -
4826 round_down(offset, 1 << blkbits)) >> blkbits,
4827 new_size, flags, mode);
4828 if (ret)
4829 goto out_mutex;
4830
4831 }
4832
4833 /* Zero range excluding the unaligned edges */
Lukas Czernerb8a86842014-03-18 18:05:35 -04004834 if (max_blocks > 0) {
Lukas Czernerb9fca5c2015-04-03 00:09:13 -04004835 flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN |
4836 EXT4_EX_NOCACHE);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004837
4838 /* Now release the pages and zero block aligned part of pages*/
4839 truncate_pagecache_range(inode, start, end - 1);
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004840 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004841
4842 /* Wait all existing dio workers, newcomers will block on i_mutex */
4843 ext4_inode_block_unlocked_dio(inode);
4844 inode_dio_wait(inode);
4845
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004846 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
4847 flags, mode);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004848 if (ret)
4849 goto out_dio;
4850 }
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004851 if (!partial_begin && !partial_end)
4852 goto out_dio;
4853
Dmitry Monakhov69dc9532014-08-27 18:33:49 -04004854 /*
4855 * In worst case we have to writeout two nonadjacent unwritten
4856 * blocks and update the inode
4857 */
4858 credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1;
4859 if (ext4_should_journal_data(inode))
4860 credits += 2;
4861 handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
Lukas Czernerb8a86842014-03-18 18:05:35 -04004862 if (IS_ERR(handle)) {
4863 ret = PTR_ERR(handle);
4864 ext4_std_error(inode->i_sb, ret);
4865 goto out_dio;
4866 }
4867
4868 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
Lukas Czernere5b30412014-04-01 00:59:21 -04004869 if (new_size) {
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04004870 ext4_update_inode_size(inode, new_size);
Lukas Czernere5b30412014-04-01 00:59:21 -04004871 } else {
Lukas Czernerb8a86842014-03-18 18:05:35 -04004872 /*
4873 * Mark that we allocate beyond EOF so the subsequent truncate
4874 * can proceed even if the new size is the same as i_size.
4875 */
4876 if ((offset + len) > i_size_read(inode))
4877 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
4878 }
Lukas Czernerb8a86842014-03-18 18:05:35 -04004879 ext4_mark_inode_dirty(handle, inode);
4880
4881 /* Zero out partial block at the edges of the range */
4882 ret = ext4_zero_partial_blocks(handle, inode, offset, len);
4883
4884 if (file->f_flags & O_SYNC)
4885 ext4_handle_sync(handle);
4886
4887 ext4_journal_stop(handle);
4888out_dio:
4889 ext4_inode_resume_unlocked_dio(inode);
4890out_mutex:
4891 mutex_unlock(&inode->i_mutex);
4892 return ret;
4893}
4894
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004895/*
4896 * preallocate space for a file. This implements ext4's fallocate file
4897 * operation, which gets called from sys_fallocate system call.
4898 * For block-mapped files, posix_fallocate should fall back to the method
4899 * of writing zeroes to the required new blocks (the same behavior which is
4900 * expected for file systems which do not support fallocate() system call).
4901 */
4902long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
4903{
4904 struct inode *inode = file_inode(file);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004905 loff_t new_size = 0;
4906 unsigned int max_blocks;
4907 int ret = 0;
4908 int flags;
4909 ext4_lblk_t lblk;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004910 unsigned int blkbits = inode->i_blkbits;
4911
4912 /* Return error if mode is not supported */
4913 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
Lukas Czernerb8a86842014-03-18 18:05:35 -04004914 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE))
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004915 return -EOPNOTSUPP;
4916
4917 if (mode & FALLOC_FL_PUNCH_HOLE)
4918 return ext4_punch_hole(inode, offset, len);
4919
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004920 ret = ext4_convert_inline_data(inode);
4921 if (ret)
4922 return ret;
4923
Theodore Ts'o40c406c2014-04-12 22:53:53 -04004924 if (mode & FALLOC_FL_COLLAPSE_RANGE)
4925 return ext4_collapse_range(inode, offset, len);
4926
Lukas Czernerb8a86842014-03-18 18:05:35 -04004927 if (mode & FALLOC_FL_ZERO_RANGE)
4928 return ext4_zero_range(file, offset, len, mode);
4929
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004930 trace_ext4_fallocate_enter(inode, offset, len, mode);
4931 lblk = offset >> blkbits;
4932 /*
4933 * We can't just convert len to max_blocks because
4934 * If blocksize = 4096 offset = 3072 and len = 2048
4935 */
4936 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
4937 - lblk;
4938
Lukas Czerner556615d2014-04-20 23:45:47 -04004939 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004940 if (mode & FALLOC_FL_KEEP_SIZE)
4941 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4942
4943 mutex_lock(&inode->i_mutex);
4944
Davide Italianoe5975e42015-05-02 23:21:15 -04004945 /*
4946 * We only support preallocation for extent-based files only
4947 */
4948 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4949 ret = -EOPNOTSUPP;
4950 goto out;
4951 }
4952
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004953 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
4954 offset + len > i_size_read(inode)) {
4955 new_size = offset + len;
4956 ret = inode_newsize_ok(inode, new_size);
4957 if (ret)
4958 goto out;
4959 }
4960
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004961 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
4962 flags, mode);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004963 if (ret)
4964 goto out;
4965
Dmitry Monakhovc174e6d2014-08-27 18:40:00 -04004966 if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) {
4967 ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
4968 EXT4_I(inode)->i_sync_tid);
Lukas Czernerf282ac12014-03-18 17:44:35 -04004969 }
Lukas Czernerf282ac12014-03-18 17:44:35 -04004970out:
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05004971 mutex_unlock(&inode->i_mutex);
Lukas Czerner0e8b6872014-03-18 18:03:51 -04004972 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
4973 return ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004974}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004975
4976/*
Mingming Cao00314622009-09-28 15:49:08 -04004977 * This function convert a range of blocks to written extents
4978 * The caller of this function will pass the start offset and the size.
4979 * all unwritten extents within this range will be converted to
4980 * written extents.
4981 *
4982 * This function is called from the direct IO end io call back
4983 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05004984 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04004985 */
Jan Kara6b523df2013-06-04 13:21:11 -04004986int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
4987 loff_t offset, ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04004988{
Mingming Cao00314622009-09-28 15:49:08 -04004989 unsigned int max_blocks;
4990 int ret = 0;
4991 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004992 struct ext4_map_blocks map;
Mingming Cao00314622009-09-28 15:49:08 -04004993 unsigned int credits, blkbits = inode->i_blkbits;
4994
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004995 map.m_lblk = offset >> blkbits;
Mingming Cao00314622009-09-28 15:49:08 -04004996 /*
4997 * We can't just convert len to max_blocks because
4998 * If blocksize = 4096 offset = 3072 and len = 2048
4999 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04005000 max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
5001 map.m_lblk);
Mingming Cao00314622009-09-28 15:49:08 -04005002 /*
Jan Kara6b523df2013-06-04 13:21:11 -04005003 * This is somewhat ugly but the idea is clear: When transaction is
5004 * reserved, everything goes into it. Otherwise we rather start several
5005 * smaller transactions for conversion of each extent separately.
Mingming Cao00314622009-09-28 15:49:08 -04005006 */
Jan Kara6b523df2013-06-04 13:21:11 -04005007 if (handle) {
5008 handle = ext4_journal_start_reserved(handle,
5009 EXT4_HT_EXT_CONVERT);
5010 if (IS_ERR(handle))
5011 return PTR_ERR(handle);
5012 credits = 0;
5013 } else {
5014 /*
5015 * credits to insert 1 extent into extent tree
5016 */
5017 credits = ext4_chunk_trans_blocks(inode, max_blocks);
5018 }
Mingming Cao00314622009-09-28 15:49:08 -04005019 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04005020 map.m_lblk += ret;
5021 map.m_len = (max_blocks -= ret);
Jan Kara6b523df2013-06-04 13:21:11 -04005022 if (credits) {
5023 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
5024 credits);
5025 if (IS_ERR(handle)) {
5026 ret = PTR_ERR(handle);
5027 break;
5028 }
Mingming Cao00314622009-09-28 15:49:08 -04005029 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04005030 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05005031 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Lukas Czernerb06acd32013-01-28 21:21:12 -05005032 if (ret <= 0)
5033 ext4_warning(inode->i_sb,
5034 "inode #%lu: block %u: len %u: "
5035 "ext4_ext_map_blocks returned %d",
5036 inode->i_ino, map.m_lblk,
5037 map.m_len, ret);
Mingming Cao00314622009-09-28 15:49:08 -04005038 ext4_mark_inode_dirty(handle, inode);
Jan Kara6b523df2013-06-04 13:21:11 -04005039 if (credits)
5040 ret2 = ext4_journal_stop(handle);
5041 if (ret <= 0 || ret2)
Mingming Cao00314622009-09-28 15:49:08 -04005042 break;
5043 }
Jan Kara6b523df2013-06-04 13:21:11 -04005044 if (!credits)
5045 ret2 = ext4_journal_stop(handle);
Mingming Cao00314622009-09-28 15:49:08 -04005046 return ret > 0 ? ret2 : ret;
5047}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005048
Mingming Cao00314622009-09-28 15:49:08 -04005049/*
Zheng Liu69eb33d2013-02-18 00:31:07 -05005050 * If newes is not existing extent (newes->ec_pblk equals zero) find
5051 * delayed extent at start of newes and update newes accordingly and
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005052 * return start of the next delayed extent.
5053 *
Zheng Liu69eb33d2013-02-18 00:31:07 -05005054 * If newes is existing extent (newes->ec_pblk is not equal zero)
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005055 * return start of next delayed extent or EXT_MAX_BLOCKS if no delayed
Zheng Liu69eb33d2013-02-18 00:31:07 -05005056 * extent found. Leave newes unmodified.
Eric Sandeen6873fa02008-10-07 00:46:36 -04005057 */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005058static int ext4_find_delayed_extent(struct inode *inode,
Zheng Liu69eb33d2013-02-18 00:31:07 -05005059 struct extent_status *newes)
Eric Sandeen6873fa02008-10-07 00:46:36 -04005060{
Zheng Liub3aff3e2012-11-08 21:57:37 -05005061 struct extent_status es;
Zheng Liube401362013-02-18 00:27:26 -05005062 ext4_lblk_t block, next_del;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005063
Zheng Liu69eb33d2013-02-18 00:31:07 -05005064 if (newes->es_pblk == 0) {
Yan, Zhenge30b5dc2013-05-03 02:15:52 -04005065 ext4_es_find_delayed_extent_range(inode, newes->es_lblk,
5066 newes->es_lblk + newes->es_len - 1, &es);
5067
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005068 /*
Zheng Liu69eb33d2013-02-18 00:31:07 -05005069 * No extent in extent-tree contains block @newes->es_pblk,
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005070 * then the block may stay in 1)a hole or 2)delayed-extent.
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005071 */
Zheng Liu06b0c882013-02-18 00:26:51 -05005072 if (es.es_len == 0)
Zheng Liub3aff3e2012-11-08 21:57:37 -05005073 /* A hole found. */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005074 return 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005075
Zheng Liu69eb33d2013-02-18 00:31:07 -05005076 if (es.es_lblk > newes->es_lblk) {
Zheng Liub3aff3e2012-11-08 21:57:37 -05005077 /* A hole found. */
Zheng Liu69eb33d2013-02-18 00:31:07 -05005078 newes->es_len = min(es.es_lblk - newes->es_lblk,
5079 newes->es_len);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005080 return 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005081 }
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05005082
Zheng Liu69eb33d2013-02-18 00:31:07 -05005083 newes->es_len = es.es_lblk + es.es_len - newes->es_lblk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005084 }
5085
Zheng Liu69eb33d2013-02-18 00:31:07 -05005086 block = newes->es_lblk + newes->es_len;
Yan, Zhenge30b5dc2013-05-03 02:15:52 -04005087 ext4_es_find_delayed_extent_range(inode, block, EXT_MAX_BLOCKS, &es);
Zheng Liube401362013-02-18 00:27:26 -05005088 if (es.es_len == 0)
5089 next_del = EXT_MAX_BLOCKS;
5090 else
5091 next_del = es.es_lblk;
5092
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005093 return next_del;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005094}
Eric Sandeen6873fa02008-10-07 00:46:36 -04005095/* fiemap flags we can handle specified here */
5096#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
5097
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05005098static int ext4_xattr_fiemap(struct inode *inode,
5099 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04005100{
5101 __u64 physical = 0;
5102 __u64 length;
5103 __u32 flags = FIEMAP_EXTENT_LAST;
5104 int blockbits = inode->i_sb->s_blocksize_bits;
5105 int error = 0;
5106
5107 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005108 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04005109 struct ext4_iloc iloc;
5110 int offset; /* offset of xattr in inode */
5111
5112 error = ext4_get_inode_loc(inode, &iloc);
5113 if (error)
5114 return error;
Jan Karaa60697f2013-05-31 19:38:56 -04005115 physical = (__u64)iloc.bh->b_blocknr << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005116 offset = EXT4_GOOD_OLD_INODE_SIZE +
5117 EXT4_I(inode)->i_extra_isize;
5118 physical += offset;
5119 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
5120 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04005121 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005122 } else { /* external block */
Jan Karaa60697f2013-05-31 19:38:56 -04005123 physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005124 length = inode->i_sb->s_blocksize;
5125 }
5126
5127 if (physical)
5128 error = fiemap_fill_next_extent(fieinfo, 0, physical,
5129 length, flags);
5130 return (error < 0 ? error : 0);
5131}
5132
5133int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
5134 __u64 start, __u64 len)
5135{
5136 ext4_lblk_t start_blk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005137 int error = 0;
5138
Tao Ma94191982012-12-10 14:06:02 -05005139 if (ext4_has_inline_data(inode)) {
5140 int has_inline = 1;
5141
5142 error = ext4_inline_data_fiemap(inode, fieinfo, &has_inline);
5143
5144 if (has_inline)
5145 return error;
5146 }
5147
Theodore Ts'o7869a4a2013-08-16 22:05:14 -04005148 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
5149 error = ext4_ext_precache(inode);
5150 if (error)
5151 return error;
5152 }
5153
Eric Sandeen6873fa02008-10-07 00:46:36 -04005154 /* fallback to generic here if not in extents fmt */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005155 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeen6873fa02008-10-07 00:46:36 -04005156 return generic_block_fiemap(inode, fieinfo, start, len,
5157 ext4_get_block);
5158
5159 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
5160 return -EBADR;
5161
5162 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
5163 error = ext4_xattr_fiemap(inode, fieinfo);
5164 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005165 ext4_lblk_t len_blks;
5166 __u64 last_blk;
5167
Eric Sandeen6873fa02008-10-07 00:46:36 -04005168 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005169 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04005170 if (last_blk >= EXT_MAX_BLOCKS)
5171 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05005172 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04005173
5174 /*
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005175 * Walk the extent tree gathering extent information
5176 * and pushing extents back to the user.
Eric Sandeen6873fa02008-10-07 00:46:36 -04005177 */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05005178 error = ext4_fill_fiemap_extents(inode, start_blk,
5179 len_blks, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005180 }
Theodore Ts'o107a7bd2013-08-16 21:23:41 -04005181 ext4_es_lru_add(inode);
Eric Sandeen6873fa02008-10-07 00:46:36 -04005182 return error;
5183}
Namjae Jeon9eb79482014-02-23 15:18:59 -05005184
5185/*
5186 * ext4_access_path:
5187 * Function to access the path buffer for marking it dirty.
5188 * It also checks if there are sufficient credits left in the journal handle
5189 * to update path.
5190 */
5191static int
5192ext4_access_path(handle_t *handle, struct inode *inode,
5193 struct ext4_ext_path *path)
5194{
5195 int credits, err;
5196
5197 if (!ext4_handle_valid(handle))
5198 return 0;
5199
5200 /*
5201 * Check if need to extend journal credits
5202 * 3 for leaf, sb, and inode plus 2 (bmap and group
5203 * descriptor) for each block group; assume two block
5204 * groups
5205 */
5206 if (handle->h_buffer_credits < 7) {
5207 credits = ext4_writepage_trans_blocks(inode);
5208 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
5209 /* EAGAIN is success */
5210 if (err && err != -EAGAIN)
5211 return err;
5212 }
5213
5214 err = ext4_ext_get_access(handle, inode, path);
5215 return err;
5216}
5217
5218/*
5219 * ext4_ext_shift_path_extents:
5220 * Shift the extents of a path structure lying between path[depth].p_ext
5221 * and EXT_LAST_EXTENT(path[depth].p_hdr) downwards, by subtracting shift
5222 * from starting block for each extent.
5223 */
5224static int
5225ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift,
5226 struct inode *inode, handle_t *handle,
5227 ext4_lblk_t *start)
5228{
5229 int depth, err = 0;
5230 struct ext4_extent *ex_start, *ex_last;
5231 bool update = 0;
5232 depth = path->p_depth;
5233
5234 while (depth >= 0) {
5235 if (depth == path->p_depth) {
5236 ex_start = path[depth].p_ext;
5237 if (!ex_start)
5238 return -EIO;
5239
5240 ex_last = EXT_LAST_EXTENT(path[depth].p_hdr);
5241 if (!ex_last)
5242 return -EIO;
5243
5244 err = ext4_access_path(handle, inode, path + depth);
5245 if (err)
5246 goto out;
5247
5248 if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr))
5249 update = 1;
5250
Zheng Liu847c6c42014-04-12 12:45:55 -04005251 *start = le32_to_cpu(ex_last->ee_block) +
Namjae Jeon9eb79482014-02-23 15:18:59 -05005252 ext4_ext_get_actual_len(ex_last);
5253
5254 while (ex_start <= ex_last) {
Zheng Liu847c6c42014-04-12 12:45:55 -04005255 le32_add_cpu(&ex_start->ee_block, -shift);
Lukas Czerner6dd834e2014-04-18 10:55:24 -04005256 /* Try to merge to the left. */
5257 if ((ex_start >
5258 EXT_FIRST_EXTENT(path[depth].p_hdr)) &&
5259 ext4_ext_try_to_merge_right(inode,
5260 path, ex_start - 1))
5261 ex_last--;
5262 else
5263 ex_start++;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005264 }
5265 err = ext4_ext_dirty(handle, inode, path + depth);
5266 if (err)
5267 goto out;
5268
5269 if (--depth < 0 || !update)
5270 break;
5271 }
5272
5273 /* Update index too */
5274 err = ext4_access_path(handle, inode, path + depth);
5275 if (err)
5276 goto out;
5277
Zheng Liu847c6c42014-04-12 12:45:55 -04005278 le32_add_cpu(&path[depth].p_idx->ei_block, -shift);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005279 err = ext4_ext_dirty(handle, inode, path + depth);
5280 if (err)
5281 goto out;
5282
5283 /* we are done if current index is not a starting index */
5284 if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr))
5285 break;
5286
5287 depth--;
5288 }
5289
5290out:
5291 return err;
5292}
5293
5294/*
5295 * ext4_ext_shift_extents:
5296 * All the extents which lies in the range from start to the last allocated
5297 * block for the file are shifted downwards by shift blocks.
5298 * On success, 0 is returned, error otherwise.
5299 */
5300static int
5301ext4_ext_shift_extents(struct inode *inode, handle_t *handle,
5302 ext4_lblk_t start, ext4_lblk_t shift)
5303{
5304 struct ext4_ext_path *path;
5305 int ret = 0, depth;
5306 struct ext4_extent *extent;
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005307 ext4_lblk_t stop_block;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005308 ext4_lblk_t ex_start, ex_end;
5309
5310 /* Let path point to the last extent */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005311 path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL, 0);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005312 if (IS_ERR(path))
5313 return PTR_ERR(path);
5314
5315 depth = path->p_depth;
5316 extent = path[depth].p_ext;
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005317 if (!extent)
5318 goto out;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005319
Zheng Liu847c6c42014-04-12 12:45:55 -04005320 stop_block = le32_to_cpu(extent->ee_block) +
5321 ext4_ext_get_actual_len(extent);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005322
5323 /* Nothing to shift, if hole is at the end of file */
5324 if (start >= stop_block)
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005325 goto out;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005326
5327 /*
5328 * Don't start shifting extents until we make sure the hole is big
5329 * enough to accomodate the shift.
5330 */
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005331 path = ext4_find_extent(inode, start - 1, &path, 0);
Dmitry Monakhov8dc79ec2014-04-13 15:05:42 -04005332 if (IS_ERR(path))
5333 return PTR_ERR(path);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005334 depth = path->p_depth;
5335 extent = path[depth].p_ext;
Dmitry Monakhov8dc79ec2014-04-13 15:05:42 -04005336 if (extent) {
5337 ex_start = le32_to_cpu(extent->ee_block);
5338 ex_end = le32_to_cpu(extent->ee_block) +
Zheng Liu847c6c42014-04-12 12:45:55 -04005339 ext4_ext_get_actual_len(extent);
Dmitry Monakhov8dc79ec2014-04-13 15:05:42 -04005340 } else {
5341 ex_start = 0;
5342 ex_end = 0;
5343 }
Namjae Jeon9eb79482014-02-23 15:18:59 -05005344
5345 if ((start == ex_start && shift > ex_start) ||
5346 (shift > start - ex_end))
5347 return -EINVAL;
5348
5349 /* Its safe to start updating extents */
5350 while (start < stop_block) {
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005351 path = ext4_find_extent(inode, start, &path, 0);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005352 if (IS_ERR(path))
5353 return PTR_ERR(path);
5354 depth = path->p_depth;
5355 extent = path[depth].p_ext;
Dmitry Monakhova18ed352014-04-13 15:41:13 -04005356 if (!extent) {
5357 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
5358 (unsigned long) start);
5359 return -EIO;
5360 }
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005361 if (start > le32_to_cpu(extent->ee_block)) {
Namjae Jeon9eb79482014-02-23 15:18:59 -05005362 /* Hole, move to the next extent */
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005363 if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) {
5364 path[depth].p_ext++;
5365 } else {
5366 start = ext4_ext_next_allocated_block(path);
Dmitry Monakhovf8fb4f42014-08-30 23:50:56 -04005367 continue;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005368 }
5369 }
5370 ret = ext4_ext_shift_path_extents(path, shift, inode,
5371 handle, &start);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005372 if (ret)
5373 break;
5374 }
Theodore Ts'oee4bd0d92014-09-01 14:41:09 -04005375out:
5376 ext4_ext_drop_refs(path);
5377 kfree(path);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005378 return ret;
5379}
5380
5381/*
5382 * ext4_collapse_range:
5383 * This implements the fallocate's collapse range functionality for ext4
5384 * Returns: 0 and non-zero on error.
5385 */
5386int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)
5387{
5388 struct super_block *sb = inode->i_sb;
5389 ext4_lblk_t punch_start, punch_stop;
5390 handle_t *handle;
5391 unsigned int credits;
Namjae Jeona8680e02014-04-19 16:37:31 -04005392 loff_t new_size, ioffset;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005393 int ret;
5394
Namjae Jeon9eb79482014-02-23 15:18:59 -05005395 /* Collapse range works only on fs block size aligned offsets. */
Namjae Jeonee98fa32014-07-29 10:45:31 -04005396 if (offset & (EXT4_CLUSTER_SIZE(sb) - 1) ||
5397 len & (EXT4_CLUSTER_SIZE(sb) - 1))
Namjae Jeon9eb79482014-02-23 15:18:59 -05005398 return -EINVAL;
5399
5400 if (!S_ISREG(inode->i_mode))
Theodore Ts'o86f1ca32014-04-18 11:52:11 -04005401 return -EINVAL;
Namjae Jeon9eb79482014-02-23 15:18:59 -05005402
5403 trace_ext4_collapse_range(inode, offset, len);
5404
5405 punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5406 punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb);
5407
Namjae Jeon1ce01c42014-04-10 22:58:20 -04005408 /* Call ext4_force_commit to flush all data in case of data=journal. */
5409 if (ext4_should_journal_data(inode)) {
5410 ret = ext4_force_commit(inode->i_sb);
5411 if (ret)
5412 return ret;
5413 }
5414
Namjae Jeona8680e02014-04-19 16:37:31 -04005415 /*
5416 * Need to round down offset to be aligned with page size boundary
5417 * for page size > block size.
5418 */
5419 ioffset = round_down(offset, PAGE_SIZE);
5420
Namjae Jeon9eb79482014-02-23 15:18:59 -05005421 /* Write out all dirty pages */
Namjae Jeona8680e02014-04-19 16:37:31 -04005422 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset,
5423 LLONG_MAX);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005424 if (ret)
5425 return ret;
5426
5427 /* Take mutex lock */
5428 mutex_lock(&inode->i_mutex);
5429
Lukas Czerner23fffa92014-04-12 09:56:41 -04005430 /*
5431 * There is no need to overlap collapse range with EOF, in which case
5432 * it is effectively a truncate operation
5433 */
5434 if (offset + len >= i_size_read(inode)) {
5435 ret = -EINVAL;
5436 goto out_mutex;
5437 }
5438
Namjae Jeon9eb79482014-02-23 15:18:59 -05005439 /* Currently just for extent based files */
5440 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5441 ret = -EOPNOTSUPP;
5442 goto out_mutex;
5443 }
5444
Namjae Jeona8680e02014-04-19 16:37:31 -04005445 truncate_pagecache(inode, ioffset);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005446
5447 /* Wait for existing dio to complete */
5448 ext4_inode_block_unlocked_dio(inode);
5449 inode_dio_wait(inode);
5450
5451 credits = ext4_writepage_trans_blocks(inode);
5452 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5453 if (IS_ERR(handle)) {
5454 ret = PTR_ERR(handle);
5455 goto out_dio;
5456 }
5457
5458 down_write(&EXT4_I(inode)->i_data_sem);
5459 ext4_discard_preallocations(inode);
5460
5461 ret = ext4_es_remove_extent(inode, punch_start,
Lukas Czerner2c1d2322014-04-18 10:43:21 -04005462 EXT_MAX_BLOCKS - punch_start);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005463 if (ret) {
5464 up_write(&EXT4_I(inode)->i_data_sem);
5465 goto out_stop;
5466 }
5467
5468 ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
5469 if (ret) {
5470 up_write(&EXT4_I(inode)->i_data_sem);
5471 goto out_stop;
5472 }
Lukas Czerneref24f6c2014-04-18 10:50:23 -04005473 ext4_discard_preallocations(inode);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005474
5475 ret = ext4_ext_shift_extents(inode, handle, punch_stop,
5476 punch_stop - punch_start);
5477 if (ret) {
5478 up_write(&EXT4_I(inode)->i_data_sem);
5479 goto out_stop;
5480 }
5481
5482 new_size = i_size_read(inode) - len;
Lukas Czerner9337d5d2014-04-18 10:48:25 -04005483 i_size_write(inode, new_size);
Namjae Jeon9eb79482014-02-23 15:18:59 -05005484 EXT4_I(inode)->i_disksize = new_size;
5485
Namjae Jeon9eb79482014-02-23 15:18:59 -05005486 up_write(&EXT4_I(inode)->i_data_sem);
5487 if (IS_SYNC(inode))
5488 ext4_handle_sync(handle);
5489 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
5490 ext4_mark_inode_dirty(handle, inode);
5491
5492out_stop:
5493 ext4_journal_stop(handle);
5494out_dio:
5495 ext4_inode_resume_unlocked_dio(inode);
5496out_mutex:
5497 mutex_unlock(&inode->i_mutex);
5498 return ret;
5499}
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005500
5501/**
5502 * ext4_swap_extents - Swap extents between two inodes
5503 *
5504 * @inode1: First inode
5505 * @inode2: Second inode
5506 * @lblk1: Start block for first inode
5507 * @lblk2: Start block for second inode
5508 * @count: Number of blocks to swap
5509 * @mark_unwritten: Mark second inode's extents as unwritten after swap
5510 * @erp: Pointer to save error value
5511 *
5512 * This helper routine does exactly what is promise "swap extents". All other
5513 * stuff such as page-cache locking consistency, bh mapping consistency or
5514 * extent's data copying must be performed by caller.
5515 * Locking:
5516 * i_mutex is held for both inodes
5517 * i_data_sem is locked for write for both inodes
5518 * Assumptions:
5519 * All pages from requested range are locked for both inodes
5520 */
5521int
5522ext4_swap_extents(handle_t *handle, struct inode *inode1,
5523 struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2,
5524 ext4_lblk_t count, int unwritten, int *erp)
5525{
5526 struct ext4_ext_path *path1 = NULL;
5527 struct ext4_ext_path *path2 = NULL;
5528 int replaced_count = 0;
5529
5530 BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem));
5531 BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem));
5532 BUG_ON(!mutex_is_locked(&inode1->i_mutex));
5533 BUG_ON(!mutex_is_locked(&inode1->i_mutex));
5534
5535 *erp = ext4_es_remove_extent(inode1, lblk1, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005536 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005537 return 0;
5538 *erp = ext4_es_remove_extent(inode2, lblk2, count);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005539 if (unlikely(*erp))
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005540 return 0;
5541
5542 while (count) {
5543 struct ext4_extent *ex1, *ex2, tmp_ex;
5544 ext4_lblk_t e1_blk, e2_blk;
5545 int e1_len, e2_len, len;
5546 int split = 0;
5547
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005548 path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005549 if (unlikely(IS_ERR(path1))) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005550 *erp = PTR_ERR(path1);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005551 path1 = NULL;
5552 finish:
5553 count = 0;
5554 goto repeat;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005555 }
Theodore Ts'oed8a1a72014-09-01 14:43:09 -04005556 path2 = ext4_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005557 if (unlikely(IS_ERR(path2))) {
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005558 *erp = PTR_ERR(path2);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005559 path2 = NULL;
5560 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005561 }
5562 ex1 = path1[path1->p_depth].p_ext;
5563 ex2 = path2[path2->p_depth].p_ext;
5564 /* Do we have somthing to swap ? */
5565 if (unlikely(!ex2 || !ex1))
Theodore Ts'o19008f62014-08-31 15:03:14 -04005566 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005567
5568 e1_blk = le32_to_cpu(ex1->ee_block);
5569 e2_blk = le32_to_cpu(ex2->ee_block);
5570 e1_len = ext4_ext_get_actual_len(ex1);
5571 e2_len = ext4_ext_get_actual_len(ex2);
5572
5573 /* Hole handling */
5574 if (!in_range(lblk1, e1_blk, e1_len) ||
5575 !in_range(lblk2, e2_blk, e2_len)) {
5576 ext4_lblk_t next1, next2;
5577
5578 /* if hole after extent, then go to next extent */
5579 next1 = ext4_ext_next_allocated_block(path1);
5580 next2 = ext4_ext_next_allocated_block(path2);
5581 /* If hole before extent, then shift to that extent */
5582 if (e1_blk > lblk1)
5583 next1 = e1_blk;
5584 if (e2_blk > lblk2)
5585 next2 = e1_blk;
5586 /* Do we have something to swap */
5587 if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005588 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005589 /* Move to the rightest boundary */
5590 len = next1 - lblk1;
5591 if (len < next2 - lblk2)
5592 len = next2 - lblk2;
5593 if (len > count)
5594 len = count;
5595 lblk1 += len;
5596 lblk2 += len;
5597 count -= len;
5598 goto repeat;
5599 }
5600
5601 /* Prepare left boundary */
5602 if (e1_blk < lblk1) {
5603 split = 1;
5604 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005605 &path1, lblk1, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005606 if (unlikely(*erp))
5607 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005608 }
5609 if (e2_blk < lblk2) {
5610 split = 1;
5611 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005612 &path2, lblk2, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005613 if (unlikely(*erp))
5614 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005615 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005616 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005617 * path must to be revalidated. */
5618 if (split)
5619 goto repeat;
5620
5621 /* Prepare right boundary */
5622 len = count;
5623 if (len > e1_blk + e1_len - lblk1)
5624 len = e1_blk + e1_len - lblk1;
5625 if (len > e2_blk + e2_len - lblk2)
5626 len = e2_blk + e2_len - lblk2;
5627
5628 if (len != e1_len) {
5629 split = 1;
5630 *erp = ext4_force_split_extent_at(handle, inode1,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005631 &path1, lblk1 + len, 0);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005632 if (unlikely(*erp))
5633 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005634 }
5635 if (len != e2_len) {
5636 split = 1;
5637 *erp = ext4_force_split_extent_at(handle, inode2,
Theodore Ts'odfe50802014-09-01 14:37:09 -04005638 &path2, lblk2 + len, 0);
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005639 if (*erp)
Theodore Ts'o19008f62014-08-31 15:03:14 -04005640 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005641 }
Theodore Ts'odfe50802014-09-01 14:37:09 -04005642 /* ext4_split_extent_at() may result in leaf extent split,
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005643 * path must to be revalidated. */
5644 if (split)
5645 goto repeat;
5646
5647 BUG_ON(e2_len != e1_len);
5648 *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005649 if (unlikely(*erp))
5650 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005651 *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005652 if (unlikely(*erp))
5653 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005654
5655 /* Both extents are fully inside boundaries. Swap it now */
5656 tmp_ex = *ex1;
5657 ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2));
5658 ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex));
5659 ex1->ee_len = cpu_to_le16(e2_len);
5660 ex2->ee_len = cpu_to_le16(e1_len);
5661 if (unwritten)
5662 ext4_ext_mark_unwritten(ex2);
5663 if (ext4_ext_is_unwritten(&tmp_ex))
5664 ext4_ext_mark_unwritten(ex1);
5665
5666 ext4_ext_try_to_merge(handle, inode2, path2, ex2);
5667 ext4_ext_try_to_merge(handle, inode1, path1, ex1);
5668 *erp = ext4_ext_dirty(handle, inode2, path2 +
5669 path2->p_depth);
Theodore Ts'o19008f62014-08-31 15:03:14 -04005670 if (unlikely(*erp))
5671 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005672 *erp = ext4_ext_dirty(handle, inode1, path1 +
5673 path1->p_depth);
5674 /*
5675 * Looks scarry ah..? second inode already points to new blocks,
5676 * and it was successfully dirtied. But luckily error may happen
5677 * only due to journal error, so full transaction will be
5678 * aborted anyway.
5679 */
Theodore Ts'o19008f62014-08-31 15:03:14 -04005680 if (unlikely(*erp))
5681 goto finish;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005682 lblk1 += len;
5683 lblk2 += len;
5684 replaced_count += len;
5685 count -= len;
5686
5687 repeat:
Theodore Ts'ob7ea89a2014-09-01 14:39:09 -04005688 ext4_ext_drop_refs(path1);
5689 kfree(path1);
5690 ext4_ext_drop_refs(path2);
5691 kfree(path2);
5692 path1 = path2 = NULL;
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005693 }
Dmitry Monakhovfcf6b1b72014-08-30 23:52:19 -04005694 return replaced_count;
5695}