]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/ext4/extents.c
ext4: remove ext4_new_meta_block()
[mirror_ubuntu-zesty-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
AT
46
47
d0d856e8
RD
48/*
49 * ext_pblock:
50 * combine low and high parts of physical block number into ext4_fsblk_t
51 */
09b88252 52static ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
f65e6fba
AT
53{
54 ext4_fsblk_t block;
55
b377611d 56 block = le32_to_cpu(ex->ee_start_lo);
9b8f1f01 57 block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1;
f65e6fba
AT
58 return block;
59}
60
d0d856e8
RD
61/*
62 * idx_pblock:
63 * combine low and high parts of a leaf physical block number into ext4_fsblk_t
64 */
c14c6fd5 65ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
f65e6fba
AT
66{
67 ext4_fsblk_t block;
68
d8dd0b45 69 block = le32_to_cpu(ix->ei_leaf_lo);
9b8f1f01 70 block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1;
f65e6fba
AT
71 return block;
72}
73
d0d856e8
RD
74/*
75 * ext4_ext_store_pblock:
76 * stores a large physical block number into an extent struct,
77 * breaking it into parts
78 */
c14c6fd5 79void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
f65e6fba 80{
b377611d 81 ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
9b8f1f01 82 ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
f65e6fba
AT
83}
84
d0d856e8
RD
85/*
86 * ext4_idx_store_pblock:
87 * stores a large physical block number into an index struct,
88 * breaking it into parts
89 */
09b88252 90static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb)
f65e6fba 91{
d8dd0b45 92 ix->ei_leaf_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
9b8f1f01 93 ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
f65e6fba
AT
94}
95
9102e4fa 96static int ext4_ext_journal_restart(handle_t *handle, int needed)
a86c6181
AT
97{
98 int err;
99
100 if (handle->h_buffer_credits > needed)
9102e4fa
SF
101 return 0;
102 err = ext4_journal_extend(handle, needed);
0123c939 103 if (err <= 0)
9102e4fa
SF
104 return err;
105 return ext4_journal_restart(handle, needed);
a86c6181
AT
106}
107
108/*
109 * could return:
110 * - EROFS
111 * - ENOMEM
112 */
113static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
114 struct ext4_ext_path *path)
115{
116 if (path->p_bh) {
117 /* path points to block */
118 return ext4_journal_get_write_access(handle, path->p_bh);
119 }
120 /* path points to leaf/index in inode body */
121 /* we use in-core data, no need to protect them */
122 return 0;
123}
124
125/*
126 * could return:
127 * - EROFS
128 * - ENOMEM
129 * - EIO
130 */
131static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
132 struct ext4_ext_path *path)
133{
134 int err;
135 if (path->p_bh) {
136 /* path points to block */
137 err = ext4_journal_dirty_metadata(handle, path->p_bh);
138 } else {
139 /* path points to leaf/index in inode body */
140 err = ext4_mark_inode_dirty(handle, inode);
141 }
142 return err;
143}
144
f65e6fba 145static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
a86c6181 146 struct ext4_ext_path *path,
725d26d3 147 ext4_lblk_t block)
a86c6181
AT
148{
149 struct ext4_inode_info *ei = EXT4_I(inode);
f65e6fba 150 ext4_fsblk_t bg_start;
74d3487f 151 ext4_fsblk_t last_block;
f65e6fba 152 ext4_grpblk_t colour;
a86c6181
AT
153 int depth;
154
155 if (path) {
156 struct ext4_extent *ex;
157 depth = path->p_depth;
158
159 /* try to predict block placement */
7e028976
AM
160 ex = path[depth].p_ext;
161 if (ex)
f65e6fba 162 return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
a86c6181 163
d0d856e8
RD
164 /* it looks like index is empty;
165 * try to find starting block from index itself */
a86c6181
AT
166 if (path[depth].p_bh)
167 return path[depth].p_bh->b_blocknr;
168 }
169
170 /* OK. use inode's group */
171 bg_start = (ei->i_block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) +
172 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block);
74d3487f
VC
173 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
174
175 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
176 colour = (current->pid % 16) *
a86c6181 177 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
74d3487f
VC
178 else
179 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
a86c6181
AT
180 return bg_start + colour + block;
181}
182
654b4908
AK
183/*
184 * Allocation for a meta data block
185 */
f65e6fba 186static ext4_fsblk_t
654b4908 187ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
a86c6181
AT
188 struct ext4_ext_path *path,
189 struct ext4_extent *ex, int *err)
190{
f65e6fba 191 ext4_fsblk_t goal, newblock;
cfe82c85 192 unsigned long count = 1;
a86c6181
AT
193
194 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
cfe82c85 195 newblock = ext4_new_meta_blocks(handle, inode, goal, &count, err);
a86c6181
AT
196 return newblock;
197}
198
09b88252 199static int ext4_ext_space_block(struct inode *inode)
a86c6181
AT
200{
201 int size;
202
203 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
204 / sizeof(struct ext4_extent);
bbf2f9fb 205#ifdef AGGRESSIVE_TEST
a86c6181
AT
206 if (size > 6)
207 size = 6;
208#endif
209 return size;
210}
211
09b88252 212static int ext4_ext_space_block_idx(struct inode *inode)
a86c6181
AT
213{
214 int size;
215
216 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
217 / sizeof(struct ext4_extent_idx);
bbf2f9fb 218#ifdef AGGRESSIVE_TEST
a86c6181
AT
219 if (size > 5)
220 size = 5;
221#endif
222 return size;
223}
224
09b88252 225static int ext4_ext_space_root(struct inode *inode)
a86c6181
AT
226{
227 int size;
228
229 size = sizeof(EXT4_I(inode)->i_data);
230 size -= sizeof(struct ext4_extent_header);
231 size /= sizeof(struct ext4_extent);
bbf2f9fb 232#ifdef AGGRESSIVE_TEST
a86c6181
AT
233 if (size > 3)
234 size = 3;
235#endif
236 return size;
237}
238
09b88252 239static int ext4_ext_space_root_idx(struct inode *inode)
a86c6181
AT
240{
241 int size;
242
243 size = sizeof(EXT4_I(inode)->i_data);
244 size -= sizeof(struct ext4_extent_header);
245 size /= sizeof(struct ext4_extent_idx);
bbf2f9fb 246#ifdef AGGRESSIVE_TEST
a86c6181
AT
247 if (size > 4)
248 size = 4;
249#endif
250 return size;
251}
252
d2a17637
MC
253/*
254 * Calculate the number of metadata blocks needed
255 * to allocate @blocks
256 * Worse case is one block per extent
257 */
258int ext4_ext_calc_metadata_amount(struct inode *inode, int blocks)
259{
260 int lcap, icap, rcap, leafs, idxs, num;
261 int newextents = blocks;
262
263 rcap = ext4_ext_space_root_idx(inode);
264 lcap = ext4_ext_space_block(inode);
265 icap = ext4_ext_space_block_idx(inode);
266
267 /* number of new leaf blocks needed */
268 num = leafs = (newextents + lcap - 1) / lcap;
269
270 /*
271 * Worse case, we need separate index block(s)
272 * to link all new leaf blocks
273 */
274 idxs = (leafs + icap - 1) / icap;
275 do {
276 num += idxs;
277 idxs = (idxs + icap - 1) / icap;
278 } while (idxs > rcap);
279
280 return num;
281}
282
c29c0ae7
AT
283static int
284ext4_ext_max_entries(struct inode *inode, int depth)
285{
286 int max;
287
288 if (depth == ext_depth(inode)) {
289 if (depth == 0)
290 max = ext4_ext_space_root(inode);
291 else
292 max = ext4_ext_space_root_idx(inode);
293 } else {
294 if (depth == 0)
295 max = ext4_ext_space_block(inode);
296 else
297 max = ext4_ext_space_block_idx(inode);
298 }
299
300 return max;
301}
302
303static int __ext4_ext_check_header(const char *function, struct inode *inode,
304 struct ext4_extent_header *eh,
305 int depth)
306{
307 const char *error_msg;
308 int max = 0;
309
310 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
311 error_msg = "invalid magic";
312 goto corrupted;
313 }
314 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
315 error_msg = "unexpected eh_depth";
316 goto corrupted;
317 }
318 if (unlikely(eh->eh_max == 0)) {
319 error_msg = "invalid eh_max";
320 goto corrupted;
321 }
322 max = ext4_ext_max_entries(inode, depth);
323 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
324 error_msg = "too large eh_max";
325 goto corrupted;
326 }
327 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
328 error_msg = "invalid eh_entries";
329 goto corrupted;
330 }
331 return 0;
332
333corrupted:
334 ext4_error(inode->i_sb, function,
335 "bad header in inode #%lu: %s - magic %x, "
336 "entries %u, max %u(%u), depth %u(%u)",
337 inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
338 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
339 max, le16_to_cpu(eh->eh_depth), depth);
340
341 return -EIO;
342}
343
344#define ext4_ext_check_header(inode, eh, depth) \
46e665e9 345 __ext4_ext_check_header(__func__, inode, eh, depth)
c29c0ae7 346
a86c6181
AT
347#ifdef EXT_DEBUG
348static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
349{
350 int k, l = path->p_depth;
351
352 ext_debug("path:");
353 for (k = 0; k <= l; k++, path++) {
354 if (path->p_idx) {
2ae02107 355 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
f65e6fba 356 idx_pblock(path->p_idx));
a86c6181 357 } else if (path->p_ext) {
2ae02107 358 ext_debug(" %d:%d:%llu ",
a86c6181 359 le32_to_cpu(path->p_ext->ee_block),
a2df2a63 360 ext4_ext_get_actual_len(path->p_ext),
f65e6fba 361 ext_pblock(path->p_ext));
a86c6181
AT
362 } else
363 ext_debug(" []");
364 }
365 ext_debug("\n");
366}
367
368static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
369{
370 int depth = ext_depth(inode);
371 struct ext4_extent_header *eh;
372 struct ext4_extent *ex;
373 int i;
374
375 if (!path)
376 return;
377
378 eh = path[depth].p_hdr;
379 ex = EXT_FIRST_EXTENT(eh);
380
381 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
2ae02107 382 ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
a2df2a63 383 ext4_ext_get_actual_len(ex), ext_pblock(ex));
a86c6181
AT
384 }
385 ext_debug("\n");
386}
387#else
af5bc92d
TT
388#define ext4_ext_show_path(inode, path)
389#define ext4_ext_show_leaf(inode, path)
a86c6181
AT
390#endif
391
b35905c1 392void ext4_ext_drop_refs(struct ext4_ext_path *path)
a86c6181
AT
393{
394 int depth = path->p_depth;
395 int i;
396
397 for (i = 0; i <= depth; i++, path++)
398 if (path->p_bh) {
399 brelse(path->p_bh);
400 path->p_bh = NULL;
401 }
402}
403
404/*
d0d856e8
RD
405 * ext4_ext_binsearch_idx:
406 * binary search for the closest index of the given block
c29c0ae7 407 * the header must be checked before calling this
a86c6181
AT
408 */
409static void
725d26d3
AK
410ext4_ext_binsearch_idx(struct inode *inode,
411 struct ext4_ext_path *path, ext4_lblk_t block)
a86c6181
AT
412{
413 struct ext4_extent_header *eh = path->p_hdr;
414 struct ext4_extent_idx *r, *l, *m;
415
a86c6181 416
bba90743 417 ext_debug("binsearch for %u(idx): ", block);
a86c6181
AT
418
419 l = EXT_FIRST_INDEX(eh) + 1;
e9f410b1 420 r = EXT_LAST_INDEX(eh);
a86c6181
AT
421 while (l <= r) {
422 m = l + (r - l) / 2;
423 if (block < le32_to_cpu(m->ei_block))
424 r = m - 1;
425 else
426 l = m + 1;
26d535ed
DM
427 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
428 m, le32_to_cpu(m->ei_block),
429 r, le32_to_cpu(r->ei_block));
a86c6181
AT
430 }
431
432 path->p_idx = l - 1;
f65e6fba 433 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
26d535ed 434 idx_pblock(path->p_idx));
a86c6181
AT
435
436#ifdef CHECK_BINSEARCH
437 {
438 struct ext4_extent_idx *chix, *ix;
439 int k;
440
441 chix = ix = EXT_FIRST_INDEX(eh);
442 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
443 if (k != 0 &&
444 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
4776004f
TT
445 printk(KERN_DEBUG "k=%d, ix=0x%p, "
446 "first=0x%p\n", k,
447 ix, EXT_FIRST_INDEX(eh));
448 printk(KERN_DEBUG "%u <= %u\n",
a86c6181
AT
449 le32_to_cpu(ix->ei_block),
450 le32_to_cpu(ix[-1].ei_block));
451 }
452 BUG_ON(k && le32_to_cpu(ix->ei_block)
8c55e204 453 <= le32_to_cpu(ix[-1].ei_block));
a86c6181
AT
454 if (block < le32_to_cpu(ix->ei_block))
455 break;
456 chix = ix;
457 }
458 BUG_ON(chix != path->p_idx);
459 }
460#endif
461
462}
463
464/*
d0d856e8
RD
465 * ext4_ext_binsearch:
466 * binary search for closest extent of the given block
c29c0ae7 467 * the header must be checked before calling this
a86c6181
AT
468 */
469static void
725d26d3
AK
470ext4_ext_binsearch(struct inode *inode,
471 struct ext4_ext_path *path, ext4_lblk_t block)
a86c6181
AT
472{
473 struct ext4_extent_header *eh = path->p_hdr;
474 struct ext4_extent *r, *l, *m;
475
a86c6181
AT
476 if (eh->eh_entries == 0) {
477 /*
d0d856e8
RD
478 * this leaf is empty:
479 * we get such a leaf in split/add case
a86c6181
AT
480 */
481 return;
482 }
483
bba90743 484 ext_debug("binsearch for %u: ", block);
a86c6181
AT
485
486 l = EXT_FIRST_EXTENT(eh) + 1;
e9f410b1 487 r = EXT_LAST_EXTENT(eh);
a86c6181
AT
488
489 while (l <= r) {
490 m = l + (r - l) / 2;
491 if (block < le32_to_cpu(m->ee_block))
492 r = m - 1;
493 else
494 l = m + 1;
26d535ed
DM
495 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
496 m, le32_to_cpu(m->ee_block),
497 r, le32_to_cpu(r->ee_block));
a86c6181
AT
498 }
499
500 path->p_ext = l - 1;
2ae02107 501 ext_debug(" -> %d:%llu:%d ",
8c55e204
DK
502 le32_to_cpu(path->p_ext->ee_block),
503 ext_pblock(path->p_ext),
a2df2a63 504 ext4_ext_get_actual_len(path->p_ext));
a86c6181
AT
505
506#ifdef CHECK_BINSEARCH
507 {
508 struct ext4_extent *chex, *ex;
509 int k;
510
511 chex = ex = EXT_FIRST_EXTENT(eh);
512 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
513 BUG_ON(k && le32_to_cpu(ex->ee_block)
8c55e204 514 <= le32_to_cpu(ex[-1].ee_block));
a86c6181
AT
515 if (block < le32_to_cpu(ex->ee_block))
516 break;
517 chex = ex;
518 }
519 BUG_ON(chex != path->p_ext);
520 }
521#endif
522
523}
524
525int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
526{
527 struct ext4_extent_header *eh;
528
529 eh = ext_inode_hdr(inode);
530 eh->eh_depth = 0;
531 eh->eh_entries = 0;
532 eh->eh_magic = EXT4_EXT_MAGIC;
533 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
534 ext4_mark_inode_dirty(handle, inode);
535 ext4_ext_invalidate_cache(inode);
536 return 0;
537}
538
539struct ext4_ext_path *
725d26d3
AK
540ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
541 struct ext4_ext_path *path)
a86c6181
AT
542{
543 struct ext4_extent_header *eh;
544 struct buffer_head *bh;
545 short int depth, i, ppos = 0, alloc = 0;
546
547 eh = ext_inode_hdr(inode);
c29c0ae7
AT
548 depth = ext_depth(inode);
549 if (ext4_ext_check_header(inode, eh, depth))
a86c6181
AT
550 return ERR_PTR(-EIO);
551
a86c6181
AT
552
553 /* account possible depth increase */
554 if (!path) {
5d4958f9 555 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
a86c6181
AT
556 GFP_NOFS);
557 if (!path)
558 return ERR_PTR(-ENOMEM);
559 alloc = 1;
560 }
a86c6181 561 path[0].p_hdr = eh;
1973adcb 562 path[0].p_bh = NULL;
a86c6181 563
c29c0ae7 564 i = depth;
a86c6181
AT
565 /* walk through the tree */
566 while (i) {
567 ext_debug("depth %d: num %d, max %d\n",
568 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
c29c0ae7 569
a86c6181 570 ext4_ext_binsearch_idx(inode, path + ppos, block);
f65e6fba 571 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
a86c6181
AT
572 path[ppos].p_depth = i;
573 path[ppos].p_ext = NULL;
574
575 bh = sb_bread(inode->i_sb, path[ppos].p_block);
576 if (!bh)
577 goto err;
578
579 eh = ext_block_hdr(bh);
580 ppos++;
581 BUG_ON(ppos > depth);
582 path[ppos].p_bh = bh;
583 path[ppos].p_hdr = eh;
584 i--;
585
c29c0ae7 586 if (ext4_ext_check_header(inode, eh, i))
a86c6181
AT
587 goto err;
588 }
589
590 path[ppos].p_depth = i;
a86c6181
AT
591 path[ppos].p_ext = NULL;
592 path[ppos].p_idx = NULL;
593
a86c6181
AT
594 /* find extent */
595 ext4_ext_binsearch(inode, path + ppos, block);
1973adcb
SF
596 /* if not an empty leaf */
597 if (path[ppos].p_ext)
598 path[ppos].p_block = ext_pblock(path[ppos].p_ext);
a86c6181
AT
599
600 ext4_ext_show_path(inode, path);
601
602 return path;
603
604err:
605 ext4_ext_drop_refs(path);
606 if (alloc)
607 kfree(path);
608 return ERR_PTR(-EIO);
609}
610
611/*
d0d856e8
RD
612 * ext4_ext_insert_index:
613 * insert new index [@logical;@ptr] into the block at @curp;
614 * check where to insert: before @curp or after @curp
a86c6181
AT
615 */
616static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
617 struct ext4_ext_path *curp,
f65e6fba 618 int logical, ext4_fsblk_t ptr)
a86c6181
AT
619{
620 struct ext4_extent_idx *ix;
621 int len, err;
622
7e028976
AM
623 err = ext4_ext_get_access(handle, inode, curp);
624 if (err)
a86c6181
AT
625 return err;
626
627 BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
628 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
629 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
630 /* insert after */
631 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
632 len = (len - 1) * sizeof(struct ext4_extent_idx);
633 len = len < 0 ? 0 : len;
26d535ed 634 ext_debug("insert new index %d after: %llu. "
a86c6181
AT
635 "move %d from 0x%p to 0x%p\n",
636 logical, ptr, len,
637 (curp->p_idx + 1), (curp->p_idx + 2));
638 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
639 }
640 ix = curp->p_idx + 1;
641 } else {
642 /* insert before */
643 len = len * sizeof(struct ext4_extent_idx);
644 len = len < 0 ? 0 : len;
26d535ed 645 ext_debug("insert new index %d before: %llu. "
a86c6181
AT
646 "move %d from 0x%p to 0x%p\n",
647 logical, ptr, len,
648 curp->p_idx, (curp->p_idx + 1));
649 memmove(curp->p_idx + 1, curp->p_idx, len);
650 ix = curp->p_idx;
651 }
652
653 ix->ei_block = cpu_to_le32(logical);
f65e6fba 654 ext4_idx_store_pblock(ix, ptr);
e8546d06 655 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
a86c6181
AT
656
657 BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
8c55e204 658 > le16_to_cpu(curp->p_hdr->eh_max));
a86c6181
AT
659 BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
660
661 err = ext4_ext_dirty(handle, inode, curp);
662 ext4_std_error(inode->i_sb, err);
663
664 return err;
665}
666
667/*
d0d856e8
RD
668 * ext4_ext_split:
669 * inserts new subtree into the path, using free index entry
670 * at depth @at:
671 * - allocates all needed blocks (new leaf and all intermediate index blocks)
672 * - makes decision where to split
673 * - moves remaining extents and index entries (right to the split point)
674 * into the newly allocated blocks
675 * - initializes subtree
a86c6181
AT
676 */
677static int ext4_ext_split(handle_t *handle, struct inode *inode,
678 struct ext4_ext_path *path,
679 struct ext4_extent *newext, int at)
680{
681 struct buffer_head *bh = NULL;
682 int depth = ext_depth(inode);
683 struct ext4_extent_header *neh;
684 struct ext4_extent_idx *fidx;
685 struct ext4_extent *ex;
686 int i = at, k, m, a;
f65e6fba 687 ext4_fsblk_t newblock, oldblock;
a86c6181 688 __le32 border;
f65e6fba 689 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
a86c6181
AT
690 int err = 0;
691
692 /* make decision: where to split? */
d0d856e8 693 /* FIXME: now decision is simplest: at current extent */
a86c6181 694
d0d856e8 695 /* if current leaf will be split, then we should use
a86c6181
AT
696 * border from split point */
697 BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
698 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
699 border = path[depth].p_ext[1].ee_block;
d0d856e8 700 ext_debug("leaf will be split."
a86c6181 701 " next leaf starts at %d\n",
8c55e204 702 le32_to_cpu(border));
a86c6181
AT
703 } else {
704 border = newext->ee_block;
705 ext_debug("leaf will be added."
706 " next leaf starts at %d\n",
8c55e204 707 le32_to_cpu(border));
a86c6181
AT
708 }
709
710 /*
d0d856e8
RD
711 * If error occurs, then we break processing
712 * and mark filesystem read-only. index won't
a86c6181 713 * be inserted and tree will be in consistent
d0d856e8 714 * state. Next mount will repair buffers too.
a86c6181
AT
715 */
716
717 /*
d0d856e8
RD
718 * Get array to track all allocated blocks.
719 * We need this to handle errors and free blocks
720 * upon them.
a86c6181 721 */
5d4958f9 722 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
a86c6181
AT
723 if (!ablocks)
724 return -ENOMEM;
a86c6181
AT
725
726 /* allocate all needed blocks */
727 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
728 for (a = 0; a < depth - at; a++) {
654b4908
AK
729 newblock = ext4_ext_new_meta_block(handle, inode, path,
730 newext, &err);
a86c6181
AT
731 if (newblock == 0)
732 goto cleanup;
733 ablocks[a] = newblock;
734 }
735
736 /* initialize new leaf */
737 newblock = ablocks[--a];
738 BUG_ON(newblock == 0);
739 bh = sb_getblk(inode->i_sb, newblock);
740 if (!bh) {
741 err = -EIO;
742 goto cleanup;
743 }
744 lock_buffer(bh);
745
7e028976
AM
746 err = ext4_journal_get_create_access(handle, bh);
747 if (err)
a86c6181
AT
748 goto cleanup;
749
750 neh = ext_block_hdr(bh);
751 neh->eh_entries = 0;
752 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
753 neh->eh_magic = EXT4_EXT_MAGIC;
754 neh->eh_depth = 0;
755 ex = EXT_FIRST_EXTENT(neh);
756
d0d856e8 757 /* move remainder of path[depth] to the new leaf */
a86c6181
AT
758 BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
759 /* start copy from next extent */
760 /* TODO: we could do it by single memmove */
761 m = 0;
762 path[depth].p_ext++;
763 while (path[depth].p_ext <=
764 EXT_MAX_EXTENT(path[depth].p_hdr)) {
2ae02107 765 ext_debug("move %d:%llu:%d in new leaf %llu\n",
8c55e204
DK
766 le32_to_cpu(path[depth].p_ext->ee_block),
767 ext_pblock(path[depth].p_ext),
a2df2a63 768 ext4_ext_get_actual_len(path[depth].p_ext),
a86c6181
AT
769 newblock);
770 /*memmove(ex++, path[depth].p_ext++,
771 sizeof(struct ext4_extent));
772 neh->eh_entries++;*/
773 path[depth].p_ext++;
774 m++;
775 }
776 if (m) {
777 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
e8546d06 778 le16_add_cpu(&neh->eh_entries, m);
a86c6181
AT
779 }
780
781 set_buffer_uptodate(bh);
782 unlock_buffer(bh);
783
7e028976
AM
784 err = ext4_journal_dirty_metadata(handle, bh);
785 if (err)
a86c6181
AT
786 goto cleanup;
787 brelse(bh);
788 bh = NULL;
789
790 /* correct old leaf */
791 if (m) {
7e028976
AM
792 err = ext4_ext_get_access(handle, inode, path + depth);
793 if (err)
a86c6181 794 goto cleanup;
e8546d06 795 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
7e028976
AM
796 err = ext4_ext_dirty(handle, inode, path + depth);
797 if (err)
a86c6181
AT
798 goto cleanup;
799
800 }
801
802 /* create intermediate indexes */
803 k = depth - at - 1;
804 BUG_ON(k < 0);
805 if (k)
806 ext_debug("create %d intermediate indices\n", k);
807 /* insert new index into current index block */
808 /* current depth stored in i var */
809 i = depth - 1;
810 while (k--) {
811 oldblock = newblock;
812 newblock = ablocks[--a];
bba90743 813 bh = sb_getblk(inode->i_sb, newblock);
a86c6181
AT
814 if (!bh) {
815 err = -EIO;
816 goto cleanup;
817 }
818 lock_buffer(bh);
819
7e028976
AM
820 err = ext4_journal_get_create_access(handle, bh);
821 if (err)
a86c6181
AT
822 goto cleanup;
823
824 neh = ext_block_hdr(bh);
825 neh->eh_entries = cpu_to_le16(1);
826 neh->eh_magic = EXT4_EXT_MAGIC;
827 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
828 neh->eh_depth = cpu_to_le16(depth - i);
829 fidx = EXT_FIRST_INDEX(neh);
830 fidx->ei_block = border;
f65e6fba 831 ext4_idx_store_pblock(fidx, oldblock);
a86c6181 832
bba90743
ES
833 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
834 i, newblock, le32_to_cpu(border), oldblock);
a86c6181
AT
835 /* copy indexes */
836 m = 0;
837 path[i].p_idx++;
838
839 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
840 EXT_MAX_INDEX(path[i].p_hdr));
841 BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
842 EXT_LAST_INDEX(path[i].p_hdr));
843 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
26d535ed 844 ext_debug("%d: move %d:%llu in new index %llu\n", i,
8c55e204
DK
845 le32_to_cpu(path[i].p_idx->ei_block),
846 idx_pblock(path[i].p_idx),
847 newblock);
a86c6181
AT
848 /*memmove(++fidx, path[i].p_idx++,
849 sizeof(struct ext4_extent_idx));
850 neh->eh_entries++;
851 BUG_ON(neh->eh_entries > neh->eh_max);*/
852 path[i].p_idx++;
853 m++;
854 }
855 if (m) {
856 memmove(++fidx, path[i].p_idx - m,
857 sizeof(struct ext4_extent_idx) * m);
e8546d06 858 le16_add_cpu(&neh->eh_entries, m);
a86c6181
AT
859 }
860 set_buffer_uptodate(bh);
861 unlock_buffer(bh);
862
7e028976
AM
863 err = ext4_journal_dirty_metadata(handle, bh);
864 if (err)
a86c6181
AT
865 goto cleanup;
866 brelse(bh);
867 bh = NULL;
868
869 /* correct old index */
870 if (m) {
871 err = ext4_ext_get_access(handle, inode, path + i);
872 if (err)
873 goto cleanup;
e8546d06 874 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
a86c6181
AT
875 err = ext4_ext_dirty(handle, inode, path + i);
876 if (err)
877 goto cleanup;
878 }
879
880 i--;
881 }
882
883 /* insert new index */
a86c6181
AT
884 err = ext4_ext_insert_index(handle, inode, path + at,
885 le32_to_cpu(border), newblock);
886
887cleanup:
888 if (bh) {
889 if (buffer_locked(bh))
890 unlock_buffer(bh);
891 brelse(bh);
892 }
893
894 if (err) {
895 /* free all allocated blocks in error case */
896 for (i = 0; i < depth; i++) {
897 if (!ablocks[i])
898 continue;
c9de560d 899 ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
a86c6181
AT
900 }
901 }
902 kfree(ablocks);
903
904 return err;
905}
906
907/*
d0d856e8
RD
908 * ext4_ext_grow_indepth:
909 * implements tree growing procedure:
910 * - allocates new block
911 * - moves top-level data (index block or leaf) into the new block
912 * - initializes new top-level, creating index that points to the
913 * just created block
a86c6181
AT
914 */
915static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
916 struct ext4_ext_path *path,
917 struct ext4_extent *newext)
918{
919 struct ext4_ext_path *curp = path;
920 struct ext4_extent_header *neh;
921 struct ext4_extent_idx *fidx;
922 struct buffer_head *bh;
f65e6fba 923 ext4_fsblk_t newblock;
a86c6181
AT
924 int err = 0;
925
654b4908 926 newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
a86c6181
AT
927 if (newblock == 0)
928 return err;
929
930 bh = sb_getblk(inode->i_sb, newblock);
931 if (!bh) {
932 err = -EIO;
933 ext4_std_error(inode->i_sb, err);
934 return err;
935 }
936 lock_buffer(bh);
937
7e028976
AM
938 err = ext4_journal_get_create_access(handle, bh);
939 if (err) {
a86c6181
AT
940 unlock_buffer(bh);
941 goto out;
942 }
943
944 /* move top-level index/leaf into new block */
945 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
946
947 /* set size of new block */
948 neh = ext_block_hdr(bh);
949 /* old root could have indexes or leaves
950 * so calculate e_max right way */
951 if (ext_depth(inode))
952 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
953 else
954 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
955 neh->eh_magic = EXT4_EXT_MAGIC;
956 set_buffer_uptodate(bh);
957 unlock_buffer(bh);
958
7e028976
AM
959 err = ext4_journal_dirty_metadata(handle, bh);
960 if (err)
a86c6181
AT
961 goto out;
962
963 /* create index in new top-level index: num,max,pointer */
7e028976
AM
964 err = ext4_ext_get_access(handle, inode, curp);
965 if (err)
a86c6181
AT
966 goto out;
967
968 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
969 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
970 curp->p_hdr->eh_entries = cpu_to_le16(1);
971 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
e9f410b1
DM
972
973 if (path[0].p_hdr->eh_depth)
974 curp->p_idx->ei_block =
975 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
976 else
977 curp->p_idx->ei_block =
978 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
f65e6fba 979 ext4_idx_store_pblock(curp->p_idx, newblock);
a86c6181
AT
980
981 neh = ext_inode_hdr(inode);
982 fidx = EXT_FIRST_INDEX(neh);
2ae02107 983 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
a86c6181 984 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
f65e6fba 985 le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
a86c6181
AT
986
987 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
988 err = ext4_ext_dirty(handle, inode, curp);
989out:
990 brelse(bh);
991
992 return err;
993}
994
995/*
d0d856e8
RD
996 * ext4_ext_create_new_leaf:
997 * finds empty index and adds new leaf.
998 * if no free index is found, then it requests in-depth growing.
a86c6181
AT
999 */
1000static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
1001 struct ext4_ext_path *path,
1002 struct ext4_extent *newext)
1003{
1004 struct ext4_ext_path *curp;
1005 int depth, i, err = 0;
1006
1007repeat:
1008 i = depth = ext_depth(inode);
1009
1010 /* walk up to the tree and look for free index entry */
1011 curp = path + depth;
1012 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1013 i--;
1014 curp--;
1015 }
1016
d0d856e8
RD
1017 /* we use already allocated block for index block,
1018 * so subsequent data blocks should be contiguous */
a86c6181
AT
1019 if (EXT_HAS_FREE_INDEX(curp)) {
1020 /* if we found index with free entry, then use that
1021 * entry: create all needed subtree and add new leaf */
1022 err = ext4_ext_split(handle, inode, path, newext, i);
787e0981
SF
1023 if (err)
1024 goto out;
a86c6181
AT
1025
1026 /* refill path */
1027 ext4_ext_drop_refs(path);
1028 path = ext4_ext_find_extent(inode,
725d26d3
AK
1029 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1030 path);
a86c6181
AT
1031 if (IS_ERR(path))
1032 err = PTR_ERR(path);
1033 } else {
1034 /* tree is full, time to grow in depth */
1035 err = ext4_ext_grow_indepth(handle, inode, path, newext);
1036 if (err)
1037 goto out;
1038
1039 /* refill path */
1040 ext4_ext_drop_refs(path);
1041 path = ext4_ext_find_extent(inode,
725d26d3
AK
1042 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1043 path);
a86c6181
AT
1044 if (IS_ERR(path)) {
1045 err = PTR_ERR(path);
1046 goto out;
1047 }
1048
1049 /*
d0d856e8
RD
1050 * only first (depth 0 -> 1) produces free space;
1051 * in all other cases we have to split the grown tree
a86c6181
AT
1052 */
1053 depth = ext_depth(inode);
1054 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
d0d856e8 1055 /* now we need to split */
a86c6181
AT
1056 goto repeat;
1057 }
1058 }
1059
1060out:
1061 return err;
1062}
1063
1988b51e
AT
1064/*
1065 * search the closest allocated block to the left for *logical
1066 * and returns it at @logical + it's physical address at @phys
1067 * if *logical is the smallest allocated block, the function
1068 * returns 0 at @phys
1069 * return value contains 0 (success) or error code
1070 */
1071int
1072ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
1073 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1074{
1075 struct ext4_extent_idx *ix;
1076 struct ext4_extent *ex;
b939e376 1077 int depth, ee_len;
1988b51e
AT
1078
1079 BUG_ON(path == NULL);
1080 depth = path->p_depth;
1081 *phys = 0;
1082
1083 if (depth == 0 && path->p_ext == NULL)
1084 return 0;
1085
1086 /* usually extent in the path covers blocks smaller
1087 * then *logical, but it can be that extent is the
1088 * first one in the file */
1089
1090 ex = path[depth].p_ext;
b939e376 1091 ee_len = ext4_ext_get_actual_len(ex);
1988b51e
AT
1092 if (*logical < le32_to_cpu(ex->ee_block)) {
1093 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1094 while (--depth >= 0) {
1095 ix = path[depth].p_idx;
1096 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1097 }
1098 return 0;
1099 }
1100
b939e376 1101 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
1988b51e 1102
b939e376
AK
1103 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1104 *phys = ext_pblock(ex) + ee_len - 1;
1988b51e
AT
1105 return 0;
1106}
1107
1108/*
1109 * search the closest allocated block to the right for *logical
1110 * and returns it at @logical + it's physical address at @phys
1111 * if *logical is the smallest allocated block, the function
1112 * returns 0 at @phys
1113 * return value contains 0 (success) or error code
1114 */
1115int
1116ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
1117 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1118{
1119 struct buffer_head *bh = NULL;
1120 struct ext4_extent_header *eh;
1121 struct ext4_extent_idx *ix;
1122 struct ext4_extent *ex;
1123 ext4_fsblk_t block;
b939e376 1124 int depth, ee_len;
1988b51e
AT
1125
1126 BUG_ON(path == NULL);
1127 depth = path->p_depth;
1128 *phys = 0;
1129
1130 if (depth == 0 && path->p_ext == NULL)
1131 return 0;
1132
1133 /* usually extent in the path covers blocks smaller
1134 * then *logical, but it can be that extent is the
1135 * first one in the file */
1136
1137 ex = path[depth].p_ext;
b939e376 1138 ee_len = ext4_ext_get_actual_len(ex);
1988b51e
AT
1139 if (*logical < le32_to_cpu(ex->ee_block)) {
1140 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1141 while (--depth >= 0) {
1142 ix = path[depth].p_idx;
1143 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1144 }
1145 *logical = le32_to_cpu(ex->ee_block);
1146 *phys = ext_pblock(ex);
1147 return 0;
1148 }
1149
b939e376 1150 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
1988b51e
AT
1151
1152 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1153 /* next allocated block in this leaf */
1154 ex++;
1155 *logical = le32_to_cpu(ex->ee_block);
1156 *phys = ext_pblock(ex);
1157 return 0;
1158 }
1159
1160 /* go up and search for index to the right */
1161 while (--depth >= 0) {
1162 ix = path[depth].p_idx;
1163 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
1164 break;
1165 }
1166
1167 if (depth < 0) {
1168 /* we've gone up to the root and
1169 * found no index to the right */
1170 return 0;
1171 }
1172
1173 /* we've found index to the right, let's
1174 * follow it and find the closest allocated
1175 * block to the right */
1176 ix++;
1177 block = idx_pblock(ix);
1178 while (++depth < path->p_depth) {
1179 bh = sb_bread(inode->i_sb, block);
1180 if (bh == NULL)
1181 return -EIO;
1182 eh = ext_block_hdr(bh);
1183 if (ext4_ext_check_header(inode, eh, depth)) {
1184 put_bh(bh);
1185 return -EIO;
1186 }
1187 ix = EXT_FIRST_INDEX(eh);
1188 block = idx_pblock(ix);
1189 put_bh(bh);
1190 }
1191
1192 bh = sb_bread(inode->i_sb, block);
1193 if (bh == NULL)
1194 return -EIO;
1195 eh = ext_block_hdr(bh);
1196 if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) {
1197 put_bh(bh);
1198 return -EIO;
1199 }
1200 ex = EXT_FIRST_EXTENT(eh);
1201 *logical = le32_to_cpu(ex->ee_block);
1202 *phys = ext_pblock(ex);
1203 put_bh(bh);
1204 return 0;
1205
1206}
1207
a86c6181 1208/*
d0d856e8
RD
1209 * ext4_ext_next_allocated_block:
1210 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1211 * NOTE: it considers block number from index entry as
1212 * allocated block. Thus, index entries have to be consistent
1213 * with leaves.
a86c6181 1214 */
725d26d3 1215static ext4_lblk_t
a86c6181
AT
1216ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1217{
1218 int depth;
1219
1220 BUG_ON(path == NULL);
1221 depth = path->p_depth;
1222
1223 if (depth == 0 && path->p_ext == NULL)
1224 return EXT_MAX_BLOCK;
1225
1226 while (depth >= 0) {
1227 if (depth == path->p_depth) {
1228 /* leaf */
1229 if (path[depth].p_ext !=
1230 EXT_LAST_EXTENT(path[depth].p_hdr))
1231 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1232 } else {
1233 /* index */
1234 if (path[depth].p_idx !=
1235 EXT_LAST_INDEX(path[depth].p_hdr))
1236 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1237 }
1238 depth--;
1239 }
1240
1241 return EXT_MAX_BLOCK;
1242}
1243
1244/*
d0d856e8 1245 * ext4_ext_next_leaf_block:
a86c6181
AT
1246 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1247 */
725d26d3 1248static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
63f57933 1249 struct ext4_ext_path *path)
a86c6181
AT
1250{
1251 int depth;
1252
1253 BUG_ON(path == NULL);
1254 depth = path->p_depth;
1255
1256 /* zero-tree has no leaf blocks at all */
1257 if (depth == 0)
1258 return EXT_MAX_BLOCK;
1259
1260 /* go to index block */
1261 depth--;
1262
1263 while (depth >= 0) {
1264 if (path[depth].p_idx !=
1265 EXT_LAST_INDEX(path[depth].p_hdr))
725d26d3
AK
1266 return (ext4_lblk_t)
1267 le32_to_cpu(path[depth].p_idx[1].ei_block);
a86c6181
AT
1268 depth--;
1269 }
1270
1271 return EXT_MAX_BLOCK;
1272}
1273
1274/*
d0d856e8
RD
1275 * ext4_ext_correct_indexes:
1276 * if leaf gets modified and modified extent is first in the leaf,
1277 * then we have to correct all indexes above.
a86c6181
AT
1278 * TODO: do we need to correct tree in all cases?
1279 */
1d03ec98 1280static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
a86c6181
AT
1281 struct ext4_ext_path *path)
1282{
1283 struct ext4_extent_header *eh;
1284 int depth = ext_depth(inode);
1285 struct ext4_extent *ex;
1286 __le32 border;
1287 int k, err = 0;
1288
1289 eh = path[depth].p_hdr;
1290 ex = path[depth].p_ext;
1291 BUG_ON(ex == NULL);
1292 BUG_ON(eh == NULL);
1293
1294 if (depth == 0) {
1295 /* there is no tree at all */
1296 return 0;
1297 }
1298
1299 if (ex != EXT_FIRST_EXTENT(eh)) {
1300 /* we correct tree if first leaf got modified only */
1301 return 0;
1302 }
1303
1304 /*
d0d856e8 1305 * TODO: we need correction if border is smaller than current one
a86c6181
AT
1306 */
1307 k = depth - 1;
1308 border = path[depth].p_ext->ee_block;
7e028976
AM
1309 err = ext4_ext_get_access(handle, inode, path + k);
1310 if (err)
a86c6181
AT
1311 return err;
1312 path[k].p_idx->ei_block = border;
7e028976
AM
1313 err = ext4_ext_dirty(handle, inode, path + k);
1314 if (err)
a86c6181
AT
1315 return err;
1316
1317 while (k--) {
1318 /* change all left-side indexes */
1319 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1320 break;
7e028976
AM
1321 err = ext4_ext_get_access(handle, inode, path + k);
1322 if (err)
a86c6181
AT
1323 break;
1324 path[k].p_idx->ei_block = border;
7e028976
AM
1325 err = ext4_ext_dirty(handle, inode, path + k);
1326 if (err)
a86c6181
AT
1327 break;
1328 }
1329
1330 return err;
1331}
1332
09b88252 1333static int
a86c6181
AT
1334ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1335 struct ext4_extent *ex2)
1336{
749269fa 1337 unsigned short ext1_ee_len, ext2_ee_len, max_len;
a2df2a63
AA
1338
1339 /*
1340 * Make sure that either both extents are uninitialized, or
1341 * both are _not_.
1342 */
1343 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1344 return 0;
1345
749269fa
AA
1346 if (ext4_ext_is_uninitialized(ex1))
1347 max_len = EXT_UNINIT_MAX_LEN;
1348 else
1349 max_len = EXT_INIT_MAX_LEN;
1350
a2df2a63
AA
1351 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1352 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1353
1354 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
63f57933 1355 le32_to_cpu(ex2->ee_block))
a86c6181
AT
1356 return 0;
1357
471d4011
SB
1358 /*
1359 * To allow future support for preallocated extents to be added
1360 * as an RO_COMPAT feature, refuse to merge to extents if
d0d856e8 1361 * this can result in the top bit of ee_len being set.
471d4011 1362 */
749269fa 1363 if (ext1_ee_len + ext2_ee_len > max_len)
471d4011 1364 return 0;
bbf2f9fb 1365#ifdef AGGRESSIVE_TEST
b939e376 1366 if (ext1_ee_len >= 4)
a86c6181
AT
1367 return 0;
1368#endif
1369
a2df2a63 1370 if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
a86c6181
AT
1371 return 1;
1372 return 0;
1373}
1374
56055d3a
AA
1375/*
1376 * This function tries to merge the "ex" extent to the next extent in the tree.
1377 * It always tries to merge towards right. If you want to merge towards
1378 * left, pass "ex - 1" as argument instead of "ex".
1379 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1380 * 1 if they got merged.
1381 */
1382int ext4_ext_try_to_merge(struct inode *inode,
1383 struct ext4_ext_path *path,
1384 struct ext4_extent *ex)
1385{
1386 struct ext4_extent_header *eh;
1387 unsigned int depth, len;
1388 int merge_done = 0;
1389 int uninitialized = 0;
1390
1391 depth = ext_depth(inode);
1392 BUG_ON(path[depth].p_hdr == NULL);
1393 eh = path[depth].p_hdr;
1394
1395 while (ex < EXT_LAST_EXTENT(eh)) {
1396 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1397 break;
1398 /* merge with next extent! */
1399 if (ext4_ext_is_uninitialized(ex))
1400 uninitialized = 1;
1401 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1402 + ext4_ext_get_actual_len(ex + 1));
1403 if (uninitialized)
1404 ext4_ext_mark_uninitialized(ex);
1405
1406 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1407 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1408 * sizeof(struct ext4_extent);
1409 memmove(ex + 1, ex + 2, len);
1410 }
e8546d06 1411 le16_add_cpu(&eh->eh_entries, -1);
56055d3a
AA
1412 merge_done = 1;
1413 WARN_ON(eh->eh_entries == 0);
1414 if (!eh->eh_entries)
1415 ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
1416 "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
1417 }
1418
1419 return merge_done;
1420}
1421
25d14f98
AA
1422/*
1423 * check if a portion of the "newext" extent overlaps with an
1424 * existing extent.
1425 *
1426 * If there is an overlap discovered, it updates the length of the newext
1427 * such that there will be no overlap, and then returns 1.
1428 * If there is no overlap found, it returns 0.
1429 */
1430unsigned int ext4_ext_check_overlap(struct inode *inode,
1431 struct ext4_extent *newext,
1432 struct ext4_ext_path *path)
1433{
725d26d3 1434 ext4_lblk_t b1, b2;
25d14f98
AA
1435 unsigned int depth, len1;
1436 unsigned int ret = 0;
1437
1438 b1 = le32_to_cpu(newext->ee_block);
a2df2a63 1439 len1 = ext4_ext_get_actual_len(newext);
25d14f98
AA
1440 depth = ext_depth(inode);
1441 if (!path[depth].p_ext)
1442 goto out;
1443 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1444
1445 /*
1446 * get the next allocated block if the extent in the path
2b2d6d01 1447 * is before the requested block(s)
25d14f98
AA
1448 */
1449 if (b2 < b1) {
1450 b2 = ext4_ext_next_allocated_block(path);
1451 if (b2 == EXT_MAX_BLOCK)
1452 goto out;
1453 }
1454
725d26d3 1455 /* check for wrap through zero on extent logical start block*/
25d14f98
AA
1456 if (b1 + len1 < b1) {
1457 len1 = EXT_MAX_BLOCK - b1;
1458 newext->ee_len = cpu_to_le16(len1);
1459 ret = 1;
1460 }
1461
1462 /* check for overlap */
1463 if (b1 + len1 > b2) {
1464 newext->ee_len = cpu_to_le16(b2 - b1);
1465 ret = 1;
1466 }
1467out:
1468 return ret;
1469}
1470
a86c6181 1471/*
d0d856e8
RD
1472 * ext4_ext_insert_extent:
1473 * tries to merge requsted extent into the existing extent or
1474 * inserts requested extent as new one into the tree,
1475 * creating new leaf in the no-space case.
a86c6181
AT
1476 */
1477int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1478 struct ext4_ext_path *path,
1479 struct ext4_extent *newext)
1480{
af5bc92d 1481 struct ext4_extent_header *eh;
a86c6181
AT
1482 struct ext4_extent *ex, *fex;
1483 struct ext4_extent *nearex; /* nearest extent */
1484 struct ext4_ext_path *npath = NULL;
725d26d3
AK
1485 int depth, len, err;
1486 ext4_lblk_t next;
a2df2a63 1487 unsigned uninitialized = 0;
a86c6181 1488
a2df2a63 1489 BUG_ON(ext4_ext_get_actual_len(newext) == 0);
a86c6181
AT
1490 depth = ext_depth(inode);
1491 ex = path[depth].p_ext;
1492 BUG_ON(path[depth].p_hdr == NULL);
1493
1494 /* try to insert block into found extent and return */
1495 if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
2ae02107 1496 ext_debug("append %d block to %d:%d (from %llu)\n",
a2df2a63 1497 ext4_ext_get_actual_len(newext),
a86c6181 1498 le32_to_cpu(ex->ee_block),
a2df2a63 1499 ext4_ext_get_actual_len(ex), ext_pblock(ex));
7e028976
AM
1500 err = ext4_ext_get_access(handle, inode, path + depth);
1501 if (err)
a86c6181 1502 return err;
a2df2a63
AA
1503
1504 /*
1505 * ext4_can_extents_be_merged should have checked that either
1506 * both extents are uninitialized, or both aren't. Thus we
1507 * need to check only one of them here.
1508 */
1509 if (ext4_ext_is_uninitialized(ex))
1510 uninitialized = 1;
1511 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1512 + ext4_ext_get_actual_len(newext));
1513 if (uninitialized)
1514 ext4_ext_mark_uninitialized(ex);
a86c6181
AT
1515 eh = path[depth].p_hdr;
1516 nearex = ex;
1517 goto merge;
1518 }
1519
1520repeat:
1521 depth = ext_depth(inode);
1522 eh = path[depth].p_hdr;
1523 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1524 goto has_space;
1525
1526 /* probably next leaf has space for us? */
1527 fex = EXT_LAST_EXTENT(eh);
1528 next = ext4_ext_next_leaf_block(inode, path);
1529 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1530 && next != EXT_MAX_BLOCK) {
1531 ext_debug("next leaf block - %d\n", next);
1532 BUG_ON(npath != NULL);
1533 npath = ext4_ext_find_extent(inode, next, NULL);
1534 if (IS_ERR(npath))
1535 return PTR_ERR(npath);
1536 BUG_ON(npath->p_depth != path->p_depth);
1537 eh = npath[depth].p_hdr;
1538 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1539 ext_debug("next leaf isnt full(%d)\n",
1540 le16_to_cpu(eh->eh_entries));
1541 path = npath;
1542 goto repeat;
1543 }
1544 ext_debug("next leaf has no free space(%d,%d)\n",
1545 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1546 }
1547
1548 /*
d0d856e8
RD
1549 * There is no free space in the found leaf.
1550 * We're gonna add a new leaf in the tree.
a86c6181
AT
1551 */
1552 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1553 if (err)
1554 goto cleanup;
1555 depth = ext_depth(inode);
1556 eh = path[depth].p_hdr;
1557
1558has_space:
1559 nearex = path[depth].p_ext;
1560
7e028976
AM
1561 err = ext4_ext_get_access(handle, inode, path + depth);
1562 if (err)
a86c6181
AT
1563 goto cleanup;
1564
1565 if (!nearex) {
1566 /* there is no extent in this leaf, create first one */
2ae02107 1567 ext_debug("first extent in the leaf: %d:%llu:%d\n",
8c55e204
DK
1568 le32_to_cpu(newext->ee_block),
1569 ext_pblock(newext),
a2df2a63 1570 ext4_ext_get_actual_len(newext));
a86c6181
AT
1571 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1572 } else if (le32_to_cpu(newext->ee_block)
8c55e204 1573 > le32_to_cpu(nearex->ee_block)) {
a86c6181
AT
1574/* BUG_ON(newext->ee_block == nearex->ee_block); */
1575 if (nearex != EXT_LAST_EXTENT(eh)) {
1576 len = EXT_MAX_EXTENT(eh) - nearex;
1577 len = (len - 1) * sizeof(struct ext4_extent);
1578 len = len < 0 ? 0 : len;
2ae02107 1579 ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
a86c6181 1580 "move %d from 0x%p to 0x%p\n",
8c55e204
DK
1581 le32_to_cpu(newext->ee_block),
1582 ext_pblock(newext),
a2df2a63 1583 ext4_ext_get_actual_len(newext),
a86c6181
AT
1584 nearex, len, nearex + 1, nearex + 2);
1585 memmove(nearex + 2, nearex + 1, len);
1586 }
1587 path[depth].p_ext = nearex + 1;
1588 } else {
1589 BUG_ON(newext->ee_block == nearex->ee_block);
1590 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1591 len = len < 0 ? 0 : len;
2ae02107 1592 ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
a86c6181
AT
1593 "move %d from 0x%p to 0x%p\n",
1594 le32_to_cpu(newext->ee_block),
f65e6fba 1595 ext_pblock(newext),
a2df2a63 1596 ext4_ext_get_actual_len(newext),
a86c6181
AT
1597 nearex, len, nearex + 1, nearex + 2);
1598 memmove(nearex + 1, nearex, len);
1599 path[depth].p_ext = nearex;
1600 }
1601
e8546d06 1602 le16_add_cpu(&eh->eh_entries, 1);
a86c6181
AT
1603 nearex = path[depth].p_ext;
1604 nearex->ee_block = newext->ee_block;
b377611d 1605 ext4_ext_store_pblock(nearex, ext_pblock(newext));
a86c6181 1606 nearex->ee_len = newext->ee_len;
a86c6181
AT
1607
1608merge:
1609 /* try to merge extents to the right */
56055d3a 1610 ext4_ext_try_to_merge(inode, path, nearex);
a86c6181
AT
1611
1612 /* try to merge extents to the left */
1613
1614 /* time to correct all indexes above */
1615 err = ext4_ext_correct_indexes(handle, inode, path);
1616 if (err)
1617 goto cleanup;
1618
1619 err = ext4_ext_dirty(handle, inode, path + depth);
1620
1621cleanup:
1622 if (npath) {
1623 ext4_ext_drop_refs(npath);
1624 kfree(npath);
1625 }
1626 ext4_ext_tree_changed(inode);
1627 ext4_ext_invalidate_cache(inode);
1628 return err;
1629}
1630
6873fa0d
ES
1631int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1632 ext4_lblk_t num, ext_prepare_callback func,
1633 void *cbdata)
1634{
1635 struct ext4_ext_path *path = NULL;
1636 struct ext4_ext_cache cbex;
1637 struct ext4_extent *ex;
1638 ext4_lblk_t next, start = 0, end = 0;
1639 ext4_lblk_t last = block + num;
1640 int depth, exists, err = 0;
1641
1642 BUG_ON(func == NULL);
1643 BUG_ON(inode == NULL);
1644
1645 while (block < last && block != EXT_MAX_BLOCK) {
1646 num = last - block;
1647 /* find extent for this block */
1648 path = ext4_ext_find_extent(inode, block, path);
1649 if (IS_ERR(path)) {
1650 err = PTR_ERR(path);
1651 path = NULL;
1652 break;
1653 }
1654
1655 depth = ext_depth(inode);
1656 BUG_ON(path[depth].p_hdr == NULL);
1657 ex = path[depth].p_ext;
1658 next = ext4_ext_next_allocated_block(path);
1659
1660 exists = 0;
1661 if (!ex) {
1662 /* there is no extent yet, so try to allocate
1663 * all requested space */
1664 start = block;
1665 end = block + num;
1666 } else if (le32_to_cpu(ex->ee_block) > block) {
1667 /* need to allocate space before found extent */
1668 start = block;
1669 end = le32_to_cpu(ex->ee_block);
1670 if (block + num < end)
1671 end = block + num;
1672 } else if (block >= le32_to_cpu(ex->ee_block)
1673 + ext4_ext_get_actual_len(ex)) {
1674 /* need to allocate space after found extent */
1675 start = block;
1676 end = block + num;
1677 if (end >= next)
1678 end = next;
1679 } else if (block >= le32_to_cpu(ex->ee_block)) {
1680 /*
1681 * some part of requested space is covered
1682 * by found extent
1683 */
1684 start = block;
1685 end = le32_to_cpu(ex->ee_block)
1686 + ext4_ext_get_actual_len(ex);
1687 if (block + num < end)
1688 end = block + num;
1689 exists = 1;
1690 } else {
1691 BUG();
1692 }
1693 BUG_ON(end <= start);
1694
1695 if (!exists) {
1696 cbex.ec_block = start;
1697 cbex.ec_len = end - start;
1698 cbex.ec_start = 0;
1699 cbex.ec_type = EXT4_EXT_CACHE_GAP;
1700 } else {
1701 cbex.ec_block = le32_to_cpu(ex->ee_block);
1702 cbex.ec_len = ext4_ext_get_actual_len(ex);
1703 cbex.ec_start = ext_pblock(ex);
1704 cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
1705 }
1706
1707 BUG_ON(cbex.ec_len == 0);
1708 err = func(inode, path, &cbex, ex, cbdata);
1709 ext4_ext_drop_refs(path);
1710
1711 if (err < 0)
1712 break;
1713
1714 if (err == EXT_REPEAT)
1715 continue;
1716 else if (err == EXT_BREAK) {
1717 err = 0;
1718 break;
1719 }
1720
1721 if (ext_depth(inode) != depth) {
1722 /* depth was changed. we have to realloc path */
1723 kfree(path);
1724 path = NULL;
1725 }
1726
1727 block = cbex.ec_block + cbex.ec_len;
1728 }
1729
1730 if (path) {
1731 ext4_ext_drop_refs(path);
1732 kfree(path);
1733 }
1734
1735 return err;
1736}
1737
09b88252 1738static void
725d26d3 1739ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
dd54567a 1740 __u32 len, ext4_fsblk_t start, int type)
a86c6181
AT
1741{
1742 struct ext4_ext_cache *cex;
1743 BUG_ON(len == 0);
1744 cex = &EXT4_I(inode)->i_cached_extent;
1745 cex->ec_type = type;
1746 cex->ec_block = block;
1747 cex->ec_len = len;
1748 cex->ec_start = start;
1749}
1750
1751/*
d0d856e8
RD
1752 * ext4_ext_put_gap_in_cache:
1753 * calculate boundaries of the gap that the requested block fits into
a86c6181
AT
1754 * and cache this gap
1755 */
09b88252 1756static void
a86c6181 1757ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
725d26d3 1758 ext4_lblk_t block)
a86c6181
AT
1759{
1760 int depth = ext_depth(inode);
725d26d3
AK
1761 unsigned long len;
1762 ext4_lblk_t lblock;
a86c6181
AT
1763 struct ext4_extent *ex;
1764
1765 ex = path[depth].p_ext;
1766 if (ex == NULL) {
1767 /* there is no extent yet, so gap is [0;-] */
1768 lblock = 0;
1769 len = EXT_MAX_BLOCK;
1770 ext_debug("cache gap(whole file):");
1771 } else if (block < le32_to_cpu(ex->ee_block)) {
1772 lblock = block;
1773 len = le32_to_cpu(ex->ee_block) - block;
bba90743
ES
1774 ext_debug("cache gap(before): %u [%u:%u]",
1775 block,
1776 le32_to_cpu(ex->ee_block),
1777 ext4_ext_get_actual_len(ex));
a86c6181 1778 } else if (block >= le32_to_cpu(ex->ee_block)
a2df2a63 1779 + ext4_ext_get_actual_len(ex)) {
725d26d3 1780 ext4_lblk_t next;
8c55e204 1781 lblock = le32_to_cpu(ex->ee_block)
a2df2a63 1782 + ext4_ext_get_actual_len(ex);
725d26d3
AK
1783
1784 next = ext4_ext_next_allocated_block(path);
bba90743
ES
1785 ext_debug("cache gap(after): [%u:%u] %u",
1786 le32_to_cpu(ex->ee_block),
1787 ext4_ext_get_actual_len(ex),
1788 block);
725d26d3
AK
1789 BUG_ON(next == lblock);
1790 len = next - lblock;
a86c6181
AT
1791 } else {
1792 lblock = len = 0;
1793 BUG();
1794 }
1795
bba90743 1796 ext_debug(" -> %u:%lu\n", lblock, len);
a86c6181
AT
1797 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1798}
1799
09b88252 1800static int
725d26d3 1801ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
a86c6181
AT
1802 struct ext4_extent *ex)
1803{
1804 struct ext4_ext_cache *cex;
1805
1806 cex = &EXT4_I(inode)->i_cached_extent;
1807
1808 /* has cache valid data? */
1809 if (cex->ec_type == EXT4_EXT_CACHE_NO)
1810 return EXT4_EXT_CACHE_NO;
1811
1812 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1813 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1814 if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
8c55e204 1815 ex->ee_block = cpu_to_le32(cex->ec_block);
f65e6fba 1816 ext4_ext_store_pblock(ex, cex->ec_start);
8c55e204 1817 ex->ee_len = cpu_to_le16(cex->ec_len);
bba90743
ES
1818 ext_debug("%u cached by %u:%u:%llu\n",
1819 block,
1820 cex->ec_block, cex->ec_len, cex->ec_start);
a86c6181
AT
1821 return cex->ec_type;
1822 }
1823
1824 /* not in cache */
1825 return EXT4_EXT_CACHE_NO;
1826}
1827
1828/*
d0d856e8
RD
1829 * ext4_ext_rm_idx:
1830 * removes index from the index block.
1831 * It's used in truncate case only, thus all requests are for
1832 * last index in the block only.
a86c6181 1833 */
1d03ec98 1834static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
a86c6181
AT
1835 struct ext4_ext_path *path)
1836{
1837 struct buffer_head *bh;
1838 int err;
f65e6fba 1839 ext4_fsblk_t leaf;
a86c6181
AT
1840
1841 /* free index block */
1842 path--;
f65e6fba 1843 leaf = idx_pblock(path->p_idx);
a86c6181 1844 BUG_ON(path->p_hdr->eh_entries == 0);
7e028976
AM
1845 err = ext4_ext_get_access(handle, inode, path);
1846 if (err)
a86c6181 1847 return err;
e8546d06 1848 le16_add_cpu(&path->p_hdr->eh_entries, -1);
7e028976
AM
1849 err = ext4_ext_dirty(handle, inode, path);
1850 if (err)
a86c6181 1851 return err;
2ae02107 1852 ext_debug("index is empty, remove it, free block %llu\n", leaf);
a86c6181
AT
1853 bh = sb_find_get_block(inode->i_sb, leaf);
1854 ext4_forget(handle, 1, inode, bh, leaf);
c9de560d 1855 ext4_free_blocks(handle, inode, leaf, 1, 1);
a86c6181
AT
1856 return err;
1857}
1858
1859/*
ee12b630
MC
1860 * ext4_ext_calc_credits_for_single_extent:
1861 * This routine returns max. credits that needed to insert an extent
1862 * to the extent tree.
1863 * When pass the actual path, the caller should calculate credits
1864 * under i_data_sem.
a86c6181 1865 */
525f4ed8 1866int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
a86c6181
AT
1867 struct ext4_ext_path *path)
1868{
a86c6181 1869 if (path) {
ee12b630 1870 int depth = ext_depth(inode);
f3bd1f3f 1871 int ret = 0;
ee12b630 1872
a86c6181 1873 /* probably there is space in leaf? */
a86c6181 1874 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
ee12b630 1875 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
a86c6181 1876
ee12b630
MC
1877 /*
1878 * There are some space in the leaf tree, no
1879 * need to account for leaf block credit
1880 *
1881 * bitmaps and block group descriptor blocks
1882 * and other metadat blocks still need to be
1883 * accounted.
1884 */
525f4ed8 1885 /* 1 bitmap, 1 block group descriptor */
ee12b630
MC
1886 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
1887 }
1888 }
a86c6181 1889
525f4ed8 1890 return ext4_chunk_trans_blocks(inode, nrblocks);
ee12b630 1891}
a86c6181 1892
ee12b630
MC
1893/*
1894 * How many index/leaf blocks need to change/allocate to modify nrblocks?
1895 *
1896 * if nrblocks are fit in a single extent (chunk flag is 1), then
1897 * in the worse case, each tree level index/leaf need to be changed
1898 * if the tree split due to insert a new extent, then the old tree
1899 * index/leaf need to be updated too
1900 *
1901 * If the nrblocks are discontiguous, they could cause
1902 * the whole tree split more than once, but this is really rare.
1903 */
525f4ed8 1904int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
ee12b630
MC
1905{
1906 int index;
1907 int depth = ext_depth(inode);
a86c6181 1908
ee12b630
MC
1909 if (chunk)
1910 index = depth * 2;
1911 else
1912 index = depth * 3;
a86c6181 1913
ee12b630 1914 return index;
a86c6181
AT
1915}
1916
1917static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
1918 struct ext4_extent *ex,
725d26d3 1919 ext4_lblk_t from, ext4_lblk_t to)
a86c6181
AT
1920{
1921 struct buffer_head *bh;
a2df2a63 1922 unsigned short ee_len = ext4_ext_get_actual_len(ex);
c9de560d 1923 int i, metadata = 0;
a86c6181 1924
c9de560d
AT
1925 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1926 metadata = 1;
a86c6181
AT
1927#ifdef EXTENTS_STATS
1928 {
1929 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
a86c6181
AT
1930 spin_lock(&sbi->s_ext_stats_lock);
1931 sbi->s_ext_blocks += ee_len;
1932 sbi->s_ext_extents++;
1933 if (ee_len < sbi->s_ext_min)
1934 sbi->s_ext_min = ee_len;
1935 if (ee_len > sbi->s_ext_max)
1936 sbi->s_ext_max = ee_len;
1937 if (ext_depth(inode) > sbi->s_depth_max)
1938 sbi->s_depth_max = ext_depth(inode);
1939 spin_unlock(&sbi->s_ext_stats_lock);
1940 }
1941#endif
1942 if (from >= le32_to_cpu(ex->ee_block)
a2df2a63 1943 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
a86c6181 1944 /* tail removal */
725d26d3 1945 ext4_lblk_t num;
f65e6fba 1946 ext4_fsblk_t start;
725d26d3 1947
a2df2a63
AA
1948 num = le32_to_cpu(ex->ee_block) + ee_len - from;
1949 start = ext_pblock(ex) + ee_len - num;
725d26d3 1950 ext_debug("free last %u blocks starting %llu\n", num, start);
a86c6181
AT
1951 for (i = 0; i < num; i++) {
1952 bh = sb_find_get_block(inode->i_sb, start + i);
1953 ext4_forget(handle, 0, inode, bh, start + i);
1954 }
c9de560d 1955 ext4_free_blocks(handle, inode, start, num, metadata);
a86c6181 1956 } else if (from == le32_to_cpu(ex->ee_block)
a2df2a63 1957 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
725d26d3 1958 printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
a2df2a63 1959 from, to, le32_to_cpu(ex->ee_block), ee_len);
a86c6181 1960 } else {
725d26d3
AK
1961 printk(KERN_INFO "strange request: removal(2) "
1962 "%u-%u from %u:%u\n",
1963 from, to, le32_to_cpu(ex->ee_block), ee_len);
a86c6181
AT
1964 }
1965 return 0;
1966}
1967
1968static int
1969ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
725d26d3 1970 struct ext4_ext_path *path, ext4_lblk_t start)
a86c6181
AT
1971{
1972 int err = 0, correct_index = 0;
1973 int depth = ext_depth(inode), credits;
1974 struct ext4_extent_header *eh;
725d26d3
AK
1975 ext4_lblk_t a, b, block;
1976 unsigned num;
1977 ext4_lblk_t ex_ee_block;
a86c6181 1978 unsigned short ex_ee_len;
a2df2a63 1979 unsigned uninitialized = 0;
a86c6181
AT
1980 struct ext4_extent *ex;
1981
c29c0ae7 1982 /* the header must be checked already in ext4_ext_remove_space() */
725d26d3 1983 ext_debug("truncate since %u in leaf\n", start);
a86c6181
AT
1984 if (!path[depth].p_hdr)
1985 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
1986 eh = path[depth].p_hdr;
1987 BUG_ON(eh == NULL);
a86c6181
AT
1988
1989 /* find where to start removing */
1990 ex = EXT_LAST_EXTENT(eh);
1991
1992 ex_ee_block = le32_to_cpu(ex->ee_block);
a2df2a63
AA
1993 if (ext4_ext_is_uninitialized(ex))
1994 uninitialized = 1;
1995 ex_ee_len = ext4_ext_get_actual_len(ex);
a86c6181
AT
1996
1997 while (ex >= EXT_FIRST_EXTENT(eh) &&
1998 ex_ee_block + ex_ee_len > start) {
1999 ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
2000 path[depth].p_ext = ex;
2001
2002 a = ex_ee_block > start ? ex_ee_block : start;
2003 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
2004 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
2005
2006 ext_debug(" border %u:%u\n", a, b);
2007
2008 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
2009 block = 0;
2010 num = 0;
2011 BUG();
2012 } else if (a != ex_ee_block) {
2013 /* remove tail of the extent */
2014 block = ex_ee_block;
2015 num = a - block;
2016 } else if (b != ex_ee_block + ex_ee_len - 1) {
2017 /* remove head of the extent */
2018 block = a;
2019 num = b - a;
2020 /* there is no "make a hole" API yet */
2021 BUG();
2022 } else {
2023 /* remove whole extent: excellent! */
2024 block = ex_ee_block;
2025 num = 0;
2026 BUG_ON(a != ex_ee_block);
2027 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
2028 }
2029
34071da7
TT
2030 /*
2031 * 3 for leaf, sb, and inode plus 2 (bmap and group
2032 * descriptor) for each block group; assume two block
2033 * groups plus ex_ee_len/blocks_per_block_group for
2034 * the worst case
2035 */
2036 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
a86c6181
AT
2037 if (ex == EXT_FIRST_EXTENT(eh)) {
2038 correct_index = 1;
2039 credits += (ext_depth(inode)) + 1;
2040 }
a86c6181 2041 credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
a86c6181 2042
9102e4fa
SF
2043 err = ext4_ext_journal_restart(handle, credits);
2044 if (err)
a86c6181 2045 goto out;
a86c6181
AT
2046
2047 err = ext4_ext_get_access(handle, inode, path + depth);
2048 if (err)
2049 goto out;
2050
2051 err = ext4_remove_blocks(handle, inode, ex, a, b);
2052 if (err)
2053 goto out;
2054
2055 if (num == 0) {
d0d856e8 2056 /* this extent is removed; mark slot entirely unused */
f65e6fba 2057 ext4_ext_store_pblock(ex, 0);
e8546d06 2058 le16_add_cpu(&eh->eh_entries, -1);
a86c6181
AT
2059 }
2060
2061 ex->ee_block = cpu_to_le32(block);
2062 ex->ee_len = cpu_to_le16(num);
749269fa
AA
2063 /*
2064 * Do not mark uninitialized if all the blocks in the
2065 * extent have been removed.
2066 */
2067 if (uninitialized && num)
a2df2a63 2068 ext4_ext_mark_uninitialized(ex);
a86c6181
AT
2069
2070 err = ext4_ext_dirty(handle, inode, path + depth);
2071 if (err)
2072 goto out;
2073
2ae02107 2074 ext_debug("new extent: %u:%u:%llu\n", block, num,
f65e6fba 2075 ext_pblock(ex));
a86c6181
AT
2076 ex--;
2077 ex_ee_block = le32_to_cpu(ex->ee_block);
a2df2a63 2078 ex_ee_len = ext4_ext_get_actual_len(ex);
a86c6181
AT
2079 }
2080
2081 if (correct_index && eh->eh_entries)
2082 err = ext4_ext_correct_indexes(handle, inode, path);
2083
2084 /* if this leaf is free, then we should
2085 * remove it from index block above */
2086 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2087 err = ext4_ext_rm_idx(handle, inode, path + depth);
2088
2089out:
2090 return err;
2091}
2092
2093/*
d0d856e8
RD
2094 * ext4_ext_more_to_rm:
2095 * returns 1 if current index has to be freed (even partial)
a86c6181 2096 */
09b88252 2097static int
a86c6181
AT
2098ext4_ext_more_to_rm(struct ext4_ext_path *path)
2099{
2100 BUG_ON(path->p_idx == NULL);
2101
2102 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2103 return 0;
2104
2105 /*
d0d856e8 2106 * if truncate on deeper level happened, it wasn't partial,
a86c6181
AT
2107 * so we have to consider current index for truncation
2108 */
2109 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2110 return 0;
2111 return 1;
2112}
2113
1d03ec98 2114static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
a86c6181
AT
2115{
2116 struct super_block *sb = inode->i_sb;
2117 int depth = ext_depth(inode);
2118 struct ext4_ext_path *path;
2119 handle_t *handle;
2120 int i = 0, err = 0;
2121
725d26d3 2122 ext_debug("truncate since %u\n", start);
a86c6181
AT
2123
2124 /* probably first extent we're gonna free will be last in block */
2125 handle = ext4_journal_start(inode, depth + 1);
2126 if (IS_ERR(handle))
2127 return PTR_ERR(handle);
2128
2129 ext4_ext_invalidate_cache(inode);
2130
2131 /*
d0d856e8
RD
2132 * We start scanning from right side, freeing all the blocks
2133 * after i_size and walking into the tree depth-wise.
a86c6181 2134 */
216553c4 2135 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
a86c6181
AT
2136 if (path == NULL) {
2137 ext4_journal_stop(handle);
2138 return -ENOMEM;
2139 }
a86c6181 2140 path[0].p_hdr = ext_inode_hdr(inode);
c29c0ae7 2141 if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) {
a86c6181
AT
2142 err = -EIO;
2143 goto out;
2144 }
2145 path[0].p_depth = depth;
2146
2147 while (i >= 0 && err == 0) {
2148 if (i == depth) {
2149 /* this is leaf block */
2150 err = ext4_ext_rm_leaf(handle, inode, path, start);
d0d856e8 2151 /* root level has p_bh == NULL, brelse() eats this */
a86c6181
AT
2152 brelse(path[i].p_bh);
2153 path[i].p_bh = NULL;
2154 i--;
2155 continue;
2156 }
2157
2158 /* this is index block */
2159 if (!path[i].p_hdr) {
2160 ext_debug("initialize header\n");
2161 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
a86c6181
AT
2162 }
2163
a86c6181 2164 if (!path[i].p_idx) {
d0d856e8 2165 /* this level hasn't been touched yet */
a86c6181
AT
2166 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2167 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2168 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2169 path[i].p_hdr,
2170 le16_to_cpu(path[i].p_hdr->eh_entries));
2171 } else {
d0d856e8 2172 /* we were already here, see at next index */
a86c6181
AT
2173 path[i].p_idx--;
2174 }
2175
2176 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2177 i, EXT_FIRST_INDEX(path[i].p_hdr),
2178 path[i].p_idx);
2179 if (ext4_ext_more_to_rm(path + i)) {
c29c0ae7 2180 struct buffer_head *bh;
a86c6181 2181 /* go to the next level */
2ae02107 2182 ext_debug("move to level %d (block %llu)\n",
f65e6fba 2183 i + 1, idx_pblock(path[i].p_idx));
a86c6181 2184 memset(path + i + 1, 0, sizeof(*path));
c29c0ae7
AT
2185 bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2186 if (!bh) {
a86c6181
AT
2187 /* should we reset i_size? */
2188 err = -EIO;
2189 break;
2190 }
c29c0ae7
AT
2191 if (WARN_ON(i + 1 > depth)) {
2192 err = -EIO;
2193 break;
2194 }
2195 if (ext4_ext_check_header(inode, ext_block_hdr(bh),
2196 depth - i - 1)) {
2197 err = -EIO;
2198 break;
2199 }
2200 path[i + 1].p_bh = bh;
a86c6181 2201
d0d856e8
RD
2202 /* save actual number of indexes since this
2203 * number is changed at the next iteration */
a86c6181
AT
2204 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2205 i++;
2206 } else {
d0d856e8 2207 /* we finished processing this index, go up */
a86c6181 2208 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
d0d856e8 2209 /* index is empty, remove it;
a86c6181
AT
2210 * handle must be already prepared by the
2211 * truncatei_leaf() */
2212 err = ext4_ext_rm_idx(handle, inode, path + i);
2213 }
d0d856e8 2214 /* root level has p_bh == NULL, brelse() eats this */
a86c6181
AT
2215 brelse(path[i].p_bh);
2216 path[i].p_bh = NULL;
2217 i--;
2218 ext_debug("return to level %d\n", i);
2219 }
2220 }
2221
2222 /* TODO: flexible tree reduction should be here */
2223 if (path->p_hdr->eh_entries == 0) {
2224 /*
d0d856e8
RD
2225 * truncate to zero freed all the tree,
2226 * so we need to correct eh_depth
a86c6181
AT
2227 */
2228 err = ext4_ext_get_access(handle, inode, path);
2229 if (err == 0) {
2230 ext_inode_hdr(inode)->eh_depth = 0;
2231 ext_inode_hdr(inode)->eh_max =
2232 cpu_to_le16(ext4_ext_space_root(inode));
2233 err = ext4_ext_dirty(handle, inode, path);
2234 }
2235 }
2236out:
2237 ext4_ext_tree_changed(inode);
2238 ext4_ext_drop_refs(path);
2239 kfree(path);
2240 ext4_journal_stop(handle);
2241
2242 return err;
2243}
2244
2245/*
2246 * called at mount time
2247 */
2248void ext4_ext_init(struct super_block *sb)
2249{
2250 /*
2251 * possible initialization would be here
2252 */
2253
2254 if (test_opt(sb, EXTENTS)) {
4776004f 2255 printk(KERN_INFO "EXT4-fs: file extents enabled");
bbf2f9fb
RD
2256#ifdef AGGRESSIVE_TEST
2257 printk(", aggressive tests");
a86c6181
AT
2258#endif
2259#ifdef CHECK_BINSEARCH
2260 printk(", check binsearch");
2261#endif
2262#ifdef EXTENTS_STATS
2263 printk(", stats");
2264#endif
2265 printk("\n");
2266#ifdef EXTENTS_STATS
2267 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2268 EXT4_SB(sb)->s_ext_min = 1 << 30;
2269 EXT4_SB(sb)->s_ext_max = 0;
2270#endif
2271 }
2272}
2273
2274/*
2275 * called at umount time
2276 */
2277void ext4_ext_release(struct super_block *sb)
2278{
2279 if (!test_opt(sb, EXTENTS))
2280 return;
2281
2282#ifdef EXTENTS_STATS
2283 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2284 struct ext4_sb_info *sbi = EXT4_SB(sb);
2285 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2286 sbi->s_ext_blocks, sbi->s_ext_extents,
2287 sbi->s_ext_blocks / sbi->s_ext_extents);
2288 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2289 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2290 }
2291#endif
2292}
2293
093a088b
AK
2294static void bi_complete(struct bio *bio, int error)
2295{
2296 complete((struct completion *)bio->bi_private);
2297}
2298
2299/* FIXME!! we need to try to merge to left or right after zero-out */
2300static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2301{
2302 int ret = -EIO;
2303 struct bio *bio;
2304 int blkbits, blocksize;
2305 sector_t ee_pblock;
2306 struct completion event;
2307 unsigned int ee_len, len, done, offset;
2308
2309
2310 blkbits = inode->i_blkbits;
2311 blocksize = inode->i_sb->s_blocksize;
2312 ee_len = ext4_ext_get_actual_len(ex);
2313 ee_pblock = ext_pblock(ex);
2314
2315 /* convert ee_pblock to 512 byte sectors */
2316 ee_pblock = ee_pblock << (blkbits - 9);
2317
2318 while (ee_len > 0) {
2319
2320 if (ee_len > BIO_MAX_PAGES)
2321 len = BIO_MAX_PAGES;
2322 else
2323 len = ee_len;
2324
2325 bio = bio_alloc(GFP_NOIO, len);
2326 if (!bio)
2327 return -ENOMEM;
2328 bio->bi_sector = ee_pblock;
2329 bio->bi_bdev = inode->i_sb->s_bdev;
2330
2331 done = 0;
2332 offset = 0;
2333 while (done < len) {
2334 ret = bio_add_page(bio, ZERO_PAGE(0),
2335 blocksize, offset);
2336 if (ret != blocksize) {
2337 /*
2338 * We can't add any more pages because of
2339 * hardware limitations. Start a new bio.
2340 */
2341 break;
2342 }
2343 done++;
2344 offset += blocksize;
2345 if (offset >= PAGE_CACHE_SIZE)
2346 offset = 0;
2347 }
2348
2349 init_completion(&event);
2350 bio->bi_private = &event;
2351 bio->bi_end_io = bi_complete;
2352 submit_bio(WRITE, bio);
2353 wait_for_completion(&event);
2354
2355 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
2356 ret = 0;
2357 else {
2358 ret = -EIO;
2359 break;
2360 }
2361 bio_put(bio);
2362 ee_len -= done;
2363 ee_pblock += done << (blkbits - 9);
2364 }
2365 return ret;
2366}
2367
3977c965
AK
2368#define EXT4_EXT_ZERO_LEN 7
2369
56055d3a
AA
2370/*
2371 * This function is called by ext4_ext_get_blocks() if someone tries to write
2372 * to an uninitialized extent. It may result in splitting the uninitialized
2373 * extent into multiple extents (upto three - one initialized and two
2374 * uninitialized).
2375 * There are three possibilities:
2376 * a> There is no split required: Entire extent should be initialized
2377 * b> Splits in two extents: Write is happening at either end of the extent
2378 * c> Splits in three extents: Somone is writing in middle of the extent
2379 */
725d26d3
AK
2380static int ext4_ext_convert_to_initialized(handle_t *handle,
2381 struct inode *inode,
2382 struct ext4_ext_path *path,
2383 ext4_lblk_t iblock,
2384 unsigned long max_blocks)
56055d3a 2385{
95c3889c 2386 struct ext4_extent *ex, newex, orig_ex;
56055d3a
AA
2387 struct ext4_extent *ex1 = NULL;
2388 struct ext4_extent *ex2 = NULL;
2389 struct ext4_extent *ex3 = NULL;
2390 struct ext4_extent_header *eh;
725d26d3
AK
2391 ext4_lblk_t ee_block;
2392 unsigned int allocated, ee_len, depth;
56055d3a
AA
2393 ext4_fsblk_t newblock;
2394 int err = 0;
2395 int ret = 0;
2396
2397 depth = ext_depth(inode);
2398 eh = path[depth].p_hdr;
2399 ex = path[depth].p_ext;
2400 ee_block = le32_to_cpu(ex->ee_block);
2401 ee_len = ext4_ext_get_actual_len(ex);
2402 allocated = ee_len - (iblock - ee_block);
2403 newblock = iblock - ee_block + ext_pblock(ex);
2404 ex2 = ex;
95c3889c
AK
2405 orig_ex.ee_block = ex->ee_block;
2406 orig_ex.ee_len = cpu_to_le16(ee_len);
2407 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
56055d3a 2408
9df5643a
AK
2409 err = ext4_ext_get_access(handle, inode, path + depth);
2410 if (err)
2411 goto out;
3977c965
AK
2412 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2413 if (ee_len <= 2*EXT4_EXT_ZERO_LEN) {
2414 err = ext4_ext_zeroout(inode, &orig_ex);
2415 if (err)
2416 goto fix_extent_len;
2417 /* update the extent length and mark as initialized */
2418 ex->ee_block = orig_ex.ee_block;
2419 ex->ee_len = orig_ex.ee_len;
2420 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2421 ext4_ext_dirty(handle, inode, path + depth);
161e7b7c
AK
2422 /* zeroed the full extent */
2423 return allocated;
3977c965 2424 }
9df5643a 2425
56055d3a
AA
2426 /* ex1: ee_block to iblock - 1 : uninitialized */
2427 if (iblock > ee_block) {
2428 ex1 = ex;
2429 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2430 ext4_ext_mark_uninitialized(ex1);
2431 ex2 = &newex;
2432 }
2433 /*
2434 * for sanity, update the length of the ex2 extent before
2435 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2436 * overlap of blocks.
2437 */
2438 if (!ex1 && allocated > max_blocks)
2439 ex2->ee_len = cpu_to_le16(max_blocks);
2440 /* ex3: to ee_block + ee_len : uninitialised */
2441 if (allocated > max_blocks) {
2442 unsigned int newdepth;
3977c965
AK
2443 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
2444 if (allocated <= EXT4_EXT_ZERO_LEN) {
d03856bd
AK
2445 /*
2446 * iblock == ee_block is handled by the zerouout
2447 * at the beginning.
2448 * Mark first half uninitialized.
3977c965
AK
2449 * Mark second half initialized and zero out the
2450 * initialized extent
2451 */
2452 ex->ee_block = orig_ex.ee_block;
2453 ex->ee_len = cpu_to_le16(ee_len - allocated);
2454 ext4_ext_mark_uninitialized(ex);
2455 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2456 ext4_ext_dirty(handle, inode, path + depth);
2457
2458 ex3 = &newex;
2459 ex3->ee_block = cpu_to_le32(iblock);
2460 ext4_ext_store_pblock(ex3, newblock);
2461 ex3->ee_len = cpu_to_le16(allocated);
2462 err = ext4_ext_insert_extent(handle, inode, path, ex3);
2463 if (err == -ENOSPC) {
2464 err = ext4_ext_zeroout(inode, &orig_ex);
2465 if (err)
2466 goto fix_extent_len;
2467 ex->ee_block = orig_ex.ee_block;
2468 ex->ee_len = orig_ex.ee_len;
2469 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2470 ext4_ext_dirty(handle, inode, path + depth);
d03856bd 2471 /* blocks available from iblock */
161e7b7c 2472 return allocated;
3977c965
AK
2473
2474 } else if (err)
2475 goto fix_extent_len;
2476
161e7b7c
AK
2477 /*
2478 * We need to zero out the second half because
2479 * an fallocate request can update file size and
2480 * converting the second half to initialized extent
2481 * implies that we can leak some junk data to user
2482 * space.
2483 */
2484 err = ext4_ext_zeroout(inode, ex3);
2485 if (err) {
2486 /*
2487 * We should actually mark the
2488 * second half as uninit and return error
2489 * Insert would have changed the extent
2490 */
2491 depth = ext_depth(inode);
2492 ext4_ext_drop_refs(path);
2493 path = ext4_ext_find_extent(inode,
2494 iblock, path);
2495 if (IS_ERR(path)) {
2496 err = PTR_ERR(path);
2497 return err;
2498 }
d03856bd 2499 /* get the second half extent details */
161e7b7c
AK
2500 ex = path[depth].p_ext;
2501 err = ext4_ext_get_access(handle, inode,
2502 path + depth);
2503 if (err)
2504 return err;
2505 ext4_ext_mark_uninitialized(ex);
2506 ext4_ext_dirty(handle, inode, path + depth);
2507 return err;
2508 }
2509
2510 /* zeroed the second half */
3977c965
AK
2511 return allocated;
2512 }
56055d3a
AA
2513 ex3 = &newex;
2514 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2515 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2516 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2517 ext4_ext_mark_uninitialized(ex3);
2518 err = ext4_ext_insert_extent(handle, inode, path, ex3);
093a088b
AK
2519 if (err == -ENOSPC) {
2520 err = ext4_ext_zeroout(inode, &orig_ex);
2521 if (err)
2522 goto fix_extent_len;
2523 /* update the extent length and mark as initialized */
95c3889c
AK
2524 ex->ee_block = orig_ex.ee_block;
2525 ex->ee_len = orig_ex.ee_len;
2526 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
95c3889c 2527 ext4_ext_dirty(handle, inode, path + depth);
161e7b7c 2528 /* zeroed the full extent */
d03856bd 2529 /* blocks available from iblock */
161e7b7c 2530 return allocated;
093a088b
AK
2531
2532 } else if (err)
2533 goto fix_extent_len;
56055d3a
AA
2534 /*
2535 * The depth, and hence eh & ex might change
2536 * as part of the insert above.
2537 */
2538 newdepth = ext_depth(inode);
95c3889c
AK
2539 /*
2540 * update the extent length after successfull insert of the
2541 * split extent
2542 */
2543 orig_ex.ee_len = cpu_to_le16(ee_len -
2544 ext4_ext_get_actual_len(ex3));
d03856bd
AK
2545 depth = newdepth;
2546 ext4_ext_drop_refs(path);
2547 path = ext4_ext_find_extent(inode, iblock, path);
2548 if (IS_ERR(path)) {
2549 err = PTR_ERR(path);
2550 goto out;
56055d3a 2551 }
d03856bd
AK
2552 eh = path[depth].p_hdr;
2553 ex = path[depth].p_ext;
2554 if (ex2 != &newex)
2555 ex2 = ex;
2556
2557 err = ext4_ext_get_access(handle, inode, path + depth);
2558 if (err)
2559 goto out;
2560
56055d3a 2561 allocated = max_blocks;
3977c965
AK
2562
2563 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2564 * to insert a extent in the middle zerout directly
2565 * otherwise give the extent a chance to merge to left
2566 */
2567 if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
2568 iblock != ee_block) {
2569 err = ext4_ext_zeroout(inode, &orig_ex);
2570 if (err)
2571 goto fix_extent_len;
2572 /* update the extent length and mark as initialized */
2573 ex->ee_block = orig_ex.ee_block;
2574 ex->ee_len = orig_ex.ee_len;
2575 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2576 ext4_ext_dirty(handle, inode, path + depth);
161e7b7c 2577 /* zero out the first half */
d03856bd 2578 /* blocks available from iblock */
161e7b7c 2579 return allocated;
3977c965 2580 }
56055d3a
AA
2581 }
2582 /*
2583 * If there was a change of depth as part of the
2584 * insertion of ex3 above, we need to update the length
2585 * of the ex1 extent again here
2586 */
2587 if (ex1 && ex1 != ex) {
2588 ex1 = ex;
2589 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2590 ext4_ext_mark_uninitialized(ex1);
2591 ex2 = &newex;
2592 }
2593 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2594 ex2->ee_block = cpu_to_le32(iblock);
56055d3a
AA
2595 ext4_ext_store_pblock(ex2, newblock);
2596 ex2->ee_len = cpu_to_le16(allocated);
2597 if (ex2 != ex)
2598 goto insert;
56055d3a
AA
2599 /*
2600 * New (initialized) extent starts from the first block
2601 * in the current extent. i.e., ex2 == ex
2602 * We have to see if it can be merged with the extent
2603 * on the left.
2604 */
2605 if (ex2 > EXT_FIRST_EXTENT(eh)) {
2606 /*
2607 * To merge left, pass "ex2 - 1" to try_to_merge(),
2608 * since it merges towards right _only_.
2609 */
2610 ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
2611 if (ret) {
2612 err = ext4_ext_correct_indexes(handle, inode, path);
2613 if (err)
2614 goto out;
2615 depth = ext_depth(inode);
2616 ex2--;
2617 }
2618 }
2619 /*
2620 * Try to Merge towards right. This might be required
2621 * only when the whole extent is being written to.
2622 * i.e. ex2 == ex and ex3 == NULL.
2623 */
2624 if (!ex3) {
2625 ret = ext4_ext_try_to_merge(inode, path, ex2);
2626 if (ret) {
2627 err = ext4_ext_correct_indexes(handle, inode, path);
2628 if (err)
2629 goto out;
2630 }
2631 }
2632 /* Mark modified extent as dirty */
2633 err = ext4_ext_dirty(handle, inode, path + depth);
2634 goto out;
2635insert:
2636 err = ext4_ext_insert_extent(handle, inode, path, &newex);
093a088b
AK
2637 if (err == -ENOSPC) {
2638 err = ext4_ext_zeroout(inode, &orig_ex);
2639 if (err)
2640 goto fix_extent_len;
2641 /* update the extent length and mark as initialized */
95c3889c
AK
2642 ex->ee_block = orig_ex.ee_block;
2643 ex->ee_len = orig_ex.ee_len;
2644 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
95c3889c 2645 ext4_ext_dirty(handle, inode, path + depth);
161e7b7c
AK
2646 /* zero out the first half */
2647 return allocated;
093a088b
AK
2648 } else if (err)
2649 goto fix_extent_len;
56055d3a
AA
2650out:
2651 return err ? err : allocated;
093a088b
AK
2652
2653fix_extent_len:
2654 ex->ee_block = orig_ex.ee_block;
2655 ex->ee_len = orig_ex.ee_len;
2656 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2657 ext4_ext_mark_uninitialized(ex);
2658 ext4_ext_dirty(handle, inode, path + depth);
2659 return err;
56055d3a
AA
2660}
2661
c278bfec 2662/*
f5ab0d1f
MC
2663 * Block allocation/map/preallocation routine for extents based files
2664 *
2665 *
c278bfec 2666 * Need to be called with
0e855ac8
AK
2667 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
2668 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
f5ab0d1f
MC
2669 *
2670 * return > 0, number of of blocks already mapped/allocated
2671 * if create == 0 and these are pre-allocated blocks
2672 * buffer head is unmapped
2673 * otherwise blocks are mapped
2674 *
2675 * return = 0, if plain look up failed (blocks have not been allocated)
2676 * buffer head is unmapped
2677 *
2678 * return < 0, error case.
c278bfec 2679 */
f65e6fba 2680int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
725d26d3 2681 ext4_lblk_t iblock,
a86c6181
AT
2682 unsigned long max_blocks, struct buffer_head *bh_result,
2683 int create, int extend_disksize)
2684{
2685 struct ext4_ext_path *path = NULL;
56055d3a 2686 struct ext4_extent_header *eh;
a86c6181 2687 struct ext4_extent newex, *ex;
f65e6fba 2688 ext4_fsblk_t goal, newblock;
56055d3a 2689 int err = 0, depth, ret;
a86c6181 2690 unsigned long allocated = 0;
c9de560d 2691 struct ext4_allocation_request ar;
61628a3f 2692 loff_t disksize;
a86c6181
AT
2693
2694 __clear_bit(BH_New, &bh_result->b_state);
bba90743
ES
2695 ext_debug("blocks %u/%lu requested for inode %u\n",
2696 iblock, max_blocks, inode->i_ino);
a86c6181
AT
2697
2698 /* check in cache */
7e028976
AM
2699 goal = ext4_ext_in_cache(inode, iblock, &newex);
2700 if (goal) {
a86c6181
AT
2701 if (goal == EXT4_EXT_CACHE_GAP) {
2702 if (!create) {
56055d3a
AA
2703 /*
2704 * block isn't allocated yet and
2705 * user doesn't want to allocate it
2706 */
a86c6181
AT
2707 goto out2;
2708 }
2709 /* we should allocate requested block */
2710 } else if (goal == EXT4_EXT_CACHE_EXTENT) {
2711 /* block is already allocated */
8c55e204
DK
2712 newblock = iblock
2713 - le32_to_cpu(newex.ee_block)
2714 + ext_pblock(&newex);
d0d856e8 2715 /* number of remaining blocks in the extent */
b939e376 2716 allocated = ext4_ext_get_actual_len(&newex) -
a86c6181
AT
2717 (iblock - le32_to_cpu(newex.ee_block));
2718 goto out;
2719 } else {
2720 BUG();
2721 }
2722 }
2723
2724 /* find extent for this block */
2725 path = ext4_ext_find_extent(inode, iblock, NULL);
2726 if (IS_ERR(path)) {
2727 err = PTR_ERR(path);
2728 path = NULL;
2729 goto out2;
2730 }
2731
2732 depth = ext_depth(inode);
2733
2734 /*
d0d856e8
RD
2735 * consistent leaf must not be empty;
2736 * this situation is possible, though, _during_ tree modification;
a86c6181
AT
2737 * this is why assert can't be put in ext4_ext_find_extent()
2738 */
2739 BUG_ON(path[depth].p_ext == NULL && depth != 0);
56055d3a 2740 eh = path[depth].p_hdr;
a86c6181 2741
7e028976
AM
2742 ex = path[depth].p_ext;
2743 if (ex) {
725d26d3 2744 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
f65e6fba 2745 ext4_fsblk_t ee_start = ext_pblock(ex);
a2df2a63 2746 unsigned short ee_len;
471d4011
SB
2747
2748 /*
471d4011 2749 * Uninitialized extents are treated as holes, except that
56055d3a 2750 * we split out initialized portions during a write.
471d4011 2751 */
a2df2a63 2752 ee_len = ext4_ext_get_actual_len(ex);
d0d856e8 2753 /* if found extent covers block, simply return it */
8c55e204 2754 if (iblock >= ee_block && iblock < ee_block + ee_len) {
a86c6181 2755 newblock = iblock - ee_block + ee_start;
d0d856e8 2756 /* number of remaining blocks in the extent */
a86c6181 2757 allocated = ee_len - (iblock - ee_block);
bba90743 2758 ext_debug("%u fit into %lu:%d -> %llu\n", iblock,
a86c6181 2759 ee_block, ee_len, newblock);
56055d3a 2760
a2df2a63 2761 /* Do not put uninitialized extent in the cache */
56055d3a 2762 if (!ext4_ext_is_uninitialized(ex)) {
a2df2a63
AA
2763 ext4_ext_put_in_cache(inode, ee_block,
2764 ee_len, ee_start,
2765 EXT4_EXT_CACHE_EXTENT);
56055d3a
AA
2766 goto out;
2767 }
2768 if (create == EXT4_CREATE_UNINITIALIZED_EXT)
2769 goto out;
e067ba00
AK
2770 if (!create) {
2771 /*
2772 * We have blocks reserved already. We
2773 * return allocated blocks so that delalloc
2774 * won't do block reservation for us. But
2775 * the buffer head will be unmapped so that
2776 * a read from the block returns 0s.
2777 */
2778 if (allocated > max_blocks)
2779 allocated = max_blocks;
953e622b 2780 set_buffer_unwritten(bh_result);
56055d3a 2781 goto out2;
e067ba00 2782 }
56055d3a
AA
2783
2784 ret = ext4_ext_convert_to_initialized(handle, inode,
2785 path, iblock,
2786 max_blocks);
dbf9d7da
DM
2787 if (ret <= 0) {
2788 err = ret;
56055d3a 2789 goto out2;
dbf9d7da 2790 } else
56055d3a
AA
2791 allocated = ret;
2792 goto outnew;
a86c6181
AT
2793 }
2794 }
2795
2796 /*
d0d856e8 2797 * requested block isn't allocated yet;
a86c6181
AT
2798 * we couldn't try to create block if create flag is zero
2799 */
2800 if (!create) {
56055d3a
AA
2801 /*
2802 * put just found gap into cache to speed up
2803 * subsequent requests
2804 */
a86c6181
AT
2805 ext4_ext_put_gap_in_cache(inode, path, iblock);
2806 goto out2;
2807 }
2808 /*
c2ea3fde 2809 * Okay, we need to do block allocation.
63f57933 2810 */
a86c6181 2811
c9de560d
AT
2812 /* find neighbour allocated blocks */
2813 ar.lleft = iblock;
2814 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
2815 if (err)
2816 goto out2;
2817 ar.lright = iblock;
2818 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
2819 if (err)
2820 goto out2;
25d14f98 2821
749269fa
AA
2822 /*
2823 * See if request is beyond maximum number of blocks we can have in
2824 * a single extent. For an initialized extent this limit is
2825 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
2826 * EXT_UNINIT_MAX_LEN.
2827 */
2828 if (max_blocks > EXT_INIT_MAX_LEN &&
2829 create != EXT4_CREATE_UNINITIALIZED_EXT)
2830 max_blocks = EXT_INIT_MAX_LEN;
2831 else if (max_blocks > EXT_UNINIT_MAX_LEN &&
2832 create == EXT4_CREATE_UNINITIALIZED_EXT)
2833 max_blocks = EXT_UNINIT_MAX_LEN;
2834
25d14f98
AA
2835 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
2836 newex.ee_block = cpu_to_le32(iblock);
2837 newex.ee_len = cpu_to_le16(max_blocks);
2838 err = ext4_ext_check_overlap(inode, &newex, path);
2839 if (err)
b939e376 2840 allocated = ext4_ext_get_actual_len(&newex);
25d14f98
AA
2841 else
2842 allocated = max_blocks;
c9de560d
AT
2843
2844 /* allocate new block */
2845 ar.inode = inode;
2846 ar.goal = ext4_ext_find_goal(inode, path, iblock);
2847 ar.logical = iblock;
2848 ar.len = allocated;
2849 if (S_ISREG(inode->i_mode))
2850 ar.flags = EXT4_MB_HINT_DATA;
2851 else
2852 /* disable in-core preallocation for non-regular files */
2853 ar.flags = 0;
2854 newblock = ext4_mb_new_blocks(handle, &ar, &err);
a86c6181
AT
2855 if (!newblock)
2856 goto out2;
2ae02107 2857 ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
a86c6181
AT
2858 goal, newblock, allocated);
2859
2860 /* try to insert new extent into found leaf and return */
f65e6fba 2861 ext4_ext_store_pblock(&newex, newblock);
c9de560d 2862 newex.ee_len = cpu_to_le16(ar.len);
a2df2a63
AA
2863 if (create == EXT4_CREATE_UNINITIALIZED_EXT) /* Mark uninitialized */
2864 ext4_ext_mark_uninitialized(&newex);
a86c6181 2865 err = ext4_ext_insert_extent(handle, inode, path, &newex);
315054f0
AT
2866 if (err) {
2867 /* free data blocks we just allocated */
c9de560d
AT
2868 /* not a good idea to call discard here directly,
2869 * but otherwise we'd need to call it every free() */
c2ea3fde 2870 ext4_discard_preallocations(inode);
315054f0 2871 ext4_free_blocks(handle, inode, ext_pblock(&newex),
b939e376 2872 ext4_ext_get_actual_len(&newex), 0);
a86c6181 2873 goto out2;
315054f0 2874 }
a86c6181 2875
a86c6181 2876 /* previous routine could use block we allocated */
f65e6fba 2877 newblock = ext_pblock(&newex);
b939e376 2878 allocated = ext4_ext_get_actual_len(&newex);
56055d3a 2879outnew:
61628a3f
MC
2880 if (extend_disksize) {
2881 disksize = ((loff_t) iblock + ar.len) << inode->i_blkbits;
2882 if (disksize > i_size_read(inode))
2883 disksize = i_size_read(inode);
2884 if (disksize > EXT4_I(inode)->i_disksize)
2885 EXT4_I(inode)->i_disksize = disksize;
2886 }
a379cd1d 2887
953e622b 2888 set_buffer_new(bh_result);
a86c6181 2889
a2df2a63
AA
2890 /* Cache only when it is _not_ an uninitialized extent */
2891 if (create != EXT4_CREATE_UNINITIALIZED_EXT)
2892 ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
2893 EXT4_EXT_CACHE_EXTENT);
a86c6181
AT
2894out:
2895 if (allocated > max_blocks)
2896 allocated = max_blocks;
2897 ext4_ext_show_leaf(inode, path);
953e622b 2898 set_buffer_mapped(bh_result);
a86c6181
AT
2899 bh_result->b_bdev = inode->i_sb->s_bdev;
2900 bh_result->b_blocknr = newblock;
2901out2:
2902 if (path) {
2903 ext4_ext_drop_refs(path);
2904 kfree(path);
2905 }
a86c6181
AT
2906 return err ? err : allocated;
2907}
2908
cf108bca 2909void ext4_ext_truncate(struct inode *inode)
a86c6181
AT
2910{
2911 struct address_space *mapping = inode->i_mapping;
2912 struct super_block *sb = inode->i_sb;
725d26d3 2913 ext4_lblk_t last_block;
a86c6181
AT
2914 handle_t *handle;
2915 int err = 0;
2916
2917 /*
2918 * probably first extent we're gonna free will be last in block
2919 */
f3bd1f3f 2920 err = ext4_writepage_trans_blocks(inode);
a86c6181 2921 handle = ext4_journal_start(inode, err);
cf108bca 2922 if (IS_ERR(handle))
a86c6181 2923 return;
a86c6181 2924
cf108bca
JK
2925 if (inode->i_size & (sb->s_blocksize - 1))
2926 ext4_block_truncate_page(handle, mapping, inode->i_size);
a86c6181 2927
9ddfc3dc
JK
2928 if (ext4_orphan_add(handle, inode))
2929 goto out_stop;
2930
0e855ac8 2931 down_write(&EXT4_I(inode)->i_data_sem);
a86c6181
AT
2932 ext4_ext_invalidate_cache(inode);
2933
c2ea3fde 2934 ext4_discard_preallocations(inode);
c9de560d 2935
a86c6181 2936 /*
d0d856e8
RD
2937 * TODO: optimization is possible here.
2938 * Probably we need not scan at all,
2939 * because page truncation is enough.
a86c6181 2940 */
a86c6181
AT
2941
2942 /* we have to know where to truncate from in crash case */
2943 EXT4_I(inode)->i_disksize = inode->i_size;
2944 ext4_mark_inode_dirty(handle, inode);
2945
2946 last_block = (inode->i_size + sb->s_blocksize - 1)
2947 >> EXT4_BLOCK_SIZE_BITS(sb);
2948 err = ext4_ext_remove_space(inode, last_block);
2949
2950 /* In a multi-transaction truncate, we only make the final
56055d3a
AA
2951 * transaction synchronous.
2952 */
a86c6181
AT
2953 if (IS_SYNC(inode))
2954 handle->h_sync = 1;
2955
2956out_stop:
9ddfc3dc 2957 up_write(&EXT4_I(inode)->i_data_sem);
a86c6181 2958 /*
d0d856e8 2959 * If this was a simple ftruncate() and the file will remain alive,
a86c6181
AT
2960 * then we need to clear up the orphan record which we created above.
2961 * However, if this was a real unlink then we were called by
2962 * ext4_delete_inode(), and we allow that function to clean up the
2963 * orphan info for us.
2964 */
2965 if (inode->i_nlink)
2966 ext4_orphan_del(handle, inode);
2967
ef737728
SR
2968 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
2969 ext4_mark_inode_dirty(handle, inode);
a86c6181
AT
2970 ext4_journal_stop(handle);
2971}
2972
fd28784a
AK
2973static void ext4_falloc_update_inode(struct inode *inode,
2974 int mode, loff_t new_size, int update_ctime)
2975{
2976 struct timespec now;
2977
2978 if (update_ctime) {
2979 now = current_fs_time(inode->i_sb);
2980 if (!timespec_equal(&inode->i_ctime, &now))
2981 inode->i_ctime = now;
2982 }
2983 /*
2984 * Update only when preallocation was requested beyond
2985 * the file size.
2986 */
cf17fea6
AK
2987 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
2988 if (new_size > i_size_read(inode))
2989 i_size_write(inode, new_size);
2990 if (new_size > EXT4_I(inode)->i_disksize)
2991 ext4_update_i_disksize(inode, new_size);
fd28784a
AK
2992 }
2993
2994}
2995
a2df2a63
AA
2996/*
2997 * preallocate space for a file. This implements ext4's fallocate inode
2998 * operation, which gets called from sys_fallocate system call.
2999 * For block-mapped files, posix_fallocate should fall back to the method
3000 * of writing zeroes to the required new blocks (the same behavior which is
3001 * expected for file systems which do not support fallocate() system call).
3002 */
3003long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
3004{
3005 handle_t *handle;
725d26d3 3006 ext4_lblk_t block;
fd28784a 3007 loff_t new_size;
725d26d3 3008 unsigned long max_blocks;
a2df2a63
AA
3009 int ret = 0;
3010 int ret2 = 0;
3011 int retries = 0;
3012 struct buffer_head map_bh;
3013 unsigned int credits, blkbits = inode->i_blkbits;
3014
3015 /*
3016 * currently supporting (pre)allocate mode for extent-based
3017 * files _only_
3018 */
3019 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
3020 return -EOPNOTSUPP;
3021
3022 /* preallocation to directories is currently not supported */
3023 if (S_ISDIR(inode->i_mode))
3024 return -ENODEV;
3025
3026 block = offset >> blkbits;
fd28784a
AK
3027 /*
3028 * We can't just convert len to max_blocks because
3029 * If blocksize = 4096 offset = 3072 and len = 2048
3030 */
a2df2a63 3031 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
fd28784a 3032 - block;
a2df2a63 3033 /*
f3bd1f3f 3034 * credits to insert 1 extent into extent tree
a2df2a63 3035 */
f3bd1f3f 3036 credits = ext4_chunk_trans_blocks(inode, max_blocks);
55bd725a 3037 mutex_lock(&inode->i_mutex);
a2df2a63
AA
3038retry:
3039 while (ret >= 0 && ret < max_blocks) {
3040 block = block + ret;
3041 max_blocks = max_blocks - ret;
3042 handle = ext4_journal_start(inode, credits);
3043 if (IS_ERR(handle)) {
3044 ret = PTR_ERR(handle);
3045 break;
3046 }
55bd725a 3047 ret = ext4_get_blocks_wrap(handle, inode, block,
a2df2a63 3048 max_blocks, &map_bh,
d2a17637 3049 EXT4_CREATE_UNINITIALIZED_EXT, 0, 0);
221879c9 3050 if (ret <= 0) {
2c98615d
AK
3051#ifdef EXT4FS_DEBUG
3052 WARN_ON(ret <= 0);
3053 printk(KERN_ERR "%s: ext4_ext_get_blocks "
3054 "returned error inode#%lu, block=%u, "
3055 "max_blocks=%lu", __func__,
221879c9 3056 inode->i_ino, block, max_blocks);
2c98615d 3057#endif
a2df2a63
AA
3058 ext4_mark_inode_dirty(handle, inode);
3059 ret2 = ext4_journal_stop(handle);
3060 break;
3061 }
fd28784a
AK
3062 if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
3063 blkbits) >> blkbits))
3064 new_size = offset + len;
3065 else
3066 new_size = (block + ret) << blkbits;
a2df2a63 3067
fd28784a
AK
3068 ext4_falloc_update_inode(inode, mode, new_size,
3069 buffer_new(&map_bh));
a2df2a63
AA
3070 ext4_mark_inode_dirty(handle, inode);
3071 ret2 = ext4_journal_stop(handle);
3072 if (ret2)
3073 break;
3074 }
fd28784a
AK
3075 if (ret == -ENOSPC &&
3076 ext4_should_retry_alloc(inode->i_sb, &retries)) {
3077 ret = 0;
a2df2a63 3078 goto retry;
a2df2a63 3079 }
55bd725a 3080 mutex_unlock(&inode->i_mutex);
a2df2a63
AA
3081 return ret > 0 ? ret2 : ret;
3082}
6873fa0d
ES
3083
3084/*
3085 * Callback function called for each extent to gather FIEMAP information.
3086 */
3087int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path,
3088 struct ext4_ext_cache *newex, struct ext4_extent *ex,
3089 void *data)
3090{
3091 struct fiemap_extent_info *fieinfo = data;
3092 unsigned long blksize_bits = inode->i_sb->s_blocksize_bits;
3093 __u64 logical;
3094 __u64 physical;
3095 __u64 length;
3096 __u32 flags = 0;
3097 int error;
3098
3099 logical = (__u64)newex->ec_block << blksize_bits;
3100
3101 if (newex->ec_type == EXT4_EXT_CACHE_GAP) {
3102 pgoff_t offset;
3103 struct page *page;
3104 struct buffer_head *bh = NULL;
3105
3106 offset = logical >> PAGE_SHIFT;
3107 page = find_get_page(inode->i_mapping, offset);
3108 if (!page || !page_has_buffers(page))
3109 return EXT_CONTINUE;
3110
3111 bh = page_buffers(page);
3112
3113 if (!bh)
3114 return EXT_CONTINUE;
3115
3116 if (buffer_delay(bh)) {
3117 flags |= FIEMAP_EXTENT_DELALLOC;
3118 page_cache_release(page);
3119 } else {
3120 page_cache_release(page);
3121 return EXT_CONTINUE;
3122 }
3123 }
3124
3125 physical = (__u64)newex->ec_start << blksize_bits;
3126 length = (__u64)newex->ec_len << blksize_bits;
3127
3128 if (ex && ext4_ext_is_uninitialized(ex))
3129 flags |= FIEMAP_EXTENT_UNWRITTEN;
3130
3131 /*
3132 * If this extent reaches EXT_MAX_BLOCK, it must be last.
3133 *
3134 * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK,
3135 * this also indicates no more allocated blocks.
3136 *
3137 * XXX this might miss a single-block extent at EXT_MAX_BLOCK
3138 */
3139 if (logical + length - 1 == EXT_MAX_BLOCK ||
3140 ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK)
3141 flags |= FIEMAP_EXTENT_LAST;
3142
3143 error = fiemap_fill_next_extent(fieinfo, logical, physical,
3144 length, flags);
3145 if (error < 0)
3146 return error;
3147 if (error == 1)
3148 return EXT_BREAK;
3149
3150 return EXT_CONTINUE;
3151}
3152
3153/* fiemap flags we can handle specified here */
3154#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
3155
3156int ext4_xattr_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo)
3157{
3158 __u64 physical = 0;
3159 __u64 length;
3160 __u32 flags = FIEMAP_EXTENT_LAST;
3161 int blockbits = inode->i_sb->s_blocksize_bits;
3162 int error = 0;
3163
3164 /* in-inode? */
3165 if (EXT4_I(inode)->i_state & EXT4_STATE_XATTR) {
3166 struct ext4_iloc iloc;
3167 int offset; /* offset of xattr in inode */
3168
3169 error = ext4_get_inode_loc(inode, &iloc);
3170 if (error)
3171 return error;
3172 physical = iloc.bh->b_blocknr << blockbits;
3173 offset = EXT4_GOOD_OLD_INODE_SIZE +
3174 EXT4_I(inode)->i_extra_isize;
3175 physical += offset;
3176 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
3177 flags |= FIEMAP_EXTENT_DATA_INLINE;
3178 } else { /* external block */
3179 physical = EXT4_I(inode)->i_file_acl << blockbits;
3180 length = inode->i_sb->s_blocksize;
3181 }
3182
3183 if (physical)
3184 error = fiemap_fill_next_extent(fieinfo, 0, physical,
3185 length, flags);
3186 return (error < 0 ? error : 0);
3187}
3188
3189int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
3190 __u64 start, __u64 len)
3191{
3192 ext4_lblk_t start_blk;
3193 ext4_lblk_t len_blks;
3194 int error = 0;
3195
3196 /* fallback to generic here if not in extents fmt */
3197 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
3198 return generic_block_fiemap(inode, fieinfo, start, len,
3199 ext4_get_block);
3200
3201 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
3202 return -EBADR;
3203
3204 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
3205 error = ext4_xattr_fiemap(inode, fieinfo);
3206 } else {
3207 start_blk = start >> inode->i_sb->s_blocksize_bits;
3208 len_blks = len >> inode->i_sb->s_blocksize_bits;
3209
3210 /*
3211 * Walk the extent tree gathering extent information.
3212 * ext4_ext_fiemap_cb will push extents back to user.
3213 */
3214 down_write(&EXT4_I(inode)->i_data_sem);
3215 error = ext4_ext_walk_space(inode, start_blk, len_blks,
3216 ext4_ext_fiemap_cb, fieinfo);
3217 up_write(&EXT4_I(inode)->i_data_sem);
3218 }
3219
3220 return error;
3221}
3222