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