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