blob: a8db7fdf9cb7a7642acc01f1e6d0ff9eff120967 [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>
Mingming Caocd02ff02007-10-16 18:38:25 -040035#include <linux/jbd2.h>
Alex Tomasa86c6182006-10-11 01:21:03 -070036#include <linux/highuid.h>
37#include <linux/pagemap.h>
38#include <linux/quotaops.h>
39#include <linux/string.h>
40#include <linux/slab.h>
Amit Aroraa2df2a62007-07-17 21:42:41 -040041#include <linux/falloc.h>
Alex Tomasa86c6182006-10-11 01:21:03 -070042#include <asm/uaccess.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040043#include "ext4_jbd2.h"
44#include "ext4_extents.h"
Alex Tomasa86c6182006-10-11 01:21:03 -070045
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 */
Avantika Mathur09b88252006-12-06 20:41:36 -080051static ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
Alex Tomasf65e6fb2006-10-11 01:21:05 -070052{
53 ext4_fsblk_t block;
54
Aneesh Kumar K.Vb3776112007-10-16 18:38:25 -040055 block = le32_to_cpu(ex->ee_start_lo);
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 */
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -050064ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
Alex Tomasf65e6fb2006-10-11 01:21:05 -070065{
66 ext4_fsblk_t block;
67
Aneesh Kumar K.Vd8dd0b42007-10-16 18:38:25 -040068 block = le32_to_cpu(ix->ei_leaf_lo);
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 */
Aneesh Kumar K.Vc14c6fd2008-01-28 23:58:26 -050078void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
Alex Tomasf65e6fb2006-10-11 01:21:05 -070079{
Aneesh Kumar K.Vb3776112007-10-16 18:38:25 -040080 ex->ee_start_lo = 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 */
Avantika Mathur09b88252006-12-06 20:41:36 -080089static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb)
Alex Tomasf65e6fb2006-10-11 01:21:05 -070090{
Aneesh Kumar K.Vd8dd0b42007-10-16 18:38:25 -040091 ix->ei_leaf_lo = 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
Shen Feng9102e4f2008-07-11 19:27:31 -040095static int ext4_ext_journal_restart(handle_t *handle, int needed)
Alex Tomasa86c6182006-10-11 01:21:03 -070096{
97 int err;
98
99 if (handle->h_buffer_credits > needed)
Shen Feng9102e4f2008-07-11 19:27:31 -0400100 return 0;
101 err = ext4_journal_extend(handle, needed);
Theodore Ts'o0123c932008-08-01 20:57:54 -0400102 if (err <= 0)
Shen Feng9102e4f2008-07-11 19:27:31 -0400103 return err;
104 return ext4_journal_restart(handle, needed);
Alex Tomasa86c6182006-10-11 01:21:03 -0700105}
106
107/*
108 * could return:
109 * - EROFS
110 * - ENOMEM
111 */
112static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
113 struct ext4_ext_path *path)
114{
115 if (path->p_bh) {
116 /* path points to block */
117 return ext4_journal_get_write_access(handle, path->p_bh);
118 }
119 /* path points to leaf/index in inode body */
120 /* we use in-core data, no need to protect them */
121 return 0;
122}
123
124/*
125 * could return:
126 * - EROFS
127 * - ENOMEM
128 * - EIO
129 */
130static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
131 struct ext4_ext_path *path)
132{
133 int err;
134 if (path->p_bh) {
135 /* path points to block */
136 err = ext4_journal_dirty_metadata(handle, path->p_bh);
137 } else {
138 /* path points to leaf/index in inode body */
139 err = ext4_mark_inode_dirty(handle, inode);
140 }
141 return err;
142}
143
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700144static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700145 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500146 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700147{
148 struct ext4_inode_info *ei = EXT4_I(inode);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700149 ext4_fsblk_t bg_start;
Valerie Clement74d34872008-02-15 13:43:07 -0500150 ext4_fsblk_t last_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700151 ext4_grpblk_t colour;
Alex Tomasa86c6182006-10-11 01:21:03 -0700152 int depth;
153
154 if (path) {
155 struct ext4_extent *ex;
156 depth = path->p_depth;
157
158 /* try to predict block placement */
Avantika Mathur7e028972006-12-06 20:41:33 -0800159 ex = path[depth].p_ext;
160 if (ex)
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700161 return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700162
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700163 /* it looks like index is empty;
164 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700165 if (path[depth].p_bh)
166 return path[depth].p_bh->b_blocknr;
167 }
168
169 /* OK. use inode's group */
170 bg_start = (ei->i_block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) +
171 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block);
Valerie Clement74d34872008-02-15 13:43:07 -0500172 last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1;
173
174 if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block)
175 colour = (current->pid % 16) *
Alex Tomasa86c6182006-10-11 01:21:03 -0700176 (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
Valerie Clement74d34872008-02-15 13:43:07 -0500177 else
178 colour = (current->pid % 16) * ((last_block - bg_start) / 16);
Alex Tomasa86c6182006-10-11 01:21:03 -0700179 return bg_start + colour + block;
180}
181
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400182/*
183 * Allocation for a meta data block
184 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700185static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400186ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700187 struct ext4_ext_path *path,
188 struct ext4_extent *ex, int *err)
189{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700190 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700191
192 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Aneesh Kumar K.V7061eba2008-07-11 19:27:31 -0400193 newblock = ext4_new_meta_block(handle, inode, goal, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700194 return newblock;
195}
196
Avantika Mathur09b88252006-12-06 20:41:36 -0800197static int ext4_ext_space_block(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -0700198{
199 int size;
200
201 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
202 / sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100203#ifdef AGGRESSIVE_TEST
Alex Tomasa86c6182006-10-11 01:21:03 -0700204 if (size > 6)
205 size = 6;
206#endif
207 return size;
208}
209
Avantika Mathur09b88252006-12-06 20:41:36 -0800210static int ext4_ext_space_block_idx(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -0700211{
212 int size;
213
214 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
215 / sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100216#ifdef AGGRESSIVE_TEST
Alex Tomasa86c6182006-10-11 01:21:03 -0700217 if (size > 5)
218 size = 5;
219#endif
220 return size;
221}
222
Avantika Mathur09b88252006-12-06 20:41:36 -0800223static int ext4_ext_space_root(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -0700224{
225 int size;
226
227 size = sizeof(EXT4_I(inode)->i_data);
228 size -= sizeof(struct ext4_extent_header);
229 size /= sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100230#ifdef AGGRESSIVE_TEST
Alex Tomasa86c6182006-10-11 01:21:03 -0700231 if (size > 3)
232 size = 3;
233#endif
234 return size;
235}
236
Avantika Mathur09b88252006-12-06 20:41:36 -0800237static int ext4_ext_space_root_idx(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -0700238{
239 int size;
240
241 size = sizeof(EXT4_I(inode)->i_data);
242 size -= sizeof(struct ext4_extent_header);
243 size /= sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100244#ifdef AGGRESSIVE_TEST
Alex Tomasa86c6182006-10-11 01:21:03 -0700245 if (size > 4)
246 size = 4;
247#endif
248 return size;
249}
250
Mingming Caod2a17632008-07-14 17:52:37 -0400251/*
252 * Calculate the number of metadata blocks needed
253 * to allocate @blocks
254 * Worse case is one block per extent
255 */
256int ext4_ext_calc_metadata_amount(struct inode *inode, int blocks)
257{
258 int lcap, icap, rcap, leafs, idxs, num;
259 int newextents = blocks;
260
261 rcap = ext4_ext_space_root_idx(inode);
262 lcap = ext4_ext_space_block(inode);
263 icap = ext4_ext_space_block_idx(inode);
264
265 /* number of new leaf blocks needed */
266 num = leafs = (newextents + lcap - 1) / lcap;
267
268 /*
269 * Worse case, we need separate index block(s)
270 * to link all new leaf blocks
271 */
272 idxs = (leafs + icap - 1) / icap;
273 do {
274 num += idxs;
275 idxs = (idxs + icap - 1) / icap;
276 } while (idxs > rcap);
277
278 return num;
279}
280
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400281static int
282ext4_ext_max_entries(struct inode *inode, int depth)
283{
284 int max;
285
286 if (depth == ext_depth(inode)) {
287 if (depth == 0)
288 max = ext4_ext_space_root(inode);
289 else
290 max = ext4_ext_space_root_idx(inode);
291 } else {
292 if (depth == 0)
293 max = ext4_ext_space_block(inode);
294 else
295 max = ext4_ext_space_block_idx(inode);
296 }
297
298 return max;
299}
300
301static int __ext4_ext_check_header(const char *function, struct inode *inode,
302 struct ext4_extent_header *eh,
303 int depth)
304{
305 const char *error_msg;
306 int max = 0;
307
308 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
309 error_msg = "invalid magic";
310 goto corrupted;
311 }
312 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
313 error_msg = "unexpected eh_depth";
314 goto corrupted;
315 }
316 if (unlikely(eh->eh_max == 0)) {
317 error_msg = "invalid eh_max";
318 goto corrupted;
319 }
320 max = ext4_ext_max_entries(inode, depth);
321 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
322 error_msg = "too large eh_max";
323 goto corrupted;
324 }
325 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
326 error_msg = "invalid eh_entries";
327 goto corrupted;
328 }
329 return 0;
330
331corrupted:
332 ext4_error(inode->i_sb, function,
333 "bad header in inode #%lu: %s - magic %x, "
334 "entries %u, max %u(%u), depth %u(%u)",
335 inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
336 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
337 max, le16_to_cpu(eh->eh_depth), depth);
338
339 return -EIO;
340}
341
342#define ext4_ext_check_header(inode, eh, depth) \
Harvey Harrison46e665e2008-04-17 10:38:59 -0400343 __ext4_ext_check_header(__func__, inode, eh, depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400344
Alex Tomasa86c6182006-10-11 01:21:03 -0700345#ifdef EXT_DEBUG
346static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
347{
348 int k, l = path->p_depth;
349
350 ext_debug("path:");
351 for (k = 0; k <= l; k++, path++) {
352 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700353 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700354 idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700355 } else if (path->p_ext) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700356 ext_debug(" %d:%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700357 le32_to_cpu(path->p_ext->ee_block),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400358 ext4_ext_get_actual_len(path->p_ext),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700359 ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700360 } else
361 ext_debug(" []");
362 }
363 ext_debug("\n");
364}
365
366static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
367{
368 int depth = ext_depth(inode);
369 struct ext4_extent_header *eh;
370 struct ext4_extent *ex;
371 int i;
372
373 if (!path)
374 return;
375
376 eh = path[depth].p_hdr;
377 ex = EXT_FIRST_EXTENT(eh);
378
379 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700380 ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400381 ext4_ext_get_actual_len(ex), ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700382 }
383 ext_debug("\n");
384}
385#else
386#define ext4_ext_show_path(inode,path)
387#define ext4_ext_show_leaf(inode,path)
388#endif
389
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500390void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700391{
392 int depth = path->p_depth;
393 int i;
394
395 for (i = 0; i <= depth; i++, path++)
396 if (path->p_bh) {
397 brelse(path->p_bh);
398 path->p_bh = NULL;
399 }
400}
401
402/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700403 * ext4_ext_binsearch_idx:
404 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400405 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700406 */
407static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500408ext4_ext_binsearch_idx(struct inode *inode,
409 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700410{
411 struct ext4_extent_header *eh = path->p_hdr;
412 struct ext4_extent_idx *r, *l, *m;
413
Alex Tomasa86c6182006-10-11 01:21:03 -0700414
Eric Sandeenbba90742008-01-28 23:58:27 -0500415 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700416
417 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400418 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700419 while (l <= r) {
420 m = l + (r - l) / 2;
421 if (block < le32_to_cpu(m->ei_block))
422 r = m - 1;
423 else
424 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400425 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
426 m, le32_to_cpu(m->ei_block),
427 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700428 }
429
430 path->p_idx = l - 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700431 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400432 idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700433
434#ifdef CHECK_BINSEARCH
435 {
436 struct ext4_extent_idx *chix, *ix;
437 int k;
438
439 chix = ix = EXT_FIRST_INDEX(eh);
440 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
441 if (k != 0 &&
442 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400443 printk(KERN_DEBUG "k=%d, ix=0x%p, "
444 "first=0x%p\n", k,
445 ix, EXT_FIRST_INDEX(eh));
446 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700447 le32_to_cpu(ix->ei_block),
448 le32_to_cpu(ix[-1].ei_block));
449 }
450 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400451 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700452 if (block < le32_to_cpu(ix->ei_block))
453 break;
454 chix = ix;
455 }
456 BUG_ON(chix != path->p_idx);
457 }
458#endif
459
460}
461
462/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700463 * ext4_ext_binsearch:
464 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400465 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700466 */
467static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500468ext4_ext_binsearch(struct inode *inode,
469 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700470{
471 struct ext4_extent_header *eh = path->p_hdr;
472 struct ext4_extent *r, *l, *m;
473
Alex Tomasa86c6182006-10-11 01:21:03 -0700474 if (eh->eh_entries == 0) {
475 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700476 * this leaf is empty:
477 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700478 */
479 return;
480 }
481
Eric Sandeenbba90742008-01-28 23:58:27 -0500482 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700483
484 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400485 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700486
487 while (l <= r) {
488 m = l + (r - l) / 2;
489 if (block < le32_to_cpu(m->ee_block))
490 r = m - 1;
491 else
492 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400493 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
494 m, le32_to_cpu(m->ee_block),
495 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700496 }
497
498 path->p_ext = l - 1;
Mingming Cao2ae02102006-10-11 01:21:11 -0700499 ext_debug(" -> %d:%llu:%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400500 le32_to_cpu(path->p_ext->ee_block),
501 ext_pblock(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400502 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700503
504#ifdef CHECK_BINSEARCH
505 {
506 struct ext4_extent *chex, *ex;
507 int k;
508
509 chex = ex = EXT_FIRST_EXTENT(eh);
510 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
511 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400512 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700513 if (block < le32_to_cpu(ex->ee_block))
514 break;
515 chex = ex;
516 }
517 BUG_ON(chex != path->p_ext);
518 }
519#endif
520
521}
522
523int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
524{
525 struct ext4_extent_header *eh;
526
527 eh = ext_inode_hdr(inode);
528 eh->eh_depth = 0;
529 eh->eh_entries = 0;
530 eh->eh_magic = EXT4_EXT_MAGIC;
531 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
532 ext4_mark_inode_dirty(handle, inode);
533 ext4_ext_invalidate_cache(inode);
534 return 0;
535}
536
537struct ext4_ext_path *
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500538ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
539 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700540{
541 struct ext4_extent_header *eh;
542 struct buffer_head *bh;
543 short int depth, i, ppos = 0, alloc = 0;
544
545 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400546 depth = ext_depth(inode);
547 if (ext4_ext_check_header(inode, eh, depth))
Alex Tomasa86c6182006-10-11 01:21:03 -0700548 return ERR_PTR(-EIO);
549
Alex Tomasa86c6182006-10-11 01:21:03 -0700550
551 /* account possible depth increase */
552 if (!path) {
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800553 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700554 GFP_NOFS);
555 if (!path)
556 return ERR_PTR(-ENOMEM);
557 alloc = 1;
558 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700559 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400560 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700561
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400562 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700563 /* walk through the tree */
564 while (i) {
565 ext_debug("depth %d: num %d, max %d\n",
566 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400567
Alex Tomasa86c6182006-10-11 01:21:03 -0700568 ext4_ext_binsearch_idx(inode, path + ppos, block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700569 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700570 path[ppos].p_depth = i;
571 path[ppos].p_ext = NULL;
572
573 bh = sb_bread(inode->i_sb, path[ppos].p_block);
574 if (!bh)
575 goto err;
576
577 eh = ext_block_hdr(bh);
578 ppos++;
579 BUG_ON(ppos > depth);
580 path[ppos].p_bh = bh;
581 path[ppos].p_hdr = eh;
582 i--;
583
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400584 if (ext4_ext_check_header(inode, eh, i))
Alex Tomasa86c6182006-10-11 01:21:03 -0700585 goto err;
586 }
587
588 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700589 path[ppos].p_ext = NULL;
590 path[ppos].p_idx = NULL;
591
Alex Tomasa86c6182006-10-11 01:21:03 -0700592 /* find extent */
593 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400594 /* if not an empty leaf */
595 if (path[ppos].p_ext)
596 path[ppos].p_block = ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700597
598 ext4_ext_show_path(inode, path);
599
600 return path;
601
602err:
603 ext4_ext_drop_refs(path);
604 if (alloc)
605 kfree(path);
606 return ERR_PTR(-EIO);
607}
608
609/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700610 * ext4_ext_insert_index:
611 * insert new index [@logical;@ptr] into the block at @curp;
612 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700613 */
614static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
615 struct ext4_ext_path *curp,
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700616 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700617{
618 struct ext4_extent_idx *ix;
619 int len, err;
620
Avantika Mathur7e028972006-12-06 20:41:33 -0800621 err = ext4_ext_get_access(handle, inode, curp);
622 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700623 return err;
624
625 BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
626 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
627 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
628 /* insert after */
629 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
630 len = (len - 1) * sizeof(struct ext4_extent_idx);
631 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400632 ext_debug("insert new index %d after: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700633 "move %d from 0x%p to 0x%p\n",
634 logical, ptr, len,
635 (curp->p_idx + 1), (curp->p_idx + 2));
636 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
637 }
638 ix = curp->p_idx + 1;
639 } else {
640 /* insert before */
641 len = len * sizeof(struct ext4_extent_idx);
642 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400643 ext_debug("insert new index %d before: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700644 "move %d from 0x%p to 0x%p\n",
645 logical, ptr, len,
646 curp->p_idx, (curp->p_idx + 1));
647 memmove(curp->p_idx + 1, curp->p_idx, len);
648 ix = curp->p_idx;
649 }
650
651 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700652 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400653 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700654
655 BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400656 > le16_to_cpu(curp->p_hdr->eh_max));
Alex Tomasa86c6182006-10-11 01:21:03 -0700657 BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
658
659 err = ext4_ext_dirty(handle, inode, curp);
660 ext4_std_error(inode->i_sb, err);
661
662 return err;
663}
664
665/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700666 * ext4_ext_split:
667 * inserts new subtree into the path, using free index entry
668 * at depth @at:
669 * - allocates all needed blocks (new leaf and all intermediate index blocks)
670 * - makes decision where to split
671 * - moves remaining extents and index entries (right to the split point)
672 * into the newly allocated blocks
673 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700674 */
675static int ext4_ext_split(handle_t *handle, struct inode *inode,
676 struct ext4_ext_path *path,
677 struct ext4_extent *newext, int at)
678{
679 struct buffer_head *bh = NULL;
680 int depth = ext_depth(inode);
681 struct ext4_extent_header *neh;
682 struct ext4_extent_idx *fidx;
683 struct ext4_extent *ex;
684 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700685 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700686 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700687 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700688 int err = 0;
689
690 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700691 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700692
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700693 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700694 * border from split point */
695 BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
696 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
697 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700698 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700699 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400700 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700701 } else {
702 border = newext->ee_block;
703 ext_debug("leaf will be added."
704 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400705 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700706 }
707
708 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700709 * If error occurs, then we break processing
710 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700711 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700712 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700713 */
714
715 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700716 * Get array to track all allocated blocks.
717 * We need this to handle errors and free blocks
718 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700719 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800720 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700721 if (!ablocks)
722 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700723
724 /* allocate all needed blocks */
725 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
726 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400727 newblock = ext4_ext_new_meta_block(handle, inode, path,
728 newext, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700729 if (newblock == 0)
730 goto cleanup;
731 ablocks[a] = newblock;
732 }
733
734 /* initialize new leaf */
735 newblock = ablocks[--a];
736 BUG_ON(newblock == 0);
737 bh = sb_getblk(inode->i_sb, newblock);
738 if (!bh) {
739 err = -EIO;
740 goto cleanup;
741 }
742 lock_buffer(bh);
743
Avantika Mathur7e028972006-12-06 20:41:33 -0800744 err = ext4_journal_get_create_access(handle, bh);
745 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700746 goto cleanup;
747
748 neh = ext_block_hdr(bh);
749 neh->eh_entries = 0;
750 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
751 neh->eh_magic = EXT4_EXT_MAGIC;
752 neh->eh_depth = 0;
753 ex = EXT_FIRST_EXTENT(neh);
754
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700755 /* move remainder of path[depth] to the new leaf */
Alex Tomasa86c6182006-10-11 01:21:03 -0700756 BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
757 /* start copy from next extent */
758 /* TODO: we could do it by single memmove */
759 m = 0;
760 path[depth].p_ext++;
761 while (path[depth].p_ext <=
762 EXT_MAX_EXTENT(path[depth].p_hdr)) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700763 ext_debug("move %d:%llu:%d in new leaf %llu\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400764 le32_to_cpu(path[depth].p_ext->ee_block),
765 ext_pblock(path[depth].p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400766 ext4_ext_get_actual_len(path[depth].p_ext),
Alex Tomasa86c6182006-10-11 01:21:03 -0700767 newblock);
768 /*memmove(ex++, path[depth].p_ext++,
769 sizeof(struct ext4_extent));
770 neh->eh_entries++;*/
771 path[depth].p_ext++;
772 m++;
773 }
774 if (m) {
775 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400776 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700777 }
778
779 set_buffer_uptodate(bh);
780 unlock_buffer(bh);
781
Avantika Mathur7e028972006-12-06 20:41:33 -0800782 err = ext4_journal_dirty_metadata(handle, bh);
783 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700784 goto cleanup;
785 brelse(bh);
786 bh = NULL;
787
788 /* correct old leaf */
789 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800790 err = ext4_ext_get_access(handle, inode, path + depth);
791 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700792 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400793 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800794 err = ext4_ext_dirty(handle, inode, path + depth);
795 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700796 goto cleanup;
797
798 }
799
800 /* create intermediate indexes */
801 k = depth - at - 1;
802 BUG_ON(k < 0);
803 if (k)
804 ext_debug("create %d intermediate indices\n", k);
805 /* insert new index into current index block */
806 /* current depth stored in i var */
807 i = depth - 1;
808 while (k--) {
809 oldblock = newblock;
810 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -0500811 bh = sb_getblk(inode->i_sb, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700812 if (!bh) {
813 err = -EIO;
814 goto cleanup;
815 }
816 lock_buffer(bh);
817
Avantika Mathur7e028972006-12-06 20:41:33 -0800818 err = ext4_journal_get_create_access(handle, bh);
819 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700820 goto cleanup;
821
822 neh = ext_block_hdr(bh);
823 neh->eh_entries = cpu_to_le16(1);
824 neh->eh_magic = EXT4_EXT_MAGIC;
825 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
826 neh->eh_depth = cpu_to_le16(depth - i);
827 fidx = EXT_FIRST_INDEX(neh);
828 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700829 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700830
Eric Sandeenbba90742008-01-28 23:58:27 -0500831 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
832 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700833 /* copy indexes */
834 m = 0;
835 path[i].p_idx++;
836
837 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
838 EXT_MAX_INDEX(path[i].p_hdr));
839 BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
840 EXT_LAST_INDEX(path[i].p_hdr));
841 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400842 ext_debug("%d: move %d:%llu in new index %llu\n", i,
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400843 le32_to_cpu(path[i].p_idx->ei_block),
844 idx_pblock(path[i].p_idx),
845 newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700846 /*memmove(++fidx, path[i].p_idx++,
847 sizeof(struct ext4_extent_idx));
848 neh->eh_entries++;
849 BUG_ON(neh->eh_entries > neh->eh_max);*/
850 path[i].p_idx++;
851 m++;
852 }
853 if (m) {
854 memmove(++fidx, path[i].p_idx - m,
855 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400856 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700857 }
858 set_buffer_uptodate(bh);
859 unlock_buffer(bh);
860
Avantika Mathur7e028972006-12-06 20:41:33 -0800861 err = ext4_journal_dirty_metadata(handle, bh);
862 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700863 goto cleanup;
864 brelse(bh);
865 bh = NULL;
866
867 /* correct old index */
868 if (m) {
869 err = ext4_ext_get_access(handle, inode, path + i);
870 if (err)
871 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400872 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700873 err = ext4_ext_dirty(handle, inode, path + i);
874 if (err)
875 goto cleanup;
876 }
877
878 i--;
879 }
880
881 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -0700882 err = ext4_ext_insert_index(handle, inode, path + at,
883 le32_to_cpu(border), newblock);
884
885cleanup:
886 if (bh) {
887 if (buffer_locked(bh))
888 unlock_buffer(bh);
889 brelse(bh);
890 }
891
892 if (err) {
893 /* free all allocated blocks in error case */
894 for (i = 0; i < depth; i++) {
895 if (!ablocks[i])
896 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -0500897 ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700898 }
899 }
900 kfree(ablocks);
901
902 return err;
903}
904
905/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700906 * ext4_ext_grow_indepth:
907 * implements tree growing procedure:
908 * - allocates new block
909 * - moves top-level data (index block or leaf) into the new block
910 * - initializes new top-level, creating index that points to the
911 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -0700912 */
913static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
914 struct ext4_ext_path *path,
915 struct ext4_extent *newext)
916{
917 struct ext4_ext_path *curp = path;
918 struct ext4_extent_header *neh;
919 struct ext4_extent_idx *fidx;
920 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700921 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700922 int err = 0;
923
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400924 newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700925 if (newblock == 0)
926 return err;
927
928 bh = sb_getblk(inode->i_sb, newblock);
929 if (!bh) {
930 err = -EIO;
931 ext4_std_error(inode->i_sb, err);
932 return err;
933 }
934 lock_buffer(bh);
935
Avantika Mathur7e028972006-12-06 20:41:33 -0800936 err = ext4_journal_get_create_access(handle, bh);
937 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -0700938 unlock_buffer(bh);
939 goto out;
940 }
941
942 /* move top-level index/leaf into new block */
943 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
944
945 /* set size of new block */
946 neh = ext_block_hdr(bh);
947 /* old root could have indexes or leaves
948 * so calculate e_max right way */
949 if (ext_depth(inode))
950 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
951 else
952 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
953 neh->eh_magic = EXT4_EXT_MAGIC;
954 set_buffer_uptodate(bh);
955 unlock_buffer(bh);
956
Avantika Mathur7e028972006-12-06 20:41:33 -0800957 err = ext4_journal_dirty_metadata(handle, bh);
958 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700959 goto out;
960
961 /* create index in new top-level index: num,max,pointer */
Avantika Mathur7e028972006-12-06 20:41:33 -0800962 err = ext4_ext_get_access(handle, inode, curp);
963 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700964 goto out;
965
966 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
967 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
968 curp->p_hdr->eh_entries = cpu_to_le16(1);
969 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400970
971 if (path[0].p_hdr->eh_depth)
972 curp->p_idx->ei_block =
973 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
974 else
975 curp->p_idx->ei_block =
976 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700977 ext4_idx_store_pblock(curp->p_idx, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700978
979 neh = ext_inode_hdr(inode);
980 fidx = EXT_FIRST_INDEX(neh);
Mingming Cao2ae02102006-10-11 01:21:11 -0700981 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700982 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700983 le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700984
985 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
986 err = ext4_ext_dirty(handle, inode, curp);
987out:
988 brelse(bh);
989
990 return err;
991}
992
993/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700994 * ext4_ext_create_new_leaf:
995 * finds empty index and adds new leaf.
996 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -0700997 */
998static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
999 struct ext4_ext_path *path,
1000 struct ext4_extent *newext)
1001{
1002 struct ext4_ext_path *curp;
1003 int depth, i, err = 0;
1004
1005repeat:
1006 i = depth = ext_depth(inode);
1007
1008 /* walk up to the tree and look for free index entry */
1009 curp = path + depth;
1010 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1011 i--;
1012 curp--;
1013 }
1014
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001015 /* we use already allocated block for index block,
1016 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001017 if (EXT_HAS_FREE_INDEX(curp)) {
1018 /* if we found index with free entry, then use that
1019 * entry: create all needed subtree and add new leaf */
1020 err = ext4_ext_split(handle, inode, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001021 if (err)
1022 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001023
1024 /* refill path */
1025 ext4_ext_drop_refs(path);
1026 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001027 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1028 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001029 if (IS_ERR(path))
1030 err = PTR_ERR(path);
1031 } else {
1032 /* tree is full, time to grow in depth */
1033 err = ext4_ext_grow_indepth(handle, inode, path, newext);
1034 if (err)
1035 goto out;
1036
1037 /* refill path */
1038 ext4_ext_drop_refs(path);
1039 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001040 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1041 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001042 if (IS_ERR(path)) {
1043 err = PTR_ERR(path);
1044 goto out;
1045 }
1046
1047 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001048 * only first (depth 0 -> 1) produces free space;
1049 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001050 */
1051 depth = ext_depth(inode);
1052 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001053 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001054 goto repeat;
1055 }
1056 }
1057
1058out:
1059 return err;
1060}
1061
1062/*
Alex Tomas1988b512008-01-28 23:58:27 -05001063 * search the closest allocated block to the left for *logical
1064 * and returns it at @logical + it's physical address at @phys
1065 * if *logical is the smallest allocated block, the function
1066 * returns 0 at @phys
1067 * return value contains 0 (success) or error code
1068 */
1069int
1070ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
1071 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1072{
1073 struct ext4_extent_idx *ix;
1074 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001075 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001076
1077 BUG_ON(path == NULL);
1078 depth = path->p_depth;
1079 *phys = 0;
1080
1081 if (depth == 0 && path->p_ext == NULL)
1082 return 0;
1083
1084 /* usually extent in the path covers blocks smaller
1085 * then *logical, but it can be that extent is the
1086 * first one in the file */
1087
1088 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001089 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001090 if (*logical < le32_to_cpu(ex->ee_block)) {
1091 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1092 while (--depth >= 0) {
1093 ix = path[depth].p_idx;
1094 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1095 }
1096 return 0;
1097 }
1098
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001099 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
Alex Tomas1988b512008-01-28 23:58:27 -05001100
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001101 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1102 *phys = ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001103 return 0;
1104}
1105
1106/*
1107 * search the closest allocated block to the right for *logical
1108 * and returns it at @logical + it's physical address at @phys
1109 * if *logical is the smallest allocated block, the function
1110 * returns 0 at @phys
1111 * return value contains 0 (success) or error code
1112 */
1113int
1114ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
1115 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1116{
1117 struct buffer_head *bh = NULL;
1118 struct ext4_extent_header *eh;
1119 struct ext4_extent_idx *ix;
1120 struct ext4_extent *ex;
1121 ext4_fsblk_t block;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001122 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001123
1124 BUG_ON(path == NULL);
1125 depth = path->p_depth;
1126 *phys = 0;
1127
1128 if (depth == 0 && path->p_ext == NULL)
1129 return 0;
1130
1131 /* usually extent in the path covers blocks smaller
1132 * then *logical, but it can be that extent is the
1133 * first one in the file */
1134
1135 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001136 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001137 if (*logical < le32_to_cpu(ex->ee_block)) {
1138 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1139 while (--depth >= 0) {
1140 ix = path[depth].p_idx;
1141 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1142 }
1143 *logical = le32_to_cpu(ex->ee_block);
1144 *phys = ext_pblock(ex);
1145 return 0;
1146 }
1147
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001148 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
Alex Tomas1988b512008-01-28 23:58:27 -05001149
1150 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1151 /* next allocated block in this leaf */
1152 ex++;
1153 *logical = le32_to_cpu(ex->ee_block);
1154 *phys = ext_pblock(ex);
1155 return 0;
1156 }
1157
1158 /* go up and search for index to the right */
1159 while (--depth >= 0) {
1160 ix = path[depth].p_idx;
1161 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
1162 break;
1163 }
1164
1165 if (depth < 0) {
1166 /* we've gone up to the root and
1167 * found no index to the right */
1168 return 0;
1169 }
1170
1171 /* we've found index to the right, let's
1172 * follow it and find the closest allocated
1173 * block to the right */
1174 ix++;
1175 block = idx_pblock(ix);
1176 while (++depth < path->p_depth) {
1177 bh = sb_bread(inode->i_sb, block);
1178 if (bh == NULL)
1179 return -EIO;
1180 eh = ext_block_hdr(bh);
1181 if (ext4_ext_check_header(inode, eh, depth)) {
1182 put_bh(bh);
1183 return -EIO;
1184 }
1185 ix = EXT_FIRST_INDEX(eh);
1186 block = idx_pblock(ix);
1187 put_bh(bh);
1188 }
1189
1190 bh = sb_bread(inode->i_sb, block);
1191 if (bh == NULL)
1192 return -EIO;
1193 eh = ext_block_hdr(bh);
1194 if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) {
1195 put_bh(bh);
1196 return -EIO;
1197 }
1198 ex = EXT_FIRST_EXTENT(eh);
1199 *logical = le32_to_cpu(ex->ee_block);
1200 *phys = ext_pblock(ex);
1201 put_bh(bh);
1202 return 0;
1203
1204}
1205
1206/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001207 * ext4_ext_next_allocated_block:
1208 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1209 * NOTE: it considers block number from index entry as
1210 * allocated block. Thus, index entries have to be consistent
1211 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001212 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001213static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001214ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1215{
1216 int depth;
1217
1218 BUG_ON(path == NULL);
1219 depth = path->p_depth;
1220
1221 if (depth == 0 && path->p_ext == NULL)
1222 return EXT_MAX_BLOCK;
1223
1224 while (depth >= 0) {
1225 if (depth == path->p_depth) {
1226 /* leaf */
1227 if (path[depth].p_ext !=
1228 EXT_LAST_EXTENT(path[depth].p_hdr))
1229 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1230 } else {
1231 /* index */
1232 if (path[depth].p_idx !=
1233 EXT_LAST_INDEX(path[depth].p_hdr))
1234 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1235 }
1236 depth--;
1237 }
1238
1239 return EXT_MAX_BLOCK;
1240}
1241
1242/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001243 * ext4_ext_next_leaf_block:
Alex Tomasa86c6182006-10-11 01:21:03 -07001244 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1245 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001246static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
Andrew Morton63f57932006-10-11 01:21:24 -07001247 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001248{
1249 int depth;
1250
1251 BUG_ON(path == NULL);
1252 depth = path->p_depth;
1253
1254 /* zero-tree has no leaf blocks at all */
1255 if (depth == 0)
1256 return EXT_MAX_BLOCK;
1257
1258 /* go to index block */
1259 depth--;
1260
1261 while (depth >= 0) {
1262 if (path[depth].p_idx !=
1263 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001264 return (ext4_lblk_t)
1265 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001266 depth--;
1267 }
1268
1269 return EXT_MAX_BLOCK;
1270}
1271
1272/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001273 * ext4_ext_correct_indexes:
1274 * if leaf gets modified and modified extent is first in the leaf,
1275 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001276 * TODO: do we need to correct tree in all cases?
1277 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001278static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001279 struct ext4_ext_path *path)
1280{
1281 struct ext4_extent_header *eh;
1282 int depth = ext_depth(inode);
1283 struct ext4_extent *ex;
1284 __le32 border;
1285 int k, err = 0;
1286
1287 eh = path[depth].p_hdr;
1288 ex = path[depth].p_ext;
1289 BUG_ON(ex == NULL);
1290 BUG_ON(eh == NULL);
1291
1292 if (depth == 0) {
1293 /* there is no tree at all */
1294 return 0;
1295 }
1296
1297 if (ex != EXT_FIRST_EXTENT(eh)) {
1298 /* we correct tree if first leaf got modified only */
1299 return 0;
1300 }
1301
1302 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001303 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001304 */
1305 k = depth - 1;
1306 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001307 err = ext4_ext_get_access(handle, inode, path + k);
1308 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001309 return err;
1310 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001311 err = ext4_ext_dirty(handle, inode, path + k);
1312 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001313 return err;
1314
1315 while (k--) {
1316 /* change all left-side indexes */
1317 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1318 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001319 err = ext4_ext_get_access(handle, inode, path + k);
1320 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001321 break;
1322 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001323 err = ext4_ext_dirty(handle, inode, path + k);
1324 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001325 break;
1326 }
1327
1328 return err;
1329}
1330
Avantika Mathur09b88252006-12-06 20:41:36 -08001331static int
Alex Tomasa86c6182006-10-11 01:21:03 -07001332ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1333 struct ext4_extent *ex2)
1334{
Amit Arora749269f2007-07-18 09:02:56 -04001335 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001336
1337 /*
1338 * Make sure that either both extents are uninitialized, or
1339 * both are _not_.
1340 */
1341 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1342 return 0;
1343
Amit Arora749269f2007-07-18 09:02:56 -04001344 if (ext4_ext_is_uninitialized(ex1))
1345 max_len = EXT_UNINIT_MAX_LEN;
1346 else
1347 max_len = EXT_INIT_MAX_LEN;
1348
Amit Aroraa2df2a62007-07-17 21:42:41 -04001349 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1350 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1351
1352 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001353 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001354 return 0;
1355
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001356 /*
1357 * To allow future support for preallocated extents to be added
1358 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001359 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001360 */
Amit Arora749269f2007-07-18 09:02:56 -04001361 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001362 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001363#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001364 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001365 return 0;
1366#endif
1367
Amit Aroraa2df2a62007-07-17 21:42:41 -04001368 if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001369 return 1;
1370 return 0;
1371}
1372
1373/*
Amit Arora56055d32007-07-17 21:42:38 -04001374 * This function tries to merge the "ex" extent to the next extent in the tree.
1375 * It always tries to merge towards right. If you want to merge towards
1376 * left, pass "ex - 1" as argument instead of "ex".
1377 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1378 * 1 if they got merged.
1379 */
1380int ext4_ext_try_to_merge(struct inode *inode,
1381 struct ext4_ext_path *path,
1382 struct ext4_extent *ex)
1383{
1384 struct ext4_extent_header *eh;
1385 unsigned int depth, len;
1386 int merge_done = 0;
1387 int uninitialized = 0;
1388
1389 depth = ext_depth(inode);
1390 BUG_ON(path[depth].p_hdr == NULL);
1391 eh = path[depth].p_hdr;
1392
1393 while (ex < EXT_LAST_EXTENT(eh)) {
1394 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1395 break;
1396 /* merge with next extent! */
1397 if (ext4_ext_is_uninitialized(ex))
1398 uninitialized = 1;
1399 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1400 + ext4_ext_get_actual_len(ex + 1));
1401 if (uninitialized)
1402 ext4_ext_mark_uninitialized(ex);
1403
1404 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1405 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1406 * sizeof(struct ext4_extent);
1407 memmove(ex + 1, ex + 2, len);
1408 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001409 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001410 merge_done = 1;
1411 WARN_ON(eh->eh_entries == 0);
1412 if (!eh->eh_entries)
1413 ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
1414 "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
1415 }
1416
1417 return merge_done;
1418}
1419
1420/*
Amit Arora25d14f92007-05-24 13:04:13 -04001421 * check if a portion of the "newext" extent overlaps with an
1422 * existing extent.
1423 *
1424 * If there is an overlap discovered, it updates the length of the newext
1425 * such that there will be no overlap, and then returns 1.
1426 * If there is no overlap found, it returns 0.
1427 */
1428unsigned int ext4_ext_check_overlap(struct inode *inode,
1429 struct ext4_extent *newext,
1430 struct ext4_ext_path *path)
1431{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001432 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001433 unsigned int depth, len1;
1434 unsigned int ret = 0;
1435
1436 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001437 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001438 depth = ext_depth(inode);
1439 if (!path[depth].p_ext)
1440 goto out;
1441 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1442
1443 /*
1444 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001445 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001446 */
1447 if (b2 < b1) {
1448 b2 = ext4_ext_next_allocated_block(path);
1449 if (b2 == EXT_MAX_BLOCK)
1450 goto out;
1451 }
1452
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001453 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001454 if (b1 + len1 < b1) {
1455 len1 = EXT_MAX_BLOCK - b1;
1456 newext->ee_len = cpu_to_le16(len1);
1457 ret = 1;
1458 }
1459
1460 /* check for overlap */
1461 if (b1 + len1 > b2) {
1462 newext->ee_len = cpu_to_le16(b2 - b1);
1463 ret = 1;
1464 }
1465out:
1466 return ret;
1467}
1468
1469/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001470 * ext4_ext_insert_extent:
1471 * tries to merge requsted extent into the existing extent or
1472 * inserts requested extent as new one into the tree,
1473 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001474 */
1475int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1476 struct ext4_ext_path *path,
1477 struct ext4_extent *newext)
1478{
1479 struct ext4_extent_header * eh;
1480 struct ext4_extent *ex, *fex;
1481 struct ext4_extent *nearex; /* nearest extent */
1482 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001483 int depth, len, err;
1484 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001485 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001486
Amit Aroraa2df2a62007-07-17 21:42:41 -04001487 BUG_ON(ext4_ext_get_actual_len(newext) == 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07001488 depth = ext_depth(inode);
1489 ex = path[depth].p_ext;
1490 BUG_ON(path[depth].p_hdr == NULL);
1491
1492 /* try to insert block into found extent and return */
1493 if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
Mingming Cao2ae02102006-10-11 01:21:11 -07001494 ext_debug("append %d block to %d:%d (from %llu)\n",
Amit Aroraa2df2a62007-07-17 21:42:41 -04001495 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001496 le32_to_cpu(ex->ee_block),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001497 ext4_ext_get_actual_len(ex), ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001498 err = ext4_ext_get_access(handle, inode, path + depth);
1499 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001500 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001501
1502 /*
1503 * ext4_can_extents_be_merged should have checked that either
1504 * both extents are uninitialized, or both aren't. Thus we
1505 * need to check only one of them here.
1506 */
1507 if (ext4_ext_is_uninitialized(ex))
1508 uninitialized = 1;
1509 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1510 + ext4_ext_get_actual_len(newext));
1511 if (uninitialized)
1512 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001513 eh = path[depth].p_hdr;
1514 nearex = ex;
1515 goto merge;
1516 }
1517
1518repeat:
1519 depth = ext_depth(inode);
1520 eh = path[depth].p_hdr;
1521 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1522 goto has_space;
1523
1524 /* probably next leaf has space for us? */
1525 fex = EXT_LAST_EXTENT(eh);
1526 next = ext4_ext_next_leaf_block(inode, path);
1527 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1528 && next != EXT_MAX_BLOCK) {
1529 ext_debug("next leaf block - %d\n", next);
1530 BUG_ON(npath != NULL);
1531 npath = ext4_ext_find_extent(inode, next, NULL);
1532 if (IS_ERR(npath))
1533 return PTR_ERR(npath);
1534 BUG_ON(npath->p_depth != path->p_depth);
1535 eh = npath[depth].p_hdr;
1536 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1537 ext_debug("next leaf isnt full(%d)\n",
1538 le16_to_cpu(eh->eh_entries));
1539 path = npath;
1540 goto repeat;
1541 }
1542 ext_debug("next leaf has no free space(%d,%d)\n",
1543 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1544 }
1545
1546 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001547 * There is no free space in the found leaf.
1548 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001549 */
1550 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1551 if (err)
1552 goto cleanup;
1553 depth = ext_depth(inode);
1554 eh = path[depth].p_hdr;
1555
1556has_space:
1557 nearex = path[depth].p_ext;
1558
Avantika Mathur7e028972006-12-06 20:41:33 -08001559 err = ext4_ext_get_access(handle, inode, path + depth);
1560 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001561 goto cleanup;
1562
1563 if (!nearex) {
1564 /* there is no extent in this leaf, create first one */
Mingming Cao2ae02102006-10-11 01:21:11 -07001565 ext_debug("first extent in the leaf: %d:%llu:%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001566 le32_to_cpu(newext->ee_block),
1567 ext_pblock(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001568 ext4_ext_get_actual_len(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001569 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1570 } else if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001571 > le32_to_cpu(nearex->ee_block)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001572/* BUG_ON(newext->ee_block == nearex->ee_block); */
1573 if (nearex != EXT_LAST_EXTENT(eh)) {
1574 len = EXT_MAX_EXTENT(eh) - nearex;
1575 len = (len - 1) * sizeof(struct ext4_extent);
1576 len = len < 0 ? 0 : len;
Mingming Cao2ae02102006-10-11 01:21:11 -07001577 ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001578 "move %d from 0x%p to 0x%p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001579 le32_to_cpu(newext->ee_block),
1580 ext_pblock(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001581 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001582 nearex, len, nearex + 1, nearex + 2);
1583 memmove(nearex + 2, nearex + 1, len);
1584 }
1585 path[depth].p_ext = nearex + 1;
1586 } else {
1587 BUG_ON(newext->ee_block == nearex->ee_block);
1588 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1589 len = len < 0 ? 0 : len;
Mingming Cao2ae02102006-10-11 01:21:11 -07001590 ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001591 "move %d from 0x%p to 0x%p\n",
1592 le32_to_cpu(newext->ee_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001593 ext_pblock(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001594 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001595 nearex, len, nearex + 1, nearex + 2);
1596 memmove(nearex + 1, nearex, len);
1597 path[depth].p_ext = nearex;
1598 }
1599
Marcin Slusarze8546d02008-04-17 10:38:59 -04001600 le16_add_cpu(&eh->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001601 nearex = path[depth].p_ext;
1602 nearex->ee_block = newext->ee_block;
Aneesh Kumar K.Vb3776112007-10-16 18:38:25 -04001603 ext4_ext_store_pblock(nearex, ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001604 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001605
1606merge:
1607 /* try to merge extents to the right */
Amit Arora56055d32007-07-17 21:42:38 -04001608 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001609
1610 /* try to merge extents to the left */
1611
1612 /* time to correct all indexes above */
1613 err = ext4_ext_correct_indexes(handle, inode, path);
1614 if (err)
1615 goto cleanup;
1616
1617 err = ext4_ext_dirty(handle, inode, path + depth);
1618
1619cleanup:
1620 if (npath) {
1621 ext4_ext_drop_refs(npath);
1622 kfree(npath);
1623 }
1624 ext4_ext_tree_changed(inode);
1625 ext4_ext_invalidate_cache(inode);
1626 return err;
1627}
1628
Avantika Mathur09b88252006-12-06 20:41:36 -08001629static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001630ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Mingming Caodd545672007-07-31 00:37:46 -07001631 __u32 len, ext4_fsblk_t start, int type)
Alex Tomasa86c6182006-10-11 01:21:03 -07001632{
1633 struct ext4_ext_cache *cex;
1634 BUG_ON(len == 0);
1635 cex = &EXT4_I(inode)->i_cached_extent;
1636 cex->ec_type = type;
1637 cex->ec_block = block;
1638 cex->ec_len = len;
1639 cex->ec_start = start;
1640}
1641
1642/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001643 * ext4_ext_put_gap_in_cache:
1644 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07001645 * and cache this gap
1646 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001647static void
Alex Tomasa86c6182006-10-11 01:21:03 -07001648ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001649 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07001650{
1651 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001652 unsigned long len;
1653 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001654 struct ext4_extent *ex;
1655
1656 ex = path[depth].p_ext;
1657 if (ex == NULL) {
1658 /* there is no extent yet, so gap is [0;-] */
1659 lblock = 0;
1660 len = EXT_MAX_BLOCK;
1661 ext_debug("cache gap(whole file):");
1662 } else if (block < le32_to_cpu(ex->ee_block)) {
1663 lblock = block;
1664 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05001665 ext_debug("cache gap(before): %u [%u:%u]",
1666 block,
1667 le32_to_cpu(ex->ee_block),
1668 ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07001669 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001670 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001671 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001672 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001673 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001674
1675 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05001676 ext_debug("cache gap(after): [%u:%u] %u",
1677 le32_to_cpu(ex->ee_block),
1678 ext4_ext_get_actual_len(ex),
1679 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001680 BUG_ON(next == lblock);
1681 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001682 } else {
1683 lblock = len = 0;
1684 BUG();
1685 }
1686
Eric Sandeenbba90742008-01-28 23:58:27 -05001687 ext_debug(" -> %u:%lu\n", lblock, len);
Alex Tomasa86c6182006-10-11 01:21:03 -07001688 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1689}
1690
Avantika Mathur09b88252006-12-06 20:41:36 -08001691static int
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001692ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
Alex Tomasa86c6182006-10-11 01:21:03 -07001693 struct ext4_extent *ex)
1694{
1695 struct ext4_ext_cache *cex;
1696
1697 cex = &EXT4_I(inode)->i_cached_extent;
1698
1699 /* has cache valid data? */
1700 if (cex->ec_type == EXT4_EXT_CACHE_NO)
1701 return EXT4_EXT_CACHE_NO;
1702
1703 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1704 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1705 if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001706 ex->ee_block = cpu_to_le32(cex->ec_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001707 ext4_ext_store_pblock(ex, cex->ec_start);
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001708 ex->ee_len = cpu_to_le16(cex->ec_len);
Eric Sandeenbba90742008-01-28 23:58:27 -05001709 ext_debug("%u cached by %u:%u:%llu\n",
1710 block,
1711 cex->ec_block, cex->ec_len, cex->ec_start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001712 return cex->ec_type;
1713 }
1714
1715 /* not in cache */
1716 return EXT4_EXT_CACHE_NO;
1717}
1718
1719/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001720 * ext4_ext_rm_idx:
1721 * removes index from the index block.
1722 * It's used in truncate case only, thus all requests are for
1723 * last index in the block only.
Alex Tomasa86c6182006-10-11 01:21:03 -07001724 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001725static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001726 struct ext4_ext_path *path)
1727{
1728 struct buffer_head *bh;
1729 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001730 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07001731
1732 /* free index block */
1733 path--;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001734 leaf = idx_pblock(path->p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -07001735 BUG_ON(path->p_hdr->eh_entries == 0);
Avantika Mathur7e028972006-12-06 20:41:33 -08001736 err = ext4_ext_get_access(handle, inode, path);
1737 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001738 return err;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001739 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08001740 err = ext4_ext_dirty(handle, inode, path);
1741 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001742 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07001743 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Alex Tomasa86c6182006-10-11 01:21:03 -07001744 bh = sb_find_get_block(inode->i_sb, leaf);
1745 ext4_forget(handle, 1, inode, bh, leaf);
Alex Tomasc9de5602008-01-29 00:19:52 -05001746 ext4_free_blocks(handle, inode, leaf, 1, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001747 return err;
1748}
1749
1750/*
Mingming Caoee12b632008-08-19 22:16:05 -04001751 * ext4_ext_calc_credits_for_single_extent:
1752 * This routine returns max. credits that needed to insert an extent
1753 * to the extent tree.
1754 * When pass the actual path, the caller should calculate credits
1755 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07001756 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04001757int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07001758 struct ext4_ext_path *path)
1759{
Alex Tomasa86c6182006-10-11 01:21:03 -07001760 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04001761 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04001762 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04001763
Alex Tomasa86c6182006-10-11 01:21:03 -07001764 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07001765 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04001766 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
1767
1768 /*
1769 * There are some space in the leaf tree, no
1770 * need to account for leaf block credit
1771 *
1772 * bitmaps and block group descriptor blocks
1773 * and other metadat blocks still need to be
1774 * accounted.
1775 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04001776 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04001777 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
1778 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001779 }
1780
Mingming Cao525f4ed2008-08-19 22:15:58 -04001781 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04001782}
Alex Tomasa86c6182006-10-11 01:21:03 -07001783
Mingming Caoee12b632008-08-19 22:16:05 -04001784/*
1785 * How many index/leaf blocks need to change/allocate to modify nrblocks?
1786 *
1787 * if nrblocks are fit in a single extent (chunk flag is 1), then
1788 * in the worse case, each tree level index/leaf need to be changed
1789 * if the tree split due to insert a new extent, then the old tree
1790 * index/leaf need to be updated too
1791 *
1792 * If the nrblocks are discontiguous, they could cause
1793 * the whole tree split more than once, but this is really rare.
1794 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04001795int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoee12b632008-08-19 22:16:05 -04001796{
1797 int index;
1798 int depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07001799
Mingming Caoee12b632008-08-19 22:16:05 -04001800 if (chunk)
1801 index = depth * 2;
1802 else
1803 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07001804
Mingming Caoee12b632008-08-19 22:16:05 -04001805 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07001806}
1807
1808static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
1809 struct ext4_extent *ex,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001810 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07001811{
1812 struct buffer_head *bh;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001813 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001814 int i, metadata = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001815
Alex Tomasc9de5602008-01-29 00:19:52 -05001816 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1817 metadata = 1;
Alex Tomasa86c6182006-10-11 01:21:03 -07001818#ifdef EXTENTS_STATS
1819 {
1820 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07001821 spin_lock(&sbi->s_ext_stats_lock);
1822 sbi->s_ext_blocks += ee_len;
1823 sbi->s_ext_extents++;
1824 if (ee_len < sbi->s_ext_min)
1825 sbi->s_ext_min = ee_len;
1826 if (ee_len > sbi->s_ext_max)
1827 sbi->s_ext_max = ee_len;
1828 if (ext_depth(inode) > sbi->s_depth_max)
1829 sbi->s_depth_max = ext_depth(inode);
1830 spin_unlock(&sbi->s_ext_stats_lock);
1831 }
1832#endif
1833 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001834 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001835 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001836 ext4_lblk_t num;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001837 ext4_fsblk_t start;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001838
Amit Aroraa2df2a62007-07-17 21:42:41 -04001839 num = le32_to_cpu(ex->ee_block) + ee_len - from;
1840 start = ext_pblock(ex) + ee_len - num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001841 ext_debug("free last %u blocks starting %llu\n", num, start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001842 for (i = 0; i < num; i++) {
1843 bh = sb_find_get_block(inode->i_sb, start + i);
1844 ext4_forget(handle, 0, inode, bh, start + i);
1845 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001846 ext4_free_blocks(handle, inode, start, num, metadata);
Alex Tomasa86c6182006-10-11 01:21:03 -07001847 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001848 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001849 printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
Amit Aroraa2df2a62007-07-17 21:42:41 -04001850 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07001851 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001852 printk(KERN_INFO "strange request: removal(2) "
1853 "%u-%u from %u:%u\n",
1854 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07001855 }
1856 return 0;
1857}
1858
1859static int
1860ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001861 struct ext4_ext_path *path, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07001862{
1863 int err = 0, correct_index = 0;
1864 int depth = ext_depth(inode), credits;
1865 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001866 ext4_lblk_t a, b, block;
1867 unsigned num;
1868 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07001869 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001870 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001871 struct ext4_extent *ex;
1872
Alex Tomasc29c0ae2007-07-18 09:19:09 -04001873 /* the header must be checked already in ext4_ext_remove_space() */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001874 ext_debug("truncate since %u in leaf\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001875 if (!path[depth].p_hdr)
1876 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
1877 eh = path[depth].p_hdr;
1878 BUG_ON(eh == NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07001879
1880 /* find where to start removing */
1881 ex = EXT_LAST_EXTENT(eh);
1882
1883 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001884 if (ext4_ext_is_uninitialized(ex))
1885 uninitialized = 1;
1886 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001887
1888 while (ex >= EXT_FIRST_EXTENT(eh) &&
1889 ex_ee_block + ex_ee_len > start) {
1890 ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
1891 path[depth].p_ext = ex;
1892
1893 a = ex_ee_block > start ? ex_ee_block : start;
1894 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
1895 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
1896
1897 ext_debug(" border %u:%u\n", a, b);
1898
1899 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
1900 block = 0;
1901 num = 0;
1902 BUG();
1903 } else if (a != ex_ee_block) {
1904 /* remove tail of the extent */
1905 block = ex_ee_block;
1906 num = a - block;
1907 } else if (b != ex_ee_block + ex_ee_len - 1) {
1908 /* remove head of the extent */
1909 block = a;
1910 num = b - a;
1911 /* there is no "make a hole" API yet */
1912 BUG();
1913 } else {
1914 /* remove whole extent: excellent! */
1915 block = ex_ee_block;
1916 num = 0;
1917 BUG_ON(a != ex_ee_block);
1918 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
1919 }
1920
Theodore Ts'o34071da2008-08-01 21:59:19 -04001921 /*
1922 * 3 for leaf, sb, and inode plus 2 (bmap and group
1923 * descriptor) for each block group; assume two block
1924 * groups plus ex_ee_len/blocks_per_block_group for
1925 * the worst case
1926 */
1927 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07001928 if (ex == EXT_FIRST_EXTENT(eh)) {
1929 correct_index = 1;
1930 credits += (ext_depth(inode)) + 1;
1931 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001932 credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07001933
Shen Feng9102e4f2008-07-11 19:27:31 -04001934 err = ext4_ext_journal_restart(handle, credits);
1935 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001936 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001937
1938 err = ext4_ext_get_access(handle, inode, path + depth);
1939 if (err)
1940 goto out;
1941
1942 err = ext4_remove_blocks(handle, inode, ex, a, b);
1943 if (err)
1944 goto out;
1945
1946 if (num == 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001947 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001948 ext4_ext_store_pblock(ex, 0);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001949 le16_add_cpu(&eh->eh_entries, -1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001950 }
1951
1952 ex->ee_block = cpu_to_le32(block);
1953 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04001954 /*
1955 * Do not mark uninitialized if all the blocks in the
1956 * extent have been removed.
1957 */
1958 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001959 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001960
1961 err = ext4_ext_dirty(handle, inode, path + depth);
1962 if (err)
1963 goto out;
1964
Mingming Cao2ae02102006-10-11 01:21:11 -07001965 ext_debug("new extent: %u:%u:%llu\n", block, num,
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001966 ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07001967 ex--;
1968 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001969 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001970 }
1971
1972 if (correct_index && eh->eh_entries)
1973 err = ext4_ext_correct_indexes(handle, inode, path);
1974
1975 /* if this leaf is free, then we should
1976 * remove it from index block above */
1977 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1978 err = ext4_ext_rm_idx(handle, inode, path + depth);
1979
1980out:
1981 return err;
1982}
1983
1984/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001985 * ext4_ext_more_to_rm:
1986 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07001987 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001988static int
Alex Tomasa86c6182006-10-11 01:21:03 -07001989ext4_ext_more_to_rm(struct ext4_ext_path *path)
1990{
1991 BUG_ON(path->p_idx == NULL);
1992
1993 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1994 return 0;
1995
1996 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001997 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07001998 * so we have to consider current index for truncation
1999 */
2000 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2001 return 0;
2002 return 1;
2003}
2004
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002005static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07002006{
2007 struct super_block *sb = inode->i_sb;
2008 int depth = ext_depth(inode);
2009 struct ext4_ext_path *path;
2010 handle_t *handle;
2011 int i = 0, err = 0;
2012
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002013 ext_debug("truncate since %u\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002014
2015 /* probably first extent we're gonna free will be last in block */
2016 handle = ext4_journal_start(inode, depth + 1);
2017 if (IS_ERR(handle))
2018 return PTR_ERR(handle);
2019
2020 ext4_ext_invalidate_cache(inode);
2021
2022 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002023 * We start scanning from right side, freeing all the blocks
2024 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002025 */
Josef Bacik216553c2008-04-29 22:02:02 -04002026 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07002027 if (path == NULL) {
2028 ext4_journal_stop(handle);
2029 return -ENOMEM;
2030 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002031 path[0].p_hdr = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002032 if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002033 err = -EIO;
2034 goto out;
2035 }
2036 path[0].p_depth = depth;
2037
2038 while (i >= 0 && err == 0) {
2039 if (i == depth) {
2040 /* this is leaf block */
2041 err = ext4_ext_rm_leaf(handle, inode, path, start);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002042 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002043 brelse(path[i].p_bh);
2044 path[i].p_bh = NULL;
2045 i--;
2046 continue;
2047 }
2048
2049 /* this is index block */
2050 if (!path[i].p_hdr) {
2051 ext_debug("initialize header\n");
2052 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002053 }
2054
Alex Tomasa86c6182006-10-11 01:21:03 -07002055 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002056 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002057 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2058 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2059 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2060 path[i].p_hdr,
2061 le16_to_cpu(path[i].p_hdr->eh_entries));
2062 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002063 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002064 path[i].p_idx--;
2065 }
2066
2067 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2068 i, EXT_FIRST_INDEX(path[i].p_hdr),
2069 path[i].p_idx);
2070 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002071 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002072 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002073 ext_debug("move to level %d (block %llu)\n",
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002074 i + 1, idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002075 memset(path + i + 1, 0, sizeof(*path));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002076 bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2077 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002078 /* should we reset i_size? */
2079 err = -EIO;
2080 break;
2081 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002082 if (WARN_ON(i + 1 > depth)) {
2083 err = -EIO;
2084 break;
2085 }
2086 if (ext4_ext_check_header(inode, ext_block_hdr(bh),
2087 depth - i - 1)) {
2088 err = -EIO;
2089 break;
2090 }
2091 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002092
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002093 /* save actual number of indexes since this
2094 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002095 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2096 i++;
2097 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002098 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002099 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002100 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002101 * handle must be already prepared by the
2102 * truncatei_leaf() */
2103 err = ext4_ext_rm_idx(handle, inode, path + i);
2104 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002105 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002106 brelse(path[i].p_bh);
2107 path[i].p_bh = NULL;
2108 i--;
2109 ext_debug("return to level %d\n", i);
2110 }
2111 }
2112
2113 /* TODO: flexible tree reduction should be here */
2114 if (path->p_hdr->eh_entries == 0) {
2115 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002116 * truncate to zero freed all the tree,
2117 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002118 */
2119 err = ext4_ext_get_access(handle, inode, path);
2120 if (err == 0) {
2121 ext_inode_hdr(inode)->eh_depth = 0;
2122 ext_inode_hdr(inode)->eh_max =
2123 cpu_to_le16(ext4_ext_space_root(inode));
2124 err = ext4_ext_dirty(handle, inode, path);
2125 }
2126 }
2127out:
2128 ext4_ext_tree_changed(inode);
2129 ext4_ext_drop_refs(path);
2130 kfree(path);
2131 ext4_journal_stop(handle);
2132
2133 return err;
2134}
2135
2136/*
2137 * called at mount time
2138 */
2139void ext4_ext_init(struct super_block *sb)
2140{
2141 /*
2142 * possible initialization would be here
2143 */
2144
2145 if (test_opt(sb, EXTENTS)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002146 printk(KERN_INFO "EXT4-fs: file extents enabled");
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002147#ifdef AGGRESSIVE_TEST
2148 printk(", aggressive tests");
Alex Tomasa86c6182006-10-11 01:21:03 -07002149#endif
2150#ifdef CHECK_BINSEARCH
2151 printk(", check binsearch");
2152#endif
2153#ifdef EXTENTS_STATS
2154 printk(", stats");
2155#endif
2156 printk("\n");
2157#ifdef EXTENTS_STATS
2158 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2159 EXT4_SB(sb)->s_ext_min = 1 << 30;
2160 EXT4_SB(sb)->s_ext_max = 0;
2161#endif
2162 }
2163}
2164
2165/*
2166 * called at umount time
2167 */
2168void ext4_ext_release(struct super_block *sb)
2169{
2170 if (!test_opt(sb, EXTENTS))
2171 return;
2172
2173#ifdef EXTENTS_STATS
2174 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2175 struct ext4_sb_info *sbi = EXT4_SB(sb);
2176 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2177 sbi->s_ext_blocks, sbi->s_ext_extents,
2178 sbi->s_ext_blocks / sbi->s_ext_extents);
2179 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2180 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2181 }
2182#endif
2183}
2184
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002185static void bi_complete(struct bio *bio, int error)
2186{
2187 complete((struct completion *)bio->bi_private);
2188}
2189
2190/* FIXME!! we need to try to merge to left or right after zero-out */
2191static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2192{
2193 int ret = -EIO;
2194 struct bio *bio;
2195 int blkbits, blocksize;
2196 sector_t ee_pblock;
2197 struct completion event;
2198 unsigned int ee_len, len, done, offset;
2199
2200
2201 blkbits = inode->i_blkbits;
2202 blocksize = inode->i_sb->s_blocksize;
2203 ee_len = ext4_ext_get_actual_len(ex);
2204 ee_pblock = ext_pblock(ex);
2205
2206 /* convert ee_pblock to 512 byte sectors */
2207 ee_pblock = ee_pblock << (blkbits - 9);
2208
2209 while (ee_len > 0) {
2210
2211 if (ee_len > BIO_MAX_PAGES)
2212 len = BIO_MAX_PAGES;
2213 else
2214 len = ee_len;
2215
2216 bio = bio_alloc(GFP_NOIO, len);
2217 if (!bio)
2218 return -ENOMEM;
2219 bio->bi_sector = ee_pblock;
2220 bio->bi_bdev = inode->i_sb->s_bdev;
2221
2222 done = 0;
2223 offset = 0;
2224 while (done < len) {
2225 ret = bio_add_page(bio, ZERO_PAGE(0),
2226 blocksize, offset);
2227 if (ret != blocksize) {
2228 /*
2229 * We can't add any more pages because of
2230 * hardware limitations. Start a new bio.
2231 */
2232 break;
2233 }
2234 done++;
2235 offset += blocksize;
2236 if (offset >= PAGE_CACHE_SIZE)
2237 offset = 0;
2238 }
2239
2240 init_completion(&event);
2241 bio->bi_private = &event;
2242 bio->bi_end_io = bi_complete;
2243 submit_bio(WRITE, bio);
2244 wait_for_completion(&event);
2245
2246 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
2247 ret = 0;
2248 else {
2249 ret = -EIO;
2250 break;
2251 }
2252 bio_put(bio);
2253 ee_len -= done;
2254 ee_pblock += done << (blkbits - 9);
2255 }
2256 return ret;
2257}
2258
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002259#define EXT4_EXT_ZERO_LEN 7
2260
Amit Arora56055d32007-07-17 21:42:38 -04002261/*
2262 * This function is called by ext4_ext_get_blocks() if someone tries to write
2263 * to an uninitialized extent. It may result in splitting the uninitialized
2264 * extent into multiple extents (upto three - one initialized and two
2265 * uninitialized).
2266 * There are three possibilities:
2267 * a> There is no split required: Entire extent should be initialized
2268 * b> Splits in two extents: Write is happening at either end of the extent
2269 * c> Splits in three extents: Somone is writing in middle of the extent
2270 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002271static int ext4_ext_convert_to_initialized(handle_t *handle,
2272 struct inode *inode,
2273 struct ext4_ext_path *path,
2274 ext4_lblk_t iblock,
2275 unsigned long max_blocks)
Amit Arora56055d32007-07-17 21:42:38 -04002276{
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002277 struct ext4_extent *ex, newex, orig_ex;
Amit Arora56055d32007-07-17 21:42:38 -04002278 struct ext4_extent *ex1 = NULL;
2279 struct ext4_extent *ex2 = NULL;
2280 struct ext4_extent *ex3 = NULL;
2281 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002282 ext4_lblk_t ee_block;
2283 unsigned int allocated, ee_len, depth;
Amit Arora56055d32007-07-17 21:42:38 -04002284 ext4_fsblk_t newblock;
2285 int err = 0;
2286 int ret = 0;
2287
2288 depth = ext_depth(inode);
2289 eh = path[depth].p_hdr;
2290 ex = path[depth].p_ext;
2291 ee_block = le32_to_cpu(ex->ee_block);
2292 ee_len = ext4_ext_get_actual_len(ex);
2293 allocated = ee_len - (iblock - ee_block);
2294 newblock = iblock - ee_block + ext_pblock(ex);
2295 ex2 = ex;
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002296 orig_ex.ee_block = ex->ee_block;
2297 orig_ex.ee_len = cpu_to_le16(ee_len);
2298 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
Amit Arora56055d32007-07-17 21:42:38 -04002299
Aneesh Kumar K.V9df56432008-02-22 06:17:31 -05002300 err = ext4_ext_get_access(handle, inode, path + depth);
2301 if (err)
2302 goto out;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002303 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2304 if (ee_len <= 2*EXT4_EXT_ZERO_LEN) {
2305 err = ext4_ext_zeroout(inode, &orig_ex);
2306 if (err)
2307 goto fix_extent_len;
2308 /* update the extent length and mark as initialized */
2309 ex->ee_block = orig_ex.ee_block;
2310 ex->ee_len = orig_ex.ee_len;
2311 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2312 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002313 /* zeroed the full extent */
2314 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002315 }
Aneesh Kumar K.V9df56432008-02-22 06:17:31 -05002316
Amit Arora56055d32007-07-17 21:42:38 -04002317 /* ex1: ee_block to iblock - 1 : uninitialized */
2318 if (iblock > ee_block) {
2319 ex1 = ex;
2320 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2321 ext4_ext_mark_uninitialized(ex1);
2322 ex2 = &newex;
2323 }
2324 /*
2325 * for sanity, update the length of the ex2 extent before
2326 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2327 * overlap of blocks.
2328 */
2329 if (!ex1 && allocated > max_blocks)
2330 ex2->ee_len = cpu_to_le16(max_blocks);
2331 /* ex3: to ee_block + ee_len : uninitialised */
2332 if (allocated > max_blocks) {
2333 unsigned int newdepth;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002334 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
2335 if (allocated <= EXT4_EXT_ZERO_LEN) {
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002336 /*
2337 * iblock == ee_block is handled by the zerouout
2338 * at the beginning.
2339 * Mark first half uninitialized.
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002340 * Mark second half initialized and zero out the
2341 * initialized extent
2342 */
2343 ex->ee_block = orig_ex.ee_block;
2344 ex->ee_len = cpu_to_le16(ee_len - allocated);
2345 ext4_ext_mark_uninitialized(ex);
2346 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2347 ext4_ext_dirty(handle, inode, path + depth);
2348
2349 ex3 = &newex;
2350 ex3->ee_block = cpu_to_le32(iblock);
2351 ext4_ext_store_pblock(ex3, newblock);
2352 ex3->ee_len = cpu_to_le16(allocated);
2353 err = ext4_ext_insert_extent(handle, inode, path, ex3);
2354 if (err == -ENOSPC) {
2355 err = ext4_ext_zeroout(inode, &orig_ex);
2356 if (err)
2357 goto fix_extent_len;
2358 ex->ee_block = orig_ex.ee_block;
2359 ex->ee_len = orig_ex.ee_len;
2360 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2361 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002362 /* blocks available from iblock */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002363 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002364
2365 } else if (err)
2366 goto fix_extent_len;
2367
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002368 /*
2369 * We need to zero out the second half because
2370 * an fallocate request can update file size and
2371 * converting the second half to initialized extent
2372 * implies that we can leak some junk data to user
2373 * space.
2374 */
2375 err = ext4_ext_zeroout(inode, ex3);
2376 if (err) {
2377 /*
2378 * We should actually mark the
2379 * second half as uninit and return error
2380 * Insert would have changed the extent
2381 */
2382 depth = ext_depth(inode);
2383 ext4_ext_drop_refs(path);
2384 path = ext4_ext_find_extent(inode,
2385 iblock, path);
2386 if (IS_ERR(path)) {
2387 err = PTR_ERR(path);
2388 return err;
2389 }
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002390 /* get the second half extent details */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002391 ex = path[depth].p_ext;
2392 err = ext4_ext_get_access(handle, inode,
2393 path + depth);
2394 if (err)
2395 return err;
2396 ext4_ext_mark_uninitialized(ex);
2397 ext4_ext_dirty(handle, inode, path + depth);
2398 return err;
2399 }
2400
2401 /* zeroed the second half */
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002402 return allocated;
2403 }
Amit Arora56055d32007-07-17 21:42:38 -04002404 ex3 = &newex;
2405 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2406 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2407 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2408 ext4_ext_mark_uninitialized(ex3);
2409 err = ext4_ext_insert_extent(handle, inode, path, ex3);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002410 if (err == -ENOSPC) {
2411 err = ext4_ext_zeroout(inode, &orig_ex);
2412 if (err)
2413 goto fix_extent_len;
2414 /* update the extent length and mark as initialized */
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002415 ex->ee_block = orig_ex.ee_block;
2416 ex->ee_len = orig_ex.ee_len;
2417 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002418 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002419 /* zeroed the full extent */
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002420 /* blocks available from iblock */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002421 return allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002422
2423 } else if (err)
2424 goto fix_extent_len;
Amit Arora56055d32007-07-17 21:42:38 -04002425 /*
2426 * The depth, and hence eh & ex might change
2427 * as part of the insert above.
2428 */
2429 newdepth = ext_depth(inode);
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002430 /*
2431 * update the extent length after successfull insert of the
2432 * split extent
2433 */
2434 orig_ex.ee_len = cpu_to_le16(ee_len -
2435 ext4_ext_get_actual_len(ex3));
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002436 depth = newdepth;
2437 ext4_ext_drop_refs(path);
2438 path = ext4_ext_find_extent(inode, iblock, path);
2439 if (IS_ERR(path)) {
2440 err = PTR_ERR(path);
2441 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04002442 }
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002443 eh = path[depth].p_hdr;
2444 ex = path[depth].p_ext;
2445 if (ex2 != &newex)
2446 ex2 = ex;
2447
2448 err = ext4_ext_get_access(handle, inode, path + depth);
2449 if (err)
2450 goto out;
2451
Amit Arora56055d32007-07-17 21:42:38 -04002452 allocated = max_blocks;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002453
2454 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2455 * to insert a extent in the middle zerout directly
2456 * otherwise give the extent a chance to merge to left
2457 */
2458 if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
2459 iblock != ee_block) {
2460 err = ext4_ext_zeroout(inode, &orig_ex);
2461 if (err)
2462 goto fix_extent_len;
2463 /* update the extent length and mark as initialized */
2464 ex->ee_block = orig_ex.ee_block;
2465 ex->ee_len = orig_ex.ee_len;
2466 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2467 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002468 /* zero out the first half */
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002469 /* blocks available from iblock */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002470 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002471 }
Amit Arora56055d32007-07-17 21:42:38 -04002472 }
2473 /*
2474 * If there was a change of depth as part of the
2475 * insertion of ex3 above, we need to update the length
2476 * of the ex1 extent again here
2477 */
2478 if (ex1 && ex1 != ex) {
2479 ex1 = ex;
2480 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2481 ext4_ext_mark_uninitialized(ex1);
2482 ex2 = &newex;
2483 }
2484 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2485 ex2->ee_block = cpu_to_le32(iblock);
Amit Arora56055d32007-07-17 21:42:38 -04002486 ext4_ext_store_pblock(ex2, newblock);
2487 ex2->ee_len = cpu_to_le16(allocated);
2488 if (ex2 != ex)
2489 goto insert;
Amit Arora56055d32007-07-17 21:42:38 -04002490 /*
2491 * New (initialized) extent starts from the first block
2492 * in the current extent. i.e., ex2 == ex
2493 * We have to see if it can be merged with the extent
2494 * on the left.
2495 */
2496 if (ex2 > EXT_FIRST_EXTENT(eh)) {
2497 /*
2498 * To merge left, pass "ex2 - 1" to try_to_merge(),
2499 * since it merges towards right _only_.
2500 */
2501 ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
2502 if (ret) {
2503 err = ext4_ext_correct_indexes(handle, inode, path);
2504 if (err)
2505 goto out;
2506 depth = ext_depth(inode);
2507 ex2--;
2508 }
2509 }
2510 /*
2511 * Try to Merge towards right. This might be required
2512 * only when the whole extent is being written to.
2513 * i.e. ex2 == ex and ex3 == NULL.
2514 */
2515 if (!ex3) {
2516 ret = ext4_ext_try_to_merge(inode, path, ex2);
2517 if (ret) {
2518 err = ext4_ext_correct_indexes(handle, inode, path);
2519 if (err)
2520 goto out;
2521 }
2522 }
2523 /* Mark modified extent as dirty */
2524 err = ext4_ext_dirty(handle, inode, path + depth);
2525 goto out;
2526insert:
2527 err = ext4_ext_insert_extent(handle, inode, path, &newex);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002528 if (err == -ENOSPC) {
2529 err = ext4_ext_zeroout(inode, &orig_ex);
2530 if (err)
2531 goto fix_extent_len;
2532 /* update the extent length and mark as initialized */
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002533 ex->ee_block = orig_ex.ee_block;
2534 ex->ee_len = orig_ex.ee_len;
2535 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002536 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002537 /* zero out the first half */
2538 return allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002539 } else if (err)
2540 goto fix_extent_len;
Amit Arora56055d32007-07-17 21:42:38 -04002541out:
2542 return err ? err : allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002543
2544fix_extent_len:
2545 ex->ee_block = orig_ex.ee_block;
2546 ex->ee_len = orig_ex.ee_len;
2547 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2548 ext4_ext_mark_uninitialized(ex);
2549 ext4_ext_dirty(handle, inode, path + depth);
2550 return err;
Amit Arora56055d32007-07-17 21:42:38 -04002551}
2552
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05002553/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05002554 * Block allocation/map/preallocation routine for extents based files
2555 *
2556 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05002557 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05002558 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
2559 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05002560 *
2561 * return > 0, number of of blocks already mapped/allocated
2562 * if create == 0 and these are pre-allocated blocks
2563 * buffer head is unmapped
2564 * otherwise blocks are mapped
2565 *
2566 * return = 0, if plain look up failed (blocks have not been allocated)
2567 * buffer head is unmapped
2568 *
2569 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05002570 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002571int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002572 ext4_lblk_t iblock,
Alex Tomasa86c6182006-10-11 01:21:03 -07002573 unsigned long max_blocks, struct buffer_head *bh_result,
2574 int create, int extend_disksize)
2575{
2576 struct ext4_ext_path *path = NULL;
Amit Arora56055d32007-07-17 21:42:38 -04002577 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002578 struct ext4_extent newex, *ex;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002579 ext4_fsblk_t goal, newblock;
Amit Arora56055d32007-07-17 21:42:38 -04002580 int err = 0, depth, ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07002581 unsigned long allocated = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002582 struct ext4_allocation_request ar;
Mingming Cao61628a32008-07-11 19:27:31 -04002583 loff_t disksize;
Alex Tomasa86c6182006-10-11 01:21:03 -07002584
2585 __clear_bit(BH_New, &bh_result->b_state);
Eric Sandeenbba90742008-01-28 23:58:27 -05002586 ext_debug("blocks %u/%lu requested for inode %u\n",
2587 iblock, max_blocks, inode->i_ino);
Alex Tomasa86c6182006-10-11 01:21:03 -07002588
2589 /* check in cache */
Avantika Mathur7e028972006-12-06 20:41:33 -08002590 goal = ext4_ext_in_cache(inode, iblock, &newex);
2591 if (goal) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002592 if (goal == EXT4_EXT_CACHE_GAP) {
2593 if (!create) {
Amit Arora56055d32007-07-17 21:42:38 -04002594 /*
2595 * block isn't allocated yet and
2596 * user doesn't want to allocate it
2597 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002598 goto out2;
2599 }
2600 /* we should allocate requested block */
2601 } else if (goal == EXT4_EXT_CACHE_EXTENT) {
2602 /* block is already allocated */
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002603 newblock = iblock
2604 - le32_to_cpu(newex.ee_block)
2605 + ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002606 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05002607 allocated = ext4_ext_get_actual_len(&newex) -
Alex Tomasa86c6182006-10-11 01:21:03 -07002608 (iblock - le32_to_cpu(newex.ee_block));
2609 goto out;
2610 } else {
2611 BUG();
2612 }
2613 }
2614
2615 /* find extent for this block */
2616 path = ext4_ext_find_extent(inode, iblock, NULL);
2617 if (IS_ERR(path)) {
2618 err = PTR_ERR(path);
2619 path = NULL;
2620 goto out2;
2621 }
2622
2623 depth = ext_depth(inode);
2624
2625 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002626 * consistent leaf must not be empty;
2627 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07002628 * this is why assert can't be put in ext4_ext_find_extent()
2629 */
2630 BUG_ON(path[depth].p_ext == NULL && depth != 0);
Amit Arora56055d32007-07-17 21:42:38 -04002631 eh = path[depth].p_hdr;
Alex Tomasa86c6182006-10-11 01:21:03 -07002632
Avantika Mathur7e028972006-12-06 20:41:33 -08002633 ex = path[depth].p_ext;
2634 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002635 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002636 ext4_fsblk_t ee_start = ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002637 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07002638
2639 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07002640 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04002641 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07002642 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04002643 ee_len = ext4_ext_get_actual_len(ex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002644 /* if found extent covers block, simply return it */
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002645 if (iblock >= ee_block && iblock < ee_block + ee_len) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002646 newblock = iblock - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002647 /* number of remaining blocks in the extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07002648 allocated = ee_len - (iblock - ee_block);
Eric Sandeenbba90742008-01-28 23:58:27 -05002649 ext_debug("%u fit into %lu:%d -> %llu\n", iblock,
Alex Tomasa86c6182006-10-11 01:21:03 -07002650 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04002651
Amit Aroraa2df2a62007-07-17 21:42:41 -04002652 /* Do not put uninitialized extent in the cache */
Amit Arora56055d32007-07-17 21:42:38 -04002653 if (!ext4_ext_is_uninitialized(ex)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04002654 ext4_ext_put_in_cache(inode, ee_block,
2655 ee_len, ee_start,
2656 EXT4_EXT_CACHE_EXTENT);
Amit Arora56055d32007-07-17 21:42:38 -04002657 goto out;
2658 }
2659 if (create == EXT4_CREATE_UNINITIALIZED_EXT)
2660 goto out;
Aneesh Kumar K.Ve067ba02008-04-29 08:11:12 -04002661 if (!create) {
2662 /*
2663 * We have blocks reserved already. We
2664 * return allocated blocks so that delalloc
2665 * won't do block reservation for us. But
2666 * the buffer head will be unmapped so that
2667 * a read from the block returns 0s.
2668 */
2669 if (allocated > max_blocks)
2670 allocated = max_blocks;
Eric Sandeen953e6222008-07-11 19:27:31 -04002671 set_buffer_unwritten(bh_result);
Amit Arora56055d32007-07-17 21:42:38 -04002672 goto out2;
Aneesh Kumar K.Ve067ba02008-04-29 08:11:12 -04002673 }
Amit Arora56055d32007-07-17 21:42:38 -04002674
2675 ret = ext4_ext_convert_to_initialized(handle, inode,
2676 path, iblock,
2677 max_blocks);
Dmitry Monakhovdbf9d7d2008-01-28 23:58:27 -05002678 if (ret <= 0) {
2679 err = ret;
Amit Arora56055d32007-07-17 21:42:38 -04002680 goto out2;
Dmitry Monakhovdbf9d7d2008-01-28 23:58:27 -05002681 } else
Amit Arora56055d32007-07-17 21:42:38 -04002682 allocated = ret;
2683 goto outnew;
Alex Tomasa86c6182006-10-11 01:21:03 -07002684 }
2685 }
2686
2687 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002688 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07002689 * we couldn't try to create block if create flag is zero
2690 */
2691 if (!create) {
Amit Arora56055d32007-07-17 21:42:38 -04002692 /*
2693 * put just found gap into cache to speed up
2694 * subsequent requests
2695 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002696 ext4_ext_put_gap_in_cache(inode, path, iblock);
2697 goto out2;
2698 }
2699 /*
Andrew Morton63f57932006-10-11 01:21:24 -07002700 * Okay, we need to do block allocation. Lazily initialize the block
2701 * allocation info here if necessary.
2702 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002703 if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
2704 ext4_init_block_alloc_info(inode);
2705
Alex Tomasc9de5602008-01-29 00:19:52 -05002706 /* find neighbour allocated blocks */
2707 ar.lleft = iblock;
2708 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
2709 if (err)
2710 goto out2;
2711 ar.lright = iblock;
2712 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
2713 if (err)
2714 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04002715
Amit Arora749269f2007-07-18 09:02:56 -04002716 /*
2717 * See if request is beyond maximum number of blocks we can have in
2718 * a single extent. For an initialized extent this limit is
2719 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
2720 * EXT_UNINIT_MAX_LEN.
2721 */
2722 if (max_blocks > EXT_INIT_MAX_LEN &&
2723 create != EXT4_CREATE_UNINITIALIZED_EXT)
2724 max_blocks = EXT_INIT_MAX_LEN;
2725 else if (max_blocks > EXT_UNINIT_MAX_LEN &&
2726 create == EXT4_CREATE_UNINITIALIZED_EXT)
2727 max_blocks = EXT_UNINIT_MAX_LEN;
2728
Amit Arora25d14f92007-05-24 13:04:13 -04002729 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
2730 newex.ee_block = cpu_to_le32(iblock);
2731 newex.ee_len = cpu_to_le16(max_blocks);
2732 err = ext4_ext_check_overlap(inode, &newex, path);
2733 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05002734 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04002735 else
2736 allocated = max_blocks;
Alex Tomasc9de5602008-01-29 00:19:52 -05002737
2738 /* allocate new block */
2739 ar.inode = inode;
2740 ar.goal = ext4_ext_find_goal(inode, path, iblock);
2741 ar.logical = iblock;
2742 ar.len = allocated;
2743 if (S_ISREG(inode->i_mode))
2744 ar.flags = EXT4_MB_HINT_DATA;
2745 else
2746 /* disable in-core preallocation for non-regular files */
2747 ar.flags = 0;
2748 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07002749 if (!newblock)
2750 goto out2;
Mingming Cao2ae02102006-10-11 01:21:11 -07002751 ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002752 goal, newblock, allocated);
2753
2754 /* try to insert new extent into found leaf and return */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002755 ext4_ext_store_pblock(&newex, newblock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002756 newex.ee_len = cpu_to_le16(ar.len);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002757 if (create == EXT4_CREATE_UNINITIALIZED_EXT) /* Mark uninitialized */
2758 ext4_ext_mark_uninitialized(&newex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002759 err = ext4_ext_insert_extent(handle, inode, path, &newex);
Alex Tomas315054f2007-05-24 13:04:25 -04002760 if (err) {
2761 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05002762 /* not a good idea to call discard here directly,
2763 * but otherwise we'd need to call it every free() */
2764 ext4_mb_discard_inode_preallocations(inode);
Alex Tomas315054f2007-05-24 13:04:25 -04002765 ext4_free_blocks(handle, inode, ext_pblock(&newex),
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05002766 ext4_ext_get_actual_len(&newex), 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002767 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04002768 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002769
Alex Tomasa86c6182006-10-11 01:21:03 -07002770 /* previous routine could use block we allocated */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002771 newblock = ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05002772 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora56055d32007-07-17 21:42:38 -04002773outnew:
Mingming Cao61628a32008-07-11 19:27:31 -04002774 if (extend_disksize) {
2775 disksize = ((loff_t) iblock + ar.len) << inode->i_blkbits;
2776 if (disksize > i_size_read(inode))
2777 disksize = i_size_read(inode);
2778 if (disksize > EXT4_I(inode)->i_disksize)
2779 EXT4_I(inode)->i_disksize = disksize;
2780 }
Aneesh Kumar K.Va379cd12008-07-11 19:27:31 -04002781
Eric Sandeen953e6222008-07-11 19:27:31 -04002782 set_buffer_new(bh_result);
Alex Tomasa86c6182006-10-11 01:21:03 -07002783
Amit Aroraa2df2a62007-07-17 21:42:41 -04002784 /* Cache only when it is _not_ an uninitialized extent */
2785 if (create != EXT4_CREATE_UNINITIALIZED_EXT)
2786 ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
2787 EXT4_EXT_CACHE_EXTENT);
Alex Tomasa86c6182006-10-11 01:21:03 -07002788out:
2789 if (allocated > max_blocks)
2790 allocated = max_blocks;
2791 ext4_ext_show_leaf(inode, path);
Eric Sandeen953e6222008-07-11 19:27:31 -04002792 set_buffer_mapped(bh_result);
Alex Tomasa86c6182006-10-11 01:21:03 -07002793 bh_result->b_bdev = inode->i_sb->s_bdev;
2794 bh_result->b_blocknr = newblock;
2795out2:
2796 if (path) {
2797 ext4_ext_drop_refs(path);
2798 kfree(path);
2799 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002800 return err ? err : allocated;
2801}
2802
Jan Karacf108bc2008-07-11 19:27:31 -04002803void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07002804{
2805 struct address_space *mapping = inode->i_mapping;
2806 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002807 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002808 handle_t *handle;
2809 int err = 0;
2810
2811 /*
2812 * probably first extent we're gonna free will be last in block
2813 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04002814 err = ext4_writepage_trans_blocks(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002815 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04002816 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07002817 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07002818
Jan Karacf108bc2008-07-11 19:27:31 -04002819 if (inode->i_size & (sb->s_blocksize - 1))
2820 ext4_block_truncate_page(handle, mapping, inode->i_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07002821
Jan Kara9ddfc3d2008-07-11 19:27:31 -04002822 if (ext4_orphan_add(handle, inode))
2823 goto out_stop;
2824
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05002825 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07002826 ext4_ext_invalidate_cache(inode);
2827
Theodore Ts'o88aa3cf2008-08-16 07:57:35 -04002828 ext4_discard_reservation(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05002829
Alex Tomasa86c6182006-10-11 01:21:03 -07002830 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002831 * TODO: optimization is possible here.
2832 * Probably we need not scan at all,
2833 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07002834 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002835
2836 /* we have to know where to truncate from in crash case */
2837 EXT4_I(inode)->i_disksize = inode->i_size;
2838 ext4_mark_inode_dirty(handle, inode);
2839
2840 last_block = (inode->i_size + sb->s_blocksize - 1)
2841 >> EXT4_BLOCK_SIZE_BITS(sb);
2842 err = ext4_ext_remove_space(inode, last_block);
2843
2844 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04002845 * transaction synchronous.
2846 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002847 if (IS_SYNC(inode))
2848 handle->h_sync = 1;
2849
2850out_stop:
Jan Kara9ddfc3d2008-07-11 19:27:31 -04002851 up_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07002852 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002853 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07002854 * then we need to clear up the orphan record which we created above.
2855 * However, if this was a real unlink then we were called by
2856 * ext4_delete_inode(), and we allow that function to clean up the
2857 * orphan info for us.
2858 */
2859 if (inode->i_nlink)
2860 ext4_orphan_del(handle, inode);
2861
Solofo Ramangalahyef737722008-04-29 22:00:41 -04002862 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
2863 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002864 ext4_journal_stop(handle);
2865}
2866
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04002867static void ext4_falloc_update_inode(struct inode *inode,
2868 int mode, loff_t new_size, int update_ctime)
2869{
2870 struct timespec now;
2871
2872 if (update_ctime) {
2873 now = current_fs_time(inode->i_sb);
2874 if (!timespec_equal(&inode->i_ctime, &now))
2875 inode->i_ctime = now;
2876 }
2877 /*
2878 * Update only when preallocation was requested beyond
2879 * the file size.
2880 */
2881 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
2882 new_size > i_size_read(inode)) {
2883 i_size_write(inode, new_size);
2884 EXT4_I(inode)->i_disksize = new_size;
2885 }
2886
2887}
2888
Amit Aroraa2df2a62007-07-17 21:42:41 -04002889/*
2890 * preallocate space for a file. This implements ext4's fallocate inode
2891 * operation, which gets called from sys_fallocate system call.
2892 * For block-mapped files, posix_fallocate should fall back to the method
2893 * of writing zeroes to the required new blocks (the same behavior which is
2894 * expected for file systems which do not support fallocate() system call).
2895 */
2896long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
2897{
2898 handle_t *handle;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002899 ext4_lblk_t block;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04002900 loff_t new_size;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002901 unsigned long max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002902 int ret = 0;
2903 int ret2 = 0;
2904 int retries = 0;
2905 struct buffer_head map_bh;
2906 unsigned int credits, blkbits = inode->i_blkbits;
2907
2908 /*
2909 * currently supporting (pre)allocate mode for extent-based
2910 * files _only_
2911 */
2912 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
2913 return -EOPNOTSUPP;
2914
2915 /* preallocation to directories is currently not supported */
2916 if (S_ISDIR(inode->i_mode))
2917 return -ENODEV;
2918
2919 block = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04002920 /*
2921 * We can't just convert len to max_blocks because
2922 * If blocksize = 4096 offset = 3072 and len = 2048
2923 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04002924 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04002925 - block;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002926 /*
Mingming Caof3bd1f32008-08-19 22:16:03 -04002927 * credits to insert 1 extent into extent tree
Amit Aroraa2df2a62007-07-17 21:42:41 -04002928 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04002929 credits = ext4_chunk_trans_blocks(inode, max_blocks);
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05002930 mutex_lock(&inode->i_mutex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002931retry:
2932 while (ret >= 0 && ret < max_blocks) {
2933 block = block + ret;
2934 max_blocks = max_blocks - ret;
2935 handle = ext4_journal_start(inode, credits);
2936 if (IS_ERR(handle)) {
2937 ret = PTR_ERR(handle);
2938 break;
2939 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05002940 ret = ext4_get_blocks_wrap(handle, inode, block,
Amit Aroraa2df2a62007-07-17 21:42:41 -04002941 max_blocks, &map_bh,
Mingming Caod2a17632008-07-14 17:52:37 -04002942 EXT4_CREATE_UNINITIALIZED_EXT, 0, 0);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05002943 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05002944#ifdef EXT4FS_DEBUG
2945 WARN_ON(ret <= 0);
2946 printk(KERN_ERR "%s: ext4_ext_get_blocks "
2947 "returned error inode#%lu, block=%u, "
2948 "max_blocks=%lu", __func__,
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05002949 inode->i_ino, block, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05002950#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04002951 ext4_mark_inode_dirty(handle, inode);
2952 ret2 = ext4_journal_stop(handle);
2953 break;
2954 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04002955 if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
2956 blkbits) >> blkbits))
2957 new_size = offset + len;
2958 else
2959 new_size = (block + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002960
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04002961 ext4_falloc_update_inode(inode, mode, new_size,
2962 buffer_new(&map_bh));
Amit Aroraa2df2a62007-07-17 21:42:41 -04002963 ext4_mark_inode_dirty(handle, inode);
2964 ret2 = ext4_journal_stop(handle);
2965 if (ret2)
2966 break;
2967 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04002968 if (ret == -ENOSPC &&
2969 ext4_should_retry_alloc(inode->i_sb, &retries)) {
2970 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002971 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002972 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05002973 mutex_unlock(&inode->i_mutex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002974 return ret > 0 ? ret2 : ret;
2975}