| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1 | /* | 
 | 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 Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 32 | #include <linux/fs.h> | 
 | 33 | #include <linux/time.h> | 
| Mingming Cao | cd02ff0 | 2007-10-16 18:38:25 -0400 | [diff] [blame] | 34 | #include <linux/jbd2.h> | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 35 | #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 Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 40 | #include <linux/falloc.h> | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 41 | #include <asm/uaccess.h> | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 42 | #include <linux/fiemap.h> | 
| Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 43 | #include "ext4_jbd2.h" | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 44 |  | 
| Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 45 | #include <trace/events/ext4.h> | 
 | 46 |  | 
| Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 47 | static int ext4_split_extent(handle_t *handle, | 
 | 48 | 				struct inode *inode, | 
 | 49 | 				struct ext4_ext_path *path, | 
 | 50 | 				struct ext4_map_blocks *map, | 
 | 51 | 				int split_flag, | 
 | 52 | 				int flags); | 
 | 53 |  | 
| Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 54 | static int ext4_ext_truncate_extend_restart(handle_t *handle, | 
 | 55 | 					    struct inode *inode, | 
 | 56 | 					    int needed) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 57 | { | 
 | 58 | 	int err; | 
 | 59 |  | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 60 | 	if (!ext4_handle_valid(handle)) | 
 | 61 | 		return 0; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 62 | 	if (handle->h_buffer_credits > needed) | 
| Shen Feng | 9102e4f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 63 | 		return 0; | 
 | 64 | 	err = ext4_journal_extend(handle, needed); | 
| Theodore Ts'o | 0123c93 | 2008-08-01 20:57:54 -0400 | [diff] [blame] | 65 | 	if (err <= 0) | 
| Shen Feng | 9102e4f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 66 | 		return err; | 
| Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 67 | 	err = ext4_truncate_restart_trans(handle, inode, needed); | 
| Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 68 | 	if (err == 0) | 
 | 69 | 		err = -EAGAIN; | 
| Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 70 |  | 
 | 71 | 	return err; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 72 | } | 
 | 73 |  | 
 | 74 | /* | 
 | 75 |  * could return: | 
 | 76 |  *  - EROFS | 
 | 77 |  *  - ENOMEM | 
 | 78 |  */ | 
 | 79 | static int ext4_ext_get_access(handle_t *handle, struct inode *inode, | 
 | 80 | 				struct ext4_ext_path *path) | 
 | 81 | { | 
 | 82 | 	if (path->p_bh) { | 
 | 83 | 		/* path points to block */ | 
 | 84 | 		return ext4_journal_get_write_access(handle, path->p_bh); | 
 | 85 | 	} | 
 | 86 | 	/* path points to leaf/index in inode body */ | 
 | 87 | 	/* we use in-core data, no need to protect them */ | 
 | 88 | 	return 0; | 
 | 89 | } | 
 | 90 |  | 
 | 91 | /* | 
 | 92 |  * could return: | 
 | 93 |  *  - EROFS | 
 | 94 |  *  - ENOMEM | 
 | 95 |  *  - EIO | 
 | 96 |  */ | 
| Theodore Ts'o | 9ea7a0d | 2011-09-04 10:18:14 -0400 | [diff] [blame] | 97 | #define ext4_ext_dirty(handle, inode, path) \ | 
 | 98 | 		__ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path)) | 
 | 99 | static int __ext4_ext_dirty(const char *where, unsigned int line, | 
 | 100 | 			    handle_t *handle, struct inode *inode, | 
 | 101 | 			    struct ext4_ext_path *path) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 102 | { | 
 | 103 | 	int err; | 
 | 104 | 	if (path->p_bh) { | 
 | 105 | 		/* path points to block */ | 
| Theodore Ts'o | 9ea7a0d | 2011-09-04 10:18:14 -0400 | [diff] [blame] | 106 | 		err = __ext4_handle_dirty_metadata(where, line, handle, | 
 | 107 | 						   inode, path->p_bh); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 108 | 	} else { | 
 | 109 | 		/* path points to leaf/index in inode body */ | 
 | 110 | 		err = ext4_mark_inode_dirty(handle, inode); | 
 | 111 | 	} | 
 | 112 | 	return err; | 
 | 113 | } | 
 | 114 |  | 
| Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 115 | static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 116 | 			      struct ext4_ext_path *path, | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 117 | 			      ext4_lblk_t block) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 118 | { | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 119 | 	if (path) { | 
| Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 120 | 		int depth = path->p_depth; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 121 | 		struct ext4_extent *ex; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 122 |  | 
| Kazuya Mio | ad4fb9c | 2011-01-10 12:12:28 -0500 | [diff] [blame] | 123 | 		/* | 
 | 124 | 		 * Try to predict block placement assuming that we are | 
 | 125 | 		 * filling in a file which will eventually be | 
 | 126 | 		 * non-sparse --- i.e., in the case of libbfd writing | 
 | 127 | 		 * an ELF object sections out-of-order but in a way | 
 | 128 | 		 * the eventually results in a contiguous object or | 
 | 129 | 		 * executable file, or some database extending a table | 
 | 130 | 		 * space file.  However, this is actually somewhat | 
 | 131 | 		 * non-ideal if we are writing a sparse file such as | 
 | 132 | 		 * qemu or KVM writing a raw image file that is going | 
 | 133 | 		 * to stay fairly sparse, since it will end up | 
 | 134 | 		 * fragmenting the file system's free space.  Maybe we | 
 | 135 | 		 * should have some hueristics or some way to allow | 
 | 136 | 		 * userspace to pass a hint to file system, | 
| Tao Ma | b8d6568 | 2011-01-21 23:21:31 +0800 | [diff] [blame] | 137 | 		 * especially if the latter case turns out to be | 
| Kazuya Mio | ad4fb9c | 2011-01-10 12:12:28 -0500 | [diff] [blame] | 138 | 		 * common. | 
 | 139 | 		 */ | 
| Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 140 | 		ex = path[depth].p_ext; | 
| Kazuya Mio | ad4fb9c | 2011-01-10 12:12:28 -0500 | [diff] [blame] | 141 | 		if (ex) { | 
 | 142 | 			ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex); | 
 | 143 | 			ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block); | 
 | 144 |  | 
 | 145 | 			if (block > ext_block) | 
 | 146 | 				return ext_pblk + (block - ext_block); | 
 | 147 | 			else | 
 | 148 | 				return ext_pblk - (ext_block - block); | 
 | 149 | 		} | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 150 |  | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 151 | 		/* it looks like index is empty; | 
 | 152 | 		 * try to find starting block from index itself */ | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 153 | 		if (path[depth].p_bh) | 
 | 154 | 			return path[depth].p_bh->b_blocknr; | 
 | 155 | 	} | 
 | 156 |  | 
 | 157 | 	/* OK. use inode's group */ | 
| Eric Sandeen | f86186b | 2011-06-28 10:01:31 -0400 | [diff] [blame] | 158 | 	return ext4_inode_to_goal_block(inode); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 159 | } | 
 | 160 |  | 
| Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 161 | /* | 
 | 162 |  * Allocation for a meta data block | 
 | 163 |  */ | 
| Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 164 | static ext4_fsblk_t | 
| Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 165 | ext4_ext_new_meta_block(handle_t *handle, struct inode *inode, | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 166 | 			struct ext4_ext_path *path, | 
| Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 167 | 			struct ext4_extent *ex, int *err, unsigned int flags) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 168 | { | 
| Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 169 | 	ext4_fsblk_t goal, newblock; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 170 |  | 
 | 171 | 	goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block)); | 
| Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 172 | 	newblock = ext4_new_meta_blocks(handle, inode, goal, flags, | 
 | 173 | 					NULL, err); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 174 | 	return newblock; | 
 | 175 | } | 
 | 176 |  | 
| Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 177 | static inline int ext4_ext_space_block(struct inode *inode, int check) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 178 | { | 
 | 179 | 	int size; | 
 | 180 |  | 
 | 181 | 	size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) | 
 | 182 | 			/ sizeof(struct ext4_extent); | 
| Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 183 | #ifdef AGGRESSIVE_TEST | 
| Yongqiang Yang | 02dc62fb | 2011-10-29 09:29:11 -0400 | [diff] [blame] | 184 | 	if (!check && size > 6) | 
 | 185 | 		size = 6; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 186 | #endif | 
 | 187 | 	return size; | 
 | 188 | } | 
 | 189 |  | 
| Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 190 | static inline int ext4_ext_space_block_idx(struct inode *inode, int check) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 191 | { | 
 | 192 | 	int size; | 
 | 193 |  | 
 | 194 | 	size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) | 
 | 195 | 			/ sizeof(struct ext4_extent_idx); | 
| Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 196 | #ifdef AGGRESSIVE_TEST | 
| Yongqiang Yang | 02dc62fb | 2011-10-29 09:29:11 -0400 | [diff] [blame] | 197 | 	if (!check && size > 5) | 
 | 198 | 		size = 5; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 199 | #endif | 
 | 200 | 	return size; | 
 | 201 | } | 
 | 202 |  | 
| Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 203 | static inline int ext4_ext_space_root(struct inode *inode, int check) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 204 | { | 
 | 205 | 	int size; | 
 | 206 |  | 
 | 207 | 	size = sizeof(EXT4_I(inode)->i_data); | 
 | 208 | 	size -= sizeof(struct ext4_extent_header); | 
 | 209 | 	size /= sizeof(struct ext4_extent); | 
| Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 210 | #ifdef AGGRESSIVE_TEST | 
| Yongqiang Yang | 02dc62fb | 2011-10-29 09:29:11 -0400 | [diff] [blame] | 211 | 	if (!check && size > 3) | 
 | 212 | 		size = 3; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 213 | #endif | 
 | 214 | 	return size; | 
 | 215 | } | 
 | 216 |  | 
| Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 217 | static inline int ext4_ext_space_root_idx(struct inode *inode, int check) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 218 | { | 
 | 219 | 	int size; | 
 | 220 |  | 
 | 221 | 	size = sizeof(EXT4_I(inode)->i_data); | 
 | 222 | 	size -= sizeof(struct ext4_extent_header); | 
 | 223 | 	size /= sizeof(struct ext4_extent_idx); | 
| Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 224 | #ifdef AGGRESSIVE_TEST | 
| Yongqiang Yang | 02dc62fb | 2011-10-29 09:29:11 -0400 | [diff] [blame] | 225 | 	if (!check && size > 4) | 
 | 226 | 		size = 4; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 227 | #endif | 
 | 228 | 	return size; | 
 | 229 | } | 
 | 230 |  | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 231 | /* | 
 | 232 |  * Calculate the number of metadata blocks needed | 
 | 233 |  * to allocate @blocks | 
 | 234 |  * Worse case is one block per extent | 
 | 235 |  */ | 
| Theodore Ts'o | 01f49d0 | 2011-01-10 12:13:03 -0500 | [diff] [blame] | 236 | int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock) | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 237 | { | 
| Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 238 | 	struct ext4_inode_info *ei = EXT4_I(inode); | 
| Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 239 | 	int idxs; | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 240 |  | 
| Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 241 | 	idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) | 
 | 242 | 		/ sizeof(struct ext4_extent_idx)); | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 243 |  | 
 | 244 | 	/* | 
| Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 245 | 	 * If the new delayed allocation block is contiguous with the | 
 | 246 | 	 * previous da block, it can share index blocks with the | 
 | 247 | 	 * previous block, so we only need to allocate a new index | 
 | 248 | 	 * block every idxs leaf blocks.  At ldxs**2 blocks, we need | 
 | 249 | 	 * an additional index block, and at ldxs**3 blocks, yet | 
 | 250 | 	 * another index blocks. | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 251 | 	 */ | 
| Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 252 | 	if (ei->i_da_metadata_calc_len && | 
 | 253 | 	    ei->i_da_metadata_calc_last_lblock+1 == lblock) { | 
| Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 254 | 		int num = 0; | 
 | 255 |  | 
| Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 256 | 		if ((ei->i_da_metadata_calc_len % idxs) == 0) | 
 | 257 | 			num++; | 
 | 258 | 		if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0) | 
 | 259 | 			num++; | 
 | 260 | 		if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) { | 
 | 261 | 			num++; | 
 | 262 | 			ei->i_da_metadata_calc_len = 0; | 
 | 263 | 		} else | 
 | 264 | 			ei->i_da_metadata_calc_len++; | 
 | 265 | 		ei->i_da_metadata_calc_last_lblock++; | 
 | 266 | 		return num; | 
 | 267 | 	} | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 268 |  | 
| Theodore Ts'o | 9d0be50 | 2010-01-01 02:41:30 -0500 | [diff] [blame] | 269 | 	/* | 
 | 270 | 	 * In the worst case we need a new set of index blocks at | 
 | 271 | 	 * every level of the inode's extent tree. | 
 | 272 | 	 */ | 
 | 273 | 	ei->i_da_metadata_calc_len = 1; | 
 | 274 | 	ei->i_da_metadata_calc_last_lblock = lblock; | 
 | 275 | 	return ext_depth(inode) + 1; | 
| Mingming Cao | d2a1763 | 2008-07-14 17:52:37 -0400 | [diff] [blame] | 276 | } | 
 | 277 |  | 
| Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 278 | static int | 
 | 279 | ext4_ext_max_entries(struct inode *inode, int depth) | 
 | 280 | { | 
 | 281 | 	int max; | 
 | 282 |  | 
 | 283 | 	if (depth == ext_depth(inode)) { | 
 | 284 | 		if (depth == 0) | 
| Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 285 | 			max = ext4_ext_space_root(inode, 1); | 
| Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 286 | 		else | 
| Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 287 | 			max = ext4_ext_space_root_idx(inode, 1); | 
| Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 288 | 	} else { | 
 | 289 | 		if (depth == 0) | 
| Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 290 | 			max = ext4_ext_space_block(inode, 1); | 
| Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 291 | 		else | 
| Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 292 | 			max = ext4_ext_space_block_idx(inode, 1); | 
| Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 293 | 	} | 
 | 294 |  | 
 | 295 | 	return max; | 
 | 296 | } | 
 | 297 |  | 
| Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 298 | static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext) | 
 | 299 | { | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 300 | 	ext4_fsblk_t block = ext4_ext_pblock(ext); | 
| Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 301 | 	int len = ext4_ext_get_actual_len(ext); | 
| Theodore Ts'o | e84a26c | 2009-04-22 20:52:25 -0400 | [diff] [blame] | 302 |  | 
| Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 303 | 	return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len); | 
| Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 304 | } | 
 | 305 |  | 
 | 306 | static int ext4_valid_extent_idx(struct inode *inode, | 
 | 307 | 				struct ext4_extent_idx *ext_idx) | 
 | 308 | { | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 309 | 	ext4_fsblk_t block = ext4_idx_pblock(ext_idx); | 
| Theodore Ts'o | e84a26c | 2009-04-22 20:52:25 -0400 | [diff] [blame] | 310 |  | 
| Theodore Ts'o | 6fd058f | 2009-05-17 15:38:01 -0400 | [diff] [blame] | 311 | 	return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1); | 
| Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 312 | } | 
 | 313 |  | 
 | 314 | static int ext4_valid_extent_entries(struct inode *inode, | 
 | 315 | 				struct ext4_extent_header *eh, | 
 | 316 | 				int depth) | 
 | 317 | { | 
| Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 318 | 	unsigned short entries; | 
 | 319 | 	if (eh->eh_entries == 0) | 
 | 320 | 		return 1; | 
 | 321 |  | 
 | 322 | 	entries = le16_to_cpu(eh->eh_entries); | 
 | 323 |  | 
 | 324 | 	if (depth == 0) { | 
 | 325 | 		/* leaf entries */ | 
| Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 326 | 		struct ext4_extent *ext = EXT_FIRST_EXTENT(eh); | 
| Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 327 | 		while (entries) { | 
 | 328 | 			if (!ext4_valid_extent(inode, ext)) | 
 | 329 | 				return 0; | 
 | 330 | 			ext++; | 
 | 331 | 			entries--; | 
 | 332 | 		} | 
 | 333 | 	} else { | 
| Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 334 | 		struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh); | 
| Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 335 | 		while (entries) { | 
 | 336 | 			if (!ext4_valid_extent_idx(inode, ext_idx)) | 
 | 337 | 				return 0; | 
 | 338 | 			ext_idx++; | 
 | 339 | 			entries--; | 
 | 340 | 		} | 
 | 341 | 	} | 
 | 342 | 	return 1; | 
 | 343 | } | 
 | 344 |  | 
| Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 345 | static int __ext4_ext_check(const char *function, unsigned int line, | 
 | 346 | 			    struct inode *inode, struct ext4_extent_header *eh, | 
 | 347 | 			    int depth) | 
| Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 348 | { | 
 | 349 | 	const char *error_msg; | 
 | 350 | 	int max = 0; | 
 | 351 |  | 
 | 352 | 	if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) { | 
 | 353 | 		error_msg = "invalid magic"; | 
 | 354 | 		goto corrupted; | 
 | 355 | 	} | 
 | 356 | 	if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) { | 
 | 357 | 		error_msg = "unexpected eh_depth"; | 
 | 358 | 		goto corrupted; | 
 | 359 | 	} | 
 | 360 | 	if (unlikely(eh->eh_max == 0)) { | 
 | 361 | 		error_msg = "invalid eh_max"; | 
 | 362 | 		goto corrupted; | 
 | 363 | 	} | 
 | 364 | 	max = ext4_ext_max_entries(inode, depth); | 
 | 365 | 	if (unlikely(le16_to_cpu(eh->eh_max) > max)) { | 
 | 366 | 		error_msg = "too large eh_max"; | 
 | 367 | 		goto corrupted; | 
 | 368 | 	} | 
 | 369 | 	if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) { | 
 | 370 | 		error_msg = "invalid eh_entries"; | 
 | 371 | 		goto corrupted; | 
 | 372 | 	} | 
| Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 373 | 	if (!ext4_valid_extent_entries(inode, eh, depth)) { | 
 | 374 | 		error_msg = "invalid extent entries"; | 
 | 375 | 		goto corrupted; | 
 | 376 | 	} | 
| Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 377 | 	return 0; | 
 | 378 |  | 
 | 379 | corrupted: | 
| Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 380 | 	ext4_error_inode(inode, function, line, 0, | 
| Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 381 | 			"bad header/extent: %s - magic %x, " | 
| Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 382 | 			"entries %u, max %u(%u), depth %u(%u)", | 
| Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 383 | 			error_msg, le16_to_cpu(eh->eh_magic), | 
| Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 384 | 			le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max), | 
 | 385 | 			max, le16_to_cpu(eh->eh_depth), depth); | 
 | 386 |  | 
 | 387 | 	return -EIO; | 
 | 388 | } | 
 | 389 |  | 
| Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 390 | #define ext4_ext_check(inode, eh, depth)	\ | 
| Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 391 | 	__ext4_ext_check(__func__, __LINE__, inode, eh, depth) | 
| Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 392 |  | 
| Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 393 | int ext4_ext_check_inode(struct inode *inode) | 
 | 394 | { | 
 | 395 | 	return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode)); | 
 | 396 | } | 
 | 397 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 398 | #ifdef EXT_DEBUG | 
 | 399 | static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) | 
 | 400 | { | 
 | 401 | 	int k, l = path->p_depth; | 
 | 402 |  | 
 | 403 | 	ext_debug("path:"); | 
 | 404 | 	for (k = 0; k <= l; k++, path++) { | 
 | 405 | 		if (path->p_idx) { | 
| Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 406 | 		  ext_debug("  %d->%llu", le32_to_cpu(path->p_idx->ei_block), | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 407 | 			    ext4_idx_pblock(path->p_idx)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 408 | 		} else if (path->p_ext) { | 
| Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 409 | 			ext_debug("  %d:[%d]%d:%llu ", | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 410 | 				  le32_to_cpu(path->p_ext->ee_block), | 
| Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 411 | 				  ext4_ext_is_uninitialized(path->p_ext), | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 412 | 				  ext4_ext_get_actual_len(path->p_ext), | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 413 | 				  ext4_ext_pblock(path->p_ext)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 414 | 		} else | 
 | 415 | 			ext_debug("  []"); | 
 | 416 | 	} | 
 | 417 | 	ext_debug("\n"); | 
 | 418 | } | 
 | 419 |  | 
 | 420 | static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) | 
 | 421 | { | 
 | 422 | 	int depth = ext_depth(inode); | 
 | 423 | 	struct ext4_extent_header *eh; | 
 | 424 | 	struct ext4_extent *ex; | 
 | 425 | 	int i; | 
 | 426 |  | 
 | 427 | 	if (!path) | 
 | 428 | 		return; | 
 | 429 |  | 
 | 430 | 	eh = path[depth].p_hdr; | 
 | 431 | 	ex = EXT_FIRST_EXTENT(eh); | 
 | 432 |  | 
| Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 433 | 	ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino); | 
 | 434 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 435 | 	for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { | 
| Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 436 | 		ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block), | 
 | 437 | 			  ext4_ext_is_uninitialized(ex), | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 438 | 			  ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 439 | 	} | 
 | 440 | 	ext_debug("\n"); | 
 | 441 | } | 
| Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 442 |  | 
 | 443 | static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path, | 
 | 444 | 			ext4_fsblk_t newblock, int level) | 
 | 445 | { | 
 | 446 | 	int depth = ext_depth(inode); | 
 | 447 | 	struct ext4_extent *ex; | 
 | 448 |  | 
 | 449 | 	if (depth != level) { | 
 | 450 | 		struct ext4_extent_idx *idx; | 
 | 451 | 		idx = path[level].p_idx; | 
 | 452 | 		while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) { | 
 | 453 | 			ext_debug("%d: move %d:%llu in new index %llu\n", level, | 
 | 454 | 					le32_to_cpu(idx->ei_block), | 
 | 455 | 					ext4_idx_pblock(idx), | 
 | 456 | 					newblock); | 
 | 457 | 			idx++; | 
 | 458 | 		} | 
 | 459 |  | 
 | 460 | 		return; | 
 | 461 | 	} | 
 | 462 |  | 
 | 463 | 	ex = path[depth].p_ext; | 
 | 464 | 	while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) { | 
 | 465 | 		ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n", | 
 | 466 | 				le32_to_cpu(ex->ee_block), | 
 | 467 | 				ext4_ext_pblock(ex), | 
 | 468 | 				ext4_ext_is_uninitialized(ex), | 
 | 469 | 				ext4_ext_get_actual_len(ex), | 
 | 470 | 				newblock); | 
 | 471 | 		ex++; | 
 | 472 | 	} | 
 | 473 | } | 
 | 474 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 475 | #else | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 476 | #define ext4_ext_show_path(inode, path) | 
 | 477 | #define ext4_ext_show_leaf(inode, path) | 
| Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 478 | #define ext4_ext_show_move(inode, path, newblock, level) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 479 | #endif | 
 | 480 |  | 
| Aneesh Kumar K.V | b35905c | 2008-02-25 16:54:37 -0500 | [diff] [blame] | 481 | void ext4_ext_drop_refs(struct ext4_ext_path *path) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 482 | { | 
 | 483 | 	int depth = path->p_depth; | 
 | 484 | 	int i; | 
 | 485 |  | 
 | 486 | 	for (i = 0; i <= depth; i++, path++) | 
 | 487 | 		if (path->p_bh) { | 
 | 488 | 			brelse(path->p_bh); | 
 | 489 | 			path->p_bh = NULL; | 
 | 490 | 		} | 
 | 491 | } | 
 | 492 |  | 
 | 493 | /* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 494 |  * ext4_ext_binsearch_idx: | 
 | 495 |  * binary search for the closest index of the given block | 
| Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 496 |  * the header must be checked before calling this | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 497 |  */ | 
 | 498 | static void | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 499 | ext4_ext_binsearch_idx(struct inode *inode, | 
 | 500 | 			struct ext4_ext_path *path, ext4_lblk_t block) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 501 | { | 
 | 502 | 	struct ext4_extent_header *eh = path->p_hdr; | 
 | 503 | 	struct ext4_extent_idx *r, *l, *m; | 
 | 504 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 505 |  | 
| Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 506 | 	ext_debug("binsearch for %u(idx):  ", block); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 507 |  | 
 | 508 | 	l = EXT_FIRST_INDEX(eh) + 1; | 
| Dmitry Monakhov | e9f410b | 2007-07-18 09:09:15 -0400 | [diff] [blame] | 509 | 	r = EXT_LAST_INDEX(eh); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 510 | 	while (l <= r) { | 
 | 511 | 		m = l + (r - l) / 2; | 
 | 512 | 		if (block < le32_to_cpu(m->ei_block)) | 
 | 513 | 			r = m - 1; | 
 | 514 | 		else | 
 | 515 | 			l = m + 1; | 
| Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 516 | 		ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block), | 
 | 517 | 				m, le32_to_cpu(m->ei_block), | 
 | 518 | 				r, le32_to_cpu(r->ei_block)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 519 | 	} | 
 | 520 |  | 
 | 521 | 	path->p_idx = l - 1; | 
| Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 522 | 	ext_debug("  -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block), | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 523 | 		  ext4_idx_pblock(path->p_idx)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 524 |  | 
 | 525 | #ifdef CHECK_BINSEARCH | 
 | 526 | 	{ | 
 | 527 | 		struct ext4_extent_idx *chix, *ix; | 
 | 528 | 		int k; | 
 | 529 |  | 
 | 530 | 		chix = ix = EXT_FIRST_INDEX(eh); | 
 | 531 | 		for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) { | 
 | 532 | 		  if (k != 0 && | 
 | 533 | 		      le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) { | 
| Theodore Ts'o | 4776004 | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 534 | 				printk(KERN_DEBUG "k=%d, ix=0x%p, " | 
 | 535 | 				       "first=0x%p\n", k, | 
 | 536 | 				       ix, EXT_FIRST_INDEX(eh)); | 
 | 537 | 				printk(KERN_DEBUG "%u <= %u\n", | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 538 | 				       le32_to_cpu(ix->ei_block), | 
 | 539 | 				       le32_to_cpu(ix[-1].ei_block)); | 
 | 540 | 			} | 
 | 541 | 			BUG_ON(k && le32_to_cpu(ix->ei_block) | 
| Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 542 | 					   <= le32_to_cpu(ix[-1].ei_block)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 543 | 			if (block < le32_to_cpu(ix->ei_block)) | 
 | 544 | 				break; | 
 | 545 | 			chix = ix; | 
 | 546 | 		} | 
 | 547 | 		BUG_ON(chix != path->p_idx); | 
 | 548 | 	} | 
 | 549 | #endif | 
 | 550 |  | 
 | 551 | } | 
 | 552 |  | 
 | 553 | /* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 554 |  * ext4_ext_binsearch: | 
 | 555 |  * binary search for closest extent of the given block | 
| Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 556 |  * the header must be checked before calling this | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 557 |  */ | 
 | 558 | static void | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 559 | ext4_ext_binsearch(struct inode *inode, | 
 | 560 | 		struct ext4_ext_path *path, ext4_lblk_t block) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 561 | { | 
 | 562 | 	struct ext4_extent_header *eh = path->p_hdr; | 
 | 563 | 	struct ext4_extent *r, *l, *m; | 
 | 564 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 565 | 	if (eh->eh_entries == 0) { | 
 | 566 | 		/* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 567 | 		 * this leaf is empty: | 
 | 568 | 		 * we get such a leaf in split/add case | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 569 | 		 */ | 
 | 570 | 		return; | 
 | 571 | 	} | 
 | 572 |  | 
| Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 573 | 	ext_debug("binsearch for %u:  ", block); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 574 |  | 
 | 575 | 	l = EXT_FIRST_EXTENT(eh) + 1; | 
| Dmitry Monakhov | e9f410b | 2007-07-18 09:09:15 -0400 | [diff] [blame] | 576 | 	r = EXT_LAST_EXTENT(eh); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 577 |  | 
 | 578 | 	while (l <= r) { | 
 | 579 | 		m = l + (r - l) / 2; | 
 | 580 | 		if (block < le32_to_cpu(m->ee_block)) | 
 | 581 | 			r = m - 1; | 
 | 582 | 		else | 
 | 583 | 			l = m + 1; | 
| Dmitry Monakhov | 26d535e | 2007-07-18 08:33:37 -0400 | [diff] [blame] | 584 | 		ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block), | 
 | 585 | 				m, le32_to_cpu(m->ee_block), | 
 | 586 | 				r, le32_to_cpu(r->ee_block)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 587 | 	} | 
 | 588 |  | 
 | 589 | 	path->p_ext = l - 1; | 
| Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 590 | 	ext_debug("  -> %d:%llu:[%d]%d ", | 
| Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 591 | 			le32_to_cpu(path->p_ext->ee_block), | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 592 | 			ext4_ext_pblock(path->p_ext), | 
| Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 593 | 			ext4_ext_is_uninitialized(path->p_ext), | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 594 | 			ext4_ext_get_actual_len(path->p_ext)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 595 |  | 
 | 596 | #ifdef CHECK_BINSEARCH | 
 | 597 | 	{ | 
 | 598 | 		struct ext4_extent *chex, *ex; | 
 | 599 | 		int k; | 
 | 600 |  | 
 | 601 | 		chex = ex = EXT_FIRST_EXTENT(eh); | 
 | 602 | 		for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) { | 
 | 603 | 			BUG_ON(k && le32_to_cpu(ex->ee_block) | 
| Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 604 | 					  <= le32_to_cpu(ex[-1].ee_block)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 605 | 			if (block < le32_to_cpu(ex->ee_block)) | 
 | 606 | 				break; | 
 | 607 | 			chex = ex; | 
 | 608 | 		} | 
 | 609 | 		BUG_ON(chex != path->p_ext); | 
 | 610 | 	} | 
 | 611 | #endif | 
 | 612 |  | 
 | 613 | } | 
 | 614 |  | 
 | 615 | int ext4_ext_tree_init(handle_t *handle, struct inode *inode) | 
 | 616 | { | 
 | 617 | 	struct ext4_extent_header *eh; | 
 | 618 |  | 
 | 619 | 	eh = ext_inode_hdr(inode); | 
 | 620 | 	eh->eh_depth = 0; | 
 | 621 | 	eh->eh_entries = 0; | 
 | 622 | 	eh->eh_magic = EXT4_EXT_MAGIC; | 
| Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 623 | 	eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 624 | 	ext4_mark_inode_dirty(handle, inode); | 
 | 625 | 	ext4_ext_invalidate_cache(inode); | 
 | 626 | 	return 0; | 
 | 627 | } | 
 | 628 |  | 
 | 629 | struct ext4_ext_path * | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 630 | ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, | 
 | 631 | 					struct ext4_ext_path *path) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 632 | { | 
 | 633 | 	struct ext4_extent_header *eh; | 
 | 634 | 	struct buffer_head *bh; | 
 | 635 | 	short int depth, i, ppos = 0, alloc = 0; | 
 | 636 |  | 
 | 637 | 	eh = ext_inode_hdr(inode); | 
| Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 638 | 	depth = ext_depth(inode); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 639 |  | 
 | 640 | 	/* account possible depth increase */ | 
 | 641 | 	if (!path) { | 
| Avantika Mathur | 5d4958f | 2006-12-06 20:41:35 -0800 | [diff] [blame] | 642 | 		path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2), | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 643 | 				GFP_NOFS); | 
 | 644 | 		if (!path) | 
 | 645 | 			return ERR_PTR(-ENOMEM); | 
 | 646 | 		alloc = 1; | 
 | 647 | 	} | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 648 | 	path[0].p_hdr = eh; | 
| Shen Feng | 1973adc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 649 | 	path[0].p_bh = NULL; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 650 |  | 
| Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 651 | 	i = depth; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 652 | 	/* walk through the tree */ | 
 | 653 | 	while (i) { | 
| Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 654 | 		int need_to_validate = 0; | 
 | 655 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 656 | 		ext_debug("depth %d: num %d, max %d\n", | 
 | 657 | 			  ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); | 
| Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 658 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 659 | 		ext4_ext_binsearch_idx(inode, path + ppos, block); | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 660 | 		path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 661 | 		path[ppos].p_depth = i; | 
 | 662 | 		path[ppos].p_ext = NULL; | 
 | 663 |  | 
| Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 664 | 		bh = sb_getblk(inode->i_sb, path[ppos].p_block); | 
 | 665 | 		if (unlikely(!bh)) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 666 | 			goto err; | 
| Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 667 | 		if (!bh_uptodate_or_lock(bh)) { | 
| Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 668 | 			trace_ext4_ext_load_extent(inode, block, | 
 | 669 | 						path[ppos].p_block); | 
| Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 670 | 			if (bh_submit_read(bh) < 0) { | 
 | 671 | 				put_bh(bh); | 
 | 672 | 				goto err; | 
 | 673 | 			} | 
 | 674 | 			/* validate the extent entries */ | 
 | 675 | 			need_to_validate = 1; | 
 | 676 | 		} | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 677 | 		eh = ext_block_hdr(bh); | 
 | 678 | 		ppos++; | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 679 | 		if (unlikely(ppos > depth)) { | 
 | 680 | 			put_bh(bh); | 
 | 681 | 			EXT4_ERROR_INODE(inode, | 
 | 682 | 					 "ppos %d > depth %d", ppos, depth); | 
 | 683 | 			goto err; | 
 | 684 | 		} | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 685 | 		path[ppos].p_bh = bh; | 
 | 686 | 		path[ppos].p_hdr = eh; | 
 | 687 | 		i--; | 
 | 688 |  | 
| Aneesh Kumar K.V | 7a262f7 | 2009-03-27 16:39:58 -0400 | [diff] [blame] | 689 | 		if (need_to_validate && ext4_ext_check(inode, eh, i)) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 690 | 			goto err; | 
 | 691 | 	} | 
 | 692 |  | 
 | 693 | 	path[ppos].p_depth = i; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 694 | 	path[ppos].p_ext = NULL; | 
 | 695 | 	path[ppos].p_idx = NULL; | 
 | 696 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 697 | 	/* find extent */ | 
 | 698 | 	ext4_ext_binsearch(inode, path + ppos, block); | 
| Shen Feng | 1973adc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 699 | 	/* if not an empty leaf */ | 
 | 700 | 	if (path[ppos].p_ext) | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 701 | 		path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 702 |  | 
 | 703 | 	ext4_ext_show_path(inode, path); | 
 | 704 |  | 
 | 705 | 	return path; | 
 | 706 |  | 
 | 707 | err: | 
 | 708 | 	ext4_ext_drop_refs(path); | 
 | 709 | 	if (alloc) | 
 | 710 | 		kfree(path); | 
 | 711 | 	return ERR_PTR(-EIO); | 
 | 712 | } | 
 | 713 |  | 
 | 714 | /* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 715 |  * ext4_ext_insert_index: | 
 | 716 |  * insert new index [@logical;@ptr] into the block at @curp; | 
 | 717 |  * check where to insert: before @curp or after @curp | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 718 |  */ | 
| Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 719 | static int ext4_ext_insert_index(handle_t *handle, struct inode *inode, | 
 | 720 | 				 struct ext4_ext_path *curp, | 
 | 721 | 				 int logical, ext4_fsblk_t ptr) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 722 | { | 
 | 723 | 	struct ext4_extent_idx *ix; | 
 | 724 | 	int len, err; | 
 | 725 |  | 
| Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 726 | 	err = ext4_ext_get_access(handle, inode, curp); | 
 | 727 | 	if (err) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 728 | 		return err; | 
 | 729 |  | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 730 | 	if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) { | 
 | 731 | 		EXT4_ERROR_INODE(inode, | 
 | 732 | 				 "logical %d == ei_block %d!", | 
 | 733 | 				 logical, le32_to_cpu(curp->p_idx->ei_block)); | 
 | 734 | 		return -EIO; | 
 | 735 | 	} | 
