blob: 4423b11476af62f1fbf7e59dadfcb840c04a0eae [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"
Alex Tomasa86c6182006-10-11 01:21:03 -070045
Jiaying Zhang0562e0b2011-03-21 21:38:05 -040046#include <trace/events/ext4.h>
47
Allison Hendersond583fb82011-05-25 07:41:43 -040048static int ext4_split_extent(handle_t *handle,
49 struct inode *inode,
50 struct ext4_ext_path *path,
51 struct ext4_map_blocks *map,
52 int split_flag,
53 int flags);
54
Jan Kara487caee2009-08-17 22:17:20 -040055static int ext4_ext_truncate_extend_restart(handle_t *handle,
56 struct inode *inode,
57 int needed)
Alex Tomasa86c6182006-10-11 01:21:03 -070058{
59 int err;
60
Frank Mayhar03901312009-01-07 00:06:22 -050061 if (!ext4_handle_valid(handle))
62 return 0;
Alex Tomasa86c6182006-10-11 01:21:03 -070063 if (handle->h_buffer_credits > needed)
Shen Feng9102e4f2008-07-11 19:27:31 -040064 return 0;
65 err = ext4_journal_extend(handle, needed);
Theodore Ts'o0123c932008-08-01 20:57:54 -040066 if (err <= 0)
Shen Feng9102e4f2008-07-11 19:27:31 -040067 return err;
Jan Kara487caee2009-08-17 22:17:20 -040068 err = ext4_truncate_restart_trans(handle, inode, needed);
Dmitry Monakhov0617b832010-05-17 01:00:00 -040069 if (err == 0)
70 err = -EAGAIN;
Jan Kara487caee2009-08-17 22:17:20 -040071
72 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -070073}
74
75/*
76 * could return:
77 * - EROFS
78 * - ENOMEM
79 */
80static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
81 struct ext4_ext_path *path)
82{
83 if (path->p_bh) {
84 /* path points to block */
85 return ext4_journal_get_write_access(handle, path->p_bh);
86 }
87 /* path points to leaf/index in inode body */
88 /* we use in-core data, no need to protect them */
89 return 0;
90}
91
92/*
93 * could return:
94 * - EROFS
95 * - ENOMEM
96 * - EIO
97 */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -040098#define ext4_ext_dirty(handle, inode, path) \
99 __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path))
100static int __ext4_ext_dirty(const char *where, unsigned int line,
101 handle_t *handle, struct inode *inode,
102 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700103{
104 int err;
105 if (path->p_bh) {
106 /* path points to block */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -0400107 err = __ext4_handle_dirty_metadata(where, line, handle,
108 inode, path->p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700109 } else {
110 /* path points to leaf/index in inode body */
111 err = ext4_mark_inode_dirty(handle, inode);
112 }
113 return err;
114}
115
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700116static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700117 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500118 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700119{
Alex Tomasa86c6182006-10-11 01:21:03 -0700120 if (path) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400121 int depth = path->p_depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700122 struct ext4_extent *ex;
Alex Tomasa86c6182006-10-11 01:21:03 -0700123
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500124 /*
125 * Try to predict block placement assuming that we are
126 * filling in a file which will eventually be
127 * non-sparse --- i.e., in the case of libbfd writing
128 * an ELF object sections out-of-order but in a way
129 * the eventually results in a contiguous object or
130 * executable file, or some database extending a table
131 * space file. However, this is actually somewhat
132 * non-ideal if we are writing a sparse file such as
133 * qemu or KVM writing a raw image file that is going
134 * to stay fairly sparse, since it will end up
135 * fragmenting the file system's free space. Maybe we
136 * should have some hueristics or some way to allow
137 * userspace to pass a hint to file system,
Tao Mab8d65682011-01-21 23:21:31 +0800138 * especially if the latter case turns out to be
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500139 * common.
140 */
Avantika Mathur7e028972006-12-06 20:41:33 -0800141 ex = path[depth].p_ext;
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500142 if (ex) {
143 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
144 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
145
146 if (block > ext_block)
147 return ext_pblk + (block - ext_block);
148 else
149 return ext_pblk - (ext_block - block);
150 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700151
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700152 /* it looks like index is empty;
153 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700154 if (path[depth].p_bh)
155 return path[depth].p_bh->b_blocknr;
156 }
157
158 /* OK. use inode's group */
Eric Sandeenf86186b2011-06-28 10:01:31 -0400159 return ext4_inode_to_goal_block(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700160}
161
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400162/*
163 * Allocation for a meta data block
164 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700165static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400166ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700167 struct ext4_ext_path *path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400168 struct ext4_extent *ex, int *err, unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700169{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700170 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700171
172 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Allison Henderson55f020d2011-05-25 07:41:26 -0400173 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
174 NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700175 return newblock;
176}
177
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400178static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700179{
180 int size;
181
182 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
183 / sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100184#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400185 if (!check && size > 6)
186 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700187#endif
188 return size;
189}
190
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400191static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700192{
193 int size;
194
195 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
196 / sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100197#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400198 if (!check && size > 5)
199 size = 5;
Alex Tomasa86c6182006-10-11 01:21:03 -0700200#endif
201 return size;
202}
203
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400204static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700205{
206 int size;
207
208 size = sizeof(EXT4_I(inode)->i_data);
209 size -= sizeof(struct ext4_extent_header);
210 size /= sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100211#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400212 if (!check && size > 3)
213 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700214#endif
215 return size;
216}
217
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400218static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700219{
220 int size;
221
222 size = sizeof(EXT4_I(inode)->i_data);
223 size -= sizeof(struct ext4_extent_header);
224 size /= sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100225#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400226 if (!check && size > 4)
227 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700228#endif
229 return size;
230}
231
Mingming Caod2a17632008-07-14 17:52:37 -0400232/*
233 * Calculate the number of metadata blocks needed
234 * to allocate @blocks
235 * Worse case is one block per extent
236 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -0500237int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -0400238{
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500239 struct ext4_inode_info *ei = EXT4_I(inode);
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400240 int idxs;
Mingming Caod2a17632008-07-14 17:52:37 -0400241
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500242 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
243 / sizeof(struct ext4_extent_idx));
Mingming Caod2a17632008-07-14 17:52:37 -0400244
245 /*
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500246 * If the new delayed allocation block is contiguous with the
247 * previous da block, it can share index blocks with the
248 * previous block, so we only need to allocate a new index
249 * block every idxs leaf blocks. At ldxs**2 blocks, we need
250 * an additional index block, and at ldxs**3 blocks, yet
251 * another index blocks.
Mingming Caod2a17632008-07-14 17:52:37 -0400252 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500253 if (ei->i_da_metadata_calc_len &&
254 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400255 int num = 0;
256
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500257 if ((ei->i_da_metadata_calc_len % idxs) == 0)
258 num++;
259 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
260 num++;
261 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
262 num++;
263 ei->i_da_metadata_calc_len = 0;
264 } else
265 ei->i_da_metadata_calc_len++;
266 ei->i_da_metadata_calc_last_lblock++;
267 return num;
268 }
Mingming Caod2a17632008-07-14 17:52:37 -0400269
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500270 /*
271 * In the worst case we need a new set of index blocks at
272 * every level of the inode's extent tree.
273 */
274 ei->i_da_metadata_calc_len = 1;
275 ei->i_da_metadata_calc_last_lblock = lblock;
276 return ext_depth(inode) + 1;
Mingming Caod2a17632008-07-14 17:52:37 -0400277}
278
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400279static int
280ext4_ext_max_entries(struct inode *inode, int depth)
281{
282 int max;
283
284 if (depth == ext_depth(inode)) {
285 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400286 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400287 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400288 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400289 } else {
290 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400291 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400292 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400293 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400294 }
295
296 return max;
297}
298
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400299static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
300{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400301 ext4_fsblk_t block = ext4_ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400302 int len = ext4_ext_get_actual_len(ext);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400303
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400304 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400305}
306
307static int ext4_valid_extent_idx(struct inode *inode,
308 struct ext4_extent_idx *ext_idx)
309{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400310 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400311
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400312 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400313}
314
315static int ext4_valid_extent_entries(struct inode *inode,
316 struct ext4_extent_header *eh,
317 int depth)
318{
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400319 unsigned short entries;
320 if (eh->eh_entries == 0)
321 return 1;
322
323 entries = le16_to_cpu(eh->eh_entries);
324
325 if (depth == 0) {
326 /* leaf entries */
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400327 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400328 while (entries) {
329 if (!ext4_valid_extent(inode, ext))
330 return 0;
331 ext++;
332 entries--;
333 }
334 } else {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400335 struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400336 while (entries) {
337 if (!ext4_valid_extent_idx(inode, ext_idx))
338 return 0;
339 ext_idx++;
340 entries--;
341 }
342 }
343 return 1;
344}
345
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400346static int __ext4_ext_check(const char *function, unsigned int line,
347 struct inode *inode, struct ext4_extent_header *eh,
348 int depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400349{
350 const char *error_msg;
351 int max = 0;
352
353 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
354 error_msg = "invalid magic";
355 goto corrupted;
356 }
357 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
358 error_msg = "unexpected eh_depth";
359 goto corrupted;
360 }
361 if (unlikely(eh->eh_max == 0)) {
362 error_msg = "invalid eh_max";
363 goto corrupted;
364 }
365 max = ext4_ext_max_entries(inode, depth);
366 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
367 error_msg = "too large eh_max";
368 goto corrupted;
369 }
370 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
371 error_msg = "invalid eh_entries";
372 goto corrupted;
373 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400374 if (!ext4_valid_extent_entries(inode, eh, depth)) {
375 error_msg = "invalid extent entries";
376 goto corrupted;
377 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400378 return 0;
379
380corrupted:
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400381 ext4_error_inode(inode, function, line, 0,
Theodore Ts'o24676da2010-05-16 21:00:00 -0400382 "bad header/extent: %s - magic %x, "
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400383 "entries %u, max %u(%u), depth %u(%u)",
Theodore Ts'o24676da2010-05-16 21:00:00 -0400384 error_msg, le16_to_cpu(eh->eh_magic),
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400385 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
386 max, le16_to_cpu(eh->eh_depth), depth);
387
388 return -EIO;
389}
390
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400391#define ext4_ext_check(inode, eh, depth) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400392 __ext4_ext_check(__func__, __LINE__, inode, eh, depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400393
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400394int ext4_ext_check_inode(struct inode *inode)
395{
396 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
397}
398
Alex Tomasa86c6182006-10-11 01:21:03 -0700399#ifdef EXT_DEBUG
400static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
401{
402 int k, l = path->p_depth;
403
404 ext_debug("path:");
405 for (k = 0; k <= l; k++, path++) {
406 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700407 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400408 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700409 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400410 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700411 le32_to_cpu(path->p_ext->ee_block),
Mingming553f9002009-09-18 13:34:55 -0400412 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400413 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400414 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700415 } else
416 ext_debug(" []");
417 }
418 ext_debug("\n");
419}
420
421static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
422{
423 int depth = ext_depth(inode);
424 struct ext4_extent_header *eh;
425 struct ext4_extent *ex;
426 int i;
427
428 if (!path)
429 return;
430
431 eh = path[depth].p_hdr;
432 ex = EXT_FIRST_EXTENT(eh);
433
Mingming553f9002009-09-18 13:34:55 -0400434 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
435
Alex Tomasa86c6182006-10-11 01:21:03 -0700436 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400437 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
438 ext4_ext_is_uninitialized(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400439 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700440 }
441 ext_debug("\n");
442}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400443
444static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
445 ext4_fsblk_t newblock, int level)
446{
447 int depth = ext_depth(inode);
448 struct ext4_extent *ex;
449
450 if (depth != level) {
451 struct ext4_extent_idx *idx;
452 idx = path[level].p_idx;
453 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
454 ext_debug("%d: move %d:%llu in new index %llu\n", level,
455 le32_to_cpu(idx->ei_block),
456 ext4_idx_pblock(idx),
457 newblock);
458 idx++;
459 }
460
461 return;
462 }
463
464 ex = path[depth].p_ext;
465 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
466 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
467 le32_to_cpu(ex->ee_block),
468 ext4_ext_pblock(ex),
469 ext4_ext_is_uninitialized(ex),
470 ext4_ext_get_actual_len(ex),
471 newblock);
472 ex++;
473 }
474}
475
Alex Tomasa86c6182006-10-11 01:21:03 -0700476#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400477#define ext4_ext_show_path(inode, path)
478#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400479#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700480#endif
481
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500482void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700483{
484 int depth = path->p_depth;
485 int i;
486
487 for (i = 0; i <= depth; i++, path++)
488 if (path->p_bh) {
489 brelse(path->p_bh);
490 path->p_bh = NULL;
491 }
492}
493
494/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700495 * ext4_ext_binsearch_idx:
496 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400497 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700498 */
499static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500500ext4_ext_binsearch_idx(struct inode *inode,
501 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700502{
503 struct ext4_extent_header *eh = path->p_hdr;
504 struct ext4_extent_idx *r, *l, *m;
505
Alex Tomasa86c6182006-10-11 01:21:03 -0700506
Eric Sandeenbba90742008-01-28 23:58:27 -0500507 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700508
509 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400510 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700511 while (l <= r) {
512 m = l + (r - l) / 2;
513 if (block < le32_to_cpu(m->ei_block))
514 r = m - 1;
515 else
516 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400517 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
518 m, le32_to_cpu(m->ei_block),
519 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700520 }
521
522 path->p_idx = l - 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700523 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400524 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700525
526#ifdef CHECK_BINSEARCH
527 {
528 struct ext4_extent_idx *chix, *ix;
529 int k;
530
531 chix = ix = EXT_FIRST_INDEX(eh);
532 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
533 if (k != 0 &&
534 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400535 printk(KERN_DEBUG "k=%d, ix=0x%p, "
536 "first=0x%p\n", k,
537 ix, EXT_FIRST_INDEX(eh));
538 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700539 le32_to_cpu(ix->ei_block),
540 le32_to_cpu(ix[-1].ei_block));
541 }
542 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400543 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700544 if (block < le32_to_cpu(ix->ei_block))
545 break;
546 chix = ix;
547 }
548 BUG_ON(chix != path->p_idx);
549 }
550#endif
551
552}
553
554/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700555 * ext4_ext_binsearch:
556 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400557 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700558 */
559static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500560ext4_ext_binsearch(struct inode *inode,
561 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700562{
563 struct ext4_extent_header *eh = path->p_hdr;
564 struct ext4_extent *r, *l, *m;
565
Alex Tomasa86c6182006-10-11 01:21:03 -0700566 if (eh->eh_entries == 0) {
567 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700568 * this leaf is empty:
569 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700570 */
571 return;
572 }
573
Eric Sandeenbba90742008-01-28 23:58:27 -0500574 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700575
576 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400577 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700578
579 while (l <= r) {
580 m = l + (r - l) / 2;
581 if (block < le32_to_cpu(m->ee_block))
582 r = m - 1;
583 else
584 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400585 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
586 m, le32_to_cpu(m->ee_block),
587 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700588 }
589
590 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400591 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400592 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400593 ext4_ext_pblock(path->p_ext),
Mingming553f9002009-09-18 13:34:55 -0400594 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400595 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700596
597#ifdef CHECK_BINSEARCH
598 {
599 struct ext4_extent *chex, *ex;
600 int k;
601
602 chex = ex = EXT_FIRST_EXTENT(eh);
603 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
604 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400605 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700606 if (block < le32_to_cpu(ex->ee_block))
607 break;
608 chex = ex;
609 }
610 BUG_ON(chex != path->p_ext);
611 }
612#endif
613
614}
615
616int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
617{
618 struct ext4_extent_header *eh;
619
620 eh = ext_inode_hdr(inode);
621 eh->eh_depth = 0;
622 eh->eh_entries = 0;
623 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400624 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700625 ext4_mark_inode_dirty(handle, inode);
626 ext4_ext_invalidate_cache(inode);
627 return 0;
628}
629
630struct ext4_ext_path *
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500631ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
632 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700633{
634 struct ext4_extent_header *eh;
635 struct buffer_head *bh;
636 short int depth, i, ppos = 0, alloc = 0;
637
638 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400639 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700640
641 /* account possible depth increase */
642 if (!path) {
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800643 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700644 GFP_NOFS);
645 if (!path)
646 return ERR_PTR(-ENOMEM);
647 alloc = 1;
648 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700649 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400650 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700651
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400652 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700653 /* walk through the tree */
654 while (i) {
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400655 int need_to_validate = 0;
656
Alex Tomasa86c6182006-10-11 01:21:03 -0700657 ext_debug("depth %d: num %d, max %d\n",
658 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400659
Alex Tomasa86c6182006-10-11 01:21:03 -0700660 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400661 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700662 path[ppos].p_depth = i;
663 path[ppos].p_ext = NULL;
664
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400665 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
666 if (unlikely(!bh))
Alex Tomasa86c6182006-10-11 01:21:03 -0700667 goto err;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400668 if (!bh_uptodate_or_lock(bh)) {
Jiaying Zhang0562e0b2011-03-21 21:38:05 -0400669 trace_ext4_ext_load_extent(inode, block,
670 path[ppos].p_block);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400671 if (bh_submit_read(bh) < 0) {
672 put_bh(bh);
673 goto err;
674 }
675 /* validate the extent entries */
676 need_to_validate = 1;
677 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700678 eh = ext_block_hdr(bh);
679 ppos++;
Frank Mayhar273df552010-03-02 11:46:09 -0500680 if (unlikely(ppos > depth)) {
681 put_bh(bh);
682 EXT4_ERROR_INODE(inode,
683 "ppos %d > depth %d", ppos, depth);
684 goto err;
685 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700686 path[ppos].p_bh = bh;
687 path[ppos].p_hdr = eh;
688 i--;
689
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400690 if (need_to_validate && ext4_ext_check(inode, eh, i))
Alex Tomasa86c6182006-10-11 01:21:03 -0700691 goto err;
692 }
693
694 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700695 path[ppos].p_ext = NULL;
696 path[ppos].p_idx = NULL;
697
Alex Tomasa86c6182006-10-11 01:21:03 -0700698 /* find extent */
699 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400700 /* if not an empty leaf */
701 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400702 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700703
704 ext4_ext_show_path(inode, path);
705
706 return path;
707
708err:
709 ext4_ext_drop_refs(path);
710 if (alloc)
711 kfree(path);
712 return ERR_PTR(-EIO);
713}
714
715/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700716 * ext4_ext_insert_index:
717 * insert new index [@logical;@ptr] into the block at @curp;
718 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700719 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400720static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
721 struct ext4_ext_path *curp,
722 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700723{
724 struct ext4_extent_idx *ix;
725 int len, err;
726
Avantika Mathur7e028972006-12-06 20:41:33 -0800727 err = ext4_ext_get_access(handle, inode, curp);
728 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700729 return err;
730
Frank Mayhar273df552010-03-02 11:46:09 -0500731 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
732 EXT4_ERROR_INODE(inode,
733 "logical %d == ei_block %d!",
734 logical, le32_to_cpu(curp->p_idx->ei_block));
735 return -EIO;
736 }
Robin Dongd4620312011-07-17 23:43:42 -0400737
738 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
739 >= le16_to_cpu(curp->p_hdr->eh_max))) {
740 EXT4_ERROR_INODE(inode,
741 "eh_entries %d >= eh_max %d!",
742 le16_to_cpu(curp->p_hdr->eh_entries),
743 le16_to_cpu(curp->p_hdr->eh_max));
744 return -EIO;
745 }
746
Alex Tomasa86c6182006-10-11 01:21:03 -0700747 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
748 /* insert after */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400749 ext_debug("insert new index %d after: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700750 ix = curp->p_idx + 1;
751 } else {
752 /* insert before */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400753 ext_debug("insert new index %d before: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700754 ix = curp->p_idx;
755 }
756
Eric Gouriou80e675f2011-10-27 11:52:18 -0400757 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
758 BUG_ON(len < 0);
759 if (len > 0) {
760 ext_debug("insert new index %d: "
761 "move %d indices from 0x%p to 0x%p\n",
762 logical, len, ix, ix + 1);
763 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
764 }
765
Tao Maf472e022011-10-17 10:13:46 -0400766 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
767 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
768 return -EIO;
769 }
770
Alex Tomasa86c6182006-10-11 01:21:03 -0700771 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700772 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400773 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700774
Frank Mayhar273df552010-03-02 11:46:09 -0500775 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
776 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
777 return -EIO;
778 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700779
780 err = ext4_ext_dirty(handle, inode, curp);
781 ext4_std_error(inode->i_sb, err);
782
783 return err;
784}
785
786/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700787 * ext4_ext_split:
788 * inserts new subtree into the path, using free index entry
789 * at depth @at:
790 * - allocates all needed blocks (new leaf and all intermediate index blocks)
791 * - makes decision where to split
792 * - moves remaining extents and index entries (right to the split point)
793 * into the newly allocated blocks
794 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700795 */
796static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -0400797 unsigned int flags,
798 struct ext4_ext_path *path,
799 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -0700800{
801 struct buffer_head *bh = NULL;
802 int depth = ext_depth(inode);
803 struct ext4_extent_header *neh;
804 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -0700805 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700806 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700807 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700808 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700809 int err = 0;
810
811 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700812 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700813
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700814 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700815 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -0500816 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
817 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
818 return -EIO;
819 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700820 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
821 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700822 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700823 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400824 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700825 } else {
826 border = newext->ee_block;
827 ext_debug("leaf will be added."
828 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400829 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700830 }
831
832 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700833 * If error occurs, then we break processing
834 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700835 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700836 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700837 */
838
839 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700840 * Get array to track all allocated blocks.
841 * We need this to handle errors and free blocks
842 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700843 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800844 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700845 if (!ablocks)
846 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700847
848 /* allocate all needed blocks */
849 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
850 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400851 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400852 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -0700853 if (newblock == 0)
854 goto cleanup;
855 ablocks[a] = newblock;
856 }
857
858 /* initialize new leaf */
859 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -0500860 if (unlikely(newblock == 0)) {
861 EXT4_ERROR_INODE(inode, "newblock == 0!");
862 err = -EIO;
863 goto cleanup;
864 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700865 bh = sb_getblk(inode->i_sb, newblock);
866 if (!bh) {
867 err = -EIO;
868 goto cleanup;
869 }
870 lock_buffer(bh);
871
Avantika Mathur7e028972006-12-06 20:41:33 -0800872 err = ext4_journal_get_create_access(handle, bh);
873 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700874 goto cleanup;
875
876 neh = ext_block_hdr(bh);
877 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400878 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700879 neh->eh_magic = EXT4_EXT_MAGIC;
880 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700881
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700882 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -0500883 if (unlikely(path[depth].p_hdr->eh_entries !=
884 path[depth].p_hdr->eh_max)) {
885 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
886 path[depth].p_hdr->eh_entries,
887 path[depth].p_hdr->eh_max);
888 err = -EIO;
889 goto cleanup;
890 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700891 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400892 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
893 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -0700894 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400895 struct ext4_extent *ex;
896 ex = EXT_FIRST_EXTENT(neh);
897 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400898 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700899 }
900
901 set_buffer_uptodate(bh);
902 unlock_buffer(bh);
903
Frank Mayhar03901312009-01-07 00:06:22 -0500904 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800905 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700906 goto cleanup;
907 brelse(bh);
908 bh = NULL;
909
910 /* correct old leaf */
911 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800912 err = ext4_ext_get_access(handle, inode, path + depth);
913 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700914 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400915 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800916 err = ext4_ext_dirty(handle, inode, path + depth);
917 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700918 goto cleanup;
919
920 }
921
922 /* create intermediate indexes */
923 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -0500924 if (unlikely(k < 0)) {
925 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
926 err = -EIO;
927 goto cleanup;
928 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700929 if (k)
930 ext_debug("create %d intermediate indices\n", k);
931 /* insert new index into current index block */
932 /* current depth stored in i var */
933 i = depth - 1;
934 while (k--) {
935 oldblock = newblock;
936 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -0500937 bh = sb_getblk(inode->i_sb, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700938 if (!bh) {
939 err = -EIO;
940 goto cleanup;
941 }
942 lock_buffer(bh);
943
Avantika Mathur7e028972006-12-06 20:41:33 -0800944 err = ext4_journal_get_create_access(handle, bh);
945 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700946 goto cleanup;
947
948 neh = ext_block_hdr(bh);
949 neh->eh_entries = cpu_to_le16(1);
950 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400951 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700952 neh->eh_depth = cpu_to_le16(depth - i);
953 fidx = EXT_FIRST_INDEX(neh);
954 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700955 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700956
Eric Sandeenbba90742008-01-28 23:58:27 -0500957 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
958 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700959
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400960 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -0500961 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
962 EXT_LAST_INDEX(path[i].p_hdr))) {
963 EXT4_ERROR_INODE(inode,
964 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
965 le32_to_cpu(path[i].p_ext->ee_block));
966 err = -EIO;
967 goto cleanup;
968 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400969 /* start copy indexes */
970 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
971 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
972 EXT_MAX_INDEX(path[i].p_hdr));
973 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -0700974 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400975 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -0700976 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400977 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700978 }
979 set_buffer_uptodate(bh);
980 unlock_buffer(bh);
981
Frank Mayhar03901312009-01-07 00:06:22 -0500982 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800983 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700984 goto cleanup;
985 brelse(bh);
986 bh = NULL;
987
988 /* correct old index */
989 if (m) {
990 err = ext4_ext_get_access(handle, inode, path + i);
991 if (err)
992 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400993 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700994 err = ext4_ext_dirty(handle, inode, path + i);
995 if (err)
996 goto cleanup;
997 }
998
999 i--;
1000 }
1001
1002 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001003 err = ext4_ext_insert_index(handle, inode, path + at,
1004 le32_to_cpu(border), newblock);
1005
1006cleanup:
1007 if (bh) {
1008 if (buffer_locked(bh))
1009 unlock_buffer(bh);
1010 brelse(bh);
1011 }
1012
1013 if (err) {
1014 /* free all allocated blocks in error case */
1015 for (i = 0; i < depth; i++) {
1016 if (!ablocks[i])
1017 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001018 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001019 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001020 }
1021 }
1022 kfree(ablocks);
1023
1024 return err;
1025}
1026
1027/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001028 * ext4_ext_grow_indepth:
1029 * implements tree growing procedure:
1030 * - allocates new block
1031 * - moves top-level data (index block or leaf) into the new block
1032 * - initializes new top-level, creating index that points to the
1033 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001034 */
1035static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001036 unsigned int flags,
Allison Henderson55f020d2011-05-25 07:41:26 -04001037 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001038{
Alex Tomasa86c6182006-10-11 01:21:03 -07001039 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001040 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001041 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001042 int err = 0;
1043
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001044 newblock = ext4_ext_new_meta_block(handle, inode, NULL,
Allison Henderson55f020d2011-05-25 07:41:26 -04001045 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001046 if (newblock == 0)
1047 return err;
1048
1049 bh = sb_getblk(inode->i_sb, newblock);
1050 if (!bh) {
1051 err = -EIO;
1052 ext4_std_error(inode->i_sb, err);
1053 return err;
1054 }
1055 lock_buffer(bh);
1056
Avantika Mathur7e028972006-12-06 20:41:33 -08001057 err = ext4_journal_get_create_access(handle, bh);
1058 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001059 unlock_buffer(bh);
1060 goto out;
1061 }
1062
1063 /* move top-level index/leaf into new block */
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001064 memmove(bh->b_data, EXT4_I(inode)->i_data,
1065 sizeof(EXT4_I(inode)->i_data));
Alex Tomasa86c6182006-10-11 01:21:03 -07001066
1067 /* set size of new block */
1068 neh = ext_block_hdr(bh);
1069 /* old root could have indexes or leaves
1070 * so calculate e_max right way */
1071 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001072 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001073 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001074 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001075 neh->eh_magic = EXT4_EXT_MAGIC;
1076 set_buffer_uptodate(bh);
1077 unlock_buffer(bh);
1078
Frank Mayhar03901312009-01-07 00:06:22 -05001079 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001080 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001081 goto out;
1082
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001083 /* Update top-level index: num,max,pointer */
Alex Tomasa86c6182006-10-11 01:21:03 -07001084 neh = ext_inode_hdr(inode);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001085 neh->eh_entries = cpu_to_le16(1);
1086 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1087 if (neh->eh_depth == 0) {
1088 /* Root extent block becomes index block */
1089 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1090 EXT_FIRST_INDEX(neh)->ei_block =
1091 EXT_FIRST_EXTENT(neh)->ee_block;
1092 }
Mingming Cao2ae02102006-10-11 01:21:11 -07001093 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001094 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001095 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001096 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001097
Paul Mackerrasb4611ab2011-12-12 11:00:56 -05001098 neh->eh_depth = cpu_to_le16(le16_to_cpu(neh->eh_depth) + 1);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001099 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07001100out:
1101 brelse(bh);
1102
1103 return err;
1104}
1105
1106/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001107 * ext4_ext_create_new_leaf:
1108 * finds empty index and adds new leaf.
1109 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001110 */
1111static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001112 unsigned int flags,
1113 struct ext4_ext_path *path,
1114 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001115{
1116 struct ext4_ext_path *curp;
1117 int depth, i, err = 0;
1118
1119repeat:
1120 i = depth = ext_depth(inode);
1121
1122 /* walk up to the tree and look for free index entry */
1123 curp = path + depth;
1124 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1125 i--;
1126 curp--;
1127 }
1128
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001129 /* we use already allocated block for index block,
1130 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001131 if (EXT_HAS_FREE_INDEX(curp)) {
1132 /* if we found index with free entry, then use that
1133 * entry: create all needed subtree and add new leaf */
Allison Henderson55f020d2011-05-25 07:41:26 -04001134 err = ext4_ext_split(handle, inode, flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001135 if (err)
1136 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001137
1138 /* refill path */
1139 ext4_ext_drop_refs(path);
1140 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001141 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1142 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001143 if (IS_ERR(path))
1144 err = PTR_ERR(path);
1145 } else {
1146 /* tree is full, time to grow in depth */
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001147 err = ext4_ext_grow_indepth(handle, inode, flags, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001148 if (err)
1149 goto out;
1150
1151 /* refill path */
1152 ext4_ext_drop_refs(path);
1153 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001154 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1155 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001156 if (IS_ERR(path)) {
1157 err = PTR_ERR(path);
1158 goto out;
1159 }
1160
1161 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001162 * only first (depth 0 -> 1) produces free space;
1163 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001164 */
1165 depth = ext_depth(inode);
1166 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001167 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001168 goto repeat;
1169 }
1170 }
1171
1172out:
1173 return err;
1174}
1175
1176/*
Alex Tomas1988b512008-01-28 23:58:27 -05001177 * search the closest allocated block to the left for *logical
1178 * and returns it at @logical + it's physical address at @phys
1179 * if *logical is the smallest allocated block, the function
1180 * returns 0 at @phys
1181 * return value contains 0 (success) or error code
1182 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001183static int ext4_ext_search_left(struct inode *inode,
1184 struct ext4_ext_path *path,
1185 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001186{
1187 struct ext4_extent_idx *ix;
1188 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001189 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001190
Frank Mayhar273df552010-03-02 11:46:09 -05001191 if (unlikely(path == NULL)) {
1192 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1193 return -EIO;
1194 }
Alex Tomas1988b512008-01-28 23:58:27 -05001195 depth = path->p_depth;
1196 *phys = 0;
1197
1198 if (depth == 0 && path->p_ext == NULL)
1199 return 0;
1200
1201 /* usually extent in the path covers blocks smaller
1202 * then *logical, but it can be that extent is the
1203 * first one in the file */
1204
1205 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001206 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001207 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001208 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1209 EXT4_ERROR_INODE(inode,
1210 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1211 *logical, le32_to_cpu(ex->ee_block));
1212 return -EIO;
1213 }
Alex Tomas1988b512008-01-28 23:58:27 -05001214 while (--depth >= 0) {
1215 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001216 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1217 EXT4_ERROR_INODE(inode,
1218 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
Tao Ma6ee3b212011-10-08 16:08:34 -04001219 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001220 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
Tao Ma6ee3b212011-10-08 16:08:34 -04001221 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001222 depth);
1223 return -EIO;
1224 }
Alex Tomas1988b512008-01-28 23:58:27 -05001225 }
1226 return 0;
1227 }
1228
Frank Mayhar273df552010-03-02 11:46:09 -05001229 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1230 EXT4_ERROR_INODE(inode,
1231 "logical %d < ee_block %d + ee_len %d!",
1232 *logical, le32_to_cpu(ex->ee_block), ee_len);
1233 return -EIO;
1234 }
Alex Tomas1988b512008-01-28 23:58:27 -05001235
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001236 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001237 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001238 return 0;
1239}
1240
1241/*
1242 * search the closest allocated block to the right for *logical
1243 * and returns it at @logical + it's physical address at @phys
Tao Madf3ab172011-10-08 15:53:49 -04001244 * if *logical is the largest allocated block, the function
Alex Tomas1988b512008-01-28 23:58:27 -05001245 * returns 0 at @phys
1246 * return value contains 0 (success) or error code
1247 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001248static int ext4_ext_search_right(struct inode *inode,
1249 struct ext4_ext_path *path,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001250 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1251 struct ext4_extent **ret_ex)
Alex Tomas1988b512008-01-28 23:58:27 -05001252{
1253 struct buffer_head *bh = NULL;
1254 struct ext4_extent_header *eh;
1255 struct ext4_extent_idx *ix;
1256 struct ext4_extent *ex;
1257 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001258 int depth; /* Note, NOT eh_depth; depth from top of tree */
1259 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001260
Frank Mayhar273df552010-03-02 11:46:09 -05001261 if (unlikely(path == NULL)) {
1262 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1263 return -EIO;
1264 }
Alex Tomas1988b512008-01-28 23:58:27 -05001265 depth = path->p_depth;
1266 *phys = 0;
1267
1268 if (depth == 0 && path->p_ext == NULL)
1269 return 0;
1270
1271 /* usually extent in the path covers blocks smaller
1272 * then *logical, but it can be that extent is the
1273 * first one in the file */
1274
1275 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001276 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001277 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001278 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1279 EXT4_ERROR_INODE(inode,
1280 "first_extent(path[%d].p_hdr) != ex",
1281 depth);
1282 return -EIO;
1283 }
Alex Tomas1988b512008-01-28 23:58:27 -05001284 while (--depth >= 0) {
1285 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001286 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1287 EXT4_ERROR_INODE(inode,
1288 "ix != EXT_FIRST_INDEX *logical %d!",
1289 *logical);
1290 return -EIO;
1291 }
Alex Tomas1988b512008-01-28 23:58:27 -05001292 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001293 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001294 }
1295
Frank Mayhar273df552010-03-02 11:46:09 -05001296 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1297 EXT4_ERROR_INODE(inode,
1298 "logical %d < ee_block %d + ee_len %d!",
1299 *logical, le32_to_cpu(ex->ee_block), ee_len);
1300 return -EIO;
1301 }
Alex Tomas1988b512008-01-28 23:58:27 -05001302
1303 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1304 /* next allocated block in this leaf */
1305 ex++;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001306 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001307 }
1308
1309 /* go up and search for index to the right */
1310 while (--depth >= 0) {
1311 ix = path[depth].p_idx;
1312 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001313 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001314 }
1315
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001316 /* we've gone up to the root and found no index to the right */
1317 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001318
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001319got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001320 /* we've found index to the right, let's
1321 * follow it and find the closest allocated
1322 * block to the right */
1323 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001324 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001325 while (++depth < path->p_depth) {
1326 bh = sb_bread(inode->i_sb, block);
1327 if (bh == NULL)
1328 return -EIO;
1329 eh = ext_block_hdr(bh);
Eric Sandeen395a87b2009-03-10 18:18:47 -04001330 /* subtract from p_depth to get proper eh_depth */
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001331 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001332 put_bh(bh);
1333 return -EIO;
1334 }
1335 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001336 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001337 put_bh(bh);
1338 }
1339
1340 bh = sb_bread(inode->i_sb, block);
1341 if (bh == NULL)
1342 return -EIO;
1343 eh = ext_block_hdr(bh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001344 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001345 put_bh(bh);
1346 return -EIO;
1347 }
1348 ex = EXT_FIRST_EXTENT(eh);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001349found_extent:
Alex Tomas1988b512008-01-28 23:58:27 -05001350 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001351 *phys = ext4_ext_pblock(ex);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001352 *ret_ex = ex;
1353 if (bh)
1354 put_bh(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001355 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001356}
1357
1358/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001359 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001360 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001361 * NOTE: it considers block number from index entry as
1362 * allocated block. Thus, index entries have to be consistent
1363 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001364 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001365static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001366ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1367{
1368 int depth;
1369
1370 BUG_ON(path == NULL);
1371 depth = path->p_depth;
1372
1373 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001374 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001375
1376 while (depth >= 0) {
1377 if (depth == path->p_depth) {
1378 /* leaf */
Curt Wohlgemuth6f8ff532011-10-26 04:38:59 -04001379 if (path[depth].p_ext &&
1380 path[depth].p_ext !=
Alex Tomasa86c6182006-10-11 01:21:03 -07001381 EXT_LAST_EXTENT(path[depth].p_hdr))
1382 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1383 } else {
1384 /* index */
1385 if (path[depth].p_idx !=
1386 EXT_LAST_INDEX(path[depth].p_hdr))
1387 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1388 }
1389 depth--;
1390 }
1391
Lukas Czernerf17722f2011-06-06 00:05:17 -04001392 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001393}
1394
1395/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001396 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001397 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001398 */
Robin Dong57187892011-07-23 21:49:07 -04001399static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001400{
1401 int depth;
1402
1403 BUG_ON(path == NULL);
1404 depth = path->p_depth;
1405
1406 /* zero-tree has no leaf blocks at all */
1407 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001408 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001409
1410 /* go to index block */
1411 depth--;
1412
1413 while (depth >= 0) {
1414 if (path[depth].p_idx !=
1415 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001416 return (ext4_lblk_t)
1417 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001418 depth--;
1419 }
1420
Lukas Czernerf17722f2011-06-06 00:05:17 -04001421 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001422}
1423
1424/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001425 * ext4_ext_correct_indexes:
1426 * if leaf gets modified and modified extent is first in the leaf,
1427 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001428 * TODO: do we need to correct tree in all cases?
1429 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001430static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001431 struct ext4_ext_path *path)
1432{
1433 struct ext4_extent_header *eh;
1434 int depth = ext_depth(inode);
1435 struct ext4_extent *ex;
1436 __le32 border;
1437 int k, err = 0;
1438
1439 eh = path[depth].p_hdr;
1440 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001441
1442 if (unlikely(ex == NULL || eh == NULL)) {
1443 EXT4_ERROR_INODE(inode,
1444 "ex %p == NULL or eh %p == NULL", ex, eh);
1445 return -EIO;
1446 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001447
1448 if (depth == 0) {
1449 /* there is no tree at all */
1450 return 0;
1451 }
1452
1453 if (ex != EXT_FIRST_EXTENT(eh)) {
1454 /* we correct tree if first leaf got modified only */
1455 return 0;
1456 }
1457
1458 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001459 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001460 */
1461 k = depth - 1;
1462 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001463 err = ext4_ext_get_access(handle, inode, path + k);
1464 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001465 return err;
1466 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001467 err = ext4_ext_dirty(handle, inode, path + k);
1468 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001469 return err;
1470
1471 while (k--) {
1472 /* change all left-side indexes */
1473 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1474 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001475 err = ext4_ext_get_access(handle, inode, path + k);
1476 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001477 break;
1478 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001479 err = ext4_ext_dirty(handle, inode, path + k);
1480 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001481 break;
1482 }
1483
1484 return err;
1485}
1486
Akira Fujita748de672009-06-17 19:24:03 -04001487int
Alex Tomasa86c6182006-10-11 01:21:03 -07001488ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1489 struct ext4_extent *ex2)
1490{
Amit Arora749269f2007-07-18 09:02:56 -04001491 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001492
1493 /*
1494 * Make sure that either both extents are uninitialized, or
1495 * both are _not_.
1496 */
1497 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1498 return 0;
1499
Amit Arora749269f2007-07-18 09:02:56 -04001500 if (ext4_ext_is_uninitialized(ex1))
1501 max_len = EXT_UNINIT_MAX_LEN;
1502 else
1503 max_len = EXT_INIT_MAX_LEN;
1504
Amit Aroraa2df2a62007-07-17 21:42:41 -04001505 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1506 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1507
1508 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001509 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001510 return 0;
1511
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001512 /*
1513 * To allow future support for preallocated extents to be added
1514 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001515 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001516 */
Amit Arora749269f2007-07-18 09:02:56 -04001517 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001518 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001519#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001520 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001521 return 0;
1522#endif
1523
Theodore Ts'obf89d162010-10-27 21:30:14 -04001524 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001525 return 1;
1526 return 0;
1527}
1528
1529/*
Amit Arora56055d32007-07-17 21:42:38 -04001530 * This function tries to merge the "ex" extent to the next extent in the tree.
1531 * It always tries to merge towards right. If you want to merge towards
1532 * left, pass "ex - 1" as argument instead of "ex".
1533 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1534 * 1 if they got merged.
1535 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001536static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001537 struct ext4_ext_path *path,
1538 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001539{
1540 struct ext4_extent_header *eh;
1541 unsigned int depth, len;
1542 int merge_done = 0;
1543 int uninitialized = 0;
1544
1545 depth = ext_depth(inode);
1546 BUG_ON(path[depth].p_hdr == NULL);
1547 eh = path[depth].p_hdr;
1548
1549 while (ex < EXT_LAST_EXTENT(eh)) {
1550 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1551 break;
1552 /* merge with next extent! */
1553 if (ext4_ext_is_uninitialized(ex))
1554 uninitialized = 1;
1555 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1556 + ext4_ext_get_actual_len(ex + 1));
1557 if (uninitialized)
1558 ext4_ext_mark_uninitialized(ex);
1559
1560 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1561 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1562 * sizeof(struct ext4_extent);
1563 memmove(ex + 1, ex + 2, len);
1564 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001565 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001566 merge_done = 1;
1567 WARN_ON(eh->eh_entries == 0);
1568 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001569 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001570 }
1571
1572 return merge_done;
1573}
1574
1575/*
Yongqiang Yang197217a2011-05-03 11:45:29 -04001576 * This function tries to merge the @ex extent to neighbours in the tree.
1577 * return 1 if merge left else 0.
1578 */
1579static int ext4_ext_try_to_merge(struct inode *inode,
1580 struct ext4_ext_path *path,
1581 struct ext4_extent *ex) {
1582 struct ext4_extent_header *eh;
1583 unsigned int depth;
1584 int merge_done = 0;
1585 int ret = 0;
1586
1587 depth = ext_depth(inode);
1588 BUG_ON(path[depth].p_hdr == NULL);
1589 eh = path[depth].p_hdr;
1590
1591 if (ex > EXT_FIRST_EXTENT(eh))
1592 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1593
1594 if (!merge_done)
1595 ret = ext4_ext_try_to_merge_right(inode, path, ex);
1596
1597 return ret;
1598}
1599
1600/*
Amit Arora25d14f92007-05-24 13:04:13 -04001601 * check if a portion of the "newext" extent overlaps with an
1602 * existing extent.
1603 *
1604 * If there is an overlap discovered, it updates the length of the newext
1605 * such that there will be no overlap, and then returns 1.
1606 * If there is no overlap found, it returns 0.
1607 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001608static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1609 struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001610 struct ext4_extent *newext,
1611 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001612{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001613 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001614 unsigned int depth, len1;
1615 unsigned int ret = 0;
1616
1617 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001618 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001619 depth = ext_depth(inode);
1620 if (!path[depth].p_ext)
1621 goto out;
1622 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001623 b2 &= ~(sbi->s_cluster_ratio - 1);
Amit Arora25d14f92007-05-24 13:04:13 -04001624
1625 /*
1626 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001627 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001628 */
1629 if (b2 < b1) {
1630 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001631 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001632 goto out;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001633 b2 &= ~(sbi->s_cluster_ratio - 1);
Amit Arora25d14f92007-05-24 13:04:13 -04001634 }
1635
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001636 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001637 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001638 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001639 newext->ee_len = cpu_to_le16(len1);
1640 ret = 1;
1641 }
1642
1643 /* check for overlap */
1644 if (b1 + len1 > b2) {
1645 newext->ee_len = cpu_to_le16(b2 - b1);
1646 ret = 1;
1647 }
1648out:
1649 return ret;
1650}
1651
1652/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001653 * ext4_ext_insert_extent:
1654 * tries to merge requsted extent into the existing extent or
1655 * inserts requested extent as new one into the tree,
1656 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001657 */
1658int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1659 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04001660 struct ext4_extent *newext, int flag)
Alex Tomasa86c6182006-10-11 01:21:03 -07001661{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001662 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001663 struct ext4_extent *ex, *fex;
1664 struct ext4_extent *nearex; /* nearest extent */
1665 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001666 int depth, len, err;
1667 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001668 unsigned uninitialized = 0;
Allison Henderson55f020d2011-05-25 07:41:26 -04001669 int flags = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001670
Frank Mayhar273df552010-03-02 11:46:09 -05001671 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1672 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1673 return -EIO;
1674 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001675 depth = ext_depth(inode);
1676 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001677 if (unlikely(path[depth].p_hdr == NULL)) {
1678 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1679 return -EIO;
1680 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001681
1682 /* try to insert block into found extent and return */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001683 if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
Mingming Cao00314622009-09-28 15:49:08 -04001684 && ext4_can_extents_be_merged(inode, ex, newext)) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04001685 ext_debug("append [%d]%d block to %u:[%d]%d (from %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04001686 ext4_ext_is_uninitialized(newext),
1687 ext4_ext_get_actual_len(newext),
1688 le32_to_cpu(ex->ee_block),
1689 ext4_ext_is_uninitialized(ex),
1690 ext4_ext_get_actual_len(ex),
1691 ext4_ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001692 err = ext4_ext_get_access(handle, inode, path + depth);
1693 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001694 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001695
1696 /*
1697 * ext4_can_extents_be_merged should have checked that either
1698 * both extents are uninitialized, or both aren't. Thus we
1699 * need to check only one of them here.
1700 */
1701 if (ext4_ext_is_uninitialized(ex))
1702 uninitialized = 1;
1703 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1704 + ext4_ext_get_actual_len(newext));
1705 if (uninitialized)
1706 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001707 eh = path[depth].p_hdr;
1708 nearex = ex;
1709 goto merge;
1710 }
1711
Alex Tomasa86c6182006-10-11 01:21:03 -07001712 depth = ext_depth(inode);
1713 eh = path[depth].p_hdr;
1714 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1715 goto has_space;
1716
1717 /* probably next leaf has space for us? */
1718 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04001719 next = EXT_MAX_BLOCKS;
1720 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
Robin Dong57187892011-07-23 21:49:07 -04001721 next = ext4_ext_next_leaf_block(path);
Robin Dong598dbdf2011-07-11 18:24:01 -04001722 if (next != EXT_MAX_BLOCKS) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04001723 ext_debug("next leaf block - %u\n", next);
Alex Tomasa86c6182006-10-11 01:21:03 -07001724 BUG_ON(npath != NULL);
1725 npath = ext4_ext_find_extent(inode, next, NULL);
1726 if (IS_ERR(npath))
1727 return PTR_ERR(npath);
1728 BUG_ON(npath->p_depth != path->p_depth);
1729 eh = npath[depth].p_hdr;
1730 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001731 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001732 le16_to_cpu(eh->eh_entries));
1733 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04001734 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07001735 }
1736 ext_debug("next leaf has no free space(%d,%d)\n",
1737 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1738 }
1739
1740 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001741 * There is no free space in the found leaf.
1742 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001743 */
Allison Henderson55f020d2011-05-25 07:41:26 -04001744 if (flag & EXT4_GET_BLOCKS_PUNCH_OUT_EXT)
1745 flags = EXT4_MB_USE_ROOT_BLOCKS;
1746 err = ext4_ext_create_new_leaf(handle, inode, flags, path, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001747 if (err)
1748 goto cleanup;
1749 depth = ext_depth(inode);
1750 eh = path[depth].p_hdr;
1751
1752has_space:
1753 nearex = path[depth].p_ext;
1754
Avantika Mathur7e028972006-12-06 20:41:33 -08001755 err = ext4_ext_get_access(handle, inode, path + depth);
1756 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001757 goto cleanup;
1758
1759 if (!nearex) {
1760 /* there is no extent in this leaf, create first one */
Yongqiang Yang32de6752011-11-01 18:56:41 -04001761 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001762 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001763 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001764 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001765 ext4_ext_get_actual_len(newext));
Eric Gouriou80e675f2011-10-27 11:52:18 -04001766 nearex = EXT_FIRST_EXTENT(eh);
1767 } else {
1768 if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001769 > le32_to_cpu(nearex->ee_block)) {
Eric Gouriou80e675f2011-10-27 11:52:18 -04001770 /* Insert after */
Yongqiang Yang32de6752011-11-01 18:56:41 -04001771 ext_debug("insert %u:%llu:[%d]%d before: "
1772 "nearest %p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001773 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001774 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001775 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001776 ext4_ext_get_actual_len(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04001777 nearex);
1778 nearex++;
1779 } else {
1780 /* Insert before */
1781 BUG_ON(newext->ee_block == nearex->ee_block);
Yongqiang Yang32de6752011-11-01 18:56:41 -04001782 ext_debug("insert %u:%llu:[%d]%d after: "
1783 "nearest %p\n",
Eric Gouriou80e675f2011-10-27 11:52:18 -04001784 le32_to_cpu(newext->ee_block),
1785 ext4_ext_pblock(newext),
1786 ext4_ext_is_uninitialized(newext),
1787 ext4_ext_get_actual_len(newext),
1788 nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001789 }
Eric Gouriou80e675f2011-10-27 11:52:18 -04001790 len = EXT_LAST_EXTENT(eh) - nearex + 1;
1791 if (len > 0) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04001792 ext_debug("insert %u:%llu:[%d]%d: "
Eric Gouriou80e675f2011-10-27 11:52:18 -04001793 "move %d extents from 0x%p to 0x%p\n",
1794 le32_to_cpu(newext->ee_block),
1795 ext4_ext_pblock(newext),
1796 ext4_ext_is_uninitialized(newext),
1797 ext4_ext_get_actual_len(newext),
1798 len, nearex, nearex + 1);
1799 memmove(nearex + 1, nearex,
1800 len * sizeof(struct ext4_extent));
1801 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001802 }
1803
Marcin Slusarze8546d02008-04-17 10:38:59 -04001804 le16_add_cpu(&eh->eh_entries, 1);
Eric Gouriou80e675f2011-10-27 11:52:18 -04001805 path[depth].p_ext = nearex;
Alex Tomasa86c6182006-10-11 01:21:03 -07001806 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001807 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001808 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001809
1810merge:
1811 /* try to merge extents to the right */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001812 if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
Mingming Cao00314622009-09-28 15:49:08 -04001813 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001814
1815 /* try to merge extents to the left */
1816
1817 /* time to correct all indexes above */
1818 err = ext4_ext_correct_indexes(handle, inode, path);
1819 if (err)
1820 goto cleanup;
1821
1822 err = ext4_ext_dirty(handle, inode, path + depth);
1823
1824cleanup:
1825 if (npath) {
1826 ext4_ext_drop_refs(npath);
1827 kfree(npath);
1828 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001829 ext4_ext_invalidate_cache(inode);
1830 return err;
1831}
1832
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001833static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1834 ext4_lblk_t num, ext_prepare_callback func,
1835 void *cbdata)
Eric Sandeen6873fa02008-10-07 00:46:36 -04001836{
1837 struct ext4_ext_path *path = NULL;
1838 struct ext4_ext_cache cbex;
1839 struct ext4_extent *ex;
1840 ext4_lblk_t next, start = 0, end = 0;
1841 ext4_lblk_t last = block + num;
1842 int depth, exists, err = 0;
1843
1844 BUG_ON(func == NULL);
1845 BUG_ON(inode == NULL);
1846
Lukas Czernerf17722f2011-06-06 00:05:17 -04001847 while (block < last && block != EXT_MAX_BLOCKS) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04001848 num = last - block;
1849 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001850 down_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001851 path = ext4_ext_find_extent(inode, block, path);
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001852 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001853 if (IS_ERR(path)) {
1854 err = PTR_ERR(path);
1855 path = NULL;
1856 break;
1857 }
1858
1859 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05001860 if (unlikely(path[depth].p_hdr == NULL)) {
1861 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1862 err = -EIO;
1863 break;
1864 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04001865 ex = path[depth].p_ext;
1866 next = ext4_ext_next_allocated_block(path);
1867
1868 exists = 0;
1869 if (!ex) {
1870 /* there is no extent yet, so try to allocate
1871 * all requested space */
1872 start = block;
1873 end = block + num;
1874 } else if (le32_to_cpu(ex->ee_block) > block) {
1875 /* need to allocate space before found extent */
1876 start = block;
1877 end = le32_to_cpu(ex->ee_block);
1878 if (block + num < end)
1879 end = block + num;
1880 } else if (block >= le32_to_cpu(ex->ee_block)
1881 + ext4_ext_get_actual_len(ex)) {
1882 /* need to allocate space after found extent */
1883 start = block;
1884 end = block + num;
1885 if (end >= next)
1886 end = next;
1887 } else if (block >= le32_to_cpu(ex->ee_block)) {
1888 /*
1889 * some part of requested space is covered
1890 * by found extent
1891 */
1892 start = block;
1893 end = le32_to_cpu(ex->ee_block)
1894 + ext4_ext_get_actual_len(ex);
1895 if (block + num < end)
1896 end = block + num;
1897 exists = 1;
1898 } else {
1899 BUG();
1900 }
1901 BUG_ON(end <= start);
1902
1903 if (!exists) {
1904 cbex.ec_block = start;
1905 cbex.ec_len = end - start;
1906 cbex.ec_start = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04001907 } else {
1908 cbex.ec_block = le32_to_cpu(ex->ee_block);
1909 cbex.ec_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001910 cbex.ec_start = ext4_ext_pblock(ex);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001911 }
1912
Frank Mayhar273df552010-03-02 11:46:09 -05001913 if (unlikely(cbex.ec_len == 0)) {
1914 EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
1915 err = -EIO;
1916 break;
1917 }
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04001918 err = func(inode, next, &cbex, ex, cbdata);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001919 ext4_ext_drop_refs(path);
1920
1921 if (err < 0)
1922 break;
1923
1924 if (err == EXT_REPEAT)
1925 continue;
1926 else if (err == EXT_BREAK) {
1927 err = 0;
1928 break;
1929 }
1930
1931 if (ext_depth(inode) != depth) {
1932 /* depth was changed. we have to realloc path */
1933 kfree(path);
1934 path = NULL;
1935 }
1936
1937 block = cbex.ec_block + cbex.ec_len;
1938 }
1939
1940 if (path) {
1941 ext4_ext_drop_refs(path);
1942 kfree(path);
1943 }
1944
1945 return err;
1946}
1947
Avantika Mathur09b88252006-12-06 20:41:36 -08001948static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001949ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05001950 __u32 len, ext4_fsblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07001951{
1952 struct ext4_ext_cache *cex;
1953 BUG_ON(len == 0);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001954 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Aditya Kalid8990242011-09-09 19:18:51 -04001955 trace_ext4_ext_put_in_cache(inode, block, len, start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001956 cex = &EXT4_I(inode)->i_cached_extent;
Alex Tomasa86c6182006-10-11 01:21:03 -07001957 cex->ec_block = block;
1958 cex->ec_len = len;
1959 cex->ec_start = start;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001960 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001961}
1962
1963/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001964 * ext4_ext_put_gap_in_cache:
1965 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07001966 * and cache this gap
1967 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001968static void
Alex Tomasa86c6182006-10-11 01:21:03 -07001969ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001970 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07001971{
1972 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001973 unsigned long len;
1974 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001975 struct ext4_extent *ex;
1976
1977 ex = path[depth].p_ext;
1978 if (ex == NULL) {
1979 /* there is no extent yet, so gap is [0;-] */
1980 lblock = 0;
Lukas Czernerf17722f2011-06-06 00:05:17 -04001981 len = EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001982 ext_debug("cache gap(whole file):");
1983 } else if (block < le32_to_cpu(ex->ee_block)) {
1984 lblock = block;
1985 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05001986 ext_debug("cache gap(before): %u [%u:%u]",
1987 block,
1988 le32_to_cpu(ex->ee_block),
1989 ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07001990 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001991 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001992 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001993 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001994 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001995
1996 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05001997 ext_debug("cache gap(after): [%u:%u] %u",
1998 le32_to_cpu(ex->ee_block),
1999 ext4_ext_get_actual_len(ex),
2000 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002001 BUG_ON(next == lblock);
2002 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07002003 } else {
2004 lblock = len = 0;
2005 BUG();
2006 }
2007
Eric Sandeenbba90742008-01-28 23:58:27 -05002008 ext_debug(" -> %u:%lu\n", lblock, len);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002009 ext4_ext_put_in_cache(inode, lblock, len, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002010}
2011
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002012/*
Robin Dongb7ca1e82011-07-23 21:53:25 -04002013 * ext4_ext_check_cache()
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002014 * Checks to see if the given block is in the cache.
2015 * If it is, the cached extent is stored in the given
2016 * cache extent pointer. If the cached extent is a hole,
2017 * this routine should be used instead of
2018 * ext4_ext_in_cache if the calling function needs to
2019 * know the size of the hole.
2020 *
2021 * @inode: The files inode
2022 * @block: The block to look for in the cache
2023 * @ex: Pointer where the cached extent will be stored
2024 * if it contains block
2025 *
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002026 * Return 0 if cache is invalid; 1 if the cache is valid
2027 */
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002028static int ext4_ext_check_cache(struct inode *inode, ext4_lblk_t block,
2029 struct ext4_ext_cache *ex){
Alex Tomasa86c6182006-10-11 01:21:03 -07002030 struct ext4_ext_cache *cex;
Vivek Haldar77f41352011-05-22 21:24:16 -04002031 struct ext4_sb_info *sbi;
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002032 int ret = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002033
Theodore Ts'o60e66792010-05-17 07:00:00 -04002034 /*
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002035 * We borrow i_block_reservation_lock to protect i_cached_extent
2036 */
2037 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07002038 cex = &EXT4_I(inode)->i_cached_extent;
Vivek Haldar77f41352011-05-22 21:24:16 -04002039 sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002040
2041 /* has cache valid data? */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002042 if (cex->ec_len == 0)
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002043 goto errout;
Alex Tomasa86c6182006-10-11 01:21:03 -07002044
Akinobu Mita731eb1a2010-03-03 23:55:01 -05002045 if (in_range(block, cex->ec_block, cex->ec_len)) {
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002046 memcpy(ex, cex, sizeof(struct ext4_ext_cache));
Eric Sandeenbba90742008-01-28 23:58:27 -05002047 ext_debug("%u cached by %u:%u:%llu\n",
2048 block,
2049 cex->ec_block, cex->ec_len, cex->ec_start);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002050 ret = 1;
Alex Tomasa86c6182006-10-11 01:21:03 -07002051 }
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002052errout:
Vivek Haldar77f41352011-05-22 21:24:16 -04002053 if (!ret)
2054 sbi->extent_cache_misses++;
2055 else
2056 sbi->extent_cache_hits++;
Aditya Kalid8990242011-09-09 19:18:51 -04002057 trace_ext4_ext_in_cache(inode, block, ret);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002058 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
2059 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07002060}
2061
2062/*
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002063 * ext4_ext_in_cache()
2064 * Checks to see if the given block is in the cache.
2065 * If it is, the cached extent is stored in the given
2066 * extent pointer.
2067 *
2068 * @inode: The files inode
2069 * @block: The block to look for in the cache
2070 * @ex: Pointer where the cached extent will be stored
2071 * if it contains block
2072 *
2073 * Return 0 if cache is invalid; 1 if the cache is valid
2074 */
2075static int
2076ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
2077 struct ext4_extent *ex)
2078{
2079 struct ext4_ext_cache cex;
2080 int ret = 0;
2081
2082 if (ext4_ext_check_cache(inode, block, &cex)) {
2083 ex->ee_block = cpu_to_le32(cex.ec_block);
2084 ext4_ext_store_pblock(ex, cex.ec_start);
2085 ex->ee_len = cpu_to_le16(cex.ec_len);
2086 ret = 1;
2087 }
2088
2089 return ret;
2090}
2091
2092
2093/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002094 * ext4_ext_rm_idx:
2095 * removes index from the index block.
Alex Tomasa86c6182006-10-11 01:21:03 -07002096 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002097static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07002098 struct ext4_ext_path *path)
2099{
Alex Tomasa86c6182006-10-11 01:21:03 -07002100 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002101 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002102
2103 /* free index block */
2104 path--;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002105 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002106 if (unlikely(path->p_hdr->eh_entries == 0)) {
2107 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2108 return -EIO;
2109 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002110 err = ext4_ext_get_access(handle, inode, path);
2111 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002112 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002113
2114 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2115 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2116 len *= sizeof(struct ext4_extent_idx);
2117 memmove(path->p_idx, path->p_idx + 1, len);
2118 }
2119
Marcin Slusarze8546d02008-04-17 10:38:59 -04002120 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002121 err = ext4_ext_dirty(handle, inode, path);
2122 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002123 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002124 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Aditya Kalid8990242011-09-09 19:18:51 -04002125 trace_ext4_ext_rm_idx(inode, leaf);
2126
Peter Huewe7dc57612011-02-21 21:01:42 -05002127 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002128 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Alex Tomasa86c6182006-10-11 01:21:03 -07002129 return err;
2130}
2131
2132/*
Mingming Caoee12b632008-08-19 22:16:05 -04002133 * ext4_ext_calc_credits_for_single_extent:
2134 * This routine returns max. credits that needed to insert an extent
2135 * to the extent tree.
2136 * When pass the actual path, the caller should calculate credits
2137 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002138 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002139int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002140 struct ext4_ext_path *path)
2141{
Alex Tomasa86c6182006-10-11 01:21:03 -07002142 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002143 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002144 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002145
Alex Tomasa86c6182006-10-11 01:21:03 -07002146 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002147 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002148 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2149
2150 /*
2151 * There are some space in the leaf tree, no
2152 * need to account for leaf block credit
2153 *
2154 * bitmaps and block group descriptor blocks
Tao Madf3ab172011-10-08 15:53:49 -04002155 * and other metadata blocks still need to be
Mingming Caoee12b632008-08-19 22:16:05 -04002156 * accounted.
2157 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002158 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002159 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002160 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002161 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002162 }
2163
Mingming Cao525f4ed2008-08-19 22:15:58 -04002164 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002165}
Alex Tomasa86c6182006-10-11 01:21:03 -07002166
Mingming Caoee12b632008-08-19 22:16:05 -04002167/*
2168 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2169 *
2170 * if nrblocks are fit in a single extent (chunk flag is 1), then
2171 * in the worse case, each tree level index/leaf need to be changed
2172 * if the tree split due to insert a new extent, then the old tree
2173 * index/leaf need to be updated too
2174 *
2175 * If the nrblocks are discontiguous, they could cause
2176 * the whole tree split more than once, but this is really rare.
2177 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002178int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoee12b632008-08-19 22:16:05 -04002179{
2180 int index;
2181 int depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002182
Mingming Caoee12b632008-08-19 22:16:05 -04002183 if (chunk)
2184 index = depth * 2;
2185 else
2186 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002187
Mingming Caoee12b632008-08-19 22:16:05 -04002188 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002189}
2190
2191static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002192 struct ext4_extent *ex,
2193 ext4_fsblk_t *partial_cluster,
2194 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002195{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002196 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002197 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002198 ext4_fsblk_t pblk;
Theodore Ts'oe6362602009-11-23 07:17:05 -05002199 int flags = EXT4_FREE_BLOCKS_FORGET;
Alex Tomasa86c6182006-10-11 01:21:03 -07002200
Alex Tomasc9de5602008-01-29 00:19:52 -05002201 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
Theodore Ts'oe6362602009-11-23 07:17:05 -05002202 flags |= EXT4_FREE_BLOCKS_METADATA;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002203 /*
2204 * For bigalloc file systems, we never free a partial cluster
2205 * at the beginning of the extent. Instead, we make a note
2206 * that we tried freeing the cluster, and check to see if we
2207 * need to free it on a subsequent call to ext4_remove_blocks,
2208 * or at the end of the ext4_truncate() operation.
2209 */
2210 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
2211
Aditya Kalid8990242011-09-09 19:18:51 -04002212 trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002213 /*
2214 * If we have a partial cluster, and it's different from the
2215 * cluster of the last block, we need to explicitly free the
2216 * partial cluster here.
2217 */
2218 pblk = ext4_ext_pblock(ex) + ee_len - 1;
2219 if (*partial_cluster && (EXT4_B2C(sbi, pblk) != *partial_cluster)) {
2220 ext4_free_blocks(handle, inode, NULL,
2221 EXT4_C2B(sbi, *partial_cluster),
2222 sbi->s_cluster_ratio, flags);
2223 *partial_cluster = 0;
2224 }
2225
Alex Tomasa86c6182006-10-11 01:21:03 -07002226#ifdef EXTENTS_STATS
2227 {
2228 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002229 spin_lock(&sbi->s_ext_stats_lock);
2230 sbi->s_ext_blocks += ee_len;
2231 sbi->s_ext_extents++;
2232 if (ee_len < sbi->s_ext_min)
2233 sbi->s_ext_min = ee_len;
2234 if (ee_len > sbi->s_ext_max)
2235 sbi->s_ext_max = ee_len;
2236 if (ext_depth(inode) > sbi->s_depth_max)
2237 sbi->s_depth_max = ext_depth(inode);
2238 spin_unlock(&sbi->s_ext_stats_lock);
2239 }
2240#endif
2241 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002242 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002243 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002244 ext4_lblk_t num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002245
Amit Aroraa2df2a62007-07-17 21:42:41 -04002246 num = le32_to_cpu(ex->ee_block) + ee_len - from;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002247 pblk = ext4_ext_pblock(ex) + ee_len - num;
2248 ext_debug("free last %u blocks starting %llu\n", num, pblk);
2249 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
2250 /*
2251 * If the block range to be freed didn't start at the
2252 * beginning of a cluster, and we removed the entire
2253 * extent, save the partial cluster here, since we
2254 * might need to delete if we determine that the
2255 * truncate operation has removed all of the blocks in
2256 * the cluster.
2257 */
2258 if (pblk & (sbi->s_cluster_ratio - 1) &&
2259 (ee_len == num))
2260 *partial_cluster = EXT4_B2C(sbi, pblk);
2261 else
2262 *partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002263 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002264 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002265 /* head removal */
2266 ext4_lblk_t num;
2267 ext4_fsblk_t start;
2268
2269 num = to - from;
2270 start = ext4_ext_pblock(ex);
2271
2272 ext_debug("free first %u blocks starting %llu\n", num, start);
H Hartley Sweetenee90d572011-10-18 11:01:51 -04002273 ext4_free_blocks(handle, inode, NULL, start, num, flags);
Allison Hendersond583fb82011-05-25 07:41:43 -04002274
Alex Tomasa86c6182006-10-11 01:21:03 -07002275 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002276 printk(KERN_INFO "strange request: removal(2) "
2277 "%u-%u from %u:%u\n",
2278 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002279 }
2280 return 0;
2281}
2282
Allison Hendersond583fb82011-05-25 07:41:43 -04002283
2284/*
2285 * ext4_ext_rm_leaf() Removes the extents associated with the
2286 * blocks appearing between "start" and "end", and splits the extents
2287 * if "start" and "end" appear in the same extent
2288 *
2289 * @handle: The journal handle
2290 * @inode: The files inode
2291 * @path: The path to the leaf
2292 * @start: The first block to remove
2293 * @end: The last block to remove
2294 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002295static int
2296ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002297 struct ext4_ext_path *path, ext4_fsblk_t *partial_cluster,
2298 ext4_lblk_t start, ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002299{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002300 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002301 int err = 0, correct_index = 0;
2302 int depth = ext_depth(inode), credits;
2303 struct ext4_extent_header *eh;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002304 ext4_lblk_t a, b;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002305 unsigned num;
2306 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002307 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002308 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002309 struct ext4_extent *ex;
2310
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002311 /* the header must be checked already in ext4_ext_remove_space() */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002312 ext_debug("truncate since %u in leaf\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002313 if (!path[depth].p_hdr)
2314 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2315 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002316 if (unlikely(path[depth].p_hdr == NULL)) {
2317 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2318 return -EIO;
2319 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002320 /* find where to start removing */
2321 ex = EXT_LAST_EXTENT(eh);
2322
2323 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002324 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002325
Aditya Kalid8990242011-09-09 19:18:51 -04002326 trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster);
2327
Alex Tomasa86c6182006-10-11 01:21:03 -07002328 while (ex >= EXT_FIRST_EXTENT(eh) &&
2329 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002330
2331 if (ext4_ext_is_uninitialized(ex))
2332 uninitialized = 1;
2333 else
2334 uninitialized = 0;
2335
Mingming553f9002009-09-18 13:34:55 -04002336 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2337 uninitialized, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002338 path[depth].p_ext = ex;
2339
2340 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002341 b = ex_ee_block+ex_ee_len - 1 < end ?
2342 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002343
2344 ext_debug(" border %u:%u\n", a, b);
2345
Allison Hendersond583fb82011-05-25 07:41:43 -04002346 /* If this extent is beyond the end of the hole, skip it */
2347 if (end <= ex_ee_block) {
2348 ex--;
2349 ex_ee_block = le32_to_cpu(ex->ee_block);
2350 ex_ee_len = ext4_ext_get_actual_len(ex);
2351 continue;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002352 } else if (b != ex_ee_block + ex_ee_len - 1) {
2353 EXT4_ERROR_INODE(inode," bad truncate %u:%u\n",
2354 start, end);
2355 err = -EIO;
2356 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002357 } else if (a != ex_ee_block) {
2358 /* remove tail of the extent */
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002359 num = a - ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002360 } else {
2361 /* remove whole extent: excellent! */
Alex Tomasa86c6182006-10-11 01:21:03 -07002362 num = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002363 }
Theodore Ts'o34071da2008-08-01 21:59:19 -04002364 /*
2365 * 3 for leaf, sb, and inode plus 2 (bmap and group
2366 * descriptor) for each block group; assume two block
2367 * groups plus ex_ee_len/blocks_per_block_group for
2368 * the worst case
2369 */
2370 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002371 if (ex == EXT_FIRST_EXTENT(eh)) {
2372 correct_index = 1;
2373 credits += (ext_depth(inode)) + 1;
2374 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002375 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002376
Jan Kara487caee2009-08-17 22:17:20 -04002377 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
Shen Feng9102e4f2008-07-11 19:27:31 -04002378 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002379 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002380
2381 err = ext4_ext_get_access(handle, inode, path + depth);
2382 if (err)
2383 goto out;
2384
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002385 err = ext4_remove_blocks(handle, inode, ex, partial_cluster,
2386 a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002387 if (err)
2388 goto out;
2389
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002390 if (num == 0)
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002391 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002392 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002393
Alex Tomasa86c6182006-10-11 01:21:03 -07002394 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002395 /*
2396 * Do not mark uninitialized if all the blocks in the
2397 * extent have been removed.
2398 */
2399 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002400 ext4_ext_mark_uninitialized(ex);
Allison Hendersond583fb82011-05-25 07:41:43 -04002401 /*
2402 * If the extent was completely released,
2403 * we need to remove it from the leaf
2404 */
2405 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002406 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002407 /*
2408 * For hole punching, we need to scoot all the
2409 * extents up when an extent is removed so that
2410 * we dont have blank extents in the middle
2411 */
2412 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2413 sizeof(struct ext4_extent));
2414
2415 /* Now get rid of the one at the end */
2416 memset(EXT_LAST_EXTENT(eh), 0,
2417 sizeof(struct ext4_extent));
2418 }
2419 le16_add_cpu(&eh->eh_entries, -1);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002420 } else
2421 *partial_cluster = 0;
Allison Hendersond583fb82011-05-25 07:41:43 -04002422
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002423 err = ext4_ext_dirty(handle, inode, path + depth);
2424 if (err)
2425 goto out;
2426
Yongqiang Yangbf52c6f2011-11-01 18:59:26 -04002427 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002428 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002429 ex--;
2430 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002431 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002432 }
2433
2434 if (correct_index && eh->eh_entries)
2435 err = ext4_ext_correct_indexes(handle, inode, path);
2436
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002437 /*
2438 * If there is still a entry in the leaf node, check to see if
2439 * it references the partial cluster. This is the only place
2440 * where it could; if it doesn't, we can free the cluster.
2441 */
2442 if (*partial_cluster && ex >= EXT_FIRST_EXTENT(eh) &&
2443 (EXT4_B2C(sbi, ext4_ext_pblock(ex) + ex_ee_len - 1) !=
2444 *partial_cluster)) {
2445 int flags = EXT4_FREE_BLOCKS_FORGET;
2446
2447 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2448 flags |= EXT4_FREE_BLOCKS_METADATA;
2449
2450 ext4_free_blocks(handle, inode, NULL,
2451 EXT4_C2B(sbi, *partial_cluster),
2452 sbi->s_cluster_ratio, flags);
2453 *partial_cluster = 0;
2454 }
2455
Alex Tomasa86c6182006-10-11 01:21:03 -07002456 /* if this leaf is free, then we should
2457 * remove it from index block above */
2458 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2459 err = ext4_ext_rm_idx(handle, inode, path + depth);
2460
2461out:
2462 return err;
2463}
2464
2465/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002466 * ext4_ext_more_to_rm:
2467 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002468 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002469static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002470ext4_ext_more_to_rm(struct ext4_ext_path *path)
2471{
2472 BUG_ON(path->p_idx == NULL);
2473
2474 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2475 return 0;
2476
2477 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002478 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002479 * so we have to consider current index for truncation
2480 */
2481 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2482 return 0;
2483 return 1;
2484}
2485
Allison Hendersonc6a03712011-07-17 23:21:03 -04002486static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07002487{
2488 struct super_block *sb = inode->i_sb;
2489 int depth = ext_depth(inode);
2490 struct ext4_ext_path *path;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002491 ext4_fsblk_t partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002492 handle_t *handle;
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002493 int i, err;
Alex Tomasa86c6182006-10-11 01:21:03 -07002494
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002495 ext_debug("truncate since %u\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002496
2497 /* probably first extent we're gonna free will be last in block */
2498 handle = ext4_journal_start(inode, depth + 1);
2499 if (IS_ERR(handle))
2500 return PTR_ERR(handle);
2501
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002502again:
Alex Tomasa86c6182006-10-11 01:21:03 -07002503 ext4_ext_invalidate_cache(inode);
2504
Aditya Kalid8990242011-09-09 19:18:51 -04002505 trace_ext4_ext_remove_space(inode, start, depth);
2506
Alex Tomasa86c6182006-10-11 01:21:03 -07002507 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002508 * We start scanning from right side, freeing all the blocks
2509 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002510 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002511 depth = ext_depth(inode);
Josef Bacik216553c2008-04-29 22:02:02 -04002512 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07002513 if (path == NULL) {
2514 ext4_journal_stop(handle);
2515 return -ENOMEM;
2516 }
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002517 path[0].p_depth = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -07002518 path[0].p_hdr = ext_inode_hdr(inode);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002519 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002520 err = -EIO;
2521 goto out;
2522 }
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002523 i = err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002524
2525 while (i >= 0 && err == 0) {
2526 if (i == depth) {
2527 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04002528 err = ext4_ext_rm_leaf(handle, inode, path,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002529 &partial_cluster, start,
2530 EXT_MAX_BLOCKS - 1);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002531 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002532 brelse(path[i].p_bh);
2533 path[i].p_bh = NULL;
2534 i--;
2535 continue;
2536 }
2537
2538 /* this is index block */
2539 if (!path[i].p_hdr) {
2540 ext_debug("initialize header\n");
2541 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002542 }
2543
Alex Tomasa86c6182006-10-11 01:21:03 -07002544 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002545 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002546 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2547 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2548 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2549 path[i].p_hdr,
2550 le16_to_cpu(path[i].p_hdr->eh_entries));
2551 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002552 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002553 path[i].p_idx--;
2554 }
2555
2556 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2557 i, EXT_FIRST_INDEX(path[i].p_hdr),
2558 path[i].p_idx);
2559 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002560 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002561 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002562 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04002563 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002564 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'obf89d162010-10-27 21:30:14 -04002565 bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002566 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002567 /* should we reset i_size? */
2568 err = -EIO;
2569 break;
2570 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002571 if (WARN_ON(i + 1 > depth)) {
2572 err = -EIO;
2573 break;
2574 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002575 if (ext4_ext_check(inode, ext_block_hdr(bh),
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002576 depth - i - 1)) {
2577 err = -EIO;
2578 break;
2579 }
2580 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002581
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002582 /* save actual number of indexes since this
2583 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002584 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2585 i++;
2586 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002587 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002588 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002589 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002590 * handle must be already prepared by the
2591 * truncatei_leaf() */
2592 err = ext4_ext_rm_idx(handle, inode, path + i);
2593 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002594 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002595 brelse(path[i].p_bh);
2596 path[i].p_bh = NULL;
2597 i--;
2598 ext_debug("return to level %d\n", i);
2599 }
2600 }
2601
Aditya Kalid8990242011-09-09 19:18:51 -04002602 trace_ext4_ext_remove_space_done(inode, start, depth, partial_cluster,
2603 path->p_hdr->eh_entries);
2604
Aditya Kali7b415bf2011-09-09 19:04:51 -04002605 /* If we still have something in the partial cluster and we have removed
2606 * even the first extent, then we should free the blocks in the partial
2607 * cluster as well. */
2608 if (partial_cluster && path->p_hdr->eh_entries == 0) {
2609 int flags = EXT4_FREE_BLOCKS_FORGET;
2610
2611 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2612 flags |= EXT4_FREE_BLOCKS_METADATA;
2613
2614 ext4_free_blocks(handle, inode, NULL,
2615 EXT4_C2B(EXT4_SB(sb), partial_cluster),
2616 EXT4_SB(sb)->s_cluster_ratio, flags);
2617 partial_cluster = 0;
2618 }
2619
Alex Tomasa86c6182006-10-11 01:21:03 -07002620 /* TODO: flexible tree reduction should be here */
2621 if (path->p_hdr->eh_entries == 0) {
2622 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002623 * truncate to zero freed all the tree,
2624 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002625 */
2626 err = ext4_ext_get_access(handle, inode, path);
2627 if (err == 0) {
2628 ext_inode_hdr(inode)->eh_depth = 0;
2629 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04002630 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07002631 err = ext4_ext_dirty(handle, inode, path);
2632 }
2633 }
2634out:
Alex Tomasa86c6182006-10-11 01:21:03 -07002635 ext4_ext_drop_refs(path);
2636 kfree(path);
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002637 if (err == -EAGAIN)
2638 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07002639 ext4_journal_stop(handle);
2640
2641 return err;
2642}
2643
2644/*
2645 * called at mount time
2646 */
2647void ext4_ext_init(struct super_block *sb)
2648{
2649 /*
2650 * possible initialization would be here
2651 */
2652
Theodore Ts'o83982b62009-01-06 14:53:16 -05002653 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04002654#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002655 printk(KERN_INFO "EXT4-fs: file extents enabled");
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002656#ifdef AGGRESSIVE_TEST
2657 printk(", aggressive tests");
Alex Tomasa86c6182006-10-11 01:21:03 -07002658#endif
2659#ifdef CHECK_BINSEARCH
2660 printk(", check binsearch");
2661#endif
2662#ifdef EXTENTS_STATS
2663 printk(", stats");
2664#endif
2665 printk("\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04002666#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07002667#ifdef EXTENTS_STATS
2668 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2669 EXT4_SB(sb)->s_ext_min = 1 << 30;
2670 EXT4_SB(sb)->s_ext_max = 0;
2671#endif
2672 }
2673}
2674
2675/*
2676 * called at umount time
2677 */
2678void ext4_ext_release(struct super_block *sb)
2679{
Theodore Ts'o83982b62009-01-06 14:53:16 -05002680 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
Alex Tomasa86c6182006-10-11 01:21:03 -07002681 return;
2682
2683#ifdef EXTENTS_STATS
2684 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2685 struct ext4_sb_info *sbi = EXT4_SB(sb);
2686 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2687 sbi->s_ext_blocks, sbi->s_ext_extents,
2688 sbi->s_ext_blocks / sbi->s_ext_extents);
2689 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2690 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2691 }
2692#endif
2693}
2694
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002695/* FIXME!! we need to try to merge to left or right after zero-out */
2696static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2697{
Lukas Czerner24075182010-10-27 21:30:06 -04002698 ext4_fsblk_t ee_pblock;
2699 unsigned int ee_len;
Jing Zhangb7203032010-05-12 00:00:00 -04002700 int ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002701
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002702 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04002703 ee_pblock = ext4_ext_pblock(ex);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002704
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04002705 ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
Lukas Czerner24075182010-10-27 21:30:06 -04002706 if (ret > 0)
2707 ret = 0;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002708
Lukas Czerner24075182010-10-27 21:30:06 -04002709 return ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002710}
2711
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002712/*
2713 * used by extent splitting.
2714 */
2715#define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
2716 due to ENOSPC */
2717#define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */
2718#define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */
2719
2720/*
2721 * ext4_split_extent_at() splits an extent at given block.
2722 *
2723 * @handle: the journal handle
2724 * @inode: the file inode
2725 * @path: the path to the extent
2726 * @split: the logical block where the extent is splitted.
2727 * @split_flags: indicates if the extent could be zeroout if split fails, and
2728 * the states(init or uninit) of new extents.
2729 * @flags: flags used to insert new extent to extent tree.
2730 *
2731 *
2732 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
2733 * of which are deterimined by split_flag.
2734 *
2735 * There are two cases:
2736 * a> the extent are splitted into two extent.
2737 * b> split is not needed, and just mark the extent.
2738 *
2739 * return 0 on success.
2740 */
2741static int ext4_split_extent_at(handle_t *handle,
2742 struct inode *inode,
2743 struct ext4_ext_path *path,
2744 ext4_lblk_t split,
2745 int split_flag,
2746 int flags)
2747{
2748 ext4_fsblk_t newblock;
2749 ext4_lblk_t ee_block;
2750 struct ext4_extent *ex, newex, orig_ex;
2751 struct ext4_extent *ex2 = NULL;
2752 unsigned int ee_len, depth;
2753 int err = 0;
2754
2755 ext_debug("ext4_split_extents_at: inode %lu, logical"
2756 "block %llu\n", inode->i_ino, (unsigned long long)split);
2757
2758 ext4_ext_show_leaf(inode, path);
2759
2760 depth = ext_depth(inode);
2761 ex = path[depth].p_ext;
2762 ee_block = le32_to_cpu(ex->ee_block);
2763 ee_len = ext4_ext_get_actual_len(ex);
2764 newblock = split - ee_block + ext4_ext_pblock(ex);
2765
2766 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
2767
2768 err = ext4_ext_get_access(handle, inode, path + depth);
2769 if (err)
2770 goto out;
2771
2772 if (split == ee_block) {
2773 /*
2774 * case b: block @split is the block that the extent begins with
2775 * then we just change the state of the extent, and splitting
2776 * is not needed.
2777 */
2778 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2779 ext4_ext_mark_uninitialized(ex);
2780 else
2781 ext4_ext_mark_initialized(ex);
2782
2783 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
2784 ext4_ext_try_to_merge(inode, path, ex);
2785
2786 err = ext4_ext_dirty(handle, inode, path + depth);
2787 goto out;
2788 }
2789
2790 /* case a */
2791 memcpy(&orig_ex, ex, sizeof(orig_ex));
2792 ex->ee_len = cpu_to_le16(split - ee_block);
2793 if (split_flag & EXT4_EXT_MARK_UNINIT1)
2794 ext4_ext_mark_uninitialized(ex);
2795
2796 /*
2797 * path may lead to new leaf, not to original leaf any more
2798 * after ext4_ext_insert_extent() returns,
2799 */
2800 err = ext4_ext_dirty(handle, inode, path + depth);
2801 if (err)
2802 goto fix_extent_len;
2803
2804 ex2 = &newex;
2805 ex2->ee_block = cpu_to_le32(split);
2806 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
2807 ext4_ext_store_pblock(ex2, newblock);
2808 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2809 ext4_ext_mark_uninitialized(ex2);
2810
2811 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
2812 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2813 err = ext4_ext_zeroout(inode, &orig_ex);
2814 if (err)
2815 goto fix_extent_len;
2816 /* update the extent length and mark as initialized */
2817 ex->ee_len = cpu_to_le32(ee_len);
2818 ext4_ext_try_to_merge(inode, path, ex);
2819 err = ext4_ext_dirty(handle, inode, path + depth);
2820 goto out;
2821 } else if (err)
2822 goto fix_extent_len;
2823
2824out:
2825 ext4_ext_show_leaf(inode, path);
2826 return err;
2827
2828fix_extent_len:
2829 ex->ee_len = orig_ex.ee_len;
2830 ext4_ext_dirty(handle, inode, path + depth);
2831 return err;
2832}
2833
2834/*
2835 * ext4_split_extents() splits an extent and mark extent which is covered
2836 * by @map as split_flags indicates
2837 *
2838 * It may result in splitting the extent into multiple extents (upto three)
2839 * There are three possibilities:
2840 * a> There is no split required
2841 * b> Splits in two extents: Split is happening at either end of the extent
2842 * c> Splits in three extents: Somone is splitting in middle of the extent
2843 *
2844 */
2845static int ext4_split_extent(handle_t *handle,
2846 struct inode *inode,
2847 struct ext4_ext_path *path,
2848 struct ext4_map_blocks *map,
2849 int split_flag,
2850 int flags)
2851{
2852 ext4_lblk_t ee_block;
2853 struct ext4_extent *ex;
2854 unsigned int ee_len, depth;
2855 int err = 0;
2856 int uninitialized;
2857 int split_flag1, flags1;
2858
2859 depth = ext_depth(inode);
2860 ex = path[depth].p_ext;
2861 ee_block = le32_to_cpu(ex->ee_block);
2862 ee_len = ext4_ext_get_actual_len(ex);
2863 uninitialized = ext4_ext_is_uninitialized(ex);
2864
2865 if (map->m_lblk + map->m_len < ee_block + ee_len) {
2866 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2867 EXT4_EXT_MAY_ZEROOUT : 0;
2868 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
2869 if (uninitialized)
2870 split_flag1 |= EXT4_EXT_MARK_UNINIT1 |
2871 EXT4_EXT_MARK_UNINIT2;
2872 err = ext4_split_extent_at(handle, inode, path,
2873 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04002874 if (err)
2875 goto out;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002876 }
2877
2878 ext4_ext_drop_refs(path);
2879 path = ext4_ext_find_extent(inode, map->m_lblk, path);
2880 if (IS_ERR(path))
2881 return PTR_ERR(path);
2882
2883 if (map->m_lblk >= ee_block) {
2884 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2885 EXT4_EXT_MAY_ZEROOUT : 0;
2886 if (uninitialized)
2887 split_flag1 |= EXT4_EXT_MARK_UNINIT1;
2888 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2889 split_flag1 |= EXT4_EXT_MARK_UNINIT2;
2890 err = ext4_split_extent_at(handle, inode, path,
2891 map->m_lblk, split_flag1, flags);
2892 if (err)
2893 goto out;
2894 }
2895
2896 ext4_ext_show_leaf(inode, path);
2897out:
2898 return err ? err : map->m_len;
2899}
2900
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002901#define EXT4_EXT_ZERO_LEN 7
Amit Arora56055d32007-07-17 21:42:38 -04002902/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002903 * This function is called by ext4_ext_map_blocks() if someone tries to write
Amit Arora56055d32007-07-17 21:42:38 -04002904 * to an uninitialized extent. It may result in splitting the uninitialized
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002905 * extent into multiple extents (up to three - one initialized and two
Amit Arora56055d32007-07-17 21:42:38 -04002906 * uninitialized).
2907 * There are three possibilities:
2908 * a> There is no split required: Entire extent should be initialized
2909 * b> Splits in two extents: Write is happening at either end of the extent
2910 * c> Splits in three extents: Somone is writing in middle of the extent
Eric Gouriou6f91bc52011-10-27 11:43:23 -04002911 *
2912 * Pre-conditions:
2913 * - The extent pointed to by 'path' is uninitialized.
2914 * - The extent pointed to by 'path' contains a superset
2915 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
2916 *
2917 * Post-conditions on success:
2918 * - the returned value is the number of blocks beyond map->l_lblk
2919 * that are allocated and initialized.
2920 * It is guaranteed to be >= map->m_len.
Amit Arora56055d32007-07-17 21:42:38 -04002921 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002922static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002923 struct inode *inode,
2924 struct ext4_map_blocks *map,
2925 struct ext4_ext_path *path)
Amit Arora56055d32007-07-17 21:42:38 -04002926{
Eric Gouriou6f91bc52011-10-27 11:43:23 -04002927 struct ext4_extent_header *eh;
Yongqiang Yang667eff32011-05-03 12:25:07 -04002928 struct ext4_map_blocks split_map;
2929 struct ext4_extent zero_ex;
2930 struct ext4_extent *ex;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002931 ext4_lblk_t ee_block, eof_block;
Dan Carpenterf85b2872011-10-26 03:42:36 -04002932 unsigned int ee_len, depth;
2933 int allocated;
Amit Arora56055d32007-07-17 21:42:38 -04002934 int err = 0;
Yongqiang Yang667eff32011-05-03 12:25:07 -04002935 int split_flag = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002936
2937 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
2938 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002939 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002940
2941 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
2942 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002943 if (eof_block < map->m_lblk + map->m_len)
2944 eof_block = map->m_lblk + map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04002945
2946 depth = ext_depth(inode);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04002947 eh = path[depth].p_hdr;
Amit Arora56055d32007-07-17 21:42:38 -04002948 ex = path[depth].p_ext;
2949 ee_block = le32_to_cpu(ex->ee_block);
2950 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002951 allocated = ee_len - (map->m_lblk - ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002952
Eric Gouriou6f91bc52011-10-27 11:43:23 -04002953 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
2954
2955 /* Pre-conditions */
2956 BUG_ON(!ext4_ext_is_uninitialized(ex));
2957 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04002958
2959 /*
2960 * Attempt to transfer newly initialized blocks from the currently
2961 * uninitialized extent to its left neighbor. This is much cheaper
2962 * than an insertion followed by a merge as those involve costly
2963 * memmove() calls. This is the common case in steady state for
2964 * workloads doing fallocate(FALLOC_FL_KEEP_SIZE) followed by append
2965 * writes.
2966 *
2967 * Limitations of the current logic:
2968 * - L1: we only deal with writes at the start of the extent.
2969 * The approach could be extended to writes at the end
2970 * of the extent but this scenario was deemed less common.
2971 * - L2: we do not deal with writes covering the whole extent.
2972 * This would require removing the extent if the transfer
2973 * is possible.
2974 * - L3: we only attempt to merge with an extent stored in the
2975 * same extent tree node.
2976 */
2977 if ((map->m_lblk == ee_block) && /*L1*/
2978 (map->m_len < ee_len) && /*L2*/
2979 (ex > EXT_FIRST_EXTENT(eh))) { /*L3*/
2980 struct ext4_extent *prev_ex;
2981 ext4_lblk_t prev_lblk;
2982 ext4_fsblk_t prev_pblk, ee_pblk;
2983 unsigned int prev_len, write_len;
2984
2985 prev_ex = ex - 1;
2986 prev_lblk = le32_to_cpu(prev_ex->ee_block);
2987 prev_len = ext4_ext_get_actual_len(prev_ex);
2988 prev_pblk = ext4_ext_pblock(prev_ex);
2989 ee_pblk = ext4_ext_pblock(ex);
2990 write_len = map->m_len;
2991
2992 /*
2993 * A transfer of blocks from 'ex' to 'prev_ex' is allowed
2994 * upon those conditions:
2995 * - C1: prev_ex is initialized,
2996 * - C2: prev_ex is logically abutting ex,
2997 * - C3: prev_ex is physically abutting ex,
2998 * - C4: prev_ex can receive the additional blocks without
2999 * overflowing the (initialized) length limit.
3000 */
3001 if ((!ext4_ext_is_uninitialized(prev_ex)) && /*C1*/
3002 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3003 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
3004 (prev_len < (EXT_INIT_MAX_LEN - write_len))) { /*C4*/
3005 err = ext4_ext_get_access(handle, inode, path + depth);
3006 if (err)
3007 goto out;
3008
3009 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3010 map, ex, prev_ex);
3011
3012 /* Shift the start of ex by 'write_len' blocks */
3013 ex->ee_block = cpu_to_le32(ee_block + write_len);
3014 ext4_ext_store_pblock(ex, ee_pblk + write_len);
3015 ex->ee_len = cpu_to_le16(ee_len - write_len);
3016 ext4_ext_mark_uninitialized(ex); /* Restore the flag */
3017
3018 /* Extend prev_ex by 'write_len' blocks */
3019 prev_ex->ee_len = cpu_to_le16(prev_len + write_len);
3020
3021 /* Mark the block containing both extents as dirty */
3022 ext4_ext_dirty(handle, inode, path + depth);
3023
3024 /* Update path to point to the right extent */
3025 path[depth].p_ext = prev_ex;
3026
3027 /* Result: number of initialized blocks past m_lblk */
3028 allocated = write_len;
3029 goto out;
3030 }
3031 }
3032
Yongqiang Yang667eff32011-05-03 12:25:07 -04003033 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003034 /*
3035 * It is safe to convert extent to initialized via explicit
3036 * zeroout only if extent is fully insde i_size or new_size.
3037 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003038 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003039
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003040 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003041 if (ee_len <= 2*EXT4_EXT_ZERO_LEN &&
3042 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3043 err = ext4_ext_zeroout(inode, ex);
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003044 if (err)
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003045 goto out;
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003046
3047 err = ext4_ext_get_access(handle, inode, path + depth);
3048 if (err)
3049 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003050 ext4_ext_mark_initialized(ex);
3051 ext4_ext_try_to_merge(inode, path, ex);
3052 err = ext4_ext_dirty(handle, inode, path + depth);
3053 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04003054 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003055
Amit Arora56055d32007-07-17 21:42:38 -04003056 /*
Yongqiang Yang667eff32011-05-03 12:25:07 -04003057 * four cases:
3058 * 1. split the extent into three extents.
3059 * 2. split the extent into two extents, zeroout the first half.
3060 * 3. split the extent into two extents, zeroout the second half.
3061 * 4. split the extent into two extents with out zeroout.
Amit Arora56055d32007-07-17 21:42:38 -04003062 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003063 split_map.m_lblk = map->m_lblk;
3064 split_map.m_len = map->m_len;
3065
3066 if (allocated > map->m_len) {
3067 if (allocated <= EXT4_EXT_ZERO_LEN &&
3068 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3069 /* case 3 */
3070 zero_ex.ee_block =
Allison Henderson9b940f82011-05-16 10:11:09 -04003071 cpu_to_le32(map->m_lblk);
3072 zero_ex.ee_len = cpu_to_le16(allocated);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003073 ext4_ext_store_pblock(&zero_ex,
3074 ext4_ext_pblock(ex) + map->m_lblk - ee_block);
3075 err = ext4_ext_zeroout(inode, &zero_ex);
Amit Arora56055d32007-07-17 21:42:38 -04003076 if (err)
3077 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003078 split_map.m_lblk = map->m_lblk;
3079 split_map.m_len = allocated;
3080 } else if ((map->m_lblk - ee_block + map->m_len <
3081 EXT4_EXT_ZERO_LEN) &&
3082 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3083 /* case 2 */
3084 if (map->m_lblk != ee_block) {
3085 zero_ex.ee_block = ex->ee_block;
3086 zero_ex.ee_len = cpu_to_le16(map->m_lblk -
3087 ee_block);
3088 ext4_ext_store_pblock(&zero_ex,
3089 ext4_ext_pblock(ex));
3090 err = ext4_ext_zeroout(inode, &zero_ex);
3091 if (err)
3092 goto out;
3093 }
3094
Yongqiang Yang667eff32011-05-03 12:25:07 -04003095 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003096 split_map.m_len = map->m_lblk - ee_block + map->m_len;
3097 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003098 }
3099 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003100
3101 allocated = ext4_split_extent(handle, inode, path,
3102 &split_map, split_flag, 0);
3103 if (allocated < 0)
3104 err = allocated;
3105
Amit Arora56055d32007-07-17 21:42:38 -04003106out:
3107 return err ? err : allocated;
3108}
3109
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003110/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003111 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003112 * ext4_get_blocks_dio_write() when DIO to write
3113 * to an uninitialized extent.
3114 *
Paul Bollefd018fe2011-02-15 00:05:43 +01003115 * Writing to an uninitialized extent may result in splitting the uninitialized
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003116 * extent into multiple /initialized uninitialized extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003117 * There are three possibilities:
3118 * a> There is no split required: Entire extent should be uninitialized
3119 * b> Splits in two extents: Write is happening at either end of the extent
3120 * c> Splits in three extents: Somone is writing in middle of the extent
3121 *
3122 * One of more index blocks maybe needed if the extent tree grow after
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003123 * the uninitialized extent split. To prevent ENOSPC occur at the IO
Mingming Cao00314622009-09-28 15:49:08 -04003124 * complete, we need to split the uninitialized extent before DIO submit
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02003125 * the IO. The uninitialized extent called at this time will be split
Mingming Cao00314622009-09-28 15:49:08 -04003126 * into three uninitialized extent(at most). After IO complete, the part
3127 * being filled will be convert to initialized by the end_io callback function
3128 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003129 *
3130 * Returns the size of uninitialized extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003131 */
3132static int ext4_split_unwritten_extents(handle_t *handle,
3133 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003134 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003135 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04003136 int flags)
3137{
Yongqiang Yang667eff32011-05-03 12:25:07 -04003138 ext4_lblk_t eof_block;
3139 ext4_lblk_t ee_block;
3140 struct ext4_extent *ex;
3141 unsigned int ee_len;
3142 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003143
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003144 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
3145 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003146 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003147
3148 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3149 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003150 if (eof_block < map->m_lblk + map->m_len)
3151 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003152 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003153 * It is safe to convert extent to initialized via explicit
3154 * zeroout only if extent is fully insde i_size or new_size.
3155 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003156 depth = ext_depth(inode);
3157 ex = path[depth].p_ext;
3158 ee_block = le32_to_cpu(ex->ee_block);
3159 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003160
Yongqiang Yang667eff32011-05-03 12:25:07 -04003161 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
3162 split_flag |= EXT4_EXT_MARK_UNINIT2;
Mingming Cao00314622009-09-28 15:49:08 -04003163
Yongqiang Yang667eff32011-05-03 12:25:07 -04003164 flags |= EXT4_GET_BLOCKS_PRE_IO;
3165 return ext4_split_extent(handle, inode, path, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003166}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003167
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003168static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003169 struct inode *inode,
3170 struct ext4_ext_path *path)
3171{
3172 struct ext4_extent *ex;
Mingming Cao00314622009-09-28 15:49:08 -04003173 int depth;
3174 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003175
3176 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003177 ex = path[depth].p_ext;
3178
Yongqiang Yang197217a2011-05-03 11:45:29 -04003179 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3180 "block %llu, max_blocks %u\n", inode->i_ino,
3181 (unsigned long long)le32_to_cpu(ex->ee_block),
3182 ext4_ext_get_actual_len(ex));
3183
Mingming Cao00314622009-09-28 15:49:08 -04003184 err = ext4_ext_get_access(handle, inode, path + depth);
3185 if (err)
3186 goto out;
3187 /* first mark the extent as initialized */
3188 ext4_ext_mark_initialized(ex);
3189
Yongqiang Yang197217a2011-05-03 11:45:29 -04003190 /* note: ext4_ext_correct_indexes() isn't needed here because
3191 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003192 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04003193 ext4_ext_try_to_merge(inode, path, ex);
3194
Mingming Cao00314622009-09-28 15:49:08 -04003195 /* Mark modified extent as dirty */
3196 err = ext4_ext_dirty(handle, inode, path + depth);
3197out:
3198 ext4_ext_show_leaf(inode, path);
3199 return err;
3200}
3201
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003202static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3203 sector_t block, int count)
3204{
3205 int i;
3206 for (i = 0; i < count; i++)
3207 unmap_underlying_metadata(bdev, block + i);
3208}
3209
Theodore Ts'o58590b02010-10-27 21:23:12 -04003210/*
3211 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3212 */
3213static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
Eric Sandeend002ebf2011-01-10 13:03:35 -05003214 ext4_lblk_t lblk,
Theodore Ts'o58590b02010-10-27 21:23:12 -04003215 struct ext4_ext_path *path,
3216 unsigned int len)
3217{
3218 int i, depth;
3219 struct ext4_extent_header *eh;
Sergey Senozhatsky65922cb2011-03-23 14:08:27 -04003220 struct ext4_extent *last_ex;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003221
3222 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3223 return 0;
3224
3225 depth = ext_depth(inode);
3226 eh = path[depth].p_hdr;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003227
3228 if (unlikely(!eh->eh_entries)) {
3229 EXT4_ERROR_INODE(inode, "eh->eh_entries == 0 and "
3230 "EOFBLOCKS_FL set");
3231 return -EIO;
3232 }
3233 last_ex = EXT_LAST_EXTENT(eh);
3234 /*
3235 * We should clear the EOFBLOCKS_FL flag if we are writing the
3236 * last block in the last extent in the file. We test this by
3237 * first checking to see if the caller to
3238 * ext4_ext_get_blocks() was interested in the last block (or
3239 * a block beyond the last block) in the current extent. If
3240 * this turns out to be false, we can bail out from this
3241 * function immediately.
3242 */
Eric Sandeend002ebf2011-01-10 13:03:35 -05003243 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
Theodore Ts'o58590b02010-10-27 21:23:12 -04003244 ext4_ext_get_actual_len(last_ex))
3245 return 0;
3246 /*
3247 * If the caller does appear to be planning to write at or
3248 * beyond the end of the current extent, we then test to see
3249 * if the current extent is the last extent in the file, by
3250 * checking to make sure it was reached via the rightmost node
3251 * at each level of the tree.
3252 */
3253 for (i = depth-1; i >= 0; i--)
3254 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3255 return 0;
3256 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3257 return ext4_mark_inode_dirty(handle, inode);
3258}
3259
Aditya Kali7b415bf2011-09-09 19:04:51 -04003260/**
3261 * ext4_find_delalloc_range: find delayed allocated block in the given range.
3262 *
3263 * Goes through the buffer heads in the range [lblk_start, lblk_end] and returns
3264 * whether there are any buffers marked for delayed allocation. It returns '1'
3265 * on the first delalloc'ed buffer head found. If no buffer head in the given
3266 * range is marked for delalloc, it returns 0.
3267 * lblk_start should always be <= lblk_end.
3268 * search_hint_reverse is to indicate that searching in reverse from lblk_end to
3269 * lblk_start might be more efficient (i.e., we will likely hit the delalloc'ed
3270 * block sooner). This is useful when blocks are truncated sequentially from
3271 * lblk_start towards lblk_end.
3272 */
3273static int ext4_find_delalloc_range(struct inode *inode,
3274 ext4_lblk_t lblk_start,
3275 ext4_lblk_t lblk_end,
3276 int search_hint_reverse)
3277{
3278 struct address_space *mapping = inode->i_mapping;
3279 struct buffer_head *head, *bh = NULL;
3280 struct page *page;
3281 ext4_lblk_t i, pg_lblk;
3282 pgoff_t index;
3283
3284 /* reverse search wont work if fs block size is less than page size */
3285 if (inode->i_blkbits < PAGE_CACHE_SHIFT)
3286 search_hint_reverse = 0;
3287
3288 if (search_hint_reverse)
3289 i = lblk_end;
3290 else
3291 i = lblk_start;
3292
3293 index = i >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
3294
3295 while ((i >= lblk_start) && (i <= lblk_end)) {
3296 page = find_get_page(mapping, index);
Aditya Kali5356f262011-09-09 19:20:51 -04003297 if (!page)
Aditya Kali7b415bf2011-09-09 19:04:51 -04003298 goto nextpage;
3299
Aditya Kali7b415bf2011-09-09 19:04:51 -04003300 if (!page_has_buffers(page))
3301 goto nextpage;
3302
3303 head = page_buffers(page);
3304 if (!head)
3305 goto nextpage;
3306
3307 bh = head;
3308 pg_lblk = index << (PAGE_CACHE_SHIFT -
3309 inode->i_blkbits);
3310 do {
3311 if (unlikely(pg_lblk < lblk_start)) {
3312 /*
3313 * This is possible when fs block size is less
3314 * than page size and our cluster starts/ends in
3315 * middle of the page. So we need to skip the
3316 * initial few blocks till we reach the 'lblk'
3317 */
3318 pg_lblk++;
3319 continue;
3320 }
3321
Aditya Kali5356f262011-09-09 19:20:51 -04003322 /* Check if the buffer is delayed allocated and that it
3323 * is not yet mapped. (when da-buffers are mapped during
3324 * their writeout, their da_mapped bit is set.)
3325 */
3326 if (buffer_delay(bh) && !buffer_da_mapped(bh)) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04003327 page_cache_release(page);
Aditya Kalid8990242011-09-09 19:18:51 -04003328 trace_ext4_find_delalloc_range(inode,
3329 lblk_start, lblk_end,
3330 search_hint_reverse,
3331 1, i);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003332 return 1;
3333 }
3334 if (search_hint_reverse)
3335 i--;
3336 else
3337 i++;
3338 } while ((i >= lblk_start) && (i <= lblk_end) &&
3339 ((bh = bh->b_this_page) != head));
3340nextpage:
3341 if (page)
3342 page_cache_release(page);
3343 /*
3344 * Move to next page. 'i' will be the first lblk in the next
3345 * page.
3346 */
3347 if (search_hint_reverse)
3348 index--;
3349 else
3350 index++;
3351 i = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
3352 }
3353
Aditya Kalid8990242011-09-09 19:18:51 -04003354 trace_ext4_find_delalloc_range(inode, lblk_start, lblk_end,
3355 search_hint_reverse, 0, 0);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003356 return 0;
3357}
3358
3359int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk,
3360 int search_hint_reverse)
3361{
3362 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3363 ext4_lblk_t lblk_start, lblk_end;
3364 lblk_start = lblk & (~(sbi->s_cluster_ratio - 1));
3365 lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
3366
3367 return ext4_find_delalloc_range(inode, lblk_start, lblk_end,
3368 search_hint_reverse);
3369}
3370
3371/**
3372 * Determines how many complete clusters (out of those specified by the 'map')
3373 * are under delalloc and were reserved quota for.
3374 * This function is called when we are writing out the blocks that were
3375 * originally written with their allocation delayed, but then the space was
3376 * allocated using fallocate() before the delayed allocation could be resolved.
3377 * The cases to look for are:
3378 * ('=' indicated delayed allocated blocks
3379 * '-' indicates non-delayed allocated blocks)
3380 * (a) partial clusters towards beginning and/or end outside of allocated range
3381 * are not delalloc'ed.
3382 * Ex:
3383 * |----c---=|====c====|====c====|===-c----|
3384 * |++++++ allocated ++++++|
3385 * ==> 4 complete clusters in above example
3386 *
3387 * (b) partial cluster (outside of allocated range) towards either end is
3388 * marked for delayed allocation. In this case, we will exclude that
3389 * cluster.
3390 * Ex:
3391 * |----====c========|========c========|
3392 * |++++++ allocated ++++++|
3393 * ==> 1 complete clusters in above example
3394 *
3395 * Ex:
3396 * |================c================|
3397 * |++++++ allocated ++++++|
3398 * ==> 0 complete clusters in above example
3399 *
3400 * The ext4_da_update_reserve_space will be called only if we
3401 * determine here that there were some "entire" clusters that span
3402 * this 'allocated' range.
3403 * In the non-bigalloc case, this function will just end up returning num_blks
3404 * without ever calling ext4_find_delalloc_range.
3405 */
3406static unsigned int
3407get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start,
3408 unsigned int num_blks)
3409{
3410 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3411 ext4_lblk_t alloc_cluster_start, alloc_cluster_end;
3412 ext4_lblk_t lblk_from, lblk_to, c_offset;
3413 unsigned int allocated_clusters = 0;
3414
3415 alloc_cluster_start = EXT4_B2C(sbi, lblk_start);
3416 alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1);
3417
3418 /* max possible clusters for this allocation */
3419 allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1;
3420
Aditya Kalid8990242011-09-09 19:18:51 -04003421 trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks);
3422
Aditya Kali7b415bf2011-09-09 19:04:51 -04003423 /* Check towards left side */
3424 c_offset = lblk_start & (sbi->s_cluster_ratio - 1);
3425 if (c_offset) {
3426 lblk_from = lblk_start & (~(sbi->s_cluster_ratio - 1));
3427 lblk_to = lblk_from + c_offset - 1;
3428
3429 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
3430 allocated_clusters--;
3431 }
3432
3433 /* Now check towards right. */
3434 c_offset = (lblk_start + num_blks) & (sbi->s_cluster_ratio - 1);
3435 if (allocated_clusters && c_offset) {
3436 lblk_from = lblk_start + num_blks;
3437 lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1;
3438
3439 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
3440 allocated_clusters--;
3441 }
3442
3443 return allocated_clusters;
3444}
3445
Mingming Cao00314622009-09-28 15:49:08 -04003446static int
3447ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003448 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003449 struct ext4_ext_path *path, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003450 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003451{
3452 int ret = 0;
3453 int err = 0;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003454 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Mingming Cao00314622009-09-28 15:49:08 -04003455
3456 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3457 "block %llu, max_blocks %u, flags %d, allocated %u",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003458 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04003459 flags, allocated);
3460 ext4_ext_show_leaf(inode, path);
3461
Aditya Kalid8990242011-09-09 19:18:51 -04003462 trace_ext4_ext_handle_uninitialized_extents(inode, map, allocated,
3463 newblock);
3464
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003465 /* get_block() before submit the IO, split the extent */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003466 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003467 ret = ext4_split_unwritten_extents(handle, inode, map,
3468 path, flags);
Mingming5f524952009-11-10 10:48:04 -05003469 /*
3470 * Flag the inode(non aio case) or end_io struct (aio case)
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003471 * that this IO needs to conversion to written when IO is
Mingming5f524952009-11-10 10:48:04 -05003472 * completed
3473 */
Tao Ma0edeb712011-10-31 17:30:44 -04003474 if (io)
3475 ext4_set_io_unwritten_flag(inode, io);
3476 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003477 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
Jiaying Zhang744692d2010-03-04 16:14:02 -05003478 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003479 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao00314622009-09-28 15:49:08 -04003480 goto out;
3481 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003482 /* IO end_io complete, convert the filled extent to written */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003483 if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003484 ret = ext4_convert_unwritten_extents_endio(handle, inode,
Mingming Cao00314622009-09-28 15:49:08 -04003485 path);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003486 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05003487 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05003488 err = check_eofblocks_fl(handle, inode, map->m_lblk,
3489 path, map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003490 } else
3491 err = ret;
Mingming Cao00314622009-09-28 15:49:08 -04003492 goto out2;
3493 }
3494 /* buffered IO case */
3495 /*
3496 * repeat fallocate creation request
3497 * we already have an unwritten extent
3498 */
3499 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3500 goto map_out;
3501
3502 /* buffered READ or buffered write_begin() lookup */
3503 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3504 /*
3505 * We have blocks reserved already. We
3506 * return allocated blocks so that delalloc
3507 * won't do block reservation for us. But
3508 * the buffer head will be unmapped so that
3509 * a read from the block returns 0s.
3510 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003511 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003512 goto out1;
3513 }
3514
3515 /* buffered write, writepage time, convert*/
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003516 ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04003517 if (ret >= 0)
Jan Karab436b9b2009-12-08 23:51:10 -05003518 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04003519out:
3520 if (ret <= 0) {
3521 err = ret;
3522 goto out2;
3523 } else
3524 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003525 map->m_flags |= EXT4_MAP_NEW;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003526 /*
3527 * if we allocated more blocks than requested
3528 * we need to make sure we unmap the extra block
3529 * allocated. The actual needed block will get
3530 * unmapped later when we find the buffer_head marked
3531 * new.
3532 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003533 if (allocated > map->m_len) {
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003534 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003535 newblock + map->m_len,
3536 allocated - map->m_len);
3537 allocated = map->m_len;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003538 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003539
3540 /*
3541 * If we have done fallocate with the offset that is already
3542 * delayed allocated, we would have block reservation
3543 * and quota reservation done in the delayed write path.
3544 * But fallocate would have already updated quota and block
3545 * count for this offset. So cancel these reservation
3546 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003547 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
3548 unsigned int reserved_clusters;
3549 reserved_clusters = get_reserved_cluster_alloc(inode,
3550 map->m_lblk, map->m_len);
3551 if (reserved_clusters)
3552 ext4_da_update_reserve_space(inode,
3553 reserved_clusters,
3554 0);
3555 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003556
Mingming Cao00314622009-09-28 15:49:08 -04003557map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003558 map->m_flags |= EXT4_MAP_MAPPED;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04003559 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
3560 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
3561 map->m_len);
3562 if (err < 0)
3563 goto out2;
3564 }
Mingming Cao00314622009-09-28 15:49:08 -04003565out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003566 if (allocated > map->m_len)
3567 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003568 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003569 map->m_pblk = newblock;
3570 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04003571out2:
3572 if (path) {
3573 ext4_ext_drop_refs(path);
3574 kfree(path);
3575 }
3576 return err ? err : allocated;
3577}
Theodore Ts'o58590b02010-10-27 21:23:12 -04003578
Mingming Cao00314622009-09-28 15:49:08 -04003579/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003580 * get_implied_cluster_alloc - check to see if the requested
3581 * allocation (in the map structure) overlaps with a cluster already
3582 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04003583 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003584 * @map The requested lblk->pblk mapping
3585 * @ex The extent structure which might contain an implied
3586 * cluster allocation
3587 *
3588 * This function is called by ext4_ext_map_blocks() after we failed to
3589 * find blocks that were already in the inode's extent tree. Hence,
3590 * we know that the beginning of the requested region cannot overlap
3591 * the extent from the inode's extent tree. There are three cases we
3592 * want to catch. The first is this case:
3593 *
3594 * |--- cluster # N--|
3595 * |--- extent ---| |---- requested region ---|
3596 * |==========|
3597 *
3598 * The second case that we need to test for is this one:
3599 *
3600 * |--------- cluster # N ----------------|
3601 * |--- requested region --| |------- extent ----|
3602 * |=======================|
3603 *
3604 * The third case is when the requested region lies between two extents
3605 * within the same cluster:
3606 * |------------- cluster # N-------------|
3607 * |----- ex -----| |---- ex_right ----|
3608 * |------ requested region ------|
3609 * |================|
3610 *
3611 * In each of the above cases, we need to set the map->m_pblk and
3612 * map->m_len so it corresponds to the return the extent labelled as
3613 * "|====|" from cluster #N, since it is already in use for data in
3614 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
3615 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
3616 * as a new "allocated" block region. Otherwise, we will return 0 and
3617 * ext4_ext_map_blocks() will then allocate one or more new clusters
3618 * by calling ext4_mb_new_blocks().
3619 */
Aditya Kalid8990242011-09-09 19:18:51 -04003620static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003621 struct ext4_map_blocks *map,
3622 struct ext4_extent *ex,
3623 struct ext4_ext_path *path)
3624{
Aditya Kalid8990242011-09-09 19:18:51 -04003625 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003626 ext4_lblk_t c_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
3627 ext4_lblk_t ex_cluster_start, ex_cluster_end;
Curt Wohlgemuth14d7f3e2011-12-18 17:39:02 -05003628 ext4_lblk_t rr_cluster_start;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003629 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
3630 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
3631 unsigned short ee_len = ext4_ext_get_actual_len(ex);
3632
3633 /* The extent passed in that we are trying to match */
3634 ex_cluster_start = EXT4_B2C(sbi, ee_block);
3635 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
3636
3637 /* The requested region passed into ext4_map_blocks() */
3638 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003639
3640 if ((rr_cluster_start == ex_cluster_end) ||
3641 (rr_cluster_start == ex_cluster_start)) {
3642 if (rr_cluster_start == ex_cluster_end)
3643 ee_start += ee_len - 1;
3644 map->m_pblk = (ee_start & ~(sbi->s_cluster_ratio - 1)) +
3645 c_offset;
3646 map->m_len = min(map->m_len,
3647 (unsigned) sbi->s_cluster_ratio - c_offset);
3648 /*
3649 * Check for and handle this case:
3650 *
3651 * |--------- cluster # N-------------|
3652 * |------- extent ----|
3653 * |--- requested region ---|
3654 * |===========|
3655 */
3656
3657 if (map->m_lblk < ee_block)
3658 map->m_len = min(map->m_len, ee_block - map->m_lblk);
3659
3660 /*
3661 * Check for the case where there is already another allocated
3662 * block to the right of 'ex' but before the end of the cluster.
3663 *
3664 * |------------- cluster # N-------------|
3665 * |----- ex -----| |---- ex_right ----|
3666 * |------ requested region ------|
3667 * |================|
3668 */
3669 if (map->m_lblk > ee_block) {
3670 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
3671 map->m_len = min(map->m_len, next - map->m_lblk);
3672 }
Aditya Kalid8990242011-09-09 19:18:51 -04003673
3674 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003675 return 1;
3676 }
Aditya Kalid8990242011-09-09 19:18:51 -04003677
3678 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003679 return 0;
3680}
3681
3682
3683/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05003684 * Block allocation/map/preallocation routine for extents based files
3685 *
3686 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003687 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003688 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3689 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05003690 *
3691 * return > 0, number of of blocks already mapped/allocated
3692 * if create == 0 and these are pre-allocated blocks
3693 * buffer head is unmapped
3694 * otherwise blocks are mapped
3695 *
3696 * return = 0, if plain look up failed (blocks have not been allocated)
3697 * buffer head is unmapped
3698 *
3699 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003700 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003701int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3702 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07003703{
3704 struct ext4_ext_path *path = NULL;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003705 struct ext4_extent newex, *ex, *ex2;
3706 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003707 ext4_fsblk_t newblock = 0;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003708 int free_on_err = 0, err = 0, depth, ret;
3709 unsigned int allocated = 0, offset = 0;
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04003710 unsigned int allocated_clusters = 0;
Allison Hendersone8613042011-05-25 07:41:46 -04003711 unsigned int punched_out = 0;
3712 unsigned int result = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003713 struct ext4_allocation_request ar;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003714 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003715 ext4_lblk_t cluster_offset;
Alex Tomasa86c6182006-10-11 01:21:03 -07003716
Mingming84fe3be2009-09-01 08:44:37 -04003717 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003718 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003719 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07003720
3721 /* check in cache */
Robin Dong015861b2011-07-17 23:27:43 -04003722 if (!(flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) &&
3723 ext4_ext_in_cache(inode, map->m_lblk, &newex)) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003724 if (!newex.ee_start_lo && !newex.ee_start_hi) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04003725 if ((sbi->s_cluster_ratio > 1) &&
3726 ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
3727 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3728
Theodore Ts'oc2177052009-05-14 00:58:52 -04003729 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003730 /*
3731 * block isn't allocated yet and
3732 * user doesn't want to allocate it
3733 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003734 goto out2;
3735 }
3736 /* we should allocate requested block */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003737 } else {
Alex Tomasa86c6182006-10-11 01:21:03 -07003738 /* block is already allocated */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003739 if (sbi->s_cluster_ratio > 1)
3740 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003741 newblock = map->m_lblk
Dave Kleikamp8c55e202007-05-24 13:04:54 -04003742 - le32_to_cpu(newex.ee_block)
Theodore Ts'obf89d162010-10-27 21:30:14 -04003743 + ext4_ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003744 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003745 allocated = ext4_ext_get_actual_len(&newex) -
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003746 (map->m_lblk - le32_to_cpu(newex.ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -07003747 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07003748 }
3749 }
3750
3751 /* find extent for this block */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003752 path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07003753 if (IS_ERR(path)) {
3754 err = PTR_ERR(path);
3755 path = NULL;
3756 goto out2;
3757 }
3758
3759 depth = ext_depth(inode);
3760
3761 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003762 * consistent leaf must not be empty;
3763 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07003764 * this is why assert can't be put in ext4_ext_find_extent()
3765 */
Frank Mayhar273df552010-03-02 11:46:09 -05003766 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
3767 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04003768 "lblock: %lu, depth: %d pblock %lld",
3769 (unsigned long) map->m_lblk, depth,
3770 path[depth].p_block);
Surbhi Palande034fb4c2009-12-14 09:53:52 -05003771 err = -EIO;
3772 goto out2;
3773 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003774
Avantika Mathur7e028972006-12-06 20:41:33 -08003775 ex = path[depth].p_ext;
3776 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003777 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003778 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003779 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003780
3781 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003782 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04003783 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003784 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003785 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04003786
3787 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
3788
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003789 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003790 if (in_range(map->m_lblk, ee_block, ee_len)) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04003791 struct ext4_map_blocks punch_map;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04003792 ext4_fsblk_t partial_cluster = 0;
3793
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003794 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003795 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003796 allocated = ee_len - (map->m_lblk - ee_block);
3797 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
3798 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04003799
Allison Hendersone8613042011-05-25 07:41:46 -04003800 if ((flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) == 0) {
3801 /*
3802 * Do not put uninitialized extent
3803 * in the cache
3804 */
3805 if (!ext4_ext_is_uninitialized(ex)) {
3806 ext4_ext_put_in_cache(inode, ee_block,
3807 ee_len, ee_start);
3808 goto out;
3809 }
3810 ret = ext4_ext_handle_uninitialized_extents(
3811 handle, inode, map, path, flags,
3812 allocated, newblock);
3813 return ret;
Amit Arora56055d32007-07-17 21:42:38 -04003814 }
Allison Hendersone8613042011-05-25 07:41:46 -04003815
3816 /*
3817 * Punch out the map length, but only to the
3818 * end of the extent
3819 */
3820 punched_out = allocated < map->m_len ?
3821 allocated : map->m_len;
3822
3823 /*
3824 * Sense extents need to be converted to
3825 * uninitialized, they must fit in an
3826 * uninitialized extent
3827 */
3828 if (punched_out > EXT_UNINIT_MAX_LEN)
3829 punched_out = EXT_UNINIT_MAX_LEN;
3830
3831 punch_map.m_lblk = map->m_lblk;
3832 punch_map.m_pblk = newblock;
3833 punch_map.m_len = punched_out;
3834 punch_map.m_flags = 0;
3835
3836 /* Check to see if the extent needs to be split */
3837 if (punch_map.m_len != ee_len ||
3838 punch_map.m_lblk != ee_block) {
3839
3840 ret = ext4_split_extent(handle, inode,
3841 path, &punch_map, 0,
3842 EXT4_GET_BLOCKS_PUNCH_OUT_EXT |
3843 EXT4_GET_BLOCKS_PRE_IO);
3844
3845 if (ret < 0) {
3846 err = ret;
3847 goto out2;
3848 }
3849 /*
3850 * find extent for the block at
3851 * the start of the hole
3852 */
3853 ext4_ext_drop_refs(path);
3854 kfree(path);
3855
3856 path = ext4_ext_find_extent(inode,
3857 map->m_lblk, NULL);
3858 if (IS_ERR(path)) {
3859 err = PTR_ERR(path);
3860 path = NULL;
3861 goto out2;
3862 }
3863
3864 depth = ext_depth(inode);
3865 ex = path[depth].p_ext;
3866 ee_len = ext4_ext_get_actual_len(ex);
3867 ee_block = le32_to_cpu(ex->ee_block);
3868 ee_start = ext4_ext_pblock(ex);
3869
3870 }
3871
3872 ext4_ext_mark_uninitialized(ex);
3873
Allison Hendersonf7d0d372011-07-17 23:17:02 -04003874 ext4_ext_invalidate_cache(inode);
3875
3876 err = ext4_ext_rm_leaf(handle, inode, path,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04003877 &partial_cluster, map->m_lblk,
3878 map->m_lblk + punched_out);
Allison Hendersonf7d0d372011-07-17 23:17:02 -04003879
3880 if (!err && path->p_hdr->eh_entries == 0) {
3881 /*
3882 * Punch hole freed all of this sub tree,
3883 * so we need to correct eh_depth
3884 */
3885 err = ext4_ext_get_access(handle, inode, path);
3886 if (err == 0) {
3887 ext_inode_hdr(inode)->eh_depth = 0;
3888 ext_inode_hdr(inode)->eh_max =
3889 cpu_to_le16(ext4_ext_space_root(
3890 inode, 0));
3891
3892 err = ext4_ext_dirty(
3893 handle, inode, path);
3894 }
3895 }
Allison Hendersone8613042011-05-25 07:41:46 -04003896
3897 goto out2;
Alex Tomasa86c6182006-10-11 01:21:03 -07003898 }
3899 }
3900
Aditya Kali7b415bf2011-09-09 19:04:51 -04003901 if ((sbi->s_cluster_ratio > 1) &&
3902 ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
3903 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3904
Alex Tomasa86c6182006-10-11 01:21:03 -07003905 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003906 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07003907 * we couldn't try to create block if create flag is zero
3908 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04003909 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003910 /*
3911 * put just found gap into cache to speed up
3912 * subsequent requests
3913 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003914 ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
Alex Tomasa86c6182006-10-11 01:21:03 -07003915 goto out2;
3916 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003917
Alex Tomasa86c6182006-10-11 01:21:03 -07003918 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003919 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07003920 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003921 map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003922 newex.ee_block = cpu_to_le32(map->m_lblk);
3923 cluster_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
3924
3925 /*
3926 * If we are doing bigalloc, check to see if the extent returned
3927 * by ext4_ext_find_extent() implies a cluster we can use.
3928 */
3929 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04003930 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003931 ar.len = allocated = map->m_len;
3932 newblock = map->m_pblk;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003933 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003934 goto got_allocated_blocks;
3935 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003936
Alex Tomasc9de5602008-01-29 00:19:52 -05003937 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003938 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003939 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3940 if (err)
3941 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003942 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003943 ex2 = NULL;
3944 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
Alex Tomasc9de5602008-01-29 00:19:52 -05003945 if (err)
3946 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04003947
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003948 /* Check if the extent after searching to the right implies a
3949 * cluster we can use. */
3950 if ((sbi->s_cluster_ratio > 1) && ex2 &&
Aditya Kalid8990242011-09-09 19:18:51 -04003951 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003952 ar.len = allocated = map->m_len;
3953 newblock = map->m_pblk;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003954 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003955 goto got_allocated_blocks;
3956 }
3957
Amit Arora749269f2007-07-18 09:02:56 -04003958 /*
3959 * See if request is beyond maximum number of blocks we can have in
3960 * a single extent. For an initialized extent this limit is
3961 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3962 * EXT_UNINIT_MAX_LEN.
3963 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003964 if (map->m_len > EXT_INIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003965 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003966 map->m_len = EXT_INIT_MAX_LEN;
3967 else if (map->m_len > EXT_UNINIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003968 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003969 map->m_len = EXT_UNINIT_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04003970
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003971 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003972 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003973 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04003974 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003975 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04003976 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003977 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003978
3979 /* allocate new block */
3980 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003981 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
3982 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003983 /*
3984 * We calculate the offset from the beginning of the cluster
3985 * for the logical block number, since when we allocate a
3986 * physical cluster, the physical block should start at the
3987 * same offset from the beginning of the cluster. This is
3988 * needed so that future calls to get_implied_cluster_alloc()
3989 * work correctly.
3990 */
3991 offset = map->m_lblk & (sbi->s_cluster_ratio - 1);
3992 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
3993 ar.goal -= offset;
3994 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05003995 if (S_ISREG(inode->i_mode))
3996 ar.flags = EXT4_MB_HINT_DATA;
3997 else
3998 /* disable in-core preallocation for non-regular files */
3999 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04004000 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4001 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05004002 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004003 if (!newblock)
4004 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04004005 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004006 ar.goal, newblock, allocated);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004007 free_on_err = 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004008 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004009 ar.len = EXT4_C2B(sbi, ar.len) - offset;
4010 if (ar.len > allocated)
4011 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004012
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004013got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07004014 /* try to insert new extent into found leaf and return */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004015 ext4_ext_store_pblock(&newex, newblock + offset);
Alex Tomasc9de5602008-01-29 00:19:52 -05004016 newex.ee_len = cpu_to_le16(ar.len);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004017 /* Mark uninitialized */
4018 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
Amit Aroraa2df2a62007-07-17 21:42:41 -04004019 ext4_ext_mark_uninitialized(&newex);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004020 /*
Jiaying Zhang744692d2010-03-04 16:14:02 -05004021 * io_end structure was created for every IO write to an
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004022 * uninitialized extent. To avoid unnecessary conversion,
Jiaying Zhang744692d2010-03-04 16:14:02 -05004023 * here we flag the IO that really needs the conversion.
Mingming5f524952009-11-10 10:48:04 -05004024 * For non asycn direct IO case, flag the inode state
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004025 * that we need to perform conversion when IO is done.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004026 */
Jiaying Zhang744692d2010-03-04 16:14:02 -05004027 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Tao Ma0edeb712011-10-31 17:30:44 -04004028 if (io)
4029 ext4_set_io_unwritten_flag(inode, io);
4030 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004031 ext4_set_inode_state(inode,
4032 EXT4_STATE_DIO_UNWRITTEN);
Mingming5f524952009-11-10 10:48:04 -05004033 }
Jiaying Zhang744692d2010-03-04 16:14:02 -05004034 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004035 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004036 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004037
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004038 err = 0;
4039 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
4040 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4041 path, ar.len);
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004042 if (!err)
4043 err = ext4_ext_insert_extent(handle, inode, path,
4044 &newex, flags);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004045 if (err && free_on_err) {
Maxim Patlasov7132de72011-07-10 19:37:48 -04004046 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
4047 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
Alex Tomas315054f2007-05-24 13:04:25 -04004048 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05004049 /* not a good idea to call discard here directly,
4050 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004051 ext4_discard_preallocations(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05004052 ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex),
Maxim Patlasov7132de72011-07-10 19:37:48 -04004053 ext4_ext_get_actual_len(&newex), fb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004054 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04004055 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004056
Alex Tomasa86c6182006-10-11 01:21:03 -07004057 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04004058 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004059 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004060 if (allocated > map->m_len)
4061 allocated = map->m_len;
4062 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07004063
Jan Karab436b9b2009-12-08 23:51:10 -05004064 /*
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004065 * Update reserved blocks/metadata blocks after successful
4066 * block allocation which had been deferred till now.
4067 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04004068 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004069 unsigned int reserved_clusters;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004070 /*
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004071 * Check how many clusters we had reserved this allocated range
Aditya Kali7b415bf2011-09-09 19:04:51 -04004072 */
4073 reserved_clusters = get_reserved_cluster_alloc(inode,
4074 map->m_lblk, allocated);
4075 if (map->m_flags & EXT4_MAP_FROM_CLUSTER) {
4076 if (reserved_clusters) {
4077 /*
4078 * We have clusters reserved for this range.
4079 * But since we are not doing actual allocation
4080 * and are simply using blocks from previously
4081 * allocated cluster, we should release the
4082 * reservation and not claim quota.
4083 */
4084 ext4_da_update_reserve_space(inode,
4085 reserved_clusters, 0);
4086 }
4087 } else {
4088 BUG_ON(allocated_clusters < reserved_clusters);
4089 /* We will claim quota for all newly allocated blocks.*/
4090 ext4_da_update_reserve_space(inode, allocated_clusters,
4091 1);
4092 if (reserved_clusters < allocated_clusters) {
Aditya Kali5356f262011-09-09 19:20:51 -04004093 struct ext4_inode_info *ei = EXT4_I(inode);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004094 int reservation = allocated_clusters -
4095 reserved_clusters;
4096 /*
4097 * It seems we claimed few clusters outside of
4098 * the range of this allocation. We should give
4099 * it back to the reservation pool. This can
4100 * happen in the following case:
4101 *
4102 * * Suppose s_cluster_ratio is 4 (i.e., each
4103 * cluster has 4 blocks. Thus, the clusters
4104 * are [0-3],[4-7],[8-11]...
4105 * * First comes delayed allocation write for
4106 * logical blocks 10 & 11. Since there were no
4107 * previous delayed allocated blocks in the
4108 * range [8-11], we would reserve 1 cluster
4109 * for this write.
4110 * * Next comes write for logical blocks 3 to 8.
4111 * In this case, we will reserve 2 clusters
4112 * (for [0-3] and [4-7]; and not for [8-11] as
4113 * that range has a delayed allocated blocks.
4114 * Thus total reserved clusters now becomes 3.
4115 * * Now, during the delayed allocation writeout
4116 * time, we will first write blocks [3-8] and
4117 * allocate 3 clusters for writing these
4118 * blocks. Also, we would claim all these
4119 * three clusters above.
4120 * * Now when we come here to writeout the
4121 * blocks [10-11], we would expect to claim
4122 * the reservation of 1 cluster we had made
4123 * (and we would claim it since there are no
4124 * more delayed allocated blocks in the range
4125 * [8-11]. But our reserved cluster count had
4126 * already gone to 0.
4127 *
4128 * Thus, at the step 4 above when we determine
4129 * that there are still some unwritten delayed
4130 * allocated blocks outside of our current
4131 * block range, we should increment the
4132 * reserved clusters count so that when the
4133 * remaining blocks finally gets written, we
4134 * could claim them.
4135 */
Aditya Kali5356f262011-09-09 19:20:51 -04004136 dquot_reserve_block(inode,
4137 EXT4_C2B(sbi, reservation));
4138 spin_lock(&ei->i_block_reservation_lock);
4139 ei->i_reserved_data_blocks += reservation;
4140 spin_unlock(&ei->i_block_reservation_lock);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004141 }
4142 }
4143 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004144
4145 /*
Jan Karab436b9b2009-12-08 23:51:10 -05004146 * Cache the extent and update transaction to commit on fdatasync only
4147 * when it is _not_ an uninitialized extent.
4148 */
4149 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05004150 ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock);
Jan Karab436b9b2009-12-08 23:51:10 -05004151 ext4_update_inode_fsync_trans(handle, inode, 1);
4152 } else
4153 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004154out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004155 if (allocated > map->m_len)
4156 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004157 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004158 map->m_flags |= EXT4_MAP_MAPPED;
4159 map->m_pblk = newblock;
4160 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004161out2:
4162 if (path) {
4163 ext4_ext_drop_refs(path);
4164 kfree(path);
4165 }
Allison Hendersone8613042011-05-25 07:41:46 -04004166 result = (flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) ?
4167 punched_out : allocated;
4168
Yongqiang Yange7b319e2011-10-29 09:39:51 -04004169 trace_ext4_ext_map_blocks_exit(inode, map->m_lblk,
4170 newblock, map->m_len, err ? err : result);
4171
Allison Hendersone8613042011-05-25 07:41:46 -04004172 return err ? err : result;
Alex Tomasa86c6182006-10-11 01:21:03 -07004173}
4174
Jan Karacf108bc2008-07-11 19:27:31 -04004175void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004176{
4177 struct address_space *mapping = inode->i_mapping;
4178 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004179 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004180 handle_t *handle;
Allison Henderson189e8682011-09-06 21:49:44 -04004181 loff_t page_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004182 int err = 0;
4183
4184 /*
Jiaying Zhang3889fd52011-01-10 12:47:05 -05004185 * finish any pending end_io work so we won't run the risk of
4186 * converting any truncated blocks to initialized later
4187 */
4188 ext4_flush_completed_IO(inode);
4189
4190 /*
Alex Tomasa86c6182006-10-11 01:21:03 -07004191 * probably first extent we're gonna free will be last in block
4192 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04004193 err = ext4_writepage_trans_blocks(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07004194 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04004195 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07004196 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07004197
Allison Henderson189e8682011-09-06 21:49:44 -04004198 if (inode->i_size % PAGE_CACHE_SIZE != 0) {
4199 page_len = PAGE_CACHE_SIZE -
4200 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4201
4202 err = ext4_discard_partial_page_buffers(handle,
4203 mapping, inode->i_size, page_len, 0);
4204
4205 if (err)
4206 goto out_stop;
4207 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004208
Jan Kara9ddfc3d2008-07-11 19:27:31 -04004209 if (ext4_orphan_add(handle, inode))
4210 goto out_stop;
4211
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004212 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07004213 ext4_ext_invalidate_cache(inode);
4214
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004215 ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05004216
Alex Tomasa86c6182006-10-11 01:21:03 -07004217 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004218 * TODO: optimization is possible here.
4219 * Probably we need not scan at all,
4220 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004221 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004222
4223 /* we have to know where to truncate from in crash case */
4224 EXT4_I(inode)->i_disksize = inode->i_size;
4225 ext4_mark_inode_dirty(handle, inode);
4226
4227 last_block = (inode->i_size + sb->s_blocksize - 1)
4228 >> EXT4_BLOCK_SIZE_BITS(sb);
Allison Hendersonc6a03712011-07-17 23:21:03 -04004229 err = ext4_ext_remove_space(inode, last_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07004230
4231 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04004232 * transaction synchronous.
4233 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004234 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05004235 ext4_handle_sync(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07004236
Jan Kara9ddfc3d2008-07-11 19:27:31 -04004237 up_write(&EXT4_I(inode)->i_data_sem);
Eric Gouriouf6d2f6b2011-05-22 21:33:00 -04004238
4239out_stop:
Alex Tomasa86c6182006-10-11 01:21:03 -07004240 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004241 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07004242 * then we need to clear up the orphan record which we created above.
4243 * However, if this was a real unlink then we were called by
4244 * ext4_delete_inode(), and we allow that function to clean up the
4245 * orphan info for us.
4246 */
4247 if (inode->i_nlink)
4248 ext4_orphan_del(handle, inode);
4249
Solofo Ramangalahyef737722008-04-29 22:00:41 -04004250 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4251 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07004252 ext4_journal_stop(handle);
4253}
4254
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004255static void ext4_falloc_update_inode(struct inode *inode,
4256 int mode, loff_t new_size, int update_ctime)
4257{
4258 struct timespec now;
4259
4260 if (update_ctime) {
4261 now = current_fs_time(inode->i_sb);
4262 if (!timespec_equal(&inode->i_ctime, &now))
4263 inode->i_ctime = now;
4264 }
4265 /*
4266 * Update only when preallocation was requested beyond
4267 * the file size.
4268 */
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04004269 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
4270 if (new_size > i_size_read(inode))
4271 i_size_write(inode, new_size);
4272 if (new_size > EXT4_I(inode)->i_disksize)
4273 ext4_update_i_disksize(inode, new_size);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004274 } else {
4275 /*
4276 * Mark that we allocate beyond EOF so the subsequent truncate
4277 * can proceed even if the new size is the same as i_size.
4278 */
4279 if (new_size > i_size_read(inode))
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004280 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004281 }
4282
4283}
4284
Amit Aroraa2df2a62007-07-17 21:42:41 -04004285/*
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004286 * preallocate space for a file. This implements ext4's fallocate file
Amit Aroraa2df2a62007-07-17 21:42:41 -04004287 * operation, which gets called from sys_fallocate system call.
4288 * For block-mapped files, posix_fallocate should fall back to the method
4289 * of writing zeroes to the required new blocks (the same behavior which is
4290 * expected for file systems which do not support fallocate() system call).
4291 */
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004292long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004293{
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004294 struct inode *inode = file->f_path.dentry->d_inode;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004295 handle_t *handle;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004296 loff_t new_size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004297 unsigned int max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004298 int ret = 0;
4299 int ret2 = 0;
4300 int retries = 0;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004301 int flags;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004302 struct ext4_map_blocks map;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004303 unsigned int credits, blkbits = inode->i_blkbits;
4304
4305 /*
4306 * currently supporting (pre)allocate mode for extent-based
4307 * files _only_
4308 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004309 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Amit Aroraa2df2a62007-07-17 21:42:41 -04004310 return -EOPNOTSUPP;
4311
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004312 /* Return error if mode is not supported */
4313 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
4314 return -EOPNOTSUPP;
4315
4316 if (mode & FALLOC_FL_PUNCH_HOLE)
4317 return ext4_punch_hole(file, offset, len);
4318
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004319 trace_ext4_fallocate_enter(inode, offset, len, mode);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004320 map.m_lblk = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004321 /*
4322 * We can't just convert len to max_blocks because
4323 * If blocksize = 4096 offset = 3072 and len = 2048
4324 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004325 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004326 - map.m_lblk;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004327 /*
Mingming Caof3bd1f32008-08-19 22:16:03 -04004328 * credits to insert 1 extent into extent tree
Amit Aroraa2df2a62007-07-17 21:42:41 -04004329 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04004330 credits = ext4_chunk_trans_blocks(inode, max_blocks);
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05004331 mutex_lock(&inode->i_mutex);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04004332 ret = inode_newsize_ok(inode, (len + offset));
4333 if (ret) {
4334 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004335 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04004336 return ret;
4337 }
Greg Harm3c6fe772011-10-31 18:41:47 -04004338 flags = EXT4_GET_BLOCKS_CREATE_UNINIT_EXT;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004339 if (mode & FALLOC_FL_KEEP_SIZE)
4340 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
Greg Harm3c6fe772011-10-31 18:41:47 -04004341 /*
4342 * Don't normalize the request if it can fit in one extent so
4343 * that it doesn't get unnecessarily split into multiple
4344 * extents.
4345 */
4346 if (len <= EXT_UNINIT_MAX_LEN << blkbits)
4347 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004348retry:
4349 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004350 map.m_lblk = map.m_lblk + ret;
4351 map.m_len = max_blocks = max_blocks - ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004352 handle = ext4_journal_start(inode, credits);
4353 if (IS_ERR(handle)) {
4354 ret = PTR_ERR(handle);
4355 break;
4356 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004357 ret = ext4_map_blocks(handle, inode, &map, flags);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05004358 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004359#ifdef EXT4FS_DEBUG
4360 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004361 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004362 "returned error inode#%lu, block=%u, "
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05004363 "max_blocks=%u", __func__,
Kazuya Mioa6371b62010-10-27 21:30:15 -04004364 inode->i_ino, map.m_lblk, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004365#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04004366 ext4_mark_inode_dirty(handle, inode);
4367 ret2 = ext4_journal_stop(handle);
4368 break;
4369 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004370 if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004371 blkbits) >> blkbits))
4372 new_size = offset + len;
4373 else
Utako Kusaka29ae07b2011-07-27 22:11:20 -04004374 new_size = ((loff_t) map.m_lblk + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004375
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004376 ext4_falloc_update_inode(inode, mode, new_size,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004377 (map.m_flags & EXT4_MAP_NEW));
Amit Aroraa2df2a62007-07-17 21:42:41 -04004378 ext4_mark_inode_dirty(handle, inode);
4379 ret2 = ext4_journal_stop(handle);
4380 if (ret2)
4381 break;
4382 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004383 if (ret == -ENOSPC &&
4384 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4385 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004386 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004387 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05004388 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004389 trace_ext4_fallocate_exit(inode, offset, max_blocks,
4390 ret > 0 ? ret2 : ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004391 return ret > 0 ? ret2 : ret;
4392}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004393
4394/*
Mingming Cao00314622009-09-28 15:49:08 -04004395 * This function convert a range of blocks to written extents
4396 * The caller of this function will pass the start offset and the size.
4397 * all unwritten extents within this range will be converted to
4398 * written extents.
4399 *
4400 * This function is called from the direct IO end io call back
4401 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05004402 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04004403 */
4404int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
Eric Sandeena1de02d2010-02-04 23:58:38 -05004405 ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04004406{
4407 handle_t *handle;
Mingming Cao00314622009-09-28 15:49:08 -04004408 unsigned int max_blocks;
4409 int ret = 0;
4410 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004411 struct ext4_map_blocks map;
Mingming Cao00314622009-09-28 15:49:08 -04004412 unsigned int credits, blkbits = inode->i_blkbits;
4413
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004414 map.m_lblk = offset >> blkbits;
Mingming Cao00314622009-09-28 15:49:08 -04004415 /*
4416 * We can't just convert len to max_blocks because
4417 * If blocksize = 4096 offset = 3072 and len = 2048
4418 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004419 max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
4420 map.m_lblk);
Mingming Cao00314622009-09-28 15:49:08 -04004421 /*
4422 * credits to insert 1 extent into extent tree
4423 */
4424 credits = ext4_chunk_trans_blocks(inode, max_blocks);
4425 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004426 map.m_lblk += ret;
4427 map.m_len = (max_blocks -= ret);
Mingming Cao00314622009-09-28 15:49:08 -04004428 handle = ext4_journal_start(inode, credits);
4429 if (IS_ERR(handle)) {
4430 ret = PTR_ERR(handle);
4431 break;
4432 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004433 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004434 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Mingming Cao00314622009-09-28 15:49:08 -04004435 if (ret <= 0) {
4436 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004437 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Mingming Cao00314622009-09-28 15:49:08 -04004438 "returned error inode#%lu, block=%u, "
4439 "max_blocks=%u", __func__,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004440 inode->i_ino, map.m_lblk, map.m_len);
Mingming Cao00314622009-09-28 15:49:08 -04004441 }
4442 ext4_mark_inode_dirty(handle, inode);
4443 ret2 = ext4_journal_stop(handle);
4444 if (ret <= 0 || ret2 )
4445 break;
4446 }
4447 return ret > 0 ? ret2 : ret;
4448}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004449
Mingming Cao00314622009-09-28 15:49:08 -04004450/*
Eric Sandeen6873fa02008-10-07 00:46:36 -04004451 * Callback function called for each extent to gather FIEMAP information.
4452 */
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04004453static int ext4_ext_fiemap_cb(struct inode *inode, ext4_lblk_t next,
Eric Sandeen6873fa02008-10-07 00:46:36 -04004454 struct ext4_ext_cache *newex, struct ext4_extent *ex,
4455 void *data)
4456{
Eric Sandeen6873fa02008-10-07 00:46:36 -04004457 __u64 logical;
4458 __u64 physical;
4459 __u64 length;
4460 __u32 flags = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004461 int ret = 0;
4462 struct fiemap_extent_info *fieinfo = data;
4463 unsigned char blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004464
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004465 blksize_bits = inode->i_sb->s_blocksize_bits;
4466 logical = (__u64)newex->ec_block << blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004467
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05004468 if (newex->ec_start == 0) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004469 /*
4470 * No extent in extent-tree contains block @newex->ec_start,
4471 * then the block may stay in 1)a hole or 2)delayed-extent.
4472 *
4473 * Holes or delayed-extents are processed as follows.
4474 * 1. lookup dirty pages with specified range in pagecache.
4475 * If no page is got, then there is no delayed-extent and
4476 * return with EXT_CONTINUE.
4477 * 2. find the 1st mapped buffer,
4478 * 3. check if the mapped buffer is both in the request range
4479 * and a delayed buffer. If not, there is no delayed-extent,
4480 * then return.
4481 * 4. a delayed-extent is found, the extent will be collected.
4482 */
4483 ext4_lblk_t end = 0;
4484 pgoff_t last_offset;
4485 pgoff_t offset;
4486 pgoff_t index;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004487 pgoff_t start_index = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004488 struct page **pages = NULL;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004489 struct buffer_head *bh = NULL;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004490 struct buffer_head *head = NULL;
4491 unsigned int nr_pages = PAGE_SIZE / sizeof(struct page *);
4492
4493 pages = kmalloc(PAGE_SIZE, GFP_KERNEL);
4494 if (pages == NULL)
4495 return -ENOMEM;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004496
4497 offset = logical >> PAGE_SHIFT;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004498repeat:
4499 last_offset = offset;
4500 head = NULL;
4501 ret = find_get_pages_tag(inode->i_mapping, &offset,
4502 PAGECACHE_TAG_DIRTY, nr_pages, pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004503
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004504 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4505 /* First time, try to find a mapped buffer. */
4506 if (ret == 0) {
4507out:
4508 for (index = 0; index < ret; index++)
4509 page_cache_release(pages[index]);
4510 /* just a hole. */
4511 kfree(pages);
4512 return EXT_CONTINUE;
4513 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004514 index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004515
Yongqiang Yangb2213492011-05-24 11:36:58 -04004516next_page:
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004517 /* Try to find the 1st mapped buffer. */
Yongqiang Yangb2213492011-05-24 11:36:58 -04004518 end = ((__u64)pages[index]->index << PAGE_SHIFT) >>
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004519 blksize_bits;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004520 if (!page_has_buffers(pages[index]))
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004521 goto out;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004522 head = page_buffers(pages[index]);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004523 if (!head)
4524 goto out;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004525
Yongqiang Yangb2213492011-05-24 11:36:58 -04004526 index++;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004527 bh = head;
4528 do {
Yongqiang Yangb2213492011-05-24 11:36:58 -04004529 if (end >= newex->ec_block +
4530 newex->ec_len)
4531 /* The buffer is out of
4532 * the request range.
4533 */
4534 goto out;
4535
4536 if (buffer_mapped(bh) &&
4537 end >= newex->ec_block) {
4538 start_index = index - 1;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004539 /* get the 1st mapped buffer. */
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004540 goto found_mapped_buffer;
4541 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004542
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004543 bh = bh->b_this_page;
4544 end++;
4545 } while (bh != head);
4546
Yongqiang Yangb2213492011-05-24 11:36:58 -04004547 /* No mapped buffer in the range found in this page,
4548 * We need to look up next page.
4549 */
4550 if (index >= ret) {
4551 /* There is no page left, but we need to limit
4552 * newex->ec_len.
4553 */
4554 newex->ec_len = end - newex->ec_block;
4555 goto out;
4556 }
4557 goto next_page;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004558 } else {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004559 /*Find contiguous delayed buffers. */
4560 if (ret > 0 && pages[0]->index == last_offset)
4561 head = page_buffers(pages[0]);
4562 bh = head;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004563 index = 1;
4564 start_index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004565 }
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004566
4567found_mapped_buffer:
4568 if (bh != NULL && buffer_delay(bh)) {
4569 /* 1st or contiguous delayed buffer found. */
4570 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4571 /*
4572 * 1st delayed buffer found, record
4573 * the start of extent.
4574 */
4575 flags |= FIEMAP_EXTENT_DELALLOC;
4576 newex->ec_block = end;
4577 logical = (__u64)end << blksize_bits;
4578 }
4579 /* Find contiguous delayed buffers. */
4580 do {
4581 if (!buffer_delay(bh))
4582 goto found_delayed_extent;
4583 bh = bh->b_this_page;
4584 end++;
4585 } while (bh != head);
4586
Yongqiang Yangb2213492011-05-24 11:36:58 -04004587 for (; index < ret; index++) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004588 if (!page_has_buffers(pages[index])) {
4589 bh = NULL;
4590 break;
4591 }
4592 head = page_buffers(pages[index]);
4593 if (!head) {
4594 bh = NULL;
4595 break;
4596 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004597
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004598 if (pages[index]->index !=
Yongqiang Yangb2213492011-05-24 11:36:58 -04004599 pages[start_index]->index + index
4600 - start_index) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004601 /* Blocks are not contiguous. */
4602 bh = NULL;
4603 break;
4604 }
4605 bh = head;
4606 do {
4607 if (!buffer_delay(bh))
4608 /* Delayed-extent ends. */
4609 goto found_delayed_extent;
4610 bh = bh->b_this_page;
4611 end++;
4612 } while (bh != head);
4613 }
4614 } else if (!(flags & FIEMAP_EXTENT_DELALLOC))
4615 /* a hole found. */
4616 goto out;
4617
4618found_delayed_extent:
4619 newex->ec_len = min(end - newex->ec_block,
4620 (ext4_lblk_t)EXT_INIT_MAX_LEN);
4621 if (ret == nr_pages && bh != NULL &&
4622 newex->ec_len < EXT_INIT_MAX_LEN &&
4623 buffer_delay(bh)) {
4624 /* Have not collected an extent and continue. */
4625 for (index = 0; index < ret; index++)
4626 page_cache_release(pages[index]);
4627 goto repeat;
4628 }
4629
4630 for (index = 0; index < ret; index++)
4631 page_cache_release(pages[index]);
4632 kfree(pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004633 }
4634
4635 physical = (__u64)newex->ec_start << blksize_bits;
4636 length = (__u64)newex->ec_len << blksize_bits;
4637
4638 if (ex && ext4_ext_is_uninitialized(ex))
4639 flags |= FIEMAP_EXTENT_UNWRITTEN;
4640
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04004641 if (next == EXT_MAX_BLOCKS)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004642 flags |= FIEMAP_EXTENT_LAST;
4643
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004644 ret = fiemap_fill_next_extent(fieinfo, logical, physical,
Eric Sandeen6873fa02008-10-07 00:46:36 -04004645 length, flags);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004646 if (ret < 0)
4647 return ret;
4648 if (ret == 1)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004649 return EXT_BREAK;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004650 return EXT_CONTINUE;
4651}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004652/* fiemap flags we can handle specified here */
4653#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
4654
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004655static int ext4_xattr_fiemap(struct inode *inode,
4656 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004657{
4658 __u64 physical = 0;
4659 __u64 length;
4660 __u32 flags = FIEMAP_EXTENT_LAST;
4661 int blockbits = inode->i_sb->s_blocksize_bits;
4662 int error = 0;
4663
4664 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004665 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04004666 struct ext4_iloc iloc;
4667 int offset; /* offset of xattr in inode */
4668
4669 error = ext4_get_inode_loc(inode, &iloc);
4670 if (error)
4671 return error;
4672 physical = iloc.bh->b_blocknr << blockbits;
4673 offset = EXT4_GOOD_OLD_INODE_SIZE +
4674 EXT4_I(inode)->i_extra_isize;
4675 physical += offset;
4676 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
4677 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004678 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004679 } else { /* external block */
4680 physical = EXT4_I(inode)->i_file_acl << blockbits;
4681 length = inode->i_sb->s_blocksize;
4682 }
4683
4684 if (physical)
4685 error = fiemap_fill_next_extent(fieinfo, 0, physical,
4686 length, flags);
4687 return (error < 0 ? error : 0);
4688}
4689
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004690/*
4691 * ext4_ext_punch_hole
4692 *
4693 * Punches a hole of "length" bytes in a file starting
4694 * at byte "offset"
4695 *
4696 * @inode: The inode of the file to punch a hole in
4697 * @offset: The starting byte offset of the hole
4698 * @length: The length of the hole
4699 *
4700 * Returns the number of blocks removed or negative on err
4701 */
4702int ext4_ext_punch_hole(struct file *file, loff_t offset, loff_t length)
4703{
4704 struct inode *inode = file->f_path.dentry->d_inode;
4705 struct super_block *sb = inode->i_sb;
4706 struct ext4_ext_cache cache_ex;
4707 ext4_lblk_t first_block, last_block, num_blocks, iblock, max_blocks;
4708 struct address_space *mapping = inode->i_mapping;
4709 struct ext4_map_blocks map;
4710 handle_t *handle;
Allison Hendersonba062082011-09-03 11:55:59 -04004711 loff_t first_page, last_page, page_len;
4712 loff_t first_page_offset, last_page_offset;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004713 int ret, credits, blocks_released, err = 0;
4714
Allison Henderson2be47512011-09-03 11:56:52 -04004715 /* No need to punch hole beyond i_size */
4716 if (offset >= inode->i_size)
4717 return 0;
4718
4719 /*
4720 * If the hole extends beyond i_size, set the hole
4721 * to end after the page that contains i_size
4722 */
4723 if (offset + length > inode->i_size) {
4724 length = inode->i_size +
4725 PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) -
4726 offset;
4727 }
4728
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004729 first_block = (offset + sb->s_blocksize - 1) >>
4730 EXT4_BLOCK_SIZE_BITS(sb);
4731 last_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
4732
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004733 first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
4734 last_page = (offset + length) >> PAGE_CACHE_SHIFT;
4735
4736 first_page_offset = first_page << PAGE_CACHE_SHIFT;
4737 last_page_offset = last_page << PAGE_CACHE_SHIFT;
4738
4739 /*
4740 * Write out all dirty pages to avoid race conditions
4741 * Then release them.
4742 */
4743 if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
4744 err = filemap_write_and_wait_range(mapping,
Allison Henderson2be47512011-09-03 11:56:52 -04004745 offset, offset + length - 1);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004746
Allison Henderson2be47512011-09-03 11:56:52 -04004747 if (err)
4748 return err;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004749 }
4750
4751 /* Now release the pages */
4752 if (last_page_offset > first_page_offset) {
4753 truncate_inode_pages_range(mapping, first_page_offset,
4754 last_page_offset-1);
4755 }
4756
4757 /* finish any pending end_io work */
4758 ext4_flush_completed_IO(inode);
4759
4760 credits = ext4_writepage_trans_blocks(inode);
4761 handle = ext4_journal_start(inode, credits);
4762 if (IS_ERR(handle))
4763 return PTR_ERR(handle);
4764
4765 err = ext4_orphan_add(handle, inode);
4766 if (err)
4767 goto out;
4768
4769 /*
Allison Hendersonba062082011-09-03 11:55:59 -04004770 * Now we need to zero out the non-page-aligned data in the
4771 * pages at the start and tail of the hole, and unmap the buffer
4772 * heads for the block aligned regions of the page that were
4773 * completely zeroed.
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004774 */
Allison Hendersonba062082011-09-03 11:55:59 -04004775 if (first_page > last_page) {
4776 /*
4777 * If the file space being truncated is contained within a page
4778 * just zero out and unmap the middle of that page
4779 */
4780 err = ext4_discard_partial_page_buffers(handle,
4781 mapping, offset, length, 0);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004782
Allison Hendersonba062082011-09-03 11:55:59 -04004783 if (err)
4784 goto out;
4785 } else {
4786 /*
4787 * zero out and unmap the partial page that contains
4788 * the start of the hole
4789 */
4790 page_len = first_page_offset - offset;
4791 if (page_len > 0) {
4792 err = ext4_discard_partial_page_buffers(handle, mapping,
4793 offset, page_len, 0);
4794 if (err)
4795 goto out;
4796 }
4797
4798 /*
4799 * zero out and unmap the partial page that contains
4800 * the end of the hole
4801 */
4802 page_len = offset + length - last_page_offset;
4803 if (page_len > 0) {
4804 err = ext4_discard_partial_page_buffers(handle, mapping,
4805 last_page_offset, page_len, 0);
4806 if (err)
4807 goto out;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004808 }
4809 }
4810
Allison Henderson2be47512011-09-03 11:56:52 -04004811
4812 /*
4813 * If i_size is contained in the last page, we need to
4814 * unmap and zero the partial page after i_size
4815 */
4816 if (inode->i_size >> PAGE_CACHE_SHIFT == last_page &&
4817 inode->i_size % PAGE_CACHE_SIZE != 0) {
4818
4819 page_len = PAGE_CACHE_SIZE -
4820 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4821
4822 if (page_len > 0) {
4823 err = ext4_discard_partial_page_buffers(handle,
4824 mapping, inode->i_size, page_len, 0);
4825
4826 if (err)
4827 goto out;
4828 }
4829 }
4830
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004831 /* If there are no blocks to remove, return now */
4832 if (first_block >= last_block)
4833 goto out;
4834
4835 down_write(&EXT4_I(inode)->i_data_sem);
4836 ext4_ext_invalidate_cache(inode);
4837 ext4_discard_preallocations(inode);
4838
4839 /*
4840 * Loop over all the blocks and identify blocks
4841 * that need to be punched out
4842 */
4843 iblock = first_block;
4844 blocks_released = 0;
4845 while (iblock < last_block) {
4846 max_blocks = last_block - iblock;
4847 num_blocks = 1;
4848 memset(&map, 0, sizeof(map));
4849 map.m_lblk = iblock;
4850 map.m_len = max_blocks;
4851 ret = ext4_ext_map_blocks(handle, inode, &map,
4852 EXT4_GET_BLOCKS_PUNCH_OUT_EXT);
4853
4854 if (ret > 0) {
4855 blocks_released += ret;
4856 num_blocks = ret;
4857 } else if (ret == 0) {
4858 /*
4859 * If map blocks could not find the block,
4860 * then it is in a hole. If the hole was
4861 * not already cached, then map blocks should
4862 * put it in the cache. So we can get the hole
4863 * out of the cache
4864 */
4865 memset(&cache_ex, 0, sizeof(cache_ex));
4866 if ((ext4_ext_check_cache(inode, iblock, &cache_ex)) &&
4867 !cache_ex.ec_start) {
4868
4869 /* The hole is cached */
4870 num_blocks = cache_ex.ec_block +
4871 cache_ex.ec_len - iblock;
4872
4873 } else {
4874 /* The block could not be identified */
4875 err = -EIO;
4876 break;
4877 }
4878 } else {
4879 /* Map blocks error */
4880 err = ret;
4881 break;
4882 }
4883
4884 if (num_blocks == 0) {
4885 /* This condition should never happen */
4886 ext_debug("Block lookup failed");
4887 err = -EIO;
4888 break;
4889 }
4890
4891 iblock += num_blocks;
4892 }
4893
4894 if (blocks_released > 0) {
4895 ext4_ext_invalidate_cache(inode);
4896 ext4_discard_preallocations(inode);
4897 }
4898
4899 if (IS_SYNC(inode))
4900 ext4_handle_sync(handle);
4901
4902 up_write(&EXT4_I(inode)->i_data_sem);
4903
4904out:
4905 ext4_orphan_del(handle, inode);
4906 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4907 ext4_mark_inode_dirty(handle, inode);
4908 ext4_journal_stop(handle);
4909 return err;
4910}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004911int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4912 __u64 start, __u64 len)
4913{
4914 ext4_lblk_t start_blk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004915 int error = 0;
4916
4917 /* fallback to generic here if not in extents fmt */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004918 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeen6873fa02008-10-07 00:46:36 -04004919 return generic_block_fiemap(inode, fieinfo, start, len,
4920 ext4_get_block);
4921
4922 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
4923 return -EBADR;
4924
4925 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
4926 error = ext4_xattr_fiemap(inode, fieinfo);
4927 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004928 ext4_lblk_t len_blks;
4929 __u64 last_blk;
4930
Eric Sandeen6873fa02008-10-07 00:46:36 -04004931 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004932 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04004933 if (last_blk >= EXT_MAX_BLOCKS)
4934 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004935 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004936
4937 /*
4938 * Walk the extent tree gathering extent information.
4939 * ext4_ext_fiemap_cb will push extents back to user.
4940 */
Eric Sandeen6873fa02008-10-07 00:46:36 -04004941 error = ext4_ext_walk_space(inode, start_blk, len_blks,
4942 ext4_ext_fiemap_cb, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004943 }
4944
4945 return error;
4946}