blob: 73482fc865617282ea839bb65cd841d24ef6add4 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002/*
Mingming Cao617ba132006-10-11 01:20:53 -07003 * linux/fs/ext4/inode.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004 *
5 * Copyright (C) 1992, 1993, 1994, 1995
6 * Remy Card (card@masi.ibp.fr)
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
9 *
10 * from
11 *
12 * linux/fs/minix/inode.c
13 *
14 * Copyright (C) 1991, 1992 Linus Torvalds
15 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -070016 * 64-bit file support on 64-bit platforms by Jakub Jelinek
17 * (jj@sunsite.ms.mff.cuni.cz)
18 *
Mingming Cao617ba132006-10-11 01:20:53 -070019 * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000
Dave Kleikampac27a0e2006-10-11 01:20:50 -070020 */
21
Dave Kleikampac27a0e2006-10-11 01:20:50 -070022#include <linux/fs.h>
23#include <linux/time.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070024#include <linux/highuid.h>
25#include <linux/pagemap.h>
Matthew Wilcoxc94c2ac2015-09-08 14:58:40 -070026#include <linux/dax.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070027#include <linux/quotaops.h>
28#include <linux/string.h>
29#include <linux/buffer_head.h>
30#include <linux/writeback.h>
Alex Tomas64769242008-07-11 19:27:31 -040031#include <linux/pagevec.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070032#include <linux/mpage.h>
Duane Griffine83c1392008-12-19 20:47:15 +000033#include <linux/namei.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070034#include <linux/uio.h>
35#include <linux/bio.h>
Mingming Cao4c0425f2009-09-28 15:48:41 -040036#include <linux/workqueue.h>
Jiaying Zhang744692d2010-03-04 16:14:02 -050037#include <linux/kernel.h>
Andrew Morton6db26ff2011-01-12 16:59:13 -080038#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Theodore Ts'o00a1a052014-03-30 10:20:01 -040040#include <linux/bitops.h>
Jan Kara364443c2016-11-20 17:36:06 -050041#include <linux/iomap.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050042#include <linux/iversion.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040043
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040044#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070045#include "xattr.h"
46#include "acl.h"
Theodore Ts'o9f125d62011-06-27 19:16:04 -040047#include "truncate.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070048
Theodore Ts'o9bffad12009-06-17 11:48:11 -040049#include <trace/events/ext4.h>
50
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -040051#define MPAGE_DA_EXTENT_TAIL 0x01
52
Darrick J. Wong814525f2012-04-29 18:31:10 -040053static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw,
54 struct ext4_inode_info *ei)
55{
56 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Darrick J. Wong814525f2012-04-29 18:31:10 -040057 __u32 csum;
Daeho Jeongb47820e2016-07-03 17:51:39 -040058 __u16 dummy_csum = 0;
59 int offset = offsetof(struct ext4_inode, i_checksum_lo);
60 unsigned int csum_size = sizeof(dummy_csum);
Darrick J. Wong814525f2012-04-29 18:31:10 -040061
Daeho Jeongb47820e2016-07-03 17:51:39 -040062 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, offset);
63 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, csum_size);
64 offset += csum_size;
65 csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset,
66 EXT4_GOOD_OLD_INODE_SIZE - offset);
67
68 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
69 offset = offsetof(struct ext4_inode, i_checksum_hi);
70 csum = ext4_chksum(sbi, csum, (__u8 *)raw +
71 EXT4_GOOD_OLD_INODE_SIZE,
72 offset - EXT4_GOOD_OLD_INODE_SIZE);
73 if (EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) {
74 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum,
75 csum_size);
76 offset += csum_size;
Daeho Jeongb47820e2016-07-03 17:51:39 -040077 }
Daeho Jeong05ac5aa2016-12-01 11:49:12 -050078 csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset,
79 EXT4_INODE_SIZE(inode->i_sb) - offset);
Darrick J. Wong814525f2012-04-29 18:31:10 -040080 }
81
Darrick J. Wong814525f2012-04-29 18:31:10 -040082 return csum;
83}
84
85static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw,
86 struct ext4_inode_info *ei)
87{
88 __u32 provided, calculated;
89
90 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
91 cpu_to_le32(EXT4_OS_LINUX) ||
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -040092 !ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong814525f2012-04-29 18:31:10 -040093 return 1;
94
95 provided = le16_to_cpu(raw->i_checksum_lo);
96 calculated = ext4_inode_csum(inode, raw, ei);
97 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
98 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
99 provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16;
100 else
101 calculated &= 0xFFFF;
102
103 return provided == calculated;
104}
105
106static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
107 struct ext4_inode_info *ei)
108{
109 __u32 csum;
110
111 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
112 cpu_to_le32(EXT4_OS_LINUX) ||
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400113 !ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong814525f2012-04-29 18:31:10 -0400114 return;
115
116 csum = ext4_inode_csum(inode, raw, ei);
117 raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF);
118 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
119 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
120 raw->i_checksum_hi = cpu_to_le16(csum >> 16);
121}
122
Jan Kara678aaf42008-07-11 19:27:31 -0400123static inline int ext4_begin_ordered_truncate(struct inode *inode,
124 loff_t new_size)
125{
Theodore Ts'o7ff9c072010-11-08 13:51:33 -0500126 trace_ext4_begin_ordered_truncate(inode, new_size);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -0500127 /*
128 * If jinode is zero, then we never opened the file for
129 * writing, so there's no need to call
130 * jbd2_journal_begin_ordered_truncate() since there's no
131 * outstanding writes we need to flush.
132 */
133 if (!EXT4_I(inode)->jinode)
134 return 0;
135 return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
136 EXT4_I(inode)->jinode,
137 new_size);
Jan Kara678aaf42008-07-11 19:27:31 -0400138}
139
Lukas Czernerd47992f2013-05-21 23:17:23 -0400140static void ext4_invalidatepage(struct page *page, unsigned int offset,
141 unsigned int length);
Theodore Ts'ocb20d512010-10-27 21:30:09 -0400142static int __ext4_journalled_writepage(struct page *page, unsigned int len);
143static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400144static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
145 int pextents);
Alex Tomas64769242008-07-11 19:27:31 -0400146
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700147/*
148 * Test whether an inode is a fast symlink.
Tahsin Erdogan407cd7f2017-07-04 00:11:21 -0400149 * A fast symlink has its symlink data stored in ext4_inode_info->i_data.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700150 */
Theodore Ts'of348c252015-04-16 01:55:00 -0400151int ext4_inode_is_fast_symlink(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700152{
Andi Kleenfc822282017-12-03 20:38:01 -0500153 if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
154 int ea_blocks = EXT4_I(inode)->i_file_acl ?
155 EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0;
156
157 if (ext4_has_inline_data(inode))
158 return 0;
159
160 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
161 }
Tahsin Erdogan407cd7f2017-07-04 00:11:21 -0400162 return S_ISLNK(inode->i_mode) && inode->i_size &&
163 (inode->i_size < EXT4_N_BLOCKS * 4);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700164}
165
166/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700167 * Called at the last iput() if i_nlink is zero.
168 */
Al Viro0930fcc2010-06-07 13:16:22 -0400169void ext4_evict_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700170{
171 handle_t *handle;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400172 int err;
Jan Kara65db8692019-11-05 17:44:12 +0100173 /*
174 * Credits for final inode cleanup and freeing:
175 * sb + inode (ext4_orphan_del()), block bitmap, group descriptor
176 * (xattr block freeing), bitmap, group descriptor (inode freeing)
177 */
178 int extra_credits = 6;
Tahsin Erdogan0421a182017-06-22 10:26:31 -0400179 struct ext4_xattr_inode_array *ea_inode_array = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700180
Theodore Ts'o7ff9c072010-11-08 13:51:33 -0500181 trace_ext4_evict_inode(inode);
Jiaying Zhang2581fdc2011-08-13 12:17:13 -0400182
Al Viro0930fcc2010-06-07 13:16:22 -0400183 if (inode->i_nlink) {
Jan Kara2d859db2011-07-26 09:07:11 -0400184 /*
185 * When journalling data dirty buffers are tracked only in the
186 * journal. So although mm thinks everything is clean and
187 * ready for reaping the inode might still have some pages to
188 * write in the running transaction or waiting to be
189 * checkpointed. Thus calling jbd2_journal_invalidatepage()
190 * (via truncate_inode_pages()) to discard these buffers can
191 * cause data loss. Also even if we did not discard these
192 * buffers, we would have no way to find them after the inode
193 * is reaped and thus user could see stale data if he tries to
194 * read them before the transaction is checkpointed. So be
195 * careful and force everything to disk here... We use
196 * ei->i_datasync_tid to store the newest transaction
197 * containing inode's data.
198 *
199 * Note that directories do not have this problem because they
200 * don't use page cache.
201 */
Vegard Nossum6a7fd522016-07-04 11:03:00 -0400202 if (inode->i_ino != EXT4_JOURNAL_INO &&
203 ext4_should_journal_data(inode) &&
Jan Kara3abb1a02017-06-22 23:49:46 -0400204 (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode)) &&
205 inode->i_data.nrpages) {
Jan Kara2d859db2011-07-26 09:07:11 -0400206 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
207 tid_t commit_tid = EXT4_I(inode)->i_datasync_tid;
208
Theodore Ts'od76a3a772013-04-03 22:02:52 -0400209 jbd2_complete_transaction(journal, commit_tid);
Jan Kara2d859db2011-07-26 09:07:11 -0400210 filemap_write_and_wait(&inode->i_data);
211 }
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700212 truncate_inode_pages_final(&inode->i_data);
Jan Kara5dc23bd2013-06-04 14:46:12 -0400213
Al Viro0930fcc2010-06-07 13:16:22 -0400214 goto no_delete;
215 }
216
Theodore Ts'oe2bfb082014-10-05 22:47:07 -0400217 if (is_bad_inode(inode))
218 goto no_delete;
219 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500220
Jan Kara678aaf42008-07-11 19:27:31 -0400221 if (ext4_should_order_data(inode))
222 ext4_begin_ordered_truncate(inode, 0);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700223 truncate_inode_pages_final(&inode->i_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700224
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200225 /*
226 * Protect us against freezing - iput() caller didn't have to have any
227 * protection against it
228 */
229 sb_start_intwrite(inode->i_sb);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400230
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400231 if (!IS_NOQUOTA(inode))
232 extra_credits += EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb);
233
Jan Kara65db8692019-11-05 17:44:12 +0100234 /*
235 * Block bitmap, group descriptor, and inode are accounted in both
236 * ext4_blocks_for_truncate() and extra_credits. So subtract 3.
237 */
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400238 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
Jan Kara65db8692019-11-05 17:44:12 +0100239 ext4_blocks_for_truncate(inode) + extra_credits - 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700240 if (IS_ERR(handle)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400241 ext4_std_error(inode->i_sb, PTR_ERR(handle));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700242 /*
243 * If we're going to skip the normal cleanup, we still need to
244 * make sure that the in-core orphan linked list is properly
245 * cleaned up.
246 */
Mingming Cao617ba132006-10-11 01:20:53 -0700247 ext4_orphan_del(NULL, inode);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200248 sb_end_intwrite(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700249 goto no_delete;
250 }
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400251
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700252 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500253 ext4_handle_sync(handle);
Tahsin Erdogan407cd7f2017-07-04 00:11:21 -0400254
255 /*
256 * Set inode->i_size to 0 before calling ext4_truncate(). We need
257 * special handling of symlinks here because i_size is used to
258 * determine whether ext4_inode_info->i_data contains symlink data or
259 * block mappings. Setting i_size to 0 will remove its fast symlink
260 * status. Erase i_data so that it becomes a valid empty block map.
261 */
262 if (ext4_inode_is_fast_symlink(inode))
263 memset(EXT4_I(inode)->i_data, 0, sizeof(EXT4_I(inode)->i_data));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700264 inode->i_size = 0;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400265 err = ext4_mark_inode_dirty(handle, inode);
266 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500267 ext4_warning(inode->i_sb,
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400268 "couldn't mark inode dirty (err %d)", err);
269 goto stop_handle;
270 }
Theodore Ts'o2c98eb52016-11-13 22:02:26 -0500271 if (inode->i_blocks) {
272 err = ext4_truncate(inode);
273 if (err) {
Theodore Ts'o878520a2019-11-19 21:54:15 -0500274 ext4_set_errno(inode->i_sb, -err);
Theodore Ts'o2c98eb52016-11-13 22:02:26 -0500275 ext4_error(inode->i_sb,
276 "couldn't truncate inode %lu (err %d)",
277 inode->i_ino, err);
278 goto stop_handle;
279 }
280 }
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400281
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400282 /* Remove xattr references. */
283 err = ext4_xattr_delete_inode(handle, inode, &ea_inode_array,
284 extra_credits);
285 if (err) {
286 ext4_warning(inode->i_sb, "xattr delete (err %d)", err);
287stop_handle:
288 ext4_journal_stop(handle);
289 ext4_orphan_del(NULL, inode);
290 sb_end_intwrite(inode->i_sb);
291 ext4_xattr_inode_array_free(ea_inode_array);
292 goto no_delete;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400293 }
294
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700295 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700296 * Kill off the orphan record which ext4_truncate created.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700297 * AKPM: I think this can be inside the above `if'.
Mingming Cao617ba132006-10-11 01:20:53 -0700298 * Note that ext4_orphan_del() has to be able to cope with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700299 * deletion of a non-existent orphan - this is because we don't
Mingming Cao617ba132006-10-11 01:20:53 -0700300 * know if ext4_truncate() actually created an orphan record.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700301 * (Well, we could do this if we need to, but heck - it works)
302 */
Mingming Cao617ba132006-10-11 01:20:53 -0700303 ext4_orphan_del(handle, inode);
Arnd Bergmann5ffff832018-07-29 15:50:00 -0400304 EXT4_I(inode)->i_dtime = (__u32)ktime_get_real_seconds();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700305
306 /*
307 * One subtle ordering requirement: if anything has gone wrong
308 * (transaction abort, IO errors, whatever), then we can still
309 * do these next steps (the fs will already have been marked as
310 * having errors), but we can't free the inode if the mark_dirty
311 * fails.
312 */
Mingming Cao617ba132006-10-11 01:20:53 -0700313 if (ext4_mark_inode_dirty(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700314 /* If that failed, just do the required in-core inode clear. */
Al Viro0930fcc2010-06-07 13:16:22 -0400315 ext4_clear_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700316 else
Mingming Cao617ba132006-10-11 01:20:53 -0700317 ext4_free_inode(handle, inode);
318 ext4_journal_stop(handle);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200319 sb_end_intwrite(inode->i_sb);
Tahsin Erdogan0421a182017-06-22 10:26:31 -0400320 ext4_xattr_inode_array_free(ea_inode_array);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700321 return;
322no_delete:
Al Viro0930fcc2010-06-07 13:16:22 -0400323 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700324}
325
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300326#ifdef CONFIG_QUOTA
327qsize_t *ext4_get_reserved_space(struct inode *inode)
Mingming Cao60e58e02009-01-22 18:13:05 +0100328{
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300329 return &EXT4_I(inode)->i_reserved_quota;
Mingming Cao60e58e02009-01-22 18:13:05 +0100330}
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300331#endif
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500332
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400333/*
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500334 * Called with i_data_sem down, which is important since we can call
335 * ext4_discard_preallocations() from here.
336 */
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500337void ext4_da_update_reserve_space(struct inode *inode,
338 int used, int quota_claim)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400339{
340 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500341 struct ext4_inode_info *ei = EXT4_I(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400342
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500343 spin_lock(&ei->i_block_reservation_lock);
Aditya Kalid8990242011-09-09 19:18:51 -0400344 trace_ext4_da_update_reserve_space(inode, used, quota_claim);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500345 if (unlikely(used > ei->i_reserved_data_blocks)) {
Theodore Ts'o8de5c322013-02-14 15:11:41 -0500346 ext4_warning(inode->i_sb, "%s: ino %lu, used %d "
Theodore Ts'o1084f252012-03-19 23:13:43 -0400347 "with only %d reserved data blocks",
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500348 __func__, inode->i_ino, used,
349 ei->i_reserved_data_blocks);
350 WARN_ON(1);
351 used = ei->i_reserved_data_blocks;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400352 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400353
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500354 /* Update per-inode reservations */
355 ei->i_reserved_data_blocks -= used;
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -0400356 percpu_counter_sub(&sbi->s_dirtyclusters_counter, used);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500357
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400358 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +0100359
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400360 /* Update quota subsystem for data blocks */
361 if (quota_claim)
Aditya Kali7b415bf2011-09-09 19:04:51 -0400362 dquot_claim_block(inode, EXT4_C2B(sbi, used));
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400363 else {
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500364 /*
365 * We did fallocate with an offset that is already delayed
366 * allocated. So on delayed allocated writeback we should
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400367 * not re-claim the quota for fallocated blocks.
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500368 */
Aditya Kali7b415bf2011-09-09 19:04:51 -0400369 dquot_release_reservation_block(inode, EXT4_C2B(sbi, used));
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500370 }
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -0400371
372 /*
373 * If we have done all the pending block allocations and if
374 * there aren't any writers on the inode, we can discard the
375 * inode's preallocations.
376 */
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500377 if ((ei->i_reserved_data_blocks == 0) &&
Nikolay Borisov82dd1242019-02-10 23:04:16 -0500378 !inode_is_open_for_write(inode))
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -0400379 ext4_discard_preallocations(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400380}
381
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400382static int __check_block_validity(struct inode *inode, const char *func,
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400383 unsigned int line,
384 struct ext4_map_blocks *map)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400385{
Theodore Ts'o345c0db2019-04-09 23:37:08 -0400386 if (ext4_has_feature_journal(inode->i_sb) &&
387 (inode->i_ino ==
388 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum)))
389 return 0;
Theodore Ts'o24676da2010-05-16 21:00:00 -0400390 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
391 map->m_len)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400392 ext4_error_inode(inode, func, line, map->m_pblk,
Theodore Ts'obdbd6ce2018-06-15 12:27:16 -0400393 "lblock %lu mapped to illegal pblock %llu "
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400394 "(length %d)", (unsigned long) map->m_lblk,
Theodore Ts'obdbd6ce2018-06-15 12:27:16 -0400395 map->m_pblk, map->m_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400396 return -EFSCORRUPTED;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400397 }
398 return 0;
399}
400
Jan Kara53085fa2015-12-07 15:09:35 -0500401int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk,
402 ext4_lblk_t len)
403{
404 int ret;
405
Eric Biggers33b4cc22019-12-26 10:10:22 -0600406 if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400407 return fscrypt_zeroout_range(inode, lblk, pblk, len);
Jan Kara53085fa2015-12-07 15:09:35 -0500408
409 ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS);
410 if (ret > 0)
411 ret = 0;
412
413 return ret;
414}
415
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400416#define check_block_validity(inode, map) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400417 __check_block_validity((inode), __func__, __LINE__, (map))
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400418
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400419#ifdef ES_AGGRESSIVE_TEST
420static void ext4_map_blocks_es_recheck(handle_t *handle,
421 struct inode *inode,
422 struct ext4_map_blocks *es_map,
423 struct ext4_map_blocks *map,
424 int flags)
425{
426 int retval;
427
428 map->m_flags = 0;
429 /*
430 * There is a race window that the result is not the same.
431 * e.g. xfstests #223 when dioread_nolock enables. The reason
432 * is that we lookup a block mapping in extent status tree with
433 * out taking i_data_sem. So at the time the unwritten extent
434 * could be converted.
435 */
Jan Kara2dcba472015-12-07 15:04:57 -0500436 down_read(&EXT4_I(inode)->i_data_sem);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400437 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
438 retval = ext4_ext_map_blocks(handle, inode, map, flags &
439 EXT4_GET_BLOCKS_KEEP_SIZE);
440 } else {
441 retval = ext4_ind_map_blocks(handle, inode, map, flags &
442 EXT4_GET_BLOCKS_KEEP_SIZE);
443 }
Jan Kara2dcba472015-12-07 15:04:57 -0500444 up_read((&EXT4_I(inode)->i_data_sem));
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400445
446 /*
447 * We don't check m_len because extent will be collpased in status
448 * tree. So the m_len might not equal.
449 */
450 if (es_map->m_lblk != map->m_lblk ||
451 es_map->m_flags != map->m_flags ||
452 es_map->m_pblk != map->m_pblk) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400453 printk("ES cache assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400454 "es_cached ex [%d/%d/%llu/%x] != "
455 "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
456 inode->i_ino, es_map->m_lblk, es_map->m_len,
457 es_map->m_pblk, es_map->m_flags, map->m_lblk,
458 map->m_len, map->m_pblk, map->m_flags,
459 retval, flags);
460 }
461}
462#endif /* ES_AGGRESSIVE_TEST */
463
Theodore Ts'o55138e0b2009-09-29 13:31:31 -0400464/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400465 * The ext4_map_blocks() function tries to look up the requested blocks,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400466 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500467 *
Mingming Caof5ab0d12008-02-25 15:29:55 -0500468 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
469 * and store the allocated blocks in the result buffer head and mark it
470 * mapped.
471 *
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400472 * If file type is extents based, it will call ext4_ext_map_blocks(),
473 * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
Mingming Caof5ab0d12008-02-25 15:29:55 -0500474 * based files
475 *
Jan Karafacab4d2016-03-09 22:54:00 -0500476 * On success, it returns the number of blocks being mapped or allocated. if
477 * create==0 and the blocks are pre-allocated and unwritten, the resulting @map
478 * is marked as unwritten. If the create == 1, it will mark @map as mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500479 *
480 * It returns 0 if plain look up failed (blocks have not been allocated), in
Jan Karafacab4d2016-03-09 22:54:00 -0500481 * that case, @map is returned as unmapped but we still do fill map->m_len to
482 * indicate the length of a hole starting at map->m_lblk.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500483 *
484 * It returns the error in case of allocation failure.
485 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400486int ext4_map_blocks(handle_t *handle, struct inode *inode,
487 struct ext4_map_blocks *map, int flags)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500488{
Zheng Liud100eef2013-02-18 00:29:59 -0500489 struct extent_status es;
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500490 int retval;
Lukas Czernerb8a86842014-03-18 18:05:35 -0400491 int ret = 0;
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400492#ifdef ES_AGGRESSIVE_TEST
493 struct ext4_map_blocks orig_map;
494
495 memcpy(&orig_map, map, sizeof(*map));
496#endif
Mingming Caof5ab0d12008-02-25 15:29:55 -0500497
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400498 map->m_flags = 0;
499 ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
500 "logical block %lu\n", inode->i_ino, flags, map->m_len,
501 (unsigned long) map->m_lblk);
Zheng Liud100eef2013-02-18 00:29:59 -0500502
Theodore Ts'oe861b5e2014-02-20 12:54:05 -0500503 /*
504 * ext4_map_blocks returns an int, and m_len is an unsigned int
505 */
506 if (unlikely(map->m_len > INT_MAX))
507 map->m_len = INT_MAX;
508
Kazuya Mio4adb6ab2014-04-07 10:53:28 -0400509 /* We can handle the block number less than EXT_MAX_BLOCKS */
510 if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400511 return -EFSCORRUPTED;
Kazuya Mio4adb6ab2014-04-07 10:53:28 -0400512
Zheng Liud100eef2013-02-18 00:29:59 -0500513 /* Lookup extent status tree firstly */
Theodore Ts'obb5835e2019-08-11 16:32:41 -0400514 if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) {
Zheng Liud100eef2013-02-18 00:29:59 -0500515 if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
516 map->m_pblk = ext4_es_pblock(&es) +
517 map->m_lblk - es.es_lblk;
518 map->m_flags |= ext4_es_is_written(&es) ?
519 EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN;
520 retval = es.es_len - (map->m_lblk - es.es_lblk);
521 if (retval > map->m_len)
522 retval = map->m_len;
523 map->m_len = retval;
524 } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) {
Jan Karafacab4d2016-03-09 22:54:00 -0500525 map->m_pblk = 0;
526 retval = es.es_len - (map->m_lblk - es.es_lblk);
527 if (retval > map->m_len)
528 retval = map->m_len;
529 map->m_len = retval;
Zheng Liud100eef2013-02-18 00:29:59 -0500530 retval = 0;
531 } else {
Arnd Bergmann1e83bc82019-04-07 12:24:43 -0400532 BUG();
Zheng Liud100eef2013-02-18 00:29:59 -0500533 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400534#ifdef ES_AGGRESSIVE_TEST
535 ext4_map_blocks_es_recheck(handle, inode, map,
536 &orig_map, flags);
537#endif
Zheng Liud100eef2013-02-18 00:29:59 -0500538 goto found;
539 }
540
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500541 /*
Theodore Ts'ob920c752009-05-14 00:54:29 -0400542 * Try to see if we can get the block without requesting a new
543 * file system block.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500544 */
Jan Kara2dcba472015-12-07 15:04:57 -0500545 down_read(&EXT4_I(inode)->i_data_sem);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400546 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Dmitry Monakhova4e5d882011-10-25 08:15:12 -0400547 retval = ext4_ext_map_blocks(handle, inode, map, flags &
548 EXT4_GET_BLOCKS_KEEP_SIZE);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500549 } else {
Dmitry Monakhova4e5d882011-10-25 08:15:12 -0400550 retval = ext4_ind_map_blocks(handle, inode, map, flags &
551 EXT4_GET_BLOCKS_KEEP_SIZE);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500552 }
Zheng Liuf7fec032013-02-18 00:28:47 -0500553 if (retval > 0) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400554 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -0500555
Zheng Liu44fb851d2013-07-29 12:51:42 -0400556 if (unlikely(retval != map->m_len)) {
557 ext4_warning(inode->i_sb,
558 "ES len assertion failed for inode "
559 "%lu: retval %d != map->m_len %d",
560 inode->i_ino, retval, map->m_len);
561 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400562 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400563
Zheng Liuf7fec032013-02-18 00:28:47 -0500564 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
565 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
566 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400567 !(status & EXTENT_STATUS_WRITTEN) &&
Eric Whitneyad431022018-10-01 14:10:39 -0400568 ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk,
569 map->m_lblk + map->m_len - 1))
Zheng Liuf7fec032013-02-18 00:28:47 -0500570 status |= EXTENT_STATUS_DELAYED;
571 ret = ext4_es_insert_extent(inode, map->m_lblk,
572 map->m_len, map->m_pblk, status);
573 if (ret < 0)
574 retval = ret;
575 }
Jan Kara2dcba472015-12-07 15:04:57 -0500576 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -0500577
Zheng Liud100eef2013-02-18 00:29:59 -0500578found:
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400579 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Lukas Czernerb8a86842014-03-18 18:05:35 -0400580 ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400581 if (ret != 0)
582 return ret;
583 }
584
Mingming Caof5ab0d12008-02-25 15:29:55 -0500585 /* If it is only a block(s) look up */
Theodore Ts'oc2177052009-05-14 00:58:52 -0400586 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500587 return retval;
588
589 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -0500590 * Returns if the blocks have already allocated
591 *
592 * Note that if blocks have been preallocated
Tao Madf3ab172011-10-08 15:53:49 -0400593 * ext4_ext_get_block() returns the create = 0
Mingming Caof5ab0d12008-02-25 15:29:55 -0500594 * with buffer head unmapped.
595 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400596 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
Lukas Czernerb8a86842014-03-18 18:05:35 -0400597 /*
598 * If we need to convert extent to unwritten
599 * we continue and do the actual work in
600 * ext4_ext_map_blocks()
601 */
602 if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN))
603 return retval;
Mingming Caof5ab0d12008-02-25 15:29:55 -0500604
605 /*
Zheng Liua25a4e12013-02-18 00:28:04 -0500606 * Here we clear m_flags because after allocating an new extent,
607 * it will be set again.
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -0400608 */
Zheng Liua25a4e12013-02-18 00:28:04 -0500609 map->m_flags &= ~EXT4_MAP_FLAGS;
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -0400610
611 /*
Lukas Czerner556615d2014-04-20 23:45:47 -0400612 * New blocks allocate and/or writing to unwritten extent
Mingming Caof5ab0d12008-02-25 15:29:55 -0500613 * will possibly result in updating i_data, so we take
Seunghun Leed91bd2c2014-09-01 22:15:30 -0400614 * the write lock of i_data_sem, and call get_block()
Mingming Caof5ab0d12008-02-25 15:29:55 -0500615 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500616 */
Lukas Czernerc8b459f2014-05-12 12:55:07 -0400617 down_write(&EXT4_I(inode)->i_data_sem);
Mingming Caod2a17632008-07-14 17:52:37 -0400618
619 /*
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500620 * We need to check for EXT4 here because migrate
621 * could have changed the inode type in between
622 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400623 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400624 retval = ext4_ext_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500625 } else {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400626 retval = ext4_ind_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400627
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400628 if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400629 /*
630 * We allocated new blocks which will result in
631 * i_data's format changing. Force the migrate
632 * to fail by clearing migrate flags
633 */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500634 ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400635 }
Mingming Caod2a17632008-07-14 17:52:37 -0400636
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500637 /*
638 * Update reserved blocks/metadata blocks after successful
639 * block allocation which had been deferred till now. We don't
640 * support fallocate for non extent files. So we can update
641 * reserve space here.
642 */
643 if ((retval > 0) &&
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -0500644 (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500645 ext4_da_update_reserve_space(inode, retval, 1);
646 }
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -0400647
Zheng Liuf7fec032013-02-18 00:28:47 -0500648 if (retval > 0) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400649 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -0500650
Zheng Liu44fb851d2013-07-29 12:51:42 -0400651 if (unlikely(retval != map->m_len)) {
652 ext4_warning(inode->i_sb,
653 "ES len assertion failed for inode "
654 "%lu: retval %d != map->m_len %d",
655 inode->i_ino, retval, map->m_len);
656 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400657 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400658
Zheng Liuadb23552013-03-10 21:13:05 -0400659 /*
Jan Karac86d8db2015-12-07 15:10:26 -0500660 * We have to zeroout blocks before inserting them into extent
661 * status tree. Otherwise someone could look them up there and
Jan Kara9b623df2016-09-30 02:02:29 -0400662 * use them before they are really zeroed. We also have to
663 * unmap metadata before zeroing as otherwise writeback can
664 * overwrite zeros with stale data from block device.
Jan Karac86d8db2015-12-07 15:10:26 -0500665 */
666 if (flags & EXT4_GET_BLOCKS_ZERO &&
667 map->m_flags & EXT4_MAP_MAPPED &&
668 map->m_flags & EXT4_MAP_NEW) {
669 ret = ext4_issue_zeroout(inode, map->m_lblk,
670 map->m_pblk, map->m_len);
671 if (ret) {
672 retval = ret;
673 goto out_sem;
674 }
675 }
676
677 /*
Zheng Liuadb23552013-03-10 21:13:05 -0400678 * If the extent has been zeroed out, we don't need to update
679 * extent status tree.
680 */
681 if ((flags & EXT4_GET_BLOCKS_PRE_IO) &&
Theodore Ts'obb5835e2019-08-11 16:32:41 -0400682 ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) {
Zheng Liuadb23552013-03-10 21:13:05 -0400683 if (ext4_es_is_written(&es))
Jan Karac86d8db2015-12-07 15:10:26 -0500684 goto out_sem;
Zheng Liuadb23552013-03-10 21:13:05 -0400685 }
Zheng Liuf7fec032013-02-18 00:28:47 -0500686 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
687 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
688 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400689 !(status & EXTENT_STATUS_WRITTEN) &&
Eric Whitneyad431022018-10-01 14:10:39 -0400690 ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk,
691 map->m_lblk + map->m_len - 1))
Zheng Liuf7fec032013-02-18 00:28:47 -0500692 status |= EXTENT_STATUS_DELAYED;
693 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
694 map->m_pblk, status);
Jan Karac86d8db2015-12-07 15:10:26 -0500695 if (ret < 0) {
Zheng Liuf7fec032013-02-18 00:28:47 -0500696 retval = ret;
Jan Karac86d8db2015-12-07 15:10:26 -0500697 goto out_sem;
698 }
Aditya Kali5356f2612011-09-09 19:20:51 -0400699 }
700
Jan Karac86d8db2015-12-07 15:10:26 -0500701out_sem:
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500702 up_write((&EXT4_I(inode)->i_data_sem));
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400703 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Lukas Czernerb8a86842014-03-18 18:05:35 -0400704 ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400705 if (ret != 0)
706 return ret;
Jan Kara06bd3c32016-04-24 00:56:03 -0400707
708 /*
709 * Inodes with freshly allocated blocks where contents will be
710 * visible after transaction commit must be on transaction's
711 * ordered data list.
712 */
713 if (map->m_flags & EXT4_MAP_NEW &&
714 !(map->m_flags & EXT4_MAP_UNWRITTEN) &&
715 !(flags & EXT4_GET_BLOCKS_ZERO) &&
Tahsin Erdogan02749a42017-06-22 11:31:25 -0400716 !ext4_is_quota_file(inode) &&
Jan Kara06bd3c32016-04-24 00:56:03 -0400717 ext4_should_order_data(inode)) {
Ross Zwisler73131fb2019-06-20 17:26:26 -0400718 loff_t start_byte =
719 (loff_t)map->m_lblk << inode->i_blkbits;
720 loff_t length = (loff_t)map->m_len << inode->i_blkbits;
721
Jan Karaee0876b2016-04-24 00:56:08 -0400722 if (flags & EXT4_GET_BLOCKS_IO_SUBMIT)
Ross Zwisler73131fb2019-06-20 17:26:26 -0400723 ret = ext4_jbd2_inode_add_wait(handle, inode,
724 start_byte, length);
Jan Karaee0876b2016-04-24 00:56:08 -0400725 else
Ross Zwisler73131fb2019-06-20 17:26:26 -0400726 ret = ext4_jbd2_inode_add_write(handle, inode,
727 start_byte, length);
Jan Kara06bd3c32016-04-24 00:56:03 -0400728 if (ret)
729 return ret;
730 }
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400731 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500732 return retval;
733}
734
Jan Karaed8ad832016-02-19 00:18:25 -0500735/*
736 * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages
737 * we have to be careful as someone else may be manipulating b_state as well.
738 */
739static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags)
740{
741 unsigned long old_state;
742 unsigned long new_state;
743
744 flags &= EXT4_MAP_FLAGS;
745
746 /* Dummy buffer_head? Set non-atomically. */
747 if (!bh->b_page) {
748 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags;
749 return;
750 }
751 /*
752 * Someone else may be modifying b_state. Be careful! This is ugly but
753 * once we get rid of using bh as a container for mapping information
754 * to pass to / from get_block functions, this can go away.
755 */
756 do {
757 old_state = READ_ONCE(bh->b_state);
758 new_state = (old_state & ~EXT4_MAP_FLAGS) | flags;
759 } while (unlikely(
760 cmpxchg(&bh->b_state, old_state, new_state) != old_state));
761}
762
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400763static int _ext4_get_block(struct inode *inode, sector_t iblock,
764 struct buffer_head *bh, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700765{
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400766 struct ext4_map_blocks map;
Jan Karaefe70c22016-03-08 23:35:46 -0500767 int ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700768
Tao Ma46c7f252012-12-10 14:04:52 -0500769 if (ext4_has_inline_data(inode))
770 return -ERANGE;
771
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400772 map.m_lblk = iblock;
773 map.m_len = bh->b_size >> inode->i_blkbits;
774
Jan Karaefe70c22016-03-08 23:35:46 -0500775 ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map,
776 flags);
Jan Kara7fb54092008-02-10 01:08:38 -0500777 if (ret > 0) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400778 map_bh(bh, inode->i_sb, map.m_pblk);
Jan Karaed8ad832016-02-19 00:18:25 -0500779 ext4_update_bh_state(bh, map.m_flags);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400780 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
Jan Kara7fb54092008-02-10 01:08:38 -0500781 ret = 0;
Ross Zwisler547edce2016-11-08 11:30:58 +1100782 } else if (ret == 0) {
783 /* hole case, need to fill in bh->b_size */
784 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700785 }
786 return ret;
787}
788
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400789int ext4_get_block(struct inode *inode, sector_t iblock,
790 struct buffer_head *bh, int create)
791{
792 return _ext4_get_block(inode, iblock, bh,
793 create ? EXT4_GET_BLOCKS_CREATE : 0);
794}
795
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700796/*
Jan Kara705965b2016-03-08 23:08:10 -0500797 * Get block function used when preparing for buffered write if we require
798 * creating an unwritten extent if blocks haven't been allocated. The extent
799 * will be converted to written after the IO is complete.
800 */
801int ext4_get_block_unwritten(struct inode *inode, sector_t iblock,
802 struct buffer_head *bh_result, int create)
803{
804 ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n",
805 inode->i_ino, create);
806 return _ext4_get_block(inode, iblock, bh_result,
807 EXT4_GET_BLOCKS_IO_CREATE_EXT);
808}
809
Jan Karaefe70c22016-03-08 23:35:46 -0500810/* Maximum number of blocks we map for direct IO at once. */
811#define DIO_MAX_BLOCKS 4096
812
Jan Karae84dfbe2016-04-01 02:07:22 -0400813/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700814 * `handle' can be NULL if create is zero
815 */
Mingming Cao617ba132006-10-11 01:20:53 -0700816struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400817 ext4_lblk_t block, int map_flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700818{
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400819 struct ext4_map_blocks map;
820 struct buffer_head *bh;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400821 int create = map_flags & EXT4_GET_BLOCKS_CREATE;
Theodore Ts'o10560082014-08-29 20:51:32 -0400822 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700823
824 J_ASSERT(handle != NULL || create == 0);
825
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400826 map.m_lblk = block;
827 map.m_len = 1;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400828 err = ext4_map_blocks(handle, inode, &map, map_flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700829
Theodore Ts'o10560082014-08-29 20:51:32 -0400830 if (err == 0)
831 return create ? ERR_PTR(-ENOSPC) : NULL;
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400832 if (err < 0)
Theodore Ts'o10560082014-08-29 20:51:32 -0400833 return ERR_PTR(err);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400834
835 bh = sb_getblk(inode->i_sb, map.m_pblk);
Theodore Ts'o10560082014-08-29 20:51:32 -0400836 if (unlikely(!bh))
837 return ERR_PTR(-ENOMEM);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400838 if (map.m_flags & EXT4_MAP_NEW) {
839 J_ASSERT(create != 0);
840 J_ASSERT(handle != NULL);
841
842 /*
843 * Now that we do not always journal data, we should
844 * keep in mind whether this should always journal the
845 * new buffer as metadata. For now, regular file
846 * writes use ext4_get_block instead, so it's not a
847 * problem.
848 */
849 lock_buffer(bh);
850 BUFFER_TRACE(bh, "call get_create_access");
Theodore Ts'o10560082014-08-29 20:51:32 -0400851 err = ext4_journal_get_create_access(handle, bh);
852 if (unlikely(err)) {
853 unlock_buffer(bh);
854 goto errout;
855 }
856 if (!buffer_uptodate(bh)) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400857 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
858 set_buffer_uptodate(bh);
859 }
860 unlock_buffer(bh);
861 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
862 err = ext4_handle_dirty_metadata(handle, inode, bh);
Theodore Ts'o10560082014-08-29 20:51:32 -0400863 if (unlikely(err))
864 goto errout;
865 } else
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400866 BUFFER_TRACE(bh, "not a new buffer");
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400867 return bh;
Theodore Ts'o10560082014-08-29 20:51:32 -0400868errout:
869 brelse(bh);
870 return ERR_PTR(err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700871}
872
Mingming Cao617ba132006-10-11 01:20:53 -0700873struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400874 ext4_lblk_t block, int map_flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700875{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400876 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700877
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400878 bh = ext4_getblk(handle, inode, block, map_flags);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400879 if (IS_ERR(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700880 return bh;
ZhangXiaoxu7963e5a2019-08-22 23:00:32 -0400881 if (!bh || ext4_buffer_uptodate(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700882 return bh;
Mike Christiedfec8a12016-06-05 14:31:44 -0500883 ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700884 wait_on_buffer(bh);
885 if (buffer_uptodate(bh))
886 return bh;
887 put_bh(bh);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400888 return ERR_PTR(-EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700889}
890
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400891/* Read a contiguous batch of blocks. */
892int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count,
893 bool wait, struct buffer_head **bhs)
894{
895 int i, err;
896
897 for (i = 0; i < bh_count; i++) {
898 bhs[i] = ext4_getblk(NULL, inode, block + i, 0 /* map_flags */);
899 if (IS_ERR(bhs[i])) {
900 err = PTR_ERR(bhs[i]);
901 bh_count = i;
902 goto out_brelse;
903 }
904 }
905
906 for (i = 0; i < bh_count; i++)
907 /* Note that NULL bhs[i] is valid because of holes. */
ZhangXiaoxu7963e5a2019-08-22 23:00:32 -0400908 if (bhs[i] && !ext4_buffer_uptodate(bhs[i]))
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400909 ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1,
910 &bhs[i]);
911
912 if (!wait)
913 return 0;
914
915 for (i = 0; i < bh_count; i++)
916 if (bhs[i])
917 wait_on_buffer(bhs[i]);
918
919 for (i = 0; i < bh_count; i++) {
920 if (bhs[i] && !buffer_uptodate(bhs[i])) {
921 err = -EIO;
922 goto out_brelse;
923 }
924 }
925 return 0;
926
927out_brelse:
928 for (i = 0; i < bh_count; i++) {
929 brelse(bhs[i]);
930 bhs[i] = NULL;
931 }
932 return err;
933}
934
Tao Maf19d5872012-12-10 14:05:51 -0500935int ext4_walk_page_buffers(handle_t *handle,
936 struct buffer_head *head,
937 unsigned from,
938 unsigned to,
939 int *partial,
940 int (*fn)(handle_t *handle,
941 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700942{
943 struct buffer_head *bh;
944 unsigned block_start, block_end;
945 unsigned blocksize = head->b_size;
946 int err, ret = 0;
947 struct buffer_head *next;
948
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400949 for (bh = head, block_start = 0;
950 ret == 0 && (bh != head || !block_start);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400951 block_start = block_end, bh = next) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700952 next = bh->b_this_page;
953 block_end = block_start + blocksize;
954 if (block_end <= from || block_start >= to) {
955 if (partial && !buffer_uptodate(bh))
956 *partial = 1;
957 continue;
958 }
959 err = (*fn)(handle, bh);
960 if (!ret)
961 ret = err;
962 }
963 return ret;
964}
965
966/*
967 * To preserve ordering, it is essential that the hole instantiation and
968 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -0700969 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -0700970 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700971 * prepare_write() is the right place.
972 *
Jan Kara36ade452013-01-28 09:30:52 -0500973 * Also, this function can nest inside ext4_writepage(). In that case, we
974 * *know* that ext4_writepage() has generated enough buffer credits to do the
975 * whole page. So we won't block on the journal in that case, which is good,
976 * because the caller may be PF_MEMALLOC.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700977 *
Mingming Cao617ba132006-10-11 01:20:53 -0700978 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700979 * quota file writes. If we were to commit the transaction while thus
980 * reentered, there can be a deadlock - we would be holding a quota
981 * lock, and the commit would never complete if another thread had a
982 * transaction open and was blocking on the quota lock - a ranking
983 * violation.
984 *
Mingming Caodab291a2006-10-11 01:21:01 -0700985 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700986 * will _not_ run commit under these circumstances because handle->h_ref
987 * is elevated. We'll still have enough credits for the tiny quotafile
988 * write.
989 */
Tao Maf19d5872012-12-10 14:05:51 -0500990int do_journal_get_write_access(handle_t *handle,
991 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700992{
Jan Kara56d35a42010-08-05 14:41:42 -0400993 int dirty = buffer_dirty(bh);
994 int ret;
995
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700996 if (!buffer_mapped(bh) || buffer_freed(bh))
997 return 0;
Jan Kara56d35a42010-08-05 14:41:42 -0400998 /*
Christoph Hellwigebdec242010-10-06 10:47:23 +0200999 * __block_write_begin() could have dirtied some buffers. Clean
Jan Kara56d35a42010-08-05 14:41:42 -04001000 * the dirty bit as jbd2_journal_get_write_access() could complain
1001 * otherwise about fs integrity issues. Setting of the dirty bit
Christoph Hellwigebdec242010-10-06 10:47:23 +02001002 * by __block_write_begin() isn't a real problem here as we clear
Jan Kara56d35a42010-08-05 14:41:42 -04001003 * the bit before releasing a page lock and thus writeback cannot
1004 * ever write the buffer.
1005 */
1006 if (dirty)
1007 clear_buffer_dirty(bh);
liang xie5d601252014-05-12 22:06:43 -04001008 BUFFER_TRACE(bh, "get write access");
Jan Kara56d35a42010-08-05 14:41:42 -04001009 ret = ext4_journal_get_write_access(handle, bh);
1010 if (!ret && dirty)
1011 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
1012 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001013}
1014
Chandan Rajendra643fa962018-12-12 15:20:12 +05301015#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrow2058f832015-04-12 00:55:10 -04001016static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len,
1017 get_block_t *get_block)
1018{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001019 unsigned from = pos & (PAGE_SIZE - 1);
Michael Halcrow2058f832015-04-12 00:55:10 -04001020 unsigned to = from + len;
1021 struct inode *inode = page->mapping->host;
1022 unsigned block_start, block_end;
1023 sector_t block;
1024 int err = 0;
1025 unsigned blocksize = inode->i_sb->s_blocksize;
1026 unsigned bbits;
Chandan Rajendra0b578f32019-05-20 09:29:50 -07001027 struct buffer_head *bh, *head, *wait[2];
1028 int nr_wait = 0;
1029 int i;
Michael Halcrow2058f832015-04-12 00:55:10 -04001030
1031 BUG_ON(!PageLocked(page));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001032 BUG_ON(from > PAGE_SIZE);
1033 BUG_ON(to > PAGE_SIZE);
Michael Halcrow2058f832015-04-12 00:55:10 -04001034 BUG_ON(from > to);
1035
1036 if (!page_has_buffers(page))
1037 create_empty_buffers(page, blocksize, 0);
1038 head = page_buffers(page);
1039 bbits = ilog2(blocksize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001040 block = (sector_t)page->index << (PAGE_SHIFT - bbits);
Michael Halcrow2058f832015-04-12 00:55:10 -04001041
1042 for (bh = head, block_start = 0; bh != head || !block_start;
1043 block++, block_start = block_end, bh = bh->b_this_page) {
1044 block_end = block_start + blocksize;
1045 if (block_end <= from || block_start >= to) {
1046 if (PageUptodate(page)) {
1047 if (!buffer_uptodate(bh))
1048 set_buffer_uptodate(bh);
1049 }
1050 continue;
1051 }
1052 if (buffer_new(bh))
1053 clear_buffer_new(bh);
1054 if (!buffer_mapped(bh)) {
1055 WARN_ON(bh->b_size != blocksize);
1056 err = get_block(inode, block, bh, 1);
1057 if (err)
1058 break;
1059 if (buffer_new(bh)) {
Michael Halcrow2058f832015-04-12 00:55:10 -04001060 if (PageUptodate(page)) {
1061 clear_buffer_new(bh);
1062 set_buffer_uptodate(bh);
1063 mark_buffer_dirty(bh);
1064 continue;
1065 }
1066 if (block_end > to || block_start < from)
1067 zero_user_segments(page, to, block_end,
1068 block_start, from);
1069 continue;
1070 }
1071 }
1072 if (PageUptodate(page)) {
1073 if (!buffer_uptodate(bh))
1074 set_buffer_uptodate(bh);
1075 continue;
1076 }
1077 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
1078 !buffer_unwritten(bh) &&
1079 (block_start < from || block_end > to)) {
Mike Christiedfec8a12016-06-05 14:31:44 -05001080 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
Chandan Rajendra0b578f32019-05-20 09:29:50 -07001081 wait[nr_wait++] = bh;
Michael Halcrow2058f832015-04-12 00:55:10 -04001082 }
1083 }
1084 /*
1085 * If we issued read requests, let them complete.
1086 */
Chandan Rajendra0b578f32019-05-20 09:29:50 -07001087 for (i = 0; i < nr_wait; i++) {
1088 wait_on_buffer(wait[i]);
1089 if (!buffer_uptodate(wait[i]))
Michael Halcrow2058f832015-04-12 00:55:10 -04001090 err = -EIO;
1091 }
Chandan Rajendra7e0785f2019-05-20 09:29:49 -07001092 if (unlikely(err)) {
Michael Halcrow2058f832015-04-12 00:55:10 -04001093 page_zero_new_buffers(page, from, to);
Chandan Rajendra0b578f32019-05-20 09:29:50 -07001094 } else if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) {
1095 for (i = 0; i < nr_wait; i++) {
1096 int err2;
1097
1098 err2 = fscrypt_decrypt_pagecache_blocks(page, blocksize,
1099 bh_offset(wait[i]));
1100 if (err2) {
1101 clear_buffer_uptodate(wait[i]);
1102 err = err2;
1103 }
1104 }
Chandan Rajendra7e0785f2019-05-20 09:29:49 -07001105 }
1106
Michael Halcrow2058f832015-04-12 00:55:10 -04001107 return err;
1108}
1109#endif
1110
Nick Pigginbfc1af62007-10-16 01:25:05 -07001111static int ext4_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001112 loff_t pos, unsigned len, unsigned flags,
1113 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001114{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001115 struct inode *inode = mapping->host;
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001116 int ret, needed_blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001117 handle_t *handle;
1118 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001119 struct page *page;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001120 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001121 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001122
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05001123 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
1124 return -EIO;
1125
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001126 trace_ext4_write_begin(inode, pos, len, flags);
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001127 /*
1128 * Reserve one block more for addition to orphan list in case
1129 * we allocate blocks but write fails for some reason
1130 */
1131 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001132 index = pos >> PAGE_SHIFT;
1133 from = pos & (PAGE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001134 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001135
Tao Maf19d5872012-12-10 14:05:51 -05001136 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
1137 ret = ext4_try_to_write_inline_data(mapping, inode, pos, len,
1138 flags, pagep);
1139 if (ret < 0)
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001140 return ret;
1141 if (ret == 1)
1142 return 0;
Tao Maf19d5872012-12-10 14:05:51 -05001143 }
1144
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001145 /*
1146 * grab_cache_page_write_begin() can take a long time if the
1147 * system is thrashing due to memory pressure, or if the page
1148 * is being written back. So grab it first before we start
1149 * the transaction handle. This also allows us to allocate
1150 * the page (if needed) without using GFP_NOFS.
1151 */
1152retry_grab:
Nick Piggin54566b22009-01-04 12:00:53 -08001153 page = grab_cache_page_write_begin(mapping, index, flags);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001154 if (!page)
1155 return -ENOMEM;
1156 unlock_page(page);
1157
1158retry_journal:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001159 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001160 if (IS_ERR(handle)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001161 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001162 return PTR_ERR(handle);
Jan Karacf108bc2008-07-11 19:27:31 -04001163 }
Tao Maf19d5872012-12-10 14:05:51 -05001164
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001165 lock_page(page);
1166 if (page->mapping != mapping) {
1167 /* The page got truncated from under us */
1168 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001169 put_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04001170 ext4_journal_stop(handle);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001171 goto retry_grab;
Jan Karacf108bc2008-07-11 19:27:31 -04001172 }
Dmitry Monakhov7afe5aa2013-08-28 14:30:47 -04001173 /* In case writeback began while the page was unlocked */
1174 wait_for_stable_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04001175
Chandan Rajendra643fa962018-12-12 15:20:12 +05301176#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrow2058f832015-04-12 00:55:10 -04001177 if (ext4_should_dioread_nolock(inode))
1178 ret = ext4_block_write_begin(page, pos, len,
Jan Kara705965b2016-03-08 23:08:10 -05001179 ext4_get_block_unwritten);
Michael Halcrow2058f832015-04-12 00:55:10 -04001180 else
1181 ret = ext4_block_write_begin(page, pos, len,
1182 ext4_get_block);
1183#else
Jiaying Zhang744692d2010-03-04 16:14:02 -05001184 if (ext4_should_dioread_nolock(inode))
Jan Kara705965b2016-03-08 23:08:10 -05001185 ret = __block_write_begin(page, pos, len,
1186 ext4_get_block_unwritten);
Jiaying Zhang744692d2010-03-04 16:14:02 -05001187 else
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001188 ret = __block_write_begin(page, pos, len, ext4_get_block);
Michael Halcrow2058f832015-04-12 00:55:10 -04001189#endif
Nick Pigginbfc1af62007-10-16 01:25:05 -07001190 if (!ret && ext4_should_journal_data(inode)) {
Tao Maf19d5872012-12-10 14:05:51 -05001191 ret = ext4_walk_page_buffers(handle, page_buffers(page),
1192 from, to, NULL,
1193 do_journal_get_write_access);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001194 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001195
1196 if (ret) {
Eric Biggersc93d8f82019-07-22 09:26:24 -07001197 bool extended = (pos + len > inode->i_size) &&
1198 !ext4_verity_in_progress(inode);
1199
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001200 unlock_page(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001201 /*
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001202 * __block_write_begin may have instantiated a few blocks
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001203 * outside i_size. Trim these off again. Don't need
1204 * i_size_read because we hold i_mutex.
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001205 *
1206 * Add inode to orphan list in case we crash before
1207 * truncate finishes
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001208 */
Eric Biggersc93d8f82019-07-22 09:26:24 -07001209 if (extended && ext4_can_truncate(inode))
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001210 ext4_orphan_add(handle, inode);
1211
1212 ext4_journal_stop(handle);
Eric Biggersc93d8f82019-07-22 09:26:24 -07001213 if (extended) {
Jan Karab9a42072009-12-08 21:24:33 -05001214 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001215 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001216 * If truncate failed early the inode might
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001217 * still be on the orphan list; we need to
1218 * make sure the inode is removed from the
1219 * orphan list in that case.
1220 */
1221 if (inode->i_nlink)
1222 ext4_orphan_del(NULL, inode);
1223 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001224
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001225 if (ret == -ENOSPC &&
1226 ext4_should_retry_alloc(inode->i_sb, &retries))
1227 goto retry_journal;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001228 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001229 return ret;
1230 }
1231 *pagep = page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001232 return ret;
1233}
1234
Nick Pigginbfc1af62007-10-16 01:25:05 -07001235/* For write_end() in data=journal mode */
1236static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001237{
Theodore Ts'o13fca322013-04-21 16:45:54 -04001238 int ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001239 if (!buffer_mapped(bh) || buffer_freed(bh))
1240 return 0;
1241 set_buffer_uptodate(bh);
Theodore Ts'o13fca322013-04-21 16:45:54 -04001242 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
1243 clear_buffer_meta(bh);
1244 clear_buffer_prio(bh);
1245 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001246}
1247
Zheng Liueed43332013-04-03 12:41:17 -04001248/*
1249 * We need to pick up the new inode size which generic_commit_write gave us
1250 * `file' can be NULL - eg, when called from page_symlink().
1251 *
1252 * ext4 never places buffers on inode->i_mapping->private_list. metadata
1253 * buffers are managed internally.
1254 */
1255static int ext4_write_end(struct file *file,
1256 struct address_space *mapping,
1257 loff_t pos, unsigned len, unsigned copied,
1258 struct page *page, void *fsdata)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001259{
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001260 handle_t *handle = ext4_journal_current_handle();
Zheng Liueed43332013-04-03 12:41:17 -04001261 struct inode *inode = mapping->host;
Xiaoguang Wang05726392015-02-12 23:00:17 -05001262 loff_t old_size = inode->i_size;
Zheng Liueed43332013-04-03 12:41:17 -04001263 int ret = 0, ret2;
1264 int i_size_changed = 0;
Theodore Ts'o362eca72018-07-10 01:07:43 -04001265 int inline_data = ext4_has_inline_data(inode);
Eric Biggersc93d8f82019-07-22 09:26:24 -07001266 bool verity = ext4_verity_in_progress(inode);
Zheng Liueed43332013-04-03 12:41:17 -04001267
1268 trace_ext4_write_end(inode, pos, len, copied);
Theodore Ts'o362eca72018-07-10 01:07:43 -04001269 if (inline_data) {
Theodore Ts'o42c832d2013-07-01 08:12:39 -04001270 ret = ext4_write_inline_data_end(inode, pos, len,
1271 copied, page);
Theodore Ts'oeb5efbc2017-02-04 23:04:00 -05001272 if (ret < 0) {
1273 unlock_page(page);
1274 put_page(page);
Theodore Ts'o42c832d2013-07-01 08:12:39 -04001275 goto errout;
Theodore Ts'oeb5efbc2017-02-04 23:04:00 -05001276 }
Theodore Ts'o42c832d2013-07-01 08:12:39 -04001277 copied = ret;
1278 } else
Tao Maf19d5872012-12-10 14:05:51 -05001279 copied = block_write_end(file, mapping, pos,
1280 len, copied, page, fsdata);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001281 /*
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001282 * it's important to update i_size while still holding page lock:
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001283 * page writeout could otherwise come in and zero beyond i_size.
Eric Biggersc93d8f82019-07-22 09:26:24 -07001284 *
1285 * If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree
1286 * blocks are being written past EOF, so skip the i_size update.
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001287 */
Eric Biggersc93d8f82019-07-22 09:26:24 -07001288 if (!verity)
1289 i_size_changed = ext4_update_inode_size(inode, pos + copied);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001290 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001291 put_page(page);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001292
Eric Biggersc93d8f82019-07-22 09:26:24 -07001293 if (old_size < pos && !verity)
Xiaoguang Wang05726392015-02-12 23:00:17 -05001294 pagecache_isize_extended(inode, old_size, pos);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001295 /*
1296 * Don't mark the inode dirty under page lock. First, it unnecessarily
1297 * makes the holding time of page lock longer. Second, it forces lock
1298 * ordering of page lock and transaction start for journaling
1299 * filesystems.
1300 */
Theodore Ts'o362eca72018-07-10 01:07:43 -04001301 if (i_size_changed || inline_data)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001302 ext4_mark_inode_dirty(handle, inode);
1303
Eric Biggersc93d8f82019-07-22 09:26:24 -07001304 if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001305 /* if we have allocated more blocks and copied
1306 * less. We will have blocks allocated outside
1307 * inode->i_size. So truncate them
1308 */
1309 ext4_orphan_add(handle, inode);
Theodore Ts'o74d553a2013-04-03 12:39:17 -04001310errout:
Mingming Cao617ba132006-10-11 01:20:53 -07001311 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001312 if (!ret)
1313 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001314
Eric Biggersc93d8f82019-07-22 09:26:24 -07001315 if (pos + len > inode->i_size && !verity) {
Jan Karab9a42072009-12-08 21:24:33 -05001316 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001317 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001318 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001319 * on the orphan list; we need to make sure the inode
1320 * is removed from the orphan list in that case.
1321 */
1322 if (inode->i_nlink)
1323 ext4_orphan_del(NULL, inode);
1324 }
1325
Nick Pigginbfc1af62007-10-16 01:25:05 -07001326 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001327}
1328
Theodore Ts'ob90197b2015-10-15 10:29:05 -04001329/*
1330 * This is a private version of page_zero_new_buffers() which doesn't
1331 * set the buffer to be dirty, since in data=journalled mode we need
1332 * to call ext4_handle_dirty_metadata() instead.
1333 */
Jan Kara3b136492017-01-27 14:35:38 -05001334static void ext4_journalled_zero_new_buffers(handle_t *handle,
1335 struct page *page,
1336 unsigned from, unsigned to)
Theodore Ts'ob90197b2015-10-15 10:29:05 -04001337{
1338 unsigned int block_start = 0, block_end;
1339 struct buffer_head *head, *bh;
1340
1341 bh = head = page_buffers(page);
1342 do {
1343 block_end = block_start + bh->b_size;
1344 if (buffer_new(bh)) {
1345 if (block_end > from && block_start < to) {
1346 if (!PageUptodate(page)) {
1347 unsigned start, size;
1348
1349 start = max(from, block_start);
1350 size = min(to, block_end) - start;
1351
1352 zero_user(page, start, size);
Jan Kara3b136492017-01-27 14:35:38 -05001353 write_end_fn(handle, bh);
Theodore Ts'ob90197b2015-10-15 10:29:05 -04001354 }
1355 clear_buffer_new(bh);
1356 }
1357 }
1358 block_start = block_end;
1359 bh = bh->b_this_page;
1360 } while (bh != head);
1361}
1362
Nick Pigginbfc1af62007-10-16 01:25:05 -07001363static int ext4_journalled_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001364 struct address_space *mapping,
1365 loff_t pos, unsigned len, unsigned copied,
1366 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001367{
Mingming Cao617ba132006-10-11 01:20:53 -07001368 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -07001369 struct inode *inode = mapping->host;
Xiaoguang Wang05726392015-02-12 23:00:17 -05001370 loff_t old_size = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001371 int ret = 0, ret2;
1372 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001373 unsigned from, to;
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001374 int size_changed = 0;
Theodore Ts'o362eca72018-07-10 01:07:43 -04001375 int inline_data = ext4_has_inline_data(inode);
Eric Biggersc93d8f82019-07-22 09:26:24 -07001376 bool verity = ext4_verity_in_progress(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001377
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001378 trace_ext4_journalled_write_end(inode, pos, len, copied);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001379 from = pos & (PAGE_SIZE - 1);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001380 to = from + len;
1381
Curt Wohlgemuth441c8502011-08-13 11:25:18 -04001382 BUG_ON(!ext4_handle_valid(handle));
1383
Theodore Ts'o362eca72018-07-10 01:07:43 -04001384 if (inline_data) {
Theodore Ts'oeb5efbc2017-02-04 23:04:00 -05001385 ret = ext4_write_inline_data_end(inode, pos, len,
1386 copied, page);
1387 if (ret < 0) {
1388 unlock_page(page);
1389 put_page(page);
1390 goto errout;
1391 }
1392 copied = ret;
1393 } else if (unlikely(copied < len) && !PageUptodate(page)) {
Jan Kara3b136492017-01-27 14:35:38 -05001394 copied = 0;
1395 ext4_journalled_zero_new_buffers(handle, page, from, to);
1396 } else {
1397 if (unlikely(copied < len))
1398 ext4_journalled_zero_new_buffers(handle, page,
1399 from + copied, to);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001400 ret = ext4_walk_page_buffers(handle, page_buffers(page), from,
Jan Kara3b136492017-01-27 14:35:38 -05001401 from + copied, &partial,
1402 write_end_fn);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001403 if (!partial)
1404 SetPageUptodate(page);
1405 }
Eric Biggersc93d8f82019-07-22 09:26:24 -07001406 if (!verity)
1407 size_changed = ext4_update_inode_size(inode, pos + copied);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001408 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Jan Kara2d859db2011-07-26 09:07:11 -04001409 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001410 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001411 put_page(page);
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001412
Eric Biggersc93d8f82019-07-22 09:26:24 -07001413 if (old_size < pos && !verity)
Xiaoguang Wang05726392015-02-12 23:00:17 -05001414 pagecache_isize_extended(inode, old_size, pos);
1415
Theodore Ts'o362eca72018-07-10 01:07:43 -04001416 if (size_changed || inline_data) {
Mingming Cao617ba132006-10-11 01:20:53 -07001417 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001418 if (!ret)
1419 ret = ret2;
1420 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001421
Eric Biggersc93d8f82019-07-22 09:26:24 -07001422 if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001423 /* if we have allocated more blocks and copied
1424 * less. We will have blocks allocated outside
1425 * inode->i_size. So truncate them
1426 */
1427 ext4_orphan_add(handle, inode);
1428
Theodore Ts'oeb5efbc2017-02-04 23:04:00 -05001429errout:
Mingming Cao617ba132006-10-11 01:20:53 -07001430 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001431 if (!ret)
1432 ret = ret2;
Eric Biggersc93d8f82019-07-22 09:26:24 -07001433 if (pos + len > inode->i_size && !verity) {
Jan Karab9a42072009-12-08 21:24:33 -05001434 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001435 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001436 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001437 * on the orphan list; we need to make sure the inode
1438 * is removed from the orphan list in that case.
1439 */
1440 if (inode->i_nlink)
1441 ext4_orphan_del(NULL, inode);
1442 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001443
1444 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001445}
Mingming Caod2a17632008-07-14 17:52:37 -04001446
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001447/*
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001448 * Reserve space for a single cluster
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001449 */
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001450static int ext4_da_reserve_space(struct inode *inode)
Mingming Caod2a17632008-07-14 17:52:37 -04001451{
Mingming Cao60e58e02009-01-22 18:13:05 +01001452 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001453 struct ext4_inode_info *ei = EXT4_I(inode);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001454 int ret;
Mingming Caod2a17632008-07-14 17:52:37 -04001455
Mingming Cao60e58e02009-01-22 18:13:05 +01001456 /*
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001457 * We will charge metadata quota at writeout time; this saves
1458 * us from metadata over-estimation, though we may go over by
1459 * a small amount in the end. Here we just reserve for data.
Mingming Cao60e58e02009-01-22 18:13:05 +01001460 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04001461 ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001462 if (ret)
1463 return ret;
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001464
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001465 spin_lock(&ei->i_block_reservation_lock);
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04001466 if (ext4_claim_free_clusters(sbi, 1, 0)) {
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001467 spin_unlock(&ei->i_block_reservation_lock);
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001468 dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
Mingming Caod2a17632008-07-14 17:52:37 -04001469 return -ENOSPC;
1470 }
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001471 ei->i_reserved_data_blocks++;
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001472 trace_ext4_da_reserve_space(inode);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001473 spin_unlock(&ei->i_block_reservation_lock);
Dmitry Monakhov39bc6802009-12-10 16:36:27 +00001474
Mingming Caod2a17632008-07-14 17:52:37 -04001475 return 0; /* success */
1476}
1477
Eric Whitneyf4567672018-10-01 14:33:24 -04001478void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001479{
1480 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001481 struct ext4_inode_info *ei = EXT4_I(inode);
Mingming Caod2a17632008-07-14 17:52:37 -04001482
Mingming Caocd213222008-08-19 22:16:59 -04001483 if (!to_free)
1484 return; /* Nothing to release, exit */
1485
Mingming Caod2a17632008-07-14 17:52:37 -04001486 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001487
Li Zefan5a58ec872010-05-17 02:00:00 -04001488 trace_ext4_da_release_space(inode, to_free);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001489 if (unlikely(to_free > ei->i_reserved_data_blocks)) {
Mingming Caocd213222008-08-19 22:16:59 -04001490 /*
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001491 * if there aren't enough reserved blocks, then the
1492 * counter is messed up somewhere. Since this
1493 * function is called from invalidate page, it's
1494 * harmless to return without any action.
Mingming Caocd213222008-08-19 22:16:59 -04001495 */
Theodore Ts'o8de5c322013-02-14 15:11:41 -05001496 ext4_warning(inode->i_sb, "ext4_da_release_space: "
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001497 "ino %lu, to_free %d with only %d reserved "
Theodore Ts'o1084f252012-03-19 23:13:43 -04001498 "data blocks", inode->i_ino, to_free,
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001499 ei->i_reserved_data_blocks);
1500 WARN_ON(1);
1501 to_free = ei->i_reserved_data_blocks;
1502 }
1503 ei->i_reserved_data_blocks -= to_free;
1504
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001505 /* update fs dirty data blocks counter */
Theodore Ts'o57042652011-09-09 18:56:51 -04001506 percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free);
Mingming Caod2a17632008-07-14 17:52:37 -04001507
Mingming Caod2a17632008-07-14 17:52:37 -04001508 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001509
Aditya Kali7b415bf2011-09-09 19:04:51 -04001510 dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free));
Mingming Caod2a17632008-07-14 17:52:37 -04001511}
1512
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001513/*
Alex Tomas64769242008-07-11 19:27:31 -04001514 * Delayed allocation stuff
1515 */
1516
Jan Kara4e7ea812013-06-04 13:17:40 -04001517struct mpage_da_data {
1518 struct inode *inode;
1519 struct writeback_control *wbc;
Jan Kara6b523df2013-06-04 13:21:11 -04001520
Jan Kara4e7ea812013-06-04 13:17:40 -04001521 pgoff_t first_page; /* The first page to write */
1522 pgoff_t next_page; /* Current page to examine */
1523 pgoff_t last_page; /* Last page to examine */
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001524 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04001525 * Extent to map - this can be after first_page because that can be
1526 * fully mapped. We somewhat abuse m_flags to store whether the extent
1527 * is delalloc or unwritten.
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001528 */
Jan Kara4e7ea812013-06-04 13:17:40 -04001529 struct ext4_map_blocks map;
1530 struct ext4_io_submit io_submit; /* IO submission data */
Jan Karadddbd6a2017-04-30 18:29:10 -04001531 unsigned int do_map:1;
Jan Kara4e7ea812013-06-04 13:17:40 -04001532};
Alex Tomas64769242008-07-11 19:27:31 -04001533
Jan Kara4e7ea812013-06-04 13:17:40 -04001534static void mpage_release_unused_pages(struct mpage_da_data *mpd,
1535 bool invalidate)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001536{
1537 int nr_pages, i;
1538 pgoff_t index, end;
1539 struct pagevec pvec;
1540 struct inode *inode = mpd->inode;
1541 struct address_space *mapping = inode->i_mapping;
Jan Kara4e7ea812013-06-04 13:17:40 -04001542
1543 /* This is necessary when next_page == 0. */
1544 if (mpd->first_page >= mpd->next_page)
1545 return;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001546
Curt Wohlgemuthc7f59382011-02-26 12:27:52 -05001547 index = mpd->first_page;
1548 end = mpd->next_page - 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04001549 if (invalidate) {
1550 ext4_lblk_t start, last;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001551 start = index << (PAGE_SHIFT - inode->i_blkbits);
1552 last = end << (PAGE_SHIFT - inode->i_blkbits);
Jan Kara4e7ea812013-06-04 13:17:40 -04001553 ext4_es_remove_extent(inode, start, last - start + 1);
1554 }
Zheng Liu51865fd2012-11-08 21:57:32 -05001555
Mel Gorman86679822017-11-15 17:37:52 -08001556 pagevec_init(&pvec);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001557 while (index <= end) {
Jan Kara397162f2017-09-06 16:21:43 -07001558 nr_pages = pagevec_lookup_range(&pvec, mapping, &index, end);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001559 if (nr_pages == 0)
1560 break;
1561 for (i = 0; i < nr_pages; i++) {
1562 struct page *page = pvec.pages[i];
Jan Kara2b85a612017-09-06 16:21:30 -07001563
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001564 BUG_ON(!PageLocked(page));
1565 BUG_ON(PageWriteback(page));
Jan Kara4e7ea812013-06-04 13:17:40 -04001566 if (invalidate) {
wangguang4e800c02016-09-15 11:32:46 -04001567 if (page_mapped(page))
1568 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001569 block_invalidatepage(page, 0, PAGE_SIZE);
Jan Kara4e7ea812013-06-04 13:17:40 -04001570 ClearPageUptodate(page);
1571 }
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001572 unlock_page(page);
1573 }
Jan Kara9b1d09982010-03-03 16:19:32 -05001574 pagevec_release(&pvec);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001575 }
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001576}
1577
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001578static void ext4_print_free_blocks(struct inode *inode)
1579{
1580 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o92b97812012-03-19 23:41:49 -04001581 struct super_block *sb = inode->i_sb;
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001582 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'o92b97812012-03-19 23:41:49 -04001583
1584 ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld",
Theodore Ts'o5dee5432011-09-09 19:10:51 -04001585 EXT4_C2B(EXT4_SB(inode->i_sb),
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001586 ext4_count_free_clusters(sb)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001587 ext4_msg(sb, KERN_CRIT, "Free/Dirty block details");
1588 ext4_msg(sb, KERN_CRIT, "free_blocks=%lld",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001589 (long long) EXT4_C2B(EXT4_SB(sb),
Theodore Ts'o57042652011-09-09 18:56:51 -04001590 percpu_counter_sum(&sbi->s_freeclusters_counter)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001591 ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001592 (long long) EXT4_C2B(EXT4_SB(sb),
Aditya Kali7b415bf2011-09-09 19:04:51 -04001593 percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001594 ext4_msg(sb, KERN_CRIT, "Block reservation details");
1595 ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001596 ei->i_reserved_data_blocks);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001597 return;
1598}
1599
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001600static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001601{
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001602 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001603}
1604
Alex Tomas64769242008-07-11 19:27:31 -04001605/*
Eric Whitney0b02f4c2018-10-01 14:19:37 -04001606 * ext4_insert_delayed_block - adds a delayed block to the extents status
1607 * tree, incrementing the reserved cluster/block
1608 * count or making a pending reservation
1609 * where needed
1610 *
1611 * @inode - file containing the newly added block
1612 * @lblk - logical block to be added
1613 *
1614 * Returns 0 on success, negative error code on failure.
1615 */
1616static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk)
1617{
1618 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1619 int ret;
1620 bool allocated = false;
1621
1622 /*
1623 * If the cluster containing lblk is shared with a delayed,
1624 * written, or unwritten extent in a bigalloc file system, it's
1625 * already been accounted for and does not need to be reserved.
1626 * A pending reservation must be made for the cluster if it's
1627 * shared with a written or unwritten extent and doesn't already
1628 * have one. Written and unwritten extents can be purged from the
1629 * extents status tree if the system is under memory pressure, so
1630 * it's necessary to examine the extent tree if a search of the
1631 * extents status tree doesn't get a match.
1632 */
1633 if (sbi->s_cluster_ratio == 1) {
1634 ret = ext4_da_reserve_space(inode);
1635 if (ret != 0) /* ENOSPC */
1636 goto errout;
1637 } else { /* bigalloc */
1638 if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) {
1639 if (!ext4_es_scan_clu(inode,
1640 &ext4_es_is_mapped, lblk)) {
1641 ret = ext4_clu_mapped(inode,
1642 EXT4_B2C(sbi, lblk));
1643 if (ret < 0)
1644 goto errout;
1645 if (ret == 0) {
1646 ret = ext4_da_reserve_space(inode);
1647 if (ret != 0) /* ENOSPC */
1648 goto errout;
1649 } else {
1650 allocated = true;
1651 }
1652 } else {
1653 allocated = true;
1654 }
1655 }
1656 }
1657
1658 ret = ext4_es_insert_delayed_block(inode, lblk, allocated);
1659
1660errout:
1661 return ret;
1662}
1663
1664/*
Aditya Kali5356f2612011-09-09 19:20:51 -04001665 * This function is grabs code from the very beginning of
1666 * ext4_map_blocks, but assumes that the caller is from delayed write
1667 * time. This function looks up the requested blocks and sets the
1668 * buffer delay bit under the protection of i_data_sem.
1669 */
1670static int ext4_da_map_blocks(struct inode *inode, sector_t iblock,
1671 struct ext4_map_blocks *map,
1672 struct buffer_head *bh)
1673{
Zheng Liud100eef2013-02-18 00:29:59 -05001674 struct extent_status es;
Aditya Kali5356f2612011-09-09 19:20:51 -04001675 int retval;
1676 sector_t invalid_block = ~((sector_t) 0xffff);
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001677#ifdef ES_AGGRESSIVE_TEST
1678 struct ext4_map_blocks orig_map;
1679
1680 memcpy(&orig_map, map, sizeof(*map));
1681#endif
Aditya Kali5356f2612011-09-09 19:20:51 -04001682
1683 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
1684 invalid_block = ~0;
1685
1686 map->m_flags = 0;
1687 ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
1688 "logical block %lu\n", inode->i_ino, map->m_len,
1689 (unsigned long) map->m_lblk);
Zheng Liud100eef2013-02-18 00:29:59 -05001690
1691 /* Lookup extent status tree firstly */
Theodore Ts'obb5835e2019-08-11 16:32:41 -04001692 if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) {
Zheng Liud100eef2013-02-18 00:29:59 -05001693 if (ext4_es_is_hole(&es)) {
1694 retval = 0;
Lukas Czernerc8b459f2014-05-12 12:55:07 -04001695 down_read(&EXT4_I(inode)->i_data_sem);
Zheng Liud100eef2013-02-18 00:29:59 -05001696 goto add_delayed;
1697 }
1698
1699 /*
1700 * Delayed extent could be allocated by fallocate.
1701 * So we need to check it.
1702 */
1703 if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) {
1704 map_bh(bh, inode->i_sb, invalid_block);
1705 set_buffer_new(bh);
1706 set_buffer_delay(bh);
1707 return 0;
1708 }
1709
1710 map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk;
1711 retval = es.es_len - (iblock - es.es_lblk);
1712 if (retval > map->m_len)
1713 retval = map->m_len;
1714 map->m_len = retval;
1715 if (ext4_es_is_written(&es))
1716 map->m_flags |= EXT4_MAP_MAPPED;
1717 else if (ext4_es_is_unwritten(&es))
1718 map->m_flags |= EXT4_MAP_UNWRITTEN;
1719 else
Arnd Bergmann1e83bc82019-04-07 12:24:43 -04001720 BUG();
Zheng Liud100eef2013-02-18 00:29:59 -05001721
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001722#ifdef ES_AGGRESSIVE_TEST
1723 ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0);
1724#endif
Zheng Liud100eef2013-02-18 00:29:59 -05001725 return retval;
1726 }
1727
Aditya Kali5356f2612011-09-09 19:20:51 -04001728 /*
1729 * Try to see if we can get the block without requesting a new
1730 * file system block.
1731 */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04001732 down_read(&EXT4_I(inode)->i_data_sem);
Jan Karacbd75842014-11-25 11:41:49 -05001733 if (ext4_has_inline_data(inode))
Tao Ma9c3569b2012-12-10 14:05:57 -05001734 retval = 0;
Jan Karacbd75842014-11-25 11:41:49 -05001735 else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Zheng Liu2f8e0a72014-11-25 11:44:37 -05001736 retval = ext4_ext_map_blocks(NULL, inode, map, 0);
Aditya Kali5356f2612011-09-09 19:20:51 -04001737 else
Zheng Liu2f8e0a72014-11-25 11:44:37 -05001738 retval = ext4_ind_map_blocks(NULL, inode, map, 0);
Aditya Kali5356f2612011-09-09 19:20:51 -04001739
Zheng Liud100eef2013-02-18 00:29:59 -05001740add_delayed:
Aditya Kali5356f2612011-09-09 19:20:51 -04001741 if (retval == 0) {
Zheng Liuf7fec032013-02-18 00:28:47 -05001742 int ret;
Eric Whitneyad431022018-10-01 14:10:39 -04001743
Aditya Kali5356f2612011-09-09 19:20:51 -04001744 /*
1745 * XXX: __block_prepare_write() unmaps passed block,
1746 * is it OK?
1747 */
Aditya Kali5356f2612011-09-09 19:20:51 -04001748
Eric Whitney0b02f4c2018-10-01 14:19:37 -04001749 ret = ext4_insert_delayed_block(inode, map->m_lblk);
1750 if (ret != 0) {
Zheng Liuf7fec032013-02-18 00:28:47 -05001751 retval = ret;
Zheng Liu51865fd2012-11-08 21:57:32 -05001752 goto out_unlock;
Zheng Liuf7fec032013-02-18 00:28:47 -05001753 }
Zheng Liu51865fd2012-11-08 21:57:32 -05001754
Aditya Kali5356f2612011-09-09 19:20:51 -04001755 map_bh(bh, inode->i_sb, invalid_block);
1756 set_buffer_new(bh);
1757 set_buffer_delay(bh);
Zheng Liuf7fec032013-02-18 00:28:47 -05001758 } else if (retval > 0) {
1759 int ret;
Theodore Ts'o3be78c72013-08-16 21:22:41 -04001760 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -05001761
Zheng Liu44fb851d2013-07-29 12:51:42 -04001762 if (unlikely(retval != map->m_len)) {
1763 ext4_warning(inode->i_sb,
1764 "ES len assertion failed for inode "
1765 "%lu: retval %d != map->m_len %d",
1766 inode->i_ino, retval, map->m_len);
1767 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001768 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001769
Zheng Liuf7fec032013-02-18 00:28:47 -05001770 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
1771 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
1772 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
1773 map->m_pblk, status);
1774 if (ret != 0)
1775 retval = ret;
Aditya Kali5356f2612011-09-09 19:20:51 -04001776 }
1777
1778out_unlock:
1779 up_read((&EXT4_I(inode)->i_data_sem));
1780
1781 return retval;
1782}
1783
1784/*
Seunghun Leed91bd2c2014-09-01 22:15:30 -04001785 * This is a special get_block_t callback which is used by
Theodore Ts'ob920c752009-05-14 00:54:29 -04001786 * ext4_da_write_begin(). It will either return mapped block or
1787 * reserve space for a single block.
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001788 *
1789 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
1790 * We also have b_blocknr = -1 and b_bdev initialized properly
1791 *
1792 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
1793 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
1794 * initialized properly.
Alex Tomas64769242008-07-11 19:27:31 -04001795 */
Tao Ma9c3569b2012-12-10 14:05:57 -05001796int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
1797 struct buffer_head *bh, int create)
Alex Tomas64769242008-07-11 19:27:31 -04001798{
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001799 struct ext4_map_blocks map;
Alex Tomas64769242008-07-11 19:27:31 -04001800 int ret = 0;
1801
1802 BUG_ON(create == 0);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001803 BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
1804
1805 map.m_lblk = iblock;
1806 map.m_len = 1;
Alex Tomas64769242008-07-11 19:27:31 -04001807
1808 /*
1809 * first, we need to know whether the block is allocated already
1810 * preallocated blocks are unmapped but should treated
1811 * the same as allocated blocks.
1812 */
Aditya Kali5356f2612011-09-09 19:20:51 -04001813 ret = ext4_da_map_blocks(inode, iblock, &map, bh);
1814 if (ret <= 0)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001815 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04001816
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001817 map_bh(bh, inode->i_sb, map.m_pblk);
Jan Karaed8ad832016-02-19 00:18:25 -05001818 ext4_update_bh_state(bh, map.m_flags);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001819
1820 if (buffer_unwritten(bh)) {
1821 /* A delayed write to unwritten bh should be marked
1822 * new and mapped. Mapped ensures that we don't do
1823 * get_block multiple times when we write to the same
1824 * offset and new ensures that we do proper zero out
1825 * for partial write.
1826 */
1827 set_buffer_new(bh);
Theodore Ts'oc8205632011-04-10 22:30:07 -04001828 set_buffer_mapped(bh);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001829 }
1830 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04001831}
Mingming Cao61628a32008-07-11 19:27:31 -04001832
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001833static int bget_one(handle_t *handle, struct buffer_head *bh)
1834{
1835 get_bh(bh);
1836 return 0;
1837}
1838
1839static int bput_one(handle_t *handle, struct buffer_head *bh)
1840{
1841 put_bh(bh);
1842 return 0;
1843}
1844
1845static int __ext4_journalled_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001846 unsigned int len)
1847{
1848 struct address_space *mapping = page->mapping;
1849 struct inode *inode = mapping->host;
Tao Ma3fdcfb62012-12-10 14:05:57 -05001850 struct buffer_head *page_bufs = NULL;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001851 handle_t *handle = NULL;
Tao Ma3fdcfb62012-12-10 14:05:57 -05001852 int ret = 0, err = 0;
1853 int inline_data = ext4_has_inline_data(inode);
1854 struct buffer_head *inode_bh = NULL;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001855
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001856 ClearPageChecked(page);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001857
1858 if (inline_data) {
1859 BUG_ON(page->index != 0);
1860 BUG_ON(len > ext4_get_max_inline_size(inode));
1861 inode_bh = ext4_journalled_write_inline_data(inode, len, page);
1862 if (inode_bh == NULL)
1863 goto out;
1864 } else {
1865 page_bufs = page_buffers(page);
1866 if (!page_bufs) {
1867 BUG();
1868 goto out;
1869 }
1870 ext4_walk_page_buffers(handle, page_bufs, 0, len,
1871 NULL, bget_one);
1872 }
Theodore Ts'obdf96832015-06-12 23:45:33 -04001873 /*
1874 * We need to release the page lock before we start the
1875 * journal, so grab a reference so the page won't disappear
1876 * out from under us.
1877 */
1878 get_page(page);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001879 unlock_page(page);
1880
Theodore Ts'o9924a922013-02-08 21:59:22 -05001881 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
1882 ext4_writepage_trans_blocks(inode));
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001883 if (IS_ERR(handle)) {
1884 ret = PTR_ERR(handle);
Theodore Ts'obdf96832015-06-12 23:45:33 -04001885 put_page(page);
1886 goto out_no_pagelock;
1887 }
1888 BUG_ON(!ext4_handle_valid(handle));
1889
1890 lock_page(page);
1891 put_page(page);
1892 if (page->mapping != mapping) {
1893 /* The page got truncated from under us */
1894 ext4_journal_stop(handle);
1895 ret = 0;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001896 goto out;
1897 }
1898
Tao Ma3fdcfb62012-12-10 14:05:57 -05001899 if (inline_data) {
Theodore Ts'o362eca72018-07-10 01:07:43 -04001900 ret = ext4_mark_inode_dirty(handle, inode);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001901 } else {
1902 ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
1903 do_journal_get_write_access);
1904
1905 err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
1906 write_end_fn);
1907 }
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001908 if (ret == 0)
1909 ret = err;
Jan Kara2d859db2011-07-26 09:07:11 -04001910 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001911 err = ext4_journal_stop(handle);
1912 if (!ret)
1913 ret = err;
1914
Tao Ma3fdcfb62012-12-10 14:05:57 -05001915 if (!ext4_has_inline_data(inode))
Theodore Ts'o8c9367f2014-01-07 13:08:03 -05001916 ext4_walk_page_buffers(NULL, page_bufs, 0, len,
Tao Ma3fdcfb62012-12-10 14:05:57 -05001917 NULL, bput_one);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001918 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001919out:
Theodore Ts'obdf96832015-06-12 23:45:33 -04001920 unlock_page(page);
1921out_no_pagelock:
Tao Ma3fdcfb62012-12-10 14:05:57 -05001922 brelse(inode_bh);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001923 return ret;
1924}
1925
Mingming Cao61628a32008-07-11 19:27:31 -04001926/*
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001927 * Note that we don't need to start a transaction unless we're journaling data
1928 * because we should have holes filled from ext4_page_mkwrite(). We even don't
1929 * need to file the inode to the transaction's list in ordered mode because if
1930 * we are writing back data added by write(), the inode is already there and if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001931 * we are writing back data modified via mmap(), no one guarantees in which
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001932 * transaction the data will hit the disk. In case we are journaling data, we
1933 * cannot start transaction directly because transaction start ranks above page
1934 * lock so we have to do some magic.
1935 *
Theodore Ts'ob920c752009-05-14 00:54:29 -04001936 * This function can get called via...
Theodore Ts'o20970ba2013-06-06 14:00:46 -04001937 * - ext4_writepages after taking page lock (have journal handle)
Theodore Ts'ob920c752009-05-14 00:54:29 -04001938 * - journal_submit_inode_data_buffers (no journal handle)
Artem Bityutskiyf6463b02012-07-25 18:12:04 +03001939 * - shrink_page_list via the kswapd/direct reclaim (no journal handle)
Theodore Ts'ob920c752009-05-14 00:54:29 -04001940 * - grab_page_cache when doing write_begin (have journal handle)
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001941 *
1942 * We don't do any block allocation in this function. If we have page with
1943 * multiple blocks we need to write those buffer_heads that are mapped. This
1944 * is important for mmaped based write. So if we do with blocksize 1K
1945 * truncate(f, 1024);
1946 * a = mmap(f, 0, 4096);
1947 * a[0] = 'a';
1948 * truncate(f, 4096);
1949 * we have in the page first buffer_head mapped via page_mkwrite call back
Paul Bolle90802ed2011-12-05 13:00:34 +01001950 * but other buffer_heads would be unmapped but dirty (dirty done via the
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001951 * do_wp_page). So writepage should write the first block. If we modify
1952 * the mmap area beyond 1024 we will again get a page_fault and the
1953 * page_mkwrite callback will do the block allocation and mark the
1954 * buffer_heads mapped.
1955 *
1956 * We redirty the page if we have any buffer_heads that is either delay or
1957 * unwritten in the page.
1958 *
1959 * We can get recursively called as show below.
1960 *
1961 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
1962 * ext4_writepage()
1963 *
1964 * But since we don't do any block allocation we should not deadlock.
1965 * Page also have the dirty flag cleared so we don't get recurive page_lock.
Mingming Cao61628a32008-07-11 19:27:31 -04001966 */
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001967static int ext4_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001968 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04001969{
Jan Karaf8bec372013-01-28 12:55:08 -05001970 int ret = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04001971 loff_t size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001972 unsigned int len;
Jiaying Zhang744692d2010-03-04 16:14:02 -05001973 struct buffer_head *page_bufs = NULL;
Mingming Cao61628a32008-07-11 19:27:31 -04001974 struct inode *inode = page->mapping->host;
Jan Kara36ade452013-01-28 09:30:52 -05001975 struct ext4_io_submit io_submit;
Namjae Jeon1c8349a2014-05-12 08:12:25 -04001976 bool keep_towrite = false;
Alex Tomas64769242008-07-11 19:27:31 -04001977
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05001978 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) {
1979 ext4_invalidatepage(page, 0, PAGE_SIZE);
1980 unlock_page(page);
1981 return -EIO;
1982 }
1983
Lukas Czernera9c667f2011-06-06 09:51:52 -04001984 trace_ext4_writepage(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04001985 size = i_size_read(inode);
Eric Biggersc93d8f82019-07-22 09:26:24 -07001986 if (page->index == size >> PAGE_SHIFT &&
1987 !ext4_verity_in_progress(inode))
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001988 len = size & ~PAGE_MASK;
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04001989 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001990 len = PAGE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04001991
Theodore Ts'oa42afc52010-10-27 21:30:09 -04001992 page_bufs = page_buffers(page);
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001993 /*
Jan Karafe386132013-01-28 21:06:42 -05001994 * We cannot do block allocation or other extent handling in this
1995 * function. If there are buffers needing that, we have to redirty
1996 * the page. But we may reach here when we do a journal commit via
1997 * journal_submit_inode_data_buffers() and in that case we must write
1998 * allocated buffers to achieve data=ordered mode guarantees.
Theodore Ts'occcd1472015-10-03 10:49:23 -04001999 *
2000 * Also, if there is only one buffer per page (the fs block
2001 * size == the page size), if one buffer needs block
2002 * allocation or needs to modify the extent tree to clear the
2003 * unwritten flag, we know that the page can't be written at
2004 * all, so we might as well refuse the write immediately.
2005 * Unfortunately if the block size != page size, we can't as
2006 * easily detect this case using ext4_walk_page_buffers(), but
2007 * for the extremely common case, this is an optimization that
2008 * skips a useless round trip through ext4_bio_write_page().
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04002009 */
Tao Maf19d5872012-12-10 14:05:51 -05002010 if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL,
2011 ext4_bh_delay_or_unwritten)) {
Jan Karaf8bec372013-01-28 12:55:08 -05002012 redirty_page_for_writepage(wbc, page);
Theodore Ts'occcd1472015-10-03 10:49:23 -04002013 if ((current->flags & PF_MEMALLOC) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002014 (inode->i_sb->s_blocksize == PAGE_SIZE)) {
Jan Karafe386132013-01-28 21:06:42 -05002015 /*
2016 * For memory cleaning there's no point in writing only
2017 * some buffers. So just bail out. Warn if we came here
2018 * from direct reclaim.
2019 */
2020 WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD))
2021 == PF_MEMALLOC);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002022 unlock_page(page);
2023 return 0;
2024 }
Namjae Jeon1c8349a2014-05-12 08:12:25 -04002025 keep_towrite = true;
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002026 }
Alex Tomas64769242008-07-11 19:27:31 -04002027
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002028 if (PageChecked(page) && ext4_should_journal_data(inode))
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002029 /*
2030 * It's mmapped pagecache. Add buffers and journal it. There
2031 * doesn't seem much point in redirtying the page here.
2032 */
Wu Fengguang3f0ca302009-11-24 11:15:44 -05002033 return __ext4_journalled_writepage(page, len);
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002034
Jan Kara97a851e2013-06-04 11:58:58 -04002035 ext4_io_submit_init(&io_submit, wbc);
2036 io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS);
2037 if (!io_submit.io_end) {
2038 redirty_page_for_writepage(wbc, page);
2039 unlock_page(page);
2040 return -ENOMEM;
2041 }
Namjae Jeon1c8349a2014-05-12 08:12:25 -04002042 ret = ext4_bio_write_page(&io_submit, page, len, wbc, keep_towrite);
Jan Kara36ade452013-01-28 09:30:52 -05002043 ext4_io_submit(&io_submit);
Jan Kara97a851e2013-06-04 11:58:58 -04002044 /* Drop io_end reference we got from init */
2045 ext4_put_io_end_defer(io_submit.io_end);
Alex Tomas64769242008-07-11 19:27:31 -04002046 return ret;
2047}
2048
Jan Kara5f1132b2013-08-17 10:02:33 -04002049static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page)
2050{
2051 int len;
Jan Karaa056bda2017-05-26 17:45:45 -04002052 loff_t size;
Jan Kara5f1132b2013-08-17 10:02:33 -04002053 int err;
2054
2055 BUG_ON(page->index != mpd->first_page);
Jan Karaa056bda2017-05-26 17:45:45 -04002056 clear_page_dirty_for_io(page);
2057 /*
2058 * We have to be very careful here! Nothing protects writeback path
2059 * against i_size changes and the page can be writeably mapped into
2060 * page tables. So an application can be growing i_size and writing
2061 * data through mmap while writeback runs. clear_page_dirty_for_io()
2062 * write-protects our page in page tables and the page cannot get
2063 * written to again until we release page lock. So only after
2064 * clear_page_dirty_for_io() we are safe to sample i_size for
2065 * ext4_bio_write_page() to zero-out tail of the written page. We rely
2066 * on the barrier provided by TestClearPageDirty in
2067 * clear_page_dirty_for_io() to make sure i_size is really sampled only
2068 * after page tables are updated.
2069 */
2070 size = i_size_read(mpd->inode);
Eric Biggersc93d8f82019-07-22 09:26:24 -07002071 if (page->index == size >> PAGE_SHIFT &&
2072 !ext4_verity_in_progress(mpd->inode))
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002073 len = size & ~PAGE_MASK;
Jan Kara5f1132b2013-08-17 10:02:33 -04002074 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002075 len = PAGE_SIZE;
Namjae Jeon1c8349a2014-05-12 08:12:25 -04002076 err = ext4_bio_write_page(&mpd->io_submit, page, len, mpd->wbc, false);
Jan Kara5f1132b2013-08-17 10:02:33 -04002077 if (!err)
2078 mpd->wbc->nr_to_write--;
2079 mpd->first_page++;
2080
2081 return err;
2082}
2083
Jan Kara4e7ea812013-06-04 13:17:40 -04002084#define BH_FLAGS ((1 << BH_Unwritten) | (1 << BH_Delay))
2085
Mingming Cao61628a32008-07-11 19:27:31 -04002086/*
Jan Karafffb2732013-06-04 13:01:11 -04002087 * mballoc gives us at most this number of blocks...
2088 * XXX: That seems to be only a limitation of ext4_mb_normalize_request().
Anatol Pomozov70261f52013-08-28 14:40:12 -04002089 * The rest of mballoc seems to handle chunks up to full group size.
Mingming Cao61628a32008-07-11 19:27:31 -04002090 */
Jan Karafffb2732013-06-04 13:01:11 -04002091#define MAX_WRITEPAGES_EXTENT_LEN 2048
Mingming Cao525f4ed2008-08-19 22:15:58 -04002092
Jan Karafffb2732013-06-04 13:01:11 -04002093/*
Jan Kara4e7ea812013-06-04 13:17:40 -04002094 * mpage_add_bh_to_extent - try to add bh to extent of blocks to map
2095 *
2096 * @mpd - extent of blocks
2097 * @lblk - logical number of the block in the file
Jan Kara09930042013-08-17 09:57:56 -04002098 * @bh - buffer head we want to add to the extent
Jan Kara4e7ea812013-06-04 13:17:40 -04002099 *
Jan Kara09930042013-08-17 09:57:56 -04002100 * The function is used to collect contig. blocks in the same state. If the
2101 * buffer doesn't require mapping for writeback and we haven't started the
2102 * extent of buffers to map yet, the function returns 'true' immediately - the
2103 * caller can write the buffer right away. Otherwise the function returns true
2104 * if the block has been added to the extent, false if the block couldn't be
2105 * added.
Jan Kara4e7ea812013-06-04 13:17:40 -04002106 */
Jan Kara09930042013-08-17 09:57:56 -04002107static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk,
2108 struct buffer_head *bh)
Jan Kara4e7ea812013-06-04 13:17:40 -04002109{
2110 struct ext4_map_blocks *map = &mpd->map;
2111
Jan Kara09930042013-08-17 09:57:56 -04002112 /* Buffer that doesn't need mapping for writeback? */
2113 if (!buffer_dirty(bh) || !buffer_mapped(bh) ||
2114 (!buffer_delay(bh) && !buffer_unwritten(bh))) {
2115 /* So far no extent to map => we write the buffer right away */
2116 if (map->m_len == 0)
2117 return true;
2118 return false;
2119 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002120
2121 /* First block in the extent? */
2122 if (map->m_len == 0) {
Jan Karadddbd6a2017-04-30 18:29:10 -04002123 /* We cannot map unless handle is started... */
2124 if (!mpd->do_map)
2125 return false;
Jan Kara4e7ea812013-06-04 13:17:40 -04002126 map->m_lblk = lblk;
2127 map->m_len = 1;
Jan Kara09930042013-08-17 09:57:56 -04002128 map->m_flags = bh->b_state & BH_FLAGS;
2129 return true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002130 }
2131
Jan Kara09930042013-08-17 09:57:56 -04002132 /* Don't go larger than mballoc is willing to allocate */
2133 if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN)
2134 return false;
2135
Jan Kara4e7ea812013-06-04 13:17:40 -04002136 /* Can we merge the block to our big extent? */
2137 if (lblk == map->m_lblk + map->m_len &&
Jan Kara09930042013-08-17 09:57:56 -04002138 (bh->b_state & BH_FLAGS) == map->m_flags) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002139 map->m_len++;
Jan Kara09930042013-08-17 09:57:56 -04002140 return true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002141 }
Jan Kara09930042013-08-17 09:57:56 -04002142 return false;
Jan Kara4e7ea812013-06-04 13:17:40 -04002143}
2144
Jan Kara5f1132b2013-08-17 10:02:33 -04002145/*
2146 * mpage_process_page_bufs - submit page buffers for IO or add them to extent
2147 *
2148 * @mpd - extent of blocks for mapping
2149 * @head - the first buffer in the page
2150 * @bh - buffer we should start processing from
2151 * @lblk - logical number of the block in the file corresponding to @bh
2152 *
2153 * Walk through page buffers from @bh upto @head (exclusive) and either submit
2154 * the page for IO if all buffers in this page were mapped and there's no
2155 * accumulated extent of buffers to map or add buffers in the page to the
2156 * extent of buffers to map. The function returns 1 if the caller can continue
2157 * by processing the next page, 0 if it should stop adding buffers to the
2158 * extent to map because we cannot extend it anymore. It can also return value
2159 * < 0 in case of error during IO submission.
2160 */
2161static int mpage_process_page_bufs(struct mpage_da_data *mpd,
2162 struct buffer_head *head,
2163 struct buffer_head *bh,
2164 ext4_lblk_t lblk)
Jan Kara4e7ea812013-06-04 13:17:40 -04002165{
2166 struct inode *inode = mpd->inode;
Jan Kara5f1132b2013-08-17 10:02:33 -04002167 int err;
Fabian Frederick93407472017-02-27 14:28:32 -08002168 ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(inode) - 1)
Jan Kara4e7ea812013-06-04 13:17:40 -04002169 >> inode->i_blkbits;
2170
Eric Biggersc93d8f82019-07-22 09:26:24 -07002171 if (ext4_verity_in_progress(inode))
2172 blocks = EXT_MAX_BLOCKS;
2173
Jan Kara4e7ea812013-06-04 13:17:40 -04002174 do {
2175 BUG_ON(buffer_locked(bh));
2176
Jan Kara09930042013-08-17 09:57:56 -04002177 if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002178 /* Found extent to map? */
2179 if (mpd->map.m_len)
Jan Kara5f1132b2013-08-17 10:02:33 -04002180 return 0;
Jan Karadddbd6a2017-04-30 18:29:10 -04002181 /* Buffer needs mapping and handle is not started? */
2182 if (!mpd->do_map)
2183 return 0;
Jan Kara09930042013-08-17 09:57:56 -04002184 /* Everything mapped so far and we hit EOF */
Jan Kara5f1132b2013-08-17 10:02:33 -04002185 break;
Jan Kara4e7ea812013-06-04 13:17:40 -04002186 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002187 } while (lblk++, (bh = bh->b_this_page) != head);
Jan Kara5f1132b2013-08-17 10:02:33 -04002188 /* So far everything mapped? Submit the page for IO. */
2189 if (mpd->map.m_len == 0) {
2190 err = mpage_submit_page(mpd, head->b_page);
2191 if (err < 0)
2192 return err;
2193 }
2194 return lblk < blocks;
Jan Kara4e7ea812013-06-04 13:17:40 -04002195}
2196
2197/*
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302198 * mpage_process_page - update page buffers corresponding to changed extent and
2199 * may submit fully mapped page for IO
2200 *
2201 * @mpd - description of extent to map, on return next extent to map
2202 * @m_lblk - logical block mapping.
2203 * @m_pblk - corresponding physical mapping.
2204 * @map_bh - determines on return whether this page requires any further
2205 * mapping or not.
2206 * Scan given page buffers corresponding to changed extent and update buffer
2207 * state according to new extent state.
2208 * We map delalloc buffers to their physical location, clear unwritten bits.
2209 * If the given page is not fully mapped, we update @map to the next extent in
2210 * the given page that needs mapping & return @map_bh as true.
2211 */
2212static int mpage_process_page(struct mpage_da_data *mpd, struct page *page,
2213 ext4_lblk_t *m_lblk, ext4_fsblk_t *m_pblk,
2214 bool *map_bh)
2215{
2216 struct buffer_head *head, *bh;
2217 ext4_io_end_t *io_end = mpd->io_submit.io_end;
2218 ext4_lblk_t lblk = *m_lblk;
2219 ext4_fsblk_t pblock = *m_pblk;
2220 int err = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302221 int blkbits = mpd->inode->i_blkbits;
2222 ssize_t io_end_size = 0;
2223 struct ext4_io_end_vec *io_end_vec = ext4_last_io_end_vec(io_end);
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302224
2225 bh = head = page_buffers(page);
2226 do {
2227 if (lblk < mpd->map.m_lblk)
2228 continue;
2229 if (lblk >= mpd->map.m_lblk + mpd->map.m_len) {
2230 /*
2231 * Buffer after end of mapped extent.
2232 * Find next buffer in the page to map.
2233 */
2234 mpd->map.m_len = 0;
2235 mpd->map.m_flags = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302236 io_end_vec->size += io_end_size;
2237 io_end_size = 0;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302238
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302239 err = mpage_process_page_bufs(mpd, head, bh, lblk);
2240 if (err > 0)
2241 err = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302242 if (!err && mpd->map.m_len && mpd->map.m_lblk > lblk) {
2243 io_end_vec = ext4_alloc_io_end_vec(io_end);
Ritesh Harjani4d06bfb2019-11-06 15:08:09 +05302244 if (IS_ERR(io_end_vec)) {
2245 err = PTR_ERR(io_end_vec);
2246 goto out;
2247 }
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302248 io_end_vec->offset = mpd->map.m_lblk << blkbits;
2249 }
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302250 *map_bh = true;
2251 goto out;
2252 }
2253 if (buffer_delay(bh)) {
2254 clear_buffer_delay(bh);
2255 bh->b_blocknr = pblock++;
2256 }
2257 clear_buffer_unwritten(bh);
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302258 io_end_size += (1 << blkbits);
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302259 } while (lblk++, (bh = bh->b_this_page) != head);
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302260
2261 io_end_vec->size += io_end_size;
2262 io_end_size = 0;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302263 *map_bh = false;
2264out:
2265 *m_lblk = lblk;
2266 *m_pblk = pblock;
2267 return err;
2268}
2269
2270/*
Jan Kara4e7ea812013-06-04 13:17:40 -04002271 * mpage_map_buffers - update buffers corresponding to changed extent and
2272 * submit fully mapped pages for IO
2273 *
2274 * @mpd - description of extent to map, on return next extent to map
2275 *
2276 * Scan buffers corresponding to changed extent (we expect corresponding pages
2277 * to be already locked) and update buffer state according to new extent state.
2278 * We map delalloc buffers to their physical location, clear unwritten bits,
Lukas Czerner556615d2014-04-20 23:45:47 -04002279 * and mark buffers as uninit when we perform writes to unwritten extents
Jan Kara4e7ea812013-06-04 13:17:40 -04002280 * and do extent conversion after IO is finished. If the last page is not fully
2281 * mapped, we update @map to the next extent in the last page that needs
2282 * mapping. Otherwise we submit the page for IO.
2283 */
2284static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd)
2285{
2286 struct pagevec pvec;
2287 int nr_pages, i;
2288 struct inode *inode = mpd->inode;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002289 int bpp_bits = PAGE_SHIFT - inode->i_blkbits;
Jan Kara4e7ea812013-06-04 13:17:40 -04002290 pgoff_t start, end;
2291 ext4_lblk_t lblk;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302292 ext4_fsblk_t pblock;
Jan Kara4e7ea812013-06-04 13:17:40 -04002293 int err;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302294 bool map_bh = false;
Jan Kara4e7ea812013-06-04 13:17:40 -04002295
2296 start = mpd->map.m_lblk >> bpp_bits;
2297 end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits;
2298 lblk = start << bpp_bits;
2299 pblock = mpd->map.m_pblk;
2300
Mel Gorman86679822017-11-15 17:37:52 -08002301 pagevec_init(&pvec);
Jan Kara4e7ea812013-06-04 13:17:40 -04002302 while (start <= end) {
Jan Kara2b85a612017-09-06 16:21:30 -07002303 nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping,
Jan Kara397162f2017-09-06 16:21:43 -07002304 &start, end);
Jan Kara4e7ea812013-06-04 13:17:40 -04002305 if (nr_pages == 0)
2306 break;
2307 for (i = 0; i < nr_pages; i++) {
2308 struct page *page = pvec.pages[i];
2309
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302310 err = mpage_process_page(mpd, page, &lblk, &pblock,
2311 &map_bh);
Jan Kara4e7ea812013-06-04 13:17:40 -04002312 /*
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302313 * If map_bh is true, means page may require further bh
2314 * mapping, or maybe the page was submitted for IO.
2315 * So we return to call further extent mapping.
Jan Kara4e7ea812013-06-04 13:17:40 -04002316 */
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302317 if (err < 0 || map_bh == true)
2318 goto out;
Jan Kara4e7ea812013-06-04 13:17:40 -04002319 /* Page fully mapped - let IO run! */
2320 err = mpage_submit_page(mpd, page);
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302321 if (err < 0)
2322 goto out;
Jan Kara4e7ea812013-06-04 13:17:40 -04002323 }
2324 pagevec_release(&pvec);
2325 }
2326 /* Extent fully mapped and matches with page boundary. We are done. */
2327 mpd->map.m_len = 0;
2328 mpd->map.m_flags = 0;
2329 return 0;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302330out:
2331 pagevec_release(&pvec);
2332 return err;
Jan Kara4e7ea812013-06-04 13:17:40 -04002333}
2334
2335static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd)
2336{
2337 struct inode *inode = mpd->inode;
2338 struct ext4_map_blocks *map = &mpd->map;
2339 int get_blocks_flags;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002340 int err, dioread_nolock;
Jan Kara4e7ea812013-06-04 13:17:40 -04002341
2342 trace_ext4_da_write_pages_extent(inode, map);
2343 /*
2344 * Call ext4_map_blocks() to allocate any delayed allocation blocks, or
Lukas Czerner556615d2014-04-20 23:45:47 -04002345 * to convert an unwritten extent to be initialized (in the case
Jan Kara4e7ea812013-06-04 13:17:40 -04002346 * where we have written into one or more preallocated blocks). It is
2347 * possible that we're going to need more metadata blocks than
2348 * previously reserved. However we must not fail because we're in
2349 * writeback and there is nothing we can do about it so it might result
2350 * in data loss. So use reserved blocks to allocate metadata if
2351 * possible.
2352 *
Theodore Ts'o754cfed2014-09-04 18:08:22 -04002353 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if
2354 * the blocks in question are delalloc blocks. This indicates
2355 * that the blocks and quotas has already been checked when
2356 * the data was copied into the page cache.
Jan Kara4e7ea812013-06-04 13:17:40 -04002357 */
2358 get_blocks_flags = EXT4_GET_BLOCKS_CREATE |
Jan Karaee0876b2016-04-24 00:56:08 -04002359 EXT4_GET_BLOCKS_METADATA_NOFAIL |
2360 EXT4_GET_BLOCKS_IO_SUBMIT;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002361 dioread_nolock = ext4_should_dioread_nolock(inode);
2362 if (dioread_nolock)
Jan Kara4e7ea812013-06-04 13:17:40 -04002363 get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
2364 if (map->m_flags & (1 << BH_Delay))
2365 get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
2366
2367 err = ext4_map_blocks(handle, inode, map, get_blocks_flags);
2368 if (err < 0)
2369 return err;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002370 if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) {
Jan Kara6b523df2013-06-04 13:21:11 -04002371 if (!mpd->io_submit.io_end->handle &&
2372 ext4_handle_valid(handle)) {
2373 mpd->io_submit.io_end->handle = handle->h_rsv_handle;
2374 handle->h_rsv_handle = NULL;
2375 }
Jan Kara3613d222013-06-04 13:19:34 -04002376 ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end);
Jan Kara6b523df2013-06-04 13:21:11 -04002377 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002378
2379 BUG_ON(map->m_len == 0);
Jan Kara4e7ea812013-06-04 13:17:40 -04002380 return 0;
2381}
2382
2383/*
2384 * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length
2385 * mpd->len and submit pages underlying it for IO
2386 *
2387 * @handle - handle for journal operations
2388 * @mpd - extent to map
Jan Kara7534e852013-10-16 08:26:08 -04002389 * @give_up_on_write - we set this to true iff there is a fatal error and there
2390 * is no hope of writing the data. The caller should discard
2391 * dirty pages to avoid infinite loops.
Jan Kara4e7ea812013-06-04 13:17:40 -04002392 *
2393 * The function maps extent starting at mpd->lblk of length mpd->len. If it is
2394 * delayed, blocks are allocated, if it is unwritten, we may need to convert
2395 * them to initialized or split the described range from larger unwritten
2396 * extent. Note that we need not map all the described range since allocation
2397 * can return less blocks or the range is covered by more unwritten extents. We
2398 * cannot map more because we are limited by reserved transaction credits. On
2399 * the other hand we always make sure that the last touched page is fully
2400 * mapped so that it can be written out (and thus forward progress is
2401 * guaranteed). After mapping we submit all mapped pages for IO.
2402 */
2403static int mpage_map_and_submit_extent(handle_t *handle,
Theodore Ts'ocb530542013-07-01 08:12:40 -04002404 struct mpage_da_data *mpd,
2405 bool *give_up_on_write)
Jan Kara4e7ea812013-06-04 13:17:40 -04002406{
2407 struct inode *inode = mpd->inode;
2408 struct ext4_map_blocks *map = &mpd->map;
2409 int err;
2410 loff_t disksize;
Dmitry Monakhov66031202014-08-27 18:40:03 -04002411 int progress = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302412 ext4_io_end_t *io_end = mpd->io_submit.io_end;
Ritesh Harjani4d06bfb2019-11-06 15:08:09 +05302413 struct ext4_io_end_vec *io_end_vec;
Jan Kara4e7ea812013-06-04 13:17:40 -04002414
Ritesh Harjani4d06bfb2019-11-06 15:08:09 +05302415 io_end_vec = ext4_alloc_io_end_vec(io_end);
2416 if (IS_ERR(io_end_vec))
2417 return PTR_ERR(io_end_vec);
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302418 io_end_vec->offset = ((loff_t)map->m_lblk) << inode->i_blkbits;
Jan Kara27d7c4e2013-07-05 21:57:22 -04002419 do {
Jan Kara4e7ea812013-06-04 13:17:40 -04002420 err = mpage_map_one_extent(handle, mpd);
2421 if (err < 0) {
2422 struct super_block *sb = inode->i_sb;
2423
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002424 if (ext4_forced_shutdown(EXT4_SB(sb)) ||
2425 EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)
Theodore Ts'ocb530542013-07-01 08:12:40 -04002426 goto invalidate_dirty_pages;
Jan Kara4e7ea812013-06-04 13:17:40 -04002427 /*
Theodore Ts'ocb530542013-07-01 08:12:40 -04002428 * Let the uper layers retry transient errors.
2429 * In the case of ENOSPC, if ext4_count_free_blocks()
2430 * is non-zero, a commit should free up blocks.
Jan Kara4e7ea812013-06-04 13:17:40 -04002431 */
Theodore Ts'ocb530542013-07-01 08:12:40 -04002432 if ((err == -ENOMEM) ||
Dmitry Monakhov66031202014-08-27 18:40:03 -04002433 (err == -ENOSPC && ext4_count_free_clusters(sb))) {
2434 if (progress)
2435 goto update_disksize;
Theodore Ts'ocb530542013-07-01 08:12:40 -04002436 return err;
Dmitry Monakhov66031202014-08-27 18:40:03 -04002437 }
Theodore Ts'ocb530542013-07-01 08:12:40 -04002438 ext4_msg(sb, KERN_CRIT,
2439 "Delayed block allocation failed for "
2440 "inode %lu at logical offset %llu with"
2441 " max blocks %u with error %d",
2442 inode->i_ino,
2443 (unsigned long long)map->m_lblk,
2444 (unsigned)map->m_len, -err);
2445 ext4_msg(sb, KERN_CRIT,
2446 "This should not happen!! Data will "
2447 "be lost\n");
2448 if (err == -ENOSPC)
2449 ext4_print_free_blocks(inode);
2450 invalidate_dirty_pages:
2451 *give_up_on_write = true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002452 return err;
2453 }
Dmitry Monakhov66031202014-08-27 18:40:03 -04002454 progress = 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04002455 /*
2456 * Update buffer state, submit mapped pages, and get us new
2457 * extent to map
2458 */
2459 err = mpage_map_and_submit_buffers(mpd);
2460 if (err < 0)
Dmitry Monakhov66031202014-08-27 18:40:03 -04002461 goto update_disksize;
Jan Kara27d7c4e2013-07-05 21:57:22 -04002462 } while (map->m_len);
Jan Kara4e7ea812013-06-04 13:17:40 -04002463
Dmitry Monakhov66031202014-08-27 18:40:03 -04002464update_disksize:
Theodore Ts'o622cad12014-04-11 10:35:17 -04002465 /*
2466 * Update on-disk size after IO is submitted. Races with
2467 * truncate are avoided by checking i_size under i_data_sem.
2468 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002469 disksize = ((loff_t)mpd->first_page) << PAGE_SHIFT;
Jan Kara4e7ea812013-06-04 13:17:40 -04002470 if (disksize > EXT4_I(inode)->i_disksize) {
2471 int err2;
Theodore Ts'o622cad12014-04-11 10:35:17 -04002472 loff_t i_size;
Jan Kara4e7ea812013-06-04 13:17:40 -04002473
Theodore Ts'o622cad12014-04-11 10:35:17 -04002474 down_write(&EXT4_I(inode)->i_data_sem);
2475 i_size = i_size_read(inode);
2476 if (disksize > i_size)
2477 disksize = i_size;
2478 if (disksize > EXT4_I(inode)->i_disksize)
2479 EXT4_I(inode)->i_disksize = disksize;
Theodore Ts'o622cad12014-04-11 10:35:17 -04002480 up_write(&EXT4_I(inode)->i_data_sem);
Theodore Ts'ob907f2d2017-01-11 22:14:49 -05002481 err2 = ext4_mark_inode_dirty(handle, inode);
Theodore Ts'o878520a2019-11-19 21:54:15 -05002482 if (err2) {
2483 ext4_set_errno(inode->i_sb, -err2);
Jan Kara4e7ea812013-06-04 13:17:40 -04002484 ext4_error(inode->i_sb,
2485 "Failed to mark inode %lu dirty",
2486 inode->i_ino);
Theodore Ts'o878520a2019-11-19 21:54:15 -05002487 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002488 if (!err)
2489 err = err2;
2490 }
2491 return err;
2492}
2493
2494/*
Jan Karafffb2732013-06-04 13:01:11 -04002495 * Calculate the total number of credits to reserve for one writepages
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002496 * iteration. This is called from ext4_writepages(). We map an extent of
Anatol Pomozov70261f52013-08-28 14:40:12 -04002497 * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping
Jan Karafffb2732013-06-04 13:01:11 -04002498 * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN +
2499 * bpp - 1 blocks in bpp different extents.
2500 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002501static int ext4_da_writepages_trans_blocks(struct inode *inode)
2502{
Jan Karafffb2732013-06-04 13:01:11 -04002503 int bpp = ext4_journal_blocks_per_page(inode);
Mingming Cao525f4ed2008-08-19 22:15:58 -04002504
Jan Karafffb2732013-06-04 13:01:11 -04002505 return ext4_meta_trans_blocks(inode,
2506 MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp);
Mingming Cao525f4ed2008-08-19 22:15:58 -04002507}
Mingming Cao61628a32008-07-11 19:27:31 -04002508
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002509/*
Jan Kara4e7ea812013-06-04 13:17:40 -04002510 * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages
2511 * and underlying extent to map
2512 *
2513 * @mpd - where to look for pages
2514 *
2515 * Walk dirty pages in the mapping. If they are fully mapped, submit them for
2516 * IO immediately. When we find a page which isn't mapped we start accumulating
2517 * extent of buffers underlying these pages that needs mapping (formed by
2518 * either delayed or unwritten buffers). We also lock the pages containing
2519 * these buffers. The extent found is returned in @mpd structure (starting at
2520 * mpd->lblk with length mpd->len blocks).
2521 *
2522 * Note that this function can attach bios to one io_end structure which are
2523 * neither logically nor physically contiguous. Although it may seem as an
2524 * unnecessary complication, it is actually inevitable in blocksize < pagesize
2525 * case as we need to track IO to all buffers underlying a page in one io_end.
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002526 */
Jan Kara4e7ea812013-06-04 13:17:40 -04002527static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd)
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002528{
Jan Kara4e7ea812013-06-04 13:17:40 -04002529 struct address_space *mapping = mpd->inode->i_mapping;
2530 struct pagevec pvec;
2531 unsigned int nr_pages;
Ming Leiaeac5892013-10-17 18:56:16 -04002532 long left = mpd->wbc->nr_to_write;
Jan Kara4e7ea812013-06-04 13:17:40 -04002533 pgoff_t index = mpd->first_page;
2534 pgoff_t end = mpd->last_page;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05002535 xa_mark_t tag;
Jan Kara4e7ea812013-06-04 13:17:40 -04002536 int i, err = 0;
2537 int blkbits = mpd->inode->i_blkbits;
2538 ext4_lblk_t lblk;
2539 struct buffer_head *head;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002540
Jan Kara4e7ea812013-06-04 13:17:40 -04002541 if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages)
Eric Sandeen5b41d922010-10-27 21:30:13 -04002542 tag = PAGECACHE_TAG_TOWRITE;
2543 else
2544 tag = PAGECACHE_TAG_DIRTY;
2545
Mel Gorman86679822017-11-15 17:37:52 -08002546 pagevec_init(&pvec);
Jan Kara4e7ea812013-06-04 13:17:40 -04002547 mpd->map.m_len = 0;
2548 mpd->next_page = index;
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002549 while (index <= end) {
Jan Karadc7f3e82017-11-15 17:34:44 -08002550 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08002551 tag);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002552 if (nr_pages == 0)
Jan Kara4e7ea812013-06-04 13:17:40 -04002553 goto out;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002554
2555 for (i = 0; i < nr_pages; i++) {
2556 struct page *page = pvec.pages[i];
2557
2558 /*
Ming Leiaeac5892013-10-17 18:56:16 -04002559 * Accumulated enough dirty pages? This doesn't apply
2560 * to WB_SYNC_ALL mode. For integrity sync we have to
2561 * keep going because someone may be concurrently
2562 * dirtying pages, and we might have synced a lot of
2563 * newly appeared dirty pages, but have not synced all
2564 * of the old dirty pages.
2565 */
2566 if (mpd->wbc->sync_mode == WB_SYNC_NONE && left <= 0)
2567 goto out;
2568
Jan Kara4e7ea812013-06-04 13:17:40 -04002569 /* If we can't merge this page, we are done. */
2570 if (mpd->map.m_len > 0 && mpd->next_page != page->index)
2571 goto out;
Theodore Ts'o78aaced2011-02-26 14:09:14 -05002572
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002573 lock_page(page);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002574 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002575 * If the page is no longer dirty, or its mapping no
2576 * longer corresponds to inode we are writing (which
2577 * means it has been truncated or invalidated), or the
2578 * page is already under writeback and we are not doing
2579 * a data integrity writeback, skip the page
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002580 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002581 if (!PageDirty(page) ||
2582 (PageWriteback(page) &&
Jan Kara4e7ea812013-06-04 13:17:40 -04002583 (mpd->wbc->sync_mode == WB_SYNC_NONE)) ||
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002584 unlikely(page->mapping != mapping)) {
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002585 unlock_page(page);
2586 continue;
2587 }
2588
Darrick J. Wong7cb1a532011-05-18 13:53:20 -04002589 wait_on_page_writeback(page);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002590 BUG_ON(PageWriteback(page));
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002591
Jan Kara4e7ea812013-06-04 13:17:40 -04002592 if (mpd->map.m_len == 0)
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002593 mpd->first_page = page->index;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002594 mpd->next_page = page->index + 1;
Jan Karaf8bec372013-01-28 12:55:08 -05002595 /* Add all dirty buffers to mpd */
Jan Kara4e7ea812013-06-04 13:17:40 -04002596 lblk = ((ext4_lblk_t)page->index) <<
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002597 (PAGE_SHIFT - blkbits);
Jan Karaf8bec372013-01-28 12:55:08 -05002598 head = page_buffers(page);
Jan Kara5f1132b2013-08-17 10:02:33 -04002599 err = mpage_process_page_bufs(mpd, head, head, lblk);
2600 if (err <= 0)
Jan Kara4e7ea812013-06-04 13:17:40 -04002601 goto out;
Jan Kara5f1132b2013-08-17 10:02:33 -04002602 err = 0;
Ming Leiaeac5892013-10-17 18:56:16 -04002603 left--;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002604 }
2605 pagevec_release(&pvec);
2606 cond_resched();
2607 }
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002608 return 0;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002609out:
2610 pagevec_release(&pvec);
Jan Kara4e7ea812013-06-04 13:17:40 -04002611 return err;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002612}
2613
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002614static int ext4_writepages(struct address_space *mapping,
2615 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002616{
Jan Kara4e7ea812013-06-04 13:17:40 -04002617 pgoff_t writeback_index = 0;
2618 long nr_to_write = wbc->nr_to_write;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002619 int range_whole = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002620 int cycled = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002621 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002622 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002623 struct inode *inode = mapping->host;
Jan Kara6b523df2013-06-04 13:21:11 -04002624 int needed_blocks, rsv_blocks = 0, ret = 0;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002625 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Jan Kara4e7ea812013-06-04 13:17:40 -04002626 bool done;
Shaohua Li1bce63d12011-10-18 10:55:51 -04002627 struct blk_plug plug;
Theodore Ts'ocb530542013-07-01 08:12:40 -04002628 bool give_up_on_write = false;
Mingming Cao61628a32008-07-11 19:27:31 -04002629
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002630 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
2631 return -EIO;
2632
Daeho Jeongc8585c62016-04-25 23:22:35 -04002633 percpu_down_read(&sbi->s_journal_flag_rwsem);
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002634 trace_ext4_writepages(inode, wbc);
Theodore Ts'oba80b102009-01-03 20:03:21 -05002635
Mingming Cao61628a32008-07-11 19:27:31 -04002636 /*
2637 * No pages to write? This is mainly a kludge to avoid starting
2638 * a transaction for special inodes like journal inode on last iput()
2639 * because that could violate lock ordering on umount
2640 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002641 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Ming Leibbf023c72013-10-30 07:27:16 -04002642 goto out_writepages;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002643
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002644 if (ext4_should_journal_data(inode)) {
Goldwyn Rodrigues043d20d2018-03-26 01:32:50 -04002645 ret = generic_writepages(mapping, wbc);
Ming Leibbf023c72013-10-30 07:27:16 -04002646 goto out_writepages;
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002647 }
2648
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002649 /*
2650 * If the filesystem has aborted, it is read-only, so return
2651 * right away instead of dumping stack traces later on that
2652 * will obscure the real source of the problem. We test
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002653 * EXT4_MF_FS_ABORTED instead of sb->s_flag's SB_RDONLY because
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002654 * the latter could be true if the filesystem is mounted
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002655 * read-only, and in that case, ext4_writepages should
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002656 * *never* be called, so if that ever happens, we would want
2657 * the stack trace.
2658 */
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002659 if (unlikely(ext4_forced_shutdown(EXT4_SB(mapping->host->i_sb)) ||
2660 sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) {
Ming Leibbf023c72013-10-30 07:27:16 -04002661 ret = -EROFS;
2662 goto out_writepages;
2663 }
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002664
Jan Kara4e7ea812013-06-04 13:17:40 -04002665 /*
2666 * If we have inline data and arrive here, it means that
2667 * we will soon create the block for the 1st page, so
2668 * we'd better clear the inline data here.
2669 */
2670 if (ext4_has_inline_data(inode)) {
2671 /* Just inode will be modified... */
2672 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
2673 if (IS_ERR(handle)) {
2674 ret = PTR_ERR(handle);
2675 goto out_writepages;
2676 }
2677 BUG_ON(ext4_test_inode_state(inode,
2678 EXT4_STATE_MAY_INLINE_DATA));
2679 ext4_destroy_inline_data(handle, inode);
2680 ext4_journal_stop(handle);
2681 }
2682
yangerkun4e343232019-08-11 16:27:41 -04002683 if (ext4_should_dioread_nolock(inode)) {
2684 /*
2685 * We may need to convert up to one extent per block in
2686 * the page and we may dirty the inode.
2687 */
2688 rsv_blocks = 1 + ext4_chunk_trans_blocks(inode,
2689 PAGE_SIZE >> inode->i_blkbits);
2690 }
2691
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002692 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2693 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002694
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002695 if (wbc->range_cyclic) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002696 writeback_index = mapping->writeback_index;
2697 if (writeback_index)
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002698 cycled = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002699 mpd.first_page = writeback_index;
2700 mpd.last_page = -1;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002701 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002702 mpd.first_page = wbc->range_start >> PAGE_SHIFT;
2703 mpd.last_page = wbc->range_end >> PAGE_SHIFT;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002704 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002705
Jan Kara4e7ea812013-06-04 13:17:40 -04002706 mpd.inode = inode;
2707 mpd.wbc = wbc;
2708 ext4_io_submit_init(&mpd.io_submit, wbc);
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002709retry:
Wu Fengguang6e6938b2010-06-06 10:38:15 -06002710 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Jan Kara4e7ea812013-06-04 13:17:40 -04002711 tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page);
2712 done = false;
Shaohua Li1bce63d12011-10-18 10:55:51 -04002713 blk_start_plug(&plug);
Jan Karadddbd6a2017-04-30 18:29:10 -04002714
2715 /*
2716 * First writeback pages that don't need mapping - we can avoid
2717 * starting a transaction unnecessarily and also avoid being blocked
2718 * in the block layer on device congestion while having transaction
2719 * started.
2720 */
2721 mpd.do_map = 0;
2722 mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
2723 if (!mpd.io_submit.io_end) {
2724 ret = -ENOMEM;
2725 goto unplug;
2726 }
2727 ret = mpage_prepare_extent_to_map(&mpd);
Xiaoguang Wanga297b2f2019-02-10 23:53:21 -05002728 /* Unlock pages we didn't use */
2729 mpage_release_unused_pages(&mpd, false);
Jan Karadddbd6a2017-04-30 18:29:10 -04002730 /* Submit prepared bio */
2731 ext4_io_submit(&mpd.io_submit);
2732 ext4_put_io_end_defer(mpd.io_submit.io_end);
2733 mpd.io_submit.io_end = NULL;
Jan Karadddbd6a2017-04-30 18:29:10 -04002734 if (ret < 0)
2735 goto unplug;
2736
Jan Kara4e7ea812013-06-04 13:17:40 -04002737 while (!done && mpd.first_page <= mpd.last_page) {
2738 /* For each extent of pages we use new io_end */
2739 mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
2740 if (!mpd.io_submit.io_end) {
2741 ret = -ENOMEM;
2742 break;
2743 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002744
2745 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002746 * We have two constraints: We find one extent to map and we
2747 * must always write out whole page (makes a difference when
2748 * blocksize < pagesize) so that we don't block on IO when we
2749 * try to write out the rest of the page. Journalled mode is
2750 * not supported by delalloc.
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002751 */
2752 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04002753 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002754
Jan Kara4e7ea812013-06-04 13:17:40 -04002755 /* start a new transaction */
Jan Kara6b523df2013-06-04 13:21:11 -04002756 handle = ext4_journal_start_with_reserve(inode,
2757 EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks);
Mingming Cao61628a32008-07-11 19:27:31 -04002758 if (IS_ERR(handle)) {
2759 ret = PTR_ERR(handle);
Theodore Ts'o16939182009-09-26 17:43:59 -04002760 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
Curt Wohlgemuthfbe845d2010-05-16 13:00:00 -04002761 "%ld pages, ino %lu; err %d", __func__,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002762 wbc->nr_to_write, inode->i_ino, ret);
Jan Kara4e7ea812013-06-04 13:17:40 -04002763 /* Release allocated io_end */
2764 ext4_put_io_end(mpd.io_submit.io_end);
Jan Karadddbd6a2017-04-30 18:29:10 -04002765 mpd.io_submit.io_end = NULL;
Jan Kara4e7ea812013-06-04 13:17:40 -04002766 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002767 }
Jan Karadddbd6a2017-04-30 18:29:10 -04002768 mpd.do_map = 1;
Theodore Ts'of63e60052009-02-23 16:42:39 -05002769
Jan Kara4e7ea812013-06-04 13:17:40 -04002770 trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc);
2771 ret = mpage_prepare_extent_to_map(&mpd);
2772 if (!ret) {
2773 if (mpd.map.m_len)
Theodore Ts'ocb530542013-07-01 08:12:40 -04002774 ret = mpage_map_and_submit_extent(handle, &mpd,
2775 &give_up_on_write);
Jan Kara4e7ea812013-06-04 13:17:40 -04002776 else {
2777 /*
2778 * We scanned the whole range (or exhausted
2779 * nr_to_write), submitted what was mapped and
2780 * didn't find anything needing mapping. We are
2781 * done.
2782 */
2783 done = true;
2784 }
Theodore Ts'of63e60052009-02-23 16:42:39 -05002785 }
Jan Kara646caa92016-07-04 10:14:01 -04002786 /*
2787 * Caution: If the handle is synchronous,
2788 * ext4_journal_stop() can wait for transaction commit
2789 * to finish which may depend on writeback of pages to
2790 * complete or on page lock to be released. In that
2791 * case, we have to wait until after after we have
2792 * submitted all the IO, released page locks we hold,
2793 * and dropped io_end reference (for extent conversion
2794 * to be able to complete) before stopping the handle.
2795 */
2796 if (!ext4_handle_valid(handle) || handle->h_sync == 0) {
2797 ext4_journal_stop(handle);
2798 handle = NULL;
Jan Karadddbd6a2017-04-30 18:29:10 -04002799 mpd.do_map = 0;
Jan Kara646caa92016-07-04 10:14:01 -04002800 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002801 /* Unlock pages we didn't use */
Theodore Ts'ocb530542013-07-01 08:12:40 -04002802 mpage_release_unused_pages(&mpd, give_up_on_write);
Xiaoguang Wanga297b2f2019-02-10 23:53:21 -05002803 /* Submit prepared bio */
2804 ext4_io_submit(&mpd.io_submit);
2805
Jan Kara646caa92016-07-04 10:14:01 -04002806 /*
2807 * Drop our io_end reference we got from init. We have
2808 * to be careful and use deferred io_end finishing if
2809 * we are still holding the transaction as we can
2810 * release the last reference to io_end which may end
2811 * up doing unwritten extent conversion.
2812 */
2813 if (handle) {
2814 ext4_put_io_end_defer(mpd.io_submit.io_end);
2815 ext4_journal_stop(handle);
2816 } else
2817 ext4_put_io_end(mpd.io_submit.io_end);
Jan Karadddbd6a2017-04-30 18:29:10 -04002818 mpd.io_submit.io_end = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002819
Jan Kara4e7ea812013-06-04 13:17:40 -04002820 if (ret == -ENOSPC && sbi->s_journal) {
2821 /*
2822 * Commit the transaction which would
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002823 * free blocks released in the transaction
2824 * and try again
2825 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002826 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002827 ret = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002828 continue;
2829 }
2830 /* Fatal error - ENOMEM, EIO... */
2831 if (ret)
Mingming Cao61628a32008-07-11 19:27:31 -04002832 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002833 }
Jan Karadddbd6a2017-04-30 18:29:10 -04002834unplug:
Shaohua Li1bce63d12011-10-18 10:55:51 -04002835 blk_finish_plug(&plug);
Jan Kara9c12a832013-09-16 08:24:26 -04002836 if (!ret && !cycled && wbc->nr_to_write > 0) {
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002837 cycled = 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04002838 mpd.last_page = writeback_index - 1;
2839 mpd.first_page = 0;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002840 goto retry;
2841 }
Mingming Cao61628a32008-07-11 19:27:31 -04002842
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002843 /* Update index */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002844 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2845 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002846 * Set the writeback_index so that range_cyclic
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002847 * mode will write it back later
2848 */
Jan Kara4e7ea812013-06-04 13:17:40 -04002849 mapping->writeback_index = mpd.first_page;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002850
Mingming Cao61628a32008-07-11 19:27:31 -04002851out_writepages:
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002852 trace_ext4_writepages_result(inode, wbc, ret,
2853 nr_to_write - wbc->nr_to_write);
Daeho Jeongc8585c62016-04-25 23:22:35 -04002854 percpu_up_read(&sbi->s_journal_flag_rwsem);
Mingming Cao61628a32008-07-11 19:27:31 -04002855 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002856}
2857
Dan Williams5f0663b2017-12-21 12:25:11 -08002858static int ext4_dax_writepages(struct address_space *mapping,
2859 struct writeback_control *wbc)
2860{
2861 int ret;
2862 long nr_to_write = wbc->nr_to_write;
2863 struct inode *inode = mapping->host;
2864 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
2865
2866 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
2867 return -EIO;
2868
2869 percpu_down_read(&sbi->s_journal_flag_rwsem);
2870 trace_ext4_writepages(inode, wbc);
2871
2872 ret = dax_writeback_mapping_range(mapping, inode->i_sb->s_bdev, wbc);
2873 trace_ext4_writepages_result(inode, wbc, ret,
2874 nr_to_write - wbc->nr_to_write);
2875 percpu_up_read(&sbi->s_journal_flag_rwsem);
2876 return ret;
2877}
2878
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002879static int ext4_nonda_switch(struct super_block *sb)
2880{
Eric Whitney5c1ff332013-04-09 09:27:31 -04002881 s64 free_clusters, dirty_clusters;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002882 struct ext4_sb_info *sbi = EXT4_SB(sb);
2883
2884 /*
2885 * switch to non delalloc mode if we are running low
2886 * on free block. The free block accounting via percpu
Eric Dumazet179f7eb2009-01-06 14:41:04 -08002887 * counters can get slightly wrong with percpu_counter_batch getting
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002888 * accumulated on each CPU without updating global counters
2889 * Delalloc need an accurate free block accounting. So switch
2890 * to non delalloc when we are near to error range.
2891 */
Eric Whitney5c1ff332013-04-09 09:27:31 -04002892 free_clusters =
2893 percpu_counter_read_positive(&sbi->s_freeclusters_counter);
2894 dirty_clusters =
2895 percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
Theodore Ts'o00d4e732012-09-19 22:42:36 -04002896 /*
2897 * Start pushing delalloc when 1/2 of free blocks are dirty.
2898 */
Eric Whitney5c1ff332013-04-09 09:27:31 -04002899 if (dirty_clusters && (free_clusters < 2 * dirty_clusters))
Miao Xie10ee27a2013-01-10 13:47:57 +08002900 try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE);
Theodore Ts'o00d4e732012-09-19 22:42:36 -04002901
Eric Whitney5c1ff332013-04-09 09:27:31 -04002902 if (2 * free_clusters < 3 * dirty_clusters ||
2903 free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) {
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002904 /*
Eric Sandeenc8afb442009-12-23 07:58:12 -05002905 * free block count is less than 150% of dirty blocks
2906 * or free blocks is less than watermark
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002907 */
2908 return 1;
2909 }
2910 return 0;
2911}
2912
Eric Sandeen0ff89472014-10-11 19:51:17 -04002913/* We always reserve for an inode update; the superblock could be there too */
2914static int ext4_da_write_credits(struct inode *inode, loff_t pos, unsigned len)
2915{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002916 if (likely(ext4_has_feature_large_file(inode->i_sb)))
Eric Sandeen0ff89472014-10-11 19:51:17 -04002917 return 1;
2918
2919 if (pos + len <= 0x7fffffffULL)
2920 return 1;
2921
2922 /* We might need to update the superblock to set LARGE_FILE */
2923 return 2;
2924}
2925
Alex Tomas64769242008-07-11 19:27:31 -04002926static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002927 loff_t pos, unsigned len, unsigned flags,
2928 struct page **pagep, void **fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04002929{
Eric Sandeen72b8ab92010-05-16 11:00:00 -04002930 int ret, retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002931 struct page *page;
2932 pgoff_t index;
Alex Tomas64769242008-07-11 19:27:31 -04002933 struct inode *inode = mapping->host;
2934 handle_t *handle;
2935
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002936 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
2937 return -EIO;
2938
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002939 index = pos >> PAGE_SHIFT;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002940
Eric Biggersc93d8f82019-07-22 09:26:24 -07002941 if (ext4_nonda_switch(inode->i_sb) || S_ISLNK(inode->i_mode) ||
2942 ext4_verity_in_progress(inode)) {
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002943 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
2944 return ext4_write_begin(file, mapping, pos,
2945 len, flags, pagep, fsdata);
2946 }
2947 *fsdata = (void *)0;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002948 trace_ext4_da_write_begin(inode, pos, len, flags);
Tao Ma9c3569b2012-12-10 14:05:57 -05002949
2950 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2951 ret = ext4_da_write_inline_data_begin(mapping, inode,
2952 pos, len, flags,
2953 pagep, fsdata);
2954 if (ret < 0)
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002955 return ret;
2956 if (ret == 1)
2957 return 0;
Tao Ma9c3569b2012-12-10 14:05:57 -05002958 }
2959
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002960 /*
2961 * grab_cache_page_write_begin() can take a long time if the
2962 * system is thrashing due to memory pressure, or if the page
2963 * is being written back. So grab it first before we start
2964 * the transaction handle. This also allows us to allocate
2965 * the page (if needed) without using GFP_NOFS.
2966 */
2967retry_grab:
2968 page = grab_cache_page_write_begin(mapping, index, flags);
2969 if (!page)
2970 return -ENOMEM;
2971 unlock_page(page);
2972
Alex Tomas64769242008-07-11 19:27:31 -04002973 /*
2974 * With delayed allocation, we don't log the i_disksize update
2975 * if there is delayed block allocation. But we still need
2976 * to journalling the i_disksize update if writes to the end
2977 * of file which has an already mapped buffer.
2978 */
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002979retry_journal:
Eric Sandeen0ff89472014-10-11 19:51:17 -04002980 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
2981 ext4_da_write_credits(inode, pos, len));
Alex Tomas64769242008-07-11 19:27:31 -04002982 if (IS_ERR(handle)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002983 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002984 return PTR_ERR(handle);
Alex Tomas64769242008-07-11 19:27:31 -04002985 }
2986
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002987 lock_page(page);
2988 if (page->mapping != mapping) {
2989 /* The page got truncated from under us */
2990 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002991 put_page(page);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002992 ext4_journal_stop(handle);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002993 goto retry_grab;
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002994 }
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002995 /* In case writeback began while the page was unlocked */
Dmitry Monakhov7afe5aa2013-08-28 14:30:47 -04002996 wait_for_stable_page(page);
Alex Tomas64769242008-07-11 19:27:31 -04002997
Chandan Rajendra643fa962018-12-12 15:20:12 +05302998#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrow2058f832015-04-12 00:55:10 -04002999 ret = ext4_block_write_begin(page, pos, len,
3000 ext4_da_get_block_prep);
3001#else
Christoph Hellwig6e1db882010-06-04 11:29:57 +02003002 ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
Michael Halcrow2058f832015-04-12 00:55:10 -04003003#endif
Alex Tomas64769242008-07-11 19:27:31 -04003004 if (ret < 0) {
3005 unlock_page(page);
3006 ext4_journal_stop(handle);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04003007 /*
3008 * block_write_begin may have instantiated a few blocks
3009 * outside i_size. Trim these off again. Don't need
3010 * i_size_read because we hold i_mutex.
3011 */
3012 if (pos + len > inode->i_size)
Jan Karab9a42072009-12-08 21:24:33 -05003013 ext4_truncate_failed_write(inode);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05003014
3015 if (ret == -ENOSPC &&
3016 ext4_should_retry_alloc(inode->i_sb, &retries))
3017 goto retry_journal;
3018
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003019 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05003020 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04003021 }
3022
Theodore Ts'o47564bf2013-02-09 09:24:14 -05003023 *pagep = page;
Alex Tomas64769242008-07-11 19:27:31 -04003024 return ret;
3025}
3026
Mingming Cao632eaea2008-07-11 19:27:31 -04003027/*
3028 * Check if we should update i_disksize
3029 * when write to the end of file but not require block allocation
3030 */
3031static int ext4_da_should_update_i_disksize(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003032 unsigned long offset)
Mingming Cao632eaea2008-07-11 19:27:31 -04003033{
3034 struct buffer_head *bh;
3035 struct inode *inode = page->mapping->host;
3036 unsigned int idx;
3037 int i;
3038
3039 bh = page_buffers(page);
3040 idx = offset >> inode->i_blkbits;
3041
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003042 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04003043 bh = bh->b_this_page;
3044
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04003045 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
Mingming Cao632eaea2008-07-11 19:27:31 -04003046 return 0;
3047 return 1;
3048}
3049
Alex Tomas64769242008-07-11 19:27:31 -04003050static int ext4_da_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003051 struct address_space *mapping,
3052 loff_t pos, unsigned len, unsigned copied,
3053 struct page *page, void *fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04003054{
3055 struct inode *inode = mapping->host;
3056 int ret = 0, ret2;
3057 handle_t *handle = ext4_journal_current_handle();
3058 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04003059 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003060 int write_mode = (int)(unsigned long)fsdata;
3061
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003062 if (write_mode == FALL_BACK_TO_NONDELALLOC)
3063 return ext4_write_end(file, mapping, pos,
3064 len, copied, page, fsdata);
Mingming Cao632eaea2008-07-11 19:27:31 -04003065
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003066 trace_ext4_da_write_end(inode, pos, len, copied);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003067 start = pos & (PAGE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003068 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04003069
3070 /*
3071 * generic_write_end() will run mark_inode_dirty() if i_size
3072 * changes. So let's piggyback the i_disksize mark_inode_dirty
3073 * into that.
3074 */
Alex Tomas64769242008-07-11 19:27:31 -04003075 new_i_size = pos + copied;
Andrea Arcangeliea51d132011-12-13 21:41:15 -05003076 if (copied && new_i_size > EXT4_I(inode)->i_disksize) {
Tao Ma9c3569b2012-12-10 14:05:57 -05003077 if (ext4_has_inline_data(inode) ||
3078 ext4_da_should_update_i_disksize(page, end)) {
Dmitry Monakhovee124d22014-08-30 23:34:06 -04003079 ext4_update_i_disksize(inode, new_i_size);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003080 /* We need to mark inode dirty even if
3081 * new_i_size is less that inode->i_size
3082 * bu greater than i_disksize.(hint delalloc)
3083 */
3084 ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04003085 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003086 }
Tao Ma9c3569b2012-12-10 14:05:57 -05003087
3088 if (write_mode != CONVERT_INLINE_DATA &&
3089 ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
3090 ext4_has_inline_data(inode))
3091 ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied,
3092 page);
3093 else
3094 ret2 = generic_write_end(file, mapping, pos, len, copied,
Alex Tomas64769242008-07-11 19:27:31 -04003095 page, fsdata);
Tao Ma9c3569b2012-12-10 14:05:57 -05003096
Alex Tomas64769242008-07-11 19:27:31 -04003097 copied = ret2;
3098 if (ret2 < 0)
3099 ret = ret2;
3100 ret2 = ext4_journal_stop(handle);
3101 if (!ret)
3102 ret = ret2;
3103
3104 return ret ? ret : copied;
3105}
3106
Theodore Ts'occd25062009-02-26 01:04:07 -05003107/*
3108 * Force all delayed allocation blocks to be allocated for a given inode.
3109 */
3110int ext4_alloc_da_blocks(struct inode *inode)
3111{
Theodore Ts'ofb40ba02009-09-16 19:30:40 -04003112 trace_ext4_alloc_da_blocks(inode);
3113
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04003114 if (!EXT4_I(inode)->i_reserved_data_blocks)
Theodore Ts'occd25062009-02-26 01:04:07 -05003115 return 0;
3116
3117 /*
3118 * We do something simple for now. The filemap_flush() will
3119 * also start triggering a write of the data blocks, which is
3120 * not strictly speaking necessary (and for users of
3121 * laptop_mode, not even desirable). However, to do otherwise
3122 * would require replicating code paths in:
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003123 *
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003124 * ext4_writepages() ->
Theodore Ts'occd25062009-02-26 01:04:07 -05003125 * write_cache_pages() ---> (via passed in callback function)
3126 * __mpage_da_writepage() -->
3127 * mpage_add_bh_to_extent()
3128 * mpage_da_map_blocks()
3129 *
3130 * The problem is that write_cache_pages(), located in
3131 * mm/page-writeback.c, marks pages clean in preparation for
3132 * doing I/O, which is not desirable if we're not planning on
3133 * doing I/O at all.
3134 *
3135 * We could call write_cache_pages(), and then redirty all of
Wu Fengguang380cf092010-11-11 19:23:29 +08003136 * the pages by calling redirty_page_for_writepage() but that
Theodore Ts'occd25062009-02-26 01:04:07 -05003137 * would be ugly in the extreme. So instead we would need to
3138 * replicate parts of the code in the above functions,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003139 * simplifying them because we wouldn't actually intend to
Theodore Ts'occd25062009-02-26 01:04:07 -05003140 * write out the pages, but rather only collect contiguous
3141 * logical block extents, call the multi-block allocator, and
3142 * then update the buffer heads with the block allocations.
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003143 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003144 * For now, though, we'll cheat by calling filemap_flush(),
3145 * which will map the blocks, and start the I/O, but not
3146 * actually wait for the I/O to complete.
3147 */
3148 return filemap_flush(inode->i_mapping);
3149}
Alex Tomas64769242008-07-11 19:27:31 -04003150
3151/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003152 * bmap() is special. It gets used by applications such as lilo and by
3153 * the swapper to find the on-disk block of a specific piece of data.
3154 *
3155 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07003156 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003157 * filesystem and enables swap, then they may get a nasty shock when the
3158 * data getting swapped to that swapfile suddenly gets overwritten by
3159 * the original zero's written out previously to the journal and
3160 * awaiting writeback in the kernel's buffer cache.
3161 *
3162 * So, if we see any bmap calls here on a modified, data-journaled file,
3163 * take extra steps to flush any blocks which might be in the cache.
3164 */
Mingming Cao617ba132006-10-11 01:20:53 -07003165static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003166{
3167 struct inode *inode = mapping->host;
3168 journal_t *journal;
3169 int err;
3170
Tao Ma46c7f252012-12-10 14:04:52 -05003171 /*
3172 * We can get here for an inline file via the FIBMAP ioctl
3173 */
3174 if (ext4_has_inline_data(inode))
3175 return 0;
3176
Alex Tomas64769242008-07-11 19:27:31 -04003177 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
3178 test_opt(inode->i_sb, DELALLOC)) {
3179 /*
3180 * With delalloc we want to sync the file
3181 * so that we can make sure we allocate
3182 * blocks for file
3183 */
3184 filemap_write_and_wait(mapping);
3185 }
3186
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003187 if (EXT4_JOURNAL(inode) &&
3188 ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003189 /*
3190 * This is a REALLY heavyweight approach, but the use of
3191 * bmap on dirty files is expected to be extremely rare:
3192 * only if we run lilo or swapon on a freshly made file
3193 * do we expect this to happen.
3194 *
3195 * (bmap requires CAP_SYS_RAWIO so this does not
3196 * represent an unprivileged user DOS attack --- we'd be
3197 * in trouble if mortal users could trigger this path at
3198 * will.)
3199 *
Mingming Cao617ba132006-10-11 01:20:53 -07003200 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003201 * regular files. If somebody wants to bmap a directory
3202 * or symlink and gets confused because the buffer
3203 * hasn't yet been flushed to disk, they deserve
3204 * everything they get.
3205 */
3206
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003207 ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
Mingming Cao617ba132006-10-11 01:20:53 -07003208 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07003209 jbd2_journal_lock_updates(journal);
3210 err = jbd2_journal_flush(journal);
3211 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003212
3213 if (err)
3214 return 0;
3215 }
3216
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003217 return generic_block_bmap(mapping, block, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003218}
3219
Mingming Cao617ba132006-10-11 01:20:53 -07003220static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003221{
Tao Ma46c7f252012-12-10 14:04:52 -05003222 int ret = -EAGAIN;
3223 struct inode *inode = page->mapping->host;
3224
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003225 trace_ext4_readpage(page);
Tao Ma46c7f252012-12-10 14:04:52 -05003226
3227 if (ext4_has_inline_data(inode))
3228 ret = ext4_readpage_inline(inode, page);
3229
3230 if (ret == -EAGAIN)
Jens Axboeac22b462018-08-17 15:45:42 -07003231 return ext4_mpage_readpages(page->mapping, NULL, page, 1,
3232 false);
Tao Ma46c7f252012-12-10 14:04:52 -05003233
3234 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003235}
3236
3237static int
Mingming Cao617ba132006-10-11 01:20:53 -07003238ext4_readpages(struct file *file, struct address_space *mapping,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003239 struct list_head *pages, unsigned nr_pages)
3240{
Tao Ma46c7f252012-12-10 14:04:52 -05003241 struct inode *inode = mapping->host;
3242
3243 /* If the file has inline data, no need to do readpages. */
3244 if (ext4_has_inline_data(inode))
3245 return 0;
3246
Jens Axboeac22b462018-08-17 15:45:42 -07003247 return ext4_mpage_readpages(mapping, pages, NULL, nr_pages, true);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003248}
3249
Lukas Czernerd47992f2013-05-21 23:17:23 -04003250static void ext4_invalidatepage(struct page *page, unsigned int offset,
3251 unsigned int length)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003252{
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003253 trace_ext4_invalidatepage(page, offset, length);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003254
Jan Kara4520fb32012-12-25 13:28:54 -05003255 /* No journalling happens on data buffers when this function is used */
3256 WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page)));
3257
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003258 block_invalidatepage(page, offset, length);
Jan Kara4520fb32012-12-25 13:28:54 -05003259}
3260
Jan Kara53e87262012-12-25 13:29:52 -05003261static int __ext4_journalled_invalidatepage(struct page *page,
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003262 unsigned int offset,
3263 unsigned int length)
Jan Kara4520fb32012-12-25 13:28:54 -05003264{
3265 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
3266
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003267 trace_ext4_journalled_invalidatepage(page, offset, length);
Jan Kara4520fb32012-12-25 13:28:54 -05003268
Jiaying Zhang744692d2010-03-04 16:14:02 -05003269 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003270 * If it's a full truncate we just forget about the pending dirtying
3271 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003272 if (offset == 0 && length == PAGE_SIZE)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003273 ClearPageChecked(page);
3274
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003275 return jbd2_journal_invalidatepage(journal, page, offset, length);
Jan Kara53e87262012-12-25 13:29:52 -05003276}
3277
3278/* Wrapper for aops... */
3279static void ext4_journalled_invalidatepage(struct page *page,
Lukas Czernerd47992f2013-05-21 23:17:23 -04003280 unsigned int offset,
3281 unsigned int length)
Jan Kara53e87262012-12-25 13:29:52 -05003282{
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003283 WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003284}
3285
Mingming Cao617ba132006-10-11 01:20:53 -07003286static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003287{
Mingming Cao617ba132006-10-11 01:20:53 -07003288 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003289
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003290 trace_ext4_releasepage(page);
3291
Jan Karae1c36592013-03-10 22:19:00 -04003292 /* Page has dirty journalled data -> cannot release */
3293 if (PageChecked(page))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003294 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05003295 if (journal)
3296 return jbd2_journal_try_to_free_buffers(journal, page, wait);
3297 else
3298 return try_to_free_buffers(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003299}
3300
Jan Karab8a61762017-11-01 16:36:45 +01003301static bool ext4_inode_datasync_dirty(struct inode *inode)
3302{
3303 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
3304
3305 if (journal)
3306 return !jbd2_transaction_committed(journal,
3307 EXT4_I(inode)->i_datasync_tid);
3308 /* Any metadata buffers to write? */
3309 if (!list_empty(&inode->i_mapping->private_list))
3310 return true;
3311 return inode->i_state & I_DIRTY_DATASYNC;
3312}
3313
Matthew Bobrowskic8fdfe292019-11-05 22:59:56 +11003314static void ext4_set_iomap(struct inode *inode, struct iomap *iomap,
3315 struct ext4_map_blocks *map, loff_t offset,
3316 loff_t length)
Jan Kara364443c2016-11-20 17:36:06 -05003317{
Matthew Bobrowskic8fdfe292019-11-05 22:59:56 +11003318 u8 blkbits = inode->i_blkbits;
3319
3320 /*
3321 * Writes that span EOF might trigger an I/O size update on completion,
3322 * so consider them to be dirty for the purpose of O_DSYNC, even if
3323 * there is no other metadata changes being made or are pending.
3324 */
3325 iomap->flags = 0;
3326 if (ext4_inode_datasync_dirty(inode) ||
3327 offset + length > i_size_read(inode))
3328 iomap->flags |= IOMAP_F_DIRTY;
3329
3330 if (map->m_flags & EXT4_MAP_NEW)
3331 iomap->flags |= IOMAP_F_NEW;
3332
3333 iomap->bdev = inode->i_sb->s_bdev;
3334 iomap->dax_dev = EXT4_SB(inode->i_sb)->s_daxdev;
3335 iomap->offset = (u64) map->m_lblk << blkbits;
3336 iomap->length = (u64) map->m_len << blkbits;
3337
3338 /*
3339 * Flags passed to ext4_map_blocks() for direct I/O writes can result
3340 * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits
3341 * set. In order for any allocated unwritten extents to be converted
3342 * into written extents correctly within the ->end_io() handler, we
3343 * need to ensure that the iomap->type is set appropriately. Hence, the
3344 * reason why we need to check whether the EXT4_MAP_UNWRITTEN bit has
3345 * been set first.
3346 */
3347 if (map->m_flags & EXT4_MAP_UNWRITTEN) {
3348 iomap->type = IOMAP_UNWRITTEN;
3349 iomap->addr = (u64) map->m_pblk << blkbits;
3350 } else if (map->m_flags & EXT4_MAP_MAPPED) {
3351 iomap->type = IOMAP_MAPPED;
3352 iomap->addr = (u64) map->m_pblk << blkbits;
3353 } else {
3354 iomap->type = IOMAP_HOLE;
3355 iomap->addr = IOMAP_NULL_ADDR;
3356 }
3357}
3358
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003359static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map,
3360 unsigned int flags)
3361{
3362 handle_t *handle;
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003363 u8 blkbits = inode->i_blkbits;
3364 int ret, dio_credits, m_flags = 0, retries = 0;
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003365
3366 /*
3367 * Trim the mapping request to the maximum value that we can map at
3368 * once for direct I/O.
3369 */
3370 if (map->m_len > DIO_MAX_BLOCKS)
3371 map->m_len = DIO_MAX_BLOCKS;
3372 dio_credits = ext4_chunk_trans_blocks(inode, map->m_len);
3373
3374retry:
3375 /*
3376 * Either we allocate blocks and then don't get an unwritten extent, so
3377 * in that case we have reserved enough credits. Or, the blocks are
3378 * already allocated and unwritten. In that case, the extent conversion
3379 * fits into the credits as well.
3380 */
3381 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits);
3382 if (IS_ERR(handle))
3383 return PTR_ERR(handle);
3384
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003385 /*
3386 * DAX and direct I/O are the only two operations that are currently
3387 * supported with IOMAP_WRITE.
3388 */
3389 WARN_ON(!IS_DAX(inode) && !(flags & IOMAP_DIRECT));
3390 if (IS_DAX(inode))
3391 m_flags = EXT4_GET_BLOCKS_CREATE_ZERO;
3392 /*
3393 * We use i_size instead of i_disksize here because delalloc writeback
3394 * can complete at any point during the I/O and subsequently push the
3395 * i_disksize out to i_size. This could be beyond where direct I/O is
3396 * happening and thus expose allocated blocks to direct I/O reads.
3397 */
3398 else if ((map->m_lblk * (1 << blkbits)) >= i_size_read(inode))
3399 m_flags = EXT4_GET_BLOCKS_CREATE;
3400 else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3401 m_flags = EXT4_GET_BLOCKS_IO_CREATE_EXT;
3402
3403 ret = ext4_map_blocks(handle, inode, map, m_flags);
3404
3405 /*
3406 * We cannot fill holes in indirect tree based inodes as that could
3407 * expose stale data in the case of a crash. Use the magic error code
3408 * to fallback to buffered I/O.
3409 */
3410 if (!m_flags && !ret)
3411 ret = -ENOTBLK;
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003412
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003413 ext4_journal_stop(handle);
3414 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3415 goto retry;
3416
3417 return ret;
3418}
3419
3420
Jan Kara364443c2016-11-20 17:36:06 -05003421static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -07003422 unsigned flags, struct iomap *iomap, struct iomap *srcmap)
Jan Kara364443c2016-11-20 17:36:06 -05003423{
Jan Kara364443c2016-11-20 17:36:06 -05003424 int ret;
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003425 struct ext4_map_blocks map;
3426 u8 blkbits = inode->i_blkbits;
Jan Kara364443c2016-11-20 17:36:06 -05003427
Theodore Ts'obcd8e912018-09-01 12:45:04 -04003428 if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
3429 return -EINVAL;
Andreas Gruenbacher7046ae32017-10-01 17:57:54 -04003430
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003431 if (WARN_ON_ONCE(ext4_has_inline_data(inode)))
3432 return -ERANGE;
Jan Kara364443c2016-11-20 17:36:06 -05003433
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003434 /*
3435 * Calculate the first and last logical blocks respectively.
3436 */
3437 map.m_lblk = offset >> blkbits;
3438 map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits,
3439 EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1;
Jan Kara364443c2016-11-20 17:36:06 -05003440
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003441 if (flags & IOMAP_WRITE)
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003442 ret = ext4_iomap_alloc(inode, &map, flags);
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003443 else
Jan Kara776722e2016-11-20 18:09:11 -05003444 ret = ext4_map_blocks(NULL, inode, &map, 0);
Christoph Hellwig545052e2017-10-01 17:58:54 -04003445
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003446 if (ret < 0)
3447 return ret;
Christoph Hellwig545052e2017-10-01 17:58:54 -04003448
Matthew Bobrowskic8fdfe292019-11-05 22:59:56 +11003449 ext4_set_iomap(inode, iomap, &map, offset, length);
Christoph Hellwig545052e2017-10-01 17:58:54 -04003450
Jan Kara364443c2016-11-20 17:36:06 -05003451 return 0;
3452}
3453
Jan Kara8cd115b2019-12-18 18:44:33 +01003454static int ext4_iomap_overwrite_begin(struct inode *inode, loff_t offset,
3455 loff_t length, unsigned flags, struct iomap *iomap,
3456 struct iomap *srcmap)
3457{
3458 int ret;
3459
3460 /*
3461 * Even for writes we don't need to allocate blocks, so just pretend
3462 * we are reading to save overhead of starting a transaction.
3463 */
3464 flags &= ~IOMAP_WRITE;
3465 ret = ext4_iomap_begin(inode, offset, length, flags, iomap, srcmap);
3466 WARN_ON_ONCE(iomap->type != IOMAP_MAPPED);
3467 return ret;
3468}
3469
Jan Kara776722e2016-11-20 18:09:11 -05003470static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length,
3471 ssize_t written, unsigned flags, struct iomap *iomap)
3472{
Jan Kara776722e2016-11-20 18:09:11 -05003473 /*
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003474 * Check to see whether an error occurred while writing out the data to
3475 * the allocated blocks. If so, return the magic error code so that we
3476 * fallback to buffered I/O and attempt to complete the remainder of
3477 * the I/O. Any blocks that may have been allocated in preparation for
3478 * the direct I/O will be reused during buffered I/O.
Jan Kara776722e2016-11-20 18:09:11 -05003479 */
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003480 if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0)
3481 return -ENOTBLK;
Jan Kara776722e2016-11-20 18:09:11 -05003482
Matthew Bobrowski569342d2019-11-05 23:01:51 +11003483 return 0;
Jan Kara776722e2016-11-20 18:09:11 -05003484}
3485
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08003486const struct iomap_ops ext4_iomap_ops = {
Jan Kara364443c2016-11-20 17:36:06 -05003487 .iomap_begin = ext4_iomap_begin,
Jan Kara776722e2016-11-20 18:09:11 -05003488 .iomap_end = ext4_iomap_end,
Jan Kara364443c2016-11-20 17:36:06 -05003489};
3490
Jan Kara8cd115b2019-12-18 18:44:33 +01003491const struct iomap_ops ext4_iomap_overwrite_ops = {
3492 .iomap_begin = ext4_iomap_overwrite_begin,
3493 .iomap_end = ext4_iomap_end,
3494};
3495
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003496static bool ext4_iomap_is_delalloc(struct inode *inode,
3497 struct ext4_map_blocks *map)
Mingming Cao4c0425f2009-09-28 15:48:41 -04003498{
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003499 struct extent_status es;
3500 ext4_lblk_t offset = 0, end = map->m_lblk + map->m_len - 1;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003501
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003502 ext4_es_find_extent_range(inode, &ext4_es_is_delayed,
3503 map->m_lblk, end, &es);
Mingming4b70df12009-11-03 14:44:54 -05003504
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003505 if (!es.es_len || es.es_lblk > end)
3506 return false;
3507
3508 if (es.es_lblk > map->m_lblk) {
3509 map->m_len = es.es_lblk - map->m_lblk;
3510 return false;
3511 }
3512
3513 offset = map->m_lblk - es.es_lblk;
3514 map->m_len = es.es_len - offset;
3515
3516 return true;
3517}
3518
3519static int ext4_iomap_begin_report(struct inode *inode, loff_t offset,
3520 loff_t length, unsigned int flags,
3521 struct iomap *iomap, struct iomap *srcmap)
3522{
3523 int ret;
3524 bool delalloc = false;
3525 struct ext4_map_blocks map;
3526 u8 blkbits = inode->i_blkbits;
3527
3528 if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
3529 return -EINVAL;
3530
3531 if (ext4_has_inline_data(inode)) {
3532 ret = ext4_inline_data_iomap(inode, iomap);
3533 if (ret != -EAGAIN) {
3534 if (ret == 0 && offset >= iomap->length)
3535 ret = -ENOENT;
3536 return ret;
3537 }
3538 }
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003539
Jan Kara74c66bc2016-02-29 08:36:38 +11003540 /*
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003541 * Calculate the first and last logical block respectively.
Jan Kara74c66bc2016-02-29 08:36:38 +11003542 */
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003543 map.m_lblk = offset >> blkbits;
3544 map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits,
3545 EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1;
3546
3547 ret = ext4_map_blocks(NULL, inode, &map, 0);
3548 if (ret < 0)
3549 return ret;
3550 if (ret == 0)
3551 delalloc = ext4_iomap_is_delalloc(inode, &map);
3552
3553 ext4_set_iomap(inode, iomap, &map, offset, length);
3554 if (delalloc && iomap->type == IOMAP_HOLE)
3555 iomap->type = IOMAP_DELALLOC;
Christoph Hellwig187372a2016-02-08 14:40:51 +11003556
3557 return 0;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003558}
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003559
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003560const struct iomap_ops ext4_iomap_report_ops = {
3561 .iomap_begin = ext4_iomap_begin_report,
3562};
Mingming Cao4c0425f2009-09-28 15:48:41 -04003563
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003564/*
Mingming Cao617ba132006-10-11 01:20:53 -07003565 * Pages can be marked dirty completely asynchronously from ext4's journalling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003566 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3567 * much here because ->set_page_dirty is called under VFS locks. The page is
3568 * not necessarily locked.
3569 *
3570 * We cannot just dirty the page and leave attached buffers clean, because the
3571 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3572 * or jbddirty because all the journalling code will explode.
3573 *
3574 * So what we do is to mark the page "pending dirty" and next time writepage
3575 * is called, propagate that into the buffers appropriately.
3576 */
Mingming Cao617ba132006-10-11 01:20:53 -07003577static int ext4_journalled_set_page_dirty(struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003578{
3579 SetPageChecked(page);
3580 return __set_page_dirty_nobuffers(page);
3581}
3582
Jan Kara6dcc6932016-12-01 11:46:40 -05003583static int ext4_set_page_dirty(struct page *page)
3584{
3585 WARN_ON_ONCE(!PageLocked(page) && !PageDirty(page));
3586 WARN_ON_ONCE(!page_has_buffers(page));
3587 return __set_page_dirty_buffers(page);
3588}
3589
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003590static const struct address_space_operations ext4_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003591 .readpage = ext4_readpage,
3592 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003593 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003594 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003595 .write_begin = ext4_write_begin,
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003596 .write_end = ext4_write_end,
Jan Kara6dcc6932016-12-01 11:46:40 -05003597 .set_page_dirty = ext4_set_page_dirty,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003598 .bmap = ext4_bmap,
3599 .invalidatepage = ext4_invalidatepage,
3600 .releasepage = ext4_releasepage,
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003601 .direct_IO = noop_direct_IO,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003602 .migratepage = buffer_migrate_page,
3603 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003604 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003605};
3606
Mingming Cao617ba132006-10-11 01:20:53 -07003607static const struct address_space_operations ext4_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003608 .readpage = ext4_readpage,
3609 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003610 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003611 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003612 .write_begin = ext4_write_begin,
3613 .write_end = ext4_journalled_write_end,
3614 .set_page_dirty = ext4_journalled_set_page_dirty,
3615 .bmap = ext4_bmap,
Jan Kara4520fb32012-12-25 13:28:54 -05003616 .invalidatepage = ext4_journalled_invalidatepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003617 .releasepage = ext4_releasepage,
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003618 .direct_IO = noop_direct_IO,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003619 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003620 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003621};
3622
Alex Tomas64769242008-07-11 19:27:31 -04003623static const struct address_space_operations ext4_da_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003624 .readpage = ext4_readpage,
3625 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003626 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003627 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003628 .write_begin = ext4_da_write_begin,
3629 .write_end = ext4_da_write_end,
Jan Kara6dcc6932016-12-01 11:46:40 -05003630 .set_page_dirty = ext4_set_page_dirty,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003631 .bmap = ext4_bmap,
Eric Whitney8fcc3a52019-08-22 23:22:14 -04003632 .invalidatepage = ext4_invalidatepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003633 .releasepage = ext4_releasepage,
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003634 .direct_IO = noop_direct_IO,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003635 .migratepage = buffer_migrate_page,
3636 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003637 .error_remove_page = generic_error_remove_page,
Alex Tomas64769242008-07-11 19:27:31 -04003638};
3639
Dan Williams5f0663b2017-12-21 12:25:11 -08003640static const struct address_space_operations ext4_dax_aops = {
3641 .writepages = ext4_dax_writepages,
3642 .direct_IO = noop_direct_IO,
3643 .set_page_dirty = noop_set_page_dirty,
Toshi Kani94dbb632018-09-15 21:23:41 -04003644 .bmap = ext4_bmap,
Dan Williams5f0663b2017-12-21 12:25:11 -08003645 .invalidatepage = noop_invalidatepage,
3646};
3647
Mingming Cao617ba132006-10-11 01:20:53 -07003648void ext4_set_aops(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003649{
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003650 switch (ext4_inode_journal_mode(inode)) {
3651 case EXT4_INODE_ORDERED_DATA_MODE:
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003652 case EXT4_INODE_WRITEBACK_DATA_MODE:
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003653 break;
3654 case EXT4_INODE_JOURNAL_DATA_MODE:
Mingming Cao617ba132006-10-11 01:20:53 -07003655 inode->i_mapping->a_ops = &ext4_journalled_aops;
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003656 return;
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003657 default:
3658 BUG();
3659 }
Dan Williams5f0663b2017-12-21 12:25:11 -08003660 if (IS_DAX(inode))
3661 inode->i_mapping->a_ops = &ext4_dax_aops;
3662 else if (test_opt(inode->i_sb, DELALLOC))
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003663 inode->i_mapping->a_ops = &ext4_da_aops;
3664 else
3665 inode->i_mapping->a_ops = &ext4_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003666}
3667
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003668static int __ext4_block_zero_page_range(handle_t *handle,
Lukas Czernerd863dc32013-05-27 23:32:35 -04003669 struct address_space *mapping, loff_t from, loff_t length)
3670{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003671 ext4_fsblk_t index = from >> PAGE_SHIFT;
3672 unsigned offset = from & (PAGE_SIZE-1);
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003673 unsigned blocksize, pos;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003674 ext4_lblk_t iblock;
3675 struct inode *inode = mapping->host;
3676 struct buffer_head *bh;
3677 struct page *page;
3678 int err = 0;
3679
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003680 page = find_or_create_page(mapping, from >> PAGE_SHIFT,
Michal Hockoc62d2552015-11-06 16:28:49 -08003681 mapping_gfp_constraint(mapping, ~__GFP_FS));
Lukas Czernerd863dc32013-05-27 23:32:35 -04003682 if (!page)
3683 return -ENOMEM;
3684
3685 blocksize = inode->i_sb->s_blocksize;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003686
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003687 iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003688
3689 if (!page_has_buffers(page))
3690 create_empty_buffers(page, blocksize, 0);
3691
3692 /* Find the buffer that contains "offset" */
3693 bh = page_buffers(page);
3694 pos = blocksize;
3695 while (offset >= pos) {
3696 bh = bh->b_this_page;
3697 iblock++;
3698 pos += blocksize;
3699 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003700 if (buffer_freed(bh)) {
3701 BUFFER_TRACE(bh, "freed: skip");
3702 goto unlock;
3703 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003704 if (!buffer_mapped(bh)) {
3705 BUFFER_TRACE(bh, "unmapped");
3706 ext4_get_block(inode, iblock, bh, 0);
3707 /* unmapped? It's a hole - nothing to do */
3708 if (!buffer_mapped(bh)) {
3709 BUFFER_TRACE(bh, "still unmapped");
3710 goto unlock;
3711 }
3712 }
3713
3714 /* Ok, it's mapped. Make sure it's up-to-date */
3715 if (PageUptodate(page))
3716 set_buffer_uptodate(bh);
3717
3718 if (!buffer_uptodate(bh)) {
3719 err = -EIO;
Mike Christiedfec8a12016-06-05 14:31:44 -05003720 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003721 wait_on_buffer(bh);
3722 /* Uhhuh. Read error. Complain and punt. */
3723 if (!buffer_uptodate(bh))
3724 goto unlock;
Chandan Rajendra592ddec2018-12-12 15:20:10 +05303725 if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode)) {
Michael Halcrowc9c74292015-04-12 00:56:10 -04003726 /* We expect the key to be set. */
Jaegeuk Kima7550b32016-07-10 14:01:03 -04003727 BUG_ON(!fscrypt_has_encryption_key(inode));
Eric Biggers834f1562019-12-26 09:41:05 -06003728 err = fscrypt_decrypt_pagecache_blocks(page, blocksize,
3729 bh_offset(bh));
3730 if (err) {
3731 clear_buffer_uptodate(bh);
3732 goto unlock;
3733 }
Michael Halcrowc9c74292015-04-12 00:56:10 -04003734 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003735 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003736 if (ext4_should_journal_data(inode)) {
3737 BUFFER_TRACE(bh, "get write access");
3738 err = ext4_journal_get_write_access(handle, bh);
3739 if (err)
3740 goto unlock;
3741 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003742 zero_user(page, offset, length);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003743 BUFFER_TRACE(bh, "zeroed end of block");
3744
Lukas Czernerd863dc32013-05-27 23:32:35 -04003745 if (ext4_should_journal_data(inode)) {
3746 err = ext4_handle_dirty_metadata(handle, inode, bh);
Lukas Czerner0713ed02013-05-27 23:32:35 -04003747 } else {
jon ernst353eefd2013-07-01 08:12:39 -04003748 err = 0;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003749 mark_buffer_dirty(bh);
Jan Kara3957ef52016-04-24 00:56:05 -04003750 if (ext4_should_order_data(inode))
Ross Zwisler73131fb2019-06-20 17:26:26 -04003751 err = ext4_jbd2_inode_add_write(handle, inode, from,
3752 length);
Lukas Czerner0713ed02013-05-27 23:32:35 -04003753 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003754
3755unlock:
3756 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003757 put_page(page);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003758 return err;
3759}
3760
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003761/*
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003762 * ext4_block_zero_page_range() zeros out a mapping of length 'length'
3763 * starting from file offset 'from'. The range to be zero'd must
3764 * be contained with in one block. If the specified range exceeds
3765 * the end of the block it will be shortened to end of the block
3766 * that cooresponds to 'from'
3767 */
3768static int ext4_block_zero_page_range(handle_t *handle,
3769 struct address_space *mapping, loff_t from, loff_t length)
3770{
3771 struct inode *inode = mapping->host;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003772 unsigned offset = from & (PAGE_SIZE-1);
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003773 unsigned blocksize = inode->i_sb->s_blocksize;
3774 unsigned max = blocksize - (offset & (blocksize - 1));
3775
3776 /*
3777 * correct length if it does not fall between
3778 * 'from' and the end of the block
3779 */
3780 if (length > max || length < 0)
3781 length = max;
3782
Jan Kara47e69352016-11-20 18:08:05 -05003783 if (IS_DAX(inode)) {
3784 return iomap_zero_range(inode, from, length, NULL,
3785 &ext4_iomap_ops);
3786 }
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003787 return __ext4_block_zero_page_range(handle, mapping, from, length);
3788}
3789
3790/*
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003791 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
3792 * up to the end of the block which corresponds to `from'.
3793 * This required during truncate. We need to physically zero the tail end
3794 * of that block so it doesn't yield old data if the file is later grown.
3795 */
Stephen Hemmingerc1978552014-05-12 10:50:23 -04003796static int ext4_block_truncate_page(handle_t *handle,
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003797 struct address_space *mapping, loff_t from)
3798{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003799 unsigned offset = from & (PAGE_SIZE-1);
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003800 unsigned length;
3801 unsigned blocksize;
3802 struct inode *inode = mapping->host;
3803
Theodore Ts'o0d068632017-02-14 11:31:15 -05003804 /* If we are processing an encrypted inode during orphan list handling */
Chandan Rajendra592ddec2018-12-12 15:20:10 +05303805 if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode))
Theodore Ts'o0d068632017-02-14 11:31:15 -05003806 return 0;
3807
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003808 blocksize = inode->i_sb->s_blocksize;
3809 length = blocksize - (offset & (blocksize - 1));
3810
3811 return ext4_block_zero_page_range(handle, mapping, from, length);
3812}
3813
Lukas Czernera87dd182013-05-27 23:32:35 -04003814int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
3815 loff_t lstart, loff_t length)
3816{
3817 struct super_block *sb = inode->i_sb;
3818 struct address_space *mapping = inode->i_mapping;
Lukas Czernere1be3a92013-07-01 08:12:39 -04003819 unsigned partial_start, partial_end;
Lukas Czernera87dd182013-05-27 23:32:35 -04003820 ext4_fsblk_t start, end;
3821 loff_t byte_end = (lstart + length - 1);
3822 int err = 0;
3823
Lukas Czernere1be3a92013-07-01 08:12:39 -04003824 partial_start = lstart & (sb->s_blocksize - 1);
3825 partial_end = byte_end & (sb->s_blocksize - 1);
3826
Lukas Czernera87dd182013-05-27 23:32:35 -04003827 start = lstart >> sb->s_blocksize_bits;
3828 end = byte_end >> sb->s_blocksize_bits;
3829
3830 /* Handle partial zero within the single block */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003831 if (start == end &&
3832 (partial_start || (partial_end != sb->s_blocksize - 1))) {
Lukas Czernera87dd182013-05-27 23:32:35 -04003833 err = ext4_block_zero_page_range(handle, mapping,
3834 lstart, length);
3835 return err;
3836 }
3837 /* Handle partial zero out on the start of the range */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003838 if (partial_start) {
Lukas Czernera87dd182013-05-27 23:32:35 -04003839 err = ext4_block_zero_page_range(handle, mapping,
3840 lstart, sb->s_blocksize);
3841 if (err)
3842 return err;
3843 }
3844 /* Handle partial zero out on the end of the range */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003845 if (partial_end != sb->s_blocksize - 1)
Lukas Czernera87dd182013-05-27 23:32:35 -04003846 err = ext4_block_zero_page_range(handle, mapping,
Lukas Czernere1be3a92013-07-01 08:12:39 -04003847 byte_end - partial_end,
3848 partial_end + 1);
Lukas Czernera87dd182013-05-27 23:32:35 -04003849 return err;
3850}
3851
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003852int ext4_can_truncate(struct inode *inode)
3853{
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003854 if (S_ISREG(inode->i_mode))
3855 return 1;
3856 if (S_ISDIR(inode->i_mode))
3857 return 1;
3858 if (S_ISLNK(inode->i_mode))
3859 return !ext4_inode_is_fast_symlink(inode);
3860 return 0;
3861}
3862
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003863/*
Jan Kara01127842015-12-07 14:34:49 -05003864 * We have to make sure i_disksize gets properly updated before we truncate
3865 * page cache due to hole punching or zero range. Otherwise i_disksize update
3866 * can get lost as it may have been postponed to submission of writeback but
3867 * that will never happen after we truncate page cache.
3868 */
3869int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset,
3870 loff_t len)
3871{
3872 handle_t *handle;
3873 loff_t size = i_size_read(inode);
3874
Al Viro59551022016-01-22 15:40:57 -05003875 WARN_ON(!inode_is_locked(inode));
Jan Kara01127842015-12-07 14:34:49 -05003876 if (offset > size || offset + len < size)
3877 return 0;
3878
3879 if (EXT4_I(inode)->i_disksize >= size)
3880 return 0;
3881
3882 handle = ext4_journal_start(inode, EXT4_HT_MISC, 1);
3883 if (IS_ERR(handle))
3884 return PTR_ERR(handle);
3885 ext4_update_i_disksize(inode, size);
3886 ext4_mark_inode_dirty(handle, inode);
3887 ext4_journal_stop(handle);
3888
3889 return 0;
3890}
3891
Ross Zwislerb1f38212018-09-11 13:31:16 -04003892static void ext4_wait_dax_page(struct ext4_inode_info *ei)
Ross Zwisler430657b2018-07-29 17:00:22 -04003893{
Ross Zwisler430657b2018-07-29 17:00:22 -04003894 up_write(&ei->i_mmap_sem);
3895 schedule();
3896 down_write(&ei->i_mmap_sem);
3897}
3898
3899int ext4_break_layouts(struct inode *inode)
3900{
3901 struct ext4_inode_info *ei = EXT4_I(inode);
3902 struct page *page;
Ross Zwisler430657b2018-07-29 17:00:22 -04003903 int error;
3904
3905 if (WARN_ON_ONCE(!rwsem_is_locked(&ei->i_mmap_sem)))
3906 return -EINVAL;
3907
3908 do {
Ross Zwisler430657b2018-07-29 17:00:22 -04003909 page = dax_layout_busy_page(inode->i_mapping);
3910 if (!page)
3911 return 0;
3912
3913 error = ___wait_var_event(&page->_refcount,
3914 atomic_read(&page->_refcount) == 1,
3915 TASK_INTERRUPTIBLE, 0, 0,
Ross Zwislerb1f38212018-09-11 13:31:16 -04003916 ext4_wait_dax_page(ei));
3917 } while (error == 0);
Ross Zwisler430657b2018-07-29 17:00:22 -04003918
3919 return error;
3920}
3921
Jan Kara01127842015-12-07 14:34:49 -05003922/*
Ross Zwislercca32b72016-09-22 11:49:38 -04003923 * ext4_punch_hole: punches a hole in a file by releasing the blocks
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003924 * associated with the given offset and length
3925 *
3926 * @inode: File inode
3927 * @offset: The offset where the hole will begin
3928 * @len: The length of the hole
3929 *
Anatol Pomozov4907cb72012-09-01 10:31:09 -07003930 * Returns: 0 on success or negative on failure
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003931 */
3932
Ashish Sangwanaeb28172013-07-01 08:12:38 -04003933int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length)
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003934{
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003935 struct super_block *sb = inode->i_sb;
3936 ext4_lblk_t first_block, stop_block;
3937 struct address_space *mapping = inode->i_mapping;
Lukas Czernera87dd182013-05-27 23:32:35 -04003938 loff_t first_block_offset, last_block_offset;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003939 handle_t *handle;
3940 unsigned int credits;
3941 int ret = 0;
3942
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003943 if (!S_ISREG(inode->i_mode))
Allison Henderson73355192012-03-21 22:23:31 -04003944 return -EOPNOTSUPP;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003945
Lukas Czernerb8a86842014-03-18 18:05:35 -04003946 trace_ext4_punch_hole(inode, offset, length, 0);
Zheng Liuaaddea82013-01-16 20:21:26 -05003947
Theodore Ts'oc1e82202019-08-23 22:38:00 -04003948 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
3949 if (ext4_has_inline_data(inode)) {
3950 down_write(&EXT4_I(inode)->i_mmap_sem);
3951 ret = ext4_convert_inline_data(inode);
3952 up_write(&EXT4_I(inode)->i_mmap_sem);
3953 if (ret)
3954 return ret;
3955 }
3956
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003957 /*
3958 * Write out all dirty pages to avoid race conditions
3959 * Then release them.
3960 */
Ross Zwislercca32b72016-09-22 11:49:38 -04003961 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003962 ret = filemap_write_and_wait_range(mapping, offset,
3963 offset + length - 1);
3964 if (ret)
3965 return ret;
3966 }
3967
Al Viro59551022016-01-22 15:40:57 -05003968 inode_lock(inode);
Lukas Czerner9ef06ce2014-04-12 09:47:00 -04003969
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003970 /* No need to punch hole beyond i_size */
3971 if (offset >= inode->i_size)
3972 goto out_mutex;
3973
3974 /*
3975 * If the hole extends beyond i_size, set the hole
3976 * to end after the page that contains i_size
3977 */
3978 if (offset + length > inode->i_size) {
3979 length = inode->i_size +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003980 PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) -
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003981 offset;
3982 }
3983
Jan Karaa3612932013-08-16 21:19:41 -04003984 if (offset & (sb->s_blocksize - 1) ||
3985 (offset + length) & (sb->s_blocksize - 1)) {
3986 /*
3987 * Attach jinode to inode for jbd2 if we do any zeroing of
3988 * partial block
3989 */
3990 ret = ext4_inode_attach_jinode(inode);
3991 if (ret < 0)
3992 goto out_mutex;
3993
3994 }
3995
Jan Karaea3d7202015-12-07 14:28:03 -05003996 /* Wait all existing dio workers, newcomers will block on i_mutex */
Jan Karaea3d7202015-12-07 14:28:03 -05003997 inode_dio_wait(inode);
3998
3999 /*
4000 * Prevent page faults from reinstantiating pages we have released from
4001 * page cache.
4002 */
4003 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04004004
4005 ret = ext4_break_layouts(inode);
4006 if (ret)
4007 goto out_dio;
4008
Lukas Czernera87dd182013-05-27 23:32:35 -04004009 first_block_offset = round_up(offset, sb->s_blocksize);
4010 last_block_offset = round_down((offset + length), sb->s_blocksize) - 1;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004011
Lukas Czernera87dd182013-05-27 23:32:35 -04004012 /* Now release the pages and zero block aligned part of pages*/
Jan Kara01127842015-12-07 14:34:49 -05004013 if (last_block_offset > first_block_offset) {
4014 ret = ext4_update_disksize_before_punch(inode, offset, length);
4015 if (ret)
4016 goto out_dio;
Lukas Czernera87dd182013-05-27 23:32:35 -04004017 truncate_pagecache_range(inode, first_block_offset,
4018 last_block_offset);
Jan Kara01127842015-12-07 14:34:49 -05004019 }
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004020
4021 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4022 credits = ext4_writepage_trans_blocks(inode);
4023 else
4024 credits = ext4_blocks_for_truncate(inode);
4025 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
4026 if (IS_ERR(handle)) {
4027 ret = PTR_ERR(handle);
4028 ext4_std_error(sb, ret);
4029 goto out_dio;
4030 }
4031
Lukas Czernera87dd182013-05-27 23:32:35 -04004032 ret = ext4_zero_partial_blocks(handle, inode, offset,
4033 length);
4034 if (ret)
4035 goto out_stop;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004036
4037 first_block = (offset + sb->s_blocksize - 1) >>
4038 EXT4_BLOCK_SIZE_BITS(sb);
4039 stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
4040
Lukas Czernereee597a2018-05-13 19:28:35 -04004041 /* If there are blocks to remove, do it */
4042 if (stop_block > first_block) {
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004043
Lukas Czernereee597a2018-05-13 19:28:35 -04004044 down_write(&EXT4_I(inode)->i_data_sem);
4045 ext4_discard_preallocations(inode);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004046
Lukas Czernereee597a2018-05-13 19:28:35 -04004047 ret = ext4_es_remove_extent(inode, first_block,
4048 stop_block - first_block);
4049 if (ret) {
4050 up_write(&EXT4_I(inode)->i_data_sem);
4051 goto out_stop;
4052 }
4053
4054 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4055 ret = ext4_ext_remove_space(inode, first_block,
4056 stop_block - 1);
4057 else
4058 ret = ext4_ind_remove_space(handle, inode, first_block,
4059 stop_block);
4060
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004061 up_write(&EXT4_I(inode)->i_data_sem);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004062 }
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004063 if (IS_SYNC(inode))
4064 ext4_handle_sync(handle);
Maxim Patlasove251f9b2014-02-20 16:58:05 -05004065
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004066 inode->i_mtime = inode->i_ctime = current_time(inode);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004067 ext4_mark_inode_dirty(handle, inode);
Jan Kara67a7d5f2017-05-29 13:24:55 -04004068 if (ret >= 0)
4069 ext4_update_inode_fsync_trans(handle, inode, 1);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004070out_stop:
4071 ext4_journal_stop(handle);
4072out_dio:
Jan Karaea3d7202015-12-07 14:28:03 -05004073 up_write(&EXT4_I(inode)->i_mmap_sem);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004074out_mutex:
Al Viro59551022016-01-22 15:40:57 -05004075 inode_unlock(inode);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004076 return ret;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004077}
4078
Jan Karaa3612932013-08-16 21:19:41 -04004079int ext4_inode_attach_jinode(struct inode *inode)
4080{
4081 struct ext4_inode_info *ei = EXT4_I(inode);
4082 struct jbd2_inode *jinode;
4083
4084 if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal)
4085 return 0;
4086
4087 jinode = jbd2_alloc_inode(GFP_KERNEL);
4088 spin_lock(&inode->i_lock);
4089 if (!ei->jinode) {
4090 if (!jinode) {
4091 spin_unlock(&inode->i_lock);
4092 return -ENOMEM;
4093 }
4094 ei->jinode = jinode;
4095 jbd2_journal_init_jbd_inode(ei->jinode, inode);
4096 jinode = NULL;
4097 }
4098 spin_unlock(&inode->i_lock);
4099 if (unlikely(jinode != NULL))
4100 jbd2_free_inode(jinode);
4101 return 0;
4102}
4103
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004104/*
Mingming Cao617ba132006-10-11 01:20:53 -07004105 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004106 *
Mingming Cao617ba132006-10-11 01:20:53 -07004107 * We block out ext4_get_block() block instantiations across the entire
4108 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004109 * simultaneously on behalf of the same inode.
4110 *
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08004111 * As we work through the truncate and commit bits of it to the journal there
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004112 * is one core, guiding principle: the file's tree must always be consistent on
4113 * disk. We must be able to restart the truncate after a crash.
4114 *
4115 * The file's tree may be transiently inconsistent in memory (although it
4116 * probably isn't), but whenever we close off and commit a journal transaction,
4117 * the contents of (the filesystem + the journal) must be consistent and
4118 * restartable. It's pretty simple, really: bottom up, right to left (although
4119 * left-to-right works OK too).
4120 *
4121 * Note that at recovery time, journal replay occurs *before* the restart of
4122 * truncate against the orphan inode list.
4123 *
4124 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07004125 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004126 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07004127 * ext4_truncate() to have another go. So there will be instantiated blocks
4128 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004129 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07004130 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004131 */
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004132int ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004133{
Theodore Ts'o819c4922013-04-03 12:47:17 -04004134 struct ext4_inode_info *ei = EXT4_I(inode);
4135 unsigned int credits;
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004136 int err = 0;
Theodore Ts'o819c4922013-04-03 12:47:17 -04004137 handle_t *handle;
4138 struct address_space *mapping = inode->i_mapping;
Theodore Ts'o819c4922013-04-03 12:47:17 -04004139
Theodore Ts'o19b5ef62013-04-03 21:58:52 -04004140 /*
4141 * There is a possibility that we're either freeing the inode
Matthew Wilcoxe04027e2014-03-24 15:15:07 -04004142 * or it's a completely new inode. In those cases we might not
Theodore Ts'o19b5ef62013-04-03 21:58:52 -04004143 * have i_mutex locked because it's not necessary.
4144 */
4145 if (!(inode->i_state & (I_NEW|I_FREEING)))
Al Viro59551022016-01-22 15:40:57 -05004146 WARN_ON(!inode_is_locked(inode));
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004147 trace_ext4_truncate_enter(inode);
4148
Duane Griffin91ef4ca2008-07-11 19:27:31 -04004149 if (!ext4_can_truncate(inode))
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004150 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004151
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004152 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004153
Theodore Ts'o5534fb52009-09-17 09:34:16 -04004154 if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004155 ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
Theodore Ts'o7d8f9f72009-02-24 08:21:14 -05004156
Tao Maaef1c852012-12-10 14:06:02 -05004157 if (ext4_has_inline_data(inode)) {
4158 int has_inline = 1;
4159
Theodore Ts'o01daf942017-01-22 19:35:49 -05004160 err = ext4_inline_data_truncate(inode, &has_inline);
4161 if (err)
4162 return err;
Tao Maaef1c852012-12-10 14:06:02 -05004163 if (has_inline)
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004164 return 0;
Tao Maaef1c852012-12-10 14:06:02 -05004165 }
4166
Jan Karaa3612932013-08-16 21:19:41 -04004167 /* If we zero-out tail of the page, we have to create jinode for jbd2 */
4168 if (inode->i_size & (inode->i_sb->s_blocksize - 1)) {
4169 if (ext4_inode_attach_jinode(inode) < 0)
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004170 return 0;
Jan Karaa3612932013-08-16 21:19:41 -04004171 }
4172
Amir Goldsteinff9893d2011-06-27 16:36:31 -04004173 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Theodore Ts'o819c4922013-04-03 12:47:17 -04004174 credits = ext4_writepage_trans_blocks(inode);
Amir Goldsteinff9893d2011-06-27 16:36:31 -04004175 else
Theodore Ts'o819c4922013-04-03 12:47:17 -04004176 credits = ext4_blocks_for_truncate(inode);
4177
4178 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004179 if (IS_ERR(handle))
4180 return PTR_ERR(handle);
Theodore Ts'o819c4922013-04-03 12:47:17 -04004181
Lukas Czernereb3544c2013-05-27 23:32:35 -04004182 if (inode->i_size & (inode->i_sb->s_blocksize - 1))
4183 ext4_block_truncate_page(handle, mapping, inode->i_size);
Theodore Ts'o819c4922013-04-03 12:47:17 -04004184
4185 /*
4186 * We add the inode to the orphan list, so that if this
4187 * truncate spans multiple transactions, and we crash, we will
4188 * resume the truncate when the filesystem recovers. It also
4189 * marks the inode dirty, to catch the new size.
4190 *
4191 * Implication: the file must always be in a sane, consistent
4192 * truncatable state while each transaction commits.
4193 */
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004194 err = ext4_orphan_add(handle, inode);
4195 if (err)
Theodore Ts'o819c4922013-04-03 12:47:17 -04004196 goto out_stop;
4197
4198 down_write(&EXT4_I(inode)->i_data_sem);
4199
4200 ext4_discard_preallocations(inode);
4201
4202 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Theodore Ts'od0abb362016-11-13 22:02:28 -05004203 err = ext4_ext_truncate(handle, inode);
Theodore Ts'o819c4922013-04-03 12:47:17 -04004204 else
4205 ext4_ind_truncate(handle, inode);
4206
4207 up_write(&ei->i_data_sem);
Theodore Ts'od0abb362016-11-13 22:02:28 -05004208 if (err)
4209 goto out_stop;
Theodore Ts'o819c4922013-04-03 12:47:17 -04004210
4211 if (IS_SYNC(inode))
4212 ext4_handle_sync(handle);
4213
4214out_stop:
4215 /*
4216 * If this was a simple ftruncate() and the file will remain alive,
4217 * then we need to clear up the orphan record which we created above.
4218 * However, if this was a real unlink then we were called by
Wang Shilong58d86a52014-11-25 16:17:29 -05004219 * ext4_evict_inode(), and we allow that function to clean up the
Theodore Ts'o819c4922013-04-03 12:47:17 -04004220 * orphan info for us.
4221 */
4222 if (inode->i_nlink)
4223 ext4_orphan_del(handle, inode);
4224
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004225 inode->i_mtime = inode->i_ctime = current_time(inode);
Theodore Ts'o819c4922013-04-03 12:47:17 -04004226 ext4_mark_inode_dirty(handle, inode);
4227 ext4_journal_stop(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07004228
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004229 trace_ext4_truncate_exit(inode);
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004230 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004231}
4232
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004233/*
Mingming Cao617ba132006-10-11 01:20:53 -07004234 * ext4_get_inode_loc returns with an extra refcount against the inode's
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004235 * underlying buffer_head on success. If 'in_mem' is true, we have all
4236 * data in memory that is needed to recreate the on-disk version of this
4237 * inode.
4238 */
Mingming Cao617ba132006-10-11 01:20:53 -07004239static int __ext4_get_inode_loc(struct inode *inode,
4240 struct ext4_iloc *iloc, int in_mem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004241{
Theodore Ts'o240799c2008-10-09 23:53:47 -04004242 struct ext4_group_desc *gdp;
4243 struct buffer_head *bh;
4244 struct super_block *sb = inode->i_sb;
4245 ext4_fsblk_t block;
Linus Torvalds02f03c42019-09-29 17:59:23 -07004246 struct blk_plug plug;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004247 int inodes_per_block, inode_offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004248
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004249 iloc->bh = NULL;
Theodore Ts'oc37e9e02018-06-17 00:41:14 -04004250 if (inode->i_ino < EXT4_ROOT_INO ||
4251 inode->i_ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004252 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004253
Theodore Ts'o240799c2008-10-09 23:53:47 -04004254 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
4255 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
4256 if (!gdp)
4257 return -EIO;
4258
4259 /*
4260 * Figure out the offset within the block group inode table
4261 */
Tao Ma00d09882011-05-09 10:26:41 -04004262 inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004263 inode_offset = ((inode->i_ino - 1) %
4264 EXT4_INODES_PER_GROUP(sb));
4265 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
4266 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
4267
4268 bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05004269 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -05004270 return -ENOMEM;
Theodore Ts'o46f870d62019-11-21 13:09:43 -05004271 if (ext4_simulate_fail(sb, EXT4_SIM_INODE_EIO))
4272 goto simulate_eio;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004273 if (!buffer_uptodate(bh)) {
4274 lock_buffer(bh);
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04004275
4276 /*
4277 * If the buffer has the write error flag, we have failed
4278 * to write out another inode in the same block. In this
4279 * case, we don't have to read the block because we may
4280 * read the old inode data successfully.
4281 */
4282 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
4283 set_buffer_uptodate(bh);
4284
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004285 if (buffer_uptodate(bh)) {
4286 /* someone brought it uptodate while we waited */
4287 unlock_buffer(bh);
4288 goto has_buffer;
4289 }
4290
4291 /*
4292 * If we have all information of the inode in memory and this
4293 * is the only valid inode in the block, we need not read the
4294 * block.
4295 */
4296 if (in_mem) {
4297 struct buffer_head *bitmap_bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004298 int i, start;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004299
Theodore Ts'o240799c2008-10-09 23:53:47 -04004300 start = inode_offset & ~(inodes_per_block - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004301
4302 /* Is the inode bitmap in cache? */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004303 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
Wang Shilongaebf0242013-01-12 16:28:47 -05004304 if (unlikely(!bitmap_bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004305 goto make_io;
4306
4307 /*
4308 * If the inode bitmap isn't in cache then the
4309 * optimisation may end up performing two reads instead
4310 * of one, so skip it.
4311 */
4312 if (!buffer_uptodate(bitmap_bh)) {
4313 brelse(bitmap_bh);
4314 goto make_io;
4315 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04004316 for (i = start; i < start + inodes_per_block; i++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004317 if (i == inode_offset)
4318 continue;
Mingming Cao617ba132006-10-11 01:20:53 -07004319 if (ext4_test_bit(i, bitmap_bh->b_data))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004320 break;
4321 }
4322 brelse(bitmap_bh);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004323 if (i == start + inodes_per_block) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004324 /* all other inodes are free, so skip I/O */
4325 memset(bh->b_data, 0, bh->b_size);
4326 set_buffer_uptodate(bh);
4327 unlock_buffer(bh);
4328 goto has_buffer;
4329 }
4330 }
4331
4332make_io:
4333 /*
Theodore Ts'o240799c2008-10-09 23:53:47 -04004334 * If we need to do any I/O, try to pre-readahead extra
4335 * blocks from the inode table.
4336 */
Linus Torvalds02f03c42019-09-29 17:59:23 -07004337 blk_start_plug(&plug);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004338 if (EXT4_SB(sb)->s_inode_readahead_blks) {
4339 ext4_fsblk_t b, end, table;
4340 unsigned num;
Theodore Ts'o0d606e2c2013-04-23 08:59:35 -04004341 __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004342
4343 table = ext4_inode_table(sb, gdp);
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004344 /* s_inode_readahead_blks is always a power of 2 */
Theodore Ts'o0d606e2c2013-04-23 08:59:35 -04004345 b = block & ~((ext4_fsblk_t) ra_blks - 1);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004346 if (table > b)
4347 b = table;
Theodore Ts'o0d606e2c2013-04-23 08:59:35 -04004348 end = b + ra_blks;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004349 num = EXT4_INODES_PER_GROUP(sb);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04004350 if (ext4_has_group_desc_csum(sb))
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004351 num -= ext4_itable_unused_count(sb, gdp);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004352 table += num / inodes_per_block;
4353 if (end > table)
4354 end = table;
4355 while (b <= end)
4356 sb_breadahead(sb, b++);
4357 }
4358
4359 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004360 * There are other valid inodes in the buffer, this inode
4361 * has in-inode xattrs, or we don't have this inode in memory.
4362 * Read the block from disk.
4363 */
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004364 trace_ext4_load_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004365 get_bh(bh);
4366 bh->b_end_io = end_buffer_read_sync;
Mike Christie2a222ca2016-06-05 14:31:43 -05004367 submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh);
Linus Torvalds02f03c42019-09-29 17:59:23 -07004368 blk_finish_plug(&plug);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004369 wait_on_buffer(bh);
4370 if (!buffer_uptodate(bh)) {
Theodore Ts'o46f870d62019-11-21 13:09:43 -05004371 simulate_eio:
Theodore Ts'o878520a2019-11-19 21:54:15 -05004372 ext4_set_errno(inode->i_sb, EIO);
Theodore Ts'oc398eda2010-07-27 11:56:40 -04004373 EXT4_ERROR_INODE_BLOCK(inode, block,
4374 "unable to read itable block");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004375 brelse(bh);
4376 return -EIO;
4377 }
4378 }
4379has_buffer:
4380 iloc->bh = bh;
4381 return 0;
4382}
4383
Mingming Cao617ba132006-10-11 01:20:53 -07004384int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004385{
4386 /* We have all inode data except xattrs in memory here. */
Mingming Cao617ba132006-10-11 01:20:53 -07004387 return __ext4_get_inode_loc(inode, iloc,
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004388 !ext4_test_inode_state(inode, EXT4_STATE_XATTR));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004389}
4390
Ross Zwisler66425862017-10-12 12:00:59 -04004391static bool ext4_should_use_dax(struct inode *inode)
4392{
4393 if (!test_opt(inode->i_sb, DAX))
4394 return false;
4395 if (!S_ISREG(inode->i_mode))
4396 return false;
4397 if (ext4_should_journal_data(inode))
4398 return false;
4399 if (ext4_has_inline_data(inode))
4400 return false;
Chandan Rajendra592ddec2018-12-12 15:20:10 +05304401 if (ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT))
Ross Zwisler66425862017-10-12 12:00:59 -04004402 return false;
Eric Biggersc93d8f82019-07-22 09:26:24 -07004403 if (ext4_test_inode_flag(inode, EXT4_INODE_VERITY))
4404 return false;
Ross Zwisler66425862017-10-12 12:00:59 -04004405 return true;
4406}
4407
Mingming Cao617ba132006-10-11 01:20:53 -07004408void ext4_set_inode_flags(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004409{
Mingming Cao617ba132006-10-11 01:20:53 -07004410 unsigned int flags = EXT4_I(inode)->i_flags;
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004411 unsigned int new_fl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004412
Mingming Cao617ba132006-10-11 01:20:53 -07004413 if (flags & EXT4_SYNC_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004414 new_fl |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07004415 if (flags & EXT4_APPEND_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004416 new_fl |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07004417 if (flags & EXT4_IMMUTABLE_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004418 new_fl |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07004419 if (flags & EXT4_NOATIME_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004420 new_fl |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07004421 if (flags & EXT4_DIRSYNC_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004422 new_fl |= S_DIRSYNC;
Ross Zwisler66425862017-10-12 12:00:59 -04004423 if (ext4_should_use_dax(inode))
Ross Zwisler923ae0f2015-02-16 15:59:38 -08004424 new_fl |= S_DAX;
Eric Biggers2ee6a572017-10-09 12:15:35 -07004425 if (flags & EXT4_ENCRYPT_FL)
4426 new_fl |= S_ENCRYPTED;
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04004427 if (flags & EXT4_CASEFOLD_FL)
4428 new_fl |= S_CASEFOLD;
Eric Biggersc93d8f82019-07-22 09:26:24 -07004429 if (flags & EXT4_VERITY_FL)
4430 new_fl |= S_VERITY;
Theodore Ts'o5f16f322014-03-24 14:43:12 -04004431 inode_set_flags(inode, new_fl,
Eric Biggers2ee6a572017-10-09 12:15:35 -07004432 S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX|
Eric Biggersc93d8f82019-07-22 09:26:24 -07004433 S_ENCRYPTED|S_CASEFOLD|S_VERITY);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004434}
4435
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004436static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004437 struct ext4_inode_info *ei)
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004438{
4439 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004440 struct inode *inode = &(ei->vfs_inode);
4441 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004442
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004443 if (ext4_has_feature_huge_file(sb)) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004444 /* we are using combined 48 bit field */
4445 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
4446 le32_to_cpu(raw_inode->i_blocks_lo);
Theodore Ts'o07a03822010-06-14 09:54:48 -04004447 if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004448 /* i_blocks represent file system block size */
4449 return i_blocks << (inode->i_blkbits - 9);
4450 } else {
4451 return i_blocks;
4452 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004453 } else {
4454 return le32_to_cpu(raw_inode->i_blocks_lo);
4455 }
4456}
Jan Karaff9ddf72007-07-18 09:24:20 -04004457
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004458static inline int ext4_iget_extra_inode(struct inode *inode,
Tao Ma152a7b02012-12-02 11:13:24 -05004459 struct ext4_inode *raw_inode,
4460 struct ext4_inode_info *ei)
4461{
4462 __le32 *magic = (void *)raw_inode +
4463 EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004464
Eric Biggers290ab232016-12-01 14:51:58 -05004465 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize + sizeof(__le32) <=
4466 EXT4_INODE_SIZE(inode->i_sb) &&
4467 *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
Tao Ma152a7b02012-12-02 11:13:24 -05004468 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004469 return ext4_find_inline_data_nolock(inode);
Tao Maf19d5872012-12-10 14:05:51 -05004470 } else
4471 EXT4_I(inode)->i_inline_off = 0;
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004472 return 0;
Tao Ma152a7b02012-12-02 11:13:24 -05004473}
4474
Li Xi040cb372016-01-08 16:01:21 -05004475int ext4_get_projid(struct inode *inode, kprojid_t *projid)
4476{
Kaho Ng0b7b7772016-09-05 23:11:58 -04004477 if (!ext4_has_feature_project(inode->i_sb))
Li Xi040cb372016-01-08 16:01:21 -05004478 return -EOPNOTSUPP;
4479 *projid = EXT4_I(inode)->i_projid;
4480 return 0;
4481}
4482
Eryu Guane254d1a2018-05-10 11:55:31 -04004483/*
4484 * ext4 has self-managed i_version for ea inodes, it stores the lower 32bit of
4485 * refcount in i_version, so use raw values if inode has EXT4_EA_INODE_FL flag
4486 * set.
4487 */
4488static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val)
4489{
4490 if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
4491 inode_set_iversion_raw(inode, val);
4492 else
4493 inode_set_iversion_queried(inode, val);
4494}
4495static inline u64 ext4_inode_peek_iversion(const struct inode *inode)
4496{
4497 if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
4498 return inode_peek_iversion_raw(inode);
4499 else
4500 return inode_peek_iversion(inode);
4501}
4502
Theodore Ts'o8a363972018-12-19 12:29:13 -05004503struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
4504 ext4_iget_flags flags, const char *function,
4505 unsigned int line)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004506{
Mingming Cao617ba132006-10-11 01:20:53 -07004507 struct ext4_iloc iloc;
4508 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08004509 struct ext4_inode_info *ei;
David Howells1d1fe1e2008-02-07 00:15:37 -08004510 struct inode *inode;
Jan Karab436b9b2009-12-08 23:51:10 -05004511 journal_t *journal = EXT4_SB(sb)->s_journal;
David Howells1d1fe1e2008-02-07 00:15:37 -08004512 long ret;
Darrick J. Wong7e6e1ef2016-12-10 09:55:01 -05004513 loff_t size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004514 int block;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004515 uid_t i_uid;
4516 gid_t i_gid;
Li Xi040cb372016-01-08 16:01:21 -05004517 projid_t i_projid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004518
Theodore Ts'o191ce172018-12-31 22:34:31 -05004519 if ((!(flags & EXT4_IGET_SPECIAL) &&
Theodore Ts'o8a363972018-12-19 12:29:13 -05004520 (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)) ||
4521 (ino < EXT4_ROOT_INO) ||
4522 (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))) {
4523 if (flags & EXT4_IGET_HANDLE)
4524 return ERR_PTR(-ESTALE);
4525 __ext4_error(sb, function, line,
4526 "inode #%lu: comm %s: iget: illegal inode #",
4527 ino, current->comm);
4528 return ERR_PTR(-EFSCORRUPTED);
4529 }
4530
David Howells1d1fe1e2008-02-07 00:15:37 -08004531 inode = iget_locked(sb, ino);
4532 if (!inode)
4533 return ERR_PTR(-ENOMEM);
4534 if (!(inode->i_state & I_NEW))
4535 return inode;
4536
4537 ei = EXT4_I(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05004538 iloc.bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004539
David Howells1d1fe1e2008-02-07 00:15:37 -08004540 ret = __ext4_get_inode_loc(inode, &iloc, 0);
4541 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004542 goto bad_inode;
Mingming Cao617ba132006-10-11 01:20:53 -07004543 raw_inode = ext4_raw_inode(&iloc);
Darrick J. Wong814525f2012-04-29 18:31:10 -04004544
Theodore Ts'o8e4b5ea2018-03-29 21:56:09 -04004545 if ((ino == EXT4_ROOT_INO) && (raw_inode->i_links_count == 0)) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004546 ext4_error_inode(inode, function, line, 0,
4547 "iget: root inode unallocated");
Theodore Ts'o8e4b5ea2018-03-29 21:56:09 -04004548 ret = -EFSCORRUPTED;
4549 goto bad_inode;
4550 }
4551
Theodore Ts'o8a363972018-12-19 12:29:13 -05004552 if ((flags & EXT4_IGET_HANDLE) &&
4553 (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) {
4554 ret = -ESTALE;
4555 goto bad_inode;
4556 }
4557
Darrick J. Wong814525f2012-04-29 18:31:10 -04004558 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4559 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
4560 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
Eric Biggers2dc8d9e2016-12-01 14:43:33 -05004561 EXT4_INODE_SIZE(inode->i_sb) ||
4562 (ei->i_extra_isize & 3)) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004563 ext4_error_inode(inode, function, line, 0,
4564 "iget: bad extra_isize %u "
4565 "(inode size %u)",
Eric Biggers2dc8d9e2016-12-01 14:43:33 -05004566 ei->i_extra_isize,
4567 EXT4_INODE_SIZE(inode->i_sb));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004568 ret = -EFSCORRUPTED;
Darrick J. Wong814525f2012-04-29 18:31:10 -04004569 goto bad_inode;
4570 }
4571 } else
4572 ei->i_extra_isize = 0;
4573
4574 /* Precompute checksum seed for inode metadata */
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04004575 if (ext4_has_metadata_csum(sb)) {
Darrick J. Wong814525f2012-04-29 18:31:10 -04004576 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4577 __u32 csum;
4578 __le32 inum = cpu_to_le32(inode->i_ino);
4579 __le32 gen = raw_inode->i_generation;
4580 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
4581 sizeof(inum));
4582 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
4583 sizeof(gen));
4584 }
4585
Theodore Ts'o46f870d62019-11-21 13:09:43 -05004586 if (!ext4_inode_csum_verify(inode, raw_inode, ei) ||
4587 ext4_simulate_fail(sb, EXT4_SIM_INODE_CRC)) {
Theodore Ts'o878520a2019-11-19 21:54:15 -05004588 ext4_set_errno(inode->i_sb, EFSBADCRC);
Theodore Ts'o8a363972018-12-19 12:29:13 -05004589 ext4_error_inode(inode, function, line, 0,
4590 "iget: checksum invalid");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004591 ret = -EFSBADCRC;
Darrick J. Wong814525f2012-04-29 18:31:10 -04004592 goto bad_inode;
4593 }
4594
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004595 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004596 i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4597 i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Kaho Ng0b7b7772016-09-05 23:11:58 -04004598 if (ext4_has_feature_project(sb) &&
Li Xi040cb372016-01-08 16:01:21 -05004599 EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE &&
4600 EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
4601 i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid);
4602 else
4603 i_projid = EXT4_DEF_PROJID;
4604
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004605 if (!(test_opt(inode->i_sb, NO_UID32))) {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004606 i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4607 i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004608 }
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004609 i_uid_write(inode, i_uid);
4610 i_gid_write(inode, i_gid);
Li Xi040cb372016-01-08 16:01:21 -05004611 ei->i_projid = make_kprojid(&init_user_ns, i_projid);
Miklos Szeredibfe86842011-10-28 14:13:29 +02004612 set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004613
Theodore Ts'o353eb832011-01-10 12:18:25 -05004614 ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
Tao Ma67cf5b02012-12-10 14:04:46 -05004615 ei->i_inline_off = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004616 ei->i_dir_start_lookup = 0;
4617 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4618 /* We now have enough fields to check if the inode was active or not.
4619 * This is needed because nfsd might try to access dead inodes
4620 * the test is that same one that e2fsck uses
4621 * NeilBrown 1999oct15
4622 */
4623 if (inode->i_nlink == 0) {
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004624 if ((inode->i_mode == 0 ||
4625 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) &&
4626 ino != EXT4_BOOT_LOADER_INO) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004627 /* this inode is deleted */
David Howells1d1fe1e2008-02-07 00:15:37 -08004628 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004629 goto bad_inode;
4630 }
4631 /* The only unlinked inodes we let through here have
4632 * valid i_mode and are being read by the orphan
4633 * recovery code: that's fine, we're about to complete
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004634 * the process of deleting those.
4635 * OR it is the EXT4_BOOT_LOADER_INO which is
4636 * not initialized on a new filesystem. */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004637 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004638 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Toshi Kanicce6c9f2018-09-15 21:37:59 -04004639 ext4_set_inode_flags(inode);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004640 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004641 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004642 if (ext4_has_feature_64bit(sb))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004643 ei->i_file_acl |=
4644 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04004645 inode->i_size = ext4_isize(sb, raw_inode);
Darrick J. Wong7e6e1ef2016-12-10 09:55:01 -05004646 if ((size = i_size_read(inode)) < 0) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004647 ext4_error_inode(inode, function, line, 0,
4648 "iget: bad i_size value: %lld", size);
Darrick J. Wong7e6e1ef2016-12-10 09:55:01 -05004649 ret = -EFSCORRUPTED;
4650 goto bad_inode;
4651 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004652 ei->i_disksize = inode->i_size;
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03004653#ifdef CONFIG_QUOTA
4654 ei->i_reserved_quota = 0;
4655#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004656 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4657 ei->i_block_group = iloc.block_group;
Theodore Ts'oa4912122009-03-12 12:18:34 -04004658 ei->i_last_alloc_group = ~0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004659 /*
4660 * NOTE! The in-memory inode i_data array is in little-endian order
4661 * even on big-endian machines: we do NOT byteswap the block numbers!
4662 */
Mingming Cao617ba132006-10-11 01:20:53 -07004663 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004664 ei->i_data[block] = raw_inode->i_block[block];
4665 INIT_LIST_HEAD(&ei->i_orphan);
4666
Jan Karab436b9b2009-12-08 23:51:10 -05004667 /*
4668 * Set transaction id's of transactions that have to be committed
4669 * to finish f[data]sync. We set them to currently running transaction
4670 * as we cannot be sure that the inode or some of its metadata isn't
4671 * part of the transaction - the inode could have been reclaimed and
4672 * now it is reread from disk.
4673 */
4674 if (journal) {
4675 transaction_t *transaction;
4676 tid_t tid;
4677
Theodore Ts'oa931da62010-08-03 21:35:12 -04004678 read_lock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05004679 if (journal->j_running_transaction)
4680 transaction = journal->j_running_transaction;
4681 else
4682 transaction = journal->j_committing_transaction;
4683 if (transaction)
4684 tid = transaction->t_tid;
4685 else
4686 tid = journal->j_commit_sequence;
Theodore Ts'oa931da62010-08-03 21:35:12 -04004687 read_unlock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05004688 ei->i_sync_tid = tid;
4689 ei->i_datasync_tid = tid;
4690 }
4691
Eric Sandeen0040d982008-02-05 22:36:43 -05004692 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004693 if (ei->i_extra_isize == 0) {
4694 /* The extra space is currently unused. Use it. */
Eric Biggers2dc8d9e2016-12-01 14:43:33 -05004695 BUILD_BUG_ON(sizeof(struct ext4_inode) & 3);
Mingming Cao617ba132006-10-11 01:20:53 -07004696 ei->i_extra_isize = sizeof(struct ext4_inode) -
4697 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004698 } else {
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004699 ret = ext4_iget_extra_inode(inode, raw_inode, ei);
4700 if (ret)
4701 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004702 }
Darrick J. Wong814525f2012-04-29 18:31:10 -04004703 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004704
Kalpak Shahef7f3832007-07-18 09:15:20 -04004705 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4706 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4707 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4708 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4709
Theodore Ts'oed3654e2014-03-24 14:09:06 -04004710 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
Jeff Laytonee73f9a2018-01-09 08:21:39 -05004711 u64 ivers = le32_to_cpu(raw_inode->i_disk_version);
4712
Theodore Ts'oc4f65702014-03-20 00:32:57 -04004713 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4714 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
Jeff Laytonee73f9a2018-01-09 08:21:39 -05004715 ivers |=
Theodore Ts'oc4f65702014-03-20 00:32:57 -04004716 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
4717 }
Eryu Guane254d1a2018-05-10 11:55:31 -04004718 ext4_inode_set_iversion_queried(inode, ivers);
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004719 }
4720
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004721 ret = 0;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004722 if (ei->i_file_acl &&
Theodore Ts'o10329882009-11-15 15:29:56 -05004723 !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004724 ext4_error_inode(inode, function, line, 0,
4725 "iget: bad extended attribute block %llu",
Theodore Ts'o24676da2010-05-16 21:00:00 -04004726 ei->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004727 ret = -EFSCORRUPTED;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004728 goto bad_inode;
Tao Maf19d5872012-12-10 14:05:51 -05004729 } else if (!ext4_has_inline_data(inode)) {
Liu Songbc716522018-08-02 00:11:16 -04004730 /* validate the block references in the inode */
4731 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4732 (S_ISLNK(inode->i_mode) &&
4733 !ext4_inode_is_fast_symlink(inode))) {
4734 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Tao Maf19d5872012-12-10 14:05:51 -05004735 ret = ext4_ext_check_inode(inode);
Liu Songbc716522018-08-02 00:11:16 -04004736 else
4737 ret = ext4_ind_check_inode(inode);
Tao Maf19d5872012-12-10 14:05:51 -05004738 }
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004739 }
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004740 if (ret)
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004741 goto bad_inode;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -04004742
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004743 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004744 inode->i_op = &ext4_file_inode_operations;
Boaz Harroshbe64f882015-04-15 16:15:17 -07004745 inode->i_fop = &ext4_file_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07004746 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004747 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004748 inode->i_op = &ext4_dir_inode_operations;
4749 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004750 } else if (S_ISLNK(inode->i_mode)) {
Luis R. Rodriguez6390d332018-05-13 16:45:56 -04004751 /* VFS does not allow setting these so must be corruption */
4752 if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004753 ext4_error_inode(inode, function, line, 0,
4754 "iget: immutable or append flags "
4755 "not allowed on symlinks");
Luis R. Rodriguez6390d332018-05-13 16:45:56 -04004756 ret = -EFSCORRUPTED;
4757 goto bad_inode;
4758 }
Chandan Rajendra592ddec2018-12-12 15:20:10 +05304759 if (IS_ENCRYPTED(inode)) {
Al Viroa7a67e82015-04-27 17:51:30 -04004760 inode->i_op = &ext4_encrypted_symlink_inode_operations;
4761 ext4_set_aops(inode);
4762 } else if (ext4_inode_is_fast_symlink(inode)) {
Al Viro75e75662015-05-02 10:13:58 -04004763 inode->i_link = (char *)ei->i_data;
Mingming Cao617ba132006-10-11 01:20:53 -07004764 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00004765 nd_terminate_link(ei->i_data, inode->i_size,
4766 sizeof(ei->i_data) - 1);
4767 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004768 inode->i_op = &ext4_symlink_inode_operations;
4769 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004770 }
Al Viro21fc61c2015-11-17 01:07:57 -05004771 inode_nohighmem(inode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004772 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
4773 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004774 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004775 if (raw_inode->i_block[0])
4776 init_special_inode(inode, inode->i_mode,
4777 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4778 else
4779 init_special_inode(inode, inode->i_mode,
4780 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004781 } else if (ino == EXT4_BOOT_LOADER_INO) {
4782 make_bad_inode(inode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004783 } else {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004784 ret = -EFSCORRUPTED;
Theodore Ts'o8a363972018-12-19 12:29:13 -05004785 ext4_error_inode(inode, function, line, 0,
4786 "iget: bogus i_mode (%o)", inode->i_mode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004787 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004788 }
Theodore Ts'o6456ca62019-09-03 01:43:17 -04004789 if (IS_CASEFOLDED(inode) && !ext4_has_feature_casefold(inode->i_sb))
4790 ext4_error_inode(inode, function, line, 0,
4791 "casefold flag without casefold feature");
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004792 brelse(iloc.bh);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04004793
David Howells1d1fe1e2008-02-07 00:15:37 -08004794 unlock_new_inode(inode);
4795 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004796
4797bad_inode:
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004798 brelse(iloc.bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004799 iget_failed(inode);
4800 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004801}
4802
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004803static int ext4_inode_blocks_set(handle_t *handle,
4804 struct ext4_inode *raw_inode,
4805 struct ext4_inode_info *ei)
4806{
4807 struct inode *inode = &(ei->vfs_inode);
4808 u64 i_blocks = inode->i_blocks;
4809 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004810
4811 if (i_blocks <= ~0U) {
4812 /*
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004813 * i_blocks can be represented in a 32 bit variable
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004814 * as multiple of 512 bytes
4815 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004816 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004817 raw_inode->i_blocks_high = 0;
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004818 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004819 return 0;
4820 }
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004821 if (!ext4_has_feature_huge_file(sb))
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004822 return -EFBIG;
4823
4824 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004825 /*
4826 * i_blocks can be represented in a 48 bit variable
4827 * as multiple of 512 bytes
4828 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004829 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004830 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004831 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004832 } else {
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004833 ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004834 /* i_block is stored in file system block size */
4835 i_blocks = i_blocks >> (inode->i_blkbits - 9);
4836 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4837 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004838 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004839 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004840}
4841
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004842struct other_inode {
4843 unsigned long orig_ino;
4844 struct ext4_inode *raw_inode;
4845};
4846
4847static int other_inode_match(struct inode * inode, unsigned long ino,
4848 void *data)
4849{
4850 struct other_inode *oi = (struct other_inode *) data;
4851
4852 if ((inode->i_ino != ino) ||
4853 (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW |
Christoph Hellwig0e11f642018-02-21 07:54:49 -08004854 I_DIRTY_INODE)) ||
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004855 ((inode->i_state & I_DIRTY_TIME) == 0))
4856 return 0;
4857 spin_lock(&inode->i_lock);
4858 if (((inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW |
Christoph Hellwig0e11f642018-02-21 07:54:49 -08004859 I_DIRTY_INODE)) == 0) &&
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004860 (inode->i_state & I_DIRTY_TIME)) {
4861 struct ext4_inode_info *ei = EXT4_I(inode);
4862
4863 inode->i_state &= ~(I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED);
4864 spin_unlock(&inode->i_lock);
4865
4866 spin_lock(&ei->i_raw_lock);
4867 EXT4_INODE_SET_XTIME(i_ctime, inode, oi->raw_inode);
4868 EXT4_INODE_SET_XTIME(i_mtime, inode, oi->raw_inode);
4869 EXT4_INODE_SET_XTIME(i_atime, inode, oi->raw_inode);
4870 ext4_inode_csum_set(inode, oi->raw_inode, ei);
4871 spin_unlock(&ei->i_raw_lock);
4872 trace_ext4_other_inode_update_time(inode, oi->orig_ino);
4873 return -1;
4874 }
4875 spin_unlock(&inode->i_lock);
4876 return -1;
4877}
4878
4879/*
4880 * Opportunistically update the other time fields for other inodes in
4881 * the same inode table block.
4882 */
4883static void ext4_update_other_inodes_time(struct super_block *sb,
4884 unsigned long orig_ino, char *buf)
4885{
4886 struct other_inode oi;
4887 unsigned long ino;
4888 int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
4889 int inode_size = EXT4_INODE_SIZE(sb);
4890
4891 oi.orig_ino = orig_ino;
Theodore Ts'o0f0ff9a2015-07-01 23:37:46 -04004892 /*
4893 * Calculate the first inode in the inode table block. Inode
4894 * numbers are one-based. That is, the first inode in a block
4895 * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1).
4896 */
4897 ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1;
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004898 for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) {
4899 if (ino == orig_ino)
4900 continue;
4901 oi.raw_inode = (struct ext4_inode *) buf;
4902 (void) find_inode_nowait(sb, ino, other_inode_match, &oi);
4903 }
4904}
4905
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004906/*
4907 * Post the struct inode info into an on-disk inode location in the
4908 * buffer-cache. This gobbles the caller's reference to the
4909 * buffer_head in the inode location struct.
4910 *
4911 * The caller must have write access to iloc->bh.
4912 */
Mingming Cao617ba132006-10-11 01:20:53 -07004913static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004914 struct inode *inode,
Frank Mayhar830156c2009-09-29 10:07:47 -04004915 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004916{
Mingming Cao617ba132006-10-11 01:20:53 -07004917 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
4918 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004919 struct buffer_head *bh = iloc->bh;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004920 struct super_block *sb = inode->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004921 int err = 0, rc, block;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004922 int need_datasync = 0, set_large_file = 0;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004923 uid_t i_uid;
4924 gid_t i_gid;
Li Xi040cb372016-01-08 16:01:21 -05004925 projid_t i_projid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004926
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004927 spin_lock(&ei->i_raw_lock);
4928
4929 /* For fields not tracked in the in-memory inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004930 * initialise them to zero for new inodes. */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004931 if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
Mingming Cao617ba132006-10-11 01:20:53 -07004932 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004933
4934 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004935 i_uid = i_uid_read(inode);
4936 i_gid = i_gid_read(inode);
Li Xi040cb372016-01-08 16:01:21 -05004937 i_projid = from_kprojid(&init_user_ns, ei->i_projid);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004938 if (!(test_opt(inode->i_sb, NO_UID32))) {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004939 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid));
4940 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004941/*
4942 * Fix up interoperability with old kernels. Otherwise, old inodes get
4943 * re-used with the upper 16 bits of the uid/gid intact
4944 */
Daeho Jeong93e3b4e2016-09-05 22:56:10 -04004945 if (ei->i_dtime && list_empty(&ei->i_orphan)) {
4946 raw_inode->i_uid_high = 0;
4947 raw_inode->i_gid_high = 0;
4948 } else {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004949 raw_inode->i_uid_high =
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004950 cpu_to_le16(high_16_bits(i_uid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004951 raw_inode->i_gid_high =
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004952 cpu_to_le16(high_16_bits(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004953 }
4954 } else {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004955 raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid));
4956 raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004957 raw_inode->i_uid_high = 0;
4958 raw_inode->i_gid_high = 0;
4959 }
4960 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004961
4962 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
4963 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
4964 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
4965 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
4966
Li Xibce92d52014-10-01 22:11:06 -04004967 err = ext4_inode_blocks_set(handle, raw_inode, ei);
4968 if (err) {
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004969 spin_unlock(&ei->i_raw_lock);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004970 goto out_brelse;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004971 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004972 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Theodore Ts'o353eb832011-01-10 12:18:25 -05004973 raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
Theodore Ts'oed3654e2014-03-24 14:09:06 -04004974 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT)))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004975 raw_inode->i_file_acl_high =
4976 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004977 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04004978 if (ei->i_disksize != ext4_isize(inode->i_sb, raw_inode)) {
Jan Karab71fc072012-09-26 21:52:20 -04004979 ext4_isize_set(raw_inode, ei->i_disksize);
4980 need_datasync = 1;
4981 }
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004982 if (ei->i_disksize > 0x7fffffffULL) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004983 if (!ext4_has_feature_large_file(sb) ||
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004984 EXT4_SB(sb)->s_es->s_rev_level ==
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004985 cpu_to_le32(EXT4_GOOD_OLD_REV))
4986 set_large_file = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004987 }
4988 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
4989 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
4990 if (old_valid_dev(inode->i_rdev)) {
4991 raw_inode->i_block[0] =
4992 cpu_to_le32(old_encode_dev(inode->i_rdev));
4993 raw_inode->i_block[1] = 0;
4994 } else {
4995 raw_inode->i_block[0] = 0;
4996 raw_inode->i_block[1] =
4997 cpu_to_le32(new_encode_dev(inode->i_rdev));
4998 raw_inode->i_block[2] = 0;
4999 }
Tao Maf19d5872012-12-10 14:05:51 -05005000 } else if (!ext4_has_inline_data(inode)) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04005001 for (block = 0; block < EXT4_N_BLOCKS; block++)
5002 raw_inode->i_block[block] = ei->i_data[block];
Tao Maf19d5872012-12-10 14:05:51 -05005003 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005004
Theodore Ts'oed3654e2014-03-24 14:09:06 -04005005 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
Eryu Guane254d1a2018-05-10 11:55:31 -04005006 u64 ivers = ext4_inode_peek_iversion(inode);
Jeff Laytonee73f9a2018-01-09 08:21:39 -05005007
5008 raw_inode->i_disk_version = cpu_to_le32(ivers);
Theodore Ts'oc4f65702014-03-20 00:32:57 -04005009 if (ei->i_extra_isize) {
5010 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
5011 raw_inode->i_version_hi =
Jeff Laytonee73f9a2018-01-09 08:21:39 -05005012 cpu_to_le32(ivers >> 32);
Theodore Ts'oc4f65702014-03-20 00:32:57 -04005013 raw_inode->i_extra_isize =
5014 cpu_to_le16(ei->i_extra_isize);
5015 }
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005016 }
Li Xi040cb372016-01-08 16:01:21 -05005017
Kaho Ng0b7b7772016-09-05 23:11:58 -04005018 BUG_ON(!ext4_has_feature_project(inode->i_sb) &&
Li Xi040cb372016-01-08 16:01:21 -05005019 i_projid != EXT4_DEF_PROJID);
5020
5021 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
5022 EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
5023 raw_inode->i_projid = cpu_to_le32(i_projid);
5024
Darrick J. Wong814525f2012-04-29 18:31:10 -04005025 ext4_inode_csum_set(inode, raw_inode, ei);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005026 spin_unlock(&ei->i_raw_lock);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08005027 if (inode->i_sb->s_flags & SB_LAZYTIME)
Theodore Ts'oa26f4992015-02-02 00:37:02 -05005028 ext4_update_other_inodes_time(inode->i_sb, inode->i_ino,
5029 bh->b_data);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005030
Frank Mayhar830156c2009-09-29 10:07:47 -04005031 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05005032 rc = ext4_handle_dirty_metadata(handle, NULL, bh);
Frank Mayhar830156c2009-09-29 10:07:47 -04005033 if (!err)
5034 err = rc;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005035 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005036 if (set_large_file) {
liang xie5d601252014-05-12 22:06:43 -04005037 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access");
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005038 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
5039 if (err)
5040 goto out_brelse;
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005041 ext4_set_feature_large_file(sb);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005042 ext4_handle_sync(handle);
5043 err = ext4_handle_dirty_super(handle, sb);
5044 }
Jan Karab71fc072012-09-26 21:52:20 -04005045 ext4_update_inode_fsync_trans(handle, inode, need_datasync);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005046out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04005047 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07005048 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005049 return err;
5050}
5051
5052/*
Mingming Cao617ba132006-10-11 01:20:53 -07005053 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005054 *
5055 * We are called from a few places:
5056 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005057 * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005058 * Here, there will be no transaction running. We wait for any running
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005059 * transaction to commit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005060 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005061 * - Within flush work (sys_sync(), kupdate and such).
5062 * We wait on commit, if told to.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005063 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005064 * - Within iput_final() -> write_inode_now()
5065 * We wait on commit, if told to.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005066 *
5067 * In all cases it is actually safe for us to return without doing anything,
5068 * because the inode has been copied into a raw inode buffer in
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005069 * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL
5070 * writeback.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005071 *
5072 * Note that we are absolutely dependent upon all inode dirtiers doing the
5073 * right thing: they *must* call mark_inode_dirty() after dirtying info in
5074 * which we are interested.
5075 *
5076 * It would be a bug for them to not do this. The code:
5077 *
5078 * mark_inode_dirty(inode)
5079 * stuff();
5080 * inode->i_size = expr;
5081 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005082 * is in error because write_inode() could occur while `stuff()' is running,
5083 * and the new i_size will be lost. Plus the inode will no longer be on the
5084 * superblock's dirty inode list.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005085 */
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005086int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005087{
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005088 int err;
5089
Theodore Ts'o18f2c4f2018-12-19 14:36:58 -05005090 if (WARN_ON_ONCE(current->flags & PF_MEMALLOC) ||
5091 sb_rdonly(inode->i_sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005092 return 0;
5093
Theodore Ts'o18f2c4f2018-12-19 14:36:58 -05005094 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5095 return -EIO;
5096
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005097 if (EXT4_SB(inode->i_sb)->s_journal) {
5098 if (ext4_journal_current_handle()) {
5099 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
5100 dump_stack();
5101 return -EIO;
5102 }
5103
Jan Kara10542c22014-03-04 10:50:50 -05005104 /*
5105 * No need to force transaction in WB_SYNC_NONE mode. Also
5106 * ext4_sync_fs() will force the commit after everything is
5107 * written.
5108 */
5109 if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync)
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005110 return 0;
5111
Theodore Ts'o18f2c4f2018-12-19 14:36:58 -05005112 err = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
5113 EXT4_I(inode)->i_sync_tid);
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005114 } else {
5115 struct ext4_iloc iloc;
5116
Curt Wohlgemuth8b472d72010-04-03 16:45:06 -04005117 err = __ext4_get_inode_loc(inode, &iloc, 0);
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005118 if (err)
5119 return err;
Jan Kara10542c22014-03-04 10:50:50 -05005120 /*
5121 * sync(2) will flush the whole buffer cache. No need to do
5122 * it here separately for each inode.
5123 */
5124 if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
Frank Mayhar830156c2009-09-29 10:07:47 -04005125 sync_dirty_buffer(iloc.bh);
5126 if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
Theodore Ts'o878520a2019-11-19 21:54:15 -05005127 ext4_set_errno(inode->i_sb, EIO);
Theodore Ts'oc398eda2010-07-27 11:56:40 -04005128 EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr,
5129 "IO error syncing inode");
Frank Mayhar830156c2009-09-29 10:07:47 -04005130 err = -EIO;
5131 }
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04005132 brelse(iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005133 }
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005134 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005135}
5136
5137/*
Jan Kara53e87262012-12-25 13:29:52 -05005138 * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
5139 * buffers that are attached to a page stradding i_size and are undergoing
5140 * commit. In that case we have to wait for commit to finish and try again.
5141 */
5142static void ext4_wait_for_tail_page_commit(struct inode *inode)
5143{
5144 struct page *page;
5145 unsigned offset;
5146 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
5147 tid_t commit_tid = 0;
5148 int ret;
5149
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005150 offset = inode->i_size & (PAGE_SIZE - 1);
Jan Kara53e87262012-12-25 13:29:52 -05005151 /*
yangerkun565333a2019-09-19 14:35:08 +08005152 * If the page is fully truncated, we don't need to wait for any commit
5153 * (and we even should not as __ext4_journalled_invalidatepage() may
5154 * strip all buffers from the page but keep the page dirty which can then
5155 * confuse e.g. concurrent ext4_writepage() seeing dirty page without
5156 * buffers). Also we don't need to wait for any commit if all buffers in
5157 * the page remain valid. This is most beneficial for the common case of
5158 * blocksize == PAGESIZE.
Jan Kara53e87262012-12-25 13:29:52 -05005159 */
yangerkun565333a2019-09-19 14:35:08 +08005160 if (!offset || offset > (PAGE_SIZE - i_blocksize(inode)))
Jan Kara53e87262012-12-25 13:29:52 -05005161 return;
5162 while (1) {
5163 page = find_lock_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005164 inode->i_size >> PAGE_SHIFT);
Jan Kara53e87262012-12-25 13:29:52 -05005165 if (!page)
5166 return;
Lukas Czernerca99fdd2013-05-21 23:25:01 -04005167 ret = __ext4_journalled_invalidatepage(page, offset,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005168 PAGE_SIZE - offset);
Jan Kara53e87262012-12-25 13:29:52 -05005169 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005170 put_page(page);
Jan Kara53e87262012-12-25 13:29:52 -05005171 if (ret != -EBUSY)
5172 return;
5173 commit_tid = 0;
5174 read_lock(&journal->j_state_lock);
5175 if (journal->j_committing_transaction)
5176 commit_tid = journal->j_committing_transaction->t_tid;
5177 read_unlock(&journal->j_state_lock);
5178 if (commit_tid)
5179 jbd2_log_wait_commit(journal, commit_tid);
5180 }
5181}
5182
5183/*
Mingming Cao617ba132006-10-11 01:20:53 -07005184 * ext4_setattr()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005185 *
5186 * Called from notify_change.
5187 *
5188 * We want to trap VFS attempts to truncate the file as soon as
5189 * possible. In particular, we want to make sure that when the VFS
5190 * shrinks i_size, we put the inode on the orphan list and modify
5191 * i_disksize immediately, so that during the subsequent flushing of
5192 * dirty pages and freeing of disk blocks, we can guarantee that any
5193 * commit will leave the blocks being flushed in an unused state on
5194 * disk. (On recovery, the inode will get truncated and the blocks will
5195 * be freed, so we have a strong guarantee that no future commit will
5196 * leave these blocks visible to the user.)
5197 *
Jan Kara678aaf42008-07-11 19:27:31 -04005198 * Another thing we have to assure is that if we are in ordered mode
5199 * and inode is still attached to the committing transaction, we must
5200 * we start writeout of all the dirty pages which are being truncated.
5201 * This way we are sure that all the data written in the previous
5202 * transaction are already on disk (truncate waits for pages under
5203 * writeback).
5204 *
5205 * Called with inode->i_mutex down.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005206 */
Mingming Cao617ba132006-10-11 01:20:53 -07005207int ext4_setattr(struct dentry *dentry, struct iattr *attr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005208{
David Howells2b0143b2015-03-17 22:25:59 +00005209 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005210 int error, rc = 0;
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04005211 int orphan = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005212 const unsigned int ia_valid = attr->ia_valid;
5213
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05005214 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5215 return -EIO;
5216
Theodore Ts'o02b016c2019-06-09 22:04:33 -04005217 if (unlikely(IS_IMMUTABLE(inode)))
5218 return -EPERM;
5219
5220 if (unlikely(IS_APPEND(inode) &&
5221 (ia_valid & (ATTR_MODE | ATTR_UID |
5222 ATTR_GID | ATTR_TIMES_SET))))
5223 return -EPERM;
5224
Jan Kara31051c82016-05-26 16:55:18 +02005225 error = setattr_prepare(dentry, attr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005226 if (error)
5227 return error;
5228
Eric Biggers3ce2b8d2017-10-18 20:21:58 -04005229 error = fscrypt_prepare_setattr(dentry, attr);
5230 if (error)
5231 return error;
5232
Eric Biggersc93d8f82019-07-22 09:26:24 -07005233 error = fsverity_prepare_setattr(dentry, attr);
5234 if (error)
5235 return error;
5236
Jan Karaa7cdade2015-06-29 16:22:54 +02005237 if (is_quota_modification(inode, attr)) {
5238 error = dquot_initialize(inode);
5239 if (error)
5240 return error;
5241 }
Eric W. Biederman08cefc72012-02-07 15:41:49 -08005242 if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
5243 (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005244 handle_t *handle;
5245
5246 /* (user+group)*(old+new) structure, inode write (sb,
5247 * inode block, ? - but truncate inode update has it) */
Theodore Ts'o9924a922013-02-08 21:59:22 -05005248 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
5249 (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) +
5250 EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005251 if (IS_ERR(handle)) {
5252 error = PTR_ERR(handle);
5253 goto err_out;
5254 }
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -04005255
5256 /* dquot_transfer() calls back ext4_get_inode_usage() which
5257 * counts xattr inode references.
5258 */
5259 down_read(&EXT4_I(inode)->xattr_sem);
Christoph Hellwigb43fa822010-03-03 09:05:03 -05005260 error = dquot_transfer(inode, attr);
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -04005261 up_read(&EXT4_I(inode)->xattr_sem);
5262
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005263 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07005264 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005265 return error;
5266 }
5267 /* Update corresponding info in inode so that everything is in
5268 * one transaction */
5269 if (attr->ia_valid & ATTR_UID)
5270 inode->i_uid = attr->ia_uid;
5271 if (attr->ia_valid & ATTR_GID)
5272 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07005273 error = ext4_mark_inode_dirty(handle, inode);
5274 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005275 }
5276
Josef Bacik3da40c72015-06-22 00:31:26 -04005277 if (attr->ia_valid & ATTR_SIZE) {
Jan Kara52083862013-08-17 10:07:17 -04005278 handle_t *handle;
Josef Bacik3da40c72015-06-22 00:31:26 -04005279 loff_t oldsize = inode->i_size;
Jan Karab9c1c262019-05-30 11:56:23 -04005280 int shrink = (attr->ia_size < inode->i_size);
Christoph Hellwig562c72aa52011-06-24 14:29:45 -04005281
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005282 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
Eric Sandeene2b46572008-01-28 23:58:27 -05005283 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5284
Theodore Ts'o0c095c72010-07-27 11:56:06 -04005285 if (attr->ia_size > sbi->s_bitmap_maxbytes)
5286 return -EFBIG;
Eric Sandeene2b46572008-01-28 23:58:27 -05005287 }
Josef Bacik3da40c72015-06-22 00:31:26 -04005288 if (!S_ISREG(inode->i_mode))
5289 return -EINVAL;
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005290
5291 if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size)
5292 inode_inc_iversion(inode);
5293
Jan Karab9c1c262019-05-30 11:56:23 -04005294 if (shrink) {
5295 if (ext4_should_order_data(inode)) {
5296 error = ext4_begin_ordered_truncate(inode,
Jan Kara678aaf42008-07-11 19:27:31 -04005297 attr->ia_size);
Jan Karab9c1c262019-05-30 11:56:23 -04005298 if (error)
5299 goto err_out;
5300 }
5301 /*
5302 * Blocks are going to be removed from the inode. Wait
5303 * for dio in flight.
5304 */
5305 inode_dio_wait(inode);
Josef Bacik3da40c72015-06-22 00:31:26 -04005306 }
Jan Karab9c1c262019-05-30 11:56:23 -04005307
5308 down_write(&EXT4_I(inode)->i_mmap_sem);
5309
5310 rc = ext4_break_layouts(inode);
5311 if (rc) {
5312 up_write(&EXT4_I(inode)->i_mmap_sem);
5313 return rc;
5314 }
5315
Josef Bacik3da40c72015-06-22 00:31:26 -04005316 if (attr->ia_size != inode->i_size) {
Jan Kara52083862013-08-17 10:07:17 -04005317 handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
5318 if (IS_ERR(handle)) {
5319 error = PTR_ERR(handle);
Jan Karab9c1c262019-05-30 11:56:23 -04005320 goto out_mmap_sem;
Jan Kara52083862013-08-17 10:07:17 -04005321 }
Josef Bacik3da40c72015-06-22 00:31:26 -04005322 if (ext4_handle_valid(handle) && shrink) {
Jan Kara52083862013-08-17 10:07:17 -04005323 error = ext4_orphan_add(handle, inode);
5324 orphan = 1;
5325 }
Eryu Guan911af572015-07-28 15:08:41 -04005326 /*
5327 * Update c/mtime on truncate up, ext4_truncate() will
5328 * update c/mtime in shrink case below
5329 */
5330 if (!shrink) {
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005331 inode->i_mtime = current_time(inode);
Eryu Guan911af572015-07-28 15:08:41 -04005332 inode->i_ctime = inode->i_mtime;
5333 }
Jan Kara90e775b2013-08-17 10:09:31 -04005334 down_write(&EXT4_I(inode)->i_data_sem);
Jan Kara52083862013-08-17 10:07:17 -04005335 EXT4_I(inode)->i_disksize = attr->ia_size;
5336 rc = ext4_mark_inode_dirty(handle, inode);
5337 if (!error)
5338 error = rc;
Jan Kara90e775b2013-08-17 10:09:31 -04005339 /*
5340 * We have to update i_size under i_data_sem together
5341 * with i_disksize to avoid races with writeback code
5342 * running ext4_wb_update_i_disksize().
5343 */
5344 if (!error)
5345 i_size_write(inode, attr->ia_size);
5346 up_write(&EXT4_I(inode)->i_data_sem);
Jan Kara52083862013-08-17 10:07:17 -04005347 ext4_journal_stop(handle);
Jan Karab9c1c262019-05-30 11:56:23 -04005348 if (error)
5349 goto out_mmap_sem;
5350 if (!shrink) {
5351 pagecache_isize_extended(inode, oldsize,
5352 inode->i_size);
5353 } else if (ext4_should_journal_data(inode)) {
5354 ext4_wait_for_tail_page_commit(inode);
Jan Kara678aaf42008-07-11 19:27:31 -04005355 }
Jan Karad6320cb2014-10-01 21:49:46 -04005356 }
Ross Zwisler430657b2018-07-29 17:00:22 -04005357
Jan Kara52083862013-08-17 10:07:17 -04005358 /*
5359 * Truncate pagecache after we've waited for commit
5360 * in data=journal mode to make pages freeable.
5361 */
Ross Zwisler923ae0f2015-02-16 15:59:38 -08005362 truncate_pagecache(inode, inode->i_size);
Jan Karab9c1c262019-05-30 11:56:23 -04005363 /*
5364 * Call ext4_truncate() even if i_size didn't change to
5365 * truncate possible preallocated blocks.
5366 */
5367 if (attr->ia_size <= oldsize) {
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05005368 rc = ext4_truncate(inode);
5369 if (rc)
5370 error = rc;
5371 }
Jan Karab9c1c262019-05-30 11:56:23 -04005372out_mmap_sem:
Jan Karaea3d7202015-12-07 14:28:03 -05005373 up_write(&EXT4_I(inode)->i_mmap_sem);
Theodore Ts'o072bd7e2011-05-23 15:13:02 -04005374 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005375
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05005376 if (!error) {
Christoph Hellwig10257742010-06-04 11:30:02 +02005377 setattr_copy(inode, attr);
5378 mark_inode_dirty(inode);
5379 }
5380
5381 /*
5382 * If the call to ext4_truncate failed to get a transaction handle at
5383 * all, we need to clean up the in-core orphan list manually.
5384 */
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04005385 if (orphan && inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07005386 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005387
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05005388 if (!error && (ia_valid & ATTR_MODE))
Christoph Hellwig64e178a2013-12-20 05:16:44 -08005389 rc = posix_acl_chmod(inode, inode->i_mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005390
5391err_out:
Mingming Cao617ba132006-10-11 01:20:53 -07005392 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005393 if (!error)
5394 error = rc;
5395 return error;
5396}
5397
David Howellsa528d352017-01-31 16:46:22 +00005398int ext4_getattr(const struct path *path, struct kstat *stat,
5399 u32 request_mask, unsigned int query_flags)
Mingming Cao3e3398a2008-07-11 19:27:31 -04005400{
David Howells99652ea2017-03-31 18:31:56 +01005401 struct inode *inode = d_inode(path->dentry);
5402 struct ext4_inode *raw_inode;
5403 struct ext4_inode_info *ei = EXT4_I(inode);
5404 unsigned int flags;
Mingming Cao3e3398a2008-07-11 19:27:31 -04005405
Theodore Ts'od4c5e962019-11-28 22:26:51 -05005406 if ((request_mask & STATX_BTIME) &&
5407 EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime)) {
David Howells99652ea2017-03-31 18:31:56 +01005408 stat->result_mask |= STATX_BTIME;
5409 stat->btime.tv_sec = ei->i_crtime.tv_sec;
5410 stat->btime.tv_nsec = ei->i_crtime.tv_nsec;
5411 }
5412
5413 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
5414 if (flags & EXT4_APPEND_FL)
5415 stat->attributes |= STATX_ATTR_APPEND;
5416 if (flags & EXT4_COMPR_FL)
5417 stat->attributes |= STATX_ATTR_COMPRESSED;
5418 if (flags & EXT4_ENCRYPT_FL)
5419 stat->attributes |= STATX_ATTR_ENCRYPTED;
5420 if (flags & EXT4_IMMUTABLE_FL)
5421 stat->attributes |= STATX_ATTR_IMMUTABLE;
5422 if (flags & EXT4_NODUMP_FL)
5423 stat->attributes |= STATX_ATTR_NODUMP;
Eric Biggers1f607192019-10-29 13:41:39 -07005424 if (flags & EXT4_VERITY_FL)
5425 stat->attributes |= STATX_ATTR_VERITY;
David Howells99652ea2017-03-31 18:31:56 +01005426
David Howells3209f682017-03-31 18:32:17 +01005427 stat->attributes_mask |= (STATX_ATTR_APPEND |
5428 STATX_ATTR_COMPRESSED |
5429 STATX_ATTR_ENCRYPTED |
5430 STATX_ATTR_IMMUTABLE |
Eric Biggers1f607192019-10-29 13:41:39 -07005431 STATX_ATTR_NODUMP |
5432 STATX_ATTR_VERITY);
David Howells3209f682017-03-31 18:32:17 +01005433
Mingming Cao3e3398a2008-07-11 19:27:31 -04005434 generic_fillattr(inode, stat);
David Howells99652ea2017-03-31 18:31:56 +01005435 return 0;
5436}
5437
5438int ext4_file_getattr(const struct path *path, struct kstat *stat,
5439 u32 request_mask, unsigned int query_flags)
5440{
5441 struct inode *inode = d_inode(path->dentry);
5442 u64 delalloc_blocks;
5443
5444 ext4_getattr(path, stat, request_mask, query_flags);
Mingming Cao3e3398a2008-07-11 19:27:31 -04005445
5446 /*
Andreas Dilger9206c562013-11-11 22:38:12 -05005447 * If there is inline data in the inode, the inode will normally not
5448 * have data blocks allocated (it may have an external xattr block).
5449 * Report at least one sector for such files, so tools like tar, rsync,
Theodore Ts'od67d64f2017-03-25 17:33:31 -04005450 * others don't incorrectly think the file is completely sparse.
Andreas Dilger9206c562013-11-11 22:38:12 -05005451 */
5452 if (unlikely(ext4_has_inline_data(inode)))
5453 stat->blocks += (stat->size + 511) >> 9;
5454
5455 /*
Mingming Cao3e3398a2008-07-11 19:27:31 -04005456 * We can't update i_blocks if the block allocation is delayed
5457 * otherwise in the case of system crash before the real block
5458 * allocation is done, we will have i_blocks inconsistent with
5459 * on-disk file blocks.
5460 * We always keep i_blocks updated together with real
5461 * allocation. But to not confuse with user, stat
5462 * will return the blocks that include the delayed allocation
5463 * blocks for this file.
5464 */
Tao Ma96607552012-05-31 22:54:16 -04005465 delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
Andreas Dilger9206c562013-11-11 22:38:12 -05005466 EXT4_I(inode)->i_reserved_data_blocks);
5467 stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9);
Mingming Cao3e3398a2008-07-11 19:27:31 -04005468 return 0;
5469}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005470
Jan Karafffb2732013-06-04 13:01:11 -04005471static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
5472 int pextents)
Mingming Caoa02908f2008-08-19 22:16:07 -04005473{
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005474 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Jan Karafffb2732013-06-04 13:01:11 -04005475 return ext4_ind_trans_blocks(inode, lblocks);
5476 return ext4_ext_index_trans_blocks(inode, pextents);
Mingming Caoa02908f2008-08-19 22:16:07 -04005477}
Theodore Ts'oac51d832008-11-06 16:49:36 -05005478
Mingming Caoa02908f2008-08-19 22:16:07 -04005479/*
5480 * Account for index blocks, block groups bitmaps and block group
5481 * descriptor blocks if modify datablocks and index blocks
5482 * worse case, the indexs blocks spread over different block groups
5483 *
5484 * If datablocks are discontiguous, they are possible to spread over
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005485 * different block groups too. If they are contiguous, with flexbg,
Mingming Caoa02908f2008-08-19 22:16:07 -04005486 * they could still across block group boundary.
5487 *
5488 * Also account for superblock, inode, quota and xattr blocks
5489 */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04005490static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
Jan Karafffb2732013-06-04 13:01:11 -04005491 int pextents)
Mingming Caoa02908f2008-08-19 22:16:07 -04005492{
Theodore Ts'o8df96752009-05-01 08:50:38 -04005493 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
5494 int gdpblocks;
Mingming Caoa02908f2008-08-19 22:16:07 -04005495 int idxblocks;
5496 int ret = 0;
5497
5498 /*
Jan Karafffb2732013-06-04 13:01:11 -04005499 * How many index blocks need to touch to map @lblocks logical blocks
5500 * to @pextents physical extents?
Mingming Caoa02908f2008-08-19 22:16:07 -04005501 */
Jan Karafffb2732013-06-04 13:01:11 -04005502 idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents);
Mingming Caoa02908f2008-08-19 22:16:07 -04005503
5504 ret = idxblocks;
5505
5506 /*
5507 * Now let's see how many group bitmaps and group descriptors need
5508 * to account
5509 */
Jan Karafffb2732013-06-04 13:01:11 -04005510 groups = idxblocks + pextents;
Mingming Caoa02908f2008-08-19 22:16:07 -04005511 gdpblocks = groups;
Theodore Ts'o8df96752009-05-01 08:50:38 -04005512 if (groups > ngroups)
5513 groups = ngroups;
Mingming Caoa02908f2008-08-19 22:16:07 -04005514 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
5515 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
5516
5517 /* bitmaps and block group descriptor blocks */
5518 ret += groups + gdpblocks;
5519
5520 /* Blocks for super block, inode, quota and xattr blocks */
5521 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005522
5523 return ret;
5524}
5525
5526/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005527 * Calculate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04005528 * the modification of a single pages into a single transaction,
5529 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04005530 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04005531 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04005532 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04005533 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04005534 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04005535 */
5536int ext4_writepage_trans_blocks(struct inode *inode)
5537{
5538 int bpp = ext4_journal_blocks_per_page(inode);
5539 int ret;
5540
Jan Karafffb2732013-06-04 13:01:11 -04005541 ret = ext4_meta_trans_blocks(inode, bpp, bpp);
Mingming Caoa02908f2008-08-19 22:16:07 -04005542
5543 /* Account for data blocks for journalled mode */
5544 if (ext4_should_journal_data(inode))
5545 ret += bpp;
5546 return ret;
5547}
Mingming Caof3bd1f32008-08-19 22:16:03 -04005548
5549/*
5550 * Calculate the journal credits for a chunk of data modification.
5551 *
5552 * This is called from DIO, fallocate or whoever calling
Eric Sandeen79e83032010-07-27 11:56:07 -04005553 * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
Mingming Caof3bd1f32008-08-19 22:16:03 -04005554 *
5555 * journal buffers for data blocks are not included here, as DIO
5556 * and fallocate do no need to journal data buffers.
5557 */
5558int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
5559{
5560 return ext4_meta_trans_blocks(inode, nrblocks, 1);
5561}
5562
Mingming Caoa02908f2008-08-19 22:16:07 -04005563/*
Mingming Cao617ba132006-10-11 01:20:53 -07005564 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005565 * Give this, we know that the caller already has write access to iloc->bh.
5566 */
Mingming Cao617ba132006-10-11 01:20:53 -07005567int ext4_mark_iloc_dirty(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04005568 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005569{
5570 int err = 0;
5571
Vasily Averina6758302018-11-06 16:49:50 -05005572 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) {
5573 put_bh(iloc->bh);
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05005574 return -EIO;
Vasily Averina6758302018-11-06 16:49:50 -05005575 }
Theodore Ts'oc64db502012-03-02 12:23:11 -05005576 if (IS_I_VERSION(inode))
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005577 inode_inc_iversion(inode);
5578
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005579 /* the do_update_inode consumes one bh->b_count */
5580 get_bh(iloc->bh);
5581
Mingming Caodab291a2006-10-11 01:21:01 -07005582 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Frank Mayhar830156c2009-09-29 10:07:47 -04005583 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005584 put_bh(iloc->bh);
5585 return err;
5586}
5587
5588/*
5589 * On success, We end up with an outstanding reference count against
5590 * iloc->bh. This _must_ be cleaned up later.
5591 */
5592
5593int
Mingming Cao617ba132006-10-11 01:20:53 -07005594ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
5595 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005596{
Frank Mayhar03901312009-01-07 00:06:22 -05005597 int err;
5598
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05005599 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5600 return -EIO;
5601
Frank Mayhar03901312009-01-07 00:06:22 -05005602 err = ext4_get_inode_loc(inode, iloc);
5603 if (!err) {
5604 BUFFER_TRACE(iloc->bh, "get_write_access");
5605 err = ext4_journal_get_write_access(handle, iloc->bh);
5606 if (err) {
5607 brelse(iloc->bh);
5608 iloc->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005609 }
5610 }
Mingming Cao617ba132006-10-11 01:20:53 -07005611 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005612 return err;
5613}
5614
Miao Xiec03b45b2017-08-06 01:00:49 -04005615static int __ext4_expand_extra_isize(struct inode *inode,
5616 unsigned int new_extra_isize,
5617 struct ext4_iloc *iloc,
5618 handle_t *handle, int *no_expand)
5619{
5620 struct ext4_inode *raw_inode;
5621 struct ext4_xattr_ibody_header *header;
Theodore Ts'o4ea99932019-11-07 21:43:41 -05005622 unsigned int inode_size = EXT4_INODE_SIZE(inode->i_sb);
5623 struct ext4_inode_info *ei = EXT4_I(inode);
Miao Xiec03b45b2017-08-06 01:00:49 -04005624 int error;
5625
Theodore Ts'o4ea99932019-11-07 21:43:41 -05005626 /* this was checked at iget time, but double check for good measure */
5627 if ((EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > inode_size) ||
5628 (ei->i_extra_isize & 3)) {
5629 EXT4_ERROR_INODE(inode, "bad extra_isize %u (inode size %u)",
5630 ei->i_extra_isize,
5631 EXT4_INODE_SIZE(inode->i_sb));
5632 return -EFSCORRUPTED;
5633 }
5634 if ((new_extra_isize < ei->i_extra_isize) ||
5635 (new_extra_isize < 4) ||
5636 (new_extra_isize > inode_size - EXT4_GOOD_OLD_INODE_SIZE))
5637 return -EINVAL; /* Should never happen */
5638
Miao Xiec03b45b2017-08-06 01:00:49 -04005639 raw_inode = ext4_raw_inode(iloc);
5640
5641 header = IHDR(inode, raw_inode);
5642
5643 /* No extended attributes present */
5644 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
5645 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
5646 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE +
5647 EXT4_I(inode)->i_extra_isize, 0,
5648 new_extra_isize - EXT4_I(inode)->i_extra_isize);
5649 EXT4_I(inode)->i_extra_isize = new_extra_isize;
5650 return 0;
5651 }
5652
5653 /* try to expand with EAs present */
5654 error = ext4_expand_extra_isize_ea(inode, new_extra_isize,
5655 raw_inode, handle);
5656 if (error) {
5657 /*
5658 * Inode size expansion failed; don't try again
5659 */
5660 *no_expand = 1;
5661 }
5662
5663 return error;
5664}
5665
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005666/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005667 * Expand an inode by new_extra_isize bytes.
5668 * Returns 0 on success or negative error number on failure.
5669 */
Miao Xiecf0a5e82017-08-06 00:40:01 -04005670static int ext4_try_to_expand_extra_isize(struct inode *inode,
5671 unsigned int new_extra_isize,
5672 struct ext4_iloc iloc,
5673 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005674{
Miao Xie3b10fdc2017-08-06 00:27:38 -04005675 int no_expand;
5676 int error;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005677
Miao Xiecf0a5e82017-08-06 00:40:01 -04005678 if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND))
5679 return -EOVERFLOW;
5680
5681 /*
5682 * In nojournal mode, we can immediately attempt to expand
5683 * the inode. When journaled, we first need to obtain extra
5684 * buffer credits since we may write into the EA block
5685 * with this same handle. If journal_extend fails, then it will
5686 * only result in a minor loss of functionality for that inode.
5687 * If this is felt to be critical, then e2fsck should be run to
5688 * force a large enough s_min_extra_isize.
5689 */
Jan Kara6cb367c2019-11-05 17:44:14 +01005690 if (ext4_journal_extend(handle,
Jan Kara83448bd2019-11-05 17:44:29 +01005691 EXT4_DATA_TRANS_BLOCKS(inode->i_sb), 0) != 0)
Miao Xiecf0a5e82017-08-06 00:40:01 -04005692 return -ENOSPC;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005693
Miao Xie3b10fdc2017-08-06 00:27:38 -04005694 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
Miao Xiecf0a5e82017-08-06 00:40:01 -04005695 return -EBUSY;
Miao Xie3b10fdc2017-08-06 00:27:38 -04005696
Miao Xiec03b45b2017-08-06 01:00:49 -04005697 error = __ext4_expand_extra_isize(inode, new_extra_isize, &iloc,
5698 handle, &no_expand);
Miao Xie3b10fdc2017-08-06 00:27:38 -04005699 ext4_write_unlock_xattr(inode, &no_expand);
Miao Xiecf0a5e82017-08-06 00:40:01 -04005700
Miao Xie3b10fdc2017-08-06 00:27:38 -04005701 return error;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005702}
5703
Miao Xiec03b45b2017-08-06 01:00:49 -04005704int ext4_expand_extra_isize(struct inode *inode,
5705 unsigned int new_extra_isize,
5706 struct ext4_iloc *iloc)
5707{
5708 handle_t *handle;
5709 int no_expand;
5710 int error, rc;
5711
5712 if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
5713 brelse(iloc->bh);
5714 return -EOVERFLOW;
5715 }
5716
5717 handle = ext4_journal_start(inode, EXT4_HT_INODE,
5718 EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
5719 if (IS_ERR(handle)) {
5720 error = PTR_ERR(handle);
5721 brelse(iloc->bh);
5722 return error;
5723 }
5724
5725 ext4_write_lock_xattr(inode, &no_expand);
5726
zhangyi (F)ddccb6d2019-02-21 11:29:10 -05005727 BUFFER_TRACE(iloc->bh, "get_write_access");
Miao Xiec03b45b2017-08-06 01:00:49 -04005728 error = ext4_journal_get_write_access(handle, iloc->bh);
5729 if (error) {
5730 brelse(iloc->bh);
Dan Carpenter7f420d642019-12-13 21:50:11 +03005731 goto out_unlock;
Miao Xiec03b45b2017-08-06 01:00:49 -04005732 }
5733
5734 error = __ext4_expand_extra_isize(inode, new_extra_isize, iloc,
5735 handle, &no_expand);
5736
5737 rc = ext4_mark_iloc_dirty(handle, inode, iloc);
5738 if (!error)
5739 error = rc;
5740
Dan Carpenter7f420d642019-12-13 21:50:11 +03005741out_unlock:
Miao Xiec03b45b2017-08-06 01:00:49 -04005742 ext4_write_unlock_xattr(inode, &no_expand);
Miao Xiec03b45b2017-08-06 01:00:49 -04005743 ext4_journal_stop(handle);
5744 return error;
5745}
5746
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005747/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005748 * What we do here is to mark the in-core inode as clean with respect to inode
5749 * dirtiness (it may still be data-dirty).
5750 * This means that the in-core inode may be reaped by prune_icache
5751 * without having to perform any I/O. This is a very good thing,
5752 * because *any* task may call prune_icache - even ones which
5753 * have a transaction open against a different journal.
5754 *
5755 * Is this cheating? Not really. Sure, we haven't written the
5756 * inode out, but prune_icache isn't a user-visible syncing function.
5757 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5758 * we start and wait on commits.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005759 */
Mingming Cao617ba132006-10-11 01:20:53 -07005760int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005761{
Mingming Cao617ba132006-10-11 01:20:53 -07005762 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005763 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Miao Xiecf0a5e82017-08-06 00:40:01 -04005764 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005765
5766 might_sleep();
Theodore Ts'o7ff9c072010-11-08 13:51:33 -05005767 trace_ext4_mark_inode_dirty(inode, _RET_IP_);
Mingming Cao617ba132006-10-11 01:20:53 -07005768 err = ext4_reserve_inode_write(handle, inode, &iloc);
Eryu Guan5e1021f2016-03-12 21:40:32 -05005769 if (err)
5770 return err;
Miao Xiecf0a5e82017-08-06 00:40:01 -04005771
5772 if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize)
5773 ext4_try_to_expand_extra_isize(inode, sbi->s_want_extra_isize,
5774 iloc, handle);
5775
Eryu Guan5e1021f2016-03-12 21:40:32 -05005776 return ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005777}
5778
5779/*
Mingming Cao617ba132006-10-11 01:20:53 -07005780 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005781 *
5782 * We're really interested in the case where a file is being extended.
5783 * i_size has been changed by generic_commit_write() and we thus need
5784 * to include the updated inode in the current transaction.
5785 *
Christoph Hellwig5dd40562010-03-03 09:05:00 -05005786 * Also, dquot_alloc_block() will always dirty the inode when blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005787 * are allocated to the file.
5788 *
5789 * If the inode is marked synchronous, we don't honour that here - doing
5790 * so would cause a commit on atime updates, which we don't bother doing.
5791 * We handle synchronous inodes at the highest possible level.
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05005792 *
5793 * If only the I_DIRTY_TIME flag is set, we can skip everything. If
5794 * I_DIRTY_TIME and I_DIRTY_SYNC is set, the only inode fields we need
5795 * to copy into the on-disk inode structure are the timestamp files.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005796 */
Christoph Hellwigaa385722011-05-27 06:53:02 -04005797void ext4_dirty_inode(struct inode *inode, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005798{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005799 handle_t *handle;
5800
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05005801 if (flags == I_DIRTY_TIME)
5802 return;
Theodore Ts'o9924a922013-02-08 21:59:22 -05005803 handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005804 if (IS_ERR(handle))
5805 goto out;
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005806
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005807 ext4_mark_inode_dirty(handle, inode);
5808
Mingming Cao617ba132006-10-11 01:20:53 -07005809 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005810out:
5811 return;
5812}
5813
Mingming Cao617ba132006-10-11 01:20:53 -07005814int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005815{
5816 journal_t *journal;
5817 handle_t *handle;
5818 int err;
Daeho Jeongc8585c62016-04-25 23:22:35 -04005819 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005820
5821 /*
5822 * We have to be very careful here: changing a data block's
5823 * journaling status dynamically is dangerous. If we write a
5824 * data block to the journal, change the status and then delete
5825 * that block, we risk forgetting to revoke the old log record
5826 * from the journal and so a subsequent replay can corrupt data.
5827 * So, first we make sure that the journal is empty and that
5828 * nobody is changing anything.
5829 */
5830
Mingming Cao617ba132006-10-11 01:20:53 -07005831 journal = EXT4_JOURNAL(inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005832 if (!journal)
5833 return 0;
Dave Hansend6995942007-07-18 08:33:51 -04005834 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005835 return -EROFS;
5836
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04005837 /* Wait for all existing dio workers */
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04005838 inode_dio_wait(inode);
5839
Daeho Jeong4c546592016-04-25 23:21:00 -04005840 /*
5841 * Before flushing the journal and switching inode's aops, we have
5842 * to flush all dirty data the inode has. There can be outstanding
5843 * delayed allocations, there can be unwritten extents created by
5844 * fallocate or buffered writes in dioread_nolock mode covered by
5845 * dirty data which can be converted only after flushing the dirty
5846 * data (and journalled aops don't know how to handle these cases).
5847 */
5848 if (val) {
5849 down_write(&EXT4_I(inode)->i_mmap_sem);
5850 err = filemap_write_and_wait(inode->i_mapping);
5851 if (err < 0) {
5852 up_write(&EXT4_I(inode)->i_mmap_sem);
Daeho Jeong4c546592016-04-25 23:21:00 -04005853 return err;
5854 }
5855 }
5856
Daeho Jeongc8585c62016-04-25 23:22:35 -04005857 percpu_down_write(&sbi->s_journal_flag_rwsem);
Mingming Caodab291a2006-10-11 01:21:01 -07005858 jbd2_journal_lock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005859
5860 /*
5861 * OK, there are no updates running now, and all cached data is
5862 * synced to disk. We are now in a completely consistent state
5863 * which doesn't have anything in the journal, and we know that
5864 * no filesystem updates are running, so it is safe to modify
5865 * the inode's in-core data-journaling state flag now.
5866 */
5867
5868 if (val)
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005869 ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Yongqiang Yang5872dda2011-12-28 13:55:51 -05005870 else {
Jan Kara4f879ca2014-10-30 10:53:17 -04005871 err = jbd2_journal_flush(journal);
5872 if (err < 0) {
5873 jbd2_journal_unlock_updates(journal);
Daeho Jeongc8585c62016-04-25 23:22:35 -04005874 percpu_up_write(&sbi->s_journal_flag_rwsem);
Jan Kara4f879ca2014-10-30 10:53:17 -04005875 return err;
5876 }
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005877 ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Yongqiang Yang5872dda2011-12-28 13:55:51 -05005878 }
Mingming Cao617ba132006-10-11 01:20:53 -07005879 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005880
Mingming Caodab291a2006-10-11 01:21:01 -07005881 jbd2_journal_unlock_updates(journal);
Daeho Jeongc8585c62016-04-25 23:22:35 -04005882 percpu_up_write(&sbi->s_journal_flag_rwsem);
5883
Daeho Jeong4c546592016-04-25 23:21:00 -04005884 if (val)
5885 up_write(&EXT4_I(inode)->i_mmap_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005886
5887 /* Finally we can mark the inode as dirty. */
5888
Theodore Ts'o9924a922013-02-08 21:59:22 -05005889 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005890 if (IS_ERR(handle))
5891 return PTR_ERR(handle);
5892
Mingming Cao617ba132006-10-11 01:20:53 -07005893 err = ext4_mark_inode_dirty(handle, inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005894 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005895 ext4_journal_stop(handle);
5896 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005897
5898 return err;
5899}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005900
5901static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
5902{
5903 return !buffer_mapped(bh);
5904}
5905
Souptick Joarder401b25a2018-10-02 22:20:50 -04005906vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf)
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005907{
Dave Jiang11bac802017-02-24 14:56:41 -08005908 struct vm_area_struct *vma = vmf->vma;
Nick Pigginc2ec1752009-03-31 15:23:21 -07005909 struct page *page = vmf->page;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005910 loff_t size;
5911 unsigned long len;
Souptick Joarder401b25a2018-10-02 22:20:50 -04005912 int err;
5913 vm_fault_t ret;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005914 struct file *file = vma->vm_file;
Al Viro496ad9a2013-01-23 17:07:38 -05005915 struct inode *inode = file_inode(file);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005916 struct address_space *mapping = inode->i_mapping;
Jan Kara9ea7df52011-06-24 14:29:41 -04005917 handle_t *handle;
5918 get_block_t *get_block;
5919 int retries = 0;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005920
Theodore Ts'o02b016c2019-06-09 22:04:33 -04005921 if (unlikely(IS_IMMUTABLE(inode)))
5922 return VM_FAULT_SIGBUS;
5923
Jan Kara8e8ad8a2012-06-12 16:20:38 +02005924 sb_start_pagefault(inode->i_sb);
Theodore Ts'o041bbb6d2012-09-30 23:04:56 -04005925 file_update_time(vma->vm_file);
Jan Karaea3d7202015-12-07 14:28:03 -05005926
5927 down_read(&EXT4_I(inode)->i_mmap_sem);
Eric Biggers7b4cc972017-04-30 00:10:50 -04005928
Souptick Joarder401b25a2018-10-02 22:20:50 -04005929 err = ext4_convert_inline_data(inode);
5930 if (err)
Eric Biggers7b4cc972017-04-30 00:10:50 -04005931 goto out_ret;
5932
Jan Kara9ea7df52011-06-24 14:29:41 -04005933 /* Delalloc case is easy... */
5934 if (test_opt(inode->i_sb, DELALLOC) &&
5935 !ext4_should_journal_data(inode) &&
5936 !ext4_nonda_switch(inode->i_sb)) {
5937 do {
Souptick Joarder401b25a2018-10-02 22:20:50 -04005938 err = block_page_mkwrite(vma, vmf,
Jan Kara9ea7df52011-06-24 14:29:41 -04005939 ext4_da_get_block_prep);
Souptick Joarder401b25a2018-10-02 22:20:50 -04005940 } while (err == -ENOSPC &&
Jan Kara9ea7df52011-06-24 14:29:41 -04005941 ext4_should_retry_alloc(inode->i_sb, &retries));
5942 goto out_ret;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005943 }
Darrick J. Wong0e499892011-05-18 13:55:20 -04005944
5945 lock_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04005946 size = i_size_read(inode);
5947 /* Page got truncated from under us? */
5948 if (page->mapping != mapping || page_offset(page) > size) {
5949 unlock_page(page);
5950 ret = VM_FAULT_NOPAGE;
5951 goto out;
Darrick J. Wong0e499892011-05-18 13:55:20 -04005952 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005953
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005954 if (page->index == size >> PAGE_SHIFT)
5955 len = size & ~PAGE_MASK;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005956 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005957 len = PAGE_SIZE;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005958 /*
Jan Kara9ea7df52011-06-24 14:29:41 -04005959 * Return if we have all the buffers mapped. This avoids the need to do
5960 * journal_start/journal_stop which can block and take a long time
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005961 */
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005962 if (page_has_buffers(page)) {
Tao Maf19d5872012-12-10 14:05:51 -05005963 if (!ext4_walk_page_buffers(NULL, page_buffers(page),
5964 0, len, NULL,
5965 ext4_bh_unmapped)) {
Jan Kara9ea7df52011-06-24 14:29:41 -04005966 /* Wait so that we don't change page under IO */
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08005967 wait_for_stable_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04005968 ret = VM_FAULT_LOCKED;
5969 goto out;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005970 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005971 }
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005972 unlock_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04005973 /* OK, we need to fill the hole... */
5974 if (ext4_should_dioread_nolock(inode))
Jan Kara705965b2016-03-08 23:08:10 -05005975 get_block = ext4_get_block_unwritten;
Jan Kara9ea7df52011-06-24 14:29:41 -04005976 else
5977 get_block = ext4_get_block;
5978retry_alloc:
Theodore Ts'o9924a922013-02-08 21:59:22 -05005979 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
5980 ext4_writepage_trans_blocks(inode));
Jan Kara9ea7df52011-06-24 14:29:41 -04005981 if (IS_ERR(handle)) {
Nick Pigginc2ec1752009-03-31 15:23:21 -07005982 ret = VM_FAULT_SIGBUS;
Jan Kara9ea7df52011-06-24 14:29:41 -04005983 goto out;
5984 }
Souptick Joarder401b25a2018-10-02 22:20:50 -04005985 err = block_page_mkwrite(vma, vmf, get_block);
5986 if (!err && ext4_should_journal_data(inode)) {
Tao Maf19d5872012-12-10 14:05:51 -05005987 if (ext4_walk_page_buffers(handle, page_buffers(page), 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005988 PAGE_SIZE, NULL, do_journal_get_write_access)) {
Jan Kara9ea7df52011-06-24 14:29:41 -04005989 unlock_page(page);
5990 ret = VM_FAULT_SIGBUS;
Yongqiang Yangfcbb5512011-10-26 05:00:19 -04005991 ext4_journal_stop(handle);
Jan Kara9ea7df52011-06-24 14:29:41 -04005992 goto out;
5993 }
5994 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
5995 }
5996 ext4_journal_stop(handle);
Souptick Joarder401b25a2018-10-02 22:20:50 -04005997 if (err == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
Jan Kara9ea7df52011-06-24 14:29:41 -04005998 goto retry_alloc;
5999out_ret:
Souptick Joarder401b25a2018-10-02 22:20:50 -04006000 ret = block_page_mkwrite_return(err);
Jan Kara9ea7df52011-06-24 14:29:41 -04006001out:
Jan Karaea3d7202015-12-07 14:28:03 -05006002 up_read(&EXT4_I(inode)->i_mmap_sem);
Jan Kara8e8ad8a2012-06-12 16:20:38 +02006003 sb_end_pagefault(inode->i_sb);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04006004 return ret;
6005}
Jan Karaea3d7202015-12-07 14:28:03 -05006006
Souptick Joarder401b25a2018-10-02 22:20:50 -04006007vm_fault_t ext4_filemap_fault(struct vm_fault *vmf)
Jan Karaea3d7202015-12-07 14:28:03 -05006008{
Dave Jiang11bac802017-02-24 14:56:41 -08006009 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarder401b25a2018-10-02 22:20:50 -04006010 vm_fault_t ret;
Jan Karaea3d7202015-12-07 14:28:03 -05006011
6012 down_read(&EXT4_I(inode)->i_mmap_sem);
Souptick Joarder401b25a2018-10-02 22:20:50 -04006013 ret = filemap_fault(vmf);
Jan Karaea3d7202015-12-07 14:28:03 -05006014 up_read(&EXT4_I(inode)->i_mmap_sem);
6015
Souptick Joarder401b25a2018-10-02 22:20:50 -04006016 return ret;
Jan Karaea3d7202015-12-07 14:28:03 -05006017}