blob: 9ac89b67104730f630c885ebe00b9e565da605a0 [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
Alex Tomasa86c6182006-10-11 01:21:03 -0700415#ifdef EXT_DEBUG
416static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
417{
418 int k, l = path->p_depth;
419
420 ext_debug("path:");
421 for (k = 0; k <= l; k++, path++) {
422 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700423 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400424 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700425 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400426 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700427 le32_to_cpu(path->p_ext->ee_block),
Mingming553f9002009-09-18 13:34:55 -0400428 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400429 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400430 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700431 } else
432 ext_debug(" []");
433 }
434 ext_debug("\n");
435}
436
437static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
438{
439 int depth = ext_depth(inode);
440 struct ext4_extent_header *eh;
441 struct ext4_extent *ex;
442 int i;
443
444 if (!path)
445 return;
446
447 eh = path[depth].p_hdr;
448 ex = EXT_FIRST_EXTENT(eh);
449
Mingming553f9002009-09-18 13:34:55 -0400450 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
451
Alex Tomasa86c6182006-10-11 01:21:03 -0700452 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400453 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
454 ext4_ext_is_uninitialized(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400455 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700456 }
457 ext_debug("\n");
458}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400459
460static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
461 ext4_fsblk_t newblock, int level)
462{
463 int depth = ext_depth(inode);
464 struct ext4_extent *ex;
465
466 if (depth != level) {
467 struct ext4_extent_idx *idx;
468 idx = path[level].p_idx;
469 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
470 ext_debug("%d: move %d:%llu in new index %llu\n", level,
471 le32_to_cpu(idx->ei_block),
472 ext4_idx_pblock(idx),
473 newblock);
474 idx++;
475 }
476
477 return;
478 }
479
480 ex = path[depth].p_ext;
481 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
482 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
483 le32_to_cpu(ex->ee_block),
484 ext4_ext_pblock(ex),
485 ext4_ext_is_uninitialized(ex),
486 ext4_ext_get_actual_len(ex),
487 newblock);
488 ex++;
489 }
490}
491
Alex Tomasa86c6182006-10-11 01:21:03 -0700492#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400493#define ext4_ext_show_path(inode, path)
494#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400495#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700496#endif
497
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500498void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700499{
500 int depth = path->p_depth;
501 int i;
502
503 for (i = 0; i <= depth; i++, path++)
504 if (path->p_bh) {
505 brelse(path->p_bh);
506 path->p_bh = NULL;
507 }
508}
509
510/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700511 * ext4_ext_binsearch_idx:
512 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400513 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700514 */
515static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500516ext4_ext_binsearch_idx(struct inode *inode,
517 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700518{
519 struct ext4_extent_header *eh = path->p_hdr;
520 struct ext4_extent_idx *r, *l, *m;
521
Alex Tomasa86c6182006-10-11 01:21:03 -0700522
Eric Sandeenbba90742008-01-28 23:58:27 -0500523 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700524
525 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400526 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700527 while (l <= r) {
528 m = l + (r - l) / 2;
529 if (block < le32_to_cpu(m->ei_block))
530 r = m - 1;
531 else
532 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400533 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
534 m, le32_to_cpu(m->ei_block),
535 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700536 }
537
538 path->p_idx = l - 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700539 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400540 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700541
542#ifdef CHECK_BINSEARCH
543 {
544 struct ext4_extent_idx *chix, *ix;
545 int k;
546
547 chix = ix = EXT_FIRST_INDEX(eh);
548 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
549 if (k != 0 &&
550 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400551 printk(KERN_DEBUG "k=%d, ix=0x%p, "
552 "first=0x%p\n", k,
553 ix, EXT_FIRST_INDEX(eh));
554 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700555 le32_to_cpu(ix->ei_block),
556 le32_to_cpu(ix[-1].ei_block));
557 }
558 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400559 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700560 if (block < le32_to_cpu(ix->ei_block))
561 break;
562 chix = ix;
563 }
564 BUG_ON(chix != path->p_idx);
565 }
566#endif
567
568}
569
570/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700571 * ext4_ext_binsearch:
572 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400573 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700574 */
575static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500576ext4_ext_binsearch(struct inode *inode,
577 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700578{
579 struct ext4_extent_header *eh = path->p_hdr;
580 struct ext4_extent *r, *l, *m;
581
Alex Tomasa86c6182006-10-11 01:21:03 -0700582 if (eh->eh_entries == 0) {
583 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700584 * this leaf is empty:
585 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700586 */
587 return;
588 }
589
Eric Sandeenbba90742008-01-28 23:58:27 -0500590 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700591
592 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400593 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700594
595 while (l <= r) {
596 m = l + (r - l) / 2;
597 if (block < le32_to_cpu(m->ee_block))
598 r = m - 1;
599 else
600 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400601 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
602 m, le32_to_cpu(m->ee_block),
603 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700604 }
605
606 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400607 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400608 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400609 ext4_ext_pblock(path->p_ext),
Mingming553f9002009-09-18 13:34:55 -0400610 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400611 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700612
613#ifdef CHECK_BINSEARCH
614 {
615 struct ext4_extent *chex, *ex;
616 int k;
617
618 chex = ex = EXT_FIRST_EXTENT(eh);
619 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
620 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400621 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700622 if (block < le32_to_cpu(ex->ee_block))
623 break;
624 chex = ex;
625 }
626 BUG_ON(chex != path->p_ext);
627 }
628#endif
629
630}
631
632int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
633{
634 struct ext4_extent_header *eh;
635
636 eh = ext_inode_hdr(inode);
637 eh->eh_depth = 0;
638 eh->eh_entries = 0;
639 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400640 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700641 ext4_mark_inode_dirty(handle, inode);
642 ext4_ext_invalidate_cache(inode);
643 return 0;
644}
645
646struct ext4_ext_path *
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500647ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
648 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700649{
650 struct ext4_extent_header *eh;
651 struct buffer_head *bh;
652 short int depth, i, ppos = 0, alloc = 0;
653
654 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400655 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700656
657 /* account possible depth increase */
658 if (!path) {
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800659 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700660 GFP_NOFS);
661 if (!path)
662 return ERR_PTR(-ENOMEM);
663 alloc = 1;
664 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700665 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400666 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700667
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400668 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700669 /* walk through the tree */
670 while (i) {
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400671 int need_to_validate = 0;
672
Alex Tomasa86c6182006-10-11 01:21:03 -0700673 ext_debug("depth %d: num %d, max %d\n",
674 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400675
Alex Tomasa86c6182006-10-11 01:21:03 -0700676 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400677 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700678 path[ppos].p_depth = i;
679 path[ppos].p_ext = NULL;
680
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400681 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
682 if (unlikely(!bh))
Alex Tomasa86c6182006-10-11 01:21:03 -0700683 goto err;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400684 if (!bh_uptodate_or_lock(bh)) {
Jiaying Zhang0562e0b2011-03-21 21:38:05 -0400685 trace_ext4_ext_load_extent(inode, block,
686 path[ppos].p_block);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400687 if (bh_submit_read(bh) < 0) {
688 put_bh(bh);
689 goto err;
690 }
691 /* validate the extent entries */
692 need_to_validate = 1;
693 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700694 eh = ext_block_hdr(bh);
695 ppos++;
Frank Mayhar273df552010-03-02 11:46:09 -0500696 if (unlikely(ppos > depth)) {
697 put_bh(bh);
698 EXT4_ERROR_INODE(inode,
699 "ppos %d > depth %d", ppos, depth);
700 goto err;
701 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700702 path[ppos].p_bh = bh;
703 path[ppos].p_hdr = eh;
704 i--;
705
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400706 if (need_to_validate && ext4_ext_check(inode, eh, i))
Alex Tomasa86c6182006-10-11 01:21:03 -0700707 goto err;
708 }
709
710 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700711 path[ppos].p_ext = NULL;
712 path[ppos].p_idx = NULL;
713
Alex Tomasa86c6182006-10-11 01:21:03 -0700714 /* find extent */
715 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400716 /* if not an empty leaf */
717 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400718 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700719
720 ext4_ext_show_path(inode, path);
721
722 return path;
723
724err:
725 ext4_ext_drop_refs(path);
726 if (alloc)
727 kfree(path);
728 return ERR_PTR(-EIO);
729}
730
731/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700732 * ext4_ext_insert_index:
733 * insert new index [@logical;@ptr] into the block at @curp;
734 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700735 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400736static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
737 struct ext4_ext_path *curp,
738 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700739{
740 struct ext4_extent_idx *ix;
741 int len, err;
742
Avantika Mathur7e028972006-12-06 20:41:33 -0800743 err = ext4_ext_get_access(handle, inode, curp);
744 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700745 return err;
746
Frank Mayhar273df552010-03-02 11:46:09 -0500747 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
748 EXT4_ERROR_INODE(inode,
749 "logical %d == ei_block %d!",
750 logical, le32_to_cpu(curp->p_idx->ei_block));
751 return -EIO;
752 }
Robin Dongd4620312011-07-17 23:43:42 -0400753
754 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
755 >= le16_to_cpu(curp->p_hdr->eh_max))) {
756 EXT4_ERROR_INODE(inode,
757 "eh_entries %d >= eh_max %d!",
758 le16_to_cpu(curp->p_hdr->eh_entries),
759 le16_to_cpu(curp->p_hdr->eh_max));
760 return -EIO;
761 }
762
Alex Tomasa86c6182006-10-11 01:21:03 -0700763 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
764 /* insert after */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400765 ext_debug("insert new index %d after: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700766 ix = curp->p_idx + 1;
767 } else {
768 /* insert before */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400769 ext_debug("insert new index %d before: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700770 ix = curp->p_idx;
771 }
772
Eric Gouriou80e675f2011-10-27 11:52:18 -0400773 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
774 BUG_ON(len < 0);
775 if (len > 0) {
776 ext_debug("insert new index %d: "
777 "move %d indices from 0x%p to 0x%p\n",
778 logical, len, ix, ix + 1);
779 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
780 }
781
Tao Maf472e022011-10-17 10:13:46 -0400782 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
783 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
784 return -EIO;
785 }
786
Alex Tomasa86c6182006-10-11 01:21:03 -0700787 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700788 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400789 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700790
Frank Mayhar273df552010-03-02 11:46:09 -0500791 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
792 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
793 return -EIO;
794 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700795
796 err = ext4_ext_dirty(handle, inode, curp);
797 ext4_std_error(inode->i_sb, err);
798
799 return err;
800}
801
802/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700803 * ext4_ext_split:
804 * inserts new subtree into the path, using free index entry
805 * at depth @at:
806 * - allocates all needed blocks (new leaf and all intermediate index blocks)
807 * - makes decision where to split
808 * - moves remaining extents and index entries (right to the split point)
809 * into the newly allocated blocks
810 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700811 */
812static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -0400813 unsigned int flags,
814 struct ext4_ext_path *path,
815 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -0700816{
817 struct buffer_head *bh = NULL;
818 int depth = ext_depth(inode);
819 struct ext4_extent_header *neh;
820 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -0700821 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700822 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700823 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700824 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700825 int err = 0;
826
827 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700828 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700829
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700830 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700831 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -0500832 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
833 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
834 return -EIO;
835 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700836 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
837 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700838 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700839 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400840 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700841 } else {
842 border = newext->ee_block;
843 ext_debug("leaf will be added."
844 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400845 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700846 }
847
848 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700849 * If error occurs, then we break processing
850 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700851 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700852 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700853 */
854
855 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700856 * Get array to track all allocated blocks.
857 * We need this to handle errors and free blocks
858 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700859 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800860 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700861 if (!ablocks)
862 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700863
864 /* allocate all needed blocks */
865 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
866 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400867 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400868 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -0700869 if (newblock == 0)
870 goto cleanup;
871 ablocks[a] = newblock;
872 }
873
874 /* initialize new leaf */
875 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -0500876 if (unlikely(newblock == 0)) {
877 EXT4_ERROR_INODE(inode, "newblock == 0!");
878 err = -EIO;
879 goto cleanup;
880 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700881 bh = sb_getblk(inode->i_sb, newblock);
882 if (!bh) {
883 err = -EIO;
884 goto cleanup;
885 }
886 lock_buffer(bh);
887
Avantika Mathur7e028972006-12-06 20:41:33 -0800888 err = ext4_journal_get_create_access(handle, bh);
889 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700890 goto cleanup;
891
892 neh = ext_block_hdr(bh);
893 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400894 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700895 neh->eh_magic = EXT4_EXT_MAGIC;
896 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700897
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700898 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -0500899 if (unlikely(path[depth].p_hdr->eh_entries !=
900 path[depth].p_hdr->eh_max)) {
901 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
902 path[depth].p_hdr->eh_entries,
903 path[depth].p_hdr->eh_max);
904 err = -EIO;
905 goto cleanup;
906 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700907 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400908 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
909 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -0700910 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400911 struct ext4_extent *ex;
912 ex = EXT_FIRST_EXTENT(neh);
913 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400914 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700915 }
916
917 set_buffer_uptodate(bh);
918 unlock_buffer(bh);
919
Frank Mayhar03901312009-01-07 00:06:22 -0500920 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800921 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700922 goto cleanup;
923 brelse(bh);
924 bh = NULL;
925
926 /* correct old leaf */
927 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800928 err = ext4_ext_get_access(handle, inode, path + depth);
929 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700930 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400931 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800932 err = ext4_ext_dirty(handle, inode, path + depth);
933 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700934 goto cleanup;
935
936 }
937
938 /* create intermediate indexes */
939 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -0500940 if (unlikely(k < 0)) {
941 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
942 err = -EIO;
943 goto cleanup;
944 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700945 if (k)
946 ext_debug("create %d intermediate indices\n", k);
947 /* insert new index into current index block */
948 /* current depth stored in i var */
949 i = depth - 1;
950 while (k--) {
951 oldblock = newblock;
952 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -0500953 bh = sb_getblk(inode->i_sb, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700954 if (!bh) {
955 err = -EIO;
956 goto cleanup;
957 }
958 lock_buffer(bh);
959
Avantika Mathur7e028972006-12-06 20:41:33 -0800960 err = ext4_journal_get_create_access(handle, bh);
961 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700962 goto cleanup;
963
964 neh = ext_block_hdr(bh);
965 neh->eh_entries = cpu_to_le16(1);
966 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400967 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700968 neh->eh_depth = cpu_to_le16(depth - i);
969 fidx = EXT_FIRST_INDEX(neh);
970 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700971 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700972
Eric Sandeenbba90742008-01-28 23:58:27 -0500973 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
974 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700975
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400976 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -0500977 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
978 EXT_LAST_INDEX(path[i].p_hdr))) {
979 EXT4_ERROR_INODE(inode,
980 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
981 le32_to_cpu(path[i].p_ext->ee_block));
982 err = -EIO;
983 goto cleanup;
984 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400985 /* start copy indexes */
986 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
987 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
988 EXT_MAX_INDEX(path[i].p_hdr));
989 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -0700990 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400991 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -0700992 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400993 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700994 }
995 set_buffer_uptodate(bh);
996 unlock_buffer(bh);
997
Frank Mayhar03901312009-01-07 00:06:22 -0500998 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800999 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001000 goto cleanup;
1001 brelse(bh);
1002 bh = NULL;
1003
1004 /* correct old index */
1005 if (m) {
1006 err = ext4_ext_get_access(handle, inode, path + i);
1007 if (err)
1008 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001009 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001010 err = ext4_ext_dirty(handle, inode, path + i);
1011 if (err)
1012 goto cleanup;
1013 }
1014
1015 i--;
1016 }
1017
1018 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001019 err = ext4_ext_insert_index(handle, inode, path + at,
1020 le32_to_cpu(border), newblock);
1021
1022cleanup:
1023 if (bh) {
1024 if (buffer_locked(bh))
1025 unlock_buffer(bh);
1026 brelse(bh);
1027 }
1028
1029 if (err) {
1030 /* free all allocated blocks in error case */
1031 for (i = 0; i < depth; i++) {
1032 if (!ablocks[i])
1033 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001034 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001035 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001036 }
1037 }
1038 kfree(ablocks);
1039
1040 return err;
1041}
1042
1043/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001044 * ext4_ext_grow_indepth:
1045 * implements tree growing procedure:
1046 * - allocates new block
1047 * - moves top-level data (index block or leaf) into the new block
1048 * - initializes new top-level, creating index that points to the
1049 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001050 */
1051static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001052 unsigned int flags,
Allison Henderson55f020d2011-05-25 07:41:26 -04001053 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001054{
Alex Tomasa86c6182006-10-11 01:21:03 -07001055 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001056 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001057 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001058 int err = 0;
1059
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001060 newblock = ext4_ext_new_meta_block(handle, inode, NULL,
Allison Henderson55f020d2011-05-25 07:41:26 -04001061 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001062 if (newblock == 0)
1063 return err;
1064
1065 bh = sb_getblk(inode->i_sb, newblock);
1066 if (!bh) {
1067 err = -EIO;
1068 ext4_std_error(inode->i_sb, err);
1069 return err;
1070 }
1071 lock_buffer(bh);
1072
Avantika Mathur7e028972006-12-06 20:41:33 -08001073 err = ext4_journal_get_create_access(handle, bh);
1074 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001075 unlock_buffer(bh);
1076 goto out;
1077 }
1078
1079 /* move top-level index/leaf into new block */
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001080 memmove(bh->b_data, EXT4_I(inode)->i_data,
1081 sizeof(EXT4_I(inode)->i_data));
Alex Tomasa86c6182006-10-11 01:21:03 -07001082
1083 /* set size of new block */
1084 neh = ext_block_hdr(bh);
1085 /* old root could have indexes or leaves
1086 * so calculate e_max right way */
1087 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001088 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001089 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001090 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001091 neh->eh_magic = EXT4_EXT_MAGIC;
1092 set_buffer_uptodate(bh);
1093 unlock_buffer(bh);
1094
Frank Mayhar03901312009-01-07 00:06:22 -05001095 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001096 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001097 goto out;
1098
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001099 /* Update top-level index: num,max,pointer */
Alex Tomasa86c6182006-10-11 01:21:03 -07001100 neh = ext_inode_hdr(inode);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001101 neh->eh_entries = cpu_to_le16(1);
1102 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1103 if (neh->eh_depth == 0) {
1104 /* Root extent block becomes index block */
1105 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1106 EXT_FIRST_INDEX(neh)->ei_block =
1107 EXT_FIRST_EXTENT(neh)->ee_block;
1108 }
Mingming Cao2ae02102006-10-11 01:21:11 -07001109 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001110 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001111 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001112 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001113
Paul Mackerrasb4611ab2011-12-12 11:00:56 -05001114 neh->eh_depth = cpu_to_le16(le16_to_cpu(neh->eh_depth) + 1);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001115 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07001116out:
1117 brelse(bh);
1118
1119 return err;
1120}
1121
1122/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001123 * ext4_ext_create_new_leaf:
1124 * finds empty index and adds new leaf.
1125 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001126 */
1127static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001128 unsigned int flags,
1129 struct ext4_ext_path *path,
1130 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001131{
1132 struct ext4_ext_path *curp;
1133 int depth, i, err = 0;
1134
1135repeat:
1136 i = depth = ext_depth(inode);
1137
1138 /* walk up to the tree and look for free index entry */
1139 curp = path + depth;
1140 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1141 i--;
1142 curp--;
1143 }
1144
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001145 /* we use already allocated block for index block,
1146 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001147 if (EXT_HAS_FREE_INDEX(curp)) {
1148 /* if we found index with free entry, then use that
1149 * entry: create all needed subtree and add new leaf */
Allison Henderson55f020d2011-05-25 07:41:26 -04001150 err = ext4_ext_split(handle, inode, flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001151 if (err)
1152 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001153
1154 /* refill path */
1155 ext4_ext_drop_refs(path);
1156 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001157 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1158 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001159 if (IS_ERR(path))
1160 err = PTR_ERR(path);
1161 } else {
1162 /* tree is full, time to grow in depth */
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001163 err = ext4_ext_grow_indepth(handle, inode, flags, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001164 if (err)
1165 goto out;
1166
1167 /* refill path */
1168 ext4_ext_drop_refs(path);
1169 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001170 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1171 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001172 if (IS_ERR(path)) {
1173 err = PTR_ERR(path);
1174 goto out;
1175 }
1176
1177 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001178 * only first (depth 0 -> 1) produces free space;
1179 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001180 */
1181 depth = ext_depth(inode);
1182 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001183 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001184 goto repeat;
1185 }
1186 }
1187
1188out:
1189 return err;
1190}
1191
1192/*
Alex Tomas1988b512008-01-28 23:58:27 -05001193 * search the closest allocated block to the left for *logical
1194 * and returns it at @logical + it's physical address at @phys
1195 * if *logical is the smallest allocated block, the function
1196 * returns 0 at @phys
1197 * return value contains 0 (success) or error code
1198 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001199static int ext4_ext_search_left(struct inode *inode,
1200 struct ext4_ext_path *path,
1201 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001202{
1203 struct ext4_extent_idx *ix;
1204 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001205 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001206
Frank Mayhar273df552010-03-02 11:46:09 -05001207 if (unlikely(path == NULL)) {
1208 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1209 return -EIO;
1210 }
Alex Tomas1988b512008-01-28 23:58:27 -05001211 depth = path->p_depth;
1212 *phys = 0;
1213
1214 if (depth == 0 && path->p_ext == NULL)
1215 return 0;
1216
1217 /* usually extent in the path covers blocks smaller
1218 * then *logical, but it can be that extent is the
1219 * first one in the file */
1220
1221 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001222 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001223 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001224 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1225 EXT4_ERROR_INODE(inode,
1226 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1227 *logical, le32_to_cpu(ex->ee_block));
1228 return -EIO;
1229 }
Alex Tomas1988b512008-01-28 23:58:27 -05001230 while (--depth >= 0) {
1231 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001232 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1233 EXT4_ERROR_INODE(inode,
1234 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
Tao Ma6ee3b212011-10-08 16:08:34 -04001235 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001236 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
Tao Ma6ee3b212011-10-08 16:08:34 -04001237 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001238 depth);
1239 return -EIO;
1240 }
Alex Tomas1988b512008-01-28 23:58:27 -05001241 }
1242 return 0;
1243 }
1244
Frank Mayhar273df552010-03-02 11:46:09 -05001245 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1246 EXT4_ERROR_INODE(inode,
1247 "logical %d < ee_block %d + ee_len %d!",
1248 *logical, le32_to_cpu(ex->ee_block), ee_len);
1249 return -EIO;
1250 }
Alex Tomas1988b512008-01-28 23:58:27 -05001251
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001252 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001253 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001254 return 0;
1255}
1256
1257/*
1258 * search the closest allocated block to the right for *logical
1259 * and returns it at @logical + it's physical address at @phys
Tao Madf3ab172011-10-08 15:53:49 -04001260 * if *logical is the largest allocated block, the function
Alex Tomas1988b512008-01-28 23:58:27 -05001261 * returns 0 at @phys
1262 * return value contains 0 (success) or error code
1263 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001264static int ext4_ext_search_right(struct inode *inode,
1265 struct ext4_ext_path *path,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001266 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1267 struct ext4_extent **ret_ex)
Alex Tomas1988b512008-01-28 23:58:27 -05001268{
1269 struct buffer_head *bh = NULL;
1270 struct ext4_extent_header *eh;
1271 struct ext4_extent_idx *ix;
1272 struct ext4_extent *ex;
1273 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001274 int depth; /* Note, NOT eh_depth; depth from top of tree */
1275 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001276
Frank Mayhar273df552010-03-02 11:46:09 -05001277 if (unlikely(path == NULL)) {
1278 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1279 return -EIO;
1280 }
Alex Tomas1988b512008-01-28 23:58:27 -05001281 depth = path->p_depth;
1282 *phys = 0;
1283
1284 if (depth == 0 && path->p_ext == NULL)
1285 return 0;
1286
1287 /* usually extent in the path covers blocks smaller
1288 * then *logical, but it can be that extent is the
1289 * first one in the file */
1290
1291 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001292 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001293 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001294 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1295 EXT4_ERROR_INODE(inode,
1296 "first_extent(path[%d].p_hdr) != ex",
1297 depth);
1298 return -EIO;
1299 }
Alex Tomas1988b512008-01-28 23:58:27 -05001300 while (--depth >= 0) {
1301 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001302 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1303 EXT4_ERROR_INODE(inode,
1304 "ix != EXT_FIRST_INDEX *logical %d!",
1305 *logical);
1306 return -EIO;
1307 }
Alex Tomas1988b512008-01-28 23:58:27 -05001308 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001309 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001310 }
1311
Frank Mayhar273df552010-03-02 11:46:09 -05001312 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1313 EXT4_ERROR_INODE(inode,
1314 "logical %d < ee_block %d + ee_len %d!",
1315 *logical, le32_to_cpu(ex->ee_block), ee_len);
1316 return -EIO;
1317 }
Alex Tomas1988b512008-01-28 23:58:27 -05001318
1319 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1320 /* next allocated block in this leaf */
1321 ex++;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001322 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001323 }
1324
1325 /* go up and search for index to the right */
1326 while (--depth >= 0) {
1327 ix = path[depth].p_idx;
1328 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001329 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001330 }
1331
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001332 /* we've gone up to the root and found no index to the right */
1333 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001334
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001335got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001336 /* we've found index to the right, let's
1337 * follow it and find the closest allocated
1338 * block to the right */
1339 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001340 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001341 while (++depth < path->p_depth) {
1342 bh = sb_bread(inode->i_sb, block);
1343 if (bh == NULL)
1344 return -EIO;
1345 eh = ext_block_hdr(bh);
Eric Sandeen395a87b2009-03-10 18:18:47 -04001346 /* subtract from p_depth to get proper eh_depth */
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001347 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001348 put_bh(bh);
1349 return -EIO;
1350 }
1351 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001352 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001353 put_bh(bh);
1354 }
1355
1356 bh = sb_bread(inode->i_sb, block);
1357 if (bh == NULL)
1358 return -EIO;
1359 eh = ext_block_hdr(bh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001360 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001361 put_bh(bh);
1362 return -EIO;
1363 }
1364 ex = EXT_FIRST_EXTENT(eh);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001365found_extent:
Alex Tomas1988b512008-01-28 23:58:27 -05001366 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001367 *phys = ext4_ext_pblock(ex);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001368 *ret_ex = ex;
1369 if (bh)
1370 put_bh(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001371 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001372}
1373
1374/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001375 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001376 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001377 * NOTE: it considers block number from index entry as
1378 * allocated block. Thus, index entries have to be consistent
1379 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001380 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001381static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001382ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1383{
1384 int depth;
1385
1386 BUG_ON(path == NULL);
1387 depth = path->p_depth;
1388
1389 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001390 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001391
1392 while (depth >= 0) {
1393 if (depth == path->p_depth) {
1394 /* leaf */
Curt Wohlgemuth6f8ff532011-10-26 04:38:59 -04001395 if (path[depth].p_ext &&
1396 path[depth].p_ext !=
Alex Tomasa86c6182006-10-11 01:21:03 -07001397 EXT_LAST_EXTENT(path[depth].p_hdr))
1398 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1399 } else {
1400 /* index */
1401 if (path[depth].p_idx !=
1402 EXT_LAST_INDEX(path[depth].p_hdr))
1403 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1404 }
1405 depth--;
1406 }
1407
Lukas Czernerf17722f2011-06-06 00:05:17 -04001408 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001409}
1410
1411/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001412 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001413 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001414 */
Robin Dong57187892011-07-23 21:49:07 -04001415static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001416{
1417 int depth;
1418
1419 BUG_ON(path == NULL);
1420 depth = path->p_depth;
1421
1422 /* zero-tree has no leaf blocks at all */
1423 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001424 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001425
1426 /* go to index block */
1427 depth--;
1428
1429 while (depth >= 0) {
1430 if (path[depth].p_idx !=
1431 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001432 return (ext4_lblk_t)
1433 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001434 depth--;
1435 }
1436
Lukas Czernerf17722f2011-06-06 00:05:17 -04001437 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001438}
1439
1440/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001441 * ext4_ext_correct_indexes:
1442 * if leaf gets modified and modified extent is first in the leaf,
1443 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001444 * TODO: do we need to correct tree in all cases?
1445 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001446static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001447 struct ext4_ext_path *path)
1448{
1449 struct ext4_extent_header *eh;
1450 int depth = ext_depth(inode);
1451 struct ext4_extent *ex;
1452 __le32 border;
1453 int k, err = 0;
1454
1455 eh = path[depth].p_hdr;
1456 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001457
1458 if (unlikely(ex == NULL || eh == NULL)) {
1459 EXT4_ERROR_INODE(inode,
1460 "ex %p == NULL or eh %p == NULL", ex, eh);
1461 return -EIO;
1462 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001463
1464 if (depth == 0) {
1465 /* there is no tree at all */
1466 return 0;
1467 }
1468
1469 if (ex != EXT_FIRST_EXTENT(eh)) {
1470 /* we correct tree if first leaf got modified only */
1471 return 0;
1472 }
1473
1474 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001475 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001476 */
1477 k = depth - 1;
1478 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001479 err = ext4_ext_get_access(handle, inode, path + k);
1480 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001481 return err;
1482 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001483 err = ext4_ext_dirty(handle, inode, path + k);
1484 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001485 return err;
1486
1487 while (k--) {
1488 /* change all left-side indexes */
1489 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1490 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001491 err = ext4_ext_get_access(handle, inode, path + k);
1492 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001493 break;
1494 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001495 err = ext4_ext_dirty(handle, inode, path + k);
1496 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001497 break;
1498 }
1499
1500 return err;
1501}
1502
Akira Fujita748de672009-06-17 19:24:03 -04001503int
Alex Tomasa86c6182006-10-11 01:21:03 -07001504ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1505 struct ext4_extent *ex2)
1506{
Amit Arora749269f2007-07-18 09:02:56 -04001507 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001508
1509 /*
1510 * Make sure that either both extents are uninitialized, or
1511 * both are _not_.
1512 */
1513 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1514 return 0;
1515
Amit Arora749269f2007-07-18 09:02:56 -04001516 if (ext4_ext_is_uninitialized(ex1))
1517 max_len = EXT_UNINIT_MAX_LEN;
1518 else
1519 max_len = EXT_INIT_MAX_LEN;
1520
Amit Aroraa2df2a62007-07-17 21:42:41 -04001521 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1522 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1523
1524 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001525 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001526 return 0;
1527
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001528 /*
1529 * To allow future support for preallocated extents to be added
1530 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001531 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001532 */
Amit Arora749269f2007-07-18 09:02:56 -04001533 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001534 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001535#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001536 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001537 return 0;
1538#endif
1539
Theodore Ts'obf89d162010-10-27 21:30:14 -04001540 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001541 return 1;
1542 return 0;
1543}
1544
1545/*
Amit Arora56055d32007-07-17 21:42:38 -04001546 * This function tries to merge the "ex" extent to the next extent in the tree.
1547 * It always tries to merge towards right. If you want to merge towards
1548 * left, pass "ex - 1" as argument instead of "ex".
1549 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1550 * 1 if they got merged.
1551 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001552static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001553 struct ext4_ext_path *path,
1554 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001555{
1556 struct ext4_extent_header *eh;
1557 unsigned int depth, len;
1558 int merge_done = 0;
1559 int uninitialized = 0;
1560
1561 depth = ext_depth(inode);
1562 BUG_ON(path[depth].p_hdr == NULL);
1563 eh = path[depth].p_hdr;
1564
1565 while (ex < EXT_LAST_EXTENT(eh)) {
1566 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1567 break;
1568 /* merge with next extent! */
1569 if (ext4_ext_is_uninitialized(ex))
1570 uninitialized = 1;
1571 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1572 + ext4_ext_get_actual_len(ex + 1));
1573 if (uninitialized)
1574 ext4_ext_mark_uninitialized(ex);
1575
1576 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1577 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1578 * sizeof(struct ext4_extent);
1579 memmove(ex + 1, ex + 2, len);
1580 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001581 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001582 merge_done = 1;
1583 WARN_ON(eh->eh_entries == 0);
1584 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001585 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001586 }
1587
1588 return merge_done;
1589}
1590
1591/*
Yongqiang Yang197217a2011-05-03 11:45:29 -04001592 * This function tries to merge the @ex extent to neighbours in the tree.
1593 * return 1 if merge left else 0.
1594 */
1595static int ext4_ext_try_to_merge(struct inode *inode,
1596 struct ext4_ext_path *path,
1597 struct ext4_extent *ex) {
1598 struct ext4_extent_header *eh;
1599 unsigned int depth;
1600 int merge_done = 0;
1601 int ret = 0;
1602
1603 depth = ext_depth(inode);
1604 BUG_ON(path[depth].p_hdr == NULL);
1605 eh = path[depth].p_hdr;
1606
1607 if (ex > EXT_FIRST_EXTENT(eh))
1608 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1609
1610 if (!merge_done)
1611 ret = ext4_ext_try_to_merge_right(inode, path, ex);
1612
1613 return ret;
1614}
1615
1616/*
Amit Arora25d14f92007-05-24 13:04:13 -04001617 * check if a portion of the "newext" extent overlaps with an
1618 * existing extent.
1619 *
1620 * If there is an overlap discovered, it updates the length of the newext
1621 * such that there will be no overlap, and then returns 1.
1622 * If there is no overlap found, it returns 0.
1623 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001624static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1625 struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001626 struct ext4_extent *newext,
1627 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001628{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001629 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001630 unsigned int depth, len1;
1631 unsigned int ret = 0;
1632
1633 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001634 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001635 depth = ext_depth(inode);
1636 if (!path[depth].p_ext)
1637 goto out;
1638 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001639 b2 &= ~(sbi->s_cluster_ratio - 1);
Amit Arora25d14f92007-05-24 13:04:13 -04001640
1641 /*
1642 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001643 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001644 */
1645 if (b2 < b1) {
1646 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001647 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001648 goto out;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001649 b2 &= ~(sbi->s_cluster_ratio - 1);
Amit Arora25d14f92007-05-24 13:04:13 -04001650 }
1651
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001652 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001653 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001654 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001655 newext->ee_len = cpu_to_le16(len1);
1656 ret = 1;
1657 }
1658
1659 /* check for overlap */
1660 if (b1 + len1 > b2) {
1661 newext->ee_len = cpu_to_le16(b2 - b1);
1662 ret = 1;
1663 }
1664out:
1665 return ret;
1666}
1667
1668/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001669 * ext4_ext_insert_extent:
1670 * tries to merge requsted extent into the existing extent or
1671 * inserts requested extent as new one into the tree,
1672 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001673 */
1674int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1675 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04001676 struct ext4_extent *newext, int flag)
Alex Tomasa86c6182006-10-11 01:21:03 -07001677{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001678 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001679 struct ext4_extent *ex, *fex;
1680 struct ext4_extent *nearex; /* nearest extent */
1681 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001682 int depth, len, err;
1683 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001684 unsigned uninitialized = 0;
Allison Henderson55f020d2011-05-25 07:41:26 -04001685 int flags = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001686
Frank Mayhar273df552010-03-02 11:46:09 -05001687 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1688 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1689 return -EIO;
1690 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001691 depth = ext_depth(inode);
1692 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001693 if (unlikely(path[depth].p_hdr == NULL)) {
1694 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1695 return -EIO;
1696 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001697
1698 /* try to insert block into found extent and return */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001699 if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
Mingming Cao00314622009-09-28 15:49:08 -04001700 && ext4_can_extents_be_merged(inode, ex, newext)) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04001701 ext_debug("append [%d]%d block to %u:[%d]%d (from %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04001702 ext4_ext_is_uninitialized(newext),
1703 ext4_ext_get_actual_len(newext),
1704 le32_to_cpu(ex->ee_block),
1705 ext4_ext_is_uninitialized(ex),
1706 ext4_ext_get_actual_len(ex),
1707 ext4_ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001708 err = ext4_ext_get_access(handle, inode, path + depth);
1709 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001710 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001711
1712 /*
1713 * ext4_can_extents_be_merged should have checked that either
1714 * both extents are uninitialized, or both aren't. Thus we
1715 * need to check only one of them here.
1716 */
1717 if (ext4_ext_is_uninitialized(ex))
1718 uninitialized = 1;
1719 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1720 + ext4_ext_get_actual_len(newext));
1721 if (uninitialized)
1722 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001723 eh = path[depth].p_hdr;
1724 nearex = ex;
1725 goto merge;
1726 }
1727
Alex Tomasa86c6182006-10-11 01:21:03 -07001728 depth = ext_depth(inode);
1729 eh = path[depth].p_hdr;
1730 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1731 goto has_space;
1732
1733 /* probably next leaf has space for us? */
1734 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04001735 next = EXT_MAX_BLOCKS;
1736 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
Robin Dong57187892011-07-23 21:49:07 -04001737 next = ext4_ext_next_leaf_block(path);
Robin Dong598dbdf2011-07-11 18:24:01 -04001738 if (next != EXT_MAX_BLOCKS) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04001739 ext_debug("next leaf block - %u\n", next);
Alex Tomasa86c6182006-10-11 01:21:03 -07001740 BUG_ON(npath != NULL);
1741 npath = ext4_ext_find_extent(inode, next, NULL);
1742 if (IS_ERR(npath))
1743 return PTR_ERR(npath);
1744 BUG_ON(npath->p_depth != path->p_depth);
1745 eh = npath[depth].p_hdr;
1746 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001747 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001748 le16_to_cpu(eh->eh_entries));
1749 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04001750 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07001751 }
1752 ext_debug("next leaf has no free space(%d,%d)\n",
1753 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1754 }
1755
1756 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001757 * There is no free space in the found leaf.
1758 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001759 */
Allison Henderson55f020d2011-05-25 07:41:26 -04001760 if (flag & EXT4_GET_BLOCKS_PUNCH_OUT_EXT)
1761 flags = EXT4_MB_USE_ROOT_BLOCKS;
1762 err = ext4_ext_create_new_leaf(handle, inode, flags, path, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001763 if (err)
1764 goto cleanup;
1765 depth = ext_depth(inode);
1766 eh = path[depth].p_hdr;
1767
1768has_space:
1769 nearex = path[depth].p_ext;
1770
Avantika Mathur7e028972006-12-06 20:41:33 -08001771 err = ext4_ext_get_access(handle, inode, path + depth);
1772 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001773 goto cleanup;
1774
1775 if (!nearex) {
1776 /* there is no extent in this leaf, create first one */
Yongqiang Yang32de6752011-11-01 18:56:41 -04001777 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001778 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001779 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001780 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001781 ext4_ext_get_actual_len(newext));
Eric Gouriou80e675f2011-10-27 11:52:18 -04001782 nearex = EXT_FIRST_EXTENT(eh);
1783 } else {
1784 if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001785 > le32_to_cpu(nearex->ee_block)) {
Eric Gouriou80e675f2011-10-27 11:52:18 -04001786 /* Insert after */
Yongqiang Yang32de6752011-11-01 18:56:41 -04001787 ext_debug("insert %u:%llu:[%d]%d before: "
1788 "nearest %p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001789 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001790 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001791 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001792 ext4_ext_get_actual_len(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04001793 nearex);
1794 nearex++;
1795 } else {
1796 /* Insert before */
1797 BUG_ON(newext->ee_block == nearex->ee_block);
Yongqiang Yang32de6752011-11-01 18:56:41 -04001798 ext_debug("insert %u:%llu:[%d]%d after: "
1799 "nearest %p\n",
Eric Gouriou80e675f2011-10-27 11:52:18 -04001800 le32_to_cpu(newext->ee_block),
1801 ext4_ext_pblock(newext),
1802 ext4_ext_is_uninitialized(newext),
1803 ext4_ext_get_actual_len(newext),
1804 nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001805 }
Eric Gouriou80e675f2011-10-27 11:52:18 -04001806 len = EXT_LAST_EXTENT(eh) - nearex + 1;
1807 if (len > 0) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04001808 ext_debug("insert %u:%llu:[%d]%d: "
Eric Gouriou80e675f2011-10-27 11:52:18 -04001809 "move %d extents from 0x%p to 0x%p\n",
1810 le32_to_cpu(newext->ee_block),
1811 ext4_ext_pblock(newext),
1812 ext4_ext_is_uninitialized(newext),
1813 ext4_ext_get_actual_len(newext),
1814 len, nearex, nearex + 1);
1815 memmove(nearex + 1, nearex,
1816 len * sizeof(struct ext4_extent));
1817 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001818 }
1819
Marcin Slusarze8546d02008-04-17 10:38:59 -04001820 le16_add_cpu(&eh->eh_entries, 1);
Eric Gouriou80e675f2011-10-27 11:52:18 -04001821 path[depth].p_ext = nearex;
Alex Tomasa86c6182006-10-11 01:21:03 -07001822 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001823 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001824 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001825
1826merge:
1827 /* try to merge extents to the right */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001828 if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
Mingming Cao00314622009-09-28 15:49:08 -04001829 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001830
1831 /* try to merge extents to the left */
1832
1833 /* time to correct all indexes above */
1834 err = ext4_ext_correct_indexes(handle, inode, path);
1835 if (err)
1836 goto cleanup;
1837
1838 err = ext4_ext_dirty(handle, inode, path + depth);
1839
1840cleanup:
1841 if (npath) {
1842 ext4_ext_drop_refs(npath);
1843 kfree(npath);
1844 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001845 ext4_ext_invalidate_cache(inode);
1846 return err;
1847}
1848
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001849static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1850 ext4_lblk_t num, ext_prepare_callback func,
1851 void *cbdata)
Eric Sandeen6873fa02008-10-07 00:46:36 -04001852{
1853 struct ext4_ext_path *path = NULL;
1854 struct ext4_ext_cache cbex;
1855 struct ext4_extent *ex;
1856 ext4_lblk_t next, start = 0, end = 0;
1857 ext4_lblk_t last = block + num;
1858 int depth, exists, err = 0;
1859
1860 BUG_ON(func == NULL);
1861 BUG_ON(inode == NULL);
1862
Lukas Czernerf17722f2011-06-06 00:05:17 -04001863 while (block < last && block != EXT_MAX_BLOCKS) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04001864 num = last - block;
1865 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001866 down_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001867 path = ext4_ext_find_extent(inode, block, path);
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001868 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001869 if (IS_ERR(path)) {
1870 err = PTR_ERR(path);
1871 path = NULL;
1872 break;
1873 }
1874
1875 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05001876 if (unlikely(path[depth].p_hdr == NULL)) {
1877 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1878 err = -EIO;
1879 break;
1880 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04001881 ex = path[depth].p_ext;
1882 next = ext4_ext_next_allocated_block(path);
1883
1884 exists = 0;
1885 if (!ex) {
1886 /* there is no extent yet, so try to allocate
1887 * all requested space */
1888 start = block;
1889 end = block + num;
1890 } else if (le32_to_cpu(ex->ee_block) > block) {
1891 /* need to allocate space before found extent */
1892 start = block;
1893 end = le32_to_cpu(ex->ee_block);
1894 if (block + num < end)
1895 end = block + num;
1896 } else if (block >= le32_to_cpu(ex->ee_block)
1897 + ext4_ext_get_actual_len(ex)) {
1898 /* need to allocate space after found extent */
1899 start = block;
1900 end = block + num;
1901 if (end >= next)
1902 end = next;
1903 } else if (block >= le32_to_cpu(ex->ee_block)) {
1904 /*
1905 * some part of requested space is covered
1906 * by found extent
1907 */
1908 start = block;
1909 end = le32_to_cpu(ex->ee_block)
1910 + ext4_ext_get_actual_len(ex);
1911 if (block + num < end)
1912 end = block + num;
1913 exists = 1;
1914 } else {
1915 BUG();
1916 }
1917 BUG_ON(end <= start);
1918
1919 if (!exists) {
1920 cbex.ec_block = start;
1921 cbex.ec_len = end - start;
1922 cbex.ec_start = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04001923 } else {
1924 cbex.ec_block = le32_to_cpu(ex->ee_block);
1925 cbex.ec_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001926 cbex.ec_start = ext4_ext_pblock(ex);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001927 }
1928
Frank Mayhar273df552010-03-02 11:46:09 -05001929 if (unlikely(cbex.ec_len == 0)) {
1930 EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
1931 err = -EIO;
1932 break;
1933 }
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04001934 err = func(inode, next, &cbex, ex, cbdata);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001935 ext4_ext_drop_refs(path);
1936
1937 if (err < 0)
1938 break;
1939
1940 if (err == EXT_REPEAT)
1941 continue;
1942 else if (err == EXT_BREAK) {
1943 err = 0;
1944 break;
1945 }
1946
1947 if (ext_depth(inode) != depth) {
1948 /* depth was changed. we have to realloc path */
1949 kfree(path);
1950 path = NULL;
1951 }
1952
1953 block = cbex.ec_block + cbex.ec_len;
1954 }
1955
1956 if (path) {
1957 ext4_ext_drop_refs(path);
1958 kfree(path);
1959 }
1960
1961 return err;
1962}
1963
Avantika Mathur09b88252006-12-06 20:41:36 -08001964static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001965ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05001966 __u32 len, ext4_fsblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07001967{
1968 struct ext4_ext_cache *cex;
1969 BUG_ON(len == 0);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001970 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Aditya Kalid8990242011-09-09 19:18:51 -04001971 trace_ext4_ext_put_in_cache(inode, block, len, start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001972 cex = &EXT4_I(inode)->i_cached_extent;
Alex Tomasa86c6182006-10-11 01:21:03 -07001973 cex->ec_block = block;
1974 cex->ec_len = len;
1975 cex->ec_start = start;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001976 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001977}
1978
1979/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001980 * ext4_ext_put_gap_in_cache:
1981 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07001982 * and cache this gap
1983 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001984static void
Alex Tomasa86c6182006-10-11 01:21:03 -07001985ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001986 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07001987{
1988 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001989 unsigned long len;
1990 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001991 struct ext4_extent *ex;
1992
1993 ex = path[depth].p_ext;
1994 if (ex == NULL) {
1995 /* there is no extent yet, so gap is [0;-] */
1996 lblock = 0;
Lukas Czernerf17722f2011-06-06 00:05:17 -04001997 len = EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001998 ext_debug("cache gap(whole file):");
1999 } else if (block < le32_to_cpu(ex->ee_block)) {
2000 lblock = block;
2001 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05002002 ext_debug("cache gap(before): %u [%u:%u]",
2003 block,
2004 le32_to_cpu(ex->ee_block),
2005 ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002006 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002007 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002008 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002009 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002010 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002011
2012 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05002013 ext_debug("cache gap(after): [%u:%u] %u",
2014 le32_to_cpu(ex->ee_block),
2015 ext4_ext_get_actual_len(ex),
2016 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002017 BUG_ON(next == lblock);
2018 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07002019 } else {
2020 lblock = len = 0;
2021 BUG();
2022 }
2023
Eric Sandeenbba90742008-01-28 23:58:27 -05002024 ext_debug(" -> %u:%lu\n", lblock, len);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002025 ext4_ext_put_in_cache(inode, lblock, len, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002026}
2027
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002028/*
Robin Dongb7ca1e82011-07-23 21:53:25 -04002029 * ext4_ext_check_cache()
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002030 * Checks to see if the given block is in the cache.
2031 * If it is, the cached extent is stored in the given
2032 * cache extent pointer. If the cached extent is a hole,
2033 * this routine should be used instead of
2034 * ext4_ext_in_cache if the calling function needs to
2035 * know the size of the hole.
2036 *
2037 * @inode: The files inode
2038 * @block: The block to look for in the cache
2039 * @ex: Pointer where the cached extent will be stored
2040 * if it contains block
2041 *
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002042 * Return 0 if cache is invalid; 1 if the cache is valid
2043 */
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002044static int ext4_ext_check_cache(struct inode *inode, ext4_lblk_t block,
2045 struct ext4_ext_cache *ex){
Alex Tomasa86c6182006-10-11 01:21:03 -07002046 struct ext4_ext_cache *cex;
Vivek Haldar77f41352011-05-22 21:24:16 -04002047 struct ext4_sb_info *sbi;
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002048 int ret = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002049
Theodore Ts'o60e66792010-05-17 07:00:00 -04002050 /*
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002051 * We borrow i_block_reservation_lock to protect i_cached_extent
2052 */
2053 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07002054 cex = &EXT4_I(inode)->i_cached_extent;
Vivek Haldar77f41352011-05-22 21:24:16 -04002055 sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002056
2057 /* has cache valid data? */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002058 if (cex->ec_len == 0)
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002059 goto errout;
Alex Tomasa86c6182006-10-11 01:21:03 -07002060
Akinobu Mita731eb1a2010-03-03 23:55:01 -05002061 if (in_range(block, cex->ec_block, cex->ec_len)) {
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002062 memcpy(ex, cex, sizeof(struct ext4_ext_cache));
Eric Sandeenbba90742008-01-28 23:58:27 -05002063 ext_debug("%u cached by %u:%u:%llu\n",
2064 block,
2065 cex->ec_block, cex->ec_len, cex->ec_start);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002066 ret = 1;
Alex Tomasa86c6182006-10-11 01:21:03 -07002067 }
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002068errout:
Vivek Haldar77f41352011-05-22 21:24:16 -04002069 if (!ret)
2070 sbi->extent_cache_misses++;
2071 else
2072 sbi->extent_cache_hits++;
Aditya Kalid8990242011-09-09 19:18:51 -04002073 trace_ext4_ext_in_cache(inode, block, ret);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002074 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
2075 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07002076}
2077
2078/*
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002079 * ext4_ext_in_cache()
2080 * Checks to see if the given block is in the cache.
2081 * If it is, the cached extent is stored in the given
2082 * extent pointer.
2083 *
2084 * @inode: The files inode
2085 * @block: The block to look for in the cache
2086 * @ex: Pointer where the cached extent will be stored
2087 * if it contains block
2088 *
2089 * Return 0 if cache is invalid; 1 if the cache is valid
2090 */
2091static int
2092ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
2093 struct ext4_extent *ex)
2094{
2095 struct ext4_ext_cache cex;
2096 int ret = 0;
2097
2098 if (ext4_ext_check_cache(inode, block, &cex)) {
2099 ex->ee_block = cpu_to_le32(cex.ec_block);
2100 ext4_ext_store_pblock(ex, cex.ec_start);
2101 ex->ee_len = cpu_to_le16(cex.ec_len);
2102 ret = 1;
2103 }
2104
2105 return ret;
2106}
2107
2108
2109/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002110 * ext4_ext_rm_idx:
2111 * removes index from the index block.
Alex Tomasa86c6182006-10-11 01:21:03 -07002112 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002113static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07002114 struct ext4_ext_path *path)
2115{
Alex Tomasa86c6182006-10-11 01:21:03 -07002116 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002117 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002118
2119 /* free index block */
2120 path--;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002121 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002122 if (unlikely(path->p_hdr->eh_entries == 0)) {
2123 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2124 return -EIO;
2125 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002126 err = ext4_ext_get_access(handle, inode, path);
2127 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002128 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002129
2130 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2131 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2132 len *= sizeof(struct ext4_extent_idx);
2133 memmove(path->p_idx, path->p_idx + 1, len);
2134 }
2135
Marcin Slusarze8546d02008-04-17 10:38:59 -04002136 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002137 err = ext4_ext_dirty(handle, inode, path);
2138 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002139 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002140 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Aditya Kalid8990242011-09-09 19:18:51 -04002141 trace_ext4_ext_rm_idx(inode, leaf);
2142
Peter Huewe7dc57612011-02-21 21:01:42 -05002143 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002144 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Alex Tomasa86c6182006-10-11 01:21:03 -07002145 return err;
2146}
2147
2148/*
Mingming Caoee12b632008-08-19 22:16:05 -04002149 * ext4_ext_calc_credits_for_single_extent:
2150 * This routine returns max. credits that needed to insert an extent
2151 * to the extent tree.
2152 * When pass the actual path, the caller should calculate credits
2153 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002154 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002155int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002156 struct ext4_ext_path *path)
2157{
Alex Tomasa86c6182006-10-11 01:21:03 -07002158 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002159 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002160 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002161
Alex Tomasa86c6182006-10-11 01:21:03 -07002162 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002163 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002164 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2165
2166 /*
2167 * There are some space in the leaf tree, no
2168 * need to account for leaf block credit
2169 *
2170 * bitmaps and block group descriptor blocks
Tao Madf3ab172011-10-08 15:53:49 -04002171 * and other metadata blocks still need to be
Mingming Caoee12b632008-08-19 22:16:05 -04002172 * accounted.
2173 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002174 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002175 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002176 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002177 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002178 }
2179
Mingming Cao525f4ed2008-08-19 22:15:58 -04002180 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002181}
Alex Tomasa86c6182006-10-11 01:21:03 -07002182
Mingming Caoee12b632008-08-19 22:16:05 -04002183/*
2184 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2185 *
2186 * if nrblocks are fit in a single extent (chunk flag is 1), then
2187 * in the worse case, each tree level index/leaf need to be changed
2188 * if the tree split due to insert a new extent, then the old tree
2189 * index/leaf need to be updated too
2190 *
2191 * If the nrblocks are discontiguous, they could cause
2192 * the whole tree split more than once, but this is really rare.
2193 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002194int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoee12b632008-08-19 22:16:05 -04002195{
2196 int index;
2197 int depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002198
Mingming Caoee12b632008-08-19 22:16:05 -04002199 if (chunk)
2200 index = depth * 2;
2201 else
2202 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002203
Mingming Caoee12b632008-08-19 22:16:05 -04002204 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002205}
2206
2207static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002208 struct ext4_extent *ex,
2209 ext4_fsblk_t *partial_cluster,
2210 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002211{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002212 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002213 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002214 ext4_fsblk_t pblk;
Theodore Ts'oe6362602009-11-23 07:17:05 -05002215 int flags = EXT4_FREE_BLOCKS_FORGET;
Alex Tomasa86c6182006-10-11 01:21:03 -07002216
Alex Tomasc9de5602008-01-29 00:19:52 -05002217 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
Theodore Ts'oe6362602009-11-23 07:17:05 -05002218 flags |= EXT4_FREE_BLOCKS_METADATA;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002219 /*
2220 * For bigalloc file systems, we never free a partial cluster
2221 * at the beginning of the extent. Instead, we make a note
2222 * that we tried freeing the cluster, and check to see if we
2223 * need to free it on a subsequent call to ext4_remove_blocks,
2224 * or at the end of the ext4_truncate() operation.
2225 */
2226 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
2227
Aditya Kalid8990242011-09-09 19:18:51 -04002228 trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002229 /*
2230 * If we have a partial cluster, and it's different from the
2231 * cluster of the last block, we need to explicitly free the
2232 * partial cluster here.
2233 */
2234 pblk = ext4_ext_pblock(ex) + ee_len - 1;
2235 if (*partial_cluster && (EXT4_B2C(sbi, pblk) != *partial_cluster)) {
2236 ext4_free_blocks(handle, inode, NULL,
2237 EXT4_C2B(sbi, *partial_cluster),
2238 sbi->s_cluster_ratio, flags);
2239 *partial_cluster = 0;
2240 }
2241
Alex Tomasa86c6182006-10-11 01:21:03 -07002242#ifdef EXTENTS_STATS
2243 {
2244 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002245 spin_lock(&sbi->s_ext_stats_lock);
2246 sbi->s_ext_blocks += ee_len;
2247 sbi->s_ext_extents++;
2248 if (ee_len < sbi->s_ext_min)
2249 sbi->s_ext_min = ee_len;
2250 if (ee_len > sbi->s_ext_max)
2251 sbi->s_ext_max = ee_len;
2252 if (ext_depth(inode) > sbi->s_depth_max)
2253 sbi->s_depth_max = ext_depth(inode);
2254 spin_unlock(&sbi->s_ext_stats_lock);
2255 }
2256#endif
2257 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002258 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002259 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002260 ext4_lblk_t num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002261
Amit Aroraa2df2a62007-07-17 21:42:41 -04002262 num = le32_to_cpu(ex->ee_block) + ee_len - from;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002263 pblk = ext4_ext_pblock(ex) + ee_len - num;
2264 ext_debug("free last %u blocks starting %llu\n", num, pblk);
2265 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
2266 /*
2267 * If the block range to be freed didn't start at the
2268 * beginning of a cluster, and we removed the entire
2269 * extent, save the partial cluster here, since we
2270 * might need to delete if we determine that the
2271 * truncate operation has removed all of the blocks in
2272 * the cluster.
2273 */
2274 if (pblk & (sbi->s_cluster_ratio - 1) &&
2275 (ee_len == num))
2276 *partial_cluster = EXT4_B2C(sbi, pblk);
2277 else
2278 *partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002279 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002280 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002281 /* head removal */
2282 ext4_lblk_t num;
2283 ext4_fsblk_t start;
2284
2285 num = to - from;
2286 start = ext4_ext_pblock(ex);
2287
2288 ext_debug("free first %u blocks starting %llu\n", num, start);
H Hartley Sweetenee90d572011-10-18 11:01:51 -04002289 ext4_free_blocks(handle, inode, NULL, start, num, flags);
Allison Hendersond583fb82011-05-25 07:41:43 -04002290
Alex Tomasa86c6182006-10-11 01:21:03 -07002291 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002292 printk(KERN_INFO "strange request: removal(2) "
2293 "%u-%u from %u:%u\n",
2294 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002295 }
2296 return 0;
2297}
2298
Allison Hendersond583fb82011-05-25 07:41:43 -04002299
2300/*
2301 * ext4_ext_rm_leaf() Removes the extents associated with the
2302 * blocks appearing between "start" and "end", and splits the extents
2303 * if "start" and "end" appear in the same extent
2304 *
2305 * @handle: The journal handle
2306 * @inode: The files inode
2307 * @path: The path to the leaf
2308 * @start: The first block to remove
2309 * @end: The last block to remove
2310 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002311static int
2312ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002313 struct ext4_ext_path *path, ext4_fsblk_t *partial_cluster,
2314 ext4_lblk_t start, ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002315{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002316 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002317 int err = 0, correct_index = 0;
2318 int depth = ext_depth(inode), credits;
2319 struct ext4_extent_header *eh;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002320 ext4_lblk_t a, b;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002321 unsigned num;
2322 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002323 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002324 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002325 struct ext4_extent *ex;
2326
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002327 /* the header must be checked already in ext4_ext_remove_space() */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002328 ext_debug("truncate since %u in leaf to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002329 if (!path[depth].p_hdr)
2330 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2331 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002332 if (unlikely(path[depth].p_hdr == NULL)) {
2333 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2334 return -EIO;
2335 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002336 /* find where to start removing */
2337 ex = EXT_LAST_EXTENT(eh);
2338
2339 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002340 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002341
Aditya Kalid8990242011-09-09 19:18:51 -04002342 trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster);
2343
Alex Tomasa86c6182006-10-11 01:21:03 -07002344 while (ex >= EXT_FIRST_EXTENT(eh) &&
2345 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002346
2347 if (ext4_ext_is_uninitialized(ex))
2348 uninitialized = 1;
2349 else
2350 uninitialized = 0;
2351
Mingming553f9002009-09-18 13:34:55 -04002352 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2353 uninitialized, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002354 path[depth].p_ext = ex;
2355
2356 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002357 b = ex_ee_block+ex_ee_len - 1 < end ?
2358 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002359
2360 ext_debug(" border %u:%u\n", a, b);
2361
Allison Hendersond583fb82011-05-25 07:41:43 -04002362 /* If this extent is beyond the end of the hole, skip it */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002363 if (end < ex_ee_block) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002364 ex--;
2365 ex_ee_block = le32_to_cpu(ex->ee_block);
2366 ex_ee_len = ext4_ext_get_actual_len(ex);
2367 continue;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002368 } else if (b != ex_ee_block + ex_ee_len - 1) {
2369 EXT4_ERROR_INODE(inode," bad truncate %u:%u\n",
2370 start, end);
2371 err = -EIO;
2372 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002373 } else if (a != ex_ee_block) {
2374 /* remove tail of the extent */
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002375 num = a - ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002376 } else {
2377 /* remove whole extent: excellent! */
Alex Tomasa86c6182006-10-11 01:21:03 -07002378 num = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002379 }
Theodore Ts'o34071da2008-08-01 21:59:19 -04002380 /*
2381 * 3 for leaf, sb, and inode plus 2 (bmap and group
2382 * descriptor) for each block group; assume two block
2383 * groups plus ex_ee_len/blocks_per_block_group for
2384 * the worst case
2385 */
2386 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002387 if (ex == EXT_FIRST_EXTENT(eh)) {
2388 correct_index = 1;
2389 credits += (ext_depth(inode)) + 1;
2390 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002391 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002392
Jan Kara487caee2009-08-17 22:17:20 -04002393 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
Shen Feng9102e4f2008-07-11 19:27:31 -04002394 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002395 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002396
2397 err = ext4_ext_get_access(handle, inode, path + depth);
2398 if (err)
2399 goto out;
2400
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002401 err = ext4_remove_blocks(handle, inode, ex, partial_cluster,
2402 a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002403 if (err)
2404 goto out;
2405
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002406 if (num == 0)
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002407 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002408 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002409
Alex Tomasa86c6182006-10-11 01:21:03 -07002410 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002411 /*
2412 * Do not mark uninitialized if all the blocks in the
2413 * extent have been removed.
2414 */
2415 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002416 ext4_ext_mark_uninitialized(ex);
Allison Hendersond583fb82011-05-25 07:41:43 -04002417 /*
2418 * If the extent was completely released,
2419 * we need to remove it from the leaf
2420 */
2421 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002422 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002423 /*
2424 * For hole punching, we need to scoot all the
2425 * extents up when an extent is removed so that
2426 * we dont have blank extents in the middle
2427 */
2428 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2429 sizeof(struct ext4_extent));
2430
2431 /* Now get rid of the one at the end */
2432 memset(EXT_LAST_EXTENT(eh), 0,
2433 sizeof(struct ext4_extent));
2434 }
2435 le16_add_cpu(&eh->eh_entries, -1);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002436 } else
2437 *partial_cluster = 0;
Allison Hendersond583fb82011-05-25 07:41:43 -04002438
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002439 err = ext4_ext_dirty(handle, inode, path + depth);
2440 if (err)
2441 goto out;
2442
Yongqiang Yangbf52c6f2011-11-01 18:59:26 -04002443 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002444 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002445 ex--;
2446 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002447 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002448 }
2449
2450 if (correct_index && eh->eh_entries)
2451 err = ext4_ext_correct_indexes(handle, inode, path);
2452
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002453 /*
2454 * If there is still a entry in the leaf node, check to see if
2455 * it references the partial cluster. This is the only place
2456 * where it could; if it doesn't, we can free the cluster.
2457 */
2458 if (*partial_cluster && ex >= EXT_FIRST_EXTENT(eh) &&
2459 (EXT4_B2C(sbi, ext4_ext_pblock(ex) + ex_ee_len - 1) !=
2460 *partial_cluster)) {
2461 int flags = EXT4_FREE_BLOCKS_FORGET;
2462
2463 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2464 flags |= EXT4_FREE_BLOCKS_METADATA;
2465
2466 ext4_free_blocks(handle, inode, NULL,
2467 EXT4_C2B(sbi, *partial_cluster),
2468 sbi->s_cluster_ratio, flags);
2469 *partial_cluster = 0;
2470 }
2471
Alex Tomasa86c6182006-10-11 01:21:03 -07002472 /* if this leaf is free, then we should
2473 * remove it from index block above */
2474 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2475 err = ext4_ext_rm_idx(handle, inode, path + depth);
2476
2477out:
2478 return err;
2479}
2480
2481/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002482 * ext4_ext_more_to_rm:
2483 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002484 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002485static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002486ext4_ext_more_to_rm(struct ext4_ext_path *path)
2487{
2488 BUG_ON(path->p_idx == NULL);
2489
2490 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2491 return 0;
2492
2493 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002494 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002495 * so we have to consider current index for truncation
2496 */
2497 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2498 return 0;
2499 return 1;
2500}
2501
Lukas Czerner5f95d212012-03-19 23:03:19 -04002502static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2503 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002504{
2505 struct super_block *sb = inode->i_sb;
2506 int depth = ext_depth(inode);
2507 struct ext4_ext_path *path;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002508 ext4_fsblk_t partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002509 handle_t *handle;
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002510 int i, err;
Alex Tomasa86c6182006-10-11 01:21:03 -07002511
Lukas Czerner5f95d212012-03-19 23:03:19 -04002512 ext_debug("truncate since %u to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002513
2514 /* probably first extent we're gonna free will be last in block */
2515 handle = ext4_journal_start(inode, depth + 1);
2516 if (IS_ERR(handle))
2517 return PTR_ERR(handle);
2518
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002519again:
Alex Tomasa86c6182006-10-11 01:21:03 -07002520 ext4_ext_invalidate_cache(inode);
2521
Aditya Kalid8990242011-09-09 19:18:51 -04002522 trace_ext4_ext_remove_space(inode, start, depth);
2523
Alex Tomasa86c6182006-10-11 01:21:03 -07002524 /*
Lukas Czerner5f95d212012-03-19 23:03:19 -04002525 * Check if we are removing extents inside the extent tree. If that
2526 * is the case, we are going to punch a hole inside the extent tree
2527 * so we have to check whether we need to split the extent covering
2528 * the last block to remove so we can easily remove the part of it
2529 * in ext4_ext_rm_leaf().
2530 */
2531 if (end < EXT_MAX_BLOCKS - 1) {
2532 struct ext4_extent *ex;
2533 ext4_lblk_t ee_block;
2534
2535 /* find extent for this block */
2536 path = ext4_ext_find_extent(inode, end, NULL);
2537 if (IS_ERR(path)) {
2538 ext4_journal_stop(handle);
2539 return PTR_ERR(path);
2540 }
2541 depth = ext_depth(inode);
2542 ex = path[depth].p_ext;
2543 if (!ex)
2544 goto cont;
2545
2546 ee_block = le32_to_cpu(ex->ee_block);
2547
2548 /*
2549 * See if the last block is inside the extent, if so split
2550 * the extent at 'end' block so we can easily remove the
2551 * tail of the first part of the split extent in
2552 * ext4_ext_rm_leaf().
2553 */
2554 if (end >= ee_block &&
2555 end < ee_block + ext4_ext_get_actual_len(ex) - 1) {
2556 int split_flag = 0;
2557
2558 if (ext4_ext_is_uninitialized(ex))
2559 split_flag = EXT4_EXT_MARK_UNINIT1 |
2560 EXT4_EXT_MARK_UNINIT2;
2561
2562 /*
2563 * Split the extent in two so that 'end' is the last
2564 * block in the first new extent
2565 */
2566 err = ext4_split_extent_at(handle, inode, path,
2567 end + 1, split_flag,
2568 EXT4_GET_BLOCKS_PRE_IO |
2569 EXT4_GET_BLOCKS_PUNCH_OUT_EXT);
2570
2571 if (err < 0)
2572 goto out;
2573 }
2574 ext4_ext_drop_refs(path);
2575 kfree(path);
2576 }
2577cont:
2578
2579 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002580 * We start scanning from right side, freeing all the blocks
2581 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002582 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002583 depth = ext_depth(inode);
Josef Bacik216553c2008-04-29 22:02:02 -04002584 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07002585 if (path == NULL) {
2586 ext4_journal_stop(handle);
2587 return -ENOMEM;
2588 }
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002589 path[0].p_depth = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -07002590 path[0].p_hdr = ext_inode_hdr(inode);
Lukas Czerner5f95d212012-03-19 23:03:19 -04002591
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002592 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002593 err = -EIO;
2594 goto out;
2595 }
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002596 i = err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002597
2598 while (i >= 0 && err == 0) {
2599 if (i == depth) {
2600 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04002601 err = ext4_ext_rm_leaf(handle, inode, path,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002602 &partial_cluster, start,
Lukas Czerner5f95d212012-03-19 23:03:19 -04002603 end);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002604 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002605 brelse(path[i].p_bh);
2606 path[i].p_bh = NULL;
2607 i--;
2608 continue;
2609 }
2610
2611 /* this is index block */
2612 if (!path[i].p_hdr) {
2613 ext_debug("initialize header\n");
2614 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002615 }
2616
Alex Tomasa86c6182006-10-11 01:21:03 -07002617 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002618 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002619 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2620 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2621 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2622 path[i].p_hdr,
2623 le16_to_cpu(path[i].p_hdr->eh_entries));
2624 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002625 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002626 path[i].p_idx--;
2627 }
2628
2629 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2630 i, EXT_FIRST_INDEX(path[i].p_hdr),
2631 path[i].p_idx);
2632 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002633 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002634 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002635 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04002636 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002637 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'obf89d162010-10-27 21:30:14 -04002638 bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002639 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002640 /* should we reset i_size? */
2641 err = -EIO;
2642 break;
2643 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002644 if (WARN_ON(i + 1 > depth)) {
2645 err = -EIO;
2646 break;
2647 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002648 if (ext4_ext_check(inode, ext_block_hdr(bh),
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002649 depth - i - 1)) {
2650 err = -EIO;
2651 break;
2652 }
2653 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002654
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002655 /* save actual number of indexes since this
2656 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002657 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2658 i++;
2659 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002660 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002661 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002662 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002663 * handle must be already prepared by the
2664 * truncatei_leaf() */
2665 err = ext4_ext_rm_idx(handle, inode, path + i);
2666 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002667 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002668 brelse(path[i].p_bh);
2669 path[i].p_bh = NULL;
2670 i--;
2671 ext_debug("return to level %d\n", i);
2672 }
2673 }
2674
Aditya Kalid8990242011-09-09 19:18:51 -04002675 trace_ext4_ext_remove_space_done(inode, start, depth, partial_cluster,
2676 path->p_hdr->eh_entries);
2677
Aditya Kali7b415bf2011-09-09 19:04:51 -04002678 /* If we still have something in the partial cluster and we have removed
2679 * even the first extent, then we should free the blocks in the partial
2680 * cluster as well. */
2681 if (partial_cluster && path->p_hdr->eh_entries == 0) {
2682 int flags = EXT4_FREE_BLOCKS_FORGET;
2683
2684 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2685 flags |= EXT4_FREE_BLOCKS_METADATA;
2686
2687 ext4_free_blocks(handle, inode, NULL,
2688 EXT4_C2B(EXT4_SB(sb), partial_cluster),
2689 EXT4_SB(sb)->s_cluster_ratio, flags);
2690 partial_cluster = 0;
2691 }
2692
Alex Tomasa86c6182006-10-11 01:21:03 -07002693 /* TODO: flexible tree reduction should be here */
2694 if (path->p_hdr->eh_entries == 0) {
2695 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002696 * truncate to zero freed all the tree,
2697 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002698 */
2699 err = ext4_ext_get_access(handle, inode, path);
2700 if (err == 0) {
2701 ext_inode_hdr(inode)->eh_depth = 0;
2702 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04002703 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07002704 err = ext4_ext_dirty(handle, inode, path);
2705 }
2706 }
2707out:
Alex Tomasa86c6182006-10-11 01:21:03 -07002708 ext4_ext_drop_refs(path);
2709 kfree(path);
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002710 if (err == -EAGAIN)
2711 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07002712 ext4_journal_stop(handle);
2713
2714 return err;
2715}
2716
2717/*
2718 * called at mount time
2719 */
2720void ext4_ext_init(struct super_block *sb)
2721{
2722 /*
2723 * possible initialization would be here
2724 */
2725
Theodore Ts'o83982b62009-01-06 14:53:16 -05002726 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04002727#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002728 printk(KERN_INFO "EXT4-fs: file extents enabled");
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002729#ifdef AGGRESSIVE_TEST
2730 printk(", aggressive tests");
Alex Tomasa86c6182006-10-11 01:21:03 -07002731#endif
2732#ifdef CHECK_BINSEARCH
2733 printk(", check binsearch");
2734#endif
2735#ifdef EXTENTS_STATS
2736 printk(", stats");
2737#endif
2738 printk("\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04002739#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07002740#ifdef EXTENTS_STATS
2741 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2742 EXT4_SB(sb)->s_ext_min = 1 << 30;
2743 EXT4_SB(sb)->s_ext_max = 0;
2744#endif
2745 }
2746}
2747
2748/*
2749 * called at umount time
2750 */
2751void ext4_ext_release(struct super_block *sb)
2752{
Theodore Ts'o83982b62009-01-06 14:53:16 -05002753 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
Alex Tomasa86c6182006-10-11 01:21:03 -07002754 return;
2755
2756#ifdef EXTENTS_STATS
2757 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2758 struct ext4_sb_info *sbi = EXT4_SB(sb);
2759 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2760 sbi->s_ext_blocks, sbi->s_ext_extents,
2761 sbi->s_ext_blocks / sbi->s_ext_extents);
2762 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2763 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2764 }
2765#endif
2766}
2767
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002768/* FIXME!! we need to try to merge to left or right after zero-out */
2769static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2770{
Lukas Czerner24075182010-10-27 21:30:06 -04002771 ext4_fsblk_t ee_pblock;
2772 unsigned int ee_len;
Jing Zhangb7203032010-05-12 00:00:00 -04002773 int ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002774
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002775 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04002776 ee_pblock = ext4_ext_pblock(ex);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002777
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04002778 ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
Lukas Czerner24075182010-10-27 21:30:06 -04002779 if (ret > 0)
2780 ret = 0;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002781
Lukas Czerner24075182010-10-27 21:30:06 -04002782 return ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002783}
2784
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002785/*
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002786 * ext4_split_extent_at() splits an extent at given block.
2787 *
2788 * @handle: the journal handle
2789 * @inode: the file inode
2790 * @path: the path to the extent
2791 * @split: the logical block where the extent is splitted.
2792 * @split_flags: indicates if the extent could be zeroout if split fails, and
2793 * the states(init or uninit) of new extents.
2794 * @flags: flags used to insert new extent to extent tree.
2795 *
2796 *
2797 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
2798 * of which are deterimined by split_flag.
2799 *
2800 * There are two cases:
2801 * a> the extent are splitted into two extent.
2802 * b> split is not needed, and just mark the extent.
2803 *
2804 * return 0 on success.
2805 */
2806static int ext4_split_extent_at(handle_t *handle,
2807 struct inode *inode,
2808 struct ext4_ext_path *path,
2809 ext4_lblk_t split,
2810 int split_flag,
2811 int flags)
2812{
2813 ext4_fsblk_t newblock;
2814 ext4_lblk_t ee_block;
2815 struct ext4_extent *ex, newex, orig_ex;
2816 struct ext4_extent *ex2 = NULL;
2817 unsigned int ee_len, depth;
2818 int err = 0;
2819
2820 ext_debug("ext4_split_extents_at: inode %lu, logical"
2821 "block %llu\n", inode->i_ino, (unsigned long long)split);
2822
2823 ext4_ext_show_leaf(inode, path);
2824
2825 depth = ext_depth(inode);
2826 ex = path[depth].p_ext;
2827 ee_block = le32_to_cpu(ex->ee_block);
2828 ee_len = ext4_ext_get_actual_len(ex);
2829 newblock = split - ee_block + ext4_ext_pblock(ex);
2830
2831 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
2832
2833 err = ext4_ext_get_access(handle, inode, path + depth);
2834 if (err)
2835 goto out;
2836
2837 if (split == ee_block) {
2838 /*
2839 * case b: block @split is the block that the extent begins with
2840 * then we just change the state of the extent, and splitting
2841 * is not needed.
2842 */
2843 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2844 ext4_ext_mark_uninitialized(ex);
2845 else
2846 ext4_ext_mark_initialized(ex);
2847
2848 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
2849 ext4_ext_try_to_merge(inode, path, ex);
2850
2851 err = ext4_ext_dirty(handle, inode, path + depth);
2852 goto out;
2853 }
2854
2855 /* case a */
2856 memcpy(&orig_ex, ex, sizeof(orig_ex));
2857 ex->ee_len = cpu_to_le16(split - ee_block);
2858 if (split_flag & EXT4_EXT_MARK_UNINIT1)
2859 ext4_ext_mark_uninitialized(ex);
2860
2861 /*
2862 * path may lead to new leaf, not to original leaf any more
2863 * after ext4_ext_insert_extent() returns,
2864 */
2865 err = ext4_ext_dirty(handle, inode, path + depth);
2866 if (err)
2867 goto fix_extent_len;
2868
2869 ex2 = &newex;
2870 ex2->ee_block = cpu_to_le32(split);
2871 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
2872 ext4_ext_store_pblock(ex2, newblock);
2873 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2874 ext4_ext_mark_uninitialized(ex2);
2875
2876 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
2877 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2878 err = ext4_ext_zeroout(inode, &orig_ex);
2879 if (err)
2880 goto fix_extent_len;
2881 /* update the extent length and mark as initialized */
2882 ex->ee_len = cpu_to_le32(ee_len);
2883 ext4_ext_try_to_merge(inode, path, ex);
2884 err = ext4_ext_dirty(handle, inode, path + depth);
2885 goto out;
2886 } else if (err)
2887 goto fix_extent_len;
2888
2889out:
2890 ext4_ext_show_leaf(inode, path);
2891 return err;
2892
2893fix_extent_len:
2894 ex->ee_len = orig_ex.ee_len;
2895 ext4_ext_dirty(handle, inode, path + depth);
2896 return err;
2897}
2898
2899/*
2900 * ext4_split_extents() splits an extent and mark extent which is covered
2901 * by @map as split_flags indicates
2902 *
2903 * It may result in splitting the extent into multiple extents (upto three)
2904 * There are three possibilities:
2905 * a> There is no split required
2906 * b> Splits in two extents: Split is happening at either end of the extent
2907 * c> Splits in three extents: Somone is splitting in middle of the extent
2908 *
2909 */
2910static int ext4_split_extent(handle_t *handle,
2911 struct inode *inode,
2912 struct ext4_ext_path *path,
2913 struct ext4_map_blocks *map,
2914 int split_flag,
2915 int flags)
2916{
2917 ext4_lblk_t ee_block;
2918 struct ext4_extent *ex;
2919 unsigned int ee_len, depth;
2920 int err = 0;
2921 int uninitialized;
2922 int split_flag1, flags1;
2923
2924 depth = ext_depth(inode);
2925 ex = path[depth].p_ext;
2926 ee_block = le32_to_cpu(ex->ee_block);
2927 ee_len = ext4_ext_get_actual_len(ex);
2928 uninitialized = ext4_ext_is_uninitialized(ex);
2929
2930 if (map->m_lblk + map->m_len < ee_block + ee_len) {
2931 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2932 EXT4_EXT_MAY_ZEROOUT : 0;
2933 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
2934 if (uninitialized)
2935 split_flag1 |= EXT4_EXT_MARK_UNINIT1 |
2936 EXT4_EXT_MARK_UNINIT2;
2937 err = ext4_split_extent_at(handle, inode, path,
2938 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04002939 if (err)
2940 goto out;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002941 }
2942
2943 ext4_ext_drop_refs(path);
2944 path = ext4_ext_find_extent(inode, map->m_lblk, path);
2945 if (IS_ERR(path))
2946 return PTR_ERR(path);
2947
2948 if (map->m_lblk >= ee_block) {
2949 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2950 EXT4_EXT_MAY_ZEROOUT : 0;
2951 if (uninitialized)
2952 split_flag1 |= EXT4_EXT_MARK_UNINIT1;
2953 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2954 split_flag1 |= EXT4_EXT_MARK_UNINIT2;
2955 err = ext4_split_extent_at(handle, inode, path,
2956 map->m_lblk, split_flag1, flags);
2957 if (err)
2958 goto out;
2959 }
2960
2961 ext4_ext_show_leaf(inode, path);
2962out:
2963 return err ? err : map->m_len;
2964}
2965
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002966#define EXT4_EXT_ZERO_LEN 7
Amit Arora56055d32007-07-17 21:42:38 -04002967/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002968 * This function is called by ext4_ext_map_blocks() if someone tries to write
Amit Arora56055d32007-07-17 21:42:38 -04002969 * to an uninitialized extent. It may result in splitting the uninitialized
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002970 * extent into multiple extents (up to three - one initialized and two
Amit Arora56055d32007-07-17 21:42:38 -04002971 * uninitialized).
2972 * There are three possibilities:
2973 * a> There is no split required: Entire extent should be initialized
2974 * b> Splits in two extents: Write is happening at either end of the extent
2975 * c> Splits in three extents: Somone is writing in middle of the extent
Eric Gouriou6f91bc52011-10-27 11:43:23 -04002976 *
2977 * Pre-conditions:
2978 * - The extent pointed to by 'path' is uninitialized.
2979 * - The extent pointed to by 'path' contains a superset
2980 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
2981 *
2982 * Post-conditions on success:
2983 * - the returned value is the number of blocks beyond map->l_lblk
2984 * that are allocated and initialized.
2985 * It is guaranteed to be >= map->m_len.
Amit Arora56055d32007-07-17 21:42:38 -04002986 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002987static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002988 struct inode *inode,
2989 struct ext4_map_blocks *map,
2990 struct ext4_ext_path *path)
Amit Arora56055d32007-07-17 21:42:38 -04002991{
Eric Gouriou6f91bc52011-10-27 11:43:23 -04002992 struct ext4_extent_header *eh;
Yongqiang Yang667eff32011-05-03 12:25:07 -04002993 struct ext4_map_blocks split_map;
2994 struct ext4_extent zero_ex;
2995 struct ext4_extent *ex;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002996 ext4_lblk_t ee_block, eof_block;
Dan Carpenterf85b2872011-10-26 03:42:36 -04002997 unsigned int ee_len, depth;
2998 int allocated;
Amit Arora56055d32007-07-17 21:42:38 -04002999 int err = 0;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003000 int split_flag = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003001
3002 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3003 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003004 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003005
3006 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3007 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003008 if (eof_block < map->m_lblk + map->m_len)
3009 eof_block = map->m_lblk + map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003010
3011 depth = ext_depth(inode);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003012 eh = path[depth].p_hdr;
Amit Arora56055d32007-07-17 21:42:38 -04003013 ex = path[depth].p_ext;
3014 ee_block = le32_to_cpu(ex->ee_block);
3015 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003016 allocated = ee_len - (map->m_lblk - ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003017
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003018 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
3019
3020 /* Pre-conditions */
3021 BUG_ON(!ext4_ext_is_uninitialized(ex));
3022 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003023
3024 /*
3025 * Attempt to transfer newly initialized blocks from the currently
3026 * uninitialized extent to its left neighbor. This is much cheaper
3027 * than an insertion followed by a merge as those involve costly
3028 * memmove() calls. This is the common case in steady state for
3029 * workloads doing fallocate(FALLOC_FL_KEEP_SIZE) followed by append
3030 * writes.
3031 *
3032 * Limitations of the current logic:
3033 * - L1: we only deal with writes at the start of the extent.
3034 * The approach could be extended to writes at the end
3035 * of the extent but this scenario was deemed less common.
3036 * - L2: we do not deal with writes covering the whole extent.
3037 * This would require removing the extent if the transfer
3038 * is possible.
3039 * - L3: we only attempt to merge with an extent stored in the
3040 * same extent tree node.
3041 */
3042 if ((map->m_lblk == ee_block) && /*L1*/
3043 (map->m_len < ee_len) && /*L2*/
3044 (ex > EXT_FIRST_EXTENT(eh))) { /*L3*/
3045 struct ext4_extent *prev_ex;
3046 ext4_lblk_t prev_lblk;
3047 ext4_fsblk_t prev_pblk, ee_pblk;
3048 unsigned int prev_len, write_len;
3049
3050 prev_ex = ex - 1;
3051 prev_lblk = le32_to_cpu(prev_ex->ee_block);
3052 prev_len = ext4_ext_get_actual_len(prev_ex);
3053 prev_pblk = ext4_ext_pblock(prev_ex);
3054 ee_pblk = ext4_ext_pblock(ex);
3055 write_len = map->m_len;
3056
3057 /*
3058 * A transfer of blocks from 'ex' to 'prev_ex' is allowed
3059 * upon those conditions:
3060 * - C1: prev_ex is initialized,
3061 * - C2: prev_ex is logically abutting ex,
3062 * - C3: prev_ex is physically abutting ex,
3063 * - C4: prev_ex can receive the additional blocks without
3064 * overflowing the (initialized) length limit.
3065 */
3066 if ((!ext4_ext_is_uninitialized(prev_ex)) && /*C1*/
3067 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3068 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
3069 (prev_len < (EXT_INIT_MAX_LEN - write_len))) { /*C4*/
3070 err = ext4_ext_get_access(handle, inode, path + depth);
3071 if (err)
3072 goto out;
3073
3074 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3075 map, ex, prev_ex);
3076
3077 /* Shift the start of ex by 'write_len' blocks */
3078 ex->ee_block = cpu_to_le32(ee_block + write_len);
3079 ext4_ext_store_pblock(ex, ee_pblk + write_len);
3080 ex->ee_len = cpu_to_le16(ee_len - write_len);
3081 ext4_ext_mark_uninitialized(ex); /* Restore the flag */
3082
3083 /* Extend prev_ex by 'write_len' blocks */
3084 prev_ex->ee_len = cpu_to_le16(prev_len + write_len);
3085
3086 /* Mark the block containing both extents as dirty */
3087 ext4_ext_dirty(handle, inode, path + depth);
3088
3089 /* Update path to point to the right extent */
3090 path[depth].p_ext = prev_ex;
3091
3092 /* Result: number of initialized blocks past m_lblk */
3093 allocated = write_len;
3094 goto out;
3095 }
3096 }
3097
Yongqiang Yang667eff32011-05-03 12:25:07 -04003098 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003099 /*
3100 * It is safe to convert extent to initialized via explicit
3101 * zeroout only if extent is fully insde i_size or new_size.
3102 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003103 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003104
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003105 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003106 if (ee_len <= 2*EXT4_EXT_ZERO_LEN &&
3107 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3108 err = ext4_ext_zeroout(inode, ex);
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003109 if (err)
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003110 goto out;
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003111
3112 err = ext4_ext_get_access(handle, inode, path + depth);
3113 if (err)
3114 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003115 ext4_ext_mark_initialized(ex);
3116 ext4_ext_try_to_merge(inode, path, ex);
3117 err = ext4_ext_dirty(handle, inode, path + depth);
3118 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04003119 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003120
Amit Arora56055d32007-07-17 21:42:38 -04003121 /*
Yongqiang Yang667eff32011-05-03 12:25:07 -04003122 * four cases:
3123 * 1. split the extent into three extents.
3124 * 2. split the extent into two extents, zeroout the first half.
3125 * 3. split the extent into two extents, zeroout the second half.
3126 * 4. split the extent into two extents with out zeroout.
Amit Arora56055d32007-07-17 21:42:38 -04003127 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003128 split_map.m_lblk = map->m_lblk;
3129 split_map.m_len = map->m_len;
3130
3131 if (allocated > map->m_len) {
3132 if (allocated <= EXT4_EXT_ZERO_LEN &&
3133 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3134 /* case 3 */
3135 zero_ex.ee_block =
Allison Henderson9b940f82011-05-16 10:11:09 -04003136 cpu_to_le32(map->m_lblk);
3137 zero_ex.ee_len = cpu_to_le16(allocated);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003138 ext4_ext_store_pblock(&zero_ex,
3139 ext4_ext_pblock(ex) + map->m_lblk - ee_block);
3140 err = ext4_ext_zeroout(inode, &zero_ex);
Amit Arora56055d32007-07-17 21:42:38 -04003141 if (err)
3142 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003143 split_map.m_lblk = map->m_lblk;
3144 split_map.m_len = allocated;
3145 } else if ((map->m_lblk - ee_block + map->m_len <
3146 EXT4_EXT_ZERO_LEN) &&
3147 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3148 /* case 2 */
3149 if (map->m_lblk != ee_block) {
3150 zero_ex.ee_block = ex->ee_block;
3151 zero_ex.ee_len = cpu_to_le16(map->m_lblk -
3152 ee_block);
3153 ext4_ext_store_pblock(&zero_ex,
3154 ext4_ext_pblock(ex));
3155 err = ext4_ext_zeroout(inode, &zero_ex);
3156 if (err)
3157 goto out;
3158 }
3159
Yongqiang Yang667eff32011-05-03 12:25:07 -04003160 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003161 split_map.m_len = map->m_lblk - ee_block + map->m_len;
3162 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003163 }
3164 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003165
3166 allocated = ext4_split_extent(handle, inode, path,
3167 &split_map, split_flag, 0);
3168 if (allocated < 0)
3169 err = allocated;
3170
Amit Arora56055d32007-07-17 21:42:38 -04003171out:
3172 return err ? err : allocated;
3173}
3174
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003175/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003176 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003177 * ext4_get_blocks_dio_write() when DIO to write
3178 * to an uninitialized extent.
3179 *
Paul Bollefd018fe2011-02-15 00:05:43 +01003180 * Writing to an uninitialized extent may result in splitting the uninitialized
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003181 * extent into multiple /initialized uninitialized extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003182 * There are three possibilities:
3183 * a> There is no split required: Entire extent should be uninitialized
3184 * b> Splits in two extents: Write is happening at either end of the extent
3185 * c> Splits in three extents: Somone is writing in middle of the extent
3186 *
3187 * One of more index blocks maybe needed if the extent tree grow after
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003188 * the uninitialized extent split. To prevent ENOSPC occur at the IO
Mingming Cao00314622009-09-28 15:49:08 -04003189 * complete, we need to split the uninitialized extent before DIO submit
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02003190 * the IO. The uninitialized extent called at this time will be split
Mingming Cao00314622009-09-28 15:49:08 -04003191 * into three uninitialized extent(at most). After IO complete, the part
3192 * being filled will be convert to initialized by the end_io callback function
3193 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003194 *
3195 * Returns the size of uninitialized extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003196 */
3197static int ext4_split_unwritten_extents(handle_t *handle,
3198 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003199 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003200 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04003201 int flags)
3202{
Yongqiang Yang667eff32011-05-03 12:25:07 -04003203 ext4_lblk_t eof_block;
3204 ext4_lblk_t ee_block;
3205 struct ext4_extent *ex;
3206 unsigned int ee_len;
3207 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003208
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003209 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
3210 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003211 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003212
3213 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3214 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003215 if (eof_block < map->m_lblk + map->m_len)
3216 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003217 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003218 * It is safe to convert extent to initialized via explicit
3219 * zeroout only if extent is fully insde i_size or new_size.
3220 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003221 depth = ext_depth(inode);
3222 ex = path[depth].p_ext;
3223 ee_block = le32_to_cpu(ex->ee_block);
3224 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003225
Yongqiang Yang667eff32011-05-03 12:25:07 -04003226 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
3227 split_flag |= EXT4_EXT_MARK_UNINIT2;
Mingming Cao00314622009-09-28 15:49:08 -04003228
Yongqiang Yang667eff32011-05-03 12:25:07 -04003229 flags |= EXT4_GET_BLOCKS_PRE_IO;
3230 return ext4_split_extent(handle, inode, path, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003231}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003232
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003233static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003234 struct inode *inode,
3235 struct ext4_ext_path *path)
3236{
3237 struct ext4_extent *ex;
Mingming Cao00314622009-09-28 15:49:08 -04003238 int depth;
3239 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003240
3241 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003242 ex = path[depth].p_ext;
3243
Yongqiang Yang197217a2011-05-03 11:45:29 -04003244 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3245 "block %llu, max_blocks %u\n", inode->i_ino,
3246 (unsigned long long)le32_to_cpu(ex->ee_block),
3247 ext4_ext_get_actual_len(ex));
3248
Mingming Cao00314622009-09-28 15:49:08 -04003249 err = ext4_ext_get_access(handle, inode, path + depth);
3250 if (err)
3251 goto out;
3252 /* first mark the extent as initialized */
3253 ext4_ext_mark_initialized(ex);
3254
Yongqiang Yang197217a2011-05-03 11:45:29 -04003255 /* note: ext4_ext_correct_indexes() isn't needed here because
3256 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003257 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04003258 ext4_ext_try_to_merge(inode, path, ex);
3259
Mingming Cao00314622009-09-28 15:49:08 -04003260 /* Mark modified extent as dirty */
3261 err = ext4_ext_dirty(handle, inode, path + depth);
3262out:
3263 ext4_ext_show_leaf(inode, path);
3264 return err;
3265}
3266
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003267static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3268 sector_t block, int count)
3269{
3270 int i;
3271 for (i = 0; i < count; i++)
3272 unmap_underlying_metadata(bdev, block + i);
3273}
3274
Theodore Ts'o58590b02010-10-27 21:23:12 -04003275/*
3276 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3277 */
3278static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
Eric Sandeend002ebf2011-01-10 13:03:35 -05003279 ext4_lblk_t lblk,
Theodore Ts'o58590b02010-10-27 21:23:12 -04003280 struct ext4_ext_path *path,
3281 unsigned int len)
3282{
3283 int i, depth;
3284 struct ext4_extent_header *eh;
Sergey Senozhatsky65922cb2011-03-23 14:08:27 -04003285 struct ext4_extent *last_ex;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003286
3287 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3288 return 0;
3289
3290 depth = ext_depth(inode);
3291 eh = path[depth].p_hdr;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003292
3293 if (unlikely(!eh->eh_entries)) {
3294 EXT4_ERROR_INODE(inode, "eh->eh_entries == 0 and "
3295 "EOFBLOCKS_FL set");
3296 return -EIO;
3297 }
3298 last_ex = EXT_LAST_EXTENT(eh);
3299 /*
3300 * We should clear the EOFBLOCKS_FL flag if we are writing the
3301 * last block in the last extent in the file. We test this by
3302 * first checking to see if the caller to
3303 * ext4_ext_get_blocks() was interested in the last block (or
3304 * a block beyond the last block) in the current extent. If
3305 * this turns out to be false, we can bail out from this
3306 * function immediately.
3307 */
Eric Sandeend002ebf2011-01-10 13:03:35 -05003308 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
Theodore Ts'o58590b02010-10-27 21:23:12 -04003309 ext4_ext_get_actual_len(last_ex))
3310 return 0;
3311 /*
3312 * If the caller does appear to be planning to write at or
3313 * beyond the end of the current extent, we then test to see
3314 * if the current extent is the last extent in the file, by
3315 * checking to make sure it was reached via the rightmost node
3316 * at each level of the tree.
3317 */
3318 for (i = depth-1; i >= 0; i--)
3319 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3320 return 0;
3321 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3322 return ext4_mark_inode_dirty(handle, inode);
3323}
3324
Aditya Kali7b415bf2011-09-09 19:04:51 -04003325/**
3326 * ext4_find_delalloc_range: find delayed allocated block in the given range.
3327 *
3328 * Goes through the buffer heads in the range [lblk_start, lblk_end] and returns
3329 * whether there are any buffers marked for delayed allocation. It returns '1'
3330 * on the first delalloc'ed buffer head found. If no buffer head in the given
3331 * range is marked for delalloc, it returns 0.
3332 * lblk_start should always be <= lblk_end.
3333 * search_hint_reverse is to indicate that searching in reverse from lblk_end to
3334 * lblk_start might be more efficient (i.e., we will likely hit the delalloc'ed
3335 * block sooner). This is useful when blocks are truncated sequentially from
3336 * lblk_start towards lblk_end.
3337 */
3338static int ext4_find_delalloc_range(struct inode *inode,
3339 ext4_lblk_t lblk_start,
3340 ext4_lblk_t lblk_end,
3341 int search_hint_reverse)
3342{
3343 struct address_space *mapping = inode->i_mapping;
3344 struct buffer_head *head, *bh = NULL;
3345 struct page *page;
3346 ext4_lblk_t i, pg_lblk;
3347 pgoff_t index;
3348
Robin Dong8c48f7e2011-12-18 23:05:43 -05003349 if (!test_opt(inode->i_sb, DELALLOC))
3350 return 0;
3351
Aditya Kali7b415bf2011-09-09 19:04:51 -04003352 /* reverse search wont work if fs block size is less than page size */
3353 if (inode->i_blkbits < PAGE_CACHE_SHIFT)
3354 search_hint_reverse = 0;
3355
3356 if (search_hint_reverse)
3357 i = lblk_end;
3358 else
3359 i = lblk_start;
3360
3361 index = i >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
3362
3363 while ((i >= lblk_start) && (i <= lblk_end)) {
3364 page = find_get_page(mapping, index);
Aditya Kali5356f262011-09-09 19:20:51 -04003365 if (!page)
Aditya Kali7b415bf2011-09-09 19:04:51 -04003366 goto nextpage;
3367
Aditya Kali7b415bf2011-09-09 19:04:51 -04003368 if (!page_has_buffers(page))
3369 goto nextpage;
3370
3371 head = page_buffers(page);
3372 if (!head)
3373 goto nextpage;
3374
3375 bh = head;
3376 pg_lblk = index << (PAGE_CACHE_SHIFT -
3377 inode->i_blkbits);
3378 do {
3379 if (unlikely(pg_lblk < lblk_start)) {
3380 /*
3381 * This is possible when fs block size is less
3382 * than page size and our cluster starts/ends in
3383 * middle of the page. So we need to skip the
3384 * initial few blocks till we reach the 'lblk'
3385 */
3386 pg_lblk++;
3387 continue;
3388 }
3389
Aditya Kali5356f262011-09-09 19:20:51 -04003390 /* Check if the buffer is delayed allocated and that it
3391 * is not yet mapped. (when da-buffers are mapped during
3392 * their writeout, their da_mapped bit is set.)
3393 */
3394 if (buffer_delay(bh) && !buffer_da_mapped(bh)) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04003395 page_cache_release(page);
Aditya Kalid8990242011-09-09 19:18:51 -04003396 trace_ext4_find_delalloc_range(inode,
3397 lblk_start, lblk_end,
3398 search_hint_reverse,
3399 1, i);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003400 return 1;
3401 }
3402 if (search_hint_reverse)
3403 i--;
3404 else
3405 i++;
3406 } while ((i >= lblk_start) && (i <= lblk_end) &&
3407 ((bh = bh->b_this_page) != head));
3408nextpage:
3409 if (page)
3410 page_cache_release(page);
3411 /*
3412 * Move to next page. 'i' will be the first lblk in the next
3413 * page.
3414 */
3415 if (search_hint_reverse)
3416 index--;
3417 else
3418 index++;
3419 i = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
3420 }
3421
Aditya Kalid8990242011-09-09 19:18:51 -04003422 trace_ext4_find_delalloc_range(inode, lblk_start, lblk_end,
3423 search_hint_reverse, 0, 0);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003424 return 0;
3425}
3426
3427int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk,
3428 int search_hint_reverse)
3429{
3430 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3431 ext4_lblk_t lblk_start, lblk_end;
3432 lblk_start = lblk & (~(sbi->s_cluster_ratio - 1));
3433 lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
3434
3435 return ext4_find_delalloc_range(inode, lblk_start, lblk_end,
3436 search_hint_reverse);
3437}
3438
3439/**
3440 * Determines how many complete clusters (out of those specified by the 'map')
3441 * are under delalloc and were reserved quota for.
3442 * This function is called when we are writing out the blocks that were
3443 * originally written with their allocation delayed, but then the space was
3444 * allocated using fallocate() before the delayed allocation could be resolved.
3445 * The cases to look for are:
3446 * ('=' indicated delayed allocated blocks
3447 * '-' indicates non-delayed allocated blocks)
3448 * (a) partial clusters towards beginning and/or end outside of allocated range
3449 * are not delalloc'ed.
3450 * Ex:
3451 * |----c---=|====c====|====c====|===-c----|
3452 * |++++++ allocated ++++++|
3453 * ==> 4 complete clusters in above example
3454 *
3455 * (b) partial cluster (outside of allocated range) towards either end is
3456 * marked for delayed allocation. In this case, we will exclude that
3457 * cluster.
3458 * Ex:
3459 * |----====c========|========c========|
3460 * |++++++ allocated ++++++|
3461 * ==> 1 complete clusters in above example
3462 *
3463 * Ex:
3464 * |================c================|
3465 * |++++++ allocated ++++++|
3466 * ==> 0 complete clusters in above example
3467 *
3468 * The ext4_da_update_reserve_space will be called only if we
3469 * determine here that there were some "entire" clusters that span
3470 * this 'allocated' range.
3471 * In the non-bigalloc case, this function will just end up returning num_blks
3472 * without ever calling ext4_find_delalloc_range.
3473 */
3474static unsigned int
3475get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start,
3476 unsigned int num_blks)
3477{
3478 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3479 ext4_lblk_t alloc_cluster_start, alloc_cluster_end;
3480 ext4_lblk_t lblk_from, lblk_to, c_offset;
3481 unsigned int allocated_clusters = 0;
3482
3483 alloc_cluster_start = EXT4_B2C(sbi, lblk_start);
3484 alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1);
3485
3486 /* max possible clusters for this allocation */
3487 allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1;
3488
Aditya Kalid8990242011-09-09 19:18:51 -04003489 trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks);
3490
Aditya Kali7b415bf2011-09-09 19:04:51 -04003491 /* Check towards left side */
3492 c_offset = lblk_start & (sbi->s_cluster_ratio - 1);
3493 if (c_offset) {
3494 lblk_from = lblk_start & (~(sbi->s_cluster_ratio - 1));
3495 lblk_to = lblk_from + c_offset - 1;
3496
3497 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
3498 allocated_clusters--;
3499 }
3500
3501 /* Now check towards right. */
3502 c_offset = (lblk_start + num_blks) & (sbi->s_cluster_ratio - 1);
3503 if (allocated_clusters && c_offset) {
3504 lblk_from = lblk_start + num_blks;
3505 lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1;
3506
3507 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
3508 allocated_clusters--;
3509 }
3510
3511 return allocated_clusters;
3512}
3513
Mingming Cao00314622009-09-28 15:49:08 -04003514static int
3515ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003516 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003517 struct ext4_ext_path *path, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003518 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003519{
3520 int ret = 0;
3521 int err = 0;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003522 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Mingming Cao00314622009-09-28 15:49:08 -04003523
Zheng Liu88635ca2011-12-28 19:00:25 -05003524 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical "
3525 "block %llu, max_blocks %u, flags %x, allocated %u\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003526 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04003527 flags, allocated);
3528 ext4_ext_show_leaf(inode, path);
3529
Aditya Kalid8990242011-09-09 19:18:51 -04003530 trace_ext4_ext_handle_uninitialized_extents(inode, map, allocated,
3531 newblock);
3532
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003533 /* get_block() before submit the IO, split the extent */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003534 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003535 ret = ext4_split_unwritten_extents(handle, inode, map,
3536 path, flags);
Mingming5f524952009-11-10 10:48:04 -05003537 /*
3538 * Flag the inode(non aio case) or end_io struct (aio case)
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003539 * that this IO needs to conversion to written when IO is
Mingming5f524952009-11-10 10:48:04 -05003540 * completed
3541 */
Tao Ma0edeb712011-10-31 17:30:44 -04003542 if (io)
3543 ext4_set_io_unwritten_flag(inode, io);
3544 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003545 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
Jiaying Zhang744692d2010-03-04 16:14:02 -05003546 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003547 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao00314622009-09-28 15:49:08 -04003548 goto out;
3549 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003550 /* IO end_io complete, convert the filled extent to written */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003551 if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003552 ret = ext4_convert_unwritten_extents_endio(handle, inode,
Mingming Cao00314622009-09-28 15:49:08 -04003553 path);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003554 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05003555 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05003556 err = check_eofblocks_fl(handle, inode, map->m_lblk,
3557 path, map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003558 } else
3559 err = ret;
Mingming Cao00314622009-09-28 15:49:08 -04003560 goto out2;
3561 }
3562 /* buffered IO case */
3563 /*
3564 * repeat fallocate creation request
3565 * we already have an unwritten extent
3566 */
3567 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3568 goto map_out;
3569
3570 /* buffered READ or buffered write_begin() lookup */
3571 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3572 /*
3573 * We have blocks reserved already. We
3574 * return allocated blocks so that delalloc
3575 * won't do block reservation for us. But
3576 * the buffer head will be unmapped so that
3577 * a read from the block returns 0s.
3578 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003579 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003580 goto out1;
3581 }
3582
3583 /* buffered write, writepage time, convert*/
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003584 ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04003585 if (ret >= 0)
Jan Karab436b9b2009-12-08 23:51:10 -05003586 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04003587out:
3588 if (ret <= 0) {
3589 err = ret;
3590 goto out2;
3591 } else
3592 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003593 map->m_flags |= EXT4_MAP_NEW;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003594 /*
3595 * if we allocated more blocks than requested
3596 * we need to make sure we unmap the extra block
3597 * allocated. The actual needed block will get
3598 * unmapped later when we find the buffer_head marked
3599 * new.
3600 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003601 if (allocated > map->m_len) {
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003602 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003603 newblock + map->m_len,
3604 allocated - map->m_len);
3605 allocated = map->m_len;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003606 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003607
3608 /*
3609 * If we have done fallocate with the offset that is already
3610 * delayed allocated, we would have block reservation
3611 * and quota reservation done in the delayed write path.
3612 * But fallocate would have already updated quota and block
3613 * count for this offset. So cancel these reservation
3614 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003615 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
3616 unsigned int reserved_clusters;
3617 reserved_clusters = get_reserved_cluster_alloc(inode,
3618 map->m_lblk, map->m_len);
3619 if (reserved_clusters)
3620 ext4_da_update_reserve_space(inode,
3621 reserved_clusters,
3622 0);
3623 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003624
Mingming Cao00314622009-09-28 15:49:08 -04003625map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003626 map->m_flags |= EXT4_MAP_MAPPED;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04003627 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
3628 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
3629 map->m_len);
3630 if (err < 0)
3631 goto out2;
3632 }
Mingming Cao00314622009-09-28 15:49:08 -04003633out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003634 if (allocated > map->m_len)
3635 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003636 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003637 map->m_pblk = newblock;
3638 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04003639out2:
3640 if (path) {
3641 ext4_ext_drop_refs(path);
3642 kfree(path);
3643 }
3644 return err ? err : allocated;
3645}
Theodore Ts'o58590b02010-10-27 21:23:12 -04003646
Mingming Cao00314622009-09-28 15:49:08 -04003647/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003648 * get_implied_cluster_alloc - check to see if the requested
3649 * allocation (in the map structure) overlaps with a cluster already
3650 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04003651 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003652 * @map The requested lblk->pblk mapping
3653 * @ex The extent structure which might contain an implied
3654 * cluster allocation
3655 *
3656 * This function is called by ext4_ext_map_blocks() after we failed to
3657 * find blocks that were already in the inode's extent tree. Hence,
3658 * we know that the beginning of the requested region cannot overlap
3659 * the extent from the inode's extent tree. There are three cases we
3660 * want to catch. The first is this case:
3661 *
3662 * |--- cluster # N--|
3663 * |--- extent ---| |---- requested region ---|
3664 * |==========|
3665 *
3666 * The second case that we need to test for is this one:
3667 *
3668 * |--------- cluster # N ----------------|
3669 * |--- requested region --| |------- extent ----|
3670 * |=======================|
3671 *
3672 * The third case is when the requested region lies between two extents
3673 * within the same cluster:
3674 * |------------- cluster # N-------------|
3675 * |----- ex -----| |---- ex_right ----|
3676 * |------ requested region ------|
3677 * |================|
3678 *
3679 * In each of the above cases, we need to set the map->m_pblk and
3680 * map->m_len so it corresponds to the return the extent labelled as
3681 * "|====|" from cluster #N, since it is already in use for data in
3682 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
3683 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
3684 * as a new "allocated" block region. Otherwise, we will return 0 and
3685 * ext4_ext_map_blocks() will then allocate one or more new clusters
3686 * by calling ext4_mb_new_blocks().
3687 */
Aditya Kalid8990242011-09-09 19:18:51 -04003688static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003689 struct ext4_map_blocks *map,
3690 struct ext4_extent *ex,
3691 struct ext4_ext_path *path)
3692{
Aditya Kalid8990242011-09-09 19:18:51 -04003693 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003694 ext4_lblk_t c_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
3695 ext4_lblk_t ex_cluster_start, ex_cluster_end;
Curt Wohlgemuth14d7f3e2011-12-18 17:39:02 -05003696 ext4_lblk_t rr_cluster_start;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003697 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
3698 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
3699 unsigned short ee_len = ext4_ext_get_actual_len(ex);
3700
3701 /* The extent passed in that we are trying to match */
3702 ex_cluster_start = EXT4_B2C(sbi, ee_block);
3703 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
3704
3705 /* The requested region passed into ext4_map_blocks() */
3706 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003707
3708 if ((rr_cluster_start == ex_cluster_end) ||
3709 (rr_cluster_start == ex_cluster_start)) {
3710 if (rr_cluster_start == ex_cluster_end)
3711 ee_start += ee_len - 1;
3712 map->m_pblk = (ee_start & ~(sbi->s_cluster_ratio - 1)) +
3713 c_offset;
3714 map->m_len = min(map->m_len,
3715 (unsigned) sbi->s_cluster_ratio - c_offset);
3716 /*
3717 * Check for and handle this case:
3718 *
3719 * |--------- cluster # N-------------|
3720 * |------- extent ----|
3721 * |--- requested region ---|
3722 * |===========|
3723 */
3724
3725 if (map->m_lblk < ee_block)
3726 map->m_len = min(map->m_len, ee_block - map->m_lblk);
3727
3728 /*
3729 * Check for the case where there is already another allocated
3730 * block to the right of 'ex' but before the end of the cluster.
3731 *
3732 * |------------- cluster # N-------------|
3733 * |----- ex -----| |---- ex_right ----|
3734 * |------ requested region ------|
3735 * |================|
3736 */
3737 if (map->m_lblk > ee_block) {
3738 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
3739 map->m_len = min(map->m_len, next - map->m_lblk);
3740 }
Aditya Kalid8990242011-09-09 19:18:51 -04003741
3742 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003743 return 1;
3744 }
Aditya Kalid8990242011-09-09 19:18:51 -04003745
3746 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003747 return 0;
3748}
3749
3750
3751/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05003752 * Block allocation/map/preallocation routine for extents based files
3753 *
3754 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003755 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003756 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3757 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05003758 *
3759 * return > 0, number of of blocks already mapped/allocated
3760 * if create == 0 and these are pre-allocated blocks
3761 * buffer head is unmapped
3762 * otherwise blocks are mapped
3763 *
3764 * return = 0, if plain look up failed (blocks have not been allocated)
3765 * buffer head is unmapped
3766 *
3767 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003768 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003769int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3770 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07003771{
3772 struct ext4_ext_path *path = NULL;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003773 struct ext4_extent newex, *ex, *ex2;
3774 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003775 ext4_fsblk_t newblock = 0;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003776 int free_on_err = 0, err = 0, depth, ret;
3777 unsigned int allocated = 0, offset = 0;
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04003778 unsigned int allocated_clusters = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003779 struct ext4_allocation_request ar;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003780 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003781 ext4_lblk_t cluster_offset;
Alex Tomasa86c6182006-10-11 01:21:03 -07003782
Mingming84fe3be2009-09-01 08:44:37 -04003783 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003784 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003785 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07003786
3787 /* check in cache */
Lukas Czerner78771912012-03-19 23:05:43 -04003788 if (ext4_ext_in_cache(inode, map->m_lblk, &newex)) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003789 if (!newex.ee_start_lo && !newex.ee_start_hi) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04003790 if ((sbi->s_cluster_ratio > 1) &&
3791 ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
3792 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3793
Theodore Ts'oc2177052009-05-14 00:58:52 -04003794 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003795 /*
3796 * block isn't allocated yet and
3797 * user doesn't want to allocate it
3798 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003799 goto out2;
3800 }
3801 /* we should allocate requested block */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003802 } else {
Alex Tomasa86c6182006-10-11 01:21:03 -07003803 /* block is already allocated */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003804 if (sbi->s_cluster_ratio > 1)
3805 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003806 newblock = map->m_lblk
Dave Kleikamp8c55e202007-05-24 13:04:54 -04003807 - le32_to_cpu(newex.ee_block)
Theodore Ts'obf89d162010-10-27 21:30:14 -04003808 + ext4_ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003809 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003810 allocated = ext4_ext_get_actual_len(&newex) -
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003811 (map->m_lblk - le32_to_cpu(newex.ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -07003812 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07003813 }
3814 }
3815
3816 /* find extent for this block */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003817 path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07003818 if (IS_ERR(path)) {
3819 err = PTR_ERR(path);
3820 path = NULL;
3821 goto out2;
3822 }
3823
3824 depth = ext_depth(inode);
3825
3826 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003827 * consistent leaf must not be empty;
3828 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07003829 * this is why assert can't be put in ext4_ext_find_extent()
3830 */
Frank Mayhar273df552010-03-02 11:46:09 -05003831 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
3832 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04003833 "lblock: %lu, depth: %d pblock %lld",
3834 (unsigned long) map->m_lblk, depth,
3835 path[depth].p_block);
Surbhi Palande034fb4c2009-12-14 09:53:52 -05003836 err = -EIO;
3837 goto out2;
3838 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003839
Avantika Mathur7e028972006-12-06 20:41:33 -08003840 ex = path[depth].p_ext;
3841 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003842 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003843 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003844 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003845
3846 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003847 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04003848 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003849 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003850 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04003851
3852 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
3853
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003854 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003855 if (in_range(map->m_lblk, ee_block, ee_len)) {
3856 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003857 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003858 allocated = ee_len - (map->m_lblk - ee_block);
3859 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
3860 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04003861
Allison Hendersone8613042011-05-25 07:41:46 -04003862 /*
Lukas Czerner78771912012-03-19 23:05:43 -04003863 * Do not put uninitialized extent
3864 * in the cache
Allison Hendersone8613042011-05-25 07:41:46 -04003865 */
Lukas Czerner78771912012-03-19 23:05:43 -04003866 if (!ext4_ext_is_uninitialized(ex)) {
3867 ext4_ext_put_in_cache(inode, ee_block,
3868 ee_len, ee_start);
3869 goto out;
Allison Hendersone8613042011-05-25 07:41:46 -04003870 }
Lukas Czerner78771912012-03-19 23:05:43 -04003871 ret = ext4_ext_handle_uninitialized_extents(
3872 handle, inode, map, path, flags,
3873 allocated, newblock);
3874 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07003875 }
3876 }
3877
Aditya Kali7b415bf2011-09-09 19:04:51 -04003878 if ((sbi->s_cluster_ratio > 1) &&
3879 ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
3880 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3881
Alex Tomasa86c6182006-10-11 01:21:03 -07003882 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003883 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07003884 * we couldn't try to create block if create flag is zero
3885 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04003886 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003887 /*
3888 * put just found gap into cache to speed up
3889 * subsequent requests
3890 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003891 ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
Alex Tomasa86c6182006-10-11 01:21:03 -07003892 goto out2;
3893 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003894
Alex Tomasa86c6182006-10-11 01:21:03 -07003895 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003896 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07003897 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003898 map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003899 newex.ee_block = cpu_to_le32(map->m_lblk);
3900 cluster_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
3901
3902 /*
3903 * If we are doing bigalloc, check to see if the extent returned
3904 * by ext4_ext_find_extent() implies a cluster we can use.
3905 */
3906 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04003907 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003908 ar.len = allocated = map->m_len;
3909 newblock = map->m_pblk;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003910 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003911 goto got_allocated_blocks;
3912 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003913
Alex Tomasc9de5602008-01-29 00:19:52 -05003914 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003915 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003916 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3917 if (err)
3918 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003919 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003920 ex2 = NULL;
3921 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
Alex Tomasc9de5602008-01-29 00:19:52 -05003922 if (err)
3923 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04003924
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003925 /* Check if the extent after searching to the right implies a
3926 * cluster we can use. */
3927 if ((sbi->s_cluster_ratio > 1) && ex2 &&
Aditya Kalid8990242011-09-09 19:18:51 -04003928 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003929 ar.len = allocated = map->m_len;
3930 newblock = map->m_pblk;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003931 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003932 goto got_allocated_blocks;
3933 }
3934
Amit Arora749269f2007-07-18 09:02:56 -04003935 /*
3936 * See if request is beyond maximum number of blocks we can have in
3937 * a single extent. For an initialized extent this limit is
3938 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3939 * EXT_UNINIT_MAX_LEN.
3940 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003941 if (map->m_len > EXT_INIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003942 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003943 map->m_len = EXT_INIT_MAX_LEN;
3944 else if (map->m_len > EXT_UNINIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003945 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003946 map->m_len = EXT_UNINIT_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04003947
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003948 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003949 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003950 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04003951 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003952 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04003953 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003954 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003955
3956 /* allocate new block */
3957 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003958 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
3959 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003960 /*
3961 * We calculate the offset from the beginning of the cluster
3962 * for the logical block number, since when we allocate a
3963 * physical cluster, the physical block should start at the
3964 * same offset from the beginning of the cluster. This is
3965 * needed so that future calls to get_implied_cluster_alloc()
3966 * work correctly.
3967 */
3968 offset = map->m_lblk & (sbi->s_cluster_ratio - 1);
3969 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
3970 ar.goal -= offset;
3971 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05003972 if (S_ISREG(inode->i_mode))
3973 ar.flags = EXT4_MB_HINT_DATA;
3974 else
3975 /* disable in-core preallocation for non-regular files */
3976 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04003977 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
3978 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05003979 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07003980 if (!newblock)
3981 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04003982 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003983 ar.goal, newblock, allocated);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003984 free_on_err = 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003985 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003986 ar.len = EXT4_C2B(sbi, ar.len) - offset;
3987 if (ar.len > allocated)
3988 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07003989
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003990got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07003991 /* try to insert new extent into found leaf and return */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003992 ext4_ext_store_pblock(&newex, newblock + offset);
Alex Tomasc9de5602008-01-29 00:19:52 -05003993 newex.ee_len = cpu_to_le16(ar.len);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003994 /* Mark uninitialized */
3995 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
Amit Aroraa2df2a62007-07-17 21:42:41 -04003996 ext4_ext_mark_uninitialized(&newex);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003997 /*
Jiaying Zhang744692d2010-03-04 16:14:02 -05003998 * io_end structure was created for every IO write to an
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003999 * uninitialized extent. To avoid unnecessary conversion,
Jiaying Zhang744692d2010-03-04 16:14:02 -05004000 * here we flag the IO that really needs the conversion.
Mingming5f524952009-11-10 10:48:04 -05004001 * For non asycn direct IO case, flag the inode state
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004002 * that we need to perform conversion when IO is done.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004003 */
Jiaying Zhang744692d2010-03-04 16:14:02 -05004004 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Tao Ma0edeb712011-10-31 17:30:44 -04004005 if (io)
4006 ext4_set_io_unwritten_flag(inode, io);
4007 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004008 ext4_set_inode_state(inode,
4009 EXT4_STATE_DIO_UNWRITTEN);
Mingming5f524952009-11-10 10:48:04 -05004010 }
Jiaying Zhang744692d2010-03-04 16:14:02 -05004011 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004012 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004013 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004014
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004015 err = 0;
4016 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
4017 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4018 path, ar.len);
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004019 if (!err)
4020 err = ext4_ext_insert_extent(handle, inode, path,
4021 &newex, flags);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004022 if (err && free_on_err) {
Maxim Patlasov7132de72011-07-10 19:37:48 -04004023 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
4024 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
Alex Tomas315054f2007-05-24 13:04:25 -04004025 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05004026 /* not a good idea to call discard here directly,
4027 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004028 ext4_discard_preallocations(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05004029 ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex),
Maxim Patlasov7132de72011-07-10 19:37:48 -04004030 ext4_ext_get_actual_len(&newex), fb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004031 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04004032 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004033
Alex Tomasa86c6182006-10-11 01:21:03 -07004034 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04004035 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004036 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004037 if (allocated > map->m_len)
4038 allocated = map->m_len;
4039 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07004040
Jan Karab436b9b2009-12-08 23:51:10 -05004041 /*
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004042 * Update reserved blocks/metadata blocks after successful
4043 * block allocation which had been deferred till now.
4044 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04004045 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004046 unsigned int reserved_clusters;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004047 /*
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004048 * Check how many clusters we had reserved this allocated range
Aditya Kali7b415bf2011-09-09 19:04:51 -04004049 */
4050 reserved_clusters = get_reserved_cluster_alloc(inode,
4051 map->m_lblk, allocated);
4052 if (map->m_flags & EXT4_MAP_FROM_CLUSTER) {
4053 if (reserved_clusters) {
4054 /*
4055 * We have clusters reserved for this range.
4056 * But since we are not doing actual allocation
4057 * and are simply using blocks from previously
4058 * allocated cluster, we should release the
4059 * reservation and not claim quota.
4060 */
4061 ext4_da_update_reserve_space(inode,
4062 reserved_clusters, 0);
4063 }
4064 } else {
4065 BUG_ON(allocated_clusters < reserved_clusters);
4066 /* We will claim quota for all newly allocated blocks.*/
4067 ext4_da_update_reserve_space(inode, allocated_clusters,
4068 1);
4069 if (reserved_clusters < allocated_clusters) {
Aditya Kali5356f262011-09-09 19:20:51 -04004070 struct ext4_inode_info *ei = EXT4_I(inode);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004071 int reservation = allocated_clusters -
4072 reserved_clusters;
4073 /*
4074 * It seems we claimed few clusters outside of
4075 * the range of this allocation. We should give
4076 * it back to the reservation pool. This can
4077 * happen in the following case:
4078 *
4079 * * Suppose s_cluster_ratio is 4 (i.e., each
4080 * cluster has 4 blocks. Thus, the clusters
4081 * are [0-3],[4-7],[8-11]...
4082 * * First comes delayed allocation write for
4083 * logical blocks 10 & 11. Since there were no
4084 * previous delayed allocated blocks in the
4085 * range [8-11], we would reserve 1 cluster
4086 * for this write.
4087 * * Next comes write for logical blocks 3 to 8.
4088 * In this case, we will reserve 2 clusters
4089 * (for [0-3] and [4-7]; and not for [8-11] as
4090 * that range has a delayed allocated blocks.
4091 * Thus total reserved clusters now becomes 3.
4092 * * Now, during the delayed allocation writeout
4093 * time, we will first write blocks [3-8] and
4094 * allocate 3 clusters for writing these
4095 * blocks. Also, we would claim all these
4096 * three clusters above.
4097 * * Now when we come here to writeout the
4098 * blocks [10-11], we would expect to claim
4099 * the reservation of 1 cluster we had made
4100 * (and we would claim it since there are no
4101 * more delayed allocated blocks in the range
4102 * [8-11]. But our reserved cluster count had
4103 * already gone to 0.
4104 *
4105 * Thus, at the step 4 above when we determine
4106 * that there are still some unwritten delayed
4107 * allocated blocks outside of our current
4108 * block range, we should increment the
4109 * reserved clusters count so that when the
4110 * remaining blocks finally gets written, we
4111 * could claim them.
4112 */
Aditya Kali5356f262011-09-09 19:20:51 -04004113 dquot_reserve_block(inode,
4114 EXT4_C2B(sbi, reservation));
4115 spin_lock(&ei->i_block_reservation_lock);
4116 ei->i_reserved_data_blocks += reservation;
4117 spin_unlock(&ei->i_block_reservation_lock);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004118 }
4119 }
4120 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004121
4122 /*
Jan Karab436b9b2009-12-08 23:51:10 -05004123 * Cache the extent and update transaction to commit on fdatasync only
4124 * when it is _not_ an uninitialized extent.
4125 */
4126 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05004127 ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock);
Jan Karab436b9b2009-12-08 23:51:10 -05004128 ext4_update_inode_fsync_trans(handle, inode, 1);
4129 } else
4130 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004131out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004132 if (allocated > map->m_len)
4133 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004134 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004135 map->m_flags |= EXT4_MAP_MAPPED;
4136 map->m_pblk = newblock;
4137 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004138out2:
4139 if (path) {
4140 ext4_ext_drop_refs(path);
4141 kfree(path);
4142 }
Allison Hendersone8613042011-05-25 07:41:46 -04004143
Yongqiang Yange7b319e2011-10-29 09:39:51 -04004144 trace_ext4_ext_map_blocks_exit(inode, map->m_lblk,
Lukas Czerner78771912012-03-19 23:05:43 -04004145 newblock, map->m_len, err ? err : allocated);
Yongqiang Yange7b319e2011-10-29 09:39:51 -04004146
Lukas Czerner78771912012-03-19 23:05:43 -04004147 return err ? err : allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004148}
4149
Jan Karacf108bc2008-07-11 19:27:31 -04004150void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004151{
4152 struct address_space *mapping = inode->i_mapping;
4153 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004154 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004155 handle_t *handle;
Allison Henderson189e8682011-09-06 21:49:44 -04004156 loff_t page_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004157 int err = 0;
4158
4159 /*
Jiaying Zhang3889fd52011-01-10 12:47:05 -05004160 * finish any pending end_io work so we won't run the risk of
4161 * converting any truncated blocks to initialized later
4162 */
4163 ext4_flush_completed_IO(inode);
4164
4165 /*
Alex Tomasa86c6182006-10-11 01:21:03 -07004166 * probably first extent we're gonna free will be last in block
4167 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04004168 err = ext4_writepage_trans_blocks(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07004169 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04004170 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07004171 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07004172
Allison Henderson189e8682011-09-06 21:49:44 -04004173 if (inode->i_size % PAGE_CACHE_SIZE != 0) {
4174 page_len = PAGE_CACHE_SIZE -
4175 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4176
4177 err = ext4_discard_partial_page_buffers(handle,
4178 mapping, inode->i_size, page_len, 0);
4179
4180 if (err)
4181 goto out_stop;
4182 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004183
Jan Kara9ddfc3d2008-07-11 19:27:31 -04004184 if (ext4_orphan_add(handle, inode))
4185 goto out_stop;
4186
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004187 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07004188 ext4_ext_invalidate_cache(inode);
4189
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004190 ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05004191
Alex Tomasa86c6182006-10-11 01:21:03 -07004192 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004193 * TODO: optimization is possible here.
4194 * Probably we need not scan at all,
4195 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004196 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004197
4198 /* we have to know where to truncate from in crash case */
4199 EXT4_I(inode)->i_disksize = inode->i_size;
4200 ext4_mark_inode_dirty(handle, inode);
4201
4202 last_block = (inode->i_size + sb->s_blocksize - 1)
4203 >> EXT4_BLOCK_SIZE_BITS(sb);
Lukas Czerner5f95d212012-03-19 23:03:19 -04004204 err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07004205
4206 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04004207 * transaction synchronous.
4208 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004209 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05004210 ext4_handle_sync(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07004211
Jan Kara9ddfc3d2008-07-11 19:27:31 -04004212 up_write(&EXT4_I(inode)->i_data_sem);
Eric Gouriouf6d2f6b2011-05-22 21:33:00 -04004213
4214out_stop:
Alex Tomasa86c6182006-10-11 01:21:03 -07004215 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004216 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07004217 * then we need to clear up the orphan record which we created above.
4218 * However, if this was a real unlink then we were called by
4219 * ext4_delete_inode(), and we allow that function to clean up the
4220 * orphan info for us.
4221 */
4222 if (inode->i_nlink)
4223 ext4_orphan_del(handle, inode);
4224
Solofo Ramangalahyef737722008-04-29 22:00:41 -04004225 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4226 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07004227 ext4_journal_stop(handle);
4228}
4229
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004230static void ext4_falloc_update_inode(struct inode *inode,
4231 int mode, loff_t new_size, int update_ctime)
4232{
4233 struct timespec now;
4234
4235 if (update_ctime) {
4236 now = current_fs_time(inode->i_sb);
4237 if (!timespec_equal(&inode->i_ctime, &now))
4238 inode->i_ctime = now;
4239 }
4240 /*
4241 * Update only when preallocation was requested beyond
4242 * the file size.
4243 */
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04004244 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
4245 if (new_size > i_size_read(inode))
4246 i_size_write(inode, new_size);
4247 if (new_size > EXT4_I(inode)->i_disksize)
4248 ext4_update_i_disksize(inode, new_size);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004249 } else {
4250 /*
4251 * Mark that we allocate beyond EOF so the subsequent truncate
4252 * can proceed even if the new size is the same as i_size.
4253 */
4254 if (new_size > i_size_read(inode))
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004255 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004256 }
4257
4258}
4259
Amit Aroraa2df2a62007-07-17 21:42:41 -04004260/*
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004261 * preallocate space for a file. This implements ext4's fallocate file
Amit Aroraa2df2a62007-07-17 21:42:41 -04004262 * operation, which gets called from sys_fallocate system call.
4263 * For block-mapped files, posix_fallocate should fall back to the method
4264 * of writing zeroes to the required new blocks (the same behavior which is
4265 * expected for file systems which do not support fallocate() system call).
4266 */
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004267long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004268{
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004269 struct inode *inode = file->f_path.dentry->d_inode;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004270 handle_t *handle;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004271 loff_t new_size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004272 unsigned int max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004273 int ret = 0;
4274 int ret2 = 0;
4275 int retries = 0;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004276 int flags;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004277 struct ext4_map_blocks map;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004278 unsigned int credits, blkbits = inode->i_blkbits;
4279
4280 /*
4281 * currently supporting (pre)allocate mode for extent-based
4282 * files _only_
4283 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004284 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Amit Aroraa2df2a62007-07-17 21:42:41 -04004285 return -EOPNOTSUPP;
4286
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004287 /* Return error if mode is not supported */
4288 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
4289 return -EOPNOTSUPP;
4290
4291 if (mode & FALLOC_FL_PUNCH_HOLE)
4292 return ext4_punch_hole(file, offset, len);
4293
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004294 trace_ext4_fallocate_enter(inode, offset, len, mode);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004295 map.m_lblk = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004296 /*
4297 * We can't just convert len to max_blocks because
4298 * If blocksize = 4096 offset = 3072 and len = 2048
4299 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004300 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004301 - map.m_lblk;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004302 /*
Mingming Caof3bd1f32008-08-19 22:16:03 -04004303 * credits to insert 1 extent into extent tree
Amit Aroraa2df2a62007-07-17 21:42:41 -04004304 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04004305 credits = ext4_chunk_trans_blocks(inode, max_blocks);
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05004306 mutex_lock(&inode->i_mutex);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04004307 ret = inode_newsize_ok(inode, (len + offset));
4308 if (ret) {
4309 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004310 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04004311 return ret;
4312 }
Greg Harm3c6fe772011-10-31 18:41:47 -04004313 flags = EXT4_GET_BLOCKS_CREATE_UNINIT_EXT;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004314 if (mode & FALLOC_FL_KEEP_SIZE)
4315 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
Greg Harm3c6fe772011-10-31 18:41:47 -04004316 /*
4317 * Don't normalize the request if it can fit in one extent so
4318 * that it doesn't get unnecessarily split into multiple
4319 * extents.
4320 */
4321 if (len <= EXT_UNINIT_MAX_LEN << blkbits)
4322 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004323retry:
4324 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004325 map.m_lblk = map.m_lblk + ret;
4326 map.m_len = max_blocks = max_blocks - ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004327 handle = ext4_journal_start(inode, credits);
4328 if (IS_ERR(handle)) {
4329 ret = PTR_ERR(handle);
4330 break;
4331 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004332 ret = ext4_map_blocks(handle, inode, &map, flags);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05004333 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004334#ifdef EXT4FS_DEBUG
4335 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004336 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004337 "returned error inode#%lu, block=%u, "
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05004338 "max_blocks=%u", __func__,
Kazuya Mioa6371b62010-10-27 21:30:15 -04004339 inode->i_ino, map.m_lblk, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004340#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04004341 ext4_mark_inode_dirty(handle, inode);
4342 ret2 = ext4_journal_stop(handle);
4343 break;
4344 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004345 if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004346 blkbits) >> blkbits))
4347 new_size = offset + len;
4348 else
Utako Kusaka29ae07b2011-07-27 22:11:20 -04004349 new_size = ((loff_t) map.m_lblk + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004350
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004351 ext4_falloc_update_inode(inode, mode, new_size,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004352 (map.m_flags & EXT4_MAP_NEW));
Amit Aroraa2df2a62007-07-17 21:42:41 -04004353 ext4_mark_inode_dirty(handle, inode);
4354 ret2 = ext4_journal_stop(handle);
4355 if (ret2)
4356 break;
4357 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004358 if (ret == -ENOSPC &&
4359 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4360 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004361 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004362 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05004363 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004364 trace_ext4_fallocate_exit(inode, offset, max_blocks,
4365 ret > 0 ? ret2 : ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004366 return ret > 0 ? ret2 : ret;
4367}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004368
4369/*
Mingming Cao00314622009-09-28 15:49:08 -04004370 * This function convert a range of blocks to written extents
4371 * The caller of this function will pass the start offset and the size.
4372 * all unwritten extents within this range will be converted to
4373 * written extents.
4374 *
4375 * This function is called from the direct IO end io call back
4376 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05004377 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04004378 */
4379int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
Eric Sandeena1de02d2010-02-04 23:58:38 -05004380 ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04004381{
4382 handle_t *handle;
Mingming Cao00314622009-09-28 15:49:08 -04004383 unsigned int max_blocks;
4384 int ret = 0;
4385 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004386 struct ext4_map_blocks map;
Mingming Cao00314622009-09-28 15:49:08 -04004387 unsigned int credits, blkbits = inode->i_blkbits;
4388
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004389 map.m_lblk = offset >> blkbits;
Mingming Cao00314622009-09-28 15:49:08 -04004390 /*
4391 * We can't just convert len to max_blocks because
4392 * If blocksize = 4096 offset = 3072 and len = 2048
4393 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004394 max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
4395 map.m_lblk);
Mingming Cao00314622009-09-28 15:49:08 -04004396 /*
4397 * credits to insert 1 extent into extent tree
4398 */
4399 credits = ext4_chunk_trans_blocks(inode, max_blocks);
4400 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004401 map.m_lblk += ret;
4402 map.m_len = (max_blocks -= ret);
Mingming Cao00314622009-09-28 15:49:08 -04004403 handle = ext4_journal_start(inode, credits);
4404 if (IS_ERR(handle)) {
4405 ret = PTR_ERR(handle);
4406 break;
4407 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004408 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004409 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Mingming Cao00314622009-09-28 15:49:08 -04004410 if (ret <= 0) {
4411 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004412 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Mingming Cao00314622009-09-28 15:49:08 -04004413 "returned error inode#%lu, block=%u, "
4414 "max_blocks=%u", __func__,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004415 inode->i_ino, map.m_lblk, map.m_len);
Mingming Cao00314622009-09-28 15:49:08 -04004416 }
4417 ext4_mark_inode_dirty(handle, inode);
4418 ret2 = ext4_journal_stop(handle);
4419 if (ret <= 0 || ret2 )
4420 break;
4421 }
4422 return ret > 0 ? ret2 : ret;
4423}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004424
Mingming Cao00314622009-09-28 15:49:08 -04004425/*
Eric Sandeen6873fa02008-10-07 00:46:36 -04004426 * Callback function called for each extent to gather FIEMAP information.
4427 */
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04004428static int ext4_ext_fiemap_cb(struct inode *inode, ext4_lblk_t next,
Eric Sandeen6873fa02008-10-07 00:46:36 -04004429 struct ext4_ext_cache *newex, struct ext4_extent *ex,
4430 void *data)
4431{
Eric Sandeen6873fa02008-10-07 00:46:36 -04004432 __u64 logical;
4433 __u64 physical;
4434 __u64 length;
4435 __u32 flags = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004436 int ret = 0;
4437 struct fiemap_extent_info *fieinfo = data;
4438 unsigned char blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004439
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004440 blksize_bits = inode->i_sb->s_blocksize_bits;
4441 logical = (__u64)newex->ec_block << blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004442
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05004443 if (newex->ec_start == 0) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004444 /*
4445 * No extent in extent-tree contains block @newex->ec_start,
4446 * then the block may stay in 1)a hole or 2)delayed-extent.
4447 *
4448 * Holes or delayed-extents are processed as follows.
4449 * 1. lookup dirty pages with specified range in pagecache.
4450 * If no page is got, then there is no delayed-extent and
4451 * return with EXT_CONTINUE.
4452 * 2. find the 1st mapped buffer,
4453 * 3. check if the mapped buffer is both in the request range
4454 * and a delayed buffer. If not, there is no delayed-extent,
4455 * then return.
4456 * 4. a delayed-extent is found, the extent will be collected.
4457 */
4458 ext4_lblk_t end = 0;
4459 pgoff_t last_offset;
4460 pgoff_t offset;
4461 pgoff_t index;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004462 pgoff_t start_index = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004463 struct page **pages = NULL;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004464 struct buffer_head *bh = NULL;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004465 struct buffer_head *head = NULL;
4466 unsigned int nr_pages = PAGE_SIZE / sizeof(struct page *);
4467
4468 pages = kmalloc(PAGE_SIZE, GFP_KERNEL);
4469 if (pages == NULL)
4470 return -ENOMEM;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004471
4472 offset = logical >> PAGE_SHIFT;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004473repeat:
4474 last_offset = offset;
4475 head = NULL;
4476 ret = find_get_pages_tag(inode->i_mapping, &offset,
4477 PAGECACHE_TAG_DIRTY, nr_pages, pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004478
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004479 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4480 /* First time, try to find a mapped buffer. */
4481 if (ret == 0) {
4482out:
4483 for (index = 0; index < ret; index++)
4484 page_cache_release(pages[index]);
4485 /* just a hole. */
4486 kfree(pages);
4487 return EXT_CONTINUE;
4488 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004489 index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004490
Yongqiang Yangb2213492011-05-24 11:36:58 -04004491next_page:
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004492 /* Try to find the 1st mapped buffer. */
Yongqiang Yangb2213492011-05-24 11:36:58 -04004493 end = ((__u64)pages[index]->index << PAGE_SHIFT) >>
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004494 blksize_bits;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004495 if (!page_has_buffers(pages[index]))
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004496 goto out;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004497 head = page_buffers(pages[index]);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004498 if (!head)
4499 goto out;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004500
Yongqiang Yangb2213492011-05-24 11:36:58 -04004501 index++;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004502 bh = head;
4503 do {
Yongqiang Yangb2213492011-05-24 11:36:58 -04004504 if (end >= newex->ec_block +
4505 newex->ec_len)
4506 /* The buffer is out of
4507 * the request range.
4508 */
4509 goto out;
4510
4511 if (buffer_mapped(bh) &&
4512 end >= newex->ec_block) {
4513 start_index = index - 1;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004514 /* get the 1st mapped buffer. */
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004515 goto found_mapped_buffer;
4516 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004517
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004518 bh = bh->b_this_page;
4519 end++;
4520 } while (bh != head);
4521
Yongqiang Yangb2213492011-05-24 11:36:58 -04004522 /* No mapped buffer in the range found in this page,
4523 * We need to look up next page.
4524 */
4525 if (index >= ret) {
4526 /* There is no page left, but we need to limit
4527 * newex->ec_len.
4528 */
4529 newex->ec_len = end - newex->ec_block;
4530 goto out;
4531 }
4532 goto next_page;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004533 } else {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004534 /*Find contiguous delayed buffers. */
4535 if (ret > 0 && pages[0]->index == last_offset)
4536 head = page_buffers(pages[0]);
4537 bh = head;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004538 index = 1;
4539 start_index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004540 }
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004541
4542found_mapped_buffer:
4543 if (bh != NULL && buffer_delay(bh)) {
4544 /* 1st or contiguous delayed buffer found. */
4545 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4546 /*
4547 * 1st delayed buffer found, record
4548 * the start of extent.
4549 */
4550 flags |= FIEMAP_EXTENT_DELALLOC;
4551 newex->ec_block = end;
4552 logical = (__u64)end << blksize_bits;
4553 }
4554 /* Find contiguous delayed buffers. */
4555 do {
4556 if (!buffer_delay(bh))
4557 goto found_delayed_extent;
4558 bh = bh->b_this_page;
4559 end++;
4560 } while (bh != head);
4561
Yongqiang Yangb2213492011-05-24 11:36:58 -04004562 for (; index < ret; index++) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004563 if (!page_has_buffers(pages[index])) {
4564 bh = NULL;
4565 break;
4566 }
4567 head = page_buffers(pages[index]);
4568 if (!head) {
4569 bh = NULL;
4570 break;
4571 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004572
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004573 if (pages[index]->index !=
Yongqiang Yangb2213492011-05-24 11:36:58 -04004574 pages[start_index]->index + index
4575 - start_index) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004576 /* Blocks are not contiguous. */
4577 bh = NULL;
4578 break;
4579 }
4580 bh = head;
4581 do {
4582 if (!buffer_delay(bh))
4583 /* Delayed-extent ends. */
4584 goto found_delayed_extent;
4585 bh = bh->b_this_page;
4586 end++;
4587 } while (bh != head);
4588 }
4589 } else if (!(flags & FIEMAP_EXTENT_DELALLOC))
4590 /* a hole found. */
4591 goto out;
4592
4593found_delayed_extent:
4594 newex->ec_len = min(end - newex->ec_block,
4595 (ext4_lblk_t)EXT_INIT_MAX_LEN);
4596 if (ret == nr_pages && bh != NULL &&
4597 newex->ec_len < EXT_INIT_MAX_LEN &&
4598 buffer_delay(bh)) {
4599 /* Have not collected an extent and continue. */
4600 for (index = 0; index < ret; index++)
4601 page_cache_release(pages[index]);
4602 goto repeat;
4603 }
4604
4605 for (index = 0; index < ret; index++)
4606 page_cache_release(pages[index]);
4607 kfree(pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004608 }
4609
4610 physical = (__u64)newex->ec_start << blksize_bits;
4611 length = (__u64)newex->ec_len << blksize_bits;
4612
4613 if (ex && ext4_ext_is_uninitialized(ex))
4614 flags |= FIEMAP_EXTENT_UNWRITTEN;
4615
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04004616 if (next == EXT_MAX_BLOCKS)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004617 flags |= FIEMAP_EXTENT_LAST;
4618
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004619 ret = fiemap_fill_next_extent(fieinfo, logical, physical,
Eric Sandeen6873fa02008-10-07 00:46:36 -04004620 length, flags);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004621 if (ret < 0)
4622 return ret;
4623 if (ret == 1)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004624 return EXT_BREAK;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004625 return EXT_CONTINUE;
4626}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004627/* fiemap flags we can handle specified here */
4628#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
4629
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004630static int ext4_xattr_fiemap(struct inode *inode,
4631 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004632{
4633 __u64 physical = 0;
4634 __u64 length;
4635 __u32 flags = FIEMAP_EXTENT_LAST;
4636 int blockbits = inode->i_sb->s_blocksize_bits;
4637 int error = 0;
4638
4639 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004640 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04004641 struct ext4_iloc iloc;
4642 int offset; /* offset of xattr in inode */
4643
4644 error = ext4_get_inode_loc(inode, &iloc);
4645 if (error)
4646 return error;
4647 physical = iloc.bh->b_blocknr << blockbits;
4648 offset = EXT4_GOOD_OLD_INODE_SIZE +
4649 EXT4_I(inode)->i_extra_isize;
4650 physical += offset;
4651 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
4652 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004653 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004654 } else { /* external block */
4655 physical = EXT4_I(inode)->i_file_acl << blockbits;
4656 length = inode->i_sb->s_blocksize;
4657 }
4658
4659 if (physical)
4660 error = fiemap_fill_next_extent(fieinfo, 0, physical,
4661 length, flags);
4662 return (error < 0 ? error : 0);
4663}
4664
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004665/*
4666 * ext4_ext_punch_hole
4667 *
4668 * Punches a hole of "length" bytes in a file starting
4669 * at byte "offset"
4670 *
4671 * @inode: The inode of the file to punch a hole in
4672 * @offset: The starting byte offset of the hole
4673 * @length: The length of the hole
4674 *
4675 * Returns the number of blocks removed or negative on err
4676 */
4677int ext4_ext_punch_hole(struct file *file, loff_t offset, loff_t length)
4678{
4679 struct inode *inode = file->f_path.dentry->d_inode;
4680 struct super_block *sb = inode->i_sb;
Lukas Czerner5f95d212012-03-19 23:03:19 -04004681 ext4_lblk_t first_block, stop_block;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004682 struct address_space *mapping = inode->i_mapping;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004683 handle_t *handle;
Allison Hendersonba062082011-09-03 11:55:59 -04004684 loff_t first_page, last_page, page_len;
4685 loff_t first_page_offset, last_page_offset;
Lukas Czerner5f95d212012-03-19 23:03:19 -04004686 int credits, err = 0;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004687
Allison Henderson2be47512011-09-03 11:56:52 -04004688 /* No need to punch hole beyond i_size */
4689 if (offset >= inode->i_size)
4690 return 0;
4691
4692 /*
4693 * If the hole extends beyond i_size, set the hole
4694 * to end after the page that contains i_size
4695 */
4696 if (offset + length > inode->i_size) {
4697 length = inode->i_size +
4698 PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) -
4699 offset;
4700 }
4701
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004702 first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
4703 last_page = (offset + length) >> PAGE_CACHE_SHIFT;
4704
4705 first_page_offset = first_page << PAGE_CACHE_SHIFT;
4706 last_page_offset = last_page << PAGE_CACHE_SHIFT;
4707
4708 /*
4709 * Write out all dirty pages to avoid race conditions
4710 * Then release them.
4711 */
4712 if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
4713 err = filemap_write_and_wait_range(mapping,
Allison Henderson2be47512011-09-03 11:56:52 -04004714 offset, offset + length - 1);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004715
Allison Henderson2be47512011-09-03 11:56:52 -04004716 if (err)
4717 return err;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004718 }
4719
4720 /* Now release the pages */
4721 if (last_page_offset > first_page_offset) {
4722 truncate_inode_pages_range(mapping, first_page_offset,
4723 last_page_offset-1);
4724 }
4725
4726 /* finish any pending end_io work */
4727 ext4_flush_completed_IO(inode);
4728
4729 credits = ext4_writepage_trans_blocks(inode);
4730 handle = ext4_journal_start(inode, credits);
4731 if (IS_ERR(handle))
4732 return PTR_ERR(handle);
4733
4734 err = ext4_orphan_add(handle, inode);
4735 if (err)
4736 goto out;
4737
4738 /*
Allison Hendersonba062082011-09-03 11:55:59 -04004739 * Now we need to zero out the non-page-aligned data in the
4740 * pages at the start and tail of the hole, and unmap the buffer
4741 * heads for the block aligned regions of the page that were
4742 * completely zeroed.
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004743 */
Allison Hendersonba062082011-09-03 11:55:59 -04004744 if (first_page > last_page) {
4745 /*
4746 * If the file space being truncated is contained within a page
4747 * just zero out and unmap the middle of that page
4748 */
4749 err = ext4_discard_partial_page_buffers(handle,
4750 mapping, offset, length, 0);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004751
Allison Hendersonba062082011-09-03 11:55:59 -04004752 if (err)
4753 goto out;
4754 } else {
4755 /*
4756 * zero out and unmap the partial page that contains
4757 * the start of the hole
4758 */
4759 page_len = first_page_offset - offset;
4760 if (page_len > 0) {
4761 err = ext4_discard_partial_page_buffers(handle, mapping,
4762 offset, page_len, 0);
4763 if (err)
4764 goto out;
4765 }
4766
4767 /*
4768 * zero out and unmap the partial page that contains
4769 * the end of the hole
4770 */
4771 page_len = offset + length - last_page_offset;
4772 if (page_len > 0) {
4773 err = ext4_discard_partial_page_buffers(handle, mapping,
4774 last_page_offset, page_len, 0);
4775 if (err)
4776 goto out;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004777 }
4778 }
4779
Allison Henderson2be47512011-09-03 11:56:52 -04004780 /*
4781 * If i_size is contained in the last page, we need to
4782 * unmap and zero the partial page after i_size
4783 */
4784 if (inode->i_size >> PAGE_CACHE_SHIFT == last_page &&
4785 inode->i_size % PAGE_CACHE_SIZE != 0) {
4786
4787 page_len = PAGE_CACHE_SIZE -
4788 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4789
4790 if (page_len > 0) {
4791 err = ext4_discard_partial_page_buffers(handle,
4792 mapping, inode->i_size, page_len, 0);
4793
4794 if (err)
4795 goto out;
4796 }
4797 }
4798
Lukas Czerner5f95d212012-03-19 23:03:19 -04004799 first_block = (offset + sb->s_blocksize - 1) >>
4800 EXT4_BLOCK_SIZE_BITS(sb);
4801 stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
4802
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004803 /* If there are no blocks to remove, return now */
Lukas Czerner5f95d212012-03-19 23:03:19 -04004804 if (first_block >= stop_block)
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004805 goto out;
4806
4807 down_write(&EXT4_I(inode)->i_data_sem);
4808 ext4_ext_invalidate_cache(inode);
4809 ext4_discard_preallocations(inode);
4810
Lukas Czerner5f95d212012-03-19 23:03:19 -04004811 err = ext4_ext_remove_space(inode, first_block, stop_block - 1);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004812
Lukas Czerner5f95d212012-03-19 23:03:19 -04004813 ext4_ext_invalidate_cache(inode);
4814 ext4_discard_preallocations(inode);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004815
4816 if (IS_SYNC(inode))
4817 ext4_handle_sync(handle);
4818
4819 up_write(&EXT4_I(inode)->i_data_sem);
4820
4821out:
4822 ext4_orphan_del(handle, inode);
4823 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4824 ext4_mark_inode_dirty(handle, inode);
4825 ext4_journal_stop(handle);
4826 return err;
4827}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004828int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4829 __u64 start, __u64 len)
4830{
4831 ext4_lblk_t start_blk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004832 int error = 0;
4833
4834 /* fallback to generic here if not in extents fmt */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004835 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeen6873fa02008-10-07 00:46:36 -04004836 return generic_block_fiemap(inode, fieinfo, start, len,
4837 ext4_get_block);
4838
4839 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
4840 return -EBADR;
4841
4842 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
4843 error = ext4_xattr_fiemap(inode, fieinfo);
4844 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004845 ext4_lblk_t len_blks;
4846 __u64 last_blk;
4847
Eric Sandeen6873fa02008-10-07 00:46:36 -04004848 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004849 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04004850 if (last_blk >= EXT_MAX_BLOCKS)
4851 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004852 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004853
4854 /*
4855 * Walk the extent tree gathering extent information.
4856 * ext4_ext_fiemap_cb will push extents back to user.
4857 */
Eric Sandeen6873fa02008-10-07 00:46:36 -04004858 error = ext4_ext_walk_space(inode, start_blk, len_blks,
4859 ext4_ext_fiemap_cb, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004860 }
4861
4862 return error;
4863}