blob: c215985425854944e6746d4792555e46f0bc5706 [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
Lukas Czerner5f95d212012-03-19 23:03:19 -040047/*
48 * used by extent splitting.
49 */
50#define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
51 due to ENOSPC */
52#define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */
53#define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */
54
Dmitry Monakhov4319fd02012-10-10 01:04:58 -040055#define EXT4_EXT_DATA_VALID1 0x8 /* first half contains valid data */
56#define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */
57
Allison Hendersond583fb82011-05-25 07:41:43 -040058static int ext4_split_extent(handle_t *handle,
59 struct inode *inode,
60 struct ext4_ext_path *path,
61 struct ext4_map_blocks *map,
62 int split_flag,
63 int flags);
64
Lukas Czerner5f95d212012-03-19 23:03:19 -040065static int ext4_split_extent_at(handle_t *handle,
66 struct inode *inode,
67 struct ext4_ext_path *path,
68 ext4_lblk_t split,
69 int split_flag,
70 int flags);
71
Jan Kara487caee2009-08-17 22:17:20 -040072static int ext4_ext_truncate_extend_restart(handle_t *handle,
73 struct inode *inode,
74 int needed)
Alex Tomasa86c6182006-10-11 01:21:03 -070075{
76 int err;
77
Frank Mayhar03901312009-01-07 00:06:22 -050078 if (!ext4_handle_valid(handle))
79 return 0;
Alex Tomasa86c6182006-10-11 01:21:03 -070080 if (handle->h_buffer_credits > needed)
Shen Feng9102e4f2008-07-11 19:27:31 -040081 return 0;
82 err = ext4_journal_extend(handle, needed);
Theodore Ts'o0123c932008-08-01 20:57:54 -040083 if (err <= 0)
Shen Feng9102e4f2008-07-11 19:27:31 -040084 return err;
Jan Kara487caee2009-08-17 22:17:20 -040085 err = ext4_truncate_restart_trans(handle, inode, needed);
Dmitry Monakhov0617b832010-05-17 01:00:00 -040086 if (err == 0)
87 err = -EAGAIN;
Jan Kara487caee2009-08-17 22:17:20 -040088
89 return err;
Alex Tomasa86c6182006-10-11 01:21:03 -070090}
91
92/*
93 * could return:
94 * - EROFS
95 * - ENOMEM
96 */
97static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
98 struct ext4_ext_path *path)
99{
100 if (path->p_bh) {
101 /* path points to block */
102 return ext4_journal_get_write_access(handle, path->p_bh);
103 }
104 /* path points to leaf/index in inode body */
105 /* we use in-core data, no need to protect them */
106 return 0;
107}
108
109/*
110 * could return:
111 * - EROFS
112 * - ENOMEM
113 * - EIO
114 */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -0400115#define ext4_ext_dirty(handle, inode, path) \
116 __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path))
117static int __ext4_ext_dirty(const char *where, unsigned int line,
118 handle_t *handle, struct inode *inode,
119 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700120{
121 int err;
122 if (path->p_bh) {
123 /* path points to block */
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -0400124 err = __ext4_handle_dirty_metadata(where, line, handle,
125 inode, path->p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700126 } else {
127 /* path points to leaf/index in inode body */
128 err = ext4_mark_inode_dirty(handle, inode);
129 }
130 return err;
131}
132
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700133static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700134 struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500135 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700136{
Alex Tomasa86c6182006-10-11 01:21:03 -0700137 if (path) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400138 int depth = path->p_depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700139 struct ext4_extent *ex;
Alex Tomasa86c6182006-10-11 01:21:03 -0700140
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500141 /*
142 * Try to predict block placement assuming that we are
143 * filling in a file which will eventually be
144 * non-sparse --- i.e., in the case of libbfd writing
145 * an ELF object sections out-of-order but in a way
146 * the eventually results in a contiguous object or
147 * executable file, or some database extending a table
148 * space file. However, this is actually somewhat
149 * non-ideal if we are writing a sparse file such as
150 * qemu or KVM writing a raw image file that is going
151 * to stay fairly sparse, since it will end up
152 * fragmenting the file system's free space. Maybe we
153 * should have some hueristics or some way to allow
154 * userspace to pass a hint to file system,
Tao Mab8d65682011-01-21 23:21:31 +0800155 * especially if the latter case turns out to be
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500156 * common.
157 */
Avantika Mathur7e028972006-12-06 20:41:33 -0800158 ex = path[depth].p_ext;
Kazuya Mioad4fb9c2011-01-10 12:12:28 -0500159 if (ex) {
160 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
161 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
162
163 if (block > ext_block)
164 return ext_pblk + (block - ext_block);
165 else
166 return ext_pblk - (ext_block - block);
167 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700168
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700169 /* it looks like index is empty;
170 * try to find starting block from index itself */
Alex Tomasa86c6182006-10-11 01:21:03 -0700171 if (path[depth].p_bh)
172 return path[depth].p_bh->b_blocknr;
173 }
174
175 /* OK. use inode's group */
Eric Sandeenf86186b2011-06-28 10:01:31 -0400176 return ext4_inode_to_goal_block(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700177}
178
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400179/*
180 * Allocation for a meta data block
181 */
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700182static ext4_fsblk_t
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400183ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -0700184 struct ext4_ext_path *path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400185 struct ext4_extent *ex, int *err, unsigned int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -0700186{
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700187 ext4_fsblk_t goal, newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700188
189 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
Allison Henderson55f020d2011-05-25 07:41:26 -0400190 newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
191 NULL, err);
Alex Tomasa86c6182006-10-11 01:21:03 -0700192 return newblock;
193}
194
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400195static inline int ext4_ext_space_block(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700196{
197 int size;
198
199 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
200 / sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100201#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400202 if (!check && size > 6)
203 size = 6;
Alex Tomasa86c6182006-10-11 01:21:03 -0700204#endif
205 return size;
206}
207
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400208static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700209{
210 int size;
211
212 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
213 / sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100214#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400215 if (!check && size > 5)
216 size = 5;
Alex Tomasa86c6182006-10-11 01:21:03 -0700217#endif
218 return size;
219}
220
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400221static inline int ext4_ext_space_root(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700222{
223 int size;
224
225 size = sizeof(EXT4_I(inode)->i_data);
226 size -= sizeof(struct ext4_extent_header);
227 size /= sizeof(struct ext4_extent);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100228#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400229 if (!check && size > 3)
230 size = 3;
Alex Tomasa86c6182006-10-11 01:21:03 -0700231#endif
232 return size;
233}
234
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400235static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
Alex Tomasa86c6182006-10-11 01:21:03 -0700236{
237 int size;
238
239 size = sizeof(EXT4_I(inode)->i_data);
240 size -= sizeof(struct ext4_extent_header);
241 size /= sizeof(struct ext4_extent_idx);
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +0100242#ifdef AGGRESSIVE_TEST
Yongqiang Yang02dc62fb2011-10-29 09:29:11 -0400243 if (!check && size > 4)
244 size = 4;
Alex Tomasa86c6182006-10-11 01:21:03 -0700245#endif
246 return size;
247}
248
Mingming Caod2a17632008-07-14 17:52:37 -0400249/*
250 * Calculate the number of metadata blocks needed
251 * to allocate @blocks
252 * Worse case is one block per extent
253 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -0500254int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -0400255{
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500256 struct ext4_inode_info *ei = EXT4_I(inode);
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400257 int idxs;
Mingming Caod2a17632008-07-14 17:52:37 -0400258
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500259 idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
260 / sizeof(struct ext4_extent_idx));
Mingming Caod2a17632008-07-14 17:52:37 -0400261
262 /*
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500263 * If the new delayed allocation block is contiguous with the
264 * previous da block, it can share index blocks with the
265 * previous block, so we only need to allocate a new index
266 * block every idxs leaf blocks. At ldxs**2 blocks, we need
267 * an additional index block, and at ldxs**3 blocks, yet
268 * another index blocks.
Mingming Caod2a17632008-07-14 17:52:37 -0400269 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500270 if (ei->i_da_metadata_calc_len &&
271 ei->i_da_metadata_calc_last_lblock+1 == lblock) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400272 int num = 0;
273
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500274 if ((ei->i_da_metadata_calc_len % idxs) == 0)
275 num++;
276 if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
277 num++;
278 if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
279 num++;
280 ei->i_da_metadata_calc_len = 0;
281 } else
282 ei->i_da_metadata_calc_len++;
283 ei->i_da_metadata_calc_last_lblock++;
284 return num;
285 }
Mingming Caod2a17632008-07-14 17:52:37 -0400286
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500287 /*
288 * In the worst case we need a new set of index blocks at
289 * every level of the inode's extent tree.
290 */
291 ei->i_da_metadata_calc_len = 1;
292 ei->i_da_metadata_calc_last_lblock = lblock;
293 return ext_depth(inode) + 1;
Mingming Caod2a17632008-07-14 17:52:37 -0400294}
295
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400296static int
297ext4_ext_max_entries(struct inode *inode, int depth)
298{
299 int max;
300
301 if (depth == ext_depth(inode)) {
302 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400303 max = ext4_ext_space_root(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400304 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400305 max = ext4_ext_space_root_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400306 } else {
307 if (depth == 0)
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400308 max = ext4_ext_space_block(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400309 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400310 max = ext4_ext_space_block_idx(inode, 1);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400311 }
312
313 return max;
314}
315
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400316static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
317{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400318 ext4_fsblk_t block = ext4_ext_pblock(ext);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400319 int len = ext4_ext_get_actual_len(ext);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400320
Theodore Ts'o31d4f3a2012-03-11 23:30:16 -0400321 if (len == 0)
322 return 0;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400323 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400324}
325
326static int ext4_valid_extent_idx(struct inode *inode,
327 struct ext4_extent_idx *ext_idx)
328{
Theodore Ts'obf89d162010-10-27 21:30:14 -0400329 ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
Theodore Ts'oe84a26c2009-04-22 20:52:25 -0400330
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400331 return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400332}
333
334static int ext4_valid_extent_entries(struct inode *inode,
335 struct ext4_extent_header *eh,
336 int depth)
337{
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400338 unsigned short entries;
339 if (eh->eh_entries == 0)
340 return 1;
341
342 entries = le16_to_cpu(eh->eh_entries);
343
344 if (depth == 0) {
345 /* leaf entries */
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400346 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400347 while (entries) {
348 if (!ext4_valid_extent(inode, ext))
349 return 0;
350 ext++;
351 entries--;
352 }
353 } else {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -0400354 struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400355 while (entries) {
356 if (!ext4_valid_extent_idx(inode, ext_idx))
357 return 0;
358 ext_idx++;
359 entries--;
360 }
361 }
362 return 1;
363}
364
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400365static int __ext4_ext_check(const char *function, unsigned int line,
366 struct inode *inode, struct ext4_extent_header *eh,
367 int depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400368{
369 const char *error_msg;
370 int max = 0;
371
372 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
373 error_msg = "invalid magic";
374 goto corrupted;
375 }
376 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
377 error_msg = "unexpected eh_depth";
378 goto corrupted;
379 }
380 if (unlikely(eh->eh_max == 0)) {
381 error_msg = "invalid eh_max";
382 goto corrupted;
383 }
384 max = ext4_ext_max_entries(inode, depth);
385 if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
386 error_msg = "too large eh_max";
387 goto corrupted;
388 }
389 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
390 error_msg = "invalid eh_entries";
391 goto corrupted;
392 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400393 if (!ext4_valid_extent_entries(inode, eh, depth)) {
394 error_msg = "invalid extent entries";
395 goto corrupted;
396 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400397 return 0;
398
399corrupted:
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400400 ext4_error_inode(inode, function, line, 0,
Theodore Ts'o24676da2010-05-16 21:00:00 -0400401 "bad header/extent: %s - magic %x, "
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400402 "entries %u, max %u(%u), depth %u(%u)",
Theodore Ts'o24676da2010-05-16 21:00:00 -0400403 error_msg, le16_to_cpu(eh->eh_magic),
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400404 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
405 max, le16_to_cpu(eh->eh_depth), depth);
406
407 return -EIO;
408}
409
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -0400410#define ext4_ext_check(inode, eh, depth) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400411 __ext4_ext_check(__func__, __LINE__, inode, eh, depth)
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400412
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400413int ext4_ext_check_inode(struct inode *inode)
414{
415 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode));
416}
417
Alex Tomasa86c6182006-10-11 01:21:03 -0700418#ifdef EXT_DEBUG
419static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
420{
421 int k, l = path->p_depth;
422
423 ext_debug("path:");
424 for (k = 0; k <= l; k++, path++) {
425 if (path->p_idx) {
Mingming Cao2ae02102006-10-11 01:21:11 -0700426 ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400427 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700428 } else if (path->p_ext) {
Mingming553f9002009-09-18 13:34:55 -0400429 ext_debug(" %d:[%d]%d:%llu ",
Alex Tomasa86c6182006-10-11 01:21:03 -0700430 le32_to_cpu(path->p_ext->ee_block),
Mingming553f9002009-09-18 13:34:55 -0400431 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400432 ext4_ext_get_actual_len(path->p_ext),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400433 ext4_ext_pblock(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700434 } else
435 ext_debug(" []");
436 }
437 ext_debug("\n");
438}
439
440static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
441{
442 int depth = ext_depth(inode);
443 struct ext4_extent_header *eh;
444 struct ext4_extent *ex;
445 int i;
446
447 if (!path)
448 return;
449
450 eh = path[depth].p_hdr;
451 ex = EXT_FIRST_EXTENT(eh);
452
Mingming553f9002009-09-18 13:34:55 -0400453 ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
454
Alex Tomasa86c6182006-10-11 01:21:03 -0700455 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
Mingming553f9002009-09-18 13:34:55 -0400456 ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
457 ext4_ext_is_uninitialized(ex),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400458 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -0700459 }
460 ext_debug("\n");
461}
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400462
463static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
464 ext4_fsblk_t newblock, int level)
465{
466 int depth = ext_depth(inode);
467 struct ext4_extent *ex;
468
469 if (depth != level) {
470 struct ext4_extent_idx *idx;
471 idx = path[level].p_idx;
472 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
473 ext_debug("%d: move %d:%llu in new index %llu\n", level,
474 le32_to_cpu(idx->ei_block),
475 ext4_idx_pblock(idx),
476 newblock);
477 idx++;
478 }
479
480 return;
481 }
482
483 ex = path[depth].p_ext;
484 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
485 ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
486 le32_to_cpu(ex->ee_block),
487 ext4_ext_pblock(ex),
488 ext4_ext_is_uninitialized(ex),
489 ext4_ext_get_actual_len(ex),
490 newblock);
491 ex++;
492 }
493}
494
Alex Tomasa86c6182006-10-11 01:21:03 -0700495#else
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400496#define ext4_ext_show_path(inode, path)
497#define ext4_ext_show_leaf(inode, path)
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400498#define ext4_ext_show_move(inode, path, newblock, level)
Alex Tomasa86c6182006-10-11 01:21:03 -0700499#endif
500
Aneesh Kumar K.Vb35905c2008-02-25 16:54:37 -0500501void ext4_ext_drop_refs(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700502{
503 int depth = path->p_depth;
504 int i;
505
506 for (i = 0; i <= depth; i++, path++)
507 if (path->p_bh) {
508 brelse(path->p_bh);
509 path->p_bh = NULL;
510 }
511}
512
513/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700514 * ext4_ext_binsearch_idx:
515 * binary search for the closest index of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400516 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700517 */
518static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500519ext4_ext_binsearch_idx(struct inode *inode,
520 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700521{
522 struct ext4_extent_header *eh = path->p_hdr;
523 struct ext4_extent_idx *r, *l, *m;
524
Alex Tomasa86c6182006-10-11 01:21:03 -0700525
Eric Sandeenbba90742008-01-28 23:58:27 -0500526 ext_debug("binsearch for %u(idx): ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700527
528 l = EXT_FIRST_INDEX(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400529 r = EXT_LAST_INDEX(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700530 while (l <= r) {
531 m = l + (r - l) / 2;
532 if (block < le32_to_cpu(m->ei_block))
533 r = m - 1;
534 else
535 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400536 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
537 m, le32_to_cpu(m->ei_block),
538 r, le32_to_cpu(r->ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700539 }
540
541 path->p_idx = l - 1;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700542 ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400543 ext4_idx_pblock(path->p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -0700544
545#ifdef CHECK_BINSEARCH
546 {
547 struct ext4_extent_idx *chix, *ix;
548 int k;
549
550 chix = ix = EXT_FIRST_INDEX(eh);
551 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
552 if (k != 0 &&
553 le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
Theodore Ts'o47760042008-09-08 23:00:52 -0400554 printk(KERN_DEBUG "k=%d, ix=0x%p, "
555 "first=0x%p\n", k,
556 ix, EXT_FIRST_INDEX(eh));
557 printk(KERN_DEBUG "%u <= %u\n",
Alex Tomasa86c6182006-10-11 01:21:03 -0700558 le32_to_cpu(ix->ei_block),
559 le32_to_cpu(ix[-1].ei_block));
560 }
561 BUG_ON(k && le32_to_cpu(ix->ei_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400562 <= le32_to_cpu(ix[-1].ei_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700563 if (block < le32_to_cpu(ix->ei_block))
564 break;
565 chix = ix;
566 }
567 BUG_ON(chix != path->p_idx);
568 }
569#endif
570
571}
572
573/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700574 * ext4_ext_binsearch:
575 * binary search for closest extent of the given block
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400576 * the header must be checked before calling this
Alex Tomasa86c6182006-10-11 01:21:03 -0700577 */
578static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500579ext4_ext_binsearch(struct inode *inode,
580 struct ext4_ext_path *path, ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -0700581{
582 struct ext4_extent_header *eh = path->p_hdr;
583 struct ext4_extent *r, *l, *m;
584
Alex Tomasa86c6182006-10-11 01:21:03 -0700585 if (eh->eh_entries == 0) {
586 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700587 * this leaf is empty:
588 * we get such a leaf in split/add case
Alex Tomasa86c6182006-10-11 01:21:03 -0700589 */
590 return;
591 }
592
Eric Sandeenbba90742008-01-28 23:58:27 -0500593 ext_debug("binsearch for %u: ", block);
Alex Tomasa86c6182006-10-11 01:21:03 -0700594
595 l = EXT_FIRST_EXTENT(eh) + 1;
Dmitry Monakhove9f410b2007-07-18 09:09:15 -0400596 r = EXT_LAST_EXTENT(eh);
Alex Tomasa86c6182006-10-11 01:21:03 -0700597
598 while (l <= r) {
599 m = l + (r - l) / 2;
600 if (block < le32_to_cpu(m->ee_block))
601 r = m - 1;
602 else
603 l = m + 1;
Dmitry Monakhov26d535e2007-07-18 08:33:37 -0400604 ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
605 m, le32_to_cpu(m->ee_block),
606 r, le32_to_cpu(r->ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700607 }
608
609 path->p_ext = l - 1;
Mingming553f9002009-09-18 13:34:55 -0400610 ext_debug(" -> %d:%llu:[%d]%d ",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400611 le32_to_cpu(path->p_ext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -0400612 ext4_ext_pblock(path->p_ext),
Mingming553f9002009-09-18 13:34:55 -0400613 ext4_ext_is_uninitialized(path->p_ext),
Amit Aroraa2df2a62007-07-17 21:42:41 -0400614 ext4_ext_get_actual_len(path->p_ext));
Alex Tomasa86c6182006-10-11 01:21:03 -0700615
616#ifdef CHECK_BINSEARCH
617 {
618 struct ext4_extent *chex, *ex;
619 int k;
620
621 chex = ex = EXT_FIRST_EXTENT(eh);
622 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
623 BUG_ON(k && le32_to_cpu(ex->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400624 <= le32_to_cpu(ex[-1].ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -0700625 if (block < le32_to_cpu(ex->ee_block))
626 break;
627 chex = ex;
628 }
629 BUG_ON(chex != path->p_ext);
630 }
631#endif
632
633}
634
635int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
636{
637 struct ext4_extent_header *eh;
638
639 eh = ext_inode_hdr(inode);
640 eh->eh_depth = 0;
641 eh->eh_entries = 0;
642 eh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400643 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700644 ext4_mark_inode_dirty(handle, inode);
645 ext4_ext_invalidate_cache(inode);
646 return 0;
647}
648
649struct ext4_ext_path *
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500650ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
651 struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -0700652{
653 struct ext4_extent_header *eh;
654 struct buffer_head *bh;
655 short int depth, i, ppos = 0, alloc = 0;
656
657 eh = ext_inode_hdr(inode);
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400658 depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -0700659
660 /* account possible depth increase */
661 if (!path) {
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800662 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
Alex Tomasa86c6182006-10-11 01:21:03 -0700663 GFP_NOFS);
664 if (!path)
665 return ERR_PTR(-ENOMEM);
666 alloc = 1;
667 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700668 path[0].p_hdr = eh;
Shen Feng1973adc2008-07-11 19:27:31 -0400669 path[0].p_bh = NULL;
Alex Tomasa86c6182006-10-11 01:21:03 -0700670
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400671 i = depth;
Alex Tomasa86c6182006-10-11 01:21:03 -0700672 /* walk through the tree */
673 while (i) {
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400674 int need_to_validate = 0;
675
Alex Tomasa86c6182006-10-11 01:21:03 -0700676 ext_debug("depth %d: num %d, max %d\n",
677 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
Alex Tomasc29c0ae2007-07-18 09:19:09 -0400678
Alex Tomasa86c6182006-10-11 01:21:03 -0700679 ext4_ext_binsearch_idx(inode, path + ppos, block);
Theodore Ts'obf89d162010-10-27 21:30:14 -0400680 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
Alex Tomasa86c6182006-10-11 01:21:03 -0700681 path[ppos].p_depth = i;
682 path[ppos].p_ext = NULL;
683
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400684 bh = sb_getblk(inode->i_sb, path[ppos].p_block);
685 if (unlikely(!bh))
Alex Tomasa86c6182006-10-11 01:21:03 -0700686 goto err;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400687 if (!bh_uptodate_or_lock(bh)) {
Jiaying Zhang0562e0b2011-03-21 21:38:05 -0400688 trace_ext4_ext_load_extent(inode, block,
689 path[ppos].p_block);
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400690 if (bh_submit_read(bh) < 0) {
691 put_bh(bh);
692 goto err;
693 }
694 /* validate the extent entries */
695 need_to_validate = 1;
696 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700697 eh = ext_block_hdr(bh);
698 ppos++;
Frank Mayhar273df552010-03-02 11:46:09 -0500699 if (unlikely(ppos > depth)) {
700 put_bh(bh);
701 EXT4_ERROR_INODE(inode,
702 "ppos %d > depth %d", ppos, depth);
703 goto err;
704 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700705 path[ppos].p_bh = bh;
706 path[ppos].p_hdr = eh;
707 i--;
708
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -0400709 if (need_to_validate && ext4_ext_check(inode, eh, i))
Alex Tomasa86c6182006-10-11 01:21:03 -0700710 goto err;
711 }
712
713 path[ppos].p_depth = i;
Alex Tomasa86c6182006-10-11 01:21:03 -0700714 path[ppos].p_ext = NULL;
715 path[ppos].p_idx = NULL;
716
Alex Tomasa86c6182006-10-11 01:21:03 -0700717 /* find extent */
718 ext4_ext_binsearch(inode, path + ppos, block);
Shen Feng1973adc2008-07-11 19:27:31 -0400719 /* if not an empty leaf */
720 if (path[ppos].p_ext)
Theodore Ts'obf89d162010-10-27 21:30:14 -0400721 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
Alex Tomasa86c6182006-10-11 01:21:03 -0700722
723 ext4_ext_show_path(inode, path);
724
725 return path;
726
727err:
728 ext4_ext_drop_refs(path);
729 if (alloc)
730 kfree(path);
731 return ERR_PTR(-EIO);
732}
733
734/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700735 * ext4_ext_insert_index:
736 * insert new index [@logical;@ptr] into the block at @curp;
737 * check where to insert: before @curp or after @curp
Alex Tomasa86c6182006-10-11 01:21:03 -0700738 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -0400739static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
740 struct ext4_ext_path *curp,
741 int logical, ext4_fsblk_t ptr)
Alex Tomasa86c6182006-10-11 01:21:03 -0700742{
743 struct ext4_extent_idx *ix;
744 int len, err;
745
Avantika Mathur7e028972006-12-06 20:41:33 -0800746 err = ext4_ext_get_access(handle, inode, curp);
747 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700748 return err;
749
Frank Mayhar273df552010-03-02 11:46:09 -0500750 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
751 EXT4_ERROR_INODE(inode,
752 "logical %d == ei_block %d!",
753 logical, le32_to_cpu(curp->p_idx->ei_block));
754 return -EIO;
755 }
Robin Dongd4620312011-07-17 23:43:42 -0400756
757 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
758 >= le16_to_cpu(curp->p_hdr->eh_max))) {
759 EXT4_ERROR_INODE(inode,
760 "eh_entries %d >= eh_max %d!",
761 le16_to_cpu(curp->p_hdr->eh_entries),
762 le16_to_cpu(curp->p_hdr->eh_max));
763 return -EIO;
764 }
765
Alex Tomasa86c6182006-10-11 01:21:03 -0700766 if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
767 /* insert after */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400768 ext_debug("insert new index %d after: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700769 ix = curp->p_idx + 1;
770 } else {
771 /* insert before */
Eric Gouriou80e675f2011-10-27 11:52:18 -0400772 ext_debug("insert new index %d before: %llu\n", logical, ptr);
Alex Tomasa86c6182006-10-11 01:21:03 -0700773 ix = curp->p_idx;
774 }
775
Eric Gouriou80e675f2011-10-27 11:52:18 -0400776 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
777 BUG_ON(len < 0);
778 if (len > 0) {
779 ext_debug("insert new index %d: "
780 "move %d indices from 0x%p to 0x%p\n",
781 logical, len, ix, ix + 1);
782 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
783 }
784
Tao Maf472e022011-10-17 10:13:46 -0400785 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
786 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
787 return -EIO;
788 }
789
Alex Tomasa86c6182006-10-11 01:21:03 -0700790 ix->ei_block = cpu_to_le32(logical);
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700791 ext4_idx_store_pblock(ix, ptr);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400792 le16_add_cpu(&curp->p_hdr->eh_entries, 1);
Alex Tomasa86c6182006-10-11 01:21:03 -0700793
Frank Mayhar273df552010-03-02 11:46:09 -0500794 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
795 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
796 return -EIO;
797 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700798
799 err = ext4_ext_dirty(handle, inode, curp);
800 ext4_std_error(inode->i_sb, err);
801
802 return err;
803}
804
805/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700806 * ext4_ext_split:
807 * inserts new subtree into the path, using free index entry
808 * at depth @at:
809 * - allocates all needed blocks (new leaf and all intermediate index blocks)
810 * - makes decision where to split
811 * - moves remaining extents and index entries (right to the split point)
812 * into the newly allocated blocks
813 * - initializes subtree
Alex Tomasa86c6182006-10-11 01:21:03 -0700814 */
815static int ext4_ext_split(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -0400816 unsigned int flags,
817 struct ext4_ext_path *path,
818 struct ext4_extent *newext, int at)
Alex Tomasa86c6182006-10-11 01:21:03 -0700819{
820 struct buffer_head *bh = NULL;
821 int depth = ext_depth(inode);
822 struct ext4_extent_header *neh;
823 struct ext4_extent_idx *fidx;
Alex Tomasa86c6182006-10-11 01:21:03 -0700824 int i = at, k, m, a;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700825 ext4_fsblk_t newblock, oldblock;
Alex Tomasa86c6182006-10-11 01:21:03 -0700826 __le32 border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700827 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
Alex Tomasa86c6182006-10-11 01:21:03 -0700828 int err = 0;
829
830 /* make decision: where to split? */
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700831 /* FIXME: now decision is simplest: at current extent */
Alex Tomasa86c6182006-10-11 01:21:03 -0700832
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700833 /* if current leaf will be split, then we should use
Alex Tomasa86c6182006-10-11 01:21:03 -0700834 * border from split point */
Frank Mayhar273df552010-03-02 11:46:09 -0500835 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
836 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
837 return -EIO;
838 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700839 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
840 border = path[depth].p_ext[1].ee_block;
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700841 ext_debug("leaf will be split."
Alex Tomasa86c6182006-10-11 01:21:03 -0700842 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400843 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700844 } else {
845 border = newext->ee_block;
846 ext_debug("leaf will be added."
847 " next leaf starts at %d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -0400848 le32_to_cpu(border));
Alex Tomasa86c6182006-10-11 01:21:03 -0700849 }
850
851 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700852 * If error occurs, then we break processing
853 * and mark filesystem read-only. index won't
Alex Tomasa86c6182006-10-11 01:21:03 -0700854 * be inserted and tree will be in consistent
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700855 * state. Next mount will repair buffers too.
Alex Tomasa86c6182006-10-11 01:21:03 -0700856 */
857
858 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700859 * Get array to track all allocated blocks.
860 * We need this to handle errors and free blocks
861 * upon them.
Alex Tomasa86c6182006-10-11 01:21:03 -0700862 */
Avantika Mathur5d4958f2006-12-06 20:41:35 -0800863 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
Alex Tomasa86c6182006-10-11 01:21:03 -0700864 if (!ablocks)
865 return -ENOMEM;
Alex Tomasa86c6182006-10-11 01:21:03 -0700866
867 /* allocate all needed blocks */
868 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
869 for (a = 0; a < depth - at; a++) {
Aneesh Kumar K.V654b4902008-07-11 19:27:31 -0400870 newblock = ext4_ext_new_meta_block(handle, inode, path,
Allison Henderson55f020d2011-05-25 07:41:26 -0400871 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -0700872 if (newblock == 0)
873 goto cleanup;
874 ablocks[a] = newblock;
875 }
876
877 /* initialize new leaf */
878 newblock = ablocks[--a];
Frank Mayhar273df552010-03-02 11:46:09 -0500879 if (unlikely(newblock == 0)) {
880 EXT4_ERROR_INODE(inode, "newblock == 0!");
881 err = -EIO;
882 goto cleanup;
883 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700884 bh = sb_getblk(inode->i_sb, newblock);
885 if (!bh) {
886 err = -EIO;
887 goto cleanup;
888 }
889 lock_buffer(bh);
890
Avantika Mathur7e028972006-12-06 20:41:33 -0800891 err = ext4_journal_get_create_access(handle, bh);
892 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700893 goto cleanup;
894
895 neh = ext_block_hdr(bh);
896 neh->eh_entries = 0;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400897 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700898 neh->eh_magic = EXT4_EXT_MAGIC;
899 neh->eh_depth = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700900
Randy Dunlapd0d856e2006-10-11 01:21:07 -0700901 /* move remainder of path[depth] to the new leaf */
Frank Mayhar273df552010-03-02 11:46:09 -0500902 if (unlikely(path[depth].p_hdr->eh_entries !=
903 path[depth].p_hdr->eh_max)) {
904 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
905 path[depth].p_hdr->eh_entries,
906 path[depth].p_hdr->eh_max);
907 err = -EIO;
908 goto cleanup;
909 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700910 /* start copy from next extent */
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400911 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
912 ext4_ext_show_move(inode, path, newblock, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -0700913 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400914 struct ext4_extent *ex;
915 ex = EXT_FIRST_EXTENT(neh);
916 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400917 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700918 }
919
920 set_buffer_uptodate(bh);
921 unlock_buffer(bh);
922
Frank Mayhar03901312009-01-07 00:06:22 -0500923 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -0800924 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700925 goto cleanup;
926 brelse(bh);
927 bh = NULL;
928
929 /* correct old leaf */
930 if (m) {
Avantika Mathur7e028972006-12-06 20:41:33 -0800931 err = ext4_ext_get_access(handle, inode, path + depth);
932 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700933 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -0400934 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
Avantika Mathur7e028972006-12-06 20:41:33 -0800935 err = ext4_ext_dirty(handle, inode, path + depth);
936 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700937 goto cleanup;
938
939 }
940
941 /* create intermediate indexes */
942 k = depth - at - 1;
Frank Mayhar273df552010-03-02 11:46:09 -0500943 if (unlikely(k < 0)) {
944 EXT4_ERROR_INODE(inode, "k %d < 0!", k);
945 err = -EIO;
946 goto cleanup;
947 }
Alex Tomasa86c6182006-10-11 01:21:03 -0700948 if (k)
949 ext_debug("create %d intermediate indices\n", k);
950 /* insert new index into current index block */
951 /* current depth stored in i var */
952 i = depth - 1;
953 while (k--) {
954 oldblock = newblock;
955 newblock = ablocks[--a];
Eric Sandeenbba90742008-01-28 23:58:27 -0500956 bh = sb_getblk(inode->i_sb, newblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700957 if (!bh) {
958 err = -EIO;
959 goto cleanup;
960 }
961 lock_buffer(bh);
962
Avantika Mathur7e028972006-12-06 20:41:33 -0800963 err = ext4_journal_get_create_access(handle, bh);
964 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -0700965 goto cleanup;
966
967 neh = ext_block_hdr(bh);
968 neh->eh_entries = cpu_to_le16(1);
969 neh->eh_magic = EXT4_EXT_MAGIC;
Theodore Ts'o55ad63b2009-08-28 10:40:33 -0400970 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -0700971 neh->eh_depth = cpu_to_le16(depth - i);
972 fidx = EXT_FIRST_INDEX(neh);
973 fidx->ei_block = border;
Alex Tomasf65e6fb2006-10-11 01:21:05 -0700974 ext4_idx_store_pblock(fidx, oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700975
Eric Sandeenbba90742008-01-28 23:58:27 -0500976 ext_debug("int.index at %d (block %llu): %u -> %llu\n",
977 i, newblock, le32_to_cpu(border), oldblock);
Alex Tomasa86c6182006-10-11 01:21:03 -0700978
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400979 /* move remainder of path[i] to the new index block */
Frank Mayhar273df552010-03-02 11:46:09 -0500980 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
981 EXT_LAST_INDEX(path[i].p_hdr))) {
982 EXT4_ERROR_INODE(inode,
983 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
984 le32_to_cpu(path[i].p_ext->ee_block));
985 err = -EIO;
986 goto cleanup;
987 }
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400988 /* start copy indexes */
989 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
990 ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
991 EXT_MAX_INDEX(path[i].p_hdr));
992 ext4_ext_show_move(inode, path, newblock, i);
Alex Tomasa86c6182006-10-11 01:21:03 -0700993 if (m) {
Yongqiang Yang1b16da72011-05-25 17:41:48 -0400994 memmove(++fidx, path[i].p_idx,
Alex Tomasa86c6182006-10-11 01:21:03 -0700995 sizeof(struct ext4_extent_idx) * m);
Marcin Slusarze8546d02008-04-17 10:38:59 -0400996 le16_add_cpu(&neh->eh_entries, m);
Alex Tomasa86c6182006-10-11 01:21:03 -0700997 }
998 set_buffer_uptodate(bh);
999 unlock_buffer(bh);
1000
Frank Mayhar03901312009-01-07 00:06:22 -05001001 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001002 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001003 goto cleanup;
1004 brelse(bh);
1005 bh = NULL;
1006
1007 /* correct old index */
1008 if (m) {
1009 err = ext4_ext_get_access(handle, inode, path + i);
1010 if (err)
1011 goto cleanup;
Marcin Slusarze8546d02008-04-17 10:38:59 -04001012 le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
Alex Tomasa86c6182006-10-11 01:21:03 -07001013 err = ext4_ext_dirty(handle, inode, path + i);
1014 if (err)
1015 goto cleanup;
1016 }
1017
1018 i--;
1019 }
1020
1021 /* insert new index */
Alex Tomasa86c6182006-10-11 01:21:03 -07001022 err = ext4_ext_insert_index(handle, inode, path + at,
1023 le32_to_cpu(border), newblock);
1024
1025cleanup:
1026 if (bh) {
1027 if (buffer_locked(bh))
1028 unlock_buffer(bh);
1029 brelse(bh);
1030 }
1031
1032 if (err) {
1033 /* free all allocated blocks in error case */
1034 for (i = 0; i < depth; i++) {
1035 if (!ablocks[i])
1036 continue;
Peter Huewe7dc57612011-02-21 21:01:42 -05001037 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05001038 EXT4_FREE_BLOCKS_METADATA);
Alex Tomasa86c6182006-10-11 01:21:03 -07001039 }
1040 }
1041 kfree(ablocks);
1042
1043 return err;
1044}
1045
1046/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001047 * ext4_ext_grow_indepth:
1048 * implements tree growing procedure:
1049 * - allocates new block
1050 * - moves top-level data (index block or leaf) into the new block
1051 * - initializes new top-level, creating index that points to the
1052 * just created block
Alex Tomasa86c6182006-10-11 01:21:03 -07001053 */
1054static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001055 unsigned int flags,
Allison Henderson55f020d2011-05-25 07:41:26 -04001056 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001057{
Alex Tomasa86c6182006-10-11 01:21:03 -07001058 struct ext4_extent_header *neh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001059 struct buffer_head *bh;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07001060 ext4_fsblk_t newblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001061 int err = 0;
1062
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001063 newblock = ext4_ext_new_meta_block(handle, inode, NULL,
Allison Henderson55f020d2011-05-25 07:41:26 -04001064 newext, &err, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07001065 if (newblock == 0)
1066 return err;
1067
1068 bh = sb_getblk(inode->i_sb, newblock);
1069 if (!bh) {
1070 err = -EIO;
1071 ext4_std_error(inode->i_sb, err);
1072 return err;
1073 }
1074 lock_buffer(bh);
1075
Avantika Mathur7e028972006-12-06 20:41:33 -08001076 err = ext4_journal_get_create_access(handle, bh);
1077 if (err) {
Alex Tomasa86c6182006-10-11 01:21:03 -07001078 unlock_buffer(bh);
1079 goto out;
1080 }
1081
1082 /* move top-level index/leaf into new block */
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001083 memmove(bh->b_data, EXT4_I(inode)->i_data,
1084 sizeof(EXT4_I(inode)->i_data));
Alex Tomasa86c6182006-10-11 01:21:03 -07001085
1086 /* set size of new block */
1087 neh = ext_block_hdr(bh);
1088 /* old root could have indexes or leaves
1089 * so calculate e_max right way */
1090 if (ext_depth(inode))
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001091 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001092 else
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04001093 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07001094 neh->eh_magic = EXT4_EXT_MAGIC;
1095 set_buffer_uptodate(bh);
1096 unlock_buffer(bh);
1097
Frank Mayhar03901312009-01-07 00:06:22 -05001098 err = ext4_handle_dirty_metadata(handle, inode, bh);
Avantika Mathur7e028972006-12-06 20:41:33 -08001099 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001100 goto out;
1101
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001102 /* Update top-level index: num,max,pointer */
Alex Tomasa86c6182006-10-11 01:21:03 -07001103 neh = ext_inode_hdr(inode);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001104 neh->eh_entries = cpu_to_le16(1);
1105 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
1106 if (neh->eh_depth == 0) {
1107 /* Root extent block becomes index block */
1108 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
1109 EXT_FIRST_INDEX(neh)->ei_block =
1110 EXT_FIRST_EXTENT(neh)->ee_block;
1111 }
Mingming Cao2ae02102006-10-11 01:21:11 -07001112 ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001113 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
Andi Kleen5a0790c2010-06-14 13:28:03 -04001114 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001115 ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
Alex Tomasa86c6182006-10-11 01:21:03 -07001116
Paul Mackerrasb4611ab2011-12-12 11:00:56 -05001117 neh->eh_depth = cpu_to_le16(le16_to_cpu(neh->eh_depth) + 1);
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001118 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07001119out:
1120 brelse(bh);
1121
1122 return err;
1123}
1124
1125/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001126 * ext4_ext_create_new_leaf:
1127 * finds empty index and adds new leaf.
1128 * if no free index is found, then it requests in-depth growing.
Alex Tomasa86c6182006-10-11 01:21:03 -07001129 */
1130static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
Allison Henderson55f020d2011-05-25 07:41:26 -04001131 unsigned int flags,
1132 struct ext4_ext_path *path,
1133 struct ext4_extent *newext)
Alex Tomasa86c6182006-10-11 01:21:03 -07001134{
1135 struct ext4_ext_path *curp;
1136 int depth, i, err = 0;
1137
1138repeat:
1139 i = depth = ext_depth(inode);
1140
1141 /* walk up to the tree and look for free index entry */
1142 curp = path + depth;
1143 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
1144 i--;
1145 curp--;
1146 }
1147
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001148 /* we use already allocated block for index block,
1149 * so subsequent data blocks should be contiguous */
Alex Tomasa86c6182006-10-11 01:21:03 -07001150 if (EXT_HAS_FREE_INDEX(curp)) {
1151 /* if we found index with free entry, then use that
1152 * entry: create all needed subtree and add new leaf */
Allison Henderson55f020d2011-05-25 07:41:26 -04001153 err = ext4_ext_split(handle, inode, flags, path, newext, i);
Shen Feng787e0982008-07-11 19:27:31 -04001154 if (err)
1155 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07001156
1157 /* refill path */
1158 ext4_ext_drop_refs(path);
1159 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001160 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1161 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001162 if (IS_ERR(path))
1163 err = PTR_ERR(path);
1164 } else {
1165 /* tree is full, time to grow in depth */
Dmitry Monakhov1939dd82011-10-22 01:26:05 -04001166 err = ext4_ext_grow_indepth(handle, inode, flags, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001167 if (err)
1168 goto out;
1169
1170 /* refill path */
1171 ext4_ext_drop_refs(path);
1172 path = ext4_ext_find_extent(inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001173 (ext4_lblk_t)le32_to_cpu(newext->ee_block),
1174 path);
Alex Tomasa86c6182006-10-11 01:21:03 -07001175 if (IS_ERR(path)) {
1176 err = PTR_ERR(path);
1177 goto out;
1178 }
1179
1180 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001181 * only first (depth 0 -> 1) produces free space;
1182 * in all other cases we have to split the grown tree
Alex Tomasa86c6182006-10-11 01:21:03 -07001183 */
1184 depth = ext_depth(inode);
1185 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001186 /* now we need to split */
Alex Tomasa86c6182006-10-11 01:21:03 -07001187 goto repeat;
1188 }
1189 }
1190
1191out:
1192 return err;
1193}
1194
1195/*
Alex Tomas1988b512008-01-28 23:58:27 -05001196 * search the closest allocated block to the left for *logical
1197 * and returns it at @logical + it's physical address at @phys
1198 * if *logical is the smallest allocated block, the function
1199 * returns 0 at @phys
1200 * return value contains 0 (success) or error code
1201 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001202static int ext4_ext_search_left(struct inode *inode,
1203 struct ext4_ext_path *path,
1204 ext4_lblk_t *logical, ext4_fsblk_t *phys)
Alex Tomas1988b512008-01-28 23:58:27 -05001205{
1206 struct ext4_extent_idx *ix;
1207 struct ext4_extent *ex;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001208 int depth, ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001209
Frank Mayhar273df552010-03-02 11:46:09 -05001210 if (unlikely(path == NULL)) {
1211 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1212 return -EIO;
1213 }
Alex Tomas1988b512008-01-28 23:58:27 -05001214 depth = path->p_depth;
1215 *phys = 0;
1216
1217 if (depth == 0 && path->p_ext == NULL)
1218 return 0;
1219
1220 /* usually extent in the path covers blocks smaller
1221 * then *logical, but it can be that extent is the
1222 * first one in the file */
1223
1224 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001225 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001226 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001227 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1228 EXT4_ERROR_INODE(inode,
1229 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
1230 *logical, le32_to_cpu(ex->ee_block));
1231 return -EIO;
1232 }
Alex Tomas1988b512008-01-28 23:58:27 -05001233 while (--depth >= 0) {
1234 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001235 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1236 EXT4_ERROR_INODE(inode,
1237 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
Tao Ma6ee3b212011-10-08 16:08:34 -04001238 ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001239 EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
Tao Ma6ee3b212011-10-08 16:08:34 -04001240 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
Frank Mayhar273df552010-03-02 11:46:09 -05001241 depth);
1242 return -EIO;
1243 }
Alex Tomas1988b512008-01-28 23:58:27 -05001244 }
1245 return 0;
1246 }
1247
Frank Mayhar273df552010-03-02 11:46:09 -05001248 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1249 EXT4_ERROR_INODE(inode,
1250 "logical %d < ee_block %d + ee_len %d!",
1251 *logical, le32_to_cpu(ex->ee_block), ee_len);
1252 return -EIO;
1253 }
Alex Tomas1988b512008-01-28 23:58:27 -05001254
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001255 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001256 *phys = ext4_ext_pblock(ex) + ee_len - 1;
Alex Tomas1988b512008-01-28 23:58:27 -05001257 return 0;
1258}
1259
1260/*
1261 * search the closest allocated block to the right for *logical
1262 * and returns it at @logical + it's physical address at @phys
Tao Madf3ab172011-10-08 15:53:49 -04001263 * if *logical is the largest allocated block, the function
Alex Tomas1988b512008-01-28 23:58:27 -05001264 * returns 0 at @phys
1265 * return value contains 0 (success) or error code
1266 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001267static int ext4_ext_search_right(struct inode *inode,
1268 struct ext4_ext_path *path,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001269 ext4_lblk_t *logical, ext4_fsblk_t *phys,
1270 struct ext4_extent **ret_ex)
Alex Tomas1988b512008-01-28 23:58:27 -05001271{
1272 struct buffer_head *bh = NULL;
1273 struct ext4_extent_header *eh;
1274 struct ext4_extent_idx *ix;
1275 struct ext4_extent *ex;
1276 ext4_fsblk_t block;
Eric Sandeen395a87b2009-03-10 18:18:47 -04001277 int depth; /* Note, NOT eh_depth; depth from top of tree */
1278 int ee_len;
Alex Tomas1988b512008-01-28 23:58:27 -05001279
Frank Mayhar273df552010-03-02 11:46:09 -05001280 if (unlikely(path == NULL)) {
1281 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
1282 return -EIO;
1283 }
Alex Tomas1988b512008-01-28 23:58:27 -05001284 depth = path->p_depth;
1285 *phys = 0;
1286
1287 if (depth == 0 && path->p_ext == NULL)
1288 return 0;
1289
1290 /* usually extent in the path covers blocks smaller
1291 * then *logical, but it can be that extent is the
1292 * first one in the file */
1293
1294 ex = path[depth].p_ext;
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001295 ee_len = ext4_ext_get_actual_len(ex);
Alex Tomas1988b512008-01-28 23:58:27 -05001296 if (*logical < le32_to_cpu(ex->ee_block)) {
Frank Mayhar273df552010-03-02 11:46:09 -05001297 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
1298 EXT4_ERROR_INODE(inode,
1299 "first_extent(path[%d].p_hdr) != ex",
1300 depth);
1301 return -EIO;
1302 }
Alex Tomas1988b512008-01-28 23:58:27 -05001303 while (--depth >= 0) {
1304 ix = path[depth].p_idx;
Frank Mayhar273df552010-03-02 11:46:09 -05001305 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
1306 EXT4_ERROR_INODE(inode,
1307 "ix != EXT_FIRST_INDEX *logical %d!",
1308 *logical);
1309 return -EIO;
1310 }
Alex Tomas1988b512008-01-28 23:58:27 -05001311 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001312 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001313 }
1314
Frank Mayhar273df552010-03-02 11:46:09 -05001315 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
1316 EXT4_ERROR_INODE(inode,
1317 "logical %d < ee_block %d + ee_len %d!",
1318 *logical, le32_to_cpu(ex->ee_block), ee_len);
1319 return -EIO;
1320 }
Alex Tomas1988b512008-01-28 23:58:27 -05001321
1322 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
1323 /* next allocated block in this leaf */
1324 ex++;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001325 goto found_extent;
Alex Tomas1988b512008-01-28 23:58:27 -05001326 }
1327
1328 /* go up and search for index to the right */
1329 while (--depth >= 0) {
1330 ix = path[depth].p_idx;
1331 if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001332 goto got_index;
Alex Tomas1988b512008-01-28 23:58:27 -05001333 }
1334
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001335 /* we've gone up to the root and found no index to the right */
1336 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001337
Wu Fengguang25f1ee32008-11-25 17:24:23 -05001338got_index:
Alex Tomas1988b512008-01-28 23:58:27 -05001339 /* we've found index to the right, let's
1340 * follow it and find the closest allocated
1341 * block to the right */
1342 ix++;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001343 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001344 while (++depth < path->p_depth) {
1345 bh = sb_bread(inode->i_sb, block);
1346 if (bh == NULL)
1347 return -EIO;
1348 eh = ext_block_hdr(bh);
Eric Sandeen395a87b2009-03-10 18:18:47 -04001349 /* subtract from p_depth to get proper eh_depth */
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001350 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001351 put_bh(bh);
1352 return -EIO;
1353 }
1354 ix = EXT_FIRST_INDEX(eh);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001355 block = ext4_idx_pblock(ix);
Alex Tomas1988b512008-01-28 23:58:27 -05001356 put_bh(bh);
1357 }
1358
1359 bh = sb_bread(inode->i_sb, block);
1360 if (bh == NULL)
1361 return -EIO;
1362 eh = ext_block_hdr(bh);
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04001363 if (ext4_ext_check(inode, eh, path->p_depth - depth)) {
Alex Tomas1988b512008-01-28 23:58:27 -05001364 put_bh(bh);
1365 return -EIO;
1366 }
1367 ex = EXT_FIRST_EXTENT(eh);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001368found_extent:
Alex Tomas1988b512008-01-28 23:58:27 -05001369 *logical = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001370 *phys = ext4_ext_pblock(ex);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001371 *ret_ex = ex;
1372 if (bh)
1373 put_bh(bh);
Alex Tomas1988b512008-01-28 23:58:27 -05001374 return 0;
Alex Tomas1988b512008-01-28 23:58:27 -05001375}
1376
1377/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001378 * ext4_ext_next_allocated_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001379 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001380 * NOTE: it considers block number from index entry as
1381 * allocated block. Thus, index entries have to be consistent
1382 * with leaves.
Alex Tomasa86c6182006-10-11 01:21:03 -07001383 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001384static ext4_lblk_t
Alex Tomasa86c6182006-10-11 01:21:03 -07001385ext4_ext_next_allocated_block(struct ext4_ext_path *path)
1386{
1387 int depth;
1388
1389 BUG_ON(path == NULL);
1390 depth = path->p_depth;
1391
1392 if (depth == 0 && path->p_ext == NULL)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001393 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001394
1395 while (depth >= 0) {
1396 if (depth == path->p_depth) {
1397 /* leaf */
Curt Wohlgemuth6f8ff532011-10-26 04:38:59 -04001398 if (path[depth].p_ext &&
1399 path[depth].p_ext !=
Alex Tomasa86c6182006-10-11 01:21:03 -07001400 EXT_LAST_EXTENT(path[depth].p_hdr))
1401 return le32_to_cpu(path[depth].p_ext[1].ee_block);
1402 } else {
1403 /* index */
1404 if (path[depth].p_idx !=
1405 EXT_LAST_INDEX(path[depth].p_hdr))
1406 return le32_to_cpu(path[depth].p_idx[1].ei_block);
1407 }
1408 depth--;
1409 }
1410
Lukas Czernerf17722f2011-06-06 00:05:17 -04001411 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001412}
1413
1414/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001415 * ext4_ext_next_leaf_block:
Lukas Czernerf17722f2011-06-06 00:05:17 -04001416 * returns first allocated block from next leaf or EXT_MAX_BLOCKS
Alex Tomasa86c6182006-10-11 01:21:03 -07001417 */
Robin Dong57187892011-07-23 21:49:07 -04001418static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
Alex Tomasa86c6182006-10-11 01:21:03 -07001419{
1420 int depth;
1421
1422 BUG_ON(path == NULL);
1423 depth = path->p_depth;
1424
1425 /* zero-tree has no leaf blocks at all */
1426 if (depth == 0)
Lukas Czernerf17722f2011-06-06 00:05:17 -04001427 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001428
1429 /* go to index block */
1430 depth--;
1431
1432 while (depth >= 0) {
1433 if (path[depth].p_idx !=
1434 EXT_LAST_INDEX(path[depth].p_hdr))
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001435 return (ext4_lblk_t)
1436 le32_to_cpu(path[depth].p_idx[1].ei_block);
Alex Tomasa86c6182006-10-11 01:21:03 -07001437 depth--;
1438 }
1439
Lukas Czernerf17722f2011-06-06 00:05:17 -04001440 return EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07001441}
1442
1443/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001444 * ext4_ext_correct_indexes:
1445 * if leaf gets modified and modified extent is first in the leaf,
1446 * then we have to correct all indexes above.
Alex Tomasa86c6182006-10-11 01:21:03 -07001447 * TODO: do we need to correct tree in all cases?
1448 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001449static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07001450 struct ext4_ext_path *path)
1451{
1452 struct ext4_extent_header *eh;
1453 int depth = ext_depth(inode);
1454 struct ext4_extent *ex;
1455 __le32 border;
1456 int k, err = 0;
1457
1458 eh = path[depth].p_hdr;
1459 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001460
1461 if (unlikely(ex == NULL || eh == NULL)) {
1462 EXT4_ERROR_INODE(inode,
1463 "ex %p == NULL or eh %p == NULL", ex, eh);
1464 return -EIO;
1465 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001466
1467 if (depth == 0) {
1468 /* there is no tree at all */
1469 return 0;
1470 }
1471
1472 if (ex != EXT_FIRST_EXTENT(eh)) {
1473 /* we correct tree if first leaf got modified only */
1474 return 0;
1475 }
1476
1477 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001478 * TODO: we need correction if border is smaller than current one
Alex Tomasa86c6182006-10-11 01:21:03 -07001479 */
1480 k = depth - 1;
1481 border = path[depth].p_ext->ee_block;
Avantika Mathur7e028972006-12-06 20:41:33 -08001482 err = ext4_ext_get_access(handle, inode, path + k);
1483 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001484 return err;
1485 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001486 err = ext4_ext_dirty(handle, inode, path + k);
1487 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001488 return err;
1489
1490 while (k--) {
1491 /* change all left-side indexes */
1492 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1493 break;
Avantika Mathur7e028972006-12-06 20:41:33 -08001494 err = ext4_ext_get_access(handle, inode, path + k);
1495 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001496 break;
1497 path[k].p_idx->ei_block = border;
Avantika Mathur7e028972006-12-06 20:41:33 -08001498 err = ext4_ext_dirty(handle, inode, path + k);
1499 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001500 break;
1501 }
1502
1503 return err;
1504}
1505
Akira Fujita748de672009-06-17 19:24:03 -04001506int
Alex Tomasa86c6182006-10-11 01:21:03 -07001507ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1508 struct ext4_extent *ex2)
1509{
Amit Arora749269f2007-07-18 09:02:56 -04001510 unsigned short ext1_ee_len, ext2_ee_len, max_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001511
1512 /*
1513 * Make sure that either both extents are uninitialized, or
1514 * both are _not_.
1515 */
1516 if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
1517 return 0;
1518
Amit Arora749269f2007-07-18 09:02:56 -04001519 if (ext4_ext_is_uninitialized(ex1))
1520 max_len = EXT_UNINIT_MAX_LEN;
1521 else
1522 max_len = EXT_INIT_MAX_LEN;
1523
Amit Aroraa2df2a62007-07-17 21:42:41 -04001524 ext1_ee_len = ext4_ext_get_actual_len(ex1);
1525 ext2_ee_len = ext4_ext_get_actual_len(ex2);
1526
1527 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
Andrew Morton63f57932006-10-11 01:21:24 -07001528 le32_to_cpu(ex2->ee_block))
Alex Tomasa86c6182006-10-11 01:21:03 -07001529 return 0;
1530
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001531 /*
1532 * To allow future support for preallocated extents to be added
1533 * as an RO_COMPAT feature, refuse to merge to extents if
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001534 * this can result in the top bit of ee_len being set.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001535 */
Amit Arora749269f2007-07-18 09:02:56 -04001536 if (ext1_ee_len + ext2_ee_len > max_len)
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07001537 return 0;
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01001538#ifdef AGGRESSIVE_TEST
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05001539 if (ext1_ee_len >= 4)
Alex Tomasa86c6182006-10-11 01:21:03 -07001540 return 0;
1541#endif
1542
Theodore Ts'obf89d162010-10-27 21:30:14 -04001543 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
Alex Tomasa86c6182006-10-11 01:21:03 -07001544 return 1;
1545 return 0;
1546}
1547
1548/*
Amit Arora56055d32007-07-17 21:42:38 -04001549 * This function tries to merge the "ex" extent to the next extent in the tree.
1550 * It always tries to merge towards right. If you want to merge towards
1551 * left, pass "ex - 1" as argument instead of "ex".
1552 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
1553 * 1 if they got merged.
1554 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04001555static int ext4_ext_try_to_merge_right(struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001556 struct ext4_ext_path *path,
1557 struct ext4_extent *ex)
Amit Arora56055d32007-07-17 21:42:38 -04001558{
1559 struct ext4_extent_header *eh;
1560 unsigned int depth, len;
1561 int merge_done = 0;
1562 int uninitialized = 0;
1563
1564 depth = ext_depth(inode);
1565 BUG_ON(path[depth].p_hdr == NULL);
1566 eh = path[depth].p_hdr;
1567
1568 while (ex < EXT_LAST_EXTENT(eh)) {
1569 if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
1570 break;
1571 /* merge with next extent! */
1572 if (ext4_ext_is_uninitialized(ex))
1573 uninitialized = 1;
1574 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1575 + ext4_ext_get_actual_len(ex + 1));
1576 if (uninitialized)
1577 ext4_ext_mark_uninitialized(ex);
1578
1579 if (ex + 1 < EXT_LAST_EXTENT(eh)) {
1580 len = (EXT_LAST_EXTENT(eh) - ex - 1)
1581 * sizeof(struct ext4_extent);
1582 memmove(ex + 1, ex + 2, len);
1583 }
Marcin Slusarze8546d02008-04-17 10:38:59 -04001584 le16_add_cpu(&eh->eh_entries, -1);
Amit Arora56055d32007-07-17 21:42:38 -04001585 merge_done = 1;
1586 WARN_ON(eh->eh_entries == 0);
1587 if (!eh->eh_entries)
Theodore Ts'o24676da2010-05-16 21:00:00 -04001588 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
Amit Arora56055d32007-07-17 21:42:38 -04001589 }
1590
1591 return merge_done;
1592}
1593
1594/*
Yongqiang Yang197217a2011-05-03 11:45:29 -04001595 * This function tries to merge the @ex extent to neighbours in the tree.
1596 * return 1 if merge left else 0.
1597 */
1598static int ext4_ext_try_to_merge(struct inode *inode,
1599 struct ext4_ext_path *path,
1600 struct ext4_extent *ex) {
1601 struct ext4_extent_header *eh;
1602 unsigned int depth;
1603 int merge_done = 0;
1604 int ret = 0;
1605
1606 depth = ext_depth(inode);
1607 BUG_ON(path[depth].p_hdr == NULL);
1608 eh = path[depth].p_hdr;
1609
1610 if (ex > EXT_FIRST_EXTENT(eh))
1611 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
1612
1613 if (!merge_done)
1614 ret = ext4_ext_try_to_merge_right(inode, path, ex);
1615
1616 return ret;
1617}
1618
1619/*
Amit Arora25d14f92007-05-24 13:04:13 -04001620 * check if a portion of the "newext" extent overlaps with an
1621 * existing extent.
1622 *
1623 * If there is an overlap discovered, it updates the length of the newext
1624 * such that there will be no overlap, and then returns 1.
1625 * If there is no overlap found, it returns 0.
1626 */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001627static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
1628 struct inode *inode,
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001629 struct ext4_extent *newext,
1630 struct ext4_ext_path *path)
Amit Arora25d14f92007-05-24 13:04:13 -04001631{
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001632 ext4_lblk_t b1, b2;
Amit Arora25d14f92007-05-24 13:04:13 -04001633 unsigned int depth, len1;
1634 unsigned int ret = 0;
1635
1636 b1 = le32_to_cpu(newext->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04001637 len1 = ext4_ext_get_actual_len(newext);
Amit Arora25d14f92007-05-24 13:04:13 -04001638 depth = ext_depth(inode);
1639 if (!path[depth].p_ext)
1640 goto out;
1641 b2 = le32_to_cpu(path[depth].p_ext->ee_block);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001642 b2 &= ~(sbi->s_cluster_ratio - 1);
Amit Arora25d14f92007-05-24 13:04:13 -04001643
1644 /*
1645 * get the next allocated block if the extent in the path
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001646 * is before the requested block(s)
Amit Arora25d14f92007-05-24 13:04:13 -04001647 */
1648 if (b2 < b1) {
1649 b2 = ext4_ext_next_allocated_block(path);
Lukas Czernerf17722f2011-06-06 00:05:17 -04001650 if (b2 == EXT_MAX_BLOCKS)
Amit Arora25d14f92007-05-24 13:04:13 -04001651 goto out;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04001652 b2 &= ~(sbi->s_cluster_ratio - 1);
Amit Arora25d14f92007-05-24 13:04:13 -04001653 }
1654
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001655 /* check for wrap through zero on extent logical start block*/
Amit Arora25d14f92007-05-24 13:04:13 -04001656 if (b1 + len1 < b1) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04001657 len1 = EXT_MAX_BLOCKS - b1;
Amit Arora25d14f92007-05-24 13:04:13 -04001658 newext->ee_len = cpu_to_le16(len1);
1659 ret = 1;
1660 }
1661
1662 /* check for overlap */
1663 if (b1 + len1 > b2) {
1664 newext->ee_len = cpu_to_le16(b2 - b1);
1665 ret = 1;
1666 }
1667out:
1668 return ret;
1669}
1670
1671/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001672 * ext4_ext_insert_extent:
1673 * tries to merge requsted extent into the existing extent or
1674 * inserts requested extent as new one into the tree,
1675 * creating new leaf in the no-space case.
Alex Tomasa86c6182006-10-11 01:21:03 -07001676 */
1677int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1678 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04001679 struct ext4_extent *newext, int flag)
Alex Tomasa86c6182006-10-11 01:21:03 -07001680{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001681 struct ext4_extent_header *eh;
Alex Tomasa86c6182006-10-11 01:21:03 -07001682 struct ext4_extent *ex, *fex;
1683 struct ext4_extent *nearex; /* nearest extent */
1684 struct ext4_ext_path *npath = NULL;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001685 int depth, len, err;
1686 ext4_lblk_t next;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001687 unsigned uninitialized = 0;
Allison Henderson55f020d2011-05-25 07:41:26 -04001688 int flags = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07001689
Frank Mayhar273df552010-03-02 11:46:09 -05001690 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
1691 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
1692 return -EIO;
1693 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001694 depth = ext_depth(inode);
1695 ex = path[depth].p_ext;
Frank Mayhar273df552010-03-02 11:46:09 -05001696 if (unlikely(path[depth].p_hdr == NULL)) {
1697 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1698 return -EIO;
1699 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001700
1701 /* try to insert block into found extent and return */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001702 if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO)
Mingming Cao00314622009-09-28 15:49:08 -04001703 && ext4_can_extents_be_merged(inode, ex, newext)) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04001704 ext_debug("append [%d]%d block to %u:[%d]%d (from %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04001705 ext4_ext_is_uninitialized(newext),
1706 ext4_ext_get_actual_len(newext),
1707 le32_to_cpu(ex->ee_block),
1708 ext4_ext_is_uninitialized(ex),
1709 ext4_ext_get_actual_len(ex),
1710 ext4_ext_pblock(ex));
Avantika Mathur7e028972006-12-06 20:41:33 -08001711 err = ext4_ext_get_access(handle, inode, path + depth);
1712 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001713 return err;
Amit Aroraa2df2a62007-07-17 21:42:41 -04001714
1715 /*
1716 * ext4_can_extents_be_merged should have checked that either
1717 * both extents are uninitialized, or both aren't. Thus we
1718 * need to check only one of them here.
1719 */
1720 if (ext4_ext_is_uninitialized(ex))
1721 uninitialized = 1;
1722 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
1723 + ext4_ext_get_actual_len(newext));
1724 if (uninitialized)
1725 ext4_ext_mark_uninitialized(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001726 eh = path[depth].p_hdr;
1727 nearex = ex;
1728 goto merge;
1729 }
1730
Alex Tomasa86c6182006-10-11 01:21:03 -07001731 depth = ext_depth(inode);
1732 eh = path[depth].p_hdr;
1733 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
1734 goto has_space;
1735
1736 /* probably next leaf has space for us? */
1737 fex = EXT_LAST_EXTENT(eh);
Robin Dong598dbdf2011-07-11 18:24:01 -04001738 next = EXT_MAX_BLOCKS;
1739 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
Robin Dong57187892011-07-23 21:49:07 -04001740 next = ext4_ext_next_leaf_block(path);
Robin Dong598dbdf2011-07-11 18:24:01 -04001741 if (next != EXT_MAX_BLOCKS) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04001742 ext_debug("next leaf block - %u\n", next);
Alex Tomasa86c6182006-10-11 01:21:03 -07001743 BUG_ON(npath != NULL);
1744 npath = ext4_ext_find_extent(inode, next, NULL);
1745 if (IS_ERR(npath))
1746 return PTR_ERR(npath);
1747 BUG_ON(npath->p_depth != path->p_depth);
1748 eh = npath[depth].p_hdr;
1749 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001750 ext_debug("next leaf isn't full(%d)\n",
Alex Tomasa86c6182006-10-11 01:21:03 -07001751 le16_to_cpu(eh->eh_entries));
1752 path = npath;
Robin Dongffb505f2011-07-11 11:43:59 -04001753 goto has_space;
Alex Tomasa86c6182006-10-11 01:21:03 -07001754 }
1755 ext_debug("next leaf has no free space(%d,%d)\n",
1756 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
1757 }
1758
1759 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001760 * There is no free space in the found leaf.
1761 * We're gonna add a new leaf in the tree.
Alex Tomasa86c6182006-10-11 01:21:03 -07001762 */
Allison Henderson55f020d2011-05-25 07:41:26 -04001763 if (flag & EXT4_GET_BLOCKS_PUNCH_OUT_EXT)
1764 flags = EXT4_MB_USE_ROOT_BLOCKS;
1765 err = ext4_ext_create_new_leaf(handle, inode, flags, path, newext);
Alex Tomasa86c6182006-10-11 01:21:03 -07001766 if (err)
1767 goto cleanup;
1768 depth = ext_depth(inode);
1769 eh = path[depth].p_hdr;
1770
1771has_space:
1772 nearex = path[depth].p_ext;
1773
Avantika Mathur7e028972006-12-06 20:41:33 -08001774 err = ext4_ext_get_access(handle, inode, path + depth);
1775 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07001776 goto cleanup;
1777
1778 if (!nearex) {
1779 /* there is no extent in this leaf, create first one */
Yongqiang Yang32de6752011-11-01 18:56:41 -04001780 ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001781 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001782 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001783 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001784 ext4_ext_get_actual_len(newext));
Eric Gouriou80e675f2011-10-27 11:52:18 -04001785 nearex = EXT_FIRST_EXTENT(eh);
1786 } else {
1787 if (le32_to_cpu(newext->ee_block)
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001788 > le32_to_cpu(nearex->ee_block)) {
Eric Gouriou80e675f2011-10-27 11:52:18 -04001789 /* Insert after */
Yongqiang Yang32de6752011-11-01 18:56:41 -04001790 ext_debug("insert %u:%llu:[%d]%d before: "
1791 "nearest %p\n",
Dave Kleikamp8c55e202007-05-24 13:04:54 -04001792 le32_to_cpu(newext->ee_block),
Theodore Ts'obf89d162010-10-27 21:30:14 -04001793 ext4_ext_pblock(newext),
Mingming553f9002009-09-18 13:34:55 -04001794 ext4_ext_is_uninitialized(newext),
Amit Aroraa2df2a62007-07-17 21:42:41 -04001795 ext4_ext_get_actual_len(newext),
Eric Gouriou80e675f2011-10-27 11:52:18 -04001796 nearex);
1797 nearex++;
1798 } else {
1799 /* Insert before */
1800 BUG_ON(newext->ee_block == nearex->ee_block);
Yongqiang Yang32de6752011-11-01 18:56:41 -04001801 ext_debug("insert %u:%llu:[%d]%d after: "
1802 "nearest %p\n",
Eric Gouriou80e675f2011-10-27 11:52:18 -04001803 le32_to_cpu(newext->ee_block),
1804 ext4_ext_pblock(newext),
1805 ext4_ext_is_uninitialized(newext),
1806 ext4_ext_get_actual_len(newext),
1807 nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001808 }
Eric Gouriou80e675f2011-10-27 11:52:18 -04001809 len = EXT_LAST_EXTENT(eh) - nearex + 1;
1810 if (len > 0) {
Yongqiang Yang32de6752011-11-01 18:56:41 -04001811 ext_debug("insert %u:%llu:[%d]%d: "
Eric Gouriou80e675f2011-10-27 11:52:18 -04001812 "move %d extents from 0x%p to 0x%p\n",
1813 le32_to_cpu(newext->ee_block),
1814 ext4_ext_pblock(newext),
1815 ext4_ext_is_uninitialized(newext),
1816 ext4_ext_get_actual_len(newext),
1817 len, nearex, nearex + 1);
1818 memmove(nearex + 1, nearex,
1819 len * sizeof(struct ext4_extent));
1820 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001821 }
1822
Marcin Slusarze8546d02008-04-17 10:38:59 -04001823 le16_add_cpu(&eh->eh_entries, 1);
Eric Gouriou80e675f2011-10-27 11:52:18 -04001824 path[depth].p_ext = nearex;
Alex Tomasa86c6182006-10-11 01:21:03 -07001825 nearex->ee_block = newext->ee_block;
Theodore Ts'obf89d162010-10-27 21:30:14 -04001826 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
Alex Tomasa86c6182006-10-11 01:21:03 -07001827 nearex->ee_len = newext->ee_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07001828
1829merge:
1830 /* try to merge extents to the right */
Jiaying Zhang744692d2010-03-04 16:14:02 -05001831 if (!(flag & EXT4_GET_BLOCKS_PRE_IO))
Mingming Cao00314622009-09-28 15:49:08 -04001832 ext4_ext_try_to_merge(inode, path, nearex);
Alex Tomasa86c6182006-10-11 01:21:03 -07001833
1834 /* try to merge extents to the left */
1835
1836 /* time to correct all indexes above */
1837 err = ext4_ext_correct_indexes(handle, inode, path);
1838 if (err)
1839 goto cleanup;
1840
1841 err = ext4_ext_dirty(handle, inode, path + depth);
1842
1843cleanup:
1844 if (npath) {
1845 ext4_ext_drop_refs(npath);
1846 kfree(npath);
1847 }
Alex Tomasa86c6182006-10-11 01:21:03 -07001848 ext4_ext_invalidate_cache(inode);
1849 return err;
1850}
1851
Theodore Ts'o1f109d52010-10-27 21:30:14 -04001852static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block,
1853 ext4_lblk_t num, ext_prepare_callback func,
1854 void *cbdata)
Eric Sandeen6873fa02008-10-07 00:46:36 -04001855{
1856 struct ext4_ext_path *path = NULL;
1857 struct ext4_ext_cache cbex;
1858 struct ext4_extent *ex;
1859 ext4_lblk_t next, start = 0, end = 0;
1860 ext4_lblk_t last = block + num;
1861 int depth, exists, err = 0;
1862
1863 BUG_ON(func == NULL);
1864 BUG_ON(inode == NULL);
1865
Lukas Czernerf17722f2011-06-06 00:05:17 -04001866 while (block < last && block != EXT_MAX_BLOCKS) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04001867 num = last - block;
1868 /* find extent for this block */
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001869 down_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001870 path = ext4_ext_find_extent(inode, block, path);
Theodore Ts'ofab3a542009-12-09 21:30:02 -05001871 up_read(&EXT4_I(inode)->i_data_sem);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001872 if (IS_ERR(path)) {
1873 err = PTR_ERR(path);
1874 path = NULL;
1875 break;
1876 }
1877
1878 depth = ext_depth(inode);
Frank Mayhar273df552010-03-02 11:46:09 -05001879 if (unlikely(path[depth].p_hdr == NULL)) {
1880 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
1881 err = -EIO;
1882 break;
1883 }
Eric Sandeen6873fa02008-10-07 00:46:36 -04001884 ex = path[depth].p_ext;
1885 next = ext4_ext_next_allocated_block(path);
1886
1887 exists = 0;
1888 if (!ex) {
1889 /* there is no extent yet, so try to allocate
1890 * all requested space */
1891 start = block;
1892 end = block + num;
1893 } else if (le32_to_cpu(ex->ee_block) > block) {
1894 /* need to allocate space before found extent */
1895 start = block;
1896 end = le32_to_cpu(ex->ee_block);
1897 if (block + num < end)
1898 end = block + num;
1899 } else if (block >= le32_to_cpu(ex->ee_block)
1900 + ext4_ext_get_actual_len(ex)) {
1901 /* need to allocate space after found extent */
1902 start = block;
1903 end = block + num;
1904 if (end >= next)
1905 end = next;
1906 } else if (block >= le32_to_cpu(ex->ee_block)) {
1907 /*
1908 * some part of requested space is covered
1909 * by found extent
1910 */
1911 start = block;
1912 end = le32_to_cpu(ex->ee_block)
1913 + ext4_ext_get_actual_len(ex);
1914 if (block + num < end)
1915 end = block + num;
1916 exists = 1;
1917 } else {
1918 BUG();
1919 }
1920 BUG_ON(end <= start);
1921
1922 if (!exists) {
1923 cbex.ec_block = start;
1924 cbex.ec_len = end - start;
1925 cbex.ec_start = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04001926 } else {
1927 cbex.ec_block = le32_to_cpu(ex->ee_block);
1928 cbex.ec_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04001929 cbex.ec_start = ext4_ext_pblock(ex);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001930 }
1931
Frank Mayhar273df552010-03-02 11:46:09 -05001932 if (unlikely(cbex.ec_len == 0)) {
1933 EXT4_ERROR_INODE(inode, "cbex.ec_len == 0");
1934 err = -EIO;
1935 break;
1936 }
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04001937 err = func(inode, next, &cbex, ex, cbdata);
Eric Sandeen6873fa02008-10-07 00:46:36 -04001938 ext4_ext_drop_refs(path);
1939
1940 if (err < 0)
1941 break;
1942
1943 if (err == EXT_REPEAT)
1944 continue;
1945 else if (err == EXT_BREAK) {
1946 err = 0;
1947 break;
1948 }
1949
1950 if (ext_depth(inode) != depth) {
1951 /* depth was changed. we have to realloc path */
1952 kfree(path);
1953 path = NULL;
1954 }
1955
1956 block = cbex.ec_block + cbex.ec_len;
1957 }
1958
1959 if (path) {
1960 ext4_ext_drop_refs(path);
1961 kfree(path);
1962 }
1963
1964 return err;
1965}
1966
Avantika Mathur09b88252006-12-06 20:41:36 -08001967static void
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001968ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05001969 __u32 len, ext4_fsblk_t start)
Alex Tomasa86c6182006-10-11 01:21:03 -07001970{
1971 struct ext4_ext_cache *cex;
1972 BUG_ON(len == 0);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001973 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Aditya Kalid8990242011-09-09 19:18:51 -04001974 trace_ext4_ext_put_in_cache(inode, block, len, start);
Alex Tomasa86c6182006-10-11 01:21:03 -07001975 cex = &EXT4_I(inode)->i_cached_extent;
Alex Tomasa86c6182006-10-11 01:21:03 -07001976 cex->ec_block = block;
1977 cex->ec_len = len;
1978 cex->ec_start = start;
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04001979 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07001980}
1981
1982/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07001983 * ext4_ext_put_gap_in_cache:
1984 * calculate boundaries of the gap that the requested block fits into
Alex Tomasa86c6182006-10-11 01:21:03 -07001985 * and cache this gap
1986 */
Avantika Mathur09b88252006-12-06 20:41:36 -08001987static void
Alex Tomasa86c6182006-10-11 01:21:03 -07001988ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001989 ext4_lblk_t block)
Alex Tomasa86c6182006-10-11 01:21:03 -07001990{
1991 int depth = ext_depth(inode);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05001992 unsigned long len;
1993 ext4_lblk_t lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07001994 struct ext4_extent *ex;
1995
1996 ex = path[depth].p_ext;
1997 if (ex == NULL) {
1998 /* there is no extent yet, so gap is [0;-] */
1999 lblock = 0;
Lukas Czernerf17722f2011-06-06 00:05:17 -04002000 len = EXT_MAX_BLOCKS;
Alex Tomasa86c6182006-10-11 01:21:03 -07002001 ext_debug("cache gap(whole file):");
2002 } else if (block < le32_to_cpu(ex->ee_block)) {
2003 lblock = block;
2004 len = le32_to_cpu(ex->ee_block) - block;
Eric Sandeenbba90742008-01-28 23:58:27 -05002005 ext_debug("cache gap(before): %u [%u:%u]",
2006 block,
2007 le32_to_cpu(ex->ee_block),
2008 ext4_ext_get_actual_len(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002009 } else if (block >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002010 + ext4_ext_get_actual_len(ex)) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002011 ext4_lblk_t next;
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002012 lblock = le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002013 + ext4_ext_get_actual_len(ex);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002014
2015 next = ext4_ext_next_allocated_block(path);
Eric Sandeenbba90742008-01-28 23:58:27 -05002016 ext_debug("cache gap(after): [%u:%u] %u",
2017 le32_to_cpu(ex->ee_block),
2018 ext4_ext_get_actual_len(ex),
2019 block);
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002020 BUG_ON(next == lblock);
2021 len = next - lblock;
Alex Tomasa86c6182006-10-11 01:21:03 -07002022 } else {
2023 lblock = len = 0;
2024 BUG();
2025 }
2026
Eric Sandeenbba90742008-01-28 23:58:27 -05002027 ext_debug(" -> %u:%lu\n", lblock, len);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002028 ext4_ext_put_in_cache(inode, lblock, len, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002029}
2030
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002031/*
Robin Dongb7ca1e82011-07-23 21:53:25 -04002032 * ext4_ext_check_cache()
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002033 * Checks to see if the given block is in the cache.
2034 * If it is, the cached extent is stored in the given
2035 * cache extent pointer. If the cached extent is a hole,
2036 * this routine should be used instead of
2037 * ext4_ext_in_cache if the calling function needs to
2038 * know the size of the hole.
2039 *
2040 * @inode: The files inode
2041 * @block: The block to look for in the cache
2042 * @ex: Pointer where the cached extent will be stored
2043 * if it contains block
2044 *
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002045 * Return 0 if cache is invalid; 1 if the cache is valid
2046 */
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002047static int ext4_ext_check_cache(struct inode *inode, ext4_lblk_t block,
2048 struct ext4_ext_cache *ex){
Alex Tomasa86c6182006-10-11 01:21:03 -07002049 struct ext4_ext_cache *cex;
Vivek Haldar77f41352011-05-22 21:24:16 -04002050 struct ext4_sb_info *sbi;
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002051 int ret = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002052
Theodore Ts'o60e66792010-05-17 07:00:00 -04002053 /*
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002054 * We borrow i_block_reservation_lock to protect i_cached_extent
2055 */
2056 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Alex Tomasa86c6182006-10-11 01:21:03 -07002057 cex = &EXT4_I(inode)->i_cached_extent;
Vivek Haldar77f41352011-05-22 21:24:16 -04002058 sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002059
2060 /* has cache valid data? */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002061 if (cex->ec_len == 0)
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002062 goto errout;
Alex Tomasa86c6182006-10-11 01:21:03 -07002063
Akinobu Mita731eb1a2010-03-03 23:55:01 -05002064 if (in_range(block, cex->ec_block, cex->ec_len)) {
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002065 memcpy(ex, cex, sizeof(struct ext4_ext_cache));
Eric Sandeenbba90742008-01-28 23:58:27 -05002066 ext_debug("%u cached by %u:%u:%llu\n",
2067 block,
2068 cex->ec_block, cex->ec_len, cex->ec_start);
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05002069 ret = 1;
Alex Tomasa86c6182006-10-11 01:21:03 -07002070 }
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002071errout:
Aditya Kalid8990242011-09-09 19:18:51 -04002072 trace_ext4_ext_in_cache(inode, block, ret);
Theodore Ts'o2ec0ae32009-05-15 09:07:28 -04002073 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
2074 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07002075}
2076
2077/*
Allison Hendersona4bb6b62011-05-25 07:41:50 -04002078 * ext4_ext_in_cache()
2079 * Checks to see if the given block is in the cache.
2080 * If it is, the cached extent is stored in the given
2081 * extent pointer.
2082 *
2083 * @inode: The files inode
2084 * @block: The block to look for in the cache
2085 * @ex: Pointer where the cached extent will be stored
2086 * if it contains block
2087 *
2088 * Return 0 if cache is invalid; 1 if the cache is valid
2089 */
2090static int
2091ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
2092 struct ext4_extent *ex)
2093{
2094 struct ext4_ext_cache cex;
2095 int ret = 0;
2096
2097 if (ext4_ext_check_cache(inode, block, &cex)) {
2098 ex->ee_block = cpu_to_le32(cex.ec_block);
2099 ext4_ext_store_pblock(ex, cex.ec_start);
2100 ex->ee_len = cpu_to_le16(cex.ec_len);
2101 ret = 1;
2102 }
2103
2104 return ret;
2105}
2106
2107
2108/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002109 * ext4_ext_rm_idx:
2110 * removes index from the index block.
Alex Tomasa86c6182006-10-11 01:21:03 -07002111 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002112static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
Alex Tomasa86c6182006-10-11 01:21:03 -07002113 struct ext4_ext_path *path)
2114{
Alex Tomasa86c6182006-10-11 01:21:03 -07002115 int err;
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002116 ext4_fsblk_t leaf;
Alex Tomasa86c6182006-10-11 01:21:03 -07002117
2118 /* free index block */
2119 path--;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002120 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002121 if (unlikely(path->p_hdr->eh_entries == 0)) {
2122 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2123 return -EIO;
2124 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002125 err = ext4_ext_get_access(handle, inode, path);
2126 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002127 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002128
2129 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2130 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2131 len *= sizeof(struct ext4_extent_idx);
2132 memmove(path->p_idx, path->p_idx + 1, len);
2133 }
2134
Marcin Slusarze8546d02008-04-17 10:38:59 -04002135 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002136 err = ext4_ext_dirty(handle, inode, path);
2137 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002138 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002139 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Aditya Kalid8990242011-09-09 19:18:51 -04002140 trace_ext4_ext_rm_idx(inode, leaf);
2141
Peter Huewe7dc57612011-02-21 21:01:42 -05002142 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002143 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Alex Tomasa86c6182006-10-11 01:21:03 -07002144 return err;
2145}
2146
2147/*
Mingming Caoee12b632008-08-19 22:16:05 -04002148 * ext4_ext_calc_credits_for_single_extent:
2149 * This routine returns max. credits that needed to insert an extent
2150 * to the extent tree.
2151 * When pass the actual path, the caller should calculate credits
2152 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002153 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002154int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002155 struct ext4_ext_path *path)
2156{
Alex Tomasa86c6182006-10-11 01:21:03 -07002157 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002158 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002159 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002160
Alex Tomasa86c6182006-10-11 01:21:03 -07002161 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002162 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002163 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2164
2165 /*
2166 * There are some space in the leaf tree, no
2167 * need to account for leaf block credit
2168 *
2169 * bitmaps and block group descriptor blocks
Tao Madf3ab172011-10-08 15:53:49 -04002170 * and other metadata blocks still need to be
Mingming Caoee12b632008-08-19 22:16:05 -04002171 * accounted.
2172 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002173 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002174 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002175 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002176 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002177 }
2178
Mingming Cao525f4ed2008-08-19 22:15:58 -04002179 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002180}
Alex Tomasa86c6182006-10-11 01:21:03 -07002181
Mingming Caoee12b632008-08-19 22:16:05 -04002182/*
2183 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2184 *
2185 * if nrblocks are fit in a single extent (chunk flag is 1), then
2186 * in the worse case, each tree level index/leaf need to be changed
2187 * if the tree split due to insert a new extent, then the old tree
2188 * index/leaf need to be updated too
2189 *
2190 * If the nrblocks are discontiguous, they could cause
2191 * the whole tree split more than once, but this is really rare.
2192 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002193int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoee12b632008-08-19 22:16:05 -04002194{
2195 int index;
2196 int depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002197
Mingming Caoee12b632008-08-19 22:16:05 -04002198 if (chunk)
2199 index = depth * 2;
2200 else
2201 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002202
Mingming Caoee12b632008-08-19 22:16:05 -04002203 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002204}
2205
2206static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002207 struct ext4_extent *ex,
2208 ext4_fsblk_t *partial_cluster,
2209 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002210{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002211 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002212 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002213 ext4_fsblk_t pblk;
Theodore Ts'oe6362602009-11-23 07:17:05 -05002214 int flags = EXT4_FREE_BLOCKS_FORGET;
Alex Tomasa86c6182006-10-11 01:21:03 -07002215
Alex Tomasc9de5602008-01-29 00:19:52 -05002216 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
Theodore Ts'oe6362602009-11-23 07:17:05 -05002217 flags |= EXT4_FREE_BLOCKS_METADATA;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002218 /*
2219 * For bigalloc file systems, we never free a partial cluster
2220 * at the beginning of the extent. Instead, we make a note
2221 * that we tried freeing the cluster, and check to see if we
2222 * need to free it on a subsequent call to ext4_remove_blocks,
2223 * or at the end of the ext4_truncate() operation.
2224 */
2225 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
2226
Aditya Kalid8990242011-09-09 19:18:51 -04002227 trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002228 /*
2229 * If we have a partial cluster, and it's different from the
2230 * cluster of the last block, we need to explicitly free the
2231 * partial cluster here.
2232 */
2233 pblk = ext4_ext_pblock(ex) + ee_len - 1;
2234 if (*partial_cluster && (EXT4_B2C(sbi, pblk) != *partial_cluster)) {
2235 ext4_free_blocks(handle, inode, NULL,
2236 EXT4_C2B(sbi, *partial_cluster),
2237 sbi->s_cluster_ratio, flags);
2238 *partial_cluster = 0;
2239 }
2240
Alex Tomasa86c6182006-10-11 01:21:03 -07002241#ifdef EXTENTS_STATS
2242 {
2243 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002244 spin_lock(&sbi->s_ext_stats_lock);
2245 sbi->s_ext_blocks += ee_len;
2246 sbi->s_ext_extents++;
2247 if (ee_len < sbi->s_ext_min)
2248 sbi->s_ext_min = ee_len;
2249 if (ee_len > sbi->s_ext_max)
2250 sbi->s_ext_max = ee_len;
2251 if (ext_depth(inode) > sbi->s_depth_max)
2252 sbi->s_depth_max = ext_depth(inode);
2253 spin_unlock(&sbi->s_ext_stats_lock);
2254 }
2255#endif
2256 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002257 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002258 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002259 ext4_lblk_t num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002260
Amit Aroraa2df2a62007-07-17 21:42:41 -04002261 num = le32_to_cpu(ex->ee_block) + ee_len - from;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002262 pblk = ext4_ext_pblock(ex) + ee_len - num;
2263 ext_debug("free last %u blocks starting %llu\n", num, pblk);
2264 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
2265 /*
2266 * If the block range to be freed didn't start at the
2267 * beginning of a cluster, and we removed the entire
2268 * extent, save the partial cluster here, since we
2269 * might need to delete if we determine that the
2270 * truncate operation has removed all of the blocks in
2271 * the cluster.
2272 */
2273 if (pblk & (sbi->s_cluster_ratio - 1) &&
2274 (ee_len == num))
2275 *partial_cluster = EXT4_B2C(sbi, pblk);
2276 else
2277 *partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002278 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002279 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002280 /* head removal */
2281 ext4_lblk_t num;
2282 ext4_fsblk_t start;
2283
2284 num = to - from;
2285 start = ext4_ext_pblock(ex);
2286
2287 ext_debug("free first %u blocks starting %llu\n", num, start);
H Hartley Sweetenee90d572011-10-18 11:01:51 -04002288 ext4_free_blocks(handle, inode, NULL, start, num, flags);
Allison Hendersond583fb82011-05-25 07:41:43 -04002289
Alex Tomasa86c6182006-10-11 01:21:03 -07002290 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002291 printk(KERN_INFO "strange request: removal(2) "
2292 "%u-%u from %u:%u\n",
2293 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002294 }
2295 return 0;
2296}
2297
Allison Hendersond583fb82011-05-25 07:41:43 -04002298
2299/*
2300 * ext4_ext_rm_leaf() Removes the extents associated with the
2301 * blocks appearing between "start" and "end", and splits the extents
2302 * if "start" and "end" appear in the same extent
2303 *
2304 * @handle: The journal handle
2305 * @inode: The files inode
2306 * @path: The path to the leaf
2307 * @start: The first block to remove
2308 * @end: The last block to remove
2309 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002310static int
2311ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002312 struct ext4_ext_path *path, ext4_fsblk_t *partial_cluster,
2313 ext4_lblk_t start, ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002314{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002315 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002316 int err = 0, correct_index = 0;
2317 int depth = ext_depth(inode), credits;
2318 struct ext4_extent_header *eh;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002319 ext4_lblk_t a, b;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002320 unsigned num;
2321 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002322 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002323 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002324 struct ext4_extent *ex;
2325
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002326 /* the header must be checked already in ext4_ext_remove_space() */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002327 ext_debug("truncate since %u in leaf to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002328 if (!path[depth].p_hdr)
2329 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2330 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002331 if (unlikely(path[depth].p_hdr == NULL)) {
2332 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2333 return -EIO;
2334 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002335 /* find where to start removing */
2336 ex = EXT_LAST_EXTENT(eh);
2337
2338 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002339 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002340
Aditya Kalid8990242011-09-09 19:18:51 -04002341 trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster);
2342
Alex Tomasa86c6182006-10-11 01:21:03 -07002343 while (ex >= EXT_FIRST_EXTENT(eh) &&
2344 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002345
2346 if (ext4_ext_is_uninitialized(ex))
2347 uninitialized = 1;
2348 else
2349 uninitialized = 0;
2350
Mingming553f9002009-09-18 13:34:55 -04002351 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2352 uninitialized, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002353 path[depth].p_ext = ex;
2354
2355 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002356 b = ex_ee_block+ex_ee_len - 1 < end ?
2357 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002358
2359 ext_debug(" border %u:%u\n", a, b);
2360
Allison Hendersond583fb82011-05-25 07:41:43 -04002361 /* If this extent is beyond the end of the hole, skip it */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002362 if (end < ex_ee_block) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002363 ex--;
2364 ex_ee_block = le32_to_cpu(ex->ee_block);
2365 ex_ee_len = ext4_ext_get_actual_len(ex);
2366 continue;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002367 } else if (b != ex_ee_block + ex_ee_len - 1) {
Lukas Czernerdc1841d2012-03-19 23:07:43 -04002368 EXT4_ERROR_INODE(inode,
2369 "can not handle truncate %u:%u "
2370 "on extent %u:%u",
2371 start, end, ex_ee_block,
2372 ex_ee_block + ex_ee_len - 1);
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002373 err = -EIO;
2374 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002375 } else if (a != ex_ee_block) {
2376 /* remove tail of the extent */
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002377 num = a - ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002378 } else {
2379 /* remove whole extent: excellent! */
Alex Tomasa86c6182006-10-11 01:21:03 -07002380 num = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002381 }
Theodore Ts'o34071da2008-08-01 21:59:19 -04002382 /*
2383 * 3 for leaf, sb, and inode plus 2 (bmap and group
2384 * descriptor) for each block group; assume two block
2385 * groups plus ex_ee_len/blocks_per_block_group for
2386 * the worst case
2387 */
2388 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002389 if (ex == EXT_FIRST_EXTENT(eh)) {
2390 correct_index = 1;
2391 credits += (ext_depth(inode)) + 1;
2392 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002393 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002394
Jan Kara487caee2009-08-17 22:17:20 -04002395 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
Shen Feng9102e4f2008-07-11 19:27:31 -04002396 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002397 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002398
2399 err = ext4_ext_get_access(handle, inode, path + depth);
2400 if (err)
2401 goto out;
2402
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002403 err = ext4_remove_blocks(handle, inode, ex, partial_cluster,
2404 a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002405 if (err)
2406 goto out;
2407
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002408 if (num == 0)
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002409 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002410 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002411
Alex Tomasa86c6182006-10-11 01:21:03 -07002412 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002413 /*
2414 * Do not mark uninitialized if all the blocks in the
2415 * extent have been removed.
2416 */
2417 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002418 ext4_ext_mark_uninitialized(ex);
Allison Hendersond583fb82011-05-25 07:41:43 -04002419 /*
2420 * If the extent was completely released,
2421 * we need to remove it from the leaf
2422 */
2423 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002424 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002425 /*
2426 * For hole punching, we need to scoot all the
2427 * extents up when an extent is removed so that
2428 * we dont have blank extents in the middle
2429 */
2430 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2431 sizeof(struct ext4_extent));
2432
2433 /* Now get rid of the one at the end */
2434 memset(EXT_LAST_EXTENT(eh), 0,
2435 sizeof(struct ext4_extent));
2436 }
2437 le16_add_cpu(&eh->eh_entries, -1);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002438 } else
2439 *partial_cluster = 0;
Allison Hendersond583fb82011-05-25 07:41:43 -04002440
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002441 err = ext4_ext_dirty(handle, inode, path + depth);
2442 if (err)
2443 goto out;
2444
Yongqiang Yangbf52c6f2011-11-01 18:59:26 -04002445 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002446 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002447 ex--;
2448 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002449 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002450 }
2451
2452 if (correct_index && eh->eh_entries)
2453 err = ext4_ext_correct_indexes(handle, inode, path);
2454
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002455 /*
2456 * If there is still a entry in the leaf node, check to see if
2457 * it references the partial cluster. This is the only place
2458 * where it could; if it doesn't, we can free the cluster.
2459 */
2460 if (*partial_cluster && ex >= EXT_FIRST_EXTENT(eh) &&
2461 (EXT4_B2C(sbi, ext4_ext_pblock(ex) + ex_ee_len - 1) !=
2462 *partial_cluster)) {
2463 int flags = EXT4_FREE_BLOCKS_FORGET;
2464
2465 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2466 flags |= EXT4_FREE_BLOCKS_METADATA;
2467
2468 ext4_free_blocks(handle, inode, NULL,
2469 EXT4_C2B(sbi, *partial_cluster),
2470 sbi->s_cluster_ratio, flags);
2471 *partial_cluster = 0;
2472 }
2473
Alex Tomasa86c6182006-10-11 01:21:03 -07002474 /* if this leaf is free, then we should
2475 * remove it from index block above */
2476 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
2477 err = ext4_ext_rm_idx(handle, inode, path + depth);
2478
2479out:
2480 return err;
2481}
2482
2483/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002484 * ext4_ext_more_to_rm:
2485 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002486 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002487static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002488ext4_ext_more_to_rm(struct ext4_ext_path *path)
2489{
2490 BUG_ON(path->p_idx == NULL);
2491
2492 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2493 return 0;
2494
2495 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002496 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002497 * so we have to consider current index for truncation
2498 */
2499 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2500 return 0;
2501 return 1;
2502}
2503
Lukas Czerner5f95d212012-03-19 23:03:19 -04002504static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2505 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002506{
2507 struct super_block *sb = inode->i_sb;
2508 int depth = ext_depth(inode);
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002509 struct ext4_ext_path *path = NULL;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002510 ext4_fsblk_t partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002511 handle_t *handle;
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002512 int i = 0, err;
Alex Tomasa86c6182006-10-11 01:21:03 -07002513
Lukas Czerner5f95d212012-03-19 23:03:19 -04002514 ext_debug("truncate since %u to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002515
2516 /* probably first extent we're gonna free will be last in block */
2517 handle = ext4_journal_start(inode, depth + 1);
2518 if (IS_ERR(handle))
2519 return PTR_ERR(handle);
2520
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002521again:
Alex Tomasa86c6182006-10-11 01:21:03 -07002522 ext4_ext_invalidate_cache(inode);
2523
Aditya Kalid8990242011-09-09 19:18:51 -04002524 trace_ext4_ext_remove_space(inode, start, depth);
2525
Alex Tomasa86c6182006-10-11 01:21:03 -07002526 /*
Lukas Czerner5f95d212012-03-19 23:03:19 -04002527 * Check if we are removing extents inside the extent tree. If that
2528 * is the case, we are going to punch a hole inside the extent tree
2529 * so we have to check whether we need to split the extent covering
2530 * the last block to remove so we can easily remove the part of it
2531 * in ext4_ext_rm_leaf().
2532 */
2533 if (end < EXT_MAX_BLOCKS - 1) {
2534 struct ext4_extent *ex;
2535 ext4_lblk_t ee_block;
2536
2537 /* find extent for this block */
2538 path = ext4_ext_find_extent(inode, end, NULL);
2539 if (IS_ERR(path)) {
2540 ext4_journal_stop(handle);
2541 return PTR_ERR(path);
2542 }
2543 depth = ext_depth(inode);
2544 ex = path[depth].p_ext;
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002545 if (!ex) {
2546 ext4_ext_drop_refs(path);
2547 kfree(path);
2548 path = NULL;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002549 goto cont;
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002550 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002551
2552 ee_block = le32_to_cpu(ex->ee_block);
2553
2554 /*
2555 * See if the last block is inside the extent, if so split
2556 * the extent at 'end' block so we can easily remove the
2557 * tail of the first part of the split extent in
2558 * ext4_ext_rm_leaf().
2559 */
2560 if (end >= ee_block &&
2561 end < ee_block + ext4_ext_get_actual_len(ex) - 1) {
2562 int split_flag = 0;
2563
2564 if (ext4_ext_is_uninitialized(ex))
2565 split_flag = EXT4_EXT_MARK_UNINIT1 |
2566 EXT4_EXT_MARK_UNINIT2;
2567
2568 /*
2569 * Split the extent in two so that 'end' is the last
2570 * block in the first new extent
2571 */
2572 err = ext4_split_extent_at(handle, inode, path,
2573 end + 1, split_flag,
2574 EXT4_GET_BLOCKS_PRE_IO |
2575 EXT4_GET_BLOCKS_PUNCH_OUT_EXT);
2576
2577 if (err < 0)
2578 goto out;
2579 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002580 }
2581cont:
2582
2583 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002584 * We start scanning from right side, freeing all the blocks
2585 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002586 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002587 depth = ext_depth(inode);
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002588 if (path) {
2589 int k = i = depth;
2590 while (--k > 0)
2591 path[k].p_block =
2592 le16_to_cpu(path[k].p_hdr->eh_entries)+1;
2593 } else {
2594 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1),
2595 GFP_NOFS);
2596 if (path == NULL) {
2597 ext4_journal_stop(handle);
2598 return -ENOMEM;
2599 }
2600 path[0].p_depth = depth;
2601 path[0].p_hdr = ext_inode_hdr(inode);
Theodore Ts'od8b868f2012-08-17 08:54:52 -04002602 i = 0;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002603
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002604 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
2605 err = -EIO;
2606 goto out;
2607 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002608 }
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002609 err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002610
2611 while (i >= 0 && err == 0) {
2612 if (i == depth) {
2613 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04002614 err = ext4_ext_rm_leaf(handle, inode, path,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002615 &partial_cluster, start,
Lukas Czerner5f95d212012-03-19 23:03:19 -04002616 end);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002617 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002618 brelse(path[i].p_bh);
2619 path[i].p_bh = NULL;
2620 i--;
2621 continue;
2622 }
2623
2624 /* this is index block */
2625 if (!path[i].p_hdr) {
2626 ext_debug("initialize header\n");
2627 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002628 }
2629
Alex Tomasa86c6182006-10-11 01:21:03 -07002630 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002631 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002632 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2633 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2634 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2635 path[i].p_hdr,
2636 le16_to_cpu(path[i].p_hdr->eh_entries));
2637 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002638 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002639 path[i].p_idx--;
2640 }
2641
2642 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2643 i, EXT_FIRST_INDEX(path[i].p_hdr),
2644 path[i].p_idx);
2645 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002646 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002647 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002648 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04002649 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002650 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'obf89d162010-10-27 21:30:14 -04002651 bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002652 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002653 /* should we reset i_size? */
2654 err = -EIO;
2655 break;
2656 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002657 if (WARN_ON(i + 1 > depth)) {
2658 err = -EIO;
2659 break;
2660 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002661 if (ext4_ext_check(inode, ext_block_hdr(bh),
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002662 depth - i - 1)) {
2663 err = -EIO;
2664 break;
2665 }
2666 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002667
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002668 /* save actual number of indexes since this
2669 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002670 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2671 i++;
2672 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002673 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002674 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002675 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002676 * handle must be already prepared by the
2677 * truncatei_leaf() */
2678 err = ext4_ext_rm_idx(handle, inode, path + i);
2679 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002680 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002681 brelse(path[i].p_bh);
2682 path[i].p_bh = NULL;
2683 i--;
2684 ext_debug("return to level %d\n", i);
2685 }
2686 }
2687
Aditya Kalid8990242011-09-09 19:18:51 -04002688 trace_ext4_ext_remove_space_done(inode, start, depth, partial_cluster,
2689 path->p_hdr->eh_entries);
2690
Aditya Kali7b415bf2011-09-09 19:04:51 -04002691 /* If we still have something in the partial cluster and we have removed
2692 * even the first extent, then we should free the blocks in the partial
2693 * cluster as well. */
2694 if (partial_cluster && path->p_hdr->eh_entries == 0) {
2695 int flags = EXT4_FREE_BLOCKS_FORGET;
2696
2697 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2698 flags |= EXT4_FREE_BLOCKS_METADATA;
2699
2700 ext4_free_blocks(handle, inode, NULL,
2701 EXT4_C2B(EXT4_SB(sb), partial_cluster),
2702 EXT4_SB(sb)->s_cluster_ratio, flags);
2703 partial_cluster = 0;
2704 }
2705
Alex Tomasa86c6182006-10-11 01:21:03 -07002706 /* TODO: flexible tree reduction should be here */
2707 if (path->p_hdr->eh_entries == 0) {
2708 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002709 * truncate to zero freed all the tree,
2710 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002711 */
2712 err = ext4_ext_get_access(handle, inode, path);
2713 if (err == 0) {
2714 ext_inode_hdr(inode)->eh_depth = 0;
2715 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04002716 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07002717 err = ext4_ext_dirty(handle, inode, path);
2718 }
2719 }
2720out:
Alex Tomasa86c6182006-10-11 01:21:03 -07002721 ext4_ext_drop_refs(path);
2722 kfree(path);
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002723 if (err == -EAGAIN) {
2724 path = NULL;
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002725 goto again;
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002726 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002727 ext4_journal_stop(handle);
2728
2729 return err;
2730}
2731
2732/*
2733 * called at mount time
2734 */
2735void ext4_ext_init(struct super_block *sb)
2736{
2737 /*
2738 * possible initialization would be here
2739 */
2740
Theodore Ts'o83982b62009-01-06 14:53:16 -05002741 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04002742#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o92b97812012-03-19 23:41:49 -04002743 printk(KERN_INFO "EXT4-fs: file extents enabled"
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002744#ifdef AGGRESSIVE_TEST
Theodore Ts'o92b97812012-03-19 23:41:49 -04002745 ", aggressive tests"
Alex Tomasa86c6182006-10-11 01:21:03 -07002746#endif
2747#ifdef CHECK_BINSEARCH
Theodore Ts'o92b97812012-03-19 23:41:49 -04002748 ", check binsearch"
Alex Tomasa86c6182006-10-11 01:21:03 -07002749#endif
2750#ifdef EXTENTS_STATS
Theodore Ts'o92b97812012-03-19 23:41:49 -04002751 ", stats"
Alex Tomasa86c6182006-10-11 01:21:03 -07002752#endif
Theodore Ts'o92b97812012-03-19 23:41:49 -04002753 "\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04002754#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07002755#ifdef EXTENTS_STATS
2756 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2757 EXT4_SB(sb)->s_ext_min = 1 << 30;
2758 EXT4_SB(sb)->s_ext_max = 0;
2759#endif
2760 }
2761}
2762
2763/*
2764 * called at umount time
2765 */
2766void ext4_ext_release(struct super_block *sb)
2767{
Theodore Ts'o83982b62009-01-06 14:53:16 -05002768 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
Alex Tomasa86c6182006-10-11 01:21:03 -07002769 return;
2770
2771#ifdef EXTENTS_STATS
2772 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2773 struct ext4_sb_info *sbi = EXT4_SB(sb);
2774 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2775 sbi->s_ext_blocks, sbi->s_ext_extents,
2776 sbi->s_ext_blocks / sbi->s_ext_extents);
2777 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2778 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2779 }
2780#endif
2781}
2782
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002783/* FIXME!! we need to try to merge to left or right after zero-out */
2784static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2785{
Lukas Czerner24075182010-10-27 21:30:06 -04002786 ext4_fsblk_t ee_pblock;
2787 unsigned int ee_len;
Jing Zhangb7203032010-05-12 00:00:00 -04002788 int ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002789
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002790 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04002791 ee_pblock = ext4_ext_pblock(ex);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002792
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04002793 ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
Lukas Czerner24075182010-10-27 21:30:06 -04002794 if (ret > 0)
2795 ret = 0;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002796
Lukas Czerner24075182010-10-27 21:30:06 -04002797 return ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002798}
2799
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002800/*
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002801 * ext4_split_extent_at() splits an extent at given block.
2802 *
2803 * @handle: the journal handle
2804 * @inode: the file inode
2805 * @path: the path to the extent
2806 * @split: the logical block where the extent is splitted.
2807 * @split_flags: indicates if the extent could be zeroout if split fails, and
2808 * the states(init or uninit) of new extents.
2809 * @flags: flags used to insert new extent to extent tree.
2810 *
2811 *
2812 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
2813 * of which are deterimined by split_flag.
2814 *
2815 * There are two cases:
2816 * a> the extent are splitted into two extent.
2817 * b> split is not needed, and just mark the extent.
2818 *
2819 * return 0 on success.
2820 */
2821static int ext4_split_extent_at(handle_t *handle,
2822 struct inode *inode,
2823 struct ext4_ext_path *path,
2824 ext4_lblk_t split,
2825 int split_flag,
2826 int flags)
2827{
2828 ext4_fsblk_t newblock;
2829 ext4_lblk_t ee_block;
2830 struct ext4_extent *ex, newex, orig_ex;
2831 struct ext4_extent *ex2 = NULL;
2832 unsigned int ee_len, depth;
2833 int err = 0;
2834
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04002835 BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) ==
2836 (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2));
2837
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002838 ext_debug("ext4_split_extents_at: inode %lu, logical"
2839 "block %llu\n", inode->i_ino, (unsigned long long)split);
2840
2841 ext4_ext_show_leaf(inode, path);
2842
2843 depth = ext_depth(inode);
2844 ex = path[depth].p_ext;
2845 ee_block = le32_to_cpu(ex->ee_block);
2846 ee_len = ext4_ext_get_actual_len(ex);
2847 newblock = split - ee_block + ext4_ext_pblock(ex);
2848
2849 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
2850
2851 err = ext4_ext_get_access(handle, inode, path + depth);
2852 if (err)
2853 goto out;
2854
2855 if (split == ee_block) {
2856 /*
2857 * case b: block @split is the block that the extent begins with
2858 * then we just change the state of the extent, and splitting
2859 * is not needed.
2860 */
2861 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2862 ext4_ext_mark_uninitialized(ex);
2863 else
2864 ext4_ext_mark_initialized(ex);
2865
2866 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
2867 ext4_ext_try_to_merge(inode, path, ex);
2868
2869 err = ext4_ext_dirty(handle, inode, path + depth);
2870 goto out;
2871 }
2872
2873 /* case a */
2874 memcpy(&orig_ex, ex, sizeof(orig_ex));
2875 ex->ee_len = cpu_to_le16(split - ee_block);
2876 if (split_flag & EXT4_EXT_MARK_UNINIT1)
2877 ext4_ext_mark_uninitialized(ex);
2878
2879 /*
2880 * path may lead to new leaf, not to original leaf any more
2881 * after ext4_ext_insert_extent() returns,
2882 */
2883 err = ext4_ext_dirty(handle, inode, path + depth);
2884 if (err)
2885 goto fix_extent_len;
2886
2887 ex2 = &newex;
2888 ex2->ee_block = cpu_to_le32(split);
2889 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
2890 ext4_ext_store_pblock(ex2, newblock);
2891 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2892 ext4_ext_mark_uninitialized(ex2);
2893
2894 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
2895 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04002896 if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) {
2897 if (split_flag & EXT4_EXT_DATA_VALID1)
2898 err = ext4_ext_zeroout(inode, ex2);
2899 else
2900 err = ext4_ext_zeroout(inode, ex);
2901 } else
2902 err = ext4_ext_zeroout(inode, &orig_ex);
2903
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002904 if (err)
2905 goto fix_extent_len;
2906 /* update the extent length and mark as initialized */
Al Viroaf1584f2012-04-12 20:32:25 -04002907 ex->ee_len = cpu_to_le16(ee_len);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002908 ext4_ext_try_to_merge(inode, path, ex);
2909 err = ext4_ext_dirty(handle, inode, path + depth);
2910 goto out;
2911 } else if (err)
2912 goto fix_extent_len;
2913
2914out:
2915 ext4_ext_show_leaf(inode, path);
2916 return err;
2917
2918fix_extent_len:
2919 ex->ee_len = orig_ex.ee_len;
2920 ext4_ext_dirty(handle, inode, path + depth);
2921 return err;
2922}
2923
2924/*
2925 * ext4_split_extents() splits an extent and mark extent which is covered
2926 * by @map as split_flags indicates
2927 *
2928 * It may result in splitting the extent into multiple extents (upto three)
2929 * There are three possibilities:
2930 * a> There is no split required
2931 * b> Splits in two extents: Split is happening at either end of the extent
2932 * c> Splits in three extents: Somone is splitting in middle of the extent
2933 *
2934 */
2935static int ext4_split_extent(handle_t *handle,
2936 struct inode *inode,
2937 struct ext4_ext_path *path,
2938 struct ext4_map_blocks *map,
2939 int split_flag,
2940 int flags)
2941{
2942 ext4_lblk_t ee_block;
2943 struct ext4_extent *ex;
2944 unsigned int ee_len, depth;
2945 int err = 0;
2946 int uninitialized;
2947 int split_flag1, flags1;
2948
2949 depth = ext_depth(inode);
2950 ex = path[depth].p_ext;
2951 ee_block = le32_to_cpu(ex->ee_block);
2952 ee_len = ext4_ext_get_actual_len(ex);
2953 uninitialized = ext4_ext_is_uninitialized(ex);
2954
2955 if (map->m_lblk + map->m_len < ee_block + ee_len) {
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04002956 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002957 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
2958 if (uninitialized)
2959 split_flag1 |= EXT4_EXT_MARK_UNINIT1 |
2960 EXT4_EXT_MARK_UNINIT2;
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04002961 if (split_flag & EXT4_EXT_DATA_VALID2)
2962 split_flag1 |= EXT4_EXT_DATA_VALID1;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002963 err = ext4_split_extent_at(handle, inode, path,
2964 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04002965 if (err)
2966 goto out;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002967 }
2968
2969 ext4_ext_drop_refs(path);
2970 path = ext4_ext_find_extent(inode, map->m_lblk, path);
2971 if (IS_ERR(path))
2972 return PTR_ERR(path);
2973
2974 if (map->m_lblk >= ee_block) {
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04002975 split_flag1 = split_flag & (EXT4_EXT_MAY_ZEROOUT |
2976 EXT4_EXT_DATA_VALID2);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002977 if (uninitialized)
2978 split_flag1 |= EXT4_EXT_MARK_UNINIT1;
2979 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2980 split_flag1 |= EXT4_EXT_MARK_UNINIT2;
2981 err = ext4_split_extent_at(handle, inode, path,
2982 map->m_lblk, split_flag1, flags);
2983 if (err)
2984 goto out;
2985 }
2986
2987 ext4_ext_show_leaf(inode, path);
2988out:
2989 return err ? err : map->m_len;
2990}
2991
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04002992#define EXT4_EXT_ZERO_LEN 7
Amit Arora56055d32007-07-17 21:42:38 -04002993/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04002994 * This function is called by ext4_ext_map_blocks() if someone tries to write
Amit Arora56055d32007-07-17 21:42:38 -04002995 * to an uninitialized extent. It may result in splitting the uninitialized
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002996 * extent into multiple extents (up to three - one initialized and two
Amit Arora56055d32007-07-17 21:42:38 -04002997 * uninitialized).
2998 * There are three possibilities:
2999 * a> There is no split required: Entire extent should be initialized
3000 * b> Splits in two extents: Write is happening at either end of the extent
3001 * c> Splits in three extents: Somone is writing in middle of the extent
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003002 *
3003 * Pre-conditions:
3004 * - The extent pointed to by 'path' is uninitialized.
3005 * - The extent pointed to by 'path' contains a superset
3006 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
3007 *
3008 * Post-conditions on success:
3009 * - the returned value is the number of blocks beyond map->l_lblk
3010 * that are allocated and initialized.
3011 * It is guaranteed to be >= map->m_len.
Amit Arora56055d32007-07-17 21:42:38 -04003012 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003013static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003014 struct inode *inode,
3015 struct ext4_map_blocks *map,
3016 struct ext4_ext_path *path)
Amit Arora56055d32007-07-17 21:42:38 -04003017{
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003018 struct ext4_extent_header *eh;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003019 struct ext4_map_blocks split_map;
3020 struct ext4_extent zero_ex;
3021 struct ext4_extent *ex;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003022 ext4_lblk_t ee_block, eof_block;
Dan Carpenterf85b2872011-10-26 03:42:36 -04003023 unsigned int ee_len, depth;
3024 int allocated;
Amit Arora56055d32007-07-17 21:42:38 -04003025 int err = 0;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003026 int split_flag = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003027
3028 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3029 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003030 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003031
3032 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3033 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003034 if (eof_block < map->m_lblk + map->m_len)
3035 eof_block = map->m_lblk + map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003036
3037 depth = ext_depth(inode);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003038 eh = path[depth].p_hdr;
Amit Arora56055d32007-07-17 21:42:38 -04003039 ex = path[depth].p_ext;
3040 ee_block = le32_to_cpu(ex->ee_block);
3041 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003042 allocated = ee_len - (map->m_lblk - ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003043
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003044 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
3045
3046 /* Pre-conditions */
3047 BUG_ON(!ext4_ext_is_uninitialized(ex));
3048 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003049
3050 /*
3051 * Attempt to transfer newly initialized blocks from the currently
3052 * uninitialized extent to its left neighbor. This is much cheaper
3053 * than an insertion followed by a merge as those involve costly
3054 * memmove() calls. This is the common case in steady state for
3055 * workloads doing fallocate(FALLOC_FL_KEEP_SIZE) followed by append
3056 * writes.
3057 *
3058 * Limitations of the current logic:
3059 * - L1: we only deal with writes at the start of the extent.
3060 * The approach could be extended to writes at the end
3061 * of the extent but this scenario was deemed less common.
3062 * - L2: we do not deal with writes covering the whole extent.
3063 * This would require removing the extent if the transfer
3064 * is possible.
3065 * - L3: we only attempt to merge with an extent stored in the
3066 * same extent tree node.
3067 */
3068 if ((map->m_lblk == ee_block) && /*L1*/
3069 (map->m_len < ee_len) && /*L2*/
3070 (ex > EXT_FIRST_EXTENT(eh))) { /*L3*/
3071 struct ext4_extent *prev_ex;
3072 ext4_lblk_t prev_lblk;
3073 ext4_fsblk_t prev_pblk, ee_pblk;
3074 unsigned int prev_len, write_len;
3075
3076 prev_ex = ex - 1;
3077 prev_lblk = le32_to_cpu(prev_ex->ee_block);
3078 prev_len = ext4_ext_get_actual_len(prev_ex);
3079 prev_pblk = ext4_ext_pblock(prev_ex);
3080 ee_pblk = ext4_ext_pblock(ex);
3081 write_len = map->m_len;
3082
3083 /*
3084 * A transfer of blocks from 'ex' to 'prev_ex' is allowed
3085 * upon those conditions:
3086 * - C1: prev_ex is initialized,
3087 * - C2: prev_ex is logically abutting ex,
3088 * - C3: prev_ex is physically abutting ex,
3089 * - C4: prev_ex can receive the additional blocks without
3090 * overflowing the (initialized) length limit.
3091 */
3092 if ((!ext4_ext_is_uninitialized(prev_ex)) && /*C1*/
3093 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3094 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
3095 (prev_len < (EXT_INIT_MAX_LEN - write_len))) { /*C4*/
3096 err = ext4_ext_get_access(handle, inode, path + depth);
3097 if (err)
3098 goto out;
3099
3100 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3101 map, ex, prev_ex);
3102
3103 /* Shift the start of ex by 'write_len' blocks */
3104 ex->ee_block = cpu_to_le32(ee_block + write_len);
3105 ext4_ext_store_pblock(ex, ee_pblk + write_len);
3106 ex->ee_len = cpu_to_le16(ee_len - write_len);
3107 ext4_ext_mark_uninitialized(ex); /* Restore the flag */
3108
3109 /* Extend prev_ex by 'write_len' blocks */
3110 prev_ex->ee_len = cpu_to_le16(prev_len + write_len);
3111
3112 /* Mark the block containing both extents as dirty */
3113 ext4_ext_dirty(handle, inode, path + depth);
3114
3115 /* Update path to point to the right extent */
3116 path[depth].p_ext = prev_ex;
3117
3118 /* Result: number of initialized blocks past m_lblk */
3119 allocated = write_len;
3120 goto out;
3121 }
3122 }
3123
Yongqiang Yang667eff32011-05-03 12:25:07 -04003124 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003125 /*
3126 * It is safe to convert extent to initialized via explicit
3127 * zeroout only if extent is fully insde i_size or new_size.
3128 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003129 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003130
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003131 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003132 if (ee_len <= 2*EXT4_EXT_ZERO_LEN &&
3133 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3134 err = ext4_ext_zeroout(inode, ex);
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003135 if (err)
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003136 goto out;
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003137
3138 err = ext4_ext_get_access(handle, inode, path + depth);
3139 if (err)
3140 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003141 ext4_ext_mark_initialized(ex);
3142 ext4_ext_try_to_merge(inode, path, ex);
3143 err = ext4_ext_dirty(handle, inode, path + depth);
3144 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04003145 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003146
Amit Arora56055d32007-07-17 21:42:38 -04003147 /*
Yongqiang Yang667eff32011-05-03 12:25:07 -04003148 * four cases:
3149 * 1. split the extent into three extents.
3150 * 2. split the extent into two extents, zeroout the first half.
3151 * 3. split the extent into two extents, zeroout the second half.
3152 * 4. split the extent into two extents with out zeroout.
Amit Arora56055d32007-07-17 21:42:38 -04003153 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003154 split_map.m_lblk = map->m_lblk;
3155 split_map.m_len = map->m_len;
3156
3157 if (allocated > map->m_len) {
3158 if (allocated <= EXT4_EXT_ZERO_LEN &&
3159 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3160 /* case 3 */
3161 zero_ex.ee_block =
Allison Henderson9b940f82011-05-16 10:11:09 -04003162 cpu_to_le32(map->m_lblk);
3163 zero_ex.ee_len = cpu_to_le16(allocated);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003164 ext4_ext_store_pblock(&zero_ex,
3165 ext4_ext_pblock(ex) + map->m_lblk - ee_block);
3166 err = ext4_ext_zeroout(inode, &zero_ex);
Amit Arora56055d32007-07-17 21:42:38 -04003167 if (err)
3168 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003169 split_map.m_lblk = map->m_lblk;
3170 split_map.m_len = allocated;
3171 } else if ((map->m_lblk - ee_block + map->m_len <
3172 EXT4_EXT_ZERO_LEN) &&
3173 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3174 /* case 2 */
3175 if (map->m_lblk != ee_block) {
3176 zero_ex.ee_block = ex->ee_block;
3177 zero_ex.ee_len = cpu_to_le16(map->m_lblk -
3178 ee_block);
3179 ext4_ext_store_pblock(&zero_ex,
3180 ext4_ext_pblock(ex));
3181 err = ext4_ext_zeroout(inode, &zero_ex);
3182 if (err)
3183 goto out;
3184 }
3185
Yongqiang Yang667eff32011-05-03 12:25:07 -04003186 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003187 split_map.m_len = map->m_lblk - ee_block + map->m_len;
3188 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003189 }
3190 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003191
3192 allocated = ext4_split_extent(handle, inode, path,
3193 &split_map, split_flag, 0);
3194 if (allocated < 0)
3195 err = allocated;
3196
Amit Arora56055d32007-07-17 21:42:38 -04003197out:
3198 return err ? err : allocated;
3199}
3200
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003201/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003202 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003203 * ext4_get_blocks_dio_write() when DIO to write
3204 * to an uninitialized extent.
3205 *
Paul Bollefd018fe2011-02-15 00:05:43 +01003206 * Writing to an uninitialized extent may result in splitting the uninitialized
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003207 * extent into multiple /initialized uninitialized extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003208 * There are three possibilities:
3209 * a> There is no split required: Entire extent should be uninitialized
3210 * b> Splits in two extents: Write is happening at either end of the extent
3211 * c> Splits in three extents: Somone is writing in middle of the extent
3212 *
3213 * One of more index blocks maybe needed if the extent tree grow after
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003214 * the uninitialized extent split. To prevent ENOSPC occur at the IO
Mingming Cao00314622009-09-28 15:49:08 -04003215 * complete, we need to split the uninitialized extent before DIO submit
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02003216 * the IO. The uninitialized extent called at this time will be split
Mingming Cao00314622009-09-28 15:49:08 -04003217 * into three uninitialized extent(at most). After IO complete, the part
3218 * being filled will be convert to initialized by the end_io callback function
3219 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003220 *
3221 * Returns the size of uninitialized extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003222 */
3223static int ext4_split_unwritten_extents(handle_t *handle,
3224 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003225 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003226 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04003227 int flags)
3228{
Yongqiang Yang667eff32011-05-03 12:25:07 -04003229 ext4_lblk_t eof_block;
3230 ext4_lblk_t ee_block;
3231 struct ext4_extent *ex;
3232 unsigned int ee_len;
3233 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003234
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003235 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
3236 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003237 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003238
3239 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3240 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003241 if (eof_block < map->m_lblk + map->m_len)
3242 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003243 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003244 * It is safe to convert extent to initialized via explicit
3245 * zeroout only if extent is fully insde i_size or new_size.
3246 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003247 depth = ext_depth(inode);
3248 ex = path[depth].p_ext;
3249 ee_block = le32_to_cpu(ex->ee_block);
3250 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003251
Yongqiang Yang667eff32011-05-03 12:25:07 -04003252 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
3253 split_flag |= EXT4_EXT_MARK_UNINIT2;
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04003254 if (flags & EXT4_GET_BLOCKS_CONVERT)
3255 split_flag |= EXT4_EXT_DATA_VALID2;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003256 flags |= EXT4_GET_BLOCKS_PRE_IO;
3257 return ext4_split_extent(handle, inode, path, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003258}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003259
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003260static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04003261 struct inode *inode,
3262 struct ext4_map_blocks *map,
3263 struct ext4_ext_path *path)
Mingming Cao00314622009-09-28 15:49:08 -04003264{
3265 struct ext4_extent *ex;
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04003266 ext4_lblk_t ee_block;
3267 unsigned int ee_len;
Mingming Cao00314622009-09-28 15:49:08 -04003268 int depth;
3269 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003270
3271 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003272 ex = path[depth].p_ext;
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04003273 ee_block = le32_to_cpu(ex->ee_block);
3274 ee_len = ext4_ext_get_actual_len(ex);
Mingming Cao00314622009-09-28 15:49:08 -04003275
Yongqiang Yang197217a2011-05-03 11:45:29 -04003276 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3277 "block %llu, max_blocks %u\n", inode->i_ino,
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04003278 (unsigned long long)ee_block, ee_len);
3279
3280 /* If extent is larger than requested then split is required */
3281 if (ee_block != map->m_lblk || ee_len > map->m_len) {
3282 err = ext4_split_unwritten_extents(handle, inode, map, path,
3283 EXT4_GET_BLOCKS_CONVERT);
3284 if (err < 0)
3285 goto out;
3286 ext4_ext_drop_refs(path);
3287 path = ext4_ext_find_extent(inode, map->m_lblk, path);
3288 if (IS_ERR(path)) {
3289 err = PTR_ERR(path);
3290 goto out;
3291 }
3292 depth = ext_depth(inode);
3293 ex = path[depth].p_ext;
3294 }
Yongqiang Yang197217a2011-05-03 11:45:29 -04003295
Mingming Cao00314622009-09-28 15:49:08 -04003296 err = ext4_ext_get_access(handle, inode, path + depth);
3297 if (err)
3298 goto out;
3299 /* first mark the extent as initialized */
3300 ext4_ext_mark_initialized(ex);
3301
Yongqiang Yang197217a2011-05-03 11:45:29 -04003302 /* note: ext4_ext_correct_indexes() isn't needed here because
3303 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003304 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04003305 ext4_ext_try_to_merge(inode, path, ex);
3306
Mingming Cao00314622009-09-28 15:49:08 -04003307 /* Mark modified extent as dirty */
3308 err = ext4_ext_dirty(handle, inode, path + depth);
3309out:
3310 ext4_ext_show_leaf(inode, path);
3311 return err;
3312}
3313
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003314static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3315 sector_t block, int count)
3316{
3317 int i;
3318 for (i = 0; i < count; i++)
3319 unmap_underlying_metadata(bdev, block + i);
3320}
3321
Theodore Ts'o58590b02010-10-27 21:23:12 -04003322/*
3323 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3324 */
3325static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
Eric Sandeend002ebf2011-01-10 13:03:35 -05003326 ext4_lblk_t lblk,
Theodore Ts'o58590b02010-10-27 21:23:12 -04003327 struct ext4_ext_path *path,
3328 unsigned int len)
3329{
3330 int i, depth;
3331 struct ext4_extent_header *eh;
Sergey Senozhatsky65922cb2011-03-23 14:08:27 -04003332 struct ext4_extent *last_ex;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003333
3334 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3335 return 0;
3336
3337 depth = ext_depth(inode);
3338 eh = path[depth].p_hdr;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003339
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003340 /*
3341 * We're going to remove EOFBLOCKS_FL entirely in future so we
3342 * do not care for this case anymore. Simply remove the flag
3343 * if there are no extents.
3344 */
3345 if (unlikely(!eh->eh_entries))
3346 goto out;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003347 last_ex = EXT_LAST_EXTENT(eh);
3348 /*
3349 * We should clear the EOFBLOCKS_FL flag if we are writing the
3350 * last block in the last extent in the file. We test this by
3351 * first checking to see if the caller to
3352 * ext4_ext_get_blocks() was interested in the last block (or
3353 * a block beyond the last block) in the current extent. If
3354 * this turns out to be false, we can bail out from this
3355 * function immediately.
3356 */
Eric Sandeend002ebf2011-01-10 13:03:35 -05003357 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
Theodore Ts'o58590b02010-10-27 21:23:12 -04003358 ext4_ext_get_actual_len(last_ex))
3359 return 0;
3360 /*
3361 * If the caller does appear to be planning to write at or
3362 * beyond the end of the current extent, we then test to see
3363 * if the current extent is the last extent in the file, by
3364 * checking to make sure it was reached via the rightmost node
3365 * at each level of the tree.
3366 */
3367 for (i = depth-1; i >= 0; i--)
3368 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3369 return 0;
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003370out:
Theodore Ts'o58590b02010-10-27 21:23:12 -04003371 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3372 return ext4_mark_inode_dirty(handle, inode);
3373}
3374
Aditya Kali7b415bf2011-09-09 19:04:51 -04003375/**
3376 * ext4_find_delalloc_range: find delayed allocated block in the given range.
3377 *
3378 * Goes through the buffer heads in the range [lblk_start, lblk_end] and returns
3379 * whether there are any buffers marked for delayed allocation. It returns '1'
3380 * on the first delalloc'ed buffer head found. If no buffer head in the given
3381 * range is marked for delalloc, it returns 0.
3382 * lblk_start should always be <= lblk_end.
3383 * search_hint_reverse is to indicate that searching in reverse from lblk_end to
3384 * lblk_start might be more efficient (i.e., we will likely hit the delalloc'ed
3385 * block sooner). This is useful when blocks are truncated sequentially from
3386 * lblk_start towards lblk_end.
3387 */
3388static int ext4_find_delalloc_range(struct inode *inode,
3389 ext4_lblk_t lblk_start,
3390 ext4_lblk_t lblk_end,
3391 int search_hint_reverse)
3392{
3393 struct address_space *mapping = inode->i_mapping;
3394 struct buffer_head *head, *bh = NULL;
3395 struct page *page;
3396 ext4_lblk_t i, pg_lblk;
3397 pgoff_t index;
3398
Robin Dong8c48f7e2011-12-18 23:05:43 -05003399 if (!test_opt(inode->i_sb, DELALLOC))
3400 return 0;
3401
Aditya Kali7b415bf2011-09-09 19:04:51 -04003402 /* reverse search wont work if fs block size is less than page size */
3403 if (inode->i_blkbits < PAGE_CACHE_SHIFT)
3404 search_hint_reverse = 0;
3405
3406 if (search_hint_reverse)
3407 i = lblk_end;
3408 else
3409 i = lblk_start;
3410
3411 index = i >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
3412
3413 while ((i >= lblk_start) && (i <= lblk_end)) {
3414 page = find_get_page(mapping, index);
Aditya Kali5356f262011-09-09 19:20:51 -04003415 if (!page)
Aditya Kali7b415bf2011-09-09 19:04:51 -04003416 goto nextpage;
3417
Aditya Kali7b415bf2011-09-09 19:04:51 -04003418 if (!page_has_buffers(page))
3419 goto nextpage;
3420
3421 head = page_buffers(page);
3422 if (!head)
3423 goto nextpage;
3424
3425 bh = head;
3426 pg_lblk = index << (PAGE_CACHE_SHIFT -
3427 inode->i_blkbits);
3428 do {
3429 if (unlikely(pg_lblk < lblk_start)) {
3430 /*
3431 * This is possible when fs block size is less
3432 * than page size and our cluster starts/ends in
3433 * middle of the page. So we need to skip the
3434 * initial few blocks till we reach the 'lblk'
3435 */
3436 pg_lblk++;
3437 continue;
3438 }
3439
Aditya Kali5356f262011-09-09 19:20:51 -04003440 /* Check if the buffer is delayed allocated and that it
3441 * is not yet mapped. (when da-buffers are mapped during
3442 * their writeout, their da_mapped bit is set.)
3443 */
3444 if (buffer_delay(bh) && !buffer_da_mapped(bh)) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04003445 page_cache_release(page);
Aditya Kalid8990242011-09-09 19:18:51 -04003446 trace_ext4_find_delalloc_range(inode,
3447 lblk_start, lblk_end,
3448 search_hint_reverse,
3449 1, i);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003450 return 1;
3451 }
3452 if (search_hint_reverse)
3453 i--;
3454 else
3455 i++;
3456 } while ((i >= lblk_start) && (i <= lblk_end) &&
3457 ((bh = bh->b_this_page) != head));
3458nextpage:
3459 if (page)
3460 page_cache_release(page);
3461 /*
3462 * Move to next page. 'i' will be the first lblk in the next
3463 * page.
3464 */
3465 if (search_hint_reverse)
3466 index--;
3467 else
3468 index++;
3469 i = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
3470 }
3471
Aditya Kalid8990242011-09-09 19:18:51 -04003472 trace_ext4_find_delalloc_range(inode, lblk_start, lblk_end,
3473 search_hint_reverse, 0, 0);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003474 return 0;
3475}
3476
3477int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk,
3478 int search_hint_reverse)
3479{
3480 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3481 ext4_lblk_t lblk_start, lblk_end;
3482 lblk_start = lblk & (~(sbi->s_cluster_ratio - 1));
3483 lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
3484
3485 return ext4_find_delalloc_range(inode, lblk_start, lblk_end,
3486 search_hint_reverse);
3487}
3488
3489/**
3490 * Determines how many complete clusters (out of those specified by the 'map')
3491 * are under delalloc and were reserved quota for.
3492 * This function is called when we are writing out the blocks that were
3493 * originally written with their allocation delayed, but then the space was
3494 * allocated using fallocate() before the delayed allocation could be resolved.
3495 * The cases to look for are:
3496 * ('=' indicated delayed allocated blocks
3497 * '-' indicates non-delayed allocated blocks)
3498 * (a) partial clusters towards beginning and/or end outside of allocated range
3499 * are not delalloc'ed.
3500 * Ex:
3501 * |----c---=|====c====|====c====|===-c----|
3502 * |++++++ allocated ++++++|
3503 * ==> 4 complete clusters in above example
3504 *
3505 * (b) partial cluster (outside of allocated range) towards either end is
3506 * marked for delayed allocation. In this case, we will exclude that
3507 * cluster.
3508 * Ex:
3509 * |----====c========|========c========|
3510 * |++++++ allocated ++++++|
3511 * ==> 1 complete clusters in above example
3512 *
3513 * Ex:
3514 * |================c================|
3515 * |++++++ allocated ++++++|
3516 * ==> 0 complete clusters in above example
3517 *
3518 * The ext4_da_update_reserve_space will be called only if we
3519 * determine here that there were some "entire" clusters that span
3520 * this 'allocated' range.
3521 * In the non-bigalloc case, this function will just end up returning num_blks
3522 * without ever calling ext4_find_delalloc_range.
3523 */
3524static unsigned int
3525get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start,
3526 unsigned int num_blks)
3527{
3528 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3529 ext4_lblk_t alloc_cluster_start, alloc_cluster_end;
3530 ext4_lblk_t lblk_from, lblk_to, c_offset;
3531 unsigned int allocated_clusters = 0;
3532
3533 alloc_cluster_start = EXT4_B2C(sbi, lblk_start);
3534 alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1);
3535
3536 /* max possible clusters for this allocation */
3537 allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1;
3538
Aditya Kalid8990242011-09-09 19:18:51 -04003539 trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks);
3540
Aditya Kali7b415bf2011-09-09 19:04:51 -04003541 /* Check towards left side */
3542 c_offset = lblk_start & (sbi->s_cluster_ratio - 1);
3543 if (c_offset) {
3544 lblk_from = lblk_start & (~(sbi->s_cluster_ratio - 1));
3545 lblk_to = lblk_from + c_offset - 1;
3546
3547 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
3548 allocated_clusters--;
3549 }
3550
3551 /* Now check towards right. */
3552 c_offset = (lblk_start + num_blks) & (sbi->s_cluster_ratio - 1);
3553 if (allocated_clusters && c_offset) {
3554 lblk_from = lblk_start + num_blks;
3555 lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1;
3556
3557 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
3558 allocated_clusters--;
3559 }
3560
3561 return allocated_clusters;
3562}
3563
Mingming Cao00314622009-09-28 15:49:08 -04003564static int
3565ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003566 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003567 struct ext4_ext_path *path, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003568 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003569{
3570 int ret = 0;
3571 int err = 0;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003572 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Mingming Cao00314622009-09-28 15:49:08 -04003573
Zheng Liu88635ca2011-12-28 19:00:25 -05003574 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical "
3575 "block %llu, max_blocks %u, flags %x, allocated %u\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003576 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04003577 flags, allocated);
3578 ext4_ext_show_leaf(inode, path);
3579
Aditya Kalid8990242011-09-09 19:18:51 -04003580 trace_ext4_ext_handle_uninitialized_extents(inode, map, allocated,
3581 newblock);
3582
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003583 /* get_block() before submit the IO, split the extent */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003584 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003585 ret = ext4_split_unwritten_extents(handle, inode, map,
3586 path, flags);
Mingming5f524952009-11-10 10:48:04 -05003587 /*
3588 * Flag the inode(non aio case) or end_io struct (aio case)
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003589 * that this IO needs to conversion to written when IO is
Mingming5f524952009-11-10 10:48:04 -05003590 * completed
3591 */
Tao Ma0edeb712011-10-31 17:30:44 -04003592 if (io)
3593 ext4_set_io_unwritten_flag(inode, io);
3594 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003595 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
Jiaying Zhang744692d2010-03-04 16:14:02 -05003596 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003597 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao00314622009-09-28 15:49:08 -04003598 goto out;
3599 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003600 /* IO end_io complete, convert the filled extent to written */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003601 if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04003602 ret = ext4_convert_unwritten_extents_endio(handle, inode, map,
Mingming Cao00314622009-09-28 15:49:08 -04003603 path);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003604 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05003605 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05003606 err = check_eofblocks_fl(handle, inode, map->m_lblk,
3607 path, map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003608 } else
3609 err = ret;
Mingming Cao00314622009-09-28 15:49:08 -04003610 goto out2;
3611 }
3612 /* buffered IO case */
3613 /*
3614 * repeat fallocate creation request
3615 * we already have an unwritten extent
3616 */
3617 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3618 goto map_out;
3619
3620 /* buffered READ or buffered write_begin() lookup */
3621 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3622 /*
3623 * We have blocks reserved already. We
3624 * return allocated blocks so that delalloc
3625 * won't do block reservation for us. But
3626 * the buffer head will be unmapped so that
3627 * a read from the block returns 0s.
3628 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003629 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003630 goto out1;
3631 }
3632
3633 /* buffered write, writepage time, convert*/
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003634 ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04003635 if (ret >= 0)
Jan Karab436b9b2009-12-08 23:51:10 -05003636 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04003637out:
3638 if (ret <= 0) {
3639 err = ret;
3640 goto out2;
3641 } else
3642 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003643 map->m_flags |= EXT4_MAP_NEW;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003644 /*
3645 * if we allocated more blocks than requested
3646 * we need to make sure we unmap the extra block
3647 * allocated. The actual needed block will get
3648 * unmapped later when we find the buffer_head marked
3649 * new.
3650 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003651 if (allocated > map->m_len) {
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003652 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003653 newblock + map->m_len,
3654 allocated - map->m_len);
3655 allocated = map->m_len;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003656 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003657
3658 /*
3659 * If we have done fallocate with the offset that is already
3660 * delayed allocated, we would have block reservation
3661 * and quota reservation done in the delayed write path.
3662 * But fallocate would have already updated quota and block
3663 * count for this offset. So cancel these reservation
3664 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003665 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
3666 unsigned int reserved_clusters;
3667 reserved_clusters = get_reserved_cluster_alloc(inode,
3668 map->m_lblk, map->m_len);
3669 if (reserved_clusters)
3670 ext4_da_update_reserve_space(inode,
3671 reserved_clusters,
3672 0);
3673 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003674
Mingming Cao00314622009-09-28 15:49:08 -04003675map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003676 map->m_flags |= EXT4_MAP_MAPPED;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04003677 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
3678 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
3679 map->m_len);
3680 if (err < 0)
3681 goto out2;
3682 }
Mingming Cao00314622009-09-28 15:49:08 -04003683out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003684 if (allocated > map->m_len)
3685 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003686 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003687 map->m_pblk = newblock;
3688 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04003689out2:
3690 if (path) {
3691 ext4_ext_drop_refs(path);
3692 kfree(path);
3693 }
3694 return err ? err : allocated;
3695}
Theodore Ts'o58590b02010-10-27 21:23:12 -04003696
Mingming Cao00314622009-09-28 15:49:08 -04003697/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003698 * get_implied_cluster_alloc - check to see if the requested
3699 * allocation (in the map structure) overlaps with a cluster already
3700 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04003701 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003702 * @map The requested lblk->pblk mapping
3703 * @ex The extent structure which might contain an implied
3704 * cluster allocation
3705 *
3706 * This function is called by ext4_ext_map_blocks() after we failed to
3707 * find blocks that were already in the inode's extent tree. Hence,
3708 * we know that the beginning of the requested region cannot overlap
3709 * the extent from the inode's extent tree. There are three cases we
3710 * want to catch. The first is this case:
3711 *
3712 * |--- cluster # N--|
3713 * |--- extent ---| |---- requested region ---|
3714 * |==========|
3715 *
3716 * The second case that we need to test for is this one:
3717 *
3718 * |--------- cluster # N ----------------|
3719 * |--- requested region --| |------- extent ----|
3720 * |=======================|
3721 *
3722 * The third case is when the requested region lies between two extents
3723 * within the same cluster:
3724 * |------------- cluster # N-------------|
3725 * |----- ex -----| |---- ex_right ----|
3726 * |------ requested region ------|
3727 * |================|
3728 *
3729 * In each of the above cases, we need to set the map->m_pblk and
3730 * map->m_len so it corresponds to the return the extent labelled as
3731 * "|====|" from cluster #N, since it is already in use for data in
3732 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
3733 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
3734 * as a new "allocated" block region. Otherwise, we will return 0 and
3735 * ext4_ext_map_blocks() will then allocate one or more new clusters
3736 * by calling ext4_mb_new_blocks().
3737 */
Aditya Kalid8990242011-09-09 19:18:51 -04003738static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003739 struct ext4_map_blocks *map,
3740 struct ext4_extent *ex,
3741 struct ext4_ext_path *path)
3742{
Aditya Kalid8990242011-09-09 19:18:51 -04003743 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003744 ext4_lblk_t c_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
3745 ext4_lblk_t ex_cluster_start, ex_cluster_end;
Curt Wohlgemuth14d7f3e2011-12-18 17:39:02 -05003746 ext4_lblk_t rr_cluster_start;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003747 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
3748 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
3749 unsigned short ee_len = ext4_ext_get_actual_len(ex);
3750
3751 /* The extent passed in that we are trying to match */
3752 ex_cluster_start = EXT4_B2C(sbi, ee_block);
3753 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
3754
3755 /* The requested region passed into ext4_map_blocks() */
3756 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003757
3758 if ((rr_cluster_start == ex_cluster_end) ||
3759 (rr_cluster_start == ex_cluster_start)) {
3760 if (rr_cluster_start == ex_cluster_end)
3761 ee_start += ee_len - 1;
3762 map->m_pblk = (ee_start & ~(sbi->s_cluster_ratio - 1)) +
3763 c_offset;
3764 map->m_len = min(map->m_len,
3765 (unsigned) sbi->s_cluster_ratio - c_offset);
3766 /*
3767 * Check for and handle this case:
3768 *
3769 * |--------- cluster # N-------------|
3770 * |------- extent ----|
3771 * |--- requested region ---|
3772 * |===========|
3773 */
3774
3775 if (map->m_lblk < ee_block)
3776 map->m_len = min(map->m_len, ee_block - map->m_lblk);
3777
3778 /*
3779 * Check for the case where there is already another allocated
3780 * block to the right of 'ex' but before the end of the cluster.
3781 *
3782 * |------------- cluster # N-------------|
3783 * |----- ex -----| |---- ex_right ----|
3784 * |------ requested region ------|
3785 * |================|
3786 */
3787 if (map->m_lblk > ee_block) {
3788 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
3789 map->m_len = min(map->m_len, next - map->m_lblk);
3790 }
Aditya Kalid8990242011-09-09 19:18:51 -04003791
3792 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003793 return 1;
3794 }
Aditya Kalid8990242011-09-09 19:18:51 -04003795
3796 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003797 return 0;
3798}
3799
3800
3801/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05003802 * Block allocation/map/preallocation routine for extents based files
3803 *
3804 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003805 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003806 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3807 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05003808 *
3809 * return > 0, number of of blocks already mapped/allocated
3810 * if create == 0 and these are pre-allocated blocks
3811 * buffer head is unmapped
3812 * otherwise blocks are mapped
3813 *
3814 * return = 0, if plain look up failed (blocks have not been allocated)
3815 * buffer head is unmapped
3816 *
3817 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003818 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003819int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3820 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07003821{
3822 struct ext4_ext_path *path = NULL;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003823 struct ext4_extent newex, *ex, *ex2;
3824 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003825 ext4_fsblk_t newblock = 0;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003826 int free_on_err = 0, err = 0, depth, ret;
3827 unsigned int allocated = 0, offset = 0;
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04003828 unsigned int allocated_clusters = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003829 struct ext4_allocation_request ar;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003830 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003831 ext4_lblk_t cluster_offset;
Alex Tomasa86c6182006-10-11 01:21:03 -07003832
Mingming84fe3be2009-09-01 08:44:37 -04003833 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003834 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003835 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07003836
3837 /* check in cache */
Lukas Czerner78771912012-03-19 23:05:43 -04003838 if (ext4_ext_in_cache(inode, map->m_lblk, &newex)) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003839 if (!newex.ee_start_lo && !newex.ee_start_hi) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04003840 if ((sbi->s_cluster_ratio > 1) &&
3841 ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
3842 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3843
Theodore Ts'oc2177052009-05-14 00:58:52 -04003844 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003845 /*
3846 * block isn't allocated yet and
3847 * user doesn't want to allocate it
3848 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003849 goto out2;
3850 }
3851 /* we should allocate requested block */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003852 } else {
Alex Tomasa86c6182006-10-11 01:21:03 -07003853 /* block is already allocated */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003854 if (sbi->s_cluster_ratio > 1)
3855 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003856 newblock = map->m_lblk
Dave Kleikamp8c55e202007-05-24 13:04:54 -04003857 - le32_to_cpu(newex.ee_block)
Theodore Ts'obf89d162010-10-27 21:30:14 -04003858 + ext4_ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003859 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003860 allocated = ext4_ext_get_actual_len(&newex) -
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003861 (map->m_lblk - le32_to_cpu(newex.ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -07003862 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07003863 }
3864 }
3865
3866 /* find extent for this block */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003867 path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07003868 if (IS_ERR(path)) {
3869 err = PTR_ERR(path);
3870 path = NULL;
3871 goto out2;
3872 }
3873
3874 depth = ext_depth(inode);
3875
3876 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003877 * consistent leaf must not be empty;
3878 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07003879 * this is why assert can't be put in ext4_ext_find_extent()
3880 */
Frank Mayhar273df552010-03-02 11:46:09 -05003881 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
3882 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04003883 "lblock: %lu, depth: %d pblock %lld",
3884 (unsigned long) map->m_lblk, depth,
3885 path[depth].p_block);
Surbhi Palande034fb4c2009-12-14 09:53:52 -05003886 err = -EIO;
3887 goto out2;
3888 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003889
Avantika Mathur7e028972006-12-06 20:41:33 -08003890 ex = path[depth].p_ext;
3891 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003892 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003893 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003894 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003895
3896 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003897 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04003898 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003899 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003900 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04003901
3902 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
3903
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003904 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003905 if (in_range(map->m_lblk, ee_block, ee_len)) {
3906 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003907 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003908 allocated = ee_len - (map->m_lblk - ee_block);
3909 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
3910 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04003911
Allison Hendersone8613042011-05-25 07:41:46 -04003912 /*
Lukas Czerner78771912012-03-19 23:05:43 -04003913 * Do not put uninitialized extent
3914 * in the cache
Allison Hendersone8613042011-05-25 07:41:46 -04003915 */
Lukas Czerner78771912012-03-19 23:05:43 -04003916 if (!ext4_ext_is_uninitialized(ex)) {
3917 ext4_ext_put_in_cache(inode, ee_block,
3918 ee_len, ee_start);
3919 goto out;
Allison Hendersone8613042011-05-25 07:41:46 -04003920 }
Lukas Czerner78771912012-03-19 23:05:43 -04003921 ret = ext4_ext_handle_uninitialized_extents(
3922 handle, inode, map, path, flags,
3923 allocated, newblock);
3924 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07003925 }
3926 }
3927
Aditya Kali7b415bf2011-09-09 19:04:51 -04003928 if ((sbi->s_cluster_ratio > 1) &&
3929 ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
3930 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3931
Alex Tomasa86c6182006-10-11 01:21:03 -07003932 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003933 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07003934 * we couldn't try to create block if create flag is zero
3935 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04003936 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003937 /*
3938 * put just found gap into cache to speed up
3939 * subsequent requests
3940 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003941 ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
Alex Tomasa86c6182006-10-11 01:21:03 -07003942 goto out2;
3943 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003944
Alex Tomasa86c6182006-10-11 01:21:03 -07003945 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003946 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07003947 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003948 map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003949 newex.ee_block = cpu_to_le32(map->m_lblk);
3950 cluster_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
3951
3952 /*
3953 * If we are doing bigalloc, check to see if the extent returned
3954 * by ext4_ext_find_extent() implies a cluster we can use.
3955 */
3956 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04003957 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003958 ar.len = allocated = map->m_len;
3959 newblock = map->m_pblk;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003960 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003961 goto got_allocated_blocks;
3962 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003963
Alex Tomasc9de5602008-01-29 00:19:52 -05003964 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003965 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003966 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3967 if (err)
3968 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003969 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003970 ex2 = NULL;
3971 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
Alex Tomasc9de5602008-01-29 00:19:52 -05003972 if (err)
3973 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04003974
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003975 /* Check if the extent after searching to the right implies a
3976 * cluster we can use. */
3977 if ((sbi->s_cluster_ratio > 1) && ex2 &&
Aditya Kalid8990242011-09-09 19:18:51 -04003978 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003979 ar.len = allocated = map->m_len;
3980 newblock = map->m_pblk;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003981 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003982 goto got_allocated_blocks;
3983 }
3984
Amit Arora749269f2007-07-18 09:02:56 -04003985 /*
3986 * See if request is beyond maximum number of blocks we can have in
3987 * a single extent. For an initialized extent this limit is
3988 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
3989 * EXT_UNINIT_MAX_LEN.
3990 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003991 if (map->m_len > EXT_INIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003992 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003993 map->m_len = EXT_INIT_MAX_LEN;
3994 else if (map->m_len > EXT_UNINIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04003995 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003996 map->m_len = EXT_UNINIT_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04003997
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003998 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003999 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004000 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04004001 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004002 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04004003 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004004 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004005
4006 /* allocate new block */
4007 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004008 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
4009 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004010 /*
4011 * We calculate the offset from the beginning of the cluster
4012 * for the logical block number, since when we allocate a
4013 * physical cluster, the physical block should start at the
4014 * same offset from the beginning of the cluster. This is
4015 * needed so that future calls to get_implied_cluster_alloc()
4016 * work correctly.
4017 */
4018 offset = map->m_lblk & (sbi->s_cluster_ratio - 1);
4019 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
4020 ar.goal -= offset;
4021 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05004022 if (S_ISREG(inode->i_mode))
4023 ar.flags = EXT4_MB_HINT_DATA;
4024 else
4025 /* disable in-core preallocation for non-regular files */
4026 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04004027 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4028 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05004029 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004030 if (!newblock)
4031 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04004032 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004033 ar.goal, newblock, allocated);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004034 free_on_err = 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004035 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004036 ar.len = EXT4_C2B(sbi, ar.len) - offset;
4037 if (ar.len > allocated)
4038 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004039
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004040got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07004041 /* try to insert new extent into found leaf and return */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004042 ext4_ext_store_pblock(&newex, newblock + offset);
Alex Tomasc9de5602008-01-29 00:19:52 -05004043 newex.ee_len = cpu_to_le16(ar.len);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004044 /* Mark uninitialized */
4045 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
Amit Aroraa2df2a62007-07-17 21:42:41 -04004046 ext4_ext_mark_uninitialized(&newex);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004047 /*
Jiaying Zhang744692d2010-03-04 16:14:02 -05004048 * io_end structure was created for every IO write to an
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004049 * uninitialized extent. To avoid unnecessary conversion,
Jiaying Zhang744692d2010-03-04 16:14:02 -05004050 * here we flag the IO that really needs the conversion.
Mingming5f524952009-11-10 10:48:04 -05004051 * For non asycn direct IO case, flag the inode state
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004052 * that we need to perform conversion when IO is done.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004053 */
Jiaying Zhang744692d2010-03-04 16:14:02 -05004054 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Tao Ma0edeb712011-10-31 17:30:44 -04004055 if (io)
4056 ext4_set_io_unwritten_flag(inode, io);
4057 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004058 ext4_set_inode_state(inode,
4059 EXT4_STATE_DIO_UNWRITTEN);
Mingming5f524952009-11-10 10:48:04 -05004060 }
Jiaying Zhang744692d2010-03-04 16:14:02 -05004061 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004062 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004063 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004064
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004065 err = 0;
4066 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
4067 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4068 path, ar.len);
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004069 if (!err)
4070 err = ext4_ext_insert_extent(handle, inode, path,
4071 &newex, flags);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004072 if (err && free_on_err) {
Maxim Patlasov7132de72011-07-10 19:37:48 -04004073 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
4074 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
Alex Tomas315054f2007-05-24 13:04:25 -04004075 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05004076 /* not a good idea to call discard here directly,
4077 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004078 ext4_discard_preallocations(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05004079 ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex),
Maxim Patlasov7132de72011-07-10 19:37:48 -04004080 ext4_ext_get_actual_len(&newex), fb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004081 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04004082 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004083
Alex Tomasa86c6182006-10-11 01:21:03 -07004084 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04004085 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004086 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004087 if (allocated > map->m_len)
4088 allocated = map->m_len;
4089 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07004090
Jan Karab436b9b2009-12-08 23:51:10 -05004091 /*
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004092 * Update reserved blocks/metadata blocks after successful
4093 * block allocation which had been deferred till now.
4094 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04004095 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004096 unsigned int reserved_clusters;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004097 /*
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004098 * Check how many clusters we had reserved this allocated range
Aditya Kali7b415bf2011-09-09 19:04:51 -04004099 */
4100 reserved_clusters = get_reserved_cluster_alloc(inode,
4101 map->m_lblk, allocated);
4102 if (map->m_flags & EXT4_MAP_FROM_CLUSTER) {
4103 if (reserved_clusters) {
4104 /*
4105 * We have clusters reserved for this range.
4106 * But since we are not doing actual allocation
4107 * and are simply using blocks from previously
4108 * allocated cluster, we should release the
4109 * reservation and not claim quota.
4110 */
4111 ext4_da_update_reserve_space(inode,
4112 reserved_clusters, 0);
4113 }
4114 } else {
4115 BUG_ON(allocated_clusters < reserved_clusters);
4116 /* We will claim quota for all newly allocated blocks.*/
4117 ext4_da_update_reserve_space(inode, allocated_clusters,
4118 1);
4119 if (reserved_clusters < allocated_clusters) {
Aditya Kali5356f262011-09-09 19:20:51 -04004120 struct ext4_inode_info *ei = EXT4_I(inode);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004121 int reservation = allocated_clusters -
4122 reserved_clusters;
4123 /*
4124 * It seems we claimed few clusters outside of
4125 * the range of this allocation. We should give
4126 * it back to the reservation pool. This can
4127 * happen in the following case:
4128 *
4129 * * Suppose s_cluster_ratio is 4 (i.e., each
4130 * cluster has 4 blocks. Thus, the clusters
4131 * are [0-3],[4-7],[8-11]...
4132 * * First comes delayed allocation write for
4133 * logical blocks 10 & 11. Since there were no
4134 * previous delayed allocated blocks in the
4135 * range [8-11], we would reserve 1 cluster
4136 * for this write.
4137 * * Next comes write for logical blocks 3 to 8.
4138 * In this case, we will reserve 2 clusters
4139 * (for [0-3] and [4-7]; and not for [8-11] as
4140 * that range has a delayed allocated blocks.
4141 * Thus total reserved clusters now becomes 3.
4142 * * Now, during the delayed allocation writeout
4143 * time, we will first write blocks [3-8] and
4144 * allocate 3 clusters for writing these
4145 * blocks. Also, we would claim all these
4146 * three clusters above.
4147 * * Now when we come here to writeout the
4148 * blocks [10-11], we would expect to claim
4149 * the reservation of 1 cluster we had made
4150 * (and we would claim it since there are no
4151 * more delayed allocated blocks in the range
4152 * [8-11]. But our reserved cluster count had
4153 * already gone to 0.
4154 *
4155 * Thus, at the step 4 above when we determine
4156 * that there are still some unwritten delayed
4157 * allocated blocks outside of our current
4158 * block range, we should increment the
4159 * reserved clusters count so that when the
4160 * remaining blocks finally gets written, we
4161 * could claim them.
4162 */
Aditya Kali5356f262011-09-09 19:20:51 -04004163 dquot_reserve_block(inode,
4164 EXT4_C2B(sbi, reservation));
4165 spin_lock(&ei->i_block_reservation_lock);
4166 ei->i_reserved_data_blocks += reservation;
4167 spin_unlock(&ei->i_block_reservation_lock);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004168 }
4169 }
4170 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004171
4172 /*
Jan Karab436b9b2009-12-08 23:51:10 -05004173 * Cache the extent and update transaction to commit on fdatasync only
4174 * when it is _not_ an uninitialized extent.
4175 */
4176 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05004177 ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock);
Jan Karab436b9b2009-12-08 23:51:10 -05004178 ext4_update_inode_fsync_trans(handle, inode, 1);
4179 } else
4180 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004181out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004182 if (allocated > map->m_len)
4183 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004184 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004185 map->m_flags |= EXT4_MAP_MAPPED;
4186 map->m_pblk = newblock;
4187 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004188out2:
4189 if (path) {
4190 ext4_ext_drop_refs(path);
4191 kfree(path);
4192 }
Allison Hendersone8613042011-05-25 07:41:46 -04004193
Yongqiang Yange7b319e2011-10-29 09:39:51 -04004194 trace_ext4_ext_map_blocks_exit(inode, map->m_lblk,
Lukas Czerner78771912012-03-19 23:05:43 -04004195 newblock, map->m_len, err ? err : allocated);
Yongqiang Yange7b319e2011-10-29 09:39:51 -04004196
Lukas Czerner78771912012-03-19 23:05:43 -04004197 return err ? err : allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004198}
4199
Jan Karacf108bc2008-07-11 19:27:31 -04004200void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004201{
4202 struct address_space *mapping = inode->i_mapping;
4203 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004204 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004205 handle_t *handle;
Allison Henderson189e8682011-09-06 21:49:44 -04004206 loff_t page_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004207 int err = 0;
4208
4209 /*
Jiaying Zhang3889fd52011-01-10 12:47:05 -05004210 * finish any pending end_io work so we won't run the risk of
4211 * converting any truncated blocks to initialized later
4212 */
4213 ext4_flush_completed_IO(inode);
4214
4215 /*
Alex Tomasa86c6182006-10-11 01:21:03 -07004216 * probably first extent we're gonna free will be last in block
4217 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04004218 err = ext4_writepage_trans_blocks(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07004219 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04004220 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07004221 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07004222
Allison Henderson189e8682011-09-06 21:49:44 -04004223 if (inode->i_size % PAGE_CACHE_SIZE != 0) {
4224 page_len = PAGE_CACHE_SIZE -
4225 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4226
4227 err = ext4_discard_partial_page_buffers(handle,
4228 mapping, inode->i_size, page_len, 0);
4229
4230 if (err)
4231 goto out_stop;
4232 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004233
Jan Kara9ddfc3d2008-07-11 19:27:31 -04004234 if (ext4_orphan_add(handle, inode))
4235 goto out_stop;
4236
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004237 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07004238 ext4_ext_invalidate_cache(inode);
4239
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004240 ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05004241
Alex Tomasa86c6182006-10-11 01:21:03 -07004242 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004243 * TODO: optimization is possible here.
4244 * Probably we need not scan at all,
4245 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004246 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004247
4248 /* we have to know where to truncate from in crash case */
4249 EXT4_I(inode)->i_disksize = inode->i_size;
4250 ext4_mark_inode_dirty(handle, inode);
4251
4252 last_block = (inode->i_size + sb->s_blocksize - 1)
4253 >> EXT4_BLOCK_SIZE_BITS(sb);
Lukas Czerner5f95d212012-03-19 23:03:19 -04004254 err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07004255
4256 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04004257 * transaction synchronous.
4258 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004259 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05004260 ext4_handle_sync(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07004261
Jan Kara9ddfc3d2008-07-11 19:27:31 -04004262 up_write(&EXT4_I(inode)->i_data_sem);
Eric Gouriouf6d2f6b2011-05-22 21:33:00 -04004263
4264out_stop:
Alex Tomasa86c6182006-10-11 01:21:03 -07004265 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004266 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07004267 * then we need to clear up the orphan record which we created above.
4268 * However, if this was a real unlink then we were called by
4269 * ext4_delete_inode(), and we allow that function to clean up the
4270 * orphan info for us.
4271 */
4272 if (inode->i_nlink)
4273 ext4_orphan_del(handle, inode);
4274
Solofo Ramangalahyef737722008-04-29 22:00:41 -04004275 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4276 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07004277 ext4_journal_stop(handle);
4278}
4279
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004280static void ext4_falloc_update_inode(struct inode *inode,
4281 int mode, loff_t new_size, int update_ctime)
4282{
4283 struct timespec now;
4284
4285 if (update_ctime) {
4286 now = current_fs_time(inode->i_sb);
4287 if (!timespec_equal(&inode->i_ctime, &now))
4288 inode->i_ctime = now;
4289 }
4290 /*
4291 * Update only when preallocation was requested beyond
4292 * the file size.
4293 */
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04004294 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
4295 if (new_size > i_size_read(inode))
4296 i_size_write(inode, new_size);
4297 if (new_size > EXT4_I(inode)->i_disksize)
4298 ext4_update_i_disksize(inode, new_size);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004299 } else {
4300 /*
4301 * Mark that we allocate beyond EOF so the subsequent truncate
4302 * can proceed even if the new size is the same as i_size.
4303 */
4304 if (new_size > i_size_read(inode))
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004305 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004306 }
4307
4308}
4309
Amit Aroraa2df2a62007-07-17 21:42:41 -04004310/*
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004311 * preallocate space for a file. This implements ext4's fallocate file
Amit Aroraa2df2a62007-07-17 21:42:41 -04004312 * operation, which gets called from sys_fallocate system call.
4313 * For block-mapped files, posix_fallocate should fall back to the method
4314 * of writing zeroes to the required new blocks (the same behavior which is
4315 * expected for file systems which do not support fallocate() system call).
4316 */
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004317long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004318{
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004319 struct inode *inode = file->f_path.dentry->d_inode;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004320 handle_t *handle;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004321 loff_t new_size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004322 unsigned int max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004323 int ret = 0;
4324 int ret2 = 0;
4325 int retries = 0;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004326 int flags;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004327 struct ext4_map_blocks map;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004328 unsigned int credits, blkbits = inode->i_blkbits;
4329
4330 /*
4331 * currently supporting (pre)allocate mode for extent-based
4332 * files _only_
4333 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004334 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Amit Aroraa2df2a62007-07-17 21:42:41 -04004335 return -EOPNOTSUPP;
4336
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004337 /* Return error if mode is not supported */
4338 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
4339 return -EOPNOTSUPP;
4340
4341 if (mode & FALLOC_FL_PUNCH_HOLE)
4342 return ext4_punch_hole(file, offset, len);
4343
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004344 trace_ext4_fallocate_enter(inode, offset, len, mode);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004345 map.m_lblk = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004346 /*
4347 * We can't just convert len to max_blocks because
4348 * If blocksize = 4096 offset = 3072 and len = 2048
4349 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004350 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004351 - map.m_lblk;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004352 /*
Mingming Caof3bd1f32008-08-19 22:16:03 -04004353 * credits to insert 1 extent into extent tree
Amit Aroraa2df2a62007-07-17 21:42:41 -04004354 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04004355 credits = ext4_chunk_trans_blocks(inode, max_blocks);
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05004356 mutex_lock(&inode->i_mutex);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04004357 ret = inode_newsize_ok(inode, (len + offset));
4358 if (ret) {
4359 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004360 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04004361 return ret;
4362 }
Greg Harm3c6fe772011-10-31 18:41:47 -04004363 flags = EXT4_GET_BLOCKS_CREATE_UNINIT_EXT;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004364 if (mode & FALLOC_FL_KEEP_SIZE)
4365 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
Greg Harm3c6fe772011-10-31 18:41:47 -04004366 /*
4367 * Don't normalize the request if it can fit in one extent so
4368 * that it doesn't get unnecessarily split into multiple
4369 * extents.
4370 */
4371 if (len <= EXT_UNINIT_MAX_LEN << blkbits)
4372 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004373retry:
4374 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004375 map.m_lblk = map.m_lblk + ret;
4376 map.m_len = max_blocks = max_blocks - ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004377 handle = ext4_journal_start(inode, credits);
4378 if (IS_ERR(handle)) {
4379 ret = PTR_ERR(handle);
4380 break;
4381 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004382 ret = ext4_map_blocks(handle, inode, &map, flags);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05004383 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004384#ifdef EXT4FS_DEBUG
4385 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004386 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004387 "returned error inode#%lu, block=%u, "
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05004388 "max_blocks=%u", __func__,
Kazuya Mioa6371b62010-10-27 21:30:15 -04004389 inode->i_ino, map.m_lblk, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004390#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04004391 ext4_mark_inode_dirty(handle, inode);
4392 ret2 = ext4_journal_stop(handle);
4393 break;
4394 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004395 if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004396 blkbits) >> blkbits))
4397 new_size = offset + len;
4398 else
Utako Kusaka29ae07b2011-07-27 22:11:20 -04004399 new_size = ((loff_t) map.m_lblk + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004400
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004401 ext4_falloc_update_inode(inode, mode, new_size,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004402 (map.m_flags & EXT4_MAP_NEW));
Amit Aroraa2df2a62007-07-17 21:42:41 -04004403 ext4_mark_inode_dirty(handle, inode);
4404 ret2 = ext4_journal_stop(handle);
4405 if (ret2)
4406 break;
4407 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004408 if (ret == -ENOSPC &&
4409 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4410 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004411 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004412 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05004413 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004414 trace_ext4_fallocate_exit(inode, offset, max_blocks,
4415 ret > 0 ? ret2 : ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004416 return ret > 0 ? ret2 : ret;
4417}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004418
4419/*
Mingming Cao00314622009-09-28 15:49:08 -04004420 * This function convert a range of blocks to written extents
4421 * The caller of this function will pass the start offset and the size.
4422 * all unwritten extents within this range will be converted to
4423 * written extents.
4424 *
4425 * This function is called from the direct IO end io call back
4426 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05004427 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04004428 */
4429int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
Eric Sandeena1de02d2010-02-04 23:58:38 -05004430 ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04004431{
4432 handle_t *handle;
Mingming Cao00314622009-09-28 15:49:08 -04004433 unsigned int max_blocks;
4434 int ret = 0;
4435 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004436 struct ext4_map_blocks map;
Mingming Cao00314622009-09-28 15:49:08 -04004437 unsigned int credits, blkbits = inode->i_blkbits;
4438
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004439 map.m_lblk = offset >> blkbits;
Mingming Cao00314622009-09-28 15:49:08 -04004440 /*
4441 * We can't just convert len to max_blocks because
4442 * If blocksize = 4096 offset = 3072 and len = 2048
4443 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004444 max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
4445 map.m_lblk);
Mingming Cao00314622009-09-28 15:49:08 -04004446 /*
4447 * credits to insert 1 extent into extent tree
4448 */
4449 credits = ext4_chunk_trans_blocks(inode, max_blocks);
4450 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004451 map.m_lblk += ret;
4452 map.m_len = (max_blocks -= ret);
Mingming Cao00314622009-09-28 15:49:08 -04004453 handle = ext4_journal_start(inode, credits);
4454 if (IS_ERR(handle)) {
4455 ret = PTR_ERR(handle);
4456 break;
4457 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004458 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004459 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Mingming Cao00314622009-09-28 15:49:08 -04004460 if (ret <= 0) {
4461 WARN_ON(ret <= 0);
Theodore Ts'o92b97812012-03-19 23:41:49 -04004462 ext4_msg(inode->i_sb, KERN_ERR,
4463 "%s:%d: inode #%lu: block %u: len %u: "
4464 "ext4_ext_map_blocks returned %d",
4465 __func__, __LINE__, inode->i_ino, map.m_lblk,
4466 map.m_len, ret);
Mingming Cao00314622009-09-28 15:49:08 -04004467 }
4468 ext4_mark_inode_dirty(handle, inode);
4469 ret2 = ext4_journal_stop(handle);
4470 if (ret <= 0 || ret2 )
4471 break;
4472 }
4473 return ret > 0 ? ret2 : ret;
4474}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004475
Mingming Cao00314622009-09-28 15:49:08 -04004476/*
Eric Sandeen6873fa02008-10-07 00:46:36 -04004477 * Callback function called for each extent to gather FIEMAP information.
4478 */
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04004479static int ext4_ext_fiemap_cb(struct inode *inode, ext4_lblk_t next,
Eric Sandeen6873fa02008-10-07 00:46:36 -04004480 struct ext4_ext_cache *newex, struct ext4_extent *ex,
4481 void *data)
4482{
Eric Sandeen6873fa02008-10-07 00:46:36 -04004483 __u64 logical;
4484 __u64 physical;
4485 __u64 length;
4486 __u32 flags = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004487 int ret = 0;
4488 struct fiemap_extent_info *fieinfo = data;
4489 unsigned char blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004490
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004491 blksize_bits = inode->i_sb->s_blocksize_bits;
4492 logical = (__u64)newex->ec_block << blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004493
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05004494 if (newex->ec_start == 0) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004495 /*
4496 * No extent in extent-tree contains block @newex->ec_start,
4497 * then the block may stay in 1)a hole or 2)delayed-extent.
4498 *
4499 * Holes or delayed-extents are processed as follows.
4500 * 1. lookup dirty pages with specified range in pagecache.
4501 * If no page is got, then there is no delayed-extent and
4502 * return with EXT_CONTINUE.
4503 * 2. find the 1st mapped buffer,
4504 * 3. check if the mapped buffer is both in the request range
4505 * and a delayed buffer. If not, there is no delayed-extent,
4506 * then return.
4507 * 4. a delayed-extent is found, the extent will be collected.
4508 */
4509 ext4_lblk_t end = 0;
4510 pgoff_t last_offset;
4511 pgoff_t offset;
4512 pgoff_t index;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004513 pgoff_t start_index = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004514 struct page **pages = NULL;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004515 struct buffer_head *bh = NULL;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004516 struct buffer_head *head = NULL;
4517 unsigned int nr_pages = PAGE_SIZE / sizeof(struct page *);
4518
4519 pages = kmalloc(PAGE_SIZE, GFP_KERNEL);
4520 if (pages == NULL)
4521 return -ENOMEM;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004522
4523 offset = logical >> PAGE_SHIFT;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004524repeat:
4525 last_offset = offset;
4526 head = NULL;
4527 ret = find_get_pages_tag(inode->i_mapping, &offset,
4528 PAGECACHE_TAG_DIRTY, nr_pages, pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004529
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004530 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4531 /* First time, try to find a mapped buffer. */
4532 if (ret == 0) {
4533out:
4534 for (index = 0; index < ret; index++)
4535 page_cache_release(pages[index]);
4536 /* just a hole. */
4537 kfree(pages);
4538 return EXT_CONTINUE;
4539 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004540 index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004541
Yongqiang Yangb2213492011-05-24 11:36:58 -04004542next_page:
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004543 /* Try to find the 1st mapped buffer. */
Yongqiang Yangb2213492011-05-24 11:36:58 -04004544 end = ((__u64)pages[index]->index << PAGE_SHIFT) >>
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004545 blksize_bits;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004546 if (!page_has_buffers(pages[index]))
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004547 goto out;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004548 head = page_buffers(pages[index]);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004549 if (!head)
4550 goto out;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004551
Yongqiang Yangb2213492011-05-24 11:36:58 -04004552 index++;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004553 bh = head;
4554 do {
Yongqiang Yangb2213492011-05-24 11:36:58 -04004555 if (end >= newex->ec_block +
4556 newex->ec_len)
4557 /* The buffer is out of
4558 * the request range.
4559 */
4560 goto out;
4561
4562 if (buffer_mapped(bh) &&
4563 end >= newex->ec_block) {
4564 start_index = index - 1;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004565 /* get the 1st mapped buffer. */
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004566 goto found_mapped_buffer;
4567 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004568
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004569 bh = bh->b_this_page;
4570 end++;
4571 } while (bh != head);
4572
Yongqiang Yangb2213492011-05-24 11:36:58 -04004573 /* No mapped buffer in the range found in this page,
4574 * We need to look up next page.
4575 */
4576 if (index >= ret) {
4577 /* There is no page left, but we need to limit
4578 * newex->ec_len.
4579 */
4580 newex->ec_len = end - newex->ec_block;
4581 goto out;
4582 }
4583 goto next_page;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004584 } else {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004585 /*Find contiguous delayed buffers. */
4586 if (ret > 0 && pages[0]->index == last_offset)
4587 head = page_buffers(pages[0]);
4588 bh = head;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004589 index = 1;
4590 start_index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004591 }
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004592
4593found_mapped_buffer:
4594 if (bh != NULL && buffer_delay(bh)) {
4595 /* 1st or contiguous delayed buffer found. */
4596 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4597 /*
4598 * 1st delayed buffer found, record
4599 * the start of extent.
4600 */
4601 flags |= FIEMAP_EXTENT_DELALLOC;
4602 newex->ec_block = end;
4603 logical = (__u64)end << blksize_bits;
4604 }
4605 /* Find contiguous delayed buffers. */
4606 do {
4607 if (!buffer_delay(bh))
4608 goto found_delayed_extent;
4609 bh = bh->b_this_page;
4610 end++;
4611 } while (bh != head);
4612
Yongqiang Yangb2213492011-05-24 11:36:58 -04004613 for (; index < ret; index++) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004614 if (!page_has_buffers(pages[index])) {
4615 bh = NULL;
4616 break;
4617 }
4618 head = page_buffers(pages[index]);
4619 if (!head) {
4620 bh = NULL;
4621 break;
4622 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004623
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004624 if (pages[index]->index !=
Yongqiang Yangb2213492011-05-24 11:36:58 -04004625 pages[start_index]->index + index
4626 - start_index) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004627 /* Blocks are not contiguous. */
4628 bh = NULL;
4629 break;
4630 }
4631 bh = head;
4632 do {
4633 if (!buffer_delay(bh))
4634 /* Delayed-extent ends. */
4635 goto found_delayed_extent;
4636 bh = bh->b_this_page;
4637 end++;
4638 } while (bh != head);
4639 }
4640 } else if (!(flags & FIEMAP_EXTENT_DELALLOC))
4641 /* a hole found. */
4642 goto out;
4643
4644found_delayed_extent:
4645 newex->ec_len = min(end - newex->ec_block,
4646 (ext4_lblk_t)EXT_INIT_MAX_LEN);
4647 if (ret == nr_pages && bh != NULL &&
4648 newex->ec_len < EXT_INIT_MAX_LEN &&
4649 buffer_delay(bh)) {
4650 /* Have not collected an extent and continue. */
4651 for (index = 0; index < ret; index++)
4652 page_cache_release(pages[index]);
4653 goto repeat;
4654 }
4655
4656 for (index = 0; index < ret; index++)
4657 page_cache_release(pages[index]);
4658 kfree(pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004659 }
4660
4661 physical = (__u64)newex->ec_start << blksize_bits;
4662 length = (__u64)newex->ec_len << blksize_bits;
4663
4664 if (ex && ext4_ext_is_uninitialized(ex))
4665 flags |= FIEMAP_EXTENT_UNWRITTEN;
4666
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04004667 if (next == EXT_MAX_BLOCKS)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004668 flags |= FIEMAP_EXTENT_LAST;
4669
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004670 ret = fiemap_fill_next_extent(fieinfo, logical, physical,
Eric Sandeen6873fa02008-10-07 00:46:36 -04004671 length, flags);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004672 if (ret < 0)
4673 return ret;
4674 if (ret == 1)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004675 return EXT_BREAK;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004676 return EXT_CONTINUE;
4677}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004678/* fiemap flags we can handle specified here */
4679#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
4680
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004681static int ext4_xattr_fiemap(struct inode *inode,
4682 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004683{
4684 __u64 physical = 0;
4685 __u64 length;
4686 __u32 flags = FIEMAP_EXTENT_LAST;
4687 int blockbits = inode->i_sb->s_blocksize_bits;
4688 int error = 0;
4689
4690 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004691 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04004692 struct ext4_iloc iloc;
4693 int offset; /* offset of xattr in inode */
4694
4695 error = ext4_get_inode_loc(inode, &iloc);
4696 if (error)
4697 return error;
4698 physical = iloc.bh->b_blocknr << blockbits;
4699 offset = EXT4_GOOD_OLD_INODE_SIZE +
4700 EXT4_I(inode)->i_extra_isize;
4701 physical += offset;
4702 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
4703 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004704 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004705 } else { /* external block */
4706 physical = EXT4_I(inode)->i_file_acl << blockbits;
4707 length = inode->i_sb->s_blocksize;
4708 }
4709
4710 if (physical)
4711 error = fiemap_fill_next_extent(fieinfo, 0, physical,
4712 length, flags);
4713 return (error < 0 ? error : 0);
4714}
4715
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004716/*
4717 * ext4_ext_punch_hole
4718 *
4719 * Punches a hole of "length" bytes in a file starting
4720 * at byte "offset"
4721 *
4722 * @inode: The inode of the file to punch a hole in
4723 * @offset: The starting byte offset of the hole
4724 * @length: The length of the hole
4725 *
4726 * Returns the number of blocks removed or negative on err
4727 */
4728int ext4_ext_punch_hole(struct file *file, loff_t offset, loff_t length)
4729{
4730 struct inode *inode = file->f_path.dentry->d_inode;
4731 struct super_block *sb = inode->i_sb;
Lukas Czerner5f95d212012-03-19 23:03:19 -04004732 ext4_lblk_t first_block, stop_block;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004733 struct address_space *mapping = inode->i_mapping;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004734 handle_t *handle;
Allison Hendersonba062082011-09-03 11:55:59 -04004735 loff_t first_page, last_page, page_len;
4736 loff_t first_page_offset, last_page_offset;
Lukas Czerner5f95d212012-03-19 23:03:19 -04004737 int credits, err = 0;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004738
Allison Henderson2be47512011-09-03 11:56:52 -04004739 /* No need to punch hole beyond i_size */
4740 if (offset >= inode->i_size)
4741 return 0;
4742
4743 /*
4744 * If the hole extends beyond i_size, set the hole
4745 * to end after the page that contains i_size
4746 */
4747 if (offset + length > inode->i_size) {
4748 length = inode->i_size +
4749 PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) -
4750 offset;
4751 }
4752
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004753 first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
4754 last_page = (offset + length) >> PAGE_CACHE_SHIFT;
4755
4756 first_page_offset = first_page << PAGE_CACHE_SHIFT;
4757 last_page_offset = last_page << PAGE_CACHE_SHIFT;
4758
4759 /*
4760 * Write out all dirty pages to avoid race conditions
4761 * Then release them.
4762 */
4763 if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
4764 err = filemap_write_and_wait_range(mapping,
Allison Henderson2be47512011-09-03 11:56:52 -04004765 offset, offset + length - 1);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004766
Allison Henderson2be47512011-09-03 11:56:52 -04004767 if (err)
4768 return err;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004769 }
4770
4771 /* Now release the pages */
4772 if (last_page_offset > first_page_offset) {
4773 truncate_inode_pages_range(mapping, first_page_offset,
4774 last_page_offset-1);
4775 }
4776
4777 /* finish any pending end_io work */
4778 ext4_flush_completed_IO(inode);
4779
4780 credits = ext4_writepage_trans_blocks(inode);
4781 handle = ext4_journal_start(inode, credits);
4782 if (IS_ERR(handle))
4783 return PTR_ERR(handle);
4784
4785 err = ext4_orphan_add(handle, inode);
4786 if (err)
4787 goto out;
4788
4789 /*
Allison Hendersonba062082011-09-03 11:55:59 -04004790 * Now we need to zero out the non-page-aligned data in the
4791 * pages at the start and tail of the hole, and unmap the buffer
4792 * heads for the block aligned regions of the page that were
4793 * completely zeroed.
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004794 */
Allison Hendersonba062082011-09-03 11:55:59 -04004795 if (first_page > last_page) {
4796 /*
4797 * If the file space being truncated is contained within a page
4798 * just zero out and unmap the middle of that page
4799 */
4800 err = ext4_discard_partial_page_buffers(handle,
4801 mapping, offset, length, 0);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004802
Allison Hendersonba062082011-09-03 11:55:59 -04004803 if (err)
4804 goto out;
4805 } else {
4806 /*
4807 * zero out and unmap the partial page that contains
4808 * the start of the hole
4809 */
4810 page_len = first_page_offset - offset;
4811 if (page_len > 0) {
4812 err = ext4_discard_partial_page_buffers(handle, mapping,
4813 offset, page_len, 0);
4814 if (err)
4815 goto out;
4816 }
4817
4818 /*
4819 * zero out and unmap the partial page that contains
4820 * the end of the hole
4821 */
4822 page_len = offset + length - last_page_offset;
4823 if (page_len > 0) {
4824 err = ext4_discard_partial_page_buffers(handle, mapping,
4825 last_page_offset, page_len, 0);
4826 if (err)
4827 goto out;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004828 }
4829 }
4830
Allison Henderson2be47512011-09-03 11:56:52 -04004831 /*
4832 * If i_size is contained in the last page, we need to
4833 * unmap and zero the partial page after i_size
4834 */
4835 if (inode->i_size >> PAGE_CACHE_SHIFT == last_page &&
4836 inode->i_size % PAGE_CACHE_SIZE != 0) {
4837
4838 page_len = PAGE_CACHE_SIZE -
4839 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4840
4841 if (page_len > 0) {
4842 err = ext4_discard_partial_page_buffers(handle,
4843 mapping, inode->i_size, page_len, 0);
4844
4845 if (err)
4846 goto out;
4847 }
4848 }
4849
Lukas Czerner5f95d212012-03-19 23:03:19 -04004850 first_block = (offset + sb->s_blocksize - 1) >>
4851 EXT4_BLOCK_SIZE_BITS(sb);
4852 stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
4853
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004854 /* If there are no blocks to remove, return now */
Lukas Czerner5f95d212012-03-19 23:03:19 -04004855 if (first_block >= stop_block)
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004856 goto out;
4857
4858 down_write(&EXT4_I(inode)->i_data_sem);
4859 ext4_ext_invalidate_cache(inode);
4860 ext4_discard_preallocations(inode);
4861
Lukas Czerner5f95d212012-03-19 23:03:19 -04004862 err = ext4_ext_remove_space(inode, first_block, stop_block - 1);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004863
Lukas Czerner5f95d212012-03-19 23:03:19 -04004864 ext4_ext_invalidate_cache(inode);
4865 ext4_discard_preallocations(inode);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004866
4867 if (IS_SYNC(inode))
4868 ext4_handle_sync(handle);
4869
4870 up_write(&EXT4_I(inode)->i_data_sem);
4871
4872out:
4873 ext4_orphan_del(handle, inode);
4874 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4875 ext4_mark_inode_dirty(handle, inode);
4876 ext4_journal_stop(handle);
4877 return err;
4878}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004879int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4880 __u64 start, __u64 len)
4881{
4882 ext4_lblk_t start_blk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004883 int error = 0;
4884
4885 /* fallback to generic here if not in extents fmt */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004886 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeen6873fa02008-10-07 00:46:36 -04004887 return generic_block_fiemap(inode, fieinfo, start, len,
4888 ext4_get_block);
4889
4890 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
4891 return -EBADR;
4892
4893 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
4894 error = ext4_xattr_fiemap(inode, fieinfo);
4895 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004896 ext4_lblk_t len_blks;
4897 __u64 last_blk;
4898
Eric Sandeen6873fa02008-10-07 00:46:36 -04004899 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004900 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04004901 if (last_blk >= EXT_MAX_BLOCKS)
4902 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004903 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004904
4905 /*
4906 * Walk the extent tree gathering extent information.
4907 * ext4_ext_fiemap_cb will push extents back to user.
4908 */
Eric Sandeen6873fa02008-10-07 00:46:36 -04004909 error = ext4_ext_walk_space(inode, start_blk, len_blks,
4910 ext4_ext_fiemap_cb, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004911 }
4912
4913 return error;
4914}