Commit | Line | Data |
---|---|---|
ac27a0ec | 1 | /* |
617ba13b | 2 | * linux/fs/ext4/inode.c |
ac27a0ec DK |
3 | * |
4 | * Copyright (C) 1992, 1993, 1994, 1995 | |
5 | * Remy Card (card@masi.ibp.fr) | |
6 | * Laboratoire MASI - Institut Blaise Pascal | |
7 | * Universite Pierre et Marie Curie (Paris VI) | |
8 | * | |
9 | * from | |
10 | * | |
11 | * linux/fs/minix/inode.c | |
12 | * | |
13 | * Copyright (C) 1991, 1992 Linus Torvalds | |
14 | * | |
ac27a0ec DK |
15 | * 64-bit file support on 64-bit platforms by Jakub Jelinek |
16 | * (jj@sunsite.ms.mff.cuni.cz) | |
17 | * | |
617ba13b | 18 | * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000 |
ac27a0ec DK |
19 | */ |
20 | ||
ac27a0ec DK |
21 | #include <linux/fs.h> |
22 | #include <linux/time.h> | |
dab291af | 23 | #include <linux/jbd2.h> |
ac27a0ec DK |
24 | #include <linux/highuid.h> |
25 | #include <linux/pagemap.h> | |
26 | #include <linux/quotaops.h> | |
27 | #include <linux/string.h> | |
28 | #include <linux/buffer_head.h> | |
29 | #include <linux/writeback.h> | |
64769240 | 30 | #include <linux/pagevec.h> |
ac27a0ec | 31 | #include <linux/mpage.h> |
e83c1397 | 32 | #include <linux/namei.h> |
ac27a0ec DK |
33 | #include <linux/uio.h> |
34 | #include <linux/bio.h> | |
4c0425ff | 35 | #include <linux/workqueue.h> |
744692dc | 36 | #include <linux/kernel.h> |
6db26ffc | 37 | #include <linux/printk.h> |
5a0e3ad6 | 38 | #include <linux/slab.h> |
a8901d34 | 39 | #include <linux/ratelimit.h> |
a27bb332 | 40 | #include <linux/aio.h> |
0a0ae7b3 | 41 | #include <linux/bitops.h> |
9bffad1e | 42 | |
3dcf5451 | 43 | #include "ext4_jbd2.h" |
ac27a0ec DK |
44 | #include "xattr.h" |
45 | #include "acl.h" | |
9f125d64 | 46 | #include "truncate.h" |
ac27a0ec | 47 | |
9bffad1e | 48 | #include <trace/events/ext4.h> |
6fa3eb70 | 49 | #include <linux/blkdev.h> |
9bffad1e | 50 | |
a1d6cc56 AK |
51 | #define MPAGE_DA_EXTENT_TAIL 0x01 |
52 | ||
814525f4 DW |
53 | static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw, |
54 | struct ext4_inode_info *ei) | |
55 | { | |
56 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
814525f4 | 57 | __u32 csum; |
3a45bbb2 DJ |
58 | __u16 dummy_csum = 0; |
59 | int offset = offsetof(struct ext4_inode, i_checksum_lo); | |
60 | unsigned int csum_size = sizeof(dummy_csum); | |
814525f4 | 61 | |
3a45bbb2 DJ |
62 | csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, offset); |
63 | csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, csum_size); | |
64 | offset += csum_size; | |
65 | csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset, | |
66 | EXT4_GOOD_OLD_INODE_SIZE - offset); | |
814525f4 | 67 | |
3a45bbb2 DJ |
68 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { |
69 | offset = offsetof(struct ext4_inode, i_checksum_hi); | |
70 | csum = ext4_chksum(sbi, csum, (__u8 *)raw + | |
71 | EXT4_GOOD_OLD_INODE_SIZE, | |
72 | offset - EXT4_GOOD_OLD_INODE_SIZE); | |
73 | if (EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) { | |
74 | csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, | |
75 | csum_size); | |
76 | offset += csum_size; | |
3a45bbb2 | 77 | } |
cd0d9254 DJ |
78 | csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset, |
79 | EXT4_INODE_SIZE(inode->i_sb) - offset); | |
3a45bbb2 | 80 | } |
814525f4 DW |
81 | |
82 | return csum; | |
83 | } | |
84 | ||
85 | static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw, | |
86 | struct ext4_inode_info *ei) | |
87 | { | |
88 | __u32 provided, calculated; | |
89 | ||
90 | if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != | |
91 | cpu_to_le32(EXT4_OS_LINUX) || | |
92 | !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, | |
93 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) | |
94 | return 1; | |
95 | ||
96 | provided = le16_to_cpu(raw->i_checksum_lo); | |
97 | calculated = ext4_inode_csum(inode, raw, ei); | |
98 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && | |
99 | EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) | |
100 | provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16; | |
101 | else | |
102 | calculated &= 0xFFFF; | |
103 | ||
104 | return provided == calculated; | |
105 | } | |
106 | ||
107 | static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw, | |
108 | struct ext4_inode_info *ei) | |
109 | { | |
110 | __u32 csum; | |
111 | ||
112 | if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != | |
113 | cpu_to_le32(EXT4_OS_LINUX) || | |
114 | !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, | |
115 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) | |
116 | return; | |
117 | ||
118 | csum = ext4_inode_csum(inode, raw, ei); | |
119 | raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF); | |
120 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE && | |
121 | EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) | |
122 | raw->i_checksum_hi = cpu_to_le16(csum >> 16); | |
123 | } | |
124 | ||
678aaf48 JK |
125 | static inline int ext4_begin_ordered_truncate(struct inode *inode, |
126 | loff_t new_size) | |
127 | { | |
7ff9c073 | 128 | trace_ext4_begin_ordered_truncate(inode, new_size); |
8aefcd55 TT |
129 | /* |
130 | * If jinode is zero, then we never opened the file for | |
131 | * writing, so there's no need to call | |
132 | * jbd2_journal_begin_ordered_truncate() since there's no | |
133 | * outstanding writes we need to flush. | |
134 | */ | |
135 | if (!EXT4_I(inode)->jinode) | |
136 | return 0; | |
137 | return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode), | |
138 | EXT4_I(inode)->jinode, | |
139 | new_size); | |
678aaf48 JK |
140 | } |
141 | ||
64769240 | 142 | static void ext4_invalidatepage(struct page *page, unsigned long offset); |
cb20d518 TT |
143 | static int __ext4_journalled_writepage(struct page *page, unsigned int len); |
144 | static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh); | |
5f163cc7 ES |
145 | static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle, |
146 | struct inode *inode, struct page *page, loff_t from, | |
147 | loff_t length, int flags); | |
64769240 | 148 | |
ac27a0ec DK |
149 | /* |
150 | * Test whether an inode is a fast symlink. | |
151 | */ | |
617ba13b | 152 | static int ext4_inode_is_fast_symlink(struct inode *inode) |
ac27a0ec | 153 | { |
617ba13b | 154 | int ea_blocks = EXT4_I(inode)->i_file_acl ? |
ac27a0ec DK |
155 | (inode->i_sb->s_blocksize >> 9) : 0; |
156 | ||
157 | return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0); | |
158 | } | |
159 | ||
ac27a0ec DK |
160 | /* |
161 | * Restart the transaction associated with *handle. This does a commit, | |
162 | * so before we call here everything must be consistently dirtied against | |
163 | * this transaction. | |
164 | */ | |
fa5d1113 | 165 | int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode, |
487caeef | 166 | int nblocks) |
ac27a0ec | 167 | { |
487caeef JK |
168 | int ret; |
169 | ||
170 | /* | |
e35fd660 | 171 | * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this |
487caeef JK |
172 | * moment, get_block can be called only for blocks inside i_size since |
173 | * page cache has been already dropped and writes are blocked by | |
174 | * i_mutex. So we can safely drop the i_data_sem here. | |
175 | */ | |
0390131b | 176 | BUG_ON(EXT4_JOURNAL(inode) == NULL); |
ac27a0ec | 177 | jbd_debug(2, "restarting handle %p\n", handle); |
487caeef | 178 | up_write(&EXT4_I(inode)->i_data_sem); |
8e8eaabe | 179 | ret = ext4_journal_restart(handle, nblocks); |
487caeef | 180 | down_write(&EXT4_I(inode)->i_data_sem); |
fa5d1113 | 181 | ext4_discard_preallocations(inode); |
487caeef JK |
182 | |
183 | return ret; | |
ac27a0ec DK |
184 | } |
185 | ||
186 | /* | |
187 | * Called at the last iput() if i_nlink is zero. | |
188 | */ | |
0930fcc1 | 189 | void ext4_evict_inode(struct inode *inode) |
ac27a0ec DK |
190 | { |
191 | handle_t *handle; | |
bc965ab3 | 192 | int err; |
ac27a0ec | 193 | |
7ff9c073 | 194 | trace_ext4_evict_inode(inode); |
2581fdc8 | 195 | |
0930fcc1 | 196 | if (inode->i_nlink) { |
2d859db3 JK |
197 | /* |
198 | * When journalling data dirty buffers are tracked only in the | |
199 | * journal. So although mm thinks everything is clean and | |
200 | * ready for reaping the inode might still have some pages to | |
201 | * write in the running transaction or waiting to be | |
202 | * checkpointed. Thus calling jbd2_journal_invalidatepage() | |
203 | * (via truncate_inode_pages()) to discard these buffers can | |
204 | * cause data loss. Also even if we did not discard these | |
205 | * buffers, we would have no way to find them after the inode | |
206 | * is reaped and thus user could see stale data if he tries to | |
207 | * read them before the transaction is checkpointed. So be | |
208 | * careful and force everything to disk here... We use | |
209 | * ei->i_datasync_tid to store the newest transaction | |
210 | * containing inode's data. | |
211 | * | |
212 | * Note that directories do not have this problem because they | |
213 | * don't use page cache. | |
214 | */ | |
8b6ab35c VN |
215 | if (inode->i_ino != EXT4_JOURNAL_INO && |
216 | ext4_should_journal_data(inode) && | |
217 | (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode))) { | |
2d859db3 JK |
218 | journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; |
219 | tid_t commit_tid = EXT4_I(inode)->i_datasync_tid; | |
220 | ||
d76a3a77 | 221 | jbd2_complete_transaction(journal, commit_tid); |
2d859db3 JK |
222 | filemap_write_and_wait(&inode->i_data); |
223 | } | |
0930fcc1 | 224 | truncate_inode_pages(&inode->i_data, 0); |
1ada47d9 | 225 | ext4_ioend_shutdown(inode); |
0930fcc1 AV |
226 | goto no_delete; |
227 | } | |
228 | ||
907f4554 | 229 | if (!is_bad_inode(inode)) |
871a2931 | 230 | dquot_initialize(inode); |
907f4554 | 231 | |
678aaf48 JK |
232 | if (ext4_should_order_data(inode)) |
233 | ext4_begin_ordered_truncate(inode, 0); | |
ac27a0ec | 234 | truncate_inode_pages(&inode->i_data, 0); |
1ada47d9 | 235 | ext4_ioend_shutdown(inode); |
ac27a0ec DK |
236 | |
237 | if (is_bad_inode(inode)) | |
238 | goto no_delete; | |
239 | ||
8e8ad8a5 JK |
240 | /* |
241 | * Protect us against freezing - iput() caller didn't have to have any | |
242 | * protection against it | |
243 | */ | |
244 | sb_start_intwrite(inode->i_sb); | |
9924a92a TT |
245 | handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, |
246 | ext4_blocks_for_truncate(inode)+3); | |
ac27a0ec | 247 | if (IS_ERR(handle)) { |
bc965ab3 | 248 | ext4_std_error(inode->i_sb, PTR_ERR(handle)); |
ac27a0ec DK |
249 | /* |
250 | * If we're going to skip the normal cleanup, we still need to | |
251 | * make sure that the in-core orphan linked list is properly | |
252 | * cleaned up. | |
253 | */ | |
617ba13b | 254 | ext4_orphan_del(NULL, inode); |
8e8ad8a5 | 255 | sb_end_intwrite(inode->i_sb); |
ac27a0ec DK |
256 | goto no_delete; |
257 | } | |
258 | ||
259 | if (IS_SYNC(inode)) | |
0390131b | 260 | ext4_handle_sync(handle); |
ac27a0ec | 261 | inode->i_size = 0; |
bc965ab3 TT |
262 | err = ext4_mark_inode_dirty(handle, inode); |
263 | if (err) { | |
12062ddd | 264 | ext4_warning(inode->i_sb, |
bc965ab3 TT |
265 | "couldn't mark inode dirty (err %d)", err); |
266 | goto stop_handle; | |
267 | } | |
ac27a0ec | 268 | if (inode->i_blocks) |
617ba13b | 269 | ext4_truncate(inode); |
bc965ab3 TT |
270 | |
271 | /* | |
272 | * ext4_ext_truncate() doesn't reserve any slop when it | |
273 | * restarts journal transactions; therefore there may not be | |
274 | * enough credits left in the handle to remove the inode from | |
275 | * the orphan list and set the dtime field. | |
276 | */ | |
0390131b | 277 | if (!ext4_handle_has_enough_credits(handle, 3)) { |
bc965ab3 TT |
278 | err = ext4_journal_extend(handle, 3); |
279 | if (err > 0) | |
280 | err = ext4_journal_restart(handle, 3); | |
281 | if (err != 0) { | |
12062ddd | 282 | ext4_warning(inode->i_sb, |
bc965ab3 TT |
283 | "couldn't extend journal (err %d)", err); |
284 | stop_handle: | |
285 | ext4_journal_stop(handle); | |
45388219 | 286 | ext4_orphan_del(NULL, inode); |
8e8ad8a5 | 287 | sb_end_intwrite(inode->i_sb); |
bc965ab3 TT |
288 | goto no_delete; |
289 | } | |
290 | } | |
291 | ||
ac27a0ec | 292 | /* |
617ba13b | 293 | * Kill off the orphan record which ext4_truncate created. |
ac27a0ec | 294 | * AKPM: I think this can be inside the above `if'. |
617ba13b | 295 | * Note that ext4_orphan_del() has to be able to cope with the |
ac27a0ec | 296 | * deletion of a non-existent orphan - this is because we don't |
617ba13b | 297 | * know if ext4_truncate() actually created an orphan record. |
ac27a0ec DK |
298 | * (Well, we could do this if we need to, but heck - it works) |
299 | */ | |
617ba13b MC |
300 | ext4_orphan_del(handle, inode); |
301 | EXT4_I(inode)->i_dtime = get_seconds(); | |
ac27a0ec DK |
302 | |
303 | /* | |
304 | * One subtle ordering requirement: if anything has gone wrong | |
305 | * (transaction abort, IO errors, whatever), then we can still | |
306 | * do these next steps (the fs will already have been marked as | |
307 | * having errors), but we can't free the inode if the mark_dirty | |
308 | * fails. | |
309 | */ | |
617ba13b | 310 | if (ext4_mark_inode_dirty(handle, inode)) |
ac27a0ec | 311 | /* If that failed, just do the required in-core inode clear. */ |
0930fcc1 | 312 | ext4_clear_inode(inode); |
ac27a0ec | 313 | else |
617ba13b MC |
314 | ext4_free_inode(handle, inode); |
315 | ext4_journal_stop(handle); | |
8e8ad8a5 | 316 | sb_end_intwrite(inode->i_sb); |
ac27a0ec DK |
317 | return; |
318 | no_delete: | |
0930fcc1 | 319 | ext4_clear_inode(inode); /* We must guarantee clearing of inode... */ |
ac27a0ec DK |
320 | } |
321 | ||
a9e7f447 DM |
322 | #ifdef CONFIG_QUOTA |
323 | qsize_t *ext4_get_reserved_space(struct inode *inode) | |
60e58e0f | 324 | { |
a9e7f447 | 325 | return &EXT4_I(inode)->i_reserved_quota; |
60e58e0f | 326 | } |
a9e7f447 | 327 | #endif |
9d0be502 | 328 | |
12219aea AK |
329 | /* |
330 | * Calculate the number of metadata blocks need to reserve | |
9d0be502 | 331 | * to allocate a block located at @lblock |
12219aea | 332 | */ |
01f49d0b | 333 | static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock) |
12219aea | 334 | { |
12e9b892 | 335 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
9d0be502 | 336 | return ext4_ext_calc_metadata_amount(inode, lblock); |
12219aea | 337 | |
8bb2b247 | 338 | return ext4_ind_calc_metadata_amount(inode, lblock); |
12219aea AK |
339 | } |
340 | ||
0637c6f4 TT |
341 | /* |
342 | * Called with i_data_sem down, which is important since we can call | |
343 | * ext4_discard_preallocations() from here. | |
344 | */ | |
5f634d06 AK |
345 | void ext4_da_update_reserve_space(struct inode *inode, |
346 | int used, int quota_claim) | |
12219aea AK |
347 | { |
348 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
0637c6f4 | 349 | struct ext4_inode_info *ei = EXT4_I(inode); |
0637c6f4 TT |
350 | |
351 | spin_lock(&ei->i_block_reservation_lock); | |
d8990240 | 352 | trace_ext4_da_update_reserve_space(inode, used, quota_claim); |
0637c6f4 | 353 | if (unlikely(used > ei->i_reserved_data_blocks)) { |
8de5c325 | 354 | ext4_warning(inode->i_sb, "%s: ino %lu, used %d " |
1084f252 | 355 | "with only %d reserved data blocks", |
0637c6f4 TT |
356 | __func__, inode->i_ino, used, |
357 | ei->i_reserved_data_blocks); | |
358 | WARN_ON(1); | |
359 | used = ei->i_reserved_data_blocks; | |
360 | } | |
12219aea | 361 | |
97795d2a | 362 | if (unlikely(ei->i_allocated_meta_blocks > ei->i_reserved_meta_blocks)) { |
01a523eb TT |
363 | ext4_warning(inode->i_sb, "ino %lu, allocated %d " |
364 | "with only %d reserved metadata blocks " | |
365 | "(releasing %d blocks with reserved %d data blocks)", | |
366 | inode->i_ino, ei->i_allocated_meta_blocks, | |
367 | ei->i_reserved_meta_blocks, used, | |
368 | ei->i_reserved_data_blocks); | |
97795d2a BF |
369 | WARN_ON(1); |
370 | ei->i_allocated_meta_blocks = ei->i_reserved_meta_blocks; | |
371 | } | |
372 | ||
0637c6f4 TT |
373 | /* Update per-inode reservations */ |
374 | ei->i_reserved_data_blocks -= used; | |
0637c6f4 | 375 | ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks; |
57042651 | 376 | percpu_counter_sub(&sbi->s_dirtyclusters_counter, |
72b8ab9d | 377 | used + ei->i_allocated_meta_blocks); |
0637c6f4 | 378 | ei->i_allocated_meta_blocks = 0; |
6bc6e63f | 379 | |
0637c6f4 TT |
380 | if (ei->i_reserved_data_blocks == 0) { |
381 | /* | |
382 | * We can release all of the reserved metadata blocks | |
383 | * only when we have written all of the delayed | |
384 | * allocation blocks. | |
385 | */ | |
57042651 | 386 | percpu_counter_sub(&sbi->s_dirtyclusters_counter, |
72b8ab9d | 387 | ei->i_reserved_meta_blocks); |
ee5f4d9c | 388 | ei->i_reserved_meta_blocks = 0; |
9d0be502 | 389 | ei->i_da_metadata_calc_len = 0; |
6bc6e63f | 390 | } |
12219aea | 391 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
60e58e0f | 392 | |
72b8ab9d ES |
393 | /* Update quota subsystem for data blocks */ |
394 | if (quota_claim) | |
7b415bf6 | 395 | dquot_claim_block(inode, EXT4_C2B(sbi, used)); |
72b8ab9d | 396 | else { |
5f634d06 AK |
397 | /* |
398 | * We did fallocate with an offset that is already delayed | |
399 | * allocated. So on delayed allocated writeback we should | |
72b8ab9d | 400 | * not re-claim the quota for fallocated blocks. |
5f634d06 | 401 | */ |
7b415bf6 | 402 | dquot_release_reservation_block(inode, EXT4_C2B(sbi, used)); |
5f634d06 | 403 | } |
d6014301 AK |
404 | |
405 | /* | |
406 | * If we have done all the pending block allocations and if | |
407 | * there aren't any writers on the inode, we can discard the | |
408 | * inode's preallocations. | |
409 | */ | |
0637c6f4 TT |
410 | if ((ei->i_reserved_data_blocks == 0) && |
411 | (atomic_read(&inode->i_writecount) == 0)) | |
d6014301 | 412 | ext4_discard_preallocations(inode); |
12219aea AK |
413 | } |
414 | ||
e29136f8 | 415 | static int __check_block_validity(struct inode *inode, const char *func, |
c398eda0 TT |
416 | unsigned int line, |
417 | struct ext4_map_blocks *map) | |
6fd058f7 | 418 | { |
24676da4 TT |
419 | if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk, |
420 | map->m_len)) { | |
c398eda0 TT |
421 | ext4_error_inode(inode, func, line, map->m_pblk, |
422 | "lblock %lu mapped to illegal pblock " | |
423 | "(length %d)", (unsigned long) map->m_lblk, | |
424 | map->m_len); | |
6fd058f7 TT |
425 | return -EIO; |
426 | } | |
427 | return 0; | |
428 | } | |
429 | ||
e29136f8 | 430 | #define check_block_validity(inode, map) \ |
c398eda0 | 431 | __check_block_validity((inode), __func__, __LINE__, (map)) |
e29136f8 | 432 | |
55138e0b | 433 | /* |
1f94533d TT |
434 | * Return the number of contiguous dirty pages in a given inode |
435 | * starting at page frame idx. | |
55138e0b TT |
436 | */ |
437 | static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx, | |
438 | unsigned int max_pages) | |
439 | { | |
440 | struct address_space *mapping = inode->i_mapping; | |
441 | pgoff_t index; | |
442 | struct pagevec pvec; | |
443 | pgoff_t num = 0; | |
444 | int i, nr_pages, done = 0; | |
445 | ||
446 | if (max_pages == 0) | |
447 | return 0; | |
448 | pagevec_init(&pvec, 0); | |
449 | while (!done) { | |
450 | index = idx; | |
451 | nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, | |
452 | PAGECACHE_TAG_DIRTY, | |
453 | (pgoff_t)PAGEVEC_SIZE); | |
454 | if (nr_pages == 0) | |
455 | break; | |
456 | for (i = 0; i < nr_pages; i++) { | |
457 | struct page *page = pvec.pages[i]; | |
458 | struct buffer_head *bh, *head; | |
459 | ||
460 | lock_page(page); | |
461 | if (unlikely(page->mapping != mapping) || | |
462 | !PageDirty(page) || | |
463 | PageWriteback(page) || | |
464 | page->index != idx) { | |
465 | done = 1; | |
466 | unlock_page(page); | |
467 | break; | |
468 | } | |
1f94533d TT |
469 | if (page_has_buffers(page)) { |
470 | bh = head = page_buffers(page); | |
471 | do { | |
472 | if (!buffer_delay(bh) && | |
473 | !buffer_unwritten(bh)) | |
474 | done = 1; | |
475 | bh = bh->b_this_page; | |
476 | } while (!done && (bh != head)); | |
477 | } | |
55138e0b TT |
478 | unlock_page(page); |
479 | if (done) | |
480 | break; | |
481 | idx++; | |
482 | num++; | |
659c6009 ES |
483 | if (num >= max_pages) { |
484 | done = 1; | |
55138e0b | 485 | break; |
659c6009 | 486 | } |
55138e0b TT |
487 | } |
488 | pagevec_release(&pvec); | |
489 | } | |
490 | return num; | |
491 | } | |
492 | ||
921f266b DM |
493 | #ifdef ES_AGGRESSIVE_TEST |
494 | static void ext4_map_blocks_es_recheck(handle_t *handle, | |
495 | struct inode *inode, | |
496 | struct ext4_map_blocks *es_map, | |
497 | struct ext4_map_blocks *map, | |
498 | int flags) | |
499 | { | |
500 | int retval; | |
501 | ||
502 | map->m_flags = 0; | |
503 | /* | |
504 | * There is a race window that the result is not the same. | |
505 | * e.g. xfstests #223 when dioread_nolock enables. The reason | |
506 | * is that we lookup a block mapping in extent status tree with | |
507 | * out taking i_data_sem. So at the time the unwritten extent | |
508 | * could be converted. | |
509 | */ | |
510 | if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) | |
511 | down_read((&EXT4_I(inode)->i_data_sem)); | |
512 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { | |
513 | retval = ext4_ext_map_blocks(handle, inode, map, flags & | |
514 | EXT4_GET_BLOCKS_KEEP_SIZE); | |
515 | } else { | |
516 | retval = ext4_ind_map_blocks(handle, inode, map, flags & | |
517 | EXT4_GET_BLOCKS_KEEP_SIZE); | |
518 | } | |
519 | if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) | |
520 | up_read((&EXT4_I(inode)->i_data_sem)); | |
521 | /* | |
522 | * Clear EXT4_MAP_FROM_CLUSTER and EXT4_MAP_BOUNDARY flag | |
523 | * because it shouldn't be marked in es_map->m_flags. | |
524 | */ | |
525 | map->m_flags &= ~(EXT4_MAP_FROM_CLUSTER | EXT4_MAP_BOUNDARY); | |
526 | ||
527 | /* | |
528 | * We don't check m_len because extent will be collpased in status | |
529 | * tree. So the m_len might not equal. | |
530 | */ | |
531 | if (es_map->m_lblk != map->m_lblk || | |
532 | es_map->m_flags != map->m_flags || | |
533 | es_map->m_pblk != map->m_pblk) { | |
534 | printk("ES cache assertation failed for inode: %lu " | |
535 | "es_cached ex [%d/%d/%llu/%x] != " | |
536 | "found ex [%d/%d/%llu/%x] retval %d flags %x\n", | |
537 | inode->i_ino, es_map->m_lblk, es_map->m_len, | |
538 | es_map->m_pblk, es_map->m_flags, map->m_lblk, | |
539 | map->m_len, map->m_pblk, map->m_flags, | |
540 | retval, flags); | |
541 | } | |
542 | } | |
543 | #endif /* ES_AGGRESSIVE_TEST */ | |
544 | ||
f5ab0d1f | 545 | /* |
e35fd660 | 546 | * The ext4_map_blocks() function tries to look up the requested blocks, |
2b2d6d01 | 547 | * and returns if the blocks are already mapped. |
f5ab0d1f | 548 | * |
f5ab0d1f MC |
549 | * Otherwise it takes the write lock of the i_data_sem and allocate blocks |
550 | * and store the allocated blocks in the result buffer head and mark it | |
551 | * mapped. | |
552 | * | |
e35fd660 TT |
553 | * If file type is extents based, it will call ext4_ext_map_blocks(), |
554 | * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping | |
f5ab0d1f MC |
555 | * based files |
556 | * | |
557 | * On success, it returns the number of blocks being mapped or allocate. | |
558 | * if create==0 and the blocks are pre-allocated and uninitialized block, | |
559 | * the result buffer head is unmapped. If the create ==1, it will make sure | |
560 | * the buffer head is mapped. | |
561 | * | |
562 | * It returns 0 if plain look up failed (blocks have not been allocated), in | |
df3ab170 | 563 | * that case, buffer head is unmapped |
f5ab0d1f MC |
564 | * |
565 | * It returns the error in case of allocation failure. | |
566 | */ | |
e35fd660 TT |
567 | int ext4_map_blocks(handle_t *handle, struct inode *inode, |
568 | struct ext4_map_blocks *map, int flags) | |
0e855ac8 | 569 | { |
d100eef2 | 570 | struct extent_status es; |
0e855ac8 | 571 | int retval; |
921f266b DM |
572 | #ifdef ES_AGGRESSIVE_TEST |
573 | struct ext4_map_blocks orig_map; | |
574 | ||
575 | memcpy(&orig_map, map, sizeof(*map)); | |
576 | #endif | |
f5ab0d1f | 577 | |
e35fd660 TT |
578 | map->m_flags = 0; |
579 | ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u," | |
580 | "logical block %lu\n", inode->i_ino, flags, map->m_len, | |
581 | (unsigned long) map->m_lblk); | |
d100eef2 ZL |
582 | |
583 | /* Lookup extent status tree firstly */ | |
584 | if (ext4_es_lookup_extent(inode, map->m_lblk, &es)) { | |
585 | if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) { | |
586 | map->m_pblk = ext4_es_pblock(&es) + | |
587 | map->m_lblk - es.es_lblk; | |
588 | map->m_flags |= ext4_es_is_written(&es) ? | |
589 | EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN; | |
590 | retval = es.es_len - (map->m_lblk - es.es_lblk); | |
591 | if (retval > map->m_len) | |
592 | retval = map->m_len; | |
593 | map->m_len = retval; | |
594 | } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) { | |
595 | retval = 0; | |
596 | } else { | |
597 | BUG_ON(1); | |
598 | } | |
921f266b DM |
599 | #ifdef ES_AGGRESSIVE_TEST |
600 | ext4_map_blocks_es_recheck(handle, inode, map, | |
601 | &orig_map, flags); | |
602 | #endif | |
d100eef2 ZL |
603 | goto found; |
604 | } | |
605 | ||
4df3d265 | 606 | /* |
b920c755 TT |
607 | * Try to see if we can get the block without requesting a new |
608 | * file system block. | |
4df3d265 | 609 | */ |
729f52c6 ZL |
610 | if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) |
611 | down_read((&EXT4_I(inode)->i_data_sem)); | |
12e9b892 | 612 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { |
a4e5d88b DM |
613 | retval = ext4_ext_map_blocks(handle, inode, map, flags & |
614 | EXT4_GET_BLOCKS_KEEP_SIZE); | |
0e855ac8 | 615 | } else { |
a4e5d88b DM |
616 | retval = ext4_ind_map_blocks(handle, inode, map, flags & |
617 | EXT4_GET_BLOCKS_KEEP_SIZE); | |
0e855ac8 | 618 | } |
f7fec032 ZL |
619 | if (retval > 0) { |
620 | int ret; | |
621 | unsigned long long status; | |
622 | ||
921f266b DM |
623 | #ifdef ES_AGGRESSIVE_TEST |
624 | if (retval != map->m_len) { | |
625 | printk("ES len assertation failed for inode: %lu " | |
626 | "retval %d != map->m_len %d " | |
627 | "in %s (lookup)\n", inode->i_ino, retval, | |
628 | map->m_len, __func__); | |
629 | } | |
630 | #endif | |
631 | ||
f7fec032 ZL |
632 | status = map->m_flags & EXT4_MAP_UNWRITTEN ? |
633 | EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; | |
634 | if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && | |
5a7d1e16 | 635 | !(status & EXTENT_STATUS_WRITTEN) && |
f7fec032 ZL |
636 | ext4_find_delalloc_range(inode, map->m_lblk, |
637 | map->m_lblk + map->m_len - 1)) | |
638 | status |= EXTENT_STATUS_DELAYED; | |
639 | ret = ext4_es_insert_extent(inode, map->m_lblk, | |
640 | map->m_len, map->m_pblk, status); | |
641 | if (ret < 0) | |
642 | retval = ret; | |
643 | } | |
729f52c6 ZL |
644 | if (!(flags & EXT4_GET_BLOCKS_NO_LOCK)) |
645 | up_read((&EXT4_I(inode)->i_data_sem)); | |
f5ab0d1f | 646 | |
d100eef2 | 647 | found: |
e35fd660 | 648 | if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { |
f7fec032 | 649 | int ret = check_block_validity(inode, map); |
6fd058f7 TT |
650 | if (ret != 0) |
651 | return ret; | |
652 | } | |
653 | ||
f5ab0d1f | 654 | /* If it is only a block(s) look up */ |
c2177057 | 655 | if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) |
f5ab0d1f MC |
656 | return retval; |
657 | ||
658 | /* | |
659 | * Returns if the blocks have already allocated | |
660 | * | |
661 | * Note that if blocks have been preallocated | |
df3ab170 | 662 | * ext4_ext_get_block() returns the create = 0 |
f5ab0d1f MC |
663 | * with buffer head unmapped. |
664 | */ | |
e35fd660 | 665 | if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) |
4df3d265 AK |
666 | return retval; |
667 | ||
2a8964d6 | 668 | /* |
a25a4e1a ZL |
669 | * Here we clear m_flags because after allocating an new extent, |
670 | * it will be set again. | |
2a8964d6 | 671 | */ |
a25a4e1a | 672 | map->m_flags &= ~EXT4_MAP_FLAGS; |
2a8964d6 | 673 | |
4df3d265 | 674 | /* |
f5ab0d1f MC |
675 | * New blocks allocate and/or writing to uninitialized extent |
676 | * will possibly result in updating i_data, so we take | |
677 | * the write lock of i_data_sem, and call get_blocks() | |
678 | * with create == 1 flag. | |
4df3d265 AK |
679 | */ |
680 | down_write((&EXT4_I(inode)->i_data_sem)); | |
d2a17637 MC |
681 | |
682 | /* | |
683 | * if the caller is from delayed allocation writeout path | |
684 | * we have already reserved fs blocks for allocation | |
685 | * let the underlying get_block() function know to | |
686 | * avoid double accounting | |
687 | */ | |
c2177057 | 688 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) |
f2321097 | 689 | ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); |
4df3d265 AK |
690 | /* |
691 | * We need to check for EXT4 here because migrate | |
692 | * could have changed the inode type in between | |
693 | */ | |
12e9b892 | 694 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { |
e35fd660 | 695 | retval = ext4_ext_map_blocks(handle, inode, map, flags); |
0e855ac8 | 696 | } else { |
e35fd660 | 697 | retval = ext4_ind_map_blocks(handle, inode, map, flags); |
267e4db9 | 698 | |
e35fd660 | 699 | if (retval > 0 && map->m_flags & EXT4_MAP_NEW) { |
267e4db9 AK |
700 | /* |
701 | * We allocated new blocks which will result in | |
702 | * i_data's format changing. Force the migrate | |
703 | * to fail by clearing migrate flags | |
704 | */ | |
19f5fb7a | 705 | ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); |
267e4db9 | 706 | } |
d2a17637 | 707 | |
5f634d06 AK |
708 | /* |
709 | * Update reserved blocks/metadata blocks after successful | |
710 | * block allocation which had been deferred till now. We don't | |
711 | * support fallocate for non extent files. So we can update | |
712 | * reserve space here. | |
713 | */ | |
714 | if ((retval > 0) && | |
1296cc85 | 715 | (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)) |
5f634d06 AK |
716 | ext4_da_update_reserve_space(inode, retval, 1); |
717 | } | |
f7fec032 | 718 | if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) |
f2321097 | 719 | ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED); |
2ac3b6e0 | 720 | |
f7fec032 ZL |
721 | if (retval > 0) { |
722 | int ret; | |
723 | unsigned long long status; | |
724 | ||
921f266b DM |
725 | #ifdef ES_AGGRESSIVE_TEST |
726 | if (retval != map->m_len) { | |
727 | printk("ES len assertation failed for inode: %lu " | |
728 | "retval %d != map->m_len %d " | |
729 | "in %s (allocation)\n", inode->i_ino, retval, | |
730 | map->m_len, __func__); | |
731 | } | |
732 | #endif | |
733 | ||
adb23551 ZL |
734 | /* |
735 | * If the extent has been zeroed out, we don't need to update | |
736 | * extent status tree. | |
737 | */ | |
738 | if ((flags & EXT4_GET_BLOCKS_PRE_IO) && | |
739 | ext4_es_lookup_extent(inode, map->m_lblk, &es)) { | |
740 | if (ext4_es_is_written(&es)) | |
741 | goto has_zeroout; | |
742 | } | |
f7fec032 ZL |
743 | status = map->m_flags & EXT4_MAP_UNWRITTEN ? |
744 | EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; | |
745 | if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) && | |
5a7d1e16 | 746 | !(status & EXTENT_STATUS_WRITTEN) && |
f7fec032 ZL |
747 | ext4_find_delalloc_range(inode, map->m_lblk, |
748 | map->m_lblk + map->m_len - 1)) | |
749 | status |= EXTENT_STATUS_DELAYED; | |
750 | ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, | |
751 | map->m_pblk, status); | |
752 | if (ret < 0) | |
753 | retval = ret; | |
5356f261 AK |
754 | } |
755 | ||
adb23551 | 756 | has_zeroout: |
4df3d265 | 757 | up_write((&EXT4_I(inode)->i_data_sem)); |
e35fd660 | 758 | if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) { |
e29136f8 | 759 | int ret = check_block_validity(inode, map); |
6fd058f7 TT |
760 | if (ret != 0) |
761 | return ret; | |
9488a477 JK |
762 | |
763 | /* | |
764 | * Inodes with freshly allocated blocks where contents will be | |
765 | * visible after transaction commit must be on transaction's | |
766 | * ordered data list. | |
767 | */ | |
768 | if (map->m_flags & EXT4_MAP_NEW && | |
769 | !(map->m_flags & EXT4_MAP_UNWRITTEN) && | |
770 | !IS_NOQUOTA(inode) && | |
771 | ext4_should_order_data(inode)) { | |
772 | ret = ext4_jbd2_file_inode(handle, inode); | |
773 | if (ret) | |
774 | return ret; | |
775 | } | |
6fd058f7 | 776 | } |
0e855ac8 AK |
777 | return retval; |
778 | } | |
779 | ||
f3bd1f3f MC |
780 | /* Maximum number of blocks we map for direct IO at once. */ |
781 | #define DIO_MAX_BLOCKS 4096 | |
782 | ||
2ed88685 TT |
783 | static int _ext4_get_block(struct inode *inode, sector_t iblock, |
784 | struct buffer_head *bh, int flags) | |
ac27a0ec | 785 | { |
3e4fdaf8 | 786 | handle_t *handle = ext4_journal_current_handle(); |
2ed88685 | 787 | struct ext4_map_blocks map; |
7fb5409d | 788 | int ret = 0, started = 0; |
f3bd1f3f | 789 | int dio_credits; |
ac27a0ec | 790 | |
46c7f254 TM |
791 | if (ext4_has_inline_data(inode)) |
792 | return -ERANGE; | |
793 | ||
2ed88685 TT |
794 | map.m_lblk = iblock; |
795 | map.m_len = bh->b_size >> inode->i_blkbits; | |
796 | ||
8b0f165f | 797 | if (flags && !(flags & EXT4_GET_BLOCKS_NO_LOCK) && !handle) { |
7fb5409d | 798 | /* Direct IO write... */ |
2ed88685 TT |
799 | if (map.m_len > DIO_MAX_BLOCKS) |
800 | map.m_len = DIO_MAX_BLOCKS; | |
801 | dio_credits = ext4_chunk_trans_blocks(inode, map.m_len); | |
9924a92a TT |
802 | handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, |
803 | dio_credits); | |
7fb5409d | 804 | if (IS_ERR(handle)) { |
ac27a0ec | 805 | ret = PTR_ERR(handle); |
2ed88685 | 806 | return ret; |
ac27a0ec | 807 | } |
7fb5409d | 808 | started = 1; |
ac27a0ec DK |
809 | } |
810 | ||
2ed88685 | 811 | ret = ext4_map_blocks(handle, inode, &map, flags); |
7fb5409d | 812 | if (ret > 0) { |
2ed88685 TT |
813 | map_bh(bh, inode->i_sb, map.m_pblk); |
814 | bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; | |
815 | bh->b_size = inode->i_sb->s_blocksize * map.m_len; | |
7fb5409d | 816 | ret = 0; |
ac27a0ec | 817 | } |
7fb5409d JK |
818 | if (started) |
819 | ext4_journal_stop(handle); | |
ac27a0ec DK |
820 | return ret; |
821 | } | |
822 | ||
2ed88685 TT |
823 | int ext4_get_block(struct inode *inode, sector_t iblock, |
824 | struct buffer_head *bh, int create) | |
825 | { | |
826 | return _ext4_get_block(inode, iblock, bh, | |
827 | create ? EXT4_GET_BLOCKS_CREATE : 0); | |
828 | } | |
829 | ||
ac27a0ec DK |
830 | /* |
831 | * `handle' can be NULL if create is zero | |
832 | */ | |
617ba13b | 833 | struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode, |
725d26d3 | 834 | ext4_lblk_t block, int create, int *errp) |
ac27a0ec | 835 | { |
2ed88685 TT |
836 | struct ext4_map_blocks map; |
837 | struct buffer_head *bh; | |
ac27a0ec DK |
838 | int fatal = 0, err; |
839 | ||
840 | J_ASSERT(handle != NULL || create == 0); | |
841 | ||
2ed88685 TT |
842 | map.m_lblk = block; |
843 | map.m_len = 1; | |
844 | err = ext4_map_blocks(handle, inode, &map, | |
845 | create ? EXT4_GET_BLOCKS_CREATE : 0); | |
ac27a0ec | 846 | |
90b0a973 CM |
847 | /* ensure we send some value back into *errp */ |
848 | *errp = 0; | |
849 | ||
0f70b406 TT |
850 | if (create && err == 0) |
851 | err = -ENOSPC; /* should never happen */ | |
2ed88685 TT |
852 | if (err < 0) |
853 | *errp = err; | |
854 | if (err <= 0) | |
855 | return NULL; | |
2ed88685 TT |
856 | |
857 | bh = sb_getblk(inode->i_sb, map.m_pblk); | |
aebf0243 | 858 | if (unlikely(!bh)) { |
860d21e2 | 859 | *errp = -ENOMEM; |
2ed88685 | 860 | return NULL; |
ac27a0ec | 861 | } |
2ed88685 TT |
862 | if (map.m_flags & EXT4_MAP_NEW) { |
863 | J_ASSERT(create != 0); | |
864 | J_ASSERT(handle != NULL); | |
ac27a0ec | 865 | |
2ed88685 TT |
866 | /* |
867 | * Now that we do not always journal data, we should | |
868 | * keep in mind whether this should always journal the | |
869 | * new buffer as metadata. For now, regular file | |
870 | * writes use ext4_get_block instead, so it's not a | |
871 | * problem. | |
872 | */ | |
873 | lock_buffer(bh); | |
874 | BUFFER_TRACE(bh, "call get_create_access"); | |
875 | fatal = ext4_journal_get_create_access(handle, bh); | |
876 | if (!fatal && !buffer_uptodate(bh)) { | |
877 | memset(bh->b_data, 0, inode->i_sb->s_blocksize); | |
878 | set_buffer_uptodate(bh); | |
ac27a0ec | 879 | } |
2ed88685 TT |
880 | unlock_buffer(bh); |
881 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); | |
882 | err = ext4_handle_dirty_metadata(handle, inode, bh); | |
883 | if (!fatal) | |
884 | fatal = err; | |
885 | } else { | |
886 | BUFFER_TRACE(bh, "not a new buffer"); | |
ac27a0ec | 887 | } |
2ed88685 TT |
888 | if (fatal) { |
889 | *errp = fatal; | |
890 | brelse(bh); | |
891 | bh = NULL; | |
892 | } | |
893 | return bh; | |
ac27a0ec DK |
894 | } |
895 | ||
617ba13b | 896 | struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode, |
725d26d3 | 897 | ext4_lblk_t block, int create, int *err) |
ac27a0ec | 898 | { |
af5bc92d | 899 | struct buffer_head *bh; |
ac27a0ec | 900 | |
617ba13b | 901 | bh = ext4_getblk(handle, inode, block, create, err); |
ac27a0ec DK |
902 | if (!bh) |
903 | return bh; | |
904 | if (buffer_uptodate(bh)) | |
905 | return bh; | |
65299a3b | 906 | ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh); |
ac27a0ec DK |
907 | wait_on_buffer(bh); |
908 | if (buffer_uptodate(bh)) | |
909 | return bh; | |
910 | put_bh(bh); | |
911 | *err = -EIO; | |
912 | return NULL; | |
913 | } | |
914 | ||
f19d5870 TM |
915 | int ext4_walk_page_buffers(handle_t *handle, |
916 | struct buffer_head *head, | |
917 | unsigned from, | |
918 | unsigned to, | |
919 | int *partial, | |
920 | int (*fn)(handle_t *handle, | |
921 | struct buffer_head *bh)) | |
ac27a0ec DK |
922 | { |
923 | struct buffer_head *bh; | |
924 | unsigned block_start, block_end; | |
925 | unsigned blocksize = head->b_size; | |
926 | int err, ret = 0; | |
927 | struct buffer_head *next; | |
928 | ||
af5bc92d TT |
929 | for (bh = head, block_start = 0; |
930 | ret == 0 && (bh != head || !block_start); | |
de9a55b8 | 931 | block_start = block_end, bh = next) { |
ac27a0ec DK |
932 | next = bh->b_this_page; |
933 | block_end = block_start + blocksize; | |
934 | if (block_end <= from || block_start >= to) { | |
935 | if (partial && !buffer_uptodate(bh)) | |
936 | *partial = 1; | |
937 | continue; | |
938 | } | |
939 | err = (*fn)(handle, bh); | |
940 | if (!ret) | |
941 | ret = err; | |
942 | } | |
943 | return ret; | |
944 | } | |
945 | ||
946 | /* | |
947 | * To preserve ordering, it is essential that the hole instantiation and | |
948 | * the data write be encapsulated in a single transaction. We cannot | |
617ba13b | 949 | * close off a transaction and start a new one between the ext4_get_block() |
dab291af | 950 | * and the commit_write(). So doing the jbd2_journal_start at the start of |
ac27a0ec DK |
951 | * prepare_write() is the right place. |
952 | * | |
36ade451 JK |
953 | * Also, this function can nest inside ext4_writepage(). In that case, we |
954 | * *know* that ext4_writepage() has generated enough buffer credits to do the | |
955 | * whole page. So we won't block on the journal in that case, which is good, | |
956 | * because the caller may be PF_MEMALLOC. | |
ac27a0ec | 957 | * |
617ba13b | 958 | * By accident, ext4 can be reentered when a transaction is open via |
ac27a0ec DK |
959 | * quota file writes. If we were to commit the transaction while thus |
960 | * reentered, there can be a deadlock - we would be holding a quota | |
961 | * lock, and the commit would never complete if another thread had a | |
962 | * transaction open and was blocking on the quota lock - a ranking | |
963 | * violation. | |
964 | * | |
dab291af | 965 | * So what we do is to rely on the fact that jbd2_journal_stop/journal_start |
ac27a0ec DK |
966 | * will _not_ run commit under these circumstances because handle->h_ref |
967 | * is elevated. We'll still have enough credits for the tiny quotafile | |
968 | * write. | |
969 | */ | |
f19d5870 TM |
970 | int do_journal_get_write_access(handle_t *handle, |
971 | struct buffer_head *bh) | |
ac27a0ec | 972 | { |
56d35a4c JK |
973 | int dirty = buffer_dirty(bh); |
974 | int ret; | |
975 | ||
ac27a0ec DK |
976 | if (!buffer_mapped(bh) || buffer_freed(bh)) |
977 | return 0; | |
56d35a4c | 978 | /* |
ebdec241 | 979 | * __block_write_begin() could have dirtied some buffers. Clean |
56d35a4c JK |
980 | * the dirty bit as jbd2_journal_get_write_access() could complain |
981 | * otherwise about fs integrity issues. Setting of the dirty bit | |
ebdec241 | 982 | * by __block_write_begin() isn't a real problem here as we clear |
56d35a4c JK |
983 | * the bit before releasing a page lock and thus writeback cannot |
984 | * ever write the buffer. | |
985 | */ | |
986 | if (dirty) | |
987 | clear_buffer_dirty(bh); | |
988 | ret = ext4_journal_get_write_access(handle, bh); | |
989 | if (!ret && dirty) | |
990 | ret = ext4_handle_dirty_metadata(handle, NULL, bh); | |
991 | return ret; | |
ac27a0ec DK |
992 | } |
993 | ||
8b0f165f AP |
994 | static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, |
995 | struct buffer_head *bh_result, int create); | |
bfc1af65 | 996 | static int ext4_write_begin(struct file *file, struct address_space *mapping, |
de9a55b8 TT |
997 | loff_t pos, unsigned len, unsigned flags, |
998 | struct page **pagep, void **fsdata) | |
ac27a0ec | 999 | { |
af5bc92d | 1000 | struct inode *inode = mapping->host; |
1938a150 | 1001 | int ret, needed_blocks; |
ac27a0ec DK |
1002 | handle_t *handle; |
1003 | int retries = 0; | |
af5bc92d | 1004 | struct page *page; |
de9a55b8 | 1005 | pgoff_t index; |
af5bc92d | 1006 | unsigned from, to; |
6fa3eb70 S |
1007 | #if defined(FEATURE_STORAGE_PID_LOGGER) |
1008 | extern unsigned char *page_logger; | |
1009 | struct page_pid_logger *tmp_logger; | |
1010 | unsigned long page_index; | |
1011 | extern spinlock_t g_locker; | |
1012 | unsigned long g_flags; | |
1013 | #endif | |
bfc1af65 | 1014 | |
9bffad1e | 1015 | trace_ext4_write_begin(inode, pos, len, flags); |
1938a150 AK |
1016 | /* |
1017 | * Reserve one block more for addition to orphan list in case | |
1018 | * we allocate blocks but write fails for some reason | |
1019 | */ | |
1020 | needed_blocks = ext4_writepage_trans_blocks(inode) + 1; | |
de9a55b8 | 1021 | index = pos >> PAGE_CACHE_SHIFT; |
af5bc92d TT |
1022 | from = pos & (PAGE_CACHE_SIZE - 1); |
1023 | to = from + len; | |
ac27a0ec | 1024 | |
f19d5870 TM |
1025 | if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { |
1026 | ret = ext4_try_to_write_inline_data(mapping, inode, pos, len, | |
1027 | flags, pagep); | |
1028 | if (ret < 0) | |
47564bfb TT |
1029 | return ret; |
1030 | if (ret == 1) | |
1031 | return 0; | |
f19d5870 TM |
1032 | } |
1033 | ||
47564bfb TT |
1034 | /* |
1035 | * grab_cache_page_write_begin() can take a long time if the | |
1036 | * system is thrashing due to memory pressure, or if the page | |
1037 | * is being written back. So grab it first before we start | |
1038 | * the transaction handle. This also allows us to allocate | |
1039 | * the page (if needed) without using GFP_NOFS. | |
1040 | */ | |
1041 | retry_grab: | |
1042 | page = grab_cache_page_write_begin(mapping, index, flags); | |
1043 | if (!page) | |
1044 | return -ENOMEM; | |
1045 | unlock_page(page); | |
1046 | ||
1047 | retry_journal: | |
9924a92a | 1048 | handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); |
af5bc92d | 1049 | if (IS_ERR(handle)) { |
47564bfb TT |
1050 | page_cache_release(page); |
1051 | return PTR_ERR(handle); | |
7479d2b9 | 1052 | } |
ac27a0ec | 1053 | |
47564bfb TT |
1054 | lock_page(page); |
1055 | if (page->mapping != mapping) { | |
1056 | /* The page got truncated from under us */ | |
1057 | unlock_page(page); | |
1058 | page_cache_release(page); | |
cf108bca | 1059 | ext4_journal_stop(handle); |
47564bfb | 1060 | goto retry_grab; |
cf108bca | 1061 | } |
4379534c DM |
1062 | /* In case writeback began while the page was unlocked */ |
1063 | wait_for_stable_page(page); | |
cf108bca | 1064 | |
744692dc | 1065 | if (ext4_should_dioread_nolock(inode)) |
6e1db88d | 1066 | ret = __block_write_begin(page, pos, len, ext4_get_block_write); |
744692dc | 1067 | else |
6e1db88d | 1068 | ret = __block_write_begin(page, pos, len, ext4_get_block); |
bfc1af65 NP |
1069 | |
1070 | if (!ret && ext4_should_journal_data(inode)) { | |
f19d5870 TM |
1071 | ret = ext4_walk_page_buffers(handle, page_buffers(page), |
1072 | from, to, NULL, | |
1073 | do_journal_get_write_access); | |
ac27a0ec | 1074 | } |
bfc1af65 NP |
1075 | |
1076 | if (ret) { | |
af5bc92d | 1077 | unlock_page(page); |
ae4d5372 | 1078 | /* |
6e1db88d | 1079 | * __block_write_begin may have instantiated a few blocks |
ae4d5372 AK |
1080 | * outside i_size. Trim these off again. Don't need |
1081 | * i_size_read because we hold i_mutex. | |
1938a150 AK |
1082 | * |
1083 | * Add inode to orphan list in case we crash before | |
1084 | * truncate finishes | |
ae4d5372 | 1085 | */ |
ffacfa7a | 1086 | if (pos + len > inode->i_size && ext4_can_truncate(inode)) |
1938a150 AK |
1087 | ext4_orphan_add(handle, inode); |
1088 | ||
1089 | ext4_journal_stop(handle); | |
1090 | if (pos + len > inode->i_size) { | |
b9a4207d | 1091 | ext4_truncate_failed_write(inode); |
de9a55b8 | 1092 | /* |
ffacfa7a | 1093 | * If truncate failed early the inode might |
1938a150 AK |
1094 | * still be on the orphan list; we need to |
1095 | * make sure the inode is removed from the | |
1096 | * orphan list in that case. | |
1097 | */ | |
1098 | if (inode->i_nlink) | |
1099 | ext4_orphan_del(NULL, inode); | |
1100 | } | |
bfc1af65 | 1101 | |
47564bfb TT |
1102 | if (ret == -ENOSPC && |
1103 | ext4_should_retry_alloc(inode->i_sb, &retries)) | |
1104 | goto retry_journal; | |
1105 | page_cache_release(page); | |
1106 | return ret; | |
1107 | } | |
1108 | *pagep = page; | |
6fa3eb70 S |
1109 | #if defined(FEATURE_STORAGE_PID_LOGGER) |
1110 | if( page_logger && (*pagep)) { | |
1111 | //#if defined(CONFIG_FLATMEM) | |
1112 | //page_index = (unsigned long)((*pagep) - mem_map) ; | |
1113 | //#else | |
1114 | page_index = (unsigned long)(__page_to_pfn(*pagep))- PHYS_PFN_OFFSET; | |
1115 | //#endif | |
1116 | tmp_logger =((struct page_pid_logger *)page_logger) + page_index; | |
1117 | spin_lock_irqsave(&g_locker, g_flags); | |
1118 | if( page_index < num_physpages) { | |
1119 | if( tmp_logger->pid1 == 0XFFFF) | |
1120 | tmp_logger->pid1 = current->pid; | |
1121 | else if( tmp_logger->pid1 != current->pid) | |
1122 | tmp_logger->pid2 = current->pid; | |
1123 | } | |
1124 | spin_unlock_irqrestore(&g_locker, g_flags); | |
1125 | } | |
1126 | #endif | |
ac27a0ec DK |
1127 | return ret; |
1128 | } | |
1129 | ||
bfc1af65 NP |
1130 | /* For write_end() in data=journal mode */ |
1131 | static int write_end_fn(handle_t *handle, struct buffer_head *bh) | |
ac27a0ec | 1132 | { |
13fca323 | 1133 | int ret; |
ac27a0ec DK |
1134 | if (!buffer_mapped(bh) || buffer_freed(bh)) |
1135 | return 0; | |
1136 | set_buffer_uptodate(bh); | |
13fca323 TT |
1137 | ret = ext4_handle_dirty_metadata(handle, NULL, bh); |
1138 | clear_buffer_meta(bh); | |
1139 | clear_buffer_prio(bh); | |
1140 | return ret; | |
ac27a0ec DK |
1141 | } |
1142 | ||
eed4333f ZL |
1143 | /* |
1144 | * We need to pick up the new inode size which generic_commit_write gave us | |
1145 | * `file' can be NULL - eg, when called from page_symlink(). | |
1146 | * | |
1147 | * ext4 never places buffers on inode->i_mapping->private_list. metadata | |
1148 | * buffers are managed internally. | |
1149 | */ | |
1150 | static int ext4_write_end(struct file *file, | |
1151 | struct address_space *mapping, | |
1152 | loff_t pos, unsigned len, unsigned copied, | |
1153 | struct page *page, void *fsdata) | |
f8514083 | 1154 | { |
f8514083 | 1155 | handle_t *handle = ext4_journal_current_handle(); |
eed4333f ZL |
1156 | struct inode *inode = mapping->host; |
1157 | int ret = 0, ret2; | |
1158 | int i_size_changed = 0; | |
1159 | ||
1160 | trace_ext4_write_end(inode, pos, len, copied); | |
0529b225 TT |
1161 | if (ext4_has_inline_data(inode)) { |
1162 | ret = ext4_write_inline_data_end(inode, pos, len, | |
1163 | copied, page); | |
1164 | if (ret < 0) | |
1165 | goto errout; | |
1166 | copied = ret; | |
1167 | } else | |
f19d5870 TM |
1168 | copied = block_write_end(file, mapping, pos, |
1169 | len, copied, page, fsdata); | |
f8514083 AK |
1170 | |
1171 | /* | |
1172 | * No need to use i_size_read() here, the i_size | |
eed4333f | 1173 | * cannot change under us because we hole i_mutex. |
f8514083 AK |
1174 | * |
1175 | * But it's important to update i_size while still holding page lock: | |
1176 | * page writeout could otherwise come in and zero beyond i_size. | |
1177 | */ | |
1178 | if (pos + copied > inode->i_size) { | |
1179 | i_size_write(inode, pos + copied); | |
1180 | i_size_changed = 1; | |
1181 | } | |
1182 | ||
eed4333f | 1183 | if (pos + copied > EXT4_I(inode)->i_disksize) { |
f8514083 AK |
1184 | /* We need to mark inode dirty even if |
1185 | * new_i_size is less that inode->i_size | |
eed4333f | 1186 | * but greater than i_disksize. (hint delalloc) |
f8514083 AK |
1187 | */ |
1188 | ext4_update_i_disksize(inode, (pos + copied)); | |
1189 | i_size_changed = 1; | |
1190 | } | |
1191 | unlock_page(page); | |
1192 | page_cache_release(page); | |
1193 | ||
1194 | /* | |
1195 | * Don't mark the inode dirty under page lock. First, it unnecessarily | |
1196 | * makes the holding time of page lock longer. Second, it forces lock | |
1197 | * ordering of page lock and transaction start for journaling | |
1198 | * filesystems. | |
1199 | */ | |
1200 | if (i_size_changed) | |
1201 | ext4_mark_inode_dirty(handle, inode); | |
1202 | ||
74d553aa TT |
1203 | if (copied < 0) |
1204 | ret = copied; | |
ffacfa7a | 1205 | if (pos + len > inode->i_size && ext4_can_truncate(inode)) |
f8514083 AK |
1206 | /* if we have allocated more blocks and copied |
1207 | * less. We will have blocks allocated outside | |
1208 | * inode->i_size. So truncate them | |
1209 | */ | |
1210 | ext4_orphan_add(handle, inode); | |
74d553aa | 1211 | errout: |
617ba13b | 1212 | ret2 = ext4_journal_stop(handle); |
ac27a0ec DK |
1213 | if (!ret) |
1214 | ret = ret2; | |
bfc1af65 | 1215 | |
f8514083 | 1216 | if (pos + len > inode->i_size) { |
b9a4207d | 1217 | ext4_truncate_failed_write(inode); |
de9a55b8 | 1218 | /* |
ffacfa7a | 1219 | * If truncate failed early the inode might still be |
f8514083 AK |
1220 | * on the orphan list; we need to make sure the inode |
1221 | * is removed from the orphan list in that case. | |
1222 | */ | |
1223 | if (inode->i_nlink) | |
1224 | ext4_orphan_del(NULL, inode); | |
1225 | } | |
1226 | ||
bfc1af65 | 1227 | return ret ? ret : copied; |
ac27a0ec DK |
1228 | } |
1229 | ||
bfc1af65 | 1230 | static int ext4_journalled_write_end(struct file *file, |
de9a55b8 TT |
1231 | struct address_space *mapping, |
1232 | loff_t pos, unsigned len, unsigned copied, | |
1233 | struct page *page, void *fsdata) | |
ac27a0ec | 1234 | { |
617ba13b | 1235 | handle_t *handle = ext4_journal_current_handle(); |
bfc1af65 | 1236 | struct inode *inode = mapping->host; |
ac27a0ec DK |
1237 | int ret = 0, ret2; |
1238 | int partial = 0; | |
bfc1af65 | 1239 | unsigned from, to; |
cf17fea6 | 1240 | loff_t new_i_size; |
ac27a0ec | 1241 | |
9bffad1e | 1242 | trace_ext4_journalled_write_end(inode, pos, len, copied); |
bfc1af65 NP |
1243 | from = pos & (PAGE_CACHE_SIZE - 1); |
1244 | to = from + len; | |
1245 | ||
441c8508 CW |
1246 | BUG_ON(!ext4_handle_valid(handle)); |
1247 | ||
3fdcfb66 TM |
1248 | if (ext4_has_inline_data(inode)) |
1249 | copied = ext4_write_inline_data_end(inode, pos, len, | |
1250 | copied, page); | |
1251 | else { | |
1252 | if (copied < len) { | |
1253 | if (!PageUptodate(page)) | |
1254 | copied = 0; | |
1255 | page_zero_new_buffers(page, from+copied, to); | |
1256 | } | |
ac27a0ec | 1257 | |
3fdcfb66 TM |
1258 | ret = ext4_walk_page_buffers(handle, page_buffers(page), from, |
1259 | to, &partial, write_end_fn); | |
1260 | if (!partial) | |
1261 | SetPageUptodate(page); | |
1262 | } | |
cf17fea6 AK |
1263 | new_i_size = pos + copied; |
1264 | if (new_i_size > inode->i_size) | |
bfc1af65 | 1265 | i_size_write(inode, pos+copied); |
19f5fb7a | 1266 | ext4_set_inode_state(inode, EXT4_STATE_JDATA); |
2d859db3 | 1267 | EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; |
cf17fea6 AK |
1268 | if (new_i_size > EXT4_I(inode)->i_disksize) { |
1269 | ext4_update_i_disksize(inode, new_i_size); | |
617ba13b | 1270 | ret2 = ext4_mark_inode_dirty(handle, inode); |
ac27a0ec DK |
1271 | if (!ret) |
1272 | ret = ret2; | |
1273 | } | |
bfc1af65 | 1274 | |
cf108bca | 1275 | unlock_page(page); |
f8514083 | 1276 | page_cache_release(page); |
ffacfa7a | 1277 | if (pos + len > inode->i_size && ext4_can_truncate(inode)) |
f8514083 AK |
1278 | /* if we have allocated more blocks and copied |
1279 | * less. We will have blocks allocated outside | |
1280 | * inode->i_size. So truncate them | |
1281 | */ | |
1282 | ext4_orphan_add(handle, inode); | |
1283 | ||
617ba13b | 1284 | ret2 = ext4_journal_stop(handle); |
ac27a0ec DK |
1285 | if (!ret) |
1286 | ret = ret2; | |
f8514083 | 1287 | if (pos + len > inode->i_size) { |
b9a4207d | 1288 | ext4_truncate_failed_write(inode); |
de9a55b8 | 1289 | /* |
ffacfa7a | 1290 | * If truncate failed early the inode might still be |
f8514083 AK |
1291 | * on the orphan list; we need to make sure the inode |
1292 | * is removed from the orphan list in that case. | |
1293 | */ | |
1294 | if (inode->i_nlink) | |
1295 | ext4_orphan_del(NULL, inode); | |
1296 | } | |
bfc1af65 NP |
1297 | |
1298 | return ret ? ret : copied; | |
ac27a0ec | 1299 | } |
d2a17637 | 1300 | |
386ad67c LC |
1301 | /* |
1302 | * Reserve a metadata for a single block located at lblock | |
1303 | */ | |
1304 | static int ext4_da_reserve_metadata(struct inode *inode, ext4_lblk_t lblock) | |
1305 | { | |
386ad67c LC |
1306 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
1307 | struct ext4_inode_info *ei = EXT4_I(inode); | |
1308 | unsigned int md_needed; | |
1309 | ext4_lblk_t save_last_lblock; | |
1310 | int save_len; | |
1311 | ||
1312 | /* | |
1313 | * recalculate the amount of metadata blocks to reserve | |
1314 | * in order to allocate nrblocks | |
1315 | * worse case is one extent per block | |
1316 | */ | |
386ad67c LC |
1317 | spin_lock(&ei->i_block_reservation_lock); |
1318 | /* | |
1319 | * ext4_calc_metadata_amount() has side effects, which we have | |
1320 | * to be prepared undo if we fail to claim space. | |
1321 | */ | |
1322 | save_len = ei->i_da_metadata_calc_len; | |
1323 | save_last_lblock = ei->i_da_metadata_calc_last_lblock; | |
1324 | md_needed = EXT4_NUM_B2C(sbi, | |
1325 | ext4_calc_metadata_amount(inode, lblock)); | |
1326 | trace_ext4_da_reserve_space(inode, md_needed); | |
1327 | ||
1328 | /* | |
1329 | * We do still charge estimated metadata to the sb though; | |
1330 | * we cannot afford to run out of free blocks. | |
1331 | */ | |
1332 | if (ext4_claim_free_clusters(sbi, md_needed, 0)) { | |
1333 | ei->i_da_metadata_calc_len = save_len; | |
1334 | ei->i_da_metadata_calc_last_lblock = save_last_lblock; | |
1335 | spin_unlock(&ei->i_block_reservation_lock); | |
386ad67c LC |
1336 | return -ENOSPC; |
1337 | } | |
1338 | ei->i_reserved_meta_blocks += md_needed; | |
1339 | spin_unlock(&ei->i_block_reservation_lock); | |
1340 | ||
1341 | return 0; /* success */ | |
1342 | } | |
1343 | ||
9d0be502 | 1344 | /* |
7b415bf6 | 1345 | * Reserve a single cluster located at lblock |
9d0be502 | 1346 | */ |
01f49d0b | 1347 | static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock) |
d2a17637 | 1348 | { |
60e58e0f | 1349 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
0637c6f4 | 1350 | struct ext4_inode_info *ei = EXT4_I(inode); |
7b415bf6 | 1351 | unsigned int md_needed; |
5dd4056d | 1352 | int ret; |
03179fe9 TT |
1353 | ext4_lblk_t save_last_lblock; |
1354 | int save_len; | |
1355 | ||
1356 | /* | |
1357 | * We will charge metadata quota at writeout time; this saves | |
1358 | * us from metadata over-estimation, though we may go over by | |
1359 | * a small amount in the end. Here we just reserve for data. | |
1360 | */ | |
1361 | ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1)); | |
1362 | if (ret) | |
1363 | return ret; | |
d2a17637 MC |
1364 | |
1365 | /* | |
1366 | * recalculate the amount of metadata blocks to reserve | |
1367 | * in order to allocate nrblocks | |
1368 | * worse case is one extent per block | |
1369 | */ | |
0637c6f4 | 1370 | spin_lock(&ei->i_block_reservation_lock); |
03179fe9 TT |
1371 | /* |
1372 | * ext4_calc_metadata_amount() has side effects, which we have | |
1373 | * to be prepared undo if we fail to claim space. | |
1374 | */ | |
1375 | save_len = ei->i_da_metadata_calc_len; | |
1376 | save_last_lblock = ei->i_da_metadata_calc_last_lblock; | |
7b415bf6 AK |
1377 | md_needed = EXT4_NUM_B2C(sbi, |
1378 | ext4_calc_metadata_amount(inode, lblock)); | |
f8ec9d68 | 1379 | trace_ext4_da_reserve_space(inode, md_needed); |
d2a17637 | 1380 | |
72b8ab9d ES |
1381 | /* |
1382 | * We do still charge estimated metadata to the sb though; | |
1383 | * we cannot afford to run out of free blocks. | |
1384 | */ | |
e7d5f315 | 1385 | if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) { |
03179fe9 TT |
1386 | ei->i_da_metadata_calc_len = save_len; |
1387 | ei->i_da_metadata_calc_last_lblock = save_last_lblock; | |
1388 | spin_unlock(&ei->i_block_reservation_lock); | |
03179fe9 | 1389 | dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1)); |
d2a17637 MC |
1390 | return -ENOSPC; |
1391 | } | |
9d0be502 | 1392 | ei->i_reserved_data_blocks++; |
0637c6f4 TT |
1393 | ei->i_reserved_meta_blocks += md_needed; |
1394 | spin_unlock(&ei->i_block_reservation_lock); | |
39bc680a | 1395 | |
d2a17637 MC |
1396 | return 0; /* success */ |
1397 | } | |
1398 | ||
12219aea | 1399 | static void ext4_da_release_space(struct inode *inode, int to_free) |
d2a17637 MC |
1400 | { |
1401 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
0637c6f4 | 1402 | struct ext4_inode_info *ei = EXT4_I(inode); |
d2a17637 | 1403 | |
cd213226 MC |
1404 | if (!to_free) |
1405 | return; /* Nothing to release, exit */ | |
1406 | ||
d2a17637 | 1407 | spin_lock(&EXT4_I(inode)->i_block_reservation_lock); |
cd213226 | 1408 | |
5a58ec87 | 1409 | trace_ext4_da_release_space(inode, to_free); |
0637c6f4 | 1410 | if (unlikely(to_free > ei->i_reserved_data_blocks)) { |
cd213226 | 1411 | /* |
0637c6f4 TT |
1412 | * if there aren't enough reserved blocks, then the |
1413 | * counter is messed up somewhere. Since this | |
1414 | * function is called from invalidate page, it's | |
1415 | * harmless to return without any action. | |
cd213226 | 1416 | */ |
8de5c325 | 1417 | ext4_warning(inode->i_sb, "ext4_da_release_space: " |
0637c6f4 | 1418 | "ino %lu, to_free %d with only %d reserved " |
1084f252 | 1419 | "data blocks", inode->i_ino, to_free, |
0637c6f4 TT |
1420 | ei->i_reserved_data_blocks); |
1421 | WARN_ON(1); | |
1422 | to_free = ei->i_reserved_data_blocks; | |
cd213226 | 1423 | } |
0637c6f4 | 1424 | ei->i_reserved_data_blocks -= to_free; |
cd213226 | 1425 | |
0637c6f4 TT |
1426 | if (ei->i_reserved_data_blocks == 0) { |
1427 | /* | |
1428 | * We can release all of the reserved metadata blocks | |
1429 | * only when we have written all of the delayed | |
1430 | * allocation blocks. | |
7b415bf6 AK |
1431 | * Note that in case of bigalloc, i_reserved_meta_blocks, |
1432 | * i_reserved_data_blocks, etc. refer to number of clusters. | |
0637c6f4 | 1433 | */ |
57042651 | 1434 | percpu_counter_sub(&sbi->s_dirtyclusters_counter, |
72b8ab9d | 1435 | ei->i_reserved_meta_blocks); |
ee5f4d9c | 1436 | ei->i_reserved_meta_blocks = 0; |
9d0be502 | 1437 | ei->i_da_metadata_calc_len = 0; |
0637c6f4 | 1438 | } |
d2a17637 | 1439 | |
72b8ab9d | 1440 | /* update fs dirty data blocks counter */ |
57042651 | 1441 | percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free); |
d2a17637 | 1442 | |
d2a17637 | 1443 | spin_unlock(&EXT4_I(inode)->i_block_reservation_lock); |
60e58e0f | 1444 | |
7b415bf6 | 1445 | dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free)); |
d2a17637 MC |
1446 | } |
1447 | ||
1448 | static void ext4_da_page_release_reservation(struct page *page, | |
de9a55b8 | 1449 | unsigned long offset) |
d2a17637 | 1450 | { |
9b5fe3a1 | 1451 | int to_release = 0, contiguous_blks = 0; |
d2a17637 MC |
1452 | struct buffer_head *head, *bh; |
1453 | unsigned int curr_off = 0; | |
7b415bf6 AK |
1454 | struct inode *inode = page->mapping->host; |
1455 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
1456 | int num_clusters; | |
51865fda | 1457 | ext4_fsblk_t lblk; |
d2a17637 MC |
1458 | |
1459 | head = page_buffers(page); | |
1460 | bh = head; | |
1461 | do { | |
1462 | unsigned int next_off = curr_off + bh->b_size; | |
1463 | ||
1464 | if ((offset <= curr_off) && (buffer_delay(bh))) { | |
1465 | to_release++; | |
9b5fe3a1 | 1466 | contiguous_blks++; |
d2a17637 | 1467 | clear_buffer_delay(bh); |
9b5fe3a1 LC |
1468 | } else if (contiguous_blks) { |
1469 | lblk = page->index << | |
1470 | (PAGE_CACHE_SHIFT - inode->i_blkbits); | |
1471 | lblk += (curr_off >> inode->i_blkbits) - | |
1472 | contiguous_blks; | |
1473 | ext4_es_remove_extent(inode, lblk, contiguous_blks); | |
1474 | contiguous_blks = 0; | |
d2a17637 MC |
1475 | } |
1476 | curr_off = next_off; | |
1477 | } while ((bh = bh->b_this_page) != head); | |
7b415bf6 | 1478 | |
9b5fe3a1 | 1479 | if (contiguous_blks) { |
51865fda | 1480 | lblk = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits); |
9b5fe3a1 LC |
1481 | lblk += (curr_off >> inode->i_blkbits) - contiguous_blks; |
1482 | ext4_es_remove_extent(inode, lblk, contiguous_blks); | |
51865fda ZL |
1483 | } |
1484 | ||
7b415bf6 AK |
1485 | /* If we have released all the blocks belonging to a cluster, then we |
1486 | * need to release the reserved space for that cluster. */ | |
1487 | num_clusters = EXT4_NUM_B2C(sbi, to_release); | |
1488 | while (num_clusters > 0) { | |
7b415bf6 AK |
1489 | lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) + |
1490 | ((num_clusters - 1) << sbi->s_cluster_bits); | |
1491 | if (sbi->s_cluster_ratio == 1 || | |
7d1b1fbc | 1492 | !ext4_find_delalloc_cluster(inode, lblk)) |
7b415bf6 AK |
1493 | ext4_da_release_space(inode, 1); |
1494 | ||
1495 | num_clusters--; | |
1496 | } | |
d2a17637 | 1497 | } |
ac27a0ec | 1498 | |
64769240 AT |
1499 | /* |
1500 | * Delayed allocation stuff | |
1501 | */ | |
1502 | ||
64769240 AT |
1503 | /* |
1504 | * mpage_da_submit_io - walks through extent of pages and try to write | |
a1d6cc56 | 1505 | * them with writepage() call back |
64769240 AT |
1506 | * |
1507 | * @mpd->inode: inode | |
1508 | * @mpd->first_page: first page of the extent | |
1509 | * @mpd->next_page: page after the last page of the extent | |
64769240 AT |
1510 | * |
1511 | * By the time mpage_da_submit_io() is called we expect all blocks | |
1512 | * to be allocated. this may be wrong if allocation failed. | |
1513 | * | |
1514 | * As pages are already locked by write_cache_pages(), we can't use it | |
1515 | */ | |
1de3e3df TT |
1516 | static int mpage_da_submit_io(struct mpage_da_data *mpd, |
1517 | struct ext4_map_blocks *map) | |
64769240 | 1518 | { |
791b7f08 AK |
1519 | struct pagevec pvec; |
1520 | unsigned long index, end; | |
1521 | int ret = 0, err, nr_pages, i; | |
1522 | struct inode *inode = mpd->inode; | |
1523 | struct address_space *mapping = inode->i_mapping; | |
cb20d518 | 1524 | loff_t size = i_size_read(inode); |
3ecdb3a1 TT |
1525 | unsigned int len, block_start; |
1526 | struct buffer_head *bh, *page_bufs = NULL; | |
1de3e3df | 1527 | sector_t pblock = 0, cur_logical = 0; |
bd2d0210 | 1528 | struct ext4_io_submit io_submit; |
64769240 AT |
1529 | |
1530 | BUG_ON(mpd->next_page <= mpd->first_page); | |
a549984b | 1531 | memset(&io_submit, 0, sizeof(io_submit)); |
791b7f08 AK |
1532 | /* |
1533 | * We need to start from the first_page to the next_page - 1 | |
1534 | * to make sure we also write the mapped dirty buffer_heads. | |
8dc207c0 | 1535 | * If we look at mpd->b_blocknr we would only be looking |
791b7f08 AK |
1536 | * at the currently mapped buffer_heads. |
1537 | */ | |
64769240 AT |
1538 | index = mpd->first_page; |
1539 | end = mpd->next_page - 1; | |
1540 | ||
791b7f08 | 1541 | pagevec_init(&pvec, 0); |
64769240 | 1542 | while (index <= end) { |
791b7f08 | 1543 | nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); |
64769240 AT |
1544 | if (nr_pages == 0) |
1545 | break; | |
1546 | for (i = 0; i < nr_pages; i++) { | |
f8bec370 | 1547 | int skip_page = 0; |
64769240 AT |
1548 | struct page *page = pvec.pages[i]; |
1549 | ||
791b7f08 AK |
1550 | index = page->index; |
1551 | if (index > end) | |
1552 | break; | |
cb20d518 TT |
1553 | |
1554 | if (index == size >> PAGE_CACHE_SHIFT) | |
1555 | len = size & ~PAGE_CACHE_MASK; | |
1556 | else | |
1557 | len = PAGE_CACHE_SIZE; | |
1de3e3df TT |
1558 | if (map) { |
1559 | cur_logical = index << (PAGE_CACHE_SHIFT - | |
1560 | inode->i_blkbits); | |
1561 | pblock = map->m_pblk + (cur_logical - | |
1562 | map->m_lblk); | |
1563 | } | |
791b7f08 AK |
1564 | index++; |
1565 | ||
1566 | BUG_ON(!PageLocked(page)); | |
1567 | BUG_ON(PageWriteback(page)); | |
1568 | ||
3ecdb3a1 TT |
1569 | bh = page_bufs = page_buffers(page); |
1570 | block_start = 0; | |
64769240 | 1571 | do { |
1de3e3df TT |
1572 | if (map && (cur_logical >= map->m_lblk) && |
1573 | (cur_logical <= (map->m_lblk + | |
1574 | (map->m_len - 1)))) { | |
29fa89d0 AK |
1575 | if (buffer_delay(bh)) { |
1576 | clear_buffer_delay(bh); | |
1577 | bh->b_blocknr = pblock; | |
29fa89d0 | 1578 | } |
1de3e3df TT |
1579 | if (buffer_unwritten(bh) || |
1580 | buffer_mapped(bh)) | |
1581 | BUG_ON(bh->b_blocknr != pblock); | |
1582 | if (map->m_flags & EXT4_MAP_UNINIT) | |
1583 | set_buffer_uninit(bh); | |
1584 | clear_buffer_unwritten(bh); | |
1585 | } | |
29fa89d0 | 1586 | |
13a79a47 YY |
1587 | /* |
1588 | * skip page if block allocation undone and | |
1589 | * block is dirty | |
1590 | */ | |
1591 | if (ext4_bh_delay_or_unwritten(NULL, bh)) | |
97498956 | 1592 | skip_page = 1; |
3ecdb3a1 TT |
1593 | bh = bh->b_this_page; |
1594 | block_start += bh->b_size; | |
64769240 AT |
1595 | cur_logical++; |
1596 | pblock++; | |
1de3e3df TT |
1597 | } while (bh != page_bufs); |
1598 | ||
f8bec370 JK |
1599 | if (skip_page) { |
1600 | unlock_page(page); | |
1601 | continue; | |
1602 | } | |
cb20d518 | 1603 | |
97498956 | 1604 | clear_page_dirty_for_io(page); |
fe089c77 JK |
1605 | err = ext4_bio_write_page(&io_submit, page, len, |
1606 | mpd->wbc); | |
cb20d518 | 1607 | if (!err) |
a1d6cc56 | 1608 | mpd->pages_written++; |
64769240 AT |
1609 | /* |
1610 | * In error case, we have to continue because | |
1611 | * remaining pages are still locked | |
64769240 AT |
1612 | */ |
1613 | if (ret == 0) | |
1614 | ret = err; | |
64769240 AT |
1615 | } |
1616 | pagevec_release(&pvec); | |
1617 | } | |
bd2d0210 | 1618 | ext4_io_submit(&io_submit); |
64769240 | 1619 | return ret; |
64769240 AT |
1620 | } |
1621 | ||
c7f5938a | 1622 | static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd) |
c4a0c46e AK |
1623 | { |
1624 | int nr_pages, i; | |
1625 | pgoff_t index, end; | |
1626 | struct pagevec pvec; | |
1627 | struct inode *inode = mpd->inode; | |
1628 | struct address_space *mapping = inode->i_mapping; | |
51865fda | 1629 | ext4_lblk_t start, last; |
c4a0c46e | 1630 | |
c7f5938a CW |
1631 | index = mpd->first_page; |
1632 | end = mpd->next_page - 1; | |
51865fda ZL |
1633 | |
1634 | start = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); | |
1635 | last = end << (PAGE_CACHE_SHIFT - inode->i_blkbits); | |
1636 | ext4_es_remove_extent(inode, start, last - start + 1); | |
1637 | ||
66bea92c | 1638 | pagevec_init(&pvec, 0); |
c4a0c46e AK |
1639 | while (index <= end) { |
1640 | nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE); | |
1641 | if (nr_pages == 0) | |
1642 | break; | |
1643 | for (i = 0; i < nr_pages; i++) { | |
1644 | struct page *page = pvec.pages[i]; | |
9b1d0998 | 1645 | if (page->index > end) |
c4a0c46e | 1646 | break; |
c4a0c46e AK |
1647 | BUG_ON(!PageLocked(page)); |
1648 | BUG_ON(PageWriteback(page)); | |
1649 | block_invalidatepage(page, 0); | |
1650 | ClearPageUptodate(page); | |
1651 | unlock_page(page); | |
1652 | } | |
9b1d0998 JK |
1653 | index = pvec.pages[nr_pages - 1]->index + 1; |
1654 | pagevec_release(&pvec); | |
c4a0c46e AK |
1655 | } |
1656 | return; | |
1657 | } | |
1658 | ||
df22291f AK |
1659 | static void ext4_print_free_blocks(struct inode *inode) |
1660 | { | |
1661 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
92b97816 | 1662 | struct super_block *sb = inode->i_sb; |
f78ee70d | 1663 | struct ext4_inode_info *ei = EXT4_I(inode); |
92b97816 TT |
1664 | |
1665 | ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld", | |
5dee5437 | 1666 | EXT4_C2B(EXT4_SB(inode->i_sb), |
f78ee70d | 1667 | ext4_count_free_clusters(sb))); |
92b97816 TT |
1668 | ext4_msg(sb, KERN_CRIT, "Free/Dirty block details"); |
1669 | ext4_msg(sb, KERN_CRIT, "free_blocks=%lld", | |
f78ee70d | 1670 | (long long) EXT4_C2B(EXT4_SB(sb), |
57042651 | 1671 | percpu_counter_sum(&sbi->s_freeclusters_counter))); |
92b97816 | 1672 | ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld", |
f78ee70d | 1673 | (long long) EXT4_C2B(EXT4_SB(sb), |
7b415bf6 | 1674 | percpu_counter_sum(&sbi->s_dirtyclusters_counter))); |
92b97816 TT |
1675 | ext4_msg(sb, KERN_CRIT, "Block reservation details"); |
1676 | ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u", | |
f78ee70d | 1677 | ei->i_reserved_data_blocks); |
92b97816 | 1678 | ext4_msg(sb, KERN_CRIT, "i_reserved_meta_blocks=%u", |
f78ee70d LC |
1679 | ei->i_reserved_meta_blocks); |
1680 | ext4_msg(sb, KERN_CRIT, "i_allocated_meta_blocks=%u", | |
1681 | ei->i_allocated_meta_blocks); | |
df22291f AK |
1682 | return; |
1683 | } | |
1684 | ||
64769240 | 1685 | /* |
5a87b7a5 TT |
1686 | * mpage_da_map_and_submit - go through given space, map them |
1687 | * if necessary, and then submit them for I/O | |
64769240 | 1688 | * |
8dc207c0 | 1689 | * @mpd - bh describing space |
64769240 AT |
1690 | * |
1691 | * The function skips space we know is already mapped to disk blocks. | |
1692 | * | |
64769240 | 1693 | */ |
5a87b7a5 | 1694 | static void mpage_da_map_and_submit(struct mpage_da_data *mpd) |
64769240 | 1695 | { |
2ac3b6e0 | 1696 | int err, blks, get_blocks_flags; |
1de3e3df | 1697 | struct ext4_map_blocks map, *mapp = NULL; |
2fa3cdfb TT |
1698 | sector_t next = mpd->b_blocknr; |
1699 | unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits; | |
1700 | loff_t disksize = EXT4_I(mpd->inode)->i_disksize; | |
1701 | handle_t *handle = NULL; | |
64769240 AT |
1702 | |
1703 | /* | |
5a87b7a5 TT |
1704 | * If the blocks are mapped already, or we couldn't accumulate |
1705 | * any blocks, then proceed immediately to the submission stage. | |
2fa3cdfb | 1706 | */ |
5a87b7a5 TT |
1707 | if ((mpd->b_size == 0) || |
1708 | ((mpd->b_state & (1 << BH_Mapped)) && | |
1709 | !(mpd->b_state & (1 << BH_Delay)) && | |
1710 | !(mpd->b_state & (1 << BH_Unwritten)))) | |
1711 | goto submit_io; | |
2fa3cdfb TT |
1712 | |
1713 | handle = ext4_journal_current_handle(); | |
1714 | BUG_ON(!handle); | |
1715 | ||
79ffab34 | 1716 | /* |
79e83036 | 1717 | * Call ext4_map_blocks() to allocate any delayed allocation |
2ac3b6e0 TT |
1718 | * blocks, or to convert an uninitialized extent to be |
1719 | * initialized (in the case where we have written into | |
1720 | * one or more preallocated blocks). | |
1721 | * | |
1722 | * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to | |
1723 | * indicate that we are on the delayed allocation path. This | |
1724 | * affects functions in many different parts of the allocation | |
1725 | * call path. This flag exists primarily because we don't | |
79e83036 | 1726 | * want to change *many* call functions, so ext4_map_blocks() |
f2321097 | 1727 | * will set the EXT4_STATE_DELALLOC_RESERVED flag once the |
2ac3b6e0 TT |
1728 | * inode's allocation semaphore is taken. |
1729 | * | |
1730 | * If the blocks in questions were delalloc blocks, set | |
1731 | * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting | |
1732 | * variables are updated after the blocks have been allocated. | |
79ffab34 | 1733 | */ |
2ed88685 TT |
1734 | map.m_lblk = next; |
1735 | map.m_len = max_blocks; | |
27dd4385 LC |
1736 | /* |
1737 | * We're in delalloc path and it is possible that we're going to | |
1738 | * need more metadata blocks than previously reserved. However | |
1739 | * we must not fail because we're in writeback and there is | |
1740 | * nothing we can do about it so it might result in data loss. | |
1741 | * So use reserved blocks to allocate metadata if possible. | |
1742 | */ | |
1743 | get_blocks_flags = EXT4_GET_BLOCKS_CREATE | | |
1744 | EXT4_GET_BLOCKS_METADATA_NOFAIL; | |
744692dc JZ |
1745 | if (ext4_should_dioread_nolock(mpd->inode)) |
1746 | get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT; | |
2ac3b6e0 | 1747 | if (mpd->b_state & (1 << BH_Delay)) |
1296cc85 AK |
1748 | get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE; |
1749 | ||
27dd4385 | 1750 | |
2ed88685 | 1751 | blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags); |
2fa3cdfb | 1752 | if (blks < 0) { |
e3570639 ES |
1753 | struct super_block *sb = mpd->inode->i_sb; |
1754 | ||
2fa3cdfb | 1755 | err = blks; |
ed5bde0b | 1756 | /* |
5a87b7a5 | 1757 | * If get block returns EAGAIN or ENOSPC and there |
97498956 TT |
1758 | * appears to be free blocks we will just let |
1759 | * mpage_da_submit_io() unlock all of the pages. | |
c4a0c46e AK |
1760 | */ |
1761 | if (err == -EAGAIN) | |
5a87b7a5 | 1762 | goto submit_io; |
df22291f | 1763 | |
5dee5437 | 1764 | if (err == -ENOSPC && ext4_count_free_clusters(sb)) { |
df22291f | 1765 | mpd->retval = err; |
5a87b7a5 | 1766 | goto submit_io; |
df22291f AK |
1767 | } |
1768 | ||
c4a0c46e | 1769 | /* |
ed5bde0b TT |
1770 | * get block failure will cause us to loop in |
1771 | * writepages, because a_ops->writepage won't be able | |
1772 | * to make progress. The page will be redirtied by | |
1773 | * writepage and writepages will again try to write | |
1774 | * the same. | |
c4a0c46e | 1775 | */ |
e3570639 ES |
1776 | if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) { |
1777 | ext4_msg(sb, KERN_CRIT, | |
1778 | "delayed block allocation failed for inode %lu " | |
1779 | "at logical offset %llu with max blocks %zd " | |
1780 | "with error %d", mpd->inode->i_ino, | |
1781 | (unsigned long long) next, | |
1782 | mpd->b_size >> mpd->inode->i_blkbits, err); | |
1783 | ext4_msg(sb, KERN_CRIT, | |
01a523eb | 1784 | "This should not happen!! Data will be lost"); |
e3570639 ES |
1785 | if (err == -ENOSPC) |
1786 | ext4_print_free_blocks(mpd->inode); | |
030ba6bc | 1787 | } |
2fa3cdfb | 1788 | /* invalidate all the pages */ |
c7f5938a | 1789 | ext4_da_block_invalidatepages(mpd); |
e0fd9b90 CW |
1790 | |
1791 | /* Mark this page range as having been completed */ | |
1792 | mpd->io_done = 1; | |
5a87b7a5 | 1793 | return; |
c4a0c46e | 1794 | } |
2fa3cdfb TT |
1795 | BUG_ON(blks == 0); |
1796 | ||
1de3e3df | 1797 | mapp = ↦ |
2ed88685 TT |
1798 | if (map.m_flags & EXT4_MAP_NEW) { |
1799 | struct block_device *bdev = mpd->inode->i_sb->s_bdev; | |
1800 | int i; | |
64769240 | 1801 | |
2ed88685 TT |
1802 | for (i = 0; i < map.m_len; i++) |
1803 | unmap_underlying_metadata(bdev, map.m_pblk + i); | |
2fa3cdfb TT |
1804 | } |
1805 | ||
1806 | /* | |
03f5d8bc | 1807 | * Update on-disk size along with block allocation. |
2fa3cdfb TT |
1808 | */ |
1809 | disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits; | |
1810 | if (disksize > i_size_read(mpd->inode)) | |
1811 | disksize = i_size_read(mpd->inode); | |
1812 | if (disksize > EXT4_I(mpd->inode)->i_disksize) { | |
1813 | ext4_update_i_disksize(mpd->inode, disksize); | |
5a87b7a5 TT |
1814 | err = ext4_mark_inode_dirty(handle, mpd->inode); |
1815 | if (err) | |
1816 | ext4_error(mpd->inode->i_sb, | |
1817 | "Failed to mark inode %lu dirty", | |
1818 | mpd->inode->i_ino); | |
2fa3cdfb TT |
1819 | } |
1820 | ||
5a87b7a5 | 1821 | submit_io: |
1de3e3df | 1822 | mpage_da_submit_io(mpd, mapp); |
5a87b7a5 | 1823 | mpd->io_done = 1; |
64769240 AT |
1824 | } |
1825 | ||
bf068ee2 AK |
1826 | #define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \ |
1827 | (1 << BH_Delay) | (1 << BH_Unwritten)) | |
64769240 AT |
1828 | |
1829 | /* | |
1830 | * mpage_add_bh_to_extent - try to add one more block to extent of blocks | |
1831 | * | |
1832 | * @mpd->lbh - extent of blocks | |
1833 | * @logical - logical number of the block in the file | |
b6a8e62f | 1834 | * @b_state - b_state of the buffer head added |
64769240 AT |
1835 | * |
1836 | * the function is used to collect contig. blocks in same state | |
1837 | */ | |
b6a8e62f | 1838 | static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, sector_t logical, |
8dc207c0 | 1839 | unsigned long b_state) |
64769240 | 1840 | { |
64769240 | 1841 | sector_t next; |
b6a8e62f JK |
1842 | int blkbits = mpd->inode->i_blkbits; |
1843 | int nrblocks = mpd->b_size >> blkbits; | |
64769240 | 1844 | |
c445e3e0 ES |
1845 | /* |
1846 | * XXX Don't go larger than mballoc is willing to allocate | |
1847 | * This is a stopgap solution. We eventually need to fold | |
1848 | * mpage_da_submit_io() into this function and then call | |
79e83036 | 1849 | * ext4_map_blocks() multiple times in a loop |
c445e3e0 | 1850 | */ |
b6a8e62f | 1851 | if (nrblocks >= (8*1024*1024 >> blkbits)) |
c445e3e0 ES |
1852 | goto flush_it; |
1853 | ||
b6a8e62f JK |
1854 | /* check if the reserved journal credits might overflow */ |
1855 | if (!ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS)) { | |
525f4ed8 MC |
1856 | if (nrblocks >= EXT4_MAX_TRANS_DATA) { |
1857 | /* | |
1858 | * With non-extent format we are limited by the journal | |
1859 | * credit available. Total credit needed to insert | |
1860 | * nrblocks contiguous blocks is dependent on the | |
1861 | * nrblocks. So limit nrblocks. | |
1862 | */ | |
1863 | goto flush_it; | |
525f4ed8 MC |
1864 | } |
1865 | } | |
64769240 AT |
1866 | /* |
1867 | * First block in the extent | |
1868 | */ | |
8dc207c0 TT |
1869 | if (mpd->b_size == 0) { |
1870 | mpd->b_blocknr = logical; | |
b6a8e62f | 1871 | mpd->b_size = 1 << blkbits; |
8dc207c0 | 1872 | mpd->b_state = b_state & BH_FLAGS; |
64769240 AT |
1873 | return; |
1874 | } | |
1875 | ||
8dc207c0 | 1876 | next = mpd->b_blocknr + nrblocks; |
64769240 AT |
1877 | /* |
1878 | * Can we merge the block to our big extent? | |
1879 | */ | |
8dc207c0 | 1880 | if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) { |
b6a8e62f | 1881 | mpd->b_size += 1 << blkbits; |
64769240 AT |
1882 | return; |
1883 | } | |
1884 | ||
525f4ed8 | 1885 | flush_it: |
64769240 AT |
1886 | /* |
1887 | * We couldn't merge the block to our extent, so we | |
1888 | * need to flush current extent and start new one | |
1889 | */ | |
5a87b7a5 | 1890 | mpage_da_map_and_submit(mpd); |
a1d6cc56 | 1891 | return; |
64769240 AT |
1892 | } |
1893 | ||
c364b22c | 1894 | static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) |
29fa89d0 | 1895 | { |
c364b22c | 1896 | return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); |
29fa89d0 AK |
1897 | } |
1898 | ||
5356f261 AK |
1899 | /* |
1900 | * This function is grabs code from the very beginning of | |
1901 | * ext4_map_blocks, but assumes that the caller is from delayed write | |
1902 | * time. This function looks up the requested blocks and sets the | |
1903 | * buffer delay bit under the protection of i_data_sem. | |
1904 | */ | |
1905 | static int ext4_da_map_blocks(struct inode *inode, sector_t iblock, | |
1906 | struct ext4_map_blocks *map, | |
1907 | struct buffer_head *bh) | |
1908 | { | |
d100eef2 | 1909 | struct extent_status es; |
5356f261 AK |
1910 | int retval; |
1911 | sector_t invalid_block = ~((sector_t) 0xffff); | |
921f266b DM |
1912 | #ifdef ES_AGGRESSIVE_TEST |
1913 | struct ext4_map_blocks orig_map; | |
1914 | ||
1915 | memcpy(&orig_map, map, sizeof(*map)); | |
1916 | #endif | |
5356f261 AK |
1917 | |
1918 | if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es)) | |
1919 | invalid_block = ~0; | |
1920 | ||
1921 | map->m_flags = 0; | |
1922 | ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u," | |
1923 | "logical block %lu\n", inode->i_ino, map->m_len, | |
1924 | (unsigned long) map->m_lblk); | |
d100eef2 ZL |
1925 | |
1926 | /* Lookup extent status tree firstly */ | |
1927 | if (ext4_es_lookup_extent(inode, iblock, &es)) { | |
1928 | ||
1929 | if (ext4_es_is_hole(&es)) { | |
1930 | retval = 0; | |
1931 | down_read((&EXT4_I(inode)->i_data_sem)); | |
1932 | goto add_delayed; | |
1933 | } | |
1934 | ||
1935 | /* | |
1936 | * Delayed extent could be allocated by fallocate. | |
1937 | * So we need to check it. | |
1938 | */ | |
1939 | if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) { | |
1940 | map_bh(bh, inode->i_sb, invalid_block); | |
1941 | set_buffer_new(bh); | |
1942 | set_buffer_delay(bh); | |
1943 | return 0; | |
1944 | } | |
1945 | ||
1946 | map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk; | |
1947 | retval = es.es_len - (iblock - es.es_lblk); | |
1948 | if (retval > map->m_len) | |
1949 | retval = map->m_len; | |
1950 | map->m_len = retval; | |
1951 | if (ext4_es_is_written(&es)) | |
1952 | map->m_flags |= EXT4_MAP_MAPPED; | |
1953 | else if (ext4_es_is_unwritten(&es)) | |
1954 | map->m_flags |= EXT4_MAP_UNWRITTEN; | |
1955 | else | |
1956 | BUG_ON(1); | |
1957 | ||
921f266b DM |
1958 | #ifdef ES_AGGRESSIVE_TEST |
1959 | ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0); | |
1960 | #endif | |
d100eef2 ZL |
1961 | return retval; |
1962 | } | |
1963 | ||
5356f261 AK |
1964 | /* |
1965 | * Try to see if we can get the block without requesting a new | |
1966 | * file system block. | |
1967 | */ | |
1968 | down_read((&EXT4_I(inode)->i_data_sem)); | |
9c3569b5 TM |
1969 | if (ext4_has_inline_data(inode)) { |
1970 | /* | |
1971 | * We will soon create blocks for this page, and let | |
1972 | * us pretend as if the blocks aren't allocated yet. | |
1973 | * In case of clusters, we have to handle the work | |
1974 | * of mapping from cluster so that the reserved space | |
1975 | * is calculated properly. | |
1976 | */ | |
1977 | if ((EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) && | |
1978 | ext4_find_delalloc_cluster(inode, map->m_lblk)) | |
1979 | map->m_flags |= EXT4_MAP_FROM_CLUSTER; | |
1980 | retval = 0; | |
1981 | } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) | |
d100eef2 ZL |
1982 | retval = ext4_ext_map_blocks(NULL, inode, map, |
1983 | EXT4_GET_BLOCKS_NO_PUT_HOLE); | |
5356f261 | 1984 | else |
d100eef2 ZL |
1985 | retval = ext4_ind_map_blocks(NULL, inode, map, |
1986 | EXT4_GET_BLOCKS_NO_PUT_HOLE); | |
5356f261 | 1987 | |
d100eef2 | 1988 | add_delayed: |
5356f261 | 1989 | if (retval == 0) { |
f7fec032 | 1990 | int ret; |
5356f261 AK |
1991 | /* |
1992 | * XXX: __block_prepare_write() unmaps passed block, | |
1993 | * is it OK? | |
1994 | */ | |
386ad67c LC |
1995 | /* |
1996 | * If the block was allocated from previously allocated cluster, | |
1997 | * then we don't need to reserve it again. However we still need | |
1998 | * to reserve metadata for every block we're going to write. | |
1999 | */ | |
5356f261 | 2000 | if (!(map->m_flags & EXT4_MAP_FROM_CLUSTER)) { |
f7fec032 ZL |
2001 | ret = ext4_da_reserve_space(inode, iblock); |
2002 | if (ret) { | |
5356f261 | 2003 | /* not enough space to reserve */ |
f7fec032 | 2004 | retval = ret; |
5356f261 | 2005 | goto out_unlock; |
f7fec032 | 2006 | } |
386ad67c LC |
2007 | } else { |
2008 | ret = ext4_da_reserve_metadata(inode, iblock); | |
2009 | if (ret) { | |
2010 | /* not enough space to reserve */ | |
2011 | retval = ret; | |
2012 | goto out_unlock; | |
2013 | } | |
5356f261 AK |
2014 | } |
2015 | ||
f7fec032 ZL |
2016 | ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, |
2017 | ~0, EXTENT_STATUS_DELAYED); | |
2018 | if (ret) { | |
2019 | retval = ret; | |
51865fda | 2020 | goto out_unlock; |
f7fec032 | 2021 | } |
51865fda | 2022 | |
5356f261 AK |
2023 | /* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served |
2024 | * and it should not appear on the bh->b_state. | |
2025 | */ | |
2026 | map->m_flags &= ~EXT4_MAP_FROM_CLUSTER; | |
2027 | ||
2028 | map_bh(bh, inode->i_sb, invalid_block); | |
2029 | set_buffer_new(bh); | |
2030 | set_buffer_delay(bh); | |
f7fec032 ZL |
2031 | } else if (retval > 0) { |
2032 | int ret; | |
2033 | unsigned long long status; | |
2034 | ||
921f266b DM |
2035 | #ifdef ES_AGGRESSIVE_TEST |
2036 | if (retval != map->m_len) { | |
2037 | printk("ES len assertation failed for inode: %lu " | |
2038 | "retval %d != map->m_len %d " | |
2039 | "in %s (lookup)\n", inode->i_ino, retval, | |
2040 | map->m_len, __func__); | |
2041 | } | |
2042 | #endif | |
2043 | ||
f7fec032 ZL |
2044 | status = map->m_flags & EXT4_MAP_UNWRITTEN ? |
2045 | EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; | |
2046 | ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len, | |
2047 | map->m_pblk, status); | |
2048 | if (ret != 0) | |
2049 | retval = ret; | |
5356f261 AK |
2050 | } |
2051 | ||
2052 | out_unlock: | |
2053 | up_read((&EXT4_I(inode)->i_data_sem)); | |
2054 | ||
2055 | return retval; | |
2056 | } | |
2057 | ||
64769240 | 2058 | /* |
b920c755 TT |
2059 | * This is a special get_blocks_t callback which is used by |
2060 | * ext4_da_write_begin(). It will either return mapped block or | |
2061 | * reserve space for a single block. | |
29fa89d0 AK |
2062 | * |
2063 | * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set. | |
2064 | * We also have b_blocknr = -1 and b_bdev initialized properly | |
2065 | * | |
2066 | * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set. | |
2067 | * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev | |
2068 | * initialized properly. | |
64769240 | 2069 | */ |
9c3569b5 TM |
2070 | int ext4_da_get_block_prep(struct inode *inode, sector_t iblock, |
2071 | struct buffer_head *bh, int create) | |
64769240 | 2072 | { |
2ed88685 | 2073 | struct ext4_map_blocks map; |
64769240 AT |
2074 | int ret = 0; |
2075 | ||
2076 | BUG_ON(create == 0); | |
2ed88685 TT |
2077 | BUG_ON(bh->b_size != inode->i_sb->s_blocksize); |
2078 | ||
2079 | map.m_lblk = iblock; | |
2080 | map.m_len = 1; | |
64769240 AT |
2081 | |
2082 | /* | |
2083 | * first, we need to know whether the block is allocated already | |
2084 | * preallocated blocks are unmapped but should treated | |
2085 | * the same as allocated blocks. | |
2086 | */ | |
5356f261 AK |
2087 | ret = ext4_da_map_blocks(inode, iblock, &map, bh); |
2088 | if (ret <= 0) | |
2ed88685 | 2089 | return ret; |
64769240 | 2090 | |
2ed88685 TT |
2091 | map_bh(bh, inode->i_sb, map.m_pblk); |
2092 | bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags; | |
2093 | ||
2094 | if (buffer_unwritten(bh)) { | |
2095 | /* A delayed write to unwritten bh should be marked | |
2096 | * new and mapped. Mapped ensures that we don't do | |
2097 | * get_block multiple times when we write to the same | |
2098 | * offset and new ensures that we do proper zero out | |
2099 | * for partial write. | |
2100 | */ | |
2101 | set_buffer_new(bh); | |
c8205636 | 2102 | set_buffer_mapped(bh); |
2ed88685 TT |
2103 | } |
2104 | return 0; | |
64769240 | 2105 | } |
61628a3f | 2106 | |
62e086be AK |
2107 | static int bget_one(handle_t *handle, struct buffer_head *bh) |
2108 | { | |
2109 | get_bh(bh); | |
2110 | return 0; | |
2111 | } | |
2112 | ||
2113 | static int bput_one(handle_t *handle, struct buffer_head *bh) | |
2114 | { | |
2115 | put_bh(bh); | |
2116 | return 0; | |
2117 | } | |
2118 | ||
2119 | static int __ext4_journalled_writepage(struct page *page, | |
62e086be AK |
2120 | unsigned int len) |
2121 | { | |
2122 | struct address_space *mapping = page->mapping; | |
2123 | struct inode *inode = mapping->host; | |
3fdcfb66 | 2124 | struct buffer_head *page_bufs = NULL; |
62e086be | 2125 | handle_t *handle = NULL; |
3fdcfb66 TM |
2126 | int ret = 0, err = 0; |
2127 | int inline_data = ext4_has_inline_data(inode); | |
2128 | struct buffer_head *inode_bh = NULL; | |
62e086be | 2129 | |
cb20d518 | 2130 | ClearPageChecked(page); |
3fdcfb66 TM |
2131 | |
2132 | if (inline_data) { | |
2133 | BUG_ON(page->index != 0); | |
2134 | BUG_ON(len > ext4_get_max_inline_size(inode)); | |
2135 | inode_bh = ext4_journalled_write_inline_data(inode, len, page); | |
2136 | if (inode_bh == NULL) | |
2137 | goto out; | |
2138 | } else { | |
2139 | page_bufs = page_buffers(page); | |
2140 | if (!page_bufs) { | |
2141 | BUG(); | |
2142 | goto out; | |
2143 | } | |
2144 | ext4_walk_page_buffers(handle, page_bufs, 0, len, | |
2145 | NULL, bget_one); | |
2146 | } | |
58315397 TT |
2147 | /* |
2148 | * We need to release the page lock before we start the | |
2149 | * journal, so grab a reference so the page won't disappear | |
2150 | * out from under us. | |
2151 | */ | |
2152 | get_page(page); | |
62e086be AK |
2153 | unlock_page(page); |
2154 | ||
9924a92a TT |
2155 | handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, |
2156 | ext4_writepage_trans_blocks(inode)); | |
62e086be AK |
2157 | if (IS_ERR(handle)) { |
2158 | ret = PTR_ERR(handle); | |
58315397 TT |
2159 | put_page(page); |
2160 | goto out_no_pagelock; | |
62e086be | 2161 | } |
441c8508 CW |
2162 | BUG_ON(!ext4_handle_valid(handle)); |
2163 | ||
58315397 TT |
2164 | lock_page(page); |
2165 | put_page(page); | |
2166 | if (page->mapping != mapping) { | |
2167 | /* The page got truncated from under us */ | |
2168 | ext4_journal_stop(handle); | |
2169 | ret = 0; | |
2170 | goto out; | |
2171 | } | |
2172 | ||
3fdcfb66 TM |
2173 | if (inline_data) { |
2174 | ret = ext4_journal_get_write_access(handle, inode_bh); | |
62e086be | 2175 | |
3fdcfb66 TM |
2176 | err = ext4_handle_dirty_metadata(handle, inode, inode_bh); |
2177 | ||
2178 | } else { | |
2179 | ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, | |
2180 | do_journal_get_write_access); | |
2181 | ||
2182 | err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL, | |
2183 | write_end_fn); | |
2184 | } | |
62e086be AK |
2185 | if (ret == 0) |
2186 | ret = err; | |
2d859db3 | 2187 | EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid; |
62e086be AK |
2188 | err = ext4_journal_stop(handle); |
2189 | if (!ret) | |
2190 | ret = err; | |
2191 | ||
3fdcfb66 TM |
2192 | if (!ext4_has_inline_data(inode)) |
2193 | ext4_walk_page_buffers(handle, page_bufs, 0, len, | |
2194 | NULL, bput_one); | |
19f5fb7a | 2195 | ext4_set_inode_state(inode, EXT4_STATE_JDATA); |
62e086be | 2196 | out: |
58315397 TT |
2197 | unlock_page(page); |
2198 | out_no_pagelock: | |
3fdcfb66 | 2199 | brelse(inode_bh); |
62e086be AK |
2200 | return ret; |
2201 | } | |
2202 | ||
61628a3f | 2203 | /* |
43ce1d23 AK |
2204 | * Note that we don't need to start a transaction unless we're journaling data |
2205 | * because we should have holes filled from ext4_page_mkwrite(). We even don't | |
2206 | * need to file the inode to the transaction's list in ordered mode because if | |
2207 | * we are writing back data added by write(), the inode is already there and if | |
25985edc | 2208 | * we are writing back data modified via mmap(), no one guarantees in which |
43ce1d23 AK |
2209 | * transaction the data will hit the disk. In case we are journaling data, we |
2210 | * cannot start transaction directly because transaction start ranks above page | |
2211 | * lock so we have to do some magic. | |
2212 | * | |
b920c755 TT |
2213 | * This function can get called via... |
2214 | * - ext4_da_writepages after taking page lock (have journal handle) | |
2215 | * - journal_submit_inode_data_buffers (no journal handle) | |
f6463b0d | 2216 | * - shrink_page_list via the kswapd/direct reclaim (no journal handle) |
b920c755 | 2217 | * - grab_page_cache when doing write_begin (have journal handle) |
43ce1d23 AK |
2218 | * |
2219 | * We don't do any block allocation in this function. If we have page with | |
2220 | * multiple blocks we need to write those buffer_heads that are mapped. This | |
2221 | * is important for mmaped based write. So if we do with blocksize 1K | |
2222 | * truncate(f, 1024); | |
2223 | * a = mmap(f, 0, 4096); | |
2224 | * a[0] = 'a'; | |
2225 | * truncate(f, 4096); | |
2226 | * we have in the page first buffer_head mapped via page_mkwrite call back | |
90802ed9 | 2227 | * but other buffer_heads would be unmapped but dirty (dirty done via the |
43ce1d23 AK |
2228 | * do_wp_page). So writepage should write the first block. If we modify |
2229 | * the mmap area beyond 1024 we will again get a page_fault and the | |
2230 | * page_mkwrite callback will do the block allocation and mark the | |
2231 | * buffer_heads mapped. | |
2232 | * | |
2233 | * We redirty the page if we have any buffer_heads that is either delay or | |
2234 | * unwritten in the page. | |
2235 | * | |
2236 | * We can get recursively called as show below. | |
2237 | * | |
2238 | * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() -> | |
2239 | * ext4_writepage() | |
2240 | * | |
2241 | * But since we don't do any block allocation we should not deadlock. | |
2242 | * Page also have the dirty flag cleared so we don't get recurive page_lock. | |
61628a3f | 2243 | */ |
43ce1d23 | 2244 | static int ext4_writepage(struct page *page, |
62e086be | 2245 | struct writeback_control *wbc) |
64769240 | 2246 | { |
f8bec370 | 2247 | int ret = 0; |
61628a3f | 2248 | loff_t size; |
498e5f24 | 2249 | unsigned int len; |
744692dc | 2250 | struct buffer_head *page_bufs = NULL; |
61628a3f | 2251 | struct inode *inode = page->mapping->host; |
36ade451 | 2252 | struct ext4_io_submit io_submit; |
61628a3f | 2253 | |
a9c667f8 | 2254 | trace_ext4_writepage(page); |
f0e6c985 AK |
2255 | size = i_size_read(inode); |
2256 | if (page->index == size >> PAGE_CACHE_SHIFT) | |
2257 | len = size & ~PAGE_CACHE_MASK; | |
2258 | else | |
2259 | len = PAGE_CACHE_SIZE; | |
64769240 | 2260 | |
a42afc5f | 2261 | page_bufs = page_buffers(page); |
a42afc5f | 2262 | /* |
fe386132 JK |
2263 | * We cannot do block allocation or other extent handling in this |
2264 | * function. If there are buffers needing that, we have to redirty | |
2265 | * the page. But we may reach here when we do a journal commit via | |
2266 | * journal_submit_inode_data_buffers() and in that case we must write | |
2267 | * allocated buffers to achieve data=ordered mode guarantees. | |
a42afc5f | 2268 | */ |
f19d5870 TM |
2269 | if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL, |
2270 | ext4_bh_delay_or_unwritten)) { | |
f8bec370 | 2271 | redirty_page_for_writepage(wbc, page); |
fe386132 JK |
2272 | if (current->flags & PF_MEMALLOC) { |
2273 | /* | |
2274 | * For memory cleaning there's no point in writing only | |
2275 | * some buffers. So just bail out. Warn if we came here | |
2276 | * from direct reclaim. | |
2277 | */ | |
2278 | WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) | |
2279 | == PF_MEMALLOC); | |
f0e6c985 AK |
2280 | unlock_page(page); |
2281 | return 0; | |
2282 | } | |
a42afc5f | 2283 | } |
64769240 | 2284 | |
cb20d518 | 2285 | if (PageChecked(page) && ext4_should_journal_data(inode)) |
43ce1d23 AK |
2286 | /* |
2287 | * It's mmapped pagecache. Add buffers and journal it. There | |
2288 | * doesn't seem much point in redirtying the page here. | |
2289 | */ | |
3f0ca309 | 2290 | return __ext4_journalled_writepage(page, len); |
43ce1d23 | 2291 | |
a549984b | 2292 | memset(&io_submit, 0, sizeof(io_submit)); |
36ade451 JK |
2293 | ret = ext4_bio_write_page(&io_submit, page, len, wbc); |
2294 | ext4_io_submit(&io_submit); | |
64769240 AT |
2295 | return ret; |
2296 | } | |
2297 | ||
61628a3f | 2298 | /* |
525f4ed8 | 2299 | * This is called via ext4_da_writepages() to |
25985edc | 2300 | * calculate the total number of credits to reserve to fit |
525f4ed8 MC |
2301 | * a single extent allocation into a single transaction, |
2302 | * ext4_da_writpeages() will loop calling this before | |
2303 | * the block allocation. | |
61628a3f | 2304 | */ |
525f4ed8 MC |
2305 | |
2306 | static int ext4_da_writepages_trans_blocks(struct inode *inode) | |
2307 | { | |
2308 | int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; | |
2309 | ||
2310 | /* | |
2311 | * With non-extent format the journal credit needed to | |
2312 | * insert nrblocks contiguous block is dependent on | |
2313 | * number of contiguous block. So we will limit | |
2314 | * number of contiguous block to a sane value | |
2315 | */ | |
12e9b892 | 2316 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) && |
525f4ed8 MC |
2317 | (max_blocks > EXT4_MAX_TRANS_DATA)) |
2318 | max_blocks = EXT4_MAX_TRANS_DATA; | |
2319 | ||
2320 | return ext4_chunk_trans_blocks(inode, max_blocks); | |
2321 | } | |
61628a3f | 2322 | |
8e48dcfb TT |
2323 | /* |
2324 | * write_cache_pages_da - walk the list of dirty pages of the given | |
8eb9e5ce | 2325 | * address space and accumulate pages that need writing, and call |
168fc022 TT |
2326 | * mpage_da_map_and_submit to map a single contiguous memory region |
2327 | * and then write them. | |
8e48dcfb | 2328 | */ |
9c3569b5 TM |
2329 | static int write_cache_pages_da(handle_t *handle, |
2330 | struct address_space *mapping, | |
8e48dcfb | 2331 | struct writeback_control *wbc, |
72f84e65 ES |
2332 | struct mpage_da_data *mpd, |
2333 | pgoff_t *done_index) | |
8e48dcfb | 2334 | { |
4f01b02c | 2335 | struct buffer_head *bh, *head; |
168fc022 | 2336 | struct inode *inode = mapping->host; |
4f01b02c TT |
2337 | struct pagevec pvec; |
2338 | unsigned int nr_pages; | |
2339 | sector_t logical; | |
2340 | pgoff_t index, end; | |
2341 | long nr_to_write = wbc->nr_to_write; | |
2342 | int i, tag, ret = 0; | |
8e48dcfb | 2343 | |
168fc022 TT |
2344 | memset(mpd, 0, sizeof(struct mpage_da_data)); |
2345 | mpd->wbc = wbc; | |
2346 | mpd->inode = inode; | |
8e48dcfb TT |
2347 | pagevec_init(&pvec, 0); |
2348 | index = wbc->range_start >> PAGE_CACHE_SHIFT; | |
2349 | end = wbc->range_end >> PAGE_CACHE_SHIFT; | |
2350 | ||
6e6938b6 | 2351 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
5b41d924 ES |
2352 | tag = PAGECACHE_TAG_TOWRITE; |
2353 | else | |
2354 | tag = PAGECACHE_TAG_DIRTY; | |
2355 | ||
72f84e65 | 2356 | *done_index = index; |
4f01b02c | 2357 | while (index <= end) { |
5b41d924 | 2358 | nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, |
8e48dcfb TT |
2359 | min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); |
2360 | if (nr_pages == 0) | |
4f01b02c | 2361 | return 0; |
8e48dcfb TT |
2362 | |
2363 | for (i = 0; i < nr_pages; i++) { | |
2364 | struct page *page = pvec.pages[i]; | |
2365 | ||
2366 | /* | |
2367 | * At this point, the page may be truncated or | |
2368 | * invalidated (changing page->mapping to NULL), or | |
2369 | * even swizzled back from swapper_space to tmpfs file | |
2370 | * mapping. However, page->index will not change | |
2371 | * because we have a reference on the page. | |
2372 | */ | |
4f01b02c TT |
2373 | if (page->index > end) |
2374 | goto out; | |
8e48dcfb | 2375 | |
72f84e65 ES |
2376 | *done_index = page->index + 1; |
2377 | ||
78aaced3 TT |
2378 | /* |
2379 | * If we can't merge this page, and we have | |
2380 | * accumulated an contiguous region, write it | |
2381 | */ | |
2382 | if ((mpd->next_page != page->index) && | |
2383 | (mpd->next_page != mpd->first_page)) { | |
2384 | mpage_da_map_and_submit(mpd); | |
2385 | goto ret_extent_tail; | |
2386 | } | |
2387 | ||
8e48dcfb TT |
2388 | lock_page(page); |
2389 | ||
2390 | /* | |
4f01b02c TT |
2391 | * If the page is no longer dirty, or its |
2392 | * mapping no longer corresponds to inode we | |
2393 | * are writing (which means it has been | |
2394 | * truncated or invalidated), or the page is | |
2395 | * already under writeback and we are not | |
2396 | * doing a data integrity writeback, skip the page | |
8e48dcfb | 2397 | */ |
4f01b02c TT |
2398 | if (!PageDirty(page) || |
2399 | (PageWriteback(page) && | |
2400 | (wbc->sync_mode == WB_SYNC_NONE)) || | |
2401 | unlikely(page->mapping != mapping)) { | |
8e48dcfb TT |
2402 | unlock_page(page); |
2403 | continue; | |
2404 | } | |
2405 | ||
7cb1a535 | 2406 | wait_on_page_writeback(page); |
8e48dcfb | 2407 | BUG_ON(PageWriteback(page)); |
8e48dcfb | 2408 | |
9c3569b5 TM |
2409 | /* |
2410 | * If we have inline data and arrive here, it means that | |
2411 | * we will soon create the block for the 1st page, so | |
2412 | * we'd better clear the inline data here. | |
2413 | */ | |
2414 | if (ext4_has_inline_data(inode)) { | |
2415 | BUG_ON(ext4_test_inode_state(inode, | |
2416 | EXT4_STATE_MAY_INLINE_DATA)); | |
2417 | ext4_destroy_inline_data(handle, inode); | |
2418 | } | |
2419 | ||
168fc022 | 2420 | if (mpd->next_page != page->index) |
8eb9e5ce | 2421 | mpd->first_page = page->index; |
8eb9e5ce TT |
2422 | mpd->next_page = page->index + 1; |
2423 | logical = (sector_t) page->index << | |
2424 | (PAGE_CACHE_SHIFT - inode->i_blkbits); | |
2425 | ||
f8bec370 JK |
2426 | /* Add all dirty buffers to mpd */ |
2427 | head = page_buffers(page); | |
2428 | bh = head; | |
2429 | do { | |
2430 | BUG_ON(buffer_locked(bh)); | |
8eb9e5ce | 2431 | /* |
f8bec370 JK |
2432 | * We need to try to allocate unmapped blocks |
2433 | * in the same page. Otherwise we won't make | |
2434 | * progress with the page in ext4_writepage | |
8eb9e5ce | 2435 | */ |
f8bec370 JK |
2436 | if (ext4_bh_delay_or_unwritten(NULL, bh)) { |
2437 | mpage_add_bh_to_extent(mpd, logical, | |
f8bec370 JK |
2438 | bh->b_state); |
2439 | if (mpd->io_done) | |
2440 | goto ret_extent_tail; | |
2441 | } else if (buffer_dirty(bh) && | |
2442 | buffer_mapped(bh)) { | |
8eb9e5ce | 2443 | /* |
f8bec370 JK |
2444 | * mapped dirty buffer. We need to |
2445 | * update the b_state because we look | |
2446 | * at b_state in mpage_da_map_blocks. | |
2447 | * We don't update b_size because if we | |
2448 | * find an unmapped buffer_head later | |
2449 | * we need to use the b_state flag of | |
2450 | * that buffer_head. | |
8eb9e5ce | 2451 | */ |
f8bec370 JK |
2452 | if (mpd->b_size == 0) |
2453 | mpd->b_state = | |
2454 | bh->b_state & BH_FLAGS; | |
2455 | } | |
2456 | logical++; | |
2457 | } while ((bh = bh->b_this_page) != head); | |
8e48dcfb TT |
2458 | |
2459 | if (nr_to_write > 0) { | |
2460 | nr_to_write--; | |
2461 | if (nr_to_write == 0 && | |
4f01b02c | 2462 | wbc->sync_mode == WB_SYNC_NONE) |
8e48dcfb TT |
2463 | /* |
2464 | * We stop writing back only if we are | |
2465 | * not doing integrity sync. In case of | |
2466 | * integrity sync we have to keep going | |
2467 | * because someone may be concurrently | |
2468 | * dirtying pages, and we might have | |
2469 | * synced a lot of newly appeared dirty | |
2470 | * pages, but have not synced all of the | |
2471 | * old dirty pages. | |
2472 | */ | |
4f01b02c | 2473 | goto out; |
8e48dcfb TT |
2474 | } |
2475 | } | |
2476 | pagevec_release(&pvec); | |
2477 | cond_resched(); | |
2478 | } | |
4f01b02c TT |
2479 | return 0; |
2480 | ret_extent_tail: | |
2481 | ret = MPAGE_DA_EXTENT_TAIL; | |
8eb9e5ce TT |
2482 | out: |
2483 | pagevec_release(&pvec); | |
2484 | cond_resched(); | |
8e48dcfb TT |
2485 | return ret; |
2486 | } | |
2487 | ||
2488 | ||
64769240 | 2489 | static int ext4_da_writepages(struct address_space *mapping, |
a1d6cc56 | 2490 | struct writeback_control *wbc) |
64769240 | 2491 | { |
22208ded AK |
2492 | pgoff_t index; |
2493 | int range_whole = 0; | |
61628a3f | 2494 | handle_t *handle = NULL; |
df22291f | 2495 | struct mpage_da_data mpd; |
5e745b04 | 2496 | struct inode *inode = mapping->host; |
498e5f24 | 2497 | int pages_written = 0; |
55138e0b | 2498 | unsigned int max_pages; |
2acf2c26 | 2499 | int range_cyclic, cycled = 1, io_done = 0; |
55138e0b TT |
2500 | int needed_blocks, ret = 0; |
2501 | long desired_nr_to_write, nr_to_writebump = 0; | |
de89de6e | 2502 | loff_t range_start = wbc->range_start; |
5e745b04 | 2503 | struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); |
72f84e65 | 2504 | pgoff_t done_index = 0; |
5b41d924 | 2505 | pgoff_t end; |
1bce63d1 | 2506 | struct blk_plug plug; |
61628a3f | 2507 | |
9bffad1e | 2508 | trace_ext4_da_writepages(inode, wbc); |
ba80b101 | 2509 | |
61628a3f MC |
2510 | /* |
2511 | * No pages to write? This is mainly a kludge to avoid starting | |
2512 | * a transaction for special inodes like journal inode on last iput() | |
2513 | * because that could violate lock ordering on umount | |
2514 | */ | |
a1d6cc56 | 2515 | if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) |
61628a3f | 2516 | return 0; |
2a21e37e TT |
2517 | |
2518 | /* | |
2519 | * If the filesystem has aborted, it is read-only, so return | |
2520 | * right away instead of dumping stack traces later on that | |
2521 | * will obscure the real source of the problem. We test | |
4ab2f15b | 2522 | * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because |
2a21e37e TT |
2523 | * the latter could be true if the filesystem is mounted |
2524 | * read-only, and in that case, ext4_da_writepages should | |
2525 | * *never* be called, so if that ever happens, we would want | |
2526 | * the stack trace. | |
2527 | */ | |
4ab2f15b | 2528 | if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) |
2a21e37e TT |
2529 | return -EROFS; |
2530 | ||
22208ded AK |
2531 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
2532 | range_whole = 1; | |
61628a3f | 2533 | |
2acf2c26 AK |
2534 | range_cyclic = wbc->range_cyclic; |
2535 | if (wbc->range_cyclic) { | |
22208ded | 2536 | index = mapping->writeback_index; |
2acf2c26 AK |
2537 | if (index) |
2538 | cycled = 0; | |
2539 | wbc->range_start = index << PAGE_CACHE_SHIFT; | |
2540 | wbc->range_end = LLONG_MAX; | |
2541 | wbc->range_cyclic = 0; | |
5b41d924 ES |
2542 | end = -1; |
2543 | } else { | |
22208ded | 2544 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
5b41d924 ES |
2545 | end = wbc->range_end >> PAGE_CACHE_SHIFT; |
2546 | } | |
a1d6cc56 | 2547 | |
55138e0b TT |
2548 | /* |
2549 | * This works around two forms of stupidity. The first is in | |
2550 | * the writeback code, which caps the maximum number of pages | |
2551 | * written to be 1024 pages. This is wrong on multiple | |
2552 | * levels; different architectues have a different page size, | |
2553 | * which changes the maximum amount of data which gets | |
2554 | * written. Secondly, 4 megabytes is way too small. XFS | |
2555 | * forces this value to be 16 megabytes by multiplying | |
2556 | * nr_to_write parameter by four, and then relies on its | |
2557 | * allocator to allocate larger extents to make them | |
2558 | * contiguous. Unfortunately this brings us to the second | |
2559 | * stupidity, which is that ext4's mballoc code only allocates | |
2560 | * at most 2048 blocks. So we force contiguous writes up to | |
2561 | * the number of dirty blocks in the inode, or | |
2562 | * sbi->max_writeback_mb_bump whichever is smaller. | |
2563 | */ | |
2564 | max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT); | |
b443e733 ES |
2565 | if (!range_cyclic && range_whole) { |
2566 | if (wbc->nr_to_write == LONG_MAX) | |
2567 | desired_nr_to_write = wbc->nr_to_write; | |
2568 | else | |
2569 | desired_nr_to_write = wbc->nr_to_write * 8; | |
2570 | } else | |
55138e0b TT |
2571 | desired_nr_to_write = ext4_num_dirty_pages(inode, index, |
2572 | max_pages); | |
2573 | if (desired_nr_to_write > max_pages) | |
2574 | desired_nr_to_write = max_pages; | |
2575 | ||
2576 | if (wbc->nr_to_write < desired_nr_to_write) { | |
2577 | nr_to_writebump = desired_nr_to_write - wbc->nr_to_write; | |
2578 | wbc->nr_to_write = desired_nr_to_write; | |
2579 | } | |
2580 | ||
2acf2c26 | 2581 | retry: |
6e6938b6 | 2582 | if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) |
5b41d924 ES |
2583 | tag_pages_for_writeback(mapping, index, end); |
2584 | ||
1bce63d1 | 2585 | blk_start_plug(&plug); |
22208ded | 2586 | while (!ret && wbc->nr_to_write > 0) { |
a1d6cc56 AK |
2587 | |
2588 | /* | |
2589 | * we insert one extent at a time. So we need | |
2590 | * credit needed for single extent allocation. | |
2591 | * journalled mode is currently not supported | |
2592 | * by delalloc | |
2593 | */ | |
2594 | BUG_ON(ext4_should_journal_data(inode)); | |
525f4ed8 | 2595 | needed_blocks = ext4_da_writepages_trans_blocks(inode); |
a1d6cc56 | 2596 | |
61628a3f | 2597 | /* start a new transaction*/ |
9924a92a TT |
2598 | handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, |
2599 | needed_blocks); | |
61628a3f MC |
2600 | if (IS_ERR(handle)) { |
2601 | ret = PTR_ERR(handle); | |
1693918e | 2602 | ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " |
fbe845dd | 2603 | "%ld pages, ino %lu; err %d", __func__, |
a1d6cc56 | 2604 | wbc->nr_to_write, inode->i_ino, ret); |
3c1fcb2c | 2605 | blk_finish_plug(&plug); |
61628a3f MC |
2606 | goto out_writepages; |
2607 | } | |
f63e6005 TT |
2608 | |
2609 | /* | |
8eb9e5ce | 2610 | * Now call write_cache_pages_da() to find the next |
f63e6005 | 2611 | * contiguous region of logical blocks that need |
8eb9e5ce | 2612 | * blocks to be allocated by ext4 and submit them. |
f63e6005 | 2613 | */ |
9c3569b5 TM |
2614 | ret = write_cache_pages_da(handle, mapping, |
2615 | wbc, &mpd, &done_index); | |
f63e6005 | 2616 | /* |
af901ca1 | 2617 | * If we have a contiguous extent of pages and we |
f63e6005 TT |
2618 | * haven't done the I/O yet, map the blocks and submit |
2619 | * them for I/O. | |
2620 | */ | |
2621 | if (!mpd.io_done && mpd.next_page != mpd.first_page) { | |
5a87b7a5 | 2622 | mpage_da_map_and_submit(&mpd); |
f63e6005 TT |
2623 | ret = MPAGE_DA_EXTENT_TAIL; |
2624 | } | |
b3a3ca8c | 2625 | trace_ext4_da_write_pages(inode, &mpd); |
f63e6005 | 2626 | wbc->nr_to_write -= mpd.pages_written; |
df22291f | 2627 | |
61628a3f | 2628 | ext4_journal_stop(handle); |
df22291f | 2629 | |
8f64b32e | 2630 | if ((mpd.retval == -ENOSPC) && sbi->s_journal) { |
22208ded AK |
2631 | /* commit the transaction which would |
2632 | * free blocks released in the transaction | |
2633 | * and try again | |
2634 | */ | |
df22291f | 2635 | jbd2_journal_force_commit_nested(sbi->s_journal); |
22208ded AK |
2636 | ret = 0; |
2637 | } else if (ret == MPAGE_DA_EXTENT_TAIL) { | |
a1d6cc56 | 2638 | /* |
8de49e67 KM |
2639 | * Got one extent now try with rest of the pages. |
2640 | * If mpd.retval is set -EIO, journal is aborted. | |
2641 | * So we don't need to write any more. | |
a1d6cc56 | 2642 | */ |
22208ded | 2643 | pages_written += mpd.pages_written; |
8de49e67 | 2644 | ret = mpd.retval; |
2acf2c26 | 2645 | io_done = 1; |
22208ded | 2646 | } else if (wbc->nr_to_write) |
61628a3f MC |
2647 | /* |
2648 | * There is no more writeout needed | |
2649 | * or we requested for a noblocking writeout | |
2650 | * and we found the device congested | |
2651 | */ | |
61628a3f | 2652 | break; |
a1d6cc56 | 2653 | } |
1bce63d1 | 2654 | blk_finish_plug(&plug); |
2acf2c26 AK |
2655 | if (!io_done && !cycled) { |
2656 | cycled = 1; | |
2657 | index = 0; | |
2658 | wbc->range_start = index << PAGE_CACHE_SHIFT; | |
2659 | wbc->range_end = mapping->writeback_index - 1; | |
2660 | goto retry; | |
2661 | } | |
22208ded AK |
2662 | |
2663 | /* Update index */ | |
2acf2c26 | 2664 | wbc->range_cyclic = range_cyclic; |
22208ded AK |
2665 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
2666 | /* | |
2667 | * set the writeback_index so that range_cyclic | |
2668 | * mode will write it back later | |
2669 | */ | |
72f84e65 | 2670 | mapping->writeback_index = done_index; |
a1d6cc56 | 2671 | |
61628a3f | 2672 | out_writepages: |
2faf2e19 | 2673 | wbc->nr_to_write -= nr_to_writebump; |
de89de6e | 2674 | wbc->range_start = range_start; |
9bffad1e | 2675 | trace_ext4_da_writepages_result(inode, wbc, ret, pages_written); |
61628a3f | 2676 | return ret; |
64769240 AT |
2677 | } |
2678 | ||
79f0be8d AK |
2679 | static int ext4_nonda_switch(struct super_block *sb) |
2680 | { | |
5c1ff336 | 2681 | s64 free_clusters, dirty_clusters; |
79f0be8d AK |
2682 | struct ext4_sb_info *sbi = EXT4_SB(sb); |
2683 | ||
2684 | /* | |
2685 | * switch to non delalloc mode if we are running low | |
2686 | * on free block. The free block accounting via percpu | |
179f7ebf | 2687 | * counters can get slightly wrong with percpu_counter_batch getting |
79f0be8d AK |
2688 | * accumulated on each CPU without updating global counters |
2689 | * Delalloc need an accurate free block accounting. So switch | |
2690 | * to non delalloc when we are near to error range. | |
2691 | */ | |
5c1ff336 EW |
2692 | free_clusters = |
2693 | percpu_counter_read_positive(&sbi->s_freeclusters_counter); | |
2694 | dirty_clusters = | |
2695 | percpu_counter_read_positive(&sbi->s_dirtyclusters_counter); | |
00d4e736 TT |
2696 | /* |
2697 | * Start pushing delalloc when 1/2 of free blocks are dirty. | |
2698 | */ | |
5c1ff336 | 2699 | if (dirty_clusters && (free_clusters < 2 * dirty_clusters)) |
10ee27a0 | 2700 | try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE); |
00d4e736 | 2701 | |
5c1ff336 EW |
2702 | if (2 * free_clusters < 3 * dirty_clusters || |
2703 | free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) { | |
79f0be8d | 2704 | /* |
c8afb446 ES |
2705 | * free block count is less than 150% of dirty blocks |
2706 | * or free blocks is less than watermark | |
79f0be8d AK |
2707 | */ |
2708 | return 1; | |
2709 | } | |
2710 | return 0; | |
2711 | } | |
2712 | ||
1f1ccdde ES |
2713 | /* We always reserve for an inode update; the superblock could be there too */ |
2714 | static int ext4_da_write_credits(struct inode *inode, loff_t pos, unsigned len) | |
2715 | { | |
2716 | if (likely(EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, | |
2717 | EXT4_FEATURE_RO_COMPAT_LARGE_FILE))) | |
2718 | return 1; | |
2719 | ||
2720 | if (pos + len <= 0x7fffffffULL) | |
2721 | return 1; | |
2722 | ||
2723 | /* We might need to update the superblock to set LARGE_FILE */ | |
2724 | return 2; | |
2725 | } | |
2726 | ||
64769240 | 2727 | static int ext4_da_write_begin(struct file *file, struct address_space *mapping, |
de9a55b8 TT |
2728 | loff_t pos, unsigned len, unsigned flags, |
2729 | struct page **pagep, void **fsdata) | |
64769240 | 2730 | { |
72b8ab9d | 2731 | int ret, retries = 0; |
64769240 AT |
2732 | struct page *page; |
2733 | pgoff_t index; | |
64769240 AT |
2734 | struct inode *inode = mapping->host; |
2735 | handle_t *handle; | |
2736 | ||
2737 | index = pos >> PAGE_CACHE_SHIFT; | |
79f0be8d AK |
2738 | |
2739 | if (ext4_nonda_switch(inode->i_sb)) { | |
2740 | *fsdata = (void *)FALL_BACK_TO_NONDELALLOC; | |
2741 | return ext4_write_begin(file, mapping, pos, | |
2742 | len, flags, pagep, fsdata); | |
2743 | } | |
2744 | *fsdata = (void *)0; | |
9bffad1e | 2745 | trace_ext4_da_write_begin(inode, pos, len, flags); |
9c3569b5 TM |
2746 | |
2747 | if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { | |
2748 | ret = ext4_da_write_inline_data_begin(mapping, inode, | |
2749 | pos, len, flags, | |
2750 | pagep, fsdata); | |
2751 | if (ret < 0) | |
47564bfb TT |
2752 | return ret; |
2753 | if (ret == 1) | |
2754 | return 0; | |
9c3569b5 TM |
2755 | } |
2756 | ||
47564bfb TT |
2757 | /* |
2758 | * grab_cache_page_write_begin() can take a long time if the | |
2759 | * system is thrashing due to memory pressure, or if the page | |
2760 | * is being written back. So grab it first before we start | |
2761 | * the transaction handle. This also allows us to allocate | |
2762 | * the page (if needed) without using GFP_NOFS. | |
2763 | */ | |
2764 | retry_grab: | |
2765 | page = grab_cache_page_write_begin(mapping, index, flags); | |
2766 | if (!page) | |
2767 | return -ENOMEM; | |
2768 | unlock_page(page); | |
2769 | ||
64769240 AT |
2770 | /* |
2771 | * With delayed allocation, we don't log the i_disksize update | |
2772 | * if there is delayed block allocation. But we still need | |
2773 | * to journalling the i_disksize update if writes to the end | |
2774 | * of file which has an already mapped buffer. | |
2775 | */ | |
47564bfb | 2776 | retry_journal: |
1f1ccdde ES |
2777 | handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, |
2778 | ext4_da_write_credits(inode, pos, len)); | |
64769240 | 2779 | if (IS_ERR(handle)) { |
47564bfb TT |
2780 | page_cache_release(page); |
2781 | return PTR_ERR(handle); | |
64769240 AT |
2782 | } |
2783 | ||
47564bfb TT |
2784 | lock_page(page); |
2785 | if (page->mapping != mapping) { | |
2786 | /* The page got truncated from under us */ | |
2787 | unlock_page(page); | |
2788 | page_cache_release(page); | |
d5a0d4f7 | 2789 | ext4_journal_stop(handle); |
47564bfb | 2790 | goto retry_grab; |
d5a0d4f7 | 2791 | } |
47564bfb | 2792 | /* In case writeback began while the page was unlocked */ |
4379534c | 2793 | wait_for_stable_page(page); |
64769240 | 2794 | |
6e1db88d | 2795 | ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep); |
64769240 AT |
2796 | if (ret < 0) { |
2797 | unlock_page(page); | |
2798 | ext4_journal_stop(handle); | |
ae4d5372 AK |
2799 | /* |
2800 | * block_write_begin may have instantiated a few blocks | |
2801 | * outside i_size. Trim these off again. Don't need | |
2802 | * i_size_read because we hold i_mutex. | |
2803 | */ | |
2804 | if (pos + len > inode->i_size) | |
b9a4207d | 2805 | ext4_truncate_failed_write(inode); |
47564bfb TT |
2806 | |
2807 | if (ret == -ENOSPC && | |
2808 | ext4_should_retry_alloc(inode->i_sb, &retries)) | |
2809 | goto retry_journal; | |
2810 | ||
2811 | page_cache_release(page); | |
2812 | return ret; | |
64769240 AT |
2813 | } |
2814 | ||
47564bfb | 2815 | *pagep = page; |
64769240 AT |
2816 | return ret; |
2817 | } | |
2818 | ||
632eaeab MC |
2819 | /* |
2820 | * Check if we should update i_disksize | |
2821 | * when write to the end of file but not require block allocation | |
2822 | */ | |
2823 | static int ext4_da_should_update_i_disksize(struct page *page, | |
de9a55b8 | 2824 | unsigned long offset) |
632eaeab MC |
2825 | { |
2826 | struct buffer_head *bh; | |
2827 | struct inode *inode = page->mapping->host; | |
2828 | unsigned int idx; | |
2829 | int i; | |
2830 | ||
2831 | bh = page_buffers(page); | |
2832 | idx = offset >> inode->i_blkbits; | |
2833 | ||
af5bc92d | 2834 | for (i = 0; i < idx; i++) |
632eaeab MC |
2835 | bh = bh->b_this_page; |
2836 | ||
29fa89d0 | 2837 | if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh)) |
632eaeab MC |
2838 | return 0; |
2839 | return 1; | |
2840 | } | |
2841 | ||
64769240 | 2842 | static int ext4_da_write_end(struct file *file, |
de9a55b8 TT |
2843 | struct address_space *mapping, |
2844 | loff_t pos, unsigned len, unsigned copied, | |
2845 | struct page *page, void *fsdata) | |
64769240 AT |
2846 | { |
2847 | struct inode *inode = mapping->host; | |
2848 | int ret = 0, ret2; | |
2849 | handle_t *handle = ext4_journal_current_handle(); | |
2850 | loff_t new_i_size; | |
632eaeab | 2851 | unsigned long start, end; |
79f0be8d AK |
2852 | int write_mode = (int)(unsigned long)fsdata; |
2853 | ||
74d553aa TT |
2854 | if (write_mode == FALL_BACK_TO_NONDELALLOC) |
2855 | return ext4_write_end(file, mapping, pos, | |
2856 | len, copied, page, fsdata); | |
632eaeab | 2857 | |
9bffad1e | 2858 | trace_ext4_da_write_end(inode, pos, len, copied); |
632eaeab | 2859 | start = pos & (PAGE_CACHE_SIZE - 1); |
af5bc92d | 2860 | end = start + copied - 1; |
64769240 AT |
2861 | |
2862 | /* | |
2863 | * generic_write_end() will run mark_inode_dirty() if i_size | |
2864 | * changes. So let's piggyback the i_disksize mark_inode_dirty | |
2865 | * into that. | |
2866 | */ | |
64769240 | 2867 | new_i_size = pos + copied; |
ea51d132 | 2868 | if (copied && new_i_size > EXT4_I(inode)->i_disksize) { |
9c3569b5 TM |
2869 | if (ext4_has_inline_data(inode) || |
2870 | ext4_da_should_update_i_disksize(page, end)) { | |
632eaeab | 2871 | down_write(&EXT4_I(inode)->i_data_sem); |
f3b59291 | 2872 | if (new_i_size > EXT4_I(inode)->i_disksize) |
632eaeab | 2873 | EXT4_I(inode)->i_disksize = new_i_size; |
632eaeab | 2874 | up_write(&EXT4_I(inode)->i_data_sem); |
cf17fea6 AK |
2875 | /* We need to mark inode dirty even if |
2876 | * new_i_size is less that inode->i_size | |
2877 | * bu greater than i_disksize.(hint delalloc) | |
2878 | */ | |
2879 | ext4_mark_inode_dirty(handle, inode); | |
64769240 | 2880 | } |
632eaeab | 2881 | } |
9c3569b5 TM |
2882 | |
2883 | if (write_mode != CONVERT_INLINE_DATA && | |
2884 | ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) && | |
2885 | ext4_has_inline_data(inode)) | |
2886 | ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied, | |
2887 | page); | |
2888 | else | |
2889 | ret2 = generic_write_end(file, mapping, pos, len, copied, | |
64769240 | 2890 | page, fsdata); |
9c3569b5 | 2891 | |
64769240 AT |
2892 | copied = ret2; |
2893 | if (ret2 < 0) | |
2894 | ret = ret2; | |
2895 | ret2 = ext4_journal_stop(handle); | |
2896 | if (!ret) | |
2897 | ret = ret2; | |
2898 | ||
2899 | return ret ? ret : copied; | |
2900 | } | |
2901 | ||
2902 | static void ext4_da_invalidatepage(struct page *page, unsigned long offset) | |
2903 | { | |
64769240 AT |
2904 | /* |
2905 | * Drop reserved blocks | |
2906 | */ | |
2907 | BUG_ON(!PageLocked(page)); | |
2908 | if (!page_has_buffers(page)) | |
2909 | goto out; | |
2910 | ||
d2a17637 | 2911 | ext4_da_page_release_reservation(page, offset); |
64769240 AT |
2912 | |
2913 | out: | |
2914 | ext4_invalidatepage(page, offset); | |
2915 | ||
2916 | return; | |
2917 | } | |
2918 | ||
ccd2506b TT |
2919 | /* |
2920 | * Force all delayed allocation blocks to be allocated for a given inode. | |
2921 | */ | |
2922 | int ext4_alloc_da_blocks(struct inode *inode) | |
2923 | { | |
fb40ba0d TT |
2924 | trace_ext4_alloc_da_blocks(inode); |
2925 | ||
ccd2506b TT |
2926 | if (!EXT4_I(inode)->i_reserved_data_blocks && |
2927 | !EXT4_I(inode)->i_reserved_meta_blocks) | |
2928 | return 0; | |
2929 | ||
2930 | /* | |
2931 | * We do something simple for now. The filemap_flush() will | |
2932 | * also start triggering a write of the data blocks, which is | |
2933 | * not strictly speaking necessary (and for users of | |
2934 | * laptop_mode, not even desirable). However, to do otherwise | |
2935 | * would require replicating code paths in: | |
de9a55b8 | 2936 | * |
ccd2506b TT |
2937 | * ext4_da_writepages() -> |
2938 | * write_cache_pages() ---> (via passed in callback function) | |
2939 | * __mpage_da_writepage() --> | |
2940 | * mpage_add_bh_to_extent() | |
2941 | * mpage_da_map_blocks() | |
2942 | * | |
2943 | * The problem is that write_cache_pages(), located in | |
2944 | * mm/page-writeback.c, marks pages clean in preparation for | |
2945 | * doing I/O, which is not desirable if we're not planning on | |
2946 | * doing I/O at all. | |
2947 | * | |
2948 | * We could call write_cache_pages(), and then redirty all of | |
380cf090 | 2949 | * the pages by calling redirty_page_for_writepage() but that |
ccd2506b TT |
2950 | * would be ugly in the extreme. So instead we would need to |
2951 | * replicate parts of the code in the above functions, | |
25985edc | 2952 | * simplifying them because we wouldn't actually intend to |
ccd2506b TT |
2953 | * write out the pages, but rather only collect contiguous |
2954 | * logical block extents, call the multi-block allocator, and | |
2955 | * then update the buffer heads with the block allocations. | |
de9a55b8 | 2956 | * |
ccd2506b TT |
2957 | * For now, though, we'll cheat by calling filemap_flush(), |
2958 | * which will map the blocks, and start the I/O, but not | |
2959 | * actually wait for the I/O to complete. | |
2960 | */ | |
2961 | return filemap_flush(inode->i_mapping); | |
2962 | } | |
64769240 | 2963 | |
ac27a0ec DK |
2964 | /* |
2965 | * bmap() is special. It gets used by applications such as lilo and by | |
2966 | * the swapper to find the on-disk block of a specific piece of data. | |
2967 | * | |
2968 | * Naturally, this is dangerous if the block concerned is still in the | |
617ba13b | 2969 | * journal. If somebody makes a swapfile on an ext4 data-journaling |
ac27a0ec DK |
2970 | * filesystem and enables swap, then they may get a nasty shock when the |
2971 | * data getting swapped to that swapfile suddenly gets overwritten by | |
2972 | * the original zero's written out previously to the journal and | |
2973 | * awaiting writeback in the kernel's buffer cache. | |
2974 | * | |
2975 | * So, if we see any bmap calls here on a modified, data-journaled file, | |
2976 | * take extra steps to flush any blocks which might be in the cache. | |
2977 | */ | |
617ba13b | 2978 | static sector_t ext4_bmap(struct address_space *mapping, sector_t block) |
ac27a0ec DK |
2979 | { |
2980 | struct inode *inode = mapping->host; | |
2981 | journal_t *journal; | |
2982 | int err; | |
2983 | ||
46c7f254 TM |
2984 | /* |
2985 | * We can get here for an inline file via the FIBMAP ioctl | |
2986 | */ | |
2987 | if (ext4_has_inline_data(inode)) | |
2988 | return 0; | |
2989 | ||
64769240 AT |
2990 | if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && |
2991 | test_opt(inode->i_sb, DELALLOC)) { | |
2992 | /* | |
2993 | * With delalloc we want to sync the file | |
2994 | * so that we can make sure we allocate | |
2995 | * blocks for file | |
2996 | */ | |
2997 | filemap_write_and_wait(mapping); | |
2998 | } | |
2999 | ||
19f5fb7a TT |
3000 | if (EXT4_JOURNAL(inode) && |
3001 | ext4_test_inode_state(inode, EXT4_STATE_JDATA)) { | |
ac27a0ec DK |
3002 | /* |
3003 | * This is a REALLY heavyweight approach, but the use of | |
3004 | * bmap on dirty files is expected to be extremely rare: | |
3005 | * only if we run lilo or swapon on a freshly made file | |
3006 | * do we expect this to happen. | |
3007 | * | |
3008 | * (bmap requires CAP_SYS_RAWIO so this does not | |
3009 | * represent an unprivileged user DOS attack --- we'd be | |
3010 | * in trouble if mortal users could trigger this path at | |
3011 | * will.) | |
3012 | * | |
617ba13b | 3013 | * NB. EXT4_STATE_JDATA is not set on files other than |
ac27a0ec DK |
3014 | * regular files. If somebody wants to bmap a directory |
3015 | * or symlink and gets confused because the buffer | |
3016 | * hasn't yet been flushed to disk, they deserve | |
3017 | * everything they get. | |
3018 | */ | |
3019 | ||
19f5fb7a | 3020 | ext4_clear_inode_state(inode, EXT4_STATE_JDATA); |
617ba13b | 3021 | journal = EXT4_JOURNAL(inode); |
dab291af MC |
3022 | jbd2_journal_lock_updates(journal); |
3023 | err = jbd2_journal_flush(journal); | |
3024 | jbd2_journal_unlock_updates(journal); | |
ac27a0ec DK |
3025 | |
3026 | if (err) | |
3027 | return 0; | |
3028 | } | |
3029 | ||
af5bc92d | 3030 | return generic_block_bmap(mapping, block, ext4_get_block); |
ac27a0ec DK |
3031 | } |
3032 | ||
617ba13b | 3033 | static int ext4_readpage(struct file *file, struct page *page) |
ac27a0ec | 3034 | { |
46c7f254 TM |
3035 | int ret = -EAGAIN; |
3036 | struct inode *inode = page->mapping->host; | |
3037 | ||
0562e0ba | 3038 | trace_ext4_readpage(page); |
46c7f254 TM |
3039 | |
3040 | if (ext4_has_inline_data(inode)) | |
3041 | ret = ext4_readpage_inline(inode, page); | |
3042 | ||
3043 | if (ret == -EAGAIN) | |
3044 | return mpage_readpage(page, ext4_get_block); | |
3045 | ||
3046 | return ret; | |
ac27a0ec DK |
3047 | } |
3048 | ||
3049 | static int | |
617ba13b | 3050 | ext4_readpages(struct file *file, struct address_space *mapping, |
ac27a0ec DK |
3051 | struct list_head *pages, unsigned nr_pages) |
3052 | { | |
46c7f254 TM |
3053 | struct inode *inode = mapping->host; |
3054 | ||
3055 | /* If the file has inline data, no need to do readpages. */ | |
3056 | if (ext4_has_inline_data(inode)) | |
3057 | return 0; | |
3058 | ||
617ba13b | 3059 | return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); |
ac27a0ec DK |
3060 | } |
3061 | ||
617ba13b | 3062 | static void ext4_invalidatepage(struct page *page, unsigned long offset) |
ac27a0ec | 3063 | { |
0562e0ba JZ |
3064 | trace_ext4_invalidatepage(page, offset); |
3065 | ||
4520fb3c JK |
3066 | /* No journalling happens on data buffers when this function is used */ |
3067 | WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page))); | |
3068 | ||
3069 | block_invalidatepage(page, offset); | |
3070 | } | |
3071 | ||
53e87268 JK |
3072 | static int __ext4_journalled_invalidatepage(struct page *page, |
3073 | unsigned long offset) | |
4520fb3c JK |
3074 | { |
3075 | journal_t *journal = EXT4_JOURNAL(page->mapping->host); | |
3076 | ||
3077 | trace_ext4_journalled_invalidatepage(page, offset); | |
3078 | ||
ac27a0ec DK |
3079 | /* |
3080 | * If it's a full truncate we just forget about the pending dirtying | |
3081 | */ | |
3082 | if (offset == 0) | |
3083 | ClearPageChecked(page); | |
3084 | ||
53e87268 JK |
3085 | return jbd2_journal_invalidatepage(journal, page, offset); |
3086 | } | |
3087 | ||
3088 | /* Wrapper for aops... */ | |
3089 | static void ext4_journalled_invalidatepage(struct page *page, | |
3090 | unsigned long offset) | |
3091 | { | |
3092 | WARN_ON(__ext4_journalled_invalidatepage(page, offset) < 0); | |
ac27a0ec DK |
3093 | } |
3094 | ||
617ba13b | 3095 | static int ext4_releasepage(struct page *page, gfp_t wait) |
ac27a0ec | 3096 | { |
617ba13b | 3097 | journal_t *journal = EXT4_JOURNAL(page->mapping->host); |
ac27a0ec | 3098 | |
0562e0ba JZ |
3099 | trace_ext4_releasepage(page); |
3100 | ||
e1c36595 JK |
3101 | /* Page has dirty journalled data -> cannot release */ |
3102 | if (PageChecked(page)) | |
ac27a0ec | 3103 | return 0; |
0390131b FM |
3104 | if (journal) |
3105 | return jbd2_journal_try_to_free_buffers(journal, page, wait); | |
3106 | else | |
3107 | return try_to_free_buffers(page); | |
ac27a0ec DK |
3108 | } |
3109 | ||
2ed88685 TT |
3110 | /* |
3111 | * ext4_get_block used when preparing for a DIO write or buffer write. | |
3112 | * We allocate an uinitialized extent if blocks haven't been allocated. | |
3113 | * The extent will be converted to initialized after the IO is complete. | |
3114 | */ | |
f19d5870 | 3115 | int ext4_get_block_write(struct inode *inode, sector_t iblock, |
4c0425ff MC |
3116 | struct buffer_head *bh_result, int create) |
3117 | { | |
c7064ef1 | 3118 | ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n", |
8d5d02e6 | 3119 | inode->i_ino, create); |
2ed88685 TT |
3120 | return _ext4_get_block(inode, iblock, bh_result, |
3121 | EXT4_GET_BLOCKS_IO_CREATE_EXT); | |
4c0425ff MC |
3122 | } |
3123 | ||
729f52c6 | 3124 | static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock, |
8b0f165f | 3125 | struct buffer_head *bh_result, int create) |
729f52c6 | 3126 | { |
8b0f165f AP |
3127 | ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n", |
3128 | inode->i_ino, create); | |
3129 | return _ext4_get_block(inode, iblock, bh_result, | |
3130 | EXT4_GET_BLOCKS_NO_LOCK); | |
729f52c6 ZL |
3131 | } |
3132 | ||
4c0425ff | 3133 | static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, |
552ef802 CH |
3134 | ssize_t size, void *private, int ret, |
3135 | bool is_async) | |
4c0425ff | 3136 | { |
496ad9aa | 3137 | struct inode *inode = file_inode(iocb->ki_filp); |
4c0425ff | 3138 | ext4_io_end_t *io_end = iocb->private; |
4c0425ff | 3139 | |
a549984b TT |
3140 | /* if not async direct IO or dio with 0 bytes write, just return */ |
3141 | if (!io_end || !size) | |
3142 | goto out; | |
4b70df18 | 3143 | |
88635ca2 | 3144 | ext_debug("ext4_end_io_dio(): io_end 0x%p " |
ace36ad4 | 3145 | "for inode %lu, iocb 0x%p, offset %llu, size %zd\n", |
8d5d02e6 MC |
3146 | iocb->private, io_end->inode->i_ino, iocb, offset, |
3147 | size); | |
8d5d02e6 | 3148 | |
b5a7e970 | 3149 | iocb->private = NULL; |
a549984b TT |
3150 | |
3151 | /* if not aio dio with unwritten extents, just free io and return */ | |
3152 | if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) { | |
3153 | ext4_free_io_end(io_end); | |
3154 | out: | |
3155 | inode_dio_done(inode); | |
3156 | if (is_async) | |
3157 | aio_complete(iocb, ret, 0); | |
3158 | return; | |
3159 | } | |
3160 | ||
4c0425ff MC |
3161 | io_end->offset = offset; |
3162 | io_end->size = size; | |
5b3ff237 JZ |
3163 | if (is_async) { |
3164 | io_end->iocb = iocb; | |
3165 | io_end->result = ret; | |
3166 | } | |
a549984b TT |
3167 | |
3168 | ext4_add_complete_io(io_end); | |
4c0425ff | 3169 | } |
c7064ef1 | 3170 | |
4c0425ff MC |
3171 | /* |
3172 | * For ext4 extent files, ext4 will do direct-io write to holes, | |
3173 | * preallocated extents, and those write extend the file, no need to | |
3174 | * fall back to buffered IO. | |
3175 | * | |
b595076a | 3176 | * For holes, we fallocate those blocks, mark them as uninitialized |
69c499d1 | 3177 | * If those blocks were preallocated, we mark sure they are split, but |
b595076a | 3178 | * still keep the range to write as uninitialized. |
4c0425ff | 3179 | * |
69c499d1 | 3180 | * The unwritten extents will be converted to written when DIO is completed. |
8d5d02e6 | 3181 | * For async direct IO, since the IO may still pending when return, we |
25985edc | 3182 | * set up an end_io call back function, which will do the conversion |
8d5d02e6 | 3183 | * when async direct IO completed. |
4c0425ff MC |
3184 | * |
3185 | * If the O_DIRECT write will extend the file then add this inode to the | |
3186 | * orphan list. So recovery will truncate it back to the original size | |
3187 | * if the machine crashes during the write. | |
3188 | * | |
3189 | */ | |
3190 | static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb, | |
3191 | const struct iovec *iov, loff_t offset, | |
3192 | unsigned long nr_segs) | |
3193 | { | |
3194 | struct file *file = iocb->ki_filp; | |
3195 | struct inode *inode = file->f_mapping->host; | |
3196 | ssize_t ret; | |
3197 | size_t count = iov_length(iov, nr_segs); | |
69c499d1 TT |
3198 | int overwrite = 0; |
3199 | get_block_t *get_block_func = NULL; | |
3200 | int dio_flags = 0; | |
4c0425ff | 3201 | loff_t final_size = offset + count; |
729f52c6 | 3202 | |
69c499d1 TT |
3203 | /* Use the old path for reads and writes beyond i_size. */ |
3204 | if (rw != WRITE || final_size > inode->i_size) | |
3205 | return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); | |
4bd809db | 3206 | |
69c499d1 | 3207 | BUG_ON(iocb->private == NULL); |
4bd809db | 3208 | |
69c499d1 TT |
3209 | /* If we do a overwrite dio, i_mutex locking can be released */ |
3210 | overwrite = *((int *)iocb->private); | |
4bd809db | 3211 | |
69c499d1 TT |
3212 | if (overwrite) { |
3213 | atomic_inc(&inode->i_dio_count); | |
3214 | down_read(&EXT4_I(inode)->i_data_sem); | |
3215 | mutex_unlock(&inode->i_mutex); | |
3216 | } | |
8d5d02e6 | 3217 | |
69c499d1 TT |
3218 | /* |
3219 | * We could direct write to holes and fallocate. | |
3220 | * | |
3221 | * Allocated blocks to fill the hole are marked as | |
3222 | * uninitialized to prevent parallel buffered read to expose | |
3223 | * the stale data before DIO complete the data IO. | |
3224 | * | |
3225 | * As to previously fallocated extents, ext4 get_block will | |
3226 | * just simply mark the buffer mapped but still keep the | |
3227 | * extents uninitialized. | |
3228 | * | |
3229 | * For non AIO case, we will convert those unwritten extents | |
3230 | * to written after return back from blockdev_direct_IO. | |
3231 | * | |
3232 | * For async DIO, the conversion needs to be deferred when the | |
3233 | * IO is completed. The ext4 end_io callback function will be | |
3234 | * called to take care of the conversion work. Here for async | |
3235 | * case, we allocate an io_end structure to hook to the iocb. | |
3236 | */ | |
3237 | iocb->private = NULL; | |
3238 | ext4_inode_aio_set(inode, NULL); | |
3239 | if (!is_sync_kiocb(iocb)) { | |
a549984b | 3240 | ext4_io_end_t *io_end = ext4_init_io_end(inode, GFP_NOFS); |
69c499d1 TT |
3241 | if (!io_end) { |
3242 | ret = -ENOMEM; | |
3243 | goto retake_lock; | |
8b0f165f | 3244 | } |
69c499d1 | 3245 | io_end->flag |= EXT4_IO_END_DIRECT; |
a549984b | 3246 | iocb->private = io_end; |
8d5d02e6 | 3247 | /* |
69c499d1 TT |
3248 | * we save the io structure for current async direct |
3249 | * IO, so that later ext4_map_blocks() could flag the | |
3250 | * io structure whether there is a unwritten extents | |
3251 | * needs to be converted when IO is completed. | |
8d5d02e6 | 3252 | */ |
69c499d1 TT |
3253 | ext4_inode_aio_set(inode, io_end); |
3254 | } | |
4bd809db | 3255 | |
69c499d1 TT |
3256 | if (overwrite) { |
3257 | get_block_func = ext4_get_block_write_nolock; | |
3258 | } else { | |
3259 | get_block_func = ext4_get_block_write; | |
3260 | dio_flags = DIO_LOCKING; | |
3261 | } | |
3262 | ret = __blockdev_direct_IO(rw, iocb, inode, | |
3263 | inode->i_sb->s_bdev, iov, | |
3264 | offset, nr_segs, | |
3265 | get_block_func, | |
3266 | ext4_end_io_dio, | |
3267 | NULL, | |
3268 | dio_flags); | |
3269 | ||
a549984b TT |
3270 | if (iocb->private) |
3271 | ext4_inode_aio_set(inode, NULL); | |
69c499d1 | 3272 | /* |
a549984b TT |
3273 | * The io_end structure takes a reference to the inode, that |
3274 | * structure needs to be destroyed and the reference to the | |
3275 | * inode need to be dropped, when IO is complete, even with 0 | |
3276 | * byte write, or failed. | |
3277 | * | |
3278 | * In the successful AIO DIO case, the io_end structure will | |
3279 | * be destroyed and the reference to the inode will be dropped | |
3280 | * after the end_io call back function is called. | |
3281 | * | |
3282 | * In the case there is 0 byte write, or error case, since VFS | |
3283 | * direct IO won't invoke the end_io call back function, we | |
3284 | * need to free the end_io structure here. | |
69c499d1 | 3285 | */ |
a549984b TT |
3286 | if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { |
3287 | ext4_free_io_end(iocb->private); | |
3288 | iocb->private = NULL; | |
3289 | } else if (ret > 0 && !overwrite && ext4_test_inode_state(inode, | |
69c499d1 TT |
3290 | EXT4_STATE_DIO_UNWRITTEN)) { |
3291 | int err; | |
3292 | /* | |
3293 | * for non AIO case, since the IO is already | |
3294 | * completed, we could do the conversion right here | |
3295 | */ | |
3296 | err = ext4_convert_unwritten_extents(inode, | |
3297 | offset, ret); | |
3298 | if (err < 0) | |
3299 | ret = err; | |
3300 | ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); | |
3301 | } | |
4bd809db | 3302 | |
69c499d1 TT |
3303 | retake_lock: |
3304 | /* take i_mutex locking again if we do a ovewrite dio */ | |
3305 | if (overwrite) { | |
3306 | inode_dio_done(inode); | |
3307 | up_read(&EXT4_I(inode)->i_data_sem); | |
3308 | mutex_lock(&inode->i_mutex); | |
4c0425ff | 3309 | } |
8d5d02e6 | 3310 | |
69c499d1 | 3311 | return ret; |
4c0425ff MC |
3312 | } |
3313 | ||
3314 | static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb, | |
3315 | const struct iovec *iov, loff_t offset, | |
3316 | unsigned long nr_segs) | |
3317 | { | |
3318 | struct file *file = iocb->ki_filp; | |
3319 | struct inode *inode = file->f_mapping->host; | |
0562e0ba | 3320 | ssize_t ret; |
4c0425ff | 3321 | |
84ebd795 TT |
3322 | /* |
3323 | * If we are doing data journalling we don't support O_DIRECT | |
3324 | */ | |
3325 | if (ext4_should_journal_data(inode)) | |
3326 | return 0; | |
3327 | ||
46c7f254 TM |
3328 | /* Let buffer I/O handle the inline data case. */ |
3329 | if (ext4_has_inline_data(inode)) | |
3330 | return 0; | |
3331 | ||
0562e0ba | 3332 | trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw); |
12e9b892 | 3333 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
0562e0ba JZ |
3334 | ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs); |
3335 | else | |
3336 | ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs); | |
3337 | trace_ext4_direct_IO_exit(inode, offset, | |
3338 | iov_length(iov, nr_segs), rw, ret); | |
3339 | return ret; | |
4c0425ff MC |
3340 | } |
3341 | ||
ac27a0ec | 3342 | /* |
617ba13b | 3343 | * Pages can be marked dirty completely asynchronously from ext4's journalling |
ac27a0ec DK |
3344 | * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do |
3345 | * much here because ->set_page_dirty is called under VFS locks. The page is | |
3346 | * not necessarily locked. | |
3347 | * | |
3348 | * We cannot just dirty the page and leave attached buffers clean, because the | |
3349 | * buffers' dirty state is "definitive". We cannot just set the buffers dirty | |
3350 | * or jbddirty because all the journalling code will explode. | |
3351 | * | |
3352 | * So what we do is to mark the page "pending dirty" and next time writepage | |
3353 | * is called, propagate that into the buffers appropriately. | |
3354 | */ | |
617ba13b | 3355 | static int ext4_journalled_set_page_dirty(struct page *page) |
ac27a0ec DK |
3356 | { |
3357 | SetPageChecked(page); | |
3358 | return __set_page_dirty_nobuffers(page); | |
3359 | } | |
3360 | ||
74d553aa | 3361 | static const struct address_space_operations ext4_aops = { |
8ab22b9a HH |
3362 | .readpage = ext4_readpage, |
3363 | .readpages = ext4_readpages, | |
43ce1d23 | 3364 | .writepage = ext4_writepage, |
8ab22b9a | 3365 | .write_begin = ext4_write_begin, |
74d553aa | 3366 | .write_end = ext4_write_end, |
8ab22b9a HH |
3367 | .bmap = ext4_bmap, |
3368 | .invalidatepage = ext4_invalidatepage, | |
3369 | .releasepage = ext4_releasepage, | |
3370 | .direct_IO = ext4_direct_IO, | |
3371 | .migratepage = buffer_migrate_page, | |
3372 | .is_partially_uptodate = block_is_partially_uptodate, | |
aa261f54 | 3373 | .error_remove_page = generic_error_remove_page, |
ac27a0ec DK |
3374 | }; |
3375 | ||
617ba13b | 3376 | static const struct address_space_operations ext4_journalled_aops = { |
8ab22b9a HH |
3377 | .readpage = ext4_readpage, |
3378 | .readpages = ext4_readpages, | |
43ce1d23 | 3379 | .writepage = ext4_writepage, |
8ab22b9a HH |
3380 | .write_begin = ext4_write_begin, |
3381 | .write_end = ext4_journalled_write_end, | |
3382 | .set_page_dirty = ext4_journalled_set_page_dirty, | |
3383 | .bmap = ext4_bmap, | |
4520fb3c | 3384 | .invalidatepage = ext4_journalled_invalidatepage, |
8ab22b9a | 3385 | .releasepage = ext4_releasepage, |
84ebd795 | 3386 | .direct_IO = ext4_direct_IO, |
8ab22b9a | 3387 | .is_partially_uptodate = block_is_partially_uptodate, |
aa261f54 | 3388 | .error_remove_page = generic_error_remove_page, |
ac27a0ec DK |
3389 | }; |
3390 | ||
64769240 | 3391 | static const struct address_space_operations ext4_da_aops = { |
8ab22b9a HH |
3392 | .readpage = ext4_readpage, |
3393 | .readpages = ext4_readpages, | |
43ce1d23 | 3394 | .writepage = ext4_writepage, |
8ab22b9a | 3395 | .writepages = ext4_da_writepages, |
8ab22b9a HH |
3396 | .write_begin = ext4_da_write_begin, |
3397 | .write_end = ext4_da_write_end, | |
3398 | .bmap = ext4_bmap, | |
3399 | .invalidatepage = ext4_da_invalidatepage, | |
3400 | .releasepage = ext4_releasepage, | |
3401 | .direct_IO = ext4_direct_IO, | |
3402 | .migratepage = buffer_migrate_page, | |
3403 | .is_partially_uptodate = block_is_partially_uptodate, | |
aa261f54 | 3404 | .error_remove_page = generic_error_remove_page, |
64769240 AT |
3405 | }; |
3406 | ||
617ba13b | 3407 | void ext4_set_aops(struct inode *inode) |
ac27a0ec | 3408 | { |
3d2b1582 LC |
3409 | switch (ext4_inode_journal_mode(inode)) { |
3410 | case EXT4_INODE_ORDERED_DATA_MODE: | |
74d553aa | 3411 | ext4_set_inode_state(inode, EXT4_STATE_ORDERED_MODE); |
3d2b1582 LC |
3412 | break; |
3413 | case EXT4_INODE_WRITEBACK_DATA_MODE: | |
74d553aa | 3414 | ext4_clear_inode_state(inode, EXT4_STATE_ORDERED_MODE); |
3d2b1582 LC |
3415 | break; |
3416 | case EXT4_INODE_JOURNAL_DATA_MODE: | |
617ba13b | 3417 | inode->i_mapping->a_ops = &ext4_journalled_aops; |
74d553aa | 3418 | return; |
3d2b1582 LC |
3419 | default: |
3420 | BUG(); | |
3421 | } | |
74d553aa TT |
3422 | if (test_opt(inode->i_sb, DELALLOC)) |
3423 | inode->i_mapping->a_ops = &ext4_da_aops; | |
3424 | else | |
3425 | inode->i_mapping->a_ops = &ext4_aops; | |
ac27a0ec DK |
3426 | } |
3427 | ||
4e96b2db AH |
3428 | |
3429 | /* | |
3430 | * ext4_discard_partial_page_buffers() | |
3431 | * Wrapper function for ext4_discard_partial_page_buffers_no_lock. | |
3432 | * This function finds and locks the page containing the offset | |
3433 | * "from" and passes it to ext4_discard_partial_page_buffers_no_lock. | |
3434 | * Calling functions that already have the page locked should call | |
3435 | * ext4_discard_partial_page_buffers_no_lock directly. | |
3436 | */ | |
3437 | int ext4_discard_partial_page_buffers(handle_t *handle, | |
3438 | struct address_space *mapping, loff_t from, | |
3439 | loff_t length, int flags) | |
3440 | { | |
3441 | struct inode *inode = mapping->host; | |
3442 | struct page *page; | |
3443 | int err = 0; | |
3444 | ||
3445 | page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, | |
3446 | mapping_gfp_mask(mapping) & ~__GFP_FS); | |
3447 | if (!page) | |
5129d05f | 3448 | return -ENOMEM; |
4e96b2db AH |
3449 | |
3450 | err = ext4_discard_partial_page_buffers_no_lock(handle, inode, page, | |
3451 | from, length, flags); | |
3452 | ||
3453 | unlock_page(page); | |
3454 | page_cache_release(page); | |
3455 | return err; | |
3456 | } | |
3457 | ||
3458 | /* | |
3459 | * ext4_discard_partial_page_buffers_no_lock() | |
3460 | * Zeros a page range of length 'length' starting from offset 'from'. | |
3461 | * Buffer heads that correspond to the block aligned regions of the | |
3462 | * zeroed range will be unmapped. Unblock aligned regions | |
3463 | * will have the corresponding buffer head mapped if needed so that | |
3464 | * that region of the page can be updated with the partial zero out. | |
3465 | * | |
3466 | * This function assumes that the page has already been locked. The | |
3467 | * The range to be discarded must be contained with in the given page. | |
3468 | * If the specified range exceeds the end of the page it will be shortened | |
3469 | * to the end of the page that corresponds to 'from'. This function is | |
3470 | * appropriate for updating a page and it buffer heads to be unmapped and | |
3471 | * zeroed for blocks that have been either released, or are going to be | |
3472 | * released. | |
3473 | * | |
3474 | * handle: The journal handle | |
3475 | * inode: The files inode | |
3476 | * page: A locked page that contains the offset "from" | |
4907cb7b | 3477 | * from: The starting byte offset (from the beginning of the file) |
4e96b2db AH |
3478 | * to begin discarding |
3479 | * len: The length of bytes to discard | |
3480 | * flags: Optional flags that may be used: | |
3481 | * | |
3482 | * EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED | |
3483 | * Only zero the regions of the page whose buffer heads | |
3484 | * have already been unmapped. This flag is appropriate | |
4907cb7b | 3485 | * for updating the contents of a page whose blocks may |
4e96b2db AH |
3486 | * have already been released, and we only want to zero |
3487 | * out the regions that correspond to those released blocks. | |
3488 | * | |
4907cb7b | 3489 | * Returns zero on success or negative on failure. |
4e96b2db | 3490 | */ |
5f163cc7 | 3491 | static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle, |
4e96b2db AH |
3492 | struct inode *inode, struct page *page, loff_t from, |
3493 | loff_t length, int flags) | |
3494 | { | |
3495 | ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; | |
3496 | unsigned int offset = from & (PAGE_CACHE_SIZE-1); | |
3497 | unsigned int blocksize, max, pos; | |
4e96b2db AH |
3498 | ext4_lblk_t iblock; |
3499 | struct buffer_head *bh; | |
3500 | int err = 0; | |
3501 | ||
3502 | blocksize = inode->i_sb->s_blocksize; | |
3503 | max = PAGE_CACHE_SIZE - offset; | |
3504 | ||
3505 | if (index != page->index) | |
3506 | return -EINVAL; | |
3507 | ||
3508 | /* | |
3509 | * correct length if it does not fall between | |
3510 | * 'from' and the end of the page | |
3511 | */ | |
3512 | if (length > max || length < 0) | |
3513 | length = max; | |
3514 | ||
3515 | iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits); | |
3516 | ||
093e6e36 YY |
3517 | if (!page_has_buffers(page)) |
3518 | create_empty_buffers(page, blocksize, 0); | |
4e96b2db AH |
3519 | |
3520 | /* Find the buffer that contains "offset" */ | |
3521 | bh = page_buffers(page); | |
3522 | pos = blocksize; | |
3523 | while (offset >= pos) { | |
3524 | bh = bh->b_this_page; | |
3525 | iblock++; | |
3526 | pos += blocksize; | |
3527 | } | |
3528 | ||
3529 | pos = offset; | |
3530 | while (pos < offset + length) { | |
e260daf2 YY |
3531 | unsigned int end_of_block, range_to_discard; |
3532 | ||
4e96b2db AH |
3533 | err = 0; |
3534 | ||
3535 | /* The length of space left to zero and unmap */ | |
3536 | range_to_discard = offset + length - pos; | |
3537 | ||
3538 | /* The length of space until the end of the block */ | |
3539 | end_of_block = blocksize - (pos & (blocksize-1)); | |
3540 | ||
3541 | /* | |
3542 | * Do not unmap or zero past end of block | |
3543 | * for this buffer head | |
3544 | */ | |
3545 | if (range_to_discard > end_of_block) | |
3546 | range_to_discard = end_of_block; | |
3547 | ||
3548 | ||
3549 | /* | |
3550 | * Skip this buffer head if we are only zeroing unampped | |
3551 | * regions of the page | |
3552 | */ | |
3553 | if (flags & EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED && | |
3554 | buffer_mapped(bh)) | |
3555 | goto next; | |
3556 | ||
3557 | /* If the range is block aligned, unmap */ | |
3558 | if (range_to_discard == blocksize) { | |
3559 | clear_buffer_dirty(bh); | |
3560 | bh->b_bdev = NULL; | |
3561 | clear_buffer_mapped(bh); | |
3562 | clear_buffer_req(bh); | |
3563 | clear_buffer_new(bh); | |
3564 | clear_buffer_delay(bh); | |
3565 | clear_buffer_unwritten(bh); | |
3566 | clear_buffer_uptodate(bh); | |
3567 | zero_user(page, pos, range_to_discard); | |
3568 | BUFFER_TRACE(bh, "Buffer discarded"); | |
3569 | goto next; | |
3570 | } | |
3571 | ||
3572 | /* | |
3573 | * If this block is not completely contained in the range | |
3574 | * to be discarded, then it is not going to be released. Because | |
3575 | * we need to keep this block, we need to make sure this part | |
3576 | * of the page is uptodate before we modify it by writeing | |
3577 | * partial zeros on it. | |
3578 | */ | |
3579 | if (!buffer_mapped(bh)) { | |
3580 | /* | |
3581 | * Buffer head must be mapped before we can read | |
3582 | * from the block | |
3583 | */ | |
3584 | BUFFER_TRACE(bh, "unmapped"); | |
3585 | ext4_get_block(inode, iblock, bh, 0); | |
3586 | /* unmapped? It's a hole - nothing to do */ | |
3587 | if (!buffer_mapped(bh)) { | |
3588 | BUFFER_TRACE(bh, "still unmapped"); | |
3589 | goto next; | |
3590 | } | |
3591 | } | |
3592 | ||
3593 | /* Ok, it's mapped. Make sure it's up-to-date */ | |
3594 | if (PageUptodate(page)) | |
3595 | set_buffer_uptodate(bh); | |
3596 | ||
3597 | if (!buffer_uptodate(bh)) { | |
3598 | err = -EIO; | |
3599 | ll_rw_block(READ, 1, &bh); | |
3600 | wait_on_buffer(bh); | |
3601 | /* Uhhuh. Read error. Complain and punt.*/ | |
3602 | if (!buffer_uptodate(bh)) | |
3603 | goto next; | |
3604 | } | |
3605 | ||
3606 | if (ext4_should_journal_data(inode)) { | |
3607 | BUFFER_TRACE(bh, "get write access"); | |
3608 | err = ext4_journal_get_write_access(handle, bh); | |
3609 | if (err) | |
3610 | goto next; | |
3611 | } | |
3612 | ||
3613 | zero_user(page, pos, range_to_discard); | |
3614 | ||
3615 | err = 0; | |
3616 | if (ext4_should_journal_data(inode)) { | |
3617 | err = ext4_handle_dirty_metadata(handle, inode, bh); | |
decbd919 | 3618 | } else |
4e96b2db | 3619 | mark_buffer_dirty(bh); |
4e96b2db AH |
3620 | |
3621 | BUFFER_TRACE(bh, "Partial buffer zeroed"); | |
3622 | next: | |
3623 | bh = bh->b_this_page; | |
3624 | iblock++; | |
3625 | pos += range_to_discard; | |
3626 | } | |
3627 | ||
3628 | return err; | |
3629 | } | |
3630 | ||
91ef4caf DG |
3631 | int ext4_can_truncate(struct inode *inode) |
3632 | { | |
91ef4caf DG |
3633 | if (S_ISREG(inode->i_mode)) |
3634 | return 1; | |
3635 | if (S_ISDIR(inode->i_mode)) | |
3636 | return 1; | |
3637 | if (S_ISLNK(inode->i_mode)) | |
3638 | return !ext4_inode_is_fast_symlink(inode); | |
3639 | return 0; | |
3640 | } | |
3641 | ||
a4bb6b64 | 3642 | /* |
7a97321f | 3643 | * ext4_punch_hole: punches a hole in a file by releasing the blocks |
a4bb6b64 AH |
3644 | * associated with the given offset and length |
3645 | * | |
3646 | * @inode: File inode | |
3647 | * @offset: The offset where the hole will begin | |
3648 | * @len: The length of the hole | |
3649 | * | |
4907cb7b | 3650 | * Returns: 0 on success or negative on failure |
a4bb6b64 AH |
3651 | */ |
3652 | ||
3653 | int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) | |
3654 | { | |
4b9e9796 | 3655 | #if 0 |
496ad9aa | 3656 | struct inode *inode = file_inode(file); |
26a4c0c6 TT |
3657 | struct super_block *sb = inode->i_sb; |
3658 | ext4_lblk_t first_block, stop_block; | |
3659 | struct address_space *mapping = inode->i_mapping; | |
3660 | loff_t first_page, last_page, page_len; | |
3661 | loff_t first_page_offset, last_page_offset; | |
3662 | handle_t *handle; | |
3663 | unsigned int credits; | |
3664 | int ret = 0; | |
3665 | ||
a4bb6b64 | 3666 | if (!S_ISREG(inode->i_mode)) |
73355192 | 3667 | return -EOPNOTSUPP; |
a4bb6b64 | 3668 | |
26a4c0c6 | 3669 | if (EXT4_SB(sb)->s_cluster_ratio > 1) { |
bab08ab9 | 3670 | /* TODO: Add support for bigalloc file systems */ |
73355192 | 3671 | return -EOPNOTSUPP; |
bab08ab9 TT |
3672 | } |
3673 | ||
aaddea81 ZL |
3674 | trace_ext4_punch_hole(inode, offset, length); |
3675 | ||
26a4c0c6 TT |
3676 | /* |
3677 | * Write out all dirty pages to avoid race conditions | |
3678 | * Then release them. | |
3679 | */ | |
7a97321f | 3680 | if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) { |
26a4c0c6 TT |
3681 | ret = filemap_write_and_wait_range(mapping, offset, |
3682 | offset + length - 1); | |
3683 | if (ret) | |
3684 | return ret; | |
3685 | } | |
3686 | ||
3687 | mutex_lock(&inode->i_mutex); | |
3688 | /* It's not possible punch hole on append only file */ | |
3689 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) { | |
3690 | ret = -EPERM; | |
3691 | goto out_mutex; | |
3692 | } | |
3693 | if (IS_SWAPFILE(inode)) { | |
3694 | ret = -ETXTBSY; | |
3695 | goto out_mutex; | |
3696 | } | |
3697 | ||
3698 | /* No need to punch hole beyond i_size */ | |
3699 | if (offset >= inode->i_size) | |
3700 | goto out_mutex; | |
3701 | ||
3702 | /* | |
3703 | * If the hole extends beyond i_size, set the hole | |
3704 | * to end after the page that contains i_size | |
3705 | */ | |
3706 | if (offset + length > inode->i_size) { | |
3707 | length = inode->i_size + | |
3708 | PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) - | |
3709 | offset; | |
3710 | } | |
3711 | ||
3712 | first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; | |
3713 | last_page = (offset + length) >> PAGE_CACHE_SHIFT; | |
3714 | ||
3715 | first_page_offset = first_page << PAGE_CACHE_SHIFT; | |
3716 | last_page_offset = last_page << PAGE_CACHE_SHIFT; | |
3717 | ||
3718 | /* Now release the pages */ | |
3719 | if (last_page_offset > first_page_offset) { | |
3720 | truncate_pagecache_range(inode, first_page_offset, | |
3721 | last_page_offset - 1); | |
3722 | } | |
3723 | ||
3724 | /* Wait all existing dio workers, newcomers will block on i_mutex */ | |
3725 | ext4_inode_block_unlocked_dio(inode); | |
3726 | ret = ext4_flush_unwritten_io(inode); | |
3727 | if (ret) | |
3728 | goto out_dio; | |
3729 | inode_dio_wait(inode); | |
3730 | ||
3731 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) | |
3732 | credits = ext4_writepage_trans_blocks(inode); | |
3733 | else | |
3734 | credits = ext4_blocks_for_truncate(inode); | |
3735 | handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); | |
3736 | if (IS_ERR(handle)) { | |
3737 | ret = PTR_ERR(handle); | |
3738 | ext4_std_error(sb, ret); | |
3739 | goto out_dio; | |
3740 | } | |
3741 | ||
3742 | /* | |
3743 | * Now we need to zero out the non-page-aligned data in the | |
3744 | * pages at the start and tail of the hole, and unmap the | |
3745 | * buffer heads for the block aligned regions of the page that | |
3746 | * were completely zeroed. | |
3747 | */ | |
3748 | if (first_page > last_page) { | |
3749 | /* | |
3750 | * If the file space being truncated is contained | |
3751 | * within a page just zero out and unmap the middle of | |
3752 | * that page | |
3753 | */ | |
3754 | ret = ext4_discard_partial_page_buffers(handle, | |
3755 | mapping, offset, length, 0); | |
3756 | ||
3757 | if (ret) | |
3758 | goto out_stop; | |
3759 | } else { | |
3760 | /* | |
3761 | * zero out and unmap the partial page that contains | |
3762 | * the start of the hole | |
3763 | */ | |
3764 | page_len = first_page_offset - offset; | |
3765 | if (page_len > 0) { | |
3766 | ret = ext4_discard_partial_page_buffers(handle, mapping, | |
3767 | offset, page_len, 0); | |
3768 | if (ret) | |
3769 | goto out_stop; | |
3770 | } | |
3771 | ||
3772 | /* | |
3773 | * zero out and unmap the partial page that contains | |
3774 | * the end of the hole | |
3775 | */ | |
3776 | page_len = offset + length - last_page_offset; | |
3777 | if (page_len > 0) { | |
3778 | ret = ext4_discard_partial_page_buffers(handle, mapping, | |
3779 | last_page_offset, page_len, 0); | |
3780 | if (ret) | |
3781 | goto out_stop; | |
3782 | } | |
3783 | } | |
3784 | ||
3785 | /* | |
3786 | * If i_size is contained in the last page, we need to | |
3787 | * unmap and zero the partial page after i_size | |
3788 | */ | |
3789 | if (inode->i_size >> PAGE_CACHE_SHIFT == last_page && | |
3790 | inode->i_size % PAGE_CACHE_SIZE != 0) { | |
3791 | page_len = PAGE_CACHE_SIZE - | |
3792 | (inode->i_size & (PAGE_CACHE_SIZE - 1)); | |
3793 | ||
3794 | if (page_len > 0) { | |
3795 | ret = ext4_discard_partial_page_buffers(handle, | |
3796 | mapping, inode->i_size, page_len, 0); | |
3797 | ||
3798 | if (ret) | |
3799 | goto out_stop; | |
3800 | } | |
3801 | } | |
3802 | ||
3803 | first_block = (offset + sb->s_blocksize - 1) >> | |
3804 | EXT4_BLOCK_SIZE_BITS(sb); | |
3805 | stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb); | |
3806 | ||
3807 | /* If there are no blocks to remove, return now */ | |
3808 | if (first_block >= stop_block) | |
3809 | goto out_stop; | |
3810 | ||
3811 | down_write(&EXT4_I(inode)->i_data_sem); | |
3812 | ext4_discard_preallocations(inode); | |
3813 | ||
3814 | ret = ext4_es_remove_extent(inode, first_block, | |
3815 | stop_block - first_block); | |
3816 | if (ret) { | |
3817 | up_write(&EXT4_I(inode)->i_data_sem); | |
3818 | goto out_stop; | |
3819 | } | |
3820 | ||
3821 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) | |
3822 | ret = ext4_ext_remove_space(inode, first_block, | |
3823 | stop_block - 1); | |
3824 | else | |
3825 | ret = ext4_free_hole_blocks(handle, inode, first_block, | |
3826 | stop_block); | |
3827 | ||
3828 | ext4_discard_preallocations(inode); | |
819c4920 | 3829 | up_write(&EXT4_I(inode)->i_data_sem); |
26a4c0c6 TT |
3830 | if (IS_SYNC(inode)) |
3831 | ext4_handle_sync(handle); | |
26a4c0c6 TT |
3832 | inode->i_mtime = inode->i_ctime = ext4_current_time(inode); |
3833 | ext4_mark_inode_dirty(handle, inode); | |
3834 | out_stop: | |
3835 | ext4_journal_stop(handle); | |
3836 | out_dio: | |
3837 | ext4_inode_resume_unlocked_dio(inode); | |
3838 | out_mutex: | |
3839 | mutex_unlock(&inode->i_mutex); | |
3840 | return ret; | |
4b9e9796 S |
3841 | #else |
3842 | /* | |
3843 | * Disabled as per b/28760453 | |
3844 | */ | |
3845 | return -EOPNOTSUPP; | |
3846 | #endif | |
a4bb6b64 AH |
3847 | } |
3848 | ||
ac27a0ec | 3849 | /* |
617ba13b | 3850 | * ext4_truncate() |
ac27a0ec | 3851 | * |
617ba13b MC |
3852 | * We block out ext4_get_block() block instantiations across the entire |
3853 | * transaction, and VFS/VM ensures that ext4_truncate() cannot run | |
ac27a0ec DK |
3854 | * simultaneously on behalf of the same inode. |
3855 | * | |
42b2aa86 | 3856 | * As we work through the truncate and commit bits of it to the journal there |
ac27a0ec DK |
3857 | * is one core, guiding principle: the file's tree must always be consistent on |
3858 | * disk. We must be able to restart the truncate after a crash. | |
3859 | * | |
3860 | * The file's tree may be transiently inconsistent in memory (although it | |
3861 | * probably isn't), but whenever we close off and commit a journal transaction, | |
3862 | * the contents of (the filesystem + the journal) must be consistent and | |
3863 | * restartable. It's pretty simple, really: bottom up, right to left (although | |
3864 | * left-to-right works OK too). | |
3865 | * | |
3866 | * Note that at recovery time, journal replay occurs *before* the restart of | |
3867 | * truncate against the orphan inode list. | |
3868 | * | |
3869 | * The committed inode has the new, desired i_size (which is the same as | |
617ba13b | 3870 | * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see |
ac27a0ec | 3871 | * that this inode's truncate did not complete and it will again call |
617ba13b MC |
3872 | * ext4_truncate() to have another go. So there will be instantiated blocks |
3873 | * to the right of the truncation point in a crashed ext4 filesystem. But | |
ac27a0ec | 3874 | * that's fine - as long as they are linked from the inode, the post-crash |
617ba13b | 3875 | * ext4_truncate() run will find them and release them. |
ac27a0ec | 3876 | */ |
617ba13b | 3877 | void ext4_truncate(struct inode *inode) |
ac27a0ec | 3878 | { |
819c4920 TT |
3879 | struct ext4_inode_info *ei = EXT4_I(inode); |
3880 | unsigned int credits; | |
3881 | handle_t *handle; | |
3882 | struct address_space *mapping = inode->i_mapping; | |
3883 | loff_t page_len; | |
3884 | ||
19b5ef61 TT |
3885 | /* |
3886 | * There is a possibility that we're either freeing the inode | |
3887 | * or it completely new indode. In those cases we might not | |
3888 | * have i_mutex locked because it's not necessary. | |
3889 | */ | |
3890 | if (!(inode->i_state & (I_NEW|I_FREEING))) | |
3891 | WARN_ON(!mutex_is_locked(&inode->i_mutex)); | |
0562e0ba JZ |
3892 | trace_ext4_truncate_enter(inode); |
3893 | ||
91ef4caf | 3894 | if (!ext4_can_truncate(inode)) |
ac27a0ec DK |
3895 | return; |
3896 | ||
12e9b892 | 3897 | ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS); |
c8d46e41 | 3898 | |
5534fb5b | 3899 | if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC)) |
19f5fb7a | 3900 | ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE); |
7d8f9f7d | 3901 | |
aef1c851 TM |
3902 | if (ext4_has_inline_data(inode)) { |
3903 | int has_inline = 1; | |
3904 | ||
3905 | ext4_inline_data_truncate(inode, &has_inline); | |
3906 | if (has_inline) | |
3907 | return; | |
3908 | } | |
3909 | ||
819c4920 TT |
3910 | /* |
3911 | * finish any pending end_io work so we won't run the risk of | |
3912 | * converting any truncated blocks to initialized later | |
3913 | */ | |
3914 | ext4_flush_unwritten_io(inode); | |
3915 | ||
3916 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) | |
3917 | credits = ext4_writepage_trans_blocks(inode); | |
3918 | else | |
3919 | credits = ext4_blocks_for_truncate(inode); | |
3920 | ||
3921 | handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); | |
3922 | if (IS_ERR(handle)) { | |
3923 | ext4_std_error(inode->i_sb, PTR_ERR(handle)); | |
3924 | return; | |
3925 | } | |
3926 | ||
3927 | if (inode->i_size % PAGE_CACHE_SIZE != 0) { | |
3928 | page_len = PAGE_CACHE_SIZE - | |
3929 | (inode->i_size & (PAGE_CACHE_SIZE - 1)); | |
3930 | ||
3931 | if (ext4_discard_partial_page_buffers(handle, | |
3932 | mapping, inode->i_size, page_len, 0)) | |
3933 | goto out_stop; | |
3934 | } | |
3935 | ||
3936 | /* | |
3937 | * We add the inode to the orphan list, so that if this | |
3938 | * truncate spans multiple transactions, and we crash, we will | |
3939 | * resume the truncate when the filesystem recovers. It also | |
3940 | * marks the inode dirty, to catch the new size. | |
3941 | * | |
3942 | * Implication: the file must always be in a sane, consistent | |
3943 | * truncatable state while each transaction commits. | |
3944 | */ | |
3945 | if (ext4_orphan_add(handle, inode)) | |
3946 | goto out_stop; | |
3947 | ||
3948 | down_write(&EXT4_I(inode)->i_data_sem); | |
3949 | ||
3950 | ext4_discard_preallocations(inode); | |
3951 | ||
ff9893dc | 3952 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) |
819c4920 | 3953 | ext4_ext_truncate(handle, inode); |
ff9893dc | 3954 | else |
819c4920 TT |
3955 | ext4_ind_truncate(handle, inode); |
3956 | ||
3957 | up_write(&ei->i_data_sem); | |
3958 | ||
3959 | if (IS_SYNC(inode)) | |
3960 | ext4_handle_sync(handle); | |
3961 | ||
3962 | out_stop: | |
3963 | /* | |
3964 | * If this was a simple ftruncate() and the file will remain alive, | |
3965 | * then we need to clear up the orphan record which we created above. | |
3966 | * However, if this was a real unlink then we were called by | |
3967 | * ext4_delete_inode(), and we allow that function to clean up the | |
3968 | * orphan info for us. | |
3969 | */ | |
3970 | if (inode->i_nlink) | |
3971 | ext4_orphan_del(handle, inode); | |
3972 | ||
3973 | inode->i_mtime = inode->i_ctime = ext4_current_time(inode); | |
3974 | ext4_mark_inode_dirty(handle, inode); | |
3975 | ext4_journal_stop(handle); | |
ac27a0ec | 3976 | |
0562e0ba | 3977 | trace_ext4_truncate_exit(inode); |
ac27a0ec DK |
3978 | } |
3979 | ||
ac27a0ec | 3980 | /* |
617ba13b | 3981 | * ext4_get_inode_loc returns with an extra refcount against the inode's |
ac27a0ec DK |
3982 | * underlying buffer_head on success. If 'in_mem' is true, we have all |
3983 | * data in memory that is needed to recreate the on-disk version of this | |
3984 | * inode. | |
3985 | */ | |
617ba13b MC |
3986 | static int __ext4_get_inode_loc(struct inode *inode, |
3987 | struct ext4_iloc *iloc, int in_mem) | |
ac27a0ec | 3988 | { |
240799cd TT |
3989 | struct ext4_group_desc *gdp; |
3990 | struct buffer_head *bh; | |
3991 | struct super_block *sb = inode->i_sb; | |
3992 | ext4_fsblk_t block; | |
3993 | int inodes_per_block, inode_offset; | |
3994 | ||
3a06d778 | 3995 | iloc->bh = NULL; |
240799cd TT |
3996 | if (!ext4_valid_inum(sb, inode->i_ino)) |
3997 | return -EIO; | |
ac27a0ec | 3998 | |
240799cd TT |
3999 | iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb); |
4000 | gdp = ext4_get_group_desc(sb, iloc->block_group, NULL); | |
4001 | if (!gdp) | |
ac27a0ec DK |
4002 | return -EIO; |
4003 | ||
240799cd TT |
4004 | /* |
4005 | * Figure out the offset within the block group inode table | |
4006 | */ | |
00d09882 | 4007 | inodes_per_block = EXT4_SB(sb)->s_inodes_per_block; |
240799cd TT |
4008 | inode_offset = ((inode->i_ino - 1) % |
4009 | EXT4_INODES_PER_GROUP(sb)); | |
4010 | block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block); | |
4011 | iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb); | |
4012 | ||
4013 | bh = sb_getblk(sb, block); | |
aebf0243 | 4014 | if (unlikely(!bh)) |
860d21e2 | 4015 | return -ENOMEM; |
ac27a0ec DK |
4016 | if (!buffer_uptodate(bh)) { |
4017 | lock_buffer(bh); | |
9c83a923 HK |
4018 | |
4019 | /* | |
4020 | * If the buffer has the write error flag, we have failed | |
4021 | * to write out another inode in the same block. In this | |
4022 | * case, we don't have to read the block because we may | |
4023 | * read the old inode data successfully. | |
4024 | */ | |
4025 | if (buffer_write_io_error(bh) && !buffer_uptodate(bh)) | |
4026 | set_buffer_uptodate(bh); | |
4027 | ||
ac27a0ec DK |
4028 | if (buffer_uptodate(bh)) { |
4029 | /* someone brought it uptodate while we waited */ | |
4030 | unlock_buffer(bh); | |
4031 | goto has_buffer; | |
4032 | } | |
4033 | ||
4034 | /* | |
4035 | * If we have all information of the inode in memory and this | |
4036 | * is the only valid inode in the block, we need not read the | |
4037 | * block. | |
4038 | */ | |
4039 | if (in_mem) { | |
4040 | struct buffer_head *bitmap_bh; | |
240799cd | 4041 | int i, start; |
ac27a0ec | 4042 | |
240799cd | 4043 | start = inode_offset & ~(inodes_per_block - 1); |
ac27a0ec | 4044 | |
240799cd TT |
4045 | /* Is the inode bitmap in cache? */ |
4046 | bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp)); | |
aebf0243 | 4047 | if (unlikely(!bitmap_bh)) |
ac27a0ec DK |
4048 | goto make_io; |
4049 | ||
4050 | /* | |
4051 | * If the inode bitmap isn't in cache then the | |
4052 | * optimisation may end up performing two reads instead | |
4053 | * of one, so skip it. | |
4054 | */ | |
4055 | if (!buffer_uptodate(bitmap_bh)) { | |
4056 | brelse(bitmap_bh); | |
4057 | goto make_io; | |
4058 | } | |
240799cd | 4059 | for (i = start; i < start + inodes_per_block; i++) { |
ac27a0ec DK |
4060 | if (i == inode_offset) |
4061 | continue; | |
617ba13b | 4062 | if (ext4_test_bit(i, bitmap_bh->b_data)) |
ac27a0ec DK |
4063 | break; |
4064 | } | |
4065 | brelse(bitmap_bh); | |
240799cd | 4066 | if (i == start + inodes_per_block) { |
ac27a0ec DK |
4067 | /* all other inodes are free, so skip I/O */ |
4068 | memset(bh->b_data, 0, bh->b_size); | |
4069 | set_buffer_uptodate(bh); | |
4070 | unlock_buffer(bh); | |
4071 | goto has_buffer; | |
4072 | } | |
4073 | } | |
4074 | ||
4075 | make_io: | |
240799cd TT |
4076 | /* |
4077 | * If we need to do any I/O, try to pre-readahead extra | |
4078 | * blocks from the inode table. | |
4079 | */ | |
4080 | if (EXT4_SB(sb)->s_inode_readahead_blks) { | |
4081 | ext4_fsblk_t b, end, table; | |
4082 | unsigned num; | |
0d606e2c | 4083 | __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks; |
240799cd TT |
4084 | |
4085 | table = ext4_inode_table(sb, gdp); | |
b713a5ec | 4086 | /* s_inode_readahead_blks is always a power of 2 */ |
0d606e2c | 4087 | b = block & ~((ext4_fsblk_t) ra_blks - 1); |
240799cd TT |
4088 | if (table > b) |
4089 | b = table; | |
0d606e2c | 4090 | end = b + ra_blks; |
240799cd | 4091 | num = EXT4_INODES_PER_GROUP(sb); |
feb0ab32 | 4092 | if (ext4_has_group_desc_csum(sb)) |
560671a0 | 4093 | num -= ext4_itable_unused_count(sb, gdp); |
240799cd TT |
4094 | table += num / inodes_per_block; |
4095 | if (end > table) | |
4096 | end = table; | |
4097 | while (b <= end) | |
4098 | sb_breadahead(sb, b++); | |
4099 | } | |
4100 | ||
ac27a0ec DK |
4101 | /* |
4102 | * There are other valid inodes in the buffer, this inode | |
4103 | * has in-inode xattrs, or we don't have this inode in memory. | |
4104 | * Read the block from disk. | |
4105 | */ | |
0562e0ba | 4106 | trace_ext4_load_inode(inode); |
ac27a0ec DK |
4107 | get_bh(bh); |
4108 | bh->b_end_io = end_buffer_read_sync; | |
6fa3eb70 S |
4109 | #ifdef FEATURE_STORAGE_META_LOG |
4110 | if( bh && bh->b_bdev && bh->b_bdev->bd_disk) | |
4111 | set_metadata_rw_status(bh->b_bdev->bd_disk->first_minor, WAIT_READ_CNT); | |
4112 | #endif | |
65299a3b | 4113 | submit_bh(READ | REQ_META | REQ_PRIO, bh); |
ac27a0ec DK |
4114 | wait_on_buffer(bh); |
4115 | if (!buffer_uptodate(bh)) { | |
c398eda0 TT |
4116 | EXT4_ERROR_INODE_BLOCK(inode, block, |
4117 | "unable to read itable block"); | |
ac27a0ec DK |
4118 | brelse(bh); |
4119 | return -EIO; | |
4120 | } | |
4121 | } | |
4122 | has_buffer: | |
4123 | iloc->bh = bh; | |
4124 | return 0; | |
4125 | } | |
4126 | ||
617ba13b | 4127 | int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc) |
ac27a0ec DK |
4128 | { |
4129 | /* We have all inode data except xattrs in memory here. */ | |
617ba13b | 4130 | return __ext4_get_inode_loc(inode, iloc, |
19f5fb7a | 4131 | !ext4_test_inode_state(inode, EXT4_STATE_XATTR)); |
ac27a0ec DK |
4132 | } |
4133 | ||
617ba13b | 4134 | void ext4_set_inode_flags(struct inode *inode) |
ac27a0ec | 4135 | { |
617ba13b | 4136 | unsigned int flags = EXT4_I(inode)->i_flags; |
0a0ae7b3 | 4137 | unsigned int new_fl = 0; |
ac27a0ec | 4138 | |
617ba13b | 4139 | if (flags & EXT4_SYNC_FL) |
0a0ae7b3 | 4140 | new_fl |= S_SYNC; |
617ba13b | 4141 | if (flags & EXT4_APPEND_FL) |
0a0ae7b3 | 4142 | new_fl |= S_APPEND; |
617ba13b | 4143 | if (flags & EXT4_IMMUTABLE_FL) |
0a0ae7b3 | 4144 | new_fl |= S_IMMUTABLE; |
617ba13b | 4145 | if (flags & EXT4_NOATIME_FL) |
0a0ae7b3 | 4146 | new_fl |= S_NOATIME; |
617ba13b | 4147 | if (flags & EXT4_DIRSYNC_FL) |
0a0ae7b3 TT |
4148 | new_fl |= S_DIRSYNC; |
4149 | set_mask_bits(&inode->i_flags, | |
4150 | S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC, new_fl); | |
ac27a0ec DK |
4151 | } |
4152 | ||
ff9ddf7e JK |
4153 | /* Propagate flags from i_flags to EXT4_I(inode)->i_flags */ |
4154 | void ext4_get_inode_flags(struct ext4_inode_info *ei) | |
4155 | { | |
84a8dce2 DM |
4156 | unsigned int vfs_fl; |
4157 | unsigned long old_fl, new_fl; | |
4158 | ||
4159 | do { | |
4160 | vfs_fl = ei->vfs_inode.i_flags; | |
4161 | old_fl = ei->i_flags; | |
4162 | new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL| | |
4163 | EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL| | |
4164 | EXT4_DIRSYNC_FL); | |
4165 | if (vfs_fl & S_SYNC) | |
4166 | new_fl |= EXT4_SYNC_FL; | |
4167 | if (vfs_fl & S_APPEND) | |
4168 | new_fl |= EXT4_APPEND_FL; | |
4169 | if (vfs_fl & S_IMMUTABLE) | |
4170 | new_fl |= EXT4_IMMUTABLE_FL; | |
4171 | if (vfs_fl & S_NOATIME) | |
4172 | new_fl |= EXT4_NOATIME_FL; | |
4173 | if (vfs_fl & S_DIRSYNC) | |
4174 | new_fl |= EXT4_DIRSYNC_FL; | |
4175 | } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl); | |
ff9ddf7e | 4176 | } |
de9a55b8 | 4177 | |
0fc1b451 | 4178 | static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode, |
de9a55b8 | 4179 | struct ext4_inode_info *ei) |
0fc1b451 AK |
4180 | { |
4181 | blkcnt_t i_blocks ; | |
8180a562 AK |
4182 | struct inode *inode = &(ei->vfs_inode); |
4183 | struct super_block *sb = inode->i_sb; | |
0fc1b451 AK |
4184 | |
4185 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, | |
4186 | EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) { | |
4187 | /* we are using combined 48 bit field */ | |
4188 | i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 | | |
4189 | le32_to_cpu(raw_inode->i_blocks_lo); | |
07a03824 | 4190 | if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) { |
8180a562 AK |
4191 | /* i_blocks represent file system block size */ |
4192 | return i_blocks << (inode->i_blkbits - 9); | |
4193 | } else { | |
4194 | return i_blocks; | |
4195 | } | |
0fc1b451 AK |
4196 | } else { |
4197 | return le32_to_cpu(raw_inode->i_blocks_lo); | |
4198 | } | |
4199 | } | |
ff9ddf7e | 4200 | |
152a7b0a TM |
4201 | static inline void ext4_iget_extra_inode(struct inode *inode, |
4202 | struct ext4_inode *raw_inode, | |
4203 | struct ext4_inode_info *ei) | |
4204 | { | |
4205 | __le32 *magic = (void *)raw_inode + | |
4206 | EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize; | |
67cf5b09 | 4207 | if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) { |
152a7b0a | 4208 | ext4_set_inode_state(inode, EXT4_STATE_XATTR); |
67cf5b09 | 4209 | ext4_find_inline_data_nolock(inode); |
f19d5870 TM |
4210 | } else |
4211 | EXT4_I(inode)->i_inline_off = 0; | |
152a7b0a TM |
4212 | } |
4213 | ||
1d1fe1ee | 4214 | struct inode *ext4_iget(struct super_block *sb, unsigned long ino) |
ac27a0ec | 4215 | { |
617ba13b MC |
4216 | struct ext4_iloc iloc; |
4217 | struct ext4_inode *raw_inode; | |
1d1fe1ee | 4218 | struct ext4_inode_info *ei; |
1d1fe1ee | 4219 | struct inode *inode; |
b436b9be | 4220 | journal_t *journal = EXT4_SB(sb)->s_journal; |
1d1fe1ee | 4221 | long ret; |
b70876f0 | 4222 | loff_t size; |
ac27a0ec | 4223 | int block; |
08cefc7a EB |
4224 | uid_t i_uid; |
4225 | gid_t i_gid; | |
ac27a0ec | 4226 | |
1d1fe1ee DH |
4227 | inode = iget_locked(sb, ino); |
4228 | if (!inode) | |
4229 | return ERR_PTR(-ENOMEM); | |
4230 | if (!(inode->i_state & I_NEW)) | |
4231 | return inode; | |
4232 | ||
4233 | ei = EXT4_I(inode); | |
7dc57615 | 4234 | iloc.bh = NULL; |
ac27a0ec | 4235 | |
1d1fe1ee DH |
4236 | ret = __ext4_get_inode_loc(inode, &iloc, 0); |
4237 | if (ret < 0) | |
ac27a0ec | 4238 | goto bad_inode; |
617ba13b | 4239 | raw_inode = ext4_raw_inode(&iloc); |
814525f4 DW |
4240 | |
4241 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { | |
4242 | ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize); | |
4243 | if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > | |
4244 | EXT4_INODE_SIZE(inode->i_sb)) { | |
4245 | EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)", | |
4246 | EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize, | |
4247 | EXT4_INODE_SIZE(inode->i_sb)); | |
4248 | ret = -EIO; | |
4249 | goto bad_inode; | |
4250 | } | |
4251 | } else | |
4252 | ei->i_extra_isize = 0; | |
4253 | ||
4254 | /* Precompute checksum seed for inode metadata */ | |
4255 | if (EXT4_HAS_RO_COMPAT_FEATURE(sb, | |
4256 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) { | |
4257 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); | |
4258 | __u32 csum; | |
4259 | __le32 inum = cpu_to_le32(inode->i_ino); | |
4260 | __le32 gen = raw_inode->i_generation; | |
4261 | csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum, | |
4262 | sizeof(inum)); | |
4263 | ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen, | |
4264 | sizeof(gen)); | |
4265 | } | |
4266 | ||
4267 | if (!ext4_inode_csum_verify(inode, raw_inode, ei)) { | |
4268 | EXT4_ERROR_INODE(inode, "checksum invalid"); | |
4269 | ret = -EIO; | |
4270 | goto bad_inode; | |
4271 | } | |
4272 | ||
ac27a0ec | 4273 | inode->i_mode = le16_to_cpu(raw_inode->i_mode); |
08cefc7a EB |
4274 | i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low); |
4275 | i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low); | |
af5bc92d | 4276 | if (!(test_opt(inode->i_sb, NO_UID32))) { |
08cefc7a EB |
4277 | i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16; |
4278 | i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16; | |
ac27a0ec | 4279 | } |
08cefc7a EB |
4280 | i_uid_write(inode, i_uid); |
4281 | i_gid_write(inode, i_gid); | |
bfe86848 | 4282 | set_nlink(inode, le16_to_cpu(raw_inode->i_links_count)); |
ac27a0ec | 4283 | |
353eb83c | 4284 | ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */ |
67cf5b09 | 4285 | ei->i_inline_off = 0; |
ac27a0ec DK |
4286 | ei->i_dir_start_lookup = 0; |
4287 | ei->i_dtime = le32_to_cpu(raw_inode->i_dtime); | |
4288 | /* We now have enough fields to check if the inode was active or not. | |
4289 | * This is needed because nfsd might try to access dead inodes | |
4290 | * the test is that same one that e2fsck uses | |
4291 | * NeilBrown 1999oct15 | |
4292 | */ | |
4293 | if (inode->i_nlink == 0) { | |
393d1d1d DTB |
4294 | if ((inode->i_mode == 0 || |
4295 | !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) && | |
4296 | ino != EXT4_BOOT_LOADER_INO) { | |
ac27a0ec | 4297 | /* this inode is deleted */ |
1d1fe1ee | 4298 | ret = -ESTALE; |
ac27a0ec DK |
4299 | goto bad_inode; |
4300 | } | |
4301 | /* The only unlinked inodes we let through here have | |
4302 | * valid i_mode and are being read by the orphan | |
4303 | * recovery code: that's fine, we're about to complete | |
393d1d1d DTB |
4304 | * the process of deleting those. |
4305 | * OR it is the EXT4_BOOT_LOADER_INO which is | |
4306 | * not initialized on a new filesystem. */ | |
ac27a0ec | 4307 | } |
ac27a0ec | 4308 | ei->i_flags = le32_to_cpu(raw_inode->i_flags); |
0fc1b451 | 4309 | inode->i_blocks = ext4_inode_blocks(raw_inode, ei); |
7973c0c1 | 4310 | ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo); |
a9e81742 | 4311 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) |
a1ddeb7e BP |
4312 | ei->i_file_acl |= |
4313 | ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32; | |
a48380f7 | 4314 | inode->i_size = ext4_isize(raw_inode); |
b70876f0 DW |
4315 | if ((size = i_size_read(inode)) < 0) { |
4316 | EXT4_ERROR_INODE(inode, "bad i_size value: %lld", size); | |
4317 | ret = -EIO; | |
4318 | goto bad_inode; | |
4319 | } | |
ac27a0ec | 4320 | ei->i_disksize = inode->i_size; |
a9e7f447 DM |
4321 | #ifdef CONFIG_QUOTA |
4322 | ei->i_reserved_quota = 0; | |
4323 | #endif | |
ac27a0ec DK |
4324 | inode->i_generation = le32_to_cpu(raw_inode->i_generation); |
4325 | ei->i_block_group = iloc.block_group; | |
a4912123 | 4326 | ei->i_last_alloc_group = ~0; |
ac27a0ec DK |
4327 | /* |
4328 | * NOTE! The in-memory inode i_data array is in little-endian order | |
4329 | * even on big-endian machines: we do NOT byteswap the block numbers! | |
4330 | */ | |
617ba13b | 4331 | for (block = 0; block < EXT4_N_BLOCKS; block++) |
ac27a0ec DK |
4332 | ei->i_data[block] = raw_inode->i_block[block]; |
4333 | INIT_LIST_HEAD(&ei->i_orphan); | |
4334 | ||
b436b9be JK |
4335 | /* |
4336 | * Set transaction id's of transactions that have to be committed | |
4337 | * to finish f[data]sync. We set them to currently running transaction | |
4338 | * as we cannot be sure that the inode or some of its metadata isn't | |
4339 | * part of the transaction - the inode could have been reclaimed and | |
4340 | * now it is reread from disk. | |
4341 | */ | |
4342 | if (journal) { | |
4343 | transaction_t *transaction; | |
4344 | tid_t tid; | |
4345 | ||
a931da6a | 4346 | read_lock(&journal->j_state_lock); |
b436b9be JK |
4347 | if (journal->j_running_transaction) |
4348 | transaction = journal->j_running_transaction; | |
4349 | else | |
4350 | transaction = journal->j_committing_transaction; | |
4351 | if (transaction) | |
4352 | tid = transaction->t_tid; | |
4353 | else | |
4354 | tid = journal->j_commit_sequence; | |
a931da6a | 4355 | read_unlock(&journal->j_state_lock); |
b436b9be JK |
4356 | ei->i_sync_tid = tid; |
4357 | ei->i_datasync_tid = tid; | |
4358 | } | |
4359 | ||
0040d987 | 4360 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { |
ac27a0ec DK |
4361 | if (ei->i_extra_isize == 0) { |
4362 | /* The extra space is currently unused. Use it. */ | |
617ba13b MC |
4363 | ei->i_extra_isize = sizeof(struct ext4_inode) - |
4364 | EXT4_GOOD_OLD_INODE_SIZE; | |
ac27a0ec | 4365 | } else { |
152a7b0a | 4366 | ext4_iget_extra_inode(inode, raw_inode, ei); |
ac27a0ec | 4367 | } |
814525f4 | 4368 | } |
ac27a0ec | 4369 | |
ef7f3835 KS |
4370 | EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode); |
4371 | EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode); | |
4372 | EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode); | |
4373 | EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode); | |
4374 | ||
25ec56b5 JNC |
4375 | inode->i_version = le32_to_cpu(raw_inode->i_disk_version); |
4376 | if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) { | |
4377 | if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) | |
4378 | inode->i_version |= | |
4379 | (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32; | |
4380 | } | |
4381 | ||
c4b5a614 | 4382 | ret = 0; |
485c26ec | 4383 | if (ei->i_file_acl && |
1032988c | 4384 | !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) { |
24676da4 TT |
4385 | EXT4_ERROR_INODE(inode, "bad extended attribute block %llu", |
4386 | ei->i_file_acl); | |
485c26ec TT |
4387 | ret = -EIO; |
4388 | goto bad_inode; | |
f19d5870 TM |
4389 | } else if (!ext4_has_inline_data(inode)) { |
4390 | if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) { | |
4391 | if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || | |
4392 | (S_ISLNK(inode->i_mode) && | |
4393 | !ext4_inode_is_fast_symlink(inode)))) | |
4394 | /* Validate extent which is part of inode */ | |
4395 | ret = ext4_ext_check_inode(inode); | |
4396 | } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || | |
4397 | (S_ISLNK(inode->i_mode) && | |
4398 | !ext4_inode_is_fast_symlink(inode))) { | |
4399 | /* Validate block references which are part of inode */ | |
4400 | ret = ext4_ind_check_inode(inode); | |
4401 | } | |
fe2c8191 | 4402 | } |
567f3e9a | 4403 | if (ret) |
de9a55b8 | 4404 | goto bad_inode; |
7a262f7c | 4405 | |
ac27a0ec | 4406 | if (S_ISREG(inode->i_mode)) { |
617ba13b MC |
4407 | inode->i_op = &ext4_file_inode_operations; |
4408 | inode->i_fop = &ext4_file_operations; | |
4409 | ext4_set_aops(inode); | |
ac27a0ec | 4410 | } else if (S_ISDIR(inode->i_mode)) { |
617ba13b MC |
4411 | inode->i_op = &ext4_dir_inode_operations; |
4412 | inode->i_fop = &ext4_dir_operations; | |
ac27a0ec | 4413 | } else if (S_ISLNK(inode->i_mode)) { |
e83c1397 | 4414 | if (ext4_inode_is_fast_symlink(inode)) { |
617ba13b | 4415 | inode->i_op = &ext4_fast_symlink_inode_operations; |
e83c1397 DG |
4416 | nd_terminate_link(ei->i_data, inode->i_size, |
4417 | sizeof(ei->i_data) - 1); | |
4418 | } else { | |
617ba13b MC |
4419 | inode->i_op = &ext4_symlink_inode_operations; |
4420 | ext4_set_aops(inode); | |
ac27a0ec | 4421 | } |
563bdd61 TT |
4422 | } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || |
4423 | S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { | |
617ba13b | 4424 | inode->i_op = &ext4_special_inode_operations; |
ac27a0ec DK |
4425 | if (raw_inode->i_block[0]) |
4426 | init_special_inode(inode, inode->i_mode, | |
4427 | old_decode_dev(le32_to_cpu(raw_inode->i_block[0]))); | |
4428 | else | |
4429 | init_special_inode(inode, inode->i_mode, | |
4430 | new_decode_dev(le32_to_cpu(raw_inode->i_block[1]))); | |
393d1d1d DTB |
4431 | } else if (ino == EXT4_BOOT_LOADER_INO) { |
4432 | make_bad_inode(inode); | |
563bdd61 | 4433 | } else { |
563bdd61 | 4434 | ret = -EIO; |
24676da4 | 4435 | EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode); |
563bdd61 | 4436 | goto bad_inode; |
ac27a0ec | 4437 | } |
af5bc92d | 4438 | brelse(iloc.bh); |
617ba13b | 4439 | ext4_set_inode_flags(inode); |
1d1fe1ee DH |
4440 | unlock_new_inode(inode); |
4441 | return inode; | |
ac27a0ec DK |
4442 | |
4443 | bad_inode: | |
567f3e9a | 4444 | brelse(iloc.bh); |
1d1fe1ee DH |
4445 | iget_failed(inode); |
4446 | return ERR_PTR(ret); | |
ac27a0ec DK |
4447 | } |
4448 | ||
65f25799 TT |
4449 | struct inode *ext4_iget_normal(struct super_block *sb, unsigned long ino) |
4450 | { | |
4451 | if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO) | |
4452 | return ERR_PTR(-EIO); | |
4453 | return ext4_iget(sb, ino); | |
4454 | } | |
4455 | ||
0fc1b451 AK |
4456 | static int ext4_inode_blocks_set(handle_t *handle, |
4457 | struct ext4_inode *raw_inode, | |
4458 | struct ext4_inode_info *ei) | |
4459 | { | |
4460 | struct inode *inode = &(ei->vfs_inode); | |
4461 | u64 i_blocks = inode->i_blocks; | |
4462 | struct super_block *sb = inode->i_sb; | |
0fc1b451 AK |
4463 | |
4464 | if (i_blocks <= ~0U) { | |
4465 | /* | |
4907cb7b | 4466 | * i_blocks can be represented in a 32 bit variable |
0fc1b451 AK |
4467 | * as multiple of 512 bytes |
4468 | */ | |
8180a562 | 4469 | raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); |
0fc1b451 | 4470 | raw_inode->i_blocks_high = 0; |
84a8dce2 | 4471 | ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); |
f287a1a5 TT |
4472 | return 0; |
4473 | } | |
4474 | if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) | |
4475 | return -EFBIG; | |
4476 | ||
4477 | if (i_blocks <= 0xffffffffffffULL) { | |
0fc1b451 AK |
4478 | /* |
4479 | * i_blocks can be represented in a 48 bit variable | |
4480 | * as multiple of 512 bytes | |
4481 | */ | |
8180a562 | 4482 | raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); |
0fc1b451 | 4483 | raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); |
84a8dce2 | 4484 | ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE); |
0fc1b451 | 4485 | } else { |
84a8dce2 | 4486 | ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE); |
8180a562 AK |
4487 | /* i_block is stored in file system block size */ |
4488 | i_blocks = i_blocks >> (inode->i_blkbits - 9); | |
4489 | raw_inode->i_blocks_lo = cpu_to_le32(i_blocks); | |
4490 | raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32); | |
0fc1b451 | 4491 | } |
f287a1a5 | 4492 | return 0; |
0fc1b451 AK |
4493 | } |
4494 | ||
ac27a0ec DK |
4495 | /* |
4496 | * Post the struct inode info into an on-disk inode location in the | |
4497 | * buffer-cache. This gobbles the caller's reference to the | |
4498 | * buffer_head in the inode location struct. | |
4499 | * | |
4500 | * The caller must have write access to iloc->bh. | |
4501 | */ | |
617ba13b | 4502 | static int ext4_do_update_inode(handle_t *handle, |
ac27a0ec | 4503 | struct inode *inode, |
830156c7 | 4504 | struct ext4_iloc *iloc) |
ac27a0ec | 4505 | { |
617ba13b MC |
4506 | struct ext4_inode *raw_inode = ext4_raw_inode(iloc); |
4507 | struct ext4_inode_info *ei = EXT4_I(inode); | |
ac27a0ec DK |
4508 | struct buffer_head *bh = iloc->bh; |
4509 | int err = 0, rc, block; | |
b71fc079 | 4510 | int need_datasync = 0; |
08cefc7a EB |
4511 | uid_t i_uid; |
4512 | gid_t i_gid; | |
ac27a0ec DK |
4513 | |
4514 | /* For fields not not tracking in the in-memory inode, | |
4515 | * initialise them to zero for new inodes. */ | |
19f5fb7a | 4516 | if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) |
617ba13b | 4517 | memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size); |
ac27a0ec | 4518 | |
ff9ddf7e | 4519 | ext4_get_inode_flags(ei); |
ac27a0ec | 4520 | raw_inode->i_mode = cpu_to_le16(inode->i_mode); |
08cefc7a EB |
4521 | i_uid = i_uid_read(inode); |
4522 | i_gid = i_gid_read(inode); | |
af5bc92d | 4523 | if (!(test_opt(inode->i_sb, NO_UID32))) { |
08cefc7a EB |
4524 | raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid)); |
4525 | raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid)); | |
ac27a0ec DK |
4526 | /* |
4527 | * Fix up interoperability with old kernels. Otherwise, old inodes get | |
4528 | * re-used with the upper 16 bits of the uid/gid intact | |
4529 | */ | |
1326ba87 DJ |
4530 | if (ei->i_dtime && list_empty(&ei->i_orphan)) { |
4531 | raw_inode->i_uid_high = 0; | |
4532 | raw_inode->i_gid_high = 0; | |
4533 | } else { | |
ac27a0ec | 4534 | raw_inode->i_uid_high = |
08cefc7a | 4535 | cpu_to_le16(high_16_bits(i_uid)); |
ac27a0ec | 4536 | raw_inode->i_gid_high = |
08cefc7a | 4537 | cpu_to_le16(high_16_bits(i_gid)); |
ac27a0ec DK |
4538 | } |
4539 | } else { | |
08cefc7a EB |
4540 | raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid)); |
4541 | raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid)); | |
ac27a0ec DK |
4542 | raw_inode->i_uid_high = 0; |
4543 | raw_inode->i_gid_high = 0; | |
4544 | } | |
4545 | raw_inode->i_links_count = cpu_to_le16(inode->i_nlink); | |
ef7f3835 KS |
4546 | |
4547 | EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode); | |
4548 | EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode); | |
4549 | EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode); | |
4550 | EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode); | |
4551 | ||
0fc1b451 AK |
4552 | if (ext4_inode_blocks_set(handle, raw_inode, ei)) |
4553 | goto out_brelse; | |
ac27a0ec | 4554 | raw_inode->i_dtime = cpu_to_le32(ei->i_dtime); |
353eb83c | 4555 | raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF); |
9b8f1f01 MC |
4556 | if (EXT4_SB(inode->i_sb)->s_es->s_creator_os != |
4557 | cpu_to_le32(EXT4_OS_HURD)) | |
a1ddeb7e BP |
4558 | raw_inode->i_file_acl_high = |
4559 | cpu_to_le16(ei->i_file_acl >> 32); | |
7973c0c1 | 4560 | raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl); |
b71fc079 JK |
4561 | if (ei->i_disksize != ext4_isize(raw_inode)) { |
4562 | ext4_isize_set(raw_inode, ei->i_disksize); | |
4563 | need_datasync = 1; | |
4564 | } | |
a48380f7 AK |
4565 | if (ei->i_disksize > 0x7fffffffULL) { |
4566 | struct super_block *sb = inode->i_sb; | |
4567 | if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, | |
4568 | EXT4_FEATURE_RO_COMPAT_LARGE_FILE) || | |
4569 | EXT4_SB(sb)->s_es->s_rev_level == | |
4570 | cpu_to_le32(EXT4_GOOD_OLD_REV)) { | |
4571 | /* If this is the first large file | |
4572 | * created, add a flag to the superblock. | |
4573 | */ | |
4574 | err = ext4_journal_get_write_access(handle, | |
4575 | EXT4_SB(sb)->s_sbh); | |
4576 | if (err) | |
4577 | goto out_brelse; | |
4578 | ext4_update_dynamic_rev(sb); | |
4579 | EXT4_SET_RO_COMPAT_FEATURE(sb, | |
617ba13b | 4580 | EXT4_FEATURE_RO_COMPAT_LARGE_FILE); |
0390131b | 4581 | ext4_handle_sync(handle); |
b50924c2 | 4582 | err = ext4_handle_dirty_super(handle, sb); |
ac27a0ec DK |
4583 | } |
4584 | } | |
4585 | raw_inode->i_generation = cpu_to_le32(inode->i_generation); | |
4586 | if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) { | |
4587 | if (old_valid_dev(inode->i_rdev)) { | |
4588 | raw_inode->i_block[0] = | |
4589 | cpu_to_le32(old_encode_dev(inode->i_rdev)); | |
4590 | raw_inode->i_block[1] = 0; | |
4591 | } else { | |
4592 | raw_inode->i_block[0] = 0; | |
4593 | raw_inode->i_block[1] = | |
4594 | cpu_to_le32(new_encode_dev(inode->i_rdev)); | |
4595 | raw_inode->i_block[2] = 0; | |
4596 | } | |
f19d5870 | 4597 | } else if (!ext4_has_inline_data(inode)) { |
de9a55b8 TT |
4598 | for (block = 0; block < EXT4_N_BLOCKS; block++) |
4599 | raw_inode->i_block[block] = ei->i_data[block]; | |
f19d5870 | 4600 | } |
ac27a0ec | 4601 | |
25ec56b5 JNC |
4602 | raw_inode->i_disk_version = cpu_to_le32(inode->i_version); |
4603 | if (ei->i_extra_isize) { | |
4604 | if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi)) | |
4605 | raw_inode->i_version_hi = | |
4606 | cpu_to_le32(inode->i_version >> 32); | |
ac27a0ec | 4607 | raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize); |
25ec56b5 JNC |
4608 | } |
4609 | ||
814525f4 DW |
4610 | ext4_inode_csum_set(inode, raw_inode, ei); |
4611 | ||
830156c7 | 4612 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); |
73b50c1c | 4613 | rc = ext4_handle_dirty_metadata(handle, NULL, bh); |
830156c7 FM |
4614 | if (!err) |
4615 | err = rc; | |
19f5fb7a | 4616 | ext4_clear_inode_state(inode, EXT4_STATE_NEW); |
ac27a0ec | 4617 | |
b71fc079 | 4618 | ext4_update_inode_fsync_trans(handle, inode, need_datasync); |
ac27a0ec | 4619 | out_brelse: |
af5bc92d | 4620 | brelse(bh); |
617ba13b | 4621 | ext4_std_error(inode->i_sb, err); |
ac27a0ec DK |
4622 | return err; |
4623 | } | |
4624 | ||
4625 | /* | |
617ba13b | 4626 | * ext4_write_inode() |
ac27a0ec DK |
4627 | * |
4628 | * We are called from a few places: | |
4629 | * | |
4630 | * - Within generic_file_write() for O_SYNC files. | |
4631 | * Here, there will be no transaction running. We wait for any running | |
4907cb7b | 4632 | * transaction to commit. |
ac27a0ec DK |
4633 | * |
4634 | * - Within sys_sync(), kupdate and such. | |
4635 | * We wait on commit, if tol to. | |
4636 | * | |
4637 | * - Within prune_icache() (PF_MEMALLOC == true) | |
4638 | * Here we simply return. We can't afford to block kswapd on the | |
4639 | * journal commit. | |
4640 | * | |
4641 | * In all cases it is actually safe for us to return without doing anything, | |
4642 | * because the inode has been copied into a raw inode buffer in | |
617ba13b | 4643 | * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for |
ac27a0ec DK |
4644 | * knfsd. |
4645 | * | |
4646 | * Note that we are absolutely dependent upon all inode dirtiers doing the | |
4647 | * right thing: they *must* call mark_inode_dirty() after dirtying info in | |
4648 | * which we are interested. | |
4649 | * | |
4650 | * It would be a bug for them to not do this. The code: | |
4651 | * | |
4652 | * mark_inode_dirty(inode) | |
4653 | * stuff(); | |
4654 | * inode->i_size = expr; | |
4655 | * | |
4656 | * is in error because a kswapd-driven write_inode() could occur while | |
4657 | * `stuff()' is running, and the new i_size will be lost. Plus the inode | |
4658 | * will no longer be on the superblock's dirty inode list. | |
4659 | */ | |
a9185b41 | 4660 | int ext4_write_inode(struct inode *inode, struct writeback_control *wbc) |
ac27a0ec | 4661 | { |
91ac6f43 FM |
4662 | int err; |
4663 | ||
ac27a0ec DK |
4664 | if (current->flags & PF_MEMALLOC) |
4665 | return 0; | |
4666 | ||
91ac6f43 FM |
4667 | if (EXT4_SB(inode->i_sb)->s_journal) { |
4668 | if (ext4_journal_current_handle()) { | |
4669 | jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n"); | |
4670 | dump_stack(); | |
4671 | return -EIO; | |
4672 | } | |
ac27a0ec | 4673 | |
a9185b41 | 4674 | if (wbc->sync_mode != WB_SYNC_ALL) |
91ac6f43 FM |
4675 | return 0; |
4676 | ||
4677 | err = ext4_force_commit(inode->i_sb); | |
4678 | } else { | |
4679 | struct ext4_iloc iloc; | |
ac27a0ec | 4680 | |
8b472d73 | 4681 | err = __ext4_get_inode_loc(inode, &iloc, 0); |
91ac6f43 FM |
4682 | if (err) |
4683 | return err; | |
a9185b41 | 4684 | if (wbc->sync_mode == WB_SYNC_ALL) |
830156c7 FM |
4685 | sync_dirty_buffer(iloc.bh); |
4686 | if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) { | |
c398eda0 TT |
4687 | EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr, |
4688 | "IO error syncing inode"); | |
830156c7 FM |
4689 | err = -EIO; |
4690 | } | |
fd2dd9fb | 4691 | brelse(iloc.bh); |
91ac6f43 FM |
4692 | } |
4693 | return err; | |
ac27a0ec DK |
4694 | } |
4695 | ||
53e87268 JK |
4696 | /* |
4697 | * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate | |
4698 | * buffers that are attached to a page stradding i_size and are undergoing | |
4699 | * commit. In that case we have to wait for commit to finish and try again. | |
4700 | */ | |
4701 | static void ext4_wait_for_tail_page_commit(struct inode *inode) | |
4702 | { | |
4703 | struct page *page; | |
4704 | unsigned offset; | |
4705 | journal_t *journal = EXT4_SB(inode->i_sb)->s_journal; | |
4706 | tid_t commit_tid = 0; | |
4707 | int ret; | |
4708 | ||
4709 | offset = inode->i_size & (PAGE_CACHE_SIZE - 1); | |
4710 | /* | |
4711 | * All buffers in the last page remain valid? Then there's nothing to | |
4712 | * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE == | |
4713 | * blocksize case | |
4714 | */ | |
4715 | if (offset > PAGE_CACHE_SIZE - (1 << inode->i_blkbits)) | |
4716 | return; | |
4717 | while (1) { | |
4718 | page = find_lock_page(inode->i_mapping, | |
4719 | inode->i_size >> PAGE_CACHE_SHIFT); | |
4720 | if (!page) | |
4721 | return; | |
4722 | ret = __ext4_journalled_invalidatepage(page, offset); | |
4723 | unlock_page(page); | |
4724 | page_cache_release(page); | |
4725 | if (ret != -EBUSY) | |
4726 | return; | |
4727 | commit_tid = 0; | |
4728 | read_lock(&journal->j_state_lock); | |
4729 | if (journal->j_committing_transaction) | |
4730 | commit_tid = journal->j_committing_transaction->t_tid; | |
4731 | read_unlock(&journal->j_state_lock); | |
4732 | if (commit_tid) | |
4733 | jbd2_log_wait_commit(journal, commit_tid); | |
4734 | } | |
4735 | } | |
4736 | ||
ac27a0ec | 4737 | /* |
617ba13b | 4738 | * ext4_setattr() |
ac27a0ec DK |
4739 | * |
4740 | * Called from notify_change. | |
4741 | * | |
4742 | * We want to trap VFS attempts to truncate the file as soon as | |
4743 | * possible. In particular, we want to make sure that when the VFS | |
4744 | * shrinks i_size, we put the inode on the orphan list and modify | |
4745 | * i_disksize immediately, so that during the subsequent flushing of | |
4746 | * dirty pages and freeing of disk blocks, we can guarantee that any | |
4747 | * commit will leave the blocks being flushed in an unused state on | |
4748 | * disk. (On recovery, the inode will get truncated and the blocks will | |
4749 | * be freed, so we have a strong guarantee that no future commit will | |
4750 | * leave these blocks visible to the user.) | |
4751 | * | |
678aaf48 JK |
4752 | * Another thing we have to assure is that if we are in ordered mode |
4753 | * and inode is still attached to the committing transaction, we must | |
4754 | * we start writeout of all the dirty pages which are being truncated. | |
4755 | * This way we are sure that all the data written in the previous | |
4756 | * transaction are already on disk (truncate waits for pages under | |
4757 | * writeback). | |
4758 | * | |
4759 | * Called with inode->i_mutex down. | |
ac27a0ec | 4760 | */ |
617ba13b | 4761 | int ext4_setattr(struct dentry *dentry, struct iattr *attr) |
ac27a0ec DK |
4762 | { |
4763 | struct inode *inode = dentry->d_inode; | |
4764 | int error, rc = 0; | |
3d287de3 | 4765 | int orphan = 0; |
ac27a0ec DK |
4766 | const unsigned int ia_valid = attr->ia_valid; |
4767 | ||
4768 | error = inode_change_ok(inode, attr); | |
4769 | if (error) | |
4770 | return error; | |
4771 | ||
12755627 | 4772 | if (is_quota_modification(inode, attr)) |
871a2931 | 4773 | dquot_initialize(inode); |
08cefc7a EB |
4774 | if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) || |
4775 | (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) { | |
ac27a0ec DK |
4776 | handle_t *handle; |
4777 | ||
4778 | /* (user+group)*(old+new) structure, inode write (sb, | |
4779 | * inode block, ? - but truncate inode update has it) */ | |
9924a92a TT |
4780 | handle = ext4_journal_start(inode, EXT4_HT_QUOTA, |
4781 | (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) + | |
4782 | EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3); | |
ac27a0ec DK |
4783 | if (IS_ERR(handle)) { |
4784 | error = PTR_ERR(handle); | |
4785 | goto err_out; | |
4786 | } | |
b43fa828 | 4787 | error = dquot_transfer(inode, attr); |
ac27a0ec | 4788 | if (error) { |
617ba13b | 4789 | ext4_journal_stop(handle); |
ac27a0ec DK |
4790 | return error; |
4791 | } | |
4792 | /* Update corresponding info in inode so that everything is in | |
4793 | * one transaction */ | |
4794 | if (attr->ia_valid & ATTR_UID) | |
4795 | inode->i_uid = attr->ia_uid; | |
4796 | if (attr->ia_valid & ATTR_GID) | |
4797 | inode->i_gid = attr->ia_gid; | |
617ba13b MC |
4798 | error = ext4_mark_inode_dirty(handle, inode); |
4799 | ext4_journal_stop(handle); | |
ac27a0ec DK |
4800 | } |
4801 | ||
263c784f JK |
4802 | if (attr->ia_valid & ATTR_SIZE && attr->ia_size != inode->i_size) { |
4803 | handle_t *handle; | |
4804 | loff_t oldsize = inode->i_size; | |
562c72aa | 4805 | |
12e9b892 | 4806 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { |
e2b46574 ES |
4807 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
4808 | ||
0c095c7f TT |
4809 | if (attr->ia_size > sbi->s_bitmap_maxbytes) |
4810 | return -EFBIG; | |
e2b46574 | 4811 | } |
d5685be1 CH |
4812 | |
4813 | if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size) | |
4814 | inode_inc_iversion(inode); | |
4815 | ||
263c784f JK |
4816 | if (S_ISREG(inode->i_mode) && |
4817 | (attr->ia_size < inode->i_size)) { | |
4818 | if (ext4_should_order_data(inode)) { | |
4819 | error = ext4_begin_ordered_truncate(inode, | |
678aaf48 | 4820 | attr->ia_size); |
263c784f | 4821 | if (error) |
678aaf48 | 4822 | goto err_out; |
263c784f JK |
4823 | } |
4824 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 3); | |
4825 | if (IS_ERR(handle)) { | |
4826 | error = PTR_ERR(handle); | |
4827 | goto err_out; | |
4828 | } | |
4829 | if (ext4_handle_valid(handle)) { | |
4830 | error = ext4_orphan_add(handle, inode); | |
4831 | orphan = 1; | |
4832 | } | |
4833 | EXT4_I(inode)->i_disksize = attr->ia_size; | |
4834 | rc = ext4_mark_inode_dirty(handle, inode); | |
4835 | if (!error) | |
4836 | error = rc; | |
4837 | ext4_journal_stop(handle); | |
4838 | if (error) { | |
4839 | ext4_orphan_del(NULL, inode); | |
678aaf48 JK |
4840 | goto err_out; |
4841 | } | |
4842 | } | |
53e87268 | 4843 | |
263c784f JK |
4844 | i_size_write(inode, attr->ia_size); |
4845 | /* | |
4846 | * Blocks are going to be removed from the inode. Wait | |
4847 | * for dio in flight. Temporarily disable | |
4848 | * dioread_nolock to prevent livelock. | |
4849 | */ | |
4850 | if (orphan) { | |
4851 | if (!ext4_should_journal_data(inode)) { | |
4852 | ext4_inode_block_unlocked_dio(inode); | |
4853 | inode_dio_wait(inode); | |
4854 | ext4_inode_resume_unlocked_dio(inode); | |
4855 | } else | |
4856 | ext4_wait_for_tail_page_commit(inode); | |
1c9114f9 | 4857 | } |
263c784f JK |
4858 | /* |
4859 | * Truncate pagecache after we've waited for commit | |
4860 | * in data=journal mode to make pages freeable. | |
4861 | */ | |
4862 | truncate_pagecache(inode, oldsize, inode->i_size); | |
072bd7ea | 4863 | } |
263c784f JK |
4864 | /* |
4865 | * We want to call ext4_truncate() even if attr->ia_size == | |
4866 | * inode->i_size for cases like truncation of fallocated space | |
4867 | */ | |
4868 | if (attr->ia_valid & ATTR_SIZE) | |
4869 | ext4_truncate(inode); | |
ac27a0ec | 4870 | |
1025774c CH |
4871 | if (!rc) { |
4872 | setattr_copy(inode, attr); | |
4873 | mark_inode_dirty(inode); | |
4874 | } | |
4875 | ||
4876 | /* | |
4877 | * If the call to ext4_truncate failed to get a transaction handle at | |
4878 | * all, we need to clean up the in-core orphan list manually. | |
4879 | */ | |
3d287de3 | 4880 | if (orphan && inode->i_nlink) |
617ba13b | 4881 | ext4_orphan_del(NULL, inode); |
ac27a0ec DK |
4882 | |
4883 | if (!rc && (ia_valid & ATTR_MODE)) | |
617ba13b | 4884 | rc = ext4_acl_chmod(inode); |
ac27a0ec DK |
4885 | |
4886 | err_out: | |
617ba13b | 4887 | ext4_std_error(inode->i_sb, error); |
ac27a0ec DK |
4888 | if (!error) |
4889 | error = rc; | |
4890 | return error; | |
4891 | } | |
4892 | ||
3e3398a0 MC |
4893 | int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry, |
4894 | struct kstat *stat) | |
4895 | { | |
4896 | struct inode *inode; | |
bb39c83c | 4897 | unsigned long long delalloc_blocks; |
3e3398a0 MC |
4898 | |
4899 | inode = dentry->d_inode; | |
4900 | generic_fillattr(inode, stat); | |
4901 | ||
4902 | /* | |
4903 | * We can't update i_blocks if the block allocation is delayed | |
4904 | * otherwise in the case of system crash before the real block | |
4905 | * allocation is done, we will have i_blocks inconsistent with | |
4906 | * on-disk file blocks. | |
4907 | * We always keep i_blocks updated together with real | |
4908 | * allocation. But to not confuse with user, stat | |
4909 | * will return the blocks that include the delayed allocation | |
4910 | * blocks for this file. | |
4911 | */ | |
96607551 TM |
4912 | delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), |
4913 | EXT4_I(inode)->i_reserved_data_blocks); | |
3e3398a0 | 4914 | |
bb39c83c | 4915 | stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits-9); |
3e3398a0 MC |
4916 | return 0; |
4917 | } | |
ac27a0ec | 4918 | |
a02908f1 MC |
4919 | static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) |
4920 | { | |
12e9b892 | 4921 | if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) |
8bb2b247 | 4922 | return ext4_ind_trans_blocks(inode, nrblocks, chunk); |
ac51d837 | 4923 | return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); |
a02908f1 | 4924 | } |
ac51d837 | 4925 | |
ac27a0ec | 4926 | /* |
a02908f1 MC |
4927 | * Account for index blocks, block groups bitmaps and block group |
4928 | * descriptor blocks if modify datablocks and index blocks | |
4929 | * worse case, the indexs blocks spread over different block groups | |
ac27a0ec | 4930 | * |
a02908f1 | 4931 | * If datablocks are discontiguous, they are possible to spread over |
4907cb7b | 4932 | * different block groups too. If they are contiguous, with flexbg, |
a02908f1 | 4933 | * they could still across block group boundary. |
ac27a0ec | 4934 | * |
a02908f1 MC |
4935 | * Also account for superblock, inode, quota and xattr blocks |
4936 | */ | |
1f109d5a | 4937 | static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) |
a02908f1 | 4938 | { |
8df9675f TT |
4939 | ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); |
4940 | int gdpblocks; | |
a02908f1 MC |
4941 | int idxblocks; |
4942 | int ret = 0; | |
4943 | ||
4944 | /* | |
4945 | * How many index blocks need to touch to modify nrblocks? | |
4946 | * The "Chunk" flag indicating whether the nrblocks is | |
4947 | * physically contiguous on disk | |
4948 | * | |
4949 | * For Direct IO and fallocate, they calls get_block to allocate | |
4950 | * one single extent at a time, so they could set the "Chunk" flag | |
4951 | */ | |
4952 | idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); | |
4953 | ||
4954 | ret = idxblocks; | |
4955 | ||
4956 | /* | |
4957 | * Now let's see how many group bitmaps and group descriptors need | |
4958 | * to account | |
4959 | */ | |
4960 | groups = idxblocks; | |
4961 | if (chunk) | |
4962 | groups += 1; | |
4963 | else | |
4964 | groups += nrblocks; | |
4965 | ||
4966 | gdpblocks = groups; | |
8df9675f TT |
4967 | if (groups > ngroups) |
4968 | groups = ngroups; | |
a02908f1 MC |
4969 | if (groups > EXT4_SB(inode->i_sb)->s_gdb_count) |
4970 | gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count; | |
4971 | ||
4972 | /* bitmaps and block group descriptor blocks */ | |
4973 | ret += groups + gdpblocks; | |
4974 | ||
4975 | /* Blocks for super block, inode, quota and xattr blocks */ | |
4976 | ret += EXT4_META_TRANS_BLOCKS(inode->i_sb); | |
4977 | ||
4978 | return ret; | |
4979 | } | |
4980 | ||
4981 | /* | |
25985edc | 4982 | * Calculate the total number of credits to reserve to fit |
f3bd1f3f MC |
4983 | * the modification of a single pages into a single transaction, |
4984 | * which may include multiple chunks of block allocations. | |
ac27a0ec | 4985 | * |
525f4ed8 | 4986 | * This could be called via ext4_write_begin() |
ac27a0ec | 4987 | * |
525f4ed8 | 4988 | * We need to consider the worse case, when |
a02908f1 | 4989 | * one new block per extent. |
ac27a0ec | 4990 | */ |
a86c6181 | 4991 | int ext4_writepage_trans_blocks(struct inode *inode) |
ac27a0ec | 4992 | { |
617ba13b | 4993 | int bpp = ext4_journal_blocks_per_page(inode); |
ac27a0ec DK |
4994 | int ret; |
4995 | ||
a02908f1 | 4996 | ret = ext4_meta_trans_blocks(inode, bpp, 0); |
a86c6181 | 4997 | |
a02908f1 | 4998 | /* Account for data blocks for journalled mode */ |
617ba13b | 4999 | if (ext4_should_journal_data(inode)) |
a02908f1 | 5000 | ret += bpp; |
ac27a0ec DK |
5001 | return ret; |
5002 | } | |
f3bd1f3f MC |
5003 | |
5004 | /* | |
5005 | * Calculate the journal credits for a chunk of data modification. | |
5006 | * | |
5007 | * This is called from DIO, fallocate or whoever calling | |
79e83036 | 5008 | * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks. |
f3bd1f3f MC |
5009 | * |
5010 | * journal buffers for data blocks are not included here, as DIO | |
5011 | * and fallocate do no need to journal data buffers. | |
5012 | */ | |
5013 | int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks) | |
5014 | { | |
5015 | return ext4_meta_trans_blocks(inode, nrblocks, 1); | |
5016 | } | |
5017 | ||
ac27a0ec | 5018 | /* |
617ba13b | 5019 | * The caller must have previously called ext4_reserve_inode_write(). |
ac27a0ec DK |
5020 | * Give this, we know that the caller already has write access to iloc->bh. |
5021 | */ | |
617ba13b | 5022 | int ext4_mark_iloc_dirty(handle_t *handle, |
de9a55b8 | 5023 | struct inode *inode, struct ext4_iloc *iloc) |
ac27a0ec DK |
5024 | { |
5025 | int err = 0; | |
5026 | ||
c64db50e | 5027 | if (IS_I_VERSION(inode)) |
25ec56b5 JNC |
5028 | inode_inc_iversion(inode); |
5029 | ||
ac27a0ec DK |
5030 | /* the do_update_inode consumes one bh->b_count */ |
5031 | get_bh(iloc->bh); | |
5032 | ||
dab291af | 5033 | /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */ |
830156c7 | 5034 | err = ext4_do_update_inode(handle, inode, iloc); |
ac27a0ec DK |
5035 | put_bh(iloc->bh); |
5036 | return err; | |
5037 | } | |
5038 | ||
5039 | /* | |
5040 | * On success, We end up with an outstanding reference count against | |
5041 | * iloc->bh. This _must_ be cleaned up later. | |
5042 | */ | |
5043 | ||
5044 | int | |
617ba13b MC |
5045 | ext4_reserve_inode_write(handle_t *handle, struct inode *inode, |
5046 | struct ext4_iloc *iloc) | |
ac27a0ec | 5047 | { |
0390131b FM |
5048 | int err; |
5049 | ||
5050 | err = ext4_get_inode_loc(inode, iloc); | |
5051 | if (!err) { | |
5052 | BUFFER_TRACE(iloc->bh, "get_write_access"); | |
5053 | err = ext4_journal_get_write_access(handle, iloc->bh); | |
5054 | if (err) { | |
5055 | brelse(iloc->bh); | |
5056 | iloc->bh = NULL; | |
ac27a0ec DK |
5057 | } |
5058 | } | |
617ba13b | 5059 | ext4_std_error(inode->i_sb, err); |
ac27a0ec DK |
5060 | return err; |
5061 | } | |
5062 | ||
6dd4ee7c KS |
5063 | /* |
5064 | * Expand an inode by new_extra_isize bytes. | |
5065 | * Returns 0 on success or negative error number on failure. | |
5066 | */ | |
1d03ec98 AK |
5067 | static int ext4_expand_extra_isize(struct inode *inode, |
5068 | unsigned int new_extra_isize, | |
5069 | struct ext4_iloc iloc, | |
5070 | handle_t *handle) | |
6dd4ee7c KS |
5071 | { |
5072 | struct ext4_inode *raw_inode; | |
5073 | struct ext4_xattr_ibody_header *header; | |
6dd4ee7c KS |
5074 | |
5075 | if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) | |
5076 | return 0; | |
5077 | ||
5078 | raw_inode = ext4_raw_inode(&iloc); | |
5079 | ||
5080 | header = IHDR(inode, raw_inode); | |
6dd4ee7c KS |
5081 | |
5082 | /* No extended attributes present */ | |
19f5fb7a TT |
5083 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) || |
5084 | header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) { | |
6dd4ee7c KS |
5085 | memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0, |
5086 | new_extra_isize); | |
5087 | EXT4_I(inode)->i_extra_isize = new_extra_isize; | |
5088 | return 0; | |
5089 | } | |
5090 | ||
5091 | /* try to expand with EAs present */ | |
5092 | return ext4_expand_extra_isize_ea(inode, new_extra_isize, | |
5093 | raw_inode, handle); | |
5094 | } | |
5095 | ||
ac27a0ec DK |
5096 | /* |
5097 | * What we do here is to mark the in-core inode as clean with respect to inode | |
5098 | * dirtiness (it may still be data-dirty). | |
5099 | * This means that the in-core inode may be reaped by prune_icache | |
5100 | * without having to perform any I/O. This is a very good thing, | |
5101 | * because *any* task may call prune_icache - even ones which | |
5102 | * have a transaction open against a different journal. | |
5103 | * | |
5104 | * Is this cheating? Not really. Sure, we haven't written the | |
5105 | * inode out, but prune_icache isn't a user-visible syncing function. | |
5106 | * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync) | |
5107 | * we start and wait on commits. | |
ac27a0ec | 5108 | */ |
617ba13b | 5109 | int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode) |
ac27a0ec | 5110 | { |
617ba13b | 5111 | struct ext4_iloc iloc; |
6dd4ee7c KS |
5112 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
5113 | static unsigned int mnt_count; | |
5114 | int err, ret; | |
ac27a0ec DK |
5115 | |
5116 | might_sleep(); | |
7ff9c073 | 5117 | trace_ext4_mark_inode_dirty(inode, _RET_IP_); |
617ba13b | 5118 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
308f438a EG |
5119 | if (err) |
5120 | return err; | |
0390131b FM |
5121 | if (ext4_handle_valid(handle) && |
5122 | EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize && | |
19f5fb7a | 5123 | !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) { |
6dd4ee7c KS |
5124 | /* |
5125 | * We need extra buffer credits since we may write into EA block | |
5126 | * with this same handle. If journal_extend fails, then it will | |
5127 | * only result in a minor loss of functionality for that inode. | |
5128 | * If this is felt to be critical, then e2fsck should be run to | |
5129 | * force a large enough s_min_extra_isize. | |
5130 | */ | |
5131 | if ((jbd2_journal_extend(handle, | |
5132 | EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) { | |
5133 | ret = ext4_expand_extra_isize(inode, | |
5134 | sbi->s_want_extra_isize, | |
5135 | iloc, handle); | |
5136 | if (ret) { | |
19f5fb7a TT |
5137 | ext4_set_inode_state(inode, |
5138 | EXT4_STATE_NO_EXPAND); | |
c1bddad9 AK |
5139 | if (mnt_count != |
5140 | le16_to_cpu(sbi->s_es->s_mnt_count)) { | |
12062ddd | 5141 | ext4_warning(inode->i_sb, |
6dd4ee7c KS |
5142 | "Unable to expand inode %lu. Delete" |
5143 | " some EAs or run e2fsck.", | |
5144 | inode->i_ino); | |
c1bddad9 AK |
5145 | mnt_count = |
5146 | le16_to_cpu(sbi->s_es->s_mnt_count); | |
6dd4ee7c KS |
5147 | } |
5148 | } | |
5149 | } | |
5150 | } | |
308f438a | 5151 | return ext4_mark_iloc_dirty(handle, inode, &iloc); |
ac27a0ec DK |
5152 | } |
5153 | ||
5154 | /* | |
617ba13b | 5155 | * ext4_dirty_inode() is called from __mark_inode_dirty() |
ac27a0ec DK |
5156 | * |
5157 | * We're really interested in the case where a file is being extended. | |
5158 | * i_size has been changed by generic_commit_write() and we thus need | |
5159 | * to include the updated inode in the current transaction. | |
5160 | * | |
5dd4056d | 5161 | * Also, dquot_alloc_block() will always dirty the inode when blocks |
ac27a0ec DK |
5162 | * are allocated to the file. |
5163 | * | |
5164 | * If the inode is marked synchronous, we don't honour that here - doing | |
5165 | * so would cause a commit on atime updates, which we don't bother doing. | |
5166 | * We handle synchronous inodes at the highest possible level. | |
5167 | */ | |
aa385729 | 5168 | void ext4_dirty_inode(struct inode *inode, int flags) |
ac27a0ec | 5169 | { |
ac27a0ec DK |
5170 | handle_t *handle; |
5171 | ||
9924a92a | 5172 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 2); |
ac27a0ec DK |
5173 | if (IS_ERR(handle)) |
5174 | goto out; | |
f3dc272f | 5175 | |
f3dc272f CW |
5176 | ext4_mark_inode_dirty(handle, inode); |
5177 | ||
617ba13b | 5178 | ext4_journal_stop(handle); |
ac27a0ec DK |
5179 | out: |
5180 | return; | |
5181 | } | |
5182 | ||
5183 | #if 0 | |
5184 | /* | |
5185 | * Bind an inode's backing buffer_head into this transaction, to prevent | |
5186 | * it from being flushed to disk early. Unlike | |
617ba13b | 5187 | * ext4_reserve_inode_write, this leaves behind no bh reference and |
ac27a0ec DK |
5188 | * returns no iloc structure, so the caller needs to repeat the iloc |
5189 | * lookup to mark the inode dirty later. | |
5190 | */ | |
617ba13b | 5191 | static int ext4_pin_inode(handle_t *handle, struct inode *inode) |
ac27a0ec | 5192 | { |
617ba13b | 5193 | struct ext4_iloc iloc; |
ac27a0ec DK |
5194 | |
5195 | int err = 0; | |
5196 | if (handle) { | |
617ba13b | 5197 | err = ext4_get_inode_loc(inode, &iloc); |
ac27a0ec DK |
5198 | if (!err) { |
5199 | BUFFER_TRACE(iloc.bh, "get_write_access"); | |
dab291af | 5200 | err = jbd2_journal_get_write_access(handle, iloc.bh); |
ac27a0ec | 5201 | if (!err) |
0390131b | 5202 | err = ext4_handle_dirty_metadata(handle, |
73b50c1c | 5203 | NULL, |
0390131b | 5204 | iloc.bh); |
ac27a0ec DK |
5205 | brelse(iloc.bh); |
5206 | } | |
5207 | } | |
617ba13b | 5208 | ext4_std_error(inode->i_sb, err); |
ac27a0ec DK |
5209 | return err; |
5210 | } | |
5211 | #endif | |
5212 | ||
617ba13b | 5213 | int ext4_change_inode_journal_flag(struct inode *inode, int val) |
ac27a0ec DK |
5214 | { |
5215 | journal_t *journal; | |
5216 | handle_t *handle; | |
5217 | int err; | |
5218 | ||
5219 | /* | |
5220 | * We have to be very careful here: changing a data block's | |
5221 | * journaling status dynamically is dangerous. If we write a | |
5222 | * data block to the journal, change the status and then delete | |
5223 | * that block, we risk forgetting to revoke the old log record | |
5224 | * from the journal and so a subsequent replay can corrupt data. | |
5225 | * So, first we make sure that the journal is empty and that | |
5226 | * nobody is changing anything. | |
5227 | */ | |
5228 | ||
617ba13b | 5229 | journal = EXT4_JOURNAL(inode); |
0390131b FM |
5230 | if (!journal) |
5231 | return 0; | |
d699594d | 5232 | if (is_journal_aborted(journal)) |
ac27a0ec | 5233 | return -EROFS; |
2aff57b0 YY |
5234 | /* We have to allocate physical blocks for delalloc blocks |
5235 | * before flushing journal. otherwise delalloc blocks can not | |
5236 | * be allocated any more. even more truncate on delalloc blocks | |
5237 | * could trigger BUG by flushing delalloc blocks in journal. | |
5238 | * There is no delalloc block in non-journal data mode. | |
5239 | */ | |
5240 | if (val && test_opt(inode->i_sb, DELALLOC)) { | |
5241 | err = ext4_alloc_da_blocks(inode); | |
5242 | if (err < 0) | |
5243 | return err; | |
5244 | } | |
ac27a0ec | 5245 | |
17335dcc DM |
5246 | /* Wait for all existing dio workers */ |
5247 | ext4_inode_block_unlocked_dio(inode); | |
5248 | inode_dio_wait(inode); | |
5249 | ||
dab291af | 5250 | jbd2_journal_lock_updates(journal); |
ac27a0ec DK |
5251 | |
5252 | /* | |
5253 | * OK, there are no updates running now, and all cached data is | |
5254 | * synced to disk. We are now in a completely consistent state | |
5255 | * which doesn't have anything in the journal, and we know that | |
5256 | * no filesystem updates are running, so it is safe to modify | |
5257 | * the inode's in-core data-journaling state flag now. | |
5258 | */ | |
5259 | ||
5260 | if (val) | |
12e9b892 | 5261 | ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); |
5872ddaa YY |
5262 | else { |
5263 | jbd2_journal_flush(journal); | |
12e9b892 | 5264 | ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA); |
5872ddaa | 5265 | } |
617ba13b | 5266 | ext4_set_aops(inode); |
ac27a0ec | 5267 | |
dab291af | 5268 | jbd2_journal_unlock_updates(journal); |
17335dcc | 5269 | ext4_inode_resume_unlocked_dio(inode); |
ac27a0ec DK |
5270 | |
5271 | /* Finally we can mark the inode as dirty. */ | |
5272 | ||
9924a92a | 5273 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); |
ac27a0ec DK |
5274 | if (IS_ERR(handle)) |
5275 | return PTR_ERR(handle); | |
5276 | ||
617ba13b | 5277 | err = ext4_mark_inode_dirty(handle, inode); |
0390131b | 5278 | ext4_handle_sync(handle); |
617ba13b MC |
5279 | ext4_journal_stop(handle); |
5280 | ext4_std_error(inode->i_sb, err); | |
ac27a0ec DK |
5281 | |
5282 | return err; | |
5283 | } | |
2e9ee850 AK |
5284 | |
5285 | static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh) | |
5286 | { | |
5287 | return !buffer_mapped(bh); | |
5288 | } | |
5289 | ||
c2ec175c | 5290 | int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) |
2e9ee850 | 5291 | { |
c2ec175c | 5292 | struct page *page = vmf->page; |
2e9ee850 AK |
5293 | loff_t size; |
5294 | unsigned long len; | |
9ea7df53 | 5295 | int ret; |
2e9ee850 | 5296 | struct file *file = vma->vm_file; |
496ad9aa | 5297 | struct inode *inode = file_inode(file); |
2e9ee850 | 5298 | struct address_space *mapping = inode->i_mapping; |
9ea7df53 JK |
5299 | handle_t *handle; |
5300 | get_block_t *get_block; | |
5301 | int retries = 0; | |
2e9ee850 | 5302 | |
8e8ad8a5 | 5303 | sb_start_pagefault(inode->i_sb); |
041bbb6d | 5304 | file_update_time(vma->vm_file); |
9ea7df53 JK |
5305 | /* Delalloc case is easy... */ |
5306 | if (test_opt(inode->i_sb, DELALLOC) && | |
5307 | !ext4_should_journal_data(inode) && | |
5308 | !ext4_nonda_switch(inode->i_sb)) { | |
5309 | do { | |
5310 | ret = __block_page_mkwrite(vma, vmf, | |
5311 | ext4_da_get_block_prep); | |
5312 | } while (ret == -ENOSPC && | |
5313 | ext4_should_retry_alloc(inode->i_sb, &retries)); | |
5314 | goto out_ret; | |
2e9ee850 | 5315 | } |
0e499890 DW |
5316 | |
5317 | lock_page(page); | |
9ea7df53 JK |
5318 | size = i_size_read(inode); |
5319 | /* Page got truncated from under us? */ | |
5320 | if (page->mapping != mapping || page_offset(page) > size) { | |
5321 | unlock_page(page); | |
5322 | ret = VM_FAULT_NOPAGE; | |
5323 | goto out; | |
0e499890 | 5324 | } |
2e9ee850 AK |
5325 | |
5326 | if (page->index == size >> PAGE_CACHE_SHIFT) | |
5327 | len = size & ~PAGE_CACHE_MASK; | |
5328 | else | |
5329 | len = PAGE_CACHE_SIZE; | |
a827eaff | 5330 | /* |
9ea7df53 JK |
5331 | * Return if we have all the buffers mapped. This avoids the need to do |
5332 | * journal_start/journal_stop which can block and take a long time | |
a827eaff | 5333 | */ |
2e9ee850 | 5334 | if (page_has_buffers(page)) { |
f19d5870 TM |
5335 | if (!ext4_walk_page_buffers(NULL, page_buffers(page), |
5336 | 0, len, NULL, | |
5337 | ext4_bh_unmapped)) { | |
9ea7df53 | 5338 | /* Wait so that we don't change page under IO */ |
1d1d1a76 | 5339 | wait_for_stable_page(page); |
9ea7df53 JK |
5340 | ret = VM_FAULT_LOCKED; |
5341 | goto out; | |
a827eaff | 5342 | } |
2e9ee850 | 5343 | } |
a827eaff | 5344 | unlock_page(page); |
9ea7df53 JK |
5345 | /* OK, we need to fill the hole... */ |
5346 | if (ext4_should_dioread_nolock(inode)) | |
5347 | get_block = ext4_get_block_write; | |
5348 | else | |
5349 | get_block = ext4_get_block; | |
5350 | retry_alloc: | |
9924a92a TT |
5351 | handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, |
5352 | ext4_writepage_trans_blocks(inode)); | |
9ea7df53 | 5353 | if (IS_ERR(handle)) { |
c2ec175c | 5354 | ret = VM_FAULT_SIGBUS; |
9ea7df53 JK |
5355 | goto out; |
5356 | } | |
5357 | ret = __block_page_mkwrite(vma, vmf, get_block); | |
5358 | if (!ret && ext4_should_journal_data(inode)) { | |
f19d5870 | 5359 | if (ext4_walk_page_buffers(handle, page_buffers(page), 0, |
9ea7df53 JK |
5360 | PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) { |
5361 | unlock_page(page); | |
5362 | ret = VM_FAULT_SIGBUS; | |
fcbb5515 | 5363 | ext4_journal_stop(handle); |
9ea7df53 JK |
5364 | goto out; |
5365 | } | |
5366 | ext4_set_inode_state(inode, EXT4_STATE_JDATA); | |
5367 | } | |
5368 | ext4_journal_stop(handle); | |
5369 | if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) | |
5370 | goto retry_alloc; | |
5371 | out_ret: | |
5372 | ret = block_page_mkwrite_return(ret); | |
5373 | out: | |
8e8ad8a5 | 5374 | sb_end_pagefault(inode->i_sb); |
2e9ee850 AK |
5375 | return ret; |
5376 | } |