extents.c 131 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*
 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
 * Written by Alex Tomas <alex@clusterfs.com>
 *
 * Architecture independence:
 *   Copyright (c) 2005, Bull S.A.
 *   Written by Pierre Peiffer <pierre.peiffer@bull.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public Licens
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-
 */

/*
 * Extents support for EXT4
 *
 * TODO:
 *   - ext4*_error() should be used in some situations
 *   - analyze all BUG()/BUG_ON(), use -EIO where appropriate
 *   - smart tree reduction
 */

#include <linux/fs.h>
#include <linux/time.h>
34
#include <linux/jbd2.h>
35
36
37
38
39
#include <linux/highuid.h>
#include <linux/pagemap.h>
#include <linux/quotaops.h>
#include <linux/string.h>
#include <linux/slab.h>
Amit Arora's avatar
Amit Arora committed
40
#include <linux/falloc.h>
41
#include <asm/uaccess.h>
42
#include <linux/fiemap.h>
43
#include "ext4_jbd2.h"
44
#include "ext4_extents.h"
45
#include "xattr.h"
46

47
48
#include <trace/events/ext4.h>

49
50
51
52
53
54
55
56
/*
 * used by extent splitting.
 */
#define EXT4_EXT_MAY_ZEROOUT	0x1  /* safe to zeroout if split fails \
					due to ENOSPC */
#define EXT4_EXT_MARK_UNINIT1	0x2  /* mark first half uninitialized */
#define EXT4_EXT_MARK_UNINIT2	0x4  /* mark second half uninitialized */

57
58
59
#define EXT4_EXT_DATA_VALID1	0x8  /* first half contains valid data */
#define EXT4_EXT_DATA_VALID2	0x10 /* second half contains valid data */

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
static __le32 ext4_extent_block_csum(struct inode *inode,
				     struct ext4_extent_header *eh)
{
	struct ext4_inode_info *ei = EXT4_I(inode);
	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
	__u32 csum;

	csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)eh,
			   EXT4_EXTENT_TAIL_OFFSET(eh));
	return cpu_to_le32(csum);
}

static int ext4_extent_block_csum_verify(struct inode *inode,
					 struct ext4_extent_header *eh)
{
	struct ext4_extent_tail *et;

	if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
		EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
		return 1;

	et = find_ext4_extent_tail(eh);
	if (et->et_checksum != ext4_extent_block_csum(inode, eh))
		return 0;
	return 1;
}

static void ext4_extent_block_csum_set(struct inode *inode,
				       struct ext4_extent_header *eh)
{
	struct ext4_extent_tail *et;

	if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
		EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
		return;

	et = find_ext4_extent_tail(eh);
	et->et_checksum = ext4_extent_block_csum(inode, eh);
}

Allison Henderson's avatar
Allison Henderson committed
100
101
102
103
104
105
106
static int ext4_split_extent(handle_t *handle,
				struct inode *inode,
				struct ext4_ext_path *path,
				struct ext4_map_blocks *map,
				int split_flag,
				int flags);

107
108
109
110
111
112
113
static int ext4_split_extent_at(handle_t *handle,
			     struct inode *inode,
			     struct ext4_ext_path *path,
			     ext4_lblk_t split,
			     int split_flag,
			     int flags);

114
115
116
static int ext4_find_delayed_extent(struct inode *inode,
				    struct ext4_ext_cache *newex);

117
118
119
static int ext4_ext_truncate_extend_restart(handle_t *handle,
					    struct inode *inode,
					    int needed)
120
121
122
{
	int err;

123
124
	if (!ext4_handle_valid(handle))
		return 0;
125
	if (handle->h_buffer_credits > needed)
126
127
		return 0;
	err = ext4_journal_extend(handle, needed);
128
	if (err <= 0)
129
		return err;
130
	err = ext4_truncate_restart_trans(handle, inode, needed);
131
132
	if (err == 0)
		err = -EAGAIN;
133
134

	return err;
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
}

/*
 * could return:
 *  - EROFS
 *  - ENOMEM
 */
static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
				struct ext4_ext_path *path)
{
	if (path->p_bh) {
		/* path points to block */
		return ext4_journal_get_write_access(handle, path->p_bh);
	}
	/* path points to leaf/index in inode body */
	/* we use in-core data, no need to protect them */
	return 0;
}

/*
 * could return:
 *  - EROFS
 *  - ENOMEM
 *  - EIO
 */
160
161
162
163
164
#define ext4_ext_dirty(handle, inode, path) \
		__ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path))
static int __ext4_ext_dirty(const char *where, unsigned int line,
			    handle_t *handle, struct inode *inode,
			    struct ext4_ext_path *path)
165
166
167
{
	int err;
	if (path->p_bh) {
168
		ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh));
169
		/* path points to block */
170
171
		err = __ext4_handle_dirty_metadata(where, line, handle,
						   inode, path->p_bh);
172
173
174
175
176
177
178
	} else {
		/* path points to leaf/index in inode body */
		err = ext4_mark_inode_dirty(handle, inode);
	}
	return err;
}

179
static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
180
			      struct ext4_ext_path *path,
Aneesh Kumar K.V's avatar
Aneesh Kumar K.V committed
181
			      ext4_lblk_t block)
