blob: 8c1334ee8c7f1e54608105cb15e136b55ce2f8c8 [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
Allison Hendersond583fb82011-05-25 07:41:43 -040055static int ext4_split_extent(handle_t *handle,
56 struct inode *inode,
57 struct ext4_ext_path *path,
58 struct ext4_map_blocks *map,
59 int split_flag,
60 int flags);
61
Lukas Czerner5f95d212012-03-19 23:03:19 -040062static int ext4_split_extent_at(handle_t *handle,
63 struct inode *inode,
64 struct ext4_ext_path *path,
65 ext4_lblk_t split,
66 int split_flag,
67 int flags);
68
Jan Kara487caee2009-08-17 22:17:20 -040069static int ext4_ext_truncate_extend_restart(handle_t *handle,
70 struct inode *inode,
71 int needed)
Alex Tomasa86c6182006-10-11 01:21:03 -070072{
73 int err;
74
Frank Mayhar03901312009-01-07 00:06:22 -050075 if (!ext4_handle_valid(handle))
76 return 0;
Alex Tomasa86c6182006-10-11 01:21:03 -070077 if (handle->h_buffer_credits > needed)
Shen Feng9102e4f2008-07-11 19:27:31 -040078 return 0;
79 err = ext4_journal_extend(handle, needed);
Theodore Ts'o0123c932008-08-01 20:57:54 -040080 if (err <= 0)
Shen Feng9102e4f2008-07-11 19:27:31 -040081 return err;
Jan Kara487caee2009-08-17 22:17:20 -040082 err = ext4_truncate_restart_trans(handle, inode, needed);
Dmitry Monakhov0617b832010-05-17 01:00:00 -040083 if (err == 0)
84 err = -EAGAIN;
Jan Kara487caee2009-08-17 22:17:20 -040085
86 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -070087}
88
89/*
90 * could return:
91 * - EROFS
92 * - ENOMEM
93 */
94static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
95 struct ext4_ext_path *path)
96{
97 if (path->p_bh) {
98 /* path points to block */
99 return ext4_journal_get_write_access(handle, path->p_bh);
100 }
101 /* path points to leaf/index in inode body */
102 /* we use in-core data, no need to protect them */
103 return 0;
104}
105
106/*
107 * could return:
108 * - EROFS
109 * - ENOMEM
110 * - EIO
111 */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -0400112#define ext4_ext_dirty(handle, inode, path) \
113 __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path))
114static int __ext4_ext_dirty(const char *where, unsigned int line,
115 handle_t *handle, struct inode *inode,
116 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700117{
118 int err;
119 if (path->p_bh) {
120 /* path points to block */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -0400121 err = __ext4_handle_dirty_metadata(where, line, handle,
122 inode, path->p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700123 } else {
124 /* path points to leaf/index in inode body */
125 err = ext4_mark_inode_dirty(handle, inode);
126 }
127 return err;
128}
129
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700130static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700131 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500132 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700133{
Alex Tomasa86c6182006-10-11 01:21:03 -0700134 if (path) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400135 int depth = path->p_depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700136 struct ext4_extent *ex;
Alex Tomasa86c6182006-10-11 01:21:03 -0700137
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500138 /*
139 * Try to predict block placement assuming that we are
140 * filling in a file which will eventually be
141 * non-sparse --- i.e., in the case of libbfd writing
142 * an ELF object sections out-of-order but in a way
143 * the eventually results in a contiguous object or
144 * executable file, or some database extending a table
145 * space file. However, this is actually somewhat
146 * non-ideal if we are writing a sparse file such as
147 * qemu or KVM writing a raw image file that is going
148 * to stay fairly sparse, since it will end up
149 * fragmenting the file system's free space. Maybe we
150 * should have some hueristics or some way to allow
151 * userspace to pass a hint to file system,
Tao Mab8d65682011-01-21 23:21:31 +0800152 * especially if the latter case turns out to be
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500153 * common.
154 */
Avantika Mathur7e028972006-12-06 20:41:33 -0800155 ex = path[depth].p_ext;
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500156 if (ex) {
157 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
158 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
159
160 if (block > ext_block)
161 return ext_pblk + (block - ext_block);
162 else
163 return ext_pblk - (ext_block - block);
164 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700165
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700166 /* it looks like index is empty;
167 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700168 if (path[depth].p_bh)
169 return path[depth].p_bh->b_blocknr;
170 }
171
172 /* OK. use inode's group */
Eric Sandeenf86186b2011-06-28 10:01:31 -0400173 return ext4_inode_to_goal_block(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700174}
175
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400176/*
177 * Allocation for a meta data block
178 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700179static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400180ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700181 struct ext4_ext_path *path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400182 struct ext4_extent *ex, int *err, unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700183{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700184 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700185
186 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Allison Henderson55f020d2011-05-25 07:41:26 -0400187 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
188 NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700189 return newblock;
190}
191
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400192static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700193{
194 int size;
195
196 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
197 / sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100198#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400199 if (!check && size > 6)
200 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700201#endif
202 return size;
203}
204
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400205static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700206{
207 int size;
208
209 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
210 / sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100211#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400212 if (!check && size > 5)
213 size = 5;
Alex Tomasa86c6182006-10-11 01:21:03 -0700214#endif
215 return size;
216}
217
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400218static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700219{
220 int size;
221
222 size = sizeof(EXT4_I(inode)->i_data);
223 size -= sizeof(struct ext4_extent_header);
224 size /= sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100225#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400226 if (!check && size > 3)
227 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700228#endif
229 return size;
230}
231
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400232static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700233{
234 int size;
235
236 size = sizeof(EXT4_I(inode)->i_data);
237 size -= sizeof(struct ext4_extent_header);
238 size /= sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100239#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400240 if (!check && size > 4)
241 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700242#endif
243 return size;
244}
245
Mingming Caod2a17632008-07-14 17:52:37 -0400246/*
247 * Calculate the number of metadata blocks needed
248 * to allocate @blocks
249 * Worse case is one block per extent
250 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -0500251int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -0400252{
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500253 struct ext4_inode_info *ei = EXT4_I(inode);
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400254 int idxs;
Mingming Caod2a17632008-07-14 17:52:37 -0400255
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500256 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
257 / sizeof(struct ext4_extent_idx));
Mingming Caod2a17632008-07-14 17:52:37 -0400258
259 /*
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500260 * If the new delayed allocation block is contiguous with the
261 * previous da block, it can share index blocks with the
262 * previous block, so we only need to allocate a new index
263 * block every idxs leaf blocks. At ldxs**2 blocks, we need
264 * an additional index block, and at ldxs**3 blocks, yet
265 * another index blocks.
Mingming Caod2a17632008-07-14 17:52:37 -0400266 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500267 if (ei->i_da_metadata_calc_len &&
268 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400269 int num = 0;
270
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500271 if ((ei->i_da_metadata_calc_len % idxs) == 0)
272 num++;
273 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
274 num++;
275 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
276 num++;
277 ei->i_da_metadata_calc_len = 0;
278 } else
279 ei->i_da_metadata_calc_len++;
280 ei->i_da_metadata_calc_last_lblock++;
281 return num;
282 }
Mingming Caod2a17632008-07-14 17:52:37 -0400283
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500284 /*
285 * In the worst case we need a new set of index blocks at
286 * every level of the inode's extent tree.
287 */
288 ei->i_da_metadata_calc_len = 1;
289 ei->i_da_metadata_calc_last_lblock = lblock;
290 return ext_depth(inode) + 1;
Mingming Caod2a17632008-07-14 17:52:37 -0400291}
292
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400293static int
294ext4_ext_max_entries(struct inode *inode, int depth)
295{
296 int max;
297
298 if (depth == ext_depth(inode)) {
299 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400300 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400301 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400302 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400303 } else {
304 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400305 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400306 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400307 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400308 }
309
310 return max;
311}
312
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400313static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
314{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400315 ext4_fsblk_t block = ext4_ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400316 int len = ext4_ext_get_actual_len(ext);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400317
Theodore Ts'o31d4f3a2012-03-11 23:30:16 -0400318 if (len == 0)
319 return 0;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400320 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400321}
322
323static int ext4_valid_extent_idx(struct inode *inode,
324 struct ext4_extent_idx *ext_idx)
325{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400326 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400327
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400328 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400329}
330
331static int ext4_valid_extent_entries(struct inode *inode,
332 struct ext4_extent_header *eh,
333 int depth)
334{
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400335 unsigned short entries;
336 if (eh->eh_entries == 0)
337 return 1;
338
339 entries = le16_to_cpu(eh->eh_entries);
340
341 if (depth == 0) {
342 /* leaf entries */
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400343 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400344 while (entries) {
345 if (!ext4_valid_extent(inode, ext))
346 return 0;
347 ext++;
348 entries--;
349 }
350 } else {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400351 struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400352 while (entries) {
353 if (!ext4_valid_extent_idx(inode, ext_idx))
354 return 0;
355 ext_idx++;
356 entries--;
357 }
358 }
359 return 1;
360}
361
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400362static int __ext4_ext_check(const char *function, unsigned int line,
363 struct inode *inode, struct ext4_extent_header *eh,
364 int depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400365{
366 const char *error_msg;
367 int max = 0;
368
369 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
370 error_msg = "invalid magic";
371 goto corrupted;
372 }
373 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
374 error_msg = "unexpected eh_depth";
375 goto corrupted;
376 }
377 if (unlikely(eh->eh_max == 0)) {
378 error_msg = "invalid eh_max";
379 goto corrupted;
380 }
381 max = ext4_ext_max_entries(inode, depth);
382 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
383 error_msg = "too large eh_max";
384 goto corrupted;
385 }
386 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
387 error_msg = "invalid eh_entries";
388 goto corrupted;
389 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400390 if (!ext4_valid_extent_entries(inode, eh, depth)) {
391 error_msg = "invalid extent entries";
392 goto corrupted;
393 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400394 return 0;
395
396corrupted:
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400397 ext4_error_inode(inode, function, line, 0,
Theodore Ts'o24676da2010-05-16 21:00:00 -0400398 "bad header/extent: %s - magic %x, "
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400399 "entries %u, max %u(%u), depth %u(%u)",
Theodore Ts'o24676da2010-05-16 21:00:00 -0400400 error_msg, le16_to_cpu(eh->eh_magic),
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400401 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
402 max, le16_to_cpu(eh->eh_depth), depth);
403
404 return -EIO;
405}
406
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400407#define ext4_ext_check(inode, eh, depth) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400408 __ext4_ext_check(__func__, __LINE__, inode, eh, depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400409
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400410int ext4_ext_check_inode(struct inode *inode)
411{
412 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
413}
414
Darrick J. Wongf8489122012-04-29 18:21:10 -0400415static int __ext4_ext_check_block(const char *function, unsigned int line,
416 struct inode *inode,
417 struct ext4_extent_header *eh,
418 int depth,
419 struct buffer_head *bh)
420{
421 int ret;
422
423 if (buffer_verified(bh))
424 return 0;
425 ret = ext4_ext_check(inode, eh, depth);
426 if (ret)
427 return ret;
428 set_buffer_verified(bh);
429 return ret;
430}
431
432#define ext4_ext_check_block(inode, eh, depth, bh) \
433 __ext4_ext_check_block(__func__, __LINE__, inode, eh, depth, bh)
434
Alex Tomasa86c6182006-10-11 01:21:03 -0700435#ifdef EXT_DEBUG
436static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
437{
438 int k, l = path->p_depth;
439
440 ext_debug("path:");
441 for (k = 0; k <= l; k++, path++) {
442 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700443 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400444 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700445 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400446 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700447 le32_to_cpu(path->p_ext->ee_block),
Mingming553f9002009-09-18 13:34:55 -0400448 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400449 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400450 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700451 } else
452 ext_debug(" []");
453 }
454 ext_debug("\n");
455}
456
457static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
458{
459 int depth = ext_depth(inode);
460 struct ext4_extent_header *eh;
461 struct ext4_extent *ex;
462 int i;
463
464 if (!path)
465 return;
466
467 eh = path[depth].p_hdr;
468 ex = EXT_FIRST_EXTENT(eh);
469
Mingming553f9002009-09-18 13:34:55 -0400470 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
471
Alex Tomasa86c6182006-10-11 01:21:03 -0700472 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400473 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
474 ext4_ext_is_uninitialized(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400475 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700476 }
477 ext_debug("\n");
478}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400479
480static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
481 ext4_fsblk_t newblock, int level)
482{
483 int depth = ext_depth(inode);
484 struct ext4_extent *ex;
485
486 if (depth != level) {
487 struct ext4_extent_idx *idx;
488 idx = path[level].p_idx;
489 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
490 ext_debug("%d: move %d:%llu in new index %llu\n", level,
491 le32_to_cpu(idx->ei_block),
492 ext4_idx_pblock(idx),
493 newblock);
494 idx++;
495 }
496
497 return;
498 }
499
500 ex = path[depth].p_ext;
501 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
502 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
503 le32_to_cpu(ex->ee_block),
504 ext4_ext_pblock(ex),
505 ext4_ext_is_uninitialized(ex),
506 ext4_ext_get_actual_len(ex),
507 newblock);
508 ex++;
509 }
510}
511
Alex Tomasa86c6182006-10-11 01:21:03 -0700512#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400513#define ext4_ext_show_path(inode, path)
514#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400515#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700516#endif
517
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500518void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700519{
520 int depth = path->p_depth;
521 int i;
522
523 for (i = 0; i <= depth; i++, path++)
524 if (path->p_bh) {
525 brelse(path->p_bh);
526 path->p_bh = NULL;
527 }
528}
529
530/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700531 * ext4_ext_binsearch_idx:
532 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400533 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700534 */
535static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500536ext4_ext_binsearch_idx(struct inode *inode,
537 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700538{
539 struct ext4_extent_header *eh = path->p_hdr;
540 struct ext4_extent_idx *r, *l, *m;
541
Alex Tomasa86c6182006-10-11 01:21:03 -0700542
Eric Sandeenbba90742008-01-28 23:58:27 -0500543 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700544
545 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400546 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700547 while (l <= r) {
548 m = l + (r - l) / 2;
549 if (block < le32_to_cpu(m->ei_block))
550 r = m - 1;
551 else
552 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400553 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
554 m, le32_to_cpu(m->ei_block),
555 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700556 }
557
558 path->p_idx = l - 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700559 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400560 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700561
562#ifdef CHECK_BINSEARCH
563 {
564 struct ext4_extent_idx *chix, *ix;
565 int k;
566
567 chix = ix = EXT_FIRST_INDEX(eh);
568 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
569 if (k != 0 &&
570 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o47760042008-09-08 23:00:52 -0400571 printk(KERN_DEBUG "k=%d, ix=0x%p, "
572 "first=0x%p\n", k,
573 ix, EXT_FIRST_INDEX(eh));
574 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700575 le32_to_cpu(ix->ei_block),
576 le32_to_cpu(ix[-1].ei_block));
577 }
578 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400579 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700580 if (block < le32_to_cpu(ix->ei_block))
581 break;
582 chix = ix;
583 }
584 BUG_ON(chix != path->p_idx);
585 }
586#endif
587
588}
589
590/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700591 * ext4_ext_binsearch:
592 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400593 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700594 */
595static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500596ext4_ext_binsearch(struct inode *inode,
597 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700598{
599 struct ext4_extent_header *eh = path->p_hdr;
600 struct ext4_extent *r, *l, *m;
601
Alex Tomasa86c6182006-10-11 01:21:03 -0700602 if (eh->eh_entries == 0) {
603 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700604 * this leaf is empty:
605 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700606 */
607 return;
608 }
609
Eric Sandeenbba90742008-01-28 23:58:27 -0500610 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700611
612 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400613 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700614
615 while (l <= r) {
616 m = l + (r - l) / 2;
617 if (block < le32_to_cpu(m->ee_block))
618 r = m - 1;
619 else
620 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400621 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
622 m, le32_to_cpu(m->ee_block),
623 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700624 }
625
626 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400627 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400628 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400629 ext4_ext_pblock(path->p_ext),
Mingming553f9002009-09-18 13:34:55 -0400630 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400631 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700632
633#ifdef CHECK_BINSEARCH
634 {
635 struct ext4_extent *chex, *ex;
636 int k;
637
638 chex = ex = EXT_FIRST_EXTENT(eh);
639 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
640 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400641 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700642 if (block < le32_to_cpu(ex->ee_block))
643 break;
644 chex = ex;
645 }
646 BUG_ON(chex != path->p_ext);
647 }
648#endif
649
650}
651
652int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
653{
654 struct ext4_extent_header *eh;
655
656 eh = ext_inode_hdr(inode);
657 eh->eh_depth = 0;
658 eh->eh_entries = 0;
659 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400660 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700661 ext4_mark_inode_dirty(handle, inode);
662 ext4_ext_invalidate_cache(inode);
663 return 0;
664}
665
666struct ext4_ext_path *
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500667ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
668 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700669{
670 struct ext4_extent_header *eh;
671 struct buffer_head *bh;
672 short int depth, i, ppos = 0, alloc = 0;
673
674 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400675 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700676
677 /* account possible depth increase */
678 if (!path) {
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800679 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700680 GFP_NOFS);
681 if (!path)
682 return ERR_PTR(-ENOMEM);
683 alloc = 1;
684 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700685 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400686 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700687
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400688 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700689 /* walk through the tree */
690 while (i) {
691 ext_debug("depth %d: num %d, max %d\n",
692 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400693
Alex Tomasa86c6182006-10-11 01:21:03 -0700694 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400695 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700696 path[ppos].p_depth = i;
697 path[ppos].p_ext = NULL;
698
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400699 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
700 if (unlikely(!bh))
Alex Tomasa86c6182006-10-11 01:21:03 -0700701 goto err;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400702 if (!bh_uptodate_or_lock(bh)) {
Jiaying Zhang0562e0b2011-03-21 21:38:05 -0400703 trace_ext4_ext_load_extent(inode, block,
704 path[ppos].p_block);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400705 if (bh_submit_read(bh) < 0) {
706 put_bh(bh);
707 goto err;
708 }
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400709 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700710 eh = ext_block_hdr(bh);
711 ppos++;
Frank Mayhar273df552010-03-02 11:46:09 -0500712 if (unlikely(ppos > depth)) {
713 put_bh(bh);
714 EXT4_ERROR_INODE(inode,
715 "ppos %d > depth %d", ppos, depth);
716 goto err;
717 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700718 path[ppos].p_bh = bh;
719 path[ppos].p_hdr = eh;
720 i--;
721
Darrick J. Wongf8489122012-04-29 18:21:10 -0400722 if (ext4_ext_check_block(inode, eh, i, bh))
Alex Tomasa86c6182006-10-11 01:21:03 -0700723 goto err;
724 }
725
726 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700727 path[ppos].p_ext = NULL;
728 path[ppos].p_idx = NULL;
729
Alex Tomasa86c6182006-10-11 01:21:03 -0700730 /* find extent */
731 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400732 /* if not an empty leaf */
733 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400734 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700735
736 ext4_ext_show_path(inode, path);
737
738 return path;
739
740err:
741 ext4_ext_drop_refs(path);
742 if (alloc)
743 kfree(path);
744 return ERR_PTR(-EIO);
745}
746
747/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700748 * ext4_ext_insert_index:
749 * insert new index [@logical;@ptr] into the block at @curp;
750 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700751 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400752static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
753 struct ext4_ext_path *curp,
754 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700755{
756 struct ext4_extent_idx *ix;
757 int len, err;
758
Avantika Mathur7e028972006-12-06 20:41:33 -0800759 err = ext4_ext_get_access(handle, inode, curp);
760 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700761 return err;
762
Frank Mayhar273df552010-03-02 11:46:09 -0500763 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
764 EXT4_ERROR_INODE(inode,
765 "logical %d == ei_block %d!",
766 logical, le32_to_cpu(curp->p_idx->ei_block));
767 return -EIO;
768 }
Robin Dongd4620312011-07-17 23:43:42 -0400769
770 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
771 >= le16_to_cpu(curp->p_hdr->eh_max))) {
772 EXT4_ERROR_INODE(inode,
773 "eh_entries %d >= eh_max %d!",
774 le16_to_cpu(curp->p_hdr->eh_entries),
775 le16_to_cpu(curp->p_hdr->eh_max));
776 return -EIO;
777 }
778
Alex Tomasa86c6182006-10-11 01:21:03 -0700779 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
780 /* insert after */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400781 ext_debug("insert new index %d after: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700782 ix = curp->p_idx + 1;
783 } else {
784 /* insert before */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400785 ext_debug("insert new index %d before: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700786 ix = curp->p_idx;
787 }
788
Eric Gouriou80e675f2011-10-27 11:52:18 -0400789 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
790 BUG_ON(len < 0);
791 if (len > 0) {
792 ext_debug("insert new index %d: "
793 "move %d indices from 0x%p to 0x%p\n",
794 logical, len, ix, ix + 1);
795 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
796 }
797
Tao Maf472e022011-10-17 10:13:46 -0400798 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
799 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
800 return -EIO;
801 }
802
Alex Tomasa86c6182006-10-11 01:21:03 -0700803 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700804 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400805 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700806
Frank Mayhar273df552010-03-02 11:46:09 -0500807 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
808 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
809 return -EIO;
810 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700811
812 err = ext4_ext_dirty(handle, inode, curp);
813 ext4_std_error(inode->i_sb, err);
814
815 return err;
816}
817
818/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700819 * ext4_ext_split:
820 * inserts new subtree into the path, using free index entry
821 * at depth @at:
822 * - allocates all needed blocks (new leaf and all intermediate index blocks)
823 * - makes decision where to split
824 * - moves remaining extents and index entries (right to the split point)
825 * into the newly allocated blocks
826 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700827 */
828static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -0400829 unsigned int flags,
830 struct ext4_ext_path *path,
831 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -0700832{
833 struct buffer_head *bh = NULL;
834 int depth = ext_depth(inode);
835 struct ext4_extent_header *neh;
836 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -0700837 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700838 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700839 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700840 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700841 int err = 0;
842
843 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700844 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700845
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700846 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700847 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -0500848 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
849 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
850 return -EIO;
851 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700852 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
853 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700854 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700855 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400856 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700857 } else {
858 border = newext->ee_block;
859 ext_debug("leaf will be added."
860 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400861 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700862 }
863
864 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700865 * If error occurs, then we break processing
866 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700867 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700868 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700869 */
870
871 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700872 * Get array to track all allocated blocks.
873 * We need this to handle errors and free blocks
874 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700875 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800876 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700877 if (!ablocks)
878 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700879
880 /* allocate all needed blocks */
881 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
882 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400883 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400884 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -0700885 if (newblock == 0)
886 goto cleanup;
887 ablocks[a] = newblock;
888 }
889
890 /* initialize new leaf */
891 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -0500892 if (unlikely(newblock == 0)) {
893 EXT4_ERROR_INODE(inode, "newblock == 0!");
894 err = -EIO;
895 goto cleanup;
896 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700897 bh = sb_getblk(inode->i_sb, newblock);
898 if (!bh) {
899 err = -EIO;
900 goto cleanup;
901 }
902 lock_buffer(bh);
903
Avantika Mathur7e028972006-12-06 20:41:33 -0800904 err = ext4_journal_get_create_access(handle, bh);
905 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700906 goto cleanup;
907
908 neh = ext_block_hdr(bh);
909 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400910 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700911 neh->eh_magic = EXT4_EXT_MAGIC;
912 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700913
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700914 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -0500915 if (unlikely(path[depth].p_hdr->eh_entries !=
916 path[depth].p_hdr->eh_max)) {
917 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
918 path[depth].p_hdr->eh_entries,
919 path[depth].p_hdr->eh_max);
920 err = -EIO;
921 goto cleanup;
922 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700923 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400924 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
925 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -0700926 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400927 struct ext4_extent *ex;
928 ex = EXT_FIRST_EXTENT(neh);
929 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400930 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700931 }
932
933 set_buffer_uptodate(bh);
934 unlock_buffer(bh);
935
Frank Mayhar03901312009-01-07 00:06:22 -0500936 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800937 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700938 goto cleanup;
939 brelse(bh);
940 bh = NULL;
941
942 /* correct old leaf */
943 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800944 err = ext4_ext_get_access(handle, inode, path + depth);
945 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700946 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400947 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800948 err = ext4_ext_dirty(handle, inode, path + depth);
949 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700950 goto cleanup;
951
952 }
953
954 /* create intermediate indexes */
955 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -0500956 if (unlikely(k < 0)) {
957 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
958 err = -EIO;
959 goto cleanup;
960 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700961 if (k)
962 ext_debug("create %d intermediate indices\n", k);
963 /* insert new index into current index block */
964 /* current depth stored in i var */
965 i = depth - 1;
966 while (k--) {
967 oldblock = newblock;
968 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -0500969 bh = sb_getblk(inode->i_sb, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700970 if (!bh) {
971 err = -EIO;
972 goto cleanup;
973 }
974 lock_buffer(bh);
975
Avantika Mathur7e028972006-12-06 20:41:33 -0800976 err = ext4_journal_get_create_access(handle, bh);
977 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700978 goto cleanup;
979
980 neh = ext_block_hdr(bh);
981 neh->eh_entries = cpu_to_le16(1);
982 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400983 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700984 neh->eh_depth = cpu_to_le16(depth - i);
985 fidx = EXT_FIRST_INDEX(neh);
986 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700987 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700988
Eric Sandeenbba90742008-01-28 23:58:27 -0500989 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
990 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700991
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400992 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -0500993 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
994 EXT_LAST_INDEX(path[i].p_hdr))) {
995 EXT4_ERROR_INODE(inode,
996 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
997 le32_to_cpu(path[i].p_ext->ee_block));
998 err = -EIO;
999 goto cleanup;
1000 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001001 /* start copy indexes */
1002 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
1003 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
1004 EXT_MAX_INDEX(path[i].p_hdr));
1005 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07001006 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001007 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -07001008 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001009 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001010 }
1011 set_buffer_uptodate(bh);
1012 unlock_buffer(bh);
1013
Frank Mayhar03901312009-01-07 00:06:22 -05001014 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001015 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001016 goto cleanup;
1017 brelse(bh);
1018 bh = NULL;
1019
1020 /* correct old index */
1021 if (m) {
1022 err = ext4_ext_get_access(handle, inode, path + i);
1023 if (err)
1024 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001025 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001026 err = ext4_ext_dirty(handle, inode, path + i);
1027 if (err)
1028 goto cleanup;
1029 }
1030
1031 i--;
1032 }
1033
1034 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001035 err = ext4_ext_insert_index(handle, inode, path + at,
1036 le32_to_cpu(border), newblock);
1037
1038cleanup:
1039 if (bh) {
1040 if (buffer_locked(bh))
1041 unlock_buffer(bh);
1042 brelse(bh);
1043 }
1044
1045 if (err) {
1046 /* free all allocated blocks in error case */
1047 for (i = 0; i < depth; i++) {
1048 if (!ablocks[i])
1049 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001050 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001051 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001052 }
1053 }
1054 kfree(ablocks);
1055
1056 return err;
1057}
1058
1059/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001060 * ext4_ext_grow_indepth:
1061 * implements tree growing procedure:
1062 * - allocates new block
1063 * - moves top-level data (index block or leaf) into the new block
1064 * - initializes new top-level, creating index that points to the
1065 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001066 */
1067static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001068 unsigned int flags,
Allison Henderson55f020d2011-05-25 07:41:26 -04001069 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001070{
Alex Tomasa86c6182006-10-11 01:21:03 -07001071 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001072 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001073 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001074 int err = 0;
1075
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001076 newblock = ext4_ext_new_meta_block(handle, inode, NULL,
Allison Henderson55f020d2011-05-25 07:41:26 -04001077 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001078 if (newblock == 0)
1079 return err;
1080
1081 bh = sb_getblk(inode->i_sb, newblock);
1082 if (!bh) {
1083 err = -EIO;
1084 ext4_std_error(inode->i_sb, err);
1085 return err;
1086 }
1087 lock_buffer(bh);
1088
Avantika Mathur7e028972006-12-06 20:41:33 -08001089 err = ext4_journal_get_create_access(handle, bh);
1090 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001091 unlock_buffer(bh);
1092 goto out;
1093 }
1094
1095 /* move top-level index/leaf into new block */
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001096 memmove(bh->b_data, EXT4_I(inode)->i_data,
1097 sizeof(EXT4_I(inode)->i_data));
Alex Tomasa86c6182006-10-11 01:21:03 -07001098
1099 /* set size of new block */
1100 neh = ext_block_hdr(bh);
1101 /* old root could have indexes or leaves
1102 * so calculate e_max right way */
1103 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001104 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001105 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001106 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001107 neh->eh_magic = EXT4_EXT_MAGIC;
1108 set_buffer_uptodate(bh);
1109 unlock_buffer(bh);
1110
Frank Mayhar03901312009-01-07 00:06:22 -05001111 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001112 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001113 goto out;
1114
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001115 /* Update top-level index: num,max,pointer */
Alex Tomasa86c6182006-10-11 01:21:03 -07001116 neh = ext_inode_hdr(inode);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001117 neh->eh_entries = cpu_to_le16(1);
1118 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1119 if (neh->eh_depth == 0) {
1120 /* Root extent block becomes index block */
1121 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1122 EXT_FIRST_INDEX(neh)->ei_block =
1123 EXT_FIRST_EXTENT(neh)->ee_block;
1124 }
Mingming Cao2ae02102006-10-11 01:21:11 -07001125 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001126 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001127 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001128 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001129
Paul Mackerrasb4611ab2011-12-12 11:00:56 -05001130 neh->eh_depth = cpu_to_le16(le16_to_cpu(neh->eh_depth) + 1);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001131 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07001132out:
1133 brelse(bh);
1134
1135 return err;
1136}
1137
1138/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001139 * ext4_ext_create_new_leaf:
1140 * finds empty index and adds new leaf.
1141 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001142 */
1143static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001144 unsigned int flags,
1145 struct ext4_ext_path *path,
1146 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001147{
1148 struct ext4_ext_path *curp;
1149 int depth, i, err = 0;
1150
1151repeat:
1152 i = depth = ext_depth(inode);
1153
1154 /* walk up to the tree and look for free index entry */
1155 curp = path + depth;
1156 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1157 i--;
1158 curp--;
1159 }
1160
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001161 /* we use already allocated block for index block,
1162 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001163 if (EXT_HAS_FREE_INDEX(curp)) {
1164 /* if we found index with free entry, then use that
1165 * entry: create all needed subtree and add new leaf */
Allison Henderson55f020d2011-05-25 07:41:26 -04001166 err = ext4_ext_split(handle, inode, flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001167 if (err)
1168 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001169
1170 /* refill path */
1171 ext4_ext_drop_refs(path);
1172 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001173 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1174 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001175 if (IS_ERR(path))
1176 err = PTR_ERR(path);
1177 } else {
1178 /* tree is full, time to grow in depth */
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001179 err = ext4_ext_grow_indepth(handle, inode, flags, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001180 if (err)
1181 goto out;
1182
1183 /* refill path */
1184 ext4_ext_drop_refs(path);
1185 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001186 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1187 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001188 if (IS_ERR(path)) {
1189 err = PTR_ERR(path);
1190 goto out;
1191 }
1192
1193 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001194 * only first (depth 0 -> 1) produces free space;
1195 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001196 */
1197 depth = ext_depth(inode);
1198 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001199 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001200 goto repeat;
1201 }
1202 }
1203
1204out:
1205 return err;
1206}
1207
1208/*
Alex Tomas1988b512008-01-28 23:58:27 -05001209 * search the closest allocated block to the left for *logical
1210 * and returns it at @logical + it's physical address at @phys
1211 * if *logical is the smallest allocated block, the function
1212 * returns 0 at @phys
1213 * return value contains 0 (success) or error code
1214 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001215static int ext4_ext_search_left(struct inode *inode,
1216 struct ext4_ext_path *path,
1217 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001218{
1219 struct ext4_extent_idx *ix;
1220 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001221 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001222
Frank Mayhar273df552010-03-02 11:46:09 -05001223 if (unlikely(path == NULL)) {
1224 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1225 return -EIO;
1226 }
Alex Tomas1988b512008-01-28 23:58:27 -05001227 depth = path->p_depth;
1228 *phys = 0;
1229
1230 if (depth == 0 && path->p_ext == NULL)
1231 return 0;
1232
1233 /* usually extent in the path covers blocks smaller
1234 * then *logical, but it can be that extent is the
1235 * first one in the file */
1236
1237 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001238 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001239 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001240 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1241 EXT4_ERROR_INODE(inode,
1242 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1243 *logical, le32_to_cpu(ex->ee_block));
1244 return -EIO;
1245 }
Alex Tomas1988b512008-01-28 23:58:27 -05001246 while (--depth >= 0) {
1247 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001248 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1249 EXT4_ERROR_INODE(inode,
1250 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
Tao Ma6ee3b212011-10-08 16:08:34 -04001251 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001252 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
Tao Ma6ee3b212011-10-08 16:08:34 -04001253 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001254 depth);
1255 return -EIO;
1256 }
Alex Tomas1988b512008-01-28 23:58:27 -05001257 }
1258 return 0;
1259 }
1260
Frank Mayhar273df552010-03-02 11:46:09 -05001261 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1262 EXT4_ERROR_INODE(inode,
1263 "logical %d < ee_block %d + ee_len %d!",
1264 *logical, le32_to_cpu(ex->ee_block), ee_len);
1265 return -EIO;
1266 }
Alex Tomas1988b512008-01-28 23:58:27 -05001267
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001268 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001269 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001270 return 0;
1271}
1272
1273/*
1274 * search the closest allocated block to the right for *logical
1275 * and returns it at @logical + it's physical address at @phys
Tao Madf3ab172011-10-08 15:53:49 -04001276 * if *logical is the largest allocated block, the function
Alex Tomas1988b512008-01-28 23:58:27 -05001277 * returns 0 at @phys
1278 * return value contains 0 (success) or error code
1279 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001280static int ext4_ext_search_right(struct inode *inode,
1281 struct ext4_ext_path *path,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001282 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1283 struct ext4_extent **ret_ex)
Alex Tomas1988b512008-01-28 23:58:27 -05001284{
1285 struct buffer_head *bh = NULL;
1286 struct ext4_extent_header *eh;
1287 struct ext4_extent_idx *ix;
1288 struct ext4_extent *ex;
1289 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001290 int depth; /* Note, NOT eh_depth; depth from top of tree */
1291 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001292
Frank Mayhar273df552010-03-02 11:46:09 -05001293 if (unlikely(path == NULL)) {
1294 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1295 return -EIO;
1296 }
Alex Tomas1988b512008-01-28 23:58:27 -05001297 depth = path->p_depth;
1298 *phys = 0;
1299
1300 if (depth == 0 && path->p_ext == NULL)
1301 return 0;
1302
1303 /* usually extent in the path covers blocks smaller
1304 * then *logical, but it can be that extent is the
1305 * first one in the file */
1306
1307 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001308 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001309 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001310 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1311 EXT4_ERROR_INODE(inode,
1312 "first_extent(path[%d].p_hdr) != ex",
1313 depth);
1314 return -EIO;
1315 }
Alex Tomas1988b512008-01-28 23:58:27 -05001316 while (--depth >= 0) {
1317 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001318 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1319 EXT4_ERROR_INODE(inode,
1320 "ix != EXT_FIRST_INDEX *logical %d!",
1321 *logical);
1322 return -EIO;
1323 }
Alex Tomas1988b512008-01-28 23:58:27 -05001324 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001325 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001326 }
1327
Frank Mayhar273df552010-03-02 11:46:09 -05001328 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1329 EXT4_ERROR_INODE(inode,
1330 "logical %d < ee_block %d + ee_len %d!",
1331 *logical, le32_to_cpu(ex->ee_block), ee_len);
1332 return -EIO;
1333 }
Alex Tomas1988b512008-01-28 23:58:27 -05001334
1335 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1336 /* next allocated block in this leaf */
1337 ex++;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001338 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001339 }
1340
1341 /* go up and search for index to the right */
1342 while (--depth >= 0) {
1343 ix = path[depth].p_idx;
1344 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001345 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001346 }
1347
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001348 /* we've gone up to the root and found no index to the right */
1349 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001350
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001351got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001352 /* we've found index to the right, let's
1353 * follow it and find the closest allocated
1354 * block to the right */
1355 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001356 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001357 while (++depth < path->p_depth) {
1358 bh = sb_bread(inode->i_sb, block);
1359 if (bh == NULL)
1360 return -EIO;
1361 eh = ext_block_hdr(bh);
Eric Sandeen395a87b2009-03-10 18:18:47 -04001362 /* subtract from p_depth to get proper eh_depth */
Darrick J. Wongf8489122012-04-29 18:21:10 -04001363 if (ext4_ext_check_block(inode, eh,
1364 path->p_depth - depth, bh)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001365 put_bh(bh);
1366 return -EIO;
1367 }
1368 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001369 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001370 put_bh(bh);
1371 }
1372
1373 bh = sb_bread(inode->i_sb, block);
1374 if (bh == NULL)
1375 return -EIO;
1376 eh = ext_block_hdr(bh);
Darrick J. Wongf8489122012-04-29 18:21:10 -04001377 if (ext4_ext_check_block(inode, eh, path->p_depth - depth, bh)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001378 put_bh(bh);
1379 return -EIO;
1380 }
1381 ex = EXT_FIRST_EXTENT(eh);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001382found_extent:
Alex Tomas1988b512008-01-28 23:58:27 -05001383 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001384 *phys = ext4_ext_pblock(ex);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001385 *ret_ex = ex;
1386 if (bh)
1387 put_bh(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001388 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001389}
1390
1391/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001392 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001393 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001394 * NOTE: it considers block number from index entry as
1395 * allocated block. Thus, index entries have to be consistent
1396 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001397 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001398static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001399ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1400{
1401 int depth;
1402
1403 BUG_ON(path == NULL);
1404 depth = path->p_depth;
1405
1406 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001407 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001408
1409 while (depth >= 0) {
1410 if (depth == path->p_depth) {
1411 /* leaf */
Curt Wohlgemuth6f8ff532011-10-26 04:38:59 -04001412 if (path[depth].p_ext &&
1413 path[depth].p_ext !=
Alex Tomasa86c6182006-10-11 01:21:03 -07001414 EXT_LAST_EXTENT(path[depth].p_hdr))
1415 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1416 } else {
1417 /* index */
1418 if (path[depth].p_idx !=
1419 EXT_LAST_INDEX(path[depth].p_hdr))
1420 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1421 }
1422 depth--;
1423 }
1424
Lukas Czernerf17722f2011-06-06 00:05:17 -04001425 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001426}
1427
1428/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001429 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001430 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001431 */
Robin Dong57187892011-07-23 21:49:07 -04001432static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001433{
1434 int depth;
1435
1436 BUG_ON(path == NULL);
1437 depth = path->p_depth;
1438
1439 /* zero-tree has no leaf blocks at all */
1440 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001441 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001442
1443 /* go to index block */
1444 depth--;
1445
1446 while (depth >= 0) {
1447 if (path[depth].p_idx !=
1448 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001449 return (ext4_lblk_t)
1450 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001451 depth--;
1452 }
1453
Lukas Czernerf17722f2011-06-06 00:05:17 -04001454 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001455}
1456
1457/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001458 * ext4_ext_correct_indexes:
1459 * if leaf gets modified and modified extent is first in the leaf,
1460 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001461 * TODO: do we need to correct tree in all cases?
1462 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001463static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001464 struct ext4_ext_path *path)
1465{
1466 struct ext4_extent_header *eh;
1467 int depth = ext_depth(inode);
1468 struct ext4_extent *ex;
1469 __le32 border;
1470 int k, err = 0;
1471
1472 eh = path[depth].p_hdr;
1473 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001474
1475 if (unlikely(ex == NULL || eh == NULL)) {
1476 EXT4_ERROR_INODE(inode,
1477 "ex %p == NULL or eh %p == NULL", ex, eh);
1478 return -EIO;
1479 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001480
1481 if (depth == 0) {
1482 /* there is no tree at all */
1483 return 0;
1484 }
1485
1486 if (ex != EXT_FIRST_EXTENT(eh)) {
1487 /* we correct tree if first leaf got modified only */
1488 return 0;
1489 }
1490
1491 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001492 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001493 */
1494 k = depth - 1;
1495 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001496 err = ext4_ext_get_access(handle, inode, path + k);
1497 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001498 return err;
1499 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001500 err = ext4_ext_dirty(handle, inode, path + k);
1501 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001502 return err;
1503
1504 while (k--) {
1505 /* change all left-side indexes */
1506 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1507 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001508 err = ext4_ext_get_access(handle, inode, path + k);
1509 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001510 break;
1511 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001512 err = ext4_ext_dirty(handle, inode, path + k);
1513 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001514 break;
1515 }
1516
1517 return err;
1518}
1519
Akira Fujita748de672009-06-17 19:24:03 -04001520int
Alex Tomasa86c6182006-10-11 01:21:03 -07001521ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1522 struct ext4_extent *ex2)
1523{
Amit Arora749269f2007-07-18 09:02:56 -04001524 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001525
1526 /*
1527 * Make sure that either both extents are uninitialized, or
1528 * both are _not_.
1529 */
1530 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1531 return 0;
1532
Amit Arora749269f2007-07-18 09:02:56 -04001533 if (ext4_ext_is_uninitialized(ex1))
1534 max_len = EXT_UNINIT_MAX_LEN;
1535 else
1536 max_len = EXT_INIT_MAX_LEN;
1537
Amit Aroraa2df2a62007-07-17 21:42:41 -04001538 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1539 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1540
1541 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001542 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001543 return 0;
1544
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001545 /*
1546 * To allow future support for preallocated extents to be added
1547 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001548 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001549 */
Amit Arora749269f2007-07-18 09:02:56 -04001550 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001551 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001552#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001553 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001554 return 0;
1555#endif
1556
Theodore Ts'obf89d162010-10-27 21:30:14 -04001557 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001558 return 1;
1559 return 0;
1560}
1561
1562/*
Amit Arora56055d32007-07-17 21:42:38 -04001563 * This function tries to merge the "ex" extent to the next extent in the tree.
1564 * It always tries to merge towards right. If you want to merge towards
1565 * left, pass "ex - 1" as argument instead of "ex".
1566 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1567 * 1 if they got merged.
1568 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001569static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001570 struct ext4_ext_path *path,
1571 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001572{
1573 struct ext4_extent_header *eh;
1574 unsigned int depth, len;
1575 int merge_done = 0;
1576 int uninitialized = 0;
1577
1578 depth = ext_depth(inode);
1579 BUG_ON(path[depth].p_hdr == NULL);
1580 eh = path[depth].p_hdr;
1581
1582 while (ex < EXT_LAST_EXTENT(eh)) {
1583 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1584 break;
1585 /* merge with next extent! */
1586 if (ext4_ext_is_uninitialized(ex))
1587 uninitialized = 1;
1588 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1589 + ext4_ext_get_actual_len(ex + 1));
1590 if (uninitialized)
1591 ext4_ext_mark_uninitialized(ex);
1592
1593 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1594 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1595 * sizeof(struct ext4_extent);
1596 memmove(ex + 1, ex + 2, len);
1597 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001598 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001599 merge_done = 1;
1600 WARN_ON(eh->eh_entries == 0);
1601 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001602 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001603 }
1604
1605 return merge_done;
1606}
1607
1608/*
Yongqiang Yang197217a2011-05-03 11:45:29 -04001609 * This function tries to merge the @ex extent to neighbours in the tree.
1610 * return 1 if merge left else 0.
1611 */
1612static int ext4_ext_try_to_merge(struct inode *inode,
1613 struct ext4_ext_path *path,
1614 struct ext4_extent *ex) {
1615 struct ext4_extent_header *eh;
1616 unsigned int depth;
1617 int merge_done = 0;
1618 int ret = 0;
1619
1620 depth = ext_depth(inode);
1621 BUG_ON(path[depth].p_hdr == NULL);
1622 eh = path[depth].p_hdr;
1623
1624 if (ex > EXT_FIRST_EXTENT(eh))
1625 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1626
1627 if (!merge_done)
1628 ret = ext4_ext_try_to_merge_right(inode, path, ex);
1629
1630 return ret;
1631}
1632
1633/*
Amit Arora25d14f92007-05-24 13:04:13 -04001634 * check if a portion of the "newext" extent overlaps with an
1635 * existing extent.
1636 *
1637 * If there is an overlap discovered, it updates the length of the newext
1638 * such that there will be no overlap, and then returns 1.
1639 * If there is no overlap found, it returns 0.
1640 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001641static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1642 struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001643 struct ext4_extent *newext,
1644 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001645{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001646 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001647 unsigned int depth, len1;
1648 unsigned int ret = 0;
1649
1650 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001651 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001652 depth = ext_depth(inode);
1653 if (!path[depth].p_ext)
1654 goto out;
1655 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001656 b2 &= ~(sbi->s_cluster_ratio - 1);
Amit Arora25d14f92007-05-24 13:04:13 -04001657
1658 /*
1659 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001660 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001661 */
1662 if (b2 < b1) {
1663 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001664 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001665 goto out;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001666 b2 &= ~(sbi->s_cluster_ratio - 1);
Amit Arora25d14f92007-05-24 13:04:13 -04001667 }
1668
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001669 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001670 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001671 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001672 newext->ee_len = cpu_to_le16(len1);
1673 ret = 1;
1674 }
1675
1676 /* check for overlap */
1677 if (b1 + len1 > b2) {
1678 newext->ee_len = cpu_to_le16(b2 - b1);
1679 ret = 1;
1680 }
1681out:
1682 return ret;
1683}
1684
1685/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001686 * ext4_ext_insert_extent:
1687 * tries to merge requsted extent into the existing extent or
1688 * inserts requested extent as new one into the tree,
1689 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001690 */
1691int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1692 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04001693 struct ext4_extent *newext, int flag)
Alex Tomasa86c6182006-10-11 01:21:03 -07001694{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001695 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001696 struct ext4_extent *ex, *fex;
1697 struct ext4_extent *nearex; /* nearest extent */
1698 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001699 int depth, len, err;
1700 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001701 unsigned uninitialized = 0;
Allison Henderson55f020d2011-05-25 07:41:26 -04001702 int flags = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001703
Frank Mayhar273df552010-03-02 11:46:09 -05001704 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1705 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1706 return -EIO;
1707 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001708 depth = ext_depth(inode);
1709 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001710 if (unlikely(path[depth].p_hdr == NULL)) {
1711 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1712 return -EIO;
1713 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001714
1715 /* try to insert block into found extent and return */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001716 if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
Mingming Cao00314622009-09-28 15:49:08 -04001717 && ext4_can_extents_be_merged(inode, ex, newext)) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04001718 ext_debug("append [%d]%d block to %u:[%d]%d (from %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04001719 ext4_ext_is_uninitialized(newext),
1720 ext4_ext_get_actual_len(newext),
1721 le32_to_cpu(ex->ee_block),
1722 ext4_ext_is_uninitialized(ex),
1723 ext4_ext_get_actual_len(ex),
1724 ext4_ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001725 err = ext4_ext_get_access(handle, inode, path + depth);
1726 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001727 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001728
1729 /*
1730 * ext4_can_extents_be_merged should have checked that either
1731 * both extents are uninitialized, or both aren't. Thus we
1732 * need to check only one of them here.
1733 */
1734 if (ext4_ext_is_uninitialized(ex))
1735 uninitialized = 1;
1736 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1737 + ext4_ext_get_actual_len(newext));
1738 if (uninitialized)
1739 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001740 eh = path[depth].p_hdr;
1741 nearex = ex;
1742 goto merge;
1743 }
1744
Alex Tomasa86c6182006-10-11 01:21:03 -07001745 depth = ext_depth(inode);
1746 eh = path[depth].p_hdr;
1747 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1748 goto has_space;
1749
1750 /* probably next leaf has space for us? */
1751 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04001752 next = EXT_MAX_BLOCKS;
1753 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
Robin Dong57187892011-07-23 21:49:07 -04001754 next = ext4_ext_next_leaf_block(path);
Robin Dong598dbdf2011-07-11 18:24:01 -04001755 if (next != EXT_MAX_BLOCKS) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04001756 ext_debug("next leaf block - %u\n", next);
Alex Tomasa86c6182006-10-11 01:21:03 -07001757 BUG_ON(npath != NULL);
1758 npath = ext4_ext_find_extent(inode, next, NULL);
1759 if (IS_ERR(npath))
1760 return PTR_ERR(npath);
1761 BUG_ON(npath->p_depth != path->p_depth);
1762 eh = npath[depth].p_hdr;
1763 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001764 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001765 le16_to_cpu(eh->eh_entries));
1766 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04001767 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07001768 }
1769 ext_debug("next leaf has no free space(%d,%d)\n",
1770 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1771 }
1772
1773 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001774 * There is no free space in the found leaf.
1775 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001776 */
Allison Henderson55f020d2011-05-25 07:41:26 -04001777 if (flag & EXT4_GET_BLOCKS_PUNCH_OUT_EXT)
1778 flags = EXT4_MB_USE_ROOT_BLOCKS;
1779 err = ext4_ext_create_new_leaf(handle, inode, flags, path, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001780 if (err)
1781 goto cleanup;
1782 depth = ext_depth(inode);
1783 eh = path[depth].p_hdr;
1784
1785has_space:
1786 nearex = path[depth].p_ext;
1787
Avantika Mathur7e028972006-12-06 20:41:33 -08001788 err = ext4_ext_get_access(handle, inode, path + depth);
1789 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001790 goto cleanup;
1791
1792 if (!nearex) {
1793 /* there is no extent in this leaf, create first one */
Yongqiang Yang32de6752011-11-01 18:56:41 -04001794 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001795 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001796 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001797 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001798 ext4_ext_get_actual_len(newext));
Eric Gouriou80e675f2011-10-27 11:52:18 -04001799 nearex = EXT_FIRST_EXTENT(eh);
1800 } else {
1801 if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001802 > le32_to_cpu(nearex->ee_block)) {
Eric Gouriou80e675f2011-10-27 11:52:18 -04001803 /* Insert after */
Yongqiang Yang32de6752011-11-01 18:56:41 -04001804 ext_debug("insert %u:%llu:[%d]%d before: "
1805 "nearest %p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001806 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001807 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001808 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001809 ext4_ext_get_actual_len(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04001810 nearex);
1811 nearex++;
1812 } else {
1813 /* Insert before */
1814 BUG_ON(newext->ee_block == nearex->ee_block);
Yongqiang Yang32de6752011-11-01 18:56:41 -04001815 ext_debug("insert %u:%llu:[%d]%d after: "
1816 "nearest %p\n",
Eric Gouriou80e675f2011-10-27 11:52:18 -04001817 le32_to_cpu(newext->ee_block),
1818 ext4_ext_pblock(newext),
1819 ext4_ext_is_uninitialized(newext),
1820 ext4_ext_get_actual_len(newext),
1821 nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001822 }
Eric Gouriou80e675f2011-10-27 11:52:18 -04001823 len = EXT_LAST_EXTENT(eh) - nearex + 1;
1824 if (len > 0) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04001825 ext_debug("insert %u:%llu:[%d]%d: "
Eric Gouriou80e675f2011-10-27 11:52:18 -04001826 "move %d extents from 0x%p to 0x%p\n",
1827 le32_to_cpu(newext->ee_block),
1828 ext4_ext_pblock(newext),
1829 ext4_ext_is_uninitialized(newext),
1830 ext4_ext_get_actual_len(newext),
1831 len, nearex, nearex + 1);
1832 memmove(nearex + 1, nearex,
1833 len * sizeof(struct ext4_extent));
1834 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001835 }
1836
Marcin Slusarze8546d02008-04-17 10:38:59 -04001837 le16_add_cpu(&eh->eh_entries, 1);
Eric Gouriou80e675f2011-10-27 11:52:18 -04001838 path[depth].p_ext = nearex;
Alex Tomasa86c6182006-10-11 01:21:03 -07001839 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001840 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001841 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001842
1843merge:
1844 /* try to merge extents to the right */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001845 if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
Mingming Cao00314622009-09-28 15:49:08 -04001846 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001847
1848 /* try to merge extents to the left */
1849
1850 /* time to correct all indexes above */
1851 err = ext4_ext_correct_indexes(handle, inode, path);
1852 if (err)
1853 goto cleanup;
1854
1855 err = ext4_ext_dirty(handle, inode, path + depth);
1856
1857cleanup:
1858 if (npath) {
1859 ext4_ext_drop_refs(npath);
1860 kfree(npath);
1861 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001862 ext4_ext_invalidate_cache(inode);
1863 return err;
1864}
1865
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001866static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1867 ext4_lblk_t num, ext_prepare_callback func,
1868 void *cbdata)
Eric Sandeen6873fa02008-10-07 00:46:36 -04001869{
1870 struct ext4_ext_path *path = NULL;
1871 struct ext4_ext_cache cbex;
1872 struct ext4_extent *ex;
1873 ext4_lblk_t next, start = 0, end = 0;
1874 ext4_lblk_t last = block + num;
1875 int depth, exists, err = 0;
1876
1877 BUG_ON(func == NULL);
1878 BUG_ON(inode == NULL);
1879
Lukas Czernerf17722f2011-06-06 00:05:17 -04001880 while (block < last && block != EXT_MAX_BLOCKS) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04001881 num = last - block;
1882 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001883 down_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001884 path = ext4_ext_find_extent(inode, block, path);
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001885 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001886 if (IS_ERR(path)) {
1887 err = PTR_ERR(path);
1888 path = NULL;
1889 break;
1890 }
1891
1892 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05001893 if (unlikely(path[depth].p_hdr == NULL)) {
1894 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1895 err = -EIO;
1896 break;
1897 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04001898 ex = path[depth].p_ext;
1899 next = ext4_ext_next_allocated_block(path);
1900
1901 exists = 0;
1902 if (!ex) {
1903 /* there is no extent yet, so try to allocate
1904 * all requested space */
1905 start = block;
1906 end = block + num;
1907 } else if (le32_to_cpu(ex->ee_block) > block) {
1908 /* need to allocate space before found extent */
1909 start = block;
1910 end = le32_to_cpu(ex->ee_block);
1911 if (block + num < end)
1912 end = block + num;
1913 } else if (block >= le32_to_cpu(ex->ee_block)
1914 + ext4_ext_get_actual_len(ex)) {
1915 /* need to allocate space after found extent */
1916 start = block;
1917 end = block + num;
1918 if (end >= next)
1919 end = next;
1920 } else if (block >= le32_to_cpu(ex->ee_block)) {
1921 /*
1922 * some part of requested space is covered
1923 * by found extent
1924 */
1925 start = block;
1926 end = le32_to_cpu(ex->ee_block)
1927 + ext4_ext_get_actual_len(ex);
1928 if (block + num < end)
1929 end = block + num;
1930 exists = 1;
1931 } else {
1932 BUG();
1933 }
1934 BUG_ON(end <= start);
1935
1936 if (!exists) {
1937 cbex.ec_block = start;
1938 cbex.ec_len = end - start;
1939 cbex.ec_start = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04001940 } else {
1941 cbex.ec_block = le32_to_cpu(ex->ee_block);
1942 cbex.ec_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001943 cbex.ec_start = ext4_ext_pblock(ex);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001944 }
1945
Frank Mayhar273df552010-03-02 11:46:09 -05001946 if (unlikely(cbex.ec_len == 0)) {
1947 EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
1948 err = -EIO;
1949 break;
1950 }
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04001951 err = func(inode, next, &cbex, ex, cbdata);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001952 ext4_ext_drop_refs(path);
1953
1954 if (err < 0)
1955 break;
1956
1957 if (err == EXT_REPEAT)
1958 continue;
1959 else if (err == EXT_BREAK) {
1960 err = 0;
1961 break;
1962 }
1963
1964 if (ext_depth(inode) != depth) {
1965 /* depth was changed. we have to realloc path */
1966 kfree(path);
1967 path = NULL;
1968 }
1969
1970 block = cbex.ec_block + cbex.ec_len;
1971 }
1972
1973 if (path) {
1974 ext4_ext_drop_refs(path);
1975 kfree(path);
1976 }
1977
1978 return err;
1979}
1980
Avantika Mathur09b88252006-12-06 20:41:36 -08001981static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001982ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05001983 __u32 len, ext4_fsblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07001984{
1985 struct ext4_ext_cache *cex;
1986 BUG_ON(len == 0);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001987 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Aditya Kalid8990242011-09-09 19:18:51 -04001988 trace_ext4_ext_put_in_cache(inode, block, len, start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001989 cex = &EXT4_I(inode)->i_cached_extent;
Alex Tomasa86c6182006-10-11 01:21:03 -07001990 cex->ec_block = block;
1991 cex->ec_len = len;
1992 cex->ec_start = start;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001993 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001994}
1995
1996/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001997 * ext4_ext_put_gap_in_cache:
1998 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07001999 * and cache this gap
2000 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002001static void
Alex Tomasa86c6182006-10-11 01:21:03 -07002002ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002003 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07002004{
2005 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002006 unsigned long len;
2007 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07002008 struct ext4_extent *ex;
2009
2010 ex = path[depth].p_ext;
2011 if (ex == NULL) {
2012 /* there is no extent yet, so gap is [0;-] */
2013 lblock = 0;
Lukas Czernerf17722f2011-06-06 00:05:17 -04002014 len = EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07002015 ext_debug("cache gap(whole file):");
2016 } else if (block < le32_to_cpu(ex->ee_block)) {
2017 lblock = block;
2018 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05002019 ext_debug("cache gap(before): %u [%u:%u]",
2020 block,
2021 le32_to_cpu(ex->ee_block),
2022 ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002023 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002024 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002025 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002026 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002027 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002028
2029 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05002030 ext_debug("cache gap(after): [%u:%u] %u",
2031 le32_to_cpu(ex->ee_block),
2032 ext4_ext_get_actual_len(ex),
2033 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002034 BUG_ON(next == lblock);
2035 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07002036 } else {
2037 lblock = len = 0;
2038 BUG();
2039 }
2040
Eric Sandeenbba90742008-01-28 23:58:27 -05002041 ext_debug(" -> %u:%lu\n", lblock, len);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002042 ext4_ext_put_in_cache(inode, lblock, len, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002043}
2044
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002045/*
Robin Dongb7ca1e82011-07-23 21:53:25 -04002046 * ext4_ext_check_cache()
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002047 * Checks to see if the given block is in the cache.
2048 * If it is, the cached extent is stored in the given
2049 * cache extent pointer. If the cached extent is a hole,
2050 * this routine should be used instead of
2051 * ext4_ext_in_cache if the calling function needs to
2052 * know the size of the hole.
2053 *
2054 * @inode: The files inode
2055 * @block: The block to look for in the cache
2056 * @ex: Pointer where the cached extent will be stored
2057 * if it contains block
2058 *
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002059 * Return 0 if cache is invalid; 1 if the cache is valid
2060 */
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002061static int ext4_ext_check_cache(struct inode *inode, ext4_lblk_t block,
2062 struct ext4_ext_cache *ex){
Alex Tomasa86c6182006-10-11 01:21:03 -07002063 struct ext4_ext_cache *cex;
Vivek Haldar77f41352011-05-22 21:24:16 -04002064 struct ext4_sb_info *sbi;
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002065 int ret = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002066
Theodore Ts'o60e66792010-05-17 07:00:00 -04002067 /*
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002068 * We borrow i_block_reservation_lock to protect i_cached_extent
2069 */
2070 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07002071 cex = &EXT4_I(inode)->i_cached_extent;
Vivek Haldar77f41352011-05-22 21:24:16 -04002072 sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002073
2074 /* has cache valid data? */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002075 if (cex->ec_len == 0)
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002076 goto errout;
Alex Tomasa86c6182006-10-11 01:21:03 -07002077
Akinobu Mita731eb1a2010-03-03 23:55:01 -05002078 if (in_range(block, cex->ec_block, cex->ec_len)) {
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002079 memcpy(ex, cex, sizeof(struct ext4_ext_cache));
Eric Sandeenbba90742008-01-28 23:58:27 -05002080 ext_debug("%u cached by %u:%u:%llu\n",
2081 block,
2082 cex->ec_block, cex->ec_len, cex->ec_start);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002083 ret = 1;
Alex Tomasa86c6182006-10-11 01:21:03 -07002084 }
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002085errout:
Aditya Kalid8990242011-09-09 19:18:51 -04002086 trace_ext4_ext_in_cache(inode, block, ret);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002087 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
2088 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07002089}
2090
2091/*
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002092 * ext4_ext_in_cache()
2093 * Checks to see if the given block is in the cache.
2094 * If it is, the cached extent is stored in the given
2095 * extent pointer.
2096 *
2097 * @inode: The files inode
2098 * @block: The block to look for in the cache
2099 * @ex: Pointer where the cached extent will be stored
2100 * if it contains block
2101 *
2102 * Return 0 if cache is invalid; 1 if the cache is valid
2103 */
2104static int
2105ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
2106 struct ext4_extent *ex)
2107{
2108 struct ext4_ext_cache cex;
2109 int ret = 0;
2110
2111 if (ext4_ext_check_cache(inode, block, &cex)) {
2112 ex->ee_block = cpu_to_le32(cex.ec_block);
2113 ext4_ext_store_pblock(ex, cex.ec_start);
2114 ex->ee_len = cpu_to_le16(cex.ec_len);
2115 ret = 1;
2116 }
2117
2118 return ret;
2119}
2120
2121
2122/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002123 * ext4_ext_rm_idx:
2124 * removes index from the index block.
Alex Tomasa86c6182006-10-11 01:21:03 -07002125 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002126static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07002127 struct ext4_ext_path *path)
2128{
Alex Tomasa86c6182006-10-11 01:21:03 -07002129 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002130 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002131
2132 /* free index block */
2133 path--;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002134 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002135 if (unlikely(path->p_hdr->eh_entries == 0)) {
2136 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2137 return -EIO;
2138 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002139 err = ext4_ext_get_access(handle, inode, path);
2140 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002141 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002142
2143 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2144 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2145 len *= sizeof(struct ext4_extent_idx);
2146 memmove(path->p_idx, path->p_idx + 1, len);
2147 }
2148
Marcin Slusarze8546d02008-04-17 10:38:59 -04002149 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002150 err = ext4_ext_dirty(handle, inode, path);
2151 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002152 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002153 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Aditya Kalid8990242011-09-09 19:18:51 -04002154 trace_ext4_ext_rm_idx(inode, leaf);
2155
Peter Huewe7dc57612011-02-21 21:01:42 -05002156 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002157 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Alex Tomasa86c6182006-10-11 01:21:03 -07002158 return err;
2159}
2160
2161/*
Mingming Caoee12b632008-08-19 22:16:05 -04002162 * ext4_ext_calc_credits_for_single_extent:
2163 * This routine returns max. credits that needed to insert an extent
2164 * to the extent tree.
2165 * When pass the actual path, the caller should calculate credits
2166 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002167 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002168int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002169 struct ext4_ext_path *path)
2170{
Alex Tomasa86c6182006-10-11 01:21:03 -07002171 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002172 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002173 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002174
Alex Tomasa86c6182006-10-11 01:21:03 -07002175 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002176 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002177 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2178
2179 /*
2180 * There are some space in the leaf tree, no
2181 * need to account for leaf block credit
2182 *
2183 * bitmaps and block group descriptor blocks
Tao Madf3ab172011-10-08 15:53:49 -04002184 * and other metadata blocks still need to be
Mingming Caoee12b632008-08-19 22:16:05 -04002185 * accounted.
2186 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002187 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002188 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002189 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002190 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002191 }
2192
Mingming Cao525f4ed2008-08-19 22:15:58 -04002193 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002194}
Alex Tomasa86c6182006-10-11 01:21:03 -07002195
Mingming Caoee12b632008-08-19 22:16:05 -04002196/*
2197 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2198 *
2199 * if nrblocks are fit in a single extent (chunk flag is 1), then
2200 * in the worse case, each tree level index/leaf need to be changed
2201 * if the tree split due to insert a new extent, then the old tree
2202 * index/leaf need to be updated too
2203 *
2204 * If the nrblocks are discontiguous, they could cause
2205 * the whole tree split more than once, but this is really rare.
2206 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002207int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoee12b632008-08-19 22:16:05 -04002208{
2209 int index;
2210 int depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002211
Mingming Caoee12b632008-08-19 22:16:05 -04002212 if (chunk)
2213 index = depth * 2;
2214 else
2215 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002216
Mingming Caoee12b632008-08-19 22:16:05 -04002217 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002218}
2219
2220static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002221 struct ext4_extent *ex,
2222 ext4_fsblk_t *partial_cluster,
2223 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002224{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002225 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002226 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002227 ext4_fsblk_t pblk;
Theodore Ts'oe6362602009-11-23 07:17:05 -05002228 int flags = EXT4_FREE_BLOCKS_FORGET;
Alex Tomasa86c6182006-10-11 01:21:03 -07002229
Alex Tomasc9de5602008-01-29 00:19:52 -05002230 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
Theodore Ts'oe6362602009-11-23 07:17:05 -05002231 flags |= EXT4_FREE_BLOCKS_METADATA;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002232 /*
2233 * For bigalloc file systems, we never free a partial cluster
2234 * at the beginning of the extent. Instead, we make a note
2235 * that we tried freeing the cluster, and check to see if we
2236 * need to free it on a subsequent call to ext4_remove_blocks,
2237 * or at the end of the ext4_truncate() operation.
2238 */
2239 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
2240
Aditya Kalid8990242011-09-09 19:18:51 -04002241 trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002242 /*
2243 * If we have a partial cluster, and it's different from the
2244 * cluster of the last block, we need to explicitly free the
2245 * partial cluster here.
2246 */
2247 pblk = ext4_ext_pblock(ex) + ee_len - 1;
2248 if (*partial_cluster && (EXT4_B2C(sbi, pblk) != *partial_cluster)) {
2249 ext4_free_blocks(handle, inode, NULL,
2250 EXT4_C2B(sbi, *partial_cluster),
2251 sbi->s_cluster_ratio, flags);
2252 *partial_cluster = 0;
2253 }
2254
Alex Tomasa86c6182006-10-11 01:21:03 -07002255#ifdef EXTENTS_STATS
2256 {
2257 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002258 spin_lock(&sbi->s_ext_stats_lock);
2259 sbi->s_ext_blocks += ee_len;
2260 sbi->s_ext_extents++;
2261 if (ee_len < sbi->s_ext_min)
2262 sbi->s_ext_min = ee_len;
2263 if (ee_len > sbi->s_ext_max)
2264 sbi->s_ext_max = ee_len;
2265 if (ext_depth(inode) > sbi->s_depth_max)
2266 sbi->s_depth_max = ext_depth(inode);
2267 spin_unlock(&sbi->s_ext_stats_lock);
2268 }
2269#endif
2270 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002271 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002272 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002273 ext4_lblk_t num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002274
Amit Aroraa2df2a62007-07-17 21:42:41 -04002275 num = le32_to_cpu(ex->ee_block) + ee_len - from;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002276 pblk = ext4_ext_pblock(ex) + ee_len - num;
2277 ext_debug("free last %u blocks starting %llu\n", num, pblk);
2278 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
2279 /*
2280 * If the block range to be freed didn't start at the
2281 * beginning of a cluster, and we removed the entire
2282 * extent, save the partial cluster here, since we
2283 * might need to delete if we determine that the
2284 * truncate operation has removed all of the blocks in
2285 * the cluster.
2286 */
2287 if (pblk & (sbi->s_cluster_ratio - 1) &&
2288 (ee_len == num))
2289 *partial_cluster = EXT4_B2C(sbi, pblk);
2290 else
2291 *partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002292 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002293 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002294 /* head removal */
2295 ext4_lblk_t num;
2296 ext4_fsblk_t start;
2297
2298 num = to - from;
2299 start = ext4_ext_pblock(ex);
2300
2301 ext_debug("free first %u blocks starting %llu\n", num, start);
H Hartley Sweetenee90d572011-10-18 11:01:51 -04002302 ext4_free_blocks(handle, inode, NULL, start, num, flags);
Allison Hendersond583fb82011-05-25 07:41:43 -04002303
Alex Tomasa86c6182006-10-11 01:21:03 -07002304 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002305 printk(KERN_INFO "strange request: removal(2) "
2306 "%u-%u from %u:%u\n",
2307 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002308 }
2309 return 0;
2310}
2311
Allison Hendersond583fb82011-05-25 07:41:43 -04002312
2313/*
2314 * ext4_ext_rm_leaf() Removes the extents associated with the
2315 * blocks appearing between "start" and "end", and splits the extents
2316 * if "start" and "end" appear in the same extent
2317 *
2318 * @handle: The journal handle
2319 * @inode: The files inode
2320 * @path: The path to the leaf
2321 * @start: The first block to remove
2322 * @end: The last block to remove
2323 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002324static int
2325ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002326 struct ext4_ext_path *path, ext4_fsblk_t *partial_cluster,
2327 ext4_lblk_t start, ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002328{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002329 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002330 int err = 0, correct_index = 0;
2331 int depth = ext_depth(inode), credits;
2332 struct ext4_extent_header *eh;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002333 ext4_lblk_t a, b;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002334 unsigned num;
2335 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002336 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002337 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002338 struct ext4_extent *ex;
2339
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002340 /* the header must be checked already in ext4_ext_remove_space() */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002341 ext_debug("truncate since %u in leaf to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002342 if (!path[depth].p_hdr)
2343 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2344 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002345 if (unlikely(path[depth].p_hdr == NULL)) {
2346 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2347 return -EIO;
2348 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002349 /* find where to start removing */
2350 ex = EXT_LAST_EXTENT(eh);
2351
2352 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002353 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002354
Aditya Kalid8990242011-09-09 19:18:51 -04002355 trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster);
2356
Alex Tomasa86c6182006-10-11 01:21:03 -07002357 while (ex >= EXT_FIRST_EXTENT(eh) &&
2358 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002359
2360 if (ext4_ext_is_uninitialized(ex))
2361 uninitialized = 1;
2362 else
2363 uninitialized = 0;
2364
Mingming553f9002009-09-18 13:34:55 -04002365 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2366 uninitialized, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002367 path[depth].p_ext = ex;
2368
2369 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002370 b = ex_ee_block+ex_ee_len - 1 < end ?
2371 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002372
2373 ext_debug(" border %u:%u\n", a, b);
2374
Allison Hendersond583fb82011-05-25 07:41:43 -04002375 /* If this extent is beyond the end of the hole, skip it */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002376 if (end < ex_ee_block) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002377 ex--;
2378 ex_ee_block = le32_to_cpu(ex->ee_block);
2379 ex_ee_len = ext4_ext_get_actual_len(ex);
2380 continue;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002381 } else if (b != ex_ee_block + ex_ee_len - 1) {
Lukas Czernerdc1841d2012-03-19 23:07:43 -04002382 EXT4_ERROR_INODE(inode,
2383 "can not handle truncate %u:%u "
2384 "on extent %u:%u",
2385 start, end, ex_ee_block,
2386 ex_ee_block + ex_ee_len - 1);
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002387 err = -EIO;
2388 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002389 } else if (a != ex_ee_block) {
2390 /* remove tail of the extent */
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002391 num = a - ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002392 } else {
2393 /* remove whole extent: excellent! */
Alex Tomasa86c6182006-10-11 01:21:03 -07002394 num = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002395 }
Theodore Ts'o34071da2008-08-01 21:59:19 -04002396 /*
2397 * 3 for leaf, sb, and inode plus 2 (bmap and group
2398 * descriptor) for each block group; assume two block
2399 * groups plus ex_ee_len/blocks_per_block_group for
2400 * the worst case
2401 */
2402 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002403 if (ex == EXT_FIRST_EXTENT(eh)) {
2404 correct_index = 1;
2405 credits += (ext_depth(inode)) + 1;
2406 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002407 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002408
Jan Kara487caee2009-08-17 22:17:20 -04002409 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
Shen Feng9102e4f2008-07-11 19:27:31 -04002410 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002411 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002412
2413 err = ext4_ext_get_access(handle, inode, path + depth);
2414 if (err)
2415 goto out;
2416
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002417 err = ext4_remove_blocks(handle, inode, ex, partial_cluster,
2418 a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002419 if (err)
2420 goto out;
2421
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002422 if (num == 0)
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002423 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002424 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002425
Alex Tomasa86c6182006-10-11 01:21:03 -07002426 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002427 /*
2428 * Do not mark uninitialized if all the blocks in the
2429 * extent have been removed.
2430 */
2431 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002432 ext4_ext_mark_uninitialized(ex);
Allison Hendersond583fb82011-05-25 07:41:43 -04002433 /*
2434 * If the extent was completely released,
2435 * we need to remove it from the leaf
2436 */
2437 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002438 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002439 /*
2440 * For hole punching, we need to scoot all the
2441 * extents up when an extent is removed so that
2442 * we dont have blank extents in the middle
2443 */
2444 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2445 sizeof(struct ext4_extent));
2446
2447 /* Now get rid of the one at the end */
2448 memset(EXT_LAST_EXTENT(eh), 0,
2449 sizeof(struct ext4_extent));
2450 }
2451 le16_add_cpu(&eh->eh_entries, -1);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002452 } else
2453 *partial_cluster = 0;
Allison Hendersond583fb82011-05-25 07:41:43 -04002454
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002455 err = ext4_ext_dirty(handle, inode, path + depth);
2456 if (err)
2457 goto out;
2458
Yongqiang Yangbf52c6f2011-11-01 18:59:26 -04002459 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002460 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002461 ex--;
2462 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002463 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002464 }
2465
2466 if (correct_index && eh->eh_entries)
2467 err = ext4_ext_correct_indexes(handle, inode, path);
2468
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002469 /*
2470 * If there is still a entry in the leaf node, check to see if
2471 * it references the partial cluster. This is the only place
2472 * where it could; if it doesn't, we can free the cluster.
2473 */
2474 if (*partial_cluster && ex >= EXT_FIRST_EXTENT(eh) &&
2475 (EXT4_B2C(sbi, ext4_ext_pblock(ex) + ex_ee_len - 1) !=
2476 *partial_cluster)) {
2477 int flags = EXT4_FREE_BLOCKS_FORGET;
2478
2479 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2480 flags |= EXT4_FREE_BLOCKS_METADATA;
2481
2482 ext4_free_blocks(handle, inode, NULL,
2483 EXT4_C2B(sbi, *partial_cluster),
2484 sbi->s_cluster_ratio, flags);
2485 *partial_cluster = 0;
2486 }
2487
Alex Tomasa86c6182006-10-11 01:21:03 -07002488 /* if this leaf is free, then we should
2489 * remove it from index block above */
2490 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2491 err = ext4_ext_rm_idx(handle, inode, path + depth);
2492
2493out:
2494 return err;
2495}
2496
2497/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002498 * ext4_ext_more_to_rm:
2499 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002500 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002501static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002502ext4_ext_more_to_rm(struct ext4_ext_path *path)
2503{
2504 BUG_ON(path->p_idx == NULL);
2505
2506 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2507 return 0;
2508
2509 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002510 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002511 * so we have to consider current index for truncation
2512 */
2513 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2514 return 0;
2515 return 1;
2516}
2517
Lukas Czerner5f95d212012-03-19 23:03:19 -04002518static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2519 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002520{
2521 struct super_block *sb = inode->i_sb;
2522 int depth = ext_depth(inode);
2523 struct ext4_ext_path *path;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002524 ext4_fsblk_t partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002525 handle_t *handle;
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002526 int i, err;
Alex Tomasa86c6182006-10-11 01:21:03 -07002527
Lukas Czerner5f95d212012-03-19 23:03:19 -04002528 ext_debug("truncate since %u to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002529
2530 /* probably first extent we're gonna free will be last in block */
2531 handle = ext4_journal_start(inode, depth + 1);
2532 if (IS_ERR(handle))
2533 return PTR_ERR(handle);
2534
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002535again:
Alex Tomasa86c6182006-10-11 01:21:03 -07002536 ext4_ext_invalidate_cache(inode);
2537
Aditya Kalid8990242011-09-09 19:18:51 -04002538 trace_ext4_ext_remove_space(inode, start, depth);
2539
Alex Tomasa86c6182006-10-11 01:21:03 -07002540 /*
Lukas Czerner5f95d212012-03-19 23:03:19 -04002541 * Check if we are removing extents inside the extent tree. If that
2542 * is the case, we are going to punch a hole inside the extent tree
2543 * so we have to check whether we need to split the extent covering
2544 * the last block to remove so we can easily remove the part of it
2545 * in ext4_ext_rm_leaf().
2546 */
2547 if (end < EXT_MAX_BLOCKS - 1) {
2548 struct ext4_extent *ex;
2549 ext4_lblk_t ee_block;
2550
2551 /* find extent for this block */
2552 path = ext4_ext_find_extent(inode, end, NULL);
2553 if (IS_ERR(path)) {
2554 ext4_journal_stop(handle);
2555 return PTR_ERR(path);
2556 }
2557 depth = ext_depth(inode);
2558 ex = path[depth].p_ext;
2559 if (!ex)
2560 goto cont;
2561
2562 ee_block = le32_to_cpu(ex->ee_block);
2563
2564 /*
2565 * See if the last block is inside the extent, if so split
2566 * the extent at 'end' block so we can easily remove the
2567 * tail of the first part of the split extent in
2568 * ext4_ext_rm_leaf().
2569 */
2570 if (end >= ee_block &&
2571 end < ee_block + ext4_ext_get_actual_len(ex) - 1) {
2572 int split_flag = 0;
2573
2574 if (ext4_ext_is_uninitialized(ex))
2575 split_flag = EXT4_EXT_MARK_UNINIT1 |
2576 EXT4_EXT_MARK_UNINIT2;
2577
2578 /*
2579 * Split the extent in two so that 'end' is the last
2580 * block in the first new extent
2581 */
2582 err = ext4_split_extent_at(handle, inode, path,
2583 end + 1, split_flag,
2584 EXT4_GET_BLOCKS_PRE_IO |
2585 EXT4_GET_BLOCKS_PUNCH_OUT_EXT);
2586
2587 if (err < 0)
2588 goto out;
2589 }
2590 ext4_ext_drop_refs(path);
2591 kfree(path);
2592 }
2593cont:
2594
2595 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002596 * We start scanning from right side, freeing all the blocks
2597 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002598 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002599 depth = ext_depth(inode);
Josef Bacik216553c2008-04-29 22:02:02 -04002600 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07002601 if (path == NULL) {
2602 ext4_journal_stop(handle);
2603 return -ENOMEM;
2604 }
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002605 path[0].p_depth = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -07002606 path[0].p_hdr = ext_inode_hdr(inode);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002607
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002608 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002609 err = -EIO;
2610 goto out;
2611 }
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002612 i = err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002613
2614 while (i >= 0 && err == 0) {
2615 if (i == depth) {
2616 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04002617 err = ext4_ext_rm_leaf(handle, inode, path,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002618 &partial_cluster, start,
Lukas Czerner5f95d212012-03-19 23:03:19 -04002619 end);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002620 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002621 brelse(path[i].p_bh);
2622 path[i].p_bh = NULL;
2623 i--;
2624 continue;
2625 }
2626
2627 /* this is index block */
2628 if (!path[i].p_hdr) {
2629 ext_debug("initialize header\n");
2630 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002631 }
2632
Alex Tomasa86c6182006-10-11 01:21:03 -07002633 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002634 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002635 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2636 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2637 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2638 path[i].p_hdr,
2639 le16_to_cpu(path[i].p_hdr->eh_entries));
2640 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002641 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002642 path[i].p_idx--;
2643 }
2644
2645 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2646 i, EXT_FIRST_INDEX(path[i].p_hdr),
2647 path[i].p_idx);
2648 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002649 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002650 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002651 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04002652 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002653 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'obf89d162010-10-27 21:30:14 -04002654 bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002655 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002656 /* should we reset i_size? */
2657 err = -EIO;
2658 break;
2659 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002660 if (WARN_ON(i + 1 > depth)) {
2661 err = -EIO;
2662 break;
2663 }
Darrick J. Wongf8489122012-04-29 18:21:10 -04002664 if (ext4_ext_check_block(inode, ext_block_hdr(bh),
2665 depth - i - 1, bh)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002666 err = -EIO;
2667 break;
2668 }
2669 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002670
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002671 /* save actual number of indexes since this
2672 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002673 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2674 i++;
2675 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002676 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002677 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002678 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002679 * handle must be already prepared by the
2680 * truncatei_leaf() */
2681 err = ext4_ext_rm_idx(handle, inode, path + i);
2682 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002683 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002684 brelse(path[i].p_bh);
2685 path[i].p_bh = NULL;
2686 i--;
2687 ext_debug("return to level %d\n", i);
2688 }
2689 }
2690
Aditya Kalid8990242011-09-09 19:18:51 -04002691 trace_ext4_ext_remove_space_done(inode, start, depth, partial_cluster,
2692 path->p_hdr->eh_entries);
2693
Aditya Kali7b415bf2011-09-09 19:04:51 -04002694 /* If we still have something in the partial cluster and we have removed
2695 * even the first extent, then we should free the blocks in the partial
2696 * cluster as well. */
2697 if (partial_cluster && path->p_hdr->eh_entries == 0) {
2698 int flags = EXT4_FREE_BLOCKS_FORGET;
2699
2700 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2701 flags |= EXT4_FREE_BLOCKS_METADATA;
2702
2703 ext4_free_blocks(handle, inode, NULL,
2704 EXT4_C2B(EXT4_SB(sb), partial_cluster),
2705 EXT4_SB(sb)->s_cluster_ratio, flags);
2706 partial_cluster = 0;
2707 }
2708
Alex Tomasa86c6182006-10-11 01:21:03 -07002709 /* TODO: flexible tree reduction should be here */
2710 if (path->p_hdr->eh_entries == 0) {
2711 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002712 * truncate to zero freed all the tree,
2713 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002714 */
2715 err = ext4_ext_get_access(handle, inode, path);
2716 if (err == 0) {
2717 ext_inode_hdr(inode)->eh_depth = 0;
2718 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04002719 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07002720 err = ext4_ext_dirty(handle, inode, path);
2721 }
2722 }
2723out:
Alex Tomasa86c6182006-10-11 01:21:03 -07002724 ext4_ext_drop_refs(path);
2725 kfree(path);
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002726 if (err == -EAGAIN)
2727 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07002728 ext4_journal_stop(handle);
2729
2730 return err;
2731}
2732
2733/*
2734 * called at mount time
2735 */
2736void ext4_ext_init(struct super_block *sb)
2737{
2738 /*
2739 * possible initialization would be here
2740 */
2741
Theodore Ts'o83982b62009-01-06 14:53:16 -05002742 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04002743#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o92b97812012-03-19 23:41:49 -04002744 printk(KERN_INFO "EXT4-fs: file extents enabled"
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002745#ifdef AGGRESSIVE_TEST
Theodore Ts'o92b97812012-03-19 23:41:49 -04002746 ", aggressive tests"
Alex Tomasa86c6182006-10-11 01:21:03 -07002747#endif
2748#ifdef CHECK_BINSEARCH
Theodore Ts'o92b97812012-03-19 23:41:49 -04002749 ", check binsearch"
Alex Tomasa86c6182006-10-11 01:21:03 -07002750#endif
2751#ifdef EXTENTS_STATS
Theodore Ts'o92b97812012-03-19 23:41:49 -04002752 ", stats"
Alex Tomasa86c6182006-10-11 01:21:03 -07002753#endif
Theodore Ts'o92b97812012-03-19 23:41:49 -04002754 "\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04002755#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07002756#ifdef EXTENTS_STATS
2757 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2758 EXT4_SB(sb)->s_ext_min = 1 << 30;
2759 EXT4_SB(sb)->s_ext_max = 0;
2760#endif
2761 }
2762}
2763
2764/*
2765 * called at umount time
2766 */
2767void ext4_ext_release(struct super_block *sb)
2768{
Theodore Ts'o83982b62009-01-06 14:53:16 -05002769 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
Alex Tomasa86c6182006-10-11 01:21:03 -07002770 return;
2771
2772#ifdef EXTENTS_STATS
2773 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2774 struct ext4_sb_info *sbi = EXT4_SB(sb);
2775 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2776 sbi->s_ext_blocks, sbi->s_ext_extents,
2777 sbi->s_ext_blocks / sbi->s_ext_extents);
2778 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2779 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2780 }
2781#endif
2782}
2783
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002784/* FIXME!! we need to try to merge to left or right after zero-out */
2785static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2786{
Lukas Czerner24075182010-10-27 21:30:06 -04002787 ext4_fsblk_t ee_pblock;
2788 unsigned int ee_len;
Jing Zhangb7203032010-05-12 00:00:00 -04002789 int ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002790
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002791 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04002792 ee_pblock = ext4_ext_pblock(ex);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002793
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04002794 ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
Lukas Czerner24075182010-10-27 21:30:06 -04002795 if (ret > 0)
2796 ret = 0;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002797
Lukas Czerner24075182010-10-27 21:30:06 -04002798 return ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002799}
2800
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002801/*
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002802 * ext4_split_extent_at() splits an extent at given block.
2803 *
2804 * @handle: the journal handle
2805 * @inode: the file inode
2806 * @path: the path to the extent
2807 * @split: the logical block where the extent is splitted.
2808 * @split_flags: indicates if the extent could be zeroout if split fails, and
2809 * the states(init or uninit) of new extents.
2810 * @flags: flags used to insert new extent to extent tree.
2811 *
2812 *
2813 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
2814 * of which are deterimined by split_flag.
2815 *
2816 * There are two cases:
2817 * a> the extent are splitted into two extent.
2818 * b> split is not needed, and just mark the extent.
2819 *
2820 * return 0 on success.
2821 */
2822static int ext4_split_extent_at(handle_t *handle,
2823 struct inode *inode,
2824 struct ext4_ext_path *path,
2825 ext4_lblk_t split,
2826 int split_flag,
2827 int flags)
2828{
2829 ext4_fsblk_t newblock;
2830 ext4_lblk_t ee_block;
2831 struct ext4_extent *ex, newex, orig_ex;
2832 struct ext4_extent *ex2 = NULL;
2833 unsigned int ee_len, depth;
2834 int err = 0;
2835
2836 ext_debug("ext4_split_extents_at: inode %lu, logical"
2837 "block %llu\n", inode->i_ino, (unsigned long long)split);
2838
2839 ext4_ext_show_leaf(inode, path);
2840
2841 depth = ext_depth(inode);
2842 ex = path[depth].p_ext;
2843 ee_block = le32_to_cpu(ex->ee_block);
2844 ee_len = ext4_ext_get_actual_len(ex);
2845 newblock = split - ee_block + ext4_ext_pblock(ex);
2846
2847 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
2848
2849 err = ext4_ext_get_access(handle, inode, path + depth);
2850 if (err)
2851 goto out;
2852
2853 if (split == ee_block) {
2854 /*
2855 * case b: block @split is the block that the extent begins with
2856 * then we just change the state of the extent, and splitting
2857 * is not needed.
2858 */
2859 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2860 ext4_ext_mark_uninitialized(ex);
2861 else
2862 ext4_ext_mark_initialized(ex);
2863
2864 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
2865 ext4_ext_try_to_merge(inode, path, ex);
2866
2867 err = ext4_ext_dirty(handle, inode, path + depth);
2868 goto out;
2869 }
2870
2871 /* case a */
2872 memcpy(&orig_ex, ex, sizeof(orig_ex));
2873 ex->ee_len = cpu_to_le16(split - ee_block);
2874 if (split_flag & EXT4_EXT_MARK_UNINIT1)
2875 ext4_ext_mark_uninitialized(ex);
2876
2877 /*
2878 * path may lead to new leaf, not to original leaf any more
2879 * after ext4_ext_insert_extent() returns,
2880 */
2881 err = ext4_ext_dirty(handle, inode, path + depth);
2882 if (err)
2883 goto fix_extent_len;
2884
2885 ex2 = &newex;
2886 ex2->ee_block = cpu_to_le32(split);
2887 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
2888 ext4_ext_store_pblock(ex2, newblock);
2889 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2890 ext4_ext_mark_uninitialized(ex2);
2891
2892 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
2893 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2894 err = ext4_ext_zeroout(inode, &orig_ex);
2895 if (err)
2896 goto fix_extent_len;
2897 /* update the extent length and mark as initialized */
Al Viroaf1584f2012-04-12 20:32:25 -04002898 ex->ee_len = cpu_to_le16(ee_len);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002899 ext4_ext_try_to_merge(inode, path, ex);
2900 err = ext4_ext_dirty(handle, inode, path + depth);
2901 goto out;
2902 } else if (err)
2903 goto fix_extent_len;
2904
2905out:
2906 ext4_ext_show_leaf(inode, path);
2907 return err;
2908
2909fix_extent_len:
2910 ex->ee_len = orig_ex.ee_len;
2911 ext4_ext_dirty(handle, inode, path + depth);
2912 return err;
2913}
2914
2915/*
2916 * ext4_split_extents() splits an extent and mark extent which is covered
2917 * by @map as split_flags indicates
2918 *
2919 * It may result in splitting the extent into multiple extents (upto three)
2920 * There are three possibilities:
2921 * a> There is no split required
2922 * b> Splits in two extents: Split is happening at either end of the extent
2923 * c> Splits in three extents: Somone is splitting in middle of the extent
2924 *
2925 */
2926static int ext4_split_extent(handle_t *handle,
2927 struct inode *inode,
2928 struct ext4_ext_path *path,
2929 struct ext4_map_blocks *map,
2930 int split_flag,
2931 int flags)
2932{
2933 ext4_lblk_t ee_block;
2934 struct ext4_extent *ex;
2935 unsigned int ee_len, depth;
2936 int err = 0;
2937 int uninitialized;
2938 int split_flag1, flags1;
2939
2940 depth = ext_depth(inode);
2941 ex = path[depth].p_ext;
2942 ee_block = le32_to_cpu(ex->ee_block);
2943 ee_len = ext4_ext_get_actual_len(ex);
2944 uninitialized = ext4_ext_is_uninitialized(ex);
2945
2946 if (map->m_lblk + map->m_len < ee_block + ee_len) {
2947 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2948 EXT4_EXT_MAY_ZEROOUT : 0;
2949 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
2950 if (uninitialized)
2951 split_flag1 |= EXT4_EXT_MARK_UNINIT1 |
2952 EXT4_EXT_MARK_UNINIT2;
2953 err = ext4_split_extent_at(handle, inode, path,
2954 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04002955 if (err)
2956 goto out;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002957 }
2958
2959 ext4_ext_drop_refs(path);
2960 path = ext4_ext_find_extent(inode, map->m_lblk, path);
2961 if (IS_ERR(path))
2962 return PTR_ERR(path);
2963
2964 if (map->m_lblk >= ee_block) {
2965 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2966 EXT4_EXT_MAY_ZEROOUT : 0;
2967 if (uninitialized)
2968 split_flag1 |= EXT4_EXT_MARK_UNINIT1;
2969 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2970 split_flag1 |= EXT4_EXT_MARK_UNINIT2;
2971 err = ext4_split_extent_at(handle, inode, path,
2972 map->m_lblk, split_flag1, flags);
2973 if (err)
2974 goto out;
2975 }
2976
2977 ext4_ext_show_leaf(inode, path);
2978out:
2979 return err ? err : map->m_len;
2980}
2981
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002982#define EXT4_EXT_ZERO_LEN 7
Amit Arora56055d32007-07-17 21:42:38 -04002983/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002984 * This function is called by ext4_ext_map_blocks() if someone tries to write
Amit Arora56055d32007-07-17 21:42:38 -04002985 * to an uninitialized extent. It may result in splitting the uninitialized
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002986 * extent into multiple extents (up to three - one initialized and two
Amit Arora56055d32007-07-17 21:42:38 -04002987 * uninitialized).
2988 * There are three possibilities:
2989 * a> There is no split required: Entire extent should be initialized
2990 * b> Splits in two extents: Write is happening at either end of the extent
2991 * c> Splits in three extents: Somone is writing in middle of the extent
Eric Gouriou6f91bc52011-10-27 11:43:23 -04002992 *
2993 * Pre-conditions:
2994 * - The extent pointed to by 'path' is uninitialized.
2995 * - The extent pointed to by 'path' contains a superset
2996 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
2997 *
2998 * Post-conditions on success:
2999 * - the returned value is the number of blocks beyond map->l_lblk
3000 * that are allocated and initialized.
3001 * It is guaranteed to be >= map->m_len.
Amit Arora56055d32007-07-17 21:42:38 -04003002 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003003static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003004 struct inode *inode,
3005 struct ext4_map_blocks *map,
3006 struct ext4_ext_path *path)
Amit Arora56055d32007-07-17 21:42:38 -04003007{
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003008 struct ext4_extent_header *eh;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003009 struct ext4_map_blocks split_map;
3010 struct ext4_extent zero_ex;
3011 struct ext4_extent *ex;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003012 ext4_lblk_t ee_block, eof_block;
Dan Carpenterf85b2872011-10-26 03:42:36 -04003013 unsigned int ee_len, depth;
3014 int allocated;
Amit Arora56055d32007-07-17 21:42:38 -04003015 int err = 0;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003016 int split_flag = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003017
3018 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3019 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003020 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003021
3022 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3023 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003024 if (eof_block < map->m_lblk + map->m_len)
3025 eof_block = map->m_lblk + map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003026
3027 depth = ext_depth(inode);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003028 eh = path[depth].p_hdr;
Amit Arora56055d32007-07-17 21:42:38 -04003029 ex = path[depth].p_ext;
3030 ee_block = le32_to_cpu(ex->ee_block);
3031 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003032 allocated = ee_len - (map->m_lblk - ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003033
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003034 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
3035
3036 /* Pre-conditions */
3037 BUG_ON(!ext4_ext_is_uninitialized(ex));
3038 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003039
3040 /*
3041 * Attempt to transfer newly initialized blocks from the currently
3042 * uninitialized extent to its left neighbor. This is much cheaper
3043 * than an insertion followed by a merge as those involve costly
3044 * memmove() calls. This is the common case in steady state for
3045 * workloads doing fallocate(FALLOC_FL_KEEP_SIZE) followed by append
3046 * writes.
3047 *
3048 * Limitations of the current logic:
3049 * - L1: we only deal with writes at the start of the extent.
3050 * The approach could be extended to writes at the end
3051 * of the extent but this scenario was deemed less common.
3052 * - L2: we do not deal with writes covering the whole extent.
3053 * This would require removing the extent if the transfer
3054 * is possible.
3055 * - L3: we only attempt to merge with an extent stored in the
3056 * same extent tree node.
3057 */
3058 if ((map->m_lblk == ee_block) && /*L1*/
3059 (map->m_len < ee_len) && /*L2*/
3060 (ex > EXT_FIRST_EXTENT(eh))) { /*L3*/
3061 struct ext4_extent *prev_ex;
3062 ext4_lblk_t prev_lblk;
3063 ext4_fsblk_t prev_pblk, ee_pblk;
3064 unsigned int prev_len, write_len;
3065
3066 prev_ex = ex - 1;
3067 prev_lblk = le32_to_cpu(prev_ex->ee_block);
3068 prev_len = ext4_ext_get_actual_len(prev_ex);
3069 prev_pblk = ext4_ext_pblock(prev_ex);
3070 ee_pblk = ext4_ext_pblock(ex);
3071 write_len = map->m_len;
3072
3073 /*
3074 * A transfer of blocks from 'ex' to 'prev_ex' is allowed
3075 * upon those conditions:
3076 * - C1: prev_ex is initialized,
3077 * - C2: prev_ex is logically abutting ex,
3078 * - C3: prev_ex is physically abutting ex,
3079 * - C4: prev_ex can receive the additional blocks without
3080 * overflowing the (initialized) length limit.
3081 */
3082 if ((!ext4_ext_is_uninitialized(prev_ex)) && /*C1*/
3083 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3084 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
3085 (prev_len < (EXT_INIT_MAX_LEN - write_len))) { /*C4*/
3086 err = ext4_ext_get_access(handle, inode, path + depth);
3087 if (err)
3088 goto out;
3089
3090 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3091 map, ex, prev_ex);
3092
3093 /* Shift the start of ex by 'write_len' blocks */
3094 ex->ee_block = cpu_to_le32(ee_block + write_len);
3095 ext4_ext_store_pblock(ex, ee_pblk + write_len);
3096 ex->ee_len = cpu_to_le16(ee_len - write_len);
3097 ext4_ext_mark_uninitialized(ex); /* Restore the flag */
3098
3099 /* Extend prev_ex by 'write_len' blocks */
3100 prev_ex->ee_len = cpu_to_le16(prev_len + write_len);
3101
3102 /* Mark the block containing both extents as dirty */
3103 ext4_ext_dirty(handle, inode, path + depth);
3104
3105 /* Update path to point to the right extent */
3106 path[depth].p_ext = prev_ex;
3107
3108 /* Result: number of initialized blocks past m_lblk */
3109 allocated = write_len;
3110 goto out;
3111 }
3112 }
3113
Yongqiang Yang667eff32011-05-03 12:25:07 -04003114 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003115 /*
3116 * It is safe to convert extent to initialized via explicit
3117 * zeroout only if extent is fully insde i_size or new_size.
3118 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003119 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003120
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003121 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003122 if (ee_len <= 2*EXT4_EXT_ZERO_LEN &&
3123 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3124 err = ext4_ext_zeroout(inode, ex);
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003125 if (err)
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003126 goto out;
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003127
3128 err = ext4_ext_get_access(handle, inode, path + depth);
3129 if (err)
3130 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003131 ext4_ext_mark_initialized(ex);
3132 ext4_ext_try_to_merge(inode, path, ex);
3133 err = ext4_ext_dirty(handle, inode, path + depth);
3134 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04003135 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003136
Amit Arora56055d32007-07-17 21:42:38 -04003137 /*
Yongqiang Yang667eff32011-05-03 12:25:07 -04003138 * four cases:
3139 * 1. split the extent into three extents.
3140 * 2. split the extent into two extents, zeroout the first half.
3141 * 3. split the extent into two extents, zeroout the second half.
3142 * 4. split the extent into two extents with out zeroout.
Amit Arora56055d32007-07-17 21:42:38 -04003143 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003144 split_map.m_lblk = map->m_lblk;
3145 split_map.m_len = map->m_len;
3146
3147 if (allocated > map->m_len) {
3148 if (allocated <= EXT4_EXT_ZERO_LEN &&
3149 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3150 /* case 3 */
3151 zero_ex.ee_block =
Allison Henderson9b940f82011-05-16 10:11:09 -04003152 cpu_to_le32(map->m_lblk);
3153 zero_ex.ee_len = cpu_to_le16(allocated);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003154 ext4_ext_store_pblock(&zero_ex,
3155 ext4_ext_pblock(ex) + map->m_lblk - ee_block);
3156 err = ext4_ext_zeroout(inode, &zero_ex);
Amit Arora56055d32007-07-17 21:42:38 -04003157 if (err)
3158 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003159 split_map.m_lblk = map->m_lblk;
3160 split_map.m_len = allocated;
3161 } else if ((map->m_lblk - ee_block + map->m_len <
3162 EXT4_EXT_ZERO_LEN) &&
3163 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3164 /* case 2 */
3165 if (map->m_lblk != ee_block) {
3166 zero_ex.ee_block = ex->ee_block;
3167 zero_ex.ee_len = cpu_to_le16(map->m_lblk -
3168 ee_block);
3169 ext4_ext_store_pblock(&zero_ex,
3170 ext4_ext_pblock(ex));
3171 err = ext4_ext_zeroout(inode, &zero_ex);
3172 if (err)
3173 goto out;
3174 }
3175
Yongqiang Yang667eff32011-05-03 12:25:07 -04003176 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003177 split_map.m_len = map->m_lblk - ee_block + map->m_len;
3178 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003179 }
3180 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003181
3182 allocated = ext4_split_extent(handle, inode, path,
3183 &split_map, split_flag, 0);
3184 if (allocated < 0)
3185 err = allocated;
3186
Amit Arora56055d32007-07-17 21:42:38 -04003187out:
3188 return err ? err : allocated;
3189}
3190
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003191/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003192 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003193 * ext4_get_blocks_dio_write() when DIO to write
3194 * to an uninitialized extent.
3195 *
Paul Bollefd018fe2011-02-15 00:05:43 +01003196 * Writing to an uninitialized extent may result in splitting the uninitialized
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003197 * extent into multiple /initialized uninitialized extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003198 * There are three possibilities:
3199 * a> There is no split required: Entire extent should be uninitialized
3200 * b> Splits in two extents: Write is happening at either end of the extent
3201 * c> Splits in three extents: Somone is writing in middle of the extent
3202 *
3203 * One of more index blocks maybe needed if the extent tree grow after
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003204 * the uninitialized extent split. To prevent ENOSPC occur at the IO
Mingming Cao00314622009-09-28 15:49:08 -04003205 * complete, we need to split the uninitialized extent before DIO submit
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02003206 * the IO. The uninitialized extent called at this time will be split
Mingming Cao00314622009-09-28 15:49:08 -04003207 * into three uninitialized extent(at most). After IO complete, the part
3208 * being filled will be convert to initialized by the end_io callback function
3209 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003210 *
3211 * Returns the size of uninitialized extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003212 */
3213static int ext4_split_unwritten_extents(handle_t *handle,
3214 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003215 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003216 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04003217 int flags)
3218{
Yongqiang Yang667eff32011-05-03 12:25:07 -04003219 ext4_lblk_t eof_block;
3220 ext4_lblk_t ee_block;
3221 struct ext4_extent *ex;
3222 unsigned int ee_len;
3223 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003224
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003225 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
3226 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003227 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003228
3229 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3230 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003231 if (eof_block < map->m_lblk + map->m_len)
3232 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003233 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003234 * It is safe to convert extent to initialized via explicit
3235 * zeroout only if extent is fully insde i_size or new_size.
3236 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003237 depth = ext_depth(inode);
3238 ex = path[depth].p_ext;
3239 ee_block = le32_to_cpu(ex->ee_block);
3240 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003241
Yongqiang Yang667eff32011-05-03 12:25:07 -04003242 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
3243 split_flag |= EXT4_EXT_MARK_UNINIT2;
Mingming Cao00314622009-09-28 15:49:08 -04003244
Yongqiang Yang667eff32011-05-03 12:25:07 -04003245 flags |= EXT4_GET_BLOCKS_PRE_IO;
3246 return ext4_split_extent(handle, inode, path, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003247}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003248
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003249static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003250 struct inode *inode,
3251 struct ext4_ext_path *path)
3252{
3253 struct ext4_extent *ex;
Mingming Cao00314622009-09-28 15:49:08 -04003254 int depth;
3255 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003256
3257 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003258 ex = path[depth].p_ext;
3259
Yongqiang Yang197217a2011-05-03 11:45:29 -04003260 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3261 "block %llu, max_blocks %u\n", inode->i_ino,
3262 (unsigned long long)le32_to_cpu(ex->ee_block),
3263 ext4_ext_get_actual_len(ex));
3264
Mingming Cao00314622009-09-28 15:49:08 -04003265 err = ext4_ext_get_access(handle, inode, path + depth);
3266 if (err)
3267 goto out;
3268 /* first mark the extent as initialized */
3269 ext4_ext_mark_initialized(ex);
3270
Yongqiang Yang197217a2011-05-03 11:45:29 -04003271 /* note: ext4_ext_correct_indexes() isn't needed here because
3272 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003273 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04003274 ext4_ext_try_to_merge(inode, path, ex);
3275
Mingming Cao00314622009-09-28 15:49:08 -04003276 /* Mark modified extent as dirty */
3277 err = ext4_ext_dirty(handle, inode, path + depth);
3278out:
3279 ext4_ext_show_leaf(inode, path);
3280 return err;
3281}
3282
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003283static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3284 sector_t block, int count)
3285{
3286 int i;
3287 for (i = 0; i < count; i++)
3288 unmap_underlying_metadata(bdev, block + i);
3289}
3290
Theodore Ts'o58590b02010-10-27 21:23:12 -04003291/*
3292 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3293 */
3294static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
Eric Sandeend002ebf2011-01-10 13:03:35 -05003295 ext4_lblk_t lblk,
Theodore Ts'o58590b02010-10-27 21:23:12 -04003296 struct ext4_ext_path *path,
3297 unsigned int len)
3298{
3299 int i, depth;
3300 struct ext4_extent_header *eh;
Sergey Senozhatsky65922cb2011-03-23 14:08:27 -04003301 struct ext4_extent *last_ex;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003302
3303 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3304 return 0;
3305
3306 depth = ext_depth(inode);
3307 eh = path[depth].p_hdr;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003308
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003309 /*
3310 * We're going to remove EOFBLOCKS_FL entirely in future so we
3311 * do not care for this case anymore. Simply remove the flag
3312 * if there are no extents.
3313 */
3314 if (unlikely(!eh->eh_entries))
3315 goto out;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003316 last_ex = EXT_LAST_EXTENT(eh);
3317 /*
3318 * We should clear the EOFBLOCKS_FL flag if we are writing the
3319 * last block in the last extent in the file. We test this by
3320 * first checking to see if the caller to
3321 * ext4_ext_get_blocks() was interested in the last block (or
3322 * a block beyond the last block) in the current extent. If
3323 * this turns out to be false, we can bail out from this
3324 * function immediately.
3325 */
Eric Sandeend002ebf2011-01-10 13:03:35 -05003326 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
Theodore Ts'o58590b02010-10-27 21:23:12 -04003327 ext4_ext_get_actual_len(last_ex))
3328 return 0;
3329 /*
3330 * If the caller does appear to be planning to write at or
3331 * beyond the end of the current extent, we then test to see
3332 * if the current extent is the last extent in the file, by
3333 * checking to make sure it was reached via the rightmost node
3334 * at each level of the tree.
3335 */
3336 for (i = depth-1; i >= 0; i--)
3337 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3338 return 0;
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003339out:
Theodore Ts'o58590b02010-10-27 21:23:12 -04003340 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3341 return ext4_mark_inode_dirty(handle, inode);
3342}
3343
Aditya Kali7b415bf2011-09-09 19:04:51 -04003344/**
3345 * ext4_find_delalloc_range: find delayed allocated block in the given range.
3346 *
3347 * Goes through the buffer heads in the range [lblk_start, lblk_end] and returns
3348 * whether there are any buffers marked for delayed allocation. It returns '1'
3349 * on the first delalloc'ed buffer head found. If no buffer head in the given
3350 * range is marked for delalloc, it returns 0.
3351 * lblk_start should always be <= lblk_end.
3352 * search_hint_reverse is to indicate that searching in reverse from lblk_end to
3353 * lblk_start might be more efficient (i.e., we will likely hit the delalloc'ed
3354 * block sooner). This is useful when blocks are truncated sequentially from
3355 * lblk_start towards lblk_end.
3356 */
3357static int ext4_find_delalloc_range(struct inode *inode,
3358 ext4_lblk_t lblk_start,
3359 ext4_lblk_t lblk_end,
3360 int search_hint_reverse)
3361{
3362 struct address_space *mapping = inode->i_mapping;
3363 struct buffer_head *head, *bh = NULL;
3364 struct page *page;
3365 ext4_lblk_t i, pg_lblk;
3366 pgoff_t index;
3367
Robin Dong8c48f7e2011-12-18 23:05:43 -05003368 if (!test_opt(inode->i_sb, DELALLOC))
3369 return 0;
3370
Aditya Kali7b415bf2011-09-09 19:04:51 -04003371 /* reverse search wont work if fs block size is less than page size */
3372 if (inode->i_blkbits < PAGE_CACHE_SHIFT)
3373 search_hint_reverse = 0;
3374
3375 if (search_hint_reverse)
3376 i = lblk_end;
3377 else
3378 i = lblk_start;
3379
3380 index = i >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
3381
3382 while ((i >= lblk_start) && (i <= lblk_end)) {
3383 page = find_get_page(mapping, index);
Aditya Kali5356f262011-09-09 19:20:51 -04003384 if (!page)
Aditya Kali7b415bf2011-09-09 19:04:51 -04003385 goto nextpage;
3386
Aditya Kali7b415bf2011-09-09 19:04:51 -04003387 if (!page_has_buffers(page))
3388 goto nextpage;
3389
3390 head = page_buffers(page);
3391 if (!head)
3392 goto nextpage;
3393
3394 bh = head;
3395 pg_lblk = index << (PAGE_CACHE_SHIFT -
3396 inode->i_blkbits);
3397 do {
3398 if (unlikely(pg_lblk < lblk_start)) {
3399 /*
3400 * This is possible when fs block size is less
3401 * than page size and our cluster starts/ends in
3402 * middle of the page. So we need to skip the
3403 * initial few blocks till we reach the 'lblk'
3404 */
3405 pg_lblk++;
3406 continue;
3407 }
3408
Aditya Kali5356f262011-09-09 19:20:51 -04003409 /* Check if the buffer is delayed allocated and that it
3410 * is not yet mapped. (when da-buffers are mapped during
3411 * their writeout, their da_mapped bit is set.)
3412 */
3413 if (buffer_delay(bh) && !buffer_da_mapped(bh)) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04003414 page_cache_release(page);
Aditya Kalid8990242011-09-09 19:18:51 -04003415 trace_ext4_find_delalloc_range(inode,
3416 lblk_start, lblk_end,
3417 search_hint_reverse,
3418 1, i);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003419 return 1;
3420 }
3421 if (search_hint_reverse)
3422 i--;
3423 else
3424 i++;
3425 } while ((i >= lblk_start) && (i <= lblk_end) &&
3426 ((bh = bh->b_this_page) != head));
3427nextpage:
3428 if (page)
3429 page_cache_release(page);
3430 /*
3431 * Move to next page. 'i' will be the first lblk in the next
3432 * page.
3433 */
3434 if (search_hint_reverse)
3435 index--;
3436 else
3437 index++;
3438 i = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
3439 }
3440
Aditya Kalid8990242011-09-09 19:18:51 -04003441 trace_ext4_find_delalloc_range(inode, lblk_start, lblk_end,
3442 search_hint_reverse, 0, 0);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003443 return 0;
3444}
3445
3446int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk,
3447 int search_hint_reverse)
3448{
3449 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3450 ext4_lblk_t lblk_start, lblk_end;
3451 lblk_start = lblk & (~(sbi->s_cluster_ratio - 1));
3452 lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
3453
3454 return ext4_find_delalloc_range(inode, lblk_start, lblk_end,
3455 search_hint_reverse);
3456}
3457
3458/**
3459 * Determines how many complete clusters (out of those specified by the 'map')
3460 * are under delalloc and were reserved quota for.
3461 * This function is called when we are writing out the blocks that were
3462 * originally written with their allocation delayed, but then the space was
3463 * allocated using fallocate() before the delayed allocation could be resolved.
3464 * The cases to look for are:
3465 * ('=' indicated delayed allocated blocks
3466 * '-' indicates non-delayed allocated blocks)
3467 * (a) partial clusters towards beginning and/or end outside of allocated range
3468 * are not delalloc'ed.
3469 * Ex:
3470 * |----c---=|====c====|====c====|===-c----|
3471 * |++++++ allocated ++++++|
3472 * ==> 4 complete clusters in above example
3473 *
3474 * (b) partial cluster (outside of allocated range) towards either end is
3475 * marked for delayed allocation. In this case, we will exclude that
3476 * cluster.
3477 * Ex:
3478 * |----====c========|========c========|
3479 * |++++++ allocated ++++++|
3480 * ==> 1 complete clusters in above example
3481 *
3482 * Ex:
3483 * |================c================|
3484 * |++++++ allocated ++++++|
3485 * ==> 0 complete clusters in above example
3486 *
3487 * The ext4_da_update_reserve_space will be called only if we
3488 * determine here that there were some "entire" clusters that span
3489 * this 'allocated' range.
3490 * In the non-bigalloc case, this function will just end up returning num_blks
3491 * without ever calling ext4_find_delalloc_range.
3492 */
3493static unsigned int
3494get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start,
3495 unsigned int num_blks)
3496{
3497 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3498 ext4_lblk_t alloc_cluster_start, alloc_cluster_end;
3499 ext4_lblk_t lblk_from, lblk_to, c_offset;
3500 unsigned int allocated_clusters = 0;
3501
3502 alloc_cluster_start = EXT4_B2C(sbi, lblk_start);
3503 alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1);
3504
3505 /* max possible clusters for this allocation */
3506 allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1;
3507
Aditya Kalid8990242011-09-09 19:18:51 -04003508 trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks);
3509
Aditya Kali7b415bf2011-09-09 19:04:51 -04003510 /* Check towards left side */
3511 c_offset = lblk_start & (sbi->s_cluster_ratio - 1);
3512 if (c_offset) {
3513 lblk_from = lblk_start & (~(sbi->s_cluster_ratio - 1));
3514 lblk_to = lblk_from + c_offset - 1;
3515
3516 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
3517 allocated_clusters--;
3518 }
3519
3520 /* Now check towards right. */
3521 c_offset = (lblk_start + num_blks) & (sbi->s_cluster_ratio - 1);
3522 if (allocated_clusters && c_offset) {
3523 lblk_from = lblk_start + num_blks;
3524 lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1;
3525
3526 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
3527 allocated_clusters--;
3528 }
3529
3530 return allocated_clusters;
3531}
3532
Mingming Cao00314622009-09-28 15:49:08 -04003533static int
3534ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003535 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003536 struct ext4_ext_path *path, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003537 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003538{
3539 int ret = 0;
3540 int err = 0;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003541 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Mingming Cao00314622009-09-28 15:49:08 -04003542
Zheng Liu88635ca2011-12-28 19:00:25 -05003543 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical "
3544 "block %llu, max_blocks %u, flags %x, allocated %u\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003545 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04003546 flags, allocated);
3547 ext4_ext_show_leaf(inode, path);
3548
Aditya Kalid8990242011-09-09 19:18:51 -04003549 trace_ext4_ext_handle_uninitialized_extents(inode, map, allocated,
3550 newblock);
3551
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003552 /* get_block() before submit the IO, split the extent */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003553 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003554 ret = ext4_split_unwritten_extents(handle, inode, map,
3555 path, flags);
Mingming5f5249502009-11-10 10:48:04 -05003556 /*
3557 * Flag the inode(non aio case) or end_io struct (aio case)
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003558 * that this IO needs to conversion to written when IO is
Mingming5f5249502009-11-10 10:48:04 -05003559 * completed
3560 */
Tao Ma0edeb712011-10-31 17:30:44 -04003561 if (io)
3562 ext4_set_io_unwritten_flag(inode, io);
3563 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003564 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
Jiaying Zhang744692d2010-03-04 16:14:02 -05003565 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003566 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao00314622009-09-28 15:49:08 -04003567 goto out;
3568 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003569 /* IO end_io complete, convert the filled extent to written */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003570 if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003571 ret = ext4_convert_unwritten_extents_endio(handle, inode,
Mingming Cao00314622009-09-28 15:49:08 -04003572 path);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003573 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05003574 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05003575 err = check_eofblocks_fl(handle, inode, map->m_lblk,
3576 path, map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003577 } else
3578 err = ret;
Mingming Cao00314622009-09-28 15:49:08 -04003579 goto out2;
3580 }
3581 /* buffered IO case */
3582 /*
3583 * repeat fallocate creation request
3584 * we already have an unwritten extent
3585 */
3586 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3587 goto map_out;
3588
3589 /* buffered READ or buffered write_begin() lookup */
3590 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3591 /*
3592 * We have blocks reserved already. We
3593 * return allocated blocks so that delalloc
3594 * won't do block reservation for us. But
3595 * the buffer head will be unmapped so that
3596 * a read from the block returns 0s.
3597 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003598 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003599 goto out1;
3600 }
3601
3602 /* buffered write, writepage time, convert*/
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003603 ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04003604 if (ret >= 0)
Jan Karab436b9b2009-12-08 23:51:10 -05003605 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04003606out:
3607 if (ret <= 0) {
3608 err = ret;
3609 goto out2;
3610 } else
3611 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003612 map->m_flags |= EXT4_MAP_NEW;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003613 /*
3614 * if we allocated more blocks than requested
3615 * we need to make sure we unmap the extra block
3616 * allocated. The actual needed block will get
3617 * unmapped later when we find the buffer_head marked
3618 * new.
3619 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003620 if (allocated > map->m_len) {
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003621 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003622 newblock + map->m_len,
3623 allocated - map->m_len);
3624 allocated = map->m_len;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003625 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003626
3627 /*
3628 * If we have done fallocate with the offset that is already
3629 * delayed allocated, we would have block reservation
3630 * and quota reservation done in the delayed write path.
3631 * But fallocate would have already updated quota and block
3632 * count for this offset. So cancel these reservation
3633 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003634 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
3635 unsigned int reserved_clusters;
3636 reserved_clusters = get_reserved_cluster_alloc(inode,
3637 map->m_lblk, map->m_len);
3638 if (reserved_clusters)
3639 ext4_da_update_reserve_space(inode,
3640 reserved_clusters,
3641 0);
3642 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003643
Mingming Cao00314622009-09-28 15:49:08 -04003644map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003645 map->m_flags |= EXT4_MAP_MAPPED;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04003646 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
3647 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
3648 map->m_len);
3649 if (err < 0)
3650 goto out2;
3651 }
Mingming Cao00314622009-09-28 15:49:08 -04003652out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003653 if (allocated > map->m_len)
3654 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003655 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003656 map->m_pblk = newblock;
3657 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04003658out2:
3659 if (path) {
3660 ext4_ext_drop_refs(path);
3661 kfree(path);
3662 }
3663 return err ? err : allocated;
3664}
Theodore Ts'o58590b02010-10-27 21:23:12 -04003665
Mingming Cao00314622009-09-28 15:49:08 -04003666/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003667 * get_implied_cluster_alloc - check to see if the requested
3668 * allocation (in the map structure) overlaps with a cluster already
3669 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04003670 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003671 * @map The requested lblk->pblk mapping
3672 * @ex The extent structure which might contain an implied
3673 * cluster allocation
3674 *
3675 * This function is called by ext4_ext_map_blocks() after we failed to
3676 * find blocks that were already in the inode's extent tree. Hence,
3677 * we know that the beginning of the requested region cannot overlap
3678 * the extent from the inode's extent tree. There are three cases we
3679 * want to catch. The first is this case:
3680 *
3681 * |--- cluster # N--|
3682 * |--- extent ---| |---- requested region ---|
3683 * |==========|
3684 *
3685 * The second case that we need to test for is this one:
3686 *
3687 * |--------- cluster # N ----------------|
3688 * |--- requested region --| |------- extent ----|
3689 * |=======================|
3690 *
3691 * The third case is when the requested region lies between two extents
3692 * within the same cluster:
3693 * |------------- cluster # N-------------|
3694 * |----- ex -----| |---- ex_right ----|
3695 * |------ requested region ------|
3696 * |================|
3697 *
3698 * In each of the above cases, we need to set the map->m_pblk and
3699 * map->m_len so it corresponds to the return the extent labelled as
3700 * "|====|" from cluster #N, since it is already in use for data in
3701 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
3702 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
3703 * as a new "allocated" block region. Otherwise, we will return 0 and
3704 * ext4_ext_map_blocks() will then allocate one or more new clusters
3705 * by calling ext4_mb_new_blocks().
3706 */
Aditya Kalid8990242011-09-09 19:18:51 -04003707static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003708 struct ext4_map_blocks *map,
3709 struct ext4_extent *ex,
3710 struct ext4_ext_path *path)
3711{
Aditya Kalid8990242011-09-09 19:18:51 -04003712 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003713 ext4_lblk_t c_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
3714 ext4_lblk_t ex_cluster_start, ex_cluster_end;
Curt Wohlgemuth14d7f3e2011-12-18 17:39:02 -05003715 ext4_lblk_t rr_cluster_start;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003716 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
3717 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
3718 unsigned short ee_len = ext4_ext_get_actual_len(ex);
3719
3720 /* The extent passed in that we are trying to match */
3721 ex_cluster_start = EXT4_B2C(sbi, ee_block);
3722 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
3723
3724 /* The requested region passed into ext4_map_blocks() */
3725 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003726
3727 if ((rr_cluster_start == ex_cluster_end) ||
3728 (rr_cluster_start == ex_cluster_start)) {
3729 if (rr_cluster_start == ex_cluster_end)
3730 ee_start += ee_len - 1;
3731 map->m_pblk = (ee_start & ~(sbi->s_cluster_ratio - 1)) +
3732 c_offset;
3733 map->m_len = min(map->m_len,
3734 (unsigned) sbi->s_cluster_ratio - c_offset);
3735 /*
3736 * Check for and handle this case:
3737 *
3738 * |--------- cluster # N-------------|
3739 * |------- extent ----|
3740 * |--- requested region ---|
3741 * |===========|
3742 */
3743
3744 if (map->m_lblk < ee_block)
3745 map->m_len = min(map->m_len, ee_block - map->m_lblk);
3746
3747 /*
3748 * Check for the case where there is already another allocated
3749 * block to the right of 'ex' but before the end of the cluster.
3750 *
3751 * |------------- cluster # N-------------|
3752 * |----- ex -----| |---- ex_right ----|
3753 * |------ requested region ------|
3754 * |================|
3755 */
3756 if (map->m_lblk > ee_block) {
3757 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
3758 map->m_len = min(map->m_len, next - map->m_lblk);
3759 }
Aditya Kalid8990242011-09-09 19:18:51 -04003760
3761 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003762 return 1;
3763 }
Aditya Kalid8990242011-09-09 19:18:51 -04003764
3765 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003766 return 0;
3767}
3768
3769
3770/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05003771 * Block allocation/map/preallocation routine for extents based files
3772 *
3773 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003774 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003775 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3776 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05003777 *
3778 * return > 0, number of of blocks already mapped/allocated
3779 * if create == 0 and these are pre-allocated blocks
3780 * buffer head is unmapped
3781 * otherwise blocks are mapped
3782 *
3783 * return = 0, if plain look up failed (blocks have not been allocated)
3784 * buffer head is unmapped
3785 *
3786 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003787 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003788int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3789 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07003790{
3791 struct ext4_ext_path *path = NULL;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003792 struct ext4_extent newex, *ex, *ex2;
3793 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003794 ext4_fsblk_t newblock = 0;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003795 int free_on_err = 0, err = 0, depth, ret;
3796 unsigned int allocated = 0, offset = 0;
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04003797 unsigned int allocated_clusters = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003798 struct ext4_allocation_request ar;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003799 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003800 ext4_lblk_t cluster_offset;
Alex Tomasa86c6182006-10-11 01:21:03 -07003801
Mingming84fe3be2009-09-01 08:44:37 -04003802 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003803 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003804 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07003805
3806 /* check in cache */
Lukas Czerner78771912012-03-19 23:05:43 -04003807 if (ext4_ext_in_cache(inode, map->m_lblk, &newex)) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003808 if (!newex.ee_start_lo && !newex.ee_start_hi) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04003809 if ((sbi->s_cluster_ratio > 1) &&
3810 ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
3811 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3812
Theodore Ts'oc2177052009-05-14 00:58:52 -04003813 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003814 /*
3815 * block isn't allocated yet and
3816 * user doesn't want to allocate it
3817 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003818 goto out2;
3819 }
3820 /* we should allocate requested block */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003821 } else {
Alex Tomasa86c6182006-10-11 01:21:03 -07003822 /* block is already allocated */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003823 if (sbi->s_cluster_ratio > 1)
3824 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003825 newblock = map->m_lblk
Dave Kleikamp8c55e202007-05-24 13:04:54 -04003826 - le32_to_cpu(newex.ee_block)
Theodore Ts'obf89d162010-10-27 21:30:14 -04003827 + ext4_ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003828 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003829 allocated = ext4_ext_get_actual_len(&newex) -
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003830 (map->m_lblk - le32_to_cpu(newex.ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -07003831 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07003832 }
3833 }
3834
3835 /* find extent for this block */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003836 path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07003837 if (IS_ERR(path)) {
3838 err = PTR_ERR(path);
3839 path = NULL;
3840 goto out2;
3841 }
3842
3843 depth = ext_depth(inode);
3844
3845 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003846 * consistent leaf must not be empty;
3847 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07003848 * this is why assert can't be put in ext4_ext_find_extent()
3849 */
Frank Mayhar273df552010-03-02 11:46:09 -05003850 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
3851 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04003852 "lblock: %lu, depth: %d pblock %lld",
3853 (unsigned long) map->m_lblk, depth,
3854 path[depth].p_block);
Surbhi Palande034fb4c2009-12-14 09:53:52 -05003855 err = -EIO;
3856 goto out2;
3857 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003858
Avantika Mathur7e028972006-12-06 20:41:33 -08003859 ex = path[depth].p_ext;
3860 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003861 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003862 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003863 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003864
3865 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003866 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04003867 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003868 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003869 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04003870
3871 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
3872
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003873 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003874 if (in_range(map->m_lblk, ee_block, ee_len)) {
3875 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003876 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003877 allocated = ee_len - (map->m_lblk - ee_block);
3878 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
3879 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04003880
Allison Hendersone8613042011-05-25 07:41:46 -04003881 /*
Lukas Czerner78771912012-03-19 23:05:43 -04003882 * Do not put uninitialized extent
3883 * in the cache
Allison Hendersone8613042011-05-25 07:41:46 -04003884 */
Lukas Czerner78771912012-03-19 23:05:43 -04003885 if (!ext4_ext_is_uninitialized(ex)) {
3886 ext4_ext_put_in_cache(inode, ee_block,
3887 ee_len, ee_start);
3888 goto out;
Allison Hendersone8613042011-05-25 07:41:46 -04003889 }
Lukas Czerner78771912012-03-19 23:05:43 -04003890 ret = ext4_ext_handle_uninitialized_extents(
3891 handle, inode, map, path, flags,
3892 allocated, newblock);
3893 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07003894 }
3895 }
3896
Aditya Kali7b415bf2011-09-09 19:04:51 -04003897 if ((sbi->s_cluster_ratio > 1) &&
3898 ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
3899 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3900
Alex Tomasa86c6182006-10-11 01:21:03 -07003901 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003902 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07003903 * we couldn't try to create block if create flag is zero
3904 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04003905 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003906 /*
3907 * put just found gap into cache to speed up
3908 * subsequent requests
3909 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003910 ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
Alex Tomasa86c6182006-10-11 01:21:03 -07003911 goto out2;
3912 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003913
Alex Tomasa86c6182006-10-11 01:21:03 -07003914 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003915 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07003916 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003917 map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003918 newex.ee_block = cpu_to_le32(map->m_lblk);
3919 cluster_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
3920
3921 /*
3922 * If we are doing bigalloc, check to see if the extent returned
3923 * by ext4_ext_find_extent() implies a cluster we can use.
3924 */
3925 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04003926 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003927 ar.len = allocated = map->m_len;
3928 newblock = map->m_pblk;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003929 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003930 goto got_allocated_blocks;
3931 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003932
Alex Tomasc9de5602008-01-29 00:19:52 -05003933 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003934 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003935 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3936 if (err)
3937 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003938 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003939 ex2 = NULL;
3940 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
Alex Tomasc9de5602008-01-29 00:19:52 -05003941 if (err)
3942 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04003943
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003944 /* Check if the extent after searching to the right implies a
3945 * cluster we can use. */
3946 if ((sbi->s_cluster_ratio > 1) && ex2 &&
Aditya Kalid8990242011-09-09 19:18:51 -04003947 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003948 ar.len = allocated = map->m_len;
3949 newblock = map->m_pblk;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003950 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003951 goto got_allocated_blocks;
3952 }
3953
Amit Arora749269f2007-07-18 09:02:56 -04003954 /*
3955 * See if request is beyond maximum number of blocks we can have in
3956 * a single extent. For an initialized extent this limit is
3957 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3958 * EXT_UNINIT_MAX_LEN.
3959 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003960 if (map->m_len > EXT_INIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003961 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003962 map->m_len = EXT_INIT_MAX_LEN;
3963 else if (map->m_len > EXT_UNINIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003964 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003965 map->m_len = EXT_UNINIT_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04003966
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003967 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003968 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003969 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04003970 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003971 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04003972 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003973 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003974
3975 /* allocate new block */
3976 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003977 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
3978 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003979 /*
3980 * We calculate the offset from the beginning of the cluster
3981 * for the logical block number, since when we allocate a
3982 * physical cluster, the physical block should start at the
3983 * same offset from the beginning of the cluster. This is
3984 * needed so that future calls to get_implied_cluster_alloc()
3985 * work correctly.
3986 */
3987 offset = map->m_lblk & (sbi->s_cluster_ratio - 1);
3988 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
3989 ar.goal -= offset;
3990 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05003991 if (S_ISREG(inode->i_mode))
3992 ar.flags = EXT4_MB_HINT_DATA;
3993 else
3994 /* disable in-core preallocation for non-regular files */
3995 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04003996 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
3997 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05003998 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07003999 if (!newblock)
4000 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04004001 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004002 ar.goal, newblock, allocated);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004003 free_on_err = 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004004 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004005 ar.len = EXT4_C2B(sbi, ar.len) - offset;
4006 if (ar.len > allocated)
4007 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004008
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004009got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07004010 /* try to insert new extent into found leaf and return */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004011 ext4_ext_store_pblock(&newex, newblock + offset);
Alex Tomasc9de5602008-01-29 00:19:52 -05004012 newex.ee_len = cpu_to_le16(ar.len);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004013 /* Mark uninitialized */
4014 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
Amit Aroraa2df2a62007-07-17 21:42:41 -04004015 ext4_ext_mark_uninitialized(&newex);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004016 /*
Jiaying Zhang744692d2010-03-04 16:14:02 -05004017 * io_end structure was created for every IO write to an
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004018 * uninitialized extent. To avoid unnecessary conversion,
Jiaying Zhang744692d2010-03-04 16:14:02 -05004019 * here we flag the IO that really needs the conversion.
Mingming5f5249502009-11-10 10:48:04 -05004020 * For non asycn direct IO case, flag the inode state
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004021 * that we need to perform conversion when IO is done.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004022 */
Jiaying Zhang744692d2010-03-04 16:14:02 -05004023 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Tao Ma0edeb712011-10-31 17:30:44 -04004024 if (io)
4025 ext4_set_io_unwritten_flag(inode, io);
4026 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004027 ext4_set_inode_state(inode,
4028 EXT4_STATE_DIO_UNWRITTEN);
Mingming5f5249502009-11-10 10:48:04 -05004029 }
Jiaying Zhang744692d2010-03-04 16:14:02 -05004030 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004031 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004032 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004033
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004034 err = 0;
4035 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
4036 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4037 path, ar.len);
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004038 if (!err)
4039 err = ext4_ext_insert_extent(handle, inode, path,
4040 &newex, flags);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004041 if (err && free_on_err) {
Maxim Patlasov7132de72011-07-10 19:37:48 -04004042 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
4043 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
Alex Tomas315054f2007-05-24 13:04:25 -04004044 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05004045 /* not a good idea to call discard here directly,
4046 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004047 ext4_discard_preallocations(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05004048 ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex),
Maxim Patlasov7132de72011-07-10 19:37:48 -04004049 ext4_ext_get_actual_len(&newex), fb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004050 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04004051 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004052
Alex Tomasa86c6182006-10-11 01:21:03 -07004053 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04004054 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004055 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004056 if (allocated > map->m_len)
4057 allocated = map->m_len;
4058 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07004059
Jan Karab436b9b2009-12-08 23:51:10 -05004060 /*
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004061 * Update reserved blocks/metadata blocks after successful
4062 * block allocation which had been deferred till now.
4063 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04004064 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004065 unsigned int reserved_clusters;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004066 /*
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004067 * Check how many clusters we had reserved this allocated range
Aditya Kali7b415bf2011-09-09 19:04:51 -04004068 */
4069 reserved_clusters = get_reserved_cluster_alloc(inode,
4070 map->m_lblk, allocated);
4071 if (map->m_flags & EXT4_MAP_FROM_CLUSTER) {
4072 if (reserved_clusters) {
4073 /*
4074 * We have clusters reserved for this range.
4075 * But since we are not doing actual allocation
4076 * and are simply using blocks from previously
4077 * allocated cluster, we should release the
4078 * reservation and not claim quota.
4079 */
4080 ext4_da_update_reserve_space(inode,
4081 reserved_clusters, 0);
4082 }
4083 } else {
4084 BUG_ON(allocated_clusters < reserved_clusters);
4085 /* We will claim quota for all newly allocated blocks.*/
4086 ext4_da_update_reserve_space(inode, allocated_clusters,
4087 1);
4088 if (reserved_clusters < allocated_clusters) {
Aditya Kali5356f262011-09-09 19:20:51 -04004089 struct ext4_inode_info *ei = EXT4_I(inode);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004090 int reservation = allocated_clusters -
4091 reserved_clusters;
4092 /*
4093 * It seems we claimed few clusters outside of
4094 * the range of this allocation. We should give
4095 * it back to the reservation pool. This can
4096 * happen in the following case:
4097 *
4098 * * Suppose s_cluster_ratio is 4 (i.e., each
4099 * cluster has 4 blocks. Thus, the clusters
4100 * are [0-3],[4-7],[8-11]...
4101 * * First comes delayed allocation write for
4102 * logical blocks 10 & 11. Since there were no
4103 * previous delayed allocated blocks in the
4104 * range [8-11], we would reserve 1 cluster
4105 * for this write.
4106 * * Next comes write for logical blocks 3 to 8.
4107 * In this case, we will reserve 2 clusters
4108 * (for [0-3] and [4-7]; and not for [8-11] as
4109 * that range has a delayed allocated blocks.
4110 * Thus total reserved clusters now becomes 3.
4111 * * Now, during the delayed allocation writeout
4112 * time, we will first write blocks [3-8] and
4113 * allocate 3 clusters for writing these
4114 * blocks. Also, we would claim all these
4115 * three clusters above.
4116 * * Now when we come here to writeout the
4117 * blocks [10-11], we would expect to claim
4118 * the reservation of 1 cluster we had made
4119 * (and we would claim it since there are no
4120 * more delayed allocated blocks in the range
4121 * [8-11]. But our reserved cluster count had
4122 * already gone to 0.
4123 *
4124 * Thus, at the step 4 above when we determine
4125 * that there are still some unwritten delayed
4126 * allocated blocks outside of our current
4127 * block range, we should increment the
4128 * reserved clusters count so that when the
4129 * remaining blocks finally gets written, we
4130 * could claim them.
4131 */
Aditya Kali5356f262011-09-09 19:20:51 -04004132 dquot_reserve_block(inode,
4133 EXT4_C2B(sbi, reservation));
4134 spin_lock(&ei->i_block_reservation_lock);
4135 ei->i_reserved_data_blocks += reservation;
4136 spin_unlock(&ei->i_block_reservation_lock);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004137 }
4138 }
4139 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004140
4141 /*
Jan Karab436b9b2009-12-08 23:51:10 -05004142 * Cache the extent and update transaction to commit on fdatasync only
4143 * when it is _not_ an uninitialized extent.
4144 */
4145 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05004146 ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock);
Jan Karab436b9b2009-12-08 23:51:10 -05004147 ext4_update_inode_fsync_trans(handle, inode, 1);
4148 } else
4149 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004150out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004151 if (allocated > map->m_len)
4152 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004153 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004154 map->m_flags |= EXT4_MAP_MAPPED;
4155 map->m_pblk = newblock;
4156 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004157out2:
4158 if (path) {
4159 ext4_ext_drop_refs(path);
4160 kfree(path);
4161 }
Allison Hendersone8613042011-05-25 07:41:46 -04004162
Yongqiang Yange7b319e2011-10-29 09:39:51 -04004163 trace_ext4_ext_map_blocks_exit(inode, map->m_lblk,
Lukas Czerner78771912012-03-19 23:05:43 -04004164 newblock, map->m_len, err ? err : allocated);
Yongqiang Yange7b319e2011-10-29 09:39:51 -04004165
Lukas Czerner78771912012-03-19 23:05:43 -04004166 return err ? err : allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004167}
4168
Jan Karacf108bc2008-07-11 19:27:31 -04004169void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004170{
4171 struct address_space *mapping = inode->i_mapping;
4172 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004173 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004174 handle_t *handle;
Allison Henderson189e8682011-09-06 21:49:44 -04004175 loff_t page_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004176 int err = 0;
4177
4178 /*
Jiaying Zhang3889fd52011-01-10 12:47:05 -05004179 * finish any pending end_io work so we won't run the risk of
4180 * converting any truncated blocks to initialized later
4181 */
4182 ext4_flush_completed_IO(inode);
4183
4184 /*
Alex Tomasa86c6182006-10-11 01:21:03 -07004185 * probably first extent we're gonna free will be last in block
4186 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04004187 err = ext4_writepage_trans_blocks(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07004188 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04004189 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07004190 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07004191
Allison Henderson189e8682011-09-06 21:49:44 -04004192 if (inode->i_size % PAGE_CACHE_SIZE != 0) {
4193 page_len = PAGE_CACHE_SIZE -
4194 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4195
4196 err = ext4_discard_partial_page_buffers(handle,
4197 mapping, inode->i_size, page_len, 0);
4198
4199 if (err)
4200 goto out_stop;
4201 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004202
Jan Kara9ddfc3d2008-07-11 19:27:31 -04004203 if (ext4_orphan_add(handle, inode))
4204 goto out_stop;
4205
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004206 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07004207 ext4_ext_invalidate_cache(inode);
4208
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004209 ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05004210
Alex Tomasa86c6182006-10-11 01:21:03 -07004211 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004212 * TODO: optimization is possible here.
4213 * Probably we need not scan at all,
4214 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004215 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004216
4217 /* we have to know where to truncate from in crash case */
4218 EXT4_I(inode)->i_disksize = inode->i_size;
4219 ext4_mark_inode_dirty(handle, inode);
4220
4221 last_block = (inode->i_size + sb->s_blocksize - 1)
4222 >> EXT4_BLOCK_SIZE_BITS(sb);
Lukas Czerner5f95d212012-03-19 23:03:19 -04004223 err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07004224
4225 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04004226 * transaction synchronous.
4227 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004228 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05004229 ext4_handle_sync(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07004230
Jan Kara9ddfc3d2008-07-11 19:27:31 -04004231 up_write(&EXT4_I(inode)->i_data_sem);
Eric Gouriouf6d2f6b2011-05-22 21:33:00 -04004232
4233out_stop:
Alex Tomasa86c6182006-10-11 01:21:03 -07004234 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004235 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07004236 * then we need to clear up the orphan record which we created above.
4237 * However, if this was a real unlink then we were called by
4238 * ext4_delete_inode(), and we allow that function to clean up the
4239 * orphan info for us.
4240 */
4241 if (inode->i_nlink)
4242 ext4_orphan_del(handle, inode);
4243
Solofo Ramangalahyef737722008-04-29 22:00:41 -04004244 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4245 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07004246 ext4_journal_stop(handle);
4247}
4248
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004249static void ext4_falloc_update_inode(struct inode *inode,
4250 int mode, loff_t new_size, int update_ctime)
4251{
4252 struct timespec now;
4253
4254 if (update_ctime) {
4255 now = current_fs_time(inode->i_sb);
4256 if (!timespec_equal(&inode->i_ctime, &now))
4257 inode->i_ctime = now;
4258 }
4259 /*
4260 * Update only when preallocation was requested beyond
4261 * the file size.
4262 */
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04004263 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
4264 if (new_size > i_size_read(inode))
4265 i_size_write(inode, new_size);
4266 if (new_size > EXT4_I(inode)->i_disksize)
4267 ext4_update_i_disksize(inode, new_size);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004268 } else {
4269 /*
4270 * Mark that we allocate beyond EOF so the subsequent truncate
4271 * can proceed even if the new size is the same as i_size.
4272 */
4273 if (new_size > i_size_read(inode))
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004274 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004275 }
4276
4277}
4278
Amit Aroraa2df2a62007-07-17 21:42:41 -04004279/*
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004280 * preallocate space for a file. This implements ext4's fallocate file
Amit Aroraa2df2a62007-07-17 21:42:41 -04004281 * operation, which gets called from sys_fallocate system call.
4282 * For block-mapped files, posix_fallocate should fall back to the method
4283 * of writing zeroes to the required new blocks (the same behavior which is
4284 * expected for file systems which do not support fallocate() system call).
4285 */
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004286long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004287{
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004288 struct inode *inode = file->f_path.dentry->d_inode;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004289 handle_t *handle;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004290 loff_t new_size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004291 unsigned int max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004292 int ret = 0;
4293 int ret2 = 0;
4294 int retries = 0;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004295 int flags;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004296 struct ext4_map_blocks map;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004297 unsigned int credits, blkbits = inode->i_blkbits;
4298
4299 /*
4300 * currently supporting (pre)allocate mode for extent-based
4301 * files _only_
4302 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004303 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Amit Aroraa2df2a62007-07-17 21:42:41 -04004304 return -EOPNOTSUPP;
4305
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004306 /* Return error if mode is not supported */
4307 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
4308 return -EOPNOTSUPP;
4309
4310 if (mode & FALLOC_FL_PUNCH_HOLE)
4311 return ext4_punch_hole(file, offset, len);
4312
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004313 trace_ext4_fallocate_enter(inode, offset, len, mode);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004314 map.m_lblk = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004315 /*
4316 * We can't just convert len to max_blocks because
4317 * If blocksize = 4096 offset = 3072 and len = 2048
4318 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004319 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004320 - map.m_lblk;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004321 /*
Mingming Caof3bd1f32008-08-19 22:16:03 -04004322 * credits to insert 1 extent into extent tree
Amit Aroraa2df2a62007-07-17 21:42:41 -04004323 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04004324 credits = ext4_chunk_trans_blocks(inode, max_blocks);
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05004325 mutex_lock(&inode->i_mutex);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04004326 ret = inode_newsize_ok(inode, (len + offset));
4327 if (ret) {
4328 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004329 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04004330 return ret;
4331 }
Greg Harm3c6fe772011-10-31 18:41:47 -04004332 flags = EXT4_GET_BLOCKS_CREATE_UNINIT_EXT;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004333 if (mode & FALLOC_FL_KEEP_SIZE)
4334 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
Greg Harm3c6fe772011-10-31 18:41:47 -04004335 /*
4336 * Don't normalize the request if it can fit in one extent so
4337 * that it doesn't get unnecessarily split into multiple
4338 * extents.
4339 */
4340 if (len <= EXT_UNINIT_MAX_LEN << blkbits)
4341 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004342retry:
4343 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004344 map.m_lblk = map.m_lblk + ret;
4345 map.m_len = max_blocks = max_blocks - ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004346 handle = ext4_journal_start(inode, credits);
4347 if (IS_ERR(handle)) {
4348 ret = PTR_ERR(handle);
4349 break;
4350 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004351 ret = ext4_map_blocks(handle, inode, &map, flags);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05004352 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004353#ifdef EXT4FS_DEBUG
4354 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004355 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004356 "returned error inode#%lu, block=%u, "
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05004357 "max_blocks=%u", __func__,
Kazuya Mioa6371b62010-10-27 21:30:15 -04004358 inode->i_ino, map.m_lblk, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004359#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04004360 ext4_mark_inode_dirty(handle, inode);
4361 ret2 = ext4_journal_stop(handle);
4362 break;
4363 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004364 if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004365 blkbits) >> blkbits))
4366 new_size = offset + len;
4367 else
Utako Kusaka29ae07b2011-07-27 22:11:20 -04004368 new_size = ((loff_t) map.m_lblk + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004369
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004370 ext4_falloc_update_inode(inode, mode, new_size,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004371 (map.m_flags & EXT4_MAP_NEW));
Amit Aroraa2df2a62007-07-17 21:42:41 -04004372 ext4_mark_inode_dirty(handle, inode);
4373 ret2 = ext4_journal_stop(handle);
4374 if (ret2)
4375 break;
4376 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004377 if (ret == -ENOSPC &&
4378 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4379 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004380 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004381 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05004382 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004383 trace_ext4_fallocate_exit(inode, offset, max_blocks,
4384 ret > 0 ? ret2 : ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004385 return ret > 0 ? ret2 : ret;
4386}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004387
4388/*
Mingming Cao00314622009-09-28 15:49:08 -04004389 * This function convert a range of blocks to written extents
4390 * The caller of this function will pass the start offset and the size.
4391 * all unwritten extents within this range will be converted to
4392 * written extents.
4393 *
4394 * This function is called from the direct IO end io call back
4395 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05004396 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04004397 */
4398int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
Eric Sandeena1de02d2010-02-04 23:58:38 -05004399 ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04004400{
4401 handle_t *handle;
Mingming Cao00314622009-09-28 15:49:08 -04004402 unsigned int max_blocks;
4403 int ret = 0;
4404 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004405 struct ext4_map_blocks map;
Mingming Cao00314622009-09-28 15:49:08 -04004406 unsigned int credits, blkbits = inode->i_blkbits;
4407
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004408 map.m_lblk = offset >> blkbits;
Mingming Cao00314622009-09-28 15:49:08 -04004409 /*
4410 * We can't just convert len to max_blocks because
4411 * If blocksize = 4096 offset = 3072 and len = 2048
4412 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004413 max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
4414 map.m_lblk);
Mingming Cao00314622009-09-28 15:49:08 -04004415 /*
4416 * credits to insert 1 extent into extent tree
4417 */
4418 credits = ext4_chunk_trans_blocks(inode, max_blocks);
4419 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004420 map.m_lblk += ret;
4421 map.m_len = (max_blocks -= ret);
Mingming Cao00314622009-09-28 15:49:08 -04004422 handle = ext4_journal_start(inode, credits);
4423 if (IS_ERR(handle)) {
4424 ret = PTR_ERR(handle);
4425 break;
4426 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004427 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004428 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Mingming Cao00314622009-09-28 15:49:08 -04004429 if (ret <= 0) {
4430 WARN_ON(ret <= 0);
Theodore Ts'o92b97812012-03-19 23:41:49 -04004431 ext4_msg(inode->i_sb, KERN_ERR,
4432 "%s:%d: inode #%lu: block %u: len %u: "
4433 "ext4_ext_map_blocks returned %d",
4434 __func__, __LINE__, inode->i_ino, map.m_lblk,
4435 map.m_len, ret);
Mingming Cao00314622009-09-28 15:49:08 -04004436 }
4437 ext4_mark_inode_dirty(handle, inode);
4438 ret2 = ext4_journal_stop(handle);
4439 if (ret <= 0 || ret2 )
4440 break;
4441 }
4442 return ret > 0 ? ret2 : ret;
4443}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004444
Mingming Cao00314622009-09-28 15:49:08 -04004445/*
Eric Sandeen6873fa02008-10-07 00:46:36 -04004446 * Callback function called for each extent to gather FIEMAP information.
4447 */
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04004448static int ext4_ext_fiemap_cb(struct inode *inode, ext4_lblk_t next,
Eric Sandeen6873fa02008-10-07 00:46:36 -04004449 struct ext4_ext_cache *newex, struct ext4_extent *ex,
4450 void *data)
4451{
Eric Sandeen6873fa02008-10-07 00:46:36 -04004452 __u64 logical;
4453 __u64 physical;
4454 __u64 length;
4455 __u32 flags = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004456 int ret = 0;
4457 struct fiemap_extent_info *fieinfo = data;
4458 unsigned char blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004459
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004460 blksize_bits = inode->i_sb->s_blocksize_bits;
4461 logical = (__u64)newex->ec_block << blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004462
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05004463 if (newex->ec_start == 0) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004464 /*
4465 * No extent in extent-tree contains block @newex->ec_start,
4466 * then the block may stay in 1)a hole or 2)delayed-extent.
4467 *
4468 * Holes or delayed-extents are processed as follows.
4469 * 1. lookup dirty pages with specified range in pagecache.
4470 * If no page is got, then there is no delayed-extent and
4471 * return with EXT_CONTINUE.
4472 * 2. find the 1st mapped buffer,
4473 * 3. check if the mapped buffer is both in the request range
4474 * and a delayed buffer. If not, there is no delayed-extent,
4475 * then return.
4476 * 4. a delayed-extent is found, the extent will be collected.
4477 */
4478 ext4_lblk_t end = 0;
4479 pgoff_t last_offset;
4480 pgoff_t offset;
4481 pgoff_t index;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004482 pgoff_t start_index = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004483 struct page **pages = NULL;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004484 struct buffer_head *bh = NULL;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004485 struct buffer_head *head = NULL;
4486 unsigned int nr_pages = PAGE_SIZE / sizeof(struct page *);
4487
4488 pages = kmalloc(PAGE_SIZE, GFP_KERNEL);
4489 if (pages == NULL)
4490 return -ENOMEM;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004491
4492 offset = logical >> PAGE_SHIFT;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004493repeat:
4494 last_offset = offset;
4495 head = NULL;
4496 ret = find_get_pages_tag(inode->i_mapping, &offset,
4497 PAGECACHE_TAG_DIRTY, nr_pages, pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004498
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004499 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4500 /* First time, try to find a mapped buffer. */
4501 if (ret == 0) {
4502out:
4503 for (index = 0; index < ret; index++)
4504 page_cache_release(pages[index]);
4505 /* just a hole. */
4506 kfree(pages);
4507 return EXT_CONTINUE;
4508 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004509 index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004510
Yongqiang Yangb2213492011-05-24 11:36:58 -04004511next_page:
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004512 /* Try to find the 1st mapped buffer. */
Yongqiang Yangb2213492011-05-24 11:36:58 -04004513 end = ((__u64)pages[index]->index << PAGE_SHIFT) >>
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004514 blksize_bits;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004515 if (!page_has_buffers(pages[index]))
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004516 goto out;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004517 head = page_buffers(pages[index]);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004518 if (!head)
4519 goto out;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004520
Yongqiang Yangb2213492011-05-24 11:36:58 -04004521 index++;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004522 bh = head;
4523 do {
Yongqiang Yangb2213492011-05-24 11:36:58 -04004524 if (end >= newex->ec_block +
4525 newex->ec_len)
4526 /* The buffer is out of
4527 * the request range.
4528 */
4529 goto out;
4530
4531 if (buffer_mapped(bh) &&
4532 end >= newex->ec_block) {
4533 start_index = index - 1;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004534 /* get the 1st mapped buffer. */
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004535 goto found_mapped_buffer;
4536 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004537
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004538 bh = bh->b_this_page;
4539 end++;
4540 } while (bh != head);
4541
Yongqiang Yangb2213492011-05-24 11:36:58 -04004542 /* No mapped buffer in the range found in this page,
4543 * We need to look up next page.
4544 */
4545 if (index >= ret) {
4546 /* There is no page left, but we need to limit
4547 * newex->ec_len.
4548 */
4549 newex->ec_len = end - newex->ec_block;
4550 goto out;
4551 }
4552 goto next_page;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004553 } else {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004554 /*Find contiguous delayed buffers. */
4555 if (ret > 0 && pages[0]->index == last_offset)
4556 head = page_buffers(pages[0]);
4557 bh = head;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004558 index = 1;
4559 start_index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004560 }
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004561
4562found_mapped_buffer:
4563 if (bh != NULL && buffer_delay(bh)) {
4564 /* 1st or contiguous delayed buffer found. */
4565 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4566 /*
4567 * 1st delayed buffer found, record
4568 * the start of extent.
4569 */
4570 flags |= FIEMAP_EXTENT_DELALLOC;
4571 newex->ec_block = end;
4572 logical = (__u64)end << blksize_bits;
4573 }
4574 /* Find contiguous delayed buffers. */
4575 do {
4576 if (!buffer_delay(bh))
4577 goto found_delayed_extent;
4578 bh = bh->b_this_page;
4579 end++;
4580 } while (bh != head);
4581
Yongqiang Yangb2213492011-05-24 11:36:58 -04004582 for (; index < ret; index++) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004583 if (!page_has_buffers(pages[index])) {
4584 bh = NULL;
4585 break;
4586 }
4587 head = page_buffers(pages[index]);
4588 if (!head) {
4589 bh = NULL;
4590 break;
4591 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004592
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004593 if (pages[index]->index !=
Yongqiang Yangb2213492011-05-24 11:36:58 -04004594 pages[start_index]->index + index
4595 - start_index) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004596 /* Blocks are not contiguous. */
4597 bh = NULL;
4598 break;
4599 }
4600 bh = head;
4601 do {
4602 if (!buffer_delay(bh))
4603 /* Delayed-extent ends. */
4604 goto found_delayed_extent;
4605 bh = bh->b_this_page;
4606 end++;
4607 } while (bh != head);
4608 }
4609 } else if (!(flags & FIEMAP_EXTENT_DELALLOC))
4610 /* a hole found. */
4611 goto out;
4612
4613found_delayed_extent:
4614 newex->ec_len = min(end - newex->ec_block,
4615 (ext4_lblk_t)EXT_INIT_MAX_LEN);
4616 if (ret == nr_pages && bh != NULL &&
4617 newex->ec_len < EXT_INIT_MAX_LEN &&
4618 buffer_delay(bh)) {
4619 /* Have not collected an extent and continue. */
4620 for (index = 0; index < ret; index++)
4621 page_cache_release(pages[index]);
4622 goto repeat;
4623 }
4624
4625 for (index = 0; index < ret; index++)
4626 page_cache_release(pages[index]);
4627 kfree(pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004628 }
4629
4630 physical = (__u64)newex->ec_start << blksize_bits;
4631 length = (__u64)newex->ec_len << blksize_bits;
4632
4633 if (ex && ext4_ext_is_uninitialized(ex))
4634 flags |= FIEMAP_EXTENT_UNWRITTEN;
4635
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04004636 if (next == EXT_MAX_BLOCKS)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004637 flags |= FIEMAP_EXTENT_LAST;
4638
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004639 ret = fiemap_fill_next_extent(fieinfo, logical, physical,
Eric Sandeen6873fa02008-10-07 00:46:36 -04004640 length, flags);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004641 if (ret < 0)
4642 return ret;
4643 if (ret == 1)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004644 return EXT_BREAK;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004645 return EXT_CONTINUE;
4646}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004647/* fiemap flags we can handle specified here */
4648#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
4649
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004650static int ext4_xattr_fiemap(struct inode *inode,
4651 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004652{
4653 __u64 physical = 0;
4654 __u64 length;
4655 __u32 flags = FIEMAP_EXTENT_LAST;
4656 int blockbits = inode->i_sb->s_blocksize_bits;
4657 int error = 0;
4658
4659 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004660 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04004661 struct ext4_iloc iloc;
4662 int offset; /* offset of xattr in inode */
4663
4664 error = ext4_get_inode_loc(inode, &iloc);
4665 if (error)
4666 return error;
4667 physical = iloc.bh->b_blocknr << blockbits;
4668 offset = EXT4_GOOD_OLD_INODE_SIZE +
4669 EXT4_I(inode)->i_extra_isize;
4670 physical += offset;
4671 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
4672 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004673 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004674 } else { /* external block */
4675 physical = EXT4_I(inode)->i_file_acl << blockbits;
4676 length = inode->i_sb->s_blocksize;
4677 }
4678
4679 if (physical)
4680 error = fiemap_fill_next_extent(fieinfo, 0, physical,
4681 length, flags);
4682 return (error < 0 ? error : 0);
4683}
4684
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004685/*
4686 * ext4_ext_punch_hole
4687 *
4688 * Punches a hole of "length" bytes in a file starting
4689 * at byte "offset"
4690 *
4691 * @inode: The inode of the file to punch a hole in
4692 * @offset: The starting byte offset of the hole
4693 * @length: The length of the hole
4694 *
4695 * Returns the number of blocks removed or negative on err
4696 */
4697int ext4_ext_punch_hole(struct file *file, loff_t offset, loff_t length)
4698{
4699 struct inode *inode = file->f_path.dentry->d_inode;
4700 struct super_block *sb = inode->i_sb;
Lukas Czerner5f95d212012-03-19 23:03:19 -04004701 ext4_lblk_t first_block, stop_block;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004702 struct address_space *mapping = inode->i_mapping;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004703 handle_t *handle;
Allison Hendersonba062082011-09-03 11:55:59 -04004704 loff_t first_page, last_page, page_len;
4705 loff_t first_page_offset, last_page_offset;
Lukas Czerner5f95d212012-03-19 23:03:19 -04004706 int credits, err = 0;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004707
Allison Henderson2be47512011-09-03 11:56:52 -04004708 /* No need to punch hole beyond i_size */
4709 if (offset >= inode->i_size)
4710 return 0;
4711
4712 /*
4713 * If the hole extends beyond i_size, set the hole
4714 * to end after the page that contains i_size
4715 */
4716 if (offset + length > inode->i_size) {
4717 length = inode->i_size +
4718 PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) -
4719 offset;
4720 }
4721
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004722 first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
4723 last_page = (offset + length) >> PAGE_CACHE_SHIFT;
4724
4725 first_page_offset = first_page << PAGE_CACHE_SHIFT;
4726 last_page_offset = last_page << PAGE_CACHE_SHIFT;
4727
4728 /*
4729 * Write out all dirty pages to avoid race conditions
4730 * Then release them.
4731 */
4732 if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
4733 err = filemap_write_and_wait_range(mapping,
Allison Henderson2be47512011-09-03 11:56:52 -04004734 offset, offset + length - 1);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004735
Allison Henderson2be47512011-09-03 11:56:52 -04004736 if (err)
4737 return err;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004738 }
4739
4740 /* Now release the pages */
4741 if (last_page_offset > first_page_offset) {
4742 truncate_inode_pages_range(mapping, first_page_offset,
4743 last_page_offset-1);
4744 }
4745
4746 /* finish any pending end_io work */
4747 ext4_flush_completed_IO(inode);
4748
4749 credits = ext4_writepage_trans_blocks(inode);
4750 handle = ext4_journal_start(inode, credits);
4751 if (IS_ERR(handle))
4752 return PTR_ERR(handle);
4753
4754 err = ext4_orphan_add(handle, inode);
4755 if (err)
4756 goto out;
4757
4758 /*
Allison Hendersonba062082011-09-03 11:55:59 -04004759 * Now we need to zero out the non-page-aligned data in the
4760 * pages at the start and tail of the hole, and unmap the buffer
4761 * heads for the block aligned regions of the page that were
4762 * completely zeroed.
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004763 */
Allison Hendersonba062082011-09-03 11:55:59 -04004764 if (first_page > last_page) {
4765 /*
4766 * If the file space being truncated is contained within a page
4767 * just zero out and unmap the middle of that page
4768 */
4769 err = ext4_discard_partial_page_buffers(handle,
4770 mapping, offset, length, 0);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004771
Allison Hendersonba062082011-09-03 11:55:59 -04004772 if (err)
4773 goto out;
4774 } else {
4775 /*
4776 * zero out and unmap the partial page that contains
4777 * the start of the hole
4778 */
4779 page_len = first_page_offset - offset;
4780 if (page_len > 0) {
4781 err = ext4_discard_partial_page_buffers(handle, mapping,
4782 offset, page_len, 0);
4783 if (err)
4784 goto out;
4785 }
4786
4787 /*
4788 * zero out and unmap the partial page that contains
4789 * the end of the hole
4790 */
4791 page_len = offset + length - last_page_offset;
4792 if (page_len > 0) {
4793 err = ext4_discard_partial_page_buffers(handle, mapping,
4794 last_page_offset, page_len, 0);
4795 if (err)
4796 goto out;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004797 }
4798 }
4799
Allison Henderson2be47512011-09-03 11:56:52 -04004800 /*
4801 * If i_size is contained in the last page, we need to
4802 * unmap and zero the partial page after i_size
4803 */
4804 if (inode->i_size >> PAGE_CACHE_SHIFT == last_page &&
4805 inode->i_size % PAGE_CACHE_SIZE != 0) {
4806
4807 page_len = PAGE_CACHE_SIZE -
4808 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4809
4810 if (page_len > 0) {
4811 err = ext4_discard_partial_page_buffers(handle,
4812 mapping, inode->i_size, page_len, 0);
4813
4814 if (err)
4815 goto out;
4816 }
4817 }
4818
Lukas Czerner5f95d212012-03-19 23:03:19 -04004819 first_block = (offset + sb->s_blocksize - 1) >>
4820 EXT4_BLOCK_SIZE_BITS(sb);
4821 stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
4822
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004823 /* If there are no blocks to remove, return now */
Lukas Czerner5f95d212012-03-19 23:03:19 -04004824 if (first_block >= stop_block)
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004825 goto out;
4826
4827 down_write(&EXT4_I(inode)->i_data_sem);
4828 ext4_ext_invalidate_cache(inode);
4829 ext4_discard_preallocations(inode);
4830
Lukas Czerner5f95d212012-03-19 23:03:19 -04004831 err = ext4_ext_remove_space(inode, first_block, stop_block - 1);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004832
Lukas Czerner5f95d212012-03-19 23:03:19 -04004833 ext4_ext_invalidate_cache(inode);
4834 ext4_discard_preallocations(inode);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004835
4836 if (IS_SYNC(inode))
4837 ext4_handle_sync(handle);
4838
4839 up_write(&EXT4_I(inode)->i_data_sem);
4840
4841out:
4842 ext4_orphan_del(handle, inode);
4843 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4844 ext4_mark_inode_dirty(handle, inode);
4845 ext4_journal_stop(handle);
4846 return err;
4847}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004848int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4849 __u64 start, __u64 len)
4850{
4851 ext4_lblk_t start_blk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004852 int error = 0;
4853
4854 /* fallback to generic here if not in extents fmt */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004855 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeen6873fa02008-10-07 00:46:36 -04004856 return generic_block_fiemap(inode, fieinfo, start, len,
4857 ext4_get_block);
4858
4859 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
4860 return -EBADR;
4861
4862 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
4863 error = ext4_xattr_fiemap(inode, fieinfo);
4864 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004865 ext4_lblk_t len_blks;
4866 __u64 last_blk;
4867
Eric Sandeen6873fa02008-10-07 00:46:36 -04004868 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004869 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04004870 if (last_blk >= EXT_MAX_BLOCKS)
4871 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004872 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004873
4874 /*
4875 * Walk the extent tree gathering extent information.
4876 * ext4_ext_fiemap_cb will push extents back to user.
4877 */
Eric Sandeen6873fa02008-10-07 00:46:36 -04004878 error = ext4_ext_walk_space(inode, start_blk, len_blks,
4879 ext4_ext_fiemap_cb, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004880 }
4881
4882 return error;
4883}