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