extents.c 103 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
34
/*
 * 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/module.h>
#include <linux/fs.h>
#include <linux/time.h>
35
#include <linux/jbd2.h>
36
37
38
39
40
#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
41
#include <linux/falloc.h>
42
#include <asm/uaccess.h>
43
#include <linux/fiemap.h>
44
45
#include "ext4_jbd2.h"
#include "ext4_extents.h"
46

47
48
49
static int ext4_ext_truncate_extend_restart(handle_t *handle,
					    struct inode *inode,
					    int needed)
50
51
52
{
	int err;

53
54
	if (!ext4_handle_valid(handle))
		return 0;
55
	if (handle->h_buffer_credits > needed)
56
57
		return 0;
	err = ext4_journal_extend(handle, needed);
58
	if (err <= 0)
59
		return err;
60
	err = ext4_truncate_restart_trans(handle, inode, needed);
61
62
	if (err == 0)
		err = -EAGAIN;
63
64

	return err;
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
}

/*
 * 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
 */
static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
				struct ext4_ext_path *path)
{
	int err;
	if (path->p_bh) {
		/* path points to block */
96
		err = ext4_handle_dirty_metadata(handle, inode, path->p_bh);
97
98
99
100
101
102
103
	} else {
		/* path points to leaf/index in inode body */
		err = ext4_mark_inode_dirty(handle, inode);
	}
	return err;
}

104
static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
105
			      struct ext4_ext_path *path,
Aneesh Kumar K.V's avatar
Aneesh Kumar K.V committed
106
			      ext4_lblk_t block)
107
108
{
	struct ext4_inode_info *ei = EXT4_I(inode);
109
	ext4_fsblk_t bg_start;
110
	ext4_fsblk_t last_block;
111
	ext4_grpblk_t colour;
112
113
	ext4_group_t block_group;
	int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb));
114
115
116
117
118
119
	int depth;

	if (path) {
		struct ext4_extent *ex;
		depth = path->p_depth;

120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
		/*
		 * 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,
		 * especiially if the latter case turns out to be
		 * common.
		 */
137
		ex = path[depth].p_ext;
138
139
140
141
142
143
144
145
146
		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);
		}
147

148
149
		/* it looks like index is empty;
		 * try to find starting block from index itself */
150
151
152
153
154
		if (path[depth].p_bh)
			return path[depth].p_bh->b_blocknr;
	}

	/* OK. use inode's group */
155
156
157
158
	block_group = ei->i_block_group;
	if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) {
		/*
		 * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME
159
160
		 * block groups per flexgroup, reserve the first block
		 * group for directories and special files.  Regular
161
		 * files will start at the second block group.  This
162
		 * tends to speed up directory access and improves
163
164
165
166
167
168
		 * fsck times.
		 */
		block_group &= ~(flex_size-1);
		if (S_ISREG(inode->i_mode))
			block_group++;
	}
169
	bg_start = ext4_group_first_block_no(inode->i_sb, block_group);
170
171
	last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;

172
173
174
175
176
177
178
	/*
	 * If we are doing delayed allocation, we don't need take
	 * colour into account.
	 */
	if (test_opt(inode->i_sb, DELALLOC))
		return bg_start;

179
180
	if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
		colour = (current->pid % 16) *
181
			(EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
182
183
	else
		colour = (current->pid % 16) * ((last_block - bg_start) / 16);
184
185
186
	return bg_start + colour + block;
}

187
188
189
/*
 * Allocation for a meta data block
 */
190
static ext4_fsblk_t
191
ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
192
193
194
			struct ext4_ext_path *path,
			struct ext4_extent *ex, int *err)
{
195
	ext4_fsblk_t goal, newblock;
196
197

	goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Theodore Ts'o's avatar
Theodore Ts'o committed
198
	newblock = ext4_new_meta_blocks(handle, inode, goal, NULL, err);
199
200
201
	return newblock;
}

