blob: 37f9387893440e63ade0d69f397a982a9175f02c [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
47
Randy Dunlapd0d856e2006-10-11 01:21:07 -070048/*
49 * ext_pblock:
50 * combine low and high parts of physical block number into ext4_fsblk_t
51 */
Akira Fujita748de672009-06-17 19:24:03 -040052ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
Alex Tomasf65e6fb2006-10-11 01:21:05 -070053{
54 ext4_fsblk_t block;
55
Aneesh Kumar K.Vb3776112007-10-16 18:38:25 -040056 block = le32_to_cpu(ex->ee_start_lo);
Mingming Cao9b8f1f02006-10-11 01:21:13 -070057 block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -070058 return block;
59}
60
Randy Dunlapd0d856e2006-10-11 01:21:07 -070061/*
62 * idx_pblock:
63 * combine low and high parts of a leaf physical block number into ext4_fsblk_t
64 */
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -050065ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
Alex Tomasf65e6fb2006-10-11 01:21:05 -070066{
67 ext4_fsblk_t block;
68
Aneesh Kumar K.Vd8dd0b42007-10-16 18:38:25 -040069 block = le32_to_cpu(ix->ei_leaf_lo);
Mingming Cao9b8f1f02006-10-11 01:21:13 -070070 block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -070071 return block;
72}
73
Randy Dunlapd0d856e2006-10-11 01:21:07 -070074/*
75 * ext4_ext_store_pblock:
76 * stores a large physical block number into an extent struct,
77 * breaking it into parts
78 */
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -050079void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
Alex Tomasf65e6fb2006-10-11 01:21:05 -070080{
Aneesh Kumar K.Vb3776112007-10-16 18:38:25 -040081 ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
Mingming Cao9b8f1f02006-10-11 01:21:13 -070082 ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
Alex Tomasf65e6fb2006-10-11 01:21:05 -070083}
84
Randy Dunlapd0d856e2006-10-11 01:21:07 -070085/*
86 * ext4_idx_store_pblock:
87 * stores a large physical block number into an index struct,
88 * breaking it into parts
89 */
Avantika Mathur09b88252006-12-06 20:41:36 -080090static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb)
Alex Tomasf65e6fb2006-10-11 01:21:05 -070091{
Aneesh Kumar K.Vd8dd0b42007-10-16 18:38:25 -040092 ix->ei_leaf_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
Mingming Cao9b8f1f02006-10-11 01:21:13 -070093 ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
Alex Tomasf65e6fb2006-10-11 01:21:05 -070094}
95
Jan Kara487caee2009-08-17 22:17:20 -040096static int ext4_ext_truncate_extend_restart(handle_t *handle,
97 struct inode *inode,
98 int needed)
Alex Tomasa86c6182006-10-11 01:21:03 -070099{
100 int err;
101
Frank Mayhar03901312009-01-07 00:06:22 -0500102 if (!ext4_handle_valid(handle))
103 return 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700104 if (handle->h_buffer_credits > needed)
Shen Feng9102e4f2008-07-11 19:27:31 -0400105 return 0;
106 err = ext4_journal_extend(handle, needed);
Theodore Ts'o0123c932008-08-01 20:57:54 -0400107 if (err <= 0)
Shen Feng9102e4f2008-07-11 19:27:31 -0400108 return err;
Jan Kara487caee2009-08-17 22:17:20 -0400109 err = ext4_truncate_restart_trans(handle, inode, needed);
110 /*
111 * We have dropped i_data_sem so someone might have cached again
112 * an extent we are going to truncate.
113 */
114 ext4_ext_invalidate_cache(inode);
115
116 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -0700117}
118
119/*
120 * could return:
121 * - EROFS
122 * - ENOMEM
123 */
124static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
125 struct ext4_ext_path *path)
126{
127 if (path->p_bh) {
128 /* path points to block */
129 return ext4_journal_get_write_access(handle, path->p_bh);
130 }
131 /* path points to leaf/index in inode body */
132 /* we use in-core data, no need to protect them */
133 return 0;
134}
135
136/*
137 * could return:
138 * - EROFS
139 * - ENOMEM
140 * - EIO
141 */
142static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
143 struct ext4_ext_path *path)
144{
145 int err;
146 if (path->p_bh) {
147 /* path points to block */
Frank Mayhar03901312009-01-07 00:06:22 -0500148 err = ext4_handle_dirty_metadata(handle, inode, path->p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700149 } else {
150 /* path points to leaf/index in inode body */
151 err = ext4_mark_inode_dirty(handle, inode);
152 }
153 return err;
154}
155
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700156static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700157 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500158 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700159{
160 struct ext4_inode_info *ei = EXT4_I(inode);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700161 ext4_fsblk_t bg_start;
Valerie Clement74d34872008-02-15 13:43:07 -0500162 ext4_fsblk_t last_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700163 ext4_grpblk_t colour;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400164 ext4_group_t block_group;
165 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -0700166 int depth;
167
168 if (path) {
169 struct ext4_extent *ex;
170 depth = path->p_depth;
171
172 /* try to predict block placement */
Avantika Mathur7e028972006-12-06 20:41:33 -0800173 ex = path[depth].p_ext;
174 if (ex)
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700175 return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700176
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700177 /* it looks like index is empty;
178 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700179 if (path[depth].p_bh)
180 return path[depth].p_bh->b_blocknr;
181 }
182
183 /* OK. use inode's group */
Theodore Ts'oa4912122009-03-12 12:18:34 -0400184 block_group = ei->i_block_group;
185 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
186 /*
187 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
188 * block groups per flexgroup, reserve the first block
189 * group for directories and special files. Regular
190 * files will start at the second block group. This
191 * tends to speed up directory access and improves
192 * fsck times.
193 */
194 block_group &= ~(flex_size-1);
195 if (S_ISREG(inode->i_mode))
196 block_group++;
197 }
Akinobu Mita5661bd62010-03-03 23:53:39 -0500198 bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
Valerie Clement74d34872008-02-15 13:43:07 -0500199 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
200
Theodore Ts'oa4912122009-03-12 12:18:34 -0400201 /*
202 * If we are doing delayed allocation, we don't need take
203 * colour into account.
204 */
205 if (test_opt(inode->i_sb, DELALLOC))
206 return bg_start;
207
Valerie Clement74d34872008-02-15 13:43:07 -0500208 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
209 colour = (current->pid % 16) *
Alex Tomasa86c6182006-10-11 01:21:03 -0700210 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
Valerie Clement74d34872008-02-15 13:43:07 -0500211 else
212 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
Alex Tomasa86c6182006-10-11 01:21:03 -0700213 return bg_start + colour + block;
214}
215
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400216/*
217 * Allocation for a meta data block
218 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700219static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400220ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700221 struct ext4_ext_path *path,
222 struct ext4_extent *ex, int *err)
223{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700224 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700225
226 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Theodore Ts'o97df5d12008-12-12 12:41:28 -0500227 newblock = ext4_new_meta_blocks(handle, inode, goal, NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700228 return newblock;
229}
230
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400231static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700232{
233 int size;
234
235 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
236 / sizeof(struct ext4_extent);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400237 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100238#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400239 if (size > 6)
240 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700241#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400242 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700243 return size;
244}
245
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400246static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700247{
248 int size;
249
250 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
251 / sizeof(struct ext4_extent_idx);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400252 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100253#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400254 if (size > 5)
255 size = 5;
Alex Tomasa86c6182006-10-11 01:21:03 -0700256#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400257 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700258 return size;
259}
260
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400261static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700262{
263 int size;
264
265 size = sizeof(EXT4_I(inode)->i_data);
266 size -= sizeof(struct ext4_extent_header);
267 size /= sizeof(struct ext4_extent);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400268 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100269#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400270 if (size > 3)
271 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700272#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400273 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700274 return size;
275}
276
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400277static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700278{
279 int size;
280
281 size = sizeof(EXT4_I(inode)->i_data);
282 size -= sizeof(struct ext4_extent_header);
283 size /= sizeof(struct ext4_extent_idx);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400284 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100285#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400286 if (size > 4)
287 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700288#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400289 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700290 return size;
291}
292
Mingming Caod2a17632008-07-14 17:52:37 -0400293/*
294 * Calculate the number of metadata blocks needed
295 * to allocate @blocks
296 * Worse case is one block per extent
297 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500298int ext4_ext_calc_metadata_amount(struct inode *inode, sector_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -0400299{
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500300 struct ext4_inode_info *ei = EXT4_I(inode);
301 int idxs, num = 0;
Mingming Caod2a17632008-07-14 17:52:37 -0400302
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500303 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
304 / sizeof(struct ext4_extent_idx));
Mingming Caod2a17632008-07-14 17:52:37 -0400305
306 /*
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500307 * If the new delayed allocation block is contiguous with the
308 * previous da block, it can share index blocks with the
309 * previous block, so we only need to allocate a new index
310 * block every idxs leaf blocks. At ldxs**2 blocks, we need
311 * an additional index block, and at ldxs**3 blocks, yet
312 * another index blocks.
Mingming Caod2a17632008-07-14 17:52:37 -0400313 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500314 if (ei->i_da_metadata_calc_len &&
315 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
316 if ((ei->i_da_metadata_calc_len % idxs) == 0)
317 num++;
318 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
319 num++;
320 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
321 num++;
322 ei->i_da_metadata_calc_len = 0;
323 } else
324 ei->i_da_metadata_calc_len++;
325 ei->i_da_metadata_calc_last_lblock++;
326 return num;
327 }
Mingming Caod2a17632008-07-14 17:52:37 -0400328
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500329 /*
330 * In the worst case we need a new set of index blocks at
331 * every level of the inode's extent tree.
332 */
333 ei->i_da_metadata_calc_len = 1;
334 ei->i_da_metadata_calc_last_lblock = lblock;
335 return ext_depth(inode) + 1;
Mingming Caod2a17632008-07-14 17:52:37 -0400336}
337
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400338static int
339ext4_ext_max_entries(struct inode *inode, int depth)
340{
341 int max;
342
343 if (depth == ext_depth(inode)) {
344 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400345 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400346 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400347 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400348 } else {
349 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400350 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400351 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400352 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400353 }
354
355 return max;
356}
357
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400358static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
359{
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400360 ext4_fsblk_t block = ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400361 int len = ext4_ext_get_actual_len(ext);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400362
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400363 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400364}
365
366static int ext4_valid_extent_idx(struct inode *inode,
367 struct ext4_extent_idx *ext_idx)
368{
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400369 ext4_fsblk_t block = idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400370
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400371 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400372}
373
374static int ext4_valid_extent_entries(struct inode *inode,
375 struct ext4_extent_header *eh,
376 int depth)
377{
378 struct ext4_extent *ext;
379 struct ext4_extent_idx *ext_idx;
380 unsigned short entries;
381 if (eh->eh_entries == 0)
382 return 1;
383
384 entries = le16_to_cpu(eh->eh_entries);
385
386 if (depth == 0) {
387 /* leaf entries */
388 ext = EXT_FIRST_EXTENT(eh);
389 while (entries) {
390 if (!ext4_valid_extent(inode, ext))
391 return 0;
392 ext++;
393 entries--;
394 }
395 } else {
396 ext_idx = EXT_FIRST_INDEX(eh);
397 while (entries) {
398 if (!ext4_valid_extent_idx(inode, ext_idx))
399 return 0;
400 ext_idx++;
401 entries--;
402 }
403 }
404 return 1;
405}
406
407static int __ext4_ext_check(const char *function, struct inode *inode,
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400408 struct ext4_extent_header *eh,
409 int depth)
410{
411 const char *error_msg;
412 int max = 0;
413
414 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
415 error_msg = "invalid magic";
416 goto corrupted;
417 }
418 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
419 error_msg = "unexpected eh_depth";
420 goto corrupted;
421 }
422 if (unlikely(eh->eh_max == 0)) {
423 error_msg = "invalid eh_max";
424 goto corrupted;
425 }
426 max = ext4_ext_max_entries(inode, depth);
427 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
428 error_msg = "too large eh_max";
429 goto corrupted;
430 }
431 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
432 error_msg = "invalid eh_entries";
433 goto corrupted;
434 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400435 if (!ext4_valid_extent_entries(inode, eh, depth)) {
436 error_msg = "invalid extent entries";
437 goto corrupted;
438 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400439 return 0;
440
441corrupted:
Eric Sandeen12062dd2010-02-15 14:19:27 -0500442 __ext4_error(inode->i_sb, function,
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400443 "bad header/extent in inode #%lu: %s - magic %x, "
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400444 "entries %u, max %u(%u), depth %u(%u)",
445 inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
446 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
447 max, le16_to_cpu(eh->eh_depth), depth);
448
449 return -EIO;
450}
451
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400452#define ext4_ext_check(inode, eh, depth) \
453 __ext4_ext_check(__func__, inode, eh, depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400454
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400455int ext4_ext_check_inode(struct inode *inode)
456{
457 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
458}
459
Alex Tomasa86c6182006-10-11 01:21:03 -0700460#ifdef EXT_DEBUG
461static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
462{
463 int k, l = path->p_depth;
464
465 ext_debug("path:");
466 for (k = 0; k <= l; k++, path++) {
467 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700468 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700469 idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700470 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400471 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700472 le32_to_cpu(path->p_ext->ee_block),
Mingming553f9002009-09-18 13:34:55 -0400473 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400474 ext4_ext_get_actual_len(path->p_ext),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700475 ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700476 } else
477 ext_debug(" []");
478 }
479 ext_debug("\n");
480}
481
482static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
483{
484 int depth = ext_depth(inode);
485 struct ext4_extent_header *eh;
486 struct ext4_extent *ex;
487 int i;
488
489 if (!path)
490 return;
491
492 eh = path[depth].p_hdr;
493 ex = EXT_FIRST_EXTENT(eh);
494
Mingming553f9002009-09-18 13:34:55 -0400495 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
496
Alex Tomasa86c6182006-10-11 01:21:03 -0700497 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400498 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
499 ext4_ext_is_uninitialized(ex),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400500 ext4_ext_get_actual_len(ex), ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700501 }
502 ext_debug("\n");
503}
504#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400505#define ext4_ext_show_path(inode, path)
506#define ext4_ext_show_leaf(inode, path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700507#endif
508
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500509void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700510{
511 int depth = path->p_depth;
512 int i;
513
514 for (i = 0; i <= depth; i++, path++)
515 if (path->p_bh) {
516 brelse(path->p_bh);
517 path->p_bh = NULL;
518 }
519}
520
521/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700522 * ext4_ext_binsearch_idx:
523 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400524 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700525 */
526static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500527ext4_ext_binsearch_idx(struct inode *inode,
528 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700529{
530 struct ext4_extent_header *eh = path->p_hdr;
531 struct ext4_extent_idx *r, *l, *m;
532
Alex Tomasa86c6182006-10-11 01:21:03 -0700533
Eric Sandeenbba90742008-01-28 23:58:27 -0500534 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700535
536 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400537 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700538 while (l <= r) {
539 m = l + (r - l) / 2;
540 if (block < le32_to_cpu(m->ei_block))
541 r = m - 1;
542 else
543 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400544 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
545 m, le32_to_cpu(m->ei_block),
546 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700547 }
548
549 path->p_idx = l - 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700550 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400551 idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700552
553#ifdef CHECK_BINSEARCH
554 {
555 struct ext4_extent_idx *chix, *ix;
556 int k;
557
558 chix = ix = EXT_FIRST_INDEX(eh);
559 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
560 if (k != 0 &&
561 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o47760042008-09-08 23:00:52 -0400562 printk(KERN_DEBUG "k=%d, ix=0x%p, "
563 "first=0x%p\n", k,
564 ix, EXT_FIRST_INDEX(eh));
565 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700566 le32_to_cpu(ix->ei_block),
567 le32_to_cpu(ix[-1].ei_block));
568 }
569 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400570 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700571 if (block < le32_to_cpu(ix->ei_block))
572 break;
573 chix = ix;
574 }
575 BUG_ON(chix != path->p_idx);
576 }
577#endif
578
579}
580
581/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700582 * ext4_ext_binsearch:
583 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400584 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700585 */
586static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500587ext4_ext_binsearch(struct inode *inode,
588 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700589{
590 struct ext4_extent_header *eh = path->p_hdr;
591 struct ext4_extent *r, *l, *m;
592
Alex Tomasa86c6182006-10-11 01:21:03 -0700593 if (eh->eh_entries == 0) {
594 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700595 * this leaf is empty:
596 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700597 */
598 return;
599 }
600
Eric Sandeenbba90742008-01-28 23:58:27 -0500601 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700602
603 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400604 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700605
606 while (l <= r) {
607 m = l + (r - l) / 2;
608 if (block < le32_to_cpu(m->ee_block))
609 r = m - 1;
610 else
611 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400612 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
613 m, le32_to_cpu(m->ee_block),
614 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700615 }
616
617 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400618 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400619 le32_to_cpu(path->p_ext->ee_block),
620 ext_pblock(path->p_ext),
Mingming553f9002009-09-18 13:34:55 -0400621 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400622 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700623
624#ifdef CHECK_BINSEARCH
625 {
626 struct ext4_extent *chex, *ex;
627 int k;
628
629 chex = ex = EXT_FIRST_EXTENT(eh);
630 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
631 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400632 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700633 if (block < le32_to_cpu(ex->ee_block))
634 break;
635 chex = ex;
636 }
637 BUG_ON(chex != path->p_ext);
638 }
639#endif
640
641}
642
643int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
644{
645 struct ext4_extent_header *eh;
646
647 eh = ext_inode_hdr(inode);
648 eh->eh_depth = 0;
649 eh->eh_entries = 0;
650 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400651 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700652 ext4_mark_inode_dirty(handle, inode);
653 ext4_ext_invalidate_cache(inode);
654 return 0;
655}
656
657struct ext4_ext_path *
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500658ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
659 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700660{
661 struct ext4_extent_header *eh;
662 struct buffer_head *bh;
663 short int depth, i, ppos = 0, alloc = 0;
664
665 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400666 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700667
668 /* account possible depth increase */
669 if (!path) {
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800670 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700671 GFP_NOFS);
672 if (!path)
673 return ERR_PTR(-ENOMEM);
674 alloc = 1;
675 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700676 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400677 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700678
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400679 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700680 /* walk through the tree */
681 while (i) {
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400682 int need_to_validate = 0;
683
Alex Tomasa86c6182006-10-11 01:21:03 -0700684 ext_debug("depth %d: num %d, max %d\n",
685 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400686
Alex Tomasa86c6182006-10-11 01:21:03 -0700687 ext4_ext_binsearch_idx(inode, path + ppos, block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700688 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700689 path[ppos].p_depth = i;
690 path[ppos].p_ext = NULL;
691
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400692 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
693 if (unlikely(!bh))
Alex Tomasa86c6182006-10-11 01:21:03 -0700694 goto err;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400695 if (!bh_uptodate_or_lock(bh)) {
696 if (bh_submit_read(bh) < 0) {
697 put_bh(bh);
698 goto err;
699 }
700 /* validate the extent entries */
701 need_to_validate = 1;
702 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700703 eh = ext_block_hdr(bh);
704 ppos++;
Frank Mayhar273df552010-03-02 11:46:09 -0500705 if (unlikely(ppos > depth)) {
706 put_bh(bh);
707 EXT4_ERROR_INODE(inode,
708 "ppos %d > depth %d", ppos, depth);
709 goto err;
710 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700711 path[ppos].p_bh = bh;
712 path[ppos].p_hdr = eh;
713 i--;
714
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400715 if (need_to_validate && ext4_ext_check(inode, eh, i))
Alex Tomasa86c6182006-10-11 01:21:03 -0700716 goto err;
717 }
718
719 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700720 path[ppos].p_ext = NULL;
721 path[ppos].p_idx = NULL;
722
Alex Tomasa86c6182006-10-11 01:21:03 -0700723 /* find extent */
724 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400725 /* if not an empty leaf */
726 if (path[ppos].p_ext)
727 path[ppos].p_block = ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700728
729 ext4_ext_show_path(inode, path);
730
731 return path;
732
733err:
734 ext4_ext_drop_refs(path);
735 if (alloc)
736 kfree(path);
737 return ERR_PTR(-EIO);
738}
739
740/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700741 * ext4_ext_insert_index:
742 * insert new index [@logical;@ptr] into the block at @curp;
743 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700744 */
Mingming Cao00314622009-09-28 15:49:08 -0400745int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700746 struct ext4_ext_path *curp,
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700747 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700748{
749 struct ext4_extent_idx *ix;
750 int len, err;
751
Avantika Mathur7e028972006-12-06 20:41:33 -0800752 err = ext4_ext_get_access(handle, inode, curp);
753 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700754 return err;
755
Frank Mayhar273df552010-03-02 11:46:09 -0500756 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
757 EXT4_ERROR_INODE(inode,
758 "logical %d == ei_block %d!",
759 logical, le32_to_cpu(curp->p_idx->ei_block));
760 return -EIO;
761 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700762 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
763 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
764 /* insert after */
765 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
766 len = (len - 1) * sizeof(struct ext4_extent_idx);
767 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400768 ext_debug("insert new index %d after: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700769 "move %d from 0x%p to 0x%p\n",
770 logical, ptr, len,
771 (curp->p_idx + 1), (curp->p_idx + 2));
772 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
773 }
774 ix = curp->p_idx + 1;
775 } else {
776 /* insert before */
777 len = len * sizeof(struct ext4_extent_idx);
778 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400779 ext_debug("insert new index %d before: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700780 "move %d from 0x%p to 0x%p\n",
781 logical, ptr, len,
782 curp->p_idx, (curp->p_idx + 1));
783 memmove(curp->p_idx + 1, curp->p_idx, len);
784 ix = curp->p_idx;
785 }
786
787 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700788 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400789 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700790
Frank Mayhar273df552010-03-02 11:46:09 -0500791 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
792 > le16_to_cpu(curp->p_hdr->eh_max))) {
793 EXT4_ERROR_INODE(inode,
794 "logical %d == ei_block %d!",
795 logical, le32_to_cpu(curp->p_idx->ei_block));
796 return -EIO;
797 }
798 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
799 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
800 return -EIO;
801 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700802
803 err = ext4_ext_dirty(handle, inode, curp);
804 ext4_std_error(inode->i_sb, err);
805
806 return err;
807}
808
809/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700810 * ext4_ext_split:
811 * inserts new subtree into the path, using free index entry
812 * at depth @at:
813 * - allocates all needed blocks (new leaf and all intermediate index blocks)
814 * - makes decision where to split
815 * - moves remaining extents and index entries (right to the split point)
816 * into the newly allocated blocks
817 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700818 */
819static int ext4_ext_split(handle_t *handle, struct inode *inode,
820 struct ext4_ext_path *path,
821 struct ext4_extent *newext, int at)
822{
823 struct buffer_head *bh = NULL;
824 int depth = ext_depth(inode);
825 struct ext4_extent_header *neh;
826 struct ext4_extent_idx *fidx;
827 struct ext4_extent *ex;
828 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700829 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700830 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700831 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700832 int err = 0;
833
834 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700835 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700836
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700837 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700838 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -0500839 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
840 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
841 return -EIO;
842 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700843 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
844 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700845 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700846 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400847 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700848 } else {
849 border = newext->ee_block;
850 ext_debug("leaf will be added."
851 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400852 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700853 }
854
855 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700856 * If error occurs, then we break processing
857 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700858 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700859 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700860 */
861
862 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700863 * Get array to track all allocated blocks.
864 * We need this to handle errors and free blocks
865 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700866 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800867 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700868 if (!ablocks)
869 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700870
871 /* allocate all needed blocks */
872 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
873 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400874 newblock = ext4_ext_new_meta_block(handle, inode, path,
875 newext, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700876 if (newblock == 0)
877 goto cleanup;
878 ablocks[a] = newblock;
879 }
880
881 /* initialize new leaf */
882 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -0500883 if (unlikely(newblock == 0)) {
884 EXT4_ERROR_INODE(inode, "newblock == 0!");
885 err = -EIO;
886 goto cleanup;
887 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700888 bh = sb_getblk(inode->i_sb, newblock);
889 if (!bh) {
890 err = -EIO;
891 goto cleanup;
892 }
893 lock_buffer(bh);
894
Avantika Mathur7e028972006-12-06 20:41:33 -0800895 err = ext4_journal_get_create_access(handle, bh);
896 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700897 goto cleanup;
898
899 neh = ext_block_hdr(bh);
900 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400901 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700902 neh->eh_magic = EXT4_EXT_MAGIC;
903 neh->eh_depth = 0;
904 ex = EXT_FIRST_EXTENT(neh);
905
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700906 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -0500907 if (unlikely(path[depth].p_hdr->eh_entries !=
908 path[depth].p_hdr->eh_max)) {
909 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
910 path[depth].p_hdr->eh_entries,
911 path[depth].p_hdr->eh_max);
912 err = -EIO;
913 goto cleanup;
914 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700915 /* start copy from next extent */
916 /* TODO: we could do it by single memmove */
917 m = 0;
918 path[depth].p_ext++;
919 while (path[depth].p_ext <=
920 EXT_MAX_EXTENT(path[depth].p_hdr)) {
Mingming553f9002009-09-18 13:34:55 -0400921 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400922 le32_to_cpu(path[depth].p_ext->ee_block),
923 ext_pblock(path[depth].p_ext),
Mingming553f9002009-09-18 13:34:55 -0400924 ext4_ext_is_uninitialized(path[depth].p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400925 ext4_ext_get_actual_len(path[depth].p_ext),
Alex Tomasa86c6182006-10-11 01:21:03 -0700926 newblock);
927 /*memmove(ex++, path[depth].p_ext++,
928 sizeof(struct ext4_extent));
929 neh->eh_entries++;*/
930 path[depth].p_ext++;
931 m++;
932 }
933 if (m) {
934 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400935 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700936 }
937
938 set_buffer_uptodate(bh);
939 unlock_buffer(bh);
940
Frank Mayhar03901312009-01-07 00:06:22 -0500941 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800942 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700943 goto cleanup;
944 brelse(bh);
945 bh = NULL;
946
947 /* correct old leaf */
948 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800949 err = ext4_ext_get_access(handle, inode, path + depth);
950 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700951 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400952 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800953 err = ext4_ext_dirty(handle, inode, path + depth);
954 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700955 goto cleanup;
956
957 }
958
959 /* create intermediate indexes */
960 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -0500961 if (unlikely(k < 0)) {
962 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
963 err = -EIO;
964 goto cleanup;
965 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700966 if (k)
967 ext_debug("create %d intermediate indices\n", k);
968 /* insert new index into current index block */
969 /* current depth stored in i var */
970 i = depth - 1;
971 while (k--) {
972 oldblock = newblock;
973 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -0500974 bh = sb_getblk(inode->i_sb, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700975 if (!bh) {
976 err = -EIO;
977 goto cleanup;
978 }
979 lock_buffer(bh);
980
Avantika Mathur7e028972006-12-06 20:41:33 -0800981 err = ext4_journal_get_create_access(handle, bh);
982 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700983 goto cleanup;
984
985 neh = ext_block_hdr(bh);
986 neh->eh_entries = cpu_to_le16(1);
987 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400988 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700989 neh->eh_depth = cpu_to_le16(depth - i);
990 fidx = EXT_FIRST_INDEX(neh);
991 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700992 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700993
Eric Sandeenbba90742008-01-28 23:58:27 -0500994 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
995 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700996 /* copy indexes */
997 m = 0;
998 path[i].p_idx++;
999
1000 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
1001 EXT_MAX_INDEX(path[i].p_hdr));
Frank Mayhar273df552010-03-02 11:46:09 -05001002 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
1003 EXT_LAST_INDEX(path[i].p_hdr))) {
1004 EXT4_ERROR_INODE(inode,
1005 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1006 le32_to_cpu(path[i].p_ext->ee_block));
1007 err = -EIO;
1008 goto cleanup;
1009 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001010 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
Dmitry Monakhov26d535e2007-07-18 08:33:37 -04001011 ext_debug("%d: move %d:%llu in new index %llu\n", i,
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001012 le32_to_cpu(path[i].p_idx->ei_block),
1013 idx_pblock(path[i].p_idx),
1014 newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001015 /*memmove(++fidx, path[i].p_idx++,
1016 sizeof(struct ext4_extent_idx));
1017 neh->eh_entries++;
1018 BUG_ON(neh->eh_entries > neh->eh_max);*/
1019 path[i].p_idx++;
1020 m++;
1021 }
1022 if (m) {
1023 memmove(++fidx, path[i].p_idx - m,
1024 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001025 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001026 }
1027 set_buffer_uptodate(bh);
1028 unlock_buffer(bh);
1029
Frank Mayhar03901312009-01-07 00:06:22 -05001030 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001031 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001032 goto cleanup;
1033 brelse(bh);
1034 bh = NULL;
1035
1036 /* correct old index */
1037 if (m) {
1038 err = ext4_ext_get_access(handle, inode, path + i);
1039 if (err)
1040 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001041 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001042 err = ext4_ext_dirty(handle, inode, path + i);
1043 if (err)
1044 goto cleanup;
1045 }
1046
1047 i--;
1048 }
1049
1050 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001051 err = ext4_ext_insert_index(handle, inode, path + at,
1052 le32_to_cpu(border), newblock);
1053
1054cleanup:
1055 if (bh) {
1056 if (buffer_locked(bh))
1057 unlock_buffer(bh);
1058 brelse(bh);
1059 }
1060
1061 if (err) {
1062 /* free all allocated blocks in error case */
1063 for (i = 0; i < depth; i++) {
1064 if (!ablocks[i])
1065 continue;
Theodore Ts'oe6362602009-11-23 07:17:05 -05001066 ext4_free_blocks(handle, inode, 0, ablocks[i], 1,
1067 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001068 }
1069 }
1070 kfree(ablocks);
1071
1072 return err;
1073}
1074
1075/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001076 * ext4_ext_grow_indepth:
1077 * implements tree growing procedure:
1078 * - allocates new block
1079 * - moves top-level data (index block or leaf) into the new block
1080 * - initializes new top-level, creating index that points to the
1081 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001082 */
1083static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
1084 struct ext4_ext_path *path,
1085 struct ext4_extent *newext)
1086{
1087 struct ext4_ext_path *curp = path;
1088 struct ext4_extent_header *neh;
1089 struct ext4_extent_idx *fidx;
1090 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001091 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001092 int err = 0;
1093
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -04001094 newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07001095 if (newblock == 0)
1096 return err;
1097
1098 bh = sb_getblk(inode->i_sb, newblock);
1099 if (!bh) {
1100 err = -EIO;
1101 ext4_std_error(inode->i_sb, err);
1102 return err;
1103 }
1104 lock_buffer(bh);
1105
Avantika Mathur7e028972006-12-06 20:41:33 -08001106 err = ext4_journal_get_create_access(handle, bh);
1107 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001108 unlock_buffer(bh);
1109 goto out;
1110 }
1111
1112 /* move top-level index/leaf into new block */
1113 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
1114
1115 /* set size of new block */
1116 neh = ext_block_hdr(bh);
1117 /* old root could have indexes or leaves
1118 * so calculate e_max right way */
1119 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001120 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001121 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001122 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001123 neh->eh_magic = EXT4_EXT_MAGIC;
1124 set_buffer_uptodate(bh);
1125 unlock_buffer(bh);
1126
Frank Mayhar03901312009-01-07 00:06:22 -05001127 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001128 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001129 goto out;
1130
1131 /* create index in new top-level index: num,max,pointer */
Avantika Mathur7e028972006-12-06 20:41:33 -08001132 err = ext4_ext_get_access(handle, inode, curp);
1133 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001134 goto out;
1135
1136 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001137 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001138 curp->p_hdr->eh_entries = cpu_to_le16(1);
1139 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
Dmitry Monakhove9f410b2007-07-18 09:09:15 -04001140
1141 if (path[0].p_hdr->eh_depth)
1142 curp->p_idx->ei_block =
1143 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
1144 else
1145 curp->p_idx->ei_block =
1146 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001147 ext4_idx_store_pblock(curp->p_idx, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001148
1149 neh = ext_inode_hdr(inode);
1150 fidx = EXT_FIRST_INDEX(neh);
Mingming Cao2ae02102006-10-11 01:21:11 -07001151 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001152 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001153 le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
Alex Tomasa86c6182006-10-11 01:21:03 -07001154
1155 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
1156 err = ext4_ext_dirty(handle, inode, curp);
1157out:
1158 brelse(bh);
1159
1160 return err;
1161}
1162
1163/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001164 * ext4_ext_create_new_leaf:
1165 * finds empty index and adds new leaf.
1166 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001167 */
1168static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
1169 struct ext4_ext_path *path,
1170 struct ext4_extent *newext)
1171{
1172 struct ext4_ext_path *curp;
1173 int depth, i, err = 0;
1174
1175repeat:
1176 i = depth = ext_depth(inode);
1177
1178 /* walk up to the tree and look for free index entry */
1179 curp = path + depth;
1180 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1181 i--;
1182 curp--;
1183 }
1184
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001185 /* we use already allocated block for index block,
1186 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001187 if (EXT_HAS_FREE_INDEX(curp)) {
1188 /* if we found index with free entry, then use that
1189 * entry: create all needed subtree and add new leaf */
1190 err = ext4_ext_split(handle, inode, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001191 if (err)
1192 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001193
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 } else {
1202 /* tree is full, time to grow in depth */
1203 err = ext4_ext_grow_indepth(handle, inode, path, newext);
1204 if (err)
1205 goto out;
1206
1207 /* refill path */
1208 ext4_ext_drop_refs(path);
1209 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001210 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1211 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001212 if (IS_ERR(path)) {
1213 err = PTR_ERR(path);
1214 goto out;
1215 }
1216
1217 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001218 * only first (depth 0 -> 1) produces free space;
1219 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001220 */
1221 depth = ext_depth(inode);
1222 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001223 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001224 goto repeat;
1225 }
1226 }
1227
1228out:
1229 return err;
1230}
1231
1232/*
Alex Tomas1988b512008-01-28 23:58:27 -05001233 * search the closest allocated block to the left for *logical
1234 * and returns it at @logical + it's physical address at @phys
1235 * if *logical is the smallest allocated block, the function
1236 * returns 0 at @phys
1237 * return value contains 0 (success) or error code
1238 */
1239int
1240ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
1241 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1242{
1243 struct ext4_extent_idx *ix;
1244 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001245 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001246
Frank Mayhar273df552010-03-02 11:46:09 -05001247 if (unlikely(path == NULL)) {
1248 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1249 return -EIO;
1250 }
Alex Tomas1988b512008-01-28 23:58:27 -05001251 depth = path->p_depth;
1252 *phys = 0;
1253
1254 if (depth == 0 && path->p_ext == NULL)
1255 return 0;
1256
1257 /* usually extent in the path covers blocks smaller
1258 * then *logical, but it can be that extent is the
1259 * first one in the file */
1260
1261 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001262 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001263 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001264 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1265 EXT4_ERROR_INODE(inode,
1266 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1267 *logical, le32_to_cpu(ex->ee_block));
1268 return -EIO;
1269 }
Alex Tomas1988b512008-01-28 23:58:27 -05001270 while (--depth >= 0) {
1271 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001272 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1273 EXT4_ERROR_INODE(inode,
1274 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
1275 ix != NULL ? ix->ei_block : 0,
1276 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
1277 EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block : 0,
1278 depth);
1279 return -EIO;
1280 }
Alex Tomas1988b512008-01-28 23:58:27 -05001281 }
1282 return 0;
1283 }
1284
Frank Mayhar273df552010-03-02 11:46:09 -05001285 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1286 EXT4_ERROR_INODE(inode,
1287 "logical %d < ee_block %d + ee_len %d!",
1288 *logical, le32_to_cpu(ex->ee_block), ee_len);
1289 return -EIO;
1290 }
Alex Tomas1988b512008-01-28 23:58:27 -05001291
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001292 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1293 *phys = ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001294 return 0;
1295}
1296
1297/*
1298 * search the closest allocated block to the right for *logical
1299 * and returns it at @logical + it's physical address at @phys
1300 * if *logical is the smallest allocated block, the function
1301 * returns 0 at @phys
1302 * return value contains 0 (success) or error code
1303 */
1304int
1305ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
1306 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1307{
1308 struct buffer_head *bh = NULL;
1309 struct ext4_extent_header *eh;
1310 struct ext4_extent_idx *ix;
1311 struct ext4_extent *ex;
1312 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001313 int depth; /* Note, NOT eh_depth; depth from top of tree */
1314 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001315
Frank Mayhar273df552010-03-02 11:46:09 -05001316 if (unlikely(path == NULL)) {
1317 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1318 return -EIO;
1319 }
Alex Tomas1988b512008-01-28 23:58:27 -05001320 depth = path->p_depth;
1321 *phys = 0;
1322
1323 if (depth == 0 && path->p_ext == NULL)
1324 return 0;
1325
1326 /* usually extent in the path covers blocks smaller
1327 * then *logical, but it can be that extent is the
1328 * first one in the file */
1329
1330 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001331 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001332 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001333 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1334 EXT4_ERROR_INODE(inode,
1335 "first_extent(path[%d].p_hdr) != ex",
1336 depth);
1337 return -EIO;
1338 }
Alex Tomas1988b512008-01-28 23:58:27 -05001339 while (--depth >= 0) {
1340 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001341 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1342 EXT4_ERROR_INODE(inode,
1343 "ix != EXT_FIRST_INDEX *logical %d!",
1344 *logical);
1345 return -EIO;
1346 }
Alex Tomas1988b512008-01-28 23:58:27 -05001347 }
1348 *logical = le32_to_cpu(ex->ee_block);
1349 *phys = ext_pblock(ex);
1350 return 0;
1351 }
1352
Frank Mayhar273df552010-03-02 11:46:09 -05001353 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1354 EXT4_ERROR_INODE(inode,
1355 "logical %d < ee_block %d + ee_len %d!",
1356 *logical, le32_to_cpu(ex->ee_block), ee_len);
1357 return -EIO;
1358 }
Alex Tomas1988b512008-01-28 23:58:27 -05001359
1360 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1361 /* next allocated block in this leaf */
1362 ex++;
1363 *logical = le32_to_cpu(ex->ee_block);
1364 *phys = ext_pblock(ex);
1365 return 0;
1366 }
1367
1368 /* go up and search for index to the right */
1369 while (--depth >= 0) {
1370 ix = path[depth].p_idx;
1371 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001372 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001373 }
1374
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001375 /* we've gone up to the root and found no index to the right */
1376 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001377
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001378got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001379 /* we've found index to the right, let's
1380 * follow it and find the closest allocated
1381 * block to the right */
1382 ix++;
1383 block = idx_pblock(ix);
1384 while (++depth < path->p_depth) {
1385 bh = sb_bread(inode->i_sb, block);
1386 if (bh == NULL)
1387 return -EIO;
1388 eh = ext_block_hdr(bh);
Eric Sandeen395a87b2009-03-10 18:18:47 -04001389 /* subtract from p_depth to get proper eh_depth */
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 ix = EXT_FIRST_INDEX(eh);
1395 block = idx_pblock(ix);
1396 put_bh(bh);
1397 }
1398
1399 bh = sb_bread(inode->i_sb, block);
1400 if (bh == NULL)
1401 return -EIO;
1402 eh = ext_block_hdr(bh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001403 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001404 put_bh(bh);
1405 return -EIO;
1406 }
1407 ex = EXT_FIRST_EXTENT(eh);
1408 *logical = le32_to_cpu(ex->ee_block);
1409 *phys = ext_pblock(ex);
1410 put_bh(bh);
1411 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001412}
1413
1414/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001415 * ext4_ext_next_allocated_block:
1416 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1417 * NOTE: it considers block number from index entry as
1418 * allocated block. Thus, index entries have to be consistent
1419 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001420 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001421static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001422ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1423{
1424 int depth;
1425
1426 BUG_ON(path == NULL);
1427 depth = path->p_depth;
1428
1429 if (depth == 0 && path->p_ext == NULL)
1430 return EXT_MAX_BLOCK;
1431
1432 while (depth >= 0) {
1433 if (depth == path->p_depth) {
1434 /* leaf */
1435 if (path[depth].p_ext !=
1436 EXT_LAST_EXTENT(path[depth].p_hdr))
1437 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1438 } else {
1439 /* index */
1440 if (path[depth].p_idx !=
1441 EXT_LAST_INDEX(path[depth].p_hdr))
1442 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1443 }
1444 depth--;
1445 }
1446
1447 return EXT_MAX_BLOCK;
1448}
1449
1450/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001451 * ext4_ext_next_leaf_block:
Alex Tomasa86c6182006-10-11 01:21:03 -07001452 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1453 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001454static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
Andrew Morton63f57932006-10-11 01:21:24 -07001455 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001456{
1457 int depth;
1458
1459 BUG_ON(path == NULL);
1460 depth = path->p_depth;
1461
1462 /* zero-tree has no leaf blocks at all */
1463 if (depth == 0)
1464 return EXT_MAX_BLOCK;
1465
1466 /* go to index block */
1467 depth--;
1468
1469 while (depth >= 0) {
1470 if (path[depth].p_idx !=
1471 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001472 return (ext4_lblk_t)
1473 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001474 depth--;
1475 }
1476
1477 return EXT_MAX_BLOCK;
1478}
1479
1480/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001481 * ext4_ext_correct_indexes:
1482 * if leaf gets modified and modified extent is first in the leaf,
1483 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001484 * TODO: do we need to correct tree in all cases?
1485 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001486static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001487 struct ext4_ext_path *path)
1488{
1489 struct ext4_extent_header *eh;
1490 int depth = ext_depth(inode);
1491 struct ext4_extent *ex;
1492 __le32 border;
1493 int k, err = 0;
1494
1495 eh = path[depth].p_hdr;
1496 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001497
1498 if (unlikely(ex == NULL || eh == NULL)) {
1499 EXT4_ERROR_INODE(inode,
1500 "ex %p == NULL or eh %p == NULL", ex, eh);
1501 return -EIO;
1502 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001503
1504 if (depth == 0) {
1505 /* there is no tree at all */
1506 return 0;
1507 }
1508
1509 if (ex != EXT_FIRST_EXTENT(eh)) {
1510 /* we correct tree if first leaf got modified only */
1511 return 0;
1512 }
1513
1514 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001515 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001516 */
1517 k = depth - 1;
1518 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001519 err = ext4_ext_get_access(handle, inode, path + k);
1520 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001521 return err;
1522 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001523 err = ext4_ext_dirty(handle, inode, path + k);
1524 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001525 return err;
1526
1527 while (k--) {
1528 /* change all left-side indexes */
1529 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1530 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001531 err = ext4_ext_get_access(handle, inode, path + k);
1532 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001533 break;
1534 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001535 err = ext4_ext_dirty(handle, inode, path + k);
1536 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001537 break;
1538 }
1539
1540 return err;
1541}
1542
Akira Fujita748de672009-06-17 19:24:03 -04001543int
Alex Tomasa86c6182006-10-11 01:21:03 -07001544ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1545 struct ext4_extent *ex2)
1546{
Amit Arora749269f2007-07-18 09:02:56 -04001547 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001548
1549 /*
1550 * Make sure that either both extents are uninitialized, or
1551 * both are _not_.
1552 */
1553 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1554 return 0;
1555
Amit Arora749269f2007-07-18 09:02:56 -04001556 if (ext4_ext_is_uninitialized(ex1))
1557 max_len = EXT_UNINIT_MAX_LEN;
1558 else
1559 max_len = EXT_INIT_MAX_LEN;
1560
Amit Aroraa2df2a62007-07-17 21:42:41 -04001561 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1562 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1563
1564 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001565 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001566 return 0;
1567
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001568 /*
1569 * To allow future support for preallocated extents to be added
1570 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001571 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001572 */
Amit Arora749269f2007-07-18 09:02:56 -04001573 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001574 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001575#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001576 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001577 return 0;
1578#endif
1579
Amit Aroraa2df2a62007-07-17 21:42:41 -04001580 if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001581 return 1;
1582 return 0;
1583}
1584
1585/*
Amit Arora56055d32007-07-17 21:42:38 -04001586 * This function tries to merge the "ex" extent to the next extent in the tree.
1587 * It always tries to merge towards right. If you want to merge towards
1588 * left, pass "ex - 1" as argument instead of "ex".
1589 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1590 * 1 if they got merged.
1591 */
1592int ext4_ext_try_to_merge(struct inode *inode,
1593 struct ext4_ext_path *path,
1594 struct ext4_extent *ex)
1595{
1596 struct ext4_extent_header *eh;
1597 unsigned int depth, len;
1598 int merge_done = 0;
1599 int uninitialized = 0;
1600
1601 depth = ext_depth(inode);
1602 BUG_ON(path[depth].p_hdr == NULL);
1603 eh = path[depth].p_hdr;
1604
1605 while (ex < EXT_LAST_EXTENT(eh)) {
1606 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1607 break;
1608 /* merge with next extent! */
1609 if (ext4_ext_is_uninitialized(ex))
1610 uninitialized = 1;
1611 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1612 + ext4_ext_get_actual_len(ex + 1));
1613 if (uninitialized)
1614 ext4_ext_mark_uninitialized(ex);
1615
1616 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1617 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1618 * sizeof(struct ext4_extent);
1619 memmove(ex + 1, ex + 2, len);
1620 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001621 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001622 merge_done = 1;
1623 WARN_ON(eh->eh_entries == 0);
1624 if (!eh->eh_entries)
Eric Sandeen12062dd2010-02-15 14:19:27 -05001625 ext4_error(inode->i_sb,
1626 "inode#%lu, eh->eh_entries = 0!",
1627 inode->i_ino);
Amit Arora56055d32007-07-17 21:42:38 -04001628 }
1629
1630 return merge_done;
1631}
1632
1633/*
Amit Arora25d14f92007-05-24 13:04:13 -04001634 * check if a portion of the "newext" extent overlaps with an
1635 * existing extent.
1636 *
1637 * If there is an overlap discovered, it updates the length of the newext
1638 * such that there will be no overlap, and then returns 1.
1639 * If there is no overlap found, it returns 0.
1640 */
1641unsigned int ext4_ext_check_overlap(struct inode *inode,
1642 struct ext4_extent *newext,
1643 struct ext4_ext_path *path)
1644{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001645 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001646 unsigned int depth, len1;
1647 unsigned int ret = 0;
1648
1649 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001650 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001651 depth = ext_depth(inode);
1652 if (!path[depth].p_ext)
1653 goto out;
1654 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1655
1656 /*
1657 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001658 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001659 */
1660 if (b2 < b1) {
1661 b2 = ext4_ext_next_allocated_block(path);
1662 if (b2 == EXT_MAX_BLOCK)
1663 goto out;
1664 }
1665
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001666 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001667 if (b1 + len1 < b1) {
1668 len1 = EXT_MAX_BLOCK - b1;
1669 newext->ee_len = cpu_to_le16(len1);
1670 ret = 1;
1671 }
1672
1673 /* check for overlap */
1674 if (b1 + len1 > b2) {
1675 newext->ee_len = cpu_to_le16(b2 - b1);
1676 ret = 1;
1677 }
1678out:
1679 return ret;
1680}
1681
1682/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001683 * ext4_ext_insert_extent:
1684 * tries to merge requsted extent into the existing extent or
1685 * inserts requested extent as new one into the tree,
1686 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001687 */
1688int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1689 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04001690 struct ext4_extent *newext, int flag)
Alex Tomasa86c6182006-10-11 01:21:03 -07001691{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001692 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001693 struct ext4_extent *ex, *fex;
1694 struct ext4_extent *nearex; /* nearest extent */
1695 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001696 int depth, len, err;
1697 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001698 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001699
Frank Mayhar273df552010-03-02 11:46:09 -05001700 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1701 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1702 return -EIO;
1703 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001704 depth = ext_depth(inode);
1705 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001706 if (unlikely(path[depth].p_hdr == NULL)) {
1707 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1708 return -EIO;
1709 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001710
1711 /* try to insert block into found extent and return */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001712 if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
Mingming Cao00314622009-09-28 15:49:08 -04001713 && ext4_can_extents_be_merged(inode, ex, newext)) {
Mingming553f9002009-09-18 13:34:55 -04001714 ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
1715 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001716 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001717 le32_to_cpu(ex->ee_block),
Mingming553f9002009-09-18 13:34:55 -04001718 ext4_ext_is_uninitialized(ex),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001719 ext4_ext_get_actual_len(ex), ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001720 err = ext4_ext_get_access(handle, inode, path + depth);
1721 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001722 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001723
1724 /*
1725 * ext4_can_extents_be_merged should have checked that either
1726 * both extents are uninitialized, or both aren't. Thus we
1727 * need to check only one of them here.
1728 */
1729 if (ext4_ext_is_uninitialized(ex))
1730 uninitialized = 1;
1731 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1732 + ext4_ext_get_actual_len(newext));
1733 if (uninitialized)
1734 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001735 eh = path[depth].p_hdr;
1736 nearex = ex;
1737 goto merge;
1738 }
1739
1740repeat:
1741 depth = ext_depth(inode);
1742 eh = path[depth].p_hdr;
1743 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1744 goto has_space;
1745
1746 /* probably next leaf has space for us? */
1747 fex = EXT_LAST_EXTENT(eh);
1748 next = ext4_ext_next_leaf_block(inode, path);
1749 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1750 && next != EXT_MAX_BLOCK) {
1751 ext_debug("next leaf block - %d\n", next);
1752 BUG_ON(npath != NULL);
1753 npath = ext4_ext_find_extent(inode, next, NULL);
1754 if (IS_ERR(npath))
1755 return PTR_ERR(npath);
1756 BUG_ON(npath->p_depth != path->p_depth);
1757 eh = npath[depth].p_hdr;
1758 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1759 ext_debug("next leaf isnt full(%d)\n",
1760 le16_to_cpu(eh->eh_entries));
1761 path = npath;
1762 goto repeat;
1763 }
1764 ext_debug("next leaf has no free space(%d,%d)\n",
1765 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1766 }
1767
1768 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001769 * There is no free space in the found leaf.
1770 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001771 */
1772 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1773 if (err)
1774 goto cleanup;
1775 depth = ext_depth(inode);
1776 eh = path[depth].p_hdr;
1777
1778has_space:
1779 nearex = path[depth].p_ext;
1780
Avantika Mathur7e028972006-12-06 20:41:33 -08001781 err = ext4_ext_get_access(handle, inode, path + depth);
1782 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001783 goto cleanup;
1784
1785 if (!nearex) {
1786 /* there is no extent in this leaf, create first one */
Mingming553f9002009-09-18 13:34:55 -04001787 ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001788 le32_to_cpu(newext->ee_block),
1789 ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001790 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001791 ext4_ext_get_actual_len(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001792 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1793 } else if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001794 > le32_to_cpu(nearex->ee_block)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001795/* BUG_ON(newext->ee_block == nearex->ee_block); */
1796 if (nearex != EXT_LAST_EXTENT(eh)) {
1797 len = EXT_MAX_EXTENT(eh) - nearex;
1798 len = (len - 1) * sizeof(struct ext4_extent);
1799 len = len < 0 ? 0 : len;
Mingming553f9002009-09-18 13:34:55 -04001800 ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001801 "move %d from 0x%p to 0x%p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001802 le32_to_cpu(newext->ee_block),
1803 ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001804 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001805 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001806 nearex, len, nearex + 1, nearex + 2);
1807 memmove(nearex + 2, nearex + 1, len);
1808 }
1809 path[depth].p_ext = nearex + 1;
1810 } else {
1811 BUG_ON(newext->ee_block == nearex->ee_block);
1812 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1813 len = len < 0 ? 0 : len;
Mingming553f9002009-09-18 13:34:55 -04001814 ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001815 "move %d from 0x%p to 0x%p\n",
1816 le32_to_cpu(newext->ee_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001817 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 + 1, nearex, len);
1822 path[depth].p_ext = nearex;
1823 }
1824
Marcin Slusarze8546d02008-04-17 10:38:59 -04001825 le16_add_cpu(&eh->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001826 nearex = path[depth].p_ext;
1827 nearex->ee_block = newext->ee_block;
Aneesh Kumar K.Vb3776112007-10-16 18:38:25 -04001828 ext4_ext_store_pblock(nearex, ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001829 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001830
1831merge:
1832 /* try to merge extents to the right */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001833 if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
Mingming Cao00314622009-09-28 15:49:08 -04001834 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001835
1836 /* try to merge extents to the left */
1837
1838 /* time to correct all indexes above */
1839 err = ext4_ext_correct_indexes(handle, inode, path);
1840 if (err)
1841 goto cleanup;
1842
1843 err = ext4_ext_dirty(handle, inode, path + depth);
1844
1845cleanup:
1846 if (npath) {
1847 ext4_ext_drop_refs(npath);
1848 kfree(npath);
1849 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001850 ext4_ext_invalidate_cache(inode);
1851 return err;
1852}
1853
Eric Sandeen6873fa02008-10-07 00:46:36 -04001854int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1855 ext4_lblk_t num, ext_prepare_callback func,
1856 void *cbdata)
1857{
1858 struct ext4_ext_path *path = NULL;
1859 struct ext4_ext_cache cbex;
1860 struct ext4_extent *ex;
1861 ext4_lblk_t next, start = 0, end = 0;
1862 ext4_lblk_t last = block + num;
1863 int depth, exists, err = 0;
1864
1865 BUG_ON(func == NULL);
1866 BUG_ON(inode == NULL);
1867
1868 while (block < last && block != EXT_MAX_BLOCK) {
1869 num = last - block;
1870 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001871 down_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001872 path = ext4_ext_find_extent(inode, block, path);
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001873 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001874 if (IS_ERR(path)) {
1875 err = PTR_ERR(path);
1876 path = NULL;
1877 break;
1878 }
1879
1880 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05001881 if (unlikely(path[depth].p_hdr == NULL)) {
1882 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1883 err = -EIO;
1884 break;
1885 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04001886 ex = path[depth].p_ext;
1887 next = ext4_ext_next_allocated_block(path);
1888
1889 exists = 0;
1890 if (!ex) {
1891 /* there is no extent yet, so try to allocate
1892 * all requested space */
1893 start = block;
1894 end = block + num;
1895 } else if (le32_to_cpu(ex->ee_block) > block) {
1896 /* need to allocate space before found extent */
1897 start = block;
1898 end = le32_to_cpu(ex->ee_block);
1899 if (block + num < end)
1900 end = block + num;
1901 } else if (block >= le32_to_cpu(ex->ee_block)
1902 + ext4_ext_get_actual_len(ex)) {
1903 /* need to allocate space after found extent */
1904 start = block;
1905 end = block + num;
1906 if (end >= next)
1907 end = next;
1908 } else if (block >= le32_to_cpu(ex->ee_block)) {
1909 /*
1910 * some part of requested space is covered
1911 * by found extent
1912 */
1913 start = block;
1914 end = le32_to_cpu(ex->ee_block)
1915 + ext4_ext_get_actual_len(ex);
1916 if (block + num < end)
1917 end = block + num;
1918 exists = 1;
1919 } else {
1920 BUG();
1921 }
1922 BUG_ON(end <= start);
1923
1924 if (!exists) {
1925 cbex.ec_block = start;
1926 cbex.ec_len = end - start;
1927 cbex.ec_start = 0;
1928 cbex.ec_type = EXT4_EXT_CACHE_GAP;
1929 } else {
1930 cbex.ec_block = le32_to_cpu(ex->ee_block);
1931 cbex.ec_len = ext4_ext_get_actual_len(ex);
1932 cbex.ec_start = ext_pblock(ex);
1933 cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
1934 }
1935
Frank Mayhar273df552010-03-02 11:46:09 -05001936 if (unlikely(cbex.ec_len == 0)) {
1937 EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
1938 err = -EIO;
1939 break;
1940 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04001941 err = func(inode, path, &cbex, ex, cbdata);
1942 ext4_ext_drop_refs(path);
1943
1944 if (err < 0)
1945 break;
1946
1947 if (err == EXT_REPEAT)
1948 continue;
1949 else if (err == EXT_BREAK) {
1950 err = 0;
1951 break;
1952 }
1953
1954 if (ext_depth(inode) != depth) {
1955 /* depth was changed. we have to realloc path */
1956 kfree(path);
1957 path = NULL;
1958 }
1959
1960 block = cbex.ec_block + cbex.ec_len;
1961 }
1962
1963 if (path) {
1964 ext4_ext_drop_refs(path);
1965 kfree(path);
1966 }
1967
1968 return err;
1969}
1970
Avantika Mathur09b88252006-12-06 20:41:36 -08001971static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001972ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Mingming Caodd545672007-07-31 00:37:46 -07001973 __u32 len, ext4_fsblk_t start, int type)
Alex Tomasa86c6182006-10-11 01:21:03 -07001974{
1975 struct ext4_ext_cache *cex;
1976 BUG_ON(len == 0);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001977 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001978 cex = &EXT4_I(inode)->i_cached_extent;
1979 cex->ec_type = type;
1980 cex->ec_block = block;
1981 cex->ec_len = len;
1982 cex->ec_start = start;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001983 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001984}
1985
1986/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001987 * ext4_ext_put_gap_in_cache:
1988 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07001989 * and cache this gap
1990 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001991static void
Alex Tomasa86c6182006-10-11 01:21:03 -07001992ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001993 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07001994{
1995 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001996 unsigned long len;
1997 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001998 struct ext4_extent *ex;
1999
2000 ex = path[depth].p_ext;
2001 if (ex == NULL) {
2002 /* there is no extent yet, so gap is [0;-] */
2003 lblock = 0;
2004 len = EXT_MAX_BLOCK;
2005 ext_debug("cache gap(whole file):");
2006 } else if (block < le32_to_cpu(ex->ee_block)) {
2007 lblock = block;
2008 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05002009 ext_debug("cache gap(before): %u [%u:%u]",
2010 block,
2011 le32_to_cpu(ex->ee_block),
2012 ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002013 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002014 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002015 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002016 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002017 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002018
2019 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05002020 ext_debug("cache gap(after): [%u:%u] %u",
2021 le32_to_cpu(ex->ee_block),
2022 ext4_ext_get_actual_len(ex),
2023 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002024 BUG_ON(next == lblock);
2025 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07002026 } else {
2027 lblock = len = 0;
2028 BUG();
2029 }
2030
Eric Sandeenbba90742008-01-28 23:58:27 -05002031 ext_debug(" -> %u:%lu\n", lblock, len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002032 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
2033}
2034
Avantika Mathur09b88252006-12-06 20:41:36 -08002035static int
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002036ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
Alex Tomasa86c6182006-10-11 01:21:03 -07002037 struct ext4_extent *ex)
2038{
2039 struct ext4_ext_cache *cex;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002040 int ret = EXT4_EXT_CACHE_NO;
Alex Tomasa86c6182006-10-11 01:21:03 -07002041
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002042 /*
2043 * We borrow i_block_reservation_lock to protect i_cached_extent
2044 */
2045 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07002046 cex = &EXT4_I(inode)->i_cached_extent;
2047
2048 /* has cache valid data? */
2049 if (cex->ec_type == EXT4_EXT_CACHE_NO)
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002050 goto errout;
Alex Tomasa86c6182006-10-11 01:21:03 -07002051
2052 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
2053 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
Akinobu Mita731eb1a2010-03-03 23:55:01 -05002054 if (in_range(block, cex->ec_block, cex->ec_len)) {
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002055 ex->ee_block = cpu_to_le32(cex->ec_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002056 ext4_ext_store_pblock(ex, cex->ec_start);
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002057 ex->ee_len = cpu_to_le16(cex->ec_len);
Eric Sandeenbba90742008-01-28 23:58:27 -05002058 ext_debug("%u cached by %u:%u:%llu\n",
2059 block,
2060 cex->ec_block, cex->ec_len, cex->ec_start);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002061 ret = cex->ec_type;
Alex Tomasa86c6182006-10-11 01:21:03 -07002062 }
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002063errout:
2064 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
2065 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07002066}
2067
2068/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002069 * ext4_ext_rm_idx:
2070 * removes index from the index block.
2071 * It's used in truncate case only, thus all requests are for
2072 * last index in the block only.
Alex Tomasa86c6182006-10-11 01:21:03 -07002073 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002074static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07002075 struct ext4_ext_path *path)
2076{
Alex Tomasa86c6182006-10-11 01:21:03 -07002077 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002078 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002079
2080 /* free index block */
2081 path--;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002082 leaf = idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002083 if (unlikely(path->p_hdr->eh_entries == 0)) {
2084 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2085 return -EIO;
2086 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002087 err = ext4_ext_get_access(handle, inode, path);
2088 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002089 return err;
Marcin Slusarze8546d02008-04-17 10:38:59 -04002090 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002091 err = ext4_ext_dirty(handle, inode, path);
2092 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002093 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002094 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Theodore Ts'oe6362602009-11-23 07:17:05 -05002095 ext4_free_blocks(handle, inode, 0, leaf, 1,
2096 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Alex Tomasa86c6182006-10-11 01:21:03 -07002097 return err;
2098}
2099
2100/*
Mingming Caoee12b632008-08-19 22:16:05 -04002101 * ext4_ext_calc_credits_for_single_extent:
2102 * This routine returns max. credits that needed to insert an extent
2103 * to the extent tree.
2104 * When pass the actual path, the caller should calculate credits
2105 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002106 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002107int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002108 struct ext4_ext_path *path)
2109{
Alex Tomasa86c6182006-10-11 01:21:03 -07002110 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002111 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002112 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002113
Alex Tomasa86c6182006-10-11 01:21:03 -07002114 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002115 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002116 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2117
2118 /*
2119 * There are some space in the leaf tree, no
2120 * need to account for leaf block credit
2121 *
2122 * bitmaps and block group descriptor blocks
2123 * and other metadat blocks still need to be
2124 * accounted.
2125 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002126 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002127 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002128 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002129 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002130 }
2131
Mingming Cao525f4ed2008-08-19 22:15:58 -04002132 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002133}
Alex Tomasa86c6182006-10-11 01:21:03 -07002134
Mingming Caoee12b632008-08-19 22:16:05 -04002135/*
2136 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2137 *
2138 * if nrblocks are fit in a single extent (chunk flag is 1), then
2139 * in the worse case, each tree level index/leaf need to be changed
2140 * if the tree split due to insert a new extent, then the old tree
2141 * index/leaf need to be updated too
2142 *
2143 * If the nrblocks are discontiguous, they could cause
2144 * the whole tree split more than once, but this is really rare.
2145 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002146int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoee12b632008-08-19 22:16:05 -04002147{
2148 int index;
2149 int depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002150
Mingming Caoee12b632008-08-19 22:16:05 -04002151 if (chunk)
2152 index = depth * 2;
2153 else
2154 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002155
Mingming Caoee12b632008-08-19 22:16:05 -04002156 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002157}
2158
2159static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2160 struct ext4_extent *ex,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002161 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002162{
Amit Aroraa2df2a62007-07-17 21:42:41 -04002163 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe6362602009-11-23 07:17:05 -05002164 int flags = EXT4_FREE_BLOCKS_FORGET;
Alex Tomasa86c6182006-10-11 01:21:03 -07002165
Alex Tomasc9de5602008-01-29 00:19:52 -05002166 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
Theodore Ts'oe6362602009-11-23 07:17:05 -05002167 flags |= EXT4_FREE_BLOCKS_METADATA;
Alex Tomasa86c6182006-10-11 01:21:03 -07002168#ifdef EXTENTS_STATS
2169 {
2170 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002171 spin_lock(&sbi->s_ext_stats_lock);
2172 sbi->s_ext_blocks += ee_len;
2173 sbi->s_ext_extents++;
2174 if (ee_len < sbi->s_ext_min)
2175 sbi->s_ext_min = ee_len;
2176 if (ee_len > sbi->s_ext_max)
2177 sbi->s_ext_max = ee_len;
2178 if (ext_depth(inode) > sbi->s_depth_max)
2179 sbi->s_depth_max = ext_depth(inode);
2180 spin_unlock(&sbi->s_ext_stats_lock);
2181 }
2182#endif
2183 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002184 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002185 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002186 ext4_lblk_t num;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002187 ext4_fsblk_t start;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002188
Amit Aroraa2df2a62007-07-17 21:42:41 -04002189 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2190 start = ext_pblock(ex) + ee_len - num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002191 ext_debug("free last %u blocks starting %llu\n", num, start);
Theodore Ts'oe6362602009-11-23 07:17:05 -05002192 ext4_free_blocks(handle, inode, 0, start, num, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07002193 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002194 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002195 printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
Amit Aroraa2df2a62007-07-17 21:42:41 -04002196 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002197 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002198 printk(KERN_INFO "strange request: removal(2) "
2199 "%u-%u from %u:%u\n",
2200 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002201 }
2202 return 0;
2203}
2204
2205static int
2206ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002207 struct ext4_ext_path *path, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07002208{
2209 int err = 0, correct_index = 0;
2210 int depth = ext_depth(inode), credits;
2211 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002212 ext4_lblk_t a, b, block;
2213 unsigned num;
2214 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002215 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002216 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002217 struct ext4_extent *ex;
2218
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002219 /* the header must be checked already in ext4_ext_remove_space() */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002220 ext_debug("truncate since %u in leaf\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002221 if (!path[depth].p_hdr)
2222 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2223 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002224 if (unlikely(path[depth].p_hdr == NULL)) {
2225 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2226 return -EIO;
2227 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002228 /* find where to start removing */
2229 ex = EXT_LAST_EXTENT(eh);
2230
2231 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002232 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002233
2234 while (ex >= EXT_FIRST_EXTENT(eh) &&
2235 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002236
2237 if (ext4_ext_is_uninitialized(ex))
2238 uninitialized = 1;
2239 else
2240 uninitialized = 0;
2241
Mingming553f9002009-09-18 13:34:55 -04002242 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2243 uninitialized, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002244 path[depth].p_ext = ex;
2245
2246 a = ex_ee_block > start ? ex_ee_block : start;
2247 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
2248 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
2249
2250 ext_debug(" border %u:%u\n", a, b);
2251
2252 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
2253 block = 0;
2254 num = 0;
2255 BUG();
2256 } else if (a != ex_ee_block) {
2257 /* remove tail of the extent */
2258 block = ex_ee_block;
2259 num = a - block;
2260 } else if (b != ex_ee_block + ex_ee_len - 1) {
2261 /* remove head of the extent */
2262 block = a;
2263 num = b - a;
2264 /* there is no "make a hole" API yet */
2265 BUG();
2266 } else {
2267 /* remove whole extent: excellent! */
2268 block = ex_ee_block;
2269 num = 0;
2270 BUG_ON(a != ex_ee_block);
2271 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
2272 }
2273
Theodore Ts'o34071da2008-08-01 21:59:19 -04002274 /*
2275 * 3 for leaf, sb, and inode plus 2 (bmap and group
2276 * descriptor) for each block group; assume two block
2277 * groups plus ex_ee_len/blocks_per_block_group for
2278 * the worst case
2279 */
2280 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002281 if (ex == EXT_FIRST_EXTENT(eh)) {
2282 correct_index = 1;
2283 credits += (ext_depth(inode)) + 1;
2284 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002285 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002286
Jan Kara487caee2009-08-17 22:17:20 -04002287 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
Shen Feng9102e4f2008-07-11 19:27:31 -04002288 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002289 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002290
2291 err = ext4_ext_get_access(handle, inode, path + depth);
2292 if (err)
2293 goto out;
2294
2295 err = ext4_remove_blocks(handle, inode, ex, a, b);
2296 if (err)
2297 goto out;
2298
2299 if (num == 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002300 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002301 ext4_ext_store_pblock(ex, 0);
Marcin Slusarze8546d02008-04-17 10:38:59 -04002302 le16_add_cpu(&eh->eh_entries, -1);
Alex Tomasa86c6182006-10-11 01:21:03 -07002303 }
2304
2305 ex->ee_block = cpu_to_le32(block);
2306 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002307 /*
2308 * Do not mark uninitialized if all the blocks in the
2309 * extent have been removed.
2310 */
2311 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002312 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002313
2314 err = ext4_ext_dirty(handle, inode, path + depth);
2315 if (err)
2316 goto out;
2317
Mingming Cao2ae02102006-10-11 01:21:11 -07002318 ext_debug("new extent: %u:%u:%llu\n", block, num,
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002319 ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002320 ex--;
2321 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002322 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002323 }
2324
2325 if (correct_index && eh->eh_entries)
2326 err = ext4_ext_correct_indexes(handle, inode, path);
2327
2328 /* if this leaf is free, then we should
2329 * remove it from index block above */
2330 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2331 err = ext4_ext_rm_idx(handle, inode, path + depth);
2332
2333out:
2334 return err;
2335}
2336
2337/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002338 * ext4_ext_more_to_rm:
2339 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002340 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002341static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002342ext4_ext_more_to_rm(struct ext4_ext_path *path)
2343{
2344 BUG_ON(path->p_idx == NULL);
2345
2346 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2347 return 0;
2348
2349 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002350 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002351 * so we have to consider current index for truncation
2352 */
2353 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2354 return 0;
2355 return 1;
2356}
2357
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002358static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07002359{
2360 struct super_block *sb = inode->i_sb;
2361 int depth = ext_depth(inode);
2362 struct ext4_ext_path *path;
2363 handle_t *handle;
2364 int i = 0, err = 0;
2365
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002366 ext_debug("truncate since %u\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002367
2368 /* probably first extent we're gonna free will be last in block */
2369 handle = ext4_journal_start(inode, depth + 1);
2370 if (IS_ERR(handle))
2371 return PTR_ERR(handle);
2372
2373 ext4_ext_invalidate_cache(inode);
2374
2375 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002376 * We start scanning from right side, freeing all the blocks
2377 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002378 */
Josef Bacik216553c2008-04-29 22:02:02 -04002379 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07002380 if (path == NULL) {
2381 ext4_journal_stop(handle);
2382 return -ENOMEM;
2383 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002384 path[0].p_hdr = ext_inode_hdr(inode);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002385 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002386 err = -EIO;
2387 goto out;
2388 }
2389 path[0].p_depth = depth;
2390
2391 while (i >= 0 && err == 0) {
2392 if (i == depth) {
2393 /* this is leaf block */
2394 err = ext4_ext_rm_leaf(handle, inode, path, start);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002395 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002396 brelse(path[i].p_bh);
2397 path[i].p_bh = NULL;
2398 i--;
2399 continue;
2400 }
2401
2402 /* this is index block */
2403 if (!path[i].p_hdr) {
2404 ext_debug("initialize header\n");
2405 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002406 }
2407
Alex Tomasa86c6182006-10-11 01:21:03 -07002408 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002409 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002410 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2411 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2412 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2413 path[i].p_hdr,
2414 le16_to_cpu(path[i].p_hdr->eh_entries));
2415 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002416 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002417 path[i].p_idx--;
2418 }
2419
2420 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2421 i, EXT_FIRST_INDEX(path[i].p_hdr),
2422 path[i].p_idx);
2423 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002424 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002425 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002426 ext_debug("move to level %d (block %llu)\n",
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002427 i + 1, idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002428 memset(path + i + 1, 0, sizeof(*path));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002429 bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2430 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002431 /* should we reset i_size? */
2432 err = -EIO;
2433 break;
2434 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002435 if (WARN_ON(i + 1 > depth)) {
2436 err = -EIO;
2437 break;
2438 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002439 if (ext4_ext_check(inode, ext_block_hdr(bh),
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002440 depth - i - 1)) {
2441 err = -EIO;
2442 break;
2443 }
2444 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002445
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002446 /* save actual number of indexes since this
2447 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002448 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2449 i++;
2450 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002451 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002452 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002453 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002454 * handle must be already prepared by the
2455 * truncatei_leaf() */
2456 err = ext4_ext_rm_idx(handle, inode, path + i);
2457 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002458 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002459 brelse(path[i].p_bh);
2460 path[i].p_bh = NULL;
2461 i--;
2462 ext_debug("return to level %d\n", i);
2463 }
2464 }
2465
2466 /* TODO: flexible tree reduction should be here */
2467 if (path->p_hdr->eh_entries == 0) {
2468 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002469 * truncate to zero freed all the tree,
2470 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002471 */
2472 err = ext4_ext_get_access(handle, inode, path);
2473 if (err == 0) {
2474 ext_inode_hdr(inode)->eh_depth = 0;
2475 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04002476 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07002477 err = ext4_ext_dirty(handle, inode, path);
2478 }
2479 }
2480out:
Alex Tomasa86c6182006-10-11 01:21:03 -07002481 ext4_ext_drop_refs(path);
2482 kfree(path);
2483 ext4_journal_stop(handle);
2484
2485 return err;
2486}
2487
2488/*
2489 * called at mount time
2490 */
2491void ext4_ext_init(struct super_block *sb)
2492{
2493 /*
2494 * possible initialization would be here
2495 */
2496
Theodore Ts'o83982b62009-01-06 14:53:16 -05002497 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04002498#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o47760042008-09-08 23:00:52 -04002499 printk(KERN_INFO "EXT4-fs: file extents enabled");
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002500#ifdef AGGRESSIVE_TEST
2501 printk(", aggressive tests");
Alex Tomasa86c6182006-10-11 01:21:03 -07002502#endif
2503#ifdef CHECK_BINSEARCH
2504 printk(", check binsearch");
2505#endif
2506#ifdef EXTENTS_STATS
2507 printk(", stats");
2508#endif
2509 printk("\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04002510#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07002511#ifdef EXTENTS_STATS
2512 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2513 EXT4_SB(sb)->s_ext_min = 1 << 30;
2514 EXT4_SB(sb)->s_ext_max = 0;
2515#endif
2516 }
2517}
2518
2519/*
2520 * called at umount time
2521 */
2522void ext4_ext_release(struct super_block *sb)
2523{
Theodore Ts'o83982b62009-01-06 14:53:16 -05002524 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
Alex Tomasa86c6182006-10-11 01:21:03 -07002525 return;
2526
2527#ifdef EXTENTS_STATS
2528 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2529 struct ext4_sb_info *sbi = EXT4_SB(sb);
2530 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2531 sbi->s_ext_blocks, sbi->s_ext_extents,
2532 sbi->s_ext_blocks / sbi->s_ext_extents);
2533 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2534 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2535 }
2536#endif
2537}
2538
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002539static void bi_complete(struct bio *bio, int error)
2540{
2541 complete((struct completion *)bio->bi_private);
2542}
2543
2544/* FIXME!! we need to try to merge to left or right after zero-out */
2545static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2546{
Jing Zhangb7203032010-05-12 00:00:00 -04002547 int ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002548 struct bio *bio;
2549 int blkbits, blocksize;
2550 sector_t ee_pblock;
2551 struct completion event;
2552 unsigned int ee_len, len, done, offset;
2553
2554
2555 blkbits = inode->i_blkbits;
2556 blocksize = inode->i_sb->s_blocksize;
2557 ee_len = ext4_ext_get_actual_len(ex);
2558 ee_pblock = ext_pblock(ex);
2559
2560 /* convert ee_pblock to 512 byte sectors */
2561 ee_pblock = ee_pblock << (blkbits - 9);
2562
2563 while (ee_len > 0) {
2564
2565 if (ee_len > BIO_MAX_PAGES)
2566 len = BIO_MAX_PAGES;
2567 else
2568 len = ee_len;
2569
2570 bio = bio_alloc(GFP_NOIO, len);
Jing Zhangb7203032010-05-12 00:00:00 -04002571 if (!bio)
2572 return -ENOMEM;
2573
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002574 bio->bi_sector = ee_pblock;
2575 bio->bi_bdev = inode->i_sb->s_bdev;
2576
2577 done = 0;
2578 offset = 0;
2579 while (done < len) {
2580 ret = bio_add_page(bio, ZERO_PAGE(0),
2581 blocksize, offset);
2582 if (ret != blocksize) {
2583 /*
2584 * We can't add any more pages because of
2585 * hardware limitations. Start a new bio.
2586 */
2587 break;
2588 }
2589 done++;
2590 offset += blocksize;
2591 if (offset >= PAGE_CACHE_SIZE)
2592 offset = 0;
2593 }
2594
2595 init_completion(&event);
2596 bio->bi_private = &event;
2597 bio->bi_end_io = bi_complete;
2598 submit_bio(WRITE, bio);
2599 wait_for_completion(&event);
2600
Jing Zhangb7203032010-05-12 00:00:00 -04002601 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2602 bio_put(bio);
2603 return -EIO;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002604 }
2605 bio_put(bio);
2606 ee_len -= done;
2607 ee_pblock += done << (blkbits - 9);
2608 }
Jing Zhangb7203032010-05-12 00:00:00 -04002609 return 0;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002610}
2611
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002612#define EXT4_EXT_ZERO_LEN 7
Amit Arora56055d32007-07-17 21:42:38 -04002613/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002614 * This function is called by ext4_ext_map_blocks() if someone tries to write
Amit Arora56055d32007-07-17 21:42:38 -04002615 * to an uninitialized extent. It may result in splitting the uninitialized
2616 * extent into multiple extents (upto three - one initialized and two
2617 * uninitialized).
2618 * There are three possibilities:
2619 * a> There is no split required: Entire extent should be initialized
2620 * b> Splits in two extents: Write is happening at either end of the extent
2621 * c> Splits in three extents: Somone is writing in middle of the extent
2622 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002623static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002624 struct inode *inode,
2625 struct ext4_map_blocks *map,
2626 struct ext4_ext_path *path)
Amit Arora56055d32007-07-17 21:42:38 -04002627{
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002628 struct ext4_extent *ex, newex, orig_ex;
Amit Arora56055d32007-07-17 21:42:38 -04002629 struct ext4_extent *ex1 = NULL;
2630 struct ext4_extent *ex2 = NULL;
2631 struct ext4_extent *ex3 = NULL;
2632 struct ext4_extent_header *eh;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002633 ext4_lblk_t ee_block, eof_block;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002634 unsigned int allocated, ee_len, depth;
Amit Arora56055d32007-07-17 21:42:38 -04002635 ext4_fsblk_t newblock;
2636 int err = 0;
2637 int ret = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002638 int may_zeroout;
2639
2640 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
2641 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002642 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002643
2644 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
2645 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002646 if (eof_block < map->m_lblk + map->m_len)
2647 eof_block = map->m_lblk + map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04002648
2649 depth = ext_depth(inode);
2650 eh = path[depth].p_hdr;
2651 ex = path[depth].p_ext;
2652 ee_block = le32_to_cpu(ex->ee_block);
2653 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002654 allocated = ee_len - (map->m_lblk - ee_block);
2655 newblock = map->m_lblk - ee_block + ext_pblock(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002656
Amit Arora56055d32007-07-17 21:42:38 -04002657 ex2 = ex;
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002658 orig_ex.ee_block = ex->ee_block;
2659 orig_ex.ee_len = cpu_to_le16(ee_len);
2660 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
Amit Arora56055d32007-07-17 21:42:38 -04002661
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002662 /*
2663 * It is safe to convert extent to initialized via explicit
2664 * zeroout only if extent is fully insde i_size or new_size.
2665 */
2666 may_zeroout = ee_block + ee_len <= eof_block;
2667
Aneesh Kumar K.V9df56432008-02-22 06:17:31 -05002668 err = ext4_ext_get_access(handle, inode, path + depth);
2669 if (err)
2670 goto out;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002671 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002672 if (ee_len <= 2*EXT4_EXT_ZERO_LEN && may_zeroout) {
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002673 err = ext4_ext_zeroout(inode, &orig_ex);
2674 if (err)
2675 goto fix_extent_len;
2676 /* update the extent length and mark as initialized */
2677 ex->ee_block = orig_ex.ee_block;
2678 ex->ee_len = orig_ex.ee_len;
2679 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2680 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002681 /* zeroed the full extent */
2682 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002683 }
Aneesh Kumar K.V9df56432008-02-22 06:17:31 -05002684
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002685 /* ex1: ee_block to map->m_lblk - 1 : uninitialized */
2686 if (map->m_lblk > ee_block) {
Amit Arora56055d32007-07-17 21:42:38 -04002687 ex1 = ex;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002688 ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block);
Amit Arora56055d32007-07-17 21:42:38 -04002689 ext4_ext_mark_uninitialized(ex1);
2690 ex2 = &newex;
2691 }
2692 /*
2693 * for sanity, update the length of the ex2 extent before
2694 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2695 * overlap of blocks.
2696 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002697 if (!ex1 && allocated > map->m_len)
2698 ex2->ee_len = cpu_to_le16(map->m_len);
Amit Arora56055d32007-07-17 21:42:38 -04002699 /* ex3: to ee_block + ee_len : uninitialised */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002700 if (allocated > map->m_len) {
Amit Arora56055d32007-07-17 21:42:38 -04002701 unsigned int newdepth;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002702 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002703 if (allocated <= EXT4_EXT_ZERO_LEN && may_zeroout) {
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002704 /*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002705 * map->m_lblk == ee_block is handled by the zerouout
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002706 * at the beginning.
2707 * Mark first half uninitialized.
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002708 * Mark second half initialized and zero out the
2709 * initialized extent
2710 */
2711 ex->ee_block = orig_ex.ee_block;
2712 ex->ee_len = cpu_to_le16(ee_len - allocated);
2713 ext4_ext_mark_uninitialized(ex);
2714 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2715 ext4_ext_dirty(handle, inode, path + depth);
2716
2717 ex3 = &newex;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002718 ex3->ee_block = cpu_to_le32(map->m_lblk);
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002719 ext4_ext_store_pblock(ex3, newblock);
2720 ex3->ee_len = cpu_to_le16(allocated);
Mingming Cao00314622009-09-28 15:49:08 -04002721 err = ext4_ext_insert_extent(handle, inode, path,
2722 ex3, 0);
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002723 if (err == -ENOSPC) {
2724 err = ext4_ext_zeroout(inode, &orig_ex);
2725 if (err)
2726 goto fix_extent_len;
2727 ex->ee_block = orig_ex.ee_block;
2728 ex->ee_len = orig_ex.ee_len;
2729 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2730 ext4_ext_dirty(handle, inode, path + depth);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002731 /* blocks available from map->m_lblk */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002732 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002733
2734 } else if (err)
2735 goto fix_extent_len;
2736
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002737 /*
2738 * We need to zero out the second half because
2739 * an fallocate request can update file size and
2740 * converting the second half to initialized extent
2741 * implies that we can leak some junk data to user
2742 * space.
2743 */
2744 err = ext4_ext_zeroout(inode, ex3);
2745 if (err) {
2746 /*
2747 * We should actually mark the
2748 * second half as uninit and return error
2749 * Insert would have changed the extent
2750 */
2751 depth = ext_depth(inode);
2752 ext4_ext_drop_refs(path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002753 path = ext4_ext_find_extent(inode, map->m_lblk,
2754 path);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002755 if (IS_ERR(path)) {
2756 err = PTR_ERR(path);
2757 return err;
2758 }
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002759 /* get the second half extent details */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002760 ex = path[depth].p_ext;
2761 err = ext4_ext_get_access(handle, inode,
2762 path + depth);
2763 if (err)
2764 return err;
2765 ext4_ext_mark_uninitialized(ex);
2766 ext4_ext_dirty(handle, inode, path + depth);
2767 return err;
2768 }
2769
2770 /* zeroed the second half */
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002771 return allocated;
2772 }
Amit Arora56055d32007-07-17 21:42:38 -04002773 ex3 = &newex;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002774 ex3->ee_block = cpu_to_le32(map->m_lblk + map->m_len);
2775 ext4_ext_store_pblock(ex3, newblock + map->m_len);
2776 ex3->ee_len = cpu_to_le16(allocated - map->m_len);
Amit Arora56055d32007-07-17 21:42:38 -04002777 ext4_ext_mark_uninitialized(ex3);
Mingming Cao00314622009-09-28 15:49:08 -04002778 err = ext4_ext_insert_extent(handle, inode, path, ex3, 0);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002779 if (err == -ENOSPC && may_zeroout) {
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002780 err = ext4_ext_zeroout(inode, &orig_ex);
2781 if (err)
2782 goto fix_extent_len;
2783 /* update the extent length and mark as initialized */
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002784 ex->ee_block = orig_ex.ee_block;
2785 ex->ee_len = orig_ex.ee_len;
2786 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002787 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002788 /* zeroed the full extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002789 /* blocks available from map->m_lblk */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002790 return allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002791
2792 } else if (err)
2793 goto fix_extent_len;
Amit Arora56055d32007-07-17 21:42:38 -04002794 /*
2795 * The depth, and hence eh & ex might change
2796 * as part of the insert above.
2797 */
2798 newdepth = ext_depth(inode);
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002799 /*
Coly Li73ac36e2009-01-07 18:09:16 -08002800 * update the extent length after successful insert of the
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002801 * split extent
2802 */
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002803 ee_len -= ext4_ext_get_actual_len(ex3);
2804 orig_ex.ee_len = cpu_to_le16(ee_len);
2805 may_zeroout = ee_block + ee_len <= eof_block;
2806
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002807 depth = newdepth;
2808 ext4_ext_drop_refs(path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002809 path = ext4_ext_find_extent(inode, map->m_lblk, path);
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002810 if (IS_ERR(path)) {
2811 err = PTR_ERR(path);
2812 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04002813 }
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002814 eh = path[depth].p_hdr;
2815 ex = path[depth].p_ext;
2816 if (ex2 != &newex)
2817 ex2 = ex;
2818
2819 err = ext4_ext_get_access(handle, inode, path + depth);
2820 if (err)
2821 goto out;
2822
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002823 allocated = map->m_len;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002824
2825 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2826 * to insert a extent in the middle zerout directly
2827 * otherwise give the extent a chance to merge to left
2828 */
2829 if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002830 map->m_lblk != ee_block && may_zeroout) {
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002831 err = ext4_ext_zeroout(inode, &orig_ex);
2832 if (err)
2833 goto fix_extent_len;
2834 /* update the extent length and mark as initialized */
2835 ex->ee_block = orig_ex.ee_block;
2836 ex->ee_len = orig_ex.ee_len;
2837 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2838 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002839 /* zero out the first half */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002840 /* blocks available from map->m_lblk */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002841 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002842 }
Amit Arora56055d32007-07-17 21:42:38 -04002843 }
2844 /*
2845 * If there was a change of depth as part of the
2846 * insertion of ex3 above, we need to update the length
2847 * of the ex1 extent again here
2848 */
2849 if (ex1 && ex1 != ex) {
2850 ex1 = ex;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002851 ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block);
Amit Arora56055d32007-07-17 21:42:38 -04002852 ext4_ext_mark_uninitialized(ex1);
2853 ex2 = &newex;
2854 }
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002855 /* ex2: map->m_lblk to map->m_lblk + maxblocks-1 : initialised */
2856 ex2->ee_block = cpu_to_le32(map->m_lblk);
Amit Arora56055d32007-07-17 21:42:38 -04002857 ext4_ext_store_pblock(ex2, newblock);
2858 ex2->ee_len = cpu_to_le16(allocated);
2859 if (ex2 != ex)
2860 goto insert;
Amit Arora56055d32007-07-17 21:42:38 -04002861 /*
2862 * New (initialized) extent starts from the first block
2863 * in the current extent. i.e., ex2 == ex
2864 * We have to see if it can be merged with the extent
2865 * on the left.
2866 */
2867 if (ex2 > EXT_FIRST_EXTENT(eh)) {
2868 /*
2869 * To merge left, pass "ex2 - 1" to try_to_merge(),
2870 * since it merges towards right _only_.
2871 */
2872 ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
2873 if (ret) {
2874 err = ext4_ext_correct_indexes(handle, inode, path);
2875 if (err)
2876 goto out;
2877 depth = ext_depth(inode);
2878 ex2--;
2879 }
2880 }
2881 /*
2882 * Try to Merge towards right. This might be required
2883 * only when the whole extent is being written to.
2884 * i.e. ex2 == ex and ex3 == NULL.
2885 */
2886 if (!ex3) {
2887 ret = ext4_ext_try_to_merge(inode, path, ex2);
2888 if (ret) {
2889 err = ext4_ext_correct_indexes(handle, inode, path);
2890 if (err)
2891 goto out;
2892 }
2893 }
2894 /* Mark modified extent as dirty */
2895 err = ext4_ext_dirty(handle, inode, path + depth);
2896 goto out;
2897insert:
Mingming Cao00314622009-09-28 15:49:08 -04002898 err = ext4_ext_insert_extent(handle, inode, path, &newex, 0);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002899 if (err == -ENOSPC && may_zeroout) {
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002900 err = ext4_ext_zeroout(inode, &orig_ex);
2901 if (err)
2902 goto fix_extent_len;
2903 /* update the extent length and mark as initialized */
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002904 ex->ee_block = orig_ex.ee_block;
2905 ex->ee_len = orig_ex.ee_len;
2906 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002907 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002908 /* zero out the first half */
2909 return allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002910 } else if (err)
2911 goto fix_extent_len;
Amit Arora56055d32007-07-17 21:42:38 -04002912out:
Mingming553f9002009-09-18 13:34:55 -04002913 ext4_ext_show_leaf(inode, path);
Amit Arora56055d32007-07-17 21:42:38 -04002914 return err ? err : allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002915
2916fix_extent_len:
2917 ex->ee_block = orig_ex.ee_block;
2918 ex->ee_len = orig_ex.ee_len;
2919 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2920 ext4_ext_mark_uninitialized(ex);
2921 ext4_ext_dirty(handle, inode, path + depth);
2922 return err;
Amit Arora56055d32007-07-17 21:42:38 -04002923}
2924
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05002925/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002926 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04002927 * ext4_get_blocks_dio_write() when DIO to write
2928 * to an uninitialized extent.
2929 *
2930 * Writing to an uninitized extent may result in splitting the uninitialized
2931 * extent into multiple /intialized unintialized extents (up to three)
2932 * There are three possibilities:
2933 * a> There is no split required: Entire extent should be uninitialized
2934 * b> Splits in two extents: Write is happening at either end of the extent
2935 * c> Splits in three extents: Somone is writing in middle of the extent
2936 *
2937 * One of more index blocks maybe needed if the extent tree grow after
2938 * the unintialized extent split. To prevent ENOSPC occur at the IO
2939 * complete, we need to split the uninitialized extent before DIO submit
2940 * the IO. The uninitilized extent called at this time will be split
2941 * into three uninitialized extent(at most). After IO complete, the part
2942 * being filled will be convert to initialized by the end_io callback function
2943 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05002944 *
2945 * Returns the size of uninitialized extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04002946 */
2947static int ext4_split_unwritten_extents(handle_t *handle,
2948 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002949 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04002950 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04002951 int flags)
2952{
2953 struct ext4_extent *ex, newex, orig_ex;
2954 struct ext4_extent *ex1 = NULL;
2955 struct ext4_extent *ex2 = NULL;
2956 struct ext4_extent *ex3 = NULL;
2957 struct ext4_extent_header *eh;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002958 ext4_lblk_t ee_block, eof_block;
Mingming Cao00314622009-09-28 15:49:08 -04002959 unsigned int allocated, ee_len, depth;
2960 ext4_fsblk_t newblock;
2961 int err = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002962 int may_zeroout;
Mingming Cao00314622009-09-28 15:49:08 -04002963
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002964 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
2965 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002966 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002967
2968 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
2969 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002970 if (eof_block < map->m_lblk + map->m_len)
2971 eof_block = map->m_lblk + map->m_len;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002972
Mingming Cao00314622009-09-28 15:49:08 -04002973 depth = ext_depth(inode);
2974 eh = path[depth].p_hdr;
2975 ex = path[depth].p_ext;
2976 ee_block = le32_to_cpu(ex->ee_block);
2977 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002978 allocated = ee_len - (map->m_lblk - ee_block);
2979 newblock = map->m_lblk - ee_block + ext_pblock(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002980
Mingming Cao00314622009-09-28 15:49:08 -04002981 ex2 = ex;
2982 orig_ex.ee_block = ex->ee_block;
2983 orig_ex.ee_len = cpu_to_le16(ee_len);
2984 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
2985
2986 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002987 * It is safe to convert extent to initialized via explicit
2988 * zeroout only if extent is fully insde i_size or new_size.
2989 */
2990 may_zeroout = ee_block + ee_len <= eof_block;
2991
2992 /*
Mingmingba230c32009-11-06 04:01:23 -05002993 * If the uninitialized extent begins at the same logical
2994 * block where the write begins, and the write completely
2995 * covers the extent, then we don't need to split it.
Mingming Cao00314622009-09-28 15:49:08 -04002996 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002997 if ((map->m_lblk == ee_block) && (allocated <= map->m_len))
Mingmingba230c32009-11-06 04:01:23 -05002998 return allocated;
Mingming Cao00314622009-09-28 15:49:08 -04002999
3000 err = ext4_ext_get_access(handle, inode, path + depth);
3001 if (err)
3002 goto out;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003003 /* ex1: ee_block to map->m_lblk - 1 : uninitialized */
3004 if (map->m_lblk > ee_block) {
Mingming Cao00314622009-09-28 15:49:08 -04003005 ex1 = ex;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003006 ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block);
Mingming Cao00314622009-09-28 15:49:08 -04003007 ext4_ext_mark_uninitialized(ex1);
3008 ex2 = &newex;
3009 }
3010 /*
3011 * for sanity, update the length of the ex2 extent before
3012 * we insert ex3, if ex1 is NULL. This is to avoid temporary
3013 * overlap of blocks.
3014 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003015 if (!ex1 && allocated > map->m_len)
3016 ex2->ee_len = cpu_to_le16(map->m_len);
Mingming Cao00314622009-09-28 15:49:08 -04003017 /* ex3: to ee_block + ee_len : uninitialised */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003018 if (allocated > map->m_len) {
Mingming Cao00314622009-09-28 15:49:08 -04003019 unsigned int newdepth;
3020 ex3 = &newex;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003021 ex3->ee_block = cpu_to_le32(map->m_lblk + map->m_len);
3022 ext4_ext_store_pblock(ex3, newblock + map->m_len);
3023 ex3->ee_len = cpu_to_le16(allocated - map->m_len);
Mingming Cao00314622009-09-28 15:49:08 -04003024 ext4_ext_mark_uninitialized(ex3);
3025 err = ext4_ext_insert_extent(handle, inode, path, ex3, flags);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003026 if (err == -ENOSPC && may_zeroout) {
Mingming Cao00314622009-09-28 15:49:08 -04003027 err = ext4_ext_zeroout(inode, &orig_ex);
3028 if (err)
3029 goto fix_extent_len;
3030 /* update the extent length and mark as initialized */
3031 ex->ee_block = orig_ex.ee_block;
3032 ex->ee_len = orig_ex.ee_len;
3033 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
3034 ext4_ext_dirty(handle, inode, path + depth);
3035 /* zeroed the full extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003036 /* blocks available from map->m_lblk */
Mingming Cao00314622009-09-28 15:49:08 -04003037 return allocated;
3038
3039 } else if (err)
3040 goto fix_extent_len;
3041 /*
3042 * The depth, and hence eh & ex might change
3043 * as part of the insert above.
3044 */
3045 newdepth = ext_depth(inode);
3046 /*
3047 * update the extent length after successful insert of the
3048 * split extent
3049 */
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003050 ee_len -= ext4_ext_get_actual_len(ex3);
3051 orig_ex.ee_len = cpu_to_le16(ee_len);
3052 may_zeroout = ee_block + ee_len <= eof_block;
3053
Mingming Cao00314622009-09-28 15:49:08 -04003054 depth = newdepth;
3055 ext4_ext_drop_refs(path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003056 path = ext4_ext_find_extent(inode, map->m_lblk, path);
Mingming Cao00314622009-09-28 15:49:08 -04003057 if (IS_ERR(path)) {
3058 err = PTR_ERR(path);
3059 goto out;
3060 }
3061 eh = path[depth].p_hdr;
3062 ex = path[depth].p_ext;
3063 if (ex2 != &newex)
3064 ex2 = ex;
3065
3066 err = ext4_ext_get_access(handle, inode, path + depth);
3067 if (err)
3068 goto out;
3069
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003070 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003071 }
3072 /*
3073 * If there was a change of depth as part of the
3074 * insertion of ex3 above, we need to update the length
3075 * of the ex1 extent again here
3076 */
3077 if (ex1 && ex1 != ex) {
3078 ex1 = ex;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003079 ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block);
Mingming Cao00314622009-09-28 15:49:08 -04003080 ext4_ext_mark_uninitialized(ex1);
3081 ex2 = &newex;
3082 }
3083 /*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003084 * ex2: map->m_lblk to map->m_lblk + map->m_len-1 : to be written
3085 * using direct I/O, uninitialised still.
Mingming Cao00314622009-09-28 15:49:08 -04003086 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003087 ex2->ee_block = cpu_to_le32(map->m_lblk);
Mingming Cao00314622009-09-28 15:49:08 -04003088 ext4_ext_store_pblock(ex2, newblock);
3089 ex2->ee_len = cpu_to_le16(allocated);
3090 ext4_ext_mark_uninitialized(ex2);
3091 if (ex2 != ex)
3092 goto insert;
3093 /* Mark modified extent as dirty */
3094 err = ext4_ext_dirty(handle, inode, path + depth);
3095 ext_debug("out here\n");
3096 goto out;
3097insert:
3098 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003099 if (err == -ENOSPC && may_zeroout) {
Mingming Cao00314622009-09-28 15:49:08 -04003100 err = ext4_ext_zeroout(inode, &orig_ex);
3101 if (err)
3102 goto fix_extent_len;
3103 /* update the extent length and mark as initialized */
3104 ex->ee_block = orig_ex.ee_block;
3105 ex->ee_len = orig_ex.ee_len;
3106 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
3107 ext4_ext_dirty(handle, inode, path + depth);
3108 /* zero out the first half */
3109 return allocated;
3110 } else if (err)
3111 goto fix_extent_len;
3112out:
3113 ext4_ext_show_leaf(inode, path);
3114 return err ? err : allocated;
3115
3116fix_extent_len:
3117 ex->ee_block = orig_ex.ee_block;
3118 ex->ee_len = orig_ex.ee_len;
3119 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
3120 ext4_ext_mark_uninitialized(ex);
3121 ext4_ext_dirty(handle, inode, path + depth);
3122 return err;
3123}
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003124static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003125 struct inode *inode,
3126 struct ext4_ext_path *path)
3127{
3128 struct ext4_extent *ex;
3129 struct ext4_extent_header *eh;
3130 int depth;
3131 int err = 0;
3132 int ret = 0;
3133
3134 depth = ext_depth(inode);
3135 eh = path[depth].p_hdr;
3136 ex = path[depth].p_ext;
3137
3138 err = ext4_ext_get_access(handle, inode, path + depth);
3139 if (err)
3140 goto out;
3141 /* first mark the extent as initialized */
3142 ext4_ext_mark_initialized(ex);
3143
3144 /*
3145 * We have to see if it can be merged with the extent
3146 * on the left.
3147 */
3148 if (ex > EXT_FIRST_EXTENT(eh)) {
3149 /*
3150 * To merge left, pass "ex - 1" to try_to_merge(),
3151 * since it merges towards right _only_.
3152 */
3153 ret = ext4_ext_try_to_merge(inode, path, ex - 1);
3154 if (ret) {
3155 err = ext4_ext_correct_indexes(handle, inode, path);
3156 if (err)
3157 goto out;
3158 depth = ext_depth(inode);
3159 ex--;
3160 }
3161 }
3162 /*
3163 * Try to Merge towards right.
3164 */
3165 ret = ext4_ext_try_to_merge(inode, path, ex);
3166 if (ret) {
3167 err = ext4_ext_correct_indexes(handle, inode, path);
3168 if (err)
3169 goto out;
3170 depth = ext_depth(inode);
3171 }
3172 /* Mark modified extent as dirty */
3173 err = ext4_ext_dirty(handle, inode, path + depth);
3174out:
3175 ext4_ext_show_leaf(inode, path);
3176 return err;
3177}
3178
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003179static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3180 sector_t block, int count)
3181{
3182 int i;
3183 for (i = 0; i < count; i++)
3184 unmap_underlying_metadata(bdev, block + i);
3185}
3186
Mingming Cao00314622009-09-28 15:49:08 -04003187static int
3188ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003189 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003190 struct ext4_ext_path *path, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003191 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003192{
3193 int ret = 0;
3194 int err = 0;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003195 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Mingming Cao00314622009-09-28 15:49:08 -04003196
3197 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3198 "block %llu, max_blocks %u, flags %d, allocated %u",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003199 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04003200 flags, allocated);
3201 ext4_ext_show_leaf(inode, path);
3202
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003203 /* get_block() before submit the IO, split the extent */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003204 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003205 ret = ext4_split_unwritten_extents(handle, inode, map,
3206 path, flags);
Mingming5f524952009-11-10 10:48:04 -05003207 /*
3208 * Flag the inode(non aio case) or end_io struct (aio case)
3209 * that this IO needs to convertion to written when IO is
3210 * completed
3211 */
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003212 if (io)
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003213 io->flag = EXT4_IO_UNWRITTEN;
Mingming5f524952009-11-10 10:48:04 -05003214 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003215 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
Jiaying Zhang744692d2010-03-04 16:14:02 -05003216 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003217 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao00314622009-09-28 15:49:08 -04003218 goto out;
3219 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003220 /* IO end_io complete, convert the filled extent to written */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003221 if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003222 ret = ext4_convert_unwritten_extents_endio(handle, inode,
Mingming Cao00314622009-09-28 15:49:08 -04003223 path);
Jan Karab436b9b2009-12-08 23:51:10 -05003224 if (ret >= 0)
3225 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04003226 goto out2;
3227 }
3228 /* buffered IO case */
3229 /*
3230 * repeat fallocate creation request
3231 * we already have an unwritten extent
3232 */
3233 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3234 goto map_out;
3235
3236 /* buffered READ or buffered write_begin() lookup */
3237 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3238 /*
3239 * We have blocks reserved already. We
3240 * return allocated blocks so that delalloc
3241 * won't do block reservation for us. But
3242 * the buffer head will be unmapped so that
3243 * a read from the block returns 0s.
3244 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003245 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003246 goto out1;
3247 }
3248
3249 /* buffered write, writepage time, convert*/
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003250 ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
Jan Karab436b9b2009-12-08 23:51:10 -05003251 if (ret >= 0)
3252 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04003253out:
3254 if (ret <= 0) {
3255 err = ret;
3256 goto out2;
3257 } else
3258 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003259 map->m_flags |= EXT4_MAP_NEW;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003260 /*
3261 * if we allocated more blocks than requested
3262 * we need to make sure we unmap the extra block
3263 * allocated. The actual needed block will get
3264 * unmapped later when we find the buffer_head marked
3265 * new.
3266 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003267 if (allocated > map->m_len) {
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003268 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003269 newblock + map->m_len,
3270 allocated - map->m_len);
3271 allocated = map->m_len;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003272 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003273
3274 /*
3275 * If we have done fallocate with the offset that is already
3276 * delayed allocated, we would have block reservation
3277 * and quota reservation done in the delayed write path.
3278 * But fallocate would have already updated quota and block
3279 * count for this offset. So cancel these reservation
3280 */
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05003281 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003282 ext4_da_update_reserve_space(inode, allocated, 0);
3283
Mingming Cao00314622009-09-28 15:49:08 -04003284map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003285 map->m_flags |= EXT4_MAP_MAPPED;
Mingming Cao00314622009-09-28 15:49:08 -04003286out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003287 if (allocated > map->m_len)
3288 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003289 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003290 map->m_pblk = newblock;
3291 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04003292out2:
3293 if (path) {
3294 ext4_ext_drop_refs(path);
3295 kfree(path);
3296 }
3297 return err ? err : allocated;
3298}
3299/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05003300 * Block allocation/map/preallocation routine for extents based files
3301 *
3302 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003303 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003304 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3305 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05003306 *
3307 * return > 0, number of of blocks already mapped/allocated
3308 * if create == 0 and these are pre-allocated blocks
3309 * buffer head is unmapped
3310 * otherwise blocks are mapped
3311 *
3312 * return = 0, if plain look up failed (blocks have not been allocated)
3313 * buffer head is unmapped
3314 *
3315 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003316 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003317int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3318 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07003319{
3320 struct ext4_ext_path *path = NULL;
Amit Arora56055d32007-07-17 21:42:38 -04003321 struct ext4_extent_header *eh;
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003322 struct ext4_extent newex, *ex, *last_ex;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003323 ext4_fsblk_t newblock;
3324 int err = 0, depth, ret, cache_type;
3325 unsigned int allocated = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003326 struct ext4_allocation_request ar;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003327 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Alex Tomasa86c6182006-10-11 01:21:03 -07003328
Mingming84fe3be2009-09-01 08:44:37 -04003329 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003330 map->m_lblk, map->m_len, inode->i_ino);
Alex Tomasa86c6182006-10-11 01:21:03 -07003331
3332 /* check in cache */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003333 cache_type = ext4_ext_in_cache(inode, map->m_lblk, &newex);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003334 if (cache_type) {
3335 if (cache_type == EXT4_EXT_CACHE_GAP) {
Theodore Ts'oc2177052009-05-14 00:58:52 -04003336 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003337 /*
3338 * block isn't allocated yet and
3339 * user doesn't want to allocate it
3340 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003341 goto out2;
3342 }
3343 /* we should allocate requested block */
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003344 } else if (cache_type == EXT4_EXT_CACHE_EXTENT) {
Alex Tomasa86c6182006-10-11 01:21:03 -07003345 /* block is already allocated */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003346 newblock = map->m_lblk
Dave Kleikamp8c55e202007-05-24 13:04:54 -04003347 - le32_to_cpu(newex.ee_block)
3348 + ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003349 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003350 allocated = ext4_ext_get_actual_len(&newex) -
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003351 (map->m_lblk - le32_to_cpu(newex.ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -07003352 goto out;
3353 } else {
3354 BUG();
3355 }
3356 }
3357
3358 /* find extent for this block */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003359 path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07003360 if (IS_ERR(path)) {
3361 err = PTR_ERR(path);
3362 path = NULL;
3363 goto out2;
3364 }
3365
3366 depth = ext_depth(inode);
3367
3368 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003369 * consistent leaf must not be empty;
3370 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07003371 * this is why assert can't be put in ext4_ext_find_extent()
3372 */
Frank Mayhar273df552010-03-02 11:46:09 -05003373 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
3374 EXT4_ERROR_INODE(inode, "bad extent address "
3375 "iblock: %d, depth: %d pblock %lld",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003376 map->m_lblk, depth, path[depth].p_block);
Surbhi Palande034fb4c2009-12-14 09:53:52 -05003377 err = -EIO;
3378 goto out2;
3379 }
Amit Arora56055d32007-07-17 21:42:38 -04003380 eh = path[depth].p_hdr;
Alex Tomasa86c6182006-10-11 01:21:03 -07003381
Avantika Mathur7e028972006-12-06 20:41:33 -08003382 ex = path[depth].p_ext;
3383 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003384 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07003385 ext4_fsblk_t ee_start = ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003386 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003387
3388 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003389 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04003390 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003391 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003392 ee_len = ext4_ext_get_actual_len(ex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003393 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003394 if (in_range(map->m_lblk, ee_block, ee_len)) {
3395 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003396 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003397 allocated = ee_len - (map->m_lblk - ee_block);
3398 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
3399 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04003400
Amit Aroraa2df2a62007-07-17 21:42:41 -04003401 /* Do not put uninitialized extent in the cache */
Amit Arora56055d32007-07-17 21:42:38 -04003402 if (!ext4_ext_is_uninitialized(ex)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04003403 ext4_ext_put_in_cache(inode, ee_block,
3404 ee_len, ee_start,
3405 EXT4_EXT_CACHE_EXTENT);
Amit Arora56055d32007-07-17 21:42:38 -04003406 goto out;
3407 }
Mingming Cao00314622009-09-28 15:49:08 -04003408 ret = ext4_ext_handle_uninitialized_extents(handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003409 inode, map, path, flags, allocated,
3410 newblock);
Mingming Cao00314622009-09-28 15:49:08 -04003411 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07003412 }
3413 }
3414
3415 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003416 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07003417 * we couldn't try to create block if create flag is zero
3418 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04003419 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003420 /*
3421 * put just found gap into cache to speed up
3422 * subsequent requests
3423 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003424 ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
Alex Tomasa86c6182006-10-11 01:21:03 -07003425 goto out2;
3426 }
3427 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003428 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07003429 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003430
Alex Tomasc9de5602008-01-29 00:19:52 -05003431 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003432 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003433 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3434 if (err)
3435 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003436 ar.lright = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003437 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
3438 if (err)
3439 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04003440
Amit Arora749269f2007-07-18 09:02:56 -04003441 /*
3442 * See if request is beyond maximum number of blocks we can have in
3443 * a single extent. For an initialized extent this limit is
3444 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3445 * EXT_UNINIT_MAX_LEN.
3446 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003447 if (map->m_len > EXT_INIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003448 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003449 map->m_len = EXT_INIT_MAX_LEN;
3450 else if (map->m_len > EXT_UNINIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003451 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003452 map->m_len = EXT_UNINIT_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04003453
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003454 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
3455 newex.ee_block = cpu_to_le32(map->m_lblk);
3456 newex.ee_len = cpu_to_le16(map->m_len);
Amit Arora25d14f92007-05-24 13:04:13 -04003457 err = ext4_ext_check_overlap(inode, &newex, path);
3458 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003459 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04003460 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003461 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003462
3463 /* allocate new block */
3464 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003465 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
3466 ar.logical = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003467 ar.len = allocated;
3468 if (S_ISREG(inode->i_mode))
3469 ar.flags = EXT4_MB_HINT_DATA;
3470 else
3471 /* disable in-core preallocation for non-regular files */
3472 ar.flags = 0;
3473 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07003474 if (!newblock)
3475 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04003476 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003477 ar.goal, newblock, allocated);
Alex Tomasa86c6182006-10-11 01:21:03 -07003478
3479 /* try to insert new extent into found leaf and return */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07003480 ext4_ext_store_pblock(&newex, newblock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003481 newex.ee_len = cpu_to_le16(ar.len);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003482 /* Mark uninitialized */
3483 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
Amit Aroraa2df2a62007-07-17 21:42:41 -04003484 ext4_ext_mark_uninitialized(&newex);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003485 /*
Jiaying Zhang744692d2010-03-04 16:14:02 -05003486 * io_end structure was created for every IO write to an
3487 * uninitialized extent. To avoid unecessary conversion,
3488 * here we flag the IO that really needs the conversion.
Mingming5f524952009-11-10 10:48:04 -05003489 * For non asycn direct IO case, flag the inode state
3490 * that we need to perform convertion when IO is done.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003491 */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003492 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Mingming5f524952009-11-10 10:48:04 -05003493 if (io)
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003494 io->flag = EXT4_IO_UNWRITTEN;
Mingming5f524952009-11-10 10:48:04 -05003495 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003496 ext4_set_inode_state(inode,
3497 EXT4_STATE_DIO_UNWRITTEN);
Mingming5f524952009-11-10 10:48:04 -05003498 }
Jiaying Zhang744692d2010-03-04 16:14:02 -05003499 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003500 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003501 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003502
3503 if (unlikely(EXT4_I(inode)->i_flags & EXT4_EOFBLOCKS_FL)) {
Frank Mayhar273df552010-03-02 11:46:09 -05003504 if (unlikely(!eh->eh_entries)) {
3505 EXT4_ERROR_INODE(inode,
3506 "eh->eh_entries == 0 ee_block %d",
3507 ex->ee_block);
3508 err = -EIO;
3509 goto out2;
3510 }
3511 last_ex = EXT_LAST_EXTENT(eh);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003512 if (map->m_lblk + ar.len > le32_to_cpu(last_ex->ee_block)
Frank Mayhar273df552010-03-02 11:46:09 -05003513 + ext4_ext_get_actual_len(last_ex))
3514 EXT4_I(inode)->i_flags &= ~EXT4_EOFBLOCKS_FL;
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003515 }
Mingming Cao00314622009-09-28 15:49:08 -04003516 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
Alex Tomas315054f2007-05-24 13:04:25 -04003517 if (err) {
3518 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05003519 /* not a good idea to call discard here directly,
3520 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003521 ext4_discard_preallocations(inode);
Theodore Ts'oe6362602009-11-23 07:17:05 -05003522 ext4_free_blocks(handle, inode, 0, ext_pblock(&newex),
3523 ext4_ext_get_actual_len(&newex), 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07003524 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04003525 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003526
Alex Tomasa86c6182006-10-11 01:21:03 -07003527 /* previous routine could use block we allocated */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07003528 newblock = ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003529 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003530 if (allocated > map->m_len)
3531 allocated = map->m_len;
3532 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07003533
Jan Karab436b9b2009-12-08 23:51:10 -05003534 /*
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003535 * Update reserved blocks/metadata blocks after successful
3536 * block allocation which had been deferred till now.
3537 */
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05003538 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003539 ext4_da_update_reserve_space(inode, allocated, 1);
3540
3541 /*
Jan Karab436b9b2009-12-08 23:51:10 -05003542 * Cache the extent and update transaction to commit on fdatasync only
3543 * when it is _not_ an uninitialized extent.
3544 */
3545 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003546 ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock,
Amit Aroraa2df2a62007-07-17 21:42:41 -04003547 EXT4_EXT_CACHE_EXTENT);
Jan Karab436b9b2009-12-08 23:51:10 -05003548 ext4_update_inode_fsync_trans(handle, inode, 1);
3549 } else
3550 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07003551out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003552 if (allocated > map->m_len)
3553 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07003554 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003555 map->m_flags |= EXT4_MAP_MAPPED;
3556 map->m_pblk = newblock;
3557 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07003558out2:
3559 if (path) {
3560 ext4_ext_drop_refs(path);
3561 kfree(path);
3562 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003563 return err ? err : allocated;
3564}
3565
Jan Karacf108bc2008-07-11 19:27:31 -04003566void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07003567{
3568 struct address_space *mapping = inode->i_mapping;
3569 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003570 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07003571 handle_t *handle;
3572 int err = 0;
3573
3574 /*
3575 * probably first extent we're gonna free will be last in block
3576 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04003577 err = ext4_writepage_trans_blocks(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07003578 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04003579 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07003580 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07003581
Jan Karacf108bc2008-07-11 19:27:31 -04003582 if (inode->i_size & (sb->s_blocksize - 1))
3583 ext4_block_truncate_page(handle, mapping, inode->i_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07003584
Jan Kara9ddfc3d2008-07-11 19:27:31 -04003585 if (ext4_orphan_add(handle, inode))
3586 goto out_stop;
3587
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003588 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07003589 ext4_ext_invalidate_cache(inode);
3590
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003591 ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003592
Alex Tomasa86c6182006-10-11 01:21:03 -07003593 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003594 * TODO: optimization is possible here.
3595 * Probably we need not scan at all,
3596 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07003597 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003598
3599 /* we have to know where to truncate from in crash case */
3600 EXT4_I(inode)->i_disksize = inode->i_size;
3601 ext4_mark_inode_dirty(handle, inode);
3602
3603 last_block = (inode->i_size + sb->s_blocksize - 1)
3604 >> EXT4_BLOCK_SIZE_BITS(sb);
3605 err = ext4_ext_remove_space(inode, last_block);
3606
3607 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04003608 * transaction synchronous.
3609 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003610 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05003611 ext4_handle_sync(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07003612
3613out_stop:
Jan Kara9ddfc3d2008-07-11 19:27:31 -04003614 up_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07003615 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003616 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07003617 * then we need to clear up the orphan record which we created above.
3618 * However, if this was a real unlink then we were called by
3619 * ext4_delete_inode(), and we allow that function to clean up the
3620 * orphan info for us.
3621 */
3622 if (inode->i_nlink)
3623 ext4_orphan_del(handle, inode);
3624
Solofo Ramangalahyef737722008-04-29 22:00:41 -04003625 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3626 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07003627 ext4_journal_stop(handle);
3628}
3629
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003630static void ext4_falloc_update_inode(struct inode *inode,
3631 int mode, loff_t new_size, int update_ctime)
3632{
3633 struct timespec now;
3634
3635 if (update_ctime) {
3636 now = current_fs_time(inode->i_sb);
3637 if (!timespec_equal(&inode->i_ctime, &now))
3638 inode->i_ctime = now;
3639 }
3640 /*
3641 * Update only when preallocation was requested beyond
3642 * the file size.
3643 */
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003644 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3645 if (new_size > i_size_read(inode))
3646 i_size_write(inode, new_size);
3647 if (new_size > EXT4_I(inode)->i_disksize)
3648 ext4_update_i_disksize(inode, new_size);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003649 } else {
3650 /*
3651 * Mark that we allocate beyond EOF so the subsequent truncate
3652 * can proceed even if the new size is the same as i_size.
3653 */
3654 if (new_size > i_size_read(inode))
3655 EXT4_I(inode)->i_flags |= EXT4_EOFBLOCKS_FL;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003656 }
3657
3658}
3659
Amit Aroraa2df2a62007-07-17 21:42:41 -04003660/*
3661 * preallocate space for a file. This implements ext4's fallocate inode
3662 * operation, which gets called from sys_fallocate system call.
3663 * For block-mapped files, posix_fallocate should fall back to the method
3664 * of writing zeroes to the required new blocks (the same behavior which is
3665 * expected for file systems which do not support fallocate() system call).
3666 */
3667long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
3668{
3669 handle_t *handle;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003670 ext4_lblk_t block;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003671 loff_t new_size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003672 unsigned int max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003673 int ret = 0;
3674 int ret2 = 0;
3675 int retries = 0;
3676 struct buffer_head map_bh;
3677 unsigned int credits, blkbits = inode->i_blkbits;
3678
3679 /*
3680 * currently supporting (pre)allocate mode for extent-based
3681 * files _only_
3682 */
3683 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
3684 return -EOPNOTSUPP;
3685
3686 /* preallocation to directories is currently not supported */
3687 if (S_ISDIR(inode->i_mode))
3688 return -ENODEV;
3689
3690 block = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003691 /*
3692 * We can't just convert len to max_blocks because
3693 * If blocksize = 4096 offset = 3072 and len = 2048
3694 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003695 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003696 - block;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003697 /*
Mingming Caof3bd1f32008-08-19 22:16:03 -04003698 * credits to insert 1 extent into extent tree
Amit Aroraa2df2a62007-07-17 21:42:41 -04003699 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04003700 credits = ext4_chunk_trans_blocks(inode, max_blocks);
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05003701 mutex_lock(&inode->i_mutex);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04003702 ret = inode_newsize_ok(inode, (len + offset));
3703 if (ret) {
3704 mutex_unlock(&inode->i_mutex);
3705 return ret;
3706 }
Amit Aroraa2df2a62007-07-17 21:42:41 -04003707retry:
3708 while (ret >= 0 && ret < max_blocks) {
3709 block = block + ret;
3710 max_blocks = max_blocks - ret;
3711 handle = ext4_journal_start(inode, credits);
3712 if (IS_ERR(handle)) {
3713 ret = PTR_ERR(handle);
3714 break;
3715 }
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04003716 map_bh.b_state = 0;
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04003717 ret = ext4_get_blocks(handle, inode, block,
3718 max_blocks, &map_bh,
Theodore Ts'oc2177052009-05-14 00:58:52 -04003719 EXT4_GET_BLOCKS_CREATE_UNINIT_EXT);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05003720 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003721#ifdef EXT4FS_DEBUG
3722 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003723 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003724 "returned error inode#%lu, block=%u, "
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05003725 "max_blocks=%u", __func__,
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05003726 inode->i_ino, block, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003727#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04003728 ext4_mark_inode_dirty(handle, inode);
3729 ret2 = ext4_journal_stop(handle);
3730 break;
3731 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003732 if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
3733 blkbits) >> blkbits))
3734 new_size = offset + len;
3735 else
3736 new_size = (block + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003737
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003738 ext4_falloc_update_inode(inode, mode, new_size,
3739 buffer_new(&map_bh));
Amit Aroraa2df2a62007-07-17 21:42:41 -04003740 ext4_mark_inode_dirty(handle, inode);
3741 ret2 = ext4_journal_stop(handle);
3742 if (ret2)
3743 break;
3744 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003745 if (ret == -ENOSPC &&
3746 ext4_should_retry_alloc(inode->i_sb, &retries)) {
3747 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003748 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003749 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05003750 mutex_unlock(&inode->i_mutex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003751 return ret > 0 ? ret2 : ret;
3752}
Eric Sandeen6873fa02008-10-07 00:46:36 -04003753
3754/*
Mingming Cao00314622009-09-28 15:49:08 -04003755 * This function convert a range of blocks to written extents
3756 * The caller of this function will pass the start offset and the size.
3757 * all unwritten extents within this range will be converted to
3758 * written extents.
3759 *
3760 * This function is called from the direct IO end io call back
3761 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05003762 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04003763 */
3764int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
Eric Sandeena1de02d2010-02-04 23:58:38 -05003765 ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04003766{
3767 handle_t *handle;
3768 ext4_lblk_t block;
3769 unsigned int max_blocks;
3770 int ret = 0;
3771 int ret2 = 0;
3772 struct buffer_head map_bh;
3773 unsigned int credits, blkbits = inode->i_blkbits;
3774
3775 block = offset >> blkbits;
3776 /*
3777 * We can't just convert len to max_blocks because
3778 * If blocksize = 4096 offset = 3072 and len = 2048
3779 */
3780 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
3781 - block;
3782 /*
3783 * credits to insert 1 extent into extent tree
3784 */
3785 credits = ext4_chunk_trans_blocks(inode, max_blocks);
3786 while (ret >= 0 && ret < max_blocks) {
3787 block = block + ret;
3788 max_blocks = max_blocks - ret;
3789 handle = ext4_journal_start(inode, credits);
3790 if (IS_ERR(handle)) {
3791 ret = PTR_ERR(handle);
3792 break;
3793 }
3794 map_bh.b_state = 0;
3795 ret = ext4_get_blocks(handle, inode, block,
3796 max_blocks, &map_bh,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003797 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Mingming Cao00314622009-09-28 15:49:08 -04003798 if (ret <= 0) {
3799 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003800 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Mingming Cao00314622009-09-28 15:49:08 -04003801 "returned error inode#%lu, block=%u, "
3802 "max_blocks=%u", __func__,
3803 inode->i_ino, block, max_blocks);
3804 }
3805 ext4_mark_inode_dirty(handle, inode);
3806 ret2 = ext4_journal_stop(handle);
3807 if (ret <= 0 || ret2 )
3808 break;
3809 }
3810 return ret > 0 ? ret2 : ret;
3811}
3812/*
Eric Sandeen6873fa02008-10-07 00:46:36 -04003813 * Callback function called for each extent to gather FIEMAP information.
3814 */
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003815static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path,
Eric Sandeen6873fa02008-10-07 00:46:36 -04003816 struct ext4_ext_cache *newex, struct ext4_extent *ex,
3817 void *data)
3818{
3819 struct fiemap_extent_info *fieinfo = data;
Eric Sandeenc9877b22009-05-01 23:32:06 -04003820 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003821 __u64 logical;
3822 __u64 physical;
3823 __u64 length;
3824 __u32 flags = 0;
3825 int error;
3826
3827 logical = (__u64)newex->ec_block << blksize_bits;
3828
3829 if (newex->ec_type == EXT4_EXT_CACHE_GAP) {
3830 pgoff_t offset;
3831 struct page *page;
3832 struct buffer_head *bh = NULL;
3833
3834 offset = logical >> PAGE_SHIFT;
3835 page = find_get_page(inode->i_mapping, offset);
3836 if (!page || !page_has_buffers(page))
3837 return EXT_CONTINUE;
3838
3839 bh = page_buffers(page);
3840
3841 if (!bh)
3842 return EXT_CONTINUE;
3843
3844 if (buffer_delay(bh)) {
3845 flags |= FIEMAP_EXTENT_DELALLOC;
3846 page_cache_release(page);
3847 } else {
3848 page_cache_release(page);
3849 return EXT_CONTINUE;
3850 }
3851 }
3852
3853 physical = (__u64)newex->ec_start << blksize_bits;
3854 length = (__u64)newex->ec_len << blksize_bits;
3855
3856 if (ex && ext4_ext_is_uninitialized(ex))
3857 flags |= FIEMAP_EXTENT_UNWRITTEN;
3858
3859 /*
3860 * If this extent reaches EXT_MAX_BLOCK, it must be last.
3861 *
3862 * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
3863 * this also indicates no more allocated blocks.
3864 *
3865 * XXX this might miss a single-block extent at EXT_MAX_BLOCK
3866 */
Eric Sandeenc9877b22009-05-01 23:32:06 -04003867 if (ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK ||
Theodore Ts'oeefd7f02009-05-02 19:05:37 -04003868 newex->ec_block + newex->ec_len - 1 == EXT_MAX_BLOCK) {
3869 loff_t size = i_size_read(inode);
3870 loff_t bs = EXT4_BLOCK_SIZE(inode->i_sb);
3871
Eric Sandeen6873fa02008-10-07 00:46:36 -04003872 flags |= FIEMAP_EXTENT_LAST;
Theodore Ts'oeefd7f02009-05-02 19:05:37 -04003873 if ((flags & FIEMAP_EXTENT_DELALLOC) &&
3874 logical+length > size)
3875 length = (size - logical + bs - 1) & ~(bs-1);
3876 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04003877
3878 error = fiemap_fill_next_extent(fieinfo, logical, physical,
3879 length, flags);
3880 if (error < 0)
3881 return error;
3882 if (error == 1)
3883 return EXT_BREAK;
3884
3885 return EXT_CONTINUE;
3886}
3887
3888/* fiemap flags we can handle specified here */
3889#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
3890
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003891static int ext4_xattr_fiemap(struct inode *inode,
3892 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04003893{
3894 __u64 physical = 0;
3895 __u64 length;
3896 __u32 flags = FIEMAP_EXTENT_LAST;
3897 int blockbits = inode->i_sb->s_blocksize_bits;
3898 int error = 0;
3899
3900 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003901 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04003902 struct ext4_iloc iloc;
3903 int offset; /* offset of xattr in inode */
3904
3905 error = ext4_get_inode_loc(inode, &iloc);
3906 if (error)
3907 return error;
3908 physical = iloc.bh->b_blocknr << blockbits;
3909 offset = EXT4_GOOD_OLD_INODE_SIZE +
3910 EXT4_I(inode)->i_extra_isize;
3911 physical += offset;
3912 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
3913 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04003914 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04003915 } else { /* external block */
3916 physical = EXT4_I(inode)->i_file_acl << blockbits;
3917 length = inode->i_sb->s_blocksize;
3918 }
3919
3920 if (physical)
3921 error = fiemap_fill_next_extent(fieinfo, 0, physical,
3922 length, flags);
3923 return (error < 0 ? error : 0);
3924}
3925
3926int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3927 __u64 start, __u64 len)
3928{
3929 ext4_lblk_t start_blk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003930 int error = 0;
3931
3932 /* fallback to generic here if not in extents fmt */
3933 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
3934 return generic_block_fiemap(inode, fieinfo, start, len,
3935 ext4_get_block);
3936
3937 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
3938 return -EBADR;
3939
3940 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
3941 error = ext4_xattr_fiemap(inode, fieinfo);
3942 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05003943 ext4_lblk_t len_blks;
3944 __u64 last_blk;
3945
Eric Sandeen6873fa02008-10-07 00:46:36 -04003946 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05003947 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
3948 if (last_blk >= EXT_MAX_BLOCK)
3949 last_blk = EXT_MAX_BLOCK-1;
3950 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003951
3952 /*
3953 * Walk the extent tree gathering extent information.
3954 * ext4_ext_fiemap_cb will push extents back to user.
3955 */
Eric Sandeen6873fa02008-10-07 00:46:36 -04003956 error = ext4_ext_walk_space(inode, start_blk, len_blks,
3957 ext4_ext_fiemap_cb, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04003958 }
3959
3960 return error;
3961}
3962