blob: 13db43408533100847dd9dad8682180176e6b392 [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
Shen Feng9102e4f2008-07-11 19:27:31 -040096static int ext4_ext_journal_restart(handle_t *handle, int needed)
Alex Tomasa86c6182006-10-11 01:21:03 -070097{
98 int err;
99
Frank Mayhar03901312009-01-07 00:06:22 -0500100 if (!ext4_handle_valid(handle))
101 return 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700102 if (handle->h_buffer_credits > needed)
Shen Feng9102e4f2008-07-11 19:27:31 -0400103 return 0;
104 err = ext4_journal_extend(handle, needed);
Theodore Ts'o0123c932008-08-01 20:57:54 -0400105 if (err <= 0)
Shen Feng9102e4f2008-07-11 19:27:31 -0400106 return err;
107 return ext4_journal_restart(handle, needed);
Alex Tomasa86c6182006-10-11 01:21:03 -0700108}
109
110/*
111 * could return:
112 * - EROFS
113 * - ENOMEM
114 */
115static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
116 struct ext4_ext_path *path)
117{
118 if (path->p_bh) {
119 /* path points to block */
120 return ext4_journal_get_write_access(handle, path->p_bh);
121 }
122 /* path points to leaf/index in inode body */
123 /* we use in-core data, no need to protect them */
124 return 0;
125}
126
127/*
128 * could return:
129 * - EROFS
130 * - ENOMEM
131 * - EIO
132 */
133static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
134 struct ext4_ext_path *path)
135{
136 int err;
137 if (path->p_bh) {
138 /* path points to block */
Frank Mayhar03901312009-01-07 00:06:22 -0500139 err = ext4_handle_dirty_metadata(handle, inode, path->p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700140 } else {
141 /* path points to leaf/index in inode body */
142 err = ext4_mark_inode_dirty(handle, inode);
143 }
144 return err;
145}
146
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700147static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700148 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500149 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700150{
151 struct ext4_inode_info *ei = EXT4_I(inode);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700152 ext4_fsblk_t bg_start;
Valerie Clement74d34872008-02-15 13:43:07 -0500153 ext4_fsblk_t last_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700154 ext4_grpblk_t colour;
Theodore Ts'oa4912122009-03-12 12:18:34 -0400155 ext4_group_t block_group;
156 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -0700157 int depth;
158
159 if (path) {
160 struct ext4_extent *ex;
161 depth = path->p_depth;
162
163 /* try to predict block placement */
Avantika Mathur7e028972006-12-06 20:41:33 -0800164 ex = path[depth].p_ext;
165 if (ex)
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700166 return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700167
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700168 /* it looks like index is empty;
169 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700170 if (path[depth].p_bh)
171 return path[depth].p_bh->b_blocknr;
172 }
173
174 /* OK. use inode's group */
Theodore Ts'oa4912122009-03-12 12:18:34 -0400175 block_group = ei->i_block_group;
176 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
177 /*
178 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
179 * block groups per flexgroup, reserve the first block
180 * group for directories and special files. Regular
181 * files will start at the second block group. This
182 * tends to speed up directory access and improves
183 * fsck times.
184 */
185 block_group &= ~(flex_size-1);
186 if (S_ISREG(inode->i_mode))
187 block_group++;
188 }
189 bg_start = (block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) +
Alex Tomasa86c6182006-10-11 01:21:03 -0700190 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block);
Valerie Clement74d34872008-02-15 13:43:07 -0500191 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
192
Theodore Ts'oa4912122009-03-12 12:18:34 -0400193 /*
194 * If we are doing delayed allocation, we don't need take
195 * colour into account.
196 */
197 if (test_opt(inode->i_sb, DELALLOC))
198 return bg_start;
199
Valerie Clement74d34872008-02-15 13:43:07 -0500200 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
201 colour = (current->pid % 16) *
Alex Tomasa86c6182006-10-11 01:21:03 -0700202 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
Valerie Clement74d34872008-02-15 13:43:07 -0500203 else
204 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
Alex Tomasa86c6182006-10-11 01:21:03 -0700205 return bg_start + colour + block;
206}
207
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400208/*
209 * Allocation for a meta data block
210 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700211static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400212ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700213 struct ext4_ext_path *path,
214 struct ext4_extent *ex, int *err)
215{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700216 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700217
218 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Theodore Ts'o97df5d12008-12-12 12:41:28 -0500219 newblock = ext4_new_meta_blocks(handle, inode, goal, NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700220 return newblock;
221}
222
Avantika Mathur09b88252006-12-06 20:41:36 -0800223static int ext4_ext_space_block(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -0700224{
225 int size;
226
227 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
228 / sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100229#ifdef AGGRESSIVE_TEST
Alex Tomasa86c6182006-10-11 01:21:03 -0700230 if (size > 6)
231 size = 6;
232#endif
233 return size;
234}
235
Avantika Mathur09b88252006-12-06 20:41:36 -0800236static int ext4_ext_space_block_idx(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -0700237{
238 int size;
239
240 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
241 / sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100242#ifdef AGGRESSIVE_TEST
Alex Tomasa86c6182006-10-11 01:21:03 -0700243 if (size > 5)
244 size = 5;
245#endif
246 return size;
247}
248
Avantika Mathur09b88252006-12-06 20:41:36 -0800249static int ext4_ext_space_root(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -0700250{
251 int size;
252
253 size = sizeof(EXT4_I(inode)->i_data);
254 size -= sizeof(struct ext4_extent_header);
255 size /= sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100256#ifdef AGGRESSIVE_TEST
Alex Tomasa86c6182006-10-11 01:21:03 -0700257 if (size > 3)
258 size = 3;
259#endif
260 return size;
261}
262
Avantika Mathur09b88252006-12-06 20:41:36 -0800263static int ext4_ext_space_root_idx(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -0700264{
265 int size;
266
267 size = sizeof(EXT4_I(inode)->i_data);
268 size -= sizeof(struct ext4_extent_header);
269 size /= sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100270#ifdef AGGRESSIVE_TEST
Alex Tomasa86c6182006-10-11 01:21:03 -0700271 if (size > 4)
272 size = 4;
273#endif
274 return size;
275}
276
Mingming Caod2a17632008-07-14 17:52:37 -0400277/*
278 * Calculate the number of metadata blocks needed
279 * to allocate @blocks
280 * Worse case is one block per extent
281 */
282int ext4_ext_calc_metadata_amount(struct inode *inode, int blocks)
283{
284 int lcap, icap, rcap, leafs, idxs, num;
285 int newextents = blocks;
286
287 rcap = ext4_ext_space_root_idx(inode);
288 lcap = ext4_ext_space_block(inode);
289 icap = ext4_ext_space_block_idx(inode);
290
291 /* number of new leaf blocks needed */
292 num = leafs = (newextents + lcap - 1) / lcap;
293
294 /*
295 * Worse case, we need separate index block(s)
296 * to link all new leaf blocks
297 */
298 idxs = (leafs + icap - 1) / icap;
299 do {
300 num += idxs;
301 idxs = (idxs + icap - 1) / icap;
302 } while (idxs > rcap);
303
304 return num;
305}
306
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400307static int
308ext4_ext_max_entries(struct inode *inode, int depth)
309{
310 int max;
311
312 if (depth == ext_depth(inode)) {
313 if (depth == 0)
314 max = ext4_ext_space_root(inode);
315 else
316 max = ext4_ext_space_root_idx(inode);
317 } else {
318 if (depth == 0)
319 max = ext4_ext_space_block(inode);
320 else
321 max = ext4_ext_space_block_idx(inode);
322 }
323
324 return max;
325}
326
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400327static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
328{
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400329 ext4_fsblk_t block = ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400330 int len = ext4_ext_get_actual_len(ext);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400331
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400332 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400333}
334
335static int ext4_valid_extent_idx(struct inode *inode,
336 struct ext4_extent_idx *ext_idx)
337{
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400338 ext4_fsblk_t block = idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400339
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400340 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400341}
342
343static int ext4_valid_extent_entries(struct inode *inode,
344 struct ext4_extent_header *eh,
345 int depth)
346{
347 struct ext4_extent *ext;
348 struct ext4_extent_idx *ext_idx;
349 unsigned short entries;
350 if (eh->eh_entries == 0)
351 return 1;
352
353 entries = le16_to_cpu(eh->eh_entries);
354
355 if (depth == 0) {
356 /* leaf entries */
357 ext = EXT_FIRST_EXTENT(eh);
358 while (entries) {
359 if (!ext4_valid_extent(inode, ext))
360 return 0;
361 ext++;
362 entries--;
363 }
364 } else {
365 ext_idx = EXT_FIRST_INDEX(eh);
366 while (entries) {
367 if (!ext4_valid_extent_idx(inode, ext_idx))
368 return 0;
369 ext_idx++;
370 entries--;
371 }
372 }
373 return 1;
374}
375
376static int __ext4_ext_check(const char *function, struct inode *inode,
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400377 struct ext4_extent_header *eh,
378 int depth)
379{
380 const char *error_msg;
381 int max = 0;
382
383 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
384 error_msg = "invalid magic";
385 goto corrupted;
386 }
387 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
388 error_msg = "unexpected eh_depth";
389 goto corrupted;
390 }
391 if (unlikely(eh->eh_max == 0)) {
392 error_msg = "invalid eh_max";
393 goto corrupted;
394 }
395 max = ext4_ext_max_entries(inode, depth);
396 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
397 error_msg = "too large eh_max";
398 goto corrupted;
399 }
400 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
401 error_msg = "invalid eh_entries";
402 goto corrupted;
403 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400404 if (!ext4_valid_extent_entries(inode, eh, depth)) {
405 error_msg = "invalid extent entries";
406 goto corrupted;
407 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400408 return 0;
409
410corrupted:
411 ext4_error(inode->i_sb, function,
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400412 "bad header/extent in inode #%lu: %s - magic %x, "
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400413 "entries %u, max %u(%u), depth %u(%u)",
414 inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
415 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
416 max, le16_to_cpu(eh->eh_depth), depth);
417
418 return -EIO;
419}
420
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400421#define ext4_ext_check(inode, eh, depth) \
422 __ext4_ext_check(__func__, inode, eh, depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400423
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400424int ext4_ext_check_inode(struct inode *inode)
425{
426 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
427}
428
Alex Tomasa86c6182006-10-11 01:21:03 -0700429#ifdef EXT_DEBUG
430static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
431{
432 int k, l = path->p_depth;
433
434 ext_debug("path:");
435 for (k = 0; k <= l; k++, path++) {
436 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700437 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700438 idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700439 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400440 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700441 le32_to_cpu(path->p_ext->ee_block),
Mingming553f9002009-09-18 13:34:55 -0400442 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400443 ext4_ext_get_actual_len(path->p_ext),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700444 ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700445 } else
446 ext_debug(" []");
447 }
448 ext_debug("\n");
449}
450
451static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
452{
453 int depth = ext_depth(inode);
454 struct ext4_extent_header *eh;
455 struct ext4_extent *ex;
456 int i;
457
458 if (!path)
459 return;
460
461 eh = path[depth].p_hdr;
462 ex = EXT_FIRST_EXTENT(eh);
463
Mingming553f9002009-09-18 13:34:55 -0400464 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
465
Alex Tomasa86c6182006-10-11 01:21:03 -0700466 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400467 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
468 ext4_ext_is_uninitialized(ex),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400469 ext4_ext_get_actual_len(ex), ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700470 }
471 ext_debug("\n");
472}
473#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400474#define ext4_ext_show_path(inode, path)
475#define ext4_ext_show_leaf(inode, path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700476#endif
477
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500478void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700479{
480 int depth = path->p_depth;
481 int i;
482
483 for (i = 0; i <= depth; i++, path++)
484 if (path->p_bh) {
485 brelse(path->p_bh);
486 path->p_bh = NULL;
487 }
488}
489
490/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700491 * ext4_ext_binsearch_idx:
492 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400493 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700494 */
495static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500496ext4_ext_binsearch_idx(struct inode *inode,
497 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700498{
499 struct ext4_extent_header *eh = path->p_hdr;
500 struct ext4_extent_idx *r, *l, *m;
501
Alex Tomasa86c6182006-10-11 01:21:03 -0700502
Eric Sandeenbba90742008-01-28 23:58:27 -0500503 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700504
505 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400506 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700507 while (l <= r) {
508 m = l + (r - l) / 2;
509 if (block < le32_to_cpu(m->ei_block))
510 r = m - 1;
511 else
512 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400513 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
514 m, le32_to_cpu(m->ei_block),
515 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700516 }
517
518 path->p_idx = l - 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700519 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400520 idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700521
522#ifdef CHECK_BINSEARCH
523 {
524 struct ext4_extent_idx *chix, *ix;
525 int k;
526
527 chix = ix = EXT_FIRST_INDEX(eh);
528 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
529 if (k != 0 &&
530 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400531 printk(KERN_DEBUG "k=%d, ix=0x%p, "
532 "first=0x%p\n", k,
533 ix, EXT_FIRST_INDEX(eh));
534 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700535 le32_to_cpu(ix->ei_block),
536 le32_to_cpu(ix[-1].ei_block));
537 }
538 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400539 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700540 if (block < le32_to_cpu(ix->ei_block))
541 break;
542 chix = ix;
543 }
544 BUG_ON(chix != path->p_idx);
545 }
546#endif
547
548}
549
550/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700551 * ext4_ext_binsearch:
552 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400553 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700554 */
555static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500556ext4_ext_binsearch(struct inode *inode,
557 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700558{
559 struct ext4_extent_header *eh = path->p_hdr;
560 struct ext4_extent *r, *l, *m;
561
Alex Tomasa86c6182006-10-11 01:21:03 -0700562 if (eh->eh_entries == 0) {
563 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700564 * this leaf is empty:
565 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700566 */
567 return;
568 }
569
Eric Sandeenbba90742008-01-28 23:58:27 -0500570 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700571
572 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400573 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700574
575 while (l <= r) {
576 m = l + (r - l) / 2;
577 if (block < le32_to_cpu(m->ee_block))
578 r = m - 1;
579 else
580 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400581 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
582 m, le32_to_cpu(m->ee_block),
583 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700584 }
585
586 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400587 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400588 le32_to_cpu(path->p_ext->ee_block),
589 ext_pblock(path->p_ext),
Mingming553f9002009-09-18 13:34:55 -0400590 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400591 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700592
593#ifdef CHECK_BINSEARCH
594 {
595 struct ext4_extent *chex, *ex;
596 int k;
597
598 chex = ex = EXT_FIRST_EXTENT(eh);
599 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
600 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400601 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700602 if (block < le32_to_cpu(ex->ee_block))
603 break;
604 chex = ex;
605 }
606 BUG_ON(chex != path->p_ext);
607 }
608#endif
609
610}
611
612int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
613{
614 struct ext4_extent_header *eh;
615
616 eh = ext_inode_hdr(inode);
617 eh->eh_depth = 0;
618 eh->eh_entries = 0;
619 eh->eh_magic = EXT4_EXT_MAGIC;
620 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
621 ext4_mark_inode_dirty(handle, inode);
622 ext4_ext_invalidate_cache(inode);
623 return 0;
624}
625
626struct ext4_ext_path *
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500627ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
628 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700629{
630 struct ext4_extent_header *eh;
631 struct buffer_head *bh;
632 short int depth, i, ppos = 0, alloc = 0;
633
634 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400635 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700636
637 /* account possible depth increase */
638 if (!path) {
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800639 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700640 GFP_NOFS);
641 if (!path)
642 return ERR_PTR(-ENOMEM);
643 alloc = 1;
644 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700645 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400646 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700647
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400648 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700649 /* walk through the tree */
650 while (i) {
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400651 int need_to_validate = 0;
652
Alex Tomasa86c6182006-10-11 01:21:03 -0700653 ext_debug("depth %d: num %d, max %d\n",
654 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400655
Alex Tomasa86c6182006-10-11 01:21:03 -0700656 ext4_ext_binsearch_idx(inode, path + ppos, block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700657 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700658 path[ppos].p_depth = i;
659 path[ppos].p_ext = NULL;
660
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400661 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
662 if (unlikely(!bh))
Alex Tomasa86c6182006-10-11 01:21:03 -0700663 goto err;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400664 if (!bh_uptodate_or_lock(bh)) {
665 if (bh_submit_read(bh) < 0) {
666 put_bh(bh);
667 goto err;
668 }
669 /* validate the extent entries */
670 need_to_validate = 1;
671 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700672 eh = ext_block_hdr(bh);
673 ppos++;
674 BUG_ON(ppos > depth);
675 path[ppos].p_bh = bh;
676 path[ppos].p_hdr = eh;
677 i--;
678
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400679 if (need_to_validate && ext4_ext_check(inode, eh, i))
Alex Tomasa86c6182006-10-11 01:21:03 -0700680 goto err;
681 }
682
683 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700684 path[ppos].p_ext = NULL;
685 path[ppos].p_idx = NULL;
686
Alex Tomasa86c6182006-10-11 01:21:03 -0700687 /* find extent */
688 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400689 /* if not an empty leaf */
690 if (path[ppos].p_ext)
691 path[ppos].p_block = ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700692
693 ext4_ext_show_path(inode, path);
694
695 return path;
696
697err:
698 ext4_ext_drop_refs(path);
699 if (alloc)
700 kfree(path);
701 return ERR_PTR(-EIO);
702}
703
704/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700705 * ext4_ext_insert_index:
706 * insert new index [@logical;@ptr] into the block at @curp;
707 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700708 */
709static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
710 struct ext4_ext_path *curp,
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700711 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700712{
713 struct ext4_extent_idx *ix;
714 int len, err;
715
Avantika Mathur7e028972006-12-06 20:41:33 -0800716 err = ext4_ext_get_access(handle, inode, curp);
717 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700718 return err;
719
720 BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
721 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
722 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
723 /* insert after */
724 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
725 len = (len - 1) * sizeof(struct ext4_extent_idx);
726 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400727 ext_debug("insert new index %d after: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700728 "move %d from 0x%p to 0x%p\n",
729 logical, ptr, len,
730 (curp->p_idx + 1), (curp->p_idx + 2));
731 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
732 }
733 ix = curp->p_idx + 1;
734 } else {
735 /* insert before */
736 len = len * sizeof(struct ext4_extent_idx);
737 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400738 ext_debug("insert new index %d before: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700739 "move %d from 0x%p to 0x%p\n",
740 logical, ptr, len,
741 curp->p_idx, (curp->p_idx + 1));
742 memmove(curp->p_idx + 1, curp->p_idx, len);
743 ix = curp->p_idx;
744 }
745
746 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700747 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400748 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700749
750 BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400751 > le16_to_cpu(curp->p_hdr->eh_max));
Alex Tomasa86c6182006-10-11 01:21:03 -0700752 BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
753
754 err = ext4_ext_dirty(handle, inode, curp);
755 ext4_std_error(inode->i_sb, err);
756
757 return err;
758}
759
760/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700761 * ext4_ext_split:
762 * inserts new subtree into the path, using free index entry
763 * at depth @at:
764 * - allocates all needed blocks (new leaf and all intermediate index blocks)
765 * - makes decision where to split
766 * - moves remaining extents and index entries (right to the split point)
767 * into the newly allocated blocks
768 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700769 */
770static int ext4_ext_split(handle_t *handle, struct inode *inode,
771 struct ext4_ext_path *path,
772 struct ext4_extent *newext, int at)
773{
774 struct buffer_head *bh = NULL;
775 int depth = ext_depth(inode);
776 struct ext4_extent_header *neh;
777 struct ext4_extent_idx *fidx;
778 struct ext4_extent *ex;
779 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700780 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700781 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700782 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700783 int err = 0;
784
785 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700786 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700787
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700788 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700789 * border from split point */
790 BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
791 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
792 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700793 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700794 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400795 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700796 } else {
797 border = newext->ee_block;
798 ext_debug("leaf will be added."
799 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400800 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700801 }
802
803 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700804 * If error occurs, then we break processing
805 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700806 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700807 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700808 */
809
810 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700811 * Get array to track all allocated blocks.
812 * We need this to handle errors and free blocks
813 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700814 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800815 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700816 if (!ablocks)
817 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700818
819 /* allocate all needed blocks */
820 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
821 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400822 newblock = ext4_ext_new_meta_block(handle, inode, path,
823 newext, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700824 if (newblock == 0)
825 goto cleanup;
826 ablocks[a] = newblock;
827 }
828
829 /* initialize new leaf */
830 newblock = ablocks[--a];
831 BUG_ON(newblock == 0);
832 bh = sb_getblk(inode->i_sb, newblock);
833 if (!bh) {
834 err = -EIO;
835 goto cleanup;
836 }
837 lock_buffer(bh);
838
Avantika Mathur7e028972006-12-06 20:41:33 -0800839 err = ext4_journal_get_create_access(handle, bh);
840 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700841 goto cleanup;
842
843 neh = ext_block_hdr(bh);
844 neh->eh_entries = 0;
845 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
846 neh->eh_magic = EXT4_EXT_MAGIC;
847 neh->eh_depth = 0;
848 ex = EXT_FIRST_EXTENT(neh);
849
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700850 /* move remainder of path[depth] to the new leaf */
Alex Tomasa86c6182006-10-11 01:21:03 -0700851 BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
852 /* start copy from next extent */
853 /* TODO: we could do it by single memmove */
854 m = 0;
855 path[depth].p_ext++;
856 while (path[depth].p_ext <=
857 EXT_MAX_EXTENT(path[depth].p_hdr)) {
Mingming553f9002009-09-18 13:34:55 -0400858 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400859 le32_to_cpu(path[depth].p_ext->ee_block),
860 ext_pblock(path[depth].p_ext),
Mingming553f9002009-09-18 13:34:55 -0400861 ext4_ext_is_uninitialized(path[depth].p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400862 ext4_ext_get_actual_len(path[depth].p_ext),
Alex Tomasa86c6182006-10-11 01:21:03 -0700863 newblock);
864 /*memmove(ex++, path[depth].p_ext++,
865 sizeof(struct ext4_extent));
866 neh->eh_entries++;*/
867 path[depth].p_ext++;
868 m++;
869 }
870 if (m) {
871 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400872 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700873 }
874
875 set_buffer_uptodate(bh);
876 unlock_buffer(bh);
877
Frank Mayhar03901312009-01-07 00:06:22 -0500878 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800879 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700880 goto cleanup;
881 brelse(bh);
882 bh = NULL;
883
884 /* correct old leaf */
885 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800886 err = ext4_ext_get_access(handle, inode, path + depth);
887 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700888 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400889 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800890 err = ext4_ext_dirty(handle, inode, path + depth);
891 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700892 goto cleanup;
893
894 }
895
896 /* create intermediate indexes */
897 k = depth - at - 1;
898 BUG_ON(k < 0);
899 if (k)
900 ext_debug("create %d intermediate indices\n", k);
901 /* insert new index into current index block */
902 /* current depth stored in i var */
903 i = depth - 1;
904 while (k--) {
905 oldblock = newblock;
906 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -0500907 bh = sb_getblk(inode->i_sb, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700908 if (!bh) {
909 err = -EIO;
910 goto cleanup;
911 }
912 lock_buffer(bh);
913
Avantika Mathur7e028972006-12-06 20:41:33 -0800914 err = ext4_journal_get_create_access(handle, bh);
915 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700916 goto cleanup;
917
918 neh = ext_block_hdr(bh);
919 neh->eh_entries = cpu_to_le16(1);
920 neh->eh_magic = EXT4_EXT_MAGIC;
921 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
922 neh->eh_depth = cpu_to_le16(depth - i);
923 fidx = EXT_FIRST_INDEX(neh);
924 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700925 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700926
Eric Sandeenbba90742008-01-28 23:58:27 -0500927 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
928 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700929 /* copy indexes */
930 m = 0;
931 path[i].p_idx++;
932
933 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
934 EXT_MAX_INDEX(path[i].p_hdr));
935 BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
936 EXT_LAST_INDEX(path[i].p_hdr));
937 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400938 ext_debug("%d: move %d:%llu in new index %llu\n", i,
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400939 le32_to_cpu(path[i].p_idx->ei_block),
940 idx_pblock(path[i].p_idx),
941 newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700942 /*memmove(++fidx, path[i].p_idx++,
943 sizeof(struct ext4_extent_idx));
944 neh->eh_entries++;
945 BUG_ON(neh->eh_entries > neh->eh_max);*/
946 path[i].p_idx++;
947 m++;
948 }
949 if (m) {
950 memmove(++fidx, path[i].p_idx - m,
951 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400952 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700953 }
954 set_buffer_uptodate(bh);
955 unlock_buffer(bh);
956
Frank Mayhar03901312009-01-07 00:06:22 -0500957 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800958 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700959 goto cleanup;
960 brelse(bh);
961 bh = NULL;
962
963 /* correct old index */
964 if (m) {
965 err = ext4_ext_get_access(handle, inode, path + i);
966 if (err)
967 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400968 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700969 err = ext4_ext_dirty(handle, inode, path + i);
970 if (err)
971 goto cleanup;
972 }
973
974 i--;
975 }
976
977 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -0700978 err = ext4_ext_insert_index(handle, inode, path + at,
979 le32_to_cpu(border), newblock);
980
981cleanup:
982 if (bh) {
983 if (buffer_locked(bh))
984 unlock_buffer(bh);
985 brelse(bh);
986 }
987
988 if (err) {
989 /* free all allocated blocks in error case */
990 for (i = 0; i < depth; i++) {
991 if (!ablocks[i])
992 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -0500993 ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700994 }
995 }
996 kfree(ablocks);
997
998 return err;
999}
1000
1001/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001002 * ext4_ext_grow_indepth:
1003 * implements tree growing procedure:
1004 * - allocates new block
1005 * - moves top-level data (index block or leaf) into the new block
1006 * - initializes new top-level, creating index that points to the
1007 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001008 */
1009static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
1010 struct ext4_ext_path *path,
1011 struct ext4_extent *newext)
1012{
1013 struct ext4_ext_path *curp = path;
1014 struct ext4_extent_header *neh;
1015 struct ext4_extent_idx *fidx;
1016 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001017 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001018 int err = 0;
1019
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -04001020 newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07001021 if (newblock == 0)
1022 return err;
1023
1024 bh = sb_getblk(inode->i_sb, newblock);
1025 if (!bh) {
1026 err = -EIO;
1027 ext4_std_error(inode->i_sb, err);
1028 return err;
1029 }
1030 lock_buffer(bh);
1031
Avantika Mathur7e028972006-12-06 20:41:33 -08001032 err = ext4_journal_get_create_access(handle, bh);
1033 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001034 unlock_buffer(bh);
1035 goto out;
1036 }
1037
1038 /* move top-level index/leaf into new block */
1039 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
1040
1041 /* set size of new block */
1042 neh = ext_block_hdr(bh);
1043 /* old root could have indexes or leaves
1044 * so calculate e_max right way */
1045 if (ext_depth(inode))
1046 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
1047 else
1048 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
1049 neh->eh_magic = EXT4_EXT_MAGIC;
1050 set_buffer_uptodate(bh);
1051 unlock_buffer(bh);
1052
Frank Mayhar03901312009-01-07 00:06:22 -05001053 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001054 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001055 goto out;
1056
1057 /* create index in new top-level index: num,max,pointer */
Avantika Mathur7e028972006-12-06 20:41:33 -08001058 err = ext4_ext_get_access(handle, inode, curp);
1059 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001060 goto out;
1061
1062 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
1063 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
1064 curp->p_hdr->eh_entries = cpu_to_le16(1);
1065 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
Dmitry Monakhove9f410b2007-07-18 09:09:15 -04001066
1067 if (path[0].p_hdr->eh_depth)
1068 curp->p_idx->ei_block =
1069 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
1070 else
1071 curp->p_idx->ei_block =
1072 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001073 ext4_idx_store_pblock(curp->p_idx, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001074
1075 neh = ext_inode_hdr(inode);
1076 fidx = EXT_FIRST_INDEX(neh);
Mingming Cao2ae02102006-10-11 01:21:11 -07001077 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001078 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001079 le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
Alex Tomasa86c6182006-10-11 01:21:03 -07001080
1081 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
1082 err = ext4_ext_dirty(handle, inode, curp);
1083out:
1084 brelse(bh);
1085
1086 return err;
1087}
1088
1089/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001090 * ext4_ext_create_new_leaf:
1091 * finds empty index and adds new leaf.
1092 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001093 */
1094static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
1095 struct ext4_ext_path *path,
1096 struct ext4_extent *newext)
1097{
1098 struct ext4_ext_path *curp;
1099 int depth, i, err = 0;
1100
1101repeat:
1102 i = depth = ext_depth(inode);
1103
1104 /* walk up to the tree and look for free index entry */
1105 curp = path + depth;
1106 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1107 i--;
1108 curp--;
1109 }
1110
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001111 /* we use already allocated block for index block,
1112 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001113 if (EXT_HAS_FREE_INDEX(curp)) {
1114 /* if we found index with free entry, then use that
1115 * entry: create all needed subtree and add new leaf */
1116 err = ext4_ext_split(handle, inode, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001117 if (err)
1118 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001119
1120 /* refill path */
1121 ext4_ext_drop_refs(path);
1122 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001123 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1124 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001125 if (IS_ERR(path))
1126 err = PTR_ERR(path);
1127 } else {
1128 /* tree is full, time to grow in depth */
1129 err = ext4_ext_grow_indepth(handle, inode, path, newext);
1130 if (err)
1131 goto out;
1132
1133 /* refill path */
1134 ext4_ext_drop_refs(path);
1135 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001136 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1137 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001138 if (IS_ERR(path)) {
1139 err = PTR_ERR(path);
1140 goto out;
1141 }
1142
1143 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001144 * only first (depth 0 -> 1) produces free space;
1145 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001146 */
1147 depth = ext_depth(inode);
1148 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001149 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001150 goto repeat;
1151 }
1152 }
1153
1154out:
1155 return err;
1156}
1157
1158/*
Alex Tomas1988b512008-01-28 23:58:27 -05001159 * search the closest allocated block to the left for *logical
1160 * and returns it at @logical + it's physical address at @phys
1161 * if *logical is the smallest allocated block, the function
1162 * returns 0 at @phys
1163 * return value contains 0 (success) or error code
1164 */
1165int
1166ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
1167 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1168{
1169 struct ext4_extent_idx *ix;
1170 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001171 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001172
1173 BUG_ON(path == NULL);
1174 depth = path->p_depth;
1175 *phys = 0;
1176
1177 if (depth == 0 && path->p_ext == NULL)
1178 return 0;
1179
1180 /* usually extent in the path covers blocks smaller
1181 * then *logical, but it can be that extent is the
1182 * first one in the file */
1183
1184 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001185 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001186 if (*logical < le32_to_cpu(ex->ee_block)) {
1187 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1188 while (--depth >= 0) {
1189 ix = path[depth].p_idx;
1190 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1191 }
1192 return 0;
1193 }
1194
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001195 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
Alex Tomas1988b512008-01-28 23:58:27 -05001196
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001197 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1198 *phys = ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001199 return 0;
1200}
1201
1202/*
1203 * search the closest allocated block to the right for *logical
1204 * and returns it at @logical + it's physical address at @phys
1205 * if *logical is the smallest allocated block, the function
1206 * returns 0 at @phys
1207 * return value contains 0 (success) or error code
1208 */
1209int
1210ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
1211 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1212{
1213 struct buffer_head *bh = NULL;
1214 struct ext4_extent_header *eh;
1215 struct ext4_extent_idx *ix;
1216 struct ext4_extent *ex;
1217 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001218 int depth; /* Note, NOT eh_depth; depth from top of tree */
1219 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001220
1221 BUG_ON(path == NULL);
1222 depth = path->p_depth;
1223 *phys = 0;
1224
1225 if (depth == 0 && path->p_ext == NULL)
1226 return 0;
1227
1228 /* usually extent in the path covers blocks smaller
1229 * then *logical, but it can be that extent is the
1230 * first one in the file */
1231
1232 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001233 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001234 if (*logical < le32_to_cpu(ex->ee_block)) {
1235 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1236 while (--depth >= 0) {
1237 ix = path[depth].p_idx;
1238 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1239 }
1240 *logical = le32_to_cpu(ex->ee_block);
1241 *phys = ext_pblock(ex);
1242 return 0;
1243 }
1244
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001245 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
Alex Tomas1988b512008-01-28 23:58:27 -05001246
1247 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1248 /* next allocated block in this leaf */
1249 ex++;
1250 *logical = le32_to_cpu(ex->ee_block);
1251 *phys = ext_pblock(ex);
1252 return 0;
1253 }
1254
1255 /* go up and search for index to the right */
1256 while (--depth >= 0) {
1257 ix = path[depth].p_idx;
1258 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001259 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001260 }
1261
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001262 /* we've gone up to the root and found no index to the right */
1263 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001264
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001265got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001266 /* we've found index to the right, let's
1267 * follow it and find the closest allocated
1268 * block to the right */
1269 ix++;
1270 block = idx_pblock(ix);
1271 while (++depth < path->p_depth) {
1272 bh = sb_bread(inode->i_sb, block);
1273 if (bh == NULL)
1274 return -EIO;
1275 eh = ext_block_hdr(bh);
Eric Sandeen395a87b2009-03-10 18:18:47 -04001276 /* subtract from p_depth to get proper eh_depth */
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001277 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001278 put_bh(bh);
1279 return -EIO;
1280 }
1281 ix = EXT_FIRST_INDEX(eh);
1282 block = idx_pblock(ix);
1283 put_bh(bh);
1284 }
1285
1286 bh = sb_bread(inode->i_sb, block);
1287 if (bh == NULL)
1288 return -EIO;
1289 eh = ext_block_hdr(bh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001290 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001291 put_bh(bh);
1292 return -EIO;
1293 }
1294 ex = EXT_FIRST_EXTENT(eh);
1295 *logical = le32_to_cpu(ex->ee_block);
1296 *phys = ext_pblock(ex);
1297 put_bh(bh);
1298 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001299}
1300
1301/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001302 * ext4_ext_next_allocated_block:
1303 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1304 * NOTE: it considers block number from index entry as
1305 * allocated block. Thus, index entries have to be consistent
1306 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001307 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001308static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001309ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1310{
1311 int depth;
1312
1313 BUG_ON(path == NULL);
1314 depth = path->p_depth;
1315
1316 if (depth == 0 && path->p_ext == NULL)
1317 return EXT_MAX_BLOCK;
1318
1319 while (depth >= 0) {
1320 if (depth == path->p_depth) {
1321 /* leaf */
1322 if (path[depth].p_ext !=
1323 EXT_LAST_EXTENT(path[depth].p_hdr))
1324 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1325 } else {
1326 /* index */
1327 if (path[depth].p_idx !=
1328 EXT_LAST_INDEX(path[depth].p_hdr))
1329 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1330 }
1331 depth--;
1332 }
1333
1334 return EXT_MAX_BLOCK;
1335}
1336
1337/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001338 * ext4_ext_next_leaf_block:
Alex Tomasa86c6182006-10-11 01:21:03 -07001339 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1340 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001341static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
Andrew Morton63f57932006-10-11 01:21:24 -07001342 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001343{
1344 int depth;
1345
1346 BUG_ON(path == NULL);
1347 depth = path->p_depth;
1348
1349 /* zero-tree has no leaf blocks at all */
1350 if (depth == 0)
1351 return EXT_MAX_BLOCK;
1352
1353 /* go to index block */
1354 depth--;
1355
1356 while (depth >= 0) {
1357 if (path[depth].p_idx !=
1358 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001359 return (ext4_lblk_t)
1360 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001361 depth--;
1362 }
1363
1364 return EXT_MAX_BLOCK;
1365}
1366
1367/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001368 * ext4_ext_correct_indexes:
1369 * if leaf gets modified and modified extent is first in the leaf,
1370 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001371 * TODO: do we need to correct tree in all cases?
1372 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001373static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001374 struct ext4_ext_path *path)
1375{
1376 struct ext4_extent_header *eh;
1377 int depth = ext_depth(inode);
1378 struct ext4_extent *ex;
1379 __le32 border;
1380 int k, err = 0;
1381
1382 eh = path[depth].p_hdr;
1383 ex = path[depth].p_ext;
1384 BUG_ON(ex == NULL);
1385 BUG_ON(eh == NULL);
1386
1387 if (depth == 0) {
1388 /* there is no tree at all */
1389 return 0;
1390 }
1391
1392 if (ex != EXT_FIRST_EXTENT(eh)) {
1393 /* we correct tree if first leaf got modified only */
1394 return 0;
1395 }
1396
1397 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001398 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001399 */
1400 k = depth - 1;
1401 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001402 err = ext4_ext_get_access(handle, inode, path + k);
1403 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001404 return err;
1405 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001406 err = ext4_ext_dirty(handle, inode, path + k);
1407 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001408 return err;
1409
1410 while (k--) {
1411 /* change all left-side indexes */
1412 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1413 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001414 err = ext4_ext_get_access(handle, inode, path + k);
1415 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001416 break;
1417 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001418 err = ext4_ext_dirty(handle, inode, path + k);
1419 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001420 break;
1421 }
1422
1423 return err;
1424}
1425
Akira Fujita748de672009-06-17 19:24:03 -04001426int
Alex Tomasa86c6182006-10-11 01:21:03 -07001427ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1428 struct ext4_extent *ex2)
1429{
Amit Arora749269f2007-07-18 09:02:56 -04001430 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001431
1432 /*
1433 * Make sure that either both extents are uninitialized, or
1434 * both are _not_.
1435 */
1436 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1437 return 0;
1438
Amit Arora749269f2007-07-18 09:02:56 -04001439 if (ext4_ext_is_uninitialized(ex1))
1440 max_len = EXT_UNINIT_MAX_LEN;
1441 else
1442 max_len = EXT_INIT_MAX_LEN;
1443
Amit Aroraa2df2a62007-07-17 21:42:41 -04001444 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1445 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1446
1447 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001448 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001449 return 0;
1450
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001451 /*
1452 * To allow future support for preallocated extents to be added
1453 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001454 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001455 */
Amit Arora749269f2007-07-18 09:02:56 -04001456 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001457 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001458#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001459 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001460 return 0;
1461#endif
1462
Amit Aroraa2df2a62007-07-17 21:42:41 -04001463 if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001464 return 1;
1465 return 0;
1466}
1467
1468/*
Amit Arora56055d32007-07-17 21:42:38 -04001469 * This function tries to merge the "ex" extent to the next extent in the tree.
1470 * It always tries to merge towards right. If you want to merge towards
1471 * left, pass "ex - 1" as argument instead of "ex".
1472 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1473 * 1 if they got merged.
1474 */
1475int ext4_ext_try_to_merge(struct inode *inode,
1476 struct ext4_ext_path *path,
1477 struct ext4_extent *ex)
1478{
1479 struct ext4_extent_header *eh;
1480 unsigned int depth, len;
1481 int merge_done = 0;
1482 int uninitialized = 0;
1483
1484 depth = ext_depth(inode);
1485 BUG_ON(path[depth].p_hdr == NULL);
1486 eh = path[depth].p_hdr;
1487
1488 while (ex < EXT_LAST_EXTENT(eh)) {
1489 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1490 break;
1491 /* merge with next extent! */
1492 if (ext4_ext_is_uninitialized(ex))
1493 uninitialized = 1;
1494 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1495 + ext4_ext_get_actual_len(ex + 1));
1496 if (uninitialized)
1497 ext4_ext_mark_uninitialized(ex);
1498
1499 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1500 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1501 * sizeof(struct ext4_extent);
1502 memmove(ex + 1, ex + 2, len);
1503 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001504 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001505 merge_done = 1;
1506 WARN_ON(eh->eh_entries == 0);
1507 if (!eh->eh_entries)
1508 ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
1509 "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
1510 }
1511
1512 return merge_done;
1513}
1514
1515/*
Amit Arora25d14f92007-05-24 13:04:13 -04001516 * check if a portion of the "newext" extent overlaps with an
1517 * existing extent.
1518 *
1519 * If there is an overlap discovered, it updates the length of the newext
1520 * such that there will be no overlap, and then returns 1.
1521 * If there is no overlap found, it returns 0.
1522 */
1523unsigned int ext4_ext_check_overlap(struct inode *inode,
1524 struct ext4_extent *newext,
1525 struct ext4_ext_path *path)
1526{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001527 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001528 unsigned int depth, len1;
1529 unsigned int ret = 0;
1530
1531 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001532 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001533 depth = ext_depth(inode);
1534 if (!path[depth].p_ext)
1535 goto out;
1536 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1537
1538 /*
1539 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001540 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001541 */
1542 if (b2 < b1) {
1543 b2 = ext4_ext_next_allocated_block(path);
1544 if (b2 == EXT_MAX_BLOCK)
1545 goto out;
1546 }
1547
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001548 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001549 if (b1 + len1 < b1) {
1550 len1 = EXT_MAX_BLOCK - b1;
1551 newext->ee_len = cpu_to_le16(len1);
1552 ret = 1;
1553 }
1554
1555 /* check for overlap */
1556 if (b1 + len1 > b2) {
1557 newext->ee_len = cpu_to_le16(b2 - b1);
1558 ret = 1;
1559 }
1560out:
1561 return ret;
1562}
1563
1564/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001565 * ext4_ext_insert_extent:
1566 * tries to merge requsted extent into the existing extent or
1567 * inserts requested extent as new one into the tree,
1568 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001569 */
1570int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1571 struct ext4_ext_path *path,
1572 struct ext4_extent *newext)
1573{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001574 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001575 struct ext4_extent *ex, *fex;
1576 struct ext4_extent *nearex; /* nearest extent */
1577 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001578 int depth, len, err;
1579 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001580 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001581
Amit Aroraa2df2a62007-07-17 21:42:41 -04001582 BUG_ON(ext4_ext_get_actual_len(newext) == 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07001583 depth = ext_depth(inode);
1584 ex = path[depth].p_ext;
1585 BUG_ON(path[depth].p_hdr == NULL);
1586
1587 /* try to insert block into found extent and return */
1588 if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
Mingming553f9002009-09-18 13:34:55 -04001589 ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
1590 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001591 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001592 le32_to_cpu(ex->ee_block),
Mingming553f9002009-09-18 13:34:55 -04001593 ext4_ext_is_uninitialized(ex),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001594 ext4_ext_get_actual_len(ex), ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001595 err = ext4_ext_get_access(handle, inode, path + depth);
1596 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001597 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001598
1599 /*
1600 * ext4_can_extents_be_merged should have checked that either
1601 * both extents are uninitialized, or both aren't. Thus we
1602 * need to check only one of them here.
1603 */
1604 if (ext4_ext_is_uninitialized(ex))
1605 uninitialized = 1;
1606 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1607 + ext4_ext_get_actual_len(newext));
1608 if (uninitialized)
1609 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001610 eh = path[depth].p_hdr;
1611 nearex = ex;
1612 goto merge;
1613 }
1614
1615repeat:
1616 depth = ext_depth(inode);
1617 eh = path[depth].p_hdr;
1618 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1619 goto has_space;
1620
1621 /* probably next leaf has space for us? */
1622 fex = EXT_LAST_EXTENT(eh);
1623 next = ext4_ext_next_leaf_block(inode, path);
1624 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1625 && next != EXT_MAX_BLOCK) {
1626 ext_debug("next leaf block - %d\n", next);
1627 BUG_ON(npath != NULL);
1628 npath = ext4_ext_find_extent(inode, next, NULL);
1629 if (IS_ERR(npath))
1630 return PTR_ERR(npath);
1631 BUG_ON(npath->p_depth != path->p_depth);
1632 eh = npath[depth].p_hdr;
1633 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1634 ext_debug("next leaf isnt full(%d)\n",
1635 le16_to_cpu(eh->eh_entries));
1636 path = npath;
1637 goto repeat;
1638 }
1639 ext_debug("next leaf has no free space(%d,%d)\n",
1640 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1641 }
1642
1643 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001644 * There is no free space in the found leaf.
1645 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001646 */
1647 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1648 if (err)
1649 goto cleanup;
1650 depth = ext_depth(inode);
1651 eh = path[depth].p_hdr;
1652
1653has_space:
1654 nearex = path[depth].p_ext;
1655
Avantika Mathur7e028972006-12-06 20:41:33 -08001656 err = ext4_ext_get_access(handle, inode, path + depth);
1657 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001658 goto cleanup;
1659
1660 if (!nearex) {
1661 /* there is no extent in this leaf, create first one */
Mingming553f9002009-09-18 13:34:55 -04001662 ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001663 le32_to_cpu(newext->ee_block),
1664 ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001665 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001666 ext4_ext_get_actual_len(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001667 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1668 } else if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001669 > le32_to_cpu(nearex->ee_block)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001670/* BUG_ON(newext->ee_block == nearex->ee_block); */
1671 if (nearex != EXT_LAST_EXTENT(eh)) {
1672 len = EXT_MAX_EXTENT(eh) - nearex;
1673 len = (len - 1) * sizeof(struct ext4_extent);
1674 len = len < 0 ? 0 : len;
Mingming553f9002009-09-18 13:34:55 -04001675 ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001676 "move %d from 0x%p to 0x%p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001677 le32_to_cpu(newext->ee_block),
1678 ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001679 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001680 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001681 nearex, len, nearex + 1, nearex + 2);
1682 memmove(nearex + 2, nearex + 1, len);
1683 }
1684 path[depth].p_ext = nearex + 1;
1685 } else {
1686 BUG_ON(newext->ee_block == nearex->ee_block);
1687 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1688 len = len < 0 ? 0 : len;
Mingming553f9002009-09-18 13:34:55 -04001689 ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001690 "move %d from 0x%p to 0x%p\n",
1691 le32_to_cpu(newext->ee_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001692 ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001693 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001694 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001695 nearex, len, nearex + 1, nearex + 2);
1696 memmove(nearex + 1, nearex, len);
1697 path[depth].p_ext = nearex;
1698 }
1699
Marcin Slusarze8546d02008-04-17 10:38:59 -04001700 le16_add_cpu(&eh->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001701 nearex = path[depth].p_ext;
1702 nearex->ee_block = newext->ee_block;
Aneesh Kumar K.Vb3776112007-10-16 18:38:25 -04001703 ext4_ext_store_pblock(nearex, ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001704 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001705
1706merge:
1707 /* try to merge extents to the right */
Amit Arora56055d32007-07-17 21:42:38 -04001708 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001709
1710 /* try to merge extents to the left */
1711
1712 /* time to correct all indexes above */
1713 err = ext4_ext_correct_indexes(handle, inode, path);
1714 if (err)
1715 goto cleanup;
1716
1717 err = ext4_ext_dirty(handle, inode, path + depth);
1718
1719cleanup:
1720 if (npath) {
1721 ext4_ext_drop_refs(npath);
1722 kfree(npath);
1723 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001724 ext4_ext_invalidate_cache(inode);
1725 return err;
1726}
1727
Eric Sandeen6873fa02008-10-07 00:46:36 -04001728int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1729 ext4_lblk_t num, ext_prepare_callback func,
1730 void *cbdata)
1731{
1732 struct ext4_ext_path *path = NULL;
1733 struct ext4_ext_cache cbex;
1734 struct ext4_extent *ex;
1735 ext4_lblk_t next, start = 0, end = 0;
1736 ext4_lblk_t last = block + num;
1737 int depth, exists, err = 0;
1738
1739 BUG_ON(func == NULL);
1740 BUG_ON(inode == NULL);
1741
1742 while (block < last && block != EXT_MAX_BLOCK) {
1743 num = last - block;
1744 /* find extent for this block */
1745 path = ext4_ext_find_extent(inode, block, path);
1746 if (IS_ERR(path)) {
1747 err = PTR_ERR(path);
1748 path = NULL;
1749 break;
1750 }
1751
1752 depth = ext_depth(inode);
1753 BUG_ON(path[depth].p_hdr == NULL);
1754 ex = path[depth].p_ext;
1755 next = ext4_ext_next_allocated_block(path);
1756
1757 exists = 0;
1758 if (!ex) {
1759 /* there is no extent yet, so try to allocate
1760 * all requested space */
1761 start = block;
1762 end = block + num;
1763 } else if (le32_to_cpu(ex->ee_block) > block) {
1764 /* need to allocate space before found extent */
1765 start = block;
1766 end = le32_to_cpu(ex->ee_block);
1767 if (block + num < end)
1768 end = block + num;
1769 } else if (block >= le32_to_cpu(ex->ee_block)
1770 + ext4_ext_get_actual_len(ex)) {
1771 /* need to allocate space after found extent */
1772 start = block;
1773 end = block + num;
1774 if (end >= next)
1775 end = next;
1776 } else if (block >= le32_to_cpu(ex->ee_block)) {
1777 /*
1778 * some part of requested space is covered
1779 * by found extent
1780 */
1781 start = block;
1782 end = le32_to_cpu(ex->ee_block)
1783 + ext4_ext_get_actual_len(ex);
1784 if (block + num < end)
1785 end = block + num;
1786 exists = 1;
1787 } else {
1788 BUG();
1789 }
1790 BUG_ON(end <= start);
1791
1792 if (!exists) {
1793 cbex.ec_block = start;
1794 cbex.ec_len = end - start;
1795 cbex.ec_start = 0;
1796 cbex.ec_type = EXT4_EXT_CACHE_GAP;
1797 } else {
1798 cbex.ec_block = le32_to_cpu(ex->ee_block);
1799 cbex.ec_len = ext4_ext_get_actual_len(ex);
1800 cbex.ec_start = ext_pblock(ex);
1801 cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
1802 }
1803
1804 BUG_ON(cbex.ec_len == 0);
1805 err = func(inode, path, &cbex, ex, cbdata);
1806 ext4_ext_drop_refs(path);
1807
1808 if (err < 0)
1809 break;
1810
1811 if (err == EXT_REPEAT)
1812 continue;
1813 else if (err == EXT_BREAK) {
1814 err = 0;
1815 break;
1816 }
1817
1818 if (ext_depth(inode) != depth) {
1819 /* depth was changed. we have to realloc path */
1820 kfree(path);
1821 path = NULL;
1822 }
1823
1824 block = cbex.ec_block + cbex.ec_len;
1825 }
1826
1827 if (path) {
1828 ext4_ext_drop_refs(path);
1829 kfree(path);
1830 }
1831
1832 return err;
1833}
1834
Avantika Mathur09b88252006-12-06 20:41:36 -08001835static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001836ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Mingming Caodd545672007-07-31 00:37:46 -07001837 __u32 len, ext4_fsblk_t start, int type)
Alex Tomasa86c6182006-10-11 01:21:03 -07001838{
1839 struct ext4_ext_cache *cex;
1840 BUG_ON(len == 0);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001841 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001842 cex = &EXT4_I(inode)->i_cached_extent;
1843 cex->ec_type = type;
1844 cex->ec_block = block;
1845 cex->ec_len = len;
1846 cex->ec_start = start;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001847 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001848}
1849
1850/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001851 * ext4_ext_put_gap_in_cache:
1852 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07001853 * and cache this gap
1854 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001855static void
Alex Tomasa86c6182006-10-11 01:21:03 -07001856ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001857 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07001858{
1859 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001860 unsigned long len;
1861 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001862 struct ext4_extent *ex;
1863
1864 ex = path[depth].p_ext;
1865 if (ex == NULL) {
1866 /* there is no extent yet, so gap is [0;-] */
1867 lblock = 0;
1868 len = EXT_MAX_BLOCK;
1869 ext_debug("cache gap(whole file):");
1870 } else if (block < le32_to_cpu(ex->ee_block)) {
1871 lblock = block;
1872 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05001873 ext_debug("cache gap(before): %u [%u:%u]",
1874 block,
1875 le32_to_cpu(ex->ee_block),
1876 ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07001877 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001878 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001879 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001880 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001881 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001882
1883 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05001884 ext_debug("cache gap(after): [%u:%u] %u",
1885 le32_to_cpu(ex->ee_block),
1886 ext4_ext_get_actual_len(ex),
1887 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001888 BUG_ON(next == lblock);
1889 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001890 } else {
1891 lblock = len = 0;
1892 BUG();
1893 }
1894
Eric Sandeenbba90742008-01-28 23:58:27 -05001895 ext_debug(" -> %u:%lu\n", lblock, len);
Alex Tomasa86c6182006-10-11 01:21:03 -07001896 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1897}
1898
Avantika Mathur09b88252006-12-06 20:41:36 -08001899static int
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001900ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
Alex Tomasa86c6182006-10-11 01:21:03 -07001901 struct ext4_extent *ex)
1902{
1903 struct ext4_ext_cache *cex;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001904 int ret = EXT4_EXT_CACHE_NO;
Alex Tomasa86c6182006-10-11 01:21:03 -07001905
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001906 /*
1907 * We borrow i_block_reservation_lock to protect i_cached_extent
1908 */
1909 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001910 cex = &EXT4_I(inode)->i_cached_extent;
1911
1912 /* has cache valid data? */
1913 if (cex->ec_type == EXT4_EXT_CACHE_NO)
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001914 goto errout;
Alex Tomasa86c6182006-10-11 01:21:03 -07001915
1916 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1917 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1918 if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001919 ex->ee_block = cpu_to_le32(cex->ec_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001920 ext4_ext_store_pblock(ex, cex->ec_start);
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001921 ex->ee_len = cpu_to_le16(cex->ec_len);
Eric Sandeenbba90742008-01-28 23:58:27 -05001922 ext_debug("%u cached by %u:%u:%llu\n",
1923 block,
1924 cex->ec_block, cex->ec_len, cex->ec_start);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001925 ret = cex->ec_type;
Alex Tomasa86c6182006-10-11 01:21:03 -07001926 }
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001927errout:
1928 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
1929 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07001930}
1931
1932/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001933 * ext4_ext_rm_idx:
1934 * removes index from the index block.
1935 * It's used in truncate case only, thus all requests are for
1936 * last index in the block only.
Alex Tomasa86c6182006-10-11 01:21:03 -07001937 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001938static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001939 struct ext4_ext_path *path)
1940{
1941 struct buffer_head *bh;
1942 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001943 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07001944
1945 /* free index block */
1946 path--;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001947 leaf = idx_pblock(path->p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -07001948 BUG_ON(path->p_hdr->eh_entries == 0);
Avantika Mathur7e028972006-12-06 20:41:33 -08001949 err = ext4_ext_get_access(handle, inode, path);
1950 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001951 return err;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001952 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08001953 err = ext4_ext_dirty(handle, inode, path);
1954 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001955 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07001956 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Alex Tomasa86c6182006-10-11 01:21:03 -07001957 bh = sb_find_get_block(inode->i_sb, leaf);
1958 ext4_forget(handle, 1, inode, bh, leaf);
Alex Tomasc9de5602008-01-29 00:19:52 -05001959 ext4_free_blocks(handle, inode, leaf, 1, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001960 return err;
1961}
1962
1963/*
Mingming Caoee12b632008-08-19 22:16:05 -04001964 * ext4_ext_calc_credits_for_single_extent:
1965 * This routine returns max. credits that needed to insert an extent
1966 * to the extent tree.
1967 * When pass the actual path, the caller should calculate credits
1968 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07001969 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04001970int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07001971 struct ext4_ext_path *path)
1972{
Alex Tomasa86c6182006-10-11 01:21:03 -07001973 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04001974 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04001975 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04001976
Alex Tomasa86c6182006-10-11 01:21:03 -07001977 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07001978 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04001979 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
1980
1981 /*
1982 * There are some space in the leaf tree, no
1983 * need to account for leaf block credit
1984 *
1985 * bitmaps and block group descriptor blocks
1986 * and other metadat blocks still need to be
1987 * accounted.
1988 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04001989 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04001990 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04001991 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04001992 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001993 }
1994
Mingming Cao525f4ed2008-08-19 22:15:58 -04001995 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04001996}
Alex Tomasa86c6182006-10-11 01:21:03 -07001997
Mingming Caoee12b632008-08-19 22:16:05 -04001998/*
1999 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2000 *
2001 * if nrblocks are fit in a single extent (chunk flag is 1), then
2002 * in the worse case, each tree level index/leaf need to be changed
2003 * if the tree split due to insert a new extent, then the old tree
2004 * index/leaf need to be updated too
2005 *
2006 * If the nrblocks are discontiguous, they could cause
2007 * the whole tree split more than once, but this is really rare.
2008 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002009int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoee12b632008-08-19 22:16:05 -04002010{
2011 int index;
2012 int depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002013
Mingming Caoee12b632008-08-19 22:16:05 -04002014 if (chunk)
2015 index = depth * 2;
2016 else
2017 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002018
Mingming Caoee12b632008-08-19 22:16:05 -04002019 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002020}
2021
2022static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2023 struct ext4_extent *ex,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002024 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002025{
2026 struct buffer_head *bh;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002027 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002028 int i, metadata = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002029
Alex Tomasc9de5602008-01-29 00:19:52 -05002030 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2031 metadata = 1;
Alex Tomasa86c6182006-10-11 01:21:03 -07002032#ifdef EXTENTS_STATS
2033 {
2034 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002035 spin_lock(&sbi->s_ext_stats_lock);
2036 sbi->s_ext_blocks += ee_len;
2037 sbi->s_ext_extents++;
2038 if (ee_len < sbi->s_ext_min)
2039 sbi->s_ext_min = ee_len;
2040 if (ee_len > sbi->s_ext_max)
2041 sbi->s_ext_max = ee_len;
2042 if (ext_depth(inode) > sbi->s_depth_max)
2043 sbi->s_depth_max = ext_depth(inode);
2044 spin_unlock(&sbi->s_ext_stats_lock);
2045 }
2046#endif
2047 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002048 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002049 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002050 ext4_lblk_t num;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002051 ext4_fsblk_t start;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002052
Amit Aroraa2df2a62007-07-17 21:42:41 -04002053 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2054 start = ext_pblock(ex) + ee_len - num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002055 ext_debug("free last %u blocks starting %llu\n", num, start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002056 for (i = 0; i < num; i++) {
2057 bh = sb_find_get_block(inode->i_sb, start + i);
2058 ext4_forget(handle, 0, inode, bh, start + i);
2059 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002060 ext4_free_blocks(handle, inode, start, num, metadata);
Alex Tomasa86c6182006-10-11 01:21:03 -07002061 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002062 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002063 printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
Amit Aroraa2df2a62007-07-17 21:42:41 -04002064 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002065 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002066 printk(KERN_INFO "strange request: removal(2) "
2067 "%u-%u from %u:%u\n",
2068 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002069 }
2070 return 0;
2071}
2072
2073static int
2074ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002075 struct ext4_ext_path *path, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07002076{
2077 int err = 0, correct_index = 0;
2078 int depth = ext_depth(inode), credits;
2079 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002080 ext4_lblk_t a, b, block;
2081 unsigned num;
2082 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002083 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002084 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002085 struct ext4_extent *ex;
2086
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002087 /* the header must be checked already in ext4_ext_remove_space() */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002088 ext_debug("truncate since %u in leaf\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002089 if (!path[depth].p_hdr)
2090 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2091 eh = path[depth].p_hdr;
2092 BUG_ON(eh == NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07002093
2094 /* find where to start removing */
2095 ex = EXT_LAST_EXTENT(eh);
2096
2097 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002098 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002099
2100 while (ex >= EXT_FIRST_EXTENT(eh) &&
2101 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002102
2103 if (ext4_ext_is_uninitialized(ex))
2104 uninitialized = 1;
2105 else
2106 uninitialized = 0;
2107
Mingming553f9002009-09-18 13:34:55 -04002108 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2109 uninitialized, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002110 path[depth].p_ext = ex;
2111
2112 a = ex_ee_block > start ? ex_ee_block : start;
2113 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
2114 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
2115
2116 ext_debug(" border %u:%u\n", a, b);
2117
2118 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
2119 block = 0;
2120 num = 0;
2121 BUG();
2122 } else if (a != ex_ee_block) {
2123 /* remove tail of the extent */
2124 block = ex_ee_block;
2125 num = a - block;
2126 } else if (b != ex_ee_block + ex_ee_len - 1) {
2127 /* remove head of the extent */
2128 block = a;
2129 num = b - a;
2130 /* there is no "make a hole" API yet */
2131 BUG();
2132 } else {
2133 /* remove whole extent: excellent! */
2134 block = ex_ee_block;
2135 num = 0;
2136 BUG_ON(a != ex_ee_block);
2137 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
2138 }
2139
Theodore Ts'o34071da2008-08-01 21:59:19 -04002140 /*
2141 * 3 for leaf, sb, and inode plus 2 (bmap and group
2142 * descriptor) for each block group; assume two block
2143 * groups plus ex_ee_len/blocks_per_block_group for
2144 * the worst case
2145 */
2146 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002147 if (ex == EXT_FIRST_EXTENT(eh)) {
2148 correct_index = 1;
2149 credits += (ext_depth(inode)) + 1;
2150 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002151 credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002152
Shen Feng9102e4f2008-07-11 19:27:31 -04002153 err = ext4_ext_journal_restart(handle, credits);
2154 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002155 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002156
2157 err = ext4_ext_get_access(handle, inode, path + depth);
2158 if (err)
2159 goto out;
2160
2161 err = ext4_remove_blocks(handle, inode, ex, a, b);
2162 if (err)
2163 goto out;
2164
2165 if (num == 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002166 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002167 ext4_ext_store_pblock(ex, 0);
Marcin Slusarze8546d02008-04-17 10:38:59 -04002168 le16_add_cpu(&eh->eh_entries, -1);
Alex Tomasa86c6182006-10-11 01:21:03 -07002169 }
2170
2171 ex->ee_block = cpu_to_le32(block);
2172 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002173 /*
2174 * Do not mark uninitialized if all the blocks in the
2175 * extent have been removed.
2176 */
2177 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002178 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002179
2180 err = ext4_ext_dirty(handle, inode, path + depth);
2181 if (err)
2182 goto out;
2183
Mingming Cao2ae02102006-10-11 01:21:11 -07002184 ext_debug("new extent: %u:%u:%llu\n", block, num,
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002185 ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002186 ex--;
2187 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002188 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002189 }
2190
2191 if (correct_index && eh->eh_entries)
2192 err = ext4_ext_correct_indexes(handle, inode, path);
2193
2194 /* if this leaf is free, then we should
2195 * remove it from index block above */
2196 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2197 err = ext4_ext_rm_idx(handle, inode, path + depth);
2198
2199out:
2200 return err;
2201}
2202
2203/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002204 * ext4_ext_more_to_rm:
2205 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002206 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002207static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002208ext4_ext_more_to_rm(struct ext4_ext_path *path)
2209{
2210 BUG_ON(path->p_idx == NULL);
2211
2212 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2213 return 0;
2214
2215 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002216 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002217 * so we have to consider current index for truncation
2218 */
2219 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2220 return 0;
2221 return 1;
2222}
2223
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002224static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07002225{
2226 struct super_block *sb = inode->i_sb;
2227 int depth = ext_depth(inode);
2228 struct ext4_ext_path *path;
2229 handle_t *handle;
2230 int i = 0, err = 0;
2231
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002232 ext_debug("truncate since %u\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002233
2234 /* probably first extent we're gonna free will be last in block */
2235 handle = ext4_journal_start(inode, depth + 1);
2236 if (IS_ERR(handle))
2237 return PTR_ERR(handle);
2238
2239 ext4_ext_invalidate_cache(inode);
2240
2241 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002242 * We start scanning from right side, freeing all the blocks
2243 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002244 */
Josef Bacik216553c2008-04-29 22:02:02 -04002245 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07002246 if (path == NULL) {
2247 ext4_journal_stop(handle);
2248 return -ENOMEM;
2249 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002250 path[0].p_hdr = ext_inode_hdr(inode);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002251 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002252 err = -EIO;
2253 goto out;
2254 }
2255 path[0].p_depth = depth;
2256
2257 while (i >= 0 && err == 0) {
2258 if (i == depth) {
2259 /* this is leaf block */
2260 err = ext4_ext_rm_leaf(handle, inode, path, start);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002261 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002262 brelse(path[i].p_bh);
2263 path[i].p_bh = NULL;
2264 i--;
2265 continue;
2266 }
2267
2268 /* this is index block */
2269 if (!path[i].p_hdr) {
2270 ext_debug("initialize header\n");
2271 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002272 }
2273
Alex Tomasa86c6182006-10-11 01:21:03 -07002274 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002275 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002276 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2277 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2278 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2279 path[i].p_hdr,
2280 le16_to_cpu(path[i].p_hdr->eh_entries));
2281 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002282 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002283 path[i].p_idx--;
2284 }
2285
2286 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2287 i, EXT_FIRST_INDEX(path[i].p_hdr),
2288 path[i].p_idx);
2289 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002290 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002291 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002292 ext_debug("move to level %d (block %llu)\n",
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002293 i + 1, idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002294 memset(path + i + 1, 0, sizeof(*path));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002295 bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2296 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002297 /* should we reset i_size? */
2298 err = -EIO;
2299 break;
2300 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002301 if (WARN_ON(i + 1 > depth)) {
2302 err = -EIO;
2303 break;
2304 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002305 if (ext4_ext_check(inode, ext_block_hdr(bh),
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002306 depth - i - 1)) {
2307 err = -EIO;
2308 break;
2309 }
2310 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002311
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002312 /* save actual number of indexes since this
2313 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002314 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2315 i++;
2316 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002317 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002318 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002319 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002320 * handle must be already prepared by the
2321 * truncatei_leaf() */
2322 err = ext4_ext_rm_idx(handle, inode, path + i);
2323 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002324 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002325 brelse(path[i].p_bh);
2326 path[i].p_bh = NULL;
2327 i--;
2328 ext_debug("return to level %d\n", i);
2329 }
2330 }
2331
2332 /* TODO: flexible tree reduction should be here */
2333 if (path->p_hdr->eh_entries == 0) {
2334 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002335 * truncate to zero freed all the tree,
2336 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002337 */
2338 err = ext4_ext_get_access(handle, inode, path);
2339 if (err == 0) {
2340 ext_inode_hdr(inode)->eh_depth = 0;
2341 ext_inode_hdr(inode)->eh_max =
2342 cpu_to_le16(ext4_ext_space_root(inode));
2343 err = ext4_ext_dirty(handle, inode, path);
2344 }
2345 }
2346out:
Alex Tomasa86c6182006-10-11 01:21:03 -07002347 ext4_ext_drop_refs(path);
2348 kfree(path);
2349 ext4_journal_stop(handle);
2350
2351 return err;
2352}
2353
2354/*
2355 * called at mount time
2356 */
2357void ext4_ext_init(struct super_block *sb)
2358{
2359 /*
2360 * possible initialization would be here
2361 */
2362
Theodore Ts'o83982b62009-01-06 14:53:16 -05002363 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002364 printk(KERN_INFO "EXT4-fs: file extents enabled");
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002365#ifdef AGGRESSIVE_TEST
2366 printk(", aggressive tests");
Alex Tomasa86c6182006-10-11 01:21:03 -07002367#endif
2368#ifdef CHECK_BINSEARCH
2369 printk(", check binsearch");
2370#endif
2371#ifdef EXTENTS_STATS
2372 printk(", stats");
2373#endif
2374 printk("\n");
2375#ifdef EXTENTS_STATS
2376 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2377 EXT4_SB(sb)->s_ext_min = 1 << 30;
2378 EXT4_SB(sb)->s_ext_max = 0;
2379#endif
2380 }
2381}
2382
2383/*
2384 * called at umount time
2385 */
2386void ext4_ext_release(struct super_block *sb)
2387{
Theodore Ts'o83982b62009-01-06 14:53:16 -05002388 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
Alex Tomasa86c6182006-10-11 01:21:03 -07002389 return;
2390
2391#ifdef EXTENTS_STATS
2392 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2393 struct ext4_sb_info *sbi = EXT4_SB(sb);
2394 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2395 sbi->s_ext_blocks, sbi->s_ext_extents,
2396 sbi->s_ext_blocks / sbi->s_ext_extents);
2397 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2398 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2399 }
2400#endif
2401}
2402
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002403static void bi_complete(struct bio *bio, int error)
2404{
2405 complete((struct completion *)bio->bi_private);
2406}
2407
2408/* FIXME!! we need to try to merge to left or right after zero-out */
2409static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2410{
2411 int ret = -EIO;
2412 struct bio *bio;
2413 int blkbits, blocksize;
2414 sector_t ee_pblock;
2415 struct completion event;
2416 unsigned int ee_len, len, done, offset;
2417
2418
2419 blkbits = inode->i_blkbits;
2420 blocksize = inode->i_sb->s_blocksize;
2421 ee_len = ext4_ext_get_actual_len(ex);
2422 ee_pblock = ext_pblock(ex);
2423
2424 /* convert ee_pblock to 512 byte sectors */
2425 ee_pblock = ee_pblock << (blkbits - 9);
2426
2427 while (ee_len > 0) {
2428
2429 if (ee_len > BIO_MAX_PAGES)
2430 len = BIO_MAX_PAGES;
2431 else
2432 len = ee_len;
2433
2434 bio = bio_alloc(GFP_NOIO, len);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002435 bio->bi_sector = ee_pblock;
2436 bio->bi_bdev = inode->i_sb->s_bdev;
2437
2438 done = 0;
2439 offset = 0;
2440 while (done < len) {
2441 ret = bio_add_page(bio, ZERO_PAGE(0),
2442 blocksize, offset);
2443 if (ret != blocksize) {
2444 /*
2445 * We can't add any more pages because of
2446 * hardware limitations. Start a new bio.
2447 */
2448 break;
2449 }
2450 done++;
2451 offset += blocksize;
2452 if (offset >= PAGE_CACHE_SIZE)
2453 offset = 0;
2454 }
2455
2456 init_completion(&event);
2457 bio->bi_private = &event;
2458 bio->bi_end_io = bi_complete;
2459 submit_bio(WRITE, bio);
2460 wait_for_completion(&event);
2461
2462 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
2463 ret = 0;
2464 else {
2465 ret = -EIO;
2466 break;
2467 }
2468 bio_put(bio);
2469 ee_len -= done;
2470 ee_pblock += done << (blkbits - 9);
2471 }
2472 return ret;
2473}
2474
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002475#define EXT4_EXT_ZERO_LEN 7
2476
Amit Arora56055d32007-07-17 21:42:38 -04002477/*
2478 * This function is called by ext4_ext_get_blocks() if someone tries to write
2479 * to an uninitialized extent. It may result in splitting the uninitialized
2480 * extent into multiple extents (upto three - one initialized and two
2481 * uninitialized).
2482 * There are three possibilities:
2483 * a> There is no split required: Entire extent should be initialized
2484 * b> Splits in two extents: Write is happening at either end of the extent
2485 * c> Splits in three extents: Somone is writing in middle of the extent
2486 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002487static int ext4_ext_convert_to_initialized(handle_t *handle,
2488 struct inode *inode,
2489 struct ext4_ext_path *path,
2490 ext4_lblk_t iblock,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002491 unsigned int max_blocks)
Amit Arora56055d32007-07-17 21:42:38 -04002492{
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002493 struct ext4_extent *ex, newex, orig_ex;
Amit Arora56055d32007-07-17 21:42:38 -04002494 struct ext4_extent *ex1 = NULL;
2495 struct ext4_extent *ex2 = NULL;
2496 struct ext4_extent *ex3 = NULL;
2497 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002498 ext4_lblk_t ee_block;
2499 unsigned int allocated, ee_len, depth;
Amit Arora56055d32007-07-17 21:42:38 -04002500 ext4_fsblk_t newblock;
2501 int err = 0;
2502 int ret = 0;
2503
2504 depth = ext_depth(inode);
2505 eh = path[depth].p_hdr;
2506 ex = path[depth].p_ext;
2507 ee_block = le32_to_cpu(ex->ee_block);
2508 ee_len = ext4_ext_get_actual_len(ex);
2509 allocated = ee_len - (iblock - ee_block);
2510 newblock = iblock - ee_block + ext_pblock(ex);
2511 ex2 = ex;
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002512 orig_ex.ee_block = ex->ee_block;
2513 orig_ex.ee_len = cpu_to_le16(ee_len);
2514 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
Amit Arora56055d32007-07-17 21:42:38 -04002515
Aneesh Kumar K.V9df56432008-02-22 06:17:31 -05002516 err = ext4_ext_get_access(handle, inode, path + depth);
2517 if (err)
2518 goto out;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002519 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2520 if (ee_len <= 2*EXT4_EXT_ZERO_LEN) {
2521 err = ext4_ext_zeroout(inode, &orig_ex);
2522 if (err)
2523 goto fix_extent_len;
2524 /* update the extent length and mark as initialized */
2525 ex->ee_block = orig_ex.ee_block;
2526 ex->ee_len = orig_ex.ee_len;
2527 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2528 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002529 /* zeroed the full extent */
2530 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002531 }
Aneesh Kumar K.V9df56432008-02-22 06:17:31 -05002532
Amit Arora56055d32007-07-17 21:42:38 -04002533 /* ex1: ee_block to iblock - 1 : uninitialized */
2534 if (iblock > ee_block) {
2535 ex1 = ex;
2536 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2537 ext4_ext_mark_uninitialized(ex1);
2538 ex2 = &newex;
2539 }
2540 /*
2541 * for sanity, update the length of the ex2 extent before
2542 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2543 * overlap of blocks.
2544 */
2545 if (!ex1 && allocated > max_blocks)
2546 ex2->ee_len = cpu_to_le16(max_blocks);
2547 /* ex3: to ee_block + ee_len : uninitialised */
2548 if (allocated > max_blocks) {
2549 unsigned int newdepth;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002550 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
2551 if (allocated <= EXT4_EXT_ZERO_LEN) {
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002552 /*
2553 * iblock == ee_block is handled by the zerouout
2554 * at the beginning.
2555 * Mark first half uninitialized.
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002556 * Mark second half initialized and zero out the
2557 * initialized extent
2558 */
2559 ex->ee_block = orig_ex.ee_block;
2560 ex->ee_len = cpu_to_le16(ee_len - allocated);
2561 ext4_ext_mark_uninitialized(ex);
2562 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2563 ext4_ext_dirty(handle, inode, path + depth);
2564
2565 ex3 = &newex;
2566 ex3->ee_block = cpu_to_le32(iblock);
2567 ext4_ext_store_pblock(ex3, newblock);
2568 ex3->ee_len = cpu_to_le16(allocated);
2569 err = ext4_ext_insert_extent(handle, inode, path, ex3);
2570 if (err == -ENOSPC) {
2571 err = ext4_ext_zeroout(inode, &orig_ex);
2572 if (err)
2573 goto fix_extent_len;
2574 ex->ee_block = orig_ex.ee_block;
2575 ex->ee_len = orig_ex.ee_len;
2576 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2577 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002578 /* blocks available from iblock */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002579 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002580
2581 } else if (err)
2582 goto fix_extent_len;
2583
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002584 /*
2585 * We need to zero out the second half because
2586 * an fallocate request can update file size and
2587 * converting the second half to initialized extent
2588 * implies that we can leak some junk data to user
2589 * space.
2590 */
2591 err = ext4_ext_zeroout(inode, ex3);
2592 if (err) {
2593 /*
2594 * We should actually mark the
2595 * second half as uninit and return error
2596 * Insert would have changed the extent
2597 */
2598 depth = ext_depth(inode);
2599 ext4_ext_drop_refs(path);
2600 path = ext4_ext_find_extent(inode,
2601 iblock, path);
2602 if (IS_ERR(path)) {
2603 err = PTR_ERR(path);
2604 return err;
2605 }
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002606 /* get the second half extent details */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002607 ex = path[depth].p_ext;
2608 err = ext4_ext_get_access(handle, inode,
2609 path + depth);
2610 if (err)
2611 return err;
2612 ext4_ext_mark_uninitialized(ex);
2613 ext4_ext_dirty(handle, inode, path + depth);
2614 return err;
2615 }
2616
2617 /* zeroed the second half */
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002618 return allocated;
2619 }
Amit Arora56055d32007-07-17 21:42:38 -04002620 ex3 = &newex;
2621 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2622 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2623 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2624 ext4_ext_mark_uninitialized(ex3);
2625 err = ext4_ext_insert_extent(handle, inode, path, ex3);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002626 if (err == -ENOSPC) {
2627 err = ext4_ext_zeroout(inode, &orig_ex);
2628 if (err)
2629 goto fix_extent_len;
2630 /* update the extent length and mark as initialized */
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002631 ex->ee_block = orig_ex.ee_block;
2632 ex->ee_len = orig_ex.ee_len;
2633 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002634 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002635 /* zeroed the full extent */
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002636 /* blocks available from iblock */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002637 return allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002638
2639 } else if (err)
2640 goto fix_extent_len;
Amit Arora56055d32007-07-17 21:42:38 -04002641 /*
2642 * The depth, and hence eh & ex might change
2643 * as part of the insert above.
2644 */
2645 newdepth = ext_depth(inode);
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002646 /*
Coly Li73ac36e2009-01-07 18:09:16 -08002647 * update the extent length after successful insert of the
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002648 * split extent
2649 */
2650 orig_ex.ee_len = cpu_to_le16(ee_len -
2651 ext4_ext_get_actual_len(ex3));
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002652 depth = newdepth;
2653 ext4_ext_drop_refs(path);
2654 path = ext4_ext_find_extent(inode, iblock, path);
2655 if (IS_ERR(path)) {
2656 err = PTR_ERR(path);
2657 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04002658 }
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002659 eh = path[depth].p_hdr;
2660 ex = path[depth].p_ext;
2661 if (ex2 != &newex)
2662 ex2 = ex;
2663
2664 err = ext4_ext_get_access(handle, inode, path + depth);
2665 if (err)
2666 goto out;
2667
Amit Arora56055d32007-07-17 21:42:38 -04002668 allocated = max_blocks;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002669
2670 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2671 * to insert a extent in the middle zerout directly
2672 * otherwise give the extent a chance to merge to left
2673 */
2674 if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
2675 iblock != ee_block) {
2676 err = ext4_ext_zeroout(inode, &orig_ex);
2677 if (err)
2678 goto fix_extent_len;
2679 /* update the extent length and mark as initialized */
2680 ex->ee_block = orig_ex.ee_block;
2681 ex->ee_len = orig_ex.ee_len;
2682 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2683 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002684 /* zero out the first half */
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002685 /* blocks available from iblock */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002686 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002687 }
Amit Arora56055d32007-07-17 21:42:38 -04002688 }
2689 /*
2690 * If there was a change of depth as part of the
2691 * insertion of ex3 above, we need to update the length
2692 * of the ex1 extent again here
2693 */
2694 if (ex1 && ex1 != ex) {
2695 ex1 = ex;
2696 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2697 ext4_ext_mark_uninitialized(ex1);
2698 ex2 = &newex;
2699 }
2700 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2701 ex2->ee_block = cpu_to_le32(iblock);
Amit Arora56055d32007-07-17 21:42:38 -04002702 ext4_ext_store_pblock(ex2, newblock);
2703 ex2->ee_len = cpu_to_le16(allocated);
2704 if (ex2 != ex)
2705 goto insert;
Amit Arora56055d32007-07-17 21:42:38 -04002706 /*
2707 * New (initialized) extent starts from the first block
2708 * in the current extent. i.e., ex2 == ex
2709 * We have to see if it can be merged with the extent
2710 * on the left.
2711 */
2712 if (ex2 > EXT_FIRST_EXTENT(eh)) {
2713 /*
2714 * To merge left, pass "ex2 - 1" to try_to_merge(),
2715 * since it merges towards right _only_.
2716 */
2717 ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
2718 if (ret) {
2719 err = ext4_ext_correct_indexes(handle, inode, path);
2720 if (err)
2721 goto out;
2722 depth = ext_depth(inode);
2723 ex2--;
2724 }
2725 }
2726 /*
2727 * Try to Merge towards right. This might be required
2728 * only when the whole extent is being written to.
2729 * i.e. ex2 == ex and ex3 == NULL.
2730 */
2731 if (!ex3) {
2732 ret = ext4_ext_try_to_merge(inode, path, ex2);
2733 if (ret) {
2734 err = ext4_ext_correct_indexes(handle, inode, path);
2735 if (err)
2736 goto out;
2737 }
2738 }
2739 /* Mark modified extent as dirty */
2740 err = ext4_ext_dirty(handle, inode, path + depth);
2741 goto out;
2742insert:
2743 err = ext4_ext_insert_extent(handle, inode, path, &newex);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002744 if (err == -ENOSPC) {
2745 err = ext4_ext_zeroout(inode, &orig_ex);
2746 if (err)
2747 goto fix_extent_len;
2748 /* update the extent length and mark as initialized */
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002749 ex->ee_block = orig_ex.ee_block;
2750 ex->ee_len = orig_ex.ee_len;
2751 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002752 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002753 /* zero out the first half */
2754 return allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002755 } else if (err)
2756 goto fix_extent_len;
Amit Arora56055d32007-07-17 21:42:38 -04002757out:
Mingming553f9002009-09-18 13:34:55 -04002758 ext4_ext_show_leaf(inode, path);
Amit Arora56055d32007-07-17 21:42:38 -04002759 return err ? err : allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002760
2761fix_extent_len:
2762 ex->ee_block = orig_ex.ee_block;
2763 ex->ee_len = orig_ex.ee_len;
2764 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2765 ext4_ext_mark_uninitialized(ex);
2766 ext4_ext_dirty(handle, inode, path + depth);
2767 return err;
Amit Arora56055d32007-07-17 21:42:38 -04002768}
2769
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05002770/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05002771 * Block allocation/map/preallocation routine for extents based files
2772 *
2773 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05002774 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05002775 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
2776 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05002777 *
2778 * return > 0, number of of blocks already mapped/allocated
2779 * if create == 0 and these are pre-allocated blocks
2780 * buffer head is unmapped
2781 * otherwise blocks are mapped
2782 *
2783 * return = 0, if plain look up failed (blocks have not been allocated)
2784 * buffer head is unmapped
2785 *
2786 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05002787 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002788int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002789 ext4_lblk_t iblock,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002790 unsigned int max_blocks, struct buffer_head *bh_result,
Theodore Ts'oc2177052009-05-14 00:58:52 -04002791 int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07002792{
2793 struct ext4_ext_path *path = NULL;
Amit Arora56055d32007-07-17 21:42:38 -04002794 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002795 struct ext4_extent newex, *ex;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002796 ext4_fsblk_t newblock;
2797 int err = 0, depth, ret, cache_type;
2798 unsigned int allocated = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002799 struct ext4_allocation_request ar;
Alex Tomasa86c6182006-10-11 01:21:03 -07002800
2801 __clear_bit(BH_New, &bh_result->b_state);
Mingming84fe3be2009-09-01 08:44:37 -04002802 ext_debug("blocks %u/%u requested for inode %lu\n",
Eric Sandeenbba90742008-01-28 23:58:27 -05002803 iblock, max_blocks, inode->i_ino);
Alex Tomasa86c6182006-10-11 01:21:03 -07002804
2805 /* check in cache */
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002806 cache_type = ext4_ext_in_cache(inode, iblock, &newex);
2807 if (cache_type) {
2808 if (cache_type == EXT4_EXT_CACHE_GAP) {
Theodore Ts'oc2177052009-05-14 00:58:52 -04002809 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04002810 /*
2811 * block isn't allocated yet and
2812 * user doesn't want to allocate it
2813 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002814 goto out2;
2815 }
2816 /* we should allocate requested block */
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002817 } else if (cache_type == EXT4_EXT_CACHE_EXTENT) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002818 /* block is already allocated */
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002819 newblock = iblock
2820 - le32_to_cpu(newex.ee_block)
2821 + ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002822 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05002823 allocated = ext4_ext_get_actual_len(&newex) -
Alex Tomasa86c6182006-10-11 01:21:03 -07002824 (iblock - le32_to_cpu(newex.ee_block));
2825 goto out;
2826 } else {
2827 BUG();
2828 }
2829 }
2830
2831 /* find extent for this block */
2832 path = ext4_ext_find_extent(inode, iblock, NULL);
2833 if (IS_ERR(path)) {
2834 err = PTR_ERR(path);
2835 path = NULL;
2836 goto out2;
2837 }
2838
2839 depth = ext_depth(inode);
2840
2841 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002842 * consistent leaf must not be empty;
2843 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07002844 * this is why assert can't be put in ext4_ext_find_extent()
2845 */
2846 BUG_ON(path[depth].p_ext == NULL && depth != 0);
Amit Arora56055d32007-07-17 21:42:38 -04002847 eh = path[depth].p_hdr;
Alex Tomasa86c6182006-10-11 01:21:03 -07002848
Avantika Mathur7e028972006-12-06 20:41:33 -08002849 ex = path[depth].p_ext;
2850 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002851 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002852 ext4_fsblk_t ee_start = ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002853 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07002854
2855 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07002856 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04002857 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07002858 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04002859 ee_len = ext4_ext_get_actual_len(ex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002860 /* if found extent covers block, simply return it */
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002861 if (iblock >= ee_block && iblock < ee_block + ee_len) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002862 newblock = iblock - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002863 /* number of remaining blocks in the extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07002864 allocated = ee_len - (iblock - ee_block);
Mingming84fe3be2009-09-01 08:44:37 -04002865 ext_debug("%u fit into %u:%d -> %llu\n", iblock,
Alex Tomasa86c6182006-10-11 01:21:03 -07002866 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04002867
Amit Aroraa2df2a62007-07-17 21:42:41 -04002868 /* Do not put uninitialized extent in the cache */
Amit Arora56055d32007-07-17 21:42:38 -04002869 if (!ext4_ext_is_uninitialized(ex)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04002870 ext4_ext_put_in_cache(inode, ee_block,
2871 ee_len, ee_start,
2872 EXT4_EXT_CACHE_EXTENT);
Amit Arora56055d32007-07-17 21:42:38 -04002873 goto out;
2874 }
Theodore Ts'oc2177052009-05-14 00:58:52 -04002875 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
Amit Arora56055d32007-07-17 21:42:38 -04002876 goto out;
Theodore Ts'oc2177052009-05-14 00:58:52 -04002877 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002878 if (allocated > max_blocks)
2879 allocated = max_blocks;
Aneesh Kumar K.Ve067ba02008-04-29 08:11:12 -04002880 /*
2881 * We have blocks reserved already. We
2882 * return allocated blocks so that delalloc
2883 * won't do block reservation for us. But
2884 * the buffer head will be unmapped so that
2885 * a read from the block returns 0s.
2886 */
Eric Sandeen953e6222008-07-11 19:27:31 -04002887 set_buffer_unwritten(bh_result);
Aneesh Kumar K.V9c1ee182009-05-13 18:36:58 -04002888 bh_result->b_bdev = inode->i_sb->s_bdev;
2889 bh_result->b_blocknr = newblock;
Amit Arora56055d32007-07-17 21:42:38 -04002890 goto out2;
Aneesh Kumar K.Ve067ba02008-04-29 08:11:12 -04002891 }
Amit Arora56055d32007-07-17 21:42:38 -04002892
2893 ret = ext4_ext_convert_to_initialized(handle, inode,
2894 path, iblock,
2895 max_blocks);
Dmitry Monakhovdbf9d7d2008-01-28 23:58:27 -05002896 if (ret <= 0) {
2897 err = ret;
Amit Arora56055d32007-07-17 21:42:38 -04002898 goto out2;
Dmitry Monakhovdbf9d7d2008-01-28 23:58:27 -05002899 } else
Amit Arora56055d32007-07-17 21:42:38 -04002900 allocated = ret;
2901 goto outnew;
Alex Tomasa86c6182006-10-11 01:21:03 -07002902 }
2903 }
2904
2905 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002906 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07002907 * we couldn't try to create block if create flag is zero
2908 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04002909 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04002910 /*
2911 * put just found gap into cache to speed up
2912 * subsequent requests
2913 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002914 ext4_ext_put_gap_in_cache(inode, path, iblock);
2915 goto out2;
2916 }
2917 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04002918 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07002919 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002920
Alex Tomasc9de5602008-01-29 00:19:52 -05002921 /* find neighbour allocated blocks */
2922 ar.lleft = iblock;
2923 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
2924 if (err)
2925 goto out2;
2926 ar.lright = iblock;
2927 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
2928 if (err)
2929 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04002930
Amit Arora749269f2007-07-18 09:02:56 -04002931 /*
2932 * See if request is beyond maximum number of blocks we can have in
2933 * a single extent. For an initialized extent this limit is
2934 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
2935 * EXT_UNINIT_MAX_LEN.
2936 */
2937 if (max_blocks > EXT_INIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04002938 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Amit Arora749269f2007-07-18 09:02:56 -04002939 max_blocks = EXT_INIT_MAX_LEN;
2940 else if (max_blocks > EXT_UNINIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04002941 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Amit Arora749269f2007-07-18 09:02:56 -04002942 max_blocks = EXT_UNINIT_MAX_LEN;
2943
Amit Arora25d14f92007-05-24 13:04:13 -04002944 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
2945 newex.ee_block = cpu_to_le32(iblock);
2946 newex.ee_len = cpu_to_le16(max_blocks);
2947 err = ext4_ext_check_overlap(inode, &newex, path);
2948 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05002949 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04002950 else
2951 allocated = max_blocks;
Alex Tomasc9de5602008-01-29 00:19:52 -05002952
2953 /* allocate new block */
2954 ar.inode = inode;
2955 ar.goal = ext4_ext_find_goal(inode, path, iblock);
2956 ar.logical = iblock;
2957 ar.len = allocated;
2958 if (S_ISREG(inode->i_mode))
2959 ar.flags = EXT4_MB_HINT_DATA;
2960 else
2961 /* disable in-core preallocation for non-regular files */
2962 ar.flags = 0;
2963 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07002964 if (!newblock)
2965 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04002966 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002967 ar.goal, newblock, allocated);
Alex Tomasa86c6182006-10-11 01:21:03 -07002968
2969 /* try to insert new extent into found leaf and return */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002970 ext4_ext_store_pblock(&newex, newblock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002971 newex.ee_len = cpu_to_le16(ar.len);
Theodore Ts'oc2177052009-05-14 00:58:52 -04002972 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT) /* Mark uninitialized */
Amit Aroraa2df2a62007-07-17 21:42:41 -04002973 ext4_ext_mark_uninitialized(&newex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002974 err = ext4_ext_insert_extent(handle, inode, path, &newex);
Alex Tomas315054f2007-05-24 13:04:25 -04002975 if (err) {
2976 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05002977 /* not a good idea to call discard here directly,
2978 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04002979 ext4_discard_preallocations(inode);
Alex Tomas315054f2007-05-24 13:04:25 -04002980 ext4_free_blocks(handle, inode, ext_pblock(&newex),
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05002981 ext4_ext_get_actual_len(&newex), 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002982 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04002983 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002984
Alex Tomasa86c6182006-10-11 01:21:03 -07002985 /* previous routine could use block we allocated */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002986 newblock = ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05002987 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora56055d32007-07-17 21:42:38 -04002988outnew:
Eric Sandeen953e6222008-07-11 19:27:31 -04002989 set_buffer_new(bh_result);
Alex Tomasa86c6182006-10-11 01:21:03 -07002990
Amit Aroraa2df2a62007-07-17 21:42:41 -04002991 /* Cache only when it is _not_ an uninitialized extent */
Theodore Ts'oc2177052009-05-14 00:58:52 -04002992 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002993 ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
2994 EXT4_EXT_CACHE_EXTENT);
Alex Tomasa86c6182006-10-11 01:21:03 -07002995out:
2996 if (allocated > max_blocks)
2997 allocated = max_blocks;
2998 ext4_ext_show_leaf(inode, path);
Eric Sandeen953e6222008-07-11 19:27:31 -04002999 set_buffer_mapped(bh_result);
Alex Tomasa86c6182006-10-11 01:21:03 -07003000 bh_result->b_bdev = inode->i_sb->s_bdev;
3001 bh_result->b_blocknr = newblock;
3002out2:
3003 if (path) {
3004 ext4_ext_drop_refs(path);
3005 kfree(path);
3006 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003007 return err ? err : allocated;
3008}
3009
Jan Karacf108bc2008-07-11 19:27:31 -04003010void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07003011{
3012 struct address_space *mapping = inode->i_mapping;
3013 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003014 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07003015 handle_t *handle;
3016 int err = 0;
3017
3018 /*
3019 * probably first extent we're gonna free will be last in block
3020 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04003021 err = ext4_writepage_trans_blocks(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07003022 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04003023 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07003024 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07003025
Jan Karacf108bc2008-07-11 19:27:31 -04003026 if (inode->i_size & (sb->s_blocksize - 1))
3027 ext4_block_truncate_page(handle, mapping, inode->i_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07003028
Jan Kara9ddfc3d2008-07-11 19:27:31 -04003029 if (ext4_orphan_add(handle, inode))
3030 goto out_stop;
3031
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003032 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07003033 ext4_ext_invalidate_cache(inode);
3034
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003035 ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003036
Alex Tomasa86c6182006-10-11 01:21:03 -07003037 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003038 * TODO: optimization is possible here.
3039 * Probably we need not scan at all,
3040 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07003041 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003042
3043 /* we have to know where to truncate from in crash case */
3044 EXT4_I(inode)->i_disksize = inode->i_size;
3045 ext4_mark_inode_dirty(handle, inode);
3046
3047 last_block = (inode->i_size + sb->s_blocksize - 1)
3048 >> EXT4_BLOCK_SIZE_BITS(sb);
3049 err = ext4_ext_remove_space(inode, last_block);
3050
3051 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04003052 * transaction synchronous.
3053 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003054 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05003055 ext4_handle_sync(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07003056
3057out_stop:
Jan Kara9ddfc3d2008-07-11 19:27:31 -04003058 up_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07003059 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003060 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07003061 * then we need to clear up the orphan record which we created above.
3062 * However, if this was a real unlink then we were called by
3063 * ext4_delete_inode(), and we allow that function to clean up the
3064 * orphan info for us.
3065 */
3066 if (inode->i_nlink)
3067 ext4_orphan_del(handle, inode);
3068
Solofo Ramangalahyef737722008-04-29 22:00:41 -04003069 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3070 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07003071 ext4_journal_stop(handle);
3072}
3073
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003074static void ext4_falloc_update_inode(struct inode *inode,
3075 int mode, loff_t new_size, int update_ctime)
3076{
3077 struct timespec now;
3078
3079 if (update_ctime) {
3080 now = current_fs_time(inode->i_sb);
3081 if (!timespec_equal(&inode->i_ctime, &now))
3082 inode->i_ctime = now;
3083 }
3084 /*
3085 * Update only when preallocation was requested beyond
3086 * the file size.
3087 */
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003088 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3089 if (new_size > i_size_read(inode))
3090 i_size_write(inode, new_size);
3091 if (new_size > EXT4_I(inode)->i_disksize)
3092 ext4_update_i_disksize(inode, new_size);
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003093 }
3094
3095}
3096
Amit Aroraa2df2a62007-07-17 21:42:41 -04003097/*
3098 * preallocate space for a file. This implements ext4's fallocate inode
3099 * operation, which gets called from sys_fallocate system call.
3100 * For block-mapped files, posix_fallocate should fall back to the method
3101 * of writing zeroes to the required new blocks (the same behavior which is
3102 * expected for file systems which do not support fallocate() system call).
3103 */
3104long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
3105{
3106 handle_t *handle;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003107 ext4_lblk_t block;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003108 loff_t new_size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003109 unsigned int max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003110 int ret = 0;
3111 int ret2 = 0;
3112 int retries = 0;
3113 struct buffer_head map_bh;
3114 unsigned int credits, blkbits = inode->i_blkbits;
3115
3116 /*
3117 * currently supporting (pre)allocate mode for extent-based
3118 * files _only_
3119 */
3120 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
3121 return -EOPNOTSUPP;
3122
3123 /* preallocation to directories is currently not supported */
3124 if (S_ISDIR(inode->i_mode))
3125 return -ENODEV;
3126
3127 block = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003128 /*
3129 * We can't just convert len to max_blocks because
3130 * If blocksize = 4096 offset = 3072 and len = 2048
3131 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003132 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003133 - block;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003134 /*
Mingming Caof3bd1f32008-08-19 22:16:03 -04003135 * credits to insert 1 extent into extent tree
Amit Aroraa2df2a62007-07-17 21:42:41 -04003136 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04003137 credits = ext4_chunk_trans_blocks(inode, max_blocks);
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05003138 mutex_lock(&inode->i_mutex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003139retry:
3140 while (ret >= 0 && ret < max_blocks) {
3141 block = block + ret;
3142 max_blocks = max_blocks - ret;
3143 handle = ext4_journal_start(inode, credits);
3144 if (IS_ERR(handle)) {
3145 ret = PTR_ERR(handle);
3146 break;
3147 }
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04003148 map_bh.b_state = 0;
Theodore Ts'o12b7ac12009-05-14 00:57:44 -04003149 ret = ext4_get_blocks(handle, inode, block,
3150 max_blocks, &map_bh,
Theodore Ts'oc2177052009-05-14 00:58:52 -04003151 EXT4_GET_BLOCKS_CREATE_UNINIT_EXT);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05003152 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003153#ifdef EXT4FS_DEBUG
3154 WARN_ON(ret <= 0);
3155 printk(KERN_ERR "%s: ext4_ext_get_blocks "
3156 "returned error inode#%lu, block=%u, "
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05003157 "max_blocks=%u", __func__,
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05003158 inode->i_ino, block, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003159#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04003160 ext4_mark_inode_dirty(handle, inode);
3161 ret2 = ext4_journal_stop(handle);
3162 break;
3163 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003164 if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
3165 blkbits) >> blkbits))
3166 new_size = offset + len;
3167 else
3168 new_size = (block + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003169
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003170 ext4_falloc_update_inode(inode, mode, new_size,
3171 buffer_new(&map_bh));
Amit Aroraa2df2a62007-07-17 21:42:41 -04003172 ext4_mark_inode_dirty(handle, inode);
3173 ret2 = ext4_journal_stop(handle);
3174 if (ret2)
3175 break;
3176 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003177 if (ret == -ENOSPC &&
3178 ext4_should_retry_alloc(inode->i_sb, &retries)) {
3179 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003180 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003181 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05003182 mutex_unlock(&inode->i_mutex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003183 return ret > 0 ? ret2 : ret;
3184}
Eric Sandeen6873fa02008-10-07 00:46:36 -04003185
3186/*
3187 * Callback function called for each extent to gather FIEMAP information.
3188 */
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003189static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path,
Eric Sandeen6873fa02008-10-07 00:46:36 -04003190 struct ext4_ext_cache *newex, struct ext4_extent *ex,
3191 void *data)
3192{
3193 struct fiemap_extent_info *fieinfo = data;
Eric Sandeenc9877b22009-05-01 23:32:06 -04003194 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04003195 __u64 logical;
3196 __u64 physical;
3197 __u64 length;
3198 __u32 flags = 0;
3199 int error;
3200
3201 logical = (__u64)newex->ec_block << blksize_bits;
3202
3203 if (newex->ec_type == EXT4_EXT_CACHE_GAP) {
3204 pgoff_t offset;
3205 struct page *page;
3206 struct buffer_head *bh = NULL;
3207
3208 offset = logical >> PAGE_SHIFT;
3209 page = find_get_page(inode->i_mapping, offset);
3210 if (!page || !page_has_buffers(page))
3211 return EXT_CONTINUE;
3212
3213 bh = page_buffers(page);
3214
3215 if (!bh)
3216 return EXT_CONTINUE;
3217
3218 if (buffer_delay(bh)) {
3219 flags |= FIEMAP_EXTENT_DELALLOC;
3220 page_cache_release(page);
3221 } else {
3222 page_cache_release(page);
3223 return EXT_CONTINUE;
3224 }
3225 }
3226
3227 physical = (__u64)newex->ec_start << blksize_bits;
3228 length = (__u64)newex->ec_len << blksize_bits;
3229
3230 if (ex && ext4_ext_is_uninitialized(ex))
3231 flags |= FIEMAP_EXTENT_UNWRITTEN;
3232
3233 /*
3234 * If this extent reaches EXT_MAX_BLOCK, it must be last.
3235 *
3236 * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
3237 * this also indicates no more allocated blocks.
3238 *
3239 * XXX this might miss a single-block extent at EXT_MAX_BLOCK
3240 */
Eric Sandeenc9877b22009-05-01 23:32:06 -04003241 if (ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK ||
Theodore Ts'oeefd7f02009-05-02 19:05:37 -04003242 newex->ec_block + newex->ec_len - 1 == EXT_MAX_BLOCK) {
3243 loff_t size = i_size_read(inode);
3244 loff_t bs = EXT4_BLOCK_SIZE(inode->i_sb);
3245
Eric Sandeen6873fa02008-10-07 00:46:36 -04003246 flags |= FIEMAP_EXTENT_LAST;
Theodore Ts'oeefd7f02009-05-02 19:05:37 -04003247 if ((flags & FIEMAP_EXTENT_DELALLOC) &&
3248 logical+length > size)
3249 length = (size - logical + bs - 1) & ~(bs-1);
3250 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04003251
3252 error = fiemap_fill_next_extent(fieinfo, logical, physical,
3253 length, flags);
3254 if (error < 0)
3255 return error;
3256 if (error == 1)
3257 return EXT_BREAK;
3258
3259 return EXT_CONTINUE;
3260}
3261
3262/* fiemap flags we can handle specified here */
3263#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
3264
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003265static int ext4_xattr_fiemap(struct inode *inode,
3266 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04003267{
3268 __u64 physical = 0;
3269 __u64 length;
3270 __u32 flags = FIEMAP_EXTENT_LAST;
3271 int blockbits = inode->i_sb->s_blocksize_bits;
3272 int error = 0;
3273
3274 /* in-inode? */
3275 if (EXT4_I(inode)->i_state & EXT4_STATE_XATTR) {
3276 struct ext4_iloc iloc;
3277 int offset; /* offset of xattr in inode */
3278
3279 error = ext4_get_inode_loc(inode, &iloc);
3280 if (error)
3281 return error;
3282 physical = iloc.bh->b_blocknr << blockbits;
3283 offset = EXT4_GOOD_OLD_INODE_SIZE +
3284 EXT4_I(inode)->i_extra_isize;
3285 physical += offset;
3286 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
3287 flags |= FIEMAP_EXTENT_DATA_INLINE;
3288 } else { /* external block */
3289 physical = EXT4_I(inode)->i_file_acl << blockbits;
3290 length = inode->i_sb->s_blocksize;
3291 }
3292
3293 if (physical)
3294 error = fiemap_fill_next_extent(fieinfo, 0, physical,
3295 length, flags);
3296 return (error < 0 ? error : 0);
3297}
3298
3299int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3300 __u64 start, __u64 len)
3301{
3302 ext4_lblk_t start_blk;
3303 ext4_lblk_t len_blks;
3304 int error = 0;
3305
3306 /* fallback to generic here if not in extents fmt */
3307 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
3308 return generic_block_fiemap(inode, fieinfo, start, len,
3309 ext4_get_block);
3310
3311 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
3312 return -EBADR;
3313
3314 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
3315 error = ext4_xattr_fiemap(inode, fieinfo);
3316 } else {
3317 start_blk = start >> inode->i_sb->s_blocksize_bits;
3318 len_blks = len >> inode->i_sb->s_blocksize_bits;
3319
3320 /*
3321 * Walk the extent tree gathering extent information.
3322 * ext4_ext_fiemap_cb will push extents back to user.
3323 */
Theodore Ts'o0568c512009-05-17 23:31:23 -04003324 down_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04003325 error = ext4_ext_walk_space(inode, start_blk, len_blks,
3326 ext4_ext_fiemap_cb, fieinfo);
Theodore Ts'o0568c512009-05-17 23:31:23 -04003327 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04003328 }
3329
3330 return error;
3331}
3332