202
static inline int ext4_ext_space_block(struct inode *inode, int check)
203
204
205
206
207
{
	int size;

	size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
			/ sizeof(struct ext4_extent);
208
	if (!check) {
209
#ifdef AGGRESSIVE_TEST
210
211
		if (size > 6)
			size = 6;
212
#endif
213
	}
214
215
216
	return size;
}

217
static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
218
219
220
221
222
{
	int size;

	size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
			/ sizeof(struct ext4_extent_idx);
223
	if (!check) {
224
#ifdef AGGRESSIVE_TEST
225
226
		if (size > 5)
			size = 5;
227
#endif
228
	}
229
230
231
	return size;
}

232
static inline int ext4_ext_space_root(struct inode *inode, int check)
233
234
235
236
237
238
{
	int size;

	size = sizeof(EXT4_I(inode)->i_data);
	size -= sizeof(struct ext4_extent_header);
	size /= sizeof(struct ext4_extent);
239
	if (!check) {
240
#ifdef AGGRESSIVE_TEST
241
242
		if (size > 3)
			size = 3;
243
#endif
244
	}
245
246
247
	return size;
}

248
static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
249
250
251
252
253
254
{
	int size;

	size = sizeof(EXT4_I(inode)->i_data);
	size -= sizeof(struct ext4_extent_header);
	size /= sizeof(struct ext4_extent_idx);
255
	if (!check) {
256
#ifdef AGGRESSIVE_TEST
257
258
		if (size > 4)
			size = 4;
259
#endif
260
	}
261
262
263
	return size;
}

264
265
266
267
268
/*
 * Calculate the number of metadata blocks needed
 * to allocate @blocks
 * Worse case is one block per extent
 */
269
int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
270
{
271
272
	struct ext4_inode_info *ei = EXT4_I(inode);
	int idxs, num = 0;
273

274
275
	idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
		/ sizeof(struct ext4_extent_idx));
276
277

	/*
278
279
280
281
282
283
	 * 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.
284
	 */
285
286
287
288
289
290
291
292
293
294
295
296
297
298
	if (ei->i_da_metadata_calc_len &&
	    ei->i_da_metadata_calc_last_lblock+1 == lblock) {
		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;
	}
299

300
301
302
303
304
305
306
	/*
	 * 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;
307
308
}

309
310
311
312
313
314
315
static int
ext4_ext_max_entries(struct inode *inode, int depth)
{
	int max;

	if (depth == ext_depth(inode)) {
		if (depth == 0)
316
			max = ext4_ext_space_root(inode, 1);
317
		else
318
			max = ext4_ext_space_root_idx(inode, 1);
319
320
	} else {
		if (depth == 0)
321
			max = ext4_ext_space_block(inode, 1);
322
		else
323
			max = ext4_ext_space_block_idx(inode, 1);
324
325
326
327
328
	}

	return max;
}

329
330
static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
{
331
	ext4_fsblk_t block = ext4_ext_pblock(ext);
332
	int len = ext4_ext_get_actual_len(ext);
333

334
	return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
335
336
337
338
339
}

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

342
	return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
}

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

	entries = le16_to_cpu(eh->eh_entries);

	if (depth == 0) {
		/* leaf entries */
		ext = EXT_FIRST_EXTENT(eh);
		while (entries) {
			if (!ext4_valid_extent(inode, ext))
				return 0;
			ext++;
			entries--;
		}
	} else {
		ext_idx = EXT_FIRST_INDEX(eh);
		while (entries) {
			if (!ext4_valid_extent_idx(inode, ext_idx))
				return 0;
			ext_idx++;
			entries--;
		}
	}
	return 1;
}

378
379
380
static int __ext4_ext_check(const char *function, unsigned int line,
			    struct inode *inode, struct ext4_extent_header *eh,
			    int depth)
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
{
	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;
	}
406
407
408
409
	if (!ext4_valid_extent_entries(inode, eh, depth)) {
		error_msg = "invalid extent entries";
		goto corrupted;
	}
