]>
Commit | Line | Data |
---|---|---|
a86c6181 AT |
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 | ||
32 | #include <linux/module.h> | |
33 | #include <linux/fs.h> | |
34 | #include <linux/time.h> | |
cd02ff0b | 35 | #include <linux/jbd2.h> |
a86c6181 AT |
36 | #include <linux/highuid.h> |
37 | #include <linux/pagemap.h> | |
38 | #include <linux/quotaops.h> | |
39 | #include <linux/string.h> | |
40 | #include <linux/slab.h> | |
a2df2a63 | 41 | #include <linux/falloc.h> |
a86c6181 | 42 | #include <asm/uaccess.h> |
6873fa0d | 43 | #include <linux/fiemap.h> |
3dcf5451 CH |
44 | #include "ext4_jbd2.h" |
45 | #include "ext4_extents.h" | |
a86c6181 AT |
46 | |
47 | ||
d0d856e8 RD |
48 | /* |
49 | * ext_pblock: | |
50 | * combine low and high parts of physical block number into ext4_fsblk_t | |
51 | */ | |
09b88252 | 52 | static ext4_fsblk_t ext_pblock(struct ext4_extent *ex) |
f65e6fba AT |
53 | { |
54 | ext4_fsblk_t block; | |
55 | ||
b377611d | 56 | block = le32_to_cpu(ex->ee_start_lo); |
9b8f1f01 | 57 | block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1; |
f65e6fba AT |
58 | return block; |
59 | } | |
60 | ||
d0d856e8 RD |
61 | /* |
62 | * idx_pblock: | |
63 | * combine low and high parts of a leaf physical block number into ext4_fsblk_t | |
64 | */ | |
c14c6fd5 | 65 | ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix) |
f65e6fba AT |
66 | { |
67 | ext4_fsblk_t block; | |
68 | ||
d8dd0b45 | 69 | block = le32_to_cpu(ix->ei_leaf_lo); |
9b8f1f01 | 70 | block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1; |
f65e6fba AT |
71 | return block; |
72 | } | |
73 | ||
d0d856e8 RD |
74 | /* |
75 | * ext4_ext_store_pblock: | |
76 | * stores a large physical block number into an extent struct, | |
77 | * breaking it into parts | |
78 | */ | |
c14c6fd5 | 79 | void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb) |
f65e6fba | 80 | { |
b377611d | 81 | ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff)); |
9b8f1f01 | 82 | ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff); |
f65e6fba AT |
83 | } |
84 | ||
d0d856e8 RD |
85 | /* |
86 | * ext4_idx_store_pblock: | |
87 | * stores a large physical block number into an index struct, | |
88 | * breaking it into parts | |
89 | */ | |
09b88252 | 90 | static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb) |
f65e6fba | 91 | { |
d8dd0b45 | 92 | ix->ei_leaf_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff)); |
9b8f1f01 | 93 | ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff); |
f65e6fba AT |
94 | } |
95 | ||
9102e4fa | 96 | static int ext4_ext_journal_restart(handle_t *handle, int needed) |
a86c6181 AT |
97 | { |
98 | int err; | |
99 | ||
0390131b FM |
100 | if (!ext4_handle_valid(handle)) |
101 | return 0; | |
a86c6181 | 102 | if (handle->h_buffer_credits > needed) |
9102e4fa SF |
103 | return 0; |
104 | err = ext4_journal_extend(handle, needed); | |
0123c939 | 105 | if (err <= 0) |
9102e4fa SF |
106 | return err; |
107 | return ext4_journal_restart(handle, needed); | |
a86c6181 AT |
108 | } |
109 | ||
110 | /* | |
111 | * could return: | |
112 | * - EROFS | |
113 | * - ENOMEM | |
114 | */ | |
115 | static int ext4_ext_get_access(handle_t *handle, struct inode *inode, | |
116 | struct ext4_ext_path *path) | |
117 | { | |
118 | if (path->p_bh) { | |
119 | /* path points to block */ | |
120 | return ext4_journal_get_write_access(handle, path->p_bh); | |
121 | } | |
122 | /* path points to leaf/index in inode body */ | |
123 | /* we use in-core data, no need to protect them */ | |
124 | return 0; | |
125 | } | |
126 | ||
127 | /* | |
128 | * could return: | |
129 | * - EROFS | |
130 | * - ENOMEM | |
131 | * - EIO | |
132 | */ | |
133 | static int ext4_ext_dirty(handle_t *handle, struct inode *inode, | |
134 | struct ext4_ext_path *path) | |
135 | { | |
136 | int err; | |
137 | if (path->p_bh) { | |
138 | /* path points to block */ | |
0390131b | 139 | err = ext4_handle_dirty_metadata(handle, inode, path->p_bh); |
a86c6181 AT |
140 | } else { |
141 | /* path points to leaf/index in inode body */ | |
142 | err = ext4_mark_inode_dirty(handle, inode); | |
143 | } | |
144 | return err; | |
145 | } | |
146 | ||
f65e6fba | 147 | static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, |
a86c6181 | 148 | struct ext4_ext_path *path, |
725d26d3 | 149 | ext4_lblk_t block) |
a86c6181 AT |
150 | { |
151 | struct ext4_inode_info *ei = EXT4_I(inode); | |
f65e6fba | 152 | ext4_fsblk_t bg_start; |
74d3487f | 153 | ext4_fsblk_t last_block; |
f65e6fba | 154 | ext4_grpblk_t colour; |
a4912123 TT |
155 | ext4_group_t block_group; |
156 | int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb)); | |
a86c6181 AT |
157 | int depth; |
158 | ||
159 | if (path) { | |
160 | struct ext4_extent *ex; | |
161 | depth = path->p_depth; | |
162 | ||
163 | /* try to predict block placement */ | |
7e028976 AM |
164 | ex = path[depth].p_ext; |
165 | if (ex) | |
f65e6fba | 166 | return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block)); |
a86c6181 | 167 | |
d0d856e8 RD |
168 | /* it looks like index is empty; |
169 | * try to find starting block from index itself */ | |
a86c6181 AT |
170 | if (path[depth].p_bh) |
171 | return path[depth].p_bh->b_blocknr; | |
172 | } | |
173 | ||
174 | /* OK. use inode's group */ | |
a4912123 TT |
175 | block_group = ei->i_block_group; |
176 | if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) { | |
177 | /* | |
178 | * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME | |
179 | * block groups per flexgroup, reserve the first block | |
180 | * group for directories and special files. Regular | |
181 | * files will start at the second block group. This | |
182 | * tends to speed up directory access and improves | |
183 | * fsck times. | |
184 | */ | |
185 | block_group &= ~(flex_size-1); | |
186 | if (S_ISREG(inode->i_mode)) | |
187 | block_group++; | |
188 | } | |
189 | bg_start = (block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) + | |
a86c6181 | 190 | le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block); |
74d3487f VC |
191 | last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1; |
192 | ||
a4912123 TT |
193 | /* |
194 | * If we are doing delayed allocation, we don't need take | |
195 | * colour into account. | |
196 | */ | |
197 | if (test_opt(inode->i_sb, DELALLOC)) | |
198 | return bg_start; | |
199 | ||
74d3487f VC |
200 | if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block) |
201 | colour = (current->pid % 16) * | |
a86c6181 | 202 | (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); |
74d3487f VC |
203 | else |
204 | colour = (current->pid % 16) * ((last_block - bg_start) / 16); | |
a86c6181 AT |
205 | return bg_start + colour + block; |
206 | } | |
207 | ||
654b4908 AK |
208 | /* |
209 | * Allocation for a meta data block | |
210 | */ | |
f65e6fba | 211 | static ext4_fsblk_t |
654b4908 | 212 | ext4_ext_new_meta_block(handle_t *handle, struct inode *inode, |
a86c6181 AT |
213 | struct ext4_ext_path *path, |
214 | struct ext4_extent *ex, int *err) | |
215 | { | |
f65e6fba | 216 | ext4_fsblk_t goal, newblock; |
a86c6181 AT |
217 | |
218 | goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block)); | |
97df5d15 | 219 | newblock = ext4_new_meta_blocks(handle, inode, goal, NULL, err); |
a86c6181 AT |
220 | return newblock; |
221 | } | |
222 | ||
09b88252 | 223 | static int ext4_ext_space_block(struct inode *inode) |
a86c6181 AT |
224 | { |
225 | int size; | |
226 | ||
227 | size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) | |
228 | / sizeof(struct ext4_extent); | |
bbf2f9fb | 229 | #ifdef AGGRESSIVE_TEST |
a86c6181 AT |
230 | if (size > 6) |
231 | size = 6; | |
232 | #endif | |
233 | return size; | |
234 | } | |
235 | ||
09b88252 | 236 | static int ext4_ext_space_block_idx(struct inode *inode) |
a86c6181 AT |
237 | { |
238 | int size; | |
239 | ||
240 | size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) | |
241 | / sizeof(struct ext4_extent_idx); | |
bbf2f9fb | 242 | #ifdef AGGRESSIVE_TEST |
a86c6181 AT |
243 | if (size > 5) |
244 | size = 5; | |
245 | #endif | |
246 | return size; | |
247 | } | |
248 | ||
09b88252 | 249 | static int ext4_ext_space_root(struct inode *inode) |
a86c6181 AT |
250 | { |
251 | int size; | |
252 | ||
253 | size = sizeof(EXT4_I(inode)->i_data); | |
254 | size -= sizeof(struct ext4_extent_header); | |
255 | size /= sizeof(struct ext4_extent); | |
bbf2f9fb | 256 | #ifdef AGGRESSIVE_TEST |
a86c6181 AT |
257 | if (size > 3) |
258 | size = 3; | |
259 | #endif | |
260 | return size; | |
261 | } | |
262 | ||
09b88252 | 263 | static int ext4_ext_space_root_idx(struct inode *inode) |
a86c6181 AT |
264 | { |
265 | int size; | |
266 | ||
267 | size = sizeof(EXT4_I(inode)->i_data); | |
268 | size -= sizeof(struct ext4_extent_header); | |
269 | size /= sizeof(struct ext4_extent_idx); | |
bbf2f9fb | 270 | #ifdef AGGRESSIVE_TEST |
a86c6181 AT |
271 | if (size > 4) |
272 | size = 4; | |
273 | #endif | |
274 | return size; | |
275 | } | |
276 | ||
d2a17637 MC |
277 | /* |
278 | * Calculate the number of metadata blocks needed | |
279 | * to allocate @blocks | |
280 | * Worse case is one block per extent | |
281 | */ | |
282 | int ext4_ext_calc_metadata_amount(struct inode *inode, int blocks) | |
283 | { | |
284 | int lcap, icap, rcap, leafs, idxs, num; | |
285 | int newextents = blocks; | |
286 | ||
287 | rcap = ext4_ext_space_root_idx(inode); | |
288 | lcap = ext4_ext_space_block(inode); | |
289 | icap = ext4_ext_space_block_idx(inode); | |
290 | ||
291 | /* number of new leaf blocks needed */ | |
292 | num = leafs = (newextents + lcap - 1) / lcap; | |
293 | ||
294 | /* | |
295 | * Worse case, we need separate index block(s) | |
296 | * to link all new leaf blocks | |
297 | */ | |
298 | idxs = (leafs + icap - 1) / icap; | |
299 | do { | |
300 | num += idxs; | |
301 | idxs = (idxs + icap - 1) / icap; | |
302 | } while (idxs > rcap); | |
303 | ||
304 | return num; | |
305 | } | |
306 | ||
c29c0ae7 AT |
307 | static int |
308 | ext4_ext_max_entries(struct inode *inode, int depth) | |
309 | { | |
310 | int max; | |
311 | ||
312 | if (depth == ext_depth(inode)) { | |
313 | if (depth == 0) | |
314 | max = ext4_ext_space_root(inode); | |
315 | else | |
316 | max = ext4_ext_space_root_idx(inode); | |
317 | } else { | |
318 | if (depth == 0) | |
319 | max = ext4_ext_space_block(inode); | |
320 | else | |
321 | max = ext4_ext_space_block_idx(inode); | |
322 | } | |
323 | ||
324 | return max; | |
325 | } | |
326 | ||
327 | static int __ext4_ext_check_header(const char *function, struct inode *inode, | |
328 | struct ext4_extent_header *eh, | |
329 | int depth) | |
330 | { | |
331 | const char *error_msg; | |
332 | int max = 0; | |
333 | ||
334 | if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) { | |
335 | error_msg = "invalid magic"; | |
336 | goto corrupted; | |
337 | } | |
338 | if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) { | |
339 | error_msg = "unexpected eh_depth"; | |
340 | goto corrupted; | |
341 | } | |
342 | if (unlikely(eh->eh_max == 0)) { | |
343 | error_msg = "invalid eh_max"; | |
344 | goto corrupted; | |
345 | } | |
346 | max = ext4_ext_max_entries(inode, depth); | |
347 | if (unlikely(le16_to_cpu(eh->eh_max) > max)) { | |
348 | error_msg = "too large eh_max"; | |
349 | goto corrupted; | |
350 | } | |
351 | if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) { | |
352 | error_msg = "invalid eh_entries"; | |
353 | goto corrupted; | |
354 | } | |
355 | return 0; | |
356 | ||
357 | corrupted: | |
358 | ext4_error(inode->i_sb, function, | |
359 | "bad header in inode #%lu: %s - magic %x, " | |
360 | "entries %u, max %u(%u), depth %u(%u)", | |
361 | inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic), | |
362 | le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max), | |
363 | max, le16_to_cpu(eh->eh_depth), depth); | |
364 | ||
365 | return -EIO; | |
366 | } | |
367 | ||
368 | #define ext4_ext_check_header(inode, eh, depth) \ | |
46e665e9 | 369 | __ext4_ext_check_header(__func__, inode, eh, depth) |
c29c0ae7 | 370 | |
a86c6181 AT |
371 | #ifdef EXT_DEBUG |
372 | static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) | |
373 | { | |
374 | int k, l = path->p_depth; | |
375 | ||
376 | ext_debug("path:"); | |
377 | for (k = 0; k <= l; k++, path++) { | |
378 | if (path->p_idx) { | |
2ae02107 | 379 | ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block), |
f65e6fba | 380 | idx_pblock(path->p_idx)); |
a86c6181 | 381 | } else if (path->p_ext) { |
2ae02107 | 382 | ext_debug(" %d:%d:%llu ", |
a86c6181 | 383 | le32_to_cpu(path->p_ext->ee_block), |
a2df2a63 | 384 | ext4_ext_get_actual_len(path->p_ext), |
f65e6fba | 385 | ext_pblock(path->p_ext)); |
a86c6181 AT |
386 | } else |
387 | ext_debug(" []"); | |
388 | } | |
389 | ext_debug("\n"); | |
390 | } | |
391 | ||
392 | static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) | |
393 | { | |
394 | int depth = ext_depth(inode); | |
395 | struct ext4_extent_header *eh; | |
396 | struct ext4_extent *ex; | |
397 | int i; | |
398 | ||
399 | if (!path) | |
400 | return; | |
401 | ||
402 | eh = path[depth].p_hdr; | |
403 | ex = EXT_FIRST_EXTENT(eh); | |
404 | ||
405 | for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { | |
2ae02107 | 406 | ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block), |
a2df2a63 | 407 | ext4_ext_get_actual_len(ex), ext_pblock(ex)); |
a86c6181 AT |
408 | } |
409 | ext_debug("\n"); | |
410 | } | |
411 | #else | |
af5bc92d TT |
412 | #define ext4_ext_show_path(inode, path) |
413 | #define ext4_ext_show_leaf(inode, path) | |
a86c6181 AT |
414 | #endif |
415 | ||
b35905c1 | 416 | void ext4_ext_drop_refs(struct ext4_ext_path *path) |
a86c6181 AT |
417 | { |
418 | int depth = path->p_depth; | |
419 | int i; | |
420 | ||
421 | for (i = 0; i <= depth; i++, path++) | |
422 | if (path->p_bh) { | |
423 | brelse(path->p_bh); | |
424 | path->p_bh = NULL; | |
425 | } | |
426 | } | |
427 | ||
428 | /* | |
d0d856e8 RD |
429 | * ext4_ext_binsearch_idx: |
430 | * binary search for the closest index of the given block | |
c29c0ae7 | 431 | * the header must be checked before calling this |
a86c6181 AT |
432 | */ |
433 | static void | |
725d26d3 AK |
434 | ext4_ext_binsearch_idx(struct inode *inode, |
435 | struct ext4_ext_path *path, ext4_lblk_t block) | |
a86c6181 AT |
436 | { |
437 | struct ext4_extent_header *eh = path->p_hdr; | |
438 | struct ext4_extent_idx *r, *l, *m; | |
439 | ||
a86c6181 | 440 | |
bba90743 | 441 | ext_debug("binsearch for %u(idx): ", block); |
a86c6181 AT |
442 | |
443 | l = EXT_FIRST_INDEX(eh) + 1; | |
e9f410b1 | 444 | r = EXT_LAST_INDEX(eh); |
a86c6181 AT |
445 | while (l <= r) { |
446 | m = l + (r - l) / 2; | |
447 | if (block < le32_to_cpu(m->ei_block)) | |
448 | r = m - 1; | |
449 | else | |
450 | l = m + 1; | |
26d535ed DM |
451 | ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block), |
452 | m, le32_to_cpu(m->ei_block), | |
453 | r, le32_to_cpu(r->ei_block)); | |
a86c6181 AT |
454 | } |
455 | ||
456 | path->p_idx = l - 1; | |
f65e6fba | 457 | ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block), |
26d535ed | 458 | idx_pblock(path->p_idx)); |
a86c6181 AT |
459 | |
460 | #ifdef CHECK_BINSEARCH | |
461 | { | |
462 | struct ext4_extent_idx *chix, *ix; | |
463 | int k; | |
464 | ||
465 | chix = ix = EXT_FIRST_INDEX(eh); | |
466 | for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) { | |
467 | if (k != 0 && | |
468 | le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) { | |
4776004f TT |
469 | printk(KERN_DEBUG "k=%d, ix=0x%p, " |
470 | "first=0x%p\n", k, | |
471 | ix, EXT_FIRST_INDEX(eh)); | |
472 | printk(KERN_DEBUG "%u <= %u\n", | |
a86c6181 AT |
473 | le32_to_cpu(ix->ei_block), |
474 | le32_to_cpu(ix[-1].ei_block)); | |
475 | } | |
476 | BUG_ON(k && le32_to_cpu(ix->ei_block) | |
8c55e204 | 477 | <= le32_to_cpu(ix[-1].ei_block)); |
a86c6181 AT |
478 | if (block < le32_to_cpu(ix->ei_block)) |
479 | break; | |
480 | chix = ix; | |
481 | } | |
482 | BUG_ON(chix != path->p_idx); | |
483 | } | |
484 | #endif | |
485 | ||
486 | } | |
487 | ||
488 | /* | |
d0d856e8 RD |
489 | * ext4_ext_binsearch: |
490 | * binary search for closest extent of the given block | |
c29c0ae7 | 491 | * the header must be checked before calling this |
a86c6181 AT |
492 | */ |
493 | static void | |
725d26d3 AK |
494 | ext4_ext_binsearch(struct inode *inode, |
495 | struct ext4_ext_path *path, ext4_lblk_t block) | |
a86c6181 AT |
496 | { |
497 | struct ext4_extent_header *eh = path->p_hdr; | |
498 | struct ext4_extent *r, *l, *m; | |
499 | ||
a86c6181 AT |
500 | if (eh->eh_entries == 0) { |
501 | /* | |
d0d856e8 RD |
502 | * this leaf is empty: |
503 | * we get such a leaf in split/add case | |
a86c6181 AT |
504 | */ |
505 | return; | |
506 | } | |
507 | ||
bba90743 | 508 | ext_debug("binsearch for %u: ", block); |
a86c6181 AT |
509 | |
510 | l = EXT_FIRST_EXTENT(eh) + 1; | |
e9f410b1 | 511 | r = EXT_LAST_EXTENT(eh); |
a86c6181 AT |
512 | |
513 | while (l <= r) { | |
514 | m = l + (r - l) / 2; | |
515 | if (block < le32_to_cpu(m->ee_block)) | |
516 | r = m - 1; | |
517 | else | |
518 | l = m + 1; | |
26d535ed DM |
519 | ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block), |
520 | m, le32_to_cpu(m->ee_block), | |
521 | r, le32_to_cpu(r->ee_block)); | |
a86c6181 AT |
522 | } |
523 | ||
524 | path->p_ext = l - 1; | |
2ae02107 | 525 | ext_debug(" -> %d:%llu:%d ", |
8c55e204 DK |
526 | le32_to_cpu(path->p_ext->ee_block), |
527 | ext_pblock(path->p_ext), | |
a2df2a63 | 528 | ext4_ext_get_actual_len(path->p_ext)); |
a86c6181 AT |
529 | |
530 | #ifdef CHECK_BINSEARCH | |
531 | { | |
532 | struct ext4_extent *chex, *ex; | |
533 | int k; | |
534 | ||
535 | chex = ex = EXT_FIRST_EXTENT(eh); | |
536 | for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) { | |
537 | BUG_ON(k && le32_to_cpu(ex->ee_block) | |
8c55e204 | 538 | <= le32_to_cpu(ex[-1].ee_block)); |
a86c6181 AT |
539 | if (block < le32_to_cpu(ex->ee_block)) |
540 | break; | |
541 | chex = ex; | |
542 | } | |
543 | BUG_ON(chex != path->p_ext); | |
544 | } | |
545 | #endif | |
546 | ||
547 | } | |
548 | ||
549 | int ext4_ext_tree_init(handle_t *handle, struct inode *inode) | |
550 | { | |
551 | struct ext4_extent_header *eh; | |
552 | ||
553 | eh = ext_inode_hdr(inode); | |
554 | eh->eh_depth = 0; | |
555 | eh->eh_entries = 0; | |
556 | eh->eh_magic = EXT4_EXT_MAGIC; | |
557 | eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode)); | |
558 | ext4_mark_inode_dirty(handle, inode); | |
559 | ext4_ext_invalidate_cache(inode); | |
560 | return 0; | |
561 | } | |
562 | ||
563 | struct ext4_ext_path * | |
725d26d3 AK |
564 | ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, |
565 | struct ext4_ext_path *path) | |
a86c6181 AT |
566 | { |
567 | struct ext4_extent_header *eh; | |
568 | struct buffer_head *bh; | |
569 | short int depth, i, ppos = 0, alloc = 0; | |
570 | ||
571 | eh = ext_inode_hdr(inode); | |
c29c0ae7 AT |
572 | depth = ext_depth(inode); |
573 | if (ext4_ext_check_header(inode, eh, depth)) | |
a86c6181 AT |
574 | return ERR_PTR(-EIO); |
575 | ||
a86c6181 AT |
576 | |
577 | /* account possible depth increase */ | |
578 | if (!path) { | |
5d4958f9 | 579 | path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2), |
a86c6181 AT |
580 | GFP_NOFS); |
581 | if (!path) | |
582 | return ERR_PTR(-ENOMEM); | |
583 | alloc = 1; | |
584 | } | |
a86c6181 | 585 | path[0].p_hdr = eh; |
1973adcb | 586 | path[0].p_bh = NULL; |
a86c6181 | 587 | |
c29c0ae7 | 588 | i = depth; |
a86c6181 AT |
589 | /* walk through the tree */ |
590 | while (i) { | |
591 | ext_debug("depth %d: num %d, max %d\n", | |
592 | ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); | |
c29c0ae7 | 593 | |
a86c6181 | 594 | ext4_ext_binsearch_idx(inode, path + ppos, block); |
f65e6fba | 595 | path[ppos].p_block = idx_pblock(path[ppos].p_idx); |
a86c6181 AT |
596 | path[ppos].p_depth = i; |
597 | path[ppos].p_ext = NULL; | |
598 | ||
599 | bh = sb_bread(inode->i_sb, path[ppos].p_block); | |
600 | if (!bh) | |
601 | goto err; | |
602 | ||
603 | eh = ext_block_hdr(bh); | |
604 | ppos++; | |
605 | BUG_ON(ppos > depth); | |
606 | path[ppos].p_bh = bh; | |
607 | path[ppos].p_hdr = eh; | |
608 | i--; | |
609 | ||
c29c0ae7 | 610 | if (ext4_ext_check_header(inode, eh, i)) |
a86c6181 AT |
611 | goto err; |
612 | } | |
613 | ||
614 | path[ppos].p_depth = i; | |
a86c6181 AT |
615 | path[ppos].p_ext = NULL; |
616 | path[ppos].p_idx = NULL; | |
617 | ||
a86c6181 AT |
618 | /* find extent */ |
619 | ext4_ext_binsearch(inode, path + ppos, block); | |
1973adcb SF |
620 | /* if not an empty leaf */ |
621 | if (path[ppos].p_ext) | |
622 | path[ppos].p_block = ext_pblock(path[ppos].p_ext); | |
a86c6181 AT |
623 | |
624 | ext4_ext_show_path(inode, path); | |
625 | ||
626 | return path; | |
627 | ||
628 | err: | |
629 | ext4_ext_drop_refs(path); | |
630 | if (alloc) | |
631 | kfree(path); | |
632 | return ERR_PTR(-EIO); | |
633 | } | |
634 | ||
635 | /* | |
d0d856e8 RD |
636 | * ext4_ext_insert_index: |
637 | * insert new index [@logical;@ptr] into the block at @curp; | |
638 | * check where to insert: before @curp or after @curp | |
a86c6181 AT |
639 | */ |
640 | static int ext4_ext_insert_index(handle_t *handle, struct inode *inode, | |
641 | struct ext4_ext_path *curp, | |
f65e6fba | 642 | int logical, ext4_fsblk_t ptr) |
a86c6181 AT |
643 | { |
644 | struct ext4_extent_idx *ix; | |
645 | int len, err; | |
646 | ||
7e028976 AM |
647 | err = ext4_ext_get_access(handle, inode, curp); |
648 | if (err) | |
a86c6181 AT |
649 | return err; |
650 | ||
651 | BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block)); | |
652 | len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx; | |
653 | if (logical > le32_to_cpu(curp->p_idx->ei_block)) { | |
654 | /* insert after */ | |
655 | if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) { | |
656 | len = (len - 1) * sizeof(struct ext4_extent_idx); | |
657 | len = len < 0 ? 0 : len; | |
26d535ed | 658 | ext_debug("insert new index %d after: %llu. " |
a86c6181 AT |
659 | "move %d from 0x%p to 0x%p\n", |
660 | logical, ptr, len, | |
661 | (curp->p_idx + 1), (curp->p_idx + 2)); | |
662 | memmove(curp->p_idx + 2, curp->p_idx + 1, len); | |
663 | } | |
664 | ix = curp->p_idx + 1; | |
665 | } else { | |
666 | /* insert before */ | |
667 | len = len * sizeof(struct ext4_extent_idx); | |
668 | len = len < 0 ? 0 : len; | |
26d535ed | 669 | ext_debug("insert new index %d before: %llu. " |
a86c6181 AT |
670 | "move %d from 0x%p to 0x%p\n", |
671 | logical, ptr, len, | |
672 | curp->p_idx, (curp->p_idx + 1)); | |
673 | memmove(curp->p_idx + 1, curp->p_idx, len); | |
674 | ix = curp->p_idx; | |
675 | } | |
676 | ||
677 | ix->ei_block = cpu_to_le32(logical); | |
f65e6fba | 678 | ext4_idx_store_pblock(ix, ptr); |
e8546d06 | 679 | le16_add_cpu(&curp->p_hdr->eh_entries, 1); |
a86c6181 AT |
680 | |
681 | BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries) | |
8c55e204 | 682 | > le16_to_cpu(curp->p_hdr->eh_max)); |
a86c6181 AT |
683 | BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr)); |
684 | ||
685 | err = ext4_ext_dirty(handle, inode, curp); | |
686 | ext4_std_error(inode->i_sb, err); | |
687 | ||
688 | return err; | |
689 | } | |
690 | ||
691 | /* | |
d0d856e8 RD |
692 | * ext4_ext_split: |
693 | * inserts new subtree into the path, using free index entry | |
694 | * at depth @at: | |
695 | * - allocates all needed blocks (new leaf and all intermediate index blocks) | |
696 | * - makes decision where to split | |
697 | * - moves remaining extents and index entries (right to the split point) | |
698 | * into the newly allocated blocks | |
699 | * - initializes subtree | |
a86c6181 AT |
700 | */ |
701 | static int ext4_ext_split(handle_t *handle, struct inode *inode, | |
702 | struct ext4_ext_path *path, | |
703 | struct ext4_extent *newext, int at) | |
704 | { | |
705 | struct buffer_head *bh = NULL; | |
706 | int depth = ext_depth(inode); | |
707 | struct ext4_extent_header *neh; | |
708 | struct ext4_extent_idx *fidx; | |
709 | struct ext4_extent *ex; | |
710 | int i = at, k, m, a; | |
f65e6fba | 711 | ext4_fsblk_t newblock, oldblock; |
a86c6181 | 712 | __le32 border; |
f65e6fba | 713 | ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */ |
a86c6181 AT |
714 | int err = 0; |
715 | ||
716 | /* make decision: where to split? */ | |
d0d856e8 | 717 | /* FIXME: now decision is simplest: at current extent */ |
a86c6181 | 718 | |
d0d856e8 | 719 | /* if current leaf will be split, then we should use |
a86c6181 AT |
720 | * border from split point */ |
721 | BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr)); | |
722 | if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) { | |
723 | border = path[depth].p_ext[1].ee_block; | |
d0d856e8 | 724 | ext_debug("leaf will be split." |
a86c6181 | 725 | " next leaf starts at %d\n", |
8c55e204 | 726 | le32_to_cpu(border)); |
a86c6181 AT |
727 | } else { |
728 | border = newext->ee_block; | |
729 | ext_debug("leaf will be added." | |
730 | " next leaf starts at %d\n", | |
8c55e204 | 731 | le32_to_cpu(border)); |
a86c6181 AT |
732 | } |
733 | ||
734 | /* | |
d0d856e8 RD |
735 | * If error occurs, then we break processing |
736 | * and mark filesystem read-only. index won't | |
a86c6181 | 737 | * be inserted and tree will be in consistent |
d0d856e8 | 738 | * state. Next mount will repair buffers too. |
a86c6181 AT |
739 | */ |
740 | ||
741 | /* | |
d0d856e8 RD |
742 | * Get array to track all allocated blocks. |
743 | * We need this to handle errors and free blocks | |
744 | * upon them. | |
a86c6181 | 745 | */ |
5d4958f9 | 746 | ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS); |
a86c6181 AT |
747 | if (!ablocks) |
748 | return -ENOMEM; | |
a86c6181 AT |
749 | |
750 | /* allocate all needed blocks */ | |
751 | ext_debug("allocate %d blocks for indexes/leaf\n", depth - at); | |
752 | for (a = 0; a < depth - at; a++) { | |
654b4908 AK |
753 | newblock = ext4_ext_new_meta_block(handle, inode, path, |
754 | newext, &err); | |
a86c6181 AT |
755 | if (newblock == 0) |
756 | goto cleanup; | |
757 | ablocks[a] = newblock; | |
758 | } | |
759 | ||
760 | /* initialize new leaf */ | |
761 | newblock = ablocks[--a]; | |
762 | BUG_ON(newblock == 0); | |
763 | bh = sb_getblk(inode->i_sb, newblock); | |
764 | if (!bh) { | |
765 | err = -EIO; | |
766 | goto cleanup; | |
767 | } | |
768 | lock_buffer(bh); | |
769 | ||
7e028976 AM |
770 | err = ext4_journal_get_create_access(handle, bh); |
771 | if (err) | |
a86c6181 AT |
772 | goto cleanup; |
773 | ||
774 | neh = ext_block_hdr(bh); | |
775 | neh->eh_entries = 0; | |
776 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode)); | |
777 | neh->eh_magic = EXT4_EXT_MAGIC; | |
778 | neh->eh_depth = 0; | |
779 | ex = EXT_FIRST_EXTENT(neh); | |
780 | ||
d0d856e8 | 781 | /* move remainder of path[depth] to the new leaf */ |
a86c6181 AT |
782 | BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max); |
783 | /* start copy from next extent */ | |
784 | /* TODO: we could do it by single memmove */ | |
785 | m = 0; | |
786 | path[depth].p_ext++; | |
787 | while (path[depth].p_ext <= | |
788 | EXT_MAX_EXTENT(path[depth].p_hdr)) { | |
2ae02107 | 789 | ext_debug("move %d:%llu:%d in new leaf %llu\n", |
8c55e204 DK |
790 | le32_to_cpu(path[depth].p_ext->ee_block), |
791 | ext_pblock(path[depth].p_ext), | |
a2df2a63 | 792 | ext4_ext_get_actual_len(path[depth].p_ext), |
a86c6181 AT |
793 | newblock); |
794 | /*memmove(ex++, path[depth].p_ext++, | |
795 | sizeof(struct ext4_extent)); | |
796 | neh->eh_entries++;*/ | |
797 | path[depth].p_ext++; | |
798 | m++; | |
799 | } | |
800 | if (m) { | |
801 | memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m); | |
e8546d06 | 802 | le16_add_cpu(&neh->eh_entries, m); |
a86c6181 AT |
803 | } |
804 | ||
805 | set_buffer_uptodate(bh); | |
806 | unlock_buffer(bh); | |
807 | ||
0390131b | 808 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
7e028976 | 809 | if (err) |
a86c6181 AT |
810 | goto cleanup; |
811 | brelse(bh); | |
812 | bh = NULL; | |
813 | ||
814 | /* correct old leaf */ | |
815 | if (m) { | |
7e028976 AM |
816 | err = ext4_ext_get_access(handle, inode, path + depth); |
817 | if (err) | |
a86c6181 | 818 | goto cleanup; |
e8546d06 | 819 | le16_add_cpu(&path[depth].p_hdr->eh_entries, -m); |
7e028976 AM |
820 | err = ext4_ext_dirty(handle, inode, path + depth); |
821 | if (err) | |
a86c6181 AT |
822 | goto cleanup; |
823 | ||
824 | } | |
825 | ||
826 | /* create intermediate indexes */ | |
827 | k = depth - at - 1; | |
828 | BUG_ON(k < 0); | |
829 | if (k) | |
830 | ext_debug("create %d intermediate indices\n", k); | |
831 | /* insert new index into current index block */ | |
832 | /* current depth stored in i var */ | |
833 | i = depth - 1; | |
834 | while (k--) { | |
835 | oldblock = newblock; | |
836 | newblock = ablocks[--a]; | |
bba90743 | 837 | bh = sb_getblk(inode->i_sb, newblock); |
a86c6181 AT |
838 | if (!bh) { |
839 | err = -EIO; | |
840 | goto cleanup; | |
841 | } | |
842 | lock_buffer(bh); | |
843 | ||
7e028976 AM |
844 | err = ext4_journal_get_create_access(handle, bh); |
845 | if (err) | |
a86c6181 AT |
846 | goto cleanup; |
847 | ||
848 | neh = ext_block_hdr(bh); | |
849 | neh->eh_entries = cpu_to_le16(1); | |
850 | neh->eh_magic = EXT4_EXT_MAGIC; | |
851 | neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode)); | |
852 | neh->eh_depth = cpu_to_le16(depth - i); | |
853 | fidx = EXT_FIRST_INDEX(neh); | |
854 | fidx->ei_block = border; | |
f65e6fba | 855 | ext4_idx_store_pblock(fidx, oldblock); |
a86c6181 | 856 | |
bba90743 ES |
857 | ext_debug("int.index at %d (block %llu): %u -> %llu\n", |
858 | i, newblock, le32_to_cpu(border), oldblock); | |
a86c6181 AT |
859 | /* copy indexes */ |
860 | m = 0; | |
861 | path[i].p_idx++; | |
862 | ||
863 | ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx, | |
864 | EXT_MAX_INDEX(path[i].p_hdr)); | |
865 | BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) != | |
866 | EXT_LAST_INDEX(path[i].p_hdr)); | |
867 | while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) { | |
26d535ed | 868 | ext_debug("%d: move %d:%llu in new index %llu\n", i, |
8c55e204 DK |
869 | le32_to_cpu(path[i].p_idx->ei_block), |
870 | idx_pblock(path[i].p_idx), | |
871 | newblock); | |
a86c6181 AT |
872 | /*memmove(++fidx, path[i].p_idx++, |
873 | sizeof(struct ext4_extent_idx)); | |
874 | neh->eh_entries++; | |
875 | BUG_ON(neh->eh_entries > neh->eh_max);*/ | |
876 | path[i].p_idx++; | |
877 | m++; | |
878 | } | |
879 | if (m) { | |
880 | memmove(++fidx, path[i].p_idx - m, | |
881 | sizeof(struct ext4_extent_idx) * m); | |
e8546d06 | 882 | le16_add_cpu(&neh->eh_entries, m); |
a86c6181 AT |
883 | } |
884 | set_buffer_uptodate(bh); | |
885 | unlock_buffer(bh); | |
886 | ||
0390131b | 887 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
7e028976 | 888 | if (err) |
a86c6181 AT |
889 | goto cleanup; |
890 | brelse(bh); | |
891 | bh = NULL; | |
892 | ||
893 | /* correct old index */ | |
894 | if (m) { | |
895 | err = ext4_ext_get_access(handle, inode, path + i); | |
896 | if (err) | |
897 | goto cleanup; | |
e8546d06 | 898 | le16_add_cpu(&path[i].p_hdr->eh_entries, -m); |
a86c6181 AT |
899 | err = ext4_ext_dirty(handle, inode, path + i); |
900 | if (err) | |
901 | goto cleanup; | |
902 | } | |
903 | ||
904 | i--; | |
905 | } | |
906 | ||
907 | /* insert new index */ | |
a86c6181 AT |
908 | err = ext4_ext_insert_index(handle, inode, path + at, |
909 | le32_to_cpu(border), newblock); | |
910 | ||
911 | cleanup: | |
912 | if (bh) { | |
913 | if (buffer_locked(bh)) | |
914 | unlock_buffer(bh); | |
915 | brelse(bh); | |
916 | } | |
917 | ||
918 | if (err) { | |
919 | /* free all allocated blocks in error case */ | |
920 | for (i = 0; i < depth; i++) { | |
921 | if (!ablocks[i]) | |
922 | continue; | |
c9de560d | 923 | ext4_free_blocks(handle, inode, ablocks[i], 1, 1); |
a86c6181 AT |
924 | } |
925 | } | |
926 | kfree(ablocks); | |
927 | ||
928 | return err; | |
929 | } | |
930 | ||
931 | /* | |
d0d856e8 RD |
932 | * ext4_ext_grow_indepth: |
933 | * implements tree growing procedure: | |
934 | * - allocates new block | |
935 | * - moves top-level data (index block or leaf) into the new block | |
936 | * - initializes new top-level, creating index that points to the | |
937 | * just created block | |
a86c6181 AT |
938 | */ |
939 | static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, | |
940 | struct ext4_ext_path *path, | |
941 | struct ext4_extent *newext) | |
942 | { | |
943 | struct ext4_ext_path *curp = path; | |
944 | struct ext4_extent_header *neh; | |
945 | struct ext4_extent_idx *fidx; | |
946 | struct buffer_head *bh; | |
f65e6fba | 947 | ext4_fsblk_t newblock; |
a86c6181 AT |
948 | int err = 0; |
949 | ||
654b4908 | 950 | newblock = ext4_ext_new_meta_block(handle, inode, path, newext, &err); |
a86c6181 AT |
951 | if (newblock == 0) |
952 | return err; | |
953 | ||
954 | bh = sb_getblk(inode->i_sb, newblock); | |
955 | if (!bh) { | |
956 | err = -EIO; | |
957 | ext4_std_error(inode->i_sb, err); | |
958 | return err; | |
959 | } | |
960 | lock_buffer(bh); | |
961 | ||
7e028976 AM |
962 | err = ext4_journal_get_create_access(handle, bh); |
963 | if (err) { | |
a86c6181 AT |
964 | unlock_buffer(bh); |
965 | goto out; | |
966 | } | |
967 | ||
968 | /* move top-level index/leaf into new block */ | |
969 | memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data)); | |
970 | ||
971 | /* set size of new block */ | |
972 | neh = ext_block_hdr(bh); | |
973 | /* old root could have indexes or leaves | |
974 | * so calculate e_max right way */ | |
975 | if (ext_depth(inode)) | |
976 | neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode)); | |
977 | else | |
978 | neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode)); | |
979 | neh->eh_magic = EXT4_EXT_MAGIC; | |
980 | set_buffer_uptodate(bh); | |
981 | unlock_buffer(bh); | |
982 | ||
0390131b | 983 | err = ext4_handle_dirty_metadata(handle, inode, bh); |
7e028976 | 984 | if (err) |
a86c6181 AT |
985 | goto out; |
986 | ||
987 | /* create index in new top-level index: num,max,pointer */ | |
7e028976 AM |
988 | err = ext4_ext_get_access(handle, inode, curp); |
989 | if (err) | |
a86c6181 AT |
990 | goto out; |
991 | ||
992 | curp->p_hdr->eh_magic = EXT4_EXT_MAGIC; | |
993 | curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode)); | |
994 | curp->p_hdr->eh_entries = cpu_to_le16(1); | |
995 | curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr); | |
e9f410b1 DM |
996 | |
997 | if (path[0].p_hdr->eh_depth) | |
998 | curp->p_idx->ei_block = | |
999 | EXT_FIRST_INDEX(path[0].p_hdr)->ei_block; | |
1000 | else | |
1001 | curp->p_idx->ei_block = | |
1002 | EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block; | |
f65e6fba | 1003 | ext4_idx_store_pblock(curp->p_idx, newblock); |
a86c6181 AT |
1004 | |
1005 | neh = ext_inode_hdr(inode); | |
1006 | fidx = EXT_FIRST_INDEX(neh); | |
2ae02107 | 1007 | ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n", |
a86c6181 | 1008 | le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), |
f65e6fba | 1009 | le32_to_cpu(fidx->ei_block), idx_pblock(fidx)); |
a86c6181 AT |
1010 | |
1011 | neh->eh_depth = cpu_to_le16(path->p_depth + 1); | |
1012 | err = ext4_ext_dirty(handle, inode, curp); | |
1013 | out: | |
1014 | brelse(bh); | |
1015 | ||
1016 | return err; | |
1017 | } | |
1018 | ||
1019 | /* | |
d0d856e8 RD |
1020 | * ext4_ext_create_new_leaf: |
1021 | * finds empty index and adds new leaf. | |
1022 | * if no free index is found, then it requests in-depth growing. | |
a86c6181 AT |
1023 | */ |
1024 | static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode, | |
1025 | struct ext4_ext_path *path, | |
1026 | struct ext4_extent *newext) | |
1027 | { | |
1028 | struct ext4_ext_path *curp; | |
1029 | int depth, i, err = 0; | |
1030 | ||
1031 | repeat: | |
1032 | i = depth = ext_depth(inode); | |
1033 | ||
1034 | /* walk up to the tree and look for free index entry */ | |
1035 | curp = path + depth; | |
1036 | while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) { | |
1037 | i--; | |
1038 | curp--; | |
1039 | } | |
1040 | ||
d0d856e8 RD |
1041 | /* we use already allocated block for index block, |
1042 | * so subsequent data blocks should be contiguous */ | |
a86c6181 AT |
1043 | if (EXT_HAS_FREE_INDEX(curp)) { |
1044 | /* if we found index with free entry, then use that | |
1045 | * entry: create all needed subtree and add new leaf */ | |
1046 | err = ext4_ext_split(handle, inode, path, newext, i); | |
787e0981 SF |
1047 | if (err) |
1048 | goto out; | |
a86c6181 AT |
1049 | |
1050 | /* refill path */ | |
1051 | ext4_ext_drop_refs(path); | |
1052 | path = ext4_ext_find_extent(inode, | |
725d26d3 AK |
1053 | (ext4_lblk_t)le32_to_cpu(newext->ee_block), |
1054 | path); | |
a86c6181 AT |
1055 | if (IS_ERR(path)) |
1056 | err = PTR_ERR(path); | |
1057 | } else { | |
1058 | /* tree is full, time to grow in depth */ | |
1059 | err = ext4_ext_grow_indepth(handle, inode, path, newext); | |
1060 | if (err) | |
1061 | goto out; | |
1062 | ||
1063 | /* refill path */ | |
1064 | ext4_ext_drop_refs(path); | |
1065 | path = ext4_ext_find_extent(inode, | |
725d26d3 AK |
1066 | (ext4_lblk_t)le32_to_cpu(newext->ee_block), |
1067 | path); | |
a86c6181 AT |
1068 | if (IS_ERR(path)) { |
1069 | err = PTR_ERR(path); | |
1070 | goto out; | |
1071 | } | |
1072 | ||
1073 | /* | |
d0d856e8 RD |
1074 | * only first (depth 0 -> 1) produces free space; |
1075 | * in all other cases we have to split the grown tree | |
a86c6181 AT |
1076 | */ |
1077 | depth = ext_depth(inode); | |
1078 | if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) { | |
d0d856e8 | 1079 | /* now we need to split */ |
a86c6181 AT |
1080 | goto repeat; |
1081 | } | |
1082 | } | |
1083 | ||
1084 | out: | |
1085 | return err; | |
1086 | } | |
1087 | ||
1988b51e AT |
1088 | /* |
1089 | * search the closest allocated block to the left for *logical | |
1090 | * and returns it at @logical + it's physical address at @phys | |
1091 | * if *logical is the smallest allocated block, the function | |
1092 | * returns 0 at @phys | |
1093 | * return value contains 0 (success) or error code | |
1094 | */ | |
1095 | int | |
1096 | ext4_ext_search_left(struct inode *inode, struct ext4_ext_path *path, | |
1097 | ext4_lblk_t *logical, ext4_fsblk_t *phys) | |
1098 | { | |
1099 | struct ext4_extent_idx *ix; | |
1100 | struct ext4_extent *ex; | |
b939e376 | 1101 | int depth, ee_len; |
1988b51e AT |
1102 | |
1103 | BUG_ON(path == NULL); | |
1104 | depth = path->p_depth; | |
1105 | *phys = 0; | |
1106 | ||
1107 | if (depth == 0 && path->p_ext == NULL) | |
1108 | return 0; | |
1109 | ||
1110 | /* usually extent in the path covers blocks smaller | |
1111 | * then *logical, but it can be that extent is the | |
1112 | * first one in the file */ | |
1113 | ||
1114 | ex = path[depth].p_ext; | |
b939e376 | 1115 | ee_len = ext4_ext_get_actual_len(ex); |
1988b51e AT |
1116 | if (*logical < le32_to_cpu(ex->ee_block)) { |
1117 | BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex); | |
1118 | while (--depth >= 0) { | |
1119 | ix = path[depth].p_idx; | |
1120 | BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr)); | |
1121 | } | |
1122 | return 0; | |
1123 | } | |
1124 | ||
b939e376 | 1125 | BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len)); |
1988b51e | 1126 | |
b939e376 AK |
1127 | *logical = le32_to_cpu(ex->ee_block) + ee_len - 1; |
1128 | *phys = ext_pblock(ex) + ee_len - 1; | |
1988b51e AT |
1129 | return 0; |
1130 | } | |
1131 | ||
1132 | /* | |
1133 | * search the closest allocated block to the right for *logical | |
1134 | * and returns it at @logical + it's physical address at @phys | |
1135 | * if *logical is the smallest allocated block, the function | |
1136 | * returns 0 at @phys | |
1137 | * return value contains 0 (success) or error code | |
1138 | */ | |
1139 | int | |
1140 | ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path, | |
1141 | ext4_lblk_t *logical, ext4_fsblk_t *phys) | |
1142 | { | |
1143 | struct buffer_head *bh = NULL; | |
1144 | struct ext4_extent_header *eh; | |
1145 | struct ext4_extent_idx *ix; | |
1146 | struct ext4_extent *ex; | |
1147 | ext4_fsblk_t block; | |
395a87bf ES |
1148 | int depth; /* Note, NOT eh_depth; depth from top of tree */ |
1149 | int ee_len; | |
1988b51e AT |
1150 | |
1151 | BUG_ON(path == NULL); | |
1152 | depth = path->p_depth; | |
1153 | *phys = 0; | |
1154 | ||
1155 | if (depth == 0 && path->p_ext == NULL) | |
1156 | return 0; | |
1157 | ||
1158 | /* usually extent in the path covers blocks smaller | |
1159 | * then *logical, but it can be that extent is the | |
1160 | * first one in the file */ | |
1161 | ||
1162 | ex = path[depth].p_ext; | |
b939e376 | 1163 | ee_len = ext4_ext_get_actual_len(ex); |
1988b51e AT |
1164 | if (*logical < le32_to_cpu(ex->ee_block)) { |
1165 | BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex); | |
1166 | while (--depth >= 0) { | |
1167 | ix = path[depth].p_idx; | |
1168 | BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr)); | |
1169 | } | |
1170 | *logical = le32_to_cpu(ex->ee_block); | |
1171 | *phys = ext_pblock(ex); | |
1172 | return 0; | |
1173 | } | |
1174 | ||
b939e376 | 1175 | BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len)); |
1988b51e AT |
1176 | |
1177 | if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) { | |
1178 | /* next allocated block in this leaf */ | |
1179 | ex++; | |
1180 | *logical = le32_to_cpu(ex->ee_block); | |
1181 | *phys = ext_pblock(ex); | |
1182 | return 0; | |
1183 | } | |
1184 | ||
1185 | /* go up and search for index to the right */ | |
1186 | while (--depth >= 0) { | |
1187 | ix = path[depth].p_idx; | |
1188 | if (ix != EXT_LAST_INDEX(path[depth].p_hdr)) | |
25f1ee3a | 1189 | goto got_index; |
1988b51e AT |
1190 | } |
1191 | ||
25f1ee3a WF |
1192 | /* we've gone up to the root and found no index to the right */ |
1193 | return 0; | |
1988b51e | 1194 | |
25f1ee3a | 1195 | got_index: |
1988b51e AT |
1196 | /* we've found index to the right, let's |
1197 | * follow it and find the closest allocated | |
1198 | * block to the right */ | |
1199 | ix++; | |
1200 | block = idx_pblock(ix); | |
1201 | while (++depth < path->p_depth) { | |
1202 | bh = sb_bread(inode->i_sb, block); | |
1203 | if (bh == NULL) | |
1204 | return -EIO; | |
1205 | eh = ext_block_hdr(bh); | |
395a87bf ES |
1206 | /* subtract from p_depth to get proper eh_depth */ |
1207 | if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) { | |
1988b51e AT |
1208 | put_bh(bh); |
1209 | return -EIO; | |
1210 | } | |
1211 | ix = EXT_FIRST_INDEX(eh); | |
1212 | block = idx_pblock(ix); | |
1213 | put_bh(bh); | |
1214 | } | |
1215 | ||
1216 | bh = sb_bread(inode->i_sb, block); | |
1217 | if (bh == NULL) | |
1218 | return -EIO; | |
1219 | eh = ext_block_hdr(bh); | |
1220 | if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) { | |
1221 | put_bh(bh); | |
1222 | return -EIO; | |
1223 | } | |
1224 | ex = EXT_FIRST_EXTENT(eh); | |
1225 | *logical = le32_to_cpu(ex->ee_block); | |
1226 | *phys = ext_pblock(ex); | |
1227 | put_bh(bh); | |
1228 | return 0; | |
1988b51e AT |
1229 | } |
1230 | ||
a86c6181 | 1231 | /* |
d0d856e8 RD |
1232 | * ext4_ext_next_allocated_block: |
1233 | * returns allocated block in subsequent extent or EXT_MAX_BLOCK. | |
1234 | * NOTE: it considers block number from index entry as | |
1235 | * allocated block. Thus, index entries have to be consistent | |
1236 | * with leaves. | |
a86c6181 | 1237 | */ |
725d26d3 | 1238 | static ext4_lblk_t |
a86c6181 AT |
1239 | ext4_ext_next_allocated_block(struct ext4_ext_path *path) |
1240 | { | |
1241 | int depth; | |
1242 | ||
1243 | BUG_ON(path == NULL); | |
1244 | depth = path->p_depth; | |
1245 | ||
1246 | if (depth == 0 && path->p_ext == NULL) | |
1247 | return EXT_MAX_BLOCK; | |
1248 | ||
1249 | while (depth >= 0) { | |
1250 | if (depth == path->p_depth) { | |
1251 | /* leaf */ | |
1252 | if (path[depth].p_ext != | |
1253 | EXT_LAST_EXTENT(path[depth].p_hdr)) | |
1254 | return le32_to_cpu(path[depth].p_ext[1].ee_block); | |
1255 | } else { | |
1256 | /* index */ | |
1257 | if (path[depth].p_idx != | |
1258 | EXT_LAST_INDEX(path[depth].p_hdr)) | |
1259 | return le32_to_cpu(path[depth].p_idx[1].ei_block); | |
1260 | } | |
1261 | depth--; | |
1262 | } | |
1263 | ||
1264 | return EXT_MAX_BLOCK; | |
1265 | } | |
1266 | ||
1267 | /* | |
d0d856e8 | 1268 | * ext4_ext_next_leaf_block: |
a86c6181 AT |
1269 | * returns first allocated block from next leaf or EXT_MAX_BLOCK |
1270 | */ | |
725d26d3 | 1271 | static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode, |
63f57933 | 1272 | struct ext4_ext_path *path) |
a86c6181 AT |
1273 | { |
1274 | int depth; | |
1275 | ||
1276 | BUG_ON(path == NULL); | |
1277 | depth = path->p_depth; | |
1278 | ||
1279 | /* zero-tree has no leaf blocks at all */ | |
1280 | if (depth == 0) | |
1281 | return EXT_MAX_BLOCK; | |
1282 | ||
1283 | /* go to index block */ | |
1284 | depth--; | |
1285 | ||
1286 | while (depth >= 0) { | |
1287 | if (path[depth].p_idx != | |
1288 | EXT_LAST_INDEX(path[depth].p_hdr)) | |
725d26d3 AK |
1289 | return (ext4_lblk_t) |
1290 | le32_to_cpu(path[depth].p_idx[1].ei_block); | |
a86c6181 AT |
1291 | depth--; |
1292 | } | |
1293 | ||
1294 | return EXT_MAX_BLOCK; | |
1295 | } | |
1296 | ||
1297 | /* | |
d0d856e8 RD |
1298 | * ext4_ext_correct_indexes: |
1299 | * if leaf gets modified and modified extent is first in the leaf, | |
1300 | * then we have to correct all indexes above. | |
a86c6181 AT |
1301 | * TODO: do we need to correct tree in all cases? |
1302 | */ | |
1d03ec98 | 1303 | static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode, |
a86c6181 AT |
1304 | struct ext4_ext_path *path) |
1305 | { | |
1306 | struct ext4_extent_header *eh; | |
1307 | int depth = ext_depth(inode); | |
1308 | struct ext4_extent *ex; | |
1309 | __le32 border; | |
1310 | int k, err = 0; | |
1311 | ||
1312 | eh = path[depth].p_hdr; | |
1313 | ex = path[depth].p_ext; | |
1314 | BUG_ON(ex == NULL); | |
1315 | BUG_ON(eh == NULL); | |
1316 | ||
1317 | if (depth == 0) { | |
1318 | /* there is no tree at all */ | |
1319 | return 0; | |
1320 | } | |
1321 | ||
1322 | if (ex != EXT_FIRST_EXTENT(eh)) { | |
1323 | /* we correct tree if first leaf got modified only */ | |
1324 | return 0; | |
1325 | } | |
1326 | ||
1327 | /* | |
d0d856e8 | 1328 | * TODO: we need correction if border is smaller than current one |
a86c6181 AT |
1329 | */ |
1330 | k = depth - 1; | |
1331 | border = path[depth].p_ext->ee_block; | |
7e028976 AM |
1332 | err = ext4_ext_get_access(handle, inode, path + k); |
1333 | if (err) | |
a86c6181 AT |
1334 | return err; |
1335 | path[k].p_idx->ei_block = border; | |
7e028976 AM |
1336 | err = ext4_ext_dirty(handle, inode, path + k); |
1337 | if (err) | |
a86c6181 AT |
1338 | return err; |
1339 | ||
1340 | while (k--) { | |
1341 | /* change all left-side indexes */ | |
1342 | if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr)) | |
1343 | break; | |
7e028976 AM |
1344 | err = ext4_ext_get_access(handle, inode, path + k); |
1345 | if (err) | |
a86c6181 AT |
1346 | break; |
1347 | path[k].p_idx->ei_block = border; | |
7e028976 AM |
1348 | err = ext4_ext_dirty(handle, inode, path + k); |
1349 | if (err) | |
a86c6181 AT |
1350 | break; |
1351 | } | |
1352 | ||
1353 | return err; | |
1354 | } | |
1355 | ||
09b88252 | 1356 | static int |
a86c6181 AT |
1357 | ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1, |
1358 | struct ext4_extent *ex2) | |
1359 | { | |
749269fa | 1360 | unsigned short ext1_ee_len, ext2_ee_len, max_len; |
a2df2a63 AA |
1361 | |
1362 | /* | |
1363 | * Make sure that either both extents are uninitialized, or | |
1364 | * both are _not_. | |
1365 | */ | |
1366 | if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2)) | |
1367 | return 0; | |
1368 | ||
749269fa AA |
1369 | if (ext4_ext_is_uninitialized(ex1)) |
1370 | max_len = EXT_UNINIT_MAX_LEN; | |
1371 | else | |
1372 | max_len = EXT_INIT_MAX_LEN; | |
1373 | ||
a2df2a63 AA |
1374 | ext1_ee_len = ext4_ext_get_actual_len(ex1); |
1375 | ext2_ee_len = ext4_ext_get_actual_len(ex2); | |
1376 | ||
1377 | if (le32_to_cpu(ex1->ee_block) + ext1_ee_len != | |
63f57933 | 1378 | le32_to_cpu(ex2->ee_block)) |
a86c6181 AT |
1379 | return 0; |
1380 | ||
471d4011 SB |
1381 | /* |
1382 | * To allow future support for preallocated extents to be added | |
1383 | * as an RO_COMPAT feature, refuse to merge to extents if | |
d0d856e8 | 1384 | * this can result in the top bit of ee_len being set. |
471d4011 | 1385 | */ |
749269fa | 1386 | if (ext1_ee_len + ext2_ee_len > max_len) |
471d4011 | 1387 | return 0; |
bbf2f9fb | 1388 | #ifdef AGGRESSIVE_TEST |
b939e376 | 1389 | if (ext1_ee_len >= 4) |
a86c6181 AT |
1390 | return 0; |
1391 | #endif | |
1392 | ||
a2df2a63 | 1393 | if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2)) |
a86c6181 AT |
1394 | return 1; |
1395 | return 0; | |
1396 | } | |
1397 | ||
56055d3a AA |
1398 | /* |
1399 | * This function tries to merge the "ex" extent to the next extent in the tree. | |
1400 | * It always tries to merge towards right. If you want to merge towards | |
1401 | * left, pass "ex - 1" as argument instead of "ex". | |
1402 | * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns | |
1403 | * 1 if they got merged. | |
1404 | */ | |
1405 | int ext4_ext_try_to_merge(struct inode *inode, | |
1406 | struct ext4_ext_path *path, | |
1407 | struct ext4_extent *ex) | |
1408 | { | |
1409 | struct ext4_extent_header *eh; | |
1410 | unsigned int depth, len; | |
1411 | int merge_done = 0; | |
1412 | int uninitialized = 0; | |
1413 | ||
1414 | depth = ext_depth(inode); | |
1415 | BUG_ON(path[depth].p_hdr == NULL); | |
1416 | eh = path[depth].p_hdr; | |
1417 | ||
1418 | while (ex < EXT_LAST_EXTENT(eh)) { | |
1419 | if (!ext4_can_extents_be_merged(inode, ex, ex + 1)) | |
1420 | break; | |
1421 | /* merge with next extent! */ | |
1422 | if (ext4_ext_is_uninitialized(ex)) | |
1423 | uninitialized = 1; | |
1424 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) | |
1425 | + ext4_ext_get_actual_len(ex + 1)); | |
1426 | if (uninitialized) | |
1427 | ext4_ext_mark_uninitialized(ex); | |
1428 | ||
1429 | if (ex + 1 < EXT_LAST_EXTENT(eh)) { | |
1430 | len = (EXT_LAST_EXTENT(eh) - ex - 1) | |
1431 | * sizeof(struct ext4_extent); | |
1432 | memmove(ex + 1, ex + 2, len); | |
1433 | } | |
e8546d06 | 1434 | le16_add_cpu(&eh->eh_entries, -1); |
56055d3a AA |
1435 | merge_done = 1; |
1436 | WARN_ON(eh->eh_entries == 0); | |
1437 | if (!eh->eh_entries) | |
1438 | ext4_error(inode->i_sb, "ext4_ext_try_to_merge", | |
1439 | "inode#%lu, eh->eh_entries = 0!", inode->i_ino); | |
1440 | } | |
1441 | ||
1442 | return merge_done; | |
1443 | } | |
1444 | ||
25d14f98 AA |
1445 | /* |
1446 | * check if a portion of the "newext" extent overlaps with an | |
1447 | * existing extent. | |
1448 | * | |
1449 | * If there is an overlap discovered, it updates the length of the newext | |
1450 | * such that there will be no overlap, and then returns 1. | |
1451 | * If there is no overlap found, it returns 0. | |
1452 | */ | |
1453 | unsigned int ext4_ext_check_overlap(struct inode *inode, | |
1454 | struct ext4_extent *newext, | |
1455 | struct ext4_ext_path *path) | |
1456 | { | |
725d26d3 | 1457 | ext4_lblk_t b1, b2; |
25d14f98 AA |
1458 | unsigned int depth, len1; |
1459 | unsigned int ret = 0; | |
1460 | ||
1461 | b1 = le32_to_cpu(newext->ee_block); | |
a2df2a63 | 1462 | len1 = ext4_ext_get_actual_len(newext); |
25d14f98 AA |
1463 | depth = ext_depth(inode); |
1464 | if (!path[depth].p_ext) | |
1465 | goto out; | |
1466 | b2 = le32_to_cpu(path[depth].p_ext->ee_block); | |
1467 | ||
1468 | /* | |
1469 | * get the next allocated block if the extent in the path | |
2b2d6d01 | 1470 | * is before the requested block(s) |
25d14f98 AA |
1471 | */ |
1472 | if (b2 < b1) { | |
1473 | b2 = ext4_ext_next_allocated_block(path); | |
1474 | if (b2 == EXT_MAX_BLOCK) | |
1475 | goto out; | |
1476 | } | |
1477 | ||
725d26d3 | 1478 | /* check for wrap through zero on extent logical start block*/ |
25d14f98 AA |
1479 | if (b1 + len1 < b1) { |
1480 | len1 = EXT_MAX_BLOCK - b1; | |
1481 | newext->ee_len = cpu_to_le16(len1); | |
1482 | ret = 1; | |
1483 | } | |
1484 | ||
1485 | /* check for overlap */ | |
1486 | if (b1 + len1 > b2) { | |
1487 | newext->ee_len = cpu_to_le16(b2 - b1); | |
1488 | ret = 1; | |
1489 | } | |
1490 | out: | |
1491 | return ret; | |
1492 | } | |
1493 | ||
a86c6181 | 1494 | /* |
d0d856e8 RD |
1495 | * ext4_ext_insert_extent: |
1496 | * tries to merge requsted extent into the existing extent or | |
1497 | * inserts requested extent as new one into the tree, | |
1498 | * creating new leaf in the no-space case. | |
a86c6181 AT |
1499 | */ |
1500 | int ext4_ext_insert_extent(handle_t *handle, struct inode *inode, | |
1501 | struct ext4_ext_path *path, | |
1502 | struct ext4_extent *newext) | |
1503 | { | |
af5bc92d | 1504 | struct ext4_extent_header *eh; |
a86c6181 AT |
1505 | struct ext4_extent *ex, *fex; |
1506 | struct ext4_extent *nearex; /* nearest extent */ | |
1507 | struct ext4_ext_path *npath = NULL; | |
725d26d3 AK |
1508 | int depth, len, err; |
1509 | ext4_lblk_t next; | |
a2df2a63 | 1510 | unsigned uninitialized = 0; |
a86c6181 | 1511 | |
a2df2a63 | 1512 | BUG_ON(ext4_ext_get_actual_len(newext) == 0); |
a86c6181 AT |
1513 | depth = ext_depth(inode); |
1514 | ex = path[depth].p_ext; | |
1515 | BUG_ON(path[depth].p_hdr == NULL); | |
1516 | ||
1517 | /* try to insert block into found extent and return */ | |
1518 | if (ex && ext4_can_extents_be_merged(inode, ex, newext)) { | |
2ae02107 | 1519 | ext_debug("append %d block to %d:%d (from %llu)\n", |
a2df2a63 | 1520 | ext4_ext_get_actual_len(newext), |
a86c6181 | 1521 | le32_to_cpu(ex->ee_block), |
a2df2a63 | 1522 | ext4_ext_get_actual_len(ex), ext_pblock(ex)); |
7e028976 AM |
1523 | err = ext4_ext_get_access(handle, inode, path + depth); |
1524 | if (err) | |
a86c6181 | 1525 | return err; |
a2df2a63 AA |
1526 | |
1527 | /* | |
1528 | * ext4_can_extents_be_merged should have checked that either | |
1529 | * both extents are uninitialized, or both aren't. Thus we | |
1530 | * need to check only one of them here. | |
1531 | */ | |
1532 | if (ext4_ext_is_uninitialized(ex)) | |
1533 | uninitialized = 1; | |
1534 | ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) | |
1535 | + ext4_ext_get_actual_len(newext)); | |
1536 | if (uninitialized) | |
1537 | ext4_ext_mark_uninitialized(ex); | |
a86c6181 AT |
1538 | eh = path[depth].p_hdr; |
1539 | nearex = ex; | |
1540 | goto merge; | |
1541 | } | |
1542 | ||
1543 | repeat: | |
1544 | depth = ext_depth(inode); | |
1545 | eh = path[depth].p_hdr; | |
1546 | if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) | |
1547 | goto has_space; | |
1548 | ||
1549 | /* probably next leaf has space for us? */ | |
1550 | fex = EXT_LAST_EXTENT(eh); | |
1551 | next = ext4_ext_next_leaf_block(inode, path); | |
1552 | if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block) | |
1553 | && next != EXT_MAX_BLOCK) { | |
1554 | ext_debug("next leaf block - %d\n", next); | |
1555 | BUG_ON(npath != NULL); | |
1556 | npath = ext4_ext_find_extent(inode, next, NULL); | |
1557 | if (IS_ERR(npath)) | |
1558 | return PTR_ERR(npath); | |
1559 | BUG_ON(npath->p_depth != path->p_depth); | |
1560 | eh = npath[depth].p_hdr; | |
1561 | if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) { | |
1562 | ext_debug("next leaf isnt full(%d)\n", | |
1563 | le16_to_cpu(eh->eh_entries)); | |
1564 | path = npath; | |
1565 | goto repeat; | |
1566 | } | |
1567 | ext_debug("next leaf has no free space(%d,%d)\n", | |
1568 | le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); | |
1569 | } | |
1570 | ||
1571 | /* | |
d0d856e8 RD |
1572 | * There is no free space in the found leaf. |
1573 | * We're gonna add a new leaf in the tree. | |
a86c6181 AT |
1574 | */ |
1575 | err = ext4_ext_create_new_leaf(handle, inode, path, newext); | |
1576 | if (err) | |
1577 | goto cleanup; | |
1578 | depth = ext_depth(inode); | |
1579 | eh = path[depth].p_hdr; | |
1580 | ||
1581 | has_space: | |
1582 | nearex = path[depth].p_ext; | |
1583 | ||
7e028976 AM |
1584 | err = ext4_ext_get_access(handle, inode, path + depth); |
1585 | if (err) | |
a86c6181 AT |
1586 | goto cleanup; |
1587 | ||
1588 | if (!nearex) { | |
1589 | /* there is no extent in this leaf, create first one */ | |
2ae02107 | 1590 | ext_debug("first extent in the leaf: %d:%llu:%d\n", |
8c55e204 DK |
1591 | le32_to_cpu(newext->ee_block), |
1592 | ext_pblock(newext), | |
a2df2a63 | 1593 | ext4_ext_get_actual_len(newext)); |
a86c6181 AT |
1594 | path[depth].p_ext = EXT_FIRST_EXTENT(eh); |
1595 | } else if (le32_to_cpu(newext->ee_block) | |
8c55e204 | 1596 | > le32_to_cpu(nearex->ee_block)) { |
a86c6181 AT |
1597 | /* BUG_ON(newext->ee_block == nearex->ee_block); */ |
1598 | if (nearex != EXT_LAST_EXTENT(eh)) { | |
1599 | len = EXT_MAX_EXTENT(eh) - nearex; | |
1600 | len = (len - 1) * sizeof(struct ext4_extent); | |
1601 | len = len < 0 ? 0 : len; | |
2ae02107 | 1602 | ext_debug("insert %d:%llu:%d after: nearest 0x%p, " |
a86c6181 | 1603 | "move %d from 0x%p to 0x%p\n", |
8c55e204 DK |
1604 | le32_to_cpu(newext->ee_block), |
1605 | ext_pblock(newext), | |
a2df2a63 | 1606 | ext4_ext_get_actual_len(newext), |
a86c6181 AT |
1607 | nearex, len, nearex + 1, nearex + 2); |
1608 | memmove(nearex + 2, nearex + 1, len); | |
1609 | } | |
1610 | path[depth].p_ext = nearex + 1; | |
1611 | } else { | |
1612 | BUG_ON(newext->ee_block == nearex->ee_block); | |
1613 | len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent); | |
1614 | len = len < 0 ? 0 : len; | |
2ae02107 | 1615 | ext_debug("insert %d:%llu:%d before: nearest 0x%p, " |
a86c6181 AT |
1616 | "move %d from 0x%p to 0x%p\n", |
1617 | le32_to_cpu(newext->ee_block), | |
f65e6fba | 1618 | ext_pblock(newext), |
a2df2a63 | 1619 | ext4_ext_get_actual_len(newext), |
a86c6181 AT |
1620 | nearex, len, nearex + 1, nearex + 2); |
1621 | memmove(nearex + 1, nearex, len); | |
1622 | path[depth].p_ext = nearex; | |
1623 | } | |
1624 | ||
e8546d06 | 1625 | le16_add_cpu(&eh->eh_entries, 1); |
a86c6181 AT |
1626 | nearex = path[depth].p_ext; |
1627 | nearex->ee_block = newext->ee_block; | |
b377611d | 1628 | ext4_ext_store_pblock(nearex, ext_pblock(newext)); |
a86c6181 | 1629 | nearex->ee_len = newext->ee_len; |
a86c6181 AT |
1630 | |
1631 | merge: | |
1632 | /* try to merge extents to the right */ | |
56055d3a | 1633 | ext4_ext_try_to_merge(inode, path, nearex); |
a86c6181 AT |
1634 | |
1635 | /* try to merge extents to the left */ | |
1636 | ||
1637 | /* time to correct all indexes above */ | |
1638 | err = ext4_ext_correct_indexes(handle, inode, path); | |
1639 | if (err) | |
1640 | goto cleanup; | |
1641 | ||
1642 | err = ext4_ext_dirty(handle, inode, path + depth); | |
1643 | ||
1644 | cleanup: | |
1645 | if (npath) { | |
1646 | ext4_ext_drop_refs(npath); | |
1647 | kfree(npath); | |
1648 | } | |
a86c6181 AT |
1649 | ext4_ext_invalidate_cache(inode); |
1650 | return err; | |
1651 | } | |
1652 | ||
6873fa0d ES |
1653 | int ext4_ext_walk_space(struct inode *inode, ext4_lblk_t block, |
1654 | ext4_lblk_t num, ext_prepare_callback func, | |
1655 | void *cbdata) | |
1656 | { | |
1657 | struct ext4_ext_path *path = NULL; | |
1658 | struct ext4_ext_cache cbex; | |
1659 | struct ext4_extent *ex; | |
1660 | ext4_lblk_t next, start = 0, end = 0; | |
1661 | ext4_lblk_t last = block + num; | |
1662 | int depth, exists, err = 0; | |
1663 | ||
1664 | BUG_ON(func == NULL); | |
1665 | BUG_ON(inode == NULL); | |
1666 | ||
1667 | while (block < last && block != EXT_MAX_BLOCK) { | |
1668 | num = last - block; | |
1669 | /* find extent for this block */ | |
1670 | path = ext4_ext_find_extent(inode, block, path); | |
1671 | if (IS_ERR(path)) { | |
1672 | err = PTR_ERR(path); | |
1673 | path = NULL; | |
1674 | break; | |
1675 | } | |
1676 | ||
1677 | depth = ext_depth(inode); | |
1678 | BUG_ON(path[depth].p_hdr == NULL); | |
1679 | ex = path[depth].p_ext; | |
1680 | next = ext4_ext_next_allocated_block(path); | |
1681 | ||
1682 | exists = 0; | |
1683 | if (!ex) { | |
1684 | /* there is no extent yet, so try to allocate | |
1685 | * all requested space */ | |
1686 | start = block; | |
1687 | end = block + num; | |
1688 | } else if (le32_to_cpu(ex->ee_block) > block) { | |
1689 | /* need to allocate space before found extent */ | |
1690 | start = block; | |
1691 | end = le32_to_cpu(ex->ee_block); | |
1692 | if (block + num < end) | |
1693 | end = block + num; | |
1694 | } else if (block >= le32_to_cpu(ex->ee_block) | |
1695 | + ext4_ext_get_actual_len(ex)) { | |
1696 | /* need to allocate space after found extent */ | |
1697 | start = block; | |
1698 | end = block + num; | |
1699 | if (end >= next) | |
1700 | end = next; | |
1701 | } else if (block >= le32_to_cpu(ex->ee_block)) { | |
1702 | /* | |
1703 | * some part of requested space is covered | |
1704 | * by found extent | |
1705 | */ | |
1706 | start = block; | |
1707 | end = le32_to_cpu(ex->ee_block) | |
1708 | + ext4_ext_get_actual_len(ex); | |
1709 | if (block + num < end) | |
1710 | end = block + num; | |
1711 | exists = 1; | |
1712 | } else { | |
1713 | BUG(); | |
1714 | } | |
1715 | BUG_ON(end <= start); | |
1716 | ||
1717 | if (!exists) { | |
1718 | cbex.ec_block = start; | |
1719 | cbex.ec_len = end - start; | |
1720 | cbex.ec_start = 0; | |
1721 | cbex.ec_type = EXT4_EXT_CACHE_GAP; | |
1722 | } else { | |
1723 | cbex.ec_block = le32_to_cpu(ex->ee_block); | |
1724 | cbex.ec_len = ext4_ext_get_actual_len(ex); | |
1725 | cbex.ec_start = ext_pblock(ex); | |
1726 | cbex.ec_type = EXT4_EXT_CACHE_EXTENT; | |
1727 | } | |
1728 | ||
1729 | BUG_ON(cbex.ec_len == 0); | |
1730 | err = func(inode, path, &cbex, ex, cbdata); | |
1731 | ext4_ext_drop_refs(path); | |
1732 | ||
1733 | if (err < 0) | |
1734 | break; | |
1735 | ||
1736 | if (err == EXT_REPEAT) | |
1737 | continue; | |
1738 | else if (err == EXT_BREAK) { | |
1739 | err = 0; | |
1740 | break; | |
1741 | } | |
1742 | ||
1743 | if (ext_depth(inode) != depth) { | |
1744 | /* depth was changed. we have to realloc path */ | |
1745 | kfree(path); | |
1746 | path = NULL; | |
1747 | } | |
1748 | ||
1749 | block = cbex.ec_block + cbex.ec_len; | |
1750 | } | |
1751 | ||
1752 | if (path) { | |
1753 | ext4_ext_drop_refs(path); | |
1754 | kfree(path); | |
1755 | } | |
1756 | ||
1757 | return err; | |
1758 | } | |
1759 | ||
09b88252 | 1760 | static void |
725d26d3 | 1761 | ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block, |
dd54567a | 1762 | __u32 len, ext4_fsblk_t start, int type) |
a86c6181 AT |
1763 | { |
1764 | struct ext4_ext_cache *cex; | |
1765 | BUG_ON(len == 0); | |
1766 | cex = &EXT4_I(inode)->i_cached_extent; | |
1767 | cex->ec_type = type; | |
1768 | cex->ec_block = block; | |
1769 | cex->ec_len = len; | |
1770 | cex->ec_start = start; | |
1771 | } | |
1772 | ||
1773 | /* | |
d0d856e8 RD |
1774 | * ext4_ext_put_gap_in_cache: |
1775 | * calculate boundaries of the gap that the requested block fits into | |
a86c6181 AT |
1776 | * and cache this gap |
1777 | */ | |
09b88252 | 1778 | static void |
a86c6181 | 1779 | ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path, |
725d26d3 | 1780 | ext4_lblk_t block) |
a86c6181 AT |
1781 | { |
1782 | int depth = ext_depth(inode); | |
725d26d3 AK |
1783 | unsigned long len; |
1784 | ext4_lblk_t lblock; | |
a86c6181 AT |
1785 | struct ext4_extent *ex; |
1786 | ||
1787 | ex = path[depth].p_ext; | |
1788 | if (ex == NULL) { | |
1789 | /* there is no extent yet, so gap is [0;-] */ | |
1790 | lblock = 0; | |
1791 | len = EXT_MAX_BLOCK; | |
1792 | ext_debug("cache gap(whole file):"); | |
1793 | } else if (block < le32_to_cpu(ex->ee_block)) { | |
1794 | lblock = block; | |
1795 | len = le32_to_cpu(ex->ee_block) - block; | |
bba90743 ES |
1796 | ext_debug("cache gap(before): %u [%u:%u]", |
1797 | block, | |
1798 | le32_to_cpu(ex->ee_block), | |
1799 | ext4_ext_get_actual_len(ex)); | |
a86c6181 | 1800 | } else if (block >= le32_to_cpu(ex->ee_block) |
a2df2a63 | 1801 | + ext4_ext_get_actual_len(ex)) { |
725d26d3 | 1802 | ext4_lblk_t next; |
8c55e204 | 1803 | lblock = le32_to_cpu(ex->ee_block) |
a2df2a63 | 1804 | + ext4_ext_get_actual_len(ex); |
725d26d3 AK |
1805 | |
1806 | next = ext4_ext_next_allocated_block(path); | |
bba90743 ES |
1807 | ext_debug("cache gap(after): [%u:%u] %u", |
1808 | le32_to_cpu(ex->ee_block), | |
1809 | ext4_ext_get_actual_len(ex), | |
1810 | block); | |
725d26d3 AK |
1811 | BUG_ON(next == lblock); |
1812 | len = next - lblock; | |
a86c6181 AT |
1813 | } else { |
1814 | lblock = len = 0; | |
1815 | BUG(); | |
1816 | } | |
1817 | ||
bba90743 | 1818 | ext_debug(" -> %u:%lu\n", lblock, len); |
a86c6181 AT |
1819 | ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP); |
1820 | } | |
1821 | ||
09b88252 | 1822 | static int |
725d26d3 | 1823 | ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block, |
a86c6181 AT |
1824 | struct ext4_extent *ex) |
1825 | { | |
1826 | struct ext4_ext_cache *cex; | |
1827 | ||
1828 | cex = &EXT4_I(inode)->i_cached_extent; | |
1829 | ||
1830 | /* has cache valid data? */ | |
1831 | if (cex->ec_type == EXT4_EXT_CACHE_NO) | |
1832 | return EXT4_EXT_CACHE_NO; | |
1833 | ||
1834 | BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP && | |
1835 | cex->ec_type != EXT4_EXT_CACHE_EXTENT); | |
1836 | if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) { | |
8c55e204 | 1837 | ex->ee_block = cpu_to_le32(cex->ec_block); |
f65e6fba | 1838 | ext4_ext_store_pblock(ex, cex->ec_start); |
8c55e204 | 1839 | ex->ee_len = cpu_to_le16(cex->ec_len); |
bba90743 ES |
1840 | ext_debug("%u cached by %u:%u:%llu\n", |
1841 | block, | |
1842 | cex->ec_block, cex->ec_len, cex->ec_start); | |
a86c6181 AT |
1843 | return cex->ec_type; |
1844 | } | |
1845 | ||
1846 | /* not in cache */ | |
1847 | return EXT4_EXT_CACHE_NO; | |
1848 | } | |
1849 | ||
1850 | /* | |
d0d856e8 RD |
1851 | * ext4_ext_rm_idx: |
1852 | * removes index from the index block. | |
1853 | * It's used in truncate case only, thus all requests are for | |
1854 | * last index in the block only. | |
a86c6181 | 1855 | */ |
1d03ec98 | 1856 | static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, |
a86c6181 AT |
1857 | struct ext4_ext_path *path) |
1858 | { | |
1859 | struct buffer_head *bh; | |
1860 | int err; | |
f65e6fba | 1861 | ext4_fsblk_t leaf; |
a86c6181 AT |
1862 | |
1863 | /* free index block */ | |
1864 | path--; | |
f65e6fba | 1865 | leaf = idx_pblock(path->p_idx); |
a86c6181 | 1866 | BUG_ON(path->p_hdr->eh_entries == 0); |
7e028976 AM |
1867 | err = ext4_ext_get_access(handle, inode, path); |
1868 | if (err) | |
a86c6181 | 1869 | return err; |
e8546d06 | 1870 | le16_add_cpu(&path->p_hdr->eh_entries, -1); |
7e028976 AM |
1871 | err = ext4_ext_dirty(handle, inode, path); |
1872 | if (err) | |
a86c6181 | 1873 | return err; |
2ae02107 | 1874 | ext_debug("index is empty, remove it, free block %llu\n", leaf); |
a86c6181 AT |
1875 | bh = sb_find_get_block(inode->i_sb, leaf); |
1876 | ext4_forget(handle, 1, inode, bh, leaf); | |
c9de560d | 1877 | ext4_free_blocks(handle, inode, leaf, 1, 1); |
a86c6181 AT |
1878 | return err; |
1879 | } | |
1880 | ||
1881 | /* | |
ee12b630 MC |
1882 | * ext4_ext_calc_credits_for_single_extent: |
1883 | * This routine returns max. credits that needed to insert an extent | |
1884 | * to the extent tree. | |
1885 | * When pass the actual path, the caller should calculate credits | |
1886 | * under i_data_sem. | |
a86c6181 | 1887 | */ |
525f4ed8 | 1888 | int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks, |
a86c6181 AT |
1889 | struct ext4_ext_path *path) |
1890 | { | |
a86c6181 | 1891 | if (path) { |
ee12b630 | 1892 | int depth = ext_depth(inode); |
f3bd1f3f | 1893 | int ret = 0; |
ee12b630 | 1894 | |
a86c6181 | 1895 | /* probably there is space in leaf? */ |
a86c6181 | 1896 | if (le16_to_cpu(path[depth].p_hdr->eh_entries) |
ee12b630 | 1897 | < le16_to_cpu(path[depth].p_hdr->eh_max)) { |
a86c6181 | 1898 | |
ee12b630 MC |
1899 | /* |
1900 | * There are some space in the leaf tree, no | |
1901 | * need to account for leaf block credit | |
1902 | * | |
1903 | * bitmaps and block group descriptor blocks | |
1904 | * and other metadat blocks still need to be | |
1905 | * accounted. | |
1906 | */ | |
525f4ed8 | 1907 | /* 1 bitmap, 1 block group descriptor */ |
ee12b630 MC |
1908 | ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb); |
1909 | } | |
1910 | } | |
a86c6181 | 1911 | |
525f4ed8 | 1912 | return ext4_chunk_trans_blocks(inode, nrblocks); |
ee12b630 | 1913 | } |
a86c6181 | 1914 | |
ee12b630 MC |
1915 | /* |
1916 | * How many index/leaf blocks need to change/allocate to modify nrblocks? | |
1917 | * | |
1918 | * if nrblocks are fit in a single extent (chunk flag is 1), then | |
1919 | * in the worse case, each tree level index/leaf need to be changed | |
1920 | * if the tree split due to insert a new extent, then the old tree | |
1921 | * index/leaf need to be updated too | |
1922 | * | |
1923 | * If the nrblocks are discontiguous, they could cause | |
1924 | * the whole tree split more than once, but this is really rare. | |
1925 | */ | |
525f4ed8 | 1926 | int ext4_ext_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) |
ee12b630 MC |
1927 | { |
1928 | int index; | |
1929 | int depth = ext_depth(inode); | |
a86c6181 | 1930 | |
ee12b630 MC |
1931 | if (chunk) |
1932 | index = depth * 2; | |
1933 | else | |
1934 | index = depth * 3; | |
a86c6181 | 1935 | |
ee12b630 | 1936 | return index; |
a86c6181 AT |
1937 | } |
1938 | ||
1939 | static int ext4_remove_blocks(handle_t *handle, struct inode *inode, | |
1940 | struct ext4_extent *ex, | |
725d26d3 | 1941 | ext4_lblk_t from, ext4_lblk_t to) |
a86c6181 AT |
1942 | { |
1943 | struct buffer_head *bh; | |
a2df2a63 | 1944 | unsigned short ee_len = ext4_ext_get_actual_len(ex); |
c9de560d | 1945 | int i, metadata = 0; |
a86c6181 | 1946 | |
c9de560d AT |
1947 | if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) |
1948 | metadata = 1; | |
a86c6181 AT |
1949 | #ifdef EXTENTS_STATS |
1950 | { | |
1951 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
a86c6181 AT |
1952 | spin_lock(&sbi->s_ext_stats_lock); |
1953 | sbi->s_ext_blocks += ee_len; | |
1954 | sbi->s_ext_extents++; | |
1955 | if (ee_len < sbi->s_ext_min) | |
1956 | sbi->s_ext_min = ee_len; | |
1957 | if (ee_len > sbi->s_ext_max) | |
1958 | sbi->s_ext_max = ee_len; | |
1959 | if (ext_depth(inode) > sbi->s_depth_max) | |
1960 | sbi->s_depth_max = ext_depth(inode); | |
1961 | spin_unlock(&sbi->s_ext_stats_lock); | |
1962 | } | |
1963 | #endif | |
1964 | if (from >= le32_to_cpu(ex->ee_block) | |
a2df2a63 | 1965 | && to == le32_to_cpu(ex->ee_block) + ee_len - 1) { |
a86c6181 | 1966 | /* tail removal */ |
725d26d3 | 1967 | ext4_lblk_t num; |
f65e6fba | 1968 | ext4_fsblk_t start; |
725d26d3 | 1969 | |
a2df2a63 AA |
1970 | num = le32_to_cpu(ex->ee_block) + ee_len - from; |
1971 | start = ext_pblock(ex) + ee_len - num; | |
725d26d3 | 1972 | ext_debug("free last %u blocks starting %llu\n", num, start); |
a86c6181 AT |
1973 | for (i = 0; i < num; i++) { |
1974 | bh = sb_find_get_block(inode->i_sb, start + i); | |
1975 | ext4_forget(handle, 0, inode, bh, start + i); | |
1976 | } | |
c9de560d | 1977 | ext4_free_blocks(handle, inode, start, num, metadata); |
a86c6181 | 1978 | } else if (from == le32_to_cpu(ex->ee_block) |
a2df2a63 | 1979 | && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) { |
725d26d3 | 1980 | printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n", |
a2df2a63 | 1981 | from, to, le32_to_cpu(ex->ee_block), ee_len); |
a86c6181 | 1982 | } else { |
725d26d3 AK |
1983 | printk(KERN_INFO "strange request: removal(2) " |
1984 | "%u-%u from %u:%u\n", | |
1985 | from, to, le32_to_cpu(ex->ee_block), ee_len); | |
a86c6181 AT |
1986 | } |
1987 | return 0; | |
1988 | } | |
1989 | ||
1990 | static int | |
1991 | ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, | |
725d26d3 | 1992 | struct ext4_ext_path *path, ext4_lblk_t start) |
a86c6181 AT |
1993 | { |
1994 | int err = 0, correct_index = 0; | |
1995 | int depth = ext_depth(inode), credits; | |
1996 | struct ext4_extent_header *eh; | |
725d26d3 AK |
1997 | ext4_lblk_t a, b, block; |
1998 | unsigned num; | |
1999 | ext4_lblk_t ex_ee_block; | |
a86c6181 | 2000 | unsigned short ex_ee_len; |
a2df2a63 | 2001 | unsigned uninitialized = 0; |
a86c6181 AT |
2002 | struct ext4_extent *ex; |
2003 | ||
c29c0ae7 | 2004 | /* the header must be checked already in ext4_ext_remove_space() */ |
725d26d3 | 2005 | ext_debug("truncate since %u in leaf\n", start); |
a86c6181 AT |
2006 | if (!path[depth].p_hdr) |
2007 | path[depth].p_hdr = ext_block_hdr(path[depth].p_bh); | |
2008 | eh = path[depth].p_hdr; | |
2009 | BUG_ON(eh == NULL); | |
a86c6181 AT |
2010 | |
2011 | /* find where to start removing */ | |
2012 | ex = EXT_LAST_EXTENT(eh); | |
2013 | ||
2014 | ex_ee_block = le32_to_cpu(ex->ee_block); | |
a2df2a63 AA |
2015 | if (ext4_ext_is_uninitialized(ex)) |
2016 | uninitialized = 1; | |
2017 | ex_ee_len = ext4_ext_get_actual_len(ex); | |
a86c6181 AT |
2018 | |
2019 | while (ex >= EXT_FIRST_EXTENT(eh) && | |
2020 | ex_ee_block + ex_ee_len > start) { | |
2021 | ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len); | |
2022 | path[depth].p_ext = ex; | |
2023 | ||
2024 | a = ex_ee_block > start ? ex_ee_block : start; | |
2025 | b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ? | |
2026 | ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK; | |
2027 | ||
2028 | ext_debug(" border %u:%u\n", a, b); | |
2029 | ||
2030 | if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) { | |
2031 | block = 0; | |
2032 | num = 0; | |
2033 | BUG(); | |
2034 | } else if (a != ex_ee_block) { | |
2035 | /* remove tail of the extent */ | |
2036 | block = ex_ee_block; | |
2037 | num = a - block; | |
2038 | } else if (b != ex_ee_block + ex_ee_len - 1) { | |
2039 | /* remove head of the extent */ | |
2040 | block = a; | |
2041 | num = b - a; | |
2042 | /* there is no "make a hole" API yet */ | |
2043 | BUG(); | |
2044 | } else { | |
2045 | /* remove whole extent: excellent! */ | |
2046 | block = ex_ee_block; | |
2047 | num = 0; | |
2048 | BUG_ON(a != ex_ee_block); | |
2049 | BUG_ON(b != ex_ee_block + ex_ee_len - 1); | |
2050 | } | |
2051 | ||
34071da7 TT |
2052 | /* |
2053 | * 3 for leaf, sb, and inode plus 2 (bmap and group | |
2054 | * descriptor) for each block group; assume two block | |
2055 | * groups plus ex_ee_len/blocks_per_block_group for | |
2056 | * the worst case | |
2057 | */ | |
2058 | credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb)); | |
a86c6181 AT |
2059 | if (ex == EXT_FIRST_EXTENT(eh)) { |
2060 | correct_index = 1; | |
2061 | credits += (ext_depth(inode)) + 1; | |
2062 | } | |
a86c6181 | 2063 | credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb); |
a86c6181 | 2064 | |
9102e4fa SF |
2065 | err = ext4_ext_journal_restart(handle, credits); |
2066 | if (err) | |
a86c6181 | 2067 | goto out; |
a86c6181 AT |
2068 | |
2069 | err = ext4_ext_get_access(handle, inode, path + depth); | |
2070 | if (err) | |
2071 | goto out; | |
2072 | ||
2073 | err = ext4_remove_blocks(handle, inode, ex, a, b); | |
2074 | if (err) | |
2075 | goto out; | |
2076 | ||
2077 | if (num == 0) { | |
d0d856e8 | 2078 | /* this extent is removed; mark slot entirely unused */ |
f65e6fba | 2079 | ext4_ext_store_pblock(ex, 0); |
e8546d06 | 2080 | le16_add_cpu(&eh->eh_entries, -1); |
a86c6181 AT |
2081 | } |
2082 | ||
2083 | ex->ee_block = cpu_to_le32(block); | |
2084 | ex->ee_len = cpu_to_le16(num); | |
749269fa AA |
2085 | /* |
2086 | * Do not mark uninitialized if all the blocks in the | |
2087 | * extent have been removed. | |
2088 | */ | |
2089 | if (uninitialized && num) | |
a2df2a63 | 2090 | ext4_ext_mark_uninitialized(ex); |
a86c6181 AT |
2091 | |
2092 | err = ext4_ext_dirty(handle, inode, path + depth); | |
2093 | if (err) | |
2094 | goto out; | |
2095 | ||
2ae02107 | 2096 | ext_debug("new extent: %u:%u:%llu\n", block, num, |
f65e6fba | 2097 | ext_pblock(ex)); |
a86c6181 AT |
2098 | ex--; |
2099 | ex_ee_block = le32_to_cpu(ex->ee_block); | |
a2df2a63 | 2100 | ex_ee_len = ext4_ext_get_actual_len(ex); |
a86c6181 AT |
2101 | } |
2102 | ||
2103 | if (correct_index && eh->eh_entries) | |
2104 | err = ext4_ext_correct_indexes(handle, inode, path); | |
2105 | ||
2106 | /* if this leaf is free, then we should | |
2107 | * remove it from index block above */ | |
2108 | if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL) | |
2109 | err = ext4_ext_rm_idx(handle, inode, path + depth); | |
2110 | ||
2111 | out: | |
2112 | return err; | |
2113 | } | |
2114 | ||
2115 | /* | |
d0d856e8 RD |
2116 | * ext4_ext_more_to_rm: |
2117 | * returns 1 if current index has to be freed (even partial) | |
a86c6181 | 2118 | */ |
09b88252 | 2119 | static int |
a86c6181 AT |
2120 | ext4_ext_more_to_rm(struct ext4_ext_path *path) |
2121 | { | |
2122 | BUG_ON(path->p_idx == NULL); | |
2123 | ||
2124 | if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr)) | |
2125 | return 0; | |
2126 | ||
2127 | /* | |
d0d856e8 | 2128 | * if truncate on deeper level happened, it wasn't partial, |
a86c6181 AT |
2129 | * so we have to consider current index for truncation |
2130 | */ | |
2131 | if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block) | |
2132 | return 0; | |
2133 | return 1; | |
2134 | } | |
2135 | ||
1d03ec98 | 2136 | static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start) |
a86c6181 AT |
2137 | { |
2138 | struct super_block *sb = inode->i_sb; | |
2139 | int depth = ext_depth(inode); | |
2140 | struct ext4_ext_path *path; | |
2141 | handle_t *handle; | |
2142 | int i = 0, err = 0; | |
2143 | ||
725d26d3 | 2144 | ext_debug("truncate since %u\n", start); |
a86c6181 AT |
2145 | |
2146 | /* probably first extent we're gonna free will be last in block */ | |
2147 | handle = ext4_journal_start(inode, depth + 1); | |
2148 | if (IS_ERR(handle)) | |
2149 | return PTR_ERR(handle); | |
2150 | ||
2151 | ext4_ext_invalidate_cache(inode); | |
2152 | ||
2153 | /* | |
d0d856e8 RD |
2154 | * We start scanning from right side, freeing all the blocks |
2155 | * after i_size and walking into the tree depth-wise. | |
a86c6181 | 2156 | */ |
216553c4 | 2157 | path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_NOFS); |
a86c6181 AT |
2158 | if (path == NULL) { |
2159 | ext4_journal_stop(handle); | |
2160 | return -ENOMEM; | |
2161 | } | |
a86c6181 | 2162 | path[0].p_hdr = ext_inode_hdr(inode); |
c29c0ae7 | 2163 | if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) { |
a86c6181 AT |
2164 | err = -EIO; |
2165 | goto out; | |
2166 | } | |
2167 | path[0].p_depth = depth; | |
2168 | ||
2169 | while (i >= 0 && err == 0) { | |
2170 | if (i == depth) { | |
2171 | /* this is leaf block */ | |
2172 | err = ext4_ext_rm_leaf(handle, inode, path, start); | |
d0d856e8 | 2173 | /* root level has p_bh == NULL, brelse() eats this */ |
a86c6181 AT |
2174 | brelse(path[i].p_bh); |
2175 | path[i].p_bh = NULL; | |
2176 | i--; | |
2177 | continue; | |
2178 | } | |
2179 | ||
2180 | /* this is index block */ | |
2181 | if (!path[i].p_hdr) { | |
2182 | ext_debug("initialize header\n"); | |
2183 | path[i].p_hdr = ext_block_hdr(path[i].p_bh); | |
a86c6181 AT |
2184 | } |
2185 | ||
a86c6181 | 2186 | if (!path[i].p_idx) { |
d0d856e8 | 2187 | /* this level hasn't been touched yet */ |
a86c6181 AT |
2188 | path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr); |
2189 | path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1; | |
2190 | ext_debug("init index ptr: hdr 0x%p, num %d\n", | |
2191 | path[i].p_hdr, | |
2192 | le16_to_cpu(path[i].p_hdr->eh_entries)); | |
2193 | } else { | |
d0d856e8 | 2194 | /* we were already here, see at next index */ |
a86c6181 AT |
2195 | path[i].p_idx--; |
2196 | } | |
2197 | ||
2198 | ext_debug("level %d - index, first 0x%p, cur 0x%p\n", | |
2199 | i, EXT_FIRST_INDEX(path[i].p_hdr), | |
2200 | path[i].p_idx); | |
2201 | if (ext4_ext_more_to_rm(path + i)) { | |
c29c0ae7 | 2202 | struct buffer_head *bh; |
a86c6181 | 2203 | /* go to the next level */ |
2ae02107 | 2204 | ext_debug("move to level %d (block %llu)\n", |
f65e6fba | 2205 | i + 1, idx_pblock(path[i].p_idx)); |
a86c6181 | 2206 | memset(path + i + 1, 0, sizeof(*path)); |
c29c0ae7 AT |
2207 | bh = sb_bread(sb, idx_pblock(path[i].p_idx)); |
2208 | if (!bh) { | |
a86c6181 AT |
2209 | /* should we reset i_size? */ |
2210 | err = -EIO; | |
2211 | break; | |
2212 | } | |
c29c0ae7 AT |
2213 | if (WARN_ON(i + 1 > depth)) { |
2214 | err = -EIO; | |
2215 | break; | |
2216 | } | |
2217 | if (ext4_ext_check_header(inode, ext_block_hdr(bh), | |
2218 | depth - i - 1)) { | |
2219 | err = -EIO; | |
2220 | break; | |
2221 | } | |
2222 | path[i + 1].p_bh = bh; | |
a86c6181 | 2223 | |
d0d856e8 RD |
2224 | /* save actual number of indexes since this |
2225 | * number is changed at the next iteration */ | |
a86c6181 AT |
2226 | path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries); |
2227 | i++; | |
2228 | } else { | |
d0d856e8 | 2229 | /* we finished processing this index, go up */ |
a86c6181 | 2230 | if (path[i].p_hdr->eh_entries == 0 && i > 0) { |
d0d856e8 | 2231 | /* index is empty, remove it; |
a86c6181 AT |
2232 | * handle must be already prepared by the |
2233 | * truncatei_leaf() */ | |
2234 | err = ext4_ext_rm_idx(handle, inode, path + i); | |
2235 | } | |
d0d856e8 | 2236 | /* root level has p_bh == NULL, brelse() eats this */ |
a86c6181 AT |
2237 | brelse(path[i].p_bh); |
2238 | path[i].p_bh = NULL; | |
2239 | i--; | |
2240 | ext_debug("return to level %d\n", i); | |
2241 | } | |
2242 | } | |
2243 | ||
2244 | /* TODO: flexible tree reduction should be here */ | |
2245 | if (path->p_hdr->eh_entries == 0) { | |
2246 | /* | |
d0d856e8 RD |
2247 | * truncate to zero freed all the tree, |
2248 | * so we need to correct eh_depth | |
a86c6181 AT |
2249 | */ |
2250 | err = ext4_ext_get_access(handle, inode, path); | |
2251 | if (err == 0) { | |
2252 | ext_inode_hdr(inode)->eh_depth = 0; | |
2253 | ext_inode_hdr(inode)->eh_max = | |
2254 | cpu_to_le16(ext4_ext_space_root(inode)); | |
2255 | err = ext4_ext_dirty(handle, inode, path); | |
2256 | } | |
2257 | } | |
2258 | out: | |
a86c6181 AT |
2259 | ext4_ext_drop_refs(path); |
2260 | kfree(path); | |
2261 | ext4_journal_stop(handle); | |
2262 | ||
2263 | return err; | |
2264 | } | |
2265 | ||
2266 | /* | |
2267 | * called at mount time | |
2268 | */ | |
2269 | void ext4_ext_init(struct super_block *sb) | |
2270 | { | |
2271 | /* | |
2272 | * possible initialization would be here | |
2273 | */ | |
2274 | ||
83982b6f | 2275 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) { |
4776004f | 2276 | printk(KERN_INFO "EXT4-fs: file extents enabled"); |
bbf2f9fb RD |
2277 | #ifdef AGGRESSIVE_TEST |
2278 | printk(", aggressive tests"); | |
a86c6181 AT |
2279 | #endif |
2280 | #ifdef CHECK_BINSEARCH | |
2281 | printk(", check binsearch"); | |
2282 | #endif | |
2283 | #ifdef EXTENTS_STATS | |
2284 | printk(", stats"); | |
2285 | #endif | |
2286 | printk("\n"); | |
2287 | #ifdef EXTENTS_STATS | |
2288 | spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); | |
2289 | EXT4_SB(sb)->s_ext_min = 1 << 30; | |
2290 | EXT4_SB(sb)->s_ext_max = 0; | |
2291 | #endif | |
2292 | } | |
2293 | } | |
2294 | ||
2295 | /* | |
2296 | * called at umount time | |
2297 | */ | |
2298 | void ext4_ext_release(struct super_block *sb) | |
2299 | { | |
83982b6f | 2300 | if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) |
a86c6181 AT |
2301 | return; |
2302 | ||
2303 | #ifdef EXTENTS_STATS | |
2304 | if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) { | |
2305 | struct ext4_sb_info *sbi = EXT4_SB(sb); | |
2306 | printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n", | |
2307 | sbi->s_ext_blocks, sbi->s_ext_extents, | |
2308 | sbi->s_ext_blocks / sbi->s_ext_extents); | |
2309 | printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n", | |
2310 | sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max); | |
2311 | } | |
2312 | #endif | |
2313 | } | |
2314 | ||
093a088b AK |
2315 | static void bi_complete(struct bio *bio, int error) |
2316 | { | |
2317 | complete((struct completion *)bio->bi_private); | |
2318 | } | |
2319 | ||
2320 | /* FIXME!! we need to try to merge to left or right after zero-out */ | |
2321 | static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) | |
2322 | { | |
2323 | int ret = -EIO; | |
2324 | struct bio *bio; | |
2325 | int blkbits, blocksize; | |
2326 | sector_t ee_pblock; | |
2327 | struct completion event; | |
2328 | unsigned int ee_len, len, done, offset; | |
2329 | ||
2330 | ||
2331 | blkbits = inode->i_blkbits; | |
2332 | blocksize = inode->i_sb->s_blocksize; | |
2333 | ee_len = ext4_ext_get_actual_len(ex); | |
2334 | ee_pblock = ext_pblock(ex); | |
2335 | ||
2336 | /* convert ee_pblock to 512 byte sectors */ | |
2337 | ee_pblock = ee_pblock << (blkbits - 9); | |
2338 | ||
2339 | while (ee_len > 0) { | |
2340 | ||
2341 | if (ee_len > BIO_MAX_PAGES) | |
2342 | len = BIO_MAX_PAGES; | |
2343 | else | |
2344 | len = ee_len; | |
2345 | ||
2346 | bio = bio_alloc(GFP_NOIO, len); | |
2347 | if (!bio) | |
2348 | return -ENOMEM; | |
2349 | bio->bi_sector = ee_pblock; | |
2350 | bio->bi_bdev = inode->i_sb->s_bdev; | |
2351 | ||
2352 | done = 0; | |
2353 | offset = 0; | |
2354 | while (done < len) { | |
2355 | ret = bio_add_page(bio, ZERO_PAGE(0), | |
2356 | blocksize, offset); | |
2357 | if (ret != blocksize) { | |
2358 | /* | |
2359 | * We can't add any more pages because of | |
2360 | * hardware limitations. Start a new bio. | |
2361 | */ | |
2362 | break; | |
2363 | } | |
2364 | done++; | |
2365 | offset += blocksize; | |
2366 | if (offset >= PAGE_CACHE_SIZE) | |
2367 | offset = 0; | |
2368 | } | |
2369 | ||
2370 | init_completion(&event); | |
2371 | bio->bi_private = &event; | |
2372 | bio->bi_end_io = bi_complete; | |
2373 | submit_bio(WRITE, bio); | |
2374 | wait_for_completion(&event); | |
2375 | ||
2376 | if (test_bit(BIO_UPTODATE, &bio->bi_flags)) | |
2377 | ret = 0; | |
2378 | else { | |
2379 | ret = -EIO; | |
2380 | break; | |
2381 | } | |
2382 | bio_put(bio); | |
2383 | ee_len -= done; | |
2384 | ee_pblock += done << (blkbits - 9); | |
2385 | } | |
2386 | return ret; | |
2387 | } | |
2388 | ||
3977c965 AK |
2389 | #define EXT4_EXT_ZERO_LEN 7 |
2390 | ||
56055d3a AA |
2391 | /* |
2392 | * This function is called by ext4_ext_get_blocks() if someone tries to write | |
2393 | * to an uninitialized extent. It may result in splitting the uninitialized | |
2394 | * extent into multiple extents (upto three - one initialized and two | |
2395 | * uninitialized). | |
2396 | * There are three possibilities: | |
2397 | * a> There is no split required: Entire extent should be initialized | |
2398 | * b> Splits in two extents: Write is happening at either end of the extent | |
2399 | * c> Splits in three extents: Somone is writing in middle of the extent | |
2400 | */ | |
725d26d3 AK |
2401 | static int ext4_ext_convert_to_initialized(handle_t *handle, |
2402 | struct inode *inode, | |
2403 | struct ext4_ext_path *path, | |
2404 | ext4_lblk_t iblock, | |
498e5f24 | 2405 | unsigned int max_blocks) |
56055d3a | 2406 | { |
95c3889c | 2407 | struct ext4_extent *ex, newex, orig_ex; |
56055d3a AA |
2408 | struct ext4_extent *ex1 = NULL; |
2409 | struct ext4_extent *ex2 = NULL; | |
2410 | struct ext4_extent *ex3 = NULL; | |
2411 | struct ext4_extent_header *eh; | |
725d26d3 AK |
2412 | ext4_lblk_t ee_block; |
2413 | unsigned int allocated, ee_len, depth; | |
56055d3a AA |
2414 | ext4_fsblk_t newblock; |
2415 | int err = 0; | |
2416 | int ret = 0; | |
2417 | ||
2418 | depth = ext_depth(inode); | |
2419 | eh = path[depth].p_hdr; | |
2420 | ex = path[depth].p_ext; | |
2421 | ee_block = le32_to_cpu(ex->ee_block); | |
2422 | ee_len = ext4_ext_get_actual_len(ex); | |
2423 | allocated = ee_len - (iblock - ee_block); | |
2424 | newblock = iblock - ee_block + ext_pblock(ex); | |
2425 | ex2 = ex; | |
95c3889c AK |
2426 | orig_ex.ee_block = ex->ee_block; |
2427 | orig_ex.ee_len = cpu_to_le16(ee_len); | |
2428 | ext4_ext_store_pblock(&orig_ex, ext_pblock(ex)); | |
56055d3a | 2429 | |
9df5643a AK |
2430 | err = ext4_ext_get_access(handle, inode, path + depth); |
2431 | if (err) | |
2432 | goto out; | |
3977c965 AK |
2433 | /* If extent has less than 2*EXT4_EXT_ZERO_LEN zerout directly */ |
2434 | if (ee_len <= 2*EXT4_EXT_ZERO_LEN) { | |
2435 | err = ext4_ext_zeroout(inode, &orig_ex); | |
2436 | if (err) | |
2437 | goto fix_extent_len; | |
2438 | /* update the extent length and mark as initialized */ | |
2439 | ex->ee_block = orig_ex.ee_block; | |
2440 | ex->ee_len = orig_ex.ee_len; | |
2441 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); | |
2442 | ext4_ext_dirty(handle, inode, path + depth); | |
161e7b7c AK |
2443 | /* zeroed the full extent */ |
2444 | return allocated; | |
3977c965 | 2445 | } |
9df5643a | 2446 | |
56055d3a AA |
2447 | /* ex1: ee_block to iblock - 1 : uninitialized */ |
2448 | if (iblock > ee_block) { | |
2449 | ex1 = ex; | |
2450 | ex1->ee_len = cpu_to_le16(iblock - ee_block); | |
2451 | ext4_ext_mark_uninitialized(ex1); | |
2452 | ex2 = &newex; | |
2453 | } | |
2454 | /* | |
2455 | * for sanity, update the length of the ex2 extent before | |
2456 | * we insert ex3, if ex1 is NULL. This is to avoid temporary | |
2457 | * overlap of blocks. | |
2458 | */ | |
2459 | if (!ex1 && allocated > max_blocks) | |
2460 | ex2->ee_len = cpu_to_le16(max_blocks); | |
2461 | /* ex3: to ee_block + ee_len : uninitialised */ | |
2462 | if (allocated > max_blocks) { | |
2463 | unsigned int newdepth; | |
3977c965 AK |
2464 | /* If extent has less than EXT4_EXT_ZERO_LEN zerout directly */ |
2465 | if (allocated <= EXT4_EXT_ZERO_LEN) { | |
d03856bd AK |
2466 | /* |
2467 | * iblock == ee_block is handled by the zerouout | |
2468 | * at the beginning. | |
2469 | * Mark first half uninitialized. | |
3977c965 AK |
2470 | * Mark second half initialized and zero out the |
2471 | * initialized extent | |
2472 | */ | |
2473 | ex->ee_block = orig_ex.ee_block; | |
2474 | ex->ee_len = cpu_to_le16(ee_len - allocated); | |
2475 | ext4_ext_mark_uninitialized(ex); | |
2476 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); | |
2477 | ext4_ext_dirty(handle, inode, path + depth); | |
2478 | ||
2479 | ex3 = &newex; | |
2480 | ex3->ee_block = cpu_to_le32(iblock); | |
2481 | ext4_ext_store_pblock(ex3, newblock); | |
2482 | ex3->ee_len = cpu_to_le16(allocated); | |
2483 | err = ext4_ext_insert_extent(handle, inode, path, ex3); | |
2484 | if (err == -ENOSPC) { | |
2485 | err = ext4_ext_zeroout(inode, &orig_ex); | |
2486 | if (err) | |
2487 | goto fix_extent_len; | |
2488 | ex->ee_block = orig_ex.ee_block; | |
2489 | ex->ee_len = orig_ex.ee_len; | |
2490 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); | |
2491 | ext4_ext_dirty(handle, inode, path + depth); | |
d03856bd | 2492 | /* blocks available from iblock */ |
161e7b7c | 2493 | return allocated; |
3977c965 AK |
2494 | |
2495 | } else if (err) | |
2496 | goto fix_extent_len; | |
2497 | ||
161e7b7c AK |
2498 | /* |
2499 | * We need to zero out the second half because | |
2500 | * an fallocate request can update file size and | |
2501 | * converting the second half to initialized extent | |
2502 | * implies that we can leak some junk data to user | |
2503 | * space. | |
2504 | */ | |
2505 | err = ext4_ext_zeroout(inode, ex3); | |
2506 | if (err) { | |
2507 | /* | |
2508 | * We should actually mark the | |
2509 | * second half as uninit and return error | |
2510 | * Insert would have changed the extent | |
2511 | */ | |
2512 | depth = ext_depth(inode); | |
2513 | ext4_ext_drop_refs(path); | |
2514 | path = ext4_ext_find_extent(inode, | |
2515 | iblock, path); | |
2516 | if (IS_ERR(path)) { | |
2517 | err = PTR_ERR(path); | |
2518 | return err; | |
2519 | } | |
d03856bd | 2520 | /* get the second half extent details */ |
161e7b7c AK |
2521 | ex = path[depth].p_ext; |
2522 | err = ext4_ext_get_access(handle, inode, | |
2523 | path + depth); | |
2524 | if (err) | |
2525 | return err; | |
2526 | ext4_ext_mark_uninitialized(ex); | |
2527 | ext4_ext_dirty(handle, inode, path + depth); | |
2528 | return err; | |
2529 | } | |
2530 | ||
2531 | /* zeroed the second half */ | |
3977c965 AK |
2532 | return allocated; |
2533 | } | |
56055d3a AA |
2534 | ex3 = &newex; |
2535 | ex3->ee_block = cpu_to_le32(iblock + max_blocks); | |
2536 | ext4_ext_store_pblock(ex3, newblock + max_blocks); | |
2537 | ex3->ee_len = cpu_to_le16(allocated - max_blocks); | |
2538 | ext4_ext_mark_uninitialized(ex3); | |
2539 | err = ext4_ext_insert_extent(handle, inode, path, ex3); | |
093a088b AK |
2540 | if (err == -ENOSPC) { |
2541 | err = ext4_ext_zeroout(inode, &orig_ex); | |
2542 | if (err) | |
2543 | goto fix_extent_len; | |
2544 | /* update the extent length and mark as initialized */ | |
95c3889c AK |
2545 | ex->ee_block = orig_ex.ee_block; |
2546 | ex->ee_len = orig_ex.ee_len; | |
2547 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); | |
95c3889c | 2548 | ext4_ext_dirty(handle, inode, path + depth); |
161e7b7c | 2549 | /* zeroed the full extent */ |
d03856bd | 2550 | /* blocks available from iblock */ |
161e7b7c | 2551 | return allocated; |
093a088b AK |
2552 | |
2553 | } else if (err) | |
2554 | goto fix_extent_len; | |
56055d3a AA |
2555 | /* |
2556 | * The depth, and hence eh & ex might change | |
2557 | * as part of the insert above. | |
2558 | */ | |
2559 | newdepth = ext_depth(inode); | |
95c3889c | 2560 | /* |
73ac36ea | 2561 | * update the extent length after successful insert of the |
95c3889c AK |
2562 | * split extent |
2563 | */ | |
2564 | orig_ex.ee_len = cpu_to_le16(ee_len - | |
2565 | ext4_ext_get_actual_len(ex3)); | |
d03856bd AK |
2566 | depth = newdepth; |
2567 | ext4_ext_drop_refs(path); | |
2568 | path = ext4_ext_find_extent(inode, iblock, path); | |
2569 | if (IS_ERR(path)) { | |
2570 | err = PTR_ERR(path); | |
2571 | goto out; | |
56055d3a | 2572 | } |
d03856bd AK |
2573 | eh = path[depth].p_hdr; |
2574 | ex = path[depth].p_ext; | |
2575 | if (ex2 != &newex) | |
2576 | ex2 = ex; | |
2577 | ||
2578 | err = ext4_ext_get_access(handle, inode, path + depth); | |
2579 | if (err) | |
2580 | goto out; | |
2581 | ||
56055d3a | 2582 | allocated = max_blocks; |
3977c965 AK |
2583 | |
2584 | /* If extent has less than EXT4_EXT_ZERO_LEN and we are trying | |
2585 | * to insert a extent in the middle zerout directly | |
2586 | * otherwise give the extent a chance to merge to left | |
2587 | */ | |
2588 | if (le16_to_cpu(orig_ex.ee_len) <= EXT4_EXT_ZERO_LEN && | |
2589 | iblock != ee_block) { | |
2590 | err = ext4_ext_zeroout(inode, &orig_ex); | |
2591 | if (err) | |
2592 | goto fix_extent_len; | |
2593 | /* update the extent length and mark as initialized */ | |
2594 | ex->ee_block = orig_ex.ee_block; | |
2595 | ex->ee_len = orig_ex.ee_len; | |
2596 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); | |
2597 | ext4_ext_dirty(handle, inode, path + depth); | |
161e7b7c | 2598 | /* zero out the first half */ |
d03856bd | 2599 | /* blocks available from iblock */ |
161e7b7c | 2600 | return allocated; |
3977c965 | 2601 | } |
56055d3a AA |
2602 | } |
2603 | /* | |
2604 | * If there was a change of depth as part of the | |
2605 | * insertion of ex3 above, we need to update the length | |
2606 | * of the ex1 extent again here | |
2607 | */ | |
2608 | if (ex1 && ex1 != ex) { | |
2609 | ex1 = ex; | |
2610 | ex1->ee_len = cpu_to_le16(iblock - ee_block); | |
2611 | ext4_ext_mark_uninitialized(ex1); | |
2612 | ex2 = &newex; | |
2613 | } | |
2614 | /* ex2: iblock to iblock + maxblocks-1 : initialised */ | |
2615 | ex2->ee_block = cpu_to_le32(iblock); | |
56055d3a AA |
2616 | ext4_ext_store_pblock(ex2, newblock); |
2617 | ex2->ee_len = cpu_to_le16(allocated); | |
2618 | if (ex2 != ex) | |
2619 | goto insert; | |
56055d3a AA |
2620 | /* |
2621 | * New (initialized) extent starts from the first block | |
2622 | * in the current extent. i.e., ex2 == ex | |
2623 | * We have to see if it can be merged with the extent | |
2624 | * on the left. | |
2625 | */ | |
2626 | if (ex2 > EXT_FIRST_EXTENT(eh)) { | |
2627 | /* | |
2628 | * To merge left, pass "ex2 - 1" to try_to_merge(), | |
2629 | * since it merges towards right _only_. | |
2630 | */ | |
2631 | ret = ext4_ext_try_to_merge(inode, path, ex2 - 1); | |
2632 | if (ret) { | |
2633 | err = ext4_ext_correct_indexes(handle, inode, path); | |
2634 | if (err) | |
2635 | goto out; | |
2636 | depth = ext_depth(inode); | |
2637 | ex2--; | |
2638 | } | |
2639 | } | |
2640 | /* | |
2641 | * Try to Merge towards right. This might be required | |
2642 | * only when the whole extent is being written to. | |
2643 | * i.e. ex2 == ex and ex3 == NULL. | |
2644 | */ | |
2645 | if (!ex3) { | |
2646 | ret = ext4_ext_try_to_merge(inode, path, ex2); | |
2647 | if (ret) { | |
2648 | err = ext4_ext_correct_indexes(handle, inode, path); | |
2649 | if (err) | |
2650 | goto out; | |
2651 | } | |
2652 | } | |
2653 | /* Mark modified extent as dirty */ | |
2654 | err = ext4_ext_dirty(handle, inode, path + depth); | |
2655 | goto out; | |
2656 | insert: | |
2657 | err = ext4_ext_insert_extent(handle, inode, path, &newex); | |
093a088b AK |
2658 | if (err == -ENOSPC) { |
2659 | err = ext4_ext_zeroout(inode, &orig_ex); | |
2660 | if (err) | |
2661 | goto fix_extent_len; | |
2662 | /* update the extent length and mark as initialized */ | |
95c3889c AK |
2663 | ex->ee_block = orig_ex.ee_block; |
2664 | ex->ee_len = orig_ex.ee_len; | |
2665 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); | |
95c3889c | 2666 | ext4_ext_dirty(handle, inode, path + depth); |
161e7b7c AK |
2667 | /* zero out the first half */ |
2668 | return allocated; | |
093a088b AK |
2669 | } else if (err) |
2670 | goto fix_extent_len; | |
56055d3a AA |
2671 | out: |
2672 | return err ? err : allocated; | |
093a088b AK |
2673 | |
2674 | fix_extent_len: | |
2675 | ex->ee_block = orig_ex.ee_block; | |
2676 | ex->ee_len = orig_ex.ee_len; | |
2677 | ext4_ext_store_pblock(ex, ext_pblock(&orig_ex)); | |
2678 | ext4_ext_mark_uninitialized(ex); | |
2679 | ext4_ext_dirty(handle, inode, path + depth); | |
2680 | return err; | |
56055d3a AA |
2681 | } |
2682 | ||
c278bfec | 2683 | /* |
f5ab0d1f MC |
2684 | * Block allocation/map/preallocation routine for extents based files |
2685 | * | |
2686 | * | |
c278bfec | 2687 | * Need to be called with |
0e855ac8 AK |
2688 | * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block |
2689 | * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem) | |
f5ab0d1f MC |
2690 | * |
2691 | * return > 0, number of of blocks already mapped/allocated | |
2692 | * if create == 0 and these are pre-allocated blocks | |
2693 | * buffer head is unmapped | |
2694 | * otherwise blocks are mapped | |
2695 | * | |
2696 | * return = 0, if plain look up failed (blocks have not been allocated) | |
2697 | * buffer head is unmapped | |
2698 | * | |
2699 | * return < 0, error case. | |
c278bfec | 2700 | */ |
f65e6fba | 2701 | int ext4_ext_get_blocks(handle_t *handle, struct inode *inode, |
725d26d3 | 2702 | ext4_lblk_t iblock, |
498e5f24 | 2703 | unsigned int max_blocks, struct buffer_head *bh_result, |
a86c6181 AT |
2704 | int create, int extend_disksize) |
2705 | { | |
2706 | struct ext4_ext_path *path = NULL; | |
56055d3a | 2707 | struct ext4_extent_header *eh; |
a86c6181 | 2708 | struct ext4_extent newex, *ex; |
498e5f24 TT |
2709 | ext4_fsblk_t newblock; |
2710 | int err = 0, depth, ret, cache_type; | |
2711 | unsigned int allocated = 0; | |
c9de560d | 2712 | struct ext4_allocation_request ar; |
61628a3f | 2713 | loff_t disksize; |
a86c6181 AT |
2714 | |
2715 | __clear_bit(BH_New, &bh_result->b_state); | |
498e5f24 | 2716 | ext_debug("blocks %u/%u requested for inode %u\n", |
bba90743 | 2717 | iblock, max_blocks, inode->i_ino); |
a86c6181 AT |
2718 | |
2719 | /* check in cache */ | |
498e5f24 TT |
2720 | cache_type = ext4_ext_in_cache(inode, iblock, &newex); |
2721 | if (cache_type) { | |
2722 | if (cache_type == EXT4_EXT_CACHE_GAP) { | |
a86c6181 | 2723 | if (!create) { |
56055d3a AA |
2724 | /* |
2725 | * block isn't allocated yet and | |
2726 | * user doesn't want to allocate it | |
2727 | */ | |
a86c6181 AT |
2728 | goto out2; |
2729 | } | |
2730 | /* we should allocate requested block */ | |
498e5f24 | 2731 | } else if (cache_type == EXT4_EXT_CACHE_EXTENT) { |
a86c6181 | 2732 | /* block is already allocated */ |
8c55e204 DK |
2733 | newblock = iblock |
2734 | - le32_to_cpu(newex.ee_block) | |
2735 | + ext_pblock(&newex); | |
d0d856e8 | 2736 | /* number of remaining blocks in the extent */ |
b939e376 | 2737 | allocated = ext4_ext_get_actual_len(&newex) - |
a86c6181 AT |
2738 | (iblock - le32_to_cpu(newex.ee_block)); |
2739 | goto out; | |
2740 | } else { | |
2741 | BUG(); | |
2742 | } | |
2743 | } | |
2744 | ||
2745 | /* find extent for this block */ | |
2746 | path = ext4_ext_find_extent(inode, iblock, NULL); | |
2747 | if (IS_ERR(path)) { | |
2748 | err = PTR_ERR(path); | |
2749 | path = NULL; | |
2750 | goto out2; | |
2751 | } | |
2752 | ||
2753 | depth = ext_depth(inode); | |
2754 | ||
2755 | /* | |
d0d856e8 RD |
2756 | * consistent leaf must not be empty; |
2757 | * this situation is possible, though, _during_ tree modification; | |
a86c6181 AT |
2758 | * this is why assert can't be put in ext4_ext_find_extent() |
2759 | */ | |
2760 | BUG_ON(path[depth].p_ext == NULL && depth != 0); | |
56055d3a | 2761 | eh = path[depth].p_hdr; |
a86c6181 | 2762 | |
7e028976 AM |
2763 | ex = path[depth].p_ext; |
2764 | if (ex) { | |
725d26d3 | 2765 | ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); |
f65e6fba | 2766 | ext4_fsblk_t ee_start = ext_pblock(ex); |
a2df2a63 | 2767 | unsigned short ee_len; |
471d4011 SB |
2768 | |
2769 | /* | |
471d4011 | 2770 | * Uninitialized extents are treated as holes, except that |
56055d3a | 2771 | * we split out initialized portions during a write. |
471d4011 | 2772 | */ |
a2df2a63 | 2773 | ee_len = ext4_ext_get_actual_len(ex); |
d0d856e8 | 2774 | /* if found extent covers block, simply return it */ |
8c55e204 | 2775 | if (iblock >= ee_block && iblock < ee_block + ee_len) { |
a86c6181 | 2776 | newblock = iblock - ee_block + ee_start; |
d0d856e8 | 2777 | /* number of remaining blocks in the extent */ |
a86c6181 | 2778 | allocated = ee_len - (iblock - ee_block); |
bba90743 | 2779 | ext_debug("%u fit into %lu:%d -> %llu\n", iblock, |
a86c6181 | 2780 | ee_block, ee_len, newblock); |
56055d3a | 2781 | |
a2df2a63 | 2782 | /* Do not put uninitialized extent in the cache */ |
56055d3a | 2783 | if (!ext4_ext_is_uninitialized(ex)) { |
a2df2a63 AA |
2784 | ext4_ext_put_in_cache(inode, ee_block, |
2785 | ee_len, ee_start, | |
2786 | EXT4_EXT_CACHE_EXTENT); | |
56055d3a AA |
2787 | goto out; |
2788 | } | |
2789 | if (create == EXT4_CREATE_UNINITIALIZED_EXT) | |
2790 | goto out; | |
e067ba00 AK |
2791 | if (!create) { |
2792 | /* | |
2793 | * We have blocks reserved already. We | |
2794 | * return allocated blocks so that delalloc | |
2795 | * won't do block reservation for us. But | |
2796 | * the buffer head will be unmapped so that | |
2797 | * a read from the block returns 0s. | |
2798 | */ | |
2799 | if (allocated > max_blocks) | |
2800 | allocated = max_blocks; | |
953e622b | 2801 | set_buffer_unwritten(bh_result); |
56055d3a | 2802 | goto out2; |
e067ba00 | 2803 | } |
56055d3a AA |
2804 | |
2805 | ret = ext4_ext_convert_to_initialized(handle, inode, | |
2806 | path, iblock, | |
2807 | max_blocks); | |
dbf9d7da DM |
2808 | if (ret <= 0) { |
2809 | err = ret; | |
56055d3a | 2810 | goto out2; |
dbf9d7da | 2811 | } else |
56055d3a AA |
2812 | allocated = ret; |
2813 | goto outnew; | |
a86c6181 AT |
2814 | } |
2815 | } | |
2816 | ||
2817 | /* | |
d0d856e8 | 2818 | * requested block isn't allocated yet; |
a86c6181 AT |
2819 | * we couldn't try to create block if create flag is zero |
2820 | */ | |
2821 | if (!create) { | |
56055d3a AA |
2822 | /* |
2823 | * put just found gap into cache to speed up | |
2824 | * subsequent requests | |
2825 | */ | |
a86c6181 AT |
2826 | ext4_ext_put_gap_in_cache(inode, path, iblock); |
2827 | goto out2; | |
2828 | } | |
2829 | /* | |
c2ea3fde | 2830 | * Okay, we need to do block allocation. |
63f57933 | 2831 | */ |
a86c6181 | 2832 | |
c9de560d AT |
2833 | /* find neighbour allocated blocks */ |
2834 | ar.lleft = iblock; | |
2835 | err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft); | |
2836 | if (err) | |
2837 | goto out2; | |
2838 | ar.lright = iblock; | |
2839 | err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright); | |
2840 | if (err) | |
2841 | goto out2; | |
25d14f98 | 2842 | |
749269fa AA |
2843 | /* |
2844 | * See if request is beyond maximum number of blocks we can have in | |
2845 | * a single extent. For an initialized extent this limit is | |
2846 | * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is | |
2847 | * EXT_UNINIT_MAX_LEN. | |
2848 | */ | |
2849 | if (max_blocks > EXT_INIT_MAX_LEN && | |
2850 | create != EXT4_CREATE_UNINITIALIZED_EXT) | |
2851 | max_blocks = EXT_INIT_MAX_LEN; | |
2852 | else if (max_blocks > EXT_UNINIT_MAX_LEN && | |
2853 | create == EXT4_CREATE_UNINITIALIZED_EXT) | |
2854 | max_blocks = EXT_UNINIT_MAX_LEN; | |
2855 | ||
25d14f98 AA |
2856 | /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */ |
2857 | newex.ee_block = cpu_to_le32(iblock); | |
2858 | newex.ee_len = cpu_to_le16(max_blocks); | |
2859 | err = ext4_ext_check_overlap(inode, &newex, path); | |
2860 | if (err) | |
b939e376 | 2861 | allocated = ext4_ext_get_actual_len(&newex); |
25d14f98 AA |
2862 | else |
2863 | allocated = max_blocks; | |
c9de560d AT |
2864 | |
2865 | /* allocate new block */ | |
2866 | ar.inode = inode; | |
2867 | ar.goal = ext4_ext_find_goal(inode, path, iblock); | |
2868 | ar.logical = iblock; | |
2869 | ar.len = allocated; | |
2870 | if (S_ISREG(inode->i_mode)) | |
2871 | ar.flags = EXT4_MB_HINT_DATA; | |
2872 | else | |
2873 | /* disable in-core preallocation for non-regular files */ | |
2874 | ar.flags = 0; | |
2875 | newblock = ext4_mb_new_blocks(handle, &ar, &err); | |
a86c6181 AT |
2876 | if (!newblock) |
2877 | goto out2; | |
2ae02107 | 2878 | ext_debug("allocate new block: goal %llu, found %llu/%lu\n", |
498e5f24 | 2879 | ar.goal, newblock, allocated); |
a86c6181 AT |
2880 | |
2881 | /* try to insert new extent into found leaf and return */ | |
f65e6fba | 2882 | ext4_ext_store_pblock(&newex, newblock); |
c9de560d | 2883 | newex.ee_len = cpu_to_le16(ar.len); |
a2df2a63 AA |
2884 | if (create == EXT4_CREATE_UNINITIALIZED_EXT) /* Mark uninitialized */ |
2885 | ext4_ext_mark_uninitialized(&newex); | |
a86c6181 | 2886 | err = ext4_ext_insert_extent(handle, inode, path, &newex); |
315054f0 AT |
2887 | if (err) { |
2888 | /* free data blocks we just allocated */ | |
c9de560d AT |
2889 | /* not a good idea to call discard here directly, |
2890 | * but otherwise we'd need to call it every free() */ | |
c2ea3fde | 2891 | ext4_discard_preallocations(inode); |
315054f0 | 2892 | ext4_free_blocks(handle, inode, ext_pblock(&newex), |
b939e376 | 2893 | ext4_ext_get_actual_len(&newex), 0); |
a86c6181 | 2894 | goto out2; |
315054f0 | 2895 | } |
a86c6181 | 2896 | |
a86c6181 | 2897 | /* previous routine could use block we allocated */ |
f65e6fba | 2898 | newblock = ext_pblock(&newex); |
b939e376 | 2899 | allocated = ext4_ext_get_actual_len(&newex); |
56055d3a | 2900 | outnew: |
61628a3f MC |
2901 | if (extend_disksize) { |
2902 | disksize = ((loff_t) iblock + ar.len) << inode->i_blkbits; | |
2903 | if (disksize > i_size_read(inode)) | |
2904 | disksize = i_size_read(inode); | |
2905 | if (disksize > EXT4_I(inode)->i_disksize) | |
2906 | EXT4_I(inode)->i_disksize = disksize; | |
2907 | } | |
a379cd1d | 2908 | |
953e622b | 2909 | set_buffer_new(bh_result); |
a86c6181 | 2910 | |
a2df2a63 AA |
2911 | /* Cache only when it is _not_ an uninitialized extent */ |
2912 | if (create != EXT4_CREATE_UNINITIALIZED_EXT) | |
2913 | ext4_ext_put_in_cache(inode, iblock, allocated, newblock, | |
2914 | EXT4_EXT_CACHE_EXTENT); | |
a86c6181 AT |
2915 | out: |
2916 | if (allocated > max_blocks) | |
2917 | allocated = max_blocks; | |
2918 | ext4_ext_show_leaf(inode, path); | |
953e622b | 2919 | set_buffer_mapped(bh_result); |
a86c6181 AT |
2920 | bh_result->b_bdev = inode->i_sb->s_bdev; |
2921 | bh_result->b_blocknr = newblock; | |
2922 | out2: | |
2923 | if (path) { | |
2924 | ext4_ext_drop_refs(path); | |
2925 | kfree(path); | |
2926 | } | |
a86c6181 AT |
2927 | return err ? err : allocated; |
2928 | } | |
2929 | ||
cf108bca | 2930 | void ext4_ext_truncate(struct inode *inode) |
a86c6181 AT |
2931 | { |
2932 | struct address_space *mapping = inode->i_mapping; | |
2933 | struct super_block *sb = inode->i_sb; | |
725d26d3 | 2934 | ext4_lblk_t last_block; |
a86c6181 AT |
2935 | handle_t *handle; |
2936 | int err = 0; | |
2937 | ||
2938 | /* | |
2939 | * probably first extent we're gonna free will be last in block | |
2940 | */ | |
f3bd1f3f | 2941 | err = ext4_writepage_trans_blocks(inode); |
a86c6181 | 2942 | handle = ext4_journal_start(inode, err); |
cf108bca | 2943 | if (IS_ERR(handle)) |
a86c6181 | 2944 | return; |
a86c6181 | 2945 | |
cf108bca JK |
2946 | if (inode->i_size & (sb->s_blocksize - 1)) |
2947 | ext4_block_truncate_page(handle, mapping, inode->i_size); | |
a86c6181 | 2948 | |
9ddfc3dc JK |
2949 | if (ext4_orphan_add(handle, inode)) |
2950 | goto out_stop; | |
2951 | ||
0e855ac8 | 2952 | down_write(&EXT4_I(inode)->i_data_sem); |
a86c6181 AT |
2953 | ext4_ext_invalidate_cache(inode); |
2954 | ||
c2ea3fde | 2955 | ext4_discard_preallocations(inode); |
c9de560d | 2956 | |
a86c6181 | 2957 | /* |
d0d856e8 RD |
2958 | * TODO: optimization is possible here. |
2959 | * Probably we need not scan at all, | |
2960 | * because page truncation is enough. | |
a86c6181 | 2961 | */ |
a86c6181 AT |
2962 | |
2963 | /* we have to know where to truncate from in crash case */ | |
2964 | EXT4_I(inode)->i_disksize = inode->i_size; | |
2965 | ext4_mark_inode_dirty(handle, inode); | |
2966 | ||
2967 | last_block = (inode->i_size + sb->s_blocksize - 1) | |
2968 | >> EXT4_BLOCK_SIZE_BITS(sb); | |
2969 | err = ext4_ext_remove_space(inode, last_block); | |
2970 | ||
2971 | /* In a multi-transaction truncate, we only make the final | |
56055d3a AA |
2972 | * transaction synchronous. |
2973 | */ | |
a86c6181 | 2974 | if (IS_SYNC(inode)) |
0390131b | 2975 | ext4_handle_sync(handle); |
a86c6181 AT |
2976 | |
2977 | out_stop: | |
9ddfc3dc | 2978 | up_write(&EXT4_I(inode)->i_data_sem); |
a86c6181 | 2979 | /* |
d0d856e8 | 2980 | * If this was a simple ftruncate() and the file will remain alive, |
a86c6181 AT |
2981 | * then we need to clear up the orphan record which we created above. |
2982 | * However, if this was a real unlink then we were called by | |
2983 | * ext4_delete_inode(), and we allow that function to clean up the | |
2984 | * orphan info for us. | |
2985 | */ | |
2986 | if (inode->i_nlink) | |
2987 | ext4_orphan_del(handle, inode); | |
2988 | ||
ef737728 SR |
2989 | inode->i_mtime = inode->i_ctime = ext4_current_time(inode); |
2990 | ext4_mark_inode_dirty(handle, inode); | |
a86c6181 AT |
2991 | ext4_journal_stop(handle); |
2992 | } | |
2993 | ||
fd28784a AK |
2994 | static void ext4_falloc_update_inode(struct inode *inode, |
2995 | int mode, loff_t new_size, int update_ctime) | |
2996 | { | |
2997 | struct timespec now; | |
2998 | ||
2999 | if (update_ctime) { | |
3000 | now = current_fs_time(inode->i_sb); | |
3001 | if (!timespec_equal(&inode->i_ctime, &now)) | |
3002 | inode->i_ctime = now; | |
3003 | } | |
3004 | /* | |
3005 | * Update only when preallocation was requested beyond | |
3006 | * the file size. | |
3007 | */ | |
cf17fea6 AK |
3008 | if (!(mode & FALLOC_FL_KEEP_SIZE)) { |
3009 | if (new_size > i_size_read(inode)) | |
3010 | i_size_write(inode, new_size); | |
3011 | if (new_size > EXT4_I(inode)->i_disksize) | |
3012 | ext4_update_i_disksize(inode, new_size); | |
fd28784a AK |
3013 | } |
3014 | ||
3015 | } | |
3016 | ||
a2df2a63 AA |
3017 | /* |
3018 | * preallocate space for a file. This implements ext4's fallocate inode | |
3019 | * operation, which gets called from sys_fallocate system call. | |
3020 | * For block-mapped files, posix_fallocate should fall back to the method | |
3021 | * of writing zeroes to the required new blocks (the same behavior which is | |
3022 | * expected for file systems which do not support fallocate() system call). | |
3023 | */ | |
3024 | long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len) | |
3025 | { | |
3026 | handle_t *handle; | |
725d26d3 | 3027 | ext4_lblk_t block; |
fd28784a | 3028 | loff_t new_size; |
498e5f24 | 3029 | unsigned int max_blocks; |
a2df2a63 AA |
3030 | int ret = 0; |
3031 | int ret2 = 0; | |
3032 | int retries = 0; | |
3033 | struct buffer_head map_bh; | |
3034 | unsigned int credits, blkbits = inode->i_blkbits; | |
3035 | ||
3036 | /* | |
3037 | * currently supporting (pre)allocate mode for extent-based | |
3038 | * files _only_ | |
3039 | */ | |
3040 | if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) | |
3041 | return -EOPNOTSUPP; | |
3042 | ||
3043 | /* preallocation to directories is currently not supported */ | |
3044 | if (S_ISDIR(inode->i_mode)) | |
3045 | return -ENODEV; | |
3046 | ||
3047 | block = offset >> blkbits; | |
fd28784a AK |
3048 | /* |
3049 | * We can't just convert len to max_blocks because | |
3050 | * If blocksize = 4096 offset = 3072 and len = 2048 | |
3051 | */ | |
a2df2a63 | 3052 | max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) |
fd28784a | 3053 | - block; |
a2df2a63 | 3054 | /* |
f3bd1f3f | 3055 | * credits to insert 1 extent into extent tree |
a2df2a63 | 3056 | */ |
f3bd1f3f | 3057 | credits = ext4_chunk_trans_blocks(inode, max_blocks); |
55bd725a | 3058 | mutex_lock(&inode->i_mutex); |
a2df2a63 AA |
3059 | retry: |
3060 | while (ret >= 0 && ret < max_blocks) { | |
3061 | block = block + ret; | |
3062 | max_blocks = max_blocks - ret; | |
3063 | handle = ext4_journal_start(inode, credits); | |
3064 | if (IS_ERR(handle)) { | |
3065 | ret = PTR_ERR(handle); | |
3066 | break; | |
3067 | } | |
55bd725a | 3068 | ret = ext4_get_blocks_wrap(handle, inode, block, |
a2df2a63 | 3069 | max_blocks, &map_bh, |
d2a17637 | 3070 | EXT4_CREATE_UNINITIALIZED_EXT, 0, 0); |
221879c9 | 3071 | if (ret <= 0) { |
2c98615d AK |
3072 | #ifdef EXT4FS_DEBUG |
3073 | WARN_ON(ret <= 0); | |
3074 | printk(KERN_ERR "%s: ext4_ext_get_blocks " | |
3075 | "returned error inode#%lu, block=%u, " | |
9fd9784c | 3076 | "max_blocks=%u", __func__, |
221879c9 | 3077 | inode->i_ino, block, max_blocks); |
2c98615d | 3078 | #endif |
a2df2a63 AA |
3079 | ext4_mark_inode_dirty(handle, inode); |
3080 | ret2 = ext4_journal_stop(handle); | |
3081 | break; | |
3082 | } | |
fd28784a AK |
3083 | if ((block + ret) >= (EXT4_BLOCK_ALIGN(offset + len, |
3084 | blkbits) >> blkbits)) | |
3085 | new_size = offset + len; | |
3086 | else | |
3087 | new_size = (block + ret) << blkbits; | |
a2df2a63 | 3088 | |
fd28784a AK |
3089 | ext4_falloc_update_inode(inode, mode, new_size, |
3090 | buffer_new(&map_bh)); | |
a2df2a63 AA |
3091 | ext4_mark_inode_dirty(handle, inode); |
3092 | ret2 = ext4_journal_stop(handle); | |
3093 | if (ret2) | |
3094 | break; | |
3095 | } | |
fd28784a AK |
3096 | if (ret == -ENOSPC && |
3097 | ext4_should_retry_alloc(inode->i_sb, &retries)) { | |
3098 | ret = 0; | |
a2df2a63 | 3099 | goto retry; |
a2df2a63 | 3100 | } |
55bd725a | 3101 | mutex_unlock(&inode->i_mutex); |
a2df2a63 AA |
3102 | return ret > 0 ? ret2 : ret; |
3103 | } | |
6873fa0d ES |
3104 | |
3105 | /* | |
3106 | * Callback function called for each extent to gather FIEMAP information. | |
3107 | */ | |
3a06d778 | 3108 | static int ext4_ext_fiemap_cb(struct inode *inode, struct ext4_ext_path *path, |
6873fa0d ES |
3109 | struct ext4_ext_cache *newex, struct ext4_extent *ex, |
3110 | void *data) | |
3111 | { | |
3112 | struct fiemap_extent_info *fieinfo = data; | |
3113 | unsigned long blksize_bits = inode->i_sb->s_blocksize_bits; | |
3114 | __u64 logical; | |
3115 | __u64 physical; | |
3116 | __u64 length; | |
3117 | __u32 flags = 0; | |
3118 | int error; | |
3119 | ||
3120 | logical = (__u64)newex->ec_block << blksize_bits; | |
3121 | ||
3122 | if (newex->ec_type == EXT4_EXT_CACHE_GAP) { | |
3123 | pgoff_t offset; | |
3124 | struct page *page; | |
3125 | struct buffer_head *bh = NULL; | |
3126 | ||
3127 | offset = logical >> PAGE_SHIFT; | |
3128 | page = find_get_page(inode->i_mapping, offset); | |
3129 | if (!page || !page_has_buffers(page)) | |
3130 | return EXT_CONTINUE; | |
3131 | ||
3132 | bh = page_buffers(page); | |
3133 | ||
3134 | if (!bh) | |
3135 | return EXT_CONTINUE; | |
3136 | ||
3137 | if (buffer_delay(bh)) { | |
3138 | flags |= FIEMAP_EXTENT_DELALLOC; | |
3139 | page_cache_release(page); | |
3140 | } else { | |
3141 | page_cache_release(page); | |
3142 | return EXT_CONTINUE; | |
3143 | } | |
3144 | } | |
3145 | ||
3146 | physical = (__u64)newex->ec_start << blksize_bits; | |
3147 | length = (__u64)newex->ec_len << blksize_bits; | |
3148 | ||
3149 | if (ex && ext4_ext_is_uninitialized(ex)) | |
3150 | flags |= FIEMAP_EXTENT_UNWRITTEN; | |
3151 | ||
3152 | /* | |
3153 | * If this extent reaches EXT_MAX_BLOCK, it must be last. | |
3154 | * | |
3155 | * Or if ext4_ext_next_allocated_block is EXT_MAX_BLOCK, | |
3156 | * this also indicates no more allocated blocks. | |
3157 | * | |
3158 | * XXX this might miss a single-block extent at EXT_MAX_BLOCK | |
3159 | */ | |
3160 | if (logical + length - 1 == EXT_MAX_BLOCK || | |
3161 | ext4_ext_next_allocated_block(path) == EXT_MAX_BLOCK) | |
3162 | flags |= FIEMAP_EXTENT_LAST; | |
3163 | ||
3164 | error = fiemap_fill_next_extent(fieinfo, logical, physical, | |
3165 | length, flags); | |
3166 | if (error < 0) | |
3167 | return error; | |
3168 | if (error == 1) | |
3169 | return EXT_BREAK; | |
3170 | ||
3171 | return EXT_CONTINUE; | |
3172 | } | |
3173 | ||
3174 | /* fiemap flags we can handle specified here */ | |
3175 | #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR) | |
3176 | ||
3a06d778 AK |
3177 | static int ext4_xattr_fiemap(struct inode *inode, |
3178 | struct fiemap_extent_info *fieinfo) | |
6873fa0d ES |
3179 | { |
3180 | __u64 physical = 0; | |
3181 | __u64 length; | |
3182 | __u32 flags = FIEMAP_EXTENT_LAST; | |
3183 | int blockbits = inode->i_sb->s_blocksize_bits; | |
3184 | int error = 0; | |
3185 | ||
3186 | /* in-inode? */ | |
3187 | if (EXT4_I(inode)->i_state & EXT4_STATE_XATTR) { | |
3188 | struct ext4_iloc iloc; | |
3189 | int offset; /* offset of xattr in inode */ | |
3190 | ||
3191 | error = ext4_get_inode_loc(inode, &iloc); | |
3192 | if (error) | |
3193 | return error; | |
3194 | physical = iloc.bh->b_blocknr << blockbits; | |
3195 | offset = EXT4_GOOD_OLD_INODE_SIZE + | |
3196 | EXT4_I(inode)->i_extra_isize; | |
3197 | physical += offset; | |
3198 | length = EXT4_SB(inode->i_sb)->s_inode_size - offset; | |
3199 | flags |= FIEMAP_EXTENT_DATA_INLINE; | |
3200 | } else { /* external block */ | |
3201 | physical = EXT4_I(inode)->i_file_acl << blockbits; | |
3202 | length = inode->i_sb->s_blocksize; | |
3203 | } | |
3204 | ||
3205 | if (physical) | |
3206 | error = fiemap_fill_next_extent(fieinfo, 0, physical, | |
3207 | length, flags); | |
3208 | return (error < 0 ? error : 0); | |
3209 | } | |
3210 | ||
3211 | int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, | |
3212 | __u64 start, __u64 len) | |
3213 | { | |
3214 | ext4_lblk_t start_blk; | |
3215 | ext4_lblk_t len_blks; | |
3216 | int error = 0; | |
3217 | ||
3218 | /* fallback to generic here if not in extents fmt */ | |
3219 | if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) | |
3220 | return generic_block_fiemap(inode, fieinfo, start, len, | |
3221 | ext4_get_block); | |
3222 | ||
3223 | if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS)) | |
3224 | return -EBADR; | |
3225 | ||
3226 | if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { | |
3227 | error = ext4_xattr_fiemap(inode, fieinfo); | |
3228 | } else { | |
3229 | start_blk = start >> inode->i_sb->s_blocksize_bits; | |
3230 | len_blks = len >> inode->i_sb->s_blocksize_bits; | |
3231 | ||
3232 | /* | |
3233 | * Walk the extent tree gathering extent information. | |
3234 | * ext4_ext_fiemap_cb will push extents back to user. | |
3235 | */ | |
3236 | down_write(&EXT4_I(inode)->i_data_sem); | |
3237 | error = ext4_ext_walk_space(inode, start_blk, len_blks, | |
3238 | ext4_ext_fiemap_cb, fieinfo); | |
3239 | up_write(&EXT4_I(inode)->i_data_sem); | |
3240 | } | |
3241 | ||
3242 | return error; | |
3243 | } | |
3244 |