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