410
411
412
	return 0;

corrupted:
413
	ext4_error_inode(inode, function, line, 0,
414
			"bad header/extent: %s - magic %x, "
415
			"entries %u, max %u(%u), depth %u(%u)",
416
			error_msg, le16_to_cpu(eh->eh_magic),
417
418
419
420
421
422
			le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
			max, le16_to_cpu(eh->eh_depth), depth);

	return -EIO;
}

423
#define ext4_ext_check(inode, eh, depth)	\
424
	__ext4_ext_check(__func__, __LINE__, inode, eh, depth)
425

426
427
428
429
430
int ext4_ext_check_inode(struct inode *inode)
{
	return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
}

431
432
433
434
435
436
437
438
#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) {
439
		  ext_debug("  %d->%llu", le32_to_cpu(path->p_idx->ei_block),
440
			    ext4_idx_pblock(path->p_idx));
441
		} else if (path->p_ext) {
442
			ext_debug("  %d:[%d]%d:%llu ",
443
				  le32_to_cpu(path->p_ext->ee_block),
444
				  ext4_ext_is_uninitialized(path->p_ext),
Amit Arora's avatar
Amit Arora committed
445
				  ext4_ext_get_actual_len(path->p_ext),
446
				  ext4_ext_pblock(path->p_ext));
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
		} 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);

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

468
	for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
469
470
		ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
			  ext4_ext_is_uninitialized(ex),
471
			  ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
472
473
474
475
	}
	ext_debug("\n");
}
#else
476
477
#define ext4_ext_show_path(inode, path)
#define ext4_ext_show_leaf(inode, path)
478
479
#endif

480
void ext4_ext_drop_refs(struct ext4_ext_path *path)
481
482
483
484
485
486
487
488
489
490
491
492
{
	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;
		}
}

/*
493
494
 * ext4_ext_binsearch_idx:
 * binary search for the closest index of the given block
495
 * the header must be checked before calling this
496
497
 */
static void
Aneesh Kumar K.V's avatar
Aneesh Kumar K.V committed
498
499
ext4_ext_binsearch_idx(struct inode *inode,
			struct ext4_ext_path *path, ext4_lblk_t block)
500
501
502
503
504
{
	struct ext4_extent_header *eh = path->p_hdr;
	struct ext4_extent_idx *r, *l, *m;


505
	ext_debug("binsearch for %u(idx):  ", block);
506
507

	l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhov's avatar
Dmitry Monakhov committed
508
	r = EXT_LAST_INDEX(eh);
509
510
511
512
513
514
	while (l <= r) {
		m = l + (r - l) / 2;
		if (block < le32_to_cpu(m->ei_block))
			r = m - 1;
		else
			l = m + 1;
515
516
517
		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));
518
519
520
	}

	path->p_idx = l - 1;
521
	ext_debug("  -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
522
		  ext4_idx_pblock(path->p_idx));
523
524
525
526
527
528
529
530
531
532

#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)) {
533
534
535
536
				printk(KERN_DEBUG "k=%d, ix=0x%p, "
				       "first=0x%p\n", k,
				       ix, EXT_FIRST_INDEX(eh));
				printk(KERN_DEBUG "%u <= %u\n",
537
538
539
540
				       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
541
					   <= le32_to_cpu(ix[-1].ei_block));
542
543
544
545
546
547
548
549
550
551
552
			if (block < le32_to_cpu(ix->ei_block))
				break;
			chix = ix;
		}
		BUG_ON(chix != path->p_idx);
	}
#endif

}

/*
553
554
 * ext4_ext_binsearch:
 * binary search for closest extent of the given block
555
 * the header must be checked before calling this
556
557
 */
static void
Aneesh Kumar K.V's avatar
Aneesh Kumar K.V committed
558
559
ext4_ext_binsearch(struct inode *inode,
		struct ext4_ext_path *path, ext4_lblk_t block)