182
183
{
	if (path) {
184
		int depth = path->p_depth;
185
186
		struct ext4_extent *ex;

187
188
189
190
191
192
193
194
195
196
197
198
199
200
		/*
		 * Try to predict block placement assuming that we are
		 * filling in a file which will eventually be
		 * non-sparse --- i.e., in the case of libbfd writing
		 * an ELF object sections out-of-order but in a way
		 * the eventually results in a contiguous object or
		 * executable file, or some database extending a table
		 * space file.  However, this is actually somewhat
		 * non-ideal if we are writing a sparse file such as
		 * qemu or KVM writing a raw image file that is going
		 * to stay fairly sparse, since it will end up
		 * fragmenting the file system's free space.  Maybe we
		 * should have some hueristics or some way to allow
		 * userspace to pass a hint to file system,
Tao Ma's avatar
Tao Ma committed
201
		 * especially if the latter case turns out to be
202
203
		 * common.
		 */
204
		ex = path[depth].p_ext;
205
206
207
208
209
210
211
212
213
		if (ex) {
			ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
			ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);

			if (block > ext_block)
				return ext_pblk + (block - ext_block);
			else
				return ext_pblk - (ext_block - block);
		}
214

215
216
		/* it looks like index is empty;
		 * try to find starting block from index itself */
217
218
219
220
221
		if (path[depth].p_bh)
			return path[depth].p_bh->b_blocknr;
	}

	/* OK. use inode's group */
222
	return ext4_inode_to_goal_block(inode);
223
224
}

225
226
227
/*
 * Allocation for a meta data block
 */
228
static ext4_fsblk_t
229
ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
230
			struct ext4_ext_path *path,
231
			struct ext4_extent *ex, int *err, unsigned int flags)
232
{
233
	ext4_fsblk_t goal, newblock;
234
235

	goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
236
237
	newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
					NULL, err);
238
239
240
	return newblock;
}

241
static inline int ext4_ext_space_block(struct inode *inode, int check)
242
243
244
245
246
{
	int size;

	size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
			/ sizeof(struct ext4_extent);
247
#ifdef AGGRESSIVE_TEST
248
249
	if (!check && size > 6)
		size = 6;
250
251
252
253
#endif
	return size;
}

254
static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
255
256
257
258
259
{
	int size;

	size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
			/ sizeof(struct ext4_extent_idx);
260
#ifdef AGGRESSIVE_TEST
261
262
	if (!check && size > 5)
		size = 5;
263
264
265
266
#endif
	return size;
}

267
static inline int ext4_ext_space_root(struct inode *inode, int check)
268
269
270
271
272
273
{
	int size;

	size = sizeof(EXT4_I(inode)->i_data);
	size -= sizeof(struct ext4_extent_header);
	size /= sizeof(struct ext4_extent);
274
#ifdef AGGRESSIVE_TEST
275
276
	if (!check && size > 3)
		size = 3;
277
278
279
280
#endif
	return size;
}

281
static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
282
283
284
285
286
287
{
	int size;

	size = sizeof(EXT4_I(inode)->i_data);
	size -= sizeof(struct ext4_extent_header);
	size /= sizeof(struct ext4_extent_idx);
288
#ifdef AGGRESSIVE_TEST
289
290
	if (!check && size > 4)
		size = 4;
291
292
293
294
#endif
	return size;
}

295
296
297
298
299
/*
 * Calculate the number of metadata blocks needed
 * to allocate @blocks
 * Worse case is one block per extent
 */
300
int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
301
{
302
	struct ext4_inode_info *ei = EXT4_I(inode);
303
	int idxs;
304

305
306
	idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
		/ sizeof(struct ext4_extent_idx));
307
308

	/*
309
310
311
312
313
314
	 * If the new delayed allocation block is contiguous with the
	 * previous da block, it can share index blocks with the
	 * previous block, so we only need to allocate a new index
	 * block every idxs leaf blocks.  At ldxs**2 blocks, we need
	 * an additional index block, and at ldxs**3 blocks, yet
	 * another index blocks.
315
	 */
316
317
	if (ei->i_da_metadata_calc_len &&
	    ei->i_da_metadata_calc_last_lblock+1 == lblock) {
318
319
		int num = 0;

320
321
322
323
324
325
326
327
328
329
330
331
		if ((ei->i_da_metadata_calc_len % idxs) == 0)
			num++;
		if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
			num++;
		if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
			num++;
			ei->i_da_metadata_calc_len = 0;
		} else
			ei->i_da_metadata_calc_len++;
		ei->i_da_metadata_calc_last_lblock++;
		return num;
	}
332

333
334
335
336
337
338
339
	/*
	 * In the worst case we need a new set of index blocks at
	 * every level of the inode's extent tree.
	 */
	ei->i_da_metadata_calc_len = 1;
	ei->i_da_metadata_calc_last_lblock = lblock;
	return ext_depth(inode) + 1;
340
341
}

