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