560
561
562
563
564
565
{
	struct ext4_extent_header *eh = path->p_hdr;
	struct ext4_extent *r, *l, *m;

	if (eh->eh_entries == 0) {
		/*
566
567
		 * this leaf is empty:
		 * we get such a leaf in split/add case
568
569
570
571
		 */
		return;
	}

572
	ext_debug("binsearch for %u:  ", block);
573
574

	l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhov's avatar
Dmitry Monakhov committed
575
	r = EXT_LAST_EXTENT(eh);
576
577
578
579
580
581
582

	while (l <= r) {
		m = l + (r - l) / 2;
		if (block < le32_to_cpu(m->ee_block))
			r = m - 1;
		else
			l = m + 1;
583
584
585
		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));
586
587
588
	}

	path->p_ext = l - 1;
589
	ext_debug("  -> %d:%llu:[%d]%d ",
Dave Kleikamp's avatar
Dave Kleikamp committed
590
			le32_to_cpu(path->p_ext->ee_block),
591
			ext4_ext_pblock(path->p_ext),
592
			ext4_ext_is_uninitialized(path->p_ext),
Amit Arora's avatar
Amit Arora committed
593
			ext4_ext_get_actual_len(path->p_ext));
594
595
596
597
598
599
600
601
602

#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
603
					  <= le32_to_cpu(ex[-1].ee_block));
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
			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;
622
	eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
623
624
625
626
627
628
	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
629
630
ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
					struct ext4_ext_path *path)
631
632
633
634
635
636
{
	struct ext4_extent_header *eh;
	struct buffer_head *bh;
	short int depth, i, ppos = 0, alloc = 0;

	eh = ext_inode_hdr(inode);
637
	depth = ext_depth(inode);
638
639
640

	/* account possible depth increase */
	if (!path) {
641
		path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
642
643
644
645
646
647
				GFP_NOFS);
		if (!path)
			return ERR_PTR(-ENOMEM);
		alloc = 1;
	}
	path[0].p_hdr = eh;
648
	path[0].p_bh = NULL;
649

650
	i = depth;
651
652
	/* walk through the tree */
	while (i) {
653
654
		int need_to_validate = 0;

655
656
		ext_debug("depth %d: num %d, max %d\n",
			  ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
657

658
		ext4_ext_binsearch_idx(inode, path + ppos, block);
659
		path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
660
661
662
		path[ppos].p_depth = i;
		path[ppos].p_ext = NULL;

663
664
		bh = sb_getblk(inode->i_sb, path[ppos].p_block);
		if (unlikely(!bh))
665
			goto err;
666
667
668
669
670
671
672
673
		if (!bh_uptodate_or_lock(bh)) {
			if (bh_submit_read(bh) < 0) {
				put_bh(bh);
				goto err;
			}
			/* validate the extent entries */
			need_to_validate = 1;
		}
674
675
		eh = ext_block_hdr(bh);
		ppos++;
676
677
678
679
680
681
		if (unlikely(ppos > depth)) {
			put_bh(bh);
			EXT4_ERROR_INODE(inode,
					 "ppos %d > depth %d", ppos, depth);
			goto err;
		}
682
683
684
685
		path[ppos].p_bh = bh;
		path[ppos].p_hdr = eh;
		i--;

686
		if (need_to_validate && ext4_ext_check(inode, eh, i))
687
688
689
690
691
692
693
694
695
			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);
696
697
	/* if not an empty leaf */
	if (path[ppos].p_ext)
698
		path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
699
700
701
702
703
704
705
706
707
708
709
710
711

	ext4_ext_show_path(inode, path);

	return path;

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

/*
712
713
714
 * ext4_ext_insert_index:
 * insert new index [@logical;@ptr] into the block at @curp;
 * check where to insert: before @curp or after @curp
715
 */
716
717
718
static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
				 struct ext4_ext_path *curp,
				 int logical, ext4_fsblk_t ptr)