342
343
344
345
346
347
348
static int
ext4_ext_max_entries(struct inode *inode, int depth)
{
	int max;

	if (depth == ext_depth(inode)) {
		if (depth == 0)
349
			max = ext4_ext_space_root(inode, 1);
350
		else
351
			max = ext4_ext_space_root_idx(inode, 1);
352
353
	} else {
		if (depth == 0)
354
			max = ext4_ext_space_block(inode, 1);
355
		else
356
			max = ext4_ext_space_block_idx(inode, 1);
357
358
359
360
361
	}

	return max;
}

362
363
static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
{
364
	ext4_fsblk_t block = ext4_ext_pblock(ext);
365
	int len = ext4_ext_get_actual_len(ext);
366

367
368
	if (len == 0)
		return 0;
369
	return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
370
371
372
373
374
}

static int ext4_valid_extent_idx(struct inode *inode,
				struct ext4_extent_idx *ext_idx)
{
375
	ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
376

377
	return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
378
379
380
381
382
383
384
385
386
387
388
389
390
391
}

static int ext4_valid_extent_entries(struct inode *inode,
				struct ext4_extent_header *eh,
				int depth)
{
	unsigned short entries;
	if (eh->eh_entries == 0)
		return 1;

	entries = le16_to_cpu(eh->eh_entries);

	if (depth == 0) {
		/* leaf entries */
392
		struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
393
394
395
396
397
398
399
		while (entries) {
			if (!ext4_valid_extent(inode, ext))
				return 0;
			ext++;
			entries--;
		}
	} else {
400
		struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
401
402
403
404
405
406
407
408
409
410
		while (entries) {
			if (!ext4_valid_extent_idx(inode, ext_idx))
				return 0;
			ext_idx++;
			entries--;
		}
	}
	return 1;
}

411
412
413
static int __ext4_ext_check(const char *function, unsigned int line,
			    struct inode *inode, struct ext4_extent_header *eh,
			    int depth)
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
{
	const char *error_msg;
	int max = 0;

	if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
		error_msg = "invalid magic";
		goto corrupted;
	}
	if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
		error_msg = "unexpected eh_depth";
		goto corrupted;
	}
	if (unlikely(eh->eh_max == 0)) {
		error_msg = "invalid eh_max";
		goto corrupted;
	}
	max = ext4_ext_max_entries(inode, depth);
	if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
		error_msg = "too large eh_max";
		goto corrupted;
	}
	if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
		error_msg = "invalid eh_entries";
		goto corrupted;
	}
439
440
441
442
	if (!ext4_valid_extent_entries(inode, eh, depth)) {
		error_msg = "invalid extent entries";
		goto corrupted;
	}
443
444
445
446
447
448
	/* Verify checksum on non-root extent tree nodes */
	if (ext_depth(inode) != depth &&
	    !ext4_extent_block_csum_verify(inode, eh)) {
		error_msg = "extent tree corrupted";
		goto corrupted;
	}
449
450
451
	return 0;

corrupted:
452
	ext4_error_inode(inode, function, line, 0,
453
			"bad header/extent: %s - magic %x, "
454
			"entries %u, max %u(%u), depth %u(%u)",
455
			error_msg, le16_to_cpu(eh->eh_magic),
456
457
458
459
460
461
			le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
			max, le16_to_cpu(eh->eh_depth), depth);

	return -EIO;
}

462
#define ext4_ext_check(inode, eh, depth)	\
463
	__ext4_ext_check(__func__, __LINE__, inode, eh, depth)
464

465
466
467
468
469
int ext4_ext_check_inode(struct inode *inode)
{
	return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
}

470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
static int __ext4_ext_check_block(const char *function, unsigned int line,
				  struct inode *inode,
				  struct ext4_extent_header *eh,
				  int depth,
				  struct buffer_head *bh)
{
	int ret;

	if (buffer_verified(bh))
		return 0;
	ret = ext4_ext_check(inode, eh, depth);
	if (ret)
		return ret;
	set_buffer_verified(bh);
	return ret;
}

#define ext4_ext_check_block(inode, eh, depth, bh)	\
	__ext4_ext_check_block(__func__, __LINE__, inode, eh, depth, bh)

490
491
492
493
494
495
496
497
#ifdef EXT_DEBUG
static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
{
	int k, l = path->p_depth;

	ext_debug("path:");
	for (k = 0; k <= l; k++, path++) {
		if (path->p_idx) {
498
		  ext_debug("  %d->%llu", le32_to_cpu(path->p_idx->ei_block),
499
			    ext4_idx_pblock(path->p_idx));
500
		} else if (path->p_ext) {
501
			ext_debug("  %d:[%d]%d:%llu ",
502
				  le32_to_cpu(path->p_ext->ee_block),
503
				  ext4_ext_is_uninitialized(path->p_ext),
Amit Arora's avatar
Amit Arora committed
504
				  ext4_ext_get_actual_len(path->p_ext),
505
				  ext4_ext_pblock(path->p_ext));
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
		} else
			ext_debug("  []");
	}
	ext_debug("\n");
}

static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
{
	int depth = ext_depth(inode);
	struct ext4_extent_header *eh;
	struct ext4_extent *ex;
	int i;

	if (!path)
		return;

	eh = path[depth].p_hdr;
	ex = EXT_FIRST_EXTENT(eh);

525
526
	ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);

527
	for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
528
529
		ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
			  ext4_ext_is_uninitialized(ex),
530
			  ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
