blob: 4394a757aa4cfbcbc13c644192c9cdfba5f09c17 [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
Alex Tomasa86c6182006-10-11 01:21:03 -070032#include <linux/fs.h>
33#include <linux/time.h>
Mingming Caocd02ff02007-10-16 18:38:25 -040034#include <linux/jbd2.h>
Alex Tomasa86c6182006-10-11 01:21:03 -070035#include <linux/highuid.h>
36#include <linux/pagemap.h>
37#include <linux/quotaops.h>
38#include <linux/string.h>
39#include <linux/slab.h>
Amit Aroraa2df2a62007-07-17 21:42:41 -040040#include <linux/falloc.h>
Alex Tomasa86c6182006-10-11 01:21:03 -070041#include <asm/uaccess.h>
Eric Sandeen6873fa02008-10-07 00:46:36 -040042#include <linux/fiemap.h>
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040043#include "ext4_jbd2.h"
Alex Tomasa86c6182006-10-11 01:21:03 -070044
Jiaying Zhang0562e0b2011-03-21 21:38:05 -040045#include <trace/events/ext4.h>
46
Allison Hendersond583fb82011-05-25 07:41:43 -040047static int ext4_split_extent(handle_t *handle,
48 struct inode *inode,
49 struct ext4_ext_path *path,
50 struct ext4_map_blocks *map,
51 int split_flag,
52 int flags);
53
Jan Kara487caee2009-08-17 22:17:20 -040054static int ext4_ext_truncate_extend_restart(handle_t *handle,
55 struct inode *inode,
56 int needed)
Alex Tomasa86c6182006-10-11 01:21:03 -070057{
58 int err;
59
Frank Mayhar03901312009-01-07 00:06:22 -050060 if (!ext4_handle_valid(handle))
61 return 0;
Alex Tomasa86c6182006-10-11 01:21:03 -070062 if (handle->h_buffer_credits > needed)
Shen Feng9102e4f2008-07-11 19:27:31 -040063 return 0;
64 err = ext4_journal_extend(handle, needed);
Theodore Ts'o0123c932008-08-01 20:57:54 -040065 if (err <= 0)
Shen Feng9102e4f2008-07-11 19:27:31 -040066 return err;
Jan Kara487caee2009-08-17 22:17:20 -040067 err = ext4_truncate_restart_trans(handle, inode, needed);
Dmitry Monakhov0617b832010-05-17 01:00:00 -040068 if (err == 0)
69 err = -EAGAIN;
Jan Kara487caee2009-08-17 22:17:20 -040070
71 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -070072}
73
74/*
75 * could return:
76 * - EROFS
77 * - ENOMEM
78 */
79static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
80 struct ext4_ext_path *path)
81{
82 if (path->p_bh) {
83 /* path points to block */
84 return ext4_journal_get_write_access(handle, path->p_bh);
85 }
86 /* path points to leaf/index in inode body */
87 /* we use in-core data, no need to protect them */
88 return 0;
89}
90
91/*
92 * could return:
93 * - EROFS
94 * - ENOMEM
95 * - EIO
96 */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -040097#define ext4_ext_dirty(handle, inode, path) \
98 __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path))
99static int __ext4_ext_dirty(const char *where, unsigned int line,
100 handle_t *handle, struct inode *inode,
101 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700102{
103 int err;
104 if (path->p_bh) {
105 /* path points to block */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -0400106 err = __ext4_handle_dirty_metadata(where, line, handle,
107 inode, path->p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700108 } else {
109 /* path points to leaf/index in inode body */
110 err = ext4_mark_inode_dirty(handle, inode);
111 }
112 return err;
113}
114
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700115static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700116 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500117 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700118{
Alex Tomasa86c6182006-10-11 01:21:03 -0700119 if (path) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400120 int depth = path->p_depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700121 struct ext4_extent *ex;
Alex Tomasa86c6182006-10-11 01:21:03 -0700122
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500123 /*
124 * Try to predict block placement assuming that we are
125 * filling in a file which will eventually be
126 * non-sparse --- i.e., in the case of libbfd writing
127 * an ELF object sections out-of-order but in a way
128 * the eventually results in a contiguous object or
129 * executable file, or some database extending a table
130 * space file. However, this is actually somewhat
131 * non-ideal if we are writing a sparse file such as
132 * qemu or KVM writing a raw image file that is going
133 * to stay fairly sparse, since it will end up
134 * fragmenting the file system's free space. Maybe we
135 * should have some hueristics or some way to allow
136 * userspace to pass a hint to file system,
Tao Mab8d65682011-01-21 23:21:31 +0800137 * especially if the latter case turns out to be
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500138 * common.
139 */
Avantika Mathur7e028972006-12-06 20:41:33 -0800140 ex = path[depth].p_ext;
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500141 if (ex) {
142 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
143 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
144
145 if (block > ext_block)
146 return ext_pblk + (block - ext_block);
147 else
148 return ext_pblk - (ext_block - block);
149 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700150
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700151 /* it looks like index is empty;
152 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700153 if (path[depth].p_bh)
154 return path[depth].p_bh->b_blocknr;
155 }
156
157 /* OK. use inode's group */
Eric Sandeenf86186b2011-06-28 10:01:31 -0400158 return ext4_inode_to_goal_block(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700159}
160
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400161/*
162 * Allocation for a meta data block
163 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700164static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400165ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700166 struct ext4_ext_path *path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400167 struct ext4_extent *ex, int *err, unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700168{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700169 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700170
171 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Allison Henderson55f020d2011-05-25 07:41:26 -0400172 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
173 NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700174 return newblock;
175}
176
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400177static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700178{
179 int size;
180
181 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
182 / sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100183#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400184 if (!check && size > 6)
185 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700186#endif
187 return size;
188}
189
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400190static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700191{
192 int size;
193
194 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
195 / sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100196#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400197 if (!check && size > 5)
198 size = 5;
Alex Tomasa86c6182006-10-11 01:21:03 -0700199#endif
200 return size;
201}
202
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400203static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700204{
205 int size;
206
207 size = sizeof(EXT4_I(inode)->i_data);
208 size -= sizeof(struct ext4_extent_header);
209 size /= sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100210#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400211 if (!check && size > 3)
212 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700213#endif
214 return size;
215}
216
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400217static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700218{
219 int size;
220
221 size = sizeof(EXT4_I(inode)->i_data);
222 size -= sizeof(struct ext4_extent_header);
223 size /= sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100224#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400225 if (!check && size > 4)
226 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700227#endif
228 return size;
229}
230
Mingming Caod2a17632008-07-14 17:52:37 -0400231/*
232 * Calculate the number of metadata blocks needed
233 * to allocate @blocks
234 * Worse case is one block per extent
235 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -0500236int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -0400237{
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500238 struct ext4_inode_info *ei = EXT4_I(inode);
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400239 int idxs;
Mingming Caod2a17632008-07-14 17:52:37 -0400240
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500241 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
242 / sizeof(struct ext4_extent_idx));
Mingming Caod2a17632008-07-14 17:52:37 -0400243
244 /*
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500245 * If the new delayed allocation block is contiguous with the
246 * previous da block, it can share index blocks with the
247 * previous block, so we only need to allocate a new index
248 * block every idxs leaf blocks. At ldxs**2 blocks, we need
249 * an additional index block, and at ldxs**3 blocks, yet
250 * another index blocks.
Mingming Caod2a17632008-07-14 17:52:37 -0400251 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500252 if (ei->i_da_metadata_calc_len &&
253 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400254 int num = 0;
255
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500256 if ((ei->i_da_metadata_calc_len % idxs) == 0)
257 num++;
258 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
259 num++;
260 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
261 num++;
262 ei->i_da_metadata_calc_len = 0;
263 } else
264 ei->i_da_metadata_calc_len++;
265 ei->i_da_metadata_calc_last_lblock++;
266 return num;
267 }
Mingming Caod2a17632008-07-14 17:52:37 -0400268
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500269 /*
270 * In the worst case we need a new set of index blocks at
271 * every level of the inode's extent tree.
272 */
273 ei->i_da_metadata_calc_len = 1;
274 ei->i_da_metadata_calc_last_lblock = lblock;
275 return ext_depth(inode) + 1;
Mingming Caod2a17632008-07-14 17:52:37 -0400276}
277
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400278static int
279ext4_ext_max_entries(struct inode *inode, int depth)
280{
281 int max;
282
283 if (depth == ext_depth(inode)) {
284 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400285 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400286 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400287 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400288 } else {
289 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400290 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400291 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400292 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400293 }
294
295 return max;
296}
297
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400298static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
299{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400300 ext4_fsblk_t block = ext4_ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400301 int len = ext4_ext_get_actual_len(ext);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400302
Theodore Ts'o31d4f3a2012-03-11 23:30:16 -0400303 if (len == 0)
304 return 0;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400305 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400306}
307
308static int ext4_valid_extent_idx(struct inode *inode,
309 struct ext4_extent_idx *ext_idx)
310{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400311 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400312
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400313 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400314}
315
316static int ext4_valid_extent_entries(struct inode *inode,
317 struct ext4_extent_header *eh,
318 int depth)
319{
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400320 unsigned short entries;
321 if (eh->eh_entries == 0)
322 return 1;
323
324 entries = le16_to_cpu(eh->eh_entries);
325
326 if (depth == 0) {
327 /* leaf entries */
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400328 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400329 while (entries) {
330 if (!ext4_valid_extent(inode, ext))
331 return 0;
332 ext++;
333 entries--;
334 }
335 } else {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400336 struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400337 while (entries) {
338 if (!ext4_valid_extent_idx(inode, ext_idx))
339 return 0;
340 ext_idx++;
341 entries--;
342 }
343 }
344 return 1;
345}
346
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400347static int __ext4_ext_check(const char *function, unsigned int line,
348 struct inode *inode, struct ext4_extent_header *eh,
349 int depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400350{
351 const char *error_msg;
352 int max = 0;
353
354 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
355 error_msg = "invalid magic";
356 goto corrupted;
357 }
358 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
359 error_msg = "unexpected eh_depth";
360 goto corrupted;
361 }
362 if (unlikely(eh->eh_max == 0)) {
363 error_msg = "invalid eh_max";
364 goto corrupted;
365 }
366 max = ext4_ext_max_entries(inode, depth);
367 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
368 error_msg = "too large eh_max";
369 goto corrupted;
370 }
371 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
372 error_msg = "invalid eh_entries";
373 goto corrupted;
374 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400375 if (!ext4_valid_extent_entries(inode, eh, depth)) {
376 error_msg = "invalid extent entries";
377 goto corrupted;
378 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400379 return 0;
380
381corrupted:
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400382 ext4_error_inode(inode, function, line, 0,
Theodore Ts'o24676da2010-05-16 21:00:00 -0400383 "bad header/extent: %s - magic %x, "
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400384 "entries %u, max %u(%u), depth %u(%u)",
Theodore Ts'o24676da2010-05-16 21:00:00 -0400385 error_msg, le16_to_cpu(eh->eh_magic),
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400386 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
387 max, le16_to_cpu(eh->eh_depth), depth);
388
389 return -EIO;
390}
391
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400392#define ext4_ext_check(inode, eh, depth) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400393 __ext4_ext_check(__func__, __LINE__, inode, eh, depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400394
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400395int ext4_ext_check_inode(struct inode *inode)
396{
397 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
398}
399
Alex Tomasa86c6182006-10-11 01:21:03 -0700400#ifdef EXT_DEBUG
401static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
402{
403 int k, l = path->p_depth;
404
405 ext_debug("path:");
406 for (k = 0; k <= l; k++, path++) {
407 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700408 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400409 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700410 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400411 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700412 le32_to_cpu(path->p_ext->ee_block),
Mingming553f9002009-09-18 13:34:55 -0400413 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400414 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400415 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700416 } else
417 ext_debug(" []");
418 }
419 ext_debug("\n");
420}
421
422static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
423{
424 int depth = ext_depth(inode);
425 struct ext4_extent_header *eh;
426 struct ext4_extent *ex;
427 int i;
428
429 if (!path)
430 return;
431
432 eh = path[depth].p_hdr;
433 ex = EXT_FIRST_EXTENT(eh);
434
Mingming553f9002009-09-18 13:34:55 -0400435 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
436
Alex Tomasa86c6182006-10-11 01:21:03 -0700437 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400438 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
439 ext4_ext_is_uninitialized(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400440 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700441 }
442 ext_debug("\n");
443}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400444
445static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
446 ext4_fsblk_t newblock, int level)
447{
448 int depth = ext_depth(inode);
449 struct ext4_extent *ex;
450
451 if (depth != level) {
452 struct ext4_extent_idx *idx;
453 idx = path[level].p_idx;
454 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
455 ext_debug("%d: move %d:%llu in new index %llu\n", level,
456 le32_to_cpu(idx->ei_block),
457 ext4_idx_pblock(idx),
458 newblock);
459 idx++;
460 }
461
462 return;
463 }
464
465 ex = path[depth].p_ext;
466 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
467 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
468 le32_to_cpu(ex->ee_block),
469 ext4_ext_pblock(ex),
470 ext4_ext_is_uninitialized(ex),
471 ext4_ext_get_actual_len(ex),
472 newblock);
473 ex++;
474 }
475}
476
Alex Tomasa86c6182006-10-11 01:21:03 -0700477#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400478#define ext4_ext_show_path(inode, path)
479#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400480#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700481#endif
482
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500483void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700484{
485 int depth = path->p_depth;
486 int i;
487
488 for (i = 0; i <= depth; i++, path++)
489 if (path->p_bh) {
490 brelse(path->p_bh);
491 path->p_bh = NULL;
492 }
493}
494
495/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700496 * ext4_ext_binsearch_idx:
497 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400498 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700499 */
500static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500501ext4_ext_binsearch_idx(struct inode *inode,
502 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700503{
504 struct ext4_extent_header *eh = path->p_hdr;
505 struct ext4_extent_idx *r, *l, *m;
506
Alex Tomasa86c6182006-10-11 01:21:03 -0700507
Eric Sandeenbba90742008-01-28 23:58:27 -0500508 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700509
510 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400511 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700512 while (l <= r) {
513 m = l + (r - l) / 2;
514 if (block < le32_to_cpu(m->ei_block))
515 r = m - 1;
516 else
517 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400518 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
519 m, le32_to_cpu(m->ei_block),
520 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700521 }
522
523 path->p_idx = l - 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700524 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400525 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700526
527#ifdef CHECK_BINSEARCH
528 {
529 struct ext4_extent_idx *chix, *ix;
530 int k;
531
532 chix = ix = EXT_FIRST_INDEX(eh);
533 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
534 if (k != 0 &&
535 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400536 printk(KERN_DEBUG "k=%d, ix=0x%p, "
537 "first=0x%p\n", k,
538 ix, EXT_FIRST_INDEX(eh));
539 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700540 le32_to_cpu(ix->ei_block),
541 le32_to_cpu(ix[-1].ei_block));
542 }
543 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400544 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700545 if (block < le32_to_cpu(ix->ei_block))
546 break;
547 chix = ix;
548 }
549 BUG_ON(chix != path->p_idx);
550 }
551#endif
552
553}
554
555/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700556 * ext4_ext_binsearch:
557 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400558 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700559 */
560static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500561ext4_ext_binsearch(struct inode *inode,
562 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700563{
564 struct ext4_extent_header *eh = path->p_hdr;
565 struct ext4_extent *r, *l, *m;
566
Alex Tomasa86c6182006-10-11 01:21:03 -0700567 if (eh->eh_entries == 0) {
568 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700569 * this leaf is empty:
570 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700571 */
572 return;
573 }
574
Eric Sandeenbba90742008-01-28 23:58:27 -0500575 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700576
577 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400578 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700579
580 while (l <= r) {
581 m = l + (r - l) / 2;
582 if (block < le32_to_cpu(m->ee_block))
583 r = m - 1;
584 else
585 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400586 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
587 m, le32_to_cpu(m->ee_block),
588 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700589 }
590
591 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400592 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400593 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400594 ext4_ext_pblock(path->p_ext),
Mingming553f9002009-09-18 13:34:55 -0400595 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400596 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700597
598#ifdef CHECK_BINSEARCH
599 {
600 struct ext4_extent *chex, *ex;
601 int k;
602
603 chex = ex = EXT_FIRST_EXTENT(eh);
604 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
605 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400606 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700607 if (block < le32_to_cpu(ex->ee_block))
608 break;
609 chex = ex;
610 }
611 BUG_ON(chex != path->p_ext);
612 }
613#endif
614
615}
616
617int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
618{
619 struct ext4_extent_header *eh;
620
621 eh = ext_inode_hdr(inode);
622 eh->eh_depth = 0;
623 eh->eh_entries = 0;
624 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400625 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700626 ext4_mark_inode_dirty(handle, inode);
627 ext4_ext_invalidate_cache(inode);
628 return 0;
629}
630
631struct ext4_ext_path *
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500632ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
633 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700634{
635 struct ext4_extent_header *eh;
636 struct buffer_head *bh;
637 short int depth, i, ppos = 0, alloc = 0;
638
639 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400640 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700641
642 /* account possible depth increase */
643 if (!path) {
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800644 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700645 GFP_NOFS);
646 if (!path)
647 return ERR_PTR(-ENOMEM);
648 alloc = 1;
649 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700650 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400651 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700652
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400653 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700654 /* walk through the tree */
655 while (i) {
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400656 int need_to_validate = 0;
657
Alex Tomasa86c6182006-10-11 01:21:03 -0700658 ext_debug("depth %d: num %d, max %d\n",
659 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400660
Alex Tomasa86c6182006-10-11 01:21:03 -0700661 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400662 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700663 path[ppos].p_depth = i;
664 path[ppos].p_ext = NULL;
665
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400666 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
667 if (unlikely(!bh))
Alex Tomasa86c6182006-10-11 01:21:03 -0700668 goto err;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400669 if (!bh_uptodate_or_lock(bh)) {
Jiaying Zhang0562e0b2011-03-21 21:38:05 -0400670 trace_ext4_ext_load_extent(inode, block,
671 path[ppos].p_block);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400672 if (bh_submit_read(bh) < 0) {
673 put_bh(bh);
674 goto err;
675 }
676 /* validate the extent entries */
677 need_to_validate = 1;
678 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700679 eh = ext_block_hdr(bh);
680 ppos++;
Frank Mayhar273df552010-03-02 11:46:09 -0500681 if (unlikely(ppos > depth)) {
682 put_bh(bh);
683 EXT4_ERROR_INODE(inode,
684 "ppos %d > depth %d", ppos, depth);
685 goto err;
686 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700687 path[ppos].p_bh = bh;
688 path[ppos].p_hdr = eh;
689 i--;
690
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400691 if (need_to_validate && ext4_ext_check(inode, eh, i))
Alex Tomasa86c6182006-10-11 01:21:03 -0700692 goto err;
693 }
694
695 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700696 path[ppos].p_ext = NULL;
697 path[ppos].p_idx = NULL;
698
Alex Tomasa86c6182006-10-11 01:21:03 -0700699 /* find extent */
700 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400701 /* if not an empty leaf */
702 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400703 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700704
705 ext4_ext_show_path(inode, path);
706
707 return path;
708
709err:
710 ext4_ext_drop_refs(path);
711 if (alloc)
712 kfree(path);
713 return ERR_PTR(-EIO);
714}
715
716/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700717 * ext4_ext_insert_index:
718 * insert new index [@logical;@ptr] into the block at @curp;
719 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700720 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400721static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
722 struct ext4_ext_path *curp,
723 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700724{
725 struct ext4_extent_idx *ix;
726 int len, err;
727
Avantika Mathur7e028972006-12-06 20:41:33 -0800728 err = ext4_ext_get_access(handle, inode, curp);
729 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700730 return err;
731
Frank Mayhar273df552010-03-02 11:46:09 -0500732 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
733 EXT4_ERROR_INODE(inode,
734 "logical %d == ei_block %d!",
735 logical, le32_to_cpu(curp->p_idx->ei_block));
736 return -EIO;
737 }
Robin Dongd4620312011-07-17 23:43:42 -0400738
739 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
740 >= le16_to_cpu(curp->p_hdr->eh_max))) {
741 EXT4_ERROR_INODE(inode,
742 "eh_entries %d >= eh_max %d!",
743 le16_to_cpu(curp->p_hdr->eh_entries),
744 le16_to_cpu(curp->p_hdr->eh_max));
745 return -EIO;
746 }
747
Alex Tomasa86c6182006-10-11 01:21:03 -0700748 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
749 /* insert after */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400750 ext_debug("insert new index %d after: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700751 ix = curp->p_idx + 1;
752 } else {
753 /* insert before */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400754 ext_debug("insert new index %d before: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700755 ix = curp->p_idx;
756 }
757
Eric Gouriou80e675f2011-10-27 11:52:18 -0400758 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
759 BUG_ON(len < 0);
760 if (len > 0) {
761 ext_debug("insert new index %d: "
762 "move %d indices from 0x%p to 0x%p\n",
763 logical, len, ix, ix + 1);
764 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
765 }
766
Tao Maf472e022011-10-17 10:13:46 -0400767 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
768 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
769 return -EIO;
770 }
771
Alex Tomasa86c6182006-10-11 01:21:03 -0700772 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700773 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400774 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700775
Frank Mayhar273df552010-03-02 11:46:09 -0500776 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
777 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
778 return -EIO;
779 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700780
781 err = ext4_ext_dirty(handle, inode, curp);
782 ext4_std_error(inode->i_sb, err);
783
784 return err;
785}
786
787/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700788 * ext4_ext_split:
789 * inserts new subtree into the path, using free index entry
790 * at depth @at:
791 * - allocates all needed blocks (new leaf and all intermediate index blocks)
792 * - makes decision where to split
793 * - moves remaining extents and index entries (right to the split point)
794 * into the newly allocated blocks
795 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700796 */
797static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -0400798 unsigned int flags,
799 struct ext4_ext_path *path,
800 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -0700801{
802 struct buffer_head *bh = NULL;
803 int depth = ext_depth(inode);
804 struct ext4_extent_header *neh;
805 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -0700806 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700807 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700808 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700809 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700810 int err = 0;
811
812 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700813 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700814
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700815 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700816 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -0500817 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
818 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
819 return -EIO;
820 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700821 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
822 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700823 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700824 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400825 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700826 } else {
827 border = newext->ee_block;
828 ext_debug("leaf will be added."
829 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400830 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700831 }
832
833 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700834 * If error occurs, then we break processing
835 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700836 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700837 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700838 */
839
840 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700841 * Get array to track all allocated blocks.
842 * We need this to handle errors and free blocks
843 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700844 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800845 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700846 if (!ablocks)
847 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700848
849 /* allocate all needed blocks */
850 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
851 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400852 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400853 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -0700854 if (newblock == 0)
855 goto cleanup;
856 ablocks[a] = newblock;
857 }
858
859 /* initialize new leaf */
860 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -0500861 if (unlikely(newblock == 0)) {
862 EXT4_ERROR_INODE(inode, "newblock == 0!");
863 err = -EIO;
864 goto cleanup;
865 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700866 bh = sb_getblk(inode->i_sb, newblock);
867 if (!bh) {
868 err = -EIO;
869 goto cleanup;
870 }
871 lock_buffer(bh);
872
Avantika Mathur7e028972006-12-06 20:41:33 -0800873 err = ext4_journal_get_create_access(handle, bh);
874 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700875 goto cleanup;
876
877 neh = ext_block_hdr(bh);
878 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400879 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700880 neh->eh_magic = EXT4_EXT_MAGIC;
881 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700882
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700883 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -0500884 if (unlikely(path[depth].p_hdr->eh_entries !=
885 path[depth].p_hdr->eh_max)) {
886 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
887 path[depth].p_hdr->eh_entries,
888 path[depth].p_hdr->eh_max);
889 err = -EIO;
890 goto cleanup;
891 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700892 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400893 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
894 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -0700895 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400896 struct ext4_extent *ex;
897 ex = EXT_FIRST_EXTENT(neh);
898 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400899 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700900 }
901
902 set_buffer_uptodate(bh);
903 unlock_buffer(bh);
904
Frank Mayhar03901312009-01-07 00:06:22 -0500905 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800906 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700907 goto cleanup;
908 brelse(bh);
909 bh = NULL;
910
911 /* correct old leaf */
912 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800913 err = ext4_ext_get_access(handle, inode, path + depth);
914 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700915 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400916 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800917 err = ext4_ext_dirty(handle, inode, path + depth);
918 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700919 goto cleanup;
920
921 }
922
923 /* create intermediate indexes */
924 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -0500925 if (unlikely(k < 0)) {
926 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
927 err = -EIO;
928 goto cleanup;
929 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700930 if (k)
931 ext_debug("create %d intermediate indices\n", k);
932 /* insert new index into current index block */
933 /* current depth stored in i var */
934 i = depth - 1;
935 while (k--) {
936 oldblock = newblock;
937 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -0500938 bh = sb_getblk(inode->i_sb, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700939 if (!bh) {
940 err = -EIO;
941 goto cleanup;
942 }
943 lock_buffer(bh);
944
Avantika Mathur7e028972006-12-06 20:41:33 -0800945 err = ext4_journal_get_create_access(handle, bh);
946 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700947 goto cleanup;
948
949 neh = ext_block_hdr(bh);
950 neh->eh_entries = cpu_to_le16(1);
951 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400952 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700953 neh->eh_depth = cpu_to_le16(depth - i);
954 fidx = EXT_FIRST_INDEX(neh);
955 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700956 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700957
Eric Sandeenbba90742008-01-28 23:58:27 -0500958 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
959 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700960
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400961 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -0500962 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
963 EXT_LAST_INDEX(path[i].p_hdr))) {
964 EXT4_ERROR_INODE(inode,
965 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
966 le32_to_cpu(path[i].p_ext->ee_block));
967 err = -EIO;
968 goto cleanup;
969 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400970 /* start copy indexes */
971 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
972 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
973 EXT_MAX_INDEX(path[i].p_hdr));
974 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -0700975 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400976 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -0700977 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400978 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700979 }
980 set_buffer_uptodate(bh);
981 unlock_buffer(bh);
982
Frank Mayhar03901312009-01-07 00:06:22 -0500983 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800984 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700985 goto cleanup;
986 brelse(bh);
987 bh = NULL;
988
989 /* correct old index */
990 if (m) {
991 err = ext4_ext_get_access(handle, inode, path + i);
992 if (err)
993 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400994 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700995 err = ext4_ext_dirty(handle, inode, path + i);
996 if (err)
997 goto cleanup;
998 }
999
1000 i--;
1001 }
1002
1003 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001004 err = ext4_ext_insert_index(handle, inode, path + at,
1005 le32_to_cpu(border), newblock);
1006
1007cleanup:
1008 if (bh) {
1009 if (buffer_locked(bh))
1010 unlock_buffer(bh);
1011 brelse(bh);
1012 }
1013
1014 if (err) {
1015 /* free all allocated blocks in error case */
1016 for (i = 0; i < depth; i++) {
1017 if (!ablocks[i])
1018 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001019 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001020 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001021 }
1022 }
1023 kfree(ablocks);
1024
1025 return err;
1026}
1027
1028/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001029 * ext4_ext_grow_indepth:
1030 * implements tree growing procedure:
1031 * - allocates new block
1032 * - moves top-level data (index block or leaf) into the new block
1033 * - initializes new top-level, creating index that points to the
1034 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001035 */
1036static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001037 unsigned int flags,
Allison Henderson55f020d2011-05-25 07:41:26 -04001038 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001039{
Alex Tomasa86c6182006-10-11 01:21:03 -07001040 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001041 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001042 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001043 int err = 0;
1044
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001045 newblock = ext4_ext_new_meta_block(handle, inode, NULL,
Allison Henderson55f020d2011-05-25 07:41:26 -04001046 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001047 if (newblock == 0)
1048 return err;
1049
1050 bh = sb_getblk(inode->i_sb, newblock);
1051 if (!bh) {
1052 err = -EIO;
1053 ext4_std_error(inode->i_sb, err);
1054 return err;
1055 }
1056 lock_buffer(bh);
1057
Avantika Mathur7e028972006-12-06 20:41:33 -08001058 err = ext4_journal_get_create_access(handle, bh);
1059 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001060 unlock_buffer(bh);
1061 goto out;
1062 }
1063
1064 /* move top-level index/leaf into new block */
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001065 memmove(bh->b_data, EXT4_I(inode)->i_data,
1066 sizeof(EXT4_I(inode)->i_data));
Alex Tomasa86c6182006-10-11 01:21:03 -07001067
1068 /* set size of new block */
1069 neh = ext_block_hdr(bh);
1070 /* old root could have indexes or leaves
1071 * so calculate e_max right way */
1072 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001073 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001074 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001075 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001076 neh->eh_magic = EXT4_EXT_MAGIC;
1077 set_buffer_uptodate(bh);
1078 unlock_buffer(bh);
1079
Frank Mayhar03901312009-01-07 00:06:22 -05001080 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001081 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001082 goto out;
1083
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001084 /* Update top-level index: num,max,pointer */
Alex Tomasa86c6182006-10-11 01:21:03 -07001085 neh = ext_inode_hdr(inode);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001086 neh->eh_entries = cpu_to_le16(1);
1087 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1088 if (neh->eh_depth == 0) {
1089 /* Root extent block becomes index block */
1090 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1091 EXT_FIRST_INDEX(neh)->ei_block =
1092 EXT_FIRST_EXTENT(neh)->ee_block;
1093 }
Mingming Cao2ae02102006-10-11 01:21:11 -07001094 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001095 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001096 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001097 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001098
Paul Mackerrasb4611ab2011-12-12 11:00:56 -05001099 neh->eh_depth = cpu_to_le16(le16_to_cpu(neh->eh_depth) + 1);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001100 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07001101out:
1102 brelse(bh);
1103
1104 return err;
1105}
1106
1107/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001108 * ext4_ext_create_new_leaf:
1109 * finds empty index and adds new leaf.
1110 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001111 */
1112static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001113 unsigned int flags,
1114 struct ext4_ext_path *path,
1115 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001116{
1117 struct ext4_ext_path *curp;
1118 int depth, i, err = 0;
1119
1120repeat:
1121 i = depth = ext_depth(inode);
1122
1123 /* walk up to the tree and look for free index entry */
1124 curp = path + depth;
1125 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1126 i--;
1127 curp--;
1128 }
1129
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001130 /* we use already allocated block for index block,
1131 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001132 if (EXT_HAS_FREE_INDEX(curp)) {
1133 /* if we found index with free entry, then use that
1134 * entry: create all needed subtree and add new leaf */
Allison Henderson55f020d2011-05-25 07:41:26 -04001135 err = ext4_ext_split(handle, inode, flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001136 if (err)
1137 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001138
1139 /* refill path */
1140 ext4_ext_drop_refs(path);
1141 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001142 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1143 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001144 if (IS_ERR(path))
1145 err = PTR_ERR(path);
1146 } else {
1147 /* tree is full, time to grow in depth */
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001148 err = ext4_ext_grow_indepth(handle, inode, flags, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001149 if (err)
1150 goto out;
1151
1152 /* refill path */
1153 ext4_ext_drop_refs(path);
1154 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001155 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1156 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001157 if (IS_ERR(path)) {
1158 err = PTR_ERR(path);
1159 goto out;
1160 }
1161
1162 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001163 * only first (depth 0 -> 1) produces free space;
1164 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001165 */
1166 depth = ext_depth(inode);
1167 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001168 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001169 goto repeat;
1170 }
1171 }
1172
1173out:
1174 return err;
1175}
1176
1177/*
Alex Tomas1988b512008-01-28 23:58:27 -05001178 * search the closest allocated block to the left for *logical
1179 * and returns it at @logical + it's physical address at @phys
1180 * if *logical is the smallest allocated block, the function
1181 * returns 0 at @phys
1182 * return value contains 0 (success) or error code
1183 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001184static int ext4_ext_search_left(struct inode *inode,
1185 struct ext4_ext_path *path,
1186 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001187{
1188 struct ext4_extent_idx *ix;
1189 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001190 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001191
Frank Mayhar273df552010-03-02 11:46:09 -05001192 if (unlikely(path == NULL)) {
1193 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1194 return -EIO;
1195 }
Alex Tomas1988b512008-01-28 23:58:27 -05001196 depth = path->p_depth;
1197 *phys = 0;
1198
1199 if (depth == 0 && path->p_ext == NULL)
1200 return 0;
1201
1202 /* usually extent in the path covers blocks smaller
1203 * then *logical, but it can be that extent is the
1204 * first one in the file */
1205
1206 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001207 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001208 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001209 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1210 EXT4_ERROR_INODE(inode,
1211 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1212 *logical, le32_to_cpu(ex->ee_block));
1213 return -EIO;
1214 }
Alex Tomas1988b512008-01-28 23:58:27 -05001215 while (--depth >= 0) {
1216 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001217 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1218 EXT4_ERROR_INODE(inode,
1219 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
Tao Ma6ee3b212011-10-08 16:08:34 -04001220 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001221 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
Tao Ma6ee3b212011-10-08 16:08:34 -04001222 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001223 depth);
1224 return -EIO;
1225 }
Alex Tomas1988b512008-01-28 23:58:27 -05001226 }
1227 return 0;
1228 }
1229
Frank Mayhar273df552010-03-02 11:46:09 -05001230 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1231 EXT4_ERROR_INODE(inode,
1232 "logical %d < ee_block %d + ee_len %d!",
1233 *logical, le32_to_cpu(ex->ee_block), ee_len);
1234 return -EIO;
1235 }
Alex Tomas1988b512008-01-28 23:58:27 -05001236
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001237 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001238 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001239 return 0;
1240}
1241
1242/*
1243 * search the closest allocated block to the right for *logical
1244 * and returns it at @logical + it's physical address at @phys
Tao Madf3ab172011-10-08 15:53:49 -04001245 * if *logical is the largest allocated block, the function
Alex Tomas1988b512008-01-28 23:58:27 -05001246 * returns 0 at @phys
1247 * return value contains 0 (success) or error code
1248 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001249static int ext4_ext_search_right(struct inode *inode,
1250 struct ext4_ext_path *path,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001251 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1252 struct ext4_extent **ret_ex)
Alex Tomas1988b512008-01-28 23:58:27 -05001253{
1254 struct buffer_head *bh = NULL;
1255 struct ext4_extent_header *eh;
1256 struct ext4_extent_idx *ix;
1257 struct ext4_extent *ex;
1258 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001259 int depth; /* Note, NOT eh_depth; depth from top of tree */
1260 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001261
Frank Mayhar273df552010-03-02 11:46:09 -05001262 if (unlikely(path == NULL)) {
1263 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1264 return -EIO;
1265 }
Alex Tomas1988b512008-01-28 23:58:27 -05001266 depth = path->p_depth;
1267 *phys = 0;
1268
1269 if (depth == 0 && path->p_ext == NULL)
1270 return 0;
1271
1272 /* usually extent in the path covers blocks smaller
1273 * then *logical, but it can be that extent is the
1274 * first one in the file */
1275
1276 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001277 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001278 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001279 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1280 EXT4_ERROR_INODE(inode,
1281 "first_extent(path[%d].p_hdr) != ex",
1282 depth);
1283 return -EIO;
1284 }
Alex Tomas1988b512008-01-28 23:58:27 -05001285 while (--depth >= 0) {
1286 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001287 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1288 EXT4_ERROR_INODE(inode,
1289 "ix != EXT_FIRST_INDEX *logical %d!",
1290 *logical);
1291 return -EIO;
1292 }
Alex Tomas1988b512008-01-28 23:58:27 -05001293 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001294 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001295 }
1296
Frank Mayhar273df552010-03-02 11:46:09 -05001297 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1298 EXT4_ERROR_INODE(inode,
1299 "logical %d < ee_block %d + ee_len %d!",
1300 *logical, le32_to_cpu(ex->ee_block), ee_len);
1301 return -EIO;
1302 }
Alex Tomas1988b512008-01-28 23:58:27 -05001303
1304 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1305 /* next allocated block in this leaf */
1306 ex++;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001307 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001308 }
1309
1310 /* go up and search for index to the right */
1311 while (--depth >= 0) {
1312 ix = path[depth].p_idx;
1313 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001314 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001315 }
1316
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001317 /* we've gone up to the root and found no index to the right */
1318 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001319
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001320got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001321 /* we've found index to the right, let's
1322 * follow it and find the closest allocated
1323 * block to the right */
1324 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001325 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001326 while (++depth < path->p_depth) {
1327 bh = sb_bread(inode->i_sb, block);
1328 if (bh == NULL)
1329 return -EIO;
1330 eh = ext_block_hdr(bh);
Eric Sandeen395a87b2009-03-10 18:18:47 -04001331 /* subtract from p_depth to get proper eh_depth */
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001332 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001333 put_bh(bh);
1334 return -EIO;
1335 }
1336 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001337 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001338 put_bh(bh);
1339 }
1340
1341 bh = sb_bread(inode->i_sb, block);
1342 if (bh == NULL)
1343 return -EIO;
1344 eh = ext_block_hdr(bh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001345 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001346 put_bh(bh);
1347 return -EIO;
1348 }
1349 ex = EXT_FIRST_EXTENT(eh);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001350found_extent:
Alex Tomas1988b512008-01-28 23:58:27 -05001351 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001352 *phys = ext4_ext_pblock(ex);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001353 *ret_ex = ex;
1354 if (bh)
1355 put_bh(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001356 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001357}
1358
1359/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001360 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001361 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001362 * NOTE: it considers block number from index entry as
1363 * allocated block. Thus, index entries have to be consistent
1364 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001365 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001366static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001367ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1368{
1369 int depth;
1370
1371 BUG_ON(path == NULL);
1372 depth = path->p_depth;
1373
1374 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001375 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001376
1377 while (depth >= 0) {
1378 if (depth == path->p_depth) {
1379 /* leaf */
Curt Wohlgemuth6f8ff532011-10-26 04:38:59 -04001380 if (path[depth].p_ext &&
1381 path[depth].p_ext !=
Alex Tomasa86c6182006-10-11 01:21:03 -07001382 EXT_LAST_EXTENT(path[depth].p_hdr))
1383 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1384 } else {
1385 /* index */
1386 if (path[depth].p_idx !=
1387 EXT_LAST_INDEX(path[depth].p_hdr))
1388 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1389 }
1390 depth--;
1391 }
1392
Lukas Czernerf17722f2011-06-06 00:05:17 -04001393 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001394}
1395
1396/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001397 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001398 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001399 */
Robin Dong57187892011-07-23 21:49:07 -04001400static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001401{
1402 int depth;
1403
1404 BUG_ON(path == NULL);
1405 depth = path->p_depth;
1406
1407 /* zero-tree has no leaf blocks at all */
1408 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001409 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001410
1411 /* go to index block */
1412 depth--;
1413
1414 while (depth >= 0) {
1415 if (path[depth].p_idx !=
1416 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001417 return (ext4_lblk_t)
1418 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001419 depth--;
1420 }
1421
Lukas Czernerf17722f2011-06-06 00:05:17 -04001422 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001423}
1424
1425/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001426 * ext4_ext_correct_indexes:
1427 * if leaf gets modified and modified extent is first in the leaf,
1428 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001429 * TODO: do we need to correct tree in all cases?
1430 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001431static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001432 struct ext4_ext_path *path)
1433{
1434 struct ext4_extent_header *eh;
1435 int depth = ext_depth(inode);
1436 struct ext4_extent *ex;
1437 __le32 border;
1438 int k, err = 0;
1439
1440 eh = path[depth].p_hdr;
1441 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001442
1443 if (unlikely(ex == NULL || eh == NULL)) {
1444 EXT4_ERROR_INODE(inode,
1445 "ex %p == NULL or eh %p == NULL", ex, eh);
1446 return -EIO;
1447 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001448
1449 if (depth == 0) {
1450 /* there is no tree at all */
1451 return 0;
1452 }
1453
1454 if (ex != EXT_FIRST_EXTENT(eh)) {
1455 /* we correct tree if first leaf got modified only */
1456 return 0;
1457 }
1458
1459 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001460 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001461 */
1462 k = depth - 1;
1463 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001464 err = ext4_ext_get_access(handle, inode, path + k);
1465 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001466 return err;
1467 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001468 err = ext4_ext_dirty(handle, inode, path + k);
1469 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001470 return err;
1471
1472 while (k--) {
1473 /* change all left-side indexes */
1474 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1475 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001476 err = ext4_ext_get_access(handle, inode, path + k);
1477 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001478 break;
1479 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001480 err = ext4_ext_dirty(handle, inode, path + k);
1481 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001482 break;
1483 }
1484
1485 return err;
1486}
1487
Akira Fujita748de672009-06-17 19:24:03 -04001488int
Alex Tomasa86c6182006-10-11 01:21:03 -07001489ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1490 struct ext4_extent *ex2)
1491{
Amit Arora749269f2007-07-18 09:02:56 -04001492 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001493
1494 /*
1495 * Make sure that either both extents are uninitialized, or
1496 * both are _not_.
1497 */
1498 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1499 return 0;
1500
Amit Arora749269f2007-07-18 09:02:56 -04001501 if (ext4_ext_is_uninitialized(ex1))
1502 max_len = EXT_UNINIT_MAX_LEN;
1503 else
1504 max_len = EXT_INIT_MAX_LEN;
1505
Amit Aroraa2df2a62007-07-17 21:42:41 -04001506 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1507 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1508
1509 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001510 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001511 return 0;
1512
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001513 /*
1514 * To allow future support for preallocated extents to be added
1515 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001516 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001517 */
Amit Arora749269f2007-07-18 09:02:56 -04001518 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001519 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001520#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001521 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001522 return 0;
1523#endif
1524
Theodore Ts'obf89d162010-10-27 21:30:14 -04001525 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001526 return 1;
1527 return 0;
1528}
1529
1530/*
Amit Arora56055d32007-07-17 21:42:38 -04001531 * This function tries to merge the "ex" extent to the next extent in the tree.
1532 * It always tries to merge towards right. If you want to merge towards
1533 * left, pass "ex - 1" as argument instead of "ex".
1534 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1535 * 1 if they got merged.
1536 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001537static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001538 struct ext4_ext_path *path,
1539 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001540{
1541 struct ext4_extent_header *eh;
1542 unsigned int depth, len;
1543 int merge_done = 0;
1544 int uninitialized = 0;
1545
1546 depth = ext_depth(inode);
1547 BUG_ON(path[depth].p_hdr == NULL);
1548 eh = path[depth].p_hdr;
1549
1550 while (ex < EXT_LAST_EXTENT(eh)) {
1551 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1552 break;
1553 /* merge with next extent! */
1554 if (ext4_ext_is_uninitialized(ex))
1555 uninitialized = 1;
1556 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1557 + ext4_ext_get_actual_len(ex + 1));
1558 if (uninitialized)
1559 ext4_ext_mark_uninitialized(ex);
1560
1561 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1562 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1563 * sizeof(struct ext4_extent);
1564 memmove(ex + 1, ex + 2, len);
1565 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001566 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001567 merge_done = 1;
1568 WARN_ON(eh->eh_entries == 0);
1569 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001570 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001571 }
1572
1573 return merge_done;
1574}
1575
1576/*
Yongqiang Yang197217a2011-05-03 11:45:29 -04001577 * This function tries to merge the @ex extent to neighbours in the tree.
1578 * return 1 if merge left else 0.
1579 */
1580static int ext4_ext_try_to_merge(struct inode *inode,
1581 struct ext4_ext_path *path,
1582 struct ext4_extent *ex) {
1583 struct ext4_extent_header *eh;
1584 unsigned int depth;
1585 int merge_done = 0;
1586 int ret = 0;
1587
1588 depth = ext_depth(inode);
1589 BUG_ON(path[depth].p_hdr == NULL);
1590 eh = path[depth].p_hdr;
1591
1592 if (ex > EXT_FIRST_EXTENT(eh))
1593 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1594
1595 if (!merge_done)
1596 ret = ext4_ext_try_to_merge_right(inode, path, ex);
1597
1598 return ret;
1599}
1600
1601/*
Amit Arora25d14f92007-05-24 13:04:13 -04001602 * check if a portion of the "newext" extent overlaps with an
1603 * existing extent.
1604 *
1605 * If there is an overlap discovered, it updates the length of the newext
1606 * such that there will be no overlap, and then returns 1.
1607 * If there is no overlap found, it returns 0.
1608 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001609static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1610 struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001611 struct ext4_extent *newext,
1612 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001613{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001614 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001615 unsigned int depth, len1;
1616 unsigned int ret = 0;
1617
1618 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001619 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001620 depth = ext_depth(inode);
1621 if (!path[depth].p_ext)
1622 goto out;
1623 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001624 b2 &= ~(sbi->s_cluster_ratio - 1);
Amit Arora25d14f92007-05-24 13:04:13 -04001625
1626 /*
1627 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001628 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001629 */
1630 if (b2 < b1) {
1631 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001632 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001633 goto out;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001634 b2 &= ~(sbi->s_cluster_ratio - 1);
Amit Arora25d14f92007-05-24 13:04:13 -04001635 }
1636
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001637 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001638 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001639 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001640 newext->ee_len = cpu_to_le16(len1);
1641 ret = 1;
1642 }
1643
1644 /* check for overlap */
1645 if (b1 + len1 > b2) {
1646 newext->ee_len = cpu_to_le16(b2 - b1);
1647 ret = 1;
1648 }
1649out:
1650 return ret;
1651}
1652
1653/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001654 * ext4_ext_insert_extent:
1655 * tries to merge requsted extent into the existing extent or
1656 * inserts requested extent as new one into the tree,
1657 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001658 */
1659int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1660 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04001661 struct ext4_extent *newext, int flag)
Alex Tomasa86c6182006-10-11 01:21:03 -07001662{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001663 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001664 struct ext4_extent *ex, *fex;
1665 struct ext4_extent *nearex; /* nearest extent */
1666 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001667 int depth, len, err;
1668 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001669 unsigned uninitialized = 0;
Allison Henderson55f020d2011-05-25 07:41:26 -04001670 int flags = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001671
Frank Mayhar273df552010-03-02 11:46:09 -05001672 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1673 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1674 return -EIO;
1675 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001676 depth = ext_depth(inode);
1677 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001678 if (unlikely(path[depth].p_hdr == NULL)) {
1679 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1680 return -EIO;
1681 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001682
1683 /* try to insert block into found extent and return */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001684 if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
Mingming Cao00314622009-09-28 15:49:08 -04001685 && ext4_can_extents_be_merged(inode, ex, newext)) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04001686 ext_debug("append [%d]%d block to %u:[%d]%d (from %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04001687 ext4_ext_is_uninitialized(newext),
1688 ext4_ext_get_actual_len(newext),
1689 le32_to_cpu(ex->ee_block),
1690 ext4_ext_is_uninitialized(ex),
1691 ext4_ext_get_actual_len(ex),
1692 ext4_ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001693 err = ext4_ext_get_access(handle, inode, path + depth);
1694 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001695 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001696
1697 /*
1698 * ext4_can_extents_be_merged should have checked that either
1699 * both extents are uninitialized, or both aren't. Thus we
1700 * need to check only one of them here.
1701 */
1702 if (ext4_ext_is_uninitialized(ex))
1703 uninitialized = 1;
1704 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1705 + ext4_ext_get_actual_len(newext));
1706 if (uninitialized)
1707 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001708 eh = path[depth].p_hdr;
1709 nearex = ex;
1710 goto merge;
1711 }
1712
Alex Tomasa86c6182006-10-11 01:21:03 -07001713 depth = ext_depth(inode);
1714 eh = path[depth].p_hdr;
1715 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1716 goto has_space;
1717
1718 /* probably next leaf has space for us? */
1719 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04001720 next = EXT_MAX_BLOCKS;
1721 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
Robin Dong57187892011-07-23 21:49:07 -04001722 next = ext4_ext_next_leaf_block(path);
Robin Dong598dbdf2011-07-11 18:24:01 -04001723 if (next != EXT_MAX_BLOCKS) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04001724 ext_debug("next leaf block - %u\n", next);
Alex Tomasa86c6182006-10-11 01:21:03 -07001725 BUG_ON(npath != NULL);
1726 npath = ext4_ext_find_extent(inode, next, NULL);
1727 if (IS_ERR(npath))
1728 return PTR_ERR(npath);
1729 BUG_ON(npath->p_depth != path->p_depth);
1730 eh = npath[depth].p_hdr;
1731 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001732 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001733 le16_to_cpu(eh->eh_entries));
1734 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04001735 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07001736 }
1737 ext_debug("next leaf has no free space(%d,%d)\n",
1738 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1739 }
1740
1741 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001742 * There is no free space in the found leaf.
1743 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001744 */
Allison Henderson55f020d2011-05-25 07:41:26 -04001745 if (flag & EXT4_GET_BLOCKS_PUNCH_OUT_EXT)
1746 flags = EXT4_MB_USE_ROOT_BLOCKS;
1747 err = ext4_ext_create_new_leaf(handle, inode, flags, path, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001748 if (err)
1749 goto cleanup;
1750 depth = ext_depth(inode);
1751 eh = path[depth].p_hdr;
1752
1753has_space:
1754 nearex = path[depth].p_ext;
1755
Avantika Mathur7e028972006-12-06 20:41:33 -08001756 err = ext4_ext_get_access(handle, inode, path + depth);
1757 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001758 goto cleanup;
1759
1760 if (!nearex) {
1761 /* there is no extent in this leaf, create first one */
Yongqiang Yang32de6752011-11-01 18:56:41 -04001762 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001763 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001764 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001765 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001766 ext4_ext_get_actual_len(newext));
Eric Gouriou80e675f2011-10-27 11:52:18 -04001767 nearex = EXT_FIRST_EXTENT(eh);
1768 } else {
1769 if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001770 > le32_to_cpu(nearex->ee_block)) {
Eric Gouriou80e675f2011-10-27 11:52:18 -04001771 /* Insert after */
Yongqiang Yang32de6752011-11-01 18:56:41 -04001772 ext_debug("insert %u:%llu:[%d]%d before: "
1773 "nearest %p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001774 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001775 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001776 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001777 ext4_ext_get_actual_len(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04001778 nearex);
1779 nearex++;
1780 } else {
1781 /* Insert before */
1782 BUG_ON(newext->ee_block == nearex->ee_block);
Yongqiang Yang32de6752011-11-01 18:56:41 -04001783 ext_debug("insert %u:%llu:[%d]%d after: "
1784 "nearest %p\n",
Eric Gouriou80e675f2011-10-27 11:52:18 -04001785 le32_to_cpu(newext->ee_block),
1786 ext4_ext_pblock(newext),
1787 ext4_ext_is_uninitialized(newext),
1788 ext4_ext_get_actual_len(newext),
1789 nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001790 }
Eric Gouriou80e675f2011-10-27 11:52:18 -04001791 len = EXT_LAST_EXTENT(eh) - nearex + 1;
1792 if (len > 0) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04001793 ext_debug("insert %u:%llu:[%d]%d: "
Eric Gouriou80e675f2011-10-27 11:52:18 -04001794 "move %d extents from 0x%p to 0x%p\n",
1795 le32_to_cpu(newext->ee_block),
1796 ext4_ext_pblock(newext),
1797 ext4_ext_is_uninitialized(newext),
1798 ext4_ext_get_actual_len(newext),
1799 len, nearex, nearex + 1);
1800 memmove(nearex + 1, nearex,
1801 len * sizeof(struct ext4_extent));
1802 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001803 }
1804
Marcin Slusarze8546d02008-04-17 10:38:59 -04001805 le16_add_cpu(&eh->eh_entries, 1);
Eric Gouriou80e675f2011-10-27 11:52:18 -04001806 path[depth].p_ext = nearex;
Alex Tomasa86c6182006-10-11 01:21:03 -07001807 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001808 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001809 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001810
1811merge:
1812 /* try to merge extents to the right */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001813 if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
Mingming Cao00314622009-09-28 15:49:08 -04001814 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001815
1816 /* try to merge extents to the left */
1817
1818 /* time to correct all indexes above */
1819 err = ext4_ext_correct_indexes(handle, inode, path);
1820 if (err)
1821 goto cleanup;
1822
1823 err = ext4_ext_dirty(handle, inode, path + depth);
1824
1825cleanup:
1826 if (npath) {
1827 ext4_ext_drop_refs(npath);
1828 kfree(npath);
1829 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001830 ext4_ext_invalidate_cache(inode);
1831 return err;
1832}
1833
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001834static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1835 ext4_lblk_t num, ext_prepare_callback func,
1836 void *cbdata)
Eric Sandeen6873fa02008-10-07 00:46:36 -04001837{
1838 struct ext4_ext_path *path = NULL;
1839 struct ext4_ext_cache cbex;
1840 struct ext4_extent *ex;
1841 ext4_lblk_t next, start = 0, end = 0;
1842 ext4_lblk_t last = block + num;
1843 int depth, exists, err = 0;
1844
1845 BUG_ON(func == NULL);
1846 BUG_ON(inode == NULL);
1847
Lukas Czernerf17722f2011-06-06 00:05:17 -04001848 while (block < last && block != EXT_MAX_BLOCKS) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04001849 num = last - block;
1850 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001851 down_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001852 path = ext4_ext_find_extent(inode, block, path);
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001853 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001854 if (IS_ERR(path)) {
1855 err = PTR_ERR(path);
1856 path = NULL;
1857 break;
1858 }
1859
1860 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05001861 if (unlikely(path[depth].p_hdr == NULL)) {
1862 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1863 err = -EIO;
1864 break;
1865 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04001866 ex = path[depth].p_ext;
1867 next = ext4_ext_next_allocated_block(path);
1868
1869 exists = 0;
1870 if (!ex) {
1871 /* there is no extent yet, so try to allocate
1872 * all requested space */
1873 start = block;
1874 end = block + num;
1875 } else if (le32_to_cpu(ex->ee_block) > block) {
1876 /* need to allocate space before found extent */
1877 start = block;
1878 end = le32_to_cpu(ex->ee_block);
1879 if (block + num < end)
1880 end = block + num;
1881 } else if (block >= le32_to_cpu(ex->ee_block)
1882 + ext4_ext_get_actual_len(ex)) {
1883 /* need to allocate space after found extent */
1884 start = block;
1885 end = block + num;
1886 if (end >= next)
1887 end = next;
1888 } else if (block >= le32_to_cpu(ex->ee_block)) {
1889 /*
1890 * some part of requested space is covered
1891 * by found extent
1892 */
1893 start = block;
1894 end = le32_to_cpu(ex->ee_block)
1895 + ext4_ext_get_actual_len(ex);
1896 if (block + num < end)
1897 end = block + num;
1898 exists = 1;
1899 } else {
1900 BUG();
1901 }
1902 BUG_ON(end <= start);
1903
1904 if (!exists) {
1905 cbex.ec_block = start;
1906 cbex.ec_len = end - start;
1907 cbex.ec_start = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04001908 } else {
1909 cbex.ec_block = le32_to_cpu(ex->ee_block);
1910 cbex.ec_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001911 cbex.ec_start = ext4_ext_pblock(ex);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001912 }
1913
Frank Mayhar273df552010-03-02 11:46:09 -05001914 if (unlikely(cbex.ec_len == 0)) {
1915 EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
1916 err = -EIO;
1917 break;
1918 }
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04001919 err = func(inode, next, &cbex, ex, cbdata);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001920 ext4_ext_drop_refs(path);
1921
1922 if (err < 0)
1923 break;
1924
1925 if (err == EXT_REPEAT)
1926 continue;
1927 else if (err == EXT_BREAK) {
1928 err = 0;
1929 break;
1930 }
1931
1932 if (ext_depth(inode) != depth) {
1933 /* depth was changed. we have to realloc path */
1934 kfree(path);
1935 path = NULL;
1936 }
1937
1938 block = cbex.ec_block + cbex.ec_len;
1939 }
1940
1941 if (path) {
1942 ext4_ext_drop_refs(path);
1943 kfree(path);
1944 }
1945
1946 return err;
1947}
1948
Avantika Mathur09b88252006-12-06 20:41:36 -08001949static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001950ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05001951 __u32 len, ext4_fsblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07001952{
1953 struct ext4_ext_cache *cex;
1954 BUG_ON(len == 0);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001955 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Aditya Kalid8990242011-09-09 19:18:51 -04001956 trace_ext4_ext_put_in_cache(inode, block, len, start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001957 cex = &EXT4_I(inode)->i_cached_extent;
Alex Tomasa86c6182006-10-11 01:21:03 -07001958 cex->ec_block = block;
1959 cex->ec_len = len;
1960 cex->ec_start = start;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001961 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001962}
1963
1964/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001965 * ext4_ext_put_gap_in_cache:
1966 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07001967 * and cache this gap
1968 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001969static void
Alex Tomasa86c6182006-10-11 01:21:03 -07001970ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001971 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07001972{
1973 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001974 unsigned long len;
1975 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001976 struct ext4_extent *ex;
1977
1978 ex = path[depth].p_ext;
1979 if (ex == NULL) {
1980 /* there is no extent yet, so gap is [0;-] */
1981 lblock = 0;
Lukas Czernerf17722f2011-06-06 00:05:17 -04001982 len = EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001983 ext_debug("cache gap(whole file):");
1984 } else if (block < le32_to_cpu(ex->ee_block)) {
1985 lblock = block;
1986 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05001987 ext_debug("cache gap(before): %u [%u:%u]",
1988 block,
1989 le32_to_cpu(ex->ee_block),
1990 ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07001991 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001992 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001993 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001994 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04001995 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001996
1997 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05001998 ext_debug("cache gap(after): [%u:%u] %u",
1999 le32_to_cpu(ex->ee_block),
2000 ext4_ext_get_actual_len(ex),
2001 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002002 BUG_ON(next == lblock);
2003 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07002004 } else {
2005 lblock = len = 0;
2006 BUG();
2007 }
2008
Eric Sandeenbba90742008-01-28 23:58:27 -05002009 ext_debug(" -> %u:%lu\n", lblock, len);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002010 ext4_ext_put_in_cache(inode, lblock, len, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002011}
2012
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002013/*
Robin Dongb7ca1e82011-07-23 21:53:25 -04002014 * ext4_ext_check_cache()
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002015 * Checks to see if the given block is in the cache.
2016 * If it is, the cached extent is stored in the given
2017 * cache extent pointer. If the cached extent is a hole,
2018 * this routine should be used instead of
2019 * ext4_ext_in_cache if the calling function needs to
2020 * know the size of the hole.
2021 *
2022 * @inode: The files inode
2023 * @block: The block to look for in the cache
2024 * @ex: Pointer where the cached extent will be stored
2025 * if it contains block
2026 *
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002027 * Return 0 if cache is invalid; 1 if the cache is valid
2028 */
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002029static int ext4_ext_check_cache(struct inode *inode, ext4_lblk_t block,
2030 struct ext4_ext_cache *ex){
Alex Tomasa86c6182006-10-11 01:21:03 -07002031 struct ext4_ext_cache *cex;
Vivek Haldar77f41352011-05-22 21:24:16 -04002032 struct ext4_sb_info *sbi;
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002033 int ret = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002034
Theodore Ts'o60e66792010-05-17 07:00:00 -04002035 /*
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002036 * We borrow i_block_reservation_lock to protect i_cached_extent
2037 */
2038 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07002039 cex = &EXT4_I(inode)->i_cached_extent;
Vivek Haldar77f41352011-05-22 21:24:16 -04002040 sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002041
2042 /* has cache valid data? */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002043 if (cex->ec_len == 0)
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002044 goto errout;
Alex Tomasa86c6182006-10-11 01:21:03 -07002045
Akinobu Mita731eb1a2010-03-03 23:55:01 -05002046 if (in_range(block, cex->ec_block, cex->ec_len)) {
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002047 memcpy(ex, cex, sizeof(struct ext4_ext_cache));
Eric Sandeenbba90742008-01-28 23:58:27 -05002048 ext_debug("%u cached by %u:%u:%llu\n",
2049 block,
2050 cex->ec_block, cex->ec_len, cex->ec_start);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002051 ret = 1;
Alex Tomasa86c6182006-10-11 01:21:03 -07002052 }
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002053errout:
Vivek Haldar77f41352011-05-22 21:24:16 -04002054 if (!ret)
2055 sbi->extent_cache_misses++;
2056 else
2057 sbi->extent_cache_hits++;
Aditya Kalid8990242011-09-09 19:18:51 -04002058 trace_ext4_ext_in_cache(inode, block, ret);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002059 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
2060 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07002061}
2062
2063/*
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002064 * ext4_ext_in_cache()
2065 * Checks to see if the given block is in the cache.
2066 * If it is, the cached extent is stored in the given
2067 * extent pointer.
2068 *
2069 * @inode: The files inode
2070 * @block: The block to look for in the cache
2071 * @ex: Pointer where the cached extent will be stored
2072 * if it contains block
2073 *
2074 * Return 0 if cache is invalid; 1 if the cache is valid
2075 */
2076static int
2077ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
2078 struct ext4_extent *ex)
2079{
2080 struct ext4_ext_cache cex;
2081 int ret = 0;
2082
2083 if (ext4_ext_check_cache(inode, block, &cex)) {
2084 ex->ee_block = cpu_to_le32(cex.ec_block);
2085 ext4_ext_store_pblock(ex, cex.ec_start);
2086 ex->ee_len = cpu_to_le16(cex.ec_len);
2087 ret = 1;
2088 }
2089
2090 return ret;
2091}
2092
2093
2094/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002095 * ext4_ext_rm_idx:
2096 * removes index from the index block.
Alex Tomasa86c6182006-10-11 01:21:03 -07002097 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002098static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07002099 struct ext4_ext_path *path)
2100{
Alex Tomasa86c6182006-10-11 01:21:03 -07002101 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002102 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002103
2104 /* free index block */
2105 path--;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002106 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002107 if (unlikely(path->p_hdr->eh_entries == 0)) {
2108 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2109 return -EIO;
2110 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002111 err = ext4_ext_get_access(handle, inode, path);
2112 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002113 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002114
2115 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2116 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2117 len *= sizeof(struct ext4_extent_idx);
2118 memmove(path->p_idx, path->p_idx + 1, len);
2119 }
2120
Marcin Slusarze8546d02008-04-17 10:38:59 -04002121 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002122 err = ext4_ext_dirty(handle, inode, path);
2123 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002124 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002125 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Aditya Kalid8990242011-09-09 19:18:51 -04002126 trace_ext4_ext_rm_idx(inode, leaf);
2127
Peter Huewe7dc57612011-02-21 21:01:42 -05002128 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002129 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Alex Tomasa86c6182006-10-11 01:21:03 -07002130 return err;
2131}
2132
2133/*
Mingming Caoee12b632008-08-19 22:16:05 -04002134 * ext4_ext_calc_credits_for_single_extent:
2135 * This routine returns max. credits that needed to insert an extent
2136 * to the extent tree.
2137 * When pass the actual path, the caller should calculate credits
2138 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002139 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002140int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002141 struct ext4_ext_path *path)
2142{
Alex Tomasa86c6182006-10-11 01:21:03 -07002143 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002144 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002145 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002146
Alex Tomasa86c6182006-10-11 01:21:03 -07002147 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002148 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002149 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2150
2151 /*
2152 * There are some space in the leaf tree, no
2153 * need to account for leaf block credit
2154 *
2155 * bitmaps and block group descriptor blocks
Tao Madf3ab172011-10-08 15:53:49 -04002156 * and other metadata blocks still need to be
Mingming Caoee12b632008-08-19 22:16:05 -04002157 * accounted.
2158 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002159 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002160 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002161 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002162 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002163 }
2164
Mingming Cao525f4ed2008-08-19 22:15:58 -04002165 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002166}
Alex Tomasa86c6182006-10-11 01:21:03 -07002167
Mingming Caoee12b632008-08-19 22:16:05 -04002168/*
2169 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2170 *
2171 * if nrblocks are fit in a single extent (chunk flag is 1), then
2172 * in the worse case, each tree level index/leaf need to be changed
2173 * if the tree split due to insert a new extent, then the old tree
2174 * index/leaf need to be updated too
2175 *
2176 * If the nrblocks are discontiguous, they could cause
2177 * the whole tree split more than once, but this is really rare.
2178 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002179int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoee12b632008-08-19 22:16:05 -04002180{
2181 int index;
2182 int depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002183
Mingming Caoee12b632008-08-19 22:16:05 -04002184 if (chunk)
2185 index = depth * 2;
2186 else
2187 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002188
Mingming Caoee12b632008-08-19 22:16:05 -04002189 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002190}
2191
2192static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002193 struct ext4_extent *ex,
2194 ext4_fsblk_t *partial_cluster,
2195 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002196{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002197 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002198 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002199 ext4_fsblk_t pblk;
Theodore Ts'oe6362602009-11-23 07:17:05 -05002200 int flags = EXT4_FREE_BLOCKS_FORGET;
Alex Tomasa86c6182006-10-11 01:21:03 -07002201
Alex Tomasc9de5602008-01-29 00:19:52 -05002202 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
Theodore Ts'oe6362602009-11-23 07:17:05 -05002203 flags |= EXT4_FREE_BLOCKS_METADATA;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002204 /*
2205 * For bigalloc file systems, we never free a partial cluster
2206 * at the beginning of the extent. Instead, we make a note
2207 * that we tried freeing the cluster, and check to see if we
2208 * need to free it on a subsequent call to ext4_remove_blocks,
2209 * or at the end of the ext4_truncate() operation.
2210 */
2211 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
2212
Aditya Kalid8990242011-09-09 19:18:51 -04002213 trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002214 /*
2215 * If we have a partial cluster, and it's different from the
2216 * cluster of the last block, we need to explicitly free the
2217 * partial cluster here.
2218 */
2219 pblk = ext4_ext_pblock(ex) + ee_len - 1;
2220 if (*partial_cluster && (EXT4_B2C(sbi, pblk) != *partial_cluster)) {
2221 ext4_free_blocks(handle, inode, NULL,
2222 EXT4_C2B(sbi, *partial_cluster),
2223 sbi->s_cluster_ratio, flags);
2224 *partial_cluster = 0;
2225 }
2226
Alex Tomasa86c6182006-10-11 01:21:03 -07002227#ifdef EXTENTS_STATS
2228 {
2229 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002230 spin_lock(&sbi->s_ext_stats_lock);
2231 sbi->s_ext_blocks += ee_len;
2232 sbi->s_ext_extents++;
2233 if (ee_len < sbi->s_ext_min)
2234 sbi->s_ext_min = ee_len;
2235 if (ee_len > sbi->s_ext_max)
2236 sbi->s_ext_max = ee_len;
2237 if (ext_depth(inode) > sbi->s_depth_max)
2238 sbi->s_depth_max = ext_depth(inode);
2239 spin_unlock(&sbi->s_ext_stats_lock);
2240 }
2241#endif
2242 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002243 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002244 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002245 ext4_lblk_t num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002246
Amit Aroraa2df2a62007-07-17 21:42:41 -04002247 num = le32_to_cpu(ex->ee_block) + ee_len - from;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002248 pblk = ext4_ext_pblock(ex) + ee_len - num;
2249 ext_debug("free last %u blocks starting %llu\n", num, pblk);
2250 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
2251 /*
2252 * If the block range to be freed didn't start at the
2253 * beginning of a cluster, and we removed the entire
2254 * extent, save the partial cluster here, since we
2255 * might need to delete if we determine that the
2256 * truncate operation has removed all of the blocks in
2257 * the cluster.
2258 */
2259 if (pblk & (sbi->s_cluster_ratio - 1) &&
2260 (ee_len == num))
2261 *partial_cluster = EXT4_B2C(sbi, pblk);
2262 else
2263 *partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002264 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002265 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002266 /* head removal */
2267 ext4_lblk_t num;
2268 ext4_fsblk_t start;
2269
2270 num = to - from;
2271 start = ext4_ext_pblock(ex);
2272
2273 ext_debug("free first %u blocks starting %llu\n", num, start);
H Hartley Sweetenee90d572011-10-18 11:01:51 -04002274 ext4_free_blocks(handle, inode, NULL, start, num, flags);
Allison Hendersond583fb82011-05-25 07:41:43 -04002275
Alex Tomasa86c6182006-10-11 01:21:03 -07002276 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002277 printk(KERN_INFO "strange request: removal(2) "
2278 "%u-%u from %u:%u\n",
2279 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002280 }
2281 return 0;
2282}
2283
Allison Hendersond583fb82011-05-25 07:41:43 -04002284
2285/*
2286 * ext4_ext_rm_leaf() Removes the extents associated with the
2287 * blocks appearing between "start" and "end", and splits the extents
2288 * if "start" and "end" appear in the same extent
2289 *
2290 * @handle: The journal handle
2291 * @inode: The files inode
2292 * @path: The path to the leaf
2293 * @start: The first block to remove
2294 * @end: The last block to remove
2295 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002296static int
2297ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002298 struct ext4_ext_path *path, ext4_fsblk_t *partial_cluster,
2299 ext4_lblk_t start, ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002300{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002301 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002302 int err = 0, correct_index = 0;
2303 int depth = ext_depth(inode), credits;
2304 struct ext4_extent_header *eh;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002305 ext4_lblk_t a, b;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002306 unsigned num;
2307 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002308 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002309 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002310 struct ext4_extent *ex;
2311
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002312 /* the header must be checked already in ext4_ext_remove_space() */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002313 ext_debug("truncate since %u in leaf\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002314 if (!path[depth].p_hdr)
2315 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2316 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002317 if (unlikely(path[depth].p_hdr == NULL)) {
2318 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2319 return -EIO;
2320 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002321 /* find where to start removing */
2322 ex = EXT_LAST_EXTENT(eh);
2323
2324 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002325 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002326
Aditya Kalid8990242011-09-09 19:18:51 -04002327 trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster);
2328
Alex Tomasa86c6182006-10-11 01:21:03 -07002329 while (ex >= EXT_FIRST_EXTENT(eh) &&
2330 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002331
2332 if (ext4_ext_is_uninitialized(ex))
2333 uninitialized = 1;
2334 else
2335 uninitialized = 0;
2336
Mingming553f9002009-09-18 13:34:55 -04002337 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2338 uninitialized, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002339 path[depth].p_ext = ex;
2340
2341 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002342 b = ex_ee_block+ex_ee_len - 1 < end ?
2343 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002344
2345 ext_debug(" border %u:%u\n", a, b);
2346
Allison Hendersond583fb82011-05-25 07:41:43 -04002347 /* If this extent is beyond the end of the hole, skip it */
2348 if (end <= ex_ee_block) {
2349 ex--;
2350 ex_ee_block = le32_to_cpu(ex->ee_block);
2351 ex_ee_len = ext4_ext_get_actual_len(ex);
2352 continue;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002353 } else if (b != ex_ee_block + ex_ee_len - 1) {
2354 EXT4_ERROR_INODE(inode," bad truncate %u:%u\n",
2355 start, end);
2356 err = -EIO;
2357 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002358 } else if (a != ex_ee_block) {
2359 /* remove tail of the extent */
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002360 num = a - ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002361 } else {
2362 /* remove whole extent: excellent! */
Alex Tomasa86c6182006-10-11 01:21:03 -07002363 num = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002364 }
Theodore Ts'o34071da2008-08-01 21:59:19 -04002365 /*
2366 * 3 for leaf, sb, and inode plus 2 (bmap and group
2367 * descriptor) for each block group; assume two block
2368 * groups plus ex_ee_len/blocks_per_block_group for
2369 * the worst case
2370 */
2371 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002372 if (ex == EXT_FIRST_EXTENT(eh)) {
2373 correct_index = 1;
2374 credits += (ext_depth(inode)) + 1;
2375 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002376 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002377
Jan Kara487caee2009-08-17 22:17:20 -04002378 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
Shen Feng9102e4f2008-07-11 19:27:31 -04002379 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002380 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002381
2382 err = ext4_ext_get_access(handle, inode, path + depth);
2383 if (err)
2384 goto out;
2385
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002386 err = ext4_remove_blocks(handle, inode, ex, partial_cluster,
2387 a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002388 if (err)
2389 goto out;
2390
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002391 if (num == 0)
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002392 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002393 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002394
Alex Tomasa86c6182006-10-11 01:21:03 -07002395 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002396 /*
2397 * Do not mark uninitialized if all the blocks in the
2398 * extent have been removed.
2399 */
2400 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002401 ext4_ext_mark_uninitialized(ex);
Allison Hendersond583fb82011-05-25 07:41:43 -04002402 /*
2403 * If the extent was completely released,
2404 * we need to remove it from the leaf
2405 */
2406 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002407 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002408 /*
2409 * For hole punching, we need to scoot all the
2410 * extents up when an extent is removed so that
2411 * we dont have blank extents in the middle
2412 */
2413 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2414 sizeof(struct ext4_extent));
2415
2416 /* Now get rid of the one at the end */
2417 memset(EXT_LAST_EXTENT(eh), 0,
2418 sizeof(struct ext4_extent));
2419 }
2420 le16_add_cpu(&eh->eh_entries, -1);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002421 } else
2422 *partial_cluster = 0;
Allison Hendersond583fb82011-05-25 07:41:43 -04002423
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002424 err = ext4_ext_dirty(handle, inode, path + depth);
2425 if (err)
2426 goto out;
2427
Yongqiang Yangbf52c6f2011-11-01 18:59:26 -04002428 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002429 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002430 ex--;
2431 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002432 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002433 }
2434
2435 if (correct_index && eh->eh_entries)
2436 err = ext4_ext_correct_indexes(handle, inode, path);
2437
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002438 /*
2439 * If there is still a entry in the leaf node, check to see if
2440 * it references the partial cluster. This is the only place
2441 * where it could; if it doesn't, we can free the cluster.
2442 */
2443 if (*partial_cluster && ex >= EXT_FIRST_EXTENT(eh) &&
2444 (EXT4_B2C(sbi, ext4_ext_pblock(ex) + ex_ee_len - 1) !=
2445 *partial_cluster)) {
2446 int flags = EXT4_FREE_BLOCKS_FORGET;
2447
2448 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2449 flags |= EXT4_FREE_BLOCKS_METADATA;
2450
2451 ext4_free_blocks(handle, inode, NULL,
2452 EXT4_C2B(sbi, *partial_cluster),
2453 sbi->s_cluster_ratio, flags);
2454 *partial_cluster = 0;
2455 }
2456
Alex Tomasa86c6182006-10-11 01:21:03 -07002457 /* if this leaf is free, then we should
2458 * remove it from index block above */
2459 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2460 err = ext4_ext_rm_idx(handle, inode, path + depth);
2461
2462out:
2463 return err;
2464}
2465
2466/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002467 * ext4_ext_more_to_rm:
2468 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002469 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002470static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002471ext4_ext_more_to_rm(struct ext4_ext_path *path)
2472{
2473 BUG_ON(path->p_idx == NULL);
2474
2475 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2476 return 0;
2477
2478 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002479 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002480 * so we have to consider current index for truncation
2481 */
2482 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2483 return 0;
2484 return 1;
2485}
2486
Allison Hendersonc6a03712011-07-17 23:21:03 -04002487static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07002488{
2489 struct super_block *sb = inode->i_sb;
2490 int depth = ext_depth(inode);
2491 struct ext4_ext_path *path;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002492 ext4_fsblk_t partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002493 handle_t *handle;
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002494 int i, err;
Alex Tomasa86c6182006-10-11 01:21:03 -07002495
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002496 ext_debug("truncate since %u\n", start);
Alex Tomasa86c6182006-10-11 01:21:03 -07002497
2498 /* probably first extent we're gonna free will be last in block */
2499 handle = ext4_journal_start(inode, depth + 1);
2500 if (IS_ERR(handle))
2501 return PTR_ERR(handle);
2502
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002503again:
Alex Tomasa86c6182006-10-11 01:21:03 -07002504 ext4_ext_invalidate_cache(inode);
2505
Aditya Kalid8990242011-09-09 19:18:51 -04002506 trace_ext4_ext_remove_space(inode, start, depth);
2507
Alex Tomasa86c6182006-10-11 01:21:03 -07002508 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002509 * We start scanning from right side, freeing all the blocks
2510 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002511 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002512 depth = ext_depth(inode);
Josef Bacik216553c2008-04-29 22:02:02 -04002513 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -07002514 if (path == NULL) {
2515 ext4_journal_stop(handle);
2516 return -ENOMEM;
2517 }
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002518 path[0].p_depth = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -07002519 path[0].p_hdr = ext_inode_hdr(inode);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002520 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002521 err = -EIO;
2522 goto out;
2523 }
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002524 i = err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002525
2526 while (i >= 0 && err == 0) {
2527 if (i == depth) {
2528 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04002529 err = ext4_ext_rm_leaf(handle, inode, path,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002530 &partial_cluster, start,
2531 EXT_MAX_BLOCKS - 1);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002532 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002533 brelse(path[i].p_bh);
2534 path[i].p_bh = NULL;
2535 i--;
2536 continue;
2537 }
2538
2539 /* this is index block */
2540 if (!path[i].p_hdr) {
2541 ext_debug("initialize header\n");
2542 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002543 }
2544
Alex Tomasa86c6182006-10-11 01:21:03 -07002545 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002546 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002547 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2548 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2549 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2550 path[i].p_hdr,
2551 le16_to_cpu(path[i].p_hdr->eh_entries));
2552 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002553 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002554 path[i].p_idx--;
2555 }
2556
2557 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2558 i, EXT_FIRST_INDEX(path[i].p_hdr),
2559 path[i].p_idx);
2560 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002561 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002562 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002563 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04002564 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002565 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'obf89d162010-10-27 21:30:14 -04002566 bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002567 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002568 /* should we reset i_size? */
2569 err = -EIO;
2570 break;
2571 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002572 if (WARN_ON(i + 1 > depth)) {
2573 err = -EIO;
2574 break;
2575 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002576 if (ext4_ext_check(inode, ext_block_hdr(bh),
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002577 depth - i - 1)) {
2578 err = -EIO;
2579 break;
2580 }
2581 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002582
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002583 /* save actual number of indexes since this
2584 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002585 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2586 i++;
2587 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002588 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002589 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002590 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002591 * handle must be already prepared by the
2592 * truncatei_leaf() */
2593 err = ext4_ext_rm_idx(handle, inode, path + i);
2594 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002595 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002596 brelse(path[i].p_bh);
2597 path[i].p_bh = NULL;
2598 i--;
2599 ext_debug("return to level %d\n", i);
2600 }
2601 }
2602
Aditya Kalid8990242011-09-09 19:18:51 -04002603 trace_ext4_ext_remove_space_done(inode, start, depth, partial_cluster,
2604 path->p_hdr->eh_entries);
2605
Aditya Kali7b415bf2011-09-09 19:04:51 -04002606 /* If we still have something in the partial cluster and we have removed
2607 * even the first extent, then we should free the blocks in the partial
2608 * cluster as well. */
2609 if (partial_cluster && path->p_hdr->eh_entries == 0) {
2610 int flags = EXT4_FREE_BLOCKS_FORGET;
2611
2612 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2613 flags |= EXT4_FREE_BLOCKS_METADATA;
2614
2615 ext4_free_blocks(handle, inode, NULL,
2616 EXT4_C2B(EXT4_SB(sb), partial_cluster),
2617 EXT4_SB(sb)->s_cluster_ratio, flags);
2618 partial_cluster = 0;
2619 }
2620
Alex Tomasa86c6182006-10-11 01:21:03 -07002621 /* TODO: flexible tree reduction should be here */
2622 if (path->p_hdr->eh_entries == 0) {
2623 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002624 * truncate to zero freed all the tree,
2625 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002626 */
2627 err = ext4_ext_get_access(handle, inode, path);
2628 if (err == 0) {
2629 ext_inode_hdr(inode)->eh_depth = 0;
2630 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04002631 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07002632 err = ext4_ext_dirty(handle, inode, path);
2633 }
2634 }
2635out:
Alex Tomasa86c6182006-10-11 01:21:03 -07002636 ext4_ext_drop_refs(path);
2637 kfree(path);
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002638 if (err == -EAGAIN)
2639 goto again;
Alex Tomasa86c6182006-10-11 01:21:03 -07002640 ext4_journal_stop(handle);
2641
2642 return err;
2643}
2644
2645/*
2646 * called at mount time
2647 */
2648void ext4_ext_init(struct super_block *sb)
2649{
2650 /*
2651 * possible initialization would be here
2652 */
2653
Theodore Ts'o83982b62009-01-06 14:53:16 -05002654 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04002655#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002656 printk(KERN_INFO "EXT4-fs: file extents enabled");
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002657#ifdef AGGRESSIVE_TEST
2658 printk(", aggressive tests");
Alex Tomasa86c6182006-10-11 01:21:03 -07002659#endif
2660#ifdef CHECK_BINSEARCH
2661 printk(", check binsearch");
2662#endif
2663#ifdef EXTENTS_STATS
2664 printk(", stats");
2665#endif
2666 printk("\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04002667#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07002668#ifdef EXTENTS_STATS
2669 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2670 EXT4_SB(sb)->s_ext_min = 1 << 30;
2671 EXT4_SB(sb)->s_ext_max = 0;
2672#endif
2673 }
2674}
2675
2676/*
2677 * called at umount time
2678 */
2679void ext4_ext_release(struct super_block *sb)
2680{
Theodore Ts'o83982b62009-01-06 14:53:16 -05002681 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
Alex Tomasa86c6182006-10-11 01:21:03 -07002682 return;
2683
2684#ifdef EXTENTS_STATS
2685 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2686 struct ext4_sb_info *sbi = EXT4_SB(sb);
2687 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2688 sbi->s_ext_blocks, sbi->s_ext_extents,
2689 sbi->s_ext_blocks / sbi->s_ext_extents);
2690 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2691 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2692 }
2693#endif
2694}
2695
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002696/* FIXME!! we need to try to merge to left or right after zero-out */
2697static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2698{
Lukas Czerner24075182010-10-27 21:30:06 -04002699 ext4_fsblk_t ee_pblock;
2700 unsigned int ee_len;
Jing Zhangb7203032010-05-12 00:00:00 -04002701 int ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002702
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002703 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04002704 ee_pblock = ext4_ext_pblock(ex);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002705
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04002706 ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
Lukas Czerner24075182010-10-27 21:30:06 -04002707 if (ret > 0)
2708 ret = 0;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002709
Lukas Czerner24075182010-10-27 21:30:06 -04002710 return ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002711}
2712
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002713/*
2714 * used by extent splitting.
2715 */
2716#define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
2717 due to ENOSPC */
2718#define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */
2719#define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */
2720
2721/*
2722 * ext4_split_extent_at() splits an extent at given block.
2723 *
2724 * @handle: the journal handle
2725 * @inode: the file inode
2726 * @path: the path to the extent
2727 * @split: the logical block where the extent is splitted.
2728 * @split_flags: indicates if the extent could be zeroout if split fails, and
2729 * the states(init or uninit) of new extents.
2730 * @flags: flags used to insert new extent to extent tree.
2731 *
2732 *
2733 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
2734 * of which are deterimined by split_flag.
2735 *
2736 * There are two cases:
2737 * a> the extent are splitted into two extent.
2738 * b> split is not needed, and just mark the extent.
2739 *
2740 * return 0 on success.
2741 */
2742static int ext4_split_extent_at(handle_t *handle,
2743 struct inode *inode,
2744 struct ext4_ext_path *path,
2745 ext4_lblk_t split,
2746 int split_flag,
2747 int flags)
2748{
2749 ext4_fsblk_t newblock;
2750 ext4_lblk_t ee_block;
2751 struct ext4_extent *ex, newex, orig_ex;
2752 struct ext4_extent *ex2 = NULL;
2753 unsigned int ee_len, depth;
2754 int err = 0;
2755
2756 ext_debug("ext4_split_extents_at: inode %lu, logical"
2757 "block %llu\n", inode->i_ino, (unsigned long long)split);
2758
2759 ext4_ext_show_leaf(inode, path);
2760
2761 depth = ext_depth(inode);
2762 ex = path[depth].p_ext;
2763 ee_block = le32_to_cpu(ex->ee_block);
2764 ee_len = ext4_ext_get_actual_len(ex);
2765 newblock = split - ee_block + ext4_ext_pblock(ex);
2766
2767 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
2768
2769 err = ext4_ext_get_access(handle, inode, path + depth);
2770 if (err)
2771 goto out;
2772
2773 if (split == ee_block) {
2774 /*
2775 * case b: block @split is the block that the extent begins with
2776 * then we just change the state of the extent, and splitting
2777 * is not needed.
2778 */
2779 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2780 ext4_ext_mark_uninitialized(ex);
2781 else
2782 ext4_ext_mark_initialized(ex);
2783
2784 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
2785 ext4_ext_try_to_merge(inode, path, ex);
2786
2787 err = ext4_ext_dirty(handle, inode, path + depth);
2788 goto out;
2789 }
2790
2791 /* case a */
2792 memcpy(&orig_ex, ex, sizeof(orig_ex));
2793 ex->ee_len = cpu_to_le16(split - ee_block);
2794 if (split_flag & EXT4_EXT_MARK_UNINIT1)
2795 ext4_ext_mark_uninitialized(ex);
2796
2797 /*
2798 * path may lead to new leaf, not to original leaf any more
2799 * after ext4_ext_insert_extent() returns,
2800 */
2801 err = ext4_ext_dirty(handle, inode, path + depth);
2802 if (err)
2803 goto fix_extent_len;
2804
2805 ex2 = &newex;
2806 ex2->ee_block = cpu_to_le32(split);
2807 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
2808 ext4_ext_store_pblock(ex2, newblock);
2809 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2810 ext4_ext_mark_uninitialized(ex2);
2811
2812 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
2813 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
2814 err = ext4_ext_zeroout(inode, &orig_ex);
2815 if (err)
2816 goto fix_extent_len;
2817 /* update the extent length and mark as initialized */
2818 ex->ee_len = cpu_to_le32(ee_len);
2819 ext4_ext_try_to_merge(inode, path, ex);
2820 err = ext4_ext_dirty(handle, inode, path + depth);
2821 goto out;
2822 } else if (err)
2823 goto fix_extent_len;
2824
2825out:
2826 ext4_ext_show_leaf(inode, path);
2827 return err;
2828
2829fix_extent_len:
2830 ex->ee_len = orig_ex.ee_len;
2831 ext4_ext_dirty(handle, inode, path + depth);
2832 return err;
2833}
2834
2835/*
2836 * ext4_split_extents() splits an extent and mark extent which is covered
2837 * by @map as split_flags indicates
2838 *
2839 * It may result in splitting the extent into multiple extents (upto three)
2840 * There are three possibilities:
2841 * a> There is no split required
2842 * b> Splits in two extents: Split is happening at either end of the extent
2843 * c> Splits in three extents: Somone is splitting in middle of the extent
2844 *
2845 */
2846static int ext4_split_extent(handle_t *handle,
2847 struct inode *inode,
2848 struct ext4_ext_path *path,
2849 struct ext4_map_blocks *map,
2850 int split_flag,
2851 int flags)
2852{
2853 ext4_lblk_t ee_block;
2854 struct ext4_extent *ex;
2855 unsigned int ee_len, depth;
2856 int err = 0;
2857 int uninitialized;
2858 int split_flag1, flags1;
2859
2860 depth = ext_depth(inode);
2861 ex = path[depth].p_ext;
2862 ee_block = le32_to_cpu(ex->ee_block);
2863 ee_len = ext4_ext_get_actual_len(ex);
2864 uninitialized = ext4_ext_is_uninitialized(ex);
2865
2866 if (map->m_lblk + map->m_len < ee_block + ee_len) {
2867 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2868 EXT4_EXT_MAY_ZEROOUT : 0;
2869 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
2870 if (uninitialized)
2871 split_flag1 |= EXT4_EXT_MARK_UNINIT1 |
2872 EXT4_EXT_MARK_UNINIT2;
2873 err = ext4_split_extent_at(handle, inode, path,
2874 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04002875 if (err)
2876 goto out;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002877 }
2878
2879 ext4_ext_drop_refs(path);
2880 path = ext4_ext_find_extent(inode, map->m_lblk, path);
2881 if (IS_ERR(path))
2882 return PTR_ERR(path);
2883
2884 if (map->m_lblk >= ee_block) {
2885 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ?
2886 EXT4_EXT_MAY_ZEROOUT : 0;
2887 if (uninitialized)
2888 split_flag1 |= EXT4_EXT_MARK_UNINIT1;
2889 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2890 split_flag1 |= EXT4_EXT_MARK_UNINIT2;
2891 err = ext4_split_extent_at(handle, inode, path,
2892 map->m_lblk, split_flag1, flags);
2893 if (err)
2894 goto out;
2895 }
2896
2897 ext4_ext_show_leaf(inode, path);
2898out:
2899 return err ? err : map->m_len;
2900}
2901
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002902#define EXT4_EXT_ZERO_LEN 7
Amit Arora56055d32007-07-17 21:42:38 -04002903/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002904 * This function is called by ext4_ext_map_blocks() if someone tries to write
Amit Arora56055d32007-07-17 21:42:38 -04002905 * to an uninitialized extent. It may result in splitting the uninitialized
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002906 * extent into multiple extents (up to three - one initialized and two
Amit Arora56055d32007-07-17 21:42:38 -04002907 * uninitialized).
2908 * There are three possibilities:
2909 * a> There is no split required: Entire extent should be initialized
2910 * b> Splits in two extents: Write is happening at either end of the extent
2911 * c> Splits in three extents: Somone is writing in middle of the extent
Eric Gouriou6f91bc52011-10-27 11:43:23 -04002912 *
2913 * Pre-conditions:
2914 * - The extent pointed to by 'path' is uninitialized.
2915 * - The extent pointed to by 'path' contains a superset
2916 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
2917 *
2918 * Post-conditions on success:
2919 * - the returned value is the number of blocks beyond map->l_lblk
2920 * that are allocated and initialized.
2921 * It is guaranteed to be >= map->m_len.
Amit Arora56055d32007-07-17 21:42:38 -04002922 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002923static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002924 struct inode *inode,
2925 struct ext4_map_blocks *map,
2926 struct ext4_ext_path *path)
Amit Arora56055d32007-07-17 21:42:38 -04002927{
Eric Gouriou6f91bc52011-10-27 11:43:23 -04002928 struct ext4_extent_header *eh;
Yongqiang Yang667eff32011-05-03 12:25:07 -04002929 struct ext4_map_blocks split_map;
2930 struct ext4_extent zero_ex;
2931 struct ext4_extent *ex;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002932 ext4_lblk_t ee_block, eof_block;
Dan Carpenterf85b2872011-10-26 03:42:36 -04002933 unsigned int ee_len, depth;
2934 int allocated;
Amit Arora56055d32007-07-17 21:42:38 -04002935 int err = 0;
Yongqiang Yang667eff32011-05-03 12:25:07 -04002936 int split_flag = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002937
2938 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
2939 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002940 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002941
2942 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
2943 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002944 if (eof_block < map->m_lblk + map->m_len)
2945 eof_block = map->m_lblk + map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04002946
2947 depth = ext_depth(inode);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04002948 eh = path[depth].p_hdr;
Amit Arora56055d32007-07-17 21:42:38 -04002949 ex = path[depth].p_ext;
2950 ee_block = le32_to_cpu(ex->ee_block);
2951 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002952 allocated = ee_len - (map->m_lblk - ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04002953
Eric Gouriou6f91bc52011-10-27 11:43:23 -04002954 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
2955
2956 /* Pre-conditions */
2957 BUG_ON(!ext4_ext_is_uninitialized(ex));
2958 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04002959
2960 /*
2961 * Attempt to transfer newly initialized blocks from the currently
2962 * uninitialized extent to its left neighbor. This is much cheaper
2963 * than an insertion followed by a merge as those involve costly
2964 * memmove() calls. This is the common case in steady state for
2965 * workloads doing fallocate(FALLOC_FL_KEEP_SIZE) followed by append
2966 * writes.
2967 *
2968 * Limitations of the current logic:
2969 * - L1: we only deal with writes at the start of the extent.
2970 * The approach could be extended to writes at the end
2971 * of the extent but this scenario was deemed less common.
2972 * - L2: we do not deal with writes covering the whole extent.
2973 * This would require removing the extent if the transfer
2974 * is possible.
2975 * - L3: we only attempt to merge with an extent stored in the
2976 * same extent tree node.
2977 */
2978 if ((map->m_lblk == ee_block) && /*L1*/
2979 (map->m_len < ee_len) && /*L2*/
2980 (ex > EXT_FIRST_EXTENT(eh))) { /*L3*/
2981 struct ext4_extent *prev_ex;
2982 ext4_lblk_t prev_lblk;
2983 ext4_fsblk_t prev_pblk, ee_pblk;
2984 unsigned int prev_len, write_len;
2985
2986 prev_ex = ex - 1;
2987 prev_lblk = le32_to_cpu(prev_ex->ee_block);
2988 prev_len = ext4_ext_get_actual_len(prev_ex);
2989 prev_pblk = ext4_ext_pblock(prev_ex);
2990 ee_pblk = ext4_ext_pblock(ex);
2991 write_len = map->m_len;
2992
2993 /*
2994 * A transfer of blocks from 'ex' to 'prev_ex' is allowed
2995 * upon those conditions:
2996 * - C1: prev_ex is initialized,
2997 * - C2: prev_ex is logically abutting ex,
2998 * - C3: prev_ex is physically abutting ex,
2999 * - C4: prev_ex can receive the additional blocks without
3000 * overflowing the (initialized) length limit.
3001 */
3002 if ((!ext4_ext_is_uninitialized(prev_ex)) && /*C1*/
3003 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3004 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
3005 (prev_len < (EXT_INIT_MAX_LEN - write_len))) { /*C4*/
3006 err = ext4_ext_get_access(handle, inode, path + depth);
3007 if (err)
3008 goto out;
3009
3010 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3011 map, ex, prev_ex);
3012
3013 /* Shift the start of ex by 'write_len' blocks */
3014 ex->ee_block = cpu_to_le32(ee_block + write_len);
3015 ext4_ext_store_pblock(ex, ee_pblk + write_len);
3016 ex->ee_len = cpu_to_le16(ee_len - write_len);
3017 ext4_ext_mark_uninitialized(ex); /* Restore the flag */
3018
3019 /* Extend prev_ex by 'write_len' blocks */
3020 prev_ex->ee_len = cpu_to_le16(prev_len + write_len);
3021
3022 /* Mark the block containing both extents as dirty */
3023 ext4_ext_dirty(handle, inode, path + depth);
3024
3025 /* Update path to point to the right extent */
3026 path[depth].p_ext = prev_ex;
3027
3028 /* Result: number of initialized blocks past m_lblk */
3029 allocated = write_len;
3030 goto out;
3031 }
3032 }
3033
Yongqiang Yang667eff32011-05-03 12:25:07 -04003034 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003035 /*
3036 * It is safe to convert extent to initialized via explicit
3037 * zeroout only if extent is fully insde i_size or new_size.
3038 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003039 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003040
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003041 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003042 if (ee_len <= 2*EXT4_EXT_ZERO_LEN &&
3043 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3044 err = ext4_ext_zeroout(inode, ex);
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003045 if (err)
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003046 goto out;
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003047
3048 err = ext4_ext_get_access(handle, inode, path + depth);
3049 if (err)
3050 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003051 ext4_ext_mark_initialized(ex);
3052 ext4_ext_try_to_merge(inode, path, ex);
3053 err = ext4_ext_dirty(handle, inode, path + depth);
3054 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04003055 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003056
Amit Arora56055d32007-07-17 21:42:38 -04003057 /*
Yongqiang Yang667eff32011-05-03 12:25:07 -04003058 * four cases:
3059 * 1. split the extent into three extents.
3060 * 2. split the extent into two extents, zeroout the first half.
3061 * 3. split the extent into two extents, zeroout the second half.
3062 * 4. split the extent into two extents with out zeroout.
Amit Arora56055d32007-07-17 21:42:38 -04003063 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003064 split_map.m_lblk = map->m_lblk;
3065 split_map.m_len = map->m_len;
3066
3067 if (allocated > map->m_len) {
3068 if (allocated <= EXT4_EXT_ZERO_LEN &&
3069 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3070 /* case 3 */
3071 zero_ex.ee_block =
Allison Henderson9b940f82011-05-16 10:11:09 -04003072 cpu_to_le32(map->m_lblk);
3073 zero_ex.ee_len = cpu_to_le16(allocated);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003074 ext4_ext_store_pblock(&zero_ex,
3075 ext4_ext_pblock(ex) + map->m_lblk - ee_block);
3076 err = ext4_ext_zeroout(inode, &zero_ex);
Amit Arora56055d32007-07-17 21:42:38 -04003077 if (err)
3078 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003079 split_map.m_lblk = map->m_lblk;
3080 split_map.m_len = allocated;
3081 } else if ((map->m_lblk - ee_block + map->m_len <
3082 EXT4_EXT_ZERO_LEN) &&
3083 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3084 /* case 2 */
3085 if (map->m_lblk != ee_block) {
3086 zero_ex.ee_block = ex->ee_block;
3087 zero_ex.ee_len = cpu_to_le16(map->m_lblk -
3088 ee_block);
3089 ext4_ext_store_pblock(&zero_ex,
3090 ext4_ext_pblock(ex));
3091 err = ext4_ext_zeroout(inode, &zero_ex);
3092 if (err)
3093 goto out;
3094 }
3095
Yongqiang Yang667eff32011-05-03 12:25:07 -04003096 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003097 split_map.m_len = map->m_lblk - ee_block + map->m_len;
3098 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003099 }
3100 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003101
3102 allocated = ext4_split_extent(handle, inode, path,
3103 &split_map, split_flag, 0);
3104 if (allocated < 0)
3105 err = allocated;
3106
Amit Arora56055d32007-07-17 21:42:38 -04003107out:
3108 return err ? err : allocated;
3109}
3110
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003111/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003112 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003113 * ext4_get_blocks_dio_write() when DIO to write
3114 * to an uninitialized extent.
3115 *
Paul Bollefd018fe2011-02-15 00:05:43 +01003116 * Writing to an uninitialized extent may result in splitting the uninitialized
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003117 * extent into multiple /initialized uninitialized extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003118 * There are three possibilities:
3119 * a> There is no split required: Entire extent should be uninitialized
3120 * b> Splits in two extents: Write is happening at either end of the extent
3121 * c> Splits in three extents: Somone is writing in middle of the extent
3122 *
3123 * One of more index blocks maybe needed if the extent tree grow after
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003124 * the uninitialized extent split. To prevent ENOSPC occur at the IO
Mingming Cao00314622009-09-28 15:49:08 -04003125 * complete, we need to split the uninitialized extent before DIO submit
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02003126 * the IO. The uninitialized extent called at this time will be split
Mingming Cao00314622009-09-28 15:49:08 -04003127 * into three uninitialized extent(at most). After IO complete, the part
3128 * being filled will be convert to initialized by the end_io callback function
3129 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003130 *
3131 * Returns the size of uninitialized extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003132 */
3133static int ext4_split_unwritten_extents(handle_t *handle,
3134 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003135 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003136 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04003137 int flags)
3138{
Yongqiang Yang667eff32011-05-03 12:25:07 -04003139 ext4_lblk_t eof_block;
3140 ext4_lblk_t ee_block;
3141 struct ext4_extent *ex;
3142 unsigned int ee_len;
3143 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003144
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003145 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
3146 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003147 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003148
3149 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3150 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003151 if (eof_block < map->m_lblk + map->m_len)
3152 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003153 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003154 * It is safe to convert extent to initialized via explicit
3155 * zeroout only if extent is fully insde i_size or new_size.
3156 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003157 depth = ext_depth(inode);
3158 ex = path[depth].p_ext;
3159 ee_block = le32_to_cpu(ex->ee_block);
3160 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003161
Yongqiang Yang667eff32011-05-03 12:25:07 -04003162 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
3163 split_flag |= EXT4_EXT_MARK_UNINIT2;
Mingming Cao00314622009-09-28 15:49:08 -04003164
Yongqiang Yang667eff32011-05-03 12:25:07 -04003165 flags |= EXT4_GET_BLOCKS_PRE_IO;
3166 return ext4_split_extent(handle, inode, path, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003167}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003168
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003169static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Mingming Cao00314622009-09-28 15:49:08 -04003170 struct inode *inode,
3171 struct ext4_ext_path *path)
3172{
3173 struct ext4_extent *ex;
Mingming Cao00314622009-09-28 15:49:08 -04003174 int depth;
3175 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003176
3177 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003178 ex = path[depth].p_ext;
3179
Yongqiang Yang197217a2011-05-03 11:45:29 -04003180 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3181 "block %llu, max_blocks %u\n", inode->i_ino,
3182 (unsigned long long)le32_to_cpu(ex->ee_block),
3183 ext4_ext_get_actual_len(ex));
3184
Mingming Cao00314622009-09-28 15:49:08 -04003185 err = ext4_ext_get_access(handle, inode, path + depth);
3186 if (err)
3187 goto out;
3188 /* first mark the extent as initialized */
3189 ext4_ext_mark_initialized(ex);
3190
Yongqiang Yang197217a2011-05-03 11:45:29 -04003191 /* note: ext4_ext_correct_indexes() isn't needed here because
3192 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003193 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04003194 ext4_ext_try_to_merge(inode, path, ex);
3195
Mingming Cao00314622009-09-28 15:49:08 -04003196 /* Mark modified extent as dirty */
3197 err = ext4_ext_dirty(handle, inode, path + depth);
3198out:
3199 ext4_ext_show_leaf(inode, path);
3200 return err;
3201}
3202
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003203static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3204 sector_t block, int count)
3205{
3206 int i;
3207 for (i = 0; i < count; i++)
3208 unmap_underlying_metadata(bdev, block + i);
3209}
3210
Theodore Ts'o58590b02010-10-27 21:23:12 -04003211/*
3212 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3213 */
3214static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
Eric Sandeend002ebf2011-01-10 13:03:35 -05003215 ext4_lblk_t lblk,
Theodore Ts'o58590b02010-10-27 21:23:12 -04003216 struct ext4_ext_path *path,
3217 unsigned int len)
3218{
3219 int i, depth;
3220 struct ext4_extent_header *eh;
Sergey Senozhatsky65922cb2011-03-23 14:08:27 -04003221 struct ext4_extent *last_ex;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003222
3223 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3224 return 0;
3225
3226 depth = ext_depth(inode);
3227 eh = path[depth].p_hdr;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003228
3229 if (unlikely(!eh->eh_entries)) {
3230 EXT4_ERROR_INODE(inode, "eh->eh_entries == 0 and "
3231 "EOFBLOCKS_FL set");
3232 return -EIO;
3233 }
3234 last_ex = EXT_LAST_EXTENT(eh);
3235 /*
3236 * We should clear the EOFBLOCKS_FL flag if we are writing the
3237 * last block in the last extent in the file. We test this by
3238 * first checking to see if the caller to
3239 * ext4_ext_get_blocks() was interested in the last block (or
3240 * a block beyond the last block) in the current extent. If
3241 * this turns out to be false, we can bail out from this
3242 * function immediately.
3243 */
Eric Sandeend002ebf2011-01-10 13:03:35 -05003244 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
Theodore Ts'o58590b02010-10-27 21:23:12 -04003245 ext4_ext_get_actual_len(last_ex))
3246 return 0;
3247 /*
3248 * If the caller does appear to be planning to write at or
3249 * beyond the end of the current extent, we then test to see
3250 * if the current extent is the last extent in the file, by
3251 * checking to make sure it was reached via the rightmost node
3252 * at each level of the tree.
3253 */
3254 for (i = depth-1; i >= 0; i--)
3255 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3256 return 0;
3257 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3258 return ext4_mark_inode_dirty(handle, inode);
3259}
3260
Aditya Kali7b415bf2011-09-09 19:04:51 -04003261/**
3262 * ext4_find_delalloc_range: find delayed allocated block in the given range.
3263 *
3264 * Goes through the buffer heads in the range [lblk_start, lblk_end] and returns
3265 * whether there are any buffers marked for delayed allocation. It returns '1'
3266 * on the first delalloc'ed buffer head found. If no buffer head in the given
3267 * range is marked for delalloc, it returns 0.
3268 * lblk_start should always be <= lblk_end.
3269 * search_hint_reverse is to indicate that searching in reverse from lblk_end to
3270 * lblk_start might be more efficient (i.e., we will likely hit the delalloc'ed
3271 * block sooner). This is useful when blocks are truncated sequentially from
3272 * lblk_start towards lblk_end.
3273 */
3274static int ext4_find_delalloc_range(struct inode *inode,
3275 ext4_lblk_t lblk_start,
3276 ext4_lblk_t lblk_end,
3277 int search_hint_reverse)
3278{
3279 struct address_space *mapping = inode->i_mapping;
3280 struct buffer_head *head, *bh = NULL;
3281 struct page *page;
3282 ext4_lblk_t i, pg_lblk;
3283 pgoff_t index;
3284
Robin Dong8c48f7e2011-12-18 23:05:43 -05003285 if (!test_opt(inode->i_sb, DELALLOC))
3286 return 0;
3287
Aditya Kali7b415bf2011-09-09 19:04:51 -04003288 /* reverse search wont work if fs block size is less than page size */
3289 if (inode->i_blkbits < PAGE_CACHE_SHIFT)
3290 search_hint_reverse = 0;
3291
3292 if (search_hint_reverse)
3293 i = lblk_end;
3294 else
3295 i = lblk_start;
3296
3297 index = i >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
3298
3299 while ((i >= lblk_start) && (i <= lblk_end)) {
3300 page = find_get_page(mapping, index);
Aditya Kali5356f262011-09-09 19:20:51 -04003301 if (!page)
Aditya Kali7b415bf2011-09-09 19:04:51 -04003302 goto nextpage;
3303
Aditya Kali7b415bf2011-09-09 19:04:51 -04003304 if (!page_has_buffers(page))
3305 goto nextpage;
3306
3307 head = page_buffers(page);
3308 if (!head)
3309 goto nextpage;
3310
3311 bh = head;
3312 pg_lblk = index << (PAGE_CACHE_SHIFT -
3313 inode->i_blkbits);
3314 do {
3315 if (unlikely(pg_lblk < lblk_start)) {
3316 /*
3317 * This is possible when fs block size is less
3318 * than page size and our cluster starts/ends in
3319 * middle of the page. So we need to skip the
3320 * initial few blocks till we reach the 'lblk'
3321 */
3322 pg_lblk++;
3323 continue;
3324 }
3325
Aditya Kali5356f262011-09-09 19:20:51 -04003326 /* Check if the buffer is delayed allocated and that it
3327 * is not yet mapped. (when da-buffers are mapped during
3328 * their writeout, their da_mapped bit is set.)
3329 */
3330 if (buffer_delay(bh) && !buffer_da_mapped(bh)) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04003331 page_cache_release(page);
Aditya Kalid8990242011-09-09 19:18:51 -04003332 trace_ext4_find_delalloc_range(inode,
3333 lblk_start, lblk_end,
3334 search_hint_reverse,
3335 1, i);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003336 return 1;
3337 }
3338 if (search_hint_reverse)
3339 i--;
3340 else
3341 i++;
3342 } while ((i >= lblk_start) && (i <= lblk_end) &&
3343 ((bh = bh->b_this_page) != head));
3344nextpage:
3345 if (page)
3346 page_cache_release(page);
3347 /*
3348 * Move to next page. 'i' will be the first lblk in the next
3349 * page.
3350 */
3351 if (search_hint_reverse)
3352 index--;
3353 else
3354 index++;
3355 i = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
3356 }
3357
Aditya Kalid8990242011-09-09 19:18:51 -04003358 trace_ext4_find_delalloc_range(inode, lblk_start, lblk_end,
3359 search_hint_reverse, 0, 0);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003360 return 0;
3361}
3362
3363int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk,
3364 int search_hint_reverse)
3365{
3366 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3367 ext4_lblk_t lblk_start, lblk_end;
3368 lblk_start = lblk & (~(sbi->s_cluster_ratio - 1));
3369 lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
3370
3371 return ext4_find_delalloc_range(inode, lblk_start, lblk_end,
3372 search_hint_reverse);
3373}
3374
3375/**
3376 * Determines how many complete clusters (out of those specified by the 'map')
3377 * are under delalloc and were reserved quota for.
3378 * This function is called when we are writing out the blocks that were
3379 * originally written with their allocation delayed, but then the space was
3380 * allocated using fallocate() before the delayed allocation could be resolved.
3381 * The cases to look for are:
3382 * ('=' indicated delayed allocated blocks
3383 * '-' indicates non-delayed allocated blocks)
3384 * (a) partial clusters towards beginning and/or end outside of allocated range
3385 * are not delalloc'ed.
3386 * Ex:
3387 * |----c---=|====c====|====c====|===-c----|
3388 * |++++++ allocated ++++++|
3389 * ==> 4 complete clusters in above example
3390 *
3391 * (b) partial cluster (outside of allocated range) towards either end is
3392 * marked for delayed allocation. In this case, we will exclude that
3393 * cluster.
3394 * Ex:
3395 * |----====c========|========c========|
3396 * |++++++ allocated ++++++|
3397 * ==> 1 complete clusters in above example
3398 *
3399 * Ex:
3400 * |================c================|
3401 * |++++++ allocated ++++++|
3402 * ==> 0 complete clusters in above example
3403 *
3404 * The ext4_da_update_reserve_space will be called only if we
3405 * determine here that there were some "entire" clusters that span
3406 * this 'allocated' range.
3407 * In the non-bigalloc case, this function will just end up returning num_blks
3408 * without ever calling ext4_find_delalloc_range.
3409 */
3410static unsigned int
3411get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start,
3412 unsigned int num_blks)
3413{
3414 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3415 ext4_lblk_t alloc_cluster_start, alloc_cluster_end;
3416 ext4_lblk_t lblk_from, lblk_to, c_offset;
3417 unsigned int allocated_clusters = 0;
3418
3419 alloc_cluster_start = EXT4_B2C(sbi, lblk_start);
3420 alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1);
3421
3422 /* max possible clusters for this allocation */
3423 allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1;
3424
Aditya Kalid8990242011-09-09 19:18:51 -04003425 trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks);
3426
Aditya Kali7b415bf2011-09-09 19:04:51 -04003427 /* Check towards left side */
3428 c_offset = lblk_start & (sbi->s_cluster_ratio - 1);
3429 if (c_offset) {
3430 lblk_from = lblk_start & (~(sbi->s_cluster_ratio - 1));
3431 lblk_to = lblk_from + c_offset - 1;
3432
3433 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
3434 allocated_clusters--;
3435 }
3436
3437 /* Now check towards right. */
3438 c_offset = (lblk_start + num_blks) & (sbi->s_cluster_ratio - 1);
3439 if (allocated_clusters && c_offset) {
3440 lblk_from = lblk_start + num_blks;
3441 lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1;
3442
3443 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
3444 allocated_clusters--;
3445 }
3446
3447 return allocated_clusters;
3448}
3449
Mingming Cao00314622009-09-28 15:49:08 -04003450static int
3451ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003452 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003453 struct ext4_ext_path *path, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003454 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003455{
3456 int ret = 0;
3457 int err = 0;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003458 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Mingming Cao00314622009-09-28 15:49:08 -04003459
Zheng Liu88635ca2011-12-28 19:00:25 -05003460 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical "
3461 "block %llu, max_blocks %u, flags %x, allocated %u\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003462 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04003463 flags, allocated);
3464 ext4_ext_show_leaf(inode, path);
3465
Aditya Kalid8990242011-09-09 19:18:51 -04003466 trace_ext4_ext_handle_uninitialized_extents(inode, map, allocated,
3467 newblock);
3468
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003469 /* get_block() before submit the IO, split the extent */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003470 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003471 ret = ext4_split_unwritten_extents(handle, inode, map,
3472 path, flags);
Mingming5f524952009-11-10 10:48:04 -05003473 /*
3474 * Flag the inode(non aio case) or end_io struct (aio case)
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003475 * that this IO needs to conversion to written when IO is
Mingming5f524952009-11-10 10:48:04 -05003476 * completed
3477 */
Tao Ma0edeb712011-10-31 17:30:44 -04003478 if (io)
3479 ext4_set_io_unwritten_flag(inode, io);
3480 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003481 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
Jiaying Zhang744692d2010-03-04 16:14:02 -05003482 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003483 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao00314622009-09-28 15:49:08 -04003484 goto out;
3485 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003486 /* IO end_io complete, convert the filled extent to written */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003487 if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003488 ret = ext4_convert_unwritten_extents_endio(handle, inode,
Mingming Cao00314622009-09-28 15:49:08 -04003489 path);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003490 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05003491 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05003492 err = check_eofblocks_fl(handle, inode, map->m_lblk,
3493 path, map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003494 } else
3495 err = ret;
Mingming Cao00314622009-09-28 15:49:08 -04003496 goto out2;
3497 }
3498 /* buffered IO case */
3499 /*
3500 * repeat fallocate creation request
3501 * we already have an unwritten extent
3502 */
3503 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3504 goto map_out;
3505
3506 /* buffered READ or buffered write_begin() lookup */
3507 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3508 /*
3509 * We have blocks reserved already. We
3510 * return allocated blocks so that delalloc
3511 * won't do block reservation for us. But
3512 * the buffer head will be unmapped so that
3513 * a read from the block returns 0s.
3514 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003515 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003516 goto out1;
3517 }
3518
3519 /* buffered write, writepage time, convert*/
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003520 ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04003521 if (ret >= 0)
Jan Karab436b9b2009-12-08 23:51:10 -05003522 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04003523out:
3524 if (ret <= 0) {
3525 err = ret;
3526 goto out2;
3527 } else
3528 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003529 map->m_flags |= EXT4_MAP_NEW;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003530 /*
3531 * if we allocated more blocks than requested
3532 * we need to make sure we unmap the extra block
3533 * allocated. The actual needed block will get
3534 * unmapped later when we find the buffer_head marked
3535 * new.
3536 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003537 if (allocated > map->m_len) {
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003538 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003539 newblock + map->m_len,
3540 allocated - map->m_len);
3541 allocated = map->m_len;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003542 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003543
3544 /*
3545 * If we have done fallocate with the offset that is already
3546 * delayed allocated, we would have block reservation
3547 * and quota reservation done in the delayed write path.
3548 * But fallocate would have already updated quota and block
3549 * count for this offset. So cancel these reservation
3550 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003551 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
3552 unsigned int reserved_clusters;
3553 reserved_clusters = get_reserved_cluster_alloc(inode,
3554 map->m_lblk, map->m_len);
3555 if (reserved_clusters)
3556 ext4_da_update_reserve_space(inode,
3557 reserved_clusters,
3558 0);
3559 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003560
Mingming Cao00314622009-09-28 15:49:08 -04003561map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003562 map->m_flags |= EXT4_MAP_MAPPED;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04003563 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
3564 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
3565 map->m_len);
3566 if (err < 0)
3567 goto out2;
3568 }
Mingming Cao00314622009-09-28 15:49:08 -04003569out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003570 if (allocated > map->m_len)
3571 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003572 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003573 map->m_pblk = newblock;
3574 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04003575out2:
3576 if (path) {
3577 ext4_ext_drop_refs(path);
3578 kfree(path);
3579 }
3580 return err ? err : allocated;
3581}
Theodore Ts'o58590b02010-10-27 21:23:12 -04003582
Mingming Cao00314622009-09-28 15:49:08 -04003583/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003584 * get_implied_cluster_alloc - check to see if the requested
3585 * allocation (in the map structure) overlaps with a cluster already
3586 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04003587 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003588 * @map The requested lblk->pblk mapping
3589 * @ex The extent structure which might contain an implied
3590 * cluster allocation
3591 *
3592 * This function is called by ext4_ext_map_blocks() after we failed to
3593 * find blocks that were already in the inode's extent tree. Hence,
3594 * we know that the beginning of the requested region cannot overlap
3595 * the extent from the inode's extent tree. There are three cases we
3596 * want to catch. The first is this case:
3597 *
3598 * |--- cluster # N--|
3599 * |--- extent ---| |---- requested region ---|
3600 * |==========|
3601 *
3602 * The second case that we need to test for is this one:
3603 *
3604 * |--------- cluster # N ----------------|
3605 * |--- requested region --| |------- extent ----|
3606 * |=======================|
3607 *
3608 * The third case is when the requested region lies between two extents
3609 * within the same cluster:
3610 * |------------- cluster # N-------------|
3611 * |----- ex -----| |---- ex_right ----|
3612 * |------ requested region ------|
3613 * |================|
3614 *
3615 * In each of the above cases, we need to set the map->m_pblk and
3616 * map->m_len so it corresponds to the return the extent labelled as
3617 * "|====|" from cluster #N, since it is already in use for data in
3618 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
3619 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
3620 * as a new "allocated" block region. Otherwise, we will return 0 and
3621 * ext4_ext_map_blocks() will then allocate one or more new clusters
3622 * by calling ext4_mb_new_blocks().
3623 */
Aditya Kalid8990242011-09-09 19:18:51 -04003624static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003625 struct ext4_map_blocks *map,
3626 struct ext4_extent *ex,
3627 struct ext4_ext_path *path)
3628{
Aditya Kalid8990242011-09-09 19:18:51 -04003629 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003630 ext4_lblk_t c_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
3631 ext4_lblk_t ex_cluster_start, ex_cluster_end;
Curt Wohlgemuth14d7f3e2011-12-18 17:39:02 -05003632 ext4_lblk_t rr_cluster_start;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003633 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
3634 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
3635 unsigned short ee_len = ext4_ext_get_actual_len(ex);
3636
3637 /* The extent passed in that we are trying to match */
3638 ex_cluster_start = EXT4_B2C(sbi, ee_block);
3639 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
3640
3641 /* The requested region passed into ext4_map_blocks() */
3642 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003643
3644 if ((rr_cluster_start == ex_cluster_end) ||
3645 (rr_cluster_start == ex_cluster_start)) {
3646 if (rr_cluster_start == ex_cluster_end)
3647 ee_start += ee_len - 1;
3648 map->m_pblk = (ee_start & ~(sbi->s_cluster_ratio - 1)) +
3649 c_offset;
3650 map->m_len = min(map->m_len,
3651 (unsigned) sbi->s_cluster_ratio - c_offset);
3652 /*
3653 * Check for and handle this case:
3654 *
3655 * |--------- cluster # N-------------|
3656 * |------- extent ----|
3657 * |--- requested region ---|
3658 * |===========|
3659 */
3660
3661 if (map->m_lblk < ee_block)
3662 map->m_len = min(map->m_len, ee_block - map->m_lblk);
3663
3664 /*
3665 * Check for the case where there is already another allocated
3666 * block to the right of 'ex' but before the end of the cluster.
3667 *
3668 * |------------- cluster # N-------------|
3669 * |----- ex -----| |---- ex_right ----|
3670 * |------ requested region ------|
3671 * |================|
3672 */
3673 if (map->m_lblk > ee_block) {
3674 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
3675 map->m_len = min(map->m_len, next - map->m_lblk);
3676 }
Aditya Kalid8990242011-09-09 19:18:51 -04003677
3678 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003679 return 1;
3680 }
Aditya Kalid8990242011-09-09 19:18:51 -04003681
3682 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003683 return 0;
3684}
3685
3686
3687/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05003688 * Block allocation/map/preallocation routine for extents based files
3689 *
3690 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003691 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003692 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3693 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05003694 *
3695 * return > 0, number of of blocks already mapped/allocated
3696 * if create == 0 and these are pre-allocated blocks
3697 * buffer head is unmapped
3698 * otherwise blocks are mapped
3699 *
3700 * return = 0, if plain look up failed (blocks have not been allocated)
3701 * buffer head is unmapped
3702 *
3703 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003704 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003705int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3706 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07003707{
3708 struct ext4_ext_path *path = NULL;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003709 struct ext4_extent newex, *ex, *ex2;
3710 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003711 ext4_fsblk_t newblock = 0;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003712 int free_on_err = 0, err = 0, depth, ret;
3713 unsigned int allocated = 0, offset = 0;
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04003714 unsigned int allocated_clusters = 0;
Allison Hendersone8613042011-05-25 07:41:46 -04003715 unsigned int punched_out = 0;
3716 unsigned int result = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003717 struct ext4_allocation_request ar;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003718 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003719 ext4_lblk_t cluster_offset;
Alex Tomasa86c6182006-10-11 01:21:03 -07003720
Mingming84fe3be2009-09-01 08:44:37 -04003721 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003722 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003723 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07003724
3725 /* check in cache */
Robin Dong015861b2011-07-17 23:27:43 -04003726 if (!(flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) &&
3727 ext4_ext_in_cache(inode, map->m_lblk, &newex)) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003728 if (!newex.ee_start_lo && !newex.ee_start_hi) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04003729 if ((sbi->s_cluster_ratio > 1) &&
3730 ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
3731 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3732
Theodore Ts'oc2177052009-05-14 00:58:52 -04003733 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003734 /*
3735 * block isn't allocated yet and
3736 * user doesn't want to allocate it
3737 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003738 goto out2;
3739 }
3740 /* we should allocate requested block */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003741 } else {
Alex Tomasa86c6182006-10-11 01:21:03 -07003742 /* block is already allocated */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003743 if (sbi->s_cluster_ratio > 1)
3744 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003745 newblock = map->m_lblk
Dave Kleikamp8c55e202007-05-24 13:04:54 -04003746 - le32_to_cpu(newex.ee_block)
Theodore Ts'obf89d162010-10-27 21:30:14 -04003747 + ext4_ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003748 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003749 allocated = ext4_ext_get_actual_len(&newex) -
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003750 (map->m_lblk - le32_to_cpu(newex.ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -07003751 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07003752 }
3753 }
3754
3755 /* find extent for this block */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003756 path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07003757 if (IS_ERR(path)) {
3758 err = PTR_ERR(path);
3759 path = NULL;
3760 goto out2;
3761 }
3762
3763 depth = ext_depth(inode);
3764
3765 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003766 * consistent leaf must not be empty;
3767 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07003768 * this is why assert can't be put in ext4_ext_find_extent()
3769 */
Frank Mayhar273df552010-03-02 11:46:09 -05003770 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
3771 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04003772 "lblock: %lu, depth: %d pblock %lld",
3773 (unsigned long) map->m_lblk, depth,
3774 path[depth].p_block);
Surbhi Palande034fb4c2009-12-14 09:53:52 -05003775 err = -EIO;
3776 goto out2;
3777 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003778
Avantika Mathur7e028972006-12-06 20:41:33 -08003779 ex = path[depth].p_ext;
3780 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003781 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003782 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003783 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003784
3785 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003786 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04003787 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003788 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003789 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04003790
3791 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
3792
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003793 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003794 if (in_range(map->m_lblk, ee_block, ee_len)) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04003795 struct ext4_map_blocks punch_map;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04003796 ext4_fsblk_t partial_cluster = 0;
3797
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003798 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003799 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003800 allocated = ee_len - (map->m_lblk - ee_block);
3801 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
3802 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04003803
Allison Hendersone8613042011-05-25 07:41:46 -04003804 if ((flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) == 0) {
3805 /*
3806 * Do not put uninitialized extent
3807 * in the cache
3808 */
3809 if (!ext4_ext_is_uninitialized(ex)) {
3810 ext4_ext_put_in_cache(inode, ee_block,
3811 ee_len, ee_start);
3812 goto out;
3813 }
3814 ret = ext4_ext_handle_uninitialized_extents(
3815 handle, inode, map, path, flags,
3816 allocated, newblock);
3817 return ret;
Amit Arora56055d32007-07-17 21:42:38 -04003818 }
Allison Hendersone8613042011-05-25 07:41:46 -04003819
3820 /*
3821 * Punch out the map length, but only to the
3822 * end of the extent
3823 */
3824 punched_out = allocated < map->m_len ?
3825 allocated : map->m_len;
3826
3827 /*
3828 * Sense extents need to be converted to
3829 * uninitialized, they must fit in an
3830 * uninitialized extent
3831 */
3832 if (punched_out > EXT_UNINIT_MAX_LEN)
3833 punched_out = EXT_UNINIT_MAX_LEN;
3834
3835 punch_map.m_lblk = map->m_lblk;
3836 punch_map.m_pblk = newblock;
3837 punch_map.m_len = punched_out;
3838 punch_map.m_flags = 0;
3839
3840 /* Check to see if the extent needs to be split */
3841 if (punch_map.m_len != ee_len ||
3842 punch_map.m_lblk != ee_block) {
3843
3844 ret = ext4_split_extent(handle, inode,
3845 path, &punch_map, 0,
3846 EXT4_GET_BLOCKS_PUNCH_OUT_EXT |
3847 EXT4_GET_BLOCKS_PRE_IO);
3848
3849 if (ret < 0) {
3850 err = ret;
3851 goto out2;
3852 }
3853 /*
3854 * find extent for the block at
3855 * the start of the hole
3856 */
3857 ext4_ext_drop_refs(path);
3858 kfree(path);
3859
3860 path = ext4_ext_find_extent(inode,
3861 map->m_lblk, NULL);
3862 if (IS_ERR(path)) {
3863 err = PTR_ERR(path);
3864 path = NULL;
3865 goto out2;
3866 }
3867
3868 depth = ext_depth(inode);
3869 ex = path[depth].p_ext;
3870 ee_len = ext4_ext_get_actual_len(ex);
3871 ee_block = le32_to_cpu(ex->ee_block);
3872 ee_start = ext4_ext_pblock(ex);
3873
3874 }
3875
3876 ext4_ext_mark_uninitialized(ex);
3877
Allison Hendersonf7d0d372011-07-17 23:17:02 -04003878 ext4_ext_invalidate_cache(inode);
3879
3880 err = ext4_ext_rm_leaf(handle, inode, path,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04003881 &partial_cluster, map->m_lblk,
3882 map->m_lblk + punched_out);
Allison Hendersonf7d0d372011-07-17 23:17:02 -04003883
3884 if (!err && path->p_hdr->eh_entries == 0) {
3885 /*
3886 * Punch hole freed all of this sub tree,
3887 * so we need to correct eh_depth
3888 */
3889 err = ext4_ext_get_access(handle, inode, path);
3890 if (err == 0) {
3891 ext_inode_hdr(inode)->eh_depth = 0;
3892 ext_inode_hdr(inode)->eh_max =
3893 cpu_to_le16(ext4_ext_space_root(
3894 inode, 0));
3895
3896 err = ext4_ext_dirty(
3897 handle, inode, path);
3898 }
3899 }
Allison Hendersone8613042011-05-25 07:41:46 -04003900
3901 goto out2;
Alex Tomasa86c6182006-10-11 01:21:03 -07003902 }
3903 }
3904
Aditya Kali7b415bf2011-09-09 19:04:51 -04003905 if ((sbi->s_cluster_ratio > 1) &&
3906 ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
3907 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3908
Alex Tomasa86c6182006-10-11 01:21:03 -07003909 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003910 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07003911 * we couldn't try to create block if create flag is zero
3912 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04003913 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003914 /*
3915 * put just found gap into cache to speed up
3916 * subsequent requests
3917 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003918 ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
Alex Tomasa86c6182006-10-11 01:21:03 -07003919 goto out2;
3920 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003921
Alex Tomasa86c6182006-10-11 01:21:03 -07003922 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003923 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07003924 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003925 map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003926 newex.ee_block = cpu_to_le32(map->m_lblk);
3927 cluster_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
3928
3929 /*
3930 * If we are doing bigalloc, check to see if the extent returned
3931 * by ext4_ext_find_extent() implies a cluster we can use.
3932 */
3933 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04003934 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003935 ar.len = allocated = map->m_len;
3936 newblock = map->m_pblk;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003937 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003938 goto got_allocated_blocks;
3939 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003940
Alex Tomasc9de5602008-01-29 00:19:52 -05003941 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003942 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003943 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3944 if (err)
3945 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003946 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003947 ex2 = NULL;
3948 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
Alex Tomasc9de5602008-01-29 00:19:52 -05003949 if (err)
3950 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04003951
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003952 /* Check if the extent after searching to the right implies a
3953 * cluster we can use. */
3954 if ((sbi->s_cluster_ratio > 1) && ex2 &&
Aditya Kalid8990242011-09-09 19:18:51 -04003955 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003956 ar.len = allocated = map->m_len;
3957 newblock = map->m_pblk;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003958 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003959 goto got_allocated_blocks;
3960 }
3961
Amit Arora749269f2007-07-18 09:02:56 -04003962 /*
3963 * See if request is beyond maximum number of blocks we can have in
3964 * a single extent. For an initialized extent this limit is
3965 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3966 * EXT_UNINIT_MAX_LEN.
3967 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003968 if (map->m_len > EXT_INIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003969 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003970 map->m_len = EXT_INIT_MAX_LEN;
3971 else if (map->m_len > EXT_UNINIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003972 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003973 map->m_len = EXT_UNINIT_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04003974
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003975 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003976 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003977 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04003978 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003979 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04003980 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003981 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05003982
3983 /* allocate new block */
3984 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003985 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
3986 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003987 /*
3988 * We calculate the offset from the beginning of the cluster
3989 * for the logical block number, since when we allocate a
3990 * physical cluster, the physical block should start at the
3991 * same offset from the beginning of the cluster. This is
3992 * needed so that future calls to get_implied_cluster_alloc()
3993 * work correctly.
3994 */
3995 offset = map->m_lblk & (sbi->s_cluster_ratio - 1);
3996 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
3997 ar.goal -= offset;
3998 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05003999 if (S_ISREG(inode->i_mode))
4000 ar.flags = EXT4_MB_HINT_DATA;
4001 else
4002 /* disable in-core preallocation for non-regular files */
4003 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04004004 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4005 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05004006 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004007 if (!newblock)
4008 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04004009 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004010 ar.goal, newblock, allocated);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004011 free_on_err = 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004012 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004013 ar.len = EXT4_C2B(sbi, ar.len) - offset;
4014 if (ar.len > allocated)
4015 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004016
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004017got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07004018 /* try to insert new extent into found leaf and return */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004019 ext4_ext_store_pblock(&newex, newblock + offset);
Alex Tomasc9de5602008-01-29 00:19:52 -05004020 newex.ee_len = cpu_to_le16(ar.len);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004021 /* Mark uninitialized */
4022 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
Amit Aroraa2df2a62007-07-17 21:42:41 -04004023 ext4_ext_mark_uninitialized(&newex);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004024 /*
Jiaying Zhang744692d2010-03-04 16:14:02 -05004025 * io_end structure was created for every IO write to an
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004026 * uninitialized extent. To avoid unnecessary conversion,
Jiaying Zhang744692d2010-03-04 16:14:02 -05004027 * here we flag the IO that really needs the conversion.
Mingming5f524952009-11-10 10:48:04 -05004028 * For non asycn direct IO case, flag the inode state
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004029 * that we need to perform conversion when IO is done.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004030 */
Jiaying Zhang744692d2010-03-04 16:14:02 -05004031 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Tao Ma0edeb712011-10-31 17:30:44 -04004032 if (io)
4033 ext4_set_io_unwritten_flag(inode, io);
4034 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004035 ext4_set_inode_state(inode,
4036 EXT4_STATE_DIO_UNWRITTEN);
Mingming5f524952009-11-10 10:48:04 -05004037 }
Jiaying Zhang744692d2010-03-04 16:14:02 -05004038 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004039 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004040 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004041
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004042 err = 0;
4043 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
4044 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4045 path, ar.len);
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004046 if (!err)
4047 err = ext4_ext_insert_extent(handle, inode, path,
4048 &newex, flags);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004049 if (err && free_on_err) {
Maxim Patlasov7132de72011-07-10 19:37:48 -04004050 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
4051 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
Alex Tomas315054f2007-05-24 13:04:25 -04004052 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05004053 /* not a good idea to call discard here directly,
4054 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004055 ext4_discard_preallocations(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05004056 ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex),
Maxim Patlasov7132de72011-07-10 19:37:48 -04004057 ext4_ext_get_actual_len(&newex), fb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004058 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04004059 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004060
Alex Tomasa86c6182006-10-11 01:21:03 -07004061 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04004062 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004063 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004064 if (allocated > map->m_len)
4065 allocated = map->m_len;
4066 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07004067
Jan Karab436b9b2009-12-08 23:51:10 -05004068 /*
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004069 * Update reserved blocks/metadata blocks after successful
4070 * block allocation which had been deferred till now.
4071 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04004072 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004073 unsigned int reserved_clusters;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004074 /*
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004075 * Check how many clusters we had reserved this allocated range
Aditya Kali7b415bf2011-09-09 19:04:51 -04004076 */
4077 reserved_clusters = get_reserved_cluster_alloc(inode,
4078 map->m_lblk, allocated);
4079 if (map->m_flags & EXT4_MAP_FROM_CLUSTER) {
4080 if (reserved_clusters) {
4081 /*
4082 * We have clusters reserved for this range.
4083 * But since we are not doing actual allocation
4084 * and are simply using blocks from previously
4085 * allocated cluster, we should release the
4086 * reservation and not claim quota.
4087 */
4088 ext4_da_update_reserve_space(inode,
4089 reserved_clusters, 0);
4090 }
4091 } else {
4092 BUG_ON(allocated_clusters < reserved_clusters);
4093 /* We will claim quota for all newly allocated blocks.*/
4094 ext4_da_update_reserve_space(inode, allocated_clusters,
4095 1);
4096 if (reserved_clusters < allocated_clusters) {
Aditya Kali5356f262011-09-09 19:20:51 -04004097 struct ext4_inode_info *ei = EXT4_I(inode);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004098 int reservation = allocated_clusters -
4099 reserved_clusters;
4100 /*
4101 * It seems we claimed few clusters outside of
4102 * the range of this allocation. We should give
4103 * it back to the reservation pool. This can
4104 * happen in the following case:
4105 *
4106 * * Suppose s_cluster_ratio is 4 (i.e., each
4107 * cluster has 4 blocks. Thus, the clusters
4108 * are [0-3],[4-7],[8-11]...
4109 * * First comes delayed allocation write for
4110 * logical blocks 10 & 11. Since there were no
4111 * previous delayed allocated blocks in the
4112 * range [8-11], we would reserve 1 cluster
4113 * for this write.
4114 * * Next comes write for logical blocks 3 to 8.
4115 * In this case, we will reserve 2 clusters
4116 * (for [0-3] and [4-7]; and not for [8-11] as
4117 * that range has a delayed allocated blocks.
4118 * Thus total reserved clusters now becomes 3.
4119 * * Now, during the delayed allocation writeout
4120 * time, we will first write blocks [3-8] and
4121 * allocate 3 clusters for writing these
4122 * blocks. Also, we would claim all these
4123 * three clusters above.
4124 * * Now when we come here to writeout the
4125 * blocks [10-11], we would expect to claim
4126 * the reservation of 1 cluster we had made
4127 * (and we would claim it since there are no
4128 * more delayed allocated blocks in the range
4129 * [8-11]. But our reserved cluster count had
4130 * already gone to 0.
4131 *
4132 * Thus, at the step 4 above when we determine
4133 * that there are still some unwritten delayed
4134 * allocated blocks outside of our current
4135 * block range, we should increment the
4136 * reserved clusters count so that when the
4137 * remaining blocks finally gets written, we
4138 * could claim them.
4139 */
Aditya Kali5356f262011-09-09 19:20:51 -04004140 dquot_reserve_block(inode,
4141 EXT4_C2B(sbi, reservation));
4142 spin_lock(&ei->i_block_reservation_lock);
4143 ei->i_reserved_data_blocks += reservation;
4144 spin_unlock(&ei->i_block_reservation_lock);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004145 }
4146 }
4147 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004148
4149 /*
Jan Karab436b9b2009-12-08 23:51:10 -05004150 * Cache the extent and update transaction to commit on fdatasync only
4151 * when it is _not_ an uninitialized extent.
4152 */
4153 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05004154 ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock);
Jan Karab436b9b2009-12-08 23:51:10 -05004155 ext4_update_inode_fsync_trans(handle, inode, 1);
4156 } else
4157 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004158out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004159 if (allocated > map->m_len)
4160 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004161 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004162 map->m_flags |= EXT4_MAP_MAPPED;
4163 map->m_pblk = newblock;
4164 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004165out2:
4166 if (path) {
4167 ext4_ext_drop_refs(path);
4168 kfree(path);
4169 }
Allison Hendersone8613042011-05-25 07:41:46 -04004170 result = (flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) ?
4171 punched_out : allocated;
4172
Yongqiang Yange7b319e2011-10-29 09:39:51 -04004173 trace_ext4_ext_map_blocks_exit(inode, map->m_lblk,
4174 newblock, map->m_len, err ? err : result);
4175
Allison Hendersone8613042011-05-25 07:41:46 -04004176 return err ? err : result;
Alex Tomasa86c6182006-10-11 01:21:03 -07004177}
4178
Jan Karacf108bc2008-07-11 19:27:31 -04004179void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004180{
4181 struct address_space *mapping = inode->i_mapping;
4182 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004183 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004184 handle_t *handle;
Allison Henderson189e8682011-09-06 21:49:44 -04004185 loff_t page_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004186 int err = 0;
4187
4188 /*
Jiaying Zhang3889fd52011-01-10 12:47:05 -05004189 * finish any pending end_io work so we won't run the risk of
4190 * converting any truncated blocks to initialized later
4191 */
4192 ext4_flush_completed_IO(inode);
4193
4194 /*
Alex Tomasa86c6182006-10-11 01:21:03 -07004195 * probably first extent we're gonna free will be last in block
4196 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04004197 err = ext4_writepage_trans_blocks(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07004198 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04004199 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07004200 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07004201
Allison Henderson189e8682011-09-06 21:49:44 -04004202 if (inode->i_size % PAGE_CACHE_SIZE != 0) {
4203 page_len = PAGE_CACHE_SIZE -
4204 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4205
4206 err = ext4_discard_partial_page_buffers(handle,
4207 mapping, inode->i_size, page_len, 0);
4208
4209 if (err)
4210 goto out_stop;
4211 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004212
Jan Kara9ddfc3d2008-07-11 19:27:31 -04004213 if (ext4_orphan_add(handle, inode))
4214 goto out_stop;
4215
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004216 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07004217 ext4_ext_invalidate_cache(inode);
4218
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004219 ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05004220
Alex Tomasa86c6182006-10-11 01:21:03 -07004221 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004222 * TODO: optimization is possible here.
4223 * Probably we need not scan at all,
4224 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004225 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004226
4227 /* we have to know where to truncate from in crash case */
4228 EXT4_I(inode)->i_disksize = inode->i_size;
4229 ext4_mark_inode_dirty(handle, inode);
4230
4231 last_block = (inode->i_size + sb->s_blocksize - 1)
4232 >> EXT4_BLOCK_SIZE_BITS(sb);
Allison Hendersonc6a03712011-07-17 23:21:03 -04004233 err = ext4_ext_remove_space(inode, last_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07004234
4235 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04004236 * transaction synchronous.
4237 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004238 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05004239 ext4_handle_sync(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07004240
Jan Kara9ddfc3d2008-07-11 19:27:31 -04004241 up_write(&EXT4_I(inode)->i_data_sem);
Eric Gouriouf6d2f6b2011-05-22 21:33:00 -04004242
4243out_stop:
Alex Tomasa86c6182006-10-11 01:21:03 -07004244 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004245 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07004246 * then we need to clear up the orphan record which we created above.
4247 * However, if this was a real unlink then we were called by
4248 * ext4_delete_inode(), and we allow that function to clean up the
4249 * orphan info for us.
4250 */
4251 if (inode->i_nlink)
4252 ext4_orphan_del(handle, inode);
4253
Solofo Ramangalahyef737722008-04-29 22:00:41 -04004254 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4255 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07004256 ext4_journal_stop(handle);
4257}
4258
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004259static void ext4_falloc_update_inode(struct inode *inode,
4260 int mode, loff_t new_size, int update_ctime)
4261{
4262 struct timespec now;
4263
4264 if (update_ctime) {
4265 now = current_fs_time(inode->i_sb);
4266 if (!timespec_equal(&inode->i_ctime, &now))
4267 inode->i_ctime = now;
4268 }
4269 /*
4270 * Update only when preallocation was requested beyond
4271 * the file size.
4272 */
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04004273 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
4274 if (new_size > i_size_read(inode))
4275 i_size_write(inode, new_size);
4276 if (new_size > EXT4_I(inode)->i_disksize)
4277 ext4_update_i_disksize(inode, new_size);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004278 } else {
4279 /*
4280 * Mark that we allocate beyond EOF so the subsequent truncate
4281 * can proceed even if the new size is the same as i_size.
4282 */
4283 if (new_size > i_size_read(inode))
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004284 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004285 }
4286
4287}
4288
Amit Aroraa2df2a62007-07-17 21:42:41 -04004289/*
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004290 * preallocate space for a file. This implements ext4's fallocate file
Amit Aroraa2df2a62007-07-17 21:42:41 -04004291 * operation, which gets called from sys_fallocate system call.
4292 * For block-mapped files, posix_fallocate should fall back to the method
4293 * of writing zeroes to the required new blocks (the same behavior which is
4294 * expected for file systems which do not support fallocate() system call).
4295 */
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004296long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004297{
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004298 struct inode *inode = file->f_path.dentry->d_inode;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004299 handle_t *handle;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004300 loff_t new_size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004301 unsigned int max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004302 int ret = 0;
4303 int ret2 = 0;
4304 int retries = 0;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004305 int flags;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004306 struct ext4_map_blocks map;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004307 unsigned int credits, blkbits = inode->i_blkbits;
4308
4309 /*
4310 * currently supporting (pre)allocate mode for extent-based
4311 * files _only_
4312 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004313 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Amit Aroraa2df2a62007-07-17 21:42:41 -04004314 return -EOPNOTSUPP;
4315
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004316 /* Return error if mode is not supported */
4317 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
4318 return -EOPNOTSUPP;
4319
4320 if (mode & FALLOC_FL_PUNCH_HOLE)
4321 return ext4_punch_hole(file, offset, len);
4322
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004323 trace_ext4_fallocate_enter(inode, offset, len, mode);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004324 map.m_lblk = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004325 /*
4326 * We can't just convert len to max_blocks because
4327 * If blocksize = 4096 offset = 3072 and len = 2048
4328 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004329 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004330 - map.m_lblk;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004331 /*
Mingming Caof3bd1f32008-08-19 22:16:03 -04004332 * credits to insert 1 extent into extent tree
Amit Aroraa2df2a62007-07-17 21:42:41 -04004333 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04004334 credits = ext4_chunk_trans_blocks(inode, max_blocks);
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05004335 mutex_lock(&inode->i_mutex);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04004336 ret = inode_newsize_ok(inode, (len + offset));
4337 if (ret) {
4338 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004339 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04004340 return ret;
4341 }
Greg Harm3c6fe772011-10-31 18:41:47 -04004342 flags = EXT4_GET_BLOCKS_CREATE_UNINIT_EXT;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004343 if (mode & FALLOC_FL_KEEP_SIZE)
4344 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
Greg Harm3c6fe772011-10-31 18:41:47 -04004345 /*
4346 * Don't normalize the request if it can fit in one extent so
4347 * that it doesn't get unnecessarily split into multiple
4348 * extents.
4349 */
4350 if (len <= EXT_UNINIT_MAX_LEN << blkbits)
4351 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004352retry:
4353 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004354 map.m_lblk = map.m_lblk + ret;
4355 map.m_len = max_blocks = max_blocks - ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004356 handle = ext4_journal_start(inode, credits);
4357 if (IS_ERR(handle)) {
4358 ret = PTR_ERR(handle);
4359 break;
4360 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004361 ret = ext4_map_blocks(handle, inode, &map, flags);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05004362 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004363#ifdef EXT4FS_DEBUG
4364 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004365 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004366 "returned error inode#%lu, block=%u, "
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05004367 "max_blocks=%u", __func__,
Kazuya Mioa6371b62010-10-27 21:30:15 -04004368 inode->i_ino, map.m_lblk, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004369#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04004370 ext4_mark_inode_dirty(handle, inode);
4371 ret2 = ext4_journal_stop(handle);
4372 break;
4373 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004374 if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004375 blkbits) >> blkbits))
4376 new_size = offset + len;
4377 else
Utako Kusaka29ae07b2011-07-27 22:11:20 -04004378 new_size = ((loff_t) map.m_lblk + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004379
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004380 ext4_falloc_update_inode(inode, mode, new_size,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004381 (map.m_flags & EXT4_MAP_NEW));
Amit Aroraa2df2a62007-07-17 21:42:41 -04004382 ext4_mark_inode_dirty(handle, inode);
4383 ret2 = ext4_journal_stop(handle);
4384 if (ret2)
4385 break;
4386 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004387 if (ret == -ENOSPC &&
4388 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4389 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004390 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004391 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05004392 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004393 trace_ext4_fallocate_exit(inode, offset, max_blocks,
4394 ret > 0 ? ret2 : ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004395 return ret > 0 ? ret2 : ret;
4396}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004397
4398/*
Mingming Cao00314622009-09-28 15:49:08 -04004399 * This function convert a range of blocks to written extents
4400 * The caller of this function will pass the start offset and the size.
4401 * all unwritten extents within this range will be converted to
4402 * written extents.
4403 *
4404 * This function is called from the direct IO end io call back
4405 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05004406 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04004407 */
4408int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
Eric Sandeena1de02d2010-02-04 23:58:38 -05004409 ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04004410{
4411 handle_t *handle;
Mingming Cao00314622009-09-28 15:49:08 -04004412 unsigned int max_blocks;
4413 int ret = 0;
4414 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004415 struct ext4_map_blocks map;
Mingming Cao00314622009-09-28 15:49:08 -04004416 unsigned int credits, blkbits = inode->i_blkbits;
4417
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004418 map.m_lblk = offset >> blkbits;
Mingming Cao00314622009-09-28 15:49:08 -04004419 /*
4420 * We can't just convert len to max_blocks because
4421 * If blocksize = 4096 offset = 3072 and len = 2048
4422 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004423 max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
4424 map.m_lblk);
Mingming Cao00314622009-09-28 15:49:08 -04004425 /*
4426 * credits to insert 1 extent into extent tree
4427 */
4428 credits = ext4_chunk_trans_blocks(inode, max_blocks);
4429 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004430 map.m_lblk += ret;
4431 map.m_len = (max_blocks -= ret);
Mingming Cao00314622009-09-28 15:49:08 -04004432 handle = ext4_journal_start(inode, credits);
4433 if (IS_ERR(handle)) {
4434 ret = PTR_ERR(handle);
4435 break;
4436 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004437 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004438 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Mingming Cao00314622009-09-28 15:49:08 -04004439 if (ret <= 0) {
4440 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004441 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Mingming Cao00314622009-09-28 15:49:08 -04004442 "returned error inode#%lu, block=%u, "
4443 "max_blocks=%u", __func__,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004444 inode->i_ino, map.m_lblk, map.m_len);
Mingming Cao00314622009-09-28 15:49:08 -04004445 }
4446 ext4_mark_inode_dirty(handle, inode);
4447 ret2 = ext4_journal_stop(handle);
4448 if (ret <= 0 || ret2 )
4449 break;
4450 }
4451 return ret > 0 ? ret2 : ret;
4452}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004453
Mingming Cao00314622009-09-28 15:49:08 -04004454/*
Eric Sandeen6873fa02008-10-07 00:46:36 -04004455 * Callback function called for each extent to gather FIEMAP information.
4456 */
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04004457static int ext4_ext_fiemap_cb(struct inode *inode, ext4_lblk_t next,
Eric Sandeen6873fa02008-10-07 00:46:36 -04004458 struct ext4_ext_cache *newex, struct ext4_extent *ex,
4459 void *data)
4460{
Eric Sandeen6873fa02008-10-07 00:46:36 -04004461 __u64 logical;
4462 __u64 physical;
4463 __u64 length;
4464 __u32 flags = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004465 int ret = 0;
4466 struct fiemap_extent_info *fieinfo = data;
4467 unsigned char blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004468
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004469 blksize_bits = inode->i_sb->s_blocksize_bits;
4470 logical = (__u64)newex->ec_block << blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004471
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05004472 if (newex->ec_start == 0) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004473 /*
4474 * No extent in extent-tree contains block @newex->ec_start,
4475 * then the block may stay in 1)a hole or 2)delayed-extent.
4476 *
4477 * Holes or delayed-extents are processed as follows.
4478 * 1. lookup dirty pages with specified range in pagecache.
4479 * If no page is got, then there is no delayed-extent and
4480 * return with EXT_CONTINUE.
4481 * 2. find the 1st mapped buffer,
4482 * 3. check if the mapped buffer is both in the request range
4483 * and a delayed buffer. If not, there is no delayed-extent,
4484 * then return.
4485 * 4. a delayed-extent is found, the extent will be collected.
4486 */
4487 ext4_lblk_t end = 0;
4488 pgoff_t last_offset;
4489 pgoff_t offset;
4490 pgoff_t index;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004491 pgoff_t start_index = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004492 struct page **pages = NULL;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004493 struct buffer_head *bh = NULL;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004494 struct buffer_head *head = NULL;
4495 unsigned int nr_pages = PAGE_SIZE / sizeof(struct page *);
4496
4497 pages = kmalloc(PAGE_SIZE, GFP_KERNEL);
4498 if (pages == NULL)
4499 return -ENOMEM;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004500
4501 offset = logical >> PAGE_SHIFT;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004502repeat:
4503 last_offset = offset;
4504 head = NULL;
4505 ret = find_get_pages_tag(inode->i_mapping, &offset,
4506 PAGECACHE_TAG_DIRTY, nr_pages, pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004507
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004508 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4509 /* First time, try to find a mapped buffer. */
4510 if (ret == 0) {
4511out:
4512 for (index = 0; index < ret; index++)
4513 page_cache_release(pages[index]);
4514 /* just a hole. */
4515 kfree(pages);
4516 return EXT_CONTINUE;
4517 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004518 index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004519
Yongqiang Yangb2213492011-05-24 11:36:58 -04004520next_page:
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004521 /* Try to find the 1st mapped buffer. */
Yongqiang Yangb2213492011-05-24 11:36:58 -04004522 end = ((__u64)pages[index]->index << PAGE_SHIFT) >>
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004523 blksize_bits;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004524 if (!page_has_buffers(pages[index]))
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004525 goto out;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004526 head = page_buffers(pages[index]);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004527 if (!head)
4528 goto out;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004529
Yongqiang Yangb2213492011-05-24 11:36:58 -04004530 index++;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004531 bh = head;
4532 do {
Yongqiang Yangb2213492011-05-24 11:36:58 -04004533 if (end >= newex->ec_block +
4534 newex->ec_len)
4535 /* The buffer is out of
4536 * the request range.
4537 */
4538 goto out;
4539
4540 if (buffer_mapped(bh) &&
4541 end >= newex->ec_block) {
4542 start_index = index - 1;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004543 /* get the 1st mapped buffer. */
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004544 goto found_mapped_buffer;
4545 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004546
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004547 bh = bh->b_this_page;
4548 end++;
4549 } while (bh != head);
4550
Yongqiang Yangb2213492011-05-24 11:36:58 -04004551 /* No mapped buffer in the range found in this page,
4552 * We need to look up next page.
4553 */
4554 if (index >= ret) {
4555 /* There is no page left, but we need to limit
4556 * newex->ec_len.
4557 */
4558 newex->ec_len = end - newex->ec_block;
4559 goto out;
4560 }
4561 goto next_page;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004562 } else {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004563 /*Find contiguous delayed buffers. */
4564 if (ret > 0 && pages[0]->index == last_offset)
4565 head = page_buffers(pages[0]);
4566 bh = head;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004567 index = 1;
4568 start_index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004569 }
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004570
4571found_mapped_buffer:
4572 if (bh != NULL && buffer_delay(bh)) {
4573 /* 1st or contiguous delayed buffer found. */
4574 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4575 /*
4576 * 1st delayed buffer found, record
4577 * the start of extent.
4578 */
4579 flags |= FIEMAP_EXTENT_DELALLOC;
4580 newex->ec_block = end;
4581 logical = (__u64)end << blksize_bits;
4582 }
4583 /* Find contiguous delayed buffers. */
4584 do {
4585 if (!buffer_delay(bh))
4586 goto found_delayed_extent;
4587 bh = bh->b_this_page;
4588 end++;
4589 } while (bh != head);
4590
Yongqiang Yangb2213492011-05-24 11:36:58 -04004591 for (; index < ret; index++) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004592 if (!page_has_buffers(pages[index])) {
4593 bh = NULL;
4594 break;
4595 }
4596 head = page_buffers(pages[index]);
4597 if (!head) {
4598 bh = NULL;
4599 break;
4600 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004601
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004602 if (pages[index]->index !=
Yongqiang Yangb2213492011-05-24 11:36:58 -04004603 pages[start_index]->index + index
4604 - start_index) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004605 /* Blocks are not contiguous. */
4606 bh = NULL;
4607 break;
4608 }
4609 bh = head;
4610 do {
4611 if (!buffer_delay(bh))
4612 /* Delayed-extent ends. */
4613 goto found_delayed_extent;
4614 bh = bh->b_this_page;
4615 end++;
4616 } while (bh != head);
4617 }
4618 } else if (!(flags & FIEMAP_EXTENT_DELALLOC))
4619 /* a hole found. */
4620 goto out;
4621
4622found_delayed_extent:
4623 newex->ec_len = min(end - newex->ec_block,
4624 (ext4_lblk_t)EXT_INIT_MAX_LEN);
4625 if (ret == nr_pages && bh != NULL &&
4626 newex->ec_len < EXT_INIT_MAX_LEN &&
4627 buffer_delay(bh)) {
4628 /* Have not collected an extent and continue. */
4629 for (index = 0; index < ret; index++)
4630 page_cache_release(pages[index]);
4631 goto repeat;
4632 }
4633
4634 for (index = 0; index < ret; index++)
4635 page_cache_release(pages[index]);
4636 kfree(pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004637 }
4638
4639 physical = (__u64)newex->ec_start << blksize_bits;
4640 length = (__u64)newex->ec_len << blksize_bits;
4641
4642 if (ex && ext4_ext_is_uninitialized(ex))
4643 flags |= FIEMAP_EXTENT_UNWRITTEN;
4644
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04004645 if (next == EXT_MAX_BLOCKS)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004646 flags |= FIEMAP_EXTENT_LAST;
4647
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004648 ret = fiemap_fill_next_extent(fieinfo, logical, physical,
Eric Sandeen6873fa02008-10-07 00:46:36 -04004649 length, flags);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004650 if (ret < 0)
4651 return ret;
4652 if (ret == 1)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004653 return EXT_BREAK;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004654 return EXT_CONTINUE;
4655}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004656/* fiemap flags we can handle specified here */
4657#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
4658
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004659static int ext4_xattr_fiemap(struct inode *inode,
4660 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004661{
4662 __u64 physical = 0;
4663 __u64 length;
4664 __u32 flags = FIEMAP_EXTENT_LAST;
4665 int blockbits = inode->i_sb->s_blocksize_bits;
4666 int error = 0;
4667
4668 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004669 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04004670 struct ext4_iloc iloc;
4671 int offset; /* offset of xattr in inode */
4672
4673 error = ext4_get_inode_loc(inode, &iloc);
4674 if (error)
4675 return error;
4676 physical = iloc.bh->b_blocknr << blockbits;
4677 offset = EXT4_GOOD_OLD_INODE_SIZE +
4678 EXT4_I(inode)->i_extra_isize;
4679 physical += offset;
4680 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
4681 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004682 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004683 } else { /* external block */
4684 physical = EXT4_I(inode)->i_file_acl << blockbits;
4685 length = inode->i_sb->s_blocksize;
4686 }
4687
4688 if (physical)
4689 error = fiemap_fill_next_extent(fieinfo, 0, physical,
4690 length, flags);
4691 return (error < 0 ? error : 0);
4692}
4693
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004694/*
4695 * ext4_ext_punch_hole
4696 *
4697 * Punches a hole of "length" bytes in a file starting
4698 * at byte "offset"
4699 *
4700 * @inode: The inode of the file to punch a hole in
4701 * @offset: The starting byte offset of the hole
4702 * @length: The length of the hole
4703 *
4704 * Returns the number of blocks removed or negative on err
4705 */
4706int ext4_ext_punch_hole(struct file *file, loff_t offset, loff_t length)
4707{
4708 struct inode *inode = file->f_path.dentry->d_inode;
4709 struct super_block *sb = inode->i_sb;
4710 struct ext4_ext_cache cache_ex;
4711 ext4_lblk_t first_block, last_block, num_blocks, iblock, max_blocks;
4712 struct address_space *mapping = inode->i_mapping;
4713 struct ext4_map_blocks map;
4714 handle_t *handle;
Allison Hendersonba062082011-09-03 11:55:59 -04004715 loff_t first_page, last_page, page_len;
4716 loff_t first_page_offset, last_page_offset;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004717 int ret, credits, blocks_released, err = 0;
4718
Allison Henderson2be47512011-09-03 11:56:52 -04004719 /* No need to punch hole beyond i_size */
4720 if (offset >= inode->i_size)
4721 return 0;
4722
4723 /*
4724 * If the hole extends beyond i_size, set the hole
4725 * to end after the page that contains i_size
4726 */
4727 if (offset + length > inode->i_size) {
4728 length = inode->i_size +
4729 PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) -
4730 offset;
4731 }
4732
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004733 first_block = (offset + sb->s_blocksize - 1) >>
4734 EXT4_BLOCK_SIZE_BITS(sb);
4735 last_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
4736
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004737 first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
4738 last_page = (offset + length) >> PAGE_CACHE_SHIFT;
4739
4740 first_page_offset = first_page << PAGE_CACHE_SHIFT;
4741 last_page_offset = last_page << PAGE_CACHE_SHIFT;
4742
4743 /*
4744 * Write out all dirty pages to avoid race conditions
4745 * Then release them.
4746 */
4747 if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
4748 err = filemap_write_and_wait_range(mapping,
Allison Henderson2be47512011-09-03 11:56:52 -04004749 offset, offset + length - 1);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004750
Allison Henderson2be47512011-09-03 11:56:52 -04004751 if (err)
4752 return err;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004753 }
4754
4755 /* Now release the pages */
4756 if (last_page_offset > first_page_offset) {
4757 truncate_inode_pages_range(mapping, first_page_offset,
4758 last_page_offset-1);
4759 }
4760
4761 /* finish any pending end_io work */
4762 ext4_flush_completed_IO(inode);
4763
4764 credits = ext4_writepage_trans_blocks(inode);
4765 handle = ext4_journal_start(inode, credits);
4766 if (IS_ERR(handle))
4767 return PTR_ERR(handle);
4768
4769 err = ext4_orphan_add(handle, inode);
4770 if (err)
4771 goto out;
4772
4773 /*
Allison Hendersonba062082011-09-03 11:55:59 -04004774 * Now we need to zero out the non-page-aligned data in the
4775 * pages at the start and tail of the hole, and unmap the buffer
4776 * heads for the block aligned regions of the page that were
4777 * completely zeroed.
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004778 */
Allison Hendersonba062082011-09-03 11:55:59 -04004779 if (first_page > last_page) {
4780 /*
4781 * If the file space being truncated is contained within a page
4782 * just zero out and unmap the middle of that page
4783 */
4784 err = ext4_discard_partial_page_buffers(handle,
4785 mapping, offset, length, 0);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004786
Allison Hendersonba062082011-09-03 11:55:59 -04004787 if (err)
4788 goto out;
4789 } else {
4790 /*
4791 * zero out and unmap the partial page that contains
4792 * the start of the hole
4793 */
4794 page_len = first_page_offset - offset;
4795 if (page_len > 0) {
4796 err = ext4_discard_partial_page_buffers(handle, mapping,
4797 offset, page_len, 0);
4798 if (err)
4799 goto out;
4800 }
4801
4802 /*
4803 * zero out and unmap the partial page that contains
4804 * the end of the hole
4805 */
4806 page_len = offset + length - last_page_offset;
4807 if (page_len > 0) {
4808 err = ext4_discard_partial_page_buffers(handle, mapping,
4809 last_page_offset, page_len, 0);
4810 if (err)
4811 goto out;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004812 }
4813 }
4814
Allison Henderson2be47512011-09-03 11:56:52 -04004815
4816 /*
4817 * If i_size is contained in the last page, we need to
4818 * unmap and zero the partial page after i_size
4819 */
4820 if (inode->i_size >> PAGE_CACHE_SHIFT == last_page &&
4821 inode->i_size % PAGE_CACHE_SIZE != 0) {
4822
4823 page_len = PAGE_CACHE_SIZE -
4824 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4825
4826 if (page_len > 0) {
4827 err = ext4_discard_partial_page_buffers(handle,
4828 mapping, inode->i_size, page_len, 0);
4829
4830 if (err)
4831 goto out;
4832 }
4833 }
4834
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004835 /* If there are no blocks to remove, return now */
4836 if (first_block >= last_block)
4837 goto out;
4838
4839 down_write(&EXT4_I(inode)->i_data_sem);
4840 ext4_ext_invalidate_cache(inode);
4841 ext4_discard_preallocations(inode);
4842
4843 /*
4844 * Loop over all the blocks and identify blocks
4845 * that need to be punched out
4846 */
4847 iblock = first_block;
4848 blocks_released = 0;
4849 while (iblock < last_block) {
4850 max_blocks = last_block - iblock;
4851 num_blocks = 1;
4852 memset(&map, 0, sizeof(map));
4853 map.m_lblk = iblock;
4854 map.m_len = max_blocks;
4855 ret = ext4_ext_map_blocks(handle, inode, &map,
4856 EXT4_GET_BLOCKS_PUNCH_OUT_EXT);
4857
4858 if (ret > 0) {
4859 blocks_released += ret;
4860 num_blocks = ret;
4861 } else if (ret == 0) {
4862 /*
4863 * If map blocks could not find the block,
4864 * then it is in a hole. If the hole was
4865 * not already cached, then map blocks should
4866 * put it in the cache. So we can get the hole
4867 * out of the cache
4868 */
4869 memset(&cache_ex, 0, sizeof(cache_ex));
4870 if ((ext4_ext_check_cache(inode, iblock, &cache_ex)) &&
4871 !cache_ex.ec_start) {
4872
4873 /* The hole is cached */
4874 num_blocks = cache_ex.ec_block +
4875 cache_ex.ec_len - iblock;
4876
4877 } else {
4878 /* The block could not be identified */
4879 err = -EIO;
4880 break;
4881 }
4882 } else {
4883 /* Map blocks error */
4884 err = ret;
4885 break;
4886 }
4887
4888 if (num_blocks == 0) {
4889 /* This condition should never happen */
4890 ext_debug("Block lookup failed");
4891 err = -EIO;
4892 break;
4893 }
4894
4895 iblock += num_blocks;
4896 }
4897
4898 if (blocks_released > 0) {
4899 ext4_ext_invalidate_cache(inode);
4900 ext4_discard_preallocations(inode);
4901 }
4902
4903 if (IS_SYNC(inode))
4904 ext4_handle_sync(handle);
4905
4906 up_write(&EXT4_I(inode)->i_data_sem);
4907
4908out:
4909 ext4_orphan_del(handle, inode);
4910 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4911 ext4_mark_inode_dirty(handle, inode);
4912 ext4_journal_stop(handle);
4913 return err;
4914}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004915int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4916 __u64 start, __u64 len)
4917{
4918 ext4_lblk_t start_blk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004919 int error = 0;
4920
4921 /* fallback to generic here if not in extents fmt */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004922 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeen6873fa02008-10-07 00:46:36 -04004923 return generic_block_fiemap(inode, fieinfo, start, len,
4924 ext4_get_block);
4925
4926 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
4927 return -EBADR;
4928
4929 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
4930 error = ext4_xattr_fiemap(inode, fieinfo);
4931 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004932 ext4_lblk_t len_blks;
4933 __u64 last_blk;
4934
Eric Sandeen6873fa02008-10-07 00:46:36 -04004935 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004936 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04004937 if (last_blk >= EXT_MAX_BLOCKS)
4938 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004939 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004940
4941 /*
4942 * Walk the extent tree gathering extent information.
4943 * ext4_ext_fiemap_cb will push extents back to user.
4944 */
Eric Sandeen6873fa02008-10-07 00:46:36 -04004945 error = ext4_ext_walk_space(inode, start_blk, len_blks,
4946 ext4_ext_fiemap_cb, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004947 }
4948
4949 return error;
4950}