719
720
721
722
{
	struct ext4_extent_idx *ix;
	int len, err;

723
724
	err = ext4_ext_get_access(handle, inode, curp);
	if (err)
725
726
		return err;

727
728
729
730
731
732
	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;
	}
733
734
735
736
737
738
	len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
	if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
		/* insert after */
		if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
			len = (len - 1) * sizeof(struct ext4_extent_idx);
			len = len < 0 ? 0 : len;
739
			ext_debug("insert new index %d after: %llu. "
740
741
742
743
744
745
746
747
748
749
					"move %d from 0x%p to 0x%p\n",
					logical, ptr, len,
					(curp->p_idx + 1), (curp->p_idx + 2));
			memmove(curp->p_idx + 2, curp->p_idx + 1, len);
		}
		ix = curp->p_idx + 1;
	} else {
		/* insert before */
		len = len * sizeof(struct ext4_extent_idx);
		len = len < 0 ? 0 : len;
750
		ext_debug("insert new index %d before: %llu. "
751
752
753
754
755
756
757
758
				"move %d from 0x%p to 0x%p\n",
				logical, ptr, len,
				curp->p_idx, (curp->p_idx + 1));
		memmove(curp->p_idx + 1, curp->p_idx, len);
		ix = curp->p_idx;
	}

	ix->ei_block = cpu_to_le32(logical);
759
	ext4_idx_store_pblock(ix, ptr);
Marcin Slusarz's avatar
Marcin Slusarz committed
760
	le16_add_cpu(&curp->p_hdr->eh_entries, 1);
761

762
763
764
765
766
767
768
769
770
771
772
	if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
			     > le16_to_cpu(curp->p_hdr->eh_max))) {
		EXT4_ERROR_INODE(inode,
				 "logical %d == ei_block %d!",
				 logical, le32_to_cpu(curp->p_idx->ei_block));
		return -EIO;
	}
	if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
		EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
		return -EIO;
	}
773
774
775
776
777
778
779
780

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

	return err;
}

/*
781
782
783
784
785
786
787
788
 * 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
789
790
791
792
793
794
795
796
797
798
799
 */
static int ext4_ext_split(handle_t *handle, struct inode *inode,
				struct ext4_ext_path *path,
				struct ext4_extent *newext, int at)
{
	struct buffer_head *bh = NULL;
	int depth = ext_depth(inode);
	struct ext4_extent_header *neh;
	struct ext4_extent_idx *fidx;
	struct ext4_extent *ex;
	int i = at, k, m, a;
800
	ext4_fsblk_t newblock, oldblock;
801
	__le32 border;
802
	ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
803
804
805
	int err = 0;

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

808
	/* if current leaf will be split, then we should use
809
	 * border from split point */
810
811
812
813
	if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
		EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
		return -EIO;
	}
814
815
	if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
		border = path[depth].p_ext[1].ee_block;
816
		ext_debug("leaf will be split."
817
				" next leaf starts at %d\n",
Dave Kleikamp's avatar
Dave Kleikamp committed
818
				  le32_to_cpu(border));
819
820
821
822
	} else {
		border = newext->ee_block;
		ext_debug("leaf will be added."
				" next leaf starts at %d\n",
Dave Kleikamp's avatar
Dave Kleikamp committed
823
				le32_to_cpu(border));
824
825
826
	}

	/*
827
828
	 * If error occurs, then we break processing
	 * and mark filesystem read-only. index won't
829
	 * be inserted and tree will be in consistent
830
	 * state. Next mount will repair buffers too.
831
832
833
	 */

	/*
834
835
836
	 * Get array to track all allocated blocks.
	 * We need this to handle errors and free blocks
	 * upon them.
837
	 */
838
	ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
