blob: 1d456b2ac377741a05876b6ce7d5b60cc67620ac [file] [log] [blame]
Alex Tomasa86c6182006-10-11 01:21:03 -07001/*
2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
4 *
5 * Architecture independence:
6 * Copyright (c) 2005, Bull S.A.
7 * Written by Pierre Peiffer <pierre.peiffer@bull.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public Licens
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
21 */
22
23/*
24 * Extents support for EXT4
25 *
26 * TODO:
27 * - ext4*_error() should be used in some situations
28 * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
29 * - smart tree reduction
30 */
31
32#include <linux/module.h>
33#include <linux/fs.h>
34#include <linux/time.h>
Mingming Caocd02ff02007-10-16 18:38:25 -040035#include <linux/jbd2.h>
Alex Tomasa86c6182006-10-11 01:21:03 -070036#include <linux/highuid.h>
37#include <linux/pagemap.h>
38#include <linux/quotaops.h>
39#include <linux/string.h>
40#include <linux/slab.h>
Amit Aroraa2df2a62007-07-17 21:42:41 -040041#include <linux/falloc.h>
Alex Tomasa86c6182006-10-11 01:21:03 -070042#include <asm/uaccess.h>
Eric Sandeen6873fa02008-10-07 00:46:36 -040043#include <linux/fiemap.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040044#include "ext4_jbd2.h"
45#include "ext4_extents.h"
Alex Tomasa86c6182006-10-11 01:21:03 -070046
Jiaying Zhang0562e0b2011-03-21 21:38:05 -040047#include <trace/events/ext4.h>
48
Allison Hendersond583fb82011-05-25 07:41:43 -040049static int ext4_split_extent(handle_t *handle,
50 struct inode *inode,
51 struct ext4_ext_path *path,
52 struct ext4_map_blocks *map,
53 int split_flag,
54 int flags);
55
Jan Kara487caee2009-08-17 22:17:20 -040056static int ext4_ext_truncate_extend_restart(handle_t *handle,
57 struct inode *inode,
58 int needed)
Alex Tomasa86c6182006-10-11 01:21:03 -070059{
60 int err;
61
Frank Mayhar03901312009-01-07 00:06:22 -050062 if (!ext4_handle_valid(handle))
63 return 0;
Alex Tomasa86c6182006-10-11 01:21:03 -070064 if (handle->h_buffer_credits > needed)
Shen Feng9102e4f2008-07-11 19:27:31 -040065 return 0;
66 err = ext4_journal_extend(handle, needed);
Theodore Ts'o0123c932008-08-01 20:57:54 -040067 if (err <= 0)
Shen Feng9102e4f2008-07-11 19:27:31 -040068 return err;
Jan Kara487caee2009-08-17 22:17:20 -040069 err = ext4_truncate_restart_trans(handle, inode, needed);
Dmitry Monakhov0617b832010-05-17 01:00:00 -040070 if (err == 0)
71 err = -EAGAIN;
Jan Kara487caee2009-08-17 22:17:20 -040072
73 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -070074}
75
76/*
77 * could return:
78 * - EROFS
79 * - ENOMEM
80 */
81static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
82 struct ext4_ext_path *path)
83{
84 if (path->p_bh) {
85 /* path points to block */
86 return ext4_journal_get_write_access(handle, path->p_bh);
87 }
88 /* path points to leaf/index in inode body */
89 /* we use in-core data, no need to protect them */
90 return 0;
91}
92
93/*
94 * could return:
95 * - EROFS
96 * - ENOMEM
97 * - EIO
98 */
99static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
100 struct ext4_ext_path *path)
101{
102 int err;
103 if (path->p_bh) {
104 /* path points to block */
Frank Mayhar03901312009-01-07 00:06:22 -0500105 err = ext4_handle_dirty_metadata(handle, inode, path->p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700106 } else {
107 /* path points to leaf/index in inode body */
108 err = ext4_mark_inode_dirty(handle, inode);
109 }
110 return err;
111}
112
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700113static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700114 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500115 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700116{
117 struct ext4_inode_info *ei = EXT4_I(inode);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700118 ext4_fsblk_t bg_start;
Valerie Clement74d34872008-02-15 13:43:07 -0500119 ext4_fsblk_t last_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700120 ext4_grpblk_t colour;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400121 ext4_group_t block_group;
122 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -0700123 int depth;
124
125 if (path) {
126 struct ext4_extent *ex;
127 depth = path->p_depth;
128
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500129 /*
130 * Try to predict block placement assuming that we are
131 * filling in a file which will eventually be
132 * non-sparse --- i.e., in the case of libbfd writing
133 * an ELF object sections out-of-order but in a way
134 * the eventually results in a contiguous object or
135 * executable file, or some database extending a table
136 * space file. However, this is actually somewhat
137 * non-ideal if we are writing a sparse file such as
138 * qemu or KVM writing a raw image file that is going
139 * to stay fairly sparse, since it will end up
140 * fragmenting the file system's free space. Maybe we
141 * should have some hueristics or some way to allow
142 * userspace to pass a hint to file system,
Tao Mab8d65682011-01-21 23:21:31 +0800143 * especially if the latter case turns out to be
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500144 * common.
145 */
Avantika Mathur7e028972006-12-06 20:41:33 -0800146 ex = path[depth].p_ext;
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500147 if (ex) {
148 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
149 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
150
151 if (block > ext_block)
152 return ext_pblk + (block - ext_block);
153 else
154 return ext_pblk - (ext_block - block);
155 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700156
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700157 /* it looks like index is empty;
158 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700159 if (path[depth].p_bh)
160 return path[depth].p_bh->b_blocknr;
161 }
162
163 /* OK. use inode's group */
Theodore Ts'oa4912122009-03-12 12:18:34 -0400164 block_group = ei->i_block_group;
165 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
166 /*
167 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
Theodore Ts'o60e66792010-05-17 07:00:00 -0400168 * block groups per flexgroup, reserve the first block
169 * group for directories and special files. Regular
Theodore Ts'oa4912122009-03-12 12:18:34 -0400170 * files will start at the second block group. This
Theodore Ts'o60e66792010-05-17 07:00:00 -0400171 * tends to speed up directory access and improves
Theodore Ts'oa4912122009-03-12 12:18:34 -0400172 * fsck times.
173 */
174 block_group &= ~(flex_size-1);
175 if (S_ISREG(inode->i_mode))
176 block_group++;
177 }
Akinobu Mita5661bd62010-03-03 23:53:39 -0500178 bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
Valerie Clement74d34872008-02-15 13:43:07 -0500179 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
180
Theodore Ts'oa4912122009-03-12 12:18:34 -0400181 /*
182 * If we are doing delayed allocation, we don't need take
183 * colour into account.
184 */
185 if (test_opt(inode->i_sb, DELALLOC))
186 return bg_start;
187
Valerie Clement74d34872008-02-15 13:43:07 -0500188 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
189 colour = (current->pid % 16) *
Alex Tomasa86c6182006-10-11 01:21:03 -0700190 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
Valerie Clement74d34872008-02-15 13:43:07 -0500191 else
192 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
Alex Tomasa86c6182006-10-11 01:21:03 -0700193 return bg_start + colour + block;
194}
195
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400196/*
197 * Allocation for a meta data block
198 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700199static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400200ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700201 struct ext4_ext_path *path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400202 struct ext4_extent *ex, int *err, unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700203{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700204 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700205
206 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Allison Henderson55f020d2011-05-25 07:41:26 -0400207 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
208 NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700209 return newblock;
210}
211
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400212static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700213{
214 int size;
215
216 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
217 / sizeof(struct ext4_extent);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400218 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100219#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400220 if (size > 6)
221 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700222#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400223 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700224 return size;
225}
226
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400227static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700228{
229 int size;
230
231 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
232 / 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 > 5)
236 size = 5;
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
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400242static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700243{
244 int size;
245
246 size = sizeof(EXT4_I(inode)->i_data);
247 size -= sizeof(struct ext4_extent_header);
248 size /= sizeof(struct ext4_extent);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400249 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100250#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400251 if (size > 3)
252 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700253#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400254 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700255 return size;
256}
257
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400258static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700259{
260 int size;
261
262 size = sizeof(EXT4_I(inode)->i_data);
263 size -= sizeof(struct ext4_extent_header);
264 size /= sizeof(struct ext4_extent_idx);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400265 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100266#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400267 if (size > 4)
268 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700269#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400270 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700271 return size;
272}
273
Mingming Caod2a17632008-07-14 17:52:37 -0400274/*
275 * Calculate the number of metadata blocks needed
276 * to allocate @blocks
277 * Worse case is one block per extent
278 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -0500279int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -0400280{
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500281 struct ext4_inode_info *ei = EXT4_I(inode);
282 int idxs, num = 0;
Mingming Caod2a17632008-07-14 17:52:37 -0400283
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500284 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
285 / sizeof(struct ext4_extent_idx));
Mingming Caod2a17632008-07-14 17:52:37 -0400286
287 /*
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500288 * If the new delayed allocation block is contiguous with the
289 * previous da block, it can share index blocks with the
290 * previous block, so we only need to allocate a new index
291 * block every idxs leaf blocks. At ldxs**2 blocks, we need
292 * an additional index block, and at ldxs**3 blocks, yet
293 * another index blocks.
Mingming Caod2a17632008-07-14 17:52:37 -0400294 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500295 if (ei->i_da_metadata_calc_len &&
296 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
297 if ((ei->i_da_metadata_calc_len % idxs) == 0)
298 num++;
299 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
300 num++;
301 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
302 num++;
303 ei->i_da_metadata_calc_len = 0;
304 } else
305 ei->i_da_metadata_calc_len++;
306 ei->i_da_metadata_calc_last_lblock++;
307 return num;
308 }
Mingming Caod2a17632008-07-14 17:52:37 -0400309
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500310 /*
311 * In the worst case we need a new set of index blocks at
312 * every level of the inode's extent tree.
313 */
314 ei->i_da_metadata_calc_len = 1;
315 ei->i_da_metadata_calc_last_lblock = lblock;
316 return ext_depth(inode) + 1;
Mingming Caod2a17632008-07-14 17:52:37 -0400317}
318
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400319static int
320ext4_ext_max_entries(struct inode *inode, int depth)
321{
322 int max;
323
324 if (depth == ext_depth(inode)) {
325 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400326 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400327 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400328 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400329 } else {
330 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400331 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400332 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400333 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400334 }
335
336 return max;
337}
338
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400339static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
340{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400341 ext4_fsblk_t block = ext4_ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400342 int len = ext4_ext_get_actual_len(ext);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400343
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400344 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400345}
346
347static int ext4_valid_extent_idx(struct inode *inode,
348 struct ext4_extent_idx *ext_idx)
349{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400350 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400351
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400352 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400353}
354
355static int ext4_valid_extent_entries(struct inode *inode,
356 struct ext4_extent_header *eh,
357 int depth)
358{
359 struct ext4_extent *ext;
360 struct ext4_extent_idx *ext_idx;
361 unsigned short entries;
362 if (eh->eh_entries == 0)
363 return 1;
364
365 entries = le16_to_cpu(eh->eh_entries);
366
367 if (depth == 0) {
368 /* leaf entries */
369 ext = EXT_FIRST_EXTENT(eh);
370 while (entries) {
371 if (!ext4_valid_extent(inode, ext))
372 return 0;
373 ext++;
374 entries--;
375 }
376 } else {
377 ext_idx = EXT_FIRST_INDEX(eh);
378 while (entries) {
379 if (!ext4_valid_extent_idx(inode, ext_idx))
380 return 0;
381 ext_idx++;
382 entries--;
383 }
384 }
385 return 1;
386}
387
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400388static int __ext4_ext_check(const char *function, unsigned int line,
389 struct inode *inode, struct ext4_extent_header *eh,
390 int depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400391{
392 const char *error_msg;
393 int max = 0;
394
395 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
396 error_msg = "invalid magic";
397 goto corrupted;
398 }
399 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
400 error_msg = "unexpected eh_depth";
401 goto corrupted;
402 }
403 if (unlikely(eh->eh_max == 0)) {
404 error_msg = "invalid eh_max";
405 goto corrupted;
406 }
407 max = ext4_ext_max_entries(inode, depth);
408 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
409 error_msg = "too large eh_max";
410 goto corrupted;
411 }
412 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
413 error_msg = "invalid eh_entries";
414 goto corrupted;
415 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400416 if (!ext4_valid_extent_entries(inode, eh, depth)) {
417 error_msg = "invalid extent entries";
418 goto corrupted;
419 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400420 return 0;
421
422corrupted:
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400423 ext4_error_inode(inode, function, line, 0,
Theodore Ts'o24676da2010-05-16 21:00:00 -0400424 "bad header/extent: %s - magic %x, "
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400425 "entries %u, max %u(%u), depth %u(%u)",
Theodore Ts'o24676da2010-05-16 21:00:00 -0400426 error_msg, le16_to_cpu(eh->eh_magic),
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400427 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
428 max, le16_to_cpu(eh->eh_depth), depth);
429
430 return -EIO;
431}
432
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400433#define ext4_ext_check(inode, eh, depth) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400434 __ext4_ext_check(__func__, __LINE__, inode, eh, depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400435
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400436int ext4_ext_check_inode(struct inode *inode)
437{
438 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
439}
440
Alex Tomasa86c6182006-10-11 01:21:03 -0700441#ifdef EXT_DEBUG
442static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
443{
444 int k, l = path->p_depth;
445
446 ext_debug("path:");
447 for (k = 0; k <= l; k++, path++) {
448 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700449 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400450 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700451 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400452 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700453 le32_to_cpu(path->p_ext->ee_block),
Mingming553f9002009-09-18 13:34:55 -0400454 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400455 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400456 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700457 } else
458 ext_debug(" []");
459 }
460 ext_debug("\n");
461}
462
463static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
464{
465 int depth = ext_depth(inode);
466 struct ext4_extent_header *eh;
467 struct ext4_extent *ex;
468 int i;
469
470 if (!path)
471 return;
472
473 eh = path[depth].p_hdr;
474 ex = EXT_FIRST_EXTENT(eh);
475
Mingming553f9002009-09-18 13:34:55 -0400476 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
477
Alex Tomasa86c6182006-10-11 01:21:03 -0700478 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400479 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
480 ext4_ext_is_uninitialized(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400481 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700482 }
483 ext_debug("\n");
484}
485#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400486#define ext4_ext_show_path(inode, path)
487#define ext4_ext_show_leaf(inode, path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700488#endif
489
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500490void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700491{
492 int depth = path->p_depth;
493 int i;
494
495 for (i = 0; i <= depth; i++, path++)
496 if (path->p_bh) {
497 brelse(path->p_bh);
498 path->p_bh = NULL;
499 }
500}
501
502/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700503 * ext4_ext_binsearch_idx:
504 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400505 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700506 */
507static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500508ext4_ext_binsearch_idx(struct inode *inode,
509 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700510{
511 struct ext4_extent_header *eh = path->p_hdr;
512 struct ext4_extent_idx *r, *l, *m;
513
Alex Tomasa86c6182006-10-11 01:21:03 -0700514
Eric Sandeenbba90742008-01-28 23:58:27 -0500515 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700516
517 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400518 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700519 while (l <= r) {
520 m = l + (r - l) / 2;
521 if (block < le32_to_cpu(m->ei_block))
522 r = m - 1;
523 else
524 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400525 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
526 m, le32_to_cpu(m->ei_block),
527 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700528 }
529
530 path->p_idx = l - 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700531 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400532 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700533
534#ifdef CHECK_BINSEARCH
535 {
536 struct ext4_extent_idx *chix, *ix;
537 int k;
538
539 chix = ix = EXT_FIRST_INDEX(eh);
540 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
541 if (k != 0 &&
542 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400543 printk(KERN_DEBUG "k=%d, ix=0x%p, "
544 "first=0x%p\n", k,
545 ix, EXT_FIRST_INDEX(eh));
546 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700547 le32_to_cpu(ix->ei_block),
548 le32_to_cpu(ix[-1].ei_block));
549 }
550 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400551 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700552 if (block < le32_to_cpu(ix->ei_block))
553 break;
554 chix = ix;
555 }
556 BUG_ON(chix != path->p_idx);
557 }
558#endif
559
560}
561
562/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700563 * ext4_ext_binsearch:
564 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400565 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700566 */
567static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500568ext4_ext_binsearch(struct inode *inode,
569 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700570{
571 struct ext4_extent_header *eh = path->p_hdr;
572 struct ext4_extent *r, *l, *m;
573
Alex Tomasa86c6182006-10-11 01:21:03 -0700574 if (eh->eh_entries == 0) {
575 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700576 * this leaf is empty:
577 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700578 */
579 return;
580 }
581
Eric Sandeenbba90742008-01-28 23:58:27 -0500582 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700583
584 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400585 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700586
587 while (l <= r) {
588 m = l + (r - l) / 2;
589 if (block < le32_to_cpu(m->ee_block))
590 r = m - 1;
591 else
592 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400593 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
594 m, le32_to_cpu(m->ee_block),
595 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700596 }
597
598 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400599 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400600 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400601 ext4_ext_pblock(path->p_ext),
Mingming553f9002009-09-18 13:34:55 -0400602 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400603 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700604
605#ifdef CHECK_BINSEARCH
606 {
607 struct ext4_extent *chex, *ex;
608 int k;
609
610 chex = ex = EXT_FIRST_EXTENT(eh);
611 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
612 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400613 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700614 if (block < le32_to_cpu(ex->ee_block))
615 break;
616 chex = ex;
617 }
618 BUG_ON(chex != path->p_ext);
619 }
620#endif
621
622}
623
624int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
625{
626 struct ext4_extent_header *eh;
627
628 eh = ext_inode_hdr(inode);
629 eh->eh_depth = 0;
630 eh->eh_entries = 0;
631 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400632 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700633 ext4_mark_inode_dirty(handle, inode);
634 ext4_ext_invalidate_cache(inode);
635 return 0;
636}
637
638struct ext4_ext_path *
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500639ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
640 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700641{
642 struct ext4_extent_header *eh;
643 struct buffer_head *bh;
644 short int depth, i, ppos = 0, alloc = 0;
645
646 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400647 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700648
649 /* account possible depth increase */
650 if (!path) {
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800651 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700652 GFP_NOFS);
653 if (!path)
654 return ERR_PTR(-ENOMEM);
655 alloc = 1;
656 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700657 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400658 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700659
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400660 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700661 /* walk through the tree */
662 while (i) {
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400663 int need_to_validate = 0;
664
Alex Tomasa86c6182006-10-11 01:21:03 -0700665 ext_debug("depth %d: num %d, max %d\n",
666 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400667
Alex Tomasa86c6182006-10-11 01:21:03 -0700668 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400669 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700670 path[ppos].p_depth = i;
671 path[ppos].p_ext = NULL;
672
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400673 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
674 if (unlikely(!bh))
Alex Tomasa86c6182006-10-11 01:21:03 -0700675 goto err;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400676 if (!bh_uptodate_or_lock(bh)) {
Jiaying Zhang0562e0b2011-03-21 21:38:05 -0400677 trace_ext4_ext_load_extent(inode, block,
678 path[ppos].p_block);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400679 if (bh_submit_read(bh) < 0) {
680 put_bh(bh);
681 goto err;
682 }
683 /* validate the extent entries */
684 need_to_validate = 1;
685 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700686 eh = ext_block_hdr(bh);
687 ppos++;
Frank Mayhar273df552010-03-02 11:46:09 -0500688 if (unlikely(ppos > depth)) {
689 put_bh(bh);
690 EXT4_ERROR_INODE(inode,
691 "ppos %d > depth %d", ppos, depth);
692 goto err;
693 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700694 path[ppos].p_bh = bh;
695 path[ppos].p_hdr = eh;
696 i--;
697
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400698 if (need_to_validate && ext4_ext_check(inode, eh, i))
Alex Tomasa86c6182006-10-11 01:21:03 -0700699 goto err;
700 }
701
702 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700703 path[ppos].p_ext = NULL;
704 path[ppos].p_idx = NULL;
705
Alex Tomasa86c6182006-10-11 01:21:03 -0700706 /* find extent */
707 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400708 /* if not an empty leaf */
709 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400710 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700711
712 ext4_ext_show_path(inode, path);
713
714 return path;
715
716err:
717 ext4_ext_drop_refs(path);
718 if (alloc)
719 kfree(path);
720 return ERR_PTR(-EIO);
721}
722
723/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700724 * ext4_ext_insert_index:
725 * insert new index [@logical;@ptr] into the block at @curp;
726 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700727 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400728static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
729 struct ext4_ext_path *curp,
730 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700731{
732 struct ext4_extent_idx *ix;
733 int len, err;
734
Avantika Mathur7e028972006-12-06 20:41:33 -0800735 err = ext4_ext_get_access(handle, inode, curp);
736 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700737 return err;
738
Frank Mayhar273df552010-03-02 11:46:09 -0500739 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
740 EXT4_ERROR_INODE(inode,
741 "logical %d == ei_block %d!",
742 logical, le32_to_cpu(curp->p_idx->ei_block));
743 return -EIO;
744 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700745 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
746 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
747 /* insert after */
748 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
749 len = (len - 1) * sizeof(struct ext4_extent_idx);
750 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400751 ext_debug("insert new index %d after: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700752 "move %d from 0x%p to 0x%p\n",
753 logical, ptr, len,
754 (curp->p_idx + 1), (curp->p_idx + 2));
755 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
756 }
757 ix = curp->p_idx + 1;
758 } else {
759 /* insert before */
760 len = len * sizeof(struct ext4_extent_idx);
761 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400762 ext_debug("insert new index %d before: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700763 "move %d from 0x%p to 0x%p\n",
764 logical, ptr, len,
765 curp->p_idx, (curp->p_idx + 1));
766 memmove(curp->p_idx + 1, curp->p_idx, len);
767 ix = curp->p_idx;
768 }
769
770 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700771 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400772 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700773
Frank Mayhar273df552010-03-02 11:46:09 -0500774 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
775 > le16_to_cpu(curp->p_hdr->eh_max))) {
776 EXT4_ERROR_INODE(inode,
777 "logical %d == ei_block %d!",
778 logical, le32_to_cpu(curp->p_idx->ei_block));
779 return -EIO;
780 }
781 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
782 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
783 return -EIO;
784 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700785
786 err = ext4_ext_dirty(handle, inode, curp);
787 ext4_std_error(inode->i_sb, err);
788
789 return err;
790}
791
792/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700793 * ext4_ext_split:
794 * inserts new subtree into the path, using free index entry
795 * at depth @at:
796 * - allocates all needed blocks (new leaf and all intermediate index blocks)
797 * - makes decision where to split
798 * - moves remaining extents and index entries (right to the split point)
799 * into the newly allocated blocks
800 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700801 */
802static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -0400803 unsigned int flags,
804 struct ext4_ext_path *path,
805 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -0700806{
807 struct buffer_head *bh = NULL;
808 int depth = ext_depth(inode);
809 struct ext4_extent_header *neh;
810 struct ext4_extent_idx *fidx;
811 struct ext4_extent *ex;
812 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700813 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700814 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700815 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700816 int err = 0;
817
818 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700819 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700820
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700821 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700822 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -0500823 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
824 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
825 return -EIO;
826 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700827 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
828 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700829 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700830 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400831 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700832 } else {
833 border = newext->ee_block;
834 ext_debug("leaf will be added."
835 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400836 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700837 }
838
839 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700840 * If error occurs, then we break processing
841 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700842 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700843 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700844 */
845
846 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700847 * Get array to track all allocated blocks.
848 * We need this to handle errors and free blocks
849 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700850 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800851 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700852 if (!ablocks)
853 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700854
855 /* allocate all needed blocks */
856 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
857 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400858 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400859 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -0700860 if (newblock == 0)
861 goto cleanup;
862 ablocks[a] = newblock;
863 }
864
865 /* initialize new leaf */
866 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -0500867 if (unlikely(newblock == 0)) {
868 EXT4_ERROR_INODE(inode, "newblock == 0!");
869 err = -EIO;
870 goto cleanup;
871 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700872 bh = sb_getblk(inode->i_sb, newblock);
873 if (!bh) {
874 err = -EIO;
875 goto cleanup;
876 }
877 lock_buffer(bh);
878
Avantika Mathur7e028972006-12-06 20:41:33 -0800879 err = ext4_journal_get_create_access(handle, bh);
880 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700881 goto cleanup;
882
883 neh = ext_block_hdr(bh);
884 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400885 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700886 neh->eh_magic = EXT4_EXT_MAGIC;
887 neh->eh_depth = 0;
888 ex = EXT_FIRST_EXTENT(neh);
889
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700890 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -0500891 if (unlikely(path[depth].p_hdr->eh_entries !=
892 path[depth].p_hdr->eh_max)) {
893 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
894 path[depth].p_hdr->eh_entries,
895 path[depth].p_hdr->eh_max);
896 err = -EIO;
897 goto cleanup;
898 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700899 /* start copy from next extent */
900 /* TODO: we could do it by single memmove */
901 m = 0;
902 path[depth].p_ext++;
903 while (path[depth].p_ext <=
904 EXT_MAX_EXTENT(path[depth].p_hdr)) {
Mingming553f9002009-09-18 13:34:55 -0400905 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400906 le32_to_cpu(path[depth].p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400907 ext4_ext_pblock(path[depth].p_ext),
Mingming553f9002009-09-18 13:34:55 -0400908 ext4_ext_is_uninitialized(path[depth].p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400909 ext4_ext_get_actual_len(path[depth].p_ext),
Alex Tomasa86c6182006-10-11 01:21:03 -0700910 newblock);
911 /*memmove(ex++, path[depth].p_ext++,
912 sizeof(struct ext4_extent));
913 neh->eh_entries++;*/
914 path[depth].p_ext++;
915 m++;
916 }
917 if (m) {
918 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400919 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700920 }
921
922 set_buffer_uptodate(bh);
923 unlock_buffer(bh);
924
Frank Mayhar03901312009-01-07 00:06:22 -0500925 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800926 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700927 goto cleanup;
928 brelse(bh);
929 bh = NULL;
930
931 /* correct old leaf */
932 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800933 err = ext4_ext_get_access(handle, inode, path + depth);
934 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700935 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400936 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800937 err = ext4_ext_dirty(handle, inode, path + depth);
938 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700939 goto cleanup;
940
941 }
942
943 /* create intermediate indexes */
944 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -0500945 if (unlikely(k < 0)) {
946 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
947 err = -EIO;
948 goto cleanup;
949 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700950 if (k)
951 ext_debug("create %d intermediate indices\n", k);
952 /* insert new index into current index block */
953 /* current depth stored in i var */
954 i = depth - 1;
955 while (k--) {
956 oldblock = newblock;
957 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -0500958 bh = sb_getblk(inode->i_sb, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700959 if (!bh) {
960 err = -EIO;
961 goto cleanup;
962 }
963 lock_buffer(bh);
964
Avantika Mathur7e028972006-12-06 20:41:33 -0800965 err = ext4_journal_get_create_access(handle, bh);
966 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700967 goto cleanup;
968
969 neh = ext_block_hdr(bh);
970 neh->eh_entries = cpu_to_le16(1);
971 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400972 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700973 neh->eh_depth = cpu_to_le16(depth - i);
974 fidx = EXT_FIRST_INDEX(neh);
975 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700976 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700977
Eric Sandeenbba90742008-01-28 23:58:27 -0500978 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
979 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700980 /* copy indexes */
981 m = 0;
982 path[i].p_idx++;
983
984 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
985 EXT_MAX_INDEX(path[i].p_hdr));
Frank Mayhar273df552010-03-02 11:46:09 -0500986 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
987 EXT_LAST_INDEX(path[i].p_hdr))) {
988 EXT4_ERROR_INODE(inode,
989 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
990 le32_to_cpu(path[i].p_ext->ee_block));
991 err = -EIO;
992 goto cleanup;
993 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700994 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400995 ext_debug("%d: move %d:%llu in new index %llu\n", i,
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400996 le32_to_cpu(path[i].p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400997 ext4_idx_pblock(path[i].p_idx),
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400998 newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700999 /*memmove(++fidx, path[i].p_idx++,
1000 sizeof(struct ext4_extent_idx));
1001 neh->eh_entries++;
1002 BUG_ON(neh->eh_entries > neh->eh_max);*/
1003 path[i].p_idx++;
1004 m++;
1005 }
1006 if (m) {
1007 memmove(++fidx, path[i].p_idx - m,
1008 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001009 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001010 }
1011 set_buffer_uptodate(bh);
1012 unlock_buffer(bh);
1013
Frank Mayhar03901312009-01-07 00:06:22 -05001014 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001015 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001016 goto cleanup;
1017 brelse(bh);
1018 bh = NULL;
1019
1020 /* correct old index */
1021 if (m) {
1022 err = ext4_ext_get_access(handle, inode, path + i);
1023 if (err)
1024 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001025 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001026 err = ext4_ext_dirty(handle, inode, path + i);
1027 if (err)
1028 goto cleanup;
1029 }
1030
1031 i--;
1032 }
1033
1034 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001035 err = ext4_ext_insert_index(handle, inode, path + at,
1036 le32_to_cpu(border), newblock);
1037
1038cleanup:
1039 if (bh) {
1040 if (buffer_locked(bh))
1041 unlock_buffer(bh);
1042 brelse(bh);
1043 }
1044
1045 if (err) {
1046 /* free all allocated blocks in error case */
1047 for (i = 0; i < depth; i++) {
1048 if (!ablocks[i])
1049 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001050 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001051 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001052 }
1053 }
1054 kfree(ablocks);
1055
1056 return err;
1057}
1058
1059/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001060 * ext4_ext_grow_indepth:
1061 * implements tree growing procedure:
1062 * - allocates new block
1063 * - moves top-level data (index block or leaf) into the new block
1064 * - initializes new top-level, creating index that points to the
1065 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001066 */
1067static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001068 unsigned int flags,
1069 struct ext4_ext_path *path,
1070 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001071{
1072 struct ext4_ext_path *curp = path;
1073 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001074 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001075 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001076 int err = 0;
1077
Allison Henderson55f020d2011-05-25 07:41:26 -04001078 newblock = ext4_ext_new_meta_block(handle, inode, path,
1079 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001080 if (newblock == 0)
1081 return err;
1082
1083 bh = sb_getblk(inode->i_sb, newblock);
1084 if (!bh) {
1085 err = -EIO;
1086 ext4_std_error(inode->i_sb, err);
1087 return err;
1088 }
1089 lock_buffer(bh);
1090
Avantika Mathur7e028972006-12-06 20:41:33 -08001091 err = ext4_journal_get_create_access(handle, bh);
1092 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001093 unlock_buffer(bh);
1094 goto out;
1095 }
1096
1097 /* move top-level index/leaf into new block */
1098 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
1099
1100 /* set size of new block */
1101 neh = ext_block_hdr(bh);
1102 /* old root could have indexes or leaves
1103 * so calculate e_max right way */
1104 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001105 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001106 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001107 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001108 neh->eh_magic = EXT4_EXT_MAGIC;
1109 set_buffer_uptodate(bh);
1110 unlock_buffer(bh);
1111
Frank Mayhar03901312009-01-07 00:06:22 -05001112 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001113 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001114 goto out;
1115
1116 /* create index in new top-level index: num,max,pointer */
Avantika Mathur7e028972006-12-06 20:41:33 -08001117 err = ext4_ext_get_access(handle, inode, curp);
1118 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001119 goto out;
1120
1121 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001122 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001123 curp->p_hdr->eh_entries = cpu_to_le16(1);
1124 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
Dmitry Monakhove9f410b2007-07-18 09:09:15 -04001125
1126 if (path[0].p_hdr->eh_depth)
1127 curp->p_idx->ei_block =
1128 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
1129 else
1130 curp->p_idx->ei_block =
1131 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001132 ext4_idx_store_pblock(curp->p_idx, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001133
1134 neh = ext_inode_hdr(inode);
Mingming Cao2ae02102006-10-11 01:21:11 -07001135 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001136 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001137 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001138 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001139
1140 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
1141 err = ext4_ext_dirty(handle, inode, curp);
1142out:
1143 brelse(bh);
1144
1145 return err;
1146}
1147
1148/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001149 * ext4_ext_create_new_leaf:
1150 * finds empty index and adds new leaf.
1151 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001152 */
1153static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001154 unsigned int flags,
1155 struct ext4_ext_path *path,
1156 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001157{
1158 struct ext4_ext_path *curp;
1159 int depth, i, err = 0;
1160
1161repeat:
1162 i = depth = ext_depth(inode);
1163
1164 /* walk up to the tree and look for free index entry */
1165 curp = path + depth;
1166 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1167 i--;
1168 curp--;
1169 }
1170
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001171 /* we use already allocated block for index block,
1172 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001173 if (EXT_HAS_FREE_INDEX(curp)) {
1174 /* if we found index with free entry, then use that
1175 * entry: create all needed subtree and add new leaf */
Allison Henderson55f020d2011-05-25 07:41:26 -04001176 err = ext4_ext_split(handle, inode, flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001177 if (err)
1178 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001179
1180 /* refill path */
1181 ext4_ext_drop_refs(path);
1182 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001183 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1184 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001185 if (IS_ERR(path))
1186 err = PTR_ERR(path);
1187 } else {
1188 /* tree is full, time to grow in depth */
Allison Henderson55f020d2011-05-25 07:41:26 -04001189 err = ext4_ext_grow_indepth(handle, inode, flags,
1190 path, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001191 if (err)
1192 goto out;
1193
1194 /* refill path */
1195 ext4_ext_drop_refs(path);
1196 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001197 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1198 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001199 if (IS_ERR(path)) {
1200 err = PTR_ERR(path);
1201 goto out;
1202 }
1203
1204 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001205 * only first (depth 0 -> 1) produces free space;
1206 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001207 */
1208 depth = ext_depth(inode);
1209 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001210 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001211 goto repeat;
1212 }
1213 }
1214
1215out:
1216 return err;
1217}
1218
1219/*
Alex Tomas1988b512008-01-28 23:58:27 -05001220 * search the closest allocated block to the left for *logical
1221 * and returns it at @logical + it's physical address at @phys
1222 * if *logical is the smallest allocated block, the function
1223 * returns 0 at @phys
1224 * return value contains 0 (success) or error code
1225 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001226static int ext4_ext_search_left(struct inode *inode,
1227 struct ext4_ext_path *path,
1228 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001229{
1230 struct ext4_extent_idx *ix;
1231 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001232 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001233
Frank Mayhar273df552010-03-02 11:46:09 -05001234 if (unlikely(path == NULL)) {
1235 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1236 return -EIO;
1237 }
Alex Tomas1988b512008-01-28 23:58:27 -05001238 depth = path->p_depth;
1239 *phys = 0;
1240
1241 if (depth == 0 && path->p_ext == NULL)
1242 return 0;
1243
1244 /* usually extent in the path covers blocks smaller
1245 * then *logical, but it can be that extent is the
1246 * first one in the file */
1247
1248 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001249 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001250 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001251 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1252 EXT4_ERROR_INODE(inode,
1253 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1254 *logical, le32_to_cpu(ex->ee_block));
1255 return -EIO;
1256 }
Alex Tomas1988b512008-01-28 23:58:27 -05001257 while (--depth >= 0) {
1258 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001259 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1260 EXT4_ERROR_INODE(inode,
1261 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
1262 ix != NULL ? ix->ei_block : 0,
1263 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
1264 EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block : 0,
1265 depth);
1266 return -EIO;
1267 }
Alex Tomas1988b512008-01-28 23:58:27 -05001268 }
1269 return 0;
1270 }
1271
Frank Mayhar273df552010-03-02 11:46:09 -05001272 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1273 EXT4_ERROR_INODE(inode,
1274 "logical %d < ee_block %d + ee_len %d!",
1275 *logical, le32_to_cpu(ex->ee_block), ee_len);
1276 return -EIO;
1277 }
Alex Tomas1988b512008-01-28 23:58:27 -05001278
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001279 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001280 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001281 return 0;
1282}
1283
1284/*
1285 * search the closest allocated block to the right for *logical
1286 * and returns it at @logical + it's physical address at @phys
1287 * if *logical is the smallest allocated block, the function
1288 * returns 0 at @phys
1289 * return value contains 0 (success) or error code
1290 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001291static int ext4_ext_search_right(struct inode *inode,
1292 struct ext4_ext_path *path,
1293 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001294{
1295 struct buffer_head *bh = NULL;
1296 struct ext4_extent_header *eh;
1297 struct ext4_extent_idx *ix;
1298 struct ext4_extent *ex;
1299 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001300 int depth; /* Note, NOT eh_depth; depth from top of tree */
1301 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001302
Frank Mayhar273df552010-03-02 11:46:09 -05001303 if (unlikely(path == NULL)) {
1304 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1305 return -EIO;
1306 }
Alex Tomas1988b512008-01-28 23:58:27 -05001307 depth = path->p_depth;
1308 *phys = 0;
1309
1310 if (depth == 0 && path->p_ext == NULL)
1311 return 0;
1312
1313 /* usually extent in the path covers blocks smaller
1314 * then *logical, but it can be that extent is the
1315 * first one in the file */
1316
1317 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001318 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001319 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001320 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1321 EXT4_ERROR_INODE(inode,
1322 "first_extent(path[%d].p_hdr) != ex",
1323 depth);
1324 return -EIO;
1325 }
Alex Tomas1988b512008-01-28 23:58:27 -05001326 while (--depth >= 0) {
1327 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001328 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1329 EXT4_ERROR_INODE(inode,
1330 "ix != EXT_FIRST_INDEX *logical %d!",
1331 *logical);
1332 return -EIO;
1333 }
Alex Tomas1988b512008-01-28 23:58:27 -05001334 }
1335 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001336 *phys = ext4_ext_pblock(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001337 return 0;
1338 }
1339
Frank Mayhar273df552010-03-02 11:46:09 -05001340 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1341 EXT4_ERROR_INODE(inode,
1342 "logical %d < ee_block %d + ee_len %d!",
1343 *logical, le32_to_cpu(ex->ee_block), ee_len);
1344 return -EIO;
1345 }
Alex Tomas1988b512008-01-28 23:58:27 -05001346
1347 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1348 /* next allocated block in this leaf */
1349 ex++;
1350 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001351 *phys = ext4_ext_pblock(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001352 return 0;
1353 }
1354
1355 /* go up and search for index to the right */
1356 while (--depth >= 0) {
1357 ix = path[depth].p_idx;
1358 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001359 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001360 }
1361
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001362 /* we've gone up to the root and found no index to the right */
1363 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001364
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001365got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001366 /* we've found index to the right, let's
1367 * follow it and find the closest allocated
1368 * block to the right */
1369 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001370 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001371 while (++depth < path->p_depth) {
1372 bh = sb_bread(inode->i_sb, block);
1373 if (bh == NULL)
1374 return -EIO;
1375 eh = ext_block_hdr(bh);
Eric Sandeen395a87b2009-03-10 18:18:47 -04001376 /* subtract from p_depth to get proper eh_depth */
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001377 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001378 put_bh(bh);
1379 return -EIO;
1380 }
1381 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001382 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001383 put_bh(bh);
1384 }
1385
1386 bh = sb_bread(inode->i_sb, block);
1387 if (bh == NULL)
1388 return -EIO;
1389 eh = ext_block_hdr(bh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001390 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001391 put_bh(bh);
1392 return -EIO;
1393 }
1394 ex = EXT_FIRST_EXTENT(eh);
1395 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001396 *phys = ext4_ext_pblock(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001397 put_bh(bh);
1398 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001399}
1400
1401/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001402 * ext4_ext_next_allocated_block:
1403 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1404 * NOTE: it considers block number from index entry as
1405 * allocated block. Thus, index entries have to be consistent
1406 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001407 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001408static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001409ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1410{
1411 int depth;
1412
1413 BUG_ON(path == NULL);
1414 depth = path->p_depth;
1415
1416 if (depth == 0 && path->p_ext == NULL)
1417 return EXT_MAX_BLOCK;
1418
1419 while (depth >= 0) {
1420 if (depth == path->p_depth) {
1421 /* leaf */
1422 if (path[depth].p_ext !=
1423 EXT_LAST_EXTENT(path[depth].p_hdr))
1424 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1425 } else {
1426 /* index */
1427 if (path[depth].p_idx !=
1428 EXT_LAST_INDEX(path[depth].p_hdr))
1429 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1430 }
1431 depth--;
1432 }
1433
1434 return EXT_MAX_BLOCK;
1435}
1436
1437/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001438 * ext4_ext_next_leaf_block:
Alex Tomasa86c6182006-10-11 01:21:03 -07001439 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1440 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001441static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
Andrew Morton63f57932006-10-11 01:21:24 -07001442 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001443{
1444 int depth;
1445
1446 BUG_ON(path == NULL);
1447 depth = path->p_depth;
1448
1449 /* zero-tree has no leaf blocks at all */
1450 if (depth == 0)
1451 return EXT_MAX_BLOCK;
1452
1453 /* go to index block */
1454 depth--;
1455
1456 while (depth >= 0) {
1457 if (path[depth].p_idx !=
1458 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001459 return (ext4_lblk_t)
1460 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001461 depth--;
1462 }
1463
1464 return EXT_MAX_BLOCK;
1465}
1466
1467/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001468 * ext4_ext_correct_indexes:
1469 * if leaf gets modified and modified extent is first in the leaf,
1470 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001471 * TODO: do we need to correct tree in all cases?
1472 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001473static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001474 struct ext4_ext_path *path)
1475{
1476 struct ext4_extent_header *eh;
1477 int depth = ext_depth(inode);
1478 struct ext4_extent *ex;
1479 __le32 border;
1480 int k, err = 0;
1481
1482 eh = path[depth].p_hdr;
1483 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001484
1485 if (unlikely(ex == NULL || eh == NULL)) {
1486 EXT4_ERROR_INODE(inode,
1487 "ex %p == NULL or eh %p == NULL", ex, eh);
1488 return -EIO;
1489 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001490
1491 if (depth == 0) {
1492 /* there is no tree at all */
1493 return 0;
1494 }
1495
1496 if (ex != EXT_FIRST_EXTENT(eh)) {
1497 /* we correct tree if first leaf got modified only */
1498 return 0;
1499 }
1500
1501 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001502 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001503 */
1504 k = depth - 1;
1505 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001506 err = ext4_ext_get_access(handle, inode, path + k);
1507 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001508 return err;
1509 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001510 err = ext4_ext_dirty(handle, inode, path + k);
1511 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001512 return err;
1513
1514 while (k--) {
1515 /* change all left-side indexes */
1516 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1517 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001518 err = ext4_ext_get_access(handle, inode, path + k);
1519 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001520 break;
1521 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001522 err = ext4_ext_dirty(handle, inode, path + k);
1523 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001524 break;
1525 }
1526
1527 return err;
1528}
1529
Akira Fujita748de672009-06-17 19:24:03 -04001530int
Alex Tomasa86c6182006-10-11 01:21:03 -07001531ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1532 struct ext4_extent *ex2)
1533{
Amit Arora749269f2007-07-18 09:02:56 -04001534 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001535
1536 /*
1537 * Make sure that either both extents are uninitialized, or
1538 * both are _not_.
1539 */
1540 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1541 return 0;
1542
Amit Arora749269f2007-07-18 09:02:56 -04001543 if (ext4_ext_is_uninitialized(ex1))
1544 max_len = EXT_UNINIT_MAX_LEN;
1545 else
1546 max_len = EXT_INIT_MAX_LEN;
1547
Amit Aroraa2df2a62007-07-17 21:42:41 -04001548 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1549 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1550
1551 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001552 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001553 return 0;
1554
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001555 /*
1556 * To allow future support for preallocated extents to be added
1557 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001558 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001559 */
Amit Arora749269f2007-07-18 09:02:56 -04001560 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001561 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001562#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001563 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001564 return 0;
1565#endif
1566
Theodore Ts'obf89d162010-10-27 21:30:14 -04001567 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001568 return 1;
1569 return 0;
1570}
1571
1572/*
Amit Arora56055d32007-07-17 21:42:38 -04001573 * This function tries to merge the "ex" extent to the next extent in the tree.
1574 * It always tries to merge towards right. If you want to merge towards
1575 * left, pass "ex - 1" as argument instead of "ex".
1576 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1577 * 1 if they got merged.
1578 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001579static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001580 struct ext4_ext_path *path,
1581 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001582{
1583 struct ext4_extent_header *eh;
1584 unsigned int depth, len;
1585 int merge_done = 0;
1586 int uninitialized = 0;
1587
1588 depth = ext_depth(inode);
1589 BUG_ON(path[depth].p_hdr == NULL);
1590 eh = path[depth].p_hdr;
1591
1592 while (ex < EXT_LAST_EXTENT(eh)) {
1593 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1594 break;
1595 /* merge with next extent! */
1596 if (ext4_ext_is_uninitialized(ex))
1597 uninitialized = 1;
1598 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1599 + ext4_ext_get_actual_len(ex + 1));
1600 if (uninitialized)
1601 ext4_ext_mark_uninitialized(ex);
1602
1603 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1604 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1605 * sizeof(struct ext4_extent);
1606 memmove(ex + 1, ex + 2, len);
1607 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001608 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001609 merge_done = 1;
1610 WARN_ON(eh->eh_entries == 0);
1611 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001612 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001613 }
1614
1615 return merge_done;
1616}
1617
1618/*
Yongqiang Yang197217a2011-05-03 11:45:29 -04001619 * This function tries to merge the @ex extent to neighbours in the tree.
1620 * return 1 if merge left else 0.
1621 */
1622static int ext4_ext_try_to_merge(struct inode *inode,
1623 struct ext4_ext_path *path,
1624 struct ext4_extent *ex) {
1625 struct ext4_extent_header *eh;
1626 unsigned int depth;
1627 int merge_done = 0;
1628 int ret = 0;
1629
1630 depth = ext_depth(inode);
1631 BUG_ON(path[depth].p_hdr == NULL);
1632 eh = path[depth].p_hdr;
1633
1634 if (ex > EXT_FIRST_EXTENT(eh))
1635 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1636
1637 if (!merge_done)
1638 ret = ext4_ext_try_to_merge_right(inode, path, ex);
1639
1640 return ret;
1641}
1642
1643/*
Amit Arora25d14f92007-05-24 13:04:13 -04001644 * check if a portion of the "newext" extent overlaps with an
1645 * existing extent.
1646 *
1647 * If there is an overlap discovered, it updates the length of the newext
1648 * such that there will be no overlap, and then returns 1.
1649 * If there is no overlap found, it returns 0.
1650 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001651static unsigned int ext4_ext_check_overlap(struct inode *inode,
1652 struct ext4_extent *newext,
1653 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001654{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001655 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001656 unsigned int depth, len1;
1657 unsigned int ret = 0;
1658
1659 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001660 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001661 depth = ext_depth(inode);
1662 if (!path[depth].p_ext)
1663 goto out;
1664 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1665
1666 /*
1667 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001668 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001669 */
1670 if (b2 < b1) {
1671 b2 = ext4_ext_next_allocated_block(path);
1672 if (b2 == EXT_MAX_BLOCK)
1673 goto out;
1674 }
1675
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001676 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001677 if (b1 + len1 < b1) {
1678 len1 = EXT_MAX_BLOCK - b1;
1679 newext->ee_len = cpu_to_le16(len1);
1680 ret = 1;
1681 }
1682
1683 /* check for overlap */
1684 if (b1 + len1 > b2) {
1685 newext->ee_len = cpu_to_le16(b2 - b1);
1686 ret = 1;
1687 }
1688out:
1689 return ret;
1690}
1691
1692/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001693 * ext4_ext_insert_extent:
1694 * tries to merge requsted extent into the existing extent or
1695 * inserts requested extent as new one into the tree,
1696 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001697 */
1698int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1699 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04001700 struct ext4_extent *newext, int flag)
Alex Tomasa86c6182006-10-11 01:21:03 -07001701{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001702 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001703 struct ext4_extent *ex, *fex;
1704 struct ext4_extent *nearex; /* nearest extent */
1705 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001706 int depth, len, err;
1707 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001708 unsigned uninitialized = 0;
Allison Henderson55f020d2011-05-25 07:41:26 -04001709 int flags = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001710
Frank Mayhar273df552010-03-02 11:46:09 -05001711 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1712 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1713 return -EIO;
1714 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001715 depth = ext_depth(inode);
1716 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001717 if (unlikely(path[depth].p_hdr == NULL)) {
1718 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1719 return -EIO;
1720 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001721
1722 /* try to insert block into found extent and return */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001723 if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
Mingming Cao00314622009-09-28 15:49:08 -04001724 && ext4_can_extents_be_merged(inode, ex, newext)) {
Mingming553f9002009-09-18 13:34:55 -04001725 ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04001726 ext4_ext_is_uninitialized(newext),
1727 ext4_ext_get_actual_len(newext),
1728 le32_to_cpu(ex->ee_block),
1729 ext4_ext_is_uninitialized(ex),
1730 ext4_ext_get_actual_len(ex),
1731 ext4_ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001732 err = ext4_ext_get_access(handle, inode, path + depth);
1733 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001734 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001735
1736 /*
1737 * ext4_can_extents_be_merged should have checked that either
1738 * both extents are uninitialized, or both aren't. Thus we
1739 * need to check only one of them here.
1740 */
1741 if (ext4_ext_is_uninitialized(ex))
1742 uninitialized = 1;
1743 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1744 + ext4_ext_get_actual_len(newext));
1745 if (uninitialized)
1746 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001747 eh = path[depth].p_hdr;
1748 nearex = ex;
1749 goto merge;
1750 }
1751
1752repeat:
1753 depth = ext_depth(inode);
1754 eh = path[depth].p_hdr;
1755 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1756 goto has_space;
1757
1758 /* probably next leaf has space for us? */
1759 fex = EXT_LAST_EXTENT(eh);
1760 next = ext4_ext_next_leaf_block(inode, path);
1761 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1762 && next != EXT_MAX_BLOCK) {
1763 ext_debug("next leaf block - %d\n", next);
1764 BUG_ON(npath != NULL);
1765 npath = ext4_ext_find_extent(inode, next, NULL);
1766 if (IS_ERR(npath))
1767 return PTR_ERR(npath);
1768 BUG_ON(npath->p_depth != path->p_depth);
1769 eh = npath[depth].p_hdr;
1770 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001771 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001772 le16_to_cpu(eh->eh_entries));
1773 path = npath;
1774 goto repeat;
1775 }
1776 ext_debug("next leaf has no free space(%d,%d)\n",
1777 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1778 }
1779
1780 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001781 * There is no free space in the found leaf.
1782 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001783 */
Allison Henderson55f020d2011-05-25 07:41:26 -04001784 if (flag & EXT4_GET_BLOCKS_PUNCH_OUT_EXT)
1785 flags = EXT4_MB_USE_ROOT_BLOCKS;
1786 err = ext4_ext_create_new_leaf(handle, inode, flags, path, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001787 if (err)
1788 goto cleanup;
1789 depth = ext_depth(inode);
1790 eh = path[depth].p_hdr;
1791
1792has_space:
1793 nearex = path[depth].p_ext;
1794
Avantika Mathur7e028972006-12-06 20:41:33 -08001795 err = ext4_ext_get_access(handle, inode, path + depth);
1796 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001797 goto cleanup;
1798
1799 if (!nearex) {
1800 /* there is no extent in this leaf, create first one */
Mingming553f9002009-09-18 13:34:55 -04001801 ext_debug("first extent in the leaf: %d:%llu:[%d]%d\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 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1807 } else if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001808 > le32_to_cpu(nearex->ee_block)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001809/* BUG_ON(newext->ee_block == nearex->ee_block); */
1810 if (nearex != EXT_LAST_EXTENT(eh)) {
1811 len = EXT_MAX_EXTENT(eh) - nearex;
1812 len = (len - 1) * sizeof(struct ext4_extent);
1813 len = len < 0 ? 0 : len;
Mingming553f9002009-09-18 13:34:55 -04001814 ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001815 "move %d from 0x%p to 0x%p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001816 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),
Alex Tomasa86c6182006-10-11 01:21:03 -07001820 nearex, len, nearex + 1, nearex + 2);
1821 memmove(nearex + 2, nearex + 1, len);
1822 }
1823 path[depth].p_ext = nearex + 1;
1824 } else {
1825 BUG_ON(newext->ee_block == nearex->ee_block);
1826 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1827 len = len < 0 ? 0 : len;
Mingming553f9002009-09-18 13:34:55 -04001828 ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001829 "move %d from 0x%p to 0x%p\n",
1830 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001831 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001832 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001833 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001834 nearex, len, nearex + 1, nearex + 2);
1835 memmove(nearex + 1, nearex, len);
1836 path[depth].p_ext = nearex;
1837 }
1838
Marcin Slusarze8546d02008-04-17 10:38:59 -04001839 le16_add_cpu(&eh->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001840 nearex = path[depth].p_ext;
1841 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001842 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001843 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001844
1845merge:
1846 /* try to merge extents to the right */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001847 if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
Mingming Cao00314622009-09-28 15:49:08 -04001848 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001849
1850 /* try to merge extents to the left */
1851
1852 /* time to correct all indexes above */
1853 err = ext4_ext_correct_indexes(handle, inode, path);
1854 if (err)
1855 goto cleanup;
1856
1857 err = ext4_ext_dirty(handle, inode, path + depth);
1858
1859cleanup:
1860 if (npath) {
1861 ext4_ext_drop_refs(npath);
1862 kfree(npath);
1863 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001864 ext4_ext_invalidate_cache(inode);
1865 return err;
1866}
1867
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001868static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1869 ext4_lblk_t num, ext_prepare_callback func,
1870 void *cbdata)
Eric Sandeen6873fa02008-10-07 00:46:36 -04001871{
1872 struct ext4_ext_path *path = NULL;
1873 struct ext4_ext_cache cbex;
1874 struct ext4_extent *ex;
1875 ext4_lblk_t next, start = 0, end = 0;
1876 ext4_lblk_t last = block + num;
1877 int depth, exists, err = 0;
1878
1879 BUG_ON(func == NULL);
1880 BUG_ON(inode == NULL);
1881
1882 while (block < last && block != EXT_MAX_BLOCK) {
1883 num = last - block;
1884 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001885 down_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001886 path = ext4_ext_find_extent(inode, block, path);
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001887 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001888 if (IS_ERR(path)) {
1889 err = PTR_ERR(path);
1890 path = NULL;
1891 break;
1892 }
1893
1894 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05001895 if (unlikely(path[depth].p_hdr == NULL)) {
1896 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1897 err = -EIO;
1898 break;
1899 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04001900 ex = path[depth].p_ext;
1901 next = ext4_ext_next_allocated_block(path);
1902
1903 exists = 0;
1904 if (!ex) {
1905 /* there is no extent yet, so try to allocate
1906 * all requested space */
1907 start = block;
1908 end = block + num;
1909 } else if (le32_to_cpu(ex->ee_block) > block) {
1910 /* need to allocate space before found extent */
1911 start = block;
1912 end = le32_to_cpu(ex->ee_block);
1913 if (block + num < end)
1914 end = block + num;
1915 } else if (block >= le32_to_cpu(ex->ee_block)
1916 + ext4_ext_get_actual_len(ex)) {
1917 /* need to allocate space after found extent */
1918 start = block;
1919 end = block + num;
1920 if (end >= next)
1921 end = next;
1922 } else if (block >= le32_to_cpu(ex->ee_block)) {
1923 /*
1924 * some part of requested space is covered
1925 * by found extent
1926 */
1927 start = block;
1928 end = le32_to_cpu(ex->ee_block)
1929 + ext4_ext_get_actual_len(ex);
1930 if (block + num < end)
1931 end = block + num;
1932 exists = 1;
1933 } else {
1934 BUG();
1935 }
1936 BUG_ON(end <= start);
1937
1938 if (!exists) {
1939 cbex.ec_block = start;
1940 cbex.ec_len = end - start;
1941 cbex.ec_start = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04001942 } else {
1943 cbex.ec_block = le32_to_cpu(ex->ee_block);
1944 cbex.ec_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001945 cbex.ec_start = ext4_ext_pblock(ex);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001946 }
1947
Frank Mayhar273df552010-03-02 11:46:09 -05001948 if (unlikely(cbex.ec_len == 0)) {
1949 EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
1950 err = -EIO;
1951 break;
1952 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04001953 err = func(inode, path, &cbex, ex, cbdata);
1954 ext4_ext_drop_refs(path);
1955
1956 if (err < 0)
1957 break;
1958
1959 if (err == EXT_REPEAT)
1960 continue;
1961 else if (err == EXT_BREAK) {
1962 err = 0;
1963 break;
1964 }
1965
1966 if (ext_depth(inode) != depth) {
1967 /* depth was changed. we have to realloc path */
1968 kfree(path);
1969 path = NULL;
1970 }
1971
1972 block = cbex.ec_block + cbex.ec_len;
1973 }
1974
1975 if (path) {
1976 ext4_ext_drop_refs(path);
1977 kfree(path);
1978 }
1979
1980 return err;
1981}
1982
Avantika Mathur09b88252006-12-06 20:41:36 -08001983static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001984ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05001985 __u32 len, ext4_fsblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07001986{
1987 struct ext4_ext_cache *cex;
1988 BUG_ON(len == 0);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001989 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001990 cex = &EXT4_I(inode)->i_cached_extent;
Alex Tomasa86c6182006-10-11 01:21:03 -07001991 cex->ec_block = block;
1992 cex->ec_len = len;
1993 cex->ec_start = start;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001994 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001995}
1996
1997/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001998 * ext4_ext_put_gap_in_cache:
1999 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07002000 * and cache this gap
2001 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002002static void
Alex Tomasa86c6182006-10-11 01:21:03 -07002003ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002004 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07002005{
2006 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002007 unsigned long len;
2008 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07002009 struct ext4_extent *ex;
2010
2011 ex = path[depth].p_ext;
2012 if (ex == NULL) {
2013 /* there is no extent yet, so gap is [0;-] */
2014 lblock = 0;
2015 len = EXT_MAX_BLOCK;
2016 ext_debug("cache gap(whole file):");
2017 } else if (block < le32_to_cpu(ex->ee_block)) {
2018 lblock = block;
2019 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05002020 ext_debug("cache gap(before): %u [%u:%u]",
2021 block,
2022 le32_to_cpu(ex->ee_block),
2023 ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002024 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002025 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002026 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002027 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002028 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002029
2030 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05002031 ext_debug("cache gap(after): [%u:%u] %u",
2032 le32_to_cpu(ex->ee_block),
2033 ext4_ext_get_actual_len(ex),
2034 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002035 BUG_ON(next == lblock);
2036 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07002037 } else {
2038 lblock = len = 0;
2039 BUG();
2040 }
2041
Eric Sandeenbba90742008-01-28 23:58:27 -05002042 ext_debug(" -> %u:%lu\n", lblock, len);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002043 ext4_ext_put_in_cache(inode, lblock, len, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002044}
2045
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002046/*
2047 * Return 0 if cache is invalid; 1 if the cache is valid
2048 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002049static int
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002050ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
Alex Tomasa86c6182006-10-11 01:21:03 -07002051 struct ext4_extent *ex)
2052{
2053 struct ext4_ext_cache *cex;
Vivek Haldar77f41352011-05-22 21:24:16 -04002054 struct ext4_sb_info *sbi;
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002055 int ret = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002056
Theodore Ts'o60e66792010-05-17 07:00:00 -04002057 /*
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002058 * We borrow i_block_reservation_lock to protect i_cached_extent
2059 */
2060 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07002061 cex = &EXT4_I(inode)->i_cached_extent;
Vivek Haldar77f41352011-05-22 21:24:16 -04002062 sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002063
2064 /* has cache valid data? */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002065 if (cex->ec_len == 0)
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002066 goto errout;
Alex Tomasa86c6182006-10-11 01:21:03 -07002067
Akinobu Mita731eb1a2010-03-03 23:55:01 -05002068 if (in_range(block, cex->ec_block, cex->ec_len)) {
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002069 ex->ee_block = cpu_to_le32(cex->ec_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002070 ext4_ext_store_pblock(ex, cex->ec_start);
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002071 ex->ee_len = cpu_to_le16(cex->ec_len);
Eric Sandeenbba90742008-01-28 23:58:27 -05002072 ext_debug("%u cached by %u:%u:%llu\n",
2073 block,
2074 cex->ec_block, cex->ec_len, cex->ec_start);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002075 ret = 1;
Alex Tomasa86c6182006-10-11 01:21:03 -07002076 }
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002077errout:
Vivek Haldar77f41352011-05-22 21:24:16 -04002078 if (!ret)
2079 sbi->extent_cache_misses++;
2080 else
2081 sbi->extent_cache_hits++;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002082 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
2083 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07002084}
2085
2086/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002087 * ext4_ext_rm_idx:
2088 * removes index from the index block.
2089 * It's used in truncate case only, thus all requests are for
2090 * last index in the block only.
Alex Tomasa86c6182006-10-11 01:21:03 -07002091 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002092static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07002093 struct ext4_ext_path *path)
2094{
Alex Tomasa86c6182006-10-11 01:21:03 -07002095 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002096 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002097
2098 /* free index block */
2099 path--;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002100 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002101 if (unlikely(path->p_hdr->eh_entries == 0)) {
2102 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2103 return -EIO;
2104 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002105 err = ext4_ext_get_access(handle, inode, path);
2106 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002107 return err;
Marcin Slusarze8546d02008-04-17 10:38:59 -04002108 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002109 err = ext4_ext_dirty(handle, inode, path);
2110 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002111 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002112 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Peter Huewe7dc57612011-02-21 21:01:42 -05002113 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002114 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Alex Tomasa86c6182006-10-11 01:21:03 -07002115 return err;
2116}
2117
2118/*
Mingming Caoee12b632008-08-19 22:16:05 -04002119 * ext4_ext_calc_credits_for_single_extent:
2120 * This routine returns max. credits that needed to insert an extent
2121 * to the extent tree.
2122 * When pass the actual path, the caller should calculate credits
2123 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002124 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002125int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002126 struct ext4_ext_path *path)
2127{
Alex Tomasa86c6182006-10-11 01:21:03 -07002128 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002129 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002130 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002131
Alex Tomasa86c6182006-10-11 01:21:03 -07002132 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002133 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002134 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2135
2136 /*
2137 * There are some space in the leaf tree, no
2138 * need to account for leaf block credit
2139 *
2140 * bitmaps and block group descriptor blocks
2141 * and other metadat blocks still need to be
2142 * accounted.
2143 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002144 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002145 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002146 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002147 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002148 }
2149
Mingming Cao525f4ed2008-08-19 22:15:58 -04002150 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002151}
Alex Tomasa86c6182006-10-11 01:21:03 -07002152
Mingming Caoee12b632008-08-19 22:16:05 -04002153/*
2154 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2155 *
2156 * if nrblocks are fit in a single extent (chunk flag is 1), then
2157 * in the worse case, each tree level index/leaf need to be changed
2158 * if the tree split due to insert a new extent, then the old tree
2159 * index/leaf need to be updated too
2160 *
2161 * If the nrblocks are discontiguous, they could cause
2162 * the whole tree split more than once, but this is really rare.
2163 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002164int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoee12b632008-08-19 22:16:05 -04002165{
2166 int index;
2167 int depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002168
Mingming Caoee12b632008-08-19 22:16:05 -04002169 if (chunk)
2170 index = depth * 2;
2171 else
2172 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002173
Mingming Caoee12b632008-08-19 22:16:05 -04002174 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002175}
2176
2177static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2178 struct ext4_extent *ex,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002179 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002180{
Amit Aroraa2df2a62007-07-17 21:42:41 -04002181 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe6362602009-11-23 07:17:05 -05002182 int flags = EXT4_FREE_BLOCKS_FORGET;
Alex Tomasa86c6182006-10-11 01:21:03 -07002183
Alex Tomasc9de5602008-01-29 00:19:52 -05002184 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
Theodore Ts'oe6362602009-11-23 07:17:05 -05002185 flags |= EXT4_FREE_BLOCKS_METADATA;
Alex Tomasa86c6182006-10-11 01:21:03 -07002186#ifdef EXTENTS_STATS
2187 {
2188 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002189 spin_lock(&sbi->s_ext_stats_lock);
2190 sbi->s_ext_blocks += ee_len;
2191 sbi->s_ext_extents++;
2192 if (ee_len < sbi->s_ext_min)
2193 sbi->s_ext_min = ee_len;
2194 if (ee_len > sbi->s_ext_max)
2195 sbi->s_ext_max = ee_len;
2196 if (ext_depth(inode) > sbi->s_depth_max)
2197 sbi->s_depth_max = ext_depth(inode);
2198 spin_unlock(&sbi->s_ext_stats_lock);
2199 }
2200#endif
2201 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002202 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002203 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002204 ext4_lblk_t num;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002205 ext4_fsblk_t start;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002206
Amit Aroraa2df2a62007-07-17 21:42:41 -04002207 num = le32_to_cpu(ex->ee_block) + ee_len - from;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002208 start = ext4_ext_pblock(ex) + ee_len - num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002209 ext_debug("free last %u blocks starting %llu\n", num, start);
Peter Huewe7dc57612011-02-21 21:01:42 -05002210 ext4_free_blocks(handle, inode, NULL, start, num, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07002211 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002212 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002213 /* head removal */
2214 ext4_lblk_t num;
2215 ext4_fsblk_t start;
2216
2217 num = to - from;
2218 start = ext4_ext_pblock(ex);
2219
2220 ext_debug("free first %u blocks starting %llu\n", num, start);
2221 ext4_free_blocks(handle, inode, 0, start, num, flags);
2222
Alex Tomasa86c6182006-10-11 01:21:03 -07002223 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002224 printk(KERN_INFO "strange request: removal(2) "
2225 "%u-%u from %u:%u\n",
2226 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002227 }
2228 return 0;
2229}
2230
Allison Hendersond583fb82011-05-25 07:41:43 -04002231
2232/*
2233 * ext4_ext_rm_leaf() Removes the extents associated with the
2234 * blocks appearing between "start" and "end", and splits the extents
2235 * if "start" and "end" appear in the same extent
2236 *
2237 * @handle: The journal handle
2238 * @inode: The files inode
2239 * @path: The path to the leaf
2240 * @start: The first block to remove
2241 * @end: The last block to remove
2242 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002243static int
2244ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Allison Hendersond583fb82011-05-25 07:41:43 -04002245 struct ext4_ext_path *path, ext4_lblk_t start,
2246 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002247{
2248 int err = 0, correct_index = 0;
2249 int depth = ext_depth(inode), credits;
2250 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002251 ext4_lblk_t a, b, block;
2252 unsigned num;
2253 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002254 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002255 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002256 struct ext4_extent *ex;
Allison Hendersond583fb82011-05-25 07:41:43 -04002257 struct ext4_map_blocks map;
Alex Tomasa86c6182006-10-11 01:21:03 -07002258
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002259 /* the header must be checked already in ext4_ext_remove_space() */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002260 ext_debug("truncate since %u in leaf\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002261 if (!path[depth].p_hdr)
2262 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2263 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002264 if (unlikely(path[depth].p_hdr == NULL)) {
2265 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2266 return -EIO;
2267 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002268 /* find where to start removing */
2269 ex = EXT_LAST_EXTENT(eh);
2270
2271 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002272 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002273
2274 while (ex >= EXT_FIRST_EXTENT(eh) &&
2275 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002276
2277 if (ext4_ext_is_uninitialized(ex))
2278 uninitialized = 1;
2279 else
2280 uninitialized = 0;
2281
Mingming553f9002009-09-18 13:34:55 -04002282 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2283 uninitialized, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002284 path[depth].p_ext = ex;
2285
2286 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002287 b = ex_ee_block+ex_ee_len - 1 < end ?
2288 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002289
2290 ext_debug(" border %u:%u\n", a, b);
2291
Allison Hendersond583fb82011-05-25 07:41:43 -04002292 /* If this extent is beyond the end of the hole, skip it */
2293 if (end <= ex_ee_block) {
2294 ex--;
2295 ex_ee_block = le32_to_cpu(ex->ee_block);
2296 ex_ee_len = ext4_ext_get_actual_len(ex);
2297 continue;
2298 } else if (a != ex_ee_block &&
2299 b != ex_ee_block + ex_ee_len - 1) {
2300 /*
2301 * If this is a truncate, then this condition should
2302 * never happen because at least one of the end points
2303 * needs to be on the edge of the extent.
2304 */
2305 if (end == EXT_MAX_BLOCK) {
2306 ext_debug(" bad truncate %u:%u\n",
2307 start, end);
2308 block = 0;
2309 num = 0;
2310 err = -EIO;
2311 goto out;
2312 }
2313 /*
2314 * else this is a hole punch, so the extent needs to
2315 * be split since neither edge of the hole is on the
2316 * extent edge
2317 */
2318 else{
2319 map.m_pblk = ext4_ext_pblock(ex);
2320 map.m_lblk = ex_ee_block;
2321 map.m_len = b - ex_ee_block;
2322
2323 err = ext4_split_extent(handle,
2324 inode, path, &map, 0,
2325 EXT4_GET_BLOCKS_PUNCH_OUT_EXT |
2326 EXT4_GET_BLOCKS_PRE_IO);
2327
2328 if (err < 0)
2329 goto out;
2330
2331 ex_ee_len = ext4_ext_get_actual_len(ex);
2332
2333 b = ex_ee_block+ex_ee_len - 1 < end ?
2334 ex_ee_block+ex_ee_len - 1 : end;
2335
2336 /* Then remove tail of this extent */
2337 block = ex_ee_block;
2338 num = a - block;
2339 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002340 } else if (a != ex_ee_block) {
2341 /* remove tail of the extent */
2342 block = ex_ee_block;
2343 num = a - block;
2344 } else if (b != ex_ee_block + ex_ee_len - 1) {
2345 /* remove head of the extent */
Allison Hendersond583fb82011-05-25 07:41:43 -04002346 block = b;
2347 num = ex_ee_block + ex_ee_len - b;
2348
2349 /*
2350 * If this is a truncate, this condition
2351 * should never happen
2352 */
2353 if (end == EXT_MAX_BLOCK) {
2354 ext_debug(" bad truncate %u:%u\n",
2355 start, end);
2356 err = -EIO;
2357 goto out;
2358 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002359 } else {
2360 /* remove whole extent: excellent! */
2361 block = ex_ee_block;
2362 num = 0;
Allison Hendersond583fb82011-05-25 07:41:43 -04002363 if (a != ex_ee_block) {
2364 ext_debug(" bad truncate %u:%u\n",
2365 start, end);
2366 err = -EIO;
2367 goto out;
2368 }
2369
2370 if (b != ex_ee_block + ex_ee_len - 1) {
2371 ext_debug(" bad truncate %u:%u\n",
2372 start, end);
2373 err = -EIO;
2374 goto out;
2375 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002376 }
2377
Theodore Ts'o34071da2008-08-01 21:59:19 -04002378 /*
2379 * 3 for leaf, sb, and inode plus 2 (bmap and group
2380 * descriptor) for each block group; assume two block
2381 * groups plus ex_ee_len/blocks_per_block_group for
2382 * the worst case
2383 */
2384 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002385 if (ex == EXT_FIRST_EXTENT(eh)) {
2386 correct_index = 1;
2387 credits += (ext_depth(inode)) + 1;
2388 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002389 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002390
Jan Kara487caee2009-08-17 22:17:20 -04002391 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
Shen Feng9102e4f2008-07-11 19:27:31 -04002392 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002393 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002394
2395 err = ext4_ext_get_access(handle, inode, path + depth);
2396 if (err)
2397 goto out;
2398
2399 err = ext4_remove_blocks(handle, inode, ex, a, b);
2400 if (err)
2401 goto out;
2402
2403 if (num == 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002404 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002405 ext4_ext_store_pblock(ex, 0);
Allison Hendersond583fb82011-05-25 07:41:43 -04002406 } else if (block != ex_ee_block) {
2407 /*
2408 * If this was a head removal, then we need to update
2409 * the physical block since it is now at a different
2410 * location
2411 */
2412 ext4_ext_store_pblock(ex, ext4_ext_pblock(ex) + (b-a));
Alex Tomasa86c6182006-10-11 01:21:03 -07002413 }
2414
2415 ex->ee_block = cpu_to_le32(block);
2416 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002417 /*
2418 * Do not mark uninitialized if all the blocks in the
2419 * extent have been removed.
2420 */
2421 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002422 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002423
2424 err = ext4_ext_dirty(handle, inode, path + depth);
2425 if (err)
2426 goto out;
2427
Allison Hendersond583fb82011-05-25 07:41:43 -04002428 /*
2429 * If the extent was completely released,
2430 * we need to remove it from the leaf
2431 */
2432 if (num == 0) {
2433 if (end != EXT_MAX_BLOCK) {
2434 /*
2435 * For hole punching, we need to scoot all the
2436 * extents up when an extent is removed so that
2437 * we dont have blank extents in the middle
2438 */
2439 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2440 sizeof(struct ext4_extent));
2441
2442 /* Now get rid of the one at the end */
2443 memset(EXT_LAST_EXTENT(eh), 0,
2444 sizeof(struct ext4_extent));
2445 }
2446 le16_add_cpu(&eh->eh_entries, -1);
2447 }
2448
Mingming Cao2ae02102006-10-11 01:21:11 -07002449 ext_debug("new extent: %u:%u:%llu\n", block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002450 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002451 ex--;
2452 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002453 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002454 }
2455
2456 if (correct_index && eh->eh_entries)
2457 err = ext4_ext_correct_indexes(handle, inode, path);
2458
2459 /* if this leaf is free, then we should
2460 * remove it from index block above */
2461 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2462 err = ext4_ext_rm_idx(handle, inode, path + depth);
2463
2464out:
2465 return err;
2466}
2467
2468/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002469 * ext4_ext_more_to_rm:
2470 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002471 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002472static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002473ext4_ext_more_to_rm(struct ext4_ext_path *path)
2474{
2475 BUG_ON(path->p_idx == NULL);
2476
2477 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2478 return 0;
2479
2480 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002481 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002482 * so we have to consider current index for truncation
2483 */
2484 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2485 return 0;
2486 return 1;
2487}
2488
Allison Hendersond583fb82011-05-25 07:41:43 -04002489static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2490 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002491{
2492 struct super_block *sb = inode->i_sb;
2493 int depth = ext_depth(inode);
2494 struct ext4_ext_path *path;
2495 handle_t *handle;
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002496 int i, err;
Alex Tomasa86c6182006-10-11 01:21:03 -07002497
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002498 ext_debug("truncate since %u\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002499
2500 /* probably first extent we're gonna free will be last in block */
2501 handle = ext4_journal_start(inode, depth + 1);
2502 if (IS_ERR(handle))
2503 return PTR_ERR(handle);
2504
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002505again:
Alex Tomasa86c6182006-10-11 01:21:03 -07002506 ext4_ext_invalidate_cache(inode);
2507
2508 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002509 * We start scanning from right side, freeing all the blocks
2510 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002511 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002512 depth = ext_depth(inode);
Josef Bacik216553c2008-04-29 22:02:02 -04002513 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07002514 if (path == NULL) {
2515 ext4_journal_stop(handle);
2516 return -ENOMEM;
2517 }
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002518 path[0].p_depth = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -07002519 path[0].p_hdr = ext_inode_hdr(inode);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002520 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002521 err = -EIO;
2522 goto out;
2523 }
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002524 i = err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002525
2526 while (i >= 0 && err == 0) {
2527 if (i == depth) {
2528 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04002529 err = ext4_ext_rm_leaf(handle, inode, path,
2530 start, end);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002531 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002532 brelse(path[i].p_bh);
2533 path[i].p_bh = NULL;
2534 i--;
2535 continue;
2536 }
2537
2538 /* this is index block */
2539 if (!path[i].p_hdr) {
2540 ext_debug("initialize header\n");
2541 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002542 }
2543
Alex Tomasa86c6182006-10-11 01:21:03 -07002544 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002545 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002546 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2547 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2548 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2549 path[i].p_hdr,
2550 le16_to_cpu(path[i].p_hdr->eh_entries));
2551 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002552 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002553 path[i].p_idx--;
2554 }
2555
2556 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2557 i, EXT_FIRST_INDEX(path[i].p_hdr),
2558 path[i].p_idx);
2559 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002560 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002561 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002562 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04002563 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002564 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'obf89d162010-10-27 21:30:14 -04002565 bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002566 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002567 /* should we reset i_size? */
2568 err = -EIO;
2569 break;
2570 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002571 if (WARN_ON(i + 1 > depth)) {
2572 err = -EIO;
2573 break;
2574 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002575 if (ext4_ext_check(inode, ext_block_hdr(bh),
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002576 depth - i - 1)) {
2577 err = -EIO;
2578 break;
2579 }
2580 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002581
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002582 /* save actual number of indexes since this
2583 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002584 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2585 i++;
2586 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002587 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002588 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002589 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002590 * handle must be already prepared by the
2591 * truncatei_leaf() */
2592 err = ext4_ext_rm_idx(handle, inode, path + i);
2593 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002594 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002595 brelse(path[i].p_bh);
2596 path[i].p_bh = NULL;
2597 i--;
2598 ext_debug("return to level %d\n", i);
2599 }
2600 }
2601
2602 /* TODO: flexible tree reduction should be here */
2603 if (path->p_hdr->eh_entries == 0) {
2604 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002605 * truncate to zero freed all the tree,
2606 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002607 */
2608 err = ext4_ext_get_access(handle, inode, path);
2609 if (err == 0) {
2610 ext_inode_hdr(inode)->eh_depth = 0;
2611 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04002612 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07002613 err = ext4_ext_dirty(handle, inode, path);
2614 }
2615 }
2616out:
Alex Tomasa86c6182006-10-11 01:21:03 -07002617 ext4_ext_drop_refs(path);
2618 kfree(path);
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002619 if (err == -EAGAIN)
2620 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07002621 ext4_journal_stop(handle);
2622
2623 return err;
2624}
2625
2626/*
2627 * called at mount time
2628 */
2629void ext4_ext_init(struct super_block *sb)
2630{
2631 /*
2632 * possible initialization would be here
2633 */
2634
Theodore Ts'o83982b62009-01-06 14:53:16 -05002635 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04002636#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002637 printk(KERN_INFO "EXT4-fs: file extents enabled");
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002638#ifdef AGGRESSIVE_TEST
2639 printk(", aggressive tests");
Alex Tomasa86c6182006-10-11 01:21:03 -07002640#endif
2641#ifdef CHECK_BINSEARCH
2642 printk(", check binsearch");
2643#endif
2644#ifdef EXTENTS_STATS
2645 printk(", stats");
2646#endif
2647 printk("\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04002648#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07002649#ifdef EXTENTS_STATS
2650 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2651 EXT4_SB(sb)->s_ext_min = 1 << 30;
2652 EXT4_SB(sb)->s_ext_max = 0;
2653#endif
2654 }
2655}
2656
2657/*
2658 * called at umount time
2659 */
2660void ext4_ext_release(struct super_block *sb)
2661{
Theodore Ts'o83982b62009-01-06 14:53:16 -05002662 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
Alex Tomasa86c6182006-10-11 01:21:03 -07002663 return;
2664
2665#ifdef EXTENTS_STATS
2666 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2667 struct ext4_sb_info *sbi = EXT4_SB(sb);
2668 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2669 sbi->s_ext_blocks, sbi->s_ext_extents,
2670 sbi->s_ext_blocks / sbi->s_ext_extents);
2671 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2672 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2673 }
2674#endif
2675}
2676
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002677/* FIXME!! we need to try to merge to left or right after zero-out */
2678static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2679{
Lukas Czerner24075182010-10-27 21:30:06 -04002680 ext4_fsblk_t ee_pblock;
2681 unsigned int ee_len;
Jing Zhangb7203032010-05-12 00:00:00 -04002682 int ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002683
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002684 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04002685 ee_pblock = ext4_ext_pblock(ex);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002686
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04002687 ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
Lukas Czerner24075182010-10-27 21:30:06 -04002688 if (ret > 0)
2689 ret = 0;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002690
Lukas Czerner24075182010-10-27 21:30:06 -04002691 return ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002692}
2693
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002694/*
2695 * used by extent splitting.
2696 */
2697#define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
2698 due to ENOSPC */
2699#define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */
2700#define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */
2701
2702/*
2703 * ext4_split_extent_at() splits an extent at given block.
2704 *
2705 * @handle: the journal handle
2706 * @inode: the file inode
2707 * @path: the path to the extent
2708 * @split: the logical block where the extent is splitted.
2709 * @split_flags: indicates if the extent could be zeroout if split fails, and
2710 * the states(init or uninit) of new extents.
2711 * @flags: flags used to insert new extent to extent tree.
2712 *
2713 *
2714 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
2715 * of which are deterimined by split_flag.
2716 *
2717 * There are two cases:
2718 * a> the extent are splitted into two extent.
2719 * b> split is not needed, and just mark the extent.
2720 *
2721 * return 0 on success.
2722 */
2723static int ext4_split_extent_at(handle_t *handle,
2724 struct inode *inode,
2725 struct ext4_ext_path *path,
2726 ext4_lblk_t split,
2727 int split_flag,
2728 int flags)
2729{
2730 ext4_fsblk_t newblock;
2731 ext4_lblk_t ee_block;
2732 struct ext4_extent *ex, newex, orig_ex;
2733 struct ext4_extent *ex2 = NULL;
2734 unsigned int ee_len, depth;
2735 int err = 0;
2736
2737 ext_debug("ext4_split_extents_at: inode %lu, logical"
2738 "block %llu\n", inode->i_ino, (unsigned long long)split);
2739
2740 ext4_ext_show_leaf(inode, path);
2741
2742 depth = ext_depth(inode);
2743 ex = path[depth].p_ext;
2744 ee_block = le32_to_cpu(ex->ee_block);
2745 ee_len = ext4_ext_get_actual_len(ex);
2746 newblock = split - ee_block + ext4_ext_pblock(ex);
2747
2748 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
2749
2750 err = ext4_ext_get_access(handle, inode, path + depth);
2751 if (err)
2752 goto out;
2753
2754 if (split == ee_block) {
2755 /*
2756 * case b: block @split is the block that the extent begins with
2757 * then we just change the state of the extent, and splitting
2758 * is not needed.
2759 */
2760 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2761 ext4_ext_mark_uninitialized(ex);
2762 else
2763 ext4_ext_mark_initialized(ex);
2764
2765 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
2766 ext4_ext_try_to_merge(inode, path, ex);
2767
2768 err = ext4_ext_dirty(handle, inode, path + depth);
2769 goto out;
2770 }
2771
2772 /* case a */
2773 memcpy(&orig_ex, ex, sizeof(orig_ex));
2774 ex->ee_len = cpu_to_le16(split - ee_block);
2775 if (split_flag & EXT4_EXT_MARK_UNINIT1)
2776 ext4_ext_mark_uninitialized(ex);
2777
2778 /*
2779 * path may lead to new leaf, not to original leaf any more
2780 * after ext4_ext_insert_extent() returns,
2781 */
2782 err = ext4_ext_dirty(handle, inode, path + depth);
2783 if (err)
2784 goto fix_extent_len;
2785
2786 ex2 = &newex;
2787 ex2->ee_block = cpu_to_le32(split);
2788 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
2789 ext4_ext_store_pblock(ex2, newblock);
2790 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2791 ext4_ext_mark_uninitialized(ex2);
2792
2793 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
2794 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2795 err = ext4_ext_zeroout(inode, &orig_ex);
2796 if (err)
2797 goto fix_extent_len;
2798 /* update the extent length and mark as initialized */
2799 ex->ee_len = cpu_to_le32(ee_len);
2800 ext4_ext_try_to_merge(inode, path, ex);
2801 err = ext4_ext_dirty(handle, inode, path + depth);
2802 goto out;
2803 } else if (err)
2804 goto fix_extent_len;
2805
2806out:
2807 ext4_ext_show_leaf(inode, path);
2808 return err;
2809
2810fix_extent_len:
2811 ex->ee_len = orig_ex.ee_len;
2812 ext4_ext_dirty(handle, inode, path + depth);
2813 return err;
2814}
2815
2816/*
2817 * ext4_split_extents() splits an extent and mark extent which is covered
2818 * by @map as split_flags indicates
2819 *
2820 * It may result in splitting the extent into multiple extents (upto three)
2821 * There are three possibilities:
2822 * a> There is no split required
2823 * b> Splits in two extents: Split is happening at either end of the extent
2824 * c> Splits in three extents: Somone is splitting in middle of the extent
2825 *
2826 */
2827static int ext4_split_extent(handle_t *handle,
2828 struct inode *inode,
2829 struct ext4_ext_path *path,
2830 struct ext4_map_blocks *map,
2831 int split_flag,
2832 int flags)
2833{
2834 ext4_lblk_t ee_block;
2835 struct ext4_extent *ex;
2836 unsigned int ee_len, depth;
2837 int err = 0;
2838 int uninitialized;
2839 int split_flag1, flags1;
2840
2841 depth = ext_depth(inode);
2842 ex = path[depth].p_ext;
2843 ee_block = le32_to_cpu(ex->ee_block);
2844 ee_len = ext4_ext_get_actual_len(ex);
2845 uninitialized = ext4_ext_is_uninitialized(ex);
2846
2847 if (map->m_lblk + map->m_len < ee_block + ee_len) {
2848 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2849 EXT4_EXT_MAY_ZEROOUT : 0;
2850 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
2851 if (uninitialized)
2852 split_flag1 |= EXT4_EXT_MARK_UNINIT1 |
2853 EXT4_EXT_MARK_UNINIT2;
2854 err = ext4_split_extent_at(handle, inode, path,
2855 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04002856 if (err)
2857 goto out;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002858 }
2859
2860 ext4_ext_drop_refs(path);
2861 path = ext4_ext_find_extent(inode, map->m_lblk, path);
2862 if (IS_ERR(path))
2863 return PTR_ERR(path);
2864
2865 if (map->m_lblk >= ee_block) {
2866 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2867 EXT4_EXT_MAY_ZEROOUT : 0;
2868 if (uninitialized)
2869 split_flag1 |= EXT4_EXT_MARK_UNINIT1;
2870 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2871 split_flag1 |= EXT4_EXT_MARK_UNINIT2;
2872 err = ext4_split_extent_at(handle, inode, path,
2873 map->m_lblk, split_flag1, flags);
2874 if (err)
2875 goto out;
2876 }
2877
2878 ext4_ext_show_leaf(inode, path);
2879out:
2880 return err ? err : map->m_len;
2881}
2882
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002883#define EXT4_EXT_ZERO_LEN 7
Amit Arora56055d32007-07-17 21:42:38 -04002884/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002885 * This function is called by ext4_ext_map_blocks() if someone tries to write
Amit Arora56055d32007-07-17 21:42:38 -04002886 * to an uninitialized extent. It may result in splitting the uninitialized
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002887 * extent into multiple extents (up to three - one initialized and two
Amit Arora56055d32007-07-17 21:42:38 -04002888 * uninitialized).
2889 * There are three possibilities:
2890 * a> There is no split required: Entire extent should be initialized
2891 * b> Splits in two extents: Write is happening at either end of the extent
2892 * c> Splits in three extents: Somone is writing in middle of the extent
2893 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002894static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002895 struct inode *inode,
2896 struct ext4_map_blocks *map,
2897 struct ext4_ext_path *path)
Amit Arora56055d32007-07-17 21:42:38 -04002898{
Yongqiang Yang667eff32011-05-03 12:25:07 -04002899 struct ext4_map_blocks split_map;
2900 struct ext4_extent zero_ex;
2901 struct ext4_extent *ex;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002902 ext4_lblk_t ee_block, eof_block;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002903 unsigned int allocated, ee_len, depth;
Amit Arora56055d32007-07-17 21:42:38 -04002904 int err = 0;
Yongqiang Yang667eff32011-05-03 12:25:07 -04002905 int split_flag = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002906
2907 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
2908 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002909 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002910
2911 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
2912 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002913 if (eof_block < map->m_lblk + map->m_len)
2914 eof_block = map->m_lblk + map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04002915
2916 depth = ext_depth(inode);
Amit Arora56055d32007-07-17 21:42:38 -04002917 ex = path[depth].p_ext;
2918 ee_block = le32_to_cpu(ex->ee_block);
2919 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002920 allocated = ee_len - (map->m_lblk - ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002921
Yongqiang Yang667eff32011-05-03 12:25:07 -04002922 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002923 /*
2924 * It is safe to convert extent to initialized via explicit
2925 * zeroout only if extent is fully insde i_size or new_size.
2926 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04002927 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002928
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002929 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
Yongqiang Yang667eff32011-05-03 12:25:07 -04002930 if (ee_len <= 2*EXT4_EXT_ZERO_LEN &&
2931 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2932 err = ext4_ext_zeroout(inode, ex);
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002933 if (err)
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002934 goto out;
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002935
2936 err = ext4_ext_get_access(handle, inode, path + depth);
2937 if (err)
2938 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04002939 ext4_ext_mark_initialized(ex);
2940 ext4_ext_try_to_merge(inode, path, ex);
2941 err = ext4_ext_dirty(handle, inode, path + depth);
2942 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04002943 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04002944
Amit Arora56055d32007-07-17 21:42:38 -04002945 /*
Yongqiang Yang667eff32011-05-03 12:25:07 -04002946 * four cases:
2947 * 1. split the extent into three extents.
2948 * 2. split the extent into two extents, zeroout the first half.
2949 * 3. split the extent into two extents, zeroout the second half.
2950 * 4. split the extent into two extents with out zeroout.
Amit Arora56055d32007-07-17 21:42:38 -04002951 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04002952 split_map.m_lblk = map->m_lblk;
2953 split_map.m_len = map->m_len;
2954
2955 if (allocated > map->m_len) {
2956 if (allocated <= EXT4_EXT_ZERO_LEN &&
2957 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2958 /* case 3 */
2959 zero_ex.ee_block =
Allison Henderson9b940f82011-05-16 10:11:09 -04002960 cpu_to_le32(map->m_lblk);
2961 zero_ex.ee_len = cpu_to_le16(allocated);
Yongqiang Yang667eff32011-05-03 12:25:07 -04002962 ext4_ext_store_pblock(&zero_ex,
2963 ext4_ext_pblock(ex) + map->m_lblk - ee_block);
2964 err = ext4_ext_zeroout(inode, &zero_ex);
Amit Arora56055d32007-07-17 21:42:38 -04002965 if (err)
2966 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04002967 split_map.m_lblk = map->m_lblk;
2968 split_map.m_len = allocated;
2969 } else if ((map->m_lblk - ee_block + map->m_len <
2970 EXT4_EXT_ZERO_LEN) &&
2971 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2972 /* case 2 */
2973 if (map->m_lblk != ee_block) {
2974 zero_ex.ee_block = ex->ee_block;
2975 zero_ex.ee_len = cpu_to_le16(map->m_lblk -
2976 ee_block);
2977 ext4_ext_store_pblock(&zero_ex,
2978 ext4_ext_pblock(ex));
2979 err = ext4_ext_zeroout(inode, &zero_ex);
2980 if (err)
2981 goto out;
2982 }
2983
Yongqiang Yang667eff32011-05-03 12:25:07 -04002984 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04002985 split_map.m_len = map->m_lblk - ee_block + map->m_len;
2986 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04002987 }
2988 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04002989
2990 allocated = ext4_split_extent(handle, inode, path,
2991 &split_map, split_flag, 0);
2992 if (allocated < 0)
2993 err = allocated;
2994
Amit Arora56055d32007-07-17 21:42:38 -04002995out:
2996 return err ? err : allocated;
2997}
2998
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05002999/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003000 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003001 * ext4_get_blocks_dio_write() when DIO to write
3002 * to an uninitialized extent.
3003 *
Paul Bollefd018fe2011-02-15 00:05:43 +01003004 * Writing to an uninitialized extent may result in splitting the uninitialized
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003005 * extent into multiple /initialized uninitialized extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003006 * There are three possibilities:
3007 * a> There is no split required: Entire extent should be uninitialized
3008 * b> Splits in two extents: Write is happening at either end of the extent
3009 * c> Splits in three extents: Somone is writing in middle of the extent
3010 *
3011 * One of more index blocks maybe needed if the extent tree grow after
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003012 * the uninitialized extent split. To prevent ENOSPC occur at the IO
Mingming Cao00314622009-09-28 15:49:08 -04003013 * complete, we need to split the uninitialized extent before DIO submit
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02003014 * the IO. The uninitialized extent called at this time will be split
Mingming Cao00314622009-09-28 15:49:08 -04003015 * into three uninitialized extent(at most). After IO complete, the part
3016 * being filled will be convert to initialized by the end_io callback function
3017 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003018 *
3019 * Returns the size of uninitialized extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003020 */
3021static int ext4_split_unwritten_extents(handle_t *handle,
3022 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003023 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003024 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04003025 int flags)
3026{
Yongqiang Yang667eff32011-05-03 12:25:07 -04003027 ext4_lblk_t eof_block;
3028 ext4_lblk_t ee_block;
3029 struct ext4_extent *ex;
3030 unsigned int ee_len;
3031 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003032
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003033 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
3034 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003035 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003036
3037 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3038 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003039 if (eof_block < map->m_lblk + map->m_len)
3040 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003041 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003042 * 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 depth = ext_depth(inode);
3046 ex = path[depth].p_ext;
3047 ee_block = le32_to_cpu(ex->ee_block);
3048 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003049
Yongqiang Yang667eff32011-05-03 12:25:07 -04003050 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
3051 split_flag |= EXT4_EXT_MARK_UNINIT2;
Mingming Cao00314622009-09-28 15:49:08 -04003052
Yongqiang Yang667eff32011-05-03 12:25:07 -04003053 flags |= EXT4_GET_BLOCKS_PRE_IO;
3054 return ext4_split_extent(handle, inode, path, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003055}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003056
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003057static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003058 struct inode *inode,
3059 struct ext4_ext_path *path)
3060{
3061 struct ext4_extent *ex;
3062 struct ext4_extent_header *eh;
3063 int depth;
3064 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003065
3066 depth = ext_depth(inode);
3067 eh = path[depth].p_hdr;
3068 ex = path[depth].p_ext;
3069
Yongqiang Yang197217a2011-05-03 11:45:29 -04003070 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3071 "block %llu, max_blocks %u\n", inode->i_ino,
3072 (unsigned long long)le32_to_cpu(ex->ee_block),
3073 ext4_ext_get_actual_len(ex));
3074
Mingming Cao00314622009-09-28 15:49:08 -04003075 err = ext4_ext_get_access(handle, inode, path + depth);
3076 if (err)
3077 goto out;
3078 /* first mark the extent as initialized */
3079 ext4_ext_mark_initialized(ex);
3080
Yongqiang Yang197217a2011-05-03 11:45:29 -04003081 /* note: ext4_ext_correct_indexes() isn't needed here because
3082 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003083 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04003084 ext4_ext_try_to_merge(inode, path, ex);
3085
Mingming Cao00314622009-09-28 15:49:08 -04003086 /* Mark modified extent as dirty */
3087 err = ext4_ext_dirty(handle, inode, path + depth);
3088out:
3089 ext4_ext_show_leaf(inode, path);
3090 return err;
3091}
3092
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003093static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3094 sector_t block, int count)
3095{
3096 int i;
3097 for (i = 0; i < count; i++)
3098 unmap_underlying_metadata(bdev, block + i);
3099}
3100
Theodore Ts'o58590b02010-10-27 21:23:12 -04003101/*
3102 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3103 */
3104static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
Eric Sandeend002ebf2011-01-10 13:03:35 -05003105 ext4_lblk_t lblk,
Theodore Ts'o58590b02010-10-27 21:23:12 -04003106 struct ext4_ext_path *path,
3107 unsigned int len)
3108{
3109 int i, depth;
3110 struct ext4_extent_header *eh;
Sergey Senozhatsky65922cb2011-03-23 14:08:27 -04003111 struct ext4_extent *last_ex;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003112
3113 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3114 return 0;
3115
3116 depth = ext_depth(inode);
3117 eh = path[depth].p_hdr;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003118
3119 if (unlikely(!eh->eh_entries)) {
3120 EXT4_ERROR_INODE(inode, "eh->eh_entries == 0 and "
3121 "EOFBLOCKS_FL set");
3122 return -EIO;
3123 }
3124 last_ex = EXT_LAST_EXTENT(eh);
3125 /*
3126 * We should clear the EOFBLOCKS_FL flag if we are writing the
3127 * last block in the last extent in the file. We test this by
3128 * first checking to see if the caller to
3129 * ext4_ext_get_blocks() was interested in the last block (or
3130 * a block beyond the last block) in the current extent. If
3131 * this turns out to be false, we can bail out from this
3132 * function immediately.
3133 */
Eric Sandeend002ebf2011-01-10 13:03:35 -05003134 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
Theodore Ts'o58590b02010-10-27 21:23:12 -04003135 ext4_ext_get_actual_len(last_ex))
3136 return 0;
3137 /*
3138 * If the caller does appear to be planning to write at or
3139 * beyond the end of the current extent, we then test to see
3140 * if the current extent is the last extent in the file, by
3141 * checking to make sure it was reached via the rightmost node
3142 * at each level of the tree.
3143 */
3144 for (i = depth-1; i >= 0; i--)
3145 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3146 return 0;
3147 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3148 return ext4_mark_inode_dirty(handle, inode);
3149}
3150
Mingming Cao00314622009-09-28 15:49:08 -04003151static int
3152ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003153 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003154 struct ext4_ext_path *path, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003155 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003156{
3157 int ret = 0;
3158 int err = 0;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003159 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Mingming Cao00314622009-09-28 15:49:08 -04003160
3161 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3162 "block %llu, max_blocks %u, flags %d, allocated %u",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003163 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04003164 flags, allocated);
3165 ext4_ext_show_leaf(inode, path);
3166
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003167 /* get_block() before submit the IO, split the extent */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003168 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003169 ret = ext4_split_unwritten_extents(handle, inode, map,
3170 path, flags);
Mingming5f524952009-11-10 10:48:04 -05003171 /*
3172 * Flag the inode(non aio case) or end_io struct (aio case)
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003173 * that this IO needs to conversion to written when IO is
Mingming5f524952009-11-10 10:48:04 -05003174 * completed
3175 */
Eric Sandeene9e3bce2011-02-12 08:17:34 -05003176 if (io && !(io->flag & EXT4_IO_END_UNWRITTEN)) {
Theodore Ts'obd2d0212010-10-27 21:30:10 -04003177 io->flag = EXT4_IO_END_UNWRITTEN;
Eric Sandeene9e3bce2011-02-12 08:17:34 -05003178 atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten);
3179 } else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003180 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
Jiaying Zhang744692d2010-03-04 16:14:02 -05003181 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003182 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao00314622009-09-28 15:49:08 -04003183 goto out;
3184 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003185 /* IO end_io complete, convert the filled extent to written */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003186 if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003187 ret = ext4_convert_unwritten_extents_endio(handle, inode,
Mingming Cao00314622009-09-28 15:49:08 -04003188 path);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003189 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05003190 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05003191 err = check_eofblocks_fl(handle, inode, map->m_lblk,
3192 path, map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003193 } else
3194 err = ret;
Mingming Cao00314622009-09-28 15:49:08 -04003195 goto out2;
3196 }
3197 /* buffered IO case */
3198 /*
3199 * repeat fallocate creation request
3200 * we already have an unwritten extent
3201 */
3202 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3203 goto map_out;
3204
3205 /* buffered READ or buffered write_begin() lookup */
3206 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3207 /*
3208 * We have blocks reserved already. We
3209 * return allocated blocks so that delalloc
3210 * won't do block reservation for us. But
3211 * the buffer head will be unmapped so that
3212 * a read from the block returns 0s.
3213 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003214 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003215 goto out1;
3216 }
3217
3218 /* buffered write, writepage time, convert*/
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003219 ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003220 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05003221 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05003222 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
3223 map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003224 if (err < 0)
3225 goto out2;
3226 }
3227
Mingming Cao00314622009-09-28 15:49:08 -04003228out:
3229 if (ret <= 0) {
3230 err = ret;
3231 goto out2;
3232 } else
3233 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003234 map->m_flags |= EXT4_MAP_NEW;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003235 /*
3236 * if we allocated more blocks than requested
3237 * we need to make sure we unmap the extra block
3238 * allocated. The actual needed block will get
3239 * unmapped later when we find the buffer_head marked
3240 * new.
3241 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003242 if (allocated > map->m_len) {
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003243 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003244 newblock + map->m_len,
3245 allocated - map->m_len);
3246 allocated = map->m_len;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003247 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003248
3249 /*
3250 * If we have done fallocate with the offset that is already
3251 * delayed allocated, we would have block reservation
3252 * and quota reservation done in the delayed write path.
3253 * But fallocate would have already updated quota and block
3254 * count for this offset. So cancel these reservation
3255 */
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05003256 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003257 ext4_da_update_reserve_space(inode, allocated, 0);
3258
Mingming Cao00314622009-09-28 15:49:08 -04003259map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003260 map->m_flags |= EXT4_MAP_MAPPED;
Mingming Cao00314622009-09-28 15:49:08 -04003261out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003262 if (allocated > map->m_len)
3263 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003264 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003265 map->m_pblk = newblock;
3266 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04003267out2:
3268 if (path) {
3269 ext4_ext_drop_refs(path);
3270 kfree(path);
3271 }
3272 return err ? err : allocated;
3273}
Theodore Ts'o58590b02010-10-27 21:23:12 -04003274
Mingming Cao00314622009-09-28 15:49:08 -04003275/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05003276 * Block allocation/map/preallocation routine for extents based files
3277 *
3278 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003279 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003280 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3281 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05003282 *
3283 * return > 0, number of of blocks already mapped/allocated
3284 * if create == 0 and these are pre-allocated blocks
3285 * buffer head is unmapped
3286 * otherwise blocks are mapped
3287 *
3288 * return = 0, if plain look up failed (blocks have not been allocated)
3289 * buffer head is unmapped
3290 *
3291 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003292 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003293int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3294 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07003295{
3296 struct ext4_ext_path *path = NULL;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003297 struct ext4_extent newex, *ex;
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003298 ext4_fsblk_t newblock = 0;
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003299 int err = 0, depth, ret;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003300 unsigned int allocated = 0;
Allison Hendersone8613042011-05-25 07:41:46 -04003301 unsigned int punched_out = 0;
3302 unsigned int result = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003303 struct ext4_allocation_request ar;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003304 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Allison Hendersone8613042011-05-25 07:41:46 -04003305 struct ext4_map_blocks punch_map;
Alex Tomasa86c6182006-10-11 01:21:03 -07003306
Mingming84fe3be2009-09-01 08:44:37 -04003307 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003308 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003309 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07003310
3311 /* check in cache */
Allison Hendersone8613042011-05-25 07:41:46 -04003312 if (ext4_ext_in_cache(inode, map->m_lblk, &newex) &&
3313 ((flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) == 0)) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003314 if (!newex.ee_start_lo && !newex.ee_start_hi) {
Theodore Ts'oc2177052009-05-14 00:58:52 -04003315 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003316 /*
3317 * block isn't allocated yet and
3318 * user doesn't want to allocate it
3319 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003320 goto out2;
3321 }
3322 /* we should allocate requested block */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003323 } else {
Alex Tomasa86c6182006-10-11 01:21:03 -07003324 /* block is already allocated */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003325 newblock = map->m_lblk
Dave Kleikamp8c55e202007-05-24 13:04:54 -04003326 - le32_to_cpu(newex.ee_block)
Theodore Ts'obf89d162010-10-27 21:30:14 -04003327 + ext4_ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003328 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003329 allocated = ext4_ext_get_actual_len(&newex) -
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003330 (map->m_lblk - le32_to_cpu(newex.ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -07003331 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07003332 }
3333 }
3334
3335 /* find extent for this block */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003336 path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07003337 if (IS_ERR(path)) {
3338 err = PTR_ERR(path);
3339 path = NULL;
3340 goto out2;
3341 }
3342
3343 depth = ext_depth(inode);
3344
3345 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003346 * consistent leaf must not be empty;
3347 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07003348 * this is why assert can't be put in ext4_ext_find_extent()
3349 */
Frank Mayhar273df552010-03-02 11:46:09 -05003350 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
3351 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04003352 "lblock: %lu, depth: %d pblock %lld",
3353 (unsigned long) map->m_lblk, depth,
3354 path[depth].p_block);
Surbhi Palande034fb4c2009-12-14 09:53:52 -05003355 err = -EIO;
3356 goto out2;
3357 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003358
Avantika Mathur7e028972006-12-06 20:41:33 -08003359 ex = path[depth].p_ext;
3360 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003361 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003362 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003363 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003364
3365 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003366 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04003367 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003368 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003369 ee_len = ext4_ext_get_actual_len(ex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003370 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003371 if (in_range(map->m_lblk, ee_block, ee_len)) {
3372 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003373 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003374 allocated = ee_len - (map->m_lblk - ee_block);
3375 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
3376 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04003377
Allison Hendersone8613042011-05-25 07:41:46 -04003378 if ((flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) == 0) {
3379 /*
3380 * Do not put uninitialized extent
3381 * in the cache
3382 */
3383 if (!ext4_ext_is_uninitialized(ex)) {
3384 ext4_ext_put_in_cache(inode, ee_block,
3385 ee_len, ee_start);
3386 goto out;
3387 }
3388 ret = ext4_ext_handle_uninitialized_extents(
3389 handle, inode, map, path, flags,
3390 allocated, newblock);
3391 return ret;
Amit Arora56055d32007-07-17 21:42:38 -04003392 }
Allison Hendersone8613042011-05-25 07:41:46 -04003393
3394 /*
3395 * Punch out the map length, but only to the
3396 * end of the extent
3397 */
3398 punched_out = allocated < map->m_len ?
3399 allocated : map->m_len;
3400
3401 /*
3402 * Sense extents need to be converted to
3403 * uninitialized, they must fit in an
3404 * uninitialized extent
3405 */
3406 if (punched_out > EXT_UNINIT_MAX_LEN)
3407 punched_out = EXT_UNINIT_MAX_LEN;
3408
3409 punch_map.m_lblk = map->m_lblk;
3410 punch_map.m_pblk = newblock;
3411 punch_map.m_len = punched_out;
3412 punch_map.m_flags = 0;
3413
3414 /* Check to see if the extent needs to be split */
3415 if (punch_map.m_len != ee_len ||
3416 punch_map.m_lblk != ee_block) {
3417
3418 ret = ext4_split_extent(handle, inode,
3419 path, &punch_map, 0,
3420 EXT4_GET_BLOCKS_PUNCH_OUT_EXT |
3421 EXT4_GET_BLOCKS_PRE_IO);
3422
3423 if (ret < 0) {
3424 err = ret;
3425 goto out2;
3426 }
3427 /*
3428 * find extent for the block at
3429 * the start of the hole
3430 */
3431 ext4_ext_drop_refs(path);
3432 kfree(path);
3433
3434 path = ext4_ext_find_extent(inode,
3435 map->m_lblk, NULL);
3436 if (IS_ERR(path)) {
3437 err = PTR_ERR(path);
3438 path = NULL;
3439 goto out2;
3440 }
3441
3442 depth = ext_depth(inode);
3443 ex = path[depth].p_ext;
3444 ee_len = ext4_ext_get_actual_len(ex);
3445 ee_block = le32_to_cpu(ex->ee_block);
3446 ee_start = ext4_ext_pblock(ex);
3447
3448 }
3449
3450 ext4_ext_mark_uninitialized(ex);
3451
3452 err = ext4_ext_remove_space(inode, map->m_lblk,
3453 map->m_lblk + punched_out);
3454
3455 goto out2;
Alex Tomasa86c6182006-10-11 01:21:03 -07003456 }
3457 }
3458
3459 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003460 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07003461 * we couldn't try to create block if create flag is zero
3462 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04003463 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003464 /*
3465 * put just found gap into cache to speed up
3466 * subsequent requests
3467 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003468 ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
Alex Tomasa86c6182006-10-11 01:21:03 -07003469 goto out2;
3470 }
3471 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003472 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07003473 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003474
Alex Tomasc9de5602008-01-29 00:19:52 -05003475 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003476 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003477 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3478 if (err)
3479 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003480 ar.lright = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003481 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
3482 if (err)
3483 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04003484
Amit Arora749269f2007-07-18 09:02:56 -04003485 /*
3486 * See if request is beyond maximum number of blocks we can have in
3487 * a single extent. For an initialized extent this limit is
3488 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3489 * EXT_UNINIT_MAX_LEN.
3490 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003491 if (map->m_len > EXT_INIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003492 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003493 map->m_len = EXT_INIT_MAX_LEN;
3494 else if (map->m_len > EXT_UNINIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003495 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003496 map->m_len = EXT_UNINIT_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04003497
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003498 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
3499 newex.ee_block = cpu_to_le32(map->m_lblk);
3500 newex.ee_len = cpu_to_le16(map->m_len);
Amit Arora25d14f92007-05-24 13:04:13 -04003501 err = ext4_ext_check_overlap(inode, &newex, path);
3502 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003503 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04003504 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003505 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003506
3507 /* allocate new block */
3508 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003509 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
3510 ar.logical = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003511 ar.len = allocated;
3512 if (S_ISREG(inode->i_mode))
3513 ar.flags = EXT4_MB_HINT_DATA;
3514 else
3515 /* disable in-core preallocation for non-regular files */
3516 ar.flags = 0;
3517 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07003518 if (!newblock)
3519 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04003520 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003521 ar.goal, newblock, allocated);
Alex Tomasa86c6182006-10-11 01:21:03 -07003522
3523 /* try to insert new extent into found leaf and return */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07003524 ext4_ext_store_pblock(&newex, newblock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003525 newex.ee_len = cpu_to_le16(ar.len);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003526 /* Mark uninitialized */
3527 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
Amit Aroraa2df2a62007-07-17 21:42:41 -04003528 ext4_ext_mark_uninitialized(&newex);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003529 /*
Jiaying Zhang744692d2010-03-04 16:14:02 -05003530 * io_end structure was created for every IO write to an
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003531 * uninitialized extent. To avoid unnecessary conversion,
Jiaying Zhang744692d2010-03-04 16:14:02 -05003532 * here we flag the IO that really needs the conversion.
Mingming5f524952009-11-10 10:48:04 -05003533 * For non asycn direct IO case, flag the inode state
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003534 * that we need to perform conversion when IO is done.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003535 */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003536 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Eric Sandeene9e3bce2011-02-12 08:17:34 -05003537 if (io && !(io->flag & EXT4_IO_END_UNWRITTEN)) {
Theodore Ts'obd2d0212010-10-27 21:30:10 -04003538 io->flag = EXT4_IO_END_UNWRITTEN;
Eric Sandeene9e3bce2011-02-12 08:17:34 -05003539 atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten);
3540 } else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003541 ext4_set_inode_state(inode,
3542 EXT4_STATE_DIO_UNWRITTEN);
Mingming5f524952009-11-10 10:48:04 -05003543 }
Jiaying Zhang744692d2010-03-04 16:14:02 -05003544 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003545 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003546 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003547
Eric Sandeend002ebf2011-01-10 13:03:35 -05003548 err = check_eofblocks_fl(handle, inode, map->m_lblk, path, ar.len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003549 if (err)
3550 goto out2;
3551
Mingming Cao00314622009-09-28 15:49:08 -04003552 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
Alex Tomas315054f2007-05-24 13:04:25 -04003553 if (err) {
3554 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05003555 /* not a good idea to call discard here directly,
3556 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003557 ext4_discard_preallocations(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05003558 ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex),
Theodore Ts'oe6362602009-11-23 07:17:05 -05003559 ext4_ext_get_actual_len(&newex), 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07003560 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04003561 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003562
Alex Tomasa86c6182006-10-11 01:21:03 -07003563 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04003564 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003565 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003566 if (allocated > map->m_len)
3567 allocated = map->m_len;
3568 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07003569
Jan Karab436b9b2009-12-08 23:51:10 -05003570 /*
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003571 * Update reserved blocks/metadata blocks after successful
3572 * block allocation which had been deferred till now.
3573 */
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05003574 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003575 ext4_da_update_reserve_space(inode, allocated, 1);
3576
3577 /*
Jan Karab436b9b2009-12-08 23:51:10 -05003578 * Cache the extent and update transaction to commit on fdatasync only
3579 * when it is _not_ an uninitialized extent.
3580 */
3581 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003582 ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock);
Jan Karab436b9b2009-12-08 23:51:10 -05003583 ext4_update_inode_fsync_trans(handle, inode, 1);
3584 } else
3585 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07003586out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003587 if (allocated > map->m_len)
3588 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07003589 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003590 map->m_flags |= EXT4_MAP_MAPPED;
3591 map->m_pblk = newblock;
3592 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07003593out2:
3594 if (path) {
3595 ext4_ext_drop_refs(path);
3596 kfree(path);
3597 }
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003598 trace_ext4_ext_map_blocks_exit(inode, map->m_lblk,
3599 newblock, map->m_len, err ? err : allocated);
Allison Hendersone8613042011-05-25 07:41:46 -04003600
3601 result = (flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) ?
3602 punched_out : allocated;
3603
3604 return err ? err : result;
Alex Tomasa86c6182006-10-11 01:21:03 -07003605}
3606
Jan Karacf108bc2008-07-11 19:27:31 -04003607void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07003608{
3609 struct address_space *mapping = inode->i_mapping;
3610 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003611 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07003612 handle_t *handle;
3613 int err = 0;
3614
3615 /*
Jiaying Zhang3889fd52011-01-10 12:47:05 -05003616 * finish any pending end_io work so we won't run the risk of
3617 * converting any truncated blocks to initialized later
3618 */
3619 ext4_flush_completed_IO(inode);
3620
3621 /*
Alex Tomasa86c6182006-10-11 01:21:03 -07003622 * probably first extent we're gonna free will be last in block
3623 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04003624 err = ext4_writepage_trans_blocks(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07003625 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04003626 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07003627 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07003628
Jan Karacf108bc2008-07-11 19:27:31 -04003629 if (inode->i_size & (sb->s_blocksize - 1))
3630 ext4_block_truncate_page(handle, mapping, inode->i_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07003631
Jan Kara9ddfc3d2008-07-11 19:27:31 -04003632 if (ext4_orphan_add(handle, inode))
3633 goto out_stop;
3634
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003635 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07003636 ext4_ext_invalidate_cache(inode);
3637
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003638 ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003639
Alex Tomasa86c6182006-10-11 01:21:03 -07003640 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003641 * TODO: optimization is possible here.
3642 * Probably we need not scan at all,
3643 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07003644 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003645
3646 /* we have to know where to truncate from in crash case */
3647 EXT4_I(inode)->i_disksize = inode->i_size;
3648 ext4_mark_inode_dirty(handle, inode);
3649
3650 last_block = (inode->i_size + sb->s_blocksize - 1)
3651 >> EXT4_BLOCK_SIZE_BITS(sb);
Allison Hendersond583fb82011-05-25 07:41:43 -04003652 err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCK);
Alex Tomasa86c6182006-10-11 01:21:03 -07003653
3654 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04003655 * transaction synchronous.
3656 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003657 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05003658 ext4_handle_sync(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07003659
Jan Kara9ddfc3d2008-07-11 19:27:31 -04003660 up_write(&EXT4_I(inode)->i_data_sem);
Eric Gouriouf6d2f6b2011-05-22 21:33:00 -04003661
3662out_stop:
Alex Tomasa86c6182006-10-11 01:21:03 -07003663 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003664 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07003665 * then we need to clear up the orphan record which we created above.
3666 * However, if this was a real unlink then we were called by
3667 * ext4_delete_inode(), and we allow that function to clean up the
3668 * orphan info for us.
3669 */
3670 if (inode->i_nlink)
3671 ext4_orphan_del(handle, inode);
3672
Solofo Ramangalahyef737722008-04-29 22:00:41 -04003673 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3674 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07003675 ext4_journal_stop(handle);
3676}
3677
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003678static void ext4_falloc_update_inode(struct inode *inode,
3679 int mode, loff_t new_size, int update_ctime)
3680{
3681 struct timespec now;
3682
3683 if (update_ctime) {
3684 now = current_fs_time(inode->i_sb);
3685 if (!timespec_equal(&inode->i_ctime, &now))
3686 inode->i_ctime = now;
3687 }
3688 /*
3689 * Update only when preallocation was requested beyond
3690 * the file size.
3691 */
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003692 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3693 if (new_size > i_size_read(inode))
3694 i_size_write(inode, new_size);
3695 if (new_size > EXT4_I(inode)->i_disksize)
3696 ext4_update_i_disksize(inode, new_size);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003697 } else {
3698 /*
3699 * Mark that we allocate beyond EOF so the subsequent truncate
3700 * can proceed even if the new size is the same as i_size.
3701 */
3702 if (new_size > i_size_read(inode))
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003703 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003704 }
3705
3706}
3707
Amit Aroraa2df2a62007-07-17 21:42:41 -04003708/*
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003709 * preallocate space for a file. This implements ext4's fallocate file
Amit Aroraa2df2a62007-07-17 21:42:41 -04003710 * operation, which gets called from sys_fallocate system call.
3711 * For block-mapped files, posix_fallocate should fall back to the method
3712 * of writing zeroes to the required new blocks (the same behavior which is
3713 * expected for file systems which do not support fallocate() system call).
3714 */
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003715long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
Amit Aroraa2df2a62007-07-17 21:42:41 -04003716{
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01003717 struct inode *inode = file->f_path.dentry->d_inode;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003718 handle_t *handle;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003719 loff_t new_size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003720 unsigned int max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003721 int ret = 0;
3722 int ret2 = 0;
3723 int retries = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003724 struct ext4_map_blocks map;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003725 unsigned int credits, blkbits = inode->i_blkbits;
3726
Josef Bacikd6dc8462010-11-17 20:46:18 -05003727 /* We only support the FALLOC_FL_KEEP_SIZE mode */
Christoph Hellwig64c23e82011-01-14 13:07:30 +01003728 if (mode & ~FALLOC_FL_KEEP_SIZE)
Josef Bacikd6dc8462010-11-17 20:46:18 -05003729 return -EOPNOTSUPP;
3730
Amit Aroraa2df2a62007-07-17 21:42:41 -04003731 /*
3732 * currently supporting (pre)allocate mode for extent-based
3733 * files _only_
3734 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003735 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Amit Aroraa2df2a62007-07-17 21:42:41 -04003736 return -EOPNOTSUPP;
3737
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003738 trace_ext4_fallocate_enter(inode, offset, len, mode);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003739 map.m_lblk = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003740 /*
3741 * We can't just convert len to max_blocks because
3742 * If blocksize = 4096 offset = 3072 and len = 2048
3743 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003744 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003745 - map.m_lblk;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003746 /*
Mingming Caof3bd1f32008-08-19 22:16:03 -04003747 * credits to insert 1 extent into extent tree
Amit Aroraa2df2a62007-07-17 21:42:41 -04003748 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04003749 credits = ext4_chunk_trans_blocks(inode, max_blocks);
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05003750 mutex_lock(&inode->i_mutex);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04003751 ret = inode_newsize_ok(inode, (len + offset));
3752 if (ret) {
3753 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003754 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04003755 return ret;
3756 }
Amit Aroraa2df2a62007-07-17 21:42:41 -04003757retry:
3758 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003759 map.m_lblk = map.m_lblk + ret;
3760 map.m_len = max_blocks = max_blocks - ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003761 handle = ext4_journal_start(inode, credits);
3762 if (IS_ERR(handle)) {
3763 ret = PTR_ERR(handle);
3764 break;
3765 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003766 ret = ext4_map_blocks(handle, inode, &map,
Theodore Ts'oc2177052009-05-14 00:58:52 -04003767 EXT4_GET_BLOCKS_CREATE_UNINIT_EXT);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05003768 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003769#ifdef EXT4FS_DEBUG
3770 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003771 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003772 "returned error inode#%lu, block=%u, "
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05003773 "max_blocks=%u", __func__,
Kazuya Mioa6371b62010-10-27 21:30:15 -04003774 inode->i_ino, map.m_lblk, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003775#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04003776 ext4_mark_inode_dirty(handle, inode);
3777 ret2 = ext4_journal_stop(handle);
3778 break;
3779 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003780 if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003781 blkbits) >> blkbits))
3782 new_size = offset + len;
3783 else
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003784 new_size = (map.m_lblk + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003785
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003786 ext4_falloc_update_inode(inode, mode, new_size,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003787 (map.m_flags & EXT4_MAP_NEW));
Amit Aroraa2df2a62007-07-17 21:42:41 -04003788 ext4_mark_inode_dirty(handle, inode);
3789 ret2 = ext4_journal_stop(handle);
3790 if (ret2)
3791 break;
3792 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003793 if (ret == -ENOSPC &&
3794 ext4_should_retry_alloc(inode->i_sb, &retries)) {
3795 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003796 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003797 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05003798 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003799 trace_ext4_fallocate_exit(inode, offset, max_blocks,
3800 ret > 0 ? ret2 : ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003801 return ret > 0 ? ret2 : ret;
3802}
Eric Sandeen6873fa02008-10-07 00:46:36 -04003803
3804/*
Mingming Cao00314622009-09-28 15:49:08 -04003805 * This function convert a range of blocks to written extents
3806 * The caller of this function will pass the start offset and the size.
3807 * all unwritten extents within this range will be converted to
3808 * written extents.
3809 *
3810 * This function is called from the direct IO end io call back
3811 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05003812 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04003813 */
3814int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
Eric Sandeena1de02d2010-02-04 23:58:38 -05003815 ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04003816{
3817 handle_t *handle;
Mingming Cao00314622009-09-28 15:49:08 -04003818 unsigned int max_blocks;
3819 int ret = 0;
3820 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003821 struct ext4_map_blocks map;
Mingming Cao00314622009-09-28 15:49:08 -04003822 unsigned int credits, blkbits = inode->i_blkbits;
3823
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003824 map.m_lblk = offset >> blkbits;
Mingming Cao00314622009-09-28 15:49:08 -04003825 /*
3826 * We can't just convert len to max_blocks because
3827 * If blocksize = 4096 offset = 3072 and len = 2048
3828 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003829 max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
3830 map.m_lblk);
Mingming Cao00314622009-09-28 15:49:08 -04003831 /*
3832 * credits to insert 1 extent into extent tree
3833 */
3834 credits = ext4_chunk_trans_blocks(inode, max_blocks);
3835 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003836 map.m_lblk += ret;
3837 map.m_len = (max_blocks -= ret);
Mingming Cao00314622009-09-28 15:49:08 -04003838 handle = ext4_journal_start(inode, credits);
3839 if (IS_ERR(handle)) {
3840 ret = PTR_ERR(handle);
3841 break;
3842 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003843 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003844 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Mingming Cao00314622009-09-28 15:49:08 -04003845 if (ret <= 0) {
3846 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003847 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Mingming Cao00314622009-09-28 15:49:08 -04003848 "returned error inode#%lu, block=%u, "
3849 "max_blocks=%u", __func__,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003850 inode->i_ino, map.m_lblk, map.m_len);
Mingming Cao00314622009-09-28 15:49:08 -04003851 }
3852 ext4_mark_inode_dirty(handle, inode);
3853 ret2 = ext4_journal_stop(handle);
3854 if (ret <= 0 || ret2 )
3855 break;
3856 }
3857 return ret > 0 ? ret2 : ret;
3858}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003859
Mingming Cao00314622009-09-28 15:49:08 -04003860/*
Eric Sandeen6873fa02008-10-07 00:46:36 -04003861 * Callback function called for each extent to gather FIEMAP information.
3862 */
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003863static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path,
Eric Sandeen6873fa02008-10-07 00:46:36 -04003864 struct ext4_ext_cache *newex, struct ext4_extent *ex,
3865 void *data)
3866{
Eric Sandeen6873fa02008-10-07 00:46:36 -04003867 __u64 logical;
3868 __u64 physical;
3869 __u64 length;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003870 loff_t size;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003871 __u32 flags = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003872 int ret = 0;
3873 struct fiemap_extent_info *fieinfo = data;
3874 unsigned char blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003875
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003876 blksize_bits = inode->i_sb->s_blocksize_bits;
3877 logical = (__u64)newex->ec_block << blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003878
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003879 if (newex->ec_start == 0) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003880 /*
3881 * No extent in extent-tree contains block @newex->ec_start,
3882 * then the block may stay in 1)a hole or 2)delayed-extent.
3883 *
3884 * Holes or delayed-extents are processed as follows.
3885 * 1. lookup dirty pages with specified range in pagecache.
3886 * If no page is got, then there is no delayed-extent and
3887 * return with EXT_CONTINUE.
3888 * 2. find the 1st mapped buffer,
3889 * 3. check if the mapped buffer is both in the request range
3890 * and a delayed buffer. If not, there is no delayed-extent,
3891 * then return.
3892 * 4. a delayed-extent is found, the extent will be collected.
3893 */
3894 ext4_lblk_t end = 0;
3895 pgoff_t last_offset;
3896 pgoff_t offset;
3897 pgoff_t index;
Yongqiang Yangb2213492011-05-24 11:36:58 -04003898 pgoff_t start_index = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003899 struct page **pages = NULL;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003900 struct buffer_head *bh = NULL;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003901 struct buffer_head *head = NULL;
3902 unsigned int nr_pages = PAGE_SIZE / sizeof(struct page *);
3903
3904 pages = kmalloc(PAGE_SIZE, GFP_KERNEL);
3905 if (pages == NULL)
3906 return -ENOMEM;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003907
3908 offset = logical >> PAGE_SHIFT;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003909repeat:
3910 last_offset = offset;
3911 head = NULL;
3912 ret = find_get_pages_tag(inode->i_mapping, &offset,
3913 PAGECACHE_TAG_DIRTY, nr_pages, pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04003914
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003915 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
3916 /* First time, try to find a mapped buffer. */
3917 if (ret == 0) {
3918out:
3919 for (index = 0; index < ret; index++)
3920 page_cache_release(pages[index]);
3921 /* just a hole. */
3922 kfree(pages);
3923 return EXT_CONTINUE;
3924 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04003925 index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003926
Yongqiang Yangb2213492011-05-24 11:36:58 -04003927next_page:
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003928 /* Try to find the 1st mapped buffer. */
Yongqiang Yangb2213492011-05-24 11:36:58 -04003929 end = ((__u64)pages[index]->index << PAGE_SHIFT) >>
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003930 blksize_bits;
Yongqiang Yangb2213492011-05-24 11:36:58 -04003931 if (!page_has_buffers(pages[index]))
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003932 goto out;
Yongqiang Yangb2213492011-05-24 11:36:58 -04003933 head = page_buffers(pages[index]);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003934 if (!head)
3935 goto out;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003936
Yongqiang Yangb2213492011-05-24 11:36:58 -04003937 index++;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003938 bh = head;
3939 do {
Yongqiang Yangb2213492011-05-24 11:36:58 -04003940 if (end >= newex->ec_block +
3941 newex->ec_len)
3942 /* The buffer is out of
3943 * the request range.
3944 */
3945 goto out;
3946
3947 if (buffer_mapped(bh) &&
3948 end >= newex->ec_block) {
3949 start_index = index - 1;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003950 /* get the 1st mapped buffer. */
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003951 goto found_mapped_buffer;
3952 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04003953
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003954 bh = bh->b_this_page;
3955 end++;
3956 } while (bh != head);
3957
Yongqiang Yangb2213492011-05-24 11:36:58 -04003958 /* No mapped buffer in the range found in this page,
3959 * We need to look up next page.
3960 */
3961 if (index >= ret) {
3962 /* There is no page left, but we need to limit
3963 * newex->ec_len.
3964 */
3965 newex->ec_len = end - newex->ec_block;
3966 goto out;
3967 }
3968 goto next_page;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003969 } else {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003970 /*Find contiguous delayed buffers. */
3971 if (ret > 0 && pages[0]->index == last_offset)
3972 head = page_buffers(pages[0]);
3973 bh = head;
Yongqiang Yangb2213492011-05-24 11:36:58 -04003974 index = 1;
3975 start_index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003976 }
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003977
3978found_mapped_buffer:
3979 if (bh != NULL && buffer_delay(bh)) {
3980 /* 1st or contiguous delayed buffer found. */
3981 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
3982 /*
3983 * 1st delayed buffer found, record
3984 * the start of extent.
3985 */
3986 flags |= FIEMAP_EXTENT_DELALLOC;
3987 newex->ec_block = end;
3988 logical = (__u64)end << blksize_bits;
3989 }
3990 /* Find contiguous delayed buffers. */
3991 do {
3992 if (!buffer_delay(bh))
3993 goto found_delayed_extent;
3994 bh = bh->b_this_page;
3995 end++;
3996 } while (bh != head);
3997
Yongqiang Yangb2213492011-05-24 11:36:58 -04003998 for (; index < ret; index++) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05003999 if (!page_has_buffers(pages[index])) {
4000 bh = NULL;
4001 break;
4002 }
4003 head = page_buffers(pages[index]);
4004 if (!head) {
4005 bh = NULL;
4006 break;
4007 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004008
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004009 if (pages[index]->index !=
Yongqiang Yangb2213492011-05-24 11:36:58 -04004010 pages[start_index]->index + index
4011 - start_index) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004012 /* Blocks are not contiguous. */
4013 bh = NULL;
4014 break;
4015 }
4016 bh = head;
4017 do {
4018 if (!buffer_delay(bh))
4019 /* Delayed-extent ends. */
4020 goto found_delayed_extent;
4021 bh = bh->b_this_page;
4022 end++;
4023 } while (bh != head);
4024 }
4025 } else if (!(flags & FIEMAP_EXTENT_DELALLOC))
4026 /* a hole found. */
4027 goto out;
4028
4029found_delayed_extent:
4030 newex->ec_len = min(end - newex->ec_block,
4031 (ext4_lblk_t)EXT_INIT_MAX_LEN);
4032 if (ret == nr_pages && bh != NULL &&
4033 newex->ec_len < EXT_INIT_MAX_LEN &&
4034 buffer_delay(bh)) {
4035 /* Have not collected an extent and continue. */
4036 for (index = 0; index < ret; index++)
4037 page_cache_release(pages[index]);
4038 goto repeat;
4039 }
4040
4041 for (index = 0; index < ret; index++)
4042 page_cache_release(pages[index]);
4043 kfree(pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004044 }
4045
4046 physical = (__u64)newex->ec_start << blksize_bits;
4047 length = (__u64)newex->ec_len << blksize_bits;
4048
4049 if (ex && ext4_ext_is_uninitialized(ex))
4050 flags |= FIEMAP_EXTENT_UNWRITTEN;
4051
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004052 size = i_size_read(inode);
4053 if (logical + length >= size)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004054 flags |= FIEMAP_EXTENT_LAST;
4055
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004056 ret = fiemap_fill_next_extent(fieinfo, logical, physical,
Eric Sandeen6873fa02008-10-07 00:46:36 -04004057 length, flags);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004058 if (ret < 0)
4059 return ret;
4060 if (ret == 1)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004061 return EXT_BREAK;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004062 return EXT_CONTINUE;
4063}
4064
4065/* fiemap flags we can handle specified here */
4066#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
4067
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004068static int ext4_xattr_fiemap(struct inode *inode,
4069 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004070{
4071 __u64 physical = 0;
4072 __u64 length;
4073 __u32 flags = FIEMAP_EXTENT_LAST;
4074 int blockbits = inode->i_sb->s_blocksize_bits;
4075 int error = 0;
4076
4077 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004078 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04004079 struct ext4_iloc iloc;
4080 int offset; /* offset of xattr in inode */
4081
4082 error = ext4_get_inode_loc(inode, &iloc);
4083 if (error)
4084 return error;
4085 physical = iloc.bh->b_blocknr << blockbits;
4086 offset = EXT4_GOOD_OLD_INODE_SIZE +
4087 EXT4_I(inode)->i_extra_isize;
4088 physical += offset;
4089 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
4090 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004091 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004092 } else { /* external block */
4093 physical = EXT4_I(inode)->i_file_acl << blockbits;
4094 length = inode->i_sb->s_blocksize;
4095 }
4096
4097 if (physical)
4098 error = fiemap_fill_next_extent(fieinfo, 0, physical,
4099 length, flags);
4100 return (error < 0 ? error : 0);
4101}
4102
4103int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4104 __u64 start, __u64 len)
4105{
4106 ext4_lblk_t start_blk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004107 int error = 0;
4108
4109 /* fallback to generic here if not in extents fmt */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004110 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeen6873fa02008-10-07 00:46:36 -04004111 return generic_block_fiemap(inode, fieinfo, start, len,
4112 ext4_get_block);
4113
4114 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
4115 return -EBADR;
4116
4117 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
4118 error = ext4_xattr_fiemap(inode, fieinfo);
4119 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004120 ext4_lblk_t len_blks;
4121 __u64 last_blk;
4122
Eric Sandeen6873fa02008-10-07 00:46:36 -04004123 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004124 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
4125 if (last_blk >= EXT_MAX_BLOCK)
4126 last_blk = EXT_MAX_BLOCK-1;
4127 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004128
4129 /*
4130 * Walk the extent tree gathering extent information.
4131 * ext4_ext_fiemap_cb will push extents back to user.
4132 */
Eric Sandeen6873fa02008-10-07 00:46:36 -04004133 error = ext4_ext_walk_space(inode, start_blk, len_blks,
4134 ext4_ext_fiemap_cb, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004135 }
4136
4137 return error;
4138}