| Robin Dong | d462031 | 2011-07-17 23:43:42 -0400 | [diff] [blame] | 736 |  | 
 | 737 | 	if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries) | 
 | 738 | 			     >= le16_to_cpu(curp->p_hdr->eh_max))) { | 
 | 739 | 		EXT4_ERROR_INODE(inode, | 
 | 740 | 				 "eh_entries %d >= eh_max %d!", | 
 | 741 | 				 le16_to_cpu(curp->p_hdr->eh_entries), | 
 | 742 | 				 le16_to_cpu(curp->p_hdr->eh_max)); | 
 | 743 | 		return -EIO; | 
 | 744 | 	} | 
 | 745 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 746 | 	if (logical > le32_to_cpu(curp->p_idx->ei_block)) { | 
 | 747 | 		/* insert after */ | 
| Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 748 | 		ext_debug("insert new index %d after: %llu\n", logical, ptr); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 749 | 		ix = curp->p_idx + 1; | 
 | 750 | 	} else { | 
 | 751 | 		/* insert before */ | 
| Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 752 | 		ext_debug("insert new index %d before: %llu\n", logical, ptr); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 753 | 		ix = curp->p_idx; | 
 | 754 | 	} | 
 | 755 |  | 
| Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 756 | 	len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1; | 
 | 757 | 	BUG_ON(len < 0); | 
 | 758 | 	if (len > 0) { | 
 | 759 | 		ext_debug("insert new index %d: " | 
 | 760 | 				"move %d indices from 0x%p to 0x%p\n", | 
 | 761 | 				logical, len, ix, ix + 1); | 
 | 762 | 		memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx)); | 
 | 763 | 	} | 
 | 764 |  | 
| Tao Ma | f472e02 | 2011-10-17 10:13:46 -0400 | [diff] [blame] | 765 | 	if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) { | 
 | 766 | 		EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!"); | 
 | 767 | 		return -EIO; | 
 | 768 | 	} | 
 | 769 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 770 | 	ix->ei_block = cpu_to_le32(logical); | 
| Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 771 | 	ext4_idx_store_pblock(ix, ptr); | 
| Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 772 | 	le16_add_cpu(&curp->p_hdr->eh_entries, 1); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 773 |  | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 774 | 	if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) { | 
 | 775 | 		EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!"); | 
 | 776 | 		return -EIO; | 
 | 777 | 	} | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 778 |  | 
 | 779 | 	err = ext4_ext_dirty(handle, inode, curp); | 
 | 780 | 	ext4_std_error(inode->i_sb, err); | 
 | 781 |  | 
 | 782 | 	return err; | 
 | 783 | } | 
 | 784 |  | 
 | 785 | /* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 786 |  * ext4_ext_split: | 
 | 787 |  * inserts new subtree into the path, using free index entry | 
 | 788 |  * at depth @at: | 
 | 789 |  * - allocates all needed blocks (new leaf and all intermediate index blocks) | 
 | 790 |  * - makes decision where to split | 
 | 791 |  * - moves remaining extents and index entries (right to the split point) | 
 | 792 |  *   into the newly allocated blocks | 
 | 793 |  * - initializes subtree | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 794 |  */ | 
 | 795 | static int ext4_ext_split(handle_t *handle, struct inode *inode, | 
| Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 796 | 			  unsigned int flags, | 
 | 797 | 			  struct ext4_ext_path *path, | 
 | 798 | 			  struct ext4_extent *newext, int at) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 799 | { | 
 | 800 | 	struct buffer_head *bh = NULL; | 
 | 801 | 	int depth = ext_depth(inode); | 
 | 802 | 	struct ext4_extent_header *neh; | 
 | 803 | 	struct ext4_extent_idx *fidx; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 804 | 	int i = at, k, m, a; | 
| Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 805 | 	ext4_fsblk_t newblock, oldblock; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 806 | 	__le32 border; | 
| Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 807 | 	ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */ | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 808 | 	int err = 0; | 
 | 809 |  | 
 | 810 | 	/* make decision: where to split? */ | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 811 | 	/* FIXME: now decision is simplest: at current extent */ | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 812 |  | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 813 | 	/* if current leaf will be split, then we should use | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 814 | 	 * border from split point */ | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 815 | 	if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) { | 
 | 816 | 		EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!"); | 
 | 817 | 		return -EIO; | 
 | 818 | 	} | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 819 | 	if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) { | 
 | 820 | 		border = path[depth].p_ext[1].ee_block; | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 821 | 		ext_debug("leaf will be split." | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 822 | 				" next leaf starts at %d\n", | 
| Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 823 | 				  le32_to_cpu(border)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 824 | 	} else { | 
 | 825 | 		border = newext->ee_block; | 
 | 826 | 		ext_debug("leaf will be added." | 
 | 827 | 				" next leaf starts at %d\n", | 
| Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 828 | 				le32_to_cpu(border)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 829 | 	} | 
 | 830 |  | 
 | 831 | 	/* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 832 | 	 * If error occurs, then we break processing | 
 | 833 | 	 * and mark filesystem read-only. index won't | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 834 | 	 * be inserted and tree will be in consistent | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 835 | 	 * state. Next mount will repair buffers too. | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 836 | 	 */ | 
 | 837 |  | 
 | 838 | 	/* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 839 | 	 * Get array to track all allocated blocks. | 
 | 840 | 	 * We need this to handle errors and free blocks | 
 | 841 | 	 * upon them. | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 842 | 	 */ | 
| Avantika Mathur | 5d4958f | 2006-12-06 20:41:35 -0800 | [diff] [blame] | 843 | 	ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 844 | 	if (!ablocks) | 
 | 845 | 		return -ENOMEM; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 846 |  | 
 | 847 | 	/* allocate all needed blocks */ | 
 | 848 | 	ext_debug("allocate %d blocks for indexes/leaf\n", depth - at); | 
 | 849 | 	for (a = 0; a < depth - at; a++) { | 
| Aneesh Kumar K.V | 654b490 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 850 | 		newblock = ext4_ext_new_meta_block(handle, inode, path, | 
| Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 851 | 						   newext, &err, flags); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 852 | 		if (newblock == 0) | 
 | 853 | 			goto cleanup; | 
 | 854 | 		ablocks[a] = newblock; | 
 | 855 | 	} | 
 | 856 |  | 
 | 857 | 	/* initialize new leaf */ | 
 | 858 | 	newblock = ablocks[--a]; | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 859 | 	if (unlikely(newblock == 0)) { | 
 | 860 | 		EXT4_ERROR_INODE(inode, "newblock == 0!"); | 
 | 861 | 		err = -EIO; | 
 | 862 | 		goto cleanup; | 
 | 863 | 	} | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 864 | 	bh = sb_getblk(inode->i_sb, newblock); | 
 | 865 | 	if (!bh) { | 
 | 866 | 		err = -EIO; | 
 | 867 | 		goto cleanup; | 
 | 868 | 	} | 
 | 869 | 	lock_buffer(bh); | 
 | 870 |  | 
| Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 871 | 	err = ext4_journal_get_create_access(handle, bh); | 
 | 872 | 	if (err) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 873 | 		goto cleanup; | 
 | 874 |  | 
 | 875 | 	neh = ext_block_hdr(bh); | 
 | 876 | 	neh->eh_entries = 0; | 
| Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 877 | 	neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 878 | 	neh->eh_magic = EXT4_EXT_MAGIC; | 
 | 879 | 	neh->eh_depth = 0; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 880 |  | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 881 | 	/* move remainder of path[depth] to the new leaf */ | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 882 | 	if (unlikely(path[depth].p_hdr->eh_entries != | 
 | 883 | 		     path[depth].p_hdr->eh_max)) { | 
 | 884 | 		EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!", | 
 | 885 | 				 path[depth].p_hdr->eh_entries, | 
 | 886 | 				 path[depth].p_hdr->eh_max); | 
 | 887 | 		err = -EIO; | 
 | 888 | 		goto cleanup; | 
 | 889 | 	} | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 890 | 	/* start copy from next extent */ | 
| Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 891 | 	m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++; | 
 | 892 | 	ext4_ext_show_move(inode, path, newblock, depth); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 893 | 	if (m) { | 
| Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 894 | 		struct ext4_extent *ex; | 
 | 895 | 		ex = EXT_FIRST_EXTENT(neh); | 
 | 896 | 		memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m); | 
| Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 897 | 		le16_add_cpu(&neh->eh_entries, m); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 898 | 	} | 
 | 899 |  | 
 | 900 | 	set_buffer_uptodate(bh); | 
 | 901 | 	unlock_buffer(bh); | 
 | 902 |  | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 903 | 	err = ext4_handle_dirty_metadata(handle, inode, bh); | 
| Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 904 | 	if (err) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 905 | 		goto cleanup; | 
 | 906 | 	brelse(bh); | 
 | 907 | 	bh = NULL; | 
 | 908 |  | 
 | 909 | 	/* correct old leaf */ | 
 | 910 | 	if (m) { | 
| Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 911 | 		err = ext4_ext_get_access(handle, inode, path + depth); | 
 | 912 | 		if (err) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 913 | 			goto cleanup; | 
| Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 914 | 		le16_add_cpu(&path[depth].p_hdr->eh_entries, -m); | 
| Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 915 | 		err = ext4_ext_dirty(handle, inode, path + depth); | 
 | 916 | 		if (err) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 917 | 			goto cleanup; | 
 | 918 |  | 
 | 919 | 	} | 
 | 920 |  | 
 | 921 | 	/* create intermediate indexes */ | 
 | 922 | 	k = depth - at - 1; | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 923 | 	if (unlikely(k < 0)) { | 
 | 924 | 		EXT4_ERROR_INODE(inode, "k %d < 0!", k); | 
 | 925 | 		err = -EIO; | 
 | 926 | 		goto cleanup; | 
 | 927 | 	} | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 928 | 	if (k) | 
 | 929 | 		ext_debug("create %d intermediate indices\n", k); | 
 | 930 | 	/* insert new index into current index block */ | 
 | 931 | 	/* current depth stored in i var */ | 
 | 932 | 	i = depth - 1; | 
 | 933 | 	while (k--) { | 
 | 934 | 		oldblock = newblock; | 
 | 935 | 		newblock = ablocks[--a]; | 
| Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 936 | 		bh = sb_getblk(inode->i_sb, newblock); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 937 | 		if (!bh) { | 
 | 938 | 			err = -EIO; | 
 | 939 | 			goto cleanup; | 
 | 940 | 		} | 
 | 941 | 		lock_buffer(bh); | 
 | 942 |  | 
| Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 943 | 		err = ext4_journal_get_create_access(handle, bh); | 
 | 944 | 		if (err) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 945 | 			goto cleanup; | 
 | 946 |  | 
 | 947 | 		neh = ext_block_hdr(bh); | 
 | 948 | 		neh->eh_entries = cpu_to_le16(1); | 
 | 949 | 		neh->eh_magic = EXT4_EXT_MAGIC; | 
| Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 950 | 		neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 951 | 		neh->eh_depth = cpu_to_le16(depth - i); | 
 | 952 | 		fidx = EXT_FIRST_INDEX(neh); | 
 | 953 | 		fidx->ei_block = border; | 
| Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 954 | 		ext4_idx_store_pblock(fidx, oldblock); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 955 |  | 
| Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 956 | 		ext_debug("int.index at %d (block %llu): %u -> %llu\n", | 
 | 957 | 				i, newblock, le32_to_cpu(border), oldblock); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 958 |  | 
| Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 959 | 		/* move remainder of path[i] to the new index block */ | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 960 | 		if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) != | 
 | 961 | 					EXT_LAST_INDEX(path[i].p_hdr))) { | 
 | 962 | 			EXT4_ERROR_INODE(inode, | 
 | 963 | 					 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!", | 
 | 964 | 					 le32_to_cpu(path[i].p_ext->ee_block)); | 
 | 965 | 			err = -EIO; | 
 | 966 | 			goto cleanup; | 
 | 967 | 		} | 
| Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 968 | 		/* start copy indexes */ | 
 | 969 | 		m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++; | 
 | 970 | 		ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx, | 
 | 971 | 				EXT_MAX_INDEX(path[i].p_hdr)); | 
 | 972 | 		ext4_ext_show_move(inode, path, newblock, i); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 973 | 		if (m) { | 
| Yongqiang Yang | 1b16da7 | 2011-05-25 17:41:48 -0400 | [diff] [blame] | 974 | 			memmove(++fidx, path[i].p_idx, | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 975 | 				sizeof(struct ext4_extent_idx) * m); | 
| Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 976 | 			le16_add_cpu(&neh->eh_entries, m); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 977 | 		} | 
 | 978 | 		set_buffer_uptodate(bh); | 
 | 979 | 		unlock_buffer(bh); | 
 | 980 |  | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 981 | 		err = ext4_handle_dirty_metadata(handle, inode, bh); | 
| Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 982 | 		if (err) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 983 | 			goto cleanup; | 
 | 984 | 		brelse(bh); | 
 | 985 | 		bh = NULL; | 
 | 986 |  | 
 | 987 | 		/* correct old index */ | 
 | 988 | 		if (m) { | 
 | 989 | 			err = ext4_ext_get_access(handle, inode, path + i); | 
 | 990 | 			if (err) | 
 | 991 | 				goto cleanup; | 
| Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 992 | 			le16_add_cpu(&path[i].p_hdr->eh_entries, -m); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 993 | 			err = ext4_ext_dirty(handle, inode, path + i); | 
 | 994 | 			if (err) | 
 | 995 | 				goto cleanup; | 
 | 996 | 		} | 
 | 997 |  | 
 | 998 | 		i--; | 
 | 999 | 	} | 
 | 1000 |  | 
 | 1001 | 	/* insert new index */ | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1002 | 	err = ext4_ext_insert_index(handle, inode, path + at, | 
 | 1003 | 				    le32_to_cpu(border), newblock); | 
 | 1004 |  | 
 | 1005 | cleanup: | 
 | 1006 | 	if (bh) { | 
 | 1007 | 		if (buffer_locked(bh)) | 
 | 1008 | 			unlock_buffer(bh); | 
 | 1009 | 		brelse(bh); | 
 | 1010 | 	} | 
 | 1011 |  | 
 | 1012 | 	if (err) { | 
 | 1013 | 		/* free all allocated blocks in error case */ | 
 | 1014 | 		for (i = 0; i < depth; i++) { | 
 | 1015 | 			if (!ablocks[i]) | 
 | 1016 | 				continue; | 
| Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 1017 | 			ext4_free_blocks(handle, inode, NULL, ablocks[i], 1, | 
| Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 1018 | 					 EXT4_FREE_BLOCKS_METADATA); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1019 | 		} | 
 | 1020 | 	} | 
 | 1021 | 	kfree(ablocks); | 
 | 1022 |  | 
 | 1023 | 	return err; | 
 | 1024 | } | 
 | 1025 |  | 
 | 1026 | /* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1027 |  * ext4_ext_grow_indepth: | 
 | 1028 |  * implements tree growing procedure: | 
 | 1029 |  * - allocates new block | 
 | 1030 |  * - moves top-level data (index block or leaf) into the new block | 
 | 1031 |  * - initializes new top-level, creating index that points to the | 
 | 1032 |  *   just created block | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1033 |  */ | 
 | 1034 | static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, | 
| Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1035 | 				 unsigned int flags, | 
| Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1036 | 				 struct ext4_extent *newext) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1037 | { | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1038 | 	struct ext4_extent_header *neh; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1039 | 	struct buffer_head *bh; | 
| Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 1040 | 	ext4_fsblk_t newblock; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1041 | 	int err = 0; | 
 | 1042 |  | 
| Dmitry Monakhov | 1939dd8 | 2011-10-22 01:26:05 -0400 | [diff] [blame] | 1043 | 	newblock = ext4_ext_new_meta_block(handle, inode, NULL, | 
| Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1044 | 		newext, &err, flags); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1045 | 	if (newblock == 0) | 
 | 1046 | 		return err; | 
 | 1047 |  | 
 | 1048 | 	bh = sb_getblk(inode->i_sb, newblock); | 
 | 1049 | 	if (!bh) { | 
 | 1050 | 		err = -EIO; | 
 | 1051 | 		ext4_std_error(inode->i_sb, err); | 
 | 1052 | 		return err; | 
 | 1053 | 	} | 
 | 1054 | 	lock_buffer(bh); | 
 | 1055 |  | 
| Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1056 | 	err = ext4_journal_get_create_access(handle, bh); | 
 | 1057 | 	if (err) { | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1058 | 		unlock_buffer(bh); | 
 | 1059 | 		goto out; | 
 | 1060 | 	} | 
 | 1061 |  | 
 | 1062 | 	/* move top-level index/leaf into new block */ | 
| Dmitry Monakhov | 1939dd8 | 2011-10-22 01:26:05 -0400 | [diff] [blame] | 1063 | 	memmove(bh->b_data, EXT4_I(inode)->i_data, | 
 | 1064 | 		sizeof(EXT4_I(inode)->i_data)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1065 |  | 
 | 1066 | 	/* set size of new block */ | 
 | 1067 | 	neh = ext_block_hdr(bh); | 
 | 1068 | 	/* old root could have indexes or leaves | 
 | 1069 | 	 * so calculate e_max right way */ | 
 | 1070 | 	if (ext_depth(inode)) | 
| Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1071 | 		neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1072 | 	else | 
| Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 1073 | 		neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1074 | 	neh->eh_magic = EXT4_EXT_MAGIC; | 
 | 1075 | 	set_buffer_uptodate(bh); | 
 | 1076 | 	unlock_buffer(bh); | 
 | 1077 |  | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1078 | 	err = ext4_handle_dirty_metadata(handle, inode, bh); | 
| Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1079 | 	if (err) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1080 | 		goto out; | 
 | 1081 |  | 
| Dmitry Monakhov | 1939dd8 | 2011-10-22 01:26:05 -0400 | [diff] [blame] | 1082 | 	/* Update top-level index: num,max,pointer */ | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1083 | 	neh = ext_inode_hdr(inode); | 
| Dmitry Monakhov | 1939dd8 | 2011-10-22 01:26:05 -0400 | [diff] [blame] | 1084 | 	neh->eh_entries = cpu_to_le16(1); | 
 | 1085 | 	ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock); | 
 | 1086 | 	if (neh->eh_depth == 0) { | 
 | 1087 | 		/* Root extent block becomes index block */ | 
 | 1088 | 		neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0)); | 
 | 1089 | 		EXT_FIRST_INDEX(neh)->ei_block = | 
 | 1090 | 			EXT_FIRST_EXTENT(neh)->ee_block; | 
 | 1091 | 	} | 
| Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 1092 | 	ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n", | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1093 | 		  le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), | 
| Andi Kleen | 5a0790c | 2010-06-14 13:28:03 -0400 | [diff] [blame] | 1094 | 		  le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block), | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1095 | 		  ext4_idx_pblock(EXT_FIRST_INDEX(neh))); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1096 |  | 
| Paul Mackerras | b4611ab | 2011-12-12 11:00:56 -0500 | [diff] [blame] | 1097 | 	neh->eh_depth = cpu_to_le16(le16_to_cpu(neh->eh_depth) + 1); | 
| Dmitry Monakhov | 1939dd8 | 2011-10-22 01:26:05 -0400 | [diff] [blame] | 1098 | 	ext4_mark_inode_dirty(handle, inode); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1099 | out: | 
 | 1100 | 	brelse(bh); | 
 | 1101 |  | 
 | 1102 | 	return err; | 
 | 1103 | } | 
 | 1104 |  | 
 | 1105 | /* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1106 |  * ext4_ext_create_new_leaf: | 
 | 1107 |  * finds empty index and adds new leaf. | 
 | 1108 |  * if no free index is found, then it requests in-depth growing. | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1109 |  */ | 
 | 1110 | static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode, | 
| Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1111 | 				    unsigned int flags, | 
 | 1112 | 				    struct ext4_ext_path *path, | 
 | 1113 | 				    struct ext4_extent *newext) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1114 | { | 
 | 1115 | 	struct ext4_ext_path *curp; | 
 | 1116 | 	int depth, i, err = 0; | 
 | 1117 |  | 
 | 1118 | repeat: | 
 | 1119 | 	i = depth = ext_depth(inode); | 
 | 1120 |  | 
 | 1121 | 	/* walk up to the tree and look for free index entry */ | 
 | 1122 | 	curp = path + depth; | 
 | 1123 | 	while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) { | 
 | 1124 | 		i--; | 
 | 1125 | 		curp--; | 
 | 1126 | 	} | 
 | 1127 |  | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1128 | 	/* we use already allocated block for index block, | 
 | 1129 | 	 * so subsequent data blocks should be contiguous */ | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1130 | 	if (EXT_HAS_FREE_INDEX(curp)) { | 
 | 1131 | 		/* if we found index with free entry, then use that | 
 | 1132 | 		 * entry: create all needed subtree and add new leaf */ | 
| Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1133 | 		err = ext4_ext_split(handle, inode, flags, path, newext, i); | 
| Shen Feng | 787e098 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1134 | 		if (err) | 
 | 1135 | 			goto out; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1136 |  | 
 | 1137 | 		/* refill path */ | 
 | 1138 | 		ext4_ext_drop_refs(path); | 
 | 1139 | 		path = ext4_ext_find_extent(inode, | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1140 | 				    (ext4_lblk_t)le32_to_cpu(newext->ee_block), | 
 | 1141 | 				    path); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1142 | 		if (IS_ERR(path)) | 
 | 1143 | 			err = PTR_ERR(path); | 
 | 1144 | 	} else { | 
 | 1145 | 		/* tree is full, time to grow in depth */ | 
| Dmitry Monakhov | 1939dd8 | 2011-10-22 01:26:05 -0400 | [diff] [blame] | 1146 | 		err = ext4_ext_grow_indepth(handle, inode, flags, newext); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1147 | 		if (err) | 
 | 1148 | 			goto out; | 
 | 1149 |  | 
 | 1150 | 		/* refill path */ | 
 | 1151 | 		ext4_ext_drop_refs(path); | 
 | 1152 | 		path = ext4_ext_find_extent(inode, | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1153 | 				   (ext4_lblk_t)le32_to_cpu(newext->ee_block), | 
 | 1154 | 				    path); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1155 | 		if (IS_ERR(path)) { | 
 | 1156 | 			err = PTR_ERR(path); | 
 | 1157 | 			goto out; | 
 | 1158 | 		} | 
 | 1159 |  | 
 | 1160 | 		/* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1161 | 		 * only first (depth 0 -> 1) produces free space; | 
 | 1162 | 		 * in all other cases we have to split the grown tree | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1163 | 		 */ | 
 | 1164 | 		depth = ext_depth(inode); | 
 | 1165 | 		if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) { | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1166 | 			/* now we need to split */ | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1167 | 			goto repeat; | 
 | 1168 | 		} | 
 | 1169 | 	} | 
 | 1170 |  | 
 | 1171 | out: | 
 | 1172 | 	return err; | 
 | 1173 | } | 
 | 1174 |  | 
 | 1175 | /* | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1176 |  * search the closest allocated block to the left for *logical | 
 | 1177 |  * and returns it at @logical + it's physical address at @phys | 
 | 1178 |  * if *logical is the smallest allocated block, the function | 
 | 1179 |  * returns 0 at @phys | 
 | 1180 |  * return value contains 0 (success) or error code | 
 | 1181 |  */ | 
| Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1182 | static int ext4_ext_search_left(struct inode *inode, | 
 | 1183 | 				struct ext4_ext_path *path, | 
 | 1184 | 				ext4_lblk_t *logical, ext4_fsblk_t *phys) | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1185 | { | 
 | 1186 | 	struct ext4_extent_idx *ix; | 
 | 1187 | 	struct ext4_extent *ex; | 
| Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1188 | 	int depth, ee_len; | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1189 |  | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1190 | 	if (unlikely(path == NULL)) { | 
 | 1191 | 		EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); | 
 | 1192 | 		return -EIO; | 
 | 1193 | 	} | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1194 | 	depth = path->p_depth; | 
 | 1195 | 	*phys = 0; | 
 | 1196 |  | 
 | 1197 | 	if (depth == 0 && path->p_ext == NULL) | 
 | 1198 | 		return 0; | 
 | 1199 |  | 
 | 1200 | 	/* usually extent in the path covers blocks smaller | 
 | 1201 | 	 * then *logical, but it can be that extent is the | 
 | 1202 | 	 * first one in the file */ | 
 | 1203 |  | 
 | 1204 | 	ex = path[depth].p_ext; | 
| Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1205 | 	ee_len = ext4_ext_get_actual_len(ex); | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1206 | 	if (*logical < le32_to_cpu(ex->ee_block)) { | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1207 | 		if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { | 
 | 1208 | 			EXT4_ERROR_INODE(inode, | 
 | 1209 | 					 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!", | 
 | 1210 | 					 *logical, le32_to_cpu(ex->ee_block)); | 
 | 1211 | 			return -EIO; | 
 | 1212 | 		} | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1213 | 		while (--depth >= 0) { | 
 | 1214 | 			ix = path[depth].p_idx; | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1215 | 			if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { | 
 | 1216 | 				EXT4_ERROR_INODE(inode, | 
 | 1217 | 				  "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!", | 
| Tao Ma | 6ee3b21 | 2011-10-08 16:08:34 -0400 | [diff] [blame] | 1218 | 				  ix != NULL ? le32_to_cpu(ix->ei_block) : 0, | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1219 | 				  EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ? | 
| Tao Ma | 6ee3b21 | 2011-10-08 16:08:34 -0400 | [diff] [blame] | 1220 | 		le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0, | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1221 | 				  depth); | 
 | 1222 | 				return -EIO; | 
 | 1223 | 			} | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1224 | 		} | 
 | 1225 | 		return 0; | 
 | 1226 | 	} | 
 | 1227 |  | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1228 | 	if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { | 
 | 1229 | 		EXT4_ERROR_INODE(inode, | 
 | 1230 | 				 "logical %d < ee_block %d + ee_len %d!", | 
 | 1231 | 				 *logical, le32_to_cpu(ex->ee_block), ee_len); | 
 | 1232 | 		return -EIO; | 
 | 1233 | 	} | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1234 |  | 
| Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1235 | 	*logical = le32_to_cpu(ex->ee_block) + ee_len - 1; | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1236 | 	*phys = ext4_ext_pblock(ex) + ee_len - 1; | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1237 | 	return 0; | 
 | 1238 | } | 
 | 1239 |  | 
 | 1240 | /* | 
 | 1241 |  * search the closest allocated block to the right for *logical | 
 | 1242 |  * and returns it at @logical + it's physical address at @phys | 
| Tao Ma | df3ab17 | 2011-10-08 15:53:49 -0400 | [diff] [blame] | 1243 |  * if *logical is the largest allocated block, the function | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1244 |  * returns 0 at @phys | 
 | 1245 |  * return value contains 0 (success) or error code | 
 | 1246 |  */ | 
| Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1247 | static int ext4_ext_search_right(struct inode *inode, | 
 | 1248 | 				 struct ext4_ext_path *path, | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1249 | 				 ext4_lblk_t *logical, ext4_fsblk_t *phys, | 
 | 1250 | 				 struct ext4_extent **ret_ex) | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1251 | { | 
 | 1252 | 	struct buffer_head *bh = NULL; | 
 | 1253 | 	struct ext4_extent_header *eh; | 
 | 1254 | 	struct ext4_extent_idx *ix; | 
 | 1255 | 	struct ext4_extent *ex; | 
 | 1256 | 	ext4_fsblk_t block; | 
| Eric Sandeen | 395a87b | 2009-03-10 18:18:47 -0400 | [diff] [blame] | 1257 | 	int depth;	/* Note, NOT eh_depth; depth from top of tree */ | 
 | 1258 | 	int ee_len; | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1259 |  | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1260 | 	if (unlikely(path == NULL)) { | 
 | 1261 | 		EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); | 
 | 1262 | 		return -EIO; | 
 | 1263 | 	} | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1264 | 	depth = path->p_depth; | 
 | 1265 | 	*phys = 0; | 
 | 1266 |  | 
 | 1267 | 	if (depth == 0 && path->p_ext == NULL) | 
 | 1268 | 		return 0; | 
 | 1269 |  | 
 | 1270 | 	/* usually extent in the path covers blocks smaller | 
 | 1271 | 	 * then *logical, but it can be that extent is the | 
 | 1272 | 	 * first one in the file */ | 
 | 1273 |  | 
 | 1274 | 	ex = path[depth].p_ext; | 
| Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1275 | 	ee_len = ext4_ext_get_actual_len(ex); | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1276 | 	if (*logical < le32_to_cpu(ex->ee_block)) { | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1277 | 		if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { | 
 | 1278 | 			EXT4_ERROR_INODE(inode, | 
 | 1279 | 					 "first_extent(path[%d].p_hdr) != ex", | 
 | 1280 | 					 depth); | 
 | 1281 | 			return -EIO; | 
 | 1282 | 		} | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1283 | 		while (--depth >= 0) { | 
 | 1284 | 			ix = path[depth].p_idx; | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1285 | 			if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { | 
 | 1286 | 				EXT4_ERROR_INODE(inode, | 
 | 1287 | 						 "ix != EXT_FIRST_INDEX *logical %d!", | 
 | 1288 | 						 *logical); | 
 | 1289 | 				return -EIO; | 
 | 1290 | 			} | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1291 | 		} | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1292 | 		goto found_extent; | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1293 | 	} | 
 | 1294 |  | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1295 | 	if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { | 
 | 1296 | 		EXT4_ERROR_INODE(inode, | 
 | 1297 | 				 "logical %d < ee_block %d + ee_len %d!", | 
 | 1298 | 				 *logical, le32_to_cpu(ex->ee_block), ee_len); | 
 | 1299 | 		return -EIO; | 
 | 1300 | 	} | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1301 |  | 
 | 1302 | 	if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) { | 
 | 1303 | 		/* next allocated block in this leaf */ | 
 | 1304 | 		ex++; | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1305 | 		goto found_extent; | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1306 | 	} | 
 | 1307 |  | 
 | 1308 | 	/* go up and search for index to the right */ | 
 | 1309 | 	while (--depth >= 0) { | 
 | 1310 | 		ix = path[depth].p_idx; | 
 | 1311 | 		if (ix != EXT_LAST_INDEX(path[depth].p_hdr)) | 
| Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1312 | 			goto got_index; | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1313 | 	} | 
 | 1314 |  | 
| Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1315 | 	/* we've gone up to the root and found no index to the right */ | 
 | 1316 | 	return 0; | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1317 |  | 
| Wu Fengguang | 25f1ee3 | 2008-11-25 17:24:23 -0500 | [diff] [blame] | 1318 | got_index: | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1319 | 	/* we've found index to the right, let's | 
 | 1320 | 	 * follow it and find the closest allocated | 
 | 1321 | 	 * block to the right */ | 
 | 1322 | 	ix++; | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1323 | 	block = ext4_idx_pblock(ix); | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1324 | 	while (++depth < path->p_depth) { | 
 | 1325 | 		bh = sb_bread(inode->i_sb, block); | 
 | 1326 | 		if (bh == NULL) | 
 | 1327 | 			return -EIO; | 
 | 1328 | 		eh = ext_block_hdr(bh); | 
| Eric Sandeen | 395a87b | 2009-03-10 18:18:47 -0400 | [diff] [blame] | 1329 | 		/* subtract from p_depth to get proper eh_depth */ | 
| Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 1330 | 		if (ext4_ext_check(inode, eh, path->p_depth - depth)) { | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1331 | 			put_bh(bh); | 
 | 1332 | 			return -EIO; | 
 | 1333 | 		} | 
 | 1334 | 		ix = EXT_FIRST_INDEX(eh); | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1335 | 		block = ext4_idx_pblock(ix); | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1336 | 		put_bh(bh); | 
 | 1337 | 	} | 
 | 1338 |  | 
 | 1339 | 	bh = sb_bread(inode->i_sb, block); | 
 | 1340 | 	if (bh == NULL) | 
 | 1341 | 		return -EIO; | 
 | 1342 | 	eh = ext_block_hdr(bh); | 
| Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 1343 | 	if (ext4_ext_check(inode, eh, path->p_depth - depth)) { | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1344 | 		put_bh(bh); | 
 | 1345 | 		return -EIO; | 
 | 1346 | 	} | 
 | 1347 | 	ex = EXT_FIRST_EXTENT(eh); | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1348 | found_extent: | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1349 | 	*logical = le32_to_cpu(ex->ee_block); | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1350 | 	*phys = ext4_ext_pblock(ex); | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1351 | 	*ret_ex = ex; | 
 | 1352 | 	if (bh) | 
 | 1353 | 		put_bh(bh); | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1354 | 	return 0; | 
| Alex Tomas | 1988b51 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1355 | } | 
 | 1356 |  | 
 | 1357 | /* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1358 |  * ext4_ext_next_allocated_block: | 
| Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1359 |  * returns allocated block in subsequent extent or EXT_MAX_BLOCKS. | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1360 |  * NOTE: it considers block number from index entry as | 
 | 1361 |  * allocated block. Thus, index entries have to be consistent | 
 | 1362 |  * with leaves. | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1363 |  */ | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1364 | static ext4_lblk_t | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1365 | ext4_ext_next_allocated_block(struct ext4_ext_path *path) | 
 | 1366 | { | 
 | 1367 | 	int depth; | 
 | 1368 |  | 
 | 1369 | 	BUG_ON(path == NULL); | 
 | 1370 | 	depth = path->p_depth; | 
 | 1371 |  | 
 | 1372 | 	if (depth == 0 && path->p_ext == NULL) | 
| Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1373 | 		return EXT_MAX_BLOCKS; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1374 |  | 
 | 1375 | 	while (depth >= 0) { | 
 | 1376 | 		if (depth == path->p_depth) { | 
 | 1377 | 			/* leaf */ | 
| Curt Wohlgemuth | 6f8ff53 | 2011-10-26 04:38:59 -0400 | [diff] [blame] | 1378 | 			if (path[depth].p_ext && | 
 | 1379 | 				path[depth].p_ext != | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1380 | 					EXT_LAST_EXTENT(path[depth].p_hdr)) | 
 | 1381 | 			  return le32_to_cpu(path[depth].p_ext[1].ee_block); | 
 | 1382 | 		} else { | 
 | 1383 | 			/* index */ | 
 | 1384 | 			if (path[depth].p_idx != | 
 | 1385 | 					EXT_LAST_INDEX(path[depth].p_hdr)) | 
 | 1386 | 			  return le32_to_cpu(path[depth].p_idx[1].ei_block); | 
 | 1387 | 		} | 
 | 1388 | 		depth--; | 
 | 1389 | 	} | 
 | 1390 |  | 
| Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1391 | 	return EXT_MAX_BLOCKS; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1392 | } | 
 | 1393 |  | 
 | 1394 | /* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1395 |  * ext4_ext_next_leaf_block: | 
| Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1396 |  * returns first allocated block from next leaf or EXT_MAX_BLOCKS | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1397 |  */ | 
| Robin Dong | 5718789 | 2011-07-23 21:49:07 -0400 | [diff] [blame] | 1398 | static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1399 | { | 
 | 1400 | 	int depth; | 
 | 1401 |  | 
 | 1402 | 	BUG_ON(path == NULL); | 
 | 1403 | 	depth = path->p_depth; | 
 | 1404 |  | 
 | 1405 | 	/* zero-tree has no leaf blocks at all */ | 
 | 1406 | 	if (depth == 0) | 
| Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1407 | 		return EXT_MAX_BLOCKS; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1408 |  | 
 | 1409 | 	/* go to index block */ | 
 | 1410 | 	depth--; | 
 | 1411 |  | 
 | 1412 | 	while (depth >= 0) { | 
 | 1413 | 		if (path[depth].p_idx != | 
 | 1414 | 				EXT_LAST_INDEX(path[depth].p_hdr)) | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1415 | 			return (ext4_lblk_t) | 
 | 1416 | 				le32_to_cpu(path[depth].p_idx[1].ei_block); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1417 | 		depth--; | 
 | 1418 | 	} | 
 | 1419 |  | 
| Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1420 | 	return EXT_MAX_BLOCKS; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1421 | } | 
 | 1422 |  | 
 | 1423 | /* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1424 |  * ext4_ext_correct_indexes: | 
 | 1425 |  * if leaf gets modified and modified extent is first in the leaf, | 
 | 1426 |  * then we have to correct all indexes above. | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1427 |  * TODO: do we need to correct tree in all cases? | 
 | 1428 |  */ | 
| Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1429 | static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode, | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1430 | 				struct ext4_ext_path *path) | 
 | 1431 | { | 
 | 1432 | 	struct ext4_extent_header *eh; | 
 | 1433 | 	int depth = ext_depth(inode); | 
 | 1434 | 	struct ext4_extent *ex; | 
 | 1435 | 	__le32 border; | 
 | 1436 | 	int k, err = 0; | 
 | 1437 |  | 
 | 1438 | 	eh = path[depth].p_hdr; | 
 | 1439 | 	ex = path[depth].p_ext; | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1440 |  | 
 | 1441 | 	if (unlikely(ex == NULL || eh == NULL)) { | 
 | 1442 | 		EXT4_ERROR_INODE(inode, | 
 | 1443 | 				 "ex %p == NULL or eh %p == NULL", ex, eh); | 
 | 1444 | 		return -EIO; | 
 | 1445 | 	} | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1446 |  | 
 | 1447 | 	if (depth == 0) { | 
 | 1448 | 		/* there is no tree at all */ | 
 | 1449 | 		return 0; | 
 | 1450 | 	} | 
 | 1451 |  | 
 | 1452 | 	if (ex != EXT_FIRST_EXTENT(eh)) { | 
 | 1453 | 		/* we correct tree if first leaf got modified only */ | 
 | 1454 | 		return 0; | 
 | 1455 | 	} | 
 | 1456 |  | 
 | 1457 | 	/* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1458 | 	 * TODO: we need correction if border is smaller than current one | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1459 | 	 */ | 
 | 1460 | 	k = depth - 1; | 
 | 1461 | 	border = path[depth].p_ext->ee_block; | 
| Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1462 | 	err = ext4_ext_get_access(handle, inode, path + k); | 
 | 1463 | 	if (err) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1464 | 		return err; | 
 | 1465 | 	path[k].p_idx->ei_block = border; | 
| Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1466 | 	err = ext4_ext_dirty(handle, inode, path + k); | 
 | 1467 | 	if (err) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1468 | 		return err; | 
 | 1469 |  | 
 | 1470 | 	while (k--) { | 
 | 1471 | 		/* change all left-side indexes */ | 
 | 1472 | 		if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr)) | 
 | 1473 | 			break; | 
| Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1474 | 		err = ext4_ext_get_access(handle, inode, path + k); | 
 | 1475 | 		if (err) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1476 | 			break; | 
 | 1477 | 		path[k].p_idx->ei_block = border; | 
| Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1478 | 		err = ext4_ext_dirty(handle, inode, path + k); | 
 | 1479 | 		if (err) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1480 | 			break; | 
 | 1481 | 	} | 
 | 1482 |  | 
 | 1483 | 	return err; | 
 | 1484 | } | 
 | 1485 |  | 
| Akira Fujita | 748de67 | 2009-06-17 19:24:03 -0400 | [diff] [blame] | 1486 | int | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1487 | ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1, | 
 | 1488 | 				struct ext4_extent *ex2) | 
 | 1489 | { | 
| Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 1490 | 	unsigned short ext1_ee_len, ext2_ee_len, max_len; | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1491 |  | 
 | 1492 | 	/* | 
 | 1493 | 	 * Make sure that either both extents are uninitialized, or | 
 | 1494 | 	 * both are _not_. | 
 | 1495 | 	 */ | 
 | 1496 | 	if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2)) | 
 | 1497 | 		return 0; | 
 | 1498 |  | 
| Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 1499 | 	if (ext4_ext_is_uninitialized(ex1)) | 
 | 1500 | 		max_len = EXT_UNINIT_MAX_LEN; | 
 | 1501 | 	else | 
 | 1502 | 		max_len = EXT_INIT_MAX_LEN; | 
 | 1503 |  | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1504 | 	ext1_ee_len = ext4_ext_get_actual_len(ex1); | 
 | 1505 | 	ext2_ee_len = ext4_ext_get_actual_len(ex2); | 
 | 1506 |  | 
 | 1507 | 	if (le32_to_cpu(ex1->ee_block) + ext1_ee_len != | 
| Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 1508 | 			le32_to_cpu(ex2->ee_block)) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1509 | 		return 0; | 
 | 1510 |  | 
| Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 1511 | 	/* | 
 | 1512 | 	 * To allow future support for preallocated extents to be added | 
 | 1513 | 	 * as an RO_COMPAT feature, refuse to merge to extents if | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1514 | 	 * this can result in the top bit of ee_len being set. | 
| Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 1515 | 	 */ | 
| Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 1516 | 	if (ext1_ee_len + ext2_ee_len > max_len) | 
| Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 1517 | 		return 0; | 
| Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 1518 | #ifdef AGGRESSIVE_TEST | 
| Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1519 | 	if (ext1_ee_len >= 4) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1520 | 		return 0; | 
 | 1521 | #endif | 
 | 1522 |  | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1523 | 	if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2)) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1524 | 		return 1; | 
 | 1525 | 	return 0; | 
 | 1526 | } | 
 | 1527 |  | 
 | 1528 | /* | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1529 |  * This function tries to merge the "ex" extent to the next extent in the tree. | 
 | 1530 |  * It always tries to merge towards right. If you want to merge towards | 
 | 1531 |  * left, pass "ex - 1" as argument instead of "ex". | 
 | 1532 |  * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns | 
 | 1533 |  * 1 if they got merged. | 
 | 1534 |  */ | 
| Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1535 | static int ext4_ext_try_to_merge_right(struct inode *inode, | 
| Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1536 | 				 struct ext4_ext_path *path, | 
 | 1537 | 				 struct ext4_extent *ex) | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1538 | { | 
 | 1539 | 	struct ext4_extent_header *eh; | 
 | 1540 | 	unsigned int depth, len; | 
 | 1541 | 	int merge_done = 0; | 
 | 1542 | 	int uninitialized = 0; | 
 | 1543 |  | 
 | 1544 | 	depth = ext_depth(inode); | 
 | 1545 | 	BUG_ON(path[depth].p_hdr == NULL); | 
 | 1546 | 	eh = path[depth].p_hdr; | 
 | 1547 |  | 
 | 1548 | 	while (ex < EXT_LAST_EXTENT(eh)) { | 
 | 1549 | 		if (!ext4_can_extents_be_merged(inode, ex, ex + 1)) | 
 | 1550 | 			break; | 
 | 1551 | 		/* merge with next extent! */ | 
 | 1552 | 		if (ext4_ext_is_uninitialized(ex)) | 
 | 1553 | 			uninitialized = 1; | 
 | 1554 | 		ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) | 
 | 1555 | 				+ ext4_ext_get_actual_len(ex + 1)); | 
 | 1556 | 		if (uninitialized) | 
 | 1557 | 			ext4_ext_mark_uninitialized(ex); | 
 | 1558 |  | 
 | 1559 | 		if (ex + 1 < EXT_LAST_EXTENT(eh)) { | 
 | 1560 | 			len = (EXT_LAST_EXTENT(eh) - ex - 1) | 
 | 1561 | 				* sizeof(struct ext4_extent); | 
 | 1562 | 			memmove(ex + 1, ex + 2, len); | 
 | 1563 | 		} | 
| Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1564 | 		le16_add_cpu(&eh->eh_entries, -1); | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1565 | 		merge_done = 1; | 
 | 1566 | 		WARN_ON(eh->eh_entries == 0); | 
 | 1567 | 		if (!eh->eh_entries) | 
| Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1568 | 			EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!"); | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 1569 | 	} | 
 | 1570 |  | 
 | 1571 | 	return merge_done; | 
 | 1572 | } | 
 | 1573 |  | 
 | 1574 | /* | 
| Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 1575 |  * This function tries to merge the @ex extent to neighbours in the tree. | 
 | 1576 |  * return 1 if merge left else 0. | 
 | 1577 |  */ | 
 | 1578 | static int ext4_ext_try_to_merge(struct inode *inode, | 
 | 1579 | 				  struct ext4_ext_path *path, | 
 | 1580 | 				  struct ext4_extent *ex) { | 
 | 1581 | 	struct ext4_extent_header *eh; | 
 | 1582 | 	unsigned int depth; | 
 | 1583 | 	int merge_done = 0; | 
 | 1584 | 	int ret = 0; | 
 | 1585 |  | 
 | 1586 | 	depth = ext_depth(inode); | 
 | 1587 | 	BUG_ON(path[depth].p_hdr == NULL); | 
 | 1588 | 	eh = path[depth].p_hdr; | 
 | 1589 |  | 
 | 1590 | 	if (ex > EXT_FIRST_EXTENT(eh)) | 
 | 1591 | 		merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1); | 
 | 1592 |  | 
 | 1593 | 	if (!merge_done) | 
 | 1594 | 		ret = ext4_ext_try_to_merge_right(inode, path, ex); | 
 | 1595 |  | 
 | 1596 | 	return ret; | 
 | 1597 | } | 
 | 1598 |  | 
 | 1599 | /* | 
| Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1600 |  * check if a portion of the "newext" extent overlaps with an | 
 | 1601 |  * existing extent. | 
 | 1602 |  * | 
 | 1603 |  * If there is an overlap discovered, it updates the length of the newext | 
 | 1604 |  * such that there will be no overlap, and then returns 1. | 
 | 1605 |  * If there is no overlap found, it returns 0. | 
 | 1606 |  */ | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1607 | static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi, | 
 | 1608 | 					   struct inode *inode, | 
| Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1609 | 					   struct ext4_extent *newext, | 
 | 1610 | 					   struct ext4_ext_path *path) | 
| Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1611 | { | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1612 | 	ext4_lblk_t b1, b2; | 
| Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1613 | 	unsigned int depth, len1; | 
 | 1614 | 	unsigned int ret = 0; | 
 | 1615 |  | 
 | 1616 | 	b1 = le32_to_cpu(newext->ee_block); | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1617 | 	len1 = ext4_ext_get_actual_len(newext); | 
| Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1618 | 	depth = ext_depth(inode); | 
 | 1619 | 	if (!path[depth].p_ext) | 
 | 1620 | 		goto out; | 
 | 1621 | 	b2 = le32_to_cpu(path[depth].p_ext->ee_block); | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1622 | 	b2 &= ~(sbi->s_cluster_ratio - 1); | 
| Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1623 |  | 
 | 1624 | 	/* | 
 | 1625 | 	 * get the next allocated block if the extent in the path | 
| Theodore Ts'o | 2b2d6d0 | 2008-07-26 16:15:44 -0400 | [diff] [blame] | 1626 | 	 * is before the requested block(s) | 
| Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1627 | 	 */ | 
 | 1628 | 	if (b2 < b1) { | 
 | 1629 | 		b2 = ext4_ext_next_allocated_block(path); | 
| Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1630 | 		if (b2 == EXT_MAX_BLOCKS) | 
| Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1631 | 			goto out; | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 1632 | 		b2 &= ~(sbi->s_cluster_ratio - 1); | 
| Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1633 | 	} | 
 | 1634 |  | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1635 | 	/* check for wrap through zero on extent logical start block*/ | 
| Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1636 | 	if (b1 + len1 < b1) { | 
| Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1637 | 		len1 = EXT_MAX_BLOCKS - b1; | 
| Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 1638 | 		newext->ee_len = cpu_to_le16(len1); | 
 | 1639 | 		ret = 1; | 
 | 1640 | 	} | 
 | 1641 |  | 
 | 1642 | 	/* check for overlap */ | 
 | 1643 | 	if (b1 + len1 > b2) { | 
 | 1644 | 		newext->ee_len = cpu_to_le16(b2 - b1); | 
 | 1645 | 		ret = 1; | 
 | 1646 | 	} | 
 | 1647 | out: | 
 | 1648 | 	return ret; | 
 | 1649 | } | 
 | 1650 |  | 
 | 1651 | /* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1652 |  * ext4_ext_insert_extent: | 
 | 1653 |  * tries to merge requsted extent into the existing extent or | 
 | 1654 |  * inserts requested extent as new one into the tree, | 
 | 1655 |  * creating new leaf in the no-space case. | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1656 |  */ | 
 | 1657 | int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, | 
 | 1658 | 				struct ext4_ext_path *path, | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 1659 | 				struct ext4_extent *newext, int flag) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1660 | { | 
| Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1661 | 	struct ext4_extent_header *eh; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1662 | 	struct ext4_extent *ex, *fex; | 
 | 1663 | 	struct ext4_extent *nearex; /* nearest extent */ | 
 | 1664 | 	struct ext4_ext_path *npath = NULL; | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1665 | 	int depth, len, err; | 
 | 1666 | 	ext4_lblk_t next; | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1667 | 	unsigned uninitialized = 0; | 
| Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1668 | 	int flags = 0; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1669 |  | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1670 | 	if (unlikely(ext4_ext_get_actual_len(newext) == 0)) { | 
 | 1671 | 		EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0"); | 
 | 1672 | 		return -EIO; | 
 | 1673 | 	} | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1674 | 	depth = ext_depth(inode); | 
 | 1675 | 	ex = path[depth].p_ext; | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1676 | 	if (unlikely(path[depth].p_hdr == NULL)) { | 
 | 1677 | 		EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); | 
 | 1678 | 		return -EIO; | 
 | 1679 | 	} | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1680 |  | 
 | 1681 | 	/* try to insert block into found extent and return */ | 
| Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 1682 | 	if (ex && !(flag & EXT4_GET_BLOCKS_PRE_IO) | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 1683 | 		&& ext4_can_extents_be_merged(inode, ex, newext)) { | 
| Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 1684 | 		ext_debug("append [%d]%d block to %u:[%d]%d (from %llu)\n", | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1685 | 			  ext4_ext_is_uninitialized(newext), | 
 | 1686 | 			  ext4_ext_get_actual_len(newext), | 
 | 1687 | 			  le32_to_cpu(ex->ee_block), | 
 | 1688 | 			  ext4_ext_is_uninitialized(ex), | 
 | 1689 | 			  ext4_ext_get_actual_len(ex), | 
 | 1690 | 			  ext4_ext_pblock(ex)); | 
| Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1691 | 		err = ext4_ext_get_access(handle, inode, path + depth); | 
 | 1692 | 		if (err) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1693 | 			return err; | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1694 |  | 
 | 1695 | 		/* | 
 | 1696 | 		 * ext4_can_extents_be_merged should have checked that either | 
 | 1697 | 		 * both extents are uninitialized, or both aren't. Thus we | 
 | 1698 | 		 * need to check only one of them here. | 
 | 1699 | 		 */ | 
 | 1700 | 		if (ext4_ext_is_uninitialized(ex)) | 
 | 1701 | 			uninitialized = 1; | 
 | 1702 | 		ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) | 
 | 1703 | 					+ ext4_ext_get_actual_len(newext)); | 
 | 1704 | 		if (uninitialized) | 
 | 1705 | 			ext4_ext_mark_uninitialized(ex); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1706 | 		eh = path[depth].p_hdr; | 
 | 1707 | 		nearex = ex; | 
 | 1708 | 		goto merge; | 
 | 1709 | 	} | 
 | 1710 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1711 | 	depth = ext_depth(inode); | 
 | 1712 | 	eh = path[depth].p_hdr; | 
 | 1713 | 	if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) | 
 | 1714 | 		goto has_space; | 
 | 1715 |  | 
 | 1716 | 	/* probably next leaf has space for us? */ | 
 | 1717 | 	fex = EXT_LAST_EXTENT(eh); | 
| Robin Dong | 598dbdf | 2011-07-11 18:24:01 -0400 | [diff] [blame] | 1718 | 	next = EXT_MAX_BLOCKS; | 
 | 1719 | 	if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)) | 
| Robin Dong | 5718789 | 2011-07-23 21:49:07 -0400 | [diff] [blame] | 1720 | 		next = ext4_ext_next_leaf_block(path); | 
| Robin Dong | 598dbdf | 2011-07-11 18:24:01 -0400 | [diff] [blame] | 1721 | 	if (next != EXT_MAX_BLOCKS) { | 
| Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 1722 | 		ext_debug("next leaf block - %u\n", next); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1723 | 		BUG_ON(npath != NULL); | 
 | 1724 | 		npath = ext4_ext_find_extent(inode, next, NULL); | 
 | 1725 | 		if (IS_ERR(npath)) | 
 | 1726 | 			return PTR_ERR(npath); | 
 | 1727 | 		BUG_ON(npath->p_depth != path->p_depth); | 
 | 1728 | 		eh = npath[depth].p_hdr; | 
 | 1729 | 		if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) { | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1730 | 			ext_debug("next leaf isn't full(%d)\n", | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1731 | 				  le16_to_cpu(eh->eh_entries)); | 
 | 1732 | 			path = npath; | 
| Robin Dong | ffb505f | 2011-07-11 11:43:59 -0400 | [diff] [blame] | 1733 | 			goto has_space; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1734 | 		} | 
 | 1735 | 		ext_debug("next leaf has no free space(%d,%d)\n", | 
 | 1736 | 			  le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); | 
 | 1737 | 	} | 
 | 1738 |  | 
 | 1739 | 	/* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1740 | 	 * There is no free space in the found leaf. | 
 | 1741 | 	 * We're gonna add a new leaf in the tree. | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1742 | 	 */ | 
| Allison Henderson | 55f020d | 2011-05-25 07:41:26 -0400 | [diff] [blame] | 1743 | 	if (flag & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) | 
 | 1744 | 		flags = EXT4_MB_USE_ROOT_BLOCKS; | 
 | 1745 | 	err = ext4_ext_create_new_leaf(handle, inode, flags, path, newext); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1746 | 	if (err) | 
 | 1747 | 		goto cleanup; | 
 | 1748 | 	depth = ext_depth(inode); | 
 | 1749 | 	eh = path[depth].p_hdr; | 
 | 1750 |  | 
 | 1751 | has_space: | 
 | 1752 | 	nearex = path[depth].p_ext; | 
 | 1753 |  | 
| Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 1754 | 	err = ext4_ext_get_access(handle, inode, path + depth); | 
 | 1755 | 	if (err) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1756 | 		goto cleanup; | 
 | 1757 |  | 
 | 1758 | 	if (!nearex) { | 
 | 1759 | 		/* there is no extent in this leaf, create first one */ | 
| Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 1760 | 		ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n", | 
| Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1761 | 				le32_to_cpu(newext->ee_block), | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1762 | 				ext4_ext_pblock(newext), | 
| Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 1763 | 				ext4_ext_is_uninitialized(newext), | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1764 | 				ext4_ext_get_actual_len(newext)); | 
| Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 1765 | 		nearex = EXT_FIRST_EXTENT(eh); | 
 | 1766 | 	} else { | 
 | 1767 | 		if (le32_to_cpu(newext->ee_block) | 
| Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1768 | 			   > le32_to_cpu(nearex->ee_block)) { | 
| Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 1769 | 			/* Insert after */ | 
| Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 1770 | 			ext_debug("insert %u:%llu:[%d]%d before: " | 
 | 1771 | 					"nearest %p\n", | 
| Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1772 | 					le32_to_cpu(newext->ee_block), | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1773 | 					ext4_ext_pblock(newext), | 
| Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 1774 | 					ext4_ext_is_uninitialized(newext), | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1775 | 					ext4_ext_get_actual_len(newext), | 
| Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 1776 | 					nearex); | 
 | 1777 | 			nearex++; | 
 | 1778 | 		} else { | 
 | 1779 | 			/* Insert before */ | 
 | 1780 | 			BUG_ON(newext->ee_block == nearex->ee_block); | 
| Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 1781 | 			ext_debug("insert %u:%llu:[%d]%d after: " | 
 | 1782 | 					"nearest %p\n", | 
| Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 1783 | 					le32_to_cpu(newext->ee_block), | 
 | 1784 | 					ext4_ext_pblock(newext), | 
 | 1785 | 					ext4_ext_is_uninitialized(newext), | 
 | 1786 | 					ext4_ext_get_actual_len(newext), | 
 | 1787 | 					nearex); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1788 | 		} | 
| Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 1789 | 		len = EXT_LAST_EXTENT(eh) - nearex + 1; | 
 | 1790 | 		if (len > 0) { | 
| Yongqiang Yang | 32de675 | 2011-11-01 18:56:41 -0400 | [diff] [blame] | 1791 | 			ext_debug("insert %u:%llu:[%d]%d: " | 
| Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 1792 | 					"move %d extents from 0x%p to 0x%p\n", | 
 | 1793 | 					le32_to_cpu(newext->ee_block), | 
 | 1794 | 					ext4_ext_pblock(newext), | 
 | 1795 | 					ext4_ext_is_uninitialized(newext), | 
 | 1796 | 					ext4_ext_get_actual_len(newext), | 
 | 1797 | 					len, nearex, nearex + 1); | 
 | 1798 | 			memmove(nearex + 1, nearex, | 
 | 1799 | 				len * sizeof(struct ext4_extent)); | 
 | 1800 | 		} | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1801 | 	} | 
 | 1802 |  | 
| Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 1803 | 	le16_add_cpu(&eh->eh_entries, 1); | 
| Eric Gouriou | 80e675f | 2011-10-27 11:52:18 -0400 | [diff] [blame] | 1804 | 	path[depth].p_ext = nearex; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1805 | 	nearex->ee_block = newext->ee_block; | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1806 | 	ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1807 | 	nearex->ee_len = newext->ee_len; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1808 |  | 
 | 1809 | merge: | 
 | 1810 | 	/* try to merge extents to the right */ | 
| Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 1811 | 	if (!(flag & EXT4_GET_BLOCKS_PRE_IO)) | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 1812 | 		ext4_ext_try_to_merge(inode, path, nearex); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1813 |  | 
 | 1814 | 	/* try to merge extents to the left */ | 
 | 1815 |  | 
 | 1816 | 	/* time to correct all indexes above */ | 
 | 1817 | 	err = ext4_ext_correct_indexes(handle, inode, path); | 
 | 1818 | 	if (err) | 
 | 1819 | 		goto cleanup; | 
 | 1820 |  | 
 | 1821 | 	err = ext4_ext_dirty(handle, inode, path + depth); | 
 | 1822 |  | 
 | 1823 | cleanup: | 
 | 1824 | 	if (npath) { | 
 | 1825 | 		ext4_ext_drop_refs(npath); | 
 | 1826 | 		kfree(npath); | 
 | 1827 | 	} | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1828 | 	ext4_ext_invalidate_cache(inode); | 
 | 1829 | 	return err; | 
 | 1830 | } | 
 | 1831 |  | 
| Theodore Ts'o | 1f109d5 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1832 | static int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block, | 
 | 1833 | 			       ext4_lblk_t num, ext_prepare_callback func, | 
 | 1834 | 			       void *cbdata) | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 1835 | { | 
 | 1836 | 	struct ext4_ext_path *path = NULL; | 
 | 1837 | 	struct ext4_ext_cache cbex; | 
 | 1838 | 	struct ext4_extent *ex; | 
 | 1839 | 	ext4_lblk_t next, start = 0, end = 0; | 
 | 1840 | 	ext4_lblk_t last = block + num; | 
 | 1841 | 	int depth, exists, err = 0; | 
 | 1842 |  | 
 | 1843 | 	BUG_ON(func == NULL); | 
 | 1844 | 	BUG_ON(inode == NULL); | 
 | 1845 |  | 
| Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1846 | 	while (block < last && block != EXT_MAX_BLOCKS) { | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 1847 | 		num = last - block; | 
 | 1848 | 		/* find extent for this block */ | 
| Theodore Ts'o | fab3a54 | 2009-12-09 21:30:02 -0500 | [diff] [blame] | 1849 | 		down_read(&EXT4_I(inode)->i_data_sem); | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 1850 | 		path = ext4_ext_find_extent(inode, block, path); | 
| Theodore Ts'o | fab3a54 | 2009-12-09 21:30:02 -0500 | [diff] [blame] | 1851 | 		up_read(&EXT4_I(inode)->i_data_sem); | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 1852 | 		if (IS_ERR(path)) { | 
 | 1853 | 			err = PTR_ERR(path); | 
 | 1854 | 			path = NULL; | 
 | 1855 | 			break; | 
 | 1856 | 		} | 
 | 1857 |  | 
 | 1858 | 		depth = ext_depth(inode); | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1859 | 		if (unlikely(path[depth].p_hdr == NULL)) { | 
 | 1860 | 			EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); | 
 | 1861 | 			err = -EIO; | 
 | 1862 | 			break; | 
 | 1863 | 		} | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 1864 | 		ex = path[depth].p_ext; | 
 | 1865 | 		next = ext4_ext_next_allocated_block(path); | 
 | 1866 |  | 
 | 1867 | 		exists = 0; | 
 | 1868 | 		if (!ex) { | 
 | 1869 | 			/* there is no extent yet, so try to allocate | 
 | 1870 | 			 * all requested space */ | 
 | 1871 | 			start = block; | 
 | 1872 | 			end = block + num; | 
 | 1873 | 		} else if (le32_to_cpu(ex->ee_block) > block) { | 
 | 1874 | 			/* need to allocate space before found extent */ | 
 | 1875 | 			start = block; | 
 | 1876 | 			end = le32_to_cpu(ex->ee_block); | 
 | 1877 | 			if (block + num < end) | 
 | 1878 | 				end = block + num; | 
 | 1879 | 		} else if (block >= le32_to_cpu(ex->ee_block) | 
 | 1880 | 					+ ext4_ext_get_actual_len(ex)) { | 
 | 1881 | 			/* need to allocate space after found extent */ | 
 | 1882 | 			start = block; | 
 | 1883 | 			end = block + num; | 
 | 1884 | 			if (end >= next) | 
 | 1885 | 				end = next; | 
 | 1886 | 		} else if (block >= le32_to_cpu(ex->ee_block)) { | 
 | 1887 | 			/* | 
 | 1888 | 			 * some part of requested space is covered | 
 | 1889 | 			 * by found extent | 
 | 1890 | 			 */ | 
 | 1891 | 			start = block; | 
 | 1892 | 			end = le32_to_cpu(ex->ee_block) | 
 | 1893 | 				+ ext4_ext_get_actual_len(ex); | 
 | 1894 | 			if (block + num < end) | 
 | 1895 | 				end = block + num; | 
 | 1896 | 			exists = 1; | 
 | 1897 | 		} else { | 
 | 1898 | 			BUG(); | 
 | 1899 | 		} | 
 | 1900 | 		BUG_ON(end <= start); | 
 | 1901 |  | 
 | 1902 | 		if (!exists) { | 
 | 1903 | 			cbex.ec_block = start; | 
 | 1904 | 			cbex.ec_len = end - start; | 
 | 1905 | 			cbex.ec_start = 0; | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 1906 | 		} else { | 
 | 1907 | 			cbex.ec_block = le32_to_cpu(ex->ee_block); | 
 | 1908 | 			cbex.ec_len = ext4_ext_get_actual_len(ex); | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 1909 | 			cbex.ec_start = ext4_ext_pblock(ex); | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 1910 | 		} | 
 | 1911 |  | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 1912 | 		if (unlikely(cbex.ec_len == 0)) { | 
 | 1913 | 			EXT4_ERROR_INODE(inode, "cbex.ec_len == 0"); | 
 | 1914 | 			err = -EIO; | 
 | 1915 | 			break; | 
 | 1916 | 		} | 
| Lukas Czerner | c03f8aa | 2011-06-06 00:06:52 -0400 | [diff] [blame] | 1917 | 		err = func(inode, next, &cbex, ex, cbdata); | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 1918 | 		ext4_ext_drop_refs(path); | 
 | 1919 |  | 
 | 1920 | 		if (err < 0) | 
 | 1921 | 			break; | 
 | 1922 |  | 
 | 1923 | 		if (err == EXT_REPEAT) | 
 | 1924 | 			continue; | 
 | 1925 | 		else if (err == EXT_BREAK) { | 
 | 1926 | 			err = 0; | 
 | 1927 | 			break; | 
 | 1928 | 		} | 
 | 1929 |  | 
 | 1930 | 		if (ext_depth(inode) != depth) { | 
 | 1931 | 			/* depth was changed. we have to realloc path */ | 
 | 1932 | 			kfree(path); | 
 | 1933 | 			path = NULL; | 
 | 1934 | 		} | 
 | 1935 |  | 
 | 1936 | 		block = cbex.ec_block + cbex.ec_len; | 
 | 1937 | 	} | 
 | 1938 |  | 
 | 1939 | 	if (path) { | 
 | 1940 | 		ext4_ext_drop_refs(path); | 
 | 1941 | 		kfree(path); | 
 | 1942 | 	} | 
 | 1943 |  | 
 | 1944 | 	return err; | 
 | 1945 | } | 
 | 1946 |  | 
| Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 1947 | static void | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1948 | ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block, | 
| Theodore Ts'o | b05e6ae | 2011-01-10 12:13:26 -0500 | [diff] [blame] | 1949 | 			__u32 len, ext4_fsblk_t start) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1950 | { | 
 | 1951 | 	struct ext4_ext_cache *cex; | 
 | 1952 | 	BUG_ON(len == 0); | 
| Theodore Ts'o | 2ec0ae3 | 2009-05-15 09:07:28 -0400 | [diff] [blame] | 1953 | 	spin_lock(&EXT4_I(inode)->i_block_reservation_lock); | 
| Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 1954 | 	trace_ext4_ext_put_in_cache(inode, block, len, start); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1955 | 	cex = &EXT4_I(inode)->i_cached_extent; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1956 | 	cex->ec_block = block; | 
 | 1957 | 	cex->ec_len = len; | 
 | 1958 | 	cex->ec_start = start; | 
| Theodore Ts'o | 2ec0ae3 | 2009-05-15 09:07:28 -0400 | [diff] [blame] | 1959 | 	spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1960 | } | 
 | 1961 |  | 
 | 1962 | /* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 1963 |  * ext4_ext_put_gap_in_cache: | 
 | 1964 |  * calculate boundaries of the gap that the requested block fits into | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1965 |  * and cache this gap | 
 | 1966 |  */ | 
| Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 1967 | static void | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1968 | ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path, | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1969 | 				ext4_lblk_t block) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1970 | { | 
 | 1971 | 	int depth = ext_depth(inode); | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1972 | 	unsigned long len; | 
 | 1973 | 	ext4_lblk_t lblock; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1974 | 	struct ext4_extent *ex; | 
 | 1975 |  | 
 | 1976 | 	ex = path[depth].p_ext; | 
 | 1977 | 	if (ex == NULL) { | 
 | 1978 | 		/* there is no extent yet, so gap is [0;-] */ | 
 | 1979 | 		lblock = 0; | 
| Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 1980 | 		len = EXT_MAX_BLOCKS; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1981 | 		ext_debug("cache gap(whole file):"); | 
 | 1982 | 	} else if (block < le32_to_cpu(ex->ee_block)) { | 
 | 1983 | 		lblock = block; | 
 | 1984 | 		len = le32_to_cpu(ex->ee_block) - block; | 
| Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1985 | 		ext_debug("cache gap(before): %u [%u:%u]", | 
 | 1986 | 				block, | 
 | 1987 | 				le32_to_cpu(ex->ee_block), | 
 | 1988 | 				 ext4_ext_get_actual_len(ex)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 1989 | 	} else if (block >= le32_to_cpu(ex->ee_block) | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1990 | 			+ ext4_ext_get_actual_len(ex)) { | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1991 | 		ext4_lblk_t next; | 
| Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 1992 | 		lblock = le32_to_cpu(ex->ee_block) | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 1993 | 			+ ext4_ext_get_actual_len(ex); | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1994 |  | 
 | 1995 | 		next = ext4_ext_next_allocated_block(path); | 
| Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1996 | 		ext_debug("cache gap(after): [%u:%u] %u", | 
 | 1997 | 				le32_to_cpu(ex->ee_block), | 
 | 1998 | 				ext4_ext_get_actual_len(ex), | 
 | 1999 | 				block); | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2000 | 		BUG_ON(next == lblock); | 
 | 2001 | 		len = next - lblock; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2002 | 	} else { | 
 | 2003 | 		lblock = len = 0; | 
 | 2004 | 		BUG(); | 
 | 2005 | 	} | 
 | 2006 |  | 
| Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2007 | 	ext_debug(" -> %u:%lu\n", lblock, len); | 
| Theodore Ts'o | b05e6ae | 2011-01-10 12:13:26 -0500 | [diff] [blame] | 2008 | 	ext4_ext_put_in_cache(inode, lblock, len, 0); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2009 | } | 
 | 2010 |  | 
| Theodore Ts'o | b05e6ae | 2011-01-10 12:13:26 -0500 | [diff] [blame] | 2011 | /* | 
| Robin Dong | b7ca1e8 | 2011-07-23 21:53:25 -0400 | [diff] [blame] | 2012 |  * ext4_ext_check_cache() | 
| Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 2013 |  * Checks to see if the given block is in the cache. | 
 | 2014 |  * If it is, the cached extent is stored in the given | 
 | 2015 |  * cache extent pointer.  If the cached extent is a hole, | 
 | 2016 |  * this routine should be used instead of | 
 | 2017 |  * ext4_ext_in_cache if the calling function needs to | 
 | 2018 |  * know the size of the hole. | 
 | 2019 |  * | 
 | 2020 |  * @inode: The files inode | 
 | 2021 |  * @block: The block to look for in the cache | 
 | 2022 |  * @ex:    Pointer where the cached extent will be stored | 
 | 2023 |  *         if it contains block | 
 | 2024 |  * | 
| Theodore Ts'o | b05e6ae | 2011-01-10 12:13:26 -0500 | [diff] [blame] | 2025 |  * Return 0 if cache is invalid; 1 if the cache is valid | 
 | 2026 |  */ | 
| Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 2027 | static int ext4_ext_check_cache(struct inode *inode, ext4_lblk_t block, | 
 | 2028 | 	struct ext4_ext_cache *ex){ | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2029 | 	struct ext4_ext_cache *cex; | 
| Vivek Haldar | 77f4135 | 2011-05-22 21:24:16 -0400 | [diff] [blame] | 2030 | 	struct ext4_sb_info *sbi; | 
| Theodore Ts'o | b05e6ae | 2011-01-10 12:13:26 -0500 | [diff] [blame] | 2031 | 	int ret = 0; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2032 |  | 
| Theodore Ts'o | 60e6679 | 2010-05-17 07:00:00 -0400 | [diff] [blame] | 2033 | 	/* | 
| Theodore Ts'o | 2ec0ae3 | 2009-05-15 09:07:28 -0400 | [diff] [blame] | 2034 | 	 * We borrow i_block_reservation_lock to protect i_cached_extent | 
 | 2035 | 	 */ | 
 | 2036 | 	spin_lock(&EXT4_I(inode)->i_block_reservation_lock); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2037 | 	cex = &EXT4_I(inode)->i_cached_extent; | 
| Vivek Haldar | 77f4135 | 2011-05-22 21:24:16 -0400 | [diff] [blame] | 2038 | 	sbi = EXT4_SB(inode->i_sb); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2039 |  | 
 | 2040 | 	/* has cache valid data? */ | 
| Theodore Ts'o | b05e6ae | 2011-01-10 12:13:26 -0500 | [diff] [blame] | 2041 | 	if (cex->ec_len == 0) | 
| Theodore Ts'o | 2ec0ae3 | 2009-05-15 09:07:28 -0400 | [diff] [blame] | 2042 | 		goto errout; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2043 |  | 
| Akinobu Mita | 731eb1a | 2010-03-03 23:55:01 -0500 | [diff] [blame] | 2044 | 	if (in_range(block, cex->ec_block, cex->ec_len)) { | 
| Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 2045 | 		memcpy(ex, cex, sizeof(struct ext4_ext_cache)); | 
| Eric Sandeen | bba9074 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2046 | 		ext_debug("%u cached by %u:%u:%llu\n", | 
 | 2047 | 				block, | 
 | 2048 | 				cex->ec_block, cex->ec_len, cex->ec_start); | 
| Theodore Ts'o | b05e6ae | 2011-01-10 12:13:26 -0500 | [diff] [blame] | 2049 | 		ret = 1; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2050 | 	} | 
| Theodore Ts'o | 2ec0ae3 | 2009-05-15 09:07:28 -0400 | [diff] [blame] | 2051 | errout: | 
| Vivek Haldar | 77f4135 | 2011-05-22 21:24:16 -0400 | [diff] [blame] | 2052 | 	if (!ret) | 
 | 2053 | 		sbi->extent_cache_misses++; | 
 | 2054 | 	else | 
 | 2055 | 		sbi->extent_cache_hits++; | 
| Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2056 | 	trace_ext4_ext_in_cache(inode, block, ret); | 
| Theodore Ts'o | 2ec0ae3 | 2009-05-15 09:07:28 -0400 | [diff] [blame] | 2057 | 	spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); | 
 | 2058 | 	return ret; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2059 | } | 
 | 2060 |  | 
 | 2061 | /* | 
| Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 2062 |  * ext4_ext_in_cache() | 
 | 2063 |  * Checks to see if the given block is in the cache. | 
 | 2064 |  * If it is, the cached extent is stored in the given | 
 | 2065 |  * extent pointer. | 
 | 2066 |  * | 
 | 2067 |  * @inode: The files inode | 
 | 2068 |  * @block: The block to look for in the cache | 
 | 2069 |  * @ex:    Pointer where the cached extent will be stored | 
 | 2070 |  *         if it contains block | 
 | 2071 |  * | 
 | 2072 |  * Return 0 if cache is invalid; 1 if the cache is valid | 
 | 2073 |  */ | 
 | 2074 | static int | 
 | 2075 | ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block, | 
 | 2076 | 			struct ext4_extent *ex) | 
 | 2077 | { | 
 | 2078 | 	struct ext4_ext_cache cex; | 
 | 2079 | 	int ret = 0; | 
 | 2080 |  | 
 | 2081 | 	if (ext4_ext_check_cache(inode, block, &cex)) { | 
 | 2082 | 		ex->ee_block = cpu_to_le32(cex.ec_block); | 
 | 2083 | 		ext4_ext_store_pblock(ex, cex.ec_start); | 
 | 2084 | 		ex->ee_len = cpu_to_le16(cex.ec_len); | 
 | 2085 | 		ret = 1; | 
 | 2086 | 	} | 
 | 2087 |  | 
 | 2088 | 	return ret; | 
 | 2089 | } | 
 | 2090 |  | 
 | 2091 |  | 
 | 2092 | /* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2093 |  * ext4_ext_rm_idx: | 
 | 2094 |  * removes index from the index block. | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2095 |  */ | 
| Aneesh Kumar K.V | 1d03ec9 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2096 | static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2097 | 			struct ext4_ext_path *path) | 
 | 2098 | { | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2099 | 	int err; | 
| Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2100 | 	ext4_fsblk_t leaf; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2101 |  | 
 | 2102 | 	/* free index block */ | 
 | 2103 | 	path--; | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2104 | 	leaf = ext4_idx_pblock(path->p_idx); | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2105 | 	if (unlikely(path->p_hdr->eh_entries == 0)) { | 
 | 2106 | 		EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0"); | 
 | 2107 | 		return -EIO; | 
 | 2108 | 	} | 
| Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 2109 | 	err = ext4_ext_get_access(handle, inode, path); | 
 | 2110 | 	if (err) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2111 | 		return err; | 
| Robin Dong | 0e1147b | 2011-07-27 21:29:33 -0400 | [diff] [blame] | 2112 |  | 
 | 2113 | 	if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) { | 
 | 2114 | 		int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx; | 
 | 2115 | 		len *= sizeof(struct ext4_extent_idx); | 
 | 2116 | 		memmove(path->p_idx, path->p_idx + 1, len); | 
 | 2117 | 	} | 
 | 2118 |  | 
| Marcin Slusarz | e8546d0 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2119 | 	le16_add_cpu(&path->p_hdr->eh_entries, -1); | 
| Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 2120 | 	err = ext4_ext_dirty(handle, inode, path); | 
 | 2121 | 	if (err) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2122 | 		return err; | 
| Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 2123 | 	ext_debug("index is empty, remove it, free block %llu\n", leaf); | 
| Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2124 | 	trace_ext4_ext_rm_idx(inode, leaf); | 
 | 2125 |  | 
| Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 2126 | 	ext4_free_blocks(handle, inode, NULL, leaf, 1, | 
| Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 2127 | 			 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2128 | 	return err; | 
 | 2129 | } | 
 | 2130 |  | 
 | 2131 | /* | 
| Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2132 |  * ext4_ext_calc_credits_for_single_extent: | 
 | 2133 |  * This routine returns max. credits that needed to insert an extent | 
 | 2134 |  * to the extent tree. | 
 | 2135 |  * When pass the actual path, the caller should calculate credits | 
 | 2136 |  * under i_data_sem. | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2137 |  */ | 
| Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2138 | int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks, | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2139 | 						struct ext4_ext_path *path) | 
 | 2140 | { | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2141 | 	if (path) { | 
| Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2142 | 		int depth = ext_depth(inode); | 
| Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 2143 | 		int ret = 0; | 
| Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2144 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2145 | 		/* probably there is space in leaf? */ | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2146 | 		if (le16_to_cpu(path[depth].p_hdr->eh_entries) | 
| Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2147 | 				< le16_to_cpu(path[depth].p_hdr->eh_max)) { | 
 | 2148 |  | 
 | 2149 | 			/* | 
 | 2150 | 			 *  There are some space in the leaf tree, no | 
 | 2151 | 			 *  need to account for leaf block credit | 
 | 2152 | 			 * | 
 | 2153 | 			 *  bitmaps and block group descriptor blocks | 
| Tao Ma | df3ab17 | 2011-10-08 15:53:49 -0400 | [diff] [blame] | 2154 | 			 *  and other metadata blocks still need to be | 
| Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2155 | 			 *  accounted. | 
 | 2156 | 			 */ | 
| Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2157 | 			/* 1 bitmap, 1 block group descriptor */ | 
| Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2158 | 			ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb); | 
| Aneesh Kumar K.V | 5887e98 | 2009-07-05 23:12:04 -0400 | [diff] [blame] | 2159 | 			return ret; | 
| Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2160 | 		} | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2161 | 	} | 
 | 2162 |  | 
| Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2163 | 	return ext4_chunk_trans_blocks(inode, nrblocks); | 
| Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2164 | } | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2165 |  | 
| Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2166 | /* | 
 | 2167 |  * How many index/leaf blocks need to change/allocate to modify nrblocks? | 
 | 2168 |  * | 
 | 2169 |  * if nrblocks are fit in a single extent (chunk flag is 1), then | 
 | 2170 |  * in the worse case, each tree level index/leaf need to be changed | 
 | 2171 |  * if the tree split due to insert a new extent, then the old tree | 
 | 2172 |  * index/leaf need to be updated too | 
 | 2173 |  * | 
 | 2174 |  * If the nrblocks are discontiguous, they could cause | 
 | 2175 |  * the whole tree split more than once, but this is really rare. | 
 | 2176 |  */ | 
| Mingming Cao | 525f4ed | 2008-08-19 22:15:58 -0400 | [diff] [blame] | 2177 | int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) | 
| Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2178 | { | 
 | 2179 | 	int index; | 
 | 2180 | 	int depth = ext_depth(inode); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2181 |  | 
| Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2182 | 	if (chunk) | 
 | 2183 | 		index = depth * 2; | 
 | 2184 | 	else | 
 | 2185 | 		index = depth * 3; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2186 |  | 
| Mingming Cao | ee12b63 | 2008-08-19 22:16:05 -0400 | [diff] [blame] | 2187 | 	return index; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2188 | } | 
 | 2189 |  | 
 | 2190 | static int ext4_remove_blocks(handle_t *handle, struct inode *inode, | 
| Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2191 | 			      struct ext4_extent *ex, | 
 | 2192 | 			      ext4_fsblk_t *partial_cluster, | 
 | 2193 | 			      ext4_lblk_t from, ext4_lblk_t to) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2194 | { | 
| Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2195 | 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2196 | 	unsigned short ee_len =  ext4_ext_get_actual_len(ex); | 
| Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2197 | 	ext4_fsblk_t pblk; | 
| Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 2198 | 	int flags = EXT4_FREE_BLOCKS_FORGET; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2199 |  | 
| Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 2200 | 	if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) | 
| Theodore Ts'o | e636260 | 2009-11-23 07:17:05 -0500 | [diff] [blame] | 2201 | 		flags |= EXT4_FREE_BLOCKS_METADATA; | 
| Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2202 | 	/* | 
 | 2203 | 	 * For bigalloc file systems, we never free a partial cluster | 
 | 2204 | 	 * at the beginning of the extent.  Instead, we make a note | 
 | 2205 | 	 * that we tried freeing the cluster, and check to see if we | 
 | 2206 | 	 * need to free it on a subsequent call to ext4_remove_blocks, | 
 | 2207 | 	 * or at the end of the ext4_truncate() operation. | 
 | 2208 | 	 */ | 
 | 2209 | 	flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER; | 
 | 2210 |  | 
| Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2211 | 	trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster); | 
| Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2212 | 	/* | 
 | 2213 | 	 * If we have a partial cluster, and it's different from the | 
 | 2214 | 	 * cluster of the last block, we need to explicitly free the | 
 | 2215 | 	 * partial cluster here. | 
 | 2216 | 	 */ | 
 | 2217 | 	pblk = ext4_ext_pblock(ex) + ee_len - 1; | 
 | 2218 | 	if (*partial_cluster && (EXT4_B2C(sbi, pblk) != *partial_cluster)) { | 
 | 2219 | 		ext4_free_blocks(handle, inode, NULL, | 
 | 2220 | 				 EXT4_C2B(sbi, *partial_cluster), | 
 | 2221 | 				 sbi->s_cluster_ratio, flags); | 
 | 2222 | 		*partial_cluster = 0; | 
 | 2223 | 	} | 
 | 2224 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2225 | #ifdef EXTENTS_STATS | 
 | 2226 | 	{ | 
 | 2227 | 		struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2228 | 		spin_lock(&sbi->s_ext_stats_lock); | 
 | 2229 | 		sbi->s_ext_blocks += ee_len; | 
 | 2230 | 		sbi->s_ext_extents++; | 
 | 2231 | 		if (ee_len < sbi->s_ext_min) | 
 | 2232 | 			sbi->s_ext_min = ee_len; | 
 | 2233 | 		if (ee_len > sbi->s_ext_max) | 
 | 2234 | 			sbi->s_ext_max = ee_len; | 
 | 2235 | 		if (ext_depth(inode) > sbi->s_depth_max) | 
 | 2236 | 			sbi->s_depth_max = ext_depth(inode); | 
 | 2237 | 		spin_unlock(&sbi->s_ext_stats_lock); | 
 | 2238 | 	} | 
 | 2239 | #endif | 
 | 2240 | 	if (from >= le32_to_cpu(ex->ee_block) | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2241 | 	    && to == le32_to_cpu(ex->ee_block) + ee_len - 1) { | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2242 | 		/* tail removal */ | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2243 | 		ext4_lblk_t num; | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2244 |  | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2245 | 		num = le32_to_cpu(ex->ee_block) + ee_len - from; | 
| Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2246 | 		pblk = ext4_ext_pblock(ex) + ee_len - num; | 
 | 2247 | 		ext_debug("free last %u blocks starting %llu\n", num, pblk); | 
 | 2248 | 		ext4_free_blocks(handle, inode, NULL, pblk, num, flags); | 
 | 2249 | 		/* | 
 | 2250 | 		 * If the block range to be freed didn't start at the | 
 | 2251 | 		 * beginning of a cluster, and we removed the entire | 
 | 2252 | 		 * extent, save the partial cluster here, since we | 
 | 2253 | 		 * might need to delete if we determine that the | 
 | 2254 | 		 * truncate operation has removed all of the blocks in | 
 | 2255 | 		 * the cluster. | 
 | 2256 | 		 */ | 
 | 2257 | 		if (pblk & (sbi->s_cluster_ratio - 1) && | 
 | 2258 | 		    (ee_len == num)) | 
 | 2259 | 			*partial_cluster = EXT4_B2C(sbi, pblk); | 
 | 2260 | 		else | 
 | 2261 | 			*partial_cluster = 0; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2262 | 	} else if (from == le32_to_cpu(ex->ee_block) | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2263 | 		   && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) { | 
| Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2264 | 		/* head removal */ | 
 | 2265 | 		ext4_lblk_t num; | 
 | 2266 | 		ext4_fsblk_t start; | 
 | 2267 |  | 
 | 2268 | 		num = to - from; | 
 | 2269 | 		start = ext4_ext_pblock(ex); | 
 | 2270 |  | 
 | 2271 | 		ext_debug("free first %u blocks starting %llu\n", num, start); | 
| H Hartley Sweeten | ee90d57 | 2011-10-18 11:01:51 -0400 | [diff] [blame] | 2272 | 		ext4_free_blocks(handle, inode, NULL, start, num, flags); | 
| Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2273 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2274 | 	} else { | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2275 | 		printk(KERN_INFO "strange request: removal(2) " | 
 | 2276 | 				"%u-%u from %u:%u\n", | 
 | 2277 | 				from, to, le32_to_cpu(ex->ee_block), ee_len); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2278 | 	} | 
 | 2279 | 	return 0; | 
 | 2280 | } | 
 | 2281 |  | 
| Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2282 |  | 
 | 2283 | /* | 
 | 2284 |  * ext4_ext_rm_leaf() Removes the extents associated with the | 
 | 2285 |  * blocks appearing between "start" and "end", and splits the extents | 
 | 2286 |  * if "start" and "end" appear in the same extent | 
 | 2287 |  * | 
 | 2288 |  * @handle: The journal handle | 
 | 2289 |  * @inode:  The files inode | 
 | 2290 |  * @path:   The path to the leaf | 
 | 2291 |  * @start:  The first block to remove | 
 | 2292 |  * @end:   The last block to remove | 
 | 2293 |  */ | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2294 | static int | 
 | 2295 | ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, | 
| Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2296 | 		 struct ext4_ext_path *path, ext4_fsblk_t *partial_cluster, | 
 | 2297 | 		 ext4_lblk_t start, ext4_lblk_t end) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2298 | { | 
| Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2299 | 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2300 | 	int err = 0, correct_index = 0; | 
 | 2301 | 	int depth = ext_depth(inode), credits; | 
 | 2302 | 	struct ext4_extent_header *eh; | 
| Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2303 | 	ext4_lblk_t a, b; | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2304 | 	unsigned num; | 
 | 2305 | 	ext4_lblk_t ex_ee_block; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2306 | 	unsigned short ex_ee_len; | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2307 | 	unsigned uninitialized = 0; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2308 | 	struct ext4_extent *ex; | 
 | 2309 |  | 
| Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2310 | 	/* the header must be checked already in ext4_ext_remove_space() */ | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2311 | 	ext_debug("truncate since %u in leaf\n", start); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2312 | 	if (!path[depth].p_hdr) | 
 | 2313 | 		path[depth].p_hdr = ext_block_hdr(path[depth].p_bh); | 
 | 2314 | 	eh = path[depth].p_hdr; | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 2315 | 	if (unlikely(path[depth].p_hdr == NULL)) { | 
 | 2316 | 		EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); | 
 | 2317 | 		return -EIO; | 
 | 2318 | 	} | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2319 | 	/* find where to start removing */ | 
 | 2320 | 	ex = EXT_LAST_EXTENT(eh); | 
 | 2321 |  | 
 | 2322 | 	ex_ee_block = le32_to_cpu(ex->ee_block); | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2323 | 	ex_ee_len = ext4_ext_get_actual_len(ex); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2324 |  | 
| Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2325 | 	trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster); | 
 | 2326 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2327 | 	while (ex >= EXT_FIRST_EXTENT(eh) && | 
 | 2328 | 			ex_ee_block + ex_ee_len > start) { | 
| Aneesh Kumar K.V | a41f207 | 2009-06-10 14:22:55 -0400 | [diff] [blame] | 2329 |  | 
 | 2330 | 		if (ext4_ext_is_uninitialized(ex)) | 
 | 2331 | 			uninitialized = 1; | 
 | 2332 | 		else | 
 | 2333 | 			uninitialized = 0; | 
 | 2334 |  | 
| Mingming | 553f900 | 2009-09-18 13:34:55 -0400 | [diff] [blame] | 2335 | 		ext_debug("remove ext %u:[%d]%d\n", ex_ee_block, | 
 | 2336 | 			 uninitialized, ex_ee_len); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2337 | 		path[depth].p_ext = ex; | 
 | 2338 |  | 
 | 2339 | 		a = ex_ee_block > start ? ex_ee_block : start; | 
| Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2340 | 		b = ex_ee_block+ex_ee_len - 1 < end ? | 
 | 2341 | 			ex_ee_block+ex_ee_len - 1 : end; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2342 |  | 
 | 2343 | 		ext_debug("  border %u:%u\n", a, b); | 
 | 2344 |  | 
| Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2345 | 		/* If this extent is beyond the end of the hole, skip it */ | 
 | 2346 | 		if (end <= ex_ee_block) { | 
 | 2347 | 			ex--; | 
 | 2348 | 			ex_ee_block = le32_to_cpu(ex->ee_block); | 
 | 2349 | 			ex_ee_len = ext4_ext_get_actual_len(ex); | 
 | 2350 | 			continue; | 
| Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2351 | 		} else if (b != ex_ee_block + ex_ee_len - 1) { | 
 | 2352 | 			EXT4_ERROR_INODE(inode,"  bad truncate %u:%u\n", | 
 | 2353 | 					 start, end); | 
 | 2354 | 			err = -EIO; | 
 | 2355 | 			goto out; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2356 | 		} else if (a != ex_ee_block) { | 
 | 2357 | 			/* remove tail of the extent */ | 
| Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2358 | 			num = a - ex_ee_block; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2359 | 		} else { | 
 | 2360 | 			/* remove whole extent: excellent! */ | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2361 | 			num = 0; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2362 | 		} | 
| Theodore Ts'o | 34071da | 2008-08-01 21:59:19 -0400 | [diff] [blame] | 2363 | 		/* | 
 | 2364 | 		 * 3 for leaf, sb, and inode plus 2 (bmap and group | 
 | 2365 | 		 * descriptor) for each block group; assume two block | 
 | 2366 | 		 * groups plus ex_ee_len/blocks_per_block_group for | 
 | 2367 | 		 * the worst case | 
 | 2368 | 		 */ | 
 | 2369 | 		credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2370 | 		if (ex == EXT_FIRST_EXTENT(eh)) { | 
 | 2371 | 			correct_index = 1; | 
 | 2372 | 			credits += (ext_depth(inode)) + 1; | 
 | 2373 | 		} | 
| Dmitry Monakhov | 5aca07e | 2009-12-08 22:42:15 -0500 | [diff] [blame] | 2374 | 		credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2375 |  | 
| Jan Kara | 487caee | 2009-08-17 22:17:20 -0400 | [diff] [blame] | 2376 | 		err = ext4_ext_truncate_extend_restart(handle, inode, credits); | 
| Shen Feng | 9102e4f | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 2377 | 		if (err) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2378 | 			goto out; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2379 |  | 
 | 2380 | 		err = ext4_ext_get_access(handle, inode, path + depth); | 
 | 2381 | 		if (err) | 
 | 2382 | 			goto out; | 
 | 2383 |  | 
| Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2384 | 		err = ext4_remove_blocks(handle, inode, ex, partial_cluster, | 
 | 2385 | 					 a, b); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2386 | 		if (err) | 
 | 2387 | 			goto out; | 
 | 2388 |  | 
| Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2389 | 		if (num == 0) | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2390 | 			/* this extent is removed; mark slot entirely unused */ | 
| Alex Tomas | f65e6fb | 2006-10-11 01:21:05 -0700 | [diff] [blame] | 2391 | 			ext4_ext_store_pblock(ex, 0); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2392 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2393 | 		ex->ee_len = cpu_to_le16(num); | 
| Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 2394 | 		/* | 
 | 2395 | 		 * Do not mark uninitialized if all the blocks in the | 
 | 2396 | 		 * extent have been removed. | 
 | 2397 | 		 */ | 
 | 2398 | 		if (uninitialized && num) | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2399 | 			ext4_ext_mark_uninitialized(ex); | 
| Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2400 | 		/* | 
 | 2401 | 		 * If the extent was completely released, | 
 | 2402 | 		 * we need to remove it from the leaf | 
 | 2403 | 		 */ | 
 | 2404 | 		if (num == 0) { | 
| Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 2405 | 			if (end != EXT_MAX_BLOCKS - 1) { | 
| Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2406 | 				/* | 
 | 2407 | 				 * For hole punching, we need to scoot all the | 
 | 2408 | 				 * extents up when an extent is removed so that | 
 | 2409 | 				 * we dont have blank extents in the middle | 
 | 2410 | 				 */ | 
 | 2411 | 				memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) * | 
 | 2412 | 					sizeof(struct ext4_extent)); | 
 | 2413 |  | 
 | 2414 | 				/* Now get rid of the one at the end */ | 
 | 2415 | 				memset(EXT_LAST_EXTENT(eh), 0, | 
 | 2416 | 					sizeof(struct ext4_extent)); | 
 | 2417 | 			} | 
 | 2418 | 			le16_add_cpu(&eh->eh_entries, -1); | 
| Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2419 | 		} else | 
 | 2420 | 			*partial_cluster = 0; | 
| Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2421 |  | 
| Dmitry Monakhov | 750c9c4 | 2011-10-25 05:35:05 -0400 | [diff] [blame] | 2422 | 		err = ext4_ext_dirty(handle, inode, path + depth); | 
 | 2423 | 		if (err) | 
 | 2424 | 			goto out; | 
 | 2425 |  | 
| Yongqiang Yang | bf52c6f | 2011-11-01 18:59:26 -0400 | [diff] [blame] | 2426 | 		ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num, | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2427 | 				ext4_ext_pblock(ex)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2428 | 		ex--; | 
 | 2429 | 		ex_ee_block = le32_to_cpu(ex->ee_block); | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 2430 | 		ex_ee_len = ext4_ext_get_actual_len(ex); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2431 | 	} | 
 | 2432 |  | 
 | 2433 | 	if (correct_index && eh->eh_entries) | 
 | 2434 | 		err = ext4_ext_correct_indexes(handle, inode, path); | 
 | 2435 |  | 
| Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2436 | 	/* | 
 | 2437 | 	 * If there is still a entry in the leaf node, check to see if | 
 | 2438 | 	 * it references the partial cluster.  This is the only place | 
 | 2439 | 	 * where it could; if it doesn't, we can free the cluster. | 
 | 2440 | 	 */ | 
 | 2441 | 	if (*partial_cluster && ex >= EXT_FIRST_EXTENT(eh) && | 
 | 2442 | 	    (EXT4_B2C(sbi, ext4_ext_pblock(ex) + ex_ee_len - 1) != | 
 | 2443 | 	     *partial_cluster)) { | 
 | 2444 | 		int flags = EXT4_FREE_BLOCKS_FORGET; | 
 | 2445 |  | 
 | 2446 | 		if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) | 
 | 2447 | 			flags |= EXT4_FREE_BLOCKS_METADATA; | 
 | 2448 |  | 
 | 2449 | 		ext4_free_blocks(handle, inode, NULL, | 
 | 2450 | 				 EXT4_C2B(sbi, *partial_cluster), | 
 | 2451 | 				 sbi->s_cluster_ratio, flags); | 
 | 2452 | 		*partial_cluster = 0; | 
 | 2453 | 	} | 
 | 2454 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2455 | 	/* if this leaf is free, then we should | 
 | 2456 | 	 * remove it from index block above */ | 
 | 2457 | 	if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL) | 
 | 2458 | 		err = ext4_ext_rm_idx(handle, inode, path + depth); | 
 | 2459 |  | 
 | 2460 | out: | 
 | 2461 | 	return err; | 
 | 2462 | } | 
 | 2463 |  | 
 | 2464 | /* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2465 |  * ext4_ext_more_to_rm: | 
 | 2466 |  * returns 1 if current index has to be freed (even partial) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2467 |  */ | 
| Avantika Mathur | 09b8825 | 2006-12-06 20:41:36 -0800 | [diff] [blame] | 2468 | static int | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2469 | ext4_ext_more_to_rm(struct ext4_ext_path *path) | 
 | 2470 | { | 
 | 2471 | 	BUG_ON(path->p_idx == NULL); | 
 | 2472 |  | 
 | 2473 | 	if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr)) | 
 | 2474 | 		return 0; | 
 | 2475 |  | 
 | 2476 | 	/* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2477 | 	 * if truncate on deeper level happened, it wasn't partial, | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2478 | 	 * so we have to consider current index for truncation | 
 | 2479 | 	 */ | 
 | 2480 | 	if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block) | 
 | 2481 | 		return 0; | 
 | 2482 | 	return 1; | 
 | 2483 | } | 
 | 2484 |  | 
| Allison Henderson | c6a0371 | 2011-07-17 23:21:03 -0400 | [diff] [blame] | 2485 | static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2486 | { | 
 | 2487 | 	struct super_block *sb = inode->i_sb; | 
 | 2488 | 	int depth = ext_depth(inode); | 
 | 2489 | 	struct ext4_ext_path *path; | 
| Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2490 | 	ext4_fsblk_t partial_cluster = 0; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2491 | 	handle_t *handle; | 
| Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 2492 | 	int i, err; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2493 |  | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2494 | 	ext_debug("truncate since %u\n", start); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2495 |  | 
 | 2496 | 	/* probably first extent we're gonna free will be last in block */ | 
 | 2497 | 	handle = ext4_journal_start(inode, depth + 1); | 
 | 2498 | 	if (IS_ERR(handle)) | 
 | 2499 | 		return PTR_ERR(handle); | 
 | 2500 |  | 
| Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 2501 | again: | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2502 | 	ext4_ext_invalidate_cache(inode); | 
 | 2503 |  | 
| Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2504 | 	trace_ext4_ext_remove_space(inode, start, depth); | 
 | 2505 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2506 | 	/* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2507 | 	 * We start scanning from right side, freeing all the blocks | 
 | 2508 | 	 * after i_size and walking into the tree depth-wise. | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2509 | 	 */ | 
| Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 2510 | 	depth = ext_depth(inode); | 
| Josef Bacik | 216553c | 2008-04-29 22:02:02 -0400 | [diff] [blame] | 2511 | 	path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2512 | 	if (path == NULL) { | 
 | 2513 | 		ext4_journal_stop(handle); | 
 | 2514 | 		return -ENOMEM; | 
 | 2515 | 	} | 
| Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 2516 | 	path[0].p_depth = depth; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2517 | 	path[0].p_hdr = ext_inode_hdr(inode); | 
| Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 2518 | 	if (ext4_ext_check(inode, path[0].p_hdr, depth)) { | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2519 | 		err = -EIO; | 
 | 2520 | 		goto out; | 
 | 2521 | 	} | 
| Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 2522 | 	i = err = 0; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2523 |  | 
 | 2524 | 	while (i >= 0 && err == 0) { | 
 | 2525 | 		if (i == depth) { | 
 | 2526 | 			/* this is leaf block */ | 
| Allison Henderson | d583fb8 | 2011-05-25 07:41:43 -0400 | [diff] [blame] | 2527 | 			err = ext4_ext_rm_leaf(handle, inode, path, | 
| Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 2528 | 					       &partial_cluster, start, | 
 | 2529 | 					       EXT_MAX_BLOCKS - 1); | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2530 | 			/* root level has p_bh == NULL, brelse() eats this */ | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2531 | 			brelse(path[i].p_bh); | 
 | 2532 | 			path[i].p_bh = NULL; | 
 | 2533 | 			i--; | 
 | 2534 | 			continue; | 
 | 2535 | 		} | 
 | 2536 |  | 
 | 2537 | 		/* this is index block */ | 
 | 2538 | 		if (!path[i].p_hdr) { | 
 | 2539 | 			ext_debug("initialize header\n"); | 
 | 2540 | 			path[i].p_hdr = ext_block_hdr(path[i].p_bh); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2541 | 		} | 
 | 2542 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2543 | 		if (!path[i].p_idx) { | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2544 | 			/* this level hasn't been touched yet */ | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2545 | 			path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr); | 
 | 2546 | 			path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1; | 
 | 2547 | 			ext_debug("init index ptr: hdr 0x%p, num %d\n", | 
 | 2548 | 				  path[i].p_hdr, | 
 | 2549 | 				  le16_to_cpu(path[i].p_hdr->eh_entries)); | 
 | 2550 | 		} else { | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2551 | 			/* we were already here, see at next index */ | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2552 | 			path[i].p_idx--; | 
 | 2553 | 		} | 
 | 2554 |  | 
 | 2555 | 		ext_debug("level %d - index, first 0x%p, cur 0x%p\n", | 
 | 2556 | 				i, EXT_FIRST_INDEX(path[i].p_hdr), | 
 | 2557 | 				path[i].p_idx); | 
 | 2558 | 		if (ext4_ext_more_to_rm(path + i)) { | 
| Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2559 | 			struct buffer_head *bh; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2560 | 			/* go to the next level */ | 
| Mingming Cao | 2ae0210 | 2006-10-11 01:21:11 -0700 | [diff] [blame] | 2561 | 			ext_debug("move to level %d (block %llu)\n", | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2562 | 				  i + 1, ext4_idx_pblock(path[i].p_idx)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2563 | 			memset(path + i + 1, 0, sizeof(*path)); | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2564 | 			bh = sb_bread(sb, ext4_idx_pblock(path[i].p_idx)); | 
| Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2565 | 			if (!bh) { | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2566 | 				/* should we reset i_size? */ | 
 | 2567 | 				err = -EIO; | 
 | 2568 | 				break; | 
 | 2569 | 			} | 
| Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2570 | 			if (WARN_ON(i + 1 > depth)) { | 
 | 2571 | 				err = -EIO; | 
 | 2572 | 				break; | 
 | 2573 | 			} | 
| Aneesh Kumar K.V | 56b1986 | 2009-03-12 09:51:20 -0400 | [diff] [blame] | 2574 | 			if (ext4_ext_check(inode, ext_block_hdr(bh), | 
| Alex Tomas | c29c0ae | 2007-07-18 09:19:09 -0400 | [diff] [blame] | 2575 | 							depth - i - 1)) { | 
 | 2576 | 				err = -EIO; | 
 | 2577 | 				break; | 
 | 2578 | 			} | 
 | 2579 | 			path[i + 1].p_bh = bh; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2580 |  | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2581 | 			/* save actual number of indexes since this | 
 | 2582 | 			 * number is changed at the next iteration */ | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2583 | 			path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries); | 
 | 2584 | 			i++; | 
 | 2585 | 		} else { | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2586 | 			/* we finished processing this index, go up */ | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2587 | 			if (path[i].p_hdr->eh_entries == 0 && i > 0) { | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2588 | 				/* index is empty, remove it; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2589 | 				 * handle must be already prepared by the | 
 | 2590 | 				 * truncatei_leaf() */ | 
 | 2591 | 				err = ext4_ext_rm_idx(handle, inode, path + i); | 
 | 2592 | 			} | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2593 | 			/* root level has p_bh == NULL, brelse() eats this */ | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2594 | 			brelse(path[i].p_bh); | 
 | 2595 | 			path[i].p_bh = NULL; | 
 | 2596 | 			i--; | 
 | 2597 | 			ext_debug("return to level %d\n", i); | 
 | 2598 | 		} | 
 | 2599 | 	} | 
 | 2600 |  | 
| Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 2601 | 	trace_ext4_ext_remove_space_done(inode, start, depth, partial_cluster, | 
 | 2602 | 			path->p_hdr->eh_entries); | 
 | 2603 |  | 
| Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 2604 | 	/* If we still have something in the partial cluster and we have removed | 
 | 2605 | 	 * even the first extent, then we should free the blocks in the partial | 
 | 2606 | 	 * cluster as well. */ | 
 | 2607 | 	if (partial_cluster && path->p_hdr->eh_entries == 0) { | 
 | 2608 | 		int flags = EXT4_FREE_BLOCKS_FORGET; | 
 | 2609 |  | 
 | 2610 | 		if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) | 
 | 2611 | 			flags |= EXT4_FREE_BLOCKS_METADATA; | 
 | 2612 |  | 
 | 2613 | 		ext4_free_blocks(handle, inode, NULL, | 
 | 2614 | 				 EXT4_C2B(EXT4_SB(sb), partial_cluster), | 
 | 2615 | 				 EXT4_SB(sb)->s_cluster_ratio, flags); | 
 | 2616 | 		partial_cluster = 0; | 
 | 2617 | 	} | 
 | 2618 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2619 | 	/* TODO: flexible tree reduction should be here */ | 
 | 2620 | 	if (path->p_hdr->eh_entries == 0) { | 
 | 2621 | 		/* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 2622 | 		 * truncate to zero freed all the tree, | 
 | 2623 | 		 * so we need to correct eh_depth | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2624 | 		 */ | 
 | 2625 | 		err = ext4_ext_get_access(handle, inode, path); | 
 | 2626 | 		if (err == 0) { | 
 | 2627 | 			ext_inode_hdr(inode)->eh_depth = 0; | 
 | 2628 | 			ext_inode_hdr(inode)->eh_max = | 
| Theodore Ts'o | 55ad63b | 2009-08-28 10:40:33 -0400 | [diff] [blame] | 2629 | 				cpu_to_le16(ext4_ext_space_root(inode, 0)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2630 | 			err = ext4_ext_dirty(handle, inode, path); | 
 | 2631 | 		} | 
 | 2632 | 	} | 
 | 2633 | out: | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2634 | 	ext4_ext_drop_refs(path); | 
 | 2635 | 	kfree(path); | 