839
840
841
842
843
844
	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++) {
845
846
		newblock = ext4_ext_new_meta_block(handle, inode, path,
						   newext, &err);
847
848
849
850
851
852
853
		if (newblock == 0)
			goto cleanup;
		ablocks[a] = newblock;
	}

	/* initialize new leaf */
	newblock = ablocks[--a];
854
855
856
857
858
	if (unlikely(newblock == 0)) {
		EXT4_ERROR_INODE(inode, "newblock == 0!");
		err = -EIO;
		goto cleanup;
	}
859
860
861
862
863
864
865
	bh = sb_getblk(inode->i_sb, newblock);
	if (!bh) {
		err = -EIO;
		goto cleanup;
	}
	lock_buffer(bh);

866
867
	err = ext4_journal_get_create_access(handle, bh);
	if (err)
868
869
870
871
		goto cleanup;

	neh = ext_block_hdr(bh);
	neh->eh_entries = 0;
872
	neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
873
874
875
876
	neh->eh_magic = EXT4_EXT_MAGIC;
	neh->eh_depth = 0;
	ex = EXT_FIRST_EXTENT(neh);

877
	/* move remainder of path[depth] to the new leaf */
878
879
880
881
882
883
884
885
	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;
	}
886
887
888
889
890
891
	/* start copy from next extent */
	/* TODO: we could do it by single memmove */
	m = 0;
	path[depth].p_ext++;
	while (path[depth].p_ext <=
			EXT_MAX_EXTENT(path[depth].p_hdr)) {
892
		ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
Dave Kleikamp's avatar
Dave Kleikamp committed
893
				le32_to_cpu(path[depth].p_ext->ee_block),
894
				ext4_ext_pblock(path[depth].p_ext),
895
				ext4_ext_is_uninitialized(path[depth].p_ext),
Amit Arora's avatar
Amit Arora committed
896
				ext4_ext_get_actual_len(path[depth].p_ext),
897
898
899
900
901
902
903
904
905
				newblock);
		/*memmove(ex++, path[depth].p_ext++,
				sizeof(struct ext4_extent));
		neh->eh_entries++;*/
		path[depth].p_ext++;
		m++;
	}
	if (m) {
		memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
Marcin Slusarz's avatar
Marcin Slusarz committed
906
		le16_add_cpu(&neh->eh_entries, m);
907
908
909
910
911
	}

	set_buffer_uptodate(bh);
	unlock_buffer(bh);

912
	err = ext4_handle_dirty_metadata(handle, inode, bh);
913
	if (err)
914
915
916
917
918
919
		goto cleanup;
	brelse(bh);
	bh = NULL;

	/* correct old leaf */
	if (m) {
920
921
		err = ext4_ext_get_access(handle, inode, path + depth);
		if (err)
922
			goto cleanup;
Marcin Slusarz's avatar
Marcin Slusarz committed
923
		le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
924
925
		err = ext4_ext_dirty(handle, inode, path + depth);
		if (err)
926
927
928
929
930
931
			goto cleanup;

	}

	/* create intermediate indexes */
	k = depth - at - 1;
932
933
934
935
936
	if (unlikely(k < 0)) {
		EXT4_ERROR_INODE(inode, "k %d < 0!", k);
		err = -EIO;
		goto cleanup;
	}
937
938
939
940
941
942
943
944
	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];
945
		bh = sb_getblk(inode->i_sb, newblock);
946
947
948
949
950
951
		if (!bh) {
			err = -EIO;
			goto cleanup;
		}
		lock_buffer(bh);

952
953
		err = ext4_journal_get_create_access(handle, bh);
		if (err)
954
955
956
957
958
			goto cleanup;

		neh = ext_block_hdr(bh);
		neh->eh_entries = cpu_to_le16(1);
		neh->eh_magic = EXT4_EXT_MAGIC;
959
		neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
960
961
962
		neh->eh_depth = cpu_to_le16(depth - i);
		fidx = EXT_FIRST_INDEX(neh);
		fidx->ei_block = border;
