ext4: fix fdatasync(2) after extent manipulation operations
[sfrench/cifs-2.6.git] / fs / ext4 / extents.c
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 License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-
21  */
22
23 /*
24  * Extents support for EXT4
25  *
26  * TODO:
27  *   - ext4*_error() should be used in some situations
28  *   - analyze all BUG()/BUG_ON(), use -EIO where appropriate
29  *   - smart tree reduction
30  */
31
32 #include <linux/fs.h>
33 #include <linux/time.h>
34 #include <linux/jbd2.h>
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>
40 #include <linux/uaccess.h>
41 #include <linux/fiemap.h>
42 #include <linux/backing-dev.h>
43 #include "ext4_jbd2.h"
44 #include "ext4_extents.h"
45 #include "xattr.h"
46
47 #include <trace/events/ext4.h>
48
49 /*
50  * used by extent splitting.
51  */
52 #define EXT4_EXT_MAY_ZEROOUT    0x1  /* safe to zeroout if split fails \
53                                         due to ENOSPC */
54 #define EXT4_EXT_MARK_UNWRIT1   0x2  /* mark first half unwritten */
55 #define EXT4_EXT_MARK_UNWRIT2   0x4  /* mark second half unwritten */
56
57 #define EXT4_EXT_DATA_VALID1    0x8  /* first half contains valid data */
58 #define EXT4_EXT_DATA_VALID2    0x10 /* second half contains valid data */
59
60 static __le32 ext4_extent_block_csum(struct inode *inode,
61                                      struct ext4_extent_header *eh)
62 {
63         struct ext4_inode_info *ei = EXT4_I(inode);
64         struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
65         __u32 csum;
66
67         csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)eh,
68                            EXT4_EXTENT_TAIL_OFFSET(eh));
69         return cpu_to_le32(csum);
70 }
71
72 static int ext4_extent_block_csum_verify(struct inode *inode,
73                                          struct ext4_extent_header *eh)
74 {
75         struct ext4_extent_tail *et;
76
77         if (!ext4_has_metadata_csum(inode->i_sb))
78                 return 1;
79
80         et = find_ext4_extent_tail(eh);
81         if (et->et_checksum != ext4_extent_block_csum(inode, eh))
82                 return 0;
83         return 1;
84 }
85
86 static void ext4_extent_block_csum_set(struct inode *inode,
87                                        struct ext4_extent_header *eh)
88 {
89         struct ext4_extent_tail *et;
90
91         if (!ext4_has_metadata_csum(inode->i_sb))
92                 return;
93
94         et = find_ext4_extent_tail(eh);
95         et->et_checksum = ext4_extent_block_csum(inode, eh);
96 }
97
98 static int ext4_split_extent(handle_t *handle,
99                                 struct inode *inode,
100                                 struct ext4_ext_path **ppath,
101                                 struct ext4_map_blocks *map,
102                                 int split_flag,
103                                 int flags);
104
105 static int ext4_split_extent_at(handle_t *handle,
106                              struct inode *inode,
107                              struct ext4_ext_path **ppath,
108                              ext4_lblk_t split,
109                              int split_flag,
110                              int flags);
111
112 static int ext4_find_delayed_extent(struct inode *inode,
113                                     struct extent_status *newes);
114
115 static int ext4_ext_truncate_extend_restart(handle_t *handle,
116                                             struct inode *inode,
117                                             int needed)
118 {
119         int err;
120
121         if (!ext4_handle_valid(handle))
122                 return 0;
123         if (handle->h_buffer_credits >= needed)
124                 return 0;
125         /*
126          * If we need to extend the journal get a few extra blocks
127          * while we're at it for efficiency's sake.
128          */
129         needed += 3;
130         err = ext4_journal_extend(handle, needed - handle->h_buffer_credits);
131         if (err <= 0)
132                 return err;
133         err = ext4_truncate_restart_trans(handle, inode, needed);
134         if (err == 0)
135                 err = -EAGAIN;
136
137         return err;
138 }
139
140 /*
141  * could return:
142  *  - EROFS
143  *  - ENOMEM
144  */
145 static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
146                                 struct ext4_ext_path *path)
147 {
148         if (path->p_bh) {
149                 /* path points to block */
150                 BUFFER_TRACE(path->p_bh, "get_write_access");
151                 return ext4_journal_get_write_access(handle, path->p_bh);
152         }
153         /* path points to leaf/index in inode body */
154         /* we use in-core data, no need to protect them */
155         return 0;
156 }
157
158 /*
159  * could return:
160  *  - EROFS
161  *  - ENOMEM
162  *  - EIO
163  */
164 int __ext4_ext_dirty(const char *where, unsigned int line, handle_t *handle,
165                      struct inode *inode, struct ext4_ext_path *path)
166 {
167         int err;
168
169         WARN_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
170         if (path->p_bh) {
171                 ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh));
172                 /* path points to block */
173                 err = __ext4_handle_dirty_metadata(where, line, handle,
174                                                    inode, path->p_bh);
175         } else {
176                 /* path points to leaf/index in inode body */
177                 err = ext4_mark_inode_dirty(handle, inode);
178         }
179         return err;
180 }
181
182 static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
183                               struct ext4_ext_path *path,
184                               ext4_lblk_t block)
185 {
186         if (path) {
187                 int depth = path->p_depth;
188                 struct ext4_extent *ex;
189
190                 /*
191                  * Try to predict block placement assuming that we are
192                  * filling in a file which will eventually be
193                  * non-sparse --- i.e., in the case of libbfd writing
194                  * an ELF object sections out-of-order but in a way
195                  * the eventually results in a contiguous object or
196                  * executable file, or some database extending a table
197                  * space file.  However, this is actually somewhat
198                  * non-ideal if we are writing a sparse file such as
199                  * qemu or KVM writing a raw image file that is going
200                  * to stay fairly sparse, since it will end up
201                  * fragmenting the file system's free space.  Maybe we
202                  * should have some hueristics or some way to allow
203                  * userspace to pass a hint to file system,
204                  * especially if the latter case turns out to be
205                  * common.
206                  */
207                 ex = path[depth].p_ext;
208                 if (ex) {
209                         ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
210                         ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
211
212                         if (block > ext_block)
213                                 return ext_pblk + (block - ext_block);
214                         else
215                                 return ext_pblk - (ext_block - block);
216                 }
217
218                 /* it looks like index is empty;
219                  * try to find starting block from index itself */
220                 if (path[depth].p_bh)
221                         return path[depth].p_bh->b_blocknr;
222         }
223
224         /* OK. use inode's group */
225         return ext4_inode_to_goal_block(inode);
226 }
227
228 /*
229  * Allocation for a meta data block
230  */
231 static ext4_fsblk_t
232 ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
233                         struct ext4_ext_path *path,
234                         struct ext4_extent *ex, int *err, unsigned int flags)
235 {
236         ext4_fsblk_t goal, newblock;
237
238         goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
239         newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
240                                         NULL, err);
241         return newblock;
242 }
243
244 static inline int ext4_ext_space_block(struct inode *inode, int check)
245 {
246         int size;
247
248         size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
249                         / sizeof(struct ext4_extent);
250 #ifdef AGGRESSIVE_TEST
251         if (!check && size > 6)
252                 size = 6;
253 #endif
254         return size;
255 }
256
257 static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
258 {
259         int size;
260
261         size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
262                         / sizeof(struct ext4_extent_idx);
263 #ifdef AGGRESSIVE_TEST
264         if (!check && size > 5)
265                 size = 5;
266 #endif
267         return size;
268 }
269
270 static inline int ext4_ext_space_root(struct inode *inode, int check)
271 {
272         int size;
273
274         size = sizeof(EXT4_I(inode)->i_data);
275         size -= sizeof(struct ext4_extent_header);
276         size /= sizeof(struct ext4_extent);
277 #ifdef AGGRESSIVE_TEST
278         if (!check && size > 3)
279                 size = 3;
280 #endif
281         return size;
282 }
283
284 static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
285 {
286         int size;
287
288         size = sizeof(EXT4_I(inode)->i_data);
289         size -= sizeof(struct ext4_extent_header);
290         size /= sizeof(struct ext4_extent_idx);
291 #ifdef AGGRESSIVE_TEST
292         if (!check && size > 4)
293                 size = 4;
294 #endif
295         return size;
296 }
297
298 static inline int
299 ext4_force_split_extent_at(handle_t *handle, struct inode *inode,
300                            struct ext4_ext_path **ppath, ext4_lblk_t lblk,
301                            int nofail)
302 {
303         struct ext4_ext_path *path = *ppath;
304         int unwritten = ext4_ext_is_unwritten(path[path->p_depth].p_ext);
305
306         return ext4_split_extent_at(handle, inode, ppath, lblk, unwritten ?
307                         EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0,
308                         EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO |
309                         (nofail ? EXT4_GET_BLOCKS_METADATA_NOFAIL:0));
310 }
311
312 /*
313  * Calculate the number of metadata blocks needed
314  * to allocate @blocks
315  * Worse case is one block per extent
316  */
317 int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
318 {
319         struct ext4_inode_info *ei = EXT4_I(inode);
320         int idxs;
321
322         idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
323                 / sizeof(struct ext4_extent_idx));
324
325         /*
326          * If the new delayed allocation block is contiguous with the
327          * previous da block, it can share index blocks with the
328          * previous block, so we only need to allocate a new index
329          * block every idxs leaf blocks.  At ldxs**2 blocks, we need
330          * an additional index block, and at ldxs**3 blocks, yet
331          * another index blocks.
332          */
333         if (ei->i_da_metadata_calc_len &&
334             ei->i_da_metadata_calc_last_lblock+1 == lblock) {
335                 int num = 0;
336
337                 if ((ei->i_da_metadata_calc_len % idxs) == 0)
338                         num++;
339                 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
340                         num++;
341                 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
342                         num++;
343                         ei->i_da_metadata_calc_len = 0;
344                 } else
345                         ei->i_da_metadata_calc_len++;
346                 ei->i_da_metadata_calc_last_lblock++;
347                 return num;
348         }
349
350         /*
351          * In the worst case we need a new set of index blocks at
352          * every level of the inode's extent tree.
353          */
354         ei->i_da_metadata_calc_len = 1;
355         ei->i_da_metadata_calc_last_lblock = lblock;
356         return ext_depth(inode) + 1;
357 }
358
359 static int
360 ext4_ext_max_entries(struct inode *inode, int depth)
361 {
362         int max;
363
364         if (depth == ext_depth(inode)) {
365                 if (depth == 0)
366                         max = ext4_ext_space_root(inode, 1);
367                 else
368                         max = ext4_ext_space_root_idx(inode, 1);
369         } else {
370                 if (depth == 0)
371                         max = ext4_ext_space_block(inode, 1);
372                 else
373                         max = ext4_ext_space_block_idx(inode, 1);
374         }
375
376         return max;
377 }
378
379 static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
380 {
381         ext4_fsblk_t block = ext4_ext_pblock(ext);
382         int len = ext4_ext_get_actual_len(ext);
383         ext4_lblk_t lblock = le32_to_cpu(ext->ee_block);
384
385         /*
386          * We allow neither:
387          *  - zero length
388          *  - overflow/wrap-around
389          */
390         if (lblock + len <= lblock)
391                 return 0;
392         return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
393 }
394
395 static int ext4_valid_extent_idx(struct inode *inode,
396                                 struct ext4_extent_idx *ext_idx)
397 {
398         ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
399
400         return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
401 }
402
403 static int ext4_valid_extent_entries(struct inode *inode,
404                                 struct ext4_extent_header *eh,
405                                 int depth)
406 {
407         unsigned short entries;
408         if (eh->eh_entries == 0)
409                 return 1;
410
411         entries = le16_to_cpu(eh->eh_entries);
412
413         if (depth == 0) {
414                 /* leaf entries */
415                 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
416                 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
417                 ext4_fsblk_t pblock = 0;
418                 ext4_lblk_t lblock = 0;
419                 ext4_lblk_t prev = 0;
420                 int len = 0;
421                 while (entries) {
422                         if (!ext4_valid_extent(inode, ext))
423                                 return 0;
424
425                         /* Check for overlapping extents */
426                         lblock = le32_to_cpu(ext->ee_block);
427                         len = ext4_ext_get_actual_len(ext);
428                         if ((lblock <= prev) && prev) {
429                                 pblock = ext4_ext_pblock(ext);
430                                 es->s_last_error_block = cpu_to_le64(pblock);
431                                 return 0;
432                         }
433                         ext++;
434                         entries--;
435                         prev = lblock + len - 1;
436                 }
437         } else {
438                 struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
439                 while (entries) {
440                         if (!ext4_valid_extent_idx(inode, ext_idx))
441                                 return 0;
442                         ext_idx++;
443                         entries--;
444                 }
445         }
446         return 1;
447 }
448
449 static int __ext4_ext_check(const char *function, unsigned int line,
450                             struct inode *inode, struct ext4_extent_header *eh,
451                             int depth, ext4_fsblk_t pblk)
452 {
453         const char *error_msg;
454         int max = 0, err = -EFSCORRUPTED;
455
456         if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
457                 error_msg = "invalid magic";
458                 goto corrupted;
459         }
460         if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
461                 error_msg = "unexpected eh_depth";
462                 goto corrupted;
463         }
464         if (unlikely(eh->eh_max == 0)) {
465                 error_msg = "invalid eh_max";
466                 goto corrupted;
467         }
468         max = ext4_ext_max_entries(inode, depth);
469         if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
470                 error_msg = "too large eh_max";
471                 goto corrupted;
472         }
473         if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
474                 error_msg = "invalid eh_entries";
475                 goto corrupted;
476         }
477         if (!ext4_valid_extent_entries(inode, eh, depth)) {
478                 error_msg = "invalid extent entries";
479                 goto corrupted;
480         }
481         if (unlikely(depth > 32)) {
482                 error_msg = "too large eh_depth";
483                 goto corrupted;
484         }
485         /* Verify checksum on non-root extent tree nodes */
486         if (ext_depth(inode) != depth &&
487             !ext4_extent_block_csum_verify(inode, eh)) {
488                 error_msg = "extent tree corrupted";
489                 err = -EFSBADCRC;
490                 goto corrupted;
491         }
492         return 0;
493
494 corrupted:
495         ext4_error_inode(inode, function, line, 0,
496                          "pblk %llu bad header/extent: %s - magic %x, "
497                          "entries %u, max %u(%u), depth %u(%u)",
498                          (unsigned long long) pblk, error_msg,
499                          le16_to_cpu(eh->eh_magic),
500                          le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
501                          max, le16_to_cpu(eh->eh_depth), depth);
502         return err;
503 }
504
505 #define ext4_ext_check(inode, eh, depth, pblk)                  \
506         __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk))
507
508 int ext4_ext_check_inode(struct inode *inode)
509 {
510         return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode), 0);
511 }
512
513 static struct buffer_head *
514 __read_extent_tree_block(const char *function, unsigned int line,
515                          struct inode *inode, ext4_fsblk_t pblk, int depth,
516                          int flags)
517 {
518         struct buffer_head              *bh;
519         int                             err;
520
521         bh = sb_getblk_gfp(inode->i_sb, pblk, __GFP_MOVABLE | GFP_NOFS);
522         if (unlikely(!bh))
523                 return ERR_PTR(-ENOMEM);
524
525         if (!bh_uptodate_or_lock(bh)) {
526                 trace_ext4_ext_load_extent(inode, pblk, _RET_IP_);
527                 err = bh_submit_read(bh);
528                 if (err < 0)
529                         goto errout;
530         }
531         if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE))
532                 return bh;
533         err = __ext4_ext_check(function, line, inode,
534                                ext_block_hdr(bh), depth, pblk);
535         if (err)
536                 goto errout;
537         set_buffer_verified(bh);
538         /*
539          * If this is a leaf block, cache all of its entries
540          */
541         if (!(flags & EXT4_EX_NOCACHE) && depth == 0) {
542                 struct ext4_extent_header *eh = ext_block_hdr(bh);
543                 struct ext4_extent *ex = EXT_FIRST_EXTENT(eh);
544                 ext4_lblk_t prev = 0;
545                 int i;
546
547                 for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) {
548                         unsigned int status = EXTENT_STATUS_WRITTEN;
549                         ext4_lblk_t lblk = le32_to_cpu(ex->ee_block);
550                         int len = ext4_ext_get_actual_len(ex);
551
552                         if (prev && (prev != lblk))
553                                 ext4_es_cache_extent(inode, prev,
554                                                      lblk - prev, ~0,
555                                                      EXTENT_STATUS_HOLE);
556
557                         if (ext4_ext_is_unwritten(ex))
558                                 status = EXTENT_STATUS_UNWRITTEN;
559                         ext4_es_cache_extent(inode, lblk, len,
560                                              ext4_ext_pblock(ex), status);
561                         prev = lblk + len;
562                 }
563         }
564         return bh;
565 errout:
566         put_bh(bh);
567         return ERR_PTR(err);
568
569 }
570
571 #define read_extent_tree_block(inode, pblk, depth, flags)               \
572         __read_extent_tree_block(__func__, __LINE__, (inode), (pblk),   \
573                                  (depth), (flags))
574
575 /*
576  * This function is called to cache a file's extent information in the
577  * extent status tree
578  */
579 int ext4_ext_precache(struct inode *inode)
580 {
581         struct ext4_inode_info *ei = EXT4_I(inode);
582         struct ext4_ext_path *path = NULL;
583         struct buffer_head *bh;
584         int i = 0, depth, ret = 0;
585
586         if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
587                 return 0;       /* not an extent-mapped inode */
588
589         down_read(&ei->i_data_sem);
590         depth = ext_depth(inode);
591
592         path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1),
593                        GFP_NOFS);
594         if (path == NULL) {
595                 up_read(&ei->i_data_sem);
596                 return -ENOMEM;
597         }
598
599         /* Don't cache anything if there are no external extent blocks */
600         if (depth == 0)
601                 goto out;
602         path[0].p_hdr = ext_inode_hdr(inode);
603         ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0);
604         if (ret)
605                 goto out;
606         path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr);
607         while (i >= 0) {
608                 /*
609                  * If this is a leaf block or we've reached the end of
610                  * the index block, go up
611                  */
612                 if ((i == depth) ||
613                     path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) {
614                         brelse(path[i].p_bh);
615                         path[i].p_bh = NULL;
616                         i--;
617                         continue;
618                 }
619                 bh = read_extent_tree_block(inode,
620                                             ext4_idx_pblock(path[i].p_idx++),
621                                             depth - i - 1,
622                                             EXT4_EX_FORCE_CACHE);
623                 if (IS_ERR(bh)) {
624                         ret = PTR_ERR(bh);
625                         break;
626                 }
627                 i++;
628                 path[i].p_bh = bh;
629                 path[i].p_hdr = ext_block_hdr(bh);
630                 path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr);
631         }
632         ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
633 out:
634         up_read(&ei->i_data_sem);
635         ext4_ext_drop_refs(path);
636         kfree(path);
637         return ret;
638 }
639
640 #ifdef EXT_DEBUG
641 static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
642 {
643         int k, l = path->p_depth;
644
645         ext_debug("path:");
646         for (k = 0; k <= l; k++, path++) {
647                 if (path->p_idx) {
648                   ext_debug("  %d->%llu", le32_to_cpu(path->p_idx->ei_block),
649                             ext4_idx_pblock(path->p_idx));
650                 } else if (path->p_ext) {
651                         ext_debug("  %d:[%d]%d:%llu ",
652                                   le32_to_cpu(path->p_ext->ee_block),
653                                   ext4_ext_is_unwritten(path->p_ext),
654                                   ext4_ext_get_actual_len(path->p_ext),
655                                   ext4_ext_pblock(path->p_ext));
656                 } else
657                         ext_debug("  []");
658         }
659         ext_debug("\n");
660 }
661
662 static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
663 {
664         int depth = ext_depth(inode);
665         struct ext4_extent_header *eh;
666         struct ext4_extent *ex;
667         int i;
668
669         if (!path)
670                 return;
671
672         eh = path[depth].p_hdr;
673         ex = EXT_FIRST_EXTENT(eh);
674
675         ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
676
677         for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
678                 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
679                           ext4_ext_is_unwritten(ex),
680                           ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
681         }
682         ext_debug("\n");
683 }
684
685 static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
686                         ext4_fsblk_t newblock, int level)
687 {
688         int depth = ext_depth(inode);
689         struct ext4_extent *ex;
690
691         if (depth != level) {
692                 struct ext4_extent_idx *idx;
693                 idx = path[level].p_idx;
694                 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
695                         ext_debug("%d: move %d:%llu in new index %llu\n", level,
696                                         le32_to_cpu(idx->ei_block),
697                                         ext4_idx_pblock(idx),
698                                         newblock);
699                         idx++;
700                 }
701
702                 return;
703         }
704
705         ex = path[depth].p_ext;
706         while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
707                 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
708                                 le32_to_cpu(ex->ee_block),
709                                 ext4_ext_pblock(ex),
710                                 ext4_ext_is_unwritten(ex),
711                                 ext4_ext_get_actual_len(ex),
712                                 newblock);
713                 ex++;
714         }
715 }
716
717 #else
718 #define ext4_ext_show_path(inode, path)
719 #define ext4_ext_show_leaf(inode, path)
720 #define ext4_ext_show_move(inode, path, newblock, level)
721 #endif
722
723 void ext4_ext_drop_refs(struct ext4_ext_path *path)
724 {
725         int depth, i;
726
727         if (!path)
728                 return;
729         depth = path->p_depth;
730         for (i = 0; i <= depth; i++, path++)
731                 if (path->p_bh) {
732                         brelse(path->p_bh);
733                         path->p_bh = NULL;
734                 }
735 }
736
737 /*
738  * ext4_ext_binsearch_idx:
739  * binary search for the closest index of the given block
740  * the header must be checked before calling this
741  */
742 static void
743 ext4_ext_binsearch_idx(struct inode *inode,
744                         struct ext4_ext_path *path, ext4_lblk_t block)
745 {
746         struct ext4_extent_header *eh = path->p_hdr;
747         struct ext4_extent_idx *r, *l, *m;
748
749
750         ext_debug("binsearch for %u(idx):  ", block);
751
752         l = EXT_FIRST_INDEX(eh) + 1;
753         r = EXT_LAST_INDEX(eh);
754         while (l <= r) {
755                 m = l + (r - l) / 2;
756                 if (block < le32_to_cpu(m->ei_block))
757                         r = m - 1;
758                 else
759                         l = m + 1;
760                 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
761                                 m, le32_to_cpu(m->ei_block),
762                                 r, le32_to_cpu(r->ei_block));
763         }
764
765         path->p_idx = l - 1;
766         ext_debug("  -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block),
767                   ext4_idx_pblock(path->p_idx));
768
769 #ifdef CHECK_BINSEARCH
770         {
771                 struct ext4_extent_idx *chix, *ix;
772                 int k;
773
774                 chix = ix = EXT_FIRST_INDEX(eh);
775                 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
776                   if (k != 0 &&
777                       le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
778                                 printk(KERN_DEBUG "k=%d, ix=0x%p, "
779                                        "first=0x%p\n", k,
780                                        ix, EXT_FIRST_INDEX(eh));
781                                 printk(KERN_DEBUG "%u <= %u\n",
782                                        le32_to_cpu(ix->ei_block),
783                                        le32_to_cpu(ix[-1].ei_block));
784                         }
785                         BUG_ON(k && le32_to_cpu(ix->ei_block)
786                                            <= le32_to_cpu(ix[-1].ei_block));
787                         if (block < le32_to_cpu(ix->ei_block))
788                                 break;
789                         chix = ix;
790                 }
791                 BUG_ON(chix != path->p_idx);
792         }
793 #endif
794
795 }
796
797 /*
798  * ext4_ext_binsearch:
799  * binary search for closest extent of the given block
800  * the header must be checked before calling this
801  */
802 static void
803 ext4_ext_binsearch(struct inode *inode,
804                 struct ext4_ext_path *path, ext4_lblk_t block)
805 {
806         struct ext4_extent_header *eh = path->p_hdr;
807         struct ext4_extent *r, *l, *m;
808
809         if (eh->eh_entries == 0) {
810                 /*
811                  * this leaf is empty:
812                  * we get such a leaf in split/add case
813                  */
814                 return;
815         }
816
817         ext_debug("binsearch for %u:  ", block);
818
819         l = EXT_FIRST_EXTENT(eh) + 1;
820         r = EXT_LAST_EXTENT(eh);
821
822         while (l <= r) {
823                 m = l + (r - l) / 2;
824                 if (block < le32_to_cpu(m->ee_block))
825                         r = m - 1;
826                 else
827                         l = m + 1;
828                 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
829                                 m, le32_to_cpu(m->ee_block),
830                                 r, le32_to_cpu(r->ee_block));
831         }
832
833         path->p_ext = l - 1;
834         ext_debug("  -> %d:%llu:[%d]%d ",
835                         le32_to_cpu(path->p_ext->ee_block),
836                         ext4_ext_pblock(path->p_ext),
837                         ext4_ext_is_unwritten(path->p_ext),
838                         ext4_ext_get_actual_len(path->p_ext));
839
840 #ifdef CHECK_BINSEARCH
841         {
842                 struct ext4_extent *chex, *ex;
843                 int k;
844
845                 chex = ex = EXT_FIRST_EXTENT(eh);
846                 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
847                         BUG_ON(k && le32_to_cpu(ex->ee_block)
848                                           <= le32_to_cpu(ex[-1].ee_block));
849                         if (block < le32_to_cpu(ex->ee_block))
850                                 break;
851                         chex = ex;
852                 }
853                 BUG_ON(chex != path->p_ext);
854         }
855 #endif
856
857 }
858
859 int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
860 {
861         struct ext4_extent_header *eh;
862
863         eh = ext_inode_hdr(inode);
864         eh->eh_depth = 0;
865         eh->eh_entries = 0;
866         eh->eh_magic = EXT4_EXT_MAGIC;
867         eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
868         ext4_mark_inode_dirty(handle, inode);
869         return 0;
870 }
871
872 struct ext4_ext_path *
873 ext4_find_extent(struct inode *inode, ext4_lblk_t block,
874                  struct ext4_ext_path **orig_path, int flags)
875 {
876         struct ext4_extent_header *eh;
877         struct buffer_head *bh;
878         struct ext4_ext_path *path = orig_path ? *orig_path : NULL;
879         short int depth, i, ppos = 0;
880         int ret;
881
882         eh = ext_inode_hdr(inode);
883         depth = ext_depth(inode);
884
885         if (path) {
886                 ext4_ext_drop_refs(path);
887                 if (depth > path[0].p_maxdepth) {
888                         kfree(path);
889                         *orig_path = path = NULL;
890                 }
891         }
892         if (!path) {
893                 /* account possible depth increase */
894                 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
895                                 GFP_NOFS);
896                 if (unlikely(!path))
897                         return ERR_PTR(-ENOMEM);
898                 path[0].p_maxdepth = depth + 1;
899         }
900         path[0].p_hdr = eh;
901         path[0].p_bh = NULL;
902
903         i = depth;
904         /* walk through the tree */
905         while (i) {
906                 ext_debug("depth %d: num %d, max %d\n",
907                           ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
908
909                 ext4_ext_binsearch_idx(inode, path + ppos, block);
910                 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
911                 path[ppos].p_depth = i;
912                 path[ppos].p_ext = NULL;
913
914                 bh = read_extent_tree_block(inode, path[ppos].p_block, --i,
915                                             flags);
916                 if (IS_ERR(bh)) {
917                         ret = PTR_ERR(bh);
918                         goto err;
919                 }
920
921                 eh = ext_block_hdr(bh);
922                 ppos++;
923                 path[ppos].p_bh = bh;
924                 path[ppos].p_hdr = eh;
925         }
926
927         path[ppos].p_depth = i;
928         path[ppos].p_ext = NULL;
929         path[ppos].p_idx = NULL;
930
931         /* find extent */
932         ext4_ext_binsearch(inode, path + ppos, block);
933         /* if not an empty leaf */
934         if (path[ppos].p_ext)
935                 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
936
937         ext4_ext_show_path(inode, path);
938
939         return path;
940
941 err:
942         ext4_ext_drop_refs(path);
943         kfree(path);
944         if (orig_path)
945                 *orig_path = NULL;
946         return ERR_PTR(ret);
947 }
948
949 /*
950  * ext4_ext_insert_index:
951  * insert new index [@logical;@ptr] into the block at @curp;
952  * check where to insert: before @curp or after @curp
953  */
954 static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
955                                  struct ext4_ext_path *curp,
956                                  int logical, ext4_fsblk_t ptr)
957 {
958         struct ext4_extent_idx *ix;
959         int len, err;
960
961         err = ext4_ext_get_access(handle, inode, curp);
962         if (err)
963                 return err;
964
965         if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
966                 EXT4_ERROR_INODE(inode,
967                                  "logical %d == ei_block %d!",
968                                  logical, le32_to_cpu(curp->p_idx->ei_block));
969                 return -EFSCORRUPTED;
970         }
971
972         if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
973                              >= le16_to_cpu(curp->p_hdr->eh_max))) {
974                 EXT4_ERROR_INODE(inode,
975                                  "eh_entries %d >= eh_max %d!",
976                                  le16_to_cpu(curp->p_hdr->eh_entries),
977                                  le16_to_cpu(curp->p_hdr->eh_max));
978                 return -EFSCORRUPTED;
979         }
980
981         if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
982                 /* insert after */
983                 ext_debug("insert new index %d after: %llu\n", logical, ptr);
984                 ix = curp->p_idx + 1;
985         } else {
986                 /* insert before */
987                 ext_debug("insert new index %d before: %llu\n", logical, ptr);
988                 ix = curp->p_idx;
989         }
990
991         len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
992         BUG_ON(len < 0);
993         if (len > 0) {
994                 ext_debug("insert new index %d: "
995                                 "move %d indices from 0x%p to 0x%p\n",
996                                 logical, len, ix, ix + 1);
997                 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
998         }
999
1000         if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
1001                 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
1002                 return -EFSCORRUPTED;
1003         }
1004
1005         ix->ei_block = cpu_to_le32(logical);
1006         ext4_idx_store_pblock(ix, ptr);
1007         le16_add_cpu(&curp->p_hdr->eh_entries, 1);
1008
1009         if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
1010                 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
1011                 return -EFSCORRUPTED;
1012         }
1013
1014         err = ext4_ext_dirty(handle, inode, curp);
1015         ext4_std_error(inode->i_sb, err);
1016
1017         return err;
1018 }
1019
1020 /*
1021  * ext4_ext_split:
1022  * inserts new subtree into the path, using free index entry
1023  * at depth @at:
1024  * - allocates all needed blocks (new leaf and all intermediate index blocks)
1025  * - makes decision where to split
1026  * - moves remaining extents and index entries (right to the split point)
1027  *   into the newly allocated blocks
1028  * - initializes subtree
1029  */
1030 static int ext4_ext_split(handle_t *handle, struct inode *inode,
1031                           unsigned int flags,
1032                           struct ext4_ext_path *path,
1033                           struct ext4_extent *newext, int at)
1034 {
1035         struct buffer_head *bh = NULL;
1036         int depth = ext_depth(inode);
1037         struct ext4_extent_header *neh;
1038         struct ext4_extent_idx *fidx;
1039         int i = at, k, m, a;
1040         ext4_fsblk_t newblock, oldblock;
1041         __le32 border;
1042         ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
1043         int err = 0;
1044
1045         /* make decision: where to split? */
1046         /* FIXME: now decision is simplest: at current extent */
1047
1048         /* if current leaf will be split, then we should use
1049          * border from split point */
1050         if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
1051                 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
1052                 return -EFSCORRUPTED;
1053         }
1054         if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
1055                 border = path[depth].p_ext[1].ee_block;
1056                 ext_debug("leaf will be split."
1057                                 " next leaf starts at %d\n",
1058                                   le32_to_cpu(border));
1059         } else {
1060                 border = newext->ee_block;
1061                 ext_debug("leaf will be added."
1062                                 " next leaf starts at %d\n",
1063                                 le32_to_cpu(border));
1064         }
1065
1066         /*
1067          * If error occurs, then we break processing
1068          * and mark filesystem read-only. index won't
1069          * be inserted and tree will be in consistent
1070          * state. Next mount will repair buffers too.
1071          */
1072
1073         /*
1074          * Get array to track all allocated blocks.
1075          * We need this to handle errors and free blocks
1076          * upon them.
1077          */
1078         ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
1079         if (!ablocks)
1080                 return -ENOMEM;
1081
1082         /* allocate all needed blocks */
1083         ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
1084         for (a = 0; a < depth - at; a++) {
1085                 newblock = ext4_ext_new_meta_block(handle, inode, path,
1086                                                    newext, &err, flags);
1087                 if (newblock == 0)
1088                         goto cleanup;
1089                 ablocks[a] = newblock;
1090         }
1091
1092         /* initialize new leaf */
1093         newblock = ablocks[--a];
1094         if (unlikely(newblock == 0)) {
1095                 EXT4_ERROR_INODE(inode, "newblock == 0!");
1096                 err = -EFSCORRUPTED;
1097                 goto cleanup;
1098         }
1099         bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
1100         if (unlikely(!bh)) {
1101                 err = -ENOMEM;
1102                 goto cleanup;
1103         }
1104         lock_buffer(bh);
1105
1106         err = ext4_journal_get_create_access(handle, bh);
1107         if (err)
1108                 goto cleanup;
1109
1110         neh = ext_block_hdr(bh);
1111         neh->eh_entries = 0;
1112         neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
1113         neh->eh_magic = EXT4_EXT_MAGIC;
1114         neh->eh_depth = 0;
1115
1116         /* move remainder of path[depth] to the new leaf */
1117         if (unlikely(path[depth].p_hdr->eh_entries !=
1118                      path[depth].p_hdr->eh_max)) {
1119                 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
1120                                  path[depth].p_hdr->eh_entries,
1121                                  path[depth].p_hdr->eh_max);
1122                 err = -EFSCORRUPTED;
1123                 goto cleanup;
1124         }
1125         /* start copy from next extent */
1126         m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
1127         ext4_ext_show_move(inode, path, newblock, depth);
1128         if (m) {
1129                 struct ext4_extent *ex;
1130                 ex = EXT_FIRST_EXTENT(neh);
1131                 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
1132                 le16_add_cpu(&neh->eh_entries, m);
1133         }
1134
1135         ext4_extent_block_csum_set(inode, neh);
1136         set_buffer_uptodate(bh);
1137         unlock_buffer(bh);
1138
1139         err = ext4_handle_dirty_metadata(handle, inode, bh);
1140         if (err)
1141                 goto cleanup;
1142         brelse(bh);
1143         bh = NULL;
1144
1145         /* correct old leaf */
1146         if (m) {
1147                 err = ext4_ext_get_access(handle, inode, path + depth);
1148                 if (err)
1149                         goto cleanup;
1150                 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
1151                 err = ext4_ext_dirty(handle, inode, path + depth);
1152                 if (err)
1153                         goto cleanup;
1154
1155         }
1156
1157         /* create intermediate indexes */
1158         k = depth - at - 1;
1159         if (unlikely(k < 0)) {
1160                 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
1161                 err = -EFSCORRUPTED;
1162                 goto cleanup;
1163         }
1164         if (k)
1165                 ext_debug("create %d intermediate indices\n", k);
1166         /* insert new index into current index block */
1167         /* current depth stored in i var */
1168         i = depth - 1;
1169         while (k--) {
1170                 oldblock = newblock;
1171                 newblock = ablocks[--a];
1172                 bh = sb_getblk(inode->i_sb, newblock);
1173                 if (unlikely(!bh)) {
1174                         err = -ENOMEM;
1175                         goto cleanup;
1176                 }
1177                 lock_buffer(bh);
1178
1179                 err = ext4_journal_get_create_access(handle, bh);
1180                 if (err)
1181                         goto cleanup;
1182
1183                 neh = ext_block_hdr(bh);
1184                 neh->eh_entries = cpu_to_le16(1);
1185                 neh->eh_magic = EXT4_EXT_MAGIC;
1186                 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
1187                 neh->eh_depth = cpu_to_le16(depth - i);
1188                 fidx = EXT_FIRST_INDEX(neh);
1189                 fidx->ei_block = border;
1190                 ext4_idx_store_pblock(fidx, oldblock);
1191
1192                 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
1193                                 i, newblock, le32_to_cpu(border), oldblock);
1194
1195                 /* move remainder of path[i] to the new index block */
1196                 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
1197                                         EXT_LAST_INDEX(path[i].p_hdr))) {
1198                         EXT4_ERROR_INODE(inode,
1199                                          "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1200                                          le32_to_cpu(path[i].p_ext->ee_block));
1201                         err = -EFSCORRUPTED;
1202                         goto cleanup;
1203                 }
1204                 /* start copy indexes */
1205                 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
1206                 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
1207                                 EXT_MAX_INDEX(path[i].p_hdr));
1208                 ext4_ext_show_move(inode, path, newblock, i);
1209                 if (m) {
1210                         memmove(++fidx, path[i].p_idx,
1211                                 sizeof(struct ext4_extent_idx) * m);
1212                         le16_add_cpu(&neh->eh_entries, m);
1213                 }
1214                 ext4_extent_block_csum_set(inode, neh);
1215                 set_buffer_uptodate(bh);
1216                 unlock_buffer(bh);
1217
1218                 err = ext4_handle_dirty_metadata(handle, inode, bh);
1219                 if (err)
1220                         goto cleanup;
1221                 brelse(bh);
1222                 bh = NULL;
1223
1224                 /* correct old index */
1225                 if (m) {
1226                         err = ext4_ext_get_access(handle, inode, path + i);
1227                         if (err)
1228                                 goto cleanup;
1229                         le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
1230                         err = ext4_ext_dirty(handle, inode, path + i);
1231                         if (err)
1232                                 goto cleanup;
1233                 }
1234
1235                 i--;
1236         }
1237
1238         /* insert new index */
1239         err = ext4_ext_insert_index(handle, inode, path + at,
1240                                     le32_to_cpu(border), newblock);
1241
1242 cleanup:
1243         if (bh) {
1244                 if (buffer_locked(bh))
1245                         unlock_buffer(bh);
1246                 brelse(bh);
1247         }
1248
1249         if (err) {
1250                 /* free all allocated blocks in error case */
1251                 for (i = 0; i < depth; i++) {
1252                         if (!ablocks[i])
1253                                 continue;
1254                         ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
1255                                          EXT4_FREE_BLOCKS_METADATA);
1256                 }
1257         }
1258         kfree(ablocks);
1259
1260         return err;
1261 }
1262
1263 /*
1264  * ext4_ext_grow_indepth:
1265  * implements tree growing procedure:
1266  * - allocates new block
1267  * - moves top-level data (index block or leaf) into the new block
1268  * - initializes new top-level, creating index that points to the
1269  *   just created block
1270  */
1271 static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
1272                                  unsigned int flags)
1273 {
1274         struct ext4_extent_header *neh;
1275         struct buffer_head *bh;
1276         ext4_fsblk_t newblock, goal = 0;
1277         struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
1278         int err = 0;
1279
1280         /* Try to prepend new index to old one */
1281         if (ext_depth(inode))
1282                 goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode)));
1283         if (goal > le32_to_cpu(es->s_first_data_block)) {
1284                 flags |= EXT4_MB_HINT_TRY_GOAL;
1285                 goal--;
1286         } else
1287                 goal = ext4_inode_to_goal_block(inode);
1288         newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
1289                                         NULL, &err);
1290         if (newblock == 0)
1291                 return err;
1292
1293         bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
1294         if (unlikely(!bh))
1295                 return -ENOMEM;
1296         lock_buffer(bh);
1297
1298         err = ext4_journal_get_create_access(handle, bh);
1299         if (err) {
1300                 unlock_buffer(bh);
1301                 goto out;
1302         }
1303
1304         /* move top-level index/leaf into new block */
1305         memmove(bh->b_data, EXT4_I(inode)->i_data,
1306                 sizeof(EXT4_I(inode)->i_data));
1307
1308         /* set size of new block */
1309         neh = ext_block_hdr(bh);
1310         /* old root could have indexes or leaves
1311          * so calculate e_max right way */
1312         if (ext_depth(inode))
1313                 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
1314         else
1315                 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
1316         neh->eh_magic = EXT4_EXT_MAGIC;
1317         ext4_extent_block_csum_set(inode, neh);
1318         set_buffer_uptodate(bh);
1319         unlock_buffer(bh);
1320
1321         err = ext4_handle_dirty_metadata(handle, inode, bh);
1322         if (err)
1323                 goto out;
1324
1325         /* Update top-level index: num,max,pointer */
1326         neh = ext_inode_hdr(inode);
1327         neh->eh_entries = cpu_to_le16(1);
1328         ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1329         if (neh->eh_depth == 0) {
1330                 /* Root extent block becomes index block */
1331                 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1332                 EXT_FIRST_INDEX(neh)->ei_block =
1333                         EXT_FIRST_EXTENT(neh)->ee_block;
1334         }
1335         ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1336                   le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
1337                   le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
1338                   ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
1339
1340         le16_add_cpu(&neh->eh_depth, 1);
1341         ext4_mark_inode_dirty(handle, inode);
1342 out:
1343         brelse(bh);
1344
1345         return err;
1346 }
1347
1348 /*
1349  * ext4_ext_create_new_leaf:
1350  * finds empty index and adds new leaf.
1351  * if no free index is found, then it requests in-depth growing.
1352  */
1353 static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
1354                                     unsigned int mb_flags,
1355                                     unsigned int gb_flags,
1356                                     struct ext4_ext_path **ppath,
1357                                     struct ext4_extent *newext)
1358 {
1359         struct ext4_ext_path *path = *ppath;
1360         struct ext4_ext_path *curp;
1361         int depth, i, err = 0;
1362
1363 repeat:
1364         i = depth = ext_depth(inode);
1365
1366         /* walk up to the tree and look for free index entry */
1367         curp = path + depth;
1368         while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1369                 i--;
1370                 curp--;
1371         }
1372
1373         /* we use already allocated block for index block,
1374          * so subsequent data blocks should be contiguous */
1375         if (EXT_HAS_FREE_INDEX(curp)) {
1376                 /* if we found index with free entry, then use that
1377                  * entry: create all needed subtree and add new leaf */
1378                 err = ext4_ext_split(handle, inode, mb_flags, path, newext, i);
1379                 if (err)
1380                         goto out;
1381
1382                 /* refill path */
1383                 path = ext4_find_extent(inode,
1384                                     (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1385                                     ppath, gb_flags);
1386                 if (IS_ERR(path))
1387                         err = PTR_ERR(path);
1388         } else {
1389                 /* tree is full, time to grow in depth */
1390                 err = ext4_ext_grow_indepth(handle, inode, mb_flags);
1391                 if (err)
1392                         goto out;
1393
1394                 /* refill path */
1395                 path = ext4_find_extent(inode,
1396                                    (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1397                                     ppath, gb_flags);
1398                 if (IS_ERR(path)) {
1399                         err = PTR_ERR(path);
1400                         goto out;
1401                 }
1402
1403                 /*
1404                  * only first (depth 0 -> 1) produces free space;
1405                  * in all other cases we have to split the grown tree
1406                  */
1407                 depth = ext_depth(inode);
1408                 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
1409                         /* now we need to split */
1410                         goto repeat;
1411                 }
1412         }
1413
1414 out:
1415         return err;
1416 }
1417
1418 /*
1419  * search the closest allocated block to the left for *logical
1420  * and returns it at @logical + it's physical address at @phys
1421  * if *logical is the smallest allocated block, the function
1422  * returns 0 at @phys
1423  * return value contains 0 (success) or error code
1424  */
1425 static int ext4_ext_search_left(struct inode *inode,
1426                                 struct ext4_ext_path *path,
1427                                 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1428 {
1429         struct ext4_extent_idx *ix;
1430         struct ext4_extent *ex;
1431         int depth, ee_len;
1432
1433         if (unlikely(path == NULL)) {
1434                 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1435                 return -EFSCORRUPTED;
1436         }
1437         depth = path->p_depth;
1438         *phys = 0;
1439
1440         if (depth == 0 && path->p_ext == NULL)
1441                 return 0;
1442
1443         /* usually extent in the path covers blocks smaller
1444          * then *logical, but it can be that extent is the
1445          * first one in the file */
1446
1447         ex = path[depth].p_ext;
1448         ee_len = ext4_ext_get_actual_len(ex);
1449         if (*logical < le32_to_cpu(ex->ee_block)) {
1450                 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1451                         EXT4_ERROR_INODE(inode,
1452                                          "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1453                                          *logical, le32_to_cpu(ex->ee_block));
1454                         return -EFSCORRUPTED;
1455                 }
1456                 while (--depth >= 0) {
1457                         ix = path[depth].p_idx;
1458                         if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1459                                 EXT4_ERROR_INODE(inode,
1460                                   "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
1461                                   ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
1462                                   EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
1463                 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
1464                                   depth);
1465                                 return -EFSCORRUPTED;
1466                         }
1467                 }
1468                 return 0;
1469         }
1470
1471         if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1472                 EXT4_ERROR_INODE(inode,
1473                                  "logical %d < ee_block %d + ee_len %d!",
1474                                  *logical, le32_to_cpu(ex->ee_block), ee_len);
1475                 return -EFSCORRUPTED;
1476         }
1477
1478         *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1479         *phys = ext4_ext_pblock(ex) + ee_len - 1;
1480         return 0;
1481 }
1482
1483 /*
1484  * search the closest allocated block to the right for *logical
1485  * and returns it at @logical + it's physical address at @phys
1486  * if *logical is the largest allocated block, the function
1487  * returns 0 at @phys
1488  * return value contains 0 (success) or error code
1489  */
1490 static int ext4_ext_search_right(struct inode *inode,
1491                                  struct ext4_ext_path *path,
1492                                  ext4_lblk_t *logical, ext4_fsblk_t *phys,
1493                                  struct ext4_extent **ret_ex)
1494 {
1495         struct buffer_head *bh = NULL;
1496         struct ext4_extent_header *eh;
1497         struct ext4_extent_idx *ix;
1498         struct ext4_extent *ex;
1499         ext4_fsblk_t block;
1500         int depth;      /* Note, NOT eh_depth; depth from top of tree */
1501         int ee_len;
1502
1503         if (unlikely(path == NULL)) {
1504                 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1505                 return -EFSCORRUPTED;
1506         }
1507         depth = path->p_depth;
1508         *phys = 0;
1509
1510         if (depth == 0 && path->p_ext == NULL)
1511                 return 0;
1512
1513         /* usually extent in the path covers blocks smaller
1514          * then *logical, but it can be that extent is the
1515          * first one in the file */
1516
1517         ex = path[depth].p_ext;
1518         ee_len = ext4_ext_get_actual_len(ex);
1519         if (*logical < le32_to_cpu(ex->ee_block)) {
1520                 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1521                         EXT4_ERROR_INODE(inode,
1522                                          "first_extent(path[%d].p_hdr) != ex",
1523                                          depth);
1524                         return -EFSCORRUPTED;
1525                 }
1526                 while (--depth >= 0) {
1527                         ix = path[depth].p_idx;
1528                         if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1529                                 EXT4_ERROR_INODE(inode,
1530                                                  "ix != EXT_FIRST_INDEX *logical %d!",
1531                                                  *logical);
1532                                 return -EFSCORRUPTED;
1533                         }
1534                 }
1535                 goto found_extent;
1536         }
1537
1538         if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1539                 EXT4_ERROR_INODE(inode,
1540                                  "logical %d < ee_block %d + ee_len %d!",
1541                                  *logical, le32_to_cpu(ex->ee_block), ee_len);
1542                 return -EFSCORRUPTED;
1543         }
1544
1545         if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1546                 /* next allocated block in this leaf */
1547                 ex++;
1548                 goto found_extent;
1549         }
1550
1551         /* go up and search for index to the right */
1552         while (--depth >= 0) {
1553                 ix = path[depth].p_idx;
1554                 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
1555                         goto got_index;
1556         }
1557
1558         /* we've gone up to the root and found no index to the right */
1559         return 0;
1560
1561 got_index:
1562         /* we've found index to the right, let's
1563          * follow it and find the closest allocated
1564          * block to the right */
1565         ix++;
1566         block = ext4_idx_pblock(ix);
1567         while (++depth < path->p_depth) {
1568                 /* subtract from p_depth to get proper eh_depth */
1569                 bh = read_extent_tree_block(inode, block,
1570                                             path->p_depth - depth, 0);
1571                 if (IS_ERR(bh))
1572                         return PTR_ERR(bh);
1573                 eh = ext_block_hdr(bh);
1574                 ix = EXT_FIRST_INDEX(eh);
1575                 block = ext4_idx_pblock(ix);
1576                 put_bh(bh);
1577         }
1578
1579         bh = read_extent_tree_block(inode, block, path->p_depth - depth, 0);
1580         if (IS_ERR(bh))
1581                 return PTR_ERR(bh);
1582         eh = ext_block_hdr(bh);
1583         ex = EXT_FIRST_EXTENT(eh);
1584 found_extent:
1585         *logical = le32_to_cpu(ex->ee_block);
1586         *phys = ext4_ext_pblock(ex);
1587         *ret_ex = ex;
1588         if (bh)
1589                 put_bh(bh);
1590         return 0;
1591 }
1592
1593 /*
1594  * ext4_ext_next_allocated_block:
1595  * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
1596  * NOTE: it considers block number from index entry as
1597  * allocated block. Thus, index entries have to be consistent
1598  * with leaves.
1599  */
1600 ext4_lblk_t
1601 ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1602 {
1603         int depth;
1604
1605         BUG_ON(path == NULL);
1606         depth = path->p_depth;
1607
1608         if (depth == 0 && path->p_ext == NULL)
1609                 return EXT_MAX_BLOCKS;
1610
1611         while (depth >= 0) {
1612                 if (depth == path->p_depth) {
1613                         /* leaf */
1614                         if (path[depth].p_ext &&
1615                                 path[depth].p_ext !=
1616                                         EXT_LAST_EXTENT(path[depth].p_hdr))
1617                           return le32_to_cpu(path[depth].p_ext[1].ee_block);
1618                 } else {
1619                         /* index */
1620                         if (path[depth].p_idx !=
1621                                         EXT_LAST_INDEX(path[depth].p_hdr))
1622                           return le32_to_cpu(path[depth].p_idx[1].ei_block);
1623                 }
1624                 depth--;
1625         }
1626
1627         return EXT_MAX_BLOCKS;
1628 }
1629
1630 /*
1631  * ext4_ext_next_leaf_block:
1632  * returns first allocated block from next leaf or EXT_MAX_BLOCKS
1633  */
1634 static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
1635 {
1636         int depth;
1637
1638         BUG_ON(path == NULL);
1639         depth = path->p_depth;
1640
1641         /* zero-tree has no leaf blocks at all */
1642         if (depth == 0)
1643                 return EXT_MAX_BLOCKS;
1644
1645         /* go to index block */
1646         depth--;
1647
1648         while (depth >= 0) {
1649                 if (path[depth].p_idx !=
1650                                 EXT_LAST_INDEX(path[depth].p_hdr))
1651                         return (ext4_lblk_t)
1652                                 le32_to_cpu(path[depth].p_idx[1].ei_block);
1653                 depth--;
1654         }
1655
1656         return EXT_MAX_BLOCKS;
1657 }
1658
1659 /*
1660  * ext4_ext_correct_indexes:
1661  * if leaf gets modified and modified extent is first in the leaf,
1662  * then we have to correct all indexes above.
1663  * TODO: do we need to correct tree in all cases?
1664  */
1665 static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1666                                 struct ext4_ext_path *path)
1667 {
1668         struct ext4_extent_header *eh;
1669         int depth = ext_depth(inode);
1670         struct ext4_extent *ex;
1671         __le32 border;
1672         int k, err = 0;
1673
1674         eh = path[depth].p_hdr;
1675         ex = path[depth].p_ext;
1676
1677         if (unlikely(ex == NULL || eh == NULL)) {
1678                 EXT4_ERROR_INODE(inode,
1679                                  "ex %p == NULL or eh %p == NULL", ex, eh);
1680                 return -EFSCORRUPTED;
1681         }
1682
1683         if (depth == 0) {
1684                 /* there is no tree at all */
1685                 return 0;
1686         }
1687
1688         if (ex != EXT_FIRST_EXTENT(eh)) {
1689                 /* we correct tree if first leaf got modified only */
1690                 return 0;
1691         }
1692
1693         /*
1694          * TODO: we need correction if border is smaller than current one
1695          */
1696         k = depth - 1;
1697         border = path[depth].p_ext->ee_block;
1698         err = ext4_ext_get_access(handle, inode, path + k);
1699         if (err)
1700                 return err;
1701         path[k].p_idx->ei_block = border;
1702         err = ext4_ext_dirty(handle, inode, path + k);
1703         if (err)
1704                 return err;
1705
1706         while (k--) {
1707                 /* change all left-side indexes */
1708                 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1709                         break;
1710                 err = ext4_ext_get_access(handle, inode, path + k);
1711                 if (err)
1712                         break;
1713                 path[k].p_idx->ei_block = border;
1714                 err = ext4_ext_dirty(handle, inode, path + k);
1715                 if (err)
1716                         break;
1717         }
1718
1719         return err;
1720 }
1721
1722 int
1723 ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1724                                 struct ext4_extent *ex2)
1725 {
1726         unsigned short ext1_ee_len, ext2_ee_len;
1727
1728         if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2))
1729                 return 0;
1730
1731         ext1_ee_len = ext4_ext_get_actual_len(ex1);
1732         ext2_ee_len = ext4_ext_get_actual_len(ex2);
1733
1734         if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
1735                         le32_to_cpu(ex2->ee_block))
1736                 return 0;
1737
1738         /*
1739          * To allow future support for preallocated extents to be added
1740          * as an RO_COMPAT feature, refuse to merge to extents if
1741          * this can result in the top bit of ee_len being set.
1742          */
1743         if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN)
1744                 return 0;
1745         /*
1746          * The check for IO to unwritten extent is somewhat racy as we
1747          * increment i_unwritten / set EXT4_STATE_DIO_UNWRITTEN only after
1748          * dropping i_data_sem. But reserved blocks should save us in that
1749          * case.
1750          */
1751         if (ext4_ext_is_unwritten(ex1) &&
1752             (ext4_test_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN) ||
1753              atomic_read(&EXT4_I(inode)->i_unwritten) ||
1754              (ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN)))
1755                 return 0;
1756 #ifdef AGGRESSIVE_TEST
1757         if (ext1_ee_len >= 4)
1758                 return 0;
1759 #endif
1760
1761         if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
1762                 return 1;
1763         return 0;
1764 }
1765
1766 /*
1767  * This function tries to merge the "ex" extent to the next extent in the tree.
1768  * It always tries to merge towards right. If you want to merge towards
1769  * left, pass "ex - 1" as argument instead of "ex".
1770  * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1771  * 1 if they got merged.
1772  */
1773 static int ext4_ext_try_to_merge_right(struct inode *inode,
1774                                  struct ext4_ext_path *path,
1775                                  struct ext4_extent *ex)
1776 {
1777         struct ext4_extent_header *eh;
1778         unsigned int depth, len;
1779         int merge_done = 0, unwritten;
1780
1781         depth = ext_depth(inode);
1782         BUG_ON(path[depth].p_hdr == NULL);
1783         eh = path[depth].p_hdr;
1784
1785         while (ex < EXT_LAST_EXTENT(eh)) {
1786                 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1787                         break;
1788                 /* merge with next extent! */
1789                 unwritten = ext4_ext_is_unwritten(ex);
1790                 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1791                                 + ext4_ext_get_actual_len(ex + 1));
1792                 if (unwritten)
1793                         ext4_ext_mark_unwritten(ex);
1794
1795                 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1796                         len = (EXT_LAST_EXTENT(eh) - ex - 1)
1797                                 * sizeof(struct ext4_extent);
1798                         memmove(ex + 1, ex + 2, len);
1799                 }
1800                 le16_add_cpu(&eh->eh_entries, -1);
1801                 merge_done = 1;
1802                 WARN_ON(eh->eh_entries == 0);
1803                 if (!eh->eh_entries)
1804                         EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
1805         }
1806
1807         return merge_done;
1808 }
1809
1810 /*
1811  * This function does a very simple check to see if we can collapse
1812  * an extent tree with a single extent tree leaf block into the inode.
1813  */
1814 static void ext4_ext_try_to_merge_up(handle_t *handle,
1815                                      struct inode *inode,
1816                                      struct ext4_ext_path *path)
1817 {
1818         size_t s;
1819         unsigned max_root = ext4_ext_space_root(inode, 0);
1820         ext4_fsblk_t blk;
1821
1822         if ((path[0].p_depth != 1) ||
1823             (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) ||
1824             (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root))
1825                 return;
1826
1827         /*
1828          * We need to modify the block allocation bitmap and the block
1829          * group descriptor to release the extent tree block.  If we
1830          * can't get the journal credits, give up.
1831          */
1832         if (ext4_journal_extend(handle, 2))
1833                 return;
1834
1835         /*
1836          * Copy the extent data up to the inode
1837          */
1838         blk = ext4_idx_pblock(path[0].p_idx);
1839         s = le16_to_cpu(path[1].p_hdr->eh_entries) *
1840                 sizeof(struct ext4_extent_idx);
1841         s += sizeof(struct ext4_extent_header);
1842
1843         path[1].p_maxdepth = path[0].p_maxdepth;
1844         memcpy(path[0].p_hdr, path[1].p_hdr, s);
1845         path[0].p_depth = 0;
1846         path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) +
1847                 (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr));
1848         path[0].p_hdr->eh_max = cpu_to_le16(max_root);
1849
1850         brelse(path[1].p_bh);
1851         ext4_free_blocks(handle, inode, NULL, blk, 1,
1852                          EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
1853 }
1854
1855 /*
1856  * This function tries to merge the @ex extent to neighbours in the tree.
1857  * return 1 if merge left else 0.
1858  */
1859 static void ext4_ext_try_to_merge(handle_t *handle,
1860                                   struct inode *inode,
1861                                   struct ext4_ext_path *path,
1862                                   struct ext4_extent *ex) {
1863         struct ext4_extent_header *eh;
1864         unsigned int depth;
1865         int merge_done = 0;
1866
1867         depth = ext_depth(inode);
1868         BUG_ON(path[depth].p_hdr == NULL);
1869         eh = path[depth].p_hdr;
1870
1871         if (ex > EXT_FIRST_EXTENT(eh))
1872                 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1873
1874         if (!merge_done)
1875                 (void) ext4_ext_try_to_merge_right(inode, path, ex);
1876
1877         ext4_ext_try_to_merge_up(handle, inode, path);
1878 }
1879
1880 /*
1881  * check if a portion of the "newext" extent overlaps with an
1882  * existing extent.
1883  *
1884  * If there is an overlap discovered, it updates the length of the newext
1885  * such that there will be no overlap, and then returns 1.
1886  * If there is no overlap found, it returns 0.
1887  */
1888 static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1889                                            struct inode *inode,
1890                                            struct ext4_extent *newext,
1891                                            struct ext4_ext_path *path)
1892 {
1893         ext4_lblk_t b1, b2;
1894         unsigned int depth, len1;
1895         unsigned int ret = 0;
1896
1897         b1 = le32_to_cpu(newext->ee_block);
1898         len1 = ext4_ext_get_actual_len(newext);
1899         depth = ext_depth(inode);
1900         if (!path[depth].p_ext)
1901                 goto out;
1902         b2 = EXT4_LBLK_CMASK(sbi, le32_to_cpu(path[depth].p_ext->ee_block));
1903
1904         /*
1905          * get the next allocated block if the extent in the path
1906          * is before the requested block(s)
1907          */
1908         if (b2 < b1) {
1909                 b2 = ext4_ext_next_allocated_block(path);
1910                 if (b2 == EXT_MAX_BLOCKS)
1911                         goto out;
1912                 b2 = EXT4_LBLK_CMASK(sbi, b2);
1913         }
1914
1915         /* check for wrap through zero on extent logical start block*/
1916         if (b1 + len1 < b1) {
1917                 len1 = EXT_MAX_BLOCKS - b1;
1918                 newext->ee_len = cpu_to_le16(len1);
1919                 ret = 1;
1920         }
1921
1922         /* check for overlap */
1923         if (b1 + len1 > b2) {
1924                 newext->ee_len = cpu_to_le16(b2 - b1);
1925                 ret = 1;
1926         }
1927 out:
1928         return ret;
1929 }
1930
1931 /*
1932  * ext4_ext_insert_extent:
1933  * tries to merge requsted extent into the existing extent or
1934  * inserts requested extent as new one into the tree,
1935  * creating new leaf in the no-space case.
1936  */
1937 int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1938                                 struct ext4_ext_path **ppath,
1939                                 struct ext4_extent *newext, int gb_flags)
1940 {
1941         struct ext4_ext_path *path = *ppath;
1942         struct ext4_extent_header *eh;
1943         struct ext4_extent *ex, *fex;
1944         struct ext4_extent *nearex; /* nearest extent */
1945         struct ext4_ext_path *npath = NULL;
1946         int depth, len, err;
1947         ext4_lblk_t next;
1948         int mb_flags = 0, unwritten;
1949
1950         if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
1951                 mb_flags |= EXT4_MB_DELALLOC_RESERVED;
1952         if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1953                 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1954                 return -EFSCORRUPTED;
1955         }
1956         depth = ext_depth(inode);
1957         ex = path[depth].p_ext;
1958         eh = path[depth].p_hdr;
1959         if (unlikely(path[depth].p_hdr == NULL)) {
1960                 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1961                 return -EFSCORRUPTED;
1962         }
1963
1964         /* try to insert block into found extent and return */
1965         if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) {
1966
1967                 /*
1968                  * Try to see whether we should rather test the extent on
1969                  * right from ex, or from the left of ex. This is because
1970                  * ext4_find_extent() can return either extent on the
1971                  * left, or on the right from the searched position. This
1972                  * will make merging more effective.
1973                  */
1974                 if (ex < EXT_LAST_EXTENT(eh) &&
1975                     (le32_to_cpu(ex->ee_block) +
1976                     ext4_ext_get_actual_len(ex) <
1977                     le32_to_cpu(newext->ee_block))) {
1978                         ex += 1;
1979                         goto prepend;
1980                 } else if ((ex > EXT_FIRST_EXTENT(eh)) &&
1981                            (le32_to_cpu(newext->ee_block) +
1982                            ext4_ext_get_actual_len(newext) <
1983                            le32_to_cpu(ex->ee_block)))
1984                         ex -= 1;
1985
1986                 /* Try to append newex to the ex */
1987                 if (ext4_can_extents_be_merged(inode, ex, newext)) {
1988                         ext_debug("append [%d]%d block to %u:[%d]%d"
1989                                   "(from %llu)\n",
1990                                   ext4_ext_is_unwritten(newext),
1991                                   ext4_ext_get_actual_len(newext),
1992                                   le32_to_cpu(ex->ee_block),
1993                                   ext4_ext_is_unwritten(ex),
1994                                   ext4_ext_get_actual_len(ex),
1995                                   ext4_ext_pblock(ex));
1996                         err = ext4_ext_get_access(handle, inode,
1997                                                   path + depth);
1998                         if (err)
1999                                 return err;
2000                         unwritten = ext4_ext_is_unwritten(ex);
2001                         ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
2002                                         + ext4_ext_get_actual_len(newext));
2003                         if (unwritten)
2004                                 ext4_ext_mark_unwritten(ex);
2005                         eh = path[depth].p_hdr;
2006                         nearex = ex;
2007                         goto merge;
2008                 }
2009
2010 prepend:
2011                 /* Try to prepend newex to the ex */
2012                 if (ext4_can_extents_be_merged(inode, newext, ex)) {
2013                         ext_debug("prepend %u[%d]%d block to %u:[%d]%d"
2014                                   "(from %llu)\n",
2015                                   le32_to_cpu(newext->ee_block),
2016                                   ext4_ext_is_unwritten(newext),
2017                                   ext4_ext_get_actual_len(newext),
2018                                   le32_to_cpu(ex->ee_block),
2019                                   ext4_ext_is_unwritten(ex),
2020                                   ext4_ext_get_actual_len(ex),
2021                                   ext4_ext_pblock(ex));
2022                         err = ext4_ext_get_access(handle, inode,
2023                                                   path + depth);
2024                         if (err)
2025                                 return err;
2026
2027                         unwritten = ext4_ext_is_unwritten(ex);
2028                         ex->ee_block = newext->ee_block;
2029                         ext4_ext_store_pblock(ex, ext4_ext_pblock(newext));
2030                         ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
2031                                         + ext4_ext_get_actual_len(newext));
2032                         if (unwritten)
2033                                 ext4_ext_mark_unwritten(ex);
2034                         eh = path[depth].p_hdr;
2035                         nearex = ex;
2036                         goto merge;
2037                 }
2038         }
2039
2040         depth = ext_depth(inode);
2041         eh = path[depth].p_hdr;
2042         if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
2043                 goto has_space;
2044
2045         /* probably next leaf has space for us? */
2046         fex = EXT_LAST_EXTENT(eh);
2047         next = EXT_MAX_BLOCKS;
2048         if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
2049                 next = ext4_ext_next_leaf_block(path);
2050         if (next != EXT_MAX_BLOCKS) {
2051                 ext_debug("next leaf block - %u\n", next);
2052                 BUG_ON(npath != NULL);
2053                 npath = ext4_find_extent(inode, next, NULL, 0);
2054                 if (IS_ERR(npath))
2055                         return PTR_ERR(npath);
2056                 BUG_ON(npath->p_depth != path->p_depth);
2057                 eh = npath[depth].p_hdr;
2058                 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
2059                         ext_debug("next leaf isn't full(%d)\n",
2060                                   le16_to_cpu(eh->eh_entries));
2061                         path = npath;
2062                         goto has_space;
2063                 }
2064                 ext_debug("next leaf has no free space(%d,%d)\n",
2065                           le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
2066         }
2067
2068         /*
2069          * There is no free space in the found leaf.
2070          * We're gonna add a new leaf in the tree.
2071          */
2072         if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
2073                 mb_flags |= EXT4_MB_USE_RESERVED;
2074         err = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags,
2075                                        ppath, newext);
2076         if (err)
2077                 goto cleanup;
2078         depth = ext_depth(inode);
2079         eh = path[depth].p_hdr;
2080
2081 has_space:
2082         nearex = path[depth].p_ext;
2083
2084         err = ext4_ext_get_access(handle, inode, path + depth);
2085         if (err)
2086                 goto cleanup;
2087
2088         if (!nearex) {
2089                 /* there is no extent in this leaf, create first one */
2090                 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
2091                                 le32_to_cpu(newext->ee_block),
2092                                 ext4_ext_pblock(newext),
2093                                 ext4_ext_is_unwritten(newext),
2094                                 ext4_ext_get_actual_len(newext));
2095                 nearex = EXT_FIRST_EXTENT(eh);
2096         } else {
2097                 if (le32_to_cpu(newext->ee_block)
2098                            > le32_to_cpu(nearex->ee_block)) {
2099                         /* Insert after */
2100                         ext_debug("insert %u:%llu:[%d]%d before: "
2101                                         "nearest %p\n",
2102                                         le32_to_cpu(newext->ee_block),
2103                                         ext4_ext_pblock(newext),
2104                                         ext4_ext_is_unwritten(newext),
2105                                         ext4_ext_get_actual_len(newext),
2106                                         nearex);
2107                         nearex++;
2108                 } else {
2109                         /* Insert before */
2110                         BUG_ON(newext->ee_block == nearex->ee_block);
2111                         ext_debug("insert %u:%llu:[%d]%d after: "
2112                                         "nearest %p\n",
2113                                         le32_to_cpu(newext->ee_block),
2114                                         ext4_ext_pblock(newext),
2115                                         ext4_ext_is_unwritten(newext),
2116                                         ext4_ext_get_actual_len(newext),
2117                                         nearex);
2118                 }
2119                 len = EXT_LAST_EXTENT(eh) - nearex + 1;
2120                 if (len > 0) {
2121                         ext_debug("insert %u:%llu:[%d]%d: "
2122                                         "move %d extents from 0x%p to 0x%p\n",
2123                                         le32_to_cpu(newext->ee_block),
2124                                         ext4_ext_pblock(newext),
2125                                         ext4_ext_is_unwritten(newext),
2126                                         ext4_ext_get_actual_len(newext),
2127                                         len, nearex, nearex + 1);
2128                         memmove(nearex + 1, nearex,
2129                                 len * sizeof(struct ext4_extent));
2130                 }
2131         }
2132
2133         le16_add_cpu(&eh->eh_entries, 1);
2134         path[depth].p_ext = nearex;
2135         nearex->ee_block = newext->ee_block;
2136         ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
2137         nearex->ee_len = newext->ee_len;
2138
2139 merge:
2140         /* try to merge extents */
2141         if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO))
2142                 ext4_ext_try_to_merge(handle, inode, path, nearex);
2143
2144
2145         /* time to correct all indexes above */
2146         err = ext4_ext_correct_indexes(handle, inode, path);
2147         if (err)
2148                 goto cleanup;
2149
2150         err = ext4_ext_dirty(handle, inode, path + path->p_depth);
2151
2152 cleanup:
2153         ext4_ext_drop_refs(npath);
2154         kfree(npath);
2155         return err;
2156 }
2157
2158 static int ext4_fill_fiemap_extents(struct inode *inode,
2159                                     ext4_lblk_t block, ext4_lblk_t num,
2160                                     struct fiemap_extent_info *fieinfo)
2161 {
2162         struct ext4_ext_path *path = NULL;
2163         struct ext4_extent *ex;
2164         struct extent_status es;
2165         ext4_lblk_t next, next_del, start = 0, end = 0;
2166         ext4_lblk_t last = block + num;
2167         int exists, depth = 0, err = 0;
2168         unsigned int flags = 0;
2169         unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
2170
2171         while (block < last && block != EXT_MAX_BLOCKS) {
2172                 num = last - block;
2173                 /* find extent for this block */
2174                 down_read(&EXT4_I(inode)->i_data_sem);
2175
2176                 path = ext4_find_extent(inode, block, &path, 0);
2177                 if (IS_ERR(path)) {
2178                         up_read(&EXT4_I(inode)->i_data_sem);
2179                         err = PTR_ERR(path);
2180                         path = NULL;
2181                         break;
2182                 }
2183
2184                 depth = ext_depth(inode);
2185                 if (unlikely(path[depth].p_hdr == NULL)) {
2186                         up_read(&EXT4_I(inode)->i_data_sem);
2187                         EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2188                         err = -EFSCORRUPTED;
2189                         break;
2190                 }
2191                 ex = path[depth].p_ext;
2192                 next = ext4_ext_next_allocated_block(path);
2193
2194                 flags = 0;
2195                 exists = 0;
2196                 if (!ex) {
2197                         /* there is no extent yet, so try to allocate
2198                          * all requested space */
2199                         start = block;
2200                         end = block + num;
2201                 } else if (le32_to_cpu(ex->ee_block) > block) {
2202                         /* need to allocate space before found extent */
2203                         start = block;
2204                         end = le32_to_cpu(ex->ee_block);
2205                         if (block + num < end)
2206                                 end = block + num;
2207                 } else if (block >= le32_to_cpu(ex->ee_block)
2208                                         + ext4_ext_get_actual_len(ex)) {
2209                         /* need to allocate space after found extent */
2210                         start = block;
2211                         end = block + num;
2212                         if (end >= next)
2213                                 end = next;
2214                 } else if (block >= le32_to_cpu(ex->ee_block)) {
2215                         /*
2216                          * some part of requested space is covered
2217                          * by found extent
2218                          */
2219                         start = block;
2220                         end = le32_to_cpu(ex->ee_block)
2221                                 + ext4_ext_get_actual_len(ex);
2222                         if (block + num < end)
2223                                 end = block + num;
2224                         exists = 1;
2225                 } else {
2226                         BUG();
2227                 }
2228                 BUG_ON(end <= start);
2229
2230                 if (!exists) {
2231                         es.es_lblk = start;
2232                         es.es_len = end - start;
2233                         es.es_pblk = 0;
2234                 } else {
2235                         es.es_lblk = le32_to_cpu(ex->ee_block);
2236                         es.es_len = ext4_ext_get_actual_len(ex);
2237                         es.es_pblk = ext4_ext_pblock(ex);
2238                         if (ext4_ext_is_unwritten(ex))
2239                                 flags |= FIEMAP_EXTENT_UNWRITTEN;
2240                 }
2241
2242                 /*
2243                  * Find delayed extent and update es accordingly. We call
2244                  * it even in !exists case to find out whether es is the
2245                  * last existing extent or not.
2246                  */
2247                 next_del = ext4_find_delayed_extent(inode, &es);
2248                 if (!exists && next_del) {
2249                         exists = 1;
2250                         flags |= (FIEMAP_EXTENT_DELALLOC |
2251                                   FIEMAP_EXTENT_UNKNOWN);
2252                 }
2253                 up_read(&EXT4_I(inode)->i_data_sem);
2254
2255                 if (unlikely(es.es_len == 0)) {
2256                         EXT4_ERROR_INODE(inode, "es.es_len == 0");
2257                         err = -EFSCORRUPTED;
2258                         break;
2259                 }
2260
2261                 /*
2262                  * This is possible iff next == next_del == EXT_MAX_BLOCKS.
2263                  * we need to check next == EXT_MAX_BLOCKS because it is
2264                  * possible that an extent is with unwritten and delayed
2265                  * status due to when an extent is delayed allocated and
2266                  * is allocated by fallocate status tree will track both of
2267                  * them in a extent.
2268                  *
2269                  * So we could return a unwritten and delayed extent, and
2270                  * its block is equal to 'next'.
2271                  */
2272                 if (next == next_del && next == EXT_MAX_BLOCKS) {
2273                         flags |= FIEMAP_EXTENT_LAST;
2274                         if (unlikely(next_del != EXT_MAX_BLOCKS ||
2275                                      next != EXT_MAX_BLOCKS)) {
2276                                 EXT4_ERROR_INODE(inode,
2277                                                  "next extent == %u, next "
2278                                                  "delalloc extent = %u",
2279                                                  next, next_del);
2280                                 err = -EFSCORRUPTED;
2281                                 break;
2282                         }
2283                 }
2284
2285                 if (exists) {
2286                         err = fiemap_fill_next_extent(fieinfo,
2287                                 (__u64)es.es_lblk << blksize_bits,
2288                                 (__u64)es.es_pblk << blksize_bits,
2289                                 (__u64)es.es_len << blksize_bits,
2290                                 flags);
2291                         if (err < 0)
2292                                 break;
2293                         if (err == 1) {
2294                                 err = 0;
2295                                 break;
2296                         }
2297                 }
2298
2299                 block = es.es_lblk + es.es_len;
2300         }
2301
2302         ext4_ext_drop_refs(path);
2303         kfree(path);
2304         return err;
2305 }
2306
2307 /*
2308  * ext4_ext_determine_hole - determine hole around given block
2309  * @inode:      inode we lookup in
2310  * @path:       path in extent tree to @lblk
2311  * @lblk:       pointer to logical block around which we want to determine hole
2312  *
2313  * Determine hole length (and start if easily possible) around given logical
2314  * block. We don't try too hard to find the beginning of the hole but @path
2315  * actually points to extent before @lblk, we provide it.
2316  *
2317  * The function returns the length of a hole starting at @lblk. We update @lblk
2318  * to the beginning of the hole if we managed to find it.
2319  */
2320 static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode,
2321                                            struct ext4_ext_path *path,
2322                                            ext4_lblk_t *lblk)
2323 {
2324         int depth = ext_depth(inode);
2325         struct ext4_extent *ex;
2326         ext4_lblk_t len;
2327
2328         ex = path[depth].p_ext;
2329         if (ex == NULL) {
2330                 /* there is no extent yet, so gap is [0;-] */
2331                 *lblk = 0;
2332                 len = EXT_MAX_BLOCKS;
2333         } else if (*lblk < le32_to_cpu(ex->ee_block)) {
2334                 len = le32_to_cpu(ex->ee_block) - *lblk;
2335         } else if (*lblk >= le32_to_cpu(ex->ee_block)
2336                         + ext4_ext_get_actual_len(ex)) {
2337                 ext4_lblk_t next;
2338
2339                 *lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
2340                 next = ext4_ext_next_allocated_block(path);
2341                 BUG_ON(next == *lblk);
2342                 len = next - *lblk;
2343         } else {
2344                 BUG();
2345         }
2346         return len;
2347 }
2348
2349 /*
2350  * ext4_ext_put_gap_in_cache:
2351  * calculate boundaries of the gap that the requested block fits into
2352  * and cache this gap
2353  */
2354 static void
2355 ext4_ext_put_gap_in_cache(struct inode *inode, ext4_lblk_t hole_start,
2356                           ext4_lblk_t hole_len)
2357 {
2358         struct extent_status es;
2359
2360         ext4_es_find_delayed_extent_range(inode, hole_start,
2361                                           hole_start + hole_len - 1, &es);
2362         if (es.es_len) {
2363                 /* There's delayed extent containing lblock? */
2364                 if (es.es_lblk <= hole_start)
2365                         return;
2366                 hole_len = min(es.es_lblk - hole_start, hole_len);
2367         }
2368         ext_debug(" -> %u:%u\n", hole_start, hole_len);
2369         ext4_es_insert_extent(inode, hole_start, hole_len, ~0,
2370                               EXTENT_STATUS_HOLE);
2371 }
2372
2373 /*
2374  * ext4_ext_rm_idx:
2375  * removes index from the index block.
2376  */
2377 static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
2378                         struct ext4_ext_path *path, int depth)
2379 {
2380         int err;
2381         ext4_fsblk_t leaf;
2382
2383         /* free index block */
2384         depth--;
2385         path = path + depth;
2386         leaf = ext4_idx_pblock(path->p_idx);
2387         if (unlikely(path->p_hdr->eh_entries == 0)) {
2388                 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2389                 return -EFSCORRUPTED;
2390         }
2391         err = ext4_ext_get_access(handle, inode, path);
2392         if (err)
2393                 return err;
2394
2395         if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2396                 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2397                 len *= sizeof(struct ext4_extent_idx);
2398                 memmove(path->p_idx, path->p_idx + 1, len);
2399         }
2400
2401         le16_add_cpu(&path->p_hdr->eh_entries, -1);
2402         err = ext4_ext_dirty(handle, inode, path);
2403         if (err)
2404                 return err;
2405         ext_debug("index is empty, remove it, free block %llu\n", leaf);
2406         trace_ext4_ext_rm_idx(inode, leaf);
2407
2408         ext4_free_blocks(handle, inode, NULL, leaf, 1,
2409                          EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
2410
2411         while (--depth >= 0) {
2412                 if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr))
2413                         break;
2414                 path--;
2415                 err = ext4_ext_get_access(handle, inode, path);
2416                 if (err)
2417                         break;
2418                 path->p_idx->ei_block = (path+1)->p_idx->ei_block;
2419                 err = ext4_ext_dirty(handle, inode, path);
2420                 if (err)
2421                         break;
2422         }
2423         return err;
2424 }
2425
2426 /*
2427  * ext4_ext_calc_credits_for_single_extent:
2428  * This routine returns max. credits that needed to insert an extent
2429  * to the extent tree.
2430  * When pass the actual path, the caller should calculate credits
2431  * under i_data_sem.
2432  */
2433 int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
2434                                                 struct ext4_ext_path *path)
2435 {
2436         if (path) {
2437                 int depth = ext_depth(inode);
2438                 int ret = 0;
2439
2440                 /* probably there is space in leaf? */
2441                 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
2442                                 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2443
2444                         /*
2445                          *  There are some space in the leaf tree, no
2446                          *  need to account for leaf block credit
2447                          *
2448                          *  bitmaps and block group descriptor blocks
2449                          *  and other metadata blocks still need to be
2450                          *  accounted.
2451                          */
2452                         /* 1 bitmap, 1 block group descriptor */
2453                         ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
2454                         return ret;
2455                 }
2456         }
2457
2458         return ext4_chunk_trans_blocks(inode, nrblocks);
2459 }
2460
2461 /*
2462  * How many index/leaf blocks need to change/allocate to add @extents extents?
2463  *
2464  * If we add a single extent, then in the worse case, each tree level
2465  * index/leaf need to be changed in case of the tree split.
2466  *
2467  * If more extents are inserted, they could cause the whole tree split more
2468  * than once, but this is really rare.
2469  */
2470 int ext4_ext_index_trans_blocks(struct inode *inode, int extents)
2471 {
2472         int index;
2473         int depth;
2474
2475         /* If we are converting the inline data, only one is needed here. */
2476         if (ext4_has_inline_data(inode))
2477                 return 1;
2478
2479         depth = ext_depth(inode);
2480
2481         if (extents <= 1)
2482                 index = depth * 2;
2483         else
2484                 index = depth * 3;
2485
2486         return index;
2487 }
2488
2489 static inline int get_default_free_blocks_flags(struct inode *inode)
2490 {
2491         if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2492                 return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET;
2493         else if (ext4_should_journal_data(inode))
2494                 return EXT4_FREE_BLOCKS_FORGET;
2495         return 0;
2496 }
2497
2498 static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2499                               struct ext4_extent *ex,
2500                               long long *partial_cluster,
2501                               ext4_lblk_t from, ext4_lblk_t to)
2502 {
2503         struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2504         unsigned short ee_len = ext4_ext_get_actual_len(ex);
2505         ext4_fsblk_t pblk;
2506         int flags = get_default_free_blocks_flags(inode);
2507
2508         /*
2509          * For bigalloc file systems, we never free a partial cluster
2510          * at the beginning of the extent.  Instead, we make a note
2511          * that we tried freeing the cluster, and check to see if we
2512          * need to free it on a subsequent call to ext4_remove_blocks,
2513          * or at the end of ext4_ext_rm_leaf or ext4_ext_remove_space.
2514          */
2515         flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
2516
2517         trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster);
2518         /*
2519          * If we have a partial cluster, and it's different from the
2520          * cluster of the last block, we need to explicitly free the
2521          * partial cluster here.
2522          */
2523         pblk = ext4_ext_pblock(ex) + ee_len - 1;
2524         if (*partial_cluster > 0 &&
2525             *partial_cluster != (long long) EXT4_B2C(sbi, pblk)) {
2526                 ext4_free_blocks(handle, inode, NULL,
2527                                  EXT4_C2B(sbi, *partial_cluster),
2528                                  sbi->s_cluster_ratio, flags);
2529                 *partial_cluster = 0;
2530         }
2531
2532 #ifdef EXTENTS_STATS
2533         {
2534                 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2535                 spin_lock(&sbi->s_ext_stats_lock);
2536                 sbi->s_ext_blocks += ee_len;
2537                 sbi->s_ext_extents++;
2538                 if (ee_len < sbi->s_ext_min)
2539                         sbi->s_ext_min = ee_len;
2540                 if (ee_len > sbi->s_ext_max)
2541                         sbi->s_ext_max = ee_len;
2542                 if (ext_depth(inode) > sbi->s_depth_max)
2543                         sbi->s_depth_max = ext_depth(inode);
2544                 spin_unlock(&sbi->s_ext_stats_lock);
2545         }
2546 #endif
2547         if (from >= le32_to_cpu(ex->ee_block)
2548             && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
2549                 /* tail removal */
2550                 ext4_lblk_t num;
2551                 long long first_cluster;
2552
2553                 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2554                 pblk = ext4_ext_pblock(ex) + ee_len - num;
2555                 /*
2556                  * Usually we want to free partial cluster at the end of the
2557                  * extent, except for the situation when the cluster is still
2558                  * used by any other extent (partial_cluster is negative).
2559                  */
2560                 if (*partial_cluster < 0 &&
2561                     *partial_cluster == -(long long) EXT4_B2C(sbi, pblk+num-1))
2562                         flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER;
2563
2564                 ext_debug("free last %u blocks starting %llu partial %lld\n",
2565                           num, pblk, *partial_cluster);
2566                 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
2567                 /*
2568                  * If the block range to be freed didn't start at the
2569                  * beginning of a cluster, and we removed the entire
2570                  * extent and the cluster is not used by any other extent,
2571                  * save the partial cluster here, since we might need to
2572                  * delete if we determine that the truncate or punch hole
2573                  * operation has removed all of the blocks in the cluster.
2574                  * If that cluster is used by another extent, preserve its
2575                  * negative value so it isn't freed later on.
2576                  *
2577                  * If the whole extent wasn't freed, we've reached the
2578                  * start of the truncated/punched region and have finished
2579                  * removing blocks.  If there's a partial cluster here it's
2580                  * shared with the remainder of the extent and is no longer
2581                  * a candidate for removal.
2582                  */
2583                 if (EXT4_PBLK_COFF(sbi, pblk) && ee_len == num) {
2584                         first_cluster = (long long) EXT4_B2C(sbi, pblk);
2585                         if (first_cluster != -*partial_cluster)
2586                                 *partial_cluster = first_cluster;
2587                 } else {
2588                         *partial_cluster = 0;
2589                 }
2590         } else
2591                 ext4_error(sbi->s_sb, "strange request: removal(2) "
2592                            "%u-%u from %u:%u",
2593                            from, to, le32_to_cpu(ex->ee_block), ee_len);
2594         return 0;
2595 }
2596
2597
2598 /*
2599  * ext4_ext_rm_leaf() Removes the extents associated with the
2600  * blocks appearing between "start" and "end".  Both "start"
2601  * and "end" must appear in the same extent or EIO is returned.
2602  *
2603  * @handle: The journal handle
2604  * @inode:  The files inode
2605  * @path:   The path to the leaf
2606  * @partial_cluster: The cluster which we'll have to free if all extents
2607  *                   has been released from it.  However, if this value is
2608  *                   negative, it's a cluster just to the right of the
2609  *                   punched region and it must not be freed.
2610  * @start:  The first block to remove
2611  * @end:   The last block to remove
2612  */
2613 static int
2614 ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
2615                  struct ext4_ext_path *path,
2616                  long long *partial_cluster,
2617                  ext4_lblk_t start, ext4_lblk_t end)
2618 {
2619         struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2620         int err = 0, correct_index = 0;
2621         int depth = ext_depth(inode), credits;
2622         struct ext4_extent_header *eh;
2623         ext4_lblk_t a, b;
2624         unsigned num;
2625         ext4_lblk_t ex_ee_block;
2626         unsigned short ex_ee_len;
2627         unsigned unwritten = 0;
2628         struct ext4_extent *ex;
2629         ext4_fsblk_t pblk;
2630
2631         /* the header must be checked already in ext4_ext_remove_space() */
2632         ext_debug("truncate since %u in leaf to %u\n", start, end);
2633         if (!path[depth].p_hdr)
2634                 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2635         eh = path[depth].p_hdr;
2636         if (unlikely(path[depth].p_hdr == NULL)) {
2637                 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2638                 return -EFSCORRUPTED;
2639         }
2640         /* find where to start removing */
2641         ex = path[depth].p_ext;
2642         if (!ex)
2643                 ex = EXT_LAST_EXTENT(eh);
2644
2645         ex_ee_block = le32_to_cpu(ex->ee_block);
2646         ex_ee_len = ext4_ext_get_actual_len(ex);
2647
2648         trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster);
2649
2650         while (ex >= EXT_FIRST_EXTENT(eh) &&
2651                         ex_ee_block + ex_ee_len > start) {
2652
2653                 if (ext4_ext_is_unwritten(ex))
2654                         unwritten = 1;
2655                 else
2656                         unwritten = 0;
2657
2658                 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2659                           unwritten, ex_ee_len);
2660                 path[depth].p_ext = ex;
2661
2662                 a = ex_ee_block > start ? ex_ee_block : start;
2663                 b = ex_ee_block+ex_ee_len - 1 < end ?
2664                         ex_ee_block+ex_ee_len - 1 : end;
2665
2666                 ext_debug("  border %u:%u\n", a, b);
2667
2668                 /* If this extent is beyond the end of the hole, skip it */
2669                 if (end < ex_ee_block) {
2670                         /*
2671                          * We're going to skip this extent and move to another,
2672                          * so note that its first cluster is in use to avoid
2673                          * freeing it when removing blocks.  Eventually, the
2674                          * right edge of the truncated/punched region will
2675                          * be just to the left.
2676                          */
2677                         if (sbi->s_cluster_ratio > 1) {
2678                                 pblk = ext4_ext_pblock(ex);
2679                                 *partial_cluster =
2680                                         -(long long) EXT4_B2C(sbi, pblk);
2681                         }
2682                         ex--;
2683                         ex_ee_block = le32_to_cpu(ex->ee_block);
2684                         ex_ee_len = ext4_ext_get_actual_len(ex);
2685                         continue;
2686                 } else if (b != ex_ee_block + ex_ee_len - 1) {
2687                         EXT4_ERROR_INODE(inode,
2688                                          "can not handle truncate %u:%u "
2689                                          "on extent %u:%u",
2690                                          start, end, ex_ee_block,
2691                                          ex_ee_block + ex_ee_len - 1);
2692                         err = -EFSCORRUPTED;
2693                         goto out;
2694                 } else if (a != ex_ee_block) {
2695                         /* remove tail of the extent */
2696                         num = a - ex_ee_block;
2697                 } else {
2698                         /* remove whole extent: excellent! */
2699                         num = 0;
2700                 }
2701                 /*
2702                  * 3 for leaf, sb, and inode plus 2 (bmap and group
2703                  * descriptor) for each block group; assume two block
2704                  * groups plus ex_ee_len/blocks_per_block_group for
2705                  * the worst case
2706                  */
2707                 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
2708                 if (ex == EXT_FIRST_EXTENT(eh)) {
2709                         correct_index = 1;
2710                         credits += (ext_depth(inode)) + 1;
2711                 }
2712                 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
2713
2714                 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
2715                 if (err)
2716                         goto out;
2717
2718                 err = ext4_ext_get_access(handle, inode, path + depth);
2719                 if (err)
2720                         goto out;
2721
2722                 err = ext4_remove_blocks(handle, inode, ex, partial_cluster,
2723                                          a, b);
2724                 if (err)
2725                         goto out;
2726
2727                 if (num == 0)
2728                         /* this extent is removed; mark slot entirely unused */
2729                         ext4_ext_store_pblock(ex, 0);
2730
2731                 ex->ee_len = cpu_to_le16(num);
2732                 /*
2733                  * Do not mark unwritten if all the blocks in the
2734                  * extent have been removed.
2735                  */
2736                 if (unwritten && num)
2737                         ext4_ext_mark_unwritten(ex);
2738                 /*
2739                  * If the extent was completely released,
2740                  * we need to remove it from the leaf
2741                  */
2742                 if (num == 0) {
2743                         if (end != EXT_MAX_BLOCKS - 1) {
2744                                 /*
2745                                  * For hole punching, we need to scoot all the
2746                                  * extents up when an extent is removed so that
2747                                  * we dont have blank extents in the middle
2748                                  */
2749                                 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2750                                         sizeof(struct ext4_extent));
2751
2752                                 /* Now get rid of the one at the end */
2753                                 memset(EXT_LAST_EXTENT(eh), 0,
2754                                         sizeof(struct ext4_extent));
2755                         }
2756                         le16_add_cpu(&eh->eh_entries, -1);
2757                 }
2758
2759                 err = ext4_ext_dirty(handle, inode, path + depth);
2760                 if (err)
2761                         goto out;
2762
2763                 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
2764                                 ext4_ext_pblock(ex));
2765                 ex--;
2766                 ex_ee_block = le32_to_cpu(ex->ee_block);
2767                 ex_ee_len = ext4_ext_get_actual_len(ex);
2768         }
2769
2770         if (correct_index && eh->eh_entries)
2771                 err = ext4_ext_correct_indexes(handle, inode, path);
2772
2773         /*
2774          * If there's a partial cluster and at least one extent remains in
2775          * the leaf, free the partial cluster if it isn't shared with the
2776          * current extent.  If it is shared with the current extent
2777          * we zero partial_cluster because we've reached the start of the
2778          * truncated/punched region and we're done removing blocks.
2779          */
2780         if (*partial_cluster > 0 && ex >= EXT_FIRST_EXTENT(eh)) {
2781                 pblk = ext4_ext_pblock(ex) + ex_ee_len - 1;
2782                 if (*partial_cluster != (long long) EXT4_B2C(sbi, pblk)) {
2783                         ext4_free_blocks(handle, inode, NULL,
2784                                          EXT4_C2B(sbi, *partial_cluster),
2785                                          sbi->s_cluster_ratio,
2786                                          get_default_free_blocks_flags(inode));
2787                 }
2788                 *partial_cluster = 0;
2789         }
2790
2791         /* if this leaf is free, then we should
2792          * remove it from index block above */
2793         if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2794                 err = ext4_ext_rm_idx(handle, inode, path, depth);
2795
2796 out:
2797         return err;
2798 }
2799
2800 /*
2801  * ext4_ext_more_to_rm:
2802  * returns 1 if current index has to be freed (even partial)
2803  */
2804 static int
2805 ext4_ext_more_to_rm(struct ext4_ext_path *path)
2806 {
2807         BUG_ON(path->p_idx == NULL);
2808
2809         if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2810                 return 0;
2811
2812         /*
2813          * if truncate on deeper level happened, it wasn't partial,
2814          * so we have to consider current index for truncation
2815          */
2816         if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2817                 return 0;
2818         return 1;
2819 }
2820
2821 int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2822                           ext4_lblk_t end)
2823 {
2824         struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2825         int depth = ext_depth(inode);
2826         struct ext4_ext_path *path = NULL;
2827         long long partial_cluster = 0;
2828         handle_t *handle;
2829         int i = 0, err = 0;
2830
2831         ext_debug("truncate since %u to %u\n", start, end);
2832
2833         /* probably first extent we're gonna free will be last in block */
2834         handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, depth + 1);
2835         if (IS_ERR(handle))
2836                 return PTR_ERR(handle);
2837
2838 again:
2839         trace_ext4_ext_remove_space(inode, start, end, depth);
2840
2841         /*
2842          * Check if we are removing extents inside the extent tree. If that
2843          * is the case, we are going to punch a hole inside the extent tree
2844          * so we have to check whether we need to split the extent covering
2845          * the last block to remove so we can easily remove the part of it
2846          * in ext4_ext_rm_leaf().
2847          */
2848         if (end < EXT_MAX_BLOCKS - 1) {
2849                 struct ext4_extent *ex;
2850                 ext4_lblk_t ee_block, ex_end, lblk;
2851                 ext4_fsblk_t pblk;
2852
2853                 /* find extent for or closest extent to this block */
2854                 path = ext4_find_extent(inode, end, NULL, EXT4_EX_NOCACHE);
2855                 if (IS_ERR(path)) {
2856                         ext4_journal_stop(handle);
2857                         return PTR_ERR(path);
2858                 }
2859                 depth = ext_depth(inode);
2860                 /* Leaf not may not exist only if inode has no blocks at all */
2861                 ex = path[depth].p_ext;
2862                 if (!ex) {
2863                         if (depth) {
2864                                 EXT4_ERROR_INODE(inode,
2865                                                  "path[%d].p_hdr == NULL",
2866                                                  depth);
2867                                 err = -EFSCORRUPTED;
2868                         }
2869                         goto out;
2870                 }
2871
2872                 ee_block = le32_to_cpu(ex->ee_block);
2873                 ex_end = ee_block + ext4_ext_get_actual_len(ex) - 1;
2874
2875                 /*
2876                  * See if the last block is inside the extent, if so split
2877                  * the extent at 'end' block so we can easily remove the
2878                  * tail of the first part of the split extent in
2879                  * ext4_ext_rm_leaf().
2880                  */
2881                 if (end >= ee_block && end < ex_end) {
2882
2883                         /*
2884                          * If we're going to split the extent, note that
2885                          * the cluster containing the block after 'end' is
2886                          * in use to avoid freeing it when removing blocks.
2887                          */
2888                         if (sbi->s_cluster_ratio > 1) {
2889                                 pblk = ext4_ext_pblock(ex) + end - ee_block + 2;
2890                                 partial_cluster =
2891                                         -(long long) EXT4_B2C(sbi, pblk);
2892                         }
2893
2894                         /*
2895                          * Split the extent in two so that 'end' is the last
2896                          * block in the first new extent. Also we should not
2897                          * fail removing space due to ENOSPC so try to use
2898                          * reserved block if that happens.
2899                          */
2900                         err = ext4_force_split_extent_at(handle, inode, &path,
2901                                                          end + 1, 1);
2902                         if (err < 0)
2903                                 goto out;
2904
2905                 } else if (sbi->s_cluster_ratio > 1 && end >= ex_end) {
2906                         /*
2907                          * If there's an extent to the right its first cluster
2908                          * contains the immediate right boundary of the
2909                          * truncated/punched region.  Set partial_cluster to
2910                          * its negative value so it won't be freed if shared
2911                          * with the current extent.  The end < ee_block case
2912                          * is handled in ext4_ext_rm_leaf().
2913                          */
2914                         lblk = ex_end + 1;
2915                         err = ext4_ext_search_right(inode, path, &lblk, &pblk,
2916                                                     &ex);
2917                         if (err)
2918                                 goto out;
2919                         if (pblk)
2920                                 partial_cluster =
2921                                         -(long long) EXT4_B2C(sbi, pblk);
2922                 }
2923         }
2924         /*
2925          * We start scanning from right side, freeing all the blocks
2926          * after i_size and walking into the tree depth-wise.
2927          */
2928         depth = ext_depth(inode);
2929         if (path) {
2930                 int k = i = depth;
2931                 while (--k > 0)
2932                         path[k].p_block =
2933                                 le16_to_cpu(path[k].p_hdr->eh_entries)+1;
2934         } else {
2935                 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1),
2936                                GFP_NOFS);
2937                 if (path == NULL) {
2938                         ext4_journal_stop(handle);
2939                         return -ENOMEM;
2940                 }
2941                 path[0].p_maxdepth = path[0].p_depth = depth;
2942                 path[0].p_hdr = ext_inode_hdr(inode);
2943                 i = 0;
2944
2945                 if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) {
2946                         err = -EFSCORRUPTED;
2947                         goto out;
2948                 }
2949         }
2950         err = 0;
2951
2952         while (i >= 0 && err == 0) {
2953                 if (i == depth) {
2954                         /* this is leaf block */
2955                         err = ext4_ext_rm_leaf(handle, inode, path,
2956                                                &partial_cluster, start,
2957                                                end);
2958                         /* root level has p_bh == NULL, brelse() eats this */
2959                         brelse(path[i].p_bh);
2960                         path[i].p_bh = NULL;
2961                         i--;
2962                         continue;
2963                 }
2964
2965                 /* this is index block */
2966                 if (!path[i].p_hdr) {
2967                         ext_debug("initialize header\n");
2968                         path[i].p_hdr = ext_block_hdr(path[i].p_bh);
2969                 }
2970
2971                 if (!path[i].p_idx) {
2972                         /* this level hasn't been touched yet */
2973                         path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2974                         path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2975                         ext_debug("init index ptr: hdr 0x%p, num %d\n",
2976                                   path[i].p_hdr,
2977                                   le16_to_cpu(path[i].p_hdr->eh_entries));
2978                 } else {
2979                         /* we were already here, see at next index */
2980                         path[i].p_idx--;
2981                 }
2982
2983                 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2984                                 i, EXT_FIRST_INDEX(path[i].p_hdr),
2985                                 path[i].p_idx);
2986                 if (ext4_ext_more_to_rm(path + i)) {
2987                         struct buffer_head *bh;
2988                         /* go to the next level */
2989                         ext_debug("move to level %d (block %llu)\n",
2990                                   i + 1, ext4_idx_pblock(path[i].p_idx));
2991                         memset(path + i + 1, 0, sizeof(*path));
2992                         bh = read_extent_tree_block(inode,
2993                                 ext4_idx_pblock(path[i].p_idx), depth - i - 1,
2994                                 EXT4_EX_NOCACHE);
2995                         if (IS_ERR(bh)) {
2996                                 /* should we reset i_size? */
2997                                 err = PTR_ERR(bh);
2998                                 break;
2999                         }
3000                         /* Yield here to deal with large extent trees.
3001                          * Should be a no-op if we did IO above. */
3002                         cond_resched();
3003                         if (WARN_ON(i + 1 > depth)) {
3004                                 err = -EFSCORRUPTED;
3005                                 break;
3006                         }
3007                         path[i + 1].p_bh = bh;
3008
3009                         /* save actual number of indexes since this
3010                          * number is changed at the next iteration */
3011                         path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
3012                         i++;
3013                 } else {
3014                         /* we finished processing this index, go up */
3015                         if (path[i].p_hdr->eh_entries == 0 && i > 0) {
3016                                 /* index is empty, remove it;
3017                                  * handle must be already prepared by the
3018                                  * truncatei_leaf() */
3019                                 err = ext4_ext_rm_idx(handle, inode, path, i);
3020                         }
3021                         /* root level has p_bh == NULL, brelse() eats this */
3022                         brelse(path[i].p_bh);
3023                         path[i].p_bh = NULL;
3024                         i--;
3025                         ext_debug("return to level %d\n", i);
3026                 }
3027         }
3028
3029         trace_ext4_ext_remove_space_done(inode, start, end, depth,
3030                         partial_cluster, path->p_hdr->eh_entries);
3031
3032         /*
3033          * If we still have something in the partial cluster and we have removed
3034          * even the first extent, then we should free the blocks in the partial
3035          * cluster as well.  (This code will only run when there are no leaves
3036          * to the immediate left of the truncated/punched region.)
3037          */
3038         if (partial_cluster > 0 && err == 0) {
3039                 /* don't zero partial_cluster since it's not used afterwards */
3040                 ext4_free_blocks(handle, inode, NULL,
3041                                  EXT4_C2B(sbi, partial_cluster),
3042                                  sbi->s_cluster_ratio,
3043                                  get_default_free_blocks_flags(inode));
3044         }
3045
3046         /* TODO: flexible tree reduction should be here */
3047         if (path->p_hdr->eh_entries == 0) {
3048                 /*
3049                  * truncate to zero freed all the tree,
3050                  * so we need to correct eh_depth
3051                  */
3052                 err = ext4_ext_get_access(handle, inode, path);
3053                 if (err == 0) {
3054                         ext_inode_hdr(inode)->eh_depth = 0;
3055                         ext_inode_hdr(inode)->eh_max =
3056                                 cpu_to_le16(ext4_ext_space_root(inode, 0));
3057                         err = ext4_ext_dirty(handle, inode, path);
3058                 }
3059         }
3060 out:
3061         ext4_ext_drop_refs(path);
3062         kfree(path);
3063         path = NULL;
3064         if (err == -EAGAIN)
3065                 goto again;
3066         ext4_journal_stop(handle);
3067
3068         return err;
3069 }
3070
3071 /*
3072  * called at mount time
3073  */
3074 void ext4_ext_init(struct super_block *sb)
3075 {
3076         /*
3077          * possible initialization would be here
3078          */
3079
3080         if (ext4_has_feature_extents(sb)) {
3081 #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
3082                 printk(KERN_INFO "EXT4-fs: file extents enabled"
3083 #ifdef AGGRESSIVE_TEST
3084                        ", aggressive tests"
3085 #endif
3086 #ifdef CHECK_BINSEARCH
3087                        ", check binsearch"
3088 #endif
3089 #ifdef EXTENTS_STATS
3090                        ", stats"
3091 #endif
3092                        "\n");
3093 #endif
3094 #ifdef EXTENTS_STATS
3095                 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
3096                 EXT4_SB(sb)->s_ext_min = 1 << 30;
3097                 EXT4_SB(sb)->s_ext_max = 0;
3098 #endif