blob: 2c5216a8d03be84c808243c55dea8c04b86b64dc [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 */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -040099#define ext4_ext_dirty(handle, inode, path) \
100 __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path))
101static int __ext4_ext_dirty(const char *where, unsigned int line,
102 handle_t *handle, struct inode *inode,
103 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700104{
105 int err;
106 if (path->p_bh) {
107 /* path points to block */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -0400108 err = __ext4_handle_dirty_metadata(where, line, handle,
109 inode, path->p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700110 } else {
111 /* path points to leaf/index in inode body */
112 err = ext4_mark_inode_dirty(handle, inode);
113 }
114 return err;
115}
116
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700117static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700118 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500119 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700120{
Alex Tomasa86c6182006-10-11 01:21:03 -0700121 int depth;
122
123 if (path) {
124 struct ext4_extent *ex;
125 depth = path->p_depth;
126
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500127 /*
128 * Try to predict block placement assuming that we are
129 * filling in a file which will eventually be
130 * non-sparse --- i.e., in the case of libbfd writing
131 * an ELF object sections out-of-order but in a way
132 * the eventually results in a contiguous object or
133 * executable file, or some database extending a table
134 * space file. However, this is actually somewhat
135 * non-ideal if we are writing a sparse file such as
136 * qemu or KVM writing a raw image file that is going
137 * to stay fairly sparse, since it will end up
138 * fragmenting the file system's free space. Maybe we
139 * should have some hueristics or some way to allow
140 * userspace to pass a hint to file system,
Tao Mab8d65682011-01-21 23:21:31 +0800141 * especially if the latter case turns out to be
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500142 * common.
143 */
Avantika Mathur7e028972006-12-06 20:41:33 -0800144 ex = path[depth].p_ext;
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500145 if (ex) {
146 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
147 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
148
149 if (block > ext_block)
150 return ext_pblk + (block - ext_block);
151 else
152 return ext_pblk - (ext_block - block);
153 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700154
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700155 /* it looks like index is empty;
156 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700157 if (path[depth].p_bh)
158 return path[depth].p_bh->b_blocknr;
159 }
160
161 /* OK. use inode's group */
Eric Sandeenf86186b2011-06-28 10:01:31 -0400162 return ext4_inode_to_goal_block(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700163}
164
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400165/*
166 * Allocation for a meta data block
167 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700168static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400169ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700170 struct ext4_ext_path *path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400171 struct ext4_extent *ex, int *err, unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700172{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700173 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700174
175 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Allison Henderson55f020d2011-05-25 07:41:26 -0400176 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
177 NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700178 return newblock;
179}
180
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400181static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700182{
183 int size;
184
185 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
186 / sizeof(struct ext4_extent);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400187 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100188#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400189 if (size > 6)
190 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700191#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400192 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700193 return size;
194}
195
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400196static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700197{
198 int size;
199
200 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
201 / sizeof(struct ext4_extent_idx);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400202 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100203#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400204 if (size > 5)
205 size = 5;
Alex Tomasa86c6182006-10-11 01:21:03 -0700206#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400207 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700208 return size;
209}
210
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400211static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700212{
213 int size;
214
215 size = sizeof(EXT4_I(inode)->i_data);
216 size -= sizeof(struct ext4_extent_header);
217 size /= sizeof(struct ext4_extent);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400218 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100219#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400220 if (size > 3)
221 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700222#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400223 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700224 return size;
225}
226
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400227static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700228{
229 int size;
230
231 size = sizeof(EXT4_I(inode)->i_data);
232 size -= sizeof(struct ext4_extent_header);
233 size /= sizeof(struct ext4_extent_idx);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400234 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100235#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400236 if (size > 4)
237 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700238#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400239 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700240 return size;
241}
242
Mingming Caod2a17632008-07-14 17:52:37 -0400243/*
244 * Calculate the number of metadata blocks needed
245 * to allocate @blocks
246 * Worse case is one block per extent
247 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -0500248int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -0400249{
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500250 struct ext4_inode_info *ei = EXT4_I(inode);
251 int idxs, num = 0;
Mingming Caod2a17632008-07-14 17:52:37 -0400252
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500253 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
254 / sizeof(struct ext4_extent_idx));
Mingming Caod2a17632008-07-14 17:52:37 -0400255
256 /*
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500257 * If the new delayed allocation block is contiguous with the
258 * previous da block, it can share index blocks with the
259 * previous block, so we only need to allocate a new index
260 * block every idxs leaf blocks. At ldxs**2 blocks, we need
261 * an additional index block, and at ldxs**3 blocks, yet
262 * another index blocks.
Mingming Caod2a17632008-07-14 17:52:37 -0400263 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500264 if (ei->i_da_metadata_calc_len &&
265 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
266 if ((ei->i_da_metadata_calc_len % idxs) == 0)
267 num++;
268 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
269 num++;
270 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
271 num++;
272 ei->i_da_metadata_calc_len = 0;
273 } else
274 ei->i_da_metadata_calc_len++;
275 ei->i_da_metadata_calc_last_lblock++;
276 return num;
277 }
Mingming Caod2a17632008-07-14 17:52:37 -0400278
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500279 /*
280 * In the worst case we need a new set of index blocks at
281 * every level of the inode's extent tree.
282 */
283 ei->i_da_metadata_calc_len = 1;
284 ei->i_da_metadata_calc_last_lblock = lblock;
285 return ext_depth(inode) + 1;
Mingming Caod2a17632008-07-14 17:52:37 -0400286}
287
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400288static int
289ext4_ext_max_entries(struct inode *inode, int depth)
290{
291 int max;
292
293 if (depth == ext_depth(inode)) {
294 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400295 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400296 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400297 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400298 } else {
299 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400300 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400301 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400302 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400303 }
304
305 return max;
306}
307
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400308static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
309{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400310 ext4_fsblk_t block = ext4_ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400311 int len = ext4_ext_get_actual_len(ext);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400312
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400313 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400314}
315
316static int ext4_valid_extent_idx(struct inode *inode,
317 struct ext4_extent_idx *ext_idx)
318{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400319 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400320
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400321 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400322}
323
324static int ext4_valid_extent_entries(struct inode *inode,
325 struct ext4_extent_header *eh,
326 int depth)
327{
328 struct ext4_extent *ext;
329 struct ext4_extent_idx *ext_idx;
330 unsigned short entries;
331 if (eh->eh_entries == 0)
332 return 1;
333
334 entries = le16_to_cpu(eh->eh_entries);
335
336 if (depth == 0) {
337 /* leaf entries */
338 ext = EXT_FIRST_EXTENT(eh);
339 while (entries) {
340 if (!ext4_valid_extent(inode, ext))
341 return 0;
342 ext++;
343 entries--;
344 }
345 } else {
346 ext_idx = EXT_FIRST_INDEX(eh);
347 while (entries) {
348 if (!ext4_valid_extent_idx(inode, ext_idx))
349 return 0;
350 ext_idx++;
351 entries--;
352 }
353 }
354 return 1;
355}
356
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400357static int __ext4_ext_check(const char *function, unsigned int line,
358 struct inode *inode, struct ext4_extent_header *eh,
359 int depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400360{
361 const char *error_msg;
362 int max = 0;
363
364 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
365 error_msg = "invalid magic";
366 goto corrupted;
367 }
368 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
369 error_msg = "unexpected eh_depth";
370 goto corrupted;
371 }
372 if (unlikely(eh->eh_max == 0)) {
373 error_msg = "invalid eh_max";
374 goto corrupted;
375 }
376 max = ext4_ext_max_entries(inode, depth);
377 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
378 error_msg = "too large eh_max";
379 goto corrupted;
380 }
381 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
382 error_msg = "invalid eh_entries";
383 goto corrupted;
384 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400385 if (!ext4_valid_extent_entries(inode, eh, depth)) {
386 error_msg = "invalid extent entries";
387 goto corrupted;
388 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400389 return 0;
390
391corrupted:
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400392 ext4_error_inode(inode, function, line, 0,
Theodore Ts'o24676da2010-05-16 21:00:00 -0400393 "bad header/extent: %s - magic %x, "
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400394 "entries %u, max %u(%u), depth %u(%u)",
Theodore Ts'o24676da2010-05-16 21:00:00 -0400395 error_msg, le16_to_cpu(eh->eh_magic),
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400396 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
397 max, le16_to_cpu(eh->eh_depth), depth);
398
399 return -EIO;
400}
401
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400402#define ext4_ext_check(inode, eh, depth) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400403 __ext4_ext_check(__func__, __LINE__, inode, eh, depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400404
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400405int ext4_ext_check_inode(struct inode *inode)
406{
407 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
408}
409
Alex Tomasa86c6182006-10-11 01:21:03 -0700410#ifdef EXT_DEBUG
411static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
412{
413 int k, l = path->p_depth;
414
415 ext_debug("path:");
416 for (k = 0; k <= l; k++, path++) {
417 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700418 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400419 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700420 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400421 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700422 le32_to_cpu(path->p_ext->ee_block),
Mingming553f9002009-09-18 13:34:55 -0400423 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400424 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400425 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700426 } else
427 ext_debug(" []");
428 }
429 ext_debug("\n");
430}
431
432static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
433{
434 int depth = ext_depth(inode);
435 struct ext4_extent_header *eh;
436 struct ext4_extent *ex;
437 int i;
438
439 if (!path)
440 return;
441
442 eh = path[depth].p_hdr;
443 ex = EXT_FIRST_EXTENT(eh);
444
Mingming553f9002009-09-18 13:34:55 -0400445 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
446
Alex Tomasa86c6182006-10-11 01:21:03 -0700447 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400448 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
449 ext4_ext_is_uninitialized(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400450 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700451 }
452 ext_debug("\n");
453}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400454
455static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
456 ext4_fsblk_t newblock, int level)
457{
458 int depth = ext_depth(inode);
459 struct ext4_extent *ex;
460
461 if (depth != level) {
462 struct ext4_extent_idx *idx;
463 idx = path[level].p_idx;
464 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
465 ext_debug("%d: move %d:%llu in new index %llu\n", level,
466 le32_to_cpu(idx->ei_block),
467 ext4_idx_pblock(idx),
468 newblock);
469 idx++;
470 }
471
472 return;
473 }
474
475 ex = path[depth].p_ext;
476 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
477 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
478 le32_to_cpu(ex->ee_block),
479 ext4_ext_pblock(ex),
480 ext4_ext_is_uninitialized(ex),
481 ext4_ext_get_actual_len(ex),
482 newblock);
483 ex++;
484 }
485}
486
Alex Tomasa86c6182006-10-11 01:21:03 -0700487#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400488#define ext4_ext_show_path(inode, path)
489#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400490#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700491#endif
492
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500493void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700494{
495 int depth = path->p_depth;
496 int i;
497
498 for (i = 0; i <= depth; i++, path++)
499 if (path->p_bh) {
500 brelse(path->p_bh);
501 path->p_bh = NULL;
502 }
503}
504
505/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700506 * ext4_ext_binsearch_idx:
507 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400508 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700509 */
510static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500511ext4_ext_binsearch_idx(struct inode *inode,
512 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700513{
514 struct ext4_extent_header *eh = path->p_hdr;
515 struct ext4_extent_idx *r, *l, *m;
516
Alex Tomasa86c6182006-10-11 01:21:03 -0700517
Eric Sandeenbba90742008-01-28 23:58:27 -0500518 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700519
520 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400521 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700522 while (l <= r) {
523 m = l + (r - l) / 2;
524 if (block < le32_to_cpu(m->ei_block))
525 r = m - 1;
526 else
527 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400528 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
529 m, le32_to_cpu(m->ei_block),
530 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700531 }
532
533 path->p_idx = l - 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700534 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400535 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700536
537#ifdef CHECK_BINSEARCH
538 {
539 struct ext4_extent_idx *chix, *ix;
540 int k;
541
542 chix = ix = EXT_FIRST_INDEX(eh);
543 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
544 if (k != 0 &&
545 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400546 printk(KERN_DEBUG "k=%d, ix=0x%p, "
547 "first=0x%p\n", k,
548 ix, EXT_FIRST_INDEX(eh));
549 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700550 le32_to_cpu(ix->ei_block),
551 le32_to_cpu(ix[-1].ei_block));
552 }
553 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400554 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700555 if (block < le32_to_cpu(ix->ei_block))
556 break;
557 chix = ix;
558 }
559 BUG_ON(chix != path->p_idx);
560 }
561#endif
562
563}
564
565/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700566 * ext4_ext_binsearch:
567 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400568 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700569 */
570static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500571ext4_ext_binsearch(struct inode *inode,
572 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700573{
574 struct ext4_extent_header *eh = path->p_hdr;
575 struct ext4_extent *r, *l, *m;
576
Alex Tomasa86c6182006-10-11 01:21:03 -0700577 if (eh->eh_entries == 0) {
578 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700579 * this leaf is empty:
580 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700581 */
582 return;
583 }
584
Eric Sandeenbba90742008-01-28 23:58:27 -0500585 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700586
587 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400588 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700589
590 while (l <= r) {
591 m = l + (r - l) / 2;
592 if (block < le32_to_cpu(m->ee_block))
593 r = m - 1;
594 else
595 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400596 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
597 m, le32_to_cpu(m->ee_block),
598 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700599 }
600
601 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400602 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400603 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400604 ext4_ext_pblock(path->p_ext),
Mingming553f9002009-09-18 13:34:55 -0400605 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400606 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700607
608#ifdef CHECK_BINSEARCH
609 {
610 struct ext4_extent *chex, *ex;
611 int k;
612
613 chex = ex = EXT_FIRST_EXTENT(eh);
614 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
615 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400616 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700617 if (block < le32_to_cpu(ex->ee_block))
618 break;
619 chex = ex;
620 }
621 BUG_ON(chex != path->p_ext);
622 }
623#endif
624
625}
626
627int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
628{
629 struct ext4_extent_header *eh;
630
631 eh = ext_inode_hdr(inode);
632 eh->eh_depth = 0;
633 eh->eh_entries = 0;
634 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400635 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700636 ext4_mark_inode_dirty(handle, inode);
637 ext4_ext_invalidate_cache(inode);
638 return 0;
639}
640
641struct ext4_ext_path *
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500642ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
643 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700644{
645 struct ext4_extent_header *eh;
646 struct buffer_head *bh;
647 short int depth, i, ppos = 0, alloc = 0;
648
649 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400650 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700651
652 /* account possible depth increase */
653 if (!path) {
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800654 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700655 GFP_NOFS);
656 if (!path)
657 return ERR_PTR(-ENOMEM);
658 alloc = 1;
659 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700660 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400661 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700662
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400663 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700664 /* walk through the tree */
665 while (i) {
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400666 int need_to_validate = 0;
667
Alex Tomasa86c6182006-10-11 01:21:03 -0700668 ext_debug("depth %d: num %d, max %d\n",
669 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400670
Alex Tomasa86c6182006-10-11 01:21:03 -0700671 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400672 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700673 path[ppos].p_depth = i;
674 path[ppos].p_ext = NULL;
675
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400676 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
677 if (unlikely(!bh))
Alex Tomasa86c6182006-10-11 01:21:03 -0700678 goto err;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400679 if (!bh_uptodate_or_lock(bh)) {
Jiaying Zhang0562e0b2011-03-21 21:38:05 -0400680 trace_ext4_ext_load_extent(inode, block,
681 path[ppos].p_block);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400682 if (bh_submit_read(bh) < 0) {
683 put_bh(bh);
684 goto err;
685 }
686 /* validate the extent entries */
687 need_to_validate = 1;
688 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700689 eh = ext_block_hdr(bh);
690 ppos++;
Frank Mayhar273df552010-03-02 11:46:09 -0500691 if (unlikely(ppos > depth)) {
692 put_bh(bh);
693 EXT4_ERROR_INODE(inode,
694 "ppos %d > depth %d", ppos, depth);
695 goto err;
696 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700697 path[ppos].p_bh = bh;
698 path[ppos].p_hdr = eh;
699 i--;
700
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400701 if (need_to_validate && ext4_ext_check(inode, eh, i))
Alex Tomasa86c6182006-10-11 01:21:03 -0700702 goto err;
703 }
704
705 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700706 path[ppos].p_ext = NULL;
707 path[ppos].p_idx = NULL;
708
Alex Tomasa86c6182006-10-11 01:21:03 -0700709 /* find extent */
710 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400711 /* if not an empty leaf */
712 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400713 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700714
715 ext4_ext_show_path(inode, path);
716
717 return path;
718
719err:
720 ext4_ext_drop_refs(path);
721 if (alloc)
722 kfree(path);
723 return ERR_PTR(-EIO);
724}
725
726/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700727 * ext4_ext_insert_index:
728 * insert new index [@logical;@ptr] into the block at @curp;
729 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700730 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400731static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
732 struct ext4_ext_path *curp,
733 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700734{
735 struct ext4_extent_idx *ix;
736 int len, err;
737
Avantika Mathur7e028972006-12-06 20:41:33 -0800738 err = ext4_ext_get_access(handle, inode, curp);
739 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700740 return err;
741
Frank Mayhar273df552010-03-02 11:46:09 -0500742 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
743 EXT4_ERROR_INODE(inode,
744 "logical %d == ei_block %d!",
745 logical, le32_to_cpu(curp->p_idx->ei_block));
746 return -EIO;
747 }
Robin Dongd4620312011-07-17 23:43:42 -0400748
749 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
750 >= le16_to_cpu(curp->p_hdr->eh_max))) {
751 EXT4_ERROR_INODE(inode,
752 "eh_entries %d >= eh_max %d!",
753 le16_to_cpu(curp->p_hdr->eh_entries),
754 le16_to_cpu(curp->p_hdr->eh_max));
755 return -EIO;
756 }
757
Alex Tomasa86c6182006-10-11 01:21:03 -0700758 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
759 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
760 /* insert after */
761 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
762 len = (len - 1) * sizeof(struct ext4_extent_idx);
763 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400764 ext_debug("insert new index %d after: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700765 "move %d from 0x%p to 0x%p\n",
766 logical, ptr, len,
767 (curp->p_idx + 1), (curp->p_idx + 2));
768 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
769 }
770 ix = curp->p_idx + 1;
771 } else {
772 /* insert before */
773 len = len * sizeof(struct ext4_extent_idx);
774 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400775 ext_debug("insert new index %d before: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700776 "move %d from 0x%p to 0x%p\n",
777 logical, ptr, len,
778 curp->p_idx, (curp->p_idx + 1));
779 memmove(curp->p_idx + 1, curp->p_idx, len);
780 ix = curp->p_idx;
781 }
782
783 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700784 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400785 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700786
Frank Mayhar273df552010-03-02 11:46:09 -0500787 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
788 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
789 return -EIO;
790 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700791
792 err = ext4_ext_dirty(handle, inode, curp);
793 ext4_std_error(inode->i_sb, err);
794
795 return err;
796}
797
798/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700799 * ext4_ext_split:
800 * inserts new subtree into the path, using free index entry
801 * at depth @at:
802 * - allocates all needed blocks (new leaf and all intermediate index blocks)
803 * - makes decision where to split
804 * - moves remaining extents and index entries (right to the split point)
805 * into the newly allocated blocks
806 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700807 */
808static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -0400809 unsigned int flags,
810 struct ext4_ext_path *path,
811 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -0700812{
813 struct buffer_head *bh = NULL;
814 int depth = ext_depth(inode);
815 struct ext4_extent_header *neh;
816 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -0700817 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700818 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700819 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700820 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700821 int err = 0;
822
823 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700824 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700825
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700826 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700827 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -0500828 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
829 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
830 return -EIO;
831 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700832 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
833 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700834 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700835 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400836 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700837 } else {
838 border = newext->ee_block;
839 ext_debug("leaf will be added."
840 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400841 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700842 }
843
844 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700845 * If error occurs, then we break processing
846 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700847 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700848 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700849 */
850
851 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700852 * Get array to track all allocated blocks.
853 * We need this to handle errors and free blocks
854 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700855 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800856 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700857 if (!ablocks)
858 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700859
860 /* allocate all needed blocks */
861 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
862 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400863 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400864 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -0700865 if (newblock == 0)
866 goto cleanup;
867 ablocks[a] = newblock;
868 }
869
870 /* initialize new leaf */
871 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -0500872 if (unlikely(newblock == 0)) {
873 EXT4_ERROR_INODE(inode, "newblock == 0!");
874 err = -EIO;
875 goto cleanup;
876 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700877 bh = sb_getblk(inode->i_sb, newblock);
878 if (!bh) {
879 err = -EIO;
880 goto cleanup;
881 }
882 lock_buffer(bh);
883
Avantika Mathur7e028972006-12-06 20:41:33 -0800884 err = ext4_journal_get_create_access(handle, bh);
885 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700886 goto cleanup;
887
888 neh = ext_block_hdr(bh);
889 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400890 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700891 neh->eh_magic = EXT4_EXT_MAGIC;
892 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700893
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700894 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -0500895 if (unlikely(path[depth].p_hdr->eh_entries !=
896 path[depth].p_hdr->eh_max)) {
897 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
898 path[depth].p_hdr->eh_entries,
899 path[depth].p_hdr->eh_max);
900 err = -EIO;
901 goto cleanup;
902 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700903 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400904 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
905 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -0700906 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400907 struct ext4_extent *ex;
908 ex = EXT_FIRST_EXTENT(neh);
909 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400910 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700911 }
912
913 set_buffer_uptodate(bh);
914 unlock_buffer(bh);
915
Frank Mayhar03901312009-01-07 00:06:22 -0500916 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800917 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700918 goto cleanup;
919 brelse(bh);
920 bh = NULL;
921
922 /* correct old leaf */
923 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800924 err = ext4_ext_get_access(handle, inode, path + depth);
925 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700926 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400927 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800928 err = ext4_ext_dirty(handle, inode, path + depth);
929 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700930 goto cleanup;
931
932 }
933
934 /* create intermediate indexes */
935 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -0500936 if (unlikely(k < 0)) {
937 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
938 err = -EIO;
939 goto cleanup;
940 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700941 if (k)
942 ext_debug("create %d intermediate indices\n", k);
943 /* insert new index into current index block */
944 /* current depth stored in i var */
945 i = depth - 1;
946 while (k--) {
947 oldblock = newblock;
948 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -0500949 bh = sb_getblk(inode->i_sb, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700950 if (!bh) {
951 err = -EIO;
952 goto cleanup;
953 }
954 lock_buffer(bh);
955
Avantika Mathur7e028972006-12-06 20:41:33 -0800956 err = ext4_journal_get_create_access(handle, bh);
957 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700958 goto cleanup;
959
960 neh = ext_block_hdr(bh);
961 neh->eh_entries = cpu_to_le16(1);
962 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400963 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700964 neh->eh_depth = cpu_to_le16(depth - i);
965 fidx = EXT_FIRST_INDEX(neh);
966 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700967 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700968
Eric Sandeenbba90742008-01-28 23:58:27 -0500969 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
970 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700971
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400972 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -0500973 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
974 EXT_LAST_INDEX(path[i].p_hdr))) {
975 EXT4_ERROR_INODE(inode,
976 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
977 le32_to_cpu(path[i].p_ext->ee_block));
978 err = -EIO;
979 goto cleanup;
980 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400981 /* start copy indexes */
982 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
983 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
984 EXT_MAX_INDEX(path[i].p_hdr));
985 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -0700986 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400987 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -0700988 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400989 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700990 }
991 set_buffer_uptodate(bh);
992 unlock_buffer(bh);
993
Frank Mayhar03901312009-01-07 00:06:22 -0500994 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800995 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700996 goto cleanup;
997 brelse(bh);
998 bh = NULL;
999
1000 /* correct old index */
1001 if (m) {
1002 err = ext4_ext_get_access(handle, inode, path + i);
1003 if (err)
1004 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001005 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001006 err = ext4_ext_dirty(handle, inode, path + i);
1007 if (err)
1008 goto cleanup;
1009 }
1010
1011 i--;
1012 }
1013
1014 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001015 err = ext4_ext_insert_index(handle, inode, path + at,
1016 le32_to_cpu(border), newblock);
1017
1018cleanup:
1019 if (bh) {
1020 if (buffer_locked(bh))
1021 unlock_buffer(bh);
1022 brelse(bh);
1023 }
1024
1025 if (err) {
1026 /* free all allocated blocks in error case */
1027 for (i = 0; i < depth; i++) {
1028 if (!ablocks[i])
1029 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001030 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001031 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001032 }
1033 }
1034 kfree(ablocks);
1035
1036 return err;
1037}
1038
1039/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001040 * ext4_ext_grow_indepth:
1041 * implements tree growing procedure:
1042 * - allocates new block
1043 * - moves top-level data (index block or leaf) into the new block
1044 * - initializes new top-level, creating index that points to the
1045 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001046 */
1047static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001048 unsigned int flags,
1049 struct ext4_ext_path *path,
1050 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001051{
1052 struct ext4_ext_path *curp = path;
1053 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001054 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001055 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001056 int err = 0;
1057
Allison Henderson55f020d2011-05-25 07:41:26 -04001058 newblock = ext4_ext_new_meta_block(handle, inode, path,
1059 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001060 if (newblock == 0)
1061 return err;
1062
1063 bh = sb_getblk(inode->i_sb, newblock);
1064 if (!bh) {
1065 err = -EIO;
1066 ext4_std_error(inode->i_sb, err);
1067 return err;
1068 }
1069 lock_buffer(bh);
1070
Avantika Mathur7e028972006-12-06 20:41:33 -08001071 err = ext4_journal_get_create_access(handle, bh);
1072 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001073 unlock_buffer(bh);
1074 goto out;
1075 }
1076
1077 /* move top-level index/leaf into new block */
1078 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
1079
1080 /* set size of new block */
1081 neh = ext_block_hdr(bh);
1082 /* old root could have indexes or leaves
1083 * so calculate e_max right way */
1084 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001085 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001086 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001087 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001088 neh->eh_magic = EXT4_EXT_MAGIC;
1089 set_buffer_uptodate(bh);
1090 unlock_buffer(bh);
1091
Frank Mayhar03901312009-01-07 00:06:22 -05001092 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001093 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001094 goto out;
1095
1096 /* create index in new top-level index: num,max,pointer */
Avantika Mathur7e028972006-12-06 20:41:33 -08001097 err = ext4_ext_get_access(handle, inode, curp);
1098 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001099 goto out;
1100
1101 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001102 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001103 curp->p_hdr->eh_entries = cpu_to_le16(1);
1104 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
Dmitry Monakhove9f410b2007-07-18 09:09:15 -04001105
1106 if (path[0].p_hdr->eh_depth)
1107 curp->p_idx->ei_block =
1108 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
1109 else
1110 curp->p_idx->ei_block =
1111 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001112 ext4_idx_store_pblock(curp->p_idx, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001113
1114 neh = ext_inode_hdr(inode);
Mingming Cao2ae02102006-10-11 01:21:11 -07001115 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001116 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001117 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001118 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001119
1120 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
1121 err = ext4_ext_dirty(handle, inode, curp);
1122out:
1123 brelse(bh);
1124
1125 return err;
1126}
1127
1128/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001129 * ext4_ext_create_new_leaf:
1130 * finds empty index and adds new leaf.
1131 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001132 */
1133static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001134 unsigned int flags,
1135 struct ext4_ext_path *path,
1136 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001137{
1138 struct ext4_ext_path *curp;
1139 int depth, i, err = 0;
1140
1141repeat:
1142 i = depth = ext_depth(inode);
1143
1144 /* walk up to the tree and look for free index entry */
1145 curp = path + depth;
1146 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1147 i--;
1148 curp--;
1149 }
1150
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001151 /* we use already allocated block for index block,
1152 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001153 if (EXT_HAS_FREE_INDEX(curp)) {
1154 /* if we found index with free entry, then use that
1155 * entry: create all needed subtree and add new leaf */
Allison Henderson55f020d2011-05-25 07:41:26 -04001156 err = ext4_ext_split(handle, inode, flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001157 if (err)
1158 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001159
1160 /* refill path */
1161 ext4_ext_drop_refs(path);
1162 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001163 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1164 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001165 if (IS_ERR(path))
1166 err = PTR_ERR(path);
1167 } else {
1168 /* tree is full, time to grow in depth */
Allison Henderson55f020d2011-05-25 07:41:26 -04001169 err = ext4_ext_grow_indepth(handle, inode, flags,
1170 path, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001171 if (err)
1172 goto out;
1173
1174 /* refill path */
1175 ext4_ext_drop_refs(path);
1176 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001177 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1178 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001179 if (IS_ERR(path)) {
1180 err = PTR_ERR(path);
1181 goto out;
1182 }
1183
1184 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001185 * only first (depth 0 -> 1) produces free space;
1186 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001187 */
1188 depth = ext_depth(inode);
1189 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001190 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001191 goto repeat;
1192 }
1193 }
1194
1195out:
1196 return err;
1197}
1198
1199/*
Alex Tomas1988b512008-01-28 23:58:27 -05001200 * search the closest allocated block to the left for *logical
1201 * and returns it at @logical + it's physical address at @phys
1202 * if *logical is the smallest allocated block, the function
1203 * returns 0 at @phys
1204 * return value contains 0 (success) or error code
1205 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001206static int ext4_ext_search_left(struct inode *inode,
1207 struct ext4_ext_path *path,
1208 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001209{
1210 struct ext4_extent_idx *ix;
1211 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001212 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001213
Frank Mayhar273df552010-03-02 11:46:09 -05001214 if (unlikely(path == NULL)) {
1215 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1216 return -EIO;
1217 }
Alex Tomas1988b512008-01-28 23:58:27 -05001218 depth = path->p_depth;
1219 *phys = 0;
1220
1221 if (depth == 0 && path->p_ext == NULL)
1222 return 0;
1223
1224 /* usually extent in the path covers blocks smaller
1225 * then *logical, but it can be that extent is the
1226 * first one in the file */
1227
1228 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001229 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001230 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001231 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1232 EXT4_ERROR_INODE(inode,
1233 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1234 *logical, le32_to_cpu(ex->ee_block));
1235 return -EIO;
1236 }
Alex Tomas1988b512008-01-28 23:58:27 -05001237 while (--depth >= 0) {
1238 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001239 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1240 EXT4_ERROR_INODE(inode,
1241 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
1242 ix != NULL ? ix->ei_block : 0,
1243 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
1244 EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block : 0,
1245 depth);
1246 return -EIO;
1247 }
Alex Tomas1988b512008-01-28 23:58:27 -05001248 }
1249 return 0;
1250 }
1251
Frank Mayhar273df552010-03-02 11:46:09 -05001252 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1253 EXT4_ERROR_INODE(inode,
1254 "logical %d < ee_block %d + ee_len %d!",
1255 *logical, le32_to_cpu(ex->ee_block), ee_len);
1256 return -EIO;
1257 }
Alex Tomas1988b512008-01-28 23:58:27 -05001258
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001259 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001260 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001261 return 0;
1262}
1263
1264/*
1265 * search the closest allocated block to the right for *logical
1266 * and returns it at @logical + it's physical address at @phys
1267 * if *logical is the smallest allocated block, the function
1268 * returns 0 at @phys
1269 * return value contains 0 (success) or error code
1270 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001271static int ext4_ext_search_right(struct inode *inode,
1272 struct ext4_ext_path *path,
1273 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001274{
1275 struct buffer_head *bh = NULL;
1276 struct ext4_extent_header *eh;
1277 struct ext4_extent_idx *ix;
1278 struct ext4_extent *ex;
1279 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001280 int depth; /* Note, NOT eh_depth; depth from top of tree */
1281 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001282
Frank Mayhar273df552010-03-02 11:46:09 -05001283 if (unlikely(path == NULL)) {
1284 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1285 return -EIO;
1286 }
Alex Tomas1988b512008-01-28 23:58:27 -05001287 depth = path->p_depth;
1288 *phys = 0;
1289
1290 if (depth == 0 && path->p_ext == NULL)
1291 return 0;
1292
1293 /* usually extent in the path covers blocks smaller
1294 * then *logical, but it can be that extent is the
1295 * first one in the file */
1296
1297 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001298 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001299 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001300 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1301 EXT4_ERROR_INODE(inode,
1302 "first_extent(path[%d].p_hdr) != ex",
1303 depth);
1304 return -EIO;
1305 }
Alex Tomas1988b512008-01-28 23:58:27 -05001306 while (--depth >= 0) {
1307 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001308 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1309 EXT4_ERROR_INODE(inode,
1310 "ix != EXT_FIRST_INDEX *logical %d!",
1311 *logical);
1312 return -EIO;
1313 }
Alex Tomas1988b512008-01-28 23:58:27 -05001314 }
1315 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001316 *phys = ext4_ext_pblock(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001317 return 0;
1318 }
1319
Frank Mayhar273df552010-03-02 11:46:09 -05001320 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1321 EXT4_ERROR_INODE(inode,
1322 "logical %d < ee_block %d + ee_len %d!",
1323 *logical, le32_to_cpu(ex->ee_block), ee_len);
1324 return -EIO;
1325 }
Alex Tomas1988b512008-01-28 23:58:27 -05001326
1327 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1328 /* next allocated block in this leaf */
1329 ex++;
1330 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001331 *phys = ext4_ext_pblock(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001332 return 0;
1333 }
1334
1335 /* go up and search for index to the right */
1336 while (--depth >= 0) {
1337 ix = path[depth].p_idx;
1338 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001339 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001340 }
1341
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001342 /* we've gone up to the root and found no index to the right */
1343 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001344
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001345got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001346 /* we've found index to the right, let's
1347 * follow it and find the closest allocated
1348 * block to the right */
1349 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001350 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001351 while (++depth < path->p_depth) {
1352 bh = sb_bread(inode->i_sb, block);
1353 if (bh == NULL)
1354 return -EIO;
1355 eh = ext_block_hdr(bh);
Eric Sandeen395a87b2009-03-10 18:18:47 -04001356 /* subtract from p_depth to get proper eh_depth */
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001357 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001358 put_bh(bh);
1359 return -EIO;
1360 }
1361 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001362 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001363 put_bh(bh);
1364 }
1365
1366 bh = sb_bread(inode->i_sb, block);
1367 if (bh == NULL)
1368 return -EIO;
1369 eh = ext_block_hdr(bh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001370 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001371 put_bh(bh);
1372 return -EIO;
1373 }
1374 ex = EXT_FIRST_EXTENT(eh);
1375 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001376 *phys = ext4_ext_pblock(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001377 put_bh(bh);
1378 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001379}
1380
1381/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001382 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001383 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001384 * NOTE: it considers block number from index entry as
1385 * allocated block. Thus, index entries have to be consistent
1386 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001387 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001388static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001389ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1390{
1391 int depth;
1392
1393 BUG_ON(path == NULL);
1394 depth = path->p_depth;
1395
1396 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001397 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001398
1399 while (depth >= 0) {
1400 if (depth == path->p_depth) {
1401 /* leaf */
1402 if (path[depth].p_ext !=
1403 EXT_LAST_EXTENT(path[depth].p_hdr))
1404 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1405 } else {
1406 /* index */
1407 if (path[depth].p_idx !=
1408 EXT_LAST_INDEX(path[depth].p_hdr))
1409 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1410 }
1411 depth--;
1412 }
1413
Lukas Czernerf17722f2011-06-06 00:05:17 -04001414 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001415}
1416
1417/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001418 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001419 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001420 */
Robin Dong57187892011-07-23 21:49:07 -04001421static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001422{
1423 int depth;
1424
1425 BUG_ON(path == NULL);
1426 depth = path->p_depth;
1427
1428 /* zero-tree has no leaf blocks at all */
1429 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001430 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001431
1432 /* go to index block */
1433 depth--;
1434
1435 while (depth >= 0) {
1436 if (path[depth].p_idx !=
1437 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001438 return (ext4_lblk_t)
1439 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001440 depth--;
1441 }
1442
Lukas Czernerf17722f2011-06-06 00:05:17 -04001443 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001444}
1445
1446/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001447 * ext4_ext_correct_indexes:
1448 * if leaf gets modified and modified extent is first in the leaf,
1449 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001450 * TODO: do we need to correct tree in all cases?
1451 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001452static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001453 struct ext4_ext_path *path)
1454{
1455 struct ext4_extent_header *eh;
1456 int depth = ext_depth(inode);
1457 struct ext4_extent *ex;
1458 __le32 border;
1459 int k, err = 0;
1460
1461 eh = path[depth].p_hdr;
1462 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001463
1464 if (unlikely(ex == NULL || eh == NULL)) {
1465 EXT4_ERROR_INODE(inode,
1466 "ex %p == NULL or eh %p == NULL", ex, eh);
1467 return -EIO;
1468 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001469
1470 if (depth == 0) {
1471 /* there is no tree at all */
1472 return 0;
1473 }
1474
1475 if (ex != EXT_FIRST_EXTENT(eh)) {
1476 /* we correct tree if first leaf got modified only */
1477 return 0;
1478 }
1479
1480 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001481 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001482 */
1483 k = depth - 1;
1484 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001485 err = ext4_ext_get_access(handle, inode, path + k);
1486 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001487 return err;
1488 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001489 err = ext4_ext_dirty(handle, inode, path + k);
1490 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001491 return err;
1492
1493 while (k--) {
1494 /* change all left-side indexes */
1495 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1496 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001497 err = ext4_ext_get_access(handle, inode, path + k);
1498 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001499 break;
1500 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001501 err = ext4_ext_dirty(handle, inode, path + k);
1502 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001503 break;
1504 }
1505
1506 return err;
1507}
1508
Akira Fujita748de672009-06-17 19:24:03 -04001509int
Alex Tomasa86c6182006-10-11 01:21:03 -07001510ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1511 struct ext4_extent *ex2)
1512{
Amit Arora749269f2007-07-18 09:02:56 -04001513 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001514
1515 /*
1516 * Make sure that either both extents are uninitialized, or
1517 * both are _not_.
1518 */
1519 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1520 return 0;
1521
Amit Arora749269f2007-07-18 09:02:56 -04001522 if (ext4_ext_is_uninitialized(ex1))
1523 max_len = EXT_UNINIT_MAX_LEN;
1524 else
1525 max_len = EXT_INIT_MAX_LEN;
1526
Amit Aroraa2df2a62007-07-17 21:42:41 -04001527 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1528 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1529
1530 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001531 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001532 return 0;
1533
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001534 /*
1535 * To allow future support for preallocated extents to be added
1536 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001537 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001538 */
Amit Arora749269f2007-07-18 09:02:56 -04001539 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001540 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001541#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001542 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001543 return 0;
1544#endif
1545
Theodore Ts'obf89d162010-10-27 21:30:14 -04001546 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001547 return 1;
1548 return 0;
1549}
1550
1551/*
Amit Arora56055d32007-07-17 21:42:38 -04001552 * This function tries to merge the "ex" extent to the next extent in the tree.
1553 * It always tries to merge towards right. If you want to merge towards
1554 * left, pass "ex - 1" as argument instead of "ex".
1555 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1556 * 1 if they got merged.
1557 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001558static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001559 struct ext4_ext_path *path,
1560 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001561{
1562 struct ext4_extent_header *eh;
1563 unsigned int depth, len;
1564 int merge_done = 0;
1565 int uninitialized = 0;
1566
1567 depth = ext_depth(inode);
1568 BUG_ON(path[depth].p_hdr == NULL);
1569 eh = path[depth].p_hdr;
1570
1571 while (ex < EXT_LAST_EXTENT(eh)) {
1572 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1573 break;
1574 /* merge with next extent! */
1575 if (ext4_ext_is_uninitialized(ex))
1576 uninitialized = 1;
1577 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1578 + ext4_ext_get_actual_len(ex + 1));
1579 if (uninitialized)
1580 ext4_ext_mark_uninitialized(ex);
1581
1582 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1583 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1584 * sizeof(struct ext4_extent);
1585 memmove(ex + 1, ex + 2, len);
1586 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001587 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001588 merge_done = 1;
1589 WARN_ON(eh->eh_entries == 0);
1590 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001591 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001592 }
1593
1594 return merge_done;
1595}
1596
1597/*
Yongqiang Yang197217a2011-05-03 11:45:29 -04001598 * This function tries to merge the @ex extent to neighbours in the tree.
1599 * return 1 if merge left else 0.
1600 */
1601static int ext4_ext_try_to_merge(struct inode *inode,
1602 struct ext4_ext_path *path,
1603 struct ext4_extent *ex) {
1604 struct ext4_extent_header *eh;
1605 unsigned int depth;
1606 int merge_done = 0;
1607 int ret = 0;
1608
1609 depth = ext_depth(inode);
1610 BUG_ON(path[depth].p_hdr == NULL);
1611 eh = path[depth].p_hdr;
1612
1613 if (ex > EXT_FIRST_EXTENT(eh))
1614 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1615
1616 if (!merge_done)
1617 ret = ext4_ext_try_to_merge_right(inode, path, ex);
1618
1619 return ret;
1620}
1621
1622/*
Amit Arora25d14f92007-05-24 13:04:13 -04001623 * check if a portion of the "newext" extent overlaps with an
1624 * existing extent.
1625 *
1626 * If there is an overlap discovered, it updates the length of the newext
1627 * such that there will be no overlap, and then returns 1.
1628 * If there is no overlap found, it returns 0.
1629 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001630static unsigned int ext4_ext_check_overlap(struct inode *inode,
1631 struct ext4_extent *newext,
1632 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001633{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001634 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001635 unsigned int depth, len1;
1636 unsigned int ret = 0;
1637
1638 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001639 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001640 depth = ext_depth(inode);
1641 if (!path[depth].p_ext)
1642 goto out;
1643 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1644
1645 /*
1646 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001647 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001648 */
1649 if (b2 < b1) {
1650 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001651 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001652 goto out;
1653 }
1654
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001655 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001656 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001657 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001658 newext->ee_len = cpu_to_le16(len1);
1659 ret = 1;
1660 }
1661
1662 /* check for overlap */
1663 if (b1 + len1 > b2) {
1664 newext->ee_len = cpu_to_le16(b2 - b1);
1665 ret = 1;
1666 }
1667out:
1668 return ret;
1669}
1670
1671/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001672 * ext4_ext_insert_extent:
1673 * tries to merge requsted extent into the existing extent or
1674 * inserts requested extent as new one into the tree,
1675 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001676 */
1677int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1678 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04001679 struct ext4_extent *newext, int flag)
Alex Tomasa86c6182006-10-11 01:21:03 -07001680{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001681 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001682 struct ext4_extent *ex, *fex;
1683 struct ext4_extent *nearex; /* nearest extent */
1684 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001685 int depth, len, err;
1686 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001687 unsigned uninitialized = 0;
Allison Henderson55f020d2011-05-25 07:41:26 -04001688 int flags = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001689
Frank Mayhar273df552010-03-02 11:46:09 -05001690 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1691 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1692 return -EIO;
1693 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001694 depth = ext_depth(inode);
1695 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001696 if (unlikely(path[depth].p_hdr == NULL)) {
1697 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1698 return -EIO;
1699 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001700
1701 /* try to insert block into found extent and return */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001702 if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
Mingming Cao00314622009-09-28 15:49:08 -04001703 && ext4_can_extents_be_merged(inode, ex, newext)) {
Mingming553f9002009-09-18 13:34:55 -04001704 ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04001705 ext4_ext_is_uninitialized(newext),
1706 ext4_ext_get_actual_len(newext),
1707 le32_to_cpu(ex->ee_block),
1708 ext4_ext_is_uninitialized(ex),
1709 ext4_ext_get_actual_len(ex),
1710 ext4_ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001711 err = ext4_ext_get_access(handle, inode, path + depth);
1712 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001713 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001714
1715 /*
1716 * ext4_can_extents_be_merged should have checked that either
1717 * both extents are uninitialized, or both aren't. Thus we
1718 * need to check only one of them here.
1719 */
1720 if (ext4_ext_is_uninitialized(ex))
1721 uninitialized = 1;
1722 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1723 + ext4_ext_get_actual_len(newext));
1724 if (uninitialized)
1725 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001726 eh = path[depth].p_hdr;
1727 nearex = ex;
1728 goto merge;
1729 }
1730
Alex Tomasa86c6182006-10-11 01:21:03 -07001731 depth = ext_depth(inode);
1732 eh = path[depth].p_hdr;
1733 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1734 goto has_space;
1735
1736 /* probably next leaf has space for us? */
1737 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04001738 next = EXT_MAX_BLOCKS;
1739 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
Robin Dong57187892011-07-23 21:49:07 -04001740 next = ext4_ext_next_leaf_block(path);
Robin Dong598dbdf2011-07-11 18:24:01 -04001741 if (next != EXT_MAX_BLOCKS) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001742 ext_debug("next leaf block - %d\n", next);
1743 BUG_ON(npath != NULL);
1744 npath = ext4_ext_find_extent(inode, next, NULL);
1745 if (IS_ERR(npath))
1746 return PTR_ERR(npath);
1747 BUG_ON(npath->p_depth != path->p_depth);
1748 eh = npath[depth].p_hdr;
1749 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001750 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001751 le16_to_cpu(eh->eh_entries));
1752 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04001753 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07001754 }
1755 ext_debug("next leaf has no free space(%d,%d)\n",
1756 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1757 }
1758
1759 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001760 * There is no free space in the found leaf.
1761 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001762 */
Allison Henderson55f020d2011-05-25 07:41:26 -04001763 if (flag & EXT4_GET_BLOCKS_PUNCH_OUT_EXT)
1764 flags = EXT4_MB_USE_ROOT_BLOCKS;
1765 err = ext4_ext_create_new_leaf(handle, inode, flags, path, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001766 if (err)
1767 goto cleanup;
1768 depth = ext_depth(inode);
1769 eh = path[depth].p_hdr;
1770
1771has_space:
1772 nearex = path[depth].p_ext;
1773
Avantika Mathur7e028972006-12-06 20:41:33 -08001774 err = ext4_ext_get_access(handle, inode, path + depth);
1775 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001776 goto cleanup;
1777
1778 if (!nearex) {
1779 /* there is no extent in this leaf, create first one */
Mingming553f9002009-09-18 13:34:55 -04001780 ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001781 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001782 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001783 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001784 ext4_ext_get_actual_len(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001785 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1786 } else if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001787 > le32_to_cpu(nearex->ee_block)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001788/* BUG_ON(newext->ee_block == nearex->ee_block); */
1789 if (nearex != EXT_LAST_EXTENT(eh)) {
1790 len = EXT_MAX_EXTENT(eh) - nearex;
1791 len = (len - 1) * sizeof(struct ext4_extent);
1792 len = len < 0 ? 0 : len;
Mingming553f9002009-09-18 13:34:55 -04001793 ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001794 "move %d from 0x%p to 0x%p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001795 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001796 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001797 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001798 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001799 nearex, len, nearex + 1, nearex + 2);
1800 memmove(nearex + 2, nearex + 1, len);
1801 }
1802 path[depth].p_ext = nearex + 1;
1803 } else {
1804 BUG_ON(newext->ee_block == nearex->ee_block);
1805 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1806 len = len < 0 ? 0 : len;
Mingming553f9002009-09-18 13:34:55 -04001807 ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001808 "move %d from 0x%p to 0x%p\n",
1809 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001810 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001811 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001812 ext4_ext_get_actual_len(newext),
Robin Dong07379642011-07-23 21:51:07 -04001813 nearex, len, nearex, nearex + 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001814 memmove(nearex + 1, nearex, len);
1815 path[depth].p_ext = nearex;
1816 }
1817
Marcin Slusarze8546d02008-04-17 10:38:59 -04001818 le16_add_cpu(&eh->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001819 nearex = path[depth].p_ext;
1820 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001821 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001822 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001823
1824merge:
1825 /* try to merge extents to the right */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001826 if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
Mingming Cao00314622009-09-28 15:49:08 -04001827 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001828
1829 /* try to merge extents to the left */
1830
1831 /* time to correct all indexes above */
1832 err = ext4_ext_correct_indexes(handle, inode, path);
1833 if (err)
1834 goto cleanup;
1835
1836 err = ext4_ext_dirty(handle, inode, path + depth);
1837
1838cleanup:
1839 if (npath) {
1840 ext4_ext_drop_refs(npath);
1841 kfree(npath);
1842 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001843 ext4_ext_invalidate_cache(inode);
1844 return err;
1845}
1846
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001847static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1848 ext4_lblk_t num, ext_prepare_callback func,
1849 void *cbdata)
Eric Sandeen6873fa02008-10-07 00:46:36 -04001850{
1851 struct ext4_ext_path *path = NULL;
1852 struct ext4_ext_cache cbex;
1853 struct ext4_extent *ex;
1854 ext4_lblk_t next, start = 0, end = 0;
1855 ext4_lblk_t last = block + num;
1856 int depth, exists, err = 0;
1857
1858 BUG_ON(func == NULL);
1859 BUG_ON(inode == NULL);
1860
Lukas Czernerf17722f2011-06-06 00:05:17 -04001861 while (block < last && block != EXT_MAX_BLOCKS) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04001862 num = last - block;
1863 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001864 down_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001865 path = ext4_ext_find_extent(inode, block, path);
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001866 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001867 if (IS_ERR(path)) {
1868 err = PTR_ERR(path);
1869 path = NULL;
1870 break;
1871 }
1872
1873 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05001874 if (unlikely(path[depth].p_hdr == NULL)) {
1875 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1876 err = -EIO;
1877 break;
1878 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04001879 ex = path[depth].p_ext;
1880 next = ext4_ext_next_allocated_block(path);
1881
1882 exists = 0;
1883 if (!ex) {
1884 /* there is no extent yet, so try to allocate
1885 * all requested space */
1886 start = block;
1887 end = block + num;
1888 } else if (le32_to_cpu(ex->ee_block) > block) {
1889 /* need to allocate space before found extent */
1890 start = block;
1891 end = le32_to_cpu(ex->ee_block);
1892 if (block + num < end)
1893 end = block + num;
1894 } else if (block >= le32_to_cpu(ex->ee_block)
1895 + ext4_ext_get_actual_len(ex)) {
1896 /* need to allocate space after found extent */
1897 start = block;
1898 end = block + num;
1899 if (end >= next)
1900 end = next;
1901 } else if (block >= le32_to_cpu(ex->ee_block)) {
1902 /*
1903 * some part of requested space is covered
1904 * by found extent
1905 */
1906 start = block;
1907 end = le32_to_cpu(ex->ee_block)
1908 + ext4_ext_get_actual_len(ex);
1909 if (block + num < end)
1910 end = block + num;
1911 exists = 1;
1912 } else {
1913 BUG();
1914 }
1915 BUG_ON(end <= start);
1916
1917 if (!exists) {
1918 cbex.ec_block = start;
1919 cbex.ec_len = end - start;
1920 cbex.ec_start = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04001921 } else {
1922 cbex.ec_block = le32_to_cpu(ex->ee_block);
1923 cbex.ec_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001924 cbex.ec_start = ext4_ext_pblock(ex);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001925 }
1926
Frank Mayhar273df552010-03-02 11:46:09 -05001927 if (unlikely(cbex.ec_len == 0)) {
1928 EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
1929 err = -EIO;
1930 break;
1931 }
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04001932 err = func(inode, next, &cbex, ex, cbdata);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001933 ext4_ext_drop_refs(path);
1934
1935 if (err < 0)
1936 break;
1937
1938 if (err == EXT_REPEAT)
1939 continue;
1940 else if (err == EXT_BREAK) {
1941 err = 0;
1942 break;
1943 }
1944
1945 if (ext_depth(inode) != depth) {
1946 /* depth was changed. we have to realloc path */
1947 kfree(path);
1948 path = NULL;
1949 }
1950
1951 block = cbex.ec_block + cbex.ec_len;
1952 }
1953
1954 if (path) {
1955 ext4_ext_drop_refs(path);
1956 kfree(path);
1957 }
1958
1959 return err;
1960}
1961
Avantika Mathur09b88252006-12-06 20:41:36 -08001962static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001963ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05001964 __u32 len, ext4_fsblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07001965{
1966 struct ext4_ext_cache *cex;
1967 BUG_ON(len == 0);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001968 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001969 cex = &EXT4_I(inode)->i_cached_extent;
Alex Tomasa86c6182006-10-11 01:21:03 -07001970 cex->ec_block = block;
1971 cex->ec_len = len;
1972 cex->ec_start = start;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001973 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001974}
1975
1976/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001977 * ext4_ext_put_gap_in_cache:
1978 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07001979 * and cache this gap
1980 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001981static void
Alex Tomasa86c6182006-10-11 01:21:03 -07001982ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001983 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07001984{
1985 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001986 unsigned long len;
1987 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001988 struct ext4_extent *ex;
1989
1990 ex = path[depth].p_ext;
1991 if (ex == NULL) {
1992 /* there is no extent yet, so gap is [0;-] */
1993 lblock = 0;
Lukas Czernerf17722f2011-06-06 00:05:17 -04001994 len = EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001995 ext_debug("cache gap(whole file):");
1996 } else if (block < le32_to_cpu(ex->ee_block)) {
1997 lblock = block;
1998 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05001999 ext_debug("cache gap(before): %u [%u:%u]",
2000 block,
2001 le32_to_cpu(ex->ee_block),
2002 ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002003 } else if (block >= 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 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002006 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002007 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002008
2009 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05002010 ext_debug("cache gap(after): [%u:%u] %u",
2011 le32_to_cpu(ex->ee_block),
2012 ext4_ext_get_actual_len(ex),
2013 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002014 BUG_ON(next == lblock);
2015 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07002016 } else {
2017 lblock = len = 0;
2018 BUG();
2019 }
2020
Eric Sandeenbba90742008-01-28 23:58:27 -05002021 ext_debug(" -> %u:%lu\n", lblock, len);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002022 ext4_ext_put_in_cache(inode, lblock, len, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002023}
2024
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002025/*
Robin Dongb7ca1e82011-07-23 21:53:25 -04002026 * ext4_ext_check_cache()
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002027 * Checks to see if the given block is in the cache.
2028 * If it is, the cached extent is stored in the given
2029 * cache extent pointer. If the cached extent is a hole,
2030 * this routine should be used instead of
2031 * ext4_ext_in_cache if the calling function needs to
2032 * know the size of the hole.
2033 *
2034 * @inode: The files inode
2035 * @block: The block to look for in the cache
2036 * @ex: Pointer where the cached extent will be stored
2037 * if it contains block
2038 *
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002039 * Return 0 if cache is invalid; 1 if the cache is valid
2040 */
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002041static int ext4_ext_check_cache(struct inode *inode, ext4_lblk_t block,
2042 struct ext4_ext_cache *ex){
Alex Tomasa86c6182006-10-11 01:21:03 -07002043 struct ext4_ext_cache *cex;
Vivek Haldar77f41352011-05-22 21:24:16 -04002044 struct ext4_sb_info *sbi;
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002045 int ret = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002046
Theodore Ts'o60e66792010-05-17 07:00:00 -04002047 /*
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002048 * We borrow i_block_reservation_lock to protect i_cached_extent
2049 */
2050 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07002051 cex = &EXT4_I(inode)->i_cached_extent;
Vivek Haldar77f41352011-05-22 21:24:16 -04002052 sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002053
2054 /* has cache valid data? */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002055 if (cex->ec_len == 0)
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002056 goto errout;
Alex Tomasa86c6182006-10-11 01:21:03 -07002057
Akinobu Mita731eb1a2010-03-03 23:55:01 -05002058 if (in_range(block, cex->ec_block, cex->ec_len)) {
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002059 memcpy(ex, cex, sizeof(struct ext4_ext_cache));
Eric Sandeenbba90742008-01-28 23:58:27 -05002060 ext_debug("%u cached by %u:%u:%llu\n",
2061 block,
2062 cex->ec_block, cex->ec_len, cex->ec_start);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002063 ret = 1;
Alex Tomasa86c6182006-10-11 01:21:03 -07002064 }
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002065errout:
Vivek Haldar77f41352011-05-22 21:24:16 -04002066 if (!ret)
2067 sbi->extent_cache_misses++;
2068 else
2069 sbi->extent_cache_hits++;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002070 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
2071 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07002072}
2073
2074/*
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002075 * ext4_ext_in_cache()
2076 * Checks to see if the given block is in the cache.
2077 * If it is, the cached extent is stored in the given
2078 * extent pointer.
2079 *
2080 * @inode: The files inode
2081 * @block: The block to look for in the cache
2082 * @ex: Pointer where the cached extent will be stored
2083 * if it contains block
2084 *
2085 * Return 0 if cache is invalid; 1 if the cache is valid
2086 */
2087static int
2088ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
2089 struct ext4_extent *ex)
2090{
2091 struct ext4_ext_cache cex;
2092 int ret = 0;
2093
2094 if (ext4_ext_check_cache(inode, block, &cex)) {
2095 ex->ee_block = cpu_to_le32(cex.ec_block);
2096 ext4_ext_store_pblock(ex, cex.ec_start);
2097 ex->ee_len = cpu_to_le16(cex.ec_len);
2098 ret = 1;
2099 }
2100
2101 return ret;
2102}
2103
2104
2105/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002106 * ext4_ext_rm_idx:
2107 * removes index from the index block.
Alex Tomasa86c6182006-10-11 01:21:03 -07002108 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002109static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07002110 struct ext4_ext_path *path)
2111{
Alex Tomasa86c6182006-10-11 01:21:03 -07002112 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002113 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002114
2115 /* free index block */
2116 path--;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002117 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002118 if (unlikely(path->p_hdr->eh_entries == 0)) {
2119 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2120 return -EIO;
2121 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002122 err = ext4_ext_get_access(handle, inode, path);
2123 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002124 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002125
2126 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2127 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2128 len *= sizeof(struct ext4_extent_idx);
2129 memmove(path->p_idx, path->p_idx + 1, len);
2130 }
2131
Marcin Slusarze8546d02008-04-17 10:38:59 -04002132 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002133 err = ext4_ext_dirty(handle, inode, path);
2134 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002135 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002136 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Peter Huewe7dc57612011-02-21 21:01:42 -05002137 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002138 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Alex Tomasa86c6182006-10-11 01:21:03 -07002139 return err;
2140}
2141
2142/*
Mingming Caoee12b632008-08-19 22:16:05 -04002143 * ext4_ext_calc_credits_for_single_extent:
2144 * This routine returns max. credits that needed to insert an extent
2145 * to the extent tree.
2146 * When pass the actual path, the caller should calculate credits
2147 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002148 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002149int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002150 struct ext4_ext_path *path)
2151{
Alex Tomasa86c6182006-10-11 01:21:03 -07002152 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002153 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002154 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002155
Alex Tomasa86c6182006-10-11 01:21:03 -07002156 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002157 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002158 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2159
2160 /*
2161 * There are some space in the leaf tree, no
2162 * need to account for leaf block credit
2163 *
2164 * bitmaps and block group descriptor blocks
2165 * and other metadat blocks still need to be
2166 * accounted.
2167 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002168 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002169 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002170 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002171 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002172 }
2173
Mingming Cao525f4ed2008-08-19 22:15:58 -04002174 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002175}
Alex Tomasa86c6182006-10-11 01:21:03 -07002176
Mingming Caoee12b632008-08-19 22:16:05 -04002177/*
2178 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2179 *
2180 * if nrblocks are fit in a single extent (chunk flag is 1), then
2181 * in the worse case, each tree level index/leaf need to be changed
2182 * if the tree split due to insert a new extent, then the old tree
2183 * index/leaf need to be updated too
2184 *
2185 * If the nrblocks are discontiguous, they could cause
2186 * the whole tree split more than once, but this is really rare.
2187 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002188int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoee12b632008-08-19 22:16:05 -04002189{
2190 int index;
2191 int depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002192
Mingming Caoee12b632008-08-19 22:16:05 -04002193 if (chunk)
2194 index = depth * 2;
2195 else
2196 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002197
Mingming Caoee12b632008-08-19 22:16:05 -04002198 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002199}
2200
2201static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2202 struct ext4_extent *ex,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002203 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002204{
Amit Aroraa2df2a62007-07-17 21:42:41 -04002205 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe6362602009-11-23 07:17:05 -05002206 int flags = EXT4_FREE_BLOCKS_FORGET;
Alex Tomasa86c6182006-10-11 01:21:03 -07002207
Alex Tomasc9de5602008-01-29 00:19:52 -05002208 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
Theodore Ts'oe6362602009-11-23 07:17:05 -05002209 flags |= EXT4_FREE_BLOCKS_METADATA;
Alex Tomasa86c6182006-10-11 01:21:03 -07002210#ifdef EXTENTS_STATS
2211 {
2212 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002213 spin_lock(&sbi->s_ext_stats_lock);
2214 sbi->s_ext_blocks += ee_len;
2215 sbi->s_ext_extents++;
2216 if (ee_len < sbi->s_ext_min)
2217 sbi->s_ext_min = ee_len;
2218 if (ee_len > sbi->s_ext_max)
2219 sbi->s_ext_max = ee_len;
2220 if (ext_depth(inode) > sbi->s_depth_max)
2221 sbi->s_depth_max = ext_depth(inode);
2222 spin_unlock(&sbi->s_ext_stats_lock);
2223 }
2224#endif
2225 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002226 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002227 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002228 ext4_lblk_t num;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002229 ext4_fsblk_t start;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002230
Amit Aroraa2df2a62007-07-17 21:42:41 -04002231 num = le32_to_cpu(ex->ee_block) + ee_len - from;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002232 start = ext4_ext_pblock(ex) + ee_len - num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002233 ext_debug("free last %u blocks starting %llu\n", num, start);
Peter Huewe7dc57612011-02-21 21:01:42 -05002234 ext4_free_blocks(handle, inode, NULL, start, num, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07002235 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002236 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002237 /* head removal */
2238 ext4_lblk_t num;
2239 ext4_fsblk_t start;
2240
2241 num = to - from;
2242 start = ext4_ext_pblock(ex);
2243
2244 ext_debug("free first %u blocks starting %llu\n", num, start);
2245 ext4_free_blocks(handle, inode, 0, start, num, flags);
2246
Alex Tomasa86c6182006-10-11 01:21:03 -07002247 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002248 printk(KERN_INFO "strange request: removal(2) "
2249 "%u-%u from %u:%u\n",
2250 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002251 }
2252 return 0;
2253}
2254
Allison Hendersond583fb82011-05-25 07:41:43 -04002255
2256/*
2257 * ext4_ext_rm_leaf() Removes the extents associated with the
2258 * blocks appearing between "start" and "end", and splits the extents
2259 * if "start" and "end" appear in the same extent
2260 *
2261 * @handle: The journal handle
2262 * @inode: The files inode
2263 * @path: The path to the leaf
2264 * @start: The first block to remove
2265 * @end: The last block to remove
2266 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002267static int
2268ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Allison Hendersond583fb82011-05-25 07:41:43 -04002269 struct ext4_ext_path *path, ext4_lblk_t start,
2270 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002271{
2272 int err = 0, correct_index = 0;
2273 int depth = ext_depth(inode), credits;
2274 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002275 ext4_lblk_t a, b, block;
2276 unsigned num;
2277 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002278 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002279 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002280 struct ext4_extent *ex;
Allison Hendersond583fb82011-05-25 07:41:43 -04002281 struct ext4_map_blocks map;
Alex Tomasa86c6182006-10-11 01:21:03 -07002282
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002283 /* the header must be checked already in ext4_ext_remove_space() */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002284 ext_debug("truncate since %u in leaf\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002285 if (!path[depth].p_hdr)
2286 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2287 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002288 if (unlikely(path[depth].p_hdr == NULL)) {
2289 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2290 return -EIO;
2291 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002292 /* find where to start removing */
2293 ex = EXT_LAST_EXTENT(eh);
2294
2295 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002296 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002297
2298 while (ex >= EXT_FIRST_EXTENT(eh) &&
2299 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002300
2301 if (ext4_ext_is_uninitialized(ex))
2302 uninitialized = 1;
2303 else
2304 uninitialized = 0;
2305
Mingming553f9002009-09-18 13:34:55 -04002306 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2307 uninitialized, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002308 path[depth].p_ext = ex;
2309
2310 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002311 b = ex_ee_block+ex_ee_len - 1 < end ?
2312 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002313
2314 ext_debug(" border %u:%u\n", a, b);
2315
Allison Hendersond583fb82011-05-25 07:41:43 -04002316 /* If this extent is beyond the end of the hole, skip it */
2317 if (end <= ex_ee_block) {
2318 ex--;
2319 ex_ee_block = le32_to_cpu(ex->ee_block);
2320 ex_ee_len = ext4_ext_get_actual_len(ex);
2321 continue;
2322 } else if (a != ex_ee_block &&
2323 b != ex_ee_block + ex_ee_len - 1) {
2324 /*
2325 * If this is a truncate, then this condition should
2326 * never happen because at least one of the end points
2327 * needs to be on the edge of the extent.
2328 */
Lukas Czernerf17722f2011-06-06 00:05:17 -04002329 if (end == EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002330 ext_debug(" bad truncate %u:%u\n",
2331 start, end);
2332 block = 0;
2333 num = 0;
2334 err = -EIO;
2335 goto out;
2336 }
2337 /*
2338 * else this is a hole punch, so the extent needs to
2339 * be split since neither edge of the hole is on the
2340 * extent edge
2341 */
2342 else{
2343 map.m_pblk = ext4_ext_pblock(ex);
2344 map.m_lblk = ex_ee_block;
2345 map.m_len = b - ex_ee_block;
2346
2347 err = ext4_split_extent(handle,
2348 inode, path, &map, 0,
2349 EXT4_GET_BLOCKS_PUNCH_OUT_EXT |
2350 EXT4_GET_BLOCKS_PRE_IO);
2351
2352 if (err < 0)
2353 goto out;
2354
2355 ex_ee_len = ext4_ext_get_actual_len(ex);
2356
2357 b = ex_ee_block+ex_ee_len - 1 < end ?
2358 ex_ee_block+ex_ee_len - 1 : end;
2359
2360 /* Then remove tail of this extent */
2361 block = ex_ee_block;
2362 num = a - block;
2363 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002364 } else if (a != ex_ee_block) {
2365 /* remove tail of the extent */
2366 block = ex_ee_block;
2367 num = a - block;
2368 } else if (b != ex_ee_block + ex_ee_len - 1) {
2369 /* remove head of the extent */
Allison Hendersond583fb82011-05-25 07:41:43 -04002370 block = b;
2371 num = ex_ee_block + ex_ee_len - b;
2372
2373 /*
2374 * If this is a truncate, this condition
2375 * should never happen
2376 */
Lukas Czernerf17722f2011-06-06 00:05:17 -04002377 if (end == EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002378 ext_debug(" bad truncate %u:%u\n",
2379 start, end);
2380 err = -EIO;
2381 goto out;
2382 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002383 } else {
2384 /* remove whole extent: excellent! */
2385 block = ex_ee_block;
2386 num = 0;
Allison Hendersond583fb82011-05-25 07:41:43 -04002387 if (a != ex_ee_block) {
2388 ext_debug(" bad truncate %u:%u\n",
2389 start, end);
2390 err = -EIO;
2391 goto out;
2392 }
2393
2394 if (b != ex_ee_block + ex_ee_len - 1) {
2395 ext_debug(" bad truncate %u:%u\n",
2396 start, end);
2397 err = -EIO;
2398 goto out;
2399 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002400 }
2401
Theodore Ts'o34071da2008-08-01 21:59:19 -04002402 /*
2403 * 3 for leaf, sb, and inode plus 2 (bmap and group
2404 * descriptor) for each block group; assume two block
2405 * groups plus ex_ee_len/blocks_per_block_group for
2406 * the worst case
2407 */
2408 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002409 if (ex == EXT_FIRST_EXTENT(eh)) {
2410 correct_index = 1;
2411 credits += (ext_depth(inode)) + 1;
2412 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002413 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002414
Jan Kara487caee2009-08-17 22:17:20 -04002415 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
Shen Feng9102e4f2008-07-11 19:27:31 -04002416 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002417 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002418
2419 err = ext4_ext_get_access(handle, inode, path + depth);
2420 if (err)
2421 goto out;
2422
2423 err = ext4_remove_blocks(handle, inode, ex, a, b);
2424 if (err)
2425 goto out;
2426
2427 if (num == 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002428 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002429 ext4_ext_store_pblock(ex, 0);
Allison Hendersond583fb82011-05-25 07:41:43 -04002430 } else if (block != ex_ee_block) {
2431 /*
2432 * If this was a head removal, then we need to update
2433 * the physical block since it is now at a different
2434 * location
2435 */
2436 ext4_ext_store_pblock(ex, ext4_ext_pblock(ex) + (b-a));
Alex Tomasa86c6182006-10-11 01:21:03 -07002437 }
2438
2439 ex->ee_block = cpu_to_le32(block);
2440 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002441 /*
2442 * Do not mark uninitialized if all the blocks in the
2443 * extent have been removed.
2444 */
2445 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002446 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002447
2448 err = ext4_ext_dirty(handle, inode, path + depth);
2449 if (err)
2450 goto out;
2451
Allison Hendersond583fb82011-05-25 07:41:43 -04002452 /*
2453 * If the extent was completely released,
2454 * we need to remove it from the leaf
2455 */
2456 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002457 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002458 /*
2459 * For hole punching, we need to scoot all the
2460 * extents up when an extent is removed so that
2461 * we dont have blank extents in the middle
2462 */
2463 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2464 sizeof(struct ext4_extent));
2465
2466 /* Now get rid of the one at the end */
2467 memset(EXT_LAST_EXTENT(eh), 0,
2468 sizeof(struct ext4_extent));
2469 }
2470 le16_add_cpu(&eh->eh_entries, -1);
2471 }
2472
Mingming Cao2ae02102006-10-11 01:21:11 -07002473 ext_debug("new extent: %u:%u:%llu\n", block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002474 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002475 ex--;
2476 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002477 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002478 }
2479
2480 if (correct_index && eh->eh_entries)
2481 err = ext4_ext_correct_indexes(handle, inode, path);
2482
2483 /* if this leaf is free, then we should
2484 * remove it from index block above */
2485 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2486 err = ext4_ext_rm_idx(handle, inode, path + depth);
2487
2488out:
2489 return err;
2490}
2491
2492/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002493 * ext4_ext_more_to_rm:
2494 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002495 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002496static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002497ext4_ext_more_to_rm(struct ext4_ext_path *path)
2498{
2499 BUG_ON(path->p_idx == NULL);
2500
2501 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2502 return 0;
2503
2504 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002505 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002506 * so we have to consider current index for truncation
2507 */
2508 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2509 return 0;
2510 return 1;
2511}
2512
Allison Hendersonc6a03712011-07-17 23:21:03 -04002513static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07002514{
2515 struct super_block *sb = inode->i_sb;
2516 int depth = ext_depth(inode);
2517 struct ext4_ext_path *path;
2518 handle_t *handle;
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002519 int i, err;
Alex Tomasa86c6182006-10-11 01:21:03 -07002520
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002521 ext_debug("truncate since %u\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002522
2523 /* probably first extent we're gonna free will be last in block */
2524 handle = ext4_journal_start(inode, depth + 1);
2525 if (IS_ERR(handle))
2526 return PTR_ERR(handle);
2527
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002528again:
Alex Tomasa86c6182006-10-11 01:21:03 -07002529 ext4_ext_invalidate_cache(inode);
2530
2531 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002532 * We start scanning from right side, freeing all the blocks
2533 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002534 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002535 depth = ext_depth(inode);
Josef Bacik216553c2008-04-29 22:02:02 -04002536 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07002537 if (path == NULL) {
2538 ext4_journal_stop(handle);
2539 return -ENOMEM;
2540 }
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002541 path[0].p_depth = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -07002542 path[0].p_hdr = ext_inode_hdr(inode);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002543 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002544 err = -EIO;
2545 goto out;
2546 }
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002547 i = err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002548
2549 while (i >= 0 && err == 0) {
2550 if (i == depth) {
2551 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04002552 err = ext4_ext_rm_leaf(handle, inode, path,
Allison Hendersonc6a03712011-07-17 23:21:03 -04002553 start, EXT_MAX_BLOCKS - 1);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002554 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002555 brelse(path[i].p_bh);
2556 path[i].p_bh = NULL;
2557 i--;
2558 continue;
2559 }
2560
2561 /* this is index block */
2562 if (!path[i].p_hdr) {
2563 ext_debug("initialize header\n");
2564 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002565 }
2566
Alex Tomasa86c6182006-10-11 01:21:03 -07002567 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002568 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002569 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2570 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2571 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2572 path[i].p_hdr,
2573 le16_to_cpu(path[i].p_hdr->eh_entries));
2574 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002575 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002576 path[i].p_idx--;
2577 }
2578
2579 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2580 i, EXT_FIRST_INDEX(path[i].p_hdr),
2581 path[i].p_idx);
2582 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002583 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002584 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002585 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04002586 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002587 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'obf89d162010-10-27 21:30:14 -04002588 bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002589 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002590 /* should we reset i_size? */
2591 err = -EIO;
2592 break;
2593 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002594 if (WARN_ON(i + 1 > depth)) {
2595 err = -EIO;
2596 break;
2597 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002598 if (ext4_ext_check(inode, ext_block_hdr(bh),
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002599 depth - i - 1)) {
2600 err = -EIO;
2601 break;
2602 }
2603 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002604
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002605 /* save actual number of indexes since this
2606 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002607 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2608 i++;
2609 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002610 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002611 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002612 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002613 * handle must be already prepared by the
2614 * truncatei_leaf() */
2615 err = ext4_ext_rm_idx(handle, inode, path + i);
2616 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002617 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002618 brelse(path[i].p_bh);
2619 path[i].p_bh = NULL;
2620 i--;
2621 ext_debug("return to level %d\n", i);
2622 }
2623 }
2624
2625 /* TODO: flexible tree reduction should be here */
2626 if (path->p_hdr->eh_entries == 0) {
2627 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002628 * truncate to zero freed all the tree,
2629 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002630 */
2631 err = ext4_ext_get_access(handle, inode, path);
2632 if (err == 0) {
2633 ext_inode_hdr(inode)->eh_depth = 0;
2634 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04002635 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07002636 err = ext4_ext_dirty(handle, inode, path);
2637 }
2638 }
2639out:
Alex Tomasa86c6182006-10-11 01:21:03 -07002640 ext4_ext_drop_refs(path);
2641 kfree(path);
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002642 if (err == -EAGAIN)
2643 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07002644 ext4_journal_stop(handle);
2645
2646 return err;
2647}
2648
2649/*
2650 * called at mount time
2651 */
2652void ext4_ext_init(struct super_block *sb)
2653{
2654 /*
2655 * possible initialization would be here
2656 */
2657
Theodore Ts'o83982b62009-01-06 14:53:16 -05002658 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04002659#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002660 printk(KERN_INFO "EXT4-fs: file extents enabled");
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002661#ifdef AGGRESSIVE_TEST
2662 printk(", aggressive tests");
Alex Tomasa86c6182006-10-11 01:21:03 -07002663#endif
2664#ifdef CHECK_BINSEARCH
2665 printk(", check binsearch");
2666#endif
2667#ifdef EXTENTS_STATS
2668 printk(", stats");
2669#endif
2670 printk("\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04002671#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07002672#ifdef EXTENTS_STATS
2673 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2674 EXT4_SB(sb)->s_ext_min = 1 << 30;
2675 EXT4_SB(sb)->s_ext_max = 0;
2676#endif
2677 }
2678}
2679
2680/*
2681 * called at umount time
2682 */
2683void ext4_ext_release(struct super_block *sb)
2684{
Theodore Ts'o83982b62009-01-06 14:53:16 -05002685 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
Alex Tomasa86c6182006-10-11 01:21:03 -07002686 return;
2687
2688#ifdef EXTENTS_STATS
2689 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2690 struct ext4_sb_info *sbi = EXT4_SB(sb);
2691 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2692 sbi->s_ext_blocks, sbi->s_ext_extents,
2693 sbi->s_ext_blocks / sbi->s_ext_extents);
2694 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2695 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2696 }
2697#endif
2698}
2699
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002700/* FIXME!! we need to try to merge to left or right after zero-out */
2701static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2702{
Lukas Czerner24075182010-10-27 21:30:06 -04002703 ext4_fsblk_t ee_pblock;
2704 unsigned int ee_len;
Jing Zhangb7203032010-05-12 00:00:00 -04002705 int ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002706
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002707 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04002708 ee_pblock = ext4_ext_pblock(ex);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002709
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04002710 ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
Lukas Czerner24075182010-10-27 21:30:06 -04002711 if (ret > 0)
2712 ret = 0;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002713
Lukas Czerner24075182010-10-27 21:30:06 -04002714 return ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002715}
2716
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002717/*
2718 * used by extent splitting.
2719 */
2720#define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
2721 due to ENOSPC */
2722#define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */
2723#define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */
2724
2725/*
2726 * ext4_split_extent_at() splits an extent at given block.
2727 *
2728 * @handle: the journal handle
2729 * @inode: the file inode
2730 * @path: the path to the extent
2731 * @split: the logical block where the extent is splitted.
2732 * @split_flags: indicates if the extent could be zeroout if split fails, and
2733 * the states(init or uninit) of new extents.
2734 * @flags: flags used to insert new extent to extent tree.
2735 *
2736 *
2737 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
2738 * of which are deterimined by split_flag.
2739 *
2740 * There are two cases:
2741 * a> the extent are splitted into two extent.
2742 * b> split is not needed, and just mark the extent.
2743 *
2744 * return 0 on success.
2745 */
2746static int ext4_split_extent_at(handle_t *handle,
2747 struct inode *inode,
2748 struct ext4_ext_path *path,
2749 ext4_lblk_t split,
2750 int split_flag,
2751 int flags)
2752{
2753 ext4_fsblk_t newblock;
2754 ext4_lblk_t ee_block;
2755 struct ext4_extent *ex, newex, orig_ex;
2756 struct ext4_extent *ex2 = NULL;
2757 unsigned int ee_len, depth;
2758 int err = 0;
2759
2760 ext_debug("ext4_split_extents_at: inode %lu, logical"
2761 "block %llu\n", inode->i_ino, (unsigned long long)split);
2762
2763 ext4_ext_show_leaf(inode, path);
2764
2765 depth = ext_depth(inode);
2766 ex = path[depth].p_ext;
2767 ee_block = le32_to_cpu(ex->ee_block);
2768 ee_len = ext4_ext_get_actual_len(ex);
2769 newblock = split - ee_block + ext4_ext_pblock(ex);
2770
2771 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
2772
2773 err = ext4_ext_get_access(handle, inode, path + depth);
2774 if (err)
2775 goto out;
2776
2777 if (split == ee_block) {
2778 /*
2779 * case b: block @split is the block that the extent begins with
2780 * then we just change the state of the extent, and splitting
2781 * is not needed.
2782 */
2783 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2784 ext4_ext_mark_uninitialized(ex);
2785 else
2786 ext4_ext_mark_initialized(ex);
2787
2788 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
2789 ext4_ext_try_to_merge(inode, path, ex);
2790
2791 err = ext4_ext_dirty(handle, inode, path + depth);
2792 goto out;
2793 }
2794
2795 /* case a */
2796 memcpy(&orig_ex, ex, sizeof(orig_ex));
2797 ex->ee_len = cpu_to_le16(split - ee_block);
2798 if (split_flag & EXT4_EXT_MARK_UNINIT1)
2799 ext4_ext_mark_uninitialized(ex);
2800
2801 /*
2802 * path may lead to new leaf, not to original leaf any more
2803 * after ext4_ext_insert_extent() returns,
2804 */
2805 err = ext4_ext_dirty(handle, inode, path + depth);
2806 if (err)
2807 goto fix_extent_len;
2808
2809 ex2 = &newex;
2810 ex2->ee_block = cpu_to_le32(split);
2811 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
2812 ext4_ext_store_pblock(ex2, newblock);
2813 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2814 ext4_ext_mark_uninitialized(ex2);
2815
2816 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
2817 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2818 err = ext4_ext_zeroout(inode, &orig_ex);
2819 if (err)
2820 goto fix_extent_len;
2821 /* update the extent length and mark as initialized */
2822 ex->ee_len = cpu_to_le32(ee_len);
2823 ext4_ext_try_to_merge(inode, path, ex);
2824 err = ext4_ext_dirty(handle, inode, path + depth);
2825 goto out;
2826 } else if (err)
2827 goto fix_extent_len;
2828
2829out:
2830 ext4_ext_show_leaf(inode, path);
2831 return err;
2832
2833fix_extent_len:
2834 ex->ee_len = orig_ex.ee_len;
2835 ext4_ext_dirty(handle, inode, path + depth);
2836 return err;
2837}
2838
2839/*
2840 * ext4_split_extents() splits an extent and mark extent which is covered
2841 * by @map as split_flags indicates
2842 *
2843 * It may result in splitting the extent into multiple extents (upto three)
2844 * There are three possibilities:
2845 * a> There is no split required
2846 * b> Splits in two extents: Split is happening at either end of the extent
2847 * c> Splits in three extents: Somone is splitting in middle of the extent
2848 *
2849 */
2850static int ext4_split_extent(handle_t *handle,
2851 struct inode *inode,
2852 struct ext4_ext_path *path,
2853 struct ext4_map_blocks *map,
2854 int split_flag,
2855 int flags)
2856{
2857 ext4_lblk_t ee_block;
2858 struct ext4_extent *ex;
2859 unsigned int ee_len, depth;
2860 int err = 0;
2861 int uninitialized;
2862 int split_flag1, flags1;
2863
2864 depth = ext_depth(inode);
2865 ex = path[depth].p_ext;
2866 ee_block = le32_to_cpu(ex->ee_block);
2867 ee_len = ext4_ext_get_actual_len(ex);
2868 uninitialized = ext4_ext_is_uninitialized(ex);
2869
2870 if (map->m_lblk + map->m_len < ee_block + ee_len) {
2871 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2872 EXT4_EXT_MAY_ZEROOUT : 0;
2873 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
2874 if (uninitialized)
2875 split_flag1 |= EXT4_EXT_MARK_UNINIT1 |
2876 EXT4_EXT_MARK_UNINIT2;
2877 err = ext4_split_extent_at(handle, inode, path,
2878 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04002879 if (err)
2880 goto out;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002881 }
2882
2883 ext4_ext_drop_refs(path);
2884 path = ext4_ext_find_extent(inode, map->m_lblk, path);
2885 if (IS_ERR(path))
2886 return PTR_ERR(path);
2887
2888 if (map->m_lblk >= ee_block) {
2889 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2890 EXT4_EXT_MAY_ZEROOUT : 0;
2891 if (uninitialized)
2892 split_flag1 |= EXT4_EXT_MARK_UNINIT1;
2893 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2894 split_flag1 |= EXT4_EXT_MARK_UNINIT2;
2895 err = ext4_split_extent_at(handle, inode, path,
2896 map->m_lblk, split_flag1, flags);
2897 if (err)
2898 goto out;
2899 }
2900
2901 ext4_ext_show_leaf(inode, path);
2902out:
2903 return err ? err : map->m_len;
2904}
2905
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002906#define EXT4_EXT_ZERO_LEN 7
Amit Arora56055d32007-07-17 21:42:38 -04002907/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002908 * This function is called by ext4_ext_map_blocks() if someone tries to write
Amit Arora56055d32007-07-17 21:42:38 -04002909 * to an uninitialized extent. It may result in splitting the uninitialized
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002910 * extent into multiple extents (up to three - one initialized and two
Amit Arora56055d32007-07-17 21:42:38 -04002911 * uninitialized).
2912 * There are three possibilities:
2913 * a> There is no split required: Entire extent should be initialized
2914 * b> Splits in two extents: Write is happening at either end of the extent
2915 * c> Splits in three extents: Somone is writing in middle of the extent
2916 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002917static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002918 struct inode *inode,
2919 struct ext4_map_blocks *map,
2920 struct ext4_ext_path *path)
Amit Arora56055d32007-07-17 21:42:38 -04002921{
Yongqiang Yang667eff32011-05-03 12:25:07 -04002922 struct ext4_map_blocks split_map;
2923 struct ext4_extent zero_ex;
2924 struct ext4_extent *ex;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002925 ext4_lblk_t ee_block, eof_block;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002926 unsigned int allocated, ee_len, depth;
Amit Arora56055d32007-07-17 21:42:38 -04002927 int err = 0;
Yongqiang Yang667eff32011-05-03 12:25:07 -04002928 int split_flag = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002929
2930 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
2931 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002932 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002933
2934 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
2935 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002936 if (eof_block < map->m_lblk + map->m_len)
2937 eof_block = map->m_lblk + map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04002938
2939 depth = ext_depth(inode);
Amit Arora56055d32007-07-17 21:42:38 -04002940 ex = path[depth].p_ext;
2941 ee_block = le32_to_cpu(ex->ee_block);
2942 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002943 allocated = ee_len - (map->m_lblk - ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002944
Yongqiang Yang667eff32011-05-03 12:25:07 -04002945 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002946 /*
2947 * It is safe to convert extent to initialized via explicit
2948 * zeroout only if extent is fully insde i_size or new_size.
2949 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04002950 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002951
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002952 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
Yongqiang Yang667eff32011-05-03 12:25:07 -04002953 if (ee_len <= 2*EXT4_EXT_ZERO_LEN &&
2954 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2955 err = ext4_ext_zeroout(inode, ex);
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002956 if (err)
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002957 goto out;
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002958
2959 err = ext4_ext_get_access(handle, inode, path + depth);
2960 if (err)
2961 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04002962 ext4_ext_mark_initialized(ex);
2963 ext4_ext_try_to_merge(inode, path, ex);
2964 err = ext4_ext_dirty(handle, inode, path + depth);
2965 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04002966 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04002967
Amit Arora56055d32007-07-17 21:42:38 -04002968 /*
Yongqiang Yang667eff32011-05-03 12:25:07 -04002969 * four cases:
2970 * 1. split the extent into three extents.
2971 * 2. split the extent into two extents, zeroout the first half.
2972 * 3. split the extent into two extents, zeroout the second half.
2973 * 4. split the extent into two extents with out zeroout.
Amit Arora56055d32007-07-17 21:42:38 -04002974 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04002975 split_map.m_lblk = map->m_lblk;
2976 split_map.m_len = map->m_len;
2977
2978 if (allocated > map->m_len) {
2979 if (allocated <= EXT4_EXT_ZERO_LEN &&
2980 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2981 /* case 3 */
2982 zero_ex.ee_block =
Allison Henderson9b940f82011-05-16 10:11:09 -04002983 cpu_to_le32(map->m_lblk);
2984 zero_ex.ee_len = cpu_to_le16(allocated);
Yongqiang Yang667eff32011-05-03 12:25:07 -04002985 ext4_ext_store_pblock(&zero_ex,
2986 ext4_ext_pblock(ex) + map->m_lblk - ee_block);
2987 err = ext4_ext_zeroout(inode, &zero_ex);
Amit Arora56055d32007-07-17 21:42:38 -04002988 if (err)
2989 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04002990 split_map.m_lblk = map->m_lblk;
2991 split_map.m_len = allocated;
2992 } else if ((map->m_lblk - ee_block + map->m_len <
2993 EXT4_EXT_ZERO_LEN) &&
2994 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2995 /* case 2 */
2996 if (map->m_lblk != ee_block) {
2997 zero_ex.ee_block = ex->ee_block;
2998 zero_ex.ee_len = cpu_to_le16(map->m_lblk -
2999 ee_block);
3000 ext4_ext_store_pblock(&zero_ex,
3001 ext4_ext_pblock(ex));
3002 err = ext4_ext_zeroout(inode, &zero_ex);
3003 if (err)
3004 goto out;
3005 }
3006
Yongqiang Yang667eff32011-05-03 12:25:07 -04003007 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003008 split_map.m_len = map->m_lblk - ee_block + map->m_len;
3009 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003010 }
3011 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003012
3013 allocated = ext4_split_extent(handle, inode, path,
3014 &split_map, split_flag, 0);
3015 if (allocated < 0)
3016 err = allocated;
3017
Amit Arora56055d32007-07-17 21:42:38 -04003018out:
3019 return err ? err : allocated;
3020}
3021
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003022/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003023 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003024 * ext4_get_blocks_dio_write() when DIO to write
3025 * to an uninitialized extent.
3026 *
Paul Bollefd018fe2011-02-15 00:05:43 +01003027 * Writing to an uninitialized extent may result in splitting the uninitialized
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003028 * extent into multiple /initialized uninitialized extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003029 * There are three possibilities:
3030 * a> There is no split required: Entire extent should be uninitialized
3031 * b> Splits in two extents: Write is happening at either end of the extent
3032 * c> Splits in three extents: Somone is writing in middle of the extent
3033 *
3034 * One of more index blocks maybe needed if the extent tree grow after
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003035 * the uninitialized extent split. To prevent ENOSPC occur at the IO
Mingming Cao00314622009-09-28 15:49:08 -04003036 * complete, we need to split the uninitialized extent before DIO submit
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02003037 * the IO. The uninitialized extent called at this time will be split
Mingming Cao00314622009-09-28 15:49:08 -04003038 * into three uninitialized extent(at most). After IO complete, the part
3039 * being filled will be convert to initialized by the end_io callback function
3040 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003041 *
3042 * Returns the size of uninitialized extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003043 */
3044static int ext4_split_unwritten_extents(handle_t *handle,
3045 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003046 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003047 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04003048 int flags)
3049{
Yongqiang Yang667eff32011-05-03 12:25:07 -04003050 ext4_lblk_t eof_block;
3051 ext4_lblk_t ee_block;
3052 struct ext4_extent *ex;
3053 unsigned int ee_len;
3054 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003055
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003056 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
3057 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003058 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003059
3060 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3061 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003062 if (eof_block < map->m_lblk + map->m_len)
3063 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003064 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003065 * It is safe to convert extent to initialized via explicit
3066 * zeroout only if extent is fully insde i_size or new_size.
3067 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003068 depth = ext_depth(inode);
3069 ex = path[depth].p_ext;
3070 ee_block = le32_to_cpu(ex->ee_block);
3071 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003072
Yongqiang Yang667eff32011-05-03 12:25:07 -04003073 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
3074 split_flag |= EXT4_EXT_MARK_UNINIT2;
Mingming Cao00314622009-09-28 15:49:08 -04003075
Yongqiang Yang667eff32011-05-03 12:25:07 -04003076 flags |= EXT4_GET_BLOCKS_PRE_IO;
3077 return ext4_split_extent(handle, inode, path, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003078}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003079
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003080static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003081 struct inode *inode,
3082 struct ext4_ext_path *path)
3083{
3084 struct ext4_extent *ex;
Mingming Cao00314622009-09-28 15:49:08 -04003085 int depth;
3086 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003087
3088 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003089 ex = path[depth].p_ext;
3090
Yongqiang Yang197217a2011-05-03 11:45:29 -04003091 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3092 "block %llu, max_blocks %u\n", inode->i_ino,
3093 (unsigned long long)le32_to_cpu(ex->ee_block),
3094 ext4_ext_get_actual_len(ex));
3095
Mingming Cao00314622009-09-28 15:49:08 -04003096 err = ext4_ext_get_access(handle, inode, path + depth);
3097 if (err)
3098 goto out;
3099 /* first mark the extent as initialized */
3100 ext4_ext_mark_initialized(ex);
3101
Yongqiang Yang197217a2011-05-03 11:45:29 -04003102 /* note: ext4_ext_correct_indexes() isn't needed here because
3103 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003104 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04003105 ext4_ext_try_to_merge(inode, path, ex);
3106
Mingming Cao00314622009-09-28 15:49:08 -04003107 /* Mark modified extent as dirty */
3108 err = ext4_ext_dirty(handle, inode, path + depth);
3109out:
3110 ext4_ext_show_leaf(inode, path);
3111 return err;
3112}
3113
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003114static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3115 sector_t block, int count)
3116{
3117 int i;
3118 for (i = 0; i < count; i++)
3119 unmap_underlying_metadata(bdev, block + i);
3120}
3121
Theodore Ts'o58590b02010-10-27 21:23:12 -04003122/*
3123 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3124 */
3125static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
Eric Sandeend002ebf2011-01-10 13:03:35 -05003126 ext4_lblk_t lblk,
Theodore Ts'o58590b02010-10-27 21:23:12 -04003127 struct ext4_ext_path *path,
3128 unsigned int len)
3129{
3130 int i, depth;
3131 struct ext4_extent_header *eh;
Sergey Senozhatsky65922cb2011-03-23 14:08:27 -04003132 struct ext4_extent *last_ex;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003133
3134 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3135 return 0;
3136
3137 depth = ext_depth(inode);
3138 eh = path[depth].p_hdr;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003139
3140 if (unlikely(!eh->eh_entries)) {
3141 EXT4_ERROR_INODE(inode, "eh->eh_entries == 0 and "
3142 "EOFBLOCKS_FL set");
3143 return -EIO;
3144 }
3145 last_ex = EXT_LAST_EXTENT(eh);
3146 /*
3147 * We should clear the EOFBLOCKS_FL flag if we are writing the
3148 * last block in the last extent in the file. We test this by
3149 * first checking to see if the caller to
3150 * ext4_ext_get_blocks() was interested in the last block (or
3151 * a block beyond the last block) in the current extent. If
3152 * this turns out to be false, we can bail out from this
3153 * function immediately.
3154 */
Eric Sandeend002ebf2011-01-10 13:03:35 -05003155 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
Theodore Ts'o58590b02010-10-27 21:23:12 -04003156 ext4_ext_get_actual_len(last_ex))
3157 return 0;
3158 /*
3159 * If the caller does appear to be planning to write at or
3160 * beyond the end of the current extent, we then test to see
3161 * if the current extent is the last extent in the file, by
3162 * checking to make sure it was reached via the rightmost node
3163 * at each level of the tree.
3164 */
3165 for (i = depth-1; i >= 0; i--)
3166 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3167 return 0;
3168 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3169 return ext4_mark_inode_dirty(handle, inode);
3170}
3171
Mingming Cao00314622009-09-28 15:49:08 -04003172static int
3173ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003174 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003175 struct ext4_ext_path *path, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003176 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003177{
3178 int ret = 0;
3179 int err = 0;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003180 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Mingming Cao00314622009-09-28 15:49:08 -04003181
3182 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3183 "block %llu, max_blocks %u, flags %d, allocated %u",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003184 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04003185 flags, allocated);
3186 ext4_ext_show_leaf(inode, path);
3187
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003188 /* get_block() before submit the IO, split the extent */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003189 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003190 ret = ext4_split_unwritten_extents(handle, inode, map,
3191 path, flags);
Mingming5f524952009-11-10 10:48:04 -05003192 /*
3193 * Flag the inode(non aio case) or end_io struct (aio case)
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003194 * that this IO needs to conversion to written when IO is
Mingming5f524952009-11-10 10:48:04 -05003195 * completed
3196 */
Eric Sandeene9e3bce2011-02-12 08:17:34 -05003197 if (io && !(io->flag & EXT4_IO_END_UNWRITTEN)) {
Theodore Ts'obd2d0212010-10-27 21:30:10 -04003198 io->flag = EXT4_IO_END_UNWRITTEN;
Eric Sandeene9e3bce2011-02-12 08:17:34 -05003199 atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten);
3200 } else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003201 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
Jiaying Zhang744692d2010-03-04 16:14:02 -05003202 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003203 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao00314622009-09-28 15:49:08 -04003204 goto out;
3205 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003206 /* IO end_io complete, convert the filled extent to written */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003207 if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003208 ret = ext4_convert_unwritten_extents_endio(handle, inode,
Mingming Cao00314622009-09-28 15:49:08 -04003209 path);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003210 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05003211 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05003212 err = check_eofblocks_fl(handle, inode, map->m_lblk,
3213 path, map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003214 } else
3215 err = ret;
Mingming Cao00314622009-09-28 15:49:08 -04003216 goto out2;
3217 }
3218 /* buffered IO case */
3219 /*
3220 * repeat fallocate creation request
3221 * we already have an unwritten extent
3222 */
3223 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3224 goto map_out;
3225
3226 /* buffered READ or buffered write_begin() lookup */
3227 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3228 /*
3229 * We have blocks reserved already. We
3230 * return allocated blocks so that delalloc
3231 * won't do block reservation for us. But
3232 * the buffer head will be unmapped so that
3233 * a read from the block returns 0s.
3234 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003235 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003236 goto out1;
3237 }
3238
3239 /* buffered write, writepage time, convert*/
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003240 ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003241 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05003242 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05003243 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
3244 map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003245 if (err < 0)
3246 goto out2;
3247 }
3248
Mingming Cao00314622009-09-28 15:49:08 -04003249out:
3250 if (ret <= 0) {
3251 err = ret;
3252 goto out2;
3253 } else
3254 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003255 map->m_flags |= EXT4_MAP_NEW;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003256 /*
3257 * if we allocated more blocks than requested
3258 * we need to make sure we unmap the extra block
3259 * allocated. The actual needed block will get
3260 * unmapped later when we find the buffer_head marked
3261 * new.
3262 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003263 if (allocated > map->m_len) {
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003264 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003265 newblock + map->m_len,
3266 allocated - map->m_len);
3267 allocated = map->m_len;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003268 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003269
3270 /*
3271 * If we have done fallocate with the offset that is already
3272 * delayed allocated, we would have block reservation
3273 * and quota reservation done in the delayed write path.
3274 * But fallocate would have already updated quota and block
3275 * count for this offset. So cancel these reservation
3276 */
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05003277 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003278 ext4_da_update_reserve_space(inode, allocated, 0);
3279
Mingming Cao00314622009-09-28 15:49:08 -04003280map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003281 map->m_flags |= EXT4_MAP_MAPPED;
Mingming Cao00314622009-09-28 15:49:08 -04003282out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003283 if (allocated > map->m_len)
3284 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003285 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003286 map->m_pblk = newblock;
3287 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04003288out2:
3289 if (path) {
3290 ext4_ext_drop_refs(path);
3291 kfree(path);
3292 }
3293 return err ? err : allocated;
3294}
Theodore Ts'o58590b02010-10-27 21:23:12 -04003295
Mingming Cao00314622009-09-28 15:49:08 -04003296/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05003297 * Block allocation/map/preallocation routine for extents based files
3298 *
3299 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003300 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003301 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3302 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05003303 *
3304 * return > 0, number of of blocks already mapped/allocated
3305 * if create == 0 and these are pre-allocated blocks
3306 * buffer head is unmapped
3307 * otherwise blocks are mapped
3308 *
3309 * return = 0, if plain look up failed (blocks have not been allocated)
3310 * buffer head is unmapped
3311 *
3312 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003313 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003314int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3315 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07003316{
3317 struct ext4_ext_path *path = NULL;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003318 struct ext4_extent newex, *ex;
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003319 ext4_fsblk_t newblock = 0;
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003320 int err = 0, depth, ret;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003321 unsigned int allocated = 0;
Allison Hendersone8613042011-05-25 07:41:46 -04003322 unsigned int punched_out = 0;
3323 unsigned int result = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003324 struct ext4_allocation_request ar;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003325 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Allison Hendersone8613042011-05-25 07:41:46 -04003326 struct ext4_map_blocks punch_map;
Alex Tomasa86c6182006-10-11 01:21:03 -07003327
Mingming84fe3be2009-09-01 08:44:37 -04003328 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003329 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003330 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07003331
3332 /* check in cache */
Robin Dong015861b2011-07-17 23:27:43 -04003333 if (!(flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) &&
3334 ext4_ext_in_cache(inode, map->m_lblk, &newex)) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003335 if (!newex.ee_start_lo && !newex.ee_start_hi) {
Theodore Ts'oc2177052009-05-14 00:58:52 -04003336 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003337 /*
3338 * block isn't allocated yet and
3339 * user doesn't want to allocate it
3340 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003341 goto out2;
3342 }
3343 /* we should allocate requested block */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003344 } else {
Alex Tomasa86c6182006-10-11 01:21:03 -07003345 /* block is already allocated */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003346 newblock = map->m_lblk
Dave Kleikamp8c55e202007-05-24 13:04:54 -04003347 - le32_to_cpu(newex.ee_block)
Theodore Ts'obf89d162010-10-27 21:30:14 -04003348 + ext4_ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003349 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003350 allocated = ext4_ext_get_actual_len(&newex) -
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003351 (map->m_lblk - le32_to_cpu(newex.ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -07003352 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07003353 }
3354 }
3355
3356 /* find extent for this block */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003357 path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07003358 if (IS_ERR(path)) {
3359 err = PTR_ERR(path);
3360 path = NULL;
3361 goto out2;
3362 }
3363
3364 depth = ext_depth(inode);
3365
3366 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003367 * consistent leaf must not be empty;
3368 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07003369 * this is why assert can't be put in ext4_ext_find_extent()
3370 */
Frank Mayhar273df552010-03-02 11:46:09 -05003371 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
3372 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04003373 "lblock: %lu, depth: %d pblock %lld",
3374 (unsigned long) map->m_lblk, depth,
3375 path[depth].p_block);
Surbhi Palande034fb4c2009-12-14 09:53:52 -05003376 err = -EIO;
3377 goto out2;
3378 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003379
Avantika Mathur7e028972006-12-06 20:41:33 -08003380 ex = path[depth].p_ext;
3381 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003382 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003383 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003384 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003385
3386 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003387 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04003388 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003389 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003390 ee_len = ext4_ext_get_actual_len(ex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003391 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003392 if (in_range(map->m_lblk, ee_block, ee_len)) {
3393 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003394 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003395 allocated = ee_len - (map->m_lblk - ee_block);
3396 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
3397 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04003398
Allison Hendersone8613042011-05-25 07:41:46 -04003399 if ((flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) == 0) {
3400 /*
3401 * Do not put uninitialized extent
3402 * in the cache
3403 */
3404 if (!ext4_ext_is_uninitialized(ex)) {
3405 ext4_ext_put_in_cache(inode, ee_block,
3406 ee_len, ee_start);
3407 goto out;
3408 }
3409 ret = ext4_ext_handle_uninitialized_extents(
3410 handle, inode, map, path, flags,
3411 allocated, newblock);
3412 return ret;
Amit Arora56055d32007-07-17 21:42:38 -04003413 }
Allison Hendersone8613042011-05-25 07:41:46 -04003414
3415 /*
3416 * Punch out the map length, but only to the
3417 * end of the extent
3418 */
3419 punched_out = allocated < map->m_len ?
3420 allocated : map->m_len;
3421
3422 /*
3423 * Sense extents need to be converted to
3424 * uninitialized, they must fit in an
3425 * uninitialized extent
3426 */
3427 if (punched_out > EXT_UNINIT_MAX_LEN)
3428 punched_out = EXT_UNINIT_MAX_LEN;
3429
3430 punch_map.m_lblk = map->m_lblk;
3431 punch_map.m_pblk = newblock;
3432 punch_map.m_len = punched_out;
3433 punch_map.m_flags = 0;
3434
3435 /* Check to see if the extent needs to be split */
3436 if (punch_map.m_len != ee_len ||
3437 punch_map.m_lblk != ee_block) {
3438
3439 ret = ext4_split_extent(handle, inode,
3440 path, &punch_map, 0,
3441 EXT4_GET_BLOCKS_PUNCH_OUT_EXT |
3442 EXT4_GET_BLOCKS_PRE_IO);
3443
3444 if (ret < 0) {
3445 err = ret;
3446 goto out2;
3447 }
3448 /*
3449 * find extent for the block at
3450 * the start of the hole
3451 */
3452 ext4_ext_drop_refs(path);
3453 kfree(path);
3454
3455 path = ext4_ext_find_extent(inode,
3456 map->m_lblk, NULL);
3457 if (IS_ERR(path)) {
3458 err = PTR_ERR(path);
3459 path = NULL;
3460 goto out2;
3461 }
3462
3463 depth = ext_depth(inode);
3464 ex = path[depth].p_ext;
3465 ee_len = ext4_ext_get_actual_len(ex);
3466 ee_block = le32_to_cpu(ex->ee_block);
3467 ee_start = ext4_ext_pblock(ex);
3468
3469 }
3470
3471 ext4_ext_mark_uninitialized(ex);
3472
Allison Hendersonf7d0d372011-07-17 23:17:02 -04003473 ext4_ext_invalidate_cache(inode);
3474
3475 err = ext4_ext_rm_leaf(handle, inode, path,
3476 map->m_lblk, map->m_lblk + punched_out);
3477
3478 if (!err && path->p_hdr->eh_entries == 0) {
3479 /*
3480 * Punch hole freed all of this sub tree,
3481 * so we need to correct eh_depth
3482 */
3483 err = ext4_ext_get_access(handle, inode, path);
3484 if (err == 0) {
3485 ext_inode_hdr(inode)->eh_depth = 0;
3486 ext_inode_hdr(inode)->eh_max =
3487 cpu_to_le16(ext4_ext_space_root(
3488 inode, 0));
3489
3490 err = ext4_ext_dirty(
3491 handle, inode, path);
3492 }
3493 }
Allison Hendersone8613042011-05-25 07:41:46 -04003494
3495 goto out2;
Alex Tomasa86c6182006-10-11 01:21:03 -07003496 }
3497 }
3498
3499 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003500 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07003501 * we couldn't try to create block if create flag is zero
3502 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04003503 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003504 /*
3505 * put just found gap into cache to speed up
3506 * subsequent requests
3507 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003508 ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
Alex Tomasa86c6182006-10-11 01:21:03 -07003509 goto out2;
3510 }
3511 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003512 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07003513 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003514
Alex Tomasc9de5602008-01-29 00:19:52 -05003515 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003516 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003517 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3518 if (err)
3519 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003520 ar.lright = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003521 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
3522 if (err)
3523 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04003524
Amit Arora749269f2007-07-18 09:02:56 -04003525 /*
3526 * See if request is beyond maximum number of blocks we can have in
3527 * a single extent. For an initialized extent this limit is
3528 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3529 * EXT_UNINIT_MAX_LEN.
3530 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003531 if (map->m_len > EXT_INIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003532 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003533 map->m_len = EXT_INIT_MAX_LEN;
3534 else if (map->m_len > EXT_UNINIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003535 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003536 map->m_len = EXT_UNINIT_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04003537
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003538 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
3539 newex.ee_block = cpu_to_le32(map->m_lblk);
3540 newex.ee_len = cpu_to_le16(map->m_len);
Amit Arora25d14f92007-05-24 13:04:13 -04003541 err = ext4_ext_check_overlap(inode, &newex, path);
3542 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003543 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04003544 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003545 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003546
3547 /* allocate new block */
3548 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003549 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
3550 ar.logical = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003551 ar.len = allocated;
3552 if (S_ISREG(inode->i_mode))
3553 ar.flags = EXT4_MB_HINT_DATA;
3554 else
3555 /* disable in-core preallocation for non-regular files */
3556 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04003557 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
3558 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05003559 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07003560 if (!newblock)
3561 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04003562 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003563 ar.goal, newblock, allocated);
Alex Tomasa86c6182006-10-11 01:21:03 -07003564
3565 /* try to insert new extent into found leaf and return */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07003566 ext4_ext_store_pblock(&newex, newblock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003567 newex.ee_len = cpu_to_le16(ar.len);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003568 /* Mark uninitialized */
3569 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
Amit Aroraa2df2a62007-07-17 21:42:41 -04003570 ext4_ext_mark_uninitialized(&newex);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003571 /*
Jiaying Zhang744692d2010-03-04 16:14:02 -05003572 * io_end structure was created for every IO write to an
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003573 * uninitialized extent. To avoid unnecessary conversion,
Jiaying Zhang744692d2010-03-04 16:14:02 -05003574 * here we flag the IO that really needs the conversion.
Mingming5f524952009-11-10 10:48:04 -05003575 * For non asycn direct IO case, flag the inode state
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003576 * that we need to perform conversion when IO is done.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003577 */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003578 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Eric Sandeene9e3bce2011-02-12 08:17:34 -05003579 if (io && !(io->flag & EXT4_IO_END_UNWRITTEN)) {
Theodore Ts'obd2d0212010-10-27 21:30:10 -04003580 io->flag = EXT4_IO_END_UNWRITTEN;
Eric Sandeene9e3bce2011-02-12 08:17:34 -05003581 atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten);
3582 } else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003583 ext4_set_inode_state(inode,
3584 EXT4_STATE_DIO_UNWRITTEN);
Mingming5f524952009-11-10 10:48:04 -05003585 }
Jiaying Zhang744692d2010-03-04 16:14:02 -05003586 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003587 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003588 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003589
Eric Sandeend002ebf2011-01-10 13:03:35 -05003590 err = check_eofblocks_fl(handle, inode, map->m_lblk, path, ar.len);
Jiaying Zhang575a1d42011-07-10 20:07:25 -04003591 if (!err)
3592 err = ext4_ext_insert_extent(handle, inode, path,
3593 &newex, flags);
Alex Tomas315054f2007-05-24 13:04:25 -04003594 if (err) {
Maxim Patlasov7132de72011-07-10 19:37:48 -04003595 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
3596 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
Alex Tomas315054f2007-05-24 13:04:25 -04003597 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05003598 /* not a good idea to call discard here directly,
3599 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003600 ext4_discard_preallocations(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05003601 ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex),
Maxim Patlasov7132de72011-07-10 19:37:48 -04003602 ext4_ext_get_actual_len(&newex), fb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07003603 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04003604 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003605
Alex Tomasa86c6182006-10-11 01:21:03 -07003606 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04003607 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003608 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003609 if (allocated > map->m_len)
3610 allocated = map->m_len;
3611 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07003612
Jan Karab436b9b2009-12-08 23:51:10 -05003613 /*
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003614 * Update reserved blocks/metadata blocks after successful
3615 * block allocation which had been deferred till now.
3616 */
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05003617 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003618 ext4_da_update_reserve_space(inode, allocated, 1);
3619
3620 /*
Jan Karab436b9b2009-12-08 23:51:10 -05003621 * Cache the extent and update transaction to commit on fdatasync only
3622 * when it is _not_ an uninitialized extent.
3623 */
3624 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003625 ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock);
Jan Karab436b9b2009-12-08 23:51:10 -05003626 ext4_update_inode_fsync_trans(handle, inode, 1);
3627 } else
3628 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07003629out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003630 if (allocated > map->m_len)
3631 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07003632 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003633 map->m_flags |= EXT4_MAP_MAPPED;
3634 map->m_pblk = newblock;
3635 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07003636out2:
3637 if (path) {
3638 ext4_ext_drop_refs(path);
3639 kfree(path);
3640 }
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003641 trace_ext4_ext_map_blocks_exit(inode, map->m_lblk,
3642 newblock, map->m_len, err ? err : allocated);
Allison Hendersone8613042011-05-25 07:41:46 -04003643
3644 result = (flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) ?
3645 punched_out : allocated;
3646
3647 return err ? err : result;
Alex Tomasa86c6182006-10-11 01:21:03 -07003648}
3649
Jan Karacf108bc2008-07-11 19:27:31 -04003650void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07003651{
3652 struct address_space *mapping = inode->i_mapping;
3653 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003654 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07003655 handle_t *handle;
3656 int err = 0;
3657
3658 /*
Jiaying Zhang3889fd52011-01-10 12:47:05 -05003659 * finish any pending end_io work so we won't run the risk of
3660 * converting any truncated blocks to initialized later
3661 */
3662 ext4_flush_completed_IO(inode);
3663
3664 /*
Alex Tomasa86c6182006-10-11 01:21:03 -07003665 * probably first extent we're gonna free will be last in block
3666 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04003667 err = ext4_writepage_trans_blocks(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07003668 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04003669 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07003670 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07003671
Jan Karacf108bc2008-07-11 19:27:31 -04003672 if (inode->i_size & (sb->s_blocksize - 1))
3673 ext4_block_truncate_page(handle, mapping, inode->i_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07003674
Jan Kara9ddfc3d2008-07-11 19:27:31 -04003675 if (ext4_orphan_add(handle, inode))
3676 goto out_stop;
3677
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003678 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07003679 ext4_ext_invalidate_cache(inode);
3680
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003681 ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003682
Alex Tomasa86c6182006-10-11 01:21:03 -07003683 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003684 * TODO: optimization is possible here.
3685 * Probably we need not scan at all,
3686 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07003687 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003688
3689 /* we have to know where to truncate from in crash case */
3690 EXT4_I(inode)->i_disksize = inode->i_size;
3691 ext4_mark_inode_dirty(handle, inode);
3692
3693 last_block = (inode->i_size + sb->s_blocksize - 1)
3694 >> EXT4_BLOCK_SIZE_BITS(sb);
Allison Hendersonc6a03712011-07-17 23:21:03 -04003695 err = ext4_ext_remove_space(inode, last_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07003696
3697 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04003698 * transaction synchronous.
3699 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003700 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05003701 ext4_handle_sync(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07003702
Jan Kara9ddfc3d2008-07-11 19:27:31 -04003703 up_write(&EXT4_I(inode)->i_data_sem);
Eric Gouriouf6d2f6b2011-05-22 21:33:00 -04003704
3705out_stop:
Alex Tomasa86c6182006-10-11 01:21:03 -07003706 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003707 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07003708 * then we need to clear up the orphan record which we created above.
3709 * However, if this was a real unlink then we were called by
3710 * ext4_delete_inode(), and we allow that function to clean up the
3711 * orphan info for us.
3712 */
3713 if (inode->i_nlink)
3714 ext4_orphan_del(handle, inode);
3715
Solofo Ramangalahyef737722008-04-29 22:00:41 -04003716 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3717 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07003718 ext4_journal_stop(handle);
3719}
3720
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003721static void ext4_falloc_update_inode(struct inode *inode,
3722 int mode, loff_t new_size, int update_ctime)
3723{
3724 struct timespec now;
3725
3726 if (update_ctime) {
3727 now = current_fs_time(inode->i_sb);
3728 if (!timespec_equal(&inode->i_ctime, &now))
3729 inode->i_ctime = now;
3730 }
3731 /*
3732 * Update only when preallocation was requested beyond
3733 * the file size.
3734 */
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003735 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3736 if (new_size > i_size_read(inode))
3737 i_size_write(inode, new_size);
3738 if (new_size > EXT4_I(inode)->i_disksize)
3739 ext4_update_i_disksize(inode, new_size);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003740 } else {
3741 /*
3742 * Mark that we allocate beyond EOF so the subsequent truncate
3743 * can proceed even if the new size is the same as i_size.
3744 */
3745 if (new_size > i_size_read(inode))
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003746 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003747 }
3748
3749}
3750
Amit Aroraa2df2a62007-07-17 21:42:41 -04003751/*
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003752 * preallocate space for a file. This implements ext4's fallocate file
Amit Aroraa2df2a62007-07-17 21:42:41 -04003753 * operation, which gets called from sys_fallocate system call.
3754 * For block-mapped files, posix_fallocate should fall back to the method
3755 * of writing zeroes to the required new blocks (the same behavior which is
3756 * expected for file systems which do not support fallocate() system call).
3757 */
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003758long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
Amit Aroraa2df2a62007-07-17 21:42:41 -04003759{
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003760 struct inode *inode = file->f_path.dentry->d_inode;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003761 handle_t *handle;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003762 loff_t new_size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003763 unsigned int max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003764 int ret = 0;
3765 int ret2 = 0;
3766 int retries = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003767 struct ext4_map_blocks map;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003768 unsigned int credits, blkbits = inode->i_blkbits;
3769
3770 /*
3771 * currently supporting (pre)allocate mode for extent-based
3772 * files _only_
3773 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003774 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Amit Aroraa2df2a62007-07-17 21:42:41 -04003775 return -EOPNOTSUPP;
3776
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003777 /* Return error if mode is not supported */
3778 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
3779 return -EOPNOTSUPP;
3780
3781 if (mode & FALLOC_FL_PUNCH_HOLE)
3782 return ext4_punch_hole(file, offset, len);
3783
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003784 trace_ext4_fallocate_enter(inode, offset, len, mode);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003785 map.m_lblk = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003786 /*
3787 * We can't just convert len to max_blocks because
3788 * If blocksize = 4096 offset = 3072 and len = 2048
3789 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003790 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003791 - map.m_lblk;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003792 /*
Mingming Caof3bd1f32008-08-19 22:16:03 -04003793 * credits to insert 1 extent into extent tree
Amit Aroraa2df2a62007-07-17 21:42:41 -04003794 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04003795 credits = ext4_chunk_trans_blocks(inode, max_blocks);
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05003796 mutex_lock(&inode->i_mutex);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04003797 ret = inode_newsize_ok(inode, (len + offset));
3798 if (ret) {
3799 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003800 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04003801 return ret;
3802 }
Amit Aroraa2df2a62007-07-17 21:42:41 -04003803retry:
3804 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003805 map.m_lblk = map.m_lblk + ret;
3806 map.m_len = max_blocks = max_blocks - ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003807 handle = ext4_journal_start(inode, credits);
3808 if (IS_ERR(handle)) {
3809 ret = PTR_ERR(handle);
3810 break;
3811 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003812 ret = ext4_map_blocks(handle, inode, &map,
Vivek Haldar556b27a2011-05-25 07:41:54 -04003813 EXT4_GET_BLOCKS_CREATE_UNINIT_EXT |
3814 EXT4_GET_BLOCKS_NO_NORMALIZE);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05003815 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003816#ifdef EXT4FS_DEBUG
3817 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003818 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003819 "returned error inode#%lu, block=%u, "
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05003820 "max_blocks=%u", __func__,
Kazuya Mioa6371b62010-10-27 21:30:15 -04003821 inode->i_ino, map.m_lblk, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003822#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04003823 ext4_mark_inode_dirty(handle, inode);
3824 ret2 = ext4_journal_stop(handle);
3825 break;
3826 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003827 if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003828 blkbits) >> blkbits))
3829 new_size = offset + len;
3830 else
Utako Kusaka29ae07b2011-07-27 22:11:20 -04003831 new_size = ((loff_t) map.m_lblk + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003832
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003833 ext4_falloc_update_inode(inode, mode, new_size,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003834 (map.m_flags & EXT4_MAP_NEW));
Amit Aroraa2df2a62007-07-17 21:42:41 -04003835 ext4_mark_inode_dirty(handle, inode);
3836 ret2 = ext4_journal_stop(handle);
3837 if (ret2)
3838 break;
3839 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003840 if (ret == -ENOSPC &&
3841 ext4_should_retry_alloc(inode->i_sb, &retries)) {
3842 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003843 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003844 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05003845 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003846 trace_ext4_fallocate_exit(inode, offset, max_blocks,
3847 ret > 0 ? ret2 : ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003848 return ret > 0 ? ret2 : ret;
3849}
Eric Sandeen6873fa02008-10-07 00:46:36 -04003850
3851/*
Mingming Cao00314622009-09-28 15:49:08 -04003852 * This function convert a range of blocks to written extents
3853 * The caller of this function will pass the start offset and the size.
3854 * all unwritten extents within this range will be converted to
3855 * written extents.
3856 *
3857 * This function is called from the direct IO end io call back
3858 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05003859 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04003860 */
3861int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
Eric Sandeena1de02d2010-02-04 23:58:38 -05003862 ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04003863{
3864 handle_t *handle;
Mingming Cao00314622009-09-28 15:49:08 -04003865 unsigned int max_blocks;
3866 int ret = 0;
3867 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003868 struct ext4_map_blocks map;
Mingming Cao00314622009-09-28 15:49:08 -04003869 unsigned int credits, blkbits = inode->i_blkbits;
3870
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003871 map.m_lblk = offset >> blkbits;
Mingming Cao00314622009-09-28 15:49:08 -04003872 /*
3873 * We can't just convert len to max_blocks because
3874 * If blocksize = 4096 offset = 3072 and len = 2048
3875 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003876 max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
3877 map.m_lblk);
Mingming Cao00314622009-09-28 15:49:08 -04003878 /*
3879 * credits to insert 1 extent into extent tree
3880 */
3881 credits = ext4_chunk_trans_blocks(inode, max_blocks);
3882 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003883 map.m_lblk += ret;
3884 map.m_len = (max_blocks -= ret);
Mingming Cao00314622009-09-28 15:49:08 -04003885 handle = ext4_journal_start(inode, credits);
3886 if (IS_ERR(handle)) {
3887 ret = PTR_ERR(handle);
3888 break;
3889 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003890 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003891 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Mingming Cao00314622009-09-28 15:49:08 -04003892 if (ret <= 0) {
3893 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003894 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Mingming Cao00314622009-09-28 15:49:08 -04003895 "returned error inode#%lu, block=%u, "
3896 "max_blocks=%u", __func__,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003897 inode->i_ino, map.m_lblk, map.m_len);
Mingming Cao00314622009-09-28 15:49:08 -04003898 }
3899 ext4_mark_inode_dirty(handle, inode);
3900 ret2 = ext4_journal_stop(handle);
3901 if (ret <= 0 || ret2 )
3902 break;
3903 }
3904 return ret > 0 ? ret2 : ret;
3905}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003906
Mingming Cao00314622009-09-28 15:49:08 -04003907/*
Eric Sandeen6873fa02008-10-07 00:46:36 -04003908 * Callback function called for each extent to gather FIEMAP information.
3909 */
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04003910static int ext4_ext_fiemap_cb(struct inode *inode, ext4_lblk_t next,
Eric Sandeen6873fa02008-10-07 00:46:36 -04003911 struct ext4_ext_cache *newex, struct ext4_extent *ex,
3912 void *data)
3913{
Eric Sandeen6873fa02008-10-07 00:46:36 -04003914 __u64 logical;
3915 __u64 physical;
3916 __u64 length;
3917 __u32 flags = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003918 int ret = 0;
3919 struct fiemap_extent_info *fieinfo = data;
3920 unsigned char blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003921
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003922 blksize_bits = inode->i_sb->s_blocksize_bits;
3923 logical = (__u64)newex->ec_block << blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003924
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003925 if (newex->ec_start == 0) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003926 /*
3927 * No extent in extent-tree contains block @newex->ec_start,
3928 * then the block may stay in 1)a hole or 2)delayed-extent.
3929 *
3930 * Holes or delayed-extents are processed as follows.
3931 * 1. lookup dirty pages with specified range in pagecache.
3932 * If no page is got, then there is no delayed-extent and
3933 * return with EXT_CONTINUE.
3934 * 2. find the 1st mapped buffer,
3935 * 3. check if the mapped buffer is both in the request range
3936 * and a delayed buffer. If not, there is no delayed-extent,
3937 * then return.
3938 * 4. a delayed-extent is found, the extent will be collected.
3939 */
3940 ext4_lblk_t end = 0;
3941 pgoff_t last_offset;
3942 pgoff_t offset;
3943 pgoff_t index;
Yongqiang Yangb2213492011-05-24 11:36:58 -04003944 pgoff_t start_index = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003945 struct page **pages = NULL;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003946 struct buffer_head *bh = NULL;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003947 struct buffer_head *head = NULL;
3948 unsigned int nr_pages = PAGE_SIZE / sizeof(struct page *);
3949
3950 pages = kmalloc(PAGE_SIZE, GFP_KERNEL);
3951 if (pages == NULL)
3952 return -ENOMEM;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003953
3954 offset = logical >> PAGE_SHIFT;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003955repeat:
3956 last_offset = offset;
3957 head = NULL;
3958 ret = find_get_pages_tag(inode->i_mapping, &offset,
3959 PAGECACHE_TAG_DIRTY, nr_pages, pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04003960
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003961 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
3962 /* First time, try to find a mapped buffer. */
3963 if (ret == 0) {
3964out:
3965 for (index = 0; index < ret; index++)
3966 page_cache_release(pages[index]);
3967 /* just a hole. */
3968 kfree(pages);
3969 return EXT_CONTINUE;
3970 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04003971 index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003972
Yongqiang Yangb2213492011-05-24 11:36:58 -04003973next_page:
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003974 /* Try to find the 1st mapped buffer. */
Yongqiang Yangb2213492011-05-24 11:36:58 -04003975 end = ((__u64)pages[index]->index << PAGE_SHIFT) >>
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003976 blksize_bits;
Yongqiang Yangb2213492011-05-24 11:36:58 -04003977 if (!page_has_buffers(pages[index]))
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003978 goto out;
Yongqiang Yangb2213492011-05-24 11:36:58 -04003979 head = page_buffers(pages[index]);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003980 if (!head)
3981 goto out;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003982
Yongqiang Yangb2213492011-05-24 11:36:58 -04003983 index++;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003984 bh = head;
3985 do {
Yongqiang Yangb2213492011-05-24 11:36:58 -04003986 if (end >= newex->ec_block +
3987 newex->ec_len)
3988 /* The buffer is out of
3989 * the request range.
3990 */
3991 goto out;
3992
3993 if (buffer_mapped(bh) &&
3994 end >= newex->ec_block) {
3995 start_index = index - 1;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003996 /* get the 1st mapped buffer. */
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003997 goto found_mapped_buffer;
3998 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04003999
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004000 bh = bh->b_this_page;
4001 end++;
4002 } while (bh != head);
4003
Yongqiang Yangb2213492011-05-24 11:36:58 -04004004 /* No mapped buffer in the range found in this page,
4005 * We need to look up next page.
4006 */
4007 if (index >= ret) {
4008 /* There is no page left, but we need to limit
4009 * newex->ec_len.
4010 */
4011 newex->ec_len = end - newex->ec_block;
4012 goto out;
4013 }
4014 goto next_page;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004015 } else {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004016 /*Find contiguous delayed buffers. */
4017 if (ret > 0 && pages[0]->index == last_offset)
4018 head = page_buffers(pages[0]);
4019 bh = head;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004020 index = 1;
4021 start_index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004022 }
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004023
4024found_mapped_buffer:
4025 if (bh != NULL && buffer_delay(bh)) {
4026 /* 1st or contiguous delayed buffer found. */
4027 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4028 /*
4029 * 1st delayed buffer found, record
4030 * the start of extent.
4031 */
4032 flags |= FIEMAP_EXTENT_DELALLOC;
4033 newex->ec_block = end;
4034 logical = (__u64)end << blksize_bits;
4035 }
4036 /* Find contiguous delayed buffers. */
4037 do {
4038 if (!buffer_delay(bh))
4039 goto found_delayed_extent;
4040 bh = bh->b_this_page;
4041 end++;
4042 } while (bh != head);
4043
Yongqiang Yangb2213492011-05-24 11:36:58 -04004044 for (; index < ret; index++) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004045 if (!page_has_buffers(pages[index])) {
4046 bh = NULL;
4047 break;
4048 }
4049 head = page_buffers(pages[index]);
4050 if (!head) {
4051 bh = NULL;
4052 break;
4053 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004054
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004055 if (pages[index]->index !=
Yongqiang Yangb2213492011-05-24 11:36:58 -04004056 pages[start_index]->index + index
4057 - start_index) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004058 /* Blocks are not contiguous. */
4059 bh = NULL;
4060 break;
4061 }
4062 bh = head;
4063 do {
4064 if (!buffer_delay(bh))
4065 /* Delayed-extent ends. */
4066 goto found_delayed_extent;
4067 bh = bh->b_this_page;
4068 end++;
4069 } while (bh != head);
4070 }
4071 } else if (!(flags & FIEMAP_EXTENT_DELALLOC))
4072 /* a hole found. */
4073 goto out;
4074
4075found_delayed_extent:
4076 newex->ec_len = min(end - newex->ec_block,
4077 (ext4_lblk_t)EXT_INIT_MAX_LEN);
4078 if (ret == nr_pages && bh != NULL &&
4079 newex->ec_len < EXT_INIT_MAX_LEN &&
4080 buffer_delay(bh)) {
4081 /* Have not collected an extent and continue. */
4082 for (index = 0; index < ret; index++)
4083 page_cache_release(pages[index]);
4084 goto repeat;
4085 }
4086
4087 for (index = 0; index < ret; index++)
4088 page_cache_release(pages[index]);
4089 kfree(pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004090 }
4091
4092 physical = (__u64)newex->ec_start << blksize_bits;
4093 length = (__u64)newex->ec_len << blksize_bits;
4094
4095 if (ex && ext4_ext_is_uninitialized(ex))
4096 flags |= FIEMAP_EXTENT_UNWRITTEN;
4097
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04004098 if (next == EXT_MAX_BLOCKS)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004099 flags |= FIEMAP_EXTENT_LAST;
4100
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004101 ret = fiemap_fill_next_extent(fieinfo, logical, physical,
Eric Sandeen6873fa02008-10-07 00:46:36 -04004102 length, flags);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004103 if (ret < 0)
4104 return ret;
4105 if (ret == 1)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004106 return EXT_BREAK;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004107 return EXT_CONTINUE;
4108}
4109
4110/* fiemap flags we can handle specified here */
4111#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
4112
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004113static int ext4_xattr_fiemap(struct inode *inode,
4114 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004115{
4116 __u64 physical = 0;
4117 __u64 length;
4118 __u32 flags = FIEMAP_EXTENT_LAST;
4119 int blockbits = inode->i_sb->s_blocksize_bits;
4120 int error = 0;
4121
4122 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004123 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04004124 struct ext4_iloc iloc;
4125 int offset; /* offset of xattr in inode */
4126
4127 error = ext4_get_inode_loc(inode, &iloc);
4128 if (error)
4129 return error;
4130 physical = iloc.bh->b_blocknr << blockbits;
4131 offset = EXT4_GOOD_OLD_INODE_SIZE +
4132 EXT4_I(inode)->i_extra_isize;
4133 physical += offset;
4134 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
4135 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004136 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004137 } else { /* external block */
4138 physical = EXT4_I(inode)->i_file_acl << blockbits;
4139 length = inode->i_sb->s_blocksize;
4140 }
4141
4142 if (physical)
4143 error = fiemap_fill_next_extent(fieinfo, 0, physical,
4144 length, flags);
4145 return (error < 0 ? error : 0);
4146}
4147
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004148/*
4149 * ext4_ext_punch_hole
4150 *
4151 * Punches a hole of "length" bytes in a file starting
4152 * at byte "offset"
4153 *
4154 * @inode: The inode of the file to punch a hole in
4155 * @offset: The starting byte offset of the hole
4156 * @length: The length of the hole
4157 *
4158 * Returns the number of blocks removed or negative on err
4159 */
4160int ext4_ext_punch_hole(struct file *file, loff_t offset, loff_t length)
4161{
4162 struct inode *inode = file->f_path.dentry->d_inode;
4163 struct super_block *sb = inode->i_sb;
4164 struct ext4_ext_cache cache_ex;
4165 ext4_lblk_t first_block, last_block, num_blocks, iblock, max_blocks;
4166 struct address_space *mapping = inode->i_mapping;
4167 struct ext4_map_blocks map;
4168 handle_t *handle;
Allison Hendersonba062082011-09-03 11:55:59 -04004169 loff_t first_page, last_page, page_len;
4170 loff_t first_page_offset, last_page_offset;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004171 int ret, credits, blocks_released, err = 0;
4172
Allison Henderson2be47512011-09-03 11:56:52 -04004173 /* No need to punch hole beyond i_size */
4174 if (offset >= inode->i_size)
4175 return 0;
4176
4177 /*
4178 * If the hole extends beyond i_size, set the hole
4179 * to end after the page that contains i_size
4180 */
4181 if (offset + length > inode->i_size) {
4182 length = inode->i_size +
4183 PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) -
4184 offset;
4185 }
4186
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004187 first_block = (offset + sb->s_blocksize - 1) >>
4188 EXT4_BLOCK_SIZE_BITS(sb);
4189 last_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
4190
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004191 first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
4192 last_page = (offset + length) >> PAGE_CACHE_SHIFT;
4193
4194 first_page_offset = first_page << PAGE_CACHE_SHIFT;
4195 last_page_offset = last_page << PAGE_CACHE_SHIFT;
4196
4197 /*
4198 * Write out all dirty pages to avoid race conditions
4199 * Then release them.
4200 */
4201 if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
4202 err = filemap_write_and_wait_range(mapping,
Allison Henderson2be47512011-09-03 11:56:52 -04004203 offset, offset + length - 1);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004204
Allison Henderson2be47512011-09-03 11:56:52 -04004205 if (err)
4206 return err;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004207 }
4208
4209 /* Now release the pages */
4210 if (last_page_offset > first_page_offset) {
4211 truncate_inode_pages_range(mapping, first_page_offset,
4212 last_page_offset-1);
4213 }
4214
4215 /* finish any pending end_io work */
4216 ext4_flush_completed_IO(inode);
4217
4218 credits = ext4_writepage_trans_blocks(inode);
4219 handle = ext4_journal_start(inode, credits);
4220 if (IS_ERR(handle))
4221 return PTR_ERR(handle);
4222
4223 err = ext4_orphan_add(handle, inode);
4224 if (err)
4225 goto out;
4226
4227 /*
Allison Hendersonba062082011-09-03 11:55:59 -04004228 * Now we need to zero out the non-page-aligned data in the
4229 * pages at the start and tail of the hole, and unmap the buffer
4230 * heads for the block aligned regions of the page that were
4231 * completely zeroed.
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004232 */
Allison Hendersonba062082011-09-03 11:55:59 -04004233 if (first_page > last_page) {
4234 /*
4235 * If the file space being truncated is contained within a page
4236 * just zero out and unmap the middle of that page
4237 */
4238 err = ext4_discard_partial_page_buffers(handle,
4239 mapping, offset, length, 0);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004240
Allison Hendersonba062082011-09-03 11:55:59 -04004241 if (err)
4242 goto out;
4243 } else {
4244 /*
4245 * zero out and unmap the partial page that contains
4246 * the start of the hole
4247 */
4248 page_len = first_page_offset - offset;
4249 if (page_len > 0) {
4250 err = ext4_discard_partial_page_buffers(handle, mapping,
4251 offset, page_len, 0);
4252 if (err)
4253 goto out;
4254 }
4255
4256 /*
4257 * zero out and unmap the partial page that contains
4258 * the end of the hole
4259 */
4260 page_len = offset + length - last_page_offset;
4261 if (page_len > 0) {
4262 err = ext4_discard_partial_page_buffers(handle, mapping,
4263 last_page_offset, page_len, 0);
4264 if (err)
4265 goto out;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004266 }
4267 }
4268
Allison Henderson2be47512011-09-03 11:56:52 -04004269
4270 /*
4271 * If i_size is contained in the last page, we need to
4272 * unmap and zero the partial page after i_size
4273 */
4274 if (inode->i_size >> PAGE_CACHE_SHIFT == last_page &&
4275 inode->i_size % PAGE_CACHE_SIZE != 0) {
4276
4277 page_len = PAGE_CACHE_SIZE -
4278 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4279
4280 if (page_len > 0) {
4281 err = ext4_discard_partial_page_buffers(handle,
4282 mapping, inode->i_size, page_len, 0);
4283
4284 if (err)
4285 goto out;
4286 }
4287 }
4288
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004289 /* If there are no blocks to remove, return now */
4290 if (first_block >= last_block)
4291 goto out;
4292
4293 down_write(&EXT4_I(inode)->i_data_sem);
4294 ext4_ext_invalidate_cache(inode);
4295 ext4_discard_preallocations(inode);
4296
4297 /*
4298 * Loop over all the blocks and identify blocks
4299 * that need to be punched out
4300 */
4301 iblock = first_block;
4302 blocks_released = 0;
4303 while (iblock < last_block) {
4304 max_blocks = last_block - iblock;
4305 num_blocks = 1;
4306 memset(&map, 0, sizeof(map));
4307 map.m_lblk = iblock;
4308 map.m_len = max_blocks;
4309 ret = ext4_ext_map_blocks(handle, inode, &map,
4310 EXT4_GET_BLOCKS_PUNCH_OUT_EXT);
4311
4312 if (ret > 0) {
4313 blocks_released += ret;
4314 num_blocks = ret;
4315 } else if (ret == 0) {
4316 /*
4317 * If map blocks could not find the block,
4318 * then it is in a hole. If the hole was
4319 * not already cached, then map blocks should
4320 * put it in the cache. So we can get the hole
4321 * out of the cache
4322 */
4323 memset(&cache_ex, 0, sizeof(cache_ex));
4324 if ((ext4_ext_check_cache(inode, iblock, &cache_ex)) &&
4325 !cache_ex.ec_start) {
4326
4327 /* The hole is cached */
4328 num_blocks = cache_ex.ec_block +
4329 cache_ex.ec_len - iblock;
4330
4331 } else {
4332 /* The block could not be identified */
4333 err = -EIO;
4334 break;
4335 }
4336 } else {
4337 /* Map blocks error */
4338 err = ret;
4339 break;
4340 }
4341
4342 if (num_blocks == 0) {
4343 /* This condition should never happen */
4344 ext_debug("Block lookup failed");
4345 err = -EIO;
4346 break;
4347 }
4348
4349 iblock += num_blocks;
4350 }
4351
4352 if (blocks_released > 0) {
4353 ext4_ext_invalidate_cache(inode);
4354 ext4_discard_preallocations(inode);
4355 }
4356
4357 if (IS_SYNC(inode))
4358 ext4_handle_sync(handle);
4359
4360 up_write(&EXT4_I(inode)->i_data_sem);
4361
4362out:
4363 ext4_orphan_del(handle, inode);
4364 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4365 ext4_mark_inode_dirty(handle, inode);
4366 ext4_journal_stop(handle);
4367 return err;
4368}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004369int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4370 __u64 start, __u64 len)
4371{
4372 ext4_lblk_t start_blk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004373 int error = 0;
4374
4375 /* fallback to generic here if not in extents fmt */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004376 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeen6873fa02008-10-07 00:46:36 -04004377 return generic_block_fiemap(inode, fieinfo, start, len,
4378 ext4_get_block);
4379
4380 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
4381 return -EBADR;
4382
4383 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
4384 error = ext4_xattr_fiemap(inode, fieinfo);
4385 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004386 ext4_lblk_t len_blks;
4387 __u64 last_blk;
4388
Eric Sandeen6873fa02008-10-07 00:46:36 -04004389 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004390 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04004391 if (last_blk >= EXT_MAX_BLOCKS)
4392 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004393 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004394
4395 /*
4396 * Walk the extent tree gathering extent information.
4397 * ext4_ext_fiemap_cb will push extents back to user.
4398 */
Eric Sandeen6873fa02008-10-07 00:46:36 -04004399 error = ext4_ext_walk_space(inode, start_blk, len_blks,
4400 ext4_ext_fiemap_cb, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004401 }
4402
4403 return error;
4404}