963
		ext4_idx_store_pblock(fidx, oldblock);
964

965
966
		ext_debug("int.index at %d (block %llu): %u -> %llu\n",
				i, newblock, le32_to_cpu(border), oldblock);
967
968
969
970
971
972
		/* copy indexes */
		m = 0;
		path[i].p_idx++;

		ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
				EXT_MAX_INDEX(path[i].p_hdr));
973
974
975
976
977
978
979
980
		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;
		}
981
		while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
982
			ext_debug("%d: move %d:%llu in new index %llu\n", i,
Dave Kleikamp's avatar
Dave Kleikamp committed
983
					le32_to_cpu(path[i].p_idx->ei_block),
984
					ext4_idx_pblock(path[i].p_idx),
Dave Kleikamp's avatar
Dave Kleikamp committed
985
					newblock);
986
987
988
989
990
991
992
993
994
995
			/*memmove(++fidx, path[i].p_idx++,
					sizeof(struct ext4_extent_idx));
			neh->eh_entries++;
			BUG_ON(neh->eh_entries > neh->eh_max);*/
			path[i].p_idx++;
			m++;
		}
		if (m) {
			memmove(++fidx, path[i].p_idx - m,
				sizeof(struct ext4_extent_idx) * m);
Marcin Slusarz's avatar
Marcin Slusarz committed
996
			le16_add_cpu(&neh->eh_entries, m);
997
998
999
1000
		}
		set_buffer_uptodate(bh);
		unlock_buffer(bh);

1001
		err = ext4_handle_dirty_metadata(handle, inode, bh);
1002
		if (err)
1003
1004
1005
1006
1007
1008
1009
1010
1011
			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
1012
			le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
			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;
1037
1038
			ext4_free_blocks(handle, inode, 0, ablocks[i], 1,
					 EXT4_FREE_BLOCKS_METADATA);
1039
1040
1041
1042
1043
1044
1045
1046
		}
	}
	kfree(ablocks);

	return err;
}

/*
1047
1048
1049
1050
1051
1052
 * 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
1053
1054
1055
1056
1057
1058
1059
1060
 */
static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
					struct ext4_ext_path *path,
					struct ext4_extent *newext)
{
	struct ext4_ext_path *curp = path;
	struct ext4_extent_header *neh;
	struct buffer_head *bh;
1061
	ext4_fsblk_t newblock;
1062
1063
	int err = 0;

1064
	newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
	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);

1076
1077
	err = ext4_journal_get_create_access(handle, bh);
	if (err) {
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
		unlock_buffer(bh);
		goto out;
	}

	/* move top-level index/leaf into new block */
	memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));

	/* 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))
1090
		neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
1091
	else
1092
		neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
1093
1094
1095
1096
	neh->eh_magic = EXT4_EXT_MAGIC;
	set_buffer_uptodate(bh);
	unlock_buffer(bh);

1097
	err = ext4_handle_dirty_metadata(handle, inode, bh);
1098
	if (err)
1099
1100
1101
		goto out;

	/* create index in new top-level index: num,max,pointer */
1102
1103
	err = ext4_ext_get_access(handle, inode, curp);
	if (err)
1104
1105
1106
		goto out;

	curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
1107
	curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1108
1109
	curp->p_hdr->eh_entries = cpu_to_le16(1);
	curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
Dmitry Monakhov's avatar
Dmitry Monakhov committed
1110
1111
1112
1113
1114
1115
1116

	if (path[0].p_hdr->eh_depth)
		curp->p_idx->ei_block =
			EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
	else
		curp->p_idx->ei_block =
			EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
1117
	ext4_idx_store_pblock(curp->p_idx, newblock);
1118
1119

	neh = ext_inode_hdr(inode);
1120
	ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
1121
		  le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
1122
		  le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
1123
		  ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133

	neh->eh_depth = cpu_to_le16(path->p_depth + 1);
	err = ext4_ext_dirty(handle, inode, curp);
