blob: bf029c7d5518d1352d276fe4d26206be3d3007be [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);
Dmitry Monakhov0617b832010-05-17 01:00:00 -0400110 if (err == 0)
111 err = -EAGAIN;
Jan Kara487caee2009-08-17 22:17:20 -0400112
113 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -0700114}
115
116/*
117 * could return:
118 * - EROFS
119 * - ENOMEM
120 */
121static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
122 struct ext4_ext_path *path)
123{
124 if (path->p_bh) {
125 /* path points to block */
126 return ext4_journal_get_write_access(handle, path->p_bh);
127 }
128 /* path points to leaf/index in inode body */
129 /* we use in-core data, no need to protect them */
130 return 0;
131}
132
133/*
134 * could return:
135 * - EROFS
136 * - ENOMEM
137 * - EIO
138 */
139static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
140 struct ext4_ext_path *path)
141{
142 int err;
143 if (path->p_bh) {
144 /* path points to block */
Frank Mayhar03901312009-01-07 00:06:22 -0500145 err = ext4_handle_dirty_metadata(handle, inode, path->p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700146 } else {
147 /* path points to leaf/index in inode body */
148 err = ext4_mark_inode_dirty(handle, inode);
149 }
150 return err;
151}
152
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700153static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700154 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500155 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700156{
157 struct ext4_inode_info *ei = EXT4_I(inode);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700158 ext4_fsblk_t bg_start;
Valerie Clement74d34872008-02-15 13:43:07 -0500159 ext4_fsblk_t last_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700160 ext4_grpblk_t colour;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400161 ext4_group_t block_group;
162 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -0700163 int depth;
164
165 if (path) {
166 struct ext4_extent *ex;
167 depth = path->p_depth;
168
169 /* try to predict block placement */
Avantika Mathur7e028972006-12-06 20:41:33 -0800170 ex = path[depth].p_ext;
171 if (ex)
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700172 return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700173
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700174 /* it looks like index is empty;
175 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700176 if (path[depth].p_bh)
177 return path[depth].p_bh->b_blocknr;
178 }
179
180 /* OK. use inode's group */
Theodore Ts'oa4912122009-03-12 12:18:34 -0400181 block_group = ei->i_block_group;
182 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
183 /*
184 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
Theodore Ts'o60e66792010-05-17 07:00:00 -0400185 * block groups per flexgroup, reserve the first block
186 * group for directories and special files. Regular
Theodore Ts'oa4912122009-03-12 12:18:34 -0400187 * files will start at the second block group. This
Theodore Ts'o60e66792010-05-17 07:00:00 -0400188 * tends to speed up directory access and improves
Theodore Ts'oa4912122009-03-12 12:18:34 -0400189 * fsck times.
190 */
191 block_group &= ~(flex_size-1);
192 if (S_ISREG(inode->i_mode))
193 block_group++;
194 }
Akinobu Mita5661bd62010-03-03 23:53:39 -0500195 bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
Valerie Clement74d34872008-02-15 13:43:07 -0500196 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
197
Theodore Ts'oa4912122009-03-12 12:18:34 -0400198 /*
199 * If we are doing delayed allocation, we don't need take
200 * colour into account.
201 */
202 if (test_opt(inode->i_sb, DELALLOC))
203 return bg_start;
204
Valerie Clement74d34872008-02-15 13:43:07 -0500205 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
206 colour = (current->pid % 16) *
Alex Tomasa86c6182006-10-11 01:21:03 -0700207 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
Valerie Clement74d34872008-02-15 13:43:07 -0500208 else
209 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
Alex Tomasa86c6182006-10-11 01:21:03 -0700210 return bg_start + colour + block;
211}
212
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400213/*
214 * Allocation for a meta data block
215 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700216static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400217ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700218 struct ext4_ext_path *path,
219 struct ext4_extent *ex, int *err)
220{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700221 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700222
223 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Theodore Ts'o97df5d12008-12-12 12:41:28 -0500224 newblock = ext4_new_meta_blocks(handle, inode, goal, NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700225 return newblock;
226}
227
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400228static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700229{
230 int size;
231
232 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
233 / sizeof(struct ext4_extent);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400234 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100235#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400236 if (size > 6)
237 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700238#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400239 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700240 return size;
241}
242
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400243static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700244{
245 int size;
246
247 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
248 / sizeof(struct ext4_extent_idx);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400249 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100250#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400251 if (size > 5)
252 size = 5;
Alex Tomasa86c6182006-10-11 01:21:03 -0700253#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400254 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700255 return size;
256}
257
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400258static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700259{
260 int size;
261
262 size = sizeof(EXT4_I(inode)->i_data);
263 size -= sizeof(struct ext4_extent_header);
264 size /= sizeof(struct ext4_extent);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400265 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100266#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400267 if (size > 3)
268 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700269#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400270 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700271 return size;
272}
273
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400274static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700275{
276 int size;
277
278 size = sizeof(EXT4_I(inode)->i_data);
279 size -= sizeof(struct ext4_extent_header);
280 size /= sizeof(struct ext4_extent_idx);
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400281 if (!check) {
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100282#ifdef AGGRESSIVE_TEST
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400283 if (size > 4)
284 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700285#endif
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400286 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700287 return size;
288}
289
Mingming Caod2a17632008-07-14 17:52:37 -0400290/*
291 * Calculate the number of metadata blocks needed
292 * to allocate @blocks
293 * Worse case is one block per extent
294 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500295int ext4_ext_calc_metadata_amount(struct inode *inode, sector_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -0400296{
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500297 struct ext4_inode_info *ei = EXT4_I(inode);
298 int idxs, num = 0;
Mingming Caod2a17632008-07-14 17:52:37 -0400299
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500300 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
301 / sizeof(struct ext4_extent_idx));
Mingming Caod2a17632008-07-14 17:52:37 -0400302
303 /*
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500304 * If the new delayed allocation block is contiguous with the
305 * previous da block, it can share index blocks with the
306 * previous block, so we only need to allocate a new index
307 * block every idxs leaf blocks. At ldxs**2 blocks, we need
308 * an additional index block, and at ldxs**3 blocks, yet
309 * another index blocks.
Mingming Caod2a17632008-07-14 17:52:37 -0400310 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500311 if (ei->i_da_metadata_calc_len &&
312 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
313 if ((ei->i_da_metadata_calc_len % idxs) == 0)
314 num++;
315 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
316 num++;
317 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
318 num++;
319 ei->i_da_metadata_calc_len = 0;
320 } else
321 ei->i_da_metadata_calc_len++;
322 ei->i_da_metadata_calc_last_lblock++;
323 return num;
324 }
Mingming Caod2a17632008-07-14 17:52:37 -0400325
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500326 /*
327 * In the worst case we need a new set of index blocks at
328 * every level of the inode's extent tree.
329 */
330 ei->i_da_metadata_calc_len = 1;
331 ei->i_da_metadata_calc_last_lblock = lblock;
332 return ext_depth(inode) + 1;
Mingming Caod2a17632008-07-14 17:52:37 -0400333}
334
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400335static int
336ext4_ext_max_entries(struct inode *inode, int depth)
337{
338 int max;
339
340 if (depth == ext_depth(inode)) {
341 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400342 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400343 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400344 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400345 } else {
346 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400347 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400348 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400349 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400350 }
351
352 return max;
353}
354
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400355static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
356{
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400357 ext4_fsblk_t block = ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400358 int len = ext4_ext_get_actual_len(ext);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400359
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400360 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400361}
362
363static int ext4_valid_extent_idx(struct inode *inode,
364 struct ext4_extent_idx *ext_idx)
365{
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400366 ext4_fsblk_t block = idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400367
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400368 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400369}
370
371static int ext4_valid_extent_entries(struct inode *inode,
372 struct ext4_extent_header *eh,
373 int depth)
374{
375 struct ext4_extent *ext;
376 struct ext4_extent_idx *ext_idx;
377 unsigned short entries;
378 if (eh->eh_entries == 0)
379 return 1;
380
381 entries = le16_to_cpu(eh->eh_entries);
382
383 if (depth == 0) {
384 /* leaf entries */
385 ext = EXT_FIRST_EXTENT(eh);
386 while (entries) {
387 if (!ext4_valid_extent(inode, ext))
388 return 0;
389 ext++;
390 entries--;
391 }
392 } else {
393 ext_idx = EXT_FIRST_INDEX(eh);
394 while (entries) {
395 if (!ext4_valid_extent_idx(inode, ext_idx))
396 return 0;
397 ext_idx++;
398 entries--;
399 }
400 }
401 return 1;
402}
403
404static int __ext4_ext_check(const char *function, struct inode *inode,
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400405 struct ext4_extent_header *eh,
406 int depth)
407{
408 const char *error_msg;
409 int max = 0;
410
411 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
412 error_msg = "invalid magic";
413 goto corrupted;
414 }
415 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
416 error_msg = "unexpected eh_depth";
417 goto corrupted;
418 }
419 if (unlikely(eh->eh_max == 0)) {
420 error_msg = "invalid eh_max";
421 goto corrupted;
422 }
423 max = ext4_ext_max_entries(inode, depth);
424 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
425 error_msg = "too large eh_max";
426 goto corrupted;
427 }
428 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
429 error_msg = "invalid eh_entries";
430 goto corrupted;
431 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400432 if (!ext4_valid_extent_entries(inode, eh, depth)) {
433 error_msg = "invalid extent entries";
434 goto corrupted;
435 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400436 return 0;
437
438corrupted:
Theodore Ts'o24676da2010-05-16 21:00:00 -0400439 ext4_error_inode(function, inode,
440 "bad header/extent: %s - magic %x, "
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400441 "entries %u, max %u(%u), depth %u(%u)",
Theodore Ts'o24676da2010-05-16 21:00:00 -0400442 error_msg, le16_to_cpu(eh->eh_magic),
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400443 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
444 max, le16_to_cpu(eh->eh_depth), depth);
445
446 return -EIO;
447}
448
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400449#define ext4_ext_check(inode, eh, depth) \
450 __ext4_ext_check(__func__, inode, eh, depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400451
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400452int ext4_ext_check_inode(struct inode *inode)
453{
454 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
455}
456
Alex Tomasa86c6182006-10-11 01:21:03 -0700457#ifdef EXT_DEBUG
458static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
459{
460 int k, l = path->p_depth;
461
462 ext_debug("path:");
463 for (k = 0; k <= l; k++, path++) {
464 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700465 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700466 idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700467 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400468 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700469 le32_to_cpu(path->p_ext->ee_block),
Mingming553f9002009-09-18 13:34:55 -0400470 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400471 ext4_ext_get_actual_len(path->p_ext),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700472 ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700473 } else
474 ext_debug(" []");
475 }
476 ext_debug("\n");
477}
478
479static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
480{
481 int depth = ext_depth(inode);
482 struct ext4_extent_header *eh;
483 struct ext4_extent *ex;
484 int i;
485
486 if (!path)
487 return;
488
489 eh = path[depth].p_hdr;
490 ex = EXT_FIRST_EXTENT(eh);
491
Mingming553f9002009-09-18 13:34:55 -0400492 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
493
Alex Tomasa86c6182006-10-11 01:21:03 -0700494 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400495 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
496 ext4_ext_is_uninitialized(ex),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400497 ext4_ext_get_actual_len(ex), ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700498 }
499 ext_debug("\n");
500}
501#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400502#define ext4_ext_show_path(inode, path)
503#define ext4_ext_show_leaf(inode, path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700504#endif
505
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500506void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700507{
508 int depth = path->p_depth;
509 int i;
510
511 for (i = 0; i <= depth; i++, path++)
512 if (path->p_bh) {
513 brelse(path->p_bh);
514 path->p_bh = NULL;
515 }
516}
517
518/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700519 * ext4_ext_binsearch_idx:
520 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400521 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700522 */
523static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500524ext4_ext_binsearch_idx(struct inode *inode,
525 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700526{
527 struct ext4_extent_header *eh = path->p_hdr;
528 struct ext4_extent_idx *r, *l, *m;
529
Alex Tomasa86c6182006-10-11 01:21:03 -0700530
Eric Sandeenbba90742008-01-28 23:58:27 -0500531 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700532
533 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400534 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700535 while (l <= r) {
536 m = l + (r - l) / 2;
537 if (block < le32_to_cpu(m->ei_block))
538 r = m - 1;
539 else
540 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400541 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
542 m, le32_to_cpu(m->ei_block),
543 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700544 }
545
546 path->p_idx = l - 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700547 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400548 idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700549
550#ifdef CHECK_BINSEARCH
551 {
552 struct ext4_extent_idx *chix, *ix;
553 int k;
554
555 chix = ix = EXT_FIRST_INDEX(eh);
556 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
557 if (k != 0 &&
558 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400559 printk(KERN_DEBUG "k=%d, ix=0x%p, "
560 "first=0x%p\n", k,
561 ix, EXT_FIRST_INDEX(eh));
562 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700563 le32_to_cpu(ix->ei_block),
564 le32_to_cpu(ix[-1].ei_block));
565 }
566 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400567 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700568 if (block < le32_to_cpu(ix->ei_block))
569 break;
570 chix = ix;
571 }
572 BUG_ON(chix != path->p_idx);
573 }
574#endif
575
576}
577
578/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700579 * ext4_ext_binsearch:
580 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400581 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700582 */
583static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500584ext4_ext_binsearch(struct inode *inode,
585 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700586{
587 struct ext4_extent_header *eh = path->p_hdr;
588 struct ext4_extent *r, *l, *m;
589
Alex Tomasa86c6182006-10-11 01:21:03 -0700590 if (eh->eh_entries == 0) {
591 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700592 * this leaf is empty:
593 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700594 */
595 return;
596 }
597
Eric Sandeenbba90742008-01-28 23:58:27 -0500598 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700599
600 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400601 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700602
603 while (l <= r) {
604 m = l + (r - l) / 2;
605 if (block < le32_to_cpu(m->ee_block))
606 r = m - 1;
607 else
608 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400609 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
610 m, le32_to_cpu(m->ee_block),
611 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700612 }
613
614 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400615 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400616 le32_to_cpu(path->p_ext->ee_block),
617 ext_pblock(path->p_ext),
Mingming553f9002009-09-18 13:34:55 -0400618 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400619 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700620
621#ifdef CHECK_BINSEARCH
622 {
623 struct ext4_extent *chex, *ex;
624 int k;
625
626 chex = ex = EXT_FIRST_EXTENT(eh);
627 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
628 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400629 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700630 if (block < le32_to_cpu(ex->ee_block))
631 break;
632 chex = ex;
633 }
634 BUG_ON(chex != path->p_ext);
635 }
636#endif
637
638}
639
640int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
641{
642 struct ext4_extent_header *eh;
643
644 eh = ext_inode_hdr(inode);
645 eh->eh_depth = 0;
646 eh->eh_entries = 0;
647 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400648 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700649 ext4_mark_inode_dirty(handle, inode);
650 ext4_ext_invalidate_cache(inode);
651 return 0;
652}
653
654struct ext4_ext_path *
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500655ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
656 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700657{
658 struct ext4_extent_header *eh;
659 struct buffer_head *bh;
660 short int depth, i, ppos = 0, alloc = 0;
661
662 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400663 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700664
665 /* account possible depth increase */
666 if (!path) {
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800667 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700668 GFP_NOFS);
669 if (!path)
670 return ERR_PTR(-ENOMEM);
671 alloc = 1;
672 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700673 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400674 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700675
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400676 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700677 /* walk through the tree */
678 while (i) {
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400679 int need_to_validate = 0;
680
Alex Tomasa86c6182006-10-11 01:21:03 -0700681 ext_debug("depth %d: num %d, max %d\n",
682 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400683
Alex Tomasa86c6182006-10-11 01:21:03 -0700684 ext4_ext_binsearch_idx(inode, path + ppos, block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700685 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700686 path[ppos].p_depth = i;
687 path[ppos].p_ext = NULL;
688
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400689 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
690 if (unlikely(!bh))
Alex Tomasa86c6182006-10-11 01:21:03 -0700691 goto err;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400692 if (!bh_uptodate_or_lock(bh)) {
693 if (bh_submit_read(bh) < 0) {
694 put_bh(bh);
695 goto err;
696 }
697 /* validate the extent entries */
698 need_to_validate = 1;
699 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700700 eh = ext_block_hdr(bh);
701 ppos++;
Frank Mayhar273df552010-03-02 11:46:09 -0500702 if (unlikely(ppos > depth)) {
703 put_bh(bh);
704 EXT4_ERROR_INODE(inode,
705 "ppos %d > depth %d", ppos, depth);
706 goto err;
707 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700708 path[ppos].p_bh = bh;
709 path[ppos].p_hdr = eh;
710 i--;
711
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400712 if (need_to_validate && ext4_ext_check(inode, eh, i))
Alex Tomasa86c6182006-10-11 01:21:03 -0700713 goto err;
714 }
715
716 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700717 path[ppos].p_ext = NULL;
718 path[ppos].p_idx = NULL;
719
Alex Tomasa86c6182006-10-11 01:21:03 -0700720 /* find extent */
721 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400722 /* if not an empty leaf */
723 if (path[ppos].p_ext)
724 path[ppos].p_block = ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700725
726 ext4_ext_show_path(inode, path);
727
728 return path;
729
730err:
731 ext4_ext_drop_refs(path);
732 if (alloc)
733 kfree(path);
734 return ERR_PTR(-EIO);
735}
736
737/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700738 * ext4_ext_insert_index:
739 * insert new index [@logical;@ptr] into the block at @curp;
740 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700741 */
Mingming Cao00314622009-09-28 15:49:08 -0400742int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700743 struct ext4_ext_path *curp,
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700744 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700745{
746 struct ext4_extent_idx *ix;
747 int len, err;
748
Avantika Mathur7e028972006-12-06 20:41:33 -0800749 err = ext4_ext_get_access(handle, inode, curp);
750 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700751 return err;
752
Frank Mayhar273df552010-03-02 11:46:09 -0500753 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
754 EXT4_ERROR_INODE(inode,
755 "logical %d == ei_block %d!",
756 logical, le32_to_cpu(curp->p_idx->ei_block));
757 return -EIO;
758 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700759 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
760 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
761 /* insert after */
762 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
763 len = (len - 1) * sizeof(struct ext4_extent_idx);
764 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400765 ext_debug("insert new index %d after: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700766 "move %d from 0x%p to 0x%p\n",
767 logical, ptr, len,
768 (curp->p_idx + 1), (curp->p_idx + 2));
769 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
770 }
771 ix = curp->p_idx + 1;
772 } else {
773 /* insert before */
774 len = len * sizeof(struct ext4_extent_idx);
775 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400776 ext_debug("insert new index %d before: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700777 "move %d from 0x%p to 0x%p\n",
778 logical, ptr, len,
779 curp->p_idx, (curp->p_idx + 1));
780 memmove(curp->p_idx + 1, curp->p_idx, len);
781 ix = curp->p_idx;
782 }
783
784 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700785 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400786 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700787
Frank Mayhar273df552010-03-02 11:46:09 -0500788 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
789 > le16_to_cpu(curp->p_hdr->eh_max))) {
790 EXT4_ERROR_INODE(inode,
791 "logical %d == ei_block %d!",
792 logical, le32_to_cpu(curp->p_idx->ei_block));
793 return -EIO;
794 }
795 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
796 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
797 return -EIO;
798 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700799
800 err = ext4_ext_dirty(handle, inode, curp);
801 ext4_std_error(inode->i_sb, err);
802
803 return err;
804}
805
806/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700807 * ext4_ext_split:
808 * inserts new subtree into the path, using free index entry
809 * at depth @at:
810 * - allocates all needed blocks (new leaf and all intermediate index blocks)
811 * - makes decision where to split
812 * - moves remaining extents and index entries (right to the split point)
813 * into the newly allocated blocks
814 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700815 */
816static int ext4_ext_split(handle_t *handle, struct inode *inode,
817 struct ext4_ext_path *path,
818 struct ext4_extent *newext, int at)
819{
820 struct buffer_head *bh = NULL;
821 int depth = ext_depth(inode);
822 struct ext4_extent_header *neh;
823 struct ext4_extent_idx *fidx;
824 struct ext4_extent *ex;
825 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700826 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700827 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700828 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700829 int err = 0;
830
831 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700832 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700833
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700834 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700835 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -0500836 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
837 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
838 return -EIO;
839 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700840 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
841 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700842 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700843 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400844 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700845 } else {
846 border = newext->ee_block;
847 ext_debug("leaf will be added."
848 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400849 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700850 }
851
852 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700853 * If error occurs, then we break processing
854 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700855 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700856 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700857 */
858
859 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700860 * Get array to track all allocated blocks.
861 * We need this to handle errors and free blocks
862 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700863 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800864 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700865 if (!ablocks)
866 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700867
868 /* allocate all needed blocks */
869 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
870 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400871 newblock = ext4_ext_new_meta_block(handle, inode, path,
872 newext, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700873 if (newblock == 0)
874 goto cleanup;
875 ablocks[a] = newblock;
876 }
877
878 /* initialize new leaf */
879 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -0500880 if (unlikely(newblock == 0)) {
881 EXT4_ERROR_INODE(inode, "newblock == 0!");
882 err = -EIO;
883 goto cleanup;
884 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700885 bh = sb_getblk(inode->i_sb, newblock);
886 if (!bh) {
887 err = -EIO;
888 goto cleanup;
889 }
890 lock_buffer(bh);
891
Avantika Mathur7e028972006-12-06 20:41:33 -0800892 err = ext4_journal_get_create_access(handle, bh);
893 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700894 goto cleanup;
895
896 neh = ext_block_hdr(bh);
897 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400898 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700899 neh->eh_magic = EXT4_EXT_MAGIC;
900 neh->eh_depth = 0;
901 ex = EXT_FIRST_EXTENT(neh);
902
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700903 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -0500904 if (unlikely(path[depth].p_hdr->eh_entries !=
905 path[depth].p_hdr->eh_max)) {
906 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
907 path[depth].p_hdr->eh_entries,
908 path[depth].p_hdr->eh_max);
909 err = -EIO;
910 goto cleanup;
911 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700912 /* start copy from next extent */
913 /* TODO: we could do it by single memmove */
914 m = 0;
915 path[depth].p_ext++;
916 while (path[depth].p_ext <=
917 EXT_MAX_EXTENT(path[depth].p_hdr)) {
Mingming553f9002009-09-18 13:34:55 -0400918 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400919 le32_to_cpu(path[depth].p_ext->ee_block),
920 ext_pblock(path[depth].p_ext),
Mingming553f9002009-09-18 13:34:55 -0400921 ext4_ext_is_uninitialized(path[depth].p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400922 ext4_ext_get_actual_len(path[depth].p_ext),
Alex Tomasa86c6182006-10-11 01:21:03 -0700923 newblock);
924 /*memmove(ex++, path[depth].p_ext++,
925 sizeof(struct ext4_extent));
926 neh->eh_entries++;*/
927 path[depth].p_ext++;
928 m++;
929 }
930 if (m) {
931 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400932 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700933 }
934
935 set_buffer_uptodate(bh);
936 unlock_buffer(bh);
937
Frank Mayhar03901312009-01-07 00:06:22 -0500938 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800939 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700940 goto cleanup;
941 brelse(bh);
942 bh = NULL;
943
944 /* correct old leaf */
945 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800946 err = ext4_ext_get_access(handle, inode, path + depth);
947 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700948 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400949 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800950 err = ext4_ext_dirty(handle, inode, path + depth);
951 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700952 goto cleanup;
953
954 }
955
956 /* create intermediate indexes */
957 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -0500958 if (unlikely(k < 0)) {
959 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
960 err = -EIO;
961 goto cleanup;
962 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700963 if (k)
964 ext_debug("create %d intermediate indices\n", k);
965 /* insert new index into current index block */
966 /* current depth stored in i var */
967 i = depth - 1;
968 while (k--) {
969 oldblock = newblock;
970 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -0500971 bh = sb_getblk(inode->i_sb, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700972 if (!bh) {
973 err = -EIO;
974 goto cleanup;
975 }
976 lock_buffer(bh);
977
Avantika Mathur7e028972006-12-06 20:41:33 -0800978 err = ext4_journal_get_create_access(handle, bh);
979 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700980 goto cleanup;
981
982 neh = ext_block_hdr(bh);
983 neh->eh_entries = cpu_to_le16(1);
984 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400985 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700986 neh->eh_depth = cpu_to_le16(depth - i);
987 fidx = EXT_FIRST_INDEX(neh);
988 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700989 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700990
Eric Sandeenbba90742008-01-28 23:58:27 -0500991 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
992 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700993 /* copy indexes */
994 m = 0;
995 path[i].p_idx++;
996
997 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
998 EXT_MAX_INDEX(path[i].p_hdr));
Frank Mayhar273df552010-03-02 11:46:09 -0500999 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
1000 EXT_LAST_INDEX(path[i].p_hdr))) {
1001 EXT4_ERROR_INODE(inode,
1002 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1003 le32_to_cpu(path[i].p_ext->ee_block));
1004 err = -EIO;
1005 goto cleanup;
1006 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001007 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
Dmitry Monakhov26d535e2007-07-18 08:33:37 -04001008 ext_debug("%d: move %d:%llu in new index %llu\n", i,
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001009 le32_to_cpu(path[i].p_idx->ei_block),
1010 idx_pblock(path[i].p_idx),
1011 newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001012 /*memmove(++fidx, path[i].p_idx++,
1013 sizeof(struct ext4_extent_idx));
1014 neh->eh_entries++;
1015 BUG_ON(neh->eh_entries > neh->eh_max);*/
1016 path[i].p_idx++;
1017 m++;
1018 }
1019 if (m) {
1020 memmove(++fidx, path[i].p_idx - m,
1021 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001022 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001023 }
1024 set_buffer_uptodate(bh);
1025 unlock_buffer(bh);
1026
Frank Mayhar03901312009-01-07 00:06:22 -05001027 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001028 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001029 goto cleanup;
1030 brelse(bh);
1031 bh = NULL;
1032
1033 /* correct old index */
1034 if (m) {
1035 err = ext4_ext_get_access(handle, inode, path + i);
1036 if (err)
1037 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001038 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001039 err = ext4_ext_dirty(handle, inode, path + i);
1040 if (err)
1041 goto cleanup;
1042 }
1043
1044 i--;
1045 }
1046
1047 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001048 err = ext4_ext_insert_index(handle, inode, path + at,
1049 le32_to_cpu(border), newblock);
1050
1051cleanup:
1052 if (bh) {
1053 if (buffer_locked(bh))
1054 unlock_buffer(bh);
1055 brelse(bh);
1056 }
1057
1058 if (err) {
1059 /* free all allocated blocks in error case */
1060 for (i = 0; i < depth; i++) {
1061 if (!ablocks[i])
1062 continue;
Theodore Ts'oe6362602009-11-23 07:17:05 -05001063 ext4_free_blocks(handle, inode, 0, ablocks[i], 1,
1064 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001065 }
1066 }
1067 kfree(ablocks);
1068
1069 return err;
1070}
1071
1072/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001073 * ext4_ext_grow_indepth:
1074 * implements tree growing procedure:
1075 * - allocates new block
1076 * - moves top-level data (index block or leaf) into the new block
1077 * - initializes new top-level, creating index that points to the
1078 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001079 */
1080static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
1081 struct ext4_ext_path *path,
1082 struct ext4_extent *newext)
1083{
1084 struct ext4_ext_path *curp = path;
1085 struct ext4_extent_header *neh;
1086 struct ext4_extent_idx *fidx;
1087 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001088 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001089 int err = 0;
1090
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -04001091 newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07001092 if (newblock == 0)
1093 return err;
1094
1095 bh = sb_getblk(inode->i_sb, newblock);
1096 if (!bh) {
1097 err = -EIO;
1098 ext4_std_error(inode->i_sb, err);
1099 return err;
1100 }
1101 lock_buffer(bh);
1102
Avantika Mathur7e028972006-12-06 20:41:33 -08001103 err = ext4_journal_get_create_access(handle, bh);
1104 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001105 unlock_buffer(bh);
1106 goto out;
1107 }
1108
1109 /* move top-level index/leaf into new block */
1110 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
1111
1112 /* set size of new block */
1113 neh = ext_block_hdr(bh);
1114 /* old root could have indexes or leaves
1115 * so calculate e_max right way */
1116 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001117 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001118 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001119 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001120 neh->eh_magic = EXT4_EXT_MAGIC;
1121 set_buffer_uptodate(bh);
1122 unlock_buffer(bh);
1123
Frank Mayhar03901312009-01-07 00:06:22 -05001124 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001125 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001126 goto out;
1127
1128 /* create index in new top-level index: num,max,pointer */
Avantika Mathur7e028972006-12-06 20:41:33 -08001129 err = ext4_ext_get_access(handle, inode, curp);
1130 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001131 goto out;
1132
1133 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001134 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001135 curp->p_hdr->eh_entries = cpu_to_le16(1);
1136 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
Dmitry Monakhove9f410b2007-07-18 09:09:15 -04001137
1138 if (path[0].p_hdr->eh_depth)
1139 curp->p_idx->ei_block =
1140 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
1141 else
1142 curp->p_idx->ei_block =
1143 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001144 ext4_idx_store_pblock(curp->p_idx, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001145
1146 neh = ext_inode_hdr(inode);
1147 fidx = EXT_FIRST_INDEX(neh);
Mingming Cao2ae02102006-10-11 01:21:11 -07001148 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001149 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001150 le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
Alex Tomasa86c6182006-10-11 01:21:03 -07001151
1152 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
1153 err = ext4_ext_dirty(handle, inode, curp);
1154out:
1155 brelse(bh);
1156
1157 return err;
1158}
1159
1160/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001161 * ext4_ext_create_new_leaf:
1162 * finds empty index and adds new leaf.
1163 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001164 */
1165static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
1166 struct ext4_ext_path *path,
1167 struct ext4_extent *newext)
1168{
1169 struct ext4_ext_path *curp;
1170 int depth, i, err = 0;
1171
1172repeat:
1173 i = depth = ext_depth(inode);
1174
1175 /* walk up to the tree and look for free index entry */
1176 curp = path + depth;
1177 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1178 i--;
1179 curp--;
1180 }
1181
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001182 /* we use already allocated block for index block,
1183 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001184 if (EXT_HAS_FREE_INDEX(curp)) {
1185 /* if we found index with free entry, then use that
1186 * entry: create all needed subtree and add new leaf */
1187 err = ext4_ext_split(handle, inode, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001188 if (err)
1189 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001190
1191 /* refill path */
1192 ext4_ext_drop_refs(path);
1193 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001194 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1195 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001196 if (IS_ERR(path))
1197 err = PTR_ERR(path);
1198 } else {
1199 /* tree is full, time to grow in depth */
1200 err = ext4_ext_grow_indepth(handle, inode, path, newext);
1201 if (err)
1202 goto out;
1203
1204 /* refill path */
1205 ext4_ext_drop_refs(path);
1206 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001207 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1208 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001209 if (IS_ERR(path)) {
1210 err = PTR_ERR(path);
1211 goto out;
1212 }
1213
1214 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001215 * only first (depth 0 -> 1) produces free space;
1216 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001217 */
1218 depth = ext_depth(inode);
1219 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001220 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001221 goto repeat;
1222 }
1223 }
1224
1225out:
1226 return err;
1227}
1228
1229/*
Alex Tomas1988b512008-01-28 23:58:27 -05001230 * search the closest allocated block to the left for *logical
1231 * and returns it at @logical + it's physical address at @phys
1232 * if *logical is the smallest allocated block, the function
1233 * returns 0 at @phys
1234 * return value contains 0 (success) or error code
1235 */
1236int
1237ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
1238 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1239{
1240 struct ext4_extent_idx *ix;
1241 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001242 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001243
Frank Mayhar273df552010-03-02 11:46:09 -05001244 if (unlikely(path == NULL)) {
1245 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1246 return -EIO;
1247 }
Alex Tomas1988b512008-01-28 23:58:27 -05001248 depth = path->p_depth;
1249 *phys = 0;
1250
1251 if (depth == 0 && path->p_ext == NULL)
1252 return 0;
1253
1254 /* usually extent in the path covers blocks smaller
1255 * then *logical, but it can be that extent is the
1256 * first one in the file */
1257
1258 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001259 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001260 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001261 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1262 EXT4_ERROR_INODE(inode,
1263 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1264 *logical, le32_to_cpu(ex->ee_block));
1265 return -EIO;
1266 }
Alex Tomas1988b512008-01-28 23:58:27 -05001267 while (--depth >= 0) {
1268 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001269 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1270 EXT4_ERROR_INODE(inode,
1271 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
1272 ix != NULL ? ix->ei_block : 0,
1273 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
1274 EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block : 0,
1275 depth);
1276 return -EIO;
1277 }
Alex Tomas1988b512008-01-28 23:58:27 -05001278 }
1279 return 0;
1280 }
1281
Frank Mayhar273df552010-03-02 11:46:09 -05001282 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1283 EXT4_ERROR_INODE(inode,
1284 "logical %d < ee_block %d + ee_len %d!",
1285 *logical, le32_to_cpu(ex->ee_block), ee_len);
1286 return -EIO;
1287 }
Alex Tomas1988b512008-01-28 23:58:27 -05001288
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001289 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1290 *phys = ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001291 return 0;
1292}
1293
1294/*
1295 * search the closest allocated block to the right for *logical
1296 * and returns it at @logical + it's physical address at @phys
1297 * if *logical is the smallest allocated block, the function
1298 * returns 0 at @phys
1299 * return value contains 0 (success) or error code
1300 */
1301int
1302ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
1303 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1304{
1305 struct buffer_head *bh = NULL;
1306 struct ext4_extent_header *eh;
1307 struct ext4_extent_idx *ix;
1308 struct ext4_extent *ex;
1309 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001310 int depth; /* Note, NOT eh_depth; depth from top of tree */
1311 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001312
Frank Mayhar273df552010-03-02 11:46:09 -05001313 if (unlikely(path == NULL)) {
1314 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1315 return -EIO;
1316 }
Alex Tomas1988b512008-01-28 23:58:27 -05001317 depth = path->p_depth;
1318 *phys = 0;
1319
1320 if (depth == 0 && path->p_ext == NULL)
1321 return 0;
1322
1323 /* usually extent in the path covers blocks smaller
1324 * then *logical, but it can be that extent is the
1325 * first one in the file */
1326
1327 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001328 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001329 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001330 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1331 EXT4_ERROR_INODE(inode,
1332 "first_extent(path[%d].p_hdr) != ex",
1333 depth);
1334 return -EIO;
1335 }
Alex Tomas1988b512008-01-28 23:58:27 -05001336 while (--depth >= 0) {
1337 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001338 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1339 EXT4_ERROR_INODE(inode,
1340 "ix != EXT_FIRST_INDEX *logical %d!",
1341 *logical);
1342 return -EIO;
1343 }
Alex Tomas1988b512008-01-28 23:58:27 -05001344 }
1345 *logical = le32_to_cpu(ex->ee_block);
1346 *phys = ext_pblock(ex);
1347 return 0;
1348 }
1349
Frank Mayhar273df552010-03-02 11:46:09 -05001350 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1351 EXT4_ERROR_INODE(inode,
1352 "logical %d < ee_block %d + ee_len %d!",
1353 *logical, le32_to_cpu(ex->ee_block), ee_len);
1354 return -EIO;
1355 }
Alex Tomas1988b512008-01-28 23:58:27 -05001356
1357 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1358 /* next allocated block in this leaf */
1359 ex++;
1360 *logical = le32_to_cpu(ex->ee_block);
1361 *phys = ext_pblock(ex);
1362 return 0;
1363 }
1364
1365 /* go up and search for index to the right */
1366 while (--depth >= 0) {
1367 ix = path[depth].p_idx;
1368 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001369 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001370 }
1371
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001372 /* we've gone up to the root and found no index to the right */
1373 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001374
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001375got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001376 /* we've found index to the right, let's
1377 * follow it and find the closest allocated
1378 * block to the right */
1379 ix++;
1380 block = idx_pblock(ix);
1381 while (++depth < path->p_depth) {
1382 bh = sb_bread(inode->i_sb, block);
1383 if (bh == NULL)
1384 return -EIO;
1385 eh = ext_block_hdr(bh);
Eric Sandeen395a87b2009-03-10 18:18:47 -04001386 /* subtract from p_depth to get proper eh_depth */
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001387 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001388 put_bh(bh);
1389 return -EIO;
1390 }
1391 ix = EXT_FIRST_INDEX(eh);
1392 block = idx_pblock(ix);
1393 put_bh(bh);
1394 }
1395
1396 bh = sb_bread(inode->i_sb, block);
1397 if (bh == NULL)
1398 return -EIO;
1399 eh = ext_block_hdr(bh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001400 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001401 put_bh(bh);
1402 return -EIO;
1403 }
1404 ex = EXT_FIRST_EXTENT(eh);
1405 *logical = le32_to_cpu(ex->ee_block);
1406 *phys = ext_pblock(ex);
1407 put_bh(bh);
1408 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001409}
1410
1411/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001412 * ext4_ext_next_allocated_block:
1413 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1414 * NOTE: it considers block number from index entry as
1415 * allocated block. Thus, index entries have to be consistent
1416 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001417 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001418static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001419ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1420{
1421 int depth;
1422
1423 BUG_ON(path == NULL);
1424 depth = path->p_depth;
1425
1426 if (depth == 0 && path->p_ext == NULL)
1427 return EXT_MAX_BLOCK;
1428
1429 while (depth >= 0) {
1430 if (depth == path->p_depth) {
1431 /* leaf */
1432 if (path[depth].p_ext !=
1433 EXT_LAST_EXTENT(path[depth].p_hdr))
1434 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1435 } else {
1436 /* index */
1437 if (path[depth].p_idx !=
1438 EXT_LAST_INDEX(path[depth].p_hdr))
1439 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1440 }
1441 depth--;
1442 }
1443
1444 return EXT_MAX_BLOCK;
1445}
1446
1447/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001448 * ext4_ext_next_leaf_block:
Alex Tomasa86c6182006-10-11 01:21:03 -07001449 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1450 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001451static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
Andrew Morton63f57932006-10-11 01:21:24 -07001452 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001453{
1454 int depth;
1455
1456 BUG_ON(path == NULL);
1457 depth = path->p_depth;
1458
1459 /* zero-tree has no leaf blocks at all */
1460 if (depth == 0)
1461 return EXT_MAX_BLOCK;
1462
1463 /* go to index block */
1464 depth--;
1465
1466 while (depth >= 0) {
1467 if (path[depth].p_idx !=
1468 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001469 return (ext4_lblk_t)
1470 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001471 depth--;
1472 }
1473
1474 return EXT_MAX_BLOCK;
1475}
1476
1477/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001478 * ext4_ext_correct_indexes:
1479 * if leaf gets modified and modified extent is first in the leaf,
1480 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001481 * TODO: do we need to correct tree in all cases?
1482 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001483static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001484 struct ext4_ext_path *path)
1485{
1486 struct ext4_extent_header *eh;
1487 int depth = ext_depth(inode);
1488 struct ext4_extent *ex;
1489 __le32 border;
1490 int k, err = 0;
1491
1492 eh = path[depth].p_hdr;
1493 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001494
1495 if (unlikely(ex == NULL || eh == NULL)) {
1496 EXT4_ERROR_INODE(inode,
1497 "ex %p == NULL or eh %p == NULL", ex, eh);
1498 return -EIO;
1499 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001500
1501 if (depth == 0) {
1502 /* there is no tree at all */
1503 return 0;
1504 }
1505
1506 if (ex != EXT_FIRST_EXTENT(eh)) {
1507 /* we correct tree if first leaf got modified only */
1508 return 0;
1509 }
1510
1511 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001512 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001513 */
1514 k = depth - 1;
1515 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001516 err = ext4_ext_get_access(handle, inode, path + k);
1517 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001518 return err;
1519 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001520 err = ext4_ext_dirty(handle, inode, path + k);
1521 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001522 return err;
1523
1524 while (k--) {
1525 /* change all left-side indexes */
1526 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1527 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001528 err = ext4_ext_get_access(handle, inode, path + k);
1529 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001530 break;
1531 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001532 err = ext4_ext_dirty(handle, inode, path + k);
1533 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001534 break;
1535 }
1536
1537 return err;
1538}
1539
Akira Fujita748de672009-06-17 19:24:03 -04001540int
Alex Tomasa86c6182006-10-11 01:21:03 -07001541ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1542 struct ext4_extent *ex2)
1543{
Amit Arora749269f2007-07-18 09:02:56 -04001544 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001545
1546 /*
1547 * Make sure that either both extents are uninitialized, or
1548 * both are _not_.
1549 */
1550 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1551 return 0;
1552
Amit Arora749269f2007-07-18 09:02:56 -04001553 if (ext4_ext_is_uninitialized(ex1))
1554 max_len = EXT_UNINIT_MAX_LEN;
1555 else
1556 max_len = EXT_INIT_MAX_LEN;
1557
Amit Aroraa2df2a62007-07-17 21:42:41 -04001558 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1559 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1560
1561 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001562 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001563 return 0;
1564
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001565 /*
1566 * To allow future support for preallocated extents to be added
1567 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001568 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001569 */
Amit Arora749269f2007-07-18 09:02:56 -04001570 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001571 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001572#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001573 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001574 return 0;
1575#endif
1576
Amit Aroraa2df2a62007-07-17 21:42:41 -04001577 if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001578 return 1;
1579 return 0;
1580}
1581
1582/*
Amit Arora56055d32007-07-17 21:42:38 -04001583 * This function tries to merge the "ex" extent to the next extent in the tree.
1584 * It always tries to merge towards right. If you want to merge towards
1585 * left, pass "ex - 1" as argument instead of "ex".
1586 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1587 * 1 if they got merged.
1588 */
1589int ext4_ext_try_to_merge(struct inode *inode,
1590 struct ext4_ext_path *path,
1591 struct ext4_extent *ex)
1592{
1593 struct ext4_extent_header *eh;
1594 unsigned int depth, len;
1595 int merge_done = 0;
1596 int uninitialized = 0;
1597
1598 depth = ext_depth(inode);
1599 BUG_ON(path[depth].p_hdr == NULL);
1600 eh = path[depth].p_hdr;
1601
1602 while (ex < EXT_LAST_EXTENT(eh)) {
1603 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1604 break;
1605 /* merge with next extent! */
1606 if (ext4_ext_is_uninitialized(ex))
1607 uninitialized = 1;
1608 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1609 + ext4_ext_get_actual_len(ex + 1));
1610 if (uninitialized)
1611 ext4_ext_mark_uninitialized(ex);
1612
1613 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1614 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1615 * sizeof(struct ext4_extent);
1616 memmove(ex + 1, ex + 2, len);
1617 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001618 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001619 merge_done = 1;
1620 WARN_ON(eh->eh_entries == 0);
1621 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001622 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001623 }
1624
1625 return merge_done;
1626}
1627
1628/*
Amit Arora25d14f92007-05-24 13:04:13 -04001629 * check if a portion of the "newext" extent overlaps with an
1630 * existing extent.
1631 *
1632 * If there is an overlap discovered, it updates the length of the newext
1633 * such that there will be no overlap, and then returns 1.
1634 * If there is no overlap found, it returns 0.
1635 */
1636unsigned int ext4_ext_check_overlap(struct inode *inode,
1637 struct ext4_extent *newext,
1638 struct ext4_ext_path *path)
1639{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001640 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001641 unsigned int depth, len1;
1642 unsigned int ret = 0;
1643
1644 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001645 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001646 depth = ext_depth(inode);
1647 if (!path[depth].p_ext)
1648 goto out;
1649 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1650
1651 /*
1652 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001653 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001654 */
1655 if (b2 < b1) {
1656 b2 = ext4_ext_next_allocated_block(path);
1657 if (b2 == EXT_MAX_BLOCK)
1658 goto out;
1659 }
1660
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001661 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001662 if (b1 + len1 < b1) {
1663 len1 = EXT_MAX_BLOCK - b1;
1664 newext->ee_len = cpu_to_le16(len1);
1665 ret = 1;
1666 }
1667
1668 /* check for overlap */
1669 if (b1 + len1 > b2) {
1670 newext->ee_len = cpu_to_le16(b2 - b1);
1671 ret = 1;
1672 }
1673out:
1674 return ret;
1675}
1676
1677/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001678 * ext4_ext_insert_extent:
1679 * tries to merge requsted extent into the existing extent or
1680 * inserts requested extent as new one into the tree,
1681 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001682 */
1683int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1684 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04001685 struct ext4_extent *newext, int flag)
Alex Tomasa86c6182006-10-11 01:21:03 -07001686{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001687 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001688 struct ext4_extent *ex, *fex;
1689 struct ext4_extent *nearex; /* nearest extent */
1690 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001691 int depth, len, err;
1692 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001693 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001694
Frank Mayhar273df552010-03-02 11:46:09 -05001695 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1696 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1697 return -EIO;
1698 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001699 depth = ext_depth(inode);
1700 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001701 if (unlikely(path[depth].p_hdr == NULL)) {
1702 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1703 return -EIO;
1704 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001705
1706 /* try to insert block into found extent and return */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001707 if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
Mingming Cao00314622009-09-28 15:49:08 -04001708 && ext4_can_extents_be_merged(inode, ex, newext)) {
Mingming553f9002009-09-18 13:34:55 -04001709 ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
1710 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001711 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001712 le32_to_cpu(ex->ee_block),
Mingming553f9002009-09-18 13:34:55 -04001713 ext4_ext_is_uninitialized(ex),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001714 ext4_ext_get_actual_len(ex), ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001715 err = ext4_ext_get_access(handle, inode, path + depth);
1716 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001717 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001718
1719 /*
1720 * ext4_can_extents_be_merged should have checked that either
1721 * both extents are uninitialized, or both aren't. Thus we
1722 * need to check only one of them here.
1723 */
1724 if (ext4_ext_is_uninitialized(ex))
1725 uninitialized = 1;
1726 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1727 + ext4_ext_get_actual_len(newext));
1728 if (uninitialized)
1729 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001730 eh = path[depth].p_hdr;
1731 nearex = ex;
1732 goto merge;
1733 }
1734
1735repeat:
1736 depth = ext_depth(inode);
1737 eh = path[depth].p_hdr;
1738 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1739 goto has_space;
1740
1741 /* probably next leaf has space for us? */
1742 fex = EXT_LAST_EXTENT(eh);
1743 next = ext4_ext_next_leaf_block(inode, path);
1744 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1745 && next != EXT_MAX_BLOCK) {
1746 ext_debug("next leaf block - %d\n", next);
1747 BUG_ON(npath != NULL);
1748 npath = ext4_ext_find_extent(inode, next, NULL);
1749 if (IS_ERR(npath))
1750 return PTR_ERR(npath);
1751 BUG_ON(npath->p_depth != path->p_depth);
1752 eh = npath[depth].p_hdr;
1753 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1754 ext_debug("next leaf isnt full(%d)\n",
1755 le16_to_cpu(eh->eh_entries));
1756 path = npath;
1757 goto repeat;
1758 }
1759 ext_debug("next leaf has no free space(%d,%d)\n",
1760 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1761 }
1762
1763 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001764 * There is no free space in the found leaf.
1765 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001766 */
1767 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1768 if (err)
1769 goto cleanup;
1770 depth = ext_depth(inode);
1771 eh = path[depth].p_hdr;
1772
1773has_space:
1774 nearex = path[depth].p_ext;
1775
Avantika Mathur7e028972006-12-06 20:41:33 -08001776 err = ext4_ext_get_access(handle, inode, path + depth);
1777 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001778 goto cleanup;
1779
1780 if (!nearex) {
1781 /* there is no extent in this leaf, create first one */
Mingming553f9002009-09-18 13:34:55 -04001782 ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001783 le32_to_cpu(newext->ee_block),
1784 ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001785 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001786 ext4_ext_get_actual_len(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001787 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1788 } else if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001789 > le32_to_cpu(nearex->ee_block)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001790/* BUG_ON(newext->ee_block == nearex->ee_block); */
1791 if (nearex != EXT_LAST_EXTENT(eh)) {
1792 len = EXT_MAX_EXTENT(eh) - nearex;
1793 len = (len - 1) * sizeof(struct ext4_extent);
1794 len = len < 0 ? 0 : len;
Mingming553f9002009-09-18 13:34:55 -04001795 ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001796 "move %d from 0x%p to 0x%p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001797 le32_to_cpu(newext->ee_block),
1798 ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001799 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001800 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001801 nearex, len, nearex + 1, nearex + 2);
1802 memmove(nearex + 2, nearex + 1, len);
1803 }
1804 path[depth].p_ext = nearex + 1;
1805 } else {
1806 BUG_ON(newext->ee_block == nearex->ee_block);
1807 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1808 len = len < 0 ? 0 : len;
Mingming553f9002009-09-18 13:34:55 -04001809 ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001810 "move %d from 0x%p to 0x%p\n",
1811 le32_to_cpu(newext->ee_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001812 ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001813 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001814 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001815 nearex, len, nearex + 1, nearex + 2);
1816 memmove(nearex + 1, nearex, len);
1817 path[depth].p_ext = nearex;
1818 }
1819
Marcin Slusarze8546d02008-04-17 10:38:59 -04001820 le16_add_cpu(&eh->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001821 nearex = path[depth].p_ext;
1822 nearex->ee_block = newext->ee_block;
Aneesh Kumar K.Vb3776112007-10-16 18:38:25 -04001823 ext4_ext_store_pblock(nearex, ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001824 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001825
1826merge:
1827 /* try to merge extents to the right */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001828 if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
Mingming Cao00314622009-09-28 15:49:08 -04001829 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001830
1831 /* try to merge extents to the left */
1832
1833 /* time to correct all indexes above */
1834 err = ext4_ext_correct_indexes(handle, inode, path);
1835 if (err)
1836 goto cleanup;
1837
1838 err = ext4_ext_dirty(handle, inode, path + depth);
1839
1840cleanup:
1841 if (npath) {
1842 ext4_ext_drop_refs(npath);
1843 kfree(npath);
1844 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001845 ext4_ext_invalidate_cache(inode);
1846 return err;
1847}
1848
Eric Sandeen6873fa02008-10-07 00:46:36 -04001849int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1850 ext4_lblk_t num, ext_prepare_callback func,
1851 void *cbdata)
1852{
1853 struct ext4_ext_path *path = NULL;
1854 struct ext4_ext_cache cbex;
1855 struct ext4_extent *ex;
1856 ext4_lblk_t next, start = 0, end = 0;
1857 ext4_lblk_t last = block + num;
1858 int depth, exists, err = 0;
1859
1860 BUG_ON(func == NULL);
1861 BUG_ON(inode == NULL);
1862
1863 while (block < last && block != EXT_MAX_BLOCK) {
1864 num = last - block;
1865 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001866 down_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001867 path = ext4_ext_find_extent(inode, block, path);
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001868 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001869 if (IS_ERR(path)) {
1870 err = PTR_ERR(path);
1871 path = NULL;
1872 break;
1873 }
1874
1875 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05001876 if (unlikely(path[depth].p_hdr == NULL)) {
1877 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1878 err = -EIO;
1879 break;
1880 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04001881 ex = path[depth].p_ext;
1882 next = ext4_ext_next_allocated_block(path);
1883
1884 exists = 0;
1885 if (!ex) {
1886 /* there is no extent yet, so try to allocate
1887 * all requested space */
1888 start = block;
1889 end = block + num;
1890 } else if (le32_to_cpu(ex->ee_block) > block) {
1891 /* need to allocate space before found extent */
1892 start = block;
1893 end = le32_to_cpu(ex->ee_block);
1894 if (block + num < end)
1895 end = block + num;
1896 } else if (block >= le32_to_cpu(ex->ee_block)
1897 + ext4_ext_get_actual_len(ex)) {
1898 /* need to allocate space after found extent */
1899 start = block;
1900 end = block + num;
1901 if (end >= next)
1902 end = next;
1903 } else if (block >= le32_to_cpu(ex->ee_block)) {
1904 /*
1905 * some part of requested space is covered
1906 * by found extent
1907 */
1908 start = block;
1909 end = le32_to_cpu(ex->ee_block)
1910 + ext4_ext_get_actual_len(ex);
1911 if (block + num < end)
1912 end = block + num;
1913 exists = 1;
1914 } else {
1915 BUG();
1916 }
1917 BUG_ON(end <= start);
1918
1919 if (!exists) {
1920 cbex.ec_block = start;
1921 cbex.ec_len = end - start;
1922 cbex.ec_start = 0;
1923 cbex.ec_type = EXT4_EXT_CACHE_GAP;
1924 } else {
1925 cbex.ec_block = le32_to_cpu(ex->ee_block);
1926 cbex.ec_len = ext4_ext_get_actual_len(ex);
1927 cbex.ec_start = ext_pblock(ex);
1928 cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
1929 }
1930
Frank Mayhar273df552010-03-02 11:46:09 -05001931 if (unlikely(cbex.ec_len == 0)) {
1932 EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
1933 err = -EIO;
1934 break;
1935 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04001936 err = func(inode, path, &cbex, ex, cbdata);
1937 ext4_ext_drop_refs(path);
1938
1939 if (err < 0)
1940 break;
1941
1942 if (err == EXT_REPEAT)
1943 continue;
1944 else if (err == EXT_BREAK) {
1945 err = 0;
1946 break;
1947 }
1948
1949 if (ext_depth(inode) != depth) {
1950 /* depth was changed. we have to realloc path */
1951 kfree(path);
1952 path = NULL;
1953 }
1954
1955 block = cbex.ec_block + cbex.ec_len;
1956 }
1957
1958 if (path) {
1959 ext4_ext_drop_refs(path);
1960 kfree(path);
1961 }
1962
1963 return err;
1964}
1965
Avantika Mathur09b88252006-12-06 20:41:36 -08001966static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001967ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Mingming Caodd545672007-07-31 00:37:46 -07001968 __u32 len, ext4_fsblk_t start, int type)
Alex Tomasa86c6182006-10-11 01:21:03 -07001969{
1970 struct ext4_ext_cache *cex;
1971 BUG_ON(len == 0);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001972 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001973 cex = &EXT4_I(inode)->i_cached_extent;
1974 cex->ec_type = type;
1975 cex->ec_block = block;
1976 cex->ec_len = len;
1977 cex->ec_start = start;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001978 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001979}
1980
1981/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001982 * ext4_ext_put_gap_in_cache:
1983 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07001984 * and cache this gap
1985 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001986static void
Alex Tomasa86c6182006-10-11 01:21:03 -07001987ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001988 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07001989{
1990 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001991 unsigned long len;
1992 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001993 struct ext4_extent *ex;
1994
1995 ex = path[depth].p_ext;
1996 if (ex == NULL) {
1997 /* there is no extent yet, so gap is [0;-] */
1998 lblock = 0;
1999 len = EXT_MAX_BLOCK;
2000 ext_debug("cache gap(whole file):");
2001 } else if (block < le32_to_cpu(ex->ee_block)) {
2002 lblock = block;
2003 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05002004 ext_debug("cache gap(before): %u [%u:%u]",
2005 block,
2006 le32_to_cpu(ex->ee_block),
2007 ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002008 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002009 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002010 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002011 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002012 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002013
2014 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05002015 ext_debug("cache gap(after): [%u:%u] %u",
2016 le32_to_cpu(ex->ee_block),
2017 ext4_ext_get_actual_len(ex),
2018 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002019 BUG_ON(next == lblock);
2020 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07002021 } else {
2022 lblock = len = 0;
2023 BUG();
2024 }
2025
Eric Sandeenbba90742008-01-28 23:58:27 -05002026 ext_debug(" -> %u:%lu\n", lblock, len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002027 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
2028}
2029
Avantika Mathur09b88252006-12-06 20:41:36 -08002030static int
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002031ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
Alex Tomasa86c6182006-10-11 01:21:03 -07002032 struct ext4_extent *ex)
2033{
2034 struct ext4_ext_cache *cex;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002035 int ret = EXT4_EXT_CACHE_NO;
Alex Tomasa86c6182006-10-11 01:21:03 -07002036
Theodore Ts'o60e66792010-05-17 07:00:00 -04002037 /*
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002038 * We borrow i_block_reservation_lock to protect i_cached_extent
2039 */
2040 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07002041 cex = &EXT4_I(inode)->i_cached_extent;
2042
2043 /* has cache valid data? */
2044 if (cex->ec_type == EXT4_EXT_CACHE_NO)
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002045 goto errout;
Alex Tomasa86c6182006-10-11 01:21:03 -07002046
2047 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
2048 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
Akinobu Mita731eb1a2010-03-03 23:55:01 -05002049 if (in_range(block, cex->ec_block, cex->ec_len)) {
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002050 ex->ee_block = cpu_to_le32(cex->ec_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002051 ext4_ext_store_pblock(ex, cex->ec_start);
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002052 ex->ee_len = cpu_to_le16(cex->ec_len);
Eric Sandeenbba90742008-01-28 23:58:27 -05002053 ext_debug("%u cached by %u:%u:%llu\n",
2054 block,
2055 cex->ec_block, cex->ec_len, cex->ec_start);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002056 ret = cex->ec_type;
Alex Tomasa86c6182006-10-11 01:21:03 -07002057 }
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002058errout:
2059 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
2060 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07002061}
2062
2063/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002064 * ext4_ext_rm_idx:
2065 * removes index from the index block.
2066 * It's used in truncate case only, thus all requests are for
2067 * last index in the block only.
Alex Tomasa86c6182006-10-11 01:21:03 -07002068 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002069static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07002070 struct ext4_ext_path *path)
2071{
Alex Tomasa86c6182006-10-11 01:21:03 -07002072 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002073 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002074
2075 /* free index block */
2076 path--;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002077 leaf = idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002078 if (unlikely(path->p_hdr->eh_entries == 0)) {
2079 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2080 return -EIO;
2081 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002082 err = ext4_ext_get_access(handle, inode, path);
2083 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002084 return err;
Marcin Slusarze8546d02008-04-17 10:38:59 -04002085 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002086 err = ext4_ext_dirty(handle, inode, path);
2087 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002088 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002089 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Theodore Ts'oe6362602009-11-23 07:17:05 -05002090 ext4_free_blocks(handle, inode, 0, leaf, 1,
2091 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Alex Tomasa86c6182006-10-11 01:21:03 -07002092 return err;
2093}
2094
2095/*
Mingming Caoee12b632008-08-19 22:16:05 -04002096 * ext4_ext_calc_credits_for_single_extent:
2097 * This routine returns max. credits that needed to insert an extent
2098 * to the extent tree.
2099 * When pass the actual path, the caller should calculate credits
2100 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002101 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002102int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002103 struct ext4_ext_path *path)
2104{
Alex Tomasa86c6182006-10-11 01:21:03 -07002105 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002106 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002107 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002108
Alex Tomasa86c6182006-10-11 01:21:03 -07002109 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002110 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002111 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2112
2113 /*
2114 * There are some space in the leaf tree, no
2115 * need to account for leaf block credit
2116 *
2117 * bitmaps and block group descriptor blocks
2118 * and other metadat blocks still need to be
2119 * accounted.
2120 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002121 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002122 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002123 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002124 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002125 }
2126
Mingming Cao525f4ed2008-08-19 22:15:58 -04002127 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002128}
Alex Tomasa86c6182006-10-11 01:21:03 -07002129
Mingming Caoee12b632008-08-19 22:16:05 -04002130/*
2131 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2132 *
2133 * if nrblocks are fit in a single extent (chunk flag is 1), then
2134 * in the worse case, each tree level index/leaf need to be changed
2135 * if the tree split due to insert a new extent, then the old tree
2136 * index/leaf need to be updated too
2137 *
2138 * If the nrblocks are discontiguous, they could cause
2139 * the whole tree split more than once, but this is really rare.
2140 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002141int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoee12b632008-08-19 22:16:05 -04002142{
2143 int index;
2144 int depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002145
Mingming Caoee12b632008-08-19 22:16:05 -04002146 if (chunk)
2147 index = depth * 2;
2148 else
2149 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002150
Mingming Caoee12b632008-08-19 22:16:05 -04002151 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002152}
2153
2154static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2155 struct ext4_extent *ex,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002156 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002157{
Amit Aroraa2df2a62007-07-17 21:42:41 -04002158 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe6362602009-11-23 07:17:05 -05002159 int flags = EXT4_FREE_BLOCKS_FORGET;
Alex Tomasa86c6182006-10-11 01:21:03 -07002160
Alex Tomasc9de5602008-01-29 00:19:52 -05002161 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
Theodore Ts'oe6362602009-11-23 07:17:05 -05002162 flags |= EXT4_FREE_BLOCKS_METADATA;
Alex Tomasa86c6182006-10-11 01:21:03 -07002163#ifdef EXTENTS_STATS
2164 {
2165 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002166 spin_lock(&sbi->s_ext_stats_lock);
2167 sbi->s_ext_blocks += ee_len;
2168 sbi->s_ext_extents++;
2169 if (ee_len < sbi->s_ext_min)
2170 sbi->s_ext_min = ee_len;
2171 if (ee_len > sbi->s_ext_max)
2172 sbi->s_ext_max = ee_len;
2173 if (ext_depth(inode) > sbi->s_depth_max)
2174 sbi->s_depth_max = ext_depth(inode);
2175 spin_unlock(&sbi->s_ext_stats_lock);
2176 }
2177#endif
2178 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002179 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002180 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002181 ext4_lblk_t num;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002182 ext4_fsblk_t start;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002183
Amit Aroraa2df2a62007-07-17 21:42:41 -04002184 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2185 start = ext_pblock(ex) + ee_len - num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002186 ext_debug("free last %u blocks starting %llu\n", num, start);
Theodore Ts'oe6362602009-11-23 07:17:05 -05002187 ext4_free_blocks(handle, inode, 0, start, num, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07002188 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002189 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002190 printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
Amit Aroraa2df2a62007-07-17 21:42:41 -04002191 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002192 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002193 printk(KERN_INFO "strange request: removal(2) "
2194 "%u-%u from %u:%u\n",
2195 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002196 }
2197 return 0;
2198}
2199
2200static int
2201ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002202 struct ext4_ext_path *path, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07002203{
2204 int err = 0, correct_index = 0;
2205 int depth = ext_depth(inode), credits;
2206 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002207 ext4_lblk_t a, b, block;
2208 unsigned num;
2209 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002210 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002211 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002212 struct ext4_extent *ex;
2213
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002214 /* the header must be checked already in ext4_ext_remove_space() */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002215 ext_debug("truncate since %u in leaf\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002216 if (!path[depth].p_hdr)
2217 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2218 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002219 if (unlikely(path[depth].p_hdr == NULL)) {
2220 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2221 return -EIO;
2222 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002223 /* find where to start removing */
2224 ex = EXT_LAST_EXTENT(eh);
2225
2226 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002227 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002228
2229 while (ex >= EXT_FIRST_EXTENT(eh) &&
2230 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002231
2232 if (ext4_ext_is_uninitialized(ex))
2233 uninitialized = 1;
2234 else
2235 uninitialized = 0;
2236
Mingming553f9002009-09-18 13:34:55 -04002237 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2238 uninitialized, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002239 path[depth].p_ext = ex;
2240
2241 a = ex_ee_block > start ? ex_ee_block : start;
2242 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
2243 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
2244
2245 ext_debug(" border %u:%u\n", a, b);
2246
2247 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
2248 block = 0;
2249 num = 0;
2250 BUG();
2251 } else if (a != ex_ee_block) {
2252 /* remove tail of the extent */
2253 block = ex_ee_block;
2254 num = a - block;
2255 } else if (b != ex_ee_block + ex_ee_len - 1) {
2256 /* remove head of the extent */
2257 block = a;
2258 num = b - a;
2259 /* there is no "make a hole" API yet */
2260 BUG();
2261 } else {
2262 /* remove whole extent: excellent! */
2263 block = ex_ee_block;
2264 num = 0;
2265 BUG_ON(a != ex_ee_block);
2266 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
2267 }
2268
Theodore Ts'o34071da2008-08-01 21:59:19 -04002269 /*
2270 * 3 for leaf, sb, and inode plus 2 (bmap and group
2271 * descriptor) for each block group; assume two block
2272 * groups plus ex_ee_len/blocks_per_block_group for
2273 * the worst case
2274 */
2275 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002276 if (ex == EXT_FIRST_EXTENT(eh)) {
2277 correct_index = 1;
2278 credits += (ext_depth(inode)) + 1;
2279 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002280 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002281
Jan Kara487caee2009-08-17 22:17:20 -04002282 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
Shen Feng9102e4f2008-07-11 19:27:31 -04002283 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002284 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002285
2286 err = ext4_ext_get_access(handle, inode, path + depth);
2287 if (err)
2288 goto out;
2289
2290 err = ext4_remove_blocks(handle, inode, ex, a, b);
2291 if (err)
2292 goto out;
2293
2294 if (num == 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002295 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002296 ext4_ext_store_pblock(ex, 0);
Marcin Slusarze8546d02008-04-17 10:38:59 -04002297 le16_add_cpu(&eh->eh_entries, -1);
Alex Tomasa86c6182006-10-11 01:21:03 -07002298 }
2299
2300 ex->ee_block = cpu_to_le32(block);
2301 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002302 /*
2303 * Do not mark uninitialized if all the blocks in the
2304 * extent have been removed.
2305 */
2306 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002307 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002308
2309 err = ext4_ext_dirty(handle, inode, path + depth);
2310 if (err)
2311 goto out;
2312
Mingming Cao2ae02102006-10-11 01:21:11 -07002313 ext_debug("new extent: %u:%u:%llu\n", block, num,
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002314 ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002315 ex--;
2316 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002317 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002318 }
2319
2320 if (correct_index && eh->eh_entries)
2321 err = ext4_ext_correct_indexes(handle, inode, path);
2322
2323 /* if this leaf is free, then we should
2324 * remove it from index block above */
2325 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2326 err = ext4_ext_rm_idx(handle, inode, path + depth);
2327
2328out:
2329 return err;
2330}
2331
2332/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002333 * ext4_ext_more_to_rm:
2334 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002335 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002336static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002337ext4_ext_more_to_rm(struct ext4_ext_path *path)
2338{
2339 BUG_ON(path->p_idx == NULL);
2340
2341 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2342 return 0;
2343
2344 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002345 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002346 * so we have to consider current index for truncation
2347 */
2348 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2349 return 0;
2350 return 1;
2351}
2352
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002353static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07002354{
2355 struct super_block *sb = inode->i_sb;
2356 int depth = ext_depth(inode);
2357 struct ext4_ext_path *path;
2358 handle_t *handle;
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002359 int i, err;
Alex Tomasa86c6182006-10-11 01:21:03 -07002360
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002361 ext_debug("truncate since %u\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002362
2363 /* probably first extent we're gonna free will be last in block */
2364 handle = ext4_journal_start(inode, depth + 1);
2365 if (IS_ERR(handle))
2366 return PTR_ERR(handle);
2367
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002368again:
Alex Tomasa86c6182006-10-11 01:21:03 -07002369 ext4_ext_invalidate_cache(inode);
2370
2371 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002372 * We start scanning from right side, freeing all the blocks
2373 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002374 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002375 depth = ext_depth(inode);
Josef Bacik216553c2008-04-29 22:02:02 -04002376 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07002377 if (path == NULL) {
2378 ext4_journal_stop(handle);
2379 return -ENOMEM;
2380 }
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002381 path[0].p_depth = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -07002382 path[0].p_hdr = ext_inode_hdr(inode);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002383 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002384 err = -EIO;
2385 goto out;
2386 }
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002387 i = err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002388
2389 while (i >= 0 && err == 0) {
2390 if (i == depth) {
2391 /* this is leaf block */
2392 err = ext4_ext_rm_leaf(handle, inode, path, start);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002393 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002394 brelse(path[i].p_bh);
2395 path[i].p_bh = NULL;
2396 i--;
2397 continue;
2398 }
2399
2400 /* this is index block */
2401 if (!path[i].p_hdr) {
2402 ext_debug("initialize header\n");
2403 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002404 }
2405
Alex Tomasa86c6182006-10-11 01:21:03 -07002406 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002407 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002408 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2409 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2410 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2411 path[i].p_hdr,
2412 le16_to_cpu(path[i].p_hdr->eh_entries));
2413 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002414 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002415 path[i].p_idx--;
2416 }
2417
2418 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2419 i, EXT_FIRST_INDEX(path[i].p_hdr),
2420 path[i].p_idx);
2421 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002422 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002423 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002424 ext_debug("move to level %d (block %llu)\n",
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002425 i + 1, idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002426 memset(path + i + 1, 0, sizeof(*path));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002427 bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2428 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002429 /* should we reset i_size? */
2430 err = -EIO;
2431 break;
2432 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002433 if (WARN_ON(i + 1 > depth)) {
2434 err = -EIO;
2435 break;
2436 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002437 if (ext4_ext_check(inode, ext_block_hdr(bh),
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002438 depth - i - 1)) {
2439 err = -EIO;
2440 break;
2441 }
2442 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002443
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002444 /* save actual number of indexes since this
2445 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002446 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2447 i++;
2448 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002449 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002450 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002451 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002452 * handle must be already prepared by the
2453 * truncatei_leaf() */
2454 err = ext4_ext_rm_idx(handle, inode, path + i);
2455 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002456 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002457 brelse(path[i].p_bh);
2458 path[i].p_bh = NULL;
2459 i--;
2460 ext_debug("return to level %d\n", i);
2461 }
2462 }
2463
2464 /* TODO: flexible tree reduction should be here */
2465 if (path->p_hdr->eh_entries == 0) {
2466 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002467 * truncate to zero freed all the tree,
2468 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002469 */
2470 err = ext4_ext_get_access(handle, inode, path);
2471 if (err == 0) {
2472 ext_inode_hdr(inode)->eh_depth = 0;
2473 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04002474 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07002475 err = ext4_ext_dirty(handle, inode, path);
2476 }
2477 }
2478out:
Alex Tomasa86c6182006-10-11 01:21:03 -07002479 ext4_ext_drop_refs(path);
2480 kfree(path);
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002481 if (err == -EAGAIN)
2482 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07002483 ext4_journal_stop(handle);
2484
2485 return err;
2486}
2487
2488/*
2489 * called at mount time
2490 */
2491void ext4_ext_init(struct super_block *sb)
2492{
2493 /*
2494 * possible initialization would be here
2495 */
2496
Theodore Ts'o83982b62009-01-06 14:53:16 -05002497 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04002498#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002499 printk(KERN_INFO "EXT4-fs: file extents enabled");
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002500#ifdef AGGRESSIVE_TEST
2501 printk(", aggressive tests");
Alex Tomasa86c6182006-10-11 01:21:03 -07002502#endif
2503#ifdef CHECK_BINSEARCH
2504 printk(", check binsearch");
2505#endif
2506#ifdef EXTENTS_STATS
2507 printk(", stats");
2508#endif
2509 printk("\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04002510#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07002511#ifdef EXTENTS_STATS
2512 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2513 EXT4_SB(sb)->s_ext_min = 1 << 30;
2514 EXT4_SB(sb)->s_ext_max = 0;
2515#endif
2516 }
2517}
2518
2519/*
2520 * called at umount time
2521 */
2522void ext4_ext_release(struct super_block *sb)
2523{
Theodore Ts'o83982b62009-01-06 14:53:16 -05002524 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
Alex Tomasa86c6182006-10-11 01:21:03 -07002525 return;
2526
2527#ifdef EXTENTS_STATS
2528 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2529 struct ext4_sb_info *sbi = EXT4_SB(sb);
2530 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2531 sbi->s_ext_blocks, sbi->s_ext_extents,
2532 sbi->s_ext_blocks / sbi->s_ext_extents);
2533 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2534 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2535 }
2536#endif
2537}
2538
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002539static void bi_complete(struct bio *bio, int error)
2540{
2541 complete((struct completion *)bio->bi_private);
2542}
2543
2544/* FIXME!! we need to try to merge to left or right after zero-out */
2545static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2546{
Jing Zhangb7203032010-05-12 00:00:00 -04002547 int ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002548 struct bio *bio;
2549 int blkbits, blocksize;
2550 sector_t ee_pblock;
2551 struct completion event;
2552 unsigned int ee_len, len, done, offset;
2553
2554
2555 blkbits = inode->i_blkbits;
2556 blocksize = inode->i_sb->s_blocksize;
2557 ee_len = ext4_ext_get_actual_len(ex);
2558 ee_pblock = ext_pblock(ex);
2559
2560 /* convert ee_pblock to 512 byte sectors */
2561 ee_pblock = ee_pblock << (blkbits - 9);
2562
2563 while (ee_len > 0) {
2564
2565 if (ee_len > BIO_MAX_PAGES)
2566 len = BIO_MAX_PAGES;
2567 else
2568 len = ee_len;
2569
2570 bio = bio_alloc(GFP_NOIO, len);
Jing Zhangb7203032010-05-12 00:00:00 -04002571 if (!bio)
2572 return -ENOMEM;
2573
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002574 bio->bi_sector = ee_pblock;
2575 bio->bi_bdev = inode->i_sb->s_bdev;
2576
2577 done = 0;
2578 offset = 0;
2579 while (done < len) {
2580 ret = bio_add_page(bio, ZERO_PAGE(0),
2581 blocksize, offset);
2582 if (ret != blocksize) {
2583 /*
2584 * We can't add any more pages because of
2585 * hardware limitations. Start a new bio.
2586 */
2587 break;
2588 }
2589 done++;
2590 offset += blocksize;
2591 if (offset >= PAGE_CACHE_SIZE)
2592 offset = 0;
2593 }
2594
2595 init_completion(&event);
2596 bio->bi_private = &event;
2597 bio->bi_end_io = bi_complete;
2598 submit_bio(WRITE, bio);
2599 wait_for_completion(&event);
2600
Jing Zhangb7203032010-05-12 00:00:00 -04002601 if (!test_bit(BIO_UPTODATE, &bio->bi_flags)) {
2602 bio_put(bio);
2603 return -EIO;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002604 }
2605 bio_put(bio);
2606 ee_len -= done;
2607 ee_pblock += done << (blkbits - 9);
2608 }
Jing Zhangb7203032010-05-12 00:00:00 -04002609 return 0;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002610}
2611
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002612#define EXT4_EXT_ZERO_LEN 7
Amit Arora56055d32007-07-17 21:42:38 -04002613/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002614 * This function is called by ext4_ext_map_blocks() if someone tries to write
Amit Arora56055d32007-07-17 21:42:38 -04002615 * to an uninitialized extent. It may result in splitting the uninitialized
2616 * extent into multiple extents (upto three - one initialized and two
2617 * uninitialized).
2618 * There are three possibilities:
2619 * a> There is no split required: Entire extent should be initialized
2620 * b> Splits in two extents: Write is happening at either end of the extent
2621 * c> Splits in three extents: Somone is writing in middle of the extent
2622 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002623static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002624 struct inode *inode,
2625 struct ext4_map_blocks *map,
2626 struct ext4_ext_path *path)
Amit Arora56055d32007-07-17 21:42:38 -04002627{
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002628 struct ext4_extent *ex, newex, orig_ex;
Amit Arora56055d32007-07-17 21:42:38 -04002629 struct ext4_extent *ex1 = NULL;
2630 struct ext4_extent *ex2 = NULL;
2631 struct ext4_extent *ex3 = NULL;
2632 struct ext4_extent_header *eh;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002633 ext4_lblk_t ee_block, eof_block;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002634 unsigned int allocated, ee_len, depth;
Amit Arora56055d32007-07-17 21:42:38 -04002635 ext4_fsblk_t newblock;
2636 int err = 0;
2637 int ret = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002638 int may_zeroout;
2639
2640 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
2641 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002642 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002643
2644 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
2645 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002646 if (eof_block < map->m_lblk + map->m_len)
2647 eof_block = map->m_lblk + map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04002648
2649 depth = ext_depth(inode);
2650 eh = path[depth].p_hdr;
2651 ex = path[depth].p_ext;
2652 ee_block = le32_to_cpu(ex->ee_block);
2653 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002654 allocated = ee_len - (map->m_lblk - ee_block);
2655 newblock = map->m_lblk - ee_block + ext_pblock(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002656
Amit Arora56055d32007-07-17 21:42:38 -04002657 ex2 = ex;
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002658 orig_ex.ee_block = ex->ee_block;
2659 orig_ex.ee_len = cpu_to_le16(ee_len);
2660 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
Amit Arora56055d32007-07-17 21:42:38 -04002661
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002662 /*
2663 * It is safe to convert extent to initialized via explicit
2664 * zeroout only if extent is fully insde i_size or new_size.
2665 */
2666 may_zeroout = ee_block + ee_len <= eof_block;
2667
Aneesh Kumar K.V9df56432008-02-22 06:17:31 -05002668 err = ext4_ext_get_access(handle, inode, path + depth);
2669 if (err)
2670 goto out;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002671 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002672 if (ee_len <= 2*EXT4_EXT_ZERO_LEN && may_zeroout) {
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002673 err = ext4_ext_zeroout(inode, &orig_ex);
2674 if (err)
2675 goto fix_extent_len;
2676 /* update the extent length and mark as initialized */
2677 ex->ee_block = orig_ex.ee_block;
2678 ex->ee_len = orig_ex.ee_len;
2679 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2680 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002681 /* zeroed the full extent */
2682 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002683 }
Aneesh Kumar K.V9df56432008-02-22 06:17:31 -05002684
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002685 /* ex1: ee_block to map->m_lblk - 1 : uninitialized */
2686 if (map->m_lblk > ee_block) {
Amit Arora56055d32007-07-17 21:42:38 -04002687 ex1 = ex;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002688 ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block);
Amit Arora56055d32007-07-17 21:42:38 -04002689 ext4_ext_mark_uninitialized(ex1);
2690 ex2 = &newex;
2691 }
2692 /*
2693 * for sanity, update the length of the ex2 extent before
2694 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2695 * overlap of blocks.
2696 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002697 if (!ex1 && allocated > map->m_len)
2698 ex2->ee_len = cpu_to_le16(map->m_len);
Amit Arora56055d32007-07-17 21:42:38 -04002699 /* ex3: to ee_block + ee_len : uninitialised */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002700 if (allocated > map->m_len) {
Amit Arora56055d32007-07-17 21:42:38 -04002701 unsigned int newdepth;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002702 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002703 if (allocated <= EXT4_EXT_ZERO_LEN && may_zeroout) {
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002704 /*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002705 * map->m_lblk == ee_block is handled by the zerouout
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002706 * at the beginning.
2707 * Mark first half uninitialized.
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002708 * Mark second half initialized and zero out the
2709 * initialized extent
2710 */
2711 ex->ee_block = orig_ex.ee_block;
2712 ex->ee_len = cpu_to_le16(ee_len - allocated);
2713 ext4_ext_mark_uninitialized(ex);
2714 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2715 ext4_ext_dirty(handle, inode, path + depth);
2716
2717 ex3 = &newex;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002718 ex3->ee_block = cpu_to_le32(map->m_lblk);
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002719 ext4_ext_store_pblock(ex3, newblock);
2720 ex3->ee_len = cpu_to_le16(allocated);
Mingming Cao00314622009-09-28 15:49:08 -04002721 err = ext4_ext_insert_extent(handle, inode, path,
2722 ex3, 0);
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002723 if (err == -ENOSPC) {
2724 err = ext4_ext_zeroout(inode, &orig_ex);
2725 if (err)
2726 goto fix_extent_len;
2727 ex->ee_block = orig_ex.ee_block;
2728 ex->ee_len = orig_ex.ee_len;
2729 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2730 ext4_ext_dirty(handle, inode, path + depth);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002731 /* blocks available from map->m_lblk */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002732 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002733
2734 } else if (err)
2735 goto fix_extent_len;
2736
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002737 /*
2738 * We need to zero out the second half because
2739 * an fallocate request can update file size and
2740 * converting the second half to initialized extent
2741 * implies that we can leak some junk data to user
2742 * space.
2743 */
2744 err = ext4_ext_zeroout(inode, ex3);
2745 if (err) {
2746 /*
2747 * We should actually mark the
2748 * second half as uninit and return error
2749 * Insert would have changed the extent
2750 */
2751 depth = ext_depth(inode);
2752 ext4_ext_drop_refs(path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002753 path = ext4_ext_find_extent(inode, map->m_lblk,
2754 path);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002755 if (IS_ERR(path)) {
2756 err = PTR_ERR(path);
2757 return err;
2758 }
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002759 /* get the second half extent details */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002760 ex = path[depth].p_ext;
2761 err = ext4_ext_get_access(handle, inode,
2762 path + depth);
2763 if (err)
2764 return err;
2765 ext4_ext_mark_uninitialized(ex);
2766 ext4_ext_dirty(handle, inode, path + depth);
2767 return err;
2768 }
2769
2770 /* zeroed the second half */
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002771 return allocated;
2772 }
Amit Arora56055d32007-07-17 21:42:38 -04002773 ex3 = &newex;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002774 ex3->ee_block = cpu_to_le32(map->m_lblk + map->m_len);
2775 ext4_ext_store_pblock(ex3, newblock + map->m_len);
2776 ex3->ee_len = cpu_to_le16(allocated - map->m_len);
Amit Arora56055d32007-07-17 21:42:38 -04002777 ext4_ext_mark_uninitialized(ex3);
Mingming Cao00314622009-09-28 15:49:08 -04002778 err = ext4_ext_insert_extent(handle, inode, path, ex3, 0);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002779 if (err == -ENOSPC && may_zeroout) {
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002780 err = ext4_ext_zeroout(inode, &orig_ex);
2781 if (err)
2782 goto fix_extent_len;
2783 /* update the extent length and mark as initialized */
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002784 ex->ee_block = orig_ex.ee_block;
2785 ex->ee_len = orig_ex.ee_len;
2786 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002787 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002788 /* zeroed the full extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002789 /* blocks available from map->m_lblk */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002790 return allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002791
2792 } else if (err)
2793 goto fix_extent_len;
Amit Arora56055d32007-07-17 21:42:38 -04002794 /*
2795 * The depth, and hence eh & ex might change
2796 * as part of the insert above.
2797 */
2798 newdepth = ext_depth(inode);
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002799 /*
Coly Li73ac36e2009-01-07 18:09:16 -08002800 * update the extent length after successful insert of the
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002801 * split extent
2802 */
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002803 ee_len -= ext4_ext_get_actual_len(ex3);
2804 orig_ex.ee_len = cpu_to_le16(ee_len);
2805 may_zeroout = ee_block + ee_len <= eof_block;
2806
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002807 depth = newdepth;
2808 ext4_ext_drop_refs(path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002809 path = ext4_ext_find_extent(inode, map->m_lblk, path);
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002810 if (IS_ERR(path)) {
2811 err = PTR_ERR(path);
2812 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04002813 }
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002814 eh = path[depth].p_hdr;
2815 ex = path[depth].p_ext;
2816 if (ex2 != &newex)
2817 ex2 = ex;
2818
2819 err = ext4_ext_get_access(handle, inode, path + depth);
2820 if (err)
2821 goto out;
2822
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002823 allocated = map->m_len;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002824
2825 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2826 * to insert a extent in the middle zerout directly
2827 * otherwise give the extent a chance to merge to left
2828 */
2829 if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002830 map->m_lblk != ee_block && may_zeroout) {
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002831 err = ext4_ext_zeroout(inode, &orig_ex);
2832 if (err)
2833 goto fix_extent_len;
2834 /* update the extent length and mark as initialized */
2835 ex->ee_block = orig_ex.ee_block;
2836 ex->ee_len = orig_ex.ee_len;
2837 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2838 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002839 /* zero out the first half */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002840 /* blocks available from map->m_lblk */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002841 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002842 }
Amit Arora56055d32007-07-17 21:42:38 -04002843 }
2844 /*
2845 * If there was a change of depth as part of the
2846 * insertion of ex3 above, we need to update the length
2847 * of the ex1 extent again here
2848 */
2849 if (ex1 && ex1 != ex) {
2850 ex1 = ex;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002851 ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block);
Amit Arora56055d32007-07-17 21:42:38 -04002852 ext4_ext_mark_uninitialized(ex1);
2853 ex2 = &newex;
2854 }
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002855 /* ex2: map->m_lblk to map->m_lblk + maxblocks-1 : initialised */
2856 ex2->ee_block = cpu_to_le32(map->m_lblk);
Amit Arora56055d32007-07-17 21:42:38 -04002857 ext4_ext_store_pblock(ex2, newblock);
2858 ex2->ee_len = cpu_to_le16(allocated);
2859 if (ex2 != ex)
2860 goto insert;
Amit Arora56055d32007-07-17 21:42:38 -04002861 /*
2862 * New (initialized) extent starts from the first block
2863 * in the current extent. i.e., ex2 == ex
2864 * We have to see if it can be merged with the extent
2865 * on the left.
2866 */
2867 if (ex2 > EXT_FIRST_EXTENT(eh)) {
2868 /*
2869 * To merge left, pass "ex2 - 1" to try_to_merge(),
2870 * since it merges towards right _only_.
2871 */
2872 ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
2873 if (ret) {
2874 err = ext4_ext_correct_indexes(handle, inode, path);
2875 if (err)
2876 goto out;
2877 depth = ext_depth(inode);
2878 ex2--;
2879 }
2880 }
2881 /*
2882 * Try to Merge towards right. This might be required
2883 * only when the whole extent is being written to.
2884 * i.e. ex2 == ex and ex3 == NULL.
2885 */
2886 if (!ex3) {
2887 ret = ext4_ext_try_to_merge(inode, path, ex2);
2888 if (ret) {
2889 err = ext4_ext_correct_indexes(handle, inode, path);
2890 if (err)
2891 goto out;
2892 }
2893 }
2894 /* Mark modified extent as dirty */
2895 err = ext4_ext_dirty(handle, inode, path + depth);
2896 goto out;
2897insert:
Mingming Cao00314622009-09-28 15:49:08 -04002898 err = ext4_ext_insert_extent(handle, inode, path, &newex, 0);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002899 if (err == -ENOSPC && may_zeroout) {
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002900 err = ext4_ext_zeroout(inode, &orig_ex);
2901 if (err)
2902 goto fix_extent_len;
2903 /* update the extent length and mark as initialized */
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002904 ex->ee_block = orig_ex.ee_block;
2905 ex->ee_len = orig_ex.ee_len;
2906 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002907 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002908 /* zero out the first half */
2909 return allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002910 } else if (err)
2911 goto fix_extent_len;
Amit Arora56055d32007-07-17 21:42:38 -04002912out:
Mingming553f9002009-09-18 13:34:55 -04002913 ext4_ext_show_leaf(inode, path);
Amit Arora56055d32007-07-17 21:42:38 -04002914 return err ? err : allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002915
2916fix_extent_len:
2917 ex->ee_block = orig_ex.ee_block;
2918 ex->ee_len = orig_ex.ee_len;
2919 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2920 ext4_ext_mark_uninitialized(ex);
2921 ext4_ext_dirty(handle, inode, path + depth);
2922 return err;
Amit Arora56055d32007-07-17 21:42:38 -04002923}
2924
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05002925/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002926 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04002927 * ext4_get_blocks_dio_write() when DIO to write
2928 * to an uninitialized extent.
2929 *
2930 * Writing to an uninitized extent may result in splitting the uninitialized
2931 * extent into multiple /intialized unintialized extents (up to three)
2932 * There are three possibilities:
2933 * a> There is no split required: Entire extent should be uninitialized
2934 * b> Splits in two extents: Write is happening at either end of the extent
2935 * c> Splits in three extents: Somone is writing in middle of the extent
2936 *
2937 * One of more index blocks maybe needed if the extent tree grow after
2938 * the unintialized extent split. To prevent ENOSPC occur at the IO
2939 * complete, we need to split the uninitialized extent before DIO submit
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02002940 * the IO. The uninitialized extent called at this time will be split
Mingming Cao00314622009-09-28 15:49:08 -04002941 * into three uninitialized extent(at most). After IO complete, the part
2942 * being filled will be convert to initialized by the end_io callback function
2943 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05002944 *
2945 * Returns the size of uninitialized extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04002946 */
2947static int ext4_split_unwritten_extents(handle_t *handle,
2948 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002949 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04002950 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04002951 int flags)
2952{
2953 struct ext4_extent *ex, newex, orig_ex;
2954 struct ext4_extent *ex1 = NULL;
2955 struct ext4_extent *ex2 = NULL;
2956 struct ext4_extent *ex3 = NULL;
2957 struct ext4_extent_header *eh;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002958 ext4_lblk_t ee_block, eof_block;
Mingming Cao00314622009-09-28 15:49:08 -04002959 unsigned int allocated, ee_len, depth;
2960 ext4_fsblk_t newblock;
2961 int err = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002962 int may_zeroout;
Mingming Cao00314622009-09-28 15:49:08 -04002963
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002964 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
2965 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002966 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002967
2968 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
2969 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002970 if (eof_block < map->m_lblk + map->m_len)
2971 eof_block = map->m_lblk + map->m_len;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002972
Mingming Cao00314622009-09-28 15:49:08 -04002973 depth = ext_depth(inode);
2974 eh = path[depth].p_hdr;
2975 ex = path[depth].p_ext;
2976 ee_block = le32_to_cpu(ex->ee_block);
2977 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002978 allocated = ee_len - (map->m_lblk - ee_block);
2979 newblock = map->m_lblk - ee_block + ext_pblock(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002980
Mingming Cao00314622009-09-28 15:49:08 -04002981 ex2 = ex;
2982 orig_ex.ee_block = ex->ee_block;
2983 orig_ex.ee_len = cpu_to_le16(ee_len);
2984 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
2985
2986 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002987 * It is safe to convert extent to initialized via explicit
2988 * zeroout only if extent is fully insde i_size or new_size.
2989 */
2990 may_zeroout = ee_block + ee_len <= eof_block;
2991
2992 /*
Mingmingba230c32009-11-06 04:01:23 -05002993 * If the uninitialized extent begins at the same logical
2994 * block where the write begins, and the write completely
2995 * covers the extent, then we don't need to split it.
Mingming Cao00314622009-09-28 15:49:08 -04002996 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002997 if ((map->m_lblk == ee_block) && (allocated <= map->m_len))
Mingmingba230c32009-11-06 04:01:23 -05002998 return allocated;
Mingming Cao00314622009-09-28 15:49:08 -04002999
3000 err = ext4_ext_get_access(handle, inode, path + depth);
3001 if (err)
3002 goto out;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003003 /* ex1: ee_block to map->m_lblk - 1 : uninitialized */
3004 if (map->m_lblk > ee_block) {
Mingming Cao00314622009-09-28 15:49:08 -04003005 ex1 = ex;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003006 ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block);
Mingming Cao00314622009-09-28 15:49:08 -04003007 ext4_ext_mark_uninitialized(ex1);
3008 ex2 = &newex;
3009 }
3010 /*
3011 * for sanity, update the length of the ex2 extent before
3012 * we insert ex3, if ex1 is NULL. This is to avoid temporary
3013 * overlap of blocks.
3014 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003015 if (!ex1 && allocated > map->m_len)
3016 ex2->ee_len = cpu_to_le16(map->m_len);
Mingming Cao00314622009-09-28 15:49:08 -04003017 /* ex3: to ee_block + ee_len : uninitialised */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003018 if (allocated > map->m_len) {
Mingming Cao00314622009-09-28 15:49:08 -04003019 unsigned int newdepth;
3020 ex3 = &newex;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003021 ex3->ee_block = cpu_to_le32(map->m_lblk + map->m_len);
3022 ext4_ext_store_pblock(ex3, newblock + map->m_len);
3023 ex3->ee_len = cpu_to_le16(allocated - map->m_len);
Mingming Cao00314622009-09-28 15:49:08 -04003024 ext4_ext_mark_uninitialized(ex3);
3025 err = ext4_ext_insert_extent(handle, inode, path, ex3, flags);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003026 if (err == -ENOSPC && may_zeroout) {
Mingming Cao00314622009-09-28 15:49:08 -04003027 err = ext4_ext_zeroout(inode, &orig_ex);
3028 if (err)
3029 goto fix_extent_len;
3030 /* update the extent length and mark as initialized */
3031 ex->ee_block = orig_ex.ee_block;
3032 ex->ee_len = orig_ex.ee_len;
3033 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
3034 ext4_ext_dirty(handle, inode, path + depth);
3035 /* zeroed the full extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003036 /* blocks available from map->m_lblk */
Mingming Cao00314622009-09-28 15:49:08 -04003037 return allocated;
3038
3039 } else if (err)
3040 goto fix_extent_len;
3041 /*
3042 * The depth, and hence eh & ex might change
3043 * as part of the insert above.
3044 */
3045 newdepth = ext_depth(inode);
3046 /*
3047 * update the extent length after successful insert of the
3048 * split extent
3049 */
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003050 ee_len -= ext4_ext_get_actual_len(ex3);
3051 orig_ex.ee_len = cpu_to_le16(ee_len);
3052 may_zeroout = ee_block + ee_len <= eof_block;
3053
Mingming Cao00314622009-09-28 15:49:08 -04003054 depth = newdepth;
3055 ext4_ext_drop_refs(path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003056 path = ext4_ext_find_extent(inode, map->m_lblk, path);
Mingming Cao00314622009-09-28 15:49:08 -04003057 if (IS_ERR(path)) {
3058 err = PTR_ERR(path);
3059 goto out;
3060 }
3061 eh = path[depth].p_hdr;
3062 ex = path[depth].p_ext;
3063 if (ex2 != &newex)
3064 ex2 = ex;
3065
3066 err = ext4_ext_get_access(handle, inode, path + depth);
3067 if (err)
3068 goto out;
3069
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003070 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003071 }
3072 /*
3073 * If there was a change of depth as part of the
3074 * insertion of ex3 above, we need to update the length
3075 * of the ex1 extent again here
3076 */
3077 if (ex1 && ex1 != ex) {
3078 ex1 = ex;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003079 ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block);
Mingming Cao00314622009-09-28 15:49:08 -04003080 ext4_ext_mark_uninitialized(ex1);
3081 ex2 = &newex;
3082 }
3083 /*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003084 * ex2: map->m_lblk to map->m_lblk + map->m_len-1 : to be written
3085 * using direct I/O, uninitialised still.
Mingming Cao00314622009-09-28 15:49:08 -04003086 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003087 ex2->ee_block = cpu_to_le32(map->m_lblk);
Mingming Cao00314622009-09-28 15:49:08 -04003088 ext4_ext_store_pblock(ex2, newblock);
3089 ex2->ee_len = cpu_to_le16(allocated);
3090 ext4_ext_mark_uninitialized(ex2);
3091 if (ex2 != ex)
3092 goto insert;
3093 /* Mark modified extent as dirty */
3094 err = ext4_ext_dirty(handle, inode, path + depth);
3095 ext_debug("out here\n");
3096 goto out;
3097insert:
3098 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003099 if (err == -ENOSPC && may_zeroout) {
Mingming Cao00314622009-09-28 15:49:08 -04003100 err = ext4_ext_zeroout(inode, &orig_ex);
3101 if (err)
3102 goto fix_extent_len;
3103 /* update the extent length and mark as initialized */
3104 ex->ee_block = orig_ex.ee_block;
3105 ex->ee_len = orig_ex.ee_len;
3106 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
3107 ext4_ext_dirty(handle, inode, path + depth);
3108 /* zero out the first half */
3109 return allocated;
3110 } else if (err)
3111 goto fix_extent_len;
3112out:
3113 ext4_ext_show_leaf(inode, path);
3114 return err ? err : allocated;
3115
3116fix_extent_len:
3117 ex->ee_block = orig_ex.ee_block;
3118 ex->ee_len = orig_ex.ee_len;
3119 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
3120 ext4_ext_mark_uninitialized(ex);
3121 ext4_ext_dirty(handle, inode, path + depth);
3122 return err;
3123}
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003124static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003125 struct inode *inode,
3126 struct ext4_ext_path *path)
3127{
3128 struct ext4_extent *ex;
3129 struct ext4_extent_header *eh;
3130 int depth;
3131 int err = 0;
3132 int ret = 0;
3133
3134 depth = ext_depth(inode);
3135 eh = path[depth].p_hdr;
3136 ex = path[depth].p_ext;
3137
3138 err = ext4_ext_get_access(handle, inode, path + depth);
3139 if (err)
3140 goto out;
3141 /* first mark the extent as initialized */
3142 ext4_ext_mark_initialized(ex);
3143
3144 /*
3145 * We have to see if it can be merged with the extent
3146 * on the left.
3147 */
3148 if (ex > EXT_FIRST_EXTENT(eh)) {
3149 /*
3150 * To merge left, pass "ex - 1" to try_to_merge(),
3151 * since it merges towards right _only_.
3152 */
3153 ret = ext4_ext_try_to_merge(inode, path, ex - 1);
3154 if (ret) {
3155 err = ext4_ext_correct_indexes(handle, inode, path);
3156 if (err)
3157 goto out;
3158 depth = ext_depth(inode);
3159 ex--;
3160 }
3161 }
3162 /*
3163 * Try to Merge towards right.
3164 */
3165 ret = ext4_ext_try_to_merge(inode, path, ex);
3166 if (ret) {
3167 err = ext4_ext_correct_indexes(handle, inode, path);
3168 if (err)
3169 goto out;
3170 depth = ext_depth(inode);
3171 }
3172 /* Mark modified extent as dirty */
3173 err = ext4_ext_dirty(handle, inode, path + depth);
3174out:
3175 ext4_ext_show_leaf(inode, path);
3176 return err;
3177}
3178
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003179static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3180 sector_t block, int count)
3181{
3182 int i;
3183 for (i = 0; i < count; i++)
3184 unmap_underlying_metadata(bdev, block + i);
3185}
3186
Mingming Cao00314622009-09-28 15:49:08 -04003187static int
3188ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003189 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003190 struct ext4_ext_path *path, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003191 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003192{
3193 int ret = 0;
3194 int err = 0;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003195 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Mingming Cao00314622009-09-28 15:49:08 -04003196
3197 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3198 "block %llu, max_blocks %u, flags %d, allocated %u",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003199 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04003200 flags, allocated);
3201 ext4_ext_show_leaf(inode, path);
3202
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003203 /* get_block() before submit the IO, split the extent */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003204 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003205 ret = ext4_split_unwritten_extents(handle, inode, map,
3206 path, flags);
Mingming5f524952009-11-10 10:48:04 -05003207 /*
3208 * Flag the inode(non aio case) or end_io struct (aio case)
3209 * that this IO needs to convertion to written when IO is
3210 * completed
3211 */
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003212 if (io)
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003213 io->flag = EXT4_IO_UNWRITTEN;
Mingming5f524952009-11-10 10:48:04 -05003214 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003215 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
Jiaying Zhang744692d2010-03-04 16:14:02 -05003216 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003217 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao00314622009-09-28 15:49:08 -04003218 goto out;
3219 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003220 /* IO end_io complete, convert the filled extent to written */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003221 if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003222 ret = ext4_convert_unwritten_extents_endio(handle, inode,
Mingming Cao00314622009-09-28 15:49:08 -04003223 path);
Jan Karab436b9b2009-12-08 23:51:10 -05003224 if (ret >= 0)
3225 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04003226 goto out2;
3227 }
3228 /* buffered IO case */
3229 /*
3230 * repeat fallocate creation request
3231 * we already have an unwritten extent
3232 */
3233 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3234 goto map_out;
3235
3236 /* buffered READ or buffered write_begin() lookup */
3237 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3238 /*
3239 * We have blocks reserved already. We
3240 * return allocated blocks so that delalloc
3241 * won't do block reservation for us. But
3242 * the buffer head will be unmapped so that
3243 * a read from the block returns 0s.
3244 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003245 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003246 goto out1;
3247 }
3248
3249 /* buffered write, writepage time, convert*/
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003250 ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
Jan Karab436b9b2009-12-08 23:51:10 -05003251 if (ret >= 0)
3252 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04003253out:
3254 if (ret <= 0) {
3255 err = ret;
3256 goto out2;
3257 } else
3258 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003259 map->m_flags |= EXT4_MAP_NEW;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003260 /*
3261 * if we allocated more blocks than requested
3262 * we need to make sure we unmap the extra block
3263 * allocated. The actual needed block will get
3264 * unmapped later when we find the buffer_head marked
3265 * new.
3266 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003267 if (allocated > map->m_len) {
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003268 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003269 newblock + map->m_len,
3270 allocated - map->m_len);
3271 allocated = map->m_len;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003272 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003273
3274 /*
3275 * If we have done fallocate with the offset that is already
3276 * delayed allocated, we would have block reservation
3277 * and quota reservation done in the delayed write path.
3278 * But fallocate would have already updated quota and block
3279 * count for this offset. So cancel these reservation
3280 */
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05003281 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003282 ext4_da_update_reserve_space(inode, allocated, 0);
3283
Mingming Cao00314622009-09-28 15:49:08 -04003284map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003285 map->m_flags |= EXT4_MAP_MAPPED;
Mingming Cao00314622009-09-28 15:49:08 -04003286out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003287 if (allocated > map->m_len)
3288 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003289 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003290 map->m_pblk = newblock;
3291 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04003292out2:
3293 if (path) {
3294 ext4_ext_drop_refs(path);
3295 kfree(path);
3296 }
3297 return err ? err : allocated;
3298}
3299/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05003300 * Block allocation/map/preallocation routine for extents based files
3301 *
3302 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003303 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003304 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3305 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05003306 *
3307 * return > 0, number of of blocks already mapped/allocated
3308 * if create == 0 and these are pre-allocated blocks
3309 * buffer head is unmapped
3310 * otherwise blocks are mapped
3311 *
3312 * return = 0, if plain look up failed (blocks have not been allocated)
3313 * buffer head is unmapped
3314 *
3315 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003316 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003317int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3318 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07003319{
3320 struct ext4_ext_path *path = NULL;
Amit Arora56055d32007-07-17 21:42:38 -04003321 struct ext4_extent_header *eh;
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003322 struct ext4_extent newex, *ex, *last_ex;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003323 ext4_fsblk_t newblock;
Theodore Ts'o786ec792010-05-17 00:00:00 -04003324 int i, err = 0, depth, ret, cache_type;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003325 unsigned int allocated = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003326 struct ext4_allocation_request ar;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003327 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Alex Tomasa86c6182006-10-11 01:21:03 -07003328
Mingming84fe3be2009-09-01 08:44:37 -04003329 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003330 map->m_lblk, map->m_len, inode->i_ino);
Alex Tomasa86c6182006-10-11 01:21:03 -07003331
3332 /* check in cache */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003333 cache_type = ext4_ext_in_cache(inode, map->m_lblk, &newex);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003334 if (cache_type) {
3335 if (cache_type == EXT4_EXT_CACHE_GAP) {
Theodore Ts'oc2177052009-05-14 00:58:52 -04003336 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003337 /*
3338 * block isn't allocated yet and
3339 * user doesn't want to allocate it
3340 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003341 goto out2;
3342 }
3343 /* we should allocate requested block */
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003344 } else if (cache_type == EXT4_EXT_CACHE_EXTENT) {
Alex Tomasa86c6182006-10-11 01:21:03 -07003345 /* block is already allocated */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003346 newblock = map->m_lblk
Dave Kleikamp8c55e202007-05-24 13:04:54 -04003347 - le32_to_cpu(newex.ee_block)
3348 + ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003349 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003350 allocated = ext4_ext_get_actual_len(&newex) -
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003351 (map->m_lblk - le32_to_cpu(newex.ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -07003352 goto out;
3353 } else {
3354 BUG();
3355 }
3356 }
3357
3358 /* find extent for this block */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003359 path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07003360 if (IS_ERR(path)) {
3361 err = PTR_ERR(path);
3362 path = NULL;
3363 goto out2;
3364 }
3365
3366 depth = ext_depth(inode);
3367
3368 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003369 * consistent leaf must not be empty;
3370 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07003371 * this is why assert can't be put in ext4_ext_find_extent()
3372 */
Frank Mayhar273df552010-03-02 11:46:09 -05003373 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
3374 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04003375 "lblock: %lu, depth: %d pblock %lld",
3376 (unsigned long) map->m_lblk, depth,
3377 path[depth].p_block);
Surbhi Palande034fb4c2009-12-14 09:53:52 -05003378 err = -EIO;
3379 goto out2;
3380 }
Amit Arora56055d32007-07-17 21:42:38 -04003381 eh = path[depth].p_hdr;
Alex Tomasa86c6182006-10-11 01:21:03 -07003382
Avantika Mathur7e028972006-12-06 20:41:33 -08003383 ex = path[depth].p_ext;
3384 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003385 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07003386 ext4_fsblk_t ee_start = ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003387 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003388
3389 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003390 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04003391 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003392 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003393 ee_len = ext4_ext_get_actual_len(ex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003394 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003395 if (in_range(map->m_lblk, ee_block, ee_len)) {
3396 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003397 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003398 allocated = ee_len - (map->m_lblk - ee_block);
3399 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
3400 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04003401
Amit Aroraa2df2a62007-07-17 21:42:41 -04003402 /* Do not put uninitialized extent in the cache */
Amit Arora56055d32007-07-17 21:42:38 -04003403 if (!ext4_ext_is_uninitialized(ex)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04003404 ext4_ext_put_in_cache(inode, ee_block,
3405 ee_len, ee_start,
3406 EXT4_EXT_CACHE_EXTENT);
Amit Arora56055d32007-07-17 21:42:38 -04003407 goto out;
3408 }
Mingming Cao00314622009-09-28 15:49:08 -04003409 ret = ext4_ext_handle_uninitialized_extents(handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003410 inode, map, path, flags, allocated,
3411 newblock);
Mingming Cao00314622009-09-28 15:49:08 -04003412 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07003413 }
3414 }
3415
3416 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003417 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07003418 * we couldn't try to create block if create flag is zero
3419 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04003420 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003421 /*
3422 * put just found gap into cache to speed up
3423 * subsequent requests
3424 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003425 ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
Alex Tomasa86c6182006-10-11 01:21:03 -07003426 goto out2;
3427 }
3428 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003429 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07003430 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003431
Alex Tomasc9de5602008-01-29 00:19:52 -05003432 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003433 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003434 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3435 if (err)
3436 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003437 ar.lright = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003438 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
3439 if (err)
3440 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04003441
Amit Arora749269f2007-07-18 09:02:56 -04003442 /*
3443 * See if request is beyond maximum number of blocks we can have in
3444 * a single extent. For an initialized extent this limit is
3445 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3446 * EXT_UNINIT_MAX_LEN.
3447 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003448 if (map->m_len > EXT_INIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003449 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003450 map->m_len = EXT_INIT_MAX_LEN;
3451 else if (map->m_len > EXT_UNINIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003452 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003453 map->m_len = EXT_UNINIT_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04003454
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003455 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
3456 newex.ee_block = cpu_to_le32(map->m_lblk);
3457 newex.ee_len = cpu_to_le16(map->m_len);
Amit Arora25d14f92007-05-24 13:04:13 -04003458 err = ext4_ext_check_overlap(inode, &newex, path);
3459 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003460 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04003461 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003462 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003463
3464 /* allocate new block */
3465 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003466 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
3467 ar.logical = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003468 ar.len = allocated;
3469 if (S_ISREG(inode->i_mode))
3470 ar.flags = EXT4_MB_HINT_DATA;
3471 else
3472 /* disable in-core preallocation for non-regular files */
3473 ar.flags = 0;
3474 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07003475 if (!newblock)
3476 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04003477 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003478 ar.goal, newblock, allocated);
Alex Tomasa86c6182006-10-11 01:21:03 -07003479
3480 /* try to insert new extent into found leaf and return */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07003481 ext4_ext_store_pblock(&newex, newblock);
Alex Tomasc9de5602008-01-29 00:19:52 -05003482 newex.ee_len = cpu_to_le16(ar.len);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003483 /* Mark uninitialized */
3484 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
Amit Aroraa2df2a62007-07-17 21:42:41 -04003485 ext4_ext_mark_uninitialized(&newex);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003486 /*
Jiaying Zhang744692d2010-03-04 16:14:02 -05003487 * io_end structure was created for every IO write to an
3488 * uninitialized extent. To avoid unecessary conversion,
3489 * here we flag the IO that really needs the conversion.
Mingming5f524952009-11-10 10:48:04 -05003490 * For non asycn direct IO case, flag the inode state
3491 * that we need to perform convertion when IO is done.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003492 */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003493 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Mingming5f524952009-11-10 10:48:04 -05003494 if (io)
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003495 io->flag = EXT4_IO_UNWRITTEN;
Mingming5f524952009-11-10 10:48:04 -05003496 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003497 ext4_set_inode_state(inode,
3498 EXT4_STATE_DIO_UNWRITTEN);
Mingming5f524952009-11-10 10:48:04 -05003499 }
Jiaying Zhang744692d2010-03-04 16:14:02 -05003500 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003501 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003502 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003503
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003504 if (unlikely(ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))) {
Frank Mayhar273df552010-03-02 11:46:09 -05003505 if (unlikely(!eh->eh_entries)) {
3506 EXT4_ERROR_INODE(inode,
Theodore Ts'of70f3622010-05-16 23:00:00 -04003507 "eh->eh_entries == 0 and "
3508 "EOFBLOCKS_FL set");
Frank Mayhar273df552010-03-02 11:46:09 -05003509 err = -EIO;
3510 goto out2;
3511 }
3512 last_ex = EXT_LAST_EXTENT(eh);
Theodore Ts'o786ec792010-05-17 00:00:00 -04003513 /*
3514 * If the current leaf block was reached by looking at
3515 * the last index block all the way down the tree, and
3516 * we are extending the inode beyond the last extent
3517 * in the current leaf block, then clear the
3518 * EOFBLOCKS_FL flag.
3519 */
3520 for (i = depth-1; i >= 0; i--) {
3521 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3522 break;
3523 }
3524 if ((i < 0) &&
3525 (map->m_lblk + ar.len > le32_to_cpu(last_ex->ee_block) +
3526 ext4_ext_get_actual_len(last_ex)))
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003527 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003528 }
Mingming Cao00314622009-09-28 15:49:08 -04003529 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
Alex Tomas315054f2007-05-24 13:04:25 -04003530 if (err) {
3531 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05003532 /* not a good idea to call discard here directly,
3533 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003534 ext4_discard_preallocations(inode);
Theodore Ts'oe6362602009-11-23 07:17:05 -05003535 ext4_free_blocks(handle, inode, 0, ext_pblock(&newex),
3536 ext4_ext_get_actual_len(&newex), 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07003537 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04003538 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003539
Alex Tomasa86c6182006-10-11 01:21:03 -07003540 /* previous routine could use block we allocated */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07003541 newblock = ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003542 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003543 if (allocated > map->m_len)
3544 allocated = map->m_len;
3545 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07003546
Jan Karab436b9b2009-12-08 23:51:10 -05003547 /*
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003548 * Update reserved blocks/metadata blocks after successful
3549 * block allocation which had been deferred till now.
3550 */
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05003551 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003552 ext4_da_update_reserve_space(inode, allocated, 1);
3553
3554 /*
Jan Karab436b9b2009-12-08 23:51:10 -05003555 * Cache the extent and update transaction to commit on fdatasync only
3556 * when it is _not_ an uninitialized extent.
3557 */
3558 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003559 ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock,
Amit Aroraa2df2a62007-07-17 21:42:41 -04003560 EXT4_EXT_CACHE_EXTENT);
Jan Karab436b9b2009-12-08 23:51:10 -05003561 ext4_update_inode_fsync_trans(handle, inode, 1);
3562 } else
3563 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07003564out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003565 if (allocated > map->m_len)
3566 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07003567 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003568 map->m_flags |= EXT4_MAP_MAPPED;
3569 map->m_pblk = newblock;
3570 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07003571out2:
3572 if (path) {
3573 ext4_ext_drop_refs(path);
3574 kfree(path);
3575 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003576 return err ? err : allocated;
3577}
3578
Jan Karacf108bc2008-07-11 19:27:31 -04003579void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07003580{
3581 struct address_space *mapping = inode->i_mapping;
3582 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003583 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07003584 handle_t *handle;
3585 int err = 0;
3586
3587 /*
3588 * probably first extent we're gonna free will be last in block
3589 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04003590 err = ext4_writepage_trans_blocks(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07003591 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04003592 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07003593 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07003594
Jan Karacf108bc2008-07-11 19:27:31 -04003595 if (inode->i_size & (sb->s_blocksize - 1))
3596 ext4_block_truncate_page(handle, mapping, inode->i_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07003597
Jan Kara9ddfc3d2008-07-11 19:27:31 -04003598 if (ext4_orphan_add(handle, inode))
3599 goto out_stop;
3600
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003601 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07003602 ext4_ext_invalidate_cache(inode);
3603
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003604 ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003605
Alex Tomasa86c6182006-10-11 01:21:03 -07003606 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003607 * TODO: optimization is possible here.
3608 * Probably we need not scan at all,
3609 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07003610 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003611
3612 /* we have to know where to truncate from in crash case */
3613 EXT4_I(inode)->i_disksize = inode->i_size;
3614 ext4_mark_inode_dirty(handle, inode);
3615
3616 last_block = (inode->i_size + sb->s_blocksize - 1)
3617 >> EXT4_BLOCK_SIZE_BITS(sb);
3618 err = ext4_ext_remove_space(inode, last_block);
3619
3620 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04003621 * transaction synchronous.
3622 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003623 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05003624 ext4_handle_sync(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07003625
3626out_stop:
Jan Kara9ddfc3d2008-07-11 19:27:31 -04003627 up_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07003628 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003629 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07003630 * then we need to clear up the orphan record which we created above.
3631 * However, if this was a real unlink then we were called by
3632 * ext4_delete_inode(), and we allow that function to clean up the
3633 * orphan info for us.
3634 */
3635 if (inode->i_nlink)
3636 ext4_orphan_del(handle, inode);
3637
Solofo Ramangalahyef737722008-04-29 22:00:41 -04003638 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3639 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07003640 ext4_journal_stop(handle);
3641}
3642
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003643static void ext4_falloc_update_inode(struct inode *inode,
3644 int mode, loff_t new_size, int update_ctime)
3645{
3646 struct timespec now;
3647
3648 if (update_ctime) {
3649 now = current_fs_time(inode->i_sb);
3650 if (!timespec_equal(&inode->i_ctime, &now))
3651 inode->i_ctime = now;
3652 }
3653 /*
3654 * Update only when preallocation was requested beyond
3655 * the file size.
3656 */
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003657 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3658 if (new_size > i_size_read(inode))
3659 i_size_write(inode, new_size);
3660 if (new_size > EXT4_I(inode)->i_disksize)
3661 ext4_update_i_disksize(inode, new_size);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003662 } else {
3663 /*
3664 * Mark that we allocate beyond EOF so the subsequent truncate
3665 * can proceed even if the new size is the same as i_size.
3666 */
3667 if (new_size > i_size_read(inode))
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003668 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003669 }
3670
3671}
3672
Amit Aroraa2df2a62007-07-17 21:42:41 -04003673/*
3674 * preallocate space for a file. This implements ext4's fallocate inode
3675 * operation, which gets called from sys_fallocate system call.
3676 * For block-mapped files, posix_fallocate should fall back to the method
3677 * of writing zeroes to the required new blocks (the same behavior which is
3678 * expected for file systems which do not support fallocate() system call).
3679 */
3680long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
3681{
3682 handle_t *handle;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003683 loff_t new_size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003684 unsigned int max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003685 int ret = 0;
3686 int ret2 = 0;
3687 int retries = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003688 struct ext4_map_blocks map;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003689 unsigned int credits, blkbits = inode->i_blkbits;
3690
3691 /*
3692 * currently supporting (pre)allocate mode for extent-based
3693 * files _only_
3694 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003695 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Amit Aroraa2df2a62007-07-17 21:42:41 -04003696 return -EOPNOTSUPP;
3697
3698 /* preallocation to directories is currently not supported */
3699 if (S_ISDIR(inode->i_mode))
3700 return -ENODEV;
3701
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003702 map.m_lblk = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003703 /*
3704 * We can't just convert len to max_blocks because
3705 * If blocksize = 4096 offset = 3072 and len = 2048
3706 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003707 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003708 - map.m_lblk;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003709 /*
Mingming Caof3bd1f32008-08-19 22:16:03 -04003710 * credits to insert 1 extent into extent tree
Amit Aroraa2df2a62007-07-17 21:42:41 -04003711 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04003712 credits = ext4_chunk_trans_blocks(inode, max_blocks);
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05003713 mutex_lock(&inode->i_mutex);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04003714 ret = inode_newsize_ok(inode, (len + offset));
3715 if (ret) {
3716 mutex_unlock(&inode->i_mutex);
3717 return ret;
3718 }
Amit Aroraa2df2a62007-07-17 21:42:41 -04003719retry:
3720 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003721 map.m_lblk = map.m_lblk + ret;
3722 map.m_len = max_blocks = max_blocks - ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003723 handle = ext4_journal_start(inode, credits);
3724 if (IS_ERR(handle)) {
3725 ret = PTR_ERR(handle);
3726 break;
3727 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003728 ret = ext4_map_blocks(handle, inode, &map,
Theodore Ts'oc2177052009-05-14 00:58:52 -04003729 EXT4_GET_BLOCKS_CREATE_UNINIT_EXT);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05003730 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003731#ifdef EXT4FS_DEBUG
3732 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003733 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003734 "returned error inode#%lu, block=%u, "
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05003735 "max_blocks=%u", __func__,
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05003736 inode->i_ino, block, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003737#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04003738 ext4_mark_inode_dirty(handle, inode);
3739 ret2 = ext4_journal_stop(handle);
3740 break;
3741 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003742 if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003743 blkbits) >> blkbits))
3744 new_size = offset + len;
3745 else
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003746 new_size = (map.m_lblk + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003747
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003748 ext4_falloc_update_inode(inode, mode, new_size,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003749 (map.m_flags & EXT4_MAP_NEW));
Amit Aroraa2df2a62007-07-17 21:42:41 -04003750 ext4_mark_inode_dirty(handle, inode);
3751 ret2 = ext4_journal_stop(handle);
3752 if (ret2)
3753 break;
3754 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003755 if (ret == -ENOSPC &&
3756 ext4_should_retry_alloc(inode->i_sb, &retries)) {
3757 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003758 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003759 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05003760 mutex_unlock(&inode->i_mutex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003761 return ret > 0 ? ret2 : ret;
3762}
Eric Sandeen6873fa02008-10-07 00:46:36 -04003763
3764/*
Mingming Cao00314622009-09-28 15:49:08 -04003765 * This function convert a range of blocks to written extents
3766 * The caller of this function will pass the start offset and the size.
3767 * all unwritten extents within this range will be converted to
3768 * written extents.
3769 *
3770 * This function is called from the direct IO end io call back
3771 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05003772 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04003773 */
3774int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
Eric Sandeena1de02d2010-02-04 23:58:38 -05003775 ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04003776{
3777 handle_t *handle;
Mingming Cao00314622009-09-28 15:49:08 -04003778 unsigned int max_blocks;
3779 int ret = 0;
3780 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003781 struct ext4_map_blocks map;
Mingming Cao00314622009-09-28 15:49:08 -04003782 unsigned int credits, blkbits = inode->i_blkbits;
3783
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003784 map.m_lblk = offset >> blkbits;
Mingming Cao00314622009-09-28 15:49:08 -04003785 /*
3786 * We can't just convert len to max_blocks because
3787 * If blocksize = 4096 offset = 3072 and len = 2048
3788 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003789 max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
3790 map.m_lblk);
Mingming Cao00314622009-09-28 15:49:08 -04003791 /*
3792 * credits to insert 1 extent into extent tree
3793 */
3794 credits = ext4_chunk_trans_blocks(inode, max_blocks);
3795 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003796 map.m_lblk += ret;
3797 map.m_len = (max_blocks -= ret);
Mingming Cao00314622009-09-28 15:49:08 -04003798 handle = ext4_journal_start(inode, credits);
3799 if (IS_ERR(handle)) {
3800 ret = PTR_ERR(handle);
3801 break;
3802 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003803 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003804 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Mingming Cao00314622009-09-28 15:49:08 -04003805 if (ret <= 0) {
3806 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003807 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Mingming Cao00314622009-09-28 15:49:08 -04003808 "returned error inode#%lu, block=%u, "
3809 "max_blocks=%u", __func__,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003810 inode->i_ino, map.m_lblk, map.m_len);
Mingming Cao00314622009-09-28 15:49:08 -04003811 }
3812 ext4_mark_inode_dirty(handle, inode);
3813 ret2 = ext4_journal_stop(handle);
3814 if (ret <= 0 || ret2 )
3815 break;
3816 }
3817 return ret > 0 ? ret2 : ret;
3818}
3819/*
Eric Sandeen6873fa02008-10-07 00:46:36 -04003820 * Callback function called for each extent to gather FIEMAP information.
3821 */
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003822static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path,
Eric Sandeen6873fa02008-10-07 00:46:36 -04003823 struct ext4_ext_cache *newex, struct ext4_extent *ex,
3824 void *data)
3825{
3826 struct fiemap_extent_info *fieinfo = data;
Eric Sandeenc9877b22009-05-01 23:32:06 -04003827 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003828 __u64 logical;
3829 __u64 physical;
3830 __u64 length;
3831 __u32 flags = 0;
3832 int error;
3833
3834 logical = (__u64)newex->ec_block << blksize_bits;
3835
3836 if (newex->ec_type == EXT4_EXT_CACHE_GAP) {
3837 pgoff_t offset;
3838 struct page *page;
3839 struct buffer_head *bh = NULL;
3840
3841 offset = logical >> PAGE_SHIFT;
3842 page = find_get_page(inode->i_mapping, offset);
3843 if (!page || !page_has_buffers(page))
3844 return EXT_CONTINUE;
3845
3846 bh = page_buffers(page);
3847
3848 if (!bh)
3849 return EXT_CONTINUE;
3850
3851 if (buffer_delay(bh)) {
3852 flags |= FIEMAP_EXTENT_DELALLOC;
3853 page_cache_release(page);
3854 } else {
3855 page_cache_release(page);
3856 return EXT_CONTINUE;
3857 }
3858 }
3859
3860 physical = (__u64)newex->ec_start << blksize_bits;
3861 length = (__u64)newex->ec_len << blksize_bits;
3862
3863 if (ex && ext4_ext_is_uninitialized(ex))
3864 flags |= FIEMAP_EXTENT_UNWRITTEN;
3865
3866 /*
3867 * If this extent reaches EXT_MAX_BLOCK, it must be last.
3868 *
3869 * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
3870 * this also indicates no more allocated blocks.
3871 *
3872 * XXX this might miss a single-block extent at EXT_MAX_BLOCK
3873 */
Eric Sandeenc9877b22009-05-01 23:32:06 -04003874 if (ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK ||
Theodore Ts'oeefd7f02009-05-02 19:05:37 -04003875 newex->ec_block + newex->ec_len - 1 == EXT_MAX_BLOCK) {
3876 loff_t size = i_size_read(inode);
3877 loff_t bs = EXT4_BLOCK_SIZE(inode->i_sb);
3878
Eric Sandeen6873fa02008-10-07 00:46:36 -04003879 flags |= FIEMAP_EXTENT_LAST;
Theodore Ts'oeefd7f02009-05-02 19:05:37 -04003880 if ((flags & FIEMAP_EXTENT_DELALLOC) &&
3881 logical+length > size)
3882 length = (size - logical + bs - 1) & ~(bs-1);
3883 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04003884
3885 error = fiemap_fill_next_extent(fieinfo, logical, physical,
3886 length, flags);
3887 if (error < 0)
3888 return error;
3889 if (error == 1)
3890 return EXT_BREAK;
3891
3892 return EXT_CONTINUE;
3893}
3894
3895/* fiemap flags we can handle specified here */
3896#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
3897
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003898static int ext4_xattr_fiemap(struct inode *inode,
3899 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04003900{
3901 __u64 physical = 0;
3902 __u64 length;
3903 __u32 flags = FIEMAP_EXTENT_LAST;
3904 int blockbits = inode->i_sb->s_blocksize_bits;
3905 int error = 0;
3906
3907 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003908 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04003909 struct ext4_iloc iloc;
3910 int offset; /* offset of xattr in inode */
3911
3912 error = ext4_get_inode_loc(inode, &iloc);
3913 if (error)
3914 return error;
3915 physical = iloc.bh->b_blocknr << blockbits;
3916 offset = EXT4_GOOD_OLD_INODE_SIZE +
3917 EXT4_I(inode)->i_extra_isize;
3918 physical += offset;
3919 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
3920 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04003921 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04003922 } else { /* external block */
3923 physical = EXT4_I(inode)->i_file_acl << blockbits;
3924 length = inode->i_sb->s_blocksize;
3925 }
3926
3927 if (physical)
3928 error = fiemap_fill_next_extent(fieinfo, 0, physical,
3929 length, flags);
3930 return (error < 0 ? error : 0);
3931}
3932
3933int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3934 __u64 start, __u64 len)
3935{
3936 ext4_lblk_t start_blk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003937 int error = 0;
3938
3939 /* fallback to generic here if not in extents fmt */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003940 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeen6873fa02008-10-07 00:46:36 -04003941 return generic_block_fiemap(inode, fieinfo, start, len,
3942 ext4_get_block);
3943
3944 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
3945 return -EBADR;
3946
3947 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
3948 error = ext4_xattr_fiemap(inode, fieinfo);
3949 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05003950 ext4_lblk_t len_blks;
3951 __u64 last_blk;
3952
Eric Sandeen6873fa02008-10-07 00:46:36 -04003953 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05003954 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
3955 if (last_blk >= EXT_MAX_BLOCK)
3956 last_blk = EXT_MAX_BLOCK-1;
3957 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003958
3959 /*
3960 * Walk the extent tree gathering extent information.
3961 * ext4_ext_fiemap_cb will push extents back to user.
3962 */
Eric Sandeen6873fa02008-10-07 00:46:36 -04003963 error = ext4_ext_walk_space(inode, start_blk, len_blks,
3964 ext4_ext_fiemap_cb, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04003965 }
3966
3967 return error;
3968}
3969