| Dmitry Monakhov | 0617b83 | 2010-05-17 01:00:00 -0400 | [diff] [blame] | 2636 | 	if (err == -EAGAIN) | 
 | 2637 | 		goto again; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2638 | 	ext4_journal_stop(handle); | 
 | 2639 |  | 
 | 2640 | 	return err; | 
 | 2641 | } | 
 | 2642 |  | 
 | 2643 | /* | 
 | 2644 |  * called at mount time | 
 | 2645 |  */ | 
 | 2646 | void ext4_ext_init(struct super_block *sb) | 
 | 2647 | { | 
 | 2648 | 	/* | 
 | 2649 | 	 * possible initialization would be here | 
 | 2650 | 	 */ | 
 | 2651 |  | 
| Theodore Ts'o | 83982b6 | 2009-01-06 14:53:16 -0500 | [diff] [blame] | 2652 | 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { | 
| Theodore Ts'o | 90576c0 | 2009-09-29 15:51:30 -0400 | [diff] [blame] | 2653 | #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS) | 
| Theodore Ts'o | 4776004 | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 2654 | 		printk(KERN_INFO "EXT4-fs: file extents enabled"); | 
| Robert P. J. Day | bbf2f9f | 2007-02-17 19:20:16 +0100 | [diff] [blame] | 2655 | #ifdef AGGRESSIVE_TEST | 
 | 2656 | 		printk(", aggressive tests"); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2657 | #endif | 
 | 2658 | #ifdef CHECK_BINSEARCH | 
 | 2659 | 		printk(", check binsearch"); | 
 | 2660 | #endif | 
 | 2661 | #ifdef EXTENTS_STATS | 
 | 2662 | 		printk(", stats"); | 
 | 2663 | #endif | 
 | 2664 | 		printk("\n"); | 
| Theodore Ts'o | 90576c0 | 2009-09-29 15:51:30 -0400 | [diff] [blame] | 2665 | #endif | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2666 | #ifdef EXTENTS_STATS | 
 | 2667 | 		spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); | 
 | 2668 | 		EXT4_SB(sb)->s_ext_min = 1 << 30; | 
 | 2669 | 		EXT4_SB(sb)->s_ext_max = 0; | 
 | 2670 | #endif | 
 | 2671 | 	} | 
 | 2672 | } | 
 | 2673 |  | 
 | 2674 | /* | 
 | 2675 |  * called at umount time | 
 | 2676 |  */ | 
 | 2677 | void ext4_ext_release(struct super_block *sb) | 
 | 2678 | { | 
| Theodore Ts'o | 83982b6 | 2009-01-06 14:53:16 -0500 | [diff] [blame] | 2679 | 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 2680 | 		return; | 
 | 2681 |  | 
 | 2682 | #ifdef EXTENTS_STATS | 
 | 2683 | 	if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) { | 
 | 2684 | 		struct ext4_sb_info *sbi = EXT4_SB(sb); | 
 | 2685 | 		printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n", | 
 | 2686 | 			sbi->s_ext_blocks, sbi->s_ext_extents, | 
 | 2687 | 			sbi->s_ext_blocks / sbi->s_ext_extents); | 
 | 2688 | 		printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n", | 
 | 2689 | 			sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max); | 
 | 2690 | 	} | 
 | 2691 | #endif | 
 | 2692 | } | 
 | 2693 |  | 
| Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2694 | /* FIXME!! we need to try to merge to left or right after zero-out  */ | 
 | 2695 | static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) | 
 | 2696 | { | 
| Lukas Czerner | 2407518 | 2010-10-27 21:30:06 -0400 | [diff] [blame] | 2697 | 	ext4_fsblk_t ee_pblock; | 
 | 2698 | 	unsigned int ee_len; | 
| Jing Zhang | b720303 | 2010-05-12 00:00:00 -0400 | [diff] [blame] | 2699 | 	int ret; | 
| Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2700 |  | 
| Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2701 | 	ee_len    = ext4_ext_get_actual_len(ex); | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 2702 | 	ee_pblock = ext4_ext_pblock(ex); | 
| Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2703 |  | 
| Theodore Ts'o | a107e5a | 2010-10-27 23:44:47 -0400 | [diff] [blame] | 2704 | 	ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS); | 
| Lukas Czerner | 2407518 | 2010-10-27 21:30:06 -0400 | [diff] [blame] | 2705 | 	if (ret > 0) | 
 | 2706 | 		ret = 0; | 
| Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2707 |  | 
| Lukas Czerner | 2407518 | 2010-10-27 21:30:06 -0400 | [diff] [blame] | 2708 | 	return ret; | 
| Aneesh Kumar K.V | 093a088 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2709 | } | 
 | 2710 |  | 
| Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 2711 | /* | 
 | 2712 |  * used by extent splitting. | 
 | 2713 |  */ | 
 | 2714 | #define EXT4_EXT_MAY_ZEROOUT	0x1  /* safe to zeroout if split fails \ | 
 | 2715 | 					due to ENOSPC */ | 
 | 2716 | #define EXT4_EXT_MARK_UNINIT1	0x2  /* mark first half uninitialized */ | 
 | 2717 | #define EXT4_EXT_MARK_UNINIT2	0x4  /* mark second half uninitialized */ | 
 | 2718 |  | 
 | 2719 | /* | 
 | 2720 |  * ext4_split_extent_at() splits an extent at given block. | 
 | 2721 |  * | 
 | 2722 |  * @handle: the journal handle | 
 | 2723 |  * @inode: the file inode | 
 | 2724 |  * @path: the path to the extent | 
 | 2725 |  * @split: the logical block where the extent is splitted. | 
 | 2726 |  * @split_flags: indicates if the extent could be zeroout if split fails, and | 
 | 2727 |  *		 the states(init or uninit) of new extents. | 
 | 2728 |  * @flags: flags used to insert new extent to extent tree. | 
 | 2729 |  * | 
 | 2730 |  * | 
 | 2731 |  * Splits extent [a, b] into two extents [a, @split) and [@split, b], states | 
 | 2732 |  * of which are deterimined by split_flag. | 
 | 2733 |  * | 
 | 2734 |  * There are two cases: | 
 | 2735 |  *  a> the extent are splitted into two extent. | 
 | 2736 |  *  b> split is not needed, and just mark the extent. | 
 | 2737 |  * | 
 | 2738 |  * return 0 on success. | 
 | 2739 |  */ | 
 | 2740 | static int ext4_split_extent_at(handle_t *handle, | 
 | 2741 | 			     struct inode *inode, | 
 | 2742 | 			     struct ext4_ext_path *path, | 
 | 2743 | 			     ext4_lblk_t split, | 
 | 2744 | 			     int split_flag, | 
 | 2745 | 			     int flags) | 
 | 2746 | { | 
 | 2747 | 	ext4_fsblk_t newblock; | 
 | 2748 | 	ext4_lblk_t ee_block; | 
 | 2749 | 	struct ext4_extent *ex, newex, orig_ex; | 
 | 2750 | 	struct ext4_extent *ex2 = NULL; | 
 | 2751 | 	unsigned int ee_len, depth; | 
 | 2752 | 	int err = 0; | 
 | 2753 |  | 
 | 2754 | 	ext_debug("ext4_split_extents_at: inode %lu, logical" | 
 | 2755 | 		"block %llu\n", inode->i_ino, (unsigned long long)split); | 
 | 2756 |  | 
 | 2757 | 	ext4_ext_show_leaf(inode, path); | 
 | 2758 |  | 
 | 2759 | 	depth = ext_depth(inode); | 
 | 2760 | 	ex = path[depth].p_ext; | 
 | 2761 | 	ee_block = le32_to_cpu(ex->ee_block); | 
 | 2762 | 	ee_len = ext4_ext_get_actual_len(ex); | 
 | 2763 | 	newblock = split - ee_block + ext4_ext_pblock(ex); | 
 | 2764 |  | 
 | 2765 | 	BUG_ON(split < ee_block || split >= (ee_block + ee_len)); | 
 | 2766 |  | 
 | 2767 | 	err = ext4_ext_get_access(handle, inode, path + depth); | 
 | 2768 | 	if (err) | 
 | 2769 | 		goto out; | 
 | 2770 |  | 
 | 2771 | 	if (split == ee_block) { | 
 | 2772 | 		/* | 
 | 2773 | 		 * case b: block @split is the block that the extent begins with | 
 | 2774 | 		 * then we just change the state of the extent, and splitting | 
 | 2775 | 		 * is not needed. | 
 | 2776 | 		 */ | 
 | 2777 | 		if (split_flag & EXT4_EXT_MARK_UNINIT2) | 
 | 2778 | 			ext4_ext_mark_uninitialized(ex); | 
 | 2779 | 		else | 
 | 2780 | 			ext4_ext_mark_initialized(ex); | 
 | 2781 |  | 
 | 2782 | 		if (!(flags & EXT4_GET_BLOCKS_PRE_IO)) | 
 | 2783 | 			ext4_ext_try_to_merge(inode, path, ex); | 
 | 2784 |  | 
 | 2785 | 		err = ext4_ext_dirty(handle, inode, path + depth); | 
 | 2786 | 		goto out; | 
 | 2787 | 	} | 
 | 2788 |  | 
 | 2789 | 	/* case a */ | 
 | 2790 | 	memcpy(&orig_ex, ex, sizeof(orig_ex)); | 
 | 2791 | 	ex->ee_len = cpu_to_le16(split - ee_block); | 
 | 2792 | 	if (split_flag & EXT4_EXT_MARK_UNINIT1) | 
 | 2793 | 		ext4_ext_mark_uninitialized(ex); | 
 | 2794 |  | 
 | 2795 | 	/* | 
 | 2796 | 	 * path may lead to new leaf, not to original leaf any more | 
 | 2797 | 	 * after ext4_ext_insert_extent() returns, | 
 | 2798 | 	 */ | 
 | 2799 | 	err = ext4_ext_dirty(handle, inode, path + depth); | 
 | 2800 | 	if (err) | 
 | 2801 | 		goto fix_extent_len; | 
 | 2802 |  | 
 | 2803 | 	ex2 = &newex; | 
 | 2804 | 	ex2->ee_block = cpu_to_le32(split); | 
 | 2805 | 	ex2->ee_len   = cpu_to_le16(ee_len - (split - ee_block)); | 
 | 2806 | 	ext4_ext_store_pblock(ex2, newblock); | 
 | 2807 | 	if (split_flag & EXT4_EXT_MARK_UNINIT2) | 
 | 2808 | 		ext4_ext_mark_uninitialized(ex2); | 
 | 2809 |  | 
 | 2810 | 	err = ext4_ext_insert_extent(handle, inode, path, &newex, flags); | 
 | 2811 | 	if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) { | 
 | 2812 | 		err = ext4_ext_zeroout(inode, &orig_ex); | 
 | 2813 | 		if (err) | 
 | 2814 | 			goto fix_extent_len; | 
 | 2815 | 		/* update the extent length and mark as initialized */ | 
 | 2816 | 		ex->ee_len = cpu_to_le32(ee_len); | 
 | 2817 | 		ext4_ext_try_to_merge(inode, path, ex); | 
 | 2818 | 		err = ext4_ext_dirty(handle, inode, path + depth); | 
 | 2819 | 		goto out; | 
 | 2820 | 	} else if (err) | 
 | 2821 | 		goto fix_extent_len; | 
 | 2822 |  | 
 | 2823 | out: | 
 | 2824 | 	ext4_ext_show_leaf(inode, path); | 
 | 2825 | 	return err; | 
 | 2826 |  | 
 | 2827 | fix_extent_len: | 
 | 2828 | 	ex->ee_len = orig_ex.ee_len; | 
 | 2829 | 	ext4_ext_dirty(handle, inode, path + depth); | 
 | 2830 | 	return err; | 
 | 2831 | } | 
 | 2832 |  | 
 | 2833 | /* | 
 | 2834 |  * ext4_split_extents() splits an extent and mark extent which is covered | 
 | 2835 |  * by @map as split_flags indicates | 
 | 2836 |  * | 
 | 2837 |  * It may result in splitting the extent into multiple extents (upto three) | 
 | 2838 |  * There are three possibilities: | 
 | 2839 |  *   a> There is no split required | 
 | 2840 |  *   b> Splits in two extents: Split is happening at either end of the extent | 
 | 2841 |  *   c> Splits in three extents: Somone is splitting in middle of the extent | 
 | 2842 |  * | 
 | 2843 |  */ | 
 | 2844 | static int ext4_split_extent(handle_t *handle, | 
 | 2845 | 			      struct inode *inode, | 
 | 2846 | 			      struct ext4_ext_path *path, | 
 | 2847 | 			      struct ext4_map_blocks *map, | 
 | 2848 | 			      int split_flag, | 
 | 2849 | 			      int flags) | 
 | 2850 | { | 
 | 2851 | 	ext4_lblk_t ee_block; | 
 | 2852 | 	struct ext4_extent *ex; | 
 | 2853 | 	unsigned int ee_len, depth; | 
 | 2854 | 	int err = 0; | 
 | 2855 | 	int uninitialized; | 
 | 2856 | 	int split_flag1, flags1; | 
 | 2857 |  | 
 | 2858 | 	depth = ext_depth(inode); | 
 | 2859 | 	ex = path[depth].p_ext; | 
 | 2860 | 	ee_block = le32_to_cpu(ex->ee_block); | 
 | 2861 | 	ee_len = ext4_ext_get_actual_len(ex); | 
 | 2862 | 	uninitialized = ext4_ext_is_uninitialized(ex); | 
 | 2863 |  | 
 | 2864 | 	if (map->m_lblk + map->m_len < ee_block + ee_len) { | 
 | 2865 | 		split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ? | 
 | 2866 | 			      EXT4_EXT_MAY_ZEROOUT : 0; | 
 | 2867 | 		flags1 = flags | EXT4_GET_BLOCKS_PRE_IO; | 
 | 2868 | 		if (uninitialized) | 
 | 2869 | 			split_flag1 |= EXT4_EXT_MARK_UNINIT1 | | 
 | 2870 | 				       EXT4_EXT_MARK_UNINIT2; | 
 | 2871 | 		err = ext4_split_extent_at(handle, inode, path, | 
 | 2872 | 				map->m_lblk + map->m_len, split_flag1, flags1); | 
| Yongqiang Yang | 9391741 | 2011-05-22 20:49:12 -0400 | [diff] [blame] | 2873 | 		if (err) | 
 | 2874 | 			goto out; | 
| Yongqiang Yang | 47ea3bb | 2011-05-03 12:23:07 -0400 | [diff] [blame] | 2875 | 	} | 
 | 2876 |  | 
 | 2877 | 	ext4_ext_drop_refs(path); | 
 | 2878 | 	path = ext4_ext_find_extent(inode, map->m_lblk, path); | 
 | 2879 | 	if (IS_ERR(path)) | 
 | 2880 | 		return PTR_ERR(path); | 
 | 2881 |  | 
 | 2882 | 	if (map->m_lblk >= ee_block) { | 
 | 2883 | 		split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT ? | 
 | 2884 | 			      EXT4_EXT_MAY_ZEROOUT : 0; | 
 | 2885 | 		if (uninitialized) | 
 | 2886 | 			split_flag1 |= EXT4_EXT_MARK_UNINIT1; | 
 | 2887 | 		if (split_flag & EXT4_EXT_MARK_UNINIT2) | 
 | 2888 | 			split_flag1 |= EXT4_EXT_MARK_UNINIT2; | 
 | 2889 | 		err = ext4_split_extent_at(handle, inode, path, | 
 | 2890 | 				map->m_lblk, split_flag1, flags); | 
 | 2891 | 		if (err) | 
 | 2892 | 			goto out; | 
 | 2893 | 	} | 
 | 2894 |  | 
 | 2895 | 	ext4_ext_show_leaf(inode, path); | 
 | 2896 | out: | 
 | 2897 | 	return err ? err : map->m_len; | 
 | 2898 | } | 
 | 2899 |  | 
| Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 2900 | #define EXT4_EXT_ZERO_LEN 7 | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2901 | /* | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 2902 |  * This function is called by ext4_ext_map_blocks() if someone tries to write | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2903 |  * to an uninitialized extent. It may result in splitting the uninitialized | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2904 |  * extent into multiple extents (up to three - one initialized and two | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2905 |  * uninitialized). | 
 | 2906 |  * There are three possibilities: | 
 | 2907 |  *   a> There is no split required: Entire extent should be initialized | 
 | 2908 |  *   b> Splits in two extents: Write is happening at either end of the extent | 
 | 2909 |  *   c> Splits in three extents: Somone is writing in middle of the extent | 
| Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 2910 |  * | 
 | 2911 |  * Pre-conditions: | 
 | 2912 |  *  - The extent pointed to by 'path' is uninitialized. | 
 | 2913 |  *  - The extent pointed to by 'path' contains a superset | 
 | 2914 |  *    of the logical span [map->m_lblk, map->m_lblk + map->m_len). | 
 | 2915 |  * | 
 | 2916 |  * Post-conditions on success: | 
 | 2917 |  *  - the returned value is the number of blocks beyond map->l_lblk | 
 | 2918 |  *    that are allocated and initialized. | 
 | 2919 |  *    It is guaranteed to be >= map->m_len. | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2920 |  */ | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2921 | static int ext4_ext_convert_to_initialized(handle_t *handle, | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 2922 | 					   struct inode *inode, | 
 | 2923 | 					   struct ext4_map_blocks *map, | 
 | 2924 | 					   struct ext4_ext_path *path) | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2925 | { | 
| Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 2926 | 	struct ext4_extent_header *eh; | 
| Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 2927 | 	struct ext4_map_blocks split_map; | 
 | 2928 | 	struct ext4_extent zero_ex; | 
 | 2929 | 	struct ext4_extent *ex; | 
| Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 2930 | 	ext4_lblk_t ee_block, eof_block; | 
| Dan Carpenter | f85b287 | 2011-10-26 03:42:36 -0400 | [diff] [blame] | 2931 | 	unsigned int ee_len, depth; | 
 | 2932 | 	int allocated; | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2933 | 	int err = 0; | 
| Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 2934 | 	int split_flag = 0; | 
| Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 2935 |  | 
 | 2936 | 	ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical" | 
 | 2937 | 		"block %llu, max_blocks %u\n", inode->i_ino, | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 2938 | 		(unsigned long long)map->m_lblk, map->m_len); | 
| Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 2939 |  | 
 | 2940 | 	eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >> | 
 | 2941 | 		inode->i_sb->s_blocksize_bits; | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 2942 | 	if (eof_block < map->m_lblk + map->m_len) | 
 | 2943 | 		eof_block = map->m_lblk + map->m_len; | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2944 |  | 
 | 2945 | 	depth = ext_depth(inode); | 
| Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 2946 | 	eh = path[depth].p_hdr; | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 2947 | 	ex = path[depth].p_ext; | 
 | 2948 | 	ee_block = le32_to_cpu(ex->ee_block); | 
 | 2949 | 	ee_len = ext4_ext_get_actual_len(ex); | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 2950 | 	allocated = ee_len - (map->m_lblk - ee_block); | 
| Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 2951 |  | 
| Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 2952 | 	trace_ext4_ext_convert_to_initialized_enter(inode, map, ex); | 
 | 2953 |  | 
 | 2954 | 	/* Pre-conditions */ | 
 | 2955 | 	BUG_ON(!ext4_ext_is_uninitialized(ex)); | 
 | 2956 | 	BUG_ON(!in_range(map->m_lblk, ee_block, ee_len)); | 
| Eric Gouriou | 6f91bc5 | 2011-10-27 11:43:23 -0400 | [diff] [blame] | 2957 |  | 
 | 2958 | 	/* | 
 | 2959 | 	 * Attempt to transfer newly initialized blocks from the currently | 
 | 2960 | 	 * uninitialized extent to its left neighbor. This is much cheaper | 
 | 2961 | 	 * than an insertion followed by a merge as those involve costly | 
 | 2962 | 	 * memmove() calls. This is the common case in steady state for | 
 | 2963 | 	 * workloads doing fallocate(FALLOC_FL_KEEP_SIZE) followed by append | 
 | 2964 | 	 * writes. | 
 | 2965 | 	 * | 
 | 2966 | 	 * Limitations of the current logic: | 
 | 2967 | 	 *  - L1: we only deal with writes at the start of the extent. | 
 | 2968 | 	 *    The approach could be extended to writes at the end | 
 | 2969 | 	 *    of the extent but this scenario was deemed less common. | 
 | 2970 | 	 *  - L2: we do not deal with writes covering the whole extent. | 
 | 2971 | 	 *    This would require removing the extent if the transfer | 
 | 2972 | 	 *    is possible. | 
 | 2973 | 	 *  - L3: we only attempt to merge with an extent stored in the | 
 | 2974 | 	 *    same extent tree node. | 
 | 2975 | 	 */ | 
 | 2976 | 	if ((map->m_lblk == ee_block) &&	/*L1*/ | 
 | 2977 | 		(map->m_len < ee_len) &&	/*L2*/ | 
 | 2978 | 		(ex > EXT_FIRST_EXTENT(eh))) {	/*L3*/ | 
 | 2979 | 		struct ext4_extent *prev_ex; | 
 | 2980 | 		ext4_lblk_t prev_lblk; | 
 | 2981 | 		ext4_fsblk_t prev_pblk, ee_pblk; | 
 | 2982 | 		unsigned int prev_len, write_len; | 
 | 2983 |  | 
 | 2984 | 		prev_ex = ex - 1; | 
 | 2985 | 		prev_lblk = le32_to_cpu(prev_ex->ee_block); | 
 | 2986 | 		prev_len = ext4_ext_get_actual_len(prev_ex); | 
 | 2987 | 		prev_pblk = ext4_ext_pblock(prev_ex); | 
 | 2988 | 		ee_pblk = ext4_ext_pblock(ex); | 
 | 2989 | 		write_len = map->m_len; | 
 | 2990 |  | 
 | 2991 | 		/* | 
 | 2992 | 		 * A transfer of blocks from 'ex' to 'prev_ex' is allowed | 
 | 2993 | 		 * upon those conditions: | 
 | 2994 | 		 * - C1: prev_ex is initialized, | 
 | 2995 | 		 * - C2: prev_ex is logically abutting ex, | 
 | 2996 | 		 * - C3: prev_ex is physically abutting ex, | 
 | 2997 | 		 * - C4: prev_ex can receive the additional blocks without | 
 | 2998 | 		 *   overflowing the (initialized) length limit. | 
 | 2999 | 		 */ | 
 | 3000 | 		if ((!ext4_ext_is_uninitialized(prev_ex)) &&		/*C1*/ | 
 | 3001 | 			((prev_lblk + prev_len) == ee_block) &&		/*C2*/ | 
 | 3002 | 			((prev_pblk + prev_len) == ee_pblk) &&		/*C3*/ | 
 | 3003 | 			(prev_len < (EXT_INIT_MAX_LEN - write_len))) {	/*C4*/ | 
 | 3004 | 			err = ext4_ext_get_access(handle, inode, path + depth); | 
 | 3005 | 			if (err) | 
 | 3006 | 				goto out; | 
 | 3007 |  | 
 | 3008 | 			trace_ext4_ext_convert_to_initialized_fastpath(inode, | 
 | 3009 | 				map, ex, prev_ex); | 
 | 3010 |  | 
 | 3011 | 			/* Shift the start of ex by 'write_len' blocks */ | 
 | 3012 | 			ex->ee_block = cpu_to_le32(ee_block + write_len); | 
 | 3013 | 			ext4_ext_store_pblock(ex, ee_pblk + write_len); | 
 | 3014 | 			ex->ee_len = cpu_to_le16(ee_len - write_len); | 
 | 3015 | 			ext4_ext_mark_uninitialized(ex); /* Restore the flag */ | 
 | 3016 |  | 
 | 3017 | 			/* Extend prev_ex by 'write_len' blocks */ | 
 | 3018 | 			prev_ex->ee_len = cpu_to_le16(prev_len + write_len); | 
 | 3019 |  | 
 | 3020 | 			/* Mark the block containing both extents as dirty */ | 
 | 3021 | 			ext4_ext_dirty(handle, inode, path + depth); | 
 | 3022 |  | 
 | 3023 | 			/* Update path to point to the right extent */ | 
 | 3024 | 			path[depth].p_ext = prev_ex; | 
 | 3025 |  | 
 | 3026 | 			/* Result: number of initialized blocks past m_lblk */ | 
 | 3027 | 			allocated = write_len; | 
 | 3028 | 			goto out; | 
 | 3029 | 		} | 
 | 3030 | 	} | 
 | 3031 |  | 
| Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3032 | 	WARN_ON(map->m_lblk < ee_block); | 
| Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3033 | 	/* | 
 | 3034 | 	 * It is safe to convert extent to initialized via explicit | 
 | 3035 | 	 * zeroout only if extent is fully insde i_size or new_size. | 
 | 3036 | 	 */ | 
| Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3037 | 	split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0; | 
| Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3038 |  | 
| Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 3039 | 	/* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */ | 
| Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3040 | 	if (ee_len <= 2*EXT4_EXT_ZERO_LEN && | 
 | 3041 | 	    (EXT4_EXT_MAY_ZEROOUT & split_flag)) { | 
 | 3042 | 		err = ext4_ext_zeroout(inode, ex); | 
| Aneesh Kumar K.V | 3977c96 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 3043 | 		if (err) | 
| Aneesh Kumar K.V | d03856b | 2008-08-02 18:51:32 -0400 | [diff] [blame] | 3044 | 			goto out; | 
| Aneesh Kumar K.V | d03856b | 2008-08-02 18:51:32 -0400 | [diff] [blame] | 3045 |  | 
 | 3046 | 		err = ext4_ext_get_access(handle, inode, path + depth); | 
 | 3047 | 		if (err) | 
 | 3048 | 			goto out; | 
| Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3049 | 		ext4_ext_mark_initialized(ex); | 
 | 3050 | 		ext4_ext_try_to_merge(inode, path, ex); | 
 | 3051 | 		err = ext4_ext_dirty(handle, inode, path + depth); | 
 | 3052 | 		goto out; | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3053 | 	} | 
| Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3054 |  | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3055 | 	/* | 
| Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3056 | 	 * four cases: | 
 | 3057 | 	 * 1. split the extent into three extents. | 
 | 3058 | 	 * 2. split the extent into two extents, zeroout the first half. | 
 | 3059 | 	 * 3. split the extent into two extents, zeroout the second half. | 
 | 3060 | 	 * 4. split the extent into two extents with out zeroout. | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3061 | 	 */ | 
| Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3062 | 	split_map.m_lblk = map->m_lblk; | 
 | 3063 | 	split_map.m_len = map->m_len; | 
 | 3064 |  | 
 | 3065 | 	if (allocated > map->m_len) { | 
 | 3066 | 		if (allocated <= EXT4_EXT_ZERO_LEN && | 
 | 3067 | 		    (EXT4_EXT_MAY_ZEROOUT & split_flag)) { | 
 | 3068 | 			/* case 3 */ | 
 | 3069 | 			zero_ex.ee_block = | 
| Allison Henderson | 9b940f8 | 2011-05-16 10:11:09 -0400 | [diff] [blame] | 3070 | 					 cpu_to_le32(map->m_lblk); | 
 | 3071 | 			zero_ex.ee_len = cpu_to_le16(allocated); | 
| Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3072 | 			ext4_ext_store_pblock(&zero_ex, | 
 | 3073 | 				ext4_ext_pblock(ex) + map->m_lblk - ee_block); | 
 | 3074 | 			err = ext4_ext_zeroout(inode, &zero_ex); | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3075 | 			if (err) | 
 | 3076 | 				goto out; | 
| Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3077 | 			split_map.m_lblk = map->m_lblk; | 
 | 3078 | 			split_map.m_len = allocated; | 
 | 3079 | 		} else if ((map->m_lblk - ee_block + map->m_len < | 
 | 3080 | 			   EXT4_EXT_ZERO_LEN) && | 
 | 3081 | 			   (EXT4_EXT_MAY_ZEROOUT & split_flag)) { | 
 | 3082 | 			/* case 2 */ | 
 | 3083 | 			if (map->m_lblk != ee_block) { | 
 | 3084 | 				zero_ex.ee_block = ex->ee_block; | 
 | 3085 | 				zero_ex.ee_len = cpu_to_le16(map->m_lblk - | 
 | 3086 | 							ee_block); | 
 | 3087 | 				ext4_ext_store_pblock(&zero_ex, | 
 | 3088 | 						      ext4_ext_pblock(ex)); | 
 | 3089 | 				err = ext4_ext_zeroout(inode, &zero_ex); | 
 | 3090 | 				if (err) | 
 | 3091 | 					goto out; | 
 | 3092 | 			} | 
 | 3093 |  | 
| Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3094 | 			split_map.m_lblk = ee_block; | 
| Allison Henderson | 9b940f8 | 2011-05-16 10:11:09 -0400 | [diff] [blame] | 3095 | 			split_map.m_len = map->m_lblk - ee_block + map->m_len; | 
 | 3096 | 			allocated = map->m_len; | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3097 | 		} | 
 | 3098 | 	} | 
| Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3099 |  | 
 | 3100 | 	allocated = ext4_split_extent(handle, inode, path, | 
 | 3101 | 				       &split_map, split_flag, 0); | 
 | 3102 | 	if (allocated < 0) | 
 | 3103 | 		err = allocated; | 
 | 3104 |  | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3105 | out: | 
 | 3106 | 	return err ? err : allocated; | 
 | 3107 | } | 
 | 3108 |  | 
| Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3109 | /* | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3110 |  * This function is called by ext4_ext_map_blocks() from | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3111 |  * ext4_get_blocks_dio_write() when DIO to write | 
 | 3112 |  * to an uninitialized extent. | 
 | 3113 |  * | 
| Paul Bolle | fd018fe | 2011-02-15 00:05:43 +0100 | [diff] [blame] | 3114 |  * Writing to an uninitialized extent may result in splitting the uninitialized | 
| Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 3115 |  * extent into multiple /initialized uninitialized extents (up to three) | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3116 |  * There are three possibilities: | 
 | 3117 |  *   a> There is no split required: Entire extent should be uninitialized | 
 | 3118 |  *   b> Splits in two extents: Write is happening at either end of the extent | 
 | 3119 |  *   c> Splits in three extents: Somone is writing in middle of the extent | 
 | 3120 |  * | 
 | 3121 |  * One of more index blocks maybe needed if the extent tree grow after | 
| Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 3122 |  * the uninitialized extent split. To prevent ENOSPC occur at the IO | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3123 |  * complete, we need to split the uninitialized extent before DIO submit | 
| Uwe Kleine-König | 421f91d | 2010-06-11 12:17:00 +0200 | [diff] [blame] | 3124 |  * the IO. The uninitialized extent called at this time will be split | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3125 |  * into three uninitialized extent(at most). After IO complete, the part | 
 | 3126 |  * being filled will be convert to initialized by the end_io callback function | 
 | 3127 |  * via ext4_convert_unwritten_extents(). | 
| Mingming | ba230c3 | 2009-11-06 04:01:23 -0500 | [diff] [blame] | 3128 |  * | 
 | 3129 |  * Returns the size of uninitialized extent to be written on success. | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3130 |  */ | 
 | 3131 | static int ext4_split_unwritten_extents(handle_t *handle, | 
 | 3132 | 					struct inode *inode, | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3133 | 					struct ext4_map_blocks *map, | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3134 | 					struct ext4_ext_path *path, | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3135 | 					int flags) | 
 | 3136 | { | 
| Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3137 | 	ext4_lblk_t eof_block; | 
 | 3138 | 	ext4_lblk_t ee_block; | 
 | 3139 | 	struct ext4_extent *ex; | 
 | 3140 | 	unsigned int ee_len; | 
 | 3141 | 	int split_flag = 0, depth; | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3142 |  | 
| Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3143 | 	ext_debug("ext4_split_unwritten_extents: inode %lu, logical" | 
 | 3144 | 		"block %llu, max_blocks %u\n", inode->i_ino, | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3145 | 		(unsigned long long)map->m_lblk, map->m_len); | 
| Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3146 |  | 
 | 3147 | 	eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >> | 
 | 3148 | 		inode->i_sb->s_blocksize_bits; | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3149 | 	if (eof_block < map->m_lblk + map->m_len) | 
 | 3150 | 		eof_block = map->m_lblk + map->m_len; | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3151 | 	/* | 
| Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3152 | 	 * It is safe to convert extent to initialized via explicit | 
 | 3153 | 	 * zeroout only if extent is fully insde i_size or new_size. | 
 | 3154 | 	 */ | 
| Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3155 | 	depth = ext_depth(inode); | 
 | 3156 | 	ex = path[depth].p_ext; | 
 | 3157 | 	ee_block = le32_to_cpu(ex->ee_block); | 
 | 3158 | 	ee_len = ext4_ext_get_actual_len(ex); | 
| Dmitry Monakhov | 21ca087 | 2010-05-16 06:00:00 -0400 | [diff] [blame] | 3159 |  | 
| Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3160 | 	split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0; | 
 | 3161 | 	split_flag |= EXT4_EXT_MARK_UNINIT2; | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3162 |  | 
| Yongqiang Yang | 667eff3 | 2011-05-03 12:25:07 -0400 | [diff] [blame] | 3163 | 	flags |= EXT4_GET_BLOCKS_PRE_IO; | 
 | 3164 | 	return ext4_split_extent(handle, inode, path, map, split_flag, flags); | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3165 | } | 
| Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3166 |  | 
| Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 3167 | static int ext4_convert_unwritten_extents_endio(handle_t *handle, | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3168 | 					      struct inode *inode, | 
 | 3169 | 					      struct ext4_ext_path *path) | 
 | 3170 | { | 
 | 3171 | 	struct ext4_extent *ex; | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3172 | 	int depth; | 
 | 3173 | 	int err = 0; | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3174 |  | 
 | 3175 | 	depth = ext_depth(inode); | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3176 | 	ex = path[depth].p_ext; | 
 | 3177 |  | 
| Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3178 | 	ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical" | 
 | 3179 | 		"block %llu, max_blocks %u\n", inode->i_ino, | 
 | 3180 | 		(unsigned long long)le32_to_cpu(ex->ee_block), | 
 | 3181 | 		ext4_ext_get_actual_len(ex)); | 
 | 3182 |  | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3183 | 	err = ext4_ext_get_access(handle, inode, path + depth); | 
 | 3184 | 	if (err) | 
 | 3185 | 		goto out; | 
 | 3186 | 	/* first mark the extent as initialized */ | 
 | 3187 | 	ext4_ext_mark_initialized(ex); | 
 | 3188 |  | 
| Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3189 | 	/* note: ext4_ext_correct_indexes() isn't needed here because | 
 | 3190 | 	 * borders are not changed | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3191 | 	 */ | 
| Yongqiang Yang | 197217a | 2011-05-03 11:45:29 -0400 | [diff] [blame] | 3192 | 	ext4_ext_try_to_merge(inode, path, ex); | 
 | 3193 |  | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3194 | 	/* Mark modified extent as dirty */ | 
 | 3195 | 	err = ext4_ext_dirty(handle, inode, path + depth); | 
 | 3196 | out: | 
 | 3197 | 	ext4_ext_show_leaf(inode, path); | 
 | 3198 | 	return err; | 
 | 3199 | } | 
 | 3200 |  | 
| Aneesh Kumar K.V | 515f41c | 2009-12-29 23:39:06 -0500 | [diff] [blame] | 3201 | static void unmap_underlying_metadata_blocks(struct block_device *bdev, | 
 | 3202 | 			sector_t block, int count) | 
 | 3203 | { | 
 | 3204 | 	int i; | 
 | 3205 | 	for (i = 0; i < count; i++) | 
 | 3206 |                 unmap_underlying_metadata(bdev, block + i); | 
 | 3207 | } | 
 | 3208 |  | 
| Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3209 | /* | 
 | 3210 |  * Handle EOFBLOCKS_FL flag, clearing it if necessary | 
 | 3211 |  */ | 
 | 3212 | static int check_eofblocks_fl(handle_t *handle, struct inode *inode, | 
| Eric Sandeen | d002ebf | 2011-01-10 13:03:35 -0500 | [diff] [blame] | 3213 | 			      ext4_lblk_t lblk, | 
| Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3214 | 			      struct ext4_ext_path *path, | 
 | 3215 | 			      unsigned int len) | 
 | 3216 | { | 
 | 3217 | 	int i, depth; | 
 | 3218 | 	struct ext4_extent_header *eh; | 
| Sergey Senozhatsky | 65922cb | 2011-03-23 14:08:27 -0400 | [diff] [blame] | 3219 | 	struct ext4_extent *last_ex; | 
| Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3220 |  | 
 | 3221 | 	if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS)) | 
 | 3222 | 		return 0; | 
 | 3223 |  | 
 | 3224 | 	depth = ext_depth(inode); | 
 | 3225 | 	eh = path[depth].p_hdr; | 
| Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3226 |  | 
 | 3227 | 	if (unlikely(!eh->eh_entries)) { | 
 | 3228 | 		EXT4_ERROR_INODE(inode, "eh->eh_entries == 0 and " | 
 | 3229 | 				 "EOFBLOCKS_FL set"); | 
 | 3230 | 		return -EIO; | 
 | 3231 | 	} | 
 | 3232 | 	last_ex = EXT_LAST_EXTENT(eh); | 
 | 3233 | 	/* | 
 | 3234 | 	 * We should clear the EOFBLOCKS_FL flag if we are writing the | 
 | 3235 | 	 * last block in the last extent in the file.  We test this by | 
 | 3236 | 	 * first checking to see if the caller to | 
 | 3237 | 	 * ext4_ext_get_blocks() was interested in the last block (or | 
 | 3238 | 	 * a block beyond the last block) in the current extent.  If | 
 | 3239 | 	 * this turns out to be false, we can bail out from this | 
 | 3240 | 	 * function immediately. | 
 | 3241 | 	 */ | 
| Eric Sandeen | d002ebf | 2011-01-10 13:03:35 -0500 | [diff] [blame] | 3242 | 	if (lblk + len < le32_to_cpu(last_ex->ee_block) + | 
| Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3243 | 	    ext4_ext_get_actual_len(last_ex)) | 
 | 3244 | 		return 0; | 
 | 3245 | 	/* | 
 | 3246 | 	 * If the caller does appear to be planning to write at or | 
 | 3247 | 	 * beyond the end of the current extent, we then test to see | 
 | 3248 | 	 * if the current extent is the last extent in the file, by | 
 | 3249 | 	 * checking to make sure it was reached via the rightmost node | 
 | 3250 | 	 * at each level of the tree. | 
 | 3251 | 	 */ | 
 | 3252 | 	for (i = depth-1; i >= 0; i--) | 
 | 3253 | 		if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr)) | 
 | 3254 | 			return 0; | 
 | 3255 | 	ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); | 
 | 3256 | 	return ext4_mark_inode_dirty(handle, inode); | 
 | 3257 | } | 
 | 3258 |  | 
| Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3259 | /** | 
 | 3260 |  * ext4_find_delalloc_range: find delayed allocated block in the given range. | 
 | 3261 |  * | 
 | 3262 |  * Goes through the buffer heads in the range [lblk_start, lblk_end] and returns | 
 | 3263 |  * whether there are any buffers marked for delayed allocation. It returns '1' | 
 | 3264 |  * on the first delalloc'ed buffer head found. If no buffer head in the given | 
 | 3265 |  * range is marked for delalloc, it returns 0. | 
 | 3266 |  * lblk_start should always be <= lblk_end. | 
 | 3267 |  * search_hint_reverse is to indicate that searching in reverse from lblk_end to | 
 | 3268 |  * lblk_start might be more efficient (i.e., we will likely hit the delalloc'ed | 
 | 3269 |  * block sooner). This is useful when blocks are truncated sequentially from | 
 | 3270 |  * lblk_start towards lblk_end. | 
 | 3271 |  */ | 
 | 3272 | static int ext4_find_delalloc_range(struct inode *inode, | 
 | 3273 | 				    ext4_lblk_t lblk_start, | 
 | 3274 | 				    ext4_lblk_t lblk_end, | 
 | 3275 | 				    int search_hint_reverse) | 
 | 3276 | { | 
 | 3277 | 	struct address_space *mapping = inode->i_mapping; | 
 | 3278 | 	struct buffer_head *head, *bh = NULL; | 
 | 3279 | 	struct page *page; | 
 | 3280 | 	ext4_lblk_t i, pg_lblk; | 
 | 3281 | 	pgoff_t index; | 
 | 3282 |  | 
| Robin Dong | 8c48f7e | 2011-12-18 23:05:43 -0500 | [diff] [blame] | 3283 | 	if (!test_opt(inode->i_sb, DELALLOC)) | 
 | 3284 | 		return 0; | 
 | 3285 |  | 
| Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3286 | 	/* reverse search wont work if fs block size is less than page size */ | 
 | 3287 | 	if (inode->i_blkbits < PAGE_CACHE_SHIFT) | 
 | 3288 | 		search_hint_reverse = 0; | 
 | 3289 |  | 
 | 3290 | 	if (search_hint_reverse) | 
 | 3291 | 		i = lblk_end; | 
 | 3292 | 	else | 
 | 3293 | 		i = lblk_start; | 
 | 3294 |  | 
 | 3295 | 	index = i >> (PAGE_CACHE_SHIFT - inode->i_blkbits); | 
 | 3296 |  | 
 | 3297 | 	while ((i >= lblk_start) && (i <= lblk_end)) { | 
 | 3298 | 		page = find_get_page(mapping, index); | 
| Aditya Kali | 5356f26 | 2011-09-09 19:20:51 -0400 | [diff] [blame] | 3299 | 		if (!page) | 
| Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3300 | 			goto nextpage; | 
 | 3301 |  | 
| Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3302 | 		if (!page_has_buffers(page)) | 
 | 3303 | 			goto nextpage; | 
 | 3304 |  | 
 | 3305 | 		head = page_buffers(page); | 
 | 3306 | 		if (!head) | 
 | 3307 | 			goto nextpage; | 
 | 3308 |  | 
 | 3309 | 		bh = head; | 
 | 3310 | 		pg_lblk = index << (PAGE_CACHE_SHIFT - | 
 | 3311 | 						inode->i_blkbits); | 
 | 3312 | 		do { | 
 | 3313 | 			if (unlikely(pg_lblk < lblk_start)) { | 
 | 3314 | 				/* | 
 | 3315 | 				 * This is possible when fs block size is less | 
 | 3316 | 				 * than page size and our cluster starts/ends in | 
 | 3317 | 				 * middle of the page. So we need to skip the | 
 | 3318 | 				 * initial few blocks till we reach the 'lblk' | 
 | 3319 | 				 */ | 
 | 3320 | 				pg_lblk++; | 
 | 3321 | 				continue; | 
 | 3322 | 			} | 
 | 3323 |  | 
| Aditya Kali | 5356f26 | 2011-09-09 19:20:51 -0400 | [diff] [blame] | 3324 | 			/* Check if the buffer is delayed allocated and that it | 
 | 3325 | 			 * is not yet mapped. (when da-buffers are mapped during | 
 | 3326 | 			 * their writeout, their da_mapped bit is set.) | 
 | 3327 | 			 */ | 
 | 3328 | 			if (buffer_delay(bh) && !buffer_da_mapped(bh)) { | 
| Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3329 | 				page_cache_release(page); | 
| Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3330 | 				trace_ext4_find_delalloc_range(inode, | 
 | 3331 | 						lblk_start, lblk_end, | 
 | 3332 | 						search_hint_reverse, | 
 | 3333 | 						1, i); | 
| Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3334 | 				return 1; | 
 | 3335 | 			} | 
 | 3336 | 			if (search_hint_reverse) | 
 | 3337 | 				i--; | 
 | 3338 | 			else | 
 | 3339 | 				i++; | 
 | 3340 | 		} while ((i >= lblk_start) && (i <= lblk_end) && | 
 | 3341 | 				((bh = bh->b_this_page) != head)); | 
 | 3342 | nextpage: | 
 | 3343 | 		if (page) | 
 | 3344 | 			page_cache_release(page); | 
 | 3345 | 		/* | 
 | 3346 | 		 * Move to next page. 'i' will be the first lblk in the next | 
 | 3347 | 		 * page. | 
 | 3348 | 		 */ | 
 | 3349 | 		if (search_hint_reverse) | 
 | 3350 | 			index--; | 
 | 3351 | 		else | 
 | 3352 | 			index++; | 
 | 3353 | 		i = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); | 
 | 3354 | 	} | 
 | 3355 |  | 
| Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3356 | 	trace_ext4_find_delalloc_range(inode, lblk_start, lblk_end, | 
 | 3357 | 					search_hint_reverse, 0, 0); | 
| Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3358 | 	return 0; | 
 | 3359 | } | 
 | 3360 |  | 
 | 3361 | int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk, | 
 | 3362 | 			       int search_hint_reverse) | 
 | 3363 | { | 
 | 3364 | 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | 
 | 3365 | 	ext4_lblk_t lblk_start, lblk_end; | 
 | 3366 | 	lblk_start = lblk & (~(sbi->s_cluster_ratio - 1)); | 
 | 3367 | 	lblk_end = lblk_start + sbi->s_cluster_ratio - 1; | 
 | 3368 |  | 
 | 3369 | 	return ext4_find_delalloc_range(inode, lblk_start, lblk_end, | 
 | 3370 | 					search_hint_reverse); | 
 | 3371 | } | 
 | 3372 |  | 
 | 3373 | /** | 
 | 3374 |  * Determines how many complete clusters (out of those specified by the 'map') | 
 | 3375 |  * are under delalloc and were reserved quota for. | 
 | 3376 |  * This function is called when we are writing out the blocks that were | 
 | 3377 |  * originally written with their allocation delayed, but then the space was | 
 | 3378 |  * allocated using fallocate() before the delayed allocation could be resolved. | 
 | 3379 |  * The cases to look for are: | 
 | 3380 |  * ('=' indicated delayed allocated blocks | 
 | 3381 |  *  '-' indicates non-delayed allocated blocks) | 
 | 3382 |  * (a) partial clusters towards beginning and/or end outside of allocated range | 
 | 3383 |  *     are not delalloc'ed. | 
 | 3384 |  *	Ex: | 
 | 3385 |  *	|----c---=|====c====|====c====|===-c----| | 
 | 3386 |  *	         |++++++ allocated ++++++| | 
 | 3387 |  *	==> 4 complete clusters in above example | 
 | 3388 |  * | 
 | 3389 |  * (b) partial cluster (outside of allocated range) towards either end is | 
 | 3390 |  *     marked for delayed allocation. In this case, we will exclude that | 
 | 3391 |  *     cluster. | 
 | 3392 |  *	Ex: | 
 | 3393 |  *	|----====c========|========c========| | 
 | 3394 |  *	     |++++++ allocated ++++++| | 
 | 3395 |  *	==> 1 complete clusters in above example | 
 | 3396 |  * | 
 | 3397 |  *	Ex: | 
 | 3398 |  *	|================c================| | 
 | 3399 |  *            |++++++ allocated ++++++| | 
 | 3400 |  *	==> 0 complete clusters in above example | 
 | 3401 |  * | 
 | 3402 |  * The ext4_da_update_reserve_space will be called only if we | 
 | 3403 |  * determine here that there were some "entire" clusters that span | 
 | 3404 |  * this 'allocated' range. | 
 | 3405 |  * In the non-bigalloc case, this function will just end up returning num_blks | 
 | 3406 |  * without ever calling ext4_find_delalloc_range. | 
 | 3407 |  */ | 
 | 3408 | static unsigned int | 
 | 3409 | get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start, | 
 | 3410 | 			   unsigned int num_blks) | 
 | 3411 | { | 
 | 3412 | 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | 
 | 3413 | 	ext4_lblk_t alloc_cluster_start, alloc_cluster_end; | 
 | 3414 | 	ext4_lblk_t lblk_from, lblk_to, c_offset; | 
 | 3415 | 	unsigned int allocated_clusters = 0; | 
 | 3416 |  | 
 | 3417 | 	alloc_cluster_start = EXT4_B2C(sbi, lblk_start); | 
 | 3418 | 	alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1); | 
 | 3419 |  | 
 | 3420 | 	/* max possible clusters for this allocation */ | 
 | 3421 | 	allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1; | 
 | 3422 |  | 
| Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3423 | 	trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks); | 
 | 3424 |  | 
| Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3425 | 	/* Check towards left side */ | 
 | 3426 | 	c_offset = lblk_start & (sbi->s_cluster_ratio - 1); | 
 | 3427 | 	if (c_offset) { | 
 | 3428 | 		lblk_from = lblk_start & (~(sbi->s_cluster_ratio - 1)); | 
 | 3429 | 		lblk_to = lblk_from + c_offset - 1; | 
 | 3430 |  | 
 | 3431 | 		if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0)) | 
 | 3432 | 			allocated_clusters--; | 
 | 3433 | 	} | 
 | 3434 |  | 
 | 3435 | 	/* Now check towards right. */ | 
 | 3436 | 	c_offset = (lblk_start + num_blks) & (sbi->s_cluster_ratio - 1); | 
 | 3437 | 	if (allocated_clusters && c_offset) { | 
 | 3438 | 		lblk_from = lblk_start + num_blks; | 
 | 3439 | 		lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1; | 
 | 3440 |  | 
 | 3441 | 		if (ext4_find_delalloc_range(inode, lblk_from, lblk_to, 0)) | 
 | 3442 | 			allocated_clusters--; | 
 | 3443 | 	} | 
 | 3444 |  | 
 | 3445 | 	return allocated_clusters; | 
 | 3446 | } | 
 | 3447 |  | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3448 | static int | 
 | 3449 | ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode, | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3450 | 			struct ext4_map_blocks *map, | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3451 | 			struct ext4_ext_path *path, int flags, | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3452 | 			unsigned int allocated, ext4_fsblk_t newblock) | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3453 | { | 
 | 3454 | 	int ret = 0; | 
 | 3455 | 	int err = 0; | 
| Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3456 | 	ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio; | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3457 |  | 
| Zheng Liu | 88635ca | 2011-12-28 19:00:25 -0500 | [diff] [blame] | 3458 | 	ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical " | 
 | 3459 | 		  "block %llu, max_blocks %u, flags %x, allocated %u\n", | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3460 | 		  inode->i_ino, (unsigned long long)map->m_lblk, map->m_len, | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3461 | 		  flags, allocated); | 
 | 3462 | 	ext4_ext_show_leaf(inode, path); | 
 | 3463 |  | 
| Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3464 | 	trace_ext4_ext_handle_uninitialized_extents(inode, map, allocated, | 
 | 3465 | 						    newblock); | 
 | 3466 |  | 
| Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 3467 | 	/* get_block() before submit the IO, split the extent */ | 
| Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 3468 | 	if ((flags & EXT4_GET_BLOCKS_PRE_IO)) { | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3469 | 		ret = ext4_split_unwritten_extents(handle, inode, map, | 
 | 3470 | 						   path, flags); | 
| Mingming | 5f52495 | 2009-11-10 10:48:04 -0500 | [diff] [blame] | 3471 | 		/* | 
 | 3472 | 		 * Flag the inode(non aio case) or end_io struct (aio case) | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3473 | 		 * that this IO needs to conversion to written when IO is | 
| Mingming | 5f52495 | 2009-11-10 10:48:04 -0500 | [diff] [blame] | 3474 | 		 * completed | 
 | 3475 | 		 */ | 
| Tao Ma | 0edeb71 | 2011-10-31 17:30:44 -0400 | [diff] [blame] | 3476 | 		if (io) | 
 | 3477 | 			ext4_set_io_unwritten_flag(inode, io); | 
 | 3478 | 		else | 
| Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 3479 | 			ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); | 
| Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 3480 | 		if (ext4_should_dioread_nolock(inode)) | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3481 | 			map->m_flags |= EXT4_MAP_UNINIT; | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3482 | 		goto out; | 
 | 3483 | 	} | 
| Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 3484 | 	/* IO end_io complete, convert the filled extent to written */ | 
| Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 3485 | 	if ((flags & EXT4_GET_BLOCKS_CONVERT)) { | 
| Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 3486 | 		ret = ext4_convert_unwritten_extents_endio(handle, inode, | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3487 | 							path); | 
| Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3488 | 		if (ret >= 0) { | 
| Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 3489 | 			ext4_update_inode_fsync_trans(handle, inode, 1); | 
| Eric Sandeen | d002ebf | 2011-01-10 13:03:35 -0500 | [diff] [blame] | 3490 | 			err = check_eofblocks_fl(handle, inode, map->m_lblk, | 
 | 3491 | 						 path, map->m_len); | 
| Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3492 | 		} else | 
 | 3493 | 			err = ret; | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3494 | 		goto out2; | 
 | 3495 | 	} | 
 | 3496 | 	/* buffered IO case */ | 
 | 3497 | 	/* | 
 | 3498 | 	 * repeat fallocate creation request | 
 | 3499 | 	 * we already have an unwritten extent | 
 | 3500 | 	 */ | 
 | 3501 | 	if (flags & EXT4_GET_BLOCKS_UNINIT_EXT) | 
 | 3502 | 		goto map_out; | 
 | 3503 |  | 
 | 3504 | 	/* buffered READ or buffered write_begin() lookup */ | 
 | 3505 | 	if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { | 
 | 3506 | 		/* | 
 | 3507 | 		 * We have blocks reserved already.  We | 
 | 3508 | 		 * return allocated blocks so that delalloc | 
 | 3509 | 		 * won't do block reservation for us.  But | 
 | 3510 | 		 * the buffer head will be unmapped so that | 
 | 3511 | 		 * a read from the block returns 0s. | 
 | 3512 | 		 */ | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3513 | 		map->m_flags |= EXT4_MAP_UNWRITTEN; | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3514 | 		goto out1; | 
 | 3515 | 	} | 
 | 3516 |  | 
 | 3517 | 	/* buffered write, writepage time, convert*/ | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3518 | 	ret = ext4_ext_convert_to_initialized(handle, inode, map, path); | 
| Dmitry Monakhov | a4e5d88 | 2011-10-25 08:15:12 -0400 | [diff] [blame] | 3519 | 	if (ret >= 0) | 
| Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 3520 | 		ext4_update_inode_fsync_trans(handle, inode, 1); | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3521 | out: | 
 | 3522 | 	if (ret <= 0) { | 
 | 3523 | 		err = ret; | 
 | 3524 | 		goto out2; | 
 | 3525 | 	} else | 
 | 3526 | 		allocated = ret; | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3527 | 	map->m_flags |= EXT4_MAP_NEW; | 
| Aneesh Kumar K.V | 515f41c | 2009-12-29 23:39:06 -0500 | [diff] [blame] | 3528 | 	/* | 
 | 3529 | 	 * if we allocated more blocks than requested | 
 | 3530 | 	 * we need to make sure we unmap the extra block | 
 | 3531 | 	 * allocated. The actual needed block will get | 
 | 3532 | 	 * unmapped later when we find the buffer_head marked | 
 | 3533 | 	 * new. | 
 | 3534 | 	 */ | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3535 | 	if (allocated > map->m_len) { | 
| Aneesh Kumar K.V | 515f41c | 2009-12-29 23:39:06 -0500 | [diff] [blame] | 3536 | 		unmap_underlying_metadata_blocks(inode->i_sb->s_bdev, | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3537 | 					newblock + map->m_len, | 
 | 3538 | 					allocated - map->m_len); | 
 | 3539 | 		allocated = map->m_len; | 
| Aneesh Kumar K.V | 515f41c | 2009-12-29 23:39:06 -0500 | [diff] [blame] | 3540 | 	} | 
| Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 3541 |  | 
 | 3542 | 	/* | 
 | 3543 | 	 * If we have done fallocate with the offset that is already | 
 | 3544 | 	 * delayed allocated, we would have block reservation | 
 | 3545 | 	 * and quota reservation done in the delayed write path. | 
 | 3546 | 	 * But fallocate would have already updated quota and block | 
 | 3547 | 	 * count for this offset. So cancel these reservation | 
 | 3548 | 	 */ | 
| Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3549 | 	if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) { | 
 | 3550 | 		unsigned int reserved_clusters; | 
 | 3551 | 		reserved_clusters = get_reserved_cluster_alloc(inode, | 
 | 3552 | 				map->m_lblk, map->m_len); | 
 | 3553 | 		if (reserved_clusters) | 
 | 3554 | 			ext4_da_update_reserve_space(inode, | 
 | 3555 | 						     reserved_clusters, | 
 | 3556 | 						     0); | 
 | 3557 | 	} | 
| Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 3558 |  | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3559 | map_out: | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3560 | 	map->m_flags |= EXT4_MAP_MAPPED; | 
| Dmitry Monakhov | a4e5d88 | 2011-10-25 08:15:12 -0400 | [diff] [blame] | 3561 | 	if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) { | 
 | 3562 | 		err = check_eofblocks_fl(handle, inode, map->m_lblk, path, | 
 | 3563 | 					 map->m_len); | 
 | 3564 | 		if (err < 0) | 
 | 3565 | 			goto out2; | 
 | 3566 | 	} | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3567 | out1: | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3568 | 	if (allocated > map->m_len) | 
 | 3569 | 		allocated = map->m_len; | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3570 | 	ext4_ext_show_leaf(inode, path); | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3571 | 	map->m_pblk = newblock; | 
 | 3572 | 	map->m_len = allocated; | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3573 | out2: | 
 | 3574 | 	if (path) { | 
 | 3575 | 		ext4_ext_drop_refs(path); | 
 | 3576 | 		kfree(path); | 
 | 3577 | 	} | 
 | 3578 | 	return err ? err : allocated; | 
 | 3579 | } | 
| Theodore Ts'o | 58590b0 | 2010-10-27 21:23:12 -0400 | [diff] [blame] | 3580 |  | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 3581 | /* | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3582 |  * get_implied_cluster_alloc - check to see if the requested | 
 | 3583 |  * allocation (in the map structure) overlaps with a cluster already | 
 | 3584 |  * allocated in an extent. | 
| Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3585 |  *	@sb	The filesystem superblock structure | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3586 |  *	@map	The requested lblk->pblk mapping | 
 | 3587 |  *	@ex	The extent structure which might contain an implied | 
 | 3588 |  *			cluster allocation | 
 | 3589 |  * | 
 | 3590 |  * This function is called by ext4_ext_map_blocks() after we failed to | 
 | 3591 |  * find blocks that were already in the inode's extent tree.  Hence, | 
 | 3592 |  * we know that the beginning of the requested region cannot overlap | 
 | 3593 |  * the extent from the inode's extent tree.  There are three cases we | 
 | 3594 |  * want to catch.  The first is this case: | 
 | 3595 |  * | 
 | 3596 |  *		 |--- cluster # N--| | 
 | 3597 |  *    |--- extent ---|	|---- requested region ---| | 
 | 3598 |  *			|==========| | 
 | 3599 |  * | 
 | 3600 |  * The second case that we need to test for is this one: | 
 | 3601 |  * | 
 | 3602 |  *   |--------- cluster # N ----------------| | 
 | 3603 |  *	   |--- requested region --|   |------- extent ----| | 
 | 3604 |  *	   |=======================| | 
 | 3605 |  * | 
 | 3606 |  * The third case is when the requested region lies between two extents | 
 | 3607 |  * within the same cluster: | 
 | 3608 |  *          |------------- cluster # N-------------| | 
 | 3609 |  * |----- ex -----|                  |---- ex_right ----| | 
 | 3610 |  *                  |------ requested region ------| | 
 | 3611 |  *                  |================| | 
 | 3612 |  * | 
 | 3613 |  * In each of the above cases, we need to set the map->m_pblk and | 
 | 3614 |  * map->m_len so it corresponds to the return the extent labelled as | 
 | 3615 |  * "|====|" from cluster #N, since it is already in use for data in | 
 | 3616 |  * cluster EXT4_B2C(sbi, map->m_lblk).	We will then return 1 to | 
 | 3617 |  * signal to ext4_ext_map_blocks() that map->m_pblk should be treated | 
 | 3618 |  * as a new "allocated" block region.  Otherwise, we will return 0 and | 
 | 3619 |  * ext4_ext_map_blocks() will then allocate one or more new clusters | 
 | 3620 |  * by calling ext4_mb_new_blocks(). | 
 | 3621 |  */ | 
| Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3622 | static int get_implied_cluster_alloc(struct super_block *sb, | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3623 | 				     struct ext4_map_blocks *map, | 
 | 3624 | 				     struct ext4_extent *ex, | 
 | 3625 | 				     struct ext4_ext_path *path) | 
 | 3626 | { | 
| Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3627 | 	struct ext4_sb_info *sbi = EXT4_SB(sb); | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3628 | 	ext4_lblk_t c_offset = map->m_lblk & (sbi->s_cluster_ratio-1); | 
 | 3629 | 	ext4_lblk_t ex_cluster_start, ex_cluster_end; | 
| Curt Wohlgemuth | 14d7f3e | 2011-12-18 17:39:02 -0500 | [diff] [blame] | 3630 | 	ext4_lblk_t rr_cluster_start; | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3631 | 	ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); | 
 | 3632 | 	ext4_fsblk_t ee_start = ext4_ext_pblock(ex); | 
 | 3633 | 	unsigned short ee_len = ext4_ext_get_actual_len(ex); | 
 | 3634 |  | 
 | 3635 | 	/* The extent passed in that we are trying to match */ | 
 | 3636 | 	ex_cluster_start = EXT4_B2C(sbi, ee_block); | 
 | 3637 | 	ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1); | 
 | 3638 |  | 
 | 3639 | 	/* The requested region passed into ext4_map_blocks() */ | 
 | 3640 | 	rr_cluster_start = EXT4_B2C(sbi, map->m_lblk); | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3641 |  | 
 | 3642 | 	if ((rr_cluster_start == ex_cluster_end) || | 
 | 3643 | 	    (rr_cluster_start == ex_cluster_start)) { | 
 | 3644 | 		if (rr_cluster_start == ex_cluster_end) | 
 | 3645 | 			ee_start += ee_len - 1; | 
 | 3646 | 		map->m_pblk = (ee_start & ~(sbi->s_cluster_ratio - 1)) + | 
 | 3647 | 			c_offset; | 
 | 3648 | 		map->m_len = min(map->m_len, | 
 | 3649 | 				 (unsigned) sbi->s_cluster_ratio - c_offset); | 
 | 3650 | 		/* | 
 | 3651 | 		 * Check for and handle this case: | 
 | 3652 | 		 * | 
 | 3653 | 		 *   |--------- cluster # N-------------| | 
 | 3654 | 		 *		       |------- extent ----| | 
 | 3655 | 		 *	   |--- requested region ---| | 
 | 3656 | 		 *	   |===========| | 
 | 3657 | 		 */ | 
 | 3658 |  | 
 | 3659 | 		if (map->m_lblk < ee_block) | 
 | 3660 | 			map->m_len = min(map->m_len, ee_block - map->m_lblk); | 
 | 3661 |  | 
 | 3662 | 		/* | 
 | 3663 | 		 * Check for the case where there is already another allocated | 
 | 3664 | 		 * block to the right of 'ex' but before the end of the cluster. | 
 | 3665 | 		 * | 
 | 3666 | 		 *          |------------- cluster # N-------------| | 
 | 3667 | 		 * |----- ex -----|                  |---- ex_right ----| | 
 | 3668 | 		 *                  |------ requested region ------| | 
 | 3669 | 		 *                  |================| | 
 | 3670 | 		 */ | 
 | 3671 | 		if (map->m_lblk > ee_block) { | 
 | 3672 | 			ext4_lblk_t next = ext4_ext_next_allocated_block(path); | 
 | 3673 | 			map->m_len = min(map->m_len, next - map->m_lblk); | 
 | 3674 | 		} | 
| Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3675 |  | 
 | 3676 | 		trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1); | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3677 | 		return 1; | 
 | 3678 | 	} | 
| Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3679 |  | 
 | 3680 | 	trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0); | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3681 | 	return 0; | 
 | 3682 | } | 
 | 3683 |  | 
 | 3684 |  | 
 | 3685 | /* | 
| Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 3686 |  * Block allocation/map/preallocation routine for extents based files | 
 | 3687 |  * | 
 | 3688 |  * | 
| Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3689 |  * Need to be called with | 
| Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 3690 |  * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block | 
 | 3691 |  * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem) | 
| Mingming Cao | f5ab0d1 | 2008-02-25 15:29:55 -0500 | [diff] [blame] | 3692 |  * | 
 | 3693 |  * return > 0, number of of blocks already mapped/allocated | 
 | 3694 |  *          if create == 0 and these are pre-allocated blocks | 
 | 3695 |  *          	buffer head is unmapped | 
 | 3696 |  *          otherwise blocks are mapped | 
 | 3697 |  * | 
 | 3698 |  * return = 0, if plain look up failed (blocks have not been allocated) | 
 | 3699 |  *          buffer head is unmapped | 
 | 3700 |  * | 
 | 3701 |  * return < 0, error case. | 
| Aneesh Kumar K.V | c278bfe | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3702 |  */ | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3703 | int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, | 
 | 3704 | 			struct ext4_map_blocks *map, int flags) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3705 | { | 
 | 3706 | 	struct ext4_ext_path *path = NULL; | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3707 | 	struct ext4_extent newex, *ex, *ex2; | 
 | 3708 | 	struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | 
| Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 3709 | 	ext4_fsblk_t newblock = 0; | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3710 | 	int free_on_err = 0, err = 0, depth, ret; | 
 | 3711 | 	unsigned int allocated = 0, offset = 0; | 
| Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 3712 | 	unsigned int allocated_clusters = 0; | 
| Allison Henderson | e861304 | 2011-05-25 07:41:46 -0400 | [diff] [blame] | 3713 | 	unsigned int punched_out = 0; | 
 | 3714 | 	unsigned int result = 0; | 
| Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3715 | 	struct ext4_allocation_request ar; | 
| Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 3716 | 	ext4_io_end_t *io = EXT4_I(inode)->cur_aio_dio; | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3717 | 	ext4_lblk_t cluster_offset; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3718 |  | 
| Mingming | 84fe3be | 2009-09-01 08:44:37 -0400 | [diff] [blame] | 3719 | 	ext_debug("blocks %u/%u requested for inode %lu\n", | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3720 | 		  map->m_lblk, map->m_len, inode->i_ino); | 
| Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 3721 | 	trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3722 |  | 
 | 3723 | 	/* check in cache */ | 
| Robin Dong | 015861b | 2011-07-17 23:27:43 -0400 | [diff] [blame] | 3724 | 	if (!(flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) && | 
 | 3725 | 		ext4_ext_in_cache(inode, map->m_lblk, &newex)) { | 
| Theodore Ts'o | b05e6ae | 2011-01-10 12:13:26 -0500 | [diff] [blame] | 3726 | 		if (!newex.ee_start_lo && !newex.ee_start_hi) { | 
| Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3727 | 			if ((sbi->s_cluster_ratio > 1) && | 
 | 3728 | 			    ext4_find_delalloc_cluster(inode, map->m_lblk, 0)) | 
 | 3729 | 				map->m_flags |= EXT4_MAP_FROM_CLUSTER; | 
 | 3730 |  | 
| Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 3731 | 			if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3732 | 				/* | 
 | 3733 | 				 * block isn't allocated yet and | 
 | 3734 | 				 * user doesn't want to allocate it | 
 | 3735 | 				 */ | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3736 | 				goto out2; | 
 | 3737 | 			} | 
 | 3738 | 			/* we should allocate requested block */ | 
| Theodore Ts'o | b05e6ae | 2011-01-10 12:13:26 -0500 | [diff] [blame] | 3739 | 		} else { | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3740 | 			/* block is already allocated */ | 
| Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3741 | 			if (sbi->s_cluster_ratio > 1) | 
 | 3742 | 				map->m_flags |= EXT4_MAP_FROM_CLUSTER; | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3743 | 			newblock = map->m_lblk | 
| Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 3744 | 				   - le32_to_cpu(newex.ee_block) | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 3745 | 				   + ext4_ext_pblock(&newex); | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3746 | 			/* number of remaining blocks in the extent */ | 
| Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3747 | 			allocated = ext4_ext_get_actual_len(&newex) - | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3748 | 				(map->m_lblk - le32_to_cpu(newex.ee_block)); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3749 | 			goto out; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3750 | 		} | 
 | 3751 | 	} | 
 | 3752 |  | 
 | 3753 | 	/* find extent for this block */ | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3754 | 	path = ext4_ext_find_extent(inode, map->m_lblk, NULL); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3755 | 	if (IS_ERR(path)) { | 
 | 3756 | 		err = PTR_ERR(path); | 
 | 3757 | 		path = NULL; | 
 | 3758 | 		goto out2; | 
 | 3759 | 	} | 
 | 3760 |  | 
 | 3761 | 	depth = ext_depth(inode); | 
 | 3762 |  | 
 | 3763 | 	/* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3764 | 	 * consistent leaf must not be empty; | 
 | 3765 | 	 * this situation is possible, though, _during_ tree modification; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3766 | 	 * this is why assert can't be put in ext4_ext_find_extent() | 
 | 3767 | 	 */ | 
| Frank Mayhar | 273df55 | 2010-03-02 11:46:09 -0500 | [diff] [blame] | 3768 | 	if (unlikely(path[depth].p_ext == NULL && depth != 0)) { | 
 | 3769 | 		EXT4_ERROR_INODE(inode, "bad extent address " | 
| Theodore Ts'o | f70f362 | 2010-05-16 23:00:00 -0400 | [diff] [blame] | 3770 | 				 "lblock: %lu, depth: %d pblock %lld", | 
 | 3771 | 				 (unsigned long) map->m_lblk, depth, | 
 | 3772 | 				 path[depth].p_block); | 
| Surbhi Palande | 034fb4c | 2009-12-14 09:53:52 -0500 | [diff] [blame] | 3773 | 		err = -EIO; | 
 | 3774 | 		goto out2; | 
 | 3775 | 	} | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3776 |  | 
| Avantika Mathur | 7e02897 | 2006-12-06 20:41:33 -0800 | [diff] [blame] | 3777 | 	ex = path[depth].p_ext; | 
 | 3778 | 	if (ex) { | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3779 | 		ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 3780 | 		ext4_fsblk_t ee_start = ext4_ext_pblock(ex); | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3781 | 		unsigned short ee_len; | 
| Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 3782 |  | 
 | 3783 | 		/* | 
| Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 3784 | 		 * Uninitialized extents are treated as holes, except that | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3785 | 		 * we split out initialized portions during a write. | 
| Suparna Bhattacharya | 471d401 | 2006-10-11 01:21:06 -0700 | [diff] [blame] | 3786 | 		 */ | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 3787 | 		ee_len = ext4_ext_get_actual_len(ex); | 
| Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3788 |  | 
 | 3789 | 		trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len); | 
 | 3790 |  | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3791 | 		/* if found extent covers block, simply return it */ | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3792 | 		if (in_range(map->m_lblk, ee_block, ee_len)) { | 
| Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 3793 | 			struct ext4_map_blocks punch_map; | 
| Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 3794 | 			ext4_fsblk_t partial_cluster = 0; | 
 | 3795 |  | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3796 | 			newblock = map->m_lblk - ee_block + ee_start; | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3797 | 			/* number of remaining blocks in the extent */ | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3798 | 			allocated = ee_len - (map->m_lblk - ee_block); | 
 | 3799 | 			ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk, | 
 | 3800 | 				  ee_block, ee_len, newblock); | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3801 |  | 
| Allison Henderson | e861304 | 2011-05-25 07:41:46 -0400 | [diff] [blame] | 3802 | 			if ((flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) == 0) { | 
 | 3803 | 				/* | 
 | 3804 | 				 * Do not put uninitialized extent | 
 | 3805 | 				 * in the cache | 
 | 3806 | 				 */ | 
 | 3807 | 				if (!ext4_ext_is_uninitialized(ex)) { | 
 | 3808 | 					ext4_ext_put_in_cache(inode, ee_block, | 
 | 3809 | 						ee_len, ee_start); | 
 | 3810 | 					goto out; | 
 | 3811 | 				} | 
 | 3812 | 				ret = ext4_ext_handle_uninitialized_extents( | 
 | 3813 | 					handle, inode, map, path, flags, | 
 | 3814 | 					allocated, newblock); | 
 | 3815 | 				return ret; | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3816 | 			} | 
