1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <linux/fs.h>
21#include <linux/time.h>
22#include <linux/jbd2.h>
23#include <linux/highuid.h>
24#include <linux/pagemap.h>
25#include <linux/quotaops.h>
26#include <linux/string.h>
27#include <linux/slab.h>
28#include <linux/uaccess.h>
29#include <linux/fiemap.h>
30#include <linux/backing-dev.h>
31#include "ext4_jbd2.h"
32#include "ext4_extents.h"
33#include "xattr.h"
34
35#include <trace/events/ext4.h>
36
37
38
39
40#define EXT4_EXT_MAY_ZEROOUT 0x1
41
42#define EXT4_EXT_MARK_UNWRIT1 0x2
43#define EXT4_EXT_MARK_UNWRIT2 0x4
44
45#define EXT4_EXT_DATA_VALID1 0x8
46#define EXT4_EXT_DATA_VALID2 0x10
47
48static __le32 ext4_extent_block_csum(struct inode *inode,
49 struct ext4_extent_header *eh)
50{
51 struct ext4_inode_info *ei = EXT4_I(inode);
52 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
53 __u32 csum;
54
55 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)eh,
56 EXT4_EXTENT_TAIL_OFFSET(eh));
57 return cpu_to_le32(csum);
58}
59
60static int ext4_extent_block_csum_verify(struct inode *inode,
61 struct ext4_extent_header *eh)
62{
63 struct ext4_extent_tail *et;
64
65 if (!ext4_has_metadata_csum(inode->i_sb))
66 return 1;
67
68 et = find_ext4_extent_tail(eh);
69 if (et->et_checksum != ext4_extent_block_csum(inode, eh))
70 return 0;
71 return 1;
72}
73
74static void ext4_extent_block_csum_set(struct inode *inode,
75 struct ext4_extent_header *eh)
76{
77 struct ext4_extent_tail *et;
78
79 if (!ext4_has_metadata_csum(inode->i_sb))
80 return;
81
82 et = find_ext4_extent_tail(eh);
83 et->et_checksum = ext4_extent_block_csum(inode, eh);
84}
85
86static int ext4_split_extent(handle_t *handle,
87 struct inode *inode,
88 struct ext4_ext_path **ppath,
89 struct ext4_map_blocks *map,
90 int split_flag,
91 int flags);
92
93static int ext4_split_extent_at(handle_t *handle,
94 struct inode *inode,
95 struct ext4_ext_path **ppath,
96 ext4_lblk_t split,
97 int split_flag,
98 int flags);
99
100static int ext4_find_delayed_extent(struct inode *inode,
101 struct extent_status *newes);
102
103static int ext4_ext_trunc_restart_fn(struct inode *inode, int *dropped)
104{
105
106
107
108
109
110
111 BUG_ON(EXT4_JOURNAL(inode) == NULL);
112 ext4_discard_preallocations(inode);
113 up_write(&EXT4_I(inode)->i_data_sem);
114 *dropped = 1;
115 return 0;
116}
117
118
119
120
121
122
123
124
125
126int ext4_datasem_ensure_credits(handle_t *handle, struct inode *inode,
127 int check_cred, int restart_cred,
128 int revoke_cred)
129{
130 int ret;
131 int dropped = 0;
132
133 ret = ext4_journal_ensure_credits_fn(handle, check_cred, restart_cred,
134 revoke_cred, ext4_ext_trunc_restart_fn(inode, &dropped));
135 if (dropped)
136 down_write(&EXT4_I(inode)->i_data_sem);
137 return ret;
138}
139
140
141
142
143
144
145static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
146 struct ext4_ext_path *path)
147{
148 if (path->p_bh) {
149
150 BUFFER_TRACE(path->p_bh, "get_write_access");
151 return ext4_journal_get_write_access(handle, path->p_bh);
152 }
153
154
155 return 0;
156}
157
158
159
160
161
162
163
164int __ext4_ext_dirty(const char *where, unsigned int line, handle_t *handle,
165 struct inode *inode, struct ext4_ext_path *path)
166{
167 int err;
168
169 WARN_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem));
170 if (path->p_bh) {
171 ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh));
172
173 err = __ext4_handle_dirty_metadata(where, line, handle,
174 inode, path->p_bh);
175 } else {
176
177 err = ext4_mark_inode_dirty(handle, inode);
178 }
179 return err;
180}
181
182static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
183 struct ext4_ext_path *path,
184 ext4_lblk_t block)
185{
186 if (path) {
187 int depth = path->p_depth;
188 struct ext4_extent *ex;
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207 ex = path[depth].p_ext;
208 if (ex) {
209 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
210 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
211
212 if (block > ext_block)
213 return ext_pblk + (block - ext_block);
214 else
215 return ext_pblk - (ext_block - block);
216 }
217
218
219
220 if (path[depth].p_bh)
221 return path[depth].p_bh->b_blocknr;
222 }
223
224
225 return ext4_inode_to_goal_block(inode);
226}
227
228
229
230
231static ext4_fsblk_t
232ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
233 struct ext4_ext_path *path,
234 struct ext4_extent *ex, int *err, unsigned int flags)
235{
236 ext4_fsblk_t goal, newblock;
237
238 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
239 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
240 NULL, err);
241 return newblock;
242}
243
244static inline int ext4_ext_space_block(struct inode *inode, int check)
245{
246 int size;
247
248 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
249 / sizeof(struct ext4_extent);
250#ifdef AGGRESSIVE_TEST
251 if (!check && size > 6)
252 size = 6;
253#endif
254 return size;
255}
256
257static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
258{
259 int size;
260
261 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
262 / sizeof(struct ext4_extent_idx);
263#ifdef AGGRESSIVE_TEST
264 if (!check && size > 5)
265 size = 5;
266#endif
267 return size;
268}
269
270static inline int ext4_ext_space_root(struct inode *inode, int check)
271{
272 int size;
273
274 size = sizeof(EXT4_I(inode)->i_data);
275 size -= sizeof(struct ext4_extent_header);
276 size /= sizeof(struct ext4_extent);
277#ifdef AGGRESSIVE_TEST
278 if (!check && size > 3)
279 size = 3;
280#endif
281 return size;
282}
283
284static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
285{
286 int size;
287
288 size = sizeof(EXT4_I(inode)->i_data);
289 size -= sizeof(struct ext4_extent_header);
290 size /= sizeof(struct ext4_extent_idx);
291#ifdef AGGRESSIVE_TEST
292 if (!check && size > 4)
293 size = 4;
294#endif
295 return size;
296}
297
298static inline int
299ext4_force_split_extent_at(handle_t *handle, struct inode *inode,
300 struct ext4_ext_path **ppath, ext4_lblk_t lblk,
301 int nofail)
302{
303 struct ext4_ext_path *path = *ppath;
304 int unwritten = ext4_ext_is_unwritten(path[path->p_depth].p_ext);
305
306 return ext4_split_extent_at(handle, inode, ppath, lblk, unwritten ?
307 EXT4_EXT_MARK_UNWRIT1|EXT4_EXT_MARK_UNWRIT2 : 0,
308 EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_PRE_IO |
309 (nofail ? EXT4_GET_BLOCKS_METADATA_NOFAIL:0));
310}
311
312
313
314
315
316
317int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
318{
319 struct ext4_inode_info *ei = EXT4_I(inode);
320 int idxs;
321
322 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
323 / sizeof(struct ext4_extent_idx));
324
325
326
327
328
329
330
331
332
333 if (ei->i_da_metadata_calc_len &&
334 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
335 int num = 0;
336
337 if ((ei->i_da_metadata_calc_len % idxs) == 0)
338 num++;
339 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
340 num++;
341 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
342 num++;
343 ei->i_da_metadata_calc_len = 0;
344 } else
345 ei->i_da_metadata_calc_len++;
346 ei->i_da_metadata_calc_last_lblock++;
347 return num;
348 }
349
350
351
352
353
354 ei->i_da_metadata_calc_len = 1;
355 ei->i_da_metadata_calc_last_lblock = lblock;
356 return ext_depth(inode) + 1;
357}
358
359static int
360ext4_ext_max_entries(struct inode *inode, int depth)
361{
362 int max;
363
364 if (depth == ext_depth(inode)) {
365 if (depth == 0)
366 max = ext4_ext_space_root(inode, 1);
367 else
368 max = ext4_ext_space_root_idx(inode, 1);
369 } else {
370 if (depth == 0)
371 max = ext4_ext_space_block(inode, 1);
372 else
373 max = ext4_ext_space_block_idx(inode, 1);
374 }
375
376 return max;
377}
378
379static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
380{
381 ext4_fsblk_t block = ext4_ext_pblock(ext);
382 int len = ext4_ext_get_actual_len(ext);
383 ext4_lblk_t lblock = le32_to_cpu(ext->ee_block);
384
385
386
387
388
389
390 if (lblock + len <= lblock)
391 return 0;
392 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
393}
394
395static int ext4_valid_extent_idx(struct inode *inode,
396 struct ext4_extent_idx *ext_idx)
397{
398 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
399
400 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
401}
402
403static int ext4_valid_extent_entries(struct inode *inode,
404 struct ext4_extent_header *eh,
405 int depth)
406{
407 unsigned short entries;
408 if (eh->eh_entries == 0)
409 return 1;
410
411 entries = le16_to_cpu(eh->eh_entries);
412
413 if (depth == 0) {
414
415 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
416 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
417 ext4_fsblk_t pblock = 0;
418 ext4_lblk_t lblock = 0;
419 ext4_lblk_t prev = 0;
420 int len = 0;
421 while (entries) {
422 if (!ext4_valid_extent(inode, ext))
423 return 0;
424
425
426 lblock = le32_to_cpu(ext->ee_block);
427 len = ext4_ext_get_actual_len(ext);
428 if ((lblock <= prev) && prev) {
429 pblock = ext4_ext_pblock(ext);
430 es->s_last_error_block = cpu_to_le64(pblock);
431 return 0;
432 }
433 ext++;
434 entries--;
435 prev = lblock + len - 1;
436 }
437 } else {
438 struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
439 while (entries) {
440 if (!ext4_valid_extent_idx(inode, ext_idx))
441 return 0;
442 ext_idx++;
443 entries--;
444 }
445 }
446 return 1;
447}
448
449static int __ext4_ext_check(const char *function, unsigned int line,
450 struct inode *inode, struct ext4_extent_header *eh,
451 int depth, ext4_fsblk_t pblk)
452{
453 const char *error_msg;
454 int max = 0, err = -EFSCORRUPTED;
455
456 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
457 error_msg = "invalid magic";
458 goto corrupted;
459 }
460 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
461 error_msg = "unexpected eh_depth";
462 goto corrupted;
463 }
464 if (unlikely(eh->eh_max == 0)) {
465 error_msg = "invalid eh_max";
466 goto corrupted;
467 }
468 max = ext4_ext_max_entries(inode, depth);
469 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
470 error_msg = "too large eh_max";
471 goto corrupted;
472 }
473 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
474 error_msg = "invalid eh_entries";
475 goto corrupted;
476 }
477 if (!ext4_valid_extent_entries(inode, eh, depth)) {
478 error_msg = "invalid extent entries";
479 goto corrupted;
480 }
481 if (unlikely(depth > 32)) {
482 error_msg = "too large eh_depth";
483 goto corrupted;
484 }
485
486 if (ext_depth(inode) != depth &&
487 !ext4_extent_block_csum_verify(inode, eh)) {
488 error_msg = "extent tree corrupted";
489 err = -EFSBADCRC;
490 goto corrupted;
491 }
492 return 0;
493
494corrupted:
495 ext4_error_inode(inode, function, line, 0,
496 "pblk %llu bad header/extent: %s - magic %x, "
497 "entries %u, max %u(%u), depth %u(%u)",
498 (unsigned long long) pblk, error_msg,
499 le16_to_cpu(eh->eh_magic),
500 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
501 max, le16_to_cpu(eh->eh_depth), depth);
502 return err;
503}
504
505#define ext4_ext_check(inode, eh, depth, pblk) \
506 __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk))
507
508int ext4_ext_check_inode(struct inode *inode)
509{
510 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode), 0);
511}
512
513static struct buffer_head *
514__read_extent_tree_block(const char *function, unsigned int line,
515 struct inode *inode, ext4_fsblk_t pblk, int depth,
516 int flags)
517{
518 struct buffer_head *bh;
519 int err;
520
521 bh = sb_getblk_gfp(inode->i_sb, pblk, __GFP_MOVABLE | GFP_NOFS);
522 if (unlikely(!bh))
523 return ERR_PTR(-ENOMEM);
524
525 if (!bh_uptodate_or_lock(bh)) {
526 trace_ext4_ext_load_extent(inode, pblk, _RET_IP_);
527 err = bh_submit_read(bh);
528 if (err < 0)
529 goto errout;
530 }
531 if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE))
532 return bh;
533 if (!ext4_has_feature_journal(inode->i_sb) ||
534 (inode->i_ino !=
535 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum))) {
536 err = __ext4_ext_check(function, line, inode,
537 ext_block_hdr(bh), depth, pblk);
538 if (err)
539 goto errout;
540 }
541 set_buffer_verified(bh);
542
543
544
545 if (!(flags & EXT4_EX_NOCACHE) && depth == 0) {
546 struct ext4_extent_header *eh = ext_block_hdr(bh);
547 struct ext4_extent *ex = EXT_FIRST_EXTENT(eh);
548 ext4_lblk_t prev = 0;
549 int i;
550
551 for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) {
552 unsigned int status = EXTENT_STATUS_WRITTEN;
553 ext4_lblk_t lblk = le32_to_cpu(ex->ee_block);
554 int len = ext4_ext_get_actual_len(ex);
555
556 if (prev && (prev != lblk))
557 ext4_es_cache_extent(inode, prev,
558 lblk - prev, ~0,
559 EXTENT_STATUS_HOLE);
560
561 if (ext4_ext_is_unwritten(ex))
562 status = EXTENT_STATUS_UNWRITTEN;
563 ext4_es_cache_extent(inode, lblk, len,
564 ext4_ext_pblock(ex), status);
565 prev = lblk + len;
566 }
567 }
568 return bh;
569errout:
570 put_bh(bh);
571 return ERR_PTR(err);
572
573}
574
575#define read_extent_tree_block(inode, pblk, depth, flags) \
576 __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \
577 (depth), (flags))
578
579
580
581
582
583int ext4_ext_precache(struct inode *inode)
584{
585 struct ext4_inode_info *ei = EXT4_I(inode);
586 struct ext4_ext_path *path = NULL;
587 struct buffer_head *bh;
588 int i = 0, depth, ret = 0;
589
590 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
591 return 0;
592
593 down_read(&ei->i_data_sem);
594 depth = ext_depth(inode);
595
596 path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
597 GFP_NOFS);
598 if (path == NULL) {
599 up_read(&ei->i_data_sem);
600 return -ENOMEM;
601 }
602
603
604 if (depth == 0)
605 goto out;
606 path[0].p_hdr = ext_inode_hdr(inode);
607 ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0);
608 if (ret)
609 goto out;
610 path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr);
611 while (i >= 0) {
612
613
614
615
616 if ((i == depth) ||
617 path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) {
618 brelse(path[i].p_bh);
619 path[i].p_bh = NULL;
620 i--;
621 continue;
622 }
623 bh = read_extent_tree_block(inode,
624 ext4_idx_pblock(path[i].p_idx++),
625 depth - i - 1,
626 EXT4_EX_FORCE_CACHE);
627 if (IS_ERR(bh)) {
628 ret = PTR_ERR(bh);
629 break;
630 }
631 i++;
632 path[i].p_bh = bh;
633 path[i].p_hdr = ext_block_hdr(bh);
634 path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr);
635 }
636 ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
637out:
638 up_read(&ei->i_data_sem);
639 ext4_ext_drop_refs(path);
640 kfree(path);
641 return ret;
642}
643
644#ifdef EXT_DEBUG
645static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
646{
647 int k, l = path->p_depth;
648
649 ext_debug("path:");
650 for (k = 0; k <= l; k++, path++) {
651 if (path->p_idx) {
652 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
653 ext4_idx_pblock(path->p_idx));
654 } else if (path->p_ext) {
655 ext_debug(" %d:[%d]%d:%llu ",
656 le32_to_cpu(path->p_ext->ee_block),
657 ext4_ext_is_unwritten(path->p_ext),
658 ext4_ext_get_actual_len(path->p_ext),
659 ext4_ext_pblock(path->p_ext));
660 } else
661 ext_debug(" []");
662 }
663 ext_debug("\n");
664}
665
666static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
667{
668 int depth = ext_depth(inode);
669 struct ext4_extent_header *eh;
670 struct ext4_extent *ex;
671 int i;
672
673 if (!path)
674 return;
675
676 eh = path[depth].p_hdr;
677 ex = EXT_FIRST_EXTENT(eh);
678
679 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
680
681 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
682 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
683 ext4_ext_is_unwritten(ex),
684 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
685 }
686 ext_debug("\n");
687}
688
689static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
690 ext4_fsblk_t newblock, int level)
691{
692 int depth = ext_depth(inode);
693 struct ext4_extent *ex;
694
695 if (depth != level) {
696 struct ext4_extent_idx *idx;
697 idx = path[level].p_idx;
698 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
699 ext_debug("%d: move %d:%llu in new index %llu\n", level,
700 le32_to_cpu(idx->ei_block),
701 ext4_idx_pblock(idx),
702 newblock);
703 idx++;
704 }
705
706 return;
707 }
708
709 ex = path[depth].p_ext;
710 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
711 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
712 le32_to_cpu(ex->ee_block),
713 ext4_ext_pblock(ex),
714 ext4_ext_is_unwritten(ex),
715 ext4_ext_get_actual_len(ex),
716 newblock);
717 ex++;
718 }
719}
720
721#else
722#define ext4_ext_show_path(inode, path)
723#define ext4_ext_show_leaf(inode, path)
724#define ext4_ext_show_move(inode, path, newblock, level)
725#endif
726
727void ext4_ext_drop_refs(struct ext4_ext_path *path)
728{
729 int depth, i;
730
731 if (!path)
732 return;
733 depth = path->p_depth;
734 for (i = 0; i <= depth; i++, path++)
735 if (path->p_bh) {
736 brelse(path->p_bh);
737 path->p_bh = NULL;
738 }
739}
740
741
742
743
744
745
746static void
747ext4_ext_binsearch_idx(struct inode *inode,
748 struct ext4_ext_path *path, ext4_lblk_t block)
749{
750 struct ext4_extent_header *eh = path->p_hdr;
751 struct ext4_extent_idx *r, *l, *m;
752
753
754 ext_debug("binsearch for %u(idx): ", block);
755
756 l = EXT_FIRST_INDEX(eh) + 1;
757 r = EXT_LAST_INDEX(eh);
758 while (l <= r) {
759 m = l + (r - l) / 2;
760 if (block < le32_to_cpu(m->ei_block))
761 r = m - 1;
762 else
763 l = m + 1;
764 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
765 m, le32_to_cpu(m->ei_block),
766 r, le32_to_cpu(r->ei_block));
767 }
768
769 path->p_idx = l - 1;
770 ext_debug(" -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block),
771 ext4_idx_pblock(path->p_idx));
772
773#ifdef CHECK_BINSEARCH
774 {
775 struct ext4_extent_idx *chix, *ix;
776 int k;
777
778 chix = ix = EXT_FIRST_INDEX(eh);
779 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
780 if (k != 0 &&
781 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
782 printk(KERN_DEBUG "k=%d, ix=0x%p, "
783 "first=0x%p\n", k,
784 ix, EXT_FIRST_INDEX(eh));
785 printk(KERN_DEBUG "%u <= %u\n",
786 le32_to_cpu(ix->ei_block),
787 le32_to_cpu(ix[-1].ei_block));
788 }
789 BUG_ON(k && le32_to_cpu(ix->ei_block)
790 <= le32_to_cpu(ix[-1].ei_block));
791 if (block < le32_to_cpu(ix->ei_block))
792 break;
793 chix = ix;
794 }
795 BUG_ON(chix != path->p_idx);
796 }
797#endif
798
799}
800
801
802
803
804
805
806static void
807ext4_ext_binsearch(struct inode *inode,
808 struct ext4_ext_path *path, ext4_lblk_t block)
809{
810 struct ext4_extent_header *eh = path->p_hdr;
811 struct ext4_extent *r, *l, *m;
812
813 if (eh->eh_entries == 0) {
814
815
816
817
818 return;
819 }
820
821 ext_debug("binsearch for %u: ", block);
822
823 l = EXT_FIRST_EXTENT(eh) + 1;
824 r = EXT_LAST_EXTENT(eh);
825
826 while (l <= r) {
827 m = l + (r - l) / 2;
828 if (block < le32_to_cpu(m->ee_block))
829 r = m - 1;
830 else
831 l = m + 1;
832 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
833 m, le32_to_cpu(m->ee_block),
834 r, le32_to_cpu(r->ee_block));
835 }
836
837 path->p_ext = l - 1;
838 ext_debug(" -> %d:%llu:[%d]%d ",
839 le32_to_cpu(path->p_ext->ee_block),
840 ext4_ext_pblock(path->p_ext),
841 ext4_ext_is_unwritten(path->p_ext),
842 ext4_ext_get_actual_len(path->p_ext));
843
844#ifdef CHECK_BINSEARCH
845 {
846 struct ext4_extent *chex, *ex;
847 int k;
848
849 chex = ex = EXT_FIRST_EXTENT(eh);
850 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
851 BUG_ON(k && le32_to_cpu(ex->ee_block)
852 <= le32_to_cpu(ex[-1].ee_block));
853 if (block < le32_to_cpu(ex->ee_block))
854 break;
855 chex = ex;
856 }
857 BUG_ON(chex != path->p_ext);
858 }
859#endif
860
861}
862
863int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
864{
865 struct ext4_extent_header *eh;
866
867 eh = ext_inode_hdr(inode);
868 eh->eh_depth = 0;
869 eh->eh_entries = 0;
870 eh->eh_magic = EXT4_EXT_MAGIC;
871 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
872 ext4_mark_inode_dirty(handle, inode);
873 return 0;
874}
875
876struct ext4_ext_path *
877ext4_find_extent(struct inode *inode, ext4_lblk_t block,
878 struct ext4_ext_path **orig_path, int flags)
879{
880 struct ext4_extent_header *eh;
881 struct buffer_head *bh;
882 struct ext4_ext_path *path = orig_path ? *orig_path : NULL;
883 short int depth, i, ppos = 0;
884 int ret;
885
886 eh = ext_inode_hdr(inode);
887 depth = ext_depth(inode);
888 if (depth < 0 || depth > EXT4_MAX_EXTENT_DEPTH) {
889 EXT4_ERROR_INODE(inode, "inode has invalid extent depth: %d",
890 depth);
891 ret = -EFSCORRUPTED;
892 goto err;
893 }
894
895 if (path) {
896 ext4_ext_drop_refs(path);
897 if (depth > path[0].p_maxdepth) {
898 kfree(path);
899 *orig_path = path = NULL;
900 }
901 }
902 if (!path) {
903
904 path = kcalloc(depth + 2, sizeof(struct ext4_ext_path),
905 GFP_NOFS);
906 if (unlikely(!path))
907 return ERR_PTR(-ENOMEM);
908 path[0].p_maxdepth = depth + 1;
909 }
910 path[0].p_hdr = eh;
911 path[0].p_bh = NULL;
912
913 i = depth;
914
915 while (i) {
916 ext_debug("depth %d: num %d, max %d\n",
917 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
918
919 ext4_ext_binsearch_idx(inode, path + ppos, block);
920 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
921 path[ppos].p_depth = i;
922 path[ppos].p_ext = NULL;
923
924 bh = read_extent_tree_block(inode, path[ppos].p_block, --i,
925 flags);
926 if (IS_ERR(bh)) {
927 ret = PTR_ERR(bh);
928 goto err;
929 }
930
931 eh = ext_block_hdr(bh);
932 ppos++;
933 path[ppos].p_bh = bh;
934 path[ppos].p_hdr = eh;
935 }
936
937 path[ppos].p_depth = i;
938 path[ppos].p_ext = NULL;
939 path[ppos].p_idx = NULL;
940
941
942 ext4_ext_binsearch(inode, path + ppos, block);
943
944 if (path[ppos].p_ext)
945 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
946
947 ext4_ext_show_path(inode, path);
948
949 return path;
950
951err:
952 ext4_ext_drop_refs(path);
953 kfree(path);
954 if (orig_path)
955 *orig_path = NULL;
956 return ERR_PTR(ret);
957}
958
959
960
961
962
963
964static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
965 struct ext4_ext_path *curp,
966 int logical, ext4_fsblk_t ptr)
967{
968 struct ext4_extent_idx *ix;
969 int len, err;
970
971 err = ext4_ext_get_access(handle, inode, curp);
972 if (err)
973 return err;
974
975 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
976 EXT4_ERROR_INODE(inode,
977 "logical %d == ei_block %d!",
978 logical, le32_to_cpu(curp->p_idx->ei_block));
979 return -EFSCORRUPTED;
980 }
981
982 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
983 >= le16_to_cpu(curp->p_hdr->eh_max))) {
984 EXT4_ERROR_INODE(inode,
985 "eh_entries %d >= eh_max %d!",
986 le16_to_cpu(curp->p_hdr->eh_entries),
987 le16_to_cpu(curp->p_hdr->eh_max));
988 return -EFSCORRUPTED;
989 }
990
991 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
992
993 ext_debug("insert new index %d after: %llu\n", logical, ptr);
994 ix = curp->p_idx + 1;
995 } else {
996
997 ext_debug("insert new index %d before: %llu\n", logical, ptr);
998 ix = curp->p_idx;
999 }
1000
1001 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
1002 BUG_ON(len < 0);
1003 if (len > 0) {
1004 ext_debug("insert new index %d: "
1005 "move %d indices from 0x%p to 0x%p\n",
1006 logical, len, ix, ix + 1);
1007 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
1008 }
1009
1010 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
1011 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
1012 return -EFSCORRUPTED;
1013 }
1014
1015 ix->ei_block = cpu_to_le32(logical);
1016 ext4_idx_store_pblock(ix, ptr);
1017 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
1018
1019 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
1020 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
1021 return -EFSCORRUPTED;
1022 }
1023
1024 err = ext4_ext_dirty(handle, inode, curp);
1025 ext4_std_error(inode->i_sb, err);
1026
1027 return err;
1028}
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040static int ext4_ext_split(handle_t *handle, struct inode *inode,
1041 unsigned int flags,
1042 struct ext4_ext_path *path,
1043 struct ext4_extent *newext, int at)
1044{
1045 struct buffer_head *bh = NULL;
1046 int depth = ext_depth(inode);
1047 struct ext4_extent_header *neh;
1048 struct ext4_extent_idx *fidx;
1049 int i = at, k, m, a;
1050 ext4_fsblk_t newblock, oldblock;
1051 __le32 border;
1052 ext4_fsblk_t *ablocks = NULL;
1053 int err = 0;
1054 size_t ext_size = 0;
1055
1056
1057
1058
1059
1060
1061 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
1062 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
1063 return -EFSCORRUPTED;
1064 }
1065 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
1066 border = path[depth].p_ext[1].ee_block;
1067 ext_debug("leaf will be split."
1068 " next leaf starts at %d\n",
1069 le32_to_cpu(border));
1070 } else {
1071 border = newext->ee_block;
1072 ext_debug("leaf will be added."
1073 " next leaf starts at %d\n",
1074 le32_to_cpu(border));
1075 }
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089 ablocks = kcalloc(depth, sizeof(ext4_fsblk_t), GFP_NOFS);
1090 if (!ablocks)
1091 return -ENOMEM;
1092
1093
1094 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
1095 for (a = 0; a < depth - at; a++) {
1096 newblock = ext4_ext_new_meta_block(handle, inode, path,
1097 newext, &err, flags);
1098 if (newblock == 0)
1099 goto cleanup;
1100 ablocks[a] = newblock;
1101 }
1102
1103
1104 newblock = ablocks[--a];
1105 if (unlikely(newblock == 0)) {
1106 EXT4_ERROR_INODE(inode, "newblock == 0!");
1107 err = -EFSCORRUPTED;
1108 goto cleanup;
1109 }
1110 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
1111 if (unlikely(!bh)) {
1112 err = -ENOMEM;
1113 goto cleanup;
1114 }
1115 lock_buffer(bh);
1116
1117 err = ext4_journal_get_create_access(handle, bh);
1118 if (err)
1119 goto cleanup;
1120
1121 neh = ext_block_hdr(bh);
1122 neh->eh_entries = 0;
1123 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
1124 neh->eh_magic = EXT4_EXT_MAGIC;
1125 neh->eh_depth = 0;
1126
1127
1128 if (unlikely(path[depth].p_hdr->eh_entries !=
1129 path[depth].p_hdr->eh_max)) {
1130 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
1131 path[depth].p_hdr->eh_entries,
1132 path[depth].p_hdr->eh_max);
1133 err = -EFSCORRUPTED;
1134 goto cleanup;
1135 }
1136
1137 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
1138 ext4_ext_show_move(inode, path, newblock, depth);
1139 if (m) {
1140 struct ext4_extent *ex;
1141 ex = EXT_FIRST_EXTENT(neh);
1142 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
1143 le16_add_cpu(&neh->eh_entries, m);
1144 }
1145
1146
1147 ext_size = sizeof(struct ext4_extent_header) +
1148 sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries);
1149 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
1150 ext4_extent_block_csum_set(inode, neh);
1151 set_buffer_uptodate(bh);
1152 unlock_buffer(bh);
1153
1154 err = ext4_handle_dirty_metadata(handle, inode, bh);
1155 if (err)
1156 goto cleanup;
1157 brelse(bh);
1158 bh = NULL;
1159
1160
1161 if (m) {
1162 err = ext4_ext_get_access(handle, inode, path + depth);
1163 if (err)
1164 goto cleanup;
1165 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
1166 err = ext4_ext_dirty(handle, inode, path + depth);
1167 if (err)
1168 goto cleanup;
1169
1170 }
1171
1172
1173 k = depth - at - 1;
1174 if (unlikely(k < 0)) {
1175 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
1176 err = -EFSCORRUPTED;
1177 goto cleanup;
1178 }
1179 if (k)
1180 ext_debug("create %d intermediate indices\n", k);
1181
1182
1183 i = depth - 1;
1184 while (k--) {
1185 oldblock = newblock;
1186 newblock = ablocks[--a];
1187 bh = sb_getblk(inode->i_sb, newblock);
1188 if (unlikely(!bh)) {
1189 err = -ENOMEM;
1190 goto cleanup;
1191 }
1192 lock_buffer(bh);
1193
1194 err = ext4_journal_get_create_access(handle, bh);
1195 if (err)
1196 goto cleanup;
1197
1198 neh = ext_block_hdr(bh);
1199 neh->eh_entries = cpu_to_le16(1);
1200 neh->eh_magic = EXT4_EXT_MAGIC;
1201 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
1202 neh->eh_depth = cpu_to_le16(depth - i);
1203 fidx = EXT_FIRST_INDEX(neh);
1204 fidx->ei_block = border;
1205 ext4_idx_store_pblock(fidx, oldblock);
1206
1207 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
1208 i, newblock, le32_to_cpu(border), oldblock);
1209
1210
1211 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
1212 EXT_LAST_INDEX(path[i].p_hdr))) {
1213 EXT4_ERROR_INODE(inode,
1214 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
1215 le32_to_cpu(path[i].p_ext->ee_block));
1216 err = -EFSCORRUPTED;
1217 goto cleanup;
1218 }
1219
1220 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
1221 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
1222 EXT_MAX_INDEX(path[i].p_hdr));
1223 ext4_ext_show_move(inode, path, newblock, i);
1224 if (m) {
1225 memmove(++fidx, path[i].p_idx,
1226 sizeof(struct ext4_extent_idx) * m);
1227 le16_add_cpu(&neh->eh_entries, m);
1228 }
1229
1230 ext_size = sizeof(struct ext4_extent_header) +
1231 (sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries));
1232 memset(bh->b_data + ext_size, 0,
1233 inode->i_sb->s_blocksize - ext_size);
1234 ext4_extent_block_csum_set(inode, neh);
1235 set_buffer_uptodate(bh);
1236 unlock_buffer(bh);
1237
1238 err = ext4_handle_dirty_metadata(handle, inode, bh);
1239 if (err)
1240 goto cleanup;
1241 brelse(bh);
1242 bh = NULL;
1243
1244
1245 if (m) {
1246 err = ext4_ext_get_access(handle, inode, path + i);
1247 if (err)
1248 goto cleanup;
1249 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
1250 err = ext4_ext_dirty(handle, inode, path + i);
1251 if (err)
1252 goto cleanup;
1253 }
1254
1255 i--;
1256 }
1257
1258
1259 err = ext4_ext_insert_index(handle, inode, path + at,
1260 le32_to_cpu(border), newblock);
1261
1262cleanup:
1263 if (bh) {
1264 if (buffer_locked(bh))
1265 unlock_buffer(bh);
1266 brelse(bh);
1267 }
1268
1269 if (err) {
1270
1271 for (i = 0; i < depth; i++) {
1272 if (!ablocks[i])
1273 continue;
1274 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
1275 EXT4_FREE_BLOCKS_METADATA);
1276 }
1277 }
1278 kfree(ablocks);
1279
1280 return err;
1281}
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
1292 unsigned int flags)
1293{
1294 struct ext4_extent_header *neh;
1295 struct buffer_head *bh;
1296 ext4_fsblk_t newblock, goal = 0;
1297 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
1298 int err = 0;
1299 size_t ext_size = 0;
1300
1301
1302 if (ext_depth(inode))
1303 goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode)));
1304 if (goal > le32_to_cpu(es->s_first_data_block)) {
1305 flags |= EXT4_MB_HINT_TRY_GOAL;
1306 goal--;
1307 } else
1308 goal = ext4_inode_to_goal_block(inode);
1309 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
1310 NULL, &err);
1311 if (newblock == 0)
1312 return err;
1313
1314 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS);
1315 if (unlikely(!bh))
1316 return -ENOMEM;
1317 lock_buffer(bh);
1318
1319 err = ext4_journal_get_create_access(handle, bh);
1320 if (err) {
1321 unlock_buffer(bh);
1322 goto out;
1323 }
1324
1325 ext_size = sizeof(EXT4_I(inode)->i_data);
1326
1327 memmove(bh->b_data, EXT4_I(inode)->i_data, ext_size);
1328
1329 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size);
1330
1331
1332 neh = ext_block_hdr(bh);
1333
1334
1335 if (ext_depth(inode))
1336 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
1337 else
1338 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
1339 neh->eh_magic = EXT4_EXT_MAGIC;
1340 ext4_extent_block_csum_set(inode, neh);
1341 set_buffer_uptodate(bh);
1342 unlock_buffer(bh);
1343
1344 err = ext4_handle_dirty_metadata(handle, inode, bh);
1345 if (err)
1346 goto out;
1347
1348
1349 neh = ext_inode_hdr(inode);
1350 neh->eh_entries = cpu_to_le16(1);
1351 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1352 if (neh->eh_depth == 0) {
1353
1354 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1355 EXT_FIRST_INDEX(neh)->ei_block =
1356 EXT_FIRST_EXTENT(neh)->ee_block;
1357 }
1358 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1359 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
1360 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
1361 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
1362
1363 le16_add_cpu(&neh->eh_depth, 1);
1364 ext4_mark_inode_dirty(handle, inode);
1365out:
1366 brelse(bh);
1367
1368 return err;
1369}
1370
1371
1372
1373
1374
1375
1376static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
1377 unsigned int mb_flags,
1378 unsigned int gb_flags,
1379 struct ext4_ext_path **ppath,
1380 struct ext4_extent *newext)
1381{
1382 struct ext4_ext_path *path = *ppath;
1383 struct ext4_ext_path *curp;
1384 int depth, i, err = 0;
1385
1386repeat:
1387 i = depth = ext_depth(inode);
1388
1389
1390 curp = path + depth;
1391 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1392 i--;
1393 curp--;
1394 }
1395
1396
1397
1398 if (EXT_HAS_FREE_INDEX(curp)) {
1399
1400
1401 err = ext4_ext_split(handle, inode, mb_flags, path, newext, i);
1402 if (err)
1403 goto out;
1404
1405
1406 path = ext4_find_extent(inode,
1407 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1408 ppath, gb_flags);
1409 if (IS_ERR(path))
1410 err = PTR_ERR(path);
1411 } else {
1412
1413 err = ext4_ext_grow_indepth(handle, inode, mb_flags);
1414 if (err)
1415 goto out;
1416
1417
1418 path = ext4_find_extent(inode,
1419 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1420 ppath, gb_flags);
1421 if (IS_ERR(path)) {
1422 err = PTR_ERR(path);
1423 goto out;
1424 }
1425
1426
1427
1428
1429
1430 depth = ext_depth(inode);
1431 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
1432
1433 goto repeat;
1434 }
1435 }
1436
1437out:
1438 return err;
1439}
1440
1441
1442
1443
1444
1445
1446
1447
1448static int ext4_ext_search_left(struct inode *inode,
1449 struct ext4_ext_path *path,
1450 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1451{
1452 struct ext4_extent_idx *ix;
1453 struct ext4_extent *ex;
1454 int depth, ee_len;
1455
1456 if (unlikely(path == NULL)) {
1457 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1458 return -EFSCORRUPTED;
1459 }
1460 depth = path->p_depth;
1461 *phys = 0;
1462
1463 if (depth == 0 && path->p_ext == NULL)
1464 return 0;
1465
1466
1467
1468
1469
1470 ex = path[depth].p_ext;
1471 ee_len = ext4_ext_get_actual_len(ex);
1472 if (*logical < le32_to_cpu(ex->ee_block)) {
1473 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1474 EXT4_ERROR_INODE(inode,
1475 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1476 *logical, le32_to_cpu(ex->ee_block));
1477 return -EFSCORRUPTED;
1478 }
1479 while (--depth >= 0) {
1480 ix = path[depth].p_idx;
1481 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1482 EXT4_ERROR_INODE(inode,
1483 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
1484 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
1485 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
1486 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
1487 depth);
1488 return -EFSCORRUPTED;
1489 }
1490 }
1491 return 0;
1492 }
1493
1494 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1495 EXT4_ERROR_INODE(inode,
1496 "logical %d < ee_block %d + ee_len %d!",
1497 *logical, le32_to_cpu(ex->ee_block), ee_len);
1498 return -EFSCORRUPTED;
1499 }
1500
1501 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1502 *phys = ext4_ext_pblock(ex) + ee_len - 1;
1503 return 0;
1504}
1505
1506
1507
1508
1509
1510
1511
1512
1513static int ext4_ext_search_right(struct inode *inode,
1514 struct ext4_ext_path *path,
1515 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1516 struct ext4_extent **ret_ex)
1517{
1518 struct buffer_head *bh = NULL;
1519 struct ext4_extent_header *eh;
1520 struct ext4_extent_idx *ix;
1521 struct ext4_extent *ex;
1522 ext4_fsblk_t block;
1523 int depth;
1524 int ee_len;
1525
1526 if (unlikely(path == NULL)) {
1527 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1528 return -EFSCORRUPTED;
1529 }
1530 depth = path->p_depth;
1531 *phys = 0;
1532
1533 if (depth == 0 && path->p_ext == NULL)
1534 return 0;
1535
1536
1537
1538
1539
1540 ex = path[depth].p_ext;
1541 ee_len = ext4_ext_get_actual_len(ex);
1542 if (*logical < le32_to_cpu(ex->ee_block)) {
1543 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1544 EXT4_ERROR_INODE(inode,
1545 "first_extent(path[%d].p_hdr) != ex",
1546 depth);
1547 return -EFSCORRUPTED;
1548 }
1549 while (--depth >= 0) {
1550 ix = path[depth].p_idx;
1551 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1552 EXT4_ERROR_INODE(inode,
1553 "ix != EXT_FIRST_INDEX *logical %d!",
1554 *logical);
1555 return -EFSCORRUPTED;
1556 }
1557 }
1558 goto found_extent;
1559 }
1560
1561 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1562 EXT4_ERROR_INODE(inode,
1563 "logical %d < ee_block %d + ee_len %d!",
1564 *logical, le32_to_cpu(ex->ee_block), ee_len);
1565 return -EFSCORRUPTED;
1566 }
1567
1568 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1569
1570 ex++;
1571 goto found_extent;
1572 }
1573
1574
1575 while (--depth >= 0) {
1576 ix = path[depth].p_idx;
1577 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
1578 goto got_index;
1579 }
1580
1581
1582 return 0;
1583
1584got_index:
1585
1586
1587
1588 ix++;
1589 block = ext4_idx_pblock(ix);
1590 while (++depth < path->p_depth) {
1591
1592 bh = read_extent_tree_block(inode, block,
1593 path->p_depth - depth, 0);
1594 if (IS_ERR(bh))
1595 return PTR_ERR(bh);
1596 eh = ext_block_hdr(bh);
1597 ix = EXT_FIRST_INDEX(eh);
1598 block = ext4_idx_pblock(ix);
1599 put_bh(bh);
1600 }
1601
1602 bh = read_extent_tree_block(inode, block, path->p_depth - depth, 0);
1603 if (IS_ERR(bh))
1604 return PTR_ERR(bh);
1605 eh = ext_block_hdr(bh);
1606 ex = EXT_FIRST_EXTENT(eh);
1607found_extent:
1608 *logical = le32_to_cpu(ex->ee_block);
1609 *phys = ext4_ext_pblock(ex);
1610 *ret_ex = ex;
1611 if (bh)
1612 put_bh(bh);
1613 return 0;
1614}
1615
1616
1617
1618
1619
1620
1621
1622
1623ext4_lblk_t
1624ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1625{
1626 int depth;
1627
1628 BUG_ON(path == NULL);
1629 depth = path->p_depth;
1630
1631 if (depth == 0 && path->p_ext == NULL)
1632 return EXT_MAX_BLOCKS;
1633
1634 while (depth >= 0) {
1635 if (depth == path->p_depth) {
1636
1637 if (path[depth].p_ext &&
1638 path[depth].p_ext !=
1639 EXT_LAST_EXTENT(path[depth].p_hdr))
1640 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1641 } else {
1642
1643 if (path[depth].p_idx !=
1644 EXT_LAST_INDEX(path[depth].p_hdr))
1645 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1646 }
1647 depth--;
1648 }
1649
1650 return EXT_MAX_BLOCKS;
1651}
1652
1653
1654
1655
1656
1657static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
1658{
1659 int depth;
1660
1661 BUG_ON(path == NULL);
1662 depth = path->p_depth;
1663
1664
1665 if (depth == 0)
1666 return EXT_MAX_BLOCKS;
1667
1668
1669 depth--;
1670
1671 while (depth >= 0) {
1672 if (path[depth].p_idx !=
1673 EXT_LAST_INDEX(path[depth].p_hdr))
1674 return (ext4_lblk_t)
1675 le32_to_cpu(path[depth].p_idx[1].ei_block);
1676 depth--;
1677 }
1678
1679 return EXT_MAX_BLOCKS;
1680}
1681
1682
1683
1684
1685
1686
1687
1688static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1689 struct ext4_ext_path *path)
1690{
1691 struct ext4_extent_header *eh;
1692 int depth = ext_depth(inode);
1693 struct ext4_extent *ex;
1694 __le32 border;
1695 int k, err = 0;
1696
1697 eh = path[depth].p_hdr;
1698 ex = path[depth].p_ext;
1699
1700 if (unlikely(ex == NULL || eh == NULL)) {
1701 EXT4_ERROR_INODE(inode,
1702 "ex %p == NULL or eh %p == NULL", ex, eh);
1703 return -EFSCORRUPTED;
1704 }
1705
1706 if (depth == 0) {
1707
1708 return 0;
1709 }
1710
1711 if (ex != EXT_FIRST_EXTENT(eh)) {
1712
1713 return 0;
1714 }
1715
1716
1717
1718
1719 k = depth - 1;
1720 border = path[depth].p_ext->ee_block;
1721 err = ext4_ext_get_access(handle, inode, path + k);
1722 if (err)
1723 return err;
1724 path[k].p_idx->ei_block = border;
1725 err = ext4_ext_dirty(handle, inode, path + k);
1726 if (err)
1727 return err;
1728
1729 while (k--) {
1730
1731 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1732 break;
1733 err = ext4_ext_get_access(handle, inode, path + k);
1734 if (err)
1735 break;
1736 path[k].p_idx->ei_block = border;
1737 err = ext4_ext_dirty(handle, inode, path + k);
1738 if (err)
1739 break;
1740 }
1741
1742 return err;
1743}
1744
1745int
1746ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1747 struct ext4_extent *ex2)
1748{
1749 unsigned short ext1_ee_len, ext2_ee_len;
1750
1751 if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2))
1752 return 0;
1753
1754 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1755 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1756
1757 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
1758 le32_to_cpu(ex2->ee_block))
1759 return 0;
1760
1761
1762
1763
1764
1765
1766 if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN)
1767 return 0;
1768
1769 if (ext4_ext_is_unwritten(ex1) &&
1770 ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN)
1771 return 0;
1772#ifdef AGGRESSIVE_TEST
1773 if (ext1_ee_len >= 4)
1774 return 0;
1775#endif
1776
1777 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
1778 return 1;
1779 return 0;
1780}
1781
1782
1783
1784
1785
1786
1787
1788
1789static int ext4_ext_try_to_merge_right(struct inode *inode,
1790 struct ext4_ext_path *path,
1791 struct ext4_extent *ex)
1792{
1793 struct ext4_extent_header *eh;
1794 unsigned int depth, len;
1795 int merge_done = 0, unwritten;
1796
1797 depth = ext_depth(inode);
1798 BUG_ON(path[depth].p_hdr == NULL);
1799 eh = path[depth].p_hdr;
1800
1801 while (ex < EXT_LAST_EXTENT(eh)) {
1802 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1803 break;
1804
1805 unwritten = ext4_ext_is_unwritten(ex);
1806 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1807 + ext4_ext_get_actual_len(ex + 1));
1808 if (unwritten)
1809 ext4_ext_mark_unwritten(ex);
1810
1811 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1812 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1813 * sizeof(struct ext4_extent);
1814 memmove(ex + 1, ex + 2, len);
1815 }
1816 le16_add_cpu(&eh->eh_entries, -1);
1817 merge_done = 1;
1818 WARN_ON(eh->eh_entries == 0);
1819 if (!eh->eh_entries)
1820 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
1821 }
1822
1823 return merge_done;
1824}
1825
1826
1827
1828
1829
1830static void ext4_ext_try_to_merge_up(handle_t *handle,
1831 struct inode *inode,
1832 struct ext4_ext_path *path)
1833{
1834 size_t s;
1835 unsigned max_root = ext4_ext_space_root(inode, 0);
1836 ext4_fsblk_t blk;
1837
1838 if ((path[0].p_depth != 1) ||
1839 (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) ||
1840 (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root))
1841 return;
1842
1843
1844
1845
1846
1847
1848 if (ext4_journal_extend(handle, 2,
1849 ext4_free_metadata_revoke_credits(inode->i_sb, 1)))
1850 return;
1851
1852
1853
1854
1855 blk = ext4_idx_pblock(path[0].p_idx);
1856 s = le16_to_cpu(path[1].p_hdr->eh_entries) *
1857 sizeof(struct ext4_extent_idx);
1858 s += sizeof(struct ext4_extent_header);
1859
1860 path[1].p_maxdepth = path[0].p_maxdepth;
1861 memcpy(path[0].p_hdr, path[1].p_hdr, s);
1862 path[0].p_depth = 0;
1863 path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) +
1864 (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr));
1865 path[0].p_hdr->eh_max = cpu_to_le16(max_root);
1866
1867 brelse(path[1].p_bh);
1868 ext4_free_blocks(handle, inode, NULL, blk, 1,
1869 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
1870}
1871
1872
1873
1874
1875
1876static void ext4_ext_try_to_merge(handle_t *handle,
1877 struct inode *inode,
1878 struct ext4_ext_path *path,
1879 struct ext4_extent *ex) {
1880 struct ext4_extent_header *eh;
1881 unsigned int depth;
1882 int merge_done = 0;
1883
1884 depth = ext_depth(inode);
1885 BUG_ON(path[depth].p_hdr == NULL);
1886 eh = path[depth].p_hdr;
1887
1888 if (ex > EXT_FIRST_EXTENT(eh))
1889 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1890
1891 if (!merge_done)
1892 (void) ext4_ext_try_to_merge_right(inode, path, ex);
1893
1894 ext4_ext_try_to_merge_up(handle, inode, path);
1895}
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1906 struct inode *inode,
1907 struct ext4_extent *newext,
1908 struct ext4_ext_path *path)
1909{
1910 ext4_lblk_t b1, b2;
1911 unsigned int depth, len1;
1912 unsigned int ret = 0;
1913
1914 b1 = le32_to_cpu(newext->ee_block);
1915 len1 = ext4_ext_get_actual_len(newext);
1916 depth = ext_depth(inode);
1917 if (!path[depth].p_ext)
1918 goto out;
1919 b2 = EXT4_LBLK_CMASK(sbi, le32_to_cpu(path[depth].p_ext->ee_block));
1920
1921
1922
1923
1924
1925 if (b2 < b1) {
1926 b2 = ext4_ext_next_allocated_block(path);
1927 if (b2 == EXT_MAX_BLOCKS)
1928 goto out;
1929 b2 = EXT4_LBLK_CMASK(sbi, b2);
1930 }
1931
1932
1933 if (b1 + len1 < b1) {
1934 len1 = EXT_MAX_BLOCKS - b1;
1935 newext->ee_len = cpu_to_le16(len1);
1936 ret = 1;
1937 }
1938
1939
1940 if (b1 + len1 > b2) {
1941 newext->ee_len = cpu_to_le16(b2 - b1);
1942 ret = 1;
1943 }
1944out:
1945 return ret;
1946}
1947
1948
1949
1950
1951
1952
1953
1954int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1955 struct ext4_ext_path **ppath,
1956 struct ext4_extent *newext, int gb_flags)
1957{
1958 struct ext4_ext_path *path = *ppath;
1959 struct ext4_extent_header *eh;
1960 struct ext4_extent *ex, *fex;
1961 struct ext4_extent *nearex;
1962 struct ext4_ext_path *npath = NULL;
1963 int depth, len, err;
1964 ext4_lblk_t next;
1965 int mb_flags = 0, unwritten;
1966
1967 if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
1968 mb_flags |= EXT4_MB_DELALLOC_RESERVED;
1969 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1970 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1971 return -EFSCORRUPTED;
1972 }
1973 depth = ext_depth(inode);
1974 ex = path[depth].p_ext;
1975 eh = path[depth].p_hdr;
1976 if (unlikely(path[depth].p_hdr == NULL)) {
1977 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1978 return -EFSCORRUPTED;
1979 }
1980
1981
1982 if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) {
1983
1984
1985
1986
1987
1988
1989
1990
1991 if (ex < EXT_LAST_EXTENT(eh) &&
1992 (le32_to_cpu(ex->ee_block) +
1993 ext4_ext_get_actual_len(ex) <
1994 le32_to_cpu(newext->ee_block))) {
1995 ex += 1;
1996 goto prepend;
1997 } else if ((ex > EXT_FIRST_EXTENT(eh)) &&
1998 (le32_to_cpu(newext->ee_block) +
1999 ext4_ext_get_actual_len(newext) <
2000 le32_to_cpu(ex->ee_block)))
2001 ex -= 1;
2002
2003
2004 if (ext4_can_extents_be_merged(inode, ex, newext)) {
2005 ext_debug("append [%d]%d block to %u:[%d]%d"
2006 "(from %llu)\n",
2007 ext4_ext_is_unwritten(newext),
2008 ext4_ext_get_actual_len(newext),
2009 le32_to_cpu(ex->ee_block),
2010 ext4_ext_is_unwritten(ex),
2011 ext4_ext_get_actual_len(ex),
2012 ext4_ext_pblock(ex));
2013 err = ext4_ext_get_access(handle, inode,
2014 path + depth);
2015 if (err)
2016 return err;
2017 unwritten = ext4_ext_is_unwritten(ex);
2018 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
2019 + ext4_ext_get_actual_len(newext));
2020 if (unwritten)
2021 ext4_ext_mark_unwritten(ex);
2022 eh = path[depth].p_hdr;
2023 nearex = ex;
2024 goto merge;
2025 }
2026
2027prepend:
2028
2029 if (ext4_can_extents_be_merged(inode, newext, ex)) {
2030 ext_debug("prepend %u[%d]%d block to %u:[%d]%d"
2031 "(from %llu)\n",
2032 le32_to_cpu(newext->ee_block),
2033 ext4_ext_is_unwritten(newext),
2034 ext4_ext_get_actual_len(newext),
2035 le32_to_cpu(ex->ee_block),
2036 ext4_ext_is_unwritten(ex),
2037 ext4_ext_get_actual_len(ex),
2038 ext4_ext_pblock(ex));
2039 err = ext4_ext_get_access(handle, inode,
2040 path + depth);
2041 if (err)
2042 return err;
2043
2044 unwritten = ext4_ext_is_unwritten(ex);
2045 ex->ee_block = newext->ee_block;
2046 ext4_ext_store_pblock(ex, ext4_ext_pblock(newext));
2047 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
2048 + ext4_ext_get_actual_len(newext));
2049 if (unwritten)
2050 ext4_ext_mark_unwritten(ex);
2051 eh = path[depth].p_hdr;
2052 nearex = ex;
2053 goto merge;
2054 }
2055 }
2056
2057 depth = ext_depth(inode);
2058 eh = path[depth].p_hdr;
2059 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
2060 goto has_space;
2061
2062
2063 fex = EXT_LAST_EXTENT(eh);
2064 next = EXT_MAX_BLOCKS;
2065 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
2066 next = ext4_ext_next_leaf_block(path);
2067 if (next != EXT_MAX_BLOCKS) {
2068 ext_debug("next leaf block - %u\n", next);
2069 BUG_ON(npath != NULL);
2070 npath = ext4_find_extent(inode, next, NULL, 0);
2071 if (IS_ERR(npath))
2072 return PTR_ERR(npath);
2073 BUG_ON(npath->p_depth != path->p_depth);
2074 eh = npath[depth].p_hdr;
2075 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
2076 ext_debug("next leaf isn't full(%d)\n",
2077 le16_to_cpu(eh->eh_entries));
2078 path = npath;
2079 goto has_space;
2080 }
2081 ext_debug("next leaf has no free space(%d,%d)\n",
2082 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
2083 }
2084
2085
2086
2087
2088
2089 if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
2090 mb_flags |= EXT4_MB_USE_RESERVED;
2091 err = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags,
2092 ppath, newext);
2093 if (err)
2094 goto cleanup;
2095 depth = ext_depth(inode);
2096 eh = path[depth].p_hdr;
2097
2098has_space:
2099 nearex = path[depth].p_ext;
2100
2101 err = ext4_ext_get_access(handle, inode, path + depth);
2102 if (err)
2103 goto cleanup;
2104
2105 if (!nearex) {
2106
2107 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
2108 le32_to_cpu(newext->ee_block),
2109 ext4_ext_pblock(newext),
2110 ext4_ext_is_unwritten(newext),
2111 ext4_ext_get_actual_len(newext));
2112 nearex = EXT_FIRST_EXTENT(eh);
2113 } else {
2114 if (le32_to_cpu(newext->ee_block)
2115 > le32_to_cpu(nearex->ee_block)) {
2116
2117 ext_debug("insert %u:%llu:[%d]%d before: "
2118 "nearest %p\n",
2119 le32_to_cpu(newext->ee_block),
2120 ext4_ext_pblock(newext),
2121 ext4_ext_is_unwritten(newext),
2122 ext4_ext_get_actual_len(newext),
2123 nearex);
2124 nearex++;
2125 } else {
2126
2127 BUG_ON(newext->ee_block == nearex->ee_block);
2128 ext_debug("insert %u:%llu:[%d]%d after: "
2129 "nearest %p\n",
2130 le32_to_cpu(newext->ee_block),
2131 ext4_ext_pblock(newext),
2132 ext4_ext_is_unwritten(newext),
2133 ext4_ext_get_actual_len(newext),
2134 nearex);
2135 }
2136 len = EXT_LAST_EXTENT(eh) - nearex + 1;
2137 if (len > 0) {
2138 ext_debug("insert %u:%llu:[%d]%d: "
2139 "move %d extents from 0x%p to 0x%p\n",
2140 le32_to_cpu(newext->ee_block),
2141 ext4_ext_pblock(newext),
2142 ext4_ext_is_unwritten(newext),
2143 ext4_ext_get_actual_len(newext),
2144 len, nearex, nearex + 1);
2145 memmove(nearex + 1, nearex,
2146 len * sizeof(struct ext4_extent));
2147 }
2148 }
2149
2150 le16_add_cpu(&eh->eh_entries, 1);
2151 path[depth].p_ext = nearex;
2152 nearex->ee_block = newext->ee_block;
2153 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
2154 nearex->ee_len = newext->ee_len;
2155
2156merge:
2157
2158 if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO))
2159 ext4_ext_try_to_merge(handle, inode, path, nearex);
2160
2161
2162
2163 err = ext4_ext_correct_indexes(handle, inode, path);
2164 if (err)
2165 goto cleanup;
2166
2167 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
2168
2169cleanup:
2170 ext4_ext_drop_refs(npath);
2171 kfree(npath);
2172 return err;
2173}
2174
2175static int ext4_fill_fiemap_extents(struct inode *inode,
2176 ext4_lblk_t block, ext4_lblk_t num,
2177 struct fiemap_extent_info *fieinfo)
2178{
2179 struct ext4_ext_path *path = NULL;
2180 struct ext4_extent *ex;
2181 struct extent_status es;
2182 ext4_lblk_t next, next_del, start = 0, end = 0;
2183 ext4_lblk_t last = block + num;
2184 int exists, depth = 0, err = 0;
2185 unsigned int flags = 0;
2186 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
2187
2188 while (block < last && block != EXT_MAX_BLOCKS) {
2189 num = last - block;
2190
2191 down_read(&EXT4_I(inode)->i_data_sem);
2192
2193 path = ext4_find_extent(inode, block, &path, 0);
2194 if (IS_ERR(path)) {
2195 up_read(&EXT4_I(inode)->i_data_sem);
2196 err = PTR_ERR(path);
2197 path = NULL;
2198 break;
2199 }
2200
2201 depth = ext_depth(inode);
2202 if (unlikely(path[depth].p_hdr == NULL)) {
2203 up_read(&EXT4_I(inode)->i_data_sem);
2204 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2205 err = -EFSCORRUPTED;
2206 break;
2207 }
2208 ex = path[depth].p_ext;
2209 next = ext4_ext_next_allocated_block(path);
2210
2211 flags = 0;
2212 exists = 0;
2213 if (!ex) {
2214
2215
2216 start = block;
2217 end = block + num;
2218 } else if (le32_to_cpu(ex->ee_block) > block) {
2219
2220 start = block;
2221 end = le32_to_cpu(ex->ee_block);
2222 if (block + num < end)
2223 end = block + num;
2224 } else if (block >= le32_to_cpu(ex->ee_block)
2225 + ext4_ext_get_actual_len(ex)) {
2226
2227 start = block;
2228 end = block + num;
2229 if (end >= next)
2230 end = next;
2231 } else if (block >= le32_to_cpu(ex->ee_block)) {
2232
2233
2234
2235
2236 start = block;
2237 end = le32_to_cpu(ex->ee_block)
2238 + ext4_ext_get_actual_len(ex);
2239 if (block + num < end)
2240 end = block + num;
2241 exists = 1;
2242 } else {
2243 BUG();
2244 }
2245 BUG_ON(end <= start);
2246
2247 if (!exists) {
2248 es.es_lblk = start;
2249 es.es_len = end - start;
2250 es.es_pblk = 0;
2251 } else {
2252 es.es_lblk = le32_to_cpu(ex->ee_block);
2253 es.es_len = ext4_ext_get_actual_len(ex);
2254 es.es_pblk = ext4_ext_pblock(ex);
2255 if (ext4_ext_is_unwritten(ex))
2256 flags |= FIEMAP_EXTENT_UNWRITTEN;
2257 }
2258
2259
2260
2261
2262
2263
2264 next_del = ext4_find_delayed_extent(inode, &es);
2265 if (!exists && next_del) {
2266 exists = 1;
2267 flags |= (FIEMAP_EXTENT_DELALLOC |
2268 FIEMAP_EXTENT_UNKNOWN);
2269 }
2270 up_read(&EXT4_I(inode)->i_data_sem);
2271
2272 if (unlikely(es.es_len == 0)) {
2273 EXT4_ERROR_INODE(inode, "es.es_len == 0");
2274 err = -EFSCORRUPTED;
2275 break;
2276 }
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289 if (next == next_del && next == EXT_MAX_BLOCKS) {
2290 flags |= FIEMAP_EXTENT_LAST;
2291 if (unlikely(next_del != EXT_MAX_BLOCKS ||
2292 next != EXT_MAX_BLOCKS)) {
2293 EXT4_ERROR_INODE(inode,
2294 "next extent == %u, next "
2295 "delalloc extent = %u",
2296 next, next_del);
2297 err = -EFSCORRUPTED;
2298 break;
2299 }
2300 }
2301
2302 if (exists) {
2303 err = fiemap_fill_next_extent(fieinfo,
2304 (__u64)es.es_lblk << blksize_bits,
2305 (__u64)es.es_pblk << blksize_bits,
2306 (__u64)es.es_len << blksize_bits,
2307 flags);
2308 if (err < 0)
2309 break;
2310 if (err == 1) {
2311 err = 0;
2312 break;
2313 }
2314 }
2315
2316 block = es.es_lblk + es.es_len;
2317 }
2318
2319 ext4_ext_drop_refs(path);
2320 kfree(path);
2321 return err;
2322}
2323
2324static int ext4_fill_es_cache_info(struct inode *inode,
2325 ext4_lblk_t block, ext4_lblk_t num,
2326 struct fiemap_extent_info *fieinfo)
2327{
2328 ext4_lblk_t next, end = block + num - 1;
2329 struct extent_status es;
2330 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
2331 unsigned int flags;
2332 int err;
2333
2334 while (block <= end) {
2335 next = 0;
2336 flags = 0;
2337 if (!ext4_es_lookup_extent(inode, block, &next, &es))
2338 break;
2339 if (ext4_es_is_unwritten(&es))
2340 flags |= FIEMAP_EXTENT_UNWRITTEN;
2341 if (ext4_es_is_delayed(&es))
2342 flags |= (FIEMAP_EXTENT_DELALLOC |
2343 FIEMAP_EXTENT_UNKNOWN);
2344 if (ext4_es_is_hole(&es))
2345 flags |= EXT4_FIEMAP_EXTENT_HOLE;
2346 if (next == 0)
2347 flags |= FIEMAP_EXTENT_LAST;
2348 if (flags & (FIEMAP_EXTENT_DELALLOC|
2349 EXT4_FIEMAP_EXTENT_HOLE))
2350 es.es_pblk = 0;
2351 else
2352 es.es_pblk = ext4_es_pblock(&es);
2353 err = fiemap_fill_next_extent(fieinfo,
2354 (__u64)es.es_lblk << blksize_bits,
2355 (__u64)es.es_pblk << blksize_bits,
2356 (__u64)es.es_len << blksize_bits,
2357 flags);
2358 if (next == 0)
2359 break;
2360 block = next;
2361 if (err < 0)
2362 return err;
2363 if (err == 1)
2364 return 0;
2365 }
2366 return 0;
2367}
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383static ext4_lblk_t ext4_ext_determine_hole(struct inode *inode,
2384 struct ext4_ext_path *path,
2385 ext4_lblk_t *lblk)
2386{
2387 int depth = ext_depth(inode);
2388 struct ext4_extent *ex;
2389 ext4_lblk_t len;
2390
2391 ex = path[depth].p_ext;
2392 if (ex == NULL) {
2393
2394 *lblk = 0;
2395 len = EXT_MAX_BLOCKS;
2396 } else if (*lblk < le32_to_cpu(ex->ee_block)) {
2397 len = le32_to_cpu(ex->ee_block) - *lblk;
2398 } else if (*lblk >= le32_to_cpu(ex->ee_block)
2399 + ext4_ext_get_actual_len(ex)) {
2400 ext4_lblk_t next;
2401
2402 *lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex);
2403 next = ext4_ext_next_allocated_block(path);
2404 BUG_ON(next == *lblk);
2405 len = next - *lblk;
2406 } else {
2407 BUG();
2408 }
2409 return len;
2410}
2411
2412
2413
2414
2415
2416
2417static void
2418ext4_ext_put_gap_in_cache(struct inode *inode, ext4_lblk_t hole_start,
2419 ext4_lblk_t hole_len)
2420{
2421 struct extent_status es;
2422
2423 ext4_es_find_extent_range(inode, &ext4_es_is_delayed, hole_start,
2424 hole_start + hole_len - 1, &es);
2425 if (es.es_len) {
2426
2427 if (es.es_lblk <= hole_start)
2428 return;
2429 hole_len = min(es.es_lblk - hole_start, hole_len);
2430 }
2431 ext_debug(" -> %u:%u\n", hole_start, hole_len);
2432 ext4_es_insert_extent(inode, hole_start, hole_len, ~0,
2433 EXTENT_STATUS_HOLE);
2434}
2435
2436
2437
2438
2439
2440static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
2441 struct ext4_ext_path *path, int depth)
2442{
2443 int err;
2444 ext4_fsblk_t leaf;
2445
2446
2447 depth--;
2448 path = path + depth;
2449 leaf = ext4_idx_pblock(path->p_idx);
2450 if (unlikely(path->p_hdr->eh_entries == 0)) {
2451 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2452 return -EFSCORRUPTED;
2453 }
2454 err = ext4_ext_get_access(handle, inode, path);
2455 if (err)
2456 return err;
2457
2458 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2459 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2460 len *= sizeof(struct ext4_extent_idx);
2461 memmove(path->p_idx, path->p_idx + 1, len);
2462 }
2463
2464 le16_add_cpu(&path->p_hdr->eh_entries, -1);
2465 err = ext4_ext_dirty(handle, inode, path);
2466 if (err)
2467 return err;
2468 ext_debug("index is empty, remove it, free block %llu\n", leaf);
2469 trace_ext4_ext_rm_idx(inode, leaf);
2470
2471 ext4_free_blocks(handle, inode, NULL, leaf, 1,
2472 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
2473
2474 while (--depth >= 0) {
2475 if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr))
2476 break;
2477 path--;
2478 err = ext4_ext_get_access(handle, inode, path);
2479 if (err)
2480 break;
2481 path->p_idx->ei_block = (path+1)->p_idx->ei_block;
2482 err = ext4_ext_dirty(handle, inode, path);
2483 if (err)
2484 break;
2485 }
2486 return err;
2487}
2488
2489
2490
2491
2492
2493
2494
2495
2496int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
2497 struct ext4_ext_path *path)
2498{
2499 if (path) {
2500 int depth = ext_depth(inode);
2501 int ret = 0;
2502
2503
2504 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
2505 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
2517 return ret;
2518 }
2519 }
2520
2521 return ext4_chunk_trans_blocks(inode, nrblocks);
2522}
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533int ext4_ext_index_trans_blocks(struct inode *inode, int extents)
2534{
2535 int index;
2536 int depth;
2537
2538
2539 if (ext4_has_inline_data(inode))
2540 return 1;
2541
2542 depth = ext_depth(inode);
2543
2544 if (extents <= 1)
2545 index = depth * 2;
2546 else
2547 index = depth * 3;
2548
2549 return index;
2550}
2551
2552static inline int get_default_free_blocks_flags(struct inode *inode)
2553{
2554 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) ||
2555 ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE))
2556 return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET;
2557 else if (ext4_should_journal_data(inode))
2558 return EXT4_FREE_BLOCKS_FORGET;
2559 return 0;
2560}
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577static void ext4_rereserve_cluster(struct inode *inode, ext4_lblk_t lblk)
2578{
2579 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2580 struct ext4_inode_info *ei = EXT4_I(inode);
2581
2582 dquot_reclaim_block(inode, EXT4_C2B(sbi, 1));
2583
2584 spin_lock(&ei->i_block_reservation_lock);
2585 ei->i_reserved_data_blocks++;
2586 percpu_counter_add(&sbi->s_dirtyclusters_counter, 1);
2587 spin_unlock(&ei->i_block_reservation_lock);
2588
2589 percpu_counter_add(&sbi->s_freeclusters_counter, 1);
2590 ext4_remove_pending(inode, lblk);
2591}
2592
2593static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
2594 struct ext4_extent *ex,
2595 struct partial_cluster *partial,
2596 ext4_lblk_t from, ext4_lblk_t to)
2597{
2598 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2599 unsigned short ee_len = ext4_ext_get_actual_len(ex);
2600 ext4_fsblk_t last_pblk, pblk;
2601 ext4_lblk_t num;
2602 int flags;
2603
2604
2605 if (from < le32_to_cpu(ex->ee_block) ||
2606 to != le32_to_cpu(ex->ee_block) + ee_len - 1) {
2607 ext4_error(sbi->s_sb,
2608 "strange request: removal(2) %u-%u from %u:%u",
2609 from, to, le32_to_cpu(ex->ee_block), ee_len);
2610 return 0;
2611 }
2612
2613#ifdef EXTENTS_STATS
2614 spin_lock(&sbi->s_ext_stats_lock);
2615 sbi->s_ext_blocks += ee_len;
2616 sbi->s_ext_extents++;
2617 if (ee_len < sbi->s_ext_min)
2618 sbi->s_ext_min = ee_len;
2619 if (ee_len > sbi->s_ext_max)
2620 sbi->s_ext_max = ee_len;
2621 if (ext_depth(inode) > sbi->s_depth_max)
2622 sbi->s_depth_max = ext_depth(inode);
2623 spin_unlock(&sbi->s_ext_stats_lock);
2624#endif
2625
2626 trace_ext4_remove_blocks(inode, ex, from, to, partial);
2627
2628
2629
2630
2631
2632 last_pblk = ext4_ext_pblock(ex) + ee_len - 1;
2633
2634 if (partial->state != initial &&
2635 partial->pclu != EXT4_B2C(sbi, last_pblk)) {
2636 if (partial->state == tofree) {
2637 flags = get_default_free_blocks_flags(inode);
2638 if (ext4_is_pending(inode, partial->lblk))
2639 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
2640 ext4_free_blocks(handle, inode, NULL,
2641 EXT4_C2B(sbi, partial->pclu),
2642 sbi->s_cluster_ratio, flags);
2643 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2644 ext4_rereserve_cluster(inode, partial->lblk);
2645 }
2646 partial->state = initial;
2647 }
2648
2649 num = le32_to_cpu(ex->ee_block) + ee_len - from;
2650 pblk = ext4_ext_pblock(ex) + ee_len - num;
2651
2652
2653
2654
2655
2656
2657
2658 flags = get_default_free_blocks_flags(inode);
2659
2660
2661 if ((EXT4_LBLK_COFF(sbi, to) != sbi->s_cluster_ratio - 1) &&
2662 (EXT4_LBLK_CMASK(sbi, to) >= from) &&
2663 (partial->state != nofree)) {
2664 if (ext4_is_pending(inode, to))
2665 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
2666 ext4_free_blocks(handle, inode, NULL,
2667 EXT4_PBLK_CMASK(sbi, last_pblk),
2668 sbi->s_cluster_ratio, flags);
2669 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2670 ext4_rereserve_cluster(inode, to);
2671 partial->state = initial;
2672 flags = get_default_free_blocks_flags(inode);
2673 }
2674
2675 flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER;
2676
2677
2678
2679
2680
2681
2682
2683 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
2684 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
2685
2686
2687 if (partial->state != initial && partial->pclu != EXT4_B2C(sbi, pblk))
2688 partial->state = initial;
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700 if (EXT4_LBLK_COFF(sbi, from) && num == ee_len) {
2701 if (partial->state == initial) {
2702 partial->pclu = EXT4_B2C(sbi, pblk);
2703 partial->lblk = from;
2704 partial->state = tofree;
2705 }
2706 } else {
2707 partial->state = initial;
2708 }
2709
2710 return 0;
2711}
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728static int
2729ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
2730 struct ext4_ext_path *path,
2731 struct partial_cluster *partial,
2732 ext4_lblk_t start, ext4_lblk_t end)
2733{
2734 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2735 int err = 0, correct_index = 0;
2736 int depth = ext_depth(inode), credits, revoke_credits;
2737 struct ext4_extent_header *eh;
2738 ext4_lblk_t a, b;
2739 unsigned num;
2740 ext4_lblk_t ex_ee_block;
2741 unsigned short ex_ee_len;
2742 unsigned unwritten = 0;
2743 struct ext4_extent *ex;
2744 ext4_fsblk_t pblk;
2745
2746
2747 ext_debug("truncate since %u in leaf to %u\n", start, end);
2748 if (!path[depth].p_hdr)
2749 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2750 eh = path[depth].p_hdr;
2751 if (unlikely(path[depth].p_hdr == NULL)) {
2752 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2753 return -EFSCORRUPTED;
2754 }
2755
2756 ex = path[depth].p_ext;
2757 if (!ex)
2758 ex = EXT_LAST_EXTENT(eh);
2759
2760 ex_ee_block = le32_to_cpu(ex->ee_block);
2761 ex_ee_len = ext4_ext_get_actual_len(ex);
2762
2763 trace_ext4_ext_rm_leaf(inode, start, ex, partial);
2764
2765 while (ex >= EXT_FIRST_EXTENT(eh) &&
2766 ex_ee_block + ex_ee_len > start) {
2767
2768 if (ext4_ext_is_unwritten(ex))
2769 unwritten = 1;
2770 else
2771 unwritten = 0;
2772
2773 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2774 unwritten, ex_ee_len);
2775 path[depth].p_ext = ex;
2776
2777 a = ex_ee_block > start ? ex_ee_block : start;
2778 b = ex_ee_block+ex_ee_len - 1 < end ?
2779 ex_ee_block+ex_ee_len - 1 : end;
2780
2781 ext_debug(" border %u:%u\n", a, b);
2782
2783
2784 if (end < ex_ee_block) {
2785
2786
2787
2788
2789
2790
2791
2792 if (sbi->s_cluster_ratio > 1) {
2793 pblk = ext4_ext_pblock(ex);
2794 partial->pclu = EXT4_B2C(sbi, pblk);
2795 partial->state = nofree;
2796 }
2797 ex--;
2798 ex_ee_block = le32_to_cpu(ex->ee_block);
2799 ex_ee_len = ext4_ext_get_actual_len(ex);
2800 continue;
2801 } else if (b != ex_ee_block + ex_ee_len - 1) {
2802 EXT4_ERROR_INODE(inode,
2803 "can not handle truncate %u:%u "
2804 "on extent %u:%u",
2805 start, end, ex_ee_block,
2806 ex_ee_block + ex_ee_len - 1);
2807 err = -EFSCORRUPTED;
2808 goto out;
2809 } else if (a != ex_ee_block) {
2810
2811 num = a - ex_ee_block;
2812 } else {
2813
2814 num = 0;
2815 }
2816
2817
2818
2819
2820
2821
2822 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
2823 if (ex == EXT_FIRST_EXTENT(eh)) {
2824 correct_index = 1;
2825 credits += (ext_depth(inode)) + 1;
2826 }
2827 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
2828
2829
2830
2831
2832
2833 revoke_credits =
2834 ext4_free_metadata_revoke_credits(inode->i_sb,
2835 ext_depth(inode)) +
2836 ext4_free_data_revoke_credits(inode, b - a + 1);
2837
2838 err = ext4_datasem_ensure_credits(handle, inode, credits,
2839 credits, revoke_credits);
2840 if (err) {
2841 if (err > 0)
2842 err = -EAGAIN;
2843 goto out;
2844 }
2845
2846 err = ext4_ext_get_access(handle, inode, path + depth);
2847 if (err)
2848 goto out;
2849
2850 err = ext4_remove_blocks(handle, inode, ex, partial, a, b);
2851 if (err)
2852 goto out;
2853
2854 if (num == 0)
2855
2856 ext4_ext_store_pblock(ex, 0);
2857
2858 ex->ee_len = cpu_to_le16(num);
2859
2860
2861
2862
2863 if (unwritten && num)
2864 ext4_ext_mark_unwritten(ex);
2865
2866
2867
2868
2869 if (num == 0) {
2870 if (end != EXT_MAX_BLOCKS - 1) {
2871
2872
2873
2874
2875
2876 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2877 sizeof(struct ext4_extent));
2878
2879
2880 memset(EXT_LAST_EXTENT(eh), 0,
2881 sizeof(struct ext4_extent));
2882 }
2883 le16_add_cpu(&eh->eh_entries, -1);
2884 }
2885
2886 err = ext4_ext_dirty(handle, inode, path + depth);
2887 if (err)
2888 goto out;
2889
2890 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
2891 ext4_ext_pblock(ex));
2892 ex--;
2893 ex_ee_block = le32_to_cpu(ex->ee_block);
2894 ex_ee_len = ext4_ext_get_actual_len(ex);
2895 }
2896
2897 if (correct_index && eh->eh_entries)
2898 err = ext4_ext_correct_indexes(handle, inode, path);
2899
2900
2901
2902
2903
2904
2905
2906
2907 if (partial->state == tofree && ex >= EXT_FIRST_EXTENT(eh)) {
2908 pblk = ext4_ext_pblock(ex) + ex_ee_len - 1;
2909 if (partial->pclu != EXT4_B2C(sbi, pblk)) {
2910 int flags = get_default_free_blocks_flags(inode);
2911
2912 if (ext4_is_pending(inode, partial->lblk))
2913 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
2914 ext4_free_blocks(handle, inode, NULL,
2915 EXT4_C2B(sbi, partial->pclu),
2916 sbi->s_cluster_ratio, flags);
2917 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
2918 ext4_rereserve_cluster(inode, partial->lblk);
2919 }
2920 partial->state = initial;
2921 }
2922
2923
2924
2925 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2926 err = ext4_ext_rm_idx(handle, inode, path, depth);
2927
2928out:
2929 return err;
2930}
2931
2932
2933
2934
2935
2936static int
2937ext4_ext_more_to_rm(struct ext4_ext_path *path)
2938{
2939 BUG_ON(path->p_idx == NULL);
2940
2941 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2942 return 0;
2943
2944
2945
2946
2947
2948 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2949 return 0;
2950 return 1;
2951}
2952
2953int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2954 ext4_lblk_t end)
2955{
2956 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2957 int depth = ext_depth(inode);
2958 struct ext4_ext_path *path = NULL;
2959 struct partial_cluster partial;
2960 handle_t *handle;
2961 int i = 0, err = 0;
2962
2963 partial.pclu = 0;
2964 partial.lblk = 0;
2965 partial.state = initial;
2966
2967 ext_debug("truncate since %u to %u\n", start, end);
2968
2969
2970 handle = ext4_journal_start_with_revoke(inode, EXT4_HT_TRUNCATE,
2971 depth + 1,
2972 ext4_free_metadata_revoke_credits(inode->i_sb, depth));
2973 if (IS_ERR(handle))
2974 return PTR_ERR(handle);
2975
2976again:
2977 trace_ext4_ext_remove_space(inode, start, end, depth);
2978
2979
2980
2981
2982
2983
2984
2985
2986 if (end < EXT_MAX_BLOCKS - 1) {
2987 struct ext4_extent *ex;
2988 ext4_lblk_t ee_block, ex_end, lblk;
2989 ext4_fsblk_t pblk;
2990
2991
2992 path = ext4_find_extent(inode, end, NULL, EXT4_EX_NOCACHE);
2993 if (IS_ERR(path)) {
2994 ext4_journal_stop(handle);
2995 return PTR_ERR(path);
2996 }
2997 depth = ext_depth(inode);
2998
2999 ex = path[depth].p_ext;
3000 if (!ex) {
3001 if (depth) {
3002 EXT4_ERROR_INODE(inode,
3003 "path[%d].p_hdr == NULL",
3004 depth);
3005 err = -EFSCORRUPTED;
3006 }
3007 goto out;
3008 }
3009
3010 ee_block = le32_to_cpu(ex->ee_block);
3011 ex_end = ee_block + ext4_ext_get_actual_len(ex) - 1;
3012
3013
3014
3015
3016
3017
3018
3019 if (end >= ee_block && end < ex_end) {
3020
3021
3022
3023
3024
3025
3026 if (sbi->s_cluster_ratio > 1) {
3027 pblk = ext4_ext_pblock(ex) + end - ee_block + 2;
3028 partial.pclu = EXT4_B2C(sbi, pblk);
3029 partial.state = nofree;
3030 }
3031
3032
3033
3034
3035
3036
3037
3038 err = ext4_force_split_extent_at(handle, inode, &path,
3039 end + 1, 1);
3040 if (err < 0)
3041 goto out;
3042
3043 } else if (sbi->s_cluster_ratio > 1 && end >= ex_end &&
3044 partial.state == initial) {
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055 lblk = ex_end + 1;
3056 err = ext4_ext_search_right(inode, path, &lblk, &pblk,
3057 &ex);
3058 if (err)
3059 goto out;
3060 if (pblk) {
3061 partial.pclu = EXT4_B2C(sbi, pblk);
3062 partial.state = nofree;
3063 }
3064 }
3065 }
3066
3067
3068
3069
3070 depth = ext_depth(inode);
3071 if (path) {
3072 int k = i = depth;
3073 while (--k > 0)
3074 path[k].p_block =
3075 le16_to_cpu(path[k].p_hdr->eh_entries)+1;
3076 } else {
3077 path = kcalloc(depth + 1, sizeof(struct ext4_ext_path),
3078 GFP_NOFS);
3079 if (path == NULL) {
3080 ext4_journal_stop(handle);
3081 return -ENOMEM;
3082 }
3083 path[0].p_maxdepth = path[0].p_depth = depth;
3084 path[0].p_hdr = ext_inode_hdr(inode);
3085 i = 0;
3086
3087 if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) {
3088 err = -EFSCORRUPTED;
3089 goto out;
3090 }
3091 }
3092 err = 0;
3093
3094 while (i >= 0 && err == 0) {
3095 if (i == depth) {
3096
3097 err = ext4_ext_rm_leaf(handle, inode, path,
3098 &partial, start, end);
3099
3100 brelse(path[i].p_bh);
3101 path[i].p_bh = NULL;
3102 i--;
3103 continue;
3104 }
3105
3106
3107 if (!path[i].p_hdr) {
3108 ext_debug("initialize header\n");
3109 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
3110 }
3111
3112 if (!path[i].p_idx) {
3113
3114 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
3115 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
3116 ext_debug("init index ptr: hdr 0x%p, num %d\n",
3117 path[i].p_hdr,
3118 le16_to_cpu(path[i].p_hdr->eh_entries));
3119 } else {
3120
3121 path[i].p_idx--;
3122 }
3123
3124 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
3125 i, EXT_FIRST_INDEX(path[i].p_hdr),
3126 path[i].p_idx);
3127 if (ext4_ext_more_to_rm(path + i)) {
3128 struct buffer_head *bh;
3129
3130 ext_debug("move to level %d (block %llu)\n",
3131 i + 1, ext4_idx_pblock(path[i].p_idx));
3132 memset(path + i + 1, 0, sizeof(*path));
3133 bh = read_extent_tree_block(inode,
3134 ext4_idx_pblock(path[i].p_idx), depth - i - 1,
3135 EXT4_EX_NOCACHE);
3136 if (IS_ERR(bh)) {
3137
3138 err = PTR_ERR(bh);
3139 break;
3140 }
3141
3142
3143 cond_resched();
3144 if (WARN_ON(i + 1 > depth)) {
3145 err = -EFSCORRUPTED;
3146 break;
3147 }
3148 path[i + 1].p_bh = bh;
3149
3150
3151
3152 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
3153 i++;
3154 } else {
3155
3156 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
3157
3158
3159
3160 err = ext4_ext_rm_idx(handle, inode, path, i);
3161 }
3162
3163 brelse(path[i].p_bh);
3164 path[i].p_bh = NULL;
3165 i--;
3166 ext_debug("return to level %d\n", i);
3167 }
3168 }
3169
3170 trace_ext4_ext_remove_space_done(inode, start, end, depth, &partial,
3171 path->p_hdr->eh_entries);
3172
3173
3174
3175
3176
3177 if (partial.state == tofree && err == 0) {
3178 int flags = get_default_free_blocks_flags(inode);
3179
3180 if (ext4_is_pending(inode, partial.lblk))
3181 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER;
3182 ext4_free_blocks(handle, inode, NULL,
3183 EXT4_C2B(sbi, partial.pclu),
3184 sbi->s_cluster_ratio, flags);
3185 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER)
3186 ext4_rereserve_cluster(inode, partial.lblk);
3187 partial.state = initial;
3188 }
3189
3190
3191 if (path->p_hdr->eh_entries == 0) {
3192
3193
3194
3195
3196 err = ext4_ext_get_access(handle, inode, path);
3197 if (err == 0) {
3198 ext_inode_hdr(inode)->eh_depth = 0;
3199 ext_inode_hdr(inode)->eh_max =
3200 cpu_to_le16(ext4_ext_space_root(inode, 0));
3201 err = ext4_ext_dirty(handle, inode, path);
3202 }
3203 }
3204out:
3205 ext4_ext_drop_refs(path);
3206 kfree(path);
3207 path = NULL;
3208 if (err == -EAGAIN)
3209 goto again;
3210 ext4_journal_stop(handle);
3211
3212 return err;
3213}
3214
3215
3216
3217
3218void ext4_ext_init(struct super_block *sb)
3219{
3220
3221
3222
3223
3224 if (ext4_has_feature_extents(sb)) {
3225#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
3226 printk(KERN_INFO "EXT4-fs: file extents enabled"
3227#ifdef AGGRESSIVE_TEST
3228 ", aggressive tests"
3229#endif
3230#ifdef CHECK_BINSEARCH
3231 ", check binsearch"
3232#endif
3233#ifdef EXTENTS_STATS
3234 ", stats"
3235#endif
3236 "\n");
3237#endif
3238#ifdef EXTENTS_STATS
3239 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
3240 EXT4_SB(sb)->s_ext_min = 1 << 30;
3241 EXT4_SB(sb)->s_ext_max = 0;
3242#endif
3243 }
3244}
3245
3246
3247
3248
3249void ext4_ext_release(struct super_block *sb)
3250{
3251 if (!ext4_has_feature_extents(sb))
3252 return;
3253
3254#ifdef EXTENTS_STATS
3255 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
3256 struct ext4_sb_info *sbi = EXT4_SB(sb);
3257 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
3258 sbi->s_ext_blocks, sbi->s_ext_extents,
3259 sbi->s_ext_blocks / sbi->s_ext_extents);
3260 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
3261 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
3262 }
3263#endif
3264}
3265
3266static int ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex)
3267{
3268 ext4_lblk_t ee_block;
3269 ext4_fsblk_t ee_pblock;
3270 unsigned int ee_len;
3271
3272 ee_block = le32_to_cpu(ex->ee_block);
3273 ee_len = ext4_ext_get_actual_len(ex);
3274 ee_pblock = ext4_ext_pblock(ex);
3275
3276 if (ee_len == 0)
3277 return 0;
3278
3279 return ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock,
3280 EXTENT_STATUS_WRITTEN);
3281}
3282
3283
3284static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
3285{
3286 ext4_fsblk_t ee_pblock;
3287 unsigned int ee_len;
3288
3289 ee_len = ext4_ext_get_actual_len(ex);
3290 ee_pblock = ext4_ext_pblock(ex);
3291 return ext4_issue_zeroout(inode, le32_to_cpu(ex->ee_block), ee_pblock,
3292 ee_len);
3293}
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316static int ext4_split_extent_at(handle_t *handle,
3317 struct inode *inode,
3318 struct ext4_ext_path **ppath,
3319 ext4_lblk_t split,
3320 int split_flag,
3321 int flags)
3322{
3323 struct ext4_ext_path *path = *ppath;
3324 ext4_fsblk_t newblock;
3325 ext4_lblk_t ee_block;
3326 struct ext4_extent *ex, newex, orig_ex, zero_ex;
3327 struct ext4_extent *ex2 = NULL;
3328 unsigned int ee_len, depth;
3329 int err = 0;
3330
3331 BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) ==
3332 (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2));
3333
3334 ext_debug("ext4_split_extents_at: inode %lu, logical"
3335 "block %llu\n", inode->i_ino, (unsigned long long)split);
3336
3337 ext4_ext_show_leaf(inode, path);
3338
3339 depth = ext_depth(inode);
3340 ex = path[depth].p_ext;
3341 ee_block = le32_to_cpu(ex->ee_block);
3342 ee_len = ext4_ext_get_actual_len(ex);
3343 newblock = split - ee_block + ext4_ext_pblock(ex);
3344
3345 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
3346 BUG_ON(!ext4_ext_is_unwritten(ex) &&
3347 split_flag & (EXT4_EXT_MAY_ZEROOUT |
3348 EXT4_EXT_MARK_UNWRIT1 |
3349 EXT4_EXT_MARK_UNWRIT2));
3350
3351 err = ext4_ext_get_access(handle, inode, path + depth);
3352 if (err)
3353 goto out;
3354
3355 if (split == ee_block) {
3356
3357
3358
3359
3360
3361 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3362 ext4_ext_mark_unwritten(ex);
3363 else
3364 ext4_ext_mark_initialized(ex);
3365
3366 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
3367 ext4_ext_try_to_merge(handle, inode, path, ex);
3368
3369 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
3370 goto out;
3371 }
3372
3373
3374 memcpy(&orig_ex, ex, sizeof(orig_ex));
3375 ex->ee_len = cpu_to_le16(split - ee_block);
3376 if (split_flag & EXT4_EXT_MARK_UNWRIT1)
3377 ext4_ext_mark_unwritten(ex);
3378
3379
3380
3381
3382
3383 err = ext4_ext_dirty(handle, inode, path + depth);
3384 if (err)
3385 goto fix_extent_len;
3386
3387 ex2 = &newex;
3388 ex2->ee_block = cpu_to_le32(split);
3389 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
3390 ext4_ext_store_pblock(ex2, newblock);
3391 if (split_flag & EXT4_EXT_MARK_UNWRIT2)
3392 ext4_ext_mark_unwritten(ex2);
3393
3394 err = ext4_ext_insert_extent(handle, inode, ppath, &newex, flags);
3395 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3396 if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) {
3397 if (split_flag & EXT4_EXT_DATA_VALID1) {
3398 err = ext4_ext_zeroout(inode, ex2);
3399 zero_ex.ee_block = ex2->ee_block;
3400 zero_ex.ee_len = cpu_to_le16(
3401 ext4_ext_get_actual_len(ex2));
3402 ext4_ext_store_pblock(&zero_ex,
3403 ext4_ext_pblock(ex2));
3404 } else {
3405 err = ext4_ext_zeroout(inode, ex);
3406 zero_ex.ee_block = ex->ee_block;
3407 zero_ex.ee_len = cpu_to_le16(
3408 ext4_ext_get_actual_len(ex));
3409 ext4_ext_store_pblock(&zero_ex,
3410 ext4_ext_pblock(ex));
3411 }
3412 } else {
3413 err = ext4_ext_zeroout(inode, &orig_ex);
3414 zero_ex.ee_block = orig_ex.ee_block;
3415 zero_ex.ee_len = cpu_to_le16(
3416 ext4_ext_get_actual_len(&orig_ex));
3417 ext4_ext_store_pblock(&zero_ex,
3418 ext4_ext_pblock(&orig_ex));
3419 }
3420
3421 if (err)
3422 goto fix_extent_len;
3423
3424 ex->ee_len = cpu_to_le16(ee_len);
3425 ext4_ext_try_to_merge(handle, inode, path, ex);
3426 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
3427 if (err)
3428 goto fix_extent_len;
3429
3430
3431 err = ext4_zeroout_es(inode, &zero_ex);
3432
3433 goto out;
3434 } else if (err)
3435 goto fix_extent_len;
3436
3437out:
3438 ext4_ext_show_leaf(inode, path);
3439 return err;
3440
3441fix_extent_len:
3442 ex->ee_len = orig_ex.ee_len;
3443 ext4_ext_dirty(handle, inode, path + path->p_depth);
3444 return err;
3445}
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458static int ext4_split_extent(handle_t *handle,
3459 struct inode *inode,
3460 struct ext4_ext_path **ppath,
3461 struct ext4_map_blocks *map,
3462 int split_flag,
3463 int flags)
3464{
3465 struct ext4_ext_path *path = *ppath;
3466 ext4_lblk_t ee_block;
3467 struct ext4_extent *ex;
3468 unsigned int ee_len, depth;
3469 int err = 0;
3470 int unwritten;
3471 int split_flag1, flags1;
3472 int allocated = map->m_len;
3473
3474 depth = ext_depth(inode);
3475 ex = path[depth].p_ext;
3476 ee_block = le32_to_cpu(ex->ee_block);
3477 ee_len = ext4_ext_get_actual_len(ex);
3478 unwritten = ext4_ext_is_unwritten(ex);
3479
3480 if (map->m_lblk + map->m_len < ee_block + ee_len) {
3481 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT;
3482 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
3483 if (unwritten)
3484 split_flag1 |= EXT4_EXT_MARK_UNWRIT1 |
3485 EXT4_EXT_MARK_UNWRIT2;
3486 if (split_flag & EXT4_EXT_DATA_VALID2)
3487 split_flag1 |= EXT4_EXT_DATA_VALID1;
3488 err = ext4_split_extent_at(handle, inode, ppath,
3489 map->m_lblk + map->m_len, split_flag1, flags1);
3490 if (err)
3491 goto out;
3492 } else {
3493 allocated = ee_len - (map->m_lblk - ee_block);
3494 }
3495
3496
3497
3498
3499 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
3500 if (IS_ERR(path))
3501 return PTR_ERR(path);
3502 depth = ext_depth(inode);
3503 ex = path[depth].p_ext;
3504 if (!ex) {
3505 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
3506 (unsigned long) map->m_lblk);
3507 return -EFSCORRUPTED;
3508 }
3509 unwritten = ext4_ext_is_unwritten(ex);
3510 split_flag1 = 0;
3511
3512 if (map->m_lblk >= ee_block) {
3513 split_flag1 = split_flag & EXT4_EXT_DATA_VALID2;
3514 if (unwritten) {
3515 split_flag1 |= EXT4_EXT_MARK_UNWRIT1;
3516 split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT |
3517 EXT4_EXT_MARK_UNWRIT2);
3518 }
3519 err = ext4_split_extent_at(handle, inode, ppath,
3520 map->m_lblk, split_flag1, flags);
3521 if (err)
3522 goto out;
3523 }
3524
3525 ext4_ext_show_leaf(inode, path);
3526out:
3527 return err ? err : allocated;
3528}
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550static int ext4_ext_convert_to_initialized(handle_t *handle,
3551 struct inode *inode,
3552 struct ext4_map_blocks *map,
3553 struct ext4_ext_path **ppath,
3554 int flags)
3555{
3556 struct ext4_ext_path *path = *ppath;
3557 struct ext4_sb_info *sbi;
3558 struct ext4_extent_header *eh;
3559 struct ext4_map_blocks split_map;
3560 struct ext4_extent zero_ex1, zero_ex2;
3561 struct ext4_extent *ex, *abut_ex;
3562 ext4_lblk_t ee_block, eof_block;
3563 unsigned int ee_len, depth, map_len = map->m_len;
3564 int allocated = 0, max_zeroout = 0;
3565 int err = 0;
3566 int split_flag = EXT4_EXT_DATA_VALID2;
3567
3568 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3569 "block %llu, max_blocks %u\n", inode->i_ino,
3570 (unsigned long long)map->m_lblk, map_len);
3571
3572 sbi = EXT4_SB(inode->i_sb);
3573 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3574 inode->i_sb->s_blocksize_bits;
3575 if (eof_block < map->m_lblk + map_len)
3576 eof_block = map->m_lblk + map_len;
3577
3578 depth = ext_depth(inode);
3579 eh = path[depth].p_hdr;
3580 ex = path[depth].p_ext;
3581 ee_block = le32_to_cpu(ex->ee_block);
3582 ee_len = ext4_ext_get_actual_len(ex);
3583 zero_ex1.ee_len = 0;
3584 zero_ex2.ee_len = 0;
3585
3586 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
3587
3588
3589 BUG_ON(!ext4_ext_is_unwritten(ex));
3590 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607 if ((map->m_lblk == ee_block) &&
3608
3609 (map_len < ee_len) &&
3610 (ex > EXT_FIRST_EXTENT(eh))) {
3611 ext4_lblk_t prev_lblk;
3612 ext4_fsblk_t prev_pblk, ee_pblk;
3613 unsigned int prev_len;
3614
3615 abut_ex = ex - 1;
3616 prev_lblk = le32_to_cpu(abut_ex->ee_block);
3617 prev_len = ext4_ext_get_actual_len(abut_ex);
3618 prev_pblk = ext4_ext_pblock(abut_ex);
3619 ee_pblk = ext4_ext_pblock(ex);
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630 if ((!ext4_ext_is_unwritten(abut_ex)) &&
3631 ((prev_lblk + prev_len) == ee_block) &&
3632 ((prev_pblk + prev_len) == ee_pblk) &&
3633 (prev_len < (EXT_INIT_MAX_LEN - map_len))) {
3634 err = ext4_ext_get_access(handle, inode, path + depth);
3635 if (err)
3636 goto out;
3637
3638 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3639 map, ex, abut_ex);
3640
3641
3642 ex->ee_block = cpu_to_le32(ee_block + map_len);
3643 ext4_ext_store_pblock(ex, ee_pblk + map_len);
3644 ex->ee_len = cpu_to_le16(ee_len - map_len);
3645 ext4_ext_mark_unwritten(ex);
3646
3647
3648 abut_ex->ee_len = cpu_to_le16(prev_len + map_len);
3649
3650
3651 allocated = map_len;
3652 }
3653 } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) &&
3654 (map_len < ee_len) &&
3655 ex < EXT_LAST_EXTENT(eh)) {
3656
3657 ext4_lblk_t next_lblk;
3658 ext4_fsblk_t next_pblk, ee_pblk;
3659 unsigned int next_len;
3660
3661 abut_ex = ex + 1;
3662 next_lblk = le32_to_cpu(abut_ex->ee_block);
3663 next_len = ext4_ext_get_actual_len(abut_ex);
3664 next_pblk = ext4_ext_pblock(abut_ex);
3665 ee_pblk = ext4_ext_pblock(ex);
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676 if ((!ext4_ext_is_unwritten(abut_ex)) &&
3677 ((map->m_lblk + map_len) == next_lblk) &&
3678 ((ee_pblk + ee_len) == next_pblk) &&
3679 (next_len < (EXT_INIT_MAX_LEN - map_len))) {
3680 err = ext4_ext_get_access(handle, inode, path + depth);
3681 if (err)
3682 goto out;
3683
3684 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3685 map, ex, abut_ex);
3686
3687
3688 abut_ex->ee_block = cpu_to_le32(next_lblk - map_len);
3689 ext4_ext_store_pblock(abut_ex, next_pblk - map_len);
3690 ex->ee_len = cpu_to_le16(ee_len - map_len);
3691 ext4_ext_mark_unwritten(ex);
3692
3693
3694 abut_ex->ee_len = cpu_to_le16(next_len + map_len);
3695
3696
3697 allocated = map_len;
3698 }
3699 }
3700 if (allocated) {
3701
3702 ext4_ext_dirty(handle, inode, path + depth);
3703
3704
3705 path[depth].p_ext = abut_ex;
3706 goto out;
3707 } else
3708 allocated = ee_len - (map->m_lblk - ee_block);
3709
3710 WARN_ON(map->m_lblk < ee_block);
3711
3712
3713
3714
3715 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
3716
3717 if (EXT4_EXT_MAY_ZEROOUT & split_flag)
3718 max_zeroout = sbi->s_extent_max_zeroout_kb >>
3719 (inode->i_sb->s_blocksize_bits - 10);
3720
3721 if (IS_ENCRYPTED(inode))
3722 max_zeroout = 0;
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735 split_map.m_lblk = map->m_lblk;
3736 split_map.m_len = map->m_len;
3737
3738 if (max_zeroout && (allocated > split_map.m_len)) {
3739 if (allocated <= max_zeroout) {
3740
3741 zero_ex1.ee_block =
3742 cpu_to_le32(split_map.m_lblk +
3743 split_map.m_len);
3744 zero_ex1.ee_len =
3745 cpu_to_le16(allocated - split_map.m_len);
3746 ext4_ext_store_pblock(&zero_ex1,
3747 ext4_ext_pblock(ex) + split_map.m_lblk +
3748 split_map.m_len - ee_block);
3749 err = ext4_ext_zeroout(inode, &zero_ex1);
3750 if (err)
3751 goto out;
3752 split_map.m_len = allocated;
3753 }
3754 if (split_map.m_lblk - ee_block + split_map.m_len <
3755 max_zeroout) {
3756
3757 if (split_map.m_lblk != ee_block) {
3758 zero_ex2.ee_block = ex->ee_block;
3759 zero_ex2.ee_len = cpu_to_le16(split_map.m_lblk -
3760 ee_block);
3761 ext4_ext_store_pblock(&zero_ex2,
3762 ext4_ext_pblock(ex));
3763 err = ext4_ext_zeroout(inode, &zero_ex2);
3764 if (err)
3765 goto out;
3766 }
3767
3768 split_map.m_len += split_map.m_lblk - ee_block;
3769 split_map.m_lblk = ee_block;
3770 allocated = map->m_len;
3771 }
3772 }
3773
3774 err = ext4_split_extent(handle, inode, ppath, &split_map, split_flag,
3775 flags);
3776 if (err > 0)
3777 err = 0;
3778out:
3779
3780 if (!err) {
3781 err = ext4_zeroout_es(inode, &zero_ex1);
3782 if (!err)
3783 err = ext4_zeroout_es(inode, &zero_ex2);
3784 }
3785 return err ? err : allocated;
3786}
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812static int ext4_split_convert_extents(handle_t *handle,
3813 struct inode *inode,
3814 struct ext4_map_blocks *map,
3815 struct ext4_ext_path **ppath,
3816 int flags)
3817{
3818 struct ext4_ext_path *path = *ppath;
3819 ext4_lblk_t eof_block;
3820 ext4_lblk_t ee_block;
3821 struct ext4_extent *ex;
3822 unsigned int ee_len;
3823 int split_flag = 0, depth;
3824
3825 ext_debug("%s: inode %lu, logical block %llu, max_blocks %u\n",
3826 __func__, inode->i_ino,
3827 (unsigned long long)map->m_lblk, map->m_len);
3828
3829 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3830 inode->i_sb->s_blocksize_bits;
3831 if (eof_block < map->m_lblk + map->m_len)
3832 eof_block = map->m_lblk + map->m_len;
3833
3834
3835
3836
3837 depth = ext_depth(inode);
3838 ex = path[depth].p_ext;
3839 ee_block = le32_to_cpu(ex->ee_block);
3840 ee_len = ext4_ext_get_actual_len(ex);
3841
3842
3843 if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) {
3844 split_flag |= EXT4_EXT_DATA_VALID1;
3845
3846 } else if (flags & EXT4_GET_BLOCKS_CONVERT) {
3847 split_flag |= ee_block + ee_len <= eof_block ?
3848 EXT4_EXT_MAY_ZEROOUT : 0;
3849 split_flag |= (EXT4_EXT_MARK_UNWRIT2 | EXT4_EXT_DATA_VALID2);
3850 }
3851 flags |= EXT4_GET_BLOCKS_PRE_IO;
3852 return ext4_split_extent(handle, inode, ppath, map, split_flag, flags);
3853}
3854
3855static int ext4_convert_unwritten_extents_endio(handle_t *handle,
3856 struct inode *inode,
3857 struct ext4_map_blocks *map,
3858 struct ext4_ext_path **ppath)
3859{
3860 struct ext4_ext_path *path = *ppath;
3861 struct ext4_extent *ex;
3862 ext4_lblk_t ee_block;
3863 unsigned int ee_len;
3864 int depth;
3865 int err = 0;
3866
3867 depth = ext_depth(inode);
3868 ex = path[depth].p_ext;
3869 ee_block = le32_to_cpu(ex->ee_block);
3870 ee_len = ext4_ext_get_actual_len(ex);
3871
3872 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3873 "block %llu, max_blocks %u\n", inode->i_ino,
3874 (unsigned long long)ee_block, ee_len);
3875
3876
3877
3878
3879
3880
3881
3882 if (ee_block != map->m_lblk || ee_len > map->m_len) {
3883#ifdef CONFIG_EXT4_DEBUG
3884 ext4_warning(inode->i_sb, "Inode (%ld) finished: extent logical block %llu,"
3885 " len %u; IO logical block %llu, len %u",
3886 inode->i_ino, (unsigned long long)ee_block, ee_len,
3887 (unsigned long long)map->m_lblk, map->m_len);
3888#endif
3889 err = ext4_split_convert_extents(handle, inode, map, ppath,
3890 EXT4_GET_BLOCKS_CONVERT);
3891 if (err < 0)
3892 return err;
3893 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
3894 if (IS_ERR(path))
3895 return PTR_ERR(path);
3896 depth = ext_depth(inode);
3897 ex = path[depth].p_ext;
3898 }
3899
3900 err = ext4_ext_get_access(handle, inode, path + depth);
3901 if (err)
3902 goto out;
3903
3904 ext4_ext_mark_initialized(ex);
3905
3906
3907
3908
3909 ext4_ext_try_to_merge(handle, inode, path, ex);
3910
3911
3912 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
3913out:
3914 ext4_ext_show_leaf(inode, path);
3915 return err;
3916}
3917
3918
3919
3920
3921static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
3922 ext4_lblk_t lblk,
3923 struct ext4_ext_path *path,
3924 unsigned int len)
3925{
3926 int i, depth;
3927 struct ext4_extent_header *eh;
3928 struct ext4_extent *last_ex;
3929
3930 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3931 return 0;
3932
3933 depth = ext_depth(inode);
3934 eh = path[depth].p_hdr;
3935
3936
3937
3938
3939
3940
3941 if (unlikely(!eh->eh_entries))
3942 goto out;
3943 last_ex = EXT_LAST_EXTENT(eh);
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
3954 ext4_ext_get_actual_len(last_ex))
3955 return 0;
3956
3957
3958
3959
3960
3961
3962
3963 for (i = depth-1; i >= 0; i--)
3964 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3965 return 0;
3966out:
3967 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3968 return ext4_mark_inode_dirty(handle, inode);
3969}
3970
3971static int
3972convert_initialized_extent(handle_t *handle, struct inode *inode,
3973 struct ext4_map_blocks *map,
3974 struct ext4_ext_path **ppath,
3975 unsigned int allocated)
3976{
3977 struct ext4_ext_path *path = *ppath;
3978 struct ext4_extent *ex;
3979 ext4_lblk_t ee_block;
3980 unsigned int ee_len;
3981 int depth;
3982 int err = 0;
3983
3984
3985
3986
3987
3988 if (map->m_len > EXT_UNWRITTEN_MAX_LEN)
3989 map->m_len = EXT_UNWRITTEN_MAX_LEN / 2;
3990
3991 depth = ext_depth(inode);
3992 ex = path[depth].p_ext;
3993 ee_block = le32_to_cpu(ex->ee_block);
3994 ee_len = ext4_ext_get_actual_len(ex);
3995
3996 ext_debug("%s: inode %lu, logical"
3997 "block %llu, max_blocks %u\n", __func__, inode->i_ino,
3998 (unsigned long long)ee_block, ee_len);
3999
4000 if (ee_block != map->m_lblk || ee_len > map->m_len) {
4001 err = ext4_split_convert_extents(handle, inode, map, ppath,
4002 EXT4_GET_BLOCKS_CONVERT_UNWRITTEN);
4003 if (err < 0)
4004 return err;
4005 path = ext4_find_extent(inode, map->m_lblk, ppath, 0);
4006 if (IS_ERR(path))
4007 return PTR_ERR(path);
4008 depth = ext_depth(inode);
4009 ex = path[depth].p_ext;
4010 if (!ex) {
4011 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
4012 (unsigned long) map->m_lblk);
4013 return -EFSCORRUPTED;
4014 }
4015 }
4016
4017 err = ext4_ext_get_access(handle, inode, path + depth);
4018 if (err)
4019 return err;
4020
4021 ext4_ext_mark_unwritten(ex);
4022
4023
4024
4025
4026 ext4_ext_try_to_merge(handle, inode, path, ex);
4027
4028
4029 err = ext4_ext_dirty(handle, inode, path + path->p_depth);
4030 if (err)
4031 return err;
4032 ext4_ext_show_leaf(inode, path);
4033
4034 ext4_update_inode_fsync_trans(handle, inode, 1);
4035 err = check_eofblocks_fl(handle, inode, map->m_lblk, path, map->m_len);
4036 if (err)
4037 return err;
4038 map->m_flags |= EXT4_MAP_UNWRITTEN;
4039 if (allocated > map->m_len)
4040 allocated = map->m_len;
4041 map->m_len = allocated;
4042 return allocated;
4043}
4044
4045static int
4046ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode,
4047 struct ext4_map_blocks *map,
4048 struct ext4_ext_path **ppath, int flags,
4049 unsigned int allocated, ext4_fsblk_t newblock)
4050{
4051 struct ext4_ext_path *path = *ppath;
4052 int ret = 0;
4053 int err = 0;
4054
4055 ext_debug("ext4_ext_handle_unwritten_extents: inode %lu, logical "
4056 "block %llu, max_blocks %u, flags %x, allocated %u\n",
4057 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
4058 flags, allocated);
4059 ext4_ext_show_leaf(inode, path);
4060
4061
4062
4063
4064
4065 flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL;
4066
4067 trace_ext4_ext_handle_unwritten_extents(inode, map, flags,
4068 allocated, newblock);
4069
4070
4071 if (flags & EXT4_GET_BLOCKS_PRE_IO) {
4072 ret = ext4_split_convert_extents(handle, inode, map, ppath,
4073 flags | EXT4_GET_BLOCKS_CONVERT);
4074 if (ret <= 0)
4075 goto out;
4076 map->m_flags |= EXT4_MAP_UNWRITTEN;
4077 goto out;
4078 }
4079
4080 if (flags & EXT4_GET_BLOCKS_CONVERT) {
4081 if (flags & EXT4_GET_BLOCKS_ZERO) {
4082 if (allocated > map->m_len)
4083 allocated = map->m_len;
4084 err = ext4_issue_zeroout(inode, map->m_lblk, newblock,
4085 allocated);
4086 if (err < 0)
4087 goto out2;
4088 }
4089 ret = ext4_convert_unwritten_extents_endio(handle, inode, map,
4090 ppath);
4091 if (ret >= 0) {
4092 ext4_update_inode_fsync_trans(handle, inode, 1);
4093 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4094 path, map->m_len);
4095 } else
4096 err = ret;
4097 map->m_flags |= EXT4_MAP_MAPPED;
4098 map->m_pblk = newblock;
4099 if (allocated > map->m_len)
4100 allocated = map->m_len;
4101 map->m_len = allocated;
4102 goto out2;
4103 }
4104
4105
4106
4107
4108
4109 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) {
4110 map->m_flags |= EXT4_MAP_UNWRITTEN;
4111 goto map_out;
4112 }
4113
4114
4115 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
4116
4117
4118
4119
4120
4121
4122
4123 map->m_flags |= EXT4_MAP_UNWRITTEN;
4124 goto out1;
4125 }
4126
4127
4128 ret = ext4_ext_convert_to_initialized(handle, inode, map, ppath, flags);
4129 if (ret >= 0)
4130 ext4_update_inode_fsync_trans(handle, inode, 1);
4131out:
4132 if (ret <= 0) {
4133 err = ret;
4134 goto out2;
4135 } else
4136 allocated = ret;
4137 map->m_flags |= EXT4_MAP_NEW;
4138 if (allocated > map->m_len)
4139 allocated = map->m_len;
4140 map->m_len = allocated;
4141
4142map_out:
4143 map->m_flags |= EXT4_MAP_MAPPED;
4144 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
4145 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
4146 map->m_len);
4147 if (err < 0)
4148 goto out2;
4149 }
4150out1:
4151 if (allocated > map->m_len)
4152 allocated = map->m_len;
4153 ext4_ext_show_leaf(inode, path);
4154 map->m_pblk = newblock;
4155 map->m_len = allocated;
4156out2:
4157 return err ? err : allocated;
4158}
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201static int get_implied_cluster_alloc(struct super_block *sb,
4202 struct ext4_map_blocks *map,
4203 struct ext4_extent *ex,
4204 struct ext4_ext_path *path)
4205{
4206 struct ext4_sb_info *sbi = EXT4_SB(sb);
4207 ext4_lblk_t c_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
4208 ext4_lblk_t ex_cluster_start, ex_cluster_end;
4209 ext4_lblk_t rr_cluster_start;
4210 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
4211 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
4212 unsigned short ee_len = ext4_ext_get_actual_len(ex);
4213
4214
4215 ex_cluster_start = EXT4_B2C(sbi, ee_block);
4216 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
4217
4218
4219 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
4220
4221 if ((rr_cluster_start == ex_cluster_end) ||
4222 (rr_cluster_start == ex_cluster_start)) {
4223 if (rr_cluster_start == ex_cluster_end)
4224 ee_start += ee_len - 1;
4225 map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset;
4226 map->m_len = min(map->m_len,
4227 (unsigned) sbi->s_cluster_ratio - c_offset);
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237 if (map->m_lblk < ee_block)
4238 map->m_len = min(map->m_len, ee_block - map->m_lblk);
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249 if (map->m_lblk > ee_block) {
4250 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
4251 map->m_len = min(map->m_len, next - map->m_lblk);
4252 }
4253
4254 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
4255 return 1;
4256 }
4257
4258 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
4259 return 0;
4260}
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
4282 struct ext4_map_blocks *map, int flags)
4283{
4284 struct ext4_ext_path *path = NULL;
4285 struct ext4_extent newex, *ex, *ex2;
4286 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4287 ext4_fsblk_t newblock = 0;
4288 int free_on_err = 0, err = 0, depth, ret;
4289 unsigned int allocated = 0, offset = 0;
4290 unsigned int allocated_clusters = 0;
4291 struct ext4_allocation_request ar;
4292 ext4_lblk_t cluster_offset;
4293 bool map_from_cluster = false;
4294
4295 ext_debug("blocks %u/%u requested for inode %lu\n",
4296 map->m_lblk, map->m_len, inode->i_ino);
4297 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
4298
4299
4300 path = ext4_find_extent(inode, map->m_lblk, NULL, 0);
4301 if (IS_ERR(path)) {
4302 err = PTR_ERR(path);
4303 path = NULL;
4304 goto out2;
4305 }
4306
4307 depth = ext_depth(inode);
4308
4309
4310
4311
4312
4313
4314 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
4315 EXT4_ERROR_INODE(inode, "bad extent address "
4316 "lblock: %lu, depth: %d pblock %lld",
4317 (unsigned long) map->m_lblk, depth,
4318 path[depth].p_block);
4319 err = -EFSCORRUPTED;
4320 goto out2;
4321 }
4322
4323 ex = path[depth].p_ext;
4324 if (ex) {
4325 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
4326 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
4327 unsigned short ee_len;
4328
4329
4330
4331
4332
4333
4334 ee_len = ext4_ext_get_actual_len(ex);
4335
4336 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
4337
4338
4339 if (in_range(map->m_lblk, ee_block, ee_len)) {
4340 newblock = map->m_lblk - ee_block + ee_start;
4341
4342 allocated = ee_len - (map->m_lblk - ee_block);
4343 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
4344 ee_block, ee_len, newblock);
4345
4346
4347
4348
4349
4350 if ((!ext4_ext_is_unwritten(ex)) &&
4351 (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) {
4352 allocated = convert_initialized_extent(
4353 handle, inode, map, &path,
4354 allocated);
4355 goto out2;
4356 } else if (!ext4_ext_is_unwritten(ex))
4357 goto out;
4358
4359 ret = ext4_ext_handle_unwritten_extents(
4360 handle, inode, map, &path, flags,
4361 allocated, newblock);
4362 if (ret < 0)
4363 err = ret;
4364 else
4365 allocated = ret;
4366 goto out2;
4367 }
4368 }
4369
4370
4371
4372
4373
4374 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
4375 ext4_lblk_t hole_start, hole_len;
4376
4377 hole_start = map->m_lblk;
4378 hole_len = ext4_ext_determine_hole(inode, path, &hole_start);
4379
4380
4381
4382
4383 ext4_ext_put_gap_in_cache(inode, hole_start, hole_len);
4384
4385
4386 if (hole_start != map->m_lblk)
4387 hole_len -= map->m_lblk - hole_start;
4388 map->m_pblk = 0;
4389 map->m_len = min_t(unsigned int, map->m_len, hole_len);
4390
4391 goto out2;
4392 }
4393
4394
4395
4396
4397 newex.ee_block = cpu_to_le32(map->m_lblk);
4398 cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
4399
4400
4401
4402
4403
4404 if (cluster_offset && ex &&
4405 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
4406 ar.len = allocated = map->m_len;
4407 newblock = map->m_pblk;
4408 map_from_cluster = true;
4409 goto got_allocated_blocks;
4410 }
4411
4412
4413 ar.lleft = map->m_lblk;
4414 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
4415 if (err)
4416 goto out2;
4417 ar.lright = map->m_lblk;
4418 ex2 = NULL;
4419 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
4420 if (err)
4421 goto out2;
4422
4423
4424
4425 if ((sbi->s_cluster_ratio > 1) && ex2 &&
4426 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
4427 ar.len = allocated = map->m_len;
4428 newblock = map->m_pblk;
4429 map_from_cluster = true;
4430 goto got_allocated_blocks;
4431 }
4432
4433
4434
4435
4436
4437
4438
4439 if (map->m_len > EXT_INIT_MAX_LEN &&
4440 !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
4441 map->m_len = EXT_INIT_MAX_LEN;
4442 else if (map->m_len > EXT_UNWRITTEN_MAX_LEN &&
4443 (flags & EXT4_GET_BLOCKS_UNWRIT_EXT))
4444 map->m_len = EXT_UNWRITTEN_MAX_LEN;
4445
4446
4447 newex.ee_len = cpu_to_le16(map->m_len);
4448 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
4449 if (err)
4450 allocated = ext4_ext_get_actual_len(&newex);
4451 else
4452 allocated = map->m_len;
4453
4454
4455 ar.inode = inode;
4456 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
4457 ar.logical = map->m_lblk;
4458
4459
4460
4461
4462
4463
4464
4465
4466 offset = EXT4_LBLK_COFF(sbi, map->m_lblk);
4467 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
4468 ar.goal -= offset;
4469 ar.logical -= offset;
4470 if (S_ISREG(inode->i_mode))
4471 ar.flags = EXT4_MB_HINT_DATA;
4472 else
4473
4474 ar.flags = 0;
4475 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4476 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
4477 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
4478 ar.flags |= EXT4_MB_DELALLOC_RESERVED;
4479 if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
4480 ar.flags |= EXT4_MB_USE_RESERVED;
4481 newblock = ext4_mb_new_blocks(handle, &ar, &err);
4482 if (!newblock)
4483 goto out2;
4484 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
4485 ar.goal, newblock, allocated);
4486 free_on_err = 1;
4487 allocated_clusters = ar.len;
4488 ar.len = EXT4_C2B(sbi, ar.len) - offset;
4489 if (ar.len > allocated)
4490 ar.len = allocated;
4491
4492got_allocated_blocks:
4493
4494 ext4_ext_store_pblock(&newex, newblock + offset);
4495 newex.ee_len = cpu_to_le16(ar.len);
4496
4497 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT){
4498 ext4_ext_mark_unwritten(&newex);
4499 map->m_flags |= EXT4_MAP_UNWRITTEN;
4500 }
4501
4502 err = 0;
4503 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
4504 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4505 path, ar.len);
4506 if (!err)
4507 err = ext4_ext_insert_extent(handle, inode, &path,
4508 &newex, flags);
4509
4510 if (err && free_on_err) {
4511 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
4512 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
4513
4514
4515
4516 ext4_discard_preallocations(inode);
4517 ext4_free_blocks(handle, inode, NULL, newblock,
4518 EXT4_C2B(sbi, allocated_clusters), fb_flags);
4519 goto out2;
4520 }
4521
4522
4523 newblock = ext4_ext_pblock(&newex);
4524 allocated = ext4_ext_get_actual_len(&newex);
4525 if (allocated > map->m_len)
4526 allocated = map->m_len;
4527 map->m_flags |= EXT4_MAP_NEW;
4528
4529
4530
4531
4532
4533
4534
4535 if (test_opt(inode->i_sb, DELALLOC) && !map_from_cluster) {
4536 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
4537
4538
4539
4540
4541 ext4_da_update_reserve_space(inode, allocated_clusters,
4542 1);
4543 } else {
4544 ext4_lblk_t lblk, len;
4545 unsigned int n;
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558 lblk = EXT4_LBLK_CMASK(sbi, map->m_lblk);
4559 len = allocated_clusters << sbi->s_cluster_bits;
4560 n = ext4_es_delayed_clu(inode, lblk, len);
4561 if (n > 0)
4562 ext4_da_update_reserve_space(inode, (int) n, 0);
4563 }
4564 }
4565
4566
4567
4568
4569
4570 if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0)
4571 ext4_update_inode_fsync_trans(handle, inode, 1);
4572 else
4573 ext4_update_inode_fsync_trans(handle, inode, 0);
4574out:
4575 if (allocated > map->m_len)
4576 allocated = map->m_len;
4577 ext4_ext_show_leaf(inode, path);
4578 map->m_flags |= EXT4_MAP_MAPPED;
4579 map->m_pblk = newblock;
4580 map->m_len = allocated;
4581out2:
4582 ext4_ext_drop_refs(path);
4583 kfree(path);
4584
4585 trace_ext4_ext_map_blocks_exit(inode, flags, map,
4586 err ? err : allocated);
4587 return err ? err : allocated;
4588}
4589
4590int ext4_ext_truncate(handle_t *handle, struct inode *inode)
4591{
4592 struct super_block *sb = inode->i_sb;
4593 ext4_lblk_t last_block;
4594 int err = 0;
4595
4596
4597
4598
4599
4600
4601
4602
4603 EXT4_I(inode)->i_disksize = inode->i_size;
4604 err = ext4_mark_inode_dirty(handle, inode);
4605 if (err)
4606 return err;
4607
4608 last_block = (inode->i_size + sb->s_blocksize - 1)
4609 >> EXT4_BLOCK_SIZE_BITS(sb);
4610retry:
4611 err = ext4_es_remove_extent(inode, last_block,
4612 EXT_MAX_BLOCKS - last_block);
4613 if (err == -ENOMEM) {
4614 cond_resched();
4615 congestion_wait(BLK_RW_ASYNC, HZ/50);
4616 goto retry;
4617 }
4618 if (err)
4619 return err;
4620 return ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
4621}
4622
4623static int ext4_alloc_file_blocks(struct file *file, ext4_lblk_t offset,
4624 ext4_lblk_t len, loff_t new_size,
4625 int flags)
4626{
4627 struct inode *inode = file_inode(file);
4628 handle_t *handle;
4629 int ret = 0;
4630 int ret2 = 0;
4631 int retries = 0;
4632 int depth = 0;
4633 struct ext4_map_blocks map;
4634 unsigned int credits;
4635 loff_t epos;
4636
4637 BUG_ON(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS));
4638 map.m_lblk = offset;
4639 map.m_len = len;
4640
4641
4642
4643
4644
4645 if (len <= EXT_UNWRITTEN_MAX_LEN)
4646 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
4647
4648
4649
4650
4651 credits = ext4_chunk_trans_blocks(inode, len);
4652 depth = ext_depth(inode);
4653
4654retry:
4655 while (ret >= 0 && len) {
4656
4657
4658
4659 if (depth != ext_depth(inode)) {
4660 credits = ext4_chunk_trans_blocks(inode, len);
4661 depth = ext_depth(inode);
4662 }
4663
4664 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
4665 credits);
4666 if (IS_ERR(handle)) {
4667 ret = PTR_ERR(handle);
4668 break;
4669 }
4670 ret = ext4_map_blocks(handle, inode, &map, flags);
4671 if (ret <= 0) {
4672 ext4_debug("inode #%lu: block %u: len %u: "
4673 "ext4_ext_map_blocks returned %d",
4674 inode->i_ino, map.m_lblk,
4675 map.m_len, ret);
4676 ext4_mark_inode_dirty(handle, inode);
4677 ret2 = ext4_journal_stop(handle);
4678 break;
4679 }
4680 map.m_lblk += ret;
4681 map.m_len = len = len - ret;
4682 epos = (loff_t)map.m_lblk << inode->i_blkbits;
4683 inode->i_ctime = current_time(inode);
4684 if (new_size) {
4685 if (epos > new_size)
4686 epos = new_size;
4687 if (ext4_update_inode_size(inode, epos) & 0x1)
4688 inode->i_mtime = inode->i_ctime;
4689 } else {
4690 if (epos > inode->i_size)
4691 ext4_set_inode_flag(inode,
4692 EXT4_INODE_EOFBLOCKS);
4693 }
4694 ext4_mark_inode_dirty(handle, inode);
4695 ext4_update_inode_fsync_trans(handle, inode, 1);
4696 ret2 = ext4_journal_stop(handle);
4697 if (ret2)
4698 break;
4699 }
4700 if (ret == -ENOSPC &&
4701 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4702 ret = 0;
4703 goto retry;
4704 }
4705
4706 return ret > 0 ? ret2 : ret;
4707}
4708
4709static long ext4_zero_range(struct file *file, loff_t offset,
4710 loff_t len, int mode)
4711{
4712 struct inode *inode = file_inode(file);
4713 handle_t *handle = NULL;
4714 unsigned int max_blocks;
4715 loff_t new_size = 0;
4716 int ret = 0;
4717 int flags;
4718 int credits;
4719 int partial_begin, partial_end;
4720 loff_t start, end;
4721 ext4_lblk_t lblk;
4722 unsigned int blkbits = inode->i_blkbits;
4723
4724 trace_ext4_zero_range(inode, offset, len, mode);
4725
4726 if (!S_ISREG(inode->i_mode))
4727 return -EINVAL;
4728
4729
4730 if (ext4_should_journal_data(inode)) {
4731 ret = ext4_force_commit(inode->i_sb);
4732 if (ret)
4733 return ret;
4734 }
4735
4736
4737
4738
4739
4740
4741
4742 start = round_up(offset, 1 << blkbits);
4743 end = round_down((offset + len), 1 << blkbits);
4744
4745 if (start < offset || end > offset + len)
4746 return -EINVAL;
4747 partial_begin = offset & ((1 << blkbits) - 1);
4748 partial_end = (offset + len) & ((1 << blkbits) - 1);
4749
4750 lblk = start >> blkbits;
4751 max_blocks = (end >> blkbits);
4752 if (max_blocks < lblk)
4753 max_blocks = 0;
4754 else
4755 max_blocks -= lblk;
4756
4757 inode_lock(inode);
4758
4759
4760
4761
4762 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4763 ret = -EOPNOTSUPP;
4764 goto out_mutex;
4765 }
4766
4767 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
4768 (offset + len > i_size_read(inode) ||
4769 offset + len > EXT4_I(inode)->i_disksize)) {
4770 new_size = offset + len;
4771 ret = inode_newsize_ok(inode, new_size);
4772 if (ret)
4773 goto out_mutex;
4774 }
4775
4776 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
4777 if (mode & FALLOC_FL_KEEP_SIZE)
4778 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4779
4780
4781 inode_dio_wait(inode);
4782
4783
4784 if (partial_begin || partial_end) {
4785 ret = ext4_alloc_file_blocks(file,
4786 round_down(offset, 1 << blkbits) >> blkbits,
4787 (round_up((offset + len), 1 << blkbits) -
4788 round_down(offset, 1 << blkbits)) >> blkbits,
4789 new_size, flags);
4790 if (ret)
4791 goto out_mutex;
4792
4793 }
4794
4795
4796 if (max_blocks > 0) {
4797 flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN |
4798 EXT4_EX_NOCACHE);
4799
4800
4801
4802
4803
4804 down_write(&EXT4_I(inode)->i_mmap_sem);
4805
4806 ret = ext4_break_layouts(inode);
4807 if (ret) {
4808 up_write(&EXT4_I(inode)->i_mmap_sem);
4809 goto out_mutex;
4810 }
4811
4812 ret = ext4_update_disksize_before_punch(inode, offset, len);
4813 if (ret) {
4814 up_write(&EXT4_I(inode)->i_mmap_sem);
4815 goto out_mutex;
4816 }
4817
4818 truncate_pagecache_range(inode, start, end - 1);
4819 inode->i_mtime = inode->i_ctime = current_time(inode);
4820
4821 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size,
4822 flags);
4823 up_write(&EXT4_I(inode)->i_mmap_sem);
4824 if (ret)
4825 goto out_mutex;
4826 }
4827 if (!partial_begin && !partial_end)
4828 goto out_mutex;
4829
4830
4831
4832
4833
4834 credits = (2 * ext4_ext_index_trans_blocks(inode, 2)) + 1;
4835 if (ext4_should_journal_data(inode))
4836 credits += 2;
4837 handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
4838 if (IS_ERR(handle)) {
4839 ret = PTR_ERR(handle);
4840 ext4_std_error(inode->i_sb, ret);
4841 goto out_mutex;
4842 }
4843
4844 inode->i_mtime = inode->i_ctime = current_time(inode);
4845 if (new_size) {
4846 ext4_update_inode_size(inode, new_size);
4847 } else {
4848
4849
4850
4851
4852 if ((offset + len) > i_size_read(inode))
4853 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
4854 }
4855 ext4_mark_inode_dirty(handle, inode);
4856
4857
4858 ret = ext4_zero_partial_blocks(handle, inode, offset, len);
4859 if (ret >= 0)
4860 ext4_update_inode_fsync_trans(handle, inode, 1);
4861
4862 if (file->f_flags & O_SYNC)
4863 ext4_handle_sync(handle);
4864
4865 ext4_journal_stop(handle);
4866out_mutex:
4867 inode_unlock(inode);
4868 return ret;
4869}
4870
4871
4872
4873
4874
4875
4876
4877
4878long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
4879{
4880 struct inode *inode = file_inode(file);
4881 loff_t new_size = 0;
4882 unsigned int max_blocks;
4883 int ret = 0;
4884 int flags;
4885 ext4_lblk_t lblk;
4886 unsigned int blkbits = inode->i_blkbits;
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898 if (IS_ENCRYPTED(inode) &&
4899 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE |
4900 FALLOC_FL_ZERO_RANGE)))
4901 return -EOPNOTSUPP;
4902
4903
4904 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
4905 FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE |
4906 FALLOC_FL_INSERT_RANGE))
4907 return -EOPNOTSUPP;
4908
4909 if (mode & FALLOC_FL_PUNCH_HOLE)
4910 return ext4_punch_hole(inode, offset, len);
4911
4912 ret = ext4_convert_inline_data(inode);
4913 if (ret)
4914 return ret;
4915
4916 if (mode & FALLOC_FL_COLLAPSE_RANGE)
4917 return ext4_collapse_range(inode, offset, len);
4918
4919 if (mode & FALLOC_FL_INSERT_RANGE)
4920 return ext4_insert_range(inode, offset, len);
4921
4922 if (mode & FALLOC_FL_ZERO_RANGE)
4923 return ext4_zero_range(file, offset, len, mode);
4924
4925 trace_ext4_fallocate_enter(inode, offset, len, mode);
4926 lblk = offset >> blkbits;
4927
4928 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
4929 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT;
4930 if (mode & FALLOC_FL_KEEP_SIZE)
4931 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
4932
4933 inode_lock(inode);
4934
4935
4936
4937
4938 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
4939 ret = -EOPNOTSUPP;
4940 goto out;
4941 }
4942
4943 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
4944 (offset + len > i_size_read(inode) ||
4945 offset + len > EXT4_I(inode)->i_disksize)) {
4946 new_size = offset + len;
4947 ret = inode_newsize_ok(inode, new_size);
4948 if (ret)
4949 goto out;
4950 }
4951
4952
4953 inode_dio_wait(inode);
4954
4955 ret = ext4_alloc_file_blocks(file, lblk, max_blocks, new_size, flags);
4956 if (ret)
4957 goto out;
4958
4959 if (file->f_flags & O_SYNC && EXT4_SB(inode->i_sb)->s_journal) {
4960 ret = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
4961 EXT4_I(inode)->i_sync_tid);
4962 }
4963out:
4964 inode_unlock(inode);
4965 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
4966 return ret;
4967}
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
4980 loff_t offset, ssize_t len)
4981{
4982 unsigned int max_blocks;
4983 int ret = 0;
4984 int ret2 = 0;
4985 struct ext4_map_blocks map;
4986 unsigned int blkbits = inode->i_blkbits;
4987 unsigned int credits = 0;
4988
4989 map.m_lblk = offset >> blkbits;
4990 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
4991
4992 if (!handle) {
4993
4994
4995
4996 credits = ext4_chunk_trans_blocks(inode, max_blocks);
4997 }
4998 while (ret >= 0 && ret < max_blocks) {
4999 map.m_lblk += ret;
5000 map.m_len = (max_blocks -= ret);
5001 if (credits) {
5002 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
5003 credits);
5004 if (IS_ERR(handle)) {
5005 ret = PTR_ERR(handle);
5006 break;
5007 }
5008 }
5009 ret = ext4_map_blocks(handle, inode, &map,
5010 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
5011 if (ret <= 0)
5012 ext4_warning(inode->i_sb,
5013 "inode #%lu: block %u: len %u: "
5014 "ext4_ext_map_blocks returned %d",
5015 inode->i_ino, map.m_lblk,
5016 map.m_len, ret);
5017 ext4_mark_inode_dirty(handle, inode);
5018 if (credits)
5019 ret2 = ext4_journal_stop(handle);
5020 if (ret <= 0 || ret2)
5021 break;
5022 }
5023 return ret > 0 ? ret2 : ret;
5024}
5025
5026int ext4_convert_unwritten_io_end_vec(handle_t *handle, ext4_io_end_t *io_end)
5027{
5028 int ret, err = 0;
5029 struct ext4_io_end_vec *io_end_vec;
5030
5031
5032
5033
5034
5035
5036 if (handle) {
5037 handle = ext4_journal_start_reserved(handle,
5038 EXT4_HT_EXT_CONVERT);
5039 if (IS_ERR(handle))
5040 return PTR_ERR(handle);
5041 }
5042
5043 list_for_each_entry(io_end_vec, &io_end->list_vec, list) {
5044 ret = ext4_convert_unwritten_extents(handle, io_end->inode,
5045 io_end_vec->offset,
5046 io_end_vec->size);
5047 if (ret)
5048 break;
5049 }
5050
5051 if (handle)
5052 err = ext4_journal_stop(handle);
5053
5054 return ret < 0 ? ret : err;
5055}
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066static int ext4_find_delayed_extent(struct inode *inode,
5067 struct extent_status *newes)
5068{
5069 struct extent_status es;
5070 ext4_lblk_t block, next_del;
5071
5072 if (newes->es_pblk == 0) {
5073 ext4_es_find_extent_range(inode, &ext4_es_is_delayed,
5074 newes->es_lblk,
5075 newes->es_lblk + newes->es_len - 1,
5076 &es);
5077
5078
5079
5080
5081
5082 if (es.es_len == 0)
5083
5084 return 0;
5085
5086 if (es.es_lblk > newes->es_lblk) {
5087
5088 newes->es_len = min(es.es_lblk - newes->es_lblk,
5089 newes->es_len);
5090 return 0;
5091 }
5092
5093 newes->es_len = es.es_lblk + es.es_len - newes->es_lblk;
5094 }
5095
5096 block = newes->es_lblk + newes->es_len;
5097 ext4_es_find_extent_range(inode, &ext4_es_is_delayed, block,
5098 EXT_MAX_BLOCKS, &es);
5099 if (es.es_len == 0)
5100 next_del = EXT_MAX_BLOCKS;
5101 else
5102 next_del = es.es_lblk;
5103
5104 return next_del;
5105}
5106
5107static int ext4_xattr_fiemap(struct inode *inode,
5108 struct fiemap_extent_info *fieinfo)
5109{
5110 __u64 physical = 0;
5111 __u64 length;
5112 __u32 flags = FIEMAP_EXTENT_LAST;
5113 int blockbits = inode->i_sb->s_blocksize_bits;
5114 int error = 0;
5115
5116
5117 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
5118 struct ext4_iloc iloc;
5119 int offset;
5120
5121 error = ext4_get_inode_loc(inode, &iloc);
5122 if (error)
5123 return error;
5124 physical = (__u64)iloc.bh->b_blocknr << blockbits;
5125 offset = EXT4_GOOD_OLD_INODE_SIZE +
5126 EXT4_I(inode)->i_extra_isize;
5127 physical += offset;
5128 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
5129 flags |= FIEMAP_EXTENT_DATA_INLINE;
5130 brelse(iloc.bh);
5131 } else {
5132 physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits;
5133 length = inode->i_sb->s_blocksize;
5134 }
5135
5136 if (physical)
5137 error = fiemap_fill_next_extent(fieinfo, 0, physical,
5138 length, flags);
5139 return (error < 0 ? error : 0);
5140}
5141
5142static int _ext4_fiemap(struct inode *inode,
5143 struct fiemap_extent_info *fieinfo,
5144 __u64 start, __u64 len,
5145 int (*fill)(struct inode *, ext4_lblk_t,
5146 ext4_lblk_t,
5147 struct fiemap_extent_info *))
5148{
5149 ext4_lblk_t start_blk;
5150 u32 ext4_fiemap_flags = FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR;
5151
5152 int error = 0;
5153
5154 if (ext4_has_inline_data(inode)) {
5155 int has_inline = 1;
5156
5157 error = ext4_inline_data_fiemap(inode, fieinfo, &has_inline,
5158 start, len);
5159
5160 if (has_inline)
5161 return error;
5162 }
5163
5164 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
5165 error = ext4_ext_precache(inode);
5166 if (error)
5167 return error;
5168 fieinfo->fi_flags &= ~FIEMAP_FLAG_CACHE;
5169 }
5170
5171
5172 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) &&
5173 fill == ext4_fill_fiemap_extents)
5174 return generic_block_fiemap(inode, fieinfo, start, len,
5175 ext4_get_block);
5176
5177 if (fill == ext4_fill_es_cache_info)
5178 ext4_fiemap_flags &= FIEMAP_FLAG_XATTR;
5179 if (fiemap_check_flags(fieinfo, ext4_fiemap_flags))
5180 return -EBADR;
5181
5182 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
5183 error = ext4_xattr_fiemap(inode, fieinfo);
5184 } else {
5185 ext4_lblk_t len_blks;
5186 __u64 last_blk;
5187
5188 start_blk = start >> inode->i_sb->s_blocksize_bits;
5189 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
5190 if (last_blk >= EXT_MAX_BLOCKS)
5191 last_blk = EXT_MAX_BLOCKS-1;
5192 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
5193
5194
5195
5196
5197
5198 error = fill(inode, start_blk, len_blks, fieinfo);
5199 }
5200 return error;
5201}
5202
5203int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
5204 __u64 start, __u64 len)
5205{
5206 return _ext4_fiemap(inode, fieinfo, start, len,
5207 ext4_fill_fiemap_extents);
5208}
5209
5210int ext4_get_es_cache(struct inode *inode, struct fiemap_extent_info *fieinfo,
5211 __u64 start, __u64 len)
5212{
5213 if (ext4_has_inline_data(inode)) {
5214 int has_inline;
5215
5216 down_read(&EXT4_I(inode)->xattr_sem);
5217 has_inline = ext4_has_inline_data(inode);
5218 up_read(&EXT4_I(inode)->xattr_sem);
5219 if (has_inline)
5220 return 0;
5221 }
5222
5223 return _ext4_fiemap(inode, fieinfo, start, len,
5224 ext4_fill_es_cache_info);
5225}
5226
5227
5228
5229
5230
5231
5232
5233
5234static int
5235ext4_access_path(handle_t *handle, struct inode *inode,
5236 struct ext4_ext_path *path)
5237{
5238 int credits, err;
5239
5240 if (!ext4_handle_valid(handle))
5241 return 0;
5242
5243
5244
5245
5246
5247
5248
5249 credits = ext4_writepage_trans_blocks(inode);
5250 err = ext4_datasem_ensure_credits(handle, inode, 7, credits, 0);
5251 if (err < 0)
5252 return err;
5253
5254 err = ext4_ext_get_access(handle, inode, path);
5255 return err;
5256}
5257
5258
5259
5260
5261
5262
5263
5264static int
5265ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift,
5266 struct inode *inode, handle_t *handle,
5267 enum SHIFT_DIRECTION SHIFT)
5268{
5269 int depth, err = 0;
5270 struct ext4_extent *ex_start, *ex_last;
5271 bool update = 0;
5272 depth = path->p_depth;
5273
5274 while (depth >= 0) {
5275 if (depth == path->p_depth) {
5276 ex_start = path[depth].p_ext;
5277 if (!ex_start)
5278 return -EFSCORRUPTED;
5279
5280 ex_last = EXT_LAST_EXTENT(path[depth].p_hdr);
5281
5282 err = ext4_access_path(handle, inode, path + depth);
5283 if (err)
5284 goto out;
5285
5286 if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr))
5287 update = 1;
5288
5289 while (ex_start <= ex_last) {
5290 if (SHIFT == SHIFT_LEFT) {
5291 le32_add_cpu(&ex_start->ee_block,
5292 -shift);
5293
5294 if ((ex_start >
5295 EXT_FIRST_EXTENT(path[depth].p_hdr))
5296 &&
5297 ext4_ext_try_to_merge_right(inode,
5298 path, ex_start - 1))
5299 ex_last--;
5300 else
5301 ex_start++;
5302 } else {
5303 le32_add_cpu(&ex_last->ee_block, shift);
5304 ext4_ext_try_to_merge_right(inode, path,
5305 ex_last);
5306 ex_last--;
5307 }
5308 }
5309 err = ext4_ext_dirty(handle, inode, path + depth);
5310 if (err)
5311 goto out;
5312
5313 if (--depth < 0 || !update)
5314 break;
5315 }
5316
5317
5318 err = ext4_access_path(handle, inode, path + depth);
5319 if (err)
5320 goto out;
5321
5322 if (SHIFT == SHIFT_LEFT)
5323 le32_add_cpu(&path[depth].p_idx->ei_block, -shift);
5324 else
5325 le32_add_cpu(&path[depth].p_idx->ei_block, shift);
5326 err = ext4_ext_dirty(handle, inode, path + depth);
5327 if (err)
5328 goto out;
5329
5330
5331 if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr))
5332 break;
5333
5334 depth--;
5335 }
5336
5337out:
5338 return err;
5339}
5340
5341
5342
5343
5344
5345
5346
5347
5348static int
5349ext4_ext_shift_extents(struct inode *inode, handle_t *handle,
5350 ext4_lblk_t start, ext4_lblk_t shift,
5351 enum SHIFT_DIRECTION SHIFT)
5352{
5353 struct ext4_ext_path *path;
5354 int ret = 0, depth;
5355 struct ext4_extent *extent;
5356 ext4_lblk_t stop, *iterator, ex_start, ex_end;
5357
5358
5359 path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL,
5360 EXT4_EX_NOCACHE);
5361 if (IS_ERR(path))
5362 return PTR_ERR(path);
5363
5364 depth = path->p_depth;
5365 extent = path[depth].p_ext;
5366 if (!extent)
5367 goto out;
5368
5369 stop = le32_to_cpu(extent->ee_block);
5370
5371
5372
5373
5374
5375
5376 if (SHIFT == SHIFT_LEFT) {
5377 path = ext4_find_extent(inode, start - 1, &path,
5378 EXT4_EX_NOCACHE);
5379 if (IS_ERR(path))
5380 return PTR_ERR(path);
5381 depth = path->p_depth;
5382 extent = path[depth].p_ext;
5383 if (extent) {
5384 ex_start = le32_to_cpu(extent->ee_block);
5385 ex_end = le32_to_cpu(extent->ee_block) +
5386 ext4_ext_get_actual_len(extent);
5387 } else {
5388 ex_start = 0;
5389 ex_end = 0;
5390 }
5391
5392 if ((start == ex_start && shift > ex_start) ||
5393 (shift > start - ex_end)) {
5394 ret = -EINVAL;
5395 goto out;
5396 }
5397 } else {
5398 if (shift > EXT_MAX_BLOCKS -
5399 (stop + ext4_ext_get_actual_len(extent))) {
5400 ret = -EINVAL;
5401 goto out;
5402 }
5403 }
5404
5405
5406
5407
5408
5409
5410 if (SHIFT == SHIFT_LEFT)
5411 iterator = &start;
5412 else
5413 iterator = &stop;
5414
5415
5416
5417
5418
5419
5420 while (iterator && start <= stop) {
5421 path = ext4_find_extent(inode, *iterator, &path,
5422 EXT4_EX_NOCACHE);
5423 if (IS_ERR(path))
5424 return PTR_ERR(path);
5425 depth = path->p_depth;
5426 extent = path[depth].p_ext;
5427 if (!extent) {
5428 EXT4_ERROR_INODE(inode, "unexpected hole at %lu",
5429 (unsigned long) *iterator);
5430 return -EFSCORRUPTED;
5431 }
5432 if (SHIFT == SHIFT_LEFT && *iterator >
5433 le32_to_cpu(extent->ee_block)) {
5434
5435 if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) {
5436 path[depth].p_ext++;
5437 } else {
5438 *iterator = ext4_ext_next_allocated_block(path);
5439 continue;
5440 }
5441 }
5442
5443 if (SHIFT == SHIFT_LEFT) {
5444 extent = EXT_LAST_EXTENT(path[depth].p_hdr);
5445 *iterator = le32_to_cpu(extent->ee_block) +
5446 ext4_ext_get_actual_len(extent);
5447 } else {
5448 extent = EXT_FIRST_EXTENT(path[depth].p_hdr);
5449 if (le32_to_cpu(extent->ee_block) > 0)
5450 *iterator = le32_to_cpu(extent->ee_block) - 1;
5451 else
5452
5453 iterator = NULL;
5454
5455 while (le32_to_cpu(extent->ee_block) < start)
5456 extent++;
5457 path[depth].p_ext = extent;
5458 }
5459 ret = ext4_ext_shift_path_extents(path, shift, inode,
5460 handle, SHIFT);
5461 if (ret)
5462 break;
5463 }
5464out:
5465 ext4_ext_drop_refs(path);
5466 kfree(path);
5467 return ret;
5468}
5469
5470
5471
5472
5473
5474
5475int ext4_collapse_range(struct inode *inode, loff_t offset, loff_t len)
5476{
5477 struct super_block *sb = inode->i_sb;
5478 ext4_lblk_t punch_start, punch_stop;
5479 handle_t *handle;
5480 unsigned int credits;
5481 loff_t new_size, ioffset;
5482 int ret;
5483
5484
5485
5486
5487
5488
5489 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5490 return -EOPNOTSUPP;
5491
5492
5493 if (offset & (EXT4_CLUSTER_SIZE(sb) - 1) ||
5494 len & (EXT4_CLUSTER_SIZE(sb) - 1))
5495 return -EINVAL;
5496
5497 if (!S_ISREG(inode->i_mode))
5498 return -EINVAL;
5499
5500 trace_ext4_collapse_range(inode, offset, len);
5501
5502 punch_start = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5503 punch_stop = (offset + len) >> EXT4_BLOCK_SIZE_BITS(sb);
5504
5505
5506 if (ext4_should_journal_data(inode)) {
5507 ret = ext4_force_commit(inode->i_sb);
5508 if (ret)
5509 return ret;
5510 }
5511
5512 inode_lock(inode);
5513
5514
5515
5516
5517 if (offset + len >= i_size_read(inode)) {
5518 ret = -EINVAL;
5519 goto out_mutex;
5520 }
5521
5522
5523 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5524 ret = -EOPNOTSUPP;
5525 goto out_mutex;
5526 }
5527
5528
5529 inode_dio_wait(inode);
5530
5531
5532
5533
5534
5535 down_write(&EXT4_I(inode)->i_mmap_sem);
5536
5537 ret = ext4_break_layouts(inode);
5538 if (ret)
5539 goto out_mmap;
5540
5541
5542
5543
5544
5545 ioffset = round_down(offset, PAGE_SIZE);
5546
5547
5548
5549
5550 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset, offset);
5551 if (ret)
5552 goto out_mmap;
5553
5554
5555
5556
5557
5558 ret = filemap_write_and_wait_range(inode->i_mapping, offset + len,
5559 LLONG_MAX);
5560 if (ret)
5561 goto out_mmap;
5562 truncate_pagecache(inode, ioffset);
5563
5564 credits = ext4_writepage_trans_blocks(inode);
5565 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5566 if (IS_ERR(handle)) {
5567 ret = PTR_ERR(handle);
5568 goto out_mmap;
5569 }
5570
5571 down_write(&EXT4_I(inode)->i_data_sem);
5572 ext4_discard_preallocations(inode);
5573
5574 ret = ext4_es_remove_extent(inode, punch_start,
5575 EXT_MAX_BLOCKS - punch_start);
5576 if (ret) {
5577 up_write(&EXT4_I(inode)->i_data_sem);
5578 goto out_stop;
5579 }
5580
5581 ret = ext4_ext_remove_space(inode, punch_start, punch_stop - 1);
5582 if (ret) {
5583 up_write(&EXT4_I(inode)->i_data_sem);
5584 goto out_stop;
5585 }
5586 ext4_discard_preallocations(inode);
5587
5588 ret = ext4_ext_shift_extents(inode, handle, punch_stop,
5589 punch_stop - punch_start, SHIFT_LEFT);
5590 if (ret) {
5591 up_write(&EXT4_I(inode)->i_data_sem);
5592 goto out_stop;
5593 }
5594
5595 new_size = i_size_read(inode) - len;
5596 i_size_write(inode, new_size);
5597 EXT4_I(inode)->i_disksize = new_size;
5598
5599 up_write(&EXT4_I(inode)->i_data_sem);
5600 if (IS_SYNC(inode))
5601 ext4_handle_sync(handle);
5602 inode->i_mtime = inode->i_ctime = current_time(inode);
5603 ext4_mark_inode_dirty(handle, inode);
5604 ext4_update_inode_fsync_trans(handle, inode, 1);
5605
5606out_stop:
5607 ext4_journal_stop(handle);
5608out_mmap:
5609 up_write(&EXT4_I(inode)->i_mmap_sem);
5610out_mutex:
5611 inode_unlock(inode);
5612 return ret;
5613}
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623int ext4_insert_range(struct inode *inode, loff_t offset, loff_t len)
5624{
5625 struct super_block *sb = inode->i_sb;
5626 handle_t *handle;
5627 struct ext4_ext_path *path;
5628 struct ext4_extent *extent;
5629 ext4_lblk_t offset_lblk, len_lblk, ee_start_lblk = 0;
5630 unsigned int credits, ee_len;
5631 int ret = 0, depth, split_flag = 0;
5632 loff_t ioffset;
5633
5634
5635
5636
5637
5638
5639 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
5640 return -EOPNOTSUPP;
5641
5642
5643 if (offset & (EXT4_CLUSTER_SIZE(sb) - 1) ||
5644 len & (EXT4_CLUSTER_SIZE(sb) - 1))
5645 return -EINVAL;
5646
5647 if (!S_ISREG(inode->i_mode))
5648 return -EOPNOTSUPP;
5649
5650 trace_ext4_insert_range(inode, offset, len);
5651
5652 offset_lblk = offset >> EXT4_BLOCK_SIZE_BITS(sb);
5653 len_lblk = len >> EXT4_BLOCK_SIZE_BITS(sb);
5654
5655
5656 if (ext4_should_journal_data(inode)) {
5657 ret = ext4_force_commit(inode->i_sb);
5658 if (ret)
5659 return ret;
5660 }
5661
5662 inode_lock(inode);
5663
5664 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
5665 ret = -EOPNOTSUPP;
5666 goto out_mutex;
5667 }
5668
5669
5670 if (inode->i_size + len > inode->i_sb->s_maxbytes) {
5671 ret = -EFBIG;
5672 goto out_mutex;
5673 }
5674
5675
5676 if (offset >= i_size_read(inode)) {
5677 ret = -EINVAL;
5678 goto out_mutex;
5679 }
5680
5681
5682 inode_dio_wait(inode);
5683
5684
5685
5686
5687
5688 down_write(&EXT4_I(inode)->i_mmap_sem);
5689
5690 ret = ext4_break_layouts(inode);
5691 if (ret)
5692 goto out_mmap;
5693
5694
5695
5696
5697
5698 ioffset = round_down(offset, PAGE_SIZE);
5699
5700 ret = filemap_write_and_wait_range(inode->i_mapping, ioffset,
5701 LLONG_MAX);
5702 if (ret)
5703 goto out_mmap;
5704 truncate_pagecache(inode, ioffset);
5705
5706 credits = ext4_writepage_trans_blocks(inode);
5707 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
5708 if (IS_ERR(handle)) {
5709 ret = PTR_ERR(handle);
5710 goto out_mmap;
5711 }
5712
5713
5714 inode->i_size += len;
5715 EXT4_I(inode)->i_disksize += len;
5716 inode->i_mtime = inode->i_ctime = current_time(inode);
5717 ret = ext4_mark_inode_dirty(handle, inode);
5718 if (ret)
5719 goto out_stop;
5720
5721 down_write(&EXT4_I(inode)->i_data_sem);
5722 ext4_discard_preallocations(inode);
5723
5724 path = ext4_find_extent(inode, offset_lblk, NULL, 0);
5725 if (IS_ERR(path)) {
5726 up_write(&EXT4_I(inode)->i_data_sem);
5727 goto out_stop;
5728 }
5729
5730 depth = ext_depth(inode);
5731 extent = path[depth].p_ext;
5732 if (extent) {
5733 ee_start_lblk = le32_to_cpu(extent->ee_block);
5734 ee_len = ext4_ext_get_actual_len(extent);
5735
5736
5737
5738
5739
5740 if ((offset_lblk > ee_start_lblk) &&
5741 (offset_lblk < (ee_start_lblk + ee_len))) {
5742 if (ext4_ext_is_unwritten(extent))
5743 split_flag = EXT4_EXT_MARK_UNWRIT1 |
5744 EXT4_EXT_MARK_UNWRIT2;
5745 ret = ext4_split_extent_at(handle, inode, &path,
5746 offset_lblk, split_flag,
5747 EXT4_EX_NOCACHE |
5748 EXT4_GET_BLOCKS_PRE_IO |
5749 EXT4_GET_BLOCKS_METADATA_NOFAIL);
5750 }
5751
5752 ext4_ext_drop_refs(path);
5753 kfree(path);
5754 if (ret < 0) {
5755 up_write(&EXT4_I(inode)->i_data_sem);
5756 goto out_stop;
5757 }
5758 } else {
5759 ext4_ext_drop_refs(path);
5760 kfree(path);
5761 }
5762
5763 ret = ext4_es_remove_extent(inode, offset_lblk,
5764 EXT_MAX_BLOCKS - offset_lblk);
5765 if (ret) {
5766 up_write(&EXT4_I(inode)->i_data_sem);
5767 goto out_stop;
5768 }
5769
5770
5771
5772
5773
5774 ret = ext4_ext_shift_extents(inode, handle,
5775 ee_start_lblk > offset_lblk ? ee_start_lblk : offset_lblk,
5776 len_lblk, SHIFT_RIGHT);
5777
5778 up_write(&EXT4_I(inode)->i_data_sem);
5779 if (IS_SYNC(inode))
5780 ext4_handle_sync(handle);
5781 if (ret >= 0)
5782 ext4_update_inode_fsync_trans(handle, inode, 1);
5783
5784out_stop:
5785 ext4_journal_stop(handle);
5786out_mmap:
5787 up_write(&EXT4_I(inode)->i_mmap_sem);
5788out_mutex:
5789 inode_unlock(inode);
5790 return ret;
5791}
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813int
5814ext4_swap_extents(handle_t *handle, struct inode *inode1,
5815 struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2,
5816 ext4_lblk_t count, int unwritten, int *erp)
5817{
5818 struct ext4_ext_path *path1 = NULL;
5819 struct ext4_ext_path *path2 = NULL;
5820 int replaced_count = 0;
5821
5822 BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem));
5823 BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem));
5824 BUG_ON(!inode_is_locked(inode1));
5825 BUG_ON(!inode_is_locked(inode2));
5826
5827 *erp = ext4_es_remove_extent(inode1, lblk1, count);
5828 if (unlikely(*erp))
5829 return 0;
5830 *erp = ext4_es_remove_extent(inode2, lblk2, count);
5831 if (unlikely(*erp))
5832 return 0;
5833
5834 while (count) {
5835 struct ext4_extent *ex1, *ex2, tmp_ex;
5836 ext4_lblk_t e1_blk, e2_blk;
5837 int e1_len, e2_len, len;
5838 int split = 0;
5839
5840 path1 = ext4_find_extent(inode1, lblk1, NULL, EXT4_EX_NOCACHE);
5841 if (IS_ERR(path1)) {
5842 *erp = PTR_ERR(path1);
5843 path1 = NULL;
5844 finish:
5845 count = 0;
5846 goto repeat;
5847 }
5848 path2 = ext4_find_extent(inode2, lblk2, NULL, EXT4_EX_NOCACHE);
5849 if (IS_ERR(path2)) {
5850 *erp = PTR_ERR(path2);
5851 path2 = NULL;
5852 goto finish;
5853 }
5854 ex1 = path1[path1->p_depth].p_ext;
5855 ex2 = path2[path2->p_depth].p_ext;
5856
5857 if (unlikely(!ex2 || !ex1))
5858 goto finish;
5859
5860 e1_blk = le32_to_cpu(ex1->ee_block);
5861 e2_blk = le32_to_cpu(ex2->ee_block);
5862 e1_len = ext4_ext_get_actual_len(ex1);
5863 e2_len = ext4_ext_get_actual_len(ex2);
5864
5865
5866 if (!in_range(lblk1, e1_blk, e1_len) ||
5867 !in_range(lblk2, e2_blk, e2_len)) {
5868 ext4_lblk_t next1, next2;
5869
5870
5871 next1 = ext4_ext_next_allocated_block(path1);
5872 next2 = ext4_ext_next_allocated_block(path2);
5873
5874 if (e1_blk > lblk1)
5875 next1 = e1_blk;
5876 if (e2_blk > lblk2)
5877 next2 = e2_blk;
5878
5879 if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS)
5880 goto finish;
5881
5882 len = next1 - lblk1;
5883 if (len < next2 - lblk2)
5884 len = next2 - lblk2;
5885 if (len > count)
5886 len = count;
5887 lblk1 += len;
5888 lblk2 += len;
5889 count -= len;
5890 goto repeat;
5891 }
5892
5893
5894 if (e1_blk < lblk1) {
5895 split = 1;
5896 *erp = ext4_force_split_extent_at(handle, inode1,
5897 &path1, lblk1, 0);
5898 if (unlikely(*erp))
5899 goto finish;
5900 }
5901 if (e2_blk < lblk2) {
5902 split = 1;
5903 *erp = ext4_force_split_extent_at(handle, inode2,
5904 &path2, lblk2, 0);
5905 if (unlikely(*erp))
5906 goto finish;
5907 }
5908
5909
5910 if (split)
5911 goto repeat;
5912
5913
5914 len = count;
5915 if (len > e1_blk + e1_len - lblk1)
5916 len = e1_blk + e1_len - lblk1;
5917 if (len > e2_blk + e2_len - lblk2)
5918 len = e2_blk + e2_len - lblk2;
5919
5920 if (len != e1_len) {
5921 split = 1;
5922 *erp = ext4_force_split_extent_at(handle, inode1,
5923 &path1, lblk1 + len, 0);
5924 if (unlikely(*erp))
5925 goto finish;
5926 }
5927 if (len != e2_len) {
5928 split = 1;
5929 *erp = ext4_force_split_extent_at(handle, inode2,
5930 &path2, lblk2 + len, 0);
5931 if (*erp)
5932 goto finish;
5933 }
5934
5935
5936 if (split)
5937 goto repeat;
5938
5939 BUG_ON(e2_len != e1_len);
5940 *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth);
5941 if (unlikely(*erp))
5942 goto finish;
5943 *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth);
5944 if (unlikely(*erp))
5945 goto finish;
5946
5947
5948 tmp_ex = *ex1;
5949 ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2));
5950 ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex));
5951 ex1->ee_len = cpu_to_le16(e2_len);
5952 ex2->ee_len = cpu_to_le16(e1_len);
5953 if (unwritten)
5954 ext4_ext_mark_unwritten(ex2);
5955 if (ext4_ext_is_unwritten(&tmp_ex))
5956 ext4_ext_mark_unwritten(ex1);
5957
5958 ext4_ext_try_to_merge(handle, inode2, path2, ex2);
5959 ext4_ext_try_to_merge(handle, inode1, path1, ex1);
5960 *erp = ext4_ext_dirty(handle, inode2, path2 +
5961 path2->p_depth);
5962 if (unlikely(*erp))
5963 goto finish;
5964 *erp = ext4_ext_dirty(handle, inode1, path1 +
5965 path1->p_depth);
5966
5967
5968
5969
5970
5971
5972 if (unlikely(*erp))
5973 goto finish;
5974 lblk1 += len;
5975 lblk2 += len;
5976 replaced_count += len;
5977 count -= len;
5978
5979 repeat:
5980 ext4_ext_drop_refs(path1);
5981 kfree(path1);
5982 ext4_ext_drop_refs(path2);
5983 kfree(path2);
5984 path1 = path2 = NULL;
5985 }
5986 return replaced_count;
5987}
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu)
6002{
6003 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
6004 struct ext4_ext_path *path;
6005 int depth, mapped = 0, err = 0;
6006 struct ext4_extent *extent;
6007 ext4_lblk_t first_lblk, first_lclu, last_lclu;
6008
6009
6010 path = ext4_find_extent(inode, EXT4_C2B(sbi, lclu), NULL, 0);
6011 if (IS_ERR(path)) {
6012 err = PTR_ERR(path);
6013 path = NULL;
6014 goto out;
6015 }
6016
6017 depth = ext_depth(inode);
6018
6019
6020
6021
6022
6023
6024 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
6025 EXT4_ERROR_INODE(inode,
6026 "bad extent address - lblock: %lu, depth: %d, pblock: %lld",
6027 (unsigned long) EXT4_C2B(sbi, lclu),
6028 depth, path[depth].p_block);
6029 err = -EFSCORRUPTED;
6030 goto out;
6031 }
6032
6033 extent = path[depth].p_ext;
6034
6035
6036 if (extent == NULL)
6037 goto out;
6038
6039 first_lblk = le32_to_cpu(extent->ee_block);
6040 first_lclu = EXT4_B2C(sbi, first_lblk);
6041
6042
6043
6044
6045
6046
6047
6048 if (lclu >= first_lclu) {
6049 last_lclu = EXT4_B2C(sbi, first_lblk +
6050 ext4_ext_get_actual_len(extent) - 1);
6051 if (lclu <= last_lclu) {
6052 mapped = 1;
6053 } else {
6054 first_lblk = ext4_ext_next_allocated_block(path);
6055 first_lclu = EXT4_B2C(sbi, first_lblk);
6056 if (lclu == first_lclu)
6057 mapped = 1;
6058 }
6059 }
6060
6061out:
6062 ext4_ext_drop_refs(path);
6063 kfree(path);
6064
6065 return err ? err : mapped;
6066}
6067