blob: 06ce8e87731ed745000400bd19acd7606ed7f30e [file] [log] [blame]
Alex Tomasa86c6182006-10-11 01:21:03 -07001/*
2 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
3 * Written by Alex Tomas <alex@clusterfs.com>
4 *
5 * Architecture independence:
6 * Copyright (c) 2005, Bull S.A.
7 * Written by Pierre Peiffer <pierre.peiffer@bull.net>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public Licens
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
21 */
22
23/*
24 * Extents support for EXT4
25 *
26 * TODO:
27 * - ext4*_error() should be used in some situations
28 * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
29 * - smart tree reduction
30 */
31
32#include <linux/module.h>
33#include <linux/fs.h>
34#include <linux/time.h>
35#include <linux/ext4_jbd2.h>
36#include <linux/jbd.h>
37#include <linux/smp_lock.h>
38#include <linux/highuid.h>
39#include <linux/pagemap.h>
40#include <linux/quotaops.h>
41#include <linux/string.h>
42#include <linux/slab.h>
43#include <linux/ext4_fs_extents.h>
44#include <asm/uaccess.h>
45
46
Randy Dunlapd0d856e2006-10-11 01:21:07 -070047/*
48 * ext_pblock:
49 * combine low and high parts of physical block number into ext4_fsblk_t
50 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -070051static inline ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
52{
53 ext4_fsblk_t block;
54
55 block = le32_to_cpu(ex->ee_start);
Mingming Cao9b8f1f02006-10-11 01:21:13 -070056 block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -070057 return block;
58}
59
Randy Dunlapd0d856e2006-10-11 01:21:07 -070060/*
61 * idx_pblock:
62 * combine low and high parts of a leaf physical block number into ext4_fsblk_t
63 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -070064static inline ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
65{
66 ext4_fsblk_t block;
67
68 block = le32_to_cpu(ix->ei_leaf);
Mingming Cao9b8f1f02006-10-11 01:21:13 -070069 block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -070070 return block;
71}
72
Randy Dunlapd0d856e2006-10-11 01:21:07 -070073/*
74 * ext4_ext_store_pblock:
75 * stores a large physical block number into an extent struct,
76 * breaking it into parts
77 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -070078static inline void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
79{
80 ex->ee_start = cpu_to_le32((unsigned long) (pb & 0xffffffff));
Mingming Cao9b8f1f02006-10-11 01:21:13 -070081 ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
Alex Tomasf65e6fb2006-10-11 01:21:05 -070082}
83
Randy Dunlapd0d856e2006-10-11 01:21:07 -070084/*
85 * ext4_idx_store_pblock:
86 * stores a large physical block number into an index struct,
87 * breaking it into parts
88 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -070089static inline void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb)
90{
91 ix->ei_leaf = cpu_to_le32((unsigned long) (pb & 0xffffffff));
Mingming Cao9b8f1f02006-10-11 01:21:13 -070092 ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
Alex Tomasf65e6fb2006-10-11 01:21:05 -070093}
94
Alex Tomasa86c6182006-10-11 01:21:03 -070095static int ext4_ext_check_header(const char *function, struct inode *inode,
96 struct ext4_extent_header *eh)
97{
98 const char *error_msg = NULL;
99
100 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
101 error_msg = "invalid magic";
102 goto corrupted;
103 }
104 if (unlikely(eh->eh_max == 0)) {
105 error_msg = "invalid eh_max";
106 goto corrupted;
107 }
108 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
109 error_msg = "invalid eh_entries";
110 goto corrupted;
111 }
112 return 0;
113
114corrupted:
115 ext4_error(inode->i_sb, function,
116 "bad header in inode #%lu: %s - magic %x, "
117 "entries %u, max %u, depth %u",
118 inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
119 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
120 le16_to_cpu(eh->eh_depth));
121
122 return -EIO;
123}
124
125static handle_t *ext4_ext_journal_restart(handle_t *handle, int needed)
126{
127 int err;
128
129 if (handle->h_buffer_credits > needed)
130 return handle;
131 if (!ext4_journal_extend(handle, needed))
132 return handle;
133 err = ext4_journal_restart(handle, needed);
134
135 return handle;
136}
137
138/*
139 * could return:
140 * - EROFS
141 * - ENOMEM
142 */
143static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
144 struct ext4_ext_path *path)
145{
146 if (path->p_bh) {
147 /* path points to block */
148 return ext4_journal_get_write_access(handle, path->p_bh);
149 }
150 /* path points to leaf/index in inode body */
151 /* we use in-core data, no need to protect them */
152 return 0;
153}
154
155/*
156 * could return:
157 * - EROFS
158 * - ENOMEM
159 * - EIO
160 */
161static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
162 struct ext4_ext_path *path)
163{
164 int err;
165 if (path->p_bh) {
166 /* path points to block */
167 err = ext4_journal_dirty_metadata(handle, path->p_bh);
168 } else {
169 /* path points to leaf/index in inode body */
170 err = ext4_mark_inode_dirty(handle, inode);
171 }
172 return err;
173}
174
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700175static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700176 struct ext4_ext_path *path,
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700177 ext4_fsblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700178{
179 struct ext4_inode_info *ei = EXT4_I(inode);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700180 ext4_fsblk_t bg_start;
181 ext4_grpblk_t colour;
Alex Tomasa86c6182006-10-11 01:21:03 -0700182 int depth;
183
184 if (path) {
185 struct ext4_extent *ex;
186 depth = path->p_depth;
187
188 /* try to predict block placement */
Avantika Mathur7e028972006-12-06 20:41:33 -0800189 ex = path[depth].p_ext;
190 if (ex)
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700191 return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700192
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700193 /* it looks like index is empty;
194 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700195 if (path[depth].p_bh)
196 return path[depth].p_bh->b_blocknr;
197 }
198
199 /* OK. use inode's group */
200 bg_start = (ei->i_block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) +
201 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block);
202 colour = (current->pid % 16) *
203 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
204 return bg_start + colour + block;
205}
206
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700207static ext4_fsblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -0700208ext4_ext_new_block(handle_t *handle, struct inode *inode,
209 struct ext4_ext_path *path,
210 struct ext4_extent *ex, int *err)
211{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700212 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700213
214 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
215 newblock = ext4_new_block(handle, inode, goal, err);
216 return newblock;
217}
218
219static inline int ext4_ext_space_block(struct inode *inode)
220{
221 int size;
222
223 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
224 / sizeof(struct ext4_extent);
225#ifdef AGRESSIVE_TEST
226 if (size > 6)
227 size = 6;
228#endif
229 return size;
230}
231
232static inline int ext4_ext_space_block_idx(struct inode *inode)
233{
234 int size;
235
236 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
237 / sizeof(struct ext4_extent_idx);
238#ifdef AGRESSIVE_TEST
239 if (size > 5)
240 size = 5;
241#endif
242 return size;
243}
244
245static inline int ext4_ext_space_root(struct inode *inode)
246{
247 int size;
248
249 size = sizeof(EXT4_I(inode)->i_data);
250 size -= sizeof(struct ext4_extent_header);
251 size /= sizeof(struct ext4_extent);
252#ifdef AGRESSIVE_TEST
253 if (size > 3)
254 size = 3;
255#endif
256 return size;
257}
258
259static inline int ext4_ext_space_root_idx(struct inode *inode)
260{
261 int size;
262
263 size = sizeof(EXT4_I(inode)->i_data);
264 size -= sizeof(struct ext4_extent_header);
265 size /= sizeof(struct ext4_extent_idx);
266#ifdef AGRESSIVE_TEST
267 if (size > 4)
268 size = 4;
269#endif
270 return size;
271}
272
273#ifdef EXT_DEBUG
274static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
275{
276 int k, l = path->p_depth;
277
278 ext_debug("path:");
279 for (k = 0; k <= l; k++, path++) {
280 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700281 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700282 idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700283 } else if (path->p_ext) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700284 ext_debug(" %d:%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700285 le32_to_cpu(path->p_ext->ee_block),
286 le16_to_cpu(path->p_ext->ee_len),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700287 ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700288 } else
289 ext_debug(" []");
290 }
291 ext_debug("\n");
292}
293
294static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
295{
296 int depth = ext_depth(inode);
297 struct ext4_extent_header *eh;
298 struct ext4_extent *ex;
299 int i;
300
301 if (!path)
302 return;
303
304 eh = path[depth].p_hdr;
305 ex = EXT_FIRST_EXTENT(eh);
306
307 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700308 ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700309 le16_to_cpu(ex->ee_len), ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700310 }
311 ext_debug("\n");
312}
313#else
314#define ext4_ext_show_path(inode,path)
315#define ext4_ext_show_leaf(inode,path)
316#endif
317
318static void ext4_ext_drop_refs(struct ext4_ext_path *path)
319{
320 int depth = path->p_depth;
321 int i;
322
323 for (i = 0; i <= depth; i++, path++)
324 if (path->p_bh) {
325 brelse(path->p_bh);
326 path->p_bh = NULL;
327 }
328}
329
330/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700331 * ext4_ext_binsearch_idx:
332 * binary search for the closest index of the given block
Alex Tomasa86c6182006-10-11 01:21:03 -0700333 */
334static void
335ext4_ext_binsearch_idx(struct inode *inode, struct ext4_ext_path *path, int block)
336{
337 struct ext4_extent_header *eh = path->p_hdr;
338 struct ext4_extent_idx *r, *l, *m;
339
340 BUG_ON(eh->eh_magic != EXT4_EXT_MAGIC);
341 BUG_ON(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max));
342 BUG_ON(le16_to_cpu(eh->eh_entries) <= 0);
343
344 ext_debug("binsearch for %d(idx): ", block);
345
346 l = EXT_FIRST_INDEX(eh) + 1;
347 r = EXT_FIRST_INDEX(eh) + le16_to_cpu(eh->eh_entries) - 1;
348 while (l <= r) {
349 m = l + (r - l) / 2;
350 if (block < le32_to_cpu(m->ei_block))
351 r = m - 1;
352 else
353 l = m + 1;
354 ext_debug("%p(%u):%p(%u):%p(%u) ", l, l->ei_block,
355 m, m->ei_block, r, r->ei_block);
356 }
357
358 path->p_idx = l - 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700359 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
360 idx_block(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700361
362#ifdef CHECK_BINSEARCH
363 {
364 struct ext4_extent_idx *chix, *ix;
365 int k;
366
367 chix = ix = EXT_FIRST_INDEX(eh);
368 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
369 if (k != 0 &&
370 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
371 printk("k=%d, ix=0x%p, first=0x%p\n", k,
372 ix, EXT_FIRST_INDEX(eh));
373 printk("%u <= %u\n",
374 le32_to_cpu(ix->ei_block),
375 le32_to_cpu(ix[-1].ei_block));
376 }
377 BUG_ON(k && le32_to_cpu(ix->ei_block)
378 <= le32_to_cpu(ix[-1].ei_block));
379 if (block < le32_to_cpu(ix->ei_block))
380 break;
381 chix = ix;
382 }
383 BUG_ON(chix != path->p_idx);
384 }
385#endif
386
387}
388
389/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700390 * ext4_ext_binsearch:
391 * binary search for closest extent of the given block
Alex Tomasa86c6182006-10-11 01:21:03 -0700392 */
393static void
394ext4_ext_binsearch(struct inode *inode, struct ext4_ext_path *path, int block)
395{
396 struct ext4_extent_header *eh = path->p_hdr;
397 struct ext4_extent *r, *l, *m;
398
399 BUG_ON(eh->eh_magic != EXT4_EXT_MAGIC);
400 BUG_ON(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max));
401
402 if (eh->eh_entries == 0) {
403 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700404 * this leaf is empty:
405 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700406 */
407 return;
408 }
409
410 ext_debug("binsearch for %d: ", block);
411
412 l = EXT_FIRST_EXTENT(eh) + 1;
413 r = EXT_FIRST_EXTENT(eh) + le16_to_cpu(eh->eh_entries) - 1;
414
415 while (l <= r) {
416 m = l + (r - l) / 2;
417 if (block < le32_to_cpu(m->ee_block))
418 r = m - 1;
419 else
420 l = m + 1;
421 ext_debug("%p(%u):%p(%u):%p(%u) ", l, l->ee_block,
422 m, m->ee_block, r, r->ee_block);
423 }
424
425 path->p_ext = l - 1;
Mingming Cao2ae02102006-10-11 01:21:11 -0700426 ext_debug(" -> %d:%llu:%d ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700427 le32_to_cpu(path->p_ext->ee_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700428 ext_pblock(path->p_ext),
429 le16_to_cpu(path->p_ext->ee_len));
Alex Tomasa86c6182006-10-11 01:21:03 -0700430
431#ifdef CHECK_BINSEARCH
432 {
433 struct ext4_extent *chex, *ex;
434 int k;
435
436 chex = ex = EXT_FIRST_EXTENT(eh);
437 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
438 BUG_ON(k && le32_to_cpu(ex->ee_block)
439 <= le32_to_cpu(ex[-1].ee_block));
440 if (block < le32_to_cpu(ex->ee_block))
441 break;
442 chex = ex;
443 }
444 BUG_ON(chex != path->p_ext);
445 }
446#endif
447
448}
449
450int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
451{
452 struct ext4_extent_header *eh;
453
454 eh = ext_inode_hdr(inode);
455 eh->eh_depth = 0;
456 eh->eh_entries = 0;
457 eh->eh_magic = EXT4_EXT_MAGIC;
458 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
459 ext4_mark_inode_dirty(handle, inode);
460 ext4_ext_invalidate_cache(inode);
461 return 0;
462}
463
464struct ext4_ext_path *
465ext4_ext_find_extent(struct inode *inode, int block, struct ext4_ext_path *path)
466{
467 struct ext4_extent_header *eh;
468 struct buffer_head *bh;
469 short int depth, i, ppos = 0, alloc = 0;
470
471 eh = ext_inode_hdr(inode);
472 BUG_ON(eh == NULL);
473 if (ext4_ext_check_header(__FUNCTION__, inode, eh))
474 return ERR_PTR(-EIO);
475
476 i = depth = ext_depth(inode);
477
478 /* account possible depth increase */
479 if (!path) {
480 path = kmalloc(sizeof(struct ext4_ext_path) * (depth + 2),
481 GFP_NOFS);
482 if (!path)
483 return ERR_PTR(-ENOMEM);
484 alloc = 1;
485 }
486 memset(path, 0, sizeof(struct ext4_ext_path) * (depth + 1));
487 path[0].p_hdr = eh;
488
489 /* walk through the tree */
490 while (i) {
491 ext_debug("depth %d: num %d, max %d\n",
492 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
493 ext4_ext_binsearch_idx(inode, path + ppos, block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700494 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700495 path[ppos].p_depth = i;
496 path[ppos].p_ext = NULL;
497
498 bh = sb_bread(inode->i_sb, path[ppos].p_block);
499 if (!bh)
500 goto err;
501
502 eh = ext_block_hdr(bh);
503 ppos++;
504 BUG_ON(ppos > depth);
505 path[ppos].p_bh = bh;
506 path[ppos].p_hdr = eh;
507 i--;
508
509 if (ext4_ext_check_header(__FUNCTION__, inode, eh))
510 goto err;
511 }
512
513 path[ppos].p_depth = i;
514 path[ppos].p_hdr = eh;
515 path[ppos].p_ext = NULL;
516 path[ppos].p_idx = NULL;
517
518 if (ext4_ext_check_header(__FUNCTION__, inode, eh))
519 goto err;
520
521 /* find extent */
522 ext4_ext_binsearch(inode, path + ppos, block);
523
524 ext4_ext_show_path(inode, path);
525
526 return path;
527
528err:
529 ext4_ext_drop_refs(path);
530 if (alloc)
531 kfree(path);
532 return ERR_PTR(-EIO);
533}
534
535/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700536 * ext4_ext_insert_index:
537 * insert new index [@logical;@ptr] into the block at @curp;
538 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700539 */
540static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
541 struct ext4_ext_path *curp,
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700542 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700543{
544 struct ext4_extent_idx *ix;
545 int len, err;
546
Avantika Mathur7e028972006-12-06 20:41:33 -0800547 err = ext4_ext_get_access(handle, inode, curp);
548 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700549 return err;
550
551 BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
552 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
553 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
554 /* insert after */
555 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
556 len = (len - 1) * sizeof(struct ext4_extent_idx);
557 len = len < 0 ? 0 : len;
558 ext_debug("insert new index %d after: %d. "
559 "move %d from 0x%p to 0x%p\n",
560 logical, ptr, len,
561 (curp->p_idx + 1), (curp->p_idx + 2));
562 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
563 }
564 ix = curp->p_idx + 1;
565 } else {
566 /* insert before */
567 len = len * sizeof(struct ext4_extent_idx);
568 len = len < 0 ? 0 : len;
569 ext_debug("insert new index %d before: %d. "
570 "move %d from 0x%p to 0x%p\n",
571 logical, ptr, len,
572 curp->p_idx, (curp->p_idx + 1));
573 memmove(curp->p_idx + 1, curp->p_idx, len);
574 ix = curp->p_idx;
575 }
576
577 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700578 ext4_idx_store_pblock(ix, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700579 curp->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(curp->p_hdr->eh_entries)+1);
580
581 BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
582 > le16_to_cpu(curp->p_hdr->eh_max));
583 BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
584
585 err = ext4_ext_dirty(handle, inode, curp);
586 ext4_std_error(inode->i_sb, err);
587
588 return err;
589}
590
591/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700592 * ext4_ext_split:
593 * inserts new subtree into the path, using free index entry
594 * at depth @at:
595 * - allocates all needed blocks (new leaf and all intermediate index blocks)
596 * - makes decision where to split
597 * - moves remaining extents and index entries (right to the split point)
598 * into the newly allocated blocks
599 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700600 */
601static int ext4_ext_split(handle_t *handle, struct inode *inode,
602 struct ext4_ext_path *path,
603 struct ext4_extent *newext, int at)
604{
605 struct buffer_head *bh = NULL;
606 int depth = ext_depth(inode);
607 struct ext4_extent_header *neh;
608 struct ext4_extent_idx *fidx;
609 struct ext4_extent *ex;
610 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700611 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700612 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700613 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700614 int err = 0;
615
616 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700617 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700618
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700619 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700620 * border from split point */
621 BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
622 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
623 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700624 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700625 " next leaf starts at %d\n",
626 le32_to_cpu(border));
627 } else {
628 border = newext->ee_block;
629 ext_debug("leaf will be added."
630 " next leaf starts at %d\n",
631 le32_to_cpu(border));
632 }
633
634 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700635 * If error occurs, then we break processing
636 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700637 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700638 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700639 */
640
641 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700642 * Get array to track all allocated blocks.
643 * We need this to handle errors and free blocks
644 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700645 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700646 ablocks = kmalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700647 if (!ablocks)
648 return -ENOMEM;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700649 memset(ablocks, 0, sizeof(ext4_fsblk_t) * depth);
Alex Tomasa86c6182006-10-11 01:21:03 -0700650
651 /* allocate all needed blocks */
652 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
653 for (a = 0; a < depth - at; a++) {
654 newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
655 if (newblock == 0)
656 goto cleanup;
657 ablocks[a] = newblock;
658 }
659
660 /* initialize new leaf */
661 newblock = ablocks[--a];
662 BUG_ON(newblock == 0);
663 bh = sb_getblk(inode->i_sb, newblock);
664 if (!bh) {
665 err = -EIO;
666 goto cleanup;
667 }
668 lock_buffer(bh);
669
Avantika Mathur7e028972006-12-06 20:41:33 -0800670 err = ext4_journal_get_create_access(handle, bh);
671 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700672 goto cleanup;
673
674 neh = ext_block_hdr(bh);
675 neh->eh_entries = 0;
676 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
677 neh->eh_magic = EXT4_EXT_MAGIC;
678 neh->eh_depth = 0;
679 ex = EXT_FIRST_EXTENT(neh);
680
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700681 /* move remainder of path[depth] to the new leaf */
Alex Tomasa86c6182006-10-11 01:21:03 -0700682 BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
683 /* start copy from next extent */
684 /* TODO: we could do it by single memmove */
685 m = 0;
686 path[depth].p_ext++;
687 while (path[depth].p_ext <=
688 EXT_MAX_EXTENT(path[depth].p_hdr)) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700689 ext_debug("move %d:%llu:%d in new leaf %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700690 le32_to_cpu(path[depth].p_ext->ee_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700691 ext_pblock(path[depth].p_ext),
Alex Tomasa86c6182006-10-11 01:21:03 -0700692 le16_to_cpu(path[depth].p_ext->ee_len),
693 newblock);
694 /*memmove(ex++, path[depth].p_ext++,
695 sizeof(struct ext4_extent));
696 neh->eh_entries++;*/
697 path[depth].p_ext++;
698 m++;
699 }
700 if (m) {
701 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
702 neh->eh_entries = cpu_to_le16(le16_to_cpu(neh->eh_entries)+m);
703 }
704
705 set_buffer_uptodate(bh);
706 unlock_buffer(bh);
707
Avantika Mathur7e028972006-12-06 20:41:33 -0800708 err = ext4_journal_dirty_metadata(handle, bh);
709 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700710 goto cleanup;
711 brelse(bh);
712 bh = NULL;
713
714 /* correct old leaf */
715 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800716 err = ext4_ext_get_access(handle, inode, path + depth);
717 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700718 goto cleanup;
719 path[depth].p_hdr->eh_entries =
720 cpu_to_le16(le16_to_cpu(path[depth].p_hdr->eh_entries)-m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800721 err = ext4_ext_dirty(handle, inode, path + depth);
722 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700723 goto cleanup;
724
725 }
726
727 /* create intermediate indexes */
728 k = depth - at - 1;
729 BUG_ON(k < 0);
730 if (k)
731 ext_debug("create %d intermediate indices\n", k);
732 /* insert new index into current index block */
733 /* current depth stored in i var */
734 i = depth - 1;
735 while (k--) {
736 oldblock = newblock;
737 newblock = ablocks[--a];
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700738 bh = sb_getblk(inode->i_sb, (ext4_fsblk_t)newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700739 if (!bh) {
740 err = -EIO;
741 goto cleanup;
742 }
743 lock_buffer(bh);
744
Avantika Mathur7e028972006-12-06 20:41:33 -0800745 err = ext4_journal_get_create_access(handle, bh);
746 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700747 goto cleanup;
748
749 neh = ext_block_hdr(bh);
750 neh->eh_entries = cpu_to_le16(1);
751 neh->eh_magic = EXT4_EXT_MAGIC;
752 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
753 neh->eh_depth = cpu_to_le16(depth - i);
754 fidx = EXT_FIRST_INDEX(neh);
755 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700756 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700757
Mingming Cao2ae02102006-10-11 01:21:11 -0700758 ext_debug("int.index at %d (block %llu): %lu -> %llu\n", i,
Alex Tomasa86c6182006-10-11 01:21:03 -0700759 newblock, (unsigned long) le32_to_cpu(border),
760 oldblock);
761 /* copy indexes */
762 m = 0;
763 path[i].p_idx++;
764
765 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
766 EXT_MAX_INDEX(path[i].p_hdr));
767 BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
768 EXT_LAST_INDEX(path[i].p_hdr));
769 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700770 ext_debug("%d: move %d:%d in new index %llu\n", i,
Alex Tomasa86c6182006-10-11 01:21:03 -0700771 le32_to_cpu(path[i].p_idx->ei_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700772 idx_pblock(path[i].p_idx),
Alex Tomasa86c6182006-10-11 01:21:03 -0700773 newblock);
774 /*memmove(++fidx, path[i].p_idx++,
775 sizeof(struct ext4_extent_idx));
776 neh->eh_entries++;
777 BUG_ON(neh->eh_entries > neh->eh_max);*/
778 path[i].p_idx++;
779 m++;
780 }
781 if (m) {
782 memmove(++fidx, path[i].p_idx - m,
783 sizeof(struct ext4_extent_idx) * m);
784 neh->eh_entries =
785 cpu_to_le16(le16_to_cpu(neh->eh_entries) + m);
786 }
787 set_buffer_uptodate(bh);
788 unlock_buffer(bh);
789
Avantika Mathur7e028972006-12-06 20:41:33 -0800790 err = ext4_journal_dirty_metadata(handle, bh);
791 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700792 goto cleanup;
793 brelse(bh);
794 bh = NULL;
795
796 /* correct old index */
797 if (m) {
798 err = ext4_ext_get_access(handle, inode, path + i);
799 if (err)
800 goto cleanup;
801 path[i].p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path[i].p_hdr->eh_entries)-m);
802 err = ext4_ext_dirty(handle, inode, path + i);
803 if (err)
804 goto cleanup;
805 }
806
807 i--;
808 }
809
810 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -0700811 err = ext4_ext_insert_index(handle, inode, path + at,
812 le32_to_cpu(border), newblock);
813
814cleanup:
815 if (bh) {
816 if (buffer_locked(bh))
817 unlock_buffer(bh);
818 brelse(bh);
819 }
820
821 if (err) {
822 /* free all allocated blocks in error case */
823 for (i = 0; i < depth; i++) {
824 if (!ablocks[i])
825 continue;
826 ext4_free_blocks(handle, inode, ablocks[i], 1);
827 }
828 }
829 kfree(ablocks);
830
831 return err;
832}
833
834/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700835 * ext4_ext_grow_indepth:
836 * implements tree growing procedure:
837 * - allocates new block
838 * - moves top-level data (index block or leaf) into the new block
839 * - initializes new top-level, creating index that points to the
840 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -0700841 */
842static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
843 struct ext4_ext_path *path,
844 struct ext4_extent *newext)
845{
846 struct ext4_ext_path *curp = path;
847 struct ext4_extent_header *neh;
848 struct ext4_extent_idx *fidx;
849 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700850 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700851 int err = 0;
852
853 newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
854 if (newblock == 0)
855 return err;
856
857 bh = sb_getblk(inode->i_sb, newblock);
858 if (!bh) {
859 err = -EIO;
860 ext4_std_error(inode->i_sb, err);
861 return err;
862 }
863 lock_buffer(bh);
864
Avantika Mathur7e028972006-12-06 20:41:33 -0800865 err = ext4_journal_get_create_access(handle, bh);
866 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -0700867 unlock_buffer(bh);
868 goto out;
869 }
870
871 /* move top-level index/leaf into new block */
872 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
873
874 /* set size of new block */
875 neh = ext_block_hdr(bh);
876 /* old root could have indexes or leaves
877 * so calculate e_max right way */
878 if (ext_depth(inode))
879 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
880 else
881 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
882 neh->eh_magic = EXT4_EXT_MAGIC;
883 set_buffer_uptodate(bh);
884 unlock_buffer(bh);
885
Avantika Mathur7e028972006-12-06 20:41:33 -0800886 err = ext4_journal_dirty_metadata(handle, bh);
887 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700888 goto out;
889
890 /* create index in new top-level index: num,max,pointer */
Avantika Mathur7e028972006-12-06 20:41:33 -0800891 err = ext4_ext_get_access(handle, inode, curp);
892 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700893 goto out;
894
895 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
896 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
897 curp->p_hdr->eh_entries = cpu_to_le16(1);
898 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
899 /* FIXME: it works, but actually path[0] can be index */
900 curp->p_idx->ei_block = EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700901 ext4_idx_store_pblock(curp->p_idx, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700902
903 neh = ext_inode_hdr(inode);
904 fidx = EXT_FIRST_INDEX(neh);
Mingming Cao2ae02102006-10-11 01:21:11 -0700905 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700906 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700907 le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700908
909 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
910 err = ext4_ext_dirty(handle, inode, curp);
911out:
912 brelse(bh);
913
914 return err;
915}
916
917/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700918 * ext4_ext_create_new_leaf:
919 * finds empty index and adds new leaf.
920 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -0700921 */
922static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
923 struct ext4_ext_path *path,
924 struct ext4_extent *newext)
925{
926 struct ext4_ext_path *curp;
927 int depth, i, err = 0;
928
929repeat:
930 i = depth = ext_depth(inode);
931
932 /* walk up to the tree and look for free index entry */
933 curp = path + depth;
934 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
935 i--;
936 curp--;
937 }
938
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700939 /* we use already allocated block for index block,
940 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -0700941 if (EXT_HAS_FREE_INDEX(curp)) {
942 /* if we found index with free entry, then use that
943 * entry: create all needed subtree and add new leaf */
944 err = ext4_ext_split(handle, inode, path, newext, i);
945
946 /* refill path */
947 ext4_ext_drop_refs(path);
948 path = ext4_ext_find_extent(inode,
949 le32_to_cpu(newext->ee_block),
950 path);
951 if (IS_ERR(path))
952 err = PTR_ERR(path);
953 } else {
954 /* tree is full, time to grow in depth */
955 err = ext4_ext_grow_indepth(handle, inode, path, newext);
956 if (err)
957 goto out;
958
959 /* refill path */
960 ext4_ext_drop_refs(path);
961 path = ext4_ext_find_extent(inode,
962 le32_to_cpu(newext->ee_block),
963 path);
964 if (IS_ERR(path)) {
965 err = PTR_ERR(path);
966 goto out;
967 }
968
969 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700970 * only first (depth 0 -> 1) produces free space;
971 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -0700972 */
973 depth = ext_depth(inode);
974 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700975 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -0700976 goto repeat;
977 }
978 }
979
980out:
981 return err;
982}
983
984/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700985 * ext4_ext_next_allocated_block:
986 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
987 * NOTE: it considers block number from index entry as
988 * allocated block. Thus, index entries have to be consistent
989 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -0700990 */
991static unsigned long
992ext4_ext_next_allocated_block(struct ext4_ext_path *path)
993{
994 int depth;
995
996 BUG_ON(path == NULL);
997 depth = path->p_depth;
998
999 if (depth == 0 && path->p_ext == NULL)
1000 return EXT_MAX_BLOCK;
1001
1002 while (depth >= 0) {
1003 if (depth == path->p_depth) {
1004 /* leaf */
1005 if (path[depth].p_ext !=
1006 EXT_LAST_EXTENT(path[depth].p_hdr))
1007 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1008 } else {
1009 /* index */
1010 if (path[depth].p_idx !=
1011 EXT_LAST_INDEX(path[depth].p_hdr))
1012 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1013 }
1014 depth--;
1015 }
1016
1017 return EXT_MAX_BLOCK;
1018}
1019
1020/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001021 * ext4_ext_next_leaf_block:
Alex Tomasa86c6182006-10-11 01:21:03 -07001022 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1023 */
1024static unsigned ext4_ext_next_leaf_block(struct inode *inode,
Andrew Morton63f57932006-10-11 01:21:24 -07001025 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001026{
1027 int depth;
1028
1029 BUG_ON(path == NULL);
1030 depth = path->p_depth;
1031
1032 /* zero-tree has no leaf blocks at all */
1033 if (depth == 0)
1034 return EXT_MAX_BLOCK;
1035
1036 /* go to index block */
1037 depth--;
1038
1039 while (depth >= 0) {
1040 if (path[depth].p_idx !=
1041 EXT_LAST_INDEX(path[depth].p_hdr))
1042 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1043 depth--;
1044 }
1045
1046 return EXT_MAX_BLOCK;
1047}
1048
1049/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001050 * ext4_ext_correct_indexes:
1051 * if leaf gets modified and modified extent is first in the leaf,
1052 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001053 * TODO: do we need to correct tree in all cases?
1054 */
1055int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1056 struct ext4_ext_path *path)
1057{
1058 struct ext4_extent_header *eh;
1059 int depth = ext_depth(inode);
1060 struct ext4_extent *ex;
1061 __le32 border;
1062 int k, err = 0;
1063
1064 eh = path[depth].p_hdr;
1065 ex = path[depth].p_ext;
1066 BUG_ON(ex == NULL);
1067 BUG_ON(eh == NULL);
1068
1069 if (depth == 0) {
1070 /* there is no tree at all */
1071 return 0;
1072 }
1073
1074 if (ex != EXT_FIRST_EXTENT(eh)) {
1075 /* we correct tree if first leaf got modified only */
1076 return 0;
1077 }
1078
1079 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001080 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001081 */
1082 k = depth - 1;
1083 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001084 err = ext4_ext_get_access(handle, inode, path + k);
1085 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001086 return err;
1087 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001088 err = ext4_ext_dirty(handle, inode, path + k);
1089 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001090 return err;
1091
1092 while (k--) {
1093 /* change all left-side indexes */
1094 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1095 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001096 err = ext4_ext_get_access(handle, inode, path + k);
1097 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001098 break;
1099 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001100 err = ext4_ext_dirty(handle, inode, path + k);
1101 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001102 break;
1103 }
1104
1105 return err;
1106}
1107
1108static int inline
1109ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1110 struct ext4_extent *ex2)
1111{
Andrew Morton63f57932006-10-11 01:21:24 -07001112 if (le32_to_cpu(ex1->ee_block) + le16_to_cpu(ex1->ee_len) !=
1113 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001114 return 0;
1115
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001116 /*
1117 * To allow future support for preallocated extents to be added
1118 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001119 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001120 */
1121 if (le16_to_cpu(ex1->ee_len) + le16_to_cpu(ex2->ee_len) > EXT_MAX_LEN)
1122 return 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001123#ifdef AGRESSIVE_TEST
1124 if (le16_to_cpu(ex1->ee_len) >= 4)
1125 return 0;
1126#endif
1127
Andrew Morton63f57932006-10-11 01:21:24 -07001128 if (ext_pblock(ex1) + le16_to_cpu(ex1->ee_len) == ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001129 return 1;
1130 return 0;
1131}
1132
1133/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001134 * ext4_ext_insert_extent:
1135 * tries to merge requsted extent into the existing extent or
1136 * inserts requested extent as new one into the tree,
1137 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001138 */
1139int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1140 struct ext4_ext_path *path,
1141 struct ext4_extent *newext)
1142{
1143 struct ext4_extent_header * eh;
1144 struct ext4_extent *ex, *fex;
1145 struct ext4_extent *nearex; /* nearest extent */
1146 struct ext4_ext_path *npath = NULL;
1147 int depth, len, err, next;
1148
1149 BUG_ON(newext->ee_len == 0);
1150 depth = ext_depth(inode);
1151 ex = path[depth].p_ext;
1152 BUG_ON(path[depth].p_hdr == NULL);
1153
1154 /* try to insert block into found extent and return */
1155 if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
Mingming Cao2ae02102006-10-11 01:21:11 -07001156 ext_debug("append %d block to %d:%d (from %llu)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001157 le16_to_cpu(newext->ee_len),
1158 le32_to_cpu(ex->ee_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001159 le16_to_cpu(ex->ee_len), ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001160 err = ext4_ext_get_access(handle, inode, path + depth);
1161 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001162 return err;
1163 ex->ee_len = cpu_to_le16(le16_to_cpu(ex->ee_len)
1164 + le16_to_cpu(newext->ee_len));
1165 eh = path[depth].p_hdr;
1166 nearex = ex;
1167 goto merge;
1168 }
1169
1170repeat:
1171 depth = ext_depth(inode);
1172 eh = path[depth].p_hdr;
1173 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1174 goto has_space;
1175
1176 /* probably next leaf has space for us? */
1177 fex = EXT_LAST_EXTENT(eh);
1178 next = ext4_ext_next_leaf_block(inode, path);
1179 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1180 && next != EXT_MAX_BLOCK) {
1181 ext_debug("next leaf block - %d\n", next);
1182 BUG_ON(npath != NULL);
1183 npath = ext4_ext_find_extent(inode, next, NULL);
1184 if (IS_ERR(npath))
1185 return PTR_ERR(npath);
1186 BUG_ON(npath->p_depth != path->p_depth);
1187 eh = npath[depth].p_hdr;
1188 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1189 ext_debug("next leaf isnt full(%d)\n",
1190 le16_to_cpu(eh->eh_entries));
1191 path = npath;
1192 goto repeat;
1193 }
1194 ext_debug("next leaf has no free space(%d,%d)\n",
1195 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1196 }
1197
1198 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001199 * There is no free space in the found leaf.
1200 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001201 */
1202 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1203 if (err)
1204 goto cleanup;
1205 depth = ext_depth(inode);
1206 eh = path[depth].p_hdr;
1207
1208has_space:
1209 nearex = path[depth].p_ext;
1210
Avantika Mathur7e028972006-12-06 20:41:33 -08001211 err = ext4_ext_get_access(handle, inode, path + depth);
1212 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001213 goto cleanup;
1214
1215 if (!nearex) {
1216 /* there is no extent in this leaf, create first one */
Mingming Cao2ae02102006-10-11 01:21:11 -07001217 ext_debug("first extent in the leaf: %d:%llu:%d\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001218 le32_to_cpu(newext->ee_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001219 ext_pblock(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001220 le16_to_cpu(newext->ee_len));
1221 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1222 } else if (le32_to_cpu(newext->ee_block)
1223 > le32_to_cpu(nearex->ee_block)) {
1224/* BUG_ON(newext->ee_block == nearex->ee_block); */
1225 if (nearex != EXT_LAST_EXTENT(eh)) {
1226 len = EXT_MAX_EXTENT(eh) - nearex;
1227 len = (len - 1) * sizeof(struct ext4_extent);
1228 len = len < 0 ? 0 : len;
Mingming Cao2ae02102006-10-11 01:21:11 -07001229 ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001230 "move %d from 0x%p to 0x%p\n",
1231 le32_to_cpu(newext->ee_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001232 ext_pblock(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001233 le16_to_cpu(newext->ee_len),
1234 nearex, len, nearex + 1, nearex + 2);
1235 memmove(nearex + 2, nearex + 1, len);
1236 }
1237 path[depth].p_ext = nearex + 1;
1238 } else {
1239 BUG_ON(newext->ee_block == nearex->ee_block);
1240 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1241 len = len < 0 ? 0 : len;
Mingming Cao2ae02102006-10-11 01:21:11 -07001242 ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001243 "move %d from 0x%p to 0x%p\n",
1244 le32_to_cpu(newext->ee_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001245 ext_pblock(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001246 le16_to_cpu(newext->ee_len),
1247 nearex, len, nearex + 1, nearex + 2);
1248 memmove(nearex + 1, nearex, len);
1249 path[depth].p_ext = nearex;
1250 }
1251
1252 eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)+1);
1253 nearex = path[depth].p_ext;
1254 nearex->ee_block = newext->ee_block;
1255 nearex->ee_start = newext->ee_start;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001256 nearex->ee_start_hi = newext->ee_start_hi;
Alex Tomasa86c6182006-10-11 01:21:03 -07001257 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001258
1259merge:
1260 /* try to merge extents to the right */
1261 while (nearex < EXT_LAST_EXTENT(eh)) {
1262 if (!ext4_can_extents_be_merged(inode, nearex, nearex + 1))
1263 break;
1264 /* merge with next extent! */
1265 nearex->ee_len = cpu_to_le16(le16_to_cpu(nearex->ee_len)
1266 + le16_to_cpu(nearex[1].ee_len));
1267 if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
1268 len = (EXT_LAST_EXTENT(eh) - nearex - 1)
1269 * sizeof(struct ext4_extent);
1270 memmove(nearex + 1, nearex + 2, len);
1271 }
1272 eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
1273 BUG_ON(eh->eh_entries == 0);
1274 }
1275
1276 /* try to merge extents to the left */
1277
1278 /* time to correct all indexes above */
1279 err = ext4_ext_correct_indexes(handle, inode, path);
1280 if (err)
1281 goto cleanup;
1282
1283 err = ext4_ext_dirty(handle, inode, path + depth);
1284
1285cleanup:
1286 if (npath) {
1287 ext4_ext_drop_refs(npath);
1288 kfree(npath);
1289 }
1290 ext4_ext_tree_changed(inode);
1291 ext4_ext_invalidate_cache(inode);
1292 return err;
1293}
1294
1295int ext4_ext_walk_space(struct inode *inode, unsigned long block,
1296 unsigned long num, ext_prepare_callback func,
1297 void *cbdata)
1298{
1299 struct ext4_ext_path *path = NULL;
1300 struct ext4_ext_cache cbex;
1301 struct ext4_extent *ex;
1302 unsigned long next, start = 0, end = 0;
1303 unsigned long last = block + num;
1304 int depth, exists, err = 0;
1305
1306 BUG_ON(func == NULL);
1307 BUG_ON(inode == NULL);
1308
1309 while (block < last && block != EXT_MAX_BLOCK) {
1310 num = last - block;
1311 /* find extent for this block */
1312 path = ext4_ext_find_extent(inode, block, path);
1313 if (IS_ERR(path)) {
1314 err = PTR_ERR(path);
1315 path = NULL;
1316 break;
1317 }
1318
1319 depth = ext_depth(inode);
1320 BUG_ON(path[depth].p_hdr == NULL);
1321 ex = path[depth].p_ext;
1322 next = ext4_ext_next_allocated_block(path);
1323
1324 exists = 0;
1325 if (!ex) {
1326 /* there is no extent yet, so try to allocate
1327 * all requested space */
1328 start = block;
1329 end = block + num;
1330 } else if (le32_to_cpu(ex->ee_block) > block) {
1331 /* need to allocate space before found extent */
1332 start = block;
1333 end = le32_to_cpu(ex->ee_block);
1334 if (block + num < end)
1335 end = block + num;
1336 } else if (block >=
1337 le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len)) {
1338 /* need to allocate space after found extent */
1339 start = block;
1340 end = block + num;
1341 if (end >= next)
1342 end = next;
1343 } else if (block >= le32_to_cpu(ex->ee_block)) {
1344 /*
1345 * some part of requested space is covered
1346 * by found extent
1347 */
1348 start = block;
1349 end = le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len);
1350 if (block + num < end)
1351 end = block + num;
1352 exists = 1;
1353 } else {
1354 BUG();
1355 }
1356 BUG_ON(end <= start);
1357
1358 if (!exists) {
1359 cbex.ec_block = start;
1360 cbex.ec_len = end - start;
1361 cbex.ec_start = 0;
1362 cbex.ec_type = EXT4_EXT_CACHE_GAP;
1363 } else {
1364 cbex.ec_block = le32_to_cpu(ex->ee_block);
1365 cbex.ec_len = le16_to_cpu(ex->ee_len);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001366 cbex.ec_start = ext_pblock(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001367 cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
1368 }
1369
1370 BUG_ON(cbex.ec_len == 0);
1371 err = func(inode, path, &cbex, cbdata);
1372 ext4_ext_drop_refs(path);
1373
1374 if (err < 0)
1375 break;
1376 if (err == EXT_REPEAT)
1377 continue;
1378 else if (err == EXT_BREAK) {
1379 err = 0;
1380 break;
1381 }
1382
1383 if (ext_depth(inode) != depth) {
1384 /* depth was changed. we have to realloc path */
1385 kfree(path);
1386 path = NULL;
1387 }
1388
1389 block = cbex.ec_block + cbex.ec_len;
1390 }
1391
1392 if (path) {
1393 ext4_ext_drop_refs(path);
1394 kfree(path);
1395 }
1396
1397 return err;
1398}
1399
1400static inline void
1401ext4_ext_put_in_cache(struct inode *inode, __u32 block,
1402 __u32 len, __u32 start, int type)
1403{
1404 struct ext4_ext_cache *cex;
1405 BUG_ON(len == 0);
1406 cex = &EXT4_I(inode)->i_cached_extent;
1407 cex->ec_type = type;
1408 cex->ec_block = block;
1409 cex->ec_len = len;
1410 cex->ec_start = start;
1411}
1412
1413/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001414 * ext4_ext_put_gap_in_cache:
1415 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07001416 * and cache this gap
1417 */
1418static inline void
1419ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1420 unsigned long block)
1421{
1422 int depth = ext_depth(inode);
1423 unsigned long lblock, len;
1424 struct ext4_extent *ex;
1425
1426 ex = path[depth].p_ext;
1427 if (ex == NULL) {
1428 /* there is no extent yet, so gap is [0;-] */
1429 lblock = 0;
1430 len = EXT_MAX_BLOCK;
1431 ext_debug("cache gap(whole file):");
1432 } else if (block < le32_to_cpu(ex->ee_block)) {
1433 lblock = block;
1434 len = le32_to_cpu(ex->ee_block) - block;
1435 ext_debug("cache gap(before): %lu [%lu:%lu]",
1436 (unsigned long) block,
1437 (unsigned long) le32_to_cpu(ex->ee_block),
1438 (unsigned long) le16_to_cpu(ex->ee_len));
1439 } else if (block >= le32_to_cpu(ex->ee_block)
1440 + le16_to_cpu(ex->ee_len)) {
1441 lblock = le32_to_cpu(ex->ee_block)
1442 + le16_to_cpu(ex->ee_len);
1443 len = ext4_ext_next_allocated_block(path);
1444 ext_debug("cache gap(after): [%lu:%lu] %lu",
1445 (unsigned long) le32_to_cpu(ex->ee_block),
1446 (unsigned long) le16_to_cpu(ex->ee_len),
1447 (unsigned long) block);
1448 BUG_ON(len == lblock);
1449 len = len - lblock;
1450 } else {
1451 lblock = len = 0;
1452 BUG();
1453 }
1454
1455 ext_debug(" -> %lu:%lu\n", (unsigned long) lblock, len);
1456 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1457}
1458
1459static inline int
1460ext4_ext_in_cache(struct inode *inode, unsigned long block,
1461 struct ext4_extent *ex)
1462{
1463 struct ext4_ext_cache *cex;
1464
1465 cex = &EXT4_I(inode)->i_cached_extent;
1466
1467 /* has cache valid data? */
1468 if (cex->ec_type == EXT4_EXT_CACHE_NO)
1469 return EXT4_EXT_CACHE_NO;
1470
1471 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1472 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1473 if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
1474 ex->ee_block = cpu_to_le32(cex->ec_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001475 ext4_ext_store_pblock(ex, cex->ec_start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001476 ex->ee_len = cpu_to_le16(cex->ec_len);
Mingming Cao2ae02102006-10-11 01:21:11 -07001477 ext_debug("%lu cached by %lu:%lu:%llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001478 (unsigned long) block,
1479 (unsigned long) cex->ec_block,
1480 (unsigned long) cex->ec_len,
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001481 cex->ec_start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001482 return cex->ec_type;
1483 }
1484
1485 /* not in cache */
1486 return EXT4_EXT_CACHE_NO;
1487}
1488
1489/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001490 * ext4_ext_rm_idx:
1491 * removes index from the index block.
1492 * It's used in truncate case only, thus all requests are for
1493 * last index in the block only.
Alex Tomasa86c6182006-10-11 01:21:03 -07001494 */
1495int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
1496 struct ext4_ext_path *path)
1497{
1498 struct buffer_head *bh;
1499 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001500 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07001501
1502 /* free index block */
1503 path--;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001504 leaf = idx_pblock(path->p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -07001505 BUG_ON(path->p_hdr->eh_entries == 0);
Avantika Mathur7e028972006-12-06 20:41:33 -08001506 err = ext4_ext_get_access(handle, inode, path);
1507 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001508 return err;
1509 path->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path->p_hdr->eh_entries)-1);
Avantika Mathur7e028972006-12-06 20:41:33 -08001510 err = ext4_ext_dirty(handle, inode, path);
1511 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001512 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07001513 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Alex Tomasa86c6182006-10-11 01:21:03 -07001514 bh = sb_find_get_block(inode->i_sb, leaf);
1515 ext4_forget(handle, 1, inode, bh, leaf);
1516 ext4_free_blocks(handle, inode, leaf, 1);
1517 return err;
1518}
1519
1520/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001521 * ext4_ext_calc_credits_for_insert:
1522 * This routine returns max. credits that the extent tree can consume.
Alex Tomasa86c6182006-10-11 01:21:03 -07001523 * It should be OK for low-performance paths like ->writepage()
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001524 * To allow many writing processes to fit into a single transaction,
1525 * the caller should calculate credits under truncate_mutex and
1526 * pass the actual path.
Alex Tomasa86c6182006-10-11 01:21:03 -07001527 */
1528int inline ext4_ext_calc_credits_for_insert(struct inode *inode,
1529 struct ext4_ext_path *path)
1530{
1531 int depth, needed;
1532
1533 if (path) {
1534 /* probably there is space in leaf? */
1535 depth = ext_depth(inode);
1536 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
1537 < le16_to_cpu(path[depth].p_hdr->eh_max))
1538 return 1;
1539 }
1540
1541 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001542 * given 32-bit logical block (4294967296 blocks), max. tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001543 * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001544 * Let's also add one more level for imbalance.
Alex Tomasa86c6182006-10-11 01:21:03 -07001545 */
1546 depth = 5;
1547
1548 /* allocation of new data block(s) */
1549 needed = 2;
1550
1551 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001552 * tree can be full, so it would need to grow in depth:
Johann Lombardifeb18922006-12-06 20:40:46 -08001553 * we need one credit to modify old root, credits for
1554 * new root will be added in split accounting
Alex Tomasa86c6182006-10-11 01:21:03 -07001555 */
Johann Lombardifeb18922006-12-06 20:40:46 -08001556 needed += 1;
Alex Tomasa86c6182006-10-11 01:21:03 -07001557
1558 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001559 * Index split can happen, we would need:
Alex Tomasa86c6182006-10-11 01:21:03 -07001560 * allocate intermediate indexes (bitmap + group)
1561 * + change two blocks at each level, but root (already included)
1562 */
Johann Lombardifeb18922006-12-06 20:40:46 -08001563 needed += (depth * 2) + (depth * 2);
Alex Tomasa86c6182006-10-11 01:21:03 -07001564
1565 /* any allocation modifies superblock */
1566 needed += 1;
1567
1568 return needed;
1569}
1570
1571static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
1572 struct ext4_extent *ex,
1573 unsigned long from, unsigned long to)
1574{
1575 struct buffer_head *bh;
1576 int i;
1577
1578#ifdef EXTENTS_STATS
1579 {
1580 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1581 unsigned short ee_len = le16_to_cpu(ex->ee_len);
1582 spin_lock(&sbi->s_ext_stats_lock);
1583 sbi->s_ext_blocks += ee_len;
1584 sbi->s_ext_extents++;
1585 if (ee_len < sbi->s_ext_min)
1586 sbi->s_ext_min = ee_len;
1587 if (ee_len > sbi->s_ext_max)
1588 sbi->s_ext_max = ee_len;
1589 if (ext_depth(inode) > sbi->s_depth_max)
1590 sbi->s_depth_max = ext_depth(inode);
1591 spin_unlock(&sbi->s_ext_stats_lock);
1592 }
1593#endif
1594 if (from >= le32_to_cpu(ex->ee_block)
1595 && to == le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - 1) {
1596 /* tail removal */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001597 unsigned long num;
1598 ext4_fsblk_t start;
Alex Tomasa86c6182006-10-11 01:21:03 -07001599 num = le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - from;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001600 start = ext_pblock(ex) + le16_to_cpu(ex->ee_len) - num;
Mingming Cao2ae02102006-10-11 01:21:11 -07001601 ext_debug("free last %lu blocks starting %llu\n", num, start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001602 for (i = 0; i < num; i++) {
1603 bh = sb_find_get_block(inode->i_sb, start + i);
1604 ext4_forget(handle, 0, inode, bh, start + i);
1605 }
1606 ext4_free_blocks(handle, inode, start, num);
1607 } else if (from == le32_to_cpu(ex->ee_block)
1608 && to <= le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - 1) {
1609 printk("strange request: removal %lu-%lu from %u:%u\n",
1610 from, to, le32_to_cpu(ex->ee_block), le16_to_cpu(ex->ee_len));
1611 } else {
1612 printk("strange request: removal(2) %lu-%lu from %u:%u\n",
1613 from, to, le32_to_cpu(ex->ee_block), le16_to_cpu(ex->ee_len));
1614 }
1615 return 0;
1616}
1617
1618static int
1619ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
1620 struct ext4_ext_path *path, unsigned long start)
1621{
1622 int err = 0, correct_index = 0;
1623 int depth = ext_depth(inode), credits;
1624 struct ext4_extent_header *eh;
1625 unsigned a, b, block, num;
1626 unsigned long ex_ee_block;
1627 unsigned short ex_ee_len;
1628 struct ext4_extent *ex;
1629
1630 ext_debug("truncate since %lu in leaf\n", start);
1631 if (!path[depth].p_hdr)
1632 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
1633 eh = path[depth].p_hdr;
1634 BUG_ON(eh == NULL);
1635 BUG_ON(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max));
1636 BUG_ON(eh->eh_magic != EXT4_EXT_MAGIC);
1637
1638 /* find where to start removing */
1639 ex = EXT_LAST_EXTENT(eh);
1640
1641 ex_ee_block = le32_to_cpu(ex->ee_block);
1642 ex_ee_len = le16_to_cpu(ex->ee_len);
1643
1644 while (ex >= EXT_FIRST_EXTENT(eh) &&
1645 ex_ee_block + ex_ee_len > start) {
1646 ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
1647 path[depth].p_ext = ex;
1648
1649 a = ex_ee_block > start ? ex_ee_block : start;
1650 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
1651 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
1652
1653 ext_debug(" border %u:%u\n", a, b);
1654
1655 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
1656 block = 0;
1657 num = 0;
1658 BUG();
1659 } else if (a != ex_ee_block) {
1660 /* remove tail of the extent */
1661 block = ex_ee_block;
1662 num = a - block;
1663 } else if (b != ex_ee_block + ex_ee_len - 1) {
1664 /* remove head of the extent */
1665 block = a;
1666 num = b - a;
1667 /* there is no "make a hole" API yet */
1668 BUG();
1669 } else {
1670 /* remove whole extent: excellent! */
1671 block = ex_ee_block;
1672 num = 0;
1673 BUG_ON(a != ex_ee_block);
1674 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
1675 }
1676
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001677 /* at present, extent can't cross block group: */
Alex Tomasa86c6182006-10-11 01:21:03 -07001678 /* leaf + bitmap + group desc + sb + inode */
1679 credits = 5;
1680 if (ex == EXT_FIRST_EXTENT(eh)) {
1681 correct_index = 1;
1682 credits += (ext_depth(inode)) + 1;
1683 }
1684#ifdef CONFIG_QUOTA
1685 credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
1686#endif
1687
1688 handle = ext4_ext_journal_restart(handle, credits);
1689 if (IS_ERR(handle)) {
1690 err = PTR_ERR(handle);
1691 goto out;
1692 }
1693
1694 err = ext4_ext_get_access(handle, inode, path + depth);
1695 if (err)
1696 goto out;
1697
1698 err = ext4_remove_blocks(handle, inode, ex, a, b);
1699 if (err)
1700 goto out;
1701
1702 if (num == 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001703 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001704 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07001705 eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
1706 }
1707
1708 ex->ee_block = cpu_to_le32(block);
1709 ex->ee_len = cpu_to_le16(num);
1710
1711 err = ext4_ext_dirty(handle, inode, path + depth);
1712 if (err)
1713 goto out;
1714
Mingming Cao2ae02102006-10-11 01:21:11 -07001715 ext_debug("new extent: %u:%u:%llu\n", block, num,
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001716 ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07001717 ex--;
1718 ex_ee_block = le32_to_cpu(ex->ee_block);
1719 ex_ee_len = le16_to_cpu(ex->ee_len);
1720 }
1721
1722 if (correct_index && eh->eh_entries)
1723 err = ext4_ext_correct_indexes(handle, inode, path);
1724
1725 /* if this leaf is free, then we should
1726 * remove it from index block above */
1727 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1728 err = ext4_ext_rm_idx(handle, inode, path + depth);
1729
1730out:
1731 return err;
1732}
1733
1734/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001735 * ext4_ext_more_to_rm:
1736 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07001737 */
1738static int inline
1739ext4_ext_more_to_rm(struct ext4_ext_path *path)
1740{
1741 BUG_ON(path->p_idx == NULL);
1742
1743 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1744 return 0;
1745
1746 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001747 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07001748 * so we have to consider current index for truncation
1749 */
1750 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
1751 return 0;
1752 return 1;
1753}
1754
1755int ext4_ext_remove_space(struct inode *inode, unsigned long start)
1756{
1757 struct super_block *sb = inode->i_sb;
1758 int depth = ext_depth(inode);
1759 struct ext4_ext_path *path;
1760 handle_t *handle;
1761 int i = 0, err = 0;
1762
1763 ext_debug("truncate since %lu\n", start);
1764
1765 /* probably first extent we're gonna free will be last in block */
1766 handle = ext4_journal_start(inode, depth + 1);
1767 if (IS_ERR(handle))
1768 return PTR_ERR(handle);
1769
1770 ext4_ext_invalidate_cache(inode);
1771
1772 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001773 * We start scanning from right side, freeing all the blocks
1774 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07001775 */
1776 path = kmalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_KERNEL);
1777 if (path == NULL) {
1778 ext4_journal_stop(handle);
1779 return -ENOMEM;
1780 }
1781 memset(path, 0, sizeof(struct ext4_ext_path) * (depth + 1));
1782 path[0].p_hdr = ext_inode_hdr(inode);
1783 if (ext4_ext_check_header(__FUNCTION__, inode, path[0].p_hdr)) {
1784 err = -EIO;
1785 goto out;
1786 }
1787 path[0].p_depth = depth;
1788
1789 while (i >= 0 && err == 0) {
1790 if (i == depth) {
1791 /* this is leaf block */
1792 err = ext4_ext_rm_leaf(handle, inode, path, start);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001793 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07001794 brelse(path[i].p_bh);
1795 path[i].p_bh = NULL;
1796 i--;
1797 continue;
1798 }
1799
1800 /* this is index block */
1801 if (!path[i].p_hdr) {
1802 ext_debug("initialize header\n");
1803 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
1804 if (ext4_ext_check_header(__FUNCTION__, inode,
1805 path[i].p_hdr)) {
1806 err = -EIO;
1807 goto out;
1808 }
1809 }
1810
1811 BUG_ON(le16_to_cpu(path[i].p_hdr->eh_entries)
1812 > le16_to_cpu(path[i].p_hdr->eh_max));
1813 BUG_ON(path[i].p_hdr->eh_magic != EXT4_EXT_MAGIC);
1814
1815 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001816 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07001817 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
1818 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
1819 ext_debug("init index ptr: hdr 0x%p, num %d\n",
1820 path[i].p_hdr,
1821 le16_to_cpu(path[i].p_hdr->eh_entries));
1822 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001823 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001824 path[i].p_idx--;
1825 }
1826
1827 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
1828 i, EXT_FIRST_INDEX(path[i].p_hdr),
1829 path[i].p_idx);
1830 if (ext4_ext_more_to_rm(path + i)) {
1831 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07001832 ext_debug("move to level %d (block %llu)\n",
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001833 i + 1, idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07001834 memset(path + i + 1, 0, sizeof(*path));
1835 path[i+1].p_bh =
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001836 sb_bread(sb, idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07001837 if (!path[i+1].p_bh) {
1838 /* should we reset i_size? */
1839 err = -EIO;
1840 break;
1841 }
1842
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001843 /* save actual number of indexes since this
1844 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07001845 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
1846 i++;
1847 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001848 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07001849 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001850 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07001851 * handle must be already prepared by the
1852 * truncatei_leaf() */
1853 err = ext4_ext_rm_idx(handle, inode, path + i);
1854 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001855 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07001856 brelse(path[i].p_bh);
1857 path[i].p_bh = NULL;
1858 i--;
1859 ext_debug("return to level %d\n", i);
1860 }
1861 }
1862
1863 /* TODO: flexible tree reduction should be here */
1864 if (path->p_hdr->eh_entries == 0) {
1865 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001866 * truncate to zero freed all the tree,
1867 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07001868 */
1869 err = ext4_ext_get_access(handle, inode, path);
1870 if (err == 0) {
1871 ext_inode_hdr(inode)->eh_depth = 0;
1872 ext_inode_hdr(inode)->eh_max =
1873 cpu_to_le16(ext4_ext_space_root(inode));
1874 err = ext4_ext_dirty(handle, inode, path);
1875 }
1876 }
1877out:
1878 ext4_ext_tree_changed(inode);
1879 ext4_ext_drop_refs(path);
1880 kfree(path);
1881 ext4_journal_stop(handle);
1882
1883 return err;
1884}
1885
1886/*
1887 * called at mount time
1888 */
1889void ext4_ext_init(struct super_block *sb)
1890{
1891 /*
1892 * possible initialization would be here
1893 */
1894
1895 if (test_opt(sb, EXTENTS)) {
1896 printk("EXT4-fs: file extents enabled");
1897#ifdef AGRESSIVE_TEST
1898 printk(", agressive tests");
1899#endif
1900#ifdef CHECK_BINSEARCH
1901 printk(", check binsearch");
1902#endif
1903#ifdef EXTENTS_STATS
1904 printk(", stats");
1905#endif
1906 printk("\n");
1907#ifdef EXTENTS_STATS
1908 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
1909 EXT4_SB(sb)->s_ext_min = 1 << 30;
1910 EXT4_SB(sb)->s_ext_max = 0;
1911#endif
1912 }
1913}
1914
1915/*
1916 * called at umount time
1917 */
1918void ext4_ext_release(struct super_block *sb)
1919{
1920 if (!test_opt(sb, EXTENTS))
1921 return;
1922
1923#ifdef EXTENTS_STATS
1924 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
1925 struct ext4_sb_info *sbi = EXT4_SB(sb);
1926 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
1927 sbi->s_ext_blocks, sbi->s_ext_extents,
1928 sbi->s_ext_blocks / sbi->s_ext_extents);
1929 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
1930 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
1931 }
1932#endif
1933}
1934
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001935int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
1936 ext4_fsblk_t iblock,
Alex Tomasa86c6182006-10-11 01:21:03 -07001937 unsigned long max_blocks, struct buffer_head *bh_result,
1938 int create, int extend_disksize)
1939{
1940 struct ext4_ext_path *path = NULL;
1941 struct ext4_extent newex, *ex;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001942 ext4_fsblk_t goal, newblock;
1943 int err = 0, depth;
Alex Tomasa86c6182006-10-11 01:21:03 -07001944 unsigned long allocated = 0;
1945
1946 __clear_bit(BH_New, &bh_result->b_state);
1947 ext_debug("blocks %d/%lu requested for inode %u\n", (int) iblock,
1948 max_blocks, (unsigned) inode->i_ino);
1949 mutex_lock(&EXT4_I(inode)->truncate_mutex);
1950
1951 /* check in cache */
Avantika Mathur7e028972006-12-06 20:41:33 -08001952 goal = ext4_ext_in_cache(inode, iblock, &newex);
1953 if (goal) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001954 if (goal == EXT4_EXT_CACHE_GAP) {
1955 if (!create) {
1956 /* block isn't allocated yet and
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001957 * user doesn't want to allocate it */
Alex Tomasa86c6182006-10-11 01:21:03 -07001958 goto out2;
1959 }
1960 /* we should allocate requested block */
1961 } else if (goal == EXT4_EXT_CACHE_EXTENT) {
1962 /* block is already allocated */
1963 newblock = iblock
1964 - le32_to_cpu(newex.ee_block)
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001965 + ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001966 /* number of remaining blocks in the extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07001967 allocated = le16_to_cpu(newex.ee_len) -
1968 (iblock - le32_to_cpu(newex.ee_block));
1969 goto out;
1970 } else {
1971 BUG();
1972 }
1973 }
1974
1975 /* find extent for this block */
1976 path = ext4_ext_find_extent(inode, iblock, NULL);
1977 if (IS_ERR(path)) {
1978 err = PTR_ERR(path);
1979 path = NULL;
1980 goto out2;
1981 }
1982
1983 depth = ext_depth(inode);
1984
1985 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001986 * consistent leaf must not be empty;
1987 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07001988 * this is why assert can't be put in ext4_ext_find_extent()
1989 */
1990 BUG_ON(path[depth].p_ext == NULL && depth != 0);
1991
Avantika Mathur7e028972006-12-06 20:41:33 -08001992 ex = path[depth].p_ext;
1993 if (ex) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001994 unsigned long ee_block = le32_to_cpu(ex->ee_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001995 ext4_fsblk_t ee_start = ext_pblock(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001996 unsigned short ee_len = le16_to_cpu(ex->ee_len);
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001997
1998 /*
1999 * Allow future support for preallocated extents to be added
2000 * as an RO_COMPAT feature:
2001 * Uninitialized extents are treated as holes, except that
2002 * we avoid (fail) allocating new blocks during a write.
2003 */
2004 if (ee_len > EXT_MAX_LEN)
2005 goto out2;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002006 /* if found extent covers block, simply return it */
Alex Tomasa86c6182006-10-11 01:21:03 -07002007 if (iblock >= ee_block && iblock < ee_block + ee_len) {
2008 newblock = iblock - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002009 /* number of remaining blocks in the extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07002010 allocated = ee_len - (iblock - ee_block);
Mingming Cao2ae02102006-10-11 01:21:11 -07002011 ext_debug("%d fit into %lu:%d -> %llu\n", (int) iblock,
Alex Tomasa86c6182006-10-11 01:21:03 -07002012 ee_block, ee_len, newblock);
2013 ext4_ext_put_in_cache(inode, ee_block, ee_len,
2014 ee_start, EXT4_EXT_CACHE_EXTENT);
2015 goto out;
2016 }
2017 }
2018
2019 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002020 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07002021 * we couldn't try to create block if create flag is zero
2022 */
2023 if (!create) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002024 /* put just found gap into cache to speed up
2025 * subsequent requests */
Alex Tomasa86c6182006-10-11 01:21:03 -07002026 ext4_ext_put_gap_in_cache(inode, path, iblock);
2027 goto out2;
2028 }
2029 /*
Andrew Morton63f57932006-10-11 01:21:24 -07002030 * Okay, we need to do block allocation. Lazily initialize the block
2031 * allocation info here if necessary.
2032 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002033 if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
2034 ext4_init_block_alloc_info(inode);
2035
2036 /* allocate new block */
2037 goal = ext4_ext_find_goal(inode, path, iblock);
2038 allocated = max_blocks;
2039 newblock = ext4_new_blocks(handle, inode, goal, &allocated, &err);
2040 if (!newblock)
2041 goto out2;
Mingming Cao2ae02102006-10-11 01:21:11 -07002042 ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002043 goal, newblock, allocated);
2044
2045 /* try to insert new extent into found leaf and return */
2046 newex.ee_block = cpu_to_le32(iblock);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002047 ext4_ext_store_pblock(&newex, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -07002048 newex.ee_len = cpu_to_le16(allocated);
2049 err = ext4_ext_insert_extent(handle, inode, path, &newex);
2050 if (err)
2051 goto out2;
2052
2053 if (extend_disksize && inode->i_size > EXT4_I(inode)->i_disksize)
2054 EXT4_I(inode)->i_disksize = inode->i_size;
2055
2056 /* previous routine could use block we allocated */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002057 newblock = ext_pblock(&newex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002058 __set_bit(BH_New, &bh_result->b_state);
2059
2060 ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
2061 EXT4_EXT_CACHE_EXTENT);
2062out:
2063 if (allocated > max_blocks)
2064 allocated = max_blocks;
2065 ext4_ext_show_leaf(inode, path);
2066 __set_bit(BH_Mapped, &bh_result->b_state);
2067 bh_result->b_bdev = inode->i_sb->s_bdev;
2068 bh_result->b_blocknr = newblock;
2069out2:
2070 if (path) {
2071 ext4_ext_drop_refs(path);
2072 kfree(path);
2073 }
2074 mutex_unlock(&EXT4_I(inode)->truncate_mutex);
2075
2076 return err ? err : allocated;
2077}
2078
2079void ext4_ext_truncate(struct inode * inode, struct page *page)
2080{
2081 struct address_space *mapping = inode->i_mapping;
2082 struct super_block *sb = inode->i_sb;
2083 unsigned long last_block;
2084 handle_t *handle;
2085 int err = 0;
2086
2087 /*
2088 * probably first extent we're gonna free will be last in block
2089 */
2090 err = ext4_writepage_trans_blocks(inode) + 3;
2091 handle = ext4_journal_start(inode, err);
2092 if (IS_ERR(handle)) {
2093 if (page) {
2094 clear_highpage(page);
2095 flush_dcache_page(page);
2096 unlock_page(page);
2097 page_cache_release(page);
2098 }
2099 return;
2100 }
2101
2102 if (page)
2103 ext4_block_truncate_page(handle, page, mapping, inode->i_size);
2104
2105 mutex_lock(&EXT4_I(inode)->truncate_mutex);
2106 ext4_ext_invalidate_cache(inode);
2107
2108 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002109 * TODO: optimization is possible here.
2110 * Probably we need not scan at all,
2111 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07002112 */
2113 if (ext4_orphan_add(handle, inode))
2114 goto out_stop;
2115
2116 /* we have to know where to truncate from in crash case */
2117 EXT4_I(inode)->i_disksize = inode->i_size;
2118 ext4_mark_inode_dirty(handle, inode);
2119
2120 last_block = (inode->i_size + sb->s_blocksize - 1)
2121 >> EXT4_BLOCK_SIZE_BITS(sb);
2122 err = ext4_ext_remove_space(inode, last_block);
2123
2124 /* In a multi-transaction truncate, we only make the final
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002125 * transaction synchronous. */
Alex Tomasa86c6182006-10-11 01:21:03 -07002126 if (IS_SYNC(inode))
2127 handle->h_sync = 1;
2128
2129out_stop:
2130 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002131 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07002132 * then we need to clear up the orphan record which we created above.
2133 * However, if this was a real unlink then we were called by
2134 * ext4_delete_inode(), and we allow that function to clean up the
2135 * orphan info for us.
2136 */
2137 if (inode->i_nlink)
2138 ext4_orphan_del(handle, inode);
2139
2140 mutex_unlock(&EXT4_I(inode)->truncate_mutex);
2141 ext4_journal_stop(handle);
2142}
2143
2144/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002145 * ext4_ext_writepage_trans_blocks:
2146 * calculate max number of blocks we could modify
Alex Tomasa86c6182006-10-11 01:21:03 -07002147 * in order to allocate new block for an inode
2148 */
2149int ext4_ext_writepage_trans_blocks(struct inode *inode, int num)
2150{
2151 int needed;
2152
2153 needed = ext4_ext_calc_credits_for_insert(inode, NULL);
2154
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002155 /* caller wants to allocate num blocks, but note it includes sb */
Alex Tomasa86c6182006-10-11 01:21:03 -07002156 needed = needed * num - (num - 1);
2157
2158#ifdef CONFIG_QUOTA
2159 needed += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
2160#endif
2161
2162 return needed;
2163}
2164
2165EXPORT_SYMBOL(ext4_mark_inode_dirty);
2166EXPORT_SYMBOL(ext4_ext_invalidate_cache);
2167EXPORT_SYMBOL(ext4_ext_insert_extent);
2168EXPORT_SYMBOL(ext4_ext_walk_space);
2169EXPORT_SYMBOL(ext4_ext_find_goal);
2170EXPORT_SYMBOL(ext4_ext_calc_credits_for_insert);
2171