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