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