531
532
533
	}
	ext_debug("\n");
}
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566

static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
			ext4_fsblk_t newblock, int level)
{
	int depth = ext_depth(inode);
	struct ext4_extent *ex;

	if (depth != level) {
		struct ext4_extent_idx *idx;
		idx = path[level].p_idx;
		while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
			ext_debug("%d: move %d:%llu in new index %llu\n", level,
					le32_to_cpu(idx->ei_block),
					ext4_idx_pblock(idx),
					newblock);
			idx++;
		}

		return;
	}

	ex = path[depth].p_ext;
	while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
		ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
				le32_to_cpu(ex->ee_block),
				ext4_ext_pblock(ex),
				ext4_ext_is_uninitialized(ex),
				ext4_ext_get_actual_len(ex),
				newblock);
		ex++;
	}
}

567
#else
568
569
#define ext4_ext_show_path(inode, path)
#define ext4_ext_show_leaf(inode, path)
570
#define ext4_ext_show_move(inode, path, newblock, level)
571
572
#endif

573
void ext4_ext_drop_refs(struct ext4_ext_path *path)
574
575
576
577
578
579
580
581
582
583
584
585
{
	int depth = path->p_depth;
	int i;

	for (i = 0; i <= depth; i++, path++)
		if (path->p_bh) {
			brelse(path->p_bh);
			path->p_bh = NULL;
		}
}

/*
586
587
 * ext4_ext_binsearch_idx:
 * binary search for the closest index of the given block
588
 * the header must be checked before calling this
589
590
 */
static void
Aneesh Kumar K.V's avatar
Aneesh Kumar K.V committed
591
592
ext4_ext_binsearch_idx(struct inode *inode,
			struct ext4_ext_path *path, ext4_lblk_t block)
593
594
595
596
597
{
	struct ext4_extent_header *eh = path->p_hdr;
	struct ext4_extent_idx *r, *l, *m;


598
	ext_debug("binsearch for %u(idx):  ", block);
599
600

	l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhov's avatar
Dmitry Monakhov committed
601
	r = EXT_LAST_INDEX(eh);
602
603
604
605
606
607
	while (l <= r) {
		m = l + (r - l) / 2;
		if (block < le32_to_cpu(m->ei_block))
			r = m - 1;
		else
			l = m + 1;
608
609
610
		ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
				m, le32_to_cpu(m->ei_block),
				r, le32_to_cpu(r->ei_block));
611
612
613
	}

	path->p_idx = l - 1;
614
	ext_debug("  -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block),
615
		  ext4_idx_pblock(path->p_idx));
616
617
618
619
620
621
622
623
624
625

#ifdef CHECK_BINSEARCH
	{
		struct ext4_extent_idx *chix, *ix;
		int k;

		chix = ix = EXT_FIRST_INDEX(eh);
		for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
		  if (k != 0 &&
		      le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
626
627
628
629
				printk(KERN_DEBUG "k=%d, ix=0x%p, "
				       "first=0x%p\n", k,
				       ix, EXT_FIRST_INDEX(eh));
				printk(KERN_DEBUG "%u <= %u\n",
630
631
632
633
				       le32_to_cpu(ix->ei_block),
				       le32_to_cpu(ix[-1].ei_block));
			}
			BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp's avatar
Dave Kleikamp committed
634
					   <= le32_to_cpu(ix[-1].ei_block));
635
636
637
638
639
640
641
642
643
644
645
			if (block < le32_to_cpu(ix->ei_block))
				break;
			chix = ix;
		}
		BUG_ON(chix != path->p_idx);
	}
#endif

}

/*
646
647
 * ext4_ext_binsearch:
 * binary search for closest extent of the given block
648
 * the header must be checked before calling this
649
650
 */
static void
Aneesh Kumar K.V's avatar
Aneesh Kumar K.V committed
651
652
ext4_ext_binsearch(struct inode *inode,
		struct ext4_ext_path *path, ext4_lblk_t block)
653
654
655
656
657
658
{
	struct ext4_extent_header *eh = path->p_hdr;
	struct ext4_extent *r, *l, *m;

	if (eh->eh_entries == 0) {
		/*
659
660
		 * this leaf is empty:
		 * we get such a leaf in split/add case
661
662
663
664
		 */
		return;
	}

665
	ext_debug("binsearch for %u:  ", block);
666
667

	l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhov's avatar
Dmitry Monakhov committed
668
	r = EXT_LAST_EXTENT(eh);
669
670
671
672
673
674
675

	while (l <= r) {
		m = l + (r - l) / 2;
		if (block < le32_to_cpu(m->ee_block))
			r = m - 1;
		else
			l = m + 1;
676
677
678
		ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
				m, le32_to_cpu(m->ee_block),
				r, le32_to_cpu(r->ee_block));
679
680
681
	}

	path->p_ext = l - 1;
682
	ext_debug("  -> %d:%llu:[%d]%d ",
Dave Kleikamp's avatar
Dave Kleikamp committed
683
			le32_to_cpu(path->p_ext->ee_block),
684
			ext4_ext_pblock(path->p_ext),
685
			ext4_ext_is_uninitialized(path->p_ext),
Amit Arora's avatar
Amit Arora committed
686
			ext4_ext_get_actual_len(path->p_ext));
