blob: 29969622af8b2fe812dd55427efe282ec1e17945 [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 int depth;
121
122 if (path) {
123 struct ext4_extent *ex;
124 depth = path->p_depth;
125
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500126 /*
127 * Try to predict block placement assuming that we are
128 * filling in a file which will eventually be
129 * non-sparse --- i.e., in the case of libbfd writing
130 * an ELF object sections out-of-order but in a way
131 * the eventually results in a contiguous object or
132 * executable file, or some database extending a table
133 * space file. However, this is actually somewhat
134 * non-ideal if we are writing a sparse file such as
135 * qemu or KVM writing a raw image file that is going
136 * to stay fairly sparse, since it will end up
137 * fragmenting the file system's free space. Maybe we
138 * should have some hueristics or some way to allow
139 * userspace to pass a hint to file system,
Tao Mab8d65682011-01-21 23:21:31 +0800140 * especially if the latter case turns out to be
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500141 * common.
142 */
Avantika Mathur7e028972006-12-06 20:41:33 -0800143 ex = path[depth].p_ext;
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500144 if (ex) {
145 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
146 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
147
148 if (block > ext_block)
149 return ext_pblk + (block - ext_block);
150 else
151 return ext_pblk - (ext_block - block);
152 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700153
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700154 /* it looks like index is empty;
155 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700156 if (path[depth].p_bh)
157 return path[depth].p_bh->b_blocknr;
158 }
159
160 /* OK. use inode's group */
Eric Sandeenf86186b2011-06-28 10:01:31 -0400161 return ext4_inode_to_goal_block(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700162}
163
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400164/*
165 * Allocation for a meta data block
166 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700167static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400168ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700169 struct ext4_ext_path *path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400170 struct ext4_extent *ex, int *err, unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700171{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700172 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700173
174 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Allison Henderson55f020d2011-05-25 07:41:26 -0400175 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
176 NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700177 return newblock;
178}
179
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400180static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700181{
182 int size;
183
184 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
185 / sizeof(struct ext4_extent);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400186 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100187#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400188 if (size > 6)
189 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700190#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400191 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700192 return size;
193}
194
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400195static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700196{
197 int size;
198
199 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
200 / sizeof(struct ext4_extent_idx);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400201 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100202#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400203 if (size > 5)
204 size = 5;
Alex Tomasa86c6182006-10-11 01:21:03 -0700205#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400206 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700207 return size;
208}
209
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400210static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700211{
212 int size;
213
214 size = sizeof(EXT4_I(inode)->i_data);
215 size -= sizeof(struct ext4_extent_header);
216 size /= sizeof(struct ext4_extent);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400217 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100218#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400219 if (size > 3)
220 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700221#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400222 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700223 return size;
224}
225
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400226static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700227{
228 int size;
229
230 size = sizeof(EXT4_I(inode)->i_data);
231 size -= sizeof(struct ext4_extent_header);
232 size /= sizeof(struct ext4_extent_idx);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400233 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100234#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400235 if (size > 4)
236 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700237#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400238 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700239 return size;
240}
241
Mingming Caod2a17632008-07-14 17:52:37 -0400242/*
243 * Calculate the number of metadata blocks needed
244 * to allocate @blocks
245 * Worse case is one block per extent
246 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -0500247int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -0400248{
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500249 struct ext4_inode_info *ei = EXT4_I(inode);
250 int idxs, num = 0;
Mingming Caod2a17632008-07-14 17:52:37 -0400251
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500252 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
253 / sizeof(struct ext4_extent_idx));
Mingming Caod2a17632008-07-14 17:52:37 -0400254
255 /*
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500256 * If the new delayed allocation block is contiguous with the
257 * previous da block, it can share index blocks with the
258 * previous block, so we only need to allocate a new index
259 * block every idxs leaf blocks. At ldxs**2 blocks, we need
260 * an additional index block, and at ldxs**3 blocks, yet
261 * another index blocks.
Mingming Caod2a17632008-07-14 17:52:37 -0400262 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500263 if (ei->i_da_metadata_calc_len &&
264 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
265 if ((ei->i_da_metadata_calc_len % idxs) == 0)
266 num++;
267 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
268 num++;
269 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
270 num++;
271 ei->i_da_metadata_calc_len = 0;
272 } else
273 ei->i_da_metadata_calc_len++;
274 ei->i_da_metadata_calc_last_lblock++;
275 return num;
276 }
Mingming Caod2a17632008-07-14 17:52:37 -0400277
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500278 /*
279 * In the worst case we need a new set of index blocks at
280 * every level of the inode's extent tree.
281 */
282 ei->i_da_metadata_calc_len = 1;
283 ei->i_da_metadata_calc_last_lblock = lblock;
284 return ext_depth(inode) + 1;
Mingming Caod2a17632008-07-14 17:52:37 -0400285}
286
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400287static int
288ext4_ext_max_entries(struct inode *inode, int depth)
289{
290 int max;
291
292 if (depth == ext_depth(inode)) {
293 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400294 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400295 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400296 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400297 } else {
298 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400299 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400300 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400301 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400302 }
303
304 return max;
305}
306
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400307static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
308{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400309 ext4_fsblk_t block = ext4_ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400310 int len = ext4_ext_get_actual_len(ext);
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, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400313}
314
315static int ext4_valid_extent_idx(struct inode *inode,
316 struct ext4_extent_idx *ext_idx)
317{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400318 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400319
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400320 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400321}
322
323static int ext4_valid_extent_entries(struct inode *inode,
324 struct ext4_extent_header *eh,
325 int depth)
326{
327 struct ext4_extent *ext;
328 struct ext4_extent_idx *ext_idx;
329 unsigned short entries;
330 if (eh->eh_entries == 0)
331 return 1;
332
333 entries = le16_to_cpu(eh->eh_entries);
334
335 if (depth == 0) {
336 /* leaf entries */
337 ext = EXT_FIRST_EXTENT(eh);
338 while (entries) {
339 if (!ext4_valid_extent(inode, ext))
340 return 0;
341 ext++;
342 entries--;
343 }
344 } else {
345 ext_idx = EXT_FIRST_INDEX(eh);
346 while (entries) {
347 if (!ext4_valid_extent_idx(inode, ext_idx))
348 return 0;
349 ext_idx++;
350 entries--;
351 }
352 }
353 return 1;
354}
355
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400356static int __ext4_ext_check(const char *function, unsigned int line,
357 struct inode *inode, struct ext4_extent_header *eh,
358 int depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400359{
360 const char *error_msg;
361 int max = 0;
362
363 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
364 error_msg = "invalid magic";
365 goto corrupted;
366 }
367 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
368 error_msg = "unexpected eh_depth";
369 goto corrupted;
370 }
371 if (unlikely(eh->eh_max == 0)) {
372 error_msg = "invalid eh_max";
373 goto corrupted;
374 }
375 max = ext4_ext_max_entries(inode, depth);
376 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
377 error_msg = "too large eh_max";
378 goto corrupted;
379 }
380 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
381 error_msg = "invalid eh_entries";
382 goto corrupted;
383 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400384 if (!ext4_valid_extent_entries(inode, eh, depth)) {
385 error_msg = "invalid extent entries";
386 goto corrupted;
387 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400388 return 0;
389
390corrupted:
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400391 ext4_error_inode(inode, function, line, 0,
Theodore Ts'o24676da2010-05-16 21:00:00 -0400392 "bad header/extent: %s - magic %x, "
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400393 "entries %u, max %u(%u), depth %u(%u)",
Theodore Ts'o24676da2010-05-16 21:00:00 -0400394 error_msg, le16_to_cpu(eh->eh_magic),
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400395 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
396 max, le16_to_cpu(eh->eh_depth), depth);
397
398 return -EIO;
399}
400
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400401#define ext4_ext_check(inode, eh, depth) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400402 __ext4_ext_check(__func__, __LINE__, inode, eh, depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400403
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400404int ext4_ext_check_inode(struct inode *inode)
405{
406 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
407}
408
Alex Tomasa86c6182006-10-11 01:21:03 -0700409#ifdef EXT_DEBUG
410static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
411{
412 int k, l = path->p_depth;
413
414 ext_debug("path:");
415 for (k = 0; k <= l; k++, path++) {
416 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700417 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400418 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700419 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400420 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700421 le32_to_cpu(path->p_ext->ee_block),
Mingming553f9002009-09-18 13:34:55 -0400422 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400423 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400424 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700425 } else
426 ext_debug(" []");
427 }
428 ext_debug("\n");
429}
430
431static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
432{
433 int depth = ext_depth(inode);
434 struct ext4_extent_header *eh;
435 struct ext4_extent *ex;
436 int i;
437
438 if (!path)
439 return;
440
441 eh = path[depth].p_hdr;
442 ex = EXT_FIRST_EXTENT(eh);
443
Mingming553f9002009-09-18 13:34:55 -0400444 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
445
Alex Tomasa86c6182006-10-11 01:21:03 -0700446 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400447 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
448 ext4_ext_is_uninitialized(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400449 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700450 }
451 ext_debug("\n");
452}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400453
454static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
455 ext4_fsblk_t newblock, int level)
456{
457 int depth = ext_depth(inode);
458 struct ext4_extent *ex;
459
460 if (depth != level) {
461 struct ext4_extent_idx *idx;
462 idx = path[level].p_idx;
463 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
464 ext_debug("%d: move %d:%llu in new index %llu\n", level,
465 le32_to_cpu(idx->ei_block),
466 ext4_idx_pblock(idx),
467 newblock);
468 idx++;
469 }
470
471 return;
472 }
473
474 ex = path[depth].p_ext;
475 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
476 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
477 le32_to_cpu(ex->ee_block),
478 ext4_ext_pblock(ex),
479 ext4_ext_is_uninitialized(ex),
480 ext4_ext_get_actual_len(ex),
481 newblock);
482 ex++;
483 }
484}
485
Alex Tomasa86c6182006-10-11 01:21:03 -0700486#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400487#define ext4_ext_show_path(inode, path)
488#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400489#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700490#endif
491
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500492void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700493{
494 int depth = path->p_depth;
495 int i;
496
497 for (i = 0; i <= depth; i++, path++)
498 if (path->p_bh) {
499 brelse(path->p_bh);
500 path->p_bh = NULL;
501 }
502}
503
504/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700505 * ext4_ext_binsearch_idx:
506 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400507 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700508 */
509static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500510ext4_ext_binsearch_idx(struct inode *inode,
511 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700512{
513 struct ext4_extent_header *eh = path->p_hdr;
514 struct ext4_extent_idx *r, *l, *m;
515
Alex Tomasa86c6182006-10-11 01:21:03 -0700516
Eric Sandeenbba90742008-01-28 23:58:27 -0500517 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700518
519 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400520 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700521 while (l <= r) {
522 m = l + (r - l) / 2;
523 if (block < le32_to_cpu(m->ei_block))
524 r = m - 1;
525 else
526 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400527 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
528 m, le32_to_cpu(m->ei_block),
529 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700530 }
531
532 path->p_idx = l - 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700533 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400534 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700535
536#ifdef CHECK_BINSEARCH
537 {
538 struct ext4_extent_idx *chix, *ix;
539 int k;
540
541 chix = ix = EXT_FIRST_INDEX(eh);
542 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
543 if (k != 0 &&
544 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400545 printk(KERN_DEBUG "k=%d, ix=0x%p, "
546 "first=0x%p\n", k,
547 ix, EXT_FIRST_INDEX(eh));
548 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700549 le32_to_cpu(ix->ei_block),
550 le32_to_cpu(ix[-1].ei_block));
551 }
552 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400553 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700554 if (block < le32_to_cpu(ix->ei_block))
555 break;
556 chix = ix;
557 }
558 BUG_ON(chix != path->p_idx);
559 }
560#endif
561
562}
563
564/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700565 * ext4_ext_binsearch:
566 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400567 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700568 */
569static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500570ext4_ext_binsearch(struct inode *inode,
571 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700572{
573 struct ext4_extent_header *eh = path->p_hdr;
574 struct ext4_extent *r, *l, *m;
575
Alex Tomasa86c6182006-10-11 01:21:03 -0700576 if (eh->eh_entries == 0) {
577 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700578 * this leaf is empty:
579 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700580 */
581 return;
582 }
583
Eric Sandeenbba90742008-01-28 23:58:27 -0500584 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700585
586 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400587 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700588
589 while (l <= r) {
590 m = l + (r - l) / 2;
591 if (block < le32_to_cpu(m->ee_block))
592 r = m - 1;
593 else
594 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400595 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
596 m, le32_to_cpu(m->ee_block),
597 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700598 }
599
600 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400601 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400602 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400603 ext4_ext_pblock(path->p_ext),
Mingming553f9002009-09-18 13:34:55 -0400604 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400605 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700606
607#ifdef CHECK_BINSEARCH
608 {
609 struct ext4_extent *chex, *ex;
610 int k;
611
612 chex = ex = EXT_FIRST_EXTENT(eh);
613 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
614 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400615 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700616 if (block < le32_to_cpu(ex->ee_block))
617 break;
618 chex = ex;
619 }
620 BUG_ON(chex != path->p_ext);
621 }
622#endif
623
624}
625
626int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
627{
628 struct ext4_extent_header *eh;
629
630 eh = ext_inode_hdr(inode);
631 eh->eh_depth = 0;
632 eh->eh_entries = 0;
633 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400634 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700635 ext4_mark_inode_dirty(handle, inode);
636 ext4_ext_invalidate_cache(inode);
637 return 0;
638}
639
640struct ext4_ext_path *
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500641ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
642 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700643{
644 struct ext4_extent_header *eh;
645 struct buffer_head *bh;
646 short int depth, i, ppos = 0, alloc = 0;
647
648 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400649 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700650
651 /* account possible depth increase */
652 if (!path) {
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800653 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700654 GFP_NOFS);
655 if (!path)
656 return ERR_PTR(-ENOMEM);
657 alloc = 1;
658 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700659 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400660 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700661
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400662 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700663 /* walk through the tree */
664 while (i) {
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400665 int need_to_validate = 0;
666
Alex Tomasa86c6182006-10-11 01:21:03 -0700667 ext_debug("depth %d: num %d, max %d\n",
668 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400669
Alex Tomasa86c6182006-10-11 01:21:03 -0700670 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400671 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700672 path[ppos].p_depth = i;
673 path[ppos].p_ext = NULL;
674
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400675 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
676 if (unlikely(!bh))
Alex Tomasa86c6182006-10-11 01:21:03 -0700677 goto err;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400678 if (!bh_uptodate_or_lock(bh)) {
Jiaying Zhang0562e0b2011-03-21 21:38:05 -0400679 trace_ext4_ext_load_extent(inode, block,
680 path[ppos].p_block);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400681 if (bh_submit_read(bh) < 0) {
682 put_bh(bh);
683 goto err;
684 }
685 /* validate the extent entries */
686 need_to_validate = 1;
687 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700688 eh = ext_block_hdr(bh);
689 ppos++;
Frank Mayhar273df552010-03-02 11:46:09 -0500690 if (unlikely(ppos > depth)) {
691 put_bh(bh);
692 EXT4_ERROR_INODE(inode,
693 "ppos %d > depth %d", ppos, depth);
694 goto err;
695 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700696 path[ppos].p_bh = bh;
697 path[ppos].p_hdr = eh;
698 i--;
699
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400700 if (need_to_validate && ext4_ext_check(inode, eh, i))
Alex Tomasa86c6182006-10-11 01:21:03 -0700701 goto err;
702 }
703
704 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700705 path[ppos].p_ext = NULL;
706 path[ppos].p_idx = NULL;
707
Alex Tomasa86c6182006-10-11 01:21:03 -0700708 /* find extent */
709 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400710 /* if not an empty leaf */
711 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400712 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700713
714 ext4_ext_show_path(inode, path);
715
716 return path;
717
718err:
719 ext4_ext_drop_refs(path);
720 if (alloc)
721 kfree(path);
722 return ERR_PTR(-EIO);
723}
724
725/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700726 * ext4_ext_insert_index:
727 * insert new index [@logical;@ptr] into the block at @curp;
728 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700729 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400730static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
731 struct ext4_ext_path *curp,
732 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700733{
734 struct ext4_extent_idx *ix;
735 int len, err;
736
Avantika Mathur7e028972006-12-06 20:41:33 -0800737 err = ext4_ext_get_access(handle, inode, curp);
738 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700739 return err;
740
Frank Mayhar273df552010-03-02 11:46:09 -0500741 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
742 EXT4_ERROR_INODE(inode,
743 "logical %d == ei_block %d!",
744 logical, le32_to_cpu(curp->p_idx->ei_block));
745 return -EIO;
746 }
Robin Dongd4620312011-07-17 23:43:42 -0400747
748 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
749 >= le16_to_cpu(curp->p_hdr->eh_max))) {
750 EXT4_ERROR_INODE(inode,
751 "eh_entries %d >= eh_max %d!",
752 le16_to_cpu(curp->p_hdr->eh_entries),
753 le16_to_cpu(curp->p_hdr->eh_max));
754 return -EIO;
755 }
756
Alex Tomasa86c6182006-10-11 01:21:03 -0700757 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
758 /* insert after */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400759 ext_debug("insert new index %d after: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700760 ix = curp->p_idx + 1;
761 } else {
762 /* insert before */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400763 ext_debug("insert new index %d before: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700764 ix = curp->p_idx;
765 }
766
Eric Gouriou80e675f2011-10-27 11:52:18 -0400767 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
768 BUG_ON(len < 0);
769 if (len > 0) {
770 ext_debug("insert new index %d: "
771 "move %d indices from 0x%p to 0x%p\n",
772 logical, len, ix, ix + 1);
773 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
774 }
775
Tao Maf472e022011-10-17 10:13:46 -0400776 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
777 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
778 return -EIO;
779 }
780
Alex Tomasa86c6182006-10-11 01:21:03 -0700781 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700782 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400783 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700784
Frank Mayhar273df552010-03-02 11:46:09 -0500785 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
786 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
787 return -EIO;
788 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700789
790 err = ext4_ext_dirty(handle, inode, curp);
791 ext4_std_error(inode->i_sb, err);
792
793 return err;
794}
795
796/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700797 * ext4_ext_split:
798 * inserts new subtree into the path, using free index entry
799 * at depth @at:
800 * - allocates all needed blocks (new leaf and all intermediate index blocks)
801 * - makes decision where to split
802 * - moves remaining extents and index entries (right to the split point)
803 * into the newly allocated blocks
804 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700805 */
806static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -0400807 unsigned int flags,
808 struct ext4_ext_path *path,
809 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -0700810{
811 struct buffer_head *bh = NULL;
812 int depth = ext_depth(inode);
813 struct ext4_extent_header *neh;
814 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -0700815 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700816 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700817 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700818 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700819 int err = 0;
820
821 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700822 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700823
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700824 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700825 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -0500826 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
827 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
828 return -EIO;
829 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700830 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
831 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700832 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700833 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400834 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700835 } else {
836 border = newext->ee_block;
837 ext_debug("leaf will be added."
838 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400839 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700840 }
841
842 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700843 * If error occurs, then we break processing
844 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700845 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700846 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700847 */
848
849 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700850 * Get array to track all allocated blocks.
851 * We need this to handle errors and free blocks
852 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700853 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800854 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700855 if (!ablocks)
856 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700857
858 /* allocate all needed blocks */
859 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
860 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400861 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400862 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -0700863 if (newblock == 0)
864 goto cleanup;
865 ablocks[a] = newblock;
866 }
867
868 /* initialize new leaf */
869 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -0500870 if (unlikely(newblock == 0)) {
871 EXT4_ERROR_INODE(inode, "newblock == 0!");
872 err = -EIO;
873 goto cleanup;
874 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700875 bh = sb_getblk(inode->i_sb, newblock);
876 if (!bh) {
877 err = -EIO;
878 goto cleanup;
879 }
880 lock_buffer(bh);
881
Avantika Mathur7e028972006-12-06 20:41:33 -0800882 err = ext4_journal_get_create_access(handle, bh);
883 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700884 goto cleanup;
885
886 neh = ext_block_hdr(bh);
887 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400888 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700889 neh->eh_magic = EXT4_EXT_MAGIC;
890 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700891
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700892 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -0500893 if (unlikely(path[depth].p_hdr->eh_entries !=
894 path[depth].p_hdr->eh_max)) {
895 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
896 path[depth].p_hdr->eh_entries,
897 path[depth].p_hdr->eh_max);
898 err = -EIO;
899 goto cleanup;
900 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700901 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400902 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
903 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -0700904 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400905 struct ext4_extent *ex;
906 ex = EXT_FIRST_EXTENT(neh);
907 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400908 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700909 }
910
911 set_buffer_uptodate(bh);
912 unlock_buffer(bh);
913
Frank Mayhar03901312009-01-07 00:06:22 -0500914 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800915 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700916 goto cleanup;
917 brelse(bh);
918 bh = NULL;
919
920 /* correct old leaf */
921 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800922 err = ext4_ext_get_access(handle, inode, path + depth);
923 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700924 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400925 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800926 err = ext4_ext_dirty(handle, inode, path + depth);
927 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700928 goto cleanup;
929
930 }
931
932 /* create intermediate indexes */
933 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -0500934 if (unlikely(k < 0)) {
935 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
936 err = -EIO;
937 goto cleanup;
938 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700939 if (k)
940 ext_debug("create %d intermediate indices\n", k);
941 /* insert new index into current index block */
942 /* current depth stored in i var */
943 i = depth - 1;
944 while (k--) {
945 oldblock = newblock;
946 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -0500947 bh = sb_getblk(inode->i_sb, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700948 if (!bh) {
949 err = -EIO;
950 goto cleanup;
951 }
952 lock_buffer(bh);
953
Avantika Mathur7e028972006-12-06 20:41:33 -0800954 err = ext4_journal_get_create_access(handle, bh);
955 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700956 goto cleanup;
957
958 neh = ext_block_hdr(bh);
959 neh->eh_entries = cpu_to_le16(1);
960 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400961 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700962 neh->eh_depth = cpu_to_le16(depth - i);
963 fidx = EXT_FIRST_INDEX(neh);
964 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700965 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700966
Eric Sandeenbba90742008-01-28 23:58:27 -0500967 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
968 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700969
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400970 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -0500971 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
972 EXT_LAST_INDEX(path[i].p_hdr))) {
973 EXT4_ERROR_INODE(inode,
974 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
975 le32_to_cpu(path[i].p_ext->ee_block));
976 err = -EIO;
977 goto cleanup;
978 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400979 /* start copy indexes */
980 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
981 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
982 EXT_MAX_INDEX(path[i].p_hdr));
983 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -0700984 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400985 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -0700986 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400987 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700988 }
989 set_buffer_uptodate(bh);
990 unlock_buffer(bh);
991
Frank Mayhar03901312009-01-07 00:06:22 -0500992 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800993 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700994 goto cleanup;
995 brelse(bh);
996 bh = NULL;
997
998 /* correct old index */
999 if (m) {
1000 err = ext4_ext_get_access(handle, inode, path + i);
1001 if (err)
1002 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001003 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001004 err = ext4_ext_dirty(handle, inode, path + i);
1005 if (err)
1006 goto cleanup;
1007 }
1008
1009 i--;
1010 }
1011
1012 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001013 err = ext4_ext_insert_index(handle, inode, path + at,
1014 le32_to_cpu(border), newblock);
1015
1016cleanup:
1017 if (bh) {
1018 if (buffer_locked(bh))
1019 unlock_buffer(bh);
1020 brelse(bh);
1021 }
1022
1023 if (err) {
1024 /* free all allocated blocks in error case */
1025 for (i = 0; i < depth; i++) {
1026 if (!ablocks[i])
1027 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001028 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001029 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001030 }
1031 }
1032 kfree(ablocks);
1033
1034 return err;
1035}
1036
1037/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001038 * ext4_ext_grow_indepth:
1039 * implements tree growing procedure:
1040 * - allocates new block
1041 * - moves top-level data (index block or leaf) into the new block
1042 * - initializes new top-level, creating index that points to the
1043 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001044 */
1045static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001046 unsigned int flags,
Allison Henderson55f020d2011-05-25 07:41:26 -04001047 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001048{
Alex Tomasa86c6182006-10-11 01:21:03 -07001049 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001050 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001051 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001052 int err = 0;
1053
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001054 newblock = ext4_ext_new_meta_block(handle, inode, NULL,
Allison Henderson55f020d2011-05-25 07:41:26 -04001055 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001056 if (newblock == 0)
1057 return err;
1058
1059 bh = sb_getblk(inode->i_sb, newblock);
1060 if (!bh) {
1061 err = -EIO;
1062 ext4_std_error(inode->i_sb, err);
1063 return err;
1064 }
1065 lock_buffer(bh);
1066
Avantika Mathur7e028972006-12-06 20:41:33 -08001067 err = ext4_journal_get_create_access(handle, bh);
1068 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001069 unlock_buffer(bh);
1070 goto out;
1071 }
1072
1073 /* move top-level index/leaf into new block */
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001074 memmove(bh->b_data, EXT4_I(inode)->i_data,
1075 sizeof(EXT4_I(inode)->i_data));
Alex Tomasa86c6182006-10-11 01:21:03 -07001076
1077 /* set size of new block */
1078 neh = ext_block_hdr(bh);
1079 /* old root could have indexes or leaves
1080 * so calculate e_max right way */
1081 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001082 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001083 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001084 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001085 neh->eh_magic = EXT4_EXT_MAGIC;
1086 set_buffer_uptodate(bh);
1087 unlock_buffer(bh);
1088
Frank Mayhar03901312009-01-07 00:06:22 -05001089 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001090 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001091 goto out;
1092
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001093 /* Update top-level index: num,max,pointer */
Alex Tomasa86c6182006-10-11 01:21:03 -07001094 neh = ext_inode_hdr(inode);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001095 neh->eh_entries = cpu_to_le16(1);
1096 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1097 if (neh->eh_depth == 0) {
1098 /* Root extent block becomes index block */
1099 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1100 EXT_FIRST_INDEX(neh)->ei_block =
1101 EXT_FIRST_EXTENT(neh)->ee_block;
1102 }
Mingming Cao2ae02102006-10-11 01:21:11 -07001103 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001104 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001105 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001106 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001107
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001108 neh->eh_depth = cpu_to_le16(neh->eh_depth + 1);
1109 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07001110out:
1111 brelse(bh);
1112
1113 return err;
1114}
1115
1116/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001117 * ext4_ext_create_new_leaf:
1118 * finds empty index and adds new leaf.
1119 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001120 */
1121static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001122 unsigned int flags,
1123 struct ext4_ext_path *path,
1124 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001125{
1126 struct ext4_ext_path *curp;
1127 int depth, i, err = 0;
1128
1129repeat:
1130 i = depth = ext_depth(inode);
1131
1132 /* walk up to the tree and look for free index entry */
1133 curp = path + depth;
1134 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1135 i--;
1136 curp--;
1137 }
1138
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001139 /* we use already allocated block for index block,
1140 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001141 if (EXT_HAS_FREE_INDEX(curp)) {
1142 /* if we found index with free entry, then use that
1143 * entry: create all needed subtree and add new leaf */
Allison Henderson55f020d2011-05-25 07:41:26 -04001144 err = ext4_ext_split(handle, inode, flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001145 if (err)
1146 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001147
1148 /* refill path */
1149 ext4_ext_drop_refs(path);
1150 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001151 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1152 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001153 if (IS_ERR(path))
1154 err = PTR_ERR(path);
1155 } else {
1156 /* tree is full, time to grow in depth */
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001157 err = ext4_ext_grow_indepth(handle, inode, flags, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001158 if (err)
1159 goto out;
1160
1161 /* refill path */
1162 ext4_ext_drop_refs(path);
1163 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001164 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1165 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001166 if (IS_ERR(path)) {
1167 err = PTR_ERR(path);
1168 goto out;
1169 }
1170
1171 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001172 * only first (depth 0 -> 1) produces free space;
1173 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001174 */
1175 depth = ext_depth(inode);
1176 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001177 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001178 goto repeat;
1179 }
1180 }
1181
1182out:
1183 return err;
1184}
1185
1186/*
Alex Tomas1988b512008-01-28 23:58:27 -05001187 * search the closest allocated block to the left for *logical
1188 * and returns it at @logical + it's physical address at @phys
1189 * if *logical is the smallest allocated block, the function
1190 * returns 0 at @phys
1191 * return value contains 0 (success) or error code
1192 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001193static int ext4_ext_search_left(struct inode *inode,
1194 struct ext4_ext_path *path,
1195 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001196{
1197 struct ext4_extent_idx *ix;
1198 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001199 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001200
Frank Mayhar273df552010-03-02 11:46:09 -05001201 if (unlikely(path == NULL)) {
1202 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1203 return -EIO;
1204 }
Alex Tomas1988b512008-01-28 23:58:27 -05001205 depth = path->p_depth;
1206 *phys = 0;
1207
1208 if (depth == 0 && path->p_ext == NULL)
1209 return 0;
1210
1211 /* usually extent in the path covers blocks smaller
1212 * then *logical, but it can be that extent is the
1213 * first one in the file */
1214
1215 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001216 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001217 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001218 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1219 EXT4_ERROR_INODE(inode,
1220 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1221 *logical, le32_to_cpu(ex->ee_block));
1222 return -EIO;
1223 }
Alex Tomas1988b512008-01-28 23:58:27 -05001224 while (--depth >= 0) {
1225 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001226 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1227 EXT4_ERROR_INODE(inode,
1228 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
Tao Ma6ee3b212011-10-08 16:08:34 -04001229 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001230 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
Tao Ma6ee3b212011-10-08 16:08:34 -04001231 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001232 depth);
1233 return -EIO;
1234 }
Alex Tomas1988b512008-01-28 23:58:27 -05001235 }
1236 return 0;
1237 }
1238
Frank Mayhar273df552010-03-02 11:46:09 -05001239 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1240 EXT4_ERROR_INODE(inode,
1241 "logical %d < ee_block %d + ee_len %d!",
1242 *logical, le32_to_cpu(ex->ee_block), ee_len);
1243 return -EIO;
1244 }
Alex Tomas1988b512008-01-28 23:58:27 -05001245
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001246 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001247 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001248 return 0;
1249}
1250
1251/*
1252 * search the closest allocated block to the right for *logical
1253 * and returns it at @logical + it's physical address at @phys
Tao Madf3ab172011-10-08 15:53:49 -04001254 * if *logical is the largest allocated block, the function
Alex Tomas1988b512008-01-28 23:58:27 -05001255 * returns 0 at @phys
1256 * return value contains 0 (success) or error code
1257 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001258static int ext4_ext_search_right(struct inode *inode,
1259 struct ext4_ext_path *path,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001260 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1261 struct ext4_extent **ret_ex)
Alex Tomas1988b512008-01-28 23:58:27 -05001262{
1263 struct buffer_head *bh = NULL;
1264 struct ext4_extent_header *eh;
1265 struct ext4_extent_idx *ix;
1266 struct ext4_extent *ex;
1267 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001268 int depth; /* Note, NOT eh_depth; depth from top of tree */
1269 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001270
Frank Mayhar273df552010-03-02 11:46:09 -05001271 if (unlikely(path == NULL)) {
1272 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1273 return -EIO;
1274 }
Alex Tomas1988b512008-01-28 23:58:27 -05001275 depth = path->p_depth;
1276 *phys = 0;
1277
1278 if (depth == 0 && path->p_ext == NULL)
1279 return 0;
1280
1281 /* usually extent in the path covers blocks smaller
1282 * then *logical, but it can be that extent is the
1283 * first one in the file */
1284
1285 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001286 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001287 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001288 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1289 EXT4_ERROR_INODE(inode,
1290 "first_extent(path[%d].p_hdr) != ex",
1291 depth);
1292 return -EIO;
1293 }
Alex Tomas1988b512008-01-28 23:58:27 -05001294 while (--depth >= 0) {
1295 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001296 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1297 EXT4_ERROR_INODE(inode,
1298 "ix != EXT_FIRST_INDEX *logical %d!",
1299 *logical);
1300 return -EIO;
1301 }
Alex Tomas1988b512008-01-28 23:58:27 -05001302 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001303 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001304 }
1305
Frank Mayhar273df552010-03-02 11:46:09 -05001306 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1307 EXT4_ERROR_INODE(inode,
1308 "logical %d < ee_block %d + ee_len %d!",
1309 *logical, le32_to_cpu(ex->ee_block), ee_len);
1310 return -EIO;
1311 }
Alex Tomas1988b512008-01-28 23:58:27 -05001312
1313 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1314 /* next allocated block in this leaf */
1315 ex++;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001316 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001317 }
1318
1319 /* go up and search for index to the right */
1320 while (--depth >= 0) {
1321 ix = path[depth].p_idx;
1322 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001323 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001324 }
1325
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001326 /* we've gone up to the root and found no index to the right */
1327 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001328
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001329got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001330 /* we've found index to the right, let's
1331 * follow it and find the closest allocated
1332 * block to the right */
1333 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001334 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001335 while (++depth < path->p_depth) {
1336 bh = sb_bread(inode->i_sb, block);
1337 if (bh == NULL)
1338 return -EIO;
1339 eh = ext_block_hdr(bh);
Eric Sandeen395a87b2009-03-10 18:18:47 -04001340 /* subtract from p_depth to get proper eh_depth */
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001341 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001342 put_bh(bh);
1343 return -EIO;
1344 }
1345 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001346 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001347 put_bh(bh);
1348 }
1349
1350 bh = sb_bread(inode->i_sb, block);
1351 if (bh == NULL)
1352 return -EIO;
1353 eh = ext_block_hdr(bh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001354 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001355 put_bh(bh);
1356 return -EIO;
1357 }
1358 ex = EXT_FIRST_EXTENT(eh);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001359found_extent:
Alex Tomas1988b512008-01-28 23:58:27 -05001360 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001361 *phys = ext4_ext_pblock(ex);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001362 *ret_ex = ex;
1363 if (bh)
1364 put_bh(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001365 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001366}
1367
1368/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001369 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001370 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001371 * NOTE: it considers block number from index entry as
1372 * allocated block. Thus, index entries have to be consistent
1373 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001374 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001375static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001376ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1377{
1378 int depth;
1379
1380 BUG_ON(path == NULL);
1381 depth = path->p_depth;
1382
1383 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001384 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001385
1386 while (depth >= 0) {
1387 if (depth == path->p_depth) {
1388 /* leaf */
Curt Wohlgemuth6f8ff532011-10-26 04:38:59 -04001389 if (path[depth].p_ext &&
1390 path[depth].p_ext !=
Alex Tomasa86c6182006-10-11 01:21:03 -07001391 EXT_LAST_EXTENT(path[depth].p_hdr))
1392 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1393 } else {
1394 /* index */
1395 if (path[depth].p_idx !=
1396 EXT_LAST_INDEX(path[depth].p_hdr))
1397 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1398 }
1399 depth--;
1400 }
1401
Lukas Czernerf17722f2011-06-06 00:05:17 -04001402 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001403}
1404
1405/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001406 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001407 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001408 */
Robin Dong57187892011-07-23 21:49:07 -04001409static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001410{
1411 int depth;
1412
1413 BUG_ON(path == NULL);
1414 depth = path->p_depth;
1415
1416 /* zero-tree has no leaf blocks at all */
1417 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001418 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001419
1420 /* go to index block */
1421 depth--;
1422
1423 while (depth >= 0) {
1424 if (path[depth].p_idx !=
1425 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001426 return (ext4_lblk_t)
1427 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001428 depth--;
1429 }
1430
Lukas Czernerf17722f2011-06-06 00:05:17 -04001431 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001432}
1433
1434/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001435 * ext4_ext_correct_indexes:
1436 * if leaf gets modified and modified extent is first in the leaf,
1437 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001438 * TODO: do we need to correct tree in all cases?
1439 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001440static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001441 struct ext4_ext_path *path)
1442{
1443 struct ext4_extent_header *eh;
1444 int depth = ext_depth(inode);
1445 struct ext4_extent *ex;
1446 __le32 border;
1447 int k, err = 0;
1448
1449 eh = path[depth].p_hdr;
1450 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001451
1452 if (unlikely(ex == NULL || eh == NULL)) {
1453 EXT4_ERROR_INODE(inode,
1454 "ex %p == NULL or eh %p == NULL", ex, eh);
1455 return -EIO;
1456 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001457
1458 if (depth == 0) {
1459 /* there is no tree at all */
1460 return 0;
1461 }
1462
1463 if (ex != EXT_FIRST_EXTENT(eh)) {
1464 /* we correct tree if first leaf got modified only */
1465 return 0;
1466 }
1467
1468 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001469 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001470 */
1471 k = depth - 1;
1472 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001473 err = ext4_ext_get_access(handle, inode, path + k);
1474 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001475 return err;
1476 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001477 err = ext4_ext_dirty(handle, inode, path + k);
1478 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001479 return err;
1480
1481 while (k--) {
1482 /* change all left-side indexes */
1483 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1484 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001485 err = ext4_ext_get_access(handle, inode, path + k);
1486 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001487 break;
1488 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001489 err = ext4_ext_dirty(handle, inode, path + k);
1490 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001491 break;
1492 }
1493
1494 return err;
1495}
1496
Akira Fujita748de672009-06-17 19:24:03 -04001497int
Alex Tomasa86c6182006-10-11 01:21:03 -07001498ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1499 struct ext4_extent *ex2)
1500{
Amit Arora749269f2007-07-18 09:02:56 -04001501 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001502
1503 /*
1504 * Make sure that either both extents are uninitialized, or
1505 * both are _not_.
1506 */
1507 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1508 return 0;
1509
Amit Arora749269f2007-07-18 09:02:56 -04001510 if (ext4_ext_is_uninitialized(ex1))
1511 max_len = EXT_UNINIT_MAX_LEN;
1512 else
1513 max_len = EXT_INIT_MAX_LEN;
1514
Amit Aroraa2df2a62007-07-17 21:42:41 -04001515 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1516 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1517
1518 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001519 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001520 return 0;
1521
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001522 /*
1523 * To allow future support for preallocated extents to be added
1524 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001525 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001526 */
Amit Arora749269f2007-07-18 09:02:56 -04001527 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001528 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001529#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001530 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001531 return 0;
1532#endif
1533
Theodore Ts'obf89d162010-10-27 21:30:14 -04001534 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001535 return 1;
1536 return 0;
1537}
1538
1539/*
Amit Arora56055d32007-07-17 21:42:38 -04001540 * This function tries to merge the "ex" extent to the next extent in the tree.
1541 * It always tries to merge towards right. If you want to merge towards
1542 * left, pass "ex - 1" as argument instead of "ex".
1543 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1544 * 1 if they got merged.
1545 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001546static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001547 struct ext4_ext_path *path,
1548 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001549{
1550 struct ext4_extent_header *eh;
1551 unsigned int depth, len;
1552 int merge_done = 0;
1553 int uninitialized = 0;
1554
1555 depth = ext_depth(inode);
1556 BUG_ON(path[depth].p_hdr == NULL);
1557 eh = path[depth].p_hdr;
1558
1559 while (ex < EXT_LAST_EXTENT(eh)) {
1560 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1561 break;
1562 /* merge with next extent! */
1563 if (ext4_ext_is_uninitialized(ex))
1564 uninitialized = 1;
1565 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1566 + ext4_ext_get_actual_len(ex + 1));
1567 if (uninitialized)
1568 ext4_ext_mark_uninitialized(ex);
1569
1570 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1571 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1572 * sizeof(struct ext4_extent);
1573 memmove(ex + 1, ex + 2, len);
1574 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001575 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001576 merge_done = 1;
1577 WARN_ON(eh->eh_entries == 0);
1578 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001579 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001580 }
1581
1582 return merge_done;
1583}
1584
1585/*
Yongqiang Yang197217a2011-05-03 11:45:29 -04001586 * This function tries to merge the @ex extent to neighbours in the tree.
1587 * return 1 if merge left else 0.
1588 */
1589static int ext4_ext_try_to_merge(struct inode *inode,
1590 struct ext4_ext_path *path,
1591 struct ext4_extent *ex) {
1592 struct ext4_extent_header *eh;
1593 unsigned int depth;
1594 int merge_done = 0;
1595 int ret = 0;
1596
1597 depth = ext_depth(inode);
1598 BUG_ON(path[depth].p_hdr == NULL);
1599 eh = path[depth].p_hdr;
1600
1601 if (ex > EXT_FIRST_EXTENT(eh))
1602 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1603
1604 if (!merge_done)
1605 ret = ext4_ext_try_to_merge_right(inode, path, ex);
1606
1607 return ret;
1608}
1609
1610/*
Amit Arora25d14f92007-05-24 13:04:13 -04001611 * check if a portion of the "newext" extent overlaps with an
1612 * existing extent.
1613 *
1614 * If there is an overlap discovered, it updates the length of the newext
1615 * such that there will be no overlap, and then returns 1.
1616 * If there is no overlap found, it returns 0.
1617 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001618static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1619 struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001620 struct ext4_extent *newext,
1621 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001622{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001623 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001624 unsigned int depth, len1;
1625 unsigned int ret = 0;
1626
1627 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001628 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001629 depth = ext_depth(inode);
1630 if (!path[depth].p_ext)
1631 goto out;
1632 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001633 b2 &= ~(sbi->s_cluster_ratio - 1);
Amit Arora25d14f92007-05-24 13:04:13 -04001634
1635 /*
1636 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001637 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001638 */
1639 if (b2 < b1) {
1640 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001641 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001642 goto out;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001643 b2 &= ~(sbi->s_cluster_ratio - 1);
Amit Arora25d14f92007-05-24 13:04:13 -04001644 }
1645
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001646 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001647 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001648 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001649 newext->ee_len = cpu_to_le16(len1);
1650 ret = 1;
1651 }
1652
1653 /* check for overlap */
1654 if (b1 + len1 > b2) {
1655 newext->ee_len = cpu_to_le16(b2 - b1);
1656 ret = 1;
1657 }
1658out:
1659 return ret;
1660}
1661
1662/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001663 * ext4_ext_insert_extent:
1664 * tries to merge requsted extent into the existing extent or
1665 * inserts requested extent as new one into the tree,
1666 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001667 */
1668int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1669 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04001670 struct ext4_extent *newext, int flag)
Alex Tomasa86c6182006-10-11 01:21:03 -07001671{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001672 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001673 struct ext4_extent *ex, *fex;
1674 struct ext4_extent *nearex; /* nearest extent */
1675 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001676 int depth, len, err;
1677 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001678 unsigned uninitialized = 0;
Allison Henderson55f020d2011-05-25 07:41:26 -04001679 int flags = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001680
Frank Mayhar273df552010-03-02 11:46:09 -05001681 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1682 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1683 return -EIO;
1684 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001685 depth = ext_depth(inode);
1686 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001687 if (unlikely(path[depth].p_hdr == NULL)) {
1688 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1689 return -EIO;
1690 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001691
1692 /* try to insert block into found extent and return */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001693 if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
Mingming Cao00314622009-09-28 15:49:08 -04001694 && ext4_can_extents_be_merged(inode, ex, newext)) {
Mingming553f9002009-09-18 13:34:55 -04001695 ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04001696 ext4_ext_is_uninitialized(newext),
1697 ext4_ext_get_actual_len(newext),
1698 le32_to_cpu(ex->ee_block),
1699 ext4_ext_is_uninitialized(ex),
1700 ext4_ext_get_actual_len(ex),
1701 ext4_ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001702 err = ext4_ext_get_access(handle, inode, path + depth);
1703 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001704 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001705
1706 /*
1707 * ext4_can_extents_be_merged should have checked that either
1708 * both extents are uninitialized, or both aren't. Thus we
1709 * need to check only one of them here.
1710 */
1711 if (ext4_ext_is_uninitialized(ex))
1712 uninitialized = 1;
1713 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1714 + ext4_ext_get_actual_len(newext));
1715 if (uninitialized)
1716 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001717 eh = path[depth].p_hdr;
1718 nearex = ex;
1719 goto merge;
1720 }
1721
Alex Tomasa86c6182006-10-11 01:21:03 -07001722 depth = ext_depth(inode);
1723 eh = path[depth].p_hdr;
1724 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1725 goto has_space;
1726
1727 /* probably next leaf has space for us? */
1728 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04001729 next = EXT_MAX_BLOCKS;
1730 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
Robin Dong57187892011-07-23 21:49:07 -04001731 next = ext4_ext_next_leaf_block(path);
Robin Dong598dbdf2011-07-11 18:24:01 -04001732 if (next != EXT_MAX_BLOCKS) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001733 ext_debug("next leaf block - %d\n", next);
1734 BUG_ON(npath != NULL);
1735 npath = ext4_ext_find_extent(inode, next, NULL);
1736 if (IS_ERR(npath))
1737 return PTR_ERR(npath);
1738 BUG_ON(npath->p_depth != path->p_depth);
1739 eh = npath[depth].p_hdr;
1740 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001741 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001742 le16_to_cpu(eh->eh_entries));
1743 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04001744 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07001745 }
1746 ext_debug("next leaf has no free space(%d,%d)\n",
1747 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1748 }
1749
1750 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001751 * There is no free space in the found leaf.
1752 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001753 */
Allison Henderson55f020d2011-05-25 07:41:26 -04001754 if (flag & EXT4_GET_BLOCKS_PUNCH_OUT_EXT)
1755 flags = EXT4_MB_USE_ROOT_BLOCKS;
1756 err = ext4_ext_create_new_leaf(handle, inode, flags, path, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001757 if (err)
1758 goto cleanup;
1759 depth = ext_depth(inode);
1760 eh = path[depth].p_hdr;
1761
1762has_space:
1763 nearex = path[depth].p_ext;
1764
Avantika Mathur7e028972006-12-06 20:41:33 -08001765 err = ext4_ext_get_access(handle, inode, path + depth);
1766 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001767 goto cleanup;
1768
1769 if (!nearex) {
1770 /* there is no extent in this leaf, create first one */
Mingming553f9002009-09-18 13:34:55 -04001771 ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001772 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001773 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001774 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001775 ext4_ext_get_actual_len(newext));
Eric Gouriou80e675f2011-10-27 11:52:18 -04001776 nearex = EXT_FIRST_EXTENT(eh);
1777 } else {
1778 if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001779 > le32_to_cpu(nearex->ee_block)) {
Eric Gouriou80e675f2011-10-27 11:52:18 -04001780 /* Insert after */
1781 ext_debug("insert %d:%llu:[%d]%d %s before: "
1782 "nearest 0x%p\n"
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001783 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001784 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001785 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001786 ext4_ext_get_actual_len(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04001787 nearex);
1788 nearex++;
1789 } else {
1790 /* Insert before */
1791 BUG_ON(newext->ee_block == nearex->ee_block);
1792 ext_debug("insert %d:%llu:[%d]%d %s after: "
1793 "nearest 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 nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001799 }
Eric Gouriou80e675f2011-10-27 11:52:18 -04001800 len = EXT_LAST_EXTENT(eh) - nearex + 1;
1801 if (len > 0) {
1802 ext_debug("insert %d:%llu:[%d]%d: "
1803 "move %d extents from 0x%p to 0x%p\n",
1804 le32_to_cpu(newext->ee_block),
1805 ext4_ext_pblock(newext),
1806 ext4_ext_is_uninitialized(newext),
1807 ext4_ext_get_actual_len(newext),
1808 len, nearex, nearex + 1);
1809 memmove(nearex + 1, nearex,
1810 len * sizeof(struct ext4_extent));
1811 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001812 }
1813
Marcin Slusarze8546d02008-04-17 10:38:59 -04001814 le16_add_cpu(&eh->eh_entries, 1);
Eric Gouriou80e675f2011-10-27 11:52:18 -04001815 path[depth].p_ext = nearex;
Alex Tomasa86c6182006-10-11 01:21:03 -07001816 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001817 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001818 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001819
1820merge:
1821 /* try to merge extents to the right */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001822 if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
Mingming Cao00314622009-09-28 15:49:08 -04001823 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001824
1825 /* try to merge extents to the left */
1826
1827 /* time to correct all indexes above */
1828 err = ext4_ext_correct_indexes(handle, inode, path);
1829 if (err)
1830 goto cleanup;
1831
1832 err = ext4_ext_dirty(handle, inode, path + depth);
1833
1834cleanup:
1835 if (npath) {
1836 ext4_ext_drop_refs(npath);
1837 kfree(npath);
1838 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001839 ext4_ext_invalidate_cache(inode);
1840 return err;
1841}
1842
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001843static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1844 ext4_lblk_t num, ext_prepare_callback func,
1845 void *cbdata)
Eric Sandeen6873fa02008-10-07 00:46:36 -04001846{
1847 struct ext4_ext_path *path = NULL;
1848 struct ext4_ext_cache cbex;
1849 struct ext4_extent *ex;
1850 ext4_lblk_t next, start = 0, end = 0;
1851 ext4_lblk_t last = block + num;
1852 int depth, exists, err = 0;
1853
1854 BUG_ON(func == NULL);
1855 BUG_ON(inode == NULL);
1856
Lukas Czernerf17722f2011-06-06 00:05:17 -04001857 while (block < last && block != EXT_MAX_BLOCKS) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04001858 num = last - block;
1859 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001860 down_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001861 path = ext4_ext_find_extent(inode, block, path);
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001862 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001863 if (IS_ERR(path)) {
1864 err = PTR_ERR(path);
1865 path = NULL;
1866 break;
1867 }
1868
1869 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05001870 if (unlikely(path[depth].p_hdr == NULL)) {
1871 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1872 err = -EIO;
1873 break;
1874 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04001875 ex = path[depth].p_ext;
1876 next = ext4_ext_next_allocated_block(path);
1877
1878 exists = 0;
1879 if (!ex) {
1880 /* there is no extent yet, so try to allocate
1881 * all requested space */
1882 start = block;
1883 end = block + num;
1884 } else if (le32_to_cpu(ex->ee_block) > block) {
1885 /* need to allocate space before found extent */
1886 start = block;
1887 end = le32_to_cpu(ex->ee_block);
1888 if (block + num < end)
1889 end = block + num;
1890 } else if (block >= le32_to_cpu(ex->ee_block)
1891 + ext4_ext_get_actual_len(ex)) {
1892 /* need to allocate space after found extent */
1893 start = block;
1894 end = block + num;
1895 if (end >= next)
1896 end = next;
1897 } else if (block >= le32_to_cpu(ex->ee_block)) {
1898 /*
1899 * some part of requested space is covered
1900 * by found extent
1901 */
1902 start = block;
1903 end = le32_to_cpu(ex->ee_block)
1904 + ext4_ext_get_actual_len(ex);
1905 if (block + num < end)
1906 end = block + num;
1907 exists = 1;
1908 } else {
1909 BUG();
1910 }
1911 BUG_ON(end <= start);
1912
1913 if (!exists) {
1914 cbex.ec_block = start;
1915 cbex.ec_len = end - start;
1916 cbex.ec_start = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04001917 } else {
1918 cbex.ec_block = le32_to_cpu(ex->ee_block);
1919 cbex.ec_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001920 cbex.ec_start = ext4_ext_pblock(ex);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001921 }
1922
Frank Mayhar273df552010-03-02 11:46:09 -05001923 if (unlikely(cbex.ec_len == 0)) {
1924 EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
1925 err = -EIO;
1926 break;
1927 }
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04001928 err = func(inode, next, &cbex, ex, cbdata);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001929 ext4_ext_drop_refs(path);
1930
1931 if (err < 0)
1932 break;
1933
1934 if (err == EXT_REPEAT)
1935 continue;
1936 else if (err == EXT_BREAK) {
1937 err = 0;
1938 break;
1939 }
1940
1941 if (ext_depth(inode) != depth) {
1942 /* depth was changed. we have to realloc path */
1943 kfree(path);
1944 path = NULL;
1945 }
1946
1947 block = cbex.ec_block + cbex.ec_len;
1948 }
1949
1950 if (path) {
1951 ext4_ext_drop_refs(path);
1952 kfree(path);
1953 }
1954
1955 return err;
1956}
1957
Avantika Mathur09b88252006-12-06 20:41:36 -08001958static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001959ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05001960 __u32 len, ext4_fsblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07001961{
1962 struct ext4_ext_cache *cex;
1963 BUG_ON(len == 0);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001964 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Aditya Kalid8990242011-09-09 19:18:51 -04001965 trace_ext4_ext_put_in_cache(inode, block, len, start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001966 cex = &EXT4_I(inode)->i_cached_extent;
Alex Tomasa86c6182006-10-11 01:21:03 -07001967 cex->ec_block = block;
1968 cex->ec_len = len;
1969 cex->ec_start = start;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001970 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001971}
1972
1973/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001974 * ext4_ext_put_gap_in_cache:
1975 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07001976 * and cache this gap
1977 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001978static void
Alex Tomasa86c6182006-10-11 01:21:03 -07001979ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001980 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07001981{
1982 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001983 unsigned long len;
1984 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001985 struct ext4_extent *ex;
1986
1987 ex = path[depth].p_ext;
1988 if (ex == NULL) {
1989 /* there is no extent yet, so gap is [0;-] */
1990 lblock = 0;
Lukas Czernerf17722f2011-06-06 00:05:17 -04001991 len = EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001992 ext_debug("cache gap(whole file):");
1993 } else if (block < le32_to_cpu(ex->ee_block)) {
1994 lblock = block;
1995 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05001996 ext_debug("cache gap(before): %u [%u:%u]",
1997 block,
1998 le32_to_cpu(ex->ee_block),
1999 ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002000 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002001 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002002 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002003 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002004 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002005
2006 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05002007 ext_debug("cache gap(after): [%u:%u] %u",
2008 le32_to_cpu(ex->ee_block),
2009 ext4_ext_get_actual_len(ex),
2010 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002011 BUG_ON(next == lblock);
2012 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07002013 } else {
2014 lblock = len = 0;
2015 BUG();
2016 }
2017
Eric Sandeenbba90742008-01-28 23:58:27 -05002018 ext_debug(" -> %u:%lu\n", lblock, len);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002019 ext4_ext_put_in_cache(inode, lblock, len, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002020}
2021
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002022/*
Robin Dongb7ca1e82011-07-23 21:53:25 -04002023 * ext4_ext_check_cache()
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002024 * Checks to see if the given block is in the cache.
2025 * If it is, the cached extent is stored in the given
2026 * cache extent pointer. If the cached extent is a hole,
2027 * this routine should be used instead of
2028 * ext4_ext_in_cache if the calling function needs to
2029 * know the size of the hole.
2030 *
2031 * @inode: The files inode
2032 * @block: The block to look for in the cache
2033 * @ex: Pointer where the cached extent will be stored
2034 * if it contains block
2035 *
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002036 * Return 0 if cache is invalid; 1 if the cache is valid
2037 */
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002038static int ext4_ext_check_cache(struct inode *inode, ext4_lblk_t block,
2039 struct ext4_ext_cache *ex){
Alex Tomasa86c6182006-10-11 01:21:03 -07002040 struct ext4_ext_cache *cex;
Vivek Haldar77f41352011-05-22 21:24:16 -04002041 struct ext4_sb_info *sbi;
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002042 int ret = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002043
Theodore Ts'o60e66792010-05-17 07:00:00 -04002044 /*
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002045 * We borrow i_block_reservation_lock to protect i_cached_extent
2046 */
2047 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07002048 cex = &EXT4_I(inode)->i_cached_extent;
Vivek Haldar77f41352011-05-22 21:24:16 -04002049 sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002050
2051 /* has cache valid data? */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002052 if (cex->ec_len == 0)
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002053 goto errout;
Alex Tomasa86c6182006-10-11 01:21:03 -07002054
Akinobu Mita731eb1a2010-03-03 23:55:01 -05002055 if (in_range(block, cex->ec_block, cex->ec_len)) {
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002056 memcpy(ex, cex, sizeof(struct ext4_ext_cache));
Eric Sandeenbba90742008-01-28 23:58:27 -05002057 ext_debug("%u cached by %u:%u:%llu\n",
2058 block,
2059 cex->ec_block, cex->ec_len, cex->ec_start);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002060 ret = 1;
Alex Tomasa86c6182006-10-11 01:21:03 -07002061 }
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002062errout:
Vivek Haldar77f41352011-05-22 21:24:16 -04002063 if (!ret)
2064 sbi->extent_cache_misses++;
2065 else
2066 sbi->extent_cache_hits++;
Aditya Kalid8990242011-09-09 19:18:51 -04002067 trace_ext4_ext_in_cache(inode, block, ret);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002068 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
2069 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07002070}
2071
2072/*
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002073 * ext4_ext_in_cache()
2074 * Checks to see if the given block is in the cache.
2075 * If it is, the cached extent is stored in the given
2076 * extent pointer.
2077 *
2078 * @inode: The files inode
2079 * @block: The block to look for in the cache
2080 * @ex: Pointer where the cached extent will be stored
2081 * if it contains block
2082 *
2083 * Return 0 if cache is invalid; 1 if the cache is valid
2084 */
2085static int
2086ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
2087 struct ext4_extent *ex)
2088{
2089 struct ext4_ext_cache cex;
2090 int ret = 0;
2091
2092 if (ext4_ext_check_cache(inode, block, &cex)) {
2093 ex->ee_block = cpu_to_le32(cex.ec_block);
2094 ext4_ext_store_pblock(ex, cex.ec_start);
2095 ex->ee_len = cpu_to_le16(cex.ec_len);
2096 ret = 1;
2097 }
2098
2099 return ret;
2100}
2101
2102
2103/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002104 * ext4_ext_rm_idx:
2105 * removes index from the index block.
Alex Tomasa86c6182006-10-11 01:21:03 -07002106 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002107static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07002108 struct ext4_ext_path *path)
2109{
Alex Tomasa86c6182006-10-11 01:21:03 -07002110 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002111 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002112
2113 /* free index block */
2114 path--;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002115 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002116 if (unlikely(path->p_hdr->eh_entries == 0)) {
2117 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2118 return -EIO;
2119 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002120 err = ext4_ext_get_access(handle, inode, path);
2121 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002122 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002123
2124 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2125 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2126 len *= sizeof(struct ext4_extent_idx);
2127 memmove(path->p_idx, path->p_idx + 1, len);
2128 }
2129
Marcin Slusarze8546d02008-04-17 10:38:59 -04002130 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002131 err = ext4_ext_dirty(handle, inode, path);
2132 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002133 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002134 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Aditya Kalid8990242011-09-09 19:18:51 -04002135 trace_ext4_ext_rm_idx(inode, leaf);
2136
Peter Huewe7dc57612011-02-21 21:01:42 -05002137 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002138 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Alex Tomasa86c6182006-10-11 01:21:03 -07002139 return err;
2140}
2141
2142/*
Mingming Caoee12b632008-08-19 22:16:05 -04002143 * ext4_ext_calc_credits_for_single_extent:
2144 * This routine returns max. credits that needed to insert an extent
2145 * to the extent tree.
2146 * When pass the actual path, the caller should calculate credits
2147 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002148 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002149int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002150 struct ext4_ext_path *path)
2151{
Alex Tomasa86c6182006-10-11 01:21:03 -07002152 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002153 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002154 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002155
Alex Tomasa86c6182006-10-11 01:21:03 -07002156 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002157 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002158 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2159
2160 /*
2161 * There are some space in the leaf tree, no
2162 * need to account for leaf block credit
2163 *
2164 * bitmaps and block group descriptor blocks
Tao Madf3ab172011-10-08 15:53:49 -04002165 * and other metadata blocks still need to be
Mingming Caoee12b632008-08-19 22:16:05 -04002166 * accounted.
2167 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002168 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002169 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002170 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002171 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002172 }
2173
Mingming Cao525f4ed2008-08-19 22:15:58 -04002174 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002175}
Alex Tomasa86c6182006-10-11 01:21:03 -07002176
Mingming Caoee12b632008-08-19 22:16:05 -04002177/*
2178 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2179 *
2180 * if nrblocks are fit in a single extent (chunk flag is 1), then
2181 * in the worse case, each tree level index/leaf need to be changed
2182 * if the tree split due to insert a new extent, then the old tree
2183 * index/leaf need to be updated too
2184 *
2185 * If the nrblocks are discontiguous, they could cause
2186 * the whole tree split more than once, but this is really rare.
2187 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002188int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoee12b632008-08-19 22:16:05 -04002189{
2190 int index;
2191 int depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002192
Mingming Caoee12b632008-08-19 22:16:05 -04002193 if (chunk)
2194 index = depth * 2;
2195 else
2196 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002197
Mingming Caoee12b632008-08-19 22:16:05 -04002198 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002199}
2200
2201static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002202 struct ext4_extent *ex,
2203 ext4_fsblk_t *partial_cluster,
2204 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002205{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002206 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002207 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002208 ext4_fsblk_t pblk;
Theodore Ts'oe6362602009-11-23 07:17:05 -05002209 int flags = EXT4_FREE_BLOCKS_FORGET;
Alex Tomasa86c6182006-10-11 01:21:03 -07002210
Alex Tomasc9de5602008-01-29 00:19:52 -05002211 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
Theodore Ts'oe6362602009-11-23 07:17:05 -05002212 flags |= EXT4_FREE_BLOCKS_METADATA;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002213 /*
2214 * For bigalloc file systems, we never free a partial cluster
2215 * at the beginning of the extent. Instead, we make a note
2216 * that we tried freeing the cluster, and check to see if we
2217 * need to free it on a subsequent call to ext4_remove_blocks,
2218 * or at the end of the ext4_truncate() operation.
2219 */
2220 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
2221
Aditya Kalid8990242011-09-09 19:18:51 -04002222 trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002223 /*
2224 * If we have a partial cluster, and it's different from the
2225 * cluster of the last block, we need to explicitly free the
2226 * partial cluster here.
2227 */
2228 pblk = ext4_ext_pblock(ex) + ee_len - 1;
2229 if (*partial_cluster && (EXT4_B2C(sbi, pblk) != *partial_cluster)) {
2230 ext4_free_blocks(handle, inode, NULL,
2231 EXT4_C2B(sbi, *partial_cluster),
2232 sbi->s_cluster_ratio, flags);
2233 *partial_cluster = 0;
2234 }
2235
Alex Tomasa86c6182006-10-11 01:21:03 -07002236#ifdef EXTENTS_STATS
2237 {
2238 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002239 spin_lock(&sbi->s_ext_stats_lock);
2240 sbi->s_ext_blocks += ee_len;
2241 sbi->s_ext_extents++;
2242 if (ee_len < sbi->s_ext_min)
2243 sbi->s_ext_min = ee_len;
2244 if (ee_len > sbi->s_ext_max)
2245 sbi->s_ext_max = ee_len;
2246 if (ext_depth(inode) > sbi->s_depth_max)
2247 sbi->s_depth_max = ext_depth(inode);
2248 spin_unlock(&sbi->s_ext_stats_lock);
2249 }
2250#endif
2251 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002252 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002253 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002254 ext4_lblk_t num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002255
Amit Aroraa2df2a62007-07-17 21:42:41 -04002256 num = le32_to_cpu(ex->ee_block) + ee_len - from;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002257 pblk = ext4_ext_pblock(ex) + ee_len - num;
2258 ext_debug("free last %u blocks starting %llu\n", num, pblk);
2259 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
2260 /*
2261 * If the block range to be freed didn't start at the
2262 * beginning of a cluster, and we removed the entire
2263 * extent, save the partial cluster here, since we
2264 * might need to delete if we determine that the
2265 * truncate operation has removed all of the blocks in
2266 * the cluster.
2267 */
2268 if (pblk & (sbi->s_cluster_ratio - 1) &&
2269 (ee_len == num))
2270 *partial_cluster = EXT4_B2C(sbi, pblk);
2271 else
2272 *partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002273 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002274 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002275 /* head removal */
2276 ext4_lblk_t num;
2277 ext4_fsblk_t start;
2278
2279 num = to - from;
2280 start = ext4_ext_pblock(ex);
2281
2282 ext_debug("free first %u blocks starting %llu\n", num, start);
H Hartley Sweetenee90d572011-10-18 11:01:51 -04002283 ext4_free_blocks(handle, inode, NULL, start, num, flags);
Allison Hendersond583fb82011-05-25 07:41:43 -04002284
Alex Tomasa86c6182006-10-11 01:21:03 -07002285 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002286 printk(KERN_INFO "strange request: removal(2) "
2287 "%u-%u from %u:%u\n",
2288 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002289 }
2290 return 0;
2291}
2292
Allison Hendersond583fb82011-05-25 07:41:43 -04002293
2294/*
2295 * ext4_ext_rm_leaf() Removes the extents associated with the
2296 * blocks appearing between "start" and "end", and splits the extents
2297 * if "start" and "end" appear in the same extent
2298 *
2299 * @handle: The journal handle
2300 * @inode: The files inode
2301 * @path: The path to the leaf
2302 * @start: The first block to remove
2303 * @end: The last block to remove
2304 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002305static int
2306ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002307 struct ext4_ext_path *path, ext4_fsblk_t *partial_cluster,
2308 ext4_lblk_t start, ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002309{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002310 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002311 int err = 0, correct_index = 0;
2312 int depth = ext_depth(inode), credits;
2313 struct ext4_extent_header *eh;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002314 ext4_lblk_t a, b;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002315 unsigned num;
2316 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002317 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002318 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002319 struct ext4_extent *ex;
2320
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002321 /* the header must be checked already in ext4_ext_remove_space() */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002322 ext_debug("truncate since %u in leaf\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002323 if (!path[depth].p_hdr)
2324 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2325 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002326 if (unlikely(path[depth].p_hdr == NULL)) {
2327 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2328 return -EIO;
2329 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002330 /* find where to start removing */
2331 ex = EXT_LAST_EXTENT(eh);
2332
2333 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002334 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002335
Aditya Kalid8990242011-09-09 19:18:51 -04002336 trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster);
2337
Alex Tomasa86c6182006-10-11 01:21:03 -07002338 while (ex >= EXT_FIRST_EXTENT(eh) &&
2339 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002340
2341 if (ext4_ext_is_uninitialized(ex))
2342 uninitialized = 1;
2343 else
2344 uninitialized = 0;
2345
Mingming553f9002009-09-18 13:34:55 -04002346 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2347 uninitialized, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002348 path[depth].p_ext = ex;
2349
2350 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002351 b = ex_ee_block+ex_ee_len - 1 < end ?
2352 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002353
2354 ext_debug(" border %u:%u\n", a, b);
2355
Allison Hendersond583fb82011-05-25 07:41:43 -04002356 /* If this extent is beyond the end of the hole, skip it */
2357 if (end <= ex_ee_block) {
2358 ex--;
2359 ex_ee_block = le32_to_cpu(ex->ee_block);
2360 ex_ee_len = ext4_ext_get_actual_len(ex);
2361 continue;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002362 } else if (b != ex_ee_block + ex_ee_len - 1) {
2363 EXT4_ERROR_INODE(inode," bad truncate %u:%u\n",
2364 start, end);
2365 err = -EIO;
2366 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002367 } else if (a != ex_ee_block) {
2368 /* remove tail of the extent */
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002369 num = a - ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002370 } else {
2371 /* remove whole extent: excellent! */
Alex Tomasa86c6182006-10-11 01:21:03 -07002372 num = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002373 }
Theodore Ts'o34071da2008-08-01 21:59:19 -04002374 /*
2375 * 3 for leaf, sb, and inode plus 2 (bmap and group
2376 * descriptor) for each block group; assume two block
2377 * groups plus ex_ee_len/blocks_per_block_group for
2378 * the worst case
2379 */
2380 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002381 if (ex == EXT_FIRST_EXTENT(eh)) {
2382 correct_index = 1;
2383 credits += (ext_depth(inode)) + 1;
2384 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002385 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002386
Jan Kara487caee2009-08-17 22:17:20 -04002387 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
Shen Feng9102e4f2008-07-11 19:27:31 -04002388 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002389 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002390
2391 err = ext4_ext_get_access(handle, inode, path + depth);
2392 if (err)
2393 goto out;
2394
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002395 err = ext4_remove_blocks(handle, inode, ex, partial_cluster,
2396 a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002397 if (err)
2398 goto out;
2399
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002400 if (num == 0)
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002401 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002402 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002403
Alex Tomasa86c6182006-10-11 01:21:03 -07002404 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002405 /*
2406 * Do not mark uninitialized if all the blocks in the
2407 * extent have been removed.
2408 */
2409 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002410 ext4_ext_mark_uninitialized(ex);
Allison Hendersond583fb82011-05-25 07:41:43 -04002411 /*
2412 * If the extent was completely released,
2413 * we need to remove it from the leaf
2414 */
2415 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002416 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002417 /*
2418 * For hole punching, we need to scoot all the
2419 * extents up when an extent is removed so that
2420 * we dont have blank extents in the middle
2421 */
2422 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2423 sizeof(struct ext4_extent));
2424
2425 /* Now get rid of the one at the end */
2426 memset(EXT_LAST_EXTENT(eh), 0,
2427 sizeof(struct ext4_extent));
2428 }
2429 le16_add_cpu(&eh->eh_entries, -1);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002430 } else
2431 *partial_cluster = 0;
Allison Hendersond583fb82011-05-25 07:41:43 -04002432
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002433 err = ext4_ext_dirty(handle, inode, path + depth);
2434 if (err)
2435 goto out;
2436
Mingming Cao2ae02102006-10-11 01:21:11 -07002437 ext_debug("new extent: %u:%u:%llu\n", block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002438 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002439 ex--;
2440 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002441 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002442 }
2443
2444 if (correct_index && eh->eh_entries)
2445 err = ext4_ext_correct_indexes(handle, inode, path);
2446
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002447 /*
2448 * If there is still a entry in the leaf node, check to see if
2449 * it references the partial cluster. This is the only place
2450 * where it could; if it doesn't, we can free the cluster.
2451 */
2452 if (*partial_cluster && ex >= EXT_FIRST_EXTENT(eh) &&
2453 (EXT4_B2C(sbi, ext4_ext_pblock(ex) + ex_ee_len - 1) !=
2454 *partial_cluster)) {
2455 int flags = EXT4_FREE_BLOCKS_FORGET;
2456
2457 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2458 flags |= EXT4_FREE_BLOCKS_METADATA;
2459
2460 ext4_free_blocks(handle, inode, NULL,
2461 EXT4_C2B(sbi, *partial_cluster),
2462 sbi->s_cluster_ratio, flags);
2463 *partial_cluster = 0;
2464 }
2465
Alex Tomasa86c6182006-10-11 01:21:03 -07002466 /* if this leaf is free, then we should
2467 * remove it from index block above */
2468 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2469 err = ext4_ext_rm_idx(handle, inode, path + depth);
2470
2471out:
2472 return err;
2473}
2474
2475/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002476 * ext4_ext_more_to_rm:
2477 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002478 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002479static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002480ext4_ext_more_to_rm(struct ext4_ext_path *path)
2481{
2482 BUG_ON(path->p_idx == NULL);
2483
2484 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2485 return 0;
2486
2487 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002488 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002489 * so we have to consider current index for truncation
2490 */
2491 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2492 return 0;
2493 return 1;
2494}
2495
Allison Hendersonc6a03712011-07-17 23:21:03 -04002496static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07002497{
2498 struct super_block *sb = inode->i_sb;
2499 int depth = ext_depth(inode);
2500 struct ext4_ext_path *path;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002501 ext4_fsblk_t partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002502 handle_t *handle;
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002503 int i, err;
Alex Tomasa86c6182006-10-11 01:21:03 -07002504
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002505 ext_debug("truncate since %u\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002506
2507 /* probably first extent we're gonna free will be last in block */
2508 handle = ext4_journal_start(inode, depth + 1);
2509 if (IS_ERR(handle))
2510 return PTR_ERR(handle);
2511
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002512again:
Alex Tomasa86c6182006-10-11 01:21:03 -07002513 ext4_ext_invalidate_cache(inode);
2514
Aditya Kalid8990242011-09-09 19:18:51 -04002515 trace_ext4_ext_remove_space(inode, start, depth);
2516
Alex Tomasa86c6182006-10-11 01:21:03 -07002517 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002518 * We start scanning from right side, freeing all the blocks
2519 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002520 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002521 depth = ext_depth(inode);
Josef Bacik216553c2008-04-29 22:02:02 -04002522 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07002523 if (path == NULL) {
2524 ext4_journal_stop(handle);
2525 return -ENOMEM;
2526 }
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002527 path[0].p_depth = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -07002528 path[0].p_hdr = ext_inode_hdr(inode);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002529 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002530 err = -EIO;
2531 goto out;
2532 }
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002533 i = err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002534
2535 while (i >= 0 && err == 0) {
2536 if (i == depth) {
2537 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04002538 err = ext4_ext_rm_leaf(handle, inode, path,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002539 &partial_cluster, start,
2540 EXT_MAX_BLOCKS - 1);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002541 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002542 brelse(path[i].p_bh);
2543 path[i].p_bh = NULL;
2544 i--;
2545 continue;
2546 }
2547
2548 /* this is index block */
2549 if (!path[i].p_hdr) {
2550 ext_debug("initialize header\n");
2551 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002552 }
2553
Alex Tomasa86c6182006-10-11 01:21:03 -07002554 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002555 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002556 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2557 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2558 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2559 path[i].p_hdr,
2560 le16_to_cpu(path[i].p_hdr->eh_entries));
2561 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002562 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002563 path[i].p_idx--;
2564 }
2565
2566 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2567 i, EXT_FIRST_INDEX(path[i].p_hdr),
2568 path[i].p_idx);
2569 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002570 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002571 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002572 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04002573 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002574 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'obf89d162010-10-27 21:30:14 -04002575 bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002576 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002577 /* should we reset i_size? */
2578 err = -EIO;
2579 break;
2580 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002581 if (WARN_ON(i + 1 > depth)) {
2582 err = -EIO;
2583 break;
2584 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002585 if (ext4_ext_check(inode, ext_block_hdr(bh),
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002586 depth - i - 1)) {
2587 err = -EIO;
2588 break;
2589 }
2590 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002591
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002592 /* save actual number of indexes since this
2593 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002594 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2595 i++;
2596 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002597 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002598 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002599 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002600 * handle must be already prepared by the
2601 * truncatei_leaf() */
2602 err = ext4_ext_rm_idx(handle, inode, path + i);
2603 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002604 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002605 brelse(path[i].p_bh);
2606 path[i].p_bh = NULL;
2607 i--;
2608 ext_debug("return to level %d\n", i);
2609 }
2610 }
2611
Aditya Kalid8990242011-09-09 19:18:51 -04002612 trace_ext4_ext_remove_space_done(inode, start, depth, partial_cluster,
2613 path->p_hdr->eh_entries);
2614
Aditya Kali7b415bf2011-09-09 19:04:51 -04002615 /* If we still have something in the partial cluster and we have removed
2616 * even the first extent, then we should free the blocks in the partial
2617 * cluster as well. */
2618 if (partial_cluster && path->p_hdr->eh_entries == 0) {
2619 int flags = EXT4_FREE_BLOCKS_FORGET;
2620
2621 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2622 flags |= EXT4_FREE_BLOCKS_METADATA;
2623
2624 ext4_free_blocks(handle, inode, NULL,
2625 EXT4_C2B(EXT4_SB(sb), partial_cluster),
2626 EXT4_SB(sb)->s_cluster_ratio, flags);
2627 partial_cluster = 0;
2628 }
2629
Alex Tomasa86c6182006-10-11 01:21:03 -07002630 /* TODO: flexible tree reduction should be here */
2631 if (path->p_hdr->eh_entries == 0) {
2632 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002633 * truncate to zero freed all the tree,
2634 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002635 */
2636 err = ext4_ext_get_access(handle, inode, path);
2637 if (err == 0) {
2638 ext_inode_hdr(inode)->eh_depth = 0;
2639 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04002640 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07002641 err = ext4_ext_dirty(handle, inode, path);
2642 }
2643 }
2644out:
Alex Tomasa86c6182006-10-11 01:21:03 -07002645 ext4_ext_drop_refs(path);
2646 kfree(path);
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002647 if (err == -EAGAIN)
2648 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07002649 ext4_journal_stop(handle);
2650
2651 return err;
2652}
2653
2654/*
2655 * called at mount time
2656 */
2657void ext4_ext_init(struct super_block *sb)
2658{
2659 /*
2660 * possible initialization would be here
2661 */
2662
Theodore Ts'o83982b62009-01-06 14:53:16 -05002663 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04002664#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002665 printk(KERN_INFO "EXT4-fs: file extents enabled");
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002666#ifdef AGGRESSIVE_TEST
2667 printk(", aggressive tests");
Alex Tomasa86c6182006-10-11 01:21:03 -07002668#endif
2669#ifdef CHECK_BINSEARCH
2670 printk(", check binsearch");
2671#endif
2672#ifdef EXTENTS_STATS
2673 printk(", stats");
2674#endif
2675 printk("\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04002676#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07002677#ifdef EXTENTS_STATS
2678 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2679 EXT4_SB(sb)->s_ext_min = 1 << 30;
2680 EXT4_SB(sb)->s_ext_max = 0;
2681#endif
2682 }
2683}
2684
2685/*
2686 * called at umount time
2687 */
2688void ext4_ext_release(struct super_block *sb)
2689{
Theodore Ts'o83982b62009-01-06 14:53:16 -05002690 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
Alex Tomasa86c6182006-10-11 01:21:03 -07002691 return;
2692
2693#ifdef EXTENTS_STATS
2694 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2695 struct ext4_sb_info *sbi = EXT4_SB(sb);
2696 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2697 sbi->s_ext_blocks, sbi->s_ext_extents,
2698 sbi->s_ext_blocks / sbi->s_ext_extents);
2699 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2700 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2701 }
2702#endif
2703}
2704
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002705/* FIXME!! we need to try to merge to left or right after zero-out */
2706static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2707{
Lukas Czerner24075182010-10-27 21:30:06 -04002708 ext4_fsblk_t ee_pblock;
2709 unsigned int ee_len;
Jing Zhangb7203032010-05-12 00:00:00 -04002710 int ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002711
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002712 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04002713 ee_pblock = ext4_ext_pblock(ex);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002714
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04002715 ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
Lukas Czerner24075182010-10-27 21:30:06 -04002716 if (ret > 0)
2717 ret = 0;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002718
Lukas Czerner24075182010-10-27 21:30:06 -04002719 return ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002720}
2721
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002722/*
2723 * used by extent splitting.
2724 */
2725#define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
2726 due to ENOSPC */
2727#define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */
2728#define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */
2729
2730/*
2731 * ext4_split_extent_at() splits an extent at given block.
2732 *
2733 * @handle: the journal handle
2734 * @inode: the file inode
2735 * @path: the path to the extent
2736 * @split: the logical block where the extent is splitted.
2737 * @split_flags: indicates if the extent could be zeroout if split fails, and
2738 * the states(init or uninit) of new extents.
2739 * @flags: flags used to insert new extent to extent tree.
2740 *
2741 *
2742 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
2743 * of which are deterimined by split_flag.
2744 *
2745 * There are two cases:
2746 * a> the extent are splitted into two extent.
2747 * b> split is not needed, and just mark the extent.
2748 *
2749 * return 0 on success.
2750 */
2751static int ext4_split_extent_at(handle_t *handle,
2752 struct inode *inode,
2753 struct ext4_ext_path *path,
2754 ext4_lblk_t split,
2755 int split_flag,
2756 int flags)
2757{
2758 ext4_fsblk_t newblock;
2759 ext4_lblk_t ee_block;
2760 struct ext4_extent *ex, newex, orig_ex;
2761 struct ext4_extent *ex2 = NULL;
2762 unsigned int ee_len, depth;
2763 int err = 0;
2764
2765 ext_debug("ext4_split_extents_at: inode %lu, logical"
2766 "block %llu\n", inode->i_ino, (unsigned long long)split);
2767
2768 ext4_ext_show_leaf(inode, path);
2769
2770 depth = ext_depth(inode);
2771 ex = path[depth].p_ext;
2772 ee_block = le32_to_cpu(ex->ee_block);
2773 ee_len = ext4_ext_get_actual_len(ex);
2774 newblock = split - ee_block + ext4_ext_pblock(ex);
2775
2776 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
2777
2778 err = ext4_ext_get_access(handle, inode, path + depth);
2779 if (err)
2780 goto out;
2781
2782 if (split == ee_block) {
2783 /*
2784 * case b: block @split is the block that the extent begins with
2785 * then we just change the state of the extent, and splitting
2786 * is not needed.
2787 */
2788 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2789 ext4_ext_mark_uninitialized(ex);
2790 else
2791 ext4_ext_mark_initialized(ex);
2792
2793 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
2794 ext4_ext_try_to_merge(inode, path, ex);
2795
2796 err = ext4_ext_dirty(handle, inode, path + depth);
2797 goto out;
2798 }
2799
2800 /* case a */
2801 memcpy(&orig_ex, ex, sizeof(orig_ex));
2802 ex->ee_len = cpu_to_le16(split - ee_block);
2803 if (split_flag & EXT4_EXT_MARK_UNINIT1)
2804 ext4_ext_mark_uninitialized(ex);
2805
2806 /*
2807 * path may lead to new leaf, not to original leaf any more
2808 * after ext4_ext_insert_extent() returns,
2809 */
2810 err = ext4_ext_dirty(handle, inode, path + depth);
2811 if (err)
2812 goto fix_extent_len;
2813
2814 ex2 = &newex;
2815 ex2->ee_block = cpu_to_le32(split);
2816 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
2817 ext4_ext_store_pblock(ex2, newblock);
2818 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2819 ext4_ext_mark_uninitialized(ex2);
2820
2821 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
2822 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2823 err = ext4_ext_zeroout(inode, &orig_ex);
2824 if (err)
2825 goto fix_extent_len;
2826 /* update the extent length and mark as initialized */
2827 ex->ee_len = cpu_to_le32(ee_len);
2828 ext4_ext_try_to_merge(inode, path, ex);
2829 err = ext4_ext_dirty(handle, inode, path + depth);
2830 goto out;
2831 } else if (err)
2832 goto fix_extent_len;
2833
2834out:
2835 ext4_ext_show_leaf(inode, path);
2836 return err;
2837
2838fix_extent_len:
2839 ex->ee_len = orig_ex.ee_len;
2840 ext4_ext_dirty(handle, inode, path + depth);
2841 return err;
2842}
2843
2844/*
2845 * ext4_split_extents() splits an extent and mark extent which is covered
2846 * by @map as split_flags indicates
2847 *
2848 * It may result in splitting the extent into multiple extents (upto three)
2849 * There are three possibilities:
2850 * a> There is no split required
2851 * b> Splits in two extents: Split is happening at either end of the extent
2852 * c> Splits in three extents: Somone is splitting in middle of the extent
2853 *
2854 */
2855static int ext4_split_extent(handle_t *handle,
2856 struct inode *inode,
2857 struct ext4_ext_path *path,
2858 struct ext4_map_blocks *map,
2859 int split_flag,
2860 int flags)
2861{
2862 ext4_lblk_t ee_block;
2863 struct ext4_extent *ex;
2864 unsigned int ee_len, depth;
2865 int err = 0;
2866 int uninitialized;
2867 int split_flag1, flags1;
2868
2869 depth = ext_depth(inode);
2870 ex = path[depth].p_ext;
2871 ee_block = le32_to_cpu(ex->ee_block);
2872 ee_len = ext4_ext_get_actual_len(ex);
2873 uninitialized = ext4_ext_is_uninitialized(ex);
2874
2875 if (map->m_lblk + map->m_len < ee_block + ee_len) {
2876 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2877 EXT4_EXT_MAY_ZEROOUT : 0;
2878 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
2879 if (uninitialized)
2880 split_flag1 |= EXT4_EXT_MARK_UNINIT1 |
2881 EXT4_EXT_MARK_UNINIT2;
2882 err = ext4_split_extent_at(handle, inode, path,
2883 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04002884 if (err)
2885 goto out;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002886 }
2887
2888 ext4_ext_drop_refs(path);
2889 path = ext4_ext_find_extent(inode, map->m_lblk, path);
2890 if (IS_ERR(path))
2891 return PTR_ERR(path);
2892
2893 if (map->m_lblk >= ee_block) {
2894 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2895 EXT4_EXT_MAY_ZEROOUT : 0;
2896 if (uninitialized)
2897 split_flag1 |= EXT4_EXT_MARK_UNINIT1;
2898 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2899 split_flag1 |= EXT4_EXT_MARK_UNINIT2;
2900 err = ext4_split_extent_at(handle, inode, path,
2901 map->m_lblk, split_flag1, flags);
2902 if (err)
2903 goto out;
2904 }
2905
2906 ext4_ext_show_leaf(inode, path);
2907out:
2908 return err ? err : map->m_len;
2909}
2910
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002911#define EXT4_EXT_ZERO_LEN 7
Amit Arora56055d32007-07-17 21:42:38 -04002912/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002913 * This function is called by ext4_ext_map_blocks() if someone tries to write
Amit Arora56055d32007-07-17 21:42:38 -04002914 * to an uninitialized extent. It may result in splitting the uninitialized
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002915 * extent into multiple extents (up to three - one initialized and two
Amit Arora56055d32007-07-17 21:42:38 -04002916 * uninitialized).
2917 * There are three possibilities:
2918 * a> There is no split required: Entire extent should be initialized
2919 * b> Splits in two extents: Write is happening at either end of the extent
2920 * c> Splits in three extents: Somone is writing in middle of the extent
Eric Gouriou6f91bc52011-10-27 11:43:23 -04002921 *
2922 * Pre-conditions:
2923 * - The extent pointed to by 'path' is uninitialized.
2924 * - The extent pointed to by 'path' contains a superset
2925 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
2926 *
2927 * Post-conditions on success:
2928 * - the returned value is the number of blocks beyond map->l_lblk
2929 * that are allocated and initialized.
2930 * It is guaranteed to be >= map->m_len.
Amit Arora56055d32007-07-17 21:42:38 -04002931 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002932static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002933 struct inode *inode,
2934 struct ext4_map_blocks *map,
2935 struct ext4_ext_path *path)
Amit Arora56055d32007-07-17 21:42:38 -04002936{
Eric Gouriou6f91bc52011-10-27 11:43:23 -04002937 struct ext4_extent_header *eh;
Yongqiang Yang667eff32011-05-03 12:25:07 -04002938 struct ext4_map_blocks split_map;
2939 struct ext4_extent zero_ex;
2940 struct ext4_extent *ex;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002941 ext4_lblk_t ee_block, eof_block;
Dan Carpenterf85b2872011-10-26 03:42:36 -04002942 unsigned int ee_len, depth;
2943 int allocated;
Amit Arora56055d32007-07-17 21:42:38 -04002944 int err = 0;
Yongqiang Yang667eff32011-05-03 12:25:07 -04002945 int split_flag = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002946
2947 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
2948 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002949 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002950
2951 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
2952 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002953 if (eof_block < map->m_lblk + map->m_len)
2954 eof_block = map->m_lblk + map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04002955
2956 depth = ext_depth(inode);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04002957 eh = path[depth].p_hdr;
Amit Arora56055d32007-07-17 21:42:38 -04002958 ex = path[depth].p_ext;
2959 ee_block = le32_to_cpu(ex->ee_block);
2960 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002961 allocated = ee_len - (map->m_lblk - ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002962
Eric Gouriou6f91bc52011-10-27 11:43:23 -04002963 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
2964
2965 /* Pre-conditions */
2966 BUG_ON(!ext4_ext_is_uninitialized(ex));
2967 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
2968 BUG_ON(map->m_lblk + map->m_len > ee_block + ee_len);
2969
2970 /*
2971 * Attempt to transfer newly initialized blocks from the currently
2972 * uninitialized extent to its left neighbor. This is much cheaper
2973 * than an insertion followed by a merge as those involve costly
2974 * memmove() calls. This is the common case in steady state for
2975 * workloads doing fallocate(FALLOC_FL_KEEP_SIZE) followed by append
2976 * writes.
2977 *
2978 * Limitations of the current logic:
2979 * - L1: we only deal with writes at the start of the extent.
2980 * The approach could be extended to writes at the end
2981 * of the extent but this scenario was deemed less common.
2982 * - L2: we do not deal with writes covering the whole extent.
2983 * This would require removing the extent if the transfer
2984 * is possible.
2985 * - L3: we only attempt to merge with an extent stored in the
2986 * same extent tree node.
2987 */
2988 if ((map->m_lblk == ee_block) && /*L1*/
2989 (map->m_len < ee_len) && /*L2*/
2990 (ex > EXT_FIRST_EXTENT(eh))) { /*L3*/
2991 struct ext4_extent *prev_ex;
2992 ext4_lblk_t prev_lblk;
2993 ext4_fsblk_t prev_pblk, ee_pblk;
2994 unsigned int prev_len, write_len;
2995
2996 prev_ex = ex - 1;
2997 prev_lblk = le32_to_cpu(prev_ex->ee_block);
2998 prev_len = ext4_ext_get_actual_len(prev_ex);
2999 prev_pblk = ext4_ext_pblock(prev_ex);
3000 ee_pblk = ext4_ext_pblock(ex);
3001 write_len = map->m_len;
3002
3003 /*
3004 * A transfer of blocks from 'ex' to 'prev_ex' is allowed
3005 * upon those conditions:
3006 * - C1: prev_ex is initialized,
3007 * - C2: prev_ex is logically abutting ex,
3008 * - C3: prev_ex is physically abutting ex,
3009 * - C4: prev_ex can receive the additional blocks without
3010 * overflowing the (initialized) length limit.
3011 */
3012 if ((!ext4_ext_is_uninitialized(prev_ex)) && /*C1*/
3013 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3014 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
3015 (prev_len < (EXT_INIT_MAX_LEN - write_len))) { /*C4*/
3016 err = ext4_ext_get_access(handle, inode, path + depth);
3017 if (err)
3018 goto out;
3019
3020 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3021 map, ex, prev_ex);
3022
3023 /* Shift the start of ex by 'write_len' blocks */
3024 ex->ee_block = cpu_to_le32(ee_block + write_len);
3025 ext4_ext_store_pblock(ex, ee_pblk + write_len);
3026 ex->ee_len = cpu_to_le16(ee_len - write_len);
3027 ext4_ext_mark_uninitialized(ex); /* Restore the flag */
3028
3029 /* Extend prev_ex by 'write_len' blocks */
3030 prev_ex->ee_len = cpu_to_le16(prev_len + write_len);
3031
3032 /* Mark the block containing both extents as dirty */
3033 ext4_ext_dirty(handle, inode, path + depth);
3034
3035 /* Update path to point to the right extent */
3036 path[depth].p_ext = prev_ex;
3037
3038 /* Result: number of initialized blocks past m_lblk */
3039 allocated = write_len;
3040 goto out;
3041 }
3042 }
3043
Yongqiang Yang667eff32011-05-03 12:25:07 -04003044 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003045 /*
3046 * It is safe to convert extent to initialized via explicit
3047 * zeroout only if extent is fully insde i_size or new_size.
3048 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003049 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003050
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003051 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003052 if (ee_len <= 2*EXT4_EXT_ZERO_LEN &&
3053 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3054 err = ext4_ext_zeroout(inode, ex);
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003055 if (err)
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003056 goto out;
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003057
3058 err = ext4_ext_get_access(handle, inode, path + depth);
3059 if (err)
3060 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003061 ext4_ext_mark_initialized(ex);
3062 ext4_ext_try_to_merge(inode, path, ex);
3063 err = ext4_ext_dirty(handle, inode, path + depth);
3064 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04003065 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003066
Amit Arora56055d32007-07-17 21:42:38 -04003067 /*
Yongqiang Yang667eff32011-05-03 12:25:07 -04003068 * four cases:
3069 * 1. split the extent into three extents.
3070 * 2. split the extent into two extents, zeroout the first half.
3071 * 3. split the extent into two extents, zeroout the second half.
3072 * 4. split the extent into two extents with out zeroout.
Amit Arora56055d32007-07-17 21:42:38 -04003073 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003074 split_map.m_lblk = map->m_lblk;
3075 split_map.m_len = map->m_len;
3076
3077 if (allocated > map->m_len) {
3078 if (allocated <= EXT4_EXT_ZERO_LEN &&
3079 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3080 /* case 3 */
3081 zero_ex.ee_block =
Allison Henderson9b940f82011-05-16 10:11:09 -04003082 cpu_to_le32(map->m_lblk);
3083 zero_ex.ee_len = cpu_to_le16(allocated);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003084 ext4_ext_store_pblock(&zero_ex,
3085 ext4_ext_pblock(ex) + map->m_lblk - ee_block);
3086 err = ext4_ext_zeroout(inode, &zero_ex);
Amit Arora56055d32007-07-17 21:42:38 -04003087 if (err)
3088 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003089 split_map.m_lblk = map->m_lblk;
3090 split_map.m_len = allocated;
3091 } else if ((map->m_lblk - ee_block + map->m_len <
3092 EXT4_EXT_ZERO_LEN) &&
3093 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3094 /* case 2 */
3095 if (map->m_lblk != ee_block) {
3096 zero_ex.ee_block = ex->ee_block;
3097 zero_ex.ee_len = cpu_to_le16(map->m_lblk -
3098 ee_block);
3099 ext4_ext_store_pblock(&zero_ex,
3100 ext4_ext_pblock(ex));
3101 err = ext4_ext_zeroout(inode, &zero_ex);
3102 if (err)
3103 goto out;
3104 }
3105
Yongqiang Yang667eff32011-05-03 12:25:07 -04003106 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003107 split_map.m_len = map->m_lblk - ee_block + map->m_len;
3108 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003109 }
3110 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003111
3112 allocated = ext4_split_extent(handle, inode, path,
3113 &split_map, split_flag, 0);
3114 if (allocated < 0)
3115 err = allocated;
3116
Amit Arora56055d32007-07-17 21:42:38 -04003117out:
3118 return err ? err : allocated;
3119}
3120
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003121/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003122 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003123 * ext4_get_blocks_dio_write() when DIO to write
3124 * to an uninitialized extent.
3125 *
Paul Bollefd018fe2011-02-15 00:05:43 +01003126 * Writing to an uninitialized extent may result in splitting the uninitialized
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003127 * extent into multiple /initialized uninitialized extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003128 * There are three possibilities:
3129 * a> There is no split required: Entire extent should be uninitialized
3130 * b> Splits in two extents: Write is happening at either end of the extent
3131 * c> Splits in three extents: Somone is writing in middle of the extent
3132 *
3133 * One of more index blocks maybe needed if the extent tree grow after
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003134 * the uninitialized extent split. To prevent ENOSPC occur at the IO
Mingming Cao00314622009-09-28 15:49:08 -04003135 * complete, we need to split the uninitialized extent before DIO submit
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02003136 * the IO. The uninitialized extent called at this time will be split
Mingming Cao00314622009-09-28 15:49:08 -04003137 * into three uninitialized extent(at most). After IO complete, the part
3138 * being filled will be convert to initialized by the end_io callback function
3139 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003140 *
3141 * Returns the size of uninitialized extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003142 */
3143static int ext4_split_unwritten_extents(handle_t *handle,
3144 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003145 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003146 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04003147 int flags)
3148{
Yongqiang Yang667eff32011-05-03 12:25:07 -04003149 ext4_lblk_t eof_block;
3150 ext4_lblk_t ee_block;
3151 struct ext4_extent *ex;
3152 unsigned int ee_len;
3153 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003154
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003155 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
3156 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003157 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003158
3159 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3160 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003161 if (eof_block < map->m_lblk + map->m_len)
3162 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003163 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003164 * It is safe to convert extent to initialized via explicit
3165 * zeroout only if extent is fully insde i_size or new_size.
3166 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003167 depth = ext_depth(inode);
3168 ex = path[depth].p_ext;
3169 ee_block = le32_to_cpu(ex->ee_block);
3170 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003171
Yongqiang Yang667eff32011-05-03 12:25:07 -04003172 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
3173 split_flag |= EXT4_EXT_MARK_UNINIT2;
Mingming Cao00314622009-09-28 15:49:08 -04003174
Yongqiang Yang667eff32011-05-03 12:25:07 -04003175 flags |= EXT4_GET_BLOCKS_PRE_IO;
3176 return ext4_split_extent(handle, inode, path, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003177}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003178
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003179static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003180 struct inode *inode,
3181 struct ext4_ext_path *path)
3182{
3183 struct ext4_extent *ex;
Mingming Cao00314622009-09-28 15:49:08 -04003184 int depth;
3185 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003186
3187 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003188 ex = path[depth].p_ext;
3189
Yongqiang Yang197217a2011-05-03 11:45:29 -04003190 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3191 "block %llu, max_blocks %u\n", inode->i_ino,
3192 (unsigned long long)le32_to_cpu(ex->ee_block),
3193 ext4_ext_get_actual_len(ex));
3194
Mingming Cao00314622009-09-28 15:49:08 -04003195 err = ext4_ext_get_access(handle, inode, path + depth);
3196 if (err)
3197 goto out;
3198 /* first mark the extent as initialized */
3199 ext4_ext_mark_initialized(ex);
3200
Yongqiang Yang197217a2011-05-03 11:45:29 -04003201 /* note: ext4_ext_correct_indexes() isn't needed here because
3202 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003203 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04003204 ext4_ext_try_to_merge(inode, path, ex);
3205
Mingming Cao00314622009-09-28 15:49:08 -04003206 /* Mark modified extent as dirty */
3207 err = ext4_ext_dirty(handle, inode, path + depth);
3208out:
3209 ext4_ext_show_leaf(inode, path);
3210 return err;
3211}
3212
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003213static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3214 sector_t block, int count)
3215{
3216 int i;
3217 for (i = 0; i < count; i++)
3218 unmap_underlying_metadata(bdev, block + i);
3219}
3220
Theodore Ts'o58590b02010-10-27 21:23:12 -04003221/*
3222 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3223 */
3224static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
Eric Sandeend002ebf2011-01-10 13:03:35 -05003225 ext4_lblk_t lblk,
Theodore Ts'o58590b02010-10-27 21:23:12 -04003226 struct ext4_ext_path *path,
3227 unsigned int len)
3228{
3229 int i, depth;
3230 struct ext4_extent_header *eh;
Sergey Senozhatsky65922cb2011-03-23 14:08:27 -04003231 struct ext4_extent *last_ex;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003232
3233 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3234 return 0;
3235
3236 depth = ext_depth(inode);
3237 eh = path[depth].p_hdr;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003238
3239 if (unlikely(!eh->eh_entries)) {
3240 EXT4_ERROR_INODE(inode, "eh->eh_entries == 0 and "
3241 "EOFBLOCKS_FL set");
3242 return -EIO;
3243 }
3244 last_ex = EXT_LAST_EXTENT(eh);
3245 /*
3246 * We should clear the EOFBLOCKS_FL flag if we are writing the
3247 * last block in the last extent in the file. We test this by
3248 * first checking to see if the caller to
3249 * ext4_ext_get_blocks() was interested in the last block (or
3250 * a block beyond the last block) in the current extent. If
3251 * this turns out to be false, we can bail out from this
3252 * function immediately.
3253 */
Eric Sandeend002ebf2011-01-10 13:03:35 -05003254 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
Theodore Ts'o58590b02010-10-27 21:23:12 -04003255 ext4_ext_get_actual_len(last_ex))
3256 return 0;
3257 /*
3258 * If the caller does appear to be planning to write at or
3259 * beyond the end of the current extent, we then test to see
3260 * if the current extent is the last extent in the file, by
3261 * checking to make sure it was reached via the rightmost node
3262 * at each level of the tree.
3263 */
3264 for (i = depth-1; i >= 0; i--)
3265 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3266 return 0;
3267 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3268 return ext4_mark_inode_dirty(handle, inode);
3269}
3270
Aditya Kali7b415bf2011-09-09 19:04:51 -04003271/**
3272 * ext4_find_delalloc_range: find delayed allocated block in the given range.
3273 *
3274 * Goes through the buffer heads in the range [lblk_start, lblk_end] and returns
3275 * whether there are any buffers marked for delayed allocation. It returns '1'
3276 * on the first delalloc'ed buffer head found. If no buffer head in the given
3277 * range is marked for delalloc, it returns 0.
3278 * lblk_start should always be <= lblk_end.
3279 * search_hint_reverse is to indicate that searching in reverse from lblk_end to
3280 * lblk_start might be more efficient (i.e., we will likely hit the delalloc'ed
3281 * block sooner). This is useful when blocks are truncated sequentially from
3282 * lblk_start towards lblk_end.
3283 */
3284static int ext4_find_delalloc_range(struct inode *inode,
3285 ext4_lblk_t lblk_start,
3286 ext4_lblk_t lblk_end,
3287 int search_hint_reverse)
3288{
3289 struct address_space *mapping = inode->i_mapping;
3290 struct buffer_head *head, *bh = NULL;
3291 struct page *page;
3292 ext4_lblk_t i, pg_lblk;
3293 pgoff_t index;
3294
3295 /* reverse search wont work if fs block size is less than page size */
3296 if (inode->i_blkbits < PAGE_CACHE_SHIFT)
3297 search_hint_reverse = 0;
3298
3299 if (search_hint_reverse)
3300 i = lblk_end;
3301 else
3302 i = lblk_start;
3303
3304 index = i >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
3305
3306 while ((i >= lblk_start) && (i <= lblk_end)) {
3307 page = find_get_page(mapping, index);
Aditya Kali5356f262011-09-09 19:20:51 -04003308 if (!page)
Aditya Kali7b415bf2011-09-09 19:04:51 -04003309 goto nextpage;
3310
Aditya Kali7b415bf2011-09-09 19:04:51 -04003311 if (!page_has_buffers(page))
3312 goto nextpage;
3313
3314 head = page_buffers(page);
3315 if (!head)
3316 goto nextpage;
3317
3318 bh = head;
3319 pg_lblk = index << (PAGE_CACHE_SHIFT -
3320 inode->i_blkbits);
3321 do {
3322 if (unlikely(pg_lblk < lblk_start)) {
3323 /*
3324 * This is possible when fs block size is less
3325 * than page size and our cluster starts/ends in
3326 * middle of the page. So we need to skip the
3327 * initial few blocks till we reach the 'lblk'
3328 */
3329 pg_lblk++;
3330 continue;
3331 }
3332
Aditya Kali5356f262011-09-09 19:20:51 -04003333 /* Check if the buffer is delayed allocated and that it
3334 * is not yet mapped. (when da-buffers are mapped during
3335 * their writeout, their da_mapped bit is set.)
3336 */
3337 if (buffer_delay(bh) && !buffer_da_mapped(bh)) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04003338 page_cache_release(page);
Aditya Kalid8990242011-09-09 19:18:51 -04003339 trace_ext4_find_delalloc_range(inode,
3340 lblk_start, lblk_end,
3341 search_hint_reverse,
3342 1, i);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003343 return 1;
3344 }
3345 if (search_hint_reverse)
3346 i--;
3347 else
3348 i++;
3349 } while ((i >= lblk_start) && (i <= lblk_end) &&
3350 ((bh = bh->b_this_page) != head));
3351nextpage:
3352 if (page)
3353 page_cache_release(page);
3354 /*
3355 * Move to next page. 'i' will be the first lblk in the next
3356 * page.
3357 */
3358 if (search_hint_reverse)
3359 index--;
3360 else
3361 index++;
3362 i = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
3363 }
3364
Aditya Kalid8990242011-09-09 19:18:51 -04003365 trace_ext4_find_delalloc_range(inode, lblk_start, lblk_end,
3366 search_hint_reverse, 0, 0);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003367 return 0;
3368}
3369
3370int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk,
3371 int search_hint_reverse)
3372{
3373 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3374 ext4_lblk_t lblk_start, lblk_end;
3375 lblk_start = lblk & (~(sbi->s_cluster_ratio - 1));
3376 lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
3377
3378 return ext4_find_delalloc_range(inode, lblk_start, lblk_end,
3379 search_hint_reverse);
3380}
3381
3382/**
3383 * Determines how many complete clusters (out of those specified by the 'map')
3384 * are under delalloc and were reserved quota for.
3385 * This function is called when we are writing out the blocks that were
3386 * originally written with their allocation delayed, but then the space was
3387 * allocated using fallocate() before the delayed allocation could be resolved.
3388 * The cases to look for are:
3389 * ('=' indicated delayed allocated blocks
3390 * '-' indicates non-delayed allocated blocks)
3391 * (a) partial clusters towards beginning and/or end outside of allocated range
3392 * are not delalloc'ed.
3393 * Ex:
3394 * |----c---=|====c====|====c====|===-c----|
3395 * |++++++ allocated ++++++|
3396 * ==> 4 complete clusters in above example
3397 *
3398 * (b) partial cluster (outside of allocated range) towards either end is
3399 * marked for delayed allocation. In this case, we will exclude that
3400 * cluster.
3401 * Ex:
3402 * |----====c========|========c========|
3403 * |++++++ allocated ++++++|
3404 * ==> 1 complete clusters in above example
3405 *
3406 * Ex:
3407 * |================c================|
3408 * |++++++ allocated ++++++|
3409 * ==> 0 complete clusters in above example
3410 *
3411 * The ext4_da_update_reserve_space will be called only if we
3412 * determine here that there were some "entire" clusters that span
3413 * this 'allocated' range.
3414 * In the non-bigalloc case, this function will just end up returning num_blks
3415 * without ever calling ext4_find_delalloc_range.
3416 */
3417static unsigned int
3418get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start,
3419 unsigned int num_blks)
3420{
3421 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3422 ext4_lblk_t alloc_cluster_start, alloc_cluster_end;
3423 ext4_lblk_t lblk_from, lblk_to, c_offset;
3424 unsigned int allocated_clusters = 0;
3425
3426 alloc_cluster_start = EXT4_B2C(sbi, lblk_start);
3427 alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1);
3428
3429 /* max possible clusters for this allocation */
3430 allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1;
3431
Aditya Kalid8990242011-09-09 19:18:51 -04003432 trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks);
3433
Aditya Kali7b415bf2011-09-09 19:04:51 -04003434 /* Check towards left side */
3435 c_offset = lblk_start & (sbi->s_cluster_ratio - 1);
3436 if (c_offset) {
3437 lblk_from = lblk_start & (~(sbi->s_cluster_ratio - 1));
3438 lblk_to = lblk_from + c_offset - 1;
3439
3440 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
3441 allocated_clusters--;
3442 }
3443
3444 /* Now check towards right. */
3445 c_offset = (lblk_start + num_blks) & (sbi->s_cluster_ratio - 1);
3446 if (allocated_clusters && c_offset) {
3447 lblk_from = lblk_start + num_blks;
3448 lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1;
3449
3450 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
3451 allocated_clusters--;
3452 }
3453
3454 return allocated_clusters;
3455}
3456
Mingming Cao00314622009-09-28 15:49:08 -04003457static int
3458ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003459 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003460 struct ext4_ext_path *path, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003461 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003462{
3463 int ret = 0;
3464 int err = 0;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003465 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Mingming Cao00314622009-09-28 15:49:08 -04003466
3467 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3468 "block %llu, max_blocks %u, flags %d, allocated %u",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003469 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04003470 flags, allocated);
3471 ext4_ext_show_leaf(inode, path);
3472
Aditya Kalid8990242011-09-09 19:18:51 -04003473 trace_ext4_ext_handle_uninitialized_extents(inode, map, allocated,
3474 newblock);
3475
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003476 /* get_block() before submit the IO, split the extent */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003477 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003478 ret = ext4_split_unwritten_extents(handle, inode, map,
3479 path, flags);
Mingming5f524952009-11-10 10:48:04 -05003480 /*
3481 * Flag the inode(non aio case) or end_io struct (aio case)
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003482 * that this IO needs to conversion to written when IO is
Mingming5f524952009-11-10 10:48:04 -05003483 * completed
3484 */
Tao Mab3ff05692011-10-26 11:08:39 -04003485 if (io) {
3486 if (!(io->flag & EXT4_IO_END_UNWRITTEN)) {
3487 io->flag = EXT4_IO_END_UNWRITTEN;
3488 atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten);
3489 }
Eric Sandeene9e3bce2011-02-12 08:17:34 -05003490 } else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003491 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
Jiaying Zhang744692d2010-03-04 16:14:02 -05003492 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003493 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao00314622009-09-28 15:49:08 -04003494 goto out;
3495 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003496 /* IO end_io complete, convert the filled extent to written */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003497 if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003498 ret = ext4_convert_unwritten_extents_endio(handle, inode,
Mingming Cao00314622009-09-28 15:49:08 -04003499 path);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003500 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05003501 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05003502 err = check_eofblocks_fl(handle, inode, map->m_lblk,
3503 path, map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003504 } else
3505 err = ret;
Mingming Cao00314622009-09-28 15:49:08 -04003506 goto out2;
3507 }
3508 /* buffered IO case */
3509 /*
3510 * repeat fallocate creation request
3511 * we already have an unwritten extent
3512 */
3513 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3514 goto map_out;
3515
3516 /* buffered READ or buffered write_begin() lookup */
3517 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3518 /*
3519 * We have blocks reserved already. We
3520 * return allocated blocks so that delalloc
3521 * won't do block reservation for us. But
3522 * the buffer head will be unmapped so that
3523 * a read from the block returns 0s.
3524 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003525 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003526 goto out1;
3527 }
3528
3529 /* buffered write, writepage time, convert*/
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003530 ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04003531 if (ret >= 0)
Jan Karab436b9b2009-12-08 23:51:10 -05003532 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04003533out:
3534 if (ret <= 0) {
3535 err = ret;
3536 goto out2;
3537 } else
3538 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003539 map->m_flags |= EXT4_MAP_NEW;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003540 /*
3541 * if we allocated more blocks than requested
3542 * we need to make sure we unmap the extra block
3543 * allocated. The actual needed block will get
3544 * unmapped later when we find the buffer_head marked
3545 * new.
3546 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003547 if (allocated > map->m_len) {
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003548 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003549 newblock + map->m_len,
3550 allocated - map->m_len);
3551 allocated = map->m_len;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003552 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003553
3554 /*
3555 * If we have done fallocate with the offset that is already
3556 * delayed allocated, we would have block reservation
3557 * and quota reservation done in the delayed write path.
3558 * But fallocate would have already updated quota and block
3559 * count for this offset. So cancel these reservation
3560 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003561 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
3562 unsigned int reserved_clusters;
3563 reserved_clusters = get_reserved_cluster_alloc(inode,
3564 map->m_lblk, map->m_len);
3565 if (reserved_clusters)
3566 ext4_da_update_reserve_space(inode,
3567 reserved_clusters,
3568 0);
3569 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003570
Mingming Cao00314622009-09-28 15:49:08 -04003571map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003572 map->m_flags |= EXT4_MAP_MAPPED;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04003573 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
3574 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
3575 map->m_len);
3576 if (err < 0)
3577 goto out2;
3578 }
Mingming Cao00314622009-09-28 15:49:08 -04003579out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003580 if (allocated > map->m_len)
3581 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003582 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003583 map->m_pblk = newblock;
3584 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04003585out2:
3586 if (path) {
3587 ext4_ext_drop_refs(path);
3588 kfree(path);
3589 }
3590 return err ? err : allocated;
3591}
Theodore Ts'o58590b02010-10-27 21:23:12 -04003592
Mingming Cao00314622009-09-28 15:49:08 -04003593/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003594 * get_implied_cluster_alloc - check to see if the requested
3595 * allocation (in the map structure) overlaps with a cluster already
3596 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04003597 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003598 * @map The requested lblk->pblk mapping
3599 * @ex The extent structure which might contain an implied
3600 * cluster allocation
3601 *
3602 * This function is called by ext4_ext_map_blocks() after we failed to
3603 * find blocks that were already in the inode's extent tree. Hence,
3604 * we know that the beginning of the requested region cannot overlap
3605 * the extent from the inode's extent tree. There are three cases we
3606 * want to catch. The first is this case:
3607 *
3608 * |--- cluster # N--|
3609 * |--- extent ---| |---- requested region ---|
3610 * |==========|
3611 *
3612 * The second case that we need to test for is this one:
3613 *
3614 * |--------- cluster # N ----------------|
3615 * |--- requested region --| |------- extent ----|
3616 * |=======================|
3617 *
3618 * The third case is when the requested region lies between two extents
3619 * within the same cluster:
3620 * |------------- cluster # N-------------|
3621 * |----- ex -----| |---- ex_right ----|
3622 * |------ requested region ------|
3623 * |================|
3624 *
3625 * In each of the above cases, we need to set the map->m_pblk and
3626 * map->m_len so it corresponds to the return the extent labelled as
3627 * "|====|" from cluster #N, since it is already in use for data in
3628 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
3629 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
3630 * as a new "allocated" block region. Otherwise, we will return 0 and
3631 * ext4_ext_map_blocks() will then allocate one or more new clusters
3632 * by calling ext4_mb_new_blocks().
3633 */
Aditya Kalid8990242011-09-09 19:18:51 -04003634static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003635 struct ext4_map_blocks *map,
3636 struct ext4_extent *ex,
3637 struct ext4_ext_path *path)
3638{
Aditya Kalid8990242011-09-09 19:18:51 -04003639 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003640 ext4_lblk_t c_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
3641 ext4_lblk_t ex_cluster_start, ex_cluster_end;
3642 ext4_lblk_t rr_cluster_start, rr_cluster_end;
3643 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
3644 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
3645 unsigned short ee_len = ext4_ext_get_actual_len(ex);
3646
3647 /* The extent passed in that we are trying to match */
3648 ex_cluster_start = EXT4_B2C(sbi, ee_block);
3649 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
3650
3651 /* The requested region passed into ext4_map_blocks() */
3652 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
3653 rr_cluster_end = EXT4_B2C(sbi, map->m_lblk + map->m_len - 1);
3654
3655 if ((rr_cluster_start == ex_cluster_end) ||
3656 (rr_cluster_start == ex_cluster_start)) {
3657 if (rr_cluster_start == ex_cluster_end)
3658 ee_start += ee_len - 1;
3659 map->m_pblk = (ee_start & ~(sbi->s_cluster_ratio - 1)) +
3660 c_offset;
3661 map->m_len = min(map->m_len,
3662 (unsigned) sbi->s_cluster_ratio - c_offset);
3663 /*
3664 * Check for and handle this case:
3665 *
3666 * |--------- cluster # N-------------|
3667 * |------- extent ----|
3668 * |--- requested region ---|
3669 * |===========|
3670 */
3671
3672 if (map->m_lblk < ee_block)
3673 map->m_len = min(map->m_len, ee_block - map->m_lblk);
3674
3675 /*
3676 * Check for the case where there is already another allocated
3677 * block to the right of 'ex' but before the end of the cluster.
3678 *
3679 * |------------- cluster # N-------------|
3680 * |----- ex -----| |---- ex_right ----|
3681 * |------ requested region ------|
3682 * |================|
3683 */
3684 if (map->m_lblk > ee_block) {
3685 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
3686 map->m_len = min(map->m_len, next - map->m_lblk);
3687 }
Aditya Kalid8990242011-09-09 19:18:51 -04003688
3689 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003690 return 1;
3691 }
Aditya Kalid8990242011-09-09 19:18:51 -04003692
3693 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003694 return 0;
3695}
3696
3697
3698/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05003699 * Block allocation/map/preallocation routine for extents based files
3700 *
3701 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003702 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003703 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3704 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05003705 *
3706 * return > 0, number of of blocks already mapped/allocated
3707 * if create == 0 and these are pre-allocated blocks
3708 * buffer head is unmapped
3709 * otherwise blocks are mapped
3710 *
3711 * return = 0, if plain look up failed (blocks have not been allocated)
3712 * buffer head is unmapped
3713 *
3714 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003715 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003716int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3717 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07003718{
3719 struct ext4_ext_path *path = NULL;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003720 struct ext4_extent newex, *ex, *ex2;
3721 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003722 ext4_fsblk_t newblock = 0;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003723 int free_on_err = 0, err = 0, depth, ret;
3724 unsigned int allocated = 0, offset = 0;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003725 unsigned int allocated_clusters = 0, reserved_clusters = 0;
Allison Hendersone8613042011-05-25 07:41:46 -04003726 unsigned int punched_out = 0;
3727 unsigned int result = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003728 struct ext4_allocation_request ar;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003729 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003730 ext4_lblk_t cluster_offset;
Allison Hendersone8613042011-05-25 07:41:46 -04003731 struct ext4_map_blocks punch_map;
Alex Tomasa86c6182006-10-11 01:21:03 -07003732
Mingming84fe3be2009-09-01 08:44:37 -04003733 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003734 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003735 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07003736
3737 /* check in cache */
Robin Dong015861b2011-07-17 23:27:43 -04003738 if (!(flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) &&
3739 ext4_ext_in_cache(inode, map->m_lblk, &newex)) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003740 if (!newex.ee_start_lo && !newex.ee_start_hi) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04003741 if ((sbi->s_cluster_ratio > 1) &&
3742 ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
3743 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3744
Theodore Ts'oc2177052009-05-14 00:58:52 -04003745 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003746 /*
3747 * block isn't allocated yet and
3748 * user doesn't want to allocate it
3749 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003750 goto out2;
3751 }
3752 /* we should allocate requested block */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003753 } else {
Alex Tomasa86c6182006-10-11 01:21:03 -07003754 /* block is already allocated */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003755 if (sbi->s_cluster_ratio > 1)
3756 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003757 newblock = map->m_lblk
Dave Kleikamp8c55e202007-05-24 13:04:54 -04003758 - le32_to_cpu(newex.ee_block)
Theodore Ts'obf89d162010-10-27 21:30:14 -04003759 + ext4_ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003760 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003761 allocated = ext4_ext_get_actual_len(&newex) -
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003762 (map->m_lblk - le32_to_cpu(newex.ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -07003763 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07003764 }
3765 }
3766
3767 /* find extent for this block */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003768 path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07003769 if (IS_ERR(path)) {
3770 err = PTR_ERR(path);
3771 path = NULL;
3772 goto out2;
3773 }
3774
3775 depth = ext_depth(inode);
3776
3777 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003778 * consistent leaf must not be empty;
3779 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07003780 * this is why assert can't be put in ext4_ext_find_extent()
3781 */
Frank Mayhar273df552010-03-02 11:46:09 -05003782 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
3783 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04003784 "lblock: %lu, depth: %d pblock %lld",
3785 (unsigned long) map->m_lblk, depth,
3786 path[depth].p_block);
Surbhi Palande034fb4c2009-12-14 09:53:52 -05003787 err = -EIO;
3788 goto out2;
3789 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003790
Avantika Mathur7e028972006-12-06 20:41:33 -08003791 ex = path[depth].p_ext;
3792 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003793 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003794 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003795 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003796
3797 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003798 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04003799 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003800 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003801 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04003802
3803 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
3804
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003805 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003806 if (in_range(map->m_lblk, ee_block, ee_len)) {
Theodore Ts'o0aa06002011-09-09 18:54:51 -04003807 ext4_fsblk_t partial_cluster = 0;
3808
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003809 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003810 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003811 allocated = ee_len - (map->m_lblk - ee_block);
3812 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
3813 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04003814
Allison Hendersone8613042011-05-25 07:41:46 -04003815 if ((flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) == 0) {
3816 /*
3817 * Do not put uninitialized extent
3818 * in the cache
3819 */
3820 if (!ext4_ext_is_uninitialized(ex)) {
3821 ext4_ext_put_in_cache(inode, ee_block,
3822 ee_len, ee_start);
3823 goto out;
3824 }
3825 ret = ext4_ext_handle_uninitialized_extents(
3826 handle, inode, map, path, flags,
3827 allocated, newblock);
3828 return ret;
Amit Arora56055d32007-07-17 21:42:38 -04003829 }
Allison Hendersone8613042011-05-25 07:41:46 -04003830
3831 /*
3832 * Punch out the map length, but only to the
3833 * end of the extent
3834 */
3835 punched_out = allocated < map->m_len ?
3836 allocated : map->m_len;
3837
3838 /*
3839 * Sense extents need to be converted to
3840 * uninitialized, they must fit in an
3841 * uninitialized extent
3842 */
3843 if (punched_out > EXT_UNINIT_MAX_LEN)
3844 punched_out = EXT_UNINIT_MAX_LEN;
3845
3846 punch_map.m_lblk = map->m_lblk;
3847 punch_map.m_pblk = newblock;
3848 punch_map.m_len = punched_out;
3849 punch_map.m_flags = 0;
3850
3851 /* Check to see if the extent needs to be split */
3852 if (punch_map.m_len != ee_len ||
3853 punch_map.m_lblk != ee_block) {
3854
3855 ret = ext4_split_extent(handle, inode,
3856 path, &punch_map, 0,
3857 EXT4_GET_BLOCKS_PUNCH_OUT_EXT |
3858 EXT4_GET_BLOCKS_PRE_IO);
3859
3860 if (ret < 0) {
3861 err = ret;
3862 goto out2;
3863 }
3864 /*
3865 * find extent for the block at
3866 * the start of the hole
3867 */
3868 ext4_ext_drop_refs(path);
3869 kfree(path);
3870
3871 path = ext4_ext_find_extent(inode,
3872 map->m_lblk, NULL);
3873 if (IS_ERR(path)) {
3874 err = PTR_ERR(path);
3875 path = NULL;
3876 goto out2;
3877 }
3878
3879 depth = ext_depth(inode);
3880 ex = path[depth].p_ext;
3881 ee_len = ext4_ext_get_actual_len(ex);
3882 ee_block = le32_to_cpu(ex->ee_block);
3883 ee_start = ext4_ext_pblock(ex);
3884
3885 }
3886
3887 ext4_ext_mark_uninitialized(ex);
3888
Allison Hendersonf7d0d372011-07-17 23:17:02 -04003889 ext4_ext_invalidate_cache(inode);
3890
3891 err = ext4_ext_rm_leaf(handle, inode, path,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04003892 &partial_cluster, map->m_lblk,
3893 map->m_lblk + punched_out);
Allison Hendersonf7d0d372011-07-17 23:17:02 -04003894
3895 if (!err && path->p_hdr->eh_entries == 0) {
3896 /*
3897 * Punch hole freed all of this sub tree,
3898 * so we need to correct eh_depth
3899 */
3900 err = ext4_ext_get_access(handle, inode, path);
3901 if (err == 0) {
3902 ext_inode_hdr(inode)->eh_depth = 0;
3903 ext_inode_hdr(inode)->eh_max =
3904 cpu_to_le16(ext4_ext_space_root(
3905 inode, 0));
3906
3907 err = ext4_ext_dirty(
3908 handle, inode, path);
3909 }
3910 }
Allison Hendersone8613042011-05-25 07:41:46 -04003911
3912 goto out2;
Alex Tomasa86c6182006-10-11 01:21:03 -07003913 }
3914 }
3915
Aditya Kali7b415bf2011-09-09 19:04:51 -04003916 if ((sbi->s_cluster_ratio > 1) &&
3917 ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
3918 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3919
Alex Tomasa86c6182006-10-11 01:21:03 -07003920 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003921 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07003922 * we couldn't try to create block if create flag is zero
3923 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04003924 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003925 /*
3926 * put just found gap into cache to speed up
3927 * subsequent requests
3928 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003929 ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
Alex Tomasa86c6182006-10-11 01:21:03 -07003930 goto out2;
3931 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003932
Alex Tomasa86c6182006-10-11 01:21:03 -07003933 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003934 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07003935 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003936 map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003937 newex.ee_block = cpu_to_le32(map->m_lblk);
3938 cluster_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
3939
3940 /*
3941 * If we are doing bigalloc, check to see if the extent returned
3942 * by ext4_ext_find_extent() implies a cluster we can use.
3943 */
3944 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04003945 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003946 ar.len = allocated = map->m_len;
3947 newblock = map->m_pblk;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003948 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003949 goto got_allocated_blocks;
3950 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003951
Alex Tomasc9de5602008-01-29 00:19:52 -05003952 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003953 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003954 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3955 if (err)
3956 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003957 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003958 ex2 = NULL;
3959 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
Alex Tomasc9de5602008-01-29 00:19:52 -05003960 if (err)
3961 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04003962
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003963 /* Check if the extent after searching to the right implies a
3964 * cluster we can use. */
3965 if ((sbi->s_cluster_ratio > 1) && ex2 &&
Aditya Kalid8990242011-09-09 19:18:51 -04003966 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003967 ar.len = allocated = map->m_len;
3968 newblock = map->m_pblk;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003969 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003970 goto got_allocated_blocks;
3971 }
3972
Amit Arora749269f2007-07-18 09:02:56 -04003973 /*
3974 * See if request is beyond maximum number of blocks we can have in
3975 * a single extent. For an initialized extent this limit is
3976 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3977 * EXT_UNINIT_MAX_LEN.
3978 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003979 if (map->m_len > EXT_INIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003980 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003981 map->m_len = EXT_INIT_MAX_LEN;
3982 else if (map->m_len > EXT_UNINIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003983 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003984 map->m_len = EXT_UNINIT_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04003985
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003986 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003987 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003988 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04003989 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003990 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04003991 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003992 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003993
3994 /* allocate new block */
3995 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003996 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
3997 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003998 /*
3999 * We calculate the offset from the beginning of the cluster
4000 * for the logical block number, since when we allocate a
4001 * physical cluster, the physical block should start at the
4002 * same offset from the beginning of the cluster. This is
4003 * needed so that future calls to get_implied_cluster_alloc()
4004 * work correctly.
4005 */
4006 offset = map->m_lblk & (sbi->s_cluster_ratio - 1);
4007 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
4008 ar.goal -= offset;
4009 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05004010 if (S_ISREG(inode->i_mode))
4011 ar.flags = EXT4_MB_HINT_DATA;
4012 else
4013 /* disable in-core preallocation for non-regular files */
4014 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04004015 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4016 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05004017 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004018 if (!newblock)
4019 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04004020 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004021 ar.goal, newblock, allocated);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004022 free_on_err = 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004023 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004024 ar.len = EXT4_C2B(sbi, ar.len) - offset;
4025 if (ar.len > allocated)
4026 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004027
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004028got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07004029 /* try to insert new extent into found leaf and return */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004030 ext4_ext_store_pblock(&newex, newblock + offset);
Alex Tomasc9de5602008-01-29 00:19:52 -05004031 newex.ee_len = cpu_to_le16(ar.len);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004032 /* Mark uninitialized */
4033 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
Amit Aroraa2df2a62007-07-17 21:42:41 -04004034 ext4_ext_mark_uninitialized(&newex);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004035 /*
Jiaying Zhang744692d2010-03-04 16:14:02 -05004036 * io_end structure was created for every IO write to an
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004037 * uninitialized extent. To avoid unnecessary conversion,
Jiaying Zhang744692d2010-03-04 16:14:02 -05004038 * here we flag the IO that really needs the conversion.
Mingming5f524952009-11-10 10:48:04 -05004039 * For non asycn direct IO case, flag the inode state
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004040 * that we need to perform conversion when IO is done.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004041 */
Jiaying Zhang744692d2010-03-04 16:14:02 -05004042 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Tao Mab3ff05692011-10-26 11:08:39 -04004043 if (io) {
4044 if (!(io->flag & EXT4_IO_END_UNWRITTEN)) {
4045 io->flag = EXT4_IO_END_UNWRITTEN;
4046 atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten);
4047 }
Eric Sandeene9e3bce2011-02-12 08:17:34 -05004048 } else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004049 ext4_set_inode_state(inode,
4050 EXT4_STATE_DIO_UNWRITTEN);
Mingming5f524952009-11-10 10:48:04 -05004051 }
Jiaying Zhang744692d2010-03-04 16:14:02 -05004052 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004053 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004054 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004055
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004056 err = 0;
4057 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
4058 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4059 path, ar.len);
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004060 if (!err)
4061 err = ext4_ext_insert_extent(handle, inode, path,
4062 &newex, flags);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004063 if (err && free_on_err) {
Maxim Patlasov7132de72011-07-10 19:37:48 -04004064 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
4065 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
Alex Tomas315054f2007-05-24 13:04:25 -04004066 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05004067 /* not a good idea to call discard here directly,
4068 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004069 ext4_discard_preallocations(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05004070 ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex),
Maxim Patlasov7132de72011-07-10 19:37:48 -04004071 ext4_ext_get_actual_len(&newex), fb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004072 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04004073 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004074
Alex Tomasa86c6182006-10-11 01:21:03 -07004075 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04004076 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004077 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004078 if (allocated > map->m_len)
4079 allocated = map->m_len;
4080 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07004081
Jan Karab436b9b2009-12-08 23:51:10 -05004082 /*
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004083 * Update reserved blocks/metadata blocks after successful
4084 * block allocation which had been deferred till now.
4085 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04004086 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
4087 /*
4088 * Check how many clusters we had reserved this allocted range.
4089 */
4090 reserved_clusters = get_reserved_cluster_alloc(inode,
4091 map->m_lblk, allocated);
4092 if (map->m_flags & EXT4_MAP_FROM_CLUSTER) {
4093 if (reserved_clusters) {
4094 /*
4095 * We have clusters reserved for this range.
4096 * But since we are not doing actual allocation
4097 * and are simply using blocks from previously
4098 * allocated cluster, we should release the
4099 * reservation and not claim quota.
4100 */
4101 ext4_da_update_reserve_space(inode,
4102 reserved_clusters, 0);
4103 }
4104 } else {
4105 BUG_ON(allocated_clusters < reserved_clusters);
4106 /* We will claim quota for all newly allocated blocks.*/
4107 ext4_da_update_reserve_space(inode, allocated_clusters,
4108 1);
4109 if (reserved_clusters < allocated_clusters) {
Aditya Kali5356f262011-09-09 19:20:51 -04004110 struct ext4_inode_info *ei = EXT4_I(inode);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004111 int reservation = allocated_clusters -
4112 reserved_clusters;
4113 /*
4114 * It seems we claimed few clusters outside of
4115 * the range of this allocation. We should give
4116 * it back to the reservation pool. This can
4117 * happen in the following case:
4118 *
4119 * * Suppose s_cluster_ratio is 4 (i.e., each
4120 * cluster has 4 blocks. Thus, the clusters
4121 * are [0-3],[4-7],[8-11]...
4122 * * First comes delayed allocation write for
4123 * logical blocks 10 & 11. Since there were no
4124 * previous delayed allocated blocks in the
4125 * range [8-11], we would reserve 1 cluster
4126 * for this write.
4127 * * Next comes write for logical blocks 3 to 8.
4128 * In this case, we will reserve 2 clusters
4129 * (for [0-3] and [4-7]; and not for [8-11] as
4130 * that range has a delayed allocated blocks.
4131 * Thus total reserved clusters now becomes 3.
4132 * * Now, during the delayed allocation writeout
4133 * time, we will first write blocks [3-8] and
4134 * allocate 3 clusters for writing these
4135 * blocks. Also, we would claim all these
4136 * three clusters above.
4137 * * Now when we come here to writeout the
4138 * blocks [10-11], we would expect to claim
4139 * the reservation of 1 cluster we had made
4140 * (and we would claim it since there are no
4141 * more delayed allocated blocks in the range
4142 * [8-11]. But our reserved cluster count had
4143 * already gone to 0.
4144 *
4145 * Thus, at the step 4 above when we determine
4146 * that there are still some unwritten delayed
4147 * allocated blocks outside of our current
4148 * block range, we should increment the
4149 * reserved clusters count so that when the
4150 * remaining blocks finally gets written, we
4151 * could claim them.
4152 */
Aditya Kali5356f262011-09-09 19:20:51 -04004153 dquot_reserve_block(inode,
4154 EXT4_C2B(sbi, reservation));
4155 spin_lock(&ei->i_block_reservation_lock);
4156 ei->i_reserved_data_blocks += reservation;
4157 spin_unlock(&ei->i_block_reservation_lock);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004158 }
4159 }
4160 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004161
4162 /*
Jan Karab436b9b2009-12-08 23:51:10 -05004163 * Cache the extent and update transaction to commit on fdatasync only
4164 * when it is _not_ an uninitialized extent.
4165 */
4166 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05004167 ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock);
Jan Karab436b9b2009-12-08 23:51:10 -05004168 ext4_update_inode_fsync_trans(handle, inode, 1);
4169 } else
4170 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004171out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004172 if (allocated > map->m_len)
4173 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004174 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004175 map->m_flags |= EXT4_MAP_MAPPED;
4176 map->m_pblk = newblock;
4177 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004178out2:
4179 if (path) {
4180 ext4_ext_drop_refs(path);
4181 kfree(path);
4182 }
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004183 trace_ext4_ext_map_blocks_exit(inode, map->m_lblk,
4184 newblock, map->m_len, err ? err : allocated);
Allison Hendersone8613042011-05-25 07:41:46 -04004185
4186 result = (flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) ?
4187 punched_out : allocated;
4188
4189 return err ? err : result;
Alex Tomasa86c6182006-10-11 01:21:03 -07004190}
4191
Jan Karacf108bc2008-07-11 19:27:31 -04004192void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004193{
4194 struct address_space *mapping = inode->i_mapping;
4195 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004196 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004197 handle_t *handle;
Allison Henderson189e8682011-09-06 21:49:44 -04004198 loff_t page_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004199 int err = 0;
4200
4201 /*
Jiaying Zhang3889fd52011-01-10 12:47:05 -05004202 * finish any pending end_io work so we won't run the risk of
4203 * converting any truncated blocks to initialized later
4204 */
4205 ext4_flush_completed_IO(inode);
4206
4207 /*
Alex Tomasa86c6182006-10-11 01:21:03 -07004208 * probably first extent we're gonna free will be last in block
4209 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04004210 err = ext4_writepage_trans_blocks(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07004211 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04004212 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07004213 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07004214
Allison Henderson189e8682011-09-06 21:49:44 -04004215 if (inode->i_size % PAGE_CACHE_SIZE != 0) {
4216 page_len = PAGE_CACHE_SIZE -
4217 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4218
4219 err = ext4_discard_partial_page_buffers(handle,
4220 mapping, inode->i_size, page_len, 0);
4221
4222 if (err)
4223 goto out_stop;
4224 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004225
Jan Kara9ddfc3d2008-07-11 19:27:31 -04004226 if (ext4_orphan_add(handle, inode))
4227 goto out_stop;
4228
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004229 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07004230 ext4_ext_invalidate_cache(inode);
4231
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004232 ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05004233
Alex Tomasa86c6182006-10-11 01:21:03 -07004234 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004235 * TODO: optimization is possible here.
4236 * Probably we need not scan at all,
4237 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004238 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004239
4240 /* we have to know where to truncate from in crash case */
4241 EXT4_I(inode)->i_disksize = inode->i_size;
4242 ext4_mark_inode_dirty(handle, inode);
4243
4244 last_block = (inode->i_size + sb->s_blocksize - 1)
4245 >> EXT4_BLOCK_SIZE_BITS(sb);
Allison Hendersonc6a03712011-07-17 23:21:03 -04004246 err = ext4_ext_remove_space(inode, last_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07004247
4248 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04004249 * transaction synchronous.
4250 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004251 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05004252 ext4_handle_sync(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07004253
Jan Kara9ddfc3d2008-07-11 19:27:31 -04004254 up_write(&EXT4_I(inode)->i_data_sem);
Eric Gouriouf6d2f6b2011-05-22 21:33:00 -04004255
4256out_stop:
Alex Tomasa86c6182006-10-11 01:21:03 -07004257 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004258 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07004259 * then we need to clear up the orphan record which we created above.
4260 * However, if this was a real unlink then we were called by
4261 * ext4_delete_inode(), and we allow that function to clean up the
4262 * orphan info for us.
4263 */
4264 if (inode->i_nlink)
4265 ext4_orphan_del(handle, inode);
4266
Solofo Ramangalahyef737722008-04-29 22:00:41 -04004267 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4268 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07004269 ext4_journal_stop(handle);
4270}
4271
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004272static void ext4_falloc_update_inode(struct inode *inode,
4273 int mode, loff_t new_size, int update_ctime)
4274{
4275 struct timespec now;
4276
4277 if (update_ctime) {
4278 now = current_fs_time(inode->i_sb);
4279 if (!timespec_equal(&inode->i_ctime, &now))
4280 inode->i_ctime = now;
4281 }
4282 /*
4283 * Update only when preallocation was requested beyond
4284 * the file size.
4285 */
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04004286 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
4287 if (new_size > i_size_read(inode))
4288 i_size_write(inode, new_size);
4289 if (new_size > EXT4_I(inode)->i_disksize)
4290 ext4_update_i_disksize(inode, new_size);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004291 } else {
4292 /*
4293 * Mark that we allocate beyond EOF so the subsequent truncate
4294 * can proceed even if the new size is the same as i_size.
4295 */
4296 if (new_size > i_size_read(inode))
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004297 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004298 }
4299
4300}
4301
Amit Aroraa2df2a62007-07-17 21:42:41 -04004302/*
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004303 * preallocate space for a file. This implements ext4's fallocate file
Amit Aroraa2df2a62007-07-17 21:42:41 -04004304 * operation, which gets called from sys_fallocate system call.
4305 * For block-mapped files, posix_fallocate should fall back to the method
4306 * of writing zeroes to the required new blocks (the same behavior which is
4307 * expected for file systems which do not support fallocate() system call).
4308 */
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004309long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004310{
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004311 struct inode *inode = file->f_path.dentry->d_inode;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004312 handle_t *handle;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004313 loff_t new_size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004314 unsigned int max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004315 int ret = 0;
4316 int ret2 = 0;
4317 int retries = 0;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004318 int flags;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004319 struct ext4_map_blocks map;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004320 unsigned int credits, blkbits = inode->i_blkbits;
4321
4322 /*
4323 * currently supporting (pre)allocate mode for extent-based
4324 * files _only_
4325 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004326 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Amit Aroraa2df2a62007-07-17 21:42:41 -04004327 return -EOPNOTSUPP;
4328
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004329 /* Return error if mode is not supported */
4330 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
4331 return -EOPNOTSUPP;
4332
4333 if (mode & FALLOC_FL_PUNCH_HOLE)
4334 return ext4_punch_hole(file, offset, len);
4335
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004336 trace_ext4_fallocate_enter(inode, offset, len, mode);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004337 map.m_lblk = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004338 /*
4339 * We can't just convert len to max_blocks because
4340 * If blocksize = 4096 offset = 3072 and len = 2048
4341 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004342 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004343 - map.m_lblk;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004344 /*
Mingming Caof3bd1f32008-08-19 22:16:03 -04004345 * credits to insert 1 extent into extent tree
Amit Aroraa2df2a62007-07-17 21:42:41 -04004346 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04004347 credits = ext4_chunk_trans_blocks(inode, max_blocks);
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05004348 mutex_lock(&inode->i_mutex);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04004349 ret = inode_newsize_ok(inode, (len + offset));
4350 if (ret) {
4351 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004352 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04004353 return ret;
4354 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004355 flags = EXT4_GET_BLOCKS_CREATE_UNINIT_EXT |
4356 EXT4_GET_BLOCKS_NO_NORMALIZE;
4357 if (mode & FALLOC_FL_KEEP_SIZE)
4358 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004359retry:
4360 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004361 map.m_lblk = map.m_lblk + ret;
4362 map.m_len = max_blocks = max_blocks - ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004363 handle = ext4_journal_start(inode, credits);
4364 if (IS_ERR(handle)) {
4365 ret = PTR_ERR(handle);
4366 break;
4367 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004368 ret = ext4_map_blocks(handle, inode, &map, flags);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05004369 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004370#ifdef EXT4FS_DEBUG
4371 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004372 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004373 "returned error inode#%lu, block=%u, "
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05004374 "max_blocks=%u", __func__,
Kazuya Mioa6371b62010-10-27 21:30:15 -04004375 inode->i_ino, map.m_lblk, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004376#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04004377 ext4_mark_inode_dirty(handle, inode);
4378 ret2 = ext4_journal_stop(handle);
4379 break;
4380 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004381 if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004382 blkbits) >> blkbits))
4383 new_size = offset + len;
4384 else
Utako Kusaka29ae07b2011-07-27 22:11:20 -04004385 new_size = ((loff_t) map.m_lblk + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004386
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004387 ext4_falloc_update_inode(inode, mode, new_size,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004388 (map.m_flags & EXT4_MAP_NEW));
Amit Aroraa2df2a62007-07-17 21:42:41 -04004389 ext4_mark_inode_dirty(handle, inode);
4390 ret2 = ext4_journal_stop(handle);
4391 if (ret2)
4392 break;
4393 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004394 if (ret == -ENOSPC &&
4395 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4396 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004397 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004398 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05004399 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004400 trace_ext4_fallocate_exit(inode, offset, max_blocks,
4401 ret > 0 ? ret2 : ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004402 return ret > 0 ? ret2 : ret;
4403}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004404
4405/*
Mingming Cao00314622009-09-28 15:49:08 -04004406 * This function convert a range of blocks to written extents
4407 * The caller of this function will pass the start offset and the size.
4408 * all unwritten extents within this range will be converted to
4409 * written extents.
4410 *
4411 * This function is called from the direct IO end io call back
4412 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05004413 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04004414 */
4415int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
Eric Sandeena1de02d2010-02-04 23:58:38 -05004416 ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04004417{
4418 handle_t *handle;
Mingming Cao00314622009-09-28 15:49:08 -04004419 unsigned int max_blocks;
4420 int ret = 0;
4421 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004422 struct ext4_map_blocks map;
Mingming Cao00314622009-09-28 15:49:08 -04004423 unsigned int credits, blkbits = inode->i_blkbits;
4424
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004425 map.m_lblk = offset >> blkbits;
Mingming Cao00314622009-09-28 15:49:08 -04004426 /*
4427 * We can't just convert len to max_blocks because
4428 * If blocksize = 4096 offset = 3072 and len = 2048
4429 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004430 max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
4431 map.m_lblk);
Mingming Cao00314622009-09-28 15:49:08 -04004432 /*
4433 * credits to insert 1 extent into extent tree
4434 */
4435 credits = ext4_chunk_trans_blocks(inode, max_blocks);
4436 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004437 map.m_lblk += ret;
4438 map.m_len = (max_blocks -= ret);
Mingming Cao00314622009-09-28 15:49:08 -04004439 handle = ext4_journal_start(inode, credits);
4440 if (IS_ERR(handle)) {
4441 ret = PTR_ERR(handle);
4442 break;
4443 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004444 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004445 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Mingming Cao00314622009-09-28 15:49:08 -04004446 if (ret <= 0) {
4447 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004448 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Mingming Cao00314622009-09-28 15:49:08 -04004449 "returned error inode#%lu, block=%u, "
4450 "max_blocks=%u", __func__,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004451 inode->i_ino, map.m_lblk, map.m_len);
Mingming Cao00314622009-09-28 15:49:08 -04004452 }
4453 ext4_mark_inode_dirty(handle, inode);
4454 ret2 = ext4_journal_stop(handle);
4455 if (ret <= 0 || ret2 )
4456 break;
4457 }
4458 return ret > 0 ? ret2 : ret;
4459}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004460
Mingming Cao00314622009-09-28 15:49:08 -04004461/*
Eric Sandeen6873fa02008-10-07 00:46:36 -04004462 * Callback function called for each extent to gather FIEMAP information.
4463 */
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04004464static int ext4_ext_fiemap_cb(struct inode *inode, ext4_lblk_t next,
Eric Sandeen6873fa02008-10-07 00:46:36 -04004465 struct ext4_ext_cache *newex, struct ext4_extent *ex,
4466 void *data)
4467{
Eric Sandeen6873fa02008-10-07 00:46:36 -04004468 __u64 logical;
4469 __u64 physical;
4470 __u64 length;
4471 __u32 flags = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004472 int ret = 0;
4473 struct fiemap_extent_info *fieinfo = data;
4474 unsigned char blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004475
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004476 blksize_bits = inode->i_sb->s_blocksize_bits;
4477 logical = (__u64)newex->ec_block << blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004478
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05004479 if (newex->ec_start == 0) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004480 /*
4481 * No extent in extent-tree contains block @newex->ec_start,
4482 * then the block may stay in 1)a hole or 2)delayed-extent.
4483 *
4484 * Holes or delayed-extents are processed as follows.
4485 * 1. lookup dirty pages with specified range in pagecache.
4486 * If no page is got, then there is no delayed-extent and
4487 * return with EXT_CONTINUE.
4488 * 2. find the 1st mapped buffer,
4489 * 3. check if the mapped buffer is both in the request range
4490 * and a delayed buffer. If not, there is no delayed-extent,
4491 * then return.
4492 * 4. a delayed-extent is found, the extent will be collected.
4493 */
4494 ext4_lblk_t end = 0;
4495 pgoff_t last_offset;
4496 pgoff_t offset;
4497 pgoff_t index;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004498 pgoff_t start_index = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004499 struct page **pages = NULL;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004500 struct buffer_head *bh = NULL;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004501 struct buffer_head *head = NULL;
4502 unsigned int nr_pages = PAGE_SIZE / sizeof(struct page *);
4503
4504 pages = kmalloc(PAGE_SIZE, GFP_KERNEL);
4505 if (pages == NULL)
4506 return -ENOMEM;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004507
4508 offset = logical >> PAGE_SHIFT;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004509repeat:
4510 last_offset = offset;
4511 head = NULL;
4512 ret = find_get_pages_tag(inode->i_mapping, &offset,
4513 PAGECACHE_TAG_DIRTY, nr_pages, pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004514
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004515 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4516 /* First time, try to find a mapped buffer. */
4517 if (ret == 0) {
4518out:
4519 for (index = 0; index < ret; index++)
4520 page_cache_release(pages[index]);
4521 /* just a hole. */
4522 kfree(pages);
4523 return EXT_CONTINUE;
4524 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004525 index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004526
Yongqiang Yangb2213492011-05-24 11:36:58 -04004527next_page:
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004528 /* Try to find the 1st mapped buffer. */
Yongqiang Yangb2213492011-05-24 11:36:58 -04004529 end = ((__u64)pages[index]->index << PAGE_SHIFT) >>
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004530 blksize_bits;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004531 if (!page_has_buffers(pages[index]))
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004532 goto out;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004533 head = page_buffers(pages[index]);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004534 if (!head)
4535 goto out;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004536
Yongqiang Yangb2213492011-05-24 11:36:58 -04004537 index++;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004538 bh = head;
4539 do {
Yongqiang Yangb2213492011-05-24 11:36:58 -04004540 if (end >= newex->ec_block +
4541 newex->ec_len)
4542 /* The buffer is out of
4543 * the request range.
4544 */
4545 goto out;
4546
4547 if (buffer_mapped(bh) &&
4548 end >= newex->ec_block) {
4549 start_index = index - 1;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004550 /* get the 1st mapped buffer. */
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004551 goto found_mapped_buffer;
4552 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004553
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004554 bh = bh->b_this_page;
4555 end++;
4556 } while (bh != head);
4557
Yongqiang Yangb2213492011-05-24 11:36:58 -04004558 /* No mapped buffer in the range found in this page,
4559 * We need to look up next page.
4560 */
4561 if (index >= ret) {
4562 /* There is no page left, but we need to limit
4563 * newex->ec_len.
4564 */
4565 newex->ec_len = end - newex->ec_block;
4566 goto out;
4567 }
4568 goto next_page;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004569 } else {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004570 /*Find contiguous delayed buffers. */
4571 if (ret > 0 && pages[0]->index == last_offset)
4572 head = page_buffers(pages[0]);
4573 bh = head;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004574 index = 1;
4575 start_index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004576 }
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004577
4578found_mapped_buffer:
4579 if (bh != NULL && buffer_delay(bh)) {
4580 /* 1st or contiguous delayed buffer found. */
4581 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4582 /*
4583 * 1st delayed buffer found, record
4584 * the start of extent.
4585 */
4586 flags |= FIEMAP_EXTENT_DELALLOC;
4587 newex->ec_block = end;
4588 logical = (__u64)end << blksize_bits;
4589 }
4590 /* Find contiguous delayed buffers. */
4591 do {
4592 if (!buffer_delay(bh))
4593 goto found_delayed_extent;
4594 bh = bh->b_this_page;
4595 end++;
4596 } while (bh != head);
4597
Yongqiang Yangb2213492011-05-24 11:36:58 -04004598 for (; index < ret; index++) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004599 if (!page_has_buffers(pages[index])) {
4600 bh = NULL;
4601 break;
4602 }
4603 head = page_buffers(pages[index]);
4604 if (!head) {
4605 bh = NULL;
4606 break;
4607 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004608
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004609 if (pages[index]->index !=
Yongqiang Yangb2213492011-05-24 11:36:58 -04004610 pages[start_index]->index + index
4611 - start_index) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004612 /* Blocks are not contiguous. */
4613 bh = NULL;
4614 break;
4615 }
4616 bh = head;
4617 do {
4618 if (!buffer_delay(bh))
4619 /* Delayed-extent ends. */
4620 goto found_delayed_extent;
4621 bh = bh->b_this_page;
4622 end++;
4623 } while (bh != head);
4624 }
4625 } else if (!(flags & FIEMAP_EXTENT_DELALLOC))
4626 /* a hole found. */
4627 goto out;
4628
4629found_delayed_extent:
4630 newex->ec_len = min(end - newex->ec_block,
4631 (ext4_lblk_t)EXT_INIT_MAX_LEN);
4632 if (ret == nr_pages && bh != NULL &&
4633 newex->ec_len < EXT_INIT_MAX_LEN &&
4634 buffer_delay(bh)) {
4635 /* Have not collected an extent and continue. */
4636 for (index = 0; index < ret; index++)
4637 page_cache_release(pages[index]);
4638 goto repeat;
4639 }
4640
4641 for (index = 0; index < ret; index++)
4642 page_cache_release(pages[index]);
4643 kfree(pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004644 }
4645
4646 physical = (__u64)newex->ec_start << blksize_bits;
4647 length = (__u64)newex->ec_len << blksize_bits;
4648
4649 if (ex && ext4_ext_is_uninitialized(ex))
4650 flags |= FIEMAP_EXTENT_UNWRITTEN;
4651
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04004652 if (next == EXT_MAX_BLOCKS)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004653 flags |= FIEMAP_EXTENT_LAST;
4654
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004655 ret = fiemap_fill_next_extent(fieinfo, logical, physical,
Eric Sandeen6873fa02008-10-07 00:46:36 -04004656 length, flags);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004657 if (ret < 0)
4658 return ret;
4659 if (ret == 1)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004660 return EXT_BREAK;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004661 return EXT_CONTINUE;
4662}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004663/* fiemap flags we can handle specified here */
4664#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
4665
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004666static int ext4_xattr_fiemap(struct inode *inode,
4667 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004668{
4669 __u64 physical = 0;
4670 __u64 length;
4671 __u32 flags = FIEMAP_EXTENT_LAST;
4672 int blockbits = inode->i_sb->s_blocksize_bits;
4673 int error = 0;
4674
4675 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004676 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04004677 struct ext4_iloc iloc;
4678 int offset; /* offset of xattr in inode */
4679
4680 error = ext4_get_inode_loc(inode, &iloc);
4681 if (error)
4682 return error;
4683 physical = iloc.bh->b_blocknr << blockbits;
4684 offset = EXT4_GOOD_OLD_INODE_SIZE +
4685 EXT4_I(inode)->i_extra_isize;
4686 physical += offset;
4687 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
4688 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004689 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004690 } else { /* external block */
4691 physical = EXT4_I(inode)->i_file_acl << blockbits;
4692 length = inode->i_sb->s_blocksize;
4693 }
4694
4695 if (physical)
4696 error = fiemap_fill_next_extent(fieinfo, 0, physical,
4697 length, flags);
4698 return (error < 0 ? error : 0);
4699}
4700
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004701/*
4702 * ext4_ext_punch_hole
4703 *
4704 * Punches a hole of "length" bytes in a file starting
4705 * at byte "offset"
4706 *
4707 * @inode: The inode of the file to punch a hole in
4708 * @offset: The starting byte offset of the hole
4709 * @length: The length of the hole
4710 *
4711 * Returns the number of blocks removed or negative on err
4712 */
4713int ext4_ext_punch_hole(struct file *file, loff_t offset, loff_t length)
4714{
4715 struct inode *inode = file->f_path.dentry->d_inode;
4716 struct super_block *sb = inode->i_sb;
4717 struct ext4_ext_cache cache_ex;
4718 ext4_lblk_t first_block, last_block, num_blocks, iblock, max_blocks;
4719 struct address_space *mapping = inode->i_mapping;
4720 struct ext4_map_blocks map;
4721 handle_t *handle;
Allison Hendersonba062082011-09-03 11:55:59 -04004722 loff_t first_page, last_page, page_len;
4723 loff_t first_page_offset, last_page_offset;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004724 int ret, credits, blocks_released, err = 0;
4725
Allison Henderson2be47512011-09-03 11:56:52 -04004726 /* No need to punch hole beyond i_size */
4727 if (offset >= inode->i_size)
4728 return 0;
4729
4730 /*
4731 * If the hole extends beyond i_size, set the hole
4732 * to end after the page that contains i_size
4733 */
4734 if (offset + length > inode->i_size) {
4735 length = inode->i_size +
4736 PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) -
4737 offset;
4738 }
4739
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004740 first_block = (offset + sb->s_blocksize - 1) >>
4741 EXT4_BLOCK_SIZE_BITS(sb);
4742 last_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
4743
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004744 first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
4745 last_page = (offset + length) >> PAGE_CACHE_SHIFT;
4746
4747 first_page_offset = first_page << PAGE_CACHE_SHIFT;
4748 last_page_offset = last_page << PAGE_CACHE_SHIFT;
4749
4750 /*
4751 * Write out all dirty pages to avoid race conditions
4752 * Then release them.
4753 */
4754 if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
4755 err = filemap_write_and_wait_range(mapping,
Allison Henderson2be47512011-09-03 11:56:52 -04004756 offset, offset + length - 1);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004757
Allison Henderson2be47512011-09-03 11:56:52 -04004758 if (err)
4759 return err;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004760 }
4761
4762 /* Now release the pages */
4763 if (last_page_offset > first_page_offset) {
4764 truncate_inode_pages_range(mapping, first_page_offset,
4765 last_page_offset-1);
4766 }
4767
4768 /* finish any pending end_io work */
4769 ext4_flush_completed_IO(inode);
4770
4771 credits = ext4_writepage_trans_blocks(inode);
4772 handle = ext4_journal_start(inode, credits);
4773 if (IS_ERR(handle))
4774 return PTR_ERR(handle);
4775
4776 err = ext4_orphan_add(handle, inode);
4777 if (err)
4778 goto out;
4779
4780 /*
Allison Hendersonba062082011-09-03 11:55:59 -04004781 * Now we need to zero out the non-page-aligned data in the
4782 * pages at the start and tail of the hole, and unmap the buffer
4783 * heads for the block aligned regions of the page that were
4784 * completely zeroed.
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004785 */
Allison Hendersonba062082011-09-03 11:55:59 -04004786 if (first_page > last_page) {
4787 /*
4788 * If the file space being truncated is contained within a page
4789 * just zero out and unmap the middle of that page
4790 */
4791 err = ext4_discard_partial_page_buffers(handle,
4792 mapping, offset, length, 0);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004793
Allison Hendersonba062082011-09-03 11:55:59 -04004794 if (err)
4795 goto out;
4796 } else {
4797 /*
4798 * zero out and unmap the partial page that contains
4799 * the start of the hole
4800 */
4801 page_len = first_page_offset - offset;
4802 if (page_len > 0) {
4803 err = ext4_discard_partial_page_buffers(handle, mapping,
4804 offset, page_len, 0);
4805 if (err)
4806 goto out;
4807 }
4808
4809 /*
4810 * zero out and unmap the partial page that contains
4811 * the end of the hole
4812 */
4813 page_len = offset + length - last_page_offset;
4814 if (page_len > 0) {
4815 err = ext4_discard_partial_page_buffers(handle, mapping,
4816 last_page_offset, page_len, 0);
4817 if (err)
4818 goto out;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004819 }
4820 }
4821
Allison Henderson2be47512011-09-03 11:56:52 -04004822
4823 /*
4824 * If i_size is contained in the last page, we need to
4825 * unmap and zero the partial page after i_size
4826 */
4827 if (inode->i_size >> PAGE_CACHE_SHIFT == last_page &&
4828 inode->i_size % PAGE_CACHE_SIZE != 0) {
4829
4830 page_len = PAGE_CACHE_SIZE -
4831 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4832
4833 if (page_len > 0) {
4834 err = ext4_discard_partial_page_buffers(handle,
4835 mapping, inode->i_size, page_len, 0);
4836
4837 if (err)
4838 goto out;
4839 }
4840 }
4841
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004842 /* If there are no blocks to remove, return now */
4843 if (first_block >= last_block)
4844 goto out;
4845
4846 down_write(&EXT4_I(inode)->i_data_sem);
4847 ext4_ext_invalidate_cache(inode);
4848 ext4_discard_preallocations(inode);
4849
4850 /*
4851 * Loop over all the blocks and identify blocks
4852 * that need to be punched out
4853 */
4854 iblock = first_block;
4855 blocks_released = 0;
4856 while (iblock < last_block) {
4857 max_blocks = last_block - iblock;
4858 num_blocks = 1;
4859 memset(&map, 0, sizeof(map));
4860 map.m_lblk = iblock;
4861 map.m_len = max_blocks;
4862 ret = ext4_ext_map_blocks(handle, inode, &map,
4863 EXT4_GET_BLOCKS_PUNCH_OUT_EXT);
4864
4865 if (ret > 0) {
4866 blocks_released += ret;
4867 num_blocks = ret;
4868 } else if (ret == 0) {
4869 /*
4870 * If map blocks could not find the block,
4871 * then it is in a hole. If the hole was
4872 * not already cached, then map blocks should
4873 * put it in the cache. So we can get the hole
4874 * out of the cache
4875 */
4876 memset(&cache_ex, 0, sizeof(cache_ex));
4877 if ((ext4_ext_check_cache(inode, iblock, &cache_ex)) &&
4878 !cache_ex.ec_start) {
4879
4880 /* The hole is cached */
4881 num_blocks = cache_ex.ec_block +
4882 cache_ex.ec_len - iblock;
4883
4884 } else {
4885 /* The block could not be identified */
4886 err = -EIO;
4887 break;
4888 }
4889 } else {
4890 /* Map blocks error */
4891 err = ret;
4892 break;
4893 }
4894
4895 if (num_blocks == 0) {
4896 /* This condition should never happen */
4897 ext_debug("Block lookup failed");
4898 err = -EIO;
4899 break;
4900 }
4901
4902 iblock += num_blocks;
4903 }
4904
4905 if (blocks_released > 0) {
4906 ext4_ext_invalidate_cache(inode);
4907 ext4_discard_preallocations(inode);
4908 }
4909
4910 if (IS_SYNC(inode))
4911 ext4_handle_sync(handle);
4912
4913 up_write(&EXT4_I(inode)->i_data_sem);
4914
4915out:
4916 ext4_orphan_del(handle, inode);
4917 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4918 ext4_mark_inode_dirty(handle, inode);
4919 ext4_journal_stop(handle);
4920 return err;
4921}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004922int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4923 __u64 start, __u64 len)
4924{
4925 ext4_lblk_t start_blk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004926 int error = 0;
4927
4928 /* fallback to generic here if not in extents fmt */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004929 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeen6873fa02008-10-07 00:46:36 -04004930 return generic_block_fiemap(inode, fieinfo, start, len,
4931 ext4_get_block);
4932
4933 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
4934 return -EBADR;
4935
4936 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
4937 error = ext4_xattr_fiemap(inode, fieinfo);
4938 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004939 ext4_lblk_t len_blks;
4940 __u64 last_blk;
4941
Eric Sandeen6873fa02008-10-07 00:46:36 -04004942 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004943 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04004944 if (last_blk >= EXT_MAX_BLOCKS)
4945 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004946 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004947
4948 /*
4949 * Walk the extent tree gathering extent information.
4950 * ext4_ext_fiemap_cb will push extents back to user.
4951 */
Eric Sandeen6873fa02008-10-07 00:46:36 -04004952 error = ext4_ext_walk_space(inode, start_blk, len_blks,
4953 ext4_ext_fiemap_cb, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004954 }
4955
4956 return error;
4957}