out:
	brelse(bh);

	return err;
}

/*
1134
1135
1136
 * ext4_ext_create_new_leaf:
 * finds empty index and adds new leaf.
 * if no free index is found, then it requests in-depth growing.
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
 */
static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
					struct ext4_ext_path *path,
					struct ext4_extent *newext)
{
	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--;
	}

1155
1156
	/* we use already allocated block for index block,
	 * so subsequent data blocks should be contiguous */
1157
1158
1159
1160
	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 */
		err = ext4_ext_split(handle, inode, path, newext, i);
1161
1162
		if (err)
			goto out;
1163
1164
1165
1166

		/* refill path */
		ext4_ext_drop_refs(path);
		path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V's avatar
Aneesh Kumar K.V committed
1167
1168
				    (ext4_lblk_t)le32_to_cpu(newext->ee_block),
				    path);
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
		if (IS_ERR(path))
			err = PTR_ERR(path);
	} else {
		/* tree is full, time to grow in depth */
		err = ext4_ext_grow_indepth(handle, inode, path, newext);
		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
1180
1181
				   (ext4_lblk_t)le32_to_cpu(newext->ee_block),
				    path);
1182
1183
1184
1185
1186
1187
		if (IS_ERR(path)) {
			err = PTR_ERR(path);
			goto out;
		}

		/*
1188
1189
		 * only first (depth 0 -> 1) produces free space;
		 * in all other cases we have to split the grown tree
1190
1191
1192
		 */
		depth = ext_depth(inode);
		if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
1193
			/* now we need to split */
1194
1195
1196
1197
1198
1199
1200
1201
			goto repeat;
		}
	}

out:
	return err;
}

1202
1203
1204
1205
1206
1207
1208
/*
 * 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
 */
1209
1210
1211
static int ext4_ext_search_left(struct inode *inode,
				struct ext4_ext_path *path,
				ext4_lblk_t *logical, ext4_fsblk_t *phys)
1212
1213
1214
{
	struct ext4_extent_idx *ix;
	struct ext4_extent *ex;
1215
	int depth, ee_len;
1216

1217
1218
1219
1220
	if (unlikely(path == NULL)) {
		EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
		return -EIO;
	}
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
	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;
1232
	ee_len = ext4_ext_get_actual_len(ex);
1233
	if (*logical < le32_to_cpu(ex->ee_block)) {
1234
1235
1236
1237
1238
1239
		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;
		}
1240
1241
		while (--depth >= 0) {
			ix = path[depth].p_idx;
1242
1243
1244
1245
1246
1247
1248
1249
1250
			if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
				EXT4_ERROR_INODE(inode,
				  "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
				  ix != NULL ? ix->ei_block : 0,
				  EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
				    EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block : 0,
				  depth);
				return -EIO;
			}
1251
1252
1253
1254
		}
		return 0;
	}

1255
1256
1257
1258
1259
1260
	if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
		EXT4_ERROR_INODE(inode,
				 "logical %d < ee_block %d + ee_len %d!",
				 *logical, le32_to_cpu(ex->ee_block), ee_len);
		return -EIO;
	}
1261

1262
	*logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1263
	*phys = ext4_ext_pblock(ex) + ee_len - 1;
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
	return 0;
}

/*
 * search the closest allocated block to the right 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
 */
1274
1275
1276
static int ext4_ext_search_right(struct inode *inode,
				 struct ext4_ext_path *path,
				 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1277
1278
1279
1280
1281
1282
{
	struct buffer_head *bh = NULL;
	struct ext4_extent_header *eh;
	struct ext4_extent_idx *ix;
	struct ext4_extent *ex;
	ext4_fsblk_t block;
1283
1284
	int depth;	/* Note, NOT eh_depth; depth from top of tree */
	int ee_len;
1285

1286
1287
1288
1289
	if (unlikely(path == NULL)) {
		EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
		return -EIO;
	}
1290
1291