687
688
689
690
691
692
693
694
695

#ifdef CHECK_BINSEARCH
	{
		struct ext4_extent *chex, *ex;
		int k;

		chex = ex = EXT_FIRST_EXTENT(eh);
		for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
			BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp's avatar
Dave Kleikamp committed
696
					  <= le32_to_cpu(ex[-1].ee_block));
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
			if (block < le32_to_cpu(ex->ee_block))
				break;
			chex = ex;
		}
		BUG_ON(chex != path->p_ext);
	}
#endif

}

int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
{
	struct ext4_extent_header *eh;

	eh = ext_inode_hdr(inode);
	eh->eh_depth = 0;
	eh->eh_entries = 0;
	eh->eh_magic = EXT4_EXT_MAGIC;
715
	eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
716
717
718
719
720
721
	ext4_mark_inode_dirty(handle, inode);
	ext4_ext_invalidate_cache(inode);
	return 0;
}

struct ext4_ext_path *
Aneesh Kumar K.V's avatar
Aneesh Kumar K.V committed
722
723
ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
					struct ext4_ext_path *path)
724
725
726
727
728
729
{
	struct ext4_extent_header *eh;
	struct buffer_head *bh;
	short int depth, i, ppos = 0, alloc = 0;

	eh = ext_inode_hdr(inode);
730
	depth = ext_depth(inode);
731
732
733

	/* account possible depth increase */
	if (!path) {
734
		path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
735
736
737
738
739
740
				GFP_NOFS);
		if (!path)
			return ERR_PTR(-ENOMEM);
		alloc = 1;
	}
	path[0].p_hdr = eh;
741
	path[0].p_bh = NULL;
742

743
	i = depth;
744
745
746
747
	/* walk through the tree */
	while (i) {
		ext_debug("depth %d: num %d, max %d\n",
			  ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
748

749
		ext4_ext_binsearch_idx(inode, path + ppos, block);
750
		path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
751
752
753
		path[ppos].p_depth = i;
		path[ppos].p_ext = NULL;

754
755
		bh = sb_getblk(inode->i_sb, path[ppos].p_block);
		if (unlikely(!bh))
756
			goto err;
757
		if (!bh_uptodate_or_lock(bh)) {
758
759
			trace_ext4_ext_load_extent(inode, block,
						path[ppos].p_block);
760
761
762
763
764
			if (bh_submit_read(bh) < 0) {
				put_bh(bh);
				goto err;
			}
		}
765
766
		eh = ext_block_hdr(bh);
		ppos++;
767
768
769
770
771
772
		if (unlikely(ppos > depth)) {
			put_bh(bh);
			EXT4_ERROR_INODE(inode,
					 "ppos %d > depth %d", ppos, depth);
			goto err;
		}
773
774
775
776
		path[ppos].p_bh = bh;
		path[ppos].p_hdr = eh;
		i--;

777
		if (ext4_ext_check_block(inode, eh, i, bh))
778
779
780
781
782
783
784
785
786
			goto err;
	}

	path[ppos].p_depth = i;
	path[ppos].p_ext = NULL;
	path[ppos].p_idx = NULL;

	/* find extent */
	ext4_ext_binsearch(inode, path + ppos, block);
787
788
	/* if not an empty leaf */
	if (path[ppos].p_ext)
789
		path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
790
791
792
793
794
795
796
797
798
799
800
801
802

	ext4_ext_show_path(inode, path);

	return path;

err:
	ext4_ext_drop_refs(path);
	if (alloc)
		kfree(path);
	return ERR_PTR(-EIO);
}

/*
803
804
805
 * ext4_ext_insert_index:
 * insert new index [@logical;@ptr] into the block at @curp;
 * check where to insert: before @curp or after @curp
806
 */
807
808
809
static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
				 struct ext4_ext_path *curp,
				 int logical, ext4_fsblk_t ptr)
810
811
812
813
{
	struct ext4_extent_idx *ix;
	int len, err;

814
815
	err = ext4_ext_get_access(handle, inode, curp);
	if (err)
816
817
		return err;

818
819
820
821
822
823
	if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
		EXT4_ERROR_INODE(inode,
				 "logical %d == ei_block %d!",
				 logical, le32_to_cpu(curp->p_idx->ei_block));
		return -EIO;
	}
824
825
826
827
828
829
830
831
832
833

	if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
			     >= le16_to_cpu(curp->p_hdr->eh_max))) {
		EXT4_ERROR_INODE(inode,
				 "eh_entries %d >= eh_max %d!",
				 le16_to_cpu(curp->p_hdr->eh_entries),
				 le16_to_cpu(curp->p_hdr->eh_max));
		return -EIO;
	}

834
835
	if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
		/* insert after */
836
		ext_debug("insert new index %d after: %llu\n", logical, ptr);
837
838
839
		ix = curp->p_idx + 1;
	} else {
		/* insert before */
840
		ext_debug("insert new index %d before: %llu\n", logical, ptr);
841
842
843
		ix = curp->p_idx;
	}

844
845
846
847
848
849
850
851
852
	len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
	BUG_ON(len < 0);
	if (len > 0) {
		ext_debug("insert new index %d: "
				"move %d indices from 0x%p to 0x%p\n",
				logical, len, ix, ix + 1);
		memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
	}

