blob: 9bec432e2d26869ebfbd6234770e8187fe9abbf3 [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{
Alex Tomasa86c6182006-10-11 01:21:03 -0700117 int depth;
118
119 if (path) {
120 struct ext4_extent *ex;
121 depth = path->p_depth;
122
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500123 /*
124 * Try to predict block placement assuming that we are
125 * filling in a file which will eventually be
126 * non-sparse --- i.e., in the case of libbfd writing
127 * an ELF object sections out-of-order but in a way
128 * the eventually results in a contiguous object or
129 * executable file, or some database extending a table
130 * space file. However, this is actually somewhat
131 * non-ideal if we are writing a sparse file such as
132 * qemu or KVM writing a raw image file that is going
133 * to stay fairly sparse, since it will end up
134 * fragmenting the file system's free space. Maybe we
135 * should have some hueristics or some way to allow
136 * userspace to pass a hint to file system,
Tao Mab8d65682011-01-21 23:21:31 +0800137 * especially if the latter case turns out to be
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500138 * common.
139 */
Avantika Mathur7e028972006-12-06 20:41:33 -0800140 ex = path[depth].p_ext;
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500141 if (ex) {
142 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
143 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
144
145 if (block > ext_block)
146 return ext_pblk + (block - ext_block);
147 else
148 return ext_pblk - (ext_block - block);
149 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700150
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700151 /* it looks like index is empty;
152 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700153 if (path[depth].p_bh)
154 return path[depth].p_bh->b_blocknr;
155 }
156
157 /* OK. use inode's group */
Eric Sandeenf86186b2011-06-28 10:01:31 -0400158 return ext4_inode_to_goal_block(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700159}
160
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400161/*
162 * Allocation for a meta data block
163 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700164static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400165ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700166 struct ext4_ext_path *path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400167 struct ext4_extent *ex, int *err, unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700168{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700169 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700170
171 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Allison Henderson55f020d2011-05-25 07:41:26 -0400172 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
173 NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700174 return newblock;
175}
176
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400177static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700178{
179 int size;
180
181 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
182 / sizeof(struct ext4_extent);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400183 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100184#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400185 if (size > 6)
186 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700187#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400188 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700189 return size;
190}
191
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400192static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700193{
194 int size;
195
196 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
197 / sizeof(struct ext4_extent_idx);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400198 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100199#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400200 if (size > 5)
201 size = 5;
Alex Tomasa86c6182006-10-11 01:21:03 -0700202#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400203 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700204 return size;
205}
206
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400207static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700208{
209 int size;
210
211 size = sizeof(EXT4_I(inode)->i_data);
212 size -= sizeof(struct ext4_extent_header);
213 size /= sizeof(struct ext4_extent);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400214 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100215#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400216 if (size > 3)
217 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700218#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400219 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700220 return size;
221}
222
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400223static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700224{
225 int size;
226
227 size = sizeof(EXT4_I(inode)->i_data);
228 size -= sizeof(struct ext4_extent_header);
229 size /= sizeof(struct ext4_extent_idx);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400230 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100231#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400232 if (size > 4)
233 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700234#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400235 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700236 return size;
237}
238
Mingming Caod2a17632008-07-14 17:52:37 -0400239/*
240 * Calculate the number of metadata blocks needed
241 * to allocate @blocks
242 * Worse case is one block per extent
243 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -0500244int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -0400245{
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500246 struct ext4_inode_info *ei = EXT4_I(inode);
247 int idxs, num = 0;
Mingming Caod2a17632008-07-14 17:52:37 -0400248
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500249 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
250 / sizeof(struct ext4_extent_idx));
Mingming Caod2a17632008-07-14 17:52:37 -0400251
252 /*
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500253 * If the new delayed allocation block is contiguous with the
254 * previous da block, it can share index blocks with the
255 * previous block, so we only need to allocate a new index
256 * block every idxs leaf blocks. At ldxs**2 blocks, we need
257 * an additional index block, and at ldxs**3 blocks, yet
258 * another index blocks.
Mingming Caod2a17632008-07-14 17:52:37 -0400259 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500260 if (ei->i_da_metadata_calc_len &&
261 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
262 if ((ei->i_da_metadata_calc_len % idxs) == 0)
263 num++;
264 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
265 num++;
266 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
267 num++;
268 ei->i_da_metadata_calc_len = 0;
269 } else
270 ei->i_da_metadata_calc_len++;
271 ei->i_da_metadata_calc_last_lblock++;
272 return num;
273 }
Mingming Caod2a17632008-07-14 17:52:37 -0400274
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500275 /*
276 * In the worst case we need a new set of index blocks at
277 * every level of the inode's extent tree.
278 */
279 ei->i_da_metadata_calc_len = 1;
280 ei->i_da_metadata_calc_last_lblock = lblock;
281 return ext_depth(inode) + 1;
Mingming Caod2a17632008-07-14 17:52:37 -0400282}
283
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400284static int
285ext4_ext_max_entries(struct inode *inode, int depth)
286{
287 int max;
288
289 if (depth == ext_depth(inode)) {
290 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400291 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400292 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400293 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400294 } else {
295 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400296 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400297 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400298 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400299 }
300
301 return max;
302}
303
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400304static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
305{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400306 ext4_fsblk_t block = ext4_ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400307 int len = ext4_ext_get_actual_len(ext);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400308
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400309 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400310}
311
312static int ext4_valid_extent_idx(struct inode *inode,
313 struct ext4_extent_idx *ext_idx)
314{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400315 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400316
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400317 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400318}
319
320static int ext4_valid_extent_entries(struct inode *inode,
321 struct ext4_extent_header *eh,
322 int depth)
323{
324 struct ext4_extent *ext;
325 struct ext4_extent_idx *ext_idx;
326 unsigned short entries;
327 if (eh->eh_entries == 0)
328 return 1;
329
330 entries = le16_to_cpu(eh->eh_entries);
331
332 if (depth == 0) {
333 /* leaf entries */
334 ext = EXT_FIRST_EXTENT(eh);
335 while (entries) {
336 if (!ext4_valid_extent(inode, ext))
337 return 0;
338 ext++;
339 entries--;
340 }
341 } else {
342 ext_idx = EXT_FIRST_INDEX(eh);
343 while (entries) {
344 if (!ext4_valid_extent_idx(inode, ext_idx))
345 return 0;
346 ext_idx++;
347 entries--;
348 }
349 }
350 return 1;
351}
352
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400353static int __ext4_ext_check(const char *function, unsigned int line,
354 struct inode *inode, struct ext4_extent_header *eh,
355 int depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400356{
357 const char *error_msg;
358 int max = 0;
359
360 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
361 error_msg = "invalid magic";
362 goto corrupted;
363 }
364 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
365 error_msg = "unexpected eh_depth";
366 goto corrupted;
367 }
368 if (unlikely(eh->eh_max == 0)) {
369 error_msg = "invalid eh_max";
370 goto corrupted;
371 }
372 max = ext4_ext_max_entries(inode, depth);
373 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
374 error_msg = "too large eh_max";
375 goto corrupted;
376 }
377 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
378 error_msg = "invalid eh_entries";
379 goto corrupted;
380 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400381 if (!ext4_valid_extent_entries(inode, eh, depth)) {
382 error_msg = "invalid extent entries";
383 goto corrupted;
384 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400385 return 0;
386
387corrupted:
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400388 ext4_error_inode(inode, function, line, 0,
Theodore Ts'o24676da2010-05-16 21:00:00 -0400389 "bad header/extent: %s - magic %x, "
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400390 "entries %u, max %u(%u), depth %u(%u)",
Theodore Ts'o24676da2010-05-16 21:00:00 -0400391 error_msg, le16_to_cpu(eh->eh_magic),
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400392 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
393 max, le16_to_cpu(eh->eh_depth), depth);
394
395 return -EIO;
396}
397
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400398#define ext4_ext_check(inode, eh, depth) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400399 __ext4_ext_check(__func__, __LINE__, inode, eh, depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400400
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400401int ext4_ext_check_inode(struct inode *inode)
402{
403 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
404}
405
Alex Tomasa86c6182006-10-11 01:21:03 -0700406#ifdef EXT_DEBUG
407static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
408{
409 int k, l = path->p_depth;
410
411 ext_debug("path:");
412 for (k = 0; k <= l; k++, path++) {
413 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700414 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400415 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700416 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400417 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700418 le32_to_cpu(path->p_ext->ee_block),
Mingming553f9002009-09-18 13:34:55 -0400419 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400420 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400421 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700422 } else
423 ext_debug(" []");
424 }
425 ext_debug("\n");
426}
427
428static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
429{
430 int depth = ext_depth(inode);
431 struct ext4_extent_header *eh;
432 struct ext4_extent *ex;
433 int i;
434
435 if (!path)
436 return;
437
438 eh = path[depth].p_hdr;
439 ex = EXT_FIRST_EXTENT(eh);
440
Mingming553f9002009-09-18 13:34:55 -0400441 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
442
Alex Tomasa86c6182006-10-11 01:21:03 -0700443 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400444 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
445 ext4_ext_is_uninitialized(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400446 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700447 }
448 ext_debug("\n");
449}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400450
451static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
452 ext4_fsblk_t newblock, int level)
453{
454 int depth = ext_depth(inode);
455 struct ext4_extent *ex;
456
457 if (depth != level) {
458 struct ext4_extent_idx *idx;
459 idx = path[level].p_idx;
460 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
461 ext_debug("%d: move %d:%llu in new index %llu\n", level,
462 le32_to_cpu(idx->ei_block),
463 ext4_idx_pblock(idx),
464 newblock);
465 idx++;
466 }
467
468 return;
469 }
470
471 ex = path[depth].p_ext;
472 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
473 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
474 le32_to_cpu(ex->ee_block),
475 ext4_ext_pblock(ex),
476 ext4_ext_is_uninitialized(ex),
477 ext4_ext_get_actual_len(ex),
478 newblock);
479 ex++;
480 }
481}
482
Alex Tomasa86c6182006-10-11 01:21:03 -0700483#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400484#define ext4_ext_show_path(inode, path)
485#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400486#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700487#endif
488
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500489void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700490{
491 int depth = path->p_depth;
492 int i;
493
494 for (i = 0; i <= depth; i++, path++)
495 if (path->p_bh) {
496 brelse(path->p_bh);
497 path->p_bh = NULL;
498 }
499}
500
501/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700502 * ext4_ext_binsearch_idx:
503 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400504 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700505 */
506static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500507ext4_ext_binsearch_idx(struct inode *inode,
508 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700509{
510 struct ext4_extent_header *eh = path->p_hdr;
511 struct ext4_extent_idx *r, *l, *m;
512
Alex Tomasa86c6182006-10-11 01:21:03 -0700513
Eric Sandeenbba90742008-01-28 23:58:27 -0500514 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700515
516 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400517 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700518 while (l <= r) {
519 m = l + (r - l) / 2;
520 if (block < le32_to_cpu(m->ei_block))
521 r = m - 1;
522 else
523 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400524 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
525 m, le32_to_cpu(m->ei_block),
526 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700527 }
528
529 path->p_idx = l - 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700530 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400531 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700532
533#ifdef CHECK_BINSEARCH
534 {
535 struct ext4_extent_idx *chix, *ix;
536 int k;
537
538 chix = ix = EXT_FIRST_INDEX(eh);
539 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
540 if (k != 0 &&
541 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o47760042008-09-08 23:00:52 -0400542 printk(KERN_DEBUG "k=%d, ix=0x%p, "
543 "first=0x%p\n", k,
544 ix, EXT_FIRST_INDEX(eh));
545 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700546 le32_to_cpu(ix->ei_block),
547 le32_to_cpu(ix[-1].ei_block));
548 }
549 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400550 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700551 if (block < le32_to_cpu(ix->ei_block))
552 break;
553 chix = ix;
554 }
555 BUG_ON(chix != path->p_idx);
556 }
557#endif
558
559}
560
561/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700562 * ext4_ext_binsearch:
563 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400564 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700565 */
566static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500567ext4_ext_binsearch(struct inode *inode,
568 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700569{
570 struct ext4_extent_header *eh = path->p_hdr;
571 struct ext4_extent *r, *l, *m;
572
Alex Tomasa86c6182006-10-11 01:21:03 -0700573 if (eh->eh_entries == 0) {
574 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700575 * this leaf is empty:
576 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700577 */
578 return;
579 }
580
Eric Sandeenbba90742008-01-28 23:58:27 -0500581 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700582
583 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400584 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700585
586 while (l <= r) {
587 m = l + (r - l) / 2;
588 if (block < le32_to_cpu(m->ee_block))
589 r = m - 1;
590 else
591 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400592 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
593 m, le32_to_cpu(m->ee_block),
594 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700595 }
596
597 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400598 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400599 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400600 ext4_ext_pblock(path->p_ext),
Mingming553f9002009-09-18 13:34:55 -0400601 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400602 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700603
604#ifdef CHECK_BINSEARCH
605 {
606 struct ext4_extent *chex, *ex;
607 int k;
608
609 chex = ex = EXT_FIRST_EXTENT(eh);
610 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
611 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400612 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700613 if (block < le32_to_cpu(ex->ee_block))
614 break;
615 chex = ex;
616 }
617 BUG_ON(chex != path->p_ext);
618 }
619#endif
620
621}
622
623int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
624{
625 struct ext4_extent_header *eh;
626
627 eh = ext_inode_hdr(inode);
628 eh->eh_depth = 0;
629 eh->eh_entries = 0;
630 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400631 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700632 ext4_mark_inode_dirty(handle, inode);
633 ext4_ext_invalidate_cache(inode);
634 return 0;
635}
636
637struct ext4_ext_path *
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500638ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
639 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700640{
641 struct ext4_extent_header *eh;
642 struct buffer_head *bh;
643 short int depth, i, ppos = 0, alloc = 0;
644
645 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400646 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700647
648 /* account possible depth increase */
649 if (!path) {
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800650 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700651 GFP_NOFS);
652 if (!path)
653 return ERR_PTR(-ENOMEM);
654 alloc = 1;
655 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700656 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400657 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700658
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400659 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700660 /* walk through the tree */
661 while (i) {
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400662 int need_to_validate = 0;
663
Alex Tomasa86c6182006-10-11 01:21:03 -0700664 ext_debug("depth %d: num %d, max %d\n",
665 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400666
Alex Tomasa86c6182006-10-11 01:21:03 -0700667 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400668 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700669 path[ppos].p_depth = i;
670 path[ppos].p_ext = NULL;
671
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400672 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
673 if (unlikely(!bh))
Alex Tomasa86c6182006-10-11 01:21:03 -0700674 goto err;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400675 if (!bh_uptodate_or_lock(bh)) {
Jiaying Zhang0562e0b2011-03-21 21:38:05 -0400676 trace_ext4_ext_load_extent(inode, block,
677 path[ppos].p_block);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400678 if (bh_submit_read(bh) < 0) {
679 put_bh(bh);
680 goto err;
681 }
682 /* validate the extent entries */
683 need_to_validate = 1;
684 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700685 eh = ext_block_hdr(bh);
686 ppos++;
Frank Mayhar273df552010-03-02 11:46:09 -0500687 if (unlikely(ppos > depth)) {
688 put_bh(bh);
689 EXT4_ERROR_INODE(inode,
690 "ppos %d > depth %d", ppos, depth);
691 goto err;
692 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700693 path[ppos].p_bh = bh;
694 path[ppos].p_hdr = eh;
695 i--;
696
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400697 if (need_to_validate && ext4_ext_check(inode, eh, i))
Alex Tomasa86c6182006-10-11 01:21:03 -0700698 goto err;
699 }
700
701 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700702 path[ppos].p_ext = NULL;
703 path[ppos].p_idx = NULL;
704
Alex Tomasa86c6182006-10-11 01:21:03 -0700705 /* find extent */
706 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400707 /* if not an empty leaf */
708 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400709 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700710
711 ext4_ext_show_path(inode, path);
712
713 return path;
714
715err:
716 ext4_ext_drop_refs(path);
717 if (alloc)
718 kfree(path);
719 return ERR_PTR(-EIO);
720}
721
722/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700723 * ext4_ext_insert_index:
724 * insert new index [@logical;@ptr] into the block at @curp;
725 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700726 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400727static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
728 struct ext4_ext_path *curp,
729 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700730{
731 struct ext4_extent_idx *ix;
732 int len, err;
733
Avantika Mathur7e028972006-12-06 20:41:33 -0800734 err = ext4_ext_get_access(handle, inode, curp);
735 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700736 return err;
737
Frank Mayhar273df552010-03-02 11:46:09 -0500738 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
739 EXT4_ERROR_INODE(inode,
740 "logical %d == ei_block %d!",
741 logical, le32_to_cpu(curp->p_idx->ei_block));
742 return -EIO;
743 }
Robin Dongd4620312011-07-17 23:43:42 -0400744
745 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
746 >= le16_to_cpu(curp->p_hdr->eh_max))) {
747 EXT4_ERROR_INODE(inode,
748 "eh_entries %d >= eh_max %d!",
749 le16_to_cpu(curp->p_hdr->eh_entries),
750 le16_to_cpu(curp->p_hdr->eh_max));
751 return -EIO;
752 }
753
Alex Tomasa86c6182006-10-11 01:21:03 -0700754 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
755 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
756 /* insert after */
757 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
758 len = (len - 1) * sizeof(struct ext4_extent_idx);
759 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400760 ext_debug("insert new index %d after: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700761 "move %d from 0x%p to 0x%p\n",
762 logical, ptr, len,
763 (curp->p_idx + 1), (curp->p_idx + 2));
764 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
765 }
766 ix = curp->p_idx + 1;
767 } else {
768 /* insert before */
769 len = len * sizeof(struct ext4_extent_idx);
770 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400771 ext_debug("insert new index %d before: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700772 "move %d from 0x%p to 0x%p\n",
773 logical, ptr, len,
774 curp->p_idx, (curp->p_idx + 1));
775 memmove(curp->p_idx + 1, curp->p_idx, len);
776 ix = curp->p_idx;
777 }
778
779 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700780 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400781 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700782
Frank Mayhar273df552010-03-02 11:46:09 -0500783 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
784 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
785 return -EIO;
786 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700787
788 err = ext4_ext_dirty(handle, inode, curp);
789 ext4_std_error(inode->i_sb, err);
790
791 return err;
792}
793
794/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700795 * ext4_ext_split:
796 * inserts new subtree into the path, using free index entry
797 * at depth @at:
798 * - allocates all needed blocks (new leaf and all intermediate index blocks)
799 * - makes decision where to split
800 * - moves remaining extents and index entries (right to the split point)
801 * into the newly allocated blocks
802 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700803 */
804static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -0400805 unsigned int flags,
806 struct ext4_ext_path *path,
807 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -0700808{
809 struct buffer_head *bh = NULL;
810 int depth = ext_depth(inode);
811 struct ext4_extent_header *neh;
812 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -0700813 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700814 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700815 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700816 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700817 int err = 0;
818
819 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700820 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700821
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700822 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700823 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -0500824 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
825 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
826 return -EIO;
827 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700828 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
829 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700830 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700831 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400832 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700833 } else {
834 border = newext->ee_block;
835 ext_debug("leaf will be added."
836 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400837 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700838 }
839
840 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700841 * If error occurs, then we break processing
842 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700843 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700844 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700845 */
846
847 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700848 * Get array to track all allocated blocks.
849 * We need this to handle errors and free blocks
850 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700851 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800852 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700853 if (!ablocks)
854 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700855
856 /* allocate all needed blocks */
857 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
858 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400859 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400860 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -0700861 if (newblock == 0)
862 goto cleanup;
863 ablocks[a] = newblock;
864 }
865
866 /* initialize new leaf */
867 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -0500868 if (unlikely(newblock == 0)) {
869 EXT4_ERROR_INODE(inode, "newblock == 0!");
870 err = -EIO;
871 goto cleanup;
872 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700873 bh = sb_getblk(inode->i_sb, newblock);
874 if (!bh) {
875 err = -EIO;
876 goto cleanup;
877 }
878 lock_buffer(bh);
879
Avantika Mathur7e028972006-12-06 20:41:33 -0800880 err = ext4_journal_get_create_access(handle, bh);
881 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700882 goto cleanup;
883
884 neh = ext_block_hdr(bh);
885 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400886 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700887 neh->eh_magic = EXT4_EXT_MAGIC;
888 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700889
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700890 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -0500891 if (unlikely(path[depth].p_hdr->eh_entries !=
892 path[depth].p_hdr->eh_max)) {
893 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
894 path[depth].p_hdr->eh_entries,
895 path[depth].p_hdr->eh_max);
896 err = -EIO;
897 goto cleanup;
898 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700899 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400900 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
901 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -0700902 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400903 struct ext4_extent *ex;
904 ex = EXT_FIRST_EXTENT(neh);
905 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400906 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700907 }
908
909 set_buffer_uptodate(bh);
910 unlock_buffer(bh);
911
Frank Mayhar03901312009-01-07 00:06:22 -0500912 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800913 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700914 goto cleanup;
915 brelse(bh);
916 bh = NULL;
917
918 /* correct old leaf */
919 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800920 err = ext4_ext_get_access(handle, inode, path + depth);
921 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700922 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400923 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800924 err = ext4_ext_dirty(handle, inode, path + depth);
925 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700926 goto cleanup;
927
928 }
929
930 /* create intermediate indexes */
931 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -0500932 if (unlikely(k < 0)) {
933 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
934 err = -EIO;
935 goto cleanup;
936 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700937 if (k)
938 ext_debug("create %d intermediate indices\n", k);
939 /* insert new index into current index block */
940 /* current depth stored in i var */
941 i = depth - 1;
942 while (k--) {
943 oldblock = newblock;
944 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -0500945 bh = sb_getblk(inode->i_sb, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700946 if (!bh) {
947 err = -EIO;
948 goto cleanup;
949 }
950 lock_buffer(bh);
951
Avantika Mathur7e028972006-12-06 20:41:33 -0800952 err = ext4_journal_get_create_access(handle, bh);
953 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700954 goto cleanup;
955
956 neh = ext_block_hdr(bh);
957 neh->eh_entries = cpu_to_le16(1);
958 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400959 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700960 neh->eh_depth = cpu_to_le16(depth - i);
961 fidx = EXT_FIRST_INDEX(neh);
962 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700963 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700964
Eric Sandeenbba90742008-01-28 23:58:27 -0500965 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
966 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700967
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400968 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -0500969 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
970 EXT_LAST_INDEX(path[i].p_hdr))) {
971 EXT4_ERROR_INODE(inode,
972 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
973 le32_to_cpu(path[i].p_ext->ee_block));
974 err = -EIO;
975 goto cleanup;
976 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400977 /* start copy indexes */
978 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
979 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
980 EXT_MAX_INDEX(path[i].p_hdr));
981 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -0700982 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400983 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -0700984 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400985 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700986 }
987 set_buffer_uptodate(bh);
988 unlock_buffer(bh);
989
Frank Mayhar03901312009-01-07 00:06:22 -0500990 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800991 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700992 goto cleanup;
993 brelse(bh);
994 bh = NULL;
995
996 /* correct old index */
997 if (m) {
998 err = ext4_ext_get_access(handle, inode, path + i);
999 if (err)
1000 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001001 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001002 err = ext4_ext_dirty(handle, inode, path + i);
1003 if (err)
1004 goto cleanup;
1005 }
1006
1007 i--;
1008 }
1009
1010 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001011 err = ext4_ext_insert_index(handle, inode, path + at,
1012 le32_to_cpu(border), newblock);
1013
1014cleanup:
1015 if (bh) {
1016 if (buffer_locked(bh))
1017 unlock_buffer(bh);
1018 brelse(bh);
1019 }
1020
1021 if (err) {
1022 /* free all allocated blocks in error case */
1023 for (i = 0; i < depth; i++) {
1024 if (!ablocks[i])
1025 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001026 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001027 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001028 }
1029 }
1030 kfree(ablocks);
1031
1032 return err;
1033}
1034
1035/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001036 * ext4_ext_grow_indepth:
1037 * implements tree growing procedure:
1038 * - allocates new block
1039 * - moves top-level data (index block or leaf) into the new block
1040 * - initializes new top-level, creating index that points to the
1041 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001042 */
1043static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001044 unsigned int flags,
1045 struct ext4_ext_path *path,
1046 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001047{
1048 struct ext4_ext_path *curp = path;
1049 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001050 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001051 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001052 int err = 0;
1053
Allison Henderson55f020d2011-05-25 07:41:26 -04001054 newblock = ext4_ext_new_meta_block(handle, inode, path,
1055 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001056 if (newblock == 0)
1057 return err;
1058
1059 bh = sb_getblk(inode->i_sb, newblock);
1060 if (!bh) {
1061 err = -EIO;
1062 ext4_std_error(inode->i_sb, err);
1063 return err;
1064 }
1065 lock_buffer(bh);
1066
Avantika Mathur7e028972006-12-06 20:41:33 -08001067 err = ext4_journal_get_create_access(handle, bh);
1068 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001069 unlock_buffer(bh);
1070 goto out;
1071 }
1072
1073 /* move top-level index/leaf into new block */
1074 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
1075
1076 /* set size of new block */
1077 neh = ext_block_hdr(bh);
1078 /* old root could have indexes or leaves
1079 * so calculate e_max right way */
1080 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001081 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001082 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001083 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001084 neh->eh_magic = EXT4_EXT_MAGIC;
1085 set_buffer_uptodate(bh);
1086 unlock_buffer(bh);
1087
Frank Mayhar03901312009-01-07 00:06:22 -05001088 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001089 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001090 goto out;
1091
1092 /* create index in new top-level index: num,max,pointer */
Avantika Mathur7e028972006-12-06 20:41:33 -08001093 err = ext4_ext_get_access(handle, inode, curp);
1094 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001095 goto out;
1096
1097 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001098 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001099 curp->p_hdr->eh_entries = cpu_to_le16(1);
1100 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
Dmitry Monakhove9f410b2007-07-18 09:09:15 -04001101
1102 if (path[0].p_hdr->eh_depth)
1103 curp->p_idx->ei_block =
1104 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
1105 else
1106 curp->p_idx->ei_block =
1107 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001108 ext4_idx_store_pblock(curp->p_idx, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001109
1110 neh = ext_inode_hdr(inode);
Mingming Cao2ae02102006-10-11 01:21:11 -07001111 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001112 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001113 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001114 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001115
1116 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
1117 err = ext4_ext_dirty(handle, inode, curp);
1118out:
1119 brelse(bh);
1120
1121 return err;
1122}
1123
1124/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001125 * ext4_ext_create_new_leaf:
1126 * finds empty index and adds new leaf.
1127 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001128 */
1129static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001130 unsigned int flags,
1131 struct ext4_ext_path *path,
1132 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001133{
1134 struct ext4_ext_path *curp;
1135 int depth, i, err = 0;
1136
1137repeat:
1138 i = depth = ext_depth(inode);
1139
1140 /* walk up to the tree and look for free index entry */
1141 curp = path + depth;
1142 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1143 i--;
1144 curp--;
1145 }
1146
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001147 /* we use already allocated block for index block,
1148 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001149 if (EXT_HAS_FREE_INDEX(curp)) {
1150 /* if we found index with free entry, then use that
1151 * entry: create all needed subtree and add new leaf */
Allison Henderson55f020d2011-05-25 07:41:26 -04001152 err = ext4_ext_split(handle, inode, flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001153 if (err)
1154 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001155
1156 /* refill path */
1157 ext4_ext_drop_refs(path);
1158 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001159 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1160 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001161 if (IS_ERR(path))
1162 err = PTR_ERR(path);
1163 } else {
1164 /* tree is full, time to grow in depth */
Allison Henderson55f020d2011-05-25 07:41:26 -04001165 err = ext4_ext_grow_indepth(handle, inode, flags,
1166 path, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001167 if (err)
1168 goto out;
1169
1170 /* refill path */
1171 ext4_ext_drop_refs(path);
1172 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001173 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1174 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001175 if (IS_ERR(path)) {
1176 err = PTR_ERR(path);
1177 goto out;
1178 }
1179
1180 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001181 * only first (depth 0 -> 1) produces free space;
1182 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001183 */
1184 depth = ext_depth(inode);
1185 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001186 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001187 goto repeat;
1188 }
1189 }
1190
1191out:
1192 return err;
1193}
1194
1195/*
Alex Tomas1988b512008-01-28 23:58:27 -05001196 * search the closest allocated block to the left for *logical
1197 * and returns it at @logical + it's physical address at @phys
1198 * if *logical is the smallest allocated block, the function
1199 * returns 0 at @phys
1200 * return value contains 0 (success) or error code
1201 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001202static int ext4_ext_search_left(struct inode *inode,
1203 struct ext4_ext_path *path,
1204 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001205{
1206 struct ext4_extent_idx *ix;
1207 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001208 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001209
Frank Mayhar273df552010-03-02 11:46:09 -05001210 if (unlikely(path == NULL)) {
1211 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1212 return -EIO;
1213 }
Alex Tomas1988b512008-01-28 23:58:27 -05001214 depth = path->p_depth;
1215 *phys = 0;
1216
1217 if (depth == 0 && path->p_ext == NULL)
1218 return 0;
1219
1220 /* usually extent in the path covers blocks smaller
1221 * then *logical, but it can be that extent is the
1222 * first one in the file */
1223
1224 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001225 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001226 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001227 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1228 EXT4_ERROR_INODE(inode,
1229 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1230 *logical, le32_to_cpu(ex->ee_block));
1231 return -EIO;
1232 }
Alex Tomas1988b512008-01-28 23:58:27 -05001233 while (--depth >= 0) {
1234 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001235 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1236 EXT4_ERROR_INODE(inode,
1237 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
1238 ix != NULL ? ix->ei_block : 0,
1239 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
1240 EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block : 0,
1241 depth);
1242 return -EIO;
1243 }
Alex Tomas1988b512008-01-28 23:58:27 -05001244 }
1245 return 0;
1246 }
1247
Frank Mayhar273df552010-03-02 11:46:09 -05001248 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1249 EXT4_ERROR_INODE(inode,
1250 "logical %d < ee_block %d + ee_len %d!",
1251 *logical, le32_to_cpu(ex->ee_block), ee_len);
1252 return -EIO;
1253 }
Alex Tomas1988b512008-01-28 23:58:27 -05001254
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001255 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001256 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001257 return 0;
1258}
1259
1260/*
1261 * search the closest allocated block to the right for *logical
1262 * and returns it at @logical + it's physical address at @phys
1263 * if *logical is the smallest allocated block, the function
1264 * returns 0 at @phys
1265 * return value contains 0 (success) or error code
1266 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001267static int ext4_ext_search_right(struct inode *inode,
1268 struct ext4_ext_path *path,
1269 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001270{
1271 struct buffer_head *bh = NULL;
1272 struct ext4_extent_header *eh;
1273 struct ext4_extent_idx *ix;
1274 struct ext4_extent *ex;
1275 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001276 int depth; /* Note, NOT eh_depth; depth from top of tree */
1277 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001278
Frank Mayhar273df552010-03-02 11:46:09 -05001279 if (unlikely(path == NULL)) {
1280 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1281 return -EIO;
1282 }
Alex Tomas1988b512008-01-28 23:58:27 -05001283 depth = path->p_depth;
1284 *phys = 0;
1285
1286 if (depth == 0 && path->p_ext == NULL)
1287 return 0;
1288
1289 /* usually extent in the path covers blocks smaller
1290 * then *logical, but it can be that extent is the
1291 * first one in the file */
1292
1293 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001294 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001295 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001296 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1297 EXT4_ERROR_INODE(inode,
1298 "first_extent(path[%d].p_hdr) != ex",
1299 depth);
1300 return -EIO;
1301 }
Alex Tomas1988b512008-01-28 23:58:27 -05001302 while (--depth >= 0) {
1303 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001304 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1305 EXT4_ERROR_INODE(inode,
1306 "ix != EXT_FIRST_INDEX *logical %d!",
1307 *logical);
1308 return -EIO;
1309 }
Alex Tomas1988b512008-01-28 23:58:27 -05001310 }
1311 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001312 *phys = ext4_ext_pblock(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001313 return 0;
1314 }
1315
Frank Mayhar273df552010-03-02 11:46:09 -05001316 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1317 EXT4_ERROR_INODE(inode,
1318 "logical %d < ee_block %d + ee_len %d!",
1319 *logical, le32_to_cpu(ex->ee_block), ee_len);
1320 return -EIO;
1321 }
Alex Tomas1988b512008-01-28 23:58:27 -05001322
1323 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1324 /* next allocated block in this leaf */
1325 ex++;
1326 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001327 *phys = ext4_ext_pblock(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001328 return 0;
1329 }
1330
1331 /* go up and search for index to the right */
1332 while (--depth >= 0) {
1333 ix = path[depth].p_idx;
1334 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001335 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001336 }
1337
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001338 /* we've gone up to the root and found no index to the right */
1339 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001340
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001341got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001342 /* we've found index to the right, let's
1343 * follow it and find the closest allocated
1344 * block to the right */
1345 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001346 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001347 while (++depth < path->p_depth) {
1348 bh = sb_bread(inode->i_sb, block);
1349 if (bh == NULL)
1350 return -EIO;
1351 eh = ext_block_hdr(bh);
Eric Sandeen395a87b2009-03-10 18:18:47 -04001352 /* subtract from p_depth to get proper eh_depth */
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001353 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001354 put_bh(bh);
1355 return -EIO;
1356 }
1357 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001358 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001359 put_bh(bh);
1360 }
1361
1362 bh = sb_bread(inode->i_sb, block);
1363 if (bh == NULL)
1364 return -EIO;
1365 eh = ext_block_hdr(bh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001366 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001367 put_bh(bh);
1368 return -EIO;
1369 }
1370 ex = EXT_FIRST_EXTENT(eh);
1371 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001372 *phys = ext4_ext_pblock(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001373 put_bh(bh);
1374 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001375}
1376
1377/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001378 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001379 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001380 * NOTE: it considers block number from index entry as
1381 * allocated block. Thus, index entries have to be consistent
1382 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001383 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001384static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001385ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1386{
1387 int depth;
1388
1389 BUG_ON(path == NULL);
1390 depth = path->p_depth;
1391
1392 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001393 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001394
1395 while (depth >= 0) {
1396 if (depth == path->p_depth) {
1397 /* leaf */
1398 if (path[depth].p_ext !=
1399 EXT_LAST_EXTENT(path[depth].p_hdr))
1400 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1401 } else {
1402 /* index */
1403 if (path[depth].p_idx !=
1404 EXT_LAST_INDEX(path[depth].p_hdr))
1405 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1406 }
1407 depth--;
1408 }
1409
Lukas Czernerf17722f2011-06-06 00:05:17 -04001410 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001411}
1412
1413/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001414 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001415 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001416 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001417static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
Andrew Morton63f57932006-10-11 01:21:24 -07001418 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001419{
1420 int depth;
1421
1422 BUG_ON(path == NULL);
1423 depth = path->p_depth;
1424
1425 /* zero-tree has no leaf blocks at all */
1426 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001427 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001428
1429 /* go to index block */
1430 depth--;
1431
1432 while (depth >= 0) {
1433 if (path[depth].p_idx !=
1434 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001435 return (ext4_lblk_t)
1436 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001437 depth--;
1438 }
1439
Lukas Czernerf17722f2011-06-06 00:05:17 -04001440 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001441}
1442
1443/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001444 * ext4_ext_correct_indexes:
1445 * if leaf gets modified and modified extent is first in the leaf,
1446 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001447 * TODO: do we need to correct tree in all cases?
1448 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001449static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001450 struct ext4_ext_path *path)
1451{
1452 struct ext4_extent_header *eh;
1453 int depth = ext_depth(inode);
1454 struct ext4_extent *ex;
1455 __le32 border;
1456 int k, err = 0;
1457
1458 eh = path[depth].p_hdr;
1459 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001460
1461 if (unlikely(ex == NULL || eh == NULL)) {
1462 EXT4_ERROR_INODE(inode,
1463 "ex %p == NULL or eh %p == NULL", ex, eh);
1464 return -EIO;
1465 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001466
1467 if (depth == 0) {
1468 /* there is no tree at all */
1469 return 0;
1470 }
1471
1472 if (ex != EXT_FIRST_EXTENT(eh)) {
1473 /* we correct tree if first leaf got modified only */
1474 return 0;
1475 }
1476
1477 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001478 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001479 */
1480 k = depth - 1;
1481 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001482 err = ext4_ext_get_access(handle, inode, path + k);
1483 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001484 return err;
1485 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001486 err = ext4_ext_dirty(handle, inode, path + k);
1487 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001488 return err;
1489
1490 while (k--) {
1491 /* change all left-side indexes */
1492 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1493 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001494 err = ext4_ext_get_access(handle, inode, path + k);
1495 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001496 break;
1497 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001498 err = ext4_ext_dirty(handle, inode, path + k);
1499 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001500 break;
1501 }
1502
1503 return err;
1504}
1505
Akira Fujita748de672009-06-17 19:24:03 -04001506int
Alex Tomasa86c6182006-10-11 01:21:03 -07001507ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1508 struct ext4_extent *ex2)
1509{
Amit Arora749269f2007-07-18 09:02:56 -04001510 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001511
1512 /*
1513 * Make sure that either both extents are uninitialized, or
1514 * both are _not_.
1515 */
1516 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1517 return 0;
1518
Amit Arora749269f2007-07-18 09:02:56 -04001519 if (ext4_ext_is_uninitialized(ex1))
1520 max_len = EXT_UNINIT_MAX_LEN;
1521 else
1522 max_len = EXT_INIT_MAX_LEN;
1523
Amit Aroraa2df2a62007-07-17 21:42:41 -04001524 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1525 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1526
1527 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001528 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001529 return 0;
1530
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001531 /*
1532 * To allow future support for preallocated extents to be added
1533 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001534 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001535 */
Amit Arora749269f2007-07-18 09:02:56 -04001536 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001537 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001538#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001539 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001540 return 0;
1541#endif
1542
Theodore Ts'obf89d162010-10-27 21:30:14 -04001543 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001544 return 1;
1545 return 0;
1546}
1547
1548/*
Amit Arora56055d32007-07-17 21:42:38 -04001549 * This function tries to merge the "ex" extent to the next extent in the tree.
1550 * It always tries to merge towards right. If you want to merge towards
1551 * left, pass "ex - 1" as argument instead of "ex".
1552 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1553 * 1 if they got merged.
1554 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001555static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001556 struct ext4_ext_path *path,
1557 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001558{
1559 struct ext4_extent_header *eh;
1560 unsigned int depth, len;
1561 int merge_done = 0;
1562 int uninitialized = 0;
1563
1564 depth = ext_depth(inode);
1565 BUG_ON(path[depth].p_hdr == NULL);
1566 eh = path[depth].p_hdr;
1567
1568 while (ex < EXT_LAST_EXTENT(eh)) {
1569 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1570 break;
1571 /* merge with next extent! */
1572 if (ext4_ext_is_uninitialized(ex))
1573 uninitialized = 1;
1574 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1575 + ext4_ext_get_actual_len(ex + 1));
1576 if (uninitialized)
1577 ext4_ext_mark_uninitialized(ex);
1578
1579 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1580 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1581 * sizeof(struct ext4_extent);
1582 memmove(ex + 1, ex + 2, len);
1583 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001584 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001585 merge_done = 1;
1586 WARN_ON(eh->eh_entries == 0);
1587 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001588 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001589 }
1590
1591 return merge_done;
1592}
1593
1594/*
Yongqiang Yang197217a2011-05-03 11:45:29 -04001595 * This function tries to merge the @ex extent to neighbours in the tree.
1596 * return 1 if merge left else 0.
1597 */
1598static int ext4_ext_try_to_merge(struct inode *inode,
1599 struct ext4_ext_path *path,
1600 struct ext4_extent *ex) {
1601 struct ext4_extent_header *eh;
1602 unsigned int depth;
1603 int merge_done = 0;
1604 int ret = 0;
1605
1606 depth = ext_depth(inode);
1607 BUG_ON(path[depth].p_hdr == NULL);
1608 eh = path[depth].p_hdr;
1609
1610 if (ex > EXT_FIRST_EXTENT(eh))
1611 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1612
1613 if (!merge_done)
1614 ret = ext4_ext_try_to_merge_right(inode, path, ex);
1615
1616 return ret;
1617}
1618
1619/*
Amit Arora25d14f92007-05-24 13:04:13 -04001620 * check if a portion of the "newext" extent overlaps with an
1621 * existing extent.
1622 *
1623 * If there is an overlap discovered, it updates the length of the newext
1624 * such that there will be no overlap, and then returns 1.
1625 * If there is no overlap found, it returns 0.
1626 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001627static unsigned int ext4_ext_check_overlap(struct inode *inode,
1628 struct ext4_extent *newext,
1629 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001630{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001631 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001632 unsigned int depth, len1;
1633 unsigned int ret = 0;
1634
1635 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001636 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001637 depth = ext_depth(inode);
1638 if (!path[depth].p_ext)
1639 goto out;
1640 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1641
1642 /*
1643 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001644 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001645 */
1646 if (b2 < b1) {
1647 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001648 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001649 goto out;
1650 }
1651
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001652 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001653 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001654 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001655 newext->ee_len = cpu_to_le16(len1);
1656 ret = 1;
1657 }
1658
1659 /* check for overlap */
1660 if (b1 + len1 > b2) {
1661 newext->ee_len = cpu_to_le16(b2 - b1);
1662 ret = 1;
1663 }
1664out:
1665 return ret;
1666}
1667
1668/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001669 * ext4_ext_insert_extent:
1670 * tries to merge requsted extent into the existing extent or
1671 * inserts requested extent as new one into the tree,
1672 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001673 */
1674int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1675 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04001676 struct ext4_extent *newext, int flag)
Alex Tomasa86c6182006-10-11 01:21:03 -07001677{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001678 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001679 struct ext4_extent *ex, *fex;
1680 struct ext4_extent *nearex; /* nearest extent */
1681 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001682 int depth, len, err;
1683 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001684 unsigned uninitialized = 0;
Allison Henderson55f020d2011-05-25 07:41:26 -04001685 int flags = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001686
Frank Mayhar273df552010-03-02 11:46:09 -05001687 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1688 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1689 return -EIO;
1690 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001691 depth = ext_depth(inode);
1692 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001693 if (unlikely(path[depth].p_hdr == NULL)) {
1694 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1695 return -EIO;
1696 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001697
1698 /* try to insert block into found extent and return */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001699 if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
Mingming Cao00314622009-09-28 15:49:08 -04001700 && ext4_can_extents_be_merged(inode, ex, newext)) {
Mingming553f9002009-09-18 13:34:55 -04001701 ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04001702 ext4_ext_is_uninitialized(newext),
1703 ext4_ext_get_actual_len(newext),
1704 le32_to_cpu(ex->ee_block),
1705 ext4_ext_is_uninitialized(ex),
1706 ext4_ext_get_actual_len(ex),
1707 ext4_ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001708 err = ext4_ext_get_access(handle, inode, path + depth);
1709 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001710 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001711
1712 /*
1713 * ext4_can_extents_be_merged should have checked that either
1714 * both extents are uninitialized, or both aren't. Thus we
1715 * need to check only one of them here.
1716 */
1717 if (ext4_ext_is_uninitialized(ex))
1718 uninitialized = 1;
1719 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1720 + ext4_ext_get_actual_len(newext));
1721 if (uninitialized)
1722 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001723 eh = path[depth].p_hdr;
1724 nearex = ex;
1725 goto merge;
1726 }
1727
Alex Tomasa86c6182006-10-11 01:21:03 -07001728 depth = ext_depth(inode);
1729 eh = path[depth].p_hdr;
1730 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1731 goto has_space;
1732
1733 /* probably next leaf has space for us? */
1734 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04001735 next = EXT_MAX_BLOCKS;
1736 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
1737 next = ext4_ext_next_leaf_block(inode, path);
1738 if (next != EXT_MAX_BLOCKS) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001739 ext_debug("next leaf block - %d\n", next);
1740 BUG_ON(npath != NULL);
1741 npath = ext4_ext_find_extent(inode, next, NULL);
1742 if (IS_ERR(npath))
1743 return PTR_ERR(npath);
1744 BUG_ON(npath->p_depth != path->p_depth);
1745 eh = npath[depth].p_hdr;
1746 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001747 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001748 le16_to_cpu(eh->eh_entries));
1749 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04001750 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07001751 }
1752 ext_debug("next leaf has no free space(%d,%d)\n",
1753 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1754 }
1755
1756 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001757 * There is no free space in the found leaf.
1758 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001759 */
Allison Henderson55f020d2011-05-25 07:41:26 -04001760 if (flag & EXT4_GET_BLOCKS_PUNCH_OUT_EXT)
1761 flags = EXT4_MB_USE_ROOT_BLOCKS;
1762 err = ext4_ext_create_new_leaf(handle, inode, flags, path, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001763 if (err)
1764 goto cleanup;
1765 depth = ext_depth(inode);
1766 eh = path[depth].p_hdr;
1767
1768has_space:
1769 nearex = path[depth].p_ext;
1770
Avantika Mathur7e028972006-12-06 20:41:33 -08001771 err = ext4_ext_get_access(handle, inode, path + depth);
1772 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001773 goto cleanup;
1774
1775 if (!nearex) {
1776 /* there is no extent in this leaf, create first one */
Mingming553f9002009-09-18 13:34:55 -04001777 ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001778 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001779 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001780 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001781 ext4_ext_get_actual_len(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001782 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1783 } else if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001784 > le32_to_cpu(nearex->ee_block)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001785/* BUG_ON(newext->ee_block == nearex->ee_block); */
1786 if (nearex != EXT_LAST_EXTENT(eh)) {
1787 len = EXT_MAX_EXTENT(eh) - nearex;
1788 len = (len - 1) * sizeof(struct ext4_extent);
1789 len = len < 0 ? 0 : len;
Mingming553f9002009-09-18 13:34:55 -04001790 ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001791 "move %d from 0x%p to 0x%p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001792 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001793 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001794 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001795 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001796 nearex, len, nearex + 1, nearex + 2);
1797 memmove(nearex + 2, nearex + 1, len);
1798 }
1799 path[depth].p_ext = nearex + 1;
1800 } else {
1801 BUG_ON(newext->ee_block == nearex->ee_block);
1802 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1803 len = len < 0 ? 0 : len;
Mingming553f9002009-09-18 13:34:55 -04001804 ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001805 "move %d from 0x%p to 0x%p\n",
1806 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001807 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001808 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001809 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001810 nearex, len, nearex + 1, nearex + 2);
1811 memmove(nearex + 1, nearex, len);
1812 path[depth].p_ext = nearex;
1813 }
1814
Marcin Slusarze8546d02008-04-17 10:38:59 -04001815 le16_add_cpu(&eh->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001816 nearex = path[depth].p_ext;
1817 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001818 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001819 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001820
1821merge:
1822 /* try to merge extents to the right */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001823 if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
Mingming Cao00314622009-09-28 15:49:08 -04001824 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001825
1826 /* try to merge extents to the left */
1827
1828 /* time to correct all indexes above */
1829 err = ext4_ext_correct_indexes(handle, inode, path);
1830 if (err)
1831 goto cleanup;
1832
1833 err = ext4_ext_dirty(handle, inode, path + depth);
1834
1835cleanup:
1836 if (npath) {
1837 ext4_ext_drop_refs(npath);
1838 kfree(npath);
1839 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001840 ext4_ext_invalidate_cache(inode);
1841 return err;
1842}
1843
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001844static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1845 ext4_lblk_t num, ext_prepare_callback func,
1846 void *cbdata)
Eric Sandeen6873fa02008-10-07 00:46:36 -04001847{
1848 struct ext4_ext_path *path = NULL;
1849 struct ext4_ext_cache cbex;
1850 struct ext4_extent *ex;
1851 ext4_lblk_t next, start = 0, end = 0;
1852 ext4_lblk_t last = block + num;
1853 int depth, exists, err = 0;
1854
1855 BUG_ON(func == NULL);
1856 BUG_ON(inode == NULL);
1857
Lukas Czernerf17722f2011-06-06 00:05:17 -04001858 while (block < last && block != EXT_MAX_BLOCKS) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04001859 num = last - block;
1860 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001861 down_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001862 path = ext4_ext_find_extent(inode, block, path);
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001863 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001864 if (IS_ERR(path)) {
1865 err = PTR_ERR(path);
1866 path = NULL;
1867 break;
1868 }
1869
1870 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05001871 if (unlikely(path[depth].p_hdr == NULL)) {
1872 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1873 err = -EIO;
1874 break;
1875 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04001876 ex = path[depth].p_ext;
1877 next = ext4_ext_next_allocated_block(path);
1878
1879 exists = 0;
1880 if (!ex) {
1881 /* there is no extent yet, so try to allocate
1882 * all requested space */
1883 start = block;
1884 end = block + num;
1885 } else if (le32_to_cpu(ex->ee_block) > block) {
1886 /* need to allocate space before found extent */
1887 start = block;
1888 end = le32_to_cpu(ex->ee_block);
1889 if (block + num < end)
1890 end = block + num;
1891 } else if (block >= le32_to_cpu(ex->ee_block)
1892 + ext4_ext_get_actual_len(ex)) {
1893 /* need to allocate space after found extent */
1894 start = block;
1895 end = block + num;
1896 if (end >= next)
1897 end = next;
1898 } else if (block >= le32_to_cpu(ex->ee_block)) {
1899 /*
1900 * some part of requested space is covered
1901 * by found extent
1902 */
1903 start = block;
1904 end = le32_to_cpu(ex->ee_block)
1905 + ext4_ext_get_actual_len(ex);
1906 if (block + num < end)
1907 end = block + num;
1908 exists = 1;
1909 } else {
1910 BUG();
1911 }
1912 BUG_ON(end <= start);
1913
1914 if (!exists) {
1915 cbex.ec_block = start;
1916 cbex.ec_len = end - start;
1917 cbex.ec_start = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04001918 } else {
1919 cbex.ec_block = le32_to_cpu(ex->ee_block);
1920 cbex.ec_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001921 cbex.ec_start = ext4_ext_pblock(ex);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001922 }
1923
Frank Mayhar273df552010-03-02 11:46:09 -05001924 if (unlikely(cbex.ec_len == 0)) {
1925 EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
1926 err = -EIO;
1927 break;
1928 }
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04001929 err = func(inode, next, &cbex, ex, cbdata);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001930 ext4_ext_drop_refs(path);
1931
1932 if (err < 0)
1933 break;
1934
1935 if (err == EXT_REPEAT)
1936 continue;
1937 else if (err == EXT_BREAK) {
1938 err = 0;
1939 break;
1940 }
1941
1942 if (ext_depth(inode) != depth) {
1943 /* depth was changed. we have to realloc path */
1944 kfree(path);
1945 path = NULL;
1946 }
1947
1948 block = cbex.ec_block + cbex.ec_len;
1949 }
1950
1951 if (path) {
1952 ext4_ext_drop_refs(path);
1953 kfree(path);
1954 }
1955
1956 return err;
1957}
1958
Avantika Mathur09b88252006-12-06 20:41:36 -08001959static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001960ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05001961 __u32 len, ext4_fsblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07001962{
1963 struct ext4_ext_cache *cex;
1964 BUG_ON(len == 0);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001965 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001966 cex = &EXT4_I(inode)->i_cached_extent;
Alex Tomasa86c6182006-10-11 01:21:03 -07001967 cex->ec_block = block;
1968 cex->ec_len = len;
1969 cex->ec_start = start;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001970 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001971}
1972
1973/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001974 * ext4_ext_put_gap_in_cache:
1975 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07001976 * and cache this gap
1977 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001978static void
Alex Tomasa86c6182006-10-11 01:21:03 -07001979ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001980 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07001981{
1982 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001983 unsigned long len;
1984 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001985 struct ext4_extent *ex;
1986
1987 ex = path[depth].p_ext;
1988 if (ex == NULL) {
1989 /* there is no extent yet, so gap is [0;-] */
1990 lblock = 0;
Lukas Czernerf17722f2011-06-06 00:05:17 -04001991 len = EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001992 ext_debug("cache gap(whole file):");
1993 } else if (block < le32_to_cpu(ex->ee_block)) {
1994 lblock = block;
1995 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05001996 ext_debug("cache gap(before): %u [%u:%u]",
1997 block,
1998 le32_to_cpu(ex->ee_block),
1999 ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002000 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002001 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002002 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002003 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002004 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002005
2006 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05002007 ext_debug("cache gap(after): [%u:%u] %u",
2008 le32_to_cpu(ex->ee_block),
2009 ext4_ext_get_actual_len(ex),
2010 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002011 BUG_ON(next == lblock);
2012 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07002013 } else {
2014 lblock = len = 0;
2015 BUG();
2016 }
2017
Eric Sandeenbba90742008-01-28 23:58:27 -05002018 ext_debug(" -> %u:%lu\n", lblock, len);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002019 ext4_ext_put_in_cache(inode, lblock, len, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002020}
2021
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002022/*
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002023 * ext4_ext_in_cache()
2024 * Checks to see if the given block is in the cache.
2025 * If it is, the cached extent is stored in the given
2026 * cache extent pointer. If the cached extent is a hole,
2027 * this routine should be used instead of
2028 * ext4_ext_in_cache if the calling function needs to
2029 * know the size of the hole.
2030 *
2031 * @inode: The files inode
2032 * @block: The block to look for in the cache
2033 * @ex: Pointer where the cached extent will be stored
2034 * if it contains block
2035 *
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002036 * Return 0 if cache is invalid; 1 if the cache is valid
2037 */
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002038static int ext4_ext_check_cache(struct inode *inode, ext4_lblk_t block,
2039 struct ext4_ext_cache *ex){
Alex Tomasa86c6182006-10-11 01:21:03 -07002040 struct ext4_ext_cache *cex;
Vivek Haldar77f41352011-05-22 21:24:16 -04002041 struct ext4_sb_info *sbi;
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002042 int ret = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002043
Theodore Ts'o60e66792010-05-17 07:00:00 -04002044 /*
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002045 * We borrow i_block_reservation_lock to protect i_cached_extent
2046 */
2047 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07002048 cex = &EXT4_I(inode)->i_cached_extent;
Vivek Haldar77f41352011-05-22 21:24:16 -04002049 sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002050
2051 /* has cache valid data? */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002052 if (cex->ec_len == 0)
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002053 goto errout;
Alex Tomasa86c6182006-10-11 01:21:03 -07002054
Akinobu Mita731eb1a2010-03-03 23:55:01 -05002055 if (in_range(block, cex->ec_block, cex->ec_len)) {
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002056 memcpy(ex, cex, sizeof(struct ext4_ext_cache));
Eric Sandeenbba90742008-01-28 23:58:27 -05002057 ext_debug("%u cached by %u:%u:%llu\n",
2058 block,
2059 cex->ec_block, cex->ec_len, cex->ec_start);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002060 ret = 1;
Alex Tomasa86c6182006-10-11 01:21:03 -07002061 }
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002062errout:
Vivek Haldar77f41352011-05-22 21:24:16 -04002063 if (!ret)
2064 sbi->extent_cache_misses++;
2065 else
2066 sbi->extent_cache_hits++;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002067 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
2068 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07002069}
2070
2071/*
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002072 * ext4_ext_in_cache()
2073 * Checks to see if the given block is in the cache.
2074 * If it is, the cached extent is stored in the given
2075 * extent pointer.
2076 *
2077 * @inode: The files inode
2078 * @block: The block to look for in the cache
2079 * @ex: Pointer where the cached extent will be stored
2080 * if it contains block
2081 *
2082 * Return 0 if cache is invalid; 1 if the cache is valid
2083 */
2084static int
2085ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
2086 struct ext4_extent *ex)
2087{
2088 struct ext4_ext_cache cex;
2089 int ret = 0;
2090
2091 if (ext4_ext_check_cache(inode, block, &cex)) {
2092 ex->ee_block = cpu_to_le32(cex.ec_block);
2093 ext4_ext_store_pblock(ex, cex.ec_start);
2094 ex->ee_len = cpu_to_le16(cex.ec_len);
2095 ret = 1;
2096 }
2097
2098 return ret;
2099}
2100
2101
2102/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002103 * ext4_ext_rm_idx:
2104 * removes index from the index block.
2105 * It's used in truncate case only, thus all requests are for
2106 * last index in the block only.
Alex Tomasa86c6182006-10-11 01:21:03 -07002107 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002108static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07002109 struct ext4_ext_path *path)
2110{
Alex Tomasa86c6182006-10-11 01:21:03 -07002111 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002112 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002113
2114 /* free index block */
2115 path--;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002116 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002117 if (unlikely(path->p_hdr->eh_entries == 0)) {
2118 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2119 return -EIO;
2120 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002121 err = ext4_ext_get_access(handle, inode, path);
2122 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002123 return err;
Marcin Slusarze8546d02008-04-17 10:38:59 -04002124 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002125 err = ext4_ext_dirty(handle, inode, path);
2126 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002127 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002128 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Peter Huewe7dc57612011-02-21 21:01:42 -05002129 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002130 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Alex Tomasa86c6182006-10-11 01:21:03 -07002131 return err;
2132}
2133
2134/*
Mingming Caoee12b632008-08-19 22:16:05 -04002135 * ext4_ext_calc_credits_for_single_extent:
2136 * This routine returns max. credits that needed to insert an extent
2137 * to the extent tree.
2138 * When pass the actual path, the caller should calculate credits
2139 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002140 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002141int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002142 struct ext4_ext_path *path)
2143{
Alex Tomasa86c6182006-10-11 01:21:03 -07002144 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002145 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002146 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002147
Alex Tomasa86c6182006-10-11 01:21:03 -07002148 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002149 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002150 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2151
2152 /*
2153 * There are some space in the leaf tree, no
2154 * need to account for leaf block credit
2155 *
2156 * bitmaps and block group descriptor blocks
2157 * and other metadat blocks still need to be
2158 * accounted.
2159 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002160 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002161 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002162 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002163 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002164 }
2165
Mingming Cao525f4ed2008-08-19 22:15:58 -04002166 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002167}
Alex Tomasa86c6182006-10-11 01:21:03 -07002168
Mingming Caoee12b632008-08-19 22:16:05 -04002169/*
2170 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2171 *
2172 * if nrblocks are fit in a single extent (chunk flag is 1), then
2173 * in the worse case, each tree level index/leaf need to be changed
2174 * if the tree split due to insert a new extent, then the old tree
2175 * index/leaf need to be updated too
2176 *
2177 * If the nrblocks are discontiguous, they could cause
2178 * the whole tree split more than once, but this is really rare.
2179 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002180int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoee12b632008-08-19 22:16:05 -04002181{
2182 int index;
2183 int depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002184
Mingming Caoee12b632008-08-19 22:16:05 -04002185 if (chunk)
2186 index = depth * 2;
2187 else
2188 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002189
Mingming Caoee12b632008-08-19 22:16:05 -04002190 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002191}
2192
2193static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2194 struct ext4_extent *ex,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002195 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002196{
Amit Aroraa2df2a62007-07-17 21:42:41 -04002197 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe6362602009-11-23 07:17:05 -05002198 int flags = EXT4_FREE_BLOCKS_FORGET;
Alex Tomasa86c6182006-10-11 01:21:03 -07002199
Alex Tomasc9de5602008-01-29 00:19:52 -05002200 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
Theodore Ts'oe6362602009-11-23 07:17:05 -05002201 flags |= EXT4_FREE_BLOCKS_METADATA;
Alex Tomasa86c6182006-10-11 01:21:03 -07002202#ifdef EXTENTS_STATS
2203 {
2204 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002205 spin_lock(&sbi->s_ext_stats_lock);
2206 sbi->s_ext_blocks += ee_len;
2207 sbi->s_ext_extents++;
2208 if (ee_len < sbi->s_ext_min)
2209 sbi->s_ext_min = ee_len;
2210 if (ee_len > sbi->s_ext_max)
2211 sbi->s_ext_max = ee_len;
2212 if (ext_depth(inode) > sbi->s_depth_max)
2213 sbi->s_depth_max = ext_depth(inode);
2214 spin_unlock(&sbi->s_ext_stats_lock);
2215 }
2216#endif
2217 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002218 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002219 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002220 ext4_lblk_t num;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002221 ext4_fsblk_t start;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002222
Amit Aroraa2df2a62007-07-17 21:42:41 -04002223 num = le32_to_cpu(ex->ee_block) + ee_len - from;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002224 start = ext4_ext_pblock(ex) + ee_len - num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002225 ext_debug("free last %u blocks starting %llu\n", num, start);
Peter Huewe7dc57612011-02-21 21:01:42 -05002226 ext4_free_blocks(handle, inode, NULL, start, num, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07002227 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002228 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002229 /* head removal */
2230 ext4_lblk_t num;
2231 ext4_fsblk_t start;
2232
2233 num = to - from;
2234 start = ext4_ext_pblock(ex);
2235
2236 ext_debug("free first %u blocks starting %llu\n", num, start);
2237 ext4_free_blocks(handle, inode, 0, start, num, flags);
2238
Alex Tomasa86c6182006-10-11 01:21:03 -07002239 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002240 printk(KERN_INFO "strange request: removal(2) "
2241 "%u-%u from %u:%u\n",
2242 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002243 }
2244 return 0;
2245}
2246
Allison Hendersond583fb82011-05-25 07:41:43 -04002247
2248/*
2249 * ext4_ext_rm_leaf() Removes the extents associated with the
2250 * blocks appearing between "start" and "end", and splits the extents
2251 * if "start" and "end" appear in the same extent
2252 *
2253 * @handle: The journal handle
2254 * @inode: The files inode
2255 * @path: The path to the leaf
2256 * @start: The first block to remove
2257 * @end: The last block to remove
2258 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002259static int
2260ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Allison Hendersond583fb82011-05-25 07:41:43 -04002261 struct ext4_ext_path *path, ext4_lblk_t start,
2262 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002263{
2264 int err = 0, correct_index = 0;
2265 int depth = ext_depth(inode), credits;
2266 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002267 ext4_lblk_t a, b, block;
2268 unsigned num;
2269 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002270 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002271 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002272 struct ext4_extent *ex;
Allison Hendersond583fb82011-05-25 07:41:43 -04002273 struct ext4_map_blocks map;
Alex Tomasa86c6182006-10-11 01:21:03 -07002274
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002275 /* the header must be checked already in ext4_ext_remove_space() */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002276 ext_debug("truncate since %u in leaf\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002277 if (!path[depth].p_hdr)
2278 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2279 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002280 if (unlikely(path[depth].p_hdr == NULL)) {
2281 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2282 return -EIO;
2283 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002284 /* find where to start removing */
2285 ex = EXT_LAST_EXTENT(eh);
2286
2287 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002288 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002289
2290 while (ex >= EXT_FIRST_EXTENT(eh) &&
2291 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002292
2293 if (ext4_ext_is_uninitialized(ex))
2294 uninitialized = 1;
2295 else
2296 uninitialized = 0;
2297
Mingming553f9002009-09-18 13:34:55 -04002298 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2299 uninitialized, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002300 path[depth].p_ext = ex;
2301
2302 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002303 b = ex_ee_block+ex_ee_len - 1 < end ?
2304 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002305
2306 ext_debug(" border %u:%u\n", a, b);
2307
Allison Hendersond583fb82011-05-25 07:41:43 -04002308 /* If this extent is beyond the end of the hole, skip it */
2309 if (end <= ex_ee_block) {
2310 ex--;
2311 ex_ee_block = le32_to_cpu(ex->ee_block);
2312 ex_ee_len = ext4_ext_get_actual_len(ex);
2313 continue;
2314 } else if (a != ex_ee_block &&
2315 b != ex_ee_block + ex_ee_len - 1) {
2316 /*
2317 * If this is a truncate, then this condition should
2318 * never happen because at least one of the end points
2319 * needs to be on the edge of the extent.
2320 */
Lukas Czernerf17722f2011-06-06 00:05:17 -04002321 if (end == EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002322 ext_debug(" bad truncate %u:%u\n",
2323 start, end);
2324 block = 0;
2325 num = 0;
2326 err = -EIO;
2327 goto out;
2328 }
2329 /*
2330 * else this is a hole punch, so the extent needs to
2331 * be split since neither edge of the hole is on the
2332 * extent edge
2333 */
2334 else{
2335 map.m_pblk = ext4_ext_pblock(ex);
2336 map.m_lblk = ex_ee_block;
2337 map.m_len = b - ex_ee_block;
2338
2339 err = ext4_split_extent(handle,
2340 inode, path, &map, 0,
2341 EXT4_GET_BLOCKS_PUNCH_OUT_EXT |
2342 EXT4_GET_BLOCKS_PRE_IO);
2343
2344 if (err < 0)
2345 goto out;
2346
2347 ex_ee_len = ext4_ext_get_actual_len(ex);
2348
2349 b = ex_ee_block+ex_ee_len - 1 < end ?
2350 ex_ee_block+ex_ee_len - 1 : end;
2351
2352 /* Then remove tail of this extent */
2353 block = ex_ee_block;
2354 num = a - block;
2355 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002356 } else if (a != ex_ee_block) {
2357 /* remove tail of the extent */
2358 block = ex_ee_block;
2359 num = a - block;
2360 } else if (b != ex_ee_block + ex_ee_len - 1) {
2361 /* remove head of the extent */
Allison Hendersond583fb82011-05-25 07:41:43 -04002362 block = b;
2363 num = ex_ee_block + ex_ee_len - b;
2364
2365 /*
2366 * If this is a truncate, this condition
2367 * should never happen
2368 */
Lukas Czernerf17722f2011-06-06 00:05:17 -04002369 if (end == EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002370 ext_debug(" bad truncate %u:%u\n",
2371 start, end);
2372 err = -EIO;
2373 goto out;
2374 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002375 } else {
2376 /* remove whole extent: excellent! */
2377 block = ex_ee_block;
2378 num = 0;
Allison Hendersond583fb82011-05-25 07:41:43 -04002379 if (a != ex_ee_block) {
2380 ext_debug(" bad truncate %u:%u\n",
2381 start, end);
2382 err = -EIO;
2383 goto out;
2384 }
2385
2386 if (b != ex_ee_block + ex_ee_len - 1) {
2387 ext_debug(" bad truncate %u:%u\n",
2388 start, end);
2389 err = -EIO;
2390 goto out;
2391 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002392 }
2393
Theodore Ts'o34071da2008-08-01 21:59:19 -04002394 /*
2395 * 3 for leaf, sb, and inode plus 2 (bmap and group
2396 * descriptor) for each block group; assume two block
2397 * groups plus ex_ee_len/blocks_per_block_group for
2398 * the worst case
2399 */
2400 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002401 if (ex == EXT_FIRST_EXTENT(eh)) {
2402 correct_index = 1;
2403 credits += (ext_depth(inode)) + 1;
2404 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002405 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002406
Jan Kara487caee2009-08-17 22:17:20 -04002407 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
Shen Feng9102e4f2008-07-11 19:27:31 -04002408 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002409 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002410
2411 err = ext4_ext_get_access(handle, inode, path + depth);
2412 if (err)
2413 goto out;
2414
2415 err = ext4_remove_blocks(handle, inode, ex, a, b);
2416 if (err)
2417 goto out;
2418
2419 if (num == 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002420 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002421 ext4_ext_store_pblock(ex, 0);
Allison Hendersond583fb82011-05-25 07:41:43 -04002422 } else if (block != ex_ee_block) {
2423 /*
2424 * If this was a head removal, then we need to update
2425 * the physical block since it is now at a different
2426 * location
2427 */
2428 ext4_ext_store_pblock(ex, ext4_ext_pblock(ex) + (b-a));
Alex Tomasa86c6182006-10-11 01:21:03 -07002429 }
2430
2431 ex->ee_block = cpu_to_le32(block);
2432 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002433 /*
2434 * Do not mark uninitialized if all the blocks in the
2435 * extent have been removed.
2436 */
2437 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002438 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002439
2440 err = ext4_ext_dirty(handle, inode, path + depth);
2441 if (err)
2442 goto out;
2443
Allison Hendersond583fb82011-05-25 07:41:43 -04002444 /*
2445 * If the extent was completely released,
2446 * we need to remove it from the leaf
2447 */
2448 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002449 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002450 /*
2451 * For hole punching, we need to scoot all the
2452 * extents up when an extent is removed so that
2453 * we dont have blank extents in the middle
2454 */
2455 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2456 sizeof(struct ext4_extent));
2457
2458 /* Now get rid of the one at the end */
2459 memset(EXT_LAST_EXTENT(eh), 0,
2460 sizeof(struct ext4_extent));
2461 }
2462 le16_add_cpu(&eh->eh_entries, -1);
2463 }
2464
Mingming Cao2ae02102006-10-11 01:21:11 -07002465 ext_debug("new extent: %u:%u:%llu\n", block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002466 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002467 ex--;
2468 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002469 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002470 }
2471
2472 if (correct_index && eh->eh_entries)
2473 err = ext4_ext_correct_indexes(handle, inode, path);
2474
2475 /* if this leaf is free, then we should
2476 * remove it from index block above */
2477 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2478 err = ext4_ext_rm_idx(handle, inode, path + depth);
2479
2480out:
2481 return err;
2482}
2483
2484/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002485 * ext4_ext_more_to_rm:
2486 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002487 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002488static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002489ext4_ext_more_to_rm(struct ext4_ext_path *path)
2490{
2491 BUG_ON(path->p_idx == NULL);
2492
2493 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2494 return 0;
2495
2496 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002497 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002498 * so we have to consider current index for truncation
2499 */
2500 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2501 return 0;
2502 return 1;
2503}
2504
Allison Hendersonc6a03712011-07-17 23:21:03 -04002505static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07002506{
2507 struct super_block *sb = inode->i_sb;
2508 int depth = ext_depth(inode);
2509 struct ext4_ext_path *path;
2510 handle_t *handle;
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002511 int i, err;
Alex Tomasa86c6182006-10-11 01:21:03 -07002512
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002513 ext_debug("truncate since %u\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002514
2515 /* probably first extent we're gonna free will be last in block */
2516 handle = ext4_journal_start(inode, depth + 1);
2517 if (IS_ERR(handle))
2518 return PTR_ERR(handle);
2519
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002520again:
Alex Tomasa86c6182006-10-11 01:21:03 -07002521 ext4_ext_invalidate_cache(inode);
2522
2523 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002524 * We start scanning from right side, freeing all the blocks
2525 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002526 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002527 depth = ext_depth(inode);
Josef Bacik216553c2008-04-29 22:02:02 -04002528 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07002529 if (path == NULL) {
2530 ext4_journal_stop(handle);
2531 return -ENOMEM;
2532 }
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002533 path[0].p_depth = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -07002534 path[0].p_hdr = ext_inode_hdr(inode);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002535 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002536 err = -EIO;
2537 goto out;
2538 }
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002539 i = err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002540
2541 while (i >= 0 && err == 0) {
2542 if (i == depth) {
2543 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04002544 err = ext4_ext_rm_leaf(handle, inode, path,
Allison Hendersonc6a03712011-07-17 23:21:03 -04002545 start, EXT_MAX_BLOCKS - 1);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002546 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002547 brelse(path[i].p_bh);
2548 path[i].p_bh = NULL;
2549 i--;
2550 continue;
2551 }
2552
2553 /* this is index block */
2554 if (!path[i].p_hdr) {
2555 ext_debug("initialize header\n");
2556 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002557 }
2558
Alex Tomasa86c6182006-10-11 01:21:03 -07002559 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002560 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002561 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2562 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2563 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2564 path[i].p_hdr,
2565 le16_to_cpu(path[i].p_hdr->eh_entries));
2566 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002567 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002568 path[i].p_idx--;
2569 }
2570
2571 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2572 i, EXT_FIRST_INDEX(path[i].p_hdr),
2573 path[i].p_idx);
2574 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002575 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002576 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002577 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04002578 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002579 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'obf89d162010-10-27 21:30:14 -04002580 bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002581 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002582 /* should we reset i_size? */
2583 err = -EIO;
2584 break;
2585 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002586 if (WARN_ON(i + 1 > depth)) {
2587 err = -EIO;
2588 break;
2589 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002590 if (ext4_ext_check(inode, ext_block_hdr(bh),
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002591 depth - i - 1)) {
2592 err = -EIO;
2593 break;
2594 }
2595 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002596
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002597 /* save actual number of indexes since this
2598 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002599 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2600 i++;
2601 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002602 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002603 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002604 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002605 * handle must be already prepared by the
2606 * truncatei_leaf() */
2607 err = ext4_ext_rm_idx(handle, inode, path + i);
2608 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002609 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002610 brelse(path[i].p_bh);
2611 path[i].p_bh = NULL;
2612 i--;
2613 ext_debug("return to level %d\n", i);
2614 }
2615 }
2616
2617 /* TODO: flexible tree reduction should be here */
2618 if (path->p_hdr->eh_entries == 0) {
2619 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002620 * truncate to zero freed all the tree,
2621 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002622 */
2623 err = ext4_ext_get_access(handle, inode, path);
2624 if (err == 0) {
2625 ext_inode_hdr(inode)->eh_depth = 0;
2626 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04002627 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07002628 err = ext4_ext_dirty(handle, inode, path);
2629 }
2630 }
2631out:
Alex Tomasa86c6182006-10-11 01:21:03 -07002632 ext4_ext_drop_refs(path);
2633 kfree(path);
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002634 if (err == -EAGAIN)
2635 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07002636 ext4_journal_stop(handle);
2637
2638 return err;
2639}
2640
2641/*
2642 * called at mount time
2643 */
2644void ext4_ext_init(struct super_block *sb)
2645{
2646 /*
2647 * possible initialization would be here
2648 */
2649
Theodore Ts'o83982b62009-01-06 14:53:16 -05002650 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04002651#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o47760042008-09-08 23:00:52 -04002652 printk(KERN_INFO "EXT4-fs: file extents enabled");
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002653#ifdef AGGRESSIVE_TEST
2654 printk(", aggressive tests");
Alex Tomasa86c6182006-10-11 01:21:03 -07002655#endif
2656#ifdef CHECK_BINSEARCH
2657 printk(", check binsearch");
2658#endif
2659#ifdef EXTENTS_STATS
2660 printk(", stats");
2661#endif
2662 printk("\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04002663#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07002664#ifdef EXTENTS_STATS
2665 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2666 EXT4_SB(sb)->s_ext_min = 1 << 30;
2667 EXT4_SB(sb)->s_ext_max = 0;
2668#endif
2669 }
2670}
2671
2672/*
2673 * called at umount time
2674 */
2675void ext4_ext_release(struct super_block *sb)
2676{
Theodore Ts'o83982b62009-01-06 14:53:16 -05002677 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
Alex Tomasa86c6182006-10-11 01:21:03 -07002678 return;
2679
2680#ifdef EXTENTS_STATS
2681 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2682 struct ext4_sb_info *sbi = EXT4_SB(sb);
2683 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2684 sbi->s_ext_blocks, sbi->s_ext_extents,
2685 sbi->s_ext_blocks / sbi->s_ext_extents);
2686 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2687 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2688 }
2689#endif
2690}
2691
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002692/* FIXME!! we need to try to merge to left or right after zero-out */
2693static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2694{
Lukas Czerner24075182010-10-27 21:30:06 -04002695 ext4_fsblk_t ee_pblock;
2696 unsigned int ee_len;
Jing Zhangb7203032010-05-12 00:00:00 -04002697 int ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002698
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002699 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04002700 ee_pblock = ext4_ext_pblock(ex);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002701
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04002702 ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
Lukas Czerner24075182010-10-27 21:30:06 -04002703 if (ret > 0)
2704 ret = 0;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002705
Lukas Czerner24075182010-10-27 21:30:06 -04002706 return ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002707}
2708
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002709/*
2710 * used by extent splitting.
2711 */
2712#define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
2713 due to ENOSPC */
2714#define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */
2715#define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */
2716
2717/*
2718 * ext4_split_extent_at() splits an extent at given block.
2719 *
2720 * @handle: the journal handle
2721 * @inode: the file inode
2722 * @path: the path to the extent
2723 * @split: the logical block where the extent is splitted.
2724 * @split_flags: indicates if the extent could be zeroout if split fails, and
2725 * the states(init or uninit) of new extents.
2726 * @flags: flags used to insert new extent to extent tree.
2727 *
2728 *
2729 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
2730 * of which are deterimined by split_flag.
2731 *
2732 * There are two cases:
2733 * a> the extent are splitted into two extent.
2734 * b> split is not needed, and just mark the extent.
2735 *
2736 * return 0 on success.
2737 */
2738static int ext4_split_extent_at(handle_t *handle,
2739 struct inode *inode,
2740 struct ext4_ext_path *path,
2741 ext4_lblk_t split,
2742 int split_flag,
2743 int flags)
2744{
2745 ext4_fsblk_t newblock;
2746 ext4_lblk_t ee_block;
2747 struct ext4_extent *ex, newex, orig_ex;
2748 struct ext4_extent *ex2 = NULL;
2749 unsigned int ee_len, depth;
2750 int err = 0;
2751
2752 ext_debug("ext4_split_extents_at: inode %lu, logical"
2753 "block %llu\n", inode->i_ino, (unsigned long long)split);
2754
2755 ext4_ext_show_leaf(inode, path);
2756
2757 depth = ext_depth(inode);
2758 ex = path[depth].p_ext;
2759 ee_block = le32_to_cpu(ex->ee_block);
2760 ee_len = ext4_ext_get_actual_len(ex);
2761 newblock = split - ee_block + ext4_ext_pblock(ex);
2762
2763 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
2764
2765 err = ext4_ext_get_access(handle, inode, path + depth);
2766 if (err)
2767 goto out;
2768
2769 if (split == ee_block) {
2770 /*
2771 * case b: block @split is the block that the extent begins with
2772 * then we just change the state of the extent, and splitting
2773 * is not needed.
2774 */
2775 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2776 ext4_ext_mark_uninitialized(ex);
2777 else
2778 ext4_ext_mark_initialized(ex);
2779
2780 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
2781 ext4_ext_try_to_merge(inode, path, ex);
2782
2783 err = ext4_ext_dirty(handle, inode, path + depth);
2784 goto out;
2785 }
2786
2787 /* case a */
2788 memcpy(&orig_ex, ex, sizeof(orig_ex));
2789 ex->ee_len = cpu_to_le16(split - ee_block);
2790 if (split_flag & EXT4_EXT_MARK_UNINIT1)
2791 ext4_ext_mark_uninitialized(ex);
2792
2793 /*
2794 * path may lead to new leaf, not to original leaf any more
2795 * after ext4_ext_insert_extent() returns,
2796 */
2797 err = ext4_ext_dirty(handle, inode, path + depth);
2798 if (err)
2799 goto fix_extent_len;
2800
2801 ex2 = &newex;
2802 ex2->ee_block = cpu_to_le32(split);
2803 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
2804 ext4_ext_store_pblock(ex2, newblock);
2805 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2806 ext4_ext_mark_uninitialized(ex2);
2807
2808 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
2809 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2810 err = ext4_ext_zeroout(inode, &orig_ex);
2811 if (err)
2812 goto fix_extent_len;
2813 /* update the extent length and mark as initialized */
2814 ex->ee_len = cpu_to_le32(ee_len);
2815 ext4_ext_try_to_merge(inode, path, ex);
2816 err = ext4_ext_dirty(handle, inode, path + depth);
2817 goto out;
2818 } else if (err)
2819 goto fix_extent_len;
2820
2821out:
2822 ext4_ext_show_leaf(inode, path);
2823 return err;
2824
2825fix_extent_len:
2826 ex->ee_len = orig_ex.ee_len;
2827 ext4_ext_dirty(handle, inode, path + depth);
2828 return err;
2829}
2830
2831/*
2832 * ext4_split_extents() splits an extent and mark extent which is covered
2833 * by @map as split_flags indicates
2834 *
2835 * It may result in splitting the extent into multiple extents (upto three)
2836 * There are three possibilities:
2837 * a> There is no split required
2838 * b> Splits in two extents: Split is happening at either end of the extent
2839 * c> Splits in three extents: Somone is splitting in middle of the extent
2840 *
2841 */
2842static int ext4_split_extent(handle_t *handle,
2843 struct inode *inode,
2844 struct ext4_ext_path *path,
2845 struct ext4_map_blocks *map,
2846 int split_flag,
2847 int flags)
2848{
2849 ext4_lblk_t ee_block;
2850 struct ext4_extent *ex;
2851 unsigned int ee_len, depth;
2852 int err = 0;
2853 int uninitialized;
2854 int split_flag1, flags1;
2855
2856 depth = ext_depth(inode);
2857 ex = path[depth].p_ext;
2858 ee_block = le32_to_cpu(ex->ee_block);
2859 ee_len = ext4_ext_get_actual_len(ex);
2860 uninitialized = ext4_ext_is_uninitialized(ex);
2861
2862 if (map->m_lblk + map->m_len < ee_block + ee_len) {
2863 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2864 EXT4_EXT_MAY_ZEROOUT : 0;
2865 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
2866 if (uninitialized)
2867 split_flag1 |= EXT4_EXT_MARK_UNINIT1 |
2868 EXT4_EXT_MARK_UNINIT2;
2869 err = ext4_split_extent_at(handle, inode, path,
2870 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04002871 if (err)
2872 goto out;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002873 }
2874
2875 ext4_ext_drop_refs(path);
2876 path = ext4_ext_find_extent(inode, map->m_lblk, path);
2877 if (IS_ERR(path))
2878 return PTR_ERR(path);
2879
2880 if (map->m_lblk >= ee_block) {
2881 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2882 EXT4_EXT_MAY_ZEROOUT : 0;
2883 if (uninitialized)
2884 split_flag1 |= EXT4_EXT_MARK_UNINIT1;
2885 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2886 split_flag1 |= EXT4_EXT_MARK_UNINIT2;
2887 err = ext4_split_extent_at(handle, inode, path,
2888 map->m_lblk, split_flag1, flags);
2889 if (err)
2890 goto out;
2891 }
2892
2893 ext4_ext_show_leaf(inode, path);
2894out:
2895 return err ? err : map->m_len;
2896}
2897
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002898#define EXT4_EXT_ZERO_LEN 7
Amit Arora56055d32007-07-17 21:42:38 -04002899/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002900 * This function is called by ext4_ext_map_blocks() if someone tries to write
Amit Arora56055d32007-07-17 21:42:38 -04002901 * to an uninitialized extent. It may result in splitting the uninitialized
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002902 * extent into multiple extents (up to three - one initialized and two
Amit Arora56055d32007-07-17 21:42:38 -04002903 * uninitialized).
2904 * There are three possibilities:
2905 * a> There is no split required: Entire extent should be initialized
2906 * b> Splits in two extents: Write is happening at either end of the extent
2907 * c> Splits in three extents: Somone is writing in middle of the extent
2908 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002909static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002910 struct inode *inode,
2911 struct ext4_map_blocks *map,
2912 struct ext4_ext_path *path)
Amit Arora56055d32007-07-17 21:42:38 -04002913{
Yongqiang Yang667eff32011-05-03 12:25:07 -04002914 struct ext4_map_blocks split_map;
2915 struct ext4_extent zero_ex;
2916 struct ext4_extent *ex;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002917 ext4_lblk_t ee_block, eof_block;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002918 unsigned int allocated, ee_len, depth;
Amit Arora56055d32007-07-17 21:42:38 -04002919 int err = 0;
Yongqiang Yang667eff32011-05-03 12:25:07 -04002920 int split_flag = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002921
2922 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
2923 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002924 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002925
2926 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
2927 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002928 if (eof_block < map->m_lblk + map->m_len)
2929 eof_block = map->m_lblk + map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04002930
2931 depth = ext_depth(inode);
Amit Arora56055d32007-07-17 21:42:38 -04002932 ex = path[depth].p_ext;
2933 ee_block = le32_to_cpu(ex->ee_block);
2934 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002935 allocated = ee_len - (map->m_lblk - ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002936
Yongqiang Yang667eff32011-05-03 12:25:07 -04002937 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002938 /*
2939 * It is safe to convert extent to initialized via explicit
2940 * zeroout only if extent is fully insde i_size or new_size.
2941 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04002942 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002943
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002944 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
Yongqiang Yang667eff32011-05-03 12:25:07 -04002945 if (ee_len <= 2*EXT4_EXT_ZERO_LEN &&
2946 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2947 err = ext4_ext_zeroout(inode, ex);
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002948 if (err)
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002949 goto out;
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002950
2951 err = ext4_ext_get_access(handle, inode, path + depth);
2952 if (err)
2953 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04002954 ext4_ext_mark_initialized(ex);
2955 ext4_ext_try_to_merge(inode, path, ex);
2956 err = ext4_ext_dirty(handle, inode, path + depth);
2957 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04002958 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04002959
Amit Arora56055d32007-07-17 21:42:38 -04002960 /*
Yongqiang Yang667eff32011-05-03 12:25:07 -04002961 * four cases:
2962 * 1. split the extent into three extents.
2963 * 2. split the extent into two extents, zeroout the first half.
2964 * 3. split the extent into two extents, zeroout the second half.
2965 * 4. split the extent into two extents with out zeroout.
Amit Arora56055d32007-07-17 21:42:38 -04002966 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04002967 split_map.m_lblk = map->m_lblk;
2968 split_map.m_len = map->m_len;
2969
2970 if (allocated > map->m_len) {
2971 if (allocated <= EXT4_EXT_ZERO_LEN &&
2972 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2973 /* case 3 */
2974 zero_ex.ee_block =
Allison Henderson9b940f82011-05-16 10:11:09 -04002975 cpu_to_le32(map->m_lblk);
2976 zero_ex.ee_len = cpu_to_le16(allocated);
Yongqiang Yang667eff32011-05-03 12:25:07 -04002977 ext4_ext_store_pblock(&zero_ex,
2978 ext4_ext_pblock(ex) + map->m_lblk - ee_block);
2979 err = ext4_ext_zeroout(inode, &zero_ex);
Amit Arora56055d32007-07-17 21:42:38 -04002980 if (err)
2981 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04002982 split_map.m_lblk = map->m_lblk;
2983 split_map.m_len = allocated;
2984 } else if ((map->m_lblk - ee_block + map->m_len <
2985 EXT4_EXT_ZERO_LEN) &&
2986 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2987 /* case 2 */
2988 if (map->m_lblk != ee_block) {
2989 zero_ex.ee_block = ex->ee_block;
2990 zero_ex.ee_len = cpu_to_le16(map->m_lblk -
2991 ee_block);
2992 ext4_ext_store_pblock(&zero_ex,
2993 ext4_ext_pblock(ex));
2994 err = ext4_ext_zeroout(inode, &zero_ex);
2995 if (err)
2996 goto out;
2997 }
2998
Yongqiang Yang667eff32011-05-03 12:25:07 -04002999 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003000 split_map.m_len = map->m_lblk - ee_block + map->m_len;
3001 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003002 }
3003 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003004
3005 allocated = ext4_split_extent(handle, inode, path,
3006 &split_map, split_flag, 0);
3007 if (allocated < 0)
3008 err = allocated;
3009
Amit Arora56055d32007-07-17 21:42:38 -04003010out:
3011 return err ? err : allocated;
3012}
3013
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003014/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003015 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003016 * ext4_get_blocks_dio_write() when DIO to write
3017 * to an uninitialized extent.
3018 *
Paul Bollefd018fe2011-02-15 00:05:43 +01003019 * Writing to an uninitialized extent may result in splitting the uninitialized
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003020 * extent into multiple /initialized uninitialized extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003021 * There are three possibilities:
3022 * a> There is no split required: Entire extent should be uninitialized
3023 * b> Splits in two extents: Write is happening at either end of the extent
3024 * c> Splits in three extents: Somone is writing in middle of the extent
3025 *
3026 * One of more index blocks maybe needed if the extent tree grow after
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003027 * the uninitialized extent split. To prevent ENOSPC occur at the IO
Mingming Cao00314622009-09-28 15:49:08 -04003028 * complete, we need to split the uninitialized extent before DIO submit
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02003029 * the IO. The uninitialized extent called at this time will be split
Mingming Cao00314622009-09-28 15:49:08 -04003030 * into three uninitialized extent(at most). After IO complete, the part
3031 * being filled will be convert to initialized by the end_io callback function
3032 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003033 *
3034 * Returns the size of uninitialized extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003035 */
3036static int ext4_split_unwritten_extents(handle_t *handle,
3037 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003038 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003039 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04003040 int flags)
3041{
Yongqiang Yang667eff32011-05-03 12:25:07 -04003042 ext4_lblk_t eof_block;
3043 ext4_lblk_t ee_block;
3044 struct ext4_extent *ex;
3045 unsigned int ee_len;
3046 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003047
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003048 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
3049 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003050 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003051
3052 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3053 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003054 if (eof_block < map->m_lblk + map->m_len)
3055 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003056 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003057 * It is safe to convert extent to initialized via explicit
3058 * zeroout only if extent is fully insde i_size or new_size.
3059 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003060 depth = ext_depth(inode);
3061 ex = path[depth].p_ext;
3062 ee_block = le32_to_cpu(ex->ee_block);
3063 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003064
Yongqiang Yang667eff32011-05-03 12:25:07 -04003065 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
3066 split_flag |= EXT4_EXT_MARK_UNINIT2;
Mingming Cao00314622009-09-28 15:49:08 -04003067
Yongqiang Yang667eff32011-05-03 12:25:07 -04003068 flags |= EXT4_GET_BLOCKS_PRE_IO;
3069 return ext4_split_extent(handle, inode, path, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003070}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003071
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003072static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003073 struct inode *inode,
3074 struct ext4_ext_path *path)
3075{
3076 struct ext4_extent *ex;
Mingming Cao00314622009-09-28 15:49:08 -04003077 int depth;
3078 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003079
3080 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003081 ex = path[depth].p_ext;
3082
Yongqiang Yang197217a2011-05-03 11:45:29 -04003083 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3084 "block %llu, max_blocks %u\n", inode->i_ino,
3085 (unsigned long long)le32_to_cpu(ex->ee_block),
3086 ext4_ext_get_actual_len(ex));
3087
Mingming Cao00314622009-09-28 15:49:08 -04003088 err = ext4_ext_get_access(handle, inode, path + depth);
3089 if (err)
3090 goto out;
3091 /* first mark the extent as initialized */
3092 ext4_ext_mark_initialized(ex);
3093
Yongqiang Yang197217a2011-05-03 11:45:29 -04003094 /* note: ext4_ext_correct_indexes() isn't needed here because
3095 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003096 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04003097 ext4_ext_try_to_merge(inode, path, ex);
3098
Mingming Cao00314622009-09-28 15:49:08 -04003099 /* Mark modified extent as dirty */
3100 err = ext4_ext_dirty(handle, inode, path + depth);
3101out:
3102 ext4_ext_show_leaf(inode, path);
3103 return err;
3104}
3105
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003106static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3107 sector_t block, int count)
3108{
3109 int i;
3110 for (i = 0; i < count; i++)
3111 unmap_underlying_metadata(bdev, block + i);
3112}
3113
Theodore Ts'o58590b02010-10-27 21:23:12 -04003114/*
3115 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3116 */
3117static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
Eric Sandeend002ebf2011-01-10 13:03:35 -05003118 ext4_lblk_t lblk,
Theodore Ts'o58590b02010-10-27 21:23:12 -04003119 struct ext4_ext_path *path,
3120 unsigned int len)
3121{
3122 int i, depth;
3123 struct ext4_extent_header *eh;
Sergey Senozhatsky65922cb2011-03-23 14:08:27 -04003124 struct ext4_extent *last_ex;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003125
3126 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3127 return 0;
3128
3129 depth = ext_depth(inode);
3130 eh = path[depth].p_hdr;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003131
3132 if (unlikely(!eh->eh_entries)) {
3133 EXT4_ERROR_INODE(inode, "eh->eh_entries == 0 and "
3134 "EOFBLOCKS_FL set");
3135 return -EIO;
3136 }
3137 last_ex = EXT_LAST_EXTENT(eh);
3138 /*
3139 * We should clear the EOFBLOCKS_FL flag if we are writing the
3140 * last block in the last extent in the file. We test this by
3141 * first checking to see if the caller to
3142 * ext4_ext_get_blocks() was interested in the last block (or
3143 * a block beyond the last block) in the current extent. If
3144 * this turns out to be false, we can bail out from this
3145 * function immediately.
3146 */
Eric Sandeend002ebf2011-01-10 13:03:35 -05003147 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
Theodore Ts'o58590b02010-10-27 21:23:12 -04003148 ext4_ext_get_actual_len(last_ex))
3149 return 0;
3150 /*
3151 * If the caller does appear to be planning to write at or
3152 * beyond the end of the current extent, we then test to see
3153 * if the current extent is the last extent in the file, by
3154 * checking to make sure it was reached via the rightmost node
3155 * at each level of the tree.
3156 */
3157 for (i = depth-1; i >= 0; i--)
3158 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3159 return 0;
3160 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3161 return ext4_mark_inode_dirty(handle, inode);
3162}
3163
Mingming Cao00314622009-09-28 15:49:08 -04003164static int
3165ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003166 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003167 struct ext4_ext_path *path, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003168 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003169{
3170 int ret = 0;
3171 int err = 0;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003172 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Mingming Cao00314622009-09-28 15:49:08 -04003173
3174 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3175 "block %llu, max_blocks %u, flags %d, allocated %u",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003176 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04003177 flags, allocated);
3178 ext4_ext_show_leaf(inode, path);
3179
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003180 /* get_block() before submit the IO, split the extent */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003181 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003182 ret = ext4_split_unwritten_extents(handle, inode, map,
3183 path, flags);
Mingming5f5249502009-11-10 10:48:04 -05003184 /*
3185 * Flag the inode(non aio case) or end_io struct (aio case)
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003186 * that this IO needs to conversion to written when IO is
Mingming5f5249502009-11-10 10:48:04 -05003187 * completed
3188 */
Eric Sandeene9e3bce2011-02-12 08:17:34 -05003189 if (io && !(io->flag & EXT4_IO_END_UNWRITTEN)) {
Theodore Ts'obd2d0212010-10-27 21:30:10 -04003190 io->flag = EXT4_IO_END_UNWRITTEN;
Eric Sandeene9e3bce2011-02-12 08:17:34 -05003191 atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten);
3192 } else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003193 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
Jiaying Zhang744692d2010-03-04 16:14:02 -05003194 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003195 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao00314622009-09-28 15:49:08 -04003196 goto out;
3197 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003198 /* IO end_io complete, convert the filled extent to written */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003199 if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003200 ret = ext4_convert_unwritten_extents_endio(handle, inode,
Mingming Cao00314622009-09-28 15:49:08 -04003201 path);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003202 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05003203 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05003204 err = check_eofblocks_fl(handle, inode, map->m_lblk,
3205 path, map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003206 } else
3207 err = ret;
Mingming Cao00314622009-09-28 15:49:08 -04003208 goto out2;
3209 }
3210 /* buffered IO case */
3211 /*
3212 * repeat fallocate creation request
3213 * we already have an unwritten extent
3214 */
3215 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3216 goto map_out;
3217
3218 /* buffered READ or buffered write_begin() lookup */
3219 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3220 /*
3221 * We have blocks reserved already. We
3222 * return allocated blocks so that delalloc
3223 * won't do block reservation for us. But
3224 * the buffer head will be unmapped so that
3225 * a read from the block returns 0s.
3226 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003227 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003228 goto out1;
3229 }
3230
3231 /* buffered write, writepage time, convert*/
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003232 ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003233 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05003234 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05003235 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
3236 map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003237 if (err < 0)
3238 goto out2;
3239 }
3240
Mingming Cao00314622009-09-28 15:49:08 -04003241out:
3242 if (ret <= 0) {
3243 err = ret;
3244 goto out2;
3245 } else
3246 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003247 map->m_flags |= EXT4_MAP_NEW;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003248 /*
3249 * if we allocated more blocks than requested
3250 * we need to make sure we unmap the extra block
3251 * allocated. The actual needed block will get
3252 * unmapped later when we find the buffer_head marked
3253 * new.
3254 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003255 if (allocated > map->m_len) {
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003256 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003257 newblock + map->m_len,
3258 allocated - map->m_len);
3259 allocated = map->m_len;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003260 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003261
3262 /*
3263 * If we have done fallocate with the offset that is already
3264 * delayed allocated, we would have block reservation
3265 * and quota reservation done in the delayed write path.
3266 * But fallocate would have already updated quota and block
3267 * count for this offset. So cancel these reservation
3268 */
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05003269 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003270 ext4_da_update_reserve_space(inode, allocated, 0);
3271
Mingming Cao00314622009-09-28 15:49:08 -04003272map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003273 map->m_flags |= EXT4_MAP_MAPPED;
Mingming Cao00314622009-09-28 15:49:08 -04003274out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003275 if (allocated > map->m_len)
3276 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003277 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003278 map->m_pblk = newblock;
3279 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04003280out2:
3281 if (path) {
3282 ext4_ext_drop_refs(path);
3283 kfree(path);
3284 }
3285 return err ? err : allocated;
3286}
Theodore Ts'o58590b02010-10-27 21:23:12 -04003287
Mingming Cao00314622009-09-28 15:49:08 -04003288/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05003289 * Block allocation/map/preallocation routine for extents based files
3290 *
3291 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003292 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003293 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3294 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05003295 *
3296 * return > 0, number of of blocks already mapped/allocated
3297 * if create == 0 and these are pre-allocated blocks
3298 * buffer head is unmapped
3299 * otherwise blocks are mapped
3300 *
3301 * return = 0, if plain look up failed (blocks have not been allocated)
3302 * buffer head is unmapped
3303 *
3304 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003305 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003306int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3307 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07003308{
3309 struct ext4_ext_path *path = NULL;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003310 struct ext4_extent newex, *ex;
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003311 ext4_fsblk_t newblock = 0;
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003312 int err = 0, depth, ret;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003313 unsigned int allocated = 0;
Allison Hendersone8613042011-05-25 07:41:46 -04003314 unsigned int punched_out = 0;
3315 unsigned int result = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003316 struct ext4_allocation_request ar;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003317 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Allison Hendersone8613042011-05-25 07:41:46 -04003318 struct ext4_map_blocks punch_map;
Alex Tomasa86c6182006-10-11 01:21:03 -07003319
Mingming84fe3be2009-09-01 08:44:37 -04003320 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003321 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003322 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07003323
3324 /* check in cache */
Robin Dong015861b2011-07-17 23:27:43 -04003325 if (!(flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) &&
3326 ext4_ext_in_cache(inode, map->m_lblk, &newex)) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003327 if (!newex.ee_start_lo && !newex.ee_start_hi) {
Theodore Ts'oc2177052009-05-14 00:58:52 -04003328 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003329 /*
3330 * block isn't allocated yet and
3331 * user doesn't want to allocate it
3332 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003333 goto out2;
3334 }
3335 /* we should allocate requested block */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003336 } else {
Alex Tomasa86c6182006-10-11 01:21:03 -07003337 /* block is already allocated */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003338 newblock = map->m_lblk
Dave Kleikamp8c55e202007-05-24 13:04:54 -04003339 - le32_to_cpu(newex.ee_block)
Theodore Ts'obf89d162010-10-27 21:30:14 -04003340 + ext4_ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003341 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003342 allocated = ext4_ext_get_actual_len(&newex) -
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003343 (map->m_lblk - le32_to_cpu(newex.ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -07003344 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07003345 }
3346 }
3347
3348 /* find extent for this block */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003349 path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07003350 if (IS_ERR(path)) {
3351 err = PTR_ERR(path);
3352 path = NULL;
3353 goto out2;
3354 }
3355
3356 depth = ext_depth(inode);
3357
3358 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003359 * consistent leaf must not be empty;
3360 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07003361 * this is why assert can't be put in ext4_ext_find_extent()
3362 */
Frank Mayhar273df552010-03-02 11:46:09 -05003363 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
3364 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04003365 "lblock: %lu, depth: %d pblock %lld",
3366 (unsigned long) map->m_lblk, depth,
3367 path[depth].p_block);
Surbhi Palande034fb4c2009-12-14 09:53:52 -05003368 err = -EIO;
3369 goto out2;
3370 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003371
Avantika Mathur7e028972006-12-06 20:41:33 -08003372 ex = path[depth].p_ext;
3373 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003374 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003375 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003376 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003377
3378 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003379 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04003380 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003381 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003382 ee_len = ext4_ext_get_actual_len(ex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003383 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003384 if (in_range(map->m_lblk, ee_block, ee_len)) {
3385 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003386 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003387 allocated = ee_len - (map->m_lblk - ee_block);
3388 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
3389 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04003390
Allison Hendersone8613042011-05-25 07:41:46 -04003391 if ((flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) == 0) {
3392 /*
3393 * Do not put uninitialized extent
3394 * in the cache
3395 */
3396 if (!ext4_ext_is_uninitialized(ex)) {
3397 ext4_ext_put_in_cache(inode, ee_block,
3398 ee_len, ee_start);
3399 goto out;
3400 }
3401 ret = ext4_ext_handle_uninitialized_extents(
3402 handle, inode, map, path, flags,
3403 allocated, newblock);
3404 return ret;
Amit Arora56055d32007-07-17 21:42:38 -04003405 }
Allison Hendersone8613042011-05-25 07:41:46 -04003406
3407 /*
3408 * Punch out the map length, but only to the
3409 * end of the extent
3410 */
3411 punched_out = allocated < map->m_len ?
3412 allocated : map->m_len;
3413
3414 /*
3415 * Sense extents need to be converted to
3416 * uninitialized, they must fit in an
3417 * uninitialized extent
3418 */
3419 if (punched_out > EXT_UNINIT_MAX_LEN)
3420 punched_out = EXT_UNINIT_MAX_LEN;
3421
3422 punch_map.m_lblk = map->m_lblk;
3423 punch_map.m_pblk = newblock;
3424 punch_map.m_len = punched_out;
3425 punch_map.m_flags = 0;
3426
3427 /* Check to see if the extent needs to be split */
3428 if (punch_map.m_len != ee_len ||
3429 punch_map.m_lblk != ee_block) {
3430
3431 ret = ext4_split_extent(handle, inode,
3432 path, &punch_map, 0,
3433 EXT4_GET_BLOCKS_PUNCH_OUT_EXT |
3434 EXT4_GET_BLOCKS_PRE_IO);
3435
3436 if (ret < 0) {
3437 err = ret;
3438 goto out2;
3439 }
3440 /*
3441 * find extent for the block at
3442 * the start of the hole
3443 */
3444 ext4_ext_drop_refs(path);
3445 kfree(path);
3446
3447 path = ext4_ext_find_extent(inode,
3448 map->m_lblk, NULL);
3449 if (IS_ERR(path)) {
3450 err = PTR_ERR(path);
3451 path = NULL;
3452 goto out2;
3453 }
3454
3455 depth = ext_depth(inode);
3456 ex = path[depth].p_ext;
3457 ee_len = ext4_ext_get_actual_len(ex);
3458 ee_block = le32_to_cpu(ex->ee_block);
3459 ee_start = ext4_ext_pblock(ex);
3460
3461 }
3462
3463 ext4_ext_mark_uninitialized(ex);
3464
Allison Hendersonf7d0d372011-07-17 23:17:02 -04003465 ext4_ext_invalidate_cache(inode);
3466
3467 err = ext4_ext_rm_leaf(handle, inode, path,
3468 map->m_lblk, map->m_lblk + punched_out);
3469
3470 if (!err && path->p_hdr->eh_entries == 0) {
3471 /*
3472 * Punch hole freed all of this sub tree,
3473 * so we need to correct eh_depth
3474 */
3475 err = ext4_ext_get_access(handle, inode, path);
3476 if (err == 0) {
3477 ext_inode_hdr(inode)->eh_depth = 0;
3478 ext_inode_hdr(inode)->eh_max =
3479 cpu_to_le16(ext4_ext_space_root(
3480 inode, 0));
3481
3482 err = ext4_ext_dirty(
3483 handle, inode, path);
3484 }
3485 }
Allison Hendersone8613042011-05-25 07:41:46 -04003486
3487 goto out2;
Alex Tomasa86c6182006-10-11 01:21:03 -07003488 }
3489 }
3490
3491 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003492 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07003493 * we couldn't try to create block if create flag is zero
3494 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04003495 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003496 /*
3497 * put just found gap into cache to speed up
3498 * subsequent requests
3499 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003500 ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
Alex Tomasa86c6182006-10-11 01:21:03 -07003501 goto out2;
3502 }
3503 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003504 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07003505 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003506
Alex Tomasc9de5602008-01-29 00:19:52 -05003507 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003508 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003509 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3510 if (err)
3511 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003512 ar.lright = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003513 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
3514 if (err)
3515 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04003516
Amit Arora749269f2007-07-18 09:02:56 -04003517 /*
3518 * See if request is beyond maximum number of blocks we can have in
3519 * a single extent. For an initialized extent this limit is
3520 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3521 * EXT_UNINIT_MAX_LEN.
3522 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003523 if (map->m_len > EXT_INIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003524 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003525 map->m_len = EXT_INIT_MAX_LEN;
3526 else if (map->m_len > EXT_UNINIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003527 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003528 map->m_len = EXT_UNINIT_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04003529
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003530 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
3531 newex.ee_block = cpu_to_le32(map->m_lblk);
3532 newex.ee_len = cpu_to_le16(map->m_len);
Amit Arora25d14f92007-05-24 13:04:13 -04003533 err = ext4_ext_check_overlap(inode, &newex, path);
3534 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003535 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04003536 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003537 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003538
3539 /* allocate new block */
3540 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003541 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
3542 ar.logical = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003543 ar.len = allocated;
3544 if (S_ISREG(inode->i_mode))
3545 ar.flags = EXT4_MB_HINT_DATA;
3546 else
3547 /* disable in-core preallocation for non-regular files */
3548 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04003549 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
3550 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05003551 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07003552 if (!newblock)
3553 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04003554 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003555 ar.goal, newblock, allocated);
Alex Tomasa86c6182006-10-11 01:21:03 -07003556
3557 /* try to insert new extent into found leaf and return */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07003558 ext4_ext_store_pblock(&newex, newblock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003559 newex.ee_len = cpu_to_le16(ar.len);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003560 /* Mark uninitialized */
3561 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
Amit Aroraa2df2a62007-07-17 21:42:41 -04003562 ext4_ext_mark_uninitialized(&newex);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003563 /*
Jiaying Zhang744692d2010-03-04 16:14:02 -05003564 * io_end structure was created for every IO write to an
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003565 * uninitialized extent. To avoid unnecessary conversion,
Jiaying Zhang744692d2010-03-04 16:14:02 -05003566 * here we flag the IO that really needs the conversion.
Mingming5f5249502009-11-10 10:48:04 -05003567 * For non asycn direct IO case, flag the inode state
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003568 * that we need to perform conversion when IO is done.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003569 */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003570 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Eric Sandeene9e3bce2011-02-12 08:17:34 -05003571 if (io && !(io->flag & EXT4_IO_END_UNWRITTEN)) {
Theodore Ts'obd2d0212010-10-27 21:30:10 -04003572 io->flag = EXT4_IO_END_UNWRITTEN;
Eric Sandeene9e3bce2011-02-12 08:17:34 -05003573 atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten);
3574 } else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003575 ext4_set_inode_state(inode,
3576 EXT4_STATE_DIO_UNWRITTEN);
Mingming5f5249502009-11-10 10:48:04 -05003577 }
Jiaying Zhang744692d2010-03-04 16:14:02 -05003578 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003579 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003580 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003581
Eric Sandeend002ebf2011-01-10 13:03:35 -05003582 err = check_eofblocks_fl(handle, inode, map->m_lblk, path, ar.len);
Jiaying Zhang575a1d42011-07-10 20:07:25 -04003583 if (!err)
3584 err = ext4_ext_insert_extent(handle, inode, path,
3585 &newex, flags);
Alex Tomas315054f2007-05-24 13:04:25 -04003586 if (err) {
Maxim Patlasov7132de72011-07-10 19:37:48 -04003587 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
3588 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
Alex Tomas315054f2007-05-24 13:04:25 -04003589 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05003590 /* not a good idea to call discard here directly,
3591 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003592 ext4_discard_preallocations(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05003593 ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex),
Maxim Patlasov7132de72011-07-10 19:37:48 -04003594 ext4_ext_get_actual_len(&newex), fb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07003595 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04003596 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003597
Alex Tomasa86c6182006-10-11 01:21:03 -07003598 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04003599 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003600 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003601 if (allocated > map->m_len)
3602 allocated = map->m_len;
3603 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07003604
Jan Karab436b9b2009-12-08 23:51:10 -05003605 /*
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003606 * Update reserved blocks/metadata blocks after successful
3607 * block allocation which had been deferred till now.
3608 */
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05003609 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003610 ext4_da_update_reserve_space(inode, allocated, 1);
3611
3612 /*
Jan Karab436b9b2009-12-08 23:51:10 -05003613 * Cache the extent and update transaction to commit on fdatasync only
3614 * when it is _not_ an uninitialized extent.
3615 */
3616 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003617 ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock);
Jan Karab436b9b2009-12-08 23:51:10 -05003618 ext4_update_inode_fsync_trans(handle, inode, 1);
3619 } else
3620 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07003621out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003622 if (allocated > map->m_len)
3623 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07003624 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003625 map->m_flags |= EXT4_MAP_MAPPED;
3626 map->m_pblk = newblock;
3627 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07003628out2:
3629 if (path) {
3630 ext4_ext_drop_refs(path);
3631 kfree(path);
3632 }
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003633 trace_ext4_ext_map_blocks_exit(inode, map->m_lblk,
3634 newblock, map->m_len, err ? err : allocated);
Allison Hendersone8613042011-05-25 07:41:46 -04003635
3636 result = (flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) ?
3637 punched_out : allocated;
3638
3639 return err ? err : result;
Alex Tomasa86c6182006-10-11 01:21:03 -07003640}
3641
Jan Karacf108bc2008-07-11 19:27:31 -04003642void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07003643{
3644 struct address_space *mapping = inode->i_mapping;
3645 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003646 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07003647 handle_t *handle;
3648 int err = 0;
3649
3650 /*
Jiaying Zhang3889fd52011-01-10 12:47:05 -05003651 * finish any pending end_io work so we won't run the risk of
3652 * converting any truncated blocks to initialized later
3653 */
3654 ext4_flush_completed_IO(inode);
3655
3656 /*
Alex Tomasa86c6182006-10-11 01:21:03 -07003657 * probably first extent we're gonna free will be last in block
3658 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04003659 err = ext4_writepage_trans_blocks(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07003660 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04003661 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07003662 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07003663
Jan Karacf108bc2008-07-11 19:27:31 -04003664 if (inode->i_size & (sb->s_blocksize - 1))
3665 ext4_block_truncate_page(handle, mapping, inode->i_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07003666
Jan Kara9ddfc3d2008-07-11 19:27:31 -04003667 if (ext4_orphan_add(handle, inode))
3668 goto out_stop;
3669
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003670 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07003671 ext4_ext_invalidate_cache(inode);
3672
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003673 ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003674
Alex Tomasa86c6182006-10-11 01:21:03 -07003675 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003676 * TODO: optimization is possible here.
3677 * Probably we need not scan at all,
3678 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07003679 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003680
3681 /* we have to know where to truncate from in crash case */
3682 EXT4_I(inode)->i_disksize = inode->i_size;
3683 ext4_mark_inode_dirty(handle, inode);
3684
3685 last_block = (inode->i_size + sb->s_blocksize - 1)
3686 >> EXT4_BLOCK_SIZE_BITS(sb);
Allison Hendersonc6a03712011-07-17 23:21:03 -04003687 err = ext4_ext_remove_space(inode, last_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07003688
3689 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04003690 * transaction synchronous.
3691 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003692 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05003693 ext4_handle_sync(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07003694
Jan Kara9ddfc3d2008-07-11 19:27:31 -04003695 up_write(&EXT4_I(inode)->i_data_sem);
Eric Gouriouf6d2f6b2011-05-22 21:33:00 -04003696
3697out_stop:
Alex Tomasa86c6182006-10-11 01:21:03 -07003698 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003699 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07003700 * then we need to clear up the orphan record which we created above.
3701 * However, if this was a real unlink then we were called by
3702 * ext4_delete_inode(), and we allow that function to clean up the
3703 * orphan info for us.
3704 */
3705 if (inode->i_nlink)
3706 ext4_orphan_del(handle, inode);
3707
Solofo Ramangalahyef737722008-04-29 22:00:41 -04003708 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3709 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07003710 ext4_journal_stop(handle);
3711}
3712
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003713static void ext4_falloc_update_inode(struct inode *inode,
3714 int mode, loff_t new_size, int update_ctime)
3715{
3716 struct timespec now;
3717
3718 if (update_ctime) {
3719 now = current_fs_time(inode->i_sb);
3720 if (!timespec_equal(&inode->i_ctime, &now))
3721 inode->i_ctime = now;
3722 }
3723 /*
3724 * Update only when preallocation was requested beyond
3725 * the file size.
3726 */
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003727 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3728 if (new_size > i_size_read(inode))
3729 i_size_write(inode, new_size);
3730 if (new_size > EXT4_I(inode)->i_disksize)
3731 ext4_update_i_disksize(inode, new_size);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003732 } else {
3733 /*
3734 * Mark that we allocate beyond EOF so the subsequent truncate
3735 * can proceed even if the new size is the same as i_size.
3736 */
3737 if (new_size > i_size_read(inode))
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003738 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003739 }
3740
3741}
3742
Amit Aroraa2df2a62007-07-17 21:42:41 -04003743/*
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003744 * preallocate space for a file. This implements ext4's fallocate file
Amit Aroraa2df2a62007-07-17 21:42:41 -04003745 * operation, which gets called from sys_fallocate system call.
3746 * For block-mapped files, posix_fallocate should fall back to the method
3747 * of writing zeroes to the required new blocks (the same behavior which is
3748 * expected for file systems which do not support fallocate() system call).
3749 */
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003750long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
Amit Aroraa2df2a62007-07-17 21:42:41 -04003751{
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003752 struct inode *inode = file->f_path.dentry->d_inode;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003753 handle_t *handle;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003754 loff_t new_size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003755 unsigned int max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003756 int ret = 0;
3757 int ret2 = 0;
3758 int retries = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003759 struct ext4_map_blocks map;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003760 unsigned int credits, blkbits = inode->i_blkbits;
3761
3762 /*
3763 * currently supporting (pre)allocate mode for extent-based
3764 * files _only_
3765 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003766 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Amit Aroraa2df2a62007-07-17 21:42:41 -04003767 return -EOPNOTSUPP;
3768
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003769 /* Return error if mode is not supported */
3770 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
3771 return -EOPNOTSUPP;
3772
3773 if (mode & FALLOC_FL_PUNCH_HOLE)
3774 return ext4_punch_hole(file, offset, len);
3775
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003776 trace_ext4_fallocate_enter(inode, offset, len, mode);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003777 map.m_lblk = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003778 /*
3779 * We can't just convert len to max_blocks because
3780 * If blocksize = 4096 offset = 3072 and len = 2048
3781 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003782 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003783 - map.m_lblk;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003784 /*
Mingming Caof3bd1f32008-08-19 22:16:03 -04003785 * credits to insert 1 extent into extent tree
Amit Aroraa2df2a62007-07-17 21:42:41 -04003786 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04003787 credits = ext4_chunk_trans_blocks(inode, max_blocks);
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05003788 mutex_lock(&inode->i_mutex);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04003789 ret = inode_newsize_ok(inode, (len + offset));
3790 if (ret) {
3791 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003792 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04003793 return ret;
3794 }
Amit Aroraa2df2a62007-07-17 21:42:41 -04003795retry:
3796 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003797 map.m_lblk = map.m_lblk + ret;
3798 map.m_len = max_blocks = max_blocks - ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003799 handle = ext4_journal_start(inode, credits);
3800 if (IS_ERR(handle)) {
3801 ret = PTR_ERR(handle);
3802 break;
3803 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003804 ret = ext4_map_blocks(handle, inode, &map,
Vivek Haldar556b27a2011-05-25 07:41:54 -04003805 EXT4_GET_BLOCKS_CREATE_UNINIT_EXT |
3806 EXT4_GET_BLOCKS_NO_NORMALIZE);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05003807 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003808#ifdef EXT4FS_DEBUG
3809 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003810 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003811 "returned error inode#%lu, block=%u, "
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05003812 "max_blocks=%u", __func__,
Kazuya Mioa6371b62010-10-27 21:30:15 -04003813 inode->i_ino, map.m_lblk, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003814#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04003815 ext4_mark_inode_dirty(handle, inode);
3816 ret2 = ext4_journal_stop(handle);
3817 break;
3818 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003819 if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003820 blkbits) >> blkbits))
3821 new_size = offset + len;
3822 else
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003823 new_size = (map.m_lblk + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003824
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003825 ext4_falloc_update_inode(inode, mode, new_size,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003826 (map.m_flags & EXT4_MAP_NEW));
Amit Aroraa2df2a62007-07-17 21:42:41 -04003827 ext4_mark_inode_dirty(handle, inode);
3828 ret2 = ext4_journal_stop(handle);
3829 if (ret2)
3830 break;
3831 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003832 if (ret == -ENOSPC &&
3833 ext4_should_retry_alloc(inode->i_sb, &retries)) {
3834 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003835 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003836 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05003837 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003838 trace_ext4_fallocate_exit(inode, offset, max_blocks,
3839 ret > 0 ? ret2 : ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003840 return ret > 0 ? ret2 : ret;
3841}
Eric Sandeen6873fa02008-10-07 00:46:36 -04003842
3843/*
Mingming Cao00314622009-09-28 15:49:08 -04003844 * This function convert a range of blocks to written extents
3845 * The caller of this function will pass the start offset and the size.
3846 * all unwritten extents within this range will be converted to
3847 * written extents.
3848 *
3849 * This function is called from the direct IO end io call back
3850 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05003851 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04003852 */
3853int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
Eric Sandeena1de02d2010-02-04 23:58:38 -05003854 ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04003855{
3856 handle_t *handle;
Mingming Cao00314622009-09-28 15:49:08 -04003857 unsigned int max_blocks;
3858 int ret = 0;
3859 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003860 struct ext4_map_blocks map;
Mingming Cao00314622009-09-28 15:49:08 -04003861 unsigned int credits, blkbits = inode->i_blkbits;
3862
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003863 map.m_lblk = offset >> blkbits;
Mingming Cao00314622009-09-28 15:49:08 -04003864 /*
3865 * We can't just convert len to max_blocks because
3866 * If blocksize = 4096 offset = 3072 and len = 2048
3867 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003868 max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
3869 map.m_lblk);
Mingming Cao00314622009-09-28 15:49:08 -04003870 /*
3871 * credits to insert 1 extent into extent tree
3872 */
3873 credits = ext4_chunk_trans_blocks(inode, max_blocks);
3874 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003875 map.m_lblk += ret;
3876 map.m_len = (max_blocks -= ret);
Mingming Cao00314622009-09-28 15:49:08 -04003877 handle = ext4_journal_start(inode, credits);
3878 if (IS_ERR(handle)) {
3879 ret = PTR_ERR(handle);
3880 break;
3881 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003882 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003883 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Mingming Cao00314622009-09-28 15:49:08 -04003884 if (ret <= 0) {
3885 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003886 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Mingming Cao00314622009-09-28 15:49:08 -04003887 "returned error inode#%lu, block=%u, "
3888 "max_blocks=%u", __func__,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003889 inode->i_ino, map.m_lblk, map.m_len);
Mingming Cao00314622009-09-28 15:49:08 -04003890 }
3891 ext4_mark_inode_dirty(handle, inode);
3892 ret2 = ext4_journal_stop(handle);
3893 if (ret <= 0 || ret2 )
3894 break;
3895 }
3896 return ret > 0 ? ret2 : ret;
3897}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003898
Mingming Cao00314622009-09-28 15:49:08 -04003899/*
Eric Sandeen6873fa02008-10-07 00:46:36 -04003900 * Callback function called for each extent to gather FIEMAP information.
3901 */
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04003902static int ext4_ext_fiemap_cb(struct inode *inode, ext4_lblk_t next,
Eric Sandeen6873fa02008-10-07 00:46:36 -04003903 struct ext4_ext_cache *newex, struct ext4_extent *ex,
3904 void *data)
3905{
Eric Sandeen6873fa02008-10-07 00:46:36 -04003906 __u64 logical;
3907 __u64 physical;
3908 __u64 length;
3909 __u32 flags = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003910 int ret = 0;
3911 struct fiemap_extent_info *fieinfo = data;
3912 unsigned char blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003913
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003914 blksize_bits = inode->i_sb->s_blocksize_bits;
3915 logical = (__u64)newex->ec_block << blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003916
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003917 if (newex->ec_start == 0) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003918 /*
3919 * No extent in extent-tree contains block @newex->ec_start,
3920 * then the block may stay in 1)a hole or 2)delayed-extent.
3921 *
3922 * Holes or delayed-extents are processed as follows.
3923 * 1. lookup dirty pages with specified range in pagecache.
3924 * If no page is got, then there is no delayed-extent and
3925 * return with EXT_CONTINUE.
3926 * 2. find the 1st mapped buffer,
3927 * 3. check if the mapped buffer is both in the request range
3928 * and a delayed buffer. If not, there is no delayed-extent,
3929 * then return.
3930 * 4. a delayed-extent is found, the extent will be collected.
3931 */
3932 ext4_lblk_t end = 0;
3933 pgoff_t last_offset;
3934 pgoff_t offset;
3935 pgoff_t index;
Yongqiang Yangb2213492011-05-24 11:36:58 -04003936 pgoff_t start_index = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003937 struct page **pages = NULL;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003938 struct buffer_head *bh = NULL;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003939 struct buffer_head *head = NULL;
3940 unsigned int nr_pages = PAGE_SIZE / sizeof(struct page *);
3941
3942 pages = kmalloc(PAGE_SIZE, GFP_KERNEL);
3943 if (pages == NULL)
3944 return -ENOMEM;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003945
3946 offset = logical >> PAGE_SHIFT;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003947repeat:
3948 last_offset = offset;
3949 head = NULL;
3950 ret = find_get_pages_tag(inode->i_mapping, &offset,
3951 PAGECACHE_TAG_DIRTY, nr_pages, pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04003952
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003953 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
3954 /* First time, try to find a mapped buffer. */
3955 if (ret == 0) {
3956out:
3957 for (index = 0; index < ret; index++)
3958 page_cache_release(pages[index]);
3959 /* just a hole. */
3960 kfree(pages);
3961 return EXT_CONTINUE;
3962 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04003963 index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003964
Yongqiang Yangb2213492011-05-24 11:36:58 -04003965next_page:
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003966 /* Try to find the 1st mapped buffer. */
Yongqiang Yangb2213492011-05-24 11:36:58 -04003967 end = ((__u64)pages[index]->index << PAGE_SHIFT) >>
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003968 blksize_bits;
Yongqiang Yangb2213492011-05-24 11:36:58 -04003969 if (!page_has_buffers(pages[index]))
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003970 goto out;
Yongqiang Yangb2213492011-05-24 11:36:58 -04003971 head = page_buffers(pages[index]);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003972 if (!head)
3973 goto out;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003974
Yongqiang Yangb2213492011-05-24 11:36:58 -04003975 index++;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003976 bh = head;
3977 do {
Yongqiang Yangb2213492011-05-24 11:36:58 -04003978 if (end >= newex->ec_block +
3979 newex->ec_len)
3980 /* The buffer is out of
3981 * the request range.
3982 */
3983 goto out;
3984
3985 if (buffer_mapped(bh) &&
3986 end >= newex->ec_block) {
3987 start_index = index - 1;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003988 /* get the 1st mapped buffer. */
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003989 goto found_mapped_buffer;
3990 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04003991
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003992 bh = bh->b_this_page;
3993 end++;
3994 } while (bh != head);
3995
Yongqiang Yangb2213492011-05-24 11:36:58 -04003996 /* No mapped buffer in the range found in this page,
3997 * We need to look up next page.
3998 */
3999 if (index >= ret) {
4000 /* There is no page left, but we need to limit
4001 * newex->ec_len.
4002 */
4003 newex->ec_len = end - newex->ec_block;
4004 goto out;
4005 }
4006 goto next_page;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004007 } else {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004008 /*Find contiguous delayed buffers. */
4009 if (ret > 0 && pages[0]->index == last_offset)
4010 head = page_buffers(pages[0]);
4011 bh = head;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004012 index = 1;
4013 start_index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004014 }
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004015
4016found_mapped_buffer:
4017 if (bh != NULL && buffer_delay(bh)) {
4018 /* 1st or contiguous delayed buffer found. */
4019 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4020 /*
4021 * 1st delayed buffer found, record
4022 * the start of extent.
4023 */
4024 flags |= FIEMAP_EXTENT_DELALLOC;
4025 newex->ec_block = end;
4026 logical = (__u64)end << blksize_bits;
4027 }
4028 /* Find contiguous delayed buffers. */
4029 do {
4030 if (!buffer_delay(bh))
4031 goto found_delayed_extent;
4032 bh = bh->b_this_page;
4033 end++;
4034 } while (bh != head);
4035
Yongqiang Yangb2213492011-05-24 11:36:58 -04004036 for (; index < ret; index++) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004037 if (!page_has_buffers(pages[index])) {
4038 bh = NULL;
4039 break;
4040 }
4041 head = page_buffers(pages[index]);
4042 if (!head) {
4043 bh = NULL;
4044 break;
4045 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004046
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004047 if (pages[index]->index !=
Yongqiang Yangb2213492011-05-24 11:36:58 -04004048 pages[start_index]->index + index
4049 - start_index) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004050 /* Blocks are not contiguous. */
4051 bh = NULL;
4052 break;
4053 }
4054 bh = head;
4055 do {
4056 if (!buffer_delay(bh))
4057 /* Delayed-extent ends. */
4058 goto found_delayed_extent;
4059 bh = bh->b_this_page;
4060 end++;
4061 } while (bh != head);
4062 }
4063 } else if (!(flags & FIEMAP_EXTENT_DELALLOC))
4064 /* a hole found. */
4065 goto out;
4066
4067found_delayed_extent:
4068 newex->ec_len = min(end - newex->ec_block,
4069 (ext4_lblk_t)EXT_INIT_MAX_LEN);
4070 if (ret == nr_pages && bh != NULL &&
4071 newex->ec_len < EXT_INIT_MAX_LEN &&
4072 buffer_delay(bh)) {
4073 /* Have not collected an extent and continue. */
4074 for (index = 0; index < ret; index++)
4075 page_cache_release(pages[index]);
4076 goto repeat;
4077 }
4078
4079 for (index = 0; index < ret; index++)
4080 page_cache_release(pages[index]);
4081 kfree(pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004082 }
4083
4084 physical = (__u64)newex->ec_start << blksize_bits;
4085 length = (__u64)newex->ec_len << blksize_bits;
4086
4087 if (ex && ext4_ext_is_uninitialized(ex))
4088 flags |= FIEMAP_EXTENT_UNWRITTEN;
4089
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04004090 if (next == EXT_MAX_BLOCKS)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004091 flags |= FIEMAP_EXTENT_LAST;
4092
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004093 ret = fiemap_fill_next_extent(fieinfo, logical, physical,
Eric Sandeen6873fa02008-10-07 00:46:36 -04004094 length, flags);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004095 if (ret < 0)
4096 return ret;
4097 if (ret == 1)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004098 return EXT_BREAK;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004099 return EXT_CONTINUE;
4100}
4101
4102/* fiemap flags we can handle specified here */
4103#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
4104
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004105static int ext4_xattr_fiemap(struct inode *inode,
4106 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004107{
4108 __u64 physical = 0;
4109 __u64 length;
4110 __u32 flags = FIEMAP_EXTENT_LAST;
4111 int blockbits = inode->i_sb->s_blocksize_bits;
4112 int error = 0;
4113
4114 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004115 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04004116 struct ext4_iloc iloc;
4117 int offset; /* offset of xattr in inode */
4118
4119 error = ext4_get_inode_loc(inode, &iloc);
4120 if (error)
4121 return error;
4122 physical = iloc.bh->b_blocknr << blockbits;
4123 offset = EXT4_GOOD_OLD_INODE_SIZE +
4124 EXT4_I(inode)->i_extra_isize;
4125 physical += offset;
4126 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
4127 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004128 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004129 } else { /* external block */
4130 physical = EXT4_I(inode)->i_file_acl << blockbits;
4131 length = inode->i_sb->s_blocksize;
4132 }
4133
4134 if (physical)
4135 error = fiemap_fill_next_extent(fieinfo, 0, physical,
4136 length, flags);
4137 return (error < 0 ? error : 0);
4138}
4139
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004140/*
4141 * ext4_ext_punch_hole
4142 *
4143 * Punches a hole of "length" bytes in a file starting
4144 * at byte "offset"
4145 *
4146 * @inode: The inode of the file to punch a hole in
4147 * @offset: The starting byte offset of the hole
4148 * @length: The length of the hole
4149 *
4150 * Returns the number of blocks removed or negative on err
4151 */
4152int ext4_ext_punch_hole(struct file *file, loff_t offset, loff_t length)
4153{
4154 struct inode *inode = file->f_path.dentry->d_inode;
4155 struct super_block *sb = inode->i_sb;
4156 struct ext4_ext_cache cache_ex;
4157 ext4_lblk_t first_block, last_block, num_blocks, iblock, max_blocks;
4158 struct address_space *mapping = inode->i_mapping;
4159 struct ext4_map_blocks map;
4160 handle_t *handle;
4161 loff_t first_block_offset, last_block_offset, block_len;
4162 loff_t first_page, last_page, first_page_offset, last_page_offset;
4163 int ret, credits, blocks_released, err = 0;
4164
4165 first_block = (offset + sb->s_blocksize - 1) >>
4166 EXT4_BLOCK_SIZE_BITS(sb);
4167 last_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
4168
4169 first_block_offset = first_block << EXT4_BLOCK_SIZE_BITS(sb);
4170 last_block_offset = last_block << EXT4_BLOCK_SIZE_BITS(sb);
4171
4172 first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
4173 last_page = (offset + length) >> PAGE_CACHE_SHIFT;
4174
4175 first_page_offset = first_page << PAGE_CACHE_SHIFT;
4176 last_page_offset = last_page << PAGE_CACHE_SHIFT;
4177
4178 /*
4179 * Write out all dirty pages to avoid race conditions
4180 * Then release them.
4181 */
4182 if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
4183 err = filemap_write_and_wait_range(mapping,
4184 first_page_offset == 0 ? 0 : first_page_offset-1,
4185 last_page_offset);
4186
4187 if (err)
4188 return err;
4189 }
4190
4191 /* Now release the pages */
4192 if (last_page_offset > first_page_offset) {
4193 truncate_inode_pages_range(mapping, first_page_offset,
4194 last_page_offset-1);
4195 }
4196
4197 /* finish any pending end_io work */
4198 ext4_flush_completed_IO(inode);
4199
4200 credits = ext4_writepage_trans_blocks(inode);
4201 handle = ext4_journal_start(inode, credits);
4202 if (IS_ERR(handle))
4203 return PTR_ERR(handle);
4204
4205 err = ext4_orphan_add(handle, inode);
4206 if (err)
4207 goto out;
4208
4209 /*
4210 * Now we need to zero out the un block aligned data.
4211 * If the file is smaller than a block, just
4212 * zero out the middle
4213 */
4214 if (first_block > last_block)
4215 ext4_block_zero_page_range(handle, mapping, offset, length);
4216 else {
4217 /* zero out the head of the hole before the first block */
4218 block_len = first_block_offset - offset;
4219 if (block_len > 0)
4220 ext4_block_zero_page_range(handle, mapping,
4221 offset, block_len);
4222
4223 /* zero out the tail of the hole after the last block */
4224 block_len = offset + length - last_block_offset;
4225 if (block_len > 0) {
4226 ext4_block_zero_page_range(handle, mapping,
4227 last_block_offset, block_len);
4228 }
4229 }
4230
4231 /* If there are no blocks to remove, return now */
4232 if (first_block >= last_block)
4233 goto out;
4234
4235 down_write(&EXT4_I(inode)->i_data_sem);
4236 ext4_ext_invalidate_cache(inode);
4237 ext4_discard_preallocations(inode);
4238
4239 /*
4240 * Loop over all the blocks and identify blocks
4241 * that need to be punched out
4242 */
4243 iblock = first_block;
4244 blocks_released = 0;
4245 while (iblock < last_block) {
4246 max_blocks = last_block - iblock;
4247 num_blocks = 1;
4248 memset(&map, 0, sizeof(map));
4249 map.m_lblk = iblock;
4250 map.m_len = max_blocks;
4251 ret = ext4_ext_map_blocks(handle, inode, &map,
4252 EXT4_GET_BLOCKS_PUNCH_OUT_EXT);
4253
4254 if (ret > 0) {
4255 blocks_released += ret;
4256 num_blocks = ret;
4257 } else if (ret == 0) {
4258 /*
4259 * If map blocks could not find the block,
4260 * then it is in a hole. If the hole was
4261 * not already cached, then map blocks should
4262 * put it in the cache. So we can get the hole
4263 * out of the cache
4264 */
4265 memset(&cache_ex, 0, sizeof(cache_ex));
4266 if ((ext4_ext_check_cache(inode, iblock, &cache_ex)) &&
4267 !cache_ex.ec_start) {
4268
4269 /* The hole is cached */
4270 num_blocks = cache_ex.ec_block +
4271 cache_ex.ec_len - iblock;
4272
4273 } else {
4274 /* The block could not be identified */
4275 err = -EIO;
4276 break;
4277 }
4278 } else {
4279 /* Map blocks error */
4280 err = ret;
4281 break;
4282 }
4283
4284 if (num_blocks == 0) {
4285 /* This condition should never happen */
4286 ext_debug("Block lookup failed");
4287 err = -EIO;
4288 break;
4289 }
4290
4291 iblock += num_blocks;
4292 }
4293
4294 if (blocks_released > 0) {
4295 ext4_ext_invalidate_cache(inode);
4296 ext4_discard_preallocations(inode);
4297 }
4298
4299 if (IS_SYNC(inode))
4300 ext4_handle_sync(handle);
4301
4302 up_write(&EXT4_I(inode)->i_data_sem);
4303
4304out:
4305 ext4_orphan_del(handle, inode);
4306 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4307 ext4_mark_inode_dirty(handle, inode);
4308 ext4_journal_stop(handle);
4309 return err;
4310}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004311int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4312 __u64 start, __u64 len)
4313{
4314 ext4_lblk_t start_blk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004315 int error = 0;
4316
4317 /* fallback to generic here if not in extents fmt */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004318 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeen6873fa02008-10-07 00:46:36 -04004319 return generic_block_fiemap(inode, fieinfo, start, len,
4320 ext4_get_block);
4321
4322 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
4323 return -EBADR;
4324
4325 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
4326 error = ext4_xattr_fiemap(inode, fieinfo);
4327 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004328 ext4_lblk_t len_blks;
4329 __u64 last_blk;
4330
Eric Sandeen6873fa02008-10-07 00:46:36 -04004331 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004332 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04004333 if (last_blk >= EXT_MAX_BLOCKS)
4334 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004335 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004336
4337 /*
4338 * Walk the extent tree gathering extent information.
4339 * ext4_ext_fiemap_cb will push extents back to user.
4340 */
Eric Sandeen6873fa02008-10-07 00:46:36 -04004341 error = ext4_ext_walk_space(inode, start_blk, len_blks,
4342 ext4_ext_fiemap_cb, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004343 }
4344
4345 return error;
4346}