| Allison Henderson | e861304 | 2011-05-25 07:41:46 -0400 | [diff] [blame] | 3817 |  | 
 | 3818 | 			/* | 
 | 3819 | 			 * Punch out the map length, but only to the | 
 | 3820 | 			 * end of the extent | 
 | 3821 | 			 */ | 
 | 3822 | 			punched_out = allocated < map->m_len ? | 
 | 3823 | 				allocated : map->m_len; | 
 | 3824 |  | 
 | 3825 | 			/* | 
 | 3826 | 			 * Sense extents need to be converted to | 
 | 3827 | 			 * uninitialized, they must fit in an | 
 | 3828 | 			 * uninitialized extent | 
 | 3829 | 			 */ | 
 | 3830 | 			if (punched_out > EXT_UNINIT_MAX_LEN) | 
 | 3831 | 				punched_out = EXT_UNINIT_MAX_LEN; | 
 | 3832 |  | 
 | 3833 | 			punch_map.m_lblk = map->m_lblk; | 
 | 3834 | 			punch_map.m_pblk = newblock; | 
 | 3835 | 			punch_map.m_len = punched_out; | 
 | 3836 | 			punch_map.m_flags = 0; | 
 | 3837 |  | 
 | 3838 | 			/* Check to see if the extent needs to be split */ | 
 | 3839 | 			if (punch_map.m_len != ee_len || | 
 | 3840 | 				punch_map.m_lblk != ee_block) { | 
 | 3841 |  | 
 | 3842 | 				ret = ext4_split_extent(handle, inode, | 
 | 3843 | 				path, &punch_map, 0, | 
 | 3844 | 				EXT4_GET_BLOCKS_PUNCH_OUT_EXT | | 
 | 3845 | 				EXT4_GET_BLOCKS_PRE_IO); | 
 | 3846 |  | 
 | 3847 | 				if (ret < 0) { | 
 | 3848 | 					err = ret; | 
 | 3849 | 					goto out2; | 
 | 3850 | 				} | 
 | 3851 | 				/* | 
 | 3852 | 				 * find extent for the block at | 
 | 3853 | 				 * the start of the hole | 
 | 3854 | 				 */ | 
 | 3855 | 				ext4_ext_drop_refs(path); | 
 | 3856 | 				kfree(path); | 
 | 3857 |  | 
 | 3858 | 				path = ext4_ext_find_extent(inode, | 
 | 3859 | 				map->m_lblk, NULL); | 
 | 3860 | 				if (IS_ERR(path)) { | 
 | 3861 | 					err = PTR_ERR(path); | 
 | 3862 | 					path = NULL; | 
 | 3863 | 					goto out2; | 
 | 3864 | 				} | 
 | 3865 |  | 
 | 3866 | 				depth = ext_depth(inode); | 
 | 3867 | 				ex = path[depth].p_ext; | 
 | 3868 | 				ee_len = ext4_ext_get_actual_len(ex); | 
 | 3869 | 				ee_block = le32_to_cpu(ex->ee_block); | 
 | 3870 | 				ee_start = ext4_ext_pblock(ex); | 
 | 3871 |  | 
 | 3872 | 			} | 
 | 3873 |  | 
 | 3874 | 			ext4_ext_mark_uninitialized(ex); | 
 | 3875 |  | 
| Allison Henderson | f7d0d37 | 2011-07-17 23:17:02 -0400 | [diff] [blame] | 3876 | 			ext4_ext_invalidate_cache(inode); | 
 | 3877 |  | 
 | 3878 | 			err = ext4_ext_rm_leaf(handle, inode, path, | 
| Theodore Ts'o | 0aa0600 | 2011-09-09 18:54:51 -0400 | [diff] [blame] | 3879 | 					       &partial_cluster, map->m_lblk, | 
 | 3880 | 					       map->m_lblk + punched_out); | 
| Allison Henderson | f7d0d37 | 2011-07-17 23:17:02 -0400 | [diff] [blame] | 3881 |  | 
 | 3882 | 			if (!err && path->p_hdr->eh_entries == 0) { | 
 | 3883 | 				/* | 
 | 3884 | 				 * Punch hole freed all of this sub tree, | 
 | 3885 | 				 * so we need to correct eh_depth | 
 | 3886 | 				 */ | 
 | 3887 | 				err = ext4_ext_get_access(handle, inode, path); | 
 | 3888 | 				if (err == 0) { | 
 | 3889 | 					ext_inode_hdr(inode)->eh_depth = 0; | 
 | 3890 | 					ext_inode_hdr(inode)->eh_max = | 
 | 3891 | 					cpu_to_le16(ext4_ext_space_root( | 
 | 3892 | 						inode, 0)); | 
 | 3893 |  | 
 | 3894 | 					err = ext4_ext_dirty( | 
 | 3895 | 						handle, inode, path); | 
 | 3896 | 				} | 
 | 3897 | 			} | 
| Allison Henderson | e861304 | 2011-05-25 07:41:46 -0400 | [diff] [blame] | 3898 |  | 
 | 3899 | 			goto out2; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3900 | 		} | 
 | 3901 | 	} | 
 | 3902 |  | 
| Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3903 | 	if ((sbi->s_cluster_ratio > 1) && | 
 | 3904 | 	    ext4_find_delalloc_cluster(inode, map->m_lblk, 0)) | 
 | 3905 | 		map->m_flags |= EXT4_MAP_FROM_CLUSTER; | 
 | 3906 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3907 | 	/* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 3908 | 	 * requested block isn't allocated yet; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3909 | 	 * we couldn't try to create block if create flag is zero | 
 | 3910 | 	 */ | 
| Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 3911 | 	if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 3912 | 		/* | 
 | 3913 | 		 * put just found gap into cache to speed up | 
 | 3914 | 		 * subsequent requests | 
 | 3915 | 		 */ | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3916 | 		ext4_ext_put_gap_in_cache(inode, path, map->m_lblk); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3917 | 		goto out2; | 
 | 3918 | 	} | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3919 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3920 | 	/* | 
| Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 3921 | 	 * Okay, we need to do block allocation. | 
| Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 3922 | 	 */ | 
| Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3923 | 	map->m_flags &= ~EXT4_MAP_FROM_CLUSTER; | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3924 | 	newex.ee_block = cpu_to_le32(map->m_lblk); | 
 | 3925 | 	cluster_offset = map->m_lblk & (sbi->s_cluster_ratio-1); | 
 | 3926 |  | 
 | 3927 | 	/* | 
 | 3928 | 	 * If we are doing bigalloc, check to see if the extent returned | 
 | 3929 | 	 * by ext4_ext_find_extent() implies a cluster we can use. | 
 | 3930 | 	 */ | 
 | 3931 | 	if (cluster_offset && ex && | 
| Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3932 | 	    get_implied_cluster_alloc(inode->i_sb, map, ex, path)) { | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3933 | 		ar.len = allocated = map->m_len; | 
 | 3934 | 		newblock = map->m_pblk; | 
| Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3935 | 		map->m_flags |= EXT4_MAP_FROM_CLUSTER; | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3936 | 		goto got_allocated_blocks; | 
 | 3937 | 	} | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 3938 |  | 
| Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3939 | 	/* find neighbour allocated blocks */ | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3940 | 	ar.lleft = map->m_lblk; | 
| Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3941 | 	err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft); | 
 | 3942 | 	if (err) | 
 | 3943 | 		goto out2; | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3944 | 	ar.lright = map->m_lblk; | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3945 | 	ex2 = NULL; | 
 | 3946 | 	err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2); | 
| Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3947 | 	if (err) | 
 | 3948 | 		goto out2; | 
| Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 3949 |  | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3950 | 	/* Check if the extent after searching to the right implies a | 
 | 3951 | 	 * cluster we can use. */ | 
 | 3952 | 	if ((sbi->s_cluster_ratio > 1) && ex2 && | 
| Aditya Kali | d899024 | 2011-09-09 19:18:51 -0400 | [diff] [blame] | 3953 | 	    get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) { | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3954 | 		ar.len = allocated = map->m_len; | 
 | 3955 | 		newblock = map->m_pblk; | 
| Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 3956 | 		map->m_flags |= EXT4_MAP_FROM_CLUSTER; | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3957 | 		goto got_allocated_blocks; | 
 | 3958 | 	} | 
 | 3959 |  | 
| Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 3960 | 	/* | 
 | 3961 | 	 * See if request is beyond maximum number of blocks we can have in | 
 | 3962 | 	 * a single extent. For an initialized extent this limit is | 
 | 3963 | 	 * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is | 
 | 3964 | 	 * EXT_UNINIT_MAX_LEN. | 
 | 3965 | 	 */ | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3966 | 	if (map->m_len > EXT_INIT_MAX_LEN && | 
| Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 3967 | 	    !(flags & EXT4_GET_BLOCKS_UNINIT_EXT)) | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3968 | 		map->m_len = EXT_INIT_MAX_LEN; | 
 | 3969 | 	else if (map->m_len > EXT_UNINIT_MAX_LEN && | 
| Theodore Ts'o | c217705 | 2009-05-14 00:58:52 -0400 | [diff] [blame] | 3970 | 		 (flags & EXT4_GET_BLOCKS_UNINIT_EXT)) | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3971 | 		map->m_len = EXT_UNINIT_MAX_LEN; | 
| Amit Arora | 749269f | 2007-07-18 09:02:56 -0400 | [diff] [blame] | 3972 |  | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3973 | 	/* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */ | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3974 | 	newex.ee_len = cpu_to_le16(map->m_len); | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3975 | 	err = ext4_ext_check_overlap(sbi, inode, &newex, path); | 
| Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 3976 | 	if (err) | 
| Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 3977 | 		allocated = ext4_ext_get_actual_len(&newex); | 
| Amit Arora | 25d14f9 | 2007-05-24 13:04:13 -0400 | [diff] [blame] | 3978 | 	else | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3979 | 		allocated = map->m_len; | 
| Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3980 |  | 
 | 3981 | 	/* allocate new block */ | 
 | 3982 | 	ar.inode = inode; | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 3983 | 	ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk); | 
 | 3984 | 	ar.logical = map->m_lblk; | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 3985 | 	/* | 
 | 3986 | 	 * We calculate the offset from the beginning of the cluster | 
 | 3987 | 	 * for the logical block number, since when we allocate a | 
 | 3988 | 	 * physical cluster, the physical block should start at the | 
 | 3989 | 	 * same offset from the beginning of the cluster.  This is | 
 | 3990 | 	 * needed so that future calls to get_implied_cluster_alloc() | 
 | 3991 | 	 * work correctly. | 
 | 3992 | 	 */ | 
 | 3993 | 	offset = map->m_lblk & (sbi->s_cluster_ratio - 1); | 
 | 3994 | 	ar.len = EXT4_NUM_B2C(sbi, offset+allocated); | 
 | 3995 | 	ar.goal -= offset; | 
 | 3996 | 	ar.logical -= offset; | 
| Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 3997 | 	if (S_ISREG(inode->i_mode)) | 
 | 3998 | 		ar.flags = EXT4_MB_HINT_DATA; | 
 | 3999 | 	else | 
 | 4000 | 		/* disable in-core preallocation for non-regular files */ | 
 | 4001 | 		ar.flags = 0; | 
| Vivek Haldar | 556b27a | 2011-05-25 07:41:54 -0400 | [diff] [blame] | 4002 | 	if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE) | 
 | 4003 | 		ar.flags |= EXT4_MB_HINT_NOPREALLOC; | 
| Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4004 | 	newblock = ext4_mb_new_blocks(handle, &ar, &err); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4005 | 	if (!newblock) | 
 | 4006 | 		goto out2; | 
| Mingming | 84fe3be | 2009-09-01 08:44:37 -0400 | [diff] [blame] | 4007 | 	ext_debug("allocate new block: goal %llu, found %llu/%u\n", | 
| Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 4008 | 		  ar.goal, newblock, allocated); | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4009 | 	free_on_err = 1; | 
| Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4010 | 	allocated_clusters = ar.len; | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4011 | 	ar.len = EXT4_C2B(sbi, ar.len) - offset; | 
 | 4012 | 	if (ar.len > allocated) | 
 | 4013 | 		ar.len = allocated; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4014 |  | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4015 | got_allocated_blocks: | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4016 | 	/* try to insert new extent into found leaf and return */ | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4017 | 	ext4_ext_store_pblock(&newex, newblock + offset); | 
| Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4018 | 	newex.ee_len = cpu_to_le16(ar.len); | 
| Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 4019 | 	/* Mark uninitialized */ | 
 | 4020 | 	if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){ | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4021 | 		ext4_ext_mark_uninitialized(&newex); | 
| Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 4022 | 		/* | 
| Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 4023 | 		 * io_end structure was created for every IO write to an | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4024 | 		 * uninitialized extent. To avoid unnecessary conversion, | 
| Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 4025 | 		 * here we flag the IO that really needs the conversion. | 
| Mingming | 5f52495 | 2009-11-10 10:48:04 -0500 | [diff] [blame] | 4026 | 		 * For non asycn direct IO case, flag the inode state | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4027 | 		 * that we need to perform conversion when IO is done. | 
| Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 4028 | 		 */ | 
| Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 4029 | 		if ((flags & EXT4_GET_BLOCKS_PRE_IO)) { | 
| Tao Ma | 0edeb71 | 2011-10-31 17:30:44 -0400 | [diff] [blame] | 4030 | 			if (io) | 
 | 4031 | 				ext4_set_io_unwritten_flag(inode, io); | 
 | 4032 | 			else | 
| Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 4033 | 				ext4_set_inode_state(inode, | 
 | 4034 | 						     EXT4_STATE_DIO_UNWRITTEN); | 
| Mingming | 5f52495 | 2009-11-10 10:48:04 -0500 | [diff] [blame] | 4035 | 		} | 
| Jiaying Zhang | 744692d | 2010-03-04 16:14:02 -0500 | [diff] [blame] | 4036 | 		if (ext4_should_dioread_nolock(inode)) | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4037 | 			map->m_flags |= EXT4_MAP_UNINIT; | 
| Mingming Cao | 8d5d02e | 2009-09-28 15:48:29 -0400 | [diff] [blame] | 4038 | 	} | 
| Jiaying Zhang | c8d46e4 | 2010-02-24 09:52:53 -0500 | [diff] [blame] | 4039 |  | 
| Dmitry Monakhov | a4e5d88 | 2011-10-25 08:15:12 -0400 | [diff] [blame] | 4040 | 	err = 0; | 
 | 4041 | 	if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) | 
 | 4042 | 		err = check_eofblocks_fl(handle, inode, map->m_lblk, | 
 | 4043 | 					 path, ar.len); | 
| Jiaying Zhang | 575a1d4 | 2011-07-10 20:07:25 -0400 | [diff] [blame] | 4044 | 	if (!err) | 
 | 4045 | 		err = ext4_ext_insert_extent(handle, inode, path, | 
 | 4046 | 					     &newex, flags); | 
| Theodore Ts'o | 4d33b1e | 2011-09-09 18:52:51 -0400 | [diff] [blame] | 4047 | 	if (err && free_on_err) { | 
| Maxim Patlasov | 7132de7 | 2011-07-10 19:37:48 -0400 | [diff] [blame] | 4048 | 		int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ? | 
 | 4049 | 			EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0; | 
| Alex Tomas | 315054f | 2007-05-24 13:04:25 -0400 | [diff] [blame] | 4050 | 		/* free data blocks we just allocated */ | 
| Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4051 | 		/* not a good idea to call discard here directly, | 
 | 4052 | 		 * but otherwise we'd need to call it every free() */ | 
| Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 4053 | 		ext4_discard_preallocations(inode); | 
| Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 4054 | 		ext4_free_blocks(handle, inode, NULL, ext4_ext_pblock(&newex), | 
| Maxim Patlasov | 7132de7 | 2011-07-10 19:37:48 -0400 | [diff] [blame] | 4055 | 				 ext4_ext_get_actual_len(&newex), fb_flags); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4056 | 		goto out2; | 
| Alex Tomas | 315054f | 2007-05-24 13:04:25 -0400 | [diff] [blame] | 4057 | 	} | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4058 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4059 | 	/* previous routine could use block we allocated */ | 
| Theodore Ts'o | bf89d16 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 4060 | 	newblock = ext4_ext_pblock(&newex); | 
| Aneesh Kumar K.V | b939e37 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4061 | 	allocated = ext4_ext_get_actual_len(&newex); | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4062 | 	if (allocated > map->m_len) | 
 | 4063 | 		allocated = map->m_len; | 
 | 4064 | 	map->m_flags |= EXT4_MAP_NEW; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4065 |  | 
| Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4066 | 	/* | 
| Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 4067 | 	 * Update reserved blocks/metadata blocks after successful | 
 | 4068 | 	 * block allocation which had been deferred till now. | 
 | 4069 | 	 */ | 
| Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4070 | 	if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) { | 
| Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 4071 | 		unsigned int reserved_clusters; | 
| Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4072 | 		/* | 
| Yongqiang Yang | 81fdbb4 | 2011-10-29 09:23:38 -0400 | [diff] [blame] | 4073 | 		 * Check how many clusters we had reserved this allocated range | 
| Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4074 | 		 */ | 
 | 4075 | 		reserved_clusters = get_reserved_cluster_alloc(inode, | 
 | 4076 | 						map->m_lblk, allocated); | 
 | 4077 | 		if (map->m_flags & EXT4_MAP_FROM_CLUSTER) { | 
 | 4078 | 			if (reserved_clusters) { | 
 | 4079 | 				/* | 
 | 4080 | 				 * We have clusters reserved for this range. | 
 | 4081 | 				 * But since we are not doing actual allocation | 
 | 4082 | 				 * and are simply using blocks from previously | 
 | 4083 | 				 * allocated cluster, we should release the | 
 | 4084 | 				 * reservation and not claim quota. | 
 | 4085 | 				 */ | 
 | 4086 | 				ext4_da_update_reserve_space(inode, | 
 | 4087 | 						reserved_clusters, 0); | 
 | 4088 | 			} | 
 | 4089 | 		} else { | 
 | 4090 | 			BUG_ON(allocated_clusters < reserved_clusters); | 
 | 4091 | 			/* We will claim quota for all newly allocated blocks.*/ | 
 | 4092 | 			ext4_da_update_reserve_space(inode, allocated_clusters, | 
 | 4093 | 							1); | 
 | 4094 | 			if (reserved_clusters < allocated_clusters) { | 
| Aditya Kali | 5356f26 | 2011-09-09 19:20:51 -0400 | [diff] [blame] | 4095 | 				struct ext4_inode_info *ei = EXT4_I(inode); | 
| Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4096 | 				int reservation = allocated_clusters - | 
 | 4097 | 						  reserved_clusters; | 
 | 4098 | 				/* | 
 | 4099 | 				 * It seems we claimed few clusters outside of | 
 | 4100 | 				 * the range of this allocation. We should give | 
 | 4101 | 				 * it back to the reservation pool. This can | 
 | 4102 | 				 * happen in the following case: | 
 | 4103 | 				 * | 
 | 4104 | 				 * * Suppose s_cluster_ratio is 4 (i.e., each | 
 | 4105 | 				 *   cluster has 4 blocks. Thus, the clusters | 
 | 4106 | 				 *   are [0-3],[4-7],[8-11]... | 
 | 4107 | 				 * * First comes delayed allocation write for | 
 | 4108 | 				 *   logical blocks 10 & 11. Since there were no | 
 | 4109 | 				 *   previous delayed allocated blocks in the | 
 | 4110 | 				 *   range [8-11], we would reserve 1 cluster | 
 | 4111 | 				 *   for this write. | 
 | 4112 | 				 * * Next comes write for logical blocks 3 to 8. | 
 | 4113 | 				 *   In this case, we will reserve 2 clusters | 
 | 4114 | 				 *   (for [0-3] and [4-7]; and not for [8-11] as | 
 | 4115 | 				 *   that range has a delayed allocated blocks. | 
 | 4116 | 				 *   Thus total reserved clusters now becomes 3. | 
 | 4117 | 				 * * Now, during the delayed allocation writeout | 
 | 4118 | 				 *   time, we will first write blocks [3-8] and | 
 | 4119 | 				 *   allocate 3 clusters for writing these | 
 | 4120 | 				 *   blocks. Also, we would claim all these | 
 | 4121 | 				 *   three clusters above. | 
 | 4122 | 				 * * Now when we come here to writeout the | 
 | 4123 | 				 *   blocks [10-11], we would expect to claim | 
 | 4124 | 				 *   the reservation of 1 cluster we had made | 
 | 4125 | 				 *   (and we would claim it since there are no | 
 | 4126 | 				 *   more delayed allocated blocks in the range | 
 | 4127 | 				 *   [8-11]. But our reserved cluster count had | 
 | 4128 | 				 *   already gone to 0. | 
 | 4129 | 				 * | 
 | 4130 | 				 *   Thus, at the step 4 above when we determine | 
 | 4131 | 				 *   that there are still some unwritten delayed | 
 | 4132 | 				 *   allocated blocks outside of our current | 
 | 4133 | 				 *   block range, we should increment the | 
 | 4134 | 				 *   reserved clusters count so that when the | 
 | 4135 | 				 *   remaining blocks finally gets written, we | 
 | 4136 | 				 *   could claim them. | 
 | 4137 | 				 */ | 
| Aditya Kali | 5356f26 | 2011-09-09 19:20:51 -0400 | [diff] [blame] | 4138 | 				dquot_reserve_block(inode, | 
 | 4139 | 						EXT4_C2B(sbi, reservation)); | 
 | 4140 | 				spin_lock(&ei->i_block_reservation_lock); | 
 | 4141 | 				ei->i_reserved_data_blocks += reservation; | 
 | 4142 | 				spin_unlock(&ei->i_block_reservation_lock); | 
| Aditya Kali | 7b415bf | 2011-09-09 19:04:51 -0400 | [diff] [blame] | 4143 | 			} | 
 | 4144 | 		} | 
 | 4145 | 	} | 
| Aneesh Kumar K.V | 5f634d0 | 2010-01-25 04:00:31 -0500 | [diff] [blame] | 4146 |  | 
 | 4147 | 	/* | 
| Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4148 | 	 * Cache the extent and update transaction to commit on fdatasync only | 
 | 4149 | 	 * when it is _not_ an uninitialized extent. | 
 | 4150 | 	 */ | 
 | 4151 | 	if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0) { | 
| Theodore Ts'o | b05e6ae | 2011-01-10 12:13:26 -0500 | [diff] [blame] | 4152 | 		ext4_ext_put_in_cache(inode, map->m_lblk, allocated, newblock); | 
| Jan Kara | b436b9b | 2009-12-08 23:51:10 -0500 | [diff] [blame] | 4153 | 		ext4_update_inode_fsync_trans(handle, inode, 1); | 
 | 4154 | 	} else | 
 | 4155 | 		ext4_update_inode_fsync_trans(handle, inode, 0); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4156 | out: | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4157 | 	if (allocated > map->m_len) | 
 | 4158 | 		allocated = map->m_len; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4159 | 	ext4_ext_show_leaf(inode, path); | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4160 | 	map->m_flags |= EXT4_MAP_MAPPED; | 
 | 4161 | 	map->m_pblk = newblock; | 
 | 4162 | 	map->m_len = allocated; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4163 | out2: | 
 | 4164 | 	if (path) { | 
 | 4165 | 		ext4_ext_drop_refs(path); | 
 | 4166 | 		kfree(path); | 
 | 4167 | 	} | 
| Allison Henderson | e861304 | 2011-05-25 07:41:46 -0400 | [diff] [blame] | 4168 | 	result = (flags & EXT4_GET_BLOCKS_PUNCH_OUT_EXT) ? | 
 | 4169 | 			punched_out : allocated; | 
 | 4170 |  | 
| Yongqiang Yang | e7b319e | 2011-10-29 09:39:51 -0400 | [diff] [blame] | 4171 | 	trace_ext4_ext_map_blocks_exit(inode, map->m_lblk, | 
 | 4172 | 		newblock, map->m_len, err ? err : result); | 
 | 4173 |  | 
| Allison Henderson | e861304 | 2011-05-25 07:41:46 -0400 | [diff] [blame] | 4174 | 	return err ? err : result; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4175 | } | 
 | 4176 |  | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4177 | void ext4_ext_truncate(struct inode *inode) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4178 | { | 
 | 4179 | 	struct address_space *mapping = inode->i_mapping; | 
 | 4180 | 	struct super_block *sb = inode->i_sb; | 
| Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4181 | 	ext4_lblk_t last_block; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4182 | 	handle_t *handle; | 
| Allison Henderson | 189e868 | 2011-09-06 21:49:44 -0400 | [diff] [blame] | 4183 | 	loff_t page_len; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4184 | 	int err = 0; | 
 | 4185 |  | 
 | 4186 | 	/* | 
| Jiaying Zhang | 3889fd5 | 2011-01-10 12:47:05 -0500 | [diff] [blame] | 4187 | 	 * finish any pending end_io work so we won't run the risk of | 
 | 4188 | 	 * converting any truncated blocks to initialized later | 
 | 4189 | 	 */ | 
 | 4190 | 	ext4_flush_completed_IO(inode); | 
 | 4191 |  | 
 | 4192 | 	/* | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4193 | 	 * probably first extent we're gonna free will be last in block | 
 | 4194 | 	 */ | 
| Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 4195 | 	err = ext4_writepage_trans_blocks(inode); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4196 | 	handle = ext4_journal_start(inode, err); | 
| Jan Kara | cf108bc | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4197 | 	if (IS_ERR(handle)) | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4198 | 		return; | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4199 |  | 
| Allison Henderson | 189e868 | 2011-09-06 21:49:44 -0400 | [diff] [blame] | 4200 | 	if (inode->i_size % PAGE_CACHE_SIZE != 0) { | 
 | 4201 | 		page_len = PAGE_CACHE_SIZE - | 
 | 4202 | 			(inode->i_size & (PAGE_CACHE_SIZE - 1)); | 
 | 4203 |  | 
 | 4204 | 		err = ext4_discard_partial_page_buffers(handle, | 
 | 4205 | 			mapping, inode->i_size, page_len, 0); | 
 | 4206 |  | 
 | 4207 | 		if (err) | 
 | 4208 | 			goto out_stop; | 
 | 4209 | 	} | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4210 |  | 
| Jan Kara | 9ddfc3d | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4211 | 	if (ext4_orphan_add(handle, inode)) | 
 | 4212 | 		goto out_stop; | 
 | 4213 |  | 
| Aneesh Kumar K.V | 0e855ac | 2008-01-28 23:58:26 -0500 | [diff] [blame] | 4214 | 	down_write(&EXT4_I(inode)->i_data_sem); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4215 | 	ext4_ext_invalidate_cache(inode); | 
 | 4216 |  | 
| Theodore Ts'o | c2ea3fd | 2008-10-10 09:40:52 -0400 | [diff] [blame] | 4217 | 	ext4_discard_preallocations(inode); | 
| Alex Tomas | c9de560 | 2008-01-29 00:19:52 -0500 | [diff] [blame] | 4218 |  | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4219 | 	/* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4220 | 	 * TODO: optimization is possible here. | 
 | 4221 | 	 * Probably we need not scan at all, | 
 | 4222 | 	 * because page truncation is enough. | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4223 | 	 */ | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4224 |  | 
 | 4225 | 	/* we have to know where to truncate from in crash case */ | 
 | 4226 | 	EXT4_I(inode)->i_disksize = inode->i_size; | 
 | 4227 | 	ext4_mark_inode_dirty(handle, inode); | 
 | 4228 |  | 
 | 4229 | 	last_block = (inode->i_size + sb->s_blocksize - 1) | 
 | 4230 | 			>> EXT4_BLOCK_SIZE_BITS(sb); | 
| Allison Henderson | c6a0371 | 2011-07-17 23:21:03 -0400 | [diff] [blame] | 4231 | 	err = ext4_ext_remove_space(inode, last_block); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4232 |  | 
 | 4233 | 	/* In a multi-transaction truncate, we only make the final | 
| Amit Arora | 56055d3 | 2007-07-17 21:42:38 -0400 | [diff] [blame] | 4234 | 	 * transaction synchronous. | 
 | 4235 | 	 */ | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4236 | 	if (IS_SYNC(inode)) | 
| Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 4237 | 		ext4_handle_sync(handle); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4238 |  | 
| Jan Kara | 9ddfc3d | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 4239 | 	up_write(&EXT4_I(inode)->i_data_sem); | 
| Eric Gouriou | f6d2f6b | 2011-05-22 21:33:00 -0400 | [diff] [blame] | 4240 |  | 
 | 4241 | out_stop: | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4242 | 	/* | 
| Randy Dunlap | d0d856e | 2006-10-11 01:21:07 -0700 | [diff] [blame] | 4243 | 	 * If this was a simple ftruncate() and the file will remain alive, | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4244 | 	 * then we need to clear up the orphan record which we created above. | 
 | 4245 | 	 * However, if this was a real unlink then we were called by | 
 | 4246 | 	 * ext4_delete_inode(), and we allow that function to clean up the | 
 | 4247 | 	 * orphan info for us. | 
 | 4248 | 	 */ | 
 | 4249 | 	if (inode->i_nlink) | 
 | 4250 | 		ext4_orphan_del(handle, inode); | 
 | 4251 |  | 
| Solofo Ramangalahy | ef73772 | 2008-04-29 22:00:41 -0400 | [diff] [blame] | 4252 | 	inode->i_mtime = inode->i_ctime = ext4_current_time(inode); | 
 | 4253 | 	ext4_mark_inode_dirty(handle, inode); | 
| Alex Tomas | a86c618 | 2006-10-11 01:21:03 -0700 | [diff] [blame] | 4254 | 	ext4_journal_stop(handle); | 
 | 4255 | } | 
 | 4256 |  | 
| Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4257 | static void ext4_falloc_update_inode(struct inode *inode, | 
 | 4258 | 				int mode, loff_t new_size, int update_ctime) | 
 | 4259 | { | 
 | 4260 | 	struct timespec now; | 
 | 4261 |  | 
 | 4262 | 	if (update_ctime) { | 
 | 4263 | 		now = current_fs_time(inode->i_sb); | 
 | 4264 | 		if (!timespec_equal(&inode->i_ctime, &now)) | 
 | 4265 | 			inode->i_ctime = now; | 
 | 4266 | 	} | 
 | 4267 | 	/* | 
 | 4268 | 	 * Update only when preallocation was requested beyond | 
 | 4269 | 	 * the file size. | 
 | 4270 | 	 */ | 
| Aneesh Kumar K.V | cf17fea | 2008-09-13 13:06:18 -0400 | [diff] [blame] | 4271 | 	if (!(mode & FALLOC_FL_KEEP_SIZE)) { | 
 | 4272 | 		if (new_size > i_size_read(inode)) | 
 | 4273 | 			i_size_write(inode, new_size); | 
 | 4274 | 		if (new_size > EXT4_I(inode)->i_disksize) | 
 | 4275 | 			ext4_update_i_disksize(inode, new_size); | 
| Jiaying Zhang | c8d46e4 | 2010-02-24 09:52:53 -0500 | [diff] [blame] | 4276 | 	} else { | 
 | 4277 | 		/* | 
 | 4278 | 		 * Mark that we allocate beyond EOF so the subsequent truncate | 
 | 4279 | 		 * can proceed even if the new size is the same as i_size. | 
 | 4280 | 		 */ | 
 | 4281 | 		if (new_size > i_size_read(inode)) | 
| Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 4282 | 			ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS); | 
| Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4283 | 	} | 
 | 4284 |  | 
 | 4285 | } | 
 | 4286 |  | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4287 | /* | 
| Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 4288 |  * preallocate space for a file. This implements ext4's fallocate file | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4289 |  * operation, which gets called from sys_fallocate system call. | 
 | 4290 |  * For block-mapped files, posix_fallocate should fall back to the method | 
 | 4291 |  * of writing zeroes to the required new blocks (the same behavior which is | 
 | 4292 |  * expected for file systems which do not support fallocate() system call). | 
 | 4293 |  */ | 
| Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 4294 | long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len) | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4295 | { | 
| Christoph Hellwig | 2fe17c1 | 2011-01-14 13:07:43 +0100 | [diff] [blame] | 4296 | 	struct inode *inode = file->f_path.dentry->d_inode; | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4297 | 	handle_t *handle; | 
| Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4298 | 	loff_t new_size; | 
| Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 4299 | 	unsigned int max_blocks; | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4300 | 	int ret = 0; | 
 | 4301 | 	int ret2 = 0; | 
 | 4302 | 	int retries = 0; | 
| Dmitry Monakhov | a4e5d88 | 2011-10-25 08:15:12 -0400 | [diff] [blame] | 4303 | 	int flags; | 
| Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4304 | 	struct ext4_map_blocks map; | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4305 | 	unsigned int credits, blkbits = inode->i_blkbits; | 
 | 4306 |  | 
 | 4307 | 	/* | 
 | 4308 | 	 * currently supporting (pre)allocate mode for extent-based | 
 | 4309 | 	 * files _only_ | 
 | 4310 | 	 */ | 
| Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 4311 | 	if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4312 | 		return -EOPNOTSUPP; | 
 | 4313 |  | 
| Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 4314 | 	/* Return error if mode is not supported */ | 
 | 4315 | 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) | 
 | 4316 | 		return -EOPNOTSUPP; | 
 | 4317 |  | 
 | 4318 | 	if (mode & FALLOC_FL_PUNCH_HOLE) | 
 | 4319 | 		return ext4_punch_hole(file, offset, len); | 
 | 4320 |  | 
| Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 4321 | 	trace_ext4_fallocate_enter(inode, offset, len, mode); | 
| Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4322 | 	map.m_lblk = offset >> blkbits; | 
| Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4323 | 	/* | 
 | 4324 | 	 * We can't just convert len to max_blocks because | 
 | 4325 | 	 * If blocksize = 4096 offset = 3072 and len = 2048 | 
 | 4326 | 	 */ | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4327 | 	max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) | 
| Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4328 | 		- map.m_lblk; | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4329 | 	/* | 
| Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 4330 | 	 * credits to insert 1 extent into extent tree | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4331 | 	 */ | 
| Mingming Cao | f3bd1f3 | 2008-08-19 22:16:03 -0400 | [diff] [blame] | 4332 | 	credits = ext4_chunk_trans_blocks(inode, max_blocks); | 
| Aneesh Kumar K.V | 55bd725 | 2008-02-15 12:47:21 -0500 | [diff] [blame] | 4333 | 	mutex_lock(&inode->i_mutex); | 
| Nikanth Karthikesan | 6d19c42 | 2010-05-16 14:00:00 -0400 | [diff] [blame] | 4334 | 	ret = inode_newsize_ok(inode, (len + offset)); | 
 | 4335 | 	if (ret) { | 
 | 4336 | 		mutex_unlock(&inode->i_mutex); | 
| Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 4337 | 		trace_ext4_fallocate_exit(inode, offset, max_blocks, ret); | 
| Nikanth Karthikesan | 6d19c42 | 2010-05-16 14:00:00 -0400 | [diff] [blame] | 4338 | 		return ret; | 
 | 4339 | 	} | 
| Greg Harm | 3c6fe77 | 2011-10-31 18:41:47 -0400 | [diff] [blame] | 4340 | 	flags = EXT4_GET_BLOCKS_CREATE_UNINIT_EXT; | 
| Dmitry Monakhov | a4e5d88 | 2011-10-25 08:15:12 -0400 | [diff] [blame] | 4341 | 	if (mode & FALLOC_FL_KEEP_SIZE) | 
 | 4342 | 		flags |= EXT4_GET_BLOCKS_KEEP_SIZE; | 
