blob: 852d4c257ace6753d6e2bce038a5ef75d167efb9 [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,
Forrest Liucb832982012-12-17 09:55:39 -05002113 struct ext4_ext_path *path, int depth)
Alex Tomasa86c6182006-10-11 01:21:03 -07002114{
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 */
Forrest Liucb832982012-12-17 09:55:39 -05002119 depth--;
2120 path = path + depth;
Theodore Ts'obf89d162010-10-27 21:30:14 -04002121 leaf = ext4_idx_pblock(path->p_idx);
Frank Mayhar273df552010-03-02 11:46:09 -05002122 if (unlikely(path->p_hdr->eh_entries == 0)) {
2123 EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
2124 return -EIO;
2125 }
Avantika Mathur7e028972006-12-06 20:41:33 -08002126 err = ext4_ext_get_access(handle, inode, path);
2127 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002128 return err;
Robin Dong0e1147b2011-07-27 21:29:33 -04002129
2130 if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
2131 int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
2132 len *= sizeof(struct ext4_extent_idx);
2133 memmove(path->p_idx, path->p_idx + 1, len);
2134 }
2135
Marcin Slusarze8546d02008-04-17 10:38:59 -04002136 le16_add_cpu(&path->p_hdr->eh_entries, -1);
Avantika Mathur7e028972006-12-06 20:41:33 -08002137 err = ext4_ext_dirty(handle, inode, path);
2138 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002139 return err;
Mingming Cao2ae02102006-10-11 01:21:11 -07002140 ext_debug("index is empty, remove it, free block %llu\n", leaf);
Aditya Kalid8990242011-09-09 19:18:51 -04002141 trace_ext4_ext_rm_idx(inode, leaf);
2142
Peter Huewe7dc57612011-02-21 21:01:42 -05002143 ext4_free_blocks(handle, inode, NULL, leaf, 1,
Theodore Ts'oe6362602009-11-23 07:17:05 -05002144 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
Forrest Liucb832982012-12-17 09:55:39 -05002145
2146 while (--depth >= 0) {
2147 if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr))
2148 break;
2149 path--;
2150 err = ext4_ext_get_access(handle, inode, path);
2151 if (err)
2152 break;
2153 path->p_idx->ei_block = (path+1)->p_idx->ei_block;
2154 err = ext4_ext_dirty(handle, inode, path);
2155 if (err)
2156 break;
2157 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002158 return err;
2159}
2160
2161/*
Mingming Caoee12b632008-08-19 22:16:05 -04002162 * ext4_ext_calc_credits_for_single_extent:
2163 * This routine returns max. credits that needed to insert an extent
2164 * to the extent tree.
2165 * When pass the actual path, the caller should calculate credits
2166 * under i_data_sem.
Alex Tomasa86c6182006-10-11 01:21:03 -07002167 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002168int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
Alex Tomasa86c6182006-10-11 01:21:03 -07002169 struct ext4_ext_path *path)
2170{
Alex Tomasa86c6182006-10-11 01:21:03 -07002171 if (path) {
Mingming Caoee12b632008-08-19 22:16:05 -04002172 int depth = ext_depth(inode);
Mingming Caof3bd1f32008-08-19 22:16:03 -04002173 int ret = 0;
Mingming Caoee12b632008-08-19 22:16:05 -04002174
Alex Tomasa86c6182006-10-11 01:21:03 -07002175 /* probably there is space in leaf? */
Alex Tomasa86c6182006-10-11 01:21:03 -07002176 if (le16_to_cpu(path[depth].p_hdr->eh_entries)
Mingming Caoee12b632008-08-19 22:16:05 -04002177 < le16_to_cpu(path[depth].p_hdr->eh_max)) {
2178
2179 /*
2180 * There are some space in the leaf tree, no
2181 * need to account for leaf block credit
2182 *
2183 * bitmaps and block group descriptor blocks
Tao Madf3ab172011-10-08 15:53:49 -04002184 * and other metadata blocks still need to be
Mingming Caoee12b632008-08-19 22:16:05 -04002185 * accounted.
2186 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002187 /* 1 bitmap, 1 block group descriptor */
Mingming Caoee12b632008-08-19 22:16:05 -04002188 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
Aneesh Kumar K.V5887e982009-07-05 23:12:04 -04002189 return ret;
Mingming Caoee12b632008-08-19 22:16:05 -04002190 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002191 }
2192
Mingming Cao525f4ed2008-08-19 22:15:58 -04002193 return ext4_chunk_trans_blocks(inode, nrblocks);
Mingming Caoee12b632008-08-19 22:16:05 -04002194}
Alex Tomasa86c6182006-10-11 01:21:03 -07002195
Mingming Caoee12b632008-08-19 22:16:05 -04002196/*
2197 * How many index/leaf blocks need to change/allocate to modify nrblocks?
2198 *
2199 * if nrblocks are fit in a single extent (chunk flag is 1), then
2200 * in the worse case, each tree level index/leaf need to be changed
2201 * if the tree split due to insert a new extent, then the old tree
2202 * index/leaf need to be updated too
2203 *
2204 * If the nrblocks are discontiguous, they could cause
2205 * the whole tree split more than once, but this is really rare.
2206 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002207int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoee12b632008-08-19 22:16:05 -04002208{
2209 int index;
2210 int depth = ext_depth(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07002211
Mingming Caoee12b632008-08-19 22:16:05 -04002212 if (chunk)
2213 index = depth * 2;
2214 else
2215 index = depth * 3;
Alex Tomasa86c6182006-10-11 01:21:03 -07002216
Mingming Caoee12b632008-08-19 22:16:05 -04002217 return index;
Alex Tomasa86c6182006-10-11 01:21:03 -07002218}
2219
2220static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002221 struct ext4_extent *ex,
2222 ext4_fsblk_t *partial_cluster,
2223 ext4_lblk_t from, ext4_lblk_t to)
Alex Tomasa86c6182006-10-11 01:21:03 -07002224{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002225 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002226 unsigned short ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002227 ext4_fsblk_t pblk;
Theodore Ts'oe6362602009-11-23 07:17:05 -05002228 int flags = EXT4_FREE_BLOCKS_FORGET;
Alex Tomasa86c6182006-10-11 01:21:03 -07002229
Alex Tomasc9de5602008-01-29 00:19:52 -05002230 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
Theodore Ts'oe6362602009-11-23 07:17:05 -05002231 flags |= EXT4_FREE_BLOCKS_METADATA;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002232 /*
2233 * For bigalloc file systems, we never free a partial cluster
2234 * at the beginning of the extent. Instead, we make a note
2235 * that we tried freeing the cluster, and check to see if we
2236 * need to free it on a subsequent call to ext4_remove_blocks,
2237 * or at the end of the ext4_truncate() operation.
2238 */
2239 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
2240
Aditya Kalid8990242011-09-09 19:18:51 -04002241 trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002242 /*
2243 * If we have a partial cluster, and it's different from the
2244 * cluster of the last block, we need to explicitly free the
2245 * partial cluster here.
2246 */
2247 pblk = ext4_ext_pblock(ex) + ee_len - 1;
2248 if (*partial_cluster && (EXT4_B2C(sbi, pblk) != *partial_cluster)) {
2249 ext4_free_blocks(handle, inode, NULL,
2250 EXT4_C2B(sbi, *partial_cluster),
2251 sbi->s_cluster_ratio, flags);
2252 *partial_cluster = 0;
2253 }
2254
Alex Tomasa86c6182006-10-11 01:21:03 -07002255#ifdef EXTENTS_STATS
2256 {
2257 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002258 spin_lock(&sbi->s_ext_stats_lock);
2259 sbi->s_ext_blocks += ee_len;
2260 sbi->s_ext_extents++;
2261 if (ee_len < sbi->s_ext_min)
2262 sbi->s_ext_min = ee_len;
2263 if (ee_len > sbi->s_ext_max)
2264 sbi->s_ext_max = ee_len;
2265 if (ext_depth(inode) > sbi->s_depth_max)
2266 sbi->s_depth_max = ext_depth(inode);
2267 spin_unlock(&sbi->s_ext_stats_lock);
2268 }
2269#endif
2270 if (from >= le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002271 && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002272 /* tail removal */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002273 ext4_lblk_t num;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002274
Amit Aroraa2df2a62007-07-17 21:42:41 -04002275 num = le32_to_cpu(ex->ee_block) + ee_len - from;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002276 pblk = ext4_ext_pblock(ex) + ee_len - num;
2277 ext_debug("free last %u blocks starting %llu\n", num, pblk);
2278 ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
2279 /*
2280 * If the block range to be freed didn't start at the
2281 * beginning of a cluster, and we removed the entire
2282 * extent, save the partial cluster here, since we
2283 * might need to delete if we determine that the
2284 * truncate operation has removed all of the blocks in
2285 * the cluster.
2286 */
2287 if (pblk & (sbi->s_cluster_ratio - 1) &&
2288 (ee_len == num))
2289 *partial_cluster = EXT4_B2C(sbi, pblk);
2290 else
2291 *partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002292 } else if (from == le32_to_cpu(ex->ee_block)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002293 && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002294 /* head removal */
2295 ext4_lblk_t num;
2296 ext4_fsblk_t start;
2297
2298 num = to - from;
2299 start = ext4_ext_pblock(ex);
2300
2301 ext_debug("free first %u blocks starting %llu\n", num, start);
H Hartley Sweetenee90d572011-10-18 11:01:51 -04002302 ext4_free_blocks(handle, inode, NULL, start, num, flags);
Allison Hendersond583fb82011-05-25 07:41:43 -04002303
Alex Tomasa86c6182006-10-11 01:21:03 -07002304 } else {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002305 printk(KERN_INFO "strange request: removal(2) "
2306 "%u-%u from %u:%u\n",
2307 from, to, le32_to_cpu(ex->ee_block), ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002308 }
2309 return 0;
2310}
2311
Allison Hendersond583fb82011-05-25 07:41:43 -04002312
2313/*
2314 * ext4_ext_rm_leaf() Removes the extents associated with the
2315 * blocks appearing between "start" and "end", and splits the extents
2316 * if "start" and "end" appear in the same extent
2317 *
2318 * @handle: The journal handle
2319 * @inode: The files inode
2320 * @path: The path to the leaf
2321 * @start: The first block to remove
2322 * @end: The last block to remove
2323 */
Alex Tomasa86c6182006-10-11 01:21:03 -07002324static int
2325ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002326 struct ext4_ext_path *path, ext4_fsblk_t *partial_cluster,
2327 ext4_lblk_t start, ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002328{
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002329 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002330 int err = 0, correct_index = 0;
2331 int depth = ext_depth(inode), credits;
2332 struct ext4_extent_header *eh;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002333 ext4_lblk_t a, b;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002334 unsigned num;
2335 ext4_lblk_t ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002336 unsigned short ex_ee_len;
Amit Aroraa2df2a62007-07-17 21:42:41 -04002337 unsigned uninitialized = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002338 struct ext4_extent *ex;
2339
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002340 /* the header must be checked already in ext4_ext_remove_space() */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002341 ext_debug("truncate since %u in leaf to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002342 if (!path[depth].p_hdr)
2343 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
2344 eh = path[depth].p_hdr;
Frank Mayhar273df552010-03-02 11:46:09 -05002345 if (unlikely(path[depth].p_hdr == NULL)) {
2346 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
2347 return -EIO;
2348 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002349 /* find where to start removing */
2350 ex = EXT_LAST_EXTENT(eh);
2351
2352 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002353 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002354
Aditya Kalid8990242011-09-09 19:18:51 -04002355 trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster);
2356
Alex Tomasa86c6182006-10-11 01:21:03 -07002357 while (ex >= EXT_FIRST_EXTENT(eh) &&
2358 ex_ee_block + ex_ee_len > start) {
Aneesh Kumar K.Va41f2072009-06-10 14:22:55 -04002359
2360 if (ext4_ext_is_uninitialized(ex))
2361 uninitialized = 1;
2362 else
2363 uninitialized = 0;
2364
Mingming553f9002009-09-18 13:34:55 -04002365 ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
2366 uninitialized, ex_ee_len);
Alex Tomasa86c6182006-10-11 01:21:03 -07002367 path[depth].p_ext = ex;
2368
2369 a = ex_ee_block > start ? ex_ee_block : start;
Allison Hendersond583fb82011-05-25 07:41:43 -04002370 b = ex_ee_block+ex_ee_len - 1 < end ?
2371 ex_ee_block+ex_ee_len - 1 : end;
Alex Tomasa86c6182006-10-11 01:21:03 -07002372
2373 ext_debug(" border %u:%u\n", a, b);
2374
Allison Hendersond583fb82011-05-25 07:41:43 -04002375 /* If this extent is beyond the end of the hole, skip it */
Lukas Czerner5f95d212012-03-19 23:03:19 -04002376 if (end < ex_ee_block) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002377 ex--;
2378 ex_ee_block = le32_to_cpu(ex->ee_block);
2379 ex_ee_len = ext4_ext_get_actual_len(ex);
2380 continue;
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002381 } else if (b != ex_ee_block + ex_ee_len - 1) {
Lukas Czernerdc1841d2012-03-19 23:07:43 -04002382 EXT4_ERROR_INODE(inode,
2383 "can not handle truncate %u:%u "
2384 "on extent %u:%u",
2385 start, end, ex_ee_block,
2386 ex_ee_block + ex_ee_len - 1);
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002387 err = -EIO;
2388 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002389 } else if (a != ex_ee_block) {
2390 /* remove tail of the extent */
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002391 num = a - ex_ee_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07002392 } else {
2393 /* remove whole extent: excellent! */
Alex Tomasa86c6182006-10-11 01:21:03 -07002394 num = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002395 }
Theodore Ts'o34071da2008-08-01 21:59:19 -04002396 /*
2397 * 3 for leaf, sb, and inode plus 2 (bmap and group
2398 * descriptor) for each block group; assume two block
2399 * groups plus ex_ee_len/blocks_per_block_group for
2400 * the worst case
2401 */
2402 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
Alex Tomasa86c6182006-10-11 01:21:03 -07002403 if (ex == EXT_FIRST_EXTENT(eh)) {
2404 correct_index = 1;
2405 credits += (ext_depth(inode)) + 1;
2406 }
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05002407 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
Alex Tomasa86c6182006-10-11 01:21:03 -07002408
Jan Kara487caee2009-08-17 22:17:20 -04002409 err = ext4_ext_truncate_extend_restart(handle, inode, credits);
Shen Feng9102e4f2008-07-11 19:27:31 -04002410 if (err)
Alex Tomasa86c6182006-10-11 01:21:03 -07002411 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07002412
2413 err = ext4_ext_get_access(handle, inode, path + depth);
2414 if (err)
2415 goto out;
2416
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002417 err = ext4_remove_blocks(handle, inode, ex, partial_cluster,
2418 a, b);
Alex Tomasa86c6182006-10-11 01:21:03 -07002419 if (err)
2420 goto out;
2421
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002422 if (num == 0)
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002423 /* this extent is removed; mark slot entirely unused */
Alex Tomasf65e6fb2006-10-11 01:21:05 -07002424 ext4_ext_store_pblock(ex, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07002425
Alex Tomasa86c6182006-10-11 01:21:03 -07002426 ex->ee_len = cpu_to_le16(num);
Amit Arora749269f2007-07-18 09:02:56 -04002427 /*
2428 * Do not mark uninitialized if all the blocks in the
2429 * extent have been removed.
2430 */
2431 if (uninitialized && num)
Amit Aroraa2df2a62007-07-17 21:42:41 -04002432 ext4_ext_mark_uninitialized(ex);
Allison Hendersond583fb82011-05-25 07:41:43 -04002433 /*
2434 * If the extent was completely released,
2435 * we need to remove it from the leaf
2436 */
2437 if (num == 0) {
Lukas Czernerf17722f2011-06-06 00:05:17 -04002438 if (end != EXT_MAX_BLOCKS - 1) {
Allison Hendersond583fb82011-05-25 07:41:43 -04002439 /*
2440 * For hole punching, we need to scoot all the
2441 * extents up when an extent is removed so that
2442 * we dont have blank extents in the middle
2443 */
2444 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
2445 sizeof(struct ext4_extent));
2446
2447 /* Now get rid of the one at the end */
2448 memset(EXT_LAST_EXTENT(eh), 0,
2449 sizeof(struct ext4_extent));
2450 }
2451 le16_add_cpu(&eh->eh_entries, -1);
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002452 } else
2453 *partial_cluster = 0;
Allison Hendersond583fb82011-05-25 07:41:43 -04002454
Dmitry Monakhov750c9c42011-10-25 05:35:05 -04002455 err = ext4_ext_dirty(handle, inode, path + depth);
2456 if (err)
2457 goto out;
2458
Yongqiang Yangbf52c6f2011-11-01 18:59:26 -04002459 ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
Theodore Ts'obf89d162010-10-27 21:30:14 -04002460 ext4_ext_pblock(ex));
Alex Tomasa86c6182006-10-11 01:21:03 -07002461 ex--;
2462 ex_ee_block = le32_to_cpu(ex->ee_block);
Amit Aroraa2df2a62007-07-17 21:42:41 -04002463 ex_ee_len = ext4_ext_get_actual_len(ex);
Alex Tomasa86c6182006-10-11 01:21:03 -07002464 }
2465
2466 if (correct_index && eh->eh_entries)
2467 err = ext4_ext_correct_indexes(handle, inode, path);
2468
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002469 /*
2470 * If there is still a entry in the leaf node, check to see if
2471 * it references the partial cluster. This is the only place
2472 * where it could; if it doesn't, we can free the cluster.
2473 */
2474 if (*partial_cluster && ex >= EXT_FIRST_EXTENT(eh) &&
2475 (EXT4_B2C(sbi, ext4_ext_pblock(ex) + ex_ee_len - 1) !=
2476 *partial_cluster)) {
2477 int flags = EXT4_FREE_BLOCKS_FORGET;
2478
2479 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2480 flags |= EXT4_FREE_BLOCKS_METADATA;
2481
2482 ext4_free_blocks(handle, inode, NULL,
2483 EXT4_C2B(sbi, *partial_cluster),
2484 sbi->s_cluster_ratio, flags);
2485 *partial_cluster = 0;
2486 }
2487
Alex Tomasa86c6182006-10-11 01:21:03 -07002488 /* if this leaf is free, then we should
2489 * remove it from index block above */
2490 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
Forrest Liucb832982012-12-17 09:55:39 -05002491 err = ext4_ext_rm_idx(handle, inode, path, depth);
Alex Tomasa86c6182006-10-11 01:21:03 -07002492
2493out:
2494 return err;
2495}
2496
2497/*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002498 * ext4_ext_more_to_rm:
2499 * returns 1 if current index has to be freed (even partial)
Alex Tomasa86c6182006-10-11 01:21:03 -07002500 */
Avantika Mathur09b88252006-12-06 20:41:36 -08002501static int
Alex Tomasa86c6182006-10-11 01:21:03 -07002502ext4_ext_more_to_rm(struct ext4_ext_path *path)
2503{
2504 BUG_ON(path->p_idx == NULL);
2505
2506 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
2507 return 0;
2508
2509 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002510 * if truncate on deeper level happened, it wasn't partial,
Alex Tomasa86c6182006-10-11 01:21:03 -07002511 * so we have to consider current index for truncation
2512 */
2513 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
2514 return 0;
2515 return 1;
2516}
2517
Lukas Czerner5f95d212012-03-19 23:03:19 -04002518static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
2519 ext4_lblk_t end)
Alex Tomasa86c6182006-10-11 01:21:03 -07002520{
2521 struct super_block *sb = inode->i_sb;
2522 int depth = ext_depth(inode);
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002523 struct ext4_ext_path *path = NULL;
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002524 ext4_fsblk_t partial_cluster = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002525 handle_t *handle;
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002526 int i = 0, err;
Alex Tomasa86c6182006-10-11 01:21:03 -07002527
Lukas Czerner5f95d212012-03-19 23:03:19 -04002528 ext_debug("truncate since %u to %u\n", start, end);
Alex Tomasa86c6182006-10-11 01:21:03 -07002529
2530 /* probably first extent we're gonna free will be last in block */
2531 handle = ext4_journal_start(inode, depth + 1);
2532 if (IS_ERR(handle))
2533 return PTR_ERR(handle);
2534
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002535again:
Alex Tomasa86c6182006-10-11 01:21:03 -07002536 ext4_ext_invalidate_cache(inode);
2537
Aditya Kalid8990242011-09-09 19:18:51 -04002538 trace_ext4_ext_remove_space(inode, start, depth);
2539
Alex Tomasa86c6182006-10-11 01:21:03 -07002540 /*
Lukas Czerner5f95d212012-03-19 23:03:19 -04002541 * Check if we are removing extents inside the extent tree. If that
2542 * is the case, we are going to punch a hole inside the extent tree
2543 * so we have to check whether we need to split the extent covering
2544 * the last block to remove so we can easily remove the part of it
2545 * in ext4_ext_rm_leaf().
2546 */
2547 if (end < EXT_MAX_BLOCKS - 1) {
2548 struct ext4_extent *ex;
2549 ext4_lblk_t ee_block;
2550
2551 /* find extent for this block */
2552 path = ext4_ext_find_extent(inode, end, NULL);
2553 if (IS_ERR(path)) {
2554 ext4_journal_stop(handle);
2555 return PTR_ERR(path);
2556 }
2557 depth = ext_depth(inode);
2558 ex = path[depth].p_ext;
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002559 if (!ex) {
2560 ext4_ext_drop_refs(path);
2561 kfree(path);
2562 path = NULL;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002563 goto cont;
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002564 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002565
2566 ee_block = le32_to_cpu(ex->ee_block);
2567
2568 /*
2569 * See if the last block is inside the extent, if so split
2570 * the extent at 'end' block so we can easily remove the
2571 * tail of the first part of the split extent in
2572 * ext4_ext_rm_leaf().
2573 */
2574 if (end >= ee_block &&
2575 end < ee_block + ext4_ext_get_actual_len(ex) - 1) {
2576 int split_flag = 0;
2577
2578 if (ext4_ext_is_uninitialized(ex))
2579 split_flag = EXT4_EXT_MARK_UNINIT1 |
2580 EXT4_EXT_MARK_UNINIT2;
2581
2582 /*
2583 * Split the extent in two so that 'end' is the last
2584 * block in the first new extent
2585 */
2586 err = ext4_split_extent_at(handle, inode, path,
2587 end + 1, split_flag,
2588 EXT4_GET_BLOCKS_PRE_IO |
2589 EXT4_GET_BLOCKS_PUNCH_OUT_EXT);
2590
2591 if (err < 0)
2592 goto out;
2593 }
Lukas Czerner5f95d212012-03-19 23:03:19 -04002594 }
2595cont:
2596
2597 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002598 * We start scanning from right side, freeing all the blocks
2599 * after i_size and walking into the tree depth-wise.
Alex Tomasa86c6182006-10-11 01:21:03 -07002600 */
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002601 depth = ext_depth(inode);
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002602 if (path) {
2603 int k = i = depth;
2604 while (--k > 0)
2605 path[k].p_block =
2606 le16_to_cpu(path[k].p_hdr->eh_entries)+1;
2607 } else {
2608 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1),
2609 GFP_NOFS);
2610 if (path == NULL) {
2611 ext4_journal_stop(handle);
2612 return -ENOMEM;
2613 }
2614 path[0].p_depth = depth;
2615 path[0].p_hdr = ext_inode_hdr(inode);
Theodore Ts'od8b868f2012-08-17 08:54:52 -04002616 i = 0;
Lukas Czerner5f95d212012-03-19 23:03:19 -04002617
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002618 if (ext4_ext_check(inode, path[0].p_hdr, depth)) {
2619 err = -EIO;
2620 goto out;
2621 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002622 }
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002623 err = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -07002624
2625 while (i >= 0 && err == 0) {
2626 if (i == depth) {
2627 /* this is leaf block */
Allison Hendersond583fb82011-05-25 07:41:43 -04002628 err = ext4_ext_rm_leaf(handle, inode, path,
Theodore Ts'o0aa06002011-09-09 18:54:51 -04002629 &partial_cluster, start,
Lukas Czerner5f95d212012-03-19 23:03:19 -04002630 end);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002631 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002632 brelse(path[i].p_bh);
2633 path[i].p_bh = NULL;
2634 i--;
2635 continue;
2636 }
2637
2638 /* this is index block */
2639 if (!path[i].p_hdr) {
2640 ext_debug("initialize header\n");
2641 path[i].p_hdr = ext_block_hdr(path[i].p_bh);
Alex Tomasa86c6182006-10-11 01:21:03 -07002642 }
2643
Alex Tomasa86c6182006-10-11 01:21:03 -07002644 if (!path[i].p_idx) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002645 /* this level hasn't been touched yet */
Alex Tomasa86c6182006-10-11 01:21:03 -07002646 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
2647 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
2648 ext_debug("init index ptr: hdr 0x%p, num %d\n",
2649 path[i].p_hdr,
2650 le16_to_cpu(path[i].p_hdr->eh_entries));
2651 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002652 /* we were already here, see at next index */
Alex Tomasa86c6182006-10-11 01:21:03 -07002653 path[i].p_idx--;
2654 }
2655
2656 ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
2657 i, EXT_FIRST_INDEX(path[i].p_hdr),
2658 path[i].p_idx);
2659 if (ext4_ext_more_to_rm(path + i)) {
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002660 struct buffer_head *bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002661 /* go to the next level */
Mingming Cao2ae02102006-10-11 01:21:11 -07002662 ext_debug("move to level %d (block %llu)\n",
Theodore Ts'obf89d162010-10-27 21:30:14 -04002663 i + 1, ext4_idx_pblock(path[i].p_idx));
Alex Tomasa86c6182006-10-11 01:21:03 -07002664 memset(path + i + 1, 0, sizeof(*path));
Theodore Ts'obf89d162010-10-27 21:30:14 -04002665 bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx));
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002666 if (!bh) {
Alex Tomasa86c6182006-10-11 01:21:03 -07002667 /* should we reset i_size? */
2668 err = -EIO;
2669 break;
2670 }
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002671 if (WARN_ON(i + 1 > depth)) {
2672 err = -EIO;
2673 break;
2674 }
Aneesh Kumar K.V56b19862009-03-12 09:51:20 -04002675 if (ext4_ext_check(inode, ext_block_hdr(bh),
Alex Tomasc29c0ae2007-07-18 09:19:09 -04002676 depth - i - 1)) {
2677 err = -EIO;
2678 break;
2679 }
2680 path[i + 1].p_bh = bh;
Alex Tomasa86c6182006-10-11 01:21:03 -07002681
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002682 /* save actual number of indexes since this
2683 * number is changed at the next iteration */
Alex Tomasa86c6182006-10-11 01:21:03 -07002684 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
2685 i++;
2686 } else {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002687 /* we finished processing this index, go up */
Alex Tomasa86c6182006-10-11 01:21:03 -07002688 if (path[i].p_hdr->eh_entries == 0 && i > 0) {
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002689 /* index is empty, remove it;
Alex Tomasa86c6182006-10-11 01:21:03 -07002690 * handle must be already prepared by the
2691 * truncatei_leaf() */
Forrest Liucb832982012-12-17 09:55:39 -05002692 err = ext4_ext_rm_idx(handle, inode, path, i);
Alex Tomasa86c6182006-10-11 01:21:03 -07002693 }
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002694 /* root level has p_bh == NULL, brelse() eats this */
Alex Tomasa86c6182006-10-11 01:21:03 -07002695 brelse(path[i].p_bh);
2696 path[i].p_bh = NULL;
2697 i--;
2698 ext_debug("return to level %d\n", i);
2699 }
2700 }
2701
Aditya Kalid8990242011-09-09 19:18:51 -04002702 trace_ext4_ext_remove_space_done(inode, start, depth, partial_cluster,
2703 path->p_hdr->eh_entries);
2704
Aditya Kali7b415bf2011-09-09 19:04:51 -04002705 /* If we still have something in the partial cluster and we have removed
2706 * even the first extent, then we should free the blocks in the partial
2707 * cluster as well. */
2708 if (partial_cluster && path->p_hdr->eh_entries == 0) {
2709 int flags = EXT4_FREE_BLOCKS_FORGET;
2710
2711 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2712 flags |= EXT4_FREE_BLOCKS_METADATA;
2713
2714 ext4_free_blocks(handle, inode, NULL,
2715 EXT4_C2B(EXT4_SB(sb), partial_cluster),
2716 EXT4_SB(sb)->s_cluster_ratio, flags);
2717 partial_cluster = 0;
2718 }
2719
Alex Tomasa86c6182006-10-11 01:21:03 -07002720 /* TODO: flexible tree reduction should be here */
2721 if (path->p_hdr->eh_entries == 0) {
2722 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07002723 * truncate to zero freed all the tree,
2724 * so we need to correct eh_depth
Alex Tomasa86c6182006-10-11 01:21:03 -07002725 */
2726 err = ext4_ext_get_access(handle, inode, path);
2727 if (err == 0) {
2728 ext_inode_hdr(inode)->eh_depth = 0;
2729 ext_inode_hdr(inode)->eh_max =
Theodore Ts'o55ad63b2009-08-28 10:40:33 -04002730 cpu_to_le16(ext4_ext_space_root(inode, 0));
Alex Tomasa86c6182006-10-11 01:21:03 -07002731 err = ext4_ext_dirty(handle, inode, path);
2732 }
2733 }
2734out:
Alex Tomasa86c6182006-10-11 01:21:03 -07002735 ext4_ext_drop_refs(path);
2736 kfree(path);
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002737 if (err == -EAGAIN) {
2738 path = NULL;
Dmitry Monakhov0617b832010-05-17 01:00:00 -04002739 goto again;
Ashish Sangwan6918ff02012-07-22 22:49:08 -04002740 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002741 ext4_journal_stop(handle);
2742
2743 return err;
2744}
2745
2746/*
2747 * called at mount time
2748 */
2749void ext4_ext_init(struct super_block *sb)
2750{
2751 /*
2752 * possible initialization would be here
2753 */
2754
Theodore Ts'o83982b62009-01-06 14:53:16 -05002755 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
Theodore Ts'o90576c02009-09-29 15:51:30 -04002756#if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
Theodore Ts'o92b97812012-03-19 23:41:49 -04002757 printk(KERN_INFO "EXT4-fs: file extents enabled"
Robert P. J. Daybbf2f9f2007-02-17 19:20:16 +01002758#ifdef AGGRESSIVE_TEST
Theodore Ts'o92b97812012-03-19 23:41:49 -04002759 ", aggressive tests"
Alex Tomasa86c6182006-10-11 01:21:03 -07002760#endif
2761#ifdef CHECK_BINSEARCH
Theodore Ts'o92b97812012-03-19 23:41:49 -04002762 ", check binsearch"
Alex Tomasa86c6182006-10-11 01:21:03 -07002763#endif
2764#ifdef EXTENTS_STATS
Theodore Ts'o92b97812012-03-19 23:41:49 -04002765 ", stats"
Alex Tomasa86c6182006-10-11 01:21:03 -07002766#endif
Theodore Ts'o92b97812012-03-19 23:41:49 -04002767 "\n");
Theodore Ts'o90576c02009-09-29 15:51:30 -04002768#endif
Alex Tomasa86c6182006-10-11 01:21:03 -07002769#ifdef EXTENTS_STATS
2770 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
2771 EXT4_SB(sb)->s_ext_min = 1 << 30;
2772 EXT4_SB(sb)->s_ext_max = 0;
2773#endif
2774 }
2775}
2776
2777/*
2778 * called at umount time
2779 */
2780void ext4_ext_release(struct super_block *sb)
2781{
Theodore Ts'o83982b62009-01-06 14:53:16 -05002782 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
Alex Tomasa86c6182006-10-11 01:21:03 -07002783 return;
2784
2785#ifdef EXTENTS_STATS
2786 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
2787 struct ext4_sb_info *sbi = EXT4_SB(sb);
2788 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
2789 sbi->s_ext_blocks, sbi->s_ext_extents,
2790 sbi->s_ext_blocks / sbi->s_ext_extents);
2791 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
2792 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
2793 }
2794#endif
2795}
2796
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002797/* FIXME!! we need to try to merge to left or right after zero-out */
2798static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
2799{
Lukas Czerner24075182010-10-27 21:30:06 -04002800 ext4_fsblk_t ee_pblock;
2801 unsigned int ee_len;
Jing Zhangb7203032010-05-12 00:00:00 -04002802 int ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002803
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002804 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'obf89d162010-10-27 21:30:14 -04002805 ee_pblock = ext4_ext_pblock(ex);
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002806
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04002807 ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
Lukas Czerner24075182010-10-27 21:30:06 -04002808 if (ret > 0)
2809 ret = 0;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002810
Lukas Czerner24075182010-10-27 21:30:06 -04002811 return ret;
Aneesh Kumar K.V093a0882008-04-29 08:11:12 -04002812}
2813
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002814/*
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002815 * ext4_split_extent_at() splits an extent at given block.
2816 *
2817 * @handle: the journal handle
2818 * @inode: the file inode
2819 * @path: the path to the extent
2820 * @split: the logical block where the extent is splitted.
2821 * @split_flags: indicates if the extent could be zeroout if split fails, and
2822 * the states(init or uninit) of new extents.
2823 * @flags: flags used to insert new extent to extent tree.
2824 *
2825 *
2826 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
2827 * of which are deterimined by split_flag.
2828 *
2829 * There are two cases:
2830 * a> the extent are splitted into two extent.
2831 * b> split is not needed, and just mark the extent.
2832 *
2833 * return 0 on success.
2834 */
2835static int ext4_split_extent_at(handle_t *handle,
2836 struct inode *inode,
2837 struct ext4_ext_path *path,
2838 ext4_lblk_t split,
2839 int split_flag,
2840 int flags)
2841{
2842 ext4_fsblk_t newblock;
2843 ext4_lblk_t ee_block;
2844 struct ext4_extent *ex, newex, orig_ex;
2845 struct ext4_extent *ex2 = NULL;
2846 unsigned int ee_len, depth;
2847 int err = 0;
2848
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04002849 BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) ==
2850 (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2));
2851
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002852 ext_debug("ext4_split_extents_at: inode %lu, logical"
2853 "block %llu\n", inode->i_ino, (unsigned long long)split);
2854
2855 ext4_ext_show_leaf(inode, path);
2856
2857 depth = ext_depth(inode);
2858 ex = path[depth].p_ext;
2859 ee_block = le32_to_cpu(ex->ee_block);
2860 ee_len = ext4_ext_get_actual_len(ex);
2861 newblock = split - ee_block + ext4_ext_pblock(ex);
2862
2863 BUG_ON(split < ee_block || split >= (ee_block + ee_len));
2864
2865 err = ext4_ext_get_access(handle, inode, path + depth);
2866 if (err)
2867 goto out;
2868
2869 if (split == ee_block) {
2870 /*
2871 * case b: block @split is the block that the extent begins with
2872 * then we just change the state of the extent, and splitting
2873 * is not needed.
2874 */
2875 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2876 ext4_ext_mark_uninitialized(ex);
2877 else
2878 ext4_ext_mark_initialized(ex);
2879
2880 if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
2881 ext4_ext_try_to_merge(inode, path, ex);
2882
2883 err = ext4_ext_dirty(handle, inode, path + depth);
2884 goto out;
2885 }
2886
2887 /* case a */
2888 memcpy(&orig_ex, ex, sizeof(orig_ex));
2889 ex->ee_len = cpu_to_le16(split - ee_block);
2890 if (split_flag & EXT4_EXT_MARK_UNINIT1)
2891 ext4_ext_mark_uninitialized(ex);
2892
2893 /*
2894 * path may lead to new leaf, not to original leaf any more
2895 * after ext4_ext_insert_extent() returns,
2896 */
2897 err = ext4_ext_dirty(handle, inode, path + depth);
2898 if (err)
2899 goto fix_extent_len;
2900
2901 ex2 = &newex;
2902 ex2->ee_block = cpu_to_le32(split);
2903 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
2904 ext4_ext_store_pblock(ex2, newblock);
2905 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2906 ext4_ext_mark_uninitialized(ex2);
2907
2908 err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
2909 if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04002910 if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) {
2911 if (split_flag & EXT4_EXT_DATA_VALID1)
2912 err = ext4_ext_zeroout(inode, ex2);
2913 else
2914 err = ext4_ext_zeroout(inode, ex);
2915 } else
2916 err = ext4_ext_zeroout(inode, &orig_ex);
2917
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002918 if (err)
2919 goto fix_extent_len;
2920 /* update the extent length and mark as initialized */
Al Viroaf1584f2012-04-12 20:32:25 -04002921 ex->ee_len = cpu_to_le16(ee_len);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002922 ext4_ext_try_to_merge(inode, path, ex);
2923 err = ext4_ext_dirty(handle, inode, path + depth);
2924 goto out;
2925 } else if (err)
2926 goto fix_extent_len;
2927
2928out:
2929 ext4_ext_show_leaf(inode, path);
2930 return err;
2931
2932fix_extent_len:
2933 ex->ee_len = orig_ex.ee_len;
2934 ext4_ext_dirty(handle, inode, path + depth);
2935 return err;
2936}
2937
2938/*
2939 * ext4_split_extents() splits an extent and mark extent which is covered
2940 * by @map as split_flags indicates
2941 *
2942 * It may result in splitting the extent into multiple extents (upto three)
2943 * There are three possibilities:
2944 * a> There is no split required
2945 * b> Splits in two extents: Split is happening at either end of the extent
2946 * c> Splits in three extents: Somone is splitting in middle of the extent
2947 *
2948 */
2949static int ext4_split_extent(handle_t *handle,
2950 struct inode *inode,
2951 struct ext4_ext_path *path,
2952 struct ext4_map_blocks *map,
2953 int split_flag,
2954 int flags)
2955{
2956 ext4_lblk_t ee_block;
2957 struct ext4_extent *ex;
2958 unsigned int ee_len, depth;
2959 int err = 0;
2960 int uninitialized;
2961 int split_flag1, flags1;
2962
2963 depth = ext_depth(inode);
2964 ex = path[depth].p_ext;
2965 ee_block = le32_to_cpu(ex->ee_block);
2966 ee_len = ext4_ext_get_actual_len(ex);
2967 uninitialized = ext4_ext_is_uninitialized(ex);
2968
2969 if (map->m_lblk + map->m_len < ee_block + ee_len) {
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04002970 split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002971 flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
2972 if (uninitialized)
2973 split_flag1 |= EXT4_EXT_MARK_UNINIT1 |
2974 EXT4_EXT_MARK_UNINIT2;
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04002975 if (split_flag & EXT4_EXT_DATA_VALID2)
2976 split_flag1 |= EXT4_EXT_DATA_VALID1;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002977 err = ext4_split_extent_at(handle, inode, path,
2978 map->m_lblk + map->m_len, split_flag1, flags1);
Yongqiang Yang93917412011-05-22 20:49:12 -04002979 if (err)
2980 goto out;
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002981 }
2982
2983 ext4_ext_drop_refs(path);
2984 path = ext4_ext_find_extent(inode, map->m_lblk, path);
2985 if (IS_ERR(path))
2986 return PTR_ERR(path);
2987
2988 if (map->m_lblk >= ee_block) {
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04002989 split_flag1 = split_flag & (EXT4_EXT_MAY_ZEROOUT |
2990 EXT4_EXT_DATA_VALID2);
Yongqiang Yang47ea3bb2011-05-03 12:23:07 -04002991 if (uninitialized)
2992 split_flag1 |= EXT4_EXT_MARK_UNINIT1;
2993 if (split_flag & EXT4_EXT_MARK_UNINIT2)
2994 split_flag1 |= EXT4_EXT_MARK_UNINIT2;
2995 err = ext4_split_extent_at(handle, inode, path,
2996 map->m_lblk, split_flag1, flags);
2997 if (err)
2998 goto out;
2999 }
3000
3001 ext4_ext_show_leaf(inode, path);
3002out:
3003 return err ? err : map->m_len;
3004}
3005
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003006#define EXT4_EXT_ZERO_LEN 7
Amit Arora56055d32007-07-17 21:42:38 -04003007/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003008 * This function is called by ext4_ext_map_blocks() if someone tries to write
Amit Arora56055d32007-07-17 21:42:38 -04003009 * to an uninitialized extent. It may result in splitting the uninitialized
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003010 * extent into multiple extents (up to three - one initialized and two
Amit Arora56055d32007-07-17 21:42:38 -04003011 * uninitialized).
3012 * There are three possibilities:
3013 * a> There is no split required: Entire extent should be initialized
3014 * b> Splits in two extents: Write is happening at either end of the extent
3015 * c> Splits in three extents: Somone is writing in middle of the extent
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003016 *
3017 * Pre-conditions:
3018 * - The extent pointed to by 'path' is uninitialized.
3019 * - The extent pointed to by 'path' contains a superset
3020 * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
3021 *
3022 * Post-conditions on success:
3023 * - the returned value is the number of blocks beyond map->l_lblk
3024 * that are allocated and initialized.
3025 * It is guaranteed to be >= map->m_len.
Amit Arora56055d32007-07-17 21:42:38 -04003026 */
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003027static int ext4_ext_convert_to_initialized(handle_t *handle,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003028 struct inode *inode,
3029 struct ext4_map_blocks *map,
3030 struct ext4_ext_path *path)
Amit Arora56055d32007-07-17 21:42:38 -04003031{
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003032 struct ext4_extent_header *eh;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003033 struct ext4_map_blocks split_map;
3034 struct ext4_extent zero_ex;
3035 struct ext4_extent *ex;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003036 ext4_lblk_t ee_block, eof_block;
Dan Carpenterf85b2872011-10-26 03:42:36 -04003037 unsigned int ee_len, depth;
3038 int allocated;
Amit Arora56055d32007-07-17 21:42:38 -04003039 int err = 0;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003040 int split_flag = 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003041
3042 ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
3043 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003044 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003045
3046 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3047 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003048 if (eof_block < map->m_lblk + map->m_len)
3049 eof_block = map->m_lblk + map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003050
3051 depth = ext_depth(inode);
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003052 eh = path[depth].p_hdr;
Amit Arora56055d32007-07-17 21:42:38 -04003053 ex = path[depth].p_ext;
3054 ee_block = le32_to_cpu(ex->ee_block);
3055 ee_len = ext4_ext_get_actual_len(ex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003056 allocated = ee_len - (map->m_lblk - ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003057
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003058 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
3059
3060 /* Pre-conditions */
3061 BUG_ON(!ext4_ext_is_uninitialized(ex));
3062 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
Eric Gouriou6f91bc52011-10-27 11:43:23 -04003063
3064 /*
3065 * Attempt to transfer newly initialized blocks from the currently
3066 * uninitialized extent to its left neighbor. This is much cheaper
3067 * than an insertion followed by a merge as those involve costly
3068 * memmove() calls. This is the common case in steady state for
3069 * workloads doing fallocate(FALLOC_FL_KEEP_SIZE) followed by append
3070 * writes.
3071 *
3072 * Limitations of the current logic:
3073 * - L1: we only deal with writes at the start of the extent.
3074 * The approach could be extended to writes at the end
3075 * of the extent but this scenario was deemed less common.
3076 * - L2: we do not deal with writes covering the whole extent.
3077 * This would require removing the extent if the transfer
3078 * is possible.
3079 * - L3: we only attempt to merge with an extent stored in the
3080 * same extent tree node.
3081 */
3082 if ((map->m_lblk == ee_block) && /*L1*/
3083 (map->m_len < ee_len) && /*L2*/
3084 (ex > EXT_FIRST_EXTENT(eh))) { /*L3*/
3085 struct ext4_extent *prev_ex;
3086 ext4_lblk_t prev_lblk;
3087 ext4_fsblk_t prev_pblk, ee_pblk;
3088 unsigned int prev_len, write_len;
3089
3090 prev_ex = ex - 1;
3091 prev_lblk = le32_to_cpu(prev_ex->ee_block);
3092 prev_len = ext4_ext_get_actual_len(prev_ex);
3093 prev_pblk = ext4_ext_pblock(prev_ex);
3094 ee_pblk = ext4_ext_pblock(ex);
3095 write_len = map->m_len;
3096
3097 /*
3098 * A transfer of blocks from 'ex' to 'prev_ex' is allowed
3099 * upon those conditions:
3100 * - C1: prev_ex is initialized,
3101 * - C2: prev_ex is logically abutting ex,
3102 * - C3: prev_ex is physically abutting ex,
3103 * - C4: prev_ex can receive the additional blocks without
3104 * overflowing the (initialized) length limit.
3105 */
3106 if ((!ext4_ext_is_uninitialized(prev_ex)) && /*C1*/
3107 ((prev_lblk + prev_len) == ee_block) && /*C2*/
3108 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
3109 (prev_len < (EXT_INIT_MAX_LEN - write_len))) { /*C4*/
3110 err = ext4_ext_get_access(handle, inode, path + depth);
3111 if (err)
3112 goto out;
3113
3114 trace_ext4_ext_convert_to_initialized_fastpath(inode,
3115 map, ex, prev_ex);
3116
3117 /* Shift the start of ex by 'write_len' blocks */
3118 ex->ee_block = cpu_to_le32(ee_block + write_len);
3119 ext4_ext_store_pblock(ex, ee_pblk + write_len);
3120 ex->ee_len = cpu_to_le16(ee_len - write_len);
3121 ext4_ext_mark_uninitialized(ex); /* Restore the flag */
3122
3123 /* Extend prev_ex by 'write_len' blocks */
3124 prev_ex->ee_len = cpu_to_le16(prev_len + write_len);
3125
3126 /* Mark the block containing both extents as dirty */
3127 ext4_ext_dirty(handle, inode, path + depth);
3128
3129 /* Update path to point to the right extent */
3130 path[depth].p_ext = prev_ex;
3131
3132 /* Result: number of initialized blocks past m_lblk */
3133 allocated = write_len;
3134 goto out;
3135 }
3136 }
3137
Yongqiang Yang667eff32011-05-03 12:25:07 -04003138 WARN_ON(map->m_lblk < ee_block);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003139 /*
3140 * It is safe to convert extent to initialized via explicit
3141 * zeroout only if extent is fully insde i_size or new_size.
3142 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003143 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003144
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003145 /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003146 if (ee_len <= 2*EXT4_EXT_ZERO_LEN &&
3147 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3148 err = ext4_ext_zeroout(inode, ex);
Aneesh Kumar K.V3977c962008-04-17 10:38:59 -04003149 if (err)
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003150 goto out;
Aneesh Kumar K.Vd03856b2008-08-02 18:51:32 -04003151
3152 err = ext4_ext_get_access(handle, inode, path + depth);
3153 if (err)
3154 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003155 ext4_ext_mark_initialized(ex);
3156 ext4_ext_try_to_merge(inode, path, ex);
3157 err = ext4_ext_dirty(handle, inode, path + depth);
3158 goto out;
Amit Arora56055d32007-07-17 21:42:38 -04003159 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003160
Amit Arora56055d32007-07-17 21:42:38 -04003161 /*
Yongqiang Yang667eff32011-05-03 12:25:07 -04003162 * four cases:
3163 * 1. split the extent into three extents.
3164 * 2. split the extent into two extents, zeroout the first half.
3165 * 3. split the extent into two extents, zeroout the second half.
3166 * 4. split the extent into two extents with out zeroout.
Amit Arora56055d32007-07-17 21:42:38 -04003167 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003168 split_map.m_lblk = map->m_lblk;
3169 split_map.m_len = map->m_len;
3170
3171 if (allocated > map->m_len) {
3172 if (allocated <= EXT4_EXT_ZERO_LEN &&
3173 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3174 /* case 3 */
3175 zero_ex.ee_block =
Allison Henderson9b940f82011-05-16 10:11:09 -04003176 cpu_to_le32(map->m_lblk);
3177 zero_ex.ee_len = cpu_to_le16(allocated);
Yongqiang Yang667eff32011-05-03 12:25:07 -04003178 ext4_ext_store_pblock(&zero_ex,
3179 ext4_ext_pblock(ex) + map->m_lblk - ee_block);
3180 err = ext4_ext_zeroout(inode, &zero_ex);
Amit Arora56055d32007-07-17 21:42:38 -04003181 if (err)
3182 goto out;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003183 split_map.m_lblk = map->m_lblk;
3184 split_map.m_len = allocated;
3185 } else if ((map->m_lblk - ee_block + map->m_len <
3186 EXT4_EXT_ZERO_LEN) &&
3187 (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
3188 /* case 2 */
3189 if (map->m_lblk != ee_block) {
3190 zero_ex.ee_block = ex->ee_block;
3191 zero_ex.ee_len = cpu_to_le16(map->m_lblk -
3192 ee_block);
3193 ext4_ext_store_pblock(&zero_ex,
3194 ext4_ext_pblock(ex));
3195 err = ext4_ext_zeroout(inode, &zero_ex);
3196 if (err)
3197 goto out;
3198 }
3199
Yongqiang Yang667eff32011-05-03 12:25:07 -04003200 split_map.m_lblk = ee_block;
Allison Henderson9b940f82011-05-16 10:11:09 -04003201 split_map.m_len = map->m_lblk - ee_block + map->m_len;
3202 allocated = map->m_len;
Amit Arora56055d32007-07-17 21:42:38 -04003203 }
3204 }
Yongqiang Yang667eff32011-05-03 12:25:07 -04003205
3206 allocated = ext4_split_extent(handle, inode, path,
3207 &split_map, split_flag, 0);
3208 if (allocated < 0)
3209 err = allocated;
3210
Amit Arora56055d32007-07-17 21:42:38 -04003211out:
3212 return err ? err : allocated;
3213}
3214
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003215/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003216 * This function is called by ext4_ext_map_blocks() from
Mingming Cao00314622009-09-28 15:49:08 -04003217 * ext4_get_blocks_dio_write() when DIO to write
3218 * to an uninitialized extent.
3219 *
Paul Bollefd018fe2011-02-15 00:05:43 +01003220 * Writing to an uninitialized extent may result in splitting the uninitialized
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003221 * extent into multiple /initialized uninitialized extents (up to three)
Mingming Cao00314622009-09-28 15:49:08 -04003222 * There are three possibilities:
3223 * a> There is no split required: Entire extent should be uninitialized
3224 * b> Splits in two extents: Write is happening at either end of the extent
3225 * c> Splits in three extents: Somone is writing in middle of the extent
3226 *
3227 * One of more index blocks maybe needed if the extent tree grow after
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003228 * the uninitialized extent split. To prevent ENOSPC occur at the IO
Mingming Cao00314622009-09-28 15:49:08 -04003229 * complete, we need to split the uninitialized extent before DIO submit
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02003230 * the IO. The uninitialized extent called at this time will be split
Mingming Cao00314622009-09-28 15:49:08 -04003231 * into three uninitialized extent(at most). After IO complete, the part
3232 * being filled will be convert to initialized by the end_io callback function
3233 * via ext4_convert_unwritten_extents().
Mingmingba230c32009-11-06 04:01:23 -05003234 *
3235 * Returns the size of uninitialized extent to be written on success.
Mingming Cao00314622009-09-28 15:49:08 -04003236 */
3237static int ext4_split_unwritten_extents(handle_t *handle,
3238 struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003239 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003240 struct ext4_ext_path *path,
Mingming Cao00314622009-09-28 15:49:08 -04003241 int flags)
3242{
Yongqiang Yang667eff32011-05-03 12:25:07 -04003243 ext4_lblk_t eof_block;
3244 ext4_lblk_t ee_block;
3245 struct ext4_extent *ex;
3246 unsigned int ee_len;
3247 int split_flag = 0, depth;
Mingming Cao00314622009-09-28 15:49:08 -04003248
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003249 ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
3250 "block %llu, max_blocks %u\n", inode->i_ino,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003251 (unsigned long long)map->m_lblk, map->m_len);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003252
3253 eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
3254 inode->i_sb->s_blocksize_bits;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003255 if (eof_block < map->m_lblk + map->m_len)
3256 eof_block = map->m_lblk + map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003257 /*
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003258 * It is safe to convert extent to initialized via explicit
3259 * zeroout only if extent is fully insde i_size or new_size.
3260 */
Yongqiang Yang667eff32011-05-03 12:25:07 -04003261 depth = ext_depth(inode);
3262 ex = path[depth].p_ext;
3263 ee_block = le32_to_cpu(ex->ee_block);
3264 ee_len = ext4_ext_get_actual_len(ex);
Dmitry Monakhov21ca0872010-05-16 06:00:00 -04003265
Yongqiang Yang667eff32011-05-03 12:25:07 -04003266 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
3267 split_flag |= EXT4_EXT_MARK_UNINIT2;
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04003268 if (flags & EXT4_GET_BLOCKS_CONVERT)
3269 split_flag |= EXT4_EXT_DATA_VALID2;
Yongqiang Yang667eff32011-05-03 12:25:07 -04003270 flags |= EXT4_GET_BLOCKS_PRE_IO;
3271 return ext4_split_extent(handle, inode, path, map, split_flag, flags);
Mingming Cao00314622009-09-28 15:49:08 -04003272}
Yongqiang Yang197217a2011-05-03 11:45:29 -04003273
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003274static int ext4_convert_unwritten_extents_endio(handle_t *handle,
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04003275 struct inode *inode,
3276 struct ext4_map_blocks *map,
3277 struct ext4_ext_path *path)
Mingming Cao00314622009-09-28 15:49:08 -04003278{
3279 struct ext4_extent *ex;
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04003280 ext4_lblk_t ee_block;
3281 unsigned int ee_len;
Mingming Cao00314622009-09-28 15:49:08 -04003282 int depth;
3283 int err = 0;
Mingming Cao00314622009-09-28 15:49:08 -04003284
3285 depth = ext_depth(inode);
Mingming Cao00314622009-09-28 15:49:08 -04003286 ex = path[depth].p_ext;
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04003287 ee_block = le32_to_cpu(ex->ee_block);
3288 ee_len = ext4_ext_get_actual_len(ex);
Mingming Cao00314622009-09-28 15:49:08 -04003289
Yongqiang Yang197217a2011-05-03 11:45:29 -04003290 ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
3291 "block %llu, max_blocks %u\n", inode->i_ino,
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04003292 (unsigned long long)ee_block, ee_len);
3293
3294 /* If extent is larger than requested then split is required */
3295 if (ee_block != map->m_lblk || ee_len > map->m_len) {
3296 err = ext4_split_unwritten_extents(handle, inode, map, path,
3297 EXT4_GET_BLOCKS_CONVERT);
3298 if (err < 0)
3299 goto out;
3300 ext4_ext_drop_refs(path);
3301 path = ext4_ext_find_extent(inode, map->m_lblk, path);
3302 if (IS_ERR(path)) {
3303 err = PTR_ERR(path);
3304 goto out;
3305 }
3306 depth = ext_depth(inode);
3307 ex = path[depth].p_ext;
3308 }
Yongqiang Yang197217a2011-05-03 11:45:29 -04003309
Mingming Cao00314622009-09-28 15:49:08 -04003310 err = ext4_ext_get_access(handle, inode, path + depth);
3311 if (err)
3312 goto out;
3313 /* first mark the extent as initialized */
3314 ext4_ext_mark_initialized(ex);
3315
Yongqiang Yang197217a2011-05-03 11:45:29 -04003316 /* note: ext4_ext_correct_indexes() isn't needed here because
3317 * borders are not changed
Mingming Cao00314622009-09-28 15:49:08 -04003318 */
Yongqiang Yang197217a2011-05-03 11:45:29 -04003319 ext4_ext_try_to_merge(inode, path, ex);
3320
Mingming Cao00314622009-09-28 15:49:08 -04003321 /* Mark modified extent as dirty */
3322 err = ext4_ext_dirty(handle, inode, path + depth);
3323out:
3324 ext4_ext_show_leaf(inode, path);
3325 return err;
3326}
3327
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003328static void unmap_underlying_metadata_blocks(struct block_device *bdev,
3329 sector_t block, int count)
3330{
3331 int i;
3332 for (i = 0; i < count; i++)
3333 unmap_underlying_metadata(bdev, block + i);
3334}
3335
Theodore Ts'o58590b02010-10-27 21:23:12 -04003336/*
3337 * Handle EOFBLOCKS_FL flag, clearing it if necessary
3338 */
3339static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
Eric Sandeend002ebf2011-01-10 13:03:35 -05003340 ext4_lblk_t lblk,
Theodore Ts'o58590b02010-10-27 21:23:12 -04003341 struct ext4_ext_path *path,
3342 unsigned int len)
3343{
3344 int i, depth;
3345 struct ext4_extent_header *eh;
Sergey Senozhatsky65922cb2011-03-23 14:08:27 -04003346 struct ext4_extent *last_ex;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003347
3348 if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3349 return 0;
3350
3351 depth = ext_depth(inode);
3352 eh = path[depth].p_hdr;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003353
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003354 /*
3355 * We're going to remove EOFBLOCKS_FL entirely in future so we
3356 * do not care for this case anymore. Simply remove the flag
3357 * if there are no extents.
3358 */
3359 if (unlikely(!eh->eh_entries))
3360 goto out;
Theodore Ts'o58590b02010-10-27 21:23:12 -04003361 last_ex = EXT_LAST_EXTENT(eh);
3362 /*
3363 * We should clear the EOFBLOCKS_FL flag if we are writing the
3364 * last block in the last extent in the file. We test this by
3365 * first checking to see if the caller to
3366 * ext4_ext_get_blocks() was interested in the last block (or
3367 * a block beyond the last block) in the current extent. If
3368 * this turns out to be false, we can bail out from this
3369 * function immediately.
3370 */
Eric Sandeend002ebf2011-01-10 13:03:35 -05003371 if (lblk + len < le32_to_cpu(last_ex->ee_block) +
Theodore Ts'o58590b02010-10-27 21:23:12 -04003372 ext4_ext_get_actual_len(last_ex))
3373 return 0;
3374 /*
3375 * If the caller does appear to be planning to write at or
3376 * beyond the end of the current extent, we then test to see
3377 * if the current extent is the last extent in the file, by
3378 * checking to make sure it was reached via the rightmost node
3379 * at each level of the tree.
3380 */
3381 for (i = depth-1; i >= 0; i--)
3382 if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
3383 return 0;
Lukas Czernerafcff5d2012-03-21 21:47:55 -04003384out:
Theodore Ts'o58590b02010-10-27 21:23:12 -04003385 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3386 return ext4_mark_inode_dirty(handle, inode);
3387}
3388
Aditya Kali7b415bf2011-09-09 19:04:51 -04003389/**
3390 * ext4_find_delalloc_range: find delayed allocated block in the given range.
3391 *
3392 * Goes through the buffer heads in the range [lblk_start, lblk_end] and returns
3393 * whether there are any buffers marked for delayed allocation. It returns '1'
3394 * on the first delalloc'ed buffer head found. If no buffer head in the given
3395 * range is marked for delalloc, it returns 0.
3396 * lblk_start should always be <= lblk_end.
3397 * search_hint_reverse is to indicate that searching in reverse from lblk_end to
3398 * lblk_start might be more efficient (i.e., we will likely hit the delalloc'ed
3399 * block sooner). This is useful when blocks are truncated sequentially from
3400 * lblk_start towards lblk_end.
3401 */
3402static int ext4_find_delalloc_range(struct inode *inode,
3403 ext4_lblk_t lblk_start,
3404 ext4_lblk_t lblk_end,
3405 int search_hint_reverse)
3406{
3407 struct address_space *mapping = inode->i_mapping;
3408 struct buffer_head *head, *bh = NULL;
3409 struct page *page;
3410 ext4_lblk_t i, pg_lblk;
3411 pgoff_t index;
3412
Robin Dong8c48f7e2011-12-18 23:05:43 -05003413 if (!test_opt(inode->i_sb, DELALLOC))
3414 return 0;
3415
Aditya Kali7b415bf2011-09-09 19:04:51 -04003416 /* reverse search wont work if fs block size is less than page size */
3417 if (inode->i_blkbits < PAGE_CACHE_SHIFT)
3418 search_hint_reverse = 0;
3419
3420 if (search_hint_reverse)
3421 i = lblk_end;
3422 else
3423 i = lblk_start;
3424
3425 index = i >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
3426
3427 while ((i >= lblk_start) && (i <= lblk_end)) {
3428 page = find_get_page(mapping, index);
Aditya Kali5356f262011-09-09 19:20:51 -04003429 if (!page)
Aditya Kali7b415bf2011-09-09 19:04:51 -04003430 goto nextpage;
3431
Aditya Kali7b415bf2011-09-09 19:04:51 -04003432 if (!page_has_buffers(page))
3433 goto nextpage;
3434
3435 head = page_buffers(page);
3436 if (!head)
3437 goto nextpage;
3438
3439 bh = head;
3440 pg_lblk = index << (PAGE_CACHE_SHIFT -
3441 inode->i_blkbits);
3442 do {
3443 if (unlikely(pg_lblk < lblk_start)) {
3444 /*
3445 * This is possible when fs block size is less
3446 * than page size and our cluster starts/ends in
3447 * middle of the page. So we need to skip the
3448 * initial few blocks till we reach the 'lblk'
3449 */
3450 pg_lblk++;
3451 continue;
3452 }
3453
Aditya Kali5356f262011-09-09 19:20:51 -04003454 /* Check if the buffer is delayed allocated and that it
3455 * is not yet mapped. (when da-buffers are mapped during
3456 * their writeout, their da_mapped bit is set.)
3457 */
3458 if (buffer_delay(bh) && !buffer_da_mapped(bh)) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04003459 page_cache_release(page);
Aditya Kalid8990242011-09-09 19:18:51 -04003460 trace_ext4_find_delalloc_range(inode,
3461 lblk_start, lblk_end,
3462 search_hint_reverse,
3463 1, i);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003464 return 1;
3465 }
3466 if (search_hint_reverse)
3467 i--;
3468 else
3469 i++;
3470 } while ((i >= lblk_start) && (i <= lblk_end) &&
3471 ((bh = bh->b_this_page) != head));
3472nextpage:
3473 if (page)
3474 page_cache_release(page);
3475 /*
3476 * Move to next page. 'i' will be the first lblk in the next
3477 * page.
3478 */
3479 if (search_hint_reverse)
3480 index--;
3481 else
3482 index++;
3483 i = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
3484 }
3485
Aditya Kalid8990242011-09-09 19:18:51 -04003486 trace_ext4_find_delalloc_range(inode, lblk_start, lblk_end,
3487 search_hint_reverse, 0, 0);
Aditya Kali7b415bf2011-09-09 19:04:51 -04003488 return 0;
3489}
3490
3491int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk,
3492 int search_hint_reverse)
3493{
3494 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3495 ext4_lblk_t lblk_start, lblk_end;
3496 lblk_start = lblk & (~(sbi->s_cluster_ratio - 1));
3497 lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
3498
3499 return ext4_find_delalloc_range(inode, lblk_start, lblk_end,
3500 search_hint_reverse);
3501}
3502
3503/**
3504 * Determines how many complete clusters (out of those specified by the 'map')
3505 * are under delalloc and were reserved quota for.
3506 * This function is called when we are writing out the blocks that were
3507 * originally written with their allocation delayed, but then the space was
3508 * allocated using fallocate() before the delayed allocation could be resolved.
3509 * The cases to look for are:
3510 * ('=' indicated delayed allocated blocks
3511 * '-' indicates non-delayed allocated blocks)
3512 * (a) partial clusters towards beginning and/or end outside of allocated range
3513 * are not delalloc'ed.
3514 * Ex:
3515 * |----c---=|====c====|====c====|===-c----|
3516 * |++++++ allocated ++++++|
3517 * ==> 4 complete clusters in above example
3518 *
3519 * (b) partial cluster (outside of allocated range) towards either end is
3520 * marked for delayed allocation. In this case, we will exclude that
3521 * cluster.
3522 * Ex:
3523 * |----====c========|========c========|
3524 * |++++++ allocated ++++++|
3525 * ==> 1 complete clusters in above example
3526 *
3527 * Ex:
3528 * |================c================|
3529 * |++++++ allocated ++++++|
3530 * ==> 0 complete clusters in above example
3531 *
3532 * The ext4_da_update_reserve_space will be called only if we
3533 * determine here that there were some "entire" clusters that span
3534 * this 'allocated' range.
3535 * In the non-bigalloc case, this function will just end up returning num_blks
3536 * without ever calling ext4_find_delalloc_range.
3537 */
3538static unsigned int
3539get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start,
3540 unsigned int num_blks)
3541{
3542 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3543 ext4_lblk_t alloc_cluster_start, alloc_cluster_end;
3544 ext4_lblk_t lblk_from, lblk_to, c_offset;
3545 unsigned int allocated_clusters = 0;
3546
3547 alloc_cluster_start = EXT4_B2C(sbi, lblk_start);
3548 alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1);
3549
3550 /* max possible clusters for this allocation */
3551 allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1;
3552
Aditya Kalid8990242011-09-09 19:18:51 -04003553 trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks);
3554
Aditya Kali7b415bf2011-09-09 19:04:51 -04003555 /* Check towards left side */
3556 c_offset = lblk_start & (sbi->s_cluster_ratio - 1);
3557 if (c_offset) {
3558 lblk_from = lblk_start & (~(sbi->s_cluster_ratio - 1));
3559 lblk_to = lblk_from + c_offset - 1;
3560
3561 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
3562 allocated_clusters--;
3563 }
3564
3565 /* Now check towards right. */
3566 c_offset = (lblk_start + num_blks) & (sbi->s_cluster_ratio - 1);
3567 if (allocated_clusters && c_offset) {
3568 lblk_from = lblk_start + num_blks;
3569 lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1;
3570
3571 if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0))
3572 allocated_clusters--;
3573 }
3574
3575 return allocated_clusters;
3576}
3577
Mingming Cao00314622009-09-28 15:49:08 -04003578static int
3579ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003580 struct ext4_map_blocks *map,
Mingming Cao00314622009-09-28 15:49:08 -04003581 struct ext4_ext_path *path, int flags,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003582 unsigned int allocated, ext4_fsblk_t newblock)
Mingming Cao00314622009-09-28 15:49:08 -04003583{
3584 int ret = 0;
3585 int err = 0;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003586 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Mingming Cao00314622009-09-28 15:49:08 -04003587
Zheng Liu88635ca2011-12-28 19:00:25 -05003588 ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical "
3589 "block %llu, max_blocks %u, flags %x, allocated %u\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003590 inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
Mingming Cao00314622009-09-28 15:49:08 -04003591 flags, allocated);
3592 ext4_ext_show_leaf(inode, path);
3593
Aditya Kalid8990242011-09-09 19:18:51 -04003594 trace_ext4_ext_handle_uninitialized_extents(inode, map, allocated,
3595 newblock);
3596
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003597 /* get_block() before submit the IO, split the extent */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003598 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003599 ret = ext4_split_unwritten_extents(handle, inode, map,
3600 path, flags);
Mingming5f524952009-11-10 10:48:04 -05003601 /*
3602 * Flag the inode(non aio case) or end_io struct (aio case)
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003603 * that this IO needs to conversion to written when IO is
Mingming5f524952009-11-10 10:48:04 -05003604 * completed
3605 */
Tao Ma0edeb712011-10-31 17:30:44 -04003606 if (io)
3607 ext4_set_io_unwritten_flag(inode, io);
3608 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003609 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
Jiaying Zhang744692d2010-03-04 16:14:02 -05003610 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003611 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao00314622009-09-28 15:49:08 -04003612 goto out;
3613 }
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003614 /* IO end_io complete, convert the filled extent to written */
Jiaying Zhang744692d2010-03-04 16:14:02 -05003615 if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
Dmitry Monakhov4319fd02012-10-10 01:04:58 -04003616 ret = ext4_convert_unwritten_extents_endio(handle, inode, map,
Mingming Cao00314622009-09-28 15:49:08 -04003617 path);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003618 if (ret >= 0) {
Jan Karab436b9b2009-12-08 23:51:10 -05003619 ext4_update_inode_fsync_trans(handle, inode, 1);
Eric Sandeend002ebf2011-01-10 13:03:35 -05003620 err = check_eofblocks_fl(handle, inode, map->m_lblk,
3621 path, map->m_len);
Theodore Ts'o58590b02010-10-27 21:23:12 -04003622 } else
3623 err = ret;
Mingming Cao00314622009-09-28 15:49:08 -04003624 goto out2;
3625 }
3626 /* buffered IO case */
3627 /*
3628 * repeat fallocate creation request
3629 * we already have an unwritten extent
3630 */
3631 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT)
3632 goto map_out;
3633
3634 /* buffered READ or buffered write_begin() lookup */
3635 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
3636 /*
3637 * We have blocks reserved already. We
3638 * return allocated blocks so that delalloc
3639 * won't do block reservation for us. But
3640 * the buffer head will be unmapped so that
3641 * a read from the block returns 0s.
3642 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003643 map->m_flags |= EXT4_MAP_UNWRITTEN;
Mingming Cao00314622009-09-28 15:49:08 -04003644 goto out1;
3645 }
3646
3647 /* buffered write, writepage time, convert*/
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003648 ret = ext4_ext_convert_to_initialized(handle, inode, map, path);
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04003649 if (ret >= 0)
Jan Karab436b9b2009-12-08 23:51:10 -05003650 ext4_update_inode_fsync_trans(handle, inode, 1);
Mingming Cao00314622009-09-28 15:49:08 -04003651out:
3652 if (ret <= 0) {
3653 err = ret;
3654 goto out2;
3655 } else
3656 allocated = ret;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003657 map->m_flags |= EXT4_MAP_NEW;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003658 /*
3659 * if we allocated more blocks than requested
3660 * we need to make sure we unmap the extra block
3661 * allocated. The actual needed block will get
3662 * unmapped later when we find the buffer_head marked
3663 * new.
3664 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003665 if (allocated > map->m_len) {
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003666 unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003667 newblock + map->m_len,
3668 allocated - map->m_len);
3669 allocated = map->m_len;
Aneesh Kumar K.V515f41c2009-12-29 23:39:06 -05003670 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003671
3672 /*
3673 * If we have done fallocate with the offset that is already
3674 * delayed allocated, we would have block reservation
3675 * and quota reservation done in the delayed write path.
3676 * But fallocate would have already updated quota and block
3677 * count for this offset. So cancel these reservation
3678 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003679 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
3680 unsigned int reserved_clusters;
3681 reserved_clusters = get_reserved_cluster_alloc(inode,
3682 map->m_lblk, map->m_len);
3683 if (reserved_clusters)
3684 ext4_da_update_reserve_space(inode,
3685 reserved_clusters,
3686 0);
3687 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05003688
Mingming Cao00314622009-09-28 15:49:08 -04003689map_out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003690 map->m_flags |= EXT4_MAP_MAPPED;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04003691 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
3692 err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
3693 map->m_len);
3694 if (err < 0)
3695 goto out2;
3696 }
Mingming Cao00314622009-09-28 15:49:08 -04003697out1:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003698 if (allocated > map->m_len)
3699 allocated = map->m_len;
Mingming Cao00314622009-09-28 15:49:08 -04003700 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003701 map->m_pblk = newblock;
3702 map->m_len = allocated;
Mingming Cao00314622009-09-28 15:49:08 -04003703out2:
3704 if (path) {
3705 ext4_ext_drop_refs(path);
3706 kfree(path);
3707 }
3708 return err ? err : allocated;
3709}
Theodore Ts'o58590b02010-10-27 21:23:12 -04003710
Mingming Cao00314622009-09-28 15:49:08 -04003711/*
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003712 * get_implied_cluster_alloc - check to see if the requested
3713 * allocation (in the map structure) overlaps with a cluster already
3714 * allocated in an extent.
Aditya Kalid8990242011-09-09 19:18:51 -04003715 * @sb The filesystem superblock structure
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003716 * @map The requested lblk->pblk mapping
3717 * @ex The extent structure which might contain an implied
3718 * cluster allocation
3719 *
3720 * This function is called by ext4_ext_map_blocks() after we failed to
3721 * find blocks that were already in the inode's extent tree. Hence,
3722 * we know that the beginning of the requested region cannot overlap
3723 * the extent from the inode's extent tree. There are three cases we
3724 * want to catch. The first is this case:
3725 *
3726 * |--- cluster # N--|
3727 * |--- extent ---| |---- requested region ---|
3728 * |==========|
3729 *
3730 * The second case that we need to test for is this one:
3731 *
3732 * |--------- cluster # N ----------------|
3733 * |--- requested region --| |------- extent ----|
3734 * |=======================|
3735 *
3736 * The third case is when the requested region lies between two extents
3737 * within the same cluster:
3738 * |------------- cluster # N-------------|
3739 * |----- ex -----| |---- ex_right ----|
3740 * |------ requested region ------|
3741 * |================|
3742 *
3743 * In each of the above cases, we need to set the map->m_pblk and
3744 * map->m_len so it corresponds to the return the extent labelled as
3745 * "|====|" from cluster #N, since it is already in use for data in
3746 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
3747 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
3748 * as a new "allocated" block region. Otherwise, we will return 0 and
3749 * ext4_ext_map_blocks() will then allocate one or more new clusters
3750 * by calling ext4_mb_new_blocks().
3751 */
Aditya Kalid8990242011-09-09 19:18:51 -04003752static int get_implied_cluster_alloc(struct super_block *sb,
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003753 struct ext4_map_blocks *map,
3754 struct ext4_extent *ex,
3755 struct ext4_ext_path *path)
3756{
Aditya Kalid8990242011-09-09 19:18:51 -04003757 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003758 ext4_lblk_t c_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
3759 ext4_lblk_t ex_cluster_start, ex_cluster_end;
Curt Wohlgemuth14d7f3e2011-12-18 17:39:02 -05003760 ext4_lblk_t rr_cluster_start;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003761 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
3762 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
3763 unsigned short ee_len = ext4_ext_get_actual_len(ex);
3764
3765 /* The extent passed in that we are trying to match */
3766 ex_cluster_start = EXT4_B2C(sbi, ee_block);
3767 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
3768
3769 /* The requested region passed into ext4_map_blocks() */
3770 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003771
3772 if ((rr_cluster_start == ex_cluster_end) ||
3773 (rr_cluster_start == ex_cluster_start)) {
3774 if (rr_cluster_start == ex_cluster_end)
3775 ee_start += ee_len - 1;
3776 map->m_pblk = (ee_start & ~(sbi->s_cluster_ratio - 1)) +
3777 c_offset;
3778 map->m_len = min(map->m_len,
3779 (unsigned) sbi->s_cluster_ratio - c_offset);
3780 /*
3781 * Check for and handle this case:
3782 *
3783 * |--------- cluster # N-------------|
3784 * |------- extent ----|
3785 * |--- requested region ---|
3786 * |===========|
3787 */
3788
3789 if (map->m_lblk < ee_block)
3790 map->m_len = min(map->m_len, ee_block - map->m_lblk);
3791
3792 /*
3793 * Check for the case where there is already another allocated
3794 * block to the right of 'ex' but before the end of the cluster.
3795 *
3796 * |------------- cluster # N-------------|
3797 * |----- ex -----| |---- ex_right ----|
3798 * |------ requested region ------|
3799 * |================|
3800 */
3801 if (map->m_lblk > ee_block) {
3802 ext4_lblk_t next = ext4_ext_next_allocated_block(path);
3803 map->m_len = min(map->m_len, next - map->m_lblk);
3804 }
Aditya Kalid8990242011-09-09 19:18:51 -04003805
3806 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003807 return 1;
3808 }
Aditya Kalid8990242011-09-09 19:18:51 -04003809
3810 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003811 return 0;
3812}
3813
3814
3815/*
Mingming Caof5ab0d12008-02-25 15:29:55 -05003816 * Block allocation/map/preallocation routine for extents based files
3817 *
3818 *
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003819 * Need to be called with
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05003820 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
3821 * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
Mingming Caof5ab0d12008-02-25 15:29:55 -05003822 *
3823 * return > 0, number of of blocks already mapped/allocated
3824 * if create == 0 and these are pre-allocated blocks
3825 * buffer head is unmapped
3826 * otherwise blocks are mapped
3827 *
3828 * return = 0, if plain look up failed (blocks have not been allocated)
3829 * buffer head is unmapped
3830 *
3831 * return < 0, error case.
Aneesh Kumar K.Vc278bfe2008-01-28 23:58:27 -05003832 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003833int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3834 struct ext4_map_blocks *map, int flags)
Alex Tomasa86c6182006-10-11 01:21:03 -07003835{
3836 struct ext4_ext_path *path = NULL;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003837 struct ext4_extent newex, *ex, *ex2;
3838 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003839 ext4_fsblk_t newblock = 0;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003840 int free_on_err = 0, err = 0, depth, ret;
3841 unsigned int allocated = 0, offset = 0;
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04003842 unsigned int allocated_clusters = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003843 struct ext4_allocation_request ar;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003844 ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003845 ext4_lblk_t cluster_offset;
Alex Tomasa86c6182006-10-11 01:21:03 -07003846
Mingming84fe3be2009-09-01 08:44:37 -04003847 ext_debug("blocks %u/%u requested for inode %lu\n",
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003848 map->m_lblk, map->m_len, inode->i_ino);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003849 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07003850
3851 /* check in cache */
Lukas Czerner78771912012-03-19 23:05:43 -04003852 if (ext4_ext_in_cache(inode, map->m_lblk, &newex)) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003853 if (!newex.ee_start_lo && !newex.ee_start_hi) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04003854 if ((sbi->s_cluster_ratio > 1) &&
3855 ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
3856 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3857
Theodore Ts'oc2177052009-05-14 00:58:52 -04003858 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003859 /*
3860 * block isn't allocated yet and
3861 * user doesn't want to allocate it
3862 */
Alex Tomasa86c6182006-10-11 01:21:03 -07003863 goto out2;
3864 }
3865 /* we should allocate requested block */
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05003866 } else {
Alex Tomasa86c6182006-10-11 01:21:03 -07003867 /* block is already allocated */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003868 if (sbi->s_cluster_ratio > 1)
3869 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003870 newblock = map->m_lblk
Dave Kleikamp8c55e202007-05-24 13:04:54 -04003871 - le32_to_cpu(newex.ee_block)
Theodore Ts'obf89d162010-10-27 21:30:14 -04003872 + ext4_ext_pblock(&newex);
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003873 /* number of remaining blocks in the extent */
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05003874 allocated = ext4_ext_get_actual_len(&newex) -
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003875 (map->m_lblk - le32_to_cpu(newex.ee_block));
Alex Tomasa86c6182006-10-11 01:21:03 -07003876 goto out;
Alex Tomasa86c6182006-10-11 01:21:03 -07003877 }
3878 }
3879
3880 /* find extent for this block */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003881 path = ext4_ext_find_extent(inode, map->m_lblk, NULL);
Alex Tomasa86c6182006-10-11 01:21:03 -07003882 if (IS_ERR(path)) {
3883 err = PTR_ERR(path);
3884 path = NULL;
3885 goto out2;
3886 }
3887
3888 depth = ext_depth(inode);
3889
3890 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003891 * consistent leaf must not be empty;
3892 * this situation is possible, though, _during_ tree modification;
Alex Tomasa86c6182006-10-11 01:21:03 -07003893 * this is why assert can't be put in ext4_ext_find_extent()
3894 */
Frank Mayhar273df552010-03-02 11:46:09 -05003895 if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
3896 EXT4_ERROR_INODE(inode, "bad extent address "
Theodore Ts'of70f3622010-05-16 23:00:00 -04003897 "lblock: %lu, depth: %d pblock %lld",
3898 (unsigned long) map->m_lblk, depth,
3899 path[depth].p_block);
Surbhi Palande034fb4c2009-12-14 09:53:52 -05003900 err = -EIO;
3901 goto out2;
3902 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003903
Avantika Mathur7e028972006-12-06 20:41:33 -08003904 ex = path[depth].p_ext;
3905 if (ex) {
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003906 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
Theodore Ts'obf89d162010-10-27 21:30:14 -04003907 ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
Amit Aroraa2df2a62007-07-17 21:42:41 -04003908 unsigned short ee_len;
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003909
3910 /*
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003911 * Uninitialized extents are treated as holes, except that
Amit Arora56055d32007-07-17 21:42:38 -04003912 * we split out initialized portions during a write.
Suparna Bhattacharya471d4012006-10-11 01:21:06 -07003913 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04003914 ee_len = ext4_ext_get_actual_len(ex);
Aditya Kalid8990242011-09-09 19:18:51 -04003915
3916 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
3917
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003918 /* if found extent covers block, simply return it */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003919 if (in_range(map->m_lblk, ee_block, ee_len)) {
3920 newblock = map->m_lblk - ee_block + ee_start;
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003921 /* number of remaining blocks in the extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003922 allocated = ee_len - (map->m_lblk - ee_block);
3923 ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
3924 ee_block, ee_len, newblock);
Amit Arora56055d32007-07-17 21:42:38 -04003925
Allison Hendersone8613042011-05-25 07:41:46 -04003926 /*
Lukas Czerner78771912012-03-19 23:05:43 -04003927 * Do not put uninitialized extent
3928 * in the cache
Allison Hendersone8613042011-05-25 07:41:46 -04003929 */
Lukas Czerner78771912012-03-19 23:05:43 -04003930 if (!ext4_ext_is_uninitialized(ex)) {
3931 ext4_ext_put_in_cache(inode, ee_block,
3932 ee_len, ee_start);
3933 goto out;
Allison Hendersone8613042011-05-25 07:41:46 -04003934 }
Lukas Czerner78771912012-03-19 23:05:43 -04003935 ret = ext4_ext_handle_uninitialized_extents(
3936 handle, inode, map, path, flags,
3937 allocated, newblock);
3938 return ret;
Alex Tomasa86c6182006-10-11 01:21:03 -07003939 }
3940 }
3941
Aditya Kali7b415bf2011-09-09 19:04:51 -04003942 if ((sbi->s_cluster_ratio > 1) &&
3943 ext4_find_delalloc_cluster(inode, map->m_lblk, 0))
3944 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
3945
Alex Tomasa86c6182006-10-11 01:21:03 -07003946 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07003947 * requested block isn't allocated yet;
Alex Tomasa86c6182006-10-11 01:21:03 -07003948 * we couldn't try to create block if create flag is zero
3949 */
Theodore Ts'oc2177052009-05-14 00:58:52 -04003950 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
Amit Arora56055d32007-07-17 21:42:38 -04003951 /*
3952 * put just found gap into cache to speed up
3953 * subsequent requests
3954 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003955 ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
Alex Tomasa86c6182006-10-11 01:21:03 -07003956 goto out2;
3957 }
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003958
Alex Tomasa86c6182006-10-11 01:21:03 -07003959 /*
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04003960 * Okay, we need to do block allocation.
Andrew Morton63f57932006-10-11 01:21:24 -07003961 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04003962 map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003963 newex.ee_block = cpu_to_le32(map->m_lblk);
3964 cluster_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
3965
3966 /*
3967 * If we are doing bigalloc, check to see if the extent returned
3968 * by ext4_ext_find_extent() implies a cluster we can use.
3969 */
3970 if (cluster_offset && ex &&
Aditya Kalid8990242011-09-09 19:18:51 -04003971 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003972 ar.len = allocated = map->m_len;
3973 newblock = map->m_pblk;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003974 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003975 goto got_allocated_blocks;
3976 }
Alex Tomasa86c6182006-10-11 01:21:03 -07003977
Alex Tomasc9de5602008-01-29 00:19:52 -05003978 /* find neighbour allocated blocks */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003979 ar.lleft = map->m_lblk;
Alex Tomasc9de5602008-01-29 00:19:52 -05003980 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
3981 if (err)
3982 goto out2;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04003983 ar.lright = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003984 ex2 = NULL;
3985 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
Alex Tomasc9de5602008-01-29 00:19:52 -05003986 if (err)
3987 goto out2;
Amit Arora25d14f92007-05-24 13:04:13 -04003988
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003989 /* Check if the extent after searching to the right implies a
3990 * cluster we can use. */
3991 if ((sbi->s_cluster_ratio > 1) && ex2 &&
Aditya Kalid8990242011-09-09 19:18:51 -04003992 get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003993 ar.len = allocated = map->m_len;
3994 newblock = map->m_pblk;
Aditya Kali7b415bf2011-09-09 19:04:51 -04003995 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04003996 goto got_allocated_blocks;
3997 }
3998
Amit Arora749269f2007-07-18 09:02:56 -04003999 /*
4000 * See if request is beyond maximum number of blocks we can have in
4001 * a single extent. For an initialized extent this limit is
4002 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
4003 * EXT_UNINIT_MAX_LEN.
4004 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004005 if (map->m_len > EXT_INIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04004006 !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004007 map->m_len = EXT_INIT_MAX_LEN;
4008 else if (map->m_len > EXT_UNINIT_MAX_LEN &&
Theodore Ts'oc2177052009-05-14 00:58:52 -04004009 (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004010 map->m_len = EXT_UNINIT_MAX_LEN;
Amit Arora749269f2007-07-18 09:02:56 -04004011
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004012 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004013 newex.ee_len = cpu_to_le16(map->m_len);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004014 err = ext4_ext_check_overlap(sbi, inode, &newex, path);
Amit Arora25d14f92007-05-24 13:04:13 -04004015 if (err)
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004016 allocated = ext4_ext_get_actual_len(&newex);
Amit Arora25d14f92007-05-24 13:04:13 -04004017 else
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004018 allocated = map->m_len;
Alex Tomasc9de5602008-01-29 00:19:52 -05004019
4020 /* allocate new block */
4021 ar.inode = inode;
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004022 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
4023 ar.logical = map->m_lblk;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004024 /*
4025 * We calculate the offset from the beginning of the cluster
4026 * for the logical block number, since when we allocate a
4027 * physical cluster, the physical block should start at the
4028 * same offset from the beginning of the cluster. This is
4029 * needed so that future calls to get_implied_cluster_alloc()
4030 * work correctly.
4031 */
4032 offset = map->m_lblk & (sbi->s_cluster_ratio - 1);
4033 ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
4034 ar.goal -= offset;
4035 ar.logical -= offset;
Alex Tomasc9de5602008-01-29 00:19:52 -05004036 if (S_ISREG(inode->i_mode))
4037 ar.flags = EXT4_MB_HINT_DATA;
4038 else
4039 /* disable in-core preallocation for non-regular files */
4040 ar.flags = 0;
Vivek Haldar556b27a2011-05-25 07:41:54 -04004041 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
4042 ar.flags |= EXT4_MB_HINT_NOPREALLOC;
Alex Tomasc9de5602008-01-29 00:19:52 -05004043 newblock = ext4_mb_new_blocks(handle, &ar, &err);
Alex Tomasa86c6182006-10-11 01:21:03 -07004044 if (!newblock)
4045 goto out2;
Mingming84fe3be2009-09-01 08:44:37 -04004046 ext_debug("allocate new block: goal %llu, found %llu/%u\n",
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004047 ar.goal, newblock, allocated);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004048 free_on_err = 1;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004049 allocated_clusters = ar.len;
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004050 ar.len = EXT4_C2B(sbi, ar.len) - offset;
4051 if (ar.len > allocated)
4052 ar.len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004053
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004054got_allocated_blocks:
Alex Tomasa86c6182006-10-11 01:21:03 -07004055 /* try to insert new extent into found leaf and return */
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004056 ext4_ext_store_pblock(&newex, newblock + offset);
Alex Tomasc9de5602008-01-29 00:19:52 -05004057 newex.ee_len = cpu_to_le16(ar.len);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004058 /* Mark uninitialized */
4059 if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
Amit Aroraa2df2a62007-07-17 21:42:41 -04004060 ext4_ext_mark_uninitialized(&newex);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004061 /*
Jiaying Zhang744692d2010-03-04 16:14:02 -05004062 * io_end structure was created for every IO write to an
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004063 * uninitialized extent. To avoid unnecessary conversion,
Jiaying Zhang744692d2010-03-04 16:14:02 -05004064 * here we flag the IO that really needs the conversion.
Mingming5f524952009-11-10 10:48:04 -05004065 * For non asycn direct IO case, flag the inode state
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004066 * that we need to perform conversion when IO is done.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004067 */
Jiaying Zhang744692d2010-03-04 16:14:02 -05004068 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
Tao Ma0edeb712011-10-31 17:30:44 -04004069 if (io)
4070 ext4_set_io_unwritten_flag(inode, io);
4071 else
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004072 ext4_set_inode_state(inode,
4073 EXT4_STATE_DIO_UNWRITTEN);
Mingming5f524952009-11-10 10:48:04 -05004074 }
Jiaying Zhang744692d2010-03-04 16:14:02 -05004075 if (ext4_should_dioread_nolock(inode))
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004076 map->m_flags |= EXT4_MAP_UNINIT;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04004077 }
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004078
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004079 err = 0;
4080 if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
4081 err = check_eofblocks_fl(handle, inode, map->m_lblk,
4082 path, ar.len);
Jiaying Zhang575a1d42011-07-10 20:07:25 -04004083 if (!err)
4084 err = ext4_ext_insert_extent(handle, inode, path,
4085 &newex, flags);
Theodore Ts'o4d33b1e2011-09-09 18:52:51 -04004086 if (err && free_on_err) {
Maxim Patlasov7132de72011-07-10 19:37:48 -04004087 int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
4088 EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
Alex Tomas315054f2007-05-24 13:04:25 -04004089 /* free data blocks we just allocated */
Alex Tomasc9de5602008-01-29 00:19:52 -05004090 /* not a good idea to call discard here directly,
4091 * but otherwise we'd need to call it every free() */
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004092 ext4_discard_preallocations(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05004093 ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex),
Maxim Patlasov7132de72011-07-10 19:37:48 -04004094 ext4_ext_get_actual_len(&newex), fb_flags);
Alex Tomasa86c6182006-10-11 01:21:03 -07004095 goto out2;
Alex Tomas315054f2007-05-24 13:04:25 -04004096 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004097
Alex Tomasa86c6182006-10-11 01:21:03 -07004098 /* previous routine could use block we allocated */
Theodore Ts'obf89d162010-10-27 21:30:14 -04004099 newblock = ext4_ext_pblock(&newex);
Aneesh Kumar K.Vb939e372008-01-28 23:58:27 -05004100 allocated = ext4_ext_get_actual_len(&newex);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004101 if (allocated > map->m_len)
4102 allocated = map->m_len;
4103 map->m_flags |= EXT4_MAP_NEW;
Alex Tomasa86c6182006-10-11 01:21:03 -07004104
Jan Karab436b9b2009-12-08 23:51:10 -05004105 /*
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004106 * Update reserved blocks/metadata blocks after successful
4107 * block allocation which had been deferred till now.
4108 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04004109 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004110 unsigned int reserved_clusters;
Aditya Kali7b415bf2011-09-09 19:04:51 -04004111 /*
Yongqiang Yang81fdbb42011-10-29 09:23:38 -04004112 * Check how many clusters we had reserved this allocated range
Aditya Kali7b415bf2011-09-09 19:04:51 -04004113 */
4114 reserved_clusters = get_reserved_cluster_alloc(inode,
4115 map->m_lblk, allocated);
4116 if (map->m_flags & EXT4_MAP_FROM_CLUSTER) {
4117 if (reserved_clusters) {
4118 /*
4119 * We have clusters reserved for this range.
4120 * But since we are not doing actual allocation
4121 * and are simply using blocks from previously
4122 * allocated cluster, we should release the
4123 * reservation and not claim quota.
4124 */
4125 ext4_da_update_reserve_space(inode,
4126 reserved_clusters, 0);
4127 }
4128 } else {
4129 BUG_ON(allocated_clusters < reserved_clusters);
4130 /* We will claim quota for all newly allocated blocks.*/
4131 ext4_da_update_reserve_space(inode, allocated_clusters,
4132 1);
4133 if (reserved_clusters < allocated_clusters) {
Aditya Kali5356f262011-09-09 19:20:51 -04004134 struct ext4_inode_info *ei = EXT4_I(inode);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004135 int reservation = allocated_clusters -
4136 reserved_clusters;
4137 /*
4138 * It seems we claimed few clusters outside of
4139 * the range of this allocation. We should give
4140 * it back to the reservation pool. This can
4141 * happen in the following case:
4142 *
4143 * * Suppose s_cluster_ratio is 4 (i.e., each
4144 * cluster has 4 blocks. Thus, the clusters
4145 * are [0-3],[4-7],[8-11]...
4146 * * First comes delayed allocation write for
4147 * logical blocks 10 & 11. Since there were no
4148 * previous delayed allocated blocks in the
4149 * range [8-11], we would reserve 1 cluster
4150 * for this write.
4151 * * Next comes write for logical blocks 3 to 8.
4152 * In this case, we will reserve 2 clusters
4153 * (for [0-3] and [4-7]; and not for [8-11] as
4154 * that range has a delayed allocated blocks.
4155 * Thus total reserved clusters now becomes 3.
4156 * * Now, during the delayed allocation writeout
4157 * time, we will first write blocks [3-8] and
4158 * allocate 3 clusters for writing these
4159 * blocks. Also, we would claim all these
4160 * three clusters above.
4161 * * Now when we come here to writeout the
4162 * blocks [10-11], we would expect to claim
4163 * the reservation of 1 cluster we had made
4164 * (and we would claim it since there are no
4165 * more delayed allocated blocks in the range
4166 * [8-11]. But our reserved cluster count had
4167 * already gone to 0.
4168 *
4169 * Thus, at the step 4 above when we determine
4170 * that there are still some unwritten delayed
4171 * allocated blocks outside of our current
4172 * block range, we should increment the
4173 * reserved clusters count so that when the
4174 * remaining blocks finally gets written, we
4175 * could claim them.
4176 */
Aditya Kali5356f262011-09-09 19:20:51 -04004177 dquot_reserve_block(inode,
4178 EXT4_C2B(sbi, reservation));
4179 spin_lock(&ei->i_block_reservation_lock);
4180 ei->i_reserved_data_blocks += reservation;
4181 spin_unlock(&ei->i_block_reservation_lock);
Aditya Kali7b415bf2011-09-09 19:04:51 -04004182 }
4183 }
4184 }
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -05004185
4186 /*
Jan Karab436b9b2009-12-08 23:51:10 -05004187 * Cache the extent and update transaction to commit on fdatasync only
4188 * when it is _not_ an uninitialized extent.
4189 */
4190 if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) {
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05004191 ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock);
Jan Karab436b9b2009-12-08 23:51:10 -05004192 ext4_update_inode_fsync_trans(handle, inode, 1);
4193 } else
4194 ext4_update_inode_fsync_trans(handle, inode, 0);
Alex Tomasa86c6182006-10-11 01:21:03 -07004195out:
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004196 if (allocated > map->m_len)
4197 allocated = map->m_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004198 ext4_ext_show_leaf(inode, path);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004199 map->m_flags |= EXT4_MAP_MAPPED;
4200 map->m_pblk = newblock;
4201 map->m_len = allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004202out2:
4203 if (path) {
4204 ext4_ext_drop_refs(path);
4205 kfree(path);
4206 }
Allison Hendersone8613042011-05-25 07:41:46 -04004207
Yongqiang Yange7b319e2011-10-29 09:39:51 -04004208 trace_ext4_ext_map_blocks_exit(inode, map->m_lblk,
Lukas Czerner78771912012-03-19 23:05:43 -04004209 newblock, map->m_len, err ? err : allocated);
Yongqiang Yange7b319e2011-10-29 09:39:51 -04004210
Lukas Czerner78771912012-03-19 23:05:43 -04004211 return err ? err : allocated;
Alex Tomasa86c6182006-10-11 01:21:03 -07004212}
4213
Jan Karacf108bc2008-07-11 19:27:31 -04004214void ext4_ext_truncate(struct inode *inode)
Alex Tomasa86c6182006-10-11 01:21:03 -07004215{
4216 struct address_space *mapping = inode->i_mapping;
4217 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004218 ext4_lblk_t last_block;
Alex Tomasa86c6182006-10-11 01:21:03 -07004219 handle_t *handle;
Allison Henderson189e8682011-09-06 21:49:44 -04004220 loff_t page_len;
Alex Tomasa86c6182006-10-11 01:21:03 -07004221 int err = 0;
4222
4223 /*
Jiaying Zhang3889fd52011-01-10 12:47:05 -05004224 * finish any pending end_io work so we won't run the risk of
4225 * converting any truncated blocks to initialized later
4226 */
4227 ext4_flush_completed_IO(inode);
4228
4229 /*
Alex Tomasa86c6182006-10-11 01:21:03 -07004230 * probably first extent we're gonna free will be last in block
4231 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04004232 err = ext4_writepage_trans_blocks(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07004233 handle = ext4_journal_start(inode, err);
Jan Karacf108bc2008-07-11 19:27:31 -04004234 if (IS_ERR(handle))
Alex Tomasa86c6182006-10-11 01:21:03 -07004235 return;
Alex Tomasa86c6182006-10-11 01:21:03 -07004236
Allison Henderson189e8682011-09-06 21:49:44 -04004237 if (inode->i_size % PAGE_CACHE_SIZE != 0) {
4238 page_len = PAGE_CACHE_SIZE -
4239 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4240
4241 err = ext4_discard_partial_page_buffers(handle,
4242 mapping, inode->i_size, page_len, 0);
4243
4244 if (err)
4245 goto out_stop;
4246 }
Alex Tomasa86c6182006-10-11 01:21:03 -07004247
Jan Kara9ddfc3d2008-07-11 19:27:31 -04004248 if (ext4_orphan_add(handle, inode))
4249 goto out_stop;
4250
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05004251 down_write(&EXT4_I(inode)->i_data_sem);
Alex Tomasa86c6182006-10-11 01:21:03 -07004252 ext4_ext_invalidate_cache(inode);
4253
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04004254 ext4_discard_preallocations(inode);
Alex Tomasc9de5602008-01-29 00:19:52 -05004255
Alex Tomasa86c6182006-10-11 01:21:03 -07004256 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004257 * TODO: optimization is possible here.
4258 * Probably we need not scan at all,
4259 * because page truncation is enough.
Alex Tomasa86c6182006-10-11 01:21:03 -07004260 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004261
4262 /* we have to know where to truncate from in crash case */
4263 EXT4_I(inode)->i_disksize = inode->i_size;
4264 ext4_mark_inode_dirty(handle, inode);
4265
4266 last_block = (inode->i_size + sb->s_blocksize - 1)
4267 >> EXT4_BLOCK_SIZE_BITS(sb);
Lukas Czerner5f95d212012-03-19 23:03:19 -04004268 err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
Alex Tomasa86c6182006-10-11 01:21:03 -07004269
4270 /* In a multi-transaction truncate, we only make the final
Amit Arora56055d32007-07-17 21:42:38 -04004271 * transaction synchronous.
4272 */
Alex Tomasa86c6182006-10-11 01:21:03 -07004273 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -05004274 ext4_handle_sync(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07004275
Jan Kara9ddfc3d2008-07-11 19:27:31 -04004276 up_write(&EXT4_I(inode)->i_data_sem);
Eric Gouriouf6d2f6b2011-05-22 21:33:00 -04004277
4278out_stop:
Alex Tomasa86c6182006-10-11 01:21:03 -07004279 /*
Randy Dunlapd0d856e2006-10-11 01:21:07 -07004280 * If this was a simple ftruncate() and the file will remain alive,
Alex Tomasa86c6182006-10-11 01:21:03 -07004281 * then we need to clear up the orphan record which we created above.
4282 * However, if this was a real unlink then we were called by
4283 * ext4_delete_inode(), and we allow that function to clean up the
4284 * orphan info for us.
4285 */
4286 if (inode->i_nlink)
4287 ext4_orphan_del(handle, inode);
4288
Solofo Ramangalahyef737722008-04-29 22:00:41 -04004289 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4290 ext4_mark_inode_dirty(handle, inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07004291 ext4_journal_stop(handle);
4292}
4293
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004294static void ext4_falloc_update_inode(struct inode *inode,
4295 int mode, loff_t new_size, int update_ctime)
4296{
4297 struct timespec now;
4298
4299 if (update_ctime) {
4300 now = current_fs_time(inode->i_sb);
4301 if (!timespec_equal(&inode->i_ctime, &now))
4302 inode->i_ctime = now;
4303 }
4304 /*
4305 * Update only when preallocation was requested beyond
4306 * the file size.
4307 */
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04004308 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
4309 if (new_size > i_size_read(inode))
4310 i_size_write(inode, new_size);
4311 if (new_size > EXT4_I(inode)->i_disksize)
4312 ext4_update_i_disksize(inode, new_size);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004313 } else {
4314 /*
4315 * Mark that we allocate beyond EOF so the subsequent truncate
4316 * can proceed even if the new size is the same as i_size.
4317 */
4318 if (new_size > i_size_read(inode))
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004319 ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004320 }
4321
4322}
4323
Amit Aroraa2df2a62007-07-17 21:42:41 -04004324/*
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004325 * preallocate space for a file. This implements ext4's fallocate file
Amit Aroraa2df2a62007-07-17 21:42:41 -04004326 * operation, which gets called from sys_fallocate system call.
4327 * For block-mapped files, posix_fallocate should fall back to the method
4328 * of writing zeroes to the required new blocks (the same behavior which is
4329 * expected for file systems which do not support fallocate() system call).
4330 */
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004331long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
Amit Aroraa2df2a62007-07-17 21:42:41 -04004332{
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01004333 struct inode *inode = file->f_path.dentry->d_inode;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004334 handle_t *handle;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004335 loff_t new_size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05004336 unsigned int max_blocks;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004337 int ret = 0;
4338 int ret2 = 0;
4339 int retries = 0;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004340 int flags;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004341 struct ext4_map_blocks map;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004342 unsigned int credits, blkbits = inode->i_blkbits;
4343
4344 /*
4345 * currently supporting (pre)allocate mode for extent-based
4346 * files _only_
4347 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004348 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Amit Aroraa2df2a62007-07-17 21:42:41 -04004349 return -EOPNOTSUPP;
4350
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004351 /* Return error if mode is not supported */
4352 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
4353 return -EOPNOTSUPP;
4354
4355 if (mode & FALLOC_FL_PUNCH_HOLE)
4356 return ext4_punch_hole(file, offset, len);
4357
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004358 trace_ext4_fallocate_enter(inode, offset, len, mode);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004359 map.m_lblk = offset >> blkbits;
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004360 /*
4361 * We can't just convert len to max_blocks because
4362 * If blocksize = 4096 offset = 3072 and len = 2048
4363 */
Amit Aroraa2df2a62007-07-17 21:42:41 -04004364 max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004365 - map.m_lblk;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004366 /*
Mingming Caof3bd1f32008-08-19 22:16:03 -04004367 * credits to insert 1 extent into extent tree
Amit Aroraa2df2a62007-07-17 21:42:41 -04004368 */
Mingming Caof3bd1f32008-08-19 22:16:03 -04004369 credits = ext4_chunk_trans_blocks(inode, max_blocks);
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05004370 mutex_lock(&inode->i_mutex);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04004371 ret = inode_newsize_ok(inode, (len + offset));
4372 if (ret) {
4373 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004374 trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
Nikanth Karthikesan6d19c422010-05-16 14:00:00 -04004375 return ret;
4376 }
Greg Harm3c6fe772011-10-31 18:41:47 -04004377 flags = EXT4_GET_BLOCKS_CREATE_UNINIT_EXT;
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004378 if (mode & FALLOC_FL_KEEP_SIZE)
4379 flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
Greg Harm3c6fe772011-10-31 18:41:47 -04004380 /*
4381 * Don't normalize the request if it can fit in one extent so
4382 * that it doesn't get unnecessarily split into multiple
4383 * extents.
4384 */
4385 if (len <= EXT_UNINIT_MAX_LEN << blkbits)
4386 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004387retry:
4388 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004389 map.m_lblk = map.m_lblk + ret;
4390 map.m_len = max_blocks = max_blocks - ret;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004391 handle = ext4_journal_start(inode, credits);
4392 if (IS_ERR(handle)) {
4393 ret = PTR_ERR(handle);
4394 break;
4395 }
Dmitry Monakhova4e5d882011-10-25 08:15:12 -04004396 ret = ext4_map_blocks(handle, inode, &map, flags);
Aneesh Kumar K.V221879c2008-01-28 23:58:27 -05004397 if (ret <= 0) {
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004398#ifdef EXT4FS_DEBUG
4399 WARN_ON(ret <= 0);
Theodore Ts'oe35fd662010-05-16 19:00:00 -04004400 printk(KERN_ERR "%s: ext4_ext_map_blocks "
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004401 "returned error inode#%lu, block=%u, "
Thadeu Lima de Souza Cascardo9fd97842009-01-26 19:26:26 -05004402 "max_blocks=%u", __func__,
Kazuya Mioa6371b62010-10-27 21:30:15 -04004403 inode->i_ino, map.m_lblk, max_blocks);
Aneesh Kumar K.V2c986152008-02-25 15:41:35 -05004404#endif
Amit Aroraa2df2a62007-07-17 21:42:41 -04004405 ext4_mark_inode_dirty(handle, inode);
4406 ret2 = ext4_journal_stop(handle);
4407 break;
4408 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004409 if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004410 blkbits) >> blkbits))
4411 new_size = offset + len;
4412 else
Utako Kusaka29ae07b2011-07-27 22:11:20 -04004413 new_size = ((loff_t) map.m_lblk + ret) << blkbits;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004414
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004415 ext4_falloc_update_inode(inode, mode, new_size,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004416 (map.m_flags & EXT4_MAP_NEW));
Amit Aroraa2df2a62007-07-17 21:42:41 -04004417 ext4_mark_inode_dirty(handle, inode);
4418 ret2 = ext4_journal_stop(handle);
4419 if (ret2)
4420 break;
4421 }
Aneesh Kumar K.Vfd287842008-04-29 08:11:12 -04004422 if (ret == -ENOSPC &&
4423 ext4_should_retry_alloc(inode->i_sb, &retries)) {
4424 ret = 0;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004425 goto retry;
Amit Aroraa2df2a62007-07-17 21:42:41 -04004426 }
Aneesh Kumar K.V55bd7252008-02-15 12:47:21 -05004427 mutex_unlock(&inode->i_mutex);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004428 trace_ext4_fallocate_exit(inode, offset, max_blocks,
4429 ret > 0 ? ret2 : ret);
Amit Aroraa2df2a62007-07-17 21:42:41 -04004430 return ret > 0 ? ret2 : ret;
4431}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004432
4433/*
Mingming Cao00314622009-09-28 15:49:08 -04004434 * This function convert a range of blocks to written extents
4435 * The caller of this function will pass the start offset and the size.
4436 * all unwritten extents within this range will be converted to
4437 * written extents.
4438 *
4439 * This function is called from the direct IO end io call back
4440 * function, to convert the fallocated extents after IO is completed.
Mingming109f5562009-11-10 10:48:08 -05004441 * Returns 0 on success.
Mingming Cao00314622009-09-28 15:49:08 -04004442 */
4443int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset,
Eric Sandeena1de02d2010-02-04 23:58:38 -05004444 ssize_t len)
Mingming Cao00314622009-09-28 15:49:08 -04004445{
4446 handle_t *handle;
Mingming Cao00314622009-09-28 15:49:08 -04004447 unsigned int max_blocks;
4448 int ret = 0;
4449 int ret2 = 0;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004450 struct ext4_map_blocks map;
Mingming Cao00314622009-09-28 15:49:08 -04004451 unsigned int credits, blkbits = inode->i_blkbits;
4452
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004453 map.m_lblk = offset >> blkbits;
Mingming Cao00314622009-09-28 15:49:08 -04004454 /*
4455 * We can't just convert len to max_blocks because
4456 * If blocksize = 4096 offset = 3072 and len = 2048
4457 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004458 max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
4459 map.m_lblk);
Mingming Cao00314622009-09-28 15:49:08 -04004460 /*
4461 * credits to insert 1 extent into extent tree
4462 */
4463 credits = ext4_chunk_trans_blocks(inode, max_blocks);
4464 while (ret >= 0 && ret < max_blocks) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004465 map.m_lblk += ret;
4466 map.m_len = (max_blocks -= ret);
Mingming Cao00314622009-09-28 15:49:08 -04004467 handle = ext4_journal_start(inode, credits);
4468 if (IS_ERR(handle)) {
4469 ret = PTR_ERR(handle);
4470 break;
4471 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -04004472 ret = ext4_map_blocks(handle, inode, &map,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05004473 EXT4_GET_BLOCKS_IO_CONVERT_EXT);
Mingming Cao00314622009-09-28 15:49:08 -04004474 if (ret <= 0) {
4475 WARN_ON(ret <= 0);
Theodore Ts'o92b97812012-03-19 23:41:49 -04004476 ext4_msg(inode->i_sb, KERN_ERR,
4477 "%s:%d: inode #%lu: block %u: len %u: "
4478 "ext4_ext_map_blocks returned %d",
4479 __func__, __LINE__, inode->i_ino, map.m_lblk,
4480 map.m_len, ret);
Mingming Cao00314622009-09-28 15:49:08 -04004481 }
4482 ext4_mark_inode_dirty(handle, inode);
4483 ret2 = ext4_journal_stop(handle);
4484 if (ret <= 0 || ret2 )
4485 break;
4486 }
4487 return ret > 0 ? ret2 : ret;
4488}
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004489
Mingming Cao00314622009-09-28 15:49:08 -04004490/*
Eric Sandeen6873fa02008-10-07 00:46:36 -04004491 * Callback function called for each extent to gather FIEMAP information.
4492 */
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04004493static int ext4_ext_fiemap_cb(struct inode *inode, ext4_lblk_t next,
Eric Sandeen6873fa02008-10-07 00:46:36 -04004494 struct ext4_ext_cache *newex, struct ext4_extent *ex,
4495 void *data)
4496{
Eric Sandeen6873fa02008-10-07 00:46:36 -04004497 __u64 logical;
4498 __u64 physical;
4499 __u64 length;
4500 __u32 flags = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004501 int ret = 0;
4502 struct fiemap_extent_info *fieinfo = data;
4503 unsigned char blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004504
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004505 blksize_bits = inode->i_sb->s_blocksize_bits;
4506 logical = (__u64)newex->ec_block << blksize_bits;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004507
Theodore Ts'ob05e6ae2011-01-10 12:13:26 -05004508 if (newex->ec_start == 0) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004509 /*
4510 * No extent in extent-tree contains block @newex->ec_start,
4511 * then the block may stay in 1)a hole or 2)delayed-extent.
4512 *
4513 * Holes or delayed-extents are processed as follows.
4514 * 1. lookup dirty pages with specified range in pagecache.
4515 * If no page is got, then there is no delayed-extent and
4516 * return with EXT_CONTINUE.
4517 * 2. find the 1st mapped buffer,
4518 * 3. check if the mapped buffer is both in the request range
4519 * and a delayed buffer. If not, there is no delayed-extent,
4520 * then return.
4521 * 4. a delayed-extent is found, the extent will be collected.
4522 */
4523 ext4_lblk_t end = 0;
4524 pgoff_t last_offset;
4525 pgoff_t offset;
4526 pgoff_t index;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004527 pgoff_t start_index = 0;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004528 struct page **pages = NULL;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004529 struct buffer_head *bh = NULL;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004530 struct buffer_head *head = NULL;
4531 unsigned int nr_pages = PAGE_SIZE / sizeof(struct page *);
4532
4533 pages = kmalloc(PAGE_SIZE, GFP_KERNEL);
4534 if (pages == NULL)
4535 return -ENOMEM;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004536
4537 offset = logical >> PAGE_SHIFT;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004538repeat:
4539 last_offset = offset;
4540 head = NULL;
4541 ret = find_get_pages_tag(inode->i_mapping, &offset,
4542 PAGECACHE_TAG_DIRTY, nr_pages, pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004543
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004544 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4545 /* First time, try to find a mapped buffer. */
4546 if (ret == 0) {
4547out:
4548 for (index = 0; index < ret; index++)
4549 page_cache_release(pages[index]);
4550 /* just a hole. */
4551 kfree(pages);
4552 return EXT_CONTINUE;
4553 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004554 index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004555
Yongqiang Yangb2213492011-05-24 11:36:58 -04004556next_page:
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004557 /* Try to find the 1st mapped buffer. */
Yongqiang Yangb2213492011-05-24 11:36:58 -04004558 end = ((__u64)pages[index]->index << PAGE_SHIFT) >>
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004559 blksize_bits;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004560 if (!page_has_buffers(pages[index]))
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004561 goto out;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004562 head = page_buffers(pages[index]);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004563 if (!head)
4564 goto out;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004565
Yongqiang Yangb2213492011-05-24 11:36:58 -04004566 index++;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004567 bh = head;
4568 do {
Yongqiang Yangb2213492011-05-24 11:36:58 -04004569 if (end >= newex->ec_block +
4570 newex->ec_len)
4571 /* The buffer is out of
4572 * the request range.
4573 */
4574 goto out;
4575
4576 if (buffer_mapped(bh) &&
4577 end >= newex->ec_block) {
4578 start_index = index - 1;
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004579 /* get the 1st mapped buffer. */
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004580 goto found_mapped_buffer;
4581 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004582
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004583 bh = bh->b_this_page;
4584 end++;
4585 } while (bh != head);
4586
Yongqiang Yangb2213492011-05-24 11:36:58 -04004587 /* No mapped buffer in the range found in this page,
4588 * We need to look up next page.
4589 */
4590 if (index >= ret) {
4591 /* There is no page left, but we need to limit
4592 * newex->ec_len.
4593 */
4594 newex->ec_len = end - newex->ec_block;
4595 goto out;
4596 }
4597 goto next_page;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004598 } else {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004599 /*Find contiguous delayed buffers. */
4600 if (ret > 0 && pages[0]->index == last_offset)
4601 head = page_buffers(pages[0]);
4602 bh = head;
Yongqiang Yangb2213492011-05-24 11:36:58 -04004603 index = 1;
4604 start_index = 0;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004605 }
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004606
4607found_mapped_buffer:
4608 if (bh != NULL && buffer_delay(bh)) {
4609 /* 1st or contiguous delayed buffer found. */
4610 if (!(flags & FIEMAP_EXTENT_DELALLOC)) {
4611 /*
4612 * 1st delayed buffer found, record
4613 * the start of extent.
4614 */
4615 flags |= FIEMAP_EXTENT_DELALLOC;
4616 newex->ec_block = end;
4617 logical = (__u64)end << blksize_bits;
4618 }
4619 /* Find contiguous delayed buffers. */
4620 do {
4621 if (!buffer_delay(bh))
4622 goto found_delayed_extent;
4623 bh = bh->b_this_page;
4624 end++;
4625 } while (bh != head);
4626
Yongqiang Yangb2213492011-05-24 11:36:58 -04004627 for (; index < ret; index++) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004628 if (!page_has_buffers(pages[index])) {
4629 bh = NULL;
4630 break;
4631 }
4632 head = page_buffers(pages[index]);
4633 if (!head) {
4634 bh = NULL;
4635 break;
4636 }
Yongqiang Yangb2213492011-05-24 11:36:58 -04004637
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004638 if (pages[index]->index !=
Yongqiang Yangb2213492011-05-24 11:36:58 -04004639 pages[start_index]->index + index
4640 - start_index) {
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004641 /* Blocks are not contiguous. */
4642 bh = NULL;
4643 break;
4644 }
4645 bh = head;
4646 do {
4647 if (!buffer_delay(bh))
4648 /* Delayed-extent ends. */
4649 goto found_delayed_extent;
4650 bh = bh->b_this_page;
4651 end++;
4652 } while (bh != head);
4653 }
4654 } else if (!(flags & FIEMAP_EXTENT_DELALLOC))
4655 /* a hole found. */
4656 goto out;
4657
4658found_delayed_extent:
4659 newex->ec_len = min(end - newex->ec_block,
4660 (ext4_lblk_t)EXT_INIT_MAX_LEN);
4661 if (ret == nr_pages && bh != NULL &&
4662 newex->ec_len < EXT_INIT_MAX_LEN &&
4663 buffer_delay(bh)) {
4664 /* Have not collected an extent and continue. */
4665 for (index = 0; index < ret; index++)
4666 page_cache_release(pages[index]);
4667 goto repeat;
4668 }
4669
4670 for (index = 0; index < ret; index++)
4671 page_cache_release(pages[index]);
4672 kfree(pages);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004673 }
4674
4675 physical = (__u64)newex->ec_start << blksize_bits;
4676 length = (__u64)newex->ec_len << blksize_bits;
4677
4678 if (ex && ext4_ext_is_uninitialized(ex))
4679 flags |= FIEMAP_EXTENT_UNWRITTEN;
4680
Lukas Czernerc03f8aa2011-06-06 00:06:52 -04004681 if (next == EXT_MAX_BLOCKS)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004682 flags |= FIEMAP_EXTENT_LAST;
4683
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004684 ret = fiemap_fill_next_extent(fieinfo, logical, physical,
Eric Sandeen6873fa02008-10-07 00:46:36 -04004685 length, flags);
Yongqiang Yang6d9c85e2011-02-27 17:25:47 -05004686 if (ret < 0)
4687 return ret;
4688 if (ret == 1)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004689 return EXT_BREAK;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004690 return EXT_CONTINUE;
4691}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004692/* fiemap flags we can handle specified here */
4693#define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
4694
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004695static int ext4_xattr_fiemap(struct inode *inode,
4696 struct fiemap_extent_info *fieinfo)
Eric Sandeen6873fa02008-10-07 00:46:36 -04004697{
4698 __u64 physical = 0;
4699 __u64 length;
4700 __u32 flags = FIEMAP_EXTENT_LAST;
4701 int blockbits = inode->i_sb->s_blocksize_bits;
4702 int error = 0;
4703
4704 /* in-inode? */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004705 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
Eric Sandeen6873fa02008-10-07 00:46:36 -04004706 struct ext4_iloc iloc;
4707 int offset; /* offset of xattr in inode */
4708
4709 error = ext4_get_inode_loc(inode, &iloc);
4710 if (error)
4711 return error;
4712 physical = iloc.bh->b_blocknr << blockbits;
4713 offset = EXT4_GOOD_OLD_INODE_SIZE +
4714 EXT4_I(inode)->i_extra_isize;
4715 physical += offset;
4716 length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
4717 flags |= FIEMAP_EXTENT_DATA_INLINE;
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004718 brelse(iloc.bh);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004719 } else { /* external block */
4720 physical = EXT4_I(inode)->i_file_acl << blockbits;
4721 length = inode->i_sb->s_blocksize;
4722 }
4723
4724 if (physical)
4725 error = fiemap_fill_next_extent(fieinfo, 0, physical,
4726 length, flags);
4727 return (error < 0 ? error : 0);
4728}
4729
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004730/*
4731 * ext4_ext_punch_hole
4732 *
4733 * Punches a hole of "length" bytes in a file starting
4734 * at byte "offset"
4735 *
4736 * @inode: The inode of the file to punch a hole in
4737 * @offset: The starting byte offset of the hole
4738 * @length: The length of the hole
4739 *
4740 * Returns the number of blocks removed or negative on err
4741 */
4742int ext4_ext_punch_hole(struct file *file, loff_t offset, loff_t length)
4743{
4744 struct inode *inode = file->f_path.dentry->d_inode;
4745 struct super_block *sb = inode->i_sb;
Lukas Czerner5f95d212012-03-19 23:03:19 -04004746 ext4_lblk_t first_block, stop_block;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004747 struct address_space *mapping = inode->i_mapping;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004748 handle_t *handle;
Allison Hendersonba062082011-09-03 11:55:59 -04004749 loff_t first_page, last_page, page_len;
4750 loff_t first_page_offset, last_page_offset;
Lukas Czerner5f95d212012-03-19 23:03:19 -04004751 int credits, err = 0;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004752
Allison Henderson2be47512011-09-03 11:56:52 -04004753 /* No need to punch hole beyond i_size */
4754 if (offset >= inode->i_size)
4755 return 0;
4756
4757 /*
4758 * If the hole extends beyond i_size, set the hole
4759 * to end after the page that contains i_size
4760 */
4761 if (offset + length > inode->i_size) {
4762 length = inode->i_size +
4763 PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) -
4764 offset;
4765 }
4766
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004767 first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
4768 last_page = (offset + length) >> PAGE_CACHE_SHIFT;
4769
4770 first_page_offset = first_page << PAGE_CACHE_SHIFT;
4771 last_page_offset = last_page << PAGE_CACHE_SHIFT;
4772
4773 /*
4774 * Write out all dirty pages to avoid race conditions
4775 * Then release them.
4776 */
4777 if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
4778 err = filemap_write_and_wait_range(mapping,
Allison Henderson2be47512011-09-03 11:56:52 -04004779 offset, offset + length - 1);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004780
Allison Henderson2be47512011-09-03 11:56:52 -04004781 if (err)
4782 return err;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004783 }
4784
4785 /* Now release the pages */
4786 if (last_page_offset > first_page_offset) {
4787 truncate_inode_pages_range(mapping, first_page_offset,
4788 last_page_offset-1);
4789 }
4790
4791 /* finish any pending end_io work */
4792 ext4_flush_completed_IO(inode);
4793
4794 credits = ext4_writepage_trans_blocks(inode);
4795 handle = ext4_journal_start(inode, credits);
4796 if (IS_ERR(handle))
4797 return PTR_ERR(handle);
4798
4799 err = ext4_orphan_add(handle, inode);
4800 if (err)
4801 goto out;
4802
4803 /*
Allison Hendersonba062082011-09-03 11:55:59 -04004804 * Now we need to zero out the non-page-aligned data in the
4805 * pages at the start and tail of the hole, and unmap the buffer
4806 * heads for the block aligned regions of the page that were
4807 * completely zeroed.
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004808 */
Allison Hendersonba062082011-09-03 11:55:59 -04004809 if (first_page > last_page) {
4810 /*
4811 * If the file space being truncated is contained within a page
4812 * just zero out and unmap the middle of that page
4813 */
4814 err = ext4_discard_partial_page_buffers(handle,
4815 mapping, offset, length, 0);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004816
Allison Hendersonba062082011-09-03 11:55:59 -04004817 if (err)
4818 goto out;
4819 } else {
4820 /*
4821 * zero out and unmap the partial page that contains
4822 * the start of the hole
4823 */
4824 page_len = first_page_offset - offset;
4825 if (page_len > 0) {
4826 err = ext4_discard_partial_page_buffers(handle, mapping,
4827 offset, page_len, 0);
4828 if (err)
4829 goto out;
4830 }
4831
4832 /*
4833 * zero out and unmap the partial page that contains
4834 * the end of the hole
4835 */
4836 page_len = offset + length - last_page_offset;
4837 if (page_len > 0) {
4838 err = ext4_discard_partial_page_buffers(handle, mapping,
4839 last_page_offset, page_len, 0);
4840 if (err)
4841 goto out;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004842 }
4843 }
4844
Allison Henderson2be47512011-09-03 11:56:52 -04004845 /*
4846 * If i_size is contained in the last page, we need to
4847 * unmap and zero the partial page after i_size
4848 */
4849 if (inode->i_size >> PAGE_CACHE_SHIFT == last_page &&
4850 inode->i_size % PAGE_CACHE_SIZE != 0) {
4851
4852 page_len = PAGE_CACHE_SIZE -
4853 (inode->i_size & (PAGE_CACHE_SIZE - 1));
4854
4855 if (page_len > 0) {
4856 err = ext4_discard_partial_page_buffers(handle,
4857 mapping, inode->i_size, page_len, 0);
4858
4859 if (err)
4860 goto out;
4861 }
4862 }
4863
Lukas Czerner5f95d212012-03-19 23:03:19 -04004864 first_block = (offset + sb->s_blocksize - 1) >>
4865 EXT4_BLOCK_SIZE_BITS(sb);
4866 stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
4867
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004868 /* If there are no blocks to remove, return now */
Lukas Czerner5f95d212012-03-19 23:03:19 -04004869 if (first_block >= stop_block)
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004870 goto out;
4871
4872 down_write(&EXT4_I(inode)->i_data_sem);
4873 ext4_ext_invalidate_cache(inode);
4874 ext4_discard_preallocations(inode);
4875
Lukas Czerner5f95d212012-03-19 23:03:19 -04004876 err = ext4_ext_remove_space(inode, first_block, stop_block - 1);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004877
Lukas Czerner5f95d212012-03-19 23:03:19 -04004878 ext4_ext_invalidate_cache(inode);
4879 ext4_discard_preallocations(inode);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004880
4881 if (IS_SYNC(inode))
4882 ext4_handle_sync(handle);
4883
4884 up_write(&EXT4_I(inode)->i_data_sem);
4885
4886out:
4887 ext4_orphan_del(handle, inode);
4888 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4889 ext4_mark_inode_dirty(handle, inode);
4890 ext4_journal_stop(handle);
4891 return err;
4892}
Eric Sandeen6873fa02008-10-07 00:46:36 -04004893int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4894 __u64 start, __u64 len)
4895{
4896 ext4_lblk_t start_blk;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004897 int error = 0;
4898
4899 /* fallback to generic here if not in extents fmt */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004900 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Eric Sandeen6873fa02008-10-07 00:46:36 -04004901 return generic_block_fiemap(inode, fieinfo, start, len,
4902 ext4_get_block);
4903
4904 if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
4905 return -EBADR;
4906
4907 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
4908 error = ext4_xattr_fiemap(inode, fieinfo);
4909 } else {
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004910 ext4_lblk_t len_blks;
4911 __u64 last_blk;
4912
Eric Sandeen6873fa02008-10-07 00:46:36 -04004913 start_blk = start >> inode->i_sb->s_blocksize_bits;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004914 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
Lukas Czernerf17722f2011-06-06 00:05:17 -04004915 if (last_blk >= EXT_MAX_BLOCKS)
4916 last_blk = EXT_MAX_BLOCKS-1;
Leonard Michlmayraca92ff2010-03-04 17:07:28 -05004917 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
Eric Sandeen6873fa02008-10-07 00:46:36 -04004918
4919 /*
4920 * Walk the extent tree gathering extent information.
4921 * ext4_ext_fiemap_cb will push extents back to user.
4922 */
Eric Sandeen6873fa02008-10-07 00:46:36 -04004923 error = ext4_ext_walk_space(inode, start_blk, len_blks,
4924 ext4_ext_fiemap_cb, fieinfo);
Eric Sandeen6873fa02008-10-07 00:46:36 -04004925 }
4926
4927 return error;
4928}