blob: c883561e9b610642ffba7aa694917da3e5cb00eb [file] [log] [blame]
Alex Tomasa86c6182006-10-11 01:21:03 -07001/*
2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
4 *
5 * Architecture independence:
6 * Copyright (c) 2005, Bull S.A.
7 * Written by Pierre Peiffer <pierre.peiffer@bull.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public Licens
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
21 */
22
23/*
24 * Extents support for EXT4
25 *
26 * TODO:
27 * - ext4*_error() should be used in some situations
28 * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
29 * - smart tree reduction
30 */
31
Alex Tomasa86c6182006-10-11 01:21:03 -070032#include <linux/fs.h>
33#include <linux/time.h>
Mingming Caocd02ff02007-10-16 18:38:25 -040034#include <linux/jbd2.h>
Alex Tomasa86c6182006-10-11 01:21:03 -070035#include <linux/highuid.h>
36#include <linux/pagemap.h>
37#include <linux/quotaops.h>
38#include <linux/string.h>
39#include <linux/slab.h>
Amit Aroraa2df2a62007-07-17 21:42:41 -040040#include <linux/falloc.h>
Alex Tomasa86c6182006-10-11 01:21:03 -070041#include <asm/uaccess.h>
Eric Sandeen6873fa02008-10-07 00:46:36 -040042#include <linux/fiemap.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040043#include "ext4_jbd2.h"
Alex Tomasa86c6182006-10-11 01:21:03 -070044
Jiaying Zhang0562e0b2011-03-21 21:38:05 -040045#include <trace/events/ext4.h>
46
Lukas Czerner5f95d212012-03-19 23:03:19 -040047/*
48 * used by extent splitting.
49 */
50#define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
51 due to ENOSPC */
52#define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */
53#define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */
54
Dmitry Monakhov4319fd02012-10-10 01:04:58 -040055#define EXT4_EXT_DATA_VALID1 0x8 /* first half contains valid data */
56#define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */
57
Allison Hendersond583fb82011-05-25 07:41:43 -040058static int ext4_split_extent(handle_t *handle,
59 struct inode *inode,
60 struct ext4_ext_path *path,
61 struct ext4_map_blocks *map,
62 int split_flag,
63 int flags);
64
Lukas Czerner5f95d212012-03-19 23:03:19 -040065static int ext4_split_extent_at(handle_t *handle,
66 struct inode *inode,
67 struct ext4_ext_path *path,
68 ext4_lblk_t split,
69 int split_flag,
70 int flags);
71
Jan Kara487caee2009-08-17 22:17:20 -040072static int ext4_ext_truncate_extend_restart(handle_t *handle,
73 struct inode *inode,
74 int needed)
Alex Tomasa86c6182006-10-11 01:21:03 -070075{
76 int err;
77
Frank Mayhar03901312009-01-07 00:06:22 -050078 if (!ext4_handle_valid(handle))
79 return 0;
Alex Tomasa86c6182006-10-11 01:21:03 -070080 if (handle->h_buffer_credits > needed)
Shen Feng9102e4f2008-07-11 19:27:31 -040081 return 0;
82 err = ext4_journal_extend(handle, needed);
Theodore Ts'o0123c932008-08-01 20:57:54 -040083 if (err <= 0)
Shen Feng9102e4f2008-07-11 19:27:31 -040084 return err;
Jan Kara487caee2009-08-17 22:17:20 -040085 err = ext4_truncate_restart_trans(handle, inode, needed);
Dmitry Monakhov0617b832010-05-17 01:00:00 -040086 if (err == 0)
87 err = -EAGAIN;
Jan Kara487caee2009-08-17 22:17:20 -040088
89 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -070090}
91
92/*
93 * could return:
94 * - EROFS
95 * - ENOMEM
96 */
97static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
98 struct ext4_ext_path *path)
99{
100 if (path->p_bh) {
101 /* path points to block */
102 return ext4_journal_get_write_access(handle, path->p_bh);
103 }
104 /* path points to leaf/index in inode body */
105 /* we use in-core data, no need to protect them */
106 return 0;
107}
108
109/*
110 * could return:
111 * - EROFS
112 * - ENOMEM
113 * - EIO
114 */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -0400115#define ext4_ext_dirty(handle, inode, path) \
116 __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path))
117static int __ext4_ext_dirty(const char *where, unsigned int line,
118 handle_t *handle, struct inode *inode,
119 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700120{
121 int err;
122 if (path->p_bh) {
123 /* path points to block */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -0400124 err = __ext4_handle_dirty_metadata(where, line, handle,
125 inode, path->p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700126 } else {
127 /* path points to leaf/index in inode body */
128 err = ext4_mark_inode_dirty(handle, inode);
129 }
130 return err;
131}
132
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700133static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700134 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500135 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700136{
Alex Tomasa86c6182006-10-11 01:21:03 -0700137 if (path) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400138 int depth = path->p_depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700139 struct ext4_extent *ex;
Alex Tomasa86c6182006-10-11 01:21:03 -0700140
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500141 /*
142 * Try to predict block placement assuming that we are
143 * filling in a file which will eventually be
144 * non-sparse --- i.e., in the case of libbfd writing
145 * an ELF object sections out-of-order but in a way
146 * the eventually results in a contiguous object or
147 * executable file, or some database extending a table
148 * space file. However, this is actually somewhat
149 * non-ideal if we are writing a sparse file such as
150 * qemu or KVM writing a raw image file that is going
151 * to stay fairly sparse, since it will end up
152 * fragmenting the file system's free space. Maybe we
153 * should have some hueristics or some way to allow
154 * userspace to pass a hint to file system,
Tao Mab8d65682011-01-21 23:21:31 +0800155 * especially if the latter case turns out to be
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500156 * common.
157 */
Avantika Mathur7e028972006-12-06 20:41:33 -0800158 ex = path[depth].p_ext;
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500159 if (ex) {
160 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
161 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
162
163 if (block > ext_block)
164 return ext_pblk + (block - ext_block);
165 else
166 return ext_pblk - (ext_block - block);
167 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700168
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700169 /* it looks like index is empty;
170 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700171 if (path[depth].p_bh)
172 return path[depth].p_bh->b_blocknr;
173 }
174
175 /* OK. use inode's group */
Eric Sandeenf86186b2011-06-28 10:01:31 -0400176 return ext4_inode_to_goal_block(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700177}
178
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400179/*
180 * Allocation for a meta data block
181 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700182static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400183ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700184 struct ext4_ext_path *path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400185 struct ext4_extent *ex, int *err, unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700186{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700187 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700188
189 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Allison Henderson55f020d2011-05-25 07:41:26 -0400190 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
191 NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700192 return newblock;
193}
194
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400195static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700196{
197 int size;
198
199 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
200 / sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100201#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400202 if (!check && size > 6)
203 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700204#endif
205 return size;
206}
207
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400208static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700209{
210 int size;
211
212 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
213 / sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100214#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400215 if (!check && size > 5)
216 size = 5;
Alex Tomasa86c6182006-10-11 01:21:03 -0700217#endif
218 return size;
219}
220
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400221static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700222{
223 int size;
224
225 size = sizeof(EXT4_I(inode)->i_data);
226 size -= sizeof(struct ext4_extent_header);
227 size /= sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100228#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400229 if (!check && size > 3)
230 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700231#endif
232 return size;
233}
234
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400235static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700236{
237 int size;
238
239 size = sizeof(EXT4_I(inode)->i_data);
240 size -= sizeof(struct ext4_extent_header);
241 size /= sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100242#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400243 if (!check && size > 4)
244 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700245#endif
246 return size;
247}
248
Mingming Caod2a17632008-07-14 17:52:37 -0400249/*
250 * Calculate the number of metadata blocks needed
251 * to allocate @blocks
252 * Worse case is one block per extent
253 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -0500254int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -0400255{
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500256 struct ext4_inode_info *ei = EXT4_I(inode);
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400257 int idxs;
Mingming Caod2a17632008-07-14 17:52:37 -0400258
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500259 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
260 / sizeof(struct ext4_extent_idx));
Mingming Caod2a17632008-07-14 17:52:37 -0400261
262 /*
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500263 * If the new delayed allocation block is contiguous with the
264 * previous da block, it can share index blocks with the
265 * previous block, so we only need to allocate a new index
266 * block every idxs leaf blocks. At ldxs**2 blocks, we need
267 * an additional index block, and at ldxs**3 blocks, yet
268 * another index blocks.
Mingming Caod2a17632008-07-14 17:52:37 -0400269 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500270 if (ei->i_da_metadata_calc_len &&
271 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400272 int num = 0;
273
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500274 if ((ei->i_da_metadata_calc_len % idxs) == 0)
275 num++;
276 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
277 num++;
278 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
279 num++;
280 ei->i_da_metadata_calc_len = 0;
281 } else
282 ei->i_da_metadata_calc_len++;
283 ei->i_da_metadata_calc_last_lblock++;
284 return num;
285 }
Mingming Caod2a17632008-07-14 17:52:37 -0400286
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500287 /*
288 * In the worst case we need a new set of index blocks at
289 * every level of the inode's extent tree.
290 */
291 ei->i_da_metadata_calc_len = 1;
292 ei->i_da_metadata_calc_last_lblock = lblock;
293 return ext_depth(inode) + 1;
Mingming Caod2a17632008-07-14 17:52:37 -0400294}
295
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400296static int
297ext4_ext_max_entries(struct inode *inode, int depth)
298{
299 int max;
300
301 if (depth == ext_depth(inode)) {
302 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400303 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400304 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400305 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400306 } else {
307 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400308 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400309 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400310 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400311 }
312
313 return max;
314}
315
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400316static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
317{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400318 ext4_fsblk_t block = ext4_ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400319 int len = ext4_ext_get_actual_len(ext);
Eryu Guana1192c02013-12-03 21:22:21 -0500320 ext4_lblk_t lblock = le32_to_cpu(ext->ee_block);
321 ext4_lblk_t last = lblock + len - 1;
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400322
Eryu Guana1192c02013-12-03 21:22:21 -0500323 if (lblock > last)
Theodore Ts'o31d4f3a2012-03-11 23:30:16 -0400324 return 0;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400325 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400326}
327
328static int ext4_valid_extent_idx(struct inode *inode,
329 struct ext4_extent_idx *ext_idx)
330{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400331 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400332
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400333 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400334}
335
336static int ext4_valid_extent_entries(struct inode *inode,
337 struct ext4_extent_header *eh,
338 int depth)
339{
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400340 unsigned short entries;
341 if (eh->eh_entries == 0)
342 return 1;
343
344 entries = le16_to_cpu(eh->eh_entries);
345
346 if (depth == 0) {
347 /* leaf entries */
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400348 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
Eryu Guana1192c02013-12-03 21:22:21 -0500349 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
350 ext4_fsblk_t pblock = 0;
351 ext4_lblk_t lblock = 0;
352 ext4_lblk_t prev = 0;
353 int len = 0;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400354 while (entries) {
355 if (!ext4_valid_extent(inode, ext))
356 return 0;
Eryu Guana1192c02013-12-03 21:22:21 -0500357
358 /* Check for overlapping extents */
359 lblock = le32_to_cpu(ext->ee_block);
360 len = ext4_ext_get_actual_len(ext);
361 if ((lblock <= prev) && prev) {
362 pblock = ext4_ext_pblock(ext);
363 es->s_last_error_block = cpu_to_le64(pblock);
364 return 0;
365 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400366 ext++;
367 entries--;
Eryu Guana1192c02013-12-03 21:22:21 -0500368 prev = lblock + len - 1;
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400369 }
370 } else {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400371 struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400372 while (entries) {
373 if (!ext4_valid_extent_idx(inode, ext_idx))
374 return 0;
375 ext_idx++;
376 entries--;
377 }
378 }
379 return 1;
380}
381
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400382static int __ext4_ext_check(const char *function, unsigned int line,
383 struct inode *inode, struct ext4_extent_header *eh,
384 int depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400385{
386 const char *error_msg;
387 int max = 0;
388
389 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
390 error_msg = "invalid magic";
391 goto corrupted;
392 }
393 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
394 error_msg = "unexpected eh_depth";
395 goto corrupted;
396 }
397 if (unlikely(eh->eh_max == 0)) {
398 error_msg = "invalid eh_max";
399 goto corrupted;
400 }
401 max = ext4_ext_max_entries(inode, depth);
402 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
403 error_msg = "too large eh_max";
404 goto corrupted;
405 }
406 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
407 error_msg = "invalid eh_entries";
408 goto corrupted;
409 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400410 if (!ext4_valid_extent_entries(inode, eh, depth)) {
411 error_msg = "invalid extent entries";
412 goto corrupted;
413 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400414 return 0;
415
416corrupted:
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400417 ext4_error_inode(inode, function, line, 0,
Theodore Ts'o24676da2010-05-16 21:00:00 -0400418 "bad header/extent: %s - magic %x, "
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400419 "entries %u, max %u(%u), depth %u(%u)",
Theodore Ts'o24676da2010-05-16 21:00:00 -0400420 error_msg, le16_to_cpu(eh->eh_magic),
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400421 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
422 max, le16_to_cpu(eh->eh_depth), depth);
423
424 return -EIO;
425}
426
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400427#define ext4_ext_check(inode, eh, depth) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400428 __ext4_ext_check(__func__, __LINE__, inode, eh, depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400429
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400430int ext4_ext_check_inode(struct inode *inode)
431{
432 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
433}
434
Alex Tomasa86c6182006-10-11 01:21:03 -0700435#ifdef EXT_DEBUG
436static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
437{
438 int k, l = path->p_depth;
439
440 ext_debug("path:");
441 for (k = 0; k <= l; k++, path++) {
442 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700443 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400444 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700445 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400446 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700447 le32_to_cpu(path->p_ext->ee_block),
Mingming553f9002009-09-18 13:34:55 -0400448 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400449 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400450 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700451 } else
452 ext_debug(" []");
453 }
454 ext_debug("\n");
455}
456
457static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
458{
459 int depth = ext_depth(inode);
460 struct ext4_extent_header *eh;
461 struct ext4_extent *ex;
462 int i;
463
464 if (!path)
465 return;
466
467 eh = path[depth].p_hdr;
468 ex = EXT_FIRST_EXTENT(eh);
469
Mingming553f9002009-09-18 13:34:55 -0400470 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
471
Alex Tomasa86c6182006-10-11 01:21:03 -0700472 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400473 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
474 ext4_ext_is_uninitialized(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400475 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700476 }
477 ext_debug("\n");
478}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400479
480static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
481 ext4_fsblk_t newblock, int level)
482{
483 int depth = ext_depth(inode);
484 struct ext4_extent *ex;
485
486 if (depth != level) {
487 struct ext4_extent_idx *idx;
488 idx = path[level].p_idx;
489 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
490 ext_debug("%d: move %d:%llu in new index %llu\n", level,
491 le32_to_cpu(idx->ei_block),
492 ext4_idx_pblock(idx),
493 newblock);
494 idx++;
495 }
496
497 return;
498 }
499
500 ex = path[depth].p_ext;
501 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
502 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
503 le32_to_cpu(ex->ee_block),
504 ext4_ext_pblock(ex),
505 ext4_ext_is_uninitialized(ex),
506 ext4_ext_get_actual_len(ex),
507 newblock);
508 ex++;
509 }
510}
511
Alex Tomasa86c6182006-10-11 01:21:03 -0700512#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400513#define ext4_ext_show_path(inode, path)
514#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400515#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700516#endif
517
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500518void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700519{
520 int depth = path->p_depth;
521 int i;
522
523 for (i = 0; i <= depth; i++, path++)
524 if (path->p_bh) {
525 brelse(path->p_bh);
526 path->p_bh = NULL;
527 }
528}
529
530/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700531 * ext4_ext_binsearch_idx:
532 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400533 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700534 */
535static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500536ext4_ext_binsearch_idx(struct inode *inode,
537 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700538{
539 struct ext4_extent_header *eh = path->p_hdr;
540 struct ext4_extent_idx *r, *l, *m;
541
Alex Tomasa86c6182006-10-11 01:21:03 -0700542
Eric Sandeenbba90742008-01-28 23:58:27 -0500543 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700544
545 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400546 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700547 while (l <= r) {
548 m = l + (r - l) / 2;
549 if (block < le32_to_cpu(m->ei_block))
550 r = m - 1;
551 else
552 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400553 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
554 m, le32_to_cpu(m->ei_block),
555 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700556 }
557
558 path->p_idx = l - 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700559 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400560 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700561
562#ifdef CHECK_BINSEARCH
563 {
564 struct ext4_extent_idx *chix, *ix;
565 int k;
566
567 chix = ix = EXT_FIRST_INDEX(eh);
568 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
569 if (k != 0 &&
570 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o47760042008-09-08 23:00:52 -0400571 printk(KERN_DEBUG "k=%d, ix=0x%p, "
572 "first=0x%p\n", k,
573 ix, EXT_FIRST_INDEX(eh));
574 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700575 le32_to_cpu(ix->ei_block),
576 le32_to_cpu(ix[-1].ei_block));
577 }
578 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400579 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700580 if (block < le32_to_cpu(ix->ei_block))
581 break;
582 chix = ix;
583 }
584 BUG_ON(chix != path->p_idx);
585 }
586#endif
587
588}
589
590/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700591 * ext4_ext_binsearch:
592 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400593 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700594 */
595static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500596ext4_ext_binsearch(struct inode *inode,
597 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700598{
599 struct ext4_extent_header *eh = path->p_hdr;
600 struct ext4_extent *r, *l, *m;
601
Alex Tomasa86c6182006-10-11 01:21:03 -0700602 if (eh->eh_entries == 0) {
603 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700604 * this leaf is empty:
605 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700606 */
607 return;
608 }
609
Eric Sandeenbba90742008-01-28 23:58:27 -0500610 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700611
612 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400613 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700614
615 while (l <= r) {
616 m = l + (r - l) / 2;
617 if (block < le32_to_cpu(m->ee_block))
618 r = m - 1;
619 else
620 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400621 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
622 m, le32_to_cpu(m->ee_block),
623 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700624 }
625
626 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400627 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400628 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400629 ext4_ext_pblock(path->p_ext),
Mingming553f9002009-09-18 13:34:55 -0400630 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400631 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700632
633#ifdef CHECK_BINSEARCH
634 {
635 struct ext4_extent *chex, *ex;
636 int k;
637
638 chex = ex = EXT_FIRST_EXTENT(eh);
639 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
640 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400641 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700642 if (block < le32_to_cpu(ex->ee_block))
643 break;
644 chex = ex;
645 }
646 BUG_ON(chex != path->p_ext);
647 }
648#endif
649
650}
651
652int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
653{
654 struct ext4_extent_header *eh;
655
656 eh = ext_inode_hdr(inode);
657 eh->eh_depth = 0;
658 eh->eh_entries = 0;
659 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400660 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700661 ext4_mark_inode_dirty(handle, inode);
662 ext4_ext_invalidate_cache(inode);
663 return 0;
664}
665
666struct ext4_ext_path *
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500667ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
668 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700669{
670 struct ext4_extent_header *eh;
671 struct buffer_head *bh;
672 short int depth, i, ppos = 0, alloc = 0;
673
674 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400675 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700676
677 /* account possible depth increase */
678 if (!path) {
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800679 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700680 GFP_NOFS);
681 if (!path)
682 return ERR_PTR(-ENOMEM);
683 alloc = 1;
684 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700685 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400686 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700687
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400688 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700689 /* walk through the tree */
690 while (i) {
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400691 int need_to_validate = 0;
692
Alex Tomasa86c6182006-10-11 01:21:03 -0700693 ext_debug("depth %d: num %d, max %d\n",
694 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400695
Alex Tomasa86c6182006-10-11 01:21:03 -0700696 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400697 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700698 path[ppos].p_depth = i;
699 path[ppos].p_ext = NULL;
700
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400701 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
702 if (unlikely(!bh))
Alex Tomasa86c6182006-10-11 01:21:03 -0700703 goto err;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400704 if (!bh_uptodate_or_lock(bh)) {
Jiaying Zhang0562e0b2011-03-21 21:38:05 -0400705 trace_ext4_ext_load_extent(inode, block,
706 path[ppos].p_block);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400707 if (bh_submit_read(bh) < 0) {
708 put_bh(bh);
709 goto err;
710 }
711 /* validate the extent entries */
712 need_to_validate = 1;
713 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700714 eh = ext_block_hdr(bh);
715 ppos++;
Frank Mayhar273df552010-03-02 11:46:09 -0500716 if (unlikely(ppos > depth)) {
717 put_bh(bh);
718 EXT4_ERROR_INODE(inode,
719 "ppos %d > depth %d", ppos, depth);
720 goto err;
721 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700722 path[ppos].p_bh = bh;
723 path[ppos].p_hdr = eh;
724 i--;
725
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400726 if (need_to_validate && ext4_ext_check(inode, eh, i))
Alex Tomasa86c6182006-10-11 01:21:03 -0700727 goto err;
728 }
729
730 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700731 path[ppos].p_ext = NULL;
732 path[ppos].p_idx = NULL;
733
Alex Tomasa86c6182006-10-11 01:21:03 -0700734 /* find extent */
735 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400736 /* if not an empty leaf */
737 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400738 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700739
740 ext4_ext_show_path(inode, path);
741
742 return path;
743
744err:
745 ext4_ext_drop_refs(path);
746 if (alloc)
747 kfree(path);
748 return ERR_PTR(-EIO);
749}
750
751/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700752 * ext4_ext_insert_index:
753 * insert new index [@logical;@ptr] into the block at @curp;
754 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700755 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400756static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
757 struct ext4_ext_path *curp,
758 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700759{
760 struct ext4_extent_idx *ix;
761 int len, err;
762
Avantika Mathur7e028972006-12-06 20:41:33 -0800763 err = ext4_ext_get_access(handle, inode, curp);
764 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700765 return err;
766
Frank Mayhar273df552010-03-02 11:46:09 -0500767 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
768 EXT4_ERROR_INODE(inode,
769 "logical %d == ei_block %d!",
770 logical, le32_to_cpu(curp->p_idx->ei_block));
771 return -EIO;
772 }
Robin Dongd4620312011-07-17 23:43:42 -0400773
774 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
775 >= le16_to_cpu(curp->p_hdr->eh_max))) {
776 EXT4_ERROR_INODE(inode,
777 "eh_entries %d >= eh_max %d!",
778 le16_to_cpu(curp->p_hdr->eh_entries),
779 le16_to_cpu(curp->p_hdr->eh_max));
780 return -EIO;
781 }
782
Alex Tomasa86c6182006-10-11 01:21:03 -0700783 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
784 /* insert after */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400785 ext_debug("insert new index %d after: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700786 ix = curp->p_idx + 1;
787 } else {
788 /* insert before */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400789 ext_debug("insert new index %d before: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700790 ix = curp->p_idx;
791 }
792
Eric Gouriou80e675f2011-10-27 11:52:18 -0400793 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
794 BUG_ON(len < 0);
795 if (len > 0) {
796 ext_debug("insert new index %d: "
797 "move %d indices from 0x%p to 0x%p\n",
798 logical, len, ix, ix + 1);
799 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
800 }
801
Tao Maf472e022011-10-17 10:13:46 -0400802 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
803 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
804 return -EIO;
805 }
806
Alex Tomasa86c6182006-10-11 01:21:03 -0700807 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700808 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400809 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700810
Frank Mayhar273df552010-03-02 11:46:09 -0500811 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
812 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
813 return -EIO;
814 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700815
816 err = ext4_ext_dirty(handle, inode, curp);
817 ext4_std_error(inode->i_sb, err);
818
819 return err;
820}
821
822/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700823 * ext4_ext_split:
824 * inserts new subtree into the path, using free index entry
825 * at depth @at:
826 * - allocates all needed blocks (new leaf and all intermediate index blocks)
827 * - makes decision where to split
828 * - moves remaining extents and index entries (right to the split point)
829 * into the newly allocated blocks
830 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700831 */
832static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -0400833 unsigned int flags,
834 struct ext4_ext_path *path,
835 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -0700836{
837 struct buffer_head *bh = NULL;
838 int depth = ext_depth(inode);
839 struct ext4_extent_header *neh;
840 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -0700841 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700842 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700843 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700844 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700845 int err = 0;
846
847 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700848 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700849
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700850 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700851 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -0500852 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
853 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
854 return -EIO;
855 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700856 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
857 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700858 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700859 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400860 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700861 } else {
862 border = newext->ee_block;
863 ext_debug("leaf will be added."
864 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400865 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700866 }
867
868 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700869 * If error occurs, then we break processing
870 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700871 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700872 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700873 */
874
875 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700876 * Get array to track all allocated blocks.
877 * We need this to handle errors and free blocks
878 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700879 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800880 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700881 if (!ablocks)
882 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700883
884 /* allocate all needed blocks */
885 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
886 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400887 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400888 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -0700889 if (newblock == 0)
890 goto cleanup;
891 ablocks[a] = newblock;
892 }
893
894 /* initialize new leaf */
895 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -0500896 if (unlikely(newblock == 0)) {
897 EXT4_ERROR_INODE(inode, "newblock == 0!");
898 err = -EIO;
899 goto cleanup;
900 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700901 bh = sb_getblk(inode->i_sb, newblock);
902 if (!bh) {
903 err = -EIO;
904 goto cleanup;
905 }
906 lock_buffer(bh);
907
Avantika Mathur7e028972006-12-06 20:41:33 -0800908 err = ext4_journal_get_create_access(handle, bh);
909 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700910 goto cleanup;
911
912 neh = ext_block_hdr(bh);
913 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400914 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700915 neh->eh_magic = EXT4_EXT_MAGIC;
916 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700917
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700918 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -0500919 if (unlikely(path[depth].p_hdr->eh_entries !=
920 path[depth].p_hdr->eh_max)) {
921 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
922 path[depth].p_hdr->eh_entries,
923 path[depth].p_hdr->eh_max);
924 err = -EIO;
925 goto cleanup;
926 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700927 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400928 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
929 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -0700930 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400931 struct ext4_extent *ex;
932 ex = EXT_FIRST_EXTENT(neh);
933 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400934 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700935 }
936
937 set_buffer_uptodate(bh);
938 unlock_buffer(bh);
939
Frank Mayhar03901312009-01-07 00:06:22 -0500940 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800941 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700942 goto cleanup;
943 brelse(bh);
944 bh = NULL;
945
946 /* correct old leaf */
947 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800948 err = ext4_ext_get_access(handle, inode, path + depth);
949 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700950 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400951 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800952 err = ext4_ext_dirty(handle, inode, path + depth);
953 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700954 goto cleanup;
955
956 }
957
958 /* create intermediate indexes */
959 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -0500960 if (unlikely(k < 0)) {
961 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
962 err = -EIO;
963 goto cleanup;
964 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700965 if (k)
966 ext_debug("create %d intermediate indices\n", k);
967 /* insert new index into current index block */
968 /* current depth stored in i var */
969 i = depth - 1;
970 while (k--) {
971 oldblock = newblock;
972 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -0500973 bh = sb_getblk(inode->i_sb, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700974 if (!bh) {
975 err = -EIO;
976 goto cleanup;
977 }
978 lock_buffer(bh);
979
Avantika Mathur7e028972006-12-06 20:41:33 -0800980 err = ext4_journal_get_create_access(handle, bh);
981 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700982 goto cleanup;
983
984 neh = ext_block_hdr(bh);
985 neh->eh_entries = cpu_to_le16(1);
986 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400987 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700988 neh->eh_depth = cpu_to_le16(depth - i);
989 fidx = EXT_FIRST_INDEX(neh);
990 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700991 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700992
Eric Sandeenbba90742008-01-28 23:58:27 -0500993 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
994 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700995
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400996 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -0500997 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
998 EXT_LAST_INDEX(path[i].p_hdr))) {
999 EXT4_ERROR_INODE(inode,
1000 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1001 le32_to_cpu(path[i].p_ext->ee_block));
1002 err = -EIO;
1003 goto cleanup;
1004 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001005 /* start copy indexes */
1006 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
1007 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
1008 EXT_MAX_INDEX(path[i].p_hdr));
1009 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07001010 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001011 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -07001012 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001013 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001014 }
1015 set_buffer_uptodate(bh);
1016 unlock_buffer(bh);
1017
Frank Mayhar03901312009-01-07 00:06:22 -05001018 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001019 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001020 goto cleanup;
1021 brelse(bh);
1022 bh = NULL;
1023
1024 /* correct old index */
1025 if (m) {
1026 err = ext4_ext_get_access(handle, inode, path + i);
1027 if (err)
1028 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001029 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001030 err = ext4_ext_dirty(handle, inode, path + i);
1031 if (err)
1032 goto cleanup;
1033 }
1034
1035 i--;
1036 }
1037
1038 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001039 err = ext4_ext_insert_index(handle, inode, path + at,
1040 le32_to_cpu(border), newblock);
1041
1042cleanup:
1043 if (bh) {
1044 if (buffer_locked(bh))
1045 unlock_buffer(bh);
1046 brelse(bh);
1047 }
1048
1049 if (err) {
1050 /* free all allocated blocks in error case */
1051 for (i = 0; i < depth; i++) {
1052 if (!ablocks[i])
1053 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001054 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001055 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001056 }
1057 }
1058 kfree(ablocks);
1059
1060 return err;
1061}
1062
1063/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001064 * ext4_ext_grow_indepth:
1065 * implements tree growing procedure:
1066 * - allocates new block
1067 * - moves top-level data (index block or leaf) into the new block
1068 * - initializes new top-level, creating index that points to the
1069 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001070 */
1071static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001072 unsigned int flags,
Allison Henderson55f020d2011-05-25 07:41:26 -04001073 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001074{
Alex Tomasa86c6182006-10-11 01:21:03 -07001075 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001076 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001077 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001078 int err = 0;
1079
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001080 newblock = ext4_ext_new_meta_block(handle, inode, NULL,
Allison Henderson55f020d2011-05-25 07:41:26 -04001081 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001082 if (newblock == 0)
1083 return err;
1084
1085 bh = sb_getblk(inode->i_sb, newblock);
1086 if (!bh) {
1087 err = -EIO;
1088 ext4_std_error(inode->i_sb, err);
1089 return err;
1090 }
1091 lock_buffer(bh);
1092
Avantika Mathur7e028972006-12-06 20:41:33 -08001093 err = ext4_journal_get_create_access(handle, bh);
1094 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001095 unlock_buffer(bh);
1096 goto out;
1097 }
1098
1099 /* move top-level index/leaf into new block */
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001100 memmove(bh->b_data, EXT4_I(inode)->i_data,
1101 sizeof(EXT4_I(inode)->i_data));
Alex Tomasa86c6182006-10-11 01:21:03 -07001102
1103 /* set size of new block */
1104 neh = ext_block_hdr(bh);
1105 /* old root could have indexes or leaves
1106 * so calculate e_max right way */
1107 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001108 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001109 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001110 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001111 neh->eh_magic = EXT4_EXT_MAGIC;
1112 set_buffer_uptodate(bh);
1113 unlock_buffer(bh);
1114
Frank Mayhar03901312009-01-07 00:06:22 -05001115 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001116 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001117 goto out;
1118
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001119 /* Update top-level index: num,max,pointer */
Alex Tomasa86c6182006-10-11 01:21:03 -07001120 neh = ext_inode_hdr(inode);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001121 neh->eh_entries = cpu_to_le16(1);
1122 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1123 if (neh->eh_depth == 0) {
1124 /* Root extent block becomes index block */
1125 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1126 EXT_FIRST_INDEX(neh)->ei_block =
1127 EXT_FIRST_EXTENT(neh)->ee_block;
1128 }
Mingming Cao2ae02102006-10-11 01:21:11 -07001129 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001130 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001131 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001132 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001133
Paul Mackerrasb4611ab2011-12-12 11:00:56 -05001134 neh->eh_depth = cpu_to_le16(le16_to_cpu(neh->eh_depth) + 1);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001135 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07001136out:
1137 brelse(bh);
1138
1139 return err;
1140}
1141
1142/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001143 * ext4_ext_create_new_leaf:
1144 * finds empty index and adds new leaf.
1145 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001146 */
1147static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001148 unsigned int flags,
1149 struct ext4_ext_path *path,
1150 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001151{
1152 struct ext4_ext_path *curp;
1153 int depth, i, err = 0;
1154
1155repeat:
1156 i = depth = ext_depth(inode);
1157
1158 /* walk up to the tree and look for free index entry */
1159 curp = path + depth;
1160 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1161 i--;
1162 curp--;
1163 }
1164
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001165 /* we use already allocated block for index block,
1166 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001167 if (EXT_HAS_FREE_INDEX(curp)) {
1168 /* if we found index with free entry, then use that
1169 * entry: create all needed subtree and add new leaf */
Allison Henderson55f020d2011-05-25 07:41:26 -04001170 err = ext4_ext_split(handle, inode, flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001171 if (err)
1172 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001173
1174 /* refill path */
1175 ext4_ext_drop_refs(path);
1176 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001177 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1178 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001179 if (IS_ERR(path))
1180 err = PTR_ERR(path);
1181 } else {
1182 /* tree is full, time to grow in depth */
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001183 err = ext4_ext_grow_indepth(handle, inode, flags, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001184 if (err)
1185 goto out;
1186
1187 /* refill path */
1188 ext4_ext_drop_refs(path);
1189 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001190 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1191 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001192 if (IS_ERR(path)) {
1193 err = PTR_ERR(path);
1194 goto out;
1195 }
1196
1197 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001198 * only first (depth 0 -> 1) produces free space;
1199 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001200 */
1201 depth = ext_depth(inode);
1202 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001203 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001204 goto repeat;
1205 }
1206 }
1207
1208out:
1209 return err;
1210}
1211
1212/*
Alex Tomas1988b512008-01-28 23:58:27 -05001213 * search the closest allocated block to the left for *logical
1214 * and returns it at @logical + it's physical address at @phys
1215 * if *logical is the smallest allocated block, the function
1216 * returns 0 at @phys
1217 * return value contains 0 (success) or error code
1218 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001219static int ext4_ext_search_left(struct inode *inode,
1220 struct ext4_ext_path *path,
1221 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001222{
1223 struct ext4_extent_idx *ix;
1224 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001225 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001226
Frank Mayhar273df552010-03-02 11:46:09 -05001227 if (unlikely(path == NULL)) {
1228 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1229 return -EIO;
1230 }
Alex Tomas1988b512008-01-28 23:58:27 -05001231 depth = path->p_depth;
1232 *phys = 0;
1233
1234 if (depth == 0 && path->p_ext == NULL)
1235 return 0;
1236
1237 /* usually extent in the path covers blocks smaller
1238 * then *logical, but it can be that extent is the
1239 * first one in the file */
1240
1241 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001242 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001243 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001244 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1245 EXT4_ERROR_INODE(inode,
1246 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1247 *logical, le32_to_cpu(ex->ee_block));
1248 return -EIO;
1249 }
Alex Tomas1988b512008-01-28 23:58:27 -05001250 while (--depth >= 0) {
1251 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001252 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1253 EXT4_ERROR_INODE(inode,
1254 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
Tao Ma6ee3b212011-10-08 16:08:34 -04001255 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001256 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
Tao Ma6ee3b212011-10-08 16:08:34 -04001257 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001258 depth);
1259 return -EIO;
1260 }
Alex Tomas1988b512008-01-28 23:58:27 -05001261 }
1262 return 0;
1263 }
1264
Frank Mayhar273df552010-03-02 11:46:09 -05001265 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1266 EXT4_ERROR_INODE(inode,
1267 "logical %d < ee_block %d + ee_len %d!",
1268 *logical, le32_to_cpu(ex->ee_block), ee_len);
1269 return -EIO;
1270 }
Alex Tomas1988b512008-01-28 23:58:27 -05001271
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001272 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001273 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001274 return 0;
1275}
1276
1277/*
1278 * search the closest allocated block to the right for *logical
1279 * and returns it at @logical + it's physical address at @phys
Tao Madf3ab172011-10-08 15:53:49 -04001280 * if *logical is the largest allocated block, the function
Alex Tomas1988b512008-01-28 23:58:27 -05001281 * returns 0 at @phys
1282 * return value contains 0 (success) or error code
1283 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001284static int ext4_ext_search_right(struct inode *inode,
1285 struct ext4_ext_path *path,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001286 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1287 struct ext4_extent **ret_ex)
Alex Tomas1988b512008-01-28 23:58:27 -05001288{
1289 struct buffer_head *bh = NULL;
1290 struct ext4_extent_header *eh;
1291 struct ext4_extent_idx *ix;
1292 struct ext4_extent *ex;
1293 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001294 int depth; /* Note, NOT eh_depth; depth from top of tree */
1295 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001296
Frank Mayhar273df552010-03-02 11:46:09 -05001297 if (unlikely(path == NULL)) {
1298 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1299 return -EIO;
1300 }
Alex Tomas1988b512008-01-28 23:58:27 -05001301 depth = path->p_depth;
1302 *phys = 0;
1303
1304 if (depth == 0 && path->p_ext == NULL)
1305 return 0;
1306
1307 /* usually extent in the path covers blocks smaller
1308 * then *logical, but it can be that extent is the
1309 * first one in the file */
1310
1311 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001312 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001313 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001314 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1315 EXT4_ERROR_INODE(inode,
1316 "first_extent(path[%d].p_hdr) != ex",
1317 depth);
1318 return -EIO;
1319 }
Alex Tomas1988b512008-01-28 23:58:27 -05001320 while (--depth >= 0) {
1321 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001322 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1323 EXT4_ERROR_INODE(inode,
1324 "ix != EXT_FIRST_INDEX *logical %d!",
1325 *logical);
1326 return -EIO;
1327 }
Alex Tomas1988b512008-01-28 23:58:27 -05001328 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001329 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001330 }
1331
Frank Mayhar273df552010-03-02 11:46:09 -05001332 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1333 EXT4_ERROR_INODE(inode,
1334 "logical %d < ee_block %d + ee_len %d!",
1335 *logical, le32_to_cpu(ex->ee_block), ee_len);
1336 return -EIO;
1337 }
Alex Tomas1988b512008-01-28 23:58:27 -05001338
1339 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1340 /* next allocated block in this leaf */
1341 ex++;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001342 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001343 }
1344
1345 /* go up and search for index to the right */
1346 while (--depth >= 0) {
1347 ix = path[depth].p_idx;
1348 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001349 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001350 }
1351
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001352 /* we've gone up to the root and found no index to the right */
1353 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001354
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001355got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001356 /* we've found index to the right, let's
1357 * follow it and find the closest allocated
1358 * block to the right */
1359 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001360 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001361 while (++depth < path->p_depth) {
1362 bh = sb_bread(inode->i_sb, block);
1363 if (bh == NULL)
1364 return -EIO;
1365 eh = ext_block_hdr(bh);
Eric Sandeen395a87b2009-03-10 18:18:47 -04001366 /* subtract from p_depth to get proper eh_depth */
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001367 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001368 put_bh(bh);
1369 return -EIO;
1370 }
1371 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001372 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001373 put_bh(bh);
1374 }
1375
1376 bh = sb_bread(inode->i_sb, block);
1377 if (bh == NULL)
1378 return -EIO;
1379 eh = ext_block_hdr(bh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001380 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001381 put_bh(bh);
1382 return -EIO;
1383 }
1384 ex = EXT_FIRST_EXTENT(eh);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001385found_extent:
Alex Tomas1988b512008-01-28 23:58:27 -05001386 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001387 *phys = ext4_ext_pblock(ex);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001388 *ret_ex = ex;
1389 if (bh)
1390 put_bh(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001391 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001392}
1393
1394/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001395 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001396 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001397 * NOTE: it considers block number from index entry as
1398 * allocated block. Thus, index entries have to be consistent
1399 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001400 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001401static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001402ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1403{
1404 int depth;
1405
1406 BUG_ON(path == NULL);
1407 depth = path->p_depth;
1408
1409 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001410 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001411
1412 while (depth >= 0) {
1413 if (depth == path->p_depth) {
1414 /* leaf */
Curt Wohlgemuth6f8ff532011-10-26 04:38:59 -04001415 if (path[depth].p_ext &&
1416 path[depth].p_ext !=
Alex Tomasa86c6182006-10-11 01:21:03 -07001417 EXT_LAST_EXTENT(path[depth].p_hdr))
1418 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1419 } else {
1420 /* index */
1421 if (path[depth].p_idx !=
1422 EXT_LAST_INDEX(path[depth].p_hdr))
1423 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1424 }
1425 depth--;
1426 }
1427
Lukas Czernerf17722f2011-06-06 00:05:17 -04001428 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001429}
1430
1431/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001432 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001433 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001434 */
Robin Dong57187892011-07-23 21:49:07 -04001435static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001436{
1437 int depth;
1438
1439 BUG_ON(path == NULL);
1440 depth = path->p_depth;
1441
1442 /* zero-tree has no leaf blocks at all */
1443 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001444 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001445
1446 /* go to index block */
1447 depth--;
1448
1449 while (depth >= 0) {
1450 if (path[depth].p_idx !=
1451 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001452 return (ext4_lblk_t)
1453 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001454 depth--;
1455 }
1456
Lukas Czernerf17722f2011-06-06 00:05:17 -04001457 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001458}
1459
1460/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001461 * ext4_ext_correct_indexes:
1462 * if leaf gets modified and modified extent is first in the leaf,
1463 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001464 * TODO: do we need to correct tree in all cases?
1465 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001466static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001467 struct ext4_ext_path *path)
1468{
1469 struct ext4_extent_header *eh;
1470 int depth = ext_depth(inode);
1471 struct ext4_extent *ex;
1472 __le32 border;
1473 int k, err = 0;
1474
1475 eh = path[depth].p_hdr;
1476 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001477
1478 if (unlikely(ex == NULL || eh == NULL)) {
1479 EXT4_ERROR_INODE(inode,
1480 "ex %p == NULL or eh %p == NULL", ex, eh);
1481 return -EIO;
1482 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001483
1484 if (depth == 0) {
1485 /* there is no tree at all */
1486 return 0;
1487 }
1488
1489 if (ex != EXT_FIRST_EXTENT(eh)) {
1490 /* we correct tree if first leaf got modified only */
1491 return 0;
1492 }
1493
1494 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001495 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001496 */
1497 k = depth - 1;
1498 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001499 err = ext4_ext_get_access(handle, inode, path + k);
1500 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001501 return err;
1502 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001503 err = ext4_ext_dirty(handle, inode, path + k);
1504 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001505 return err;
1506
1507 while (k--) {
1508 /* change all left-side indexes */
1509 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1510 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001511 err = ext4_ext_get_access(handle, inode, path + k);
1512 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001513 break;
1514 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001515 err = ext4_ext_dirty(handle, inode, path + k);
1516 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001517 break;
1518 }
1519
1520 return err;
1521}
1522
Akira Fujita748de672009-06-17 19:24:03 -04001523int
Alex Tomasa86c6182006-10-11 01:21:03 -07001524ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1525 struct ext4_extent *ex2)
1526{
Amit Arora749269f2007-07-18 09:02:56 -04001527 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001528
1529 /*
1530 * Make sure that either both extents are uninitialized, or
1531 * both are _not_.
1532 */
1533 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1534 return 0;
1535
Amit Arora749269f2007-07-18 09:02:56 -04001536 if (ext4_ext_is_uninitialized(ex1))
1537 max_len = EXT_UNINIT_MAX_LEN;
1538 else
1539 max_len = EXT_INIT_MAX_LEN;
1540
Amit Aroraa2df2a62007-07-17 21:42:41 -04001541 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1542 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1543
1544 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001545 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001546 return 0;
1547
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001548 /*
1549 * To allow future support for preallocated extents to be added
1550 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001551 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001552 */
Amit Arora749269f2007-07-18 09:02:56 -04001553 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001554 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001555#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001556 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001557 return 0;
1558#endif
1559
Theodore Ts'obf89d162010-10-27 21:30:14 -04001560 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001561 return 1;
1562 return 0;
1563}
1564
1565/*
Amit Arora56055d32007-07-17 21:42:38 -04001566 * This function tries to merge the "ex" extent to the next extent in the tree.
1567 * It always tries to merge towards right. If you want to merge towards
1568 * left, pass "ex - 1" as argument instead of "ex".
1569 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1570 * 1 if they got merged.
1571 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001572static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001573 struct ext4_ext_path *path,
1574 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001575{
1576 struct ext4_extent_header *eh;
1577 unsigned int depth, len;
1578 int merge_done = 0;
1579 int uninitialized = 0;
1580
1581 depth = ext_depth(inode);
1582 BUG_ON(path[depth].p_hdr == NULL);
1583 eh = path[depth].p_hdr;
1584
1585 while (ex < EXT_LAST_EXTENT(eh)) {
1586 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1587 break;
1588 /* merge with next extent! */
1589 if (ext4_ext_is_uninitialized(ex))
1590 uninitialized = 1;
1591 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1592 + ext4_ext_get_actual_len(ex + 1));
1593 if (uninitialized)
1594 ext4_ext_mark_uninitialized(ex);
1595
1596 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1597 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1598 * sizeof(struct ext4_extent);
1599 memmove(ex + 1, ex + 2, len);
1600 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001601 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001602 merge_done = 1;
1603 WARN_ON(eh->eh_entries == 0);
1604 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001605 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001606 }
1607
1608 return merge_done;
1609}
1610
1611/*
Yongqiang Yang197217a2011-05-03 11:45:29 -04001612 * This function tries to merge the @ex extent to neighbours in the tree.
1613 * return 1 if merge left else 0.
1614 */
1615static int ext4_ext_try_to_merge(struct inode *inode,
1616 struct ext4_ext_path *path,
1617 struct ext4_extent *ex) {
1618 struct ext4_extent_header *eh;
1619 unsigned int depth;
1620 int merge_done = 0;
1621 int ret = 0;
1622
1623 depth = ext_depth(inode);
1624 BUG_ON(path[depth].p_hdr == NULL);
1625 eh = path[depth].p_hdr;
1626
1627 if (ex > EXT_FIRST_EXTENT(eh))
1628 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1629
1630 if (!merge_done)
1631 ret = ext4_ext_try_to_merge_right(inode, path, ex);
1632
1633 return ret;
1634}
1635
1636/*
Amit Arora25d14f92007-05-24 13:04:13 -04001637 * check if a portion of the "newext" extent overlaps with an
1638 * existing extent.
1639 *
1640 * If there is an overlap discovered, it updates the length of the newext
1641 * such that there will be no overlap, and then returns 1.
1642 * If there is no overlap found, it returns 0.
1643 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001644static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1645 struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001646 struct ext4_extent *newext,
1647 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001648{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001649 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001650 unsigned int depth, len1;
1651 unsigned int ret = 0;
1652
1653 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001654 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001655 depth = ext_depth(inode);
1656 if (!path[depth].p_ext)
1657 goto out;
1658 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001659 b2 &= ~(sbi->s_cluster_ratio - 1);
Amit Arora25d14f92007-05-24 13:04:13 -04001660
1661 /*
1662 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001663 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001664 */
1665 if (b2 < b1) {
1666 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001667 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001668 goto out;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001669 b2 &= ~(sbi->s_cluster_ratio - 1);
Amit Arora25d14f92007-05-24 13:04:13 -04001670 }
1671
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001672 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001673 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001674 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001675 newext->ee_len = cpu_to_le16(len1);
1676 ret = 1;
1677 }
1678
1679 /* check for overlap */
1680 if (b1 + len1 > b2) {
1681 newext->ee_len = cpu_to_le16(b2 - b1);
1682 ret = 1;
1683 }
1684out:
1685 return ret;
1686}
1687
1688/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001689 * ext4_ext_insert_extent:
1690 * tries to merge requsted extent into the existing extent or
1691 * inserts requested extent as new one into the tree,
1692 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001693 */
1694int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1695 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04001696 struct ext4_extent *newext, int flag)
Alex Tomasa86c6182006-10-11 01:21:03 -07001697{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001698 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001699 struct ext4_extent *ex, *fex;
1700 struct ext4_extent *nearex; /* nearest extent */
1701 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001702 int depth, len, err;
1703 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001704 unsigned uninitialized = 0;
Allison Henderson55f020d2011-05-25 07:41:26 -04001705 int flags = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001706
Frank Mayhar273df552010-03-02 11:46:09 -05001707 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1708 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1709 return -EIO;
1710 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001711 depth = ext_depth(inode);
1712 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001713 if (unlikely(path[depth].p_hdr == NULL)) {
1714 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1715 return -EIO;
1716 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001717
1718 /* try to insert block into found extent and return */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001719 if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
Mingming Cao00314622009-09-28 15:49:08 -04001720 && ext4_can_extents_be_merged(inode, ex, newext)) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04001721 ext_debug("append [%d]%d block to %u:[%d]%d (from %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04001722 ext4_ext_is_uninitialized(newext),
1723 ext4_ext_get_actual_len(newext),
1724 le32_to_cpu(ex->ee_block),
1725 ext4_ext_is_uninitialized(ex),
1726 ext4_ext_get_actual_len(ex),
1727 ext4_ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001728 err = ext4_ext_get_access(handle, inode, path + depth);
1729 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001730 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001731
1732 /*
1733 * ext4_can_extents_be_merged should have checked that either
1734 * both extents are uninitialized, or both aren't. Thus we
1735 * need to check only one of them here.
1736 */
1737 if (ext4_ext_is_uninitialized(ex))
1738 uninitialized = 1;
1739 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1740 + ext4_ext_get_actual_len(newext));
1741 if (uninitialized)
1742 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001743 eh = path[depth].p_hdr;
1744 nearex = ex;
1745 goto merge;
1746 }
1747
Alex Tomasa86c6182006-10-11 01:21:03 -07001748 depth = ext_depth(inode);
1749 eh = path[depth].p_hdr;
1750 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1751 goto has_space;
1752
1753 /* probably next leaf has space for us? */
1754 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04001755 next = EXT_MAX_BLOCKS;
1756 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
Robin Dong57187892011-07-23 21:49:07 -04001757 next = ext4_ext_next_leaf_block(path);
Robin Dong598dbdf2011-07-11 18:24:01 -04001758 if (next != EXT_MAX_BLOCKS) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04001759 ext_debug("next leaf block - %u\n", next);
Alex Tomasa86c6182006-10-11 01:21:03 -07001760 BUG_ON(npath != NULL);
1761 npath = ext4_ext_find_extent(inode, next, NULL);
1762 if (IS_ERR(npath))
1763 return PTR_ERR(npath);
1764 BUG_ON(npath->p_depth != path->p_depth);
1765 eh = npath[depth].p_hdr;
1766 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001767 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001768 le16_to_cpu(eh->eh_entries));
1769 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04001770 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07001771 }
1772 ext_debug("next leaf has no free space(%d,%d)\n",
1773 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1774 }
1775
1776 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001777 * There is no free space in the found leaf.
1778 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001779 */
Allison Henderson55f020d2011-05-25 07:41:26 -04001780 if (flag & EXT4_GET_BLOCKS_PUNCH_OUT_EXT)
1781 flags = EXT4_MB_USE_ROOT_BLOCKS;
1782 err = ext4_ext_create_new_leaf(handle, inode, flags, path, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001783 if (err)
1784 goto cleanup;
1785 depth = ext_depth(inode);
1786 eh = path[depth].p_hdr;
1787
1788has_space:
1789 nearex = path[depth].p_ext;
1790
Avantika Mathur7e028972006-12-06 20:41:33 -08001791 err = ext4_ext_get_access(handle, inode, path + depth);
1792 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001793 goto cleanup;
1794
1795 if (!nearex) {
1796 /* there is no extent in this leaf, create first one */
Yongqiang Yang32de6752011-11-01 18:56:41 -04001797 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001798 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001799 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001800 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001801 ext4_ext_get_actual_len(newext));
Eric Gouriou80e675f2011-10-27 11:52:18 -04001802 nearex = EXT_FIRST_EXTENT(eh);
1803 } else {
1804 if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001805 > le32_to_cpu(nearex->ee_block)) {
Eric Gouriou80e675f2011-10-27 11:52:18 -04001806 /* Insert after */
Yongqiang Yang32de6752011-11-01 18:56:41 -04001807 ext_debug("insert %u:%llu:[%d]%d before: "
1808 "nearest %p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001809 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001810 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001811 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001812 ext4_ext_get_actual_len(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04001813 nearex);
1814 nearex++;
1815 } else {
1816 /* Insert before */
1817 BUG_ON(newext->ee_block == nearex->ee_block);
Yongqiang Yang32de6752011-11-01 18:56:41 -04001818 ext_debug("insert %u:%llu:[%d]%d after: "
1819 "nearest %p\n",
Eric Gouriou80e675f2011-10-27 11:52:18 -04001820 le32_to_cpu(newext->ee_block),
1821 ext4_ext_pblock(newext),
1822 ext4_ext_is_uninitialized(newext),
1823 ext4_ext_get_actual_len(newext),
1824 nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001825 }
Eric Gouriou80e675f2011-10-27 11:52:18 -04001826 len = EXT_LAST_EXTENT(eh) - nearex + 1;
1827 if (len > 0) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04001828 ext_debug("insert %u:%llu:[%d]%d: "
Eric Gouriou80e675f2011-10-27 11:52:18 -04001829 "move %d extents from 0x%p to 0x%p\n",
1830 le32_to_cpu(newext->ee_block),
1831 ext4_ext_pblock(newext),
1832 ext4_ext_is_uninitialized(newext),
1833 ext4_ext_get_actual_len(newext),
1834 len, nearex, nearex + 1);
1835 memmove(nearex + 1, nearex,
1836 len * sizeof(struct ext4_extent));
1837 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001838 }
1839
Marcin Slusarze8546d02008-04-17 10:38:59 -04001840 le16_add_cpu(&eh->eh_entries, 1);
Eric Gouriou80e675f2011-10-27 11:52:18 -04001841 path[depth].p_ext = nearex;
Alex Tomasa86c6182006-10-11 01:21:03 -07001842 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001843 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001844 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001845
1846merge:
1847 /* try to merge extents to the right */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001848 if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
Mingming Cao00314622009-09-28 15:49:08 -04001849 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001850
1851 /* try to merge extents to the left */
1852
1853 /* time to correct all indexes above */
1854 err = ext4_ext_correct_indexes(handle, inode, path);
1855 if (err)
1856 goto cleanup;
1857
1858 err = ext4_ext_dirty(handle, inode, path + depth);
1859
1860cleanup:
1861 if (npath) {
1862 ext4_ext_drop_refs(npath);
1863 kfree(npath);
1864 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001865 ext4_ext_invalidate_cache(inode);
1866 return err;
1867}
1868
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001869static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1870 ext4_lblk_t num, ext_prepare_callback func,
1871 void *cbdata)
Eric Sandeen6873fa02008-10-07 00:46:36 -04001872{
1873 struct ext4_ext_path *path = NULL;
1874 struct ext4_ext_cache cbex;
1875 struct ext4_extent *ex;
1876 ext4_lblk_t next, start = 0, end = 0;
1877 ext4_lblk_t last = block + num;
1878 int depth, exists, err = 0;
1879
1880 BUG_ON(func == NULL);
1881 BUG_ON(inode == NULL);
1882
Lukas Czernerf17722f2011-06-06 00:05:17 -04001883 while (block < last && block != EXT_MAX_BLOCKS) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04001884 num = last - block;
1885 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001886 down_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001887 path = ext4_ext_find_extent(inode, block, path);
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001888 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001889 if (IS_ERR(path)) {
1890 err = PTR_ERR(path);
1891 path = NULL;
1892 break;
1893 }
1894
1895 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05001896 if (unlikely(path[depth].p_hdr == NULL)) {
1897 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1898 err = -EIO;
1899 break;
1900 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04001901 ex = path[depth].p_ext;
1902 next = ext4_ext_next_allocated_block(path);
1903
1904 exists = 0;
1905 if (!ex) {
1906 /* there is no extent yet, so try to allocate
1907 * all requested space */
1908 start = block;
1909 end = block + num;
1910 } else if (le32_to_cpu(ex->ee_block) > block) {
1911 /* need to allocate space before found extent */
1912 start = block;
1913 end = le32_to_cpu(ex->ee_block);
1914 if (block + num < end)
1915 end = block + num;
1916 } else if (block >= le32_to_cpu(ex->ee_block)
1917 + ext4_ext_get_actual_len(ex)) {
1918 /* need to allocate space after found extent */
1919 start = block;
1920 end = block + num;
1921 if (end >= next)
1922 end = next;
1923 } else if (block >= le32_to_cpu(ex->ee_block)) {
1924 /*
1925 * some part of requested space is covered
1926 * by found extent
1927 */
1928 start = block;
1929 end = le32_to_cpu(ex->ee_block)
1930 + ext4_ext_get_actual_len(ex);
1931 if (block + num < end)
1932 end = block + num;
1933 exists = 1;
1934 } else {
1935 BUG();
1936 }
1937 BUG_ON(end <= start);
1938
1939 if (!exists) {
1940 cbex.ec_block = start;
1941 cbex.ec_len = end - start;
1942 cbex.ec_start = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04001943 } else {
1944 cbex.ec_block = le32_to_cpu(ex->ee_block);
1945 cbex.ec_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001946 cbex.ec_start = ext4_ext_pblock(ex);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001947 }
1948
Frank Mayhar273df552010-03-02 11:46:09 -05001949 if (unlikely(cbex.ec_len == 0)) {
1950 EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
1951 err = -EIO;
1952 break;
1953 }
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04001954 err = func(inode, next, &cbex, ex, cbdata);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001955 ext4_ext_drop_refs(path);
1956
1957 if (err < 0)
1958 break;
1959
1960 if (err == EXT_REPEAT)
1961 continue;
1962 else if (err == EXT_BREAK) {
1963 err = 0;
1964 break;
1965 }
1966
1967 if (ext_depth(inode) != depth) {
1968 /* depth was changed. we have to realloc path */
1969 kfree(path);
1970 path = NULL;
1971 }
1972
1973 block = cbex.ec_block + cbex.ec_len;
1974 }
1975
1976 if (path) {
1977 ext4_ext_drop_refs(path);
1978 kfree(path);
1979 }
1980
1981 return err;
1982}
1983
Avantika Mathur09b88252006-12-06 20:41:36 -08001984static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001985ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05001986 __u32 len, ext4_fsblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07001987{
1988 struct ext4_ext_cache *cex;
1989 BUG_ON(len == 0);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001990 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Aditya Kalid8990242011-09-09 19:18:51 -04001991 trace_ext4_ext_put_in_cache(inode, block, len, start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001992 cex = &EXT4_I(inode)->i_cached_extent;
Alex Tomasa86c6182006-10-11 01:21:03 -07001993 cex->ec_block = block;
1994 cex->ec_len = len;
1995 cex->ec_start = start;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001996 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001997}
1998
1999/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002000 * ext4_ext_put_gap_in_cache:
2001 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07002002 * and cache this gap
2003 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002004static void
Alex Tomasa86c6182006-10-11 01:21:03 -07002005ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002006 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07002007{
2008 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002009 unsigned long len;
2010 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07002011 struct ext4_extent *ex;
2012
2013 ex = path[depth].p_ext;
2014 if (ex == NULL) {
2015 /* there is no extent yet, so gap is [0;-] */
2016 lblock = 0;
Lukas Czernerf17722f2011-06-06 00:05:17 -04002017 len = EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07002018 ext_debug("cache gap(whole file):");
2019 } else if (block < le32_to_cpu(ex->ee_block)) {
2020 lblock = block;
2021 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05002022 ext_debug("cache gap(before): %u [%u:%u]",
2023 block,
2024 le32_to_cpu(ex->ee_block),
2025 ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002026 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002027 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002028 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002029 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002030 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002031
2032 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05002033 ext_debug("cache gap(after): [%u:%u] %u",
2034 le32_to_cpu(ex->ee_block),
2035 ext4_ext_get_actual_len(ex),
2036 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002037 BUG_ON(next == lblock);
2038 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07002039 } else {
2040 lblock = len = 0;
2041 BUG();
2042 }
2043
Eric Sandeenbba90742008-01-28 23:58:27 -05002044 ext_debug(" -> %u:%lu\n", lblock, len);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002045 ext4_ext_put_in_cache(inode, lblock, len, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002046}
2047
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002048/*
Robin Dongb7ca1e82011-07-23 21:53:25 -04002049 * ext4_ext_check_cache()
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002050 * Checks to see if the given block is in the cache.
2051 * If it is, the cached extent is stored in the given
2052 * cache extent pointer. If the cached extent is a hole,
2053 * this routine should be used instead of
2054 * ext4_ext_in_cache if the calling function needs to
2055 * know the size of the hole.
2056 *
2057 * @inode: The files inode
2058 * @block: The block to look for in the cache
2059 * @ex: Pointer where the cached extent will be stored
2060 * if it contains block
2061 *
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002062 * Return 0 if cache is invalid; 1 if the cache is valid
2063 */
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002064static int ext4_ext_check_cache(struct inode *inode, ext4_lblk_t block,
2065 struct ext4_ext_cache *ex){
Alex Tomasa86c6182006-10-11 01:21:03 -07002066 struct ext4_ext_cache *cex;
Vivek Haldar77f41352011-05-22 21:24:16 -04002067 struct ext4_sb_info *sbi;
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002068 int ret = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002069
Theodore Ts'o60e66792010-05-17 07:00:00 -04002070 /*
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002071 * We borrow i_block_reservation_lock to protect i_cached_extent
2072 */
2073 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07002074 cex = &EXT4_I(inode)->i_cached_extent;
Vivek Haldar77f41352011-05-22 21:24:16 -04002075 sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002076
2077 /* has cache valid data? */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002078 if (cex->ec_len == 0)
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002079 goto errout;
Alex Tomasa86c6182006-10-11 01:21:03 -07002080
Akinobu Mita731eb1a2010-03-03 23:55:01 -05002081 if (in_range(block, cex->ec_block, cex->ec_len)) {
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002082 memcpy(ex, cex, sizeof(struct ext4_ext_cache));
Eric Sandeenbba90742008-01-28 23:58:27 -05002083 ext_debug("%u cached by %u:%u:%llu\n",
2084 block,
2085 cex->ec_block, cex->ec_len, cex->ec_start);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002086 ret = 1;
Alex Tomasa86c6182006-10-11 01:21:03 -07002087 }
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002088errout:
Aditya Kalid8990242011-09-09 19:18:51 -04002089 trace_ext4_ext_in_cache(inode, block, ret);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002090 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
2091 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07002092}
2093
2094/*
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002095 * ext4_ext_in_cache()
2096 * Checks to see if the given block is in the cache.
2097 * If it is, the cached extent is stored in the given
2098 * extent pointer.
2099 *
2100 * @inode: The files inode
2101 * @block: The block to look for in the cache
2102 * @ex: Pointer where the cached extent will be stored
2103 * if it contains block
2104 *
2105 * Return 0 if cache is invalid; 1 if the cache is valid
2106 */
2107static int
2108ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
2109 struct ext4_extent *ex)
2110{
2111 struct ext4_ext_cache cex;
2112 int ret = 0;
2113
2114 if (ext4_ext_check_cache(inode, block, &cex)) {
2115 ex->ee_block = cpu_to_le32(cex.ec_block);
2116 ext4_ext_store_pblock(ex, cex.ec_start);
2117 ex->ee_len = cpu_to_le16(cex.ec_len);
2118 ret = 1;
2119 }
2120
2121 return ret;
2122}
2123
2124
2125/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002126 * ext4_ext_rm_idx:
2127 * removes index from the index block.
Alex Tomasa86c6182006-10-11 01:21:03 -07002128 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002129static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Forrest Liucb832982012-12-17 09:55:39 -05002130 struct ext4_ext_path *path, int depth)
Alex Tomasa86c6182006-10-11 01:21:03 -07002131{
Alex Tomasa86c6182006-10-11 01:21:03 -07002132 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002133 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002134
2135 /* free index block */
Forrest Liucb832982012-12-17 09:55:39 -05002136 depth--;
2137 path = path + depth;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002138 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002139 if (unlikely(path->p_hdr->eh_entries == 0)) {
2140 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2141 return -EIO;
2142 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002143 err = ext4_ext_get_access(handle, inode, path);
2144 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002145 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002146
2147 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2148 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2149 len *= sizeof(struct ext4_extent_idx);
2150 memmove(path->p_idx, path->p_idx + 1, len);
2151 }
2152
Marcin Slusarze8546d02008-04-17 10:38:59 -04002153 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002154 err = ext4_ext_dirty(handle, inode, path);
2155 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002156 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002157 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Aditya Kalid8990242011-09-09 19:18:51 -04002158 trace_ext4_ext_rm_idx(inode, leaf);
2159
Peter Huewe7dc57612011-02-21 21:01:42 -05002160 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002161 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Forrest Liucb832982012-12-17 09:55:39 -05002162
2163 while (--depth >= 0) {
2164 if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr))
2165 break;
2166 path--;
2167 err = ext4_ext_get_access(handle, inode, path);
2168 if (err)
2169 break;
2170 path->p_idx->ei_block = (path+1)->p_idx->ei_block;
2171 err = ext4_ext_dirty(handle, inode, path);
2172 if (err)
2173 break;
2174 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002175 return err;
2176}
2177
2178/*
Mingming Caoee12b632008-08-19 22:16:05 -04002179 * ext4_ext_calc_credits_for_single_extent:
2180 * This routine returns max. credits that needed to insert an extent
2181 * to the extent tree.
2182 * When pass the actual path, the caller should calculate credits
2183 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002184 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002185int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002186 struct ext4_ext_path *path)
2187{
Alex Tomasa86c6182006-10-11 01:21:03 -07002188 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002189 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002190 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002191
Alex Tomasa86c6182006-10-11 01:21:03 -07002192 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002193 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002194 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2195
2196 /*
2197 * There are some space in the leaf tree, no
2198 * need to account for leaf block credit
2199 *
2200 * bitmaps and block group descriptor blocks
Tao Madf3ab172011-10-08 15:53:49 -04002201 * and other metadata blocks still need to be
Mingming Caoee12b632008-08-19 22:16:05 -04002202 * accounted.
2203 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002204 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002205 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002206 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002207 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002208 }
2209
Mingming Cao525f4ed2008-08-19 22:15:58 -04002210 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002211}
Alex Tomasa86c6182006-10-11 01:21:03 -07002212
Mingming Caoee12b632008-08-19 22:16:05 -04002213/*
2214 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2215 *
2216 * if nrblocks are fit in a single extent (chunk flag is 1), then
2217 * in the worse case, each tree level index/leaf need to be changed
2218 * if the tree split due to insert a new extent, then the old tree
2219 * index/leaf need to be updated too
2220 *
2221 * If the nrblocks are discontiguous, they could cause
2222 * the whole tree split more than once, but this is really rare.
2223 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002224int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoee12b632008-08-19 22:16:05 -04002225{
2226 int index;
2227 int depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002228
Mingming Caoee12b632008-08-19 22:16:05 -04002229 if (chunk)
2230 index = depth * 2;
2231 else
2232 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002233
Mingming Caoee12b632008-08-19 22:16:05 -04002234 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002235}
2236
2237static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002238 struct ext4_extent *ex,
2239 ext4_fsblk_t *partial_cluster,
2240 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002241{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002242 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002243 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002244 ext4_fsblk_t pblk;
Theodore Ts'oe6362602009-11-23 07:17:05 -05002245 int flags = EXT4_FREE_BLOCKS_FORGET;
Alex Tomasa86c6182006-10-11 01:21:03 -07002246
Alex Tomasc9de5602008-01-29 00:19:52 -05002247 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
Theodore Ts'oe6362602009-11-23 07:17:05 -05002248 flags |= EXT4_FREE_BLOCKS_METADATA;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002249 /*
2250 * For bigalloc file systems, we never free a partial cluster
2251 * at the beginning of the extent. Instead, we make a note
2252 * that we tried freeing the cluster, and check to see if we
2253 * need to free it on a subsequent call to ext4_remove_blocks,
2254 * or at the end of the ext4_truncate() operation.
2255 */
2256 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
2257
Aditya Kalid8990242011-09-09 19:18:51 -04002258 trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002259 /*
2260 * If we have a partial cluster, and it's different from the
2261 * cluster of the last block, we need to explicitly free the
2262 * partial cluster here.
2263 */
2264 pblk = ext4_ext_pblock(ex) + ee_len - 1;
2265 if (*partial_cluster && (EXT4_B2C(sbi, pblk) != *partial_cluster)) {
2266 ext4_free_blocks(handle, inode, NULL,
2267 EXT4_C2B(sbi, *partial_cluster),
2268 sbi->s_cluster_ratio, flags);
2269 *partial_cluster = 0;
2270 }
2271
Alex Tomasa86c6182006-10-11 01:21:03 -07002272#ifdef EXTENTS_STATS
2273 {
2274 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002275 spin_lock(&sbi->s_ext_stats_lock);
2276 sbi->s_ext_blocks += ee_len;
2277 sbi->s_ext_extents++;
2278 if (ee_len < sbi->s_ext_min)
2279 sbi->s_ext_min = ee_len;
2280 if (ee_len > sbi->s_ext_max)
2281 sbi->s_ext_max = ee_len;
2282 if (ext_depth(inode) > sbi->s_depth_max)
2283 sbi->s_depth_max = ext_depth(inode);
2284 spin_unlock(&sbi->s_ext_stats_lock);
2285 }
2286#endif
2287 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002288 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002289 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002290 ext4_lblk_t num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002291
Amit Aroraa2df2a62007-07-17 21:42:41 -04002292 num = le32_to_cpu(ex->ee_block) + ee_len - from;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002293 pblk = ext4_ext_pblock(ex) + ee_len - num;
2294 ext_debug("free last %u blocks starting %llu\n", num, pblk);
2295 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
2296 /*
2297 * If the block range to be freed didn't start at the
2298 * beginning of a cluster, and we removed the entire
2299 * extent, save the partial cluster here, since we
2300 * might need to delete if we determine that the
2301 * truncate operation has removed all of the blocks in
2302 * the cluster.
2303 */
2304 if (pblk & (sbi->s_cluster_ratio - 1) &&
2305 (ee_len == num))
2306 *partial_cluster = EXT4_B2C(sbi, pblk);
2307 else
2308 *partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002309 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002310 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002311 /* head removal */
2312 ext4_lblk_t num;
2313 ext4_fsblk_t start;
2314
2315 num = to - from;
2316 start = ext4_ext_pblock(ex);
2317
2318 ext_debug("free first %u blocks starting %llu\n", num, start);
H Hartley Sweetenee90d572011-10-18 11:01:51 -04002319 ext4_free_blocks(handle, inode, NULL, start, num, flags);
Allison Hendersond583fb82011-05-25 07:41:43 -04002320
Alex Tomasa86c6182006-10-11 01:21:03 -07002321 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002322 printk(KERN_INFO "strange request: removal(2) "
2323 "%u-%u from %u:%u\n",
2324 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002325 }
2326 return 0;
2327}
2328
Allison Hendersond583fb82011-05-25 07:41:43 -04002329
2330/*
2331 * ext4_ext_rm_leaf() Removes the extents associated with the
2332 * blocks appearing between "start" and "end", and splits the extents
2333 * if "start" and "end" appear in the same extent
2334 *
2335 * @handle: The journal handle
2336 * @inode: The files inode
2337 * @path: The path to the leaf
2338 * @start: The first block to remove
2339 * @end: The last block to remove
2340 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002341static int
2342ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002343 struct ext4_ext_path *path, ext4_fsblk_t *partial_cluster,
2344 ext4_lblk_t start, ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002345{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002346 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002347 int err = 0, correct_index = 0;
2348 int depth = ext_depth(inode), credits;
2349 struct ext4_extent_header *eh;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002350 ext4_lblk_t a, b;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002351 unsigned num;
2352 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002353 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002354 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002355 struct ext4_extent *ex;
2356
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002357 /* the header must be checked already in ext4_ext_remove_space() */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002358 ext_debug("truncate since %u in leaf to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002359 if (!path[depth].p_hdr)
2360 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2361 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002362 if (unlikely(path[depth].p_hdr == NULL)) {
2363 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2364 return -EIO;
2365 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002366 /* find where to start removing */
2367 ex = EXT_LAST_EXTENT(eh);
2368
2369 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002370 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002371
Aditya Kalid8990242011-09-09 19:18:51 -04002372 trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster);
2373
Alex Tomasa86c6182006-10-11 01:21:03 -07002374 while (ex >= EXT_FIRST_EXTENT(eh) &&
2375 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002376
2377 if (ext4_ext_is_uninitialized(ex))
2378 uninitialized = 1;
2379 else
2380 uninitialized = 0;
2381
Mingming553f9002009-09-18 13:34:55 -04002382 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2383 uninitialized, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002384 path[depth].p_ext = ex;
2385
2386 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002387 b = ex_ee_block+ex_ee_len - 1 < end ?
2388 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002389
2390 ext_debug(" border %u:%u\n", a, b);
2391
Allison Hendersond583fb82011-05-25 07:41:43 -04002392 /* If this extent is beyond the end of the hole, skip it */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002393 if (end < ex_ee_block) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002394 ex--;
2395 ex_ee_block = le32_to_cpu(ex->ee_block);
2396 ex_ee_len = ext4_ext_get_actual_len(ex);
2397 continue;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002398 } else if (b != ex_ee_block + ex_ee_len - 1) {
Lukas Czernerdc1841d2012-03-19 23:07:43 -04002399 EXT4_ERROR_INODE(inode,
2400 "can not handle truncate %u:%u "
2401 "on extent %u:%u",
2402 start, end, ex_ee_block,
2403 ex_ee_block + ex_ee_len - 1);
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002404 err = -EIO;
2405 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002406 } else if (a != ex_ee_block) {
2407 /* remove tail of the extent */
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002408 num = a - ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002409 } else {
2410 /* remove whole extent: excellent! */
Alex Tomasa86c6182006-10-11 01:21:03 -07002411 num = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002412 }
Theodore Ts'o34071da2008-08-01 21:59:19 -04002413 /*
2414 * 3 for leaf, sb, and inode plus 2 (bmap and group
2415 * descriptor) for each block group; assume two block
2416 * groups plus ex_ee_len/blocks_per_block_group for
2417 * the worst case
2418 */
2419 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002420 if (ex == EXT_FIRST_EXTENT(eh)) {
2421 correct_index = 1;
2422 credits += (ext_depth(inode)) + 1;
2423 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002424 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002425
Jan Kara487caee2009-08-17 22:17:20 -04002426 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
Shen Feng9102e4f2008-07-11 19:27:31 -04002427 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002428 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002429
2430 err = ext4_ext_get_access(handle, inode, path + depth);
2431 if (err)
2432 goto out;
2433
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002434 err = ext4_remove_blocks(handle, inode, ex, partial_cluster,
2435 a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002436 if (err)
2437 goto out;
2438
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002439 if (num == 0)
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002440 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002441 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002442
Alex Tomasa86c6182006-10-11 01:21:03 -07002443 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002444 /*
2445 * Do not mark uninitialized if all the blocks in the
2446 * extent have been removed.
2447 */
2448 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002449 ext4_ext_mark_uninitialized(ex);
Allison Hendersond583fb82011-05-25 07:41:43 -04002450 /*
2451 * If the extent was completely released,
2452 * we need to remove it from the leaf
2453 */
2454 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002455 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002456 /*
2457 * For hole punching, we need to scoot all the
2458 * extents up when an extent is removed so that
2459 * we dont have blank extents in the middle
2460 */
2461 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2462 sizeof(struct ext4_extent));
2463
2464 /* Now get rid of the one at the end */
2465 memset(EXT_LAST_EXTENT(eh), 0,
2466 sizeof(struct ext4_extent));
2467 }
2468 le16_add_cpu(&eh->eh_entries, -1);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002469 } else
2470 *partial_cluster = 0;
Allison Hendersond583fb82011-05-25 07:41:43 -04002471
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002472 err = ext4_ext_dirty(handle, inode, path + depth);
2473 if (err)
2474 goto out;
2475
Yongqiang Yangbf52c6f2011-11-01 18:59:26 -04002476 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002477 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002478 ex--;
2479 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002480 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002481 }
2482
2483 if (correct_index && eh->eh_entries)
2484 err = ext4_ext_correct_indexes(handle, inode, path);
2485
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002486 /*
2487 * If there is still a entry in the leaf node, check to see if
2488 * it references the partial cluster. This is the only place
2489 * where it could; if it doesn't, we can free the cluster.
2490 */
2491 if (*partial_cluster && ex >= EXT_FIRST_EXTENT(eh) &&
2492 (EXT4_B2C(sbi, ext4_ext_pblock(ex) + ex_ee_len - 1) !=
2493 *partial_cluster)) {
2494 int flags = EXT4_FREE_BLOCKS_FORGET;
2495
2496 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2497 flags |= EXT4_FREE_BLOCKS_METADATA;
2498
2499 ext4_free_blocks(handle, inode, NULL,
2500 EXT4_C2B(sbi, *partial_cluster),
2501 sbi->s_cluster_ratio, flags);
2502 *partial_cluster = 0;
2503 }
2504
Alex Tomasa86c6182006-10-11 01:21:03 -07002505 /* if this leaf is free, then we should
2506 * remove it from index block above */
2507 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
Forrest Liucb832982012-12-17 09:55:39 -05002508 err = ext4_ext_rm_idx(handle, inode, path, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002509
2510out:
2511 return err;
2512}
2513
2514/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002515 * ext4_ext_more_to_rm:
2516 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002517 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002518static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002519ext4_ext_more_to_rm(struct ext4_ext_path *path)
2520{
2521 BUG_ON(path->p_idx == NULL);
2522
2523 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2524 return 0;
2525
2526 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002527 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002528 * so we have to consider current index for truncation
2529 */
2530 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2531 return 0;
2532 return 1;
2533}
2534
Lukas Czerner5f95d212012-03-19 23:03:19 -04002535static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2536 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002537{
2538 struct super_block *sb = inode->i_sb;
2539 int depth = ext_depth(inode);
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002540 struct ext4_ext_path *path = NULL;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002541 ext4_fsblk_t partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002542 handle_t *handle;
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002543 int i = 0, err;
Alex Tomasa86c6182006-10-11 01:21:03 -07002544
Lukas Czerner5f95d212012-03-19 23:03:19 -04002545 ext_debug("truncate since %u to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002546
2547 /* probably first extent we're gonna free will be last in block */
2548 handle = ext4_journal_start(inode, depth + 1);
2549 if (IS_ERR(handle))
2550 return PTR_ERR(handle);
2551
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002552again:
Alex Tomasa86c6182006-10-11 01:21:03 -07002553 ext4_ext_invalidate_cache(inode);
2554
Aditya Kalid8990242011-09-09 19:18:51 -04002555 trace_ext4_ext_remove_space(inode, start, depth);
2556
Alex Tomasa86c6182006-10-11 01:21:03 -07002557 /*
Lukas Czerner5f95d212012-03-19 23:03:19 -04002558 * Check if we are removing extents inside the extent tree. If that
2559 * is the case, we are going to punch a hole inside the extent tree
2560 * so we have to check whether we need to split the extent covering
2561 * the last block to remove so we can easily remove the part of it
2562 * in ext4_ext_rm_leaf().
2563 */
2564 if (end < EXT_MAX_BLOCKS - 1) {
2565 struct ext4_extent *ex;
2566 ext4_lblk_t ee_block;
2567
2568 /* find extent for this block */
2569 path = ext4_ext_find_extent(inode, end, NULL);
2570 if (IS_ERR(path)) {
2571 ext4_journal_stop(handle);
2572 return PTR_ERR(path);
2573 }
2574 depth = ext_depth(inode);
2575 ex = path[depth].p_ext;
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002576 if (!ex) {
2577 ext4_ext_drop_refs(path);
2578 kfree(path);
2579 path = NULL;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002580 goto cont;
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002581 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002582
2583 ee_block = le32_to_cpu(ex->ee_block);
2584
2585 /*
2586 * See if the last block is inside the extent, if so split
2587 * the extent at 'end' block so we can easily remove the
2588 * tail of the first part of the split extent in
2589 * ext4_ext_rm_leaf().
2590 */
2591 if (end >= ee_block &&
2592 end < ee_block + ext4_ext_get_actual_len(ex) - 1) {
2593 int split_flag = 0;
2594
2595 if (ext4_ext_is_uninitialized(ex))
2596 split_flag = EXT4_EXT_MARK_UNINIT1 |
2597 EXT4_EXT_MARK_UNINIT2;
2598
2599 /*
2600 * Split the extent in two so that 'end' is the last
2601 * block in the first new extent
2602 */
2603 err = ext4_split_extent_at(handle, inode, path,
2604 end + 1, split_flag,
2605 EXT4_GET_BLOCKS_PRE_IO |
2606 EXT4_GET_BLOCKS_PUNCH_OUT_EXT);
2607
2608 if (err < 0)
2609 goto out;
2610 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002611 }
2612cont:
2613
2614 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002615 * We start scanning from right side, freeing all the blocks
2616 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002617 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002618 depth = ext_depth(inode);
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002619 if (path) {
2620 int k = i = depth;
2621 while (--k > 0)
2622 path[k].p_block =
2623 le16_to_cpu(path[k].p_hdr->eh_entries)+1;
2624 } else {
2625 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1),
2626 GFP_NOFS);
2627 if (path == NULL) {
2628 ext4_journal_stop(handle);
2629 return -ENOMEM;
2630 }
2631 path[0].p_depth = depth;
2632 path[0].p_hdr = ext_inode_hdr(inode);
Theodore Ts'od8b868f2012-08-17 08:54:52 -04002633 i = 0;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002634
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002635 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
2636 err = -EIO;
2637 goto out;
2638 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002639 }
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002640 err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002641
2642 while (i >= 0 && err == 0) {
2643 if (i == depth) {
2644 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04002645 err = ext4_ext_rm_leaf(handle, inode, path,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002646 &partial_cluster, start,
Lukas Czerner5f95d212012-03-19 23:03:19 -04002647 end);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002648 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002649 brelse(path[i].p_bh);
2650 path[i].p_bh = NULL;
2651 i--;
2652 continue;
2653 }
2654
2655 /* this is index block */
2656 if (!path[i].p_hdr) {
2657 ext_debug("initialize header\n");
2658 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002659 }
2660
Alex Tomasa86c6182006-10-11 01:21:03 -07002661 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002662 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002663 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2664 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2665 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2666 path[i].p_hdr,
2667 le16_to_cpu(path[i].p_hdr->eh_entries));
2668 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002669 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002670 path[i].p_idx--;
2671 }
2672
2673 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2674 i, EXT_FIRST_INDEX(path[i].p_hdr),
2675 path[i].p_idx);
2676 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002677 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002678 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002679 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04002680 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002681 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'obf89d162010-10-27 21:30:14 -04002682 bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002683 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002684 /* should we reset i_size? */
2685 err = -EIO;
2686 break;
2687 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002688 if (WARN_ON(i + 1 > depth)) {
2689 err = -EIO;
2690 break;
2691 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002692 if (ext4_ext_check(inode, ext_block_hdr(bh),
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002693 depth - i - 1)) {
2694 err = -EIO;
2695 break;
2696 }
2697 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002698
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002699 /* save actual number of indexes since this
2700 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002701 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2702 i++;
2703 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002704 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002705 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002706 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002707 * handle must be already prepared by the
2708 * truncatei_leaf() */
Forrest Liucb832982012-12-17 09:55:39 -05002709 err = ext4_ext_rm_idx(handle, inode, path, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07002710 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002711 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002712 brelse(path[i].p_bh);
2713 path[i].p_bh = NULL;
2714 i--;
2715 ext_debug("return to level %d\n", i);
2716 }
2717 }
2718
Aditya Kalid8990242011-09-09 19:18:51 -04002719 trace_ext4_ext_remove_space_done(inode, start, depth, partial_cluster,
2720 path->p_hdr->eh_entries);
2721
Aditya Kali7b415bf2011-09-09 19:04:51 -04002722 /* If we still have something in the partial cluster and we have removed
2723 * even the first extent, then we should free the blocks in the partial
2724 * cluster as well. */
2725 if (partial_cluster && path->p_hdr->eh_entries == 0) {
2726 int flags = EXT4_FREE_BLOCKS_FORGET;
2727
2728 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2729 flags |= EXT4_FREE_BLOCKS_METADATA;
2730
2731 ext4_free_blocks(handle, inode, NULL,
2732 EXT4_C2B(EXT4_SB(sb), partial_cluster),
2733 EXT4_SB(sb)->s_cluster_ratio, flags);
2734 partial_cluster = 0;
2735 }
2736
Alex Tomasa86c6182006-10-11 01:21:03 -07002737 /* TODO: flexible tree reduction should be here */
2738 if (path->p_hdr->eh_entries == 0) {
2739 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002740 * truncate to zero freed all the tree,
2741 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002742 */
2743 err = ext4_ext_get_access(handle, inode, path);
2744 if (err == 0) {
2745 ext_inode_hdr(inode)->eh_depth = 0;
2746 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04002747 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07002748 err = ext4_ext_dirty(handle, inode, path);
2749 }
2750 }
2751out:
Alex Tomasa86c6182006-10-11 01:21:03 -07002752 ext4_ext_drop_refs(path);
2753 kfree(path);
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002754 if (err == -EAGAIN) {
2755 path = NULL;
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002756 goto again;
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002757 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002758 ext4_journal_stop(handle);
2759
2760 return err;
2761}
2762
2763/*
2764 * called at mount time
2765 */
2766void ext4_ext_init(struct super_block *sb)
2767{
2768 /*
2769 * possible initialization would be here
2770 */
2771
Theodore Ts'o83982b62009-01-06 14:53:16 -05002772 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04002773#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o92b97812012-03-19 23:41:49 -04002774 printk(KERN_INFO "EXT4-fs: file extents enabled"
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002775#ifdef AGGRESSIVE_TEST
Theodore Ts'o92b97812012-03-19 23:41:49 -04002776 ", aggressive tests"
Alex Tomasa86c6182006-10-11 01:21:03 -07002777#endif
2778#ifdef CHECK_BINSEARCH
Theodore Ts'o92b97812012-03-19 23:41:49 -04002779 ", check binsearch"
Alex Tomasa86c6182006-10-11 01:21:03 -07002780#endif
2781#ifdef EXTENTS_STATS
Theodore Ts'o92b97812012-03-19 23:41:49 -04002782 ", stats"
Alex Tomasa86c6182006-10-11 01:21:03 -07002783#endif
Theodore Ts'o92b97812012-03-19 23:41:49 -04002784 "\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04002785#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07002786#ifdef EXTENTS_STATS
2787 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2788 EXT4_SB(sb)->s_ext_min = 1 << 30;
2789 EXT4_SB(sb)->s_ext_max = 0;
2790#endif
2791 }
2792}
2793
2794/*
2795 * called at umount time
2796 */
2797void ext4_ext_release(struct super_block *sb)
2798{
Theodore Ts'o83982b62009-01-06 14:53:16 -05002799 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
Alex Tomasa86c6182006-10-11 01:21:03 -07002800 return;
2801
2802#ifdef EXTENTS_STATS
2803 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2804 struct ext4_sb_info *sbi = EXT4_SB(sb);
2805 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2806 sbi->s_ext_blocks, sbi->s_ext_extents,
2807 sbi->s_ext_blocks / sbi->s_ext_extents);
2808 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2809 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2810 }
2811#endif
2812}
2813
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002814/* FIXME!! we need to try to merge to left or right after zero-out */
2815static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2816{
Lukas Czerner24075182010-10-27 21:30:06 -04002817 ext4_fsblk_t ee_pblock;
2818 unsigned int ee_len;
Jing Zhangb7203032010-05-12 00:00:00 -04002819 int ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002820
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002821 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04002822 ee_pblock = ext4_ext_pblock(ex);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002823
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04002824 ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
Lukas Czerner24075182010-10-27 21:30:06 -04002825 if (ret > 0)
2826 ret = 0;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002827
Lukas Czerner24075182010-10-27 21:30:06 -04002828 return ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002829}
2830
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002831/*
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002832 * ext4_split_extent_at() splits an extent at given block.
2833 *
2834 * @handle: the journal handle
2835 * @inode: the file inode
2836 * @path: the path to the extent
2837 * @split: the logical block where the extent is splitted.
2838 * @split_flags: indicates if the extent could be zeroout if split fails, and
2839 * the states(init or uninit) of new extents.
2840 * @flags: flags used to insert new extent to extent tree.
2841 *
2842 *
2843 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
2844 * of which are deterimined by split_flag.
2845 *
2846 * There are two cases:
2847 * a> the extent are splitted into two extent.
2848 * b> split is not needed, and just mark the extent.
2849 *
2850 * return 0 on success.
2851 */
2852static int ext4_split_extent_at(handle_t *handle,
2853 struct inode *inode,
2854 struct ext4_ext_path *path,
2855 ext4_lblk_t split,
2856 int split_flag,
2857 int flags)
2858{
2859 ext4_fsblk_t newblock;
2860 ext4_lblk_t ee_block;
2861 struct ext4_extent *ex, newex, orig_ex;
2862 struct ext4_extent *ex2 = NULL;
2863 unsigned int ee_len, depth;
2864 int err = 0;
2865
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04002866 BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) ==
2867 (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2));
2868
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002869 ext_debug("ext4_split_extents_at: inode %lu, logical"
2870 "block %llu\n", inode->i_ino, (unsigned long long)split);
2871
2872 ext4_ext_show_leaf(inode, path);
2873
2874 depth = ext_depth(inode);
2875 ex = path[depth].p_ext;
2876 ee_block = le32_to_cpu(ex->ee_block);
2877 ee_len = ext4_ext_get_actual_len(ex);
2878 newblock = split - ee_block + ext4_ext_pblock(ex);
2879
2880 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
2881
2882 err = ext4_ext_get_access(handle, inode, path + depth);
2883 if (err)
2884 goto out;
2885
2886 if (split == ee_block) {
2887 /*
2888 * case b: block @split is the block that the extent begins with
2889 * then we just change the state of the extent, and splitting
2890 * is not needed.
2891 */
2892 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2893 ext4_ext_mark_uninitialized(ex);
2894 else
2895 ext4_ext_mark_initialized(ex);
2896
2897 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
2898 ext4_ext_try_to_merge(inode, path, ex);
2899
2900 err = ext4_ext_dirty(handle, inode, path + depth);
2901 goto out;
2902 }
2903
2904 /* case a */
2905 memcpy(&orig_ex, ex, sizeof(orig_ex));
2906 ex->ee_len = cpu_to_le16(split - ee_block);
2907 if (split_flag & EXT4_EXT_MARK_UNINIT1)
2908 ext4_ext_mark_uninitialized(ex);
2909
2910 /*
2911 * path may lead to new leaf, not to original leaf any more
2912 * after ext4_ext_insert_extent() returns,
2913 */
2914 err = ext4_ext_dirty(handle, inode, path + depth);
2915 if (err)
2916 goto fix_extent_len;
2917
2918 ex2 = &newex;
2919 ex2->ee_block = cpu_to_le32(split);
2920 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
2921 ext4_ext_store_pblock(ex2, newblock);
2922 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2923 ext4_ext_mark_uninitialized(ex2);
2924
2925 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
2926 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04002927 if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) {
2928 if (split_flag & EXT4_EXT_DATA_VALID1)
2929 err = ext4_ext_zeroout(inode, ex2);
2930 else
2931 err = ext4_ext_zeroout(inode, ex);
2932 } else
2933 err = ext4_ext_zeroout(inode, &orig_ex);
2934
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002935 if (err)
2936 goto fix_extent_len;
2937 /* update the extent length and mark as initialized */
Al Viroaf1584f2012-04-12 20:32:25 -04002938 ex->ee_len = cpu_to_le16(ee_len);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002939 ext4_ext_try_to_merge(inode, path, ex);
2940 err = ext4_ext_dirty(handle, inode, path + depth);
2941 goto out;
2942 } else if (err)
2943 goto fix_extent_len;
2944
2945out:
2946 ext4_ext_show_leaf(inode, path);
2947 return err;
2948
2949fix_extent_len:
2950 ex->ee_len = orig_ex.ee_len;
2951 ext4_ext_dirty(handle, inode, path + depth);
2952 return err;
2953}
2954
2955/*
2956 * ext4_split_extents() splits an extent and mark extent which is covered
2957 * by @map as split_flags indicates
2958 *
2959 * It may result in splitting the extent into multiple extents (upto three)
2960 * There are three possibilities:
2961 * a> There is no split required
2962 * b> Splits in two extents: Split is happening at either end of the extent
2963 * c> Splits in three extents: Somone is splitting in middle of the extent
2964 *
2965 */
2966static int ext4_split_extent(handle_t *handle,
2967 struct inode *inode,
2968 struct ext4_ext_path *path,
2969 struct ext4_map_blocks *map,
2970 int split_flag,
2971 int flags)
2972{
2973 ext4_lblk_t ee_block;
2974 struct ext4_extent *ex;
2975 unsigned int ee_len, depth;
2976 int err = 0;
2977 int uninitialized;
2978 int split_flag1, flags1;
Zheng Liud24f1392013-03-10 21:20:23 -04002979 int allocated = map->m_len;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002980
2981 depth = ext_depth(inode);
2982 ex = path[depth].p_ext;
2983 ee_block = le32_to_cpu(ex->ee_block);
2984 ee_len = ext4_ext_get_actual_len(ex);
2985 uninitialized = ext4_ext_is_uninitialized(ex);
2986
2987 if (map->m_lblk + map->m_len < ee_block + ee_len) {
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04002988 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002989 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
2990 if (uninitialized)
2991 split_flag1 |= EXT4_EXT_MARK_UNINIT1 |
2992 EXT4_EXT_MARK_UNINIT2;
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04002993 if (split_flag & EXT4_EXT_DATA_VALID2)
2994 split_flag1 |= EXT4_EXT_DATA_VALID1;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002995 err = ext4_split_extent_at(handle, inode, path,
2996 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04002997 if (err)
2998 goto out;
Zheng Liud24f1392013-03-10 21:20:23 -04002999 } else {
3000 allocated = ee_len - (map->m_lblk - ee_block);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003001 }
3002
3003 ext4_ext_drop_refs(path);
3004 path = ext4_ext_find_extent(inode, map->m_lblk, path);
3005 if (IS_ERR(path))
3006 return PTR_ERR(path);
3007
3008 if (map->m_lblk >= ee_block) {
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04003009 split_flag1 = split_flag & (EXT4_EXT_MAY_ZEROOUT |
3010 EXT4_EXT_DATA_VALID2);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003011 if (uninitialized)
3012 split_flag1 |= EXT4_EXT_MARK_UNINIT1;
3013 if (split_flag & EXT4_EXT_MARK_UNINIT2)
3014 split_flag1 |= EXT4_EXT_MARK_UNINIT2;
3015 err = ext4_split_extent_at(handle, inode, path,
3016 map->m_lblk, split_flag1, flags);
3017 if (err)
3018 goto out;
3019 }
3020
3021 ext4_ext_show_leaf(inode, path);
3022out:
Zheng Liud24f1392013-03-10 21:20:23 -04003023 return err ? err : allocated;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04003024}
3025
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003026#define EXT4_EXT_ZERO_LEN 7
Amit Arora56055d32007-07-17 21:42:38 -04003027/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003028 * This function is called by ext4_ext_map_blocks() if someone tries to write
Amit Arora56055d32007-07-17 21:42:38 -04003029 * to an uninitialized extent. It may result in splitting the uninitialized
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003030 * extent into multiple extents (up to three - one initialized and two
Amit Arora56055d32007-07-17 21:42:38 -04003031 * uninitialized).
3032 * There are three possibilities:
3033 * a> There is no split required: Entire extent should be initialized
3034 * b> Splits in two extents: Write is happening at either end of the extent
3035 * c> Splits in three extents: Somone is writing in middle of the extent
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003036 *
3037 * Pre-conditions:
3038 * - The extent pointed to by 'path' is uninitialized.
3039 * - The extent pointed to by 'path' contains a superset
3040 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
3041 *
3042 * Post-conditions on success:
3043 * - the returned value is the number of blocks beyond map->l_lblk
3044 * that are allocated and initialized.
3045 * It is guaranteed to be >= map->m_len.
Amit Arora56055d32007-07-17 21:42:38 -04003046 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003047static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003048 struct inode *inode,
3049 struct ext4_map_blocks *map,
3050 struct ext4_ext_path *path)
Amit Arora56055d32007-07-17 21:42:38 -04003051{
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003052 struct ext4_extent_header *eh;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003053 struct ext4_map_blocks split_map;
3054 struct ext4_extent zero_ex;
3055 struct ext4_extent *ex;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003056 ext4_lblk_t ee_block, eof_block;
Dan Carpenterf85b2872011-10-26 03:42:36 -04003057 unsigned int ee_len, depth;
3058 int allocated;
Amit Arora56055d32007-07-17 21:42:38 -04003059 int err = 0;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003060 int split_flag = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003061
3062 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3063 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003064 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003065
3066 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3067 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003068 if (eof_block < map->m_lblk + map->m_len)
3069 eof_block = map->m_lblk + map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003070
3071 depth = ext_depth(inode);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003072 eh = path[depth].p_hdr;
Amit Arora56055d32007-07-17 21:42:38 -04003073 ex = path[depth].p_ext;
3074 ee_block = le32_to_cpu(ex->ee_block);
3075 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003076 allocated = ee_len - (map->m_lblk - ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003077
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003078 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
3079
3080 /* Pre-conditions */
3081 BUG_ON(!ext4_ext_is_uninitialized(ex));
3082 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003083
3084 /*
3085 * Attempt to transfer newly initialized blocks from the currently
3086 * uninitialized extent to its left neighbor. This is much cheaper
3087 * than an insertion followed by a merge as those involve costly
3088 * memmove() calls. This is the common case in steady state for
3089 * workloads doing fallocate(FALLOC_FL_KEEP_SIZE) followed by append
3090 * writes.
3091 *
3092 * Limitations of the current logic:
3093 * - L1: we only deal with writes at the start of the extent.
3094 * The approach could be extended to writes at the end
3095 * of the extent but this scenario was deemed less common.
3096 * - L2: we do not deal with writes covering the whole extent.
3097 * This would require removing the extent if the transfer
3098 * is possible.
3099 * - L3: we only attempt to merge with an extent stored in the
3100 * same extent tree node.
3101 */
3102 if ((map->m_lblk == ee_block) && /*L1*/
3103 (map->m_len < ee_len) && /*L2*/
3104 (ex > EXT_FIRST_EXTENT(eh))) { /*L3*/
3105 struct ext4_extent *prev_ex;
3106 ext4_lblk_t prev_lblk;
3107 ext4_fsblk_t prev_pblk, ee_pblk;
3108 unsigned int prev_len, write_len;
3109
3110 prev_ex = ex - 1;
3111 prev_lblk = le32_to_cpu(prev_ex->ee_block);
3112 prev_len = ext4_ext_get_actual_len(prev_ex);
3113 prev_pblk = ext4_ext_pblock(prev_ex);
3114 ee_pblk = ext4_ext_pblock(ex);
3115 write_len = map->m_len;
3116
3117 /*
3118 * A transfer of blocks from 'ex' to 'prev_ex' is allowed
3119 * upon those conditions:
3120 * - C1: prev_ex is initialized,
3121 * - C2: prev_ex is logically abutting ex,
3122 * - C3: prev_ex is physically abutting ex,
3123 * - C4: prev_ex can receive the additional blocks without
3124 * overflowing the (initialized) length limit.
3125 */
3126 if ((!ext4_ext_is_uninitialized(prev_ex)) && /*C1*/
3127 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3128 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
3129 (prev_len < (EXT_INIT_MAX_LEN - write_len))) { /*C4*/
3130 err = ext4_ext_get_access(handle, inode, path + depth);
3131 if (err)
3132 goto out;
3133
3134 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3135 map, ex, prev_ex);
3136
3137 /* Shift the start of ex by 'write_len' blocks */
3138 ex->ee_block = cpu_to_le32(ee_block + write_len);
3139 ext4_ext_store_pblock(ex, ee_pblk + write_len);
3140 ex->ee_len = cpu_to_le16(ee_len - write_len);
3141 ext4_ext_mark_uninitialized(ex); /* Restore the flag */
3142
3143 /* Extend prev_ex by 'write_len' blocks */
3144 prev_ex->ee_len = cpu_to_le16(prev_len + write_len);
3145
3146 /* Mark the block containing both extents as dirty */
3147 ext4_ext_dirty(handle, inode, path + depth);
3148
3149 /* Update path to point to the right extent */
3150 path[depth].p_ext = prev_ex;
3151
3152 /* Result: number of initialized blocks past m_lblk */
3153 allocated = write_len;
3154 goto out;
3155 }
3156 }
3157
Yongqiang Yang667eff32011-05-03 12:25:07 -04003158 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003159 /*
3160 * It is safe to convert extent to initialized via explicit
3161 * zeroout only if extent is fully insde i_size or new_size.
3162 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003163 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003164
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003165 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003166 if (ee_len <= 2*EXT4_EXT_ZERO_LEN &&
3167 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3168 err = ext4_ext_zeroout(inode, ex);
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003169 if (err)
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003170 goto out;
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003171
3172 err = ext4_ext_get_access(handle, inode, path + depth);
3173 if (err)
3174 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003175 ext4_ext_mark_initialized(ex);
3176 ext4_ext_try_to_merge(inode, path, ex);
3177 err = ext4_ext_dirty(handle, inode, path + depth);
3178 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04003179 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003180
Amit Arora56055d32007-07-17 21:42:38 -04003181 /*
Yongqiang Yang667eff32011-05-03 12:25:07 -04003182 * four cases:
3183 * 1. split the extent into three extents.
3184 * 2. split the extent into two extents, zeroout the first half.
3185 * 3. split the extent into two extents, zeroout the second half.
3186 * 4. split the extent into two extents with out zeroout.
Amit Arora56055d32007-07-17 21:42:38 -04003187 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003188 split_map.m_lblk = map->m_lblk;
3189 split_map.m_len = map->m_len;
3190
3191 if (allocated > map->m_len) {
3192 if (allocated <= EXT4_EXT_ZERO_LEN &&
3193 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3194 /* case 3 */
3195 zero_ex.ee_block =
Allison Henderson9b940f82011-05-16 10:11:09 -04003196 cpu_to_le32(map->m_lblk);
3197 zero_ex.ee_len = cpu_to_le16(allocated);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003198 ext4_ext_store_pblock(&zero_ex,
3199 ext4_ext_pblock(ex) + map->m_lblk - ee_block);
3200 err = ext4_ext_zeroout(inode, &zero_ex);
Amit Arora56055d32007-07-17 21:42:38 -04003201 if (err)
3202 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003203 split_map.m_lblk = map->m_lblk;
3204 split_map.m_len = allocated;
3205 } else if ((map->m_lblk - ee_block + map->m_len <
3206 EXT4_EXT_ZERO_LEN) &&
3207 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3208 /* case 2 */
3209 if (map->m_lblk != ee_block) {
3210 zero_ex.ee_block = ex->ee_block;
3211 zero_ex.ee_len = cpu_to_le16(map->m_lblk -
3212 ee_block);
3213 ext4_ext_store_pblock(&zero_ex,
3214 ext4_ext_pblock(ex));
3215 err = ext4_ext_zeroout(inode, &zero_ex);
3216 if (err)
3217 goto out;
3218 }
3219
Yongqiang Yang667eff32011-05-03 12:25:07 -04003220 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003221 split_map.m_len = map->m_lblk - ee_block + map->m_len;
3222 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003223 }
3224 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003225
3226 allocated = ext4_split_extent(handle, inode, path,
3227 &split_map, split_flag, 0);
3228 if (allocated < 0)
3229 err = allocated;
3230
Amit Arora56055d32007-07-17 21:42:38 -04003231out:
3232 return err ? err : allocated;
3233}
3234
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003235/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003236 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003237 * ext4_get_blocks_dio_write() when DIO to write
3238 * to an uninitialized extent.
3239 *
Paul Bollefd018fe2011-02-15 00:05:43 +01003240 * Writing to an uninitialized extent may result in splitting the uninitialized
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003241 * extent into multiple /initialized uninitialized extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003242 * There are three possibilities:
3243 * a> There is no split required: Entire extent should be uninitialized
3244 * b> Splits in two extents: Write is happening at either end of the extent
3245 * c> Splits in three extents: Somone is writing in middle of the extent
3246 *
3247 * One of more index blocks maybe needed if the extent tree grow after
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003248 * the uninitialized extent split. To prevent ENOSPC occur at the IO
Mingming Cao00314622009-09-28 15:49:08 -04003249 * complete, we need to split the uninitialized extent before DIO submit
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02003250 * the IO. The uninitialized extent called at this time will be split
Mingming Cao00314622009-09-28 15:49:08 -04003251 * into three uninitialized extent(at most). After IO complete, the part
3252 * being filled will be convert to initialized by the end_io callback function
3253 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003254 *
3255 * Returns the size of uninitialized extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003256 */
3257static int ext4_split_unwritten_extents(handle_t *handle,
3258 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003259 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003260 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04003261 int flags)
3262{
Yongqiang Yang667eff32011-05-03 12:25:07 -04003263 ext4_lblk_t eof_block;
3264 ext4_lblk_t ee_block;
3265 struct ext4_extent *ex;
3266 unsigned int ee_len;
3267 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003268
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003269 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
3270 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003271 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003272
3273 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3274 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003275 if (eof_block < map->m_lblk + map->m_len)
3276 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003277 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003278 * It is safe to convert extent to initialized via explicit
3279 * zeroout only if extent is fully insde i_size or new_size.
3280 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003281 depth = ext_depth(inode);
3282 ex = path[depth].p_ext;
3283 ee_block = le32_to_cpu(ex->ee_block);
3284 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003285
Yongqiang Yang667eff32011-05-03 12:25:07 -04003286 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
3287 split_flag |= EXT4_EXT_MARK_UNINIT2;
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04003288 if (flags & EXT4_GET_BLOCKS_CONVERT)
3289 split_flag |= EXT4_EXT_DATA_VALID2;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003290 flags |= EXT4_GET_BLOCKS_PRE_IO;
3291 return ext4_split_extent(handle, inode, path, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003292}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003293
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003294static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04003295 struct inode *inode,
3296 struct ext4_map_blocks *map,
3297 struct ext4_ext_path *path)
Mingming Cao00314622009-09-28 15:49:08 -04003298{
3299 struct ext4_extent *ex;
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04003300 ext4_lblk_t ee_block;
3301 unsigned int ee_len;
Mingming Cao00314622009-09-28 15:49:08 -04003302 int depth;
3303 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003304
3305 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003306 ex = path[depth].p_ext;
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04003307 ee_block = le32_to_cpu(ex->ee_block);
3308 ee_len = ext4_ext_get_actual_len(ex);
Mingming Cao00314622009-09-28 15:49:08 -04003309
Yongqiang Yang197217a2011-05-03 11:45:29 -04003310 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3311 "block %llu, max_blocks %u\n", inode->i_ino,
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04003312 (unsigned long long)ee_block, ee_len);
3313
3314 /* If extent is larger than requested then split is required */
3315 if (ee_block != map->m_lblk || ee_len > map->m_len) {
3316 err = ext4_split_unwritten_extents(handle, inode, map, path,
3317 EXT4_GET_BLOCKS_CONVERT);
3318 if (err < 0)
3319 goto out;
3320 ext4_ext_drop_refs(path);
3321 path = ext4_ext_find_extent(inode, map->m_lblk, path);
3322 if (IS_ERR(path)) {
3323 err = PTR_ERR(path);
3324 goto out;
3325 }
3326 depth = ext_depth(inode);
3327 ex = path[depth].p_ext;
3328 }
Yongqiang Yang197217a2011-05-03 11:45:29 -04003329
Mingming Cao00314622009-09-28 15:49:08 -04003330 err = ext4_ext_get_access(handle, inode, path + depth);
3331 if (err)
3332 goto out;
3333 /* first mark the extent as initialized */
3334 ext4_ext_mark_initialized(ex);
3335
Yongqiang Yang197217a2011-05-03 11:45:29 -04003336 /* note: ext4_ext_correct_indexes() isn't needed here because
3337 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003338 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04003339 ext4_ext_try_to_merge(inode, path, ex);
3340
Mingming Cao00314622009-09-28 15:49:08 -04003341 /* Mark modified extent as dirty */
3342 err = ext4_ext_dirty(handle, inode, path + depth);
3343out:
3344 ext4_ext_show_leaf(inode, path);
3345 return err;
3346}
3347
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003348static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3349 sector_t block, int count)
3350{
3351 int i;
3352 for (i = 0; i < count; i++)
3353 unmap_underlying_metadata(bdev, block + i);
3354}
3355
Theodore Ts'o58590b02010-10-27 21:23:12 -04003356/*
3357 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3358 */
3359static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
Eric Sandeend002ebf2011-01-10 13:03:35 -05003360 ext4_lblk_t lblk,
Theodore Ts'o58590b02010-10-27 21:23:12 -04003361 struct ext4_ext_path *path,
3362 unsigned int len)
3363{
3364 int i, depth;
3365 struct ext4_extent_header *eh;
Sergey Senozhatsky65922cb2011-03-23 14:08:27 -04003366 struct ext4_extent *last_ex;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003367
3368 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3369 return 0;
3370
3371 depth = ext_depth(inode);
3372 eh = path[depth].p_hdr;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003373
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003374 /*
3375 * We're going to remove EOFBLOCKS_FL entirely in future so we
3376 * do not care for this case anymore. Simply remove the flag
3377 * if there are no extents.
3378 */
3379 if (unlikely(!eh->eh_entries))
3380 goto out;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003381 last_ex = EXT_LAST_EXTENT(eh);
3382 /*
3383 * We should clear the EOFBLOCKS_FL flag if we are writing the
3384 * last block in the last extent in the file. We test this by
3385 * first checking to see if the caller to
3386 * ext4_ext_get_blocks() was interested in the last block (or
3387 * a block beyond the last block) in the current extent. If
3388 * this turns out to be false, we can bail out from this
3389 * function immediately.
3390 */
Eric Sandeend002ebf2011-01-10 13:03:35 -05003391 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
Theodore Ts'o58590b02010-10-27 21:23:12 -04003392 ext4_ext_get_actual_len(last_ex))
3393 return 0;
3394 /*
3395 * If the caller does appear to be planning to write at or
3396 * beyond the end of the current extent, we then test to see
3397 * if the current extent is the last extent in the file, by
3398 * checking to make sure it was reached via the rightmost node
3399 * at each level of the tree.
3400 */
3401 for (i = depth-1; i >= 0; i--)
3402 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3403 return 0;
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003404out:
Theodore Ts'o58590b02010-10-27 21:23:12 -04003405 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3406 return ext4_mark_inode_dirty(handle, inode);
3407}
3408
Aditya Kali7b415bf2011-09-09 19:04:51 -04003409/**
3410 * ext4_find_delalloc_range: find delayed allocated block in the given range.
3411 *
3412 * Goes through the buffer heads in the range [lblk_start, lblk_end] and returns
3413 * whether there are any buffers marked for delayed allocation. It returns '1'
3414 * on the first delalloc'ed buffer head found. If no buffer head in the given
3415 * range is marked for delalloc, it returns 0.
3416 * lblk_start should always be <= lblk_end.
3417 * search_hint_reverse is to indicate that searching in reverse from lblk_end to
3418 * lblk_start might be more efficient (i.e., we will likely hit the delalloc'ed
3419 * block sooner). This is useful when blocks are truncated sequentially from
3420 * lblk_start towards lblk_end.
3421 */
3422static int ext4_find_delalloc_range(struct inode *inode,
3423 ext4_lblk_t lblk_start,
3424 ext4_lblk_t lblk_end,
3425 int search_hint_reverse)
3426{
3427 struct address_space *mapping = inode->i_mapping;
3428 struct buffer_head *head, *bh = NULL;
3429 struct page *page;
3430 ext4_lblk_t i, pg_lblk;
3431 pgoff_t index;
3432
Robin Dong8c48f7e2011-12-18 23:05:43 -05003433 if (!test_opt(inode->i_sb, DELALLOC))
3434 return 0;
3435
Aditya Kali7b415bf2011-09-09 19:04:51 -04003436 /* reverse search wont work if fs block size is less than page size */
3437 if (inode->i_blkbits < PAGE_CACHE_SHIFT)
3438 search_hint_reverse = 0;
3439
3440 if (search_hint_reverse)
3441 i = lblk_end;
3442 else
3443 i = lblk_start;
3444
3445 index = i >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
3446
3447 while ((i >= lblk_start) && (i <= lblk_end)) {
3448 page = find_get_page(mapping, index);
Aditya Kali5356f262011-09-09 19:20:51 -04003449 if (!page)
Aditya Kali7b415bf2011-09-09 19:04:51 -04003450 goto nextpage;
3451
Aditya Kali7b415bf2011-09-09 19:04:51 -04003452 if (!page_has_buffers(page))
3453 goto nextpage;
3454
3455 head = page_buffers(page);
3456 if (!head)
3457 goto nextpage;
3458
3459 bh = head;
3460 pg_lblk = index << (PAGE_CACHE_SHIFT -
3461 inode->i_blkbits);
3462 do {
3463 if (unlikely(pg_lblk < lblk_start)) {
3464 /*
3465 * This is possible when fs block size is less
3466 * than page size and our cluster starts/ends in
3467 * middle of the page. So we need to skip the
3468 * initial few blocks till we reach the 'lblk'
3469 */
3470 pg_lblk++;
3471 continue;
3472 }
3473
Aditya Kali5356f262011-09-09 19:20:51 -04003474 /* Check if the buffer is delayed allocated and that it
3475 * is not yet mapped. (when da-buffers are mapped during
3476 * their writeout, their da_mapped bit is set.)
3477 */
3478 if (buffer_delay(bh) && !buffer_da_mapped(bh)) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04003479 page_cache_release(page);
Aditya Kalid8990242011-09-09 19:18:51 -04003480 trace_ext4_find_delalloc_range(inode,
3481 lblk_start, lblk_end,
3482 search_hint_reverse,
3483 1, i);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003484 return 1;
3485 }
3486 if (search_hint_reverse)
3487 i--;
3488 else
3489 i++;
3490 } while ((i >= lblk_start) && (i <= lblk_end) &&
3491 ((bh = bh->b_this_page) != head));
3492nextpage:
3493 if (page)
3494 page_cache_release(page);
3495 /*
3496 * Move to next page. 'i' will be the first lblk in the next
3497 * page.
3498 */
3499 if (search_hint_reverse)
3500 index--;
3501 else
3502 index++;
3503 i = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
3504 }
3505
Aditya Kalid8990242011-09-09 19:18:51 -04003506 trace_ext4_find_delalloc_range(inode, lblk_start, lblk_end,
3507 search_hint_reverse, 0, 0);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003508 return 0;
3509}
3510
3511int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk,
3512 int search_hint_reverse)
3513{
3514 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3515 ext4_lblk_t lblk_start, lblk_end;
3516 lblk_start = lblk & (~(sbi->s_cluster_ratio - 1));
3517 lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
3518
3519 return ext4_find_delalloc_range(inode, lblk_start, lblk_end,
3520 search_hint_reverse);
3521}
3522
3523/**
3524 * Determines how many complete clusters (out of those specified by the 'map')
3525 * are under delalloc and were reserved quota for.
3526 * This function is called when we are writing out the blocks that were
3527 * originally written with their allocation delayed, but then the space was
3528 * allocated using fallocate() before the delayed allocation could be resolved.
3529 * The cases to look for are:
3530 * ('=' indicated delayed allocated blocks
3531 * '-' indicates non-delayed allocated blocks)
3532 * (a) partial clusters towards beginning and/or end outside of allocated range
3533 * are not delalloc'ed.
3534 * Ex:
3535 * |----c---=|====c====|====c====|===-c----|
3536 * |++++++ allocated ++++++|
3537 * ==> 4 complete clusters in above example
3538 *
3539 * (b) partial cluster (outside of allocated range) towards either end is
3540 * marked for delayed allocation. In this case, we will exclude that
3541 * cluster.
3542 * Ex:
3543 * |----====c========|========c========|
3544 * |++++++ allocated ++++++|
3545 * ==> 1 complete clusters in above example
3546 *
3547 * Ex:
3548 * |================c================|
3549 * |++++++ allocated ++++++|
3550 * ==> 0 complete clusters in above example
3551 *
3552 * The ext4_da_update_reserve_space will be called only if we
3553 * determine here that there were some "entire" clusters that span
3554 * this 'allocated' range.
3555 * In the non-bigalloc case, this function will just end up returning num_blks
3556 * without ever calling ext4_find_delalloc_range.
3557 */
3558static unsigned int
3559get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start,
3560 unsigned int num_blks)
3561{
3562 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3563 ext4_lblk_t alloc_cluster_start, alloc_cluster_end;
3564 ext4_lblk_t lblk_from, lblk_to, c_offset;
3565 unsigned int allocated_clusters = 0;
3566
3567 alloc_cluster_start = EXT4_B2C(sbi, lblk_start);
3568 alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1);
3569
3570 /* max possible clusters for this allocation */
3571 allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1;
3572
Aditya Kalid8990242011-09-09 19:18:51 -04003573 trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks);
3574
Aditya Kali7b415bf2011-09-09 19:04:51 -04003575 /* Check towards left side */
3576 c_offset = lblk_start & (sbi->s_cluster_ratio - 1);
3577 if (c_offset) {
3578 lblk_from = lblk_start & (~(sbi->s_cluster_ratio - 1));
3579 lblk_to = lblk_from + c_offset - 1;
3580
3581 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
3582 allocated_clusters--;
3583 }
3584
3585 /* Now check towards right. */
3586 c_offset = (lblk_start + num_blks) & (sbi->s_cluster_ratio - 1);
3587 if (allocated_clusters && c_offset) {
3588 lblk_from = lblk_start + num_blks;
3589 lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1;
3590
3591 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
3592 allocated_clusters--;
3593 }
3594
3595 return allocated_clusters;
3596}
3597
Mingming Cao00314622009-09-28 15:49:08 -04003598static int
3599ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003600 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003601 struct ext4_ext_path *path, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003602 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003603{
3604 int ret = 0;
3605 int err = 0;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003606 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Mingming Cao00314622009-09-28 15:49:08 -04003607
Zheng Liu88635ca2011-12-28 19:00:25 -05003608 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical "
3609 "block %llu, max_blocks %u, flags %x, allocated %u\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003610 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04003611 flags, allocated);
3612 ext4_ext_show_leaf(inode, path);
3613
Aditya Kalid8990242011-09-09 19:18:51 -04003614 trace_ext4_ext_handle_uninitialized_extents(inode, map, allocated,
3615 newblock);
3616
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003617 /* get_block() before submit the IO, split the extent */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003618 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003619 ret = ext4_split_unwritten_extents(handle, inode, map,
3620 path, flags);
Mingming5f524952009-11-10 10:48:04 -05003621 /*
3622 * Flag the inode(non aio case) or end_io struct (aio case)
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003623 * that this IO needs to conversion to written when IO is
Mingming5f524952009-11-10 10:48:04 -05003624 * completed
3625 */
Tao Ma0edeb712011-10-31 17:30:44 -04003626 if (io)
3627 ext4_set_io_unwritten_flag(inode, io);
3628 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003629 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
Jiaying Zhang744692d2010-03-04 16:14:02 -05003630 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003631 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao00314622009-09-28 15:49:08 -04003632 goto out;
3633 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003634 /* IO end_io complete, convert the filled extent to written */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003635 if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04003636 ret = ext4_convert_unwritten_extents_endio(handle, inode, map,
Mingming Cao00314622009-09-28 15:49:08 -04003637 path);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003638 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05003639 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05003640 err = check_eofblocks_fl(handle, inode, map->m_lblk,
3641 path, map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003642 } else
3643 err = ret;
Mingming Cao00314622009-09-28 15:49:08 -04003644 goto out2;
3645 }
3646 /* buffered IO case */
3647 /*
3648 * repeat fallocate creation request
3649 * we already have an unwritten extent
3650 */
3651 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3652 goto map_out;
3653
3654 /* buffered READ or buffered write_begin() lookup */
3655 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3656 /*
3657 * We have blocks reserved already. We
3658 * return allocated blocks so that delalloc
3659 * won't do block reservation for us. But
3660 * the buffer head will be unmapped so that
3661 * a read from the block returns 0s.
3662 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003663 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003664 goto out1;
3665 }
3666
3667 /* buffered write, writepage time, convert*/
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003668 ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04003669 if (ret >= 0)
Jan Karab436b9b2009-12-08 23:51:10 -05003670 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04003671out:
3672 if (ret <= 0) {
3673 err = ret;
3674 goto out2;
3675 } else
3676 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003677 map->m_flags |= EXT4_MAP_NEW;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003678 /*
3679 * if we allocated more blocks than requested
3680 * we need to make sure we unmap the extra block
3681 * allocated. The actual needed block will get
3682 * unmapped later when we find the buffer_head marked
3683 * new.
3684 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003685 if (allocated > map->m_len) {
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003686 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003687 newblock + map->m_len,
3688 allocated - map->m_len);
3689 allocated = map->m_len;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003690 }
Zheng Liud24f1392013-03-10 21:20:23 -04003691 map->m_len = allocated;
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003692
3693 /*
3694 * If we have done fallocate with the offset that is already
3695 * delayed allocated, we would have block reservation
3696 * and quota reservation done in the delayed write path.
3697 * But fallocate would have already updated quota and block
3698 * count for this offset. So cancel these reservation
3699 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003700 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
3701 unsigned int reserved_clusters;
3702 reserved_clusters = get_reserved_cluster_alloc(inode,
3703 map->m_lblk, map->m_len);
3704 if (reserved_clusters)
3705 ext4_da_update_reserve_space(inode,
3706 reserved_clusters,
3707 0);
3708 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003709
Mingming Cao00314622009-09-28 15:49:08 -04003710map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003711 map->m_flags |= EXT4_MAP_MAPPED;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04003712 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
3713 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
3714 map->m_len);
3715 if (err < 0)
3716 goto out2;
3717 }
Mingming Cao00314622009-09-28 15:49:08 -04003718out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003719 if (allocated > map->m_len)
3720 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003721 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003722 map->m_pblk = newblock;
3723 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04003724out2:
3725 if (path) {
3726 ext4_ext_drop_refs(path);
3727 kfree(path);
3728 }
3729 return err ? err : allocated;
3730}
Theodore Ts'o58590b02010-10-27 21:23:12 -04003731
Mingming Cao00314622009-09-28 15:49:08 -04003732/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003733 * get_implied_cluster_alloc - check to see if the requested
3734 * allocation (in the map structure) overlaps with a cluster already
3735 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04003736 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003737 * @map The requested lblk->pblk mapping
3738 * @ex The extent structure which might contain an implied
3739 * cluster allocation
3740 *
3741 * This function is called by ext4_ext_map_blocks() after we failed to
3742 * find blocks that were already in the inode's extent tree. Hence,
3743 * we know that the beginning of the requested region cannot overlap
3744 * the extent from the inode's extent tree. There are three cases we
3745 * want to catch. The first is this case:
3746 *
3747 * |--- cluster # N--|
3748 * |--- extent ---| |---- requested region ---|
3749 * |==========|
3750 *
3751 * The second case that we need to test for is this one:
3752 *
3753 * |--------- cluster # N ----------------|
3754 * |--- requested region --| |------- extent ----|
3755 * |=======================|
3756 *
3757 * The third case is when the requested region lies between two extents
3758 * within the same cluster:
3759 * |------------- cluster # N-------------|
3760 * |----- ex -----| |---- ex_right ----|
3761 * |------ requested region ------|
3762 * |================|
3763 *
3764 * In each of the above cases, we need to set the map->m_pblk and
3765 * map->m_len so it corresponds to the return the extent labelled as
3766 * "|====|" from cluster #N, since it is already in use for data in
3767 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
3768 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
3769 * as a new "allocated" block region. Otherwise, we will return 0 and
3770 * ext4_ext_map_blocks() will then allocate one or more new clusters
3771 * by calling ext4_mb_new_blocks().
3772 */
Aditya Kalid8990242011-09-09 19:18:51 -04003773static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003774 struct ext4_map_blocks *map,
3775 struct ext4_extent *ex,
3776 struct ext4_ext_path *path)
3777{
Aditya Kalid8990242011-09-09 19:18:51 -04003778 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003779 ext4_lblk_t c_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
3780 ext4_lblk_t ex_cluster_start, ex_cluster_end;
Curt Wohlgemuth14d7f3e2011-12-18 17:39:02 -05003781 ext4_lblk_t rr_cluster_start;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003782 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
3783 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
3784 unsigned short ee_len = ext4_ext_get_actual_len(ex);
3785
3786 /* The extent passed in that we are trying to match */
3787 ex_cluster_start = EXT4_B2C(sbi, ee_block);
3788 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
3789
3790 /* The requested region passed into ext4_map_blocks() */
3791 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003792
3793 if ((rr_cluster_start == ex_cluster_end) ||
3794 (rr_cluster_start == ex_cluster_start)) {
3795 if (rr_cluster_start == ex_cluster_end)
3796 ee_start += ee_len - 1;
3797 map->m_pblk = (ee_start & ~(sbi->s_cluster_ratio - 1)) +
3798 c_offset;
3799 map->m_len = min(map->m_len,
3800 (unsigned) sbi->s_cluster_ratio - c_offset);
3801 /*
3802 * Check for and handle this case:
3803 *
3804 * |--------- cluster # N-------------|
3805 * |------- extent ----|
3806 * |--- requested region ---|
3807 * |===========|
3808 */
3809
3810 if (map->m_lblk < ee_block)
3811 map->m_len = min(map->m_len, ee_block - map->m_lblk);
3812
3813 /*
3814 * Check for the case where there is already another allocated
3815 * block to the right of 'ex' but before the end of the cluster.
3816 *
3817 * |------------- cluster # N-------------|
3818 * |----- ex -----| |---- ex_right ----|
3819 * |------ requested region ------|
3820 * |================|
3821 */
3822 if (map->m_lblk > ee_block) {
3823 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
3824 map->m_len = min(map->m_len, next - map->m_lblk);
3825 }
Aditya Kalid8990242011-09-09 19:18:51 -04003826
3827 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003828 return 1;
3829 }
Aditya Kalid8990242011-09-09 19:18:51 -04003830
3831 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003832 return 0;
3833}
3834
3835
3836/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05003837 * Block allocation/map/preallocation routine for extents based files
3838 *
3839 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003840 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003841 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3842 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05003843 *
3844 * return > 0, number of of blocks already mapped/allocated
3845 * if create == 0 and these are pre-allocated blocks
3846 * buffer head is unmapped
3847 * otherwise blocks are mapped
3848 *
3849 * return = 0, if plain look up failed (blocks have not been allocated)
3850 * buffer head is unmapped
3851 *
3852 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003853 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003854int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3855 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07003856{
3857 struct ext4_ext_path *path = NULL;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003858 struct ext4_extent newex, *ex, *ex2;
3859 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003860 ext4_fsblk_t newblock = 0;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003861 int free_on_err = 0, err = 0, depth, ret;
3862 unsigned int allocated = 0, offset = 0;
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04003863 unsigned int allocated_clusters = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003864 struct ext4_allocation_request ar;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003865 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003866 ext4_lblk_t cluster_offset;
Alex Tomasa86c6182006-10-11 01:21:03 -07003867
Mingming84fe3be2009-09-01 08:44:37 -04003868 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003869 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003870 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07003871
3872 /* check in cache */
Lukas Czerner78771912012-03-19 23:05:43 -04003873 if (ext4_ext_in_cache(inode, map->m_lblk, &newex)) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003874 if (!newex.ee_start_lo && !newex.ee_start_hi) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04003875 if ((sbi->s_cluster_ratio > 1) &&
3876 ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
3877 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3878
Theodore Ts'oc2177052009-05-14 00:58:52 -04003879 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003880 /*
3881 * block isn't allocated yet and
3882 * user doesn't want to allocate it
3883 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003884 goto out2;
3885 }
3886 /* we should allocate requested block */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003887 } else {
Alex Tomasa86c6182006-10-11 01:21:03 -07003888 /* block is already allocated */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003889 if (sbi->s_cluster_ratio > 1)
3890 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003891 newblock = map->m_lblk
Dave Kleikamp8c55e202007-05-24 13:04:54 -04003892 - le32_to_cpu(newex.ee_block)
Theodore Ts'obf89d162010-10-27 21:30:14 -04003893 + ext4_ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003894 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003895 allocated = ext4_ext_get_actual_len(&newex) -
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003896 (map->m_lblk - le32_to_cpu(newex.ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -07003897 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07003898 }
3899 }
3900
3901 /* find extent for this block */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003902 path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07003903 if (IS_ERR(path)) {
3904 err = PTR_ERR(path);
3905 path = NULL;
3906 goto out2;
3907 }
3908
3909 depth = ext_depth(inode);
3910
3911 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003912 * consistent leaf must not be empty;
3913 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07003914 * this is why assert can't be put in ext4_ext_find_extent()
3915 */
Frank Mayhar273df552010-03-02 11:46:09 -05003916 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
3917 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04003918 "lblock: %lu, depth: %d pblock %lld",
3919 (unsigned long) map->m_lblk, depth,
3920 path[depth].p_block);
Surbhi Palande034fb4c2009-12-14 09:53:52 -05003921 err = -EIO;
3922 goto out2;
3923 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003924
Avantika Mathur7e028972006-12-06 20:41:33 -08003925 ex = path[depth].p_ext;
3926 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003927 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003928 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003929 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003930
3931 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003932 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04003933 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003934 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003935 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04003936
3937 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
3938
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003939 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003940 if (in_range(map->m_lblk, ee_block, ee_len)) {
3941 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003942 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003943 allocated = ee_len - (map->m_lblk - ee_block);
3944 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
3945 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04003946
Allison Hendersone8613042011-05-25 07:41:46 -04003947 /*
Lukas Czerner78771912012-03-19 23:05:43 -04003948 * Do not put uninitialized extent
3949 * in the cache
Allison Hendersone8613042011-05-25 07:41:46 -04003950 */
Lukas Czerner78771912012-03-19 23:05:43 -04003951 if (!ext4_ext_is_uninitialized(ex)) {
3952 ext4_ext_put_in_cache(inode, ee_block,
3953 ee_len, ee_start);
3954 goto out;
Allison Hendersone8613042011-05-25 07:41:46 -04003955 }
Lukas Czerner78771912012-03-19 23:05:43 -04003956 ret = ext4_ext_handle_uninitialized_extents(
3957 handle, inode, map, path, flags,
3958 allocated, newblock);
3959 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07003960 }
3961 }
3962
Aditya Kali7b415bf2011-09-09 19:04:51 -04003963 if ((sbi->s_cluster_ratio > 1) &&
3964 ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
3965 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3966
Alex Tomasa86c6182006-10-11 01:21:03 -07003967 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003968 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07003969 * we couldn't try to create block if create flag is zero
3970 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04003971 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003972 /*
3973 * put just found gap into cache to speed up
3974 * subsequent requests
3975 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003976 ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
Alex Tomasa86c6182006-10-11 01:21:03 -07003977 goto out2;
3978 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003979
Alex Tomasa86c6182006-10-11 01:21:03 -07003980 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003981 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07003982 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003983 map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003984 newex.ee_block = cpu_to_le32(map->m_lblk);
3985 cluster_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
3986
3987 /*
3988 * If we are doing bigalloc, check to see if the extent returned
3989 * by ext4_ext_find_extent() implies a cluster we can use.
3990 */
3991 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04003992 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003993 ar.len = allocated = map->m_len;
3994 newblock = map->m_pblk;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003995 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003996 goto got_allocated_blocks;
3997 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003998
Alex Tomasc9de5602008-01-29 00:19:52 -05003999 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004000 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05004001 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
4002 if (err)
4003 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004004 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004005 ex2 = NULL;
4006 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
Alex Tomasc9de5602008-01-29 00:19:52 -05004007 if (err)
4008 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04004009
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004010 /* Check if the extent after searching to the right implies a
4011 * cluster we can use. */
4012 if ((sbi->s_cluster_ratio > 1) && ex2 &&
Aditya Kalid8990242011-09-09 19:18:51 -04004013 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004014 ar.len = allocated = map->m_len;
4015 newblock = map->m_pblk;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004016 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004017 goto got_allocated_blocks;
4018 }
4019
Amit Arora749269f2007-07-18 09:02:56 -04004020 /*
4021 * See if request is beyond maximum number of blocks we can have in
4022 * a single extent. For an initialized extent this limit is
4023 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
4024 * EXT_UNINIT_MAX_LEN.
4025 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004026 if (map->m_len > EXT_INIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04004027 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004028 map->m_len = EXT_INIT_MAX_LEN;
4029 else if (map->m_len > EXT_UNINIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04004030 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004031 map->m_len = EXT_UNINIT_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04004032
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004033 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004034 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004035 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04004036 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004037 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04004038 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004039 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004040
4041 /* allocate new block */
4042 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004043 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
4044 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004045 /*
4046 * We calculate the offset from the beginning of the cluster
4047 * for the logical block number, since when we allocate a
4048 * physical cluster, the physical block should start at the
4049 * same offset from the beginning of the cluster. This is
4050 * needed so that future calls to get_implied_cluster_alloc()
4051 * work correctly.
4052 */
4053 offset = map->m_lblk & (sbi->s_cluster_ratio - 1);
4054 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
4055 ar.goal -= offset;
4056 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05004057 if (S_ISREG(inode->i_mode))
4058 ar.flags = EXT4_MB_HINT_DATA;
4059 else
4060 /* disable in-core preallocation for non-regular files */
4061 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04004062 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4063 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05004064 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004065 if (!newblock)
4066 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04004067 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004068 ar.goal, newblock, allocated);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004069 free_on_err = 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004070 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004071 ar.len = EXT4_C2B(sbi, ar.len) - offset;
4072 if (ar.len > allocated)
4073 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004074
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004075got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07004076 /* try to insert new extent into found leaf and return */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004077 ext4_ext_store_pblock(&newex, newblock + offset);
Alex Tomasc9de5602008-01-29 00:19:52 -05004078 newex.ee_len = cpu_to_le16(ar.len);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004079 /* Mark uninitialized */
4080 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
Amit Aroraa2df2a62007-07-17 21:42:41 -04004081 ext4_ext_mark_uninitialized(&newex);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004082 /*
Jiaying Zhang744692d2010-03-04 16:14:02 -05004083 * io_end structure was created for every IO write to an
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004084 * uninitialized extent. To avoid unnecessary conversion,
Jiaying Zhang744692d2010-03-04 16:14:02 -05004085 * here we flag the IO that really needs the conversion.
Mingming5f524952009-11-10 10:48:04 -05004086 * For non asycn direct IO case, flag the inode state
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004087 * that we need to perform conversion when IO is done.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004088 */
Jiaying Zhang744692d2010-03-04 16:14:02 -05004089 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Tao Ma0edeb712011-10-31 17:30:44 -04004090 if (io)
4091 ext4_set_io_unwritten_flag(inode, io);
4092 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004093 ext4_set_inode_state(inode,
4094 EXT4_STATE_DIO_UNWRITTEN);
Mingming5f524952009-11-10 10:48:04 -05004095 }
Jiaying Zhang744692d2010-03-04 16:14:02 -05004096 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004097 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004098 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004099
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004100 err = 0;
4101 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
4102 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4103 path, ar.len);
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004104 if (!err)
4105 err = ext4_ext_insert_extent(handle, inode, path,
4106 &newex, flags);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004107 if (err && free_on_err) {
Maxim Patlasov7132de72011-07-10 19:37:48 -04004108 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
4109 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
Alex Tomas315054f2007-05-24 13:04:25 -04004110 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05004111 /* not a good idea to call discard here directly,
4112 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004113 ext4_discard_preallocations(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05004114 ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex),
Maxim Patlasov7132de72011-07-10 19:37:48 -04004115 ext4_ext_get_actual_len(&newex), fb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004116 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04004117 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004118
Alex Tomasa86c6182006-10-11 01:21:03 -07004119 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04004120 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004121 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004122 if (allocated > map->m_len)
4123 allocated = map->m_len;
4124 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07004125
Jan Karab436b9b2009-12-08 23:51:10 -05004126 /*
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004127 * Update reserved blocks/metadata blocks after successful
4128 * block allocation which had been deferred till now.
4129 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04004130 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004131 unsigned int reserved_clusters;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004132 /*
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004133 * Check how many clusters we had reserved this allocated range
Aditya Kali7b415bf2011-09-09 19:04:51 -04004134 */
4135 reserved_clusters = get_reserved_cluster_alloc(inode,
4136 map->m_lblk, allocated);
4137 if (map->m_flags & EXT4_MAP_FROM_CLUSTER) {
4138 if (reserved_clusters) {
4139 /*
4140 * We have clusters reserved for this range.
4141 * But since we are not doing actual allocation
4142 * and are simply using blocks from previously
4143 * allocated cluster, we should release the
4144 * reservation and not claim quota.
4145 */
4146 ext4_da_update_reserve_space(inode,
4147 reserved_clusters, 0);
4148 }
4149 } else {
4150 BUG_ON(allocated_clusters < reserved_clusters);
4151 /* We will claim quota for all newly allocated blocks.*/
4152 ext4_da_update_reserve_space(inode, allocated_clusters,
4153 1);
4154 if (reserved_clusters < allocated_clusters) {
Aditya Kali5356f262011-09-09 19:20:51 -04004155 struct ext4_inode_info *ei = EXT4_I(inode);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004156 int reservation = allocated_clusters -
4157 reserved_clusters;
4158 /*
4159 * It seems we claimed few clusters outside of
4160 * the range of this allocation. We should give
4161 * it back to the reservation pool. This can
4162 * happen in the following case:
4163 *
4164 * * Suppose s_cluster_ratio is 4 (i.e., each
4165 * cluster has 4 blocks. Thus, the clusters
4166 * are [0-3],[4-7],[8-11]...
4167 * * First comes delayed allocation write for
4168 * logical blocks 10 & 11. Since there were no
4169 * previous delayed allocated blocks in the
4170 * range [8-11], we would reserve 1 cluster
4171 * for this write.
4172 * * Next comes write for logical blocks 3 to 8.
4173 * In this case, we will reserve 2 clusters
4174 * (for [0-3] and [4-7]; and not for [8-11] as
4175 * that range has a delayed allocated blocks.
4176 * Thus total reserved clusters now becomes 3.
4177 * * Now, during the delayed allocation writeout
4178 * time, we will first write blocks [3-8] and
4179 * allocate 3 clusters for writing these
4180 * blocks. Also, we would claim all these
4181 * three clusters above.
4182 * * Now when we come here to writeout the
4183 * blocks [10-11], we would expect to claim
4184 * the reservation of 1 cluster we had made
4185 * (and we would claim it since there are no
4186 * more delayed allocated blocks in the range
4187 * [8-11]. But our reserved cluster count had
4188 * already gone to 0.
4189 *
4190 * Thus, at the step 4 above when we determine
4191 * that there are still some unwritten delayed
4192 * allocated blocks outside of our current
4193 * block range, we should increment the
4194 * reserved clusters count so that when the
4195 * remaining blocks finally gets written, we
4196 * could claim them.
4197 */
Aditya Kali5356f262011-09-09 19:20:51 -04004198 dquot_reserve_block(inode,
4199 EXT4_C2B(sbi, reservation));
4200 spin_lock(&ei->i_block_reservation_lock);
4201 ei->i_reserved_data_blocks += reservation;
4202 spin_unlock(&ei->i_block_reservation_lock);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004203 }
4204 }
4205 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004206
4207 /*
Jan Karab436b9b2009-12-08 23:51:10 -05004208 * Cache the extent and update transaction to commit on fdatasync only
4209 * when it is _not_ an uninitialized extent.
4210 */
4211 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05004212 ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock);
Jan Karab436b9b2009-12-08 23:51:10 -05004213 ext4_update_inode_fsync_trans(handle, inode, 1);
4214 } else
4215 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004216out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004217 if (allocated > map->m_len)
4218 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004219 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004220 map->m_flags |= EXT4_MAP_MAPPED;
4221 map->m_pblk = newblock;
4222 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004223out2:
4224 if (path) {
4225 ext4_ext_drop_refs(path);
4226 kfree(path);
4227 }
Allison Hendersone8613042011-05-25 07:41:46 -04004228
Yongqiang Yange7b319e2011-10-29 09:39:51 -04004229 trace_ext4_ext_map_blocks_exit(inode, map->m_lblk,
Lukas Czerner78771912012-03-19 23:05:43 -04004230 newblock, map->m_len, err ? err : allocated);
Yongqiang Yange7b319e2011-10-29 09:39:51 -04004231
Lukas Czerner78771912012-03-19 23:05:43 -04004232 return err ? err : allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004233}
4234
Jan Karacf108bc2008-07-11 19:27:31 -04004235void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004236{
4237 struct address_space *mapping = inode->i_mapping;
4238 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004239 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004240 handle_t *handle;
Allison Henderson189e8682011-09-06 21:49:44 -04004241 loff_t page_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004242 int err = 0;
4243
4244 /*
Jiaying Zhang3889fd52011-01-10 12:47:05 -05004245 * finish any pending end_io work so we won't run the risk of
4246 * converting any truncated blocks to initialized later
4247 */
4248 ext4_flush_completed_IO(inode);
4249
4250 /*
Alex Tomasa86c6182006-10-11 01:21:03 -07004251 * probably first extent we're gonna free will be last in block
4252 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04004253 err = ext4_writepage_trans_blocks(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07004254 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04004255 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07004256 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07004257
Allison Henderson189e8682011-09-06 21:49:44 -04004258 if (inode->i_size % PAGE_CACHE_SIZE != 0) {
4259 page_len = PAGE_CACHE_SIZE -
4260 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4261
4262 err = ext4_discard_partial_page_buffers(handle,
4263 mapping, inode->i_size, page_len, 0);
4264
4265 if (err)
4266 goto out_stop;
4267 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004268
Jan Kara9ddfc3d2008-07-11 19:27:31 -04004269 if (ext4_orphan_add(handle, inode))
4270 goto out_stop;
4271
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004272 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07004273 ext4_ext_invalidate_cache(inode);
4274
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004275 ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05004276
Alex Tomasa86c6182006-10-11 01:21:03 -07004277 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004278 * TODO: optimization is possible here.
4279 * Probably we need not scan at all,
4280 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004281 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004282
4283 /* we have to know where to truncate from in crash case */
4284 EXT4_I(inode)->i_disksize = inode->i_size;
4285 ext4_mark_inode_dirty(handle, inode);
4286
4287 last_block = (inode->i_size + sb->s_blocksize - 1)
4288 >> EXT4_BLOCK_SIZE_BITS(sb);
Lukas Czerner5f95d212012-03-19 23:03:19 -04004289 err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07004290
4291 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04004292 * transaction synchronous.
4293 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004294 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05004295 ext4_handle_sync(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07004296
Jan Kara9ddfc3d2008-07-11 19:27:31 -04004297 up_write(&EXT4_I(inode)->i_data_sem);
Eric Gouriouf6d2f6b2011-05-22 21:33:00 -04004298
4299out_stop:
Alex Tomasa86c6182006-10-11 01:21:03 -07004300 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004301 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07004302 * then we need to clear up the orphan record which we created above.
4303 * However, if this was a real unlink then we were called by
4304 * ext4_delete_inode(), and we allow that function to clean up the
4305 * orphan info for us.
4306 */
4307 if (inode->i_nlink)
4308 ext4_orphan_del(handle, inode);
4309
Solofo Ramangalahyef737722008-04-29 22:00:41 -04004310 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4311 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07004312 ext4_journal_stop(handle);
4313}
4314
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004315static void ext4_falloc_update_inode(struct inode *inode,
4316 int mode, loff_t new_size, int update_ctime)
4317{
4318 struct timespec now;
4319
4320 if (update_ctime) {
4321 now = current_fs_time(inode->i_sb);
4322 if (!timespec_equal(&inode->i_ctime, &now))
4323 inode->i_ctime = now;
4324 }
4325 /*
4326 * Update only when preallocation was requested beyond
4327 * the file size.
4328 */
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04004329 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
4330 if (new_size > i_size_read(inode))
4331 i_size_write(inode, new_size);
4332 if (new_size > EXT4_I(inode)->i_disksize)
4333 ext4_update_i_disksize(inode, new_size);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004334 } else {
4335 /*
4336 * Mark that we allocate beyond EOF so the subsequent truncate
4337 * can proceed even if the new size is the same as i_size.
4338 */
4339 if (new_size > i_size_read(inode))
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004340 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004341 }
4342
4343}
4344
Amit Aroraa2df2a62007-07-17 21:42:41 -04004345/*
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004346 * preallocate space for a file. This implements ext4's fallocate file
Amit Aroraa2df2a62007-07-17 21:42:41 -04004347 * operation, which gets called from sys_fallocate system call.
4348 * For block-mapped files, posix_fallocate should fall back to the method
4349 * of writing zeroes to the required new blocks (the same behavior which is
4350 * expected for file systems which do not support fallocate() system call).
4351 */
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004352long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004353{
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004354 struct inode *inode = file->f_path.dentry->d_inode;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004355 handle_t *handle;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004356 loff_t new_size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004357 unsigned int max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004358 int ret = 0;
4359 int ret2 = 0;
4360 int retries = 0;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004361 int flags;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004362 struct ext4_map_blocks map;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004363 unsigned int credits, blkbits = inode->i_blkbits;
4364
4365 /*
4366 * currently supporting (pre)allocate mode for extent-based
4367 * files _only_
4368 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004369 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Amit Aroraa2df2a62007-07-17 21:42:41 -04004370 return -EOPNOTSUPP;
4371
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004372 /* Return error if mode is not supported */
4373 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
4374 return -EOPNOTSUPP;
4375
4376 if (mode & FALLOC_FL_PUNCH_HOLE)
4377 return ext4_punch_hole(file, offset, len);
4378
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004379 trace_ext4_fallocate_enter(inode, offset, len, mode);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004380 map.m_lblk = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004381 /*
4382 * We can't just convert len to max_blocks because
4383 * If blocksize = 4096 offset = 3072 and len = 2048
4384 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004385 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004386 - map.m_lblk;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004387 /*
Mingming Caof3bd1f32008-08-19 22:16:03 -04004388 * credits to insert 1 extent into extent tree
Amit Aroraa2df2a62007-07-17 21:42:41 -04004389 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04004390 credits = ext4_chunk_trans_blocks(inode, max_blocks);
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05004391 mutex_lock(&inode->i_mutex);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04004392 ret = inode_newsize_ok(inode, (len + offset));
4393 if (ret) {
4394 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004395 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04004396 return ret;
4397 }
Greg Harm3c6fe772011-10-31 18:41:47 -04004398 flags = EXT4_GET_BLOCKS_CREATE_UNINIT_EXT;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004399 if (mode & FALLOC_FL_KEEP_SIZE)
4400 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
Greg Harm3c6fe772011-10-31 18:41:47 -04004401 /*
4402 * Don't normalize the request if it can fit in one extent so
4403 * that it doesn't get unnecessarily split into multiple
4404 * extents.
4405 */
4406 if (len <= EXT_UNINIT_MAX_LEN << blkbits)
4407 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004408retry:
4409 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004410 map.m_lblk = map.m_lblk + ret;
4411 map.m_len = max_blocks = max_blocks - ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004412 handle = ext4_journal_start(inode, credits);
4413 if (IS_ERR(handle)) {
4414 ret = PTR_ERR(handle);
4415 break;
4416 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004417 ret = ext4_map_blocks(handle, inode, &map, flags);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05004418 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004419#ifdef EXT4FS_DEBUG
4420 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004421 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004422 "returned error inode#%lu, block=%u, "
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05004423 "max_blocks=%u", __func__,
Kazuya Mioa6371b62010-10-27 21:30:15 -04004424 inode->i_ino, map.m_lblk, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004425#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04004426 ext4_mark_inode_dirty(handle, inode);
4427 ret2 = ext4_journal_stop(handle);
4428 break;
4429 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004430 if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004431 blkbits) >> blkbits))
4432 new_size = offset + len;
4433 else
Utako Kusaka29ae07b2011-07-27 22:11:20 -04004434 new_size = ((loff_t) map.m_lblk + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004435
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004436 ext4_falloc_update_inode(inode, mode, new_size,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004437 (map.m_flags & EXT4_MAP_NEW));
Amit Aroraa2df2a62007-07-17 21:42:41 -04004438 ext4_mark_inode_dirty(handle, inode);
4439 ret2 = ext4_journal_stop(handle);
4440 if (ret2)
4441 break;
4442 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004443 if (ret == -ENOSPC &&
4444 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4445 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004446 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004447 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05004448 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004449 trace_ext4_fallocate_exit(inode, offset, max_blocks,
4450 ret > 0 ? ret2 : ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004451 return ret > 0 ? ret2 : ret;
4452}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004453
4454/*
Mingming Cao00314622009-09-28 15:49:08 -04004455 * This function convert a range of blocks to written extents
4456 * The caller of this function will pass the start offset and the size.
4457 * all unwritten extents within this range will be converted to
4458 * written extents.
4459 *
4460 * This function is called from the direct IO end io call back
4461 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05004462 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04004463 */
4464int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
Eric Sandeena1de02d2010-02-04 23:58:38 -05004465 ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04004466{
4467 handle_t *handle;
Mingming Cao00314622009-09-28 15:49:08 -04004468 unsigned int max_blocks;
4469 int ret = 0;
4470 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004471 struct ext4_map_blocks map;
Mingming Cao00314622009-09-28 15:49:08 -04004472 unsigned int credits, blkbits = inode->i_blkbits;
4473
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004474 map.m_lblk = offset >> blkbits;
Mingming Cao00314622009-09-28 15:49:08 -04004475 /*
4476 * We can't just convert len to max_blocks because
4477 * If blocksize = 4096 offset = 3072 and len = 2048
4478 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004479 max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
4480 map.m_lblk);
Mingming Cao00314622009-09-28 15:49:08 -04004481 /*
4482 * credits to insert 1 extent into extent tree
4483 */
4484 credits = ext4_chunk_trans_blocks(inode, max_blocks);
4485 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004486 map.m_lblk += ret;
4487 map.m_len = (max_blocks -= ret);
Mingming Cao00314622009-09-28 15:49:08 -04004488 handle = ext4_journal_start(inode, credits);
4489 if (IS_ERR(handle)) {
4490 ret = PTR_ERR(handle);
4491 break;
4492 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004493 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004494 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Mingming Cao00314622009-09-28 15:49:08 -04004495 if (ret <= 0) {
4496 WARN_ON(ret <= 0);
Theodore Ts'o92b97812012-03-19 23:41:49 -04004497 ext4_msg(inode->i_sb, KERN_ERR,
4498 "%s:%d: inode #%lu: block %u: len %u: "
4499 "ext4_ext_map_blocks returned %d",
4500 __func__, __LINE__, inode->i_ino, map.m_lblk,
4501 map.m_len, ret);
Mingming Cao00314622009-09-28 15:49:08 -04004502 }
4503 ext4_mark_inode_dirty(handle, inode);
4504 ret2 = ext4_journal_stop(handle);
4505 if (ret <= 0 || ret2 )
4506 break;
4507 }
4508 return ret > 0 ? ret2 : ret;
4509}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004510
Mingming Cao00314622009-09-28 15:49:08 -04004511/*
Eric Sandeen6873fa02008-10-07 00:46:36 -04004512 * Callback function called for each extent to gather FIEMAP information.
4513 */
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04004514static int ext4_ext_fiemap_cb(struct inode *inode, ext4_lblk_t next,
Eric Sandeen6873fa02008-10-07 00:46:36 -04004515 struct ext4_ext_cache *newex, struct ext4_extent *ex,
4516 void *data)
4517{
Eric Sandeen6873fa02008-10-07 00:46:36 -04004518 __u64 logical;
4519 __u64 physical;
4520 __u64 length;
4521 __u32 flags = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004522 int ret = 0;
4523 struct fiemap_extent_info *fieinfo = data;
4524 unsigned char blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004525
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004526 blksize_bits = inode->i_sb->s_blocksize_bits;
4527 logical = (__u64)newex->ec_block << blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004528
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05004529 if (newex->ec_start == 0) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004530 /*
4531 * No extent in extent-tree contains block @newex->ec_start,
4532 * then the block may stay in 1)a hole or 2)delayed-extent.
4533 *
4534 * Holes or delayed-extents are processed as follows.
4535 * 1. lookup dirty pages with specified range in pagecache.
4536 * If no page is got, then there is no delayed-extent and
4537 * return with EXT_CONTINUE.
4538 * 2. find the 1st mapped buffer,
4539 * 3. check if the mapped buffer is both in the request range
4540 * and a delayed buffer. If not, there is no delayed-extent,
4541 * then return.
4542 * 4. a delayed-extent is found, the extent will be collected.
4543 */
4544 ext4_lblk_t end = 0;
4545 pgoff_t last_offset;
4546 pgoff_t offset;
4547 pgoff_t index;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004548 pgoff_t start_index = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004549 struct page **pages = NULL;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004550 struct buffer_head *bh = NULL;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004551 struct buffer_head *head = NULL;
4552 unsigned int nr_pages = PAGE_SIZE / sizeof(struct page *);
4553
4554 pages = kmalloc(PAGE_SIZE, GFP_KERNEL);
4555 if (pages == NULL)
4556 return -ENOMEM;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004557
4558 offset = logical >> PAGE_SHIFT;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004559repeat:
4560 last_offset = offset;
4561 head = NULL;
4562 ret = find_get_pages_tag(inode->i_mapping, &offset,
4563 PAGECACHE_TAG_DIRTY, nr_pages, pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004564
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004565 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4566 /* First time, try to find a mapped buffer. */
4567 if (ret == 0) {
4568out:
4569 for (index = 0; index < ret; index++)
4570 page_cache_release(pages[index]);
4571 /* just a hole. */
4572 kfree(pages);
4573 return EXT_CONTINUE;
4574 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004575 index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004576
Yongqiang Yangb2213492011-05-24 11:36:58 -04004577next_page:
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004578 /* Try to find the 1st mapped buffer. */
Yongqiang Yangb2213492011-05-24 11:36:58 -04004579 end = ((__u64)pages[index]->index << PAGE_SHIFT) >>
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004580 blksize_bits;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004581 if (!page_has_buffers(pages[index]))
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004582 goto out;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004583 head = page_buffers(pages[index]);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004584 if (!head)
4585 goto out;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004586
Yongqiang Yangb2213492011-05-24 11:36:58 -04004587 index++;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004588 bh = head;
4589 do {
Yongqiang Yangb2213492011-05-24 11:36:58 -04004590 if (end >= newex->ec_block +
4591 newex->ec_len)
4592 /* The buffer is out of
4593 * the request range.
4594 */
4595 goto out;
4596
4597 if (buffer_mapped(bh) &&
4598 end >= newex->ec_block) {
4599 start_index = index - 1;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004600 /* get the 1st mapped buffer. */
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004601 goto found_mapped_buffer;
4602 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004603
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004604 bh = bh->b_this_page;
4605 end++;
4606 } while (bh != head);
4607
Yongqiang Yangb2213492011-05-24 11:36:58 -04004608 /* No mapped buffer in the range found in this page,
4609 * We need to look up next page.
4610 */
4611 if (index >= ret) {
4612 /* There is no page left, but we need to limit
4613 * newex->ec_len.
4614 */
4615 newex->ec_len = end - newex->ec_block;
4616 goto out;
4617 }
4618 goto next_page;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004619 } else {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004620 /*Find contiguous delayed buffers. */
4621 if (ret > 0 && pages[0]->index == last_offset)
4622 head = page_buffers(pages[0]);
4623 bh = head;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004624 index = 1;
4625 start_index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004626 }
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004627
4628found_mapped_buffer:
4629 if (bh != NULL && buffer_delay(bh)) {
4630 /* 1st or contiguous delayed buffer found. */
4631 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4632 /*
4633 * 1st delayed buffer found, record
4634 * the start of extent.
4635 */
4636 flags |= FIEMAP_EXTENT_DELALLOC;
4637 newex->ec_block = end;
4638 logical = (__u64)end << blksize_bits;
4639 }
4640 /* Find contiguous delayed buffers. */
4641 do {
4642 if (!buffer_delay(bh))
4643 goto found_delayed_extent;
4644 bh = bh->b_this_page;
4645 end++;
4646 } while (bh != head);
4647
Yongqiang Yangb2213492011-05-24 11:36:58 -04004648 for (; index < ret; index++) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004649 if (!page_has_buffers(pages[index])) {
4650 bh = NULL;
4651 break;
4652 }
4653 head = page_buffers(pages[index]);
4654 if (!head) {
4655 bh = NULL;
4656 break;
4657 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004658
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004659 if (pages[index]->index !=
Yongqiang Yangb2213492011-05-24 11:36:58 -04004660 pages[start_index]->index + index
4661 - start_index) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004662 /* Blocks are not contiguous. */
4663 bh = NULL;
4664 break;
4665 }
4666 bh = head;
4667 do {
4668 if (!buffer_delay(bh))
4669 /* Delayed-extent ends. */
4670 goto found_delayed_extent;
4671 bh = bh->b_this_page;
4672 end++;
4673 } while (bh != head);
4674 }
4675 } else if (!(flags & FIEMAP_EXTENT_DELALLOC))
4676 /* a hole found. */
4677 goto out;
4678
4679found_delayed_extent:
4680 newex->ec_len = min(end - newex->ec_block,
4681 (ext4_lblk_t)EXT_INIT_MAX_LEN);
4682 if (ret == nr_pages && bh != NULL &&
4683 newex->ec_len < EXT_INIT_MAX_LEN &&
4684 buffer_delay(bh)) {
4685 /* Have not collected an extent and continue. */
4686 for (index = 0; index < ret; index++)
4687 page_cache_release(pages[index]);
4688 goto repeat;
4689 }
4690
4691 for (index = 0; index < ret; index++)
4692 page_cache_release(pages[index]);
4693 kfree(pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004694 }
4695
4696 physical = (__u64)newex->ec_start << blksize_bits;
4697 length = (__u64)newex->ec_len << blksize_bits;
4698
4699 if (ex && ext4_ext_is_uninitialized(ex))
4700 flags |= FIEMAP_EXTENT_UNWRITTEN;
4701
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04004702 if (next == EXT_MAX_BLOCKS)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004703 flags |= FIEMAP_EXTENT_LAST;
4704
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004705 ret = fiemap_fill_next_extent(fieinfo, logical, physical,
Eric Sandeen6873fa02008-10-07 00:46:36 -04004706 length, flags);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004707 if (ret < 0)
4708 return ret;
4709 if (ret == 1)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004710 return EXT_BREAK;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004711 return EXT_CONTINUE;
4712}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004713/* fiemap flags we can handle specified here */
4714#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
4715
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004716static int ext4_xattr_fiemap(struct inode *inode,
4717 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004718{
4719 __u64 physical = 0;
4720 __u64 length;
4721 __u32 flags = FIEMAP_EXTENT_LAST;
4722 int blockbits = inode->i_sb->s_blocksize_bits;
4723 int error = 0;
4724
4725 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004726 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04004727 struct ext4_iloc iloc;
4728 int offset; /* offset of xattr in inode */
4729
4730 error = ext4_get_inode_loc(inode, &iloc);
4731 if (error)
4732 return error;
Jan Karaee324d32013-05-31 19:38:56 -04004733 physical = (__u64)iloc.bh->b_blocknr << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004734 offset = EXT4_GOOD_OLD_INODE_SIZE +
4735 EXT4_I(inode)->i_extra_isize;
4736 physical += offset;
4737 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
4738 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004739 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004740 } else { /* external block */
Jan Karaee324d32013-05-31 19:38:56 -04004741 physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004742 length = inode->i_sb->s_blocksize;
4743 }
4744
4745 if (physical)
4746 error = fiemap_fill_next_extent(fieinfo, 0, physical,
4747 length, flags);
4748 return (error < 0 ? error : 0);
4749}
4750
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004751/*
4752 * ext4_ext_punch_hole
4753 *
4754 * Punches a hole of "length" bytes in a file starting
4755 * at byte "offset"
4756 *
4757 * @inode: The inode of the file to punch a hole in
4758 * @offset: The starting byte offset of the hole
4759 * @length: The length of the hole
4760 *
4761 * Returns the number of blocks removed or negative on err
4762 */
4763int ext4_ext_punch_hole(struct file *file, loff_t offset, loff_t length)
4764{
4765 struct inode *inode = file->f_path.dentry->d_inode;
4766 struct super_block *sb = inode->i_sb;
Lukas Czerner5f95d212012-03-19 23:03:19 -04004767 ext4_lblk_t first_block, stop_block;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004768 struct address_space *mapping = inode->i_mapping;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004769 handle_t *handle;
Allison Hendersonba062082011-09-03 11:55:59 -04004770 loff_t first_page, last_page, page_len;
4771 loff_t first_page_offset, last_page_offset;
Lukas Czerner5f95d212012-03-19 23:03:19 -04004772 int credits, err = 0;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004773
Allison Henderson2be47512011-09-03 11:56:52 -04004774 /* No need to punch hole beyond i_size */
4775 if (offset >= inode->i_size)
4776 return 0;
4777
4778 /*
4779 * If the hole extends beyond i_size, set the hole
4780 * to end after the page that contains i_size
4781 */
4782 if (offset + length > inode->i_size) {
4783 length = inode->i_size +
4784 PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) -
4785 offset;
4786 }
4787
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004788 first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
4789 last_page = (offset + length) >> PAGE_CACHE_SHIFT;
4790
4791 first_page_offset = first_page << PAGE_CACHE_SHIFT;
4792 last_page_offset = last_page << PAGE_CACHE_SHIFT;
4793
4794 /*
4795 * Write out all dirty pages to avoid race conditions
4796 * Then release them.
4797 */
4798 if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
4799 err = filemap_write_and_wait_range(mapping,
Allison Henderson2be47512011-09-03 11:56:52 -04004800 offset, offset + length - 1);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004801
Allison Henderson2be47512011-09-03 11:56:52 -04004802 if (err)
4803 return err;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004804 }
4805
4806 /* Now release the pages */
4807 if (last_page_offset > first_page_offset) {
4808 truncate_inode_pages_range(mapping, first_page_offset,
4809 last_page_offset-1);
4810 }
4811
4812 /* finish any pending end_io work */
4813 ext4_flush_completed_IO(inode);
4814
4815 credits = ext4_writepage_trans_blocks(inode);
4816 handle = ext4_journal_start(inode, credits);
4817 if (IS_ERR(handle))
4818 return PTR_ERR(handle);
4819
4820 err = ext4_orphan_add(handle, inode);
4821 if (err)
4822 goto out;
4823
4824 /*
Allison Hendersonba062082011-09-03 11:55:59 -04004825 * Now we need to zero out the non-page-aligned data in the
4826 * pages at the start and tail of the hole, and unmap the buffer
4827 * heads for the block aligned regions of the page that were
4828 * completely zeroed.
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004829 */
Allison Hendersonba062082011-09-03 11:55:59 -04004830 if (first_page > last_page) {
4831 /*
4832 * If the file space being truncated is contained within a page
4833 * just zero out and unmap the middle of that page
4834 */
4835 err = ext4_discard_partial_page_buffers(handle,
4836 mapping, offset, length, 0);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004837
Allison Hendersonba062082011-09-03 11:55:59 -04004838 if (err)
4839 goto out;
4840 } else {
4841 /*
4842 * zero out and unmap the partial page that contains
4843 * the start of the hole
4844 */
4845 page_len = first_page_offset - offset;
4846 if (page_len > 0) {
4847 err = ext4_discard_partial_page_buffers(handle, mapping,
4848 offset, page_len, 0);
4849 if (err)
4850 goto out;
4851 }
4852
4853 /*
4854 * zero out and unmap the partial page that contains
4855 * the end of the hole
4856 */
4857 page_len = offset + length - last_page_offset;
4858 if (page_len > 0) {
4859 err = ext4_discard_partial_page_buffers(handle, mapping,
4860 last_page_offset, page_len, 0);
4861 if (err)
4862 goto out;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004863 }
4864 }
4865
Allison Henderson2be47512011-09-03 11:56:52 -04004866 /*
4867 * If i_size is contained in the last page, we need to
4868 * unmap and zero the partial page after i_size
4869 */
4870 if (inode->i_size >> PAGE_CACHE_SHIFT == last_page &&
4871 inode->i_size % PAGE_CACHE_SIZE != 0) {
4872
4873 page_len = PAGE_CACHE_SIZE -
4874 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4875
4876 if (page_len > 0) {
4877 err = ext4_discard_partial_page_buffers(handle,
4878 mapping, inode->i_size, page_len, 0);
4879
4880 if (err)
4881 goto out;
4882 }
4883 }
4884
Lukas Czerner5f95d212012-03-19 23:03:19 -04004885 first_block = (offset + sb->s_blocksize - 1) >>
4886 EXT4_BLOCK_SIZE_BITS(sb);
4887 stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
4888
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004889 /* If there are no blocks to remove, return now */
Lukas Czerner5f95d212012-03-19 23:03:19 -04004890 if (first_block >= stop_block)
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004891 goto out;
4892
4893 down_write(&EXT4_I(inode)->i_data_sem);
4894 ext4_ext_invalidate_cache(inode);
4895 ext4_discard_preallocations(inode);
4896
Lukas Czerner5f95d212012-03-19 23:03:19 -04004897 err = ext4_ext_remove_space(inode, first_block, stop_block - 1);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004898
Lukas Czerner5f95d212012-03-19 23:03:19 -04004899 ext4_ext_invalidate_cache(inode);
4900 ext4_discard_preallocations(inode);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004901
4902 if (IS_SYNC(inode))
4903 ext4_handle_sync(handle);
4904
4905 up_write(&EXT4_I(inode)->i_data_sem);
4906
4907out:
4908 ext4_orphan_del(handle, inode);
4909 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4910 ext4_mark_inode_dirty(handle, inode);
4911 ext4_journal_stop(handle);
4912 return err;
4913}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004914int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4915 __u64 start, __u64 len)
4916{
4917 ext4_lblk_t start_blk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004918 int error = 0;
4919
4920 /* fallback to generic here if not in extents fmt */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004921 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeen6873fa02008-10-07 00:46:36 -04004922 return generic_block_fiemap(inode, fieinfo, start, len,
4923 ext4_get_block);
4924
4925 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
4926 return -EBADR;
4927
4928 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
4929 error = ext4_xattr_fiemap(inode, fieinfo);
4930 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004931 ext4_lblk_t len_blks;
4932 __u64 last_blk;
4933
Eric Sandeen6873fa02008-10-07 00:46:36 -04004934 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004935 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04004936 if (last_blk >= EXT_MAX_BLOCKS)
4937 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004938 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004939
4940 /*
4941 * Walk the extent tree gathering extent information.
4942 * ext4_ext_fiemap_cb will push extents back to user.
4943 */
Eric Sandeen6873fa02008-10-07 00:46:36 -04004944 error = ext4_ext_walk_space(inode, start_blk, len_blks,
4945 ext4_ext_fiemap_cb, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004946 }
4947
4948 return error;
4949}