blob: 62e9165bc69c06825c531bdf32c9bc1e2acdd277 [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>
Christian Brauner14f3db52021-01-21 14:19:57 +010023#include <linux/mount.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070024#include <linux/time.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070025#include <linux/highuid.h>
26#include <linux/pagemap.h>
Matthew Wilcoxc94c2ac2015-09-08 14:58:40 -070027#include <linux/dax.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070028#include <linux/quotaops.h>
29#include <linux/string.h>
30#include <linux/buffer_head.h>
31#include <linux/writeback.h>
Alex Tomas64769242008-07-11 19:27:31 -040032#include <linux/pagevec.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070033#include <linux/mpage.h>
Duane Griffine83c1392008-12-19 20:47:15 +000034#include <linux/namei.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070035#include <linux/uio.h>
36#include <linux/bio.h>
Mingming Cao4c0425f2009-09-28 15:48:41 -040037#include <linux/workqueue.h>
Jiaying Zhang744692d2010-03-04 16:14:02 -050038#include <linux/kernel.h>
Andrew Morton6db26ff2011-01-12 16:59:13 -080039#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Theodore Ts'o00a1a052014-03-30 10:20:01 -040041#include <linux/bitops.h>
Jan Kara364443c2016-11-20 17:36:06 -050042#include <linux/iomap.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050043#include <linux/iversion.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040044
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040045#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070046#include "xattr.h"
47#include "acl.h"
Theodore Ts'o9f125d62011-06-27 19:16:04 -040048#include "truncate.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070049
Theodore Ts'o9bffad12009-06-17 11:48:11 -040050#include <trace/events/ext4.h>
51
Darrick J. Wong814525f2012-04-29 18:31:10 -040052static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw,
53 struct ext4_inode_info *ei)
54{
55 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Darrick J. Wong814525f2012-04-29 18:31:10 -040056 __u32 csum;
Daeho Jeongb47820e2016-07-03 17:51:39 -040057 __u16 dummy_csum = 0;
58 int offset = offsetof(struct ext4_inode, i_checksum_lo);
59 unsigned int csum_size = sizeof(dummy_csum);
Darrick J. Wong814525f2012-04-29 18:31:10 -040060
Daeho Jeongb47820e2016-07-03 17:51:39 -040061 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, offset);
62 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, csum_size);
63 offset += csum_size;
64 csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset,
65 EXT4_GOOD_OLD_INODE_SIZE - offset);
66
67 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
68 offset = offsetof(struct ext4_inode, i_checksum_hi);
69 csum = ext4_chksum(sbi, csum, (__u8 *)raw +
70 EXT4_GOOD_OLD_INODE_SIZE,
71 offset - EXT4_GOOD_OLD_INODE_SIZE);
72 if (EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) {
73 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum,
74 csum_size);
75 offset += csum_size;
Daeho Jeongb47820e2016-07-03 17:51:39 -040076 }
Daeho Jeong05ac5aa2016-12-01 11:49:12 -050077 csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset,
78 EXT4_INODE_SIZE(inode->i_sb) - offset);
Darrick J. Wong814525f2012-04-29 18:31:10 -040079 }
80
Darrick J. Wong814525f2012-04-29 18:31:10 -040081 return csum;
82}
83
84static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw,
85 struct ext4_inode_info *ei)
86{
87 __u32 provided, calculated;
88
89 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
90 cpu_to_le32(EXT4_OS_LINUX) ||
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -040091 !ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong814525f2012-04-29 18:31:10 -040092 return 1;
93
94 provided = le16_to_cpu(raw->i_checksum_lo);
95 calculated = ext4_inode_csum(inode, raw, ei);
96 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
97 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
98 provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16;
99 else
100 calculated &= 0xFFFF;
101
102 return provided == calculated;
103}
104
Harshad Shirwadkar8016e292020-10-15 13:37:59 -0700105void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
106 struct ext4_inode_info *ei)
Darrick J. Wong814525f2012-04-29 18:31:10 -0400107{
108 __u32 csum;
109
110 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
111 cpu_to_le32(EXT4_OS_LINUX) ||
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400112 !ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong814525f2012-04-29 18:31:10 -0400113 return;
114
115 csum = ext4_inode_csum(inode, raw, ei);
116 raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF);
117 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
118 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
119 raw->i_checksum_hi = cpu_to_le16(csum >> 16);
120}
121
Jan Kara678aaf42008-07-11 19:27:31 -0400122static inline int ext4_begin_ordered_truncate(struct inode *inode,
123 loff_t new_size)
124{
Theodore Ts'o7ff9c072010-11-08 13:51:33 -0500125 trace_ext4_begin_ordered_truncate(inode, new_size);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -0500126 /*
127 * If jinode is zero, then we never opened the file for
128 * writing, so there's no need to call
129 * jbd2_journal_begin_ordered_truncate() since there's no
130 * outstanding writes we need to flush.
131 */
132 if (!EXT4_I(inode)->jinode)
133 return 0;
134 return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
135 EXT4_I(inode)->jinode,
136 new_size);
Jan Kara678aaf42008-07-11 19:27:31 -0400137}
138
Lukas Czernerd47992f2013-05-21 23:17:23 -0400139static void ext4_invalidatepage(struct page *page, unsigned int offset,
140 unsigned int length);
Theodore Ts'ocb20d512010-10-27 21:30:09 -0400141static int __ext4_journalled_writepage(struct page *page, unsigned int len);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400142static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
143 int pextents);
Alex Tomas64769242008-07-11 19:27:31 -0400144
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700145/*
146 * Test whether an inode is a fast symlink.
Tahsin Erdogan407cd7f2017-07-04 00:11:21 -0400147 * A fast symlink has its symlink data stored in ext4_inode_info->i_data.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700148 */
Theodore Ts'of348c252015-04-16 01:55:00 -0400149int ext4_inode_is_fast_symlink(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700150{
Andi Kleenfc822282017-12-03 20:38:01 -0500151 if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
152 int ea_blocks = EXT4_I(inode)->i_file_acl ?
153 EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0;
154
155 if (ext4_has_inline_data(inode))
156 return 0;
157
158 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
159 }
Tahsin Erdogan407cd7f2017-07-04 00:11:21 -0400160 return S_ISLNK(inode->i_mode) && inode->i_size &&
161 (inode->i_size < EXT4_N_BLOCKS * 4);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700162}
163
164/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700165 * Called at the last iput() if i_nlink is zero.
166 */
Al Viro0930fcc2010-06-07 13:16:22 -0400167void ext4_evict_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700168{
169 handle_t *handle;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400170 int err;
Jan Kara65db8692019-11-05 17:44:12 +0100171 /*
172 * Credits for final inode cleanup and freeing:
173 * sb + inode (ext4_orphan_del()), block bitmap, group descriptor
174 * (xattr block freeing), bitmap, group descriptor (inode freeing)
175 */
176 int extra_credits = 6;
Tahsin Erdogan0421a182017-06-22 10:26:31 -0400177 struct ext4_xattr_inode_array *ea_inode_array = NULL;
Jan Kara46e294e2020-11-27 12:06:49 +0100178 bool freeze_protected = false;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700179
Theodore Ts'o7ff9c072010-11-08 13:51:33 -0500180 trace_ext4_evict_inode(inode);
Jiaying Zhang2581fdc2011-08-13 12:17:13 -0400181
Al Viro0930fcc2010-06-07 13:16:22 -0400182 if (inode->i_nlink) {
Jan Kara2d859db2011-07-26 09:07:11 -0400183 /*
184 * When journalling data dirty buffers are tracked only in the
185 * journal. So although mm thinks everything is clean and
186 * ready for reaping the inode might still have some pages to
187 * write in the running transaction or waiting to be
188 * checkpointed. Thus calling jbd2_journal_invalidatepage()
189 * (via truncate_inode_pages()) to discard these buffers can
190 * cause data loss. Also even if we did not discard these
191 * buffers, we would have no way to find them after the inode
192 * is reaped and thus user could see stale data if he tries to
193 * read them before the transaction is checkpointed. So be
194 * careful and force everything to disk here... We use
195 * ei->i_datasync_tid to store the newest transaction
196 * containing inode's data.
197 *
198 * Note that directories do not have this problem because they
199 * don't use page cache.
200 */
Vegard Nossum6a7fd522016-07-04 11:03:00 -0400201 if (inode->i_ino != EXT4_JOURNAL_INO &&
202 ext4_should_journal_data(inode) &&
Jan Kara3abb1a02017-06-22 23:49:46 -0400203 (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode)) &&
204 inode->i_data.nrpages) {
Jan Kara2d859db2011-07-26 09:07:11 -0400205 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
206 tid_t commit_tid = EXT4_I(inode)->i_datasync_tid;
207
Theodore Ts'od76a3a772013-04-03 22:02:52 -0400208 jbd2_complete_transaction(journal, commit_tid);
Jan Kara2d859db2011-07-26 09:07:11 -0400209 filemap_write_and_wait(&inode->i_data);
210 }
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700211 truncate_inode_pages_final(&inode->i_data);
Jan Kara5dc23bd2013-06-04 14:46:12 -0400212
Al Viro0930fcc2010-06-07 13:16:22 -0400213 goto no_delete;
214 }
215
Theodore Ts'oe2bfb082014-10-05 22:47:07 -0400216 if (is_bad_inode(inode))
217 goto no_delete;
218 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500219
Jan Kara678aaf42008-07-11 19:27:31 -0400220 if (ext4_should_order_data(inode))
221 ext4_begin_ordered_truncate(inode, 0);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700222 truncate_inode_pages_final(&inode->i_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700223
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200224 /*
Jan Karaceff86f2020-04-21 10:54:45 +0200225 * For inodes with journalled data, transaction commit could have
226 * dirtied the inode. Flush worker is ignoring it because of I_FREEING
227 * flag but we still need to remove the inode from the writeback lists.
228 */
229 if (!list_empty_careful(&inode->i_io_list)) {
230 WARN_ON_ONCE(!ext4_should_journal_data(inode));
231 inode_io_list_del(inode);
232 }
233
234 /*
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200235 * Protect us against freezing - iput() caller didn't have to have any
Jan Kara46e294e2020-11-27 12:06:49 +0100236 * protection against it. When we are in a running transaction though,
237 * we are already protected against freezing and we cannot grab further
238 * protection due to lock ordering constraints.
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200239 */
Jan Kara46e294e2020-11-27 12:06:49 +0100240 if (!ext4_journal_current_handle()) {
241 sb_start_intwrite(inode->i_sb);
242 freeze_protected = true;
243 }
Andreas Dilgere50e5122017-06-21 21:10:32 -0400244
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400245 if (!IS_NOQUOTA(inode))
246 extra_credits += EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb);
247
Jan Kara65db8692019-11-05 17:44:12 +0100248 /*
249 * Block bitmap, group descriptor, and inode are accounted in both
250 * ext4_blocks_for_truncate() and extra_credits. So subtract 3.
251 */
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400252 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
Jan Kara65db8692019-11-05 17:44:12 +0100253 ext4_blocks_for_truncate(inode) + extra_credits - 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700254 if (IS_ERR(handle)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400255 ext4_std_error(inode->i_sb, PTR_ERR(handle));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700256 /*
257 * If we're going to skip the normal cleanup, we still need to
258 * make sure that the in-core orphan linked list is properly
259 * cleaned up.
260 */
Mingming Cao617ba132006-10-11 01:20:53 -0700261 ext4_orphan_del(NULL, inode);
Jan Kara46e294e2020-11-27 12:06:49 +0100262 if (freeze_protected)
263 sb_end_intwrite(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700264 goto no_delete;
265 }
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400266
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700267 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500268 ext4_handle_sync(handle);
Tahsin Erdogan407cd7f2017-07-04 00:11:21 -0400269
270 /*
271 * Set inode->i_size to 0 before calling ext4_truncate(). We need
272 * special handling of symlinks here because i_size is used to
273 * determine whether ext4_inode_info->i_data contains symlink data or
274 * block mappings. Setting i_size to 0 will remove its fast symlink
275 * status. Erase i_data so that it becomes a valid empty block map.
276 */
277 if (ext4_inode_is_fast_symlink(inode))
278 memset(EXT4_I(inode)->i_data, 0, sizeof(EXT4_I(inode)->i_data));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700279 inode->i_size = 0;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400280 err = ext4_mark_inode_dirty(handle, inode);
281 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500282 ext4_warning(inode->i_sb,
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400283 "couldn't mark inode dirty (err %d)", err);
284 goto stop_handle;
285 }
Theodore Ts'o2c98eb52016-11-13 22:02:26 -0500286 if (inode->i_blocks) {
287 err = ext4_truncate(inode);
288 if (err) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400289 ext4_error_err(inode->i_sb, -err,
290 "couldn't truncate inode %lu (err %d)",
291 inode->i_ino, err);
Theodore Ts'o2c98eb52016-11-13 22:02:26 -0500292 goto stop_handle;
293 }
294 }
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400295
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400296 /* Remove xattr references. */
297 err = ext4_xattr_delete_inode(handle, inode, &ea_inode_array,
298 extra_credits);
299 if (err) {
300 ext4_warning(inode->i_sb, "xattr delete (err %d)", err);
301stop_handle:
302 ext4_journal_stop(handle);
303 ext4_orphan_del(NULL, inode);
Jan Kara46e294e2020-11-27 12:06:49 +0100304 if (freeze_protected)
305 sb_end_intwrite(inode->i_sb);
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400306 ext4_xattr_inode_array_free(ea_inode_array);
307 goto no_delete;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400308 }
309
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700310 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700311 * Kill off the orphan record which ext4_truncate created.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700312 * AKPM: I think this can be inside the above `if'.
Mingming Cao617ba132006-10-11 01:20:53 -0700313 * Note that ext4_orphan_del() has to be able to cope with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700314 * deletion of a non-existent orphan - this is because we don't
Mingming Cao617ba132006-10-11 01:20:53 -0700315 * know if ext4_truncate() actually created an orphan record.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700316 * (Well, we could do this if we need to, but heck - it works)
317 */
Mingming Cao617ba132006-10-11 01:20:53 -0700318 ext4_orphan_del(handle, inode);
Arnd Bergmann5ffff832018-07-29 15:50:00 -0400319 EXT4_I(inode)->i_dtime = (__u32)ktime_get_real_seconds();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700320
321 /*
322 * One subtle ordering requirement: if anything has gone wrong
323 * (transaction abort, IO errors, whatever), then we can still
324 * do these next steps (the fs will already have been marked as
325 * having errors), but we can't free the inode if the mark_dirty
326 * fails.
327 */
Mingming Cao617ba132006-10-11 01:20:53 -0700328 if (ext4_mark_inode_dirty(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700329 /* If that failed, just do the required in-core inode clear. */
Al Viro0930fcc2010-06-07 13:16:22 -0400330 ext4_clear_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700331 else
Mingming Cao617ba132006-10-11 01:20:53 -0700332 ext4_free_inode(handle, inode);
333 ext4_journal_stop(handle);
Jan Kara46e294e2020-11-27 12:06:49 +0100334 if (freeze_protected)
335 sb_end_intwrite(inode->i_sb);
Tahsin Erdogan0421a182017-06-22 10:26:31 -0400336 ext4_xattr_inode_array_free(ea_inode_array);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700337 return;
338no_delete:
Harshad Shirwadkarb21ebf12020-11-05 19:58:51 -0800339 if (!list_empty(&EXT4_I(inode)->i_fc_list))
340 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_NOMEM);
Al Viro0930fcc2010-06-07 13:16:22 -0400341 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700342}
343
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300344#ifdef CONFIG_QUOTA
345qsize_t *ext4_get_reserved_space(struct inode *inode)
Mingming Cao60e58e02009-01-22 18:13:05 +0100346{
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300347 return &EXT4_I(inode)->i_reserved_quota;
Mingming Cao60e58e02009-01-22 18:13:05 +0100348}
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300349#endif
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500350
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400351/*
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500352 * Called with i_data_sem down, which is important since we can call
353 * ext4_discard_preallocations() from here.
354 */
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500355void ext4_da_update_reserve_space(struct inode *inode,
356 int used, int quota_claim)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400357{
358 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500359 struct ext4_inode_info *ei = EXT4_I(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400360
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500361 spin_lock(&ei->i_block_reservation_lock);
Aditya Kalid8990242011-09-09 19:18:51 -0400362 trace_ext4_da_update_reserve_space(inode, used, quota_claim);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500363 if (unlikely(used > ei->i_reserved_data_blocks)) {
Theodore Ts'o8de5c322013-02-14 15:11:41 -0500364 ext4_warning(inode->i_sb, "%s: ino %lu, used %d "
Theodore Ts'o1084f252012-03-19 23:13:43 -0400365 "with only %d reserved data blocks",
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500366 __func__, inode->i_ino, used,
367 ei->i_reserved_data_blocks);
368 WARN_ON(1);
369 used = ei->i_reserved_data_blocks;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400370 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400371
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500372 /* Update per-inode reservations */
373 ei->i_reserved_data_blocks -= used;
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -0400374 percpu_counter_sub(&sbi->s_dirtyclusters_counter, used);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500375
chenyichongf9505c72021-05-26 13:29:30 +0800376 spin_unlock(&ei->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +0100377
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400378 /* Update quota subsystem for data blocks */
379 if (quota_claim)
Aditya Kali7b415bf2011-09-09 19:04:51 -0400380 dquot_claim_block(inode, EXT4_C2B(sbi, used));
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400381 else {
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500382 /*
383 * We did fallocate with an offset that is already delayed
384 * allocated. So on delayed allocated writeback we should
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400385 * not re-claim the quota for fallocated blocks.
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500386 */
Aditya Kali7b415bf2011-09-09 19:04:51 -0400387 dquot_release_reservation_block(inode, EXT4_C2B(sbi, used));
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500388 }
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -0400389
390 /*
391 * If we have done all the pending block allocations and if
392 * there aren't any writers on the inode, we can discard the
393 * inode's preallocations.
394 */
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500395 if ((ei->i_reserved_data_blocks == 0) &&
Nikolay Borisov82dd1242019-02-10 23:04:16 -0500396 !inode_is_open_for_write(inode))
brookxu27bc4462020-08-17 15:36:15 +0800397 ext4_discard_preallocations(inode, 0);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400398}
399
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400400static int __check_block_validity(struct inode *inode, const char *func,
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400401 unsigned int line,
402 struct ext4_map_blocks *map)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400403{
Theodore Ts'o345c0db2019-04-09 23:37:08 -0400404 if (ext4_has_feature_journal(inode->i_sb) &&
405 (inode->i_ino ==
406 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum)))
407 return 0;
Jan Karace9f24c2020-07-28 15:04:34 +0200408 if (!ext4_inode_block_valid(inode, map->m_pblk, map->m_len)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400409 ext4_error_inode(inode, func, line, map->m_pblk,
Theodore Ts'obdbd6ce2018-06-15 12:27:16 -0400410 "lblock %lu mapped to illegal pblock %llu "
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400411 "(length %d)", (unsigned long) map->m_lblk,
Theodore Ts'obdbd6ce2018-06-15 12:27:16 -0400412 map->m_pblk, map->m_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400413 return -EFSCORRUPTED;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400414 }
415 return 0;
416}
417
Jan Kara53085fa2015-12-07 15:09:35 -0500418int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk,
419 ext4_lblk_t len)
420{
421 int ret;
422
Eric Biggers33b4cc22019-12-26 10:10:22 -0600423 if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400424 return fscrypt_zeroout_range(inode, lblk, pblk, len);
Jan Kara53085fa2015-12-07 15:09:35 -0500425
426 ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS);
427 if (ret > 0)
428 ret = 0;
429
430 return ret;
431}
432
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400433#define check_block_validity(inode, map) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400434 __check_block_validity((inode), __func__, __LINE__, (map))
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400435
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400436#ifdef ES_AGGRESSIVE_TEST
437static void ext4_map_blocks_es_recheck(handle_t *handle,
438 struct inode *inode,
439 struct ext4_map_blocks *es_map,
440 struct ext4_map_blocks *map,
441 int flags)
442{
443 int retval;
444
445 map->m_flags = 0;
446 /*
447 * There is a race window that the result is not the same.
448 * e.g. xfstests #223 when dioread_nolock enables. The reason
449 * is that we lookup a block mapping in extent status tree with
450 * out taking i_data_sem. So at the time the unwritten extent
451 * could be converted.
452 */
Jan Kara2dcba472015-12-07 15:04:57 -0500453 down_read(&EXT4_I(inode)->i_data_sem);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400454 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Eric Whitney9e524842020-04-15 16:31:39 -0400455 retval = ext4_ext_map_blocks(handle, inode, map, 0);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400456 } else {
Eric Whitney9e524842020-04-15 16:31:39 -0400457 retval = ext4_ind_map_blocks(handle, inode, map, 0);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400458 }
Jan Kara2dcba472015-12-07 15:04:57 -0500459 up_read((&EXT4_I(inode)->i_data_sem));
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400460
461 /*
462 * We don't check m_len because extent will be collpased in status
463 * tree. So the m_len might not equal.
464 */
465 if (es_map->m_lblk != map->m_lblk ||
466 es_map->m_flags != map->m_flags ||
467 es_map->m_pblk != map->m_pblk) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400468 printk("ES cache assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400469 "es_cached ex [%d/%d/%llu/%x] != "
470 "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
471 inode->i_ino, es_map->m_lblk, es_map->m_len,
472 es_map->m_pblk, es_map->m_flags, map->m_lblk,
473 map->m_len, map->m_pblk, map->m_flags,
474 retval, flags);
475 }
476}
477#endif /* ES_AGGRESSIVE_TEST */
478
Theodore Ts'o55138e0b2009-09-29 13:31:31 -0400479/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400480 * The ext4_map_blocks() function tries to look up the requested blocks,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400481 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500482 *
Mingming Caof5ab0d12008-02-25 15:29:55 -0500483 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
484 * and store the allocated blocks in the result buffer head and mark it
485 * mapped.
486 *
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400487 * If file type is extents based, it will call ext4_ext_map_blocks(),
488 * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
Mingming Caof5ab0d12008-02-25 15:29:55 -0500489 * based files
490 *
Jan Karafacab4d2016-03-09 22:54:00 -0500491 * On success, it returns the number of blocks being mapped or allocated. if
492 * create==0 and the blocks are pre-allocated and unwritten, the resulting @map
493 * is marked as unwritten. If the create == 1, it will mark @map as mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500494 *
495 * It returns 0 if plain look up failed (blocks have not been allocated), in
Jan Karafacab4d2016-03-09 22:54:00 -0500496 * that case, @map is returned as unmapped but we still do fill map->m_len to
497 * indicate the length of a hole starting at map->m_lblk.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500498 *
499 * It returns the error in case of allocation failure.
500 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400501int ext4_map_blocks(handle_t *handle, struct inode *inode,
502 struct ext4_map_blocks *map, int flags)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500503{
Zheng Liud100eef2013-02-18 00:29:59 -0500504 struct extent_status es;
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500505 int retval;
Lukas Czernerb8a86842014-03-18 18:05:35 -0400506 int ret = 0;
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400507#ifdef ES_AGGRESSIVE_TEST
508 struct ext4_map_blocks orig_map;
509
510 memcpy(&orig_map, map, sizeof(*map));
511#endif
Mingming Caof5ab0d12008-02-25 15:29:55 -0500512
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400513 map->m_flags = 0;
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530514 ext_debug(inode, "flag 0x%x, max_blocks %u, logical block %lu\n",
515 flags, map->m_len, (unsigned long) map->m_lblk);
Zheng Liud100eef2013-02-18 00:29:59 -0500516
Theodore Ts'oe861b5e2014-02-20 12:54:05 -0500517 /*
518 * ext4_map_blocks returns an int, and m_len is an unsigned int
519 */
520 if (unlikely(map->m_len > INT_MAX))
521 map->m_len = INT_MAX;
522
Kazuya Mio4adb6ab2014-04-07 10:53:28 -0400523 /* We can handle the block number less than EXT_MAX_BLOCKS */
524 if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400525 return -EFSCORRUPTED;
Kazuya Mio4adb6ab2014-04-07 10:53:28 -0400526
Zheng Liud100eef2013-02-18 00:29:59 -0500527 /* Lookup extent status tree firstly */
Harshad Shirwadkar8016e292020-10-15 13:37:59 -0700528 if (!(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY) &&
529 ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) {
Zheng Liud100eef2013-02-18 00:29:59 -0500530 if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
531 map->m_pblk = ext4_es_pblock(&es) +
532 map->m_lblk - es.es_lblk;
533 map->m_flags |= ext4_es_is_written(&es) ?
534 EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN;
535 retval = es.es_len - (map->m_lblk - es.es_lblk);
536 if (retval > map->m_len)
537 retval = map->m_len;
538 map->m_len = retval;
539 } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) {
Jan Karafacab4d2016-03-09 22:54:00 -0500540 map->m_pblk = 0;
541 retval = es.es_len - (map->m_lblk - es.es_lblk);
542 if (retval > map->m_len)
543 retval = map->m_len;
544 map->m_len = retval;
Zheng Liud100eef2013-02-18 00:29:59 -0500545 retval = 0;
546 } else {
Arnd Bergmann1e83bc82019-04-07 12:24:43 -0400547 BUG();
Zheng Liud100eef2013-02-18 00:29:59 -0500548 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400549#ifdef ES_AGGRESSIVE_TEST
550 ext4_map_blocks_es_recheck(handle, inode, map,
551 &orig_map, flags);
552#endif
Zheng Liud100eef2013-02-18 00:29:59 -0500553 goto found;
554 }
555
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500556 /*
Theodore Ts'ob920c752009-05-14 00:54:29 -0400557 * Try to see if we can get the block without requesting a new
558 * file system block.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500559 */
Jan Kara2dcba472015-12-07 15:04:57 -0500560 down_read(&EXT4_I(inode)->i_data_sem);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400561 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Eric Whitney9e524842020-04-15 16:31:39 -0400562 retval = ext4_ext_map_blocks(handle, inode, map, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500563 } else {
Eric Whitney9e524842020-04-15 16:31:39 -0400564 retval = ext4_ind_map_blocks(handle, inode, map, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500565 }
Zheng Liuf7fec032013-02-18 00:28:47 -0500566 if (retval > 0) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400567 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -0500568
Zheng Liu44fb851d2013-07-29 12:51:42 -0400569 if (unlikely(retval != map->m_len)) {
570 ext4_warning(inode->i_sb,
571 "ES len assertion failed for inode "
572 "%lu: retval %d != map->m_len %d",
573 inode->i_ino, retval, map->m_len);
574 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400575 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400576
Zheng Liuf7fec032013-02-18 00:28:47 -0500577 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
578 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
579 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400580 !(status & EXTENT_STATUS_WRITTEN) &&
Eric Whitneyad431022018-10-01 14:10:39 -0400581 ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk,
582 map->m_lblk + map->m_len - 1))
Zheng Liuf7fec032013-02-18 00:28:47 -0500583 status |= EXTENT_STATUS_DELAYED;
584 ret = ext4_es_insert_extent(inode, map->m_lblk,
585 map->m_len, map->m_pblk, status);
586 if (ret < 0)
587 retval = ret;
588 }
Jan Kara2dcba472015-12-07 15:04:57 -0500589 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -0500590
Zheng Liud100eef2013-02-18 00:29:59 -0500591found:
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400592 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Lukas Czernerb8a86842014-03-18 18:05:35 -0400593 ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400594 if (ret != 0)
595 return ret;
596 }
597
Mingming Caof5ab0d12008-02-25 15:29:55 -0500598 /* If it is only a block(s) look up */
Theodore Ts'oc2177052009-05-14 00:58:52 -0400599 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500600 return retval;
601
602 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -0500603 * Returns if the blocks have already allocated
604 *
605 * Note that if blocks have been preallocated
Tao Madf3ab172011-10-08 15:53:49 -0400606 * ext4_ext_get_block() returns the create = 0
Mingming Caof5ab0d12008-02-25 15:29:55 -0500607 * with buffer head unmapped.
608 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400609 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
Lukas Czernerb8a86842014-03-18 18:05:35 -0400610 /*
611 * If we need to convert extent to unwritten
612 * we continue and do the actual work in
613 * ext4_ext_map_blocks()
614 */
615 if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN))
616 return retval;
Mingming Caof5ab0d12008-02-25 15:29:55 -0500617
618 /*
Zheng Liua25a4e12013-02-18 00:28:04 -0500619 * Here we clear m_flags because after allocating an new extent,
620 * it will be set again.
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -0400621 */
Zheng Liua25a4e12013-02-18 00:28:04 -0500622 map->m_flags &= ~EXT4_MAP_FLAGS;
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -0400623
624 /*
Lukas Czerner556615d2014-04-20 23:45:47 -0400625 * New blocks allocate and/or writing to unwritten extent
Mingming Caof5ab0d12008-02-25 15:29:55 -0500626 * will possibly result in updating i_data, so we take
Seunghun Leed91bd2c2014-09-01 22:15:30 -0400627 * the write lock of i_data_sem, and call get_block()
Mingming Caof5ab0d12008-02-25 15:29:55 -0500628 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500629 */
Lukas Czernerc8b459f2014-05-12 12:55:07 -0400630 down_write(&EXT4_I(inode)->i_data_sem);
Mingming Caod2a17632008-07-14 17:52:37 -0400631
632 /*
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500633 * We need to check for EXT4 here because migrate
634 * could have changed the inode type in between
635 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400636 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400637 retval = ext4_ext_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500638 } else {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400639 retval = ext4_ind_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400640
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400641 if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400642 /*
643 * We allocated new blocks which will result in
644 * i_data's format changing. Force the migrate
645 * to fail by clearing migrate flags
646 */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500647 ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400648 }
Mingming Caod2a17632008-07-14 17:52:37 -0400649
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500650 /*
651 * Update reserved blocks/metadata blocks after successful
652 * block allocation which had been deferred till now. We don't
653 * support fallocate for non extent files. So we can update
654 * reserve space here.
655 */
656 if ((retval > 0) &&
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -0500657 (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500658 ext4_da_update_reserve_space(inode, retval, 1);
659 }
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -0400660
Zheng Liuf7fec032013-02-18 00:28:47 -0500661 if (retval > 0) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400662 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -0500663
Zheng Liu44fb851d2013-07-29 12:51:42 -0400664 if (unlikely(retval != map->m_len)) {
665 ext4_warning(inode->i_sb,
666 "ES len assertion failed for inode "
667 "%lu: retval %d != map->m_len %d",
668 inode->i_ino, retval, map->m_len);
669 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400670 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400671
Zheng Liuadb23552013-03-10 21:13:05 -0400672 /*
Jan Karac86d8db2015-12-07 15:10:26 -0500673 * We have to zeroout blocks before inserting them into extent
674 * status tree. Otherwise someone could look them up there and
Jan Kara9b623df2016-09-30 02:02:29 -0400675 * use them before they are really zeroed. We also have to
676 * unmap metadata before zeroing as otherwise writeback can
677 * overwrite zeros with stale data from block device.
Jan Karac86d8db2015-12-07 15:10:26 -0500678 */
679 if (flags & EXT4_GET_BLOCKS_ZERO &&
680 map->m_flags & EXT4_MAP_MAPPED &&
681 map->m_flags & EXT4_MAP_NEW) {
682 ret = ext4_issue_zeroout(inode, map->m_lblk,
683 map->m_pblk, map->m_len);
684 if (ret) {
685 retval = ret;
686 goto out_sem;
687 }
688 }
689
690 /*
Zheng Liuadb23552013-03-10 21:13:05 -0400691 * If the extent has been zeroed out, we don't need to update
692 * extent status tree.
693 */
694 if ((flags & EXT4_GET_BLOCKS_PRE_IO) &&
Theodore Ts'obb5835e2019-08-11 16:32:41 -0400695 ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) {
Zheng Liuadb23552013-03-10 21:13:05 -0400696 if (ext4_es_is_written(&es))
Jan Karac86d8db2015-12-07 15:10:26 -0500697 goto out_sem;
Zheng Liuadb23552013-03-10 21:13:05 -0400698 }
Zheng Liuf7fec032013-02-18 00:28:47 -0500699 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
700 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
701 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400702 !(status & EXTENT_STATUS_WRITTEN) &&
Eric Whitneyad431022018-10-01 14:10:39 -0400703 ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk,
704 map->m_lblk + map->m_len - 1))
Zheng Liuf7fec032013-02-18 00:28:47 -0500705 status |= EXTENT_STATUS_DELAYED;
706 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
707 map->m_pblk, status);
Jan Karac86d8db2015-12-07 15:10:26 -0500708 if (ret < 0) {
Zheng Liuf7fec032013-02-18 00:28:47 -0500709 retval = ret;
Jan Karac86d8db2015-12-07 15:10:26 -0500710 goto out_sem;
711 }
Aditya Kali5356f2612011-09-09 19:20:51 -0400712 }
713
Jan Karac86d8db2015-12-07 15:10:26 -0500714out_sem:
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500715 up_write((&EXT4_I(inode)->i_data_sem));
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400716 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Lukas Czernerb8a86842014-03-18 18:05:35 -0400717 ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400718 if (ret != 0)
719 return ret;
Jan Kara06bd3c32016-04-24 00:56:03 -0400720
721 /*
722 * Inodes with freshly allocated blocks where contents will be
723 * visible after transaction commit must be on transaction's
724 * ordered data list.
725 */
726 if (map->m_flags & EXT4_MAP_NEW &&
727 !(map->m_flags & EXT4_MAP_UNWRITTEN) &&
728 !(flags & EXT4_GET_BLOCKS_ZERO) &&
Tahsin Erdogan02749a42017-06-22 11:31:25 -0400729 !ext4_is_quota_file(inode) &&
Jan Kara06bd3c32016-04-24 00:56:03 -0400730 ext4_should_order_data(inode)) {
Ross Zwisler73131fb2019-06-20 17:26:26 -0400731 loff_t start_byte =
732 (loff_t)map->m_lblk << inode->i_blkbits;
733 loff_t length = (loff_t)map->m_len << inode->i_blkbits;
734
Jan Karaee0876b2016-04-24 00:56:08 -0400735 if (flags & EXT4_GET_BLOCKS_IO_SUBMIT)
Ross Zwisler73131fb2019-06-20 17:26:26 -0400736 ret = ext4_jbd2_inode_add_wait(handle, inode,
737 start_byte, length);
Jan Karaee0876b2016-04-24 00:56:08 -0400738 else
Ross Zwisler73131fb2019-06-20 17:26:26 -0400739 ret = ext4_jbd2_inode_add_write(handle, inode,
740 start_byte, length);
Jan Kara06bd3c32016-04-24 00:56:03 -0400741 if (ret)
742 return ret;
743 }
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -0800744 ext4_fc_track_range(handle, inode, map->m_lblk,
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -0700745 map->m_lblk + map->m_len - 1);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400746 }
Ritesh Harjaniec8c60b2020-05-10 11:54:52 +0530747
748 if (retval < 0)
Ritesh Harjani70aa1552020-05-10 11:54:55 +0530749 ext_debug(inode, "failed with err %d\n", retval);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500750 return retval;
751}
752
Jan Karaed8ad832016-02-19 00:18:25 -0500753/*
754 * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages
755 * we have to be careful as someone else may be manipulating b_state as well.
756 */
757static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags)
758{
759 unsigned long old_state;
760 unsigned long new_state;
761
762 flags &= EXT4_MAP_FLAGS;
763
764 /* Dummy buffer_head? Set non-atomically. */
765 if (!bh->b_page) {
766 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags;
767 return;
768 }
769 /*
770 * Someone else may be modifying b_state. Be careful! This is ugly but
771 * once we get rid of using bh as a container for mapping information
772 * to pass to / from get_block functions, this can go away.
773 */
774 do {
775 old_state = READ_ONCE(bh->b_state);
776 new_state = (old_state & ~EXT4_MAP_FLAGS) | flags;
777 } while (unlikely(
778 cmpxchg(&bh->b_state, old_state, new_state) != old_state));
779}
780
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400781static int _ext4_get_block(struct inode *inode, sector_t iblock,
782 struct buffer_head *bh, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700783{
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400784 struct ext4_map_blocks map;
Jan Karaefe70c22016-03-08 23:35:46 -0500785 int ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700786
Tao Ma46c7f252012-12-10 14:04:52 -0500787 if (ext4_has_inline_data(inode))
788 return -ERANGE;
789
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400790 map.m_lblk = iblock;
791 map.m_len = bh->b_size >> inode->i_blkbits;
792
Jan Karaefe70c22016-03-08 23:35:46 -0500793 ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map,
794 flags);
Jan Kara7fb54092008-02-10 01:08:38 -0500795 if (ret > 0) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400796 map_bh(bh, inode->i_sb, map.m_pblk);
Jan Karaed8ad832016-02-19 00:18:25 -0500797 ext4_update_bh_state(bh, map.m_flags);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400798 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
Jan Kara7fb54092008-02-10 01:08:38 -0500799 ret = 0;
Ross Zwisler547edce2016-11-08 11:30:58 +1100800 } else if (ret == 0) {
801 /* hole case, need to fill in bh->b_size */
802 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700803 }
804 return ret;
805}
806
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400807int ext4_get_block(struct inode *inode, sector_t iblock,
808 struct buffer_head *bh, int create)
809{
810 return _ext4_get_block(inode, iblock, bh,
811 create ? EXT4_GET_BLOCKS_CREATE : 0);
812}
813
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700814/*
Jan Kara705965b2016-03-08 23:08:10 -0500815 * Get block function used when preparing for buffered write if we require
816 * creating an unwritten extent if blocks haven't been allocated. The extent
817 * will be converted to written after the IO is complete.
818 */
819int ext4_get_block_unwritten(struct inode *inode, sector_t iblock,
820 struct buffer_head *bh_result, int create)
821{
822 ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n",
823 inode->i_ino, create);
824 return _ext4_get_block(inode, iblock, bh_result,
825 EXT4_GET_BLOCKS_IO_CREATE_EXT);
826}
827
Jan Karaefe70c22016-03-08 23:35:46 -0500828/* Maximum number of blocks we map for direct IO at once. */
829#define DIO_MAX_BLOCKS 4096
830
Jan Karae84dfbe2016-04-01 02:07:22 -0400831/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700832 * `handle' can be NULL if create is zero
833 */
Mingming Cao617ba132006-10-11 01:20:53 -0700834struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400835 ext4_lblk_t block, int map_flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700836{
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400837 struct ext4_map_blocks map;
838 struct buffer_head *bh;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400839 int create = map_flags & EXT4_GET_BLOCKS_CREATE;
Theodore Ts'o10560082014-08-29 20:51:32 -0400840 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700841
Chunguang Xu837c23f2020-11-07 23:58:11 +0800842 ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
843 || handle != NULL || create == 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700844
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400845 map.m_lblk = block;
846 map.m_len = 1;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400847 err = ext4_map_blocks(handle, inode, &map, map_flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700848
Theodore Ts'o10560082014-08-29 20:51:32 -0400849 if (err == 0)
850 return create ? ERR_PTR(-ENOSPC) : NULL;
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400851 if (err < 0)
Theodore Ts'o10560082014-08-29 20:51:32 -0400852 return ERR_PTR(err);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400853
854 bh = sb_getblk(inode->i_sb, map.m_pblk);
Theodore Ts'o10560082014-08-29 20:51:32 -0400855 if (unlikely(!bh))
856 return ERR_PTR(-ENOMEM);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400857 if (map.m_flags & EXT4_MAP_NEW) {
Chunguang Xu837c23f2020-11-07 23:58:11 +0800858 ASSERT(create != 0);
859 ASSERT((EXT4_SB(inode->i_sb)->s_mount_state & EXT4_FC_REPLAY)
860 || (handle != NULL));
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400861
862 /*
863 * Now that we do not always journal data, we should
864 * keep in mind whether this should always journal the
865 * new buffer as metadata. For now, regular file
866 * writes use ext4_get_block instead, so it's not a
867 * problem.
868 */
869 lock_buffer(bh);
870 BUFFER_TRACE(bh, "call get_create_access");
Jan Kara188c2992021-08-16 11:57:04 +0200871 err = ext4_journal_get_create_access(handle, inode->i_sb, bh,
872 EXT4_JTR_NONE);
Theodore Ts'o10560082014-08-29 20:51:32 -0400873 if (unlikely(err)) {
874 unlock_buffer(bh);
875 goto errout;
876 }
877 if (!buffer_uptodate(bh)) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400878 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
879 set_buffer_uptodate(bh);
880 }
881 unlock_buffer(bh);
882 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
883 err = ext4_handle_dirty_metadata(handle, inode, bh);
Theodore Ts'o10560082014-08-29 20:51:32 -0400884 if (unlikely(err))
885 goto errout;
886 } else
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400887 BUFFER_TRACE(bh, "not a new buffer");
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400888 return bh;
Theodore Ts'o10560082014-08-29 20:51:32 -0400889errout:
890 brelse(bh);
891 return ERR_PTR(err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700892}
893
Mingming Cao617ba132006-10-11 01:20:53 -0700894struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400895 ext4_lblk_t block, int map_flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700896{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400897 struct buffer_head *bh;
zhangyi (F)2d069c02020-09-24 15:33:33 +0800898 int ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700899
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400900 bh = ext4_getblk(handle, inode, block, map_flags);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400901 if (IS_ERR(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700902 return bh;
ZhangXiaoxu7963e5a2019-08-22 23:00:32 -0400903 if (!bh || ext4_buffer_uptodate(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700904 return bh;
zhangyi (F)2d069c02020-09-24 15:33:33 +0800905
906 ret = ext4_read_bh_lock(bh, REQ_META | REQ_PRIO, true);
907 if (ret) {
908 put_bh(bh);
909 return ERR_PTR(ret);
910 }
911 return bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700912}
913
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400914/* Read a contiguous batch of blocks. */
915int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count,
916 bool wait, struct buffer_head **bhs)
917{
918 int i, err;
919
920 for (i = 0; i < bh_count; i++) {
921 bhs[i] = ext4_getblk(NULL, inode, block + i, 0 /* map_flags */);
922 if (IS_ERR(bhs[i])) {
923 err = PTR_ERR(bhs[i]);
924 bh_count = i;
925 goto out_brelse;
926 }
927 }
928
929 for (i = 0; i < bh_count; i++)
930 /* Note that NULL bhs[i] is valid because of holes. */
zhangyi (F)2d069c02020-09-24 15:33:33 +0800931 if (bhs[i] && !ext4_buffer_uptodate(bhs[i]))
932 ext4_read_bh_lock(bhs[i], REQ_META | REQ_PRIO, false);
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400933
934 if (!wait)
935 return 0;
936
937 for (i = 0; i < bh_count; i++)
938 if (bhs[i])
939 wait_on_buffer(bhs[i]);
940
941 for (i = 0; i < bh_count; i++) {
942 if (bhs[i] && !buffer_uptodate(bhs[i])) {
943 err = -EIO;
944 goto out_brelse;
945 }
946 }
947 return 0;
948
949out_brelse:
950 for (i = 0; i < bh_count; i++) {
951 brelse(bhs[i]);
952 bhs[i] = NULL;
953 }
954 return err;
955}
956
Jan Kara188c2992021-08-16 11:57:04 +0200957int ext4_walk_page_buffers(handle_t *handle, struct inode *inode,
Tao Maf19d5872012-12-10 14:05:51 -0500958 struct buffer_head *head,
959 unsigned from,
960 unsigned to,
961 int *partial,
Jan Kara188c2992021-08-16 11:57:04 +0200962 int (*fn)(handle_t *handle, struct inode *inode,
Tao Maf19d5872012-12-10 14:05:51 -0500963 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700964{
965 struct buffer_head *bh;
966 unsigned block_start, block_end;
967 unsigned blocksize = head->b_size;
968 int err, ret = 0;
969 struct buffer_head *next;
970
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400971 for (bh = head, block_start = 0;
972 ret == 0 && (bh != head || !block_start);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400973 block_start = block_end, bh = next) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700974 next = bh->b_this_page;
975 block_end = block_start + blocksize;
976 if (block_end <= from || block_start >= to) {
977 if (partial && !buffer_uptodate(bh))
978 *partial = 1;
979 continue;
980 }
Jan Kara188c2992021-08-16 11:57:04 +0200981 err = (*fn)(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700982 if (!ret)
983 ret = err;
984 }
985 return ret;
986}
987
988/*
989 * To preserve ordering, it is essential that the hole instantiation and
990 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -0700991 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -0700992 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700993 * prepare_write() is the right place.
994 *
Jan Kara36ade452013-01-28 09:30:52 -0500995 * Also, this function can nest inside ext4_writepage(). In that case, we
996 * *know* that ext4_writepage() has generated enough buffer credits to do the
997 * whole page. So we won't block on the journal in that case, which is good,
998 * because the caller may be PF_MEMALLOC.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700999 *
Mingming Cao617ba132006-10-11 01:20:53 -07001000 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001001 * quota file writes. If we were to commit the transaction while thus
1002 * reentered, there can be a deadlock - we would be holding a quota
1003 * lock, and the commit would never complete if another thread had a
1004 * transaction open and was blocking on the quota lock - a ranking
1005 * violation.
1006 *
Mingming Caodab291a2006-10-11 01:21:01 -07001007 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001008 * will _not_ run commit under these circumstances because handle->h_ref
1009 * is elevated. We'll still have enough credits for the tiny quotafile
1010 * write.
1011 */
Jan Kara188c2992021-08-16 11:57:04 +02001012int do_journal_get_write_access(handle_t *handle, struct inode *inode,
Tao Maf19d5872012-12-10 14:05:51 -05001013 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001014{
Jan Kara56d35a42010-08-05 14:41:42 -04001015 int dirty = buffer_dirty(bh);
1016 int ret;
1017
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001018 if (!buffer_mapped(bh) || buffer_freed(bh))
1019 return 0;
Jan Kara56d35a42010-08-05 14:41:42 -04001020 /*
Christoph Hellwigebdec242010-10-06 10:47:23 +02001021 * __block_write_begin() could have dirtied some buffers. Clean
Jan Kara56d35a42010-08-05 14:41:42 -04001022 * the dirty bit as jbd2_journal_get_write_access() could complain
1023 * otherwise about fs integrity issues. Setting of the dirty bit
Christoph Hellwigebdec242010-10-06 10:47:23 +02001024 * by __block_write_begin() isn't a real problem here as we clear
Jan Kara56d35a42010-08-05 14:41:42 -04001025 * the bit before releasing a page lock and thus writeback cannot
1026 * ever write the buffer.
1027 */
1028 if (dirty)
1029 clear_buffer_dirty(bh);
liang xie5d601252014-05-12 22:06:43 -04001030 BUFFER_TRACE(bh, "get write access");
Jan Kara188c2992021-08-16 11:57:04 +02001031 ret = ext4_journal_get_write_access(handle, inode->i_sb, bh,
1032 EXT4_JTR_NONE);
Jan Kara56d35a42010-08-05 14:41:42 -04001033 if (!ret && dirty)
1034 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
1035 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001036}
1037
Chandan Rajendra643fa962018-12-12 15:20:12 +05301038#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrow2058f832015-04-12 00:55:10 -04001039static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len,
1040 get_block_t *get_block)
1041{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001042 unsigned from = pos & (PAGE_SIZE - 1);
Michael Halcrow2058f832015-04-12 00:55:10 -04001043 unsigned to = from + len;
1044 struct inode *inode = page->mapping->host;
1045 unsigned block_start, block_end;
1046 sector_t block;
1047 int err = 0;
1048 unsigned blocksize = inode->i_sb->s_blocksize;
1049 unsigned bbits;
Chandan Rajendra0b578f32019-05-20 09:29:50 -07001050 struct buffer_head *bh, *head, *wait[2];
1051 int nr_wait = 0;
1052 int i;
Michael Halcrow2058f832015-04-12 00:55:10 -04001053
1054 BUG_ON(!PageLocked(page));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001055 BUG_ON(from > PAGE_SIZE);
1056 BUG_ON(to > PAGE_SIZE);
Michael Halcrow2058f832015-04-12 00:55:10 -04001057 BUG_ON(from > to);
1058
1059 if (!page_has_buffers(page))
1060 create_empty_buffers(page, blocksize, 0);
1061 head = page_buffers(page);
1062 bbits = ilog2(blocksize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001063 block = (sector_t)page->index << (PAGE_SHIFT - bbits);
Michael Halcrow2058f832015-04-12 00:55:10 -04001064
1065 for (bh = head, block_start = 0; bh != head || !block_start;
1066 block++, block_start = block_end, bh = bh->b_this_page) {
1067 block_end = block_start + blocksize;
1068 if (block_end <= from || block_start >= to) {
1069 if (PageUptodate(page)) {
Yang Guo3cd46172021-04-01 15:03:30 +08001070 set_buffer_uptodate(bh);
Michael Halcrow2058f832015-04-12 00:55:10 -04001071 }
1072 continue;
1073 }
1074 if (buffer_new(bh))
1075 clear_buffer_new(bh);
1076 if (!buffer_mapped(bh)) {
1077 WARN_ON(bh->b_size != blocksize);
1078 err = get_block(inode, block, bh, 1);
1079 if (err)
1080 break;
1081 if (buffer_new(bh)) {
Michael Halcrow2058f832015-04-12 00:55:10 -04001082 if (PageUptodate(page)) {
1083 clear_buffer_new(bh);
1084 set_buffer_uptodate(bh);
1085 mark_buffer_dirty(bh);
1086 continue;
1087 }
1088 if (block_end > to || block_start < from)
1089 zero_user_segments(page, to, block_end,
1090 block_start, from);
1091 continue;
1092 }
1093 }
1094 if (PageUptodate(page)) {
Yang Guo3cd46172021-04-01 15:03:30 +08001095 set_buffer_uptodate(bh);
Michael Halcrow2058f832015-04-12 00:55:10 -04001096 continue;
1097 }
1098 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
1099 !buffer_unwritten(bh) &&
1100 (block_start < from || block_end > to)) {
zhangyi (F)2d069c02020-09-24 15:33:33 +08001101 ext4_read_bh_lock(bh, 0, false);
Chandan Rajendra0b578f32019-05-20 09:29:50 -07001102 wait[nr_wait++] = bh;
Michael Halcrow2058f832015-04-12 00:55:10 -04001103 }
1104 }
1105 /*
1106 * If we issued read requests, let them complete.
1107 */
Chandan Rajendra0b578f32019-05-20 09:29:50 -07001108 for (i = 0; i < nr_wait; i++) {
1109 wait_on_buffer(wait[i]);
1110 if (!buffer_uptodate(wait[i]))
Michael Halcrow2058f832015-04-12 00:55:10 -04001111 err = -EIO;
1112 }
Chandan Rajendra7e0785f2019-05-20 09:29:49 -07001113 if (unlikely(err)) {
Michael Halcrow2058f832015-04-12 00:55:10 -04001114 page_zero_new_buffers(page, from, to);
Eric Biggers4f74d152020-07-02 01:56:07 +00001115 } else if (fscrypt_inode_uses_fs_layer_crypto(inode)) {
Chandan Rajendra0b578f32019-05-20 09:29:50 -07001116 for (i = 0; i < nr_wait; i++) {
1117 int err2;
1118
1119 err2 = fscrypt_decrypt_pagecache_blocks(page, blocksize,
1120 bh_offset(wait[i]));
1121 if (err2) {
1122 clear_buffer_uptodate(wait[i]);
1123 err = err2;
1124 }
1125 }
Chandan Rajendra7e0785f2019-05-20 09:29:49 -07001126 }
1127
Michael Halcrow2058f832015-04-12 00:55:10 -04001128 return err;
1129}
1130#endif
1131
Nick Pigginbfc1af62007-10-16 01:25:05 -07001132static int ext4_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001133 loff_t pos, unsigned len, unsigned flags,
1134 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001135{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001136 struct inode *inode = mapping->host;
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001137 int ret, needed_blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001138 handle_t *handle;
1139 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001140 struct page *page;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001141 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001142 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001143
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05001144 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
1145 return -EIO;
1146
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001147 trace_ext4_write_begin(inode, pos, len, flags);
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001148 /*
1149 * Reserve one block more for addition to orphan list in case
1150 * we allocate blocks but write fails for some reason
1151 */
1152 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001153 index = pos >> PAGE_SHIFT;
1154 from = pos & (PAGE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001155 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001156
Tao Maf19d5872012-12-10 14:05:51 -05001157 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
1158 ret = ext4_try_to_write_inline_data(mapping, inode, pos, len,
1159 flags, pagep);
1160 if (ret < 0)
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001161 return ret;
1162 if (ret == 1)
1163 return 0;
Tao Maf19d5872012-12-10 14:05:51 -05001164 }
1165
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001166 /*
1167 * grab_cache_page_write_begin() can take a long time if the
1168 * system is thrashing due to memory pressure, or if the page
1169 * is being written back. So grab it first before we start
1170 * the transaction handle. This also allows us to allocate
1171 * the page (if needed) without using GFP_NOFS.
1172 */
1173retry_grab:
Nick Piggin54566b22009-01-04 12:00:53 -08001174 page = grab_cache_page_write_begin(mapping, index, flags);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001175 if (!page)
1176 return -ENOMEM;
1177 unlock_page(page);
1178
1179retry_journal:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001180 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001181 if (IS_ERR(handle)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001182 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001183 return PTR_ERR(handle);
Jan Karacf108bc2008-07-11 19:27:31 -04001184 }
Tao Maf19d5872012-12-10 14:05:51 -05001185
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001186 lock_page(page);
1187 if (page->mapping != mapping) {
1188 /* The page got truncated from under us */
1189 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001190 put_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04001191 ext4_journal_stop(handle);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001192 goto retry_grab;
Jan Karacf108bc2008-07-11 19:27:31 -04001193 }
Dmitry Monakhov7afe5aa2013-08-28 14:30:47 -04001194 /* In case writeback began while the page was unlocked */
1195 wait_for_stable_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04001196
Chandan Rajendra643fa962018-12-12 15:20:12 +05301197#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrow2058f832015-04-12 00:55:10 -04001198 if (ext4_should_dioread_nolock(inode))
1199 ret = ext4_block_write_begin(page, pos, len,
Jan Kara705965b2016-03-08 23:08:10 -05001200 ext4_get_block_unwritten);
Michael Halcrow2058f832015-04-12 00:55:10 -04001201 else
1202 ret = ext4_block_write_begin(page, pos, len,
1203 ext4_get_block);
1204#else
Jiaying Zhang744692d2010-03-04 16:14:02 -05001205 if (ext4_should_dioread_nolock(inode))
Jan Kara705965b2016-03-08 23:08:10 -05001206 ret = __block_write_begin(page, pos, len,
1207 ext4_get_block_unwritten);
Jiaying Zhang744692d2010-03-04 16:14:02 -05001208 else
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001209 ret = __block_write_begin(page, pos, len, ext4_get_block);
Michael Halcrow2058f832015-04-12 00:55:10 -04001210#endif
Nick Pigginbfc1af62007-10-16 01:25:05 -07001211 if (!ret && ext4_should_journal_data(inode)) {
Jan Kara188c2992021-08-16 11:57:04 +02001212 ret = ext4_walk_page_buffers(handle, inode,
1213 page_buffers(page), from, to, NULL,
Tao Maf19d5872012-12-10 14:05:51 -05001214 do_journal_get_write_access);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001215 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001216
1217 if (ret) {
Eric Biggersc93d8f82019-07-22 09:26:24 -07001218 bool extended = (pos + len > inode->i_size) &&
1219 !ext4_verity_in_progress(inode);
1220
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001221 unlock_page(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001222 /*
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001223 * __block_write_begin may have instantiated a few blocks
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001224 * outside i_size. Trim these off again. Don't need
1225 * i_size_read because we hold i_mutex.
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001226 *
1227 * Add inode to orphan list in case we crash before
1228 * truncate finishes
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001229 */
Eric Biggersc93d8f82019-07-22 09:26:24 -07001230 if (extended && ext4_can_truncate(inode))
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001231 ext4_orphan_add(handle, inode);
1232
1233 ext4_journal_stop(handle);
Eric Biggersc93d8f82019-07-22 09:26:24 -07001234 if (extended) {
Jan Karab9a42072009-12-08 21:24:33 -05001235 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001236 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001237 * If truncate failed early the inode might
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001238 * still be on the orphan list; we need to
1239 * make sure the inode is removed from the
1240 * orphan list in that case.
1241 */
1242 if (inode->i_nlink)
1243 ext4_orphan_del(NULL, inode);
1244 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001245
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001246 if (ret == -ENOSPC &&
1247 ext4_should_retry_alloc(inode->i_sb, &retries))
1248 goto retry_journal;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001249 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001250 return ret;
1251 }
1252 *pagep = page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001253 return ret;
1254}
1255
Nick Pigginbfc1af62007-10-16 01:25:05 -07001256/* For write_end() in data=journal mode */
Jan Kara188c2992021-08-16 11:57:04 +02001257static int write_end_fn(handle_t *handle, struct inode *inode,
1258 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001259{
Theodore Ts'o13fca322013-04-21 16:45:54 -04001260 int ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001261 if (!buffer_mapped(bh) || buffer_freed(bh))
1262 return 0;
1263 set_buffer_uptodate(bh);
Theodore Ts'o13fca322013-04-21 16:45:54 -04001264 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
1265 clear_buffer_meta(bh);
1266 clear_buffer_prio(bh);
1267 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001268}
1269
Zheng Liueed43332013-04-03 12:41:17 -04001270/*
1271 * We need to pick up the new inode size which generic_commit_write gave us
1272 * `file' can be NULL - eg, when called from page_symlink().
1273 *
1274 * ext4 never places buffers on inode->i_mapping->private_list. metadata
1275 * buffers are managed internally.
1276 */
1277static int ext4_write_end(struct file *file,
1278 struct address_space *mapping,
1279 loff_t pos, unsigned len, unsigned copied,
1280 struct page *page, void *fsdata)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001281{
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001282 handle_t *handle = ext4_journal_current_handle();
Zheng Liueed43332013-04-03 12:41:17 -04001283 struct inode *inode = mapping->host;
Xiaoguang Wang05726392015-02-12 23:00:17 -05001284 loff_t old_size = inode->i_size;
Zheng Liueed43332013-04-03 12:41:17 -04001285 int ret = 0, ret2;
1286 int i_size_changed = 0;
Theodore Ts'o362eca72018-07-10 01:07:43 -04001287 int inline_data = ext4_has_inline_data(inode);
Eric Biggersc93d8f82019-07-22 09:26:24 -07001288 bool verity = ext4_verity_in_progress(inode);
Zheng Liueed43332013-04-03 12:41:17 -04001289
1290 trace_ext4_write_end(inode, pos, len, copied);
Theodore Ts'o362eca72018-07-10 01:07:43 -04001291 if (inline_data) {
Theodore Ts'o42c832d2013-07-01 08:12:39 -04001292 ret = ext4_write_inline_data_end(inode, pos, len,
1293 copied, page);
Theodore Ts'oeb5efbc2017-02-04 23:04:00 -05001294 if (ret < 0) {
1295 unlock_page(page);
1296 put_page(page);
Theodore Ts'o42c832d2013-07-01 08:12:39 -04001297 goto errout;
Theodore Ts'oeb5efbc2017-02-04 23:04:00 -05001298 }
Theodore Ts'o42c832d2013-07-01 08:12:39 -04001299 copied = ret;
1300 } else
Tao Maf19d5872012-12-10 14:05:51 -05001301 copied = block_write_end(file, mapping, pos,
1302 len, copied, page, fsdata);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001303 /*
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001304 * it's important to update i_size while still holding page lock:
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001305 * page writeout could otherwise come in and zero beyond i_size.
Eric Biggersc93d8f82019-07-22 09:26:24 -07001306 *
1307 * If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree
1308 * blocks are being written past EOF, so skip the i_size update.
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001309 */
Eric Biggersc93d8f82019-07-22 09:26:24 -07001310 if (!verity)
1311 i_size_changed = ext4_update_inode_size(inode, pos + copied);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001312 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001313 put_page(page);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001314
Eric Biggersc93d8f82019-07-22 09:26:24 -07001315 if (old_size < pos && !verity)
Xiaoguang Wang05726392015-02-12 23:00:17 -05001316 pagecache_isize_extended(inode, old_size, pos);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001317 /*
1318 * Don't mark the inode dirty under page lock. First, it unnecessarily
1319 * makes the holding time of page lock longer. Second, it forces lock
1320 * ordering of page lock and transaction start for journaling
1321 * filesystems.
1322 */
Theodore Ts'o362eca72018-07-10 01:07:43 -04001323 if (i_size_changed || inline_data)
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07001324 ret = ext4_mark_inode_dirty(handle, inode);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001325
Eric Biggersc93d8f82019-07-22 09:26:24 -07001326 if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001327 /* if we have allocated more blocks and copied
1328 * less. We will have blocks allocated outside
1329 * inode->i_size. So truncate them
1330 */
1331 ext4_orphan_add(handle, inode);
Theodore Ts'o74d553a2013-04-03 12:39:17 -04001332errout:
Mingming Cao617ba132006-10-11 01:20:53 -07001333 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001334 if (!ret)
1335 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001336
Eric Biggersc93d8f82019-07-22 09:26:24 -07001337 if (pos + len > inode->i_size && !verity) {
Jan Karab9a42072009-12-08 21:24:33 -05001338 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001339 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001340 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001341 * on the orphan list; we need to make sure the inode
1342 * is removed from the orphan list in that case.
1343 */
1344 if (inode->i_nlink)
1345 ext4_orphan_del(NULL, inode);
1346 }
1347
Nick Pigginbfc1af62007-10-16 01:25:05 -07001348 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001349}
1350
Theodore Ts'ob90197b2015-10-15 10:29:05 -04001351/*
1352 * This is a private version of page_zero_new_buffers() which doesn't
1353 * set the buffer to be dirty, since in data=journalled mode we need
1354 * to call ext4_handle_dirty_metadata() instead.
1355 */
Jan Kara3b136492017-01-27 14:35:38 -05001356static void ext4_journalled_zero_new_buffers(handle_t *handle,
Jan Kara188c2992021-08-16 11:57:04 +02001357 struct inode *inode,
Jan Kara3b136492017-01-27 14:35:38 -05001358 struct page *page,
1359 unsigned from, unsigned to)
Theodore Ts'ob90197b2015-10-15 10:29:05 -04001360{
1361 unsigned int block_start = 0, block_end;
1362 struct buffer_head *head, *bh;
1363
1364 bh = head = page_buffers(page);
1365 do {
1366 block_end = block_start + bh->b_size;
1367 if (buffer_new(bh)) {
1368 if (block_end > from && block_start < to) {
1369 if (!PageUptodate(page)) {
1370 unsigned start, size;
1371
1372 start = max(from, block_start);
1373 size = min(to, block_end) - start;
1374
1375 zero_user(page, start, size);
Jan Kara188c2992021-08-16 11:57:04 +02001376 write_end_fn(handle, inode, bh);
Theodore Ts'ob90197b2015-10-15 10:29:05 -04001377 }
1378 clear_buffer_new(bh);
1379 }
1380 }
1381 block_start = block_end;
1382 bh = bh->b_this_page;
1383 } while (bh != head);
1384}
1385
Nick Pigginbfc1af62007-10-16 01:25:05 -07001386static int ext4_journalled_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001387 struct address_space *mapping,
1388 loff_t pos, unsigned len, unsigned copied,
1389 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001390{
Mingming Cao617ba132006-10-11 01:20:53 -07001391 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -07001392 struct inode *inode = mapping->host;
Xiaoguang Wang05726392015-02-12 23:00:17 -05001393 loff_t old_size = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001394 int ret = 0, ret2;
1395 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001396 unsigned from, to;
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001397 int size_changed = 0;
Theodore Ts'o362eca72018-07-10 01:07:43 -04001398 int inline_data = ext4_has_inline_data(inode);
Eric Biggersc93d8f82019-07-22 09:26:24 -07001399 bool verity = ext4_verity_in_progress(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001400
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001401 trace_ext4_journalled_write_end(inode, pos, len, copied);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001402 from = pos & (PAGE_SIZE - 1);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001403 to = from + len;
1404
Curt Wohlgemuth441c8502011-08-13 11:25:18 -04001405 BUG_ON(!ext4_handle_valid(handle));
1406
Theodore Ts'o362eca72018-07-10 01:07:43 -04001407 if (inline_data) {
Theodore Ts'oeb5efbc2017-02-04 23:04:00 -05001408 ret = ext4_write_inline_data_end(inode, pos, len,
1409 copied, page);
1410 if (ret < 0) {
1411 unlock_page(page);
1412 put_page(page);
1413 goto errout;
1414 }
1415 copied = ret;
1416 } else if (unlikely(copied < len) && !PageUptodate(page)) {
Jan Kara3b136492017-01-27 14:35:38 -05001417 copied = 0;
Jan Kara188c2992021-08-16 11:57:04 +02001418 ext4_journalled_zero_new_buffers(handle, inode, page, from, to);
Jan Kara3b136492017-01-27 14:35:38 -05001419 } else {
1420 if (unlikely(copied < len))
Jan Kara188c2992021-08-16 11:57:04 +02001421 ext4_journalled_zero_new_buffers(handle, inode, page,
Jan Kara3b136492017-01-27 14:35:38 -05001422 from + copied, to);
Jan Kara188c2992021-08-16 11:57:04 +02001423 ret = ext4_walk_page_buffers(handle, inode, page_buffers(page),
1424 from, from + copied, &partial,
Jan Kara3b136492017-01-27 14:35:38 -05001425 write_end_fn);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001426 if (!partial)
1427 SetPageUptodate(page);
1428 }
Eric Biggersc93d8f82019-07-22 09:26:24 -07001429 if (!verity)
1430 size_changed = ext4_update_inode_size(inode, pos + copied);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001431 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Jan Kara2d859db2011-07-26 09:07:11 -04001432 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001433 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001434 put_page(page);
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001435
Eric Biggersc93d8f82019-07-22 09:26:24 -07001436 if (old_size < pos && !verity)
Xiaoguang Wang05726392015-02-12 23:00:17 -05001437 pagecache_isize_extended(inode, old_size, pos);
1438
Theodore Ts'o362eca72018-07-10 01:07:43 -04001439 if (size_changed || inline_data) {
Mingming Cao617ba132006-10-11 01:20:53 -07001440 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001441 if (!ret)
1442 ret = ret2;
1443 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001444
Eric Biggersc93d8f82019-07-22 09:26:24 -07001445 if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001446 /* if we have allocated more blocks and copied
1447 * less. We will have blocks allocated outside
1448 * inode->i_size. So truncate them
1449 */
1450 ext4_orphan_add(handle, inode);
1451
Theodore Ts'oeb5efbc2017-02-04 23:04:00 -05001452errout:
Mingming Cao617ba132006-10-11 01:20:53 -07001453 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001454 if (!ret)
1455 ret = ret2;
Eric Biggersc93d8f82019-07-22 09:26:24 -07001456 if (pos + len > inode->i_size && !verity) {
Jan Karab9a42072009-12-08 21:24:33 -05001457 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001458 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001459 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001460 * on the orphan list; we need to make sure the inode
1461 * is removed from the orphan list in that case.
1462 */
1463 if (inode->i_nlink)
1464 ext4_orphan_del(NULL, inode);
1465 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001466
1467 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001468}
Mingming Caod2a17632008-07-14 17:52:37 -04001469
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001470/*
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001471 * Reserve space for a single cluster
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001472 */
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001473static int ext4_da_reserve_space(struct inode *inode)
Mingming Caod2a17632008-07-14 17:52:37 -04001474{
Mingming Cao60e58e02009-01-22 18:13:05 +01001475 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001476 struct ext4_inode_info *ei = EXT4_I(inode);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001477 int ret;
Mingming Caod2a17632008-07-14 17:52:37 -04001478
Mingming Cao60e58e02009-01-22 18:13:05 +01001479 /*
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001480 * We will charge metadata quota at writeout time; this saves
1481 * us from metadata over-estimation, though we may go over by
1482 * a small amount in the end. Here we just reserve for data.
Mingming Cao60e58e02009-01-22 18:13:05 +01001483 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04001484 ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001485 if (ret)
1486 return ret;
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001487
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001488 spin_lock(&ei->i_block_reservation_lock);
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04001489 if (ext4_claim_free_clusters(sbi, 1, 0)) {
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001490 spin_unlock(&ei->i_block_reservation_lock);
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001491 dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
Mingming Caod2a17632008-07-14 17:52:37 -04001492 return -ENOSPC;
1493 }
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001494 ei->i_reserved_data_blocks++;
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001495 trace_ext4_da_reserve_space(inode);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001496 spin_unlock(&ei->i_block_reservation_lock);
Dmitry Monakhov39bc6802009-12-10 16:36:27 +00001497
Mingming Caod2a17632008-07-14 17:52:37 -04001498 return 0; /* success */
1499}
1500
Eric Whitneyf4567672018-10-01 14:33:24 -04001501void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001502{
1503 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001504 struct ext4_inode_info *ei = EXT4_I(inode);
Mingming Caod2a17632008-07-14 17:52:37 -04001505
Mingming Caocd213222008-08-19 22:16:59 -04001506 if (!to_free)
1507 return; /* Nothing to release, exit */
1508
Mingming Caod2a17632008-07-14 17:52:37 -04001509 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001510
Li Zefan5a58ec872010-05-17 02:00:00 -04001511 trace_ext4_da_release_space(inode, to_free);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001512 if (unlikely(to_free > ei->i_reserved_data_blocks)) {
Mingming Caocd213222008-08-19 22:16:59 -04001513 /*
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001514 * if there aren't enough reserved blocks, then the
1515 * counter is messed up somewhere. Since this
1516 * function is called from invalidate page, it's
1517 * harmless to return without any action.
Mingming Caocd213222008-08-19 22:16:59 -04001518 */
Theodore Ts'o8de5c322013-02-14 15:11:41 -05001519 ext4_warning(inode->i_sb, "ext4_da_release_space: "
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001520 "ino %lu, to_free %d with only %d reserved "
Theodore Ts'o1084f252012-03-19 23:13:43 -04001521 "data blocks", inode->i_ino, to_free,
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001522 ei->i_reserved_data_blocks);
1523 WARN_ON(1);
1524 to_free = ei->i_reserved_data_blocks;
1525 }
1526 ei->i_reserved_data_blocks -= to_free;
1527
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001528 /* update fs dirty data blocks counter */
Theodore Ts'o57042652011-09-09 18:56:51 -04001529 percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free);
Mingming Caod2a17632008-07-14 17:52:37 -04001530
Mingming Caod2a17632008-07-14 17:52:37 -04001531 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001532
Aditya Kali7b415bf2011-09-09 19:04:51 -04001533 dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free));
Mingming Caod2a17632008-07-14 17:52:37 -04001534}
1535
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001536/*
Alex Tomas64769242008-07-11 19:27:31 -04001537 * Delayed allocation stuff
1538 */
1539
Jan Kara4e7ea812013-06-04 13:17:40 -04001540struct mpage_da_data {
1541 struct inode *inode;
1542 struct writeback_control *wbc;
Jan Kara6b523df2013-06-04 13:21:11 -04001543
Jan Kara4e7ea812013-06-04 13:17:40 -04001544 pgoff_t first_page; /* The first page to write */
1545 pgoff_t next_page; /* Current page to examine */
1546 pgoff_t last_page; /* Last page to examine */
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001547 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04001548 * Extent to map - this can be after first_page because that can be
1549 * fully mapped. We somewhat abuse m_flags to store whether the extent
1550 * is delalloc or unwritten.
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001551 */
Jan Kara4e7ea812013-06-04 13:17:40 -04001552 struct ext4_map_blocks map;
1553 struct ext4_io_submit io_submit; /* IO submission data */
Jan Karadddbd6a2017-04-30 18:29:10 -04001554 unsigned int do_map:1;
Jan Kara6b8ed622020-05-25 10:12:15 +02001555 unsigned int scanned_until_end:1;
Jan Kara4e7ea812013-06-04 13:17:40 -04001556};
Alex Tomas64769242008-07-11 19:27:31 -04001557
Jan Kara4e7ea812013-06-04 13:17:40 -04001558static void mpage_release_unused_pages(struct mpage_da_data *mpd,
1559 bool invalidate)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001560{
1561 int nr_pages, i;
1562 pgoff_t index, end;
1563 struct pagevec pvec;
1564 struct inode *inode = mpd->inode;
1565 struct address_space *mapping = inode->i_mapping;
Jan Kara4e7ea812013-06-04 13:17:40 -04001566
1567 /* This is necessary when next_page == 0. */
1568 if (mpd->first_page >= mpd->next_page)
1569 return;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001570
Jan Kara6b8ed622020-05-25 10:12:15 +02001571 mpd->scanned_until_end = 0;
Curt Wohlgemuthc7f59382011-02-26 12:27:52 -05001572 index = mpd->first_page;
1573 end = mpd->next_page - 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04001574 if (invalidate) {
1575 ext4_lblk_t start, last;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001576 start = index << (PAGE_SHIFT - inode->i_blkbits);
1577 last = end << (PAGE_SHIFT - inode->i_blkbits);
Jan Kara4e7ea812013-06-04 13:17:40 -04001578 ext4_es_remove_extent(inode, start, last - start + 1);
1579 }
Zheng Liu51865fd2012-11-08 21:57:32 -05001580
Mel Gorman86679822017-11-15 17:37:52 -08001581 pagevec_init(&pvec);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001582 while (index <= end) {
Jan Kara397162f2017-09-06 16:21:43 -07001583 nr_pages = pagevec_lookup_range(&pvec, mapping, &index, end);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001584 if (nr_pages == 0)
1585 break;
1586 for (i = 0; i < nr_pages; i++) {
1587 struct page *page = pvec.pages[i];
Jan Kara2b85a612017-09-06 16:21:30 -07001588
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001589 BUG_ON(!PageLocked(page));
1590 BUG_ON(PageWriteback(page));
Jan Kara4e7ea812013-06-04 13:17:40 -04001591 if (invalidate) {
wangguang4e800c02016-09-15 11:32:46 -04001592 if (page_mapped(page))
1593 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001594 block_invalidatepage(page, 0, PAGE_SIZE);
Jan Kara4e7ea812013-06-04 13:17:40 -04001595 ClearPageUptodate(page);
1596 }
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001597 unlock_page(page);
1598 }
Jan Kara9b1d09982010-03-03 16:19:32 -05001599 pagevec_release(&pvec);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001600 }
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001601}
1602
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001603static void ext4_print_free_blocks(struct inode *inode)
1604{
1605 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o92b97812012-03-19 23:41:49 -04001606 struct super_block *sb = inode->i_sb;
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001607 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'o92b97812012-03-19 23:41:49 -04001608
1609 ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld",
Theodore Ts'o5dee5432011-09-09 19:10:51 -04001610 EXT4_C2B(EXT4_SB(inode->i_sb),
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001611 ext4_count_free_clusters(sb)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001612 ext4_msg(sb, KERN_CRIT, "Free/Dirty block details");
1613 ext4_msg(sb, KERN_CRIT, "free_blocks=%lld",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001614 (long long) EXT4_C2B(EXT4_SB(sb),
Theodore Ts'o57042652011-09-09 18:56:51 -04001615 percpu_counter_sum(&sbi->s_freeclusters_counter)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001616 ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001617 (long long) EXT4_C2B(EXT4_SB(sb),
Aditya Kali7b415bf2011-09-09 19:04:51 -04001618 percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001619 ext4_msg(sb, KERN_CRIT, "Block reservation details");
1620 ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001621 ei->i_reserved_data_blocks);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001622 return;
1623}
1624
Jan Kara188c2992021-08-16 11:57:04 +02001625static int ext4_bh_delay_or_unwritten(handle_t *handle, struct inode *inode,
1626 struct buffer_head *bh)
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001627{
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001628 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001629}
1630
Alex Tomas64769242008-07-11 19:27:31 -04001631/*
Eric Whitney0b02f4c2018-10-01 14:19:37 -04001632 * ext4_insert_delayed_block - adds a delayed block to the extents status
1633 * tree, incrementing the reserved cluster/block
1634 * count or making a pending reservation
1635 * where needed
1636 *
1637 * @inode - file containing the newly added block
1638 * @lblk - logical block to be added
1639 *
1640 * Returns 0 on success, negative error code on failure.
1641 */
1642static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk)
1643{
1644 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1645 int ret;
1646 bool allocated = false;
1647
1648 /*
1649 * If the cluster containing lblk is shared with a delayed,
1650 * written, or unwritten extent in a bigalloc file system, it's
1651 * already been accounted for and does not need to be reserved.
1652 * A pending reservation must be made for the cluster if it's
1653 * shared with a written or unwritten extent and doesn't already
1654 * have one. Written and unwritten extents can be purged from the
1655 * extents status tree if the system is under memory pressure, so
1656 * it's necessary to examine the extent tree if a search of the
1657 * extents status tree doesn't get a match.
1658 */
1659 if (sbi->s_cluster_ratio == 1) {
1660 ret = ext4_da_reserve_space(inode);
1661 if (ret != 0) /* ENOSPC */
1662 goto errout;
1663 } else { /* bigalloc */
1664 if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) {
1665 if (!ext4_es_scan_clu(inode,
1666 &ext4_es_is_mapped, lblk)) {
1667 ret = ext4_clu_mapped(inode,
1668 EXT4_B2C(sbi, lblk));
1669 if (ret < 0)
1670 goto errout;
1671 if (ret == 0) {
1672 ret = ext4_da_reserve_space(inode);
1673 if (ret != 0) /* ENOSPC */
1674 goto errout;
1675 } else {
1676 allocated = true;
1677 }
1678 } else {
1679 allocated = true;
1680 }
1681 }
1682 }
1683
1684 ret = ext4_es_insert_delayed_block(inode, lblk, allocated);
1685
1686errout:
1687 return ret;
1688}
1689
1690/*
Aditya Kali5356f2612011-09-09 19:20:51 -04001691 * This function is grabs code from the very beginning of
1692 * ext4_map_blocks, but assumes that the caller is from delayed write
1693 * time. This function looks up the requested blocks and sets the
1694 * buffer delay bit under the protection of i_data_sem.
1695 */
1696static int ext4_da_map_blocks(struct inode *inode, sector_t iblock,
1697 struct ext4_map_blocks *map,
1698 struct buffer_head *bh)
1699{
Zheng Liud100eef2013-02-18 00:29:59 -05001700 struct extent_status es;
Aditya Kali5356f2612011-09-09 19:20:51 -04001701 int retval;
1702 sector_t invalid_block = ~((sector_t) 0xffff);
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001703#ifdef ES_AGGRESSIVE_TEST
1704 struct ext4_map_blocks orig_map;
1705
1706 memcpy(&orig_map, map, sizeof(*map));
1707#endif
Aditya Kali5356f2612011-09-09 19:20:51 -04001708
1709 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
1710 invalid_block = ~0;
1711
1712 map->m_flags = 0;
Ritesh Harjani70aa1552020-05-10 11:54:55 +05301713 ext_debug(inode, "max_blocks %u, logical block %lu\n", map->m_len,
Aditya Kali5356f2612011-09-09 19:20:51 -04001714 (unsigned long) map->m_lblk);
Zheng Liud100eef2013-02-18 00:29:59 -05001715
1716 /* Lookup extent status tree firstly */
Theodore Ts'obb5835e2019-08-11 16:32:41 -04001717 if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) {
Zheng Liud100eef2013-02-18 00:29:59 -05001718 if (ext4_es_is_hole(&es)) {
1719 retval = 0;
Lukas Czernerc8b459f2014-05-12 12:55:07 -04001720 down_read(&EXT4_I(inode)->i_data_sem);
Zheng Liud100eef2013-02-18 00:29:59 -05001721 goto add_delayed;
1722 }
1723
1724 /*
1725 * Delayed extent could be allocated by fallocate.
1726 * So we need to check it.
1727 */
1728 if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) {
1729 map_bh(bh, inode->i_sb, invalid_block);
1730 set_buffer_new(bh);
1731 set_buffer_delay(bh);
1732 return 0;
1733 }
1734
1735 map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk;
1736 retval = es.es_len - (iblock - es.es_lblk);
1737 if (retval > map->m_len)
1738 retval = map->m_len;
1739 map->m_len = retval;
1740 if (ext4_es_is_written(&es))
1741 map->m_flags |= EXT4_MAP_MAPPED;
1742 else if (ext4_es_is_unwritten(&es))
1743 map->m_flags |= EXT4_MAP_UNWRITTEN;
1744 else
Arnd Bergmann1e83bc82019-04-07 12:24:43 -04001745 BUG();
Zheng Liud100eef2013-02-18 00:29:59 -05001746
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001747#ifdef ES_AGGRESSIVE_TEST
1748 ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0);
1749#endif
Zheng Liud100eef2013-02-18 00:29:59 -05001750 return retval;
1751 }
1752
Aditya Kali5356f2612011-09-09 19:20:51 -04001753 /*
1754 * Try to see if we can get the block without requesting a new
1755 * file system block.
1756 */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04001757 down_read(&EXT4_I(inode)->i_data_sem);
Jan Karacbd75842014-11-25 11:41:49 -05001758 if (ext4_has_inline_data(inode))
Tao Ma9c3569b2012-12-10 14:05:57 -05001759 retval = 0;
Jan Karacbd75842014-11-25 11:41:49 -05001760 else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Zheng Liu2f8e0a72014-11-25 11:44:37 -05001761 retval = ext4_ext_map_blocks(NULL, inode, map, 0);
Aditya Kali5356f2612011-09-09 19:20:51 -04001762 else
Zheng Liu2f8e0a72014-11-25 11:44:37 -05001763 retval = ext4_ind_map_blocks(NULL, inode, map, 0);
Aditya Kali5356f2612011-09-09 19:20:51 -04001764
Zheng Liud100eef2013-02-18 00:29:59 -05001765add_delayed:
Aditya Kali5356f2612011-09-09 19:20:51 -04001766 if (retval == 0) {
Zheng Liuf7fec032013-02-18 00:28:47 -05001767 int ret;
Eric Whitneyad431022018-10-01 14:10:39 -04001768
Aditya Kali5356f2612011-09-09 19:20:51 -04001769 /*
1770 * XXX: __block_prepare_write() unmaps passed block,
1771 * is it OK?
1772 */
Aditya Kali5356f2612011-09-09 19:20:51 -04001773
Eric Whitney0b02f4c2018-10-01 14:19:37 -04001774 ret = ext4_insert_delayed_block(inode, map->m_lblk);
1775 if (ret != 0) {
Zheng Liuf7fec032013-02-18 00:28:47 -05001776 retval = ret;
Zheng Liu51865fd2012-11-08 21:57:32 -05001777 goto out_unlock;
Zheng Liuf7fec032013-02-18 00:28:47 -05001778 }
Zheng Liu51865fd2012-11-08 21:57:32 -05001779
Aditya Kali5356f2612011-09-09 19:20:51 -04001780 map_bh(bh, inode->i_sb, invalid_block);
1781 set_buffer_new(bh);
1782 set_buffer_delay(bh);
Zheng Liuf7fec032013-02-18 00:28:47 -05001783 } else if (retval > 0) {
1784 int ret;
Theodore Ts'o3be78c72013-08-16 21:22:41 -04001785 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -05001786
Zheng Liu44fb851d2013-07-29 12:51:42 -04001787 if (unlikely(retval != map->m_len)) {
1788 ext4_warning(inode->i_sb,
1789 "ES len assertion failed for inode "
1790 "%lu: retval %d != map->m_len %d",
1791 inode->i_ino, retval, map->m_len);
1792 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001793 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001794
Zheng Liuf7fec032013-02-18 00:28:47 -05001795 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
1796 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
1797 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
1798 map->m_pblk, status);
1799 if (ret != 0)
1800 retval = ret;
Aditya Kali5356f2612011-09-09 19:20:51 -04001801 }
1802
1803out_unlock:
1804 up_read((&EXT4_I(inode)->i_data_sem));
1805
1806 return retval;
1807}
1808
1809/*
Seunghun Leed91bd2c2014-09-01 22:15:30 -04001810 * This is a special get_block_t callback which is used by
Theodore Ts'ob920c752009-05-14 00:54:29 -04001811 * ext4_da_write_begin(). It will either return mapped block or
1812 * reserve space for a single block.
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001813 *
1814 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
1815 * We also have b_blocknr = -1 and b_bdev initialized properly
1816 *
1817 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
1818 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
1819 * initialized properly.
Alex Tomas64769242008-07-11 19:27:31 -04001820 */
Tao Ma9c3569b2012-12-10 14:05:57 -05001821int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
1822 struct buffer_head *bh, int create)
Alex Tomas64769242008-07-11 19:27:31 -04001823{
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001824 struct ext4_map_blocks map;
Alex Tomas64769242008-07-11 19:27:31 -04001825 int ret = 0;
1826
1827 BUG_ON(create == 0);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001828 BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
1829
1830 map.m_lblk = iblock;
1831 map.m_len = 1;
Alex Tomas64769242008-07-11 19:27:31 -04001832
1833 /*
1834 * first, we need to know whether the block is allocated already
1835 * preallocated blocks are unmapped but should treated
1836 * the same as allocated blocks.
1837 */
Aditya Kali5356f2612011-09-09 19:20:51 -04001838 ret = ext4_da_map_blocks(inode, iblock, &map, bh);
1839 if (ret <= 0)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001840 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04001841
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001842 map_bh(bh, inode->i_sb, map.m_pblk);
Jan Karaed8ad832016-02-19 00:18:25 -05001843 ext4_update_bh_state(bh, map.m_flags);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001844
1845 if (buffer_unwritten(bh)) {
1846 /* A delayed write to unwritten bh should be marked
1847 * new and mapped. Mapped ensures that we don't do
1848 * get_block multiple times when we write to the same
1849 * offset and new ensures that we do proper zero out
1850 * for partial write.
1851 */
1852 set_buffer_new(bh);
Theodore Ts'oc8205632011-04-10 22:30:07 -04001853 set_buffer_mapped(bh);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001854 }
1855 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04001856}
Mingming Cao61628a32008-07-11 19:27:31 -04001857
Jan Kara188c2992021-08-16 11:57:04 +02001858static int bget_one(handle_t *handle, struct inode *inode,
1859 struct buffer_head *bh)
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001860{
1861 get_bh(bh);
1862 return 0;
1863}
1864
Jan Kara188c2992021-08-16 11:57:04 +02001865static int bput_one(handle_t *handle, struct inode *inode,
1866 struct buffer_head *bh)
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001867{
1868 put_bh(bh);
1869 return 0;
1870}
1871
1872static int __ext4_journalled_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001873 unsigned int len)
1874{
1875 struct address_space *mapping = page->mapping;
1876 struct inode *inode = mapping->host;
Tao Ma3fdcfb62012-12-10 14:05:57 -05001877 struct buffer_head *page_bufs = NULL;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001878 handle_t *handle = NULL;
Tao Ma3fdcfb62012-12-10 14:05:57 -05001879 int ret = 0, err = 0;
1880 int inline_data = ext4_has_inline_data(inode);
1881 struct buffer_head *inode_bh = NULL;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001882
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001883 ClearPageChecked(page);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001884
1885 if (inline_data) {
1886 BUG_ON(page->index != 0);
1887 BUG_ON(len > ext4_get_max_inline_size(inode));
1888 inode_bh = ext4_journalled_write_inline_data(inode, len, page);
1889 if (inode_bh == NULL)
1890 goto out;
1891 } else {
1892 page_bufs = page_buffers(page);
1893 if (!page_bufs) {
1894 BUG();
1895 goto out;
1896 }
Jan Kara188c2992021-08-16 11:57:04 +02001897 ext4_walk_page_buffers(handle, inode, page_bufs, 0, len,
Tao Ma3fdcfb62012-12-10 14:05:57 -05001898 NULL, bget_one);
1899 }
Theodore Ts'obdf96832015-06-12 23:45:33 -04001900 /*
1901 * We need to release the page lock before we start the
1902 * journal, so grab a reference so the page won't disappear
1903 * out from under us.
1904 */
1905 get_page(page);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001906 unlock_page(page);
1907
Theodore Ts'o9924a922013-02-08 21:59:22 -05001908 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
1909 ext4_writepage_trans_blocks(inode));
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001910 if (IS_ERR(handle)) {
1911 ret = PTR_ERR(handle);
Theodore Ts'obdf96832015-06-12 23:45:33 -04001912 put_page(page);
1913 goto out_no_pagelock;
1914 }
1915 BUG_ON(!ext4_handle_valid(handle));
1916
1917 lock_page(page);
1918 put_page(page);
1919 if (page->mapping != mapping) {
1920 /* The page got truncated from under us */
1921 ext4_journal_stop(handle);
1922 ret = 0;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001923 goto out;
1924 }
1925
Tao Ma3fdcfb62012-12-10 14:05:57 -05001926 if (inline_data) {
Theodore Ts'o362eca72018-07-10 01:07:43 -04001927 ret = ext4_mark_inode_dirty(handle, inode);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001928 } else {
Jan Kara188c2992021-08-16 11:57:04 +02001929 ret = ext4_walk_page_buffers(handle, inode, page_bufs, 0, len,
1930 NULL, do_journal_get_write_access);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001931
Jan Kara188c2992021-08-16 11:57:04 +02001932 err = ext4_walk_page_buffers(handle, inode, page_bufs, 0, len,
1933 NULL, write_end_fn);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001934 }
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001935 if (ret == 0)
1936 ret = err;
Jan Karab5b18162020-10-27 14:27:51 +01001937 err = ext4_jbd2_inode_add_write(handle, inode, page_offset(page), len);
Mauricio Faria de Oliveiraafb585a2020-10-05 21:48:41 -03001938 if (ret == 0)
1939 ret = err;
Jan Kara2d859db2011-07-26 09:07:11 -04001940 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001941 err = ext4_journal_stop(handle);
1942 if (!ret)
1943 ret = err;
1944
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001945 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001946out:
Theodore Ts'obdf96832015-06-12 23:45:33 -04001947 unlock_page(page);
1948out_no_pagelock:
Zhaolong Zhangc915fb82021-03-02 17:42:31 +08001949 if (!inline_data && page_bufs)
Jan Kara188c2992021-08-16 11:57:04 +02001950 ext4_walk_page_buffers(NULL, inode, page_bufs, 0, len,
Zhaolong Zhangc915fb82021-03-02 17:42:31 +08001951 NULL, bput_one);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001952 brelse(inode_bh);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001953 return ret;
1954}
1955
Mingming Cao61628a32008-07-11 19:27:31 -04001956/*
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001957 * Note that we don't need to start a transaction unless we're journaling data
1958 * because we should have holes filled from ext4_page_mkwrite(). We even don't
1959 * need to file the inode to the transaction's list in ordered mode because if
1960 * we are writing back data added by write(), the inode is already there and if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001961 * we are writing back data modified via mmap(), no one guarantees in which
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001962 * transaction the data will hit the disk. In case we are journaling data, we
1963 * cannot start transaction directly because transaction start ranks above page
1964 * lock so we have to do some magic.
1965 *
Theodore Ts'ob920c752009-05-14 00:54:29 -04001966 * This function can get called via...
Theodore Ts'o20970ba2013-06-06 14:00:46 -04001967 * - ext4_writepages after taking page lock (have journal handle)
Theodore Ts'ob920c752009-05-14 00:54:29 -04001968 * - journal_submit_inode_data_buffers (no journal handle)
Artem Bityutskiyf6463b02012-07-25 18:12:04 +03001969 * - shrink_page_list via the kswapd/direct reclaim (no journal handle)
Theodore Ts'ob920c752009-05-14 00:54:29 -04001970 * - grab_page_cache when doing write_begin (have journal handle)
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001971 *
1972 * We don't do any block allocation in this function. If we have page with
1973 * multiple blocks we need to write those buffer_heads that are mapped. This
1974 * is important for mmaped based write. So if we do with blocksize 1K
1975 * truncate(f, 1024);
1976 * a = mmap(f, 0, 4096);
1977 * a[0] = 'a';
1978 * truncate(f, 4096);
1979 * we have in the page first buffer_head mapped via page_mkwrite call back
Paul Bolle90802ed2011-12-05 13:00:34 +01001980 * but other buffer_heads would be unmapped but dirty (dirty done via the
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001981 * do_wp_page). So writepage should write the first block. If we modify
1982 * the mmap area beyond 1024 we will again get a page_fault and the
1983 * page_mkwrite callback will do the block allocation and mark the
1984 * buffer_heads mapped.
1985 *
1986 * We redirty the page if we have any buffer_heads that is either delay or
1987 * unwritten in the page.
1988 *
1989 * We can get recursively called as show below.
1990 *
1991 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
1992 * ext4_writepage()
1993 *
1994 * But since we don't do any block allocation we should not deadlock.
1995 * Page also have the dirty flag cleared so we don't get recurive page_lock.
Mingming Cao61628a32008-07-11 19:27:31 -04001996 */
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001997static int ext4_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001998 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04001999{
Jan Karaf8bec372013-01-28 12:55:08 -05002000 int ret = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002001 loff_t size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002002 unsigned int len;
Jiaying Zhang744692d2010-03-04 16:14:02 -05002003 struct buffer_head *page_bufs = NULL;
Mingming Cao61628a32008-07-11 19:27:31 -04002004 struct inode *inode = page->mapping->host;
Jan Kara36ade452013-01-28 09:30:52 -05002005 struct ext4_io_submit io_submit;
Namjae Jeon1c8349a2014-05-12 08:12:25 -04002006 bool keep_towrite = false;
Alex Tomas64769242008-07-11 19:27:31 -04002007
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002008 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) {
yangerkunc2a559b2020-02-26 12:10:02 +08002009 inode->i_mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002010 unlock_page(page);
2011 return -EIO;
2012 }
2013
Lukas Czernera9c667f2011-06-06 09:51:52 -04002014 trace_ext4_writepage(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002015 size = i_size_read(inode);
Eric Biggersc93d8f82019-07-22 09:26:24 -07002016 if (page->index == size >> PAGE_SHIFT &&
2017 !ext4_verity_in_progress(inode))
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002018 len = size & ~PAGE_MASK;
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002019 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002020 len = PAGE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04002021
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002022 page_bufs = page_buffers(page);
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002023 /*
Jan Karafe386132013-01-28 21:06:42 -05002024 * We cannot do block allocation or other extent handling in this
2025 * function. If there are buffers needing that, we have to redirty
2026 * the page. But we may reach here when we do a journal commit via
2027 * journal_submit_inode_data_buffers() and in that case we must write
2028 * allocated buffers to achieve data=ordered mode guarantees.
Theodore Ts'occcd1472015-10-03 10:49:23 -04002029 *
2030 * Also, if there is only one buffer per page (the fs block
2031 * size == the page size), if one buffer needs block
2032 * allocation or needs to modify the extent tree to clear the
2033 * unwritten flag, we know that the page can't be written at
2034 * all, so we might as well refuse the write immediately.
2035 * Unfortunately if the block size != page size, we can't as
2036 * easily detect this case using ext4_walk_page_buffers(), but
2037 * for the extremely common case, this is an optimization that
2038 * skips a useless round trip through ext4_bio_write_page().
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04002039 */
Jan Kara188c2992021-08-16 11:57:04 +02002040 if (ext4_walk_page_buffers(NULL, inode, page_bufs, 0, len, NULL,
Tao Maf19d5872012-12-10 14:05:51 -05002041 ext4_bh_delay_or_unwritten)) {
Jan Karaf8bec372013-01-28 12:55:08 -05002042 redirty_page_for_writepage(wbc, page);
Theodore Ts'occcd1472015-10-03 10:49:23 -04002043 if ((current->flags & PF_MEMALLOC) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002044 (inode->i_sb->s_blocksize == PAGE_SIZE)) {
Jan Karafe386132013-01-28 21:06:42 -05002045 /*
2046 * For memory cleaning there's no point in writing only
2047 * some buffers. So just bail out. Warn if we came here
2048 * from direct reclaim.
2049 */
2050 WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD))
2051 == PF_MEMALLOC);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002052 unlock_page(page);
2053 return 0;
2054 }
Namjae Jeon1c8349a2014-05-12 08:12:25 -04002055 keep_towrite = true;
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002056 }
Alex Tomas64769242008-07-11 19:27:31 -04002057
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002058 if (PageChecked(page) && ext4_should_journal_data(inode))
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002059 /*
2060 * It's mmapped pagecache. Add buffers and journal it. There
2061 * doesn't seem much point in redirtying the page here.
2062 */
Wu Fengguang3f0ca302009-11-24 11:15:44 -05002063 return __ext4_journalled_writepage(page, len);
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002064
Jan Kara97a851e2013-06-04 11:58:58 -04002065 ext4_io_submit_init(&io_submit, wbc);
2066 io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS);
2067 if (!io_submit.io_end) {
2068 redirty_page_for_writepage(wbc, page);
2069 unlock_page(page);
2070 return -ENOMEM;
2071 }
Lei Chenbe993932020-12-11 14:54:24 +08002072 ret = ext4_bio_write_page(&io_submit, page, len, keep_towrite);
Jan Kara36ade452013-01-28 09:30:52 -05002073 ext4_io_submit(&io_submit);
Jan Kara97a851e2013-06-04 11:58:58 -04002074 /* Drop io_end reference we got from init */
2075 ext4_put_io_end_defer(io_submit.io_end);
Alex Tomas64769242008-07-11 19:27:31 -04002076 return ret;
2077}
2078
Jan Kara5f1132b2013-08-17 10:02:33 -04002079static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page)
2080{
2081 int len;
Jan Karaa056bda2017-05-26 17:45:45 -04002082 loff_t size;
Jan Kara5f1132b2013-08-17 10:02:33 -04002083 int err;
2084
2085 BUG_ON(page->index != mpd->first_page);
Jan Karaa056bda2017-05-26 17:45:45 -04002086 clear_page_dirty_for_io(page);
2087 /*
2088 * We have to be very careful here! Nothing protects writeback path
2089 * against i_size changes and the page can be writeably mapped into
2090 * page tables. So an application can be growing i_size and writing
2091 * data through mmap while writeback runs. clear_page_dirty_for_io()
2092 * write-protects our page in page tables and the page cannot get
2093 * written to again until we release page lock. So only after
2094 * clear_page_dirty_for_io() we are safe to sample i_size for
2095 * ext4_bio_write_page() to zero-out tail of the written page. We rely
2096 * on the barrier provided by TestClearPageDirty in
2097 * clear_page_dirty_for_io() to make sure i_size is really sampled only
2098 * after page tables are updated.
2099 */
2100 size = i_size_read(mpd->inode);
Eric Biggersc93d8f82019-07-22 09:26:24 -07002101 if (page->index == size >> PAGE_SHIFT &&
2102 !ext4_verity_in_progress(mpd->inode))
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002103 len = size & ~PAGE_MASK;
Jan Kara5f1132b2013-08-17 10:02:33 -04002104 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002105 len = PAGE_SIZE;
Lei Chenbe993932020-12-11 14:54:24 +08002106 err = ext4_bio_write_page(&mpd->io_submit, page, len, false);
Jan Kara5f1132b2013-08-17 10:02:33 -04002107 if (!err)
2108 mpd->wbc->nr_to_write--;
2109 mpd->first_page++;
2110
2111 return err;
2112}
2113
Ritesh Harjani6db07462020-05-10 11:54:51 +05302114#define BH_FLAGS (BIT(BH_Unwritten) | BIT(BH_Delay))
Jan Kara4e7ea812013-06-04 13:17:40 -04002115
Mingming Cao61628a32008-07-11 19:27:31 -04002116/*
Jan Karafffb2732013-06-04 13:01:11 -04002117 * mballoc gives us at most this number of blocks...
2118 * XXX: That seems to be only a limitation of ext4_mb_normalize_request().
Anatol Pomozov70261f52013-08-28 14:40:12 -04002119 * The rest of mballoc seems to handle chunks up to full group size.
Mingming Cao61628a32008-07-11 19:27:31 -04002120 */
Jan Karafffb2732013-06-04 13:01:11 -04002121#define MAX_WRITEPAGES_EXTENT_LEN 2048
Mingming Cao525f4ed2008-08-19 22:15:58 -04002122
Jan Karafffb2732013-06-04 13:01:11 -04002123/*
Jan Kara4e7ea812013-06-04 13:17:40 -04002124 * mpage_add_bh_to_extent - try to add bh to extent of blocks to map
2125 *
2126 * @mpd - extent of blocks
2127 * @lblk - logical number of the block in the file
Jan Kara09930042013-08-17 09:57:56 -04002128 * @bh - buffer head we want to add to the extent
Jan Kara4e7ea812013-06-04 13:17:40 -04002129 *
Jan Kara09930042013-08-17 09:57:56 -04002130 * The function is used to collect contig. blocks in the same state. If the
2131 * buffer doesn't require mapping for writeback and we haven't started the
2132 * extent of buffers to map yet, the function returns 'true' immediately - the
2133 * caller can write the buffer right away. Otherwise the function returns true
2134 * if the block has been added to the extent, false if the block couldn't be
2135 * added.
Jan Kara4e7ea812013-06-04 13:17:40 -04002136 */
Jan Kara09930042013-08-17 09:57:56 -04002137static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk,
2138 struct buffer_head *bh)
Jan Kara4e7ea812013-06-04 13:17:40 -04002139{
2140 struct ext4_map_blocks *map = &mpd->map;
2141
Jan Kara09930042013-08-17 09:57:56 -04002142 /* Buffer that doesn't need mapping for writeback? */
2143 if (!buffer_dirty(bh) || !buffer_mapped(bh) ||
2144 (!buffer_delay(bh) && !buffer_unwritten(bh))) {
2145 /* So far no extent to map => we write the buffer right away */
2146 if (map->m_len == 0)
2147 return true;
2148 return false;
2149 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002150
2151 /* First block in the extent? */
2152 if (map->m_len == 0) {
Jan Karadddbd6a2017-04-30 18:29:10 -04002153 /* We cannot map unless handle is started... */
2154 if (!mpd->do_map)
2155 return false;
Jan Kara4e7ea812013-06-04 13:17:40 -04002156 map->m_lblk = lblk;
2157 map->m_len = 1;
Jan Kara09930042013-08-17 09:57:56 -04002158 map->m_flags = bh->b_state & BH_FLAGS;
2159 return true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002160 }
2161
Jan Kara09930042013-08-17 09:57:56 -04002162 /* Don't go larger than mballoc is willing to allocate */
2163 if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN)
2164 return false;
2165
Jan Kara4e7ea812013-06-04 13:17:40 -04002166 /* Can we merge the block to our big extent? */
2167 if (lblk == map->m_lblk + map->m_len &&
Jan Kara09930042013-08-17 09:57:56 -04002168 (bh->b_state & BH_FLAGS) == map->m_flags) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002169 map->m_len++;
Jan Kara09930042013-08-17 09:57:56 -04002170 return true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002171 }
Jan Kara09930042013-08-17 09:57:56 -04002172 return false;
Jan Kara4e7ea812013-06-04 13:17:40 -04002173}
2174
Jan Kara5f1132b2013-08-17 10:02:33 -04002175/*
2176 * mpage_process_page_bufs - submit page buffers for IO or add them to extent
2177 *
2178 * @mpd - extent of blocks for mapping
2179 * @head - the first buffer in the page
2180 * @bh - buffer we should start processing from
2181 * @lblk - logical number of the block in the file corresponding to @bh
2182 *
2183 * Walk through page buffers from @bh upto @head (exclusive) and either submit
2184 * the page for IO if all buffers in this page were mapped and there's no
2185 * accumulated extent of buffers to map or add buffers in the page to the
2186 * extent of buffers to map. The function returns 1 if the caller can continue
2187 * by processing the next page, 0 if it should stop adding buffers to the
2188 * extent to map because we cannot extend it anymore. It can also return value
2189 * < 0 in case of error during IO submission.
2190 */
2191static int mpage_process_page_bufs(struct mpage_da_data *mpd,
2192 struct buffer_head *head,
2193 struct buffer_head *bh,
2194 ext4_lblk_t lblk)
Jan Kara4e7ea812013-06-04 13:17:40 -04002195{
2196 struct inode *inode = mpd->inode;
Jan Kara5f1132b2013-08-17 10:02:33 -04002197 int err;
Fabian Frederick93407472017-02-27 14:28:32 -08002198 ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(inode) - 1)
Jan Kara4e7ea812013-06-04 13:17:40 -04002199 >> inode->i_blkbits;
2200
Eric Biggersc93d8f82019-07-22 09:26:24 -07002201 if (ext4_verity_in_progress(inode))
2202 blocks = EXT_MAX_BLOCKS;
2203
Jan Kara4e7ea812013-06-04 13:17:40 -04002204 do {
2205 BUG_ON(buffer_locked(bh));
2206
Jan Kara09930042013-08-17 09:57:56 -04002207 if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002208 /* Found extent to map? */
2209 if (mpd->map.m_len)
Jan Kara5f1132b2013-08-17 10:02:33 -04002210 return 0;
Jan Karadddbd6a2017-04-30 18:29:10 -04002211 /* Buffer needs mapping and handle is not started? */
2212 if (!mpd->do_map)
2213 return 0;
Jan Kara09930042013-08-17 09:57:56 -04002214 /* Everything mapped so far and we hit EOF */
Jan Kara5f1132b2013-08-17 10:02:33 -04002215 break;
Jan Kara4e7ea812013-06-04 13:17:40 -04002216 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002217 } while (lblk++, (bh = bh->b_this_page) != head);
Jan Kara5f1132b2013-08-17 10:02:33 -04002218 /* So far everything mapped? Submit the page for IO. */
2219 if (mpd->map.m_len == 0) {
2220 err = mpage_submit_page(mpd, head->b_page);
2221 if (err < 0)
2222 return err;
2223 }
Jan Kara6b8ed622020-05-25 10:12:15 +02002224 if (lblk >= blocks) {
2225 mpd->scanned_until_end = 1;
2226 return 0;
2227 }
2228 return 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04002229}
2230
2231/*
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302232 * mpage_process_page - update page buffers corresponding to changed extent and
2233 * may submit fully mapped page for IO
2234 *
2235 * @mpd - description of extent to map, on return next extent to map
2236 * @m_lblk - logical block mapping.
2237 * @m_pblk - corresponding physical mapping.
2238 * @map_bh - determines on return whether this page requires any further
2239 * mapping or not.
2240 * Scan given page buffers corresponding to changed extent and update buffer
2241 * state according to new extent state.
2242 * We map delalloc buffers to their physical location, clear unwritten bits.
2243 * If the given page is not fully mapped, we update @map to the next extent in
2244 * the given page that needs mapping & return @map_bh as true.
2245 */
2246static int mpage_process_page(struct mpage_da_data *mpd, struct page *page,
2247 ext4_lblk_t *m_lblk, ext4_fsblk_t *m_pblk,
2248 bool *map_bh)
2249{
2250 struct buffer_head *head, *bh;
2251 ext4_io_end_t *io_end = mpd->io_submit.io_end;
2252 ext4_lblk_t lblk = *m_lblk;
2253 ext4_fsblk_t pblock = *m_pblk;
2254 int err = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302255 int blkbits = mpd->inode->i_blkbits;
2256 ssize_t io_end_size = 0;
2257 struct ext4_io_end_vec *io_end_vec = ext4_last_io_end_vec(io_end);
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302258
2259 bh = head = page_buffers(page);
2260 do {
2261 if (lblk < mpd->map.m_lblk)
2262 continue;
2263 if (lblk >= mpd->map.m_lblk + mpd->map.m_len) {
2264 /*
2265 * Buffer after end of mapped extent.
2266 * Find next buffer in the page to map.
2267 */
2268 mpd->map.m_len = 0;
2269 mpd->map.m_flags = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302270 io_end_vec->size += io_end_size;
2271 io_end_size = 0;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302272
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302273 err = mpage_process_page_bufs(mpd, head, bh, lblk);
2274 if (err > 0)
2275 err = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302276 if (!err && mpd->map.m_len && mpd->map.m_lblk > lblk) {
2277 io_end_vec = ext4_alloc_io_end_vec(io_end);
Ritesh Harjani4d06bfb2019-11-06 15:08:09 +05302278 if (IS_ERR(io_end_vec)) {
2279 err = PTR_ERR(io_end_vec);
2280 goto out;
2281 }
Ritesh Harjanid1e18b82020-10-08 20:32:48 +05302282 io_end_vec->offset = (loff_t)mpd->map.m_lblk << blkbits;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302283 }
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302284 *map_bh = true;
2285 goto out;
2286 }
2287 if (buffer_delay(bh)) {
2288 clear_buffer_delay(bh);
2289 bh->b_blocknr = pblock++;
2290 }
2291 clear_buffer_unwritten(bh);
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302292 io_end_size += (1 << blkbits);
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302293 } while (lblk++, (bh = bh->b_this_page) != head);
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302294
2295 io_end_vec->size += io_end_size;
2296 io_end_size = 0;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302297 *map_bh = false;
2298out:
2299 *m_lblk = lblk;
2300 *m_pblk = pblock;
2301 return err;
2302}
2303
2304/*
Jan Kara4e7ea812013-06-04 13:17:40 -04002305 * mpage_map_buffers - update buffers corresponding to changed extent and
2306 * submit fully mapped pages for IO
2307 *
2308 * @mpd - description of extent to map, on return next extent to map
2309 *
2310 * Scan buffers corresponding to changed extent (we expect corresponding pages
2311 * to be already locked) and update buffer state according to new extent state.
2312 * We map delalloc buffers to their physical location, clear unwritten bits,
Lukas Czerner556615d2014-04-20 23:45:47 -04002313 * and mark buffers as uninit when we perform writes to unwritten extents
Jan Kara4e7ea812013-06-04 13:17:40 -04002314 * and do extent conversion after IO is finished. If the last page is not fully
2315 * mapped, we update @map to the next extent in the last page that needs
2316 * mapping. Otherwise we submit the page for IO.
2317 */
2318static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd)
2319{
2320 struct pagevec pvec;
2321 int nr_pages, i;
2322 struct inode *inode = mpd->inode;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002323 int bpp_bits = PAGE_SHIFT - inode->i_blkbits;
Jan Kara4e7ea812013-06-04 13:17:40 -04002324 pgoff_t start, end;
2325 ext4_lblk_t lblk;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302326 ext4_fsblk_t pblock;
Jan Kara4e7ea812013-06-04 13:17:40 -04002327 int err;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302328 bool map_bh = false;
Jan Kara4e7ea812013-06-04 13:17:40 -04002329
2330 start = mpd->map.m_lblk >> bpp_bits;
2331 end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits;
2332 lblk = start << bpp_bits;
2333 pblock = mpd->map.m_pblk;
2334
Mel Gorman86679822017-11-15 17:37:52 -08002335 pagevec_init(&pvec);
Jan Kara4e7ea812013-06-04 13:17:40 -04002336 while (start <= end) {
Jan Kara2b85a612017-09-06 16:21:30 -07002337 nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping,
Jan Kara397162f2017-09-06 16:21:43 -07002338 &start, end);
Jan Kara4e7ea812013-06-04 13:17:40 -04002339 if (nr_pages == 0)
2340 break;
2341 for (i = 0; i < nr_pages; i++) {
2342 struct page *page = pvec.pages[i];
2343
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302344 err = mpage_process_page(mpd, page, &lblk, &pblock,
2345 &map_bh);
Jan Kara4e7ea812013-06-04 13:17:40 -04002346 /*
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302347 * If map_bh is true, means page may require further bh
2348 * mapping, or maybe the page was submitted for IO.
2349 * So we return to call further extent mapping.
Jan Kara4e7ea812013-06-04 13:17:40 -04002350 */
Jason Yan39c0ae12020-04-20 12:29:18 +08002351 if (err < 0 || map_bh)
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302352 goto out;
Jan Kara4e7ea812013-06-04 13:17:40 -04002353 /* Page fully mapped - let IO run! */
2354 err = mpage_submit_page(mpd, page);
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302355 if (err < 0)
2356 goto out;
Jan Kara4e7ea812013-06-04 13:17:40 -04002357 }
2358 pagevec_release(&pvec);
2359 }
2360 /* Extent fully mapped and matches with page boundary. We are done. */
2361 mpd->map.m_len = 0;
2362 mpd->map.m_flags = 0;
2363 return 0;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302364out:
2365 pagevec_release(&pvec);
2366 return err;
Jan Kara4e7ea812013-06-04 13:17:40 -04002367}
2368
2369static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd)
2370{
2371 struct inode *inode = mpd->inode;
2372 struct ext4_map_blocks *map = &mpd->map;
2373 int get_blocks_flags;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002374 int err, dioread_nolock;
Jan Kara4e7ea812013-06-04 13:17:40 -04002375
2376 trace_ext4_da_write_pages_extent(inode, map);
2377 /*
2378 * Call ext4_map_blocks() to allocate any delayed allocation blocks, or
Lukas Czerner556615d2014-04-20 23:45:47 -04002379 * to convert an unwritten extent to be initialized (in the case
Jan Kara4e7ea812013-06-04 13:17:40 -04002380 * where we have written into one or more preallocated blocks). It is
2381 * possible that we're going to need more metadata blocks than
2382 * previously reserved. However we must not fail because we're in
2383 * writeback and there is nothing we can do about it so it might result
2384 * in data loss. So use reserved blocks to allocate metadata if
2385 * possible.
2386 *
Theodore Ts'o754cfed2014-09-04 18:08:22 -04002387 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if
2388 * the blocks in question are delalloc blocks. This indicates
2389 * that the blocks and quotas has already been checked when
2390 * the data was copied into the page cache.
Jan Kara4e7ea812013-06-04 13:17:40 -04002391 */
2392 get_blocks_flags = EXT4_GET_BLOCKS_CREATE |
Jan Karaee0876b2016-04-24 00:56:08 -04002393 EXT4_GET_BLOCKS_METADATA_NOFAIL |
2394 EXT4_GET_BLOCKS_IO_SUBMIT;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002395 dioread_nolock = ext4_should_dioread_nolock(inode);
2396 if (dioread_nolock)
Jan Kara4e7ea812013-06-04 13:17:40 -04002397 get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
Ritesh Harjani6db07462020-05-10 11:54:51 +05302398 if (map->m_flags & BIT(BH_Delay))
Jan Kara4e7ea812013-06-04 13:17:40 -04002399 get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
2400
2401 err = ext4_map_blocks(handle, inode, map, get_blocks_flags);
2402 if (err < 0)
2403 return err;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002404 if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) {
Jan Kara6b523df2013-06-04 13:21:11 -04002405 if (!mpd->io_submit.io_end->handle &&
2406 ext4_handle_valid(handle)) {
2407 mpd->io_submit.io_end->handle = handle->h_rsv_handle;
2408 handle->h_rsv_handle = NULL;
2409 }
Jan Kara3613d222013-06-04 13:19:34 -04002410 ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end);
Jan Kara6b523df2013-06-04 13:21:11 -04002411 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002412
2413 BUG_ON(map->m_len == 0);
Jan Kara4e7ea812013-06-04 13:17:40 -04002414 return 0;
2415}
2416
2417/*
2418 * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length
2419 * mpd->len and submit pages underlying it for IO
2420 *
2421 * @handle - handle for journal operations
2422 * @mpd - extent to map
Jan Kara7534e852013-10-16 08:26:08 -04002423 * @give_up_on_write - we set this to true iff there is a fatal error and there
2424 * is no hope of writing the data. The caller should discard
2425 * dirty pages to avoid infinite loops.
Jan Kara4e7ea812013-06-04 13:17:40 -04002426 *
2427 * The function maps extent starting at mpd->lblk of length mpd->len. If it is
2428 * delayed, blocks are allocated, if it is unwritten, we may need to convert
2429 * them to initialized or split the described range from larger unwritten
2430 * extent. Note that we need not map all the described range since allocation
2431 * can return less blocks or the range is covered by more unwritten extents. We
2432 * cannot map more because we are limited by reserved transaction credits. On
2433 * the other hand we always make sure that the last touched page is fully
2434 * mapped so that it can be written out (and thus forward progress is
2435 * guaranteed). After mapping we submit all mapped pages for IO.
2436 */
2437static int mpage_map_and_submit_extent(handle_t *handle,
Theodore Ts'ocb530542013-07-01 08:12:40 -04002438 struct mpage_da_data *mpd,
2439 bool *give_up_on_write)
Jan Kara4e7ea812013-06-04 13:17:40 -04002440{
2441 struct inode *inode = mpd->inode;
2442 struct ext4_map_blocks *map = &mpd->map;
2443 int err;
2444 loff_t disksize;
Dmitry Monakhov66031202014-08-27 18:40:03 -04002445 int progress = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302446 ext4_io_end_t *io_end = mpd->io_submit.io_end;
Ritesh Harjani4d06bfb2019-11-06 15:08:09 +05302447 struct ext4_io_end_vec *io_end_vec;
Jan Kara4e7ea812013-06-04 13:17:40 -04002448
Ritesh Harjani4d06bfb2019-11-06 15:08:09 +05302449 io_end_vec = ext4_alloc_io_end_vec(io_end);
2450 if (IS_ERR(io_end_vec))
2451 return PTR_ERR(io_end_vec);
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302452 io_end_vec->offset = ((loff_t)map->m_lblk) << inode->i_blkbits;
Jan Kara27d7c4e2013-07-05 21:57:22 -04002453 do {
Jan Kara4e7ea812013-06-04 13:17:40 -04002454 err = mpage_map_one_extent(handle, mpd);
2455 if (err < 0) {
2456 struct super_block *sb = inode->i_sb;
2457
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002458 if (ext4_forced_shutdown(EXT4_SB(sb)) ||
Harshad Shirwadkar9b5f6c92020-11-05 19:59:09 -08002459 ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED))
Theodore Ts'ocb530542013-07-01 08:12:40 -04002460 goto invalidate_dirty_pages;
Jan Kara4e7ea812013-06-04 13:17:40 -04002461 /*
Theodore Ts'ocb530542013-07-01 08:12:40 -04002462 * Let the uper layers retry transient errors.
2463 * In the case of ENOSPC, if ext4_count_free_blocks()
2464 * is non-zero, a commit should free up blocks.
Jan Kara4e7ea812013-06-04 13:17:40 -04002465 */
Theodore Ts'ocb530542013-07-01 08:12:40 -04002466 if ((err == -ENOMEM) ||
Dmitry Monakhov66031202014-08-27 18:40:03 -04002467 (err == -ENOSPC && ext4_count_free_clusters(sb))) {
2468 if (progress)
2469 goto update_disksize;
Theodore Ts'ocb530542013-07-01 08:12:40 -04002470 return err;
Dmitry Monakhov66031202014-08-27 18:40:03 -04002471 }
Theodore Ts'ocb530542013-07-01 08:12:40 -04002472 ext4_msg(sb, KERN_CRIT,
2473 "Delayed block allocation failed for "
2474 "inode %lu at logical offset %llu with"
2475 " max blocks %u with error %d",
2476 inode->i_ino,
2477 (unsigned long long)map->m_lblk,
2478 (unsigned)map->m_len, -err);
2479 ext4_msg(sb, KERN_CRIT,
2480 "This should not happen!! Data will "
2481 "be lost\n");
2482 if (err == -ENOSPC)
2483 ext4_print_free_blocks(inode);
2484 invalidate_dirty_pages:
2485 *give_up_on_write = true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002486 return err;
2487 }
Dmitry Monakhov66031202014-08-27 18:40:03 -04002488 progress = 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04002489 /*
2490 * Update buffer state, submit mapped pages, and get us new
2491 * extent to map
2492 */
2493 err = mpage_map_and_submit_buffers(mpd);
2494 if (err < 0)
Dmitry Monakhov66031202014-08-27 18:40:03 -04002495 goto update_disksize;
Jan Kara27d7c4e2013-07-05 21:57:22 -04002496 } while (map->m_len);
Jan Kara4e7ea812013-06-04 13:17:40 -04002497
Dmitry Monakhov66031202014-08-27 18:40:03 -04002498update_disksize:
Theodore Ts'o622cad12014-04-11 10:35:17 -04002499 /*
2500 * Update on-disk size after IO is submitted. Races with
2501 * truncate are avoided by checking i_size under i_data_sem.
2502 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002503 disksize = ((loff_t)mpd->first_page) << PAGE_SHIFT;
Qian Cai35df4292020-02-07 09:29:11 -05002504 if (disksize > READ_ONCE(EXT4_I(inode)->i_disksize)) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002505 int err2;
Theodore Ts'o622cad12014-04-11 10:35:17 -04002506 loff_t i_size;
Jan Kara4e7ea812013-06-04 13:17:40 -04002507
Theodore Ts'o622cad12014-04-11 10:35:17 -04002508 down_write(&EXT4_I(inode)->i_data_sem);
2509 i_size = i_size_read(inode);
2510 if (disksize > i_size)
2511 disksize = i_size;
2512 if (disksize > EXT4_I(inode)->i_disksize)
2513 EXT4_I(inode)->i_disksize = disksize;
Theodore Ts'o622cad12014-04-11 10:35:17 -04002514 up_write(&EXT4_I(inode)->i_data_sem);
Theodore Ts'ob907f2d2017-01-11 22:14:49 -05002515 err2 = ext4_mark_inode_dirty(handle, inode);
Theodore Ts'o878520a2019-11-19 21:54:15 -05002516 if (err2) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04002517 ext4_error_err(inode->i_sb, -err2,
2518 "Failed to mark inode %lu dirty",
2519 inode->i_ino);
Theodore Ts'o878520a2019-11-19 21:54:15 -05002520 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002521 if (!err)
2522 err = err2;
2523 }
2524 return err;
2525}
2526
2527/*
Jan Karafffb2732013-06-04 13:01:11 -04002528 * Calculate the total number of credits to reserve for one writepages
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002529 * iteration. This is called from ext4_writepages(). We map an extent of
Anatol Pomozov70261f52013-08-28 14:40:12 -04002530 * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping
Jan Karafffb2732013-06-04 13:01:11 -04002531 * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN +
2532 * bpp - 1 blocks in bpp different extents.
2533 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002534static int ext4_da_writepages_trans_blocks(struct inode *inode)
2535{
Jan Karafffb2732013-06-04 13:01:11 -04002536 int bpp = ext4_journal_blocks_per_page(inode);
Mingming Cao525f4ed2008-08-19 22:15:58 -04002537
Jan Karafffb2732013-06-04 13:01:11 -04002538 return ext4_meta_trans_blocks(inode,
2539 MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp);
Mingming Cao525f4ed2008-08-19 22:15:58 -04002540}
Mingming Cao61628a32008-07-11 19:27:31 -04002541
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002542/*
Jan Kara4e7ea812013-06-04 13:17:40 -04002543 * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages
2544 * and underlying extent to map
2545 *
2546 * @mpd - where to look for pages
2547 *
2548 * Walk dirty pages in the mapping. If they are fully mapped, submit them for
2549 * IO immediately. When we find a page which isn't mapped we start accumulating
2550 * extent of buffers underlying these pages that needs mapping (formed by
2551 * either delayed or unwritten buffers). We also lock the pages containing
2552 * these buffers. The extent found is returned in @mpd structure (starting at
2553 * mpd->lblk with length mpd->len blocks).
2554 *
2555 * Note that this function can attach bios to one io_end structure which are
2556 * neither logically nor physically contiguous. Although it may seem as an
2557 * unnecessary complication, it is actually inevitable in blocksize < pagesize
2558 * 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 -04002559 */
Jan Kara4e7ea812013-06-04 13:17:40 -04002560static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd)
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002561{
Jan Kara4e7ea812013-06-04 13:17:40 -04002562 struct address_space *mapping = mpd->inode->i_mapping;
2563 struct pagevec pvec;
2564 unsigned int nr_pages;
Ming Leiaeac5892013-10-17 18:56:16 -04002565 long left = mpd->wbc->nr_to_write;
Jan Kara4e7ea812013-06-04 13:17:40 -04002566 pgoff_t index = mpd->first_page;
2567 pgoff_t end = mpd->last_page;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05002568 xa_mark_t tag;
Jan Kara4e7ea812013-06-04 13:17:40 -04002569 int i, err = 0;
2570 int blkbits = mpd->inode->i_blkbits;
2571 ext4_lblk_t lblk;
2572 struct buffer_head *head;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002573
Jan Kara4e7ea812013-06-04 13:17:40 -04002574 if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages)
Eric Sandeen5b41d922010-10-27 21:30:13 -04002575 tag = PAGECACHE_TAG_TOWRITE;
2576 else
2577 tag = PAGECACHE_TAG_DIRTY;
2578
Mel Gorman86679822017-11-15 17:37:52 -08002579 pagevec_init(&pvec);
Jan Kara4e7ea812013-06-04 13:17:40 -04002580 mpd->map.m_len = 0;
2581 mpd->next_page = index;
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002582 while (index <= end) {
Jan Karadc7f3e82017-11-15 17:34:44 -08002583 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08002584 tag);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002585 if (nr_pages == 0)
Jan Kara6b8ed622020-05-25 10:12:15 +02002586 break;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002587
2588 for (i = 0; i < nr_pages; i++) {
2589 struct page *page = pvec.pages[i];
2590
2591 /*
Ming Leiaeac5892013-10-17 18:56:16 -04002592 * Accumulated enough dirty pages? This doesn't apply
2593 * to WB_SYNC_ALL mode. For integrity sync we have to
2594 * keep going because someone may be concurrently
2595 * dirtying pages, and we might have synced a lot of
2596 * newly appeared dirty pages, but have not synced all
2597 * of the old dirty pages.
2598 */
2599 if (mpd->wbc->sync_mode == WB_SYNC_NONE && left <= 0)
2600 goto out;
2601
Jan Kara4e7ea812013-06-04 13:17:40 -04002602 /* If we can't merge this page, we are done. */
2603 if (mpd->map.m_len > 0 && mpd->next_page != page->index)
2604 goto out;
Theodore Ts'o78aaced2011-02-26 14:09:14 -05002605
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002606 lock_page(page);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002607 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002608 * If the page is no longer dirty, or its mapping no
2609 * longer corresponds to inode we are writing (which
2610 * means it has been truncated or invalidated), or the
2611 * page is already under writeback and we are not doing
2612 * a data integrity writeback, skip the page
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002613 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002614 if (!PageDirty(page) ||
2615 (PageWriteback(page) &&
Jan Kara4e7ea812013-06-04 13:17:40 -04002616 (mpd->wbc->sync_mode == WB_SYNC_NONE)) ||
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002617 unlikely(page->mapping != mapping)) {
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002618 unlock_page(page);
2619 continue;
2620 }
2621
Darrick J. Wong7cb1a532011-05-18 13:53:20 -04002622 wait_on_page_writeback(page);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002623 BUG_ON(PageWriteback(page));
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002624
Jan Kara4e7ea812013-06-04 13:17:40 -04002625 if (mpd->map.m_len == 0)
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002626 mpd->first_page = page->index;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002627 mpd->next_page = page->index + 1;
Jan Karaf8bec372013-01-28 12:55:08 -05002628 /* Add all dirty buffers to mpd */
Jan Kara4e7ea812013-06-04 13:17:40 -04002629 lblk = ((ext4_lblk_t)page->index) <<
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002630 (PAGE_SHIFT - blkbits);
Jan Karaf8bec372013-01-28 12:55:08 -05002631 head = page_buffers(page);
Jan Kara5f1132b2013-08-17 10:02:33 -04002632 err = mpage_process_page_bufs(mpd, head, head, lblk);
2633 if (err <= 0)
Jan Kara4e7ea812013-06-04 13:17:40 -04002634 goto out;
Jan Kara5f1132b2013-08-17 10:02:33 -04002635 err = 0;
Ming Leiaeac5892013-10-17 18:56:16 -04002636 left--;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002637 }
2638 pagevec_release(&pvec);
2639 cond_resched();
2640 }
Jan Kara6b8ed622020-05-25 10:12:15 +02002641 mpd->scanned_until_end = 1;
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002642 return 0;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002643out:
2644 pagevec_release(&pvec);
Jan Kara4e7ea812013-06-04 13:17:40 -04002645 return err;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002646}
2647
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002648static int ext4_writepages(struct address_space *mapping,
2649 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002650{
Jan Kara4e7ea812013-06-04 13:17:40 -04002651 pgoff_t writeback_index = 0;
2652 long nr_to_write = wbc->nr_to_write;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002653 int range_whole = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002654 int cycled = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002655 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002656 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002657 struct inode *inode = mapping->host;
Jan Kara6b523df2013-06-04 13:21:11 -04002658 int needed_blocks, rsv_blocks = 0, ret = 0;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002659 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Shaohua Li1bce63d12011-10-18 10:55:51 -04002660 struct blk_plug plug;
Theodore Ts'ocb530542013-07-01 08:12:40 -04002661 bool give_up_on_write = false;
Mingming Cao61628a32008-07-11 19:27:31 -04002662
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002663 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
2664 return -EIO;
2665
Eric Biggersbbd55932020-02-19 10:30:46 -08002666 percpu_down_read(&sbi->s_writepages_rwsem);
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002667 trace_ext4_writepages(inode, wbc);
Theodore Ts'oba80b102009-01-03 20:03:21 -05002668
Mingming Cao61628a32008-07-11 19:27:31 -04002669 /*
2670 * No pages to write? This is mainly a kludge to avoid starting
2671 * a transaction for special inodes like journal inode on last iput()
2672 * because that could violate lock ordering on umount
2673 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002674 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Ming Leibbf023c72013-10-30 07:27:16 -04002675 goto out_writepages;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002676
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002677 if (ext4_should_journal_data(inode)) {
Goldwyn Rodrigues043d20d2018-03-26 01:32:50 -04002678 ret = generic_writepages(mapping, wbc);
Ming Leibbf023c72013-10-30 07:27:16 -04002679 goto out_writepages;
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002680 }
2681
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002682 /*
2683 * If the filesystem has aborted, it is read-only, so return
2684 * right away instead of dumping stack traces later on that
2685 * will obscure the real source of the problem. We test
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002686 * EXT4_MF_FS_ABORTED instead of sb->s_flag's SB_RDONLY because
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002687 * the latter could be true if the filesystem is mounted
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002688 * read-only, and in that case, ext4_writepages should
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002689 * *never* be called, so if that ever happens, we would want
2690 * the stack trace.
2691 */
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002692 if (unlikely(ext4_forced_shutdown(EXT4_SB(mapping->host->i_sb)) ||
Harshad Shirwadkar9b5f6c92020-11-05 19:59:09 -08002693 ext4_test_mount_flag(inode->i_sb, EXT4_MF_FS_ABORTED))) {
Ming Leibbf023c72013-10-30 07:27:16 -04002694 ret = -EROFS;
2695 goto out_writepages;
2696 }
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002697
Jan Kara4e7ea812013-06-04 13:17:40 -04002698 /*
2699 * If we have inline data and arrive here, it means that
2700 * we will soon create the block for the 1st page, so
2701 * we'd better clear the inline data here.
2702 */
2703 if (ext4_has_inline_data(inode)) {
2704 /* Just inode will be modified... */
2705 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
2706 if (IS_ERR(handle)) {
2707 ret = PTR_ERR(handle);
2708 goto out_writepages;
2709 }
2710 BUG_ON(ext4_test_inode_state(inode,
2711 EXT4_STATE_MAY_INLINE_DATA));
2712 ext4_destroy_inline_data(handle, inode);
2713 ext4_journal_stop(handle);
2714 }
2715
yangerkun4e343232019-08-11 16:27:41 -04002716 if (ext4_should_dioread_nolock(inode)) {
2717 /*
2718 * We may need to convert up to one extent per block in
2719 * the page and we may dirty the inode.
2720 */
2721 rsv_blocks = 1 + ext4_chunk_trans_blocks(inode,
2722 PAGE_SIZE >> inode->i_blkbits);
2723 }
2724
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002725 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2726 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002727
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002728 if (wbc->range_cyclic) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002729 writeback_index = mapping->writeback_index;
2730 if (writeback_index)
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002731 cycled = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002732 mpd.first_page = writeback_index;
2733 mpd.last_page = -1;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002734 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002735 mpd.first_page = wbc->range_start >> PAGE_SHIFT;
2736 mpd.last_page = wbc->range_end >> PAGE_SHIFT;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002737 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002738
Jan Kara4e7ea812013-06-04 13:17:40 -04002739 mpd.inode = inode;
2740 mpd.wbc = wbc;
2741 ext4_io_submit_init(&mpd.io_submit, wbc);
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002742retry:
Wu Fengguang6e6938b2010-06-06 10:38:15 -06002743 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Jan Kara4e7ea812013-06-04 13:17:40 -04002744 tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page);
Shaohua Li1bce63d12011-10-18 10:55:51 -04002745 blk_start_plug(&plug);
Jan Karadddbd6a2017-04-30 18:29:10 -04002746
2747 /*
2748 * First writeback pages that don't need mapping - we can avoid
2749 * starting a transaction unnecessarily and also avoid being blocked
2750 * in the block layer on device congestion while having transaction
2751 * started.
2752 */
2753 mpd.do_map = 0;
Jan Kara6b8ed622020-05-25 10:12:15 +02002754 mpd.scanned_until_end = 0;
Jan Karadddbd6a2017-04-30 18:29:10 -04002755 mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
2756 if (!mpd.io_submit.io_end) {
2757 ret = -ENOMEM;
2758 goto unplug;
2759 }
2760 ret = mpage_prepare_extent_to_map(&mpd);
Xiaoguang Wanga297b2f2019-02-10 23:53:21 -05002761 /* Unlock pages we didn't use */
2762 mpage_release_unused_pages(&mpd, false);
Jan Karadddbd6a2017-04-30 18:29:10 -04002763 /* Submit prepared bio */
2764 ext4_io_submit(&mpd.io_submit);
2765 ext4_put_io_end_defer(mpd.io_submit.io_end);
2766 mpd.io_submit.io_end = NULL;
Jan Karadddbd6a2017-04-30 18:29:10 -04002767 if (ret < 0)
2768 goto unplug;
2769
Jan Kara6b8ed622020-05-25 10:12:15 +02002770 while (!mpd.scanned_until_end && wbc->nr_to_write > 0) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002771 /* For each extent of pages we use new io_end */
2772 mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
2773 if (!mpd.io_submit.io_end) {
2774 ret = -ENOMEM;
2775 break;
2776 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002777
2778 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002779 * We have two constraints: We find one extent to map and we
2780 * must always write out whole page (makes a difference when
2781 * blocksize < pagesize) so that we don't block on IO when we
2782 * try to write out the rest of the page. Journalled mode is
2783 * not supported by delalloc.
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002784 */
2785 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04002786 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002787
Jan Kara4e7ea812013-06-04 13:17:40 -04002788 /* start a new transaction */
Jan Kara6b523df2013-06-04 13:21:11 -04002789 handle = ext4_journal_start_with_reserve(inode,
2790 EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks);
Mingming Cao61628a32008-07-11 19:27:31 -04002791 if (IS_ERR(handle)) {
2792 ret = PTR_ERR(handle);
Theodore Ts'o16939182009-09-26 17:43:59 -04002793 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
Curt Wohlgemuthfbe845d2010-05-16 13:00:00 -04002794 "%ld pages, ino %lu; err %d", __func__,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002795 wbc->nr_to_write, inode->i_ino, ret);
Jan Kara4e7ea812013-06-04 13:17:40 -04002796 /* Release allocated io_end */
2797 ext4_put_io_end(mpd.io_submit.io_end);
Jan Karadddbd6a2017-04-30 18:29:10 -04002798 mpd.io_submit.io_end = NULL;
Jan Kara4e7ea812013-06-04 13:17:40 -04002799 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002800 }
Jan Karadddbd6a2017-04-30 18:29:10 -04002801 mpd.do_map = 1;
Theodore Ts'of63e60052009-02-23 16:42:39 -05002802
Jan Kara4e7ea812013-06-04 13:17:40 -04002803 trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc);
2804 ret = mpage_prepare_extent_to_map(&mpd);
Jan Kara6b8ed622020-05-25 10:12:15 +02002805 if (!ret && mpd.map.m_len)
2806 ret = mpage_map_and_submit_extent(handle, &mpd,
Theodore Ts'ocb530542013-07-01 08:12:40 -04002807 &give_up_on_write);
Jan Kara646caa92016-07-04 10:14:01 -04002808 /*
2809 * Caution: If the handle is synchronous,
2810 * ext4_journal_stop() can wait for transaction commit
2811 * to finish which may depend on writeback of pages to
2812 * complete or on page lock to be released. In that
Randy Dunlapb483bb72020-08-04 19:48:50 -07002813 * case, we have to wait until after we have
Jan Kara646caa92016-07-04 10:14:01 -04002814 * submitted all the IO, released page locks we hold,
2815 * and dropped io_end reference (for extent conversion
2816 * to be able to complete) before stopping the handle.
2817 */
2818 if (!ext4_handle_valid(handle) || handle->h_sync == 0) {
2819 ext4_journal_stop(handle);
2820 handle = NULL;
Jan Karadddbd6a2017-04-30 18:29:10 -04002821 mpd.do_map = 0;
Jan Kara646caa92016-07-04 10:14:01 -04002822 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002823 /* Unlock pages we didn't use */
Theodore Ts'ocb530542013-07-01 08:12:40 -04002824 mpage_release_unused_pages(&mpd, give_up_on_write);
Xiaoguang Wanga297b2f2019-02-10 23:53:21 -05002825 /* Submit prepared bio */
2826 ext4_io_submit(&mpd.io_submit);
2827
Jan Kara646caa92016-07-04 10:14:01 -04002828 /*
2829 * Drop our io_end reference we got from init. We have
2830 * to be careful and use deferred io_end finishing if
2831 * we are still holding the transaction as we can
2832 * release the last reference to io_end which may end
2833 * up doing unwritten extent conversion.
2834 */
2835 if (handle) {
2836 ext4_put_io_end_defer(mpd.io_submit.io_end);
2837 ext4_journal_stop(handle);
2838 } else
2839 ext4_put_io_end(mpd.io_submit.io_end);
Jan Karadddbd6a2017-04-30 18:29:10 -04002840 mpd.io_submit.io_end = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002841
Jan Kara4e7ea812013-06-04 13:17:40 -04002842 if (ret == -ENOSPC && sbi->s_journal) {
2843 /*
2844 * Commit the transaction which would
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002845 * free blocks released in the transaction
2846 * and try again
2847 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002848 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002849 ret = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002850 continue;
2851 }
2852 /* Fatal error - ENOMEM, EIO... */
2853 if (ret)
Mingming Cao61628a32008-07-11 19:27:31 -04002854 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002855 }
Jan Karadddbd6a2017-04-30 18:29:10 -04002856unplug:
Shaohua Li1bce63d12011-10-18 10:55:51 -04002857 blk_finish_plug(&plug);
Jan Kara9c12a832013-09-16 08:24:26 -04002858 if (!ret && !cycled && wbc->nr_to_write > 0) {
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002859 cycled = 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04002860 mpd.last_page = writeback_index - 1;
2861 mpd.first_page = 0;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002862 goto retry;
2863 }
Mingming Cao61628a32008-07-11 19:27:31 -04002864
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002865 /* Update index */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002866 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2867 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002868 * Set the writeback_index so that range_cyclic
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002869 * mode will write it back later
2870 */
Jan Kara4e7ea812013-06-04 13:17:40 -04002871 mapping->writeback_index = mpd.first_page;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002872
Mingming Cao61628a32008-07-11 19:27:31 -04002873out_writepages:
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002874 trace_ext4_writepages_result(inode, wbc, ret,
2875 nr_to_write - wbc->nr_to_write);
Eric Biggersbbd55932020-02-19 10:30:46 -08002876 percpu_up_read(&sbi->s_writepages_rwsem);
Mingming Cao61628a32008-07-11 19:27:31 -04002877 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002878}
2879
Dan Williams5f0663b2017-12-21 12:25:11 -08002880static int ext4_dax_writepages(struct address_space *mapping,
2881 struct writeback_control *wbc)
2882{
2883 int ret;
2884 long nr_to_write = wbc->nr_to_write;
2885 struct inode *inode = mapping->host;
2886 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
2887
2888 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
2889 return -EIO;
2890
Eric Biggersbbd55932020-02-19 10:30:46 -08002891 percpu_down_read(&sbi->s_writepages_rwsem);
Dan Williams5f0663b2017-12-21 12:25:11 -08002892 trace_ext4_writepages(inode, wbc);
2893
Vivek Goyal3f666c52020-01-03 13:33:07 -05002894 ret = dax_writeback_mapping_range(mapping, sbi->s_daxdev, wbc);
Dan Williams5f0663b2017-12-21 12:25:11 -08002895 trace_ext4_writepages_result(inode, wbc, ret,
2896 nr_to_write - wbc->nr_to_write);
Eric Biggersbbd55932020-02-19 10:30:46 -08002897 percpu_up_read(&sbi->s_writepages_rwsem);
Dan Williams5f0663b2017-12-21 12:25:11 -08002898 return ret;
2899}
2900
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002901static int ext4_nonda_switch(struct super_block *sb)
2902{
Eric Whitney5c1ff332013-04-09 09:27:31 -04002903 s64 free_clusters, dirty_clusters;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002904 struct ext4_sb_info *sbi = EXT4_SB(sb);
2905
2906 /*
2907 * switch to non delalloc mode if we are running low
2908 * on free block. The free block accounting via percpu
Eric Dumazet179f7eb2009-01-06 14:41:04 -08002909 * counters can get slightly wrong with percpu_counter_batch getting
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002910 * accumulated on each CPU without updating global counters
2911 * Delalloc need an accurate free block accounting. So switch
2912 * to non delalloc when we are near to error range.
2913 */
Eric Whitney5c1ff332013-04-09 09:27:31 -04002914 free_clusters =
2915 percpu_counter_read_positive(&sbi->s_freeclusters_counter);
2916 dirty_clusters =
2917 percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
Theodore Ts'o00d4e732012-09-19 22:42:36 -04002918 /*
2919 * Start pushing delalloc when 1/2 of free blocks are dirty.
2920 */
Eric Whitney5c1ff332013-04-09 09:27:31 -04002921 if (dirty_clusters && (free_clusters < 2 * dirty_clusters))
Miao Xie10ee27a2013-01-10 13:47:57 +08002922 try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE);
Theodore Ts'o00d4e732012-09-19 22:42:36 -04002923
Eric Whitney5c1ff332013-04-09 09:27:31 -04002924 if (2 * free_clusters < 3 * dirty_clusters ||
2925 free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) {
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002926 /*
Eric Sandeenc8afb442009-12-23 07:58:12 -05002927 * free block count is less than 150% of dirty blocks
2928 * or free blocks is less than watermark
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002929 */
2930 return 1;
2931 }
2932 return 0;
2933}
2934
Eric Sandeen0ff89472014-10-11 19:51:17 -04002935/* We always reserve for an inode update; the superblock could be there too */
2936static int ext4_da_write_credits(struct inode *inode, loff_t pos, unsigned len)
2937{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002938 if (likely(ext4_has_feature_large_file(inode->i_sb)))
Eric Sandeen0ff89472014-10-11 19:51:17 -04002939 return 1;
2940
2941 if (pos + len <= 0x7fffffffULL)
2942 return 1;
2943
2944 /* We might need to update the superblock to set LARGE_FILE */
2945 return 2;
2946}
2947
Alex Tomas64769242008-07-11 19:27:31 -04002948static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002949 loff_t pos, unsigned len, unsigned flags,
2950 struct page **pagep, void **fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04002951{
Eric Sandeen72b8ab92010-05-16 11:00:00 -04002952 int ret, retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002953 struct page *page;
2954 pgoff_t index;
Alex Tomas64769242008-07-11 19:27:31 -04002955 struct inode *inode = mapping->host;
2956 handle_t *handle;
2957
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002958 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
2959 return -EIO;
2960
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002961 index = pos >> PAGE_SHIFT;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002962
Eric Biggersc93d8f82019-07-22 09:26:24 -07002963 if (ext4_nonda_switch(inode->i_sb) || S_ISLNK(inode->i_mode) ||
2964 ext4_verity_in_progress(inode)) {
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002965 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
2966 return ext4_write_begin(file, mapping, pos,
2967 len, flags, pagep, fsdata);
2968 }
2969 *fsdata = (void *)0;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002970 trace_ext4_da_write_begin(inode, pos, len, flags);
Tao Ma9c3569b2012-12-10 14:05:57 -05002971
2972 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2973 ret = ext4_da_write_inline_data_begin(mapping, inode,
2974 pos, len, flags,
2975 pagep, fsdata);
2976 if (ret < 0)
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002977 return ret;
2978 if (ret == 1)
2979 return 0;
Tao Ma9c3569b2012-12-10 14:05:57 -05002980 }
2981
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002982 /*
2983 * grab_cache_page_write_begin() can take a long time if the
2984 * system is thrashing due to memory pressure, or if the page
2985 * is being written back. So grab it first before we start
2986 * the transaction handle. This also allows us to allocate
2987 * the page (if needed) without using GFP_NOFS.
2988 */
2989retry_grab:
2990 page = grab_cache_page_write_begin(mapping, index, flags);
2991 if (!page)
2992 return -ENOMEM;
2993 unlock_page(page);
2994
Alex Tomas64769242008-07-11 19:27:31 -04002995 /*
2996 * With delayed allocation, we don't log the i_disksize update
2997 * if there is delayed block allocation. But we still need
2998 * to journalling the i_disksize update if writes to the end
2999 * of file which has an already mapped buffer.
3000 */
Theodore Ts'o47564bf2013-02-09 09:24:14 -05003001retry_journal:
Eric Sandeen0ff89472014-10-11 19:51:17 -04003002 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
3003 ext4_da_write_credits(inode, pos, len));
Alex Tomas64769242008-07-11 19:27:31 -04003004 if (IS_ERR(handle)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003005 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05003006 return PTR_ERR(handle);
Alex Tomas64769242008-07-11 19:27:31 -04003007 }
3008
Theodore Ts'o47564bf2013-02-09 09:24:14 -05003009 lock_page(page);
3010 if (page->mapping != mapping) {
3011 /* The page got truncated from under us */
3012 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003013 put_page(page);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04003014 ext4_journal_stop(handle);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05003015 goto retry_grab;
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04003016 }
Theodore Ts'o47564bf2013-02-09 09:24:14 -05003017 /* In case writeback began while the page was unlocked */
Dmitry Monakhov7afe5aa2013-08-28 14:30:47 -04003018 wait_for_stable_page(page);
Alex Tomas64769242008-07-11 19:27:31 -04003019
Chandan Rajendra643fa962018-12-12 15:20:12 +05303020#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrow2058f832015-04-12 00:55:10 -04003021 ret = ext4_block_write_begin(page, pos, len,
3022 ext4_da_get_block_prep);
3023#else
Christoph Hellwig6e1db882010-06-04 11:29:57 +02003024 ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
Michael Halcrow2058f832015-04-12 00:55:10 -04003025#endif
Alex Tomas64769242008-07-11 19:27:31 -04003026 if (ret < 0) {
3027 unlock_page(page);
3028 ext4_journal_stop(handle);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04003029 /*
3030 * block_write_begin may have instantiated a few blocks
3031 * outside i_size. Trim these off again. Don't need
3032 * i_size_read because we hold i_mutex.
3033 */
3034 if (pos + len > inode->i_size)
Jan Karab9a42072009-12-08 21:24:33 -05003035 ext4_truncate_failed_write(inode);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05003036
3037 if (ret == -ENOSPC &&
3038 ext4_should_retry_alloc(inode->i_sb, &retries))
3039 goto retry_journal;
3040
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003041 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05003042 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04003043 }
3044
Theodore Ts'o47564bf2013-02-09 09:24:14 -05003045 *pagep = page;
Alex Tomas64769242008-07-11 19:27:31 -04003046 return ret;
3047}
3048
Mingming Cao632eaea2008-07-11 19:27:31 -04003049/*
3050 * Check if we should update i_disksize
3051 * when write to the end of file but not require block allocation
3052 */
3053static int ext4_da_should_update_i_disksize(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003054 unsigned long offset)
Mingming Cao632eaea2008-07-11 19:27:31 -04003055{
3056 struct buffer_head *bh;
3057 struct inode *inode = page->mapping->host;
3058 unsigned int idx;
3059 int i;
3060
3061 bh = page_buffers(page);
3062 idx = offset >> inode->i_blkbits;
3063
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003064 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04003065 bh = bh->b_this_page;
3066
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04003067 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
Mingming Cao632eaea2008-07-11 19:27:31 -04003068 return 0;
3069 return 1;
3070}
3071
Alex Tomas64769242008-07-11 19:27:31 -04003072static int ext4_da_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003073 struct address_space *mapping,
3074 loff_t pos, unsigned len, unsigned copied,
3075 struct page *page, void *fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04003076{
3077 struct inode *inode = mapping->host;
3078 int ret = 0, ret2;
3079 handle_t *handle = ext4_journal_current_handle();
3080 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04003081 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003082 int write_mode = (int)(unsigned long)fsdata;
3083
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003084 if (write_mode == FALL_BACK_TO_NONDELALLOC)
3085 return ext4_write_end(file, mapping, pos,
3086 len, copied, page, fsdata);
Mingming Cao632eaea2008-07-11 19:27:31 -04003087
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003088 trace_ext4_da_write_end(inode, pos, len, copied);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003089 start = pos & (PAGE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003090 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04003091
3092 /*
3093 * generic_write_end() will run mark_inode_dirty() if i_size
3094 * changes. So let's piggyback the i_disksize mark_inode_dirty
3095 * into that.
3096 */
Alex Tomas64769242008-07-11 19:27:31 -04003097 new_i_size = pos + copied;
Andrea Arcangeliea51d132011-12-13 21:41:15 -05003098 if (copied && new_i_size > EXT4_I(inode)->i_disksize) {
Tao Ma9c3569b2012-12-10 14:05:57 -05003099 if (ext4_has_inline_data(inode) ||
3100 ext4_da_should_update_i_disksize(page, end)) {
Dmitry Monakhovee124d22014-08-30 23:34:06 -04003101 ext4_update_i_disksize(inode, new_i_size);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003102 /* We need to mark inode dirty even if
3103 * new_i_size is less that inode->i_size
3104 * bu greater than i_disksize.(hint delalloc)
3105 */
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003106 ret = ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04003107 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003108 }
Tao Ma9c3569b2012-12-10 14:05:57 -05003109
3110 if (write_mode != CONVERT_INLINE_DATA &&
3111 ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
3112 ext4_has_inline_data(inode))
3113 ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied,
3114 page);
3115 else
3116 ret2 = generic_write_end(file, mapping, pos, len, copied,
Alex Tomas64769242008-07-11 19:27:31 -04003117 page, fsdata);
Tao Ma9c3569b2012-12-10 14:05:57 -05003118
Alex Tomas64769242008-07-11 19:27:31 -04003119 copied = ret2;
3120 if (ret2 < 0)
3121 ret = ret2;
3122 ret2 = ext4_journal_stop(handle);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003123 if (unlikely(ret2 && !ret))
Alex Tomas64769242008-07-11 19:27:31 -04003124 ret = ret2;
3125
3126 return ret ? ret : copied;
3127}
3128
Theodore Ts'occd25062009-02-26 01:04:07 -05003129/*
3130 * Force all delayed allocation blocks to be allocated for a given inode.
3131 */
3132int ext4_alloc_da_blocks(struct inode *inode)
3133{
Theodore Ts'ofb40ba02009-09-16 19:30:40 -04003134 trace_ext4_alloc_da_blocks(inode);
3135
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04003136 if (!EXT4_I(inode)->i_reserved_data_blocks)
Theodore Ts'occd25062009-02-26 01:04:07 -05003137 return 0;
3138
3139 /*
3140 * We do something simple for now. The filemap_flush() will
3141 * also start triggering a write of the data blocks, which is
3142 * not strictly speaking necessary (and for users of
3143 * laptop_mode, not even desirable). However, to do otherwise
3144 * would require replicating code paths in:
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003145 *
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003146 * ext4_writepages() ->
Theodore Ts'occd25062009-02-26 01:04:07 -05003147 * write_cache_pages() ---> (via passed in callback function)
3148 * __mpage_da_writepage() -->
3149 * mpage_add_bh_to_extent()
3150 * mpage_da_map_blocks()
3151 *
3152 * The problem is that write_cache_pages(), located in
3153 * mm/page-writeback.c, marks pages clean in preparation for
3154 * doing I/O, which is not desirable if we're not planning on
3155 * doing I/O at all.
3156 *
3157 * We could call write_cache_pages(), and then redirty all of
Wu Fengguang380cf092010-11-11 19:23:29 +08003158 * the pages by calling redirty_page_for_writepage() but that
Theodore Ts'occd25062009-02-26 01:04:07 -05003159 * would be ugly in the extreme. So instead we would need to
3160 * replicate parts of the code in the above functions,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003161 * simplifying them because we wouldn't actually intend to
Theodore Ts'occd25062009-02-26 01:04:07 -05003162 * write out the pages, but rather only collect contiguous
3163 * logical block extents, call the multi-block allocator, and
3164 * then update the buffer heads with the block allocations.
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003165 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003166 * For now, though, we'll cheat by calling filemap_flush(),
3167 * which will map the blocks, and start the I/O, but not
3168 * actually wait for the I/O to complete.
3169 */
3170 return filemap_flush(inode->i_mapping);
3171}
Alex Tomas64769242008-07-11 19:27:31 -04003172
3173/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003174 * bmap() is special. It gets used by applications such as lilo and by
3175 * the swapper to find the on-disk block of a specific piece of data.
3176 *
3177 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07003178 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003179 * filesystem and enables swap, then they may get a nasty shock when the
3180 * data getting swapped to that swapfile suddenly gets overwritten by
3181 * the original zero's written out previously to the journal and
3182 * awaiting writeback in the kernel's buffer cache.
3183 *
3184 * So, if we see any bmap calls here on a modified, data-journaled file,
3185 * take extra steps to flush any blocks which might be in the cache.
3186 */
Mingming Cao617ba132006-10-11 01:20:53 -07003187static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003188{
3189 struct inode *inode = mapping->host;
3190 journal_t *journal;
3191 int err;
3192
Tao Ma46c7f252012-12-10 14:04:52 -05003193 /*
3194 * We can get here for an inline file via the FIBMAP ioctl
3195 */
3196 if (ext4_has_inline_data(inode))
3197 return 0;
3198
Alex Tomas64769242008-07-11 19:27:31 -04003199 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
3200 test_opt(inode->i_sb, DELALLOC)) {
3201 /*
3202 * With delalloc we want to sync the file
3203 * so that we can make sure we allocate
3204 * blocks for file
3205 */
3206 filemap_write_and_wait(mapping);
3207 }
3208
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003209 if (EXT4_JOURNAL(inode) &&
3210 ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003211 /*
3212 * This is a REALLY heavyweight approach, but the use of
3213 * bmap on dirty files is expected to be extremely rare:
3214 * only if we run lilo or swapon on a freshly made file
3215 * do we expect this to happen.
3216 *
3217 * (bmap requires CAP_SYS_RAWIO so this does not
3218 * represent an unprivileged user DOS attack --- we'd be
3219 * in trouble if mortal users could trigger this path at
3220 * will.)
3221 *
Mingming Cao617ba132006-10-11 01:20:53 -07003222 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003223 * regular files. If somebody wants to bmap a directory
3224 * or symlink and gets confused because the buffer
3225 * hasn't yet been flushed to disk, they deserve
3226 * everything they get.
3227 */
3228
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003229 ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
Mingming Cao617ba132006-10-11 01:20:53 -07003230 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07003231 jbd2_journal_lock_updates(journal);
Leah Rumancik01d5d962021-05-18 15:13:25 +00003232 err = jbd2_journal_flush(journal, 0);
Mingming Caodab291a2006-10-11 01:21:01 -07003233 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003234
3235 if (err)
3236 return 0;
3237 }
3238
Ritesh Harjaniac58e4f2020-02-28 14:56:56 +05303239 return iomap_bmap(mapping, block, &ext4_iomap_ops);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003240}
3241
Mingming Cao617ba132006-10-11 01:20:53 -07003242static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003243{
Tao Ma46c7f252012-12-10 14:04:52 -05003244 int ret = -EAGAIN;
3245 struct inode *inode = page->mapping->host;
3246
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003247 trace_ext4_readpage(page);
Tao Ma46c7f252012-12-10 14:04:52 -05003248
3249 if (ext4_has_inline_data(inode))
3250 ret = ext4_readpage_inline(inode, page);
3251
3252 if (ret == -EAGAIN)
Matthew Wilcox (Oracle)a07f6242020-06-01 21:47:20 -07003253 return ext4_mpage_readpages(inode, NULL, page);
Tao Ma46c7f252012-12-10 14:04:52 -05003254
3255 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003256}
3257
Matthew Wilcox (Oracle)6311f91f2020-06-01 21:47:16 -07003258static void ext4_readahead(struct readahead_control *rac)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003259{
Matthew Wilcox (Oracle)6311f91f2020-06-01 21:47:16 -07003260 struct inode *inode = rac->mapping->host;
Tao Ma46c7f252012-12-10 14:04:52 -05003261
Matthew Wilcox (Oracle)6311f91f2020-06-01 21:47:16 -07003262 /* If the file has inline data, no need to do readahead. */
Tao Ma46c7f252012-12-10 14:04:52 -05003263 if (ext4_has_inline_data(inode))
Matthew Wilcox (Oracle)6311f91f2020-06-01 21:47:16 -07003264 return;
Tao Ma46c7f252012-12-10 14:04:52 -05003265
Matthew Wilcox (Oracle)a07f6242020-06-01 21:47:20 -07003266 ext4_mpage_readpages(inode, rac, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003267}
3268
Lukas Czernerd47992f2013-05-21 23:17:23 -04003269static void ext4_invalidatepage(struct page *page, unsigned int offset,
3270 unsigned int length)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003271{
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003272 trace_ext4_invalidatepage(page, offset, length);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003273
Jan Kara4520fb32012-12-25 13:28:54 -05003274 /* No journalling happens on data buffers when this function is used */
3275 WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page)));
3276
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003277 block_invalidatepage(page, offset, length);
Jan Kara4520fb32012-12-25 13:28:54 -05003278}
3279
Jan Kara53e87262012-12-25 13:29:52 -05003280static int __ext4_journalled_invalidatepage(struct page *page,
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003281 unsigned int offset,
3282 unsigned int length)
Jan Kara4520fb32012-12-25 13:28:54 -05003283{
3284 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
3285
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003286 trace_ext4_journalled_invalidatepage(page, offset, length);
Jan Kara4520fb32012-12-25 13:28:54 -05003287
Jiaying Zhang744692d2010-03-04 16:14:02 -05003288 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003289 * If it's a full truncate we just forget about the pending dirtying
3290 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003291 if (offset == 0 && length == PAGE_SIZE)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003292 ClearPageChecked(page);
3293
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003294 return jbd2_journal_invalidatepage(journal, page, offset, length);
Jan Kara53e87262012-12-25 13:29:52 -05003295}
3296
3297/* Wrapper for aops... */
3298static void ext4_journalled_invalidatepage(struct page *page,
Lukas Czernerd47992f2013-05-21 23:17:23 -04003299 unsigned int offset,
3300 unsigned int length)
Jan Kara53e87262012-12-25 13:29:52 -05003301{
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003302 WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003303}
3304
Mingming Cao617ba132006-10-11 01:20:53 -07003305static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003306{
Mingming Cao617ba132006-10-11 01:20:53 -07003307 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003308
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003309 trace_ext4_releasepage(page);
3310
Jan Karae1c36592013-03-10 22:19:00 -04003311 /* Page has dirty journalled data -> cannot release */
3312 if (PageChecked(page))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003313 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05003314 if (journal)
zhangyi (F)529a7812020-06-20 10:54:27 +08003315 return jbd2_journal_try_to_free_buffers(journal, page);
Frank Mayhar03901312009-01-07 00:06:22 -05003316 else
3317 return try_to_free_buffers(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003318}
3319
Jan Karab8a61762017-11-01 16:36:45 +01003320static bool ext4_inode_datasync_dirty(struct inode *inode)
3321{
3322 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
3323
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07003324 if (journal) {
3325 if (jbd2_transaction_committed(journal,
Andrea Righid0520df2020-10-26 21:49:13 -07003326 EXT4_I(inode)->i_datasync_tid))
3327 return false;
3328 if (test_opt2(inode->i_sb, JOURNAL_FAST_COMMIT))
Harshad Shirwadkar1ceecb52020-11-05 19:59:06 -08003329 return !list_empty(&EXT4_I(inode)->i_fc_list);
Andrea Righid0520df2020-10-26 21:49:13 -07003330 return true;
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07003331 }
3332
Jan Karab8a61762017-11-01 16:36:45 +01003333 /* Any metadata buffers to write? */
3334 if (!list_empty(&inode->i_mapping->private_list))
3335 return true;
3336 return inode->i_state & I_DIRTY_DATASYNC;
3337}
3338
Matthew Bobrowskic8fdfe292019-11-05 22:59:56 +11003339static void ext4_set_iomap(struct inode *inode, struct iomap *iomap,
3340 struct ext4_map_blocks *map, loff_t offset,
3341 loff_t length)
Jan Kara364443c2016-11-20 17:36:06 -05003342{
Matthew Bobrowskic8fdfe292019-11-05 22:59:56 +11003343 u8 blkbits = inode->i_blkbits;
3344
3345 /*
3346 * Writes that span EOF might trigger an I/O size update on completion,
3347 * so consider them to be dirty for the purpose of O_DSYNC, even if
3348 * there is no other metadata changes being made or are pending.
3349 */
3350 iomap->flags = 0;
3351 if (ext4_inode_datasync_dirty(inode) ||
3352 offset + length > i_size_read(inode))
3353 iomap->flags |= IOMAP_F_DIRTY;
3354
3355 if (map->m_flags & EXT4_MAP_NEW)
3356 iomap->flags |= IOMAP_F_NEW;
3357
3358 iomap->bdev = inode->i_sb->s_bdev;
3359 iomap->dax_dev = EXT4_SB(inode->i_sb)->s_daxdev;
3360 iomap->offset = (u64) map->m_lblk << blkbits;
3361 iomap->length = (u64) map->m_len << blkbits;
3362
Ritesh Harjani63867222020-02-28 14:56:54 +05303363 if ((map->m_flags & EXT4_MAP_MAPPED) &&
3364 !ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3365 iomap->flags |= IOMAP_F_MERGED;
3366
Matthew Bobrowskic8fdfe292019-11-05 22:59:56 +11003367 /*
3368 * Flags passed to ext4_map_blocks() for direct I/O writes can result
3369 * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits
3370 * set. In order for any allocated unwritten extents to be converted
3371 * into written extents correctly within the ->end_io() handler, we
3372 * need to ensure that the iomap->type is set appropriately. Hence, the
3373 * reason why we need to check whether the EXT4_MAP_UNWRITTEN bit has
3374 * been set first.
3375 */
3376 if (map->m_flags & EXT4_MAP_UNWRITTEN) {
3377 iomap->type = IOMAP_UNWRITTEN;
3378 iomap->addr = (u64) map->m_pblk << blkbits;
3379 } else if (map->m_flags & EXT4_MAP_MAPPED) {
3380 iomap->type = IOMAP_MAPPED;
3381 iomap->addr = (u64) map->m_pblk << blkbits;
3382 } else {
3383 iomap->type = IOMAP_HOLE;
3384 iomap->addr = IOMAP_NULL_ADDR;
3385 }
3386}
3387
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003388static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map,
3389 unsigned int flags)
3390{
3391 handle_t *handle;
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003392 u8 blkbits = inode->i_blkbits;
3393 int ret, dio_credits, m_flags = 0, retries = 0;
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003394
3395 /*
3396 * Trim the mapping request to the maximum value that we can map at
3397 * once for direct I/O.
3398 */
3399 if (map->m_len > DIO_MAX_BLOCKS)
3400 map->m_len = DIO_MAX_BLOCKS;
3401 dio_credits = ext4_chunk_trans_blocks(inode, map->m_len);
3402
3403retry:
3404 /*
3405 * Either we allocate blocks and then don't get an unwritten extent, so
3406 * in that case we have reserved enough credits. Or, the blocks are
3407 * already allocated and unwritten. In that case, the extent conversion
3408 * fits into the credits as well.
3409 */
3410 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits);
3411 if (IS_ERR(handle))
3412 return PTR_ERR(handle);
3413
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003414 /*
3415 * DAX and direct I/O are the only two operations that are currently
3416 * supported with IOMAP_WRITE.
3417 */
3418 WARN_ON(!IS_DAX(inode) && !(flags & IOMAP_DIRECT));
3419 if (IS_DAX(inode))
3420 m_flags = EXT4_GET_BLOCKS_CREATE_ZERO;
3421 /*
3422 * We use i_size instead of i_disksize here because delalloc writeback
3423 * can complete at any point during the I/O and subsequently push the
3424 * i_disksize out to i_size. This could be beyond where direct I/O is
3425 * happening and thus expose allocated blocks to direct I/O reads.
3426 */
Jan Karad0b040f2021-04-12 12:23:33 +02003427 else if (((loff_t)map->m_lblk << blkbits) >= i_size_read(inode))
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003428 m_flags = EXT4_GET_BLOCKS_CREATE;
3429 else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3430 m_flags = EXT4_GET_BLOCKS_IO_CREATE_EXT;
3431
3432 ret = ext4_map_blocks(handle, inode, map, m_flags);
3433
3434 /*
3435 * We cannot fill holes in indirect tree based inodes as that could
3436 * expose stale data in the case of a crash. Use the magic error code
3437 * to fallback to buffered I/O.
3438 */
3439 if (!m_flags && !ret)
3440 ret = -ENOTBLK;
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003441
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003442 ext4_journal_stop(handle);
3443 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3444 goto retry;
3445
3446 return ret;
3447}
3448
3449
Jan Kara364443c2016-11-20 17:36:06 -05003450static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -07003451 unsigned flags, struct iomap *iomap, struct iomap *srcmap)
Jan Kara364443c2016-11-20 17:36:06 -05003452{
Jan Kara364443c2016-11-20 17:36:06 -05003453 int ret;
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003454 struct ext4_map_blocks map;
3455 u8 blkbits = inode->i_blkbits;
Jan Kara364443c2016-11-20 17:36:06 -05003456
Theodore Ts'obcd8e912018-09-01 12:45:04 -04003457 if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
3458 return -EINVAL;
Andreas Gruenbacher7046ae32017-10-01 17:57:54 -04003459
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003460 if (WARN_ON_ONCE(ext4_has_inline_data(inode)))
3461 return -ERANGE;
Jan Kara364443c2016-11-20 17:36:06 -05003462
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003463 /*
3464 * Calculate the first and last logical blocks respectively.
3465 */
3466 map.m_lblk = offset >> blkbits;
3467 map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits,
3468 EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1;
Jan Kara364443c2016-11-20 17:36:06 -05003469
Ritesh Harjani9faac622020-09-18 10:36:35 +05303470 if (flags & IOMAP_WRITE) {
3471 /*
3472 * We check here if the blocks are already allocated, then we
3473 * don't need to start a journal txn and we can directly return
3474 * the mapping information. This could boost performance
3475 * especially in multi-threaded overwrite requests.
3476 */
3477 if (offset + length <= i_size_read(inode)) {
3478 ret = ext4_map_blocks(NULL, inode, &map, 0);
3479 if (ret > 0 && (map.m_flags & EXT4_MAP_MAPPED))
3480 goto out;
3481 }
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003482 ret = ext4_iomap_alloc(inode, &map, flags);
Ritesh Harjani9faac622020-09-18 10:36:35 +05303483 } else {
Jan Kara776722e2016-11-20 18:09:11 -05003484 ret = ext4_map_blocks(NULL, inode, &map, 0);
Ritesh Harjani9faac622020-09-18 10:36:35 +05303485 }
Christoph Hellwig545052e2017-10-01 17:58:54 -04003486
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003487 if (ret < 0)
3488 return ret;
Ritesh Harjani9faac622020-09-18 10:36:35 +05303489out:
Matthew Bobrowskic8fdfe292019-11-05 22:59:56 +11003490 ext4_set_iomap(inode, iomap, &map, offset, length);
Christoph Hellwig545052e2017-10-01 17:58:54 -04003491
Jan Kara364443c2016-11-20 17:36:06 -05003492 return 0;
3493}
3494
Jan Kara8cd115b2019-12-18 18:44:33 +01003495static int ext4_iomap_overwrite_begin(struct inode *inode, loff_t offset,
3496 loff_t length, unsigned flags, struct iomap *iomap,
3497 struct iomap *srcmap)
3498{
3499 int ret;
3500
3501 /*
3502 * Even for writes we don't need to allocate blocks, so just pretend
3503 * we are reading to save overhead of starting a transaction.
3504 */
3505 flags &= ~IOMAP_WRITE;
3506 ret = ext4_iomap_begin(inode, offset, length, flags, iomap, srcmap);
3507 WARN_ON_ONCE(iomap->type != IOMAP_MAPPED);
3508 return ret;
3509}
3510
Jan Kara776722e2016-11-20 18:09:11 -05003511static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length,
3512 ssize_t written, unsigned flags, struct iomap *iomap)
3513{
Jan Kara776722e2016-11-20 18:09:11 -05003514 /*
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003515 * Check to see whether an error occurred while writing out the data to
3516 * the allocated blocks. If so, return the magic error code so that we
3517 * fallback to buffered I/O and attempt to complete the remainder of
3518 * the I/O. Any blocks that may have been allocated in preparation for
3519 * the direct I/O will be reused during buffered I/O.
Jan Kara776722e2016-11-20 18:09:11 -05003520 */
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003521 if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0)
3522 return -ENOTBLK;
Jan Kara776722e2016-11-20 18:09:11 -05003523
Matthew Bobrowski569342d2019-11-05 23:01:51 +11003524 return 0;
Jan Kara776722e2016-11-20 18:09:11 -05003525}
3526
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08003527const struct iomap_ops ext4_iomap_ops = {
Jan Kara364443c2016-11-20 17:36:06 -05003528 .iomap_begin = ext4_iomap_begin,
Jan Kara776722e2016-11-20 18:09:11 -05003529 .iomap_end = ext4_iomap_end,
Jan Kara364443c2016-11-20 17:36:06 -05003530};
3531
Jan Kara8cd115b2019-12-18 18:44:33 +01003532const struct iomap_ops ext4_iomap_overwrite_ops = {
3533 .iomap_begin = ext4_iomap_overwrite_begin,
3534 .iomap_end = ext4_iomap_end,
3535};
3536
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003537static bool ext4_iomap_is_delalloc(struct inode *inode,
3538 struct ext4_map_blocks *map)
Mingming Cao4c0425f2009-09-28 15:48:41 -04003539{
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003540 struct extent_status es;
3541 ext4_lblk_t offset = 0, end = map->m_lblk + map->m_len - 1;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003542
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003543 ext4_es_find_extent_range(inode, &ext4_es_is_delayed,
3544 map->m_lblk, end, &es);
Mingming4b70df12009-11-03 14:44:54 -05003545
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003546 if (!es.es_len || es.es_lblk > end)
3547 return false;
3548
3549 if (es.es_lblk > map->m_lblk) {
3550 map->m_len = es.es_lblk - map->m_lblk;
3551 return false;
3552 }
3553
3554 offset = map->m_lblk - es.es_lblk;
3555 map->m_len = es.es_len - offset;
3556
3557 return true;
3558}
3559
3560static int ext4_iomap_begin_report(struct inode *inode, loff_t offset,
3561 loff_t length, unsigned int flags,
3562 struct iomap *iomap, struct iomap *srcmap)
3563{
3564 int ret;
3565 bool delalloc = false;
3566 struct ext4_map_blocks map;
3567 u8 blkbits = inode->i_blkbits;
3568
3569 if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
3570 return -EINVAL;
3571
3572 if (ext4_has_inline_data(inode)) {
3573 ret = ext4_inline_data_iomap(inode, iomap);
3574 if (ret != -EAGAIN) {
3575 if (ret == 0 && offset >= iomap->length)
3576 ret = -ENOENT;
3577 return ret;
3578 }
3579 }
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003580
Jan Kara74c66bc2016-02-29 08:36:38 +11003581 /*
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003582 * Calculate the first and last logical block respectively.
Jan Kara74c66bc2016-02-29 08:36:38 +11003583 */
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003584 map.m_lblk = offset >> blkbits;
3585 map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits,
3586 EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1;
3587
Ritesh Harjanib2c57642020-02-28 14:56:57 +05303588 /*
3589 * Fiemap callers may call for offset beyond s_bitmap_maxbytes.
3590 * So handle it here itself instead of querying ext4_map_blocks().
3591 * Since ext4_map_blocks() will warn about it and will return
3592 * -EIO error.
3593 */
3594 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
3595 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3596
3597 if (offset >= sbi->s_bitmap_maxbytes) {
3598 map.m_flags = 0;
3599 goto set_iomap;
3600 }
3601 }
3602
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003603 ret = ext4_map_blocks(NULL, inode, &map, 0);
3604 if (ret < 0)
3605 return ret;
3606 if (ret == 0)
3607 delalloc = ext4_iomap_is_delalloc(inode, &map);
3608
Ritesh Harjanib2c57642020-02-28 14:56:57 +05303609set_iomap:
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003610 ext4_set_iomap(inode, iomap, &map, offset, length);
3611 if (delalloc && iomap->type == IOMAP_HOLE)
3612 iomap->type = IOMAP_DELALLOC;
Christoph Hellwig187372a2016-02-08 14:40:51 +11003613
3614 return 0;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003615}
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003616
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003617const struct iomap_ops ext4_iomap_report_ops = {
3618 .iomap_begin = ext4_iomap_begin_report,
3619};
Mingming Cao4c0425f2009-09-28 15:48:41 -04003620
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003621/*
Mingming Cao617ba132006-10-11 01:20:53 -07003622 * Pages can be marked dirty completely asynchronously from ext4's journalling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003623 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3624 * much here because ->set_page_dirty is called under VFS locks. The page is
3625 * not necessarily locked.
3626 *
3627 * We cannot just dirty the page and leave attached buffers clean, because the
3628 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3629 * or jbddirty because all the journalling code will explode.
3630 *
3631 * So what we do is to mark the page "pending dirty" and next time writepage
3632 * is called, propagate that into the buffers appropriately.
3633 */
Mingming Cao617ba132006-10-11 01:20:53 -07003634static int ext4_journalled_set_page_dirty(struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003635{
3636 SetPageChecked(page);
3637 return __set_page_dirty_nobuffers(page);
3638}
3639
Jan Kara6dcc6932016-12-01 11:46:40 -05003640static int ext4_set_page_dirty(struct page *page)
3641{
3642 WARN_ON_ONCE(!PageLocked(page) && !PageDirty(page));
3643 WARN_ON_ONCE(!page_has_buffers(page));
3644 return __set_page_dirty_buffers(page);
3645}
3646
Ritesh Harjani0e6895b2020-09-04 14:46:53 +05303647static int ext4_iomap_swap_activate(struct swap_info_struct *sis,
3648 struct file *file, sector_t *span)
3649{
3650 return iomap_swapfile_activate(sis, file, span,
3651 &ext4_iomap_report_ops);
3652}
3653
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003654static const struct address_space_operations ext4_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003655 .readpage = ext4_readpage,
Matthew Wilcox (Oracle)6311f91f2020-06-01 21:47:16 -07003656 .readahead = ext4_readahead,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003657 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003658 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003659 .write_begin = ext4_write_begin,
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003660 .write_end = ext4_write_end,
Jan Kara6dcc6932016-12-01 11:46:40 -05003661 .set_page_dirty = ext4_set_page_dirty,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003662 .bmap = ext4_bmap,
3663 .invalidatepage = ext4_invalidatepage,
3664 .releasepage = ext4_releasepage,
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003665 .direct_IO = noop_direct_IO,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003666 .migratepage = buffer_migrate_page,
3667 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003668 .error_remove_page = generic_error_remove_page,
Ritesh Harjani0e6895b2020-09-04 14:46:53 +05303669 .swap_activate = ext4_iomap_swap_activate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003670};
3671
Mingming Cao617ba132006-10-11 01:20:53 -07003672static const struct address_space_operations ext4_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003673 .readpage = ext4_readpage,
Matthew Wilcox (Oracle)6311f91f2020-06-01 21:47:16 -07003674 .readahead = ext4_readahead,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003675 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003676 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003677 .write_begin = ext4_write_begin,
3678 .write_end = ext4_journalled_write_end,
3679 .set_page_dirty = ext4_journalled_set_page_dirty,
3680 .bmap = ext4_bmap,
Jan Kara4520fb32012-12-25 13:28:54 -05003681 .invalidatepage = ext4_journalled_invalidatepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003682 .releasepage = ext4_releasepage,
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003683 .direct_IO = noop_direct_IO,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003684 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003685 .error_remove_page = generic_error_remove_page,
Ritesh Harjani0e6895b2020-09-04 14:46:53 +05303686 .swap_activate = ext4_iomap_swap_activate,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003687};
3688
Alex Tomas64769242008-07-11 19:27:31 -04003689static const struct address_space_operations ext4_da_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003690 .readpage = ext4_readpage,
Matthew Wilcox (Oracle)6311f91f2020-06-01 21:47:16 -07003691 .readahead = ext4_readahead,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003692 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003693 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003694 .write_begin = ext4_da_write_begin,
3695 .write_end = ext4_da_write_end,
Jan Kara6dcc6932016-12-01 11:46:40 -05003696 .set_page_dirty = ext4_set_page_dirty,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003697 .bmap = ext4_bmap,
Eric Whitney8fcc3a52019-08-22 23:22:14 -04003698 .invalidatepage = ext4_invalidatepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003699 .releasepage = ext4_releasepage,
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003700 .direct_IO = noop_direct_IO,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003701 .migratepage = buffer_migrate_page,
3702 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003703 .error_remove_page = generic_error_remove_page,
Ritesh Harjani0e6895b2020-09-04 14:46:53 +05303704 .swap_activate = ext4_iomap_swap_activate,
Alex Tomas64769242008-07-11 19:27:31 -04003705};
3706
Dan Williams5f0663b2017-12-21 12:25:11 -08003707static const struct address_space_operations ext4_dax_aops = {
3708 .writepages = ext4_dax_writepages,
3709 .direct_IO = noop_direct_IO,
Matthew Wilcox (Oracle)b82a96c2021-06-28 19:36:27 -07003710 .set_page_dirty = __set_page_dirty_no_writeback,
Toshi Kani94dbb632018-09-15 21:23:41 -04003711 .bmap = ext4_bmap,
Dan Williams5f0663b2017-12-21 12:25:11 -08003712 .invalidatepage = noop_invalidatepage,
Ritesh Harjani0e6895b2020-09-04 14:46:53 +05303713 .swap_activate = ext4_iomap_swap_activate,
Dan Williams5f0663b2017-12-21 12:25:11 -08003714};
3715
Mingming Cao617ba132006-10-11 01:20:53 -07003716void ext4_set_aops(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003717{
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003718 switch (ext4_inode_journal_mode(inode)) {
3719 case EXT4_INODE_ORDERED_DATA_MODE:
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003720 case EXT4_INODE_WRITEBACK_DATA_MODE:
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003721 break;
3722 case EXT4_INODE_JOURNAL_DATA_MODE:
Mingming Cao617ba132006-10-11 01:20:53 -07003723 inode->i_mapping->a_ops = &ext4_journalled_aops;
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003724 return;
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003725 default:
3726 BUG();
3727 }
Dan Williams5f0663b2017-12-21 12:25:11 -08003728 if (IS_DAX(inode))
3729 inode->i_mapping->a_ops = &ext4_dax_aops;
3730 else if (test_opt(inode->i_sb, DELALLOC))
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003731 inode->i_mapping->a_ops = &ext4_da_aops;
3732 else
3733 inode->i_mapping->a_ops = &ext4_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003734}
3735
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003736static int __ext4_block_zero_page_range(handle_t *handle,
Lukas Czernerd863dc32013-05-27 23:32:35 -04003737 struct address_space *mapping, loff_t from, loff_t length)
3738{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003739 ext4_fsblk_t index = from >> PAGE_SHIFT;
3740 unsigned offset = from & (PAGE_SIZE-1);
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003741 unsigned blocksize, pos;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003742 ext4_lblk_t iblock;
3743 struct inode *inode = mapping->host;
3744 struct buffer_head *bh;
3745 struct page *page;
3746 int err = 0;
3747
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003748 page = find_or_create_page(mapping, from >> PAGE_SHIFT,
Michal Hockoc62d2552015-11-06 16:28:49 -08003749 mapping_gfp_constraint(mapping, ~__GFP_FS));
Lukas Czernerd863dc32013-05-27 23:32:35 -04003750 if (!page)
3751 return -ENOMEM;
3752
3753 blocksize = inode->i_sb->s_blocksize;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003754
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003755 iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003756
3757 if (!page_has_buffers(page))
3758 create_empty_buffers(page, blocksize, 0);
3759
3760 /* Find the buffer that contains "offset" */
3761 bh = page_buffers(page);
3762 pos = blocksize;
3763 while (offset >= pos) {
3764 bh = bh->b_this_page;
3765 iblock++;
3766 pos += blocksize;
3767 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003768 if (buffer_freed(bh)) {
3769 BUFFER_TRACE(bh, "freed: skip");
3770 goto unlock;
3771 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003772 if (!buffer_mapped(bh)) {
3773 BUFFER_TRACE(bh, "unmapped");
3774 ext4_get_block(inode, iblock, bh, 0);
3775 /* unmapped? It's a hole - nothing to do */
3776 if (!buffer_mapped(bh)) {
3777 BUFFER_TRACE(bh, "still unmapped");
3778 goto unlock;
3779 }
3780 }
3781
3782 /* Ok, it's mapped. Make sure it's up-to-date */
3783 if (PageUptodate(page))
3784 set_buffer_uptodate(bh);
3785
3786 if (!buffer_uptodate(bh)) {
zhangyi (F)2d069c02020-09-24 15:33:33 +08003787 err = ext4_read_bh_lock(bh, 0, true);
3788 if (err)
Lukas Czernerd863dc32013-05-27 23:32:35 -04003789 goto unlock;
Eric Biggers4f74d152020-07-02 01:56:07 +00003790 if (fscrypt_inode_uses_fs_layer_crypto(inode)) {
Michael Halcrowc9c74292015-04-12 00:56:10 -04003791 /* We expect the key to be set. */
Jaegeuk Kima7550b32016-07-10 14:01:03 -04003792 BUG_ON(!fscrypt_has_encryption_key(inode));
Eric Biggers834f1562019-12-26 09:41:05 -06003793 err = fscrypt_decrypt_pagecache_blocks(page, blocksize,
3794 bh_offset(bh));
3795 if (err) {
3796 clear_buffer_uptodate(bh);
3797 goto unlock;
3798 }
Michael Halcrowc9c74292015-04-12 00:56:10 -04003799 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003800 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003801 if (ext4_should_journal_data(inode)) {
3802 BUFFER_TRACE(bh, "get write access");
Jan Kara188c2992021-08-16 11:57:04 +02003803 err = ext4_journal_get_write_access(handle, inode->i_sb, bh,
3804 EXT4_JTR_NONE);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003805 if (err)
3806 goto unlock;
3807 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003808 zero_user(page, offset, length);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003809 BUFFER_TRACE(bh, "zeroed end of block");
3810
Lukas Czernerd863dc32013-05-27 23:32:35 -04003811 if (ext4_should_journal_data(inode)) {
3812 err = ext4_handle_dirty_metadata(handle, inode, bh);
Lukas Czerner0713ed02013-05-27 23:32:35 -04003813 } else {
jon ernst353eefd2013-07-01 08:12:39 -04003814 err = 0;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003815 mark_buffer_dirty(bh);
Jan Kara3957ef52016-04-24 00:56:05 -04003816 if (ext4_should_order_data(inode))
Ross Zwisler73131fb2019-06-20 17:26:26 -04003817 err = ext4_jbd2_inode_add_write(handle, inode, from,
3818 length);
Lukas Czerner0713ed02013-05-27 23:32:35 -04003819 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003820
3821unlock:
3822 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003823 put_page(page);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003824 return err;
3825}
3826
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003827/*
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003828 * ext4_block_zero_page_range() zeros out a mapping of length 'length'
3829 * starting from file offset 'from'. The range to be zero'd must
3830 * be contained with in one block. If the specified range exceeds
3831 * the end of the block it will be shortened to end of the block
Bhaskar Chowdhury3088e5a2021-03-27 16:00:05 +05303832 * that corresponds to 'from'
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003833 */
3834static int ext4_block_zero_page_range(handle_t *handle,
3835 struct address_space *mapping, loff_t from, loff_t length)
3836{
3837 struct inode *inode = mapping->host;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003838 unsigned offset = from & (PAGE_SIZE-1);
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003839 unsigned blocksize = inode->i_sb->s_blocksize;
3840 unsigned max = blocksize - (offset & (blocksize - 1));
3841
3842 /*
3843 * correct length if it does not fall between
3844 * 'from' and the end of the block
3845 */
3846 if (length > max || length < 0)
3847 length = max;
3848
Jan Kara47e69352016-11-20 18:08:05 -05003849 if (IS_DAX(inode)) {
3850 return iomap_zero_range(inode, from, length, NULL,
3851 &ext4_iomap_ops);
3852 }
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003853 return __ext4_block_zero_page_range(handle, mapping, from, length);
3854}
3855
3856/*
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003857 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
3858 * up to the end of the block which corresponds to `from'.
3859 * This required during truncate. We need to physically zero the tail end
3860 * of that block so it doesn't yield old data if the file is later grown.
3861 */
Stephen Hemmingerc1978552014-05-12 10:50:23 -04003862static int ext4_block_truncate_page(handle_t *handle,
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003863 struct address_space *mapping, loff_t from)
3864{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003865 unsigned offset = from & (PAGE_SIZE-1);
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003866 unsigned length;
3867 unsigned blocksize;
3868 struct inode *inode = mapping->host;
3869
Theodore Ts'o0d068632017-02-14 11:31:15 -05003870 /* If we are processing an encrypted inode during orphan list handling */
Chandan Rajendra592ddec2018-12-12 15:20:10 +05303871 if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode))
Theodore Ts'o0d068632017-02-14 11:31:15 -05003872 return 0;
3873
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003874 blocksize = inode->i_sb->s_blocksize;
3875 length = blocksize - (offset & (blocksize - 1));
3876
3877 return ext4_block_zero_page_range(handle, mapping, from, length);
3878}
3879
Lukas Czernera87dd182013-05-27 23:32:35 -04003880int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
3881 loff_t lstart, loff_t length)
3882{
3883 struct super_block *sb = inode->i_sb;
3884 struct address_space *mapping = inode->i_mapping;
Lukas Czernere1be3a92013-07-01 08:12:39 -04003885 unsigned partial_start, partial_end;
Lukas Czernera87dd182013-05-27 23:32:35 -04003886 ext4_fsblk_t start, end;
3887 loff_t byte_end = (lstart + length - 1);
3888 int err = 0;
3889
Lukas Czernere1be3a92013-07-01 08:12:39 -04003890 partial_start = lstart & (sb->s_blocksize - 1);
3891 partial_end = byte_end & (sb->s_blocksize - 1);
3892
Lukas Czernera87dd182013-05-27 23:32:35 -04003893 start = lstart >> sb->s_blocksize_bits;
3894 end = byte_end >> sb->s_blocksize_bits;
3895
3896 /* Handle partial zero within the single block */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003897 if (start == end &&
3898 (partial_start || (partial_end != sb->s_blocksize - 1))) {
Lukas Czernera87dd182013-05-27 23:32:35 -04003899 err = ext4_block_zero_page_range(handle, mapping,
3900 lstart, length);
3901 return err;
3902 }
3903 /* Handle partial zero out on the start of the range */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003904 if (partial_start) {
Lukas Czernera87dd182013-05-27 23:32:35 -04003905 err = ext4_block_zero_page_range(handle, mapping,
3906 lstart, sb->s_blocksize);
3907 if (err)
3908 return err;
3909 }
3910 /* Handle partial zero out on the end of the range */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003911 if (partial_end != sb->s_blocksize - 1)
Lukas Czernera87dd182013-05-27 23:32:35 -04003912 err = ext4_block_zero_page_range(handle, mapping,
Lukas Czernere1be3a92013-07-01 08:12:39 -04003913 byte_end - partial_end,
3914 partial_end + 1);
Lukas Czernera87dd182013-05-27 23:32:35 -04003915 return err;
3916}
3917
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003918int ext4_can_truncate(struct inode *inode)
3919{
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003920 if (S_ISREG(inode->i_mode))
3921 return 1;
3922 if (S_ISDIR(inode->i_mode))
3923 return 1;
3924 if (S_ISLNK(inode->i_mode))
3925 return !ext4_inode_is_fast_symlink(inode);
3926 return 0;
3927}
3928
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003929/*
Jan Kara01127842015-12-07 14:34:49 -05003930 * We have to make sure i_disksize gets properly updated before we truncate
3931 * page cache due to hole punching or zero range. Otherwise i_disksize update
3932 * can get lost as it may have been postponed to submission of writeback but
3933 * that will never happen after we truncate page cache.
3934 */
3935int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset,
3936 loff_t len)
3937{
3938 handle_t *handle;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003939 int ret;
3940
Jan Kara01127842015-12-07 14:34:49 -05003941 loff_t size = i_size_read(inode);
3942
Al Viro59551022016-01-22 15:40:57 -05003943 WARN_ON(!inode_is_locked(inode));
Jan Kara01127842015-12-07 14:34:49 -05003944 if (offset > size || offset + len < size)
3945 return 0;
3946
3947 if (EXT4_I(inode)->i_disksize >= size)
3948 return 0;
3949
3950 handle = ext4_journal_start(inode, EXT4_HT_MISC, 1);
3951 if (IS_ERR(handle))
3952 return PTR_ERR(handle);
3953 ext4_update_i_disksize(inode, size);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003954 ret = ext4_mark_inode_dirty(handle, inode);
Jan Kara01127842015-12-07 14:34:49 -05003955 ext4_journal_stop(handle);
3956
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003957 return ret;
Jan Kara01127842015-12-07 14:34:49 -05003958}
3959
Ross Zwislerb1f38212018-09-11 13:31:16 -04003960static void ext4_wait_dax_page(struct ext4_inode_info *ei)
Ross Zwisler430657b2018-07-29 17:00:22 -04003961{
Ross Zwisler430657b2018-07-29 17:00:22 -04003962 up_write(&ei->i_mmap_sem);
3963 schedule();
3964 down_write(&ei->i_mmap_sem);
3965}
3966
3967int ext4_break_layouts(struct inode *inode)
3968{
3969 struct ext4_inode_info *ei = EXT4_I(inode);
3970 struct page *page;
Ross Zwisler430657b2018-07-29 17:00:22 -04003971 int error;
3972
3973 if (WARN_ON_ONCE(!rwsem_is_locked(&ei->i_mmap_sem)))
3974 return -EINVAL;
3975
3976 do {
Ross Zwisler430657b2018-07-29 17:00:22 -04003977 page = dax_layout_busy_page(inode->i_mapping);
3978 if (!page)
3979 return 0;
3980
3981 error = ___wait_var_event(&page->_refcount,
3982 atomic_read(&page->_refcount) == 1,
3983 TASK_INTERRUPTIBLE, 0, 0,
Ross Zwislerb1f38212018-09-11 13:31:16 -04003984 ext4_wait_dax_page(ei));
3985 } while (error == 0);
Ross Zwisler430657b2018-07-29 17:00:22 -04003986
3987 return error;
3988}
3989
Jan Kara01127842015-12-07 14:34:49 -05003990/*
Ross Zwislercca32b72016-09-22 11:49:38 -04003991 * ext4_punch_hole: punches a hole in a file by releasing the blocks
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003992 * associated with the given offset and length
3993 *
3994 * @inode: File inode
3995 * @offset: The offset where the hole will begin
3996 * @len: The length of the hole
3997 *
Anatol Pomozov4907cb72012-09-01 10:31:09 -07003998 * Returns: 0 on success or negative on failure
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003999 */
4000
Ashish Sangwanaeb28172013-07-01 08:12:38 -04004001int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length)
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004002{
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004003 struct super_block *sb = inode->i_sb;
4004 ext4_lblk_t first_block, stop_block;
4005 struct address_space *mapping = inode->i_mapping;
Lukas Czernera87dd182013-05-27 23:32:35 -04004006 loff_t first_block_offset, last_block_offset;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004007 handle_t *handle;
4008 unsigned int credits;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004009 int ret = 0, ret2 = 0;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004010
Lukas Czernerb8a86842014-03-18 18:05:35 -04004011 trace_ext4_punch_hole(inode, offset, length, 0);
Zheng Liuaaddea82013-01-16 20:21:26 -05004012
Theodore Ts'oc1e82202019-08-23 22:38:00 -04004013 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
4014 if (ext4_has_inline_data(inode)) {
4015 down_write(&EXT4_I(inode)->i_mmap_sem);
4016 ret = ext4_convert_inline_data(inode);
4017 up_write(&EXT4_I(inode)->i_mmap_sem);
4018 if (ret)
4019 return ret;
4020 }
4021
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004022 /*
4023 * Write out all dirty pages to avoid race conditions
4024 * Then release them.
4025 */
Ross Zwislercca32b72016-09-22 11:49:38 -04004026 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004027 ret = filemap_write_and_wait_range(mapping, offset,
4028 offset + length - 1);
4029 if (ret)
4030 return ret;
4031 }
4032
Al Viro59551022016-01-22 15:40:57 -05004033 inode_lock(inode);
Lukas Czerner9ef06ce2014-04-12 09:47:00 -04004034
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004035 /* No need to punch hole beyond i_size */
4036 if (offset >= inode->i_size)
4037 goto out_mutex;
4038
4039 /*
4040 * If the hole extends beyond i_size, set the hole
4041 * to end after the page that contains i_size
4042 */
4043 if (offset + length > inode->i_size) {
4044 length = inode->i_size +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004045 PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) -
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004046 offset;
4047 }
4048
Jan Karaa3612932013-08-16 21:19:41 -04004049 if (offset & (sb->s_blocksize - 1) ||
4050 (offset + length) & (sb->s_blocksize - 1)) {
4051 /*
4052 * Attach jinode to inode for jbd2 if we do any zeroing of
4053 * partial block
4054 */
4055 ret = ext4_inode_attach_jinode(inode);
4056 if (ret < 0)
4057 goto out_mutex;
4058
4059 }
4060
Jan Karaea3d7202015-12-07 14:28:03 -05004061 /* Wait all existing dio workers, newcomers will block on i_mutex */
Jan Karaea3d7202015-12-07 14:28:03 -05004062 inode_dio_wait(inode);
4063
4064 /*
4065 * Prevent page faults from reinstantiating pages we have released from
4066 * page cache.
4067 */
4068 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04004069
4070 ret = ext4_break_layouts(inode);
4071 if (ret)
4072 goto out_dio;
4073
Lukas Czernera87dd182013-05-27 23:32:35 -04004074 first_block_offset = round_up(offset, sb->s_blocksize);
4075 last_block_offset = round_down((offset + length), sb->s_blocksize) - 1;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004076
Lukas Czernera87dd182013-05-27 23:32:35 -04004077 /* Now release the pages and zero block aligned part of pages*/
Jan Kara01127842015-12-07 14:34:49 -05004078 if (last_block_offset > first_block_offset) {
4079 ret = ext4_update_disksize_before_punch(inode, offset, length);
4080 if (ret)
4081 goto out_dio;
Lukas Czernera87dd182013-05-27 23:32:35 -04004082 truncate_pagecache_range(inode, first_block_offset,
4083 last_block_offset);
Jan Kara01127842015-12-07 14:34:49 -05004084 }
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004085
4086 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4087 credits = ext4_writepage_trans_blocks(inode);
4088 else
4089 credits = ext4_blocks_for_truncate(inode);
4090 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
4091 if (IS_ERR(handle)) {
4092 ret = PTR_ERR(handle);
4093 ext4_std_error(sb, ret);
4094 goto out_dio;
4095 }
4096
Lukas Czernera87dd182013-05-27 23:32:35 -04004097 ret = ext4_zero_partial_blocks(handle, inode, offset,
4098 length);
4099 if (ret)
4100 goto out_stop;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004101
4102 first_block = (offset + sb->s_blocksize - 1) >>
4103 EXT4_BLOCK_SIZE_BITS(sb);
4104 stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
4105
Lukas Czernereee597a2018-05-13 19:28:35 -04004106 /* If there are blocks to remove, do it */
4107 if (stop_block > first_block) {
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004108
Lukas Czernereee597a2018-05-13 19:28:35 -04004109 down_write(&EXT4_I(inode)->i_data_sem);
brookxu27bc4462020-08-17 15:36:15 +08004110 ext4_discard_preallocations(inode, 0);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004111
Lukas Czernereee597a2018-05-13 19:28:35 -04004112 ret = ext4_es_remove_extent(inode, first_block,
4113 stop_block - first_block);
4114 if (ret) {
4115 up_write(&EXT4_I(inode)->i_data_sem);
4116 goto out_stop;
4117 }
4118
4119 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4120 ret = ext4_ext_remove_space(inode, first_block,
4121 stop_block - 1);
4122 else
4123 ret = ext4_ind_remove_space(handle, inode, first_block,
4124 stop_block);
4125
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004126 up_write(&EXT4_I(inode)->i_data_sem);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004127 }
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08004128 ext4_fc_track_range(handle, inode, first_block, stop_block);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004129 if (IS_SYNC(inode))
4130 ext4_handle_sync(handle);
Maxim Patlasove251f9b2014-02-20 16:58:05 -05004131
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004132 inode->i_mtime = inode->i_ctime = current_time(inode);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004133 ret2 = ext4_mark_inode_dirty(handle, inode);
4134 if (unlikely(ret2))
4135 ret = ret2;
Jan Kara67a7d5f2017-05-29 13:24:55 -04004136 if (ret >= 0)
4137 ext4_update_inode_fsync_trans(handle, inode, 1);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004138out_stop:
4139 ext4_journal_stop(handle);
4140out_dio:
Jan Karaea3d7202015-12-07 14:28:03 -05004141 up_write(&EXT4_I(inode)->i_mmap_sem);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004142out_mutex:
Al Viro59551022016-01-22 15:40:57 -05004143 inode_unlock(inode);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004144 return ret;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004145}
4146
Jan Karaa3612932013-08-16 21:19:41 -04004147int ext4_inode_attach_jinode(struct inode *inode)
4148{
4149 struct ext4_inode_info *ei = EXT4_I(inode);
4150 struct jbd2_inode *jinode;
4151
4152 if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal)
4153 return 0;
4154
4155 jinode = jbd2_alloc_inode(GFP_KERNEL);
4156 spin_lock(&inode->i_lock);
4157 if (!ei->jinode) {
4158 if (!jinode) {
4159 spin_unlock(&inode->i_lock);
4160 return -ENOMEM;
4161 }
4162 ei->jinode = jinode;
4163 jbd2_journal_init_jbd_inode(ei->jinode, inode);
4164 jinode = NULL;
4165 }
4166 spin_unlock(&inode->i_lock);
4167 if (unlikely(jinode != NULL))
4168 jbd2_free_inode(jinode);
4169 return 0;
4170}
4171
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004172/*
Mingming Cao617ba132006-10-11 01:20:53 -07004173 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004174 *
Mingming Cao617ba132006-10-11 01:20:53 -07004175 * We block out ext4_get_block() block instantiations across the entire
4176 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004177 * simultaneously on behalf of the same inode.
4178 *
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08004179 * As we work through the truncate and commit bits of it to the journal there
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004180 * is one core, guiding principle: the file's tree must always be consistent on
4181 * disk. We must be able to restart the truncate after a crash.
4182 *
4183 * The file's tree may be transiently inconsistent in memory (although it
4184 * probably isn't), but whenever we close off and commit a journal transaction,
4185 * the contents of (the filesystem + the journal) must be consistent and
4186 * restartable. It's pretty simple, really: bottom up, right to left (although
4187 * left-to-right works OK too).
4188 *
4189 * Note that at recovery time, journal replay occurs *before* the restart of
4190 * truncate against the orphan inode list.
4191 *
4192 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07004193 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004194 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07004195 * ext4_truncate() to have another go. So there will be instantiated blocks
4196 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004197 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07004198 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004199 */
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004200int ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004201{
Theodore Ts'o819c4922013-04-03 12:47:17 -04004202 struct ext4_inode_info *ei = EXT4_I(inode);
4203 unsigned int credits;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004204 int err = 0, err2;
Theodore Ts'o819c4922013-04-03 12:47:17 -04004205 handle_t *handle;
4206 struct address_space *mapping = inode->i_mapping;
Theodore Ts'o819c4922013-04-03 12:47:17 -04004207
Theodore Ts'o19b5ef62013-04-03 21:58:52 -04004208 /*
4209 * There is a possibility that we're either freeing the inode
Matthew Wilcoxe04027e2014-03-24 15:15:07 -04004210 * or it's a completely new inode. In those cases we might not
Theodore Ts'o19b5ef62013-04-03 21:58:52 -04004211 * have i_mutex locked because it's not necessary.
4212 */
4213 if (!(inode->i_state & (I_NEW|I_FREEING)))
Al Viro59551022016-01-22 15:40:57 -05004214 WARN_ON(!inode_is_locked(inode));
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004215 trace_ext4_truncate_enter(inode);
4216
Duane Griffin91ef4ca2008-07-11 19:27:31 -04004217 if (!ext4_can_truncate(inode))
zhengliang9a5d2652020-07-01 16:30:27 +08004218 goto out_trace;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004219
Theodore Ts'o5534fb52009-09-17 09:34:16 -04004220 if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004221 ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
Theodore Ts'o7d8f9f72009-02-24 08:21:14 -05004222
Tao Maaef1c852012-12-10 14:06:02 -05004223 if (ext4_has_inline_data(inode)) {
4224 int has_inline = 1;
4225
Theodore Ts'o01daf942017-01-22 19:35:49 -05004226 err = ext4_inline_data_truncate(inode, &has_inline);
zhengliang9a5d2652020-07-01 16:30:27 +08004227 if (err || has_inline)
4228 goto out_trace;
Tao Maaef1c852012-12-10 14:06:02 -05004229 }
4230
Jan Karaa3612932013-08-16 21:19:41 -04004231 /* If we zero-out tail of the page, we have to create jinode for jbd2 */
4232 if (inode->i_size & (inode->i_sb->s_blocksize - 1)) {
4233 if (ext4_inode_attach_jinode(inode) < 0)
zhengliang9a5d2652020-07-01 16:30:27 +08004234 goto out_trace;
Jan Karaa3612932013-08-16 21:19:41 -04004235 }
4236
Amir Goldsteinff9893d2011-06-27 16:36:31 -04004237 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Theodore Ts'o819c4922013-04-03 12:47:17 -04004238 credits = ext4_writepage_trans_blocks(inode);
Amir Goldsteinff9893d2011-06-27 16:36:31 -04004239 else
Theodore Ts'o819c4922013-04-03 12:47:17 -04004240 credits = ext4_blocks_for_truncate(inode);
4241
4242 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
zhengliang9a5d2652020-07-01 16:30:27 +08004243 if (IS_ERR(handle)) {
4244 err = PTR_ERR(handle);
4245 goto out_trace;
4246 }
Theodore Ts'o819c4922013-04-03 12:47:17 -04004247
Lukas Czernereb3544c2013-05-27 23:32:35 -04004248 if (inode->i_size & (inode->i_sb->s_blocksize - 1))
4249 ext4_block_truncate_page(handle, mapping, inode->i_size);
Theodore Ts'o819c4922013-04-03 12:47:17 -04004250
4251 /*
4252 * We add the inode to the orphan list, so that if this
4253 * truncate spans multiple transactions, and we crash, we will
4254 * resume the truncate when the filesystem recovers. It also
4255 * marks the inode dirty, to catch the new size.
4256 *
4257 * Implication: the file must always be in a sane, consistent
4258 * truncatable state while each transaction commits.
4259 */
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004260 err = ext4_orphan_add(handle, inode);
4261 if (err)
Theodore Ts'o819c4922013-04-03 12:47:17 -04004262 goto out_stop;
4263
4264 down_write(&EXT4_I(inode)->i_data_sem);
4265
brookxu27bc4462020-08-17 15:36:15 +08004266 ext4_discard_preallocations(inode, 0);
Theodore Ts'o819c4922013-04-03 12:47:17 -04004267
4268 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Theodore Ts'od0abb362016-11-13 22:02:28 -05004269 err = ext4_ext_truncate(handle, inode);
Theodore Ts'o819c4922013-04-03 12:47:17 -04004270 else
4271 ext4_ind_truncate(handle, inode);
4272
4273 up_write(&ei->i_data_sem);
Theodore Ts'od0abb362016-11-13 22:02:28 -05004274 if (err)
4275 goto out_stop;
Theodore Ts'o819c4922013-04-03 12:47:17 -04004276
4277 if (IS_SYNC(inode))
4278 ext4_handle_sync(handle);
4279
4280out_stop:
4281 /*
4282 * If this was a simple ftruncate() and the file will remain alive,
4283 * then we need to clear up the orphan record which we created above.
4284 * However, if this was a real unlink then we were called by
Wang Shilong58d86a52014-11-25 16:17:29 -05004285 * ext4_evict_inode(), and we allow that function to clean up the
Theodore Ts'o819c4922013-04-03 12:47:17 -04004286 * orphan info for us.
4287 */
4288 if (inode->i_nlink)
4289 ext4_orphan_del(handle, inode);
4290
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004291 inode->i_mtime = inode->i_ctime = current_time(inode);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004292 err2 = ext4_mark_inode_dirty(handle, inode);
4293 if (unlikely(err2 && !err))
4294 err = err2;
Theodore Ts'o819c4922013-04-03 12:47:17 -04004295 ext4_journal_stop(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07004296
zhengliang9a5d2652020-07-01 16:30:27 +08004297out_trace:
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004298 trace_ext4_truncate_exit(inode);
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004299 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004300}
4301
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004302/*
Mingming Cao617ba132006-10-11 01:20:53 -07004303 * ext4_get_inode_loc returns with an extra refcount against the inode's
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004304 * underlying buffer_head on success. If 'in_mem' is true, we have all
4305 * data in memory that is needed to recreate the on-disk version of this
4306 * inode.
4307 */
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07004308static int __ext4_get_inode_loc(struct super_block *sb, unsigned long ino,
4309 struct ext4_iloc *iloc, int in_mem,
4310 ext4_fsblk_t *ret_block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004311{
Theodore Ts'o240799c2008-10-09 23:53:47 -04004312 struct ext4_group_desc *gdp;
4313 struct buffer_head *bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004314 ext4_fsblk_t block;
Linus Torvalds02f03c42019-09-29 17:59:23 -07004315 struct blk_plug plug;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004316 int inodes_per_block, inode_offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004317
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004318 iloc->bh = NULL;
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07004319 if (ino < EXT4_ROOT_INO ||
4320 ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004321 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004322
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07004323 iloc->block_group = (ino - 1) / EXT4_INODES_PER_GROUP(sb);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004324 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
4325 if (!gdp)
4326 return -EIO;
4327
4328 /*
4329 * Figure out the offset within the block group inode table
4330 */
Tao Ma00d09882011-05-09 10:26:41 -04004331 inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07004332 inode_offset = ((ino - 1) %
Theodore Ts'o240799c2008-10-09 23:53:47 -04004333 EXT4_INODES_PER_GROUP(sb));
4334 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
4335 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
4336
4337 bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05004338 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -05004339 return -ENOMEM;
Zhang Yi8e33fad2021-08-26 21:04:08 +08004340 if (ext4_buffer_uptodate(bh))
4341 goto has_buffer;
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04004342
Zhang Yi8e33fad2021-08-26 21:04:08 +08004343 lock_buffer(bh);
4344 /*
4345 * If we have all information of the inode in memory and this
4346 * is the only valid inode in the block, we need not read the
4347 * block.
4348 */
4349 if (in_mem) {
4350 struct buffer_head *bitmap_bh;
4351 int i, start;
4352
4353 start = inode_offset & ~(inodes_per_block - 1);
4354
4355 /* Is the inode bitmap in cache? */
4356 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
4357 if (unlikely(!bitmap_bh))
4358 goto make_io;
4359
4360 /*
4361 * If the inode bitmap isn't in cache then the
4362 * optimisation may end up performing two reads instead
4363 * of one, so skip it.
4364 */
4365 if (!buffer_uptodate(bitmap_bh)) {
4366 brelse(bitmap_bh);
4367 goto make_io;
4368 }
4369 for (i = start; i < start + inodes_per_block; i++) {
4370 if (i == inode_offset)
4371 continue;
4372 if (ext4_test_bit(i, bitmap_bh->b_data))
4373 break;
4374 }
4375 brelse(bitmap_bh);
4376 if (i == start + inodes_per_block) {
4377 /* all other inodes are free, so skip I/O */
4378 memset(bh->b_data, 0, bh->b_size);
4379 set_buffer_uptodate(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004380 unlock_buffer(bh);
4381 goto has_buffer;
4382 }
Zhang Yi8e33fad2021-08-26 21:04:08 +08004383 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004384
4385make_io:
Zhang Yi8e33fad2021-08-26 21:04:08 +08004386 /*
4387 * If we need to do any I/O, try to pre-readahead extra
4388 * blocks from the inode table.
4389 */
4390 blk_start_plug(&plug);
4391 if (EXT4_SB(sb)->s_inode_readahead_blks) {
4392 ext4_fsblk_t b, end, table;
4393 unsigned num;
4394 __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004395
Zhang Yi8e33fad2021-08-26 21:04:08 +08004396 table = ext4_inode_table(sb, gdp);
4397 /* s_inode_readahead_blks is always a power of 2 */
4398 b = block & ~((ext4_fsblk_t) ra_blks - 1);
4399 if (table > b)
4400 b = table;
4401 end = b + ra_blks;
4402 num = EXT4_INODES_PER_GROUP(sb);
4403 if (ext4_has_group_desc_csum(sb))
4404 num -= ext4_itable_unused_count(sb, gdp);
4405 table += num / inodes_per_block;
4406 if (end > table)
4407 end = table;
4408 while (b <= end)
4409 ext4_sb_breadahead_unmovable(sb, b++);
4410 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04004411
Zhang Yi8e33fad2021-08-26 21:04:08 +08004412 /*
4413 * There are other valid inodes in the buffer, this inode
4414 * has in-inode xattrs, or we don't have this inode in memory.
4415 * Read the block from disk.
4416 */
4417 trace_ext4_load_inode(sb, ino);
4418 ext4_read_bh_nowait(bh, REQ_META | REQ_PRIO, NULL);
4419 blk_finish_plug(&plug);
4420 wait_on_buffer(bh);
4421 ext4_simulate_fail_bh(sb, bh, EXT4_SIM_INODE_EIO);
4422 if (!buffer_uptodate(bh)) {
4423 if (ret_block)
4424 *ret_block = block;
4425 brelse(bh);
4426 return -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004427 }
4428has_buffer:
4429 iloc->bh = bh;
4430 return 0;
4431}
4432
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07004433static int __ext4_get_inode_loc_noinmem(struct inode *inode,
4434 struct ext4_iloc *iloc)
4435{
4436 ext4_fsblk_t err_blk;
4437 int ret;
4438
4439 ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, iloc, 0,
4440 &err_blk);
4441
4442 if (ret == -EIO)
4443 ext4_error_inode_block(inode, err_blk, EIO,
4444 "unable to read itable block");
4445
4446 return ret;
4447}
4448
Mingming Cao617ba132006-10-11 01:20:53 -07004449int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004450{
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07004451 ext4_fsblk_t err_blk;
4452 int ret;
4453
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004454 /* We have all inode data except xattrs in memory here. */
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07004455 ret = __ext4_get_inode_loc(inode->i_sb, inode->i_ino, iloc,
4456 !ext4_test_inode_state(inode, EXT4_STATE_XATTR), &err_blk);
4457
4458 if (ret == -EIO)
4459 ext4_error_inode_block(inode, err_blk, EIO,
4460 "unable to read itable block");
4461
4462 return ret;
4463}
4464
4465
4466int ext4_get_fc_inode_loc(struct super_block *sb, unsigned long ino,
4467 struct ext4_iloc *iloc)
4468{
4469 return __ext4_get_inode_loc(sb, ino, iloc, 0, NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004470}
4471
Ira Weinya8ab6d32020-05-28 07:59:58 -07004472static bool ext4_should_enable_dax(struct inode *inode)
Ross Zwisler66425862017-10-12 12:00:59 -04004473{
Ira Weinya8ab6d32020-05-28 07:59:58 -07004474 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4475
Ira Weiny9cb20f92020-05-28 08:00:00 -07004476 if (test_opt2(inode->i_sb, DAX_NEVER))
Ross Zwisler66425862017-10-12 12:00:59 -04004477 return false;
4478 if (!S_ISREG(inode->i_mode))
4479 return false;
4480 if (ext4_should_journal_data(inode))
4481 return false;
4482 if (ext4_has_inline_data(inode))
4483 return false;
Chandan Rajendra592ddec2018-12-12 15:20:10 +05304484 if (ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT))
Ross Zwisler66425862017-10-12 12:00:59 -04004485 return false;
Eric Biggersc93d8f82019-07-22 09:26:24 -07004486 if (ext4_test_inode_flag(inode, EXT4_INODE_VERITY))
4487 return false;
Ira Weinya8ab6d32020-05-28 07:59:58 -07004488 if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags))
4489 return false;
4490 if (test_opt(inode->i_sb, DAX_ALWAYS))
4491 return true;
4492
Ira Weinyb383a732020-05-28 08:00:02 -07004493 return ext4_test_inode_flag(inode, EXT4_INODE_DAX);
Ross Zwisler66425862017-10-12 12:00:59 -04004494}
4495
Ira Weiny043546e2020-05-28 07:59:59 -07004496void ext4_set_inode_flags(struct inode *inode, bool init)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004497{
Mingming Cao617ba132006-10-11 01:20:53 -07004498 unsigned int flags = EXT4_I(inode)->i_flags;
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004499 unsigned int new_fl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004500
Ira Weiny043546e2020-05-28 07:59:59 -07004501 WARN_ON_ONCE(IS_DAX(inode) && init);
4502
Mingming Cao617ba132006-10-11 01:20:53 -07004503 if (flags & EXT4_SYNC_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004504 new_fl |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07004505 if (flags & EXT4_APPEND_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004506 new_fl |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07004507 if (flags & EXT4_IMMUTABLE_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004508 new_fl |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07004509 if (flags & EXT4_NOATIME_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004510 new_fl |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07004511 if (flags & EXT4_DIRSYNC_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004512 new_fl |= S_DIRSYNC;
Ira Weiny043546e2020-05-28 07:59:59 -07004513
4514 /* Because of the way inode_set_flags() works we must preserve S_DAX
4515 * here if already set. */
4516 new_fl |= (inode->i_flags & S_DAX);
4517 if (init && ext4_should_enable_dax(inode))
Ross Zwisler923ae0f2015-02-16 15:59:38 -08004518 new_fl |= S_DAX;
Ira Weiny043546e2020-05-28 07:59:59 -07004519
Eric Biggers2ee6a572017-10-09 12:15:35 -07004520 if (flags & EXT4_ENCRYPT_FL)
4521 new_fl |= S_ENCRYPTED;
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04004522 if (flags & EXT4_CASEFOLD_FL)
4523 new_fl |= S_CASEFOLD;
Eric Biggersc93d8f82019-07-22 09:26:24 -07004524 if (flags & EXT4_VERITY_FL)
4525 new_fl |= S_VERITY;
Theodore Ts'o5f16f322014-03-24 14:43:12 -04004526 inode_set_flags(inode, new_fl,
Eric Biggers2ee6a572017-10-09 12:15:35 -07004527 S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX|
Eric Biggersc93d8f82019-07-22 09:26:24 -07004528 S_ENCRYPTED|S_CASEFOLD|S_VERITY);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004529}
4530
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004531static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004532 struct ext4_inode_info *ei)
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004533{
4534 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004535 struct inode *inode = &(ei->vfs_inode);
4536 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004537
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004538 if (ext4_has_feature_huge_file(sb)) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004539 /* we are using combined 48 bit field */
4540 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
4541 le32_to_cpu(raw_inode->i_blocks_lo);
Theodore Ts'o07a03822010-06-14 09:54:48 -04004542 if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004543 /* i_blocks represent file system block size */
4544 return i_blocks << (inode->i_blkbits - 9);
4545 } else {
4546 return i_blocks;
4547 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004548 } else {
4549 return le32_to_cpu(raw_inode->i_blocks_lo);
4550 }
4551}
Jan Karaff9ddf72007-07-18 09:24:20 -04004552
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004553static inline int ext4_iget_extra_inode(struct inode *inode,
Tao Ma152a7b02012-12-02 11:13:24 -05004554 struct ext4_inode *raw_inode,
4555 struct ext4_inode_info *ei)
4556{
4557 __le32 *magic = (void *)raw_inode +
4558 EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004559
Eric Biggers290ab232016-12-01 14:51:58 -05004560 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize + sizeof(__le32) <=
4561 EXT4_INODE_SIZE(inode->i_sb) &&
4562 *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
Tao Ma152a7b02012-12-02 11:13:24 -05004563 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004564 return ext4_find_inline_data_nolock(inode);
Tao Maf19d5872012-12-10 14:05:51 -05004565 } else
4566 EXT4_I(inode)->i_inline_off = 0;
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004567 return 0;
Tao Ma152a7b02012-12-02 11:13:24 -05004568}
4569
Li Xi040cb372016-01-08 16:01:21 -05004570int ext4_get_projid(struct inode *inode, kprojid_t *projid)
4571{
Kaho Ng0b7b7772016-09-05 23:11:58 -04004572 if (!ext4_has_feature_project(inode->i_sb))
Li Xi040cb372016-01-08 16:01:21 -05004573 return -EOPNOTSUPP;
4574 *projid = EXT4_I(inode)->i_projid;
4575 return 0;
4576}
4577
Eryu Guane254d1a2018-05-10 11:55:31 -04004578/*
4579 * ext4 has self-managed i_version for ea inodes, it stores the lower 32bit of
4580 * refcount in i_version, so use raw values if inode has EXT4_EA_INODE_FL flag
4581 * set.
4582 */
4583static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val)
4584{
4585 if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
4586 inode_set_iversion_raw(inode, val);
4587 else
4588 inode_set_iversion_queried(inode, val);
4589}
4590static inline u64 ext4_inode_peek_iversion(const struct inode *inode)
4591{
4592 if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
4593 return inode_peek_iversion_raw(inode);
4594 else
4595 return inode_peek_iversion(inode);
4596}
4597
Theodore Ts'o8a363972018-12-19 12:29:13 -05004598struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
4599 ext4_iget_flags flags, const char *function,
4600 unsigned int line)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004601{
Mingming Cao617ba132006-10-11 01:20:53 -07004602 struct ext4_iloc iloc;
4603 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08004604 struct ext4_inode_info *ei;
Jan Karabd2c38c2021-08-12 15:31:22 +02004605 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
David Howells1d1fe1e2008-02-07 00:15:37 -08004606 struct inode *inode;
Jan Karab436b9b2009-12-08 23:51:10 -05004607 journal_t *journal = EXT4_SB(sb)->s_journal;
David Howells1d1fe1e2008-02-07 00:15:37 -08004608 long ret;
Darrick J. Wong7e6e1ef2016-12-10 09:55:01 -05004609 loff_t size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004610 int block;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004611 uid_t i_uid;
4612 gid_t i_gid;
Li Xi040cb372016-01-08 16:01:21 -05004613 projid_t i_projid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004614
Theodore Ts'o191ce172018-12-31 22:34:31 -05004615 if ((!(flags & EXT4_IGET_SPECIAL) &&
Jan Karabd2c38c2021-08-12 15:31:22 +02004616 ((ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO) ||
4617 ino == le32_to_cpu(es->s_usr_quota_inum) ||
4618 ino == le32_to_cpu(es->s_grp_quota_inum) ||
Jan Kara02f310f2021-08-16 11:57:06 +02004619 ino == le32_to_cpu(es->s_prj_quota_inum) ||
4620 ino == le32_to_cpu(es->s_orphan_file_inum))) ||
Theodore Ts'o8a363972018-12-19 12:29:13 -05004621 (ino < EXT4_ROOT_INO) ||
Jan Karabd2c38c2021-08-12 15:31:22 +02004622 (ino > le32_to_cpu(es->s_inodes_count))) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004623 if (flags & EXT4_IGET_HANDLE)
4624 return ERR_PTR(-ESTALE);
Jan Kara014c9ca2020-11-27 12:33:57 +01004625 __ext4_error(sb, function, line, false, EFSCORRUPTED, 0,
Theodore Ts'o8a363972018-12-19 12:29:13 -05004626 "inode #%lu: comm %s: iget: illegal inode #",
4627 ino, current->comm);
4628 return ERR_PTR(-EFSCORRUPTED);
4629 }
4630
David Howells1d1fe1e2008-02-07 00:15:37 -08004631 inode = iget_locked(sb, ino);
4632 if (!inode)
4633 return ERR_PTR(-ENOMEM);
4634 if (!(inode->i_state & I_NEW))
4635 return inode;
4636
4637 ei = EXT4_I(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05004638 iloc.bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004639
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07004640 ret = __ext4_get_inode_loc_noinmem(inode, &iloc);
David Howells1d1fe1e2008-02-07 00:15:37 -08004641 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004642 goto bad_inode;
Mingming Cao617ba132006-10-11 01:20:53 -07004643 raw_inode = ext4_raw_inode(&iloc);
Darrick J. Wong814525f2012-04-29 18:31:10 -04004644
Theodore Ts'o8e4b5ea2018-03-29 21:56:09 -04004645 if ((ino == EXT4_ROOT_INO) && (raw_inode->i_links_count == 0)) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004646 ext4_error_inode(inode, function, line, 0,
4647 "iget: root inode unallocated");
Theodore Ts'o8e4b5ea2018-03-29 21:56:09 -04004648 ret = -EFSCORRUPTED;
4649 goto bad_inode;
4650 }
4651
Theodore Ts'o8a363972018-12-19 12:29:13 -05004652 if ((flags & EXT4_IGET_HANDLE) &&
4653 (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) {
4654 ret = -ESTALE;
4655 goto bad_inode;
4656 }
4657
Darrick J. Wong814525f2012-04-29 18:31:10 -04004658 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4659 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
4660 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
Eric Biggers2dc8d9e2016-12-01 14:43:33 -05004661 EXT4_INODE_SIZE(inode->i_sb) ||
4662 (ei->i_extra_isize & 3)) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004663 ext4_error_inode(inode, function, line, 0,
4664 "iget: bad extra_isize %u "
4665 "(inode size %u)",
Eric Biggers2dc8d9e2016-12-01 14:43:33 -05004666 ei->i_extra_isize,
4667 EXT4_INODE_SIZE(inode->i_sb));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004668 ret = -EFSCORRUPTED;
Darrick J. Wong814525f2012-04-29 18:31:10 -04004669 goto bad_inode;
4670 }
4671 } else
4672 ei->i_extra_isize = 0;
4673
4674 /* Precompute checksum seed for inode metadata */
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04004675 if (ext4_has_metadata_csum(sb)) {
Darrick J. Wong814525f2012-04-29 18:31:10 -04004676 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4677 __u32 csum;
4678 __le32 inum = cpu_to_le32(inode->i_ino);
4679 __le32 gen = raw_inode->i_generation;
4680 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
4681 sizeof(inum));
4682 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
4683 sizeof(gen));
4684 }
4685
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07004686 if ((!ext4_inode_csum_verify(inode, raw_inode, ei) ||
4687 ext4_simulate_fail(sb, EXT4_SIM_INODE_CRC)) &&
4688 (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY))) {
4689 ext4_error_inode_err(inode, function, line, 0,
4690 EFSBADCRC, "iget: checksum invalid");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004691 ret = -EFSBADCRC;
Darrick J. Wong814525f2012-04-29 18:31:10 -04004692 goto bad_inode;
4693 }
4694
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004695 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004696 i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4697 i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Kaho Ng0b7b7772016-09-05 23:11:58 -04004698 if (ext4_has_feature_project(sb) &&
Li Xi040cb372016-01-08 16:01:21 -05004699 EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE &&
4700 EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
4701 i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid);
4702 else
4703 i_projid = EXT4_DEF_PROJID;
4704
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004705 if (!(test_opt(inode->i_sb, NO_UID32))) {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004706 i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4707 i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004708 }
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004709 i_uid_write(inode, i_uid);
4710 i_gid_write(inode, i_gid);
Li Xi040cb372016-01-08 16:01:21 -05004711 ei->i_projid = make_kprojid(&init_user_ns, i_projid);
Miklos Szeredibfe86842011-10-28 14:13:29 +02004712 set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004713
Theodore Ts'o353eb832011-01-10 12:18:25 -05004714 ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
Tao Ma67cf5b02012-12-10 14:04:46 -05004715 ei->i_inline_off = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004716 ei->i_dir_start_lookup = 0;
4717 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4718 /* We now have enough fields to check if the inode was active or not.
4719 * This is needed because nfsd might try to access dead inodes
4720 * the test is that same one that e2fsck uses
4721 * NeilBrown 1999oct15
4722 */
4723 if (inode->i_nlink == 0) {
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004724 if ((inode->i_mode == 0 ||
4725 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) &&
4726 ino != EXT4_BOOT_LOADER_INO) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004727 /* this inode is deleted */
David Howells1d1fe1e2008-02-07 00:15:37 -08004728 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004729 goto bad_inode;
4730 }
4731 /* The only unlinked inodes we let through here have
4732 * valid i_mode and are being read by the orphan
4733 * recovery code: that's fine, we're about to complete
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004734 * the process of deleting those.
4735 * OR it is the EXT4_BOOT_LOADER_INO which is
4736 * not initialized on a new filesystem. */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004737 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004738 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Ira Weiny043546e2020-05-28 07:59:59 -07004739 ext4_set_inode_flags(inode, true);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004740 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004741 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004742 if (ext4_has_feature_64bit(sb))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004743 ei->i_file_acl |=
4744 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04004745 inode->i_size = ext4_isize(sb, raw_inode);
Darrick J. Wong7e6e1ef2016-12-10 09:55:01 -05004746 if ((size = i_size_read(inode)) < 0) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004747 ext4_error_inode(inode, function, line, 0,
4748 "iget: bad i_size value: %lld", size);
Darrick J. Wong7e6e1ef2016-12-10 09:55:01 -05004749 ret = -EFSCORRUPTED;
4750 goto bad_inode;
4751 }
Jan Kara48a34312020-02-10 15:43:16 +01004752 /*
4753 * If dir_index is not enabled but there's dir with INDEX flag set,
4754 * we'd normally treat htree data as empty space. But with metadata
4755 * checksumming that corrupts checksums so forbid that.
4756 */
4757 if (!ext4_has_feature_dir_index(sb) && ext4_has_metadata_csum(sb) &&
4758 ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) {
4759 ext4_error_inode(inode, function, line, 0,
4760 "iget: Dir with htree data on filesystem without dir_index feature.");
4761 ret = -EFSCORRUPTED;
4762 goto bad_inode;
4763 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004764 ei->i_disksize = inode->i_size;
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03004765#ifdef CONFIG_QUOTA
4766 ei->i_reserved_quota = 0;
4767#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004768 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4769 ei->i_block_group = iloc.block_group;
Theodore Ts'oa4912122009-03-12 12:18:34 -04004770 ei->i_last_alloc_group = ~0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004771 /*
4772 * NOTE! The in-memory inode i_data array is in little-endian order
4773 * even on big-endian machines: we do NOT byteswap the block numbers!
4774 */
Mingming Cao617ba132006-10-11 01:20:53 -07004775 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004776 ei->i_data[block] = raw_inode->i_block[block];
4777 INIT_LIST_HEAD(&ei->i_orphan);
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07004778 ext4_fc_init_inode(&ei->vfs_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004779
Jan Karab436b9b2009-12-08 23:51:10 -05004780 /*
4781 * Set transaction id's of transactions that have to be committed
4782 * to finish f[data]sync. We set them to currently running transaction
4783 * as we cannot be sure that the inode or some of its metadata isn't
4784 * part of the transaction - the inode could have been reclaimed and
4785 * now it is reread from disk.
4786 */
4787 if (journal) {
4788 transaction_t *transaction;
4789 tid_t tid;
4790
Theodore Ts'oa931da62010-08-03 21:35:12 -04004791 read_lock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05004792 if (journal->j_running_transaction)
4793 transaction = journal->j_running_transaction;
4794 else
4795 transaction = journal->j_committing_transaction;
4796 if (transaction)
4797 tid = transaction->t_tid;
4798 else
4799 tid = journal->j_commit_sequence;
Theodore Ts'oa931da62010-08-03 21:35:12 -04004800 read_unlock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05004801 ei->i_sync_tid = tid;
4802 ei->i_datasync_tid = tid;
4803 }
4804
Eric Sandeen0040d982008-02-05 22:36:43 -05004805 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004806 if (ei->i_extra_isize == 0) {
4807 /* The extra space is currently unused. Use it. */
Eric Biggers2dc8d9e2016-12-01 14:43:33 -05004808 BUILD_BUG_ON(sizeof(struct ext4_inode) & 3);
Mingming Cao617ba132006-10-11 01:20:53 -07004809 ei->i_extra_isize = sizeof(struct ext4_inode) -
4810 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004811 } else {
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004812 ret = ext4_iget_extra_inode(inode, raw_inode, ei);
4813 if (ret)
4814 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004815 }
Darrick J. Wong814525f2012-04-29 18:31:10 -04004816 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004817
Kalpak Shahef7f3832007-07-18 09:15:20 -04004818 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4819 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4820 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4821 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4822
Theodore Ts'oed3654e2014-03-24 14:09:06 -04004823 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
Jeff Laytonee73f9a2018-01-09 08:21:39 -05004824 u64 ivers = le32_to_cpu(raw_inode->i_disk_version);
4825
Theodore Ts'oc4f65702014-03-20 00:32:57 -04004826 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4827 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
Jeff Laytonee73f9a2018-01-09 08:21:39 -05004828 ivers |=
Theodore Ts'oc4f65702014-03-20 00:32:57 -04004829 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
4830 }
Eryu Guane254d1a2018-05-10 11:55:31 -04004831 ext4_inode_set_iversion_queried(inode, ivers);
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004832 }
4833
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004834 ret = 0;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004835 if (ei->i_file_acl &&
Jan Karace9f24c2020-07-28 15:04:34 +02004836 !ext4_inode_block_valid(inode, ei->i_file_acl, 1)) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004837 ext4_error_inode(inode, function, line, 0,
4838 "iget: bad extended attribute block %llu",
Theodore Ts'o24676da2010-05-16 21:00:00 -04004839 ei->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004840 ret = -EFSCORRUPTED;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004841 goto bad_inode;
Tao Maf19d5872012-12-10 14:05:51 -05004842 } else if (!ext4_has_inline_data(inode)) {
Liu Songbc716522018-08-02 00:11:16 -04004843 /* validate the block references in the inode */
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07004844 if (!(EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY) &&
4845 (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4846 (S_ISLNK(inode->i_mode) &&
4847 !ext4_inode_is_fast_symlink(inode)))) {
Liu Songbc716522018-08-02 00:11:16 -04004848 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Tao Maf19d5872012-12-10 14:05:51 -05004849 ret = ext4_ext_check_inode(inode);
Liu Songbc716522018-08-02 00:11:16 -04004850 else
4851 ret = ext4_ind_check_inode(inode);
Tao Maf19d5872012-12-10 14:05:51 -05004852 }
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004853 }
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004854 if (ret)
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004855 goto bad_inode;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -04004856
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004857 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004858 inode->i_op = &ext4_file_inode_operations;
Boaz Harroshbe64f882015-04-15 16:15:17 -07004859 inode->i_fop = &ext4_file_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07004860 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004861 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004862 inode->i_op = &ext4_dir_inode_operations;
4863 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004864 } else if (S_ISLNK(inode->i_mode)) {
Luis R. Rodriguez6390d332018-05-13 16:45:56 -04004865 /* VFS does not allow setting these so must be corruption */
4866 if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004867 ext4_error_inode(inode, function, line, 0,
4868 "iget: immutable or append flags "
4869 "not allowed on symlinks");
Luis R. Rodriguez6390d332018-05-13 16:45:56 -04004870 ret = -EFSCORRUPTED;
4871 goto bad_inode;
4872 }
Chandan Rajendra592ddec2018-12-12 15:20:10 +05304873 if (IS_ENCRYPTED(inode)) {
Al Viroa7a67e82015-04-27 17:51:30 -04004874 inode->i_op = &ext4_encrypted_symlink_inode_operations;
4875 ext4_set_aops(inode);
4876 } else if (ext4_inode_is_fast_symlink(inode)) {
Al Viro75e75662015-05-02 10:13:58 -04004877 inode->i_link = (char *)ei->i_data;
Mingming Cao617ba132006-10-11 01:20:53 -07004878 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00004879 nd_terminate_link(ei->i_data, inode->i_size,
4880 sizeof(ei->i_data) - 1);
4881 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004882 inode->i_op = &ext4_symlink_inode_operations;
4883 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004884 }
Al Viro21fc61c2015-11-17 01:07:57 -05004885 inode_nohighmem(inode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004886 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
4887 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004888 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004889 if (raw_inode->i_block[0])
4890 init_special_inode(inode, inode->i_mode,
4891 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4892 else
4893 init_special_inode(inode, inode->i_mode,
4894 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004895 } else if (ino == EXT4_BOOT_LOADER_INO) {
4896 make_bad_inode(inode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004897 } else {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004898 ret = -EFSCORRUPTED;
Theodore Ts'o8a363972018-12-19 12:29:13 -05004899 ext4_error_inode(inode, function, line, 0,
4900 "iget: bogus i_mode (%o)", inode->i_mode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004901 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004902 }
Theodore Ts'o6456ca62019-09-03 01:43:17 -04004903 if (IS_CASEFOLDED(inode) && !ext4_has_feature_casefold(inode->i_sb))
4904 ext4_error_inode(inode, function, line, 0,
4905 "casefold flag without casefold feature");
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004906 brelse(iloc.bh);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04004907
David Howells1d1fe1e2008-02-07 00:15:37 -08004908 unlock_new_inode(inode);
4909 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004910
4911bad_inode:
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004912 brelse(iloc.bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004913 iget_failed(inode);
4914 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004915}
4916
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004917static int ext4_inode_blocks_set(handle_t *handle,
4918 struct ext4_inode *raw_inode,
4919 struct ext4_inode_info *ei)
4920{
4921 struct inode *inode = &(ei->vfs_inode);
Qian Cai28936b62020-02-21 23:32:58 -05004922 u64 i_blocks = READ_ONCE(inode->i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004923 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004924
4925 if (i_blocks <= ~0U) {
4926 /*
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004927 * i_blocks can be represented in a 32 bit variable
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004928 * as multiple of 512 bytes
4929 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004930 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004931 raw_inode->i_blocks_high = 0;
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004932 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004933 return 0;
4934 }
Zhang Yibaaae9792021-08-26 21:04:09 +08004935
4936 /*
4937 * This should never happen since sb->s_maxbytes should not have
4938 * allowed this, sb->s_maxbytes was set according to the huge_file
4939 * feature in ext4_fill_super().
4940 */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004941 if (!ext4_has_feature_huge_file(sb))
Zhang Yibaaae9792021-08-26 21:04:09 +08004942 return -EFSCORRUPTED;
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004943
4944 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004945 /*
4946 * i_blocks can be represented in a 48 bit variable
4947 * as multiple of 512 bytes
4948 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004949 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004950 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004951 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004952 } else {
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004953 ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004954 /* i_block is stored in file system block size */
4955 i_blocks = i_blocks >> (inode->i_blkbits - 9);
4956 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4957 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004958 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004959 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004960}
4961
David Howells3f19b2a2017-12-01 11:40:16 +00004962static void __ext4_update_other_inode_time(struct super_block *sb,
4963 unsigned long orig_ino,
4964 unsigned long ino,
4965 struct ext4_inode *raw_inode)
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004966{
David Howells3f19b2a2017-12-01 11:40:16 +00004967 struct inode *inode;
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004968
David Howells3f19b2a2017-12-01 11:40:16 +00004969 inode = find_inode_by_ino_rcu(sb, ino);
4970 if (!inode)
4971 return;
4972
Eric Biggersed296c62021-01-12 11:02:53 -08004973 if (!inode_is_dirtytime_only(inode))
David Howells3f19b2a2017-12-01 11:40:16 +00004974 return;
4975
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004976 spin_lock(&inode->i_lock);
Eric Biggersed296c62021-01-12 11:02:53 -08004977 if (inode_is_dirtytime_only(inode)) {
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004978 struct ext4_inode_info *ei = EXT4_I(inode);
4979
Jan Kara5fcd5752020-05-29 16:24:43 +02004980 inode->i_state &= ~I_DIRTY_TIME;
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004981 spin_unlock(&inode->i_lock);
4982
4983 spin_lock(&ei->i_raw_lock);
David Howells3f19b2a2017-12-01 11:40:16 +00004984 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
4985 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
4986 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
4987 ext4_inode_csum_set(inode, raw_inode, ei);
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004988 spin_unlock(&ei->i_raw_lock);
David Howells3f19b2a2017-12-01 11:40:16 +00004989 trace_ext4_other_inode_update_time(inode, orig_ino);
4990 return;
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004991 }
4992 spin_unlock(&inode->i_lock);
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004993}
4994
4995/*
4996 * Opportunistically update the other time fields for other inodes in
4997 * the same inode table block.
4998 */
4999static void ext4_update_other_inodes_time(struct super_block *sb,
5000 unsigned long orig_ino, char *buf)
5001{
Theodore Ts'oa26f4992015-02-02 00:37:02 -05005002 unsigned long ino;
5003 int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
5004 int inode_size = EXT4_INODE_SIZE(sb);
5005
Theodore Ts'o0f0ff9a2015-07-01 23:37:46 -04005006 /*
5007 * Calculate the first inode in the inode table block. Inode
5008 * numbers are one-based. That is, the first inode in a block
5009 * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1).
5010 */
5011 ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1;
David Howells3f19b2a2017-12-01 11:40:16 +00005012 rcu_read_lock();
Theodore Ts'oa26f4992015-02-02 00:37:02 -05005013 for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) {
5014 if (ino == orig_ino)
5015 continue;
David Howells3f19b2a2017-12-01 11:40:16 +00005016 __ext4_update_other_inode_time(sb, orig_ino, ino,
5017 (struct ext4_inode *)buf);
Theodore Ts'oa26f4992015-02-02 00:37:02 -05005018 }
David Howells3f19b2a2017-12-01 11:40:16 +00005019 rcu_read_unlock();
Theodore Ts'oa26f4992015-02-02 00:37:02 -05005020}
5021
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005022/*
5023 * Post the struct inode info into an on-disk inode location in the
5024 * buffer-cache. This gobbles the caller's reference to the
5025 * buffer_head in the inode location struct.
5026 *
5027 * The caller must have write access to iloc->bh.
5028 */
Mingming Cao617ba132006-10-11 01:20:53 -07005029static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005030 struct inode *inode,
Frank Mayhar830156c2009-09-29 10:07:47 -04005031 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005032{
Mingming Cao617ba132006-10-11 01:20:53 -07005033 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
5034 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005035 struct buffer_head *bh = iloc->bh;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005036 struct super_block *sb = inode->i_sb;
Shijie Luo7d8bd3c2021-03-12 01:50:51 -05005037 int err = 0, block;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005038 int need_datasync = 0, set_large_file = 0;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08005039 uid_t i_uid;
5040 gid_t i_gid;
Li Xi040cb372016-01-08 16:01:21 -05005041 projid_t i_projid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005042
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005043 spin_lock(&ei->i_raw_lock);
5044
Zhang Yibaaae9792021-08-26 21:04:09 +08005045 /*
5046 * For fields not tracked in the in-memory inode, initialise them
5047 * to zero for new inodes.
5048 */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005049 if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
Mingming Cao617ba132006-10-11 01:20:53 -07005050 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005051
Luo Meng13221812020-10-20 09:36:31 +08005052 err = ext4_inode_blocks_set(handle, raw_inode, ei);
Luo Meng13221812020-10-20 09:36:31 +08005053
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005054 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08005055 i_uid = i_uid_read(inode);
5056 i_gid = i_gid_read(inode);
Li Xi040cb372016-01-08 16:01:21 -05005057 i_projid = from_kprojid(&init_user_ns, ei->i_projid);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04005058 if (!(test_opt(inode->i_sb, NO_UID32))) {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08005059 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid));
5060 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid));
Zhang Yibaaae9792021-08-26 21:04:09 +08005061 /*
5062 * Fix up interoperability with old kernels. Otherwise,
5063 * old inodes get re-used with the upper 16 bits of the
5064 * uid/gid intact.
5065 */
Daeho Jeong93e3b4e2016-09-05 22:56:10 -04005066 if (ei->i_dtime && list_empty(&ei->i_orphan)) {
5067 raw_inode->i_uid_high = 0;
5068 raw_inode->i_gid_high = 0;
5069 } else {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005070 raw_inode->i_uid_high =
Eric W. Biederman08cefc72012-02-07 15:41:49 -08005071 cpu_to_le16(high_16_bits(i_uid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005072 raw_inode->i_gid_high =
Eric W. Biederman08cefc72012-02-07 15:41:49 -08005073 cpu_to_le16(high_16_bits(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005074 }
5075 } else {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08005076 raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid));
5077 raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005078 raw_inode->i_uid_high = 0;
5079 raw_inode->i_gid_high = 0;
5080 }
5081 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04005082
5083 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
5084 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
5085 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
5086 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
5087
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005088 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Theodore Ts'o353eb832011-01-10 12:18:25 -05005089 raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
Theodore Ts'oed3654e2014-03-24 14:09:06 -04005090 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT)))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07005091 raw_inode->i_file_acl_high =
5092 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05005093 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Qiujun Huangdce8e232020-02-24 23:02:46 +08005094 if (READ_ONCE(ei->i_disksize) != ext4_isize(inode->i_sb, raw_inode)) {
Jan Karab71fc072012-09-26 21:52:20 -04005095 ext4_isize_set(raw_inode, ei->i_disksize);
5096 need_datasync = 1;
5097 }
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05005098 if (ei->i_disksize > 0x7fffffffULL) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005099 if (!ext4_has_feature_large_file(sb) ||
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05005100 EXT4_SB(sb)->s_es->s_rev_level ==
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005101 cpu_to_le32(EXT4_GOOD_OLD_REV))
5102 set_large_file = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005103 }
5104 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
5105 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
5106 if (old_valid_dev(inode->i_rdev)) {
5107 raw_inode->i_block[0] =
5108 cpu_to_le32(old_encode_dev(inode->i_rdev));
5109 raw_inode->i_block[1] = 0;
5110 } else {
5111 raw_inode->i_block[0] = 0;
5112 raw_inode->i_block[1] =
5113 cpu_to_le32(new_encode_dev(inode->i_rdev));
5114 raw_inode->i_block[2] = 0;
5115 }
Tao Maf19d5872012-12-10 14:05:51 -05005116 } else if (!ext4_has_inline_data(inode)) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04005117 for (block = 0; block < EXT4_N_BLOCKS; block++)
5118 raw_inode->i_block[block] = ei->i_data[block];
Tao Maf19d5872012-12-10 14:05:51 -05005119 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005120
Theodore Ts'oed3654e2014-03-24 14:09:06 -04005121 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
Eryu Guane254d1a2018-05-10 11:55:31 -04005122 u64 ivers = ext4_inode_peek_iversion(inode);
Jeff Laytonee73f9a2018-01-09 08:21:39 -05005123
5124 raw_inode->i_disk_version = cpu_to_le32(ivers);
Theodore Ts'oc4f65702014-03-20 00:32:57 -04005125 if (ei->i_extra_isize) {
5126 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
5127 raw_inode->i_version_hi =
Jeff Laytonee73f9a2018-01-09 08:21:39 -05005128 cpu_to_le32(ivers >> 32);
Theodore Ts'oc4f65702014-03-20 00:32:57 -04005129 raw_inode->i_extra_isize =
5130 cpu_to_le16(ei->i_extra_isize);
5131 }
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005132 }
Li Xi040cb372016-01-08 16:01:21 -05005133
Zhang Yibaaae9792021-08-26 21:04:09 +08005134 if (i_projid != EXT4_DEF_PROJID &&
5135 !ext4_has_feature_project(inode->i_sb))
5136 err = err ?: -EFSCORRUPTED;
Li Xi040cb372016-01-08 16:01:21 -05005137
5138 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
5139 EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
5140 raw_inode->i_projid = cpu_to_le32(i_projid);
5141
Darrick J. Wong814525f2012-04-29 18:31:10 -04005142 ext4_inode_csum_set(inode, raw_inode, ei);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005143 spin_unlock(&ei->i_raw_lock);
Zhang Yibaaae9792021-08-26 21:04:09 +08005144 if (err) {
5145 EXT4_ERROR_INODE(inode, "corrupted inode contents");
5146 goto out_brelse;
5147 }
5148
Linus Torvalds1751e8a2017-11-27 13:05:09 -08005149 if (inode->i_sb->s_flags & SB_LAZYTIME)
Theodore Ts'oa26f4992015-02-02 00:37:02 -05005150 ext4_update_other_inodes_time(inode->i_sb, inode->i_ino,
5151 bh->b_data);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005152
Frank Mayhar830156c2009-09-29 10:07:47 -04005153 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Shijie Luo7d8bd3c2021-03-12 01:50:51 -05005154 err = ext4_handle_dirty_metadata(handle, NULL, bh);
5155 if (err)
Zhang Yibaaae9792021-08-26 21:04:09 +08005156 goto out_error;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005157 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005158 if (set_large_file) {
liang xie5d601252014-05-12 22:06:43 -04005159 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access");
Jan Kara188c2992021-08-16 11:57:04 +02005160 err = ext4_journal_get_write_access(handle, sb,
5161 EXT4_SB(sb)->s_sbh,
5162 EXT4_JTR_NONE);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005163 if (err)
Zhang Yibaaae9792021-08-26 21:04:09 +08005164 goto out_error;
Jan Kara05c2c002020-12-16 11:18:39 +01005165 lock_buffer(EXT4_SB(sb)->s_sbh);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005166 ext4_set_feature_large_file(sb);
Jan Kara05c2c002020-12-16 11:18:39 +01005167 ext4_superblock_csum_set(sb);
5168 unlock_buffer(EXT4_SB(sb)->s_sbh);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005169 ext4_handle_sync(handle);
Jan Karaa3f5cf12020-12-16 11:18:44 +01005170 err = ext4_handle_dirty_metadata(handle, NULL,
5171 EXT4_SB(sb)->s_sbh);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005172 }
Jan Karab71fc072012-09-26 21:52:20 -04005173 ext4_update_inode_fsync_trans(handle, inode, need_datasync);
Zhang Yibaaae9792021-08-26 21:04:09 +08005174out_error:
5175 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005176out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04005177 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005178 return err;
5179}
5180
5181/*
Mingming Cao617ba132006-10-11 01:20:53 -07005182 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005183 *
5184 * We are called from a few places:
5185 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005186 * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005187 * Here, there will be no transaction running. We wait for any running
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005188 * transaction to commit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005189 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005190 * - Within flush work (sys_sync(), kupdate and such).
5191 * We wait on commit, if told to.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005192 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005193 * - Within iput_final() -> write_inode_now()
5194 * We wait on commit, if told to.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005195 *
5196 * In all cases it is actually safe for us to return without doing anything,
5197 * because the inode has been copied into a raw inode buffer in
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005198 * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL
5199 * writeback.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005200 *
5201 * Note that we are absolutely dependent upon all inode dirtiers doing the
5202 * right thing: they *must* call mark_inode_dirty() after dirtying info in
5203 * which we are interested.
5204 *
5205 * It would be a bug for them to not do this. The code:
5206 *
5207 * mark_inode_dirty(inode)
5208 * stuff();
5209 * inode->i_size = expr;
5210 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005211 * is in error because write_inode() could occur while `stuff()' is running,
5212 * and the new i_size will be lost. Plus the inode will no longer be on the
5213 * superblock's dirty inode list.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005214 */
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005215int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005216{
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005217 int err;
5218
Theodore Ts'o18f2c4f2018-12-19 14:36:58 -05005219 if (WARN_ON_ONCE(current->flags & PF_MEMALLOC) ||
5220 sb_rdonly(inode->i_sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005221 return 0;
5222
Theodore Ts'o18f2c4f2018-12-19 14:36:58 -05005223 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5224 return -EIO;
5225
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005226 if (EXT4_SB(inode->i_sb)->s_journal) {
5227 if (ext4_journal_current_handle()) {
5228 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
5229 dump_stack();
5230 return -EIO;
5231 }
5232
Jan Kara10542c22014-03-04 10:50:50 -05005233 /*
5234 * No need to force transaction in WB_SYNC_NONE mode. Also
5235 * ext4_sync_fs() will force the commit after everything is
5236 * written.
5237 */
5238 if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync)
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005239 return 0;
5240
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07005241 err = ext4_fc_commit(EXT4_SB(inode->i_sb)->s_journal,
Theodore Ts'o18f2c4f2018-12-19 14:36:58 -05005242 EXT4_I(inode)->i_sync_tid);
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005243 } else {
5244 struct ext4_iloc iloc;
5245
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07005246 err = __ext4_get_inode_loc_noinmem(inode, &iloc);
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005247 if (err)
5248 return err;
Jan Kara10542c22014-03-04 10:50:50 -05005249 /*
5250 * sync(2) will flush the whole buffer cache. No need to do
5251 * it here separately for each inode.
5252 */
5253 if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
Frank Mayhar830156c2009-09-29 10:07:47 -04005254 sync_dirty_buffer(iloc.bh);
5255 if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04005256 ext4_error_inode_block(inode, iloc.bh->b_blocknr, EIO,
5257 "IO error syncing inode");
Frank Mayhar830156c2009-09-29 10:07:47 -04005258 err = -EIO;
5259 }
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04005260 brelse(iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005261 }
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005262 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005263}
5264
5265/*
Jan Kara53e87262012-12-25 13:29:52 -05005266 * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
5267 * buffers that are attached to a page stradding i_size and are undergoing
5268 * commit. In that case we have to wait for commit to finish and try again.
5269 */
5270static void ext4_wait_for_tail_page_commit(struct inode *inode)
5271{
5272 struct page *page;
5273 unsigned offset;
5274 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
5275 tid_t commit_tid = 0;
5276 int ret;
5277
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005278 offset = inode->i_size & (PAGE_SIZE - 1);
Jan Kara53e87262012-12-25 13:29:52 -05005279 /*
yangerkun565333a2019-09-19 14:35:08 +08005280 * If the page is fully truncated, we don't need to wait for any commit
5281 * (and we even should not as __ext4_journalled_invalidatepage() may
5282 * strip all buffers from the page but keep the page dirty which can then
5283 * confuse e.g. concurrent ext4_writepage() seeing dirty page without
5284 * buffers). Also we don't need to wait for any commit if all buffers in
5285 * the page remain valid. This is most beneficial for the common case of
5286 * blocksize == PAGESIZE.
Jan Kara53e87262012-12-25 13:29:52 -05005287 */
yangerkun565333a2019-09-19 14:35:08 +08005288 if (!offset || offset > (PAGE_SIZE - i_blocksize(inode)))
Jan Kara53e87262012-12-25 13:29:52 -05005289 return;
5290 while (1) {
5291 page = find_lock_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005292 inode->i_size >> PAGE_SHIFT);
Jan Kara53e87262012-12-25 13:29:52 -05005293 if (!page)
5294 return;
Lukas Czernerca99fdd2013-05-21 23:25:01 -04005295 ret = __ext4_journalled_invalidatepage(page, offset,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005296 PAGE_SIZE - offset);
Jan Kara53e87262012-12-25 13:29:52 -05005297 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005298 put_page(page);
Jan Kara53e87262012-12-25 13:29:52 -05005299 if (ret != -EBUSY)
5300 return;
5301 commit_tid = 0;
5302 read_lock(&journal->j_state_lock);
5303 if (journal->j_committing_transaction)
5304 commit_tid = journal->j_committing_transaction->t_tid;
5305 read_unlock(&journal->j_state_lock);
5306 if (commit_tid)
5307 jbd2_log_wait_commit(journal, commit_tid);
5308 }
5309}
5310
5311/*
Mingming Cao617ba132006-10-11 01:20:53 -07005312 * ext4_setattr()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005313 *
5314 * Called from notify_change.
5315 *
5316 * We want to trap VFS attempts to truncate the file as soon as
5317 * possible. In particular, we want to make sure that when the VFS
5318 * shrinks i_size, we put the inode on the orphan list and modify
5319 * i_disksize immediately, so that during the subsequent flushing of
5320 * dirty pages and freeing of disk blocks, we can guarantee that any
5321 * commit will leave the blocks being flushed in an unused state on
5322 * disk. (On recovery, the inode will get truncated and the blocks will
5323 * be freed, so we have a strong guarantee that no future commit will
5324 * leave these blocks visible to the user.)
5325 *
Jan Kara678aaf42008-07-11 19:27:31 -04005326 * Another thing we have to assure is that if we are in ordered mode
5327 * and inode is still attached to the committing transaction, we must
5328 * we start writeout of all the dirty pages which are being truncated.
5329 * This way we are sure that all the data written in the previous
5330 * transaction are already on disk (truncate waits for pages under
5331 * writeback).
5332 *
5333 * Called with inode->i_mutex down.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005334 */
Christian Brauner549c7292021-01-21 14:19:43 +01005335int ext4_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
5336 struct iattr *attr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005337{
David Howells2b0143b2015-03-17 22:25:59 +00005338 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005339 int error, rc = 0;
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04005340 int orphan = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005341 const unsigned int ia_valid = attr->ia_valid;
5342
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05005343 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5344 return -EIO;
5345
Theodore Ts'o02b016c2019-06-09 22:04:33 -04005346 if (unlikely(IS_IMMUTABLE(inode)))
5347 return -EPERM;
5348
5349 if (unlikely(IS_APPEND(inode) &&
5350 (ia_valid & (ATTR_MODE | ATTR_UID |
5351 ATTR_GID | ATTR_TIMES_SET))))
5352 return -EPERM;
5353
Christian Brauner14f3db52021-01-21 14:19:57 +01005354 error = setattr_prepare(mnt_userns, dentry, attr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005355 if (error)
5356 return error;
5357
Eric Biggers3ce2b8d2017-10-18 20:21:58 -04005358 error = fscrypt_prepare_setattr(dentry, attr);
5359 if (error)
5360 return error;
5361
Eric Biggersc93d8f82019-07-22 09:26:24 -07005362 error = fsverity_prepare_setattr(dentry, attr);
5363 if (error)
5364 return error;
5365
Jan Karaa7cdade2015-06-29 16:22:54 +02005366 if (is_quota_modification(inode, attr)) {
5367 error = dquot_initialize(inode);
5368 if (error)
5369 return error;
5370 }
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07005371 ext4_fc_start_update(inode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08005372 if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
5373 (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005374 handle_t *handle;
5375
5376 /* (user+group)*(old+new) structure, inode write (sb,
5377 * inode block, ? - but truncate inode update has it) */
Theodore Ts'o9924a922013-02-08 21:59:22 -05005378 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
5379 (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) +
5380 EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005381 if (IS_ERR(handle)) {
5382 error = PTR_ERR(handle);
5383 goto err_out;
5384 }
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -04005385
5386 /* dquot_transfer() calls back ext4_get_inode_usage() which
5387 * counts xattr inode references.
5388 */
5389 down_read(&EXT4_I(inode)->xattr_sem);
Christoph Hellwigb43fa822010-03-03 09:05:03 -05005390 error = dquot_transfer(inode, attr);
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -04005391 up_read(&EXT4_I(inode)->xattr_sem);
5392
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005393 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07005394 ext4_journal_stop(handle);
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07005395 ext4_fc_stop_update(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005396 return error;
5397 }
5398 /* Update corresponding info in inode so that everything is in
5399 * one transaction */
5400 if (attr->ia_valid & ATTR_UID)
5401 inode->i_uid = attr->ia_uid;
5402 if (attr->ia_valid & ATTR_GID)
5403 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07005404 error = ext4_mark_inode_dirty(handle, inode);
5405 ext4_journal_stop(handle);
Pan Bian512c15e2021-01-17 00:57:32 -08005406 if (unlikely(error)) {
5407 ext4_fc_stop_update(inode);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07005408 return error;
Pan Bian512c15e2021-01-17 00:57:32 -08005409 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005410 }
5411
Josef Bacik3da40c72015-06-22 00:31:26 -04005412 if (attr->ia_valid & ATTR_SIZE) {
Jan Kara52083862013-08-17 10:07:17 -04005413 handle_t *handle;
Josef Bacik3da40c72015-06-22 00:31:26 -04005414 loff_t oldsize = inode->i_size;
Jan Karab9c1c262019-05-30 11:56:23 -04005415 int shrink = (attr->ia_size < inode->i_size);
Christoph Hellwig562c72aa52011-06-24 14:29:45 -04005416
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005417 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
Eric Sandeene2b46572008-01-28 23:58:27 -05005418 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5419
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07005420 if (attr->ia_size > sbi->s_bitmap_maxbytes) {
5421 ext4_fc_stop_update(inode);
Theodore Ts'o0c095c72010-07-27 11:56:06 -04005422 return -EFBIG;
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07005423 }
Eric Sandeene2b46572008-01-28 23:58:27 -05005424 }
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07005425 if (!S_ISREG(inode->i_mode)) {
5426 ext4_fc_stop_update(inode);
Josef Bacik3da40c72015-06-22 00:31:26 -04005427 return -EINVAL;
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07005428 }
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005429
5430 if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size)
5431 inode_inc_iversion(inode);
5432
Jan Karab9c1c262019-05-30 11:56:23 -04005433 if (shrink) {
5434 if (ext4_should_order_data(inode)) {
5435 error = ext4_begin_ordered_truncate(inode,
Jan Kara678aaf42008-07-11 19:27:31 -04005436 attr->ia_size);
Jan Karab9c1c262019-05-30 11:56:23 -04005437 if (error)
5438 goto err_out;
5439 }
5440 /*
5441 * Blocks are going to be removed from the inode. Wait
5442 * for dio in flight.
5443 */
5444 inode_dio_wait(inode);
Josef Bacik3da40c72015-06-22 00:31:26 -04005445 }
Jan Karab9c1c262019-05-30 11:56:23 -04005446
5447 down_write(&EXT4_I(inode)->i_mmap_sem);
5448
5449 rc = ext4_break_layouts(inode);
5450 if (rc) {
5451 up_write(&EXT4_I(inode)->i_mmap_sem);
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07005452 goto err_out;
Jan Karab9c1c262019-05-30 11:56:23 -04005453 }
5454
Josef Bacik3da40c72015-06-22 00:31:26 -04005455 if (attr->ia_size != inode->i_size) {
Jan Kara52083862013-08-17 10:07:17 -04005456 handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
5457 if (IS_ERR(handle)) {
5458 error = PTR_ERR(handle);
Jan Karab9c1c262019-05-30 11:56:23 -04005459 goto out_mmap_sem;
Jan Kara52083862013-08-17 10:07:17 -04005460 }
Josef Bacik3da40c72015-06-22 00:31:26 -04005461 if (ext4_handle_valid(handle) && shrink) {
Jan Kara52083862013-08-17 10:07:17 -04005462 error = ext4_orphan_add(handle, inode);
5463 orphan = 1;
5464 }
Eryu Guan911af572015-07-28 15:08:41 -04005465 /*
5466 * Update c/mtime on truncate up, ext4_truncate() will
5467 * update c/mtime in shrink case below
5468 */
5469 if (!shrink) {
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005470 inode->i_mtime = current_time(inode);
Eryu Guan911af572015-07-28 15:08:41 -04005471 inode->i_ctime = inode->i_mtime;
5472 }
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07005473
5474 if (shrink)
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08005475 ext4_fc_track_range(handle, inode,
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07005476 (attr->ia_size > 0 ? attr->ia_size - 1 : 0) >>
5477 inode->i_sb->s_blocksize_bits,
5478 (oldsize > 0 ? oldsize - 1 : 0) >>
5479 inode->i_sb->s_blocksize_bits);
5480 else
5481 ext4_fc_track_range(
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08005482 handle, inode,
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07005483 (oldsize > 0 ? oldsize - 1 : oldsize) >>
5484 inode->i_sb->s_blocksize_bits,
5485 (attr->ia_size > 0 ? attr->ia_size - 1 : 0) >>
5486 inode->i_sb->s_blocksize_bits);
5487
Jan Kara90e775b2013-08-17 10:09:31 -04005488 down_write(&EXT4_I(inode)->i_data_sem);
Jan Kara52083862013-08-17 10:07:17 -04005489 EXT4_I(inode)->i_disksize = attr->ia_size;
5490 rc = ext4_mark_inode_dirty(handle, inode);
5491 if (!error)
5492 error = rc;
Jan Kara90e775b2013-08-17 10:09:31 -04005493 /*
5494 * We have to update i_size under i_data_sem together
5495 * with i_disksize to avoid races with writeback code
5496 * running ext4_wb_update_i_disksize().
5497 */
5498 if (!error)
5499 i_size_write(inode, attr->ia_size);
5500 up_write(&EXT4_I(inode)->i_data_sem);
Jan Kara52083862013-08-17 10:07:17 -04005501 ext4_journal_stop(handle);
Jan Karab9c1c262019-05-30 11:56:23 -04005502 if (error)
5503 goto out_mmap_sem;
5504 if (!shrink) {
5505 pagecache_isize_extended(inode, oldsize,
5506 inode->i_size);
5507 } else if (ext4_should_journal_data(inode)) {
5508 ext4_wait_for_tail_page_commit(inode);
Jan Kara678aaf42008-07-11 19:27:31 -04005509 }
Jan Karad6320cb2014-10-01 21:49:46 -04005510 }
Ross Zwisler430657b2018-07-29 17:00:22 -04005511
Jan Kara52083862013-08-17 10:07:17 -04005512 /*
5513 * Truncate pagecache after we've waited for commit
5514 * in data=journal mode to make pages freeable.
5515 */
Ross Zwisler923ae0f2015-02-16 15:59:38 -08005516 truncate_pagecache(inode, inode->i_size);
Jan Karab9c1c262019-05-30 11:56:23 -04005517 /*
5518 * Call ext4_truncate() even if i_size didn't change to
5519 * truncate possible preallocated blocks.
5520 */
5521 if (attr->ia_size <= oldsize) {
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05005522 rc = ext4_truncate(inode);
5523 if (rc)
5524 error = rc;
5525 }
Jan Karab9c1c262019-05-30 11:56:23 -04005526out_mmap_sem:
Jan Karaea3d7202015-12-07 14:28:03 -05005527 up_write(&EXT4_I(inode)->i_mmap_sem);
Theodore Ts'o072bd7e2011-05-23 15:13:02 -04005528 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005529
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05005530 if (!error) {
Christian Brauner14f3db52021-01-21 14:19:57 +01005531 setattr_copy(mnt_userns, inode, attr);
Christoph Hellwig10257742010-06-04 11:30:02 +02005532 mark_inode_dirty(inode);
5533 }
5534
5535 /*
5536 * If the call to ext4_truncate failed to get a transaction handle at
5537 * all, we need to clean up the in-core orphan list manually.
5538 */
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04005539 if (orphan && inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07005540 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005541
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05005542 if (!error && (ia_valid & ATTR_MODE))
Christian Brauner14f3db52021-01-21 14:19:57 +01005543 rc = posix_acl_chmod(mnt_userns, inode, inode->i_mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005544
5545err_out:
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07005546 if (error)
5547 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005548 if (!error)
5549 error = rc;
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07005550 ext4_fc_stop_update(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005551 return error;
5552}
5553
Christian Brauner549c7292021-01-21 14:19:43 +01005554int ext4_getattr(struct user_namespace *mnt_userns, const struct path *path,
5555 struct kstat *stat, u32 request_mask, unsigned int query_flags)
Mingming Cao3e3398a2008-07-11 19:27:31 -04005556{
David Howells99652ea2017-03-31 18:31:56 +01005557 struct inode *inode = d_inode(path->dentry);
5558 struct ext4_inode *raw_inode;
5559 struct ext4_inode_info *ei = EXT4_I(inode);
5560 unsigned int flags;
Mingming Cao3e3398a2008-07-11 19:27:31 -04005561
Theodore Ts'od4c5e962019-11-28 22:26:51 -05005562 if ((request_mask & STATX_BTIME) &&
5563 EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime)) {
David Howells99652ea2017-03-31 18:31:56 +01005564 stat->result_mask |= STATX_BTIME;
5565 stat->btime.tv_sec = ei->i_crtime.tv_sec;
5566 stat->btime.tv_nsec = ei->i_crtime.tv_nsec;
5567 }
5568
5569 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
5570 if (flags & EXT4_APPEND_FL)
5571 stat->attributes |= STATX_ATTR_APPEND;
5572 if (flags & EXT4_COMPR_FL)
5573 stat->attributes |= STATX_ATTR_COMPRESSED;
5574 if (flags & EXT4_ENCRYPT_FL)
5575 stat->attributes |= STATX_ATTR_ENCRYPTED;
5576 if (flags & EXT4_IMMUTABLE_FL)
5577 stat->attributes |= STATX_ATTR_IMMUTABLE;
5578 if (flags & EXT4_NODUMP_FL)
5579 stat->attributes |= STATX_ATTR_NODUMP;
Eric Biggers1f607192019-10-29 13:41:39 -07005580 if (flags & EXT4_VERITY_FL)
5581 stat->attributes |= STATX_ATTR_VERITY;
David Howells99652ea2017-03-31 18:31:56 +01005582
David Howells3209f682017-03-31 18:32:17 +01005583 stat->attributes_mask |= (STATX_ATTR_APPEND |
5584 STATX_ATTR_COMPRESSED |
5585 STATX_ATTR_ENCRYPTED |
5586 STATX_ATTR_IMMUTABLE |
Eric Biggers1f607192019-10-29 13:41:39 -07005587 STATX_ATTR_NODUMP |
5588 STATX_ATTR_VERITY);
David Howells3209f682017-03-31 18:32:17 +01005589
Christian Brauner14f3db52021-01-21 14:19:57 +01005590 generic_fillattr(mnt_userns, inode, stat);
David Howells99652ea2017-03-31 18:31:56 +01005591 return 0;
5592}
5593
Christian Brauner549c7292021-01-21 14:19:43 +01005594int ext4_file_getattr(struct user_namespace *mnt_userns,
5595 const struct path *path, struct kstat *stat,
David Howells99652ea2017-03-31 18:31:56 +01005596 u32 request_mask, unsigned int query_flags)
5597{
5598 struct inode *inode = d_inode(path->dentry);
5599 u64 delalloc_blocks;
5600
Christian Brauner14f3db52021-01-21 14:19:57 +01005601 ext4_getattr(mnt_userns, path, stat, request_mask, query_flags);
Mingming Cao3e3398a2008-07-11 19:27:31 -04005602
5603 /*
Andreas Dilger9206c562013-11-11 22:38:12 -05005604 * If there is inline data in the inode, the inode will normally not
5605 * have data blocks allocated (it may have an external xattr block).
5606 * Report at least one sector for such files, so tools like tar, rsync,
Theodore Ts'od67d64f2017-03-25 17:33:31 -04005607 * others don't incorrectly think the file is completely sparse.
Andreas Dilger9206c562013-11-11 22:38:12 -05005608 */
5609 if (unlikely(ext4_has_inline_data(inode)))
5610 stat->blocks += (stat->size + 511) >> 9;
5611
5612 /*
Mingming Cao3e3398a2008-07-11 19:27:31 -04005613 * We can't update i_blocks if the block allocation is delayed
5614 * otherwise in the case of system crash before the real block
5615 * allocation is done, we will have i_blocks inconsistent with
5616 * on-disk file blocks.
5617 * We always keep i_blocks updated together with real
5618 * allocation. But to not confuse with user, stat
5619 * will return the blocks that include the delayed allocation
5620 * blocks for this file.
5621 */
Tao Ma96607552012-05-31 22:54:16 -04005622 delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
Andreas Dilger9206c562013-11-11 22:38:12 -05005623 EXT4_I(inode)->i_reserved_data_blocks);
5624 stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9);
Mingming Cao3e3398a2008-07-11 19:27:31 -04005625 return 0;
5626}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005627
Jan Karafffb2732013-06-04 13:01:11 -04005628static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
5629 int pextents)
Mingming Caoa02908f2008-08-19 22:16:07 -04005630{
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005631 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Jan Karafffb2732013-06-04 13:01:11 -04005632 return ext4_ind_trans_blocks(inode, lblocks);
5633 return ext4_ext_index_trans_blocks(inode, pextents);
Mingming Caoa02908f2008-08-19 22:16:07 -04005634}
Theodore Ts'oac51d832008-11-06 16:49:36 -05005635
Mingming Caoa02908f2008-08-19 22:16:07 -04005636/*
5637 * Account for index blocks, block groups bitmaps and block group
5638 * descriptor blocks if modify datablocks and index blocks
5639 * worse case, the indexs blocks spread over different block groups
5640 *
5641 * If datablocks are discontiguous, they are possible to spread over
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005642 * different block groups too. If they are contiguous, with flexbg,
Mingming Caoa02908f2008-08-19 22:16:07 -04005643 * they could still across block group boundary.
5644 *
5645 * Also account for superblock, inode, quota and xattr blocks
5646 */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04005647static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
Jan Karafffb2732013-06-04 13:01:11 -04005648 int pextents)
Mingming Caoa02908f2008-08-19 22:16:07 -04005649{
Theodore Ts'o8df96752009-05-01 08:50:38 -04005650 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
5651 int gdpblocks;
Mingming Caoa02908f2008-08-19 22:16:07 -04005652 int idxblocks;
5653 int ret = 0;
5654
5655 /*
Jan Karafffb2732013-06-04 13:01:11 -04005656 * How many index blocks need to touch to map @lblocks logical blocks
5657 * to @pextents physical extents?
Mingming Caoa02908f2008-08-19 22:16:07 -04005658 */
Jan Karafffb2732013-06-04 13:01:11 -04005659 idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents);
Mingming Caoa02908f2008-08-19 22:16:07 -04005660
5661 ret = idxblocks;
5662
5663 /*
5664 * Now let's see how many group bitmaps and group descriptors need
5665 * to account
5666 */
Jan Karafffb2732013-06-04 13:01:11 -04005667 groups = idxblocks + pextents;
Mingming Caoa02908f2008-08-19 22:16:07 -04005668 gdpblocks = groups;
Theodore Ts'o8df96752009-05-01 08:50:38 -04005669 if (groups > ngroups)
5670 groups = ngroups;
Mingming Caoa02908f2008-08-19 22:16:07 -04005671 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
5672 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
5673
5674 /* bitmaps and block group descriptor blocks */
5675 ret += groups + gdpblocks;
5676
5677 /* Blocks for super block, inode, quota and xattr blocks */
5678 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005679
5680 return ret;
5681}
5682
5683/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005684 * Calculate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04005685 * the modification of a single pages into a single transaction,
5686 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04005687 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04005688 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04005689 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04005690 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04005691 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04005692 */
5693int ext4_writepage_trans_blocks(struct inode *inode)
5694{
5695 int bpp = ext4_journal_blocks_per_page(inode);
5696 int ret;
5697
Jan Karafffb2732013-06-04 13:01:11 -04005698 ret = ext4_meta_trans_blocks(inode, bpp, bpp);
Mingming Caoa02908f2008-08-19 22:16:07 -04005699
5700 /* Account for data blocks for journalled mode */
5701 if (ext4_should_journal_data(inode))
5702 ret += bpp;
5703 return ret;
5704}
Mingming Caof3bd1f32008-08-19 22:16:03 -04005705
5706/*
5707 * Calculate the journal credits for a chunk of data modification.
5708 *
5709 * This is called from DIO, fallocate or whoever calling
Eric Sandeen79e83032010-07-27 11:56:07 -04005710 * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
Mingming Caof3bd1f32008-08-19 22:16:03 -04005711 *
5712 * journal buffers for data blocks are not included here, as DIO
5713 * and fallocate do no need to journal data buffers.
5714 */
5715int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
5716{
5717 return ext4_meta_trans_blocks(inode, nrblocks, 1);
5718}
5719
Mingming Caoa02908f2008-08-19 22:16:07 -04005720/*
Mingming Cao617ba132006-10-11 01:20:53 -07005721 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005722 * Give this, we know that the caller already has write access to iloc->bh.
5723 */
Mingming Cao617ba132006-10-11 01:20:53 -07005724int ext4_mark_iloc_dirty(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04005725 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005726{
5727 int err = 0;
5728
Vasily Averina6758302018-11-06 16:49:50 -05005729 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) {
5730 put_bh(iloc->bh);
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05005731 return -EIO;
Vasily Averina6758302018-11-06 16:49:50 -05005732 }
Harshad Shirwadkara80f7fc2020-11-05 19:58:53 -08005733 ext4_fc_track_inode(handle, inode);
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07005734
Theodore Ts'oc64db502012-03-02 12:23:11 -05005735 if (IS_I_VERSION(inode))
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005736 inode_inc_iversion(inode);
5737
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005738 /* the do_update_inode consumes one bh->b_count */
5739 get_bh(iloc->bh);
5740
Mingming Caodab291a2006-10-11 01:21:01 -07005741 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Frank Mayhar830156c2009-09-29 10:07:47 -04005742 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005743 put_bh(iloc->bh);
5744 return err;
5745}
5746
5747/*
5748 * On success, We end up with an outstanding reference count against
5749 * iloc->bh. This _must_ be cleaned up later.
5750 */
5751
5752int
Mingming Cao617ba132006-10-11 01:20:53 -07005753ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
5754 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005755{
Frank Mayhar03901312009-01-07 00:06:22 -05005756 int err;
5757
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05005758 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5759 return -EIO;
5760
Frank Mayhar03901312009-01-07 00:06:22 -05005761 err = ext4_get_inode_loc(inode, iloc);
5762 if (!err) {
5763 BUFFER_TRACE(iloc->bh, "get_write_access");
Jan Kara188c2992021-08-16 11:57:04 +02005764 err = ext4_journal_get_write_access(handle, inode->i_sb,
5765 iloc->bh, EXT4_JTR_NONE);
Frank Mayhar03901312009-01-07 00:06:22 -05005766 if (err) {
5767 brelse(iloc->bh);
5768 iloc->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005769 }
5770 }
Mingming Cao617ba132006-10-11 01:20:53 -07005771 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005772 return err;
5773}
5774
Miao Xiec03b45b2017-08-06 01:00:49 -04005775static int __ext4_expand_extra_isize(struct inode *inode,
5776 unsigned int new_extra_isize,
5777 struct ext4_iloc *iloc,
5778 handle_t *handle, int *no_expand)
5779{
5780 struct ext4_inode *raw_inode;
5781 struct ext4_xattr_ibody_header *header;
Theodore Ts'o4ea99932019-11-07 21:43:41 -05005782 unsigned int inode_size = EXT4_INODE_SIZE(inode->i_sb);
5783 struct ext4_inode_info *ei = EXT4_I(inode);
Miao Xiec03b45b2017-08-06 01:00:49 -04005784 int error;
5785
Theodore Ts'o4ea99932019-11-07 21:43:41 -05005786 /* this was checked at iget time, but double check for good measure */
5787 if ((EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > inode_size) ||
5788 (ei->i_extra_isize & 3)) {
5789 EXT4_ERROR_INODE(inode, "bad extra_isize %u (inode size %u)",
5790 ei->i_extra_isize,
5791 EXT4_INODE_SIZE(inode->i_sb));
5792 return -EFSCORRUPTED;
5793 }
5794 if ((new_extra_isize < ei->i_extra_isize) ||
5795 (new_extra_isize < 4) ||
5796 (new_extra_isize > inode_size - EXT4_GOOD_OLD_INODE_SIZE))
5797 return -EINVAL; /* Should never happen */
5798
Miao Xiec03b45b2017-08-06 01:00:49 -04005799 raw_inode = ext4_raw_inode(iloc);
5800
5801 header = IHDR(inode, raw_inode);
5802
5803 /* No extended attributes present */
5804 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
5805 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
5806 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE +
5807 EXT4_I(inode)->i_extra_isize, 0,
5808 new_extra_isize - EXT4_I(inode)->i_extra_isize);
5809 EXT4_I(inode)->i_extra_isize = new_extra_isize;
5810 return 0;
5811 }
5812
5813 /* try to expand with EAs present */
5814 error = ext4_expand_extra_isize_ea(inode, new_extra_isize,
5815 raw_inode, handle);
5816 if (error) {
5817 /*
5818 * Inode size expansion failed; don't try again
5819 */
5820 *no_expand = 1;
5821 }
5822
5823 return error;
5824}
5825
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005826/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005827 * Expand an inode by new_extra_isize bytes.
5828 * Returns 0 on success or negative error number on failure.
5829 */
Miao Xiecf0a5e82017-08-06 00:40:01 -04005830static int ext4_try_to_expand_extra_isize(struct inode *inode,
5831 unsigned int new_extra_isize,
5832 struct ext4_iloc iloc,
5833 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005834{
Miao Xie3b10fdc2017-08-06 00:27:38 -04005835 int no_expand;
5836 int error;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005837
Miao Xiecf0a5e82017-08-06 00:40:01 -04005838 if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND))
5839 return -EOVERFLOW;
5840
5841 /*
5842 * In nojournal mode, we can immediately attempt to expand
5843 * the inode. When journaled, we first need to obtain extra
5844 * buffer credits since we may write into the EA block
5845 * with this same handle. If journal_extend fails, then it will
5846 * only result in a minor loss of functionality for that inode.
5847 * If this is felt to be critical, then e2fsck should be run to
5848 * force a large enough s_min_extra_isize.
5849 */
Jan Kara6cb367c2019-11-05 17:44:14 +01005850 if (ext4_journal_extend(handle,
Jan Kara83448bd2019-11-05 17:44:29 +01005851 EXT4_DATA_TRANS_BLOCKS(inode->i_sb), 0) != 0)
Miao Xiecf0a5e82017-08-06 00:40:01 -04005852 return -ENOSPC;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005853
Miao Xie3b10fdc2017-08-06 00:27:38 -04005854 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
Miao Xiecf0a5e82017-08-06 00:40:01 -04005855 return -EBUSY;
Miao Xie3b10fdc2017-08-06 00:27:38 -04005856
Miao Xiec03b45b2017-08-06 01:00:49 -04005857 error = __ext4_expand_extra_isize(inode, new_extra_isize, &iloc,
5858 handle, &no_expand);
Miao Xie3b10fdc2017-08-06 00:27:38 -04005859 ext4_write_unlock_xattr(inode, &no_expand);
Miao Xiecf0a5e82017-08-06 00:40:01 -04005860
Miao Xie3b10fdc2017-08-06 00:27:38 -04005861 return error;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005862}
5863
Miao Xiec03b45b2017-08-06 01:00:49 -04005864int ext4_expand_extra_isize(struct inode *inode,
5865 unsigned int new_extra_isize,
5866 struct ext4_iloc *iloc)
5867{
5868 handle_t *handle;
5869 int no_expand;
5870 int error, rc;
5871
5872 if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
5873 brelse(iloc->bh);
5874 return -EOVERFLOW;
5875 }
5876
5877 handle = ext4_journal_start(inode, EXT4_HT_INODE,
5878 EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
5879 if (IS_ERR(handle)) {
5880 error = PTR_ERR(handle);
5881 brelse(iloc->bh);
5882 return error;
5883 }
5884
5885 ext4_write_lock_xattr(inode, &no_expand);
5886
zhangyi (F)ddccb6d2019-02-21 11:29:10 -05005887 BUFFER_TRACE(iloc->bh, "get_write_access");
Jan Kara188c2992021-08-16 11:57:04 +02005888 error = ext4_journal_get_write_access(handle, inode->i_sb, iloc->bh,
5889 EXT4_JTR_NONE);
Miao Xiec03b45b2017-08-06 01:00:49 -04005890 if (error) {
5891 brelse(iloc->bh);
Dan Carpenter7f420d642019-12-13 21:50:11 +03005892 goto out_unlock;
Miao Xiec03b45b2017-08-06 01:00:49 -04005893 }
5894
5895 error = __ext4_expand_extra_isize(inode, new_extra_isize, iloc,
5896 handle, &no_expand);
5897
5898 rc = ext4_mark_iloc_dirty(handle, inode, iloc);
5899 if (!error)
5900 error = rc;
5901
Dan Carpenter7f420d642019-12-13 21:50:11 +03005902out_unlock:
Miao Xiec03b45b2017-08-06 01:00:49 -04005903 ext4_write_unlock_xattr(inode, &no_expand);
Miao Xiec03b45b2017-08-06 01:00:49 -04005904 ext4_journal_stop(handle);
5905 return error;
5906}
5907
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005908/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005909 * What we do here is to mark the in-core inode as clean with respect to inode
5910 * dirtiness (it may still be data-dirty).
5911 * This means that the in-core inode may be reaped by prune_icache
5912 * without having to perform any I/O. This is a very good thing,
5913 * because *any* task may call prune_icache - even ones which
5914 * have a transaction open against a different journal.
5915 *
5916 * Is this cheating? Not really. Sure, we haven't written the
5917 * inode out, but prune_icache isn't a user-visible syncing function.
5918 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5919 * we start and wait on commits.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005920 */
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07005921int __ext4_mark_inode_dirty(handle_t *handle, struct inode *inode,
5922 const char *func, unsigned int line)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005923{
Mingming Cao617ba132006-10-11 01:20:53 -07005924 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005925 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Miao Xiecf0a5e82017-08-06 00:40:01 -04005926 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005927
5928 might_sleep();
Theodore Ts'o7ff9c072010-11-08 13:51:33 -05005929 trace_ext4_mark_inode_dirty(inode, _RET_IP_);
Mingming Cao617ba132006-10-11 01:20:53 -07005930 err = ext4_reserve_inode_write(handle, inode, &iloc);
Eryu Guan5e1021f2016-03-12 21:40:32 -05005931 if (err)
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07005932 goto out;
Miao Xiecf0a5e82017-08-06 00:40:01 -04005933
5934 if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize)
5935 ext4_try_to_expand_extra_isize(inode, sbi->s_want_extra_isize,
5936 iloc, handle);
5937
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07005938 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
5939out:
5940 if (unlikely(err))
5941 ext4_error_inode_err(inode, func, line, 0, err,
5942 "mark_inode_dirty error");
5943 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005944}
5945
5946/*
Mingming Cao617ba132006-10-11 01:20:53 -07005947 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005948 *
5949 * We're really interested in the case where a file is being extended.
5950 * i_size has been changed by generic_commit_write() and we thus need
5951 * to include the updated inode in the current transaction.
5952 *
Christoph Hellwig5dd40562010-03-03 09:05:00 -05005953 * Also, dquot_alloc_block() will always dirty the inode when blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005954 * are allocated to the file.
5955 *
5956 * If the inode is marked synchronous, we don't honour that here - doing
5957 * so would cause a commit on atime updates, which we don't bother doing.
5958 * We handle synchronous inodes at the highest possible level.
5959 */
Christoph Hellwigaa385722011-05-27 06:53:02 -04005960void ext4_dirty_inode(struct inode *inode, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005961{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005962 handle_t *handle;
5963
Theodore Ts'o9924a922013-02-08 21:59:22 -05005964 handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005965 if (IS_ERR(handle))
Eric Biggerse2728c52021-01-12 11:02:47 -08005966 return;
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005967 ext4_mark_inode_dirty(handle, inode);
Mingming Cao617ba132006-10-11 01:20:53 -07005968 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005969}
5970
Mingming Cao617ba132006-10-11 01:20:53 -07005971int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005972{
5973 journal_t *journal;
5974 handle_t *handle;
5975 int err;
Daeho Jeongc8585c62016-04-25 23:22:35 -04005976 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005977
5978 /*
5979 * We have to be very careful here: changing a data block's
5980 * journaling status dynamically is dangerous. If we write a
5981 * data block to the journal, change the status and then delete
5982 * that block, we risk forgetting to revoke the old log record
5983 * from the journal and so a subsequent replay can corrupt data.
5984 * So, first we make sure that the journal is empty and that
5985 * nobody is changing anything.
5986 */
5987
Mingming Cao617ba132006-10-11 01:20:53 -07005988 journal = EXT4_JOURNAL(inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005989 if (!journal)
5990 return 0;
Dave Hansend6995942007-07-18 08:33:51 -04005991 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005992 return -EROFS;
5993
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04005994 /* Wait for all existing dio workers */
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04005995 inode_dio_wait(inode);
5996
Daeho Jeong4c546592016-04-25 23:21:00 -04005997 /*
5998 * Before flushing the journal and switching inode's aops, we have
5999 * to flush all dirty data the inode has. There can be outstanding
6000 * delayed allocations, there can be unwritten extents created by
6001 * fallocate or buffered writes in dioread_nolock mode covered by
6002 * dirty data which can be converted only after flushing the dirty
6003 * data (and journalled aops don't know how to handle these cases).
6004 */
6005 if (val) {
6006 down_write(&EXT4_I(inode)->i_mmap_sem);
6007 err = filemap_write_and_wait(inode->i_mapping);
6008 if (err < 0) {
6009 up_write(&EXT4_I(inode)->i_mmap_sem);
Daeho Jeong4c546592016-04-25 23:21:00 -04006010 return err;
6011 }
6012 }
6013
Eric Biggersbbd55932020-02-19 10:30:46 -08006014 percpu_down_write(&sbi->s_writepages_rwsem);
Mingming Caodab291a2006-10-11 01:21:01 -07006015 jbd2_journal_lock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006016
6017 /*
6018 * OK, there are no updates running now, and all cached data is
6019 * synced to disk. We are now in a completely consistent state
6020 * which doesn't have anything in the journal, and we know that
6021 * no filesystem updates are running, so it is safe to modify
6022 * the inode's in-core data-journaling state flag now.
6023 */
6024
6025 if (val)
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04006026 ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Yongqiang Yang5872dda2011-12-28 13:55:51 -05006027 else {
Leah Rumancik01d5d962021-05-18 15:13:25 +00006028 err = jbd2_journal_flush(journal, 0);
Jan Kara4f879ca2014-10-30 10:53:17 -04006029 if (err < 0) {
6030 jbd2_journal_unlock_updates(journal);
Eric Biggersbbd55932020-02-19 10:30:46 -08006031 percpu_up_write(&sbi->s_writepages_rwsem);
Jan Kara4f879ca2014-10-30 10:53:17 -04006032 return err;
6033 }
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04006034 ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Yongqiang Yang5872dda2011-12-28 13:55:51 -05006035 }
Mingming Cao617ba132006-10-11 01:20:53 -07006036 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006037
Mingming Caodab291a2006-10-11 01:21:01 -07006038 jbd2_journal_unlock_updates(journal);
Eric Biggersbbd55932020-02-19 10:30:46 -08006039 percpu_up_write(&sbi->s_writepages_rwsem);
Daeho Jeongc8585c62016-04-25 23:22:35 -04006040
Daeho Jeong4c546592016-04-25 23:21:00 -04006041 if (val)
6042 up_write(&EXT4_I(inode)->i_mmap_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006043
6044 /* Finally we can mark the inode as dirty. */
6045
Theodore Ts'o9924a922013-02-08 21:59:22 -05006046 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006047 if (IS_ERR(handle))
6048 return PTR_ERR(handle);
6049
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07006050 ext4_fc_mark_ineligible(inode->i_sb,
6051 EXT4_FC_REASON_JOURNAL_FLAG_CHANGE);
Mingming Cao617ba132006-10-11 01:20:53 -07006052 err = ext4_mark_inode_dirty(handle, inode);
Frank Mayhar03901312009-01-07 00:06:22 -05006053 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07006054 ext4_journal_stop(handle);
6055 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006056
6057 return err;
6058}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04006059
Jan Kara188c2992021-08-16 11:57:04 +02006060static int ext4_bh_unmapped(handle_t *handle, struct inode *inode,
6061 struct buffer_head *bh)
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04006062{
6063 return !buffer_mapped(bh);
6064}
6065
Souptick Joarder401b25a2018-10-02 22:20:50 -04006066vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf)
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04006067{
Dave Jiang11bac802017-02-24 14:56:41 -08006068 struct vm_area_struct *vma = vmf->vma;
Nick Pigginc2ec1752009-03-31 15:23:21 -07006069 struct page *page = vmf->page;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04006070 loff_t size;
6071 unsigned long len;
Souptick Joarder401b25a2018-10-02 22:20:50 -04006072 int err;
6073 vm_fault_t ret;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04006074 struct file *file = vma->vm_file;
Al Viro496ad9a2013-01-23 17:07:38 -05006075 struct inode *inode = file_inode(file);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04006076 struct address_space *mapping = inode->i_mapping;
Jan Kara9ea7df52011-06-24 14:29:41 -04006077 handle_t *handle;
6078 get_block_t *get_block;
6079 int retries = 0;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04006080
Theodore Ts'o02b016c2019-06-09 22:04:33 -04006081 if (unlikely(IS_IMMUTABLE(inode)))
6082 return VM_FAULT_SIGBUS;
6083
Jan Kara8e8ad8a2012-06-12 16:20:38 +02006084 sb_start_pagefault(inode->i_sb);
Theodore Ts'o041bbb6d2012-09-30 23:04:56 -04006085 file_update_time(vma->vm_file);
Jan Karaea3d7202015-12-07 14:28:03 -05006086
6087 down_read(&EXT4_I(inode)->i_mmap_sem);
Eric Biggers7b4cc972017-04-30 00:10:50 -04006088
Souptick Joarder401b25a2018-10-02 22:20:50 -04006089 err = ext4_convert_inline_data(inode);
6090 if (err)
Eric Biggers7b4cc972017-04-30 00:10:50 -04006091 goto out_ret;
6092
Mauricio Faria de Oliveira64a9f142020-10-05 21:48:40 -03006093 /*
6094 * On data journalling we skip straight to the transaction handle:
6095 * there's no delalloc; page truncated will be checked later; the
6096 * early return w/ all buffers mapped (calculates size/len) can't
6097 * be used; and there's no dioread_nolock, so only ext4_get_block.
6098 */
6099 if (ext4_should_journal_data(inode))
6100 goto retry_alloc;
6101
Jan Kara9ea7df52011-06-24 14:29:41 -04006102 /* Delalloc case is easy... */
6103 if (test_opt(inode->i_sb, DELALLOC) &&
Jan Kara9ea7df52011-06-24 14:29:41 -04006104 !ext4_nonda_switch(inode->i_sb)) {
6105 do {
Souptick Joarder401b25a2018-10-02 22:20:50 -04006106 err = block_page_mkwrite(vma, vmf,
Jan Kara9ea7df52011-06-24 14:29:41 -04006107 ext4_da_get_block_prep);
Souptick Joarder401b25a2018-10-02 22:20:50 -04006108 } while (err == -ENOSPC &&
Jan Kara9ea7df52011-06-24 14:29:41 -04006109 ext4_should_retry_alloc(inode->i_sb, &retries));
6110 goto out_ret;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04006111 }
Darrick J. Wong0e499892011-05-18 13:55:20 -04006112
6113 lock_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04006114 size = i_size_read(inode);
6115 /* Page got truncated from under us? */
6116 if (page->mapping != mapping || page_offset(page) > size) {
6117 unlock_page(page);
6118 ret = VM_FAULT_NOPAGE;
6119 goto out;
Darrick J. Wong0e499892011-05-18 13:55:20 -04006120 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04006121
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006122 if (page->index == size >> PAGE_SHIFT)
6123 len = size & ~PAGE_MASK;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04006124 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006125 len = PAGE_SIZE;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04006126 /*
Jan Kara9ea7df52011-06-24 14:29:41 -04006127 * Return if we have all the buffers mapped. This avoids the need to do
6128 * journal_start/journal_stop which can block and take a long time
Mauricio Faria de Oliveira64a9f142020-10-05 21:48:40 -03006129 *
6130 * This cannot be done for data journalling, as we have to add the
6131 * inode to the transaction's list to writeprotect pages on commit.
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04006132 */
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04006133 if (page_has_buffers(page)) {
Jan Kara188c2992021-08-16 11:57:04 +02006134 if (!ext4_walk_page_buffers(NULL, inode, page_buffers(page),
Tao Maf19d5872012-12-10 14:05:51 -05006135 0, len, NULL,
6136 ext4_bh_unmapped)) {
Jan Kara9ea7df52011-06-24 14:29:41 -04006137 /* Wait so that we don't change page under IO */
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08006138 wait_for_stable_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04006139 ret = VM_FAULT_LOCKED;
6140 goto out;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04006141 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04006142 }
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04006143 unlock_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04006144 /* OK, we need to fill the hole... */
6145 if (ext4_should_dioread_nolock(inode))
Jan Kara705965b2016-03-08 23:08:10 -05006146 get_block = ext4_get_block_unwritten;
Jan Kara9ea7df52011-06-24 14:29:41 -04006147 else
6148 get_block = ext4_get_block;
6149retry_alloc:
Theodore Ts'o9924a922013-02-08 21:59:22 -05006150 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
6151 ext4_writepage_trans_blocks(inode));
Jan Kara9ea7df52011-06-24 14:29:41 -04006152 if (IS_ERR(handle)) {
Nick Pigginc2ec1752009-03-31 15:23:21 -07006153 ret = VM_FAULT_SIGBUS;
Jan Kara9ea7df52011-06-24 14:29:41 -04006154 goto out;
6155 }
Mauricio Faria de Oliveira64a9f142020-10-05 21:48:40 -03006156 /*
6157 * Data journalling can't use block_page_mkwrite() because it
6158 * will set_buffer_dirty() before do_journal_get_write_access()
6159 * thus might hit warning messages for dirty metadata buffers.
6160 */
6161 if (!ext4_should_journal_data(inode)) {
6162 err = block_page_mkwrite(vma, vmf, get_block);
6163 } else {
6164 lock_page(page);
6165 size = i_size_read(inode);
6166 /* Page got truncated from under us? */
6167 if (page->mapping != mapping || page_offset(page) > size) {
Mauricio Faria de Oliveira64a9f142020-10-05 21:48:40 -03006168 ret = VM_FAULT_NOPAGE;
Mauricio Faria de Oliveiraafb585a2020-10-05 21:48:41 -03006169 goto out_error;
Jan Kara9ea7df52011-06-24 14:29:41 -04006170 }
Mauricio Faria de Oliveira64a9f142020-10-05 21:48:40 -03006171
6172 if (page->index == size >> PAGE_SHIFT)
6173 len = size & ~PAGE_MASK;
6174 else
6175 len = PAGE_SIZE;
6176
6177 err = __block_write_begin(page, 0, len, ext4_get_block);
6178 if (!err) {
Mauricio Faria de Oliveiraafb585a2020-10-05 21:48:41 -03006179 ret = VM_FAULT_SIGBUS;
Jan Kara188c2992021-08-16 11:57:04 +02006180 if (ext4_walk_page_buffers(handle, inode,
6181 page_buffers(page), 0, len, NULL,
6182 do_journal_get_write_access))
Mauricio Faria de Oliveiraafb585a2020-10-05 21:48:41 -03006183 goto out_error;
Jan Kara188c2992021-08-16 11:57:04 +02006184 if (ext4_walk_page_buffers(handle, inode,
6185 page_buffers(page), 0, len, NULL,
6186 write_end_fn))
Mauricio Faria de Oliveiraafb585a2020-10-05 21:48:41 -03006187 goto out_error;
Jan Karab5b18162020-10-27 14:27:51 +01006188 if (ext4_jbd2_inode_add_write(handle, inode,
6189 page_offset(page), len))
Mauricio Faria de Oliveiraafb585a2020-10-05 21:48:41 -03006190 goto out_error;
Mauricio Faria de Oliveira64a9f142020-10-05 21:48:40 -03006191 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
6192 } else {
6193 unlock_page(page);
6194 }
Jan Kara9ea7df52011-06-24 14:29:41 -04006195 }
6196 ext4_journal_stop(handle);
Souptick Joarder401b25a2018-10-02 22:20:50 -04006197 if (err == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
Jan Kara9ea7df52011-06-24 14:29:41 -04006198 goto retry_alloc;
6199out_ret:
Souptick Joarder401b25a2018-10-02 22:20:50 -04006200 ret = block_page_mkwrite_return(err);
Jan Kara9ea7df52011-06-24 14:29:41 -04006201out:
Jan Karaea3d7202015-12-07 14:28:03 -05006202 up_read(&EXT4_I(inode)->i_mmap_sem);
Jan Kara8e8ad8a2012-06-12 16:20:38 +02006203 sb_end_pagefault(inode->i_sb);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04006204 return ret;
Mauricio Faria de Oliveiraafb585a2020-10-05 21:48:41 -03006205out_error:
6206 unlock_page(page);
6207 ext4_journal_stop(handle);
6208 goto out;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04006209}
Jan Karaea3d7202015-12-07 14:28:03 -05006210
Souptick Joarder401b25a2018-10-02 22:20:50 -04006211vm_fault_t ext4_filemap_fault(struct vm_fault *vmf)
Jan Karaea3d7202015-12-07 14:28:03 -05006212{
Dave Jiang11bac802017-02-24 14:56:41 -08006213 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarder401b25a2018-10-02 22:20:50 -04006214 vm_fault_t ret;
Jan Karaea3d7202015-12-07 14:28:03 -05006215
6216 down_read(&EXT4_I(inode)->i_mmap_sem);
Souptick Joarder401b25a2018-10-02 22:20:50 -04006217 ret = filemap_fault(vmf);
Jan Karaea3d7202015-12-07 14:28:03 -05006218 up_read(&EXT4_I(inode)->i_mmap_sem);
6219
Souptick Joarder401b25a2018-10-02 22:20:50 -04006220 return ret;
Jan Karaea3d7202015-12-07 14:28:03 -05006221}