blob: a2c21aa09e2b051ab8fc555cb6264de24adb47f5 [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 }
198 bg_start = (block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) +
Alex Tomasa86c6182006-10-11 01:21:03 -0700199 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block);
Valerie Clement74d34872008-02-15 13:43:07 -0500200 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
201
Theodore Ts'oa4912122009-03-12 12:18:34 -0400202 /*
203 * If we are doing delayed allocation, we don't need take
204 * colour into account.
205 */
206 if (test_opt(inode->i_sb, DELALLOC))
207 return bg_start;
208
Valerie Clement74d34872008-02-15 13:43:07 -0500209 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
210 colour = (current->pid % 16) *
Alex Tomasa86c6182006-10-11 01:21:03 -0700211 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
Valerie Clement74d34872008-02-15 13:43:07 -0500212 else
213 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
Alex Tomasa86c6182006-10-11 01:21:03 -0700214 return bg_start + colour + block;
215}
216
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400217/*
218 * Allocation for a meta data block
219 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700220static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400221ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700222 struct ext4_ext_path *path,
223 struct ext4_extent *ex, int *err)
224{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700225 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700226
227 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Theodore Ts'o97df5d12008-12-12 12:41:28 -0500228 newblock = ext4_new_meta_blocks(handle, inode, goal, NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700229 return newblock;
230}
231
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400232static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700233{
234 int size;
235
236 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
237 / sizeof(struct ext4_extent);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400238 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100239#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400240 if (size > 6)
241 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700242#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400243 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700244 return size;
245}
246
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400247static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700248{
249 int size;
250
251 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
252 / sizeof(struct ext4_extent_idx);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400253 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100254#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400255 if (size > 5)
256 size = 5;
Alex Tomasa86c6182006-10-11 01:21:03 -0700257#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400258 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700259 return size;
260}
261
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400262static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700263{
264 int size;
265
266 size = sizeof(EXT4_I(inode)->i_data);
267 size -= sizeof(struct ext4_extent_header);
268 size /= sizeof(struct ext4_extent);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400269 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100270#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400271 if (size > 3)
272 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700273#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400274 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700275 return size;
276}
277
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400278static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700279{
280 int size;
281
282 size = sizeof(EXT4_I(inode)->i_data);
283 size -= sizeof(struct ext4_extent_header);
284 size /= sizeof(struct ext4_extent_idx);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400285 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100286#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400287 if (size > 4)
288 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700289#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400290 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700291 return size;
292}
293
Mingming Caod2a17632008-07-14 17:52:37 -0400294/*
295 * Calculate the number of metadata blocks needed
296 * to allocate @blocks
297 * Worse case is one block per extent
298 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500299int ext4_ext_calc_metadata_amount(struct inode *inode, sector_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -0400300{
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500301 struct ext4_inode_info *ei = EXT4_I(inode);
302 int idxs, num = 0;
Mingming Caod2a17632008-07-14 17:52:37 -0400303
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500304 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
305 / sizeof(struct ext4_extent_idx));
Mingming Caod2a17632008-07-14 17:52:37 -0400306
307 /*
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500308 * If the new delayed allocation block is contiguous with the
309 * previous da block, it can share index blocks with the
310 * previous block, so we only need to allocate a new index
311 * block every idxs leaf blocks. At ldxs**2 blocks, we need
312 * an additional index block, and at ldxs**3 blocks, yet
313 * another index blocks.
Mingming Caod2a17632008-07-14 17:52:37 -0400314 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500315 if (ei->i_da_metadata_calc_len &&
316 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
317 if ((ei->i_da_metadata_calc_len % idxs) == 0)
318 num++;
319 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
320 num++;
321 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
322 num++;
323 ei->i_da_metadata_calc_len = 0;
324 } else
325 ei->i_da_metadata_calc_len++;
326 ei->i_da_metadata_calc_last_lblock++;
327 return num;
328 }
Mingming Caod2a17632008-07-14 17:52:37 -0400329
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500330 /*
331 * In the worst case we need a new set of index blocks at
332 * every level of the inode's extent tree.
333 */
334 ei->i_da_metadata_calc_len = 1;
335 ei->i_da_metadata_calc_last_lblock = lblock;
336 return ext_depth(inode) + 1;
Mingming Caod2a17632008-07-14 17:52:37 -0400337}
338
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400339static int
340ext4_ext_max_entries(struct inode *inode, int depth)
341{
342 int max;
343
344 if (depth == ext_depth(inode)) {
345 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400346 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400347 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400348 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400349 } else {
350 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400351 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400352 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400353 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400354 }
355
356 return max;
357}
358
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400359static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
360{
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400361 ext4_fsblk_t block = ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400362 int len = ext4_ext_get_actual_len(ext);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400363
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400364 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400365}
366
367static int ext4_valid_extent_idx(struct inode *inode,
368 struct ext4_extent_idx *ext_idx)
369{
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400370 ext4_fsblk_t block = idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400371
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400372 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400373}
374
375static int ext4_valid_extent_entries(struct inode *inode,
376 struct ext4_extent_header *eh,
377 int depth)
378{
379 struct ext4_extent *ext;
380 struct ext4_extent_idx *ext_idx;
381 unsigned short entries;
382 if (eh->eh_entries == 0)
383 return 1;
384
385 entries = le16_to_cpu(eh->eh_entries);
386
387 if (depth == 0) {
388 /* leaf entries */
389 ext = EXT_FIRST_EXTENT(eh);
390 while (entries) {
391 if (!ext4_valid_extent(inode, ext))
392 return 0;
393 ext++;
394 entries--;
395 }
396 } else {
397 ext_idx = EXT_FIRST_INDEX(eh);
398 while (entries) {
399 if (!ext4_valid_extent_idx(inode, ext_idx))
400 return 0;
401 ext_idx++;
402 entries--;
403 }
404 }
405 return 1;
406}
407
408static int __ext4_ext_check(const char *function, struct inode *inode,
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400409 struct ext4_extent_header *eh,
410 int depth)
411{
412 const char *error_msg;
413 int max = 0;
414
415 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
416 error_msg = "invalid magic";
417 goto corrupted;
418 }
419 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
420 error_msg = "unexpected eh_depth";
421 goto corrupted;
422 }
423 if (unlikely(eh->eh_max == 0)) {
424 error_msg = "invalid eh_max";
425 goto corrupted;
426 }
427 max = ext4_ext_max_entries(inode, depth);
428 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
429 error_msg = "too large eh_max";
430 goto corrupted;
431 }
432 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
433 error_msg = "invalid eh_entries";
434 goto corrupted;
435 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400436 if (!ext4_valid_extent_entries(inode, eh, depth)) {
437 error_msg = "invalid extent entries";
438 goto corrupted;
439 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400440 return 0;
441
442corrupted:
Eric Sandeen12062dd2010-02-15 14:19:27 -0500443 __ext4_error(inode->i_sb, function,
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400444 "bad header/extent in inode #%lu: %s - magic %x, "
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400445 "entries %u, max %u(%u), depth %u(%u)",
446 inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
447 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
448 max, le16_to_cpu(eh->eh_depth), depth);
449
450 return -EIO;
451}
452
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400453#define ext4_ext_check(inode, eh, depth) \
454 __ext4_ext_check(__func__, inode, eh, depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400455
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400456int ext4_ext_check_inode(struct inode *inode)
457{
458 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
459}
460
Alex Tomasa86c6182006-10-11 01:21:03 -0700461#ifdef EXT_DEBUG
462static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
463{
464 int k, l = path->p_depth;
465
466 ext_debug("path:");
467 for (k = 0; k <= l; k++, path++) {
468 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700469 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700470 idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700471 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400472 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700473 le32_to_cpu(path->p_ext->ee_block),
Mingming553f9002009-09-18 13:34:55 -0400474 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400475 ext4_ext_get_actual_len(path->p_ext),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700476 ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700477 } else
478 ext_debug(" []");
479 }
480 ext_debug("\n");
481}
482
483static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
484{
485 int depth = ext_depth(inode);
486 struct ext4_extent_header *eh;
487 struct ext4_extent *ex;
488 int i;
489
490 if (!path)
491 return;
492
493 eh = path[depth].p_hdr;
494 ex = EXT_FIRST_EXTENT(eh);
495
Mingming553f9002009-09-18 13:34:55 -0400496 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
497
Alex Tomasa86c6182006-10-11 01:21:03 -0700498 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400499 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
500 ext4_ext_is_uninitialized(ex),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400501 ext4_ext_get_actual_len(ex), ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700502 }
503 ext_debug("\n");
504}
505#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400506#define ext4_ext_show_path(inode, path)
507#define ext4_ext_show_leaf(inode, path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700508#endif
509
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500510void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700511{
512 int depth = path->p_depth;
513 int i;
514
515 for (i = 0; i <= depth; i++, path++)
516 if (path->p_bh) {
517 brelse(path->p_bh);
518 path->p_bh = NULL;
519 }
520}
521
522/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700523 * ext4_ext_binsearch_idx:
524 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400525 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700526 */
527static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500528ext4_ext_binsearch_idx(struct inode *inode,
529 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700530{
531 struct ext4_extent_header *eh = path->p_hdr;
532 struct ext4_extent_idx *r, *l, *m;
533
Alex Tomasa86c6182006-10-11 01:21:03 -0700534
Eric Sandeenbba90742008-01-28 23:58:27 -0500535 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700536
537 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400538 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700539 while (l <= r) {
540 m = l + (r - l) / 2;
541 if (block < le32_to_cpu(m->ei_block))
542 r = m - 1;
543 else
544 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400545 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
546 m, le32_to_cpu(m->ei_block),
547 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700548 }
549
550 path->p_idx = l - 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700551 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400552 idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700553
554#ifdef CHECK_BINSEARCH
555 {
556 struct ext4_extent_idx *chix, *ix;
557 int k;
558
559 chix = ix = EXT_FIRST_INDEX(eh);
560 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
561 if (k != 0 &&
562 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400563 printk(KERN_DEBUG "k=%d, ix=0x%p, "
564 "first=0x%p\n", k,
565 ix, EXT_FIRST_INDEX(eh));
566 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700567 le32_to_cpu(ix->ei_block),
568 le32_to_cpu(ix[-1].ei_block));
569 }
570 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400571 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700572 if (block < le32_to_cpu(ix->ei_block))
573 break;
574 chix = ix;
575 }
576 BUG_ON(chix != path->p_idx);
577 }
578#endif
579
580}
581
582/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700583 * ext4_ext_binsearch:
584 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400585 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700586 */
587static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500588ext4_ext_binsearch(struct inode *inode,
589 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700590{
591 struct ext4_extent_header *eh = path->p_hdr;
592 struct ext4_extent *r, *l, *m;
593
Alex Tomasa86c6182006-10-11 01:21:03 -0700594 if (eh->eh_entries == 0) {
595 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700596 * this leaf is empty:
597 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700598 */
599 return;
600 }
601
Eric Sandeenbba90742008-01-28 23:58:27 -0500602 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700603
604 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400605 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700606
607 while (l <= r) {
608 m = l + (r - l) / 2;
609 if (block < le32_to_cpu(m->ee_block))
610 r = m - 1;
611 else
612 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400613 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
614 m, le32_to_cpu(m->ee_block),
615 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700616 }
617
618 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400619 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400620 le32_to_cpu(path->p_ext->ee_block),
621 ext_pblock(path->p_ext),
Mingming553f9002009-09-18 13:34:55 -0400622 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400623 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700624
625#ifdef CHECK_BINSEARCH
626 {
627 struct ext4_extent *chex, *ex;
628 int k;
629
630 chex = ex = EXT_FIRST_EXTENT(eh);
631 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
632 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400633 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700634 if (block < le32_to_cpu(ex->ee_block))
635 break;
636 chex = ex;
637 }
638 BUG_ON(chex != path->p_ext);
639 }
640#endif
641
642}
643
644int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
645{
646 struct ext4_extent_header *eh;
647
648 eh = ext_inode_hdr(inode);
649 eh->eh_depth = 0;
650 eh->eh_entries = 0;
651 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400652 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700653 ext4_mark_inode_dirty(handle, inode);
654 ext4_ext_invalidate_cache(inode);
655 return 0;
656}
657
658struct ext4_ext_path *
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500659ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
660 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700661{
662 struct ext4_extent_header *eh;
663 struct buffer_head *bh;
664 short int depth, i, ppos = 0, alloc = 0;
665
666 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400667 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700668
669 /* account possible depth increase */
670 if (!path) {
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800671 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700672 GFP_NOFS);
673 if (!path)
674 return ERR_PTR(-ENOMEM);
675 alloc = 1;
676 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700677 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400678 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700679
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400680 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700681 /* walk through the tree */
682 while (i) {
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400683 int need_to_validate = 0;
684
Alex Tomasa86c6182006-10-11 01:21:03 -0700685 ext_debug("depth %d: num %d, max %d\n",
686 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400687
Alex Tomasa86c6182006-10-11 01:21:03 -0700688 ext4_ext_binsearch_idx(inode, path + ppos, block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700689 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700690 path[ppos].p_depth = i;
691 path[ppos].p_ext = NULL;
692
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400693 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
694 if (unlikely(!bh))
Alex Tomasa86c6182006-10-11 01:21:03 -0700695 goto err;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400696 if (!bh_uptodate_or_lock(bh)) {
697 if (bh_submit_read(bh) < 0) {
698 put_bh(bh);
699 goto err;
700 }
701 /* validate the extent entries */
702 need_to_validate = 1;
703 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700704 eh = ext_block_hdr(bh);
705 ppos++;
706 BUG_ON(ppos > depth);
707 path[ppos].p_bh = bh;
708 path[ppos].p_hdr = eh;
709 i--;
710
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400711 if (need_to_validate && ext4_ext_check(inode, eh, i))
Alex Tomasa86c6182006-10-11 01:21:03 -0700712 goto err;
713 }
714
715 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700716 path[ppos].p_ext = NULL;
717 path[ppos].p_idx = NULL;
718
Alex Tomasa86c6182006-10-11 01:21:03 -0700719 /* find extent */
720 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400721 /* if not an empty leaf */
722 if (path[ppos].p_ext)
723 path[ppos].p_block = ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700724
725 ext4_ext_show_path(inode, path);
726
727 return path;
728
729err:
730 ext4_ext_drop_refs(path);
731 if (alloc)
732 kfree(path);
733 return ERR_PTR(-EIO);
734}
735
736/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700737 * ext4_ext_insert_index:
738 * insert new index [@logical;@ptr] into the block at @curp;
739 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700740 */
Mingming Cao00314622009-09-28 15:49:08 -0400741int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700742 struct ext4_ext_path *curp,
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700743 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700744{
745 struct ext4_extent_idx *ix;
746 int len, err;
747
Avantika Mathur7e028972006-12-06 20:41:33 -0800748 err = ext4_ext_get_access(handle, inode, curp);
749 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700750 return err;
751
752 BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
753 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
754 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
755 /* insert after */
756 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
757 len = (len - 1) * sizeof(struct ext4_extent_idx);
758 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400759 ext_debug("insert new index %d after: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700760 "move %d from 0x%p to 0x%p\n",
761 logical, ptr, len,
762 (curp->p_idx + 1), (curp->p_idx + 2));
763 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
764 }
765 ix = curp->p_idx + 1;
766 } else {
767 /* insert before */
768 len = len * sizeof(struct ext4_extent_idx);
769 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400770 ext_debug("insert new index %d before: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700771 "move %d from 0x%p to 0x%p\n",
772 logical, ptr, len,
773 curp->p_idx, (curp->p_idx + 1));
774 memmove(curp->p_idx + 1, curp->p_idx, len);
775 ix = curp->p_idx;
776 }
777
778 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700779 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400780 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700781
782 BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400783 > le16_to_cpu(curp->p_hdr->eh_max));
Alex Tomasa86c6182006-10-11 01:21:03 -0700784 BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
785
786 err = ext4_ext_dirty(handle, inode, curp);
787 ext4_std_error(inode->i_sb, err);
788
789 return err;
790}
791
792/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700793 * ext4_ext_split:
794 * inserts new subtree into the path, using free index entry
795 * at depth @at:
796 * - allocates all needed blocks (new leaf and all intermediate index blocks)
797 * - makes decision where to split
798 * - moves remaining extents and index entries (right to the split point)
799 * into the newly allocated blocks
800 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700801 */
802static int ext4_ext_split(handle_t *handle, struct inode *inode,
803 struct ext4_ext_path *path,
804 struct ext4_extent *newext, int at)
805{
806 struct buffer_head *bh = NULL;
807 int depth = ext_depth(inode);
808 struct ext4_extent_header *neh;
809 struct ext4_extent_idx *fidx;
810 struct ext4_extent *ex;
811 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700812 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700813 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700814 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700815 int err = 0;
816
817 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700818 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700819
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700820 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700821 * border from split point */
822 BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
823 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
824 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700825 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700826 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400827 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700828 } else {
829 border = newext->ee_block;
830 ext_debug("leaf will be added."
831 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400832 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700833 }
834
835 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700836 * If error occurs, then we break processing
837 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700838 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700839 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700840 */
841
842 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700843 * Get array to track all allocated blocks.
844 * We need this to handle errors and free blocks
845 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700846 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800847 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700848 if (!ablocks)
849 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700850
851 /* allocate all needed blocks */
852 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
853 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400854 newblock = ext4_ext_new_meta_block(handle, inode, path,
855 newext, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700856 if (newblock == 0)
857 goto cleanup;
858 ablocks[a] = newblock;
859 }
860
861 /* initialize new leaf */
862 newblock = ablocks[--a];
863 BUG_ON(newblock == 0);
864 bh = sb_getblk(inode->i_sb, newblock);
865 if (!bh) {
866 err = -EIO;
867 goto cleanup;
868 }
869 lock_buffer(bh);
870
Avantika Mathur7e028972006-12-06 20:41:33 -0800871 err = ext4_journal_get_create_access(handle, bh);
872 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700873 goto cleanup;
874
875 neh = ext_block_hdr(bh);
876 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400877 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700878 neh->eh_magic = EXT4_EXT_MAGIC;
879 neh->eh_depth = 0;
880 ex = EXT_FIRST_EXTENT(neh);
881
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700882 /* move remainder of path[depth] to the new leaf */
Alex Tomasa86c6182006-10-11 01:21:03 -0700883 BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
884 /* start copy from next extent */
885 /* TODO: we could do it by single memmove */
886 m = 0;
887 path[depth].p_ext++;
888 while (path[depth].p_ext <=
889 EXT_MAX_EXTENT(path[depth].p_hdr)) {
Mingming553f9002009-09-18 13:34:55 -0400890 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400891 le32_to_cpu(path[depth].p_ext->ee_block),
892 ext_pblock(path[depth].p_ext),
Mingming553f9002009-09-18 13:34:55 -0400893 ext4_ext_is_uninitialized(path[depth].p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400894 ext4_ext_get_actual_len(path[depth].p_ext),
Alex Tomasa86c6182006-10-11 01:21:03 -0700895 newblock);
896 /*memmove(ex++, path[depth].p_ext++,
897 sizeof(struct ext4_extent));
898 neh->eh_entries++;*/
899 path[depth].p_ext++;
900 m++;
901 }
902 if (m) {
903 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400904 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700905 }
906
907 set_buffer_uptodate(bh);
908 unlock_buffer(bh);
909
Frank Mayhar03901312009-01-07 00:06:22 -0500910 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800911 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700912 goto cleanup;
913 brelse(bh);
914 bh = NULL;
915
916 /* correct old leaf */
917 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800918 err = ext4_ext_get_access(handle, inode, path + depth);
919 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700920 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400921 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800922 err = ext4_ext_dirty(handle, inode, path + depth);
923 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700924 goto cleanup;
925
926 }
927
928 /* create intermediate indexes */
929 k = depth - at - 1;
930 BUG_ON(k < 0);
931 if (k)
932 ext_debug("create %d intermediate indices\n", k);
933 /* insert new index into current index block */
934 /* current depth stored in i var */
935 i = depth - 1;
936 while (k--) {
937 oldblock = newblock;
938 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -0500939 bh = sb_getblk(inode->i_sb, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700940 if (!bh) {
941 err = -EIO;
942 goto cleanup;
943 }
944 lock_buffer(bh);
945
Avantika Mathur7e028972006-12-06 20:41:33 -0800946 err = ext4_journal_get_create_access(handle, bh);
947 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700948 goto cleanup;
949
950 neh = ext_block_hdr(bh);
951 neh->eh_entries = cpu_to_le16(1);
952 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400953 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700954 neh->eh_depth = cpu_to_le16(depth - i);
955 fidx = EXT_FIRST_INDEX(neh);
956 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700957 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700958
Eric Sandeenbba90742008-01-28 23:58:27 -0500959 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
960 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700961 /* copy indexes */
962 m = 0;
963 path[i].p_idx++;
964
965 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
966 EXT_MAX_INDEX(path[i].p_hdr));
967 BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
968 EXT_LAST_INDEX(path[i].p_hdr));
969 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400970 ext_debug("%d: move %d:%llu in new index %llu\n", i,
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400971 le32_to_cpu(path[i].p_idx->ei_block),
972 idx_pblock(path[i].p_idx),
973 newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700974 /*memmove(++fidx, path[i].p_idx++,
975 sizeof(struct ext4_extent_idx));
976 neh->eh_entries++;
977 BUG_ON(neh->eh_entries > neh->eh_max);*/
978 path[i].p_idx++;
979 m++;
980 }
981 if (m) {
982 memmove(++fidx, path[i].p_idx - m,
983 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400984 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700985 }
986 set_buffer_uptodate(bh);
987 unlock_buffer(bh);
988
Frank Mayhar03901312009-01-07 00:06:22 -0500989 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800990 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700991 goto cleanup;
992 brelse(bh);
993 bh = NULL;
994
995 /* correct old index */
996 if (m) {
997 err = ext4_ext_get_access(handle, inode, path + i);
998 if (err)
999 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001000 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001001 err = ext4_ext_dirty(handle, inode, path + i);
1002 if (err)
1003 goto cleanup;
1004 }
1005
1006 i--;
1007 }
1008
1009 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001010 err = ext4_ext_insert_index(handle, inode, path + at,
1011 le32_to_cpu(border), newblock);
1012
1013cleanup:
1014 if (bh) {
1015 if (buffer_locked(bh))
1016 unlock_buffer(bh);
1017 brelse(bh);
1018 }
1019
1020 if (err) {
1021 /* free all allocated blocks in error case */
1022 for (i = 0; i < depth; i++) {
1023 if (!ablocks[i])
1024 continue;
Theodore Ts'oe6362602009-11-23 07:17:05 -05001025 ext4_free_blocks(handle, inode, 0, ablocks[i], 1,
1026 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001027 }
1028 }
1029 kfree(ablocks);
1030
1031 return err;
1032}
1033
1034/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001035 * ext4_ext_grow_indepth:
1036 * implements tree growing procedure:
1037 * - allocates new block
1038 * - moves top-level data (index block or leaf) into the new block
1039 * - initializes new top-level, creating index that points to the
1040 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001041 */
1042static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
1043 struct ext4_ext_path *path,
1044 struct ext4_extent *newext)
1045{
1046 struct ext4_ext_path *curp = path;
1047 struct ext4_extent_header *neh;
1048 struct ext4_extent_idx *fidx;
1049 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001050 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001051 int err = 0;
1052
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -04001053 newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07001054 if (newblock == 0)
1055 return err;
1056
1057 bh = sb_getblk(inode->i_sb, newblock);
1058 if (!bh) {
1059 err = -EIO;
1060 ext4_std_error(inode->i_sb, err);
1061 return err;
1062 }
1063 lock_buffer(bh);
1064
Avantika Mathur7e028972006-12-06 20:41:33 -08001065 err = ext4_journal_get_create_access(handle, bh);
1066 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001067 unlock_buffer(bh);
1068 goto out;
1069 }
1070
1071 /* move top-level index/leaf into new block */
1072 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
1073
1074 /* set size of new block */
1075 neh = ext_block_hdr(bh);
1076 /* old root could have indexes or leaves
1077 * so calculate e_max right way */
1078 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001079 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001080 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001081 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001082 neh->eh_magic = EXT4_EXT_MAGIC;
1083 set_buffer_uptodate(bh);
1084 unlock_buffer(bh);
1085
Frank Mayhar03901312009-01-07 00:06:22 -05001086 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001087 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001088 goto out;
1089
1090 /* create index in new top-level index: num,max,pointer */
Avantika Mathur7e028972006-12-06 20:41:33 -08001091 err = ext4_ext_get_access(handle, inode, curp);
1092 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001093 goto out;
1094
1095 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001096 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001097 curp->p_hdr->eh_entries = cpu_to_le16(1);
1098 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
Dmitry Monakhove9f410b2007-07-18 09:09:15 -04001099
1100 if (path[0].p_hdr->eh_depth)
1101 curp->p_idx->ei_block =
1102 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
1103 else
1104 curp->p_idx->ei_block =
1105 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001106 ext4_idx_store_pblock(curp->p_idx, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001107
1108 neh = ext_inode_hdr(inode);
1109 fidx = EXT_FIRST_INDEX(neh);
Mingming Cao2ae02102006-10-11 01:21:11 -07001110 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001111 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001112 le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
Alex Tomasa86c6182006-10-11 01:21:03 -07001113
1114 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
1115 err = ext4_ext_dirty(handle, inode, curp);
1116out:
1117 brelse(bh);
1118
1119 return err;
1120}
1121
1122/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001123 * ext4_ext_create_new_leaf:
1124 * finds empty index and adds new leaf.
1125 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001126 */
1127static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
1128 struct ext4_ext_path *path,
1129 struct ext4_extent *newext)
1130{
1131 struct ext4_ext_path *curp;
1132 int depth, i, err = 0;
1133
1134repeat:
1135 i = depth = ext_depth(inode);
1136
1137 /* walk up to the tree and look for free index entry */
1138 curp = path + depth;
1139 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1140 i--;
1141 curp--;
1142 }
1143
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001144 /* we use already allocated block for index block,
1145 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001146 if (EXT_HAS_FREE_INDEX(curp)) {
1147 /* if we found index with free entry, then use that
1148 * entry: create all needed subtree and add new leaf */
1149 err = ext4_ext_split(handle, inode, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001150 if (err)
1151 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001152
1153 /* refill path */
1154 ext4_ext_drop_refs(path);
1155 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001156 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1157 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001158 if (IS_ERR(path))
1159 err = PTR_ERR(path);
1160 } else {
1161 /* tree is full, time to grow in depth */
1162 err = ext4_ext_grow_indepth(handle, inode, path, newext);
1163 if (err)
1164 goto out;
1165
1166 /* refill path */
1167 ext4_ext_drop_refs(path);
1168 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001169 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1170 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001171 if (IS_ERR(path)) {
1172 err = PTR_ERR(path);
1173 goto out;
1174 }
1175
1176 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001177 * only first (depth 0 -> 1) produces free space;
1178 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001179 */
1180 depth = ext_depth(inode);
1181 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001182 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001183 goto repeat;
1184 }
1185 }
1186
1187out:
1188 return err;
1189}
1190
1191/*
Alex Tomas1988b512008-01-28 23:58:27 -05001192 * search the closest allocated block to the left for *logical
1193 * and returns it at @logical + it's physical address at @phys
1194 * if *logical is the smallest allocated block, the function
1195 * returns 0 at @phys
1196 * return value contains 0 (success) or error code
1197 */
1198int
1199ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
1200 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1201{
1202 struct ext4_extent_idx *ix;
1203 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001204 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001205
1206 BUG_ON(path == NULL);
1207 depth = path->p_depth;
1208 *phys = 0;
1209
1210 if (depth == 0 && path->p_ext == NULL)
1211 return 0;
1212
1213 /* usually extent in the path covers blocks smaller
1214 * then *logical, but it can be that extent is the
1215 * first one in the file */
1216
1217 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001218 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001219 if (*logical < le32_to_cpu(ex->ee_block)) {
1220 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1221 while (--depth >= 0) {
1222 ix = path[depth].p_idx;
1223 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1224 }
1225 return 0;
1226 }
1227
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001228 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
Alex Tomas1988b512008-01-28 23:58:27 -05001229
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001230 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1231 *phys = ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001232 return 0;
1233}
1234
1235/*
1236 * search the closest allocated block to the right for *logical
1237 * and returns it at @logical + it's physical address at @phys
1238 * if *logical is the smallest allocated block, the function
1239 * returns 0 at @phys
1240 * return value contains 0 (success) or error code
1241 */
1242int
1243ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
1244 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1245{
1246 struct buffer_head *bh = NULL;
1247 struct ext4_extent_header *eh;
1248 struct ext4_extent_idx *ix;
1249 struct ext4_extent *ex;
1250 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001251 int depth; /* Note, NOT eh_depth; depth from top of tree */
1252 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001253
1254 BUG_ON(path == NULL);
1255 depth = path->p_depth;
1256 *phys = 0;
1257
1258 if (depth == 0 && path->p_ext == NULL)
1259 return 0;
1260
1261 /* usually extent in the path covers blocks smaller
1262 * then *logical, but it can be that extent is the
1263 * first one in the file */
1264
1265 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001266 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001267 if (*logical < le32_to_cpu(ex->ee_block)) {
1268 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1269 while (--depth >= 0) {
1270 ix = path[depth].p_idx;
1271 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1272 }
1273 *logical = le32_to_cpu(ex->ee_block);
1274 *phys = ext_pblock(ex);
1275 return 0;
1276 }
1277
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001278 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
Alex Tomas1988b512008-01-28 23:58:27 -05001279
1280 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1281 /* next allocated block in this leaf */
1282 ex++;
1283 *logical = le32_to_cpu(ex->ee_block);
1284 *phys = ext_pblock(ex);
1285 return 0;
1286 }
1287
1288 /* go up and search for index to the right */
1289 while (--depth >= 0) {
1290 ix = path[depth].p_idx;
1291 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001292 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001293 }
1294
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001295 /* we've gone up to the root and found no index to the right */
1296 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001297
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001298got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001299 /* we've found index to the right, let's
1300 * follow it and find the closest allocated
1301 * block to the right */
1302 ix++;
1303 block = idx_pblock(ix);
1304 while (++depth < path->p_depth) {
1305 bh = sb_bread(inode->i_sb, block);
1306 if (bh == NULL)
1307 return -EIO;
1308 eh = ext_block_hdr(bh);
Eric Sandeen395a87b2009-03-10 18:18:47 -04001309 /* subtract from p_depth to get proper eh_depth */
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001310 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001311 put_bh(bh);
1312 return -EIO;
1313 }
1314 ix = EXT_FIRST_INDEX(eh);
1315 block = idx_pblock(ix);
1316 put_bh(bh);
1317 }
1318
1319 bh = sb_bread(inode->i_sb, block);
1320 if (bh == NULL)
1321 return -EIO;
1322 eh = ext_block_hdr(bh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001323 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001324 put_bh(bh);
1325 return -EIO;
1326 }
1327 ex = EXT_FIRST_EXTENT(eh);
1328 *logical = le32_to_cpu(ex->ee_block);
1329 *phys = ext_pblock(ex);
1330 put_bh(bh);
1331 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001332}
1333
1334/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001335 * ext4_ext_next_allocated_block:
1336 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1337 * NOTE: it considers block number from index entry as
1338 * allocated block. Thus, index entries have to be consistent
1339 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001340 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001341static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001342ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1343{
1344 int depth;
1345
1346 BUG_ON(path == NULL);
1347 depth = path->p_depth;
1348
1349 if (depth == 0 && path->p_ext == NULL)
1350 return EXT_MAX_BLOCK;
1351
1352 while (depth >= 0) {
1353 if (depth == path->p_depth) {
1354 /* leaf */
1355 if (path[depth].p_ext !=
1356 EXT_LAST_EXTENT(path[depth].p_hdr))
1357 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1358 } else {
1359 /* index */
1360 if (path[depth].p_idx !=
1361 EXT_LAST_INDEX(path[depth].p_hdr))
1362 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1363 }
1364 depth--;
1365 }
1366
1367 return EXT_MAX_BLOCK;
1368}
1369
1370/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001371 * ext4_ext_next_leaf_block:
Alex Tomasa86c6182006-10-11 01:21:03 -07001372 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1373 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001374static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
Andrew Morton63f57932006-10-11 01:21:24 -07001375 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001376{
1377 int depth;
1378
1379 BUG_ON(path == NULL);
1380 depth = path->p_depth;
1381
1382 /* zero-tree has no leaf blocks at all */
1383 if (depth == 0)
1384 return EXT_MAX_BLOCK;
1385
1386 /* go to index block */
1387 depth--;
1388
1389 while (depth >= 0) {
1390 if (path[depth].p_idx !=
1391 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001392 return (ext4_lblk_t)
1393 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001394 depth--;
1395 }
1396
1397 return EXT_MAX_BLOCK;
1398}
1399
1400/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001401 * ext4_ext_correct_indexes:
1402 * if leaf gets modified and modified extent is first in the leaf,
1403 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001404 * TODO: do we need to correct tree in all cases?
1405 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001406static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001407 struct ext4_ext_path *path)
1408{
1409 struct ext4_extent_header *eh;
1410 int depth = ext_depth(inode);
1411 struct ext4_extent *ex;
1412 __le32 border;
1413 int k, err = 0;
1414
1415 eh = path[depth].p_hdr;
1416 ex = path[depth].p_ext;
1417 BUG_ON(ex == NULL);
1418 BUG_ON(eh == NULL);
1419
1420 if (depth == 0) {
1421 /* there is no tree at all */
1422 return 0;
1423 }
1424
1425 if (ex != EXT_FIRST_EXTENT(eh)) {
1426 /* we correct tree if first leaf got modified only */
1427 return 0;
1428 }
1429
1430 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001431 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001432 */
1433 k = depth - 1;
1434 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001435 err = ext4_ext_get_access(handle, inode, path + k);
1436 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001437 return err;
1438 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001439 err = ext4_ext_dirty(handle, inode, path + k);
1440 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001441 return err;
1442
1443 while (k--) {
1444 /* change all left-side indexes */
1445 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1446 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001447 err = ext4_ext_get_access(handle, inode, path + k);
1448 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001449 break;
1450 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001451 err = ext4_ext_dirty(handle, inode, path + k);
1452 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001453 break;
1454 }
1455
1456 return err;
1457}
1458
Akira Fujita748de672009-06-17 19:24:03 -04001459int
Alex Tomasa86c6182006-10-11 01:21:03 -07001460ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1461 struct ext4_extent *ex2)
1462{
Amit Arora749269f2007-07-18 09:02:56 -04001463 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001464
1465 /*
1466 * Make sure that either both extents are uninitialized, or
1467 * both are _not_.
1468 */
1469 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1470 return 0;
1471
Amit Arora749269f2007-07-18 09:02:56 -04001472 if (ext4_ext_is_uninitialized(ex1))
1473 max_len = EXT_UNINIT_MAX_LEN;
1474 else
1475 max_len = EXT_INIT_MAX_LEN;
1476
Amit Aroraa2df2a62007-07-17 21:42:41 -04001477 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1478 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1479
1480 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001481 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001482 return 0;
1483
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001484 /*
1485 * To allow future support for preallocated extents to be added
1486 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001487 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001488 */
Amit Arora749269f2007-07-18 09:02:56 -04001489 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001490 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001491#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001492 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001493 return 0;
1494#endif
1495
Amit Aroraa2df2a62007-07-17 21:42:41 -04001496 if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001497 return 1;
1498 return 0;
1499}
1500
1501/*
Amit Arora56055d32007-07-17 21:42:38 -04001502 * This function tries to merge the "ex" extent to the next extent in the tree.
1503 * It always tries to merge towards right. If you want to merge towards
1504 * left, pass "ex - 1" as argument instead of "ex".
1505 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1506 * 1 if they got merged.
1507 */
1508int ext4_ext_try_to_merge(struct inode *inode,
1509 struct ext4_ext_path *path,
1510 struct ext4_extent *ex)
1511{
1512 struct ext4_extent_header *eh;
1513 unsigned int depth, len;
1514 int merge_done = 0;
1515 int uninitialized = 0;
1516
1517 depth = ext_depth(inode);
1518 BUG_ON(path[depth].p_hdr == NULL);
1519 eh = path[depth].p_hdr;
1520
1521 while (ex < EXT_LAST_EXTENT(eh)) {
1522 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1523 break;
1524 /* merge with next extent! */
1525 if (ext4_ext_is_uninitialized(ex))
1526 uninitialized = 1;
1527 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1528 + ext4_ext_get_actual_len(ex + 1));
1529 if (uninitialized)
1530 ext4_ext_mark_uninitialized(ex);
1531
1532 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1533 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1534 * sizeof(struct ext4_extent);
1535 memmove(ex + 1, ex + 2, len);
1536 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001537 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001538 merge_done = 1;
1539 WARN_ON(eh->eh_entries == 0);
1540 if (!eh->eh_entries)
Eric Sandeen12062dd2010-02-15 14:19:27 -05001541 ext4_error(inode->i_sb,
1542 "inode#%lu, eh->eh_entries = 0!",
1543 inode->i_ino);
Amit Arora56055d32007-07-17 21:42:38 -04001544 }
1545
1546 return merge_done;
1547}
1548
1549/*
Amit Arora25d14f92007-05-24 13:04:13 -04001550 * check if a portion of the "newext" extent overlaps with an
1551 * existing extent.
1552 *
1553 * If there is an overlap discovered, it updates the length of the newext
1554 * such that there will be no overlap, and then returns 1.
1555 * If there is no overlap found, it returns 0.
1556 */
1557unsigned int ext4_ext_check_overlap(struct inode *inode,
1558 struct ext4_extent *newext,
1559 struct ext4_ext_path *path)
1560{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001561 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001562 unsigned int depth, len1;
1563 unsigned int ret = 0;
1564
1565 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001566 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001567 depth = ext_depth(inode);
1568 if (!path[depth].p_ext)
1569 goto out;
1570 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1571
1572 /*
1573 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001574 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001575 */
1576 if (b2 < b1) {
1577 b2 = ext4_ext_next_allocated_block(path);
1578 if (b2 == EXT_MAX_BLOCK)
1579 goto out;
1580 }
1581
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001582 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001583 if (b1 + len1 < b1) {
1584 len1 = EXT_MAX_BLOCK - b1;
1585 newext->ee_len = cpu_to_le16(len1);
1586 ret = 1;
1587 }
1588
1589 /* check for overlap */
1590 if (b1 + len1 > b2) {
1591 newext->ee_len = cpu_to_le16(b2 - b1);
1592 ret = 1;
1593 }
1594out:
1595 return ret;
1596}
1597
1598/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001599 * ext4_ext_insert_extent:
1600 * tries to merge requsted extent into the existing extent or
1601 * inserts requested extent as new one into the tree,
1602 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001603 */
1604int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1605 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04001606 struct ext4_extent *newext, int flag)
Alex Tomasa86c6182006-10-11 01:21:03 -07001607{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001608 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001609 struct ext4_extent *ex, *fex;
1610 struct ext4_extent *nearex; /* nearest extent */
1611 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001612 int depth, len, err;
1613 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001614 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001615
Amit Aroraa2df2a62007-07-17 21:42:41 -04001616 BUG_ON(ext4_ext_get_actual_len(newext) == 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07001617 depth = ext_depth(inode);
1618 ex = path[depth].p_ext;
1619 BUG_ON(path[depth].p_hdr == NULL);
1620
1621 /* try to insert block into found extent and return */
Mingming Cao00314622009-09-28 15:49:08 -04001622 if (ex && (flag != EXT4_GET_BLOCKS_DIO_CREATE_EXT)
1623 && ext4_can_extents_be_merged(inode, ex, newext)) {
Mingming553f9002009-09-18 13:34:55 -04001624 ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
1625 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001626 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001627 le32_to_cpu(ex->ee_block),
Mingming553f9002009-09-18 13:34:55 -04001628 ext4_ext_is_uninitialized(ex),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001629 ext4_ext_get_actual_len(ex), ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001630 err = ext4_ext_get_access(handle, inode, path + depth);
1631 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001632 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001633
1634 /*
1635 * ext4_can_extents_be_merged should have checked that either
1636 * both extents are uninitialized, or both aren't. Thus we
1637 * need to check only one of them here.
1638 */
1639 if (ext4_ext_is_uninitialized(ex))
1640 uninitialized = 1;
1641 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1642 + ext4_ext_get_actual_len(newext));
1643 if (uninitialized)
1644 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001645 eh = path[depth].p_hdr;
1646 nearex = ex;
1647 goto merge;
1648 }
1649
1650repeat:
1651 depth = ext_depth(inode);
1652 eh = path[depth].p_hdr;
1653 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1654 goto has_space;
1655
1656 /* probably next leaf has space for us? */
1657 fex = EXT_LAST_EXTENT(eh);
1658 next = ext4_ext_next_leaf_block(inode, path);
1659 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1660 && next != EXT_MAX_BLOCK) {
1661 ext_debug("next leaf block - %d\n", next);
1662 BUG_ON(npath != NULL);
1663 npath = ext4_ext_find_extent(inode, next, NULL);
1664 if (IS_ERR(npath))
1665 return PTR_ERR(npath);
1666 BUG_ON(npath->p_depth != path->p_depth);
1667 eh = npath[depth].p_hdr;
1668 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1669 ext_debug("next leaf isnt full(%d)\n",
1670 le16_to_cpu(eh->eh_entries));
1671 path = npath;
1672 goto repeat;
1673 }
1674 ext_debug("next leaf has no free space(%d,%d)\n",
1675 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1676 }
1677
1678 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001679 * There is no free space in the found leaf.
1680 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001681 */
1682 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1683 if (err)
1684 goto cleanup;
1685 depth = ext_depth(inode);
1686 eh = path[depth].p_hdr;
1687
1688has_space:
1689 nearex = path[depth].p_ext;
1690
Avantika Mathur7e028972006-12-06 20:41:33 -08001691 err = ext4_ext_get_access(handle, inode, path + depth);
1692 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001693 goto cleanup;
1694
1695 if (!nearex) {
1696 /* there is no extent in this leaf, create first one */
Mingming553f9002009-09-18 13:34:55 -04001697 ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001698 le32_to_cpu(newext->ee_block),
1699 ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001700 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001701 ext4_ext_get_actual_len(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001702 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1703 } else if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001704 > le32_to_cpu(nearex->ee_block)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001705/* BUG_ON(newext->ee_block == nearex->ee_block); */
1706 if (nearex != EXT_LAST_EXTENT(eh)) {
1707 len = EXT_MAX_EXTENT(eh) - nearex;
1708 len = (len - 1) * sizeof(struct ext4_extent);
1709 len = len < 0 ? 0 : len;
Mingming553f9002009-09-18 13:34:55 -04001710 ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001711 "move %d from 0x%p to 0x%p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001712 le32_to_cpu(newext->ee_block),
1713 ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001714 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001715 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001716 nearex, len, nearex + 1, nearex + 2);
1717 memmove(nearex + 2, nearex + 1, len);
1718 }
1719 path[depth].p_ext = nearex + 1;
1720 } else {
1721 BUG_ON(newext->ee_block == nearex->ee_block);
1722 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1723 len = len < 0 ? 0 : len;
Mingming553f9002009-09-18 13:34:55 -04001724 ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001725 "move %d from 0x%p to 0x%p\n",
1726 le32_to_cpu(newext->ee_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001727 ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001728 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001729 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001730 nearex, len, nearex + 1, nearex + 2);
1731 memmove(nearex + 1, nearex, len);
1732 path[depth].p_ext = nearex;
1733 }
1734
Marcin Slusarze8546d02008-04-17 10:38:59 -04001735 le16_add_cpu(&eh->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001736 nearex = path[depth].p_ext;
1737 nearex->ee_block = newext->ee_block;
Aneesh Kumar K.Vb3776112007-10-16 18:38:25 -04001738 ext4_ext_store_pblock(nearex, ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001739 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001740
1741merge:
1742 /* try to merge extents to the right */
Mingming Cao00314622009-09-28 15:49:08 -04001743 if (flag != EXT4_GET_BLOCKS_DIO_CREATE_EXT)
1744 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001745
1746 /* try to merge extents to the left */
1747
1748 /* time to correct all indexes above */
1749 err = ext4_ext_correct_indexes(handle, inode, path);
1750 if (err)
1751 goto cleanup;
1752
1753 err = ext4_ext_dirty(handle, inode, path + depth);
1754
1755cleanup:
1756 if (npath) {
1757 ext4_ext_drop_refs(npath);
1758 kfree(npath);
1759 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001760 ext4_ext_invalidate_cache(inode);
1761 return err;
1762}
1763
Eric Sandeen6873fa02008-10-07 00:46:36 -04001764int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1765 ext4_lblk_t num, ext_prepare_callback func,
1766 void *cbdata)
1767{
1768 struct ext4_ext_path *path = NULL;
1769 struct ext4_ext_cache cbex;
1770 struct ext4_extent *ex;
1771 ext4_lblk_t next, start = 0, end = 0;
1772 ext4_lblk_t last = block + num;
1773 int depth, exists, err = 0;
1774
1775 BUG_ON(func == NULL);
1776 BUG_ON(inode == NULL);
1777
1778 while (block < last && block != EXT_MAX_BLOCK) {
1779 num = last - block;
1780 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001781 down_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001782 path = ext4_ext_find_extent(inode, block, path);
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001783 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001784 if (IS_ERR(path)) {
1785 err = PTR_ERR(path);
1786 path = NULL;
1787 break;
1788 }
1789
1790 depth = ext_depth(inode);
1791 BUG_ON(path[depth].p_hdr == NULL);
1792 ex = path[depth].p_ext;
1793 next = ext4_ext_next_allocated_block(path);
1794
1795 exists = 0;
1796 if (!ex) {
1797 /* there is no extent yet, so try to allocate
1798 * all requested space */
1799 start = block;
1800 end = block + num;
1801 } else if (le32_to_cpu(ex->ee_block) > block) {
1802 /* need to allocate space before found extent */
1803 start = block;
1804 end = le32_to_cpu(ex->ee_block);
1805 if (block + num < end)
1806 end = block + num;
1807 } else if (block >= le32_to_cpu(ex->ee_block)
1808 + ext4_ext_get_actual_len(ex)) {
1809 /* need to allocate space after found extent */
1810 start = block;
1811 end = block + num;
1812 if (end >= next)
1813 end = next;
1814 } else if (block >= le32_to_cpu(ex->ee_block)) {
1815 /*
1816 * some part of requested space is covered
1817 * by found extent
1818 */
1819 start = block;
1820 end = le32_to_cpu(ex->ee_block)
1821 + ext4_ext_get_actual_len(ex);
1822 if (block + num < end)
1823 end = block + num;
1824 exists = 1;
1825 } else {
1826 BUG();
1827 }
1828 BUG_ON(end <= start);
1829
1830 if (!exists) {
1831 cbex.ec_block = start;
1832 cbex.ec_len = end - start;
1833 cbex.ec_start = 0;
1834 cbex.ec_type = EXT4_EXT_CACHE_GAP;
1835 } else {
1836 cbex.ec_block = le32_to_cpu(ex->ee_block);
1837 cbex.ec_len = ext4_ext_get_actual_len(ex);
1838 cbex.ec_start = ext_pblock(ex);
1839 cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
1840 }
1841
1842 BUG_ON(cbex.ec_len == 0);
1843 err = func(inode, path, &cbex, ex, cbdata);
1844 ext4_ext_drop_refs(path);
1845
1846 if (err < 0)
1847 break;
1848
1849 if (err == EXT_REPEAT)
1850 continue;
1851 else if (err == EXT_BREAK) {
1852 err = 0;
1853 break;
1854 }
1855
1856 if (ext_depth(inode) != depth) {
1857 /* depth was changed. we have to realloc path */
1858 kfree(path);
1859 path = NULL;
1860 }
1861
1862 block = cbex.ec_block + cbex.ec_len;
1863 }
1864
1865 if (path) {
1866 ext4_ext_drop_refs(path);
1867 kfree(path);
1868 }
1869
1870 return err;
1871}
1872
Avantika Mathur09b88252006-12-06 20:41:36 -08001873static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001874ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Mingming Caodd545672007-07-31 00:37:46 -07001875 __u32 len, ext4_fsblk_t start, int type)
Alex Tomasa86c6182006-10-11 01:21:03 -07001876{
1877 struct ext4_ext_cache *cex;
1878 BUG_ON(len == 0);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001879 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001880 cex = &EXT4_I(inode)->i_cached_extent;
1881 cex->ec_type = type;
1882 cex->ec_block = block;
1883 cex->ec_len = len;
1884 cex->ec_start = start;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001885 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001886}
1887
1888/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001889 * ext4_ext_put_gap_in_cache:
1890 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07001891 * and cache this gap
1892 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001893static void
Alex Tomasa86c6182006-10-11 01:21:03 -07001894ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001895 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07001896{
1897 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001898 unsigned long len;
1899 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001900 struct ext4_extent *ex;
1901
1902 ex = path[depth].p_ext;
1903 if (ex == NULL) {
1904 /* there is no extent yet, so gap is [0;-] */
1905 lblock = 0;
1906 len = EXT_MAX_BLOCK;
1907 ext_debug("cache gap(whole file):");
1908 } else if (block < le32_to_cpu(ex->ee_block)) {
1909 lblock = block;
1910 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05001911 ext_debug("cache gap(before): %u [%u:%u]",
1912 block,
1913 le32_to_cpu(ex->ee_block),
1914 ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07001915 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001916 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001917 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001918 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001919 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001920
1921 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05001922 ext_debug("cache gap(after): [%u:%u] %u",
1923 le32_to_cpu(ex->ee_block),
1924 ext4_ext_get_actual_len(ex),
1925 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001926 BUG_ON(next == lblock);
1927 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001928 } else {
1929 lblock = len = 0;
1930 BUG();
1931 }
1932
Eric Sandeenbba90742008-01-28 23:58:27 -05001933 ext_debug(" -> %u:%lu\n", lblock, len);
Alex Tomasa86c6182006-10-11 01:21:03 -07001934 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1935}
1936
Avantika Mathur09b88252006-12-06 20:41:36 -08001937static int
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001938ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
Alex Tomasa86c6182006-10-11 01:21:03 -07001939 struct ext4_extent *ex)
1940{
1941 struct ext4_ext_cache *cex;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001942 int ret = EXT4_EXT_CACHE_NO;
Alex Tomasa86c6182006-10-11 01:21:03 -07001943
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001944 /*
1945 * We borrow i_block_reservation_lock to protect i_cached_extent
1946 */
1947 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001948 cex = &EXT4_I(inode)->i_cached_extent;
1949
1950 /* has cache valid data? */
1951 if (cex->ec_type == EXT4_EXT_CACHE_NO)
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001952 goto errout;
Alex Tomasa86c6182006-10-11 01:21:03 -07001953
1954 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1955 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1956 if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001957 ex->ee_block = cpu_to_le32(cex->ec_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001958 ext4_ext_store_pblock(ex, cex->ec_start);
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001959 ex->ee_len = cpu_to_le16(cex->ec_len);
Eric Sandeenbba90742008-01-28 23:58:27 -05001960 ext_debug("%u cached by %u:%u:%llu\n",
1961 block,
1962 cex->ec_block, cex->ec_len, cex->ec_start);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001963 ret = cex->ec_type;
Alex Tomasa86c6182006-10-11 01:21:03 -07001964 }
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001965errout:
1966 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1967 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07001968}
1969
1970/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001971 * ext4_ext_rm_idx:
1972 * removes index from the index block.
1973 * It's used in truncate case only, thus all requests are for
1974 * last index in the block only.
Alex Tomasa86c6182006-10-11 01:21:03 -07001975 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001976static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001977 struct ext4_ext_path *path)
1978{
Alex Tomasa86c6182006-10-11 01:21:03 -07001979 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001980 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07001981
1982 /* free index block */
1983 path--;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001984 leaf = idx_pblock(path->p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -07001985 BUG_ON(path->p_hdr->eh_entries == 0);
Avantika Mathur7e028972006-12-06 20:41:33 -08001986 err = ext4_ext_get_access(handle, inode, path);
1987 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001988 return err;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001989 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08001990 err = ext4_ext_dirty(handle, inode, path);
1991 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001992 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07001993 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Theodore Ts'oe6362602009-11-23 07:17:05 -05001994 ext4_free_blocks(handle, inode, 0, leaf, 1,
1995 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Alex Tomasa86c6182006-10-11 01:21:03 -07001996 return err;
1997}
1998
1999/*
Mingming Caoee12b632008-08-19 22:16:05 -04002000 * ext4_ext_calc_credits_for_single_extent:
2001 * This routine returns max. credits that needed to insert an extent
2002 * to the extent tree.
2003 * When pass the actual path, the caller should calculate credits
2004 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002005 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002006int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002007 struct ext4_ext_path *path)
2008{
Alex Tomasa86c6182006-10-11 01:21:03 -07002009 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002010 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002011 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002012
Alex Tomasa86c6182006-10-11 01:21:03 -07002013 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002014 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002015 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2016
2017 /*
2018 * There are some space in the leaf tree, no
2019 * need to account for leaf block credit
2020 *
2021 * bitmaps and block group descriptor blocks
2022 * and other metadat blocks still need to be
2023 * accounted.
2024 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002025 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002026 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002027 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002028 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002029 }
2030
Mingming Cao525f4ed2008-08-19 22:15:58 -04002031 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002032}
Alex Tomasa86c6182006-10-11 01:21:03 -07002033
Mingming Caoee12b632008-08-19 22:16:05 -04002034/*
2035 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2036 *
2037 * if nrblocks are fit in a single extent (chunk flag is 1), then
2038 * in the worse case, each tree level index/leaf need to be changed
2039 * if the tree split due to insert a new extent, then the old tree
2040 * index/leaf need to be updated too
2041 *
2042 * If the nrblocks are discontiguous, they could cause
2043 * the whole tree split more than once, but this is really rare.
2044 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002045int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoee12b632008-08-19 22:16:05 -04002046{
2047 int index;
2048 int depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002049
Mingming Caoee12b632008-08-19 22:16:05 -04002050 if (chunk)
2051 index = depth * 2;
2052 else
2053 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002054
Mingming Caoee12b632008-08-19 22:16:05 -04002055 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002056}
2057
2058static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2059 struct ext4_extent *ex,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002060 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002061{
Amit Aroraa2df2a62007-07-17 21:42:41 -04002062 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe6362602009-11-23 07:17:05 -05002063 int flags = EXT4_FREE_BLOCKS_FORGET;
Alex Tomasa86c6182006-10-11 01:21:03 -07002064
Alex Tomasc9de5602008-01-29 00:19:52 -05002065 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
Theodore Ts'oe6362602009-11-23 07:17:05 -05002066 flags |= EXT4_FREE_BLOCKS_METADATA;
Alex Tomasa86c6182006-10-11 01:21:03 -07002067#ifdef EXTENTS_STATS
2068 {
2069 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002070 spin_lock(&sbi->s_ext_stats_lock);
2071 sbi->s_ext_blocks += ee_len;
2072 sbi->s_ext_extents++;
2073 if (ee_len < sbi->s_ext_min)
2074 sbi->s_ext_min = ee_len;
2075 if (ee_len > sbi->s_ext_max)
2076 sbi->s_ext_max = ee_len;
2077 if (ext_depth(inode) > sbi->s_depth_max)
2078 sbi->s_depth_max = ext_depth(inode);
2079 spin_unlock(&sbi->s_ext_stats_lock);
2080 }
2081#endif
2082 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002083 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002084 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002085 ext4_lblk_t num;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002086 ext4_fsblk_t start;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002087
Amit Aroraa2df2a62007-07-17 21:42:41 -04002088 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2089 start = ext_pblock(ex) + ee_len - num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002090 ext_debug("free last %u blocks starting %llu\n", num, start);
Theodore Ts'oe6362602009-11-23 07:17:05 -05002091 ext4_free_blocks(handle, inode, 0, start, num, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07002092 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002093 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002094 printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
Amit Aroraa2df2a62007-07-17 21:42:41 -04002095 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002096 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002097 printk(KERN_INFO "strange request: removal(2) "
2098 "%u-%u from %u:%u\n",
2099 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002100 }
2101 return 0;
2102}
2103
2104static int
2105ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002106 struct ext4_ext_path *path, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07002107{
2108 int err = 0, correct_index = 0;
2109 int depth = ext_depth(inode), credits;
2110 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002111 ext4_lblk_t a, b, block;
2112 unsigned num;
2113 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002114 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002115 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002116 struct ext4_extent *ex;
2117
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002118 /* the header must be checked already in ext4_ext_remove_space() */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002119 ext_debug("truncate since %u in leaf\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002120 if (!path[depth].p_hdr)
2121 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2122 eh = path[depth].p_hdr;
2123 BUG_ON(eh == NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07002124
2125 /* find where to start removing */
2126 ex = EXT_LAST_EXTENT(eh);
2127
2128 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002129 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002130
2131 while (ex >= EXT_FIRST_EXTENT(eh) &&
2132 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002133
2134 if (ext4_ext_is_uninitialized(ex))
2135 uninitialized = 1;
2136 else
2137 uninitialized = 0;
2138
Mingming553f9002009-09-18 13:34:55 -04002139 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2140 uninitialized, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002141 path[depth].p_ext = ex;
2142
2143 a = ex_ee_block > start ? ex_ee_block : start;
2144 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
2145 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
2146
2147 ext_debug(" border %u:%u\n", a, b);
2148
2149 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
2150 block = 0;
2151 num = 0;
2152 BUG();
2153 } else if (a != ex_ee_block) {
2154 /* remove tail of the extent */
2155 block = ex_ee_block;
2156 num = a - block;
2157 } else if (b != ex_ee_block + ex_ee_len - 1) {
2158 /* remove head of the extent */
2159 block = a;
2160 num = b - a;
2161 /* there is no "make a hole" API yet */
2162 BUG();
2163 } else {
2164 /* remove whole extent: excellent! */
2165 block = ex_ee_block;
2166 num = 0;
2167 BUG_ON(a != ex_ee_block);
2168 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
2169 }
2170
Theodore Ts'o34071da2008-08-01 21:59:19 -04002171 /*
2172 * 3 for leaf, sb, and inode plus 2 (bmap and group
2173 * descriptor) for each block group; assume two block
2174 * groups plus ex_ee_len/blocks_per_block_group for
2175 * the worst case
2176 */
2177 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002178 if (ex == EXT_FIRST_EXTENT(eh)) {
2179 correct_index = 1;
2180 credits += (ext_depth(inode)) + 1;
2181 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002182 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002183
Jan Kara487caee2009-08-17 22:17:20 -04002184 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
Shen Feng9102e4f2008-07-11 19:27:31 -04002185 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002186 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002187
2188 err = ext4_ext_get_access(handle, inode, path + depth);
2189 if (err)
2190 goto out;
2191
2192 err = ext4_remove_blocks(handle, inode, ex, a, b);
2193 if (err)
2194 goto out;
2195
2196 if (num == 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002197 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002198 ext4_ext_store_pblock(ex, 0);
Marcin Slusarze8546d02008-04-17 10:38:59 -04002199 le16_add_cpu(&eh->eh_entries, -1);
Alex Tomasa86c6182006-10-11 01:21:03 -07002200 }
2201
2202 ex->ee_block = cpu_to_le32(block);
2203 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002204 /*
2205 * Do not mark uninitialized if all the blocks in the
2206 * extent have been removed.
2207 */
2208 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002209 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002210
2211 err = ext4_ext_dirty(handle, inode, path + depth);
2212 if (err)
2213 goto out;
2214
Mingming Cao2ae02102006-10-11 01:21:11 -07002215 ext_debug("new extent: %u:%u:%llu\n", block, num,
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002216 ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002217 ex--;
2218 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002219 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002220 }
2221
2222 if (correct_index && eh->eh_entries)
2223 err = ext4_ext_correct_indexes(handle, inode, path);
2224
2225 /* if this leaf is free, then we should
2226 * remove it from index block above */
2227 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2228 err = ext4_ext_rm_idx(handle, inode, path + depth);
2229
2230out:
2231 return err;
2232}
2233
2234/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002235 * ext4_ext_more_to_rm:
2236 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002237 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002238static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002239ext4_ext_more_to_rm(struct ext4_ext_path *path)
2240{
2241 BUG_ON(path->p_idx == NULL);
2242
2243 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2244 return 0;
2245
2246 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002247 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002248 * so we have to consider current index for truncation
2249 */
2250 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2251 return 0;
2252 return 1;
2253}
2254
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002255static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07002256{
2257 struct super_block *sb = inode->i_sb;
2258 int depth = ext_depth(inode);
2259 struct ext4_ext_path *path;
2260 handle_t *handle;
2261 int i = 0, err = 0;
2262
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002263 ext_debug("truncate since %u\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002264
2265 /* probably first extent we're gonna free will be last in block */
2266 handle = ext4_journal_start(inode, depth + 1);
2267 if (IS_ERR(handle))
2268 return PTR_ERR(handle);
2269
2270 ext4_ext_invalidate_cache(inode);
2271
2272 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002273 * We start scanning from right side, freeing all the blocks
2274 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002275 */
Josef Bacik216553c2008-04-29 22:02:02 -04002276 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07002277 if (path == NULL) {
2278 ext4_journal_stop(handle);
2279 return -ENOMEM;
2280 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002281 path[0].p_hdr = ext_inode_hdr(inode);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002282 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002283 err = -EIO;
2284 goto out;
2285 }
2286 path[0].p_depth = depth;
2287
2288 while (i >= 0 && err == 0) {
2289 if (i == depth) {
2290 /* this is leaf block */
2291 err = ext4_ext_rm_leaf(handle, inode, path, start);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002292 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002293 brelse(path[i].p_bh);
2294 path[i].p_bh = NULL;
2295 i--;
2296 continue;
2297 }
2298
2299 /* this is index block */
2300 if (!path[i].p_hdr) {
2301 ext_debug("initialize header\n");
2302 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002303 }
2304
Alex Tomasa86c6182006-10-11 01:21:03 -07002305 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002306 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002307 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2308 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2309 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2310 path[i].p_hdr,
2311 le16_to_cpu(path[i].p_hdr->eh_entries));
2312 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002313 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002314 path[i].p_idx--;
2315 }
2316
2317 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2318 i, EXT_FIRST_INDEX(path[i].p_hdr),
2319 path[i].p_idx);
2320 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002321 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002322 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002323 ext_debug("move to level %d (block %llu)\n",
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002324 i + 1, idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002325 memset(path + i + 1, 0, sizeof(*path));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002326 bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2327 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002328 /* should we reset i_size? */
2329 err = -EIO;
2330 break;
2331 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002332 if (WARN_ON(i + 1 > depth)) {
2333 err = -EIO;
2334 break;
2335 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002336 if (ext4_ext_check(inode, ext_block_hdr(bh),
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002337 depth - i - 1)) {
2338 err = -EIO;
2339 break;
2340 }
2341 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002342
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002343 /* save actual number of indexes since this
2344 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002345 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2346 i++;
2347 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002348 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002349 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002350 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002351 * handle must be already prepared by the
2352 * truncatei_leaf() */
2353 err = ext4_ext_rm_idx(handle, inode, path + i);
2354 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002355 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002356 brelse(path[i].p_bh);
2357 path[i].p_bh = NULL;
2358 i--;
2359 ext_debug("return to level %d\n", i);
2360 }
2361 }
2362
2363 /* TODO: flexible tree reduction should be here */
2364 if (path->p_hdr->eh_entries == 0) {
2365 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002366 * truncate to zero freed all the tree,
2367 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002368 */
2369 err = ext4_ext_get_access(handle, inode, path);
2370 if (err == 0) {
2371 ext_inode_hdr(inode)->eh_depth = 0;
2372 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04002373 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07002374 err = ext4_ext_dirty(handle, inode, path);
2375 }
2376 }
2377out:
Alex Tomasa86c6182006-10-11 01:21:03 -07002378 ext4_ext_drop_refs(path);
2379 kfree(path);
2380 ext4_journal_stop(handle);
2381
2382 return err;
2383}
2384
2385/*
2386 * called at mount time
2387 */
2388void ext4_ext_init(struct super_block *sb)
2389{
2390 /*
2391 * possible initialization would be here
2392 */
2393
Theodore Ts'o83982b62009-01-06 14:53:16 -05002394 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04002395#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002396 printk(KERN_INFO "EXT4-fs: file extents enabled");
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002397#ifdef AGGRESSIVE_TEST
2398 printk(", aggressive tests");
Alex Tomasa86c6182006-10-11 01:21:03 -07002399#endif
2400#ifdef CHECK_BINSEARCH
2401 printk(", check binsearch");
2402#endif
2403#ifdef EXTENTS_STATS
2404 printk(", stats");
2405#endif
2406 printk("\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04002407#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07002408#ifdef EXTENTS_STATS
2409 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2410 EXT4_SB(sb)->s_ext_min = 1 << 30;
2411 EXT4_SB(sb)->s_ext_max = 0;
2412#endif
2413 }
2414}
2415
2416/*
2417 * called at umount time
2418 */
2419void ext4_ext_release(struct super_block *sb)
2420{
Theodore Ts'o83982b62009-01-06 14:53:16 -05002421 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
Alex Tomasa86c6182006-10-11 01:21:03 -07002422 return;
2423
2424#ifdef EXTENTS_STATS
2425 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2426 struct ext4_sb_info *sbi = EXT4_SB(sb);
2427 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2428 sbi->s_ext_blocks, sbi->s_ext_extents,
2429 sbi->s_ext_blocks / sbi->s_ext_extents);
2430 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2431 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2432 }
2433#endif
2434}
2435
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002436static void bi_complete(struct bio *bio, int error)
2437{
2438 complete((struct completion *)bio->bi_private);
2439}
2440
2441/* FIXME!! we need to try to merge to left or right after zero-out */
2442static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2443{
2444 int ret = -EIO;
2445 struct bio *bio;
2446 int blkbits, blocksize;
2447 sector_t ee_pblock;
2448 struct completion event;
2449 unsigned int ee_len, len, done, offset;
2450
2451
2452 blkbits = inode->i_blkbits;
2453 blocksize = inode->i_sb->s_blocksize;
2454 ee_len = ext4_ext_get_actual_len(ex);
2455 ee_pblock = ext_pblock(ex);
2456
2457 /* convert ee_pblock to 512 byte sectors */
2458 ee_pblock = ee_pblock << (blkbits - 9);
2459
2460 while (ee_len > 0) {
2461
2462 if (ee_len > BIO_MAX_PAGES)
2463 len = BIO_MAX_PAGES;
2464 else
2465 len = ee_len;
2466
2467 bio = bio_alloc(GFP_NOIO, len);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002468 bio->bi_sector = ee_pblock;
2469 bio->bi_bdev = inode->i_sb->s_bdev;
2470
2471 done = 0;
2472 offset = 0;
2473 while (done < len) {
2474 ret = bio_add_page(bio, ZERO_PAGE(0),
2475 blocksize, offset);
2476 if (ret != blocksize) {
2477 /*
2478 * We can't add any more pages because of
2479 * hardware limitations. Start a new bio.
2480 */
2481 break;
2482 }
2483 done++;
2484 offset += blocksize;
2485 if (offset >= PAGE_CACHE_SIZE)
2486 offset = 0;
2487 }
2488
2489 init_completion(&event);
2490 bio->bi_private = &event;
2491 bio->bi_end_io = bi_complete;
2492 submit_bio(WRITE, bio);
2493 wait_for_completion(&event);
2494
2495 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
2496 ret = 0;
2497 else {
2498 ret = -EIO;
2499 break;
2500 }
2501 bio_put(bio);
2502 ee_len -= done;
2503 ee_pblock += done << (blkbits - 9);
2504 }
2505 return ret;
2506}
2507
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002508#define EXT4_EXT_ZERO_LEN 7
Amit Arora56055d32007-07-17 21:42:38 -04002509/*
2510 * This function is called by ext4_ext_get_blocks() if someone tries to write
2511 * to an uninitialized extent. It may result in splitting the uninitialized
2512 * extent into multiple extents (upto three - one initialized and two
2513 * uninitialized).
2514 * There are three possibilities:
2515 * a> There is no split required: Entire extent should be initialized
2516 * b> Splits in two extents: Write is happening at either end of the extent
2517 * c> Splits in three extents: Somone is writing in middle of the extent
2518 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002519static int ext4_ext_convert_to_initialized(handle_t *handle,
2520 struct inode *inode,
2521 struct ext4_ext_path *path,
2522 ext4_lblk_t iblock,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002523 unsigned int max_blocks)
Amit Arora56055d32007-07-17 21:42:38 -04002524{
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002525 struct ext4_extent *ex, newex, orig_ex;
Amit Arora56055d32007-07-17 21:42:38 -04002526 struct ext4_extent *ex1 = NULL;
2527 struct ext4_extent *ex2 = NULL;
2528 struct ext4_extent *ex3 = NULL;
2529 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002530 ext4_lblk_t ee_block;
2531 unsigned int allocated, ee_len, depth;
Amit Arora56055d32007-07-17 21:42:38 -04002532 ext4_fsblk_t newblock;
2533 int err = 0;
2534 int ret = 0;
2535
2536 depth = ext_depth(inode);
2537 eh = path[depth].p_hdr;
2538 ex = path[depth].p_ext;
2539 ee_block = le32_to_cpu(ex->ee_block);
2540 ee_len = ext4_ext_get_actual_len(ex);
2541 allocated = ee_len - (iblock - ee_block);
2542 newblock = iblock - ee_block + ext_pblock(ex);
2543 ex2 = ex;
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002544 orig_ex.ee_block = ex->ee_block;
2545 orig_ex.ee_len = cpu_to_le16(ee_len);
2546 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
Amit Arora56055d32007-07-17 21:42:38 -04002547
Aneesh Kumar K.V9df56432008-02-22 06:17:31 -05002548 err = ext4_ext_get_access(handle, inode, path + depth);
2549 if (err)
2550 goto out;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002551 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2552 if (ee_len <= 2*EXT4_EXT_ZERO_LEN) {
2553 err = ext4_ext_zeroout(inode, &orig_ex);
2554 if (err)
2555 goto fix_extent_len;
2556 /* update the extent length and mark as initialized */
2557 ex->ee_block = orig_ex.ee_block;
2558 ex->ee_len = orig_ex.ee_len;
2559 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2560 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002561 /* zeroed the full extent */
2562 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002563 }
Aneesh Kumar K.V9df56432008-02-22 06:17:31 -05002564
Amit Arora56055d32007-07-17 21:42:38 -04002565 /* ex1: ee_block to iblock - 1 : uninitialized */
2566 if (iblock > ee_block) {
2567 ex1 = ex;
2568 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2569 ext4_ext_mark_uninitialized(ex1);
2570 ex2 = &newex;
2571 }
2572 /*
2573 * for sanity, update the length of the ex2 extent before
2574 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2575 * overlap of blocks.
2576 */
2577 if (!ex1 && allocated > max_blocks)
2578 ex2->ee_len = cpu_to_le16(max_blocks);
2579 /* ex3: to ee_block + ee_len : uninitialised */
2580 if (allocated > max_blocks) {
2581 unsigned int newdepth;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002582 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
2583 if (allocated <= EXT4_EXT_ZERO_LEN) {
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002584 /*
2585 * iblock == ee_block is handled by the zerouout
2586 * at the beginning.
2587 * Mark first half uninitialized.
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002588 * Mark second half initialized and zero out the
2589 * initialized extent
2590 */
2591 ex->ee_block = orig_ex.ee_block;
2592 ex->ee_len = cpu_to_le16(ee_len - allocated);
2593 ext4_ext_mark_uninitialized(ex);
2594 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2595 ext4_ext_dirty(handle, inode, path + depth);
2596
2597 ex3 = &newex;
2598 ex3->ee_block = cpu_to_le32(iblock);
2599 ext4_ext_store_pblock(ex3, newblock);
2600 ex3->ee_len = cpu_to_le16(allocated);
Mingming Cao00314622009-09-28 15:49:08 -04002601 err = ext4_ext_insert_extent(handle, inode, path,
2602 ex3, 0);
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002603 if (err == -ENOSPC) {
2604 err = ext4_ext_zeroout(inode, &orig_ex);
2605 if (err)
2606 goto fix_extent_len;
2607 ex->ee_block = orig_ex.ee_block;
2608 ex->ee_len = orig_ex.ee_len;
2609 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2610 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002611 /* blocks available from iblock */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002612 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002613
2614 } else if (err)
2615 goto fix_extent_len;
2616
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002617 /*
2618 * We need to zero out the second half because
2619 * an fallocate request can update file size and
2620 * converting the second half to initialized extent
2621 * implies that we can leak some junk data to user
2622 * space.
2623 */
2624 err = ext4_ext_zeroout(inode, ex3);
2625 if (err) {
2626 /*
2627 * We should actually mark the
2628 * second half as uninit and return error
2629 * Insert would have changed the extent
2630 */
2631 depth = ext_depth(inode);
2632 ext4_ext_drop_refs(path);
2633 path = ext4_ext_find_extent(inode,
2634 iblock, path);
2635 if (IS_ERR(path)) {
2636 err = PTR_ERR(path);
2637 return err;
2638 }
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002639 /* get the second half extent details */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002640 ex = path[depth].p_ext;
2641 err = ext4_ext_get_access(handle, inode,
2642 path + depth);
2643 if (err)
2644 return err;
2645 ext4_ext_mark_uninitialized(ex);
2646 ext4_ext_dirty(handle, inode, path + depth);
2647 return err;
2648 }
2649
2650 /* zeroed the second half */
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002651 return allocated;
2652 }
Amit Arora56055d32007-07-17 21:42:38 -04002653 ex3 = &newex;
2654 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2655 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2656 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2657 ext4_ext_mark_uninitialized(ex3);
Mingming Cao00314622009-09-28 15:49:08 -04002658 err = ext4_ext_insert_extent(handle, inode, path, ex3, 0);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002659 if (err == -ENOSPC) {
2660 err = ext4_ext_zeroout(inode, &orig_ex);
2661 if (err)
2662 goto fix_extent_len;
2663 /* update the extent length and mark as initialized */
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002664 ex->ee_block = orig_ex.ee_block;
2665 ex->ee_len = orig_ex.ee_len;
2666 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002667 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002668 /* zeroed the full extent */
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002669 /* blocks available from iblock */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002670 return allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002671
2672 } else if (err)
2673 goto fix_extent_len;
Amit Arora56055d32007-07-17 21:42:38 -04002674 /*
2675 * The depth, and hence eh & ex might change
2676 * as part of the insert above.
2677 */
2678 newdepth = ext_depth(inode);
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002679 /*
Coly Li73ac36e2009-01-07 18:09:16 -08002680 * update the extent length after successful insert of the
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002681 * split extent
2682 */
2683 orig_ex.ee_len = cpu_to_le16(ee_len -
2684 ext4_ext_get_actual_len(ex3));
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002685 depth = newdepth;
2686 ext4_ext_drop_refs(path);
2687 path = ext4_ext_find_extent(inode, iblock, path);
2688 if (IS_ERR(path)) {
2689 err = PTR_ERR(path);
2690 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04002691 }
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002692 eh = path[depth].p_hdr;
2693 ex = path[depth].p_ext;
2694 if (ex2 != &newex)
2695 ex2 = ex;
2696
2697 err = ext4_ext_get_access(handle, inode, path + depth);
2698 if (err)
2699 goto out;
2700
Amit Arora56055d32007-07-17 21:42:38 -04002701 allocated = max_blocks;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002702
2703 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2704 * to insert a extent in the middle zerout directly
2705 * otherwise give the extent a chance to merge to left
2706 */
2707 if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
2708 iblock != ee_block) {
2709 err = ext4_ext_zeroout(inode, &orig_ex);
2710 if (err)
2711 goto fix_extent_len;
2712 /* update the extent length and mark as initialized */
2713 ex->ee_block = orig_ex.ee_block;
2714 ex->ee_len = orig_ex.ee_len;
2715 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2716 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002717 /* zero out the first half */
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002718 /* blocks available from iblock */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002719 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002720 }
Amit Arora56055d32007-07-17 21:42:38 -04002721 }
2722 /*
2723 * If there was a change of depth as part of the
2724 * insertion of ex3 above, we need to update the length
2725 * of the ex1 extent again here
2726 */
2727 if (ex1 && ex1 != ex) {
2728 ex1 = ex;
2729 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2730 ext4_ext_mark_uninitialized(ex1);
2731 ex2 = &newex;
2732 }
2733 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2734 ex2->ee_block = cpu_to_le32(iblock);
Amit Arora56055d32007-07-17 21:42:38 -04002735 ext4_ext_store_pblock(ex2, newblock);
2736 ex2->ee_len = cpu_to_le16(allocated);
2737 if (ex2 != ex)
2738 goto insert;
Amit Arora56055d32007-07-17 21:42:38 -04002739 /*
2740 * New (initialized) extent starts from the first block
2741 * in the current extent. i.e., ex2 == ex
2742 * We have to see if it can be merged with the extent
2743 * on the left.
2744 */
2745 if (ex2 > EXT_FIRST_EXTENT(eh)) {
2746 /*
2747 * To merge left, pass "ex2 - 1" to try_to_merge(),
2748 * since it merges towards right _only_.
2749 */
2750 ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
2751 if (ret) {
2752 err = ext4_ext_correct_indexes(handle, inode, path);
2753 if (err)
2754 goto out;
2755 depth = ext_depth(inode);
2756 ex2--;
2757 }
2758 }
2759 /*
2760 * Try to Merge towards right. This might be required
2761 * only when the whole extent is being written to.
2762 * i.e. ex2 == ex and ex3 == NULL.
2763 */
2764 if (!ex3) {
2765 ret = ext4_ext_try_to_merge(inode, path, ex2);
2766 if (ret) {
2767 err = ext4_ext_correct_indexes(handle, inode, path);
2768 if (err)
2769 goto out;
2770 }
2771 }
2772 /* Mark modified extent as dirty */
2773 err = ext4_ext_dirty(handle, inode, path + depth);
2774 goto out;
2775insert:
Mingming Cao00314622009-09-28 15:49:08 -04002776 err = ext4_ext_insert_extent(handle, inode, path, &newex, 0);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002777 if (err == -ENOSPC) {
2778 err = ext4_ext_zeroout(inode, &orig_ex);
2779 if (err)
2780 goto fix_extent_len;
2781 /* update the extent length and mark as initialized */
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002782 ex->ee_block = orig_ex.ee_block;
2783 ex->ee_len = orig_ex.ee_len;
2784 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002785 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002786 /* zero out the first half */
2787 return allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002788 } else if (err)
2789 goto fix_extent_len;
Amit Arora56055d32007-07-17 21:42:38 -04002790out:
Mingming553f9002009-09-18 13:34:55 -04002791 ext4_ext_show_leaf(inode, path);
Amit Arora56055d32007-07-17 21:42:38 -04002792 return err ? err : allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002793
2794fix_extent_len:
2795 ex->ee_block = orig_ex.ee_block;
2796 ex->ee_len = orig_ex.ee_len;
2797 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2798 ext4_ext_mark_uninitialized(ex);
2799 ext4_ext_dirty(handle, inode, path + depth);
2800 return err;
Amit Arora56055d32007-07-17 21:42:38 -04002801}
2802
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05002803/*
Mingming Cao00314622009-09-28 15:49:08 -04002804 * This function is called by ext4_ext_get_blocks() from
2805 * ext4_get_blocks_dio_write() when DIO to write
2806 * to an uninitialized extent.
2807 *
2808 * Writing to an uninitized extent may result in splitting the uninitialized
2809 * extent into multiple /intialized unintialized extents (up to three)
2810 * There are three possibilities:
2811 * a> There is no split required: Entire extent should be uninitialized
2812 * b> Splits in two extents: Write is happening at either end of the extent
2813 * c> Splits in three extents: Somone is writing in middle of the extent
2814 *
2815 * One of more index blocks maybe needed if the extent tree grow after
2816 * the unintialized extent split. To prevent ENOSPC occur at the IO
2817 * complete, we need to split the uninitialized extent before DIO submit
2818 * the IO. The uninitilized extent called at this time will be split
2819 * into three uninitialized extent(at most). After IO complete, the part
2820 * being filled will be convert to initialized by the end_io callback function
2821 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05002822 *
2823 * Returns the size of uninitialized extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04002824 */
2825static int ext4_split_unwritten_extents(handle_t *handle,
2826 struct inode *inode,
2827 struct ext4_ext_path *path,
2828 ext4_lblk_t iblock,
2829 unsigned int max_blocks,
2830 int flags)
2831{
2832 struct ext4_extent *ex, newex, orig_ex;
2833 struct ext4_extent *ex1 = NULL;
2834 struct ext4_extent *ex2 = NULL;
2835 struct ext4_extent *ex3 = NULL;
2836 struct ext4_extent_header *eh;
2837 ext4_lblk_t ee_block;
2838 unsigned int allocated, ee_len, depth;
2839 ext4_fsblk_t newblock;
2840 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04002841
2842 ext_debug("ext4_split_unwritten_extents: inode %lu,"
2843 "iblock %llu, max_blocks %u\n", inode->i_ino,
2844 (unsigned long long)iblock, max_blocks);
2845 depth = ext_depth(inode);
2846 eh = path[depth].p_hdr;
2847 ex = path[depth].p_ext;
2848 ee_block = le32_to_cpu(ex->ee_block);
2849 ee_len = ext4_ext_get_actual_len(ex);
2850 allocated = ee_len - (iblock - ee_block);
2851 newblock = iblock - ee_block + ext_pblock(ex);
2852 ex2 = ex;
2853 orig_ex.ee_block = ex->ee_block;
2854 orig_ex.ee_len = cpu_to_le16(ee_len);
2855 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
2856
2857 /*
Mingmingba230c32009-11-06 04:01:23 -05002858 * If the uninitialized extent begins at the same logical
2859 * block where the write begins, and the write completely
2860 * covers the extent, then we don't need to split it.
Mingming Cao00314622009-09-28 15:49:08 -04002861 */
Mingmingba230c32009-11-06 04:01:23 -05002862 if ((iblock == ee_block) && (allocated <= max_blocks))
2863 return allocated;
Mingming Cao00314622009-09-28 15:49:08 -04002864
2865 err = ext4_ext_get_access(handle, inode, path + depth);
2866 if (err)
2867 goto out;
2868 /* ex1: ee_block to iblock - 1 : uninitialized */
2869 if (iblock > ee_block) {
2870 ex1 = ex;
2871 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2872 ext4_ext_mark_uninitialized(ex1);
2873 ex2 = &newex;
2874 }
2875 /*
2876 * for sanity, update the length of the ex2 extent before
2877 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2878 * overlap of blocks.
2879 */
2880 if (!ex1 && allocated > max_blocks)
2881 ex2->ee_len = cpu_to_le16(max_blocks);
2882 /* ex3: to ee_block + ee_len : uninitialised */
2883 if (allocated > max_blocks) {
2884 unsigned int newdepth;
2885 ex3 = &newex;
2886 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2887 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2888 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2889 ext4_ext_mark_uninitialized(ex3);
2890 err = ext4_ext_insert_extent(handle, inode, path, ex3, flags);
2891 if (err == -ENOSPC) {
2892 err = ext4_ext_zeroout(inode, &orig_ex);
2893 if (err)
2894 goto fix_extent_len;
2895 /* update the extent length and mark as initialized */
2896 ex->ee_block = orig_ex.ee_block;
2897 ex->ee_len = orig_ex.ee_len;
2898 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2899 ext4_ext_dirty(handle, inode, path + depth);
2900 /* zeroed the full extent */
2901 /* blocks available from iblock */
2902 return allocated;
2903
2904 } else if (err)
2905 goto fix_extent_len;
2906 /*
2907 * The depth, and hence eh & ex might change
2908 * as part of the insert above.
2909 */
2910 newdepth = ext_depth(inode);
2911 /*
2912 * update the extent length after successful insert of the
2913 * split extent
2914 */
2915 orig_ex.ee_len = cpu_to_le16(ee_len -
2916 ext4_ext_get_actual_len(ex3));
2917 depth = newdepth;
2918 ext4_ext_drop_refs(path);
2919 path = ext4_ext_find_extent(inode, iblock, path);
2920 if (IS_ERR(path)) {
2921 err = PTR_ERR(path);
2922 goto out;
2923 }
2924 eh = path[depth].p_hdr;
2925 ex = path[depth].p_ext;
2926 if (ex2 != &newex)
2927 ex2 = ex;
2928
2929 err = ext4_ext_get_access(handle, inode, path + depth);
2930 if (err)
2931 goto out;
2932
2933 allocated = max_blocks;
2934 }
2935 /*
2936 * If there was a change of depth as part of the
2937 * insertion of ex3 above, we need to update the length
2938 * of the ex1 extent again here
2939 */
2940 if (ex1 && ex1 != ex) {
2941 ex1 = ex;
2942 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2943 ext4_ext_mark_uninitialized(ex1);
2944 ex2 = &newex;
2945 }
2946 /*
2947 * ex2: iblock to iblock + maxblocks-1 : to be direct IO written,
2948 * uninitialised still.
2949 */
2950 ex2->ee_block = cpu_to_le32(iblock);
2951 ext4_ext_store_pblock(ex2, newblock);
2952 ex2->ee_len = cpu_to_le16(allocated);
2953 ext4_ext_mark_uninitialized(ex2);
2954 if (ex2 != ex)
2955 goto insert;
2956 /* Mark modified extent as dirty */
2957 err = ext4_ext_dirty(handle, inode, path + depth);
2958 ext_debug("out here\n");
2959 goto out;
2960insert:
2961 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
2962 if (err == -ENOSPC) {
2963 err = ext4_ext_zeroout(inode, &orig_ex);
2964 if (err)
2965 goto fix_extent_len;
2966 /* update the extent length and mark as initialized */
2967 ex->ee_block = orig_ex.ee_block;
2968 ex->ee_len = orig_ex.ee_len;
2969 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2970 ext4_ext_dirty(handle, inode, path + depth);
2971 /* zero out the first half */
2972 return allocated;
2973 } else if (err)
2974 goto fix_extent_len;
2975out:
2976 ext4_ext_show_leaf(inode, path);
2977 return err ? err : allocated;
2978
2979fix_extent_len:
2980 ex->ee_block = orig_ex.ee_block;
2981 ex->ee_len = orig_ex.ee_len;
2982 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2983 ext4_ext_mark_uninitialized(ex);
2984 ext4_ext_dirty(handle, inode, path + depth);
2985 return err;
2986}
2987static int ext4_convert_unwritten_extents_dio(handle_t *handle,
2988 struct inode *inode,
2989 struct ext4_ext_path *path)
2990{
2991 struct ext4_extent *ex;
2992 struct ext4_extent_header *eh;
2993 int depth;
2994 int err = 0;
2995 int ret = 0;
2996
2997 depth = ext_depth(inode);
2998 eh = path[depth].p_hdr;
2999 ex = path[depth].p_ext;
3000
3001 err = ext4_ext_get_access(handle, inode, path + depth);
3002 if (err)
3003 goto out;
3004 /* first mark the extent as initialized */
3005 ext4_ext_mark_initialized(ex);
3006
3007 /*
3008 * We have to see if it can be merged with the extent
3009 * on the left.
3010 */
3011 if (ex > EXT_FIRST_EXTENT(eh)) {
3012 /*
3013 * To merge left, pass "ex - 1" to try_to_merge(),
3014 * since it merges towards right _only_.
3015 */
3016 ret = ext4_ext_try_to_merge(inode, path, ex - 1);
3017 if (ret) {
3018 err = ext4_ext_correct_indexes(handle, inode, path);
3019 if (err)
3020 goto out;
3021 depth = ext_depth(inode);
3022 ex--;
3023 }
3024 }
3025 /*
3026 * Try to Merge towards right.
3027 */
3028 ret = ext4_ext_try_to_merge(inode, path, ex);
3029 if (ret) {
3030 err = ext4_ext_correct_indexes(handle, inode, path);
3031 if (err)
3032 goto out;
3033 depth = ext_depth(inode);
3034 }
3035 /* Mark modified extent as dirty */
3036 err = ext4_ext_dirty(handle, inode, path + depth);
3037out:
3038 ext4_ext_show_leaf(inode, path);
3039 return err;
3040}
3041
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003042static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3043 sector_t block, int count)
3044{
3045 int i;
3046 for (i = 0; i < count; i++)
3047 unmap_underlying_metadata(bdev, block + i);
3048}
3049
Mingming Cao00314622009-09-28 15:49:08 -04003050static int
3051ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
3052 ext4_lblk_t iblock, unsigned int max_blocks,
3053 struct ext4_ext_path *path, int flags,
3054 unsigned int allocated, struct buffer_head *bh_result,
3055 ext4_fsblk_t newblock)
3056{
3057 int ret = 0;
3058 int err = 0;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003059 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Mingming Cao00314622009-09-28 15:49:08 -04003060
3061 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3062 "block %llu, max_blocks %u, flags %d, allocated %u",
3063 inode->i_ino, (unsigned long long)iblock, max_blocks,
3064 flags, allocated);
3065 ext4_ext_show_leaf(inode, path);
3066
3067 /* DIO get_block() before submit the IO, split the extent */
3068 if (flags == EXT4_GET_BLOCKS_DIO_CREATE_EXT) {
3069 ret = ext4_split_unwritten_extents(handle,
3070 inode, path, iblock,
3071 max_blocks, flags);
Mingming5f524952009-11-10 10:48:04 -05003072 /*
3073 * Flag the inode(non aio case) or end_io struct (aio case)
3074 * that this IO needs to convertion to written when IO is
3075 * completed
3076 */
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003077 if (io)
3078 io->flag = DIO_AIO_UNWRITTEN;
Mingming5f524952009-11-10 10:48:04 -05003079 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003080 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
Mingming Cao00314622009-09-28 15:49:08 -04003081 goto out;
3082 }
Mingming5f524952009-11-10 10:48:04 -05003083 /* async DIO end_io complete, convert the filled extent to written */
Mingming Cao00314622009-09-28 15:49:08 -04003084 if (flags == EXT4_GET_BLOCKS_DIO_CONVERT_EXT) {
3085 ret = ext4_convert_unwritten_extents_dio(handle, inode,
3086 path);
Jan Karab436b9b2009-12-08 23:51:10 -05003087 if (ret >= 0)
3088 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04003089 goto out2;
3090 }
3091 /* buffered IO case */
3092 /*
3093 * repeat fallocate creation request
3094 * we already have an unwritten extent
3095 */
3096 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3097 goto map_out;
3098
3099 /* buffered READ or buffered write_begin() lookup */
3100 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3101 /*
3102 * We have blocks reserved already. We
3103 * return allocated blocks so that delalloc
3104 * won't do block reservation for us. But
3105 * the buffer head will be unmapped so that
3106 * a read from the block returns 0s.
3107 */
3108 set_buffer_unwritten(bh_result);
3109 goto out1;
3110 }
3111
3112 /* buffered write, writepage time, convert*/
3113 ret = ext4_ext_convert_to_initialized(handle, inode,
3114 path, iblock,
3115 max_blocks);
Jan Karab436b9b2009-12-08 23:51:10 -05003116 if (ret >= 0)
3117 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04003118out:
3119 if (ret <= 0) {
3120 err = ret;
3121 goto out2;
3122 } else
3123 allocated = ret;
3124 set_buffer_new(bh_result);
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003125 /*
3126 * if we allocated more blocks than requested
3127 * we need to make sure we unmap the extra block
3128 * allocated. The actual needed block will get
3129 * unmapped later when we find the buffer_head marked
3130 * new.
3131 */
3132 if (allocated > max_blocks) {
3133 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
3134 newblock + max_blocks,
3135 allocated - max_blocks);
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003136 allocated = max_blocks;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003137 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003138
3139 /*
3140 * If we have done fallocate with the offset that is already
3141 * delayed allocated, we would have block reservation
3142 * and quota reservation done in the delayed write path.
3143 * But fallocate would have already updated quota and block
3144 * count for this offset. So cancel these reservation
3145 */
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05003146 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003147 ext4_da_update_reserve_space(inode, allocated, 0);
3148
Mingming Cao00314622009-09-28 15:49:08 -04003149map_out:
3150 set_buffer_mapped(bh_result);
3151out1:
3152 if (allocated > max_blocks)
3153 allocated = max_blocks;
3154 ext4_ext_show_leaf(inode, path);
3155 bh_result->b_bdev = inode->i_sb->s_bdev;
3156 bh_result->b_blocknr = newblock;
3157out2:
3158 if (path) {
3159 ext4_ext_drop_refs(path);
3160 kfree(path);
3161 }
3162 return err ? err : allocated;
3163}
3164/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05003165 * Block allocation/map/preallocation routine for extents based files
3166 *
3167 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003168 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003169 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3170 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05003171 *
3172 * return > 0, number of of blocks already mapped/allocated
3173 * if create == 0 and these are pre-allocated blocks
3174 * buffer head is unmapped
3175 * otherwise blocks are mapped
3176 *
3177 * return = 0, if plain look up failed (blocks have not been allocated)
3178 * buffer head is unmapped
3179 *
3180 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003181 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07003182int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003183 ext4_lblk_t iblock,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003184 unsigned int max_blocks, struct buffer_head *bh_result,
Theodore Ts'oc2177052009-05-14 00:58:52 -04003185 int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07003186{
3187 struct ext4_ext_path *path = NULL;
Amit Arora56055d32007-07-17 21:42:38 -04003188 struct ext4_extent_header *eh;
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003189 struct ext4_extent newex, *ex, *last_ex;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003190 ext4_fsblk_t newblock;
3191 int err = 0, depth, ret, cache_type;
3192 unsigned int allocated = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003193 struct ext4_allocation_request ar;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003194 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Alex Tomasa86c6182006-10-11 01:21:03 -07003195
3196 __clear_bit(BH_New, &bh_result->b_state);
Mingming84fe3be2009-09-01 08:44:37 -04003197 ext_debug("blocks %u/%u requested for inode %lu\n",
Eric Sandeenbba90742008-01-28 23:58:27 -05003198 iblock, max_blocks, inode->i_ino);
Alex Tomasa86c6182006-10-11 01:21:03 -07003199
3200 /* check in cache */
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003201 cache_type = ext4_ext_in_cache(inode, iblock, &newex);
3202 if (cache_type) {
3203 if (cache_type == EXT4_EXT_CACHE_GAP) {
Theodore Ts'oc2177052009-05-14 00:58:52 -04003204 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003205 /*
3206 * block isn't allocated yet and
3207 * user doesn't want to allocate it
3208 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003209 goto out2;
3210 }
3211 /* we should allocate requested block */
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003212 } else if (cache_type == EXT4_EXT_CACHE_EXTENT) {
Alex Tomasa86c6182006-10-11 01:21:03 -07003213 /* block is already allocated */
Dave Kleikamp8c55e202007-05-24 13:04:54 -04003214 newblock = iblock
3215 - le32_to_cpu(newex.ee_block)
3216 + ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003217 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003218 allocated = ext4_ext_get_actual_len(&newex) -
Alex Tomasa86c6182006-10-11 01:21:03 -07003219 (iblock - le32_to_cpu(newex.ee_block));
3220 goto out;
3221 } else {
3222 BUG();
3223 }
3224 }
3225
3226 /* find extent for this block */
3227 path = ext4_ext_find_extent(inode, iblock, NULL);
3228 if (IS_ERR(path)) {
3229 err = PTR_ERR(path);
3230 path = NULL;
3231 goto out2;
3232 }
3233
3234 depth = ext_depth(inode);
3235
3236 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003237 * consistent leaf must not be empty;
3238 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07003239 * this is why assert can't be put in ext4_ext_find_extent()
3240 */
Surbhi Palande034fb4c2009-12-14 09:53:52 -05003241 if (path[depth].p_ext == NULL && depth != 0) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05003242 ext4_error(inode->i_sb, "bad extent address "
Surbhi Palande034fb4c2009-12-14 09:53:52 -05003243 "inode: %lu, iblock: %d, depth: %d",
3244 inode->i_ino, iblock, depth);
3245 err = -EIO;
3246 goto out2;
3247 }
Amit Arora56055d32007-07-17 21:42:38 -04003248 eh = path[depth].p_hdr;
Alex Tomasa86c6182006-10-11 01:21:03 -07003249
Avantika Mathur7e028972006-12-06 20:41:33 -08003250 ex = path[depth].p_ext;
3251 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003252 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07003253 ext4_fsblk_t ee_start = ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003254 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003255
3256 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003257 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04003258 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003259 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003260 ee_len = ext4_ext_get_actual_len(ex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003261 /* if found extent covers block, simply return it */
Dave Kleikamp8c55e202007-05-24 13:04:54 -04003262 if (iblock >= ee_block && iblock < ee_block + ee_len) {
Alex Tomasa86c6182006-10-11 01:21:03 -07003263 newblock = iblock - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003264 /* number of remaining blocks in the extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07003265 allocated = ee_len - (iblock - ee_block);
Mingming84fe3be2009-09-01 08:44:37 -04003266 ext_debug("%u fit into %u:%d -> %llu\n", iblock,
Alex Tomasa86c6182006-10-11 01:21:03 -07003267 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04003268
Amit Aroraa2df2a62007-07-17 21:42:41 -04003269 /* Do not put uninitialized extent in the cache */
Amit Arora56055d32007-07-17 21:42:38 -04003270 if (!ext4_ext_is_uninitialized(ex)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04003271 ext4_ext_put_in_cache(inode, ee_block,
3272 ee_len, ee_start,
3273 EXT4_EXT_CACHE_EXTENT);
Amit Arora56055d32007-07-17 21:42:38 -04003274 goto out;
3275 }
Mingming Cao00314622009-09-28 15:49:08 -04003276 ret = ext4_ext_handle_uninitialized_extents(handle,
3277 inode, iblock, max_blocks, path,
3278 flags, allocated, bh_result, newblock);
3279 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07003280 }
3281 }
3282
3283 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003284 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07003285 * we couldn't try to create block if create flag is zero
3286 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04003287 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003288 /*
3289 * put just found gap into cache to speed up
3290 * subsequent requests
3291 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003292 ext4_ext_put_gap_in_cache(inode, path, iblock);
3293 goto out2;
3294 }
3295 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003296 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07003297 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003298
Alex Tomasc9de5602008-01-29 00:19:52 -05003299 /* find neighbour allocated blocks */
3300 ar.lleft = iblock;
3301 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3302 if (err)
3303 goto out2;
3304 ar.lright = iblock;
3305 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
3306 if (err)
3307 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04003308
Amit Arora749269f2007-07-18 09:02:56 -04003309 /*
3310 * See if request is beyond maximum number of blocks we can have in
3311 * a single extent. For an initialized extent this limit is
3312 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3313 * EXT_UNINIT_MAX_LEN.
3314 */
3315 if (max_blocks > EXT_INIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003316 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Amit Arora749269f2007-07-18 09:02:56 -04003317 max_blocks = EXT_INIT_MAX_LEN;
3318 else if (max_blocks > EXT_UNINIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003319 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Amit Arora749269f2007-07-18 09:02:56 -04003320 max_blocks = EXT_UNINIT_MAX_LEN;
3321
Amit Arora25d14f92007-05-24 13:04:13 -04003322 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
3323 newex.ee_block = cpu_to_le32(iblock);
3324 newex.ee_len = cpu_to_le16(max_blocks);
3325 err = ext4_ext_check_overlap(inode, &newex, path);
3326 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003327 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04003328 else
3329 allocated = max_blocks;
Alex Tomasc9de5602008-01-29 00:19:52 -05003330
3331 /* allocate new block */
3332 ar.inode = inode;
3333 ar.goal = ext4_ext_find_goal(inode, path, iblock);
3334 ar.logical = iblock;
3335 ar.len = allocated;
3336 if (S_ISREG(inode->i_mode))
3337 ar.flags = EXT4_MB_HINT_DATA;
3338 else
3339 /* disable in-core preallocation for non-regular files */
3340 ar.flags = 0;
3341 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07003342 if (!newblock)
3343 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04003344 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003345 ar.goal, newblock, allocated);
Alex Tomasa86c6182006-10-11 01:21:03 -07003346
3347 /* try to insert new extent into found leaf and return */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07003348 ext4_ext_store_pblock(&newex, newblock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003349 newex.ee_len = cpu_to_le16(ar.len);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003350 /* Mark uninitialized */
3351 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
Amit Aroraa2df2a62007-07-17 21:42:41 -04003352 ext4_ext_mark_uninitialized(&newex);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003353 /*
3354 * io_end structure was created for every async
3355 * direct IO write to the middle of the file.
3356 * To avoid unecessary convertion for every aio dio rewrite
3357 * to the mid of file, here we flag the IO that is really
3358 * need the convertion.
Mingming5f524952009-11-10 10:48:04 -05003359 * For non asycn direct IO case, flag the inode state
3360 * that we need to perform convertion when IO is done.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003361 */
Mingming5f524952009-11-10 10:48:04 -05003362 if (flags == EXT4_GET_BLOCKS_DIO_CREATE_EXT) {
3363 if (io)
3364 io->flag = DIO_AIO_UNWRITTEN;
3365 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003366 ext4_set_inode_state(inode,
3367 EXT4_STATE_DIO_UNWRITTEN);
Mingming5f524952009-11-10 10:48:04 -05003368 }
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003369 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003370
3371 if (unlikely(EXT4_I(inode)->i_flags & EXT4_EOFBLOCKS_FL)) {
3372 if (eh->eh_entries) {
3373 last_ex = EXT_LAST_EXTENT(eh);
3374 if (iblock + ar.len > le32_to_cpu(last_ex->ee_block)
3375 + ext4_ext_get_actual_len(last_ex))
3376 EXT4_I(inode)->i_flags &= ~EXT4_EOFBLOCKS_FL;
3377 } else {
3378 WARN_ON(eh->eh_entries == 0);
3379 ext4_error(inode->i_sb, __func__,
3380 "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
3381 }
3382 }
Mingming Cao00314622009-09-28 15:49:08 -04003383 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
Alex Tomas315054f2007-05-24 13:04:25 -04003384 if (err) {
3385 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05003386 /* not a good idea to call discard here directly,
3387 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003388 ext4_discard_preallocations(inode);
Theodore Ts'oe6362602009-11-23 07:17:05 -05003389 ext4_free_blocks(handle, inode, 0, ext_pblock(&newex),
3390 ext4_ext_get_actual_len(&newex), 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07003391 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04003392 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003393
Alex Tomasa86c6182006-10-11 01:21:03 -07003394 /* previous routine could use block we allocated */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07003395 newblock = ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003396 allocated = ext4_ext_get_actual_len(&newex);
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003397 if (allocated > max_blocks)
3398 allocated = max_blocks;
Eric Sandeen953e6222008-07-11 19:27:31 -04003399 set_buffer_new(bh_result);
Alex Tomasa86c6182006-10-11 01:21:03 -07003400
Jan Karab436b9b2009-12-08 23:51:10 -05003401 /*
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003402 * Update reserved blocks/metadata blocks after successful
3403 * block allocation which had been deferred till now.
3404 */
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05003405 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003406 ext4_da_update_reserve_space(inode, allocated, 1);
3407
3408 /*
Jan Karab436b9b2009-12-08 23:51:10 -05003409 * Cache the extent and update transaction to commit on fdatasync only
3410 * when it is _not_ an uninitialized extent.
3411 */
3412 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04003413 ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
3414 EXT4_EXT_CACHE_EXTENT);
Jan Karab436b9b2009-12-08 23:51:10 -05003415 ext4_update_inode_fsync_trans(handle, inode, 1);
3416 } else
3417 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07003418out:
3419 if (allocated > max_blocks)
3420 allocated = max_blocks;
3421 ext4_ext_show_leaf(inode, path);
Eric Sandeen953e6222008-07-11 19:27:31 -04003422 set_buffer_mapped(bh_result);
Alex Tomasa86c6182006-10-11 01:21:03 -07003423 bh_result->b_bdev = inode->i_sb->s_bdev;
3424 bh_result->b_blocknr = newblock;
3425out2:
3426 if (path) {
3427 ext4_ext_drop_refs(path);
3428 kfree(path);
3429 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003430 return err ? err : allocated;
3431}
3432
Jan Karacf108bc2008-07-11 19:27:31 -04003433void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07003434{
3435 struct address_space *mapping = inode->i_mapping;
3436 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003437 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07003438 handle_t *handle;
3439 int err = 0;
3440
3441 /*
3442 * probably first extent we're gonna free will be last in block
3443 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04003444 err = ext4_writepage_trans_blocks(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07003445 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04003446 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07003447 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07003448
Jan Karacf108bc2008-07-11 19:27:31 -04003449 if (inode->i_size & (sb->s_blocksize - 1))
3450 ext4_block_truncate_page(handle, mapping, inode->i_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07003451
Jan Kara9ddfc3d2008-07-11 19:27:31 -04003452 if (ext4_orphan_add(handle, inode))
3453 goto out_stop;
3454
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003455 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07003456 ext4_ext_invalidate_cache(inode);
3457
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003458 ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003459
Alex Tomasa86c6182006-10-11 01:21:03 -07003460 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003461 * TODO: optimization is possible here.
3462 * Probably we need not scan at all,
3463 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07003464 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003465
3466 /* we have to know where to truncate from in crash case */
3467 EXT4_I(inode)->i_disksize = inode->i_size;
3468 ext4_mark_inode_dirty(handle, inode);
3469
3470 last_block = (inode->i_size + sb->s_blocksize - 1)
3471 >> EXT4_BLOCK_SIZE_BITS(sb);
3472 err = ext4_ext_remove_space(inode, last_block);
3473
3474 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04003475 * transaction synchronous.
3476 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003477 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05003478 ext4_handle_sync(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07003479
3480out_stop:
Jan Kara9ddfc3d2008-07-11 19:27:31 -04003481 up_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07003482 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003483 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07003484 * then we need to clear up the orphan record which we created above.
3485 * However, if this was a real unlink then we were called by
3486 * ext4_delete_inode(), and we allow that function to clean up the
3487 * orphan info for us.
3488 */
3489 if (inode->i_nlink)
3490 ext4_orphan_del(handle, inode);
3491
Solofo Ramangalahyef737722008-04-29 22:00:41 -04003492 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3493 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07003494 ext4_journal_stop(handle);
3495}
3496
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003497static void ext4_falloc_update_inode(struct inode *inode,
3498 int mode, loff_t new_size, int update_ctime)
3499{
3500 struct timespec now;
3501
3502 if (update_ctime) {
3503 now = current_fs_time(inode->i_sb);
3504 if (!timespec_equal(&inode->i_ctime, &now))
3505 inode->i_ctime = now;
3506 }
3507 /*
3508 * Update only when preallocation was requested beyond
3509 * the file size.
3510 */
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003511 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3512 if (new_size > i_size_read(inode))
3513 i_size_write(inode, new_size);
3514 if (new_size > EXT4_I(inode)->i_disksize)
3515 ext4_update_i_disksize(inode, new_size);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003516 } else {
3517 /*
3518 * Mark that we allocate beyond EOF so the subsequent truncate
3519 * can proceed even if the new size is the same as i_size.
3520 */
3521 if (new_size > i_size_read(inode))
3522 EXT4_I(inode)->i_flags |= EXT4_EOFBLOCKS_FL;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003523 }
3524
3525}
3526
Amit Aroraa2df2a62007-07-17 21:42:41 -04003527/*
3528 * preallocate space for a file. This implements ext4's fallocate inode
3529 * operation, which gets called from sys_fallocate system call.
3530 * For block-mapped files, posix_fallocate should fall back to the method
3531 * of writing zeroes to the required new blocks (the same behavior which is
3532 * expected for file systems which do not support fallocate() system call).
3533 */
3534long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
3535{
3536 handle_t *handle;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003537 ext4_lblk_t block;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003538 loff_t new_size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003539 unsigned int max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003540 int ret = 0;
3541 int ret2 = 0;
3542 int retries = 0;
3543 struct buffer_head map_bh;
3544 unsigned int credits, blkbits = inode->i_blkbits;
3545
3546 /*
3547 * currently supporting (pre)allocate mode for extent-based
3548 * files _only_
3549 */
3550 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
3551 return -EOPNOTSUPP;
3552
3553 /* preallocation to directories is currently not supported */
3554 if (S_ISDIR(inode->i_mode))
3555 return -ENODEV;
3556
3557 block = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003558 /*
3559 * We can't just convert len to max_blocks because
3560 * If blocksize = 4096 offset = 3072 and len = 2048
3561 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003562 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003563 - block;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003564 /*
Mingming Caof3bd1f32008-08-19 22:16:03 -04003565 * credits to insert 1 extent into extent tree
Amit Aroraa2df2a62007-07-17 21:42:41 -04003566 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04003567 credits = ext4_chunk_trans_blocks(inode, max_blocks);
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05003568 mutex_lock(&inode->i_mutex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003569retry:
3570 while (ret >= 0 && ret < max_blocks) {
3571 block = block + ret;
3572 max_blocks = max_blocks - ret;
3573 handle = ext4_journal_start(inode, credits);
3574 if (IS_ERR(handle)) {
3575 ret = PTR_ERR(handle);
3576 break;
3577 }
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04003578 map_bh.b_state = 0;
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04003579 ret = ext4_get_blocks(handle, inode, block,
3580 max_blocks, &map_bh,
Theodore Ts'oc2177052009-05-14 00:58:52 -04003581 EXT4_GET_BLOCKS_CREATE_UNINIT_EXT);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05003582 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003583#ifdef EXT4FS_DEBUG
3584 WARN_ON(ret <= 0);
3585 printk(KERN_ERR "%s: ext4_ext_get_blocks "
3586 "returned error inode#%lu, block=%u, "
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05003587 "max_blocks=%u", __func__,
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05003588 inode->i_ino, block, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003589#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04003590 ext4_mark_inode_dirty(handle, inode);
3591 ret2 = ext4_journal_stop(handle);
3592 break;
3593 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003594 if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
3595 blkbits) >> blkbits))
3596 new_size = offset + len;
3597 else
3598 new_size = (block + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003599
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003600 ext4_falloc_update_inode(inode, mode, new_size,
3601 buffer_new(&map_bh));
Amit Aroraa2df2a62007-07-17 21:42:41 -04003602 ext4_mark_inode_dirty(handle, inode);
3603 ret2 = ext4_journal_stop(handle);
3604 if (ret2)
3605 break;
3606 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003607 if (ret == -ENOSPC &&
3608 ext4_should_retry_alloc(inode->i_sb, &retries)) {
3609 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003610 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003611 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05003612 mutex_unlock(&inode->i_mutex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003613 return ret > 0 ? ret2 : ret;
3614}
Eric Sandeen6873fa02008-10-07 00:46:36 -04003615
3616/*
Mingming Cao00314622009-09-28 15:49:08 -04003617 * This function convert a range of blocks to written extents
3618 * The caller of this function will pass the start offset and the size.
3619 * all unwritten extents within this range will be converted to
3620 * written extents.
3621 *
3622 * This function is called from the direct IO end io call back
3623 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05003624 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04003625 */
3626int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
Eric Sandeena1de02d2010-02-04 23:58:38 -05003627 ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04003628{
3629 handle_t *handle;
3630 ext4_lblk_t block;
3631 unsigned int max_blocks;
3632 int ret = 0;
3633 int ret2 = 0;
3634 struct buffer_head map_bh;
3635 unsigned int credits, blkbits = inode->i_blkbits;
3636
3637 block = offset >> blkbits;
3638 /*
3639 * We can't just convert len to max_blocks because
3640 * If blocksize = 4096 offset = 3072 and len = 2048
3641 */
3642 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
3643 - block;
3644 /*
3645 * credits to insert 1 extent into extent tree
3646 */
3647 credits = ext4_chunk_trans_blocks(inode, max_blocks);
3648 while (ret >= 0 && ret < max_blocks) {
3649 block = block + ret;
3650 max_blocks = max_blocks - ret;
3651 handle = ext4_journal_start(inode, credits);
3652 if (IS_ERR(handle)) {
3653 ret = PTR_ERR(handle);
3654 break;
3655 }
3656 map_bh.b_state = 0;
3657 ret = ext4_get_blocks(handle, inode, block,
3658 max_blocks, &map_bh,
3659 EXT4_GET_BLOCKS_DIO_CONVERT_EXT);
3660 if (ret <= 0) {
3661 WARN_ON(ret <= 0);
3662 printk(KERN_ERR "%s: ext4_ext_get_blocks "
3663 "returned error inode#%lu, block=%u, "
3664 "max_blocks=%u", __func__,
3665 inode->i_ino, block, max_blocks);
3666 }
3667 ext4_mark_inode_dirty(handle, inode);
3668 ret2 = ext4_journal_stop(handle);
3669 if (ret <= 0 || ret2 )
3670 break;
3671 }
3672 return ret > 0 ? ret2 : ret;
3673}
3674/*
Eric Sandeen6873fa02008-10-07 00:46:36 -04003675 * Callback function called for each extent to gather FIEMAP information.
3676 */
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003677static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path,
Eric Sandeen6873fa02008-10-07 00:46:36 -04003678 struct ext4_ext_cache *newex, struct ext4_extent *ex,
3679 void *data)
3680{
3681 struct fiemap_extent_info *fieinfo = data;
Eric Sandeenc9877b22009-05-01 23:32:06 -04003682 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003683 __u64 logical;
3684 __u64 physical;
3685 __u64 length;
3686 __u32 flags = 0;
3687 int error;
3688
3689 logical = (__u64)newex->ec_block << blksize_bits;
3690
3691 if (newex->ec_type == EXT4_EXT_CACHE_GAP) {
3692 pgoff_t offset;
3693 struct page *page;
3694 struct buffer_head *bh = NULL;
3695
3696 offset = logical >> PAGE_SHIFT;
3697 page = find_get_page(inode->i_mapping, offset);
3698 if (!page || !page_has_buffers(page))
3699 return EXT_CONTINUE;
3700
3701 bh = page_buffers(page);
3702
3703 if (!bh)
3704 return EXT_CONTINUE;
3705
3706 if (buffer_delay(bh)) {
3707 flags |= FIEMAP_EXTENT_DELALLOC;
3708 page_cache_release(page);
3709 } else {
3710 page_cache_release(page);
3711 return EXT_CONTINUE;
3712 }
3713 }
3714
3715 physical = (__u64)newex->ec_start << blksize_bits;
3716 length = (__u64)newex->ec_len << blksize_bits;
3717
3718 if (ex && ext4_ext_is_uninitialized(ex))
3719 flags |= FIEMAP_EXTENT_UNWRITTEN;
3720
3721 /*
3722 * If this extent reaches EXT_MAX_BLOCK, it must be last.
3723 *
3724 * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
3725 * this also indicates no more allocated blocks.
3726 *
3727 * XXX this might miss a single-block extent at EXT_MAX_BLOCK
3728 */
Eric Sandeenc9877b22009-05-01 23:32:06 -04003729 if (ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK ||
Theodore Ts'oeefd7f02009-05-02 19:05:37 -04003730 newex->ec_block + newex->ec_len - 1 == EXT_MAX_BLOCK) {
3731 loff_t size = i_size_read(inode);
3732 loff_t bs = EXT4_BLOCK_SIZE(inode->i_sb);
3733
Eric Sandeen6873fa02008-10-07 00:46:36 -04003734 flags |= FIEMAP_EXTENT_LAST;
Theodore Ts'oeefd7f02009-05-02 19:05:37 -04003735 if ((flags & FIEMAP_EXTENT_DELALLOC) &&
3736 logical+length > size)
3737 length = (size - logical + bs - 1) & ~(bs-1);
3738 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04003739
3740 error = fiemap_fill_next_extent(fieinfo, logical, physical,
3741 length, flags);
3742 if (error < 0)
3743 return error;
3744 if (error == 1)
3745 return EXT_BREAK;
3746
3747 return EXT_CONTINUE;
3748}
3749
3750/* fiemap flags we can handle specified here */
3751#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
3752
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003753static int ext4_xattr_fiemap(struct inode *inode,
3754 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04003755{
3756 __u64 physical = 0;
3757 __u64 length;
3758 __u32 flags = FIEMAP_EXTENT_LAST;
3759 int blockbits = inode->i_sb->s_blocksize_bits;
3760 int error = 0;
3761
3762 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003763 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04003764 struct ext4_iloc iloc;
3765 int offset; /* offset of xattr in inode */
3766
3767 error = ext4_get_inode_loc(inode, &iloc);
3768 if (error)
3769 return error;
3770 physical = iloc.bh->b_blocknr << blockbits;
3771 offset = EXT4_GOOD_OLD_INODE_SIZE +
3772 EXT4_I(inode)->i_extra_isize;
3773 physical += offset;
3774 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
3775 flags |= FIEMAP_EXTENT_DATA_INLINE;
3776 } else { /* external block */
3777 physical = EXT4_I(inode)->i_file_acl << blockbits;
3778 length = inode->i_sb->s_blocksize;
3779 }
3780
3781 if (physical)
3782 error = fiemap_fill_next_extent(fieinfo, 0, physical,
3783 length, flags);
3784 return (error < 0 ? error : 0);
3785}
3786
3787int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3788 __u64 start, __u64 len)
3789{
3790 ext4_lblk_t start_blk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003791 int error = 0;
3792
3793 /* fallback to generic here if not in extents fmt */
3794 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
3795 return generic_block_fiemap(inode, fieinfo, start, len,
3796 ext4_get_block);
3797
3798 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
3799 return -EBADR;
3800
3801 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
3802 error = ext4_xattr_fiemap(inode, fieinfo);
3803 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05003804 ext4_lblk_t len_blks;
3805 __u64 last_blk;
3806
Eric Sandeen6873fa02008-10-07 00:46:36 -04003807 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05003808 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
3809 if (last_blk >= EXT_MAX_BLOCK)
3810 last_blk = EXT_MAX_BLOCK-1;
3811 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003812
3813 /*
3814 * Walk the extent tree gathering extent information.
3815 * ext4_ext_fiemap_cb will push extents back to user.
3816 */
Eric Sandeen6873fa02008-10-07 00:46:36 -04003817 error = ext4_ext_walk_space(inode, start_blk, len_blks,
3818 ext4_ext_fiemap_cb, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04003819 }
3820
3821 return error;
3822}
3823