blob: ac77d8b8251da9cb6ac720db55e8b7e72ec97a91 [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 */
Avantika Mathur09b88252006-12-06 20:41:36 -080052static ext4_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{
329 ext4_fsblk_t block = ext_pblock(ext);
330 int len = ext4_ext_get_actual_len(ext);
331 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
332 if (unlikely(block < le32_to_cpu(es->s_first_data_block) ||
333 ((block + len) > ext4_blocks_count(es))))
334 return 0;
335 else
336 return 1;
337}
338
339static int ext4_valid_extent_idx(struct inode *inode,
340 struct ext4_extent_idx *ext_idx)
341{
342 ext4_fsblk_t block = idx_pblock(ext_idx);
343 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
344 if (unlikely(block < le32_to_cpu(es->s_first_data_block) ||
345 (block > ext4_blocks_count(es))))
346 return 0;
347 else
348 return 1;
349}
350
351static int ext4_valid_extent_entries(struct inode *inode,
352 struct ext4_extent_header *eh,
353 int depth)
354{
355 struct ext4_extent *ext;
356 struct ext4_extent_idx *ext_idx;
357 unsigned short entries;
358 if (eh->eh_entries == 0)
359 return 1;
360
361 entries = le16_to_cpu(eh->eh_entries);
362
363 if (depth == 0) {
364 /* leaf entries */
365 ext = EXT_FIRST_EXTENT(eh);
366 while (entries) {
367 if (!ext4_valid_extent(inode, ext))
368 return 0;
369 ext++;
370 entries--;
371 }
372 } else {
373 ext_idx = EXT_FIRST_INDEX(eh);
374 while (entries) {
375 if (!ext4_valid_extent_idx(inode, ext_idx))
376 return 0;
377 ext_idx++;
378 entries--;
379 }
380 }
381 return 1;
382}
383
384static int __ext4_ext_check(const char *function, struct inode *inode,
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400385 struct ext4_extent_header *eh,
386 int depth)
387{
388 const char *error_msg;
389 int max = 0;
390
391 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
392 error_msg = "invalid magic";
393 goto corrupted;
394 }
395 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
396 error_msg = "unexpected eh_depth";
397 goto corrupted;
398 }
399 if (unlikely(eh->eh_max == 0)) {
400 error_msg = "invalid eh_max";
401 goto corrupted;
402 }
403 max = ext4_ext_max_entries(inode, depth);
404 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
405 error_msg = "too large eh_max";
406 goto corrupted;
407 }
408 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
409 error_msg = "invalid eh_entries";
410 goto corrupted;
411 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400412 if (!ext4_valid_extent_entries(inode, eh, depth)) {
413 error_msg = "invalid extent entries";
414 goto corrupted;
415 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400416 return 0;
417
418corrupted:
419 ext4_error(inode->i_sb, function,
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400420 "bad header/extent in inode #%lu: %s - magic %x, "
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400421 "entries %u, max %u(%u), depth %u(%u)",
422 inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
423 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
424 max, le16_to_cpu(eh->eh_depth), depth);
425
426 return -EIO;
427}
428
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400429#define ext4_ext_check(inode, eh, depth) \
430 __ext4_ext_check(__func__, inode, eh, depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400431
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400432int ext4_ext_check_inode(struct inode *inode)
433{
434 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
435}
436
Alex Tomasa86c6182006-10-11 01:21:03 -0700437#ifdef EXT_DEBUG
438static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
439{
440 int k, l = path->p_depth;
441
442 ext_debug("path:");
443 for (k = 0; k <= l; k++, path++) {
444 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700445 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700446 idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700447 } else if (path->p_ext) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700448 ext_debug(" %d:%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700449 le32_to_cpu(path->p_ext->ee_block),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400450 ext4_ext_get_actual_len(path->p_ext),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700451 ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700452 } else
453 ext_debug(" []");
454 }
455 ext_debug("\n");
456}
457
458static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
459{
460 int depth = ext_depth(inode);
461 struct ext4_extent_header *eh;
462 struct ext4_extent *ex;
463 int i;
464
465 if (!path)
466 return;
467
468 eh = path[depth].p_hdr;
469 ex = EXT_FIRST_EXTENT(eh);
470
471 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700472 ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400473 ext4_ext_get_actual_len(ex), ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700474 }
475 ext_debug("\n");
476}
477#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400478#define ext4_ext_show_path(inode, path)
479#define ext4_ext_show_leaf(inode, path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700480#endif
481
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500482void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700483{
484 int depth = path->p_depth;
485 int i;
486
487 for (i = 0; i <= depth; i++, path++)
488 if (path->p_bh) {
489 brelse(path->p_bh);
490 path->p_bh = NULL;
491 }
492}
493
494/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700495 * ext4_ext_binsearch_idx:
496 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400497 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700498 */
499static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500500ext4_ext_binsearch_idx(struct inode *inode,
501 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700502{
503 struct ext4_extent_header *eh = path->p_hdr;
504 struct ext4_extent_idx *r, *l, *m;
505
Alex Tomasa86c6182006-10-11 01:21:03 -0700506
Eric Sandeenbba90742008-01-28 23:58:27 -0500507 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700508
509 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400510 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700511 while (l <= r) {
512 m = l + (r - l) / 2;
513 if (block < le32_to_cpu(m->ei_block))
514 r = m - 1;
515 else
516 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400517 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
518 m, le32_to_cpu(m->ei_block),
519 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700520 }
521
522 path->p_idx = l - 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700523 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400524 idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700525
526#ifdef CHECK_BINSEARCH
527 {
528 struct ext4_extent_idx *chix, *ix;
529 int k;
530
531 chix = ix = EXT_FIRST_INDEX(eh);
532 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
533 if (k != 0 &&
534 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o47760042008-09-08 23:00:52 -0400535 printk(KERN_DEBUG "k=%d, ix=0x%p, "
536 "first=0x%p\n", k,
537 ix, EXT_FIRST_INDEX(eh));
538 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700539 le32_to_cpu(ix->ei_block),
540 le32_to_cpu(ix[-1].ei_block));
541 }
542 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400543 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700544 if (block < le32_to_cpu(ix->ei_block))
545 break;
546 chix = ix;
547 }
548 BUG_ON(chix != path->p_idx);
549 }
550#endif
551
552}
553
554/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700555 * ext4_ext_binsearch:
556 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400557 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700558 */
559static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500560ext4_ext_binsearch(struct inode *inode,
561 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700562{
563 struct ext4_extent_header *eh = path->p_hdr;
564 struct ext4_extent *r, *l, *m;
565
Alex Tomasa86c6182006-10-11 01:21:03 -0700566 if (eh->eh_entries == 0) {
567 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700568 * this leaf is empty:
569 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700570 */
571 return;
572 }
573
Eric Sandeenbba90742008-01-28 23:58:27 -0500574 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700575
576 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400577 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700578
579 while (l <= r) {
580 m = l + (r - l) / 2;
581 if (block < le32_to_cpu(m->ee_block))
582 r = m - 1;
583 else
584 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400585 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
586 m, le32_to_cpu(m->ee_block),
587 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700588 }
589
590 path->p_ext = l - 1;
Mingming Cao2ae02102006-10-11 01:21:11 -0700591 ext_debug(" -> %d:%llu:%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400592 le32_to_cpu(path->p_ext->ee_block),
593 ext_pblock(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400594 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700595
596#ifdef CHECK_BINSEARCH
597 {
598 struct ext4_extent *chex, *ex;
599 int k;
600
601 chex = ex = EXT_FIRST_EXTENT(eh);
602 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
603 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400604 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700605 if (block < le32_to_cpu(ex->ee_block))
606 break;
607 chex = ex;
608 }
609 BUG_ON(chex != path->p_ext);
610 }
611#endif
612
613}
614
615int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
616{
617 struct ext4_extent_header *eh;
618
619 eh = ext_inode_hdr(inode);
620 eh->eh_depth = 0;
621 eh->eh_entries = 0;
622 eh->eh_magic = EXT4_EXT_MAGIC;
623 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
624 ext4_mark_inode_dirty(handle, inode);
625 ext4_ext_invalidate_cache(inode);
626 return 0;
627}
628
629struct ext4_ext_path *
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500630ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
631 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700632{
633 struct ext4_extent_header *eh;
634 struct buffer_head *bh;
635 short int depth, i, ppos = 0, alloc = 0;
636
637 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400638 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700639
640 /* account possible depth increase */
641 if (!path) {
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800642 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700643 GFP_NOFS);
644 if (!path)
645 return ERR_PTR(-ENOMEM);
646 alloc = 1;
647 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700648 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400649 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700650
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400651 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700652 /* walk through the tree */
653 while (i) {
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400654 int need_to_validate = 0;
655
Alex Tomasa86c6182006-10-11 01:21:03 -0700656 ext_debug("depth %d: num %d, max %d\n",
657 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400658
Alex Tomasa86c6182006-10-11 01:21:03 -0700659 ext4_ext_binsearch_idx(inode, path + ppos, block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700660 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700661 path[ppos].p_depth = i;
662 path[ppos].p_ext = NULL;
663
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400664 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
665 if (unlikely(!bh))
Alex Tomasa86c6182006-10-11 01:21:03 -0700666 goto err;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400667 if (!bh_uptodate_or_lock(bh)) {
668 if (bh_submit_read(bh) < 0) {
669 put_bh(bh);
670 goto err;
671 }
672 /* validate the extent entries */
673 need_to_validate = 1;
674 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700675 eh = ext_block_hdr(bh);
676 ppos++;
677 BUG_ON(ppos > depth);
678 path[ppos].p_bh = bh;
679 path[ppos].p_hdr = eh;
680 i--;
681
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400682 if (need_to_validate && ext4_ext_check(inode, eh, i))
Alex Tomasa86c6182006-10-11 01:21:03 -0700683 goto err;
684 }
685
686 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700687 path[ppos].p_ext = NULL;
688 path[ppos].p_idx = NULL;
689
Alex Tomasa86c6182006-10-11 01:21:03 -0700690 /* find extent */
691 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400692 /* if not an empty leaf */
693 if (path[ppos].p_ext)
694 path[ppos].p_block = ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700695
696 ext4_ext_show_path(inode, path);
697
698 return path;
699
700err:
701 ext4_ext_drop_refs(path);
702 if (alloc)
703 kfree(path);
704 return ERR_PTR(-EIO);
705}
706
707/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700708 * ext4_ext_insert_index:
709 * insert new index [@logical;@ptr] into the block at @curp;
710 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700711 */
712static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
713 struct ext4_ext_path *curp,
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700714 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700715{
716 struct ext4_extent_idx *ix;
717 int len, err;
718
Avantika Mathur7e028972006-12-06 20:41:33 -0800719 err = ext4_ext_get_access(handle, inode, curp);
720 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700721 return err;
722
723 BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
724 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
725 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
726 /* insert after */
727 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
728 len = (len - 1) * sizeof(struct ext4_extent_idx);
729 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400730 ext_debug("insert new index %d after: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700731 "move %d from 0x%p to 0x%p\n",
732 logical, ptr, len,
733 (curp->p_idx + 1), (curp->p_idx + 2));
734 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
735 }
736 ix = curp->p_idx + 1;
737 } else {
738 /* insert before */
739 len = len * sizeof(struct ext4_extent_idx);
740 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400741 ext_debug("insert new index %d before: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700742 "move %d from 0x%p to 0x%p\n",
743 logical, ptr, len,
744 curp->p_idx, (curp->p_idx + 1));
745 memmove(curp->p_idx + 1, curp->p_idx, len);
746 ix = curp->p_idx;
747 }
748
749 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700750 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400751 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700752
753 BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400754 > le16_to_cpu(curp->p_hdr->eh_max));
Alex Tomasa86c6182006-10-11 01:21:03 -0700755 BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
756
757 err = ext4_ext_dirty(handle, inode, curp);
758 ext4_std_error(inode->i_sb, err);
759
760 return err;
761}
762
763/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700764 * ext4_ext_split:
765 * inserts new subtree into the path, using free index entry
766 * at depth @at:
767 * - allocates all needed blocks (new leaf and all intermediate index blocks)
768 * - makes decision where to split
769 * - moves remaining extents and index entries (right to the split point)
770 * into the newly allocated blocks
771 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700772 */
773static int ext4_ext_split(handle_t *handle, struct inode *inode,
774 struct ext4_ext_path *path,
775 struct ext4_extent *newext, int at)
776{
777 struct buffer_head *bh = NULL;
778 int depth = ext_depth(inode);
779 struct ext4_extent_header *neh;
780 struct ext4_extent_idx *fidx;
781 struct ext4_extent *ex;
782 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700783 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700784 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700785 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700786 int err = 0;
787
788 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700789 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700790
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700791 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700792 * border from split point */
793 BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
794 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
795 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700796 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700797 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400798 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700799 } else {
800 border = newext->ee_block;
801 ext_debug("leaf will be added."
802 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400803 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700804 }
805
806 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700807 * If error occurs, then we break processing
808 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700809 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700810 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700811 */
812
813 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700814 * Get array to track all allocated blocks.
815 * We need this to handle errors and free blocks
816 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700817 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800818 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700819 if (!ablocks)
820 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700821
822 /* allocate all needed blocks */
823 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
824 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400825 newblock = ext4_ext_new_meta_block(handle, inode, path,
826 newext, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700827 if (newblock == 0)
828 goto cleanup;
829 ablocks[a] = newblock;
830 }
831
832 /* initialize new leaf */
833 newblock = ablocks[--a];
834 BUG_ON(newblock == 0);
835 bh = sb_getblk(inode->i_sb, newblock);
836 if (!bh) {
837 err = -EIO;
838 goto cleanup;
839 }
840 lock_buffer(bh);
841
Avantika Mathur7e028972006-12-06 20:41:33 -0800842 err = ext4_journal_get_create_access(handle, bh);
843 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700844 goto cleanup;
845
846 neh = ext_block_hdr(bh);
847 neh->eh_entries = 0;
848 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
849 neh->eh_magic = EXT4_EXT_MAGIC;
850 neh->eh_depth = 0;
851 ex = EXT_FIRST_EXTENT(neh);
852
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700853 /* move remainder of path[depth] to the new leaf */
Alex Tomasa86c6182006-10-11 01:21:03 -0700854 BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
855 /* start copy from next extent */
856 /* TODO: we could do it by single memmove */
857 m = 0;
858 path[depth].p_ext++;
859 while (path[depth].p_ext <=
860 EXT_MAX_EXTENT(path[depth].p_hdr)) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700861 ext_debug("move %d:%llu:%d in new leaf %llu\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400862 le32_to_cpu(path[depth].p_ext->ee_block),
863 ext_pblock(path[depth].p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400864 ext4_ext_get_actual_len(path[depth].p_ext),
Alex Tomasa86c6182006-10-11 01:21:03 -0700865 newblock);
866 /*memmove(ex++, path[depth].p_ext++,
867 sizeof(struct ext4_extent));
868 neh->eh_entries++;*/
869 path[depth].p_ext++;
870 m++;
871 }
872 if (m) {
873 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400874 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700875 }
876
877 set_buffer_uptodate(bh);
878 unlock_buffer(bh);
879
Frank Mayhar03901312009-01-07 00:06:22 -0500880 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800881 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700882 goto cleanup;
883 brelse(bh);
884 bh = NULL;
885
886 /* correct old leaf */
887 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800888 err = ext4_ext_get_access(handle, inode, path + depth);
889 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700890 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400891 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800892 err = ext4_ext_dirty(handle, inode, path + depth);
893 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700894 goto cleanup;
895
896 }
897
898 /* create intermediate indexes */
899 k = depth - at - 1;
900 BUG_ON(k < 0);
901 if (k)
902 ext_debug("create %d intermediate indices\n", k);
903 /* insert new index into current index block */
904 /* current depth stored in i var */
905 i = depth - 1;
906 while (k--) {
907 oldblock = newblock;
908 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -0500909 bh = sb_getblk(inode->i_sb, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700910 if (!bh) {
911 err = -EIO;
912 goto cleanup;
913 }
914 lock_buffer(bh);
915
Avantika Mathur7e028972006-12-06 20:41:33 -0800916 err = ext4_journal_get_create_access(handle, bh);
917 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700918 goto cleanup;
919
920 neh = ext_block_hdr(bh);
921 neh->eh_entries = cpu_to_le16(1);
922 neh->eh_magic = EXT4_EXT_MAGIC;
923 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
924 neh->eh_depth = cpu_to_le16(depth - i);
925 fidx = EXT_FIRST_INDEX(neh);
926 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700927 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700928
Eric Sandeenbba90742008-01-28 23:58:27 -0500929 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
930 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700931 /* copy indexes */
932 m = 0;
933 path[i].p_idx++;
934
935 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
936 EXT_MAX_INDEX(path[i].p_hdr));
937 BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
938 EXT_LAST_INDEX(path[i].p_hdr));
939 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400940 ext_debug("%d: move %d:%llu in new index %llu\n", i,
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400941 le32_to_cpu(path[i].p_idx->ei_block),
942 idx_pblock(path[i].p_idx),
943 newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700944 /*memmove(++fidx, path[i].p_idx++,
945 sizeof(struct ext4_extent_idx));
946 neh->eh_entries++;
947 BUG_ON(neh->eh_entries > neh->eh_max);*/
948 path[i].p_idx++;
949 m++;
950 }
951 if (m) {
952 memmove(++fidx, path[i].p_idx - m,
953 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400954 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700955 }
956 set_buffer_uptodate(bh);
957 unlock_buffer(bh);
958
Frank Mayhar03901312009-01-07 00:06:22 -0500959 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800960 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700961 goto cleanup;
962 brelse(bh);
963 bh = NULL;
964
965 /* correct old index */
966 if (m) {
967 err = ext4_ext_get_access(handle, inode, path + i);
968 if (err)
969 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400970 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700971 err = ext4_ext_dirty(handle, inode, path + i);
972 if (err)
973 goto cleanup;
974 }
975
976 i--;
977 }
978
979 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -0700980 err = ext4_ext_insert_index(handle, inode, path + at,
981 le32_to_cpu(border), newblock);
982
983cleanup:
984 if (bh) {
985 if (buffer_locked(bh))
986 unlock_buffer(bh);
987 brelse(bh);
988 }
989
990 if (err) {
991 /* free all allocated blocks in error case */
992 for (i = 0; i < depth; i++) {
993 if (!ablocks[i])
994 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -0500995 ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700996 }
997 }
998 kfree(ablocks);
999
1000 return err;
1001}
1002
1003/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001004 * ext4_ext_grow_indepth:
1005 * implements tree growing procedure:
1006 * - allocates new block
1007 * - moves top-level data (index block or leaf) into the new block
1008 * - initializes new top-level, creating index that points to the
1009 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001010 */
1011static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
1012 struct ext4_ext_path *path,
1013 struct ext4_extent *newext)
1014{
1015 struct ext4_ext_path *curp = path;
1016 struct ext4_extent_header *neh;
1017 struct ext4_extent_idx *fidx;
1018 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001019 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001020 int err = 0;
1021
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -04001022 newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07001023 if (newblock == 0)
1024 return err;
1025
1026 bh = sb_getblk(inode->i_sb, newblock);
1027 if (!bh) {
1028 err = -EIO;
1029 ext4_std_error(inode->i_sb, err);
1030 return err;
1031 }
1032 lock_buffer(bh);
1033
Avantika Mathur7e028972006-12-06 20:41:33 -08001034 err = ext4_journal_get_create_access(handle, bh);
1035 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001036 unlock_buffer(bh);
1037 goto out;
1038 }
1039
1040 /* move top-level index/leaf into new block */
1041 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
1042
1043 /* set size of new block */
1044 neh = ext_block_hdr(bh);
1045 /* old root could have indexes or leaves
1046 * so calculate e_max right way */
1047 if (ext_depth(inode))
1048 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
1049 else
1050 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
1051 neh->eh_magic = EXT4_EXT_MAGIC;
1052 set_buffer_uptodate(bh);
1053 unlock_buffer(bh);
1054
Frank Mayhar03901312009-01-07 00:06:22 -05001055 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001056 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001057 goto out;
1058
1059 /* create index in new top-level index: num,max,pointer */
Avantika Mathur7e028972006-12-06 20:41:33 -08001060 err = ext4_ext_get_access(handle, inode, curp);
1061 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001062 goto out;
1063
1064 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
1065 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
1066 curp->p_hdr->eh_entries = cpu_to_le16(1);
1067 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
Dmitry Monakhove9f410b2007-07-18 09:09:15 -04001068
1069 if (path[0].p_hdr->eh_depth)
1070 curp->p_idx->ei_block =
1071 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
1072 else
1073 curp->p_idx->ei_block =
1074 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001075 ext4_idx_store_pblock(curp->p_idx, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001076
1077 neh = ext_inode_hdr(inode);
1078 fidx = EXT_FIRST_INDEX(neh);
Mingming Cao2ae02102006-10-11 01:21:11 -07001079 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001080 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001081 le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
Alex Tomasa86c6182006-10-11 01:21:03 -07001082
1083 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
1084 err = ext4_ext_dirty(handle, inode, curp);
1085out:
1086 brelse(bh);
1087
1088 return err;
1089}
1090
1091/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001092 * ext4_ext_create_new_leaf:
1093 * finds empty index and adds new leaf.
1094 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001095 */
1096static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
1097 struct ext4_ext_path *path,
1098 struct ext4_extent *newext)
1099{
1100 struct ext4_ext_path *curp;
1101 int depth, i, err = 0;
1102
1103repeat:
1104 i = depth = ext_depth(inode);
1105
1106 /* walk up to the tree and look for free index entry */
1107 curp = path + depth;
1108 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1109 i--;
1110 curp--;
1111 }
1112
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001113 /* we use already allocated block for index block,
1114 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001115 if (EXT_HAS_FREE_INDEX(curp)) {
1116 /* if we found index with free entry, then use that
1117 * entry: create all needed subtree and add new leaf */
1118 err = ext4_ext_split(handle, inode, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001119 if (err)
1120 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001121
1122 /* refill path */
1123 ext4_ext_drop_refs(path);
1124 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001125 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1126 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001127 if (IS_ERR(path))
1128 err = PTR_ERR(path);
1129 } else {
1130 /* tree is full, time to grow in depth */
1131 err = ext4_ext_grow_indepth(handle, inode, path, newext);
1132 if (err)
1133 goto out;
1134
1135 /* refill path */
1136 ext4_ext_drop_refs(path);
1137 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001138 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1139 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001140 if (IS_ERR(path)) {
1141 err = PTR_ERR(path);
1142 goto out;
1143 }
1144
1145 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001146 * only first (depth 0 -> 1) produces free space;
1147 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001148 */
1149 depth = ext_depth(inode);
1150 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001151 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001152 goto repeat;
1153 }
1154 }
1155
1156out:
1157 return err;
1158}
1159
1160/*
Alex Tomas1988b512008-01-28 23:58:27 -05001161 * search the closest allocated block to the left for *logical
1162 * and returns it at @logical + it's physical address at @phys
1163 * if *logical is the smallest allocated block, the function
1164 * returns 0 at @phys
1165 * return value contains 0 (success) or error code
1166 */
1167int
1168ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
1169 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1170{
1171 struct ext4_extent_idx *ix;
1172 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001173 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001174
1175 BUG_ON(path == NULL);
1176 depth = path->p_depth;
1177 *phys = 0;
1178
1179 if (depth == 0 && path->p_ext == NULL)
1180 return 0;
1181
1182 /* usually extent in the path covers blocks smaller
1183 * then *logical, but it can be that extent is the
1184 * first one in the file */
1185
1186 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001187 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001188 if (*logical < le32_to_cpu(ex->ee_block)) {
1189 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1190 while (--depth >= 0) {
1191 ix = path[depth].p_idx;
1192 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1193 }
1194 return 0;
1195 }
1196
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001197 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
Alex Tomas1988b512008-01-28 23:58:27 -05001198
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001199 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1200 *phys = ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001201 return 0;
1202}
1203
1204/*
1205 * search the closest allocated block to the right for *logical
1206 * and returns it at @logical + it's physical address at @phys
1207 * if *logical is the smallest allocated block, the function
1208 * returns 0 at @phys
1209 * return value contains 0 (success) or error code
1210 */
1211int
1212ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
1213 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1214{
1215 struct buffer_head *bh = NULL;
1216 struct ext4_extent_header *eh;
1217 struct ext4_extent_idx *ix;
1218 struct ext4_extent *ex;
1219 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001220 int depth; /* Note, NOT eh_depth; depth from top of tree */
1221 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001222
1223 BUG_ON(path == NULL);
1224 depth = path->p_depth;
1225 *phys = 0;
1226
1227 if (depth == 0 && path->p_ext == NULL)
1228 return 0;
1229
1230 /* usually extent in the path covers blocks smaller
1231 * then *logical, but it can be that extent is the
1232 * first one in the file */
1233
1234 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001235 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001236 if (*logical < le32_to_cpu(ex->ee_block)) {
1237 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1238 while (--depth >= 0) {
1239 ix = path[depth].p_idx;
1240 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1241 }
1242 *logical = le32_to_cpu(ex->ee_block);
1243 *phys = ext_pblock(ex);
1244 return 0;
1245 }
1246
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001247 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
Alex Tomas1988b512008-01-28 23:58:27 -05001248
1249 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1250 /* next allocated block in this leaf */
1251 ex++;
1252 *logical = le32_to_cpu(ex->ee_block);
1253 *phys = ext_pblock(ex);
1254 return 0;
1255 }
1256
1257 /* go up and search for index to the right */
1258 while (--depth >= 0) {
1259 ix = path[depth].p_idx;
1260 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001261 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001262 }
1263
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001264 /* we've gone up to the root and found no index to the right */
1265 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001266
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001267got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001268 /* we've found index to the right, let's
1269 * follow it and find the closest allocated
1270 * block to the right */
1271 ix++;
1272 block = idx_pblock(ix);
1273 while (++depth < path->p_depth) {
1274 bh = sb_bread(inode->i_sb, block);
1275 if (bh == NULL)
1276 return -EIO;
1277 eh = ext_block_hdr(bh);
Eric Sandeen395a87b2009-03-10 18:18:47 -04001278 /* subtract from p_depth to get proper eh_depth */
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001279 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001280 put_bh(bh);
1281 return -EIO;
1282 }
1283 ix = EXT_FIRST_INDEX(eh);
1284 block = idx_pblock(ix);
1285 put_bh(bh);
1286 }
1287
1288 bh = sb_bread(inode->i_sb, block);
1289 if (bh == NULL)
1290 return -EIO;
1291 eh = ext_block_hdr(bh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001292 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001293 put_bh(bh);
1294 return -EIO;
1295 }
1296 ex = EXT_FIRST_EXTENT(eh);
1297 *logical = le32_to_cpu(ex->ee_block);
1298 *phys = ext_pblock(ex);
1299 put_bh(bh);
1300 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001301}
1302
1303/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001304 * ext4_ext_next_allocated_block:
1305 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1306 * NOTE: it considers block number from index entry as
1307 * allocated block. Thus, index entries have to be consistent
1308 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001309 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001310static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001311ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1312{
1313 int depth;
1314
1315 BUG_ON(path == NULL);
1316 depth = path->p_depth;
1317
1318 if (depth == 0 && path->p_ext == NULL)
1319 return EXT_MAX_BLOCK;
1320
1321 while (depth >= 0) {
1322 if (depth == path->p_depth) {
1323 /* leaf */
1324 if (path[depth].p_ext !=
1325 EXT_LAST_EXTENT(path[depth].p_hdr))
1326 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1327 } else {
1328 /* index */
1329 if (path[depth].p_idx !=
1330 EXT_LAST_INDEX(path[depth].p_hdr))
1331 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1332 }
1333 depth--;
1334 }
1335
1336 return EXT_MAX_BLOCK;
1337}
1338
1339/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001340 * ext4_ext_next_leaf_block:
Alex Tomasa86c6182006-10-11 01:21:03 -07001341 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1342 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001343static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
Andrew Morton63f57932006-10-11 01:21:24 -07001344 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001345{
1346 int depth;
1347
1348 BUG_ON(path == NULL);
1349 depth = path->p_depth;
1350
1351 /* zero-tree has no leaf blocks at all */
1352 if (depth == 0)
1353 return EXT_MAX_BLOCK;
1354
1355 /* go to index block */
1356 depth--;
1357
1358 while (depth >= 0) {
1359 if (path[depth].p_idx !=
1360 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001361 return (ext4_lblk_t)
1362 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001363 depth--;
1364 }
1365
1366 return EXT_MAX_BLOCK;
1367}
1368
1369/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001370 * ext4_ext_correct_indexes:
1371 * if leaf gets modified and modified extent is first in the leaf,
1372 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001373 * TODO: do we need to correct tree in all cases?
1374 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001375static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001376 struct ext4_ext_path *path)
1377{
1378 struct ext4_extent_header *eh;
1379 int depth = ext_depth(inode);
1380 struct ext4_extent *ex;
1381 __le32 border;
1382 int k, err = 0;
1383
1384 eh = path[depth].p_hdr;
1385 ex = path[depth].p_ext;
1386 BUG_ON(ex == NULL);
1387 BUG_ON(eh == NULL);
1388
1389 if (depth == 0) {
1390 /* there is no tree at all */
1391 return 0;
1392 }
1393
1394 if (ex != EXT_FIRST_EXTENT(eh)) {
1395 /* we correct tree if first leaf got modified only */
1396 return 0;
1397 }
1398
1399 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001400 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001401 */
1402 k = depth - 1;
1403 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001404 err = ext4_ext_get_access(handle, inode, path + k);
1405 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001406 return err;
1407 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001408 err = ext4_ext_dirty(handle, inode, path + k);
1409 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001410 return err;
1411
1412 while (k--) {
1413 /* change all left-side indexes */
1414 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1415 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001416 err = ext4_ext_get_access(handle, inode, path + k);
1417 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001418 break;
1419 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001420 err = ext4_ext_dirty(handle, inode, path + k);
1421 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001422 break;
1423 }
1424
1425 return err;
1426}
1427
Avantika Mathur09b88252006-12-06 20:41:36 -08001428static int
Alex Tomasa86c6182006-10-11 01:21:03 -07001429ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1430 struct ext4_extent *ex2)
1431{
Amit Arora749269f2007-07-18 09:02:56 -04001432 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001433
1434 /*
1435 * Make sure that either both extents are uninitialized, or
1436 * both are _not_.
1437 */
1438 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1439 return 0;
1440
Amit Arora749269f2007-07-18 09:02:56 -04001441 if (ext4_ext_is_uninitialized(ex1))
1442 max_len = EXT_UNINIT_MAX_LEN;
1443 else
1444 max_len = EXT_INIT_MAX_LEN;
1445
Amit Aroraa2df2a62007-07-17 21:42:41 -04001446 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1447 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1448
1449 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001450 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001451 return 0;
1452
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001453 /*
1454 * To allow future support for preallocated extents to be added
1455 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001456 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001457 */
Amit Arora749269f2007-07-18 09:02:56 -04001458 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001459 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001460#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001461 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001462 return 0;
1463#endif
1464
Amit Aroraa2df2a62007-07-17 21:42:41 -04001465 if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001466 return 1;
1467 return 0;
1468}
1469
1470/*
Amit Arora56055d32007-07-17 21:42:38 -04001471 * This function tries to merge the "ex" extent to the next extent in the tree.
1472 * It always tries to merge towards right. If you want to merge towards
1473 * left, pass "ex - 1" as argument instead of "ex".
1474 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1475 * 1 if they got merged.
1476 */
1477int ext4_ext_try_to_merge(struct inode *inode,
1478 struct ext4_ext_path *path,
1479 struct ext4_extent *ex)
1480{
1481 struct ext4_extent_header *eh;
1482 unsigned int depth, len;
1483 int merge_done = 0;
1484 int uninitialized = 0;
1485
1486 depth = ext_depth(inode);
1487 BUG_ON(path[depth].p_hdr == NULL);
1488 eh = path[depth].p_hdr;
1489
1490 while (ex < EXT_LAST_EXTENT(eh)) {
1491 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1492 break;
1493 /* merge with next extent! */
1494 if (ext4_ext_is_uninitialized(ex))
1495 uninitialized = 1;
1496 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1497 + ext4_ext_get_actual_len(ex + 1));
1498 if (uninitialized)
1499 ext4_ext_mark_uninitialized(ex);
1500
1501 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1502 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1503 * sizeof(struct ext4_extent);
1504 memmove(ex + 1, ex + 2, len);
1505 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001506 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001507 merge_done = 1;
1508 WARN_ON(eh->eh_entries == 0);
1509 if (!eh->eh_entries)
1510 ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
1511 "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
1512 }
1513
1514 return merge_done;
1515}
1516
1517/*
Amit Arora25d14f92007-05-24 13:04:13 -04001518 * check if a portion of the "newext" extent overlaps with an
1519 * existing extent.
1520 *
1521 * If there is an overlap discovered, it updates the length of the newext
1522 * such that there will be no overlap, and then returns 1.
1523 * If there is no overlap found, it returns 0.
1524 */
1525unsigned int ext4_ext_check_overlap(struct inode *inode,
1526 struct ext4_extent *newext,
1527 struct ext4_ext_path *path)
1528{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001529 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001530 unsigned int depth, len1;
1531 unsigned int ret = 0;
1532
1533 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001534 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001535 depth = ext_depth(inode);
1536 if (!path[depth].p_ext)
1537 goto out;
1538 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1539
1540 /*
1541 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001542 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001543 */
1544 if (b2 < b1) {
1545 b2 = ext4_ext_next_allocated_block(path);
1546 if (b2 == EXT_MAX_BLOCK)
1547 goto out;
1548 }
1549
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001550 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001551 if (b1 + len1 < b1) {
1552 len1 = EXT_MAX_BLOCK - b1;
1553 newext->ee_len = cpu_to_le16(len1);
1554 ret = 1;
1555 }
1556
1557 /* check for overlap */
1558 if (b1 + len1 > b2) {
1559 newext->ee_len = cpu_to_le16(b2 - b1);
1560 ret = 1;
1561 }
1562out:
1563 return ret;
1564}
1565
1566/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001567 * ext4_ext_insert_extent:
1568 * tries to merge requsted extent into the existing extent or
1569 * inserts requested extent as new one into the tree,
1570 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001571 */
1572int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1573 struct ext4_ext_path *path,
1574 struct ext4_extent *newext)
1575{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001576 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001577 struct ext4_extent *ex, *fex;
1578 struct ext4_extent *nearex; /* nearest extent */
1579 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001580 int depth, len, err;
1581 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001582 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001583
Amit Aroraa2df2a62007-07-17 21:42:41 -04001584 BUG_ON(ext4_ext_get_actual_len(newext) == 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07001585 depth = ext_depth(inode);
1586 ex = path[depth].p_ext;
1587 BUG_ON(path[depth].p_hdr == NULL);
1588
1589 /* try to insert block into found extent and return */
1590 if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
Mingming Cao2ae02102006-10-11 01:21:11 -07001591 ext_debug("append %d block to %d:%d (from %llu)\n",
Amit Aroraa2df2a62007-07-17 21:42:41 -04001592 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001593 le32_to_cpu(ex->ee_block),
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 */
Mingming Cao2ae02102006-10-11 01:21:11 -07001662 ext_debug("first extent in the leaf: %d:%llu:%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001663 le32_to_cpu(newext->ee_block),
1664 ext_pblock(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001665 ext4_ext_get_actual_len(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001666 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1667 } else if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001668 > le32_to_cpu(nearex->ee_block)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001669/* BUG_ON(newext->ee_block == nearex->ee_block); */
1670 if (nearex != EXT_LAST_EXTENT(eh)) {
1671 len = EXT_MAX_EXTENT(eh) - nearex;
1672 len = (len - 1) * sizeof(struct ext4_extent);
1673 len = len < 0 ? 0 : len;
Mingming Cao2ae02102006-10-11 01:21:11 -07001674 ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001675 "move %d from 0x%p to 0x%p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001676 le32_to_cpu(newext->ee_block),
1677 ext_pblock(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001678 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001679 nearex, len, nearex + 1, nearex + 2);
1680 memmove(nearex + 2, nearex + 1, len);
1681 }
1682 path[depth].p_ext = nearex + 1;
1683 } else {
1684 BUG_ON(newext->ee_block == nearex->ee_block);
1685 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1686 len = len < 0 ? 0 : len;
Mingming Cao2ae02102006-10-11 01:21:11 -07001687 ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001688 "move %d from 0x%p to 0x%p\n",
1689 le32_to_cpu(newext->ee_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001690 ext_pblock(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001691 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001692 nearex, len, nearex + 1, nearex + 2);
1693 memmove(nearex + 1, nearex, len);
1694 path[depth].p_ext = nearex;
1695 }
1696
Marcin Slusarze8546d02008-04-17 10:38:59 -04001697 le16_add_cpu(&eh->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001698 nearex = path[depth].p_ext;
1699 nearex->ee_block = newext->ee_block;
Aneesh Kumar K.Vb3776112007-10-16 18:38:25 -04001700 ext4_ext_store_pblock(nearex, ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001701 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001702
1703merge:
1704 /* try to merge extents to the right */
Amit Arora56055d32007-07-17 21:42:38 -04001705 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001706
1707 /* try to merge extents to the left */
1708
1709 /* time to correct all indexes above */
1710 err = ext4_ext_correct_indexes(handle, inode, path);
1711 if (err)
1712 goto cleanup;
1713
1714 err = ext4_ext_dirty(handle, inode, path + depth);
1715
1716cleanup:
1717 if (npath) {
1718 ext4_ext_drop_refs(npath);
1719 kfree(npath);
1720 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001721 ext4_ext_invalidate_cache(inode);
1722 return err;
1723}
1724
Eric Sandeen6873fa02008-10-07 00:46:36 -04001725int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1726 ext4_lblk_t num, ext_prepare_callback func,
1727 void *cbdata)
1728{
1729 struct ext4_ext_path *path = NULL;
1730 struct ext4_ext_cache cbex;
1731 struct ext4_extent *ex;
1732 ext4_lblk_t next, start = 0, end = 0;
1733 ext4_lblk_t last = block + num;
1734 int depth, exists, err = 0;
1735
1736 BUG_ON(func == NULL);
1737 BUG_ON(inode == NULL);
1738
1739 while (block < last && block != EXT_MAX_BLOCK) {
1740 num = last - block;
1741 /* find extent for this block */
1742 path = ext4_ext_find_extent(inode, block, path);
1743 if (IS_ERR(path)) {
1744 err = PTR_ERR(path);
1745 path = NULL;
1746 break;
1747 }
1748
1749 depth = ext_depth(inode);
1750 BUG_ON(path[depth].p_hdr == NULL);
1751 ex = path[depth].p_ext;
1752 next = ext4_ext_next_allocated_block(path);
1753
1754 exists = 0;
1755 if (!ex) {
1756 /* there is no extent yet, so try to allocate
1757 * all requested space */
1758 start = block;
1759 end = block + num;
1760 } else if (le32_to_cpu(ex->ee_block) > block) {
1761 /* need to allocate space before found extent */
1762 start = block;
1763 end = le32_to_cpu(ex->ee_block);
1764 if (block + num < end)
1765 end = block + num;
1766 } else if (block >= le32_to_cpu(ex->ee_block)
1767 + ext4_ext_get_actual_len(ex)) {
1768 /* need to allocate space after found extent */
1769 start = block;
1770 end = block + num;
1771 if (end >= next)
1772 end = next;
1773 } else if (block >= le32_to_cpu(ex->ee_block)) {
1774 /*
1775 * some part of requested space is covered
1776 * by found extent
1777 */
1778 start = block;
1779 end = le32_to_cpu(ex->ee_block)
1780 + ext4_ext_get_actual_len(ex);
1781 if (block + num < end)
1782 end = block + num;
1783 exists = 1;
1784 } else {
1785 BUG();
1786 }
1787 BUG_ON(end <= start);
1788
1789 if (!exists) {
1790 cbex.ec_block = start;
1791 cbex.ec_len = end - start;
1792 cbex.ec_start = 0;
1793 cbex.ec_type = EXT4_EXT_CACHE_GAP;
1794 } else {
1795 cbex.ec_block = le32_to_cpu(ex->ee_block);
1796 cbex.ec_len = ext4_ext_get_actual_len(ex);
1797 cbex.ec_start = ext_pblock(ex);
1798 cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
1799 }
1800
1801 BUG_ON(cbex.ec_len == 0);
1802 err = func(inode, path, &cbex, ex, cbdata);
1803 ext4_ext_drop_refs(path);
1804
1805 if (err < 0)
1806 break;
1807
1808 if (err == EXT_REPEAT)
1809 continue;
1810 else if (err == EXT_BREAK) {
1811 err = 0;
1812 break;
1813 }
1814
1815 if (ext_depth(inode) != depth) {
1816 /* depth was changed. we have to realloc path */
1817 kfree(path);
1818 path = NULL;
1819 }
1820
1821 block = cbex.ec_block + cbex.ec_len;
1822 }
1823
1824 if (path) {
1825 ext4_ext_drop_refs(path);
1826 kfree(path);
1827 }
1828
1829 return err;
1830}
1831
Avantika Mathur09b88252006-12-06 20:41:36 -08001832static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001833ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Mingming Caodd545672007-07-31 00:37:46 -07001834 __u32 len, ext4_fsblk_t start, int type)
Alex Tomasa86c6182006-10-11 01:21:03 -07001835{
1836 struct ext4_ext_cache *cex;
1837 BUG_ON(len == 0);
1838 cex = &EXT4_I(inode)->i_cached_extent;
1839 cex->ec_type = type;
1840 cex->ec_block = block;
1841 cex->ec_len = len;
1842 cex->ec_start = start;
1843}
1844
1845/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001846 * ext4_ext_put_gap_in_cache:
1847 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07001848 * and cache this gap
1849 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001850static void
Alex Tomasa86c6182006-10-11 01:21:03 -07001851ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001852 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07001853{
1854 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001855 unsigned long len;
1856 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001857 struct ext4_extent *ex;
1858
1859 ex = path[depth].p_ext;
1860 if (ex == NULL) {
1861 /* there is no extent yet, so gap is [0;-] */
1862 lblock = 0;
1863 len = EXT_MAX_BLOCK;
1864 ext_debug("cache gap(whole file):");
1865 } else if (block < le32_to_cpu(ex->ee_block)) {
1866 lblock = block;
1867 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05001868 ext_debug("cache gap(before): %u [%u:%u]",
1869 block,
1870 le32_to_cpu(ex->ee_block),
1871 ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07001872 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001873 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001874 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001875 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001876 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001877
1878 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05001879 ext_debug("cache gap(after): [%u:%u] %u",
1880 le32_to_cpu(ex->ee_block),
1881 ext4_ext_get_actual_len(ex),
1882 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001883 BUG_ON(next == lblock);
1884 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001885 } else {
1886 lblock = len = 0;
1887 BUG();
1888 }
1889
Eric Sandeenbba90742008-01-28 23:58:27 -05001890 ext_debug(" -> %u:%lu\n", lblock, len);
Alex Tomasa86c6182006-10-11 01:21:03 -07001891 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1892}
1893
Avantika Mathur09b88252006-12-06 20:41:36 -08001894static int
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001895ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
Alex Tomasa86c6182006-10-11 01:21:03 -07001896 struct ext4_extent *ex)
1897{
1898 struct ext4_ext_cache *cex;
1899
1900 cex = &EXT4_I(inode)->i_cached_extent;
1901
1902 /* has cache valid data? */
1903 if (cex->ec_type == EXT4_EXT_CACHE_NO)
1904 return EXT4_EXT_CACHE_NO;
1905
1906 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1907 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1908 if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001909 ex->ee_block = cpu_to_le32(cex->ec_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001910 ext4_ext_store_pblock(ex, cex->ec_start);
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001911 ex->ee_len = cpu_to_le16(cex->ec_len);
Eric Sandeenbba90742008-01-28 23:58:27 -05001912 ext_debug("%u cached by %u:%u:%llu\n",
1913 block,
1914 cex->ec_block, cex->ec_len, cex->ec_start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001915 return cex->ec_type;
1916 }
1917
1918 /* not in cache */
1919 return EXT4_EXT_CACHE_NO;
1920}
1921
1922/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001923 * ext4_ext_rm_idx:
1924 * removes index from the index block.
1925 * It's used in truncate case only, thus all requests are for
1926 * last index in the block only.
Alex Tomasa86c6182006-10-11 01:21:03 -07001927 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001928static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001929 struct ext4_ext_path *path)
1930{
1931 struct buffer_head *bh;
1932 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001933 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07001934
1935 /* free index block */
1936 path--;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001937 leaf = idx_pblock(path->p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -07001938 BUG_ON(path->p_hdr->eh_entries == 0);
Avantika Mathur7e028972006-12-06 20:41:33 -08001939 err = ext4_ext_get_access(handle, inode, path);
1940 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001941 return err;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001942 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08001943 err = ext4_ext_dirty(handle, inode, path);
1944 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001945 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07001946 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Alex Tomasa86c6182006-10-11 01:21:03 -07001947 bh = sb_find_get_block(inode->i_sb, leaf);
1948 ext4_forget(handle, 1, inode, bh, leaf);
Alex Tomasc9de5602008-01-29 00:19:52 -05001949 ext4_free_blocks(handle, inode, leaf, 1, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001950 return err;
1951}
1952
1953/*
Mingming Caoee12b632008-08-19 22:16:05 -04001954 * ext4_ext_calc_credits_for_single_extent:
1955 * This routine returns max. credits that needed to insert an extent
1956 * to the extent tree.
1957 * When pass the actual path, the caller should calculate credits
1958 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07001959 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04001960int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07001961 struct ext4_ext_path *path)
1962{
Alex Tomasa86c6182006-10-11 01:21:03 -07001963 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04001964 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04001965 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04001966
Alex Tomasa86c6182006-10-11 01:21:03 -07001967 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07001968 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04001969 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
1970
1971 /*
1972 * There are some space in the leaf tree, no
1973 * need to account for leaf block credit
1974 *
1975 * bitmaps and block group descriptor blocks
1976 * and other metadat blocks still need to be
1977 * accounted.
1978 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04001979 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04001980 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
1981 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001982 }
1983
Mingming Cao525f4ed2008-08-19 22:15:58 -04001984 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04001985}
Alex Tomasa86c6182006-10-11 01:21:03 -07001986
Mingming Caoee12b632008-08-19 22:16:05 -04001987/*
1988 * How many index/leaf blocks need to change/allocate to modify nrblocks?
1989 *
1990 * if nrblocks are fit in a single extent (chunk flag is 1), then
1991 * in the worse case, each tree level index/leaf need to be changed
1992 * if the tree split due to insert a new extent, then the old tree
1993 * index/leaf need to be updated too
1994 *
1995 * If the nrblocks are discontiguous, they could cause
1996 * the whole tree split more than once, but this is really rare.
1997 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04001998int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoee12b632008-08-19 22:16:05 -04001999{
2000 int index;
2001 int depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002002
Mingming Caoee12b632008-08-19 22:16:05 -04002003 if (chunk)
2004 index = depth * 2;
2005 else
2006 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002007
Mingming Caoee12b632008-08-19 22:16:05 -04002008 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002009}
2010
2011static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2012 struct ext4_extent *ex,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002013 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002014{
2015 struct buffer_head *bh;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002016 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05002017 int i, metadata = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002018
Alex Tomasc9de5602008-01-29 00:19:52 -05002019 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2020 metadata = 1;
Alex Tomasa86c6182006-10-11 01:21:03 -07002021#ifdef EXTENTS_STATS
2022 {
2023 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002024 spin_lock(&sbi->s_ext_stats_lock);
2025 sbi->s_ext_blocks += ee_len;
2026 sbi->s_ext_extents++;
2027 if (ee_len < sbi->s_ext_min)
2028 sbi->s_ext_min = ee_len;
2029 if (ee_len > sbi->s_ext_max)
2030 sbi->s_ext_max = ee_len;
2031 if (ext_depth(inode) > sbi->s_depth_max)
2032 sbi->s_depth_max = ext_depth(inode);
2033 spin_unlock(&sbi->s_ext_stats_lock);
2034 }
2035#endif
2036 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002037 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002038 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002039 ext4_lblk_t num;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002040 ext4_fsblk_t start;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002041
Amit Aroraa2df2a62007-07-17 21:42:41 -04002042 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2043 start = ext_pblock(ex) + ee_len - num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002044 ext_debug("free last %u blocks starting %llu\n", num, start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002045 for (i = 0; i < num; i++) {
2046 bh = sb_find_get_block(inode->i_sb, start + i);
2047 ext4_forget(handle, 0, inode, bh, start + i);
2048 }
Alex Tomasc9de5602008-01-29 00:19:52 -05002049 ext4_free_blocks(handle, inode, start, num, metadata);
Alex Tomasa86c6182006-10-11 01:21:03 -07002050 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002051 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002052 printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
Amit Aroraa2df2a62007-07-17 21:42:41 -04002053 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002054 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002055 printk(KERN_INFO "strange request: removal(2) "
2056 "%u-%u from %u:%u\n",
2057 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002058 }
2059 return 0;
2060}
2061
2062static int
2063ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002064 struct ext4_ext_path *path, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07002065{
2066 int err = 0, correct_index = 0;
2067 int depth = ext_depth(inode), credits;
2068 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002069 ext4_lblk_t a, b, block;
2070 unsigned num;
2071 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002072 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002073 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002074 struct ext4_extent *ex;
2075
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002076 /* the header must be checked already in ext4_ext_remove_space() */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002077 ext_debug("truncate since %u in leaf\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002078 if (!path[depth].p_hdr)
2079 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2080 eh = path[depth].p_hdr;
2081 BUG_ON(eh == NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07002082
2083 /* find where to start removing */
2084 ex = EXT_LAST_EXTENT(eh);
2085
2086 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002087 if (ext4_ext_is_uninitialized(ex))
2088 uninitialized = 1;
2089 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002090
2091 while (ex >= EXT_FIRST_EXTENT(eh) &&
2092 ex_ee_block + ex_ee_len > start) {
2093 ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
2094 path[depth].p_ext = ex;
2095
2096 a = ex_ee_block > start ? ex_ee_block : start;
2097 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
2098 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
2099
2100 ext_debug(" border %u:%u\n", a, b);
2101
2102 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
2103 block = 0;
2104 num = 0;
2105 BUG();
2106 } else if (a != ex_ee_block) {
2107 /* remove tail of the extent */
2108 block = ex_ee_block;
2109 num = a - block;
2110 } else if (b != ex_ee_block + ex_ee_len - 1) {
2111 /* remove head of the extent */
2112 block = a;
2113 num = b - a;
2114 /* there is no "make a hole" API yet */
2115 BUG();
2116 } else {
2117 /* remove whole extent: excellent! */
2118 block = ex_ee_block;
2119 num = 0;
2120 BUG_ON(a != ex_ee_block);
2121 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
2122 }
2123
Theodore Ts'o34071da2008-08-01 21:59:19 -04002124 /*
2125 * 3 for leaf, sb, and inode plus 2 (bmap and group
2126 * descriptor) for each block group; assume two block
2127 * groups plus ex_ee_len/blocks_per_block_group for
2128 * the worst case
2129 */
2130 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002131 if (ex == EXT_FIRST_EXTENT(eh)) {
2132 correct_index = 1;
2133 credits += (ext_depth(inode)) + 1;
2134 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002135 credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002136
Shen Feng9102e4f2008-07-11 19:27:31 -04002137 err = ext4_ext_journal_restart(handle, credits);
2138 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002139 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002140
2141 err = ext4_ext_get_access(handle, inode, path + depth);
2142 if (err)
2143 goto out;
2144
2145 err = ext4_remove_blocks(handle, inode, ex, a, b);
2146 if (err)
2147 goto out;
2148
2149 if (num == 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002150 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002151 ext4_ext_store_pblock(ex, 0);
Marcin Slusarze8546d02008-04-17 10:38:59 -04002152 le16_add_cpu(&eh->eh_entries, -1);
Alex Tomasa86c6182006-10-11 01:21:03 -07002153 }
2154
2155 ex->ee_block = cpu_to_le32(block);
2156 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002157 /*
2158 * Do not mark uninitialized if all the blocks in the
2159 * extent have been removed.
2160 */
2161 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002162 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002163
2164 err = ext4_ext_dirty(handle, inode, path + depth);
2165 if (err)
2166 goto out;
2167
Mingming Cao2ae02102006-10-11 01:21:11 -07002168 ext_debug("new extent: %u:%u:%llu\n", block, num,
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002169 ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002170 ex--;
2171 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002172 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002173 }
2174
2175 if (correct_index && eh->eh_entries)
2176 err = ext4_ext_correct_indexes(handle, inode, path);
2177
2178 /* if this leaf is free, then we should
2179 * remove it from index block above */
2180 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2181 err = ext4_ext_rm_idx(handle, inode, path + depth);
2182
2183out:
2184 return err;
2185}
2186
2187/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002188 * ext4_ext_more_to_rm:
2189 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002190 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002191static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002192ext4_ext_more_to_rm(struct ext4_ext_path *path)
2193{
2194 BUG_ON(path->p_idx == NULL);
2195
2196 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2197 return 0;
2198
2199 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002200 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002201 * so we have to consider current index for truncation
2202 */
2203 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2204 return 0;
2205 return 1;
2206}
2207
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002208static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07002209{
2210 struct super_block *sb = inode->i_sb;
2211 int depth = ext_depth(inode);
2212 struct ext4_ext_path *path;
2213 handle_t *handle;
2214 int i = 0, err = 0;
2215
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002216 ext_debug("truncate since %u\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002217
2218 /* probably first extent we're gonna free will be last in block */
2219 handle = ext4_journal_start(inode, depth + 1);
2220 if (IS_ERR(handle))
2221 return PTR_ERR(handle);
2222
2223 ext4_ext_invalidate_cache(inode);
2224
2225 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002226 * We start scanning from right side, freeing all the blocks
2227 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002228 */
Josef Bacik216553c2008-04-29 22:02:02 -04002229 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07002230 if (path == NULL) {
2231 ext4_journal_stop(handle);
2232 return -ENOMEM;
2233 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002234 path[0].p_hdr = ext_inode_hdr(inode);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002235 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002236 err = -EIO;
2237 goto out;
2238 }
2239 path[0].p_depth = depth;
2240
2241 while (i >= 0 && err == 0) {
2242 if (i == depth) {
2243 /* this is leaf block */
2244 err = ext4_ext_rm_leaf(handle, inode, path, start);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002245 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002246 brelse(path[i].p_bh);
2247 path[i].p_bh = NULL;
2248 i--;
2249 continue;
2250 }
2251
2252 /* this is index block */
2253 if (!path[i].p_hdr) {
2254 ext_debug("initialize header\n");
2255 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002256 }
2257
Alex Tomasa86c6182006-10-11 01:21:03 -07002258 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002259 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002260 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2261 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2262 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2263 path[i].p_hdr,
2264 le16_to_cpu(path[i].p_hdr->eh_entries));
2265 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002266 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002267 path[i].p_idx--;
2268 }
2269
2270 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2271 i, EXT_FIRST_INDEX(path[i].p_hdr),
2272 path[i].p_idx);
2273 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002274 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002275 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002276 ext_debug("move to level %d (block %llu)\n",
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002277 i + 1, idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002278 memset(path + i + 1, 0, sizeof(*path));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002279 bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2280 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002281 /* should we reset i_size? */
2282 err = -EIO;
2283 break;
2284 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002285 if (WARN_ON(i + 1 > depth)) {
2286 err = -EIO;
2287 break;
2288 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002289 if (ext4_ext_check(inode, ext_block_hdr(bh),
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002290 depth - i - 1)) {
2291 err = -EIO;
2292 break;
2293 }
2294 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002295
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002296 /* save actual number of indexes since this
2297 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002298 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2299 i++;
2300 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002301 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002302 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002303 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002304 * handle must be already prepared by the
2305 * truncatei_leaf() */
2306 err = ext4_ext_rm_idx(handle, inode, path + i);
2307 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002308 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002309 brelse(path[i].p_bh);
2310 path[i].p_bh = NULL;
2311 i--;
2312 ext_debug("return to level %d\n", i);
2313 }
2314 }
2315
2316 /* TODO: flexible tree reduction should be here */
2317 if (path->p_hdr->eh_entries == 0) {
2318 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002319 * truncate to zero freed all the tree,
2320 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002321 */
2322 err = ext4_ext_get_access(handle, inode, path);
2323 if (err == 0) {
2324 ext_inode_hdr(inode)->eh_depth = 0;
2325 ext_inode_hdr(inode)->eh_max =
2326 cpu_to_le16(ext4_ext_space_root(inode));
2327 err = ext4_ext_dirty(handle, inode, path);
2328 }
2329 }
2330out:
Alex Tomasa86c6182006-10-11 01:21:03 -07002331 ext4_ext_drop_refs(path);
2332 kfree(path);
2333 ext4_journal_stop(handle);
2334
2335 return err;
2336}
2337
2338/*
2339 * called at mount time
2340 */
2341void ext4_ext_init(struct super_block *sb)
2342{
2343 /*
2344 * possible initialization would be here
2345 */
2346
Theodore Ts'o83982b62009-01-06 14:53:16 -05002347 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Theodore Ts'o47760042008-09-08 23:00:52 -04002348 printk(KERN_INFO "EXT4-fs: file extents enabled");
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002349#ifdef AGGRESSIVE_TEST
2350 printk(", aggressive tests");
Alex Tomasa86c6182006-10-11 01:21:03 -07002351#endif
2352#ifdef CHECK_BINSEARCH
2353 printk(", check binsearch");
2354#endif
2355#ifdef EXTENTS_STATS
2356 printk(", stats");
2357#endif
2358 printk("\n");
2359#ifdef EXTENTS_STATS
2360 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2361 EXT4_SB(sb)->s_ext_min = 1 << 30;
2362 EXT4_SB(sb)->s_ext_max = 0;
2363#endif
2364 }
2365}
2366
2367/*
2368 * called at umount time
2369 */
2370void ext4_ext_release(struct super_block *sb)
2371{
Theodore Ts'o83982b62009-01-06 14:53:16 -05002372 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
Alex Tomasa86c6182006-10-11 01:21:03 -07002373 return;
2374
2375#ifdef EXTENTS_STATS
2376 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2377 struct ext4_sb_info *sbi = EXT4_SB(sb);
2378 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2379 sbi->s_ext_blocks, sbi->s_ext_extents,
2380 sbi->s_ext_blocks / sbi->s_ext_extents);
2381 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2382 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2383 }
2384#endif
2385}
2386
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002387static void bi_complete(struct bio *bio, int error)
2388{
2389 complete((struct completion *)bio->bi_private);
2390}
2391
2392/* FIXME!! we need to try to merge to left or right after zero-out */
2393static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2394{
2395 int ret = -EIO;
2396 struct bio *bio;
2397 int blkbits, blocksize;
2398 sector_t ee_pblock;
2399 struct completion event;
2400 unsigned int ee_len, len, done, offset;
2401
2402
2403 blkbits = inode->i_blkbits;
2404 blocksize = inode->i_sb->s_blocksize;
2405 ee_len = ext4_ext_get_actual_len(ex);
2406 ee_pblock = ext_pblock(ex);
2407
2408 /* convert ee_pblock to 512 byte sectors */
2409 ee_pblock = ee_pblock << (blkbits - 9);
2410
2411 while (ee_len > 0) {
2412
2413 if (ee_len > BIO_MAX_PAGES)
2414 len = BIO_MAX_PAGES;
2415 else
2416 len = ee_len;
2417
2418 bio = bio_alloc(GFP_NOIO, len);
2419 if (!bio)
2420 return -ENOMEM;
2421 bio->bi_sector = ee_pblock;
2422 bio->bi_bdev = inode->i_sb->s_bdev;
2423
2424 done = 0;
2425 offset = 0;
2426 while (done < len) {
2427 ret = bio_add_page(bio, ZERO_PAGE(0),
2428 blocksize, offset);
2429 if (ret != blocksize) {
2430 /*
2431 * We can't add any more pages because of
2432 * hardware limitations. Start a new bio.
2433 */
2434 break;
2435 }
2436 done++;
2437 offset += blocksize;
2438 if (offset >= PAGE_CACHE_SIZE)
2439 offset = 0;
2440 }
2441
2442 init_completion(&event);
2443 bio->bi_private = &event;
2444 bio->bi_end_io = bi_complete;
2445 submit_bio(WRITE, bio);
2446 wait_for_completion(&event);
2447
2448 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
2449 ret = 0;
2450 else {
2451 ret = -EIO;
2452 break;
2453 }
2454 bio_put(bio);
2455 ee_len -= done;
2456 ee_pblock += done << (blkbits - 9);
2457 }
2458 return ret;
2459}
2460
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002461#define EXT4_EXT_ZERO_LEN 7
2462
Amit Arora56055d32007-07-17 21:42:38 -04002463/*
2464 * This function is called by ext4_ext_get_blocks() if someone tries to write
2465 * to an uninitialized extent. It may result in splitting the uninitialized
2466 * extent into multiple extents (upto three - one initialized and two
2467 * uninitialized).
2468 * There are three possibilities:
2469 * a> There is no split required: Entire extent should be initialized
2470 * b> Splits in two extents: Write is happening at either end of the extent
2471 * c> Splits in three extents: Somone is writing in middle of the extent
2472 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002473static int ext4_ext_convert_to_initialized(handle_t *handle,
2474 struct inode *inode,
2475 struct ext4_ext_path *path,
2476 ext4_lblk_t iblock,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002477 unsigned int max_blocks)
Amit Arora56055d32007-07-17 21:42:38 -04002478{
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002479 struct ext4_extent *ex, newex, orig_ex;
Amit Arora56055d32007-07-17 21:42:38 -04002480 struct ext4_extent *ex1 = NULL;
2481 struct ext4_extent *ex2 = NULL;
2482 struct ext4_extent *ex3 = NULL;
2483 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002484 ext4_lblk_t ee_block;
2485 unsigned int allocated, ee_len, depth;
Amit Arora56055d32007-07-17 21:42:38 -04002486 ext4_fsblk_t newblock;
2487 int err = 0;
2488 int ret = 0;
2489
2490 depth = ext_depth(inode);
2491 eh = path[depth].p_hdr;
2492 ex = path[depth].p_ext;
2493 ee_block = le32_to_cpu(ex->ee_block);
2494 ee_len = ext4_ext_get_actual_len(ex);
2495 allocated = ee_len - (iblock - ee_block);
2496 newblock = iblock - ee_block + ext_pblock(ex);
2497 ex2 = ex;
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002498 orig_ex.ee_block = ex->ee_block;
2499 orig_ex.ee_len = cpu_to_le16(ee_len);
2500 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
Amit Arora56055d32007-07-17 21:42:38 -04002501
Aneesh Kumar K.V9df56432008-02-22 06:17:31 -05002502 err = ext4_ext_get_access(handle, inode, path + depth);
2503 if (err)
2504 goto out;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002505 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2506 if (ee_len <= 2*EXT4_EXT_ZERO_LEN) {
2507 err = ext4_ext_zeroout(inode, &orig_ex);
2508 if (err)
2509 goto fix_extent_len;
2510 /* update the extent length and mark as initialized */
2511 ex->ee_block = orig_ex.ee_block;
2512 ex->ee_len = orig_ex.ee_len;
2513 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2514 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002515 /* zeroed the full extent */
2516 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002517 }
Aneesh Kumar K.V9df56432008-02-22 06:17:31 -05002518
Amit Arora56055d32007-07-17 21:42:38 -04002519 /* ex1: ee_block to iblock - 1 : uninitialized */
2520 if (iblock > ee_block) {
2521 ex1 = ex;
2522 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2523 ext4_ext_mark_uninitialized(ex1);
2524 ex2 = &newex;
2525 }
2526 /*
2527 * for sanity, update the length of the ex2 extent before
2528 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2529 * overlap of blocks.
2530 */
2531 if (!ex1 && allocated > max_blocks)
2532 ex2->ee_len = cpu_to_le16(max_blocks);
2533 /* ex3: to ee_block + ee_len : uninitialised */
2534 if (allocated > max_blocks) {
2535 unsigned int newdepth;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002536 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
2537 if (allocated <= EXT4_EXT_ZERO_LEN) {
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002538 /*
2539 * iblock == ee_block is handled by the zerouout
2540 * at the beginning.
2541 * Mark first half uninitialized.
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002542 * Mark second half initialized and zero out the
2543 * initialized extent
2544 */
2545 ex->ee_block = orig_ex.ee_block;
2546 ex->ee_len = cpu_to_le16(ee_len - allocated);
2547 ext4_ext_mark_uninitialized(ex);
2548 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2549 ext4_ext_dirty(handle, inode, path + depth);
2550
2551 ex3 = &newex;
2552 ex3->ee_block = cpu_to_le32(iblock);
2553 ext4_ext_store_pblock(ex3, newblock);
2554 ex3->ee_len = cpu_to_le16(allocated);
2555 err = ext4_ext_insert_extent(handle, inode, path, ex3);
2556 if (err == -ENOSPC) {
2557 err = ext4_ext_zeroout(inode, &orig_ex);
2558 if (err)
2559 goto fix_extent_len;
2560 ex->ee_block = orig_ex.ee_block;
2561 ex->ee_len = orig_ex.ee_len;
2562 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2563 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002564 /* blocks available from iblock */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002565 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002566
2567 } else if (err)
2568 goto fix_extent_len;
2569
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002570 /*
2571 * We need to zero out the second half because
2572 * an fallocate request can update file size and
2573 * converting the second half to initialized extent
2574 * implies that we can leak some junk data to user
2575 * space.
2576 */
2577 err = ext4_ext_zeroout(inode, ex3);
2578 if (err) {
2579 /*
2580 * We should actually mark the
2581 * second half as uninit and return error
2582 * Insert would have changed the extent
2583 */
2584 depth = ext_depth(inode);
2585 ext4_ext_drop_refs(path);
2586 path = ext4_ext_find_extent(inode,
2587 iblock, path);
2588 if (IS_ERR(path)) {
2589 err = PTR_ERR(path);
2590 return err;
2591 }
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002592 /* get the second half extent details */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002593 ex = path[depth].p_ext;
2594 err = ext4_ext_get_access(handle, inode,
2595 path + depth);
2596 if (err)
2597 return err;
2598 ext4_ext_mark_uninitialized(ex);
2599 ext4_ext_dirty(handle, inode, path + depth);
2600 return err;
2601 }
2602
2603 /* zeroed the second half */
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002604 return allocated;
2605 }
Amit Arora56055d32007-07-17 21:42:38 -04002606 ex3 = &newex;
2607 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2608 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2609 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2610 ext4_ext_mark_uninitialized(ex3);
2611 err = ext4_ext_insert_extent(handle, inode, path, ex3);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002612 if (err == -ENOSPC) {
2613 err = ext4_ext_zeroout(inode, &orig_ex);
2614 if (err)
2615 goto fix_extent_len;
2616 /* update the extent length and mark as initialized */
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002617 ex->ee_block = orig_ex.ee_block;
2618 ex->ee_len = orig_ex.ee_len;
2619 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002620 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002621 /* zeroed the full extent */
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002622 /* blocks available from iblock */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002623 return allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002624
2625 } else if (err)
2626 goto fix_extent_len;
Amit Arora56055d32007-07-17 21:42:38 -04002627 /*
2628 * The depth, and hence eh & ex might change
2629 * as part of the insert above.
2630 */
2631 newdepth = ext_depth(inode);
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002632 /*
Coly Li73ac36e2009-01-07 18:09:16 -08002633 * update the extent length after successful insert of the
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002634 * split extent
2635 */
2636 orig_ex.ee_len = cpu_to_le16(ee_len -
2637 ext4_ext_get_actual_len(ex3));
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002638 depth = newdepth;
2639 ext4_ext_drop_refs(path);
2640 path = ext4_ext_find_extent(inode, iblock, path);
2641 if (IS_ERR(path)) {
2642 err = PTR_ERR(path);
2643 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04002644 }
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002645 eh = path[depth].p_hdr;
2646 ex = path[depth].p_ext;
2647 if (ex2 != &newex)
2648 ex2 = ex;
2649
2650 err = ext4_ext_get_access(handle, inode, path + depth);
2651 if (err)
2652 goto out;
2653
Amit Arora56055d32007-07-17 21:42:38 -04002654 allocated = max_blocks;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002655
2656 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2657 * to insert a extent in the middle zerout directly
2658 * otherwise give the extent a chance to merge to left
2659 */
2660 if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
2661 iblock != ee_block) {
2662 err = ext4_ext_zeroout(inode, &orig_ex);
2663 if (err)
2664 goto fix_extent_len;
2665 /* update the extent length and mark as initialized */
2666 ex->ee_block = orig_ex.ee_block;
2667 ex->ee_len = orig_ex.ee_len;
2668 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2669 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002670 /* zero out the first half */
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002671 /* blocks available from iblock */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002672 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002673 }
Amit Arora56055d32007-07-17 21:42:38 -04002674 }
2675 /*
2676 * If there was a change of depth as part of the
2677 * insertion of ex3 above, we need to update the length
2678 * of the ex1 extent again here
2679 */
2680 if (ex1 && ex1 != ex) {
2681 ex1 = ex;
2682 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2683 ext4_ext_mark_uninitialized(ex1);
2684 ex2 = &newex;
2685 }
2686 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2687 ex2->ee_block = cpu_to_le32(iblock);
Amit Arora56055d32007-07-17 21:42:38 -04002688 ext4_ext_store_pblock(ex2, newblock);
2689 ex2->ee_len = cpu_to_le16(allocated);
2690 if (ex2 != ex)
2691 goto insert;
Amit Arora56055d32007-07-17 21:42:38 -04002692 /*
2693 * New (initialized) extent starts from the first block
2694 * in the current extent. i.e., ex2 == ex
2695 * We have to see if it can be merged with the extent
2696 * on the left.
2697 */
2698 if (ex2 > EXT_FIRST_EXTENT(eh)) {
2699 /*
2700 * To merge left, pass "ex2 - 1" to try_to_merge(),
2701 * since it merges towards right _only_.
2702 */
2703 ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
2704 if (ret) {
2705 err = ext4_ext_correct_indexes(handle, inode, path);
2706 if (err)
2707 goto out;
2708 depth = ext_depth(inode);
2709 ex2--;
2710 }
2711 }
2712 /*
2713 * Try to Merge towards right. This might be required
2714 * only when the whole extent is being written to.
2715 * i.e. ex2 == ex and ex3 == NULL.
2716 */
2717 if (!ex3) {
2718 ret = ext4_ext_try_to_merge(inode, path, ex2);
2719 if (ret) {
2720 err = ext4_ext_correct_indexes(handle, inode, path);
2721 if (err)
2722 goto out;
2723 }
2724 }
2725 /* Mark modified extent as dirty */
2726 err = ext4_ext_dirty(handle, inode, path + depth);
2727 goto out;
2728insert:
2729 err = ext4_ext_insert_extent(handle, inode, path, &newex);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002730 if (err == -ENOSPC) {
2731 err = ext4_ext_zeroout(inode, &orig_ex);
2732 if (err)
2733 goto fix_extent_len;
2734 /* update the extent length and mark as initialized */
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002735 ex->ee_block = orig_ex.ee_block;
2736 ex->ee_len = orig_ex.ee_len;
2737 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002738 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002739 /* zero out the first half */
2740 return allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002741 } else if (err)
2742 goto fix_extent_len;
Amit Arora56055d32007-07-17 21:42:38 -04002743out:
2744 return err ? err : allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002745
2746fix_extent_len:
2747 ex->ee_block = orig_ex.ee_block;
2748 ex->ee_len = orig_ex.ee_len;
2749 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2750 ext4_ext_mark_uninitialized(ex);
2751 ext4_ext_dirty(handle, inode, path + depth);
2752 return err;
Amit Arora56055d32007-07-17 21:42:38 -04002753}
2754
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05002755/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05002756 * Block allocation/map/preallocation routine for extents based files
2757 *
2758 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05002759 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05002760 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
2761 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05002762 *
2763 * return > 0, number of of blocks already mapped/allocated
2764 * if create == 0 and these are pre-allocated blocks
2765 * buffer head is unmapped
2766 * otherwise blocks are mapped
2767 *
2768 * return = 0, if plain look up failed (blocks have not been allocated)
2769 * buffer head is unmapped
2770 *
2771 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05002772 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002773int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002774 ext4_lblk_t iblock,
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002775 unsigned int max_blocks, struct buffer_head *bh_result,
Alex Tomasa86c6182006-10-11 01:21:03 -07002776 int create, int extend_disksize)
2777{
2778 struct ext4_ext_path *path = NULL;
Amit Arora56055d32007-07-17 21:42:38 -04002779 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002780 struct ext4_extent newex, *ex;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002781 ext4_fsblk_t newblock;
2782 int err = 0, depth, ret, cache_type;
2783 unsigned int allocated = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002784 struct ext4_allocation_request ar;
Mingming Cao61628a32008-07-11 19:27:31 -04002785 loff_t disksize;
Alex Tomasa86c6182006-10-11 01:21:03 -07002786
2787 __clear_bit(BH_New, &bh_result->b_state);
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002788 ext_debug("blocks %u/%u requested for inode %u\n",
Eric Sandeenbba90742008-01-28 23:58:27 -05002789 iblock, max_blocks, inode->i_ino);
Alex Tomasa86c6182006-10-11 01:21:03 -07002790
2791 /* check in cache */
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002792 cache_type = ext4_ext_in_cache(inode, iblock, &newex);
2793 if (cache_type) {
2794 if (cache_type == EXT4_EXT_CACHE_GAP) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002795 if (!create) {
Amit Arora56055d32007-07-17 21:42:38 -04002796 /*
2797 * block isn't allocated yet and
2798 * user doesn't want to allocate it
2799 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002800 goto out2;
2801 }
2802 /* we should allocate requested block */
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002803 } else if (cache_type == EXT4_EXT_CACHE_EXTENT) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002804 /* block is already allocated */
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002805 newblock = iblock
2806 - le32_to_cpu(newex.ee_block)
2807 + ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002808 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05002809 allocated = ext4_ext_get_actual_len(&newex) -
Alex Tomasa86c6182006-10-11 01:21:03 -07002810 (iblock - le32_to_cpu(newex.ee_block));
2811 goto out;
2812 } else {
2813 BUG();
2814 }
2815 }
2816
2817 /* find extent for this block */
2818 path = ext4_ext_find_extent(inode, iblock, NULL);
2819 if (IS_ERR(path)) {
2820 err = PTR_ERR(path);
2821 path = NULL;
2822 goto out2;
2823 }
2824
2825 depth = ext_depth(inode);
2826
2827 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002828 * consistent leaf must not be empty;
2829 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07002830 * this is why assert can't be put in ext4_ext_find_extent()
2831 */
2832 BUG_ON(path[depth].p_ext == NULL && depth != 0);
Amit Arora56055d32007-07-17 21:42:38 -04002833 eh = path[depth].p_hdr;
Alex Tomasa86c6182006-10-11 01:21:03 -07002834
Avantika Mathur7e028972006-12-06 20:41:33 -08002835 ex = path[depth].p_ext;
2836 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002837 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002838 ext4_fsblk_t ee_start = ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002839 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07002840
2841 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07002842 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04002843 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07002844 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04002845 ee_len = ext4_ext_get_actual_len(ex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002846 /* if found extent covers block, simply return it */
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002847 if (iblock >= ee_block && iblock < ee_block + ee_len) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002848 newblock = iblock - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002849 /* number of remaining blocks in the extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07002850 allocated = ee_len - (iblock - ee_block);
Eric Sandeenbba90742008-01-28 23:58:27 -05002851 ext_debug("%u fit into %lu:%d -> %llu\n", iblock,
Alex Tomasa86c6182006-10-11 01:21:03 -07002852 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04002853
Amit Aroraa2df2a62007-07-17 21:42:41 -04002854 /* Do not put uninitialized extent in the cache */
Amit Arora56055d32007-07-17 21:42:38 -04002855 if (!ext4_ext_is_uninitialized(ex)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04002856 ext4_ext_put_in_cache(inode, ee_block,
2857 ee_len, ee_start,
2858 EXT4_EXT_CACHE_EXTENT);
Amit Arora56055d32007-07-17 21:42:38 -04002859 goto out;
2860 }
2861 if (create == EXT4_CREATE_UNINITIALIZED_EXT)
2862 goto out;
Aneesh Kumar K.Ve067ba02008-04-29 08:11:12 -04002863 if (!create) {
2864 /*
2865 * We have blocks reserved already. We
2866 * return allocated blocks so that delalloc
2867 * won't do block reservation for us. But
2868 * the buffer head will be unmapped so that
2869 * a read from the block returns 0s.
2870 */
2871 if (allocated > max_blocks)
2872 allocated = max_blocks;
Eric Sandeen953e6222008-07-11 19:27:31 -04002873 set_buffer_unwritten(bh_result);
Amit Arora56055d32007-07-17 21:42:38 -04002874 goto out2;
Aneesh Kumar K.Ve067ba02008-04-29 08:11:12 -04002875 }
Amit Arora56055d32007-07-17 21:42:38 -04002876
2877 ret = ext4_ext_convert_to_initialized(handle, inode,
2878 path, iblock,
2879 max_blocks);
Dmitry Monakhovdbf9d7d2008-01-28 23:58:27 -05002880 if (ret <= 0) {
2881 err = ret;
Amit Arora56055d32007-07-17 21:42:38 -04002882 goto out2;
Dmitry Monakhovdbf9d7d2008-01-28 23:58:27 -05002883 } else
Amit Arora56055d32007-07-17 21:42:38 -04002884 allocated = ret;
2885 goto outnew;
Alex Tomasa86c6182006-10-11 01:21:03 -07002886 }
2887 }
2888
2889 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002890 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07002891 * we couldn't try to create block if create flag is zero
2892 */
2893 if (!create) {
Amit Arora56055d32007-07-17 21:42:38 -04002894 /*
2895 * put just found gap into cache to speed up
2896 * subsequent requests
2897 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002898 ext4_ext_put_gap_in_cache(inode, path, iblock);
2899 goto out2;
2900 }
2901 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04002902 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07002903 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002904
Alex Tomasc9de5602008-01-29 00:19:52 -05002905 /* find neighbour allocated blocks */
2906 ar.lleft = iblock;
2907 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
2908 if (err)
2909 goto out2;
2910 ar.lright = iblock;
2911 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
2912 if (err)
2913 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04002914
Amit Arora749269f2007-07-18 09:02:56 -04002915 /*
2916 * See if request is beyond maximum number of blocks we can have in
2917 * a single extent. For an initialized extent this limit is
2918 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
2919 * EXT_UNINIT_MAX_LEN.
2920 */
2921 if (max_blocks > EXT_INIT_MAX_LEN &&
2922 create != EXT4_CREATE_UNINITIALIZED_EXT)
2923 max_blocks = EXT_INIT_MAX_LEN;
2924 else if (max_blocks > EXT_UNINIT_MAX_LEN &&
2925 create == EXT4_CREATE_UNINITIALIZED_EXT)
2926 max_blocks = EXT_UNINIT_MAX_LEN;
2927
Amit Arora25d14f92007-05-24 13:04:13 -04002928 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
2929 newex.ee_block = cpu_to_le32(iblock);
2930 newex.ee_len = cpu_to_le16(max_blocks);
2931 err = ext4_ext_check_overlap(inode, &newex, path);
2932 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05002933 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04002934 else
2935 allocated = max_blocks;
Alex Tomasc9de5602008-01-29 00:19:52 -05002936
2937 /* allocate new block */
2938 ar.inode = inode;
2939 ar.goal = ext4_ext_find_goal(inode, path, iblock);
2940 ar.logical = iblock;
2941 ar.len = allocated;
2942 if (S_ISREG(inode->i_mode))
2943 ar.flags = EXT4_MB_HINT_DATA;
2944 else
2945 /* disable in-core preallocation for non-regular files */
2946 ar.flags = 0;
2947 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07002948 if (!newblock)
2949 goto out2;
Mingming Cao2ae02102006-10-11 01:21:11 -07002950 ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002951 ar.goal, newblock, allocated);
Alex Tomasa86c6182006-10-11 01:21:03 -07002952
2953 /* try to insert new extent into found leaf and return */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002954 ext4_ext_store_pblock(&newex, newblock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002955 newex.ee_len = cpu_to_le16(ar.len);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002956 if (create == EXT4_CREATE_UNINITIALIZED_EXT) /* Mark uninitialized */
2957 ext4_ext_mark_uninitialized(&newex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002958 err = ext4_ext_insert_extent(handle, inode, path, &newex);
Alex Tomas315054f2007-05-24 13:04:25 -04002959 if (err) {
2960 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05002961 /* not a good idea to call discard here directly,
2962 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04002963 ext4_discard_preallocations(inode);
Alex Tomas315054f2007-05-24 13:04:25 -04002964 ext4_free_blocks(handle, inode, ext_pblock(&newex),
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05002965 ext4_ext_get_actual_len(&newex), 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002966 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04002967 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002968
Alex Tomasa86c6182006-10-11 01:21:03 -07002969 /* previous routine could use block we allocated */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002970 newblock = ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05002971 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora56055d32007-07-17 21:42:38 -04002972outnew:
Mingming Cao61628a32008-07-11 19:27:31 -04002973 if (extend_disksize) {
2974 disksize = ((loff_t) iblock + ar.len) << inode->i_blkbits;
2975 if (disksize > i_size_read(inode))
2976 disksize = i_size_read(inode);
2977 if (disksize > EXT4_I(inode)->i_disksize)
2978 EXT4_I(inode)->i_disksize = disksize;
2979 }
Aneesh Kumar K.Va379cd12008-07-11 19:27:31 -04002980
Eric Sandeen953e6222008-07-11 19:27:31 -04002981 set_buffer_new(bh_result);
Alex Tomasa86c6182006-10-11 01:21:03 -07002982
Amit Aroraa2df2a62007-07-17 21:42:41 -04002983 /* Cache only when it is _not_ an uninitialized extent */
2984 if (create != EXT4_CREATE_UNINITIALIZED_EXT)
2985 ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
2986 EXT4_EXT_CACHE_EXTENT);
Alex Tomasa86c6182006-10-11 01:21:03 -07002987out:
2988 if (allocated > max_blocks)
2989 allocated = max_blocks;
2990 ext4_ext_show_leaf(inode, path);
Eric Sandeen953e6222008-07-11 19:27:31 -04002991 set_buffer_mapped(bh_result);
Alex Tomasa86c6182006-10-11 01:21:03 -07002992 bh_result->b_bdev = inode->i_sb->s_bdev;
2993 bh_result->b_blocknr = newblock;
2994out2:
2995 if (path) {
2996 ext4_ext_drop_refs(path);
2997 kfree(path);
2998 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002999 return err ? err : allocated;
3000}
3001
Jan Karacf108bc2008-07-11 19:27:31 -04003002void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07003003{
3004 struct address_space *mapping = inode->i_mapping;
3005 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003006 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07003007 handle_t *handle;
3008 int err = 0;
3009
3010 /*
3011 * probably first extent we're gonna free will be last in block
3012 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04003013 err = ext4_writepage_trans_blocks(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07003014 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04003015 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07003016 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07003017
Jan Karacf108bc2008-07-11 19:27:31 -04003018 if (inode->i_size & (sb->s_blocksize - 1))
3019 ext4_block_truncate_page(handle, mapping, inode->i_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07003020
Jan Kara9ddfc3d2008-07-11 19:27:31 -04003021 if (ext4_orphan_add(handle, inode))
3022 goto out_stop;
3023
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003024 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07003025 ext4_ext_invalidate_cache(inode);
3026
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003027 ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05003028
Alex Tomasa86c6182006-10-11 01:21:03 -07003029 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003030 * TODO: optimization is possible here.
3031 * Probably we need not scan at all,
3032 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07003033 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003034
3035 /* we have to know where to truncate from in crash case */
3036 EXT4_I(inode)->i_disksize = inode->i_size;
3037 ext4_mark_inode_dirty(handle, inode);
3038
3039 last_block = (inode->i_size + sb->s_blocksize - 1)
3040 >> EXT4_BLOCK_SIZE_BITS(sb);
3041 err = ext4_ext_remove_space(inode, last_block);
3042
3043 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04003044 * transaction synchronous.
3045 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003046 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05003047 ext4_handle_sync(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07003048
3049out_stop:
Jan Kara9ddfc3d2008-07-11 19:27:31 -04003050 up_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07003051 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003052 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07003053 * then we need to clear up the orphan record which we created above.
3054 * However, if this was a real unlink then we were called by
3055 * ext4_delete_inode(), and we allow that function to clean up the
3056 * orphan info for us.
3057 */
3058 if (inode->i_nlink)
3059 ext4_orphan_del(handle, inode);
3060
Solofo Ramangalahyef737722008-04-29 22:00:41 -04003061 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3062 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07003063 ext4_journal_stop(handle);
3064}
3065
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003066static void ext4_falloc_update_inode(struct inode *inode,
3067 int mode, loff_t new_size, int update_ctime)
3068{
3069 struct timespec now;
3070
3071 if (update_ctime) {
3072 now = current_fs_time(inode->i_sb);
3073 if (!timespec_equal(&inode->i_ctime, &now))
3074 inode->i_ctime = now;
3075 }
3076 /*
3077 * Update only when preallocation was requested beyond
3078 * the file size.
3079 */
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003080 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3081 if (new_size > i_size_read(inode))
3082 i_size_write(inode, new_size);
3083 if (new_size > EXT4_I(inode)->i_disksize)
3084 ext4_update_i_disksize(inode, new_size);
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003085 }
3086
3087}
3088
Amit Aroraa2df2a62007-07-17 21:42:41 -04003089/*
3090 * preallocate space for a file. This implements ext4's fallocate inode
3091 * operation, which gets called from sys_fallocate system call.
3092 * For block-mapped files, posix_fallocate should fall back to the method
3093 * of writing zeroes to the required new blocks (the same behavior which is
3094 * expected for file systems which do not support fallocate() system call).
3095 */
3096long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
3097{
3098 handle_t *handle;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003099 ext4_lblk_t block;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003100 loff_t new_size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05003101 unsigned int max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003102 int ret = 0;
3103 int ret2 = 0;
3104 int retries = 0;
3105 struct buffer_head map_bh;
3106 unsigned int credits, blkbits = inode->i_blkbits;
3107
3108 /*
3109 * currently supporting (pre)allocate mode for extent-based
3110 * files _only_
3111 */
3112 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
3113 return -EOPNOTSUPP;
3114
3115 /* preallocation to directories is currently not supported */
3116 if (S_ISDIR(inode->i_mode))
3117 return -ENODEV;
3118
3119 block = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003120 /*
3121 * We can't just convert len to max_blocks because
3122 * If blocksize = 4096 offset = 3072 and len = 2048
3123 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003124 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003125 - block;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003126 /*
Mingming Caof3bd1f32008-08-19 22:16:03 -04003127 * credits to insert 1 extent into extent tree
Amit Aroraa2df2a62007-07-17 21:42:41 -04003128 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04003129 credits = ext4_chunk_trans_blocks(inode, max_blocks);
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05003130 mutex_lock(&inode->i_mutex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003131retry:
3132 while (ret >= 0 && ret < max_blocks) {
3133 block = block + ret;
3134 max_blocks = max_blocks - ret;
3135 handle = ext4_journal_start(inode, credits);
3136 if (IS_ERR(handle)) {
3137 ret = PTR_ERR(handle);
3138 break;
3139 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05003140 ret = ext4_get_blocks_wrap(handle, inode, block,
Amit Aroraa2df2a62007-07-17 21:42:41 -04003141 max_blocks, &map_bh,
Mingming Caod2a17632008-07-14 17:52:37 -04003142 EXT4_CREATE_UNINITIALIZED_EXT, 0, 0);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05003143 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003144#ifdef EXT4FS_DEBUG
3145 WARN_ON(ret <= 0);
3146 printk(KERN_ERR "%s: ext4_ext_get_blocks "
3147 "returned error inode#%lu, block=%u, "
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05003148 "max_blocks=%u", __func__,
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05003149 inode->i_ino, block, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05003150#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04003151 ext4_mark_inode_dirty(handle, inode);
3152 ret2 = ext4_journal_stop(handle);
3153 break;
3154 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003155 if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
3156 blkbits) >> blkbits))
3157 new_size = offset + len;
3158 else
3159 new_size = (block + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003160
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003161 ext4_falloc_update_inode(inode, mode, new_size,
3162 buffer_new(&map_bh));
Amit Aroraa2df2a62007-07-17 21:42:41 -04003163 ext4_mark_inode_dirty(handle, inode);
3164 ret2 = ext4_journal_stop(handle);
3165 if (ret2)
3166 break;
3167 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04003168 if (ret == -ENOSPC &&
3169 ext4_should_retry_alloc(inode->i_sb, &retries)) {
3170 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003171 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04003172 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05003173 mutex_unlock(&inode->i_mutex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003174 return ret > 0 ? ret2 : ret;
3175}
Eric Sandeen6873fa02008-10-07 00:46:36 -04003176
3177/*
3178 * Callback function called for each extent to gather FIEMAP information.
3179 */
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003180static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path,
Eric Sandeen6873fa02008-10-07 00:46:36 -04003181 struct ext4_ext_cache *newex, struct ext4_extent *ex,
3182 void *data)
3183{
3184 struct fiemap_extent_info *fieinfo = data;
3185 unsigned long blksize_bits = inode->i_sb->s_blocksize_bits;
3186 __u64 logical;
3187 __u64 physical;
3188 __u64 length;
3189 __u32 flags = 0;
3190 int error;
3191
3192 logical = (__u64)newex->ec_block << blksize_bits;
3193
3194 if (newex->ec_type == EXT4_EXT_CACHE_GAP) {
3195 pgoff_t offset;
3196 struct page *page;
3197 struct buffer_head *bh = NULL;
3198
3199 offset = logical >> PAGE_SHIFT;
3200 page = find_get_page(inode->i_mapping, offset);
3201 if (!page || !page_has_buffers(page))
3202 return EXT_CONTINUE;
3203
3204 bh = page_buffers(page);
3205
3206 if (!bh)
3207 return EXT_CONTINUE;
3208
3209 if (buffer_delay(bh)) {
3210 flags |= FIEMAP_EXTENT_DELALLOC;
3211 page_cache_release(page);
3212 } else {
3213 page_cache_release(page);
3214 return EXT_CONTINUE;
3215 }
3216 }
3217
3218 physical = (__u64)newex->ec_start << blksize_bits;
3219 length = (__u64)newex->ec_len << blksize_bits;
3220
3221 if (ex && ext4_ext_is_uninitialized(ex))
3222 flags |= FIEMAP_EXTENT_UNWRITTEN;
3223
3224 /*
3225 * If this extent reaches EXT_MAX_BLOCK, it must be last.
3226 *
3227 * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
3228 * this also indicates no more allocated blocks.
3229 *
3230 * XXX this might miss a single-block extent at EXT_MAX_BLOCK
3231 */
3232 if (logical + length - 1 == EXT_MAX_BLOCK ||
3233 ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK)
3234 flags |= FIEMAP_EXTENT_LAST;
3235
3236 error = fiemap_fill_next_extent(fieinfo, logical, physical,
3237 length, flags);
3238 if (error < 0)
3239 return error;
3240 if (error == 1)
3241 return EXT_BREAK;
3242
3243 return EXT_CONTINUE;
3244}
3245
3246/* fiemap flags we can handle specified here */
3247#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
3248
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003249static int ext4_xattr_fiemap(struct inode *inode,
3250 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04003251{
3252 __u64 physical = 0;
3253 __u64 length;
3254 __u32 flags = FIEMAP_EXTENT_LAST;
3255 int blockbits = inode->i_sb->s_blocksize_bits;
3256 int error = 0;
3257
3258 /* in-inode? */
3259 if (EXT4_I(inode)->i_state & EXT4_STATE_XATTR) {
3260 struct ext4_iloc iloc;
3261 int offset; /* offset of xattr in inode */
3262
3263 error = ext4_get_inode_loc(inode, &iloc);
3264 if (error)
3265 return error;
3266 physical = iloc.bh->b_blocknr << blockbits;
3267 offset = EXT4_GOOD_OLD_INODE_SIZE +
3268 EXT4_I(inode)->i_extra_isize;
3269 physical += offset;
3270 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
3271 flags |= FIEMAP_EXTENT_DATA_INLINE;
3272 } else { /* external block */
3273 physical = EXT4_I(inode)->i_file_acl << blockbits;
3274 length = inode->i_sb->s_blocksize;
3275 }
3276
3277 if (physical)
3278 error = fiemap_fill_next_extent(fieinfo, 0, physical,
3279 length, flags);
3280 return (error < 0 ? error : 0);
3281}
3282
3283int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3284 __u64 start, __u64 len)
3285{
3286 ext4_lblk_t start_blk;
3287 ext4_lblk_t len_blks;
3288 int error = 0;
3289
3290 /* fallback to generic here if not in extents fmt */
3291 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
3292 return generic_block_fiemap(inode, fieinfo, start, len,
3293 ext4_get_block);
3294
3295 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
3296 return -EBADR;
3297
3298 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
3299 error = ext4_xattr_fiemap(inode, fieinfo);
3300 } else {
3301 start_blk = start >> inode->i_sb->s_blocksize_bits;
3302 len_blks = len >> inode->i_sb->s_blocksize_bits;
3303
3304 /*
3305 * Walk the extent tree gathering extent information.
3306 * ext4_ext_fiemap_cb will push extents back to user.
3307 */
3308 down_write(&EXT4_I(inode)->i_data_sem);
3309 error = ext4_ext_walk_space(inode, start_blk, len_blks,
3310 ext4_ext_fiemap_cb, fieinfo);
3311 up_write(&EXT4_I(inode)->i_data_sem);
3312 }
3313
3314 return error;
3315}
3316