]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/ext4/extents.c
ext4: add a function merging extents right and left
[mirror_ubuntu-bionic-kernel.git] / fs / ext4 / extents.c
CommitLineData
a86c6181
AT
1/*
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>
cd02ff0b 35#include <linux/jbd2.h>
a86c6181
AT
36#include <linux/highuid.h>
37#include <linux/pagemap.h>
38#include <linux/quotaops.h>
39#include <linux/string.h>
40#include <linux/slab.h>
a2df2a63 41#include <linux/falloc.h>
a86c6181 42#include <asm/uaccess.h>
6873fa0d 43#include <linux/fiemap.h>
3dcf5451
CH
44#include "ext4_jbd2.h"
45#include "ext4_extents.h"
a86c6181 46
0562e0ba
JZ
47#include <trace/events/ext4.h>
48
487caeef
JK
49static int ext4_ext_truncate_extend_restart(handle_t *handle,
50 struct inode *inode,
51 int needed)
a86c6181
AT
52{
53 int err;
54
0390131b
FM
55 if (!ext4_handle_valid(handle))
56 return 0;
a86c6181 57 if (handle->h_buffer_credits > needed)
9102e4fa
SF
58 return 0;
59 err = ext4_journal_extend(handle, needed);
0123c939 60 if (err <= 0)
9102e4fa 61 return err;
487caeef 62 err = ext4_truncate_restart_trans(handle, inode, needed);
0617b83f
DM
63 if (err == 0)
64 err = -EAGAIN;
487caeef
JK
65
66 return err;
a86c6181
AT
67}
68
69/*
70 * could return:
71 * - EROFS
72 * - ENOMEM
73 */
74static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
75 struct ext4_ext_path *path)
76{
77 if (path->p_bh) {
78 /* path points to block */
79 return ext4_journal_get_write_access(handle, path->p_bh);
80 }
81 /* path points to leaf/index in inode body */
82 /* we use in-core data, no need to protect them */
83 return 0;
84}
85
86/*
87 * could return:
88 * - EROFS
89 * - ENOMEM
90 * - EIO
91 */
92static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
93 struct ext4_ext_path *path)
94{
95 int err;
96 if (path->p_bh) {
97 /* path points to block */
0390131b 98 err = ext4_handle_dirty_metadata(handle, inode, path->p_bh);
a86c6181
AT
99 } else {
100 /* path points to leaf/index in inode body */
101 err = ext4_mark_inode_dirty(handle, inode);
102 }
103 return err;
104}
105
f65e6fba 106static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
a86c6181 107 struct ext4_ext_path *path,
725d26d3 108 ext4_lblk_t block)
a86c6181
AT
109{
110 struct ext4_inode_info *ei = EXT4_I(inode);
f65e6fba 111 ext4_fsblk_t bg_start;
74d3487f 112 ext4_fsblk_t last_block;
f65e6fba 113 ext4_grpblk_t colour;
a4912123
TT
114 ext4_group_t block_group;
115 int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
a86c6181
AT
116 int depth;
117
118 if (path) {
119 struct ext4_extent *ex;
120 depth = path->p_depth;
121
ad4fb9ca
KM
122 /*
123 * Try to predict block placement assuming that we are
124 * filling in a file which will eventually be
125 * non-sparse --- i.e., in the case of libbfd writing
126 * an ELF object sections out-of-order but in a way
127 * the eventually results in a contiguous object or
128 * executable file, or some database extending a table
129 * space file. However, this is actually somewhat
130 * non-ideal if we are writing a sparse file such as
131 * qemu or KVM writing a raw image file that is going
132 * to stay fairly sparse, since it will end up
133 * fragmenting the file system's free space. Maybe we
134 * should have some hueristics or some way to allow
135 * userspace to pass a hint to file system,
b8d6568a 136 * especially if the latter case turns out to be
ad4fb9ca
KM
137 * common.
138 */
7e028976 139 ex = path[depth].p_ext;
ad4fb9ca
KM
140 if (ex) {
141 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
142 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
143
144 if (block > ext_block)
145 return ext_pblk + (block - ext_block);
146 else
147 return ext_pblk - (ext_block - block);
148 }
a86c6181 149
d0d856e8
RD
150 /* it looks like index is empty;
151 * try to find starting block from index itself */
a86c6181
AT
152 if (path[depth].p_bh)
153 return path[depth].p_bh->b_blocknr;
154 }
155
156 /* OK. use inode's group */
a4912123
TT
157 block_group = ei->i_block_group;
158 if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
159 /*
160 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
60e6679e
TT
161 * block groups per flexgroup, reserve the first block
162 * group for directories and special files. Regular
a4912123 163 * files will start at the second block group. This
60e6679e 164 * tends to speed up directory access and improves
a4912123
TT
165 * fsck times.
166 */
167 block_group &= ~(flex_size-1);
168 if (S_ISREG(inode->i_mode))
169 block_group++;
170 }
5661bd68 171 bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
74d3487f
VC
172 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
173
a4912123
TT
174 /*
175 * If we are doing delayed allocation, we don't need take
176 * colour into account.
177 */
178 if (test_opt(inode->i_sb, DELALLOC))
179 return bg_start;
180
74d3487f
VC
181 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
182 colour = (current->pid % 16) *
a86c6181 183 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
74d3487f
VC
184 else
185 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
a86c6181
AT
186 return bg_start + colour + block;
187}
188
654b4908
AK
189/*
190 * Allocation for a meta data block
191 */
f65e6fba 192static ext4_fsblk_t
654b4908 193ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
a86c6181
AT
194 struct ext4_ext_path *path,
195 struct ext4_extent *ex, int *err)
196{
f65e6fba 197 ext4_fsblk_t goal, newblock;
a86c6181
AT
198
199 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
97df5d15 200 newblock = ext4_new_meta_blocks(handle, inode, goal, NULL, err);
a86c6181
AT
201 return newblock;
202}
203
55ad63bf 204static inline int ext4_ext_space_block(struct inode *inode, int check)
a86c6181
AT
205{
206 int size;
207
208 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
209 / sizeof(struct ext4_extent);
55ad63bf 210 if (!check) {
bbf2f9fb 211#ifdef AGGRESSIVE_TEST
55ad63bf
TT
212 if (size > 6)
213 size = 6;
a86c6181 214#endif
55ad63bf 215 }
a86c6181
AT
216 return size;
217}
218
55ad63bf 219static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
a86c6181
AT
220{
221 int size;
222
223 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
224 / sizeof(struct ext4_extent_idx);
55ad63bf 225 if (!check) {
bbf2f9fb 226#ifdef AGGRESSIVE_TEST
55ad63bf
TT
227 if (size > 5)
228 size = 5;
a86c6181 229#endif
55ad63bf 230 }
a86c6181
AT
231 return size;
232}
233
55ad63bf 234static inline int ext4_ext_space_root(struct inode *inode, int check)
a86c6181
AT
235{
236 int size;
237
238 size = sizeof(EXT4_I(inode)->i_data);
239 size -= sizeof(struct ext4_extent_header);
240 size /= sizeof(struct ext4_extent);
55ad63bf 241 if (!check) {
bbf2f9fb 242#ifdef AGGRESSIVE_TEST
55ad63bf
TT
243 if (size > 3)
244 size = 3;
a86c6181 245#endif
55ad63bf 246 }
a86c6181
AT
247 return size;
248}
249
55ad63bf 250static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
a86c6181
AT
251{
252 int size;
253
254 size = sizeof(EXT4_I(inode)->i_data);
255 size -= sizeof(struct ext4_extent_header);
256 size /= sizeof(struct ext4_extent_idx);
55ad63bf 257 if (!check) {
bbf2f9fb 258#ifdef AGGRESSIVE_TEST
55ad63bf
TT
259 if (size > 4)
260 size = 4;
a86c6181 261#endif
55ad63bf 262 }
a86c6181
AT
263 return size;
264}
265
d2a17637
MC
266/*
267 * Calculate the number of metadata blocks needed
268 * to allocate @blocks
269 * Worse case is one block per extent
270 */
01f49d0b 271int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
d2a17637 272{
9d0be502
TT
273 struct ext4_inode_info *ei = EXT4_I(inode);
274 int idxs, num = 0;
d2a17637 275
9d0be502
TT
276 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
277 / sizeof(struct ext4_extent_idx));
d2a17637
MC
278
279 /*
9d0be502
TT
280 * If the new delayed allocation block is contiguous with the
281 * previous da block, it can share index blocks with the
282 * previous block, so we only need to allocate a new index
283 * block every idxs leaf blocks. At ldxs**2 blocks, we need
284 * an additional index block, and at ldxs**3 blocks, yet
285 * another index blocks.
d2a17637 286 */
9d0be502
TT
287 if (ei->i_da_metadata_calc_len &&
288 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
289 if ((ei->i_da_metadata_calc_len % idxs) == 0)
290 num++;
291 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
292 num++;
293 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
294 num++;
295 ei->i_da_metadata_calc_len = 0;
296 } else
297 ei->i_da_metadata_calc_len++;
298 ei->i_da_metadata_calc_last_lblock++;
299 return num;
300 }
d2a17637 301
9d0be502
TT
302 /*
303 * In the worst case we need a new set of index blocks at
304 * every level of the inode's extent tree.
305 */
306 ei->i_da_metadata_calc_len = 1;
307 ei->i_da_metadata_calc_last_lblock = lblock;
308 return ext_depth(inode) + 1;
d2a17637
MC
309}
310
c29c0ae7
AT
311static int
312ext4_ext_max_entries(struct inode *inode, int depth)
313{
314 int max;
315
316 if (depth == ext_depth(inode)) {
317 if (depth == 0)
55ad63bf 318 max = ext4_ext_space_root(inode, 1);
c29c0ae7 319 else
55ad63bf 320 max = ext4_ext_space_root_idx(inode, 1);
c29c0ae7
AT
321 } else {
322 if (depth == 0)
55ad63bf 323 max = ext4_ext_space_block(inode, 1);
c29c0ae7 324 else
55ad63bf 325 max = ext4_ext_space_block_idx(inode, 1);
c29c0ae7
AT
326 }
327
328 return max;
329}
330
56b19868
AK
331static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
332{
bf89d16f 333 ext4_fsblk_t block = ext4_ext_pblock(ext);
56b19868 334 int len = ext4_ext_get_actual_len(ext);
e84a26ce 335
6fd058f7 336 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
56b19868
AK
337}
338
339static int ext4_valid_extent_idx(struct inode *inode,
340 struct ext4_extent_idx *ext_idx)
341{
bf89d16f 342 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
e84a26ce 343
6fd058f7 344 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
56b19868
AK
345}
346
347static int ext4_valid_extent_entries(struct inode *inode,
348 struct ext4_extent_header *eh,
349 int depth)
350{
351 struct ext4_extent *ext;
352 struct ext4_extent_idx *ext_idx;
353 unsigned short entries;
354 if (eh->eh_entries == 0)
355 return 1;
356
357 entries = le16_to_cpu(eh->eh_entries);
358
359 if (depth == 0) {
360 /* leaf entries */
361 ext = EXT_FIRST_EXTENT(eh);
362 while (entries) {
363 if (!ext4_valid_extent(inode, ext))
364 return 0;
365 ext++;
366 entries--;
367 }
368 } else {
369 ext_idx = EXT_FIRST_INDEX(eh);
370 while (entries) {
371 if (!ext4_valid_extent_idx(inode, ext_idx))
372 return 0;
373 ext_idx++;
374 entries--;
375 }
376 }
377 return 1;
378}
379
c398eda0
TT
380static int __ext4_ext_check(const char *function, unsigned int line,
381 struct inode *inode, struct ext4_extent_header *eh,
382 int depth)
c29c0ae7
AT
383{
384 const char *error_msg;
385 int max = 0;
386
387 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
388 error_msg = "invalid magic";
389 goto corrupted;
390 }
391 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
392 error_msg = "unexpected eh_depth";
393 goto corrupted;
394 }
395 if (unlikely(eh->eh_max == 0)) {
396 error_msg = "invalid eh_max";
397 goto corrupted;
398 }
399 max = ext4_ext_max_entries(inode, depth);
400 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
401 error_msg = "too large eh_max";
402 goto corrupted;
403 }
404 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
405 error_msg = "invalid eh_entries";
406 goto corrupted;
407 }
56b19868
AK
408 if (!ext4_valid_extent_entries(inode, eh, depth)) {
409 error_msg = "invalid extent entries";
410 goto corrupted;
411 }
c29c0ae7
AT
412 return 0;
413
414corrupted:
c398eda0 415 ext4_error_inode(inode, function, line, 0,
24676da4 416 "bad header/extent: %s - magic %x, "
c29c0ae7 417 "entries %u, max %u(%u), depth %u(%u)",
24676da4 418 error_msg, le16_to_cpu(eh->eh_magic),
c29c0ae7
AT
419 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
420 max, le16_to_cpu(eh->eh_depth), depth);
421
422 return -EIO;
423}
424
56b19868 425#define ext4_ext_check(inode, eh, depth) \
c398eda0 426 __ext4_ext_check(__func__, __LINE__, inode, eh, depth)
c29c0ae7 427
7a262f7c
AK
428int ext4_ext_check_inode(struct inode *inode)
429{
430 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
431}
432
a86c6181
AT
433#ifdef EXT_DEBUG
434static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
435{
436 int k, l = path->p_depth;
437
438 ext_debug("path:");
439 for (k = 0; k <= l; k++, path++) {
440 if (path->p_idx) {
2ae02107 441 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
bf89d16f 442 ext4_idx_pblock(path->p_idx));
a86c6181 443 } else if (path->p_ext) {
553f9008 444 ext_debug(" %d:[%d]%d:%llu ",
a86c6181 445 le32_to_cpu(path->p_ext->ee_block),
553f9008 446 ext4_ext_is_uninitialized(path->p_ext),
a2df2a63 447 ext4_ext_get_actual_len(path->p_ext),
bf89d16f 448 ext4_ext_pblock(path->p_ext));
a86c6181
AT
449 } else
450 ext_debug(" []");
451 }
452 ext_debug("\n");
453}
454
455static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
456{
457 int depth = ext_depth(inode);
458 struct ext4_extent_header *eh;
459 struct ext4_extent *ex;
460 int i;
461
462 if (!path)
463 return;
464
465 eh = path[depth].p_hdr;
466 ex = EXT_FIRST_EXTENT(eh);
467
553f9008
M
468 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
469
a86c6181 470 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
553f9008
M
471 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
472 ext4_ext_is_uninitialized(ex),
bf89d16f 473 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
a86c6181
AT
474 }
475 ext_debug("\n");
476}
477#else
af5bc92d
TT
478#define ext4_ext_show_path(inode, path)
479#define ext4_ext_show_leaf(inode, path)
a86c6181
AT
480#endif
481
b35905c1 482void ext4_ext_drop_refs(struct ext4_ext_path *path)
a86c6181
AT
483{
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/*
d0d856e8
RD
495 * ext4_ext_binsearch_idx:
496 * binary search for the closest index of the given block
c29c0ae7 497 * the header must be checked before calling this
a86c6181
AT
498 */
499static void
725d26d3
AK
500ext4_ext_binsearch_idx(struct inode *inode,
501 struct ext4_ext_path *path, ext4_lblk_t block)
a86c6181
AT
502{
503 struct ext4_extent_header *eh = path->p_hdr;
504 struct ext4_extent_idx *r, *l, *m;
505
a86c6181 506
bba90743 507 ext_debug("binsearch for %u(idx): ", block);
a86c6181
AT
508
509 l = EXT_FIRST_INDEX(eh) + 1;
e9f410b1 510 r = EXT_LAST_INDEX(eh);
a86c6181
AT
511 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;
26d535ed
DM
517 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));
a86c6181
AT
520 }
521
522 path->p_idx = l - 1;
f65e6fba 523 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
bf89d16f 524 ext4_idx_pblock(path->p_idx));
a86c6181
AT
525
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)) {
4776004f
TT
535 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",
a86c6181
AT
539 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)
8c55e204 543 <= le32_to_cpu(ix[-1].ei_block));
a86c6181
AT
544 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/*
d0d856e8
RD
555 * ext4_ext_binsearch:
556 * binary search for closest extent of the given block
c29c0ae7 557 * the header must be checked before calling this
a86c6181
AT
558 */
559static void
725d26d3
AK
560ext4_ext_binsearch(struct inode *inode,
561 struct ext4_ext_path *path, ext4_lblk_t block)
a86c6181
AT
562{
563 struct ext4_extent_header *eh = path->p_hdr;
564 struct ext4_extent *r, *l, *m;
565
a86c6181
AT
566 if (eh->eh_entries == 0) {
567 /*
d0d856e8
RD
568 * this leaf is empty:
569 * we get such a leaf in split/add case
a86c6181
AT
570 */
571 return;
572 }
573
bba90743 574 ext_debug("binsearch for %u: ", block);
a86c6181
AT
575
576 l = EXT_FIRST_EXTENT(eh) + 1;
e9f410b1 577 r = EXT_LAST_EXTENT(eh);
a86c6181
AT
578
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;
26d535ed
DM
585 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));
a86c6181
AT
588 }
589
590 path->p_ext = l - 1;
553f9008 591 ext_debug(" -> %d:%llu:[%d]%d ",
8c55e204 592 le32_to_cpu(path->p_ext->ee_block),
bf89d16f 593 ext4_ext_pblock(path->p_ext),
553f9008 594 ext4_ext_is_uninitialized(path->p_ext),
a2df2a63 595 ext4_ext_get_actual_len(path->p_ext));
a86c6181
AT
596
597#ifdef CHECK_BINSEARCH
598 {
599 struct ext4_extent *chex, *ex;
600 int k;
601
602 chex = ex = EXT_FIRST_EXTENT(eh);
603 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
604 BUG_ON(k && le32_to_cpu(ex->ee_block)
8c55e204 605 <= le32_to_cpu(ex[-1].ee_block));
a86c6181
AT
606 if (block < le32_to_cpu(ex->ee_block))
607 break;
608 chex = ex;
609 }
610 BUG_ON(chex != path->p_ext);
611 }
612#endif
613
614}
615
616int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
617{
618 struct ext4_extent_header *eh;
619
620 eh = ext_inode_hdr(inode);
621 eh->eh_depth = 0;
622 eh->eh_entries = 0;
623 eh->eh_magic = EXT4_EXT_MAGIC;
55ad63bf 624 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
a86c6181
AT
625 ext4_mark_inode_dirty(handle, inode);
626 ext4_ext_invalidate_cache(inode);
627 return 0;
628}
629
630struct ext4_ext_path *
725d26d3
AK
631ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
632 struct ext4_ext_path *path)
a86c6181
AT
633{
634 struct ext4_extent_header *eh;
635 struct buffer_head *bh;
636 short int depth, i, ppos = 0, alloc = 0;
637
638 eh = ext_inode_hdr(inode);
c29c0ae7 639 depth = ext_depth(inode);
a86c6181
AT
640
641 /* account possible depth increase */
642 if (!path) {
5d4958f9 643 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
a86c6181
AT
644 GFP_NOFS);
645 if (!path)
646 return ERR_PTR(-ENOMEM);
647 alloc = 1;
648 }
a86c6181 649 path[0].p_hdr = eh;
1973adcb 650 path[0].p_bh = NULL;
a86c6181 651
c29c0ae7 652 i = depth;
a86c6181
AT
653 /* walk through the tree */
654 while (i) {
7a262f7c
AK
655 int need_to_validate = 0;
656
a86c6181
AT
657 ext_debug("depth %d: num %d, max %d\n",
658 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
c29c0ae7 659
a86c6181 660 ext4_ext_binsearch_idx(inode, path + ppos, block);
bf89d16f 661 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
a86c6181
AT
662 path[ppos].p_depth = i;
663 path[ppos].p_ext = NULL;
664
7a262f7c
AK
665 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
666 if (unlikely(!bh))
a86c6181 667 goto err;
7a262f7c 668 if (!bh_uptodate_or_lock(bh)) {
0562e0ba
JZ
669 trace_ext4_ext_load_extent(inode, block,
670 path[ppos].p_block);
7a262f7c
AK
671 if (bh_submit_read(bh) < 0) {
672 put_bh(bh);
673 goto err;
674 }
675 /* validate the extent entries */
676 need_to_validate = 1;
677 }
a86c6181
AT
678 eh = ext_block_hdr(bh);
679 ppos++;
273df556
FM
680 if (unlikely(ppos > depth)) {
681 put_bh(bh);
682 EXT4_ERROR_INODE(inode,
683 "ppos %d > depth %d", ppos, depth);
684 goto err;
685 }
a86c6181
AT
686 path[ppos].p_bh = bh;
687 path[ppos].p_hdr = eh;
688 i--;
689
7a262f7c 690 if (need_to_validate && ext4_ext_check(inode, eh, i))
a86c6181
AT
691 goto err;
692 }
693
694 path[ppos].p_depth = i;
a86c6181
AT
695 path[ppos].p_ext = NULL;
696 path[ppos].p_idx = NULL;
697
a86c6181
AT
698 /* find extent */
699 ext4_ext_binsearch(inode, path + ppos, block);
1973adcb
SF
700 /* if not an empty leaf */
701 if (path[ppos].p_ext)
bf89d16f 702 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
a86c6181
AT
703
704 ext4_ext_show_path(inode, path);
705
706 return path;
707
708err:
709 ext4_ext_drop_refs(path);
710 if (alloc)
711 kfree(path);
712 return ERR_PTR(-EIO);
713}
714
715/*
d0d856e8
RD
716 * ext4_ext_insert_index:
717 * insert new index [@logical;@ptr] into the block at @curp;
718 * check where to insert: before @curp or after @curp
a86c6181 719 */
1f109d5a
TT
720static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
721 struct ext4_ext_path *curp,
722 int logical, ext4_fsblk_t ptr)
a86c6181
AT
723{
724 struct ext4_extent_idx *ix;
725 int len, err;
726
7e028976
AM
727 err = ext4_ext_get_access(handle, inode, curp);
728 if (err)
a86c6181
AT
729 return err;
730
273df556
FM
731 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
732 EXT4_ERROR_INODE(inode,
733 "logical %d == ei_block %d!",
734 logical, le32_to_cpu(curp->p_idx->ei_block));
735 return -EIO;
736 }
a86c6181
AT
737 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
738 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
739 /* insert after */
740 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
741 len = (len - 1) * sizeof(struct ext4_extent_idx);
742 len = len < 0 ? 0 : len;
26d535ed 743 ext_debug("insert new index %d after: %llu. "
a86c6181
AT
744 "move %d from 0x%p to 0x%p\n",
745 logical, ptr, len,
746 (curp->p_idx + 1), (curp->p_idx + 2));
747 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
748 }
749 ix = curp->p_idx + 1;
750 } else {
751 /* insert before */
752 len = len * sizeof(struct ext4_extent_idx);
753 len = len < 0 ? 0 : len;
26d535ed 754 ext_debug("insert new index %d before: %llu. "
a86c6181
AT
755 "move %d from 0x%p to 0x%p\n",
756 logical, ptr, len,
757 curp->p_idx, (curp->p_idx + 1));
758 memmove(curp->p_idx + 1, curp->p_idx, len);
759 ix = curp->p_idx;
760 }
761
762 ix->ei_block = cpu_to_le32(logical);
f65e6fba 763 ext4_idx_store_pblock(ix, ptr);
e8546d06 764 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
a86c6181 765
273df556
FM
766 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
767 > le16_to_cpu(curp->p_hdr->eh_max))) {
768 EXT4_ERROR_INODE(inode,
769 "logical %d == ei_block %d!",
770 logical, le32_to_cpu(curp->p_idx->ei_block));
771 return -EIO;
772 }
773 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
774 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
775 return -EIO;
776 }
a86c6181
AT
777
778 err = ext4_ext_dirty(handle, inode, curp);
779 ext4_std_error(inode->i_sb, err);
780
781 return err;
782}
783
784/*
d0d856e8
RD
785 * ext4_ext_split:
786 * inserts new subtree into the path, using free index entry
787 * at depth @at:
788 * - allocates all needed blocks (new leaf and all intermediate index blocks)
789 * - makes decision where to split
790 * - moves remaining extents and index entries (right to the split point)
791 * into the newly allocated blocks
792 * - initializes subtree
a86c6181
AT
793 */
794static int ext4_ext_split(handle_t *handle, struct inode *inode,
795 struct ext4_ext_path *path,
796 struct ext4_extent *newext, int at)
797{
798 struct buffer_head *bh = NULL;
799 int depth = ext_depth(inode);
800 struct ext4_extent_header *neh;
801 struct ext4_extent_idx *fidx;
802 struct ext4_extent *ex;
803 int i = at, k, m, a;
f65e6fba 804 ext4_fsblk_t newblock, oldblock;
a86c6181 805 __le32 border;
f65e6fba 806 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
a86c6181
AT
807 int err = 0;
808
809 /* make decision: where to split? */
d0d856e8 810 /* FIXME: now decision is simplest: at current extent */
a86c6181 811
d0d856e8 812 /* if current leaf will be split, then we should use
a86c6181 813 * border from split point */
273df556
FM
814 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
815 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
816 return -EIO;
817 }
a86c6181
AT
818 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
819 border = path[depth].p_ext[1].ee_block;
d0d856e8 820 ext_debug("leaf will be split."
a86c6181 821 " next leaf starts at %d\n",
8c55e204 822 le32_to_cpu(border));
a86c6181
AT
823 } else {
824 border = newext->ee_block;
825 ext_debug("leaf will be added."
826 " next leaf starts at %d\n",
8c55e204 827 le32_to_cpu(border));
a86c6181
AT
828 }
829
830 /*
d0d856e8
RD
831 * If error occurs, then we break processing
832 * and mark filesystem read-only. index won't
a86c6181 833 * be inserted and tree will be in consistent
d0d856e8 834 * state. Next mount will repair buffers too.
a86c6181
AT
835 */
836
837 /*
d0d856e8
RD
838 * Get array to track all allocated blocks.
839 * We need this to handle errors and free blocks
840 * upon them.
a86c6181 841 */
5d4958f9 842 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
a86c6181
AT
843 if (!ablocks)
844 return -ENOMEM;
a86c6181
AT
845
846 /* allocate all needed blocks */
847 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
848 for (a = 0; a < depth - at; a++) {
654b4908
AK
849 newblock = ext4_ext_new_meta_block(handle, inode, path,
850 newext, &err);
a86c6181
AT
851 if (newblock == 0)
852 goto cleanup;
853 ablocks[a] = newblock;
854 }
855
856 /* initialize new leaf */
857 newblock = ablocks[--a];
273df556
FM
858 if (unlikely(newblock == 0)) {
859 EXT4_ERROR_INODE(inode, "newblock == 0!");
860 err = -EIO;
861 goto cleanup;
862 }
a86c6181
AT
863 bh = sb_getblk(inode->i_sb, newblock);
864 if (!bh) {
865 err = -EIO;
866 goto cleanup;
867 }
868 lock_buffer(bh);
869
7e028976
AM
870 err = ext4_journal_get_create_access(handle, bh);
871 if (err)
a86c6181
AT
872 goto cleanup;
873
874 neh = ext_block_hdr(bh);
875 neh->eh_entries = 0;
55ad63bf 876 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
a86c6181
AT
877 neh->eh_magic = EXT4_EXT_MAGIC;
878 neh->eh_depth = 0;
879 ex = EXT_FIRST_EXTENT(neh);
880
d0d856e8 881 /* move remainder of path[depth] to the new leaf */
273df556
FM
882 if (unlikely(path[depth].p_hdr->eh_entries !=
883 path[depth].p_hdr->eh_max)) {
884 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
885 path[depth].p_hdr->eh_entries,
886 path[depth].p_hdr->eh_max);
887 err = -EIO;
888 goto cleanup;
889 }
a86c6181
AT
890 /* start copy from next extent */
891 /* TODO: we could do it by single memmove */
892 m = 0;
893 path[depth].p_ext++;
894 while (path[depth].p_ext <=
895 EXT_MAX_EXTENT(path[depth].p_hdr)) {
553f9008 896 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
8c55e204 897 le32_to_cpu(path[depth].p_ext->ee_block),
bf89d16f 898 ext4_ext_pblock(path[depth].p_ext),
553f9008 899 ext4_ext_is_uninitialized(path[depth].p_ext),
a2df2a63 900 ext4_ext_get_actual_len(path[depth].p_ext),
a86c6181
AT
901 newblock);
902 /*memmove(ex++, path[depth].p_ext++,
903 sizeof(struct ext4_extent));
904 neh->eh_entries++;*/
905 path[depth].p_ext++;
906 m++;
907 }
908 if (m) {
909 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
e8546d06 910 le16_add_cpu(&neh->eh_entries, m);
a86c6181
AT
911 }
912
913 set_buffer_uptodate(bh);
914 unlock_buffer(bh);
915
0390131b 916 err = ext4_handle_dirty_metadata(handle, inode, bh);
7e028976 917 if (err)
a86c6181
AT
918 goto cleanup;
919 brelse(bh);
920 bh = NULL;
921
922 /* correct old leaf */
923 if (m) {
7e028976
AM
924 err = ext4_ext_get_access(handle, inode, path + depth);
925 if (err)
a86c6181 926 goto cleanup;
e8546d06 927 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
7e028976
AM
928 err = ext4_ext_dirty(handle, inode, path + depth);
929 if (err)
a86c6181
AT
930 goto cleanup;
931
932 }
933
934 /* create intermediate indexes */
935 k = depth - at - 1;
273df556
FM
936 if (unlikely(k < 0)) {
937 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
938 err = -EIO;
939 goto cleanup;
940 }
a86c6181
AT
941 if (k)
942 ext_debug("create %d intermediate indices\n", k);
943 /* insert new index into current index block */
944 /* current depth stored in i var */
945 i = depth - 1;
946 while (k--) {
947 oldblock = newblock;
948 newblock = ablocks[--a];
bba90743 949 bh = sb_getblk(inode->i_sb, newblock);
a86c6181
AT
950 if (!bh) {
951 err = -EIO;
952 goto cleanup;
953 }
954 lock_buffer(bh);
955
7e028976
AM
956 err = ext4_journal_get_create_access(handle, bh);
957 if (err)
a86c6181
AT
958 goto cleanup;
959
960 neh = ext_block_hdr(bh);
961 neh->eh_entries = cpu_to_le16(1);
962 neh->eh_magic = EXT4_EXT_MAGIC;
55ad63bf 963 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
a86c6181
AT
964 neh->eh_depth = cpu_to_le16(depth - i);
965 fidx = EXT_FIRST_INDEX(neh);
966 fidx->ei_block = border;
f65e6fba 967 ext4_idx_store_pblock(fidx, oldblock);
a86c6181 968
bba90743
ES
969 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
970 i, newblock, le32_to_cpu(border), oldblock);
a86c6181
AT
971 /* copy indexes */
972 m = 0;
973 path[i].p_idx++;
974
975 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
976 EXT_MAX_INDEX(path[i].p_hdr));
273df556
FM
977 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
978 EXT_LAST_INDEX(path[i].p_hdr))) {
979 EXT4_ERROR_INODE(inode,
980 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
981 le32_to_cpu(path[i].p_ext->ee_block));
982 err = -EIO;
983 goto cleanup;
984 }
a86c6181 985 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
26d535ed 986 ext_debug("%d: move %d:%llu in new index %llu\n", i,
8c55e204 987 le32_to_cpu(path[i].p_idx->ei_block),
bf89d16f 988 ext4_idx_pblock(path[i].p_idx),
8c55e204 989 newblock);
a86c6181
AT
990 /*memmove(++fidx, path[i].p_idx++,
991 sizeof(struct ext4_extent_idx));
992 neh->eh_entries++;
993 BUG_ON(neh->eh_entries > neh->eh_max);*/
994 path[i].p_idx++;
995 m++;
996 }
997 if (m) {
998 memmove(++fidx, path[i].p_idx - m,
999 sizeof(struct ext4_extent_idx) * m);
e8546d06 1000 le16_add_cpu(&neh->eh_entries, m);
a86c6181
AT
1001 }
1002 set_buffer_uptodate(bh);
1003 unlock_buffer(bh);
1004
0390131b 1005 err = ext4_handle_dirty_metadata(handle, inode, bh);
7e028976 1006 if (err)
a86c6181
AT
1007 goto cleanup;
1008 brelse(bh);
1009 bh = NULL;
1010
1011 /* correct old index */
1012 if (m) {
1013 err = ext4_ext_get_access(handle, inode, path + i);
1014 if (err)
1015 goto cleanup;
e8546d06 1016 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
a86c6181
AT
1017 err = ext4_ext_dirty(handle, inode, path + i);
1018 if (err)
1019 goto cleanup;
1020 }
1021
1022 i--;
1023 }
1024
1025 /* insert new index */
a86c6181
AT
1026 err = ext4_ext_insert_index(handle, inode, path + at,
1027 le32_to_cpu(border), newblock);
1028
1029cleanup:
1030 if (bh) {
1031 if (buffer_locked(bh))
1032 unlock_buffer(bh);
1033 brelse(bh);
1034 }
1035
1036 if (err) {
1037 /* free all allocated blocks in error case */
1038 for (i = 0; i < depth; i++) {
1039 if (!ablocks[i])
1040 continue;
7dc57615 1041 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
e6362609 1042 EXT4_FREE_BLOCKS_METADATA);
a86c6181
AT
1043 }
1044 }
1045 kfree(ablocks);
1046
1047 return err;
1048}
1049
1050/*
d0d856e8
RD
1051 * ext4_ext_grow_indepth:
1052 * implements tree growing procedure:
1053 * - allocates new block
1054 * - moves top-level data (index block or leaf) into the new block
1055 * - initializes new top-level, creating index that points to the
1056 * just created block
a86c6181
AT
1057 */
1058static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
1059 struct ext4_ext_path *path,
1060 struct ext4_extent *newext)
1061{
1062 struct ext4_ext_path *curp = path;
1063 struct ext4_extent_header *neh;
a86c6181 1064 struct buffer_head *bh;
f65e6fba 1065 ext4_fsblk_t newblock;
a86c6181
AT
1066 int err = 0;
1067
654b4908 1068 newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
a86c6181
AT
1069 if (newblock == 0)
1070 return err;
1071
1072 bh = sb_getblk(inode->i_sb, newblock);
1073 if (!bh) {
1074 err = -EIO;
1075 ext4_std_error(inode->i_sb, err);
1076 return err;
1077 }
1078 lock_buffer(bh);
1079
7e028976
AM
1080 err = ext4_journal_get_create_access(handle, bh);
1081 if (err) {
a86c6181
AT
1082 unlock_buffer(bh);
1083 goto out;
1084 }
1085
1086 /* move top-level index/leaf into new block */
1087 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
1088
1089 /* set size of new block */
1090 neh = ext_block_hdr(bh);
1091 /* old root could have indexes or leaves
1092 * so calculate e_max right way */
1093 if (ext_depth(inode))
55ad63bf 1094 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
a86c6181 1095 else
55ad63bf 1096 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
a86c6181
AT
1097 neh->eh_magic = EXT4_EXT_MAGIC;
1098 set_buffer_uptodate(bh);
1099 unlock_buffer(bh);
1100
0390131b 1101 err = ext4_handle_dirty_metadata(handle, inode, bh);
7e028976 1102 if (err)
a86c6181
AT
1103 goto out;
1104
1105 /* create index in new top-level index: num,max,pointer */
7e028976
AM
1106 err = ext4_ext_get_access(handle, inode, curp);
1107 if (err)
a86c6181
AT
1108 goto out;
1109
1110 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
55ad63bf 1111 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
a86c6181
AT
1112 curp->p_hdr->eh_entries = cpu_to_le16(1);
1113 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
e9f410b1
DM
1114
1115 if (path[0].p_hdr->eh_depth)
1116 curp->p_idx->ei_block =
1117 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
1118 else
1119 curp->p_idx->ei_block =
1120 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
f65e6fba 1121 ext4_idx_store_pblock(curp->p_idx, newblock);
a86c6181
AT
1122
1123 neh = ext_inode_hdr(inode);
2ae02107 1124 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
a86c6181 1125 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
5a0790c2 1126 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
bf89d16f 1127 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
a86c6181
AT
1128
1129 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
1130 err = ext4_ext_dirty(handle, inode, curp);
1131out:
1132 brelse(bh);
1133
1134 return err;
1135}
1136
1137/*
d0d856e8
RD
1138 * ext4_ext_create_new_leaf:
1139 * finds empty index and adds new leaf.
1140 * if no free index is found, then it requests in-depth growing.
a86c6181
AT
1141 */
1142static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
1143 struct ext4_ext_path *path,
1144 struct ext4_extent *newext)
1145{
1146 struct ext4_ext_path *curp;
1147 int depth, i, err = 0;
1148
1149repeat:
1150 i = depth = ext_depth(inode);
1151
1152 /* walk up to the tree and look for free index entry */
1153 curp = path + depth;
1154 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1155 i--;
1156 curp--;
1157 }
1158
d0d856e8
RD
1159 /* we use already allocated block for index block,
1160 * so subsequent data blocks should be contiguous */
a86c6181
AT
1161 if (EXT_HAS_FREE_INDEX(curp)) {
1162 /* if we found index with free entry, then use that
1163 * entry: create all needed subtree and add new leaf */
1164 err = ext4_ext_split(handle, inode, path, newext, i);
787e0981
SF
1165 if (err)
1166 goto out;
a86c6181
AT
1167
1168 /* refill path */
1169 ext4_ext_drop_refs(path);
1170 path = ext4_ext_find_extent(inode,
725d26d3
AK
1171 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1172 path);
a86c6181
AT
1173 if (IS_ERR(path))
1174 err = PTR_ERR(path);
1175 } else {
1176 /* tree is full, time to grow in depth */
1177 err = ext4_ext_grow_indepth(handle, inode, path, newext);
1178 if (err)
1179 goto out;
1180
1181 /* refill path */
1182 ext4_ext_drop_refs(path);
1183 path = ext4_ext_find_extent(inode,
725d26d3
AK
1184 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1185 path);
a86c6181
AT
1186 if (IS_ERR(path)) {
1187 err = PTR_ERR(path);
1188 goto out;
1189 }
1190
1191 /*
d0d856e8
RD
1192 * only first (depth 0 -> 1) produces free space;
1193 * in all other cases we have to split the grown tree
a86c6181
AT
1194 */
1195 depth = ext_depth(inode);
1196 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
d0d856e8 1197 /* now we need to split */
a86c6181
AT
1198 goto repeat;
1199 }
1200 }
1201
1202out:
1203 return err;
1204}
1205
1988b51e
AT
1206/*
1207 * search the closest allocated block to the left for *logical
1208 * and returns it at @logical + it's physical address at @phys
1209 * if *logical is the smallest allocated block, the function
1210 * returns 0 at @phys
1211 * return value contains 0 (success) or error code
1212 */
1f109d5a
TT
1213static int ext4_ext_search_left(struct inode *inode,
1214 struct ext4_ext_path *path,
1215 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1988b51e
AT
1216{
1217 struct ext4_extent_idx *ix;
1218 struct ext4_extent *ex;
b939e376 1219 int depth, ee_len;
1988b51e 1220
273df556
FM
1221 if (unlikely(path == NULL)) {
1222 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1223 return -EIO;
1224 }
1988b51e
AT
1225 depth = path->p_depth;
1226 *phys = 0;
1227
1228 if (depth == 0 && path->p_ext == NULL)
1229 return 0;
1230
1231 /* usually extent in the path covers blocks smaller
1232 * then *logical, but it can be that extent is the
1233 * first one in the file */
1234
1235 ex = path[depth].p_ext;
b939e376 1236 ee_len = ext4_ext_get_actual_len(ex);
1988b51e 1237 if (*logical < le32_to_cpu(ex->ee_block)) {
273df556
FM
1238 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1239 EXT4_ERROR_INODE(inode,
1240 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1241 *logical, le32_to_cpu(ex->ee_block));
1242 return -EIO;
1243 }
1988b51e
AT
1244 while (--depth >= 0) {
1245 ix = path[depth].p_idx;
273df556
FM
1246 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1247 EXT4_ERROR_INODE(inode,
1248 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
1249 ix != NULL ? ix->ei_block : 0,
1250 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
1251 EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block : 0,
1252 depth);
1253 return -EIO;
1254 }
1988b51e
AT
1255 }
1256 return 0;
1257 }
1258
273df556
FM
1259 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1260 EXT4_ERROR_INODE(inode,
1261 "logical %d < ee_block %d + ee_len %d!",
1262 *logical, le32_to_cpu(ex->ee_block), ee_len);
1263 return -EIO;
1264 }
1988b51e 1265
b939e376 1266 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
bf89d16f 1267 *phys = ext4_ext_pblock(ex) + ee_len - 1;
1988b51e
AT
1268 return 0;
1269}
1270
1271/*
1272 * search the closest allocated block to the right for *logical
1273 * and returns it at @logical + it's physical address at @phys
1274 * if *logical is the smallest allocated block, the function
1275 * returns 0 at @phys
1276 * return value contains 0 (success) or error code
1277 */
1f109d5a
TT
1278static int ext4_ext_search_right(struct inode *inode,
1279 struct ext4_ext_path *path,
1280 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1988b51e
AT
1281{
1282 struct buffer_head *bh = NULL;
1283 struct ext4_extent_header *eh;
1284 struct ext4_extent_idx *ix;
1285 struct ext4_extent *ex;
1286 ext4_fsblk_t block;
395a87bf
ES
1287 int depth; /* Note, NOT eh_depth; depth from top of tree */
1288 int ee_len;
1988b51e 1289
273df556
FM
1290 if (unlikely(path == NULL)) {
1291 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1292 return -EIO;
1293 }
1988b51e
AT
1294 depth = path->p_depth;
1295 *phys = 0;
1296
1297 if (depth == 0 && path->p_ext == NULL)
1298 return 0;
1299
1300 /* usually extent in the path covers blocks smaller
1301 * then *logical, but it can be that extent is the
1302 * first one in the file */
1303
1304 ex = path[depth].p_ext;
b939e376 1305 ee_len = ext4_ext_get_actual_len(ex);
1988b51e 1306 if (*logical < le32_to_cpu(ex->ee_block)) {
273df556
FM
1307 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1308 EXT4_ERROR_INODE(inode,
1309 "first_extent(path[%d].p_hdr) != ex",
1310 depth);
1311 return -EIO;
1312 }
1988b51e
AT
1313 while (--depth >= 0) {
1314 ix = path[depth].p_idx;
273df556
FM
1315 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1316 EXT4_ERROR_INODE(inode,
1317 "ix != EXT_FIRST_INDEX *logical %d!",
1318 *logical);
1319 return -EIO;
1320 }
1988b51e
AT
1321 }
1322 *logical = le32_to_cpu(ex->ee_block);
bf89d16f 1323 *phys = ext4_ext_pblock(ex);
1988b51e
AT
1324 return 0;
1325 }
1326
273df556
FM
1327 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1328 EXT4_ERROR_INODE(inode,
1329 "logical %d < ee_block %d + ee_len %d!",
1330 *logical, le32_to_cpu(ex->ee_block), ee_len);
1331 return -EIO;
1332 }
1988b51e
AT
1333
1334 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1335 /* next allocated block in this leaf */
1336 ex++;
1337 *logical = le32_to_cpu(ex->ee_block);
bf89d16f 1338 *phys = ext4_ext_pblock(ex);
1988b51e
AT
1339 return 0;
1340 }
1341
1342 /* go up and search for index to the right */
1343 while (--depth >= 0) {
1344 ix = path[depth].p_idx;
1345 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
25f1ee3a 1346 goto got_index;
1988b51e
AT
1347 }
1348
25f1ee3a
WF
1349 /* we've gone up to the root and found no index to the right */
1350 return 0;
1988b51e 1351
25f1ee3a 1352got_index:
1988b51e
AT
1353 /* we've found index to the right, let's
1354 * follow it and find the closest allocated
1355 * block to the right */
1356 ix++;
bf89d16f 1357 block = ext4_idx_pblock(ix);
1988b51e
AT
1358 while (++depth < path->p_depth) {
1359 bh = sb_bread(inode->i_sb, block);
1360 if (bh == NULL)
1361 return -EIO;
1362 eh = ext_block_hdr(bh);
395a87bf 1363 /* subtract from p_depth to get proper eh_depth */
56b19868 1364 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
1988b51e
AT
1365 put_bh(bh);
1366 return -EIO;
1367 }
1368 ix = EXT_FIRST_INDEX(eh);
bf89d16f 1369 block = ext4_idx_pblock(ix);
1988b51e
AT
1370 put_bh(bh);
1371 }
1372
1373 bh = sb_bread(inode->i_sb, block);
1374 if (bh == NULL)
1375 return -EIO;
1376 eh = ext_block_hdr(bh);
56b19868 1377 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
1988b51e
AT
1378 put_bh(bh);
1379 return -EIO;
1380 }
1381 ex = EXT_FIRST_EXTENT(eh);
1382 *logical = le32_to_cpu(ex->ee_block);
bf89d16f 1383 *phys = ext4_ext_pblock(ex);
1988b51e
AT
1384 put_bh(bh);
1385 return 0;
1988b51e
AT
1386}
1387
a86c6181 1388/*
d0d856e8
RD
1389 * ext4_ext_next_allocated_block:
1390 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1391 * NOTE: it considers block number from index entry as
1392 * allocated block. Thus, index entries have to be consistent
1393 * with leaves.
a86c6181 1394 */
725d26d3 1395static ext4_lblk_t
a86c6181
AT
1396ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1397{
1398 int depth;
1399
1400 BUG_ON(path == NULL);
1401 depth = path->p_depth;
1402
1403 if (depth == 0 && path->p_ext == NULL)
1404 return EXT_MAX_BLOCK;
1405
1406 while (depth >= 0) {
1407 if (depth == path->p_depth) {
1408 /* leaf */
1409 if (path[depth].p_ext !=
1410 EXT_LAST_EXTENT(path[depth].p_hdr))
1411 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1412 } else {
1413 /* index */
1414 if (path[depth].p_idx !=
1415 EXT_LAST_INDEX(path[depth].p_hdr))
1416 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1417 }
1418 depth--;
1419 }
1420
1421 return EXT_MAX_BLOCK;
1422}
1423
1424/*
d0d856e8 1425 * ext4_ext_next_leaf_block:
a86c6181
AT
1426 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1427 */
725d26d3 1428static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
63f57933 1429 struct ext4_ext_path *path)
a86c6181
AT
1430{
1431 int depth;
1432
1433 BUG_ON(path == NULL);
1434 depth = path->p_depth;
1435
1436 /* zero-tree has no leaf blocks at all */
1437 if (depth == 0)
1438 return EXT_MAX_BLOCK;
1439
1440 /* go to index block */
1441 depth--;
1442
1443 while (depth >= 0) {
1444 if (path[depth].p_idx !=
1445 EXT_LAST_INDEX(path[depth].p_hdr))
725d26d3
AK
1446 return (ext4_lblk_t)
1447 le32_to_cpu(path[depth].p_idx[1].ei_block);
a86c6181
AT
1448 depth--;
1449 }
1450
1451 return EXT_MAX_BLOCK;
1452}
1453
1454/*
d0d856e8
RD
1455 * ext4_ext_correct_indexes:
1456 * if leaf gets modified and modified extent is first in the leaf,
1457 * then we have to correct all indexes above.
a86c6181
AT
1458 * TODO: do we need to correct tree in all cases?
1459 */
1d03ec98 1460static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
a86c6181
AT
1461 struct ext4_ext_path *path)
1462{
1463 struct ext4_extent_header *eh;
1464 int depth = ext_depth(inode);
1465 struct ext4_extent *ex;
1466 __le32 border;
1467 int k, err = 0;
1468
1469 eh = path[depth].p_hdr;
1470 ex = path[depth].p_ext;
273df556
FM
1471
1472 if (unlikely(ex == NULL || eh == NULL)) {
1473 EXT4_ERROR_INODE(inode,
1474 "ex %p == NULL or eh %p == NULL", ex, eh);
1475 return -EIO;
1476 }
a86c6181
AT
1477
1478 if (depth == 0) {
1479 /* there is no tree at all */
1480 return 0;
1481 }
1482
1483 if (ex != EXT_FIRST_EXTENT(eh)) {
1484 /* we correct tree if first leaf got modified only */
1485 return 0;
1486 }
1487
1488 /*
d0d856e8 1489 * TODO: we need correction if border is smaller than current one
a86c6181
AT
1490 */
1491 k = depth - 1;
1492 border = path[depth].p_ext->ee_block;
7e028976
AM
1493 err = ext4_ext_get_access(handle, inode, path + k);
1494 if (err)
a86c6181
AT
1495 return err;
1496 path[k].p_idx->ei_block = border;
7e028976
AM
1497 err = ext4_ext_dirty(handle, inode, path + k);
1498 if (err)
a86c6181
AT
1499 return err;
1500
1501 while (k--) {
1502 /* change all left-side indexes */
1503 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1504 break;
7e028976
AM
1505 err = ext4_ext_get_access(handle, inode, path + k);
1506 if (err)
a86c6181
AT
1507 break;
1508 path[k].p_idx->ei_block = border;
7e028976
AM
1509 err = ext4_ext_dirty(handle, inode, path + k);
1510 if (err)
a86c6181
AT
1511 break;
1512 }
1513
1514 return err;
1515}
1516
748de673 1517int
a86c6181
AT
1518ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1519 struct ext4_extent *ex2)
1520{
749269fa 1521 unsigned short ext1_ee_len, ext2_ee_len, max_len;
a2df2a63
AA
1522
1523 /*
1524 * Make sure that either both extents are uninitialized, or
1525 * both are _not_.
1526 */
1527 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1528 return 0;
1529
749269fa
AA
1530 if (ext4_ext_is_uninitialized(ex1))
1531 max_len = EXT_UNINIT_MAX_LEN;
1532 else
1533 max_len = EXT_INIT_MAX_LEN;
1534
a2df2a63
AA
1535 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1536 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1537
1538 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
63f57933 1539 le32_to_cpu(ex2->ee_block))
a86c6181
AT
1540 return 0;
1541
471d4011
SB
1542 /*
1543 * To allow future support for preallocated extents to be added
1544 * as an RO_COMPAT feature, refuse to merge to extents if
d0d856e8 1545 * this can result in the top bit of ee_len being set.
471d4011 1546 */
749269fa 1547 if (ext1_ee_len + ext2_ee_len > max_len)
471d4011 1548 return 0;
bbf2f9fb 1549#ifdef AGGRESSIVE_TEST
b939e376 1550 if (ext1_ee_len >= 4)
a86c6181
AT
1551 return 0;
1552#endif
1553
bf89d16f 1554 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
a86c6181
AT
1555 return 1;
1556 return 0;
1557}
1558
56055d3a
AA
1559/*
1560 * This function tries to merge the "ex" extent to the next extent in the tree.
1561 * It always tries to merge towards right. If you want to merge towards
1562 * left, pass "ex - 1" as argument instead of "ex".
1563 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1564 * 1 if they got merged.
1565 */
197217a5 1566static int ext4_ext_try_to_merge_right(struct inode *inode,
1f109d5a
TT
1567 struct ext4_ext_path *path,
1568 struct ext4_extent *ex)
56055d3a
AA
1569{
1570 struct ext4_extent_header *eh;
1571 unsigned int depth, len;
1572 int merge_done = 0;
1573 int uninitialized = 0;
1574
1575 depth = ext_depth(inode);
1576 BUG_ON(path[depth].p_hdr == NULL);
1577 eh = path[depth].p_hdr;
1578
1579 while (ex < EXT_LAST_EXTENT(eh)) {
1580 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1581 break;
1582 /* merge with next extent! */
1583 if (ext4_ext_is_uninitialized(ex))
1584 uninitialized = 1;
1585 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1586 + ext4_ext_get_actual_len(ex + 1));
1587 if (uninitialized)
1588 ext4_ext_mark_uninitialized(ex);
1589
1590 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1591 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1592 * sizeof(struct ext4_extent);
1593 memmove(ex + 1, ex + 2, len);
1594 }
e8546d06 1595 le16_add_cpu(&eh->eh_entries, -1);
56055d3a
AA
1596 merge_done = 1;
1597 WARN_ON(eh->eh_entries == 0);
1598 if (!eh->eh_entries)
24676da4 1599 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
56055d3a
AA
1600 }
1601
1602 return merge_done;
1603}
1604
197217a5
YY
1605/*
1606 * This function tries to merge the @ex extent to neighbours in the tree.
1607 * return 1 if merge left else 0.
1608 */
1609static int ext4_ext_try_to_merge(struct inode *inode,
1610 struct ext4_ext_path *path,
1611 struct ext4_extent *ex) {
1612 struct ext4_extent_header *eh;
1613 unsigned int depth;
1614 int merge_done = 0;
1615 int ret = 0;
1616
1617 depth = ext_depth(inode);
1618 BUG_ON(path[depth].p_hdr == NULL);
1619 eh = path[depth].p_hdr;
1620
1621 if (ex > EXT_FIRST_EXTENT(eh))
1622 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1623
1624 if (!merge_done)
1625 ret = ext4_ext_try_to_merge_right(inode, path, ex);
1626
1627 return ret;
1628}
1629
25d14f98
AA
1630/*
1631 * check if a portion of the "newext" extent overlaps with an
1632 * existing extent.
1633 *
1634 * If there is an overlap discovered, it updates the length of the newext
1635 * such that there will be no overlap, and then returns 1.
1636 * If there is no overlap found, it returns 0.
1637 */
1f109d5a
TT
1638static unsigned int ext4_ext_check_overlap(struct inode *inode,
1639 struct ext4_extent *newext,
1640 struct ext4_ext_path *path)
25d14f98 1641{
725d26d3 1642 ext4_lblk_t b1, b2;
25d14f98
AA
1643 unsigned int depth, len1;
1644 unsigned int ret = 0;
1645
1646 b1 = le32_to_cpu(newext->ee_block);
a2df2a63 1647 len1 = ext4_ext_get_actual_len(newext);
25d14f98
AA
1648 depth = ext_depth(inode);
1649 if (!path[depth].p_ext)
1650 goto out;
1651 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1652
1653 /*
1654 * get the next allocated block if the extent in the path
2b2d6d01 1655 * is before the requested block(s)
25d14f98
AA
1656 */
1657 if (b2 < b1) {
1658 b2 = ext4_ext_next_allocated_block(path);
1659 if (b2 == EXT_MAX_BLOCK)
1660 goto out;
1661 }
1662
725d26d3 1663 /* check for wrap through zero on extent logical start block*/
25d14f98
AA
1664 if (b1 + len1 < b1) {
1665 len1 = EXT_MAX_BLOCK - b1;
1666 newext->ee_len = cpu_to_le16(len1);
1667 ret = 1;
1668 }
1669
1670 /* check for overlap */
1671 if (b1 + len1 > b2) {
1672 newext->ee_len = cpu_to_le16(b2 - b1);
1673 ret = 1;
1674 }
1675out:
1676 return ret;
1677}
1678
a86c6181 1679/*
d0d856e8
RD
1680 * ext4_ext_insert_extent:
1681 * tries to merge requsted extent into the existing extent or
1682 * inserts requested extent as new one into the tree,
1683 * creating new leaf in the no-space case.
a86c6181
AT
1684 */
1685int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1686 struct ext4_ext_path *path,
0031462b 1687 struct ext4_extent *newext, int flag)
a86c6181 1688{
af5bc92d 1689 struct ext4_extent_header *eh;
a86c6181
AT
1690 struct ext4_extent *ex, *fex;
1691 struct ext4_extent *nearex; /* nearest extent */
1692 struct ext4_ext_path *npath = NULL;
725d26d3
AK
1693 int depth, len, err;
1694 ext4_lblk_t next;
a2df2a63 1695 unsigned uninitialized = 0;
a86c6181 1696
273df556
FM
1697 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1698 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1699 return -EIO;
1700 }
a86c6181
AT
1701 depth = ext_depth(inode);
1702 ex = path[depth].p_ext;
273df556
FM
1703 if (unlikely(path[depth].p_hdr == NULL)) {
1704 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1705 return -EIO;
1706 }
a86c6181
AT
1707
1708 /* try to insert block into found extent and return */
744692dc 1709 if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
0031462b 1710 && ext4_can_extents_be_merged(inode, ex, newext)) {
553f9008 1711 ext_debug("append [%d]%d block to %d:[%d]%d (from %llu)\n",
bf89d16f
TT
1712 ext4_ext_is_uninitialized(newext),
1713 ext4_ext_get_actual_len(newext),
1714 le32_to_cpu(ex->ee_block),
1715 ext4_ext_is_uninitialized(ex),
1716 ext4_ext_get_actual_len(ex),
1717 ext4_ext_pblock(ex));
7e028976
AM
1718 err = ext4_ext_get_access(handle, inode, path + depth);
1719 if (err)
a86c6181 1720 return err;
a2df2a63
AA
1721
1722 /*
1723 * ext4_can_extents_be_merged should have checked that either
1724 * both extents are uninitialized, or both aren't. Thus we
1725 * need to check only one of them here.
1726 */
1727 if (ext4_ext_is_uninitialized(ex))
1728 uninitialized = 1;
1729 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1730 + ext4_ext_get_actual_len(newext));
1731 if (uninitialized)
1732 ext4_ext_mark_uninitialized(ex);
a86c6181
AT
1733 eh = path[depth].p_hdr;
1734 nearex = ex;
1735 goto merge;
1736 }
1737
1738repeat:
1739 depth = ext_depth(inode);
1740 eh = path[depth].p_hdr;
1741 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1742 goto has_space;
1743
1744 /* probably next leaf has space for us? */
1745 fex = EXT_LAST_EXTENT(eh);
1746 next = ext4_ext_next_leaf_block(inode, path);
1747 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1748 && next != EXT_MAX_BLOCK) {
1749 ext_debug("next leaf block - %d\n", next);
1750 BUG_ON(npath != NULL);
1751 npath = ext4_ext_find_extent(inode, next, NULL);
1752 if (IS_ERR(npath))
1753 return PTR_ERR(npath);
1754 BUG_ON(npath->p_depth != path->p_depth);
1755 eh = npath[depth].p_hdr;
1756 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
25985edc 1757 ext_debug("next leaf isn't full(%d)\n",
a86c6181
AT
1758 le16_to_cpu(eh->eh_entries));
1759 path = npath;
1760 goto repeat;
1761 }
1762 ext_debug("next leaf has no free space(%d,%d)\n",
1763 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1764 }
1765
1766 /*
d0d856e8
RD
1767 * There is no free space in the found leaf.
1768 * We're gonna add a new leaf in the tree.
a86c6181
AT
1769 */
1770 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1771 if (err)
1772 goto cleanup;
1773 depth = ext_depth(inode);
1774 eh = path[depth].p_hdr;
1775
1776has_space:
1777 nearex = path[depth].p_ext;
1778
7e028976
AM
1779 err = ext4_ext_get_access(handle, inode, path + depth);
1780 if (err)
a86c6181
AT
1781 goto cleanup;
1782
1783 if (!nearex) {
1784 /* there is no extent in this leaf, create first one */
553f9008 1785 ext_debug("first extent in the leaf: %d:%llu:[%d]%d\n",
8c55e204 1786 le32_to_cpu(newext->ee_block),
bf89d16f 1787 ext4_ext_pblock(newext),
553f9008 1788 ext4_ext_is_uninitialized(newext),
a2df2a63 1789 ext4_ext_get_actual_len(newext));
a86c6181
AT
1790 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1791 } else if (le32_to_cpu(newext->ee_block)
8c55e204 1792 > le32_to_cpu(nearex->ee_block)) {
a86c6181
AT
1793/* BUG_ON(newext->ee_block == nearex->ee_block); */
1794 if (nearex != EXT_LAST_EXTENT(eh)) {
1795 len = EXT_MAX_EXTENT(eh) - nearex;
1796 len = (len - 1) * sizeof(struct ext4_extent);
1797 len = len < 0 ? 0 : len;
553f9008 1798 ext_debug("insert %d:%llu:[%d]%d after: nearest 0x%p, "
a86c6181 1799 "move %d from 0x%p to 0x%p\n",
8c55e204 1800 le32_to_cpu(newext->ee_block),
bf89d16f 1801 ext4_ext_pblock(newext),
553f9008 1802 ext4_ext_is_uninitialized(newext),
a2df2a63 1803 ext4_ext_get_actual_len(newext),
a86c6181
AT
1804 nearex, len, nearex + 1, nearex + 2);
1805 memmove(nearex + 2, nearex + 1, len);
1806 }
1807 path[depth].p_ext = nearex + 1;
1808 } else {
1809 BUG_ON(newext->ee_block == nearex->ee_block);
1810 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1811 len = len < 0 ? 0 : len;
553f9008 1812 ext_debug("insert %d:%llu:[%d]%d before: nearest 0x%p, "
a86c6181
AT
1813 "move %d from 0x%p to 0x%p\n",
1814 le32_to_cpu(newext->ee_block),
bf89d16f 1815 ext4_ext_pblock(newext),
553f9008 1816 ext4_ext_is_uninitialized(newext),
a2df2a63 1817 ext4_ext_get_actual_len(newext),
a86c6181
AT
1818 nearex, len, nearex + 1, nearex + 2);
1819 memmove(nearex + 1, nearex, len);
1820 path[depth].p_ext = nearex;
1821 }
1822
e8546d06 1823 le16_add_cpu(&eh->eh_entries, 1);
a86c6181
AT
1824 nearex = path[depth].p_ext;
1825 nearex->ee_block = newext->ee_block;
bf89d16f 1826 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
a86c6181 1827 nearex->ee_len = newext->ee_len;
a86c6181
AT
1828
1829merge:
1830 /* try to merge extents to the right */
744692dc 1831 if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
0031462b 1832 ext4_ext_try_to_merge(inode, path, nearex);
a86c6181
AT
1833
1834 /* try to merge extents to the left */
1835
1836 /* time to correct all indexes above */
1837 err = ext4_ext_correct_indexes(handle, inode, path);
1838 if (err)
1839 goto cleanup;
1840
1841 err = ext4_ext_dirty(handle, inode, path + depth);
1842
1843cleanup:
1844 if (npath) {
1845 ext4_ext_drop_refs(npath);
1846 kfree(npath);
1847 }
a86c6181
AT
1848 ext4_ext_invalidate_cache(inode);
1849 return err;
1850}
1851
1f109d5a
TT
1852static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1853 ext4_lblk_t num, ext_prepare_callback func,
1854 void *cbdata)
6873fa0d
ES
1855{
1856 struct ext4_ext_path *path = NULL;
1857 struct ext4_ext_cache cbex;
1858 struct ext4_extent *ex;
1859 ext4_lblk_t next, start = 0, end = 0;
1860 ext4_lblk_t last = block + num;
1861 int depth, exists, err = 0;
1862
1863 BUG_ON(func == NULL);
1864 BUG_ON(inode == NULL);
1865
1866 while (block < last && block != EXT_MAX_BLOCK) {
1867 num = last - block;
1868 /* find extent for this block */
fab3a549 1869 down_read(&EXT4_I(inode)->i_data_sem);
6873fa0d 1870 path = ext4_ext_find_extent(inode, block, path);
fab3a549 1871 up_read(&EXT4_I(inode)->i_data_sem);
6873fa0d
ES
1872 if (IS_ERR(path)) {
1873 err = PTR_ERR(path);
1874 path = NULL;
1875 break;
1876 }
1877
1878 depth = ext_depth(inode);
273df556
FM
1879 if (unlikely(path[depth].p_hdr == NULL)) {
1880 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1881 err = -EIO;
1882 break;
1883 }
6873fa0d
ES
1884 ex = path[depth].p_ext;
1885 next = ext4_ext_next_allocated_block(path);
1886
1887 exists = 0;
1888 if (!ex) {
1889 /* there is no extent yet, so try to allocate
1890 * all requested space */
1891 start = block;
1892 end = block + num;
1893 } else if (le32_to_cpu(ex->ee_block) > block) {
1894 /* need to allocate space before found extent */
1895 start = block;
1896 end = le32_to_cpu(ex->ee_block);
1897 if (block + num < end)
1898 end = block + num;
1899 } else if (block >= le32_to_cpu(ex->ee_block)
1900 + ext4_ext_get_actual_len(ex)) {
1901 /* need to allocate space after found extent */
1902 start = block;
1903 end = block + num;
1904 if (end >= next)
1905 end = next;
1906 } else if (block >= le32_to_cpu(ex->ee_block)) {
1907 /*
1908 * some part of requested space is covered
1909 * by found extent
1910 */
1911 start = block;
1912 end = le32_to_cpu(ex->ee_block)
1913 + ext4_ext_get_actual_len(ex);
1914 if (block + num < end)
1915 end = block + num;
1916 exists = 1;
1917 } else {
1918 BUG();
1919 }
1920 BUG_ON(end <= start);
1921
1922 if (!exists) {
1923 cbex.ec_block = start;
1924 cbex.ec_len = end - start;
1925 cbex.ec_start = 0;
6873fa0d
ES
1926 } else {
1927 cbex.ec_block = le32_to_cpu(ex->ee_block);
1928 cbex.ec_len = ext4_ext_get_actual_len(ex);
bf89d16f 1929 cbex.ec_start = ext4_ext_pblock(ex);
6873fa0d
ES
1930 }
1931
273df556
FM
1932 if (unlikely(cbex.ec_len == 0)) {
1933 EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
1934 err = -EIO;
1935 break;
1936 }
6873fa0d
ES
1937 err = func(inode, path, &cbex, ex, cbdata);
1938 ext4_ext_drop_refs(path);
1939
1940 if (err < 0)
1941 break;
1942
1943 if (err == EXT_REPEAT)
1944 continue;
1945 else if (err == EXT_BREAK) {
1946 err = 0;
1947 break;
1948 }
1949
1950 if (ext_depth(inode) != depth) {
1951 /* depth was changed. we have to realloc path */
1952 kfree(path);
1953 path = NULL;
1954 }
1955
1956 block = cbex.ec_block + cbex.ec_len;
1957 }
1958
1959 if (path) {
1960 ext4_ext_drop_refs(path);
1961 kfree(path);
1962 }
1963
1964 return err;
1965}
1966
09b88252 1967static void
725d26d3 1968ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
b05e6ae5 1969 __u32 len, ext4_fsblk_t start)
a86c6181
AT
1970{
1971 struct ext4_ext_cache *cex;
1972 BUG_ON(len == 0);
2ec0ae3a 1973 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
a86c6181 1974 cex = &EXT4_I(inode)->i_cached_extent;
a86c6181
AT
1975 cex->ec_block = block;
1976 cex->ec_len = len;
1977 cex->ec_start = start;
2ec0ae3a 1978 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
a86c6181
AT
1979}
1980
1981/*
d0d856e8
RD
1982 * ext4_ext_put_gap_in_cache:
1983 * calculate boundaries of the gap that the requested block fits into
a86c6181
AT
1984 * and cache this gap
1985 */
09b88252 1986static void
a86c6181 1987ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
725d26d3 1988 ext4_lblk_t block)
a86c6181
AT
1989{
1990 int depth = ext_depth(inode);
725d26d3
AK
1991 unsigned long len;
1992 ext4_lblk_t lblock;
a86c6181
AT
1993 struct ext4_extent *ex;
1994
1995 ex = path[depth].p_ext;
1996 if (ex == NULL) {
1997 /* there is no extent yet, so gap is [0;-] */
1998 lblock = 0;
1999 len = EXT_MAX_BLOCK;
2000 ext_debug("cache gap(whole file):");
2001 } else if (block < le32_to_cpu(ex->ee_block)) {
2002 lblock = block;
2003 len = le32_to_cpu(ex->ee_block) - block;
bba90743
ES
2004 ext_debug("cache gap(before): %u [%u:%u]",
2005 block,
2006 le32_to_cpu(ex->ee_block),
2007 ext4_ext_get_actual_len(ex));
a86c6181 2008 } else if (block >= le32_to_cpu(ex->ee_block)
a2df2a63 2009 + ext4_ext_get_actual_len(ex)) {
725d26d3 2010 ext4_lblk_t next;
8c55e204 2011 lblock = le32_to_cpu(ex->ee_block)
a2df2a63 2012 + ext4_ext_get_actual_len(ex);
725d26d3
AK
2013
2014 next = ext4_ext_next_allocated_block(path);
bba90743
ES
2015 ext_debug("cache gap(after): [%u:%u] %u",
2016 le32_to_cpu(ex->ee_block),
2017 ext4_ext_get_actual_len(ex),
2018 block);
725d26d3
AK
2019 BUG_ON(next == lblock);
2020 len = next - lblock;
a86c6181
AT
2021 } else {
2022 lblock = len = 0;
2023 BUG();
2024 }
2025
bba90743 2026 ext_debug(" -> %u:%lu\n", lblock, len);
b05e6ae5 2027 ext4_ext_put_in_cache(inode, lblock, len, 0);
a86c6181
AT
2028}
2029
b05e6ae5
TT
2030/*
2031 * Return 0 if cache is invalid; 1 if the cache is valid
2032 */
09b88252 2033static int
725d26d3 2034ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
a86c6181
AT
2035 struct ext4_extent *ex)
2036{
2037 struct ext4_ext_cache *cex;
b05e6ae5 2038 int ret = 0;
a86c6181 2039
60e6679e 2040 /*
2ec0ae3a
TT
2041 * We borrow i_block_reservation_lock to protect i_cached_extent
2042 */
2043 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
a86c6181
AT
2044 cex = &EXT4_I(inode)->i_cached_extent;
2045
2046 /* has cache valid data? */
b05e6ae5 2047 if (cex->ec_len == 0)
2ec0ae3a 2048 goto errout;
a86c6181 2049
731eb1a0 2050 if (in_range(block, cex->ec_block, cex->ec_len)) {
8c55e204 2051 ex->ee_block = cpu_to_le32(cex->ec_block);
f65e6fba 2052 ext4_ext_store_pblock(ex, cex->ec_start);
8c55e204 2053 ex->ee_len = cpu_to_le16(cex->ec_len);
bba90743
ES
2054 ext_debug("%u cached by %u:%u:%llu\n",
2055 block,
2056 cex->ec_block, cex->ec_len, cex->ec_start);
b05e6ae5 2057 ret = 1;
a86c6181 2058 }
2ec0ae3a
TT
2059errout:
2060 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
2061 return ret;
a86c6181
AT
2062}
2063
2064/*
d0d856e8
RD
2065 * ext4_ext_rm_idx:
2066 * removes index from the index block.
2067 * It's used in truncate case only, thus all requests are for
2068 * last index in the block only.
a86c6181 2069 */
1d03ec98 2070static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
a86c6181
AT
2071 struct ext4_ext_path *path)
2072{
a86c6181 2073 int err;
f65e6fba 2074 ext4_fsblk_t leaf;
a86c6181
AT
2075
2076 /* free index block */
2077 path--;
bf89d16f 2078 leaf = ext4_idx_pblock(path->p_idx);
273df556
FM
2079 if (unlikely(path->p_hdr->eh_entries == 0)) {
2080 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2081 return -EIO;
2082 }
7e028976
AM
2083 err = ext4_ext_get_access(handle, inode, path);
2084 if (err)
a86c6181 2085 return err;
e8546d06 2086 le16_add_cpu(&path->p_hdr->eh_entries, -1);
7e028976
AM
2087 err = ext4_ext_dirty(handle, inode, path);
2088 if (err)
a86c6181 2089 return err;
2ae02107 2090 ext_debug("index is empty, remove it, free block %llu\n", leaf);
7dc57615 2091 ext4_free_blocks(handle, inode, NULL, leaf, 1,
e6362609 2092 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
a86c6181
AT
2093 return err;
2094}
2095
2096/*
ee12b630
MC
2097 * ext4_ext_calc_credits_for_single_extent:
2098 * This routine returns max. credits that needed to insert an extent
2099 * to the extent tree.
2100 * When pass the actual path, the caller should calculate credits
2101 * under i_data_sem.
a86c6181 2102 */
525f4ed8 2103int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
a86c6181
AT
2104 struct ext4_ext_path *path)
2105{
a86c6181 2106 if (path) {
ee12b630 2107 int depth = ext_depth(inode);
f3bd1f3f 2108 int ret = 0;
ee12b630 2109
a86c6181 2110 /* probably there is space in leaf? */
a86c6181 2111 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
ee12b630 2112 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
a86c6181 2113
ee12b630
MC
2114 /*
2115 * There are some space in the leaf tree, no
2116 * need to account for leaf block credit
2117 *
2118 * bitmaps and block group descriptor blocks
2119 * and other metadat blocks still need to be
2120 * accounted.
2121 */
525f4ed8 2122 /* 1 bitmap, 1 block group descriptor */
ee12b630 2123 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
5887e98b 2124 return ret;
ee12b630
MC
2125 }
2126 }
a86c6181 2127
525f4ed8 2128 return ext4_chunk_trans_blocks(inode, nrblocks);
ee12b630 2129}
a86c6181 2130
ee12b630
MC
2131/*
2132 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2133 *
2134 * if nrblocks are fit in a single extent (chunk flag is 1), then
2135 * in the worse case, each tree level index/leaf need to be changed
2136 * if the tree split due to insert a new extent, then the old tree
2137 * index/leaf need to be updated too
2138 *
2139 * If the nrblocks are discontiguous, they could cause
2140 * the whole tree split more than once, but this is really rare.
2141 */
525f4ed8 2142int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
ee12b630
MC
2143{
2144 int index;
2145 int depth = ext_depth(inode);
a86c6181 2146
ee12b630
MC
2147 if (chunk)
2148 index = depth * 2;
2149 else
2150 index = depth * 3;
a86c6181 2151
ee12b630 2152 return index;
a86c6181
AT
2153}
2154
2155static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2156 struct ext4_extent *ex,
725d26d3 2157 ext4_lblk_t from, ext4_lblk_t to)
a86c6181 2158{
a2df2a63 2159 unsigned short ee_len = ext4_ext_get_actual_len(ex);
e6362609 2160 int flags = EXT4_FREE_BLOCKS_FORGET;
a86c6181 2161
c9de560d 2162 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
e6362609 2163 flags |= EXT4_FREE_BLOCKS_METADATA;
a86c6181
AT
2164#ifdef EXTENTS_STATS
2165 {
2166 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
a86c6181
AT
2167 spin_lock(&sbi->s_ext_stats_lock);
2168 sbi->s_ext_blocks += ee_len;
2169 sbi->s_ext_extents++;
2170 if (ee_len < sbi->s_ext_min)
2171 sbi->s_ext_min = ee_len;
2172 if (ee_len > sbi->s_ext_max)
2173 sbi->s_ext_max = ee_len;
2174 if (ext_depth(inode) > sbi->s_depth_max)
2175 sbi->s_depth_max = ext_depth(inode);
2176 spin_unlock(&sbi->s_ext_stats_lock);
2177 }
2178#endif
2179 if (from >= le32_to_cpu(ex->ee_block)
a2df2a63 2180 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
a86c6181 2181 /* tail removal */
725d26d3 2182 ext4_lblk_t num;
f65e6fba 2183 ext4_fsblk_t start;
725d26d3 2184
a2df2a63 2185 num = le32_to_cpu(ex->ee_block) + ee_len - from;
bf89d16f 2186 start = ext4_ext_pblock(ex) + ee_len - num;
725d26d3 2187 ext_debug("free last %u blocks starting %llu\n", num, start);
7dc57615 2188 ext4_free_blocks(handle, inode, NULL, start, num, flags);
a86c6181 2189 } else if (from == le32_to_cpu(ex->ee_block)
a2df2a63 2190 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
725d26d3 2191 printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
a2df2a63 2192 from, to, le32_to_cpu(ex->ee_block), ee_len);
a86c6181 2193 } else {
725d26d3
AK
2194 printk(KERN_INFO "strange request: removal(2) "
2195 "%u-%u from %u:%u\n",
2196 from, to, le32_to_cpu(ex->ee_block), ee_len);
a86c6181
AT
2197 }
2198 return 0;
2199}
2200
2201static int
2202ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
725d26d3 2203 struct ext4_ext_path *path, ext4_lblk_t start)
a86c6181
AT
2204{
2205 int err = 0, correct_index = 0;
2206 int depth = ext_depth(inode), credits;
2207 struct ext4_extent_header *eh;
725d26d3
AK
2208 ext4_lblk_t a, b, block;
2209 unsigned num;
2210 ext4_lblk_t ex_ee_block;
a86c6181 2211 unsigned short ex_ee_len;
a2df2a63 2212 unsigned uninitialized = 0;
a86c6181
AT
2213 struct ext4_extent *ex;
2214
c29c0ae7 2215 /* the header must be checked already in ext4_ext_remove_space() */
725d26d3 2216 ext_debug("truncate since %u in leaf\n", start);
a86c6181
AT
2217 if (!path[depth].p_hdr)
2218 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2219 eh = path[depth].p_hdr;
273df556
FM
2220 if (unlikely(path[depth].p_hdr == NULL)) {
2221 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2222 return -EIO;
2223 }
a86c6181
AT
2224 /* find where to start removing */
2225 ex = EXT_LAST_EXTENT(eh);
2226
2227 ex_ee_block = le32_to_cpu(ex->ee_block);
a2df2a63 2228 ex_ee_len = ext4_ext_get_actual_len(ex);
a86c6181
AT
2229
2230 while (ex >= EXT_FIRST_EXTENT(eh) &&
2231 ex_ee_block + ex_ee_len > start) {
a41f2071
AK
2232
2233 if (ext4_ext_is_uninitialized(ex))
2234 uninitialized = 1;
2235 else
2236 uninitialized = 0;
2237
553f9008
M
2238 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2239 uninitialized, ex_ee_len);
a86c6181
AT
2240 path[depth].p_ext = ex;
2241
2242 a = ex_ee_block > start ? ex_ee_block : start;
2243 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
2244 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
2245
2246 ext_debug(" border %u:%u\n", a, b);
2247
2248 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
2249 block = 0;
2250 num = 0;
2251 BUG();
2252 } else if (a != ex_ee_block) {
2253 /* remove tail of the extent */
2254 block = ex_ee_block;
2255 num = a - block;
2256 } else if (b != ex_ee_block + ex_ee_len - 1) {
2257 /* remove head of the extent */
2258 block = a;
2259 num = b - a;
2260 /* there is no "make a hole" API yet */
2261 BUG();
2262 } else {
2263 /* remove whole extent: excellent! */
2264 block = ex_ee_block;
2265 num = 0;
2266 BUG_ON(a != ex_ee_block);
2267 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
2268 }
2269
34071da7
TT
2270 /*
2271 * 3 for leaf, sb, and inode plus 2 (bmap and group
2272 * descriptor) for each block group; assume two block
2273 * groups plus ex_ee_len/blocks_per_block_group for
2274 * the worst case
2275 */
2276 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
a86c6181
AT
2277 if (ex == EXT_FIRST_EXTENT(eh)) {
2278 correct_index = 1;
2279 credits += (ext_depth(inode)) + 1;
2280 }
5aca07eb 2281 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
a86c6181 2282
487caeef 2283 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
9102e4fa 2284 if (err)
a86c6181 2285 goto out;
a86c6181
AT
2286
2287 err = ext4_ext_get_access(handle, inode, path + depth);
2288 if (err)
2289 goto out;
2290
2291 err = ext4_remove_blocks(handle, inode, ex, a, b);
2292 if (err)
2293 goto out;
2294
2295 if (num == 0) {
d0d856e8 2296 /* this extent is removed; mark slot entirely unused */
f65e6fba 2297 ext4_ext_store_pblock(ex, 0);
e8546d06 2298 le16_add_cpu(&eh->eh_entries, -1);
a86c6181
AT
2299 }
2300
2301 ex->ee_block = cpu_to_le32(block);
2302 ex->ee_len = cpu_to_le16(num);
749269fa
AA
2303 /*
2304 * Do not mark uninitialized if all the blocks in the
2305 * extent have been removed.
2306 */
2307 if (uninitialized && num)
a2df2a63 2308 ext4_ext_mark_uninitialized(ex);
a86c6181
AT
2309
2310 err = ext4_ext_dirty(handle, inode, path + depth);
2311 if (err)
2312 goto out;
2313
2ae02107 2314 ext_debug("new extent: %u:%u:%llu\n", block, num,
bf89d16f 2315 ext4_ext_pblock(ex));
a86c6181
AT
2316 ex--;
2317 ex_ee_block = le32_to_cpu(ex->ee_block);
a2df2a63 2318 ex_ee_len = ext4_ext_get_actual_len(ex);
a86c6181
AT
2319 }
2320
2321 if (correct_index && eh->eh_entries)
2322 err = ext4_ext_correct_indexes(handle, inode, path);
2323
2324 /* if this leaf is free, then we should
2325 * remove it from index block above */
2326 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2327 err = ext4_ext_rm_idx(handle, inode, path + depth);
2328
2329out:
2330 return err;
2331}
2332
2333/*
d0d856e8
RD
2334 * ext4_ext_more_to_rm:
2335 * returns 1 if current index has to be freed (even partial)
a86c6181 2336 */
09b88252 2337static int
a86c6181
AT
2338ext4_ext_more_to_rm(struct ext4_ext_path *path)
2339{
2340 BUG_ON(path->p_idx == NULL);
2341
2342 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2343 return 0;
2344
2345 /*
d0d856e8 2346 * if truncate on deeper level happened, it wasn't partial,
a86c6181
AT
2347 * so we have to consider current index for truncation
2348 */
2349 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2350 return 0;
2351 return 1;
2352}
2353
1d03ec98 2354static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
a86c6181
AT
2355{
2356 struct super_block *sb = inode->i_sb;
2357 int depth = ext_depth(inode);
2358 struct ext4_ext_path *path;
2359 handle_t *handle;
0617b83f 2360 int i, err;
a86c6181 2361
725d26d3 2362 ext_debug("truncate since %u\n", start);
a86c6181
AT
2363
2364 /* probably first extent we're gonna free will be last in block */
2365 handle = ext4_journal_start(inode, depth + 1);
2366 if (IS_ERR(handle))
2367 return PTR_ERR(handle);
2368
0617b83f 2369again:
a86c6181
AT
2370 ext4_ext_invalidate_cache(inode);
2371
2372 /*
d0d856e8
RD
2373 * We start scanning from right side, freeing all the blocks
2374 * after i_size and walking into the tree depth-wise.
a86c6181 2375 */
0617b83f 2376 depth = ext_depth(inode);
216553c4 2377 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
a86c6181
AT
2378 if (path == NULL) {
2379 ext4_journal_stop(handle);
2380 return -ENOMEM;
2381 }
0617b83f 2382 path[0].p_depth = depth;
a86c6181 2383 path[0].p_hdr = ext_inode_hdr(inode);
56b19868 2384 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
a86c6181
AT
2385 err = -EIO;
2386 goto out;
2387 }
0617b83f 2388 i = err = 0;
a86c6181
AT
2389
2390 while (i >= 0 && err == 0) {
2391 if (i == depth) {
2392 /* this is leaf block */
2393 err = ext4_ext_rm_leaf(handle, inode, path, start);
d0d856e8 2394 /* root level has p_bh == NULL, brelse() eats this */
a86c6181
AT
2395 brelse(path[i].p_bh);
2396 path[i].p_bh = NULL;
2397 i--;
2398 continue;
2399 }
2400
2401 /* this is index block */
2402 if (!path[i].p_hdr) {
2403 ext_debug("initialize header\n");
2404 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
a86c6181
AT
2405 }
2406
a86c6181 2407 if (!path[i].p_idx) {
d0d856e8 2408 /* this level hasn't been touched yet */
a86c6181
AT
2409 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2410 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2411 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2412 path[i].p_hdr,
2413 le16_to_cpu(path[i].p_hdr->eh_entries));
2414 } else {
d0d856e8 2415 /* we were already here, see at next index */
a86c6181
AT
2416 path[i].p_idx--;
2417 }
2418
2419 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2420 i, EXT_FIRST_INDEX(path[i].p_hdr),
2421 path[i].p_idx);
2422 if (ext4_ext_more_to_rm(path + i)) {
c29c0ae7 2423 struct buffer_head *bh;
a86c6181 2424 /* go to the next level */
2ae02107 2425 ext_debug("move to level %d (block %llu)\n",
bf89d16f 2426 i + 1, ext4_idx_pblock(path[i].p_idx));
a86c6181 2427 memset(path + i + 1, 0, sizeof(*path));
bf89d16f 2428 bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx));
c29c0ae7 2429 if (!bh) {
a86c6181
AT
2430 /* should we reset i_size? */
2431 err = -EIO;
2432 break;
2433 }
c29c0ae7
AT
2434 if (WARN_ON(i + 1 > depth)) {
2435 err = -EIO;
2436 break;
2437 }
56b19868 2438 if (ext4_ext_check(inode, ext_block_hdr(bh),
c29c0ae7
AT
2439 depth - i - 1)) {
2440 err = -EIO;
2441 break;
2442 }
2443 path[i + 1].p_bh = bh;
a86c6181 2444
d0d856e8
RD
2445 /* save actual number of indexes since this
2446 * number is changed at the next iteration */
a86c6181
AT
2447 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2448 i++;
2449 } else {
d0d856e8 2450 /* we finished processing this index, go up */
a86c6181 2451 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
d0d856e8 2452 /* index is empty, remove it;
a86c6181
AT
2453 * handle must be already prepared by the
2454 * truncatei_leaf() */
2455 err = ext4_ext_rm_idx(handle, inode, path + i);
2456 }
d0d856e8 2457 /* root level has p_bh == NULL, brelse() eats this */
a86c6181
AT
2458 brelse(path[i].p_bh);
2459 path[i].p_bh = NULL;
2460 i--;
2461 ext_debug("return to level %d\n", i);
2462 }
2463 }
2464
2465 /* TODO: flexible tree reduction should be here */
2466 if (path->p_hdr->eh_entries == 0) {
2467 /*
d0d856e8
RD
2468 * truncate to zero freed all the tree,
2469 * so we need to correct eh_depth
a86c6181
AT
2470 */
2471 err = ext4_ext_get_access(handle, inode, path);
2472 if (err == 0) {
2473 ext_inode_hdr(inode)->eh_depth = 0;
2474 ext_inode_hdr(inode)->eh_max =
55ad63bf 2475 cpu_to_le16(ext4_ext_space_root(inode, 0));
a86c6181
AT
2476 err = ext4_ext_dirty(handle, inode, path);
2477 }
2478 }
2479out:
a86c6181
AT
2480 ext4_ext_drop_refs(path);
2481 kfree(path);
0617b83f
DM
2482 if (err == -EAGAIN)
2483 goto again;
a86c6181
AT
2484 ext4_journal_stop(handle);
2485
2486 return err;
2487}
2488
2489/*
2490 * called at mount time
2491 */
2492void ext4_ext_init(struct super_block *sb)
2493{
2494 /*
2495 * possible initialization would be here
2496 */
2497
83982b6f 2498 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
90576c0b 2499#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
4776004f 2500 printk(KERN_INFO "EXT4-fs: file extents enabled");
bbf2f9fb
RD
2501#ifdef AGGRESSIVE_TEST
2502 printk(", aggressive tests");
a86c6181
AT
2503#endif
2504#ifdef CHECK_BINSEARCH
2505 printk(", check binsearch");
2506#endif
2507#ifdef EXTENTS_STATS
2508 printk(", stats");
2509#endif
2510 printk("\n");
90576c0b 2511#endif
a86c6181
AT
2512#ifdef EXTENTS_STATS
2513 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2514 EXT4_SB(sb)->s_ext_min = 1 << 30;
2515 EXT4_SB(sb)->s_ext_max = 0;
2516#endif
2517 }
2518}
2519
2520/*
2521 * called at umount time
2522 */
2523void ext4_ext_release(struct super_block *sb)
2524{
83982b6f 2525 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
a86c6181
AT
2526 return;
2527
2528#ifdef EXTENTS_STATS
2529 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2530 struct ext4_sb_info *sbi = EXT4_SB(sb);
2531 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2532 sbi->s_ext_blocks, sbi->s_ext_extents,
2533 sbi->s_ext_blocks / sbi->s_ext_extents);
2534 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2535 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2536 }
2537#endif
2538}
2539
093a088b
AK
2540/* FIXME!! we need to try to merge to left or right after zero-out */
2541static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2542{
2407518d
LC
2543 ext4_fsblk_t ee_pblock;
2544 unsigned int ee_len;
b720303d 2545 int ret;
093a088b 2546
093a088b 2547 ee_len = ext4_ext_get_actual_len(ex);
bf89d16f 2548 ee_pblock = ext4_ext_pblock(ex);
b720303d 2549
a107e5a3 2550 ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
2407518d
LC
2551 if (ret > 0)
2552 ret = 0;
093a088b 2553
2407518d 2554 return ret;
093a088b
AK
2555}
2556
3977c965 2557#define EXT4_EXT_ZERO_LEN 7
56055d3a 2558/*
e35fd660 2559 * This function is called by ext4_ext_map_blocks() if someone tries to write
56055d3a 2560 * to an uninitialized extent. It may result in splitting the uninitialized
25985edc 2561 * extent into multiple extents (up to three - one initialized and two
56055d3a
AA
2562 * uninitialized).
2563 * There are three possibilities:
2564 * a> There is no split required: Entire extent should be initialized
2565 * b> Splits in two extents: Write is happening at either end of the extent
2566 * c> Splits in three extents: Somone is writing in middle of the extent
2567 */
725d26d3 2568static int ext4_ext_convert_to_initialized(handle_t *handle,
e35fd660
TT
2569 struct inode *inode,
2570 struct ext4_map_blocks *map,
2571 struct ext4_ext_path *path)
56055d3a 2572{
95c3889c 2573 struct ext4_extent *ex, newex, orig_ex;
56055d3a
AA
2574 struct ext4_extent *ex1 = NULL;
2575 struct ext4_extent *ex2 = NULL;
2576 struct ext4_extent *ex3 = NULL;
2577 struct ext4_extent_header *eh;
21ca087a 2578 ext4_lblk_t ee_block, eof_block;
725d26d3 2579 unsigned int allocated, ee_len, depth;
56055d3a
AA
2580 ext4_fsblk_t newblock;
2581 int err = 0;
2582 int ret = 0;
21ca087a
DM
2583 int may_zeroout;
2584
2585 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
2586 "block %llu, max_blocks %u\n", inode->i_ino,
e35fd660 2587 (unsigned long long)map->m_lblk, map->m_len);
21ca087a
DM
2588
2589 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
2590 inode->i_sb->s_blocksize_bits;
e35fd660
TT
2591 if (eof_block < map->m_lblk + map->m_len)
2592 eof_block = map->m_lblk + map->m_len;
56055d3a
AA
2593
2594 depth = ext_depth(inode);
2595 eh = path[depth].p_hdr;
2596 ex = path[depth].p_ext;
2597 ee_block = le32_to_cpu(ex->ee_block);
2598 ee_len = ext4_ext_get_actual_len(ex);
e35fd660 2599 allocated = ee_len - (map->m_lblk - ee_block);
bf89d16f 2600 newblock = map->m_lblk - ee_block + ext4_ext_pblock(ex);
21ca087a 2601
56055d3a 2602 ex2 = ex;
95c3889c
AK
2603 orig_ex.ee_block = ex->ee_block;
2604 orig_ex.ee_len = cpu_to_le16(ee_len);
bf89d16f 2605 ext4_ext_store_pblock(&orig_ex, ext4_ext_pblock(ex));
56055d3a 2606
21ca087a
DM
2607 /*
2608 * It is safe to convert extent to initialized via explicit
2609 * zeroout only if extent is fully insde i_size or new_size.
2610 */
2611 may_zeroout = ee_block + ee_len <= eof_block;
2612
9df5643a
AK
2613 err = ext4_ext_get_access(handle, inode, path + depth);
2614 if (err)
2615 goto out;
3977c965 2616 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
21ca087a 2617 if (ee_len <= 2*EXT4_EXT_ZERO_LEN && may_zeroout) {
3977c965
AK
2618 err = ext4_ext_zeroout(inode, &orig_ex);
2619 if (err)
2620 goto fix_extent_len;
2621 /* update the extent length and mark as initialized */
2622 ex->ee_block = orig_ex.ee_block;
2623 ex->ee_len = orig_ex.ee_len;
bf89d16f 2624 ext4_ext_store_pblock(ex, ext4_ext_pblock(&orig_ex));
3977c965 2625 ext4_ext_dirty(handle, inode, path + depth);
161e7b7c
AK
2626 /* zeroed the full extent */
2627 return allocated;
3977c965 2628 }
9df5643a 2629
e35fd660
TT
2630 /* ex1: ee_block to map->m_lblk - 1 : uninitialized */
2631 if (map->m_lblk > ee_block) {
56055d3a 2632 ex1 = ex;
e35fd660 2633 ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block);
56055d3a
AA
2634 ext4_ext_mark_uninitialized(ex1);
2635 ex2 = &newex;
2636 }
2637 /*
2638 * for sanity, update the length of the ex2 extent before
2639 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2640 * overlap of blocks.
2641 */
e35fd660
TT
2642 if (!ex1 && allocated > map->m_len)
2643 ex2->ee_len = cpu_to_le16(map->m_len);
56055d3a 2644 /* ex3: to ee_block + ee_len : uninitialised */
e35fd660 2645 if (allocated > map->m_len) {
56055d3a 2646 unsigned int newdepth;
3977c965 2647 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
21ca087a 2648 if (allocated <= EXT4_EXT_ZERO_LEN && may_zeroout) {
d03856bd 2649 /*
e35fd660 2650 * map->m_lblk == ee_block is handled by the zerouout
d03856bd
AK
2651 * at the beginning.
2652 * Mark first half uninitialized.
3977c965
AK
2653 * Mark second half initialized and zero out the
2654 * initialized extent
2655 */
2656 ex->ee_block = orig_ex.ee_block;
2657 ex->ee_len = cpu_to_le16(ee_len - allocated);
2658 ext4_ext_mark_uninitialized(ex);
bf89d16f 2659 ext4_ext_store_pblock(ex, ext4_ext_pblock(&orig_ex));
3977c965
AK
2660 ext4_ext_dirty(handle, inode, path + depth);
2661
2662 ex3 = &newex;
e35fd660 2663 ex3->ee_block = cpu_to_le32(map->m_lblk);
3977c965
AK
2664 ext4_ext_store_pblock(ex3, newblock);
2665 ex3->ee_len = cpu_to_le16(allocated);
0031462b
MC
2666 err = ext4_ext_insert_extent(handle, inode, path,
2667 ex3, 0);
3977c965
AK
2668 if (err == -ENOSPC) {
2669 err = ext4_ext_zeroout(inode, &orig_ex);
2670 if (err)
2671 goto fix_extent_len;
2672 ex->ee_block = orig_ex.ee_block;
2673 ex->ee_len = orig_ex.ee_len;
bf89d16f
TT
2674 ext4_ext_store_pblock(ex,
2675 ext4_ext_pblock(&orig_ex));
3977c965 2676 ext4_ext_dirty(handle, inode, path + depth);
e35fd660 2677 /* blocks available from map->m_lblk */
161e7b7c 2678 return allocated;
3977c965
AK
2679
2680 } else if (err)
2681 goto fix_extent_len;
2682
161e7b7c
AK
2683 /*
2684 * We need to zero out the second half because
2685 * an fallocate request can update file size and
2686 * converting the second half to initialized extent
2687 * implies that we can leak some junk data to user
2688 * space.
2689 */
2690 err = ext4_ext_zeroout(inode, ex3);
2691 if (err) {
2692 /*
2693 * We should actually mark the
2694 * second half as uninit and return error
2695 * Insert would have changed the extent
2696 */
2697 depth = ext_depth(inode);
2698 ext4_ext_drop_refs(path);
e35fd660
TT
2699 path = ext4_ext_find_extent(inode, map->m_lblk,
2700 path);
161e7b7c
AK
2701 if (IS_ERR(path)) {
2702 err = PTR_ERR(path);
2703 return err;
2704 }
d03856bd 2705 /* get the second half extent details */
161e7b7c
AK
2706 ex = path[depth].p_ext;
2707 err = ext4_ext_get_access(handle, inode,
2708 path + depth);
2709 if (err)
2710 return err;
2711 ext4_ext_mark_uninitialized(ex);
2712 ext4_ext_dirty(handle, inode, path + depth);
2713 return err;
2714 }
2715
2716 /* zeroed the second half */
3977c965
AK
2717 return allocated;
2718 }
56055d3a 2719 ex3 = &newex;
e35fd660
TT
2720 ex3->ee_block = cpu_to_le32(map->m_lblk + map->m_len);
2721 ext4_ext_store_pblock(ex3, newblock + map->m_len);
2722 ex3->ee_len = cpu_to_le16(allocated - map->m_len);
56055d3a 2723 ext4_ext_mark_uninitialized(ex3);
0031462b 2724 err = ext4_ext_insert_extent(handle, inode, path, ex3, 0);
21ca087a 2725 if (err == -ENOSPC && may_zeroout) {
093a088b
AK
2726 err = ext4_ext_zeroout(inode, &orig_ex);
2727 if (err)
2728 goto fix_extent_len;
2729 /* update the extent length and mark as initialized */
95c3889c
AK
2730 ex->ee_block = orig_ex.ee_block;
2731 ex->ee_len = orig_ex.ee_len;
bf89d16f 2732 ext4_ext_store_pblock(ex, ext4_ext_pblock(&orig_ex));
95c3889c 2733 ext4_ext_dirty(handle, inode, path + depth);
161e7b7c 2734 /* zeroed the full extent */
e35fd660 2735 /* blocks available from map->m_lblk */
161e7b7c 2736 return allocated;
093a088b
AK
2737
2738 } else if (err)
2739 goto fix_extent_len;
56055d3a
AA
2740 /*
2741 * The depth, and hence eh & ex might change
2742 * as part of the insert above.
2743 */
2744 newdepth = ext_depth(inode);
95c3889c 2745 /*
73ac36ea 2746 * update the extent length after successful insert of the
95c3889c
AK
2747 * split extent
2748 */
21ca087a
DM
2749 ee_len -= ext4_ext_get_actual_len(ex3);
2750 orig_ex.ee_len = cpu_to_le16(ee_len);
2751 may_zeroout = ee_block + ee_len <= eof_block;
2752
d03856bd
AK
2753 depth = newdepth;
2754 ext4_ext_drop_refs(path);
e35fd660 2755 path = ext4_ext_find_extent(inode, map->m_lblk, path);
d03856bd
AK
2756 if (IS_ERR(path)) {
2757 err = PTR_ERR(path);
2758 goto out;
56055d3a 2759 }
d03856bd
AK
2760 eh = path[depth].p_hdr;
2761 ex = path[depth].p_ext;
2762 if (ex2 != &newex)
2763 ex2 = ex;
2764
2765 err = ext4_ext_get_access(handle, inode, path + depth);
2766 if (err)
2767 goto out;
2768
e35fd660 2769 allocated = map->m_len;
3977c965
AK
2770
2771 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2772 * to insert a extent in the middle zerout directly
2773 * otherwise give the extent a chance to merge to left
2774 */
2775 if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
e35fd660 2776 map->m_lblk != ee_block && may_zeroout) {
3977c965
AK
2777 err = ext4_ext_zeroout(inode, &orig_ex);
2778 if (err)
2779 goto fix_extent_len;
2780 /* update the extent length and mark as initialized */
2781 ex->ee_block = orig_ex.ee_block;
2782 ex->ee_len = orig_ex.ee_len;
bf89d16f 2783 ext4_ext_store_pblock(ex, ext4_ext_pblock(&orig_ex));
3977c965 2784 ext4_ext_dirty(handle, inode, path + depth);
161e7b7c 2785 /* zero out the first half */
e35fd660 2786 /* blocks available from map->m_lblk */
161e7b7c 2787 return allocated;
3977c965 2788 }
56055d3a
AA
2789 }
2790 /*
2791 * If there was a change of depth as part of the
2792 * insertion of ex3 above, we need to update the length
2793 * of the ex1 extent again here
2794 */
2795 if (ex1 && ex1 != ex) {
2796 ex1 = ex;
e35fd660 2797 ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block);
56055d3a
AA
2798 ext4_ext_mark_uninitialized(ex1);
2799 ex2 = &newex;
2800 }
e35fd660
TT
2801 /* ex2: map->m_lblk to map->m_lblk + maxblocks-1 : initialised */
2802 ex2->ee_block = cpu_to_le32(map->m_lblk);
56055d3a
AA
2803 ext4_ext_store_pblock(ex2, newblock);
2804 ex2->ee_len = cpu_to_le16(allocated);
2805 if (ex2 != ex)
2806 goto insert;
56055d3a
AA
2807 /*
2808 * New (initialized) extent starts from the first block
2809 * in the current extent. i.e., ex2 == ex
2810 * We have to see if it can be merged with the extent
2811 * on the left.
2812 */
2813 if (ex2 > EXT_FIRST_EXTENT(eh)) {
2814 /*
2815 * To merge left, pass "ex2 - 1" to try_to_merge(),
2816 * since it merges towards right _only_.
2817 */
2818 ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
2819 if (ret) {
2820 err = ext4_ext_correct_indexes(handle, inode, path);
2821 if (err)
2822 goto out;
2823 depth = ext_depth(inode);
2824 ex2--;
2825 }
2826 }
2827 /*
2828 * Try to Merge towards right. This might be required
2829 * only when the whole extent is being written to.
2830 * i.e. ex2 == ex and ex3 == NULL.
2831 */
2832 if (!ex3) {
2833 ret = ext4_ext_try_to_merge(inode, path, ex2);
2834 if (ret) {
2835 err = ext4_ext_correct_indexes(handle, inode, path);
2836 if (err)
2837 goto out;
2838 }
2839 }
2840 /* Mark modified extent as dirty */
2841 err = ext4_ext_dirty(handle, inode, path + depth);
2842 goto out;
2843insert:
0031462b 2844 err = ext4_ext_insert_extent(handle, inode, path, &newex, 0);
21ca087a 2845 if (err == -ENOSPC && may_zeroout) {
093a088b
AK
2846 err = ext4_ext_zeroout(inode, &orig_ex);
2847 if (err)
2848 goto fix_extent_len;
2849 /* update the extent length and mark as initialized */
95c3889c
AK
2850 ex->ee_block = orig_ex.ee_block;
2851 ex->ee_len = orig_ex.ee_len;
bf89d16f 2852 ext4_ext_store_pblock(ex, ext4_ext_pblock(&orig_ex));
95c3889c 2853 ext4_ext_dirty(handle, inode, path + depth);
161e7b7c
AK
2854 /* zero out the first half */
2855 return allocated;
093a088b
AK
2856 } else if (err)
2857 goto fix_extent_len;
56055d3a 2858out:
553f9008 2859 ext4_ext_show_leaf(inode, path);
56055d3a 2860 return err ? err : allocated;
093a088b
AK
2861
2862fix_extent_len:
2863 ex->ee_block = orig_ex.ee_block;
2864 ex->ee_len = orig_ex.ee_len;
bf89d16f 2865 ext4_ext_store_pblock(ex, ext4_ext_pblock(&orig_ex));
093a088b
AK
2866 ext4_ext_mark_uninitialized(ex);
2867 ext4_ext_dirty(handle, inode, path + depth);
2868 return err;
56055d3a
AA
2869}
2870
0031462b 2871/*
e35fd660 2872 * This function is called by ext4_ext_map_blocks() from
0031462b
MC
2873 * ext4_get_blocks_dio_write() when DIO to write
2874 * to an uninitialized extent.
2875 *
fd018fe8 2876 * Writing to an uninitialized extent may result in splitting the uninitialized
b595076a 2877 * extent into multiple /initialized uninitialized extents (up to three)
0031462b
MC
2878 * There are three possibilities:
2879 * a> There is no split required: Entire extent should be uninitialized
2880 * b> Splits in two extents: Write is happening at either end of the extent
2881 * c> Splits in three extents: Somone is writing in middle of the extent
2882 *
2883 * One of more index blocks maybe needed if the extent tree grow after
b595076a 2884 * the uninitialized extent split. To prevent ENOSPC occur at the IO
0031462b 2885 * complete, we need to split the uninitialized extent before DIO submit
421f91d2 2886 * the IO. The uninitialized extent called at this time will be split
0031462b
MC
2887 * into three uninitialized extent(at most). After IO complete, the part
2888 * being filled will be convert to initialized by the end_io callback function
2889 * via ext4_convert_unwritten_extents().
ba230c3f
M
2890 *
2891 * Returns the size of uninitialized extent to be written on success.
0031462b
MC
2892 */
2893static int ext4_split_unwritten_extents(handle_t *handle,
2894 struct inode *inode,
e35fd660 2895 struct ext4_map_blocks *map,
0031462b 2896 struct ext4_ext_path *path,
0031462b
MC
2897 int flags)
2898{
2899 struct ext4_extent *ex, newex, orig_ex;
2900 struct ext4_extent *ex1 = NULL;
2901 struct ext4_extent *ex2 = NULL;
2902 struct ext4_extent *ex3 = NULL;
21ca087a 2903 ext4_lblk_t ee_block, eof_block;
0031462b
MC
2904 unsigned int allocated, ee_len, depth;
2905 ext4_fsblk_t newblock;
2906 int err = 0;
21ca087a
DM
2907 int may_zeroout;
2908
2909 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
2910 "block %llu, max_blocks %u\n", inode->i_ino,
e35fd660 2911 (unsigned long long)map->m_lblk, map->m_len);
21ca087a
DM
2912
2913 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
2914 inode->i_sb->s_blocksize_bits;
e35fd660
TT
2915 if (eof_block < map->m_lblk + map->m_len)
2916 eof_block = map->m_lblk + map->m_len;
0031462b 2917
0031462b 2918 depth = ext_depth(inode);
0031462b
MC
2919 ex = path[depth].p_ext;
2920 ee_block = le32_to_cpu(ex->ee_block);
2921 ee_len = ext4_ext_get_actual_len(ex);
e35fd660 2922 allocated = ee_len - (map->m_lblk - ee_block);
bf89d16f 2923 newblock = map->m_lblk - ee_block + ext4_ext_pblock(ex);
21ca087a 2924
0031462b
MC
2925 ex2 = ex;
2926 orig_ex.ee_block = ex->ee_block;
2927 orig_ex.ee_len = cpu_to_le16(ee_len);
bf89d16f 2928 ext4_ext_store_pblock(&orig_ex, ext4_ext_pblock(ex));
0031462b 2929
21ca087a
DM
2930 /*
2931 * It is safe to convert extent to initialized via explicit
2932 * zeroout only if extent is fully insde i_size or new_size.
2933 */
2934 may_zeroout = ee_block + ee_len <= eof_block;
2935
0031462b 2936 /*
ba230c3f
M
2937 * If the uninitialized extent begins at the same logical
2938 * block where the write begins, and the write completely
2939 * covers the extent, then we don't need to split it.
0031462b 2940 */
e35fd660 2941 if ((map->m_lblk == ee_block) && (allocated <= map->m_len))
ba230c3f 2942 return allocated;
0031462b
MC
2943
2944 err = ext4_ext_get_access(handle, inode, path + depth);
2945 if (err)
2946 goto out;
e35fd660
TT
2947 /* ex1: ee_block to map->m_lblk - 1 : uninitialized */
2948 if (map->m_lblk > ee_block) {
0031462b 2949 ex1 = ex;
e35fd660 2950 ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block);
0031462b
MC
2951 ext4_ext_mark_uninitialized(ex1);
2952 ex2 = &newex;
2953 }
2954 /*
2955 * for sanity, update the length of the ex2 extent before
2956 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2957 * overlap of blocks.
2958 */
e35fd660
TT
2959 if (!ex1 && allocated > map->m_len)
2960 ex2->ee_len = cpu_to_le16(map->m_len);
0031462b 2961 /* ex3: to ee_block + ee_len : uninitialised */
e35fd660 2962 if (allocated > map->m_len) {
0031462b
MC
2963 unsigned int newdepth;
2964 ex3 = &newex;
e35fd660
TT
2965 ex3->ee_block = cpu_to_le32(map->m_lblk + map->m_len);
2966 ext4_ext_store_pblock(ex3, newblock + map->m_len);
2967 ex3->ee_len = cpu_to_le16(allocated - map->m_len);
0031462b
MC
2968 ext4_ext_mark_uninitialized(ex3);
2969 err = ext4_ext_insert_extent(handle, inode, path, ex3, flags);
21ca087a 2970 if (err == -ENOSPC && may_zeroout) {
0031462b
MC
2971 err = ext4_ext_zeroout(inode, &orig_ex);
2972 if (err)
2973 goto fix_extent_len;
2974 /* update the extent length and mark as initialized */
2975 ex->ee_block = orig_ex.ee_block;
2976 ex->ee_len = orig_ex.ee_len;
bf89d16f 2977 ext4_ext_store_pblock(ex, ext4_ext_pblock(&orig_ex));
0031462b
MC
2978 ext4_ext_dirty(handle, inode, path + depth);
2979 /* zeroed the full extent */
e35fd660 2980 /* blocks available from map->m_lblk */
0031462b
MC
2981 return allocated;
2982
2983 } else if (err)
2984 goto fix_extent_len;
2985 /*
2986 * The depth, and hence eh & ex might change
2987 * as part of the insert above.
2988 */
2989 newdepth = ext_depth(inode);
2990 /*
2991 * update the extent length after successful insert of the
2992 * split extent
2993 */
21ca087a
DM
2994 ee_len -= ext4_ext_get_actual_len(ex3);
2995 orig_ex.ee_len = cpu_to_le16(ee_len);
2996 may_zeroout = ee_block + ee_len <= eof_block;
2997
0031462b
MC
2998 depth = newdepth;
2999 ext4_ext_drop_refs(path);
e35fd660 3000 path = ext4_ext_find_extent(inode, map->m_lblk, path);
0031462b
MC
3001 if (IS_ERR(path)) {
3002 err = PTR_ERR(path);
3003 goto out;
3004 }
0031462b
MC
3005 ex = path[depth].p_ext;
3006 if (ex2 != &newex)
3007 ex2 = ex;
3008
3009 err = ext4_ext_get_access(handle, inode, path + depth);
3010 if (err)
3011 goto out;
3012
e35fd660 3013 allocated = map->m_len;
0031462b
MC
3014 }
3015 /*
3016 * If there was a change of depth as part of the
3017 * insertion of ex3 above, we need to update the length
3018 * of the ex1 extent again here
3019 */
3020 if (ex1 && ex1 != ex) {
3021 ex1 = ex;
e35fd660 3022 ex1->ee_len = cpu_to_le16(map->m_lblk - ee_block);
0031462b
MC
3023 ext4_ext_mark_uninitialized(ex1);
3024 ex2 = &newex;
3025 }
3026 /*
e35fd660
TT
3027 * ex2: map->m_lblk to map->m_lblk + map->m_len-1 : to be written
3028 * using direct I/O, uninitialised still.
0031462b 3029 */
e35fd660 3030 ex2->ee_block = cpu_to_le32(map->m_lblk);
0031462b
MC
3031 ext4_ext_store_pblock(ex2, newblock);
3032 ex2->ee_len = cpu_to_le16(allocated);
3033 ext4_ext_mark_uninitialized(ex2);
3034 if (ex2 != ex)
3035 goto insert;
3036 /* Mark modified extent as dirty */
3037 err = ext4_ext_dirty(handle, inode, path + depth);
3038 ext_debug("out here\n");
3039 goto out;
3040insert:
3041 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
21ca087a 3042 if (err == -ENOSPC && may_zeroout) {
0031462b
MC
3043 err = ext4_ext_zeroout(inode, &orig_ex);
3044 if (err)
3045 goto fix_extent_len;
3046 /* update the extent length and mark as initialized */
3047 ex->ee_block = orig_ex.ee_block;
3048 ex->ee_len = orig_ex.ee_len;
bf89d16f 3049 ext4_ext_store_pblock(ex, ext4_ext_pblock(&orig_ex));
0031462b
MC
3050 ext4_ext_dirty(handle, inode, path + depth);
3051 /* zero out the first half */
3052 return allocated;
3053 } else if (err)
3054 goto fix_extent_len;
3055out:
3056 ext4_ext_show_leaf(inode, path);
3057 return err ? err : allocated;
3058
3059fix_extent_len:
3060 ex->ee_block = orig_ex.ee_block;
3061 ex->ee_len = orig_ex.ee_len;
bf89d16f 3062 ext4_ext_store_pblock(ex, ext4_ext_pblock(&orig_ex));
0031462b
MC
3063 ext4_ext_mark_uninitialized(ex);
3064 ext4_ext_dirty(handle, inode, path + depth);
3065 return err;
3066}
197217a5 3067
c7064ef1 3068static int ext4_convert_unwritten_extents_endio(handle_t *handle,
0031462b
MC
3069 struct inode *inode,
3070 struct ext4_ext_path *path)
3071{
3072 struct ext4_extent *ex;
3073 struct ext4_extent_header *eh;
3074 int depth;
3075 int err = 0;
0031462b
MC
3076
3077 depth = ext_depth(inode);
3078 eh = path[depth].p_hdr;
3079 ex = path[depth].p_ext;
3080
197217a5
YY
3081 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3082 "block %llu, max_blocks %u\n", inode->i_ino,
3083 (unsigned long long)le32_to_cpu(ex->ee_block),
3084 ext4_ext_get_actual_len(ex));
3085
0031462b
MC
3086 err = ext4_ext_get_access(handle, inode, path + depth);
3087 if (err)
3088 goto out;
3089 /* first mark the extent as initialized */
3090 ext4_ext_mark_initialized(ex);
3091
197217a5
YY
3092 /* note: ext4_ext_correct_indexes() isn't needed here because
3093 * borders are not changed
0031462b 3094 */
197217a5
YY
3095 ext4_ext_try_to_merge(inode, path, ex);
3096
0031462b
MC
3097 /* Mark modified extent as dirty */
3098 err = ext4_ext_dirty(handle, inode, path + depth);
3099out:
3100 ext4_ext_show_leaf(inode, path);
3101 return err;
3102}
3103
515f41c3
AK
3104static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3105 sector_t block, int count)
3106{
3107 int i;
3108 for (i = 0; i < count; i++)
3109 unmap_underlying_metadata(bdev, block + i);
3110}
3111
58590b06
TT
3112/*
3113 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3114 */
3115static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
d002ebf1 3116 ext4_lblk_t lblk,
58590b06
TT
3117 struct ext4_ext_path *path,
3118 unsigned int len)
3119{
3120 int i, depth;
3121 struct ext4_extent_header *eh;
65922cb5 3122 struct ext4_extent *last_ex;
58590b06
TT
3123
3124 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3125 return 0;
3126
3127 depth = ext_depth(inode);
3128 eh = path[depth].p_hdr;
58590b06
TT
3129
3130 if (unlikely(!eh->eh_entries)) {
3131 EXT4_ERROR_INODE(inode, "eh->eh_entries == 0 and "
3132 "EOFBLOCKS_FL set");
3133 return -EIO;
3134 }
3135 last_ex = EXT_LAST_EXTENT(eh);
3136 /*
3137 * We should clear the EOFBLOCKS_FL flag if we are writing the
3138 * last block in the last extent in the file. We test this by
3139 * first checking to see if the caller to
3140 * ext4_ext_get_blocks() was interested in the last block (or
3141 * a block beyond the last block) in the current extent. If
3142 * this turns out to be false, we can bail out from this
3143 * function immediately.
3144 */
d002ebf1 3145 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
58590b06
TT
3146 ext4_ext_get_actual_len(last_ex))
3147 return 0;
3148 /*
3149 * If the caller does appear to be planning to write at or
3150 * beyond the end of the current extent, we then test to see
3151 * if the current extent is the last extent in the file, by
3152 * checking to make sure it was reached via the rightmost node
3153 * at each level of the tree.
3154 */
3155 for (i = depth-1; i >= 0; i--)
3156 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3157 return 0;
3158 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3159 return ext4_mark_inode_dirty(handle, inode);
3160}
3161
0031462b
MC
3162static int
3163ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
e35fd660 3164 struct ext4_map_blocks *map,
0031462b 3165 struct ext4_ext_path *path, int flags,
e35fd660 3166 unsigned int allocated, ext4_fsblk_t newblock)
0031462b
MC
3167{
3168 int ret = 0;
3169 int err = 0;
8d5d02e6 3170 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
0031462b
MC
3171
3172 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical"
3173 "block %llu, max_blocks %u, flags %d, allocated %u",
e35fd660 3174 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
0031462b
MC
3175 flags, allocated);
3176 ext4_ext_show_leaf(inode, path);
3177
c7064ef1 3178 /* get_block() before submit the IO, split the extent */
744692dc 3179 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
e35fd660
TT
3180 ret = ext4_split_unwritten_extents(handle, inode, map,
3181 path, flags);
5f524950
M
3182 /*
3183 * Flag the inode(non aio case) or end_io struct (aio case)
25985edc 3184 * that this IO needs to conversion to written when IO is
5f524950
M
3185 * completed
3186 */
e9e3bcec 3187 if (io && !(io->flag & EXT4_IO_END_UNWRITTEN)) {
bd2d0210 3188 io->flag = EXT4_IO_END_UNWRITTEN;
e9e3bcec
ES
3189 atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten);
3190 } else
19f5fb7a 3191 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
744692dc 3192 if (ext4_should_dioread_nolock(inode))
e35fd660 3193 map->m_flags |= EXT4_MAP_UNINIT;
0031462b
MC
3194 goto out;
3195 }
c7064ef1 3196 /* IO end_io complete, convert the filled extent to written */
744692dc 3197 if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
c7064ef1 3198 ret = ext4_convert_unwritten_extents_endio(handle, inode,
0031462b 3199 path);
58590b06 3200 if (ret >= 0) {
b436b9be 3201 ext4_update_inode_fsync_trans(handle, inode, 1);
d002ebf1
ES
3202 err = check_eofblocks_fl(handle, inode, map->m_lblk,
3203 path, map->m_len);
58590b06
TT
3204 } else
3205 err = ret;
0031462b
MC
3206 goto out2;
3207 }
3208 /* buffered IO case */
3209 /*
3210 * repeat fallocate creation request
3211 * we already have an unwritten extent
3212 */
3213 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3214 goto map_out;
3215
3216 /* buffered READ or buffered write_begin() lookup */
3217 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3218 /*
3219 * We have blocks reserved already. We
3220 * return allocated blocks so that delalloc
3221 * won't do block reservation for us. But
3222 * the buffer head will be unmapped so that
3223 * a read from the block returns 0s.
3224 */
e35fd660 3225 map->m_flags |= EXT4_MAP_UNWRITTEN;
0031462b
MC
3226 goto out1;
3227 }
3228
3229 /* buffered write, writepage time, convert*/
e35fd660 3230 ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
58590b06 3231 if (ret >= 0) {
b436b9be 3232 ext4_update_inode_fsync_trans(handle, inode, 1);
d002ebf1
ES
3233 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
3234 map->m_len);
58590b06
TT
3235 if (err < 0)
3236 goto out2;
3237 }
3238
0031462b
MC
3239out:
3240 if (ret <= 0) {
3241 err = ret;
3242 goto out2;
3243 } else
3244 allocated = ret;
e35fd660 3245 map->m_flags |= EXT4_MAP_NEW;
515f41c3
AK
3246 /*
3247 * if we allocated more blocks than requested
3248 * we need to make sure we unmap the extra block
3249 * allocated. The actual needed block will get
3250 * unmapped later when we find the buffer_head marked
3251 * new.
3252 */
e35fd660 3253 if (allocated > map->m_len) {
515f41c3 3254 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
e35fd660
TT
3255 newblock + map->m_len,
3256 allocated - map->m_len);
3257 allocated = map->m_len;
515f41c3 3258 }
5f634d06
AK
3259
3260 /*
3261 * If we have done fallocate with the offset that is already
3262 * delayed allocated, we would have block reservation
3263 * and quota reservation done in the delayed write path.
3264 * But fallocate would have already updated quota and block
3265 * count for this offset. So cancel these reservation
3266 */
1296cc85 3267 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
5f634d06
AK
3268 ext4_da_update_reserve_space(inode, allocated, 0);
3269
0031462b 3270map_out:
e35fd660 3271 map->m_flags |= EXT4_MAP_MAPPED;
0031462b 3272out1:
e35fd660
TT
3273 if (allocated > map->m_len)
3274 allocated = map->m_len;
0031462b 3275 ext4_ext_show_leaf(inode, path);
e35fd660
TT
3276 map->m_pblk = newblock;
3277 map->m_len = allocated;
0031462b
MC
3278out2:
3279 if (path) {
3280 ext4_ext_drop_refs(path);
3281 kfree(path);
3282 }
3283 return err ? err : allocated;
3284}
58590b06 3285
c278bfec 3286/*
f5ab0d1f
MC
3287 * Block allocation/map/preallocation routine for extents based files
3288 *
3289 *
c278bfec 3290 * Need to be called with
0e855ac8
AK
3291 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3292 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
f5ab0d1f
MC
3293 *
3294 * return > 0, number of of blocks already mapped/allocated
3295 * if create == 0 and these are pre-allocated blocks
3296 * buffer head is unmapped
3297 * otherwise blocks are mapped
3298 *
3299 * return = 0, if plain look up failed (blocks have not been allocated)
3300 * buffer head is unmapped
3301 *
3302 * return < 0, error case.
c278bfec 3303 */
e35fd660
TT
3304int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3305 struct ext4_map_blocks *map, int flags)
a86c6181
AT
3306{
3307 struct ext4_ext_path *path = NULL;
58590b06 3308 struct ext4_extent newex, *ex;
0562e0ba 3309 ext4_fsblk_t newblock = 0;
b05e6ae5 3310 int err = 0, depth, ret;
498e5f24 3311 unsigned int allocated = 0;
c9de560d 3312 struct ext4_allocation_request ar;
8d5d02e6 3313 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
a86c6181 3314
84fe3bef 3315 ext_debug("blocks %u/%u requested for inode %lu\n",
e35fd660 3316 map->m_lblk, map->m_len, inode->i_ino);
0562e0ba 3317 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
a86c6181
AT
3318
3319 /* check in cache */
b05e6ae5
TT
3320 if (ext4_ext_in_cache(inode, map->m_lblk, &newex)) {
3321 if (!newex.ee_start_lo && !newex.ee_start_hi) {
c2177057 3322 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
56055d3a
AA
3323 /*
3324 * block isn't allocated yet and
3325 * user doesn't want to allocate it
3326 */
a86c6181
AT
3327 goto out2;
3328 }
3329 /* we should allocate requested block */
b05e6ae5 3330 } else {
a86c6181 3331 /* block is already allocated */
e35fd660 3332 newblock = map->m_lblk
8c55e204 3333 - le32_to_cpu(newex.ee_block)
bf89d16f 3334 + ext4_ext_pblock(&newex);
d0d856e8 3335 /* number of remaining blocks in the extent */
b939e376 3336 allocated = ext4_ext_get_actual_len(&newex) -
e35fd660 3337 (map->m_lblk - le32_to_cpu(newex.ee_block));
a86c6181 3338 goto out;
a86c6181
AT
3339 }
3340 }
3341
3342 /* find extent for this block */
e35fd660 3343 path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
a86c6181
AT
3344 if (IS_ERR(path)) {
3345 err = PTR_ERR(path);
3346 path = NULL;
3347 goto out2;
3348 }
3349
3350 depth = ext_depth(inode);
3351
3352 /*
d0d856e8
RD
3353 * consistent leaf must not be empty;
3354 * this situation is possible, though, _during_ tree modification;
a86c6181
AT
3355 * this is why assert can't be put in ext4_ext_find_extent()
3356 */
273df556
FM
3357 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
3358 EXT4_ERROR_INODE(inode, "bad extent address "
f70f362b
TT
3359 "lblock: %lu, depth: %d pblock %lld",
3360 (unsigned long) map->m_lblk, depth,
3361 path[depth].p_block);
034fb4c9
SP
3362 err = -EIO;
3363 goto out2;
3364 }
a86c6181 3365
7e028976
AM
3366 ex = path[depth].p_ext;
3367 if (ex) {
725d26d3 3368 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
bf89d16f 3369 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
a2df2a63 3370 unsigned short ee_len;
471d4011
SB
3371
3372 /*
471d4011 3373 * Uninitialized extents are treated as holes, except that
56055d3a 3374 * we split out initialized portions during a write.
471d4011 3375 */
a2df2a63 3376 ee_len = ext4_ext_get_actual_len(ex);
d0d856e8 3377 /* if found extent covers block, simply return it */
e35fd660
TT
3378 if (in_range(map->m_lblk, ee_block, ee_len)) {
3379 newblock = map->m_lblk - ee_block + ee_start;
d0d856e8 3380 /* number of remaining blocks in the extent */
e35fd660
TT
3381 allocated = ee_len - (map->m_lblk - ee_block);
3382 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
3383 ee_block, ee_len, newblock);
56055d3a 3384
a2df2a63 3385 /* Do not put uninitialized extent in the cache */
56055d3a 3386 if (!ext4_ext_is_uninitialized(ex)) {
a2df2a63 3387 ext4_ext_put_in_cache(inode, ee_block,
b05e6ae5 3388 ee_len, ee_start);
56055d3a
AA
3389 goto out;
3390 }
0031462b 3391 ret = ext4_ext_handle_uninitialized_extents(handle,
e35fd660
TT
3392 inode, map, path, flags, allocated,
3393 newblock);
0031462b 3394 return ret;
a86c6181
AT
3395 }
3396 }
3397
3398 /*
d0d856e8 3399 * requested block isn't allocated yet;
a86c6181
AT
3400 * we couldn't try to create block if create flag is zero
3401 */
c2177057 3402 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
56055d3a
AA
3403 /*
3404 * put just found gap into cache to speed up
3405 * subsequent requests
3406 */
e35fd660 3407 ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
a86c6181
AT
3408 goto out2;
3409 }
3410 /*
c2ea3fde 3411 * Okay, we need to do block allocation.
63f57933 3412 */
a86c6181 3413
c9de560d 3414 /* find neighbour allocated blocks */
e35fd660 3415 ar.lleft = map->m_lblk;
c9de560d
AT
3416 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3417 if (err)
3418 goto out2;
e35fd660 3419 ar.lright = map->m_lblk;
c9de560d
AT
3420 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
3421 if (err)
3422 goto out2;
25d14f98 3423
749269fa
AA
3424 /*
3425 * See if request is beyond maximum number of blocks we can have in
3426 * a single extent. For an initialized extent this limit is
3427 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3428 * EXT_UNINIT_MAX_LEN.
3429 */
e35fd660 3430 if (map->m_len > EXT_INIT_MAX_LEN &&
c2177057 3431 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
e35fd660
TT
3432 map->m_len = EXT_INIT_MAX_LEN;
3433 else if (map->m_len > EXT_UNINIT_MAX_LEN &&
c2177057 3434 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
e35fd660 3435 map->m_len = EXT_UNINIT_MAX_LEN;
749269fa 3436
e35fd660
TT
3437 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
3438 newex.ee_block = cpu_to_le32(map->m_lblk);
3439 newex.ee_len = cpu_to_le16(map->m_len);
25d14f98
AA
3440 err = ext4_ext_check_overlap(inode, &newex, path);
3441 if (err)
b939e376 3442 allocated = ext4_ext_get_actual_len(&newex);
25d14f98 3443 else
e35fd660 3444 allocated = map->m_len;
c9de560d
AT
3445
3446 /* allocate new block */
3447 ar.inode = inode;
e35fd660
TT
3448 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
3449 ar.logical = map->m_lblk;
c9de560d
AT
3450 ar.len = allocated;
3451 if (S_ISREG(inode->i_mode))
3452 ar.flags = EXT4_MB_HINT_DATA;
3453 else
3454 /* disable in-core preallocation for non-regular files */
3455 ar.flags = 0;
3456 newblock = ext4_mb_new_blocks(handle, &ar, &err);
a86c6181
AT
3457 if (!newblock)
3458 goto out2;
84fe3bef 3459 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
498e5f24 3460 ar.goal, newblock, allocated);
a86c6181
AT
3461
3462 /* try to insert new extent into found leaf and return */
f65e6fba 3463 ext4_ext_store_pblock(&newex, newblock);
c9de560d 3464 newex.ee_len = cpu_to_le16(ar.len);
8d5d02e6
MC
3465 /* Mark uninitialized */
3466 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
a2df2a63 3467 ext4_ext_mark_uninitialized(&newex);
8d5d02e6 3468 /*
744692dc 3469 * io_end structure was created for every IO write to an
25985edc 3470 * uninitialized extent. To avoid unnecessary conversion,
744692dc 3471 * here we flag the IO that really needs the conversion.
5f524950 3472 * For non asycn direct IO case, flag the inode state
25985edc 3473 * that we need to perform conversion when IO is done.
8d5d02e6 3474 */
744692dc 3475 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
e9e3bcec 3476 if (io && !(io->flag & EXT4_IO_END_UNWRITTEN)) {
bd2d0210 3477 io->flag = EXT4_IO_END_UNWRITTEN;
e9e3bcec
ES
3478 atomic_inc(&EXT4_I(inode)->i_aiodio_unwritten);
3479 } else
19f5fb7a
TT
3480 ext4_set_inode_state(inode,
3481 EXT4_STATE_DIO_UNWRITTEN);
5f524950 3482 }
744692dc 3483 if (ext4_should_dioread_nolock(inode))
e35fd660 3484 map->m_flags |= EXT4_MAP_UNINIT;
8d5d02e6 3485 }
c8d46e41 3486
d002ebf1 3487 err = check_eofblocks_fl(handle, inode, map->m_lblk, path, ar.len);
58590b06
TT
3488 if (err)
3489 goto out2;
3490
0031462b 3491 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
315054f0
AT
3492 if (err) {
3493 /* free data blocks we just allocated */
c9de560d
AT
3494 /* not a good idea to call discard here directly,
3495 * but otherwise we'd need to call it every free() */
c2ea3fde 3496 ext4_discard_preallocations(inode);
7dc57615 3497 ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex),
e6362609 3498 ext4_ext_get_actual_len(&newex), 0);
a86c6181 3499 goto out2;
315054f0 3500 }
a86c6181 3501
a86c6181 3502 /* previous routine could use block we allocated */
bf89d16f 3503 newblock = ext4_ext_pblock(&newex);
b939e376 3504 allocated = ext4_ext_get_actual_len(&newex);
e35fd660
TT
3505 if (allocated > map->m_len)
3506 allocated = map->m_len;
3507 map->m_flags |= EXT4_MAP_NEW;
a86c6181 3508
5f634d06
AK
3509 /*
3510 * Update reserved blocks/metadata blocks after successful
3511 * block allocation which had been deferred till now.
3512 */
1296cc85 3513 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
5f634d06
AK
3514 ext4_da_update_reserve_space(inode, allocated, 1);
3515
b436b9be
JK
3516 /*
3517 * Cache the extent and update transaction to commit on fdatasync only
3518 * when it is _not_ an uninitialized extent.
3519 */
3520 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
b05e6ae5 3521 ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock);
b436b9be
JK
3522 ext4_update_inode_fsync_trans(handle, inode, 1);
3523 } else
3524 ext4_update_inode_fsync_trans(handle, inode, 0);
a86c6181 3525out:
e35fd660
TT
3526 if (allocated > map->m_len)
3527 allocated = map->m_len;
a86c6181 3528 ext4_ext_show_leaf(inode, path);
e35fd660
TT
3529 map->m_flags |= EXT4_MAP_MAPPED;
3530 map->m_pblk = newblock;
3531 map->m_len = allocated;
a86c6181
AT
3532out2:
3533 if (path) {
3534 ext4_ext_drop_refs(path);
3535 kfree(path);
3536 }
0562e0ba
JZ
3537 trace_ext4_ext_map_blocks_exit(inode, map->m_lblk,
3538 newblock, map->m_len, err ? err : allocated);
a86c6181
AT
3539 return err ? err : allocated;
3540}
3541
cf108bca 3542void ext4_ext_truncate(struct inode *inode)
a86c6181
AT
3543{
3544 struct address_space *mapping = inode->i_mapping;
3545 struct super_block *sb = inode->i_sb;
725d26d3 3546 ext4_lblk_t last_block;
a86c6181
AT
3547 handle_t *handle;
3548 int err = 0;
3549
3889fd57
JZ
3550 /*
3551 * finish any pending end_io work so we won't run the risk of
3552 * converting any truncated blocks to initialized later
3553 */
3554 ext4_flush_completed_IO(inode);
3555
a86c6181
AT
3556 /*
3557 * probably first extent we're gonna free will be last in block
3558 */
f3bd1f3f 3559 err = ext4_writepage_trans_blocks(inode);
a86c6181 3560 handle = ext4_journal_start(inode, err);
cf108bca 3561 if (IS_ERR(handle))
a86c6181 3562 return;
a86c6181 3563
cf108bca
JK
3564 if (inode->i_size & (sb->s_blocksize - 1))
3565 ext4_block_truncate_page(handle, mapping, inode->i_size);
a86c6181 3566
9ddfc3dc
JK
3567 if (ext4_orphan_add(handle, inode))
3568 goto out_stop;
3569
0e855ac8 3570 down_write(&EXT4_I(inode)->i_data_sem);
a86c6181
AT
3571 ext4_ext_invalidate_cache(inode);
3572
c2ea3fde 3573 ext4_discard_preallocations(inode);
c9de560d 3574
a86c6181 3575 /*
d0d856e8
RD
3576 * TODO: optimization is possible here.
3577 * Probably we need not scan at all,
3578 * because page truncation is enough.
a86c6181 3579 */
a86c6181
AT
3580
3581 /* we have to know where to truncate from in crash case */
3582 EXT4_I(inode)->i_disksize = inode->i_size;
3583 ext4_mark_inode_dirty(handle, inode);
3584
3585 last_block = (inode->i_size + sb->s_blocksize - 1)
3586 >> EXT4_BLOCK_SIZE_BITS(sb);
3587 err = ext4_ext_remove_space(inode, last_block);
3588
3589 /* In a multi-transaction truncate, we only make the final
56055d3a
AA
3590 * transaction synchronous.
3591 */
a86c6181 3592 if (IS_SYNC(inode))
0390131b 3593 ext4_handle_sync(handle);
a86c6181
AT
3594
3595out_stop:
9ddfc3dc 3596 up_write(&EXT4_I(inode)->i_data_sem);
a86c6181 3597 /*
d0d856e8 3598 * If this was a simple ftruncate() and the file will remain alive,
a86c6181
AT
3599 * then we need to clear up the orphan record which we created above.
3600 * However, if this was a real unlink then we were called by
3601 * ext4_delete_inode(), and we allow that function to clean up the
3602 * orphan info for us.
3603 */
3604 if (inode->i_nlink)
3605 ext4_orphan_del(handle, inode);
3606
ef737728
SR
3607 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3608 ext4_mark_inode_dirty(handle, inode);
a86c6181
AT
3609 ext4_journal_stop(handle);
3610}
3611
fd28784a
AK
3612static void ext4_falloc_update_inode(struct inode *inode,
3613 int mode, loff_t new_size, int update_ctime)
3614{
3615 struct timespec now;
3616
3617 if (update_ctime) {
3618 now = current_fs_time(inode->i_sb);
3619 if (!timespec_equal(&inode->i_ctime, &now))
3620 inode->i_ctime = now;
3621 }
3622 /*
3623 * Update only when preallocation was requested beyond
3624 * the file size.
3625 */
cf17fea6
AK
3626 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3627 if (new_size > i_size_read(inode))
3628 i_size_write(inode, new_size);
3629 if (new_size > EXT4_I(inode)->i_disksize)
3630 ext4_update_i_disksize(inode, new_size);
c8d46e41
JZ
3631 } else {
3632 /*
3633 * Mark that we allocate beyond EOF so the subsequent truncate
3634 * can proceed even if the new size is the same as i_size.
3635 */
3636 if (new_size > i_size_read(inode))
12e9b892 3637 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
fd28784a
AK
3638 }
3639
3640}
3641
a2df2a63 3642/*
2fe17c10 3643 * preallocate space for a file. This implements ext4's fallocate file
a2df2a63
AA
3644 * operation, which gets called from sys_fallocate system call.
3645 * For block-mapped files, posix_fallocate should fall back to the method
3646 * of writing zeroes to the required new blocks (the same behavior which is
3647 * expected for file systems which do not support fallocate() system call).
3648 */
2fe17c10 3649long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
a2df2a63 3650{
2fe17c10 3651 struct inode *inode = file->f_path.dentry->d_inode;
a2df2a63 3652 handle_t *handle;
fd28784a 3653 loff_t new_size;
498e5f24 3654 unsigned int max_blocks;
a2df2a63
AA
3655 int ret = 0;
3656 int ret2 = 0;
3657 int retries = 0;
2ed88685 3658 struct ext4_map_blocks map;
a2df2a63
AA
3659 unsigned int credits, blkbits = inode->i_blkbits;
3660
d6dc8462 3661 /* We only support the FALLOC_FL_KEEP_SIZE mode */
64c23e86 3662 if (mode & ~FALLOC_FL_KEEP_SIZE)
d6dc8462
JB
3663 return -EOPNOTSUPP;
3664
a2df2a63
AA
3665 /*
3666 * currently supporting (pre)allocate mode for extent-based
3667 * files _only_
3668 */
12e9b892 3669 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
a2df2a63
AA
3670 return -EOPNOTSUPP;
3671
0562e0ba 3672 trace_ext4_fallocate_enter(inode, offset, len, mode);
2ed88685 3673 map.m_lblk = offset >> blkbits;
fd28784a
AK
3674 /*
3675 * We can't just convert len to max_blocks because
3676 * If blocksize = 4096 offset = 3072 and len = 2048
3677 */
a2df2a63 3678 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
2ed88685 3679 - map.m_lblk;
a2df2a63 3680 /*
f3bd1f3f 3681 * credits to insert 1 extent into extent tree
a2df2a63 3682 */
f3bd1f3f 3683 credits = ext4_chunk_trans_blocks(inode, max_blocks);
55bd725a 3684 mutex_lock(&inode->i_mutex);
6d19c42b
NK
3685 ret = inode_newsize_ok(inode, (len + offset));
3686 if (ret) {
3687 mutex_unlock(&inode->i_mutex);
0562e0ba 3688 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
6d19c42b
NK
3689 return ret;
3690 }
a2df2a63
AA
3691retry:
3692 while (ret >= 0 && ret < max_blocks) {
2ed88685
TT
3693 map.m_lblk = map.m_lblk + ret;
3694 map.m_len = max_blocks = max_blocks - ret;
a2df2a63
AA
3695 handle = ext4_journal_start(inode, credits);
3696 if (IS_ERR(handle)) {
3697 ret = PTR_ERR(handle);
3698 break;
3699 }
2ed88685 3700 ret = ext4_map_blocks(handle, inode, &map,
c2177057 3701 EXT4_GET_BLOCKS_CREATE_UNINIT_EXT);
221879c9 3702 if (ret <= 0) {
2c98615d
AK
3703#ifdef EXT4FS_DEBUG
3704 WARN_ON(ret <= 0);
e35fd660 3705 printk(KERN_ERR "%s: ext4_ext_map_blocks "
2c98615d 3706 "returned error inode#%lu, block=%u, "
9fd9784c 3707 "max_blocks=%u", __func__,
a6371b63 3708 inode->i_ino, map.m_lblk, max_blocks);
2c98615d 3709#endif
a2df2a63
AA
3710 ext4_mark_inode_dirty(handle, inode);
3711 ret2 = ext4_journal_stop(handle);
3712 break;
3713 }
2ed88685 3714 if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
fd28784a
AK
3715 blkbits) >> blkbits))
3716 new_size = offset + len;
3717 else
2ed88685 3718 new_size = (map.m_lblk + ret) << blkbits;
a2df2a63 3719
fd28784a 3720 ext4_falloc_update_inode(inode, mode, new_size,
2ed88685 3721 (map.m_flags & EXT4_MAP_NEW));
a2df2a63
AA
3722 ext4_mark_inode_dirty(handle, inode);
3723 ret2 = ext4_journal_stop(handle);
3724 if (ret2)
3725 break;
3726 }
fd28784a
AK
3727 if (ret == -ENOSPC &&
3728 ext4_should_retry_alloc(inode->i_sb, &retries)) {
3729 ret = 0;
a2df2a63 3730 goto retry;
a2df2a63 3731 }
55bd725a 3732 mutex_unlock(&inode->i_mutex);
0562e0ba
JZ
3733 trace_ext4_fallocate_exit(inode, offset, max_blocks,
3734 ret > 0 ? ret2 : ret);
a2df2a63
AA
3735 return ret > 0 ? ret2 : ret;
3736}
6873fa0d 3737
0031462b
MC
3738/*
3739 * This function convert a range of blocks to written extents
3740 * The caller of this function will pass the start offset and the size.
3741 * all unwritten extents within this range will be converted to
3742 * written extents.
3743 *
3744 * This function is called from the direct IO end io call back
3745 * function, to convert the fallocated extents after IO is completed.
109f5565 3746 * Returns 0 on success.
0031462b
MC
3747 */
3748int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
a1de02dc 3749 ssize_t len)
0031462b
MC
3750{
3751 handle_t *handle;
0031462b
MC
3752 unsigned int max_blocks;
3753 int ret = 0;
3754 int ret2 = 0;
2ed88685 3755 struct ext4_map_blocks map;
0031462b
MC
3756 unsigned int credits, blkbits = inode->i_blkbits;
3757
2ed88685 3758 map.m_lblk = offset >> blkbits;
0031462b
MC
3759 /*
3760 * We can't just convert len to max_blocks because
3761 * If blocksize = 4096 offset = 3072 and len = 2048
3762 */
2ed88685
TT
3763 max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
3764 map.m_lblk);
0031462b
MC
3765 /*
3766 * credits to insert 1 extent into extent tree
3767 */
3768 credits = ext4_chunk_trans_blocks(inode, max_blocks);
3769 while (ret >= 0 && ret < max_blocks) {
2ed88685
TT
3770 map.m_lblk += ret;
3771 map.m_len = (max_blocks -= ret);
0031462b
MC
3772 handle = ext4_journal_start(inode, credits);
3773 if (IS_ERR(handle)) {
3774 ret = PTR_ERR(handle);
3775 break;
3776 }
2ed88685 3777 ret = ext4_map_blocks(handle, inode, &map,
c7064ef1 3778 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
0031462b
MC
3779 if (ret <= 0) {
3780 WARN_ON(ret <= 0);
e35fd660 3781 printk(KERN_ERR "%s: ext4_ext_map_blocks "
0031462b
MC
3782 "returned error inode#%lu, block=%u, "
3783 "max_blocks=%u", __func__,
2ed88685 3784 inode->i_ino, map.m_lblk, map.m_len);
0031462b
MC
3785 }
3786 ext4_mark_inode_dirty(handle, inode);
3787 ret2 = ext4_journal_stop(handle);
3788 if (ret <= 0 || ret2 )
3789 break;
3790 }
3791 return ret > 0 ? ret2 : ret;
3792}
6d9c85eb 3793
6873fa0d
ES
3794/*
3795 * Callback function called for each extent to gather FIEMAP information.
3796 */
3a06d778 3797static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path,
6873fa0d
ES
3798 struct ext4_ext_cache *newex, struct ext4_extent *ex,
3799 void *data)
3800{
6873fa0d
ES
3801 __u64 logical;
3802 __u64 physical;
3803 __u64 length;
6d9c85eb 3804 loff_t size;
6873fa0d 3805 __u32 flags = 0;
6d9c85eb
YY
3806 int ret = 0;
3807 struct fiemap_extent_info *fieinfo = data;
3808 unsigned char blksize_bits;
6873fa0d 3809
6d9c85eb
YY
3810 blksize_bits = inode->i_sb->s_blocksize_bits;
3811 logical = (__u64)newex->ec_block << blksize_bits;
6873fa0d 3812
b05e6ae5 3813 if (newex->ec_start == 0) {
6d9c85eb
YY
3814 /*
3815 * No extent in extent-tree contains block @newex->ec_start,
3816 * then the block may stay in 1)a hole or 2)delayed-extent.
3817 *
3818 * Holes or delayed-extents are processed as follows.
3819 * 1. lookup dirty pages with specified range in pagecache.
3820 * If no page is got, then there is no delayed-extent and
3821 * return with EXT_CONTINUE.
3822 * 2. find the 1st mapped buffer,
3823 * 3. check if the mapped buffer is both in the request range
3824 * and a delayed buffer. If not, there is no delayed-extent,
3825 * then return.
3826 * 4. a delayed-extent is found, the extent will be collected.
3827 */
3828 ext4_lblk_t end = 0;
3829 pgoff_t last_offset;
3830 pgoff_t offset;
3831 pgoff_t index;
3832 struct page **pages = NULL;
6873fa0d 3833 struct buffer_head *bh = NULL;
6d9c85eb
YY
3834 struct buffer_head *head = NULL;
3835 unsigned int nr_pages = PAGE_SIZE / sizeof(struct page *);
3836
3837 pages = kmalloc(PAGE_SIZE, GFP_KERNEL);
3838 if (pages == NULL)
3839 return -ENOMEM;
6873fa0d
ES
3840
3841 offset = logical >> PAGE_SHIFT;
6d9c85eb
YY
3842repeat:
3843 last_offset = offset;
3844 head = NULL;
3845 ret = find_get_pages_tag(inode->i_mapping, &offset,
3846 PAGECACHE_TAG_DIRTY, nr_pages, pages);
3847
3848 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
3849 /* First time, try to find a mapped buffer. */
3850 if (ret == 0) {
3851out:
3852 for (index = 0; index < ret; index++)
3853 page_cache_release(pages[index]);
3854 /* just a hole. */
3855 kfree(pages);
3856 return EXT_CONTINUE;
3857 }
6873fa0d 3858
6d9c85eb
YY
3859 /* Try to find the 1st mapped buffer. */
3860 end = ((__u64)pages[0]->index << PAGE_SHIFT) >>
3861 blksize_bits;
3862 if (!page_has_buffers(pages[0]))
3863 goto out;
3864 head = page_buffers(pages[0]);
3865 if (!head)
3866 goto out;
6873fa0d 3867
6d9c85eb
YY
3868 bh = head;
3869 do {
3870 if (buffer_mapped(bh)) {
3871 /* get the 1st mapped buffer. */
3872 if (end > newex->ec_block +
3873 newex->ec_len)
3874 /* The buffer is out of
3875 * the request range.
3876 */
3877 goto out;
3878 goto found_mapped_buffer;
3879 }
3880 bh = bh->b_this_page;
3881 end++;
3882 } while (bh != head);
6873fa0d 3883
6d9c85eb
YY
3884 /* No mapped buffer found. */
3885 goto out;
6873fa0d 3886 } else {
6d9c85eb
YY
3887 /*Find contiguous delayed buffers. */
3888 if (ret > 0 && pages[0]->index == last_offset)
3889 head = page_buffers(pages[0]);
3890 bh = head;
6873fa0d 3891 }
6d9c85eb
YY
3892
3893found_mapped_buffer:
3894 if (bh != NULL && buffer_delay(bh)) {
3895 /* 1st or contiguous delayed buffer found. */
3896 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
3897 /*
3898 * 1st delayed buffer found, record
3899 * the start of extent.
3900 */
3901 flags |= FIEMAP_EXTENT_DELALLOC;
3902 newex->ec_block = end;
3903 logical = (__u64)end << blksize_bits;
3904 }
3905 /* Find contiguous delayed buffers. */
3906 do {
3907 if (!buffer_delay(bh))
3908 goto found_delayed_extent;
3909 bh = bh->b_this_page;
3910 end++;
3911 } while (bh != head);
3912
3913 for (index = 1; index < ret; index++) {
3914 if (!page_has_buffers(pages[index])) {
3915 bh = NULL;
3916 break;
3917 }
3918 head = page_buffers(pages[index]);
3919 if (!head) {
3920 bh = NULL;
3921 break;
3922 }
3923 if (pages[index]->index !=
3924 pages[0]->index + index) {
3925 /* Blocks are not contiguous. */
3926 bh = NULL;
3927 break;
3928 }
3929 bh = head;
3930 do {
3931 if (!buffer_delay(bh))
3932 /* Delayed-extent ends. */
3933 goto found_delayed_extent;
3934 bh = bh->b_this_page;
3935 end++;
3936 } while (bh != head);
3937 }
3938 } else if (!(flags & FIEMAP_EXTENT_DELALLOC))
3939 /* a hole found. */
3940 goto out;
3941
3942found_delayed_extent:
3943 newex->ec_len = min(end - newex->ec_block,
3944 (ext4_lblk_t)EXT_INIT_MAX_LEN);
3945 if (ret == nr_pages && bh != NULL &&
3946 newex->ec_len < EXT_INIT_MAX_LEN &&
3947 buffer_delay(bh)) {
3948 /* Have not collected an extent and continue. */
3949 for (index = 0; index < ret; index++)
3950 page_cache_release(pages[index]);
3951 goto repeat;
6873fa0d 3952 }
6d9c85eb
YY
3953
3954 for (index = 0; index < ret; index++)
3955 page_cache_release(pages[index]);
3956 kfree(pages);
6873fa0d
ES
3957 }
3958
3959 physical = (__u64)newex->ec_start << blksize_bits;
3960 length = (__u64)newex->ec_len << blksize_bits;
3961
3962 if (ex && ext4_ext_is_uninitialized(ex))
3963 flags |= FIEMAP_EXTENT_UNWRITTEN;
3964
6d9c85eb
YY
3965 size = i_size_read(inode);
3966 if (logical + length >= size)
6873fa0d
ES
3967 flags |= FIEMAP_EXTENT_LAST;
3968
6d9c85eb 3969 ret = fiemap_fill_next_extent(fieinfo, logical, physical,
6873fa0d 3970 length, flags);
6d9c85eb
YY
3971 if (ret < 0)
3972 return ret;
3973 if (ret == 1)
6873fa0d 3974 return EXT_BREAK;
6873fa0d
ES
3975 return EXT_CONTINUE;
3976}
3977
3978/* fiemap flags we can handle specified here */
3979#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
3980
3a06d778
AK
3981static int ext4_xattr_fiemap(struct inode *inode,
3982 struct fiemap_extent_info *fieinfo)
6873fa0d
ES
3983{
3984 __u64 physical = 0;
3985 __u64 length;
3986 __u32 flags = FIEMAP_EXTENT_LAST;
3987 int blockbits = inode->i_sb->s_blocksize_bits;
3988 int error = 0;
3989
3990 /* in-inode? */
19f5fb7a 3991 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
6873fa0d
ES
3992 struct ext4_iloc iloc;
3993 int offset; /* offset of xattr in inode */
3994
3995 error = ext4_get_inode_loc(inode, &iloc);
3996 if (error)
3997 return error;
3998 physical = iloc.bh->b_blocknr << blockbits;
3999 offset = EXT4_GOOD_OLD_INODE_SIZE +
4000 EXT4_I(inode)->i_extra_isize;
4001 physical += offset;
4002 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
4003 flags |= FIEMAP_EXTENT_DATA_INLINE;
fd2dd9fb 4004 brelse(iloc.bh);
6873fa0d
ES
4005 } else { /* external block */
4006 physical = EXT4_I(inode)->i_file_acl << blockbits;
4007 length = inode->i_sb->s_blocksize;
4008 }
4009
4010 if (physical)
4011 error = fiemap_fill_next_extent(fieinfo, 0, physical,
4012 length, flags);
4013 return (error < 0 ? error : 0);
4014}
4015
4016int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4017 __u64 start, __u64 len)
4018{
4019 ext4_lblk_t start_blk;
6873fa0d
ES
4020 int error = 0;
4021
4022 /* fallback to generic here if not in extents fmt */
12e9b892 4023 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
6873fa0d
ES
4024 return generic_block_fiemap(inode, fieinfo, start, len,
4025 ext4_get_block);
4026
4027 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
4028 return -EBADR;
4029
4030 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
4031 error = ext4_xattr_fiemap(inode, fieinfo);
4032 } else {
aca92ff6
LM
4033 ext4_lblk_t len_blks;
4034 __u64 last_blk;
4035
6873fa0d 4036 start_blk = start >> inode->i_sb->s_blocksize_bits;
aca92ff6
LM
4037 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
4038 if (last_blk >= EXT_MAX_BLOCK)
4039 last_blk = EXT_MAX_BLOCK-1;
4040 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
6873fa0d
ES
4041
4042 /*
4043 * Walk the extent tree gathering extent information.
4044 * ext4_ext_fiemap_cb will push extents back to user.
4045 */
6873fa0d
ES
4046 error = ext4_ext_walk_space(inode, start_blk, len_blks,
4047 ext4_ext_fiemap_cb, fieinfo);
6873fa0d
ES
4048 }
4049
4050 return error;
4051}
4052