blob: eb63c7b8dfd254ffb9449812831c2d4a7d2635aa [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
32#include <linux/module.h>
33#include <linux/fs.h>
34#include <linux/time.h>
Mingming Caocd02ff02007-10-16 18:38:25 -040035#include <linux/jbd2.h>
Alex Tomasa86c6182006-10-11 01:21:03 -070036#include <linux/highuid.h>
37#include <linux/pagemap.h>
38#include <linux/quotaops.h>
39#include <linux/string.h>
40#include <linux/slab.h>
Amit Aroraa2df2a62007-07-17 21:42:41 -040041#include <linux/falloc.h>
Alex Tomasa86c6182006-10-11 01:21:03 -070042#include <asm/uaccess.h>
Eric Sandeen6873fa02008-10-07 00:46:36 -040043#include <linux/fiemap.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040044#include "ext4_jbd2.h"
45#include "ext4_extents.h"
Alex Tomasa86c6182006-10-11 01:21:03 -070046
Jiaying Zhang0562e0b2011-03-21 21:38:05 -040047#include <trace/events/ext4.h>
48
Allison Hendersond583fb82011-05-25 07:41:43 -040049static int ext4_split_extent(handle_t *handle,
50 struct inode *inode,
51 struct ext4_ext_path *path,
52 struct ext4_map_blocks *map,
53 int split_flag,
54 int flags);
55
Jan Kara487caee2009-08-17 22:17:20 -040056static int ext4_ext_truncate_extend_restart(handle_t *handle,
57 struct inode *inode,
58 int needed)
Alex Tomasa86c6182006-10-11 01:21:03 -070059{
60 int err;
61
Frank Mayhar03901312009-01-07 00:06:22 -050062 if (!ext4_handle_valid(handle))
63 return 0;
Alex Tomasa86c6182006-10-11 01:21:03 -070064 if (handle->h_buffer_credits > needed)
Shen Feng9102e4f2008-07-11 19:27:31 -040065 return 0;
66 err = ext4_journal_extend(handle, needed);
Theodore Ts'o0123c932008-08-01 20:57:54 -040067 if (err <= 0)
Shen Feng9102e4f2008-07-11 19:27:31 -040068 return err;
Jan Kara487caee2009-08-17 22:17:20 -040069 err = ext4_truncate_restart_trans(handle, inode, needed);
Dmitry Monakhov0617b832010-05-17 01:00:00 -040070 if (err == 0)
71 err = -EAGAIN;
Jan Kara487caee2009-08-17 22:17:20 -040072
73 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -070074}
75
76/*
77 * could return:
78 * - EROFS
79 * - ENOMEM
80 */
81static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
82 struct ext4_ext_path *path)
83{
84 if (path->p_bh) {
85 /* path points to block */
86 return ext4_journal_get_write_access(handle, path->p_bh);
87 }
88 /* path points to leaf/index in inode body */
89 /* we use in-core data, no need to protect them */
90 return 0;
91}
92
93/*
94 * could return:
95 * - EROFS
96 * - ENOMEM
97 * - EIO
98 */
99static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
100 struct ext4_ext_path *path)
101{
102 int err;
103 if (path->p_bh) {
104 /* path points to block */
Frank Mayhar03901312009-01-07 00:06:22 -0500105 err = ext4_handle_dirty_metadata(handle, inode, path->p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700106 } else {
107 /* path points to leaf/index in inode body */
108 err = ext4_mark_inode_dirty(handle, inode);
109 }
110 return err;
111}
112
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700113static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700114 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500115 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700116{
117 struct ext4_inode_info *ei = EXT4_I(inode);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700118 ext4_fsblk_t bg_start;
Valerie Clement74d34872008-02-15 13:43:07 -0500119 ext4_fsblk_t last_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700120 ext4_grpblk_t colour;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400121 ext4_group_t block_group;
122 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -0700123 int depth;
124
125 if (path) {
126 struct ext4_extent *ex;
127 depth = path->p_depth;
128
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500129 /*
130 * Try to predict block placement assuming that we are
131 * filling in a file which will eventually be
132 * non-sparse --- i.e., in the case of libbfd writing
133 * an ELF object sections out-of-order but in a way
134 * the eventually results in a contiguous object or
135 * executable file, or some database extending a table
136 * space file. However, this is actually somewhat
137 * non-ideal if we are writing a sparse file such as
138 * qemu or KVM writing a raw image file that is going
139 * to stay fairly sparse, since it will end up
140 * fragmenting the file system's free space. Maybe we
141 * should have some hueristics or some way to allow
142 * userspace to pass a hint to file system,
Tao Mab8d65682011-01-21 23:21:31 +0800143 * especially if the latter case turns out to be
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500144 * common.
145 */
Avantika Mathur7e028972006-12-06 20:41:33 -0800146 ex = path[depth].p_ext;
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500147 if (ex) {
148 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
149 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
150
151 if (block > ext_block)
152 return ext_pblk + (block - ext_block);
153 else
154 return ext_pblk - (ext_block - block);
155 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700156
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700157 /* it looks like index is empty;
158 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700159 if (path[depth].p_bh)
160 return path[depth].p_bh->b_blocknr;
161 }
162
163 /* OK. use inode's group */
Theodore Ts'oa4912122009-03-12 12:18:34 -0400164 block_group = ei->i_block_group;
165 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
166 /*
167 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
Theodore Ts'o60e66792010-05-17 07:00:00 -0400168 * block groups per flexgroup, reserve the first block
169 * group for directories and special files. Regular
Theodore Ts'oa4912122009-03-12 12:18:34 -0400170 * files will start at the second block group. This
Theodore Ts'o60e66792010-05-17 07:00:00 -0400171 * tends to speed up directory access and improves
Theodore Ts'oa4912122009-03-12 12:18:34 -0400172 * fsck times.
173 */
174 block_group &= ~(flex_size-1);
175 if (S_ISREG(inode->i_mode))
176 block_group++;
177 }
Akinobu Mita5661bd62010-03-03 23:53:39 -0500178 bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
Valerie Clement74d34872008-02-15 13:43:07 -0500179 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
180
Theodore Ts'oa4912122009-03-12 12:18:34 -0400181 /*
182 * If we are doing delayed allocation, we don't need take
183 * colour into account.
184 */
185 if (test_opt(inode->i_sb, DELALLOC))
186 return bg_start;
187
Valerie Clement74d34872008-02-15 13:43:07 -0500188 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
189 colour = (current->pid % 16) *
Alex Tomasa86c6182006-10-11 01:21:03 -0700190 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
Valerie Clement74d34872008-02-15 13:43:07 -0500191 else
192 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
Alex Tomasa86c6182006-10-11 01:21:03 -0700193 return bg_start + colour + block;
194}
195
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400196/*
197 * Allocation for a meta data block
198 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700199static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400200ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700201 struct ext4_ext_path *path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400202 struct ext4_extent *ex, int *err, unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700203{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700204 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700205
206 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Allison Henderson55f020d2011-05-25 07:41:26 -0400207 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
208 NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700209 return newblock;
210}
211
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400212static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700213{
214 int size;
215
216 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
217 / sizeof(struct ext4_extent);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400218 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100219#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400220 if (size > 6)
221 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700222#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400223 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700224 return size;
225}
226
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400227static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700228{
229 int size;
230
231 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
232 / sizeof(struct ext4_extent_idx);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400233 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100234#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400235 if (size > 5)
236 size = 5;
Alex Tomasa86c6182006-10-11 01:21:03 -0700237#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400238 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700239 return size;
240}
241
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400242static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700243{
244 int size;
245
246 size = sizeof(EXT4_I(inode)->i_data);
247 size -= sizeof(struct ext4_extent_header);
248 size /= sizeof(struct ext4_extent);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400249 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100250#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400251 if (size > 3)
252 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700253#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400254 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700255 return size;
256}
257
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400258static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700259{
260 int size;
261
262 size = sizeof(EXT4_I(inode)->i_data);
263 size -= sizeof(struct ext4_extent_header);
264 size /= sizeof(struct ext4_extent_idx);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400265 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100266#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400267 if (size > 4)
268 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700269#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400270 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700271 return size;
272}
273
Mingming Caod2a17632008-07-14 17:52:37 -0400274/*
275 * Calculate the number of metadata blocks needed
276 * to allocate @blocks
277 * Worse case is one block per extent
278 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -0500279int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -0400280{
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500281 struct ext4_inode_info *ei = EXT4_I(inode);
282 int idxs, num = 0;
Mingming Caod2a17632008-07-14 17:52:37 -0400283
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500284 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
285 / sizeof(struct ext4_extent_idx));
Mingming Caod2a17632008-07-14 17:52:37 -0400286
287 /*
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500288 * If the new delayed allocation block is contiguous with the
289 * previous da block, it can share index blocks with the
290 * previous block, so we only need to allocate a new index
291 * block every idxs leaf blocks. At ldxs**2 blocks, we need
292 * an additional index block, and at ldxs**3 blocks, yet
293 * another index blocks.
Mingming Caod2a17632008-07-14 17:52:37 -0400294 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500295 if (ei->i_da_metadata_calc_len &&
296 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
297 if ((ei->i_da_metadata_calc_len % idxs) == 0)
298 num++;
299 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
300 num++;
301 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
302 num++;
303 ei->i_da_metadata_calc_len = 0;
304 } else
305 ei->i_da_metadata_calc_len++;
306 ei->i_da_metadata_calc_last_lblock++;
307 return num;
308 }
Mingming Caod2a17632008-07-14 17:52:37 -0400309
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500310 /*
311 * In the worst case we need a new set of index blocks at
312 * every level of the inode's extent tree.
313 */
314 ei->i_da_metadata_calc_len = 1;
315 ei->i_da_metadata_calc_last_lblock = lblock;
316 return ext_depth(inode) + 1;
Mingming Caod2a17632008-07-14 17:52:37 -0400317}
318
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400319static int
320ext4_ext_max_entries(struct inode *inode, int depth)
321{
322 int max;
323
324 if (depth == ext_depth(inode)) {
325 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400326 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400327 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400328 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400329 } else {
330 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400331 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400332 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400333 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400334 }
335
336 return max;
337}
338
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400339static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
340{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400341 ext4_fsblk_t block = ext4_ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400342 int len = ext4_ext_get_actual_len(ext);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400343
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400344 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400345}
346
347static int ext4_valid_extent_idx(struct inode *inode,
348 struct ext4_extent_idx *ext_idx)
349{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400350 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400351
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400352 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400353}
354
355static int ext4_valid_extent_entries(struct inode *inode,
356 struct ext4_extent_header *eh,
357 int depth)
358{
359 struct ext4_extent *ext;
360 struct ext4_extent_idx *ext_idx;
361 unsigned short entries;
362 if (eh->eh_entries == 0)
363 return 1;
364
365 entries = le16_to_cpu(eh->eh_entries);
366
367 if (depth == 0) {
368 /* leaf entries */
369 ext = EXT_FIRST_EXTENT(eh);
370 while (entries) {
371 if (!ext4_valid_extent(inode, ext))
372 return 0;
373 ext++;
374 entries--;
375 }
376 } else {
377 ext_idx = EXT_FIRST_INDEX(eh);
378 while (entries) {
379 if (!ext4_valid_extent_idx(inode, ext_idx))
380 return 0;
381 ext_idx++;
382 entries--;
383 }
384 }
385 return 1;
386}
387
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400388static int __ext4_ext_check(const char *function, unsigned int line,
389 struct inode *inode, struct ext4_extent_header *eh,
390 int depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400391{
392 const char *error_msg;
393 int max = 0;
394
395 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
396 error_msg = "invalid magic";
397 goto corrupted;
398 }
399 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
400 error_msg = "unexpected eh_depth";
401 goto corrupted;
402 }
403 if (unlikely(eh->eh_max == 0)) {
404 error_msg = "invalid eh_max";
405 goto corrupted;
406 }
407 max = ext4_ext_max_entries(inode, depth);
408 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
409 error_msg = "too large eh_max";
410 goto corrupted;
411 }
412 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
413 error_msg = "invalid eh_entries";
414 goto corrupted;
415 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400416 if (!ext4_valid_extent_entries(inode, eh, depth)) {
417 error_msg = "invalid extent entries";
418 goto corrupted;
419 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400420 return 0;
421
422corrupted:
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400423 ext4_error_inode(inode, function, line, 0,
Theodore Ts'o24676da2010-05-16 21:00:00 -0400424 "bad header/extent: %s - magic %x, "
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400425 "entries %u, max %u(%u), depth %u(%u)",
Theodore Ts'o24676da2010-05-16 21:00:00 -0400426 error_msg, le16_to_cpu(eh->eh_magic),
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400427 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
428 max, le16_to_cpu(eh->eh_depth), depth);
429
430 return -EIO;
431}
432
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400433#define ext4_ext_check(inode, eh, depth) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400434 __ext4_ext_check(__func__, __LINE__, inode, eh, depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400435
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400436int ext4_ext_check_inode(struct inode *inode)
437{
438 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
439}
440
Alex Tomasa86c6182006-10-11 01:21:03 -0700441#ifdef EXT_DEBUG
442static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
443{
444 int k, l = path->p_depth;
445
446 ext_debug("path:");
447 for (k = 0; k <= l; k++, path++) {
448 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700449 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400450 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700451 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400452 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700453 le32_to_cpu(path->p_ext->ee_block),
Mingming553f9002009-09-18 13:34:55 -0400454 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400455 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400456 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700457 } else
458 ext_debug(" []");
459 }
460 ext_debug("\n");
461}
462
463static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
464{
465 int depth = ext_depth(inode);
466 struct ext4_extent_header *eh;
467 struct ext4_extent *ex;
468 int i;
469
470 if (!path)
471 return;
472
473 eh = path[depth].p_hdr;
474 ex = EXT_FIRST_EXTENT(eh);
475
Mingming553f9002009-09-18 13:34:55 -0400476 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
477
Alex Tomasa86c6182006-10-11 01:21:03 -0700478 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400479 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
480 ext4_ext_is_uninitialized(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400481 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700482 }
483 ext_debug("\n");
484}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400485
486static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
487 ext4_fsblk_t newblock, int level)
488{
489 int depth = ext_depth(inode);
490 struct ext4_extent *ex;
491
492 if (depth != level) {
493 struct ext4_extent_idx *idx;
494 idx = path[level].p_idx;
495 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
496 ext_debug("%d: move %d:%llu in new index %llu\n", level,
497 le32_to_cpu(idx->ei_block),
498 ext4_idx_pblock(idx),
499 newblock);
500 idx++;
501 }
502
503 return;
504 }
505
506 ex = path[depth].p_ext;
507 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
508 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
509 le32_to_cpu(ex->ee_block),
510 ext4_ext_pblock(ex),
511 ext4_ext_is_uninitialized(ex),
512 ext4_ext_get_actual_len(ex),
513 newblock);
514 ex++;
515 }
516}
517
Alex Tomasa86c6182006-10-11 01:21:03 -0700518#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400519#define ext4_ext_show_path(inode, path)
520#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400521#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700522#endif
523
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500524void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700525{
526 int depth = path->p_depth;
527 int i;
528
529 for (i = 0; i <= depth; i++, path++)
530 if (path->p_bh) {
531 brelse(path->p_bh);
532 path->p_bh = NULL;
533 }
534}
535
536/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700537 * ext4_ext_binsearch_idx:
538 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400539 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700540 */
541static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500542ext4_ext_binsearch_idx(struct inode *inode,
543 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700544{
545 struct ext4_extent_header *eh = path->p_hdr;
546 struct ext4_extent_idx *r, *l, *m;
547
Alex Tomasa86c6182006-10-11 01:21:03 -0700548
Eric Sandeenbba90742008-01-28 23:58:27 -0500549 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700550
551 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400552 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700553 while (l <= r) {
554 m = l + (r - l) / 2;
555 if (block < le32_to_cpu(m->ei_block))
556 r = m - 1;
557 else
558 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400559 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
560 m, le32_to_cpu(m->ei_block),
561 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700562 }
563
564 path->p_idx = l - 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700565 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400566 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700567
568#ifdef CHECK_BINSEARCH
569 {
570 struct ext4_extent_idx *chix, *ix;
571 int k;
572
573 chix = ix = EXT_FIRST_INDEX(eh);
574 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
575 if (k != 0 &&
576 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400577 printk(KERN_DEBUG "k=%d, ix=0x%p, "
578 "first=0x%p\n", k,
579 ix, EXT_FIRST_INDEX(eh));
580 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700581 le32_to_cpu(ix->ei_block),
582 le32_to_cpu(ix[-1].ei_block));
583 }
584 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400585 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700586 if (block < le32_to_cpu(ix->ei_block))
587 break;
588 chix = ix;
589 }
590 BUG_ON(chix != path->p_idx);
591 }
592#endif
593
594}
595
596/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700597 * ext4_ext_binsearch:
598 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400599 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700600 */
601static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500602ext4_ext_binsearch(struct inode *inode,
603 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700604{
605 struct ext4_extent_header *eh = path->p_hdr;
606 struct ext4_extent *r, *l, *m;
607
Alex Tomasa86c6182006-10-11 01:21:03 -0700608 if (eh->eh_entries == 0) {
609 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700610 * this leaf is empty:
611 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700612 */
613 return;
614 }
615
Eric Sandeenbba90742008-01-28 23:58:27 -0500616 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700617
618 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400619 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700620
621 while (l <= r) {
622 m = l + (r - l) / 2;
623 if (block < le32_to_cpu(m->ee_block))
624 r = m - 1;
625 else
626 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400627 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
628 m, le32_to_cpu(m->ee_block),
629 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700630 }
631
632 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400633 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400634 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400635 ext4_ext_pblock(path->p_ext),
Mingming553f9002009-09-18 13:34:55 -0400636 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400637 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700638
639#ifdef CHECK_BINSEARCH
640 {
641 struct ext4_extent *chex, *ex;
642 int k;
643
644 chex = ex = EXT_FIRST_EXTENT(eh);
645 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
646 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400647 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700648 if (block < le32_to_cpu(ex->ee_block))
649 break;
650 chex = ex;
651 }
652 BUG_ON(chex != path->p_ext);
653 }
654#endif
655
656}
657
658int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
659{
660 struct ext4_extent_header *eh;
661
662 eh = ext_inode_hdr(inode);
663 eh->eh_depth = 0;
664 eh->eh_entries = 0;
665 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400666 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700667 ext4_mark_inode_dirty(handle, inode);
668 ext4_ext_invalidate_cache(inode);
669 return 0;
670}
671
672struct ext4_ext_path *
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500673ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
674 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700675{
676 struct ext4_extent_header *eh;
677 struct buffer_head *bh;
678 short int depth, i, ppos = 0, alloc = 0;
679
680 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400681 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700682
683 /* account possible depth increase */
684 if (!path) {
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800685 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700686 GFP_NOFS);
687 if (!path)
688 return ERR_PTR(-ENOMEM);
689 alloc = 1;
690 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700691 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400692 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700693
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400694 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700695 /* walk through the tree */
696 while (i) {
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400697 int need_to_validate = 0;
698
Alex Tomasa86c6182006-10-11 01:21:03 -0700699 ext_debug("depth %d: num %d, max %d\n",
700 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400701
Alex Tomasa86c6182006-10-11 01:21:03 -0700702 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400703 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700704 path[ppos].p_depth = i;
705 path[ppos].p_ext = NULL;
706
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400707 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
708 if (unlikely(!bh))
Alex Tomasa86c6182006-10-11 01:21:03 -0700709 goto err;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400710 if (!bh_uptodate_or_lock(bh)) {
Jiaying Zhang0562e0b2011-03-21 21:38:05 -0400711 trace_ext4_ext_load_extent(inode, block,
712 path[ppos].p_block);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400713 if (bh_submit_read(bh) < 0) {
714 put_bh(bh);
715 goto err;
716 }
717 /* validate the extent entries */
718 need_to_validate = 1;
719 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700720 eh = ext_block_hdr(bh);
721 ppos++;
Frank Mayhar273df552010-03-02 11:46:09 -0500722 if (unlikely(ppos > depth)) {
723 put_bh(bh);
724 EXT4_ERROR_INODE(inode,
725 "ppos %d > depth %d", ppos, depth);
726 goto err;
727 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700728 path[ppos].p_bh = bh;
729 path[ppos].p_hdr = eh;
730 i--;
731
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400732 if (need_to_validate && ext4_ext_check(inode, eh, i))
Alex Tomasa86c6182006-10-11 01:21:03 -0700733 goto err;
734 }
735
736 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700737 path[ppos].p_ext = NULL;
738 path[ppos].p_idx = NULL;
739
Alex Tomasa86c6182006-10-11 01:21:03 -0700740 /* find extent */
741 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400742 /* if not an empty leaf */
743 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400744 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700745
746 ext4_ext_show_path(inode, path);
747
748 return path;
749
750err:
751 ext4_ext_drop_refs(path);
752 if (alloc)
753 kfree(path);
754 return ERR_PTR(-EIO);
755}
756
757/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700758 * ext4_ext_insert_index:
759 * insert new index [@logical;@ptr] into the block at @curp;
760 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700761 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400762static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
763 struct ext4_ext_path *curp,
764 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700765{
766 struct ext4_extent_idx *ix;
767 int len, err;
768
Avantika Mathur7e028972006-12-06 20:41:33 -0800769 err = ext4_ext_get_access(handle, inode, curp);
770 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700771 return err;
772
Frank Mayhar273df552010-03-02 11:46:09 -0500773 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
774 EXT4_ERROR_INODE(inode,
775 "logical %d == ei_block %d!",
776 logical, le32_to_cpu(curp->p_idx->ei_block));
777 return -EIO;
778 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700779 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
780 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
781 /* insert after */
782 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
783 len = (len - 1) * sizeof(struct ext4_extent_idx);
784 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400785 ext_debug("insert new index %d after: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700786 "move %d from 0x%p to 0x%p\n",
787 logical, ptr, len,
788 (curp->p_idx + 1), (curp->p_idx + 2));
789 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
790 }
791 ix = curp->p_idx + 1;
792 } else {
793 /* insert before */
794 len = len * sizeof(struct ext4_extent_idx);
795 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400796 ext_debug("insert new index %d before: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700797 "move %d from 0x%p to 0x%p\n",
798 logical, ptr, len,
799 curp->p_idx, (curp->p_idx + 1));
800 memmove(curp->p_idx + 1, curp->p_idx, len);
801 ix = curp->p_idx;
802 }
803
804 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700805 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400806 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700807
Frank Mayhar273df552010-03-02 11:46:09 -0500808 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
809 > le16_to_cpu(curp->p_hdr->eh_max))) {
810 EXT4_ERROR_INODE(inode,
Robin Donged7a7e12011-06-27 15:35:53 -0400811 "eh_entries %d > eh_max %d!",
812 le16_to_cpu(curp->p_hdr->eh_entries),
813 le16_to_cpu(curp->p_hdr->eh_max));
Frank Mayhar273df552010-03-02 11:46:09 -0500814 return -EIO;
815 }
816 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
817 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
818 return -EIO;
819 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700820
821 err = ext4_ext_dirty(handle, inode, curp);
822 ext4_std_error(inode->i_sb, err);
823
824 return err;
825}
826
827/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700828 * ext4_ext_split:
829 * inserts new subtree into the path, using free index entry
830 * at depth @at:
831 * - allocates all needed blocks (new leaf and all intermediate index blocks)
832 * - makes decision where to split
833 * - moves remaining extents and index entries (right to the split point)
834 * into the newly allocated blocks
835 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700836 */
837static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -0400838 unsigned int flags,
839 struct ext4_ext_path *path,
840 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -0700841{
842 struct buffer_head *bh = NULL;
843 int depth = ext_depth(inode);
844 struct ext4_extent_header *neh;
845 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -0700846 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700847 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700848 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700849 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700850 int err = 0;
851
852 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700853 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700854
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700855 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700856 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -0500857 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
858 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
859 return -EIO;
860 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700861 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
862 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700863 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700864 " 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 } else {
867 border = newext->ee_block;
868 ext_debug("leaf will be added."
869 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400870 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700871 }
872
873 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700874 * If error occurs, then we break processing
875 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700876 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700877 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700878 */
879
880 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700881 * Get array to track all allocated blocks.
882 * We need this to handle errors and free blocks
883 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700884 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800885 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700886 if (!ablocks)
887 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700888
889 /* allocate all needed blocks */
890 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
891 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400892 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400893 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -0700894 if (newblock == 0)
895 goto cleanup;
896 ablocks[a] = newblock;
897 }
898
899 /* initialize new leaf */
900 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -0500901 if (unlikely(newblock == 0)) {
902 EXT4_ERROR_INODE(inode, "newblock == 0!");
903 err = -EIO;
904 goto cleanup;
905 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700906 bh = sb_getblk(inode->i_sb, newblock);
907 if (!bh) {
908 err = -EIO;
909 goto cleanup;
910 }
911 lock_buffer(bh);
912
Avantika Mathur7e028972006-12-06 20:41:33 -0800913 err = ext4_journal_get_create_access(handle, bh);
914 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700915 goto cleanup;
916
917 neh = ext_block_hdr(bh);
918 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400919 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700920 neh->eh_magic = EXT4_EXT_MAGIC;
921 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700922
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700923 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -0500924 if (unlikely(path[depth].p_hdr->eh_entries !=
925 path[depth].p_hdr->eh_max)) {
926 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
927 path[depth].p_hdr->eh_entries,
928 path[depth].p_hdr->eh_max);
929 err = -EIO;
930 goto cleanup;
931 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700932 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400933 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
934 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -0700935 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400936 struct ext4_extent *ex;
937 ex = EXT_FIRST_EXTENT(neh);
938 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400939 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700940 }
941
942 set_buffer_uptodate(bh);
943 unlock_buffer(bh);
944
Frank Mayhar03901312009-01-07 00:06:22 -0500945 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800946 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700947 goto cleanup;
948 brelse(bh);
949 bh = NULL;
950
951 /* correct old leaf */
952 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800953 err = ext4_ext_get_access(handle, inode, path + depth);
954 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700955 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400956 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800957 err = ext4_ext_dirty(handle, inode, path + depth);
958 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700959 goto cleanup;
960
961 }
962
963 /* create intermediate indexes */
964 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -0500965 if (unlikely(k < 0)) {
966 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
967 err = -EIO;
968 goto cleanup;
969 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700970 if (k)
971 ext_debug("create %d intermediate indices\n", k);
972 /* insert new index into current index block */
973 /* current depth stored in i var */
974 i = depth - 1;
975 while (k--) {
976 oldblock = newblock;
977 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -0500978 bh = sb_getblk(inode->i_sb, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700979 if (!bh) {
980 err = -EIO;
981 goto cleanup;
982 }
983 lock_buffer(bh);
984
Avantika Mathur7e028972006-12-06 20:41:33 -0800985 err = ext4_journal_get_create_access(handle, bh);
986 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700987 goto cleanup;
988
989 neh = ext_block_hdr(bh);
990 neh->eh_entries = cpu_to_le16(1);
991 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400992 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700993 neh->eh_depth = cpu_to_le16(depth - i);
994 fidx = EXT_FIRST_INDEX(neh);
995 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700996 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700997
Eric Sandeenbba90742008-01-28 23:58:27 -0500998 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
999 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001000
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001001 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -05001002 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
1003 EXT_LAST_INDEX(path[i].p_hdr))) {
1004 EXT4_ERROR_INODE(inode,
1005 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1006 le32_to_cpu(path[i].p_ext->ee_block));
1007 err = -EIO;
1008 goto cleanup;
1009 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001010 /* start copy indexes */
1011 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
1012 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
1013 EXT_MAX_INDEX(path[i].p_hdr));
1014 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07001015 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -04001016 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -07001017 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001018 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001019 }
1020 set_buffer_uptodate(bh);
1021 unlock_buffer(bh);
1022
Frank Mayhar03901312009-01-07 00:06:22 -05001023 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001024 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001025 goto cleanup;
1026 brelse(bh);
1027 bh = NULL;
1028
1029 /* correct old index */
1030 if (m) {
1031 err = ext4_ext_get_access(handle, inode, path + i);
1032 if (err)
1033 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001034 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001035 err = ext4_ext_dirty(handle, inode, path + i);
1036 if (err)
1037 goto cleanup;
1038 }
1039
1040 i--;
1041 }
1042
1043 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001044 err = ext4_ext_insert_index(handle, inode, path + at,
1045 le32_to_cpu(border), newblock);
1046
1047cleanup:
1048 if (bh) {
1049 if (buffer_locked(bh))
1050 unlock_buffer(bh);
1051 brelse(bh);
1052 }
1053
1054 if (err) {
1055 /* free all allocated blocks in error case */
1056 for (i = 0; i < depth; i++) {
1057 if (!ablocks[i])
1058 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001059 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001060 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001061 }
1062 }
1063 kfree(ablocks);
1064
1065 return err;
1066}
1067
1068/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001069 * ext4_ext_grow_indepth:
1070 * implements tree growing procedure:
1071 * - allocates new block
1072 * - moves top-level data (index block or leaf) into the new block
1073 * - initializes new top-level, creating index that points to the
1074 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001075 */
1076static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001077 unsigned int flags,
1078 struct ext4_ext_path *path,
1079 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001080{
1081 struct ext4_ext_path *curp = path;
1082 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001083 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001084 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001085 int err = 0;
1086
Allison Henderson55f020d2011-05-25 07:41:26 -04001087 newblock = ext4_ext_new_meta_block(handle, inode, path,
1088 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001089 if (newblock == 0)
1090 return err;
1091
1092 bh = sb_getblk(inode->i_sb, newblock);
1093 if (!bh) {
1094 err = -EIO;
1095 ext4_std_error(inode->i_sb, err);
1096 return err;
1097 }
1098 lock_buffer(bh);
1099
Avantika Mathur7e028972006-12-06 20:41:33 -08001100 err = ext4_journal_get_create_access(handle, bh);
1101 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001102 unlock_buffer(bh);
1103 goto out;
1104 }
1105
1106 /* move top-level index/leaf into new block */
1107 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
1108
1109 /* set size of new block */
1110 neh = ext_block_hdr(bh);
1111 /* old root could have indexes or leaves
1112 * so calculate e_max right way */
1113 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001114 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001115 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001116 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001117 neh->eh_magic = EXT4_EXT_MAGIC;
1118 set_buffer_uptodate(bh);
1119 unlock_buffer(bh);
1120
Frank Mayhar03901312009-01-07 00:06:22 -05001121 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001122 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001123 goto out;
1124
1125 /* create index in new top-level index: num,max,pointer */
Avantika Mathur7e028972006-12-06 20:41:33 -08001126 err = ext4_ext_get_access(handle, inode, curp);
1127 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001128 goto out;
1129
1130 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001131 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001132 curp->p_hdr->eh_entries = cpu_to_le16(1);
1133 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
Dmitry Monakhove9f410b2007-07-18 09:09:15 -04001134
1135 if (path[0].p_hdr->eh_depth)
1136 curp->p_idx->ei_block =
1137 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
1138 else
1139 curp->p_idx->ei_block =
1140 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001141 ext4_idx_store_pblock(curp->p_idx, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001142
1143 neh = ext_inode_hdr(inode);
Mingming Cao2ae02102006-10-11 01:21:11 -07001144 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001145 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001146 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001147 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001148
1149 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
1150 err = ext4_ext_dirty(handle, inode, curp);
1151out:
1152 brelse(bh);
1153
1154 return err;
1155}
1156
1157/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001158 * ext4_ext_create_new_leaf:
1159 * finds empty index and adds new leaf.
1160 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001161 */
1162static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001163 unsigned int flags,
1164 struct ext4_ext_path *path,
1165 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001166{
1167 struct ext4_ext_path *curp;
1168 int depth, i, err = 0;
1169
1170repeat:
1171 i = depth = ext_depth(inode);
1172
1173 /* walk up to the tree and look for free index entry */
1174 curp = path + depth;
1175 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1176 i--;
1177 curp--;
1178 }
1179
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001180 /* we use already allocated block for index block,
1181 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001182 if (EXT_HAS_FREE_INDEX(curp)) {
1183 /* if we found index with free entry, then use that
1184 * entry: create all needed subtree and add new leaf */
Allison Henderson55f020d2011-05-25 07:41:26 -04001185 err = ext4_ext_split(handle, inode, flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001186 if (err)
1187 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001188
1189 /* refill path */
1190 ext4_ext_drop_refs(path);
1191 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001192 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1193 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001194 if (IS_ERR(path))
1195 err = PTR_ERR(path);
1196 } else {
1197 /* tree is full, time to grow in depth */
Allison Henderson55f020d2011-05-25 07:41:26 -04001198 err = ext4_ext_grow_indepth(handle, inode, flags,
1199 path, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001200 if (err)
1201 goto out;
1202
1203 /* refill path */
1204 ext4_ext_drop_refs(path);
1205 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001206 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1207 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001208 if (IS_ERR(path)) {
1209 err = PTR_ERR(path);
1210 goto out;
1211 }
1212
1213 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001214 * only first (depth 0 -> 1) produces free space;
1215 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001216 */
1217 depth = ext_depth(inode);
1218 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001219 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001220 goto repeat;
1221 }
1222 }
1223
1224out:
1225 return err;
1226}
1227
1228/*
Alex Tomas1988b512008-01-28 23:58:27 -05001229 * search the closest allocated block to the left for *logical
1230 * and returns it at @logical + it's physical address at @phys
1231 * if *logical is the smallest allocated block, the function
1232 * returns 0 at @phys
1233 * return value contains 0 (success) or error code
1234 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001235static int ext4_ext_search_left(struct inode *inode,
1236 struct ext4_ext_path *path,
1237 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001238{
1239 struct ext4_extent_idx *ix;
1240 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001241 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001242
Frank Mayhar273df552010-03-02 11:46:09 -05001243 if (unlikely(path == NULL)) {
1244 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1245 return -EIO;
1246 }
Alex Tomas1988b512008-01-28 23:58:27 -05001247 depth = path->p_depth;
1248 *phys = 0;
1249
1250 if (depth == 0 && path->p_ext == NULL)
1251 return 0;
1252
1253 /* usually extent in the path covers blocks smaller
1254 * then *logical, but it can be that extent is the
1255 * first one in the file */
1256
1257 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001258 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001259 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001260 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1261 EXT4_ERROR_INODE(inode,
1262 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1263 *logical, le32_to_cpu(ex->ee_block));
1264 return -EIO;
1265 }
Alex Tomas1988b512008-01-28 23:58:27 -05001266 while (--depth >= 0) {
1267 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001268 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1269 EXT4_ERROR_INODE(inode,
1270 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
1271 ix != NULL ? ix->ei_block : 0,
1272 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
1273 EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block : 0,
1274 depth);
1275 return -EIO;
1276 }
Alex Tomas1988b512008-01-28 23:58:27 -05001277 }
1278 return 0;
1279 }
1280
Frank Mayhar273df552010-03-02 11:46:09 -05001281 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1282 EXT4_ERROR_INODE(inode,
1283 "logical %d < ee_block %d + ee_len %d!",
1284 *logical, le32_to_cpu(ex->ee_block), ee_len);
1285 return -EIO;
1286 }
Alex Tomas1988b512008-01-28 23:58:27 -05001287
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001288 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001289 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001290 return 0;
1291}
1292
1293/*
1294 * search the closest allocated block to the right for *logical
1295 * and returns it at @logical + it's physical address at @phys
1296 * if *logical is the smallest allocated block, the function
1297 * returns 0 at @phys
1298 * return value contains 0 (success) or error code
1299 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001300static int ext4_ext_search_right(struct inode *inode,
1301 struct ext4_ext_path *path,
1302 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001303{
1304 struct buffer_head *bh = NULL;
1305 struct ext4_extent_header *eh;
1306 struct ext4_extent_idx *ix;
1307 struct ext4_extent *ex;
1308 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001309 int depth; /* Note, NOT eh_depth; depth from top of tree */
1310 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001311
Frank Mayhar273df552010-03-02 11:46:09 -05001312 if (unlikely(path == NULL)) {
1313 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1314 return -EIO;
1315 }
Alex Tomas1988b512008-01-28 23:58:27 -05001316 depth = path->p_depth;
1317 *phys = 0;
1318
1319 if (depth == 0 && path->p_ext == NULL)
1320 return 0;
1321
1322 /* usually extent in the path covers blocks smaller
1323 * then *logical, but it can be that extent is the
1324 * first one in the file */
1325
1326 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001327 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001328 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001329 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1330 EXT4_ERROR_INODE(inode,
1331 "first_extent(path[%d].p_hdr) != ex",
1332 depth);
1333 return -EIO;
1334 }
Alex Tomas1988b512008-01-28 23:58:27 -05001335 while (--depth >= 0) {
1336 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001337 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1338 EXT4_ERROR_INODE(inode,
1339 "ix != EXT_FIRST_INDEX *logical %d!",
1340 *logical);
1341 return -EIO;
1342 }
Alex Tomas1988b512008-01-28 23:58:27 -05001343 }
1344 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001345 *phys = ext4_ext_pblock(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001346 return 0;
1347 }
1348
Frank Mayhar273df552010-03-02 11:46:09 -05001349 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1350 EXT4_ERROR_INODE(inode,
1351 "logical %d < ee_block %d + ee_len %d!",
1352 *logical, le32_to_cpu(ex->ee_block), ee_len);
1353 return -EIO;
1354 }
Alex Tomas1988b512008-01-28 23:58:27 -05001355
1356 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1357 /* next allocated block in this leaf */
1358 ex++;
1359 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001360 *phys = ext4_ext_pblock(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001361 return 0;
1362 }
1363
1364 /* go up and search for index to the right */
1365 while (--depth >= 0) {
1366 ix = path[depth].p_idx;
1367 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001368 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001369 }
1370
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001371 /* we've gone up to the root and found no index to the right */
1372 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001373
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001374got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001375 /* we've found index to the right, let's
1376 * follow it and find the closest allocated
1377 * block to the right */
1378 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001379 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001380 while (++depth < path->p_depth) {
1381 bh = sb_bread(inode->i_sb, block);
1382 if (bh == NULL)
1383 return -EIO;
1384 eh = ext_block_hdr(bh);
Eric Sandeen395a87b2009-03-10 18:18:47 -04001385 /* subtract from p_depth to get proper eh_depth */
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001386 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001387 put_bh(bh);
1388 return -EIO;
1389 }
1390 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001391 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001392 put_bh(bh);
1393 }
1394
1395 bh = sb_bread(inode->i_sb, block);
1396 if (bh == NULL)
1397 return -EIO;
1398 eh = ext_block_hdr(bh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001399 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001400 put_bh(bh);
1401 return -EIO;
1402 }
1403 ex = EXT_FIRST_EXTENT(eh);
1404 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001405 *phys = ext4_ext_pblock(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001406 put_bh(bh);
1407 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001408}
1409
1410/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001411 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001412 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001413 * NOTE: it considers block number from index entry as
1414 * allocated block. Thus, index entries have to be consistent
1415 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001416 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001417static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001418ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1419{
1420 int depth;
1421
1422 BUG_ON(path == NULL);
1423 depth = path->p_depth;
1424
1425 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001426 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001427
1428 while (depth >= 0) {
1429 if (depth == path->p_depth) {
1430 /* leaf */
1431 if (path[depth].p_ext !=
1432 EXT_LAST_EXTENT(path[depth].p_hdr))
1433 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1434 } else {
1435 /* index */
1436 if (path[depth].p_idx !=
1437 EXT_LAST_INDEX(path[depth].p_hdr))
1438 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1439 }
1440 depth--;
1441 }
1442
Lukas Czernerf17722f2011-06-06 00:05:17 -04001443 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001444}
1445
1446/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001447 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001448 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001449 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001450static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
Andrew Morton63f57932006-10-11 01:21:24 -07001451 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001452{
1453 int depth;
1454
1455 BUG_ON(path == NULL);
1456 depth = path->p_depth;
1457
1458 /* zero-tree has no leaf blocks at all */
1459 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001460 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001461
1462 /* go to index block */
1463 depth--;
1464
1465 while (depth >= 0) {
1466 if (path[depth].p_idx !=
1467 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001468 return (ext4_lblk_t)
1469 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001470 depth--;
1471 }
1472
Lukas Czernerf17722f2011-06-06 00:05:17 -04001473 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001474}
1475
1476/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001477 * ext4_ext_correct_indexes:
1478 * if leaf gets modified and modified extent is first in the leaf,
1479 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001480 * TODO: do we need to correct tree in all cases?
1481 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001482static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001483 struct ext4_ext_path *path)
1484{
1485 struct ext4_extent_header *eh;
1486 int depth = ext_depth(inode);
1487 struct ext4_extent *ex;
1488 __le32 border;
1489 int k, err = 0;
1490
1491 eh = path[depth].p_hdr;
1492 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001493
1494 if (unlikely(ex == NULL || eh == NULL)) {
1495 EXT4_ERROR_INODE(inode,
1496 "ex %p == NULL or eh %p == NULL", ex, eh);
1497 return -EIO;
1498 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001499
1500 if (depth == 0) {
1501 /* there is no tree at all */
1502 return 0;
1503 }
1504
1505 if (ex != EXT_FIRST_EXTENT(eh)) {
1506 /* we correct tree if first leaf got modified only */
1507 return 0;
1508 }
1509
1510 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001511 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001512 */
1513 k = depth - 1;
1514 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001515 err = ext4_ext_get_access(handle, inode, path + k);
1516 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001517 return err;
1518 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001519 err = ext4_ext_dirty(handle, inode, path + k);
1520 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001521 return err;
1522
1523 while (k--) {
1524 /* change all left-side indexes */
1525 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1526 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001527 err = ext4_ext_get_access(handle, inode, path + k);
1528 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001529 break;
1530 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001531 err = ext4_ext_dirty(handle, inode, path + k);
1532 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001533 break;
1534 }
1535
1536 return err;
1537}
1538
Akira Fujita748de672009-06-17 19:24:03 -04001539int
Alex Tomasa86c6182006-10-11 01:21:03 -07001540ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1541 struct ext4_extent *ex2)
1542{
Amit Arora749269f2007-07-18 09:02:56 -04001543 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001544
1545 /*
1546 * Make sure that either both extents are uninitialized, or
1547 * both are _not_.
1548 */
1549 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1550 return 0;
1551
Amit Arora749269f2007-07-18 09:02:56 -04001552 if (ext4_ext_is_uninitialized(ex1))
1553 max_len = EXT_UNINIT_MAX_LEN;
1554 else
1555 max_len = EXT_INIT_MAX_LEN;
1556
Amit Aroraa2df2a62007-07-17 21:42:41 -04001557 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1558 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1559
1560 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001561 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001562 return 0;
1563
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001564 /*
1565 * To allow future support for preallocated extents to be added
1566 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001567 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001568 */
Amit Arora749269f2007-07-18 09:02:56 -04001569 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001570 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001571#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001572 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001573 return 0;
1574#endif
1575
Theodore Ts'obf89d162010-10-27 21:30:14 -04001576 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001577 return 1;
1578 return 0;
1579}
1580
1581/*
Amit Arora56055d32007-07-17 21:42:38 -04001582 * This function tries to merge the "ex" extent to the next extent in the tree.
1583 * It always tries to merge towards right. If you want to merge towards
1584 * left, pass "ex - 1" as argument instead of "ex".
1585 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1586 * 1 if they got merged.
1587 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001588static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001589 struct ext4_ext_path *path,
1590 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001591{
1592 struct ext4_extent_header *eh;
1593 unsigned int depth, len;
1594 int merge_done = 0;
1595 int uninitialized = 0;
1596
1597 depth = ext_depth(inode);
1598 BUG_ON(path[depth].p_hdr == NULL);
1599 eh = path[depth].p_hdr;
1600
1601 while (ex < EXT_LAST_EXTENT(eh)) {
1602 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1603 break;
1604 /* merge with next extent! */
1605 if (ext4_ext_is_uninitialized(ex))
1606 uninitialized = 1;
1607 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1608 + ext4_ext_get_actual_len(ex + 1));
1609 if (uninitialized)
1610 ext4_ext_mark_uninitialized(ex);
1611
1612 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1613 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1614 * sizeof(struct ext4_extent);
1615 memmove(ex + 1, ex + 2, len);
1616 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001617 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001618 merge_done = 1;
1619 WARN_ON(eh->eh_entries == 0);
1620 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001621 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001622 }
1623
1624 return merge_done;
1625}
1626
1627/*
Yongqiang Yang197217a2011-05-03 11:45:29 -04001628 * This function tries to merge the @ex extent to neighbours in the tree.
1629 * return 1 if merge left else 0.
1630 */
1631static int ext4_ext_try_to_merge(struct inode *inode,
1632 struct ext4_ext_path *path,
1633 struct ext4_extent *ex) {
1634 struct ext4_extent_header *eh;
1635 unsigned int depth;
1636 int merge_done = 0;
1637 int ret = 0;
1638
1639 depth = ext_depth(inode);
1640 BUG_ON(path[depth].p_hdr == NULL);
1641 eh = path[depth].p_hdr;
1642
1643 if (ex > EXT_FIRST_EXTENT(eh))
1644 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1645
1646 if (!merge_done)
1647 ret = ext4_ext_try_to_merge_right(inode, path, ex);
1648
1649 return ret;
1650}
1651
1652/*
Amit Arora25d14f92007-05-24 13:04:13 -04001653 * check if a portion of the "newext" extent overlaps with an
1654 * existing extent.
1655 *
1656 * If there is an overlap discovered, it updates the length of the newext
1657 * such that there will be no overlap, and then returns 1.
1658 * If there is no overlap found, it returns 0.
1659 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001660static unsigned int ext4_ext_check_overlap(struct inode *inode,
1661 struct ext4_extent *newext,
1662 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001663{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001664 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001665 unsigned int depth, len1;
1666 unsigned int ret = 0;
1667
1668 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001669 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001670 depth = ext_depth(inode);
1671 if (!path[depth].p_ext)
1672 goto out;
1673 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1674
1675 /*
1676 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001677 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001678 */
1679 if (b2 < b1) {
1680 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001681 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001682 goto out;
1683 }
1684
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001685 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001686 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001687 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001688 newext->ee_len = cpu_to_le16(len1);
1689 ret = 1;
1690 }
1691
1692 /* check for overlap */
1693 if (b1 + len1 > b2) {
1694 newext->ee_len = cpu_to_le16(b2 - b1);
1695 ret = 1;
1696 }
1697out:
1698 return ret;
1699}
1700
1701/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001702 * ext4_ext_insert_extent:
1703 * tries to merge requsted extent into the existing extent or
1704 * inserts requested extent as new one into the tree,
1705 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001706 */
1707int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1708 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04001709 struct ext4_extent *newext, int flag)
Alex Tomasa86c6182006-10-11 01:21:03 -07001710{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001711 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001712 struct ext4_extent *ex, *fex;
1713 struct ext4_extent *nearex; /* nearest extent */
1714 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001715 int depth, len, err;
1716 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001717 unsigned uninitialized = 0;
Allison Henderson55f020d2011-05-25 07:41:26 -04001718 int flags = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001719
Frank Mayhar273df552010-03-02 11:46:09 -05001720 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1721 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1722 return -EIO;
1723 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001724 depth = ext_depth(inode);
1725 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001726 if (unlikely(path[depth].p_hdr == NULL)) {
1727 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1728 return -EIO;
1729 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001730
1731 /* try to insert block into found extent and return */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001732 if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
Mingming Cao00314622009-09-28 15:49:08 -04001733 && ext4_can_extents_be_merged(inode, ex, newext)) {
Mingming553f9002009-09-18 13:34:55 -04001734 ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04001735 ext4_ext_is_uninitialized(newext),
1736 ext4_ext_get_actual_len(newext),
1737 le32_to_cpu(ex->ee_block),
1738 ext4_ext_is_uninitialized(ex),
1739 ext4_ext_get_actual_len(ex),
1740 ext4_ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001741 err = ext4_ext_get_access(handle, inode, path + depth);
1742 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001743 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001744
1745 /*
1746 * ext4_can_extents_be_merged should have checked that either
1747 * both extents are uninitialized, or both aren't. Thus we
1748 * need to check only one of them here.
1749 */
1750 if (ext4_ext_is_uninitialized(ex))
1751 uninitialized = 1;
1752 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1753 + ext4_ext_get_actual_len(newext));
1754 if (uninitialized)
1755 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001756 eh = path[depth].p_hdr;
1757 nearex = ex;
1758 goto merge;
1759 }
1760
1761repeat:
1762 depth = ext_depth(inode);
1763 eh = path[depth].p_hdr;
1764 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1765 goto has_space;
1766
1767 /* probably next leaf has space for us? */
1768 fex = EXT_LAST_EXTENT(eh);
1769 next = ext4_ext_next_leaf_block(inode, path);
1770 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001771 && next != EXT_MAX_BLOCKS) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001772 ext_debug("next leaf block - %d\n", next);
1773 BUG_ON(npath != NULL);
1774 npath = ext4_ext_find_extent(inode, next, NULL);
1775 if (IS_ERR(npath))
1776 return PTR_ERR(npath);
1777 BUG_ON(npath->p_depth != path->p_depth);
1778 eh = npath[depth].p_hdr;
1779 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001780 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001781 le16_to_cpu(eh->eh_entries));
1782 path = npath;
1783 goto repeat;
1784 }
1785 ext_debug("next leaf has no free space(%d,%d)\n",
1786 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1787 }
1788
1789 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001790 * There is no free space in the found leaf.
1791 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001792 */
Allison Henderson55f020d2011-05-25 07:41:26 -04001793 if (flag & EXT4_GET_BLOCKS_PUNCH_OUT_EXT)
1794 flags = EXT4_MB_USE_ROOT_BLOCKS;
1795 err = ext4_ext_create_new_leaf(handle, inode, flags, path, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001796 if (err)
1797 goto cleanup;
1798 depth = ext_depth(inode);
1799 eh = path[depth].p_hdr;
1800
1801has_space:
1802 nearex = path[depth].p_ext;
1803
Avantika Mathur7e028972006-12-06 20:41:33 -08001804 err = ext4_ext_get_access(handle, inode, path + depth);
1805 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001806 goto cleanup;
1807
1808 if (!nearex) {
1809 /* there is no extent in this leaf, create first one */
Mingming553f9002009-09-18 13:34:55 -04001810 ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001811 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001812 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001813 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001814 ext4_ext_get_actual_len(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001815 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1816 } else if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001817 > le32_to_cpu(nearex->ee_block)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001818/* BUG_ON(newext->ee_block == nearex->ee_block); */
1819 if (nearex != EXT_LAST_EXTENT(eh)) {
1820 len = EXT_MAX_EXTENT(eh) - nearex;
1821 len = (len - 1) * sizeof(struct ext4_extent);
1822 len = len < 0 ? 0 : len;
Mingming553f9002009-09-18 13:34:55 -04001823 ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001824 "move %d from 0x%p to 0x%p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001825 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001826 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001827 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001828 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001829 nearex, len, nearex + 1, nearex + 2);
1830 memmove(nearex + 2, nearex + 1, len);
1831 }
1832 path[depth].p_ext = nearex + 1;
1833 } else {
1834 BUG_ON(newext->ee_block == nearex->ee_block);
1835 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1836 len = len < 0 ? 0 : len;
Mingming553f9002009-09-18 13:34:55 -04001837 ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001838 "move %d from 0x%p to 0x%p\n",
1839 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001840 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001841 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001842 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001843 nearex, len, nearex + 1, nearex + 2);
1844 memmove(nearex + 1, nearex, len);
1845 path[depth].p_ext = nearex;
1846 }
1847
Marcin Slusarze8546d02008-04-17 10:38:59 -04001848 le16_add_cpu(&eh->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001849 nearex = path[depth].p_ext;
1850 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001851 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001852 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001853
1854merge:
1855 /* try to merge extents to the right */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001856 if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
Mingming Cao00314622009-09-28 15:49:08 -04001857 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001858
1859 /* try to merge extents to the left */
1860
1861 /* time to correct all indexes above */
1862 err = ext4_ext_correct_indexes(handle, inode, path);
1863 if (err)
1864 goto cleanup;
1865
1866 err = ext4_ext_dirty(handle, inode, path + depth);
1867
1868cleanup:
1869 if (npath) {
1870 ext4_ext_drop_refs(npath);
1871 kfree(npath);
1872 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001873 ext4_ext_invalidate_cache(inode);
1874 return err;
1875}
1876
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001877static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1878 ext4_lblk_t num, ext_prepare_callback func,
1879 void *cbdata)
Eric Sandeen6873fa02008-10-07 00:46:36 -04001880{
1881 struct ext4_ext_path *path = NULL;
1882 struct ext4_ext_cache cbex;
1883 struct ext4_extent *ex;
1884 ext4_lblk_t next, start = 0, end = 0;
1885 ext4_lblk_t last = block + num;
1886 int depth, exists, err = 0;
1887
1888 BUG_ON(func == NULL);
1889 BUG_ON(inode == NULL);
1890
Lukas Czernerf17722f2011-06-06 00:05:17 -04001891 while (block < last && block != EXT_MAX_BLOCKS) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04001892 num = last - block;
1893 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001894 down_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001895 path = ext4_ext_find_extent(inode, block, path);
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001896 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001897 if (IS_ERR(path)) {
1898 err = PTR_ERR(path);
1899 path = NULL;
1900 break;
1901 }
1902
1903 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05001904 if (unlikely(path[depth].p_hdr == NULL)) {
1905 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1906 err = -EIO;
1907 break;
1908 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04001909 ex = path[depth].p_ext;
1910 next = ext4_ext_next_allocated_block(path);
1911
1912 exists = 0;
1913 if (!ex) {
1914 /* there is no extent yet, so try to allocate
1915 * all requested space */
1916 start = block;
1917 end = block + num;
1918 } else if (le32_to_cpu(ex->ee_block) > block) {
1919 /* need to allocate space before found extent */
1920 start = block;
1921 end = le32_to_cpu(ex->ee_block);
1922 if (block + num < end)
1923 end = block + num;
1924 } else if (block >= le32_to_cpu(ex->ee_block)
1925 + ext4_ext_get_actual_len(ex)) {
1926 /* need to allocate space after found extent */
1927 start = block;
1928 end = block + num;
1929 if (end >= next)
1930 end = next;
1931 } else if (block >= le32_to_cpu(ex->ee_block)) {
1932 /*
1933 * some part of requested space is covered
1934 * by found extent
1935 */
1936 start = block;
1937 end = le32_to_cpu(ex->ee_block)
1938 + ext4_ext_get_actual_len(ex);
1939 if (block + num < end)
1940 end = block + num;
1941 exists = 1;
1942 } else {
1943 BUG();
1944 }
1945 BUG_ON(end <= start);
1946
1947 if (!exists) {
1948 cbex.ec_block = start;
1949 cbex.ec_len = end - start;
1950 cbex.ec_start = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04001951 } else {
1952 cbex.ec_block = le32_to_cpu(ex->ee_block);
1953 cbex.ec_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001954 cbex.ec_start = ext4_ext_pblock(ex);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001955 }
1956
Frank Mayhar273df552010-03-02 11:46:09 -05001957 if (unlikely(cbex.ec_len == 0)) {
1958 EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
1959 err = -EIO;
1960 break;
1961 }
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04001962 err = func(inode, next, &cbex, ex, cbdata);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001963 ext4_ext_drop_refs(path);
1964
1965 if (err < 0)
1966 break;
1967
1968 if (err == EXT_REPEAT)
1969 continue;
1970 else if (err == EXT_BREAK) {
1971 err = 0;
1972 break;
1973 }
1974
1975 if (ext_depth(inode) != depth) {
1976 /* depth was changed. we have to realloc path */
1977 kfree(path);
1978 path = NULL;
1979 }
1980
1981 block = cbex.ec_block + cbex.ec_len;
1982 }
1983
1984 if (path) {
1985 ext4_ext_drop_refs(path);
1986 kfree(path);
1987 }
1988
1989 return err;
1990}
1991
Avantika Mathur09b88252006-12-06 20:41:36 -08001992static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001993ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05001994 __u32 len, ext4_fsblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07001995{
1996 struct ext4_ext_cache *cex;
1997 BUG_ON(len == 0);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001998 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001999 cex = &EXT4_I(inode)->i_cached_extent;
Alex Tomasa86c6182006-10-11 01:21:03 -07002000 cex->ec_block = block;
2001 cex->ec_len = len;
2002 cex->ec_start = start;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002003 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07002004}
2005
2006/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002007 * ext4_ext_put_gap_in_cache:
2008 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07002009 * and cache this gap
2010 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002011static void
Alex Tomasa86c6182006-10-11 01:21:03 -07002012ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002013 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07002014{
2015 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002016 unsigned long len;
2017 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07002018 struct ext4_extent *ex;
2019
2020 ex = path[depth].p_ext;
2021 if (ex == NULL) {
2022 /* there is no extent yet, so gap is [0;-] */
2023 lblock = 0;
Lukas Czernerf17722f2011-06-06 00:05:17 -04002024 len = EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07002025 ext_debug("cache gap(whole file):");
2026 } else if (block < le32_to_cpu(ex->ee_block)) {
2027 lblock = block;
2028 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05002029 ext_debug("cache gap(before): %u [%u:%u]",
2030 block,
2031 le32_to_cpu(ex->ee_block),
2032 ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002033 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002034 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002035 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002036 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002037 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002038
2039 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05002040 ext_debug("cache gap(after): [%u:%u] %u",
2041 le32_to_cpu(ex->ee_block),
2042 ext4_ext_get_actual_len(ex),
2043 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002044 BUG_ON(next == lblock);
2045 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07002046 } else {
2047 lblock = len = 0;
2048 BUG();
2049 }
2050
Eric Sandeenbba90742008-01-28 23:58:27 -05002051 ext_debug(" -> %u:%lu\n", lblock, len);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002052 ext4_ext_put_in_cache(inode, lblock, len, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002053}
2054
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002055/*
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002056 * ext4_ext_in_cache()
2057 * Checks to see if the given block is in the cache.
2058 * If it is, the cached extent is stored in the given
2059 * cache extent pointer. If the cached extent is a hole,
2060 * this routine should be used instead of
2061 * ext4_ext_in_cache if the calling function needs to
2062 * know the size of the hole.
2063 *
2064 * @inode: The files inode
2065 * @block: The block to look for in the cache
2066 * @ex: Pointer where the cached extent will be stored
2067 * if it contains block
2068 *
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002069 * Return 0 if cache is invalid; 1 if the cache is valid
2070 */
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002071static int ext4_ext_check_cache(struct inode *inode, ext4_lblk_t block,
2072 struct ext4_ext_cache *ex){
Alex Tomasa86c6182006-10-11 01:21:03 -07002073 struct ext4_ext_cache *cex;
Vivek Haldar77f41352011-05-22 21:24:16 -04002074 struct ext4_sb_info *sbi;
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002075 int ret = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002076
Theodore Ts'o60e66792010-05-17 07:00:00 -04002077 /*
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002078 * We borrow i_block_reservation_lock to protect i_cached_extent
2079 */
2080 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07002081 cex = &EXT4_I(inode)->i_cached_extent;
Vivek Haldar77f41352011-05-22 21:24:16 -04002082 sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002083
2084 /* has cache valid data? */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002085 if (cex->ec_len == 0)
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002086 goto errout;
Alex Tomasa86c6182006-10-11 01:21:03 -07002087
Akinobu Mita731eb1a2010-03-03 23:55:01 -05002088 if (in_range(block, cex->ec_block, cex->ec_len)) {
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002089 memcpy(ex, cex, sizeof(struct ext4_ext_cache));
Eric Sandeenbba90742008-01-28 23:58:27 -05002090 ext_debug("%u cached by %u:%u:%llu\n",
2091 block,
2092 cex->ec_block, cex->ec_len, cex->ec_start);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002093 ret = 1;
Alex Tomasa86c6182006-10-11 01:21:03 -07002094 }
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002095errout:
Vivek Haldar77f41352011-05-22 21:24:16 -04002096 if (!ret)
2097 sbi->extent_cache_misses++;
2098 else
2099 sbi->extent_cache_hits++;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002100 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
2101 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07002102}
2103
2104/*
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002105 * ext4_ext_in_cache()
2106 * Checks to see if the given block is in the cache.
2107 * If it is, the cached extent is stored in the given
2108 * extent pointer.
2109 *
2110 * @inode: The files inode
2111 * @block: The block to look for in the cache
2112 * @ex: Pointer where the cached extent will be stored
2113 * if it contains block
2114 *
2115 * Return 0 if cache is invalid; 1 if the cache is valid
2116 */
2117static int
2118ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
2119 struct ext4_extent *ex)
2120{
2121 struct ext4_ext_cache cex;
2122 int ret = 0;
2123
2124 if (ext4_ext_check_cache(inode, block, &cex)) {
2125 ex->ee_block = cpu_to_le32(cex.ec_block);
2126 ext4_ext_store_pblock(ex, cex.ec_start);
2127 ex->ee_len = cpu_to_le16(cex.ec_len);
2128 ret = 1;
2129 }
2130
2131 return ret;
2132}
2133
2134
2135/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002136 * ext4_ext_rm_idx:
2137 * removes index from the index block.
2138 * It's used in truncate case only, thus all requests are for
2139 * last index in the block only.
Alex Tomasa86c6182006-10-11 01:21:03 -07002140 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002141static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07002142 struct ext4_ext_path *path)
2143{
Alex Tomasa86c6182006-10-11 01:21:03 -07002144 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002145 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002146
2147 /* free index block */
2148 path--;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002149 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002150 if (unlikely(path->p_hdr->eh_entries == 0)) {
2151 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2152 return -EIO;
2153 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002154 err = ext4_ext_get_access(handle, inode, path);
2155 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002156 return err;
Marcin Slusarze8546d02008-04-17 10:38:59 -04002157 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002158 err = ext4_ext_dirty(handle, inode, path);
2159 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002160 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002161 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Peter Huewe7dc57612011-02-21 21:01:42 -05002162 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002163 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Alex Tomasa86c6182006-10-11 01:21:03 -07002164 return err;
2165}
2166
2167/*
Mingming Caoee12b632008-08-19 22:16:05 -04002168 * ext4_ext_calc_credits_for_single_extent:
2169 * This routine returns max. credits that needed to insert an extent
2170 * to the extent tree.
2171 * When pass the actual path, the caller should calculate credits
2172 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002173 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002174int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002175 struct ext4_ext_path *path)
2176{
Alex Tomasa86c6182006-10-11 01:21:03 -07002177 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002178 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002179 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002180
Alex Tomasa86c6182006-10-11 01:21:03 -07002181 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002182 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002183 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2184
2185 /*
2186 * There are some space in the leaf tree, no
2187 * need to account for leaf block credit
2188 *
2189 * bitmaps and block group descriptor blocks
2190 * and other metadat blocks still need to be
2191 * accounted.
2192 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002193 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002194 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002195 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002196 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002197 }
2198
Mingming Cao525f4ed2008-08-19 22:15:58 -04002199 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002200}
Alex Tomasa86c6182006-10-11 01:21:03 -07002201
Mingming Caoee12b632008-08-19 22:16:05 -04002202/*
2203 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2204 *
2205 * if nrblocks are fit in a single extent (chunk flag is 1), then
2206 * in the worse case, each tree level index/leaf need to be changed
2207 * if the tree split due to insert a new extent, then the old tree
2208 * index/leaf need to be updated too
2209 *
2210 * If the nrblocks are discontiguous, they could cause
2211 * the whole tree split more than once, but this is really rare.
2212 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002213int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoee12b632008-08-19 22:16:05 -04002214{
2215 int index;
2216 int depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002217
Mingming Caoee12b632008-08-19 22:16:05 -04002218 if (chunk)
2219 index = depth * 2;
2220 else
2221 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002222
Mingming Caoee12b632008-08-19 22:16:05 -04002223 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002224}
2225
2226static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2227 struct ext4_extent *ex,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002228 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002229{
Amit Aroraa2df2a62007-07-17 21:42:41 -04002230 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe6362602009-11-23 07:17:05 -05002231 int flags = EXT4_FREE_BLOCKS_FORGET;
Alex Tomasa86c6182006-10-11 01:21:03 -07002232
Alex Tomasc9de5602008-01-29 00:19:52 -05002233 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
Theodore Ts'oe6362602009-11-23 07:17:05 -05002234 flags |= EXT4_FREE_BLOCKS_METADATA;
Alex Tomasa86c6182006-10-11 01:21:03 -07002235#ifdef EXTENTS_STATS
2236 {
2237 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002238 spin_lock(&sbi->s_ext_stats_lock);
2239 sbi->s_ext_blocks += ee_len;
2240 sbi->s_ext_extents++;
2241 if (ee_len < sbi->s_ext_min)
2242 sbi->s_ext_min = ee_len;
2243 if (ee_len > sbi->s_ext_max)
2244 sbi->s_ext_max = ee_len;
2245 if (ext_depth(inode) > sbi->s_depth_max)
2246 sbi->s_depth_max = ext_depth(inode);
2247 spin_unlock(&sbi->s_ext_stats_lock);
2248 }
2249#endif
2250 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002251 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002252 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002253 ext4_lblk_t num;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002254 ext4_fsblk_t start;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002255
Amit Aroraa2df2a62007-07-17 21:42:41 -04002256 num = le32_to_cpu(ex->ee_block) + ee_len - from;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002257 start = ext4_ext_pblock(ex) + ee_len - num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002258 ext_debug("free last %u blocks starting %llu\n", num, start);
Peter Huewe7dc57612011-02-21 21:01:42 -05002259 ext4_free_blocks(handle, inode, NULL, start, num, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07002260 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002261 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002262 /* head removal */
2263 ext4_lblk_t num;
2264 ext4_fsblk_t start;
2265
2266 num = to - from;
2267 start = ext4_ext_pblock(ex);
2268
2269 ext_debug("free first %u blocks starting %llu\n", num, start);
2270 ext4_free_blocks(handle, inode, 0, start, num, flags);
2271
Alex Tomasa86c6182006-10-11 01:21:03 -07002272 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002273 printk(KERN_INFO "strange request: removal(2) "
2274 "%u-%u from %u:%u\n",
2275 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002276 }
2277 return 0;
2278}
2279
Allison Hendersond583fb82011-05-25 07:41:43 -04002280
2281/*
2282 * ext4_ext_rm_leaf() Removes the extents associated with the
2283 * blocks appearing between "start" and "end", and splits the extents
2284 * if "start" and "end" appear in the same extent
2285 *
2286 * @handle: The journal handle
2287 * @inode: The files inode
2288 * @path: The path to the leaf
2289 * @start: The first block to remove
2290 * @end: The last block to remove
2291 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002292static int
2293ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Allison Hendersond583fb82011-05-25 07:41:43 -04002294 struct ext4_ext_path *path, ext4_lblk_t start,
2295 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002296{
2297 int err = 0, correct_index = 0;
2298 int depth = ext_depth(inode), credits;
2299 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002300 ext4_lblk_t a, b, block;
2301 unsigned num;
2302 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002303 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002304 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002305 struct ext4_extent *ex;
Allison Hendersond583fb82011-05-25 07:41:43 -04002306 struct ext4_map_blocks map;
Alex Tomasa86c6182006-10-11 01:21:03 -07002307
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002308 /* the header must be checked already in ext4_ext_remove_space() */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002309 ext_debug("truncate since %u in leaf\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002310 if (!path[depth].p_hdr)
2311 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2312 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002313 if (unlikely(path[depth].p_hdr == NULL)) {
2314 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2315 return -EIO;
2316 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002317 /* find where to start removing */
2318 ex = EXT_LAST_EXTENT(eh);
2319
2320 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002321 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002322
2323 while (ex >= EXT_FIRST_EXTENT(eh) &&
2324 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002325
2326 if (ext4_ext_is_uninitialized(ex))
2327 uninitialized = 1;
2328 else
2329 uninitialized = 0;
2330
Mingming553f9002009-09-18 13:34:55 -04002331 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2332 uninitialized, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002333 path[depth].p_ext = ex;
2334
2335 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002336 b = ex_ee_block+ex_ee_len - 1 < end ?
2337 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002338
2339 ext_debug(" border %u:%u\n", a, b);
2340
Allison Hendersond583fb82011-05-25 07:41:43 -04002341 /* If this extent is beyond the end of the hole, skip it */
2342 if (end <= ex_ee_block) {
2343 ex--;
2344 ex_ee_block = le32_to_cpu(ex->ee_block);
2345 ex_ee_len = ext4_ext_get_actual_len(ex);
2346 continue;
2347 } else if (a != ex_ee_block &&
2348 b != ex_ee_block + ex_ee_len - 1) {
2349 /*
2350 * If this is a truncate, then this condition should
2351 * never happen because at least one of the end points
2352 * needs to be on the edge of the extent.
2353 */
Lukas Czernerf17722f2011-06-06 00:05:17 -04002354 if (end == EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002355 ext_debug(" bad truncate %u:%u\n",
2356 start, end);
2357 block = 0;
2358 num = 0;
2359 err = -EIO;
2360 goto out;
2361 }
2362 /*
2363 * else this is a hole punch, so the extent needs to
2364 * be split since neither edge of the hole is on the
2365 * extent edge
2366 */
2367 else{
2368 map.m_pblk = ext4_ext_pblock(ex);
2369 map.m_lblk = ex_ee_block;
2370 map.m_len = b - ex_ee_block;
2371
2372 err = ext4_split_extent(handle,
2373 inode, path, &map, 0,
2374 EXT4_GET_BLOCKS_PUNCH_OUT_EXT |
2375 EXT4_GET_BLOCKS_PRE_IO);
2376
2377 if (err < 0)
2378 goto out;
2379
2380 ex_ee_len = ext4_ext_get_actual_len(ex);
2381
2382 b = ex_ee_block+ex_ee_len - 1 < end ?
2383 ex_ee_block+ex_ee_len - 1 : end;
2384
2385 /* Then remove tail of this extent */
2386 block = ex_ee_block;
2387 num = a - block;
2388 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002389 } else if (a != ex_ee_block) {
2390 /* remove tail of the extent */
2391 block = ex_ee_block;
2392 num = a - block;
2393 } else if (b != ex_ee_block + ex_ee_len - 1) {
2394 /* remove head of the extent */
Allison Hendersond583fb82011-05-25 07:41:43 -04002395 block = b;
2396 num = ex_ee_block + ex_ee_len - b;
2397
2398 /*
2399 * If this is a truncate, this condition
2400 * should never happen
2401 */
Lukas Czernerf17722f2011-06-06 00:05:17 -04002402 if (end == EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002403 ext_debug(" bad truncate %u:%u\n",
2404 start, end);
2405 err = -EIO;
2406 goto out;
2407 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002408 } else {
2409 /* remove whole extent: excellent! */
2410 block = ex_ee_block;
2411 num = 0;
Allison Hendersond583fb82011-05-25 07:41:43 -04002412 if (a != ex_ee_block) {
2413 ext_debug(" bad truncate %u:%u\n",
2414 start, end);
2415 err = -EIO;
2416 goto out;
2417 }
2418
2419 if (b != ex_ee_block + ex_ee_len - 1) {
2420 ext_debug(" bad truncate %u:%u\n",
2421 start, end);
2422 err = -EIO;
2423 goto out;
2424 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002425 }
2426
Theodore Ts'o34071da2008-08-01 21:59:19 -04002427 /*
2428 * 3 for leaf, sb, and inode plus 2 (bmap and group
2429 * descriptor) for each block group; assume two block
2430 * groups plus ex_ee_len/blocks_per_block_group for
2431 * the worst case
2432 */
2433 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002434 if (ex == EXT_FIRST_EXTENT(eh)) {
2435 correct_index = 1;
2436 credits += (ext_depth(inode)) + 1;
2437 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002438 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002439
Jan Kara487caee2009-08-17 22:17:20 -04002440 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
Shen Feng9102e4f2008-07-11 19:27:31 -04002441 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002442 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002443
2444 err = ext4_ext_get_access(handle, inode, path + depth);
2445 if (err)
2446 goto out;
2447
2448 err = ext4_remove_blocks(handle, inode, ex, a, b);
2449 if (err)
2450 goto out;
2451
2452 if (num == 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002453 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002454 ext4_ext_store_pblock(ex, 0);
Allison Hendersond583fb82011-05-25 07:41:43 -04002455 } else if (block != ex_ee_block) {
2456 /*
2457 * If this was a head removal, then we need to update
2458 * the physical block since it is now at a different
2459 * location
2460 */
2461 ext4_ext_store_pblock(ex, ext4_ext_pblock(ex) + (b-a));
Alex Tomasa86c6182006-10-11 01:21:03 -07002462 }
2463
2464 ex->ee_block = cpu_to_le32(block);
2465 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002466 /*
2467 * Do not mark uninitialized if all the blocks in the
2468 * extent have been removed.
2469 */
2470 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002471 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002472
2473 err = ext4_ext_dirty(handle, inode, path + depth);
2474 if (err)
2475 goto out;
2476
Allison Hendersond583fb82011-05-25 07:41:43 -04002477 /*
2478 * If the extent was completely released,
2479 * we need to remove it from the leaf
2480 */
2481 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002482 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002483 /*
2484 * For hole punching, we need to scoot all the
2485 * extents up when an extent is removed so that
2486 * we dont have blank extents in the middle
2487 */
2488 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2489 sizeof(struct ext4_extent));
2490
2491 /* Now get rid of the one at the end */
2492 memset(EXT_LAST_EXTENT(eh), 0,
2493 sizeof(struct ext4_extent));
2494 }
2495 le16_add_cpu(&eh->eh_entries, -1);
2496 }
2497
Mingming Cao2ae02102006-10-11 01:21:11 -07002498 ext_debug("new extent: %u:%u:%llu\n", block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002499 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002500 ex--;
2501 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002502 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002503 }
2504
2505 if (correct_index && eh->eh_entries)
2506 err = ext4_ext_correct_indexes(handle, inode, path);
2507
2508 /* if this leaf is free, then we should
2509 * remove it from index block above */
2510 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2511 err = ext4_ext_rm_idx(handle, inode, path + depth);
2512
2513out:
2514 return err;
2515}
2516
2517/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002518 * ext4_ext_more_to_rm:
2519 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002520 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002521static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002522ext4_ext_more_to_rm(struct ext4_ext_path *path)
2523{
2524 BUG_ON(path->p_idx == NULL);
2525
2526 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2527 return 0;
2528
2529 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002530 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002531 * so we have to consider current index for truncation
2532 */
2533 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2534 return 0;
2535 return 1;
2536}
2537
Allison Hendersond583fb82011-05-25 07:41:43 -04002538static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2539 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002540{
2541 struct super_block *sb = inode->i_sb;
2542 int depth = ext_depth(inode);
2543 struct ext4_ext_path *path;
2544 handle_t *handle;
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002545 int i, err;
Alex Tomasa86c6182006-10-11 01:21:03 -07002546
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002547 ext_debug("truncate since %u\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002548
2549 /* probably first extent we're gonna free will be last in block */
2550 handle = ext4_journal_start(inode, depth + 1);
2551 if (IS_ERR(handle))
2552 return PTR_ERR(handle);
2553
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002554again:
Alex Tomasa86c6182006-10-11 01:21:03 -07002555 ext4_ext_invalidate_cache(inode);
2556
2557 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002558 * We start scanning from right side, freeing all the blocks
2559 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002560 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002561 depth = ext_depth(inode);
Josef Bacik216553c2008-04-29 22:02:02 -04002562 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07002563 if (path == NULL) {
2564 ext4_journal_stop(handle);
2565 return -ENOMEM;
2566 }
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002567 path[0].p_depth = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -07002568 path[0].p_hdr = ext_inode_hdr(inode);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002569 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002570 err = -EIO;
2571 goto out;
2572 }
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002573 i = err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002574
2575 while (i >= 0 && err == 0) {
2576 if (i == depth) {
2577 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04002578 err = ext4_ext_rm_leaf(handle, inode, path,
2579 start, end);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002580 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002581 brelse(path[i].p_bh);
2582 path[i].p_bh = NULL;
2583 i--;
2584 continue;
2585 }
2586
2587 /* this is index block */
2588 if (!path[i].p_hdr) {
2589 ext_debug("initialize header\n");
2590 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002591 }
2592
Alex Tomasa86c6182006-10-11 01:21:03 -07002593 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002594 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002595 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2596 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2597 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2598 path[i].p_hdr,
2599 le16_to_cpu(path[i].p_hdr->eh_entries));
2600 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002601 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002602 path[i].p_idx--;
2603 }
2604
2605 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2606 i, EXT_FIRST_INDEX(path[i].p_hdr),
2607 path[i].p_idx);
2608 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002609 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002610 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002611 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04002612 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002613 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'obf89d162010-10-27 21:30:14 -04002614 bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002615 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002616 /* should we reset i_size? */
2617 err = -EIO;
2618 break;
2619 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002620 if (WARN_ON(i + 1 > depth)) {
2621 err = -EIO;
2622 break;
2623 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002624 if (ext4_ext_check(inode, ext_block_hdr(bh),
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002625 depth - i - 1)) {
2626 err = -EIO;
2627 break;
2628 }
2629 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002630
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002631 /* save actual number of indexes since this
2632 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002633 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2634 i++;
2635 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002636 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002637 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002638 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002639 * handle must be already prepared by the
2640 * truncatei_leaf() */
2641 err = ext4_ext_rm_idx(handle, inode, path + i);
2642 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002643 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002644 brelse(path[i].p_bh);
2645 path[i].p_bh = NULL;
2646 i--;
2647 ext_debug("return to level %d\n", i);
2648 }
2649 }
2650
2651 /* TODO: flexible tree reduction should be here */
2652 if (path->p_hdr->eh_entries == 0) {
2653 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002654 * truncate to zero freed all the tree,
2655 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002656 */
2657 err = ext4_ext_get_access(handle, inode, path);
2658 if (err == 0) {
2659 ext_inode_hdr(inode)->eh_depth = 0;
2660 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04002661 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07002662 err = ext4_ext_dirty(handle, inode, path);
2663 }
2664 }
2665out:
Alex Tomasa86c6182006-10-11 01:21:03 -07002666 ext4_ext_drop_refs(path);
2667 kfree(path);
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002668 if (err == -EAGAIN)
2669 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07002670 ext4_journal_stop(handle);
2671
2672 return err;
2673}
2674
2675/*
2676 * called at mount time
2677 */
2678void ext4_ext_init(struct super_block *sb)
2679{
2680 /*
2681 * possible initialization would be here
2682 */
2683
Theodore Ts'o83982b62009-01-06 14:53:16 -05002684 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04002685#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002686 printk(KERN_INFO "EXT4-fs: file extents enabled");
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002687#ifdef AGGRESSIVE_TEST
2688 printk(", aggressive tests");
Alex Tomasa86c6182006-10-11 01:21:03 -07002689#endif
2690#ifdef CHECK_BINSEARCH
2691 printk(", check binsearch");
2692#endif
2693#ifdef EXTENTS_STATS
2694 printk(", stats");
2695#endif
2696 printk("\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04002697#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07002698#ifdef EXTENTS_STATS
2699 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2700 EXT4_SB(sb)->s_ext_min = 1 << 30;
2701 EXT4_SB(sb)->s_ext_max = 0;
2702#endif
2703 }
2704}
2705
2706/*
2707 * called at umount time
2708 */
2709void ext4_ext_release(struct super_block *sb)
2710{
Theodore Ts'o83982b62009-01-06 14:53:16 -05002711 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
Alex Tomasa86c6182006-10-11 01:21:03 -07002712 return;
2713
2714#ifdef EXTENTS_STATS
2715 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2716 struct ext4_sb_info *sbi = EXT4_SB(sb);
2717 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2718 sbi->s_ext_blocks, sbi->s_ext_extents,
2719 sbi->s_ext_blocks / sbi->s_ext_extents);
2720 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2721 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2722 }
2723#endif
2724}
2725
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002726/* FIXME!! we need to try to merge to left or right after zero-out */
2727static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2728{
Lukas Czerner24075182010-10-27 21:30:06 -04002729 ext4_fsblk_t ee_pblock;
2730 unsigned int ee_len;
Jing Zhangb7203032010-05-12 00:00:00 -04002731 int ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002732
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002733 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04002734 ee_pblock = ext4_ext_pblock(ex);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002735
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04002736 ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
Lukas Czerner24075182010-10-27 21:30:06 -04002737 if (ret > 0)
2738 ret = 0;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002739
Lukas Czerner24075182010-10-27 21:30:06 -04002740 return ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002741}
2742
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002743/*
2744 * used by extent splitting.
2745 */
2746#define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
2747 due to ENOSPC */
2748#define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */
2749#define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */
2750
2751/*
2752 * ext4_split_extent_at() splits an extent at given block.
2753 *
2754 * @handle: the journal handle
2755 * @inode: the file inode
2756 * @path: the path to the extent
2757 * @split: the logical block where the extent is splitted.
2758 * @split_flags: indicates if the extent could be zeroout if split fails, and
2759 * the states(init or uninit) of new extents.
2760 * @flags: flags used to insert new extent to extent tree.
2761 *
2762 *
2763 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
2764 * of which are deterimined by split_flag.
2765 *
2766 * There are two cases:
2767 * a> the extent are splitted into two extent.
2768 * b> split is not needed, and just mark the extent.
2769 *
2770 * return 0 on success.
2771 */
2772static int ext4_split_extent_at(handle_t *handle,
2773 struct inode *inode,
2774 struct ext4_ext_path *path,
2775 ext4_lblk_t split,
2776 int split_flag,
2777 int flags)
2778{
2779 ext4_fsblk_t newblock;
2780 ext4_lblk_t ee_block;
2781 struct ext4_extent *ex, newex, orig_ex;
2782 struct ext4_extent *ex2 = NULL;
2783 unsigned int ee_len, depth;
2784 int err = 0;
2785
2786 ext_debug("ext4_split_extents_at: inode %lu, logical"
2787 "block %llu\n", inode->i_ino, (unsigned long long)split);
2788
2789 ext4_ext_show_leaf(inode, path);
2790
2791 depth = ext_depth(inode);
2792 ex = path[depth].p_ext;
2793 ee_block = le32_to_cpu(ex->ee_block);
2794 ee_len = ext4_ext_get_actual_len(ex);
2795 newblock = split - ee_block + ext4_ext_pblock(ex);
2796
2797 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
2798
2799 err = ext4_ext_get_access(handle, inode, path + depth);
2800 if (err)
2801 goto out;
2802
2803 if (split == ee_block) {
2804 /*
2805 * case b: block @split is the block that the extent begins with
2806 * then we just change the state of the extent, and splitting
2807 * is not needed.
2808 */
2809 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2810 ext4_ext_mark_uninitialized(ex);
2811 else
2812 ext4_ext_mark_initialized(ex);
2813
2814 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
2815 ext4_ext_try_to_merge(inode, path, ex);
2816
2817 err = ext4_ext_dirty(handle, inode, path + depth);
2818 goto out;
2819 }
2820
2821 /* case a */
2822 memcpy(&orig_ex, ex, sizeof(orig_ex));
2823 ex->ee_len = cpu_to_le16(split - ee_block);
2824 if (split_flag & EXT4_EXT_MARK_UNINIT1)
2825 ext4_ext_mark_uninitialized(ex);
2826
2827 /*
2828 * path may lead to new leaf, not to original leaf any more
2829 * after ext4_ext_insert_extent() returns,
2830 */
2831 err = ext4_ext_dirty(handle, inode, path + depth);
2832 if (err)
2833 goto fix_extent_len;
2834
2835 ex2 = &newex;
2836 ex2->ee_block = cpu_to_le32(split);
2837 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
2838 ext4_ext_store_pblock(ex2, newblock);
2839 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2840 ext4_ext_mark_uninitialized(ex2);
2841
2842 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
2843 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2844 err = ext4_ext_zeroout(inode, &orig_ex);
2845 if (err)
2846 goto fix_extent_len;
2847 /* update the extent length and mark as initialized */
2848 ex->ee_len = cpu_to_le32(ee_len);
2849 ext4_ext_try_to_merge(inode, path, ex);
2850 err = ext4_ext_dirty(handle, inode, path + depth);
2851 goto out;
2852 } else if (err)
2853 goto fix_extent_len;
2854
2855out:
2856 ext4_ext_show_leaf(inode, path);
2857 return err;
2858
2859fix_extent_len:
2860 ex->ee_len = orig_ex.ee_len;
2861 ext4_ext_dirty(handle, inode, path + depth);
2862 return err;
2863}
2864
2865/*
2866 * ext4_split_extents() splits an extent and mark extent which is covered
2867 * by @map as split_flags indicates
2868 *
2869 * It may result in splitting the extent into multiple extents (upto three)
2870 * There are three possibilities:
2871 * a> There is no split required
2872 * b> Splits in two extents: Split is happening at either end of the extent
2873 * c> Splits in three extents: Somone is splitting in middle of the extent
2874 *
2875 */
2876static int ext4_split_extent(handle_t *handle,
2877 struct inode *inode,
2878 struct ext4_ext_path *path,
2879 struct ext4_map_blocks *map,
2880 int split_flag,
2881 int flags)
2882{
2883 ext4_lblk_t ee_block;
2884 struct ext4_extent *ex;
2885 unsigned int ee_len, depth;
2886 int err = 0;
2887 int uninitialized;
2888 int split_flag1, flags1;
2889
2890 depth = ext_depth(inode);
2891 ex = path[depth].p_ext;
2892 ee_block = le32_to_cpu(ex->ee_block);
2893 ee_len = ext4_ext_get_actual_len(ex);
2894 uninitialized = ext4_ext_is_uninitialized(ex);
2895
2896 if (map->m_lblk + map->m_len < ee_block + ee_len) {
2897 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2898 EXT4_EXT_MAY_ZEROOUT : 0;
2899 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
2900 if (uninitialized)
2901 split_flag1 |= EXT4_EXT_MARK_UNINIT1 |
2902 EXT4_EXT_MARK_UNINIT2;
2903 err = ext4_split_extent_at(handle, inode, path,
2904 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04002905 if (err)
2906 goto out;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002907 }
2908
2909 ext4_ext_drop_refs(path);
2910 path = ext4_ext_find_extent(inode, map->m_lblk, path);
2911 if (IS_ERR(path))
2912 return PTR_ERR(path);
2913
2914 if (map->m_lblk >= ee_block) {
2915 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2916 EXT4_EXT_MAY_ZEROOUT : 0;
2917 if (uninitialized)
2918 split_flag1 |= EXT4_EXT_MARK_UNINIT1;
2919 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2920 split_flag1 |= EXT4_EXT_MARK_UNINIT2;
2921 err = ext4_split_extent_at(handle, inode, path,
2922 map->m_lblk, split_flag1, flags);
2923 if (err)
2924 goto out;
2925 }
2926
2927 ext4_ext_show_leaf(inode, path);
2928out:
2929 return err ? err : map->m_len;
2930}
2931
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002932#define EXT4_EXT_ZERO_LEN 7
Amit Arora56055d32007-07-17 21:42:38 -04002933/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002934 * This function is called by ext4_ext_map_blocks() if someone tries to write
Amit Arora56055d32007-07-17 21:42:38 -04002935 * to an uninitialized extent. It may result in splitting the uninitialized
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002936 * extent into multiple extents (up to three - one initialized and two
Amit Arora56055d32007-07-17 21:42:38 -04002937 * uninitialized).
2938 * There are three possibilities:
2939 * a> There is no split required: Entire extent should be initialized
2940 * b> Splits in two extents: Write is happening at either end of the extent
2941 * c> Splits in three extents: Somone is writing in middle of the extent
2942 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002943static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002944 struct inode *inode,
2945 struct ext4_map_blocks *map,
2946 struct ext4_ext_path *path)
Amit Arora56055d32007-07-17 21:42:38 -04002947{
Yongqiang Yang667eff32011-05-03 12:25:07 -04002948 struct ext4_map_blocks split_map;
2949 struct ext4_extent zero_ex;
2950 struct ext4_extent *ex;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002951 ext4_lblk_t ee_block, eof_block;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002952 unsigned int allocated, ee_len, depth;
Amit Arora56055d32007-07-17 21:42:38 -04002953 int err = 0;
Yongqiang Yang667eff32011-05-03 12:25:07 -04002954 int split_flag = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002955
2956 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
2957 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002958 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002959
2960 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
2961 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002962 if (eof_block < map->m_lblk + map->m_len)
2963 eof_block = map->m_lblk + map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04002964
2965 depth = ext_depth(inode);
Amit Arora56055d32007-07-17 21:42:38 -04002966 ex = path[depth].p_ext;
2967 ee_block = le32_to_cpu(ex->ee_block);
2968 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002969 allocated = ee_len - (map->m_lblk - ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002970
Yongqiang Yang667eff32011-05-03 12:25:07 -04002971 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002972 /*
2973 * It is safe to convert extent to initialized via explicit
2974 * zeroout only if extent is fully insde i_size or new_size.
2975 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04002976 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002977
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002978 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
Yongqiang Yang667eff32011-05-03 12:25:07 -04002979 if (ee_len <= 2*EXT4_EXT_ZERO_LEN &&
2980 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2981 err = ext4_ext_zeroout(inode, ex);
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002982 if (err)
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002983 goto out;
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002984
2985 err = ext4_ext_get_access(handle, inode, path + depth);
2986 if (err)
2987 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04002988 ext4_ext_mark_initialized(ex);
2989 ext4_ext_try_to_merge(inode, path, ex);
2990 err = ext4_ext_dirty(handle, inode, path + depth);
2991 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04002992 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04002993
Amit Arora56055d32007-07-17 21:42:38 -04002994 /*
Yongqiang Yang667eff32011-05-03 12:25:07 -04002995 * four cases:
2996 * 1. split the extent into three extents.
2997 * 2. split the extent into two extents, zeroout the first half.
2998 * 3. split the extent into two extents, zeroout the second half.
2999 * 4. split the extent into two extents with out zeroout.
Amit Arora56055d32007-07-17 21:42:38 -04003000 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003001 split_map.m_lblk = map->m_lblk;
3002 split_map.m_len = map->m_len;
3003
3004 if (allocated > map->m_len) {
3005 if (allocated <= EXT4_EXT_ZERO_LEN &&
3006 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3007 /* case 3 */
3008 zero_ex.ee_block =
Allison Henderson9b940f82011-05-16 10:11:09 -04003009 cpu_to_le32(map->m_lblk);
3010 zero_ex.ee_len = cpu_to_le16(allocated);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003011 ext4_ext_store_pblock(&zero_ex,
3012 ext4_ext_pblock(ex) + map->m_lblk - ee_block);
3013 err = ext4_ext_zeroout(inode, &zero_ex);
Amit Arora56055d32007-07-17 21:42:38 -04003014 if (err)
3015 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003016 split_map.m_lblk = map->m_lblk;
3017 split_map.m_len = allocated;
3018 } else if ((map->m_lblk - ee_block + map->m_len <
3019 EXT4_EXT_ZERO_LEN) &&
3020 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3021 /* case 2 */
3022 if (map->m_lblk != ee_block) {
3023 zero_ex.ee_block = ex->ee_block;
3024 zero_ex.ee_len = cpu_to_le16(map->m_lblk -
3025 ee_block);
3026 ext4_ext_store_pblock(&zero_ex,
3027 ext4_ext_pblock(ex));
3028 err = ext4_ext_zeroout(inode, &zero_ex);
3029 if (err)
3030 goto out;
3031 }
3032
Yongqiang Yang667eff32011-05-03 12:25:07 -04003033 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003034 split_map.m_len = map->m_lblk - ee_block + map->m_len;
3035 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003036 }
3037 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003038
3039 allocated = ext4_split_extent(handle, inode, path,
3040 &split_map, split_flag, 0);
3041 if (allocated < 0)
3042 err = allocated;
3043
Amit Arora56055d32007-07-17 21:42:38 -04003044out:
3045 return err ? err : allocated;
3046}
3047
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003048/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003049 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003050 * ext4_get_blocks_dio_write() when DIO to write
3051 * to an uninitialized extent.
3052 *
Paul Bollefd018fe2011-02-15 00:05:43 +01003053 * Writing to an uninitialized extent may result in splitting the uninitialized
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003054 * extent into multiple /initialized uninitialized extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003055 * There are three possibilities:
3056 * a> There is no split required: Entire extent should be uninitialized
3057 * b> Splits in two extents: Write is happening at either end of the extent
3058 * c> Splits in three extents: Somone is writing in middle of the extent
3059 *
3060 * One of more index blocks maybe needed if the extent tree grow after
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003061 * the uninitialized extent split. To prevent ENOSPC occur at the IO
Mingming Cao00314622009-09-28 15:49:08 -04003062 * complete, we need to split the uninitialized extent before DIO submit
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02003063 * the IO. The uninitialized extent called at this time will be split
Mingming Cao00314622009-09-28 15:49:08 -04003064 * into three uninitialized extent(at most). After IO complete, the part
3065 * being filled will be convert to initialized by the end_io callback function
3066 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003067 *
3068 * Returns the size of uninitialized extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003069 */
3070static int ext4_split_unwritten_extents(handle_t *handle,
3071 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003072 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003073 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04003074 int flags)
3075{
Yongqiang Yang667eff32011-05-03 12:25:07 -04003076 ext4_lblk_t eof_block;
3077 ext4_lblk_t ee_block;
3078 struct ext4_extent *ex;
3079 unsigned int ee_len;
3080 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003081
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003082 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
3083 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003084 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003085
3086 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3087 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003088 if (eof_block < map->m_lblk + map->m_len)
3089 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003090 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003091 * It is safe to convert extent to initialized via explicit
3092 * zeroout only if extent is fully insde i_size or new_size.
3093 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003094 depth = ext_depth(inode);
3095 ex = path[depth].p_ext;
3096 ee_block = le32_to_cpu(ex->ee_block);
3097 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003098
Yongqiang Yang667eff32011-05-03 12:25:07 -04003099 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
3100 split_flag |= EXT4_EXT_MARK_UNINIT2;
Mingming Cao00314622009-09-28 15:49:08 -04003101
Yongqiang Yang667eff32011-05-03 12:25:07 -04003102 flags |= EXT4_GET_BLOCKS_PRE_IO;
3103 return ext4_split_extent(handle, inode, path, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003104}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003105
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003106static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003107 struct inode *inode,
3108 struct ext4_ext_path *path)
3109{
3110 struct ext4_extent *ex;
3111 struct ext4_extent_header *eh;
3112 int depth;
3113 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003114
3115 depth = ext_depth(inode);
3116 eh = path[depth].p_hdr;
3117 ex = path[depth].p_ext;
3118
Yongqiang Yang197217a2011-05-03 11:45:29 -04003119 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3120 "block %llu, max_blocks %u\n", inode->i_ino,
3121 (unsigned long long)le32_to_cpu(ex->ee_block),
3122 ext4_ext_get_actual_len(ex));
3123
Mingming Cao00314622009-09-28 15:49:08 -04003124 err = ext4_ext_get_access(handle, inode, path + depth);
3125 if (err)
3126 goto out;
3127 /* first mark the extent as initialized */
3128 ext4_ext_mark_initialized(ex);
3129
Yongqiang Yang197217a2011-05-03 11:45:29 -04003130 /* note: ext4_ext_correct_indexes() isn't needed here because
3131 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003132 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04003133 ext4_ext_try_to_merge(inode, path, ex);
3134
Mingming Cao00314622009-09-28 15:49:08 -04003135 /* Mark modified extent as dirty */
3136 err = ext4_ext_dirty(handle, inode, path + depth);
3137out:
3138 ext4_ext_show_leaf(inode, path);
3139 return err;
3140}
3141
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003142static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3143 sector_t block, int count)
3144{
3145 int i;
3146 for (i = 0; i < count; i++)
3147 unmap_underlying_metadata(bdev, block + i);
3148}
3149
Theodore Ts'o58590b02010-10-27 21:23:12 -04003150/*
3151 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3152 */
3153static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
Eric Sandeend002ebf2011-01-10 13:03:35 -05003154 ext4_lblk_t lblk,
Theodore Ts'o58590b02010-10-27 21:23:12 -04003155 struct ext4_ext_path *path,
3156 unsigned int len)
3157{
3158 int i, depth;
3159 struct ext4_extent_header *eh;
Sergey Senozhatsky65922cb2011-03-23 14:08:27 -04003160 struct ext4_extent *last_ex;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003161
3162 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3163 return 0;
3164
3165 depth = ext_depth(inode);
3166 eh = path[depth].p_hdr;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003167
3168 if (unlikely(!eh->eh_entries)) {
3169 EXT4_ERROR_INODE(inode, "eh->eh_entries == 0 and "
3170 "EOFBLOCKS_FL set");
3171 return -EIO;
3172 }
3173 last_ex = EXT_LAST_EXTENT(eh);
3174 /*
3175 * We should clear the EOFBLOCKS_FL flag if we are writing the
3176 * last block in the last extent in the file. We test this by
3177 * first checking to see if the caller to
3178 * ext4_ext_get_blocks() was interested in the last block (or
3179 * a block beyond the last block) in the current extent. If
3180 * this turns out to be false, we can bail out from this
3181 * function immediately.
3182 */
Eric Sandeend002ebf2011-01-10 13:03:35 -05003183 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
Theodore Ts'o58590b02010-10-27 21:23:12 -04003184 ext4_ext_get_actual_len(last_ex))
3185 return 0;
3186 /*
3187 * If the caller does appear to be planning to write at or
3188 * beyond the end of the current extent, we then test to see
3189 * if the current extent is the last extent in the file, by
3190 * checking to make sure it was reached via the rightmost node
3191 * at each level of the tree.
3192 */
3193 for (i = depth-1; i >= 0; i--)
3194 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3195 return 0;
3196 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3197 return ext4_mark_inode_dirty(handle, inode);
3198}
3199
Mingming Cao00314622009-09-28 15:49:08 -04003200static int
3201ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003202 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003203 struct ext4_ext_path *path, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003204 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003205{
3206 int ret = 0;
3207 int err = 0;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003208 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Mingming Cao00314622009-09-28 15:49:08 -04003209
3210 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3211 "block %llu, max_blocks %u, flags %d, allocated %u",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003212 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04003213 flags, allocated);
3214 ext4_ext_show_leaf(inode, path);
3215
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003216 /* get_block() before submit the IO, split the extent */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003217 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003218 ret = ext4_split_unwritten_extents(handle, inode, map,
3219 path, flags);
Mingming5f524952009-11-10 10:48:04 -05003220 /*
3221 * Flag the inode(non aio case) or end_io struct (aio case)
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003222 * that this IO needs to conversion to written when IO is
Mingming5f524952009-11-10 10:48:04 -05003223 * completed
3224 */
Eric Sandeene9e3bce2011-02-12 08:17:34 -05003225 if (io && !(io->flag & EXT4_IO_END_UNWRITTEN)) {
Theodore Ts'obd2d0212010-10-27 21:30:10 -04003226 io->flag = EXT4_IO_END_UNWRITTEN;
Eric Sandeene9e3bce2011-02-12 08:17:34 -05003227 atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten);
3228 } else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003229 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
Jiaying Zhang744692d2010-03-04 16:14:02 -05003230 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003231 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao00314622009-09-28 15:49:08 -04003232 goto out;
3233 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003234 /* IO end_io complete, convert the filled extent to written */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003235 if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003236 ret = ext4_convert_unwritten_extents_endio(handle, inode,
Mingming Cao00314622009-09-28 15:49:08 -04003237 path);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003238 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05003239 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05003240 err = check_eofblocks_fl(handle, inode, map->m_lblk,
3241 path, map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003242 } else
3243 err = ret;
Mingming Cao00314622009-09-28 15:49:08 -04003244 goto out2;
3245 }
3246 /* buffered IO case */
3247 /*
3248 * repeat fallocate creation request
3249 * we already have an unwritten extent
3250 */
3251 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3252 goto map_out;
3253
3254 /* buffered READ or buffered write_begin() lookup */
3255 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3256 /*
3257 * We have blocks reserved already. We
3258 * return allocated blocks so that delalloc
3259 * won't do block reservation for us. But
3260 * the buffer head will be unmapped so that
3261 * a read from the block returns 0s.
3262 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003263 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003264 goto out1;
3265 }
3266
3267 /* buffered write, writepage time, convert*/
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003268 ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003269 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05003270 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05003271 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
3272 map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003273 if (err < 0)
3274 goto out2;
3275 }
3276
Mingming Cao00314622009-09-28 15:49:08 -04003277out:
3278 if (ret <= 0) {
3279 err = ret;
3280 goto out2;
3281 } else
3282 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003283 map->m_flags |= EXT4_MAP_NEW;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003284 /*
3285 * if we allocated more blocks than requested
3286 * we need to make sure we unmap the extra block
3287 * allocated. The actual needed block will get
3288 * unmapped later when we find the buffer_head marked
3289 * new.
3290 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003291 if (allocated > map->m_len) {
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003292 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003293 newblock + map->m_len,
3294 allocated - map->m_len);
3295 allocated = map->m_len;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003296 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003297
3298 /*
3299 * If we have done fallocate with the offset that is already
3300 * delayed allocated, we would have block reservation
3301 * and quota reservation done in the delayed write path.
3302 * But fallocate would have already updated quota and block
3303 * count for this offset. So cancel these reservation
3304 */
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05003305 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003306 ext4_da_update_reserve_space(inode, allocated, 0);
3307
Mingming Cao00314622009-09-28 15:49:08 -04003308map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003309 map->m_flags |= EXT4_MAP_MAPPED;
Mingming Cao00314622009-09-28 15:49:08 -04003310out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003311 if (allocated > map->m_len)
3312 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003313 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003314 map->m_pblk = newblock;
3315 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04003316out2:
3317 if (path) {
3318 ext4_ext_drop_refs(path);
3319 kfree(path);
3320 }
3321 return err ? err : allocated;
3322}
Theodore Ts'o58590b02010-10-27 21:23:12 -04003323
Mingming Cao00314622009-09-28 15:49:08 -04003324/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05003325 * Block allocation/map/preallocation routine for extents based files
3326 *
3327 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003328 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003329 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3330 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05003331 *
3332 * return > 0, number of of blocks already mapped/allocated
3333 * if create == 0 and these are pre-allocated blocks
3334 * buffer head is unmapped
3335 * otherwise blocks are mapped
3336 *
3337 * return = 0, if plain look up failed (blocks have not been allocated)
3338 * buffer head is unmapped
3339 *
3340 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003341 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003342int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3343 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07003344{
3345 struct ext4_ext_path *path = NULL;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003346 struct ext4_extent newex, *ex;
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003347 ext4_fsblk_t newblock = 0;
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003348 int err = 0, depth, ret;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003349 unsigned int allocated = 0;
Allison Hendersone8613042011-05-25 07:41:46 -04003350 unsigned int punched_out = 0;
3351 unsigned int result = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003352 struct ext4_allocation_request ar;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003353 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Allison Hendersone8613042011-05-25 07:41:46 -04003354 struct ext4_map_blocks punch_map;
Alex Tomasa86c6182006-10-11 01:21:03 -07003355
Mingming84fe3be2009-09-01 08:44:37 -04003356 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003357 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003358 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07003359
3360 /* check in cache */
Allison Hendersone8613042011-05-25 07:41:46 -04003361 if (ext4_ext_in_cache(inode, map->m_lblk, &newex) &&
3362 ((flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) == 0)) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003363 if (!newex.ee_start_lo && !newex.ee_start_hi) {
Theodore Ts'oc2177052009-05-14 00:58:52 -04003364 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003365 /*
3366 * block isn't allocated yet and
3367 * user doesn't want to allocate it
3368 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003369 goto out2;
3370 }
3371 /* we should allocate requested block */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003372 } else {
Alex Tomasa86c6182006-10-11 01:21:03 -07003373 /* block is already allocated */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003374 newblock = map->m_lblk
Dave Kleikamp8c55e202007-05-24 13:04:54 -04003375 - le32_to_cpu(newex.ee_block)
Theodore Ts'obf89d162010-10-27 21:30:14 -04003376 + ext4_ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003377 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003378 allocated = ext4_ext_get_actual_len(&newex) -
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003379 (map->m_lblk - le32_to_cpu(newex.ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -07003380 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07003381 }
3382 }
3383
3384 /* find extent for this block */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003385 path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07003386 if (IS_ERR(path)) {
3387 err = PTR_ERR(path);
3388 path = NULL;
3389 goto out2;
3390 }
3391
3392 depth = ext_depth(inode);
3393
3394 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003395 * consistent leaf must not be empty;
3396 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07003397 * this is why assert can't be put in ext4_ext_find_extent()
3398 */
Frank Mayhar273df552010-03-02 11:46:09 -05003399 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
3400 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04003401 "lblock: %lu, depth: %d pblock %lld",
3402 (unsigned long) map->m_lblk, depth,
3403 path[depth].p_block);
Surbhi Palande034fb4c2009-12-14 09:53:52 -05003404 err = -EIO;
3405 goto out2;
3406 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003407
Avantika Mathur7e028972006-12-06 20:41:33 -08003408 ex = path[depth].p_ext;
3409 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003410 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003411 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003412 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003413
3414 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003415 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04003416 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003417 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003418 ee_len = ext4_ext_get_actual_len(ex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003419 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003420 if (in_range(map->m_lblk, ee_block, ee_len)) {
3421 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003422 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003423 allocated = ee_len - (map->m_lblk - ee_block);
3424 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
3425 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04003426
Allison Hendersone8613042011-05-25 07:41:46 -04003427 if ((flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) == 0) {
3428 /*
3429 * Do not put uninitialized extent
3430 * in the cache
3431 */
3432 if (!ext4_ext_is_uninitialized(ex)) {
3433 ext4_ext_put_in_cache(inode, ee_block,
3434 ee_len, ee_start);
3435 goto out;
3436 }
3437 ret = ext4_ext_handle_uninitialized_extents(
3438 handle, inode, map, path, flags,
3439 allocated, newblock);
3440 return ret;
Amit Arora56055d32007-07-17 21:42:38 -04003441 }
Allison Hendersone8613042011-05-25 07:41:46 -04003442
3443 /*
3444 * Punch out the map length, but only to the
3445 * end of the extent
3446 */
3447 punched_out = allocated < map->m_len ?
3448 allocated : map->m_len;
3449
3450 /*
3451 * Sense extents need to be converted to
3452 * uninitialized, they must fit in an
3453 * uninitialized extent
3454 */
3455 if (punched_out > EXT_UNINIT_MAX_LEN)
3456 punched_out = EXT_UNINIT_MAX_LEN;
3457
3458 punch_map.m_lblk = map->m_lblk;
3459 punch_map.m_pblk = newblock;
3460 punch_map.m_len = punched_out;
3461 punch_map.m_flags = 0;
3462
3463 /* Check to see if the extent needs to be split */
3464 if (punch_map.m_len != ee_len ||
3465 punch_map.m_lblk != ee_block) {
3466
3467 ret = ext4_split_extent(handle, inode,
3468 path, &punch_map, 0,
3469 EXT4_GET_BLOCKS_PUNCH_OUT_EXT |
3470 EXT4_GET_BLOCKS_PRE_IO);
3471
3472 if (ret < 0) {
3473 err = ret;
3474 goto out2;
3475 }
3476 /*
3477 * find extent for the block at
3478 * the start of the hole
3479 */
3480 ext4_ext_drop_refs(path);
3481 kfree(path);
3482
3483 path = ext4_ext_find_extent(inode,
3484 map->m_lblk, NULL);
3485 if (IS_ERR(path)) {
3486 err = PTR_ERR(path);
3487 path = NULL;
3488 goto out2;
3489 }
3490
3491 depth = ext_depth(inode);
3492 ex = path[depth].p_ext;
3493 ee_len = ext4_ext_get_actual_len(ex);
3494 ee_block = le32_to_cpu(ex->ee_block);
3495 ee_start = ext4_ext_pblock(ex);
3496
3497 }
3498
3499 ext4_ext_mark_uninitialized(ex);
3500
3501 err = ext4_ext_remove_space(inode, map->m_lblk,
3502 map->m_lblk + punched_out);
3503
3504 goto out2;
Alex Tomasa86c6182006-10-11 01:21:03 -07003505 }
3506 }
3507
3508 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003509 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07003510 * we couldn't try to create block if create flag is zero
3511 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04003512 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003513 /*
3514 * put just found gap into cache to speed up
3515 * subsequent requests
3516 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003517 ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
Alex Tomasa86c6182006-10-11 01:21:03 -07003518 goto out2;
3519 }
3520 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003521 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07003522 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003523
Alex Tomasc9de5602008-01-29 00:19:52 -05003524 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003525 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003526 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3527 if (err)
3528 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003529 ar.lright = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003530 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
3531 if (err)
3532 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04003533
Amit Arora749269f2007-07-18 09:02:56 -04003534 /*
3535 * See if request is beyond maximum number of blocks we can have in
3536 * a single extent. For an initialized extent this limit is
3537 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3538 * EXT_UNINIT_MAX_LEN.
3539 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003540 if (map->m_len > EXT_INIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003541 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003542 map->m_len = EXT_INIT_MAX_LEN;
3543 else if (map->m_len > EXT_UNINIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003544 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003545 map->m_len = EXT_UNINIT_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04003546
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003547 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
3548 newex.ee_block = cpu_to_le32(map->m_lblk);
3549 newex.ee_len = cpu_to_le16(map->m_len);
Amit Arora25d14f92007-05-24 13:04:13 -04003550 err = ext4_ext_check_overlap(inode, &newex, path);
3551 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003552 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04003553 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003554 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003555
3556 /* allocate new block */
3557 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003558 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
3559 ar.logical = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003560 ar.len = allocated;
3561 if (S_ISREG(inode->i_mode))
3562 ar.flags = EXT4_MB_HINT_DATA;
3563 else
3564 /* disable in-core preallocation for non-regular files */
3565 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04003566 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
3567 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05003568 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07003569 if (!newblock)
3570 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04003571 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003572 ar.goal, newblock, allocated);
Alex Tomasa86c6182006-10-11 01:21:03 -07003573
3574 /* try to insert new extent into found leaf and return */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07003575 ext4_ext_store_pblock(&newex, newblock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003576 newex.ee_len = cpu_to_le16(ar.len);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003577 /* Mark uninitialized */
3578 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
Amit Aroraa2df2a62007-07-17 21:42:41 -04003579 ext4_ext_mark_uninitialized(&newex);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003580 /*
Jiaying Zhang744692d2010-03-04 16:14:02 -05003581 * io_end structure was created for every IO write to an
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003582 * uninitialized extent. To avoid unnecessary conversion,
Jiaying Zhang744692d2010-03-04 16:14:02 -05003583 * here we flag the IO that really needs the conversion.
Mingming5f524952009-11-10 10:48:04 -05003584 * For non asycn direct IO case, flag the inode state
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003585 * that we need to perform conversion when IO is done.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003586 */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003587 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Eric Sandeene9e3bce2011-02-12 08:17:34 -05003588 if (io && !(io->flag & EXT4_IO_END_UNWRITTEN)) {
Theodore Ts'obd2d0212010-10-27 21:30:10 -04003589 io->flag = EXT4_IO_END_UNWRITTEN;
Eric Sandeene9e3bce2011-02-12 08:17:34 -05003590 atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten);
3591 } else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003592 ext4_set_inode_state(inode,
3593 EXT4_STATE_DIO_UNWRITTEN);
Mingming5f524952009-11-10 10:48:04 -05003594 }
Jiaying Zhang744692d2010-03-04 16:14:02 -05003595 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003596 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003597 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003598
Eric Sandeend002ebf2011-01-10 13:03:35 -05003599 err = check_eofblocks_fl(handle, inode, map->m_lblk, path, ar.len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003600 if (err)
3601 goto out2;
3602
Mingming Cao00314622009-09-28 15:49:08 -04003603 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
Alex Tomas315054f2007-05-24 13:04:25 -04003604 if (err) {
3605 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05003606 /* not a good idea to call discard here directly,
3607 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003608 ext4_discard_preallocations(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05003609 ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex),
Theodore Ts'oe6362602009-11-23 07:17:05 -05003610 ext4_ext_get_actual_len(&newex), 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07003611 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04003612 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003613
Alex Tomasa86c6182006-10-11 01:21:03 -07003614 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04003615 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003616 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003617 if (allocated > map->m_len)
3618 allocated = map->m_len;
3619 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07003620
Jan Karab436b9b2009-12-08 23:51:10 -05003621 /*
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003622 * Update reserved blocks/metadata blocks after successful
3623 * block allocation which had been deferred till now.
3624 */
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05003625 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003626 ext4_da_update_reserve_space(inode, allocated, 1);
3627
3628 /*
Jan Karab436b9b2009-12-08 23:51:10 -05003629 * Cache the extent and update transaction to commit on fdatasync only
3630 * when it is _not_ an uninitialized extent.
3631 */
3632 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003633 ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock);
Jan Karab436b9b2009-12-08 23:51:10 -05003634 ext4_update_inode_fsync_trans(handle, inode, 1);
3635 } else
3636 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07003637out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003638 if (allocated > map->m_len)
3639 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07003640 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003641 map->m_flags |= EXT4_MAP_MAPPED;
3642 map->m_pblk = newblock;
3643 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07003644out2:
3645 if (path) {
3646 ext4_ext_drop_refs(path);
3647 kfree(path);
3648 }
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003649 trace_ext4_ext_map_blocks_exit(inode, map->m_lblk,
3650 newblock, map->m_len, err ? err : allocated);
Allison Hendersone8613042011-05-25 07:41:46 -04003651
3652 result = (flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) ?
3653 punched_out : allocated;
3654
3655 return err ? err : result;
Alex Tomasa86c6182006-10-11 01:21:03 -07003656}
3657
Jan Karacf108bc2008-07-11 19:27:31 -04003658void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07003659{
3660 struct address_space *mapping = inode->i_mapping;
3661 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003662 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07003663 handle_t *handle;
3664 int err = 0;
3665
3666 /*
Jiaying Zhang3889fd52011-01-10 12:47:05 -05003667 * finish any pending end_io work so we won't run the risk of
3668 * converting any truncated blocks to initialized later
3669 */
3670 ext4_flush_completed_IO(inode);
3671
3672 /*
Alex Tomasa86c6182006-10-11 01:21:03 -07003673 * probably first extent we're gonna free will be last in block
3674 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04003675 err = ext4_writepage_trans_blocks(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07003676 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04003677 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07003678 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07003679
Jan Karacf108bc2008-07-11 19:27:31 -04003680 if (inode->i_size & (sb->s_blocksize - 1))
3681 ext4_block_truncate_page(handle, mapping, inode->i_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07003682
Jan Kara9ddfc3d2008-07-11 19:27:31 -04003683 if (ext4_orphan_add(handle, inode))
3684 goto out_stop;
3685
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003686 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07003687 ext4_ext_invalidate_cache(inode);
3688
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003689 ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003690
Alex Tomasa86c6182006-10-11 01:21:03 -07003691 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003692 * TODO: optimization is possible here.
3693 * Probably we need not scan at all,
3694 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07003695 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003696
3697 /* we have to know where to truncate from in crash case */
3698 EXT4_I(inode)->i_disksize = inode->i_size;
3699 ext4_mark_inode_dirty(handle, inode);
3700
3701 last_block = (inode->i_size + sb->s_blocksize - 1)
3702 >> EXT4_BLOCK_SIZE_BITS(sb);
Lukas Czernerf17722f2011-06-06 00:05:17 -04003703 err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07003704
3705 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04003706 * transaction synchronous.
3707 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003708 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05003709 ext4_handle_sync(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07003710
Jan Kara9ddfc3d2008-07-11 19:27:31 -04003711 up_write(&EXT4_I(inode)->i_data_sem);
Eric Gouriouf6d2f6b2011-05-22 21:33:00 -04003712
3713out_stop:
Alex Tomasa86c6182006-10-11 01:21:03 -07003714 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003715 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07003716 * then we need to clear up the orphan record which we created above.
3717 * However, if this was a real unlink then we were called by
3718 * ext4_delete_inode(), and we allow that function to clean up the
3719 * orphan info for us.
3720 */
3721 if (inode->i_nlink)
3722 ext4_orphan_del(handle, inode);
3723
Solofo Ramangalahyef737722008-04-29 22:00:41 -04003724 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3725 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07003726 ext4_journal_stop(handle);
3727}
3728
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003729static void ext4_falloc_update_inode(struct inode *inode,
3730 int mode, loff_t new_size, int update_ctime)
3731{
3732 struct timespec now;
3733
3734 if (update_ctime) {
3735 now = current_fs_time(inode->i_sb);
3736 if (!timespec_equal(&inode->i_ctime, &now))
3737 inode->i_ctime = now;
3738 }
3739 /*
3740 * Update only when preallocation was requested beyond
3741 * the file size.
3742 */
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003743 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3744 if (new_size > i_size_read(inode))
3745 i_size_write(inode, new_size);
3746 if (new_size > EXT4_I(inode)->i_disksize)
3747 ext4_update_i_disksize(inode, new_size);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003748 } else {
3749 /*
3750 * Mark that we allocate beyond EOF so the subsequent truncate
3751 * can proceed even if the new size is the same as i_size.
3752 */
3753 if (new_size > i_size_read(inode))
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003754 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003755 }
3756
3757}
3758
Amit Aroraa2df2a62007-07-17 21:42:41 -04003759/*
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003760 * preallocate space for a file. This implements ext4's fallocate file
Amit Aroraa2df2a62007-07-17 21:42:41 -04003761 * operation, which gets called from sys_fallocate system call.
3762 * For block-mapped files, posix_fallocate should fall back to the method
3763 * of writing zeroes to the required new blocks (the same behavior which is
3764 * expected for file systems which do not support fallocate() system call).
3765 */
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003766long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
Amit Aroraa2df2a62007-07-17 21:42:41 -04003767{
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003768 struct inode *inode = file->f_path.dentry->d_inode;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003769 handle_t *handle;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003770 loff_t new_size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003771 unsigned int max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003772 int ret = 0;
3773 int ret2 = 0;
3774 int retries = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003775 struct ext4_map_blocks map;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003776 unsigned int credits, blkbits = inode->i_blkbits;
3777
3778 /*
3779 * currently supporting (pre)allocate mode for extent-based
3780 * files _only_
3781 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003782 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Amit Aroraa2df2a62007-07-17 21:42:41 -04003783 return -EOPNOTSUPP;
3784
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003785 /* Return error if mode is not supported */
3786 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
3787 return -EOPNOTSUPP;
3788
3789 if (mode & FALLOC_FL_PUNCH_HOLE)
3790 return ext4_punch_hole(file, offset, len);
3791
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003792 trace_ext4_fallocate_enter(inode, offset, len, mode);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003793 map.m_lblk = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003794 /*
3795 * We can't just convert len to max_blocks because
3796 * If blocksize = 4096 offset = 3072 and len = 2048
3797 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003798 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003799 - map.m_lblk;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003800 /*
Mingming Caof3bd1f32008-08-19 22:16:03 -04003801 * credits to insert 1 extent into extent tree
Amit Aroraa2df2a62007-07-17 21:42:41 -04003802 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04003803 credits = ext4_chunk_trans_blocks(inode, max_blocks);
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05003804 mutex_lock(&inode->i_mutex);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04003805 ret = inode_newsize_ok(inode, (len + offset));
3806 if (ret) {
3807 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003808 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04003809 return ret;
3810 }
Amit Aroraa2df2a62007-07-17 21:42:41 -04003811retry:
3812 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003813 map.m_lblk = map.m_lblk + ret;
3814 map.m_len = max_blocks = max_blocks - ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003815 handle = ext4_journal_start(inode, credits);
3816 if (IS_ERR(handle)) {
3817 ret = PTR_ERR(handle);
3818 break;
3819 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003820 ret = ext4_map_blocks(handle, inode, &map,
Vivek Haldar556b27a2011-05-25 07:41:54 -04003821 EXT4_GET_BLOCKS_CREATE_UNINIT_EXT |
3822 EXT4_GET_BLOCKS_NO_NORMALIZE);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05003823 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003824#ifdef EXT4FS_DEBUG
3825 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003826 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003827 "returned error inode#%lu, block=%u, "
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05003828 "max_blocks=%u", __func__,
Kazuya Mioa6371b62010-10-27 21:30:15 -04003829 inode->i_ino, map.m_lblk, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003830#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04003831 ext4_mark_inode_dirty(handle, inode);
3832 ret2 = ext4_journal_stop(handle);
3833 break;
3834 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003835 if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003836 blkbits) >> blkbits))
3837 new_size = offset + len;
3838 else
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003839 new_size = (map.m_lblk + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003840
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003841 ext4_falloc_update_inode(inode, mode, new_size,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003842 (map.m_flags & EXT4_MAP_NEW));
Amit Aroraa2df2a62007-07-17 21:42:41 -04003843 ext4_mark_inode_dirty(handle, inode);
3844 ret2 = ext4_journal_stop(handle);
3845 if (ret2)
3846 break;
3847 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003848 if (ret == -ENOSPC &&
3849 ext4_should_retry_alloc(inode->i_sb, &retries)) {
3850 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003851 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003852 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05003853 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003854 trace_ext4_fallocate_exit(inode, offset, max_blocks,
3855 ret > 0 ? ret2 : ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003856 return ret > 0 ? ret2 : ret;
3857}
Eric Sandeen6873fa02008-10-07 00:46:36 -04003858
3859/*
Mingming Cao00314622009-09-28 15:49:08 -04003860 * This function convert a range of blocks to written extents
3861 * The caller of this function will pass the start offset and the size.
3862 * all unwritten extents within this range will be converted to
3863 * written extents.
3864 *
3865 * This function is called from the direct IO end io call back
3866 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05003867 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04003868 */
3869int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
Eric Sandeena1de02d2010-02-04 23:58:38 -05003870 ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04003871{
3872 handle_t *handle;
Mingming Cao00314622009-09-28 15:49:08 -04003873 unsigned int max_blocks;
3874 int ret = 0;
3875 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003876 struct ext4_map_blocks map;
Mingming Cao00314622009-09-28 15:49:08 -04003877 unsigned int credits, blkbits = inode->i_blkbits;
3878
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003879 map.m_lblk = offset >> blkbits;
Mingming Cao00314622009-09-28 15:49:08 -04003880 /*
3881 * We can't just convert len to max_blocks because
3882 * If blocksize = 4096 offset = 3072 and len = 2048
3883 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003884 max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
3885 map.m_lblk);
Mingming Cao00314622009-09-28 15:49:08 -04003886 /*
3887 * credits to insert 1 extent into extent tree
3888 */
3889 credits = ext4_chunk_trans_blocks(inode, max_blocks);
3890 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003891 map.m_lblk += ret;
3892 map.m_len = (max_blocks -= ret);
Mingming Cao00314622009-09-28 15:49:08 -04003893 handle = ext4_journal_start(inode, credits);
3894 if (IS_ERR(handle)) {
3895 ret = PTR_ERR(handle);
3896 break;
3897 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003898 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003899 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Mingming Cao00314622009-09-28 15:49:08 -04003900 if (ret <= 0) {
3901 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003902 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Mingming Cao00314622009-09-28 15:49:08 -04003903 "returned error inode#%lu, block=%u, "
3904 "max_blocks=%u", __func__,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003905 inode->i_ino, map.m_lblk, map.m_len);
Mingming Cao00314622009-09-28 15:49:08 -04003906 }
3907 ext4_mark_inode_dirty(handle, inode);
3908 ret2 = ext4_journal_stop(handle);
3909 if (ret <= 0 || ret2 )
3910 break;
3911 }
3912 return ret > 0 ? ret2 : ret;
3913}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003914
Mingming Cao00314622009-09-28 15:49:08 -04003915/*
Eric Sandeen6873fa02008-10-07 00:46:36 -04003916 * Callback function called for each extent to gather FIEMAP information.
3917 */
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04003918static int ext4_ext_fiemap_cb(struct inode *inode, ext4_lblk_t next,
Eric Sandeen6873fa02008-10-07 00:46:36 -04003919 struct ext4_ext_cache *newex, struct ext4_extent *ex,
3920 void *data)
3921{
Eric Sandeen6873fa02008-10-07 00:46:36 -04003922 __u64 logical;
3923 __u64 physical;
3924 __u64 length;
3925 __u32 flags = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003926 int ret = 0;
3927 struct fiemap_extent_info *fieinfo = data;
3928 unsigned char blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003929
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003930 blksize_bits = inode->i_sb->s_blocksize_bits;
3931 logical = (__u64)newex->ec_block << blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003932
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003933 if (newex->ec_start == 0) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003934 /*
3935 * No extent in extent-tree contains block @newex->ec_start,
3936 * then the block may stay in 1)a hole or 2)delayed-extent.
3937 *
3938 * Holes or delayed-extents are processed as follows.
3939 * 1. lookup dirty pages with specified range in pagecache.
3940 * If no page is got, then there is no delayed-extent and
3941 * return with EXT_CONTINUE.
3942 * 2. find the 1st mapped buffer,
3943 * 3. check if the mapped buffer is both in the request range
3944 * and a delayed buffer. If not, there is no delayed-extent,
3945 * then return.
3946 * 4. a delayed-extent is found, the extent will be collected.
3947 */
3948 ext4_lblk_t end = 0;
3949 pgoff_t last_offset;
3950 pgoff_t offset;
3951 pgoff_t index;
Yongqiang Yangb2213492011-05-24 11:36:58 -04003952 pgoff_t start_index = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003953 struct page **pages = NULL;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003954 struct buffer_head *bh = NULL;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003955 struct buffer_head *head = NULL;
3956 unsigned int nr_pages = PAGE_SIZE / sizeof(struct page *);
3957
3958 pages = kmalloc(PAGE_SIZE, GFP_KERNEL);
3959 if (pages == NULL)
3960 return -ENOMEM;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003961
3962 offset = logical >> PAGE_SHIFT;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003963repeat:
3964 last_offset = offset;
3965 head = NULL;
3966 ret = find_get_pages_tag(inode->i_mapping, &offset,
3967 PAGECACHE_TAG_DIRTY, nr_pages, pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04003968
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003969 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
3970 /* First time, try to find a mapped buffer. */
3971 if (ret == 0) {
3972out:
3973 for (index = 0; index < ret; index++)
3974 page_cache_release(pages[index]);
3975 /* just a hole. */
3976 kfree(pages);
3977 return EXT_CONTINUE;
3978 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04003979 index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003980
Yongqiang Yangb2213492011-05-24 11:36:58 -04003981next_page:
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003982 /* Try to find the 1st mapped buffer. */
Yongqiang Yangb2213492011-05-24 11:36:58 -04003983 end = ((__u64)pages[index]->index << PAGE_SHIFT) >>
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003984 blksize_bits;
Yongqiang Yangb2213492011-05-24 11:36:58 -04003985 if (!page_has_buffers(pages[index]))
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003986 goto out;
Yongqiang Yangb2213492011-05-24 11:36:58 -04003987 head = page_buffers(pages[index]);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003988 if (!head)
3989 goto out;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003990
Yongqiang Yangb2213492011-05-24 11:36:58 -04003991 index++;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003992 bh = head;
3993 do {
Yongqiang Yangb2213492011-05-24 11:36:58 -04003994 if (end >= newex->ec_block +
3995 newex->ec_len)
3996 /* The buffer is out of
3997 * the request range.
3998 */
3999 goto out;
4000
4001 if (buffer_mapped(bh) &&
4002 end >= newex->ec_block) {
4003 start_index = index - 1;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004004 /* get the 1st mapped buffer. */
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004005 goto found_mapped_buffer;
4006 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004007
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004008 bh = bh->b_this_page;
4009 end++;
4010 } while (bh != head);
4011
Yongqiang Yangb2213492011-05-24 11:36:58 -04004012 /* No mapped buffer in the range found in this page,
4013 * We need to look up next page.
4014 */
4015 if (index >= ret) {
4016 /* There is no page left, but we need to limit
4017 * newex->ec_len.
4018 */
4019 newex->ec_len = end - newex->ec_block;
4020 goto out;
4021 }
4022 goto next_page;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004023 } else {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004024 /*Find contiguous delayed buffers. */
4025 if (ret > 0 && pages[0]->index == last_offset)
4026 head = page_buffers(pages[0]);
4027 bh = head;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004028 index = 1;
4029 start_index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004030 }
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004031
4032found_mapped_buffer:
4033 if (bh != NULL && buffer_delay(bh)) {
4034 /* 1st or contiguous delayed buffer found. */
4035 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4036 /*
4037 * 1st delayed buffer found, record
4038 * the start of extent.
4039 */
4040 flags |= FIEMAP_EXTENT_DELALLOC;
4041 newex->ec_block = end;
4042 logical = (__u64)end << blksize_bits;
4043 }
4044 /* Find contiguous delayed buffers. */
4045 do {
4046 if (!buffer_delay(bh))
4047 goto found_delayed_extent;
4048 bh = bh->b_this_page;
4049 end++;
4050 } while (bh != head);
4051
Yongqiang Yangb2213492011-05-24 11:36:58 -04004052 for (; index < ret; index++) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004053 if (!page_has_buffers(pages[index])) {
4054 bh = NULL;
4055 break;
4056 }
4057 head = page_buffers(pages[index]);
4058 if (!head) {
4059 bh = NULL;
4060 break;
4061 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004062
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004063 if (pages[index]->index !=
Yongqiang Yangb2213492011-05-24 11:36:58 -04004064 pages[start_index]->index + index
4065 - start_index) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004066 /* Blocks are not contiguous. */
4067 bh = NULL;
4068 break;
4069 }
4070 bh = head;
4071 do {
4072 if (!buffer_delay(bh))
4073 /* Delayed-extent ends. */
4074 goto found_delayed_extent;
4075 bh = bh->b_this_page;
4076 end++;
4077 } while (bh != head);
4078 }
4079 } else if (!(flags & FIEMAP_EXTENT_DELALLOC))
4080 /* a hole found. */
4081 goto out;
4082
4083found_delayed_extent:
4084 newex->ec_len = min(end - newex->ec_block,
4085 (ext4_lblk_t)EXT_INIT_MAX_LEN);
4086 if (ret == nr_pages && bh != NULL &&
4087 newex->ec_len < EXT_INIT_MAX_LEN &&
4088 buffer_delay(bh)) {
4089 /* Have not collected an extent and continue. */
4090 for (index = 0; index < ret; index++)
4091 page_cache_release(pages[index]);
4092 goto repeat;
4093 }
4094
4095 for (index = 0; index < ret; index++)
4096 page_cache_release(pages[index]);
4097 kfree(pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004098 }
4099
4100 physical = (__u64)newex->ec_start << blksize_bits;
4101 length = (__u64)newex->ec_len << blksize_bits;
4102
4103 if (ex && ext4_ext_is_uninitialized(ex))
4104 flags |= FIEMAP_EXTENT_UNWRITTEN;
4105
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04004106 if (next == EXT_MAX_BLOCKS)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004107 flags |= FIEMAP_EXTENT_LAST;
4108
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004109 ret = fiemap_fill_next_extent(fieinfo, logical, physical,
Eric Sandeen6873fa02008-10-07 00:46:36 -04004110 length, flags);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004111 if (ret < 0)
4112 return ret;
4113 if (ret == 1)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004114 return EXT_BREAK;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004115 return EXT_CONTINUE;
4116}
4117
4118/* fiemap flags we can handle specified here */
4119#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
4120
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004121static int ext4_xattr_fiemap(struct inode *inode,
4122 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004123{
4124 __u64 physical = 0;
4125 __u64 length;
4126 __u32 flags = FIEMAP_EXTENT_LAST;
4127 int blockbits = inode->i_sb->s_blocksize_bits;
4128 int error = 0;
4129
4130 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004131 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04004132 struct ext4_iloc iloc;
4133 int offset; /* offset of xattr in inode */
4134
4135 error = ext4_get_inode_loc(inode, &iloc);
4136 if (error)
4137 return error;
4138 physical = iloc.bh->b_blocknr << blockbits;
4139 offset = EXT4_GOOD_OLD_INODE_SIZE +
4140 EXT4_I(inode)->i_extra_isize;
4141 physical += offset;
4142 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
4143 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004144 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004145 } else { /* external block */
4146 physical = EXT4_I(inode)->i_file_acl << blockbits;
4147 length = inode->i_sb->s_blocksize;
4148 }
4149
4150 if (physical)
4151 error = fiemap_fill_next_extent(fieinfo, 0, physical,
4152 length, flags);
4153 return (error < 0 ? error : 0);
4154}
4155
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004156/*
4157 * ext4_ext_punch_hole
4158 *
4159 * Punches a hole of "length" bytes in a file starting
4160 * at byte "offset"
4161 *
4162 * @inode: The inode of the file to punch a hole in
4163 * @offset: The starting byte offset of the hole
4164 * @length: The length of the hole
4165 *
4166 * Returns the number of blocks removed or negative on err
4167 */
4168int ext4_ext_punch_hole(struct file *file, loff_t offset, loff_t length)
4169{
4170 struct inode *inode = file->f_path.dentry->d_inode;
4171 struct super_block *sb = inode->i_sb;
4172 struct ext4_ext_cache cache_ex;
4173 ext4_lblk_t first_block, last_block, num_blocks, iblock, max_blocks;
4174 struct address_space *mapping = inode->i_mapping;
4175 struct ext4_map_blocks map;
4176 handle_t *handle;
4177 loff_t first_block_offset, last_block_offset, block_len;
4178 loff_t first_page, last_page, first_page_offset, last_page_offset;
4179 int ret, credits, blocks_released, err = 0;
4180
4181 first_block = (offset + sb->s_blocksize - 1) >>
4182 EXT4_BLOCK_SIZE_BITS(sb);
4183 last_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
4184
4185 first_block_offset = first_block << EXT4_BLOCK_SIZE_BITS(sb);
4186 last_block_offset = last_block << EXT4_BLOCK_SIZE_BITS(sb);
4187
4188 first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
4189 last_page = (offset + length) >> PAGE_CACHE_SHIFT;
4190
4191 first_page_offset = first_page << PAGE_CACHE_SHIFT;
4192 last_page_offset = last_page << PAGE_CACHE_SHIFT;
4193
4194 /*
4195 * Write out all dirty pages to avoid race conditions
4196 * Then release them.
4197 */
4198 if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
4199 err = filemap_write_and_wait_range(mapping,
4200 first_page_offset == 0 ? 0 : first_page_offset-1,
4201 last_page_offset);
4202
4203 if (err)
4204 return err;
4205 }
4206
4207 /* Now release the pages */
4208 if (last_page_offset > first_page_offset) {
4209 truncate_inode_pages_range(mapping, first_page_offset,
4210 last_page_offset-1);
4211 }
4212
4213 /* finish any pending end_io work */
4214 ext4_flush_completed_IO(inode);
4215
4216 credits = ext4_writepage_trans_blocks(inode);
4217 handle = ext4_journal_start(inode, credits);
4218 if (IS_ERR(handle))
4219 return PTR_ERR(handle);
4220
4221 err = ext4_orphan_add(handle, inode);
4222 if (err)
4223 goto out;
4224
4225 /*
4226 * Now we need to zero out the un block aligned data.
4227 * If the file is smaller than a block, just
4228 * zero out the middle
4229 */
4230 if (first_block > last_block)
4231 ext4_block_zero_page_range(handle, mapping, offset, length);
4232 else {
4233 /* zero out the head of the hole before the first block */
4234 block_len = first_block_offset - offset;
4235 if (block_len > 0)
4236 ext4_block_zero_page_range(handle, mapping,
4237 offset, block_len);
4238
4239 /* zero out the tail of the hole after the last block */
4240 block_len = offset + length - last_block_offset;
4241 if (block_len > 0) {
4242 ext4_block_zero_page_range(handle, mapping,
4243 last_block_offset, block_len);
4244 }
4245 }
4246
4247 /* If there are no blocks to remove, return now */
4248 if (first_block >= last_block)
4249 goto out;
4250
4251 down_write(&EXT4_I(inode)->i_data_sem);
4252 ext4_ext_invalidate_cache(inode);
4253 ext4_discard_preallocations(inode);
4254
4255 /*
4256 * Loop over all the blocks and identify blocks
4257 * that need to be punched out
4258 */
4259 iblock = first_block;
4260 blocks_released = 0;
4261 while (iblock < last_block) {
4262 max_blocks = last_block - iblock;
4263 num_blocks = 1;
4264 memset(&map, 0, sizeof(map));
4265 map.m_lblk = iblock;
4266 map.m_len = max_blocks;
4267 ret = ext4_ext_map_blocks(handle, inode, &map,
4268 EXT4_GET_BLOCKS_PUNCH_OUT_EXT);
4269
4270 if (ret > 0) {
4271 blocks_released += ret;
4272 num_blocks = ret;
4273 } else if (ret == 0) {
4274 /*
4275 * If map blocks could not find the block,
4276 * then it is in a hole. If the hole was
4277 * not already cached, then map blocks should
4278 * put it in the cache. So we can get the hole
4279 * out of the cache
4280 */
4281 memset(&cache_ex, 0, sizeof(cache_ex));
4282 if ((ext4_ext_check_cache(inode, iblock, &cache_ex)) &&
4283 !cache_ex.ec_start) {
4284
4285 /* The hole is cached */
4286 num_blocks = cache_ex.ec_block +
4287 cache_ex.ec_len - iblock;
4288
4289 } else {
4290 /* The block could not be identified */
4291 err = -EIO;
4292 break;
4293 }
4294 } else {
4295 /* Map blocks error */
4296 err = ret;
4297 break;
4298 }
4299
4300 if (num_blocks == 0) {
4301 /* This condition should never happen */
4302 ext_debug("Block lookup failed");
4303 err = -EIO;
4304 break;
4305 }
4306
4307 iblock += num_blocks;
4308 }
4309
4310 if (blocks_released > 0) {
4311 ext4_ext_invalidate_cache(inode);
4312 ext4_discard_preallocations(inode);
4313 }
4314
4315 if (IS_SYNC(inode))
4316 ext4_handle_sync(handle);
4317
4318 up_write(&EXT4_I(inode)->i_data_sem);
4319
4320out:
4321 ext4_orphan_del(handle, inode);
4322 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4323 ext4_mark_inode_dirty(handle, inode);
4324 ext4_journal_stop(handle);
4325 return err;
4326}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004327int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4328 __u64 start, __u64 len)
4329{
4330 ext4_lblk_t start_blk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004331 int error = 0;
4332
4333 /* fallback to generic here if not in extents fmt */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004334 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeen6873fa02008-10-07 00:46:36 -04004335 return generic_block_fiemap(inode, fieinfo, start, len,
4336 ext4_get_block);
4337
4338 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
4339 return -EBADR;
4340
4341 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
4342 error = ext4_xattr_fiemap(inode, fieinfo);
4343 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004344 ext4_lblk_t len_blks;
4345 __u64 last_blk;
4346
Eric Sandeen6873fa02008-10-07 00:46:36 -04004347 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004348 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04004349 if (last_blk >= EXT_MAX_BLOCKS)
4350 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004351 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004352
4353 /*
4354 * Walk the extent tree gathering extent information.
4355 * ext4_ext_fiemap_cb will push extents back to user.
4356 */
Eric Sandeen6873fa02008-10-07 00:46:36 -04004357 error = ext4_ext_walk_space(inode, start_blk, len_blks,
4358 ext4_ext_fiemap_cb, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004359 }
4360
4361 return error;
4362}