853
854
855
856
857
	if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
		EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
		return -EIO;
	}

858
	ix->ei_block = cpu_to_le32(logical);
859
	ext4_idx_store_pblock(ix, ptr);
Marcin Slusarz's avatar
Marcin Slusarz committed
860
	le16_add_cpu(&curp->p_hdr->eh_entries, 1);
861

862
863
864
865
	if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
		EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
		return -EIO;
	}
866
867
868
869
870
871
872
873

	err = ext4_ext_dirty(handle, inode, curp);
	ext4_std_error(inode->i_sb, err);

	return err;
}

/*
874
875
876
877
878
879
880
881
 * ext4_ext_split:
 * inserts new subtree into the path, using free index entry
 * at depth @at:
 * - allocates all needed blocks (new leaf and all intermediate index blocks)
 * - makes decision where to split
 * - moves remaining extents and index entries (right to the split point)
 *   into the newly allocated blocks
 * - initializes subtree
882
883
 */
static int ext4_ext_split(handle_t *handle, struct inode *inode,
884
885
886
			  unsigned int flags,
			  struct ext4_ext_path *path,
			  struct ext4_extent *newext, int at)
887
888
889
890
891
892
{
	struct buffer_head *bh = NULL;
	int depth = ext_depth(inode);
	struct ext4_extent_header *neh;
	struct ext4_extent_idx *fidx;
	int i = at, k, m, a;
893
	ext4_fsblk_t newblock, oldblock;
894
	__le32 border;
895
	ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
896
897
898
	int err = 0;

	/* make decision: where to split? */
899
	/* FIXME: now decision is simplest: at current extent */
900

901
	/* if current leaf will be split, then we should use
902
	 * border from split point */
903
904
905
906
	if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
		EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
		return -EIO;
	}
907
908
	if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
		border = path[depth].p_ext[1].ee_block;
909
		ext_debug("leaf will be split."
910
				" next leaf starts at %d\n",
Dave Kleikamp's avatar
Dave Kleikamp committed
911
				  le32_to_cpu(border));
912
913
914
915
	} else {
		border = newext->ee_block;
		ext_debug("leaf will be added."
				" next leaf starts at %d\n",
Dave Kleikamp's avatar
Dave Kleikamp committed
916
				le32_to_cpu(border));
917
918
919
	}

	/*
920
921
	 * If error occurs, then we break processing
	 * and mark filesystem read-only. index won't
922
	 * be inserted and tree will be in consistent
923
	 * state. Next mount will repair buffers too.
924
925
926
	 */

	/*
927
928
929
	 * Get array to track all allocated blocks.
	 * We need this to handle errors and free blocks
	 * upon them.
930
	 */
931
	ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
932
933
934
935
936
937
	if (!ablocks)
		return -ENOMEM;

	/* allocate all needed blocks */
	ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
	for (a = 0; a < depth - at; a++) {
938
		newblock = ext4_ext_new_meta_block(handle, inode, path,
939
						   newext, &err, flags);
940
941
942
943
944
945
946
		if (newblock == 0)
			goto cleanup;
		ablocks[a] = newblock;
	}

	/* initialize new leaf */
	newblock = ablocks[--a];
947
948
949
950
951
	if (unlikely(newblock == 0)) {
		EXT4_ERROR_INODE(inode, "newblock == 0!");
		err = -EIO;
		goto cleanup;
	}
952
953
954
955
956
957
958
	bh = sb_getblk(inode->i_sb, newblock);
	if (!bh) {
		err = -EIO;
		goto cleanup;
	}
	lock_buffer(bh);

959
960
	err = ext4_journal_get_create_access(handle, bh);
	if (err)
961
962
963
964
		goto cleanup;

	neh = ext_block_hdr(bh);
	neh->eh_entries = 0;
965
	neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
966
967
968
	neh->eh_magic = EXT4_EXT_MAGIC;
	neh->eh_depth = 0;

969
	/* move remainder of path[depth] to the new leaf */
970
971
972
973
974
975
976
977
	if (unlikely(path[depth].p_hdr->eh_entries !=
		     path[depth].p_hdr->eh_max)) {
		EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
				 path[depth].p_hdr->eh_entries,
				 path[depth].p_hdr->eh_max);
		err = -EIO;
		goto cleanup;
	}
978
	/* start copy from next extent */
979
980
	m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
	ext4_ext_show_move(inode, path, newblock, depth);
981
	if (m) {
982
983
984
		struct ext4_extent *ex;
		ex = EXT_FIRST_EXTENT(neh);
		memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarz's avatar
Marcin Slusarz committed
985
		le16_add_cpu(&neh->eh_entries, m);
986
987
	}

988
	ext4_extent_block_csum_set(inode, neh);
989
990
991
	set_buffer_uptodate(bh);
	unlock_buffer(bh);

992
	err = ext4_handle_dirty_metadata(handle, inode, bh);
993
	if (err)