| Greg Harm | 3c6fe77 | 2011-10-31 18:41:47 -0400 | [diff] [blame] | 4343 | 	/* | 
 | 4344 | 	 * Don't normalize the request if it can fit in one extent so | 
 | 4345 | 	 * that it doesn't get unnecessarily split into multiple | 
 | 4346 | 	 * extents. | 
 | 4347 | 	 */ | 
 | 4348 | 	if (len <= EXT_UNINIT_MAX_LEN << blkbits) | 
 | 4349 | 		flags |= EXT4_GET_BLOCKS_NO_NORMALIZE; | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4350 | retry: | 
 | 4351 | 	while (ret >= 0 && ret < max_blocks) { | 
| Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4352 | 		map.m_lblk = map.m_lblk + ret; | 
 | 4353 | 		map.m_len = max_blocks = max_blocks - ret; | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4354 | 		handle = ext4_journal_start(inode, credits); | 
 | 4355 | 		if (IS_ERR(handle)) { | 
 | 4356 | 			ret = PTR_ERR(handle); | 
 | 4357 | 			break; | 
 | 4358 | 		} | 
| Dmitry Monakhov | a4e5d88 | 2011-10-25 08:15:12 -0400 | [diff] [blame] | 4359 | 		ret = ext4_map_blocks(handle, inode, &map, flags); | 
| Aneesh Kumar K.V | 221879c | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 4360 | 		if (ret <= 0) { | 
| Aneesh Kumar K.V | 2c98615 | 2008-02-25 15:41:35 -0500 | [diff] [blame] | 4361 | #ifdef EXT4FS_DEBUG | 
 | 4362 | 			WARN_ON(ret <= 0); | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4363 | 			printk(KERN_ERR "%s: ext4_ext_map_blocks " | 
| Aneesh Kumar K.V | 2c98615 | 2008-02-25 15:41:35 -0500 | [diff] [blame] | 4364 | 				    "returned error inode#%lu, block=%u, " | 
| Thadeu Lima de Souza Cascardo | 9fd9784 | 2009-01-26 19:26:26 -0500 | [diff] [blame] | 4365 | 				    "max_blocks=%u", __func__, | 
| Kazuya Mio | a6371b6 | 2010-10-27 21:30:15 -0400 | [diff] [blame] | 4366 | 				    inode->i_ino, map.m_lblk, max_blocks); | 
| Aneesh Kumar K.V | 2c98615 | 2008-02-25 15:41:35 -0500 | [diff] [blame] | 4367 | #endif | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4368 | 			ext4_mark_inode_dirty(handle, inode); | 
 | 4369 | 			ret2 = ext4_journal_stop(handle); | 
 | 4370 | 			break; | 
 | 4371 | 		} | 
| Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4372 | 		if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len, | 
| Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4373 | 						blkbits) >> blkbits)) | 
 | 4374 | 			new_size = offset + len; | 
 | 4375 | 		else | 
| Utako Kusaka | 29ae07b | 2011-07-27 22:11:20 -0400 | [diff] [blame] | 4376 | 			new_size = ((loff_t) map.m_lblk + ret) << blkbits; | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4377 |  | 
| Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4378 | 		ext4_falloc_update_inode(inode, mode, new_size, | 
| Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4379 | 					 (map.m_flags & EXT4_MAP_NEW)); | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4380 | 		ext4_mark_inode_dirty(handle, inode); | 
 | 4381 | 		ret2 = ext4_journal_stop(handle); | 
 | 4382 | 		if (ret2) | 
 | 4383 | 			break; | 
 | 4384 | 	} | 
| Aneesh Kumar K.V | fd28784 | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 4385 | 	if (ret == -ENOSPC && | 
 | 4386 | 			ext4_should_retry_alloc(inode->i_sb, &retries)) { | 
 | 4387 | 		ret = 0; | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4388 | 		goto retry; | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4389 | 	} | 
| Aneesh Kumar K.V | 55bd725 | 2008-02-15 12:47:21 -0500 | [diff] [blame] | 4390 | 	mutex_unlock(&inode->i_mutex); | 
| Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 4391 | 	trace_ext4_fallocate_exit(inode, offset, max_blocks, | 
 | 4392 | 				ret > 0 ? ret2 : ret); | 
| Amit Arora | a2df2a6 | 2007-07-17 21:42:41 -0400 | [diff] [blame] | 4393 | 	return ret > 0 ? ret2 : ret; | 
 | 4394 | } | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4395 |  | 
 | 4396 | /* | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4397 |  * This function convert a range of blocks to written extents | 
 | 4398 |  * The caller of this function will pass the start offset and the size. | 
 | 4399 |  * all unwritten extents within this range will be converted to | 
 | 4400 |  * written extents. | 
 | 4401 |  * | 
 | 4402 |  * This function is called from the direct IO end io call back | 
 | 4403 |  * function, to convert the fallocated extents after IO is completed. | 
| Mingming | 109f556 | 2009-11-10 10:48:08 -0500 | [diff] [blame] | 4404 |  * Returns 0 on success. | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4405 |  */ | 
 | 4406 | int ext4_convert_unwritten_extents(struct inode *inode, loff_t offset, | 
| Eric Sandeen | a1de02d | 2010-02-04 23:58:38 -0500 | [diff] [blame] | 4407 | 				    ssize_t len) | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4408 | { | 
 | 4409 | 	handle_t *handle; | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4410 | 	unsigned int max_blocks; | 
 | 4411 | 	int ret = 0; | 
 | 4412 | 	int ret2 = 0; | 
| Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4413 | 	struct ext4_map_blocks map; | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4414 | 	unsigned int credits, blkbits = inode->i_blkbits; | 
 | 4415 |  | 
| Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4416 | 	map.m_lblk = offset >> blkbits; | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4417 | 	/* | 
 | 4418 | 	 * We can't just convert len to max_blocks because | 
 | 4419 | 	 * If blocksize = 4096 offset = 3072 and len = 2048 | 
 | 4420 | 	 */ | 
| Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4421 | 	max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) - | 
 | 4422 | 		      map.m_lblk); | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4423 | 	/* | 
 | 4424 | 	 * credits to insert 1 extent into extent tree | 
 | 4425 | 	 */ | 
 | 4426 | 	credits = ext4_chunk_trans_blocks(inode, max_blocks); | 
 | 4427 | 	while (ret >= 0 && ret < max_blocks) { | 
| Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4428 | 		map.m_lblk += ret; | 
 | 4429 | 		map.m_len = (max_blocks -= ret); | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4430 | 		handle = ext4_journal_start(inode, credits); | 
 | 4431 | 		if (IS_ERR(handle)) { | 
 | 4432 | 			ret = PTR_ERR(handle); | 
 | 4433 | 			break; | 
 | 4434 | 		} | 
| Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4435 | 		ret = ext4_map_blocks(handle, inode, &map, | 
| Jiaying Zhang | c7064ef | 2010-03-02 13:28:44 -0500 | [diff] [blame] | 4436 | 				      EXT4_GET_BLOCKS_IO_CONVERT_EXT); | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4437 | 		if (ret <= 0) { | 
 | 4438 | 			WARN_ON(ret <= 0); | 
| Theodore Ts'o | e35fd66 | 2010-05-16 19:00:00 -0400 | [diff] [blame] | 4439 | 			printk(KERN_ERR "%s: ext4_ext_map_blocks " | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4440 | 				    "returned error inode#%lu, block=%u, " | 
 | 4441 | 				    "max_blocks=%u", __func__, | 
| Theodore Ts'o | 2ed8868 | 2010-05-16 20:00:00 -0400 | [diff] [blame] | 4442 | 				    inode->i_ino, map.m_lblk, map.m_len); | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4443 | 		} | 
 | 4444 | 		ext4_mark_inode_dirty(handle, inode); | 
 | 4445 | 		ret2 = ext4_journal_stop(handle); | 
 | 4446 | 		if (ret <= 0 || ret2 ) | 
 | 4447 | 			break; | 
 | 4448 | 	} | 
 | 4449 | 	return ret > 0 ? ret2 : ret; | 
 | 4450 | } | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4451 |  | 
| Mingming Cao | 0031462 | 2009-09-28 15:49:08 -0400 | [diff] [blame] | 4452 | /* | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4453 |  * Callback function called for each extent to gather FIEMAP information. | 
 | 4454 |  */ | 
| Lukas Czerner | c03f8aa | 2011-06-06 00:06:52 -0400 | [diff] [blame] | 4455 | static int ext4_ext_fiemap_cb(struct inode *inode, ext4_lblk_t next, | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4456 | 		       struct ext4_ext_cache *newex, struct ext4_extent *ex, | 
 | 4457 | 		       void *data) | 
 | 4458 | { | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4459 | 	__u64	logical; | 
 | 4460 | 	__u64	physical; | 
 | 4461 | 	__u64	length; | 
 | 4462 | 	__u32	flags = 0; | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4463 | 	int		ret = 0; | 
 | 4464 | 	struct fiemap_extent_info *fieinfo = data; | 
 | 4465 | 	unsigned char blksize_bits; | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4466 |  | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4467 | 	blksize_bits = inode->i_sb->s_blocksize_bits; | 
 | 4468 | 	logical = (__u64)newex->ec_block << blksize_bits; | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4469 |  | 
| Theodore Ts'o | b05e6ae | 2011-01-10 12:13:26 -0500 | [diff] [blame] | 4470 | 	if (newex->ec_start == 0) { | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4471 | 		/* | 
 | 4472 | 		 * No extent in extent-tree contains block @newex->ec_start, | 
 | 4473 | 		 * then the block may stay in 1)a hole or 2)delayed-extent. | 
 | 4474 | 		 * | 
 | 4475 | 		 * Holes or delayed-extents are processed as follows. | 
 | 4476 | 		 * 1. lookup dirty pages with specified range in pagecache. | 
 | 4477 | 		 *    If no page is got, then there is no delayed-extent and | 
 | 4478 | 		 *    return with EXT_CONTINUE. | 
 | 4479 | 		 * 2. find the 1st mapped buffer, | 
 | 4480 | 		 * 3. check if the mapped buffer is both in the request range | 
 | 4481 | 		 *    and a delayed buffer. If not, there is no delayed-extent, | 
 | 4482 | 		 *    then return. | 
 | 4483 | 		 * 4. a delayed-extent is found, the extent will be collected. | 
 | 4484 | 		 */ | 
 | 4485 | 		ext4_lblk_t	end = 0; | 
 | 4486 | 		pgoff_t		last_offset; | 
 | 4487 | 		pgoff_t		offset; | 
 | 4488 | 		pgoff_t		index; | 
| Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4489 | 		pgoff_t		start_index = 0; | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4490 | 		struct page	**pages = NULL; | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4491 | 		struct buffer_head *bh = NULL; | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4492 | 		struct buffer_head *head = NULL; | 
 | 4493 | 		unsigned int nr_pages = PAGE_SIZE / sizeof(struct page *); | 
 | 4494 |  | 
 | 4495 | 		pages = kmalloc(PAGE_SIZE, GFP_KERNEL); | 
 | 4496 | 		if (pages == NULL) | 
 | 4497 | 			return -ENOMEM; | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4498 |  | 
 | 4499 | 		offset = logical >> PAGE_SHIFT; | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4500 | repeat: | 
 | 4501 | 		last_offset = offset; | 
 | 4502 | 		head = NULL; | 
 | 4503 | 		ret = find_get_pages_tag(inode->i_mapping, &offset, | 
 | 4504 | 					PAGECACHE_TAG_DIRTY, nr_pages, pages); | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4505 |  | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4506 | 		if (!(flags & FIEMAP_EXTENT_DELALLOC)) { | 
 | 4507 | 			/* First time, try to find a mapped buffer. */ | 
 | 4508 | 			if (ret == 0) { | 
 | 4509 | out: | 
 | 4510 | 				for (index = 0; index < ret; index++) | 
 | 4511 | 					page_cache_release(pages[index]); | 
 | 4512 | 				/* just a hole. */ | 
 | 4513 | 				kfree(pages); | 
 | 4514 | 				return EXT_CONTINUE; | 
 | 4515 | 			} | 
| Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4516 | 			index = 0; | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4517 |  | 
| Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4518 | next_page: | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4519 | 			/* Try to find the 1st mapped buffer. */ | 
| Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4520 | 			end = ((__u64)pages[index]->index << PAGE_SHIFT) >> | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4521 | 				  blksize_bits; | 
| Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4522 | 			if (!page_has_buffers(pages[index])) | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4523 | 				goto out; | 
| Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4524 | 			head = page_buffers(pages[index]); | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4525 | 			if (!head) | 
 | 4526 | 				goto out; | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4527 |  | 
| Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4528 | 			index++; | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4529 | 			bh = head; | 
 | 4530 | 			do { | 
| Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4531 | 				if (end >= newex->ec_block + | 
 | 4532 | 					newex->ec_len) | 
 | 4533 | 					/* The buffer is out of | 
 | 4534 | 					 * the request range. | 
 | 4535 | 					 */ | 
 | 4536 | 					goto out; | 
 | 4537 |  | 
 | 4538 | 				if (buffer_mapped(bh) && | 
 | 4539 | 				    end >= newex->ec_block) { | 
 | 4540 | 					start_index = index - 1; | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4541 | 					/* get the 1st mapped buffer. */ | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4542 | 					goto found_mapped_buffer; | 
 | 4543 | 				} | 
| Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4544 |  | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4545 | 				bh = bh->b_this_page; | 
 | 4546 | 				end++; | 
 | 4547 | 			} while (bh != head); | 
 | 4548 |  | 
| Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4549 | 			/* No mapped buffer in the range found in this page, | 
 | 4550 | 			 * We need to look up next page. | 
 | 4551 | 			 */ | 
 | 4552 | 			if (index >= ret) { | 
 | 4553 | 				/* There is no page left, but we need to limit | 
 | 4554 | 				 * newex->ec_len. | 
 | 4555 | 				 */ | 
 | 4556 | 				newex->ec_len = end - newex->ec_block; | 
 | 4557 | 				goto out; | 
 | 4558 | 			} | 
 | 4559 | 			goto next_page; | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4560 | 		} else { | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4561 | 			/*Find contiguous delayed buffers. */ | 
 | 4562 | 			if (ret > 0 && pages[0]->index == last_offset) | 
 | 4563 | 				head = page_buffers(pages[0]); | 
 | 4564 | 			bh = head; | 
| Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4565 | 			index = 1; | 
 | 4566 | 			start_index = 0; | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4567 | 		} | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4568 |  | 
 | 4569 | found_mapped_buffer: | 
 | 4570 | 		if (bh != NULL && buffer_delay(bh)) { | 
 | 4571 | 			/* 1st or contiguous delayed buffer found. */ | 
 | 4572 | 			if (!(flags & FIEMAP_EXTENT_DELALLOC)) { | 
 | 4573 | 				/* | 
 | 4574 | 				 * 1st delayed buffer found, record | 
 | 4575 | 				 * the start of extent. | 
 | 4576 | 				 */ | 
 | 4577 | 				flags |= FIEMAP_EXTENT_DELALLOC; | 
 | 4578 | 				newex->ec_block = end; | 
 | 4579 | 				logical = (__u64)end << blksize_bits; | 
 | 4580 | 			} | 
 | 4581 | 			/* Find contiguous delayed buffers. */ | 
 | 4582 | 			do { | 
 | 4583 | 				if (!buffer_delay(bh)) | 
 | 4584 | 					goto found_delayed_extent; | 
 | 4585 | 				bh = bh->b_this_page; | 
 | 4586 | 				end++; | 
 | 4587 | 			} while (bh != head); | 
 | 4588 |  | 
| Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4589 | 			for (; index < ret; index++) { | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4590 | 				if (!page_has_buffers(pages[index])) { | 
 | 4591 | 					bh = NULL; | 
 | 4592 | 					break; | 
 | 4593 | 				} | 
 | 4594 | 				head = page_buffers(pages[index]); | 
 | 4595 | 				if (!head) { | 
 | 4596 | 					bh = NULL; | 
 | 4597 | 					break; | 
 | 4598 | 				} | 
| Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4599 |  | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4600 | 				if (pages[index]->index != | 
| Yongqiang Yang | b221349 | 2011-05-24 11:36:58 -0400 | [diff] [blame] | 4601 | 				    pages[start_index]->index + index | 
 | 4602 | 				    - start_index) { | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4603 | 					/* Blocks are not contiguous. */ | 
 | 4604 | 					bh = NULL; | 
 | 4605 | 					break; | 
 | 4606 | 				} | 
 | 4607 | 				bh = head; | 
 | 4608 | 				do { | 
 | 4609 | 					if (!buffer_delay(bh)) | 
 | 4610 | 						/* Delayed-extent ends. */ | 
 | 4611 | 						goto found_delayed_extent; | 
 | 4612 | 					bh = bh->b_this_page; | 
 | 4613 | 					end++; | 
 | 4614 | 				} while (bh != head); | 
 | 4615 | 			} | 
 | 4616 | 		} else if (!(flags & FIEMAP_EXTENT_DELALLOC)) | 
 | 4617 | 			/* a hole found. */ | 
 | 4618 | 			goto out; | 
 | 4619 |  | 
 | 4620 | found_delayed_extent: | 
 | 4621 | 		newex->ec_len = min(end - newex->ec_block, | 
 | 4622 | 						(ext4_lblk_t)EXT_INIT_MAX_LEN); | 
 | 4623 | 		if (ret == nr_pages && bh != NULL && | 
 | 4624 | 			newex->ec_len < EXT_INIT_MAX_LEN && | 
 | 4625 | 			buffer_delay(bh)) { | 
 | 4626 | 			/* Have not collected an extent and continue. */ | 
 | 4627 | 			for (index = 0; index < ret; index++) | 
 | 4628 | 				page_cache_release(pages[index]); | 
 | 4629 | 			goto repeat; | 
 | 4630 | 		} | 
 | 4631 |  | 
 | 4632 | 		for (index = 0; index < ret; index++) | 
 | 4633 | 			page_cache_release(pages[index]); | 
 | 4634 | 		kfree(pages); | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4635 | 	} | 
 | 4636 |  | 
 | 4637 | 	physical = (__u64)newex->ec_start << blksize_bits; | 
 | 4638 | 	length =   (__u64)newex->ec_len << blksize_bits; | 
 | 4639 |  | 
 | 4640 | 	if (ex && ext4_ext_is_uninitialized(ex)) | 
 | 4641 | 		flags |= FIEMAP_EXTENT_UNWRITTEN; | 
 | 4642 |  | 
| Lukas Czerner | c03f8aa | 2011-06-06 00:06:52 -0400 | [diff] [blame] | 4643 | 	if (next == EXT_MAX_BLOCKS) | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4644 | 		flags |= FIEMAP_EXTENT_LAST; | 
 | 4645 |  | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4646 | 	ret = fiemap_fill_next_extent(fieinfo, logical, physical, | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4647 | 					length, flags); | 
| Yongqiang Yang | 6d9c85e | 2011-02-27 17:25:47 -0500 | [diff] [blame] | 4648 | 	if (ret < 0) | 
 | 4649 | 		return ret; | 
 | 4650 | 	if (ret == 1) | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4651 | 		return EXT_BREAK; | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4652 | 	return EXT_CONTINUE; | 
 | 4653 | } | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4654 | /* fiemap flags we can handle specified here */ | 
 | 4655 | #define EXT4_FIEMAP_FLAGS	(FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR) | 
 | 4656 |  | 
| Aneesh Kumar K.V | 3a06d77 | 2008-11-22 15:04:59 -0500 | [diff] [blame] | 4657 | static int ext4_xattr_fiemap(struct inode *inode, | 
 | 4658 | 				struct fiemap_extent_info *fieinfo) | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4659 | { | 
 | 4660 | 	__u64 physical = 0; | 
 | 4661 | 	__u64 length; | 
 | 4662 | 	__u32 flags = FIEMAP_EXTENT_LAST; | 
 | 4663 | 	int blockbits = inode->i_sb->s_blocksize_bits; | 
 | 4664 | 	int error = 0; | 
 | 4665 |  | 
 | 4666 | 	/* in-inode? */ | 
| Theodore Ts'o | 19f5fb7 | 2010-01-24 14:34:07 -0500 | [diff] [blame] | 4667 | 	if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4668 | 		struct ext4_iloc iloc; | 
 | 4669 | 		int offset;	/* offset of xattr in inode */ | 
 | 4670 |  | 
 | 4671 | 		error = ext4_get_inode_loc(inode, &iloc); | 
 | 4672 | 		if (error) | 
 | 4673 | 			return error; | 
 | 4674 | 		physical = iloc.bh->b_blocknr << blockbits; | 
 | 4675 | 		offset = EXT4_GOOD_OLD_INODE_SIZE + | 
 | 4676 | 				EXT4_I(inode)->i_extra_isize; | 
 | 4677 | 		physical += offset; | 
 | 4678 | 		length = EXT4_SB(inode->i_sb)->s_inode_size - offset; | 
 | 4679 | 		flags |= FIEMAP_EXTENT_DATA_INLINE; | 
| Curt Wohlgemuth | fd2dd9f | 2010-04-03 17:44:16 -0400 | [diff] [blame] | 4680 | 		brelse(iloc.bh); | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4681 | 	} else { /* external block */ | 
 | 4682 | 		physical = EXT4_I(inode)->i_file_acl << blockbits; | 
 | 4683 | 		length = inode->i_sb->s_blocksize; | 
 | 4684 | 	} | 
 | 4685 |  | 
 | 4686 | 	if (physical) | 
 | 4687 | 		error = fiemap_fill_next_extent(fieinfo, 0, physical, | 
 | 4688 | 						length, flags); | 
 | 4689 | 	return (error < 0 ? error : 0); | 
 | 4690 | } | 
 | 4691 |  | 
| Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 4692 | /* | 
 | 4693 |  * ext4_ext_punch_hole | 
 | 4694 |  * | 
 | 4695 |  * Punches a hole of "length" bytes in a file starting | 
 | 4696 |  * at byte "offset" | 
 | 4697 |  * | 
 | 4698 |  * @inode:  The inode of the file to punch a hole in | 
 | 4699 |  * @offset: The starting byte offset of the hole | 
 | 4700 |  * @length: The length of the hole | 
 | 4701 |  * | 
 | 4702 |  * Returns the number of blocks removed or negative on err | 
 | 4703 |  */ | 
 | 4704 | int ext4_ext_punch_hole(struct file *file, loff_t offset, loff_t length) | 
 | 4705 | { | 
 | 4706 | 	struct inode *inode = file->f_path.dentry->d_inode; | 
 | 4707 | 	struct super_block *sb = inode->i_sb; | 
 | 4708 | 	struct ext4_ext_cache cache_ex; | 
 | 4709 | 	ext4_lblk_t first_block, last_block, num_blocks, iblock, max_blocks; | 
 | 4710 | 	struct address_space *mapping = inode->i_mapping; | 
 | 4711 | 	struct ext4_map_blocks map; | 
 | 4712 | 	handle_t *handle; | 
| Allison Henderson | ba06208 | 2011-09-03 11:55:59 -0400 | [diff] [blame] | 4713 | 	loff_t first_page, last_page, page_len; | 
 | 4714 | 	loff_t first_page_offset, last_page_offset; | 
| Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 4715 | 	int ret, credits, blocks_released, err = 0; | 
 | 4716 |  | 
| Allison Henderson | 2be4751 | 2011-09-03 11:56:52 -0400 | [diff] [blame] | 4717 | 	/* No need to punch hole beyond i_size */ | 
 | 4718 | 	if (offset >= inode->i_size) | 
 | 4719 | 		return 0; | 
 | 4720 |  | 
 | 4721 | 	/* | 
 | 4722 | 	 * If the hole extends beyond i_size, set the hole | 
 | 4723 | 	 * to end after the page that contains i_size | 
 | 4724 | 	 */ | 
 | 4725 | 	if (offset + length > inode->i_size) { | 
 | 4726 | 		length = inode->i_size + | 
 | 4727 | 		   PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) - | 
 | 4728 | 		   offset; | 
 | 4729 | 	} | 
 | 4730 |  | 
| Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 4731 | 	first_block = (offset + sb->s_blocksize - 1) >> | 
 | 4732 | 		EXT4_BLOCK_SIZE_BITS(sb); | 
 | 4733 | 	last_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb); | 
 | 4734 |  | 
| Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 4735 | 	first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; | 
 | 4736 | 	last_page = (offset + length) >> PAGE_CACHE_SHIFT; | 
 | 4737 |  | 
 | 4738 | 	first_page_offset = first_page << PAGE_CACHE_SHIFT; | 
 | 4739 | 	last_page_offset = last_page << PAGE_CACHE_SHIFT; | 
 | 4740 |  | 
 | 4741 | 	/* | 
 | 4742 | 	 * Write out all dirty pages to avoid race conditions | 
 | 4743 | 	 * Then release them. | 
 | 4744 | 	 */ | 
 | 4745 | 	if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { | 
 | 4746 | 		err = filemap_write_and_wait_range(mapping, | 
| Allison Henderson | 2be4751 | 2011-09-03 11:56:52 -0400 | [diff] [blame] | 4747 | 			offset, offset + length - 1); | 
| Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 4748 |  | 
| Allison Henderson | 2be4751 | 2011-09-03 11:56:52 -0400 | [diff] [blame] | 4749 | 		if (err) | 
 | 4750 | 			return err; | 
| Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 4751 | 	} | 
 | 4752 |  | 
 | 4753 | 	/* Now release the pages */ | 
 | 4754 | 	if (last_page_offset > first_page_offset) { | 
 | 4755 | 		truncate_inode_pages_range(mapping, first_page_offset, | 
 | 4756 | 					   last_page_offset-1); | 
 | 4757 | 	} | 
 | 4758 |  | 
 | 4759 | 	/* finish any pending end_io work */ | 
 | 4760 | 	ext4_flush_completed_IO(inode); | 
 | 4761 |  | 
 | 4762 | 	credits = ext4_writepage_trans_blocks(inode); | 
 | 4763 | 	handle = ext4_journal_start(inode, credits); | 
 | 4764 | 	if (IS_ERR(handle)) | 
 | 4765 | 		return PTR_ERR(handle); | 
 | 4766 |  | 
 | 4767 | 	err = ext4_orphan_add(handle, inode); | 
 | 4768 | 	if (err) | 
 | 4769 | 		goto out; | 
 | 4770 |  | 
 | 4771 | 	/* | 
| Allison Henderson | ba06208 | 2011-09-03 11:55:59 -0400 | [diff] [blame] | 4772 | 	 * Now we need to zero out the non-page-aligned data in the | 
 | 4773 | 	 * pages at the start and tail of the hole, and unmap the buffer | 
 | 4774 | 	 * heads for the block aligned regions of the page that were | 
 | 4775 | 	 * completely zeroed. | 
| Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 4776 | 	 */ | 
| Allison Henderson | ba06208 | 2011-09-03 11:55:59 -0400 | [diff] [blame] | 4777 | 	if (first_page > last_page) { | 
 | 4778 | 		/* | 
 | 4779 | 		 * If the file space being truncated is contained within a page | 
 | 4780 | 		 * just zero out and unmap the middle of that page | 
 | 4781 | 		 */ | 
 | 4782 | 		err = ext4_discard_partial_page_buffers(handle, | 
 | 4783 | 			mapping, offset, length, 0); | 
| Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 4784 |  | 
| Allison Henderson | ba06208 | 2011-09-03 11:55:59 -0400 | [diff] [blame] | 4785 | 		if (err) | 
 | 4786 | 			goto out; | 
 | 4787 | 	} else { | 
 | 4788 | 		/* | 
 | 4789 | 		 * zero out and unmap the partial page that contains | 
 | 4790 | 		 * the start of the hole | 
 | 4791 | 		 */ | 
 | 4792 | 		page_len  = first_page_offset - offset; | 
 | 4793 | 		if (page_len > 0) { | 
 | 4794 | 			err = ext4_discard_partial_page_buffers(handle, mapping, | 
 | 4795 | 						   offset, page_len, 0); | 
 | 4796 | 			if (err) | 
 | 4797 | 				goto out; | 
 | 4798 | 		} | 
 | 4799 |  | 
 | 4800 | 		/* | 
 | 4801 | 		 * zero out and unmap the partial page that contains | 
 | 4802 | 		 * the end of the hole | 
 | 4803 | 		 */ | 
 | 4804 | 		page_len = offset + length - last_page_offset; | 
 | 4805 | 		if (page_len > 0) { | 
 | 4806 | 			err = ext4_discard_partial_page_buffers(handle, mapping, | 
 | 4807 | 					last_page_offset, page_len, 0); | 
 | 4808 | 			if (err) | 
 | 4809 | 				goto out; | 
| Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 4810 | 		} | 
 | 4811 | 	} | 
 | 4812 |  | 
| Allison Henderson | 2be4751 | 2011-09-03 11:56:52 -0400 | [diff] [blame] | 4813 |  | 
 | 4814 | 	/* | 
 | 4815 | 	 * If i_size is contained in the last page, we need to | 
 | 4816 | 	 * unmap and zero the partial page after i_size | 
 | 4817 | 	 */ | 
 | 4818 | 	if (inode->i_size >> PAGE_CACHE_SHIFT == last_page && | 
 | 4819 | 	   inode->i_size % PAGE_CACHE_SIZE != 0) { | 
 | 4820 |  | 
 | 4821 | 		page_len = PAGE_CACHE_SIZE - | 
 | 4822 | 			(inode->i_size & (PAGE_CACHE_SIZE - 1)); | 
 | 4823 |  | 
 | 4824 | 		if (page_len > 0) { | 
 | 4825 | 			err = ext4_discard_partial_page_buffers(handle, | 
 | 4826 | 			  mapping, inode->i_size, page_len, 0); | 
 | 4827 |  | 
 | 4828 | 			if (err) | 
 | 4829 | 				goto out; | 
 | 4830 | 		} | 
 | 4831 | 	} | 
 | 4832 |  | 
| Allison Henderson | a4bb6b6 | 2011-05-25 07:41:50 -0400 | [diff] [blame] | 4833 | 	/* If there are no blocks to remove, return now */ | 
 | 4834 | 	if (first_block >= last_block) | 
 | 4835 | 		goto out; | 
 | 4836 |  | 
 | 4837 | 	down_write(&EXT4_I(inode)->i_data_sem); | 
 | 4838 | 	ext4_ext_invalidate_cache(inode); | 
 | 4839 | 	ext4_discard_preallocations(inode); | 
 | 4840 |  | 
 | 4841 | 	/* | 
 | 4842 | 	 * Loop over all the blocks and identify blocks | 
 | 4843 | 	 * that need to be punched out | 
 | 4844 | 	 */ | 
 | 4845 | 	iblock = first_block; | 
 | 4846 | 	blocks_released = 0; | 
 | 4847 | 	while (iblock < last_block) { | 
 | 4848 | 		max_blocks = last_block - iblock; | 
 | 4849 | 		num_blocks = 1; | 
 | 4850 | 		memset(&map, 0, sizeof(map)); | 
 | 4851 | 		map.m_lblk = iblock; | 
 | 4852 | 		map.m_len = max_blocks; | 
 | 4853 | 		ret = ext4_ext_map_blocks(handle, inode, &map, | 
 | 4854 | 			EXT4_GET_BLOCKS_PUNCH_OUT_EXT); | 
 | 4855 |  | 
 | 4856 | 		if (ret > 0) { | 
 | 4857 | 			blocks_released += ret; | 
 | 4858 | 			num_blocks = ret; | 
 | 4859 | 		} else if (ret == 0) { | 
 | 4860 | 			/* | 
 | 4861 | 			 * If map blocks could not find the block, | 
 | 4862 | 			 * then it is in a hole.  If the hole was | 
 | 4863 | 			 * not already cached, then map blocks should | 
 | 4864 | 			 * put it in the cache.  So we can get the hole | 
 | 4865 | 			 * out of the cache | 
 | 4866 | 			 */ | 
 | 4867 | 			memset(&cache_ex, 0, sizeof(cache_ex)); | 
 | 4868 | 			if ((ext4_ext_check_cache(inode, iblock, &cache_ex)) && | 
 | 4869 | 				!cache_ex.ec_start) { | 
 | 4870 |  | 
 | 4871 | 				/* The hole is cached */ | 
 | 4872 | 				num_blocks = cache_ex.ec_block + | 
 | 4873 | 				cache_ex.ec_len - iblock; | 
 | 4874 |  | 
 | 4875 | 			} else { | 
 | 4876 | 				/* The block could not be identified */ | 
 | 4877 | 				err = -EIO; | 
 | 4878 | 				break; | 
 | 4879 | 			} | 
 | 4880 | 		} else { | 
 | 4881 | 			/* Map blocks error */ | 
 | 4882 | 			err = ret; | 
 | 4883 | 			break; | 
 | 4884 | 		} | 
 | 4885 |  | 
 | 4886 | 		if (num_blocks == 0) { | 
 | 4887 | 			/* This condition should never happen */ | 
 | 4888 | 			ext_debug("Block lookup failed"); | 
 | 4889 | 			err = -EIO; | 
 | 4890 | 			break; | 
 | 4891 | 		} | 
 | 4892 |  | 
 | 4893 | 		iblock += num_blocks; | 
 | 4894 | 	} | 
 | 4895 |  | 
 | 4896 | 	if (blocks_released > 0) { | 
 | 4897 | 		ext4_ext_invalidate_cache(inode); | 
 | 4898 | 		ext4_discard_preallocations(inode); | 
 | 4899 | 	} | 
 | 4900 |  | 
 | 4901 | 	if (IS_SYNC(inode)) | 
 | 4902 | 		ext4_handle_sync(handle); | 
 | 4903 |  | 
 | 4904 | 	up_write(&EXT4_I(inode)->i_data_sem); | 
 | 4905 |  | 
 | 4906 | out: | 
 | 4907 | 	ext4_orphan_del(handle, inode); | 
 | 4908 | 	inode->i_mtime = inode->i_ctime = ext4_current_time(inode); | 
 | 4909 | 	ext4_mark_inode_dirty(handle, inode); | 
 | 4910 | 	ext4_journal_stop(handle); | 
 | 4911 | 	return err; | 
 | 4912 | } | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4913 | int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, | 
 | 4914 | 		__u64 start, __u64 len) | 
 | 4915 | { | 
 | 4916 | 	ext4_lblk_t start_blk; | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4917 | 	int error = 0; | 
 | 4918 |  | 
 | 4919 | 	/* fallback to generic here if not in extents fmt */ | 
| Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 4920 | 	if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4921 | 		return generic_block_fiemap(inode, fieinfo, start, len, | 
 | 4922 | 			ext4_get_block); | 
 | 4923 |  | 
 | 4924 | 	if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS)) | 
 | 4925 | 		return -EBADR; | 
 | 4926 |  | 
 | 4927 | 	if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { | 
 | 4928 | 		error = ext4_xattr_fiemap(inode, fieinfo); | 
 | 4929 | 	} else { | 
| Leonard Michlmayr | aca92ff | 2010-03-04 17:07:28 -0500 | [diff] [blame] | 4930 | 		ext4_lblk_t len_blks; | 
 | 4931 | 		__u64 last_blk; | 
 | 4932 |  | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4933 | 		start_blk = start >> inode->i_sb->s_blocksize_bits; | 
| Leonard Michlmayr | aca92ff | 2010-03-04 17:07:28 -0500 | [diff] [blame] | 4934 | 		last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits; | 
| Lukas Czerner | f17722f | 2011-06-06 00:05:17 -0400 | [diff] [blame] | 4935 | 		if (last_blk >= EXT_MAX_BLOCKS) | 
 | 4936 | 			last_blk = EXT_MAX_BLOCKS-1; | 
| Leonard Michlmayr | aca92ff | 2010-03-04 17:07:28 -0500 | [diff] [blame] | 4937 | 		len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1; | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4938 |  | 
 | 4939 | 		/* | 
 | 4940 | 		 * Walk the extent tree gathering extent information. | 
 | 4941 | 		 * ext4_ext_fiemap_cb will push extents back to user. | 
 | 4942 | 		 */ | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4943 | 		error = ext4_ext_walk_space(inode, start_blk, len_blks, | 
 | 4944 | 					  ext4_ext_fiemap_cb, fieinfo); | 
| Eric Sandeen | 6873fa0 | 2008-10-07 00:46:36 -0400 | [diff] [blame] | 4945 | 	} | 
 | 4946 |  | 
 | 4947 | 	return error; | 
 | 4948 | } |