blob: b24d3c53f20cd20407d5d5373c79b725d308cc45 [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)) {
443 printk("k=%d, ix=0x%p, first=0x%p\n", k,
444 ix, EXT_FIRST_INDEX(eh));
445 printk("%u <= %u\n",
446 le32_to_cpu(ix->ei_block),
447 le32_to_cpu(ix[-1].ei_block));
448 }
449 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400450 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700451 if (block < le32_to_cpu(ix->ei_block))
452 break;
453 chix = ix;
454 }
455 BUG_ON(chix != path->p_idx);
456 }
457#endif
458
459}
460
461/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700462 * ext4_ext_binsearch:
463 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400464 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700465 */
466static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500467ext4_ext_binsearch(struct inode *inode,
468 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700469{
470 struct ext4_extent_header *eh = path->p_hdr;
471 struct ext4_extent *r, *l, *m;
472
Alex Tomasa86c6182006-10-11 01:21:03 -0700473 if (eh->eh_entries == 0) {
474 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700475 * this leaf is empty:
476 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700477 */
478 return;
479 }
480
Eric Sandeenbba90742008-01-28 23:58:27 -0500481 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700482
483 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400484 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700485
486 while (l <= r) {
487 m = l + (r - l) / 2;
488 if (block < le32_to_cpu(m->ee_block))
489 r = m - 1;
490 else
491 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400492 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
493 m, le32_to_cpu(m->ee_block),
494 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700495 }
496
497 path->p_ext = l - 1;
Mingming Cao2ae02102006-10-11 01:21:11 -0700498 ext_debug(" -> %d:%llu:%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400499 le32_to_cpu(path->p_ext->ee_block),
500 ext_pblock(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400501 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700502
503#ifdef CHECK_BINSEARCH
504 {
505 struct ext4_extent *chex, *ex;
506 int k;
507
508 chex = ex = EXT_FIRST_EXTENT(eh);
509 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
510 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400511 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700512 if (block < le32_to_cpu(ex->ee_block))
513 break;
514 chex = ex;
515 }
516 BUG_ON(chex != path->p_ext);
517 }
518#endif
519
520}
521
522int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
523{
524 struct ext4_extent_header *eh;
525
526 eh = ext_inode_hdr(inode);
527 eh->eh_depth = 0;
528 eh->eh_entries = 0;
529 eh->eh_magic = EXT4_EXT_MAGIC;
530 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
531 ext4_mark_inode_dirty(handle, inode);
532 ext4_ext_invalidate_cache(inode);
533 return 0;
534}
535
536struct ext4_ext_path *
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500537ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
538 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700539{
540 struct ext4_extent_header *eh;
541 struct buffer_head *bh;
542 short int depth, i, ppos = 0, alloc = 0;
543
544 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400545 depth = ext_depth(inode);
546 if (ext4_ext_check_header(inode, eh, depth))
Alex Tomasa86c6182006-10-11 01:21:03 -0700547 return ERR_PTR(-EIO);
548
Alex Tomasa86c6182006-10-11 01:21:03 -0700549
550 /* account possible depth increase */
551 if (!path) {
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800552 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700553 GFP_NOFS);
554 if (!path)
555 return ERR_PTR(-ENOMEM);
556 alloc = 1;
557 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700558 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400559 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700560
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400561 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700562 /* walk through the tree */
563 while (i) {
564 ext_debug("depth %d: num %d, max %d\n",
565 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400566
Alex Tomasa86c6182006-10-11 01:21:03 -0700567 ext4_ext_binsearch_idx(inode, path + ppos, block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700568 path[ppos].p_block = idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700569 path[ppos].p_depth = i;
570 path[ppos].p_ext = NULL;
571
572 bh = sb_bread(inode->i_sb, path[ppos].p_block);
573 if (!bh)
574 goto err;
575
576 eh = ext_block_hdr(bh);
577 ppos++;
578 BUG_ON(ppos > depth);
579 path[ppos].p_bh = bh;
580 path[ppos].p_hdr = eh;
581 i--;
582
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400583 if (ext4_ext_check_header(inode, eh, i))
Alex Tomasa86c6182006-10-11 01:21:03 -0700584 goto err;
585 }
586
587 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700588 path[ppos].p_ext = NULL;
589 path[ppos].p_idx = NULL;
590
Alex Tomasa86c6182006-10-11 01:21:03 -0700591 /* find extent */
592 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400593 /* if not an empty leaf */
594 if (path[ppos].p_ext)
595 path[ppos].p_block = ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700596
597 ext4_ext_show_path(inode, path);
598
599 return path;
600
601err:
602 ext4_ext_drop_refs(path);
603 if (alloc)
604 kfree(path);
605 return ERR_PTR(-EIO);
606}
607
608/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700609 * ext4_ext_insert_index:
610 * insert new index [@logical;@ptr] into the block at @curp;
611 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700612 */
613static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
614 struct ext4_ext_path *curp,
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700615 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700616{
617 struct ext4_extent_idx *ix;
618 int len, err;
619
Avantika Mathur7e028972006-12-06 20:41:33 -0800620 err = ext4_ext_get_access(handle, inode, curp);
621 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700622 return err;
623
624 BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
625 len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
626 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
627 /* insert after */
628 if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
629 len = (len - 1) * sizeof(struct ext4_extent_idx);
630 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400631 ext_debug("insert new index %d after: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700632 "move %d from 0x%p to 0x%p\n",
633 logical, ptr, len,
634 (curp->p_idx + 1), (curp->p_idx + 2));
635 memmove(curp->p_idx + 2, curp->p_idx + 1, len);
636 }
637 ix = curp->p_idx + 1;
638 } else {
639 /* insert before */
640 len = len * sizeof(struct ext4_extent_idx);
641 len = len < 0 ? 0 : len;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400642 ext_debug("insert new index %d before: %llu. "
Alex Tomasa86c6182006-10-11 01:21:03 -0700643 "move %d from 0x%p to 0x%p\n",
644 logical, ptr, len,
645 curp->p_idx, (curp->p_idx + 1));
646 memmove(curp->p_idx + 1, curp->p_idx, len);
647 ix = curp->p_idx;
648 }
649
650 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700651 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400652 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700653
654 BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400655 > le16_to_cpu(curp->p_hdr->eh_max));
Alex Tomasa86c6182006-10-11 01:21:03 -0700656 BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
657
658 err = ext4_ext_dirty(handle, inode, curp);
659 ext4_std_error(inode->i_sb, err);
660
661 return err;
662}
663
664/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700665 * ext4_ext_split:
666 * inserts new subtree into the path, using free index entry
667 * at depth @at:
668 * - allocates all needed blocks (new leaf and all intermediate index blocks)
669 * - makes decision where to split
670 * - moves remaining extents and index entries (right to the split point)
671 * into the newly allocated blocks
672 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700673 */
674static int ext4_ext_split(handle_t *handle, struct inode *inode,
675 struct ext4_ext_path *path,
676 struct ext4_extent *newext, int at)
677{
678 struct buffer_head *bh = NULL;
679 int depth = ext_depth(inode);
680 struct ext4_extent_header *neh;
681 struct ext4_extent_idx *fidx;
682 struct ext4_extent *ex;
683 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700684 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700685 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700686 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700687 int err = 0;
688
689 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700690 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700691
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700692 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700693 * border from split point */
694 BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
695 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
696 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700697 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700698 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400699 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700700 } else {
701 border = newext->ee_block;
702 ext_debug("leaf will be added."
703 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400704 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700705 }
706
707 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700708 * If error occurs, then we break processing
709 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700710 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700711 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700712 */
713
714 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700715 * Get array to track all allocated blocks.
716 * We need this to handle errors and free blocks
717 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700718 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800719 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700720 if (!ablocks)
721 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700722
723 /* allocate all needed blocks */
724 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
725 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400726 newblock = ext4_ext_new_meta_block(handle, inode, path,
727 newext, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700728 if (newblock == 0)
729 goto cleanup;
730 ablocks[a] = newblock;
731 }
732
733 /* initialize new leaf */
734 newblock = ablocks[--a];
735 BUG_ON(newblock == 0);
736 bh = sb_getblk(inode->i_sb, newblock);
737 if (!bh) {
738 err = -EIO;
739 goto cleanup;
740 }
741 lock_buffer(bh);
742
Avantika Mathur7e028972006-12-06 20:41:33 -0800743 err = ext4_journal_get_create_access(handle, bh);
744 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700745 goto cleanup;
746
747 neh = ext_block_hdr(bh);
748 neh->eh_entries = 0;
749 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
750 neh->eh_magic = EXT4_EXT_MAGIC;
751 neh->eh_depth = 0;
752 ex = EXT_FIRST_EXTENT(neh);
753
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700754 /* move remainder of path[depth] to the new leaf */
Alex Tomasa86c6182006-10-11 01:21:03 -0700755 BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
756 /* start copy from next extent */
757 /* TODO: we could do it by single memmove */
758 m = 0;
759 path[depth].p_ext++;
760 while (path[depth].p_ext <=
761 EXT_MAX_EXTENT(path[depth].p_hdr)) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700762 ext_debug("move %d:%llu:%d in new leaf %llu\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400763 le32_to_cpu(path[depth].p_ext->ee_block),
764 ext_pblock(path[depth].p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400765 ext4_ext_get_actual_len(path[depth].p_ext),
Alex Tomasa86c6182006-10-11 01:21:03 -0700766 newblock);
767 /*memmove(ex++, path[depth].p_ext++,
768 sizeof(struct ext4_extent));
769 neh->eh_entries++;*/
770 path[depth].p_ext++;
771 m++;
772 }
773 if (m) {
774 memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400775 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700776 }
777
778 set_buffer_uptodate(bh);
779 unlock_buffer(bh);
780
Avantika Mathur7e028972006-12-06 20:41:33 -0800781 err = ext4_journal_dirty_metadata(handle, bh);
782 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700783 goto cleanup;
784 brelse(bh);
785 bh = NULL;
786
787 /* correct old leaf */
788 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800789 err = ext4_ext_get_access(handle, inode, path + depth);
790 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700791 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400792 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800793 err = ext4_ext_dirty(handle, inode, path + depth);
794 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700795 goto cleanup;
796
797 }
798
799 /* create intermediate indexes */
800 k = depth - at - 1;
801 BUG_ON(k < 0);
802 if (k)
803 ext_debug("create %d intermediate indices\n", k);
804 /* insert new index into current index block */
805 /* current depth stored in i var */
806 i = depth - 1;
807 while (k--) {
808 oldblock = newblock;
809 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -0500810 bh = sb_getblk(inode->i_sb, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700811 if (!bh) {
812 err = -EIO;
813 goto cleanup;
814 }
815 lock_buffer(bh);
816
Avantika Mathur7e028972006-12-06 20:41:33 -0800817 err = ext4_journal_get_create_access(handle, bh);
818 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700819 goto cleanup;
820
821 neh = ext_block_hdr(bh);
822 neh->eh_entries = cpu_to_le16(1);
823 neh->eh_magic = EXT4_EXT_MAGIC;
824 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
825 neh->eh_depth = cpu_to_le16(depth - i);
826 fidx = EXT_FIRST_INDEX(neh);
827 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700828 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700829
Eric Sandeenbba90742008-01-28 23:58:27 -0500830 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
831 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700832 /* copy indexes */
833 m = 0;
834 path[i].p_idx++;
835
836 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
837 EXT_MAX_INDEX(path[i].p_hdr));
838 BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
839 EXT_LAST_INDEX(path[i].p_hdr));
840 while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400841 ext_debug("%d: move %d:%llu in new index %llu\n", i,
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400842 le32_to_cpu(path[i].p_idx->ei_block),
843 idx_pblock(path[i].p_idx),
844 newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700845 /*memmove(++fidx, path[i].p_idx++,
846 sizeof(struct ext4_extent_idx));
847 neh->eh_entries++;
848 BUG_ON(neh->eh_entries > neh->eh_max);*/
849 path[i].p_idx++;
850 m++;
851 }
852 if (m) {
853 memmove(++fidx, path[i].p_idx - m,
854 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400855 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700856 }
857 set_buffer_uptodate(bh);
858 unlock_buffer(bh);
859
Avantika Mathur7e028972006-12-06 20:41:33 -0800860 err = ext4_journal_dirty_metadata(handle, bh);
861 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700862 goto cleanup;
863 brelse(bh);
864 bh = NULL;
865
866 /* correct old index */
867 if (m) {
868 err = ext4_ext_get_access(handle, inode, path + i);
869 if (err)
870 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400871 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700872 err = ext4_ext_dirty(handle, inode, path + i);
873 if (err)
874 goto cleanup;
875 }
876
877 i--;
878 }
879
880 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -0700881 err = ext4_ext_insert_index(handle, inode, path + at,
882 le32_to_cpu(border), newblock);
883
884cleanup:
885 if (bh) {
886 if (buffer_locked(bh))
887 unlock_buffer(bh);
888 brelse(bh);
889 }
890
891 if (err) {
892 /* free all allocated blocks in error case */
893 for (i = 0; i < depth; i++) {
894 if (!ablocks[i])
895 continue;
Alex Tomasc9de5602008-01-29 00:19:52 -0500896 ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700897 }
898 }
899 kfree(ablocks);
900
901 return err;
902}
903
904/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700905 * ext4_ext_grow_indepth:
906 * implements tree growing procedure:
907 * - allocates new block
908 * - moves top-level data (index block or leaf) into the new block
909 * - initializes new top-level, creating index that points to the
910 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -0700911 */
912static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
913 struct ext4_ext_path *path,
914 struct ext4_extent *newext)
915{
916 struct ext4_ext_path *curp = path;
917 struct ext4_extent_header *neh;
918 struct ext4_extent_idx *fidx;
919 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700920 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700921 int err = 0;
922
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400923 newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700924 if (newblock == 0)
925 return err;
926
927 bh = sb_getblk(inode->i_sb, newblock);
928 if (!bh) {
929 err = -EIO;
930 ext4_std_error(inode->i_sb, err);
931 return err;
932 }
933 lock_buffer(bh);
934
Avantika Mathur7e028972006-12-06 20:41:33 -0800935 err = ext4_journal_get_create_access(handle, bh);
936 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -0700937 unlock_buffer(bh);
938 goto out;
939 }
940
941 /* move top-level index/leaf into new block */
942 memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
943
944 /* set size of new block */
945 neh = ext_block_hdr(bh);
946 /* old root could have indexes or leaves
947 * so calculate e_max right way */
948 if (ext_depth(inode))
949 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
950 else
951 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
952 neh->eh_magic = EXT4_EXT_MAGIC;
953 set_buffer_uptodate(bh);
954 unlock_buffer(bh);
955
Avantika Mathur7e028972006-12-06 20:41:33 -0800956 err = ext4_journal_dirty_metadata(handle, bh);
957 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700958 goto out;
959
960 /* create index in new top-level index: num,max,pointer */
Avantika Mathur7e028972006-12-06 20:41:33 -0800961 err = ext4_ext_get_access(handle, inode, curp);
962 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700963 goto out;
964
965 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
966 curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
967 curp->p_hdr->eh_entries = cpu_to_le16(1);
968 curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400969
970 if (path[0].p_hdr->eh_depth)
971 curp->p_idx->ei_block =
972 EXT_FIRST_INDEX(path[0].p_hdr)->ei_block;
973 else
974 curp->p_idx->ei_block =
975 EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700976 ext4_idx_store_pblock(curp->p_idx, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700977
978 neh = ext_inode_hdr(inode);
979 fidx = EXT_FIRST_INDEX(neh);
Mingming Cao2ae02102006-10-11 01:21:11 -0700980 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700981 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700982 le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700983
984 neh->eh_depth = cpu_to_le16(path->p_depth + 1);
985 err = ext4_ext_dirty(handle, inode, curp);
986out:
987 brelse(bh);
988
989 return err;
990}
991
992/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700993 * ext4_ext_create_new_leaf:
994 * finds empty index and adds new leaf.
995 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -0700996 */
997static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
998 struct ext4_ext_path *path,
999 struct ext4_extent *newext)
1000{
1001 struct ext4_ext_path *curp;
1002 int depth, i, err = 0;
1003
1004repeat:
1005 i = depth = ext_depth(inode);
1006
1007 /* walk up to the tree and look for free index entry */
1008 curp = path + depth;
1009 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1010 i--;
1011 curp--;
1012 }
1013
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001014 /* we use already allocated block for index block,
1015 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001016 if (EXT_HAS_FREE_INDEX(curp)) {
1017 /* if we found index with free entry, then use that
1018 * entry: create all needed subtree and add new leaf */
1019 err = ext4_ext_split(handle, inode, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001020 if (err)
1021 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001022
1023 /* refill path */
1024 ext4_ext_drop_refs(path);
1025 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001026 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1027 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001028 if (IS_ERR(path))
1029 err = PTR_ERR(path);
1030 } else {
1031 /* tree is full, time to grow in depth */
1032 err = ext4_ext_grow_indepth(handle, inode, path, newext);
1033 if (err)
1034 goto out;
1035
1036 /* refill path */
1037 ext4_ext_drop_refs(path);
1038 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001039 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1040 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001041 if (IS_ERR(path)) {
1042 err = PTR_ERR(path);
1043 goto out;
1044 }
1045
1046 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001047 * only first (depth 0 -> 1) produces free space;
1048 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001049 */
1050 depth = ext_depth(inode);
1051 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001052 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001053 goto repeat;
1054 }
1055 }
1056
1057out:
1058 return err;
1059}
1060
1061/*
Alex Tomas1988b512008-01-28 23:58:27 -05001062 * search the closest allocated block to the left for *logical
1063 * and returns it at @logical + it's physical address at @phys
1064 * if *logical is the smallest allocated block, the function
1065 * returns 0 at @phys
1066 * return value contains 0 (success) or error code
1067 */
1068int
1069ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path,
1070 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1071{
1072 struct ext4_extent_idx *ix;
1073 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001074 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001075
1076 BUG_ON(path == NULL);
1077 depth = path->p_depth;
1078 *phys = 0;
1079
1080 if (depth == 0 && path->p_ext == NULL)
1081 return 0;
1082
1083 /* usually extent in the path covers blocks smaller
1084 * then *logical, but it can be that extent is the
1085 * first one in the file */
1086
1087 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001088 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001089 if (*logical < le32_to_cpu(ex->ee_block)) {
1090 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1091 while (--depth >= 0) {
1092 ix = path[depth].p_idx;
1093 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1094 }
1095 return 0;
1096 }
1097
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001098 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
Alex Tomas1988b512008-01-28 23:58:27 -05001099
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001100 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
1101 *phys = ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001102 return 0;
1103}
1104
1105/*
1106 * search the closest allocated block to the right for *logical
1107 * and returns it at @logical + it's physical address at @phys
1108 * if *logical is the smallest allocated block, the function
1109 * returns 0 at @phys
1110 * return value contains 0 (success) or error code
1111 */
1112int
1113ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
1114 ext4_lblk_t *logical, ext4_fsblk_t *phys)
1115{
1116 struct buffer_head *bh = NULL;
1117 struct ext4_extent_header *eh;
1118 struct ext4_extent_idx *ix;
1119 struct ext4_extent *ex;
1120 ext4_fsblk_t block;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001121 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001122
1123 BUG_ON(path == NULL);
1124 depth = path->p_depth;
1125 *phys = 0;
1126
1127 if (depth == 0 && path->p_ext == NULL)
1128 return 0;
1129
1130 /* usually extent in the path covers blocks smaller
1131 * then *logical, but it can be that extent is the
1132 * first one in the file */
1133
1134 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001135 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001136 if (*logical < le32_to_cpu(ex->ee_block)) {
1137 BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
1138 while (--depth >= 0) {
1139 ix = path[depth].p_idx;
1140 BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
1141 }
1142 *logical = le32_to_cpu(ex->ee_block);
1143 *phys = ext_pblock(ex);
1144 return 0;
1145 }
1146
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001147 BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
Alex Tomas1988b512008-01-28 23:58:27 -05001148
1149 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1150 /* next allocated block in this leaf */
1151 ex++;
1152 *logical = le32_to_cpu(ex->ee_block);
1153 *phys = ext_pblock(ex);
1154 return 0;
1155 }
1156
1157 /* go up and search for index to the right */
1158 while (--depth >= 0) {
1159 ix = path[depth].p_idx;
1160 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
1161 break;
1162 }
1163
1164 if (depth < 0) {
1165 /* we've gone up to the root and
1166 * found no index to the right */
1167 return 0;
1168 }
1169
1170 /* we've found index to the right, let's
1171 * follow it and find the closest allocated
1172 * block to the right */
1173 ix++;
1174 block = idx_pblock(ix);
1175 while (++depth < path->p_depth) {
1176 bh = sb_bread(inode->i_sb, block);
1177 if (bh == NULL)
1178 return -EIO;
1179 eh = ext_block_hdr(bh);
1180 if (ext4_ext_check_header(inode, eh, depth)) {
1181 put_bh(bh);
1182 return -EIO;
1183 }
1184 ix = EXT_FIRST_INDEX(eh);
1185 block = idx_pblock(ix);
1186 put_bh(bh);
1187 }
1188
1189 bh = sb_bread(inode->i_sb, block);
1190 if (bh == NULL)
1191 return -EIO;
1192 eh = ext_block_hdr(bh);
1193 if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) {
1194 put_bh(bh);
1195 return -EIO;
1196 }
1197 ex = EXT_FIRST_EXTENT(eh);
1198 *logical = le32_to_cpu(ex->ee_block);
1199 *phys = ext_pblock(ex);
1200 put_bh(bh);
1201 return 0;
1202
1203}
1204
1205/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001206 * ext4_ext_next_allocated_block:
1207 * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
1208 * NOTE: it considers block number from index entry as
1209 * allocated block. Thus, index entries have to be consistent
1210 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001211 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001212static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001213ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1214{
1215 int depth;
1216
1217 BUG_ON(path == NULL);
1218 depth = path->p_depth;
1219
1220 if (depth == 0 && path->p_ext == NULL)
1221 return EXT_MAX_BLOCK;
1222
1223 while (depth >= 0) {
1224 if (depth == path->p_depth) {
1225 /* leaf */
1226 if (path[depth].p_ext !=
1227 EXT_LAST_EXTENT(path[depth].p_hdr))
1228 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1229 } else {
1230 /* index */
1231 if (path[depth].p_idx !=
1232 EXT_LAST_INDEX(path[depth].p_hdr))
1233 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1234 }
1235 depth--;
1236 }
1237
1238 return EXT_MAX_BLOCK;
1239}
1240
1241/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001242 * ext4_ext_next_leaf_block:
Alex Tomasa86c6182006-10-11 01:21:03 -07001243 * returns first allocated block from next leaf or EXT_MAX_BLOCK
1244 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001245static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
Andrew Morton63f57932006-10-11 01:21:24 -07001246 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001247{
1248 int depth;
1249
1250 BUG_ON(path == NULL);
1251 depth = path->p_depth;
1252
1253 /* zero-tree has no leaf blocks at all */
1254 if (depth == 0)
1255 return EXT_MAX_BLOCK;
1256
1257 /* go to index block */
1258 depth--;
1259
1260 while (depth >= 0) {
1261 if (path[depth].p_idx !=
1262 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001263 return (ext4_lblk_t)
1264 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001265 depth--;
1266 }
1267
1268 return EXT_MAX_BLOCK;
1269}
1270
1271/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001272 * ext4_ext_correct_indexes:
1273 * if leaf gets modified and modified extent is first in the leaf,
1274 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001275 * TODO: do we need to correct tree in all cases?
1276 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001277static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001278 struct ext4_ext_path *path)
1279{
1280 struct ext4_extent_header *eh;
1281 int depth = ext_depth(inode);
1282 struct ext4_extent *ex;
1283 __le32 border;
1284 int k, err = 0;
1285
1286 eh = path[depth].p_hdr;
1287 ex = path[depth].p_ext;
1288 BUG_ON(ex == NULL);
1289 BUG_ON(eh == NULL);
1290
1291 if (depth == 0) {
1292 /* there is no tree at all */
1293 return 0;
1294 }
1295
1296 if (ex != EXT_FIRST_EXTENT(eh)) {
1297 /* we correct tree if first leaf got modified only */
1298 return 0;
1299 }
1300
1301 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001302 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001303 */
1304 k = depth - 1;
1305 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001306 err = ext4_ext_get_access(handle, inode, path + k);
1307 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001308 return err;
1309 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001310 err = ext4_ext_dirty(handle, inode, path + k);
1311 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001312 return err;
1313
1314 while (k--) {
1315 /* change all left-side indexes */
1316 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1317 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001318 err = ext4_ext_get_access(handle, inode, path + k);
1319 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001320 break;
1321 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001322 err = ext4_ext_dirty(handle, inode, path + k);
1323 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001324 break;
1325 }
1326
1327 return err;
1328}
1329
Avantika Mathur09b88252006-12-06 20:41:36 -08001330static int
Alex Tomasa86c6182006-10-11 01:21:03 -07001331ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1332 struct ext4_extent *ex2)
1333{
Amit Arora749269f2007-07-18 09:02:56 -04001334 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001335
1336 /*
1337 * Make sure that either both extents are uninitialized, or
1338 * both are _not_.
1339 */
1340 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1341 return 0;
1342
Amit Arora749269f2007-07-18 09:02:56 -04001343 if (ext4_ext_is_uninitialized(ex1))
1344 max_len = EXT_UNINIT_MAX_LEN;
1345 else
1346 max_len = EXT_INIT_MAX_LEN;
1347
Amit Aroraa2df2a62007-07-17 21:42:41 -04001348 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1349 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1350
1351 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001352 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001353 return 0;
1354
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001355 /*
1356 * To allow future support for preallocated extents to be added
1357 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001358 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001359 */
Amit Arora749269f2007-07-18 09:02:56 -04001360 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001361 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001362#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001363 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001364 return 0;
1365#endif
1366
Amit Aroraa2df2a62007-07-17 21:42:41 -04001367 if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001368 return 1;
1369 return 0;
1370}
1371
1372/*
Amit Arora56055d32007-07-17 21:42:38 -04001373 * This function tries to merge the "ex" extent to the next extent in the tree.
1374 * It always tries to merge towards right. If you want to merge towards
1375 * left, pass "ex - 1" as argument instead of "ex".
1376 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1377 * 1 if they got merged.
1378 */
1379int ext4_ext_try_to_merge(struct inode *inode,
1380 struct ext4_ext_path *path,
1381 struct ext4_extent *ex)
1382{
1383 struct ext4_extent_header *eh;
1384 unsigned int depth, len;
1385 int merge_done = 0;
1386 int uninitialized = 0;
1387
1388 depth = ext_depth(inode);
1389 BUG_ON(path[depth].p_hdr == NULL);
1390 eh = path[depth].p_hdr;
1391
1392 while (ex < EXT_LAST_EXTENT(eh)) {
1393 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1394 break;
1395 /* merge with next extent! */
1396 if (ext4_ext_is_uninitialized(ex))
1397 uninitialized = 1;
1398 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1399 + ext4_ext_get_actual_len(ex + 1));
1400 if (uninitialized)
1401 ext4_ext_mark_uninitialized(ex);
1402
1403 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1404 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1405 * sizeof(struct ext4_extent);
1406 memmove(ex + 1, ex + 2, len);
1407 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001408 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001409 merge_done = 1;
1410 WARN_ON(eh->eh_entries == 0);
1411 if (!eh->eh_entries)
1412 ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
1413 "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
1414 }
1415
1416 return merge_done;
1417}
1418
1419/*
Amit Arora25d14f92007-05-24 13:04:13 -04001420 * check if a portion of the "newext" extent overlaps with an
1421 * existing extent.
1422 *
1423 * If there is an overlap discovered, it updates the length of the newext
1424 * such that there will be no overlap, and then returns 1.
1425 * If there is no overlap found, it returns 0.
1426 */
1427unsigned int ext4_ext_check_overlap(struct inode *inode,
1428 struct ext4_extent *newext,
1429 struct ext4_ext_path *path)
1430{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001431 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001432 unsigned int depth, len1;
1433 unsigned int ret = 0;
1434
1435 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001436 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001437 depth = ext_depth(inode);
1438 if (!path[depth].p_ext)
1439 goto out;
1440 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
1441
1442 /*
1443 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001444 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001445 */
1446 if (b2 < b1) {
1447 b2 = ext4_ext_next_allocated_block(path);
1448 if (b2 == EXT_MAX_BLOCK)
1449 goto out;
1450 }
1451
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001452 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001453 if (b1 + len1 < b1) {
1454 len1 = EXT_MAX_BLOCK - b1;
1455 newext->ee_len = cpu_to_le16(len1);
1456 ret = 1;
1457 }
1458
1459 /* check for overlap */
1460 if (b1 + len1 > b2) {
1461 newext->ee_len = cpu_to_le16(b2 - b1);
1462 ret = 1;
1463 }
1464out:
1465 return ret;
1466}
1467
1468/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001469 * ext4_ext_insert_extent:
1470 * tries to merge requsted extent into the existing extent or
1471 * inserts requested extent as new one into the tree,
1472 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001473 */
1474int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1475 struct ext4_ext_path *path,
1476 struct ext4_extent *newext)
1477{
1478 struct ext4_extent_header * eh;
1479 struct ext4_extent *ex, *fex;
1480 struct ext4_extent *nearex; /* nearest extent */
1481 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001482 int depth, len, err;
1483 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001484 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001485
Amit Aroraa2df2a62007-07-17 21:42:41 -04001486 BUG_ON(ext4_ext_get_actual_len(newext) == 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07001487 depth = ext_depth(inode);
1488 ex = path[depth].p_ext;
1489 BUG_ON(path[depth].p_hdr == NULL);
1490
1491 /* try to insert block into found extent and return */
1492 if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
Mingming Cao2ae02102006-10-11 01:21:11 -07001493 ext_debug("append %d block to %d:%d (from %llu)\n",
Amit Aroraa2df2a62007-07-17 21:42:41 -04001494 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001495 le32_to_cpu(ex->ee_block),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001496 ext4_ext_get_actual_len(ex), ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001497 err = ext4_ext_get_access(handle, inode, path + depth);
1498 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001499 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001500
1501 /*
1502 * ext4_can_extents_be_merged should have checked that either
1503 * both extents are uninitialized, or both aren't. Thus we
1504 * need to check only one of them here.
1505 */
1506 if (ext4_ext_is_uninitialized(ex))
1507 uninitialized = 1;
1508 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1509 + ext4_ext_get_actual_len(newext));
1510 if (uninitialized)
1511 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001512 eh = path[depth].p_hdr;
1513 nearex = ex;
1514 goto merge;
1515 }
1516
1517repeat:
1518 depth = ext_depth(inode);
1519 eh = path[depth].p_hdr;
1520 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1521 goto has_space;
1522
1523 /* probably next leaf has space for us? */
1524 fex = EXT_LAST_EXTENT(eh);
1525 next = ext4_ext_next_leaf_block(inode, path);
1526 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
1527 && next != EXT_MAX_BLOCK) {
1528 ext_debug("next leaf block - %d\n", next);
1529 BUG_ON(npath != NULL);
1530 npath = ext4_ext_find_extent(inode, next, NULL);
1531 if (IS_ERR(npath))
1532 return PTR_ERR(npath);
1533 BUG_ON(npath->p_depth != path->p_depth);
1534 eh = npath[depth].p_hdr;
1535 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
1536 ext_debug("next leaf isnt full(%d)\n",
1537 le16_to_cpu(eh->eh_entries));
1538 path = npath;
1539 goto repeat;
1540 }
1541 ext_debug("next leaf has no free space(%d,%d)\n",
1542 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1543 }
1544
1545 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001546 * There is no free space in the found leaf.
1547 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001548 */
1549 err = ext4_ext_create_new_leaf(handle, inode, path, newext);
1550 if (err)
1551 goto cleanup;
1552 depth = ext_depth(inode);
1553 eh = path[depth].p_hdr;
1554
1555has_space:
1556 nearex = path[depth].p_ext;
1557
Avantika Mathur7e028972006-12-06 20:41:33 -08001558 err = ext4_ext_get_access(handle, inode, path + depth);
1559 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001560 goto cleanup;
1561
1562 if (!nearex) {
1563 /* there is no extent in this leaf, create first one */
Mingming Cao2ae02102006-10-11 01:21:11 -07001564 ext_debug("first extent in the leaf: %d:%llu:%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001565 le32_to_cpu(newext->ee_block),
1566 ext_pblock(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001567 ext4_ext_get_actual_len(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001568 path[depth].p_ext = EXT_FIRST_EXTENT(eh);
1569 } else if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001570 > le32_to_cpu(nearex->ee_block)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001571/* BUG_ON(newext->ee_block == nearex->ee_block); */
1572 if (nearex != EXT_LAST_EXTENT(eh)) {
1573 len = EXT_MAX_EXTENT(eh) - nearex;
1574 len = (len - 1) * sizeof(struct ext4_extent);
1575 len = len < 0 ? 0 : len;
Mingming Cao2ae02102006-10-11 01:21:11 -07001576 ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001577 "move %d from 0x%p to 0x%p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001578 le32_to_cpu(newext->ee_block),
1579 ext_pblock(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001580 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001581 nearex, len, nearex + 1, nearex + 2);
1582 memmove(nearex + 2, nearex + 1, len);
1583 }
1584 path[depth].p_ext = nearex + 1;
1585 } else {
1586 BUG_ON(newext->ee_block == nearex->ee_block);
1587 len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
1588 len = len < 0 ? 0 : len;
Mingming Cao2ae02102006-10-11 01:21:11 -07001589 ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
Alex Tomasa86c6182006-10-11 01:21:03 -07001590 "move %d from 0x%p to 0x%p\n",
1591 le32_to_cpu(newext->ee_block),
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001592 ext_pblock(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001593 ext4_ext_get_actual_len(newext),
Alex Tomasa86c6182006-10-11 01:21:03 -07001594 nearex, len, nearex + 1, nearex + 2);
1595 memmove(nearex + 1, nearex, len);
1596 path[depth].p_ext = nearex;
1597 }
1598
Marcin Slusarze8546d02008-04-17 10:38:59 -04001599 le16_add_cpu(&eh->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001600 nearex = path[depth].p_ext;
1601 nearex->ee_block = newext->ee_block;
Aneesh Kumar K.Vb3776112007-10-16 18:38:25 -04001602 ext4_ext_store_pblock(nearex, ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001603 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001604
1605merge:
1606 /* try to merge extents to the right */
Amit Arora56055d32007-07-17 21:42:38 -04001607 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001608
1609 /* try to merge extents to the left */
1610
1611 /* time to correct all indexes above */
1612 err = ext4_ext_correct_indexes(handle, inode, path);
1613 if (err)
1614 goto cleanup;
1615
1616 err = ext4_ext_dirty(handle, inode, path + depth);
1617
1618cleanup:
1619 if (npath) {
1620 ext4_ext_drop_refs(npath);
1621 kfree(npath);
1622 }
1623 ext4_ext_tree_changed(inode);
1624 ext4_ext_invalidate_cache(inode);
1625 return err;
1626}
1627
Avantika Mathur09b88252006-12-06 20:41:36 -08001628static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001629ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Mingming Caodd545672007-07-31 00:37:46 -07001630 __u32 len, ext4_fsblk_t start, int type)
Alex Tomasa86c6182006-10-11 01:21:03 -07001631{
1632 struct ext4_ext_cache *cex;
1633 BUG_ON(len == 0);
1634 cex = &EXT4_I(inode)->i_cached_extent;
1635 cex->ec_type = type;
1636 cex->ec_block = block;
1637 cex->ec_len = len;
1638 cex->ec_start = start;
1639}
1640
1641/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001642 * ext4_ext_put_gap_in_cache:
1643 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07001644 * and cache this gap
1645 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001646static void
Alex Tomasa86c6182006-10-11 01:21:03 -07001647ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001648 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07001649{
1650 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001651 unsigned long len;
1652 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001653 struct ext4_extent *ex;
1654
1655 ex = path[depth].p_ext;
1656 if (ex == NULL) {
1657 /* there is no extent yet, so gap is [0;-] */
1658 lblock = 0;
1659 len = EXT_MAX_BLOCK;
1660 ext_debug("cache gap(whole file):");
1661 } else if (block < le32_to_cpu(ex->ee_block)) {
1662 lblock = block;
1663 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05001664 ext_debug("cache gap(before): %u [%u:%u]",
1665 block,
1666 le32_to_cpu(ex->ee_block),
1667 ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07001668 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001669 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001670 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001671 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001672 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001673
1674 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05001675 ext_debug("cache gap(after): [%u:%u] %u",
1676 le32_to_cpu(ex->ee_block),
1677 ext4_ext_get_actual_len(ex),
1678 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001679 BUG_ON(next == lblock);
1680 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001681 } else {
1682 lblock = len = 0;
1683 BUG();
1684 }
1685
Eric Sandeenbba90742008-01-28 23:58:27 -05001686 ext_debug(" -> %u:%lu\n", lblock, len);
Alex Tomasa86c6182006-10-11 01:21:03 -07001687 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1688}
1689
Avantika Mathur09b88252006-12-06 20:41:36 -08001690static int
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001691ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
Alex Tomasa86c6182006-10-11 01:21:03 -07001692 struct ext4_extent *ex)
1693{
1694 struct ext4_ext_cache *cex;
1695
1696 cex = &EXT4_I(inode)->i_cached_extent;
1697
1698 /* has cache valid data? */
1699 if (cex->ec_type == EXT4_EXT_CACHE_NO)
1700 return EXT4_EXT_CACHE_NO;
1701
1702 BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
1703 cex->ec_type != EXT4_EXT_CACHE_EXTENT);
1704 if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001705 ex->ee_block = cpu_to_le32(cex->ec_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001706 ext4_ext_store_pblock(ex, cex->ec_start);
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001707 ex->ee_len = cpu_to_le16(cex->ec_len);
Eric Sandeenbba90742008-01-28 23:58:27 -05001708 ext_debug("%u cached by %u:%u:%llu\n",
1709 block,
1710 cex->ec_block, cex->ec_len, cex->ec_start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001711 return cex->ec_type;
1712 }
1713
1714 /* not in cache */
1715 return EXT4_EXT_CACHE_NO;
1716}
1717
1718/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001719 * ext4_ext_rm_idx:
1720 * removes index from the index block.
1721 * It's used in truncate case only, thus all requests are for
1722 * last index in the block only.
Alex Tomasa86c6182006-10-11 01:21:03 -07001723 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001724static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001725 struct ext4_ext_path *path)
1726{
1727 struct buffer_head *bh;
1728 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001729 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07001730
1731 /* free index block */
1732 path--;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001733 leaf = idx_pblock(path->p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -07001734 BUG_ON(path->p_hdr->eh_entries == 0);
Avantika Mathur7e028972006-12-06 20:41:33 -08001735 err = ext4_ext_get_access(handle, inode, path);
1736 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001737 return err;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001738 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08001739 err = ext4_ext_dirty(handle, inode, path);
1740 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001741 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07001742 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Alex Tomasa86c6182006-10-11 01:21:03 -07001743 bh = sb_find_get_block(inode->i_sb, leaf);
1744 ext4_forget(handle, 1, inode, bh, leaf);
Alex Tomasc9de5602008-01-29 00:19:52 -05001745 ext4_free_blocks(handle, inode, leaf, 1, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001746 return err;
1747}
1748
1749/*
Mingming Caoee12b632008-08-19 22:16:05 -04001750 * ext4_ext_calc_credits_for_single_extent:
1751 * This routine returns max. credits that needed to insert an extent
1752 * to the extent tree.
1753 * When pass the actual path, the caller should calculate credits
1754 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07001755 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04001756int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07001757 struct ext4_ext_path *path)
1758{
Alex Tomasa86c6182006-10-11 01:21:03 -07001759 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04001760 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04001761 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04001762
Alex Tomasa86c6182006-10-11 01:21:03 -07001763 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07001764 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04001765 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
1766
1767 /*
1768 * There are some space in the leaf tree, no
1769 * need to account for leaf block credit
1770 *
1771 * bitmaps and block group descriptor blocks
1772 * and other metadat blocks still need to be
1773 * accounted.
1774 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04001775 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04001776 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
1777 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001778 }
1779
Mingming Cao525f4ed2008-08-19 22:15:58 -04001780 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04001781}
Alex Tomasa86c6182006-10-11 01:21:03 -07001782
Mingming Caoee12b632008-08-19 22:16:05 -04001783/*
1784 * How many index/leaf blocks need to change/allocate to modify nrblocks?
1785 *
1786 * if nrblocks are fit in a single extent (chunk flag is 1), then
1787 * in the worse case, each tree level index/leaf need to be changed
1788 * if the tree split due to insert a new extent, then the old tree
1789 * index/leaf need to be updated too
1790 *
1791 * If the nrblocks are discontiguous, they could cause
1792 * the whole tree split more than once, but this is really rare.
1793 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04001794int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoee12b632008-08-19 22:16:05 -04001795{
1796 int index;
1797 int depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07001798
Mingming Caoee12b632008-08-19 22:16:05 -04001799 if (chunk)
1800 index = depth * 2;
1801 else
1802 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07001803
Mingming Caoee12b632008-08-19 22:16:05 -04001804 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07001805}
1806
1807static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
1808 struct ext4_extent *ex,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001809 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07001810{
1811 struct buffer_head *bh;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001812 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasc9de5602008-01-29 00:19:52 -05001813 int i, metadata = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001814
Alex Tomasc9de5602008-01-29 00:19:52 -05001815 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1816 metadata = 1;
Alex Tomasa86c6182006-10-11 01:21:03 -07001817#ifdef EXTENTS_STATS
1818 {
1819 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07001820 spin_lock(&sbi->s_ext_stats_lock);
1821 sbi->s_ext_blocks += ee_len;
1822 sbi->s_ext_extents++;
1823 if (ee_len < sbi->s_ext_min)
1824 sbi->s_ext_min = ee_len;
1825 if (ee_len > sbi->s_ext_max)
1826 sbi->s_ext_max = ee_len;
1827 if (ext_depth(inode) > sbi->s_depth_max)
1828 sbi->s_depth_max = ext_depth(inode);
1829 spin_unlock(&sbi->s_ext_stats_lock);
1830 }
1831#endif
1832 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001833 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001834 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001835 ext4_lblk_t num;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001836 ext4_fsblk_t start;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001837
Amit Aroraa2df2a62007-07-17 21:42:41 -04001838 num = le32_to_cpu(ex->ee_block) + ee_len - from;
1839 start = ext_pblock(ex) + ee_len - num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001840 ext_debug("free last %u blocks starting %llu\n", num, start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001841 for (i = 0; i < num; i++) {
1842 bh = sb_find_get_block(inode->i_sb, start + i);
1843 ext4_forget(handle, 0, inode, bh, start + i);
1844 }
Alex Tomasc9de5602008-01-29 00:19:52 -05001845 ext4_free_blocks(handle, inode, start, num, metadata);
Alex Tomasa86c6182006-10-11 01:21:03 -07001846 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001847 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001848 printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
Amit Aroraa2df2a62007-07-17 21:42:41 -04001849 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07001850 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001851 printk(KERN_INFO "strange request: removal(2) "
1852 "%u-%u from %u:%u\n",
1853 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07001854 }
1855 return 0;
1856}
1857
1858static int
1859ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001860 struct ext4_ext_path *path, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07001861{
1862 int err = 0, correct_index = 0;
1863 int depth = ext_depth(inode), credits;
1864 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001865 ext4_lblk_t a, b, block;
1866 unsigned num;
1867 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07001868 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001869 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001870 struct ext4_extent *ex;
1871
Alex Tomasc29c0ae2007-07-18 09:19:09 -04001872 /* the header must be checked already in ext4_ext_remove_space() */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001873 ext_debug("truncate since %u in leaf\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001874 if (!path[depth].p_hdr)
1875 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
1876 eh = path[depth].p_hdr;
1877 BUG_ON(eh == NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07001878
1879 /* find where to start removing */
1880 ex = EXT_LAST_EXTENT(eh);
1881
1882 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001883 if (ext4_ext_is_uninitialized(ex))
1884 uninitialized = 1;
1885 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001886
1887 while (ex >= EXT_FIRST_EXTENT(eh) &&
1888 ex_ee_block + ex_ee_len > start) {
1889 ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
1890 path[depth].p_ext = ex;
1891
1892 a = ex_ee_block > start ? ex_ee_block : start;
1893 b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
1894 ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
1895
1896 ext_debug(" border %u:%u\n", a, b);
1897
1898 if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
1899 block = 0;
1900 num = 0;
1901 BUG();
1902 } else if (a != ex_ee_block) {
1903 /* remove tail of the extent */
1904 block = ex_ee_block;
1905 num = a - block;
1906 } else if (b != ex_ee_block + ex_ee_len - 1) {
1907 /* remove head of the extent */
1908 block = a;
1909 num = b - a;
1910 /* there is no "make a hole" API yet */
1911 BUG();
1912 } else {
1913 /* remove whole extent: excellent! */
1914 block = ex_ee_block;
1915 num = 0;
1916 BUG_ON(a != ex_ee_block);
1917 BUG_ON(b != ex_ee_block + ex_ee_len - 1);
1918 }
1919
Theodore Ts'o34071da2008-08-01 21:59:19 -04001920 /*
1921 * 3 for leaf, sb, and inode plus 2 (bmap and group
1922 * descriptor) for each block group; assume two block
1923 * groups plus ex_ee_len/blocks_per_block_group for
1924 * the worst case
1925 */
1926 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07001927 if (ex == EXT_FIRST_EXTENT(eh)) {
1928 correct_index = 1;
1929 credits += (ext_depth(inode)) + 1;
1930 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001931 credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07001932
Shen Feng9102e4f2008-07-11 19:27:31 -04001933 err = ext4_ext_journal_restart(handle, credits);
1934 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001935 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001936
1937 err = ext4_ext_get_access(handle, inode, path + depth);
1938 if (err)
1939 goto out;
1940
1941 err = ext4_remove_blocks(handle, inode, ex, a, b);
1942 if (err)
1943 goto out;
1944
1945 if (num == 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001946 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001947 ext4_ext_store_pblock(ex, 0);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001948 le16_add_cpu(&eh->eh_entries, -1);
Alex Tomasa86c6182006-10-11 01:21:03 -07001949 }
1950
1951 ex->ee_block = cpu_to_le32(block);
1952 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04001953 /*
1954 * Do not mark uninitialized if all the blocks in the
1955 * extent have been removed.
1956 */
1957 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001958 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001959
1960 err = ext4_ext_dirty(handle, inode, path + depth);
1961 if (err)
1962 goto out;
1963
Mingming Cao2ae02102006-10-11 01:21:11 -07001964 ext_debug("new extent: %u:%u:%llu\n", block, num,
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001965 ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07001966 ex--;
1967 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001968 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001969 }
1970
1971 if (correct_index && eh->eh_entries)
1972 err = ext4_ext_correct_indexes(handle, inode, path);
1973
1974 /* if this leaf is free, then we should
1975 * remove it from index block above */
1976 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
1977 err = ext4_ext_rm_idx(handle, inode, path + depth);
1978
1979out:
1980 return err;
1981}
1982
1983/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001984 * ext4_ext_more_to_rm:
1985 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07001986 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001987static int
Alex Tomasa86c6182006-10-11 01:21:03 -07001988ext4_ext_more_to_rm(struct ext4_ext_path *path)
1989{
1990 BUG_ON(path->p_idx == NULL);
1991
1992 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
1993 return 0;
1994
1995 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001996 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07001997 * so we have to consider current index for truncation
1998 */
1999 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2000 return 0;
2001 return 1;
2002}
2003
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002004static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07002005{
2006 struct super_block *sb = inode->i_sb;
2007 int depth = ext_depth(inode);
2008 struct ext4_ext_path *path;
2009 handle_t *handle;
2010 int i = 0, err = 0;
2011
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002012 ext_debug("truncate since %u\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002013
2014 /* probably first extent we're gonna free will be last in block */
2015 handle = ext4_journal_start(inode, depth + 1);
2016 if (IS_ERR(handle))
2017 return PTR_ERR(handle);
2018
2019 ext4_ext_invalidate_cache(inode);
2020
2021 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002022 * We start scanning from right side, freeing all the blocks
2023 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002024 */
Josef Bacik216553c2008-04-29 22:02:02 -04002025 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07002026 if (path == NULL) {
2027 ext4_journal_stop(handle);
2028 return -ENOMEM;
2029 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002030 path[0].p_hdr = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002031 if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002032 err = -EIO;
2033 goto out;
2034 }
2035 path[0].p_depth = depth;
2036
2037 while (i >= 0 && err == 0) {
2038 if (i == depth) {
2039 /* this is leaf block */
2040 err = ext4_ext_rm_leaf(handle, inode, path, start);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002041 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002042 brelse(path[i].p_bh);
2043 path[i].p_bh = NULL;
2044 i--;
2045 continue;
2046 }
2047
2048 /* this is index block */
2049 if (!path[i].p_hdr) {
2050 ext_debug("initialize header\n");
2051 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002052 }
2053
Alex Tomasa86c6182006-10-11 01:21:03 -07002054 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002055 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002056 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2057 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2058 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2059 path[i].p_hdr,
2060 le16_to_cpu(path[i].p_hdr->eh_entries));
2061 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002062 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002063 path[i].p_idx--;
2064 }
2065
2066 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2067 i, EXT_FIRST_INDEX(path[i].p_hdr),
2068 path[i].p_idx);
2069 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002070 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002071 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002072 ext_debug("move to level %d (block %llu)\n",
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002073 i + 1, idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002074 memset(path + i + 1, 0, sizeof(*path));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002075 bh = sb_bread(sb, idx_pblock(path[i].p_idx));
2076 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002077 /* should we reset i_size? */
2078 err = -EIO;
2079 break;
2080 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002081 if (WARN_ON(i + 1 > depth)) {
2082 err = -EIO;
2083 break;
2084 }
2085 if (ext4_ext_check_header(inode, ext_block_hdr(bh),
2086 depth - i - 1)) {
2087 err = -EIO;
2088 break;
2089 }
2090 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002091
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002092 /* save actual number of indexes since this
2093 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002094 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2095 i++;
2096 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002097 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002098 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002099 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002100 * handle must be already prepared by the
2101 * truncatei_leaf() */
2102 err = ext4_ext_rm_idx(handle, inode, path + i);
2103 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002104 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002105 brelse(path[i].p_bh);
2106 path[i].p_bh = NULL;
2107 i--;
2108 ext_debug("return to level %d\n", i);
2109 }
2110 }
2111
2112 /* TODO: flexible tree reduction should be here */
2113 if (path->p_hdr->eh_entries == 0) {
2114 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002115 * truncate to zero freed all the tree,
2116 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002117 */
2118 err = ext4_ext_get_access(handle, inode, path);
2119 if (err == 0) {
2120 ext_inode_hdr(inode)->eh_depth = 0;
2121 ext_inode_hdr(inode)->eh_max =
2122 cpu_to_le16(ext4_ext_space_root(inode));
2123 err = ext4_ext_dirty(handle, inode, path);
2124 }
2125 }
2126out:
2127 ext4_ext_tree_changed(inode);
2128 ext4_ext_drop_refs(path);
2129 kfree(path);
2130 ext4_journal_stop(handle);
2131
2132 return err;
2133}
2134
2135/*
2136 * called at mount time
2137 */
2138void ext4_ext_init(struct super_block *sb)
2139{
2140 /*
2141 * possible initialization would be here
2142 */
2143
2144 if (test_opt(sb, EXTENTS)) {
2145 printk("EXT4-fs: file extents enabled");
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002146#ifdef AGGRESSIVE_TEST
2147 printk(", aggressive tests");
Alex Tomasa86c6182006-10-11 01:21:03 -07002148#endif
2149#ifdef CHECK_BINSEARCH
2150 printk(", check binsearch");
2151#endif
2152#ifdef EXTENTS_STATS
2153 printk(", stats");
2154#endif
2155 printk("\n");
2156#ifdef EXTENTS_STATS
2157 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2158 EXT4_SB(sb)->s_ext_min = 1 << 30;
2159 EXT4_SB(sb)->s_ext_max = 0;
2160#endif
2161 }
2162}
2163
2164/*
2165 * called at umount time
2166 */
2167void ext4_ext_release(struct super_block *sb)
2168{
2169 if (!test_opt(sb, EXTENTS))
2170 return;
2171
2172#ifdef EXTENTS_STATS
2173 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2174 struct ext4_sb_info *sbi = EXT4_SB(sb);
2175 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2176 sbi->s_ext_blocks, sbi->s_ext_extents,
2177 sbi->s_ext_blocks / sbi->s_ext_extents);
2178 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2179 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2180 }
2181#endif
2182}
2183
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002184static void bi_complete(struct bio *bio, int error)
2185{
2186 complete((struct completion *)bio->bi_private);
2187}
2188
2189/* FIXME!! we need to try to merge to left or right after zero-out */
2190static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2191{
2192 int ret = -EIO;
2193 struct bio *bio;
2194 int blkbits, blocksize;
2195 sector_t ee_pblock;
2196 struct completion event;
2197 unsigned int ee_len, len, done, offset;
2198
2199
2200 blkbits = inode->i_blkbits;
2201 blocksize = inode->i_sb->s_blocksize;
2202 ee_len = ext4_ext_get_actual_len(ex);
2203 ee_pblock = ext_pblock(ex);
2204
2205 /* convert ee_pblock to 512 byte sectors */
2206 ee_pblock = ee_pblock << (blkbits - 9);
2207
2208 while (ee_len > 0) {
2209
2210 if (ee_len > BIO_MAX_PAGES)
2211 len = BIO_MAX_PAGES;
2212 else
2213 len = ee_len;
2214
2215 bio = bio_alloc(GFP_NOIO, len);
2216 if (!bio)
2217 return -ENOMEM;
2218 bio->bi_sector = ee_pblock;
2219 bio->bi_bdev = inode->i_sb->s_bdev;
2220
2221 done = 0;
2222 offset = 0;
2223 while (done < len) {
2224 ret = bio_add_page(bio, ZERO_PAGE(0),
2225 blocksize, offset);
2226 if (ret != blocksize) {
2227 /*
2228 * We can't add any more pages because of
2229 * hardware limitations. Start a new bio.
2230 */
2231 break;
2232 }
2233 done++;
2234 offset += blocksize;
2235 if (offset >= PAGE_CACHE_SIZE)
2236 offset = 0;
2237 }
2238
2239 init_completion(&event);
2240 bio->bi_private = &event;
2241 bio->bi_end_io = bi_complete;
2242 submit_bio(WRITE, bio);
2243 wait_for_completion(&event);
2244
2245 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
2246 ret = 0;
2247 else {
2248 ret = -EIO;
2249 break;
2250 }
2251 bio_put(bio);
2252 ee_len -= done;
2253 ee_pblock += done << (blkbits - 9);
2254 }
2255 return ret;
2256}
2257
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002258#define EXT4_EXT_ZERO_LEN 7
2259
Amit Arora56055d32007-07-17 21:42:38 -04002260/*
2261 * This function is called by ext4_ext_get_blocks() if someone tries to write
2262 * to an uninitialized extent. It may result in splitting the uninitialized
2263 * extent into multiple extents (upto three - one initialized and two
2264 * uninitialized).
2265 * There are three possibilities:
2266 * a> There is no split required: Entire extent should be initialized
2267 * b> Splits in two extents: Write is happening at either end of the extent
2268 * c> Splits in three extents: Somone is writing in middle of the extent
2269 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002270static int ext4_ext_convert_to_initialized(handle_t *handle,
2271 struct inode *inode,
2272 struct ext4_ext_path *path,
2273 ext4_lblk_t iblock,
2274 unsigned long max_blocks)
Amit Arora56055d32007-07-17 21:42:38 -04002275{
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002276 struct ext4_extent *ex, newex, orig_ex;
Amit Arora56055d32007-07-17 21:42:38 -04002277 struct ext4_extent *ex1 = NULL;
2278 struct ext4_extent *ex2 = NULL;
2279 struct ext4_extent *ex3 = NULL;
2280 struct ext4_extent_header *eh;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002281 ext4_lblk_t ee_block;
2282 unsigned int allocated, ee_len, depth;
Amit Arora56055d32007-07-17 21:42:38 -04002283 ext4_fsblk_t newblock;
2284 int err = 0;
2285 int ret = 0;
2286
2287 depth = ext_depth(inode);
2288 eh = path[depth].p_hdr;
2289 ex = path[depth].p_ext;
2290 ee_block = le32_to_cpu(ex->ee_block);
2291 ee_len = ext4_ext_get_actual_len(ex);
2292 allocated = ee_len - (iblock - ee_block);
2293 newblock = iblock - ee_block + ext_pblock(ex);
2294 ex2 = ex;
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002295 orig_ex.ee_block = ex->ee_block;
2296 orig_ex.ee_len = cpu_to_le16(ee_len);
2297 ext4_ext_store_pblock(&orig_ex, ext_pblock(ex));
Amit Arora56055d32007-07-17 21:42:38 -04002298
Aneesh Kumar K.V9df56432008-02-22 06:17:31 -05002299 err = ext4_ext_get_access(handle, inode, path + depth);
2300 if (err)
2301 goto out;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002302 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
2303 if (ee_len <= 2*EXT4_EXT_ZERO_LEN) {
2304 err = ext4_ext_zeroout(inode, &orig_ex);
2305 if (err)
2306 goto fix_extent_len;
2307 /* update the extent length and mark as initialized */
2308 ex->ee_block = orig_ex.ee_block;
2309 ex->ee_len = orig_ex.ee_len;
2310 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2311 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002312 /* zeroed the full extent */
2313 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002314 }
Aneesh Kumar K.V9df56432008-02-22 06:17:31 -05002315
Amit Arora56055d32007-07-17 21:42:38 -04002316 /* ex1: ee_block to iblock - 1 : uninitialized */
2317 if (iblock > ee_block) {
2318 ex1 = ex;
2319 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2320 ext4_ext_mark_uninitialized(ex1);
2321 ex2 = &newex;
2322 }
2323 /*
2324 * for sanity, update the length of the ex2 extent before
2325 * we insert ex3, if ex1 is NULL. This is to avoid temporary
2326 * overlap of blocks.
2327 */
2328 if (!ex1 && allocated > max_blocks)
2329 ex2->ee_len = cpu_to_le16(max_blocks);
2330 /* ex3: to ee_block + ee_len : uninitialised */
2331 if (allocated > max_blocks) {
2332 unsigned int newdepth;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002333 /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */
2334 if (allocated <= EXT4_EXT_ZERO_LEN) {
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002335 /*
2336 * iblock == ee_block is handled by the zerouout
2337 * at the beginning.
2338 * Mark first half uninitialized.
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002339 * Mark second half initialized and zero out the
2340 * initialized extent
2341 */
2342 ex->ee_block = orig_ex.ee_block;
2343 ex->ee_len = cpu_to_le16(ee_len - allocated);
2344 ext4_ext_mark_uninitialized(ex);
2345 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2346 ext4_ext_dirty(handle, inode, path + depth);
2347
2348 ex3 = &newex;
2349 ex3->ee_block = cpu_to_le32(iblock);
2350 ext4_ext_store_pblock(ex3, newblock);
2351 ex3->ee_len = cpu_to_le16(allocated);
2352 err = ext4_ext_insert_extent(handle, inode, path, ex3);
2353 if (err == -ENOSPC) {
2354 err = ext4_ext_zeroout(inode, &orig_ex);
2355 if (err)
2356 goto fix_extent_len;
2357 ex->ee_block = orig_ex.ee_block;
2358 ex->ee_len = orig_ex.ee_len;
2359 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2360 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002361 /* blocks available from iblock */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002362 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002363
2364 } else if (err)
2365 goto fix_extent_len;
2366
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002367 /*
2368 * We need to zero out the second half because
2369 * an fallocate request can update file size and
2370 * converting the second half to initialized extent
2371 * implies that we can leak some junk data to user
2372 * space.
2373 */
2374 err = ext4_ext_zeroout(inode, ex3);
2375 if (err) {
2376 /*
2377 * We should actually mark the
2378 * second half as uninit and return error
2379 * Insert would have changed the extent
2380 */
2381 depth = ext_depth(inode);
2382 ext4_ext_drop_refs(path);
2383 path = ext4_ext_find_extent(inode,
2384 iblock, path);
2385 if (IS_ERR(path)) {
2386 err = PTR_ERR(path);
2387 return err;
2388 }
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002389 /* get the second half extent details */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002390 ex = path[depth].p_ext;
2391 err = ext4_ext_get_access(handle, inode,
2392 path + depth);
2393 if (err)
2394 return err;
2395 ext4_ext_mark_uninitialized(ex);
2396 ext4_ext_dirty(handle, inode, path + depth);
2397 return err;
2398 }
2399
2400 /* zeroed the second half */
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002401 return allocated;
2402 }
Amit Arora56055d32007-07-17 21:42:38 -04002403 ex3 = &newex;
2404 ex3->ee_block = cpu_to_le32(iblock + max_blocks);
2405 ext4_ext_store_pblock(ex3, newblock + max_blocks);
2406 ex3->ee_len = cpu_to_le16(allocated - max_blocks);
2407 ext4_ext_mark_uninitialized(ex3);
2408 err = ext4_ext_insert_extent(handle, inode, path, ex3);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002409 if (err == -ENOSPC) {
2410 err = ext4_ext_zeroout(inode, &orig_ex);
2411 if (err)
2412 goto fix_extent_len;
2413 /* update the extent length and mark as initialized */
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002414 ex->ee_block = orig_ex.ee_block;
2415 ex->ee_len = orig_ex.ee_len;
2416 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002417 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002418 /* zeroed the full extent */
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002419 /* blocks available from iblock */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002420 return allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002421
2422 } else if (err)
2423 goto fix_extent_len;
Amit Arora56055d32007-07-17 21:42:38 -04002424 /*
2425 * The depth, and hence eh & ex might change
2426 * as part of the insert above.
2427 */
2428 newdepth = ext_depth(inode);
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002429 /*
2430 * update the extent length after successfull insert of the
2431 * split extent
2432 */
2433 orig_ex.ee_len = cpu_to_le16(ee_len -
2434 ext4_ext_get_actual_len(ex3));
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002435 depth = newdepth;
2436 ext4_ext_drop_refs(path);
2437 path = ext4_ext_find_extent(inode, iblock, path);
2438 if (IS_ERR(path)) {
2439 err = PTR_ERR(path);
2440 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04002441 }
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002442 eh = path[depth].p_hdr;
2443 ex = path[depth].p_ext;
2444 if (ex2 != &newex)
2445 ex2 = ex;
2446
2447 err = ext4_ext_get_access(handle, inode, path + depth);
2448 if (err)
2449 goto out;
2450
Amit Arora56055d32007-07-17 21:42:38 -04002451 allocated = max_blocks;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002452
2453 /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying
2454 * to insert a extent in the middle zerout directly
2455 * otherwise give the extent a chance to merge to left
2456 */
2457 if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN &&
2458 iblock != ee_block) {
2459 err = ext4_ext_zeroout(inode, &orig_ex);
2460 if (err)
2461 goto fix_extent_len;
2462 /* update the extent length and mark as initialized */
2463 ex->ee_block = orig_ex.ee_block;
2464 ex->ee_len = orig_ex.ee_len;
2465 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2466 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002467 /* zero out the first half */
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04002468 /* blocks available from iblock */
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002469 return allocated;
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002470 }
Amit Arora56055d32007-07-17 21:42:38 -04002471 }
2472 /*
2473 * If there was a change of depth as part of the
2474 * insertion of ex3 above, we need to update the length
2475 * of the ex1 extent again here
2476 */
2477 if (ex1 && ex1 != ex) {
2478 ex1 = ex;
2479 ex1->ee_len = cpu_to_le16(iblock - ee_block);
2480 ext4_ext_mark_uninitialized(ex1);
2481 ex2 = &newex;
2482 }
2483 /* ex2: iblock to iblock + maxblocks-1 : initialised */
2484 ex2->ee_block = cpu_to_le32(iblock);
Amit Arora56055d32007-07-17 21:42:38 -04002485 ext4_ext_store_pblock(ex2, newblock);
2486 ex2->ee_len = cpu_to_le16(allocated);
2487 if (ex2 != ex)
2488 goto insert;
Amit Arora56055d32007-07-17 21:42:38 -04002489 /*
2490 * New (initialized) extent starts from the first block
2491 * in the current extent. i.e., ex2 == ex
2492 * We have to see if it can be merged with the extent
2493 * on the left.
2494 */
2495 if (ex2 > EXT_FIRST_EXTENT(eh)) {
2496 /*
2497 * To merge left, pass "ex2 - 1" to try_to_merge(),
2498 * since it merges towards right _only_.
2499 */
2500 ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
2501 if (ret) {
2502 err = ext4_ext_correct_indexes(handle, inode, path);
2503 if (err)
2504 goto out;
2505 depth = ext_depth(inode);
2506 ex2--;
2507 }
2508 }
2509 /*
2510 * Try to Merge towards right. This might be required
2511 * only when the whole extent is being written to.
2512 * i.e. ex2 == ex and ex3 == NULL.
2513 */
2514 if (!ex3) {
2515 ret = ext4_ext_try_to_merge(inode, path, ex2);
2516 if (ret) {
2517 err = ext4_ext_correct_indexes(handle, inode, path);
2518 if (err)
2519 goto out;
2520 }
2521 }
2522 /* Mark modified extent as dirty */
2523 err = ext4_ext_dirty(handle, inode, path + depth);
2524 goto out;
2525insert:
2526 err = ext4_ext_insert_extent(handle, inode, path, &newex);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002527 if (err == -ENOSPC) {
2528 err = ext4_ext_zeroout(inode, &orig_ex);
2529 if (err)
2530 goto fix_extent_len;
2531 /* update the extent length and mark as initialized */
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002532 ex->ee_block = orig_ex.ee_block;
2533 ex->ee_len = orig_ex.ee_len;
2534 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
Aneesh Kumar K.V95c38892008-04-17 10:38:59 -04002535 ext4_ext_dirty(handle, inode, path + depth);
Aneesh Kumar K.V161e7b72008-04-29 22:03:59 -04002536 /* zero out the first half */
2537 return allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002538 } else if (err)
2539 goto fix_extent_len;
Amit Arora56055d32007-07-17 21:42:38 -04002540out:
2541 return err ? err : allocated;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002542
2543fix_extent_len:
2544 ex->ee_block = orig_ex.ee_block;
2545 ex->ee_len = orig_ex.ee_len;
2546 ext4_ext_store_pblock(ex, ext_pblock(&orig_ex));
2547 ext4_ext_mark_uninitialized(ex);
2548 ext4_ext_dirty(handle, inode, path + depth);
2549 return err;
Amit Arora56055d32007-07-17 21:42:38 -04002550}
2551
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05002552/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05002553 * Block allocation/map/preallocation routine for extents based files
2554 *
2555 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05002556 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05002557 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
2558 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05002559 *
2560 * return > 0, number of of blocks already mapped/allocated
2561 * if create == 0 and these are pre-allocated blocks
2562 * buffer head is unmapped
2563 * otherwise blocks are mapped
2564 *
2565 * return = 0, if plain look up failed (blocks have not been allocated)
2566 * buffer head is unmapped
2567 *
2568 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05002569 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002570int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002571 ext4_lblk_t iblock,
Alex Tomasa86c6182006-10-11 01:21:03 -07002572 unsigned long max_blocks, struct buffer_head *bh_result,
2573 int create, int extend_disksize)
2574{
2575 struct ext4_ext_path *path = NULL;
Amit Arora56055d32007-07-17 21:42:38 -04002576 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002577 struct ext4_extent newex, *ex;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002578 ext4_fsblk_t goal, newblock;
Amit Arora56055d32007-07-17 21:42:38 -04002579 int err = 0, depth, ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07002580 unsigned long allocated = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05002581 struct ext4_allocation_request ar;
Mingming Cao61628a32008-07-11 19:27:31 -04002582 loff_t disksize;
Alex Tomasa86c6182006-10-11 01:21:03 -07002583
2584 __clear_bit(BH_New, &bh_result->b_state);
Eric Sandeenbba90742008-01-28 23:58:27 -05002585 ext_debug("blocks %u/%lu requested for inode %u\n",
2586 iblock, max_blocks, inode->i_ino);
Alex Tomasa86c6182006-10-11 01:21:03 -07002587
2588 /* check in cache */
Avantika Mathur7e028972006-12-06 20:41:33 -08002589 goal = ext4_ext_in_cache(inode, iblock, &newex);
2590 if (goal) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002591 if (goal == EXT4_EXT_CACHE_GAP) {
2592 if (!create) {
Amit Arora56055d32007-07-17 21:42:38 -04002593 /*
2594 * block isn't allocated yet and
2595 * user doesn't want to allocate it
2596 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002597 goto out2;
2598 }
2599 /* we should allocate requested block */
2600 } else if (goal == EXT4_EXT_CACHE_EXTENT) {
2601 /* block is already allocated */
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002602 newblock = iblock
2603 - le32_to_cpu(newex.ee_block)
2604 + ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002605 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05002606 allocated = ext4_ext_get_actual_len(&newex) -
Alex Tomasa86c6182006-10-11 01:21:03 -07002607 (iblock - le32_to_cpu(newex.ee_block));
2608 goto out;
2609 } else {
2610 BUG();
2611 }
2612 }
2613
2614 /* find extent for this block */
2615 path = ext4_ext_find_extent(inode, iblock, NULL);
2616 if (IS_ERR(path)) {
2617 err = PTR_ERR(path);
2618 path = NULL;
2619 goto out2;
2620 }
2621
2622 depth = ext_depth(inode);
2623
2624 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002625 * consistent leaf must not be empty;
2626 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07002627 * this is why assert can't be put in ext4_ext_find_extent()
2628 */
2629 BUG_ON(path[depth].p_ext == NULL && depth != 0);
Amit Arora56055d32007-07-17 21:42:38 -04002630 eh = path[depth].p_hdr;
Alex Tomasa86c6182006-10-11 01:21:03 -07002631
Avantika Mathur7e028972006-12-06 20:41:33 -08002632 ex = path[depth].p_ext;
2633 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002634 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002635 ext4_fsblk_t ee_start = ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002636 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07002637
2638 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07002639 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04002640 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07002641 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04002642 ee_len = ext4_ext_get_actual_len(ex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002643 /* if found extent covers block, simply return it */
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002644 if (iblock >= ee_block && iblock < ee_block + ee_len) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002645 newblock = iblock - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002646 /* number of remaining blocks in the extent */
Alex Tomasa86c6182006-10-11 01:21:03 -07002647 allocated = ee_len - (iblock - ee_block);
Eric Sandeenbba90742008-01-28 23:58:27 -05002648 ext_debug("%u fit into %lu:%d -> %llu\n", iblock,
Alex Tomasa86c6182006-10-11 01:21:03 -07002649 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04002650
Amit Aroraa2df2a62007-07-17 21:42:41 -04002651 /* Do not put uninitialized extent in the cache */
Amit Arora56055d32007-07-17 21:42:38 -04002652 if (!ext4_ext_is_uninitialized(ex)) {
Amit Aroraa2df2a62007-07-17 21:42:41 -04002653 ext4_ext_put_in_cache(inode, ee_block,
2654 ee_len, ee_start,
2655 EXT4_EXT_CACHE_EXTENT);
Amit Arora56055d32007-07-17 21:42:38 -04002656 goto out;
2657 }
2658 if (create == EXT4_CREATE_UNINITIALIZED_EXT)
2659 goto out;
Aneesh Kumar K.Ve067ba02008-04-29 08:11:12 -04002660 if (!create) {
2661 /*
2662 * We have blocks reserved already. We
2663 * return allocated blocks so that delalloc
2664 * won't do block reservation for us. But
2665 * the buffer head will be unmapped so that
2666 * a read from the block returns 0s.
2667 */
2668 if (allocated > max_blocks)
2669 allocated = max_blocks;
Eric Sandeen953e6222008-07-11 19:27:31 -04002670 set_buffer_unwritten(bh_result);
Amit Arora56055d32007-07-17 21:42:38 -04002671 goto out2;
Aneesh Kumar K.Ve067ba02008-04-29 08:11:12 -04002672 }
Amit Arora56055d32007-07-17 21:42:38 -04002673
2674 ret = ext4_ext_convert_to_initialized(handle, inode,
2675 path, iblock,
2676 max_blocks);
Dmitry Monakhovdbf9d7d2008-01-28 23:58:27 -05002677 if (ret <= 0) {
2678 err = ret;
Amit Arora56055d32007-07-17 21:42:38 -04002679 goto out2;
Dmitry Monakhovdbf9d7d2008-01-28 23:58:27 -05002680 } else
Amit Arora56055d32007-07-17 21:42:38 -04002681 allocated = ret;
2682 goto outnew;
Alex Tomasa86c6182006-10-11 01:21:03 -07002683 }
2684 }
2685
2686 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002687 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07002688 * we couldn't try to create block if create flag is zero
2689 */
2690 if (!create) {
Amit Arora56055d32007-07-17 21:42:38 -04002691 /*
2692 * put just found gap into cache to speed up
2693 * subsequent requests
2694 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002695 ext4_ext_put_gap_in_cache(inode, path, iblock);
2696 goto out2;
2697 }
2698 /*
Andrew Morton63f57932006-10-11 01:21:24 -07002699 * Okay, we need to do block allocation. Lazily initialize the block
2700 * allocation info here if necessary.
2701 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002702 if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
2703 ext4_init_block_alloc_info(inode);
2704
Alex Tomasc9de5602008-01-29 00:19:52 -05002705 /* find neighbour allocated blocks */
2706 ar.lleft = iblock;
2707 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
2708 if (err)
2709 goto out2;
2710 ar.lright = iblock;
2711 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
2712 if (err)
2713 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04002714
Amit Arora749269f2007-07-18 09:02:56 -04002715 /*
2716 * See if request is beyond maximum number of blocks we can have in
2717 * a single extent. For an initialized extent this limit is
2718 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
2719 * EXT_UNINIT_MAX_LEN.
2720 */
2721 if (max_blocks > EXT_INIT_MAX_LEN &&
2722 create != EXT4_CREATE_UNINITIALIZED_EXT)
2723 max_blocks = EXT_INIT_MAX_LEN;
2724 else if (max_blocks > EXT_UNINIT_MAX_LEN &&
2725 create == EXT4_CREATE_UNINITIALIZED_EXT)
2726 max_blocks = EXT_UNINIT_MAX_LEN;
2727
Amit Arora25d14f92007-05-24 13:04:13 -04002728 /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
2729 newex.ee_block = cpu_to_le32(iblock);
2730 newex.ee_len = cpu_to_le16(max_blocks);
2731 err = ext4_ext_check_overlap(inode, &newex, path);
2732 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05002733 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04002734 else
2735 allocated = max_blocks;
Alex Tomasc9de5602008-01-29 00:19:52 -05002736
2737 /* allocate new block */
2738 ar.inode = inode;
2739 ar.goal = ext4_ext_find_goal(inode, path, iblock);
2740 ar.logical = iblock;
2741 ar.len = allocated;
2742 if (S_ISREG(inode->i_mode))
2743 ar.flags = EXT4_MB_HINT_DATA;
2744 else
2745 /* disable in-core preallocation for non-regular files */
2746 ar.flags = 0;
2747 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07002748 if (!newblock)
2749 goto out2;
Mingming Cao2ae02102006-10-11 01:21:11 -07002750 ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07002751 goal, newblock, allocated);
2752
2753 /* try to insert new extent into found leaf and return */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002754 ext4_ext_store_pblock(&newex, newblock);
Alex Tomasc9de5602008-01-29 00:19:52 -05002755 newex.ee_len = cpu_to_le16(ar.len);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002756 if (create == EXT4_CREATE_UNINITIALIZED_EXT) /* Mark uninitialized */
2757 ext4_ext_mark_uninitialized(&newex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002758 err = ext4_ext_insert_extent(handle, inode, path, &newex);
Alex Tomas315054f2007-05-24 13:04:25 -04002759 if (err) {
2760 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05002761 /* not a good idea to call discard here directly,
2762 * but otherwise we'd need to call it every free() */
2763 ext4_mb_discard_inode_preallocations(inode);
Alex Tomas315054f2007-05-24 13:04:25 -04002764 ext4_free_blocks(handle, inode, ext_pblock(&newex),
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05002765 ext4_ext_get_actual_len(&newex), 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002766 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04002767 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002768
Alex Tomasa86c6182006-10-11 01:21:03 -07002769 /* previous routine could use block we allocated */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002770 newblock = ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05002771 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora56055d32007-07-17 21:42:38 -04002772outnew:
Mingming Cao61628a32008-07-11 19:27:31 -04002773 if (extend_disksize) {
2774 disksize = ((loff_t) iblock + ar.len) << inode->i_blkbits;
2775 if (disksize > i_size_read(inode))
2776 disksize = i_size_read(inode);
2777 if (disksize > EXT4_I(inode)->i_disksize)
2778 EXT4_I(inode)->i_disksize = disksize;
2779 }
Aneesh Kumar K.Va379cd12008-07-11 19:27:31 -04002780
Eric Sandeen953e6222008-07-11 19:27:31 -04002781 set_buffer_new(bh_result);
Alex Tomasa86c6182006-10-11 01:21:03 -07002782
Amit Aroraa2df2a62007-07-17 21:42:41 -04002783 /* Cache only when it is _not_ an uninitialized extent */
2784 if (create != EXT4_CREATE_UNINITIALIZED_EXT)
2785 ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
2786 EXT4_EXT_CACHE_EXTENT);
Alex Tomasa86c6182006-10-11 01:21:03 -07002787out:
2788 if (allocated > max_blocks)
2789 allocated = max_blocks;
2790 ext4_ext_show_leaf(inode, path);
Eric Sandeen953e6222008-07-11 19:27:31 -04002791 set_buffer_mapped(bh_result);
Alex Tomasa86c6182006-10-11 01:21:03 -07002792 bh_result->b_bdev = inode->i_sb->s_bdev;
2793 bh_result->b_blocknr = newblock;
2794out2:
2795 if (path) {
2796 ext4_ext_drop_refs(path);
2797 kfree(path);
2798 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002799 return err ? err : allocated;
2800}
2801
Jan Karacf108bc2008-07-11 19:27:31 -04002802void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07002803{
2804 struct address_space *mapping = inode->i_mapping;
2805 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002806 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002807 handle_t *handle;
2808 int err = 0;
2809
2810 /*
2811 * probably first extent we're gonna free will be last in block
2812 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04002813 err = ext4_writepage_trans_blocks(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002814 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04002815 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07002816 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07002817
Jan Karacf108bc2008-07-11 19:27:31 -04002818 if (inode->i_size & (sb->s_blocksize - 1))
2819 ext4_block_truncate_page(handle, mapping, inode->i_size);
Alex Tomasa86c6182006-10-11 01:21:03 -07002820
Jan Kara9ddfc3d2008-07-11 19:27:31 -04002821 if (ext4_orphan_add(handle, inode))
2822 goto out_stop;
2823
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05002824 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07002825 ext4_ext_invalidate_cache(inode);
2826
Theodore Ts'o88aa3cf2008-08-16 07:57:35 -04002827 ext4_discard_reservation(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05002828
Alex Tomasa86c6182006-10-11 01:21:03 -07002829 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002830 * TODO: optimization is possible here.
2831 * Probably we need not scan at all,
2832 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07002833 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002834
2835 /* we have to know where to truncate from in crash case */
2836 EXT4_I(inode)->i_disksize = inode->i_size;
2837 ext4_mark_inode_dirty(handle, inode);
2838
2839 last_block = (inode->i_size + sb->s_blocksize - 1)
2840 >> EXT4_BLOCK_SIZE_BITS(sb);
2841 err = ext4_ext_remove_space(inode, last_block);
2842
2843 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04002844 * transaction synchronous.
2845 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002846 if (IS_SYNC(inode))
2847 handle->h_sync = 1;
2848
2849out_stop:
Jan Kara9ddfc3d2008-07-11 19:27:31 -04002850 up_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07002851 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002852 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07002853 * then we need to clear up the orphan record which we created above.
2854 * However, if this was a real unlink then we were called by
2855 * ext4_delete_inode(), and we allow that function to clean up the
2856 * orphan info for us.
2857 */
2858 if (inode->i_nlink)
2859 ext4_orphan_del(handle, inode);
2860
Solofo Ramangalahyef737722008-04-29 22:00:41 -04002861 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
2862 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002863 ext4_journal_stop(handle);
2864}
2865
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04002866static void ext4_falloc_update_inode(struct inode *inode,
2867 int mode, loff_t new_size, int update_ctime)
2868{
2869 struct timespec now;
2870
2871 if (update_ctime) {
2872 now = current_fs_time(inode->i_sb);
2873 if (!timespec_equal(&inode->i_ctime, &now))
2874 inode->i_ctime = now;
2875 }
2876 /*
2877 * Update only when preallocation was requested beyond
2878 * the file size.
2879 */
2880 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
2881 new_size > i_size_read(inode)) {
2882 i_size_write(inode, new_size);
2883 EXT4_I(inode)->i_disksize = new_size;
2884 }
2885
2886}
2887
Amit Aroraa2df2a62007-07-17 21:42:41 -04002888/*
2889 * preallocate space for a file. This implements ext4's fallocate inode
2890 * operation, which gets called from sys_fallocate system call.
2891 * For block-mapped files, posix_fallocate should fall back to the method
2892 * of writing zeroes to the required new blocks (the same behavior which is
2893 * expected for file systems which do not support fallocate() system call).
2894 */
2895long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
2896{
2897 handle_t *handle;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002898 ext4_lblk_t block;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04002899 loff_t new_size;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002900 unsigned long max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002901 int ret = 0;
2902 int ret2 = 0;
2903 int retries = 0;
2904 struct buffer_head map_bh;
2905 unsigned int credits, blkbits = inode->i_blkbits;
2906
2907 /*
2908 * currently supporting (pre)allocate mode for extent-based
2909 * files _only_
2910 */
2911 if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
2912 return -EOPNOTSUPP;
2913
2914 /* preallocation to directories is currently not supported */
2915 if (S_ISDIR(inode->i_mode))
2916 return -ENODEV;
2917
2918 block = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04002919 /*
2920 * We can't just convert len to max_blocks because
2921 * If blocksize = 4096 offset = 3072 and len = 2048
2922 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04002923 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04002924 - block;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002925 /*
Mingming Caof3bd1f32008-08-19 22:16:03 -04002926 * credits to insert 1 extent into extent tree
Amit Aroraa2df2a62007-07-17 21:42:41 -04002927 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04002928 credits = ext4_chunk_trans_blocks(inode, max_blocks);
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05002929 mutex_lock(&inode->i_mutex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002930retry:
2931 while (ret >= 0 && ret < max_blocks) {
2932 block = block + ret;
2933 max_blocks = max_blocks - ret;
2934 handle = ext4_journal_start(inode, credits);
2935 if (IS_ERR(handle)) {
2936 ret = PTR_ERR(handle);
2937 break;
2938 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05002939 ret = ext4_get_blocks_wrap(handle, inode, block,
Amit Aroraa2df2a62007-07-17 21:42:41 -04002940 max_blocks, &map_bh,
Mingming Caod2a17632008-07-14 17:52:37 -04002941 EXT4_CREATE_UNINITIALIZED_EXT, 0, 0);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05002942 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05002943#ifdef EXT4FS_DEBUG
2944 WARN_ON(ret <= 0);
2945 printk(KERN_ERR "%s: ext4_ext_get_blocks "
2946 "returned error inode#%lu, block=%u, "
2947 "max_blocks=%lu", __func__,
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05002948 inode->i_ino, block, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05002949#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04002950 ext4_mark_inode_dirty(handle, inode);
2951 ret2 = ext4_journal_stop(handle);
2952 break;
2953 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04002954 if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
2955 blkbits) >> blkbits))
2956 new_size = offset + len;
2957 else
2958 new_size = (block + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002959
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04002960 ext4_falloc_update_inode(inode, mode, new_size,
2961 buffer_new(&map_bh));
Amit Aroraa2df2a62007-07-17 21:42:41 -04002962 ext4_mark_inode_dirty(handle, inode);
2963 ret2 = ext4_journal_stop(handle);
2964 if (ret2)
2965 break;
2966 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04002967 if (ret == -ENOSPC &&
2968 ext4_should_retry_alloc(inode->i_sb, &retries)) {
2969 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002970 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002971 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05002972 mutex_unlock(&inode->i_mutex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002973 return ret > 0 ? ret2 : ret;
2974}