blob: 2fc6cc0a51ca71b2b199accb993405f270bea508 [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 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
758 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
759 /* insert after */
760 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
761 len = (len - 1) * sizeof(struct ext4_extent_idx);
762 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400763 ext_debug("insert new index %d after: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700764 "move %d from 0x%p to 0x%p\n",
765 logical, ptr, len,
766 (curp->p_idx + 1), (curp->p_idx + 2));
767 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
768 }
769 ix = curp->p_idx + 1;
770 } else {
771 /* insert before */
772 len = len * sizeof(struct ext4_extent_idx);
773 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400774 ext_debug("insert new index %d before: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700775 "move %d from 0x%p to 0x%p\n",
776 logical, ptr, len,
777 curp->p_idx, (curp->p_idx + 1));
778 memmove(curp->p_idx + 1, curp->p_idx, len);
779 ix = curp->p_idx;
780 }
781
Tao Maf472e022011-10-17 10:13:46 -0400782 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
783 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
784 return -EIO;
785 }
786
Alex Tomasa86c6182006-10-11 01:21:03 -0700787 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700788 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400789 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700790
Frank Mayhar273df552010-03-02 11:46:09 -0500791 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
792 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
793 return -EIO;
794 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700795
796 err = ext4_ext_dirty(handle, inode, curp);
797 ext4_std_error(inode->i_sb, err);
798
799 return err;
800}
801
802/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700803 * ext4_ext_split:
804 * inserts new subtree into the path, using free index entry
805 * at depth @at:
806 * - allocates all needed blocks (new leaf and all intermediate index blocks)
807 * - makes decision where to split
808 * - moves remaining extents and index entries (right to the split point)
809 * into the newly allocated blocks
810 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700811 */
812static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -0400813 unsigned int flags,
814 struct ext4_ext_path *path,
815 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -0700816{
817 struct buffer_head *bh = NULL;
818 int depth = ext_depth(inode);
819 struct ext4_extent_header *neh;
820 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -0700821 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700822 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700823 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700824 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700825 int err = 0;
826
827 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700828 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700829
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700830 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700831 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -0500832 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
833 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
834 return -EIO;
835 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700836 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
837 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700838 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700839 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400840 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700841 } else {
842 border = newext->ee_block;
843 ext_debug("leaf will be added."
844 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400845 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700846 }
847
848 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700849 * If error occurs, then we break processing
850 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700851 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700852 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700853 */
854
855 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700856 * Get array to track all allocated blocks.
857 * We need this to handle errors and free blocks
858 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700859 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800860 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700861 if (!ablocks)
862 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700863
864 /* allocate all needed blocks */
865 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
866 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400867 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400868 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -0700869 if (newblock == 0)
870 goto cleanup;
871 ablocks[a] = newblock;
872 }
873
874 /* initialize new leaf */
875 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -0500876 if (unlikely(newblock == 0)) {
877 EXT4_ERROR_INODE(inode, "newblock == 0!");
878 err = -EIO;
879 goto cleanup;
880 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700881 bh = sb_getblk(inode->i_sb, newblock);
882 if (!bh) {
883 err = -EIO;
884 goto cleanup;
885 }
886 lock_buffer(bh);
887
Avantika Mathur7e028972006-12-06 20:41:33 -0800888 err = ext4_journal_get_create_access(handle, bh);
889 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700890 goto cleanup;
891
892 neh = ext_block_hdr(bh);
893 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400894 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700895 neh->eh_magic = EXT4_EXT_MAGIC;
896 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700897
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700898 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -0500899 if (unlikely(path[depth].p_hdr->eh_entries !=
900 path[depth].p_hdr->eh_max)) {
901 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
902 path[depth].p_hdr->eh_entries,
903 path[depth].p_hdr->eh_max);
904 err = -EIO;
905 goto cleanup;
906 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700907 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400908 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
909 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -0700910 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400911 struct ext4_extent *ex;
912 ex = EXT_FIRST_EXTENT(neh);
913 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400914 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700915 }
916
917 set_buffer_uptodate(bh);
918 unlock_buffer(bh);
919
Frank Mayhar03901312009-01-07 00:06:22 -0500920 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800921 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700922 goto cleanup;
923 brelse(bh);
924 bh = NULL;
925
926 /* correct old leaf */
927 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800928 err = ext4_ext_get_access(handle, inode, path + depth);
929 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700930 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400931 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800932 err = ext4_ext_dirty(handle, inode, path + depth);
933 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700934 goto cleanup;
935
936 }
937
938 /* create intermediate indexes */
939 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -0500940 if (unlikely(k < 0)) {
941 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
942 err = -EIO;
943 goto cleanup;
944 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700945 if (k)
946 ext_debug("create %d intermediate indices\n", k);
947 /* insert new index into current index block */
948 /* current depth stored in i var */
949 i = depth - 1;
950 while (k--) {
951 oldblock = newblock;
952 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -0500953 bh = sb_getblk(inode->i_sb, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700954 if (!bh) {
955 err = -EIO;
956 goto cleanup;
957 }
958 lock_buffer(bh);
959
Avantika Mathur7e028972006-12-06 20:41:33 -0800960 err = ext4_journal_get_create_access(handle, bh);
961 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700962 goto cleanup;
963
964 neh = ext_block_hdr(bh);
965 neh->eh_entries = cpu_to_le16(1);
966 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400967 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700968 neh->eh_depth = cpu_to_le16(depth - i);
969 fidx = EXT_FIRST_INDEX(neh);
970 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700971 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700972
Eric Sandeenbba90742008-01-28 23:58:27 -0500973 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
974 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700975
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400976 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -0500977 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
978 EXT_LAST_INDEX(path[i].p_hdr))) {
979 EXT4_ERROR_INODE(inode,
980 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
981 le32_to_cpu(path[i].p_ext->ee_block));
982 err = -EIO;
983 goto cleanup;
984 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400985 /* start copy indexes */
986 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
987 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
988 EXT_MAX_INDEX(path[i].p_hdr));
989 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -0700990 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400991 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -0700992 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400993 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700994 }
995 set_buffer_uptodate(bh);
996 unlock_buffer(bh);
997
Frank Mayhar03901312009-01-07 00:06:22 -0500998 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800999 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001000 goto cleanup;
1001 brelse(bh);
1002 bh = NULL;
1003
1004 /* correct old index */
1005 if (m) {
1006 err = ext4_ext_get_access(handle, inode, path + i);
1007 if (err)
1008 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001009 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001010 err = ext4_ext_dirty(handle, inode, path + i);
1011 if (err)
1012 goto cleanup;
1013 }
1014
1015 i--;
1016 }
1017
1018 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001019 err = ext4_ext_insert_index(handle, inode, path + at,
1020 le32_to_cpu(border), newblock);
1021
1022cleanup:
1023 if (bh) {
1024 if (buffer_locked(bh))
1025 unlock_buffer(bh);
1026 brelse(bh);
1027 }
1028
1029 if (err) {
1030 /* free all allocated blocks in error case */
1031 for (i = 0; i < depth; i++) {
1032 if (!ablocks[i])
1033 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001034 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001035 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001036 }
1037 }
1038 kfree(ablocks);
1039
1040 return err;
1041}
1042
1043/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001044 * ext4_ext_grow_indepth:
1045 * implements tree growing procedure:
1046 * - allocates new block
1047 * - moves top-level data (index block or leaf) into the new block
1048 * - initializes new top-level, creating index that points to the
1049 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001050 */
1051static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001052 unsigned int flags,
1053 struct ext4_ext_path *path,
1054 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001055{
1056 struct ext4_ext_path *curp = path;
1057 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001058 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001059 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001060 int err = 0;
1061
Allison Henderson55f020d2011-05-25 07:41:26 -04001062 newblock = ext4_ext_new_meta_block(handle, inode, path,
1063 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001064 if (newblock == 0)
1065 return err;
1066
1067 bh = sb_getblk(inode->i_sb, newblock);
1068 if (!bh) {
1069 err = -EIO;
1070 ext4_std_error(inode->i_sb, err);
1071 return err;
1072 }
1073 lock_buffer(bh);
1074
Avantika Mathur7e028972006-12-06 20:41:33 -08001075 err = ext4_journal_get_create_access(handle, bh);
1076 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001077 unlock_buffer(bh);
1078 goto out;
1079 }
1080
1081 /* move top-level index/leaf into new block */
1082 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
1083
1084 /* set size of new block */
1085 neh = ext_block_hdr(bh);
1086 /* old root could have indexes or leaves
1087 * so calculate e_max right way */
1088 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001089 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001090 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001091 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001092 neh->eh_magic = EXT4_EXT_MAGIC;
1093 set_buffer_uptodate(bh);
1094 unlock_buffer(bh);
1095
Frank Mayhar03901312009-01-07 00:06:22 -05001096 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001097 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001098 goto out;
1099
1100 /* create index in new top-level index: num,max,pointer */
Avantika Mathur7e028972006-12-06 20:41:33 -08001101 err = ext4_ext_get_access(handle, inode, curp);
1102 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001103 goto out;
1104
1105 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001106 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001107 curp->p_hdr->eh_entries = cpu_to_le16(1);
1108 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
Dmitry Monakhove9f410b2007-07-18 09:09:15 -04001109
1110 if (path[0].p_hdr->eh_depth)
1111 curp->p_idx->ei_block =
1112 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
1113 else
1114 curp->p_idx->ei_block =
1115 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001116 ext4_idx_store_pblock(curp->p_idx, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001117
1118 neh = ext_inode_hdr(inode);
Mingming Cao2ae02102006-10-11 01:21:11 -07001119 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001120 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001121 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001122 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001123
1124 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
1125 err = ext4_ext_dirty(handle, inode, curp);
1126out:
1127 brelse(bh);
1128
1129 return err;
1130}
1131
1132/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001133 * ext4_ext_create_new_leaf:
1134 * finds empty index and adds new leaf.
1135 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001136 */
1137static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001138 unsigned int flags,
1139 struct ext4_ext_path *path,
1140 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001141{
1142 struct ext4_ext_path *curp;
1143 int depth, i, err = 0;
1144
1145repeat:
1146 i = depth = ext_depth(inode);
1147
1148 /* walk up to the tree and look for free index entry */
1149 curp = path + depth;
1150 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1151 i--;
1152 curp--;
1153 }
1154
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001155 /* we use already allocated block for index block,
1156 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001157 if (EXT_HAS_FREE_INDEX(curp)) {
1158 /* if we found index with free entry, then use that
1159 * entry: create all needed subtree and add new leaf */
Allison Henderson55f020d2011-05-25 07:41:26 -04001160 err = ext4_ext_split(handle, inode, flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001161 if (err)
1162 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001163
1164 /* refill path */
1165 ext4_ext_drop_refs(path);
1166 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001167 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1168 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001169 if (IS_ERR(path))
1170 err = PTR_ERR(path);
1171 } else {
1172 /* tree is full, time to grow in depth */
Allison Henderson55f020d2011-05-25 07:41:26 -04001173 err = ext4_ext_grow_indepth(handle, inode, flags,
1174 path, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001175 if (err)
1176 goto out;
1177
1178 /* refill path */
1179 ext4_ext_drop_refs(path);
1180 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001181 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1182 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001183 if (IS_ERR(path)) {
1184 err = PTR_ERR(path);
1185 goto out;
1186 }
1187
1188 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001189 * only first (depth 0 -> 1) produces free space;
1190 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001191 */
1192 depth = ext_depth(inode);
1193 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001194 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001195 goto repeat;
1196 }
1197 }
1198
1199out:
1200 return err;
1201}
1202
1203/*
Alex Tomas1988b512008-01-28 23:58:27 -05001204 * search the closest allocated block to the left for *logical
1205 * and returns it at @logical + it's physical address at @phys
1206 * if *logical is the smallest allocated block, the function
1207 * returns 0 at @phys
1208 * return value contains 0 (success) or error code
1209 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001210static int ext4_ext_search_left(struct inode *inode,
1211 struct ext4_ext_path *path,
1212 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001213{
1214 struct ext4_extent_idx *ix;
1215 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001216 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001217
Frank Mayhar273df552010-03-02 11:46:09 -05001218 if (unlikely(path == NULL)) {
1219 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1220 return -EIO;
1221 }
Alex Tomas1988b512008-01-28 23:58:27 -05001222 depth = path->p_depth;
1223 *phys = 0;
1224
1225 if (depth == 0 && path->p_ext == NULL)
1226 return 0;
1227
1228 /* usually extent in the path covers blocks smaller
1229 * then *logical, but it can be that extent is the
1230 * first one in the file */
1231
1232 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001233 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001234 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001235 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1236 EXT4_ERROR_INODE(inode,
1237 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1238 *logical, le32_to_cpu(ex->ee_block));
1239 return -EIO;
1240 }
Alex Tomas1988b512008-01-28 23:58:27 -05001241 while (--depth >= 0) {
1242 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001243 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1244 EXT4_ERROR_INODE(inode,
1245 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
Tao Ma6ee3b212011-10-08 16:08:34 -04001246 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001247 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
Tao Ma6ee3b212011-10-08 16:08:34 -04001248 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001249 depth);
1250 return -EIO;
1251 }
Alex Tomas1988b512008-01-28 23:58:27 -05001252 }
1253 return 0;
1254 }
1255
Frank Mayhar273df552010-03-02 11:46:09 -05001256 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1257 EXT4_ERROR_INODE(inode,
1258 "logical %d < ee_block %d + ee_len %d!",
1259 *logical, le32_to_cpu(ex->ee_block), ee_len);
1260 return -EIO;
1261 }
Alex Tomas1988b512008-01-28 23:58:27 -05001262
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001263 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001264 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001265 return 0;
1266}
1267
1268/*
1269 * search the closest allocated block to the right for *logical
1270 * and returns it at @logical + it's physical address at @phys
Tao Madf3ab172011-10-08 15:53:49 -04001271 * if *logical is the largest allocated block, the function
Alex Tomas1988b512008-01-28 23:58:27 -05001272 * returns 0 at @phys
1273 * return value contains 0 (success) or error code
1274 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001275static int ext4_ext_search_right(struct inode *inode,
1276 struct ext4_ext_path *path,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001277 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1278 struct ext4_extent **ret_ex)
Alex Tomas1988b512008-01-28 23:58:27 -05001279{
1280 struct buffer_head *bh = NULL;
1281 struct ext4_extent_header *eh;
1282 struct ext4_extent_idx *ix;
1283 struct ext4_extent *ex;
1284 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001285 int depth; /* Note, NOT eh_depth; depth from top of tree */
1286 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001287
Frank Mayhar273df552010-03-02 11:46:09 -05001288 if (unlikely(path == NULL)) {
1289 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1290 return -EIO;
1291 }
Alex Tomas1988b512008-01-28 23:58:27 -05001292 depth = path->p_depth;
1293 *phys = 0;
1294
1295 if (depth == 0 && path->p_ext == NULL)
1296 return 0;
1297
1298 /* usually extent in the path covers blocks smaller
1299 * then *logical, but it can be that extent is the
1300 * first one in the file */
1301
1302 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001303 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001304 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001305 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1306 EXT4_ERROR_INODE(inode,
1307 "first_extent(path[%d].p_hdr) != ex",
1308 depth);
1309 return -EIO;
1310 }
Alex Tomas1988b512008-01-28 23:58:27 -05001311 while (--depth >= 0) {
1312 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001313 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1314 EXT4_ERROR_INODE(inode,
1315 "ix != EXT_FIRST_INDEX *logical %d!",
1316 *logical);
1317 return -EIO;
1318 }
Alex Tomas1988b512008-01-28 23:58:27 -05001319 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001320 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001321 }
1322
Frank Mayhar273df552010-03-02 11:46:09 -05001323 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1324 EXT4_ERROR_INODE(inode,
1325 "logical %d < ee_block %d + ee_len %d!",
1326 *logical, le32_to_cpu(ex->ee_block), ee_len);
1327 return -EIO;
1328 }
Alex Tomas1988b512008-01-28 23:58:27 -05001329
1330 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1331 /* next allocated block in this leaf */
1332 ex++;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001333 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001334 }
1335
1336 /* go up and search for index to the right */
1337 while (--depth >= 0) {
1338 ix = path[depth].p_idx;
1339 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001340 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001341 }
1342
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001343 /* we've gone up to the root and found no index to the right */
1344 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001345
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001346got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001347 /* we've found index to the right, let's
1348 * follow it and find the closest allocated
1349 * block to the right */
1350 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001351 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001352 while (++depth < path->p_depth) {
1353 bh = sb_bread(inode->i_sb, block);
1354 if (bh == NULL)
1355 return -EIO;
1356 eh = ext_block_hdr(bh);
Eric Sandeen395a87b2009-03-10 18:18:47 -04001357 /* subtract from p_depth to get proper eh_depth */
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001358 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001359 put_bh(bh);
1360 return -EIO;
1361 }
1362 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001363 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001364 put_bh(bh);
1365 }
1366
1367 bh = sb_bread(inode->i_sb, block);
1368 if (bh == NULL)
1369 return -EIO;
1370 eh = ext_block_hdr(bh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001371 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001372 put_bh(bh);
1373 return -EIO;
1374 }
1375 ex = EXT_FIRST_EXTENT(eh);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001376found_extent:
Alex Tomas1988b512008-01-28 23:58:27 -05001377 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001378 *phys = ext4_ext_pblock(ex);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001379 *ret_ex = ex;
1380 if (bh)
1381 put_bh(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001382 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001383}
1384
1385/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001386 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001387 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001388 * NOTE: it considers block number from index entry as
1389 * allocated block. Thus, index entries have to be consistent
1390 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001391 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001392static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001393ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1394{
1395 int depth;
1396
1397 BUG_ON(path == NULL);
1398 depth = path->p_depth;
1399
1400 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001401 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001402
1403 while (depth >= 0) {
1404 if (depth == path->p_depth) {
1405 /* leaf */
1406 if (path[depth].p_ext !=
1407 EXT_LAST_EXTENT(path[depth].p_hdr))
1408 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1409 } else {
1410 /* index */
1411 if (path[depth].p_idx !=
1412 EXT_LAST_INDEX(path[depth].p_hdr))
1413 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1414 }
1415 depth--;
1416 }
1417
Lukas Czernerf17722f2011-06-06 00:05:17 -04001418 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001419}
1420
1421/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001422 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001423 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001424 */
Robin Dong57187892011-07-23 21:49:07 -04001425static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001426{
1427 int depth;
1428
1429 BUG_ON(path == NULL);
1430 depth = path->p_depth;
1431
1432 /* zero-tree has no leaf blocks at all */
1433 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001434 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001435
1436 /* go to index block */
1437 depth--;
1438
1439 while (depth >= 0) {
1440 if (path[depth].p_idx !=
1441 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001442 return (ext4_lblk_t)
1443 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001444 depth--;
1445 }
1446
Lukas Czernerf17722f2011-06-06 00:05:17 -04001447 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001448}
1449
1450/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001451 * ext4_ext_correct_indexes:
1452 * if leaf gets modified and modified extent is first in the leaf,
1453 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001454 * TODO: do we need to correct tree in all cases?
1455 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001456static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001457 struct ext4_ext_path *path)
1458{
1459 struct ext4_extent_header *eh;
1460 int depth = ext_depth(inode);
1461 struct ext4_extent *ex;
1462 __le32 border;
1463 int k, err = 0;
1464
1465 eh = path[depth].p_hdr;
1466 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001467
1468 if (unlikely(ex == NULL || eh == NULL)) {
1469 EXT4_ERROR_INODE(inode,
1470 "ex %p == NULL or eh %p == NULL", ex, eh);
1471 return -EIO;
1472 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001473
1474 if (depth == 0) {
1475 /* there is no tree at all */
1476 return 0;
1477 }
1478
1479 if (ex != EXT_FIRST_EXTENT(eh)) {
1480 /* we correct tree if first leaf got modified only */
1481 return 0;
1482 }
1483
1484 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001485 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001486 */
1487 k = depth - 1;
1488 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001489 err = ext4_ext_get_access(handle, inode, path + k);
1490 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001491 return err;
1492 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001493 err = ext4_ext_dirty(handle, inode, path + k);
1494 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001495 return err;
1496
1497 while (k--) {
1498 /* change all left-side indexes */
1499 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1500 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001501 err = ext4_ext_get_access(handle, inode, path + k);
1502 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001503 break;
1504 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001505 err = ext4_ext_dirty(handle, inode, path + k);
1506 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001507 break;
1508 }
1509
1510 return err;
1511}
1512
Akira Fujita748de672009-06-17 19:24:03 -04001513int
Alex Tomasa86c6182006-10-11 01:21:03 -07001514ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1515 struct ext4_extent *ex2)
1516{
Amit Arora749269f2007-07-18 09:02:56 -04001517 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001518
1519 /*
1520 * Make sure that either both extents are uninitialized, or
1521 * both are _not_.
1522 */
1523 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1524 return 0;
1525
Amit Arora749269f2007-07-18 09:02:56 -04001526 if (ext4_ext_is_uninitialized(ex1))
1527 max_len = EXT_UNINIT_MAX_LEN;
1528 else
1529 max_len = EXT_INIT_MAX_LEN;
1530
Amit Aroraa2df2a62007-07-17 21:42:41 -04001531 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1532 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1533
1534 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001535 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001536 return 0;
1537
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001538 /*
1539 * To allow future support for preallocated extents to be added
1540 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001541 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001542 */
Amit Arora749269f2007-07-18 09:02:56 -04001543 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001544 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001545#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001546 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001547 return 0;
1548#endif
1549
Theodore Ts'obf89d162010-10-27 21:30:14 -04001550 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001551 return 1;
1552 return 0;
1553}
1554
1555/*
Amit Arora56055d32007-07-17 21:42:38 -04001556 * This function tries to merge the "ex" extent to the next extent in the tree.
1557 * It always tries to merge towards right. If you want to merge towards
1558 * left, pass "ex - 1" as argument instead of "ex".
1559 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1560 * 1 if they got merged.
1561 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001562static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001563 struct ext4_ext_path *path,
1564 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001565{
1566 struct ext4_extent_header *eh;
1567 unsigned int depth, len;
1568 int merge_done = 0;
1569 int uninitialized = 0;
1570
1571 depth = ext_depth(inode);
1572 BUG_ON(path[depth].p_hdr == NULL);
1573 eh = path[depth].p_hdr;
1574
1575 while (ex < EXT_LAST_EXTENT(eh)) {
1576 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1577 break;
1578 /* merge with next extent! */
1579 if (ext4_ext_is_uninitialized(ex))
1580 uninitialized = 1;
1581 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1582 + ext4_ext_get_actual_len(ex + 1));
1583 if (uninitialized)
1584 ext4_ext_mark_uninitialized(ex);
1585
1586 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1587 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1588 * sizeof(struct ext4_extent);
1589 memmove(ex + 1, ex + 2, len);
1590 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001591 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001592 merge_done = 1;
1593 WARN_ON(eh->eh_entries == 0);
1594 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001595 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001596 }
1597
1598 return merge_done;
1599}
1600
1601/*
Yongqiang Yang197217a2011-05-03 11:45:29 -04001602 * This function tries to merge the @ex extent to neighbours in the tree.
1603 * return 1 if merge left else 0.
1604 */
1605static int ext4_ext_try_to_merge(struct inode *inode,
1606 struct ext4_ext_path *path,
1607 struct ext4_extent *ex) {
1608 struct ext4_extent_header *eh;
1609 unsigned int depth;
1610 int merge_done = 0;
1611 int ret = 0;
1612
1613 depth = ext_depth(inode);
1614 BUG_ON(path[depth].p_hdr == NULL);
1615 eh = path[depth].p_hdr;
1616
1617 if (ex > EXT_FIRST_EXTENT(eh))
1618 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1619
1620 if (!merge_done)
1621 ret = ext4_ext_try_to_merge_right(inode, path, ex);
1622
1623 return ret;
1624}
1625
1626/*
Amit Arora25d14f92007-05-24 13:04:13 -04001627 * check if a portion of the "newext" extent overlaps with an
1628 * existing extent.
1629 *
1630 * If there is an overlap discovered, it updates the length of the newext
1631 * such that there will be no overlap, and then returns 1.
1632 * If there is no overlap found, it returns 0.
1633 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001634static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1635 struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001636 struct ext4_extent *newext,
1637 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001638{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001639 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001640 unsigned int depth, len1;
1641 unsigned int ret = 0;
1642
1643 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001644 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001645 depth = ext_depth(inode);
1646 if (!path[depth].p_ext)
1647 goto out;
1648 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001649 b2 &= ~(sbi->s_cluster_ratio - 1);
Amit Arora25d14f92007-05-24 13:04:13 -04001650
1651 /*
1652 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001653 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001654 */
1655 if (b2 < b1) {
1656 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001657 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001658 goto out;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001659 b2 &= ~(sbi->s_cluster_ratio - 1);
Amit Arora25d14f92007-05-24 13:04:13 -04001660 }
1661
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001662 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001663 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001664 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001665 newext->ee_len = cpu_to_le16(len1);
1666 ret = 1;
1667 }
1668
1669 /* check for overlap */
1670 if (b1 + len1 > b2) {
1671 newext->ee_len = cpu_to_le16(b2 - b1);
1672 ret = 1;
1673 }
1674out:
1675 return ret;
1676}
1677
1678/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001679 * ext4_ext_insert_extent:
1680 * tries to merge requsted extent into the existing extent or
1681 * inserts requested extent as new one into the tree,
1682 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001683 */
1684int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1685 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04001686 struct ext4_extent *newext, int flag)
Alex Tomasa86c6182006-10-11 01:21:03 -07001687{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001688 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001689 struct ext4_extent *ex, *fex;
1690 struct ext4_extent *nearex; /* nearest extent */
1691 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001692 int depth, len, err;
1693 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001694 unsigned uninitialized = 0;
Allison Henderson55f020d2011-05-25 07:41:26 -04001695 int flags = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001696
Frank Mayhar273df552010-03-02 11:46:09 -05001697 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1698 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1699 return -EIO;
1700 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001701 depth = ext_depth(inode);
1702 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001703 if (unlikely(path[depth].p_hdr == NULL)) {
1704 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1705 return -EIO;
1706 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001707
1708 /* try to insert block into found extent and return */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001709 if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
Mingming Cao00314622009-09-28 15:49:08 -04001710 && ext4_can_extents_be_merged(inode, ex, newext)) {
Mingming553f9002009-09-18 13:34:55 -04001711 ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04001712 ext4_ext_is_uninitialized(newext),
1713 ext4_ext_get_actual_len(newext),
1714 le32_to_cpu(ex->ee_block),
1715 ext4_ext_is_uninitialized(ex),
1716 ext4_ext_get_actual_len(ex),
1717 ext4_ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001718 err = ext4_ext_get_access(handle, inode, path + depth);
1719 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001720 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001721
1722 /*
1723 * ext4_can_extents_be_merged should have checked that either
1724 * both extents are uninitialized, or both aren't. Thus we
1725 * need to check only one of them here.
1726 */
1727 if (ext4_ext_is_uninitialized(ex))
1728 uninitialized = 1;
1729 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1730 + ext4_ext_get_actual_len(newext));
1731 if (uninitialized)
1732 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001733 eh = path[depth].p_hdr;
1734 nearex = ex;
1735 goto merge;
1736 }
1737
Alex Tomasa86c6182006-10-11 01:21:03 -07001738 depth = ext_depth(inode);
1739 eh = path[depth].p_hdr;
1740 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1741 goto has_space;
1742
1743 /* probably next leaf has space for us? */
1744 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04001745 next = EXT_MAX_BLOCKS;
1746 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
Robin Dong57187892011-07-23 21:49:07 -04001747 next = ext4_ext_next_leaf_block(path);
Robin Dong598dbdf2011-07-11 18:24:01 -04001748 if (next != EXT_MAX_BLOCKS) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001749 ext_debug("next leaf block - %d\n", next);
1750 BUG_ON(npath != NULL);
1751 npath = ext4_ext_find_extent(inode, next, NULL);
1752 if (IS_ERR(npath))
1753 return PTR_ERR(npath);
1754 BUG_ON(npath->p_depth != path->p_depth);
1755 eh = npath[depth].p_hdr;
1756 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001757 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001758 le16_to_cpu(eh->eh_entries));
1759 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04001760 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07001761 }
1762 ext_debug("next leaf has no free space(%d,%d)\n",
1763 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1764 }
1765
1766 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001767 * There is no free space in the found leaf.
1768 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001769 */
Allison Henderson55f020d2011-05-25 07:41:26 -04001770 if (flag & EXT4_GET_BLOCKS_PUNCH_OUT_EXT)
1771 flags = EXT4_MB_USE_ROOT_BLOCKS;
1772 err = ext4_ext_create_new_leaf(handle, inode, flags, path, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001773 if (err)
1774 goto cleanup;
1775 depth = ext_depth(inode);
1776 eh = path[depth].p_hdr;
1777
1778has_space:
1779 nearex = path[depth].p_ext;
1780
Avantika Mathur7e028972006-12-06 20:41:33 -08001781 err = ext4_ext_get_access(handle, inode, path + depth);
1782 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001783 goto cleanup;
1784
1785 if (!nearex) {
1786 /* there is no extent in this leaf, create first one */
Mingming553f9002009-09-18 13:34:55 -04001787 ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001788 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001789 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001790 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001791 ext4_ext_get_actual_len(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001792 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1793 } else if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001794 > le32_to_cpu(nearex->ee_block)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001795/* BUG_ON(newext->ee_block == nearex->ee_block); */
1796 if (nearex != EXT_LAST_EXTENT(eh)) {
1797 len = EXT_MAX_EXTENT(eh) - nearex;
1798 len = (len - 1) * sizeof(struct ext4_extent);
1799 len = len < 0 ? 0 : len;
Mingming553f9002009-09-18 13:34:55 -04001800 ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001801 "move %d from 0x%p to 0x%p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001802 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001803 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001804 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001805 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001806 nearex, len, nearex + 1, nearex + 2);
1807 memmove(nearex + 2, nearex + 1, len);
1808 }
1809 path[depth].p_ext = nearex + 1;
1810 } else {
1811 BUG_ON(newext->ee_block == nearex->ee_block);
1812 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1813 len = len < 0 ? 0 : len;
Mingming553f9002009-09-18 13:34:55 -04001814 ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001815 "move %d from 0x%p to 0x%p\n",
1816 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001817 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001818 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001819 ext4_ext_get_actual_len(newext),
Robin Dong07379642011-07-23 21:51:07 -04001820 nearex, len, nearex, nearex + 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001821 memmove(nearex + 1, nearex, len);
1822 path[depth].p_ext = nearex;
1823 }
1824
Marcin Slusarze8546d02008-04-17 10:38:59 -04001825 le16_add_cpu(&eh->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001826 nearex = path[depth].p_ext;
1827 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001828 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001829 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001830
1831merge:
1832 /* try to merge extents to the right */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001833 if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
Mingming Cao00314622009-09-28 15:49:08 -04001834 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001835
1836 /* try to merge extents to the left */
1837
1838 /* time to correct all indexes above */
1839 err = ext4_ext_correct_indexes(handle, inode, path);
1840 if (err)
1841 goto cleanup;
1842
1843 err = ext4_ext_dirty(handle, inode, path + depth);
1844
1845cleanup:
1846 if (npath) {
1847 ext4_ext_drop_refs(npath);
1848 kfree(npath);
1849 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001850 ext4_ext_invalidate_cache(inode);
1851 return err;
1852}
1853
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001854static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1855 ext4_lblk_t num, ext_prepare_callback func,
1856 void *cbdata)
Eric Sandeen6873fa02008-10-07 00:46:36 -04001857{
1858 struct ext4_ext_path *path = NULL;
1859 struct ext4_ext_cache cbex;
1860 struct ext4_extent *ex;
1861 ext4_lblk_t next, start = 0, end = 0;
1862 ext4_lblk_t last = block + num;
1863 int depth, exists, err = 0;
1864
1865 BUG_ON(func == NULL);
1866 BUG_ON(inode == NULL);
1867
Lukas Czernerf17722f2011-06-06 00:05:17 -04001868 while (block < last && block != EXT_MAX_BLOCKS) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04001869 num = last - block;
1870 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001871 down_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001872 path = ext4_ext_find_extent(inode, block, path);
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001873 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001874 if (IS_ERR(path)) {
1875 err = PTR_ERR(path);
1876 path = NULL;
1877 break;
1878 }
1879
1880 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05001881 if (unlikely(path[depth].p_hdr == NULL)) {
1882 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1883 err = -EIO;
1884 break;
1885 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04001886 ex = path[depth].p_ext;
1887 next = ext4_ext_next_allocated_block(path);
1888
1889 exists = 0;
1890 if (!ex) {
1891 /* there is no extent yet, so try to allocate
1892 * all requested space */
1893 start = block;
1894 end = block + num;
1895 } else if (le32_to_cpu(ex->ee_block) > block) {
1896 /* need to allocate space before found extent */
1897 start = block;
1898 end = le32_to_cpu(ex->ee_block);
1899 if (block + num < end)
1900 end = block + num;
1901 } else if (block >= le32_to_cpu(ex->ee_block)
1902 + ext4_ext_get_actual_len(ex)) {
1903 /* need to allocate space after found extent */
1904 start = block;
1905 end = block + num;
1906 if (end >= next)
1907 end = next;
1908 } else if (block >= le32_to_cpu(ex->ee_block)) {
1909 /*
1910 * some part of requested space is covered
1911 * by found extent
1912 */
1913 start = block;
1914 end = le32_to_cpu(ex->ee_block)
1915 + ext4_ext_get_actual_len(ex);
1916 if (block + num < end)
1917 end = block + num;
1918 exists = 1;
1919 } else {
1920 BUG();
1921 }
1922 BUG_ON(end <= start);
1923
1924 if (!exists) {
1925 cbex.ec_block = start;
1926 cbex.ec_len = end - start;
1927 cbex.ec_start = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04001928 } else {
1929 cbex.ec_block = le32_to_cpu(ex->ee_block);
1930 cbex.ec_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001931 cbex.ec_start = ext4_ext_pblock(ex);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001932 }
1933
Frank Mayhar273df552010-03-02 11:46:09 -05001934 if (unlikely(cbex.ec_len == 0)) {
1935 EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
1936 err = -EIO;
1937 break;
1938 }
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04001939 err = func(inode, next, &cbex, ex, cbdata);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001940 ext4_ext_drop_refs(path);
1941
1942 if (err < 0)
1943 break;
1944
1945 if (err == EXT_REPEAT)
1946 continue;
1947 else if (err == EXT_BREAK) {
1948 err = 0;
1949 break;
1950 }
1951
1952 if (ext_depth(inode) != depth) {
1953 /* depth was changed. we have to realloc path */
1954 kfree(path);
1955 path = NULL;
1956 }
1957
1958 block = cbex.ec_block + cbex.ec_len;
1959 }
1960
1961 if (path) {
1962 ext4_ext_drop_refs(path);
1963 kfree(path);
1964 }
1965
1966 return err;
1967}
1968
Avantika Mathur09b88252006-12-06 20:41:36 -08001969static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001970ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05001971 __u32 len, ext4_fsblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07001972{
1973 struct ext4_ext_cache *cex;
1974 BUG_ON(len == 0);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001975 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Aditya Kalid8990242011-09-09 19:18:51 -04001976 trace_ext4_ext_put_in_cache(inode, block, len, start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001977 cex = &EXT4_I(inode)->i_cached_extent;
Alex Tomasa86c6182006-10-11 01:21:03 -07001978 cex->ec_block = block;
1979 cex->ec_len = len;
1980 cex->ec_start = start;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001981 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001982}
1983
1984/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001985 * ext4_ext_put_gap_in_cache:
1986 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07001987 * and cache this gap
1988 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001989static void
Alex Tomasa86c6182006-10-11 01:21:03 -07001990ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001991 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07001992{
1993 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001994 unsigned long len;
1995 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001996 struct ext4_extent *ex;
1997
1998 ex = path[depth].p_ext;
1999 if (ex == NULL) {
2000 /* there is no extent yet, so gap is [0;-] */
2001 lblock = 0;
Lukas Czernerf17722f2011-06-06 00:05:17 -04002002 len = EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07002003 ext_debug("cache gap(whole file):");
2004 } else if (block < le32_to_cpu(ex->ee_block)) {
2005 lblock = block;
2006 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05002007 ext_debug("cache gap(before): %u [%u:%u]",
2008 block,
2009 le32_to_cpu(ex->ee_block),
2010 ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002011 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002012 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002013 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002014 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002015 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002016
2017 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05002018 ext_debug("cache gap(after): [%u:%u] %u",
2019 le32_to_cpu(ex->ee_block),
2020 ext4_ext_get_actual_len(ex),
2021 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002022 BUG_ON(next == lblock);
2023 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07002024 } else {
2025 lblock = len = 0;
2026 BUG();
2027 }
2028
Eric Sandeenbba90742008-01-28 23:58:27 -05002029 ext_debug(" -> %u:%lu\n", lblock, len);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002030 ext4_ext_put_in_cache(inode, lblock, len, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002031}
2032
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002033/*
Robin Dongb7ca1e82011-07-23 21:53:25 -04002034 * ext4_ext_check_cache()
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002035 * Checks to see if the given block is in the cache.
2036 * If it is, the cached extent is stored in the given
2037 * cache extent pointer. If the cached extent is a hole,
2038 * this routine should be used instead of
2039 * ext4_ext_in_cache if the calling function needs to
2040 * know the size of the hole.
2041 *
2042 * @inode: The files inode
2043 * @block: The block to look for in the cache
2044 * @ex: Pointer where the cached extent will be stored
2045 * if it contains block
2046 *
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002047 * Return 0 if cache is invalid; 1 if the cache is valid
2048 */
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002049static int ext4_ext_check_cache(struct inode *inode, ext4_lblk_t block,
2050 struct ext4_ext_cache *ex){
Alex Tomasa86c6182006-10-11 01:21:03 -07002051 struct ext4_ext_cache *cex;
Vivek Haldar77f41352011-05-22 21:24:16 -04002052 struct ext4_sb_info *sbi;
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002053 int ret = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002054
Theodore Ts'o60e66792010-05-17 07:00:00 -04002055 /*
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002056 * We borrow i_block_reservation_lock to protect i_cached_extent
2057 */
2058 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07002059 cex = &EXT4_I(inode)->i_cached_extent;
Vivek Haldar77f41352011-05-22 21:24:16 -04002060 sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002061
2062 /* has cache valid data? */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002063 if (cex->ec_len == 0)
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002064 goto errout;
Alex Tomasa86c6182006-10-11 01:21:03 -07002065
Akinobu Mita731eb1a2010-03-03 23:55:01 -05002066 if (in_range(block, cex->ec_block, cex->ec_len)) {
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002067 memcpy(ex, cex, sizeof(struct ext4_ext_cache));
Eric Sandeenbba90742008-01-28 23:58:27 -05002068 ext_debug("%u cached by %u:%u:%llu\n",
2069 block,
2070 cex->ec_block, cex->ec_len, cex->ec_start);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002071 ret = 1;
Alex Tomasa86c6182006-10-11 01:21:03 -07002072 }
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002073errout:
Vivek Haldar77f41352011-05-22 21:24:16 -04002074 if (!ret)
2075 sbi->extent_cache_misses++;
2076 else
2077 sbi->extent_cache_hits++;
Aditya Kalid8990242011-09-09 19:18:51 -04002078 trace_ext4_ext_in_cache(inode, block, ret);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002079 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
2080 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07002081}
2082
2083/*
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002084 * ext4_ext_in_cache()
2085 * Checks to see if the given block is in the cache.
2086 * If it is, the cached extent is stored in the given
2087 * extent pointer.
2088 *
2089 * @inode: The files inode
2090 * @block: The block to look for in the cache
2091 * @ex: Pointer where the cached extent will be stored
2092 * if it contains block
2093 *
2094 * Return 0 if cache is invalid; 1 if the cache is valid
2095 */
2096static int
2097ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
2098 struct ext4_extent *ex)
2099{
2100 struct ext4_ext_cache cex;
2101 int ret = 0;
2102
2103 if (ext4_ext_check_cache(inode, block, &cex)) {
2104 ex->ee_block = cpu_to_le32(cex.ec_block);
2105 ext4_ext_store_pblock(ex, cex.ec_start);
2106 ex->ee_len = cpu_to_le16(cex.ec_len);
2107 ret = 1;
2108 }
2109
2110 return ret;
2111}
2112
2113
2114/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002115 * ext4_ext_rm_idx:
2116 * removes index from the index block.
Alex Tomasa86c6182006-10-11 01:21:03 -07002117 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002118static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07002119 struct ext4_ext_path *path)
2120{
Alex Tomasa86c6182006-10-11 01:21:03 -07002121 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002122 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002123
2124 /* free index block */
2125 path--;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002126 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002127 if (unlikely(path->p_hdr->eh_entries == 0)) {
2128 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2129 return -EIO;
2130 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002131 err = ext4_ext_get_access(handle, inode, path);
2132 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002133 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002134
2135 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2136 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2137 len *= sizeof(struct ext4_extent_idx);
2138 memmove(path->p_idx, path->p_idx + 1, len);
2139 }
2140
Marcin Slusarze8546d02008-04-17 10:38:59 -04002141 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002142 err = ext4_ext_dirty(handle, inode, path);
2143 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002144 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002145 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Aditya Kalid8990242011-09-09 19:18:51 -04002146 trace_ext4_ext_rm_idx(inode, leaf);
2147
Peter Huewe7dc57612011-02-21 21:01:42 -05002148 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002149 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Alex Tomasa86c6182006-10-11 01:21:03 -07002150 return err;
2151}
2152
2153/*
Mingming Caoee12b632008-08-19 22:16:05 -04002154 * ext4_ext_calc_credits_for_single_extent:
2155 * This routine returns max. credits that needed to insert an extent
2156 * to the extent tree.
2157 * When pass the actual path, the caller should calculate credits
2158 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002159 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002160int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002161 struct ext4_ext_path *path)
2162{
Alex Tomasa86c6182006-10-11 01:21:03 -07002163 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002164 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002165 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002166
Alex Tomasa86c6182006-10-11 01:21:03 -07002167 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002168 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002169 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2170
2171 /*
2172 * There are some space in the leaf tree, no
2173 * need to account for leaf block credit
2174 *
2175 * bitmaps and block group descriptor blocks
Tao Madf3ab172011-10-08 15:53:49 -04002176 * and other metadata blocks still need to be
Mingming Caoee12b632008-08-19 22:16:05 -04002177 * accounted.
2178 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002179 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002180 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002181 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002182 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002183 }
2184
Mingming Cao525f4ed2008-08-19 22:15:58 -04002185 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002186}
Alex Tomasa86c6182006-10-11 01:21:03 -07002187
Mingming Caoee12b632008-08-19 22:16:05 -04002188/*
2189 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2190 *
2191 * if nrblocks are fit in a single extent (chunk flag is 1), then
2192 * in the worse case, each tree level index/leaf need to be changed
2193 * if the tree split due to insert a new extent, then the old tree
2194 * index/leaf need to be updated too
2195 *
2196 * If the nrblocks are discontiguous, they could cause
2197 * the whole tree split more than once, but this is really rare.
2198 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002199int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoee12b632008-08-19 22:16:05 -04002200{
2201 int index;
2202 int depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002203
Mingming Caoee12b632008-08-19 22:16:05 -04002204 if (chunk)
2205 index = depth * 2;
2206 else
2207 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002208
Mingming Caoee12b632008-08-19 22:16:05 -04002209 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002210}
2211
2212static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002213 struct ext4_extent *ex,
2214 ext4_fsblk_t *partial_cluster,
2215 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002216{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002217 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002218 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002219 ext4_fsblk_t pblk;
Theodore Ts'oe6362602009-11-23 07:17:05 -05002220 int flags = EXT4_FREE_BLOCKS_FORGET;
Alex Tomasa86c6182006-10-11 01:21:03 -07002221
Alex Tomasc9de5602008-01-29 00:19:52 -05002222 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
Theodore Ts'oe6362602009-11-23 07:17:05 -05002223 flags |= EXT4_FREE_BLOCKS_METADATA;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002224 /*
2225 * For bigalloc file systems, we never free a partial cluster
2226 * at the beginning of the extent. Instead, we make a note
2227 * that we tried freeing the cluster, and check to see if we
2228 * need to free it on a subsequent call to ext4_remove_blocks,
2229 * or at the end of the ext4_truncate() operation.
2230 */
2231 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
2232
Aditya Kalid8990242011-09-09 19:18:51 -04002233 trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002234 /*
2235 * If we have a partial cluster, and it's different from the
2236 * cluster of the last block, we need to explicitly free the
2237 * partial cluster here.
2238 */
2239 pblk = ext4_ext_pblock(ex) + ee_len - 1;
2240 if (*partial_cluster && (EXT4_B2C(sbi, pblk) != *partial_cluster)) {
2241 ext4_free_blocks(handle, inode, NULL,
2242 EXT4_C2B(sbi, *partial_cluster),
2243 sbi->s_cluster_ratio, flags);
2244 *partial_cluster = 0;
2245 }
2246
Alex Tomasa86c6182006-10-11 01:21:03 -07002247#ifdef EXTENTS_STATS
2248 {
2249 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002250 spin_lock(&sbi->s_ext_stats_lock);
2251 sbi->s_ext_blocks += ee_len;
2252 sbi->s_ext_extents++;
2253 if (ee_len < sbi->s_ext_min)
2254 sbi->s_ext_min = ee_len;
2255 if (ee_len > sbi->s_ext_max)
2256 sbi->s_ext_max = ee_len;
2257 if (ext_depth(inode) > sbi->s_depth_max)
2258 sbi->s_depth_max = ext_depth(inode);
2259 spin_unlock(&sbi->s_ext_stats_lock);
2260 }
2261#endif
2262 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002263 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002264 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002265 ext4_lblk_t num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002266
Amit Aroraa2df2a62007-07-17 21:42:41 -04002267 num = le32_to_cpu(ex->ee_block) + ee_len - from;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002268 pblk = ext4_ext_pblock(ex) + ee_len - num;
2269 ext_debug("free last %u blocks starting %llu\n", num, pblk);
2270 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
2271 /*
2272 * If the block range to be freed didn't start at the
2273 * beginning of a cluster, and we removed the entire
2274 * extent, save the partial cluster here, since we
2275 * might need to delete if we determine that the
2276 * truncate operation has removed all of the blocks in
2277 * the cluster.
2278 */
2279 if (pblk & (sbi->s_cluster_ratio - 1) &&
2280 (ee_len == num))
2281 *partial_cluster = EXT4_B2C(sbi, pblk);
2282 else
2283 *partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002284 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002285 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002286 /* head removal */
2287 ext4_lblk_t num;
2288 ext4_fsblk_t start;
2289
2290 num = to - from;
2291 start = ext4_ext_pblock(ex);
2292
2293 ext_debug("free first %u blocks starting %llu\n", num, start);
H Hartley Sweetenee90d572011-10-18 11:01:51 -04002294 ext4_free_blocks(handle, inode, NULL, start, num, flags);
Allison Hendersond583fb82011-05-25 07:41:43 -04002295
Alex Tomasa86c6182006-10-11 01:21:03 -07002296 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002297 printk(KERN_INFO "strange request: removal(2) "
2298 "%u-%u from %u:%u\n",
2299 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002300 }
2301 return 0;
2302}
2303
Allison Hendersond583fb82011-05-25 07:41:43 -04002304
2305/*
2306 * ext4_ext_rm_leaf() Removes the extents associated with the
2307 * blocks appearing between "start" and "end", and splits the extents
2308 * if "start" and "end" appear in the same extent
2309 *
2310 * @handle: The journal handle
2311 * @inode: The files inode
2312 * @path: The path to the leaf
2313 * @start: The first block to remove
2314 * @end: The last block to remove
2315 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002316static int
2317ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002318 struct ext4_ext_path *path, ext4_fsblk_t *partial_cluster,
2319 ext4_lblk_t start, ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002320{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002321 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002322 int err = 0, correct_index = 0;
2323 int depth = ext_depth(inode), credits;
2324 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002325 ext4_lblk_t a, b, block;
2326 unsigned num;
2327 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002328 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002329 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002330 struct ext4_extent *ex;
Allison Hendersond583fb82011-05-25 07:41:43 -04002331 struct ext4_map_blocks map;
Alex Tomasa86c6182006-10-11 01:21:03 -07002332
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002333 /* the header must be checked already in ext4_ext_remove_space() */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002334 ext_debug("truncate since %u in leaf\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002335 if (!path[depth].p_hdr)
2336 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2337 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002338 if (unlikely(path[depth].p_hdr == NULL)) {
2339 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2340 return -EIO;
2341 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002342 /* find where to start removing */
2343 ex = EXT_LAST_EXTENT(eh);
2344
2345 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002346 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002347
Aditya Kalid8990242011-09-09 19:18:51 -04002348 trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster);
2349
Alex Tomasa86c6182006-10-11 01:21:03 -07002350 while (ex >= EXT_FIRST_EXTENT(eh) &&
2351 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002352
2353 if (ext4_ext_is_uninitialized(ex))
2354 uninitialized = 1;
2355 else
2356 uninitialized = 0;
2357
Mingming553f9002009-09-18 13:34:55 -04002358 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2359 uninitialized, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002360 path[depth].p_ext = ex;
2361
2362 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002363 b = ex_ee_block+ex_ee_len - 1 < end ?
2364 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002365
2366 ext_debug(" border %u:%u\n", a, b);
2367
Allison Hendersond583fb82011-05-25 07:41:43 -04002368 /* If this extent is beyond the end of the hole, skip it */
2369 if (end <= ex_ee_block) {
2370 ex--;
2371 ex_ee_block = le32_to_cpu(ex->ee_block);
2372 ex_ee_len = ext4_ext_get_actual_len(ex);
2373 continue;
2374 } else if (a != ex_ee_block &&
2375 b != ex_ee_block + ex_ee_len - 1) {
2376 /*
2377 * If this is a truncate, then this condition should
2378 * never happen because at least one of the end points
2379 * needs to be on the edge of the extent.
2380 */
Lukas Czernerf17722f2011-06-06 00:05:17 -04002381 if (end == EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002382 ext_debug(" bad truncate %u:%u\n",
2383 start, end);
2384 block = 0;
2385 num = 0;
2386 err = -EIO;
2387 goto out;
2388 }
2389 /*
2390 * else this is a hole punch, so the extent needs to
2391 * be split since neither edge of the hole is on the
2392 * extent edge
2393 */
2394 else{
2395 map.m_pblk = ext4_ext_pblock(ex);
2396 map.m_lblk = ex_ee_block;
2397 map.m_len = b - ex_ee_block;
2398
2399 err = ext4_split_extent(handle,
2400 inode, path, &map, 0,
2401 EXT4_GET_BLOCKS_PUNCH_OUT_EXT |
2402 EXT4_GET_BLOCKS_PRE_IO);
2403
2404 if (err < 0)
2405 goto out;
2406
2407 ex_ee_len = ext4_ext_get_actual_len(ex);
2408
2409 b = ex_ee_block+ex_ee_len - 1 < end ?
2410 ex_ee_block+ex_ee_len - 1 : end;
2411
2412 /* Then remove tail of this extent */
2413 block = ex_ee_block;
2414 num = a - block;
2415 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002416 } else if (a != ex_ee_block) {
2417 /* remove tail of the extent */
2418 block = ex_ee_block;
2419 num = a - block;
2420 } else if (b != ex_ee_block + ex_ee_len - 1) {
2421 /* remove head of the extent */
Allison Hendersond583fb82011-05-25 07:41:43 -04002422 block = b;
2423 num = ex_ee_block + ex_ee_len - b;
2424
2425 /*
2426 * If this is a truncate, this condition
2427 * should never happen
2428 */
Lukas Czernerf17722f2011-06-06 00:05:17 -04002429 if (end == EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002430 ext_debug(" bad truncate %u:%u\n",
2431 start, end);
2432 err = -EIO;
2433 goto out;
2434 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002435 } else {
2436 /* remove whole extent: excellent! */
2437 block = ex_ee_block;
2438 num = 0;
Allison Hendersond583fb82011-05-25 07:41:43 -04002439 if (a != ex_ee_block) {
2440 ext_debug(" bad truncate %u:%u\n",
2441 start, end);
2442 err = -EIO;
2443 goto out;
2444 }
2445
2446 if (b != ex_ee_block + ex_ee_len - 1) {
2447 ext_debug(" bad truncate %u:%u\n",
2448 start, end);
2449 err = -EIO;
2450 goto out;
2451 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002452 }
2453
Theodore Ts'o34071da2008-08-01 21:59:19 -04002454 /*
2455 * 3 for leaf, sb, and inode plus 2 (bmap and group
2456 * descriptor) for each block group; assume two block
2457 * groups plus ex_ee_len/blocks_per_block_group for
2458 * the worst case
2459 */
2460 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002461 if (ex == EXT_FIRST_EXTENT(eh)) {
2462 correct_index = 1;
2463 credits += (ext_depth(inode)) + 1;
2464 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002465 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002466
Jan Kara487caee2009-08-17 22:17:20 -04002467 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
Shen Feng9102e4f2008-07-11 19:27:31 -04002468 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002469 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002470
2471 err = ext4_ext_get_access(handle, inode, path + depth);
2472 if (err)
2473 goto out;
2474
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002475 err = ext4_remove_blocks(handle, inode, ex, partial_cluster,
2476 a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002477 if (err)
2478 goto out;
2479
2480 if (num == 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002481 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002482 ext4_ext_store_pblock(ex, 0);
Allison Hendersond583fb82011-05-25 07:41:43 -04002483 } else if (block != ex_ee_block) {
2484 /*
2485 * If this was a head removal, then we need to update
2486 * the physical block since it is now at a different
2487 * location
2488 */
2489 ext4_ext_store_pblock(ex, ext4_ext_pblock(ex) + (b-a));
Alex Tomasa86c6182006-10-11 01:21:03 -07002490 }
2491
2492 ex->ee_block = cpu_to_le32(block);
2493 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002494 /*
2495 * Do not mark uninitialized if all the blocks in the
2496 * extent have been removed.
2497 */
2498 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002499 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002500
2501 err = ext4_ext_dirty(handle, inode, path + depth);
2502 if (err)
2503 goto out;
2504
Allison Hendersond583fb82011-05-25 07:41:43 -04002505 /*
2506 * If the extent was completely released,
2507 * we need to remove it from the leaf
2508 */
2509 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002510 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002511 /*
2512 * For hole punching, we need to scoot all the
2513 * extents up when an extent is removed so that
2514 * we dont have blank extents in the middle
2515 */
2516 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2517 sizeof(struct ext4_extent));
2518
2519 /* Now get rid of the one at the end */
2520 memset(EXT_LAST_EXTENT(eh), 0,
2521 sizeof(struct ext4_extent));
2522 }
2523 le16_add_cpu(&eh->eh_entries, -1);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002524 } else
2525 *partial_cluster = 0;
Allison Hendersond583fb82011-05-25 07:41:43 -04002526
Mingming Cao2ae02102006-10-11 01:21:11 -07002527 ext_debug("new extent: %u:%u:%llu\n", block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002528 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002529 ex--;
2530 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002531 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002532 }
2533
2534 if (correct_index && eh->eh_entries)
2535 err = ext4_ext_correct_indexes(handle, inode, path);
2536
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002537 /*
2538 * If there is still a entry in the leaf node, check to see if
2539 * it references the partial cluster. This is the only place
2540 * where it could; if it doesn't, we can free the cluster.
2541 */
2542 if (*partial_cluster && ex >= EXT_FIRST_EXTENT(eh) &&
2543 (EXT4_B2C(sbi, ext4_ext_pblock(ex) + ex_ee_len - 1) !=
2544 *partial_cluster)) {
2545 int flags = EXT4_FREE_BLOCKS_FORGET;
2546
2547 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2548 flags |= EXT4_FREE_BLOCKS_METADATA;
2549
2550 ext4_free_blocks(handle, inode, NULL,
2551 EXT4_C2B(sbi, *partial_cluster),
2552 sbi->s_cluster_ratio, flags);
2553 *partial_cluster = 0;
2554 }
2555
Alex Tomasa86c6182006-10-11 01:21:03 -07002556 /* if this leaf is free, then we should
2557 * remove it from index block above */
2558 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2559 err = ext4_ext_rm_idx(handle, inode, path + depth);
2560
2561out:
2562 return err;
2563}
2564
2565/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002566 * ext4_ext_more_to_rm:
2567 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002568 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002569static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002570ext4_ext_more_to_rm(struct ext4_ext_path *path)
2571{
2572 BUG_ON(path->p_idx == NULL);
2573
2574 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2575 return 0;
2576
2577 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002578 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002579 * so we have to consider current index for truncation
2580 */
2581 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2582 return 0;
2583 return 1;
2584}
2585
Allison Hendersonc6a03712011-07-17 23:21:03 -04002586static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07002587{
2588 struct super_block *sb = inode->i_sb;
2589 int depth = ext_depth(inode);
2590 struct ext4_ext_path *path;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002591 ext4_fsblk_t partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002592 handle_t *handle;
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002593 int i, err;
Alex Tomasa86c6182006-10-11 01:21:03 -07002594
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002595 ext_debug("truncate since %u\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002596
2597 /* probably first extent we're gonna free will be last in block */
2598 handle = ext4_journal_start(inode, depth + 1);
2599 if (IS_ERR(handle))
2600 return PTR_ERR(handle);
2601
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002602again:
Alex Tomasa86c6182006-10-11 01:21:03 -07002603 ext4_ext_invalidate_cache(inode);
2604
Aditya Kalid8990242011-09-09 19:18:51 -04002605 trace_ext4_ext_remove_space(inode, start, depth);
2606
Alex Tomasa86c6182006-10-11 01:21:03 -07002607 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002608 * We start scanning from right side, freeing all the blocks
2609 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002610 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002611 depth = ext_depth(inode);
Josef Bacik216553c2008-04-29 22:02:02 -04002612 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07002613 if (path == NULL) {
2614 ext4_journal_stop(handle);
2615 return -ENOMEM;
2616 }
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002617 path[0].p_depth = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -07002618 path[0].p_hdr = ext_inode_hdr(inode);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002619 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002620 err = -EIO;
2621 goto out;
2622 }
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002623 i = err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002624
2625 while (i >= 0 && err == 0) {
2626 if (i == depth) {
2627 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04002628 err = ext4_ext_rm_leaf(handle, inode, path,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002629 &partial_cluster, start,
2630 EXT_MAX_BLOCKS - 1);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002631 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002632 brelse(path[i].p_bh);
2633 path[i].p_bh = NULL;
2634 i--;
2635 continue;
2636 }
2637
2638 /* this is index block */
2639 if (!path[i].p_hdr) {
2640 ext_debug("initialize header\n");
2641 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002642 }
2643
Alex Tomasa86c6182006-10-11 01:21:03 -07002644 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002645 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002646 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2647 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2648 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2649 path[i].p_hdr,
2650 le16_to_cpu(path[i].p_hdr->eh_entries));
2651 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002652 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002653 path[i].p_idx--;
2654 }
2655
2656 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2657 i, EXT_FIRST_INDEX(path[i].p_hdr),
2658 path[i].p_idx);
2659 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002660 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002661 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002662 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04002663 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002664 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'obf89d162010-10-27 21:30:14 -04002665 bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002666 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002667 /* should we reset i_size? */
2668 err = -EIO;
2669 break;
2670 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002671 if (WARN_ON(i + 1 > depth)) {
2672 err = -EIO;
2673 break;
2674 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002675 if (ext4_ext_check(inode, ext_block_hdr(bh),
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002676 depth - i - 1)) {
2677 err = -EIO;
2678 break;
2679 }
2680 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002681
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002682 /* save actual number of indexes since this
2683 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002684 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2685 i++;
2686 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002687 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002688 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002689 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002690 * handle must be already prepared by the
2691 * truncatei_leaf() */
2692 err = ext4_ext_rm_idx(handle, inode, path + i);
2693 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002694 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002695 brelse(path[i].p_bh);
2696 path[i].p_bh = NULL;
2697 i--;
2698 ext_debug("return to level %d\n", i);
2699 }
2700 }
2701
Aditya Kalid8990242011-09-09 19:18:51 -04002702 trace_ext4_ext_remove_space_done(inode, start, depth, partial_cluster,
2703 path->p_hdr->eh_entries);
2704
Aditya Kali7b415bf2011-09-09 19:04:51 -04002705 /* If we still have something in the partial cluster and we have removed
2706 * even the first extent, then we should free the blocks in the partial
2707 * cluster as well. */
2708 if (partial_cluster && path->p_hdr->eh_entries == 0) {
2709 int flags = EXT4_FREE_BLOCKS_FORGET;
2710
2711 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2712 flags |= EXT4_FREE_BLOCKS_METADATA;
2713
2714 ext4_free_blocks(handle, inode, NULL,
2715 EXT4_C2B(EXT4_SB(sb), partial_cluster),
2716 EXT4_SB(sb)->s_cluster_ratio, flags);
2717 partial_cluster = 0;
2718 }
2719
Alex Tomasa86c6182006-10-11 01:21:03 -07002720 /* TODO: flexible tree reduction should be here */
2721 if (path->p_hdr->eh_entries == 0) {
2722 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002723 * truncate to zero freed all the tree,
2724 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002725 */
2726 err = ext4_ext_get_access(handle, inode, path);
2727 if (err == 0) {
2728 ext_inode_hdr(inode)->eh_depth = 0;
2729 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04002730 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07002731 err = ext4_ext_dirty(handle, inode, path);
2732 }
2733 }
2734out:
Alex Tomasa86c6182006-10-11 01:21:03 -07002735 ext4_ext_drop_refs(path);
2736 kfree(path);
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002737 if (err == -EAGAIN)
2738 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07002739 ext4_journal_stop(handle);
2740
2741 return err;
2742}
2743
2744/*
2745 * called at mount time
2746 */
2747void ext4_ext_init(struct super_block *sb)
2748{
2749 /*
2750 * possible initialization would be here
2751 */
2752
Theodore Ts'o83982b62009-01-06 14:53:16 -05002753 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04002754#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002755 printk(KERN_INFO "EXT4-fs: file extents enabled");
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002756#ifdef AGGRESSIVE_TEST
2757 printk(", aggressive tests");
Alex Tomasa86c6182006-10-11 01:21:03 -07002758#endif
2759#ifdef CHECK_BINSEARCH
2760 printk(", check binsearch");
2761#endif
2762#ifdef EXTENTS_STATS
2763 printk(", stats");
2764#endif
2765 printk("\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04002766#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07002767#ifdef EXTENTS_STATS
2768 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2769 EXT4_SB(sb)->s_ext_min = 1 << 30;
2770 EXT4_SB(sb)->s_ext_max = 0;
2771#endif
2772 }
2773}
2774
2775/*
2776 * called at umount time
2777 */
2778void ext4_ext_release(struct super_block *sb)
2779{
Theodore Ts'o83982b62009-01-06 14:53:16 -05002780 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
Alex Tomasa86c6182006-10-11 01:21:03 -07002781 return;
2782
2783#ifdef EXTENTS_STATS
2784 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2785 struct ext4_sb_info *sbi = EXT4_SB(sb);
2786 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2787 sbi->s_ext_blocks, sbi->s_ext_extents,
2788 sbi->s_ext_blocks / sbi->s_ext_extents);
2789 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2790 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2791 }
2792#endif
2793}
2794
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002795/* FIXME!! we need to try to merge to left or right after zero-out */
2796static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2797{
Lukas Czerner24075182010-10-27 21:30:06 -04002798 ext4_fsblk_t ee_pblock;
2799 unsigned int ee_len;
Jing Zhangb7203032010-05-12 00:00:00 -04002800 int ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002801
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002802 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04002803 ee_pblock = ext4_ext_pblock(ex);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002804
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04002805 ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
Lukas Czerner24075182010-10-27 21:30:06 -04002806 if (ret > 0)
2807 ret = 0;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002808
Lukas Czerner24075182010-10-27 21:30:06 -04002809 return ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002810}
2811
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002812/*
2813 * used by extent splitting.
2814 */
2815#define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
2816 due to ENOSPC */
2817#define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */
2818#define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */
2819
2820/*
2821 * ext4_split_extent_at() splits an extent at given block.
2822 *
2823 * @handle: the journal handle
2824 * @inode: the file inode
2825 * @path: the path to the extent
2826 * @split: the logical block where the extent is splitted.
2827 * @split_flags: indicates if the extent could be zeroout if split fails, and
2828 * the states(init or uninit) of new extents.
2829 * @flags: flags used to insert new extent to extent tree.
2830 *
2831 *
2832 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
2833 * of which are deterimined by split_flag.
2834 *
2835 * There are two cases:
2836 * a> the extent are splitted into two extent.
2837 * b> split is not needed, and just mark the extent.
2838 *
2839 * return 0 on success.
2840 */
2841static int ext4_split_extent_at(handle_t *handle,
2842 struct inode *inode,
2843 struct ext4_ext_path *path,
2844 ext4_lblk_t split,
2845 int split_flag,
2846 int flags)
2847{
2848 ext4_fsblk_t newblock;
2849 ext4_lblk_t ee_block;
2850 struct ext4_extent *ex, newex, orig_ex;
2851 struct ext4_extent *ex2 = NULL;
2852 unsigned int ee_len, depth;
2853 int err = 0;
2854
2855 ext_debug("ext4_split_extents_at: inode %lu, logical"
2856 "block %llu\n", inode->i_ino, (unsigned long long)split);
2857
2858 ext4_ext_show_leaf(inode, path);
2859
2860 depth = ext_depth(inode);
2861 ex = path[depth].p_ext;
2862 ee_block = le32_to_cpu(ex->ee_block);
2863 ee_len = ext4_ext_get_actual_len(ex);
2864 newblock = split - ee_block + ext4_ext_pblock(ex);
2865
2866 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
2867
2868 err = ext4_ext_get_access(handle, inode, path + depth);
2869 if (err)
2870 goto out;
2871
2872 if (split == ee_block) {
2873 /*
2874 * case b: block @split is the block that the extent begins with
2875 * then we just change the state of the extent, and splitting
2876 * is not needed.
2877 */
2878 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2879 ext4_ext_mark_uninitialized(ex);
2880 else
2881 ext4_ext_mark_initialized(ex);
2882
2883 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
2884 ext4_ext_try_to_merge(inode, path, ex);
2885
2886 err = ext4_ext_dirty(handle, inode, path + depth);
2887 goto out;
2888 }
2889
2890 /* case a */
2891 memcpy(&orig_ex, ex, sizeof(orig_ex));
2892 ex->ee_len = cpu_to_le16(split - ee_block);
2893 if (split_flag & EXT4_EXT_MARK_UNINIT1)
2894 ext4_ext_mark_uninitialized(ex);
2895
2896 /*
2897 * path may lead to new leaf, not to original leaf any more
2898 * after ext4_ext_insert_extent() returns,
2899 */
2900 err = ext4_ext_dirty(handle, inode, path + depth);
2901 if (err)
2902 goto fix_extent_len;
2903
2904 ex2 = &newex;
2905 ex2->ee_block = cpu_to_le32(split);
2906 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
2907 ext4_ext_store_pblock(ex2, newblock);
2908 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2909 ext4_ext_mark_uninitialized(ex2);
2910
2911 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
2912 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2913 err = ext4_ext_zeroout(inode, &orig_ex);
2914 if (err)
2915 goto fix_extent_len;
2916 /* update the extent length and mark as initialized */
2917 ex->ee_len = cpu_to_le32(ee_len);
2918 ext4_ext_try_to_merge(inode, path, ex);
2919 err = ext4_ext_dirty(handle, inode, path + depth);
2920 goto out;
2921 } else if (err)
2922 goto fix_extent_len;
2923
2924out:
2925 ext4_ext_show_leaf(inode, path);
2926 return err;
2927
2928fix_extent_len:
2929 ex->ee_len = orig_ex.ee_len;
2930 ext4_ext_dirty(handle, inode, path + depth);
2931 return err;
2932}
2933
2934/*
2935 * ext4_split_extents() splits an extent and mark extent which is covered
2936 * by @map as split_flags indicates
2937 *
2938 * It may result in splitting the extent into multiple extents (upto three)
2939 * There are three possibilities:
2940 * a> There is no split required
2941 * b> Splits in two extents: Split is happening at either end of the extent
2942 * c> Splits in three extents: Somone is splitting in middle of the extent
2943 *
2944 */
2945static int ext4_split_extent(handle_t *handle,
2946 struct inode *inode,
2947 struct ext4_ext_path *path,
2948 struct ext4_map_blocks *map,
2949 int split_flag,
2950 int flags)
2951{
2952 ext4_lblk_t ee_block;
2953 struct ext4_extent *ex;
2954 unsigned int ee_len, depth;
2955 int err = 0;
2956 int uninitialized;
2957 int split_flag1, flags1;
2958
2959 depth = ext_depth(inode);
2960 ex = path[depth].p_ext;
2961 ee_block = le32_to_cpu(ex->ee_block);
2962 ee_len = ext4_ext_get_actual_len(ex);
2963 uninitialized = ext4_ext_is_uninitialized(ex);
2964
2965 if (map->m_lblk + map->m_len < ee_block + ee_len) {
2966 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2967 EXT4_EXT_MAY_ZEROOUT : 0;
2968 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
2969 if (uninitialized)
2970 split_flag1 |= EXT4_EXT_MARK_UNINIT1 |
2971 EXT4_EXT_MARK_UNINIT2;
2972 err = ext4_split_extent_at(handle, inode, path,
2973 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04002974 if (err)
2975 goto out;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002976 }
2977
2978 ext4_ext_drop_refs(path);
2979 path = ext4_ext_find_extent(inode, map->m_lblk, path);
2980 if (IS_ERR(path))
2981 return PTR_ERR(path);
2982
2983 if (map->m_lblk >= ee_block) {
2984 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2985 EXT4_EXT_MAY_ZEROOUT : 0;
2986 if (uninitialized)
2987 split_flag1 |= EXT4_EXT_MARK_UNINIT1;
2988 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2989 split_flag1 |= EXT4_EXT_MARK_UNINIT2;
2990 err = ext4_split_extent_at(handle, inode, path,
2991 map->m_lblk, split_flag1, flags);
2992 if (err)
2993 goto out;
2994 }
2995
2996 ext4_ext_show_leaf(inode, path);
2997out:
2998 return err ? err : map->m_len;
2999}
3000
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003001#define EXT4_EXT_ZERO_LEN 7
Amit Arora56055d32007-07-17 21:42:38 -04003002/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003003 * This function is called by ext4_ext_map_blocks() if someone tries to write
Amit Arora56055d32007-07-17 21:42:38 -04003004 * to an uninitialized extent. It may result in splitting the uninitialized
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003005 * extent into multiple extents (up to three - one initialized and two
Amit Arora56055d32007-07-17 21:42:38 -04003006 * uninitialized).
3007 * There are three possibilities:
3008 * a> There is no split required: Entire extent should be initialized
3009 * b> Splits in two extents: Write is happening at either end of the extent
3010 * c> Splits in three extents: Somone is writing in middle of the extent
3011 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003012static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003013 struct inode *inode,
3014 struct ext4_map_blocks *map,
3015 struct ext4_ext_path *path)
Amit Arora56055d32007-07-17 21:42:38 -04003016{
Yongqiang Yang667eff32011-05-03 12:25:07 -04003017 struct ext4_map_blocks split_map;
3018 struct ext4_extent zero_ex;
3019 struct ext4_extent *ex;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003020 ext4_lblk_t ee_block, eof_block;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003021 unsigned int allocated, ee_len, depth;
Amit Arora56055d32007-07-17 21:42:38 -04003022 int err = 0;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003023 int split_flag = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003024
3025 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3026 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003027 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003028
3029 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3030 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003031 if (eof_block < map->m_lblk + map->m_len)
3032 eof_block = map->m_lblk + map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003033
3034 depth = ext_depth(inode);
Amit Arora56055d32007-07-17 21:42:38 -04003035 ex = path[depth].p_ext;
3036 ee_block = le32_to_cpu(ex->ee_block);
3037 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003038 allocated = ee_len - (map->m_lblk - ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003039
Yongqiang Yang667eff32011-05-03 12:25:07 -04003040 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003041 /*
3042 * It is safe to convert extent to initialized via explicit
3043 * zeroout only if extent is fully insde i_size or new_size.
3044 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003045 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003046
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003047 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003048 if (ee_len <= 2*EXT4_EXT_ZERO_LEN &&
3049 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3050 err = ext4_ext_zeroout(inode, ex);
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003051 if (err)
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003052 goto out;
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003053
3054 err = ext4_ext_get_access(handle, inode, path + depth);
3055 if (err)
3056 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003057 ext4_ext_mark_initialized(ex);
3058 ext4_ext_try_to_merge(inode, path, ex);
3059 err = ext4_ext_dirty(handle, inode, path + depth);
3060 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04003061 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003062
Amit Arora56055d32007-07-17 21:42:38 -04003063 /*
Yongqiang Yang667eff32011-05-03 12:25:07 -04003064 * four cases:
3065 * 1. split the extent into three extents.
3066 * 2. split the extent into two extents, zeroout the first half.
3067 * 3. split the extent into two extents, zeroout the second half.
3068 * 4. split the extent into two extents with out zeroout.
Amit Arora56055d32007-07-17 21:42:38 -04003069 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003070 split_map.m_lblk = map->m_lblk;
3071 split_map.m_len = map->m_len;
3072
3073 if (allocated > map->m_len) {
3074 if (allocated <= EXT4_EXT_ZERO_LEN &&
3075 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3076 /* case 3 */
3077 zero_ex.ee_block =
Allison Henderson9b940f82011-05-16 10:11:09 -04003078 cpu_to_le32(map->m_lblk);
3079 zero_ex.ee_len = cpu_to_le16(allocated);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003080 ext4_ext_store_pblock(&zero_ex,
3081 ext4_ext_pblock(ex) + map->m_lblk - ee_block);
3082 err = ext4_ext_zeroout(inode, &zero_ex);
Amit Arora56055d32007-07-17 21:42:38 -04003083 if (err)
3084 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003085 split_map.m_lblk = map->m_lblk;
3086 split_map.m_len = allocated;
3087 } else if ((map->m_lblk - ee_block + map->m_len <
3088 EXT4_EXT_ZERO_LEN) &&
3089 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3090 /* case 2 */
3091 if (map->m_lblk != ee_block) {
3092 zero_ex.ee_block = ex->ee_block;
3093 zero_ex.ee_len = cpu_to_le16(map->m_lblk -
3094 ee_block);
3095 ext4_ext_store_pblock(&zero_ex,
3096 ext4_ext_pblock(ex));
3097 err = ext4_ext_zeroout(inode, &zero_ex);
3098 if (err)
3099 goto out;
3100 }
3101
Yongqiang Yang667eff32011-05-03 12:25:07 -04003102 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003103 split_map.m_len = map->m_lblk - ee_block + map->m_len;
3104 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003105 }
3106 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003107
3108 allocated = ext4_split_extent(handle, inode, path,
3109 &split_map, split_flag, 0);
3110 if (allocated < 0)
3111 err = allocated;
3112
Amit Arora56055d32007-07-17 21:42:38 -04003113out:
3114 return err ? err : allocated;
3115}
3116
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003117/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003118 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003119 * ext4_get_blocks_dio_write() when DIO to write
3120 * to an uninitialized extent.
3121 *
Paul Bollefd018fe2011-02-15 00:05:43 +01003122 * Writing to an uninitialized extent may result in splitting the uninitialized
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003123 * extent into multiple /initialized uninitialized extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003124 * There are three possibilities:
3125 * a> There is no split required: Entire extent should be uninitialized
3126 * b> Splits in two extents: Write is happening at either end of the extent
3127 * c> Splits in three extents: Somone is writing in middle of the extent
3128 *
3129 * One of more index blocks maybe needed if the extent tree grow after
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003130 * the uninitialized extent split. To prevent ENOSPC occur at the IO
Mingming Cao00314622009-09-28 15:49:08 -04003131 * complete, we need to split the uninitialized extent before DIO submit
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02003132 * the IO. The uninitialized extent called at this time will be split
Mingming Cao00314622009-09-28 15:49:08 -04003133 * into three uninitialized extent(at most). After IO complete, the part
3134 * being filled will be convert to initialized by the end_io callback function
3135 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003136 *
3137 * Returns the size of uninitialized extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003138 */
3139static int ext4_split_unwritten_extents(handle_t *handle,
3140 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003141 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003142 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04003143 int flags)
3144{
Yongqiang Yang667eff32011-05-03 12:25:07 -04003145 ext4_lblk_t eof_block;
3146 ext4_lblk_t ee_block;
3147 struct ext4_extent *ex;
3148 unsigned int ee_len;
3149 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003150
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003151 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
3152 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003153 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003154
3155 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3156 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003157 if (eof_block < map->m_lblk + map->m_len)
3158 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003159 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003160 * It is safe to convert extent to initialized via explicit
3161 * zeroout only if extent is fully insde i_size or new_size.
3162 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003163 depth = ext_depth(inode);
3164 ex = path[depth].p_ext;
3165 ee_block = le32_to_cpu(ex->ee_block);
3166 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003167
Yongqiang Yang667eff32011-05-03 12:25:07 -04003168 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
3169 split_flag |= EXT4_EXT_MARK_UNINIT2;
Mingming Cao00314622009-09-28 15:49:08 -04003170
Yongqiang Yang667eff32011-05-03 12:25:07 -04003171 flags |= EXT4_GET_BLOCKS_PRE_IO;
3172 return ext4_split_extent(handle, inode, path, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003173}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003174
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003175static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003176 struct inode *inode,
3177 struct ext4_ext_path *path)
3178{
3179 struct ext4_extent *ex;
Mingming Cao00314622009-09-28 15:49:08 -04003180 int depth;
3181 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003182
3183 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003184 ex = path[depth].p_ext;
3185
Yongqiang Yang197217a2011-05-03 11:45:29 -04003186 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3187 "block %llu, max_blocks %u\n", inode->i_ino,
3188 (unsigned long long)le32_to_cpu(ex->ee_block),
3189 ext4_ext_get_actual_len(ex));
3190
Mingming Cao00314622009-09-28 15:49:08 -04003191 err = ext4_ext_get_access(handle, inode, path + depth);
3192 if (err)
3193 goto out;
3194 /* first mark the extent as initialized */
3195 ext4_ext_mark_initialized(ex);
3196
Yongqiang Yang197217a2011-05-03 11:45:29 -04003197 /* note: ext4_ext_correct_indexes() isn't needed here because
3198 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003199 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04003200 ext4_ext_try_to_merge(inode, path, ex);
3201
Mingming Cao00314622009-09-28 15:49:08 -04003202 /* Mark modified extent as dirty */
3203 err = ext4_ext_dirty(handle, inode, path + depth);
3204out:
3205 ext4_ext_show_leaf(inode, path);
3206 return err;
3207}
3208
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003209static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3210 sector_t block, int count)
3211{
3212 int i;
3213 for (i = 0; i < count; i++)
3214 unmap_underlying_metadata(bdev, block + i);
3215}
3216
Theodore Ts'o58590b02010-10-27 21:23:12 -04003217/*
3218 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3219 */
3220static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
Eric Sandeend002ebf2011-01-10 13:03:35 -05003221 ext4_lblk_t lblk,
Theodore Ts'o58590b02010-10-27 21:23:12 -04003222 struct ext4_ext_path *path,
3223 unsigned int len)
3224{
3225 int i, depth;
3226 struct ext4_extent_header *eh;
Sergey Senozhatsky65922cb2011-03-23 14:08:27 -04003227 struct ext4_extent *last_ex;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003228
3229 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3230 return 0;
3231
3232 depth = ext_depth(inode);
3233 eh = path[depth].p_hdr;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003234
3235 if (unlikely(!eh->eh_entries)) {
3236 EXT4_ERROR_INODE(inode, "eh->eh_entries == 0 and "
3237 "EOFBLOCKS_FL set");
3238 return -EIO;
3239 }
3240 last_ex = EXT_LAST_EXTENT(eh);
3241 /*
3242 * We should clear the EOFBLOCKS_FL flag if we are writing the
3243 * last block in the last extent in the file. We test this by
3244 * first checking to see if the caller to
3245 * ext4_ext_get_blocks() was interested in the last block (or
3246 * a block beyond the last block) in the current extent. If
3247 * this turns out to be false, we can bail out from this
3248 * function immediately.
3249 */
Eric Sandeend002ebf2011-01-10 13:03:35 -05003250 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
Theodore Ts'o58590b02010-10-27 21:23:12 -04003251 ext4_ext_get_actual_len(last_ex))
3252 return 0;
3253 /*
3254 * If the caller does appear to be planning to write at or
3255 * beyond the end of the current extent, we then test to see
3256 * if the current extent is the last extent in the file, by
3257 * checking to make sure it was reached via the rightmost node
3258 * at each level of the tree.
3259 */
3260 for (i = depth-1; i >= 0; i--)
3261 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3262 return 0;
3263 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3264 return ext4_mark_inode_dirty(handle, inode);
3265}
3266
Aditya Kali7b415bf2011-09-09 19:04:51 -04003267/**
3268 * ext4_find_delalloc_range: find delayed allocated block in the given range.
3269 *
3270 * Goes through the buffer heads in the range [lblk_start, lblk_end] and returns
3271 * whether there are any buffers marked for delayed allocation. It returns '1'
3272 * on the first delalloc'ed buffer head found. If no buffer head in the given
3273 * range is marked for delalloc, it returns 0.
3274 * lblk_start should always be <= lblk_end.
3275 * search_hint_reverse is to indicate that searching in reverse from lblk_end to
3276 * lblk_start might be more efficient (i.e., we will likely hit the delalloc'ed
3277 * block sooner). This is useful when blocks are truncated sequentially from
3278 * lblk_start towards lblk_end.
3279 */
3280static int ext4_find_delalloc_range(struct inode *inode,
3281 ext4_lblk_t lblk_start,
3282 ext4_lblk_t lblk_end,
3283 int search_hint_reverse)
3284{
3285 struct address_space *mapping = inode->i_mapping;
3286 struct buffer_head *head, *bh = NULL;
3287 struct page *page;
3288 ext4_lblk_t i, pg_lblk;
3289 pgoff_t index;
3290
3291 /* reverse search wont work if fs block size is less than page size */
3292 if (inode->i_blkbits < PAGE_CACHE_SHIFT)
3293 search_hint_reverse = 0;
3294
3295 if (search_hint_reverse)
3296 i = lblk_end;
3297 else
3298 i = lblk_start;
3299
3300 index = i >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
3301
3302 while ((i >= lblk_start) && (i <= lblk_end)) {
3303 page = find_get_page(mapping, index);
Aditya Kali5356f262011-09-09 19:20:51 -04003304 if (!page)
Aditya Kali7b415bf2011-09-09 19:04:51 -04003305 goto nextpage;
3306
Aditya Kali7b415bf2011-09-09 19:04:51 -04003307 if (!page_has_buffers(page))
3308 goto nextpage;
3309
3310 head = page_buffers(page);
3311 if (!head)
3312 goto nextpage;
3313
3314 bh = head;
3315 pg_lblk = index << (PAGE_CACHE_SHIFT -
3316 inode->i_blkbits);
3317 do {
3318 if (unlikely(pg_lblk < lblk_start)) {
3319 /*
3320 * This is possible when fs block size is less
3321 * than page size and our cluster starts/ends in
3322 * middle of the page. So we need to skip the
3323 * initial few blocks till we reach the 'lblk'
3324 */
3325 pg_lblk++;
3326 continue;
3327 }
3328
Aditya Kali5356f262011-09-09 19:20:51 -04003329 /* Check if the buffer is delayed allocated and that it
3330 * is not yet mapped. (when da-buffers are mapped during
3331 * their writeout, their da_mapped bit is set.)
3332 */
3333 if (buffer_delay(bh) && !buffer_da_mapped(bh)) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04003334 page_cache_release(page);
Aditya Kalid8990242011-09-09 19:18:51 -04003335 trace_ext4_find_delalloc_range(inode,
3336 lblk_start, lblk_end,
3337 search_hint_reverse,
3338 1, i);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003339 return 1;
3340 }
3341 if (search_hint_reverse)
3342 i--;
3343 else
3344 i++;
3345 } while ((i >= lblk_start) && (i <= lblk_end) &&
3346 ((bh = bh->b_this_page) != head));
3347nextpage:
3348 if (page)
3349 page_cache_release(page);
3350 /*
3351 * Move to next page. 'i' will be the first lblk in the next
3352 * page.
3353 */
3354 if (search_hint_reverse)
3355 index--;
3356 else
3357 index++;
3358 i = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
3359 }
3360
Aditya Kalid8990242011-09-09 19:18:51 -04003361 trace_ext4_find_delalloc_range(inode, lblk_start, lblk_end,
3362 search_hint_reverse, 0, 0);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003363 return 0;
3364}
3365
3366int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk,
3367 int search_hint_reverse)
3368{
3369 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3370 ext4_lblk_t lblk_start, lblk_end;
3371 lblk_start = lblk & (~(sbi->s_cluster_ratio - 1));
3372 lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
3373
3374 return ext4_find_delalloc_range(inode, lblk_start, lblk_end,
3375 search_hint_reverse);
3376}
3377
3378/**
3379 * Determines how many complete clusters (out of those specified by the 'map')
3380 * are under delalloc and were reserved quota for.
3381 * This function is called when we are writing out the blocks that were
3382 * originally written with their allocation delayed, but then the space was
3383 * allocated using fallocate() before the delayed allocation could be resolved.
3384 * The cases to look for are:
3385 * ('=' indicated delayed allocated blocks
3386 * '-' indicates non-delayed allocated blocks)
3387 * (a) partial clusters towards beginning and/or end outside of allocated range
3388 * are not delalloc'ed.
3389 * Ex:
3390 * |----c---=|====c====|====c====|===-c----|
3391 * |++++++ allocated ++++++|
3392 * ==> 4 complete clusters in above example
3393 *
3394 * (b) partial cluster (outside of allocated range) towards either end is
3395 * marked for delayed allocation. In this case, we will exclude that
3396 * cluster.
3397 * Ex:
3398 * |----====c========|========c========|
3399 * |++++++ allocated ++++++|
3400 * ==> 1 complete clusters in above example
3401 *
3402 * Ex:
3403 * |================c================|
3404 * |++++++ allocated ++++++|
3405 * ==> 0 complete clusters in above example
3406 *
3407 * The ext4_da_update_reserve_space will be called only if we
3408 * determine here that there were some "entire" clusters that span
3409 * this 'allocated' range.
3410 * In the non-bigalloc case, this function will just end up returning num_blks
3411 * without ever calling ext4_find_delalloc_range.
3412 */
3413static unsigned int
3414get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start,
3415 unsigned int num_blks)
3416{
3417 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3418 ext4_lblk_t alloc_cluster_start, alloc_cluster_end;
3419 ext4_lblk_t lblk_from, lblk_to, c_offset;
3420 unsigned int allocated_clusters = 0;
3421
3422 alloc_cluster_start = EXT4_B2C(sbi, lblk_start);
3423 alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1);
3424
3425 /* max possible clusters for this allocation */
3426 allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1;
3427
Aditya Kalid8990242011-09-09 19:18:51 -04003428 trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks);
3429
Aditya Kali7b415bf2011-09-09 19:04:51 -04003430 /* Check towards left side */
3431 c_offset = lblk_start & (sbi->s_cluster_ratio - 1);
3432 if (c_offset) {
3433 lblk_from = lblk_start & (~(sbi->s_cluster_ratio - 1));
3434 lblk_to = lblk_from + c_offset - 1;
3435
3436 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
3437 allocated_clusters--;
3438 }
3439
3440 /* Now check towards right. */
3441 c_offset = (lblk_start + num_blks) & (sbi->s_cluster_ratio - 1);
3442 if (allocated_clusters && c_offset) {
3443 lblk_from = lblk_start + num_blks;
3444 lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1;
3445
3446 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
3447 allocated_clusters--;
3448 }
3449
3450 return allocated_clusters;
3451}
3452
Mingming Cao00314622009-09-28 15:49:08 -04003453static int
3454ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003455 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003456 struct ext4_ext_path *path, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003457 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003458{
3459 int ret = 0;
3460 int err = 0;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003461 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Mingming Cao00314622009-09-28 15:49:08 -04003462
3463 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3464 "block %llu, max_blocks %u, flags %d, allocated %u",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003465 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04003466 flags, allocated);
3467 ext4_ext_show_leaf(inode, path);
3468
Aditya Kalid8990242011-09-09 19:18:51 -04003469 trace_ext4_ext_handle_uninitialized_extents(inode, map, allocated,
3470 newblock);
3471
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003472 /* get_block() before submit the IO, split the extent */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003473 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003474 ret = ext4_split_unwritten_extents(handle, inode, map,
3475 path, flags);
Mingming5f524952009-11-10 10:48:04 -05003476 /*
3477 * Flag the inode(non aio case) or end_io struct (aio case)
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003478 * that this IO needs to conversion to written when IO is
Mingming5f524952009-11-10 10:48:04 -05003479 * completed
3480 */
Eric Sandeene9e3bce2011-02-12 08:17:34 -05003481 if (io && !(io->flag & EXT4_IO_END_UNWRITTEN)) {
Theodore Ts'obd2d0212010-10-27 21:30:10 -04003482 io->flag = EXT4_IO_END_UNWRITTEN;
Eric Sandeene9e3bce2011-02-12 08:17:34 -05003483 atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten);
3484 } else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003485 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
Jiaying Zhang744692d2010-03-04 16:14:02 -05003486 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003487 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao00314622009-09-28 15:49:08 -04003488 goto out;
3489 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003490 /* IO end_io complete, convert the filled extent to written */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003491 if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003492 ret = ext4_convert_unwritten_extents_endio(handle, inode,
Mingming Cao00314622009-09-28 15:49:08 -04003493 path);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003494 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05003495 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05003496 err = check_eofblocks_fl(handle, inode, map->m_lblk,
3497 path, map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003498 } else
3499 err = ret;
Mingming Cao00314622009-09-28 15:49:08 -04003500 goto out2;
3501 }
3502 /* buffered IO case */
3503 /*
3504 * repeat fallocate creation request
3505 * we already have an unwritten extent
3506 */
3507 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3508 goto map_out;
3509
3510 /* buffered READ or buffered write_begin() lookup */
3511 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3512 /*
3513 * We have blocks reserved already. We
3514 * return allocated blocks so that delalloc
3515 * won't do block reservation for us. But
3516 * the buffer head will be unmapped so that
3517 * a read from the block returns 0s.
3518 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003519 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003520 goto out1;
3521 }
3522
3523 /* buffered write, writepage time, convert*/
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003524 ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003525 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05003526 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05003527 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
3528 map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003529 if (err < 0)
3530 goto out2;
3531 }
3532
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;
Mingming Cao00314622009-09-28 15:49:08 -04003573out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003574 if (allocated > map->m_len)
3575 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003576 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003577 map->m_pblk = newblock;
3578 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04003579out2:
3580 if (path) {
3581 ext4_ext_drop_refs(path);
3582 kfree(path);
3583 }
3584 return err ? err : allocated;
3585}
Theodore Ts'o58590b02010-10-27 21:23:12 -04003586
Mingming Cao00314622009-09-28 15:49:08 -04003587/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003588 * get_implied_cluster_alloc - check to see if the requested
3589 * allocation (in the map structure) overlaps with a cluster already
3590 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04003591 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003592 * @map The requested lblk->pblk mapping
3593 * @ex The extent structure which might contain an implied
3594 * cluster allocation
3595 *
3596 * This function is called by ext4_ext_map_blocks() after we failed to
3597 * find blocks that were already in the inode's extent tree. Hence,
3598 * we know that the beginning of the requested region cannot overlap
3599 * the extent from the inode's extent tree. There are three cases we
3600 * want to catch. The first is this case:
3601 *
3602 * |--- cluster # N--|
3603 * |--- extent ---| |---- requested region ---|
3604 * |==========|
3605 *
3606 * The second case that we need to test for is this one:
3607 *
3608 * |--------- cluster # N ----------------|
3609 * |--- requested region --| |------- extent ----|
3610 * |=======================|
3611 *
3612 * The third case is when the requested region lies between two extents
3613 * within the same cluster:
3614 * |------------- cluster # N-------------|
3615 * |----- ex -----| |---- ex_right ----|
3616 * |------ requested region ------|
3617 * |================|
3618 *
3619 * In each of the above cases, we need to set the map->m_pblk and
3620 * map->m_len so it corresponds to the return the extent labelled as
3621 * "|====|" from cluster #N, since it is already in use for data in
3622 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
3623 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
3624 * as a new "allocated" block region. Otherwise, we will return 0 and
3625 * ext4_ext_map_blocks() will then allocate one or more new clusters
3626 * by calling ext4_mb_new_blocks().
3627 */
Aditya Kalid8990242011-09-09 19:18:51 -04003628static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003629 struct ext4_map_blocks *map,
3630 struct ext4_extent *ex,
3631 struct ext4_ext_path *path)
3632{
Aditya Kalid8990242011-09-09 19:18:51 -04003633 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003634 ext4_lblk_t c_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
3635 ext4_lblk_t ex_cluster_start, ex_cluster_end;
3636 ext4_lblk_t rr_cluster_start, rr_cluster_end;
3637 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
3638 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
3639 unsigned short ee_len = ext4_ext_get_actual_len(ex);
3640
3641 /* The extent passed in that we are trying to match */
3642 ex_cluster_start = EXT4_B2C(sbi, ee_block);
3643 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
3644
3645 /* The requested region passed into ext4_map_blocks() */
3646 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
3647 rr_cluster_end = EXT4_B2C(sbi, map->m_lblk + map->m_len - 1);
3648
3649 if ((rr_cluster_start == ex_cluster_end) ||
3650 (rr_cluster_start == ex_cluster_start)) {
3651 if (rr_cluster_start == ex_cluster_end)
3652 ee_start += ee_len - 1;
3653 map->m_pblk = (ee_start & ~(sbi->s_cluster_ratio - 1)) +
3654 c_offset;
3655 map->m_len = min(map->m_len,
3656 (unsigned) sbi->s_cluster_ratio - c_offset);
3657 /*
3658 * Check for and handle this case:
3659 *
3660 * |--------- cluster # N-------------|
3661 * |------- extent ----|
3662 * |--- requested region ---|
3663 * |===========|
3664 */
3665
3666 if (map->m_lblk < ee_block)
3667 map->m_len = min(map->m_len, ee_block - map->m_lblk);
3668
3669 /*
3670 * Check for the case where there is already another allocated
3671 * block to the right of 'ex' but before the end of the cluster.
3672 *
3673 * |------------- cluster # N-------------|
3674 * |----- ex -----| |---- ex_right ----|
3675 * |------ requested region ------|
3676 * |================|
3677 */
3678 if (map->m_lblk > ee_block) {
3679 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
3680 map->m_len = min(map->m_len, next - map->m_lblk);
3681 }
Aditya Kalid8990242011-09-09 19:18:51 -04003682
3683 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003684 return 1;
3685 }
Aditya Kalid8990242011-09-09 19:18:51 -04003686
3687 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003688 return 0;
3689}
3690
3691
3692/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05003693 * Block allocation/map/preallocation routine for extents based files
3694 *
3695 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003696 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003697 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3698 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05003699 *
3700 * return > 0, number of of blocks already mapped/allocated
3701 * if create == 0 and these are pre-allocated blocks
3702 * buffer head is unmapped
3703 * otherwise blocks are mapped
3704 *
3705 * return = 0, if plain look up failed (blocks have not been allocated)
3706 * buffer head is unmapped
3707 *
3708 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003709 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003710int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3711 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07003712{
3713 struct ext4_ext_path *path = NULL;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003714 struct ext4_extent newex, *ex, *ex2;
3715 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003716 ext4_fsblk_t newblock = 0;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003717 int free_on_err = 0, err = 0, depth, ret;
3718 unsigned int allocated = 0, offset = 0;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003719 unsigned int allocated_clusters = 0, reserved_clusters = 0;
Allison Hendersone8613042011-05-25 07:41:46 -04003720 unsigned int punched_out = 0;
3721 unsigned int result = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003722 struct ext4_allocation_request ar;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003723 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003724 ext4_lblk_t cluster_offset;
Allison Hendersone8613042011-05-25 07:41:46 -04003725 struct ext4_map_blocks punch_map;
Alex Tomasa86c6182006-10-11 01:21:03 -07003726
Mingming84fe3be2009-09-01 08:44:37 -04003727 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003728 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003729 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07003730
3731 /* check in cache */
Robin Dong015861b2011-07-17 23:27:43 -04003732 if (!(flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) &&
3733 ext4_ext_in_cache(inode, map->m_lblk, &newex)) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003734 if (!newex.ee_start_lo && !newex.ee_start_hi) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04003735 if ((sbi->s_cluster_ratio > 1) &&
3736 ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
3737 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3738
Theodore Ts'oc2177052009-05-14 00:58:52 -04003739 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003740 /*
3741 * block isn't allocated yet and
3742 * user doesn't want to allocate it
3743 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003744 goto out2;
3745 }
3746 /* we should allocate requested block */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003747 } else {
Alex Tomasa86c6182006-10-11 01:21:03 -07003748 /* block is already allocated */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003749 if (sbi->s_cluster_ratio > 1)
3750 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003751 newblock = map->m_lblk
Dave Kleikamp8c55e202007-05-24 13:04:54 -04003752 - le32_to_cpu(newex.ee_block)
Theodore Ts'obf89d162010-10-27 21:30:14 -04003753 + ext4_ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003754 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003755 allocated = ext4_ext_get_actual_len(&newex) -
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003756 (map->m_lblk - le32_to_cpu(newex.ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -07003757 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07003758 }
3759 }
3760
3761 /* find extent for this block */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003762 path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07003763 if (IS_ERR(path)) {
3764 err = PTR_ERR(path);
3765 path = NULL;
3766 goto out2;
3767 }
3768
3769 depth = ext_depth(inode);
3770
3771 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003772 * consistent leaf must not be empty;
3773 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07003774 * this is why assert can't be put in ext4_ext_find_extent()
3775 */
Frank Mayhar273df552010-03-02 11:46:09 -05003776 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
3777 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04003778 "lblock: %lu, depth: %d pblock %lld",
3779 (unsigned long) map->m_lblk, depth,
3780 path[depth].p_block);
Surbhi Palande034fb4c2009-12-14 09:53:52 -05003781 err = -EIO;
3782 goto out2;
3783 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003784
Avantika Mathur7e028972006-12-06 20:41:33 -08003785 ex = path[depth].p_ext;
3786 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003787 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003788 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003789 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003790
3791 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003792 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04003793 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003794 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003795 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04003796
3797 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
3798
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003799 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003800 if (in_range(map->m_lblk, ee_block, ee_len)) {
Theodore Ts'o0aa06002011-09-09 18:54:51 -04003801 ext4_fsblk_t partial_cluster = 0;
3802
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003803 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003804 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003805 allocated = ee_len - (map->m_lblk - ee_block);
3806 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
3807 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04003808
Allison Hendersone8613042011-05-25 07:41:46 -04003809 if ((flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) == 0) {
3810 /*
3811 * Do not put uninitialized extent
3812 * in the cache
3813 */
3814 if (!ext4_ext_is_uninitialized(ex)) {
3815 ext4_ext_put_in_cache(inode, ee_block,
3816 ee_len, ee_start);
3817 goto out;
3818 }
3819 ret = ext4_ext_handle_uninitialized_extents(
3820 handle, inode, map, path, flags,
3821 allocated, newblock);
3822 return ret;
Amit Arora56055d32007-07-17 21:42:38 -04003823 }
Allison Hendersone8613042011-05-25 07:41:46 -04003824
3825 /*
3826 * Punch out the map length, but only to the
3827 * end of the extent
3828 */
3829 punched_out = allocated < map->m_len ?
3830 allocated : map->m_len;
3831
3832 /*
3833 * Sense extents need to be converted to
3834 * uninitialized, they must fit in an
3835 * uninitialized extent
3836 */
3837 if (punched_out > EXT_UNINIT_MAX_LEN)
3838 punched_out = EXT_UNINIT_MAX_LEN;
3839
3840 punch_map.m_lblk = map->m_lblk;
3841 punch_map.m_pblk = newblock;
3842 punch_map.m_len = punched_out;
3843 punch_map.m_flags = 0;
3844
3845 /* Check to see if the extent needs to be split */
3846 if (punch_map.m_len != ee_len ||
3847 punch_map.m_lblk != ee_block) {
3848
3849 ret = ext4_split_extent(handle, inode,
3850 path, &punch_map, 0,
3851 EXT4_GET_BLOCKS_PUNCH_OUT_EXT |
3852 EXT4_GET_BLOCKS_PRE_IO);
3853
3854 if (ret < 0) {
3855 err = ret;
3856 goto out2;
3857 }
3858 /*
3859 * find extent for the block at
3860 * the start of the hole
3861 */
3862 ext4_ext_drop_refs(path);
3863 kfree(path);
3864
3865 path = ext4_ext_find_extent(inode,
3866 map->m_lblk, NULL);
3867 if (IS_ERR(path)) {
3868 err = PTR_ERR(path);
3869 path = NULL;
3870 goto out2;
3871 }
3872
3873 depth = ext_depth(inode);
3874 ex = path[depth].p_ext;
3875 ee_len = ext4_ext_get_actual_len(ex);
3876 ee_block = le32_to_cpu(ex->ee_block);
3877 ee_start = ext4_ext_pblock(ex);
3878
3879 }
3880
3881 ext4_ext_mark_uninitialized(ex);
3882
Allison Hendersonf7d0d372011-07-17 23:17:02 -04003883 ext4_ext_invalidate_cache(inode);
3884
3885 err = ext4_ext_rm_leaf(handle, inode, path,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04003886 &partial_cluster, map->m_lblk,
3887 map->m_lblk + punched_out);
Allison Hendersonf7d0d372011-07-17 23:17:02 -04003888
3889 if (!err && path->p_hdr->eh_entries == 0) {
3890 /*
3891 * Punch hole freed all of this sub tree,
3892 * so we need to correct eh_depth
3893 */
3894 err = ext4_ext_get_access(handle, inode, path);
3895 if (err == 0) {
3896 ext_inode_hdr(inode)->eh_depth = 0;
3897 ext_inode_hdr(inode)->eh_max =
3898 cpu_to_le16(ext4_ext_space_root(
3899 inode, 0));
3900
3901 err = ext4_ext_dirty(
3902 handle, inode, path);
3903 }
3904 }
Allison Hendersone8613042011-05-25 07:41:46 -04003905
3906 goto out2;
Alex Tomasa86c6182006-10-11 01:21:03 -07003907 }
3908 }
3909
Aditya Kali7b415bf2011-09-09 19:04:51 -04003910 if ((sbi->s_cluster_ratio > 1) &&
3911 ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
3912 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3913
Alex Tomasa86c6182006-10-11 01:21:03 -07003914 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003915 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07003916 * we couldn't try to create block if create flag is zero
3917 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04003918 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003919 /*
3920 * put just found gap into cache to speed up
3921 * subsequent requests
3922 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003923 ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
Alex Tomasa86c6182006-10-11 01:21:03 -07003924 goto out2;
3925 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003926
Alex Tomasa86c6182006-10-11 01:21:03 -07003927 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003928 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07003929 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003930 map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003931 newex.ee_block = cpu_to_le32(map->m_lblk);
3932 cluster_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
3933
3934 /*
3935 * If we are doing bigalloc, check to see if the extent returned
3936 * by ext4_ext_find_extent() implies a cluster we can use.
3937 */
3938 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04003939 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003940 ar.len = allocated = map->m_len;
3941 newblock = map->m_pblk;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003942 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003943 goto got_allocated_blocks;
3944 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003945
Alex Tomasc9de5602008-01-29 00:19:52 -05003946 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003947 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003948 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3949 if (err)
3950 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003951 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003952 ex2 = NULL;
3953 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
Alex Tomasc9de5602008-01-29 00:19:52 -05003954 if (err)
3955 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04003956
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003957 /* Check if the extent after searching to the right implies a
3958 * cluster we can use. */
3959 if ((sbi->s_cluster_ratio > 1) && ex2 &&
Aditya Kalid8990242011-09-09 19:18:51 -04003960 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003961 ar.len = allocated = map->m_len;
3962 newblock = map->m_pblk;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003963 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003964 goto got_allocated_blocks;
3965 }
3966
Amit Arora749269f2007-07-18 09:02:56 -04003967 /*
3968 * See if request is beyond maximum number of blocks we can have in
3969 * a single extent. For an initialized extent this limit is
3970 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3971 * EXT_UNINIT_MAX_LEN.
3972 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003973 if (map->m_len > EXT_INIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003974 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003975 map->m_len = EXT_INIT_MAX_LEN;
3976 else if (map->m_len > EXT_UNINIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003977 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003978 map->m_len = EXT_UNINIT_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04003979
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003980 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003981 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003982 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04003983 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003984 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04003985 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003986 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003987
3988 /* allocate new block */
3989 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003990 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
3991 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003992 /*
3993 * We calculate the offset from the beginning of the cluster
3994 * for the logical block number, since when we allocate a
3995 * physical cluster, the physical block should start at the
3996 * same offset from the beginning of the cluster. This is
3997 * needed so that future calls to get_implied_cluster_alloc()
3998 * work correctly.
3999 */
4000 offset = map->m_lblk & (sbi->s_cluster_ratio - 1);
4001 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
4002 ar.goal -= offset;
4003 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05004004 if (S_ISREG(inode->i_mode))
4005 ar.flags = EXT4_MB_HINT_DATA;
4006 else
4007 /* disable in-core preallocation for non-regular files */
4008 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04004009 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4010 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05004011 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004012 if (!newblock)
4013 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04004014 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004015 ar.goal, newblock, allocated);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004016 free_on_err = 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004017 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004018 ar.len = EXT4_C2B(sbi, ar.len) - offset;
4019 if (ar.len > allocated)
4020 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004021
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004022got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07004023 /* try to insert new extent into found leaf and return */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004024 ext4_ext_store_pblock(&newex, newblock + offset);
Alex Tomasc9de5602008-01-29 00:19:52 -05004025 newex.ee_len = cpu_to_le16(ar.len);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004026 /* Mark uninitialized */
4027 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
Amit Aroraa2df2a62007-07-17 21:42:41 -04004028 ext4_ext_mark_uninitialized(&newex);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004029 /*
Jiaying Zhang744692d2010-03-04 16:14:02 -05004030 * io_end structure was created for every IO write to an
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004031 * uninitialized extent. To avoid unnecessary conversion,
Jiaying Zhang744692d2010-03-04 16:14:02 -05004032 * here we flag the IO that really needs the conversion.
Mingming5f524952009-11-10 10:48:04 -05004033 * For non asycn direct IO case, flag the inode state
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004034 * that we need to perform conversion when IO is done.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004035 */
Jiaying Zhang744692d2010-03-04 16:14:02 -05004036 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Eric Sandeene9e3bce2011-02-12 08:17:34 -05004037 if (io && !(io->flag & EXT4_IO_END_UNWRITTEN)) {
Theodore Ts'obd2d0212010-10-27 21:30:10 -04004038 io->flag = EXT4_IO_END_UNWRITTEN;
Eric Sandeene9e3bce2011-02-12 08:17:34 -05004039 atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten);
4040 } else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004041 ext4_set_inode_state(inode,
4042 EXT4_STATE_DIO_UNWRITTEN);
Mingming5f524952009-11-10 10:48:04 -05004043 }
Jiaying Zhang744692d2010-03-04 16:14:02 -05004044 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004045 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004046 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004047
Eric Sandeend002ebf2011-01-10 13:03:35 -05004048 err = check_eofblocks_fl(handle, inode, map->m_lblk, path, ar.len);
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004049 if (!err)
4050 err = ext4_ext_insert_extent(handle, inode, path,
4051 &newex, flags);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004052 if (err && free_on_err) {
Maxim Patlasov7132de72011-07-10 19:37:48 -04004053 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
4054 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
Alex Tomas315054f2007-05-24 13:04:25 -04004055 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05004056 /* not a good idea to call discard here directly,
4057 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004058 ext4_discard_preallocations(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05004059 ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex),
Maxim Patlasov7132de72011-07-10 19:37:48 -04004060 ext4_ext_get_actual_len(&newex), fb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004061 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04004062 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004063
Alex Tomasa86c6182006-10-11 01:21:03 -07004064 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04004065 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004066 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004067 if (allocated > map->m_len)
4068 allocated = map->m_len;
4069 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07004070
Jan Karab436b9b2009-12-08 23:51:10 -05004071 /*
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004072 * Update reserved blocks/metadata blocks after successful
4073 * block allocation which had been deferred till now.
4074 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04004075 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
4076 /*
4077 * Check how many clusters we had reserved this allocted range.
4078 */
4079 reserved_clusters = get_reserved_cluster_alloc(inode,
4080 map->m_lblk, allocated);
4081 if (map->m_flags & EXT4_MAP_FROM_CLUSTER) {
4082 if (reserved_clusters) {
4083 /*
4084 * We have clusters reserved for this range.
4085 * But since we are not doing actual allocation
4086 * and are simply using blocks from previously
4087 * allocated cluster, we should release the
4088 * reservation and not claim quota.
4089 */
4090 ext4_da_update_reserve_space(inode,
4091 reserved_clusters, 0);
4092 }
4093 } else {
4094 BUG_ON(allocated_clusters < reserved_clusters);
4095 /* We will claim quota for all newly allocated blocks.*/
4096 ext4_da_update_reserve_space(inode, allocated_clusters,
4097 1);
4098 if (reserved_clusters < allocated_clusters) {
Aditya Kali5356f262011-09-09 19:20:51 -04004099 struct ext4_inode_info *ei = EXT4_I(inode);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004100 int reservation = allocated_clusters -
4101 reserved_clusters;
4102 /*
4103 * It seems we claimed few clusters outside of
4104 * the range of this allocation. We should give
4105 * it back to the reservation pool. This can
4106 * happen in the following case:
4107 *
4108 * * Suppose s_cluster_ratio is 4 (i.e., each
4109 * cluster has 4 blocks. Thus, the clusters
4110 * are [0-3],[4-7],[8-11]...
4111 * * First comes delayed allocation write for
4112 * logical blocks 10 & 11. Since there were no
4113 * previous delayed allocated blocks in the
4114 * range [8-11], we would reserve 1 cluster
4115 * for this write.
4116 * * Next comes write for logical blocks 3 to 8.
4117 * In this case, we will reserve 2 clusters
4118 * (for [0-3] and [4-7]; and not for [8-11] as
4119 * that range has a delayed allocated blocks.
4120 * Thus total reserved clusters now becomes 3.
4121 * * Now, during the delayed allocation writeout
4122 * time, we will first write blocks [3-8] and
4123 * allocate 3 clusters for writing these
4124 * blocks. Also, we would claim all these
4125 * three clusters above.
4126 * * Now when we come here to writeout the
4127 * blocks [10-11], we would expect to claim
4128 * the reservation of 1 cluster we had made
4129 * (and we would claim it since there are no
4130 * more delayed allocated blocks in the range
4131 * [8-11]. But our reserved cluster count had
4132 * already gone to 0.
4133 *
4134 * Thus, at the step 4 above when we determine
4135 * that there are still some unwritten delayed
4136 * allocated blocks outside of our current
4137 * block range, we should increment the
4138 * reserved clusters count so that when the
4139 * remaining blocks finally gets written, we
4140 * could claim them.
4141 */
Aditya Kali5356f262011-09-09 19:20:51 -04004142 dquot_reserve_block(inode,
4143 EXT4_C2B(sbi, reservation));
4144 spin_lock(&ei->i_block_reservation_lock);
4145 ei->i_reserved_data_blocks += reservation;
4146 spin_unlock(&ei->i_block_reservation_lock);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004147 }
4148 }
4149 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004150
4151 /*
Jan Karab436b9b2009-12-08 23:51:10 -05004152 * Cache the extent and update transaction to commit on fdatasync only
4153 * when it is _not_ an uninitialized extent.
4154 */
4155 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05004156 ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock);
Jan Karab436b9b2009-12-08 23:51:10 -05004157 ext4_update_inode_fsync_trans(handle, inode, 1);
4158 } else
4159 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004160out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004161 if (allocated > map->m_len)
4162 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004163 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004164 map->m_flags |= EXT4_MAP_MAPPED;
4165 map->m_pblk = newblock;
4166 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004167out2:
4168 if (path) {
4169 ext4_ext_drop_refs(path);
4170 kfree(path);
4171 }
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004172 trace_ext4_ext_map_blocks_exit(inode, map->m_lblk,
4173 newblock, map->m_len, err ? err : allocated);
Allison Hendersone8613042011-05-25 07:41:46 -04004174
4175 result = (flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) ?
4176 punched_out : allocated;
4177
4178 return err ? err : result;
Alex Tomasa86c6182006-10-11 01:21:03 -07004179}
4180
Jan Karacf108bc2008-07-11 19:27:31 -04004181void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004182{
4183 struct address_space *mapping = inode->i_mapping;
4184 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004185 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004186 handle_t *handle;
Allison Henderson189e8682011-09-06 21:49:44 -04004187 loff_t page_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004188 int err = 0;
4189
4190 /*
Jiaying Zhang3889fd52011-01-10 12:47:05 -05004191 * finish any pending end_io work so we won't run the risk of
4192 * converting any truncated blocks to initialized later
4193 */
4194 ext4_flush_completed_IO(inode);
4195
4196 /*
Alex Tomasa86c6182006-10-11 01:21:03 -07004197 * probably first extent we're gonna free will be last in block
4198 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04004199 err = ext4_writepage_trans_blocks(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07004200 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04004201 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07004202 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07004203
Allison Henderson189e8682011-09-06 21:49:44 -04004204 if (inode->i_size % PAGE_CACHE_SIZE != 0) {
4205 page_len = PAGE_CACHE_SIZE -
4206 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4207
4208 err = ext4_discard_partial_page_buffers(handle,
4209 mapping, inode->i_size, page_len, 0);
4210
4211 if (err)
4212 goto out_stop;
4213 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004214
Jan Kara9ddfc3d2008-07-11 19:27:31 -04004215 if (ext4_orphan_add(handle, inode))
4216 goto out_stop;
4217
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004218 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07004219 ext4_ext_invalidate_cache(inode);
4220
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004221 ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05004222
Alex Tomasa86c6182006-10-11 01:21:03 -07004223 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004224 * TODO: optimization is possible here.
4225 * Probably we need not scan at all,
4226 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004227 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004228
4229 /* we have to know where to truncate from in crash case */
4230 EXT4_I(inode)->i_disksize = inode->i_size;
4231 ext4_mark_inode_dirty(handle, inode);
4232
4233 last_block = (inode->i_size + sb->s_blocksize - 1)
4234 >> EXT4_BLOCK_SIZE_BITS(sb);
Allison Hendersonc6a03712011-07-17 23:21:03 -04004235 err = ext4_ext_remove_space(inode, last_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07004236
4237 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04004238 * transaction synchronous.
4239 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004240 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05004241 ext4_handle_sync(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07004242
Jan Kara9ddfc3d2008-07-11 19:27:31 -04004243 up_write(&EXT4_I(inode)->i_data_sem);
Eric Gouriouf6d2f6b2011-05-22 21:33:00 -04004244
4245out_stop:
Alex Tomasa86c6182006-10-11 01:21:03 -07004246 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004247 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07004248 * then we need to clear up the orphan record which we created above.
4249 * However, if this was a real unlink then we were called by
4250 * ext4_delete_inode(), and we allow that function to clean up the
4251 * orphan info for us.
4252 */
4253 if (inode->i_nlink)
4254 ext4_orphan_del(handle, inode);
4255
Solofo Ramangalahyef737722008-04-29 22:00:41 -04004256 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4257 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07004258 ext4_journal_stop(handle);
4259}
4260
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004261static void ext4_falloc_update_inode(struct inode *inode,
4262 int mode, loff_t new_size, int update_ctime)
4263{
4264 struct timespec now;
4265
4266 if (update_ctime) {
4267 now = current_fs_time(inode->i_sb);
4268 if (!timespec_equal(&inode->i_ctime, &now))
4269 inode->i_ctime = now;
4270 }
4271 /*
4272 * Update only when preallocation was requested beyond
4273 * the file size.
4274 */
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04004275 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
4276 if (new_size > i_size_read(inode))
4277 i_size_write(inode, new_size);
4278 if (new_size > EXT4_I(inode)->i_disksize)
4279 ext4_update_i_disksize(inode, new_size);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004280 } else {
4281 /*
4282 * Mark that we allocate beyond EOF so the subsequent truncate
4283 * can proceed even if the new size is the same as i_size.
4284 */
4285 if (new_size > i_size_read(inode))
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004286 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004287 }
4288
4289}
4290
Amit Aroraa2df2a62007-07-17 21:42:41 -04004291/*
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004292 * preallocate space for a file. This implements ext4's fallocate file
Amit Aroraa2df2a62007-07-17 21:42:41 -04004293 * operation, which gets called from sys_fallocate system call.
4294 * For block-mapped files, posix_fallocate should fall back to the method
4295 * of writing zeroes to the required new blocks (the same behavior which is
4296 * expected for file systems which do not support fallocate() system call).
4297 */
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004298long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004299{
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004300 struct inode *inode = file->f_path.dentry->d_inode;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004301 handle_t *handle;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004302 loff_t new_size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004303 unsigned int max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004304 int ret = 0;
4305 int ret2 = 0;
4306 int retries = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004307 struct ext4_map_blocks map;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004308 unsigned int credits, blkbits = inode->i_blkbits;
4309
4310 /*
4311 * currently supporting (pre)allocate mode for extent-based
4312 * files _only_
4313 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004314 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Amit Aroraa2df2a62007-07-17 21:42:41 -04004315 return -EOPNOTSUPP;
4316
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004317 /* Return error if mode is not supported */
4318 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
4319 return -EOPNOTSUPP;
4320
4321 if (mode & FALLOC_FL_PUNCH_HOLE)
4322 return ext4_punch_hole(file, offset, len);
4323
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004324 trace_ext4_fallocate_enter(inode, offset, len, mode);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004325 map.m_lblk = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004326 /*
4327 * We can't just convert len to max_blocks because
4328 * If blocksize = 4096 offset = 3072 and len = 2048
4329 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004330 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004331 - map.m_lblk;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004332 /*
Mingming Caof3bd1f32008-08-19 22:16:03 -04004333 * credits to insert 1 extent into extent tree
Amit Aroraa2df2a62007-07-17 21:42:41 -04004334 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04004335 credits = ext4_chunk_trans_blocks(inode, max_blocks);
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05004336 mutex_lock(&inode->i_mutex);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04004337 ret = inode_newsize_ok(inode, (len + offset));
4338 if (ret) {
4339 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004340 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04004341 return ret;
4342 }
Amit Aroraa2df2a62007-07-17 21:42:41 -04004343retry:
4344 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004345 map.m_lblk = map.m_lblk + ret;
4346 map.m_len = max_blocks = max_blocks - ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004347 handle = ext4_journal_start(inode, credits);
4348 if (IS_ERR(handle)) {
4349 ret = PTR_ERR(handle);
4350 break;
4351 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004352 ret = ext4_map_blocks(handle, inode, &map,
Vivek Haldar556b27a2011-05-25 07:41:54 -04004353 EXT4_GET_BLOCKS_CREATE_UNINIT_EXT |
4354 EXT4_GET_BLOCKS_NO_NORMALIZE);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05004355 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004356#ifdef EXT4FS_DEBUG
4357 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004358 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004359 "returned error inode#%lu, block=%u, "
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05004360 "max_blocks=%u", __func__,
Kazuya Mioa6371b62010-10-27 21:30:15 -04004361 inode->i_ino, map.m_lblk, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004362#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04004363 ext4_mark_inode_dirty(handle, inode);
4364 ret2 = ext4_journal_stop(handle);
4365 break;
4366 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004367 if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004368 blkbits) >> blkbits))
4369 new_size = offset + len;
4370 else
Utako Kusaka29ae07b2011-07-27 22:11:20 -04004371 new_size = ((loff_t) map.m_lblk + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004372
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004373 ext4_falloc_update_inode(inode, mode, new_size,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004374 (map.m_flags & EXT4_MAP_NEW));
Amit Aroraa2df2a62007-07-17 21:42:41 -04004375 ext4_mark_inode_dirty(handle, inode);
4376 ret2 = ext4_journal_stop(handle);
4377 if (ret2)
4378 break;
4379 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004380 if (ret == -ENOSPC &&
4381 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4382 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004383 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004384 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05004385 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004386 trace_ext4_fallocate_exit(inode, offset, max_blocks,
4387 ret > 0 ? ret2 : ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004388 return ret > 0 ? ret2 : ret;
4389}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004390
4391/*
Mingming Cao00314622009-09-28 15:49:08 -04004392 * This function convert a range of blocks to written extents
4393 * The caller of this function will pass the start offset and the size.
4394 * all unwritten extents within this range will be converted to
4395 * written extents.
4396 *
4397 * This function is called from the direct IO end io call back
4398 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05004399 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04004400 */
4401int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
Eric Sandeena1de02d2010-02-04 23:58:38 -05004402 ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04004403{
4404 handle_t *handle;
Mingming Cao00314622009-09-28 15:49:08 -04004405 unsigned int max_blocks;
4406 int ret = 0;
4407 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004408 struct ext4_map_blocks map;
Mingming Cao00314622009-09-28 15:49:08 -04004409 unsigned int credits, blkbits = inode->i_blkbits;
4410
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004411 map.m_lblk = offset >> blkbits;
Mingming Cao00314622009-09-28 15:49:08 -04004412 /*
4413 * We can't just convert len to max_blocks because
4414 * If blocksize = 4096 offset = 3072 and len = 2048
4415 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004416 max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
4417 map.m_lblk);
Mingming Cao00314622009-09-28 15:49:08 -04004418 /*
4419 * credits to insert 1 extent into extent tree
4420 */
4421 credits = ext4_chunk_trans_blocks(inode, max_blocks);
4422 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004423 map.m_lblk += ret;
4424 map.m_len = (max_blocks -= ret);
Mingming Cao00314622009-09-28 15:49:08 -04004425 handle = ext4_journal_start(inode, credits);
4426 if (IS_ERR(handle)) {
4427 ret = PTR_ERR(handle);
4428 break;
4429 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004430 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004431 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Mingming Cao00314622009-09-28 15:49:08 -04004432 if (ret <= 0) {
4433 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004434 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Mingming Cao00314622009-09-28 15:49:08 -04004435 "returned error inode#%lu, block=%u, "
4436 "max_blocks=%u", __func__,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004437 inode->i_ino, map.m_lblk, map.m_len);
Mingming Cao00314622009-09-28 15:49:08 -04004438 }
4439 ext4_mark_inode_dirty(handle, inode);
4440 ret2 = ext4_journal_stop(handle);
4441 if (ret <= 0 || ret2 )
4442 break;
4443 }
4444 return ret > 0 ? ret2 : ret;
4445}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004446
Mingming Cao00314622009-09-28 15:49:08 -04004447/*
Eric Sandeen6873fa02008-10-07 00:46:36 -04004448 * Callback function called for each extent to gather FIEMAP information.
4449 */
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04004450static int ext4_ext_fiemap_cb(struct inode *inode, ext4_lblk_t next,
Eric Sandeen6873fa02008-10-07 00:46:36 -04004451 struct ext4_ext_cache *newex, struct ext4_extent *ex,
4452 void *data)
4453{
Eric Sandeen6873fa02008-10-07 00:46:36 -04004454 __u64 logical;
4455 __u64 physical;
4456 __u64 length;
4457 __u32 flags = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004458 int ret = 0;
4459 struct fiemap_extent_info *fieinfo = data;
4460 unsigned char blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004461
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004462 blksize_bits = inode->i_sb->s_blocksize_bits;
4463 logical = (__u64)newex->ec_block << blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004464
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05004465 if (newex->ec_start == 0) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004466 /*
4467 * No extent in extent-tree contains block @newex->ec_start,
4468 * then the block may stay in 1)a hole or 2)delayed-extent.
4469 *
4470 * Holes or delayed-extents are processed as follows.
4471 * 1. lookup dirty pages with specified range in pagecache.
4472 * If no page is got, then there is no delayed-extent and
4473 * return with EXT_CONTINUE.
4474 * 2. find the 1st mapped buffer,
4475 * 3. check if the mapped buffer is both in the request range
4476 * and a delayed buffer. If not, there is no delayed-extent,
4477 * then return.
4478 * 4. a delayed-extent is found, the extent will be collected.
4479 */
4480 ext4_lblk_t end = 0;
4481 pgoff_t last_offset;
4482 pgoff_t offset;
4483 pgoff_t index;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004484 pgoff_t start_index = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004485 struct page **pages = NULL;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004486 struct buffer_head *bh = NULL;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004487 struct buffer_head *head = NULL;
4488 unsigned int nr_pages = PAGE_SIZE / sizeof(struct page *);
4489
4490 pages = kmalloc(PAGE_SIZE, GFP_KERNEL);
4491 if (pages == NULL)
4492 return -ENOMEM;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004493
4494 offset = logical >> PAGE_SHIFT;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004495repeat:
4496 last_offset = offset;
4497 head = NULL;
4498 ret = find_get_pages_tag(inode->i_mapping, &offset,
4499 PAGECACHE_TAG_DIRTY, nr_pages, pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004500
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004501 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4502 /* First time, try to find a mapped buffer. */
4503 if (ret == 0) {
4504out:
4505 for (index = 0; index < ret; index++)
4506 page_cache_release(pages[index]);
4507 /* just a hole. */
4508 kfree(pages);
4509 return EXT_CONTINUE;
4510 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004511 index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004512
Yongqiang Yangb2213492011-05-24 11:36:58 -04004513next_page:
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004514 /* Try to find the 1st mapped buffer. */
Yongqiang Yangb2213492011-05-24 11:36:58 -04004515 end = ((__u64)pages[index]->index << PAGE_SHIFT) >>
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004516 blksize_bits;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004517 if (!page_has_buffers(pages[index]))
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004518 goto out;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004519 head = page_buffers(pages[index]);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004520 if (!head)
4521 goto out;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004522
Yongqiang Yangb2213492011-05-24 11:36:58 -04004523 index++;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004524 bh = head;
4525 do {
Yongqiang Yangb2213492011-05-24 11:36:58 -04004526 if (end >= newex->ec_block +
4527 newex->ec_len)
4528 /* The buffer is out of
4529 * the request range.
4530 */
4531 goto out;
4532
4533 if (buffer_mapped(bh) &&
4534 end >= newex->ec_block) {
4535 start_index = index - 1;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004536 /* get the 1st mapped buffer. */
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004537 goto found_mapped_buffer;
4538 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004539
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004540 bh = bh->b_this_page;
4541 end++;
4542 } while (bh != head);
4543
Yongqiang Yangb2213492011-05-24 11:36:58 -04004544 /* No mapped buffer in the range found in this page,
4545 * We need to look up next page.
4546 */
4547 if (index >= ret) {
4548 /* There is no page left, but we need to limit
4549 * newex->ec_len.
4550 */
4551 newex->ec_len = end - newex->ec_block;
4552 goto out;
4553 }
4554 goto next_page;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004555 } else {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004556 /*Find contiguous delayed buffers. */
4557 if (ret > 0 && pages[0]->index == last_offset)
4558 head = page_buffers(pages[0]);
4559 bh = head;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004560 index = 1;
4561 start_index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004562 }
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004563
4564found_mapped_buffer:
4565 if (bh != NULL && buffer_delay(bh)) {
4566 /* 1st or contiguous delayed buffer found. */
4567 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4568 /*
4569 * 1st delayed buffer found, record
4570 * the start of extent.
4571 */
4572 flags |= FIEMAP_EXTENT_DELALLOC;
4573 newex->ec_block = end;
4574 logical = (__u64)end << blksize_bits;
4575 }
4576 /* Find contiguous delayed buffers. */
4577 do {
4578 if (!buffer_delay(bh))
4579 goto found_delayed_extent;
4580 bh = bh->b_this_page;
4581 end++;
4582 } while (bh != head);
4583
Yongqiang Yangb2213492011-05-24 11:36:58 -04004584 for (; index < ret; index++) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004585 if (!page_has_buffers(pages[index])) {
4586 bh = NULL;
4587 break;
4588 }
4589 head = page_buffers(pages[index]);
4590 if (!head) {
4591 bh = NULL;
4592 break;
4593 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004594
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004595 if (pages[index]->index !=
Yongqiang Yangb2213492011-05-24 11:36:58 -04004596 pages[start_index]->index + index
4597 - start_index) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004598 /* Blocks are not contiguous. */
4599 bh = NULL;
4600 break;
4601 }
4602 bh = head;
4603 do {
4604 if (!buffer_delay(bh))
4605 /* Delayed-extent ends. */
4606 goto found_delayed_extent;
4607 bh = bh->b_this_page;
4608 end++;
4609 } while (bh != head);
4610 }
4611 } else if (!(flags & FIEMAP_EXTENT_DELALLOC))
4612 /* a hole found. */
4613 goto out;
4614
4615found_delayed_extent:
4616 newex->ec_len = min(end - newex->ec_block,
4617 (ext4_lblk_t)EXT_INIT_MAX_LEN);
4618 if (ret == nr_pages && bh != NULL &&
4619 newex->ec_len < EXT_INIT_MAX_LEN &&
4620 buffer_delay(bh)) {
4621 /* Have not collected an extent and continue. */
4622 for (index = 0; index < ret; index++)
4623 page_cache_release(pages[index]);
4624 goto repeat;
4625 }
4626
4627 for (index = 0; index < ret; index++)
4628 page_cache_release(pages[index]);
4629 kfree(pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004630 }
4631
4632 physical = (__u64)newex->ec_start << blksize_bits;
4633 length = (__u64)newex->ec_len << blksize_bits;
4634
4635 if (ex && ext4_ext_is_uninitialized(ex))
4636 flags |= FIEMAP_EXTENT_UNWRITTEN;
4637
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04004638 if (next == EXT_MAX_BLOCKS)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004639 flags |= FIEMAP_EXTENT_LAST;
4640
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004641 ret = fiemap_fill_next_extent(fieinfo, logical, physical,
Eric Sandeen6873fa02008-10-07 00:46:36 -04004642 length, flags);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004643 if (ret < 0)
4644 return ret;
4645 if (ret == 1)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004646 return EXT_BREAK;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004647 return EXT_CONTINUE;
4648}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004649/* fiemap flags we can handle specified here */
4650#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
4651
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004652static int ext4_xattr_fiemap(struct inode *inode,
4653 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004654{
4655 __u64 physical = 0;
4656 __u64 length;
4657 __u32 flags = FIEMAP_EXTENT_LAST;
4658 int blockbits = inode->i_sb->s_blocksize_bits;
4659 int error = 0;
4660
4661 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004662 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04004663 struct ext4_iloc iloc;
4664 int offset; /* offset of xattr in inode */
4665
4666 error = ext4_get_inode_loc(inode, &iloc);
4667 if (error)
4668 return error;
4669 physical = iloc.bh->b_blocknr << blockbits;
4670 offset = EXT4_GOOD_OLD_INODE_SIZE +
4671 EXT4_I(inode)->i_extra_isize;
4672 physical += offset;
4673 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
4674 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004675 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004676 } else { /* external block */
4677 physical = EXT4_I(inode)->i_file_acl << blockbits;
4678 length = inode->i_sb->s_blocksize;
4679 }
4680
4681 if (physical)
4682 error = fiemap_fill_next_extent(fieinfo, 0, physical,
4683 length, flags);
4684 return (error < 0 ? error : 0);
4685}
4686
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004687/*
4688 * ext4_ext_punch_hole
4689 *
4690 * Punches a hole of "length" bytes in a file starting
4691 * at byte "offset"
4692 *
4693 * @inode: The inode of the file to punch a hole in
4694 * @offset: The starting byte offset of the hole
4695 * @length: The length of the hole
4696 *
4697 * Returns the number of blocks removed or negative on err
4698 */
4699int ext4_ext_punch_hole(struct file *file, loff_t offset, loff_t length)
4700{
4701 struct inode *inode = file->f_path.dentry->d_inode;
4702 struct super_block *sb = inode->i_sb;
4703 struct ext4_ext_cache cache_ex;
4704 ext4_lblk_t first_block, last_block, num_blocks, iblock, max_blocks;
4705 struct address_space *mapping = inode->i_mapping;
4706 struct ext4_map_blocks map;
4707 handle_t *handle;
Allison Hendersonba062082011-09-03 11:55:59 -04004708 loff_t first_page, last_page, page_len;
4709 loff_t first_page_offset, last_page_offset;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004710 int ret, credits, blocks_released, err = 0;
4711
Allison Henderson2be47512011-09-03 11:56:52 -04004712 /* No need to punch hole beyond i_size */
4713 if (offset >= inode->i_size)
4714 return 0;
4715
4716 /*
4717 * If the hole extends beyond i_size, set the hole
4718 * to end after the page that contains i_size
4719 */
4720 if (offset + length > inode->i_size) {
4721 length = inode->i_size +
4722 PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) -
4723 offset;
4724 }
4725
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004726 first_block = (offset + sb->s_blocksize - 1) >>
4727 EXT4_BLOCK_SIZE_BITS(sb);
4728 last_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
4729
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004730 first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
4731 last_page = (offset + length) >> PAGE_CACHE_SHIFT;
4732
4733 first_page_offset = first_page << PAGE_CACHE_SHIFT;
4734 last_page_offset = last_page << PAGE_CACHE_SHIFT;
4735
4736 /*
4737 * Write out all dirty pages to avoid race conditions
4738 * Then release them.
4739 */
4740 if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
4741 err = filemap_write_and_wait_range(mapping,
Allison Henderson2be47512011-09-03 11:56:52 -04004742 offset, offset + length - 1);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004743
Allison Henderson2be47512011-09-03 11:56:52 -04004744 if (err)
4745 return err;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004746 }
4747
4748 /* Now release the pages */
4749 if (last_page_offset > first_page_offset) {
4750 truncate_inode_pages_range(mapping, first_page_offset,
4751 last_page_offset-1);
4752 }
4753
4754 /* finish any pending end_io work */
4755 ext4_flush_completed_IO(inode);
4756
4757 credits = ext4_writepage_trans_blocks(inode);
4758 handle = ext4_journal_start(inode, credits);
4759 if (IS_ERR(handle))
4760 return PTR_ERR(handle);
4761
4762 err = ext4_orphan_add(handle, inode);
4763 if (err)
4764 goto out;
4765
4766 /*
Allison Hendersonba062082011-09-03 11:55:59 -04004767 * Now we need to zero out the non-page-aligned data in the
4768 * pages at the start and tail of the hole, and unmap the buffer
4769 * heads for the block aligned regions of the page that were
4770 * completely zeroed.
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004771 */
Allison Hendersonba062082011-09-03 11:55:59 -04004772 if (first_page > last_page) {
4773 /*
4774 * If the file space being truncated is contained within a page
4775 * just zero out and unmap the middle of that page
4776 */
4777 err = ext4_discard_partial_page_buffers(handle,
4778 mapping, offset, length, 0);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004779
Allison Hendersonba062082011-09-03 11:55:59 -04004780 if (err)
4781 goto out;
4782 } else {
4783 /*
4784 * zero out and unmap the partial page that contains
4785 * the start of the hole
4786 */
4787 page_len = first_page_offset - offset;
4788 if (page_len > 0) {
4789 err = ext4_discard_partial_page_buffers(handle, mapping,
4790 offset, page_len, 0);
4791 if (err)
4792 goto out;
4793 }
4794
4795 /*
4796 * zero out and unmap the partial page that contains
4797 * the end of the hole
4798 */
4799 page_len = offset + length - last_page_offset;
4800 if (page_len > 0) {
4801 err = ext4_discard_partial_page_buffers(handle, mapping,
4802 last_page_offset, page_len, 0);
4803 if (err)
4804 goto out;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004805 }
4806 }
4807
Allison Henderson2be47512011-09-03 11:56:52 -04004808
4809 /*
4810 * If i_size is contained in the last page, we need to
4811 * unmap and zero the partial page after i_size
4812 */
4813 if (inode->i_size >> PAGE_CACHE_SHIFT == last_page &&
4814 inode->i_size % PAGE_CACHE_SIZE != 0) {
4815
4816 page_len = PAGE_CACHE_SIZE -
4817 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4818
4819 if (page_len > 0) {
4820 err = ext4_discard_partial_page_buffers(handle,
4821 mapping, inode->i_size, page_len, 0);
4822
4823 if (err)
4824 goto out;
4825 }
4826 }
4827
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004828 /* If there are no blocks to remove, return now */
4829 if (first_block >= last_block)
4830 goto out;
4831
4832 down_write(&EXT4_I(inode)->i_data_sem);
4833 ext4_ext_invalidate_cache(inode);
4834 ext4_discard_preallocations(inode);
4835
4836 /*
4837 * Loop over all the blocks and identify blocks
4838 * that need to be punched out
4839 */
4840 iblock = first_block;
4841 blocks_released = 0;
4842 while (iblock < last_block) {
4843 max_blocks = last_block - iblock;
4844 num_blocks = 1;
4845 memset(&map, 0, sizeof(map));
4846 map.m_lblk = iblock;
4847 map.m_len = max_blocks;
4848 ret = ext4_ext_map_blocks(handle, inode, &map,
4849 EXT4_GET_BLOCKS_PUNCH_OUT_EXT);
4850
4851 if (ret > 0) {
4852 blocks_released += ret;
4853 num_blocks = ret;
4854 } else if (ret == 0) {
4855 /*
4856 * If map blocks could not find the block,
4857 * then it is in a hole. If the hole was
4858 * not already cached, then map blocks should
4859 * put it in the cache. So we can get the hole
4860 * out of the cache
4861 */
4862 memset(&cache_ex, 0, sizeof(cache_ex));
4863 if ((ext4_ext_check_cache(inode, iblock, &cache_ex)) &&
4864 !cache_ex.ec_start) {
4865
4866 /* The hole is cached */
4867 num_blocks = cache_ex.ec_block +
4868 cache_ex.ec_len - iblock;
4869
4870 } else {
4871 /* The block could not be identified */
4872 err = -EIO;
4873 break;
4874 }
4875 } else {
4876 /* Map blocks error */
4877 err = ret;
4878 break;
4879 }
4880
4881 if (num_blocks == 0) {
4882 /* This condition should never happen */
4883 ext_debug("Block lookup failed");
4884 err = -EIO;
4885 break;
4886 }
4887
4888 iblock += num_blocks;
4889 }
4890
4891 if (blocks_released > 0) {
4892 ext4_ext_invalidate_cache(inode);
4893 ext4_discard_preallocations(inode);
4894 }
4895
4896 if (IS_SYNC(inode))
4897 ext4_handle_sync(handle);
4898
4899 up_write(&EXT4_I(inode)->i_data_sem);
4900
4901out:
4902 ext4_orphan_del(handle, inode);
4903 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4904 ext4_mark_inode_dirty(handle, inode);
4905 ext4_journal_stop(handle);
4906 return err;
4907}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004908int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4909 __u64 start, __u64 len)
4910{
4911 ext4_lblk_t start_blk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004912 int error = 0;
4913
4914 /* fallback to generic here if not in extents fmt */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004915 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeen6873fa02008-10-07 00:46:36 -04004916 return generic_block_fiemap(inode, fieinfo, start, len,
4917 ext4_get_block);
4918
4919 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
4920 return -EBADR;
4921
4922 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
4923 error = ext4_xattr_fiemap(inode, fieinfo);
4924 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004925 ext4_lblk_t len_blks;
4926 __u64 last_blk;
4927
Eric Sandeen6873fa02008-10-07 00:46:36 -04004928 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004929 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04004930 if (last_blk >= EXT_MAX_BLOCKS)
4931 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004932 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004933
4934 /*
4935 * Walk the extent tree gathering extent information.
4936 * ext4_ext_fiemap_cb will push extents back to user.
4937 */
Eric Sandeen6873fa02008-10-07 00:46:36 -04004938 error = ext4_ext_walk_space(inode, start_blk, len_blks,
4939 ext4_ext_fiemap_cb, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004940 }
4941
4942 return error;
4943}