994
995
996
997
998
999
		goto cleanup;
	brelse(bh);
	bh = NULL;

	/* correct old leaf */
	if (m) {
1000
1001
		err = ext4_ext_get_access(handle, inode, path + depth);
		if (err)
1002
			goto cleanup;
Marcin Slusarz's avatar
Marcin Slusarz committed
1003
		le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
1004
1005
		err = ext4_ext_dirty(handle, inode, path + depth);
		if (err)
1006
1007
1008
1009
1010
1011
			goto cleanup;

	}

	/* create intermediate indexes */
	k = depth - at - 1;
1012
1013
1014
1015
1016
	if (unlikely(k < 0)) {
		EXT4_ERROR_INODE(inode, "k %d < 0!", k);
		err = -EIO;
		goto cleanup;
	}
1017
1018
1019
1020
1021
1022
1023
1024
	if (k)
		ext_debug("create %d intermediate indices\n", k);
	/* insert new index into current index block */
	/* current depth stored in i var */
	i = depth - 1;
	while (k--) {
		oldblock = newblock;
		newblock = ablocks[--a];
1025
		bh = sb_getblk(inode->i_sb, newblock);
1026
1027
1028
1029
1030
1031
		if (!bh) {
			err = -EIO;
			goto cleanup;
		}
		lock_buffer(bh);

1032
1033
		err = ext4_journal_get_create_access(handle, bh);
		if (err)
1034
1035
1036
1037
1038
			goto cleanup;

		neh = ext_block_hdr(bh);
		neh->eh_entries = cpu_to_le16(1);
		neh->eh_magic = EXT4_EXT_MAGIC;
1039
		neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
1040
1041
1042
		neh->eh_depth = cpu_to_le16(depth - i);
		fidx = EXT_FIRST_INDEX(neh);
		fidx->ei_block = border;
1043
		ext4_idx_store_pblock(fidx, oldblock);
1044

1045
1046
		ext_debug("int.index at %d (block %llu): %u -> %llu\n",
				i, newblock, le32_to_cpu(border), oldblock);
1047

1048
		/* move remainder of path[i] to the new index block */
1049
1050
1051
1052
1053
1054
1055
1056
		if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
					EXT_LAST_INDEX(path[i].p_hdr))) {
			EXT4_ERROR_INODE(inode,
					 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
					 le32_to_cpu(path[i].p_ext->ee_block));
			err = -EIO;
			goto cleanup;
		}
1057
1058
1059
1060
1061
		/* start copy indexes */
		m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
		ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
				EXT_MAX_INDEX(path[i].p_hdr));
		ext4_ext_show_move(inode, path, newblock, i);
1062
		if (m) {
1063
			memmove(++fidx, path[i].p_idx,
1064
				sizeof(struct ext4_extent_idx) * m);
Marcin Slusarz's avatar
Marcin Slusarz committed
1065
			le16_add_cpu(&neh->eh_entries, m);
1066
		}
1067
		ext4_extent_block_csum_set(inode, neh);
1068
1069
1070
		set_buffer_uptodate(bh);
		unlock_buffer(bh);

1071
		err = ext4_handle_dirty_metadata(handle, inode, bh);
1072
		if (err)
1073
1074
1075
1076
1077
1078
1079
1080
1081
			goto cleanup;
		brelse(bh);
		bh = NULL;

		/* correct old index */
		if (m) {
			err = ext4_ext_get_access(handle, inode, path + i);
			if (err)
				goto cleanup;
Marcin Slusarz's avatar
Marcin Slusarz committed
1082
			le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
			err = ext4_ext_dirty(handle, inode, path + i);
			if (err)
				goto cleanup;
		}

		i--;
	}

	/* insert new index */
	err = ext4_ext_insert_index(handle, inode, path + at,
				    le32_to_cpu(border), newblock);

cleanup:
	if (bh) {
		if (buffer_locked(bh))
			unlock_buffer(bh);
		brelse(bh);
	}

	if (err) {
		/* free all allocated blocks in error case */
		for (i = 0; i < depth; i++) {
			if (!ablocks[i])
				continue;
1107
			ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
1108
					 EXT4_FREE_BLOCKS_METADATA);
1109
1110
1111
1112
1113
1114
1115
1116
		}
	}
	kfree(ablocks);

	return err;
}

/*
1117
1118
1119
1120
1121
1122
 * ext4_ext_grow_indepth:
 * implements tree growing procedure:
 * - allocates new block
 * - moves top-level data (index block or leaf) into the new block
 * - initializes new top-level, creating index that points to the
 *   just created block
1123
1124
 */
static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
1125
1126
				 unsigned int flags,
				 struct ext4_extent *newext)
