blob: 4bb69206f175d0faf966d75cd52f4332391d03a4 [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++;
Frank Mayhar273df552010-03-02 11:46:09 -0500706 if (unlikely(ppos > depth)) {
707 put_bh(bh);
708 EXT4_ERROR_INODE(inode,
709 "ppos %d > depth %d", ppos, depth);
710 goto err;
711 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700712 path[ppos].p_bh = bh;
713 path[ppos].p_hdr = eh;
714 i--;
715
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400716 if (need_to_validate && ext4_ext_check(inode, eh, i))
Alex Tomasa86c6182006-10-11 01:21:03 -0700717 goto err;
718 }
719
720 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700721 path[ppos].p_ext = NULL;
722 path[ppos].p_idx = NULL;
723
Alex Tomasa86c6182006-10-11 01:21:03 -0700724 /* find extent */
725 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400726 /* if not an empty leaf */
727 if (path[ppos].p_ext)
728 path[ppos].p_block = ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700729
730 ext4_ext_show_path(inode, path);
731
732 return path;
733
734err:
735 ext4_ext_drop_refs(path);
736 if (alloc)
737 kfree(path);
738 return ERR_PTR(-EIO);
739}
740
741/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700742 * ext4_ext_insert_index:
743 * insert new index [@logical;@ptr] into the block at @curp;
744 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700745 */
Mingming Cao00314622009-09-28 15:49:08 -0400746int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700747 struct ext4_ext_path *curp,
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700748 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700749{
750 struct ext4_extent_idx *ix;
751 int len, err;
752
Avantika Mathur7e028972006-12-06 20:41:33 -0800753 err = ext4_ext_get_access(handle, inode, curp);
754 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700755 return err;
756
Frank Mayhar273df552010-03-02 11:46:09 -0500757 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
758 EXT4_ERROR_INODE(inode,
759 "logical %d == ei_block %d!",
760 logical, le32_to_cpu(curp->p_idx->ei_block));
761 return -EIO;
762 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700763 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
764 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
765 /* insert after */
766 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
767 len = (len - 1) * sizeof(struct ext4_extent_idx);
768 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400769 ext_debug("insert new index %d after: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700770 "move %d from 0x%p to 0x%p\n",
771 logical, ptr, len,
772 (curp->p_idx + 1), (curp->p_idx + 2));
773 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
774 }
775 ix = curp->p_idx + 1;
776 } else {
777 /* insert before */
778 len = len * sizeof(struct ext4_extent_idx);
779 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400780 ext_debug("insert new index %d before: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700781 "move %d from 0x%p to 0x%p\n",
782 logical, ptr, len,
783 curp->p_idx, (curp->p_idx + 1));
784 memmove(curp->p_idx + 1, curp->p_idx, len);
785 ix = curp->p_idx;
786 }
787
788 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700789 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400790 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700791
Frank Mayhar273df552010-03-02 11:46:09 -0500792 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
793 > le16_to_cpu(curp->p_hdr->eh_max))) {
794 EXT4_ERROR_INODE(inode,
795 "logical %d == ei_block %d!",
796 logical, le32_to_cpu(curp->p_idx->ei_block));
797 return -EIO;
798 }
799 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
800 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
801 return -EIO;
802 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700803
804 err = ext4_ext_dirty(handle, inode, curp);
805 ext4_std_error(inode->i_sb, err);
806
807 return err;
808}
809
810/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700811 * ext4_ext_split:
812 * inserts new subtree into the path, using free index entry
813 * at depth @at:
814 * - allocates all needed blocks (new leaf and all intermediate index blocks)
815 * - makes decision where to split
816 * - moves remaining extents and index entries (right to the split point)
817 * into the newly allocated blocks
818 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700819 */
820static int ext4_ext_split(handle_t *handle, struct inode *inode,
821 struct ext4_ext_path *path,
822 struct ext4_extent *newext, int at)
823{
824 struct buffer_head *bh = NULL;
825 int depth = ext_depth(inode);
826 struct ext4_extent_header *neh;
827 struct ext4_extent_idx *fidx;
828 struct ext4_extent *ex;
829 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700830 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700831 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700832 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700833 int err = 0;
834
835 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700836 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700837
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700838 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700839 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -0500840 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
841 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
842 return -EIO;
843 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700844 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
845 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700846 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700847 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400848 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700849 } else {
850 border = newext->ee_block;
851 ext_debug("leaf will be added."
852 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400853 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700854 }
855
856 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700857 * If error occurs, then we break processing
858 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700859 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700860 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700861 */
862
863 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700864 * Get array to track all allocated blocks.
865 * We need this to handle errors and free blocks
866 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700867 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800868 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700869 if (!ablocks)
870 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700871
872 /* allocate all needed blocks */
873 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
874 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400875 newblock = ext4_ext_new_meta_block(handle, inode, path,
876 newext, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700877 if (newblock == 0)
878 goto cleanup;
879 ablocks[a] = newblock;
880 }
881
882 /* initialize new leaf */
883 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -0500884 if (unlikely(newblock == 0)) {
885 EXT4_ERROR_INODE(inode, "newblock == 0!");
886 err = -EIO;
887 goto cleanup;
888 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700889 bh = sb_getblk(inode->i_sb, newblock);
890 if (!bh) {
891 err = -EIO;
892 goto cleanup;
893 }
894 lock_buffer(bh);
895
Avantika Mathur7e028972006-12-06 20:41:33 -0800896 err = ext4_journal_get_create_access(handle, bh);
897 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700898 goto cleanup;
899
900 neh = ext_block_hdr(bh);
901 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400902 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700903 neh->eh_magic = EXT4_EXT_MAGIC;
904 neh->eh_depth = 0;
905 ex = EXT_FIRST_EXTENT(neh);
906
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700907 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -0500908 if (unlikely(path[depth].p_hdr->eh_entries !=
909 path[depth].p_hdr->eh_max)) {
910 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
911 path[depth].p_hdr->eh_entries,
912 path[depth].p_hdr->eh_max);
913 err = -EIO;
914 goto cleanup;
915 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700916 /* start copy from next extent */
917 /* TODO: we could do it by single memmove */
918 m = 0;
919 path[depth].p_ext++;
920 while (path[depth].p_ext <=
921 EXT_MAX_EXTENT(path[depth].p_hdr)) {
Mingming553f9002009-09-18 13:34:55 -0400922 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400923 le32_to_cpu(path[depth].p_ext->ee_block),
924 ext_pblock(path[depth].p_ext),
Mingming553f9002009-09-18 13:34:55 -0400925 ext4_ext_is_uninitialized(path[depth].p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400926 ext4_ext_get_actual_len(path[depth].p_ext),
Alex Tomasa86c6182006-10-11 01:21:03 -0700927 newblock);
928 /*memmove(ex++, path[depth].p_ext++,
929 sizeof(struct ext4_extent));
930 neh->eh_entries++;*/
931 path[depth].p_ext++;
932 m++;
933 }
934 if (m) {
935 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400936 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700937 }
938
939 set_buffer_uptodate(bh);
940 unlock_buffer(bh);
941
Frank Mayhar03901312009-01-07 00:06:22 -0500942 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800943 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700944 goto cleanup;
945 brelse(bh);
946 bh = NULL;
947
948 /* correct old leaf */
949 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800950 err = ext4_ext_get_access(handle, inode, path + depth);
951 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700952 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400953 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800954 err = ext4_ext_dirty(handle, inode, path + depth);
955 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700956 goto cleanup;
957
958 }
959
960 /* create intermediate indexes */
961 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -0500962 if (unlikely(k < 0)) {
963 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
964 err = -EIO;
965 goto cleanup;
966 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700967 if (k)
968 ext_debug("create %d intermediate indices\n", k);
969 /* insert new index into current index block */
970 /* current depth stored in i var */
971 i = depth - 1;
972 while (k--) {
973 oldblock = newblock;
974 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -0500975 bh = sb_getblk(inode->i_sb, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700976 if (!bh) {
977 err = -EIO;
978 goto cleanup;
979 }
980 lock_buffer(bh);
981
Avantika Mathur7e028972006-12-06 20:41:33 -0800982 err = ext4_journal_get_create_access(handle, bh);
983 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700984 goto cleanup;
985
986 neh = ext_block_hdr(bh);
987 neh->eh_entries = cpu_to_le16(1);
988 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400989 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700990 neh->eh_depth = cpu_to_le16(depth - i);
991 fidx = EXT_FIRST_INDEX(neh);
992 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700993 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700994
Eric Sandeenbba90742008-01-28 23:58:27 -0500995 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
996 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700997 /* copy indexes */
998 m = 0;
999 path[i].p_idx++;
1000
1001 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
1002 EXT_MAX_INDEX(path[i].p_hdr));
Frank Mayhar273df552010-03-02 11:46:09 -05001003 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
1004 EXT_LAST_INDEX(path[i].p_hdr))) {
1005 EXT4_ERROR_INODE(inode,
1006 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1007 le32_to_cpu(path[i].p_ext->ee_block));
1008 err = -EIO;
1009 goto cleanup;
1010 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001011 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
Dmitry Monakhov26d535e2007-07-18 08:33:37 -04001012 ext_debug("%d: move %d:%llu in new index %llu\n", i,
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001013 le32_to_cpu(path[i].p_idx->ei_block),
1014 idx_pblock(path[i].p_idx),
1015 newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001016 /*memmove(++fidx, path[i].p_idx++,
1017 sizeof(struct ext4_extent_idx));
1018 neh->eh_entries++;
1019 BUG_ON(neh->eh_entries > neh->eh_max);*/
1020 path[i].p_idx++;
1021 m++;
1022 }
1023 if (m) {
1024 memmove(++fidx, path[i].p_idx - m,
1025 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001026 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001027 }
1028 set_buffer_uptodate(bh);
1029 unlock_buffer(bh);
1030
Frank Mayhar03901312009-01-07 00:06:22 -05001031 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001032 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001033 goto cleanup;
1034 brelse(bh);
1035 bh = NULL;
1036
1037 /* correct old index */
1038 if (m) {
1039 err = ext4_ext_get_access(handle, inode, path + i);
1040 if (err)
1041 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001042 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001043 err = ext4_ext_dirty(handle, inode, path + i);
1044 if (err)
1045 goto cleanup;
1046 }
1047
1048 i--;
1049 }
1050
1051 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001052 err = ext4_ext_insert_index(handle, inode, path + at,
1053 le32_to_cpu(border), newblock);
1054
1055cleanup:
1056 if (bh) {
1057 if (buffer_locked(bh))
1058 unlock_buffer(bh);
1059 brelse(bh);
1060 }
1061
1062 if (err) {
1063 /* free all allocated blocks in error case */
1064 for (i = 0; i < depth; i++) {
1065 if (!ablocks[i])
1066 continue;
Theodore Ts'oe6362602009-11-23 07:17:05 -05001067 ext4_free_blocks(handle, inode, 0, ablocks[i], 1,
1068 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001069 }
1070 }
1071 kfree(ablocks);
1072
1073 return err;
1074}
1075
1076/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001077 * ext4_ext_grow_indepth:
1078 * implements tree growing procedure:
1079 * - allocates new block
1080 * - moves top-level data (index block or leaf) into the new block
1081 * - initializes new top-level, creating index that points to the
1082 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001083 */
1084static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
1085 struct ext4_ext_path *path,
1086 struct ext4_extent *newext)
1087{
1088 struct ext4_ext_path *curp = path;
1089 struct ext4_extent_header *neh;
1090 struct ext4_extent_idx *fidx;
1091 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001092 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001093 int err = 0;
1094
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -04001095 newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07001096 if (newblock == 0)
1097 return err;
1098
1099 bh = sb_getblk(inode->i_sb, newblock);
1100 if (!bh) {
1101 err = -EIO;
1102 ext4_std_error(inode->i_sb, err);
1103 return err;
1104 }
1105 lock_buffer(bh);
1106
Avantika Mathur7e028972006-12-06 20:41:33 -08001107 err = ext4_journal_get_create_access(handle, bh);
1108 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001109 unlock_buffer(bh);
1110 goto out;
1111 }
1112
1113 /* move top-level index/leaf into new block */
1114 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
1115
1116 /* set size of new block */
1117 neh = ext_block_hdr(bh);
1118 /* old root could have indexes or leaves
1119 * so calculate e_max right way */
1120 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001121 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001122 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001123 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001124 neh->eh_magic = EXT4_EXT_MAGIC;
1125 set_buffer_uptodate(bh);
1126 unlock_buffer(bh);
1127
Frank Mayhar03901312009-01-07 00:06:22 -05001128 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001129 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001130 goto out;
1131
1132 /* create index in new top-level index: num,max,pointer */
Avantika Mathur7e028972006-12-06 20:41:33 -08001133 err = ext4_ext_get_access(handle, inode, curp);
1134 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001135 goto out;
1136
1137 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001138 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001139 curp->p_hdr->eh_entries = cpu_to_le16(1);
1140 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
Dmitry Monakhove9f410b2007-07-18 09:09:15 -04001141
1142 if (path[0].p_hdr->eh_depth)
1143 curp->p_idx->ei_block =
1144 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
1145 else
1146 curp->p_idx->ei_block =
1147 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001148 ext4_idx_store_pblock(curp->p_idx, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001149
1150 neh = ext_inode_hdr(inode);
1151 fidx = EXT_FIRST_INDEX(neh);
Mingming Cao2ae02102006-10-11 01:21:11 -07001152 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001153 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001154 le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
Alex Tomasa86c6182006-10-11 01:21:03 -07001155
1156 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
1157 err = ext4_ext_dirty(handle, inode, curp);
1158out:
1159 brelse(bh);
1160
1161 return err;
1162}
1163
1164/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001165 * ext4_ext_create_new_leaf:
1166 * finds empty index and adds new leaf.
1167 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001168 */
1169static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
1170 struct ext4_ext_path *path,
1171 struct ext4_extent *newext)
1172{
1173 struct ext4_ext_path *curp;
1174 int depth, i, err = 0;
1175
1176repeat:
1177 i = depth = ext_depth(inode);
1178
1179 /* walk up to the tree and look for free index entry */
1180 curp = path + depth;
1181 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1182 i--;
1183 curp--;
1184 }
1185
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001186 /* we use already allocated block for index block,
1187 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001188 if (EXT_HAS_FREE_INDEX(curp)) {
1189 /* if we found index with free entry, then use that
1190 * entry: create all needed subtree and add new leaf */
1191 err = ext4_ext_split(handle, inode, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001192 if (err)
1193 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001194
1195 /* refill path */
1196 ext4_ext_drop_refs(path);
1197 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001198 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1199 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001200 if (IS_ERR(path))
1201 err = PTR_ERR(path);
1202 } else {
1203 /* tree is full, time to grow in depth */
1204 err = ext4_ext_grow_indepth(handle, inode, path, newext);
1205 if (err)
1206 goto out;
1207
1208 /* refill path */
1209 ext4_ext_drop_refs(path);
1210 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001211 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1212 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001213 if (IS_ERR(path)) {
1214 err = PTR_ERR(path);
1215 goto out;
1216 }
1217
1218 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001219 * only first (depth 0 -> 1) produces free space;
1220 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001221 */
1222 depth = ext_depth(inode);
1223 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001224 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001225 goto repeat;
1226 }
1227 }
1228
1229out:
1230 return err;
1231}
1232
1233/*
Alex Tomas1988b512008-01-28 23:58:27 -05001234 * search the closest allocated block to the left for *logical
1235 * and returns it at @logical + it's physical address at @phys
1236 * if *logical is the smallest allocated block, the function
1237 * returns 0 at @phys
1238 * return value contains 0 (success) or error code
1239 */
1240int
1241ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
1242 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1243{
1244 struct ext4_extent_idx *ix;
1245 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001246 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001247
Frank Mayhar273df552010-03-02 11:46:09 -05001248 if (unlikely(path == NULL)) {
1249 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1250 return -EIO;
1251 }
Alex Tomas1988b512008-01-28 23:58:27 -05001252 depth = path->p_depth;
1253 *phys = 0;
1254
1255 if (depth == 0 && path->p_ext == NULL)
1256 return 0;
1257
1258 /* usually extent in the path covers blocks smaller
1259 * then *logical, but it can be that extent is the
1260 * first one in the file */
1261
1262 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001263 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001264 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001265 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1266 EXT4_ERROR_INODE(inode,
1267 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1268 *logical, le32_to_cpu(ex->ee_block));
1269 return -EIO;
1270 }
Alex Tomas1988b512008-01-28 23:58:27 -05001271 while (--depth >= 0) {
1272 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001273 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1274 EXT4_ERROR_INODE(inode,
1275 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
1276 ix != NULL ? ix->ei_block : 0,
1277 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
1278 EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block : 0,
1279 depth);
1280 return -EIO;
1281 }
Alex Tomas1988b512008-01-28 23:58:27 -05001282 }
1283 return 0;
1284 }
1285
Frank Mayhar273df552010-03-02 11:46:09 -05001286 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1287 EXT4_ERROR_INODE(inode,
1288 "logical %d < ee_block %d + ee_len %d!",
1289 *logical, le32_to_cpu(ex->ee_block), ee_len);
1290 return -EIO;
1291 }
Alex Tomas1988b512008-01-28 23:58:27 -05001292
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001293 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1294 *phys = ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001295 return 0;
1296}
1297
1298/*
1299 * search the closest allocated block to the right for *logical
1300 * and returns it at @logical + it's physical address at @phys
1301 * if *logical is the smallest allocated block, the function
1302 * returns 0 at @phys
1303 * return value contains 0 (success) or error code
1304 */
1305int
1306ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
1307 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1308{
1309 struct buffer_head *bh = NULL;
1310 struct ext4_extent_header *eh;
1311 struct ext4_extent_idx *ix;
1312 struct ext4_extent *ex;
1313 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001314 int depth; /* Note, NOT eh_depth; depth from top of tree */
1315 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001316
Frank Mayhar273df552010-03-02 11:46:09 -05001317 if (unlikely(path == NULL)) {
1318 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1319 return -EIO;
1320 }
Alex Tomas1988b512008-01-28 23:58:27 -05001321 depth = path->p_depth;
1322 *phys = 0;
1323
1324 if (depth == 0 && path->p_ext == NULL)
1325 return 0;
1326
1327 /* usually extent in the path covers blocks smaller
1328 * then *logical, but it can be that extent is the
1329 * first one in the file */
1330
1331 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001332 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001333 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001334 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1335 EXT4_ERROR_INODE(inode,
1336 "first_extent(path[%d].p_hdr) != ex",
1337 depth);
1338 return -EIO;
1339 }
Alex Tomas1988b512008-01-28 23:58:27 -05001340 while (--depth >= 0) {
1341 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001342 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1343 EXT4_ERROR_INODE(inode,
1344 "ix != EXT_FIRST_INDEX *logical %d!",
1345 *logical);
1346 return -EIO;
1347 }
Alex Tomas1988b512008-01-28 23:58:27 -05001348 }
1349 *logical = le32_to_cpu(ex->ee_block);
1350 *phys = ext_pblock(ex);
1351 return 0;
1352 }
1353
Frank Mayhar273df552010-03-02 11:46:09 -05001354 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1355 EXT4_ERROR_INODE(inode,
1356 "logical %d < ee_block %d + ee_len %d!",
1357 *logical, le32_to_cpu(ex->ee_block), ee_len);
1358 return -EIO;
1359 }
Alex Tomas1988b512008-01-28 23:58:27 -05001360
1361 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1362 /* next allocated block in this leaf */
1363 ex++;
1364 *logical = le32_to_cpu(ex->ee_block);
1365 *phys = ext_pblock(ex);
1366 return 0;
1367 }
1368
1369 /* go up and search for index to the right */
1370 while (--depth >= 0) {
1371 ix = path[depth].p_idx;
1372 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001373 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001374 }
1375
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001376 /* we've gone up to the root and found no index to the right */
1377 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001378
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001379got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001380 /* we've found index to the right, let's
1381 * follow it and find the closest allocated
1382 * block to the right */
1383 ix++;
1384 block = idx_pblock(ix);
1385 while (++depth < path->p_depth) {
1386 bh = sb_bread(inode->i_sb, block);
1387 if (bh == NULL)
1388 return -EIO;
1389 eh = ext_block_hdr(bh);
Eric Sandeen395a87b2009-03-10 18:18:47 -04001390 /* subtract from p_depth to get proper eh_depth */
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001391 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001392 put_bh(bh);
1393 return -EIO;
1394 }
1395 ix = EXT_FIRST_INDEX(eh);
1396 block = idx_pblock(ix);
1397 put_bh(bh);
1398 }
1399
1400 bh = sb_bread(inode->i_sb, block);
1401 if (bh == NULL)
1402 return -EIO;
1403 eh = ext_block_hdr(bh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001404 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001405 put_bh(bh);
1406 return -EIO;
1407 }
1408 ex = EXT_FIRST_EXTENT(eh);
1409 *logical = le32_to_cpu(ex->ee_block);
1410 *phys = ext_pblock(ex);
1411 put_bh(bh);
1412 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001413}
1414
1415/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001416 * ext4_ext_next_allocated_block:
1417 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1418 * NOTE: it considers block number from index entry as
1419 * allocated block. Thus, index entries have to be consistent
1420 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001421 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001422static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001423ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1424{
1425 int depth;
1426
1427 BUG_ON(path == NULL);
1428 depth = path->p_depth;
1429
1430 if (depth == 0 && path->p_ext == NULL)
1431 return EXT_MAX_BLOCK;
1432
1433 while (depth >= 0) {
1434 if (depth == path->p_depth) {
1435 /* leaf */
1436 if (path[depth].p_ext !=
1437 EXT_LAST_EXTENT(path[depth].p_hdr))
1438 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1439 } else {
1440 /* index */
1441 if (path[depth].p_idx !=
1442 EXT_LAST_INDEX(path[depth].p_hdr))
1443 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1444 }
1445 depth--;
1446 }
1447
1448 return EXT_MAX_BLOCK;
1449}
1450
1451/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001452 * ext4_ext_next_leaf_block:
Alex Tomasa86c6182006-10-11 01:21:03 -07001453 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1454 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001455static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
Andrew Morton63f57932006-10-11 01:21:24 -07001456 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001457{
1458 int depth;
1459
1460 BUG_ON(path == NULL);
1461 depth = path->p_depth;
1462
1463 /* zero-tree has no leaf blocks at all */
1464 if (depth == 0)
1465 return EXT_MAX_BLOCK;
1466
1467 /* go to index block */
1468 depth--;
1469
1470 while (depth >= 0) {
1471 if (path[depth].p_idx !=
1472 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001473 return (ext4_lblk_t)
1474 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001475 depth--;
1476 }
1477
1478 return EXT_MAX_BLOCK;
1479}
1480
1481/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001482 * ext4_ext_correct_indexes:
1483 * if leaf gets modified and modified extent is first in the leaf,
1484 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001485 * TODO: do we need to correct tree in all cases?
1486 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001487static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001488 struct ext4_ext_path *path)
1489{
1490 struct ext4_extent_header *eh;
1491 int depth = ext_depth(inode);
1492 struct ext4_extent *ex;
1493 __le32 border;
1494 int k, err = 0;
1495
1496 eh = path[depth].p_hdr;
1497 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001498
1499 if (unlikely(ex == NULL || eh == NULL)) {
1500 EXT4_ERROR_INODE(inode,
1501 "ex %p == NULL or eh %p == NULL", ex, eh);
1502 return -EIO;
1503 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001504
1505 if (depth == 0) {
1506 /* there is no tree at all */
1507 return 0;
1508 }
1509
1510 if (ex != EXT_FIRST_EXTENT(eh)) {
1511 /* we correct tree if first leaf got modified only */
1512 return 0;
1513 }
1514
1515 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001516 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001517 */
1518 k = depth - 1;
1519 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001520 err = ext4_ext_get_access(handle, inode, path + k);
1521 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001522 return err;
1523 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001524 err = ext4_ext_dirty(handle, inode, path + k);
1525 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001526 return err;
1527
1528 while (k--) {
1529 /* change all left-side indexes */
1530 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1531 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001532 err = ext4_ext_get_access(handle, inode, path + k);
1533 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001534 break;
1535 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001536 err = ext4_ext_dirty(handle, inode, path + k);
1537 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001538 break;
1539 }
1540
1541 return err;
1542}
1543
Akira Fujita748de672009-06-17 19:24:03 -04001544int
Alex Tomasa86c6182006-10-11 01:21:03 -07001545ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1546 struct ext4_extent *ex2)
1547{
Amit Arora749269f2007-07-18 09:02:56 -04001548 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001549
1550 /*
1551 * Make sure that either both extents are uninitialized, or
1552 * both are _not_.
1553 */
1554 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1555 return 0;
1556
Amit Arora749269f2007-07-18 09:02:56 -04001557 if (ext4_ext_is_uninitialized(ex1))
1558 max_len = EXT_UNINIT_MAX_LEN;
1559 else
1560 max_len = EXT_INIT_MAX_LEN;
1561
Amit Aroraa2df2a62007-07-17 21:42:41 -04001562 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1563 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1564
1565 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001566 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001567 return 0;
1568
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001569 /*
1570 * To allow future support for preallocated extents to be added
1571 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001572 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001573 */
Amit Arora749269f2007-07-18 09:02:56 -04001574 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001575 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001576#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001577 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001578 return 0;
1579#endif
1580
Amit Aroraa2df2a62007-07-17 21:42:41 -04001581 if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001582 return 1;
1583 return 0;
1584}
1585
1586/*
Amit Arora56055d32007-07-17 21:42:38 -04001587 * This function tries to merge the "ex" extent to the next extent in the tree.
1588 * It always tries to merge towards right. If you want to merge towards
1589 * left, pass "ex - 1" as argument instead of "ex".
1590 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1591 * 1 if they got merged.
1592 */
1593int ext4_ext_try_to_merge(struct inode *inode,
1594 struct ext4_ext_path *path,
1595 struct ext4_extent *ex)
1596{
1597 struct ext4_extent_header *eh;
1598 unsigned int depth, len;
1599 int merge_done = 0;
1600 int uninitialized = 0;
1601
1602 depth = ext_depth(inode);
1603 BUG_ON(path[depth].p_hdr == NULL);
1604 eh = path[depth].p_hdr;
1605
1606 while (ex < EXT_LAST_EXTENT(eh)) {
1607 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1608 break;
1609 /* merge with next extent! */
1610 if (ext4_ext_is_uninitialized(ex))
1611 uninitialized = 1;
1612 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1613 + ext4_ext_get_actual_len(ex + 1));
1614 if (uninitialized)
1615 ext4_ext_mark_uninitialized(ex);
1616
1617 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1618 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1619 * sizeof(struct ext4_extent);
1620 memmove(ex + 1, ex + 2, len);
1621 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001622 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001623 merge_done = 1;
1624 WARN_ON(eh->eh_entries == 0);
1625 if (!eh->eh_entries)
Eric Sandeen12062dd2010-02-15 14:19:27 -05001626 ext4_error(inode->i_sb,
1627 "inode#%lu, eh->eh_entries = 0!",
1628 inode->i_ino);
Amit Arora56055d32007-07-17 21:42:38 -04001629 }
1630
1631 return merge_done;
1632}
1633
1634/*
Amit Arora25d14f92007-05-24 13:04:13 -04001635 * check if a portion of the "newext" extent overlaps with an
1636 * existing extent.
1637 *
1638 * If there is an overlap discovered, it updates the length of the newext
1639 * such that there will be no overlap, and then returns 1.
1640 * If there is no overlap found, it returns 0.
1641 */
1642unsigned int ext4_ext_check_overlap(struct inode *inode,
1643 struct ext4_extent *newext,
1644 struct ext4_ext_path *path)
1645{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001646 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001647 unsigned int depth, len1;
1648 unsigned int ret = 0;
1649
1650 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001651 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001652 depth = ext_depth(inode);
1653 if (!path[depth].p_ext)
1654 goto out;
1655 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1656
1657 /*
1658 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001659 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001660 */
1661 if (b2 < b1) {
1662 b2 = ext4_ext_next_allocated_block(path);
1663 if (b2 == EXT_MAX_BLOCK)
1664 goto out;
1665 }
1666
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001667 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001668 if (b1 + len1 < b1) {
1669 len1 = EXT_MAX_BLOCK - b1;
1670 newext->ee_len = cpu_to_le16(len1);
1671 ret = 1;
1672 }
1673
1674 /* check for overlap */
1675 if (b1 + len1 > b2) {
1676 newext->ee_len = cpu_to_le16(b2 - b1);
1677 ret = 1;
1678 }
1679out:
1680 return ret;
1681}
1682
1683/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001684 * ext4_ext_insert_extent:
1685 * tries to merge requsted extent into the existing extent or
1686 * inserts requested extent as new one into the tree,
1687 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001688 */
1689int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1690 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04001691 struct ext4_extent *newext, int flag)
Alex Tomasa86c6182006-10-11 01:21:03 -07001692{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001693 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001694 struct ext4_extent *ex, *fex;
1695 struct ext4_extent *nearex; /* nearest extent */
1696 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001697 int depth, len, err;
1698 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001699 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001700
Frank Mayhar273df552010-03-02 11:46:09 -05001701 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1702 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1703 return -EIO;
1704 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001705 depth = ext_depth(inode);
1706 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001707 if (unlikely(path[depth].p_hdr == NULL)) {
1708 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1709 return -EIO;
1710 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001711
1712 /* try to insert block into found extent and return */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001713 if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
Mingming Cao00314622009-09-28 15:49:08 -04001714 && ext4_can_extents_be_merged(inode, ex, newext)) {
Mingming553f9002009-09-18 13:34:55 -04001715 ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
1716 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001717 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001718 le32_to_cpu(ex->ee_block),
Mingming553f9002009-09-18 13:34:55 -04001719 ext4_ext_is_uninitialized(ex),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001720 ext4_ext_get_actual_len(ex), ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001721 err = ext4_ext_get_access(handle, inode, path + depth);
1722 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001723 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001724
1725 /*
1726 * ext4_can_extents_be_merged should have checked that either
1727 * both extents are uninitialized, or both aren't. Thus we
1728 * need to check only one of them here.
1729 */
1730 if (ext4_ext_is_uninitialized(ex))
1731 uninitialized = 1;
1732 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1733 + ext4_ext_get_actual_len(newext));
1734 if (uninitialized)
1735 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001736 eh = path[depth].p_hdr;
1737 nearex = ex;
1738 goto merge;
1739 }
1740
1741repeat:
1742 depth = ext_depth(inode);
1743 eh = path[depth].p_hdr;
1744 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1745 goto has_space;
1746
1747 /* probably next leaf has space for us? */
1748 fex = EXT_LAST_EXTENT(eh);
1749 next = ext4_ext_next_leaf_block(inode, path);
1750 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1751 && next != EXT_MAX_BLOCK) {
1752 ext_debug("next leaf block - %d\n", next);
1753 BUG_ON(npath != NULL);
1754 npath = ext4_ext_find_extent(inode, next, NULL);
1755 if (IS_ERR(npath))
1756 return PTR_ERR(npath);
1757 BUG_ON(npath->p_depth != path->p_depth);
1758 eh = npath[depth].p_hdr;
1759 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1760 ext_debug("next leaf isnt full(%d)\n",
1761 le16_to_cpu(eh->eh_entries));
1762 path = npath;
1763 goto repeat;
1764 }
1765 ext_debug("next leaf has no free space(%d,%d)\n",
1766 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1767 }
1768
1769 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001770 * There is no free space in the found leaf.
1771 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001772 */
1773 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1774 if (err)
1775 goto cleanup;
1776 depth = ext_depth(inode);
1777 eh = path[depth].p_hdr;
1778
1779has_space:
1780 nearex = path[depth].p_ext;
1781
Avantika Mathur7e028972006-12-06 20:41:33 -08001782 err = ext4_ext_get_access(handle, inode, path + depth);
1783 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001784 goto cleanup;
1785
1786 if (!nearex) {
1787 /* there is no extent in this leaf, create first one */
Mingming553f9002009-09-18 13:34:55 -04001788 ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001789 le32_to_cpu(newext->ee_block),
1790 ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001791 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001792 ext4_ext_get_actual_len(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001793 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1794 } else if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001795 > le32_to_cpu(nearex->ee_block)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001796/* BUG_ON(newext->ee_block == nearex->ee_block); */
1797 if (nearex != EXT_LAST_EXTENT(eh)) {
1798 len = EXT_MAX_EXTENT(eh) - nearex;
1799 len = (len - 1) * sizeof(struct ext4_extent);
1800 len = len < 0 ? 0 : len;
Mingming553f9002009-09-18 13:34:55 -04001801 ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001802 "move %d from 0x%p to 0x%p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001803 le32_to_cpu(newext->ee_block),
1804 ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001805 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001806 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001807 nearex, len, nearex + 1, nearex + 2);
1808 memmove(nearex + 2, nearex + 1, len);
1809 }
1810 path[depth].p_ext = nearex + 1;
1811 } else {
1812 BUG_ON(newext->ee_block == nearex->ee_block);
1813 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1814 len = len < 0 ? 0 : len;
Mingming553f9002009-09-18 13:34:55 -04001815 ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001816 "move %d from 0x%p to 0x%p\n",
1817 le32_to_cpu(newext->ee_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001818 ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001819 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001820 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001821 nearex, len, nearex + 1, nearex + 2);
1822 memmove(nearex + 1, nearex, len);
1823 path[depth].p_ext = nearex;
1824 }
1825
Marcin Slusarze8546d02008-04-17 10:38:59 -04001826 le16_add_cpu(&eh->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001827 nearex = path[depth].p_ext;
1828 nearex->ee_block = newext->ee_block;
Aneesh Kumar K.Vb3776112007-10-16 18:38:25 -04001829 ext4_ext_store_pblock(nearex, ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001830 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001831
1832merge:
1833 /* try to merge extents to the right */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001834 if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
Mingming Cao00314622009-09-28 15:49:08 -04001835 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001836
1837 /* try to merge extents to the left */
1838
1839 /* time to correct all indexes above */
1840 err = ext4_ext_correct_indexes(handle, inode, path);
1841 if (err)
1842 goto cleanup;
1843
1844 err = ext4_ext_dirty(handle, inode, path + depth);
1845
1846cleanup:
1847 if (npath) {
1848 ext4_ext_drop_refs(npath);
1849 kfree(npath);
1850 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001851 ext4_ext_invalidate_cache(inode);
1852 return err;
1853}
1854
Eric Sandeen6873fa02008-10-07 00:46:36 -04001855int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1856 ext4_lblk_t num, ext_prepare_callback func,
1857 void *cbdata)
1858{
1859 struct ext4_ext_path *path = NULL;
1860 struct ext4_ext_cache cbex;
1861 struct ext4_extent *ex;
1862 ext4_lblk_t next, start = 0, end = 0;
1863 ext4_lblk_t last = block + num;
1864 int depth, exists, err = 0;
1865
1866 BUG_ON(func == NULL);
1867 BUG_ON(inode == NULL);
1868
1869 while (block < last && block != EXT_MAX_BLOCK) {
1870 num = last - block;
1871 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001872 down_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001873 path = ext4_ext_find_extent(inode, block, path);
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001874 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001875 if (IS_ERR(path)) {
1876 err = PTR_ERR(path);
1877 path = NULL;
1878 break;
1879 }
1880
1881 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05001882 if (unlikely(path[depth].p_hdr == NULL)) {
1883 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1884 err = -EIO;
1885 break;
1886 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04001887 ex = path[depth].p_ext;
1888 next = ext4_ext_next_allocated_block(path);
1889
1890 exists = 0;
1891 if (!ex) {
1892 /* there is no extent yet, so try to allocate
1893 * all requested space */
1894 start = block;
1895 end = block + num;
1896 } else if (le32_to_cpu(ex->ee_block) > block) {
1897 /* need to allocate space before found extent */
1898 start = block;
1899 end = le32_to_cpu(ex->ee_block);
1900 if (block + num < end)
1901 end = block + num;
1902 } else if (block >= le32_to_cpu(ex->ee_block)
1903 + ext4_ext_get_actual_len(ex)) {
1904 /* need to allocate space after found extent */
1905 start = block;
1906 end = block + num;
1907 if (end >= next)
1908 end = next;
1909 } else if (block >= le32_to_cpu(ex->ee_block)) {
1910 /*
1911 * some part of requested space is covered
1912 * by found extent
1913 */
1914 start = block;
1915 end = le32_to_cpu(ex->ee_block)
1916 + ext4_ext_get_actual_len(ex);
1917 if (block + num < end)
1918 end = block + num;
1919 exists = 1;
1920 } else {
1921 BUG();
1922 }
1923 BUG_ON(end <= start);
1924
1925 if (!exists) {
1926 cbex.ec_block = start;
1927 cbex.ec_len = end - start;
1928 cbex.ec_start = 0;
1929 cbex.ec_type = EXT4_EXT_CACHE_GAP;
1930 } else {
1931 cbex.ec_block = le32_to_cpu(ex->ee_block);
1932 cbex.ec_len = ext4_ext_get_actual_len(ex);
1933 cbex.ec_start = ext_pblock(ex);
1934 cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
1935 }
1936
Frank Mayhar273df552010-03-02 11:46:09 -05001937 if (unlikely(cbex.ec_len == 0)) {
1938 EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
1939 err = -EIO;
1940 break;
1941 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04001942 err = func(inode, path, &cbex, ex, cbdata);
1943 ext4_ext_drop_refs(path);
1944
1945 if (err < 0)
1946 break;
1947
1948 if (err == EXT_REPEAT)
1949 continue;
1950 else if (err == EXT_BREAK) {
1951 err = 0;
1952 break;
1953 }
1954
1955 if (ext_depth(inode) != depth) {
1956 /* depth was changed. we have to realloc path */
1957 kfree(path);
1958 path = NULL;
1959 }
1960
1961 block = cbex.ec_block + cbex.ec_len;
1962 }
1963
1964 if (path) {
1965 ext4_ext_drop_refs(path);
1966 kfree(path);
1967 }
1968
1969 return err;
1970}
1971
Avantika Mathur09b88252006-12-06 20:41:36 -08001972static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001973ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Mingming Caodd545672007-07-31 00:37:46 -07001974 __u32 len, ext4_fsblk_t start, int type)
Alex Tomasa86c6182006-10-11 01:21:03 -07001975{
1976 struct ext4_ext_cache *cex;
1977 BUG_ON(len == 0);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001978 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001979 cex = &EXT4_I(inode)->i_cached_extent;
1980 cex->ec_type = type;
1981 cex->ec_block = block;
1982 cex->ec_len = len;
1983 cex->ec_start = start;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001984 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001985}
1986
1987/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001988 * ext4_ext_put_gap_in_cache:
1989 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07001990 * and cache this gap
1991 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001992static void
Alex Tomasa86c6182006-10-11 01:21:03 -07001993ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001994 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07001995{
1996 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001997 unsigned long len;
1998 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001999 struct ext4_extent *ex;
2000
2001 ex = path[depth].p_ext;
2002 if (ex == NULL) {
2003 /* there is no extent yet, so gap is [0;-] */
2004 lblock = 0;
2005 len = EXT_MAX_BLOCK;
2006 ext_debug("cache gap(whole file):");
2007 } else if (block < le32_to_cpu(ex->ee_block)) {
2008 lblock = block;
2009 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05002010 ext_debug("cache gap(before): %u [%u:%u]",
2011 block,
2012 le32_to_cpu(ex->ee_block),
2013 ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002014 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002015 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002016 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002017 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002018 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002019
2020 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05002021 ext_debug("cache gap(after): [%u:%u] %u",
2022 le32_to_cpu(ex->ee_block),
2023 ext4_ext_get_actual_len(ex),
2024 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002025 BUG_ON(next == lblock);
2026 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07002027 } else {
2028 lblock = len = 0;
2029 BUG();
2030 }
2031
Eric Sandeenbba90742008-01-28 23:58:27 -05002032 ext_debug(" -> %u:%lu\n", lblock, len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002033 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
2034}
2035
Avantika Mathur09b88252006-12-06 20:41:36 -08002036static int
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002037ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
Alex Tomasa86c6182006-10-11 01:21:03 -07002038 struct ext4_extent *ex)
2039{
2040 struct ext4_ext_cache *cex;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002041 int ret = EXT4_EXT_CACHE_NO;
Alex Tomasa86c6182006-10-11 01:21:03 -07002042
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002043 /*
2044 * We borrow i_block_reservation_lock to protect i_cached_extent
2045 */
2046 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07002047 cex = &EXT4_I(inode)->i_cached_extent;
2048
2049 /* has cache valid data? */
2050 if (cex->ec_type == EXT4_EXT_CACHE_NO)
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002051 goto errout;
Alex Tomasa86c6182006-10-11 01:21:03 -07002052
2053 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
2054 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
2055 if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002056 ex->ee_block = cpu_to_le32(cex->ec_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002057 ext4_ext_store_pblock(ex, cex->ec_start);
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002058 ex->ee_len = cpu_to_le16(cex->ec_len);
Eric Sandeenbba90742008-01-28 23:58:27 -05002059 ext_debug("%u cached by %u:%u:%llu\n",
2060 block,
2061 cex->ec_block, cex->ec_len, cex->ec_start);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002062 ret = cex->ec_type;
Alex Tomasa86c6182006-10-11 01:21:03 -07002063 }
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002064errout:
2065 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
2066 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07002067}
2068
2069/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002070 * ext4_ext_rm_idx:
2071 * removes index from the index block.
2072 * It's used in truncate case only, thus all requests are for
2073 * last index in the block only.
Alex Tomasa86c6182006-10-11 01:21:03 -07002074 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002075static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07002076 struct ext4_ext_path *path)
2077{
Alex Tomasa86c6182006-10-11 01:21:03 -07002078 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002079 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002080
2081 /* free index block */
2082 path--;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002083 leaf = idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002084 if (unlikely(path->p_hdr->eh_entries == 0)) {
2085 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2086 return -EIO;
2087 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002088 err = ext4_ext_get_access(handle, inode, path);
2089 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002090 return err;
Marcin Slusarze8546d02008-04-17 10:38:59 -04002091 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002092 err = ext4_ext_dirty(handle, inode, path);
2093 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002094 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002095 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Theodore Ts'oe6362602009-11-23 07:17:05 -05002096 ext4_free_blocks(handle, inode, 0, leaf, 1,
2097 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Alex Tomasa86c6182006-10-11 01:21:03 -07002098 return err;
2099}
2100
2101/*
Mingming Caoee12b632008-08-19 22:16:05 -04002102 * ext4_ext_calc_credits_for_single_extent:
2103 * This routine returns max. credits that needed to insert an extent
2104 * to the extent tree.
2105 * When pass the actual path, the caller should calculate credits
2106 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002107 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002108int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002109 struct ext4_ext_path *path)
2110{
Alex Tomasa86c6182006-10-11 01:21:03 -07002111 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002112 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002113 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002114
Alex Tomasa86c6182006-10-11 01:21:03 -07002115 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002116 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002117 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2118
2119 /*
2120 * There are some space in the leaf tree, no
2121 * need to account for leaf block credit
2122 *
2123 * bitmaps and block group descriptor blocks
2124 * and other metadat blocks still need to be
2125 * accounted.
2126 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002127 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002128 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002129 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002130 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002131 }
2132
Mingming Cao525f4ed2008-08-19 22:15:58 -04002133 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002134}
Alex Tomasa86c6182006-10-11 01:21:03 -07002135
Mingming Caoee12b632008-08-19 22:16:05 -04002136/*
2137 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2138 *
2139 * if nrblocks are fit in a single extent (chunk flag is 1), then
2140 * in the worse case, each tree level index/leaf need to be changed
2141 * if the tree split due to insert a new extent, then the old tree
2142 * index/leaf need to be updated too
2143 *
2144 * If the nrblocks are discontiguous, they could cause
2145 * the whole tree split more than once, but this is really rare.
2146 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002147int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoee12b632008-08-19 22:16:05 -04002148{
2149 int index;
2150 int depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002151
Mingming Caoee12b632008-08-19 22:16:05 -04002152 if (chunk)
2153 index = depth * 2;
2154 else
2155 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002156
Mingming Caoee12b632008-08-19 22:16:05 -04002157 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002158}
2159
2160static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2161 struct ext4_extent *ex,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002162 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002163{
Amit Aroraa2df2a62007-07-17 21:42:41 -04002164 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe6362602009-11-23 07:17:05 -05002165 int flags = EXT4_FREE_BLOCKS_FORGET;
Alex Tomasa86c6182006-10-11 01:21:03 -07002166
Alex Tomasc9de5602008-01-29 00:19:52 -05002167 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
Theodore Ts'oe6362602009-11-23 07:17:05 -05002168 flags |= EXT4_FREE_BLOCKS_METADATA;
Alex Tomasa86c6182006-10-11 01:21:03 -07002169#ifdef EXTENTS_STATS
2170 {
2171 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002172 spin_lock(&sbi->s_ext_stats_lock);
2173 sbi->s_ext_blocks += ee_len;
2174 sbi->s_ext_extents++;
2175 if (ee_len < sbi->s_ext_min)
2176 sbi->s_ext_min = ee_len;
2177 if (ee_len > sbi->s_ext_max)
2178 sbi->s_ext_max = ee_len;
2179 if (ext_depth(inode) > sbi->s_depth_max)
2180 sbi->s_depth_max = ext_depth(inode);
2181 spin_unlock(&sbi->s_ext_stats_lock);
2182 }
2183#endif
2184 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002185 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002186 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002187 ext4_lblk_t num;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002188 ext4_fsblk_t start;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002189
Amit Aroraa2df2a62007-07-17 21:42:41 -04002190 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2191 start = ext_pblock(ex) + ee_len - num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002192 ext_debug("free last %u blocks starting %llu\n", num, start);
Theodore Ts'oe6362602009-11-23 07:17:05 -05002193 ext4_free_blocks(handle, inode, 0, start, num, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07002194 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002195 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002196 printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
Amit Aroraa2df2a62007-07-17 21:42:41 -04002197 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002198 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002199 printk(KERN_INFO "strange request: removal(2) "
2200 "%u-%u from %u:%u\n",
2201 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002202 }
2203 return 0;
2204}
2205
2206static int
2207ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002208 struct ext4_ext_path *path, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07002209{
2210 int err = 0, correct_index = 0;
2211 int depth = ext_depth(inode), credits;
2212 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002213 ext4_lblk_t a, b, block;
2214 unsigned num;
2215 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002216 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002217 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002218 struct ext4_extent *ex;
2219
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002220 /* the header must be checked already in ext4_ext_remove_space() */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002221 ext_debug("truncate since %u in leaf\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002222 if (!path[depth].p_hdr)
2223 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2224 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002225 if (unlikely(path[depth].p_hdr == NULL)) {
2226 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2227 return -EIO;
2228 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002229 /* find where to start removing */
2230 ex = EXT_LAST_EXTENT(eh);
2231
2232 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002233 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002234
2235 while (ex >= EXT_FIRST_EXTENT(eh) &&
2236 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002237
2238 if (ext4_ext_is_uninitialized(ex))
2239 uninitialized = 1;
2240 else
2241 uninitialized = 0;
2242
Mingming553f9002009-09-18 13:34:55 -04002243 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2244 uninitialized, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002245 path[depth].p_ext = ex;
2246
2247 a = ex_ee_block > start ? ex_ee_block : start;
2248 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
2249 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
2250
2251 ext_debug(" border %u:%u\n", a, b);
2252
2253 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
2254 block = 0;
2255 num = 0;
2256 BUG();
2257 } else if (a != ex_ee_block) {
2258 /* remove tail of the extent */
2259 block = ex_ee_block;
2260 num = a - block;
2261 } else if (b != ex_ee_block + ex_ee_len - 1) {
2262 /* remove head of the extent */
2263 block = a;
2264 num = b - a;
2265 /* there is no "make a hole" API yet */
2266 BUG();
2267 } else {
2268 /* remove whole extent: excellent! */
2269 block = ex_ee_block;
2270 num = 0;
2271 BUG_ON(a != ex_ee_block);
2272 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
2273 }
2274
Theodore Ts'o34071da2008-08-01 21:59:19 -04002275 /*
2276 * 3 for leaf, sb, and inode plus 2 (bmap and group
2277 * descriptor) for each block group; assume two block
2278 * groups plus ex_ee_len/blocks_per_block_group for
2279 * the worst case
2280 */
2281 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002282 if (ex == EXT_FIRST_EXTENT(eh)) {
2283 correct_index = 1;
2284 credits += (ext_depth(inode)) + 1;
2285 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002286 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002287
Jan Kara487caee2009-08-17 22:17:20 -04002288 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
Shen Feng9102e4f2008-07-11 19:27:31 -04002289 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002290 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002291
2292 err = ext4_ext_get_access(handle, inode, path + depth);
2293 if (err)
2294 goto out;
2295
2296 err = ext4_remove_blocks(handle, inode, ex, a, b);
2297 if (err)
2298 goto out;
2299
2300 if (num == 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002301 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002302 ext4_ext_store_pblock(ex, 0);
Marcin Slusarze8546d02008-04-17 10:38:59 -04002303 le16_add_cpu(&eh->eh_entries, -1);
Alex Tomasa86c6182006-10-11 01:21:03 -07002304 }
2305
2306 ex->ee_block = cpu_to_le32(block);
2307 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002308 /*
2309 * Do not mark uninitialized if all the blocks in the
2310 * extent have been removed.
2311 */
2312 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002313 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002314
2315 err = ext4_ext_dirty(handle, inode, path + depth);
2316 if (err)
2317 goto out;
2318
Mingming Cao2ae02102006-10-11 01:21:11 -07002319 ext_debug("new extent: %u:%u:%llu\n", block, num,
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002320 ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002321 ex--;
2322 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002323 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002324 }
2325
2326 if (correct_index && eh->eh_entries)
2327 err = ext4_ext_correct_indexes(handle, inode, path);
2328
2329 /* if this leaf is free, then we should
2330 * remove it from index block above */
2331 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2332 err = ext4_ext_rm_idx(handle, inode, path + depth);
2333
2334out:
2335 return err;
2336}
2337
2338/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002339 * ext4_ext_more_to_rm:
2340 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002341 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002342static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002343ext4_ext_more_to_rm(struct ext4_ext_path *path)
2344{
2345 BUG_ON(path->p_idx == NULL);
2346
2347 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2348 return 0;
2349
2350 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002351 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002352 * so we have to consider current index for truncation
2353 */
2354 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2355 return 0;
2356 return 1;
2357}
2358
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002359static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07002360{
2361 struct super_block *sb = inode->i_sb;
2362 int depth = ext_depth(inode);
2363 struct ext4_ext_path *path;
2364 handle_t *handle;
2365 int i = 0, err = 0;
2366
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002367 ext_debug("truncate since %u\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002368
2369 /* probably first extent we're gonna free will be last in block */
2370 handle = ext4_journal_start(inode, depth + 1);
2371 if (IS_ERR(handle))
2372 return PTR_ERR(handle);
2373
2374 ext4_ext_invalidate_cache(inode);
2375
2376 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002377 * We start scanning from right side, freeing all the blocks
2378 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002379 */
Josef Bacik216553c2008-04-29 22:02:02 -04002380 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07002381 if (path == NULL) {
2382 ext4_journal_stop(handle);
2383 return -ENOMEM;
2384 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002385 path[0].p_hdr = ext_inode_hdr(inode);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002386 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002387 err = -EIO;
2388 goto out;
2389 }
2390 path[0].p_depth = depth;
2391
2392 while (i >= 0 && err == 0) {
2393 if (i == depth) {
2394 /* this is leaf block */
2395 err = ext4_ext_rm_leaf(handle, inode, path, start);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002396 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002397 brelse(path[i].p_bh);
2398 path[i].p_bh = NULL;
2399 i--;
2400 continue;
2401 }
2402
2403 /* this is index block */
2404 if (!path[i].p_hdr) {
2405 ext_debug("initialize header\n");
2406 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002407 }
2408
Alex Tomasa86c6182006-10-11 01:21:03 -07002409 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002410 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002411 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2412 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2413 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2414 path[i].p_hdr,
2415 le16_to_cpu(path[i].p_hdr->eh_entries));
2416 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002417 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002418 path[i].p_idx--;
2419 }
2420
2421 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2422 i, EXT_FIRST_INDEX(path[i].p_hdr),
2423 path[i].p_idx);
2424 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002425 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002426 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002427 ext_debug("move to level %d (block %llu)\n",
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002428 i + 1, idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002429 memset(path + i + 1, 0, sizeof(*path));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002430 bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2431 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002432 /* should we reset i_size? */
2433 err = -EIO;
2434 break;
2435 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002436 if (WARN_ON(i + 1 > depth)) {
2437 err = -EIO;
2438 break;
2439 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002440 if (ext4_ext_check(inode, ext_block_hdr(bh),
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002441 depth - i - 1)) {
2442 err = -EIO;
2443 break;
2444 }
2445 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002446
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002447 /* save actual number of indexes since this
2448 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002449 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2450 i++;
2451 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002452 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002453 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002454 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002455 * handle must be already prepared by the
2456 * truncatei_leaf() */
2457 err = ext4_ext_rm_idx(handle, inode, path + i);
2458 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002459 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002460 brelse(path[i].p_bh);
2461 path[i].p_bh = NULL;
2462 i--;
2463 ext_debug("return to level %d\n", i);
2464 }
2465 }
2466
2467 /* TODO: flexible tree reduction should be here */
2468 if (path->p_hdr->eh_entries == 0) {
2469 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002470 * truncate to zero freed all the tree,
2471 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002472 */
2473 err = ext4_ext_get_access(handle, inode, path);
2474 if (err == 0) {
2475 ext_inode_hdr(inode)->eh_depth = 0;
2476 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04002477 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07002478 err = ext4_ext_dirty(handle, inode, path);
2479 }
2480 }
2481out:
Alex Tomasa86c6182006-10-11 01:21:03 -07002482 ext4_ext_drop_refs(path);
2483 kfree(path);
2484 ext4_journal_stop(handle);
2485
2486 return err;
2487}
2488
2489/*
2490 * called at mount time
2491 */
2492void ext4_ext_init(struct super_block *sb)
2493{
2494 /*
2495 * possible initialization would be here
2496 */
2497
Theodore Ts'o83982b62009-01-06 14:53:16 -05002498 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04002499#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002500 printk(KERN_INFO "EXT4-fs: file extents enabled");
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002501#ifdef AGGRESSIVE_TEST
2502 printk(", aggressive tests");
Alex Tomasa86c6182006-10-11 01:21:03 -07002503#endif
2504#ifdef CHECK_BINSEARCH
2505 printk(", check binsearch");
2506#endif
2507#ifdef EXTENTS_STATS
2508 printk(", stats");
2509#endif
2510 printk("\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04002511#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07002512#ifdef EXTENTS_STATS
2513 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2514 EXT4_SB(sb)->s_ext_min = 1 << 30;
2515 EXT4_SB(sb)->s_ext_max = 0;
2516#endif
2517 }
2518}
2519
2520/*
2521 * called at umount time
2522 */
2523void ext4_ext_release(struct super_block *sb)
2524{
Theodore Ts'o83982b62009-01-06 14:53:16 -05002525 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
Alex Tomasa86c6182006-10-11 01:21:03 -07002526 return;
2527
2528#ifdef EXTENTS_STATS
2529 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2530 struct ext4_sb_info *sbi = EXT4_SB(sb);
2531 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2532 sbi->s_ext_blocks, sbi->s_ext_extents,
2533 sbi->s_ext_blocks / sbi->s_ext_extents);
2534 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2535 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2536 }
2537#endif
2538}
2539
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002540static void bi_complete(struct bio *bio, int error)
2541{
2542 complete((struct completion *)bio->bi_private);
2543}
2544
2545/* FIXME!! we need to try to merge to left or right after zero-out */
2546static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2547{
2548 int ret = -EIO;
2549 struct bio *bio;
2550 int blkbits, blocksize;
2551 sector_t ee_pblock;
2552 struct completion event;
2553 unsigned int ee_len, len, done, offset;
2554
2555
2556 blkbits = inode->i_blkbits;
2557 blocksize = inode->i_sb->s_blocksize;
2558 ee_len = ext4_ext_get_actual_len(ex);
2559 ee_pblock = ext_pblock(ex);
2560
2561 /* convert ee_pblock to 512 byte sectors */
2562 ee_pblock = ee_pblock << (blkbits - 9);
2563
2564 while (ee_len > 0) {
2565
2566 if (ee_len > BIO_MAX_PAGES)
2567 len = BIO_MAX_PAGES;
2568 else
2569 len = ee_len;
2570
2571 bio = bio_alloc(GFP_NOIO, len);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002572 bio->bi_sector = ee_pblock;
2573 bio->bi_bdev = inode->i_sb->s_bdev;
2574
2575 done = 0;
2576 offset = 0;
2577 while (done < len) {
2578 ret = bio_add_page(bio, ZERO_PAGE(0),
2579 blocksize, offset);
2580 if (ret != blocksize) {
2581 /*
2582 * We can't add any more pages because of
2583 * hardware limitations. Start a new bio.
2584 */
2585 break;
2586 }
2587 done++;
2588 offset += blocksize;
2589 if (offset >= PAGE_CACHE_SIZE)
2590 offset = 0;
2591 }
2592
2593 init_completion(&event);
2594 bio->bi_private = &event;
2595 bio->bi_end_io = bi_complete;
2596 submit_bio(WRITE, bio);
2597 wait_for_completion(&event);
2598
2599 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
2600 ret = 0;
2601 else {
2602 ret = -EIO;
2603 break;
2604 }
2605 bio_put(bio);
2606 ee_len -= done;
2607 ee_pblock += done << (blkbits - 9);
2608 }
2609 return ret;
2610}
2611
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002612#define EXT4_EXT_ZERO_LEN 7
Amit Arora56055d32007-07-17 21:42:38 -04002613/*
2614 * This function is called by ext4_ext_get_blocks() if someone tries to write
2615 * to an uninitialized extent. It may result in splitting the uninitialized
2616 * extent into multiple extents (upto three - one initialized and two
2617 * uninitialized).
2618 * There are three possibilities:
2619 * a> There is no split required: Entire extent should be initialized
2620 * b> Splits in two extents: Write is happening at either end of the extent
2621 * c> Splits in three extents: Somone is writing in middle of the extent
2622 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002623static int ext4_ext_convert_to_initialized(handle_t *handle,
2624 struct inode *inode,
2625 struct ext4_ext_path *path,
2626 ext4_lblk_t iblock,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002627 unsigned int max_blocks)
Amit Arora56055d32007-07-17 21:42:38 -04002628{
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002629 struct ext4_extent *ex, newex, orig_ex;
Amit Arora56055d32007-07-17 21:42:38 -04002630 struct ext4_extent *ex1 = NULL;
2631 struct ext4_extent *ex2 = NULL;
2632 struct ext4_extent *ex3 = NULL;
2633 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002634 ext4_lblk_t ee_block;
2635 unsigned int allocated, ee_len, depth;
Amit Arora56055d32007-07-17 21:42:38 -04002636 ext4_fsblk_t newblock;
2637 int err = 0;
2638 int ret = 0;
2639
2640 depth = ext_depth(inode);
2641 eh = path[depth].p_hdr;
2642 ex = path[depth].p_ext;
2643 ee_block = le32_to_cpu(ex->ee_block);
2644 ee_len = ext4_ext_get_actual_len(ex);
2645 allocated = ee_len - (iblock - ee_block);
2646 newblock = iblock - ee_block + ext_pblock(ex);
2647 ex2 = ex;
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002648 orig_ex.ee_block = ex->ee_block;
2649 orig_ex.ee_len = cpu_to_le16(ee_len);
2650 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
Amit Arora56055d32007-07-17 21:42:38 -04002651
Aneesh Kumar K.V9df56432008-02-22 06:17:31 -05002652 err = ext4_ext_get_access(handle, inode, path + depth);
2653 if (err)
2654 goto out;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002655 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2656 if (ee_len <= 2*EXT4_EXT_ZERO_LEN) {
2657 err = ext4_ext_zeroout(inode, &orig_ex);
2658 if (err)
2659 goto fix_extent_len;
2660 /* update the extent length and mark as initialized */
2661 ex->ee_block = orig_ex.ee_block;
2662 ex->ee_len = orig_ex.ee_len;
2663 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2664 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002665 /* zeroed the full extent */
2666 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002667 }
Aneesh Kumar K.V9df56432008-02-22 06:17:31 -05002668
Amit Arora56055d32007-07-17 21:42:38 -04002669 /* ex1: ee_block to iblock - 1 : uninitialized */
2670 if (iblock > ee_block) {
2671 ex1 = ex;
2672 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2673 ext4_ext_mark_uninitialized(ex1);
2674 ex2 = &newex;
2675 }
2676 /*
2677 * for sanity, update the length of the ex2 extent before
2678 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2679 * overlap of blocks.
2680 */
2681 if (!ex1 && allocated > max_blocks)
2682 ex2->ee_len = cpu_to_le16(max_blocks);
2683 /* ex3: to ee_block + ee_len : uninitialised */
2684 if (allocated > max_blocks) {
2685 unsigned int newdepth;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002686 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
2687 if (allocated <= EXT4_EXT_ZERO_LEN) {
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002688 /*
2689 * iblock == ee_block is handled by the zerouout
2690 * at the beginning.
2691 * Mark first half uninitialized.
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002692 * Mark second half initialized and zero out the
2693 * initialized extent
2694 */
2695 ex->ee_block = orig_ex.ee_block;
2696 ex->ee_len = cpu_to_le16(ee_len - allocated);
2697 ext4_ext_mark_uninitialized(ex);
2698 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2699 ext4_ext_dirty(handle, inode, path + depth);
2700
2701 ex3 = &newex;
2702 ex3->ee_block = cpu_to_le32(iblock);
2703 ext4_ext_store_pblock(ex3, newblock);
2704 ex3->ee_len = cpu_to_le16(allocated);
Mingming Cao00314622009-09-28 15:49:08 -04002705 err = ext4_ext_insert_extent(handle, inode, path,
2706 ex3, 0);
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002707 if (err == -ENOSPC) {
2708 err = ext4_ext_zeroout(inode, &orig_ex);
2709 if (err)
2710 goto fix_extent_len;
2711 ex->ee_block = orig_ex.ee_block;
2712 ex->ee_len = orig_ex.ee_len;
2713 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2714 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002715 /* blocks available from iblock */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002716 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002717
2718 } else if (err)
2719 goto fix_extent_len;
2720
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002721 /*
2722 * We need to zero out the second half because
2723 * an fallocate request can update file size and
2724 * converting the second half to initialized extent
2725 * implies that we can leak some junk data to user
2726 * space.
2727 */
2728 err = ext4_ext_zeroout(inode, ex3);
2729 if (err) {
2730 /*
2731 * We should actually mark the
2732 * second half as uninit and return error
2733 * Insert would have changed the extent
2734 */
2735 depth = ext_depth(inode);
2736 ext4_ext_drop_refs(path);
2737 path = ext4_ext_find_extent(inode,
2738 iblock, path);
2739 if (IS_ERR(path)) {
2740 err = PTR_ERR(path);
2741 return err;
2742 }
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002743 /* get the second half extent details */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002744 ex = path[depth].p_ext;
2745 err = ext4_ext_get_access(handle, inode,
2746 path + depth);
2747 if (err)
2748 return err;
2749 ext4_ext_mark_uninitialized(ex);
2750 ext4_ext_dirty(handle, inode, path + depth);
2751 return err;
2752 }
2753
2754 /* zeroed the second half */
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002755 return allocated;
2756 }
Amit Arora56055d32007-07-17 21:42:38 -04002757 ex3 = &newex;
2758 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2759 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2760 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2761 ext4_ext_mark_uninitialized(ex3);
Mingming Cao00314622009-09-28 15:49:08 -04002762 err = ext4_ext_insert_extent(handle, inode, path, ex3, 0);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002763 if (err == -ENOSPC) {
2764 err = ext4_ext_zeroout(inode, &orig_ex);
2765 if (err)
2766 goto fix_extent_len;
2767 /* update the extent length and mark as initialized */
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002768 ex->ee_block = orig_ex.ee_block;
2769 ex->ee_len = orig_ex.ee_len;
2770 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002771 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002772 /* zeroed the full extent */
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002773 /* blocks available from iblock */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002774 return allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002775
2776 } else if (err)
2777 goto fix_extent_len;
Amit Arora56055d32007-07-17 21:42:38 -04002778 /*
2779 * The depth, and hence eh & ex might change
2780 * as part of the insert above.
2781 */
2782 newdepth = ext_depth(inode);
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002783 /*
Coly Li73ac36e2009-01-07 18:09:16 -08002784 * update the extent length after successful insert of the
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002785 * split extent
2786 */
2787 orig_ex.ee_len = cpu_to_le16(ee_len -
2788 ext4_ext_get_actual_len(ex3));
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002789 depth = newdepth;
2790 ext4_ext_drop_refs(path);
2791 path = ext4_ext_find_extent(inode, iblock, path);
2792 if (IS_ERR(path)) {
2793 err = PTR_ERR(path);
2794 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04002795 }
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002796 eh = path[depth].p_hdr;
2797 ex = path[depth].p_ext;
2798 if (ex2 != &newex)
2799 ex2 = ex;
2800
2801 err = ext4_ext_get_access(handle, inode, path + depth);
2802 if (err)
2803 goto out;
2804
Amit Arora56055d32007-07-17 21:42:38 -04002805 allocated = max_blocks;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002806
2807 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2808 * to insert a extent in the middle zerout directly
2809 * otherwise give the extent a chance to merge to left
2810 */
2811 if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
2812 iblock != ee_block) {
2813 err = ext4_ext_zeroout(inode, &orig_ex);
2814 if (err)
2815 goto fix_extent_len;
2816 /* update the extent length and mark as initialized */
2817 ex->ee_block = orig_ex.ee_block;
2818 ex->ee_len = orig_ex.ee_len;
2819 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2820 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002821 /* zero out the first half */
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002822 /* blocks available from iblock */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002823 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002824 }
Amit Arora56055d32007-07-17 21:42:38 -04002825 }
2826 /*
2827 * If there was a change of depth as part of the
2828 * insertion of ex3 above, we need to update the length
2829 * of the ex1 extent again here
2830 */
2831 if (ex1 && ex1 != ex) {
2832 ex1 = ex;
2833 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2834 ext4_ext_mark_uninitialized(ex1);
2835 ex2 = &newex;
2836 }
2837 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2838 ex2->ee_block = cpu_to_le32(iblock);
Amit Arora56055d32007-07-17 21:42:38 -04002839 ext4_ext_store_pblock(ex2, newblock);
2840 ex2->ee_len = cpu_to_le16(allocated);
2841 if (ex2 != ex)
2842 goto insert;
Amit Arora56055d32007-07-17 21:42:38 -04002843 /*
2844 * New (initialized) extent starts from the first block
2845 * in the current extent. i.e., ex2 == ex
2846 * We have to see if it can be merged with the extent
2847 * on the left.
2848 */
2849 if (ex2 > EXT_FIRST_EXTENT(eh)) {
2850 /*
2851 * To merge left, pass "ex2 - 1" to try_to_merge(),
2852 * since it merges towards right _only_.
2853 */
2854 ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
2855 if (ret) {
2856 err = ext4_ext_correct_indexes(handle, inode, path);
2857 if (err)
2858 goto out;
2859 depth = ext_depth(inode);
2860 ex2--;
2861 }
2862 }
2863 /*
2864 * Try to Merge towards right. This might be required
2865 * only when the whole extent is being written to.
2866 * i.e. ex2 == ex and ex3 == NULL.
2867 */
2868 if (!ex3) {
2869 ret = ext4_ext_try_to_merge(inode, path, ex2);
2870 if (ret) {
2871 err = ext4_ext_correct_indexes(handle, inode, path);
2872 if (err)
2873 goto out;
2874 }
2875 }
2876 /* Mark modified extent as dirty */
2877 err = ext4_ext_dirty(handle, inode, path + depth);
2878 goto out;
2879insert:
Mingming Cao00314622009-09-28 15:49:08 -04002880 err = ext4_ext_insert_extent(handle, inode, path, &newex, 0);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002881 if (err == -ENOSPC) {
2882 err = ext4_ext_zeroout(inode, &orig_ex);
2883 if (err)
2884 goto fix_extent_len;
2885 /* update the extent length and mark as initialized */
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002886 ex->ee_block = orig_ex.ee_block;
2887 ex->ee_len = orig_ex.ee_len;
2888 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002889 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002890 /* zero out the first half */
2891 return allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002892 } else if (err)
2893 goto fix_extent_len;
Amit Arora56055d32007-07-17 21:42:38 -04002894out:
Mingming553f9002009-09-18 13:34:55 -04002895 ext4_ext_show_leaf(inode, path);
Amit Arora56055d32007-07-17 21:42:38 -04002896 return err ? err : allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002897
2898fix_extent_len:
2899 ex->ee_block = orig_ex.ee_block;
2900 ex->ee_len = orig_ex.ee_len;
2901 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2902 ext4_ext_mark_uninitialized(ex);
2903 ext4_ext_dirty(handle, inode, path + depth);
2904 return err;
Amit Arora56055d32007-07-17 21:42:38 -04002905}
2906
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05002907/*
Mingming Cao00314622009-09-28 15:49:08 -04002908 * This function is called by ext4_ext_get_blocks() from
2909 * ext4_get_blocks_dio_write() when DIO to write
2910 * to an uninitialized extent.
2911 *
2912 * Writing to an uninitized extent may result in splitting the uninitialized
2913 * extent into multiple /intialized unintialized extents (up to three)
2914 * There are three possibilities:
2915 * a> There is no split required: Entire extent should be uninitialized
2916 * b> Splits in two extents: Write is happening at either end of the extent
2917 * c> Splits in three extents: Somone is writing in middle of the extent
2918 *
2919 * One of more index blocks maybe needed if the extent tree grow after
2920 * the unintialized extent split. To prevent ENOSPC occur at the IO
2921 * complete, we need to split the uninitialized extent before DIO submit
2922 * the IO. The uninitilized extent called at this time will be split
2923 * into three uninitialized extent(at most). After IO complete, the part
2924 * being filled will be convert to initialized by the end_io callback function
2925 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05002926 *
2927 * Returns the size of uninitialized extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04002928 */
2929static int ext4_split_unwritten_extents(handle_t *handle,
2930 struct inode *inode,
2931 struct ext4_ext_path *path,
2932 ext4_lblk_t iblock,
2933 unsigned int max_blocks,
2934 int flags)
2935{
2936 struct ext4_extent *ex, newex, orig_ex;
2937 struct ext4_extent *ex1 = NULL;
2938 struct ext4_extent *ex2 = NULL;
2939 struct ext4_extent *ex3 = NULL;
2940 struct ext4_extent_header *eh;
2941 ext4_lblk_t ee_block;
2942 unsigned int allocated, ee_len, depth;
2943 ext4_fsblk_t newblock;
2944 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04002945
2946 ext_debug("ext4_split_unwritten_extents: inode %lu,"
2947 "iblock %llu, max_blocks %u\n", inode->i_ino,
2948 (unsigned long long)iblock, max_blocks);
2949 depth = ext_depth(inode);
2950 eh = path[depth].p_hdr;
2951 ex = path[depth].p_ext;
2952 ee_block = le32_to_cpu(ex->ee_block);
2953 ee_len = ext4_ext_get_actual_len(ex);
2954 allocated = ee_len - (iblock - ee_block);
2955 newblock = iblock - ee_block + ext_pblock(ex);
2956 ex2 = ex;
2957 orig_ex.ee_block = ex->ee_block;
2958 orig_ex.ee_len = cpu_to_le16(ee_len);
2959 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
2960
2961 /*
Mingmingba230c32009-11-06 04:01:23 -05002962 * If the uninitialized extent begins at the same logical
2963 * block where the write begins, and the write completely
2964 * covers the extent, then we don't need to split it.
Mingming Cao00314622009-09-28 15:49:08 -04002965 */
Mingmingba230c32009-11-06 04:01:23 -05002966 if ((iblock == ee_block) && (allocated <= max_blocks))
2967 return allocated;
Mingming Cao00314622009-09-28 15:49:08 -04002968
2969 err = ext4_ext_get_access(handle, inode, path + depth);
2970 if (err)
2971 goto out;
2972 /* ex1: ee_block to iblock - 1 : uninitialized */
2973 if (iblock > ee_block) {
2974 ex1 = ex;
2975 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2976 ext4_ext_mark_uninitialized(ex1);
2977 ex2 = &newex;
2978 }
2979 /*
2980 * for sanity, update the length of the ex2 extent before
2981 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2982 * overlap of blocks.
2983 */
2984 if (!ex1 && allocated > max_blocks)
2985 ex2->ee_len = cpu_to_le16(max_blocks);
2986 /* ex3: to ee_block + ee_len : uninitialised */
2987 if (allocated > max_blocks) {
2988 unsigned int newdepth;
2989 ex3 = &newex;
2990 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2991 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2992 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2993 ext4_ext_mark_uninitialized(ex3);
2994 err = ext4_ext_insert_extent(handle, inode, path, ex3, flags);
2995 if (err == -ENOSPC) {
2996 err = ext4_ext_zeroout(inode, &orig_ex);
2997 if (err)
2998 goto fix_extent_len;
2999 /* update the extent length and mark as initialized */
3000 ex->ee_block = orig_ex.ee_block;
3001 ex->ee_len = orig_ex.ee_len;
3002 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
3003 ext4_ext_dirty(handle, inode, path + depth);
3004 /* zeroed the full extent */
3005 /* blocks available from iblock */
3006 return allocated;
3007
3008 } else if (err)
3009 goto fix_extent_len;
3010 /*
3011 * The depth, and hence eh & ex might change
3012 * as part of the insert above.
3013 */
3014 newdepth = ext_depth(inode);
3015 /*
3016 * update the extent length after successful insert of the
3017 * split extent
3018 */
3019 orig_ex.ee_len = cpu_to_le16(ee_len -
3020 ext4_ext_get_actual_len(ex3));
3021 depth = newdepth;
3022 ext4_ext_drop_refs(path);
3023 path = ext4_ext_find_extent(inode, iblock, path);
3024 if (IS_ERR(path)) {
3025 err = PTR_ERR(path);
3026 goto out;
3027 }
3028 eh = path[depth].p_hdr;
3029 ex = path[depth].p_ext;
3030 if (ex2 != &newex)
3031 ex2 = ex;
3032
3033 err = ext4_ext_get_access(handle, inode, path + depth);
3034 if (err)
3035 goto out;
3036
3037 allocated = max_blocks;
3038 }
3039 /*
3040 * If there was a change of depth as part of the
3041 * insertion of ex3 above, we need to update the length
3042 * of the ex1 extent again here
3043 */
3044 if (ex1 && ex1 != ex) {
3045 ex1 = ex;
3046 ex1->ee_len = cpu_to_le16(iblock - ee_block);
3047 ext4_ext_mark_uninitialized(ex1);
3048 ex2 = &newex;
3049 }
3050 /*
3051 * ex2: iblock to iblock + maxblocks-1 : to be direct IO written,
3052 * uninitialised still.
3053 */
3054 ex2->ee_block = cpu_to_le32(iblock);
3055 ext4_ext_store_pblock(ex2, newblock);
3056 ex2->ee_len = cpu_to_le16(allocated);
3057 ext4_ext_mark_uninitialized(ex2);
3058 if (ex2 != ex)
3059 goto insert;
3060 /* Mark modified extent as dirty */
3061 err = ext4_ext_dirty(handle, inode, path + depth);
3062 ext_debug("out here\n");
3063 goto out;
3064insert:
3065 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
3066 if (err == -ENOSPC) {
3067 err = ext4_ext_zeroout(inode, &orig_ex);
3068 if (err)
3069 goto fix_extent_len;
3070 /* update the extent length and mark as initialized */
3071 ex->ee_block = orig_ex.ee_block;
3072 ex->ee_len = orig_ex.ee_len;
3073 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
3074 ext4_ext_dirty(handle, inode, path + depth);
3075 /* zero out the first half */
3076 return allocated;
3077 } else if (err)
3078 goto fix_extent_len;
3079out:
3080 ext4_ext_show_leaf(inode, path);
3081 return err ? err : allocated;
3082
3083fix_extent_len:
3084 ex->ee_block = orig_ex.ee_block;
3085 ex->ee_len = orig_ex.ee_len;
3086 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
3087 ext4_ext_mark_uninitialized(ex);
3088 ext4_ext_dirty(handle, inode, path + depth);
3089 return err;
3090}
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003091static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003092 struct inode *inode,
3093 struct ext4_ext_path *path)
3094{
3095 struct ext4_extent *ex;
3096 struct ext4_extent_header *eh;
3097 int depth;
3098 int err = 0;
3099 int ret = 0;
3100
3101 depth = ext_depth(inode);
3102 eh = path[depth].p_hdr;
3103 ex = path[depth].p_ext;
3104
3105 err = ext4_ext_get_access(handle, inode, path + depth);
3106 if (err)
3107 goto out;
3108 /* first mark the extent as initialized */
3109 ext4_ext_mark_initialized(ex);
3110
3111 /*
3112 * We have to see if it can be merged with the extent
3113 * on the left.
3114 */
3115 if (ex > EXT_FIRST_EXTENT(eh)) {
3116 /*
3117 * To merge left, pass "ex - 1" to try_to_merge(),
3118 * since it merges towards right _only_.
3119 */
3120 ret = ext4_ext_try_to_merge(inode, path, ex - 1);
3121 if (ret) {
3122 err = ext4_ext_correct_indexes(handle, inode, path);
3123 if (err)
3124 goto out;
3125 depth = ext_depth(inode);
3126 ex--;
3127 }
3128 }
3129 /*
3130 * Try to Merge towards right.
3131 */
3132 ret = ext4_ext_try_to_merge(inode, path, ex);
3133 if (ret) {
3134 err = ext4_ext_correct_indexes(handle, inode, path);
3135 if (err)
3136 goto out;
3137 depth = ext_depth(inode);
3138 }
3139 /* Mark modified extent as dirty */
3140 err = ext4_ext_dirty(handle, inode, path + depth);
3141out:
3142 ext4_ext_show_leaf(inode, path);
3143 return err;
3144}
3145
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003146static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3147 sector_t block, int count)
3148{
3149 int i;
3150 for (i = 0; i < count; i++)
3151 unmap_underlying_metadata(bdev, block + i);
3152}
3153
Mingming Cao00314622009-09-28 15:49:08 -04003154static int
3155ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
3156 ext4_lblk_t iblock, unsigned int max_blocks,
3157 struct ext4_ext_path *path, int flags,
3158 unsigned int allocated, struct buffer_head *bh_result,
3159 ext4_fsblk_t newblock)
3160{
3161 int ret = 0;
3162 int err = 0;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003163 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Mingming Cao00314622009-09-28 15:49:08 -04003164
3165 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3166 "block %llu, max_blocks %u, flags %d, allocated %u",
3167 inode->i_ino, (unsigned long long)iblock, max_blocks,
3168 flags, allocated);
3169 ext4_ext_show_leaf(inode, path);
3170
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003171 /* get_block() before submit the IO, split the extent */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003172 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Mingming Cao00314622009-09-28 15:49:08 -04003173 ret = ext4_split_unwritten_extents(handle,
3174 inode, path, iblock,
3175 max_blocks, flags);
Mingming5f524952009-11-10 10:48:04 -05003176 /*
3177 * Flag the inode(non aio case) or end_io struct (aio case)
3178 * that this IO needs to convertion to written when IO is
3179 * completed
3180 */
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003181 if (io)
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003182 io->flag = EXT4_IO_UNWRITTEN;
Mingming5f524952009-11-10 10:48:04 -05003183 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003184 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
Jiaying Zhang744692d2010-03-04 16:14:02 -05003185 if (ext4_should_dioread_nolock(inode))
3186 set_buffer_uninit(bh_result);
Mingming Cao00314622009-09-28 15:49:08 -04003187 goto out;
3188 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003189 /* IO end_io complete, convert the filled extent to written */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003190 if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003191 ret = ext4_convert_unwritten_extents_endio(handle, inode,
Mingming Cao00314622009-09-28 15:49:08 -04003192 path);
Jan Karab436b9b2009-12-08 23:51:10 -05003193 if (ret >= 0)
3194 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04003195 goto out2;
3196 }
3197 /* buffered IO case */
3198 /*
3199 * repeat fallocate creation request
3200 * we already have an unwritten extent
3201 */
3202 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3203 goto map_out;
3204
3205 /* buffered READ or buffered write_begin() lookup */
3206 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3207 /*
3208 * We have blocks reserved already. We
3209 * return allocated blocks so that delalloc
3210 * won't do block reservation for us. But
3211 * the buffer head will be unmapped so that
3212 * a read from the block returns 0s.
3213 */
3214 set_buffer_unwritten(bh_result);
3215 goto out1;
3216 }
3217
3218 /* buffered write, writepage time, convert*/
3219 ret = ext4_ext_convert_to_initialized(handle, inode,
3220 path, iblock,
3221 max_blocks);
Jan Karab436b9b2009-12-08 23:51:10 -05003222 if (ret >= 0)
3223 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04003224out:
3225 if (ret <= 0) {
3226 err = ret;
3227 goto out2;
3228 } else
3229 allocated = ret;
3230 set_buffer_new(bh_result);
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003231 /*
3232 * if we allocated more blocks than requested
3233 * we need to make sure we unmap the extra block
3234 * allocated. The actual needed block will get
3235 * unmapped later when we find the buffer_head marked
3236 * new.
3237 */
3238 if (allocated > max_blocks) {
3239 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
3240 newblock + max_blocks,
3241 allocated - max_blocks);
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003242 allocated = max_blocks;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003243 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003244
3245 /*
3246 * If we have done fallocate with the offset that is already
3247 * delayed allocated, we would have block reservation
3248 * and quota reservation done in the delayed write path.
3249 * But fallocate would have already updated quota and block
3250 * count for this offset. So cancel these reservation
3251 */
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05003252 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003253 ext4_da_update_reserve_space(inode, allocated, 0);
3254
Mingming Cao00314622009-09-28 15:49:08 -04003255map_out:
3256 set_buffer_mapped(bh_result);
3257out1:
3258 if (allocated > max_blocks)
3259 allocated = max_blocks;
3260 ext4_ext_show_leaf(inode, path);
3261 bh_result->b_bdev = inode->i_sb->s_bdev;
3262 bh_result->b_blocknr = newblock;
3263out2:
3264 if (path) {
3265 ext4_ext_drop_refs(path);
3266 kfree(path);
3267 }
3268 return err ? err : allocated;
3269}
3270/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05003271 * Block allocation/map/preallocation routine for extents based files
3272 *
3273 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003274 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003275 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3276 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05003277 *
3278 * return > 0, number of of blocks already mapped/allocated
3279 * if create == 0 and these are pre-allocated blocks
3280 * buffer head is unmapped
3281 * otherwise blocks are mapped
3282 *
3283 * return = 0, if plain look up failed (blocks have not been allocated)
3284 * buffer head is unmapped
3285 *
3286 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003287 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07003288int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003289 ext4_lblk_t iblock,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003290 unsigned int max_blocks, struct buffer_head *bh_result,
Theodore Ts'oc2177052009-05-14 00:58:52 -04003291 int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07003292{
3293 struct ext4_ext_path *path = NULL;
Amit Arora56055d32007-07-17 21:42:38 -04003294 struct ext4_extent_header *eh;
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003295 struct ext4_extent newex, *ex, *last_ex;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003296 ext4_fsblk_t newblock;
3297 int err = 0, depth, ret, cache_type;
3298 unsigned int allocated = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003299 struct ext4_allocation_request ar;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003300 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Alex Tomasa86c6182006-10-11 01:21:03 -07003301
3302 __clear_bit(BH_New, &bh_result->b_state);
Mingming84fe3be2009-09-01 08:44:37 -04003303 ext_debug("blocks %u/%u requested for inode %lu\n",
Eric Sandeenbba90742008-01-28 23:58:27 -05003304 iblock, max_blocks, inode->i_ino);
Alex Tomasa86c6182006-10-11 01:21:03 -07003305
3306 /* check in cache */
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003307 cache_type = ext4_ext_in_cache(inode, iblock, &newex);
3308 if (cache_type) {
3309 if (cache_type == EXT4_EXT_CACHE_GAP) {
Theodore Ts'oc2177052009-05-14 00:58:52 -04003310 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003311 /*
3312 * block isn't allocated yet and
3313 * user doesn't want to allocate it
3314 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003315 goto out2;
3316 }
3317 /* we should allocate requested block */
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003318 } else if (cache_type == EXT4_EXT_CACHE_EXTENT) {
Alex Tomasa86c6182006-10-11 01:21:03 -07003319 /* block is already allocated */
Dave Kleikamp8c55e202007-05-24 13:04:54 -04003320 newblock = iblock
3321 - le32_to_cpu(newex.ee_block)
3322 + ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003323 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003324 allocated = ext4_ext_get_actual_len(&newex) -
Alex Tomasa86c6182006-10-11 01:21:03 -07003325 (iblock - le32_to_cpu(newex.ee_block));
3326 goto out;
3327 } else {
3328 BUG();
3329 }
3330 }
3331
3332 /* find extent for this block */
3333 path = ext4_ext_find_extent(inode, iblock, NULL);
3334 if (IS_ERR(path)) {
3335 err = PTR_ERR(path);
3336 path = NULL;
3337 goto out2;
3338 }
3339
3340 depth = ext_depth(inode);
3341
3342 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003343 * consistent leaf must not be empty;
3344 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07003345 * this is why assert can't be put in ext4_ext_find_extent()
3346 */
Frank Mayhar273df552010-03-02 11:46:09 -05003347 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
3348 EXT4_ERROR_INODE(inode, "bad extent address "
3349 "iblock: %d, depth: %d pblock %lld",
3350 iblock, depth, path[depth].p_block);
Surbhi Palande034fb4c2009-12-14 09:53:52 -05003351 err = -EIO;
3352 goto out2;
3353 }
Amit Arora56055d32007-07-17 21:42:38 -04003354 eh = path[depth].p_hdr;
Alex Tomasa86c6182006-10-11 01:21:03 -07003355
Avantika Mathur7e028972006-12-06 20:41:33 -08003356 ex = path[depth].p_ext;
3357 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003358 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07003359 ext4_fsblk_t ee_start = ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003360 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003361
3362 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003363 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04003364 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003365 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003366 ee_len = ext4_ext_get_actual_len(ex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003367 /* if found extent covers block, simply return it */
Dave Kleikamp8c55e202007-05-24 13:04:54 -04003368 if (iblock >= ee_block && iblock < ee_block + ee_len) {
Alex Tomasa86c6182006-10-11 01:21:03 -07003369 newblock = iblock - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003370 /* number of remaining blocks in the extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07003371 allocated = ee_len - (iblock - ee_block);
Mingming84fe3be2009-09-01 08:44:37 -04003372 ext_debug("%u fit into %u:%d -> %llu\n", iblock,
Alex Tomasa86c6182006-10-11 01:21:03 -07003373 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04003374
Amit Aroraa2df2a62007-07-17 21:42:41 -04003375 /* Do not put uninitialized extent in the cache */
Amit Arora56055d32007-07-17 21:42:38 -04003376 if (!ext4_ext_is_uninitialized(ex)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04003377 ext4_ext_put_in_cache(inode, ee_block,
3378 ee_len, ee_start,
3379 EXT4_EXT_CACHE_EXTENT);
Amit Arora56055d32007-07-17 21:42:38 -04003380 goto out;
3381 }
Mingming Cao00314622009-09-28 15:49:08 -04003382 ret = ext4_ext_handle_uninitialized_extents(handle,
3383 inode, iblock, max_blocks, path,
3384 flags, allocated, bh_result, newblock);
3385 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07003386 }
3387 }
3388
3389 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003390 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07003391 * we couldn't try to create block if create flag is zero
3392 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04003393 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003394 /*
3395 * put just found gap into cache to speed up
3396 * subsequent requests
3397 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003398 ext4_ext_put_gap_in_cache(inode, path, iblock);
3399 goto out2;
3400 }
3401 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003402 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07003403 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003404
Alex Tomasc9de5602008-01-29 00:19:52 -05003405 /* find neighbour allocated blocks */
3406 ar.lleft = iblock;
3407 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3408 if (err)
3409 goto out2;
3410 ar.lright = iblock;
3411 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
3412 if (err)
3413 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04003414
Amit Arora749269f2007-07-18 09:02:56 -04003415 /*
3416 * See if request is beyond maximum number of blocks we can have in
3417 * a single extent. For an initialized extent this limit is
3418 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3419 * EXT_UNINIT_MAX_LEN.
3420 */
3421 if (max_blocks > EXT_INIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003422 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Amit Arora749269f2007-07-18 09:02:56 -04003423 max_blocks = EXT_INIT_MAX_LEN;
3424 else if (max_blocks > EXT_UNINIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003425 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Amit Arora749269f2007-07-18 09:02:56 -04003426 max_blocks = EXT_UNINIT_MAX_LEN;
3427
Amit Arora25d14f92007-05-24 13:04:13 -04003428 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
3429 newex.ee_block = cpu_to_le32(iblock);
3430 newex.ee_len = cpu_to_le16(max_blocks);
3431 err = ext4_ext_check_overlap(inode, &newex, path);
3432 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003433 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04003434 else
3435 allocated = max_blocks;
Alex Tomasc9de5602008-01-29 00:19:52 -05003436
3437 /* allocate new block */
3438 ar.inode = inode;
3439 ar.goal = ext4_ext_find_goal(inode, path, iblock);
3440 ar.logical = iblock;
3441 ar.len = allocated;
3442 if (S_ISREG(inode->i_mode))
3443 ar.flags = EXT4_MB_HINT_DATA;
3444 else
3445 /* disable in-core preallocation for non-regular files */
3446 ar.flags = 0;
3447 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07003448 if (!newblock)
3449 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04003450 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003451 ar.goal, newblock, allocated);
Alex Tomasa86c6182006-10-11 01:21:03 -07003452
3453 /* try to insert new extent into found leaf and return */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07003454 ext4_ext_store_pblock(&newex, newblock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003455 newex.ee_len = cpu_to_le16(ar.len);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003456 /* Mark uninitialized */
3457 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
Amit Aroraa2df2a62007-07-17 21:42:41 -04003458 ext4_ext_mark_uninitialized(&newex);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003459 /*
Jiaying Zhang744692d2010-03-04 16:14:02 -05003460 * io_end structure was created for every IO write to an
3461 * uninitialized extent. To avoid unecessary conversion,
3462 * here we flag the IO that really needs the conversion.
Mingming5f524952009-11-10 10:48:04 -05003463 * For non asycn direct IO case, flag the inode state
3464 * that we need to perform convertion when IO is done.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003465 */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003466 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Mingming5f524952009-11-10 10:48:04 -05003467 if (io)
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003468 io->flag = EXT4_IO_UNWRITTEN;
Mingming5f524952009-11-10 10:48:04 -05003469 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003470 ext4_set_inode_state(inode,
3471 EXT4_STATE_DIO_UNWRITTEN);
Mingming5f524952009-11-10 10:48:04 -05003472 }
Jiaying Zhang744692d2010-03-04 16:14:02 -05003473 if (ext4_should_dioread_nolock(inode))
3474 set_buffer_uninit(bh_result);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003475 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003476
3477 if (unlikely(EXT4_I(inode)->i_flags & EXT4_EOFBLOCKS_FL)) {
Frank Mayhar273df552010-03-02 11:46:09 -05003478 if (unlikely(!eh->eh_entries)) {
3479 EXT4_ERROR_INODE(inode,
3480 "eh->eh_entries == 0 ee_block %d",
3481 ex->ee_block);
3482 err = -EIO;
3483 goto out2;
3484 }
3485 last_ex = EXT_LAST_EXTENT(eh);
3486 if (iblock + ar.len > le32_to_cpu(last_ex->ee_block)
3487 + ext4_ext_get_actual_len(last_ex))
3488 EXT4_I(inode)->i_flags &= ~EXT4_EOFBLOCKS_FL;
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003489 }
Mingming Cao00314622009-09-28 15:49:08 -04003490 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
Alex Tomas315054f2007-05-24 13:04:25 -04003491 if (err) {
3492 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05003493 /* not a good idea to call discard here directly,
3494 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003495 ext4_discard_preallocations(inode);
Theodore Ts'oe6362602009-11-23 07:17:05 -05003496 ext4_free_blocks(handle, inode, 0, ext_pblock(&newex),
3497 ext4_ext_get_actual_len(&newex), 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07003498 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04003499 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003500
Alex Tomasa86c6182006-10-11 01:21:03 -07003501 /* previous routine could use block we allocated */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07003502 newblock = ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003503 allocated = ext4_ext_get_actual_len(&newex);
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003504 if (allocated > max_blocks)
3505 allocated = max_blocks;
Eric Sandeen953e6222008-07-11 19:27:31 -04003506 set_buffer_new(bh_result);
Alex Tomasa86c6182006-10-11 01:21:03 -07003507
Jan Karab436b9b2009-12-08 23:51:10 -05003508 /*
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003509 * Update reserved blocks/metadata blocks after successful
3510 * block allocation which had been deferred till now.
3511 */
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05003512 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003513 ext4_da_update_reserve_space(inode, allocated, 1);
3514
3515 /*
Jan Karab436b9b2009-12-08 23:51:10 -05003516 * Cache the extent and update transaction to commit on fdatasync only
3517 * when it is _not_ an uninitialized extent.
3518 */
3519 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04003520 ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
3521 EXT4_EXT_CACHE_EXTENT);
Jan Karab436b9b2009-12-08 23:51:10 -05003522 ext4_update_inode_fsync_trans(handle, inode, 1);
3523 } else
3524 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07003525out:
3526 if (allocated > max_blocks)
3527 allocated = max_blocks;
3528 ext4_ext_show_leaf(inode, path);
Eric Sandeen953e6222008-07-11 19:27:31 -04003529 set_buffer_mapped(bh_result);
Alex Tomasa86c6182006-10-11 01:21:03 -07003530 bh_result->b_bdev = inode->i_sb->s_bdev;
3531 bh_result->b_blocknr = newblock;
3532out2:
3533 if (path) {
3534 ext4_ext_drop_refs(path);
3535 kfree(path);
3536 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003537 return err ? err : allocated;
3538}
3539
Jan Karacf108bc2008-07-11 19:27:31 -04003540void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07003541{
3542 struct address_space *mapping = inode->i_mapping;
3543 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003544 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07003545 handle_t *handle;
3546 int err = 0;
3547
3548 /*
3549 * probably first extent we're gonna free will be last in block
3550 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04003551 err = ext4_writepage_trans_blocks(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07003552 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04003553 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07003554 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07003555
Jan Karacf108bc2008-07-11 19:27:31 -04003556 if (inode->i_size & (sb->s_blocksize - 1))
3557 ext4_block_truncate_page(handle, mapping, inode->i_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07003558
Jan Kara9ddfc3d2008-07-11 19:27:31 -04003559 if (ext4_orphan_add(handle, inode))
3560 goto out_stop;
3561
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003562 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07003563 ext4_ext_invalidate_cache(inode);
3564
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003565 ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003566
Alex Tomasa86c6182006-10-11 01:21:03 -07003567 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003568 * TODO: optimization is possible here.
3569 * Probably we need not scan at all,
3570 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07003571 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003572
3573 /* we have to know where to truncate from in crash case */
3574 EXT4_I(inode)->i_disksize = inode->i_size;
3575 ext4_mark_inode_dirty(handle, inode);
3576
3577 last_block = (inode->i_size + sb->s_blocksize - 1)
3578 >> EXT4_BLOCK_SIZE_BITS(sb);
3579 err = ext4_ext_remove_space(inode, last_block);
3580
3581 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04003582 * transaction synchronous.
3583 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003584 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05003585 ext4_handle_sync(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07003586
3587out_stop:
Jan Kara9ddfc3d2008-07-11 19:27:31 -04003588 up_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07003589 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003590 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07003591 * then we need to clear up the orphan record which we created above.
3592 * However, if this was a real unlink then we were called by
3593 * ext4_delete_inode(), and we allow that function to clean up the
3594 * orphan info for us.
3595 */
3596 if (inode->i_nlink)
3597 ext4_orphan_del(handle, inode);
3598
Solofo Ramangalahyef737722008-04-29 22:00:41 -04003599 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3600 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07003601 ext4_journal_stop(handle);
3602}
3603
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003604static void ext4_falloc_update_inode(struct inode *inode,
3605 int mode, loff_t new_size, int update_ctime)
3606{
3607 struct timespec now;
3608
3609 if (update_ctime) {
3610 now = current_fs_time(inode->i_sb);
3611 if (!timespec_equal(&inode->i_ctime, &now))
3612 inode->i_ctime = now;
3613 }
3614 /*
3615 * Update only when preallocation was requested beyond
3616 * the file size.
3617 */
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003618 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3619 if (new_size > i_size_read(inode))
3620 i_size_write(inode, new_size);
3621 if (new_size > EXT4_I(inode)->i_disksize)
3622 ext4_update_i_disksize(inode, new_size);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003623 } else {
3624 /*
3625 * Mark that we allocate beyond EOF so the subsequent truncate
3626 * can proceed even if the new size is the same as i_size.
3627 */
3628 if (new_size > i_size_read(inode))
3629 EXT4_I(inode)->i_flags |= EXT4_EOFBLOCKS_FL;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003630 }
3631
3632}
3633
Amit Aroraa2df2a62007-07-17 21:42:41 -04003634/*
3635 * preallocate space for a file. This implements ext4's fallocate inode
3636 * operation, which gets called from sys_fallocate system call.
3637 * For block-mapped files, posix_fallocate should fall back to the method
3638 * of writing zeroes to the required new blocks (the same behavior which is
3639 * expected for file systems which do not support fallocate() system call).
3640 */
3641long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
3642{
3643 handle_t *handle;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003644 ext4_lblk_t block;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003645 loff_t new_size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003646 unsigned int max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003647 int ret = 0;
3648 int ret2 = 0;
3649 int retries = 0;
3650 struct buffer_head map_bh;
3651 unsigned int credits, blkbits = inode->i_blkbits;
3652
3653 /*
3654 * currently supporting (pre)allocate mode for extent-based
3655 * files _only_
3656 */
3657 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
3658 return -EOPNOTSUPP;
3659
3660 /* preallocation to directories is currently not supported */
3661 if (S_ISDIR(inode->i_mode))
3662 return -ENODEV;
3663
3664 block = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003665 /*
3666 * We can't just convert len to max_blocks because
3667 * If blocksize = 4096 offset = 3072 and len = 2048
3668 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003669 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003670 - block;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003671 /*
Mingming Caof3bd1f32008-08-19 22:16:03 -04003672 * credits to insert 1 extent into extent tree
Amit Aroraa2df2a62007-07-17 21:42:41 -04003673 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04003674 credits = ext4_chunk_trans_blocks(inode, max_blocks);
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05003675 mutex_lock(&inode->i_mutex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003676retry:
3677 while (ret >= 0 && ret < max_blocks) {
3678 block = block + ret;
3679 max_blocks = max_blocks - ret;
3680 handle = ext4_journal_start(inode, credits);
3681 if (IS_ERR(handle)) {
3682 ret = PTR_ERR(handle);
3683 break;
3684 }
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04003685 map_bh.b_state = 0;
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04003686 ret = ext4_get_blocks(handle, inode, block,
3687 max_blocks, &map_bh,
Theodore Ts'oc2177052009-05-14 00:58:52 -04003688 EXT4_GET_BLOCKS_CREATE_UNINIT_EXT);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05003689 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003690#ifdef EXT4FS_DEBUG
3691 WARN_ON(ret <= 0);
3692 printk(KERN_ERR "%s: ext4_ext_get_blocks "
3693 "returned error inode#%lu, block=%u, "
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05003694 "max_blocks=%u", __func__,
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05003695 inode->i_ino, block, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003696#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04003697 ext4_mark_inode_dirty(handle, inode);
3698 ret2 = ext4_journal_stop(handle);
3699 break;
3700 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003701 if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
3702 blkbits) >> blkbits))
3703 new_size = offset + len;
3704 else
3705 new_size = (block + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003706
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003707 ext4_falloc_update_inode(inode, mode, new_size,
3708 buffer_new(&map_bh));
Amit Aroraa2df2a62007-07-17 21:42:41 -04003709 ext4_mark_inode_dirty(handle, inode);
3710 ret2 = ext4_journal_stop(handle);
3711 if (ret2)
3712 break;
3713 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003714 if (ret == -ENOSPC &&
3715 ext4_should_retry_alloc(inode->i_sb, &retries)) {
3716 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003717 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003718 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05003719 mutex_unlock(&inode->i_mutex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003720 return ret > 0 ? ret2 : ret;
3721}
Eric Sandeen6873fa02008-10-07 00:46:36 -04003722
3723/*
Mingming Cao00314622009-09-28 15:49:08 -04003724 * This function convert a range of blocks to written extents
3725 * The caller of this function will pass the start offset and the size.
3726 * all unwritten extents within this range will be converted to
3727 * written extents.
3728 *
3729 * This function is called from the direct IO end io call back
3730 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05003731 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04003732 */
3733int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
Eric Sandeena1de02d2010-02-04 23:58:38 -05003734 ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04003735{
3736 handle_t *handle;
3737 ext4_lblk_t block;
3738 unsigned int max_blocks;
3739 int ret = 0;
3740 int ret2 = 0;
3741 struct buffer_head map_bh;
3742 unsigned int credits, blkbits = inode->i_blkbits;
3743
3744 block = offset >> blkbits;
3745 /*
3746 * We can't just convert len to max_blocks because
3747 * If blocksize = 4096 offset = 3072 and len = 2048
3748 */
3749 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
3750 - block;
3751 /*
3752 * credits to insert 1 extent into extent tree
3753 */
3754 credits = ext4_chunk_trans_blocks(inode, max_blocks);
3755 while (ret >= 0 && ret < max_blocks) {
3756 block = block + ret;
3757 max_blocks = max_blocks - ret;
3758 handle = ext4_journal_start(inode, credits);
3759 if (IS_ERR(handle)) {
3760 ret = PTR_ERR(handle);
3761 break;
3762 }
3763 map_bh.b_state = 0;
3764 ret = ext4_get_blocks(handle, inode, block,
3765 max_blocks, &map_bh,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003766 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Mingming Cao00314622009-09-28 15:49:08 -04003767 if (ret <= 0) {
3768 WARN_ON(ret <= 0);
3769 printk(KERN_ERR "%s: ext4_ext_get_blocks "
3770 "returned error inode#%lu, block=%u, "
3771 "max_blocks=%u", __func__,
3772 inode->i_ino, block, max_blocks);
3773 }
3774 ext4_mark_inode_dirty(handle, inode);
3775 ret2 = ext4_journal_stop(handle);
3776 if (ret <= 0 || ret2 )
3777 break;
3778 }
3779 return ret > 0 ? ret2 : ret;
3780}
3781/*
Eric Sandeen6873fa02008-10-07 00:46:36 -04003782 * Callback function called for each extent to gather FIEMAP information.
3783 */
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003784static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path,
Eric Sandeen6873fa02008-10-07 00:46:36 -04003785 struct ext4_ext_cache *newex, struct ext4_extent *ex,
3786 void *data)
3787{
3788 struct fiemap_extent_info *fieinfo = data;
Eric Sandeenc9877b22009-05-01 23:32:06 -04003789 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003790 __u64 logical;
3791 __u64 physical;
3792 __u64 length;
3793 __u32 flags = 0;
3794 int error;
3795
3796 logical = (__u64)newex->ec_block << blksize_bits;
3797
3798 if (newex->ec_type == EXT4_EXT_CACHE_GAP) {
3799 pgoff_t offset;
3800 struct page *page;
3801 struct buffer_head *bh = NULL;
3802
3803 offset = logical >> PAGE_SHIFT;
3804 page = find_get_page(inode->i_mapping, offset);
3805 if (!page || !page_has_buffers(page))
3806 return EXT_CONTINUE;
3807
3808 bh = page_buffers(page);
3809
3810 if (!bh)
3811 return EXT_CONTINUE;
3812
3813 if (buffer_delay(bh)) {
3814 flags |= FIEMAP_EXTENT_DELALLOC;
3815 page_cache_release(page);
3816 } else {
3817 page_cache_release(page);
3818 return EXT_CONTINUE;
3819 }
3820 }
3821
3822 physical = (__u64)newex->ec_start << blksize_bits;
3823 length = (__u64)newex->ec_len << blksize_bits;
3824
3825 if (ex && ext4_ext_is_uninitialized(ex))
3826 flags |= FIEMAP_EXTENT_UNWRITTEN;
3827
3828 /*
3829 * If this extent reaches EXT_MAX_BLOCK, it must be last.
3830 *
3831 * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
3832 * this also indicates no more allocated blocks.
3833 *
3834 * XXX this might miss a single-block extent at EXT_MAX_BLOCK
3835 */
Eric Sandeenc9877b22009-05-01 23:32:06 -04003836 if (ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK ||
Theodore Ts'oeefd7f02009-05-02 19:05:37 -04003837 newex->ec_block + newex->ec_len - 1 == EXT_MAX_BLOCK) {
3838 loff_t size = i_size_read(inode);
3839 loff_t bs = EXT4_BLOCK_SIZE(inode->i_sb);
3840
Eric Sandeen6873fa02008-10-07 00:46:36 -04003841 flags |= FIEMAP_EXTENT_LAST;
Theodore Ts'oeefd7f02009-05-02 19:05:37 -04003842 if ((flags & FIEMAP_EXTENT_DELALLOC) &&
3843 logical+length > size)
3844 length = (size - logical + bs - 1) & ~(bs-1);
3845 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04003846
3847 error = fiemap_fill_next_extent(fieinfo, logical, physical,
3848 length, flags);
3849 if (error < 0)
3850 return error;
3851 if (error == 1)
3852 return EXT_BREAK;
3853
3854 return EXT_CONTINUE;
3855}
3856
3857/* fiemap flags we can handle specified here */
3858#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
3859
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003860static int ext4_xattr_fiemap(struct inode *inode,
3861 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04003862{
3863 __u64 physical = 0;
3864 __u64 length;
3865 __u32 flags = FIEMAP_EXTENT_LAST;
3866 int blockbits = inode->i_sb->s_blocksize_bits;
3867 int error = 0;
3868
3869 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003870 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04003871 struct ext4_iloc iloc;
3872 int offset; /* offset of xattr in inode */
3873
3874 error = ext4_get_inode_loc(inode, &iloc);
3875 if (error)
3876 return error;
3877 physical = iloc.bh->b_blocknr << blockbits;
3878 offset = EXT4_GOOD_OLD_INODE_SIZE +
3879 EXT4_I(inode)->i_extra_isize;
3880 physical += offset;
3881 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
3882 flags |= FIEMAP_EXTENT_DATA_INLINE;
3883 } else { /* external block */
3884 physical = EXT4_I(inode)->i_file_acl << blockbits;
3885 length = inode->i_sb->s_blocksize;
3886 }
3887
3888 if (physical)
3889 error = fiemap_fill_next_extent(fieinfo, 0, physical,
3890 length, flags);
3891 return (error < 0 ? error : 0);
3892}
3893
3894int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3895 __u64 start, __u64 len)
3896{
3897 ext4_lblk_t start_blk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003898 int error = 0;
3899
3900 /* fallback to generic here if not in extents fmt */
3901 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
3902 return generic_block_fiemap(inode, fieinfo, start, len,
3903 ext4_get_block);
3904
3905 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
3906 return -EBADR;
3907
3908 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
3909 error = ext4_xattr_fiemap(inode, fieinfo);
3910 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05003911 ext4_lblk_t len_blks;
3912 __u64 last_blk;
3913
Eric Sandeen6873fa02008-10-07 00:46:36 -04003914 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05003915 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
3916 if (last_blk >= EXT_MAX_BLOCK)
3917 last_blk = EXT_MAX_BLOCK-1;
3918 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003919
3920 /*
3921 * Walk the extent tree gathering extent information.
3922 * ext4_ext_fiemap_cb will push extents back to user.
3923 */
Eric Sandeen6873fa02008-10-07 00:46:36 -04003924 error = ext4_ext_walk_space(inode, start_blk, len_blks,
3925 ext4_ext_fiemap_cb, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04003926 }
3927
3928 return error;
3929}
3930