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