1127
1128
1129
{
	struct ext4_extent_header *neh;
	struct buffer_head *bh;
1130
	ext4_fsblk_t newblock;
1131
1132
	int err = 0;

1133
	newblock = ext4_ext_new_meta_block(handle, inode, NULL,
1134
		newext, &err, flags);
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
	if (newblock == 0)
		return err;

	bh = sb_getblk(inode->i_sb, newblock);
	if (!bh) {
		err = -EIO;
		ext4_std_error(inode->i_sb, err);
		return err;
	}
	lock_buffer(bh);

1146
1147
	err = ext4_journal_get_create_access(handle, bh);
	if (err) {
1148
1149
1150
1151
1152
		unlock_buffer(bh);
		goto out;
	}

	/* move top-level index/leaf into new block */
1153
1154
	memmove(bh->b_data, EXT4_I(inode)->i_data,
		sizeof(EXT4_I(inode)->i_data));
1155
1156
1157
1158
1159
1160

	/* set size of new block */
	neh = ext_block_hdr(bh);
	/* old root could have indexes or leaves
	 * so calculate e_max right way */
	if (ext_depth(inode))
1161
		neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
1162
	else
1163
		neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
1164
	neh->eh_magic = EXT4_EXT_MAGIC;
1165
	ext4_extent_block_csum_set(inode, neh);
1166
1167
1168
	set_buffer_uptodate(bh);
	unlock_buffer(bh);

1169
	err = ext4_handle_dirty_metadata(handle, inode, bh);
1170
	if (err)
1171
1172
		goto out;

1173
	/* Update top-level index: num,max,pointer */
1174
	neh = ext_inode_hdr(inode);
1175
1176
1177
1178
1179
1180
1181
1182
	neh->eh_entries = cpu_to_le16(1);
	ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
	if (neh->eh_depth == 0) {
		/* Root extent block becomes index block */
		neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
		EXT_FIRST_INDEX(neh)->ei_block =
			EXT_FIRST_EXTENT(neh)->ee_block;
	}
1183
	ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1184
		  le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
1185
		  le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
1186
		  ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
1187

1188
	le16_add_cpu(&neh->eh_depth, 1);
1189
	ext4_mark_inode_dirty(handle, inode);
1190
1191
1192
1193
1194
1195
1196
out:
	brelse(bh);

	return err;
}

/*
1197
1198
1199
 * ext4_ext_create_new_leaf:
 * finds empty index and adds new leaf.
 * if no free index is found, then it requests in-depth growing.
1200
1201
 */
static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
1202
1203
1204
				    unsigned int flags,
				    struct ext4_ext_path *path,
				    struct ext4_extent *newext)
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
{
	struct ext4_ext_path *curp;
	int depth, i, err = 0;

repeat:
	i = depth = ext_depth(inode);

	/* walk up to the tree and look for free index entry */
	curp = path + depth;
	while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
		i--;
		curp--;
	}

1219
1220
	/* we use already allocated block for index block,
	 * so subsequent data blocks should be contiguous */
1221
1222
1223
	if (EXT_HAS_FREE_INDEX(curp)) {
		/* if we found index with free entry, then use that
		 * entry: create all needed subtree and add new leaf */
1224
		err = ext4_ext_split(handle, inode, flags, path, newext, i);
1225
1226
		if (err)
			goto out;
1227
1228
1229
1230

		/* refill path */
		ext4_ext_drop_refs(path);
		path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V's avatar
Aneesh Kumar K.V committed
1231
1232
				    (ext4_lblk_t)le32_to_cpu(newext->ee_block),
				    path);
1233
1234
1235
1236
		if (IS_ERR(path))
			err = PTR_ERR(path);
	} else {
		/* tree is full, time to grow in depth */
1237
		err = ext4_ext_grow_indepth(handle, inode, flags, newext);
1238
1239
1240
1241
1242
1243
		if (err)
			goto out;

		/* refill path */
		ext4_ext_drop_refs(path);
		path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V's avatar
Aneesh Kumar K.V committed
1244
1245
				   (ext4_lblk_t)le32_to_cpu(newext->ee_block),
				    path);
1246
1247
1248
1249
1250
1251
		if (IS_ERR(path)) {
			err = PTR_ERR(path);
			goto out;
		}

		/*
1252
1253
		 * only first (depth 0 -> 1) produces free space;
		 * in all other cases we have to split the grown tree
1254
1255
1256
		 */
		depth = ext_depth(inode);
		if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
1257
			/* now we need to split */
1258
1259
1260
1261
1262
1263
1264
1265
			goto repeat;
		}
	}

out:
	return err;
}

1266
1267
1268
1269
1270
1271
1272
/*
 * search the closest allocated block to the left for *logical
 * and returns it at @logical + it's physical address at @phys
 * if *logical is the smallest allocated block, the function
 * returns 0 at @phys
 * return value contains 0 (success) or error code
 */
1273
1274
1275
static int ext4_ext_search_left(struct inode *inode,
				struct ext4_ext_path *path,
				ext4_lblk_t *logical, ext4_fsblk_t *phys)
1276
1277
1278
{
	struct ext4_extent_idx *ix;
	struct ext4_extent *ex;
1279
	int depth, ee_len;
1280

1281
1282
1283
1284
	if (unlikely(path == NULL)) {
		EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
		return -EIO;
	}
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
	depth = path->p_depth;
	*phys = 0;

	if (depth == 0 && path->p_ext == NULL)
		return 0;

	/* usually extent in the path covers blocks smaller
	 * then *logical, but it can be that extent is the
	 * first one in the file */

	ex = path[depth].p_ext;
1296
	ee_len = ext4_ext_get_actual_len(ex);
1297
	if (*logical < le32_to_cpu(ex->ee_block)) {
1298
1299
1300
1301
1302
1303
		if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
			EXT4_ERROR_INODE(inode,
					 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
					 *logical, le32_to_cpu(ex->ee_block));
			return -EIO;
		}
1304