blob: fbe7dc2842408d9554d565c70ac3bfd1d8dda3c2 [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>
Amit Aroraa2df2a62007-07-17 21:42:41 -040040#include <linux/falloc.h>
Alex Tomasa86c6182006-10-11 01:21:03 -070041#include <asm/uaccess.h>
Eric Sandeen6873fa02008-10-07 00:46:36 -040042#include <linux/fiemap.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040043#include "ext4_jbd2.h"
Alex Tomasa86c6182006-10-11 01:21:03 -070044
Jiaying Zhang0562e0b2011-03-21 21:38:05 -040045#include <trace/events/ext4.h>
46
Lukas Czerner5f95d212012-03-19 23:03:19 -040047/*
48 * used by extent splitting.
49 */
50#define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
51 due to ENOSPC */
52#define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */
53#define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */
54
Dmitry Monakhovdee1f972012-10-10 01:04:58 -040055#define EXT4_EXT_DATA_VALID1 0x8 /* first half contains valid data */
56#define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */
57
Darrick J. Wong7ac59902012-04-29 18:37:10 -040058static __le32 ext4_extent_block_csum(struct inode *inode,
59 struct ext4_extent_header *eh)
60{
61 struct ext4_inode_info *ei = EXT4_I(inode);
62 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
63 __u32 csum;
64
65 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)eh,
66 EXT4_EXTENT_TAIL_OFFSET(eh));
67 return cpu_to_le32(csum);
68}
69
70static int ext4_extent_block_csum_verify(struct inode *inode,
71 struct ext4_extent_header *eh)
72{
73 struct ext4_extent_tail *et;
74
75 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
76 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
77 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
90 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
91 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
92 return;
93
94 et = find_ext4_extent_tail(eh);
95 et->et_checksum = ext4_extent_block_csum(inode, eh);
96}
97
Allison Hendersond583fb82011-05-25 07:41:43 -040098static int ext4_split_extent(handle_t *handle,
99 struct inode *inode,
100 struct ext4_ext_path *path,
101 struct ext4_map_blocks *map,
102 int split_flag,
103 int flags);
104
Lukas Czerner5f95d212012-03-19 23:03:19 -0400105static int ext4_split_extent_at(handle_t *handle,
106 struct inode *inode,
107 struct ext4_ext_path *path,
108 ext4_lblk_t split,
109 int split_flag,
110 int flags);
111
Lukas Czerner91dd8c12012-11-28 12:32:26 -0500112static int ext4_find_delayed_extent(struct inode *inode,
113 struct ext4_ext_cache *newex);
114
Jan Kara487caee2009-08-17 22:17:20 -0400115static int ext4_ext_truncate_extend_restart(handle_t *handle,
116 struct inode *inode,
117 int needed)
Alex Tomasa86c6182006-10-11 01:21:03 -0700118{
119 int err;
120
Frank Mayhar03901312009-01-07 00:06:22 -0500121 if (!ext4_handle_valid(handle))
122 return 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700123 if (handle->h_buffer_credits > needed)
Shen Feng9102e4f2008-07-11 19:27:31 -0400124 return 0;
125 err = ext4_journal_extend(handle, needed);
Theodore Ts'o0123c932008-08-01 20:57:54 -0400126 if (err <= 0)
Shen Feng9102e4f2008-07-11 19:27:31 -0400127 return err;
Jan Kara487caee2009-08-17 22:17:20 -0400128 err = ext4_truncate_restart_trans(handle, inode, needed);
Dmitry Monakhov0617b832010-05-17 01:00:00 -0400129 if (err == 0)
130 err = -EAGAIN;
Jan Kara487caee2009-08-17 22:17:20 -0400131
132 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -0700133}
134
135/*
136 * could return:
137 * - EROFS
138 * - ENOMEM
139 */
140static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
141 struct ext4_ext_path *path)
142{
143 if (path->p_bh) {
144 /* path points to block */
145 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 */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -0400158#define ext4_ext_dirty(handle, inode, path) \
159 __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path))
160static int __ext4_ext_dirty(const char *where, unsigned int line,
161 handle_t *handle, struct inode *inode,
162 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700163{
164 int err;
165 if (path->p_bh) {
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400166 ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh));
Alex Tomasa86c6182006-10-11 01:21:03 -0700167 /* path points to block */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -0400168 err = __ext4_handle_dirty_metadata(where, line, handle,
169 inode, path->p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700170 } else {
171 /* path points to leaf/index in inode body */
172 err = ext4_mark_inode_dirty(handle, inode);
173 }
174 return err;
175}
176
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700177static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700178 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500179 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700180{
Alex Tomasa86c6182006-10-11 01:21:03 -0700181 if (path) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400182 int depth = path->p_depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700183 struct ext4_extent *ex;
Alex Tomasa86c6182006-10-11 01:21:03 -0700184
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500185 /*
186 * Try to predict block placement assuming that we are
187 * filling in a file which will eventually be
188 * non-sparse --- i.e., in the case of libbfd writing
189 * an ELF object sections out-of-order but in a way
190 * the eventually results in a contiguous object or
191 * executable file, or some database extending a table
192 * space file. However, this is actually somewhat
193 * non-ideal if we are writing a sparse file such as
194 * qemu or KVM writing a raw image file that is going
195 * to stay fairly sparse, since it will end up
196 * fragmenting the file system's free space. Maybe we
197 * should have some hueristics or some way to allow
198 * userspace to pass a hint to file system,
Tao Mab8d65682011-01-21 23:21:31 +0800199 * especially if the latter case turns out to be
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500200 * common.
201 */
Avantika Mathur7e028972006-12-06 20:41:33 -0800202 ex = path[depth].p_ext;
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500203 if (ex) {
204 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
205 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
206
207 if (block > ext_block)
208 return ext_pblk + (block - ext_block);
209 else
210 return ext_pblk - (ext_block - block);
211 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700212
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700213 /* it looks like index is empty;
214 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700215 if (path[depth].p_bh)
216 return path[depth].p_bh->b_blocknr;
217 }
218
219 /* OK. use inode's group */
Eric Sandeenf86186b2011-06-28 10:01:31 -0400220 return ext4_inode_to_goal_block(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700221}
222
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400223/*
224 * Allocation for a meta data block
225 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700226static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400227ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700228 struct ext4_ext_path *path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400229 struct ext4_extent *ex, int *err, unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700230{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700231 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700232
233 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Allison Henderson55f020d2011-05-25 07:41:26 -0400234 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
235 NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700236 return newblock;
237}
238
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400239static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700240{
241 int size;
242
243 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
244 / sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100245#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400246 if (!check && size > 6)
247 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700248#endif
249 return size;
250}
251
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400252static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700253{
254 int size;
255
256 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
257 / sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100258#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400259 if (!check && size > 5)
260 size = 5;
Alex Tomasa86c6182006-10-11 01:21:03 -0700261#endif
262 return size;
263}
264
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400265static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700266{
267 int size;
268
269 size = sizeof(EXT4_I(inode)->i_data);
270 size -= sizeof(struct ext4_extent_header);
271 size /= sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100272#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400273 if (!check && size > 3)
274 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700275#endif
276 return size;
277}
278
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400279static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700280{
281 int size;
282
283 size = sizeof(EXT4_I(inode)->i_data);
284 size -= sizeof(struct ext4_extent_header);
285 size /= sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100286#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400287 if (!check && size > 4)
288 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700289#endif
290 return size;
291}
292
Mingming Caod2a17632008-07-14 17:52:37 -0400293/*
294 * Calculate the number of metadata blocks needed
295 * to allocate @blocks
296 * Worse case is one block per extent
297 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -0500298int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -0400299{
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500300 struct ext4_inode_info *ei = EXT4_I(inode);
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400301 int idxs;
Mingming Caod2a17632008-07-14 17:52:37 -0400302
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500303 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
304 / sizeof(struct ext4_extent_idx));
Mingming Caod2a17632008-07-14 17:52:37 -0400305
306 /*
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500307 * If the new delayed allocation block is contiguous with the
308 * previous da block, it can share index blocks with the
309 * previous block, so we only need to allocate a new index
310 * block every idxs leaf blocks. At ldxs**2 blocks, we need
311 * an additional index block, and at ldxs**3 blocks, yet
312 * another index blocks.
Mingming Caod2a17632008-07-14 17:52:37 -0400313 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500314 if (ei->i_da_metadata_calc_len &&
315 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400316 int num = 0;
317
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500318 if ((ei->i_da_metadata_calc_len % idxs) == 0)
319 num++;
320 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
321 num++;
322 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
323 num++;
324 ei->i_da_metadata_calc_len = 0;
325 } else
326 ei->i_da_metadata_calc_len++;
327 ei->i_da_metadata_calc_last_lblock++;
328 return num;
329 }
Mingming Caod2a17632008-07-14 17:52:37 -0400330
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500331 /*
332 * In the worst case we need a new set of index blocks at
333 * every level of the inode's extent tree.
334 */
335 ei->i_da_metadata_calc_len = 1;
336 ei->i_da_metadata_calc_last_lblock = lblock;
337 return ext_depth(inode) + 1;
Mingming Caod2a17632008-07-14 17:52:37 -0400338}
339
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400340static int
341ext4_ext_max_entries(struct inode *inode, int depth)
342{
343 int max;
344
345 if (depth == ext_depth(inode)) {
346 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400347 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400348 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400349 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400350 } else {
351 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400352 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400353 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400354 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400355 }
356
357 return max;
358}
359
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400360static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
361{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400362 ext4_fsblk_t block = ext4_ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400363 int len = ext4_ext_get_actual_len(ext);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400364
Theodore Ts'o31d4f3a2012-03-11 23:30:16 -0400365 if (len == 0)
366 return 0;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400367 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400368}
369
370static int ext4_valid_extent_idx(struct inode *inode,
371 struct ext4_extent_idx *ext_idx)
372{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400373 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400374
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400375 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400376}
377
378static int ext4_valid_extent_entries(struct inode *inode,
379 struct ext4_extent_header *eh,
380 int depth)
381{
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400382 unsigned short entries;
383 if (eh->eh_entries == 0)
384 return 1;
385
386 entries = le16_to_cpu(eh->eh_entries);
387
388 if (depth == 0) {
389 /* leaf entries */
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400390 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400391 while (entries) {
392 if (!ext4_valid_extent(inode, ext))
393 return 0;
394 ext++;
395 entries--;
396 }
397 } else {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400398 struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400399 while (entries) {
400 if (!ext4_valid_extent_idx(inode, ext_idx))
401 return 0;
402 ext_idx++;
403 entries--;
404 }
405 }
406 return 1;
407}
408
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400409static int __ext4_ext_check(const char *function, unsigned int line,
410 struct inode *inode, struct ext4_extent_header *eh,
411 int depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400412{
413 const char *error_msg;
414 int max = 0;
415
416 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
417 error_msg = "invalid magic";
418 goto corrupted;
419 }
420 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
421 error_msg = "unexpected eh_depth";
422 goto corrupted;
423 }
424 if (unlikely(eh->eh_max == 0)) {
425 error_msg = "invalid eh_max";
426 goto corrupted;
427 }
428 max = ext4_ext_max_entries(inode, depth);
429 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
430 error_msg = "too large eh_max";
431 goto corrupted;
432 }
433 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
434 error_msg = "invalid eh_entries";
435 goto corrupted;
436 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400437 if (!ext4_valid_extent_entries(inode, eh, depth)) {
438 error_msg = "invalid extent entries";
439 goto corrupted;
440 }
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400441 /* Verify checksum on non-root extent tree nodes */
442 if (ext_depth(inode) != depth &&
443 !ext4_extent_block_csum_verify(inode, eh)) {
444 error_msg = "extent tree corrupted";
445 goto corrupted;
446 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400447 return 0;
448
449corrupted:
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400450 ext4_error_inode(inode, function, line, 0,
Theodore Ts'o24676da2010-05-16 21:00:00 -0400451 "bad header/extent: %s - magic %x, "
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400452 "entries %u, max %u(%u), depth %u(%u)",
Theodore Ts'o24676da2010-05-16 21:00:00 -0400453 error_msg, le16_to_cpu(eh->eh_magic),
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400454 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
455 max, le16_to_cpu(eh->eh_depth), depth);
456
457 return -EIO;
458}
459
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400460#define ext4_ext_check(inode, eh, depth) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400461 __ext4_ext_check(__func__, __LINE__, inode, eh, depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400462
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400463int ext4_ext_check_inode(struct inode *inode)
464{
465 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
466}
467
Darrick J. Wongf8489122012-04-29 18:21:10 -0400468static int __ext4_ext_check_block(const char *function, unsigned int line,
469 struct inode *inode,
470 struct ext4_extent_header *eh,
471 int depth,
472 struct buffer_head *bh)
473{
474 int ret;
475
476 if (buffer_verified(bh))
477 return 0;
478 ret = ext4_ext_check(inode, eh, depth);
479 if (ret)
480 return ret;
481 set_buffer_verified(bh);
482 return ret;
483}
484
485#define ext4_ext_check_block(inode, eh, depth, bh) \
486 __ext4_ext_check_block(__func__, __LINE__, inode, eh, depth, bh)
487
Alex Tomasa86c6182006-10-11 01:21:03 -0700488#ifdef EXT_DEBUG
489static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
490{
491 int k, l = path->p_depth;
492
493 ext_debug("path:");
494 for (k = 0; k <= l; k++, path++) {
495 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700496 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400497 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700498 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400499 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700500 le32_to_cpu(path->p_ext->ee_block),
Mingming553f9002009-09-18 13:34:55 -0400501 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400502 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400503 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700504 } else
505 ext_debug(" []");
506 }
507 ext_debug("\n");
508}
509
510static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
511{
512 int depth = ext_depth(inode);
513 struct ext4_extent_header *eh;
514 struct ext4_extent *ex;
515 int i;
516
517 if (!path)
518 return;
519
520 eh = path[depth].p_hdr;
521 ex = EXT_FIRST_EXTENT(eh);
522
Mingming553f9002009-09-18 13:34:55 -0400523 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
524
Alex Tomasa86c6182006-10-11 01:21:03 -0700525 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400526 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
527 ext4_ext_is_uninitialized(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400528 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700529 }
530 ext_debug("\n");
531}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400532
533static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
534 ext4_fsblk_t newblock, int level)
535{
536 int depth = ext_depth(inode);
537 struct ext4_extent *ex;
538
539 if (depth != level) {
540 struct ext4_extent_idx *idx;
541 idx = path[level].p_idx;
542 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
543 ext_debug("%d: move %d:%llu in new index %llu\n", level,
544 le32_to_cpu(idx->ei_block),
545 ext4_idx_pblock(idx),
546 newblock);
547 idx++;
548 }
549
550 return;
551 }
552
553 ex = path[depth].p_ext;
554 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
555 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
556 le32_to_cpu(ex->ee_block),
557 ext4_ext_pblock(ex),
558 ext4_ext_is_uninitialized(ex),
559 ext4_ext_get_actual_len(ex),
560 newblock);
561 ex++;
562 }
563}
564
Alex Tomasa86c6182006-10-11 01:21:03 -0700565#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400566#define ext4_ext_show_path(inode, path)
567#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400568#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700569#endif
570
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500571void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700572{
573 int depth = path->p_depth;
574 int i;
575
576 for (i = 0; i <= depth; i++, path++)
577 if (path->p_bh) {
578 brelse(path->p_bh);
579 path->p_bh = NULL;
580 }
581}
582
583/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700584 * ext4_ext_binsearch_idx:
585 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400586 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700587 */
588static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500589ext4_ext_binsearch_idx(struct inode *inode,
590 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700591{
592 struct ext4_extent_header *eh = path->p_hdr;
593 struct ext4_extent_idx *r, *l, *m;
594
Alex Tomasa86c6182006-10-11 01:21:03 -0700595
Eric Sandeenbba90742008-01-28 23:58:27 -0500596 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700597
598 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400599 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700600 while (l <= r) {
601 m = l + (r - l) / 2;
602 if (block < le32_to_cpu(m->ei_block))
603 r = m - 1;
604 else
605 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400606 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
607 m, le32_to_cpu(m->ei_block),
608 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700609 }
610
611 path->p_idx = l - 1;
Zheng Liu4a3c3a52012-05-28 17:55:16 -0400612 ext_debug(" -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400613 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700614
615#ifdef CHECK_BINSEARCH
616 {
617 struct ext4_extent_idx *chix, *ix;
618 int k;
619
620 chix = ix = EXT_FIRST_INDEX(eh);
621 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
622 if (k != 0 &&
623 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400624 printk(KERN_DEBUG "k=%d, ix=0x%p, "
625 "first=0x%p\n", k,
626 ix, EXT_FIRST_INDEX(eh));
627 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700628 le32_to_cpu(ix->ei_block),
629 le32_to_cpu(ix[-1].ei_block));
630 }
631 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400632 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700633 if (block < le32_to_cpu(ix->ei_block))
634 break;
635 chix = ix;
636 }
637 BUG_ON(chix != path->p_idx);
638 }
639#endif
640
641}
642
643/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700644 * ext4_ext_binsearch:
645 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400646 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700647 */
648static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500649ext4_ext_binsearch(struct inode *inode,
650 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700651{
652 struct ext4_extent_header *eh = path->p_hdr;
653 struct ext4_extent *r, *l, *m;
654
Alex Tomasa86c6182006-10-11 01:21:03 -0700655 if (eh->eh_entries == 0) {
656 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700657 * this leaf is empty:
658 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700659 */
660 return;
661 }
662
Eric Sandeenbba90742008-01-28 23:58:27 -0500663 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700664
665 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400666 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700667
668 while (l <= r) {
669 m = l + (r - l) / 2;
670 if (block < le32_to_cpu(m->ee_block))
671 r = m - 1;
672 else
673 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400674 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
675 m, le32_to_cpu(m->ee_block),
676 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700677 }
678
679 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400680 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400681 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400682 ext4_ext_pblock(path->p_ext),
Mingming553f9002009-09-18 13:34:55 -0400683 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400684 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700685
686#ifdef CHECK_BINSEARCH
687 {
688 struct ext4_extent *chex, *ex;
689 int k;
690
691 chex = ex = EXT_FIRST_EXTENT(eh);
692 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
693 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400694 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700695 if (block < le32_to_cpu(ex->ee_block))
696 break;
697 chex = ex;
698 }
699 BUG_ON(chex != path->p_ext);
700 }
701#endif
702
703}
704
705int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
706{
707 struct ext4_extent_header *eh;
708
709 eh = ext_inode_hdr(inode);
710 eh->eh_depth = 0;
711 eh->eh_entries = 0;
712 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400713 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700714 ext4_mark_inode_dirty(handle, inode);
715 ext4_ext_invalidate_cache(inode);
716 return 0;
717}
718
719struct ext4_ext_path *
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500720ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
721 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700722{
723 struct ext4_extent_header *eh;
724 struct buffer_head *bh;
725 short int depth, i, ppos = 0, alloc = 0;
726
727 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400728 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700729
730 /* account possible depth increase */
731 if (!path) {
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800732 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700733 GFP_NOFS);
734 if (!path)
735 return ERR_PTR(-ENOMEM);
736 alloc = 1;
737 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700738 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400739 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700740
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400741 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700742 /* walk through the tree */
743 while (i) {
744 ext_debug("depth %d: num %d, max %d\n",
745 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400746
Alex Tomasa86c6182006-10-11 01:21:03 -0700747 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400748 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700749 path[ppos].p_depth = i;
750 path[ppos].p_ext = NULL;
751
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400752 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
753 if (unlikely(!bh))
Alex Tomasa86c6182006-10-11 01:21:03 -0700754 goto err;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400755 if (!bh_uptodate_or_lock(bh)) {
Jiaying Zhang0562e0b2011-03-21 21:38:05 -0400756 trace_ext4_ext_load_extent(inode, block,
757 path[ppos].p_block);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400758 if (bh_submit_read(bh) < 0) {
759 put_bh(bh);
760 goto err;
761 }
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400762 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700763 eh = ext_block_hdr(bh);
764 ppos++;
Frank Mayhar273df552010-03-02 11:46:09 -0500765 if (unlikely(ppos > depth)) {
766 put_bh(bh);
767 EXT4_ERROR_INODE(inode,
768 "ppos %d > depth %d", ppos, depth);
769 goto err;
770 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700771 path[ppos].p_bh = bh;
772 path[ppos].p_hdr = eh;
773 i--;
774
Darrick J. Wongf8489122012-04-29 18:21:10 -0400775 if (ext4_ext_check_block(inode, eh, i, bh))
Alex Tomasa86c6182006-10-11 01:21:03 -0700776 goto err;
777 }
778
779 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700780 path[ppos].p_ext = NULL;
781 path[ppos].p_idx = NULL;
782
Alex Tomasa86c6182006-10-11 01:21:03 -0700783 /* find extent */
784 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400785 /* if not an empty leaf */
786 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400787 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700788
789 ext4_ext_show_path(inode, path);
790
791 return path;
792
793err:
794 ext4_ext_drop_refs(path);
795 if (alloc)
796 kfree(path);
797 return ERR_PTR(-EIO);
798}
799
800/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700801 * ext4_ext_insert_index:
802 * insert new index [@logical;@ptr] into the block at @curp;
803 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700804 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400805static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
806 struct ext4_ext_path *curp,
807 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700808{
809 struct ext4_extent_idx *ix;
810 int len, err;
811
Avantika Mathur7e028972006-12-06 20:41:33 -0800812 err = ext4_ext_get_access(handle, inode, curp);
813 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700814 return err;
815
Frank Mayhar273df552010-03-02 11:46:09 -0500816 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
817 EXT4_ERROR_INODE(inode,
818 "logical %d == ei_block %d!",
819 logical, le32_to_cpu(curp->p_idx->ei_block));
820 return -EIO;
821 }
Robin Dongd4620312011-07-17 23:43:42 -0400822
823 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
824 >= le16_to_cpu(curp->p_hdr->eh_max))) {
825 EXT4_ERROR_INODE(inode,
826 "eh_entries %d >= eh_max %d!",
827 le16_to_cpu(curp->p_hdr->eh_entries),
828 le16_to_cpu(curp->p_hdr->eh_max));
829 return -EIO;
830 }
831
Alex Tomasa86c6182006-10-11 01:21:03 -0700832 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
833 /* insert after */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400834 ext_debug("insert new index %d after: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700835 ix = curp->p_idx + 1;
836 } else {
837 /* insert before */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400838 ext_debug("insert new index %d before: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700839 ix = curp->p_idx;
840 }
841
Eric Gouriou80e675f2011-10-27 11:52:18 -0400842 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
843 BUG_ON(len < 0);
844 if (len > 0) {
845 ext_debug("insert new index %d: "
846 "move %d indices from 0x%p to 0x%p\n",
847 logical, len, ix, ix + 1);
848 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
849 }
850
Tao Maf472e022011-10-17 10:13:46 -0400851 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
852 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
853 return -EIO;
854 }
855
Alex Tomasa86c6182006-10-11 01:21:03 -0700856 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700857 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400858 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700859
Frank Mayhar273df552010-03-02 11:46:09 -0500860 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
861 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
862 return -EIO;
863 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700864
865 err = ext4_ext_dirty(handle, inode, curp);
866 ext4_std_error(inode->i_sb, err);
867
868 return err;
869}
870
871/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700872 * ext4_ext_split:
873 * inserts new subtree into the path, using free index entry
874 * at depth @at:
875 * - allocates all needed blocks (new leaf and all intermediate index blocks)
876 * - makes decision where to split
877 * - moves remaining extents and index entries (right to the split point)
878 * into the newly allocated blocks
879 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700880 */
881static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -0400882 unsigned int flags,
883 struct ext4_ext_path *path,
884 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -0700885{
886 struct buffer_head *bh = NULL;
887 int depth = ext_depth(inode);
888 struct ext4_extent_header *neh;
889 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -0700890 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700891 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700892 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700893 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700894 int err = 0;
895
896 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700897 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700898
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700899 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700900 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -0500901 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
902 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
903 return -EIO;
904 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700905 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
906 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700907 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700908 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400909 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700910 } else {
911 border = newext->ee_block;
912 ext_debug("leaf will be added."
913 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400914 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700915 }
916
917 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700918 * If error occurs, then we break processing
919 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700920 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700921 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700922 */
923
924 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700925 * Get array to track all allocated blocks.
926 * We need this to handle errors and free blocks
927 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700928 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800929 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700930 if (!ablocks)
931 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700932
933 /* allocate all needed blocks */
934 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
935 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400936 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400937 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -0700938 if (newblock == 0)
939 goto cleanup;
940 ablocks[a] = newblock;
941 }
942
943 /* initialize new leaf */
944 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -0500945 if (unlikely(newblock == 0)) {
946 EXT4_ERROR_INODE(inode, "newblock == 0!");
947 err = -EIO;
948 goto cleanup;
949 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700950 bh = sb_getblk(inode->i_sb, newblock);
951 if (!bh) {
952 err = -EIO;
953 goto cleanup;
954 }
955 lock_buffer(bh);
956
Avantika Mathur7e028972006-12-06 20:41:33 -0800957 err = ext4_journal_get_create_access(handle, bh);
958 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700959 goto cleanup;
960
961 neh = ext_block_hdr(bh);
962 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400963 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700964 neh->eh_magic = EXT4_EXT_MAGIC;
965 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700966
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700967 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -0500968 if (unlikely(path[depth].p_hdr->eh_entries !=
969 path[depth].p_hdr->eh_max)) {
970 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
971 path[depth].p_hdr->eh_entries,
972 path[depth].p_hdr->eh_max);
973 err = -EIO;
974 goto cleanup;
975 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700976 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400977 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
978 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -0700979 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400980 struct ext4_extent *ex;
981 ex = EXT_FIRST_EXTENT(neh);
982 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400983 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700984 }
985
Darrick J. Wong7ac59902012-04-29 18:37:10 -0400986 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700987 set_buffer_uptodate(bh);
988 unlock_buffer(bh);
989
Frank Mayhar03901312009-01-07 00:06:22 -0500990 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800991 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700992 goto cleanup;
993 brelse(bh);
994 bh = NULL;
995
996 /* correct old leaf */
997 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800998 err = ext4_ext_get_access(handle, inode, path + depth);
999 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001000 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001001 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -08001002 err = ext4_ext_dirty(handle, inode, path + depth);
1003 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001004 goto cleanup;
1005
1006 }
1007
1008 /* create intermediate indexes */
1009 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -05001010 if (unlikely(k < 0)) {
1011 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
1012 err = -EIO;
1013 goto cleanup;
1014 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001015 if (k)
1016 ext_debug("create %d intermediate indices\n", k);
1017 /* insert new index into current index block */
1018 /* current depth stored in i var */
1019 i = depth - 1;
1020 while (k--) {
1021 oldblock = newblock;
1022 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -05001023 bh = sb_getblk(inode->i_sb, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001024 if (!bh) {
1025 err = -EIO;
1026 goto cleanup;
1027 }
1028 lock_buffer(bh);
1029
Avantika Mathur7e028972006-12-06 20:41:33 -08001030 err = ext4_journal_get_create_access(handle, bh);
1031 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001032 goto cleanup;
1033
1034 neh = ext_block_hdr(bh);
1035 neh->eh_entries = cpu_to_le16(1);
1036 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001037 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001038 neh->eh_depth = cpu_to_le16(depth - i);
1039 fidx = EXT_FIRST_INDEX(neh);
1040 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001041 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001042
Eric Sandeenbba90742008-01-28 23:58:27 -05001043 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
1044 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001045
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001046 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -05001047 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
1048 EXT_LAST_INDEX(path[i].p_hdr))) {
1049 EXT4_ERROR_INODE(inode,
1050 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1051 le32_to_cpu(path[i].p_ext->ee_block));
1052 err = -EIO;
1053 goto cleanup;
1054 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001055 /* start copy indexes */
1056 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
1057 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
1058 EXT_MAX_INDEX(path[i].p_hdr));
1059 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07001060 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001061 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -07001062 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001063 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001064 }
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001065 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001066 set_buffer_uptodate(bh);
1067 unlock_buffer(bh);
1068
Frank Mayhar03901312009-01-07 00:06:22 -05001069 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001070 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001071 goto cleanup;
1072 brelse(bh);
1073 bh = NULL;
1074
1075 /* correct old index */
1076 if (m) {
1077 err = ext4_ext_get_access(handle, inode, path + i);
1078 if (err)
1079 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001080 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001081 err = ext4_ext_dirty(handle, inode, path + i);
1082 if (err)
1083 goto cleanup;
1084 }
1085
1086 i--;
1087 }
1088
1089 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001090 err = ext4_ext_insert_index(handle, inode, path + at,
1091 le32_to_cpu(border), newblock);
1092
1093cleanup:
1094 if (bh) {
1095 if (buffer_locked(bh))
1096 unlock_buffer(bh);
1097 brelse(bh);
1098 }
1099
1100 if (err) {
1101 /* free all allocated blocks in error case */
1102 for (i = 0; i < depth; i++) {
1103 if (!ablocks[i])
1104 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001105 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001106 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001107 }
1108 }
1109 kfree(ablocks);
1110
1111 return err;
1112}
1113
1114/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001115 * ext4_ext_grow_indepth:
1116 * implements tree growing procedure:
1117 * - allocates new block
1118 * - moves top-level data (index block or leaf) into the new block
1119 * - initializes new top-level, creating index that points to the
1120 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001121 */
1122static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001123 unsigned int flags,
Allison Henderson55f020d2011-05-25 07:41:26 -04001124 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001125{
Alex Tomasa86c6182006-10-11 01:21:03 -07001126 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001127 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001128 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001129 int err = 0;
1130
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001131 newblock = ext4_ext_new_meta_block(handle, inode, NULL,
Allison Henderson55f020d2011-05-25 07:41:26 -04001132 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001133 if (newblock == 0)
1134 return err;
1135
1136 bh = sb_getblk(inode->i_sb, newblock);
1137 if (!bh) {
1138 err = -EIO;
1139 ext4_std_error(inode->i_sb, err);
1140 return err;
1141 }
1142 lock_buffer(bh);
1143
Avantika Mathur7e028972006-12-06 20:41:33 -08001144 err = ext4_journal_get_create_access(handle, bh);
1145 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001146 unlock_buffer(bh);
1147 goto out;
1148 }
1149
1150 /* move top-level index/leaf into new block */
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001151 memmove(bh->b_data, EXT4_I(inode)->i_data,
1152 sizeof(EXT4_I(inode)->i_data));
Alex Tomasa86c6182006-10-11 01:21:03 -07001153
1154 /* set size of new block */
1155 neh = ext_block_hdr(bh);
1156 /* old root could have indexes or leaves
1157 * so calculate e_max right way */
1158 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001159 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001160 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001161 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001162 neh->eh_magic = EXT4_EXT_MAGIC;
Darrick J. Wong7ac59902012-04-29 18:37:10 -04001163 ext4_extent_block_csum_set(inode, neh);
Alex Tomasa86c6182006-10-11 01:21:03 -07001164 set_buffer_uptodate(bh);
1165 unlock_buffer(bh);
1166
Frank Mayhar03901312009-01-07 00:06:22 -05001167 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001168 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001169 goto out;
1170
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001171 /* Update top-level index: num,max,pointer */
Alex Tomasa86c6182006-10-11 01:21:03 -07001172 neh = ext_inode_hdr(inode);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001173 neh->eh_entries = cpu_to_le16(1);
1174 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1175 if (neh->eh_depth == 0) {
1176 /* Root extent block becomes index block */
1177 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1178 EXT_FIRST_INDEX(neh)->ei_block =
1179 EXT_FIRST_EXTENT(neh)->ee_block;
1180 }
Mingming Cao2ae02102006-10-11 01:21:11 -07001181 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001182 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001183 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001184 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001185
Wei Yongjunba39ebb2012-09-27 09:37:53 -04001186 le16_add_cpu(&neh->eh_depth, 1);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001187 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07001188out:
1189 brelse(bh);
1190
1191 return err;
1192}
1193
1194/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001195 * ext4_ext_create_new_leaf:
1196 * finds empty index and adds new leaf.
1197 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001198 */
1199static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001200 unsigned int flags,
1201 struct ext4_ext_path *path,
1202 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001203{
1204 struct ext4_ext_path *curp;
1205 int depth, i, err = 0;
1206
1207repeat:
1208 i = depth = ext_depth(inode);
1209
1210 /* walk up to the tree and look for free index entry */
1211 curp = path + depth;
1212 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1213 i--;
1214 curp--;
1215 }
1216
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001217 /* we use already allocated block for index block,
1218 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001219 if (EXT_HAS_FREE_INDEX(curp)) {
1220 /* if we found index with free entry, then use that
1221 * entry: create all needed subtree and add new leaf */
Allison Henderson55f020d2011-05-25 07:41:26 -04001222 err = ext4_ext_split(handle, inode, flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001223 if (err)
1224 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001225
1226 /* refill path */
1227 ext4_ext_drop_refs(path);
1228 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001229 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1230 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001231 if (IS_ERR(path))
1232 err = PTR_ERR(path);
1233 } else {
1234 /* tree is full, time to grow in depth */
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001235 err = ext4_ext_grow_indepth(handle, inode, flags, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001236 if (err)
1237 goto out;
1238
1239 /* refill path */
1240 ext4_ext_drop_refs(path);
1241 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001242 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1243 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001244 if (IS_ERR(path)) {
1245 err = PTR_ERR(path);
1246 goto out;
1247 }
1248
1249 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001250 * only first (depth 0 -> 1) produces free space;
1251 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001252 */
1253 depth = ext_depth(inode);
1254 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001255 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001256 goto repeat;
1257 }
1258 }
1259
1260out:
1261 return err;
1262}
1263
1264/*
Alex Tomas1988b512008-01-28 23:58:27 -05001265 * search the closest allocated block to the left for *logical
1266 * and returns it at @logical + it's physical address at @phys
1267 * if *logical is the smallest allocated block, the function
1268 * returns 0 at @phys
1269 * return value contains 0 (success) or error code
1270 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001271static int ext4_ext_search_left(struct inode *inode,
1272 struct ext4_ext_path *path,
1273 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001274{
1275 struct ext4_extent_idx *ix;
1276 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001277 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001278
Frank Mayhar273df552010-03-02 11:46:09 -05001279 if (unlikely(path == NULL)) {
1280 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1281 return -EIO;
1282 }
Alex Tomas1988b512008-01-28 23:58:27 -05001283 depth = path->p_depth;
1284 *phys = 0;
1285
1286 if (depth == 0 && path->p_ext == NULL)
1287 return 0;
1288
1289 /* usually extent in the path covers blocks smaller
1290 * then *logical, but it can be that extent is the
1291 * first one in the file */
1292
1293 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001294 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001295 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001296 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1297 EXT4_ERROR_INODE(inode,
1298 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1299 *logical, le32_to_cpu(ex->ee_block));
1300 return -EIO;
1301 }
Alex Tomas1988b512008-01-28 23:58:27 -05001302 while (--depth >= 0) {
1303 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001304 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1305 EXT4_ERROR_INODE(inode,
1306 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
Tao Ma6ee3b212011-10-08 16:08:34 -04001307 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001308 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
Tao Ma6ee3b212011-10-08 16:08:34 -04001309 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001310 depth);
1311 return -EIO;
1312 }
Alex Tomas1988b512008-01-28 23:58:27 -05001313 }
1314 return 0;
1315 }
1316
Frank Mayhar273df552010-03-02 11:46:09 -05001317 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1318 EXT4_ERROR_INODE(inode,
1319 "logical %d < ee_block %d + ee_len %d!",
1320 *logical, le32_to_cpu(ex->ee_block), ee_len);
1321 return -EIO;
1322 }
Alex Tomas1988b512008-01-28 23:58:27 -05001323
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001324 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001325 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001326 return 0;
1327}
1328
1329/*
1330 * search the closest allocated block to the right for *logical
1331 * and returns it at @logical + it's physical address at @phys
Tao Madf3ab172011-10-08 15:53:49 -04001332 * if *logical is the largest allocated block, the function
Alex Tomas1988b512008-01-28 23:58:27 -05001333 * returns 0 at @phys
1334 * return value contains 0 (success) or error code
1335 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001336static int ext4_ext_search_right(struct inode *inode,
1337 struct ext4_ext_path *path,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001338 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1339 struct ext4_extent **ret_ex)
Alex Tomas1988b512008-01-28 23:58:27 -05001340{
1341 struct buffer_head *bh = NULL;
1342 struct ext4_extent_header *eh;
1343 struct ext4_extent_idx *ix;
1344 struct ext4_extent *ex;
1345 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001346 int depth; /* Note, NOT eh_depth; depth from top of tree */
1347 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001348
Frank Mayhar273df552010-03-02 11:46:09 -05001349 if (unlikely(path == NULL)) {
1350 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1351 return -EIO;
1352 }
Alex Tomas1988b512008-01-28 23:58:27 -05001353 depth = path->p_depth;
1354 *phys = 0;
1355
1356 if (depth == 0 && path->p_ext == NULL)
1357 return 0;
1358
1359 /* usually extent in the path covers blocks smaller
1360 * then *logical, but it can be that extent is the
1361 * first one in the file */
1362
1363 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001364 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001365 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001366 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1367 EXT4_ERROR_INODE(inode,
1368 "first_extent(path[%d].p_hdr) != ex",
1369 depth);
1370 return -EIO;
1371 }
Alex Tomas1988b512008-01-28 23:58:27 -05001372 while (--depth >= 0) {
1373 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001374 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1375 EXT4_ERROR_INODE(inode,
1376 "ix != EXT_FIRST_INDEX *logical %d!",
1377 *logical);
1378 return -EIO;
1379 }
Alex Tomas1988b512008-01-28 23:58:27 -05001380 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001381 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001382 }
1383
Frank Mayhar273df552010-03-02 11:46:09 -05001384 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1385 EXT4_ERROR_INODE(inode,
1386 "logical %d < ee_block %d + ee_len %d!",
1387 *logical, le32_to_cpu(ex->ee_block), ee_len);
1388 return -EIO;
1389 }
Alex Tomas1988b512008-01-28 23:58:27 -05001390
1391 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1392 /* next allocated block in this leaf */
1393 ex++;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001394 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001395 }
1396
1397 /* go up and search for index to the right */
1398 while (--depth >= 0) {
1399 ix = path[depth].p_idx;
1400 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001401 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001402 }
1403
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001404 /* we've gone up to the root and found no index to the right */
1405 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001406
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001407got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001408 /* we've found index to the right, let's
1409 * follow it and find the closest allocated
1410 * block to the right */
1411 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001412 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001413 while (++depth < path->p_depth) {
1414 bh = sb_bread(inode->i_sb, block);
1415 if (bh == NULL)
1416 return -EIO;
1417 eh = ext_block_hdr(bh);
Eric Sandeen395a87b2009-03-10 18:18:47 -04001418 /* subtract from p_depth to get proper eh_depth */
Darrick J. Wongf8489122012-04-29 18:21:10 -04001419 if (ext4_ext_check_block(inode, eh,
1420 path->p_depth - depth, bh)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001421 put_bh(bh);
1422 return -EIO;
1423 }
1424 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001425 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001426 put_bh(bh);
1427 }
1428
1429 bh = sb_bread(inode->i_sb, block);
1430 if (bh == NULL)
1431 return -EIO;
1432 eh = ext_block_hdr(bh);
Darrick J. Wongf8489122012-04-29 18:21:10 -04001433 if (ext4_ext_check_block(inode, eh, path->p_depth - depth, bh)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001434 put_bh(bh);
1435 return -EIO;
1436 }
1437 ex = EXT_FIRST_EXTENT(eh);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001438found_extent:
Alex Tomas1988b512008-01-28 23:58:27 -05001439 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001440 *phys = ext4_ext_pblock(ex);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001441 *ret_ex = ex;
1442 if (bh)
1443 put_bh(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001444 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001445}
1446
1447/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001448 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001449 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001450 * NOTE: it considers block number from index entry as
1451 * allocated block. Thus, index entries have to be consistent
1452 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001453 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001454static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001455ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1456{
1457 int depth;
1458
1459 BUG_ON(path == NULL);
1460 depth = path->p_depth;
1461
1462 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001463 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001464
1465 while (depth >= 0) {
1466 if (depth == path->p_depth) {
1467 /* leaf */
Curt Wohlgemuth6f8ff532011-10-26 04:38:59 -04001468 if (path[depth].p_ext &&
1469 path[depth].p_ext !=
Alex Tomasa86c6182006-10-11 01:21:03 -07001470 EXT_LAST_EXTENT(path[depth].p_hdr))
1471 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1472 } else {
1473 /* index */
1474 if (path[depth].p_idx !=
1475 EXT_LAST_INDEX(path[depth].p_hdr))
1476 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1477 }
1478 depth--;
1479 }
1480
Lukas Czernerf17722f2011-06-06 00:05:17 -04001481 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001482}
1483
1484/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001485 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001486 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001487 */
Robin Dong57187892011-07-23 21:49:07 -04001488static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001489{
1490 int depth;
1491
1492 BUG_ON(path == NULL);
1493 depth = path->p_depth;
1494
1495 /* zero-tree has no leaf blocks at all */
1496 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001497 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001498
1499 /* go to index block */
1500 depth--;
1501
1502 while (depth >= 0) {
1503 if (path[depth].p_idx !=
1504 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001505 return (ext4_lblk_t)
1506 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001507 depth--;
1508 }
1509
Lukas Czernerf17722f2011-06-06 00:05:17 -04001510 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001511}
1512
1513/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001514 * ext4_ext_correct_indexes:
1515 * if leaf gets modified and modified extent is first in the leaf,
1516 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001517 * TODO: do we need to correct tree in all cases?
1518 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001519static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001520 struct ext4_ext_path *path)
1521{
1522 struct ext4_extent_header *eh;
1523 int depth = ext_depth(inode);
1524 struct ext4_extent *ex;
1525 __le32 border;
1526 int k, err = 0;
1527
1528 eh = path[depth].p_hdr;
1529 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001530
1531 if (unlikely(ex == NULL || eh == NULL)) {
1532 EXT4_ERROR_INODE(inode,
1533 "ex %p == NULL or eh %p == NULL", ex, eh);
1534 return -EIO;
1535 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001536
1537 if (depth == 0) {
1538 /* there is no tree at all */
1539 return 0;
1540 }
1541
1542 if (ex != EXT_FIRST_EXTENT(eh)) {
1543 /* we correct tree if first leaf got modified only */
1544 return 0;
1545 }
1546
1547 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001548 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001549 */
1550 k = depth - 1;
1551 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001552 err = ext4_ext_get_access(handle, inode, path + k);
1553 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001554 return err;
1555 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001556 err = ext4_ext_dirty(handle, inode, path + k);
1557 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001558 return err;
1559
1560 while (k--) {
1561 /* change all left-side indexes */
1562 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1563 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001564 err = ext4_ext_get_access(handle, inode, path + k);
1565 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001566 break;
1567 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001568 err = ext4_ext_dirty(handle, inode, path + k);
1569 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001570 break;
1571 }
1572
1573 return err;
1574}
1575
Akira Fujita748de672009-06-17 19:24:03 -04001576int
Alex Tomasa86c6182006-10-11 01:21:03 -07001577ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1578 struct ext4_extent *ex2)
1579{
Amit Arora749269f2007-07-18 09:02:56 -04001580 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001581
1582 /*
1583 * Make sure that either both extents are uninitialized, or
1584 * both are _not_.
1585 */
1586 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1587 return 0;
1588
Amit Arora749269f2007-07-18 09:02:56 -04001589 if (ext4_ext_is_uninitialized(ex1))
1590 max_len = EXT_UNINIT_MAX_LEN;
1591 else
1592 max_len = EXT_INIT_MAX_LEN;
1593
Amit Aroraa2df2a62007-07-17 21:42:41 -04001594 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1595 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1596
1597 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001598 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001599 return 0;
1600
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001601 /*
1602 * To allow future support for preallocated extents to be added
1603 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001604 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001605 */
Amit Arora749269f2007-07-18 09:02:56 -04001606 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001607 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001608#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001609 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001610 return 0;
1611#endif
1612
Theodore Ts'obf89d162010-10-27 21:30:14 -04001613 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001614 return 1;
1615 return 0;
1616}
1617
1618/*
Amit Arora56055d32007-07-17 21:42:38 -04001619 * This function tries to merge the "ex" extent to the next extent in the tree.
1620 * It always tries to merge towards right. If you want to merge towards
1621 * left, pass "ex - 1" as argument instead of "ex".
1622 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1623 * 1 if they got merged.
1624 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001625static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001626 struct ext4_ext_path *path,
1627 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001628{
1629 struct ext4_extent_header *eh;
1630 unsigned int depth, len;
1631 int merge_done = 0;
1632 int uninitialized = 0;
1633
1634 depth = ext_depth(inode);
1635 BUG_ON(path[depth].p_hdr == NULL);
1636 eh = path[depth].p_hdr;
1637
1638 while (ex < EXT_LAST_EXTENT(eh)) {
1639 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1640 break;
1641 /* merge with next extent! */
1642 if (ext4_ext_is_uninitialized(ex))
1643 uninitialized = 1;
1644 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1645 + ext4_ext_get_actual_len(ex + 1));
1646 if (uninitialized)
1647 ext4_ext_mark_uninitialized(ex);
1648
1649 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1650 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1651 * sizeof(struct ext4_extent);
1652 memmove(ex + 1, ex + 2, len);
1653 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001654 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001655 merge_done = 1;
1656 WARN_ON(eh->eh_entries == 0);
1657 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001658 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001659 }
1660
1661 return merge_done;
1662}
1663
1664/*
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001665 * This function does a very simple check to see if we can collapse
1666 * an extent tree with a single extent tree leaf block into the inode.
1667 */
1668static void ext4_ext_try_to_merge_up(handle_t *handle,
1669 struct inode *inode,
1670 struct ext4_ext_path *path)
1671{
1672 size_t s;
1673 unsigned max_root = ext4_ext_space_root(inode, 0);
1674 ext4_fsblk_t blk;
1675
1676 if ((path[0].p_depth != 1) ||
1677 (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) ||
1678 (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root))
1679 return;
1680
1681 /*
1682 * We need to modify the block allocation bitmap and the block
1683 * group descriptor to release the extent tree block. If we
1684 * can't get the journal credits, give up.
1685 */
1686 if (ext4_journal_extend(handle, 2))
1687 return;
1688
1689 /*
1690 * Copy the extent data up to the inode
1691 */
1692 blk = ext4_idx_pblock(path[0].p_idx);
1693 s = le16_to_cpu(path[1].p_hdr->eh_entries) *
1694 sizeof(struct ext4_extent_idx);
1695 s += sizeof(struct ext4_extent_header);
1696
1697 memcpy(path[0].p_hdr, path[1].p_hdr, s);
1698 path[0].p_depth = 0;
1699 path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) +
1700 (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr));
1701 path[0].p_hdr->eh_max = cpu_to_le16(max_root);
1702
1703 brelse(path[1].p_bh);
1704 ext4_free_blocks(handle, inode, NULL, blk, 1,
1705 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
1706}
1707
1708/*
Yongqiang Yang197217a2011-05-03 11:45:29 -04001709 * This function tries to merge the @ex extent to neighbours in the tree.
1710 * return 1 if merge left else 0.
1711 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001712static void ext4_ext_try_to_merge(handle_t *handle,
1713 struct inode *inode,
Yongqiang Yang197217a2011-05-03 11:45:29 -04001714 struct ext4_ext_path *path,
1715 struct ext4_extent *ex) {
1716 struct ext4_extent_header *eh;
1717 unsigned int depth;
1718 int merge_done = 0;
Yongqiang Yang197217a2011-05-03 11:45:29 -04001719
1720 depth = ext_depth(inode);
1721 BUG_ON(path[depth].p_hdr == NULL);
1722 eh = path[depth].p_hdr;
1723
1724 if (ex > EXT_FIRST_EXTENT(eh))
1725 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1726
1727 if (!merge_done)
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001728 (void) ext4_ext_try_to_merge_right(inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001729
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001730 ext4_ext_try_to_merge_up(handle, inode, path);
Yongqiang Yang197217a2011-05-03 11:45:29 -04001731}
1732
1733/*
Amit Arora25d14f92007-05-24 13:04:13 -04001734 * check if a portion of the "newext" extent overlaps with an
1735 * existing extent.
1736 *
1737 * If there is an overlap discovered, it updates the length of the newext
1738 * such that there will be no overlap, and then returns 1.
1739 * If there is no overlap found, it returns 0.
1740 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001741static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1742 struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001743 struct ext4_extent *newext,
1744 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001745{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001746 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001747 unsigned int depth, len1;
1748 unsigned int ret = 0;
1749
1750 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001751 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001752 depth = ext_depth(inode);
1753 if (!path[depth].p_ext)
1754 goto out;
1755 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001756 b2 &= ~(sbi->s_cluster_ratio - 1);
Amit Arora25d14f92007-05-24 13:04:13 -04001757
1758 /*
1759 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001760 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001761 */
1762 if (b2 < b1) {
1763 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001764 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001765 goto out;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001766 b2 &= ~(sbi->s_cluster_ratio - 1);
Amit Arora25d14f92007-05-24 13:04:13 -04001767 }
1768
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001769 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001770 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001771 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001772 newext->ee_len = cpu_to_le16(len1);
1773 ret = 1;
1774 }
1775
1776 /* check for overlap */
1777 if (b1 + len1 > b2) {
1778 newext->ee_len = cpu_to_le16(b2 - b1);
1779 ret = 1;
1780 }
1781out:
1782 return ret;
1783}
1784
1785/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001786 * ext4_ext_insert_extent:
1787 * tries to merge requsted extent into the existing extent or
1788 * inserts requested extent as new one into the tree,
1789 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001790 */
1791int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1792 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04001793 struct ext4_extent *newext, int flag)
Alex Tomasa86c6182006-10-11 01:21:03 -07001794{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001795 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001796 struct ext4_extent *ex, *fex;
1797 struct ext4_extent *nearex; /* nearest extent */
1798 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001799 int depth, len, err;
1800 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001801 unsigned uninitialized = 0;
Allison Henderson55f020d2011-05-25 07:41:26 -04001802 int flags = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001803
Frank Mayhar273df552010-03-02 11:46:09 -05001804 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1805 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1806 return -EIO;
1807 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001808 depth = ext_depth(inode);
1809 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001810 if (unlikely(path[depth].p_hdr == NULL)) {
1811 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1812 return -EIO;
1813 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001814
1815 /* try to insert block into found extent and return */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001816 if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
Mingming Cao00314622009-09-28 15:49:08 -04001817 && ext4_can_extents_be_merged(inode, ex, newext)) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04001818 ext_debug("append [%d]%d block to %u:[%d]%d (from %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04001819 ext4_ext_is_uninitialized(newext),
1820 ext4_ext_get_actual_len(newext),
1821 le32_to_cpu(ex->ee_block),
1822 ext4_ext_is_uninitialized(ex),
1823 ext4_ext_get_actual_len(ex),
1824 ext4_ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001825 err = ext4_ext_get_access(handle, inode, path + depth);
1826 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001827 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001828
1829 /*
1830 * ext4_can_extents_be_merged should have checked that either
1831 * both extents are uninitialized, or both aren't. Thus we
1832 * need to check only one of them here.
1833 */
1834 if (ext4_ext_is_uninitialized(ex))
1835 uninitialized = 1;
1836 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1837 + ext4_ext_get_actual_len(newext));
1838 if (uninitialized)
1839 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001840 eh = path[depth].p_hdr;
1841 nearex = ex;
1842 goto merge;
1843 }
1844
Alex Tomasa86c6182006-10-11 01:21:03 -07001845 depth = ext_depth(inode);
1846 eh = path[depth].p_hdr;
1847 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1848 goto has_space;
1849
1850 /* probably next leaf has space for us? */
1851 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04001852 next = EXT_MAX_BLOCKS;
1853 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
Robin Dong57187892011-07-23 21:49:07 -04001854 next = ext4_ext_next_leaf_block(path);
Robin Dong598dbdf2011-07-11 18:24:01 -04001855 if (next != EXT_MAX_BLOCKS) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04001856 ext_debug("next leaf block - %u\n", next);
Alex Tomasa86c6182006-10-11 01:21:03 -07001857 BUG_ON(npath != NULL);
1858 npath = ext4_ext_find_extent(inode, next, NULL);
1859 if (IS_ERR(npath))
1860 return PTR_ERR(npath);
1861 BUG_ON(npath->p_depth != path->p_depth);
1862 eh = npath[depth].p_hdr;
1863 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001864 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001865 le16_to_cpu(eh->eh_entries));
1866 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04001867 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07001868 }
1869 ext_debug("next leaf has no free space(%d,%d)\n",
1870 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1871 }
1872
1873 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001874 * There is no free space in the found leaf.
1875 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001876 */
Allison Henderson55f020d2011-05-25 07:41:26 -04001877 if (flag & EXT4_GET_BLOCKS_PUNCH_OUT_EXT)
1878 flags = EXT4_MB_USE_ROOT_BLOCKS;
1879 err = ext4_ext_create_new_leaf(handle, inode, flags, path, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001880 if (err)
1881 goto cleanup;
1882 depth = ext_depth(inode);
1883 eh = path[depth].p_hdr;
1884
1885has_space:
1886 nearex = path[depth].p_ext;
1887
Avantika Mathur7e028972006-12-06 20:41:33 -08001888 err = ext4_ext_get_access(handle, inode, path + depth);
1889 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001890 goto cleanup;
1891
1892 if (!nearex) {
1893 /* there is no extent in this leaf, create first one */
Yongqiang Yang32de6752011-11-01 18:56:41 -04001894 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001895 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001896 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001897 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001898 ext4_ext_get_actual_len(newext));
Eric Gouriou80e675f2011-10-27 11:52:18 -04001899 nearex = EXT_FIRST_EXTENT(eh);
1900 } else {
1901 if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001902 > le32_to_cpu(nearex->ee_block)) {
Eric Gouriou80e675f2011-10-27 11:52:18 -04001903 /* Insert after */
Yongqiang Yang32de6752011-11-01 18:56:41 -04001904 ext_debug("insert %u:%llu:[%d]%d before: "
1905 "nearest %p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001906 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001907 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001908 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001909 ext4_ext_get_actual_len(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04001910 nearex);
1911 nearex++;
1912 } else {
1913 /* Insert before */
1914 BUG_ON(newext->ee_block == nearex->ee_block);
Yongqiang Yang32de6752011-11-01 18:56:41 -04001915 ext_debug("insert %u:%llu:[%d]%d after: "
1916 "nearest %p\n",
Eric Gouriou80e675f2011-10-27 11:52:18 -04001917 le32_to_cpu(newext->ee_block),
1918 ext4_ext_pblock(newext),
1919 ext4_ext_is_uninitialized(newext),
1920 ext4_ext_get_actual_len(newext),
1921 nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001922 }
Eric Gouriou80e675f2011-10-27 11:52:18 -04001923 len = EXT_LAST_EXTENT(eh) - nearex + 1;
1924 if (len > 0) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04001925 ext_debug("insert %u:%llu:[%d]%d: "
Eric Gouriou80e675f2011-10-27 11:52:18 -04001926 "move %d extents from 0x%p to 0x%p\n",
1927 le32_to_cpu(newext->ee_block),
1928 ext4_ext_pblock(newext),
1929 ext4_ext_is_uninitialized(newext),
1930 ext4_ext_get_actual_len(newext),
1931 len, nearex, nearex + 1);
1932 memmove(nearex + 1, nearex,
1933 len * sizeof(struct ext4_extent));
1934 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001935 }
1936
Marcin Slusarze8546d02008-04-17 10:38:59 -04001937 le16_add_cpu(&eh->eh_entries, 1);
Eric Gouriou80e675f2011-10-27 11:52:18 -04001938 path[depth].p_ext = nearex;
Alex Tomasa86c6182006-10-11 01:21:03 -07001939 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001940 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001941 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001942
1943merge:
HaiboLiue7bcf822012-07-09 16:29:28 -04001944 /* try to merge extents */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001945 if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001946 ext4_ext_try_to_merge(handle, inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001947
Alex Tomasa86c6182006-10-11 01:21:03 -07001948
1949 /* time to correct all indexes above */
1950 err = ext4_ext_correct_indexes(handle, inode, path);
1951 if (err)
1952 goto cleanup;
1953
Theodore Ts'oecb94f52012-08-17 09:44:17 -04001954 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07001955
1956cleanup:
1957 if (npath) {
1958 ext4_ext_drop_refs(npath);
1959 kfree(npath);
1960 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001961 ext4_ext_invalidate_cache(inode);
1962 return err;
1963}
1964
Lukas Czerner91dd8c12012-11-28 12:32:26 -05001965static int ext4_fill_fiemap_extents(struct inode *inode,
1966 ext4_lblk_t block, ext4_lblk_t num,
1967 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04001968{
1969 struct ext4_ext_path *path = NULL;
1970 struct ext4_ext_cache cbex;
1971 struct ext4_extent *ex;
Lukas Czerner91dd8c12012-11-28 12:32:26 -05001972 ext4_lblk_t next, next_del, start = 0, end = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04001973 ext4_lblk_t last = block + num;
Lukas Czerner91dd8c12012-11-28 12:32:26 -05001974 int exists, depth = 0, err = 0;
1975 unsigned int flags = 0;
1976 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04001977
Lukas Czernerf17722f2011-06-06 00:05:17 -04001978 while (block < last && block != EXT_MAX_BLOCKS) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04001979 num = last - block;
1980 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001981 down_read(&EXT4_I(inode)->i_data_sem);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05001982
1983 if (path && ext_depth(inode) != depth) {
1984 /* depth was changed. we have to realloc path */
1985 kfree(path);
1986 path = NULL;
1987 }
1988
Eric Sandeen6873fa02008-10-07 00:46:36 -04001989 path = ext4_ext_find_extent(inode, block, path);
1990 if (IS_ERR(path)) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05001991 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001992 err = PTR_ERR(path);
1993 path = NULL;
1994 break;
1995 }
1996
1997 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05001998 if (unlikely(path[depth].p_hdr == NULL)) {
Lukas Czerner91dd8c12012-11-28 12:32:26 -05001999 up_read(&EXT4_I(inode)->i_data_sem);
Frank Mayhar273df552010-03-02 11:46:09 -05002000 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2001 err = -EIO;
2002 break;
2003 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002004 ex = path[depth].p_ext;
2005 next = ext4_ext_next_allocated_block(path);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002006 ext4_ext_drop_refs(path);
Eric Sandeen6873fa02008-10-07 00:46:36 -04002007
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002008 flags = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002009 exists = 0;
2010 if (!ex) {
2011 /* there is no extent yet, so try to allocate
2012 * all requested space */
2013 start = block;
2014 end = block + num;
2015 } else if (le32_to_cpu(ex->ee_block) > block) {
2016 /* need to allocate space before found extent */
2017 start = block;
2018 end = le32_to_cpu(ex->ee_block);
2019 if (block + num < end)
2020 end = block + num;
2021 } else if (block >= le32_to_cpu(ex->ee_block)
2022 + ext4_ext_get_actual_len(ex)) {
2023 /* need to allocate space after found extent */
2024 start = block;
2025 end = block + num;
2026 if (end >= next)
2027 end = next;
2028 } else if (block >= le32_to_cpu(ex->ee_block)) {
2029 /*
2030 * some part of requested space is covered
2031 * by found extent
2032 */
2033 start = block;
2034 end = le32_to_cpu(ex->ee_block)
2035 + ext4_ext_get_actual_len(ex);
2036 if (block + num < end)
2037 end = block + num;
2038 exists = 1;
2039 } else {
2040 BUG();
2041 }
2042 BUG_ON(end <= start);
2043
2044 if (!exists) {
2045 cbex.ec_block = start;
2046 cbex.ec_len = end - start;
2047 cbex.ec_start = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002048 } else {
2049 cbex.ec_block = le32_to_cpu(ex->ee_block);
2050 cbex.ec_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04002051 cbex.ec_start = ext4_ext_pblock(ex);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002052 if (ext4_ext_is_uninitialized(ex))
2053 flags |= FIEMAP_EXTENT_UNWRITTEN;
Eric Sandeen6873fa02008-10-07 00:46:36 -04002054 }
2055
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002056 /*
2057 * Find delayed extent and update cbex accordingly. We call
2058 * it even in !exists case to find out whether cbex is the
2059 * last existing extent or not.
2060 */
2061 next_del = ext4_find_delayed_extent(inode, &cbex);
2062 if (!exists && next_del) {
2063 exists = 1;
2064 flags |= FIEMAP_EXTENT_DELALLOC;
2065 }
2066 up_read(&EXT4_I(inode)->i_data_sem);
2067
Frank Mayhar273df552010-03-02 11:46:09 -05002068 if (unlikely(cbex.ec_len == 0)) {
2069 EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
2070 err = -EIO;
2071 break;
2072 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002073
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002074 /* This is possible iff next == next_del == EXT_MAX_BLOCKS */
2075 if (next == next_del) {
2076 flags |= FIEMAP_EXTENT_LAST;
2077 if (unlikely(next_del != EXT_MAX_BLOCKS ||
2078 next != EXT_MAX_BLOCKS)) {
2079 EXT4_ERROR_INODE(inode,
2080 "next extent == %u, next "
2081 "delalloc extent = %u",
2082 next, next_del);
2083 err = -EIO;
2084 break;
2085 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002086 }
2087
Lukas Czerner91dd8c12012-11-28 12:32:26 -05002088 if (exists) {
2089 err = fiemap_fill_next_extent(fieinfo,
2090 (__u64)cbex.ec_block << blksize_bits,
2091 (__u64)cbex.ec_start << blksize_bits,
2092 (__u64)cbex.ec_len << blksize_bits,
2093 flags);
2094 if (err < 0)
2095 break;
2096 if (err == 1) {
2097 err = 0;
2098 break;
2099 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04002100 }
2101
2102 block = cbex.ec_block + cbex.ec_len;
2103 }
2104
2105 if (path) {
2106 ext4_ext_drop_refs(path);
2107 kfree(path);
2108 }
2109
2110 return err;
2111}
2112
Avantika Mathur09b88252006-12-06 20:41:36 -08002113static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002114ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002115 __u32 len, ext4_fsblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07002116{
2117 struct ext4_ext_cache *cex;
2118 BUG_ON(len == 0);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002119 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Aditya Kalid8990242011-09-09 19:18:51 -04002120 trace_ext4_ext_put_in_cache(inode, block, len, start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002121 cex = &EXT4_I(inode)->i_cached_extent;
Alex Tomasa86c6182006-10-11 01:21:03 -07002122 cex->ec_block = block;
2123 cex->ec_len = len;
2124 cex->ec_start = start;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002125 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07002126}
2127
2128/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002129 * ext4_ext_put_gap_in_cache:
2130 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07002131 * and cache this gap
2132 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002133static void
Alex Tomasa86c6182006-10-11 01:21:03 -07002134ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002135 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07002136{
2137 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002138 unsigned long len;
2139 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07002140 struct ext4_extent *ex;
2141
2142 ex = path[depth].p_ext;
2143 if (ex == NULL) {
2144 /* there is no extent yet, so gap is [0;-] */
2145 lblock = 0;
Lukas Czernerf17722f2011-06-06 00:05:17 -04002146 len = EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07002147 ext_debug("cache gap(whole file):");
2148 } else if (block < le32_to_cpu(ex->ee_block)) {
2149 lblock = block;
2150 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05002151 ext_debug("cache gap(before): %u [%u:%u]",
2152 block,
2153 le32_to_cpu(ex->ee_block),
2154 ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002155 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002156 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002157 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002158 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002159 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002160
2161 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05002162 ext_debug("cache gap(after): [%u:%u] %u",
2163 le32_to_cpu(ex->ee_block),
2164 ext4_ext_get_actual_len(ex),
2165 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002166 BUG_ON(next == lblock);
2167 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07002168 } else {
2169 lblock = len = 0;
2170 BUG();
2171 }
2172
Eric Sandeenbba90742008-01-28 23:58:27 -05002173 ext_debug(" -> %u:%lu\n", lblock, len);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002174 ext4_ext_put_in_cache(inode, lblock, len, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002175}
2176
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002177/*
Lukas Czerner63fedaf2012-09-26 21:09:06 -04002178 * ext4_ext_in_cache()
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002179 * Checks to see if the given block is in the cache.
2180 * If it is, the cached extent is stored in the given
Lukas Czerner63fedaf2012-09-26 21:09:06 -04002181 * cache extent pointer.
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002182 *
2183 * @inode: The files inode
2184 * @block: The block to look for in the cache
2185 * @ex: Pointer where the cached extent will be stored
2186 * if it contains block
2187 *
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002188 * Return 0 if cache is invalid; 1 if the cache is valid
2189 */
Lukas Czerner63fedaf2012-09-26 21:09:06 -04002190static int
2191ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
2192 struct ext4_extent *ex)
2193{
Alex Tomasa86c6182006-10-11 01:21:03 -07002194 struct ext4_ext_cache *cex;
Vivek Haldar77f41352011-05-22 21:24:16 -04002195 struct ext4_sb_info *sbi;
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002196 int ret = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002197
Theodore Ts'o60e66792010-05-17 07:00:00 -04002198 /*
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002199 * We borrow i_block_reservation_lock to protect i_cached_extent
2200 */
2201 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07002202 cex = &EXT4_I(inode)->i_cached_extent;
Vivek Haldar77f41352011-05-22 21:24:16 -04002203 sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002204
2205 /* has cache valid data? */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002206 if (cex->ec_len == 0)
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002207 goto errout;
Alex Tomasa86c6182006-10-11 01:21:03 -07002208
Akinobu Mita731eb1a2010-03-03 23:55:01 -05002209 if (in_range(block, cex->ec_block, cex->ec_len)) {
Lukas Czerner63fedaf2012-09-26 21:09:06 -04002210 ex->ee_block = cpu_to_le32(cex->ec_block);
2211 ext4_ext_store_pblock(ex, cex->ec_start);
2212 ex->ee_len = cpu_to_le16(cex->ec_len);
Eric Sandeenbba90742008-01-28 23:58:27 -05002213 ext_debug("%u cached by %u:%u:%llu\n",
2214 block,
2215 cex->ec_block, cex->ec_len, cex->ec_start);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002216 ret = 1;
Alex Tomasa86c6182006-10-11 01:21:03 -07002217 }
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002218errout:
Aditya Kalid8990242011-09-09 19:18:51 -04002219 trace_ext4_ext_in_cache(inode, block, ret);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002220 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
2221 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07002222}
2223
2224/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002225 * ext4_ext_rm_idx:
2226 * removes index from the index block.
Alex Tomasa86c6182006-10-11 01:21:03 -07002227 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002228static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07002229 struct ext4_ext_path *path)
2230{
Alex Tomasa86c6182006-10-11 01:21:03 -07002231 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002232 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002233
2234 /* free index block */
2235 path--;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002236 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002237 if (unlikely(path->p_hdr->eh_entries == 0)) {
2238 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2239 return -EIO;
2240 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002241 err = ext4_ext_get_access(handle, inode, path);
2242 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002243 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002244
2245 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2246 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2247 len *= sizeof(struct ext4_extent_idx);
2248 memmove(path->p_idx, path->p_idx + 1, len);
2249 }
2250
Marcin Slusarze8546d02008-04-17 10:38:59 -04002251 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002252 err = ext4_ext_dirty(handle, inode, path);
2253 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002254 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002255 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Aditya Kalid8990242011-09-09 19:18:51 -04002256 trace_ext4_ext_rm_idx(inode, leaf);
2257
Peter Huewe7dc57612011-02-21 21:01:42 -05002258 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002259 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Alex Tomasa86c6182006-10-11 01:21:03 -07002260 return err;
2261}
2262
2263/*
Mingming Caoee12b632008-08-19 22:16:05 -04002264 * ext4_ext_calc_credits_for_single_extent:
2265 * This routine returns max. credits that needed to insert an extent
2266 * to the extent tree.
2267 * When pass the actual path, the caller should calculate credits
2268 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002269 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002270int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002271 struct ext4_ext_path *path)
2272{
Alex Tomasa86c6182006-10-11 01:21:03 -07002273 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002274 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002275 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002276
Alex Tomasa86c6182006-10-11 01:21:03 -07002277 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002278 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002279 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2280
2281 /*
2282 * There are some space in the leaf tree, no
2283 * need to account for leaf block credit
2284 *
2285 * bitmaps and block group descriptor blocks
Tao Madf3ab172011-10-08 15:53:49 -04002286 * and other metadata blocks still need to be
Mingming Caoee12b632008-08-19 22:16:05 -04002287 * accounted.
2288 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002289 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002290 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002291 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002292 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002293 }
2294
Mingming Cao525f4ed2008-08-19 22:15:58 -04002295 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002296}
Alex Tomasa86c6182006-10-11 01:21:03 -07002297
Mingming Caoee12b632008-08-19 22:16:05 -04002298/*
2299 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2300 *
2301 * if nrblocks are fit in a single extent (chunk flag is 1), then
2302 * in the worse case, each tree level index/leaf need to be changed
2303 * if the tree split due to insert a new extent, then the old tree
2304 * index/leaf need to be updated too
2305 *
2306 * If the nrblocks are discontiguous, they could cause
2307 * the whole tree split more than once, but this is really rare.
2308 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002309int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoee12b632008-08-19 22:16:05 -04002310{
2311 int index;
2312 int depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002313
Mingming Caoee12b632008-08-19 22:16:05 -04002314 if (chunk)
2315 index = depth * 2;
2316 else
2317 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002318
Mingming Caoee12b632008-08-19 22:16:05 -04002319 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002320}
2321
2322static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002323 struct ext4_extent *ex,
2324 ext4_fsblk_t *partial_cluster,
2325 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002326{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002327 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002328 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002329 ext4_fsblk_t pblk;
Andrey Sidorov18888cf2012-09-19 14:14:53 -04002330 int flags = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002331
Alex Tomasc9de5602008-01-29 00:19:52 -05002332 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
Andrey Sidorov18888cf2012-09-19 14:14:53 -04002333 flags |= EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET;
2334 else if (ext4_should_journal_data(inode))
2335 flags |= EXT4_FREE_BLOCKS_FORGET;
2336
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002337 /*
2338 * For bigalloc file systems, we never free a partial cluster
2339 * at the beginning of the extent. Instead, we make a note
2340 * that we tried freeing the cluster, and check to see if we
2341 * need to free it on a subsequent call to ext4_remove_blocks,
2342 * or at the end of the ext4_truncate() operation.
2343 */
2344 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
2345
Aditya Kalid8990242011-09-09 19:18:51 -04002346 trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002347 /*
2348 * If we have a partial cluster, and it's different from the
2349 * cluster of the last block, we need to explicitly free the
2350 * partial cluster here.
2351 */
2352 pblk = ext4_ext_pblock(ex) + ee_len - 1;
2353 if (*partial_cluster && (EXT4_B2C(sbi, pblk) != *partial_cluster)) {
2354 ext4_free_blocks(handle, inode, NULL,
2355 EXT4_C2B(sbi, *partial_cluster),
2356 sbi->s_cluster_ratio, flags);
2357 *partial_cluster = 0;
2358 }
2359
Alex Tomasa86c6182006-10-11 01:21:03 -07002360#ifdef EXTENTS_STATS
2361 {
2362 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002363 spin_lock(&sbi->s_ext_stats_lock);
2364 sbi->s_ext_blocks += ee_len;
2365 sbi->s_ext_extents++;
2366 if (ee_len < sbi->s_ext_min)
2367 sbi->s_ext_min = ee_len;
2368 if (ee_len > sbi->s_ext_max)
2369 sbi->s_ext_max = ee_len;
2370 if (ext_depth(inode) > sbi->s_depth_max)
2371 sbi->s_depth_max = ext_depth(inode);
2372 spin_unlock(&sbi->s_ext_stats_lock);
2373 }
2374#endif
2375 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002376 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002377 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002378 ext4_lblk_t num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002379
Amit Aroraa2df2a62007-07-17 21:42:41 -04002380 num = le32_to_cpu(ex->ee_block) + ee_len - from;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002381 pblk = ext4_ext_pblock(ex) + ee_len - num;
2382 ext_debug("free last %u blocks starting %llu\n", num, pblk);
2383 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
2384 /*
2385 * If the block range to be freed didn't start at the
2386 * beginning of a cluster, and we removed the entire
2387 * extent, save the partial cluster here, since we
2388 * might need to delete if we determine that the
2389 * truncate operation has removed all of the blocks in
2390 * the cluster.
2391 */
2392 if (pblk & (sbi->s_cluster_ratio - 1) &&
2393 (ee_len == num))
2394 *partial_cluster = EXT4_B2C(sbi, pblk);
2395 else
2396 *partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002397 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002398 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002399 /* head removal */
2400 ext4_lblk_t num;
2401 ext4_fsblk_t start;
2402
2403 num = to - from;
2404 start = ext4_ext_pblock(ex);
2405
2406 ext_debug("free first %u blocks starting %llu\n", num, start);
H Hartley Sweetenee90d572011-10-18 11:01:51 -04002407 ext4_free_blocks(handle, inode, NULL, start, num, flags);
Allison Hendersond583fb82011-05-25 07:41:43 -04002408
Alex Tomasa86c6182006-10-11 01:21:03 -07002409 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002410 printk(KERN_INFO "strange request: removal(2) "
2411 "%u-%u from %u:%u\n",
2412 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002413 }
2414 return 0;
2415}
2416
Allison Hendersond583fb82011-05-25 07:41:43 -04002417
2418/*
2419 * ext4_ext_rm_leaf() Removes the extents associated with the
2420 * blocks appearing between "start" and "end", and splits the extents
2421 * if "start" and "end" appear in the same extent
2422 *
2423 * @handle: The journal handle
2424 * @inode: The files inode
2425 * @path: The path to the leaf
2426 * @start: The first block to remove
2427 * @end: The last block to remove
2428 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002429static int
2430ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002431 struct ext4_ext_path *path, ext4_fsblk_t *partial_cluster,
2432 ext4_lblk_t start, ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002433{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002434 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002435 int err = 0, correct_index = 0;
2436 int depth = ext_depth(inode), credits;
2437 struct ext4_extent_header *eh;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002438 ext4_lblk_t a, b;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002439 unsigned num;
2440 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002441 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002442 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002443 struct ext4_extent *ex;
2444
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002445 /* the header must be checked already in ext4_ext_remove_space() */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002446 ext_debug("truncate since %u in leaf to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002447 if (!path[depth].p_hdr)
2448 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2449 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002450 if (unlikely(path[depth].p_hdr == NULL)) {
2451 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2452 return -EIO;
2453 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002454 /* find where to start removing */
2455 ex = EXT_LAST_EXTENT(eh);
2456
2457 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002458 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002459
Aditya Kalid8990242011-09-09 19:18:51 -04002460 trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster);
2461
Alex Tomasa86c6182006-10-11 01:21:03 -07002462 while (ex >= EXT_FIRST_EXTENT(eh) &&
2463 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002464
2465 if (ext4_ext_is_uninitialized(ex))
2466 uninitialized = 1;
2467 else
2468 uninitialized = 0;
2469
Mingming553f9002009-09-18 13:34:55 -04002470 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2471 uninitialized, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002472 path[depth].p_ext = ex;
2473
2474 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002475 b = ex_ee_block+ex_ee_len - 1 < end ?
2476 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002477
2478 ext_debug(" border %u:%u\n", a, b);
2479
Allison Hendersond583fb82011-05-25 07:41:43 -04002480 /* If this extent is beyond the end of the hole, skip it */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002481 if (end < ex_ee_block) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002482 ex--;
2483 ex_ee_block = le32_to_cpu(ex->ee_block);
2484 ex_ee_len = ext4_ext_get_actual_len(ex);
2485 continue;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002486 } else if (b != ex_ee_block + ex_ee_len - 1) {
Lukas Czernerdc1841d2012-03-19 23:07:43 -04002487 EXT4_ERROR_INODE(inode,
2488 "can not handle truncate %u:%u "
2489 "on extent %u:%u",
2490 start, end, ex_ee_block,
2491 ex_ee_block + ex_ee_len - 1);
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002492 err = -EIO;
2493 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002494 } else if (a != ex_ee_block) {
2495 /* remove tail of the extent */
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002496 num = a - ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002497 } else {
2498 /* remove whole extent: excellent! */
Alex Tomasa86c6182006-10-11 01:21:03 -07002499 num = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002500 }
Theodore Ts'o34071da2008-08-01 21:59:19 -04002501 /*
2502 * 3 for leaf, sb, and inode plus 2 (bmap and group
2503 * descriptor) for each block group; assume two block
2504 * groups plus ex_ee_len/blocks_per_block_group for
2505 * the worst case
2506 */
2507 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002508 if (ex == EXT_FIRST_EXTENT(eh)) {
2509 correct_index = 1;
2510 credits += (ext_depth(inode)) + 1;
2511 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002512 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002513
Jan Kara487caee2009-08-17 22:17:20 -04002514 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
Shen Feng9102e4f2008-07-11 19:27:31 -04002515 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002516 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002517
2518 err = ext4_ext_get_access(handle, inode, path + depth);
2519 if (err)
2520 goto out;
2521
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002522 err = ext4_remove_blocks(handle, inode, ex, partial_cluster,
2523 a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002524 if (err)
2525 goto out;
2526
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002527 if (num == 0)
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002528 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002529 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002530
Alex Tomasa86c6182006-10-11 01:21:03 -07002531 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002532 /*
2533 * Do not mark uninitialized if all the blocks in the
2534 * extent have been removed.
2535 */
2536 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002537 ext4_ext_mark_uninitialized(ex);
Allison Hendersond583fb82011-05-25 07:41:43 -04002538 /*
2539 * If the extent was completely released,
2540 * we need to remove it from the leaf
2541 */
2542 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002543 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002544 /*
2545 * For hole punching, we need to scoot all the
2546 * extents up when an extent is removed so that
2547 * we dont have blank extents in the middle
2548 */
2549 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2550 sizeof(struct ext4_extent));
2551
2552 /* Now get rid of the one at the end */
2553 memset(EXT_LAST_EXTENT(eh), 0,
2554 sizeof(struct ext4_extent));
2555 }
2556 le16_add_cpu(&eh->eh_entries, -1);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002557 } else
2558 *partial_cluster = 0;
Allison Hendersond583fb82011-05-25 07:41:43 -04002559
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002560 err = ext4_ext_dirty(handle, inode, path + depth);
2561 if (err)
2562 goto out;
2563
Yongqiang Yangbf52c6f2011-11-01 18:59:26 -04002564 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002565 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002566 ex--;
2567 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002568 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002569 }
2570
2571 if (correct_index && eh->eh_entries)
2572 err = ext4_ext_correct_indexes(handle, inode, path);
2573
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002574 /*
2575 * If there is still a entry in the leaf node, check to see if
2576 * it references the partial cluster. This is the only place
2577 * where it could; if it doesn't, we can free the cluster.
2578 */
2579 if (*partial_cluster && ex >= EXT_FIRST_EXTENT(eh) &&
2580 (EXT4_B2C(sbi, ext4_ext_pblock(ex) + ex_ee_len - 1) !=
2581 *partial_cluster)) {
2582 int flags = EXT4_FREE_BLOCKS_FORGET;
2583
2584 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2585 flags |= EXT4_FREE_BLOCKS_METADATA;
2586
2587 ext4_free_blocks(handle, inode, NULL,
2588 EXT4_C2B(sbi, *partial_cluster),
2589 sbi->s_cluster_ratio, flags);
2590 *partial_cluster = 0;
2591 }
2592
Alex Tomasa86c6182006-10-11 01:21:03 -07002593 /* if this leaf is free, then we should
2594 * remove it from index block above */
2595 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2596 err = ext4_ext_rm_idx(handle, inode, path + depth);
2597
2598out:
2599 return err;
2600}
2601
2602/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002603 * ext4_ext_more_to_rm:
2604 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002605 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002606static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002607ext4_ext_more_to_rm(struct ext4_ext_path *path)
2608{
2609 BUG_ON(path->p_idx == NULL);
2610
2611 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2612 return 0;
2613
2614 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002615 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002616 * so we have to consider current index for truncation
2617 */
2618 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2619 return 0;
2620 return 1;
2621}
2622
Lukas Czerner5f95d212012-03-19 23:03:19 -04002623static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2624 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002625{
2626 struct super_block *sb = inode->i_sb;
2627 int depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002628 struct ext4_ext_path *path = NULL;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002629 ext4_fsblk_t partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002630 handle_t *handle;
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002631 int i = 0, err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002632
Lukas Czerner5f95d212012-03-19 23:03:19 -04002633 ext_debug("truncate since %u to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002634
2635 /* probably first extent we're gonna free will be last in block */
2636 handle = ext4_journal_start(inode, depth + 1);
2637 if (IS_ERR(handle))
2638 return PTR_ERR(handle);
2639
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002640again:
Alex Tomasa86c6182006-10-11 01:21:03 -07002641 ext4_ext_invalidate_cache(inode);
2642
Aditya Kalid8990242011-09-09 19:18:51 -04002643 trace_ext4_ext_remove_space(inode, start, depth);
2644
Alex Tomasa86c6182006-10-11 01:21:03 -07002645 /*
Lukas Czerner5f95d212012-03-19 23:03:19 -04002646 * Check if we are removing extents inside the extent tree. If that
2647 * is the case, we are going to punch a hole inside the extent tree
2648 * so we have to check whether we need to split the extent covering
2649 * the last block to remove so we can easily remove the part of it
2650 * in ext4_ext_rm_leaf().
2651 */
2652 if (end < EXT_MAX_BLOCKS - 1) {
2653 struct ext4_extent *ex;
2654 ext4_lblk_t ee_block;
2655
2656 /* find extent for this block */
2657 path = ext4_ext_find_extent(inode, end, NULL);
2658 if (IS_ERR(path)) {
2659 ext4_journal_stop(handle);
2660 return PTR_ERR(path);
2661 }
2662 depth = ext_depth(inode);
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002663 /* Leaf not may not exist only if inode has no blocks at all */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002664 ex = path[depth].p_ext;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002665 if (!ex) {
Dmitry Monakhov6f2080e2012-09-30 23:03:50 -04002666 if (depth) {
2667 EXT4_ERROR_INODE(inode,
2668 "path[%d].p_hdr == NULL",
2669 depth);
2670 err = -EIO;
2671 }
2672 goto out;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002673 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002674
2675 ee_block = le32_to_cpu(ex->ee_block);
2676
2677 /*
2678 * See if the last block is inside the extent, if so split
2679 * the extent at 'end' block so we can easily remove the
2680 * tail of the first part of the split extent in
2681 * ext4_ext_rm_leaf().
2682 */
2683 if (end >= ee_block &&
2684 end < ee_block + ext4_ext_get_actual_len(ex) - 1) {
2685 int split_flag = 0;
2686
2687 if (ext4_ext_is_uninitialized(ex))
2688 split_flag = EXT4_EXT_MARK_UNINIT1 |
2689 EXT4_EXT_MARK_UNINIT2;
2690
2691 /*
2692 * Split the extent in two so that 'end' is the last
2693 * block in the first new extent
2694 */
2695 err = ext4_split_extent_at(handle, inode, path,
2696 end + 1, split_flag,
2697 EXT4_GET_BLOCKS_PRE_IO |
2698 EXT4_GET_BLOCKS_PUNCH_OUT_EXT);
2699
2700 if (err < 0)
2701 goto out;
2702 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002703 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002704 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002705 * We start scanning from right side, freeing all the blocks
2706 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002707 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002708 depth = ext_depth(inode);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002709 if (path) {
2710 int k = i = depth;
2711 while (--k > 0)
2712 path[k].p_block =
2713 le16_to_cpu(path[k].p_hdr->eh_entries)+1;
2714 } else {
2715 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1),
2716 GFP_NOFS);
2717 if (path == NULL) {
2718 ext4_journal_stop(handle);
2719 return -ENOMEM;
2720 }
2721 path[0].p_depth = depth;
2722 path[0].p_hdr = ext_inode_hdr(inode);
Theodore Ts'o89a4e482012-08-17 08:54:52 -04002723 i = 0;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002724
Ashish Sangwan968dee72012-07-22 22:49:08 -04002725 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
2726 err = -EIO;
2727 goto out;
2728 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002729 }
Ashish Sangwan968dee72012-07-22 22:49:08 -04002730 err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002731
2732 while (i >= 0 && err == 0) {
2733 if (i == depth) {
2734 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04002735 err = ext4_ext_rm_leaf(handle, inode, path,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002736 &partial_cluster, start,
Lukas Czerner5f95d212012-03-19 23:03:19 -04002737 end);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002738 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002739 brelse(path[i].p_bh);
2740 path[i].p_bh = NULL;
2741 i--;
2742 continue;
2743 }
2744
2745 /* this is index block */
2746 if (!path[i].p_hdr) {
2747 ext_debug("initialize header\n");
2748 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002749 }
2750
Alex Tomasa86c6182006-10-11 01:21:03 -07002751 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002752 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002753 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2754 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2755 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2756 path[i].p_hdr,
2757 le16_to_cpu(path[i].p_hdr->eh_entries));
2758 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002759 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002760 path[i].p_idx--;
2761 }
2762
2763 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2764 i, EXT_FIRST_INDEX(path[i].p_hdr),
2765 path[i].p_idx);
2766 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002767 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002768 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002769 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04002770 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002771 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'obf89d162010-10-27 21:30:14 -04002772 bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002773 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002774 /* should we reset i_size? */
2775 err = -EIO;
2776 break;
2777 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002778 if (WARN_ON(i + 1 > depth)) {
2779 err = -EIO;
2780 break;
2781 }
Darrick J. Wongf8489122012-04-29 18:21:10 -04002782 if (ext4_ext_check_block(inode, ext_block_hdr(bh),
2783 depth - i - 1, bh)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002784 err = -EIO;
2785 break;
2786 }
2787 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002788
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002789 /* save actual number of indexes since this
2790 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002791 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2792 i++;
2793 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002794 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002795 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002796 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002797 * handle must be already prepared by the
2798 * truncatei_leaf() */
2799 err = ext4_ext_rm_idx(handle, inode, path + i);
2800 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002801 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002802 brelse(path[i].p_bh);
2803 path[i].p_bh = NULL;
2804 i--;
2805 ext_debug("return to level %d\n", i);
2806 }
2807 }
2808
Aditya Kalid8990242011-09-09 19:18:51 -04002809 trace_ext4_ext_remove_space_done(inode, start, depth, partial_cluster,
2810 path->p_hdr->eh_entries);
2811
Aditya Kali7b415bf2011-09-09 19:04:51 -04002812 /* If we still have something in the partial cluster and we have removed
2813 * even the first extent, then we should free the blocks in the partial
2814 * cluster as well. */
2815 if (partial_cluster && path->p_hdr->eh_entries == 0) {
2816 int flags = EXT4_FREE_BLOCKS_FORGET;
2817
2818 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2819 flags |= EXT4_FREE_BLOCKS_METADATA;
2820
2821 ext4_free_blocks(handle, inode, NULL,
2822 EXT4_C2B(EXT4_SB(sb), partial_cluster),
2823 EXT4_SB(sb)->s_cluster_ratio, flags);
2824 partial_cluster = 0;
2825 }
2826
Alex Tomasa86c6182006-10-11 01:21:03 -07002827 /* TODO: flexible tree reduction should be here */
2828 if (path->p_hdr->eh_entries == 0) {
2829 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002830 * truncate to zero freed all the tree,
2831 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002832 */
2833 err = ext4_ext_get_access(handle, inode, path);
2834 if (err == 0) {
2835 ext_inode_hdr(inode)->eh_depth = 0;
2836 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04002837 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07002838 err = ext4_ext_dirty(handle, inode, path);
2839 }
2840 }
2841out:
Alex Tomasa86c6182006-10-11 01:21:03 -07002842 ext4_ext_drop_refs(path);
2843 kfree(path);
Ashish Sangwan968dee72012-07-22 22:49:08 -04002844 if (err == -EAGAIN) {
2845 path = NULL;
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002846 goto again;
Ashish Sangwan968dee72012-07-22 22:49:08 -04002847 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002848 ext4_journal_stop(handle);
2849
2850 return err;
2851}
2852
2853/*
2854 * called at mount time
2855 */
2856void ext4_ext_init(struct super_block *sb)
2857{
2858 /*
2859 * possible initialization would be here
2860 */
2861
Theodore Ts'o83982b62009-01-06 14:53:16 -05002862 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04002863#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o92b97812012-03-19 23:41:49 -04002864 printk(KERN_INFO "EXT4-fs: file extents enabled"
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002865#ifdef AGGRESSIVE_TEST
Theodore Ts'o92b97812012-03-19 23:41:49 -04002866 ", aggressive tests"
Alex Tomasa86c6182006-10-11 01:21:03 -07002867#endif
2868#ifdef CHECK_BINSEARCH
Theodore Ts'o92b97812012-03-19 23:41:49 -04002869 ", check binsearch"
Alex Tomasa86c6182006-10-11 01:21:03 -07002870#endif
2871#ifdef EXTENTS_STATS
Theodore Ts'o92b97812012-03-19 23:41:49 -04002872 ", stats"
Alex Tomasa86c6182006-10-11 01:21:03 -07002873#endif
Theodore Ts'o92b97812012-03-19 23:41:49 -04002874 "\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04002875#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07002876#ifdef EXTENTS_STATS
2877 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2878 EXT4_SB(sb)->s_ext_min = 1 << 30;
2879 EXT4_SB(sb)->s_ext_max = 0;
2880#endif
2881 }
2882}
2883
2884/*
2885 * called at umount time
2886 */
2887void ext4_ext_release(struct super_block *sb)
2888{
Theodore Ts'o83982b62009-01-06 14:53:16 -05002889 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
Alex Tomasa86c6182006-10-11 01:21:03 -07002890 return;
2891
2892#ifdef EXTENTS_STATS
2893 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2894 struct ext4_sb_info *sbi = EXT4_SB(sb);
2895 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2896 sbi->s_ext_blocks, sbi->s_ext_extents,
2897 sbi->s_ext_blocks / sbi->s_ext_extents);
2898 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2899 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2900 }
2901#endif
2902}
2903
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002904/* FIXME!! we need to try to merge to left or right after zero-out */
2905static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2906{
Lukas Czerner24075182010-10-27 21:30:06 -04002907 ext4_fsblk_t ee_pblock;
2908 unsigned int ee_len;
Jing Zhangb7203032010-05-12 00:00:00 -04002909 int ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002910
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002911 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04002912 ee_pblock = ext4_ext_pblock(ex);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002913
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04002914 ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
Lukas Czerner24075182010-10-27 21:30:06 -04002915 if (ret > 0)
2916 ret = 0;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002917
Lukas Czerner24075182010-10-27 21:30:06 -04002918 return ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002919}
2920
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002921/*
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002922 * ext4_split_extent_at() splits an extent at given block.
2923 *
2924 * @handle: the journal handle
2925 * @inode: the file inode
2926 * @path: the path to the extent
2927 * @split: the logical block where the extent is splitted.
2928 * @split_flags: indicates if the extent could be zeroout if split fails, and
2929 * the states(init or uninit) of new extents.
2930 * @flags: flags used to insert new extent to extent tree.
2931 *
2932 *
2933 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
2934 * of which are deterimined by split_flag.
2935 *
2936 * There are two cases:
2937 * a> the extent are splitted into two extent.
2938 * b> split is not needed, and just mark the extent.
2939 *
2940 * return 0 on success.
2941 */
2942static int ext4_split_extent_at(handle_t *handle,
2943 struct inode *inode,
2944 struct ext4_ext_path *path,
2945 ext4_lblk_t split,
2946 int split_flag,
2947 int flags)
2948{
2949 ext4_fsblk_t newblock;
2950 ext4_lblk_t ee_block;
2951 struct ext4_extent *ex, newex, orig_ex;
2952 struct ext4_extent *ex2 = NULL;
2953 unsigned int ee_len, depth;
2954 int err = 0;
2955
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04002956 BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) ==
2957 (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2));
2958
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002959 ext_debug("ext4_split_extents_at: inode %lu, logical"
2960 "block %llu\n", inode->i_ino, (unsigned long long)split);
2961
2962 ext4_ext_show_leaf(inode, path);
2963
2964 depth = ext_depth(inode);
2965 ex = path[depth].p_ext;
2966 ee_block = le32_to_cpu(ex->ee_block);
2967 ee_len = ext4_ext_get_actual_len(ex);
2968 newblock = split - ee_block + ext4_ext_pblock(ex);
2969
2970 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
2971
2972 err = ext4_ext_get_access(handle, inode, path + depth);
2973 if (err)
2974 goto out;
2975
2976 if (split == ee_block) {
2977 /*
2978 * case b: block @split is the block that the extent begins with
2979 * then we just change the state of the extent, and splitting
2980 * is not needed.
2981 */
2982 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2983 ext4_ext_mark_uninitialized(ex);
2984 else
2985 ext4_ext_mark_initialized(ex);
2986
2987 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002988 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002989
Theodore Ts'oecb94f52012-08-17 09:44:17 -04002990 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002991 goto out;
2992 }
2993
2994 /* case a */
2995 memcpy(&orig_ex, ex, sizeof(orig_ex));
2996 ex->ee_len = cpu_to_le16(split - ee_block);
2997 if (split_flag & EXT4_EXT_MARK_UNINIT1)
2998 ext4_ext_mark_uninitialized(ex);
2999
3000 /*
3001 * path may lead to new leaf, not to original leaf any more
3002 * after ext4_ext_insert_extent() returns,
3003 */
3004 err = ext4_ext_dirty(handle, inode, path + depth);
3005 if (err)
3006 goto fix_extent_len;
3007
3008 ex2 = &newex;
3009 ex2->ee_block = cpu_to_le32(split);
3010 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
3011 ext4_ext_store_pblock(ex2, newblock);
3012 if (split_flag & EXT4_EXT_MARK_UNINIT2)
3013 ext4_ext_mark_uninitialized(ex2);
3014
3015 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
3016 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003017 if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) {
3018 if (split_flag & EXT4_EXT_DATA_VALID1)
3019 err = ext4_ext_zeroout(inode, ex2);
3020 else
3021 err = ext4_ext_zeroout(inode, ex);
3022 } else
3023 err = ext4_ext_zeroout(inode, &orig_ex);
3024
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003025 if (err)
3026 goto fix_extent_len;
3027 /* update the extent length and mark as initialized */
Al Viroaf1584f2012-04-12 20:32:25 -04003028 ex->ee_len = cpu_to_le16(ee_len);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003029 ext4_ext_try_to_merge(handle, inode, path, ex);
3030 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003031 goto out;
3032 } else if (err)
3033 goto fix_extent_len;
3034
3035out:
3036 ext4_ext_show_leaf(inode, path);
3037 return err;
3038
3039fix_extent_len:
3040 ex->ee_len = orig_ex.ee_len;
3041 ext4_ext_dirty(handle, inode, path + depth);
3042 return err;
3043}
3044
3045/*
3046 * ext4_split_extents() splits an extent and mark extent which is covered
3047 * by @map as split_flags indicates
3048 *
3049 * It may result in splitting the extent into multiple extents (upto three)
3050 * There are three possibilities:
3051 * a> There is no split required
3052 * b> Splits in two extents: Split is happening at either end of the extent
3053 * c> Splits in three extents: Somone is splitting in middle of the extent
3054 *
3055 */
3056static int ext4_split_extent(handle_t *handle,
3057 struct inode *inode,
3058 struct ext4_ext_path *path,
3059 struct ext4_map_blocks *map,
3060 int split_flag,
3061 int flags)
3062{
3063 ext4_lblk_t ee_block;
3064 struct ext4_extent *ex;
3065 unsigned int ee_len, depth;
3066 int err = 0;
3067 int uninitialized;
3068 int split_flag1, flags1;
3069
3070 depth = ext_depth(inode);
3071 ex = path[depth].p_ext;
3072 ee_block = le32_to_cpu(ex->ee_block);
3073 ee_len = ext4_ext_get_actual_len(ex);
3074 uninitialized = ext4_ext_is_uninitialized(ex);
3075
3076 if (map->m_lblk + map->m_len < ee_block + ee_len) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003077 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003078 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
3079 if (uninitialized)
3080 split_flag1 |= EXT4_EXT_MARK_UNINIT1 |
3081 EXT4_EXT_MARK_UNINIT2;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003082 if (split_flag & EXT4_EXT_DATA_VALID2)
3083 split_flag1 |= EXT4_EXT_DATA_VALID1;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003084 err = ext4_split_extent_at(handle, inode, path,
3085 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04003086 if (err)
3087 goto out;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003088 }
3089
3090 ext4_ext_drop_refs(path);
3091 path = ext4_ext_find_extent(inode, map->m_lblk, path);
3092 if (IS_ERR(path))
3093 return PTR_ERR(path);
3094
3095 if (map->m_lblk >= ee_block) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003096 split_flag1 = split_flag & (EXT4_EXT_MAY_ZEROOUT |
3097 EXT4_EXT_DATA_VALID2);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003098 if (uninitialized)
3099 split_flag1 |= EXT4_EXT_MARK_UNINIT1;
3100 if (split_flag & EXT4_EXT_MARK_UNINIT2)
3101 split_flag1 |= EXT4_EXT_MARK_UNINIT2;
3102 err = ext4_split_extent_at(handle, inode, path,
3103 map->m_lblk, split_flag1, flags);
3104 if (err)
3105 goto out;
3106 }
3107
3108 ext4_ext_show_leaf(inode, path);
3109out:
3110 return err ? err : map->m_len;
3111}
3112
Amit Arora56055d32007-07-17 21:42:38 -04003113/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003114 * This function is called by ext4_ext_map_blocks() if someone tries to write
Amit Arora56055d32007-07-17 21:42:38 -04003115 * to an uninitialized extent. It may result in splitting the uninitialized
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003116 * extent into multiple extents (up to three - one initialized and two
Amit Arora56055d32007-07-17 21:42:38 -04003117 * uninitialized).
3118 * There are three possibilities:
3119 * a> There is no split required: Entire extent should be initialized
3120 * b> Splits in two extents: Write is happening at either end of the extent
3121 * c> Splits in three extents: Somone is writing in middle of the extent
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003122 *
3123 * Pre-conditions:
3124 * - The extent pointed to by 'path' is uninitialized.
3125 * - The extent pointed to by 'path' contains a superset
3126 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
3127 *
3128 * Post-conditions on success:
3129 * - the returned value is the number of blocks beyond map->l_lblk
3130 * that are allocated and initialized.
3131 * It is guaranteed to be >= map->m_len.
Amit Arora56055d32007-07-17 21:42:38 -04003132 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003133static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003134 struct inode *inode,
3135 struct ext4_map_blocks *map,
3136 struct ext4_ext_path *path)
Amit Arora56055d32007-07-17 21:42:38 -04003137{
Zheng Liu67a5da52012-08-17 09:54:17 -04003138 struct ext4_sb_info *sbi;
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003139 struct ext4_extent_header *eh;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003140 struct ext4_map_blocks split_map;
3141 struct ext4_extent zero_ex;
3142 struct ext4_extent *ex;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003143 ext4_lblk_t ee_block, eof_block;
Dan Carpenterf85b2872011-10-26 03:42:36 -04003144 unsigned int ee_len, depth;
Zheng Liu67a5da52012-08-17 09:54:17 -04003145 int allocated, max_zeroout = 0;
Amit Arora56055d32007-07-17 21:42:38 -04003146 int err = 0;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003147 int split_flag = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003148
3149 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3150 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003151 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003152
Zheng Liu67a5da52012-08-17 09:54:17 -04003153 sbi = EXT4_SB(inode->i_sb);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003154 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3155 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003156 if (eof_block < map->m_lblk + map->m_len)
3157 eof_block = map->m_lblk + map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003158
3159 depth = ext_depth(inode);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003160 eh = path[depth].p_hdr;
Amit Arora56055d32007-07-17 21:42:38 -04003161 ex = path[depth].p_ext;
3162 ee_block = le32_to_cpu(ex->ee_block);
3163 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003164 allocated = ee_len - (map->m_lblk - ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003165
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003166 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
3167
3168 /* Pre-conditions */
3169 BUG_ON(!ext4_ext_is_uninitialized(ex));
3170 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003171
3172 /*
3173 * Attempt to transfer newly initialized blocks from the currently
3174 * uninitialized extent to its left neighbor. This is much cheaper
3175 * than an insertion followed by a merge as those involve costly
3176 * memmove() calls. This is the common case in steady state for
3177 * workloads doing fallocate(FALLOC_FL_KEEP_SIZE) followed by append
3178 * writes.
3179 *
3180 * Limitations of the current logic:
3181 * - L1: we only deal with writes at the start of the extent.
3182 * The approach could be extended to writes at the end
3183 * of the extent but this scenario was deemed less common.
3184 * - L2: we do not deal with writes covering the whole extent.
3185 * This would require removing the extent if the transfer
3186 * is possible.
3187 * - L3: we only attempt to merge with an extent stored in the
3188 * same extent tree node.
3189 */
3190 if ((map->m_lblk == ee_block) && /*L1*/
3191 (map->m_len < ee_len) && /*L2*/
3192 (ex > EXT_FIRST_EXTENT(eh))) { /*L3*/
3193 struct ext4_extent *prev_ex;
3194 ext4_lblk_t prev_lblk;
3195 ext4_fsblk_t prev_pblk, ee_pblk;
3196 unsigned int prev_len, write_len;
3197
3198 prev_ex = ex - 1;
3199 prev_lblk = le32_to_cpu(prev_ex->ee_block);
3200 prev_len = ext4_ext_get_actual_len(prev_ex);
3201 prev_pblk = ext4_ext_pblock(prev_ex);
3202 ee_pblk = ext4_ext_pblock(ex);
3203 write_len = map->m_len;
3204
3205 /*
3206 * A transfer of blocks from 'ex' to 'prev_ex' is allowed
3207 * upon those conditions:
3208 * - C1: prev_ex is initialized,
3209 * - C2: prev_ex is logically abutting ex,
3210 * - C3: prev_ex is physically abutting ex,
3211 * - C4: prev_ex can receive the additional blocks without
3212 * overflowing the (initialized) length limit.
3213 */
3214 if ((!ext4_ext_is_uninitialized(prev_ex)) && /*C1*/
3215 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3216 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
3217 (prev_len < (EXT_INIT_MAX_LEN - write_len))) { /*C4*/
3218 err = ext4_ext_get_access(handle, inode, path + depth);
3219 if (err)
3220 goto out;
3221
3222 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3223 map, ex, prev_ex);
3224
3225 /* Shift the start of ex by 'write_len' blocks */
3226 ex->ee_block = cpu_to_le32(ee_block + write_len);
3227 ext4_ext_store_pblock(ex, ee_pblk + write_len);
3228 ex->ee_len = cpu_to_le16(ee_len - write_len);
3229 ext4_ext_mark_uninitialized(ex); /* Restore the flag */
3230
3231 /* Extend prev_ex by 'write_len' blocks */
3232 prev_ex->ee_len = cpu_to_le16(prev_len + write_len);
3233
3234 /* Mark the block containing both extents as dirty */
3235 ext4_ext_dirty(handle, inode, path + depth);
3236
3237 /* Update path to point to the right extent */
3238 path[depth].p_ext = prev_ex;
3239
3240 /* Result: number of initialized blocks past m_lblk */
3241 allocated = write_len;
3242 goto out;
3243 }
3244 }
3245
Yongqiang Yang667eff32011-05-03 12:25:07 -04003246 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003247 /*
3248 * It is safe to convert extent to initialized via explicit
3249 * zeroout only if extent is fully insde i_size or new_size.
3250 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003251 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003252
Zheng Liu67a5da52012-08-17 09:54:17 -04003253 if (EXT4_EXT_MAY_ZEROOUT & split_flag)
3254 max_zeroout = sbi->s_extent_max_zeroout_kb >>
3255 inode->i_sb->s_blocksize_bits;
3256
3257 /* If extent is less than s_max_zeroout_kb, zeroout directly */
3258 if (max_zeroout && (ee_len <= max_zeroout)) {
Yongqiang Yang667eff32011-05-03 12:25:07 -04003259 err = ext4_ext_zeroout(inode, ex);
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003260 if (err)
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003261 goto out;
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003262
3263 err = ext4_ext_get_access(handle, inode, path + depth);
3264 if (err)
3265 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003266 ext4_ext_mark_initialized(ex);
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003267 ext4_ext_try_to_merge(handle, inode, path, ex);
3268 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003269 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04003270 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003271
Amit Arora56055d32007-07-17 21:42:38 -04003272 /*
Yongqiang Yang667eff32011-05-03 12:25:07 -04003273 * four cases:
3274 * 1. split the extent into three extents.
3275 * 2. split the extent into two extents, zeroout the first half.
3276 * 3. split the extent into two extents, zeroout the second half.
3277 * 4. split the extent into two extents with out zeroout.
Amit Arora56055d32007-07-17 21:42:38 -04003278 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003279 split_map.m_lblk = map->m_lblk;
3280 split_map.m_len = map->m_len;
3281
Zheng Liu67a5da52012-08-17 09:54:17 -04003282 if (max_zeroout && (allocated > map->m_len)) {
3283 if (allocated <= max_zeroout) {
Yongqiang Yang667eff32011-05-03 12:25:07 -04003284 /* case 3 */
3285 zero_ex.ee_block =
Allison Henderson9b940f82011-05-16 10:11:09 -04003286 cpu_to_le32(map->m_lblk);
3287 zero_ex.ee_len = cpu_to_le16(allocated);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003288 ext4_ext_store_pblock(&zero_ex,
3289 ext4_ext_pblock(ex) + map->m_lblk - ee_block);
3290 err = ext4_ext_zeroout(inode, &zero_ex);
Amit Arora56055d32007-07-17 21:42:38 -04003291 if (err)
3292 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003293 split_map.m_lblk = map->m_lblk;
3294 split_map.m_len = allocated;
Zheng Liu67a5da52012-08-17 09:54:17 -04003295 } else if (map->m_lblk - ee_block + map->m_len < max_zeroout) {
Yongqiang Yang667eff32011-05-03 12:25:07 -04003296 /* case 2 */
3297 if (map->m_lblk != ee_block) {
3298 zero_ex.ee_block = ex->ee_block;
3299 zero_ex.ee_len = cpu_to_le16(map->m_lblk -
3300 ee_block);
3301 ext4_ext_store_pblock(&zero_ex,
3302 ext4_ext_pblock(ex));
3303 err = ext4_ext_zeroout(inode, &zero_ex);
3304 if (err)
3305 goto out;
3306 }
3307
Yongqiang Yang667eff32011-05-03 12:25:07 -04003308 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003309 split_map.m_len = map->m_lblk - ee_block + map->m_len;
3310 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003311 }
3312 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003313
3314 allocated = ext4_split_extent(handle, inode, path,
Zheng Liu67a5da52012-08-17 09:54:17 -04003315 &split_map, split_flag, 0);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003316 if (allocated < 0)
3317 err = allocated;
3318
Amit Arora56055d32007-07-17 21:42:38 -04003319out:
3320 return err ? err : allocated;
3321}
3322
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003323/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003324 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003325 * ext4_get_blocks_dio_write() when DIO to write
3326 * to an uninitialized extent.
3327 *
Paul Bollefd018fe2011-02-15 00:05:43 +01003328 * Writing to an uninitialized extent may result in splitting the uninitialized
Wang Sheng-Hui30cb27d2012-08-18 22:38:07 -04003329 * extent into multiple initialized/uninitialized extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003330 * There are three possibilities:
3331 * a> There is no split required: Entire extent should be uninitialized
3332 * b> Splits in two extents: Write is happening at either end of the extent
3333 * c> Splits in three extents: Somone is writing in middle of the extent
3334 *
3335 * One of more index blocks maybe needed if the extent tree grow after
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003336 * the uninitialized extent split. To prevent ENOSPC occur at the IO
Mingming Cao00314622009-09-28 15:49:08 -04003337 * complete, we need to split the uninitialized extent before DIO submit
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02003338 * the IO. The uninitialized extent called at this time will be split
Mingming Cao00314622009-09-28 15:49:08 -04003339 * into three uninitialized extent(at most). After IO complete, the part
3340 * being filled will be convert to initialized by the end_io callback function
3341 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003342 *
3343 * Returns the size of uninitialized extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003344 */
3345static int ext4_split_unwritten_extents(handle_t *handle,
3346 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003347 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003348 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04003349 int flags)
3350{
Yongqiang Yang667eff32011-05-03 12:25:07 -04003351 ext4_lblk_t eof_block;
3352 ext4_lblk_t ee_block;
3353 struct ext4_extent *ex;
3354 unsigned int ee_len;
3355 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003356
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003357 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
3358 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003359 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003360
3361 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3362 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003363 if (eof_block < map->m_lblk + map->m_len)
3364 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003365 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003366 * It is safe to convert extent to initialized via explicit
3367 * zeroout only if extent is fully insde i_size or new_size.
3368 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003369 depth = ext_depth(inode);
3370 ex = path[depth].p_ext;
3371 ee_block = le32_to_cpu(ex->ee_block);
3372 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003373
Yongqiang Yang667eff32011-05-03 12:25:07 -04003374 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
3375 split_flag |= EXT4_EXT_MARK_UNINIT2;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003376 if (flags & EXT4_GET_BLOCKS_CONVERT)
3377 split_flag |= EXT4_EXT_DATA_VALID2;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003378 flags |= EXT4_GET_BLOCKS_PRE_IO;
3379 return ext4_split_extent(handle, inode, path, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003380}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003381
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003382static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003383 struct inode *inode,
3384 struct ext4_map_blocks *map,
3385 struct ext4_ext_path *path)
Mingming Cao00314622009-09-28 15:49:08 -04003386{
3387 struct ext4_extent *ex;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003388 ext4_lblk_t ee_block;
3389 unsigned int ee_len;
Mingming Cao00314622009-09-28 15:49:08 -04003390 int depth;
3391 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003392
3393 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003394 ex = path[depth].p_ext;
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003395 ee_block = le32_to_cpu(ex->ee_block);
3396 ee_len = ext4_ext_get_actual_len(ex);
Mingming Cao00314622009-09-28 15:49:08 -04003397
Yongqiang Yang197217a2011-05-03 11:45:29 -04003398 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3399 "block %llu, max_blocks %u\n", inode->i_ino,
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003400 (unsigned long long)ee_block, ee_len);
3401
3402 /* If extent is larger than requested then split is required */
3403 if (ee_block != map->m_lblk || ee_len > map->m_len) {
3404 err = ext4_split_unwritten_extents(handle, inode, map, path,
3405 EXT4_GET_BLOCKS_CONVERT);
3406 if (err < 0)
3407 goto out;
3408 ext4_ext_drop_refs(path);
3409 path = ext4_ext_find_extent(inode, map->m_lblk, path);
3410 if (IS_ERR(path)) {
3411 err = PTR_ERR(path);
3412 goto out;
3413 }
3414 depth = ext_depth(inode);
3415 ex = path[depth].p_ext;
3416 }
Yongqiang Yang197217a2011-05-03 11:45:29 -04003417
Mingming Cao00314622009-09-28 15:49:08 -04003418 err = ext4_ext_get_access(handle, inode, path + depth);
3419 if (err)
3420 goto out;
3421 /* first mark the extent as initialized */
3422 ext4_ext_mark_initialized(ex);
3423
Yongqiang Yang197217a2011-05-03 11:45:29 -04003424 /* note: ext4_ext_correct_indexes() isn't needed here because
3425 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003426 */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003427 ext4_ext_try_to_merge(handle, inode, path, ex);
Yongqiang Yang197217a2011-05-03 11:45:29 -04003428
Mingming Cao00314622009-09-28 15:49:08 -04003429 /* Mark modified extent as dirty */
Theodore Ts'oecb94f52012-08-17 09:44:17 -04003430 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
Mingming Cao00314622009-09-28 15:49:08 -04003431out:
3432 ext4_ext_show_leaf(inode, path);
3433 return err;
3434}
3435
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003436static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3437 sector_t block, int count)
3438{
3439 int i;
3440 for (i = 0; i < count; i++)
3441 unmap_underlying_metadata(bdev, block + i);
3442}
3443
Theodore Ts'o58590b02010-10-27 21:23:12 -04003444/*
3445 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3446 */
3447static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
Eric Sandeend002ebf2011-01-10 13:03:35 -05003448 ext4_lblk_t lblk,
Theodore Ts'o58590b02010-10-27 21:23:12 -04003449 struct ext4_ext_path *path,
3450 unsigned int len)
3451{
3452 int i, depth;
3453 struct ext4_extent_header *eh;
Sergey Senozhatsky65922cb2011-03-23 14:08:27 -04003454 struct ext4_extent *last_ex;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003455
3456 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3457 return 0;
3458
3459 depth = ext_depth(inode);
3460 eh = path[depth].p_hdr;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003461
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003462 /*
3463 * We're going to remove EOFBLOCKS_FL entirely in future so we
3464 * do not care for this case anymore. Simply remove the flag
3465 * if there are no extents.
3466 */
3467 if (unlikely(!eh->eh_entries))
3468 goto out;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003469 last_ex = EXT_LAST_EXTENT(eh);
3470 /*
3471 * We should clear the EOFBLOCKS_FL flag if we are writing the
3472 * last block in the last extent in the file. We test this by
3473 * first checking to see if the caller to
3474 * ext4_ext_get_blocks() was interested in the last block (or
3475 * a block beyond the last block) in the current extent. If
3476 * this turns out to be false, we can bail out from this
3477 * function immediately.
3478 */
Eric Sandeend002ebf2011-01-10 13:03:35 -05003479 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
Theodore Ts'o58590b02010-10-27 21:23:12 -04003480 ext4_ext_get_actual_len(last_ex))
3481 return 0;
3482 /*
3483 * If the caller does appear to be planning to write at or
3484 * beyond the end of the current extent, we then test to see
3485 * if the current extent is the last extent in the file, by
3486 * checking to make sure it was reached via the rightmost node
3487 * at each level of the tree.
3488 */
3489 for (i = depth-1; i >= 0; i--)
3490 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3491 return 0;
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003492out:
Theodore Ts'o58590b02010-10-27 21:23:12 -04003493 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3494 return ext4_mark_inode_dirty(handle, inode);
3495}
3496
Aditya Kali7b415bf2011-09-09 19:04:51 -04003497/**
3498 * ext4_find_delalloc_range: find delayed allocated block in the given range.
3499 *
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003500 * Return 1 if there is a delalloc block in the range, otherwise 0.
Aditya Kali7b415bf2011-09-09 19:04:51 -04003501 */
3502static int ext4_find_delalloc_range(struct inode *inode,
3503 ext4_lblk_t lblk_start,
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003504 ext4_lblk_t lblk_end)
Aditya Kali7b415bf2011-09-09 19:04:51 -04003505{
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003506 struct extent_status es;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003507
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003508 es.start = lblk_start;
3509 ext4_es_find_extent(inode, &es);
3510 if (es.len == 0)
3511 return 0; /* there is no delay extent in this tree */
3512 else if (es.start <= lblk_start && lblk_start < es.start + es.len)
3513 return 1;
3514 else if (lblk_start <= es.start && es.start <= lblk_end)
3515 return 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003516 else
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003517 return 0;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003518}
3519
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003520int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk)
Aditya Kali7b415bf2011-09-09 19:04:51 -04003521{
3522 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3523 ext4_lblk_t lblk_start, lblk_end;
3524 lblk_start = lblk & (~(sbi->s_cluster_ratio - 1));
3525 lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
3526
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003527 return ext4_find_delalloc_range(inode, lblk_start, lblk_end);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003528}
3529
3530/**
3531 * Determines how many complete clusters (out of those specified by the 'map')
3532 * are under delalloc and were reserved quota for.
3533 * This function is called when we are writing out the blocks that were
3534 * originally written with their allocation delayed, but then the space was
3535 * allocated using fallocate() before the delayed allocation could be resolved.
3536 * The cases to look for are:
3537 * ('=' indicated delayed allocated blocks
3538 * '-' indicates non-delayed allocated blocks)
3539 * (a) partial clusters towards beginning and/or end outside of allocated range
3540 * are not delalloc'ed.
3541 * Ex:
3542 * |----c---=|====c====|====c====|===-c----|
3543 * |++++++ allocated ++++++|
3544 * ==> 4 complete clusters in above example
3545 *
3546 * (b) partial cluster (outside of allocated range) towards either end is
3547 * marked for delayed allocation. In this case, we will exclude that
3548 * cluster.
3549 * Ex:
3550 * |----====c========|========c========|
3551 * |++++++ allocated ++++++|
3552 * ==> 1 complete clusters in above example
3553 *
3554 * Ex:
3555 * |================c================|
3556 * |++++++ allocated ++++++|
3557 * ==> 0 complete clusters in above example
3558 *
3559 * The ext4_da_update_reserve_space will be called only if we
3560 * determine here that there were some "entire" clusters that span
3561 * this 'allocated' range.
3562 * In the non-bigalloc case, this function will just end up returning num_blks
3563 * without ever calling ext4_find_delalloc_range.
3564 */
3565static unsigned int
3566get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start,
3567 unsigned int num_blks)
3568{
3569 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3570 ext4_lblk_t alloc_cluster_start, alloc_cluster_end;
3571 ext4_lblk_t lblk_from, lblk_to, c_offset;
3572 unsigned int allocated_clusters = 0;
3573
3574 alloc_cluster_start = EXT4_B2C(sbi, lblk_start);
3575 alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1);
3576
3577 /* max possible clusters for this allocation */
3578 allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1;
3579
Aditya Kalid8990242011-09-09 19:18:51 -04003580 trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks);
3581
Aditya Kali7b415bf2011-09-09 19:04:51 -04003582 /* Check towards left side */
3583 c_offset = lblk_start & (sbi->s_cluster_ratio - 1);
3584 if (c_offset) {
3585 lblk_from = lblk_start & (~(sbi->s_cluster_ratio - 1));
3586 lblk_to = lblk_from + c_offset - 1;
3587
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003588 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to))
Aditya Kali7b415bf2011-09-09 19:04:51 -04003589 allocated_clusters--;
3590 }
3591
3592 /* Now check towards right. */
3593 c_offset = (lblk_start + num_blks) & (sbi->s_cluster_ratio - 1);
3594 if (allocated_clusters && c_offset) {
3595 lblk_from = lblk_start + num_blks;
3596 lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1;
3597
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003598 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to))
Aditya Kali7b415bf2011-09-09 19:04:51 -04003599 allocated_clusters--;
3600 }
3601
3602 return allocated_clusters;
3603}
3604
Mingming Cao00314622009-09-28 15:49:08 -04003605static int
3606ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003607 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003608 struct ext4_ext_path *path, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003609 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003610{
3611 int ret = 0;
3612 int err = 0;
Dmitry Monakhovf45ee3a2012-09-28 23:21:09 -04003613 ext4_io_end_t *io = ext4_inode_aio(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003614
Zheng Liu88635ca2011-12-28 19:00:25 -05003615 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical "
3616 "block %llu, max_blocks %u, flags %x, allocated %u\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003617 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04003618 flags, allocated);
3619 ext4_ext_show_leaf(inode, path);
3620
Zheng Liub5645532012-11-08 14:33:43 -05003621 trace_ext4_ext_handle_uninitialized_extents(inode, map, flags,
3622 allocated, newblock);
Aditya Kalid8990242011-09-09 19:18:51 -04003623
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003624 /* get_block() before submit the IO, split the extent */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003625 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003626 ret = ext4_split_unwritten_extents(handle, inode, map,
3627 path, flags);
Dmitry Monakhov82e54222012-09-28 23:36:25 -04003628 if (ret <= 0)
3629 goto out;
Mingming5f524952009-11-10 10:48:04 -05003630 /*
3631 * Flag the inode(non aio case) or end_io struct (aio case)
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003632 * that this IO needs to conversion to written when IO is
Mingming5f524952009-11-10 10:48:04 -05003633 * completed
3634 */
Tao Ma0edeb712011-10-31 17:30:44 -04003635 if (io)
3636 ext4_set_io_unwritten_flag(inode, io);
3637 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003638 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
Jiaying Zhang744692d2010-03-04 16:14:02 -05003639 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003640 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao00314622009-09-28 15:49:08 -04003641 goto out;
3642 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003643 /* IO end_io complete, convert the filled extent to written */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003644 if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
Dmitry Monakhovdee1f972012-10-10 01:04:58 -04003645 ret = ext4_convert_unwritten_extents_endio(handle, inode, map,
Mingming Cao00314622009-09-28 15:49:08 -04003646 path);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003647 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05003648 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05003649 err = check_eofblocks_fl(handle, inode, map->m_lblk,
3650 path, map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003651 } else
3652 err = ret;
Mingming Cao00314622009-09-28 15:49:08 -04003653 goto out2;
3654 }
3655 /* buffered IO case */
3656 /*
3657 * repeat fallocate creation request
3658 * we already have an unwritten extent
3659 */
3660 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3661 goto map_out;
3662
3663 /* buffered READ or buffered write_begin() lookup */
3664 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3665 /*
3666 * We have blocks reserved already. We
3667 * return allocated blocks so that delalloc
3668 * won't do block reservation for us. But
3669 * the buffer head will be unmapped so that
3670 * a read from the block returns 0s.
3671 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003672 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003673 goto out1;
3674 }
3675
3676 /* buffered write, writepage time, convert*/
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003677 ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04003678 if (ret >= 0)
Jan Karab436b9b2009-12-08 23:51:10 -05003679 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04003680out:
3681 if (ret <= 0) {
3682 err = ret;
3683 goto out2;
3684 } else
3685 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003686 map->m_flags |= EXT4_MAP_NEW;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003687 /*
3688 * if we allocated more blocks than requested
3689 * we need to make sure we unmap the extra block
3690 * allocated. The actual needed block will get
3691 * unmapped later when we find the buffer_head marked
3692 * new.
3693 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003694 if (allocated > map->m_len) {
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003695 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003696 newblock + map->m_len,
3697 allocated - map->m_len);
3698 allocated = map->m_len;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003699 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003700
3701 /*
3702 * If we have done fallocate with the offset that is already
3703 * delayed allocated, we would have block reservation
3704 * and quota reservation done in the delayed write path.
3705 * But fallocate would have already updated quota and block
3706 * count for this offset. So cancel these reservation
3707 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003708 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
3709 unsigned int reserved_clusters;
3710 reserved_clusters = get_reserved_cluster_alloc(inode,
3711 map->m_lblk, map->m_len);
3712 if (reserved_clusters)
3713 ext4_da_update_reserve_space(inode,
3714 reserved_clusters,
3715 0);
3716 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003717
Mingming Cao00314622009-09-28 15:49:08 -04003718map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003719 map->m_flags |= EXT4_MAP_MAPPED;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04003720 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
3721 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
3722 map->m_len);
3723 if (err < 0)
3724 goto out2;
3725 }
Mingming Cao00314622009-09-28 15:49:08 -04003726out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003727 if (allocated > map->m_len)
3728 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003729 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003730 map->m_pblk = newblock;
3731 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04003732out2:
3733 if (path) {
3734 ext4_ext_drop_refs(path);
3735 kfree(path);
3736 }
3737 return err ? err : allocated;
3738}
Theodore Ts'o58590b02010-10-27 21:23:12 -04003739
Mingming Cao00314622009-09-28 15:49:08 -04003740/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003741 * get_implied_cluster_alloc - check to see if the requested
3742 * allocation (in the map structure) overlaps with a cluster already
3743 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04003744 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003745 * @map The requested lblk->pblk mapping
3746 * @ex The extent structure which might contain an implied
3747 * cluster allocation
3748 *
3749 * This function is called by ext4_ext_map_blocks() after we failed to
3750 * find blocks that were already in the inode's extent tree. Hence,
3751 * we know that the beginning of the requested region cannot overlap
3752 * the extent from the inode's extent tree. There are three cases we
3753 * want to catch. The first is this case:
3754 *
3755 * |--- cluster # N--|
3756 * |--- extent ---| |---- requested region ---|
3757 * |==========|
3758 *
3759 * The second case that we need to test for is this one:
3760 *
3761 * |--------- cluster # N ----------------|
3762 * |--- requested region --| |------- extent ----|
3763 * |=======================|
3764 *
3765 * The third case is when the requested region lies between two extents
3766 * within the same cluster:
3767 * |------------- cluster # N-------------|
3768 * |----- ex -----| |---- ex_right ----|
3769 * |------ requested region ------|
3770 * |================|
3771 *
3772 * In each of the above cases, we need to set the map->m_pblk and
3773 * map->m_len so it corresponds to the return the extent labelled as
3774 * "|====|" from cluster #N, since it is already in use for data in
3775 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
3776 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
3777 * as a new "allocated" block region. Otherwise, we will return 0 and
3778 * ext4_ext_map_blocks() will then allocate one or more new clusters
3779 * by calling ext4_mb_new_blocks().
3780 */
Aditya Kalid8990242011-09-09 19:18:51 -04003781static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003782 struct ext4_map_blocks *map,
3783 struct ext4_extent *ex,
3784 struct ext4_ext_path *path)
3785{
Aditya Kalid8990242011-09-09 19:18:51 -04003786 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003787 ext4_lblk_t c_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
3788 ext4_lblk_t ex_cluster_start, ex_cluster_end;
Curt Wohlgemuth14d7f3e2011-12-18 17:39:02 -05003789 ext4_lblk_t rr_cluster_start;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003790 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
3791 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
3792 unsigned short ee_len = ext4_ext_get_actual_len(ex);
3793
3794 /* The extent passed in that we are trying to match */
3795 ex_cluster_start = EXT4_B2C(sbi, ee_block);
3796 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
3797
3798 /* The requested region passed into ext4_map_blocks() */
3799 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003800
3801 if ((rr_cluster_start == ex_cluster_end) ||
3802 (rr_cluster_start == ex_cluster_start)) {
3803 if (rr_cluster_start == ex_cluster_end)
3804 ee_start += ee_len - 1;
3805 map->m_pblk = (ee_start & ~(sbi->s_cluster_ratio - 1)) +
3806 c_offset;
3807 map->m_len = min(map->m_len,
3808 (unsigned) sbi->s_cluster_ratio - c_offset);
3809 /*
3810 * Check for and handle this case:
3811 *
3812 * |--------- cluster # N-------------|
3813 * |------- extent ----|
3814 * |--- requested region ---|
3815 * |===========|
3816 */
3817
3818 if (map->m_lblk < ee_block)
3819 map->m_len = min(map->m_len, ee_block - map->m_lblk);
3820
3821 /*
3822 * Check for the case where there is already another allocated
3823 * block to the right of 'ex' but before the end of the cluster.
3824 *
3825 * |------------- cluster # N-------------|
3826 * |----- ex -----| |---- ex_right ----|
3827 * |------ requested region ------|
3828 * |================|
3829 */
3830 if (map->m_lblk > ee_block) {
3831 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
3832 map->m_len = min(map->m_len, next - map->m_lblk);
3833 }
Aditya Kalid8990242011-09-09 19:18:51 -04003834
3835 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003836 return 1;
3837 }
Aditya Kalid8990242011-09-09 19:18:51 -04003838
3839 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003840 return 0;
3841}
3842
3843
3844/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05003845 * Block allocation/map/preallocation routine for extents based files
3846 *
3847 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003848 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003849 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3850 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05003851 *
3852 * return > 0, number of of blocks already mapped/allocated
3853 * if create == 0 and these are pre-allocated blocks
3854 * buffer head is unmapped
3855 * otherwise blocks are mapped
3856 *
3857 * return = 0, if plain look up failed (blocks have not been allocated)
3858 * buffer head is unmapped
3859 *
3860 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003861 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003862int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3863 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07003864{
3865 struct ext4_ext_path *path = NULL;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003866 struct ext4_extent newex, *ex, *ex2;
3867 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003868 ext4_fsblk_t newblock = 0;
Zheng Liu37794732012-11-08 14:47:52 -05003869 int free_on_err = 0, err = 0, depth;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003870 unsigned int allocated = 0, offset = 0;
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04003871 unsigned int allocated_clusters = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003872 struct ext4_allocation_request ar;
Dmitry Monakhovf45ee3a2012-09-28 23:21:09 -04003873 ext4_io_end_t *io = ext4_inode_aio(inode);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003874 ext4_lblk_t cluster_offset;
Dmitry Monakhov82e54222012-09-28 23:36:25 -04003875 int set_unwritten = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07003876
Mingming84fe3be2009-09-01 08:44:37 -04003877 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003878 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003879 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07003880
3881 /* check in cache */
Lukas Czerner78771912012-03-19 23:05:43 -04003882 if (ext4_ext_in_cache(inode, map->m_lblk, &newex)) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003883 if (!newex.ee_start_lo && !newex.ee_start_hi) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04003884 if ((sbi->s_cluster_ratio > 1) &&
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003885 ext4_find_delalloc_cluster(inode, map->m_lblk))
Aditya Kali7b415bf2011-09-09 19:04:51 -04003886 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3887
Theodore Ts'oc2177052009-05-14 00:58:52 -04003888 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003889 /*
3890 * block isn't allocated yet and
3891 * user doesn't want to allocate it
3892 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003893 goto out2;
3894 }
3895 /* we should allocate requested block */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003896 } else {
Alex Tomasa86c6182006-10-11 01:21:03 -07003897 /* block is already allocated */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003898 if (sbi->s_cluster_ratio > 1)
3899 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003900 newblock = map->m_lblk
Dave Kleikamp8c55e202007-05-24 13:04:54 -04003901 - le32_to_cpu(newex.ee_block)
Theodore Ts'obf89d162010-10-27 21:30:14 -04003902 + ext4_ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003903 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003904 allocated = ext4_ext_get_actual_len(&newex) -
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003905 (map->m_lblk - le32_to_cpu(newex.ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -07003906 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07003907 }
3908 }
3909
3910 /* find extent for this block */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003911 path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07003912 if (IS_ERR(path)) {
3913 err = PTR_ERR(path);
3914 path = NULL;
3915 goto out2;
3916 }
3917
3918 depth = ext_depth(inode);
3919
3920 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003921 * consistent leaf must not be empty;
3922 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07003923 * this is why assert can't be put in ext4_ext_find_extent()
3924 */
Frank Mayhar273df552010-03-02 11:46:09 -05003925 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
3926 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04003927 "lblock: %lu, depth: %d pblock %lld",
3928 (unsigned long) map->m_lblk, depth,
3929 path[depth].p_block);
Surbhi Palande034fb4c2009-12-14 09:53:52 -05003930 err = -EIO;
3931 goto out2;
3932 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003933
Avantika Mathur7e028972006-12-06 20:41:33 -08003934 ex = path[depth].p_ext;
3935 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003936 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003937 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003938 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003939
3940 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003941 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04003942 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003943 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003944 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04003945
3946 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
3947
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003948 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003949 if (in_range(map->m_lblk, ee_block, ee_len)) {
3950 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003951 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003952 allocated = ee_len - (map->m_lblk - ee_block);
3953 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
3954 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04003955
Allison Hendersone8613042011-05-25 07:41:46 -04003956 /*
Lukas Czerner78771912012-03-19 23:05:43 -04003957 * Do not put uninitialized extent
3958 * in the cache
Allison Hendersone8613042011-05-25 07:41:46 -04003959 */
Lukas Czerner78771912012-03-19 23:05:43 -04003960 if (!ext4_ext_is_uninitialized(ex)) {
3961 ext4_ext_put_in_cache(inode, ee_block,
3962 ee_len, ee_start);
3963 goto out;
Allison Hendersone8613042011-05-25 07:41:46 -04003964 }
Zheng Liu37794732012-11-08 14:47:52 -05003965 allocated = ext4_ext_handle_uninitialized_extents(
Lukas Czerner78771912012-03-19 23:05:43 -04003966 handle, inode, map, path, flags,
3967 allocated, newblock);
Zheng Liu37794732012-11-08 14:47:52 -05003968 goto out3;
Alex Tomasa86c6182006-10-11 01:21:03 -07003969 }
3970 }
3971
Aditya Kali7b415bf2011-09-09 19:04:51 -04003972 if ((sbi->s_cluster_ratio > 1) &&
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05003973 ext4_find_delalloc_cluster(inode, map->m_lblk))
Aditya Kali7b415bf2011-09-09 19:04:51 -04003974 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3975
Alex Tomasa86c6182006-10-11 01:21:03 -07003976 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003977 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07003978 * we couldn't try to create block if create flag is zero
3979 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04003980 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003981 /*
3982 * put just found gap into cache to speed up
3983 * subsequent requests
3984 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003985 ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
Alex Tomasa86c6182006-10-11 01:21:03 -07003986 goto out2;
3987 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003988
Alex Tomasa86c6182006-10-11 01:21:03 -07003989 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003990 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07003991 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003992 map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003993 newex.ee_block = cpu_to_le32(map->m_lblk);
3994 cluster_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
3995
3996 /*
3997 * If we are doing bigalloc, check to see if the extent returned
3998 * by ext4_ext_find_extent() implies a cluster we can use.
3999 */
4000 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04004001 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004002 ar.len = allocated = map->m_len;
4003 newblock = map->m_pblk;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004004 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004005 goto got_allocated_blocks;
4006 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004007
Alex Tomasc9de5602008-01-29 00:19:52 -05004008 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004009 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05004010 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
4011 if (err)
4012 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004013 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004014 ex2 = NULL;
4015 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
Alex Tomasc9de5602008-01-29 00:19:52 -05004016 if (err)
4017 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04004018
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004019 /* Check if the extent after searching to the right implies a
4020 * cluster we can use. */
4021 if ((sbi->s_cluster_ratio > 1) && ex2 &&
Aditya Kalid8990242011-09-09 19:18:51 -04004022 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004023 ar.len = allocated = map->m_len;
4024 newblock = map->m_pblk;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004025 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004026 goto got_allocated_blocks;
4027 }
4028
Amit Arora749269f2007-07-18 09:02:56 -04004029 /*
4030 * See if request is beyond maximum number of blocks we can have in
4031 * a single extent. For an initialized extent this limit is
4032 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
4033 * EXT_UNINIT_MAX_LEN.
4034 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004035 if (map->m_len > EXT_INIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04004036 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004037 map->m_len = EXT_INIT_MAX_LEN;
4038 else if (map->m_len > EXT_UNINIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04004039 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004040 map->m_len = EXT_UNINIT_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04004041
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004042 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004043 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004044 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04004045 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004046 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04004047 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004048 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004049
4050 /* allocate new block */
4051 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004052 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
4053 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004054 /*
4055 * We calculate the offset from the beginning of the cluster
4056 * for the logical block number, since when we allocate a
4057 * physical cluster, the physical block should start at the
4058 * same offset from the beginning of the cluster. This is
4059 * needed so that future calls to get_implied_cluster_alloc()
4060 * work correctly.
4061 */
4062 offset = map->m_lblk & (sbi->s_cluster_ratio - 1);
4063 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
4064 ar.goal -= offset;
4065 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05004066 if (S_ISREG(inode->i_mode))
4067 ar.flags = EXT4_MB_HINT_DATA;
4068 else
4069 /* disable in-core preallocation for non-regular files */
4070 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04004071 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4072 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05004073 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004074 if (!newblock)
4075 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04004076 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004077 ar.goal, newblock, allocated);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004078 free_on_err = 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004079 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004080 ar.len = EXT4_C2B(sbi, ar.len) - offset;
4081 if (ar.len > allocated)
4082 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004083
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004084got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07004085 /* try to insert new extent into found leaf and return */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004086 ext4_ext_store_pblock(&newex, newblock + offset);
Alex Tomasc9de5602008-01-29 00:19:52 -05004087 newex.ee_len = cpu_to_le16(ar.len);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004088 /* Mark uninitialized */
4089 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
Amit Aroraa2df2a62007-07-17 21:42:41 -04004090 ext4_ext_mark_uninitialized(&newex);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004091 /*
Jiaying Zhang744692d2010-03-04 16:14:02 -05004092 * io_end structure was created for every IO write to an
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004093 * uninitialized extent. To avoid unnecessary conversion,
Jiaying Zhang744692d2010-03-04 16:14:02 -05004094 * here we flag the IO that really needs the conversion.
Mingming5f524952009-11-10 10:48:04 -05004095 * For non asycn direct IO case, flag the inode state
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004096 * that we need to perform conversion when IO is done.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004097 */
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004098 if ((flags & EXT4_GET_BLOCKS_PRE_IO))
4099 set_unwritten = 1;
Jiaying Zhang744692d2010-03-04 16:14:02 -05004100 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004101 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004102 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004103
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004104 err = 0;
4105 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
4106 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4107 path, ar.len);
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004108 if (!err)
4109 err = ext4_ext_insert_extent(handle, inode, path,
4110 &newex, flags);
Dmitry Monakhov82e54222012-09-28 23:36:25 -04004111
4112 if (!err && set_unwritten) {
4113 if (io)
4114 ext4_set_io_unwritten_flag(inode, io);
4115 else
4116 ext4_set_inode_state(inode,
4117 EXT4_STATE_DIO_UNWRITTEN);
4118 }
4119
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004120 if (err && free_on_err) {
Maxim Patlasov7132de72011-07-10 19:37:48 -04004121 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
4122 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
Alex Tomas315054f2007-05-24 13:04:25 -04004123 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05004124 /* not a good idea to call discard here directly,
4125 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004126 ext4_discard_preallocations(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05004127 ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex),
Maxim Patlasov7132de72011-07-10 19:37:48 -04004128 ext4_ext_get_actual_len(&newex), fb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004129 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04004130 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004131
Alex Tomasa86c6182006-10-11 01:21:03 -07004132 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04004133 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004134 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004135 if (allocated > map->m_len)
4136 allocated = map->m_len;
4137 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07004138
Jan Karab436b9b2009-12-08 23:51:10 -05004139 /*
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004140 * Update reserved blocks/metadata blocks after successful
4141 * block allocation which had been deferred till now.
4142 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04004143 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004144 unsigned int reserved_clusters;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004145 /*
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004146 * Check how many clusters we had reserved this allocated range
Aditya Kali7b415bf2011-09-09 19:04:51 -04004147 */
4148 reserved_clusters = get_reserved_cluster_alloc(inode,
4149 map->m_lblk, allocated);
4150 if (map->m_flags & EXT4_MAP_FROM_CLUSTER) {
4151 if (reserved_clusters) {
4152 /*
4153 * We have clusters reserved for this range.
4154 * But since we are not doing actual allocation
4155 * and are simply using blocks from previously
4156 * allocated cluster, we should release the
4157 * reservation and not claim quota.
4158 */
4159 ext4_da_update_reserve_space(inode,
4160 reserved_clusters, 0);
4161 }
4162 } else {
4163 BUG_ON(allocated_clusters < reserved_clusters);
4164 /* We will claim quota for all newly allocated blocks.*/
4165 ext4_da_update_reserve_space(inode, allocated_clusters,
4166 1);
4167 if (reserved_clusters < allocated_clusters) {
Aditya Kali5356f262011-09-09 19:20:51 -04004168 struct ext4_inode_info *ei = EXT4_I(inode);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004169 int reservation = allocated_clusters -
4170 reserved_clusters;
4171 /*
4172 * It seems we claimed few clusters outside of
4173 * the range of this allocation. We should give
4174 * it back to the reservation pool. This can
4175 * happen in the following case:
4176 *
4177 * * Suppose s_cluster_ratio is 4 (i.e., each
4178 * cluster has 4 blocks. Thus, the clusters
4179 * are [0-3],[4-7],[8-11]...
4180 * * First comes delayed allocation write for
4181 * logical blocks 10 & 11. Since there were no
4182 * previous delayed allocated blocks in the
4183 * range [8-11], we would reserve 1 cluster
4184 * for this write.
4185 * * Next comes write for logical blocks 3 to 8.
4186 * In this case, we will reserve 2 clusters
4187 * (for [0-3] and [4-7]; and not for [8-11] as
4188 * that range has a delayed allocated blocks.
4189 * Thus total reserved clusters now becomes 3.
4190 * * Now, during the delayed allocation writeout
4191 * time, we will first write blocks [3-8] and
4192 * allocate 3 clusters for writing these
4193 * blocks. Also, we would claim all these
4194 * three clusters above.
4195 * * Now when we come here to writeout the
4196 * blocks [10-11], we would expect to claim
4197 * the reservation of 1 cluster we had made
4198 * (and we would claim it since there are no
4199 * more delayed allocated blocks in the range
4200 * [8-11]. But our reserved cluster count had
4201 * already gone to 0.
4202 *
4203 * Thus, at the step 4 above when we determine
4204 * that there are still some unwritten delayed
4205 * allocated blocks outside of our current
4206 * block range, we should increment the
4207 * reserved clusters count so that when the
4208 * remaining blocks finally gets written, we
4209 * could claim them.
4210 */
Aditya Kali5356f262011-09-09 19:20:51 -04004211 dquot_reserve_block(inode,
4212 EXT4_C2B(sbi, reservation));
4213 spin_lock(&ei->i_block_reservation_lock);
4214 ei->i_reserved_data_blocks += reservation;
4215 spin_unlock(&ei->i_block_reservation_lock);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004216 }
4217 }
4218 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004219
4220 /*
Jan Karab436b9b2009-12-08 23:51:10 -05004221 * Cache the extent and update transaction to commit on fdatasync only
4222 * when it is _not_ an uninitialized extent.
4223 */
4224 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05004225 ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock);
Jan Karab436b9b2009-12-08 23:51:10 -05004226 ext4_update_inode_fsync_trans(handle, inode, 1);
4227 } else
4228 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004229out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004230 if (allocated > map->m_len)
4231 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004232 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004233 map->m_flags |= EXT4_MAP_MAPPED;
4234 map->m_pblk = newblock;
4235 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004236out2:
4237 if (path) {
4238 ext4_ext_drop_refs(path);
4239 kfree(path);
4240 }
Allison Hendersone8613042011-05-25 07:41:46 -04004241
Zheng Liu37794732012-11-08 14:47:52 -05004242out3:
Zheng Liu19b303d2012-11-08 14:34:04 -05004243 trace_ext4_ext_map_blocks_exit(inode, map, err ? err : allocated);
Yongqiang Yange7b319e2011-10-29 09:39:51 -04004244
Lukas Czerner78771912012-03-19 23:05:43 -04004245 return err ? err : allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004246}
4247
Jan Karacf108bc2008-07-11 19:27:31 -04004248void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004249{
4250 struct address_space *mapping = inode->i_mapping;
4251 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004252 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004253 handle_t *handle;
Allison Henderson189e8682011-09-06 21:49:44 -04004254 loff_t page_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004255 int err = 0;
4256
4257 /*
Jiaying Zhang3889fd52011-01-10 12:47:05 -05004258 * finish any pending end_io work so we won't run the risk of
4259 * converting any truncated blocks to initialized later
4260 */
Dmitry Monakhovc2785312012-10-05 11:31:55 -04004261 ext4_flush_unwritten_io(inode);
Jiaying Zhang3889fd52011-01-10 12:47:05 -05004262
4263 /*
Alex Tomasa86c6182006-10-11 01:21:03 -07004264 * probably first extent we're gonna free will be last in block
4265 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04004266 err = ext4_writepage_trans_blocks(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07004267 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04004268 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07004269 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07004270
Allison Henderson189e8682011-09-06 21:49:44 -04004271 if (inode->i_size % PAGE_CACHE_SIZE != 0) {
4272 page_len = PAGE_CACHE_SIZE -
4273 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4274
4275 err = ext4_discard_partial_page_buffers(handle,
4276 mapping, inode->i_size, page_len, 0);
4277
4278 if (err)
4279 goto out_stop;
4280 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004281
Jan Kara9ddfc3d2008-07-11 19:27:31 -04004282 if (ext4_orphan_add(handle, inode))
4283 goto out_stop;
4284
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004285 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07004286 ext4_ext_invalidate_cache(inode);
4287
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004288 ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05004289
Alex Tomasa86c6182006-10-11 01:21:03 -07004290 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004291 * TODO: optimization is possible here.
4292 * Probably we need not scan at all,
4293 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004294 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004295
4296 /* we have to know where to truncate from in crash case */
4297 EXT4_I(inode)->i_disksize = inode->i_size;
4298 ext4_mark_inode_dirty(handle, inode);
4299
4300 last_block = (inode->i_size + sb->s_blocksize - 1)
4301 >> EXT4_BLOCK_SIZE_BITS(sb);
Zheng Liu51865fd2012-11-08 21:57:32 -05004302 err = ext4_es_remove_extent(inode, last_block,
4303 EXT_MAX_BLOCKS - last_block);
Lukas Czerner5f95d212012-03-19 23:03:19 -04004304 err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07004305
4306 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04004307 * transaction synchronous.
4308 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004309 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05004310 ext4_handle_sync(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07004311
Jan Kara9ddfc3d2008-07-11 19:27:31 -04004312 up_write(&EXT4_I(inode)->i_data_sem);
Eric Gouriouf6d2f6b2011-05-22 21:33:00 -04004313
4314out_stop:
Alex Tomasa86c6182006-10-11 01:21:03 -07004315 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004316 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07004317 * then we need to clear up the orphan record which we created above.
4318 * However, if this was a real unlink then we were called by
4319 * ext4_delete_inode(), and we allow that function to clean up the
4320 * orphan info for us.
4321 */
4322 if (inode->i_nlink)
4323 ext4_orphan_del(handle, inode);
4324
Solofo Ramangalahyef737722008-04-29 22:00:41 -04004325 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4326 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07004327 ext4_journal_stop(handle);
4328}
4329
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004330static void ext4_falloc_update_inode(struct inode *inode,
4331 int mode, loff_t new_size, int update_ctime)
4332{
4333 struct timespec now;
4334
4335 if (update_ctime) {
4336 now = current_fs_time(inode->i_sb);
4337 if (!timespec_equal(&inode->i_ctime, &now))
4338 inode->i_ctime = now;
4339 }
4340 /*
4341 * Update only when preallocation was requested beyond
4342 * the file size.
4343 */
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04004344 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
4345 if (new_size > i_size_read(inode))
4346 i_size_write(inode, new_size);
4347 if (new_size > EXT4_I(inode)->i_disksize)
4348 ext4_update_i_disksize(inode, new_size);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004349 } else {
4350 /*
4351 * Mark that we allocate beyond EOF so the subsequent truncate
4352 * can proceed even if the new size is the same as i_size.
4353 */
4354 if (new_size > i_size_read(inode))
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004355 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004356 }
4357
4358}
4359
Amit Aroraa2df2a62007-07-17 21:42:41 -04004360/*
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004361 * preallocate space for a file. This implements ext4's fallocate file
Amit Aroraa2df2a62007-07-17 21:42:41 -04004362 * operation, which gets called from sys_fallocate system call.
4363 * For block-mapped files, posix_fallocate should fall back to the method
4364 * of writing zeroes to the required new blocks (the same behavior which is
4365 * expected for file systems which do not support fallocate() system call).
4366 */
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004367long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004368{
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004369 struct inode *inode = file->f_path.dentry->d_inode;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004370 handle_t *handle;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004371 loff_t new_size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004372 unsigned int max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004373 int ret = 0;
4374 int ret2 = 0;
4375 int retries = 0;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004376 int flags;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004377 struct ext4_map_blocks map;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004378 unsigned int credits, blkbits = inode->i_blkbits;
4379
4380 /*
4381 * currently supporting (pre)allocate mode for extent-based
4382 * files _only_
4383 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004384 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Amit Aroraa2df2a62007-07-17 21:42:41 -04004385 return -EOPNOTSUPP;
4386
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004387 /* Return error if mode is not supported */
4388 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
4389 return -EOPNOTSUPP;
4390
4391 if (mode & FALLOC_FL_PUNCH_HOLE)
4392 return ext4_punch_hole(file, offset, len);
4393
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004394 trace_ext4_fallocate_enter(inode, offset, len, mode);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004395 map.m_lblk = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004396 /*
4397 * We can't just convert len to max_blocks because
4398 * If blocksize = 4096 offset = 3072 and len = 2048
4399 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004400 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004401 - map.m_lblk;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004402 /*
Mingming Caof3bd1f32008-08-19 22:16:03 -04004403 * credits to insert 1 extent into extent tree
Amit Aroraa2df2a62007-07-17 21:42:41 -04004404 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04004405 credits = ext4_chunk_trans_blocks(inode, max_blocks);
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05004406 mutex_lock(&inode->i_mutex);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04004407 ret = inode_newsize_ok(inode, (len + offset));
4408 if (ret) {
4409 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004410 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04004411 return ret;
4412 }
Greg Harm3c6fe772011-10-31 18:41:47 -04004413 flags = EXT4_GET_BLOCKS_CREATE_UNINIT_EXT;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004414 if (mode & FALLOC_FL_KEEP_SIZE)
4415 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
Greg Harm3c6fe772011-10-31 18:41:47 -04004416 /*
4417 * Don't normalize the request if it can fit in one extent so
4418 * that it doesn't get unnecessarily split into multiple
4419 * extents.
4420 */
4421 if (len <= EXT_UNINIT_MAX_LEN << blkbits)
4422 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
Dmitry Monakhov60d46162012-10-05 11:32:02 -04004423
4424 /* Prevent race condition between unwritten */
4425 ext4_flush_unwritten_io(inode);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004426retry:
4427 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004428 map.m_lblk = map.m_lblk + ret;
4429 map.m_len = max_blocks = max_blocks - ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004430 handle = ext4_journal_start(inode, credits);
4431 if (IS_ERR(handle)) {
4432 ret = PTR_ERR(handle);
4433 break;
4434 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004435 ret = ext4_map_blocks(handle, inode, &map, flags);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05004436 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004437#ifdef EXT4FS_DEBUG
4438 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004439 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004440 "returned error inode#%lu, block=%u, "
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05004441 "max_blocks=%u", __func__,
Kazuya Mioa6371b62010-10-27 21:30:15 -04004442 inode->i_ino, map.m_lblk, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004443#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04004444 ext4_mark_inode_dirty(handle, inode);
4445 ret2 = ext4_journal_stop(handle);
4446 break;
4447 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004448 if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004449 blkbits) >> blkbits))
4450 new_size = offset + len;
4451 else
Utako Kusaka29ae07b2011-07-27 22:11:20 -04004452 new_size = ((loff_t) map.m_lblk + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004453
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004454 ext4_falloc_update_inode(inode, mode, new_size,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004455 (map.m_flags & EXT4_MAP_NEW));
Amit Aroraa2df2a62007-07-17 21:42:41 -04004456 ext4_mark_inode_dirty(handle, inode);
Zheng Liuf4e95b32012-06-30 19:12:57 -04004457 if ((file->f_flags & O_SYNC) && ret >= max_blocks)
4458 ext4_handle_sync(handle);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004459 ret2 = ext4_journal_stop(handle);
4460 if (ret2)
4461 break;
4462 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004463 if (ret == -ENOSPC &&
4464 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4465 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004466 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004467 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05004468 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004469 trace_ext4_fallocate_exit(inode, offset, max_blocks,
4470 ret > 0 ? ret2 : ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004471 return ret > 0 ? ret2 : ret;
4472}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004473
4474/*
Mingming Cao00314622009-09-28 15:49:08 -04004475 * This function convert a range of blocks to written extents
4476 * The caller of this function will pass the start offset and the size.
4477 * all unwritten extents within this range will be converted to
4478 * written extents.
4479 *
4480 * This function is called from the direct IO end io call back
4481 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05004482 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04004483 */
4484int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
Eric Sandeena1de02d2010-02-04 23:58:38 -05004485 ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04004486{
4487 handle_t *handle;
Mingming Cao00314622009-09-28 15:49:08 -04004488 unsigned int max_blocks;
4489 int ret = 0;
4490 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004491 struct ext4_map_blocks map;
Mingming Cao00314622009-09-28 15:49:08 -04004492 unsigned int credits, blkbits = inode->i_blkbits;
4493
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004494 map.m_lblk = offset >> blkbits;
Mingming Cao00314622009-09-28 15:49:08 -04004495 /*
4496 * We can't just convert len to max_blocks because
4497 * If blocksize = 4096 offset = 3072 and len = 2048
4498 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004499 max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
4500 map.m_lblk);
Mingming Cao00314622009-09-28 15:49:08 -04004501 /*
4502 * credits to insert 1 extent into extent tree
4503 */
4504 credits = ext4_chunk_trans_blocks(inode, max_blocks);
4505 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004506 map.m_lblk += ret;
4507 map.m_len = (max_blocks -= ret);
Mingming Cao00314622009-09-28 15:49:08 -04004508 handle = ext4_journal_start(inode, credits);
4509 if (IS_ERR(handle)) {
4510 ret = PTR_ERR(handle);
4511 break;
4512 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004513 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004514 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Mingming Cao00314622009-09-28 15:49:08 -04004515 if (ret <= 0) {
4516 WARN_ON(ret <= 0);
Theodore Ts'o92b97812012-03-19 23:41:49 -04004517 ext4_msg(inode->i_sb, KERN_ERR,
4518 "%s:%d: inode #%lu: block %u: len %u: "
4519 "ext4_ext_map_blocks returned %d",
4520 __func__, __LINE__, inode->i_ino, map.m_lblk,
4521 map.m_len, ret);
Mingming Cao00314622009-09-28 15:49:08 -04004522 }
4523 ext4_mark_inode_dirty(handle, inode);
4524 ret2 = ext4_journal_stop(handle);
4525 if (ret <= 0 || ret2 )
4526 break;
4527 }
4528 return ret > 0 ? ret2 : ret;
4529}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004530
Mingming Cao00314622009-09-28 15:49:08 -04004531/*
Lukas Czerner91dd8c12012-11-28 12:32:26 -05004532 * If newex is not existing extent (newex->ec_start equals zero) find
4533 * delayed extent at start of newex and update newex accordingly and
4534 * return start of the next delayed extent.
4535 *
4536 * If newex is existing extent (newex->ec_start is not equal zero)
4537 * return start of next delayed extent or EXT_MAX_BLOCKS if no delayed
4538 * extent found. Leave newex unmodified.
Eric Sandeen6873fa02008-10-07 00:46:36 -04004539 */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05004540static int ext4_find_delayed_extent(struct inode *inode,
4541 struct ext4_ext_cache *newex)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004542{
Zheng Liub3aff3e2012-11-08 21:57:37 -05004543 struct extent_status es;
Zheng Liub3aff3e2012-11-08 21:57:37 -05004544 ext4_lblk_t next_del;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004545
Zheng Liub3aff3e2012-11-08 21:57:37 -05004546 es.start = newex->ec_block;
4547 next_del = ext4_es_find_extent(inode, &es);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004548
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05004549 if (newex->ec_start == 0) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004550 /*
4551 * No extent in extent-tree contains block @newex->ec_start,
4552 * then the block may stay in 1)a hole or 2)delayed-extent.
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004553 */
Zheng Liub3aff3e2012-11-08 21:57:37 -05004554 if (es.len == 0)
4555 /* A hole found. */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05004556 return 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004557
Zheng Liub3aff3e2012-11-08 21:57:37 -05004558 if (es.start > newex->ec_block) {
4559 /* A hole found. */
4560 newex->ec_len = min(es.start - newex->ec_block,
4561 newex->ec_len);
Lukas Czerner91dd8c12012-11-28 12:32:26 -05004562 return 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004563 }
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004564
Zheng Liub3aff3e2012-11-08 21:57:37 -05004565 newex->ec_len = es.start + es.len - newex->ec_block;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004566 }
4567
Lukas Czerner91dd8c12012-11-28 12:32:26 -05004568 return next_del;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004569}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004570/* fiemap flags we can handle specified here */
4571#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
4572
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004573static int ext4_xattr_fiemap(struct inode *inode,
4574 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004575{
4576 __u64 physical = 0;
4577 __u64 length;
4578 __u32 flags = FIEMAP_EXTENT_LAST;
4579 int blockbits = inode->i_sb->s_blocksize_bits;
4580 int error = 0;
4581
4582 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004583 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04004584 struct ext4_iloc iloc;
4585 int offset; /* offset of xattr in inode */
4586
4587 error = ext4_get_inode_loc(inode, &iloc);
4588 if (error)
4589 return error;
4590 physical = iloc.bh->b_blocknr << blockbits;
4591 offset = EXT4_GOOD_OLD_INODE_SIZE +
4592 EXT4_I(inode)->i_extra_isize;
4593 physical += offset;
4594 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
4595 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004596 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004597 } else { /* external block */
4598 physical = EXT4_I(inode)->i_file_acl << blockbits;
4599 length = inode->i_sb->s_blocksize;
4600 }
4601
4602 if (physical)
4603 error = fiemap_fill_next_extent(fieinfo, 0, physical,
4604 length, flags);
4605 return (error < 0 ? error : 0);
4606}
4607
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004608/*
4609 * ext4_ext_punch_hole
4610 *
4611 * Punches a hole of "length" bytes in a file starting
4612 * at byte "offset"
4613 *
4614 * @inode: The inode of the file to punch a hole in
4615 * @offset: The starting byte offset of the hole
4616 * @length: The length of the hole
4617 *
4618 * Returns the number of blocks removed or negative on err
4619 */
4620int ext4_ext_punch_hole(struct file *file, loff_t offset, loff_t length)
4621{
4622 struct inode *inode = file->f_path.dentry->d_inode;
4623 struct super_block *sb = inode->i_sb;
Lukas Czerner5f95d212012-03-19 23:03:19 -04004624 ext4_lblk_t first_block, stop_block;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004625 struct address_space *mapping = inode->i_mapping;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004626 handle_t *handle;
Allison Hendersonba062082011-09-03 11:55:59 -04004627 loff_t first_page, last_page, page_len;
4628 loff_t first_page_offset, last_page_offset;
Lukas Czerner5f95d212012-03-19 23:03:19 -04004629 int credits, err = 0;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004630
Dmitry Monakhov02d262d2012-09-30 23:03:42 -04004631 /*
4632 * Write out all dirty pages to avoid race conditions
4633 * Then release them.
4634 */
4635 if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
4636 err = filemap_write_and_wait_range(mapping,
4637 offset, offset + length - 1);
4638
4639 if (err)
4640 return err;
4641 }
4642
4643 mutex_lock(&inode->i_mutex);
4644 /* It's not possible punch hole on append only file */
4645 if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) {
4646 err = -EPERM;
4647 goto out_mutex;
4648 }
4649 if (IS_SWAPFILE(inode)) {
4650 err = -ETXTBSY;
4651 goto out_mutex;
4652 }
4653
Allison Henderson2be47512011-09-03 11:56:52 -04004654 /* No need to punch hole beyond i_size */
4655 if (offset >= inode->i_size)
Dmitry Monakhov02d262d2012-09-30 23:03:42 -04004656 goto out_mutex;
Allison Henderson2be47512011-09-03 11:56:52 -04004657
4658 /*
4659 * If the hole extends beyond i_size, set the hole
4660 * to end after the page that contains i_size
4661 */
4662 if (offset + length > inode->i_size) {
4663 length = inode->i_size +
4664 PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) -
4665 offset;
4666 }
4667
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004668 first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
4669 last_page = (offset + length) >> PAGE_CACHE_SHIFT;
4670
4671 first_page_offset = first_page << PAGE_CACHE_SHIFT;
4672 last_page_offset = last_page << PAGE_CACHE_SHIFT;
4673
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004674 /* Now release the pages */
4675 if (last_page_offset > first_page_offset) {
Hugh Dickins5e44f8c2012-06-01 00:15:28 -04004676 truncate_pagecache_range(inode, first_page_offset,
4677 last_page_offset - 1);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004678 }
4679
Dmitry Monakhov02d262d2012-09-30 23:03:42 -04004680 /* Wait all existing dio workers, newcomers will block on i_mutex */
4681 ext4_inode_block_unlocked_dio(inode);
Dmitry Monakhovc2785312012-10-05 11:31:55 -04004682 err = ext4_flush_unwritten_io(inode);
Dmitry Monakhov28a535f2012-09-29 00:14:55 -04004683 if (err)
Dmitry Monakhov02d262d2012-09-30 23:03:42 -04004684 goto out_dio;
Dmitry Monakhovc2785312012-10-05 11:31:55 -04004685 inode_dio_wait(inode);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004686
4687 credits = ext4_writepage_trans_blocks(inode);
4688 handle = ext4_journal_start(inode, credits);
Dmitry Monakhov02d262d2012-09-30 23:03:42 -04004689 if (IS_ERR(handle)) {
4690 err = PTR_ERR(handle);
4691 goto out_dio;
4692 }
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004693
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004694
4695 /*
Allison Hendersonba062082011-09-03 11:55:59 -04004696 * Now we need to zero out the non-page-aligned data in the
4697 * pages at the start and tail of the hole, and unmap the buffer
4698 * heads for the block aligned regions of the page that were
4699 * completely zeroed.
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004700 */
Allison Hendersonba062082011-09-03 11:55:59 -04004701 if (first_page > last_page) {
4702 /*
4703 * If the file space being truncated is contained within a page
4704 * just zero out and unmap the middle of that page
4705 */
4706 err = ext4_discard_partial_page_buffers(handle,
4707 mapping, offset, length, 0);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004708
Allison Hendersonba062082011-09-03 11:55:59 -04004709 if (err)
4710 goto out;
4711 } else {
4712 /*
4713 * zero out and unmap the partial page that contains
4714 * the start of the hole
4715 */
4716 page_len = first_page_offset - offset;
4717 if (page_len > 0) {
4718 err = ext4_discard_partial_page_buffers(handle, mapping,
4719 offset, page_len, 0);
4720 if (err)
4721 goto out;
4722 }
4723
4724 /*
4725 * zero out and unmap the partial page that contains
4726 * the end of the hole
4727 */
4728 page_len = offset + length - last_page_offset;
4729 if (page_len > 0) {
4730 err = ext4_discard_partial_page_buffers(handle, mapping,
4731 last_page_offset, page_len, 0);
4732 if (err)
4733 goto out;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004734 }
4735 }
4736
Allison Henderson2be47512011-09-03 11:56:52 -04004737 /*
4738 * If i_size is contained in the last page, we need to
4739 * unmap and zero the partial page after i_size
4740 */
4741 if (inode->i_size >> PAGE_CACHE_SHIFT == last_page &&
4742 inode->i_size % PAGE_CACHE_SIZE != 0) {
4743
4744 page_len = PAGE_CACHE_SIZE -
4745 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4746
4747 if (page_len > 0) {
4748 err = ext4_discard_partial_page_buffers(handle,
4749 mapping, inode->i_size, page_len, 0);
4750
4751 if (err)
4752 goto out;
4753 }
4754 }
4755
Lukas Czerner5f95d212012-03-19 23:03:19 -04004756 first_block = (offset + sb->s_blocksize - 1) >>
4757 EXT4_BLOCK_SIZE_BITS(sb);
4758 stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
4759
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004760 /* If there are no blocks to remove, return now */
Lukas Czerner5f95d212012-03-19 23:03:19 -04004761 if (first_block >= stop_block)
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004762 goto out;
4763
4764 down_write(&EXT4_I(inode)->i_data_sem);
4765 ext4_ext_invalidate_cache(inode);
4766 ext4_discard_preallocations(inode);
4767
Zheng Liu51865fd2012-11-08 21:57:32 -05004768 err = ext4_es_remove_extent(inode, first_block,
4769 stop_block - first_block);
Lukas Czerner5f95d212012-03-19 23:03:19 -04004770 err = ext4_ext_remove_space(inode, first_block, stop_block - 1);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004771
Lukas Czerner5f95d212012-03-19 23:03:19 -04004772 ext4_ext_invalidate_cache(inode);
4773 ext4_discard_preallocations(inode);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004774
4775 if (IS_SYNC(inode))
4776 ext4_handle_sync(handle);
4777
4778 up_write(&EXT4_I(inode)->i_data_sem);
4779
4780out:
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004781 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4782 ext4_mark_inode_dirty(handle, inode);
4783 ext4_journal_stop(handle);
Dmitry Monakhov02d262d2012-09-30 23:03:42 -04004784out_dio:
4785 ext4_inode_resume_unlocked_dio(inode);
4786out_mutex:
4787 mutex_unlock(&inode->i_mutex);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004788 return err;
4789}
Lukas Czerner91dd8c12012-11-28 12:32:26 -05004790
Eric Sandeen6873fa02008-10-07 00:46:36 -04004791int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4792 __u64 start, __u64 len)
4793{
4794 ext4_lblk_t start_blk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004795 int error = 0;
4796
4797 /* fallback to generic here if not in extents fmt */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004798 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeen6873fa02008-10-07 00:46:36 -04004799 return generic_block_fiemap(inode, fieinfo, start, len,
4800 ext4_get_block);
4801
4802 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
4803 return -EBADR;
4804
4805 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
4806 error = ext4_xattr_fiemap(inode, fieinfo);
4807 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004808 ext4_lblk_t len_blks;
4809 __u64 last_blk;
4810
Eric Sandeen6873fa02008-10-07 00:46:36 -04004811 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004812 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04004813 if (last_blk >= EXT_MAX_BLOCKS)
4814 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004815 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004816
4817 /*
Lukas Czerner91dd8c12012-11-28 12:32:26 -05004818 * Walk the extent tree gathering extent information
4819 * and pushing extents back to the user.
Eric Sandeen6873fa02008-10-07 00:46:36 -04004820 */
Lukas Czerner91dd8c12012-11-28 12:32:26 -05004821 error = ext4_fill_fiemap_extents(inode, start_blk,
4822 len_blks, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004823 }
4824
4825 return error;
4826}