blob: 0a52f98512d7895dbbb3ea99306131bbf224e802 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002/*
Mingming Cao617ba132006-10-11 01:20:53 -07003 * linux/fs/ext4/inode.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004 *
5 * Copyright (C) 1992, 1993, 1994, 1995
6 * Remy Card (card@masi.ibp.fr)
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
9 *
10 * from
11 *
12 * linux/fs/minix/inode.c
13 *
14 * Copyright (C) 1991, 1992 Linus Torvalds
15 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -070016 * 64-bit file support on 64-bit platforms by Jakub Jelinek
17 * (jj@sunsite.ms.mff.cuni.cz)
18 *
Mingming Cao617ba132006-10-11 01:20:53 -070019 * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000
Dave Kleikampac27a0e2006-10-11 01:20:50 -070020 */
21
Dave Kleikampac27a0e2006-10-11 01:20:50 -070022#include <linux/fs.h>
23#include <linux/time.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070024#include <linux/highuid.h>
25#include <linux/pagemap.h>
Matthew Wilcoxc94c2ac2015-09-08 14:58:40 -070026#include <linux/dax.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070027#include <linux/quotaops.h>
28#include <linux/string.h>
29#include <linux/buffer_head.h>
30#include <linux/writeback.h>
Alex Tomas64769242008-07-11 19:27:31 -040031#include <linux/pagevec.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070032#include <linux/mpage.h>
Duane Griffine83c1392008-12-19 20:47:15 +000033#include <linux/namei.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070034#include <linux/uio.h>
35#include <linux/bio.h>
Mingming Cao4c0425f2009-09-28 15:48:41 -040036#include <linux/workqueue.h>
Jiaying Zhang744692d2010-03-04 16:14:02 -050037#include <linux/kernel.h>
Andrew Morton6db26ff2011-01-12 16:59:13 -080038#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Theodore Ts'o00a1a052014-03-30 10:20:01 -040040#include <linux/bitops.h>
Jan Kara364443c2016-11-20 17:36:06 -050041#include <linux/iomap.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050042#include <linux/iversion.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040043
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040044#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070045#include "xattr.h"
46#include "acl.h"
Theodore Ts'o9f125d62011-06-27 19:16:04 -040047#include "truncate.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070048
Theodore Ts'o9bffad12009-06-17 11:48:11 -040049#include <trace/events/ext4.h>
50
Darrick J. Wong814525f2012-04-29 18:31:10 -040051static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw,
52 struct ext4_inode_info *ei)
53{
54 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Darrick J. Wong814525f2012-04-29 18:31:10 -040055 __u32 csum;
Daeho Jeongb47820e2016-07-03 17:51:39 -040056 __u16 dummy_csum = 0;
57 int offset = offsetof(struct ext4_inode, i_checksum_lo);
58 unsigned int csum_size = sizeof(dummy_csum);
Darrick J. Wong814525f2012-04-29 18:31:10 -040059
Daeho Jeongb47820e2016-07-03 17:51:39 -040060 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, offset);
61 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, csum_size);
62 offset += csum_size;
63 csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset,
64 EXT4_GOOD_OLD_INODE_SIZE - offset);
65
66 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
67 offset = offsetof(struct ext4_inode, i_checksum_hi);
68 csum = ext4_chksum(sbi, csum, (__u8 *)raw +
69 EXT4_GOOD_OLD_INODE_SIZE,
70 offset - EXT4_GOOD_OLD_INODE_SIZE);
71 if (EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) {
72 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum,
73 csum_size);
74 offset += csum_size;
Daeho Jeongb47820e2016-07-03 17:51:39 -040075 }
Daeho Jeong05ac5aa2016-12-01 11:49:12 -050076 csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset,
77 EXT4_INODE_SIZE(inode->i_sb) - offset);
Darrick J. Wong814525f2012-04-29 18:31:10 -040078 }
79
Darrick J. Wong814525f2012-04-29 18:31:10 -040080 return csum;
81}
82
83static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw,
84 struct ext4_inode_info *ei)
85{
86 __u32 provided, calculated;
87
88 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
89 cpu_to_le32(EXT4_OS_LINUX) ||
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -040090 !ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong814525f2012-04-29 18:31:10 -040091 return 1;
92
93 provided = le16_to_cpu(raw->i_checksum_lo);
94 calculated = ext4_inode_csum(inode, raw, ei);
95 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
96 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
97 provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16;
98 else
99 calculated &= 0xFFFF;
100
101 return provided == calculated;
102}
103
104static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
105 struct ext4_inode_info *ei)
106{
107 __u32 csum;
108
109 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
110 cpu_to_le32(EXT4_OS_LINUX) ||
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400111 !ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong814525f2012-04-29 18:31:10 -0400112 return;
113
114 csum = ext4_inode_csum(inode, raw, ei);
115 raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF);
116 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
117 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
118 raw->i_checksum_hi = cpu_to_le16(csum >> 16);
119}
120
Jan Kara678aaf42008-07-11 19:27:31 -0400121static inline int ext4_begin_ordered_truncate(struct inode *inode,
122 loff_t new_size)
123{
Theodore Ts'o7ff9c072010-11-08 13:51:33 -0500124 trace_ext4_begin_ordered_truncate(inode, new_size);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -0500125 /*
126 * If jinode is zero, then we never opened the file for
127 * writing, so there's no need to call
128 * jbd2_journal_begin_ordered_truncate() since there's no
129 * outstanding writes we need to flush.
130 */
131 if (!EXT4_I(inode)->jinode)
132 return 0;
133 return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
134 EXT4_I(inode)->jinode,
135 new_size);
Jan Kara678aaf42008-07-11 19:27:31 -0400136}
137
Lukas Czernerd47992f2013-05-21 23:17:23 -0400138static void ext4_invalidatepage(struct page *page, unsigned int offset,
139 unsigned int length);
Theodore Ts'ocb20d512010-10-27 21:30:09 -0400140static int __ext4_journalled_writepage(struct page *page, unsigned int len);
141static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
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;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700178
Theodore Ts'o7ff9c072010-11-08 13:51:33 -0500179 trace_ext4_evict_inode(inode);
Jiaying Zhang2581fdc2011-08-13 12:17:13 -0400180
Al Viro0930fcc2010-06-07 13:16:22 -0400181 if (inode->i_nlink) {
Jan Kara2d859db2011-07-26 09:07:11 -0400182 /*
183 * When journalling data dirty buffers are tracked only in the
184 * journal. So although mm thinks everything is clean and
185 * ready for reaping the inode might still have some pages to
186 * write in the running transaction or waiting to be
187 * checkpointed. Thus calling jbd2_journal_invalidatepage()
188 * (via truncate_inode_pages()) to discard these buffers can
189 * cause data loss. Also even if we did not discard these
190 * buffers, we would have no way to find them after the inode
191 * is reaped and thus user could see stale data if he tries to
192 * read them before the transaction is checkpointed. So be
193 * careful and force everything to disk here... We use
194 * ei->i_datasync_tid to store the newest transaction
195 * containing inode's data.
196 *
197 * Note that directories do not have this problem because they
198 * don't use page cache.
199 */
Vegard Nossum6a7fd522016-07-04 11:03:00 -0400200 if (inode->i_ino != EXT4_JOURNAL_INO &&
201 ext4_should_journal_data(inode) &&
Jan Kara3abb1a02017-06-22 23:49:46 -0400202 (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode)) &&
203 inode->i_data.nrpages) {
Jan Kara2d859db2011-07-26 09:07:11 -0400204 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
205 tid_t commit_tid = EXT4_I(inode)->i_datasync_tid;
206
Theodore Ts'od76a3a772013-04-03 22:02:52 -0400207 jbd2_complete_transaction(journal, commit_tid);
Jan Kara2d859db2011-07-26 09:07:11 -0400208 filemap_write_and_wait(&inode->i_data);
209 }
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700210 truncate_inode_pages_final(&inode->i_data);
Jan Kara5dc23bd2013-06-04 14:46:12 -0400211
Al Viro0930fcc2010-06-07 13:16:22 -0400212 goto no_delete;
213 }
214
Theodore Ts'oe2bfb082014-10-05 22:47:07 -0400215 if (is_bad_inode(inode))
216 goto no_delete;
217 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500218
Jan Kara678aaf42008-07-11 19:27:31 -0400219 if (ext4_should_order_data(inode))
220 ext4_begin_ordered_truncate(inode, 0);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700221 truncate_inode_pages_final(&inode->i_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700222
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200223 /*
Jan Karaceff86f2020-04-21 10:54:45 +0200224 * For inodes with journalled data, transaction commit could have
225 * dirtied the inode. Flush worker is ignoring it because of I_FREEING
226 * flag but we still need to remove the inode from the writeback lists.
227 */
228 if (!list_empty_careful(&inode->i_io_list)) {
229 WARN_ON_ONCE(!ext4_should_journal_data(inode));
230 inode_io_list_del(inode);
231 }
232
233 /*
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200234 * Protect us against freezing - iput() caller didn't have to have any
235 * protection against it
236 */
237 sb_start_intwrite(inode->i_sb);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400238
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400239 if (!IS_NOQUOTA(inode))
240 extra_credits += EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb);
241
Jan Kara65db8692019-11-05 17:44:12 +0100242 /*
243 * Block bitmap, group descriptor, and inode are accounted in both
244 * ext4_blocks_for_truncate() and extra_credits. So subtract 3.
245 */
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400246 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
Jan Kara65db8692019-11-05 17:44:12 +0100247 ext4_blocks_for_truncate(inode) + extra_credits - 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700248 if (IS_ERR(handle)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400249 ext4_std_error(inode->i_sb, PTR_ERR(handle));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700250 /*
251 * If we're going to skip the normal cleanup, we still need to
252 * make sure that the in-core orphan linked list is properly
253 * cleaned up.
254 */
Mingming Cao617ba132006-10-11 01:20:53 -0700255 ext4_orphan_del(NULL, inode);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200256 sb_end_intwrite(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700257 goto no_delete;
258 }
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400259
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700260 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500261 ext4_handle_sync(handle);
Tahsin Erdogan407cd7f2017-07-04 00:11:21 -0400262
263 /*
264 * Set inode->i_size to 0 before calling ext4_truncate(). We need
265 * special handling of symlinks here because i_size is used to
266 * determine whether ext4_inode_info->i_data contains symlink data or
267 * block mappings. Setting i_size to 0 will remove its fast symlink
268 * status. Erase i_data so that it becomes a valid empty block map.
269 */
270 if (ext4_inode_is_fast_symlink(inode))
271 memset(EXT4_I(inode)->i_data, 0, sizeof(EXT4_I(inode)->i_data));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700272 inode->i_size = 0;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400273 err = ext4_mark_inode_dirty(handle, inode);
274 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500275 ext4_warning(inode->i_sb,
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400276 "couldn't mark inode dirty (err %d)", err);
277 goto stop_handle;
278 }
Theodore Ts'o2c98eb52016-11-13 22:02:26 -0500279 if (inode->i_blocks) {
280 err = ext4_truncate(inode);
281 if (err) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400282 ext4_error_err(inode->i_sb, -err,
283 "couldn't truncate inode %lu (err %d)",
284 inode->i_ino, err);
Theodore Ts'o2c98eb52016-11-13 22:02:26 -0500285 goto stop_handle;
286 }
287 }
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400288
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400289 /* Remove xattr references. */
290 err = ext4_xattr_delete_inode(handle, inode, &ea_inode_array,
291 extra_credits);
292 if (err) {
293 ext4_warning(inode->i_sb, "xattr delete (err %d)", err);
294stop_handle:
295 ext4_journal_stop(handle);
296 ext4_orphan_del(NULL, inode);
297 sb_end_intwrite(inode->i_sb);
298 ext4_xattr_inode_array_free(ea_inode_array);
299 goto no_delete;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400300 }
301
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700302 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700303 * Kill off the orphan record which ext4_truncate created.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700304 * AKPM: I think this can be inside the above `if'.
Mingming Cao617ba132006-10-11 01:20:53 -0700305 * Note that ext4_orphan_del() has to be able to cope with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700306 * deletion of a non-existent orphan - this is because we don't
Mingming Cao617ba132006-10-11 01:20:53 -0700307 * know if ext4_truncate() actually created an orphan record.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700308 * (Well, we could do this if we need to, but heck - it works)
309 */
Mingming Cao617ba132006-10-11 01:20:53 -0700310 ext4_orphan_del(handle, inode);
Arnd Bergmann5ffff832018-07-29 15:50:00 -0400311 EXT4_I(inode)->i_dtime = (__u32)ktime_get_real_seconds();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700312
313 /*
314 * One subtle ordering requirement: if anything has gone wrong
315 * (transaction abort, IO errors, whatever), then we can still
316 * do these next steps (the fs will already have been marked as
317 * having errors), but we can't free the inode if the mark_dirty
318 * fails.
319 */
Mingming Cao617ba132006-10-11 01:20:53 -0700320 if (ext4_mark_inode_dirty(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700321 /* If that failed, just do the required in-core inode clear. */
Al Viro0930fcc2010-06-07 13:16:22 -0400322 ext4_clear_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700323 else
Mingming Cao617ba132006-10-11 01:20:53 -0700324 ext4_free_inode(handle, inode);
325 ext4_journal_stop(handle);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200326 sb_end_intwrite(inode->i_sb);
Tahsin Erdogan0421a182017-06-22 10:26:31 -0400327 ext4_xattr_inode_array_free(ea_inode_array);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700328 return;
329no_delete:
Al Viro0930fcc2010-06-07 13:16:22 -0400330 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700331}
332
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300333#ifdef CONFIG_QUOTA
334qsize_t *ext4_get_reserved_space(struct inode *inode)
Mingming Cao60e58e02009-01-22 18:13:05 +0100335{
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300336 return &EXT4_I(inode)->i_reserved_quota;
Mingming Cao60e58e02009-01-22 18:13:05 +0100337}
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300338#endif
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500339
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400340/*
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500341 * Called with i_data_sem down, which is important since we can call
342 * ext4_discard_preallocations() from here.
343 */
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500344void ext4_da_update_reserve_space(struct inode *inode,
345 int used, int quota_claim)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400346{
347 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500348 struct ext4_inode_info *ei = EXT4_I(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400349
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500350 spin_lock(&ei->i_block_reservation_lock);
Aditya Kalid8990242011-09-09 19:18:51 -0400351 trace_ext4_da_update_reserve_space(inode, used, quota_claim);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500352 if (unlikely(used > ei->i_reserved_data_blocks)) {
Theodore Ts'o8de5c322013-02-14 15:11:41 -0500353 ext4_warning(inode->i_sb, "%s: ino %lu, used %d "
Theodore Ts'o1084f252012-03-19 23:13:43 -0400354 "with only %d reserved data blocks",
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500355 __func__, inode->i_ino, used,
356 ei->i_reserved_data_blocks);
357 WARN_ON(1);
358 used = ei->i_reserved_data_blocks;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400359 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400360
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500361 /* Update per-inode reservations */
362 ei->i_reserved_data_blocks -= used;
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -0400363 percpu_counter_sub(&sbi->s_dirtyclusters_counter, used);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500364
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400365 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +0100366
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400367 /* Update quota subsystem for data blocks */
368 if (quota_claim)
Aditya Kali7b415bf2011-09-09 19:04:51 -0400369 dquot_claim_block(inode, EXT4_C2B(sbi, used));
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400370 else {
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500371 /*
372 * We did fallocate with an offset that is already delayed
373 * allocated. So on delayed allocated writeback we should
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400374 * not re-claim the quota for fallocated blocks.
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500375 */
Aditya Kali7b415bf2011-09-09 19:04:51 -0400376 dquot_release_reservation_block(inode, EXT4_C2B(sbi, used));
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500377 }
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -0400378
379 /*
380 * If we have done all the pending block allocations and if
381 * there aren't any writers on the inode, we can discard the
382 * inode's preallocations.
383 */
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500384 if ((ei->i_reserved_data_blocks == 0) &&
Nikolay Borisov82dd1242019-02-10 23:04:16 -0500385 !inode_is_open_for_write(inode))
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -0400386 ext4_discard_preallocations(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400387}
388
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400389static int __check_block_validity(struct inode *inode, const char *func,
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400390 unsigned int line,
391 struct ext4_map_blocks *map)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400392{
Theodore Ts'o345c0db2019-04-09 23:37:08 -0400393 if (ext4_has_feature_journal(inode->i_sb) &&
394 (inode->i_ino ==
395 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum)))
396 return 0;
Theodore Ts'o24676da2010-05-16 21:00:00 -0400397 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
398 map->m_len)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400399 ext4_error_inode(inode, func, line, map->m_pblk,
Theodore Ts'obdbd6ce2018-06-15 12:27:16 -0400400 "lblock %lu mapped to illegal pblock %llu "
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400401 "(length %d)", (unsigned long) map->m_lblk,
Theodore Ts'obdbd6ce2018-06-15 12:27:16 -0400402 map->m_pblk, map->m_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400403 return -EFSCORRUPTED;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400404 }
405 return 0;
406}
407
Jan Kara53085fa2015-12-07 15:09:35 -0500408int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk,
409 ext4_lblk_t len)
410{
411 int ret;
412
Eric Biggers33b4cc22019-12-26 10:10:22 -0600413 if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400414 return fscrypt_zeroout_range(inode, lblk, pblk, len);
Jan Kara53085fa2015-12-07 15:09:35 -0500415
416 ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS);
417 if (ret > 0)
418 ret = 0;
419
420 return ret;
421}
422
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400423#define check_block_validity(inode, map) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400424 __check_block_validity((inode), __func__, __LINE__, (map))
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400425
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400426#ifdef ES_AGGRESSIVE_TEST
427static void ext4_map_blocks_es_recheck(handle_t *handle,
428 struct inode *inode,
429 struct ext4_map_blocks *es_map,
430 struct ext4_map_blocks *map,
431 int flags)
432{
433 int retval;
434
435 map->m_flags = 0;
436 /*
437 * There is a race window that the result is not the same.
438 * e.g. xfstests #223 when dioread_nolock enables. The reason
439 * is that we lookup a block mapping in extent status tree with
440 * out taking i_data_sem. So at the time the unwritten extent
441 * could be converted.
442 */
Jan Kara2dcba472015-12-07 15:04:57 -0500443 down_read(&EXT4_I(inode)->i_data_sem);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400444 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Eric Whitney9e524842020-04-15 16:31:39 -0400445 retval = ext4_ext_map_blocks(handle, inode, map, 0);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400446 } else {
Eric Whitney9e524842020-04-15 16:31:39 -0400447 retval = ext4_ind_map_blocks(handle, inode, map, 0);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400448 }
Jan Kara2dcba472015-12-07 15:04:57 -0500449 up_read((&EXT4_I(inode)->i_data_sem));
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400450
451 /*
452 * We don't check m_len because extent will be collpased in status
453 * tree. So the m_len might not equal.
454 */
455 if (es_map->m_lblk != map->m_lblk ||
456 es_map->m_flags != map->m_flags ||
457 es_map->m_pblk != map->m_pblk) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400458 printk("ES cache assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400459 "es_cached ex [%d/%d/%llu/%x] != "
460 "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
461 inode->i_ino, es_map->m_lblk, es_map->m_len,
462 es_map->m_pblk, es_map->m_flags, map->m_lblk,
463 map->m_len, map->m_pblk, map->m_flags,
464 retval, flags);
465 }
466}
467#endif /* ES_AGGRESSIVE_TEST */
468
Theodore Ts'o55138e0b2009-09-29 13:31:31 -0400469/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400470 * The ext4_map_blocks() function tries to look up the requested blocks,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400471 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500472 *
Mingming Caof5ab0d12008-02-25 15:29:55 -0500473 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
474 * and store the allocated blocks in the result buffer head and mark it
475 * mapped.
476 *
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400477 * If file type is extents based, it will call ext4_ext_map_blocks(),
478 * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
Mingming Caof5ab0d12008-02-25 15:29:55 -0500479 * based files
480 *
Jan Karafacab4d2016-03-09 22:54:00 -0500481 * On success, it returns the number of blocks being mapped or allocated. if
482 * create==0 and the blocks are pre-allocated and unwritten, the resulting @map
483 * is marked as unwritten. If the create == 1, it will mark @map as mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500484 *
485 * It returns 0 if plain look up failed (blocks have not been allocated), in
Jan Karafacab4d2016-03-09 22:54:00 -0500486 * that case, @map is returned as unmapped but we still do fill map->m_len to
487 * indicate the length of a hole starting at map->m_lblk.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500488 *
489 * It returns the error in case of allocation failure.
490 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400491int ext4_map_blocks(handle_t *handle, struct inode *inode,
492 struct ext4_map_blocks *map, int flags)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500493{
Zheng Liud100eef2013-02-18 00:29:59 -0500494 struct extent_status es;
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500495 int retval;
Lukas Czernerb8a86842014-03-18 18:05:35 -0400496 int ret = 0;
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400497#ifdef ES_AGGRESSIVE_TEST
498 struct ext4_map_blocks orig_map;
499
500 memcpy(&orig_map, map, sizeof(*map));
501#endif
Mingming Caof5ab0d12008-02-25 15:29:55 -0500502
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400503 map->m_flags = 0;
504 ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
505 "logical block %lu\n", inode->i_ino, flags, map->m_len,
506 (unsigned long) map->m_lblk);
Zheng Liud100eef2013-02-18 00:29:59 -0500507
Theodore Ts'oe861b5e2014-02-20 12:54:05 -0500508 /*
509 * ext4_map_blocks returns an int, and m_len is an unsigned int
510 */
511 if (unlikely(map->m_len > INT_MAX))
512 map->m_len = INT_MAX;
513
Kazuya Mio4adb6ab2014-04-07 10:53:28 -0400514 /* We can handle the block number less than EXT_MAX_BLOCKS */
515 if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400516 return -EFSCORRUPTED;
Kazuya Mio4adb6ab2014-04-07 10:53:28 -0400517
Zheng Liud100eef2013-02-18 00:29:59 -0500518 /* Lookup extent status tree firstly */
Theodore Ts'obb5835e2019-08-11 16:32:41 -0400519 if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) {
Zheng Liud100eef2013-02-18 00:29:59 -0500520 if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
521 map->m_pblk = ext4_es_pblock(&es) +
522 map->m_lblk - es.es_lblk;
523 map->m_flags |= ext4_es_is_written(&es) ?
524 EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN;
525 retval = es.es_len - (map->m_lblk - es.es_lblk);
526 if (retval > map->m_len)
527 retval = map->m_len;
528 map->m_len = retval;
529 } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) {
Jan Karafacab4d2016-03-09 22:54:00 -0500530 map->m_pblk = 0;
531 retval = es.es_len - (map->m_lblk - es.es_lblk);
532 if (retval > map->m_len)
533 retval = map->m_len;
534 map->m_len = retval;
Zheng Liud100eef2013-02-18 00:29:59 -0500535 retval = 0;
536 } else {
Arnd Bergmann1e83bc82019-04-07 12:24:43 -0400537 BUG();
Zheng Liud100eef2013-02-18 00:29:59 -0500538 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400539#ifdef ES_AGGRESSIVE_TEST
540 ext4_map_blocks_es_recheck(handle, inode, map,
541 &orig_map, flags);
542#endif
Zheng Liud100eef2013-02-18 00:29:59 -0500543 goto found;
544 }
545
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500546 /*
Theodore Ts'ob920c752009-05-14 00:54:29 -0400547 * Try to see if we can get the block without requesting a new
548 * file system block.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500549 */
Jan Kara2dcba472015-12-07 15:04:57 -0500550 down_read(&EXT4_I(inode)->i_data_sem);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400551 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Eric Whitney9e524842020-04-15 16:31:39 -0400552 retval = ext4_ext_map_blocks(handle, inode, map, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500553 } else {
Eric Whitney9e524842020-04-15 16:31:39 -0400554 retval = ext4_ind_map_blocks(handle, inode, map, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500555 }
Zheng Liuf7fec032013-02-18 00:28:47 -0500556 if (retval > 0) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400557 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -0500558
Zheng Liu44fb851d2013-07-29 12:51:42 -0400559 if (unlikely(retval != map->m_len)) {
560 ext4_warning(inode->i_sb,
561 "ES len assertion failed for inode "
562 "%lu: retval %d != map->m_len %d",
563 inode->i_ino, retval, map->m_len);
564 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400565 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400566
Zheng Liuf7fec032013-02-18 00:28:47 -0500567 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
568 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
569 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400570 !(status & EXTENT_STATUS_WRITTEN) &&
Eric Whitneyad431022018-10-01 14:10:39 -0400571 ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk,
572 map->m_lblk + map->m_len - 1))
Zheng Liuf7fec032013-02-18 00:28:47 -0500573 status |= EXTENT_STATUS_DELAYED;
574 ret = ext4_es_insert_extent(inode, map->m_lblk,
575 map->m_len, map->m_pblk, status);
576 if (ret < 0)
577 retval = ret;
578 }
Jan Kara2dcba472015-12-07 15:04:57 -0500579 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -0500580
Zheng Liud100eef2013-02-18 00:29:59 -0500581found:
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400582 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Lukas Czernerb8a86842014-03-18 18:05:35 -0400583 ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400584 if (ret != 0)
585 return ret;
586 }
587
Mingming Caof5ab0d12008-02-25 15:29:55 -0500588 /* If it is only a block(s) look up */
Theodore Ts'oc2177052009-05-14 00:58:52 -0400589 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500590 return retval;
591
592 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -0500593 * Returns if the blocks have already allocated
594 *
595 * Note that if blocks have been preallocated
Tao Madf3ab172011-10-08 15:53:49 -0400596 * ext4_ext_get_block() returns the create = 0
Mingming Caof5ab0d12008-02-25 15:29:55 -0500597 * with buffer head unmapped.
598 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400599 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
Lukas Czernerb8a86842014-03-18 18:05:35 -0400600 /*
601 * If we need to convert extent to unwritten
602 * we continue and do the actual work in
603 * ext4_ext_map_blocks()
604 */
605 if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN))
606 return retval;
Mingming Caof5ab0d12008-02-25 15:29:55 -0500607
608 /*
Zheng Liua25a4e12013-02-18 00:28:04 -0500609 * Here we clear m_flags because after allocating an new extent,
610 * it will be set again.
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -0400611 */
Zheng Liua25a4e12013-02-18 00:28:04 -0500612 map->m_flags &= ~EXT4_MAP_FLAGS;
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -0400613
614 /*
Lukas Czerner556615d2014-04-20 23:45:47 -0400615 * New blocks allocate and/or writing to unwritten extent
Mingming Caof5ab0d12008-02-25 15:29:55 -0500616 * will possibly result in updating i_data, so we take
Seunghun Leed91bd2c2014-09-01 22:15:30 -0400617 * the write lock of i_data_sem, and call get_block()
Mingming Caof5ab0d12008-02-25 15:29:55 -0500618 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500619 */
Lukas Czernerc8b459f2014-05-12 12:55:07 -0400620 down_write(&EXT4_I(inode)->i_data_sem);
Mingming Caod2a17632008-07-14 17:52:37 -0400621
622 /*
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500623 * We need to check for EXT4 here because migrate
624 * could have changed the inode type in between
625 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400626 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400627 retval = ext4_ext_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500628 } else {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400629 retval = ext4_ind_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400630
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400631 if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400632 /*
633 * We allocated new blocks which will result in
634 * i_data's format changing. Force the migrate
635 * to fail by clearing migrate flags
636 */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500637 ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400638 }
Mingming Caod2a17632008-07-14 17:52:37 -0400639
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500640 /*
641 * Update reserved blocks/metadata blocks after successful
642 * block allocation which had been deferred till now. We don't
643 * support fallocate for non extent files. So we can update
644 * reserve space here.
645 */
646 if ((retval > 0) &&
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -0500647 (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500648 ext4_da_update_reserve_space(inode, retval, 1);
649 }
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -0400650
Zheng Liuf7fec032013-02-18 00:28:47 -0500651 if (retval > 0) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400652 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -0500653
Zheng Liu44fb851d2013-07-29 12:51:42 -0400654 if (unlikely(retval != map->m_len)) {
655 ext4_warning(inode->i_sb,
656 "ES len assertion failed for inode "
657 "%lu: retval %d != map->m_len %d",
658 inode->i_ino, retval, map->m_len);
659 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400660 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400661
Zheng Liuadb23552013-03-10 21:13:05 -0400662 /*
Jan Karac86d8db2015-12-07 15:10:26 -0500663 * We have to zeroout blocks before inserting them into extent
664 * status tree. Otherwise someone could look them up there and
Jan Kara9b623df2016-09-30 02:02:29 -0400665 * use them before they are really zeroed. We also have to
666 * unmap metadata before zeroing as otherwise writeback can
667 * overwrite zeros with stale data from block device.
Jan Karac86d8db2015-12-07 15:10:26 -0500668 */
669 if (flags & EXT4_GET_BLOCKS_ZERO &&
670 map->m_flags & EXT4_MAP_MAPPED &&
671 map->m_flags & EXT4_MAP_NEW) {
672 ret = ext4_issue_zeroout(inode, map->m_lblk,
673 map->m_pblk, map->m_len);
674 if (ret) {
675 retval = ret;
676 goto out_sem;
677 }
678 }
679
680 /*
Zheng Liuadb23552013-03-10 21:13:05 -0400681 * If the extent has been zeroed out, we don't need to update
682 * extent status tree.
683 */
684 if ((flags & EXT4_GET_BLOCKS_PRE_IO) &&
Theodore Ts'obb5835e2019-08-11 16:32:41 -0400685 ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) {
Zheng Liuadb23552013-03-10 21:13:05 -0400686 if (ext4_es_is_written(&es))
Jan Karac86d8db2015-12-07 15:10:26 -0500687 goto out_sem;
Zheng Liuadb23552013-03-10 21:13:05 -0400688 }
Zheng Liuf7fec032013-02-18 00:28:47 -0500689 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
690 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
691 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400692 !(status & EXTENT_STATUS_WRITTEN) &&
Eric Whitneyad431022018-10-01 14:10:39 -0400693 ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk,
694 map->m_lblk + map->m_len - 1))
Zheng Liuf7fec032013-02-18 00:28:47 -0500695 status |= EXTENT_STATUS_DELAYED;
696 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
697 map->m_pblk, status);
Jan Karac86d8db2015-12-07 15:10:26 -0500698 if (ret < 0) {
Zheng Liuf7fec032013-02-18 00:28:47 -0500699 retval = ret;
Jan Karac86d8db2015-12-07 15:10:26 -0500700 goto out_sem;
701 }
Aditya Kali5356f2612011-09-09 19:20:51 -0400702 }
703
Jan Karac86d8db2015-12-07 15:10:26 -0500704out_sem:
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500705 up_write((&EXT4_I(inode)->i_data_sem));
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400706 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Lukas Czernerb8a86842014-03-18 18:05:35 -0400707 ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400708 if (ret != 0)
709 return ret;
Jan Kara06bd3c32016-04-24 00:56:03 -0400710
711 /*
712 * Inodes with freshly allocated blocks where contents will be
713 * visible after transaction commit must be on transaction's
714 * ordered data list.
715 */
716 if (map->m_flags & EXT4_MAP_NEW &&
717 !(map->m_flags & EXT4_MAP_UNWRITTEN) &&
718 !(flags & EXT4_GET_BLOCKS_ZERO) &&
Tahsin Erdogan02749a42017-06-22 11:31:25 -0400719 !ext4_is_quota_file(inode) &&
Jan Kara06bd3c32016-04-24 00:56:03 -0400720 ext4_should_order_data(inode)) {
Ross Zwisler73131fb2019-06-20 17:26:26 -0400721 loff_t start_byte =
722 (loff_t)map->m_lblk << inode->i_blkbits;
723 loff_t length = (loff_t)map->m_len << inode->i_blkbits;
724
Jan Karaee0876b2016-04-24 00:56:08 -0400725 if (flags & EXT4_GET_BLOCKS_IO_SUBMIT)
Ross Zwisler73131fb2019-06-20 17:26:26 -0400726 ret = ext4_jbd2_inode_add_wait(handle, inode,
727 start_byte, length);
Jan Karaee0876b2016-04-24 00:56:08 -0400728 else
Ross Zwisler73131fb2019-06-20 17:26:26 -0400729 ret = ext4_jbd2_inode_add_write(handle, inode,
730 start_byte, length);
Jan Kara06bd3c32016-04-24 00:56:03 -0400731 if (ret)
732 return ret;
733 }
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400734 }
Ritesh Harjaniec8c60b2020-05-10 11:54:52 +0530735
736 if (retval < 0)
737 ext_debug("failed for inode %lu with err %d\n",
738 inode->i_ino, retval);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500739 return retval;
740}
741
Jan Karaed8ad832016-02-19 00:18:25 -0500742/*
743 * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages
744 * we have to be careful as someone else may be manipulating b_state as well.
745 */
746static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags)
747{
748 unsigned long old_state;
749 unsigned long new_state;
750
751 flags &= EXT4_MAP_FLAGS;
752
753 /* Dummy buffer_head? Set non-atomically. */
754 if (!bh->b_page) {
755 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags;
756 return;
757 }
758 /*
759 * Someone else may be modifying b_state. Be careful! This is ugly but
760 * once we get rid of using bh as a container for mapping information
761 * to pass to / from get_block functions, this can go away.
762 */
763 do {
764 old_state = READ_ONCE(bh->b_state);
765 new_state = (old_state & ~EXT4_MAP_FLAGS) | flags;
766 } while (unlikely(
767 cmpxchg(&bh->b_state, old_state, new_state) != old_state));
768}
769
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400770static int _ext4_get_block(struct inode *inode, sector_t iblock,
771 struct buffer_head *bh, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700772{
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400773 struct ext4_map_blocks map;
Jan Karaefe70c22016-03-08 23:35:46 -0500774 int ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700775
Tao Ma46c7f252012-12-10 14:04:52 -0500776 if (ext4_has_inline_data(inode))
777 return -ERANGE;
778
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400779 map.m_lblk = iblock;
780 map.m_len = bh->b_size >> inode->i_blkbits;
781
Jan Karaefe70c22016-03-08 23:35:46 -0500782 ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map,
783 flags);
Jan Kara7fb54092008-02-10 01:08:38 -0500784 if (ret > 0) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400785 map_bh(bh, inode->i_sb, map.m_pblk);
Jan Karaed8ad832016-02-19 00:18:25 -0500786 ext4_update_bh_state(bh, map.m_flags);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400787 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
Jan Kara7fb54092008-02-10 01:08:38 -0500788 ret = 0;
Ross Zwisler547edce2016-11-08 11:30:58 +1100789 } else if (ret == 0) {
790 /* hole case, need to fill in bh->b_size */
791 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700792 }
793 return ret;
794}
795
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400796int ext4_get_block(struct inode *inode, sector_t iblock,
797 struct buffer_head *bh, int create)
798{
799 return _ext4_get_block(inode, iblock, bh,
800 create ? EXT4_GET_BLOCKS_CREATE : 0);
801}
802
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700803/*
Jan Kara705965b2016-03-08 23:08:10 -0500804 * Get block function used when preparing for buffered write if we require
805 * creating an unwritten extent if blocks haven't been allocated. The extent
806 * will be converted to written after the IO is complete.
807 */
808int ext4_get_block_unwritten(struct inode *inode, sector_t iblock,
809 struct buffer_head *bh_result, int create)
810{
811 ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n",
812 inode->i_ino, create);
813 return _ext4_get_block(inode, iblock, bh_result,
814 EXT4_GET_BLOCKS_IO_CREATE_EXT);
815}
816
Jan Karaefe70c22016-03-08 23:35:46 -0500817/* Maximum number of blocks we map for direct IO at once. */
818#define DIO_MAX_BLOCKS 4096
819
Jan Karae84dfbe2016-04-01 02:07:22 -0400820/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700821 * `handle' can be NULL if create is zero
822 */
Mingming Cao617ba132006-10-11 01:20:53 -0700823struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400824 ext4_lblk_t block, int map_flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700825{
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400826 struct ext4_map_blocks map;
827 struct buffer_head *bh;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400828 int create = map_flags & EXT4_GET_BLOCKS_CREATE;
Theodore Ts'o10560082014-08-29 20:51:32 -0400829 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700830
831 J_ASSERT(handle != NULL || create == 0);
832
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400833 map.m_lblk = block;
834 map.m_len = 1;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400835 err = ext4_map_blocks(handle, inode, &map, map_flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700836
Theodore Ts'o10560082014-08-29 20:51:32 -0400837 if (err == 0)
838 return create ? ERR_PTR(-ENOSPC) : NULL;
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400839 if (err < 0)
Theodore Ts'o10560082014-08-29 20:51:32 -0400840 return ERR_PTR(err);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400841
842 bh = sb_getblk(inode->i_sb, map.m_pblk);
Theodore Ts'o10560082014-08-29 20:51:32 -0400843 if (unlikely(!bh))
844 return ERR_PTR(-ENOMEM);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400845 if (map.m_flags & EXT4_MAP_NEW) {
846 J_ASSERT(create != 0);
847 J_ASSERT(handle != NULL);
848
849 /*
850 * Now that we do not always journal data, we should
851 * keep in mind whether this should always journal the
852 * new buffer as metadata. For now, regular file
853 * writes use ext4_get_block instead, so it's not a
854 * problem.
855 */
856 lock_buffer(bh);
857 BUFFER_TRACE(bh, "call get_create_access");
Theodore Ts'o10560082014-08-29 20:51:32 -0400858 err = ext4_journal_get_create_access(handle, bh);
859 if (unlikely(err)) {
860 unlock_buffer(bh);
861 goto errout;
862 }
863 if (!buffer_uptodate(bh)) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400864 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
865 set_buffer_uptodate(bh);
866 }
867 unlock_buffer(bh);
868 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
869 err = ext4_handle_dirty_metadata(handle, inode, bh);
Theodore Ts'o10560082014-08-29 20:51:32 -0400870 if (unlikely(err))
871 goto errout;
872 } else
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400873 BUFFER_TRACE(bh, "not a new buffer");
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400874 return bh;
Theodore Ts'o10560082014-08-29 20:51:32 -0400875errout:
876 brelse(bh);
877 return ERR_PTR(err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700878}
879
Mingming Cao617ba132006-10-11 01:20:53 -0700880struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400881 ext4_lblk_t block, int map_flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700882{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400883 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700884
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400885 bh = ext4_getblk(handle, inode, block, map_flags);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400886 if (IS_ERR(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700887 return bh;
ZhangXiaoxu7963e5a2019-08-22 23:00:32 -0400888 if (!bh || ext4_buffer_uptodate(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700889 return bh;
Mike Christiedfec8a12016-06-05 14:31:44 -0500890 ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700891 wait_on_buffer(bh);
892 if (buffer_uptodate(bh))
893 return bh;
894 put_bh(bh);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400895 return ERR_PTR(-EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700896}
897
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400898/* Read a contiguous batch of blocks. */
899int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count,
900 bool wait, struct buffer_head **bhs)
901{
902 int i, err;
903
904 for (i = 0; i < bh_count; i++) {
905 bhs[i] = ext4_getblk(NULL, inode, block + i, 0 /* map_flags */);
906 if (IS_ERR(bhs[i])) {
907 err = PTR_ERR(bhs[i]);
908 bh_count = i;
909 goto out_brelse;
910 }
911 }
912
913 for (i = 0; i < bh_count; i++)
914 /* Note that NULL bhs[i] is valid because of holes. */
ZhangXiaoxu7963e5a2019-08-22 23:00:32 -0400915 if (bhs[i] && !ext4_buffer_uptodate(bhs[i]))
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400916 ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1,
917 &bhs[i]);
918
919 if (!wait)
920 return 0;
921
922 for (i = 0; i < bh_count; i++)
923 if (bhs[i])
924 wait_on_buffer(bhs[i]);
925
926 for (i = 0; i < bh_count; i++) {
927 if (bhs[i] && !buffer_uptodate(bhs[i])) {
928 err = -EIO;
929 goto out_brelse;
930 }
931 }
932 return 0;
933
934out_brelse:
935 for (i = 0; i < bh_count; i++) {
936 brelse(bhs[i]);
937 bhs[i] = NULL;
938 }
939 return err;
940}
941
Tao Maf19d5872012-12-10 14:05:51 -0500942int ext4_walk_page_buffers(handle_t *handle,
943 struct buffer_head *head,
944 unsigned from,
945 unsigned to,
946 int *partial,
947 int (*fn)(handle_t *handle,
948 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700949{
950 struct buffer_head *bh;
951 unsigned block_start, block_end;
952 unsigned blocksize = head->b_size;
953 int err, ret = 0;
954 struct buffer_head *next;
955
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400956 for (bh = head, block_start = 0;
957 ret == 0 && (bh != head || !block_start);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400958 block_start = block_end, bh = next) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700959 next = bh->b_this_page;
960 block_end = block_start + blocksize;
961 if (block_end <= from || block_start >= to) {
962 if (partial && !buffer_uptodate(bh))
963 *partial = 1;
964 continue;
965 }
966 err = (*fn)(handle, bh);
967 if (!ret)
968 ret = err;
969 }
970 return ret;
971}
972
973/*
974 * To preserve ordering, it is essential that the hole instantiation and
975 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -0700976 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -0700977 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700978 * prepare_write() is the right place.
979 *
Jan Kara36ade452013-01-28 09:30:52 -0500980 * Also, this function can nest inside ext4_writepage(). In that case, we
981 * *know* that ext4_writepage() has generated enough buffer credits to do the
982 * whole page. So we won't block on the journal in that case, which is good,
983 * because the caller may be PF_MEMALLOC.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700984 *
Mingming Cao617ba132006-10-11 01:20:53 -0700985 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700986 * quota file writes. If we were to commit the transaction while thus
987 * reentered, there can be a deadlock - we would be holding a quota
988 * lock, and the commit would never complete if another thread had a
989 * transaction open and was blocking on the quota lock - a ranking
990 * violation.
991 *
Mingming Caodab291a2006-10-11 01:21:01 -0700992 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700993 * will _not_ run commit under these circumstances because handle->h_ref
994 * is elevated. We'll still have enough credits for the tiny quotafile
995 * write.
996 */
Tao Maf19d5872012-12-10 14:05:51 -0500997int do_journal_get_write_access(handle_t *handle,
998 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700999{
Jan Kara56d35a42010-08-05 14:41:42 -04001000 int dirty = buffer_dirty(bh);
1001 int ret;
1002
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001003 if (!buffer_mapped(bh) || buffer_freed(bh))
1004 return 0;
Jan Kara56d35a42010-08-05 14:41:42 -04001005 /*
Christoph Hellwigebdec242010-10-06 10:47:23 +02001006 * __block_write_begin() could have dirtied some buffers. Clean
Jan Kara56d35a42010-08-05 14:41:42 -04001007 * the dirty bit as jbd2_journal_get_write_access() could complain
1008 * otherwise about fs integrity issues. Setting of the dirty bit
Christoph Hellwigebdec242010-10-06 10:47:23 +02001009 * by __block_write_begin() isn't a real problem here as we clear
Jan Kara56d35a42010-08-05 14:41:42 -04001010 * the bit before releasing a page lock and thus writeback cannot
1011 * ever write the buffer.
1012 */
1013 if (dirty)
1014 clear_buffer_dirty(bh);
liang xie5d601252014-05-12 22:06:43 -04001015 BUFFER_TRACE(bh, "get write access");
Jan Kara56d35a42010-08-05 14:41:42 -04001016 ret = ext4_journal_get_write_access(handle, bh);
1017 if (!ret && dirty)
1018 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
1019 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001020}
1021
Chandan Rajendra643fa962018-12-12 15:20:12 +05301022#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrow2058f832015-04-12 00:55:10 -04001023static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len,
1024 get_block_t *get_block)
1025{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001026 unsigned from = pos & (PAGE_SIZE - 1);
Michael Halcrow2058f832015-04-12 00:55:10 -04001027 unsigned to = from + len;
1028 struct inode *inode = page->mapping->host;
1029 unsigned block_start, block_end;
1030 sector_t block;
1031 int err = 0;
1032 unsigned blocksize = inode->i_sb->s_blocksize;
1033 unsigned bbits;
Chandan Rajendra0b578f32019-05-20 09:29:50 -07001034 struct buffer_head *bh, *head, *wait[2];
1035 int nr_wait = 0;
1036 int i;
Michael Halcrow2058f832015-04-12 00:55:10 -04001037
1038 BUG_ON(!PageLocked(page));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001039 BUG_ON(from > PAGE_SIZE);
1040 BUG_ON(to > PAGE_SIZE);
Michael Halcrow2058f832015-04-12 00:55:10 -04001041 BUG_ON(from > to);
1042
1043 if (!page_has_buffers(page))
1044 create_empty_buffers(page, blocksize, 0);
1045 head = page_buffers(page);
1046 bbits = ilog2(blocksize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001047 block = (sector_t)page->index << (PAGE_SHIFT - bbits);
Michael Halcrow2058f832015-04-12 00:55:10 -04001048
1049 for (bh = head, block_start = 0; bh != head || !block_start;
1050 block++, block_start = block_end, bh = bh->b_this_page) {
1051 block_end = block_start + blocksize;
1052 if (block_end <= from || block_start >= to) {
1053 if (PageUptodate(page)) {
1054 if (!buffer_uptodate(bh))
1055 set_buffer_uptodate(bh);
1056 }
1057 continue;
1058 }
1059 if (buffer_new(bh))
1060 clear_buffer_new(bh);
1061 if (!buffer_mapped(bh)) {
1062 WARN_ON(bh->b_size != blocksize);
1063 err = get_block(inode, block, bh, 1);
1064 if (err)
1065 break;
1066 if (buffer_new(bh)) {
Michael Halcrow2058f832015-04-12 00:55:10 -04001067 if (PageUptodate(page)) {
1068 clear_buffer_new(bh);
1069 set_buffer_uptodate(bh);
1070 mark_buffer_dirty(bh);
1071 continue;
1072 }
1073 if (block_end > to || block_start < from)
1074 zero_user_segments(page, to, block_end,
1075 block_start, from);
1076 continue;
1077 }
1078 }
1079 if (PageUptodate(page)) {
1080 if (!buffer_uptodate(bh))
1081 set_buffer_uptodate(bh);
1082 continue;
1083 }
1084 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
1085 !buffer_unwritten(bh) &&
1086 (block_start < from || block_end > to)) {
Mike Christiedfec8a12016-06-05 14:31:44 -05001087 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
Chandan Rajendra0b578f32019-05-20 09:29:50 -07001088 wait[nr_wait++] = bh;
Michael Halcrow2058f832015-04-12 00:55:10 -04001089 }
1090 }
1091 /*
1092 * If we issued read requests, let them complete.
1093 */
Chandan Rajendra0b578f32019-05-20 09:29:50 -07001094 for (i = 0; i < nr_wait; i++) {
1095 wait_on_buffer(wait[i]);
1096 if (!buffer_uptodate(wait[i]))
Michael Halcrow2058f832015-04-12 00:55:10 -04001097 err = -EIO;
1098 }
Chandan Rajendra7e0785f2019-05-20 09:29:49 -07001099 if (unlikely(err)) {
Michael Halcrow2058f832015-04-12 00:55:10 -04001100 page_zero_new_buffers(page, from, to);
Chandan Rajendra0b578f32019-05-20 09:29:50 -07001101 } else if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) {
1102 for (i = 0; i < nr_wait; i++) {
1103 int err2;
1104
1105 err2 = fscrypt_decrypt_pagecache_blocks(page, blocksize,
1106 bh_offset(wait[i]));
1107 if (err2) {
1108 clear_buffer_uptodate(wait[i]);
1109 err = err2;
1110 }
1111 }
Chandan Rajendra7e0785f2019-05-20 09:29:49 -07001112 }
1113
Michael Halcrow2058f832015-04-12 00:55:10 -04001114 return err;
1115}
1116#endif
1117
Nick Pigginbfc1af62007-10-16 01:25:05 -07001118static int ext4_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001119 loff_t pos, unsigned len, unsigned flags,
1120 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001121{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001122 struct inode *inode = mapping->host;
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001123 int ret, needed_blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001124 handle_t *handle;
1125 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001126 struct page *page;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001127 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001128 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001129
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05001130 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
1131 return -EIO;
1132
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001133 trace_ext4_write_begin(inode, pos, len, flags);
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001134 /*
1135 * Reserve one block more for addition to orphan list in case
1136 * we allocate blocks but write fails for some reason
1137 */
1138 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001139 index = pos >> PAGE_SHIFT;
1140 from = pos & (PAGE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001141 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001142
Tao Maf19d5872012-12-10 14:05:51 -05001143 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
1144 ret = ext4_try_to_write_inline_data(mapping, inode, pos, len,
1145 flags, pagep);
1146 if (ret < 0)
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001147 return ret;
1148 if (ret == 1)
1149 return 0;
Tao Maf19d5872012-12-10 14:05:51 -05001150 }
1151
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001152 /*
1153 * grab_cache_page_write_begin() can take a long time if the
1154 * system is thrashing due to memory pressure, or if the page
1155 * is being written back. So grab it first before we start
1156 * the transaction handle. This also allows us to allocate
1157 * the page (if needed) without using GFP_NOFS.
1158 */
1159retry_grab:
Nick Piggin54566b22009-01-04 12:00:53 -08001160 page = grab_cache_page_write_begin(mapping, index, flags);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001161 if (!page)
1162 return -ENOMEM;
1163 unlock_page(page);
1164
1165retry_journal:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001166 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001167 if (IS_ERR(handle)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001168 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001169 return PTR_ERR(handle);
Jan Karacf108bc2008-07-11 19:27:31 -04001170 }
Tao Maf19d5872012-12-10 14:05:51 -05001171
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001172 lock_page(page);
1173 if (page->mapping != mapping) {
1174 /* The page got truncated from under us */
1175 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001176 put_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04001177 ext4_journal_stop(handle);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001178 goto retry_grab;
Jan Karacf108bc2008-07-11 19:27:31 -04001179 }
Dmitry Monakhov7afe5aa2013-08-28 14:30:47 -04001180 /* In case writeback began while the page was unlocked */
1181 wait_for_stable_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04001182
Chandan Rajendra643fa962018-12-12 15:20:12 +05301183#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrow2058f832015-04-12 00:55:10 -04001184 if (ext4_should_dioread_nolock(inode))
1185 ret = ext4_block_write_begin(page, pos, len,
Jan Kara705965b2016-03-08 23:08:10 -05001186 ext4_get_block_unwritten);
Michael Halcrow2058f832015-04-12 00:55:10 -04001187 else
1188 ret = ext4_block_write_begin(page, pos, len,
1189 ext4_get_block);
1190#else
Jiaying Zhang744692d2010-03-04 16:14:02 -05001191 if (ext4_should_dioread_nolock(inode))
Jan Kara705965b2016-03-08 23:08:10 -05001192 ret = __block_write_begin(page, pos, len,
1193 ext4_get_block_unwritten);
Jiaying Zhang744692d2010-03-04 16:14:02 -05001194 else
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001195 ret = __block_write_begin(page, pos, len, ext4_get_block);
Michael Halcrow2058f832015-04-12 00:55:10 -04001196#endif
Nick Pigginbfc1af62007-10-16 01:25:05 -07001197 if (!ret && ext4_should_journal_data(inode)) {
Tao Maf19d5872012-12-10 14:05:51 -05001198 ret = ext4_walk_page_buffers(handle, page_buffers(page),
1199 from, to, NULL,
1200 do_journal_get_write_access);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001201 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001202
1203 if (ret) {
Eric Biggersc93d8f82019-07-22 09:26:24 -07001204 bool extended = (pos + len > inode->i_size) &&
1205 !ext4_verity_in_progress(inode);
1206
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001207 unlock_page(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001208 /*
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001209 * __block_write_begin may have instantiated a few blocks
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001210 * outside i_size. Trim these off again. Don't need
1211 * i_size_read because we hold i_mutex.
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001212 *
1213 * Add inode to orphan list in case we crash before
1214 * truncate finishes
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001215 */
Eric Biggersc93d8f82019-07-22 09:26:24 -07001216 if (extended && ext4_can_truncate(inode))
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001217 ext4_orphan_add(handle, inode);
1218
1219 ext4_journal_stop(handle);
Eric Biggersc93d8f82019-07-22 09:26:24 -07001220 if (extended) {
Jan Karab9a42072009-12-08 21:24:33 -05001221 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001222 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001223 * If truncate failed early the inode might
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001224 * still be on the orphan list; we need to
1225 * make sure the inode is removed from the
1226 * orphan list in that case.
1227 */
1228 if (inode->i_nlink)
1229 ext4_orphan_del(NULL, inode);
1230 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001231
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001232 if (ret == -ENOSPC &&
1233 ext4_should_retry_alloc(inode->i_sb, &retries))
1234 goto retry_journal;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001235 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001236 return ret;
1237 }
1238 *pagep = page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001239 return ret;
1240}
1241
Nick Pigginbfc1af62007-10-16 01:25:05 -07001242/* For write_end() in data=journal mode */
1243static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001244{
Theodore Ts'o13fca322013-04-21 16:45:54 -04001245 int ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001246 if (!buffer_mapped(bh) || buffer_freed(bh))
1247 return 0;
1248 set_buffer_uptodate(bh);
Theodore Ts'o13fca322013-04-21 16:45:54 -04001249 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
1250 clear_buffer_meta(bh);
1251 clear_buffer_prio(bh);
1252 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001253}
1254
Zheng Liueed43332013-04-03 12:41:17 -04001255/*
1256 * We need to pick up the new inode size which generic_commit_write gave us
1257 * `file' can be NULL - eg, when called from page_symlink().
1258 *
1259 * ext4 never places buffers on inode->i_mapping->private_list. metadata
1260 * buffers are managed internally.
1261 */
1262static int ext4_write_end(struct file *file,
1263 struct address_space *mapping,
1264 loff_t pos, unsigned len, unsigned copied,
1265 struct page *page, void *fsdata)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001266{
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001267 handle_t *handle = ext4_journal_current_handle();
Zheng Liueed43332013-04-03 12:41:17 -04001268 struct inode *inode = mapping->host;
Xiaoguang Wang05726392015-02-12 23:00:17 -05001269 loff_t old_size = inode->i_size;
Zheng Liueed43332013-04-03 12:41:17 -04001270 int ret = 0, ret2;
1271 int i_size_changed = 0;
Theodore Ts'o362eca72018-07-10 01:07:43 -04001272 int inline_data = ext4_has_inline_data(inode);
Eric Biggersc93d8f82019-07-22 09:26:24 -07001273 bool verity = ext4_verity_in_progress(inode);
Zheng Liueed43332013-04-03 12:41:17 -04001274
1275 trace_ext4_write_end(inode, pos, len, copied);
Theodore Ts'o362eca72018-07-10 01:07:43 -04001276 if (inline_data) {
Theodore Ts'o42c832d2013-07-01 08:12:39 -04001277 ret = ext4_write_inline_data_end(inode, pos, len,
1278 copied, page);
Theodore Ts'oeb5efbc2017-02-04 23:04:00 -05001279 if (ret < 0) {
1280 unlock_page(page);
1281 put_page(page);
Theodore Ts'o42c832d2013-07-01 08:12:39 -04001282 goto errout;
Theodore Ts'oeb5efbc2017-02-04 23:04:00 -05001283 }
Theodore Ts'o42c832d2013-07-01 08:12:39 -04001284 copied = ret;
1285 } else
Tao Maf19d5872012-12-10 14:05:51 -05001286 copied = block_write_end(file, mapping, pos,
1287 len, copied, page, fsdata);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001288 /*
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001289 * it's important to update i_size while still holding page lock:
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001290 * page writeout could otherwise come in and zero beyond i_size.
Eric Biggersc93d8f82019-07-22 09:26:24 -07001291 *
1292 * If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree
1293 * blocks are being written past EOF, so skip the i_size update.
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001294 */
Eric Biggersc93d8f82019-07-22 09:26:24 -07001295 if (!verity)
1296 i_size_changed = ext4_update_inode_size(inode, pos + copied);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001297 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001298 put_page(page);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001299
Eric Biggersc93d8f82019-07-22 09:26:24 -07001300 if (old_size < pos && !verity)
Xiaoguang Wang05726392015-02-12 23:00:17 -05001301 pagecache_isize_extended(inode, old_size, pos);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001302 /*
1303 * Don't mark the inode dirty under page lock. First, it unnecessarily
1304 * makes the holding time of page lock longer. Second, it forces lock
1305 * ordering of page lock and transaction start for journaling
1306 * filesystems.
1307 */
Theodore Ts'o362eca72018-07-10 01:07:43 -04001308 if (i_size_changed || inline_data)
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07001309 ret = ext4_mark_inode_dirty(handle, inode);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001310
Eric Biggersc93d8f82019-07-22 09:26:24 -07001311 if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001312 /* if we have allocated more blocks and copied
1313 * less. We will have blocks allocated outside
1314 * inode->i_size. So truncate them
1315 */
1316 ext4_orphan_add(handle, inode);
Theodore Ts'o74d553a2013-04-03 12:39:17 -04001317errout:
Mingming Cao617ba132006-10-11 01:20:53 -07001318 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001319 if (!ret)
1320 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001321
Eric Biggersc93d8f82019-07-22 09:26:24 -07001322 if (pos + len > inode->i_size && !verity) {
Jan Karab9a42072009-12-08 21:24:33 -05001323 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001324 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001325 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001326 * on the orphan list; we need to make sure the inode
1327 * is removed from the orphan list in that case.
1328 */
1329 if (inode->i_nlink)
1330 ext4_orphan_del(NULL, inode);
1331 }
1332
Nick Pigginbfc1af62007-10-16 01:25:05 -07001333 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001334}
1335
Theodore Ts'ob90197b2015-10-15 10:29:05 -04001336/*
1337 * This is a private version of page_zero_new_buffers() which doesn't
1338 * set the buffer to be dirty, since in data=journalled mode we need
1339 * to call ext4_handle_dirty_metadata() instead.
1340 */
Jan Kara3b136492017-01-27 14:35:38 -05001341static void ext4_journalled_zero_new_buffers(handle_t *handle,
1342 struct page *page,
1343 unsigned from, unsigned to)
Theodore Ts'ob90197b2015-10-15 10:29:05 -04001344{
1345 unsigned int block_start = 0, block_end;
1346 struct buffer_head *head, *bh;
1347
1348 bh = head = page_buffers(page);
1349 do {
1350 block_end = block_start + bh->b_size;
1351 if (buffer_new(bh)) {
1352 if (block_end > from && block_start < to) {
1353 if (!PageUptodate(page)) {
1354 unsigned start, size;
1355
1356 start = max(from, block_start);
1357 size = min(to, block_end) - start;
1358
1359 zero_user(page, start, size);
Jan Kara3b136492017-01-27 14:35:38 -05001360 write_end_fn(handle, bh);
Theodore Ts'ob90197b2015-10-15 10:29:05 -04001361 }
1362 clear_buffer_new(bh);
1363 }
1364 }
1365 block_start = block_end;
1366 bh = bh->b_this_page;
1367 } while (bh != head);
1368}
1369
Nick Pigginbfc1af62007-10-16 01:25:05 -07001370static int ext4_journalled_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001371 struct address_space *mapping,
1372 loff_t pos, unsigned len, unsigned copied,
1373 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001374{
Mingming Cao617ba132006-10-11 01:20:53 -07001375 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -07001376 struct inode *inode = mapping->host;
Xiaoguang Wang05726392015-02-12 23:00:17 -05001377 loff_t old_size = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001378 int ret = 0, ret2;
1379 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001380 unsigned from, to;
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001381 int size_changed = 0;
Theodore Ts'o362eca72018-07-10 01:07:43 -04001382 int inline_data = ext4_has_inline_data(inode);
Eric Biggersc93d8f82019-07-22 09:26:24 -07001383 bool verity = ext4_verity_in_progress(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001384
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001385 trace_ext4_journalled_write_end(inode, pos, len, copied);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001386 from = pos & (PAGE_SIZE - 1);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001387 to = from + len;
1388
Curt Wohlgemuth441c8502011-08-13 11:25:18 -04001389 BUG_ON(!ext4_handle_valid(handle));
1390
Theodore Ts'o362eca72018-07-10 01:07:43 -04001391 if (inline_data) {
Theodore Ts'oeb5efbc2017-02-04 23:04:00 -05001392 ret = ext4_write_inline_data_end(inode, pos, len,
1393 copied, page);
1394 if (ret < 0) {
1395 unlock_page(page);
1396 put_page(page);
1397 goto errout;
1398 }
1399 copied = ret;
1400 } else if (unlikely(copied < len) && !PageUptodate(page)) {
Jan Kara3b136492017-01-27 14:35:38 -05001401 copied = 0;
1402 ext4_journalled_zero_new_buffers(handle, page, from, to);
1403 } else {
1404 if (unlikely(copied < len))
1405 ext4_journalled_zero_new_buffers(handle, page,
1406 from + copied, to);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001407 ret = ext4_walk_page_buffers(handle, page_buffers(page), from,
Jan Kara3b136492017-01-27 14:35:38 -05001408 from + copied, &partial,
1409 write_end_fn);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001410 if (!partial)
1411 SetPageUptodate(page);
1412 }
Eric Biggersc93d8f82019-07-22 09:26:24 -07001413 if (!verity)
1414 size_changed = ext4_update_inode_size(inode, pos + copied);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001415 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Jan Kara2d859db2011-07-26 09:07:11 -04001416 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001417 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001418 put_page(page);
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001419
Eric Biggersc93d8f82019-07-22 09:26:24 -07001420 if (old_size < pos && !verity)
Xiaoguang Wang05726392015-02-12 23:00:17 -05001421 pagecache_isize_extended(inode, old_size, pos);
1422
Theodore Ts'o362eca72018-07-10 01:07:43 -04001423 if (size_changed || inline_data) {
Mingming Cao617ba132006-10-11 01:20:53 -07001424 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001425 if (!ret)
1426 ret = ret2;
1427 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001428
Eric Biggersc93d8f82019-07-22 09:26:24 -07001429 if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001430 /* if we have allocated more blocks and copied
1431 * less. We will have blocks allocated outside
1432 * inode->i_size. So truncate them
1433 */
1434 ext4_orphan_add(handle, inode);
1435
Theodore Ts'oeb5efbc2017-02-04 23:04:00 -05001436errout:
Mingming Cao617ba132006-10-11 01:20:53 -07001437 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001438 if (!ret)
1439 ret = ret2;
Eric Biggersc93d8f82019-07-22 09:26:24 -07001440 if (pos + len > inode->i_size && !verity) {
Jan Karab9a42072009-12-08 21:24:33 -05001441 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001442 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001443 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001444 * on the orphan list; we need to make sure the inode
1445 * is removed from the orphan list in that case.
1446 */
1447 if (inode->i_nlink)
1448 ext4_orphan_del(NULL, inode);
1449 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001450
1451 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001452}
Mingming Caod2a17632008-07-14 17:52:37 -04001453
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001454/*
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001455 * Reserve space for a single cluster
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001456 */
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001457static int ext4_da_reserve_space(struct inode *inode)
Mingming Caod2a17632008-07-14 17:52:37 -04001458{
Mingming Cao60e58e02009-01-22 18:13:05 +01001459 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001460 struct ext4_inode_info *ei = EXT4_I(inode);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001461 int ret;
Mingming Caod2a17632008-07-14 17:52:37 -04001462
Mingming Cao60e58e02009-01-22 18:13:05 +01001463 /*
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001464 * We will charge metadata quota at writeout time; this saves
1465 * us from metadata over-estimation, though we may go over by
1466 * a small amount in the end. Here we just reserve for data.
Mingming Cao60e58e02009-01-22 18:13:05 +01001467 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04001468 ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001469 if (ret)
1470 return ret;
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001471
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001472 spin_lock(&ei->i_block_reservation_lock);
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04001473 if (ext4_claim_free_clusters(sbi, 1, 0)) {
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001474 spin_unlock(&ei->i_block_reservation_lock);
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001475 dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
Mingming Caod2a17632008-07-14 17:52:37 -04001476 return -ENOSPC;
1477 }
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001478 ei->i_reserved_data_blocks++;
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001479 trace_ext4_da_reserve_space(inode);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001480 spin_unlock(&ei->i_block_reservation_lock);
Dmitry Monakhov39bc6802009-12-10 16:36:27 +00001481
Mingming Caod2a17632008-07-14 17:52:37 -04001482 return 0; /* success */
1483}
1484
Eric Whitneyf4567672018-10-01 14:33:24 -04001485void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001486{
1487 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001488 struct ext4_inode_info *ei = EXT4_I(inode);
Mingming Caod2a17632008-07-14 17:52:37 -04001489
Mingming Caocd213222008-08-19 22:16:59 -04001490 if (!to_free)
1491 return; /* Nothing to release, exit */
1492
Mingming Caod2a17632008-07-14 17:52:37 -04001493 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001494
Li Zefan5a58ec872010-05-17 02:00:00 -04001495 trace_ext4_da_release_space(inode, to_free);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001496 if (unlikely(to_free > ei->i_reserved_data_blocks)) {
Mingming Caocd213222008-08-19 22:16:59 -04001497 /*
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001498 * if there aren't enough reserved blocks, then the
1499 * counter is messed up somewhere. Since this
1500 * function is called from invalidate page, it's
1501 * harmless to return without any action.
Mingming Caocd213222008-08-19 22:16:59 -04001502 */
Theodore Ts'o8de5c322013-02-14 15:11:41 -05001503 ext4_warning(inode->i_sb, "ext4_da_release_space: "
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001504 "ino %lu, to_free %d with only %d reserved "
Theodore Ts'o1084f252012-03-19 23:13:43 -04001505 "data blocks", inode->i_ino, to_free,
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001506 ei->i_reserved_data_blocks);
1507 WARN_ON(1);
1508 to_free = ei->i_reserved_data_blocks;
1509 }
1510 ei->i_reserved_data_blocks -= to_free;
1511
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001512 /* update fs dirty data blocks counter */
Theodore Ts'o57042652011-09-09 18:56:51 -04001513 percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free);
Mingming Caod2a17632008-07-14 17:52:37 -04001514
Mingming Caod2a17632008-07-14 17:52:37 -04001515 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001516
Aditya Kali7b415bf2011-09-09 19:04:51 -04001517 dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free));
Mingming Caod2a17632008-07-14 17:52:37 -04001518}
1519
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001520/*
Alex Tomas64769242008-07-11 19:27:31 -04001521 * Delayed allocation stuff
1522 */
1523
Jan Kara4e7ea812013-06-04 13:17:40 -04001524struct mpage_da_data {
1525 struct inode *inode;
1526 struct writeback_control *wbc;
Jan Kara6b523df2013-06-04 13:21:11 -04001527
Jan Kara4e7ea812013-06-04 13:17:40 -04001528 pgoff_t first_page; /* The first page to write */
1529 pgoff_t next_page; /* Current page to examine */
1530 pgoff_t last_page; /* Last page to examine */
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001531 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04001532 * Extent to map - this can be after first_page because that can be
1533 * fully mapped. We somewhat abuse m_flags to store whether the extent
1534 * is delalloc or unwritten.
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001535 */
Jan Kara4e7ea812013-06-04 13:17:40 -04001536 struct ext4_map_blocks map;
1537 struct ext4_io_submit io_submit; /* IO submission data */
Jan Karadddbd6a2017-04-30 18:29:10 -04001538 unsigned int do_map:1;
Jan Kara4e7ea812013-06-04 13:17:40 -04001539};
Alex Tomas64769242008-07-11 19:27:31 -04001540
Jan Kara4e7ea812013-06-04 13:17:40 -04001541static void mpage_release_unused_pages(struct mpage_da_data *mpd,
1542 bool invalidate)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001543{
1544 int nr_pages, i;
1545 pgoff_t index, end;
1546 struct pagevec pvec;
1547 struct inode *inode = mpd->inode;
1548 struct address_space *mapping = inode->i_mapping;
Jan Kara4e7ea812013-06-04 13:17:40 -04001549
1550 /* This is necessary when next_page == 0. */
1551 if (mpd->first_page >= mpd->next_page)
1552 return;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001553
Curt Wohlgemuthc7f59382011-02-26 12:27:52 -05001554 index = mpd->first_page;
1555 end = mpd->next_page - 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04001556 if (invalidate) {
1557 ext4_lblk_t start, last;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001558 start = index << (PAGE_SHIFT - inode->i_blkbits);
1559 last = end << (PAGE_SHIFT - inode->i_blkbits);
Jan Kara4e7ea812013-06-04 13:17:40 -04001560 ext4_es_remove_extent(inode, start, last - start + 1);
1561 }
Zheng Liu51865fd2012-11-08 21:57:32 -05001562
Mel Gorman86679822017-11-15 17:37:52 -08001563 pagevec_init(&pvec);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001564 while (index <= end) {
Jan Kara397162f2017-09-06 16:21:43 -07001565 nr_pages = pagevec_lookup_range(&pvec, mapping, &index, end);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001566 if (nr_pages == 0)
1567 break;
1568 for (i = 0; i < nr_pages; i++) {
1569 struct page *page = pvec.pages[i];
Jan Kara2b85a612017-09-06 16:21:30 -07001570
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001571 BUG_ON(!PageLocked(page));
1572 BUG_ON(PageWriteback(page));
Jan Kara4e7ea812013-06-04 13:17:40 -04001573 if (invalidate) {
wangguang4e800c02016-09-15 11:32:46 -04001574 if (page_mapped(page))
1575 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001576 block_invalidatepage(page, 0, PAGE_SIZE);
Jan Kara4e7ea812013-06-04 13:17:40 -04001577 ClearPageUptodate(page);
1578 }
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001579 unlock_page(page);
1580 }
Jan Kara9b1d09982010-03-03 16:19:32 -05001581 pagevec_release(&pvec);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001582 }
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001583}
1584
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001585static void ext4_print_free_blocks(struct inode *inode)
1586{
1587 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o92b97812012-03-19 23:41:49 -04001588 struct super_block *sb = inode->i_sb;
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001589 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'o92b97812012-03-19 23:41:49 -04001590
1591 ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld",
Theodore Ts'o5dee5432011-09-09 19:10:51 -04001592 EXT4_C2B(EXT4_SB(inode->i_sb),
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001593 ext4_count_free_clusters(sb)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001594 ext4_msg(sb, KERN_CRIT, "Free/Dirty block details");
1595 ext4_msg(sb, KERN_CRIT, "free_blocks=%lld",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001596 (long long) EXT4_C2B(EXT4_SB(sb),
Theodore Ts'o57042652011-09-09 18:56:51 -04001597 percpu_counter_sum(&sbi->s_freeclusters_counter)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001598 ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001599 (long long) EXT4_C2B(EXT4_SB(sb),
Aditya Kali7b415bf2011-09-09 19:04:51 -04001600 percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001601 ext4_msg(sb, KERN_CRIT, "Block reservation details");
1602 ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001603 ei->i_reserved_data_blocks);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001604 return;
1605}
1606
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001607static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001608{
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001609 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001610}
1611
Alex Tomas64769242008-07-11 19:27:31 -04001612/*
Eric Whitney0b02f4c2018-10-01 14:19:37 -04001613 * ext4_insert_delayed_block - adds a delayed block to the extents status
1614 * tree, incrementing the reserved cluster/block
1615 * count or making a pending reservation
1616 * where needed
1617 *
1618 * @inode - file containing the newly added block
1619 * @lblk - logical block to be added
1620 *
1621 * Returns 0 on success, negative error code on failure.
1622 */
1623static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk)
1624{
1625 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1626 int ret;
1627 bool allocated = false;
1628
1629 /*
1630 * If the cluster containing lblk is shared with a delayed,
1631 * written, or unwritten extent in a bigalloc file system, it's
1632 * already been accounted for and does not need to be reserved.
1633 * A pending reservation must be made for the cluster if it's
1634 * shared with a written or unwritten extent and doesn't already
1635 * have one. Written and unwritten extents can be purged from the
1636 * extents status tree if the system is under memory pressure, so
1637 * it's necessary to examine the extent tree if a search of the
1638 * extents status tree doesn't get a match.
1639 */
1640 if (sbi->s_cluster_ratio == 1) {
1641 ret = ext4_da_reserve_space(inode);
1642 if (ret != 0) /* ENOSPC */
1643 goto errout;
1644 } else { /* bigalloc */
1645 if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) {
1646 if (!ext4_es_scan_clu(inode,
1647 &ext4_es_is_mapped, lblk)) {
1648 ret = ext4_clu_mapped(inode,
1649 EXT4_B2C(sbi, lblk));
1650 if (ret < 0)
1651 goto errout;
1652 if (ret == 0) {
1653 ret = ext4_da_reserve_space(inode);
1654 if (ret != 0) /* ENOSPC */
1655 goto errout;
1656 } else {
1657 allocated = true;
1658 }
1659 } else {
1660 allocated = true;
1661 }
1662 }
1663 }
1664
1665 ret = ext4_es_insert_delayed_block(inode, lblk, allocated);
1666
1667errout:
1668 return ret;
1669}
1670
1671/*
Aditya Kali5356f2612011-09-09 19:20:51 -04001672 * This function is grabs code from the very beginning of
1673 * ext4_map_blocks, but assumes that the caller is from delayed write
1674 * time. This function looks up the requested blocks and sets the
1675 * buffer delay bit under the protection of i_data_sem.
1676 */
1677static int ext4_da_map_blocks(struct inode *inode, sector_t iblock,
1678 struct ext4_map_blocks *map,
1679 struct buffer_head *bh)
1680{
Zheng Liud100eef2013-02-18 00:29:59 -05001681 struct extent_status es;
Aditya Kali5356f2612011-09-09 19:20:51 -04001682 int retval;
1683 sector_t invalid_block = ~((sector_t) 0xffff);
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001684#ifdef ES_AGGRESSIVE_TEST
1685 struct ext4_map_blocks orig_map;
1686
1687 memcpy(&orig_map, map, sizeof(*map));
1688#endif
Aditya Kali5356f2612011-09-09 19:20:51 -04001689
1690 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
1691 invalid_block = ~0;
1692
1693 map->m_flags = 0;
1694 ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
1695 "logical block %lu\n", inode->i_ino, map->m_len,
1696 (unsigned long) map->m_lblk);
Zheng Liud100eef2013-02-18 00:29:59 -05001697
1698 /* Lookup extent status tree firstly */
Theodore Ts'obb5835e2019-08-11 16:32:41 -04001699 if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) {
Zheng Liud100eef2013-02-18 00:29:59 -05001700 if (ext4_es_is_hole(&es)) {
1701 retval = 0;
Lukas Czernerc8b459f2014-05-12 12:55:07 -04001702 down_read(&EXT4_I(inode)->i_data_sem);
Zheng Liud100eef2013-02-18 00:29:59 -05001703 goto add_delayed;
1704 }
1705
1706 /*
1707 * Delayed extent could be allocated by fallocate.
1708 * So we need to check it.
1709 */
1710 if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) {
1711 map_bh(bh, inode->i_sb, invalid_block);
1712 set_buffer_new(bh);
1713 set_buffer_delay(bh);
1714 return 0;
1715 }
1716
1717 map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk;
1718 retval = es.es_len - (iblock - es.es_lblk);
1719 if (retval > map->m_len)
1720 retval = map->m_len;
1721 map->m_len = retval;
1722 if (ext4_es_is_written(&es))
1723 map->m_flags |= EXT4_MAP_MAPPED;
1724 else if (ext4_es_is_unwritten(&es))
1725 map->m_flags |= EXT4_MAP_UNWRITTEN;
1726 else
Arnd Bergmann1e83bc82019-04-07 12:24:43 -04001727 BUG();
Zheng Liud100eef2013-02-18 00:29:59 -05001728
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001729#ifdef ES_AGGRESSIVE_TEST
1730 ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0);
1731#endif
Zheng Liud100eef2013-02-18 00:29:59 -05001732 return retval;
1733 }
1734
Aditya Kali5356f2612011-09-09 19:20:51 -04001735 /*
1736 * Try to see if we can get the block without requesting a new
1737 * file system block.
1738 */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04001739 down_read(&EXT4_I(inode)->i_data_sem);
Jan Karacbd75842014-11-25 11:41:49 -05001740 if (ext4_has_inline_data(inode))
Tao Ma9c3569b2012-12-10 14:05:57 -05001741 retval = 0;
Jan Karacbd75842014-11-25 11:41:49 -05001742 else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Zheng Liu2f8e0a72014-11-25 11:44:37 -05001743 retval = ext4_ext_map_blocks(NULL, inode, map, 0);
Aditya Kali5356f2612011-09-09 19:20:51 -04001744 else
Zheng Liu2f8e0a72014-11-25 11:44:37 -05001745 retval = ext4_ind_map_blocks(NULL, inode, map, 0);
Aditya Kali5356f2612011-09-09 19:20:51 -04001746
Zheng Liud100eef2013-02-18 00:29:59 -05001747add_delayed:
Aditya Kali5356f2612011-09-09 19:20:51 -04001748 if (retval == 0) {
Zheng Liuf7fec032013-02-18 00:28:47 -05001749 int ret;
Eric Whitneyad431022018-10-01 14:10:39 -04001750
Aditya Kali5356f2612011-09-09 19:20:51 -04001751 /*
1752 * XXX: __block_prepare_write() unmaps passed block,
1753 * is it OK?
1754 */
Aditya Kali5356f2612011-09-09 19:20:51 -04001755
Eric Whitney0b02f4c2018-10-01 14:19:37 -04001756 ret = ext4_insert_delayed_block(inode, map->m_lblk);
1757 if (ret != 0) {
Zheng Liuf7fec032013-02-18 00:28:47 -05001758 retval = ret;
Zheng Liu51865fd2012-11-08 21:57:32 -05001759 goto out_unlock;
Zheng Liuf7fec032013-02-18 00:28:47 -05001760 }
Zheng Liu51865fd2012-11-08 21:57:32 -05001761
Aditya Kali5356f2612011-09-09 19:20:51 -04001762 map_bh(bh, inode->i_sb, invalid_block);
1763 set_buffer_new(bh);
1764 set_buffer_delay(bh);
Zheng Liuf7fec032013-02-18 00:28:47 -05001765 } else if (retval > 0) {
1766 int ret;
Theodore Ts'o3be78c72013-08-16 21:22:41 -04001767 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -05001768
Zheng Liu44fb851d2013-07-29 12:51:42 -04001769 if (unlikely(retval != map->m_len)) {
1770 ext4_warning(inode->i_sb,
1771 "ES len assertion failed for inode "
1772 "%lu: retval %d != map->m_len %d",
1773 inode->i_ino, retval, map->m_len);
1774 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001775 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001776
Zheng Liuf7fec032013-02-18 00:28:47 -05001777 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
1778 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
1779 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
1780 map->m_pblk, status);
1781 if (ret != 0)
1782 retval = ret;
Aditya Kali5356f2612011-09-09 19:20:51 -04001783 }
1784
1785out_unlock:
1786 up_read((&EXT4_I(inode)->i_data_sem));
1787
1788 return retval;
1789}
1790
1791/*
Seunghun Leed91bd2c2014-09-01 22:15:30 -04001792 * This is a special get_block_t callback which is used by
Theodore Ts'ob920c752009-05-14 00:54:29 -04001793 * ext4_da_write_begin(). It will either return mapped block or
1794 * reserve space for a single block.
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001795 *
1796 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
1797 * We also have b_blocknr = -1 and b_bdev initialized properly
1798 *
1799 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
1800 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
1801 * initialized properly.
Alex Tomas64769242008-07-11 19:27:31 -04001802 */
Tao Ma9c3569b2012-12-10 14:05:57 -05001803int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
1804 struct buffer_head *bh, int create)
Alex Tomas64769242008-07-11 19:27:31 -04001805{
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001806 struct ext4_map_blocks map;
Alex Tomas64769242008-07-11 19:27:31 -04001807 int ret = 0;
1808
1809 BUG_ON(create == 0);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001810 BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
1811
1812 map.m_lblk = iblock;
1813 map.m_len = 1;
Alex Tomas64769242008-07-11 19:27:31 -04001814
1815 /*
1816 * first, we need to know whether the block is allocated already
1817 * preallocated blocks are unmapped but should treated
1818 * the same as allocated blocks.
1819 */
Aditya Kali5356f2612011-09-09 19:20:51 -04001820 ret = ext4_da_map_blocks(inode, iblock, &map, bh);
1821 if (ret <= 0)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001822 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04001823
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001824 map_bh(bh, inode->i_sb, map.m_pblk);
Jan Karaed8ad832016-02-19 00:18:25 -05001825 ext4_update_bh_state(bh, map.m_flags);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001826
1827 if (buffer_unwritten(bh)) {
1828 /* A delayed write to unwritten bh should be marked
1829 * new and mapped. Mapped ensures that we don't do
1830 * get_block multiple times when we write to the same
1831 * offset and new ensures that we do proper zero out
1832 * for partial write.
1833 */
1834 set_buffer_new(bh);
Theodore Ts'oc8205632011-04-10 22:30:07 -04001835 set_buffer_mapped(bh);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001836 }
1837 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04001838}
Mingming Cao61628a32008-07-11 19:27:31 -04001839
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001840static int bget_one(handle_t *handle, struct buffer_head *bh)
1841{
1842 get_bh(bh);
1843 return 0;
1844}
1845
1846static int bput_one(handle_t *handle, struct buffer_head *bh)
1847{
1848 put_bh(bh);
1849 return 0;
1850}
1851
1852static int __ext4_journalled_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001853 unsigned int len)
1854{
1855 struct address_space *mapping = page->mapping;
1856 struct inode *inode = mapping->host;
Tao Ma3fdcfb62012-12-10 14:05:57 -05001857 struct buffer_head *page_bufs = NULL;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001858 handle_t *handle = NULL;
Tao Ma3fdcfb62012-12-10 14:05:57 -05001859 int ret = 0, err = 0;
1860 int inline_data = ext4_has_inline_data(inode);
1861 struct buffer_head *inode_bh = NULL;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001862
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001863 ClearPageChecked(page);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001864
1865 if (inline_data) {
1866 BUG_ON(page->index != 0);
1867 BUG_ON(len > ext4_get_max_inline_size(inode));
1868 inode_bh = ext4_journalled_write_inline_data(inode, len, page);
1869 if (inode_bh == NULL)
1870 goto out;
1871 } else {
1872 page_bufs = page_buffers(page);
1873 if (!page_bufs) {
1874 BUG();
1875 goto out;
1876 }
1877 ext4_walk_page_buffers(handle, page_bufs, 0, len,
1878 NULL, bget_one);
1879 }
Theodore Ts'obdf96832015-06-12 23:45:33 -04001880 /*
1881 * We need to release the page lock before we start the
1882 * journal, so grab a reference so the page won't disappear
1883 * out from under us.
1884 */
1885 get_page(page);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001886 unlock_page(page);
1887
Theodore Ts'o9924a922013-02-08 21:59:22 -05001888 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
1889 ext4_writepage_trans_blocks(inode));
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001890 if (IS_ERR(handle)) {
1891 ret = PTR_ERR(handle);
Theodore Ts'obdf96832015-06-12 23:45:33 -04001892 put_page(page);
1893 goto out_no_pagelock;
1894 }
1895 BUG_ON(!ext4_handle_valid(handle));
1896
1897 lock_page(page);
1898 put_page(page);
1899 if (page->mapping != mapping) {
1900 /* The page got truncated from under us */
1901 ext4_journal_stop(handle);
1902 ret = 0;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001903 goto out;
1904 }
1905
Tao Ma3fdcfb62012-12-10 14:05:57 -05001906 if (inline_data) {
Theodore Ts'o362eca72018-07-10 01:07:43 -04001907 ret = ext4_mark_inode_dirty(handle, inode);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001908 } else {
1909 ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
1910 do_journal_get_write_access);
1911
1912 err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
1913 write_end_fn);
1914 }
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001915 if (ret == 0)
1916 ret = err;
Jan Kara2d859db2011-07-26 09:07:11 -04001917 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001918 err = ext4_journal_stop(handle);
1919 if (!ret)
1920 ret = err;
1921
Tao Ma3fdcfb62012-12-10 14:05:57 -05001922 if (!ext4_has_inline_data(inode))
Theodore Ts'o8c9367f2014-01-07 13:08:03 -05001923 ext4_walk_page_buffers(NULL, page_bufs, 0, len,
Tao Ma3fdcfb62012-12-10 14:05:57 -05001924 NULL, bput_one);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001925 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001926out:
Theodore Ts'obdf96832015-06-12 23:45:33 -04001927 unlock_page(page);
1928out_no_pagelock:
Tao Ma3fdcfb62012-12-10 14:05:57 -05001929 brelse(inode_bh);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001930 return ret;
1931}
1932
Mingming Cao61628a32008-07-11 19:27:31 -04001933/*
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001934 * Note that we don't need to start a transaction unless we're journaling data
1935 * because we should have holes filled from ext4_page_mkwrite(). We even don't
1936 * need to file the inode to the transaction's list in ordered mode because if
1937 * we are writing back data added by write(), the inode is already there and if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001938 * we are writing back data modified via mmap(), no one guarantees in which
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001939 * transaction the data will hit the disk. In case we are journaling data, we
1940 * cannot start transaction directly because transaction start ranks above page
1941 * lock so we have to do some magic.
1942 *
Theodore Ts'ob920c752009-05-14 00:54:29 -04001943 * This function can get called via...
Theodore Ts'o20970ba2013-06-06 14:00:46 -04001944 * - ext4_writepages after taking page lock (have journal handle)
Theodore Ts'ob920c752009-05-14 00:54:29 -04001945 * - journal_submit_inode_data_buffers (no journal handle)
Artem Bityutskiyf6463b02012-07-25 18:12:04 +03001946 * - shrink_page_list via the kswapd/direct reclaim (no journal handle)
Theodore Ts'ob920c752009-05-14 00:54:29 -04001947 * - grab_page_cache when doing write_begin (have journal handle)
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001948 *
1949 * We don't do any block allocation in this function. If we have page with
1950 * multiple blocks we need to write those buffer_heads that are mapped. This
1951 * is important for mmaped based write. So if we do with blocksize 1K
1952 * truncate(f, 1024);
1953 * a = mmap(f, 0, 4096);
1954 * a[0] = 'a';
1955 * truncate(f, 4096);
1956 * we have in the page first buffer_head mapped via page_mkwrite call back
Paul Bolle90802ed2011-12-05 13:00:34 +01001957 * but other buffer_heads would be unmapped but dirty (dirty done via the
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001958 * do_wp_page). So writepage should write the first block. If we modify
1959 * the mmap area beyond 1024 we will again get a page_fault and the
1960 * page_mkwrite callback will do the block allocation and mark the
1961 * buffer_heads mapped.
1962 *
1963 * We redirty the page if we have any buffer_heads that is either delay or
1964 * unwritten in the page.
1965 *
1966 * We can get recursively called as show below.
1967 *
1968 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
1969 * ext4_writepage()
1970 *
1971 * But since we don't do any block allocation we should not deadlock.
1972 * Page also have the dirty flag cleared so we don't get recurive page_lock.
Mingming Cao61628a32008-07-11 19:27:31 -04001973 */
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001974static int ext4_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001975 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04001976{
Jan Karaf8bec372013-01-28 12:55:08 -05001977 int ret = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04001978 loff_t size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001979 unsigned int len;
Jiaying Zhang744692d2010-03-04 16:14:02 -05001980 struct buffer_head *page_bufs = NULL;
Mingming Cao61628a32008-07-11 19:27:31 -04001981 struct inode *inode = page->mapping->host;
Jan Kara36ade452013-01-28 09:30:52 -05001982 struct ext4_io_submit io_submit;
Namjae Jeon1c8349a2014-05-12 08:12:25 -04001983 bool keep_towrite = false;
Alex Tomas64769242008-07-11 19:27:31 -04001984
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05001985 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) {
yangerkunc2a559b2020-02-26 12:10:02 +08001986 inode->i_mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05001987 unlock_page(page);
1988 return -EIO;
1989 }
1990
Lukas Czernera9c667f2011-06-06 09:51:52 -04001991 trace_ext4_writepage(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04001992 size = i_size_read(inode);
Eric Biggersc93d8f82019-07-22 09:26:24 -07001993 if (page->index == size >> PAGE_SHIFT &&
1994 !ext4_verity_in_progress(inode))
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001995 len = size & ~PAGE_MASK;
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04001996 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001997 len = PAGE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04001998
Theodore Ts'oa42afc52010-10-27 21:30:09 -04001999 page_bufs = page_buffers(page);
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002000 /*
Jan Karafe386132013-01-28 21:06:42 -05002001 * We cannot do block allocation or other extent handling in this
2002 * function. If there are buffers needing that, we have to redirty
2003 * the page. But we may reach here when we do a journal commit via
2004 * journal_submit_inode_data_buffers() and in that case we must write
2005 * allocated buffers to achieve data=ordered mode guarantees.
Theodore Ts'occcd1472015-10-03 10:49:23 -04002006 *
2007 * Also, if there is only one buffer per page (the fs block
2008 * size == the page size), if one buffer needs block
2009 * allocation or needs to modify the extent tree to clear the
2010 * unwritten flag, we know that the page can't be written at
2011 * all, so we might as well refuse the write immediately.
2012 * Unfortunately if the block size != page size, we can't as
2013 * easily detect this case using ext4_walk_page_buffers(), but
2014 * for the extremely common case, this is an optimization that
2015 * skips a useless round trip through ext4_bio_write_page().
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04002016 */
Tao Maf19d5872012-12-10 14:05:51 -05002017 if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL,
2018 ext4_bh_delay_or_unwritten)) {
Jan Karaf8bec372013-01-28 12:55:08 -05002019 redirty_page_for_writepage(wbc, page);
Theodore Ts'occcd1472015-10-03 10:49:23 -04002020 if ((current->flags & PF_MEMALLOC) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002021 (inode->i_sb->s_blocksize == PAGE_SIZE)) {
Jan Karafe386132013-01-28 21:06:42 -05002022 /*
2023 * For memory cleaning there's no point in writing only
2024 * some buffers. So just bail out. Warn if we came here
2025 * from direct reclaim.
2026 */
2027 WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD))
2028 == PF_MEMALLOC);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002029 unlock_page(page);
2030 return 0;
2031 }
Namjae Jeon1c8349a2014-05-12 08:12:25 -04002032 keep_towrite = true;
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002033 }
Alex Tomas64769242008-07-11 19:27:31 -04002034
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002035 if (PageChecked(page) && ext4_should_journal_data(inode))
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002036 /*
2037 * It's mmapped pagecache. Add buffers and journal it. There
2038 * doesn't seem much point in redirtying the page here.
2039 */
Wu Fengguang3f0ca302009-11-24 11:15:44 -05002040 return __ext4_journalled_writepage(page, len);
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002041
Jan Kara97a851e2013-06-04 11:58:58 -04002042 ext4_io_submit_init(&io_submit, wbc);
2043 io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS);
2044 if (!io_submit.io_end) {
2045 redirty_page_for_writepage(wbc, page);
2046 unlock_page(page);
2047 return -ENOMEM;
2048 }
Namjae Jeon1c8349a2014-05-12 08:12:25 -04002049 ret = ext4_bio_write_page(&io_submit, page, len, wbc, keep_towrite);
Jan Kara36ade452013-01-28 09:30:52 -05002050 ext4_io_submit(&io_submit);
Jan Kara97a851e2013-06-04 11:58:58 -04002051 /* Drop io_end reference we got from init */
2052 ext4_put_io_end_defer(io_submit.io_end);
Alex Tomas64769242008-07-11 19:27:31 -04002053 return ret;
2054}
2055
Jan Kara5f1132b2013-08-17 10:02:33 -04002056static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page)
2057{
2058 int len;
Jan Karaa056bda2017-05-26 17:45:45 -04002059 loff_t size;
Jan Kara5f1132b2013-08-17 10:02:33 -04002060 int err;
2061
2062 BUG_ON(page->index != mpd->first_page);
Jan Karaa056bda2017-05-26 17:45:45 -04002063 clear_page_dirty_for_io(page);
2064 /*
2065 * We have to be very careful here! Nothing protects writeback path
2066 * against i_size changes and the page can be writeably mapped into
2067 * page tables. So an application can be growing i_size and writing
2068 * data through mmap while writeback runs. clear_page_dirty_for_io()
2069 * write-protects our page in page tables and the page cannot get
2070 * written to again until we release page lock. So only after
2071 * clear_page_dirty_for_io() we are safe to sample i_size for
2072 * ext4_bio_write_page() to zero-out tail of the written page. We rely
2073 * on the barrier provided by TestClearPageDirty in
2074 * clear_page_dirty_for_io() to make sure i_size is really sampled only
2075 * after page tables are updated.
2076 */
2077 size = i_size_read(mpd->inode);
Eric Biggersc93d8f82019-07-22 09:26:24 -07002078 if (page->index == size >> PAGE_SHIFT &&
2079 !ext4_verity_in_progress(mpd->inode))
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002080 len = size & ~PAGE_MASK;
Jan Kara5f1132b2013-08-17 10:02:33 -04002081 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002082 len = PAGE_SIZE;
Namjae Jeon1c8349a2014-05-12 08:12:25 -04002083 err = ext4_bio_write_page(&mpd->io_submit, page, len, mpd->wbc, false);
Jan Kara5f1132b2013-08-17 10:02:33 -04002084 if (!err)
2085 mpd->wbc->nr_to_write--;
2086 mpd->first_page++;
2087
2088 return err;
2089}
2090
Ritesh Harjani6db07462020-05-10 11:54:51 +05302091#define BH_FLAGS (BIT(BH_Unwritten) | BIT(BH_Delay))
Jan Kara4e7ea812013-06-04 13:17:40 -04002092
Mingming Cao61628a32008-07-11 19:27:31 -04002093/*
Jan Karafffb2732013-06-04 13:01:11 -04002094 * mballoc gives us at most this number of blocks...
2095 * XXX: That seems to be only a limitation of ext4_mb_normalize_request().
Anatol Pomozov70261f52013-08-28 14:40:12 -04002096 * The rest of mballoc seems to handle chunks up to full group size.
Mingming Cao61628a32008-07-11 19:27:31 -04002097 */
Jan Karafffb2732013-06-04 13:01:11 -04002098#define MAX_WRITEPAGES_EXTENT_LEN 2048
Mingming Cao525f4ed2008-08-19 22:15:58 -04002099
Jan Karafffb2732013-06-04 13:01:11 -04002100/*
Jan Kara4e7ea812013-06-04 13:17:40 -04002101 * mpage_add_bh_to_extent - try to add bh to extent of blocks to map
2102 *
2103 * @mpd - extent of blocks
2104 * @lblk - logical number of the block in the file
Jan Kara09930042013-08-17 09:57:56 -04002105 * @bh - buffer head we want to add to the extent
Jan Kara4e7ea812013-06-04 13:17:40 -04002106 *
Jan Kara09930042013-08-17 09:57:56 -04002107 * The function is used to collect contig. blocks in the same state. If the
2108 * buffer doesn't require mapping for writeback and we haven't started the
2109 * extent of buffers to map yet, the function returns 'true' immediately - the
2110 * caller can write the buffer right away. Otherwise the function returns true
2111 * if the block has been added to the extent, false if the block couldn't be
2112 * added.
Jan Kara4e7ea812013-06-04 13:17:40 -04002113 */
Jan Kara09930042013-08-17 09:57:56 -04002114static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk,
2115 struct buffer_head *bh)
Jan Kara4e7ea812013-06-04 13:17:40 -04002116{
2117 struct ext4_map_blocks *map = &mpd->map;
2118
Jan Kara09930042013-08-17 09:57:56 -04002119 /* Buffer that doesn't need mapping for writeback? */
2120 if (!buffer_dirty(bh) || !buffer_mapped(bh) ||
2121 (!buffer_delay(bh) && !buffer_unwritten(bh))) {
2122 /* So far no extent to map => we write the buffer right away */
2123 if (map->m_len == 0)
2124 return true;
2125 return false;
2126 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002127
2128 /* First block in the extent? */
2129 if (map->m_len == 0) {
Jan Karadddbd6a2017-04-30 18:29:10 -04002130 /* We cannot map unless handle is started... */
2131 if (!mpd->do_map)
2132 return false;
Jan Kara4e7ea812013-06-04 13:17:40 -04002133 map->m_lblk = lblk;
2134 map->m_len = 1;
Jan Kara09930042013-08-17 09:57:56 -04002135 map->m_flags = bh->b_state & BH_FLAGS;
2136 return true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002137 }
2138
Jan Kara09930042013-08-17 09:57:56 -04002139 /* Don't go larger than mballoc is willing to allocate */
2140 if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN)
2141 return false;
2142
Jan Kara4e7ea812013-06-04 13:17:40 -04002143 /* Can we merge the block to our big extent? */
2144 if (lblk == map->m_lblk + map->m_len &&
Jan Kara09930042013-08-17 09:57:56 -04002145 (bh->b_state & BH_FLAGS) == map->m_flags) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002146 map->m_len++;
Jan Kara09930042013-08-17 09:57:56 -04002147 return true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002148 }
Jan Kara09930042013-08-17 09:57:56 -04002149 return false;
Jan Kara4e7ea812013-06-04 13:17:40 -04002150}
2151
Jan Kara5f1132b2013-08-17 10:02:33 -04002152/*
2153 * mpage_process_page_bufs - submit page buffers for IO or add them to extent
2154 *
2155 * @mpd - extent of blocks for mapping
2156 * @head - the first buffer in the page
2157 * @bh - buffer we should start processing from
2158 * @lblk - logical number of the block in the file corresponding to @bh
2159 *
2160 * Walk through page buffers from @bh upto @head (exclusive) and either submit
2161 * the page for IO if all buffers in this page were mapped and there's no
2162 * accumulated extent of buffers to map or add buffers in the page to the
2163 * extent of buffers to map. The function returns 1 if the caller can continue
2164 * by processing the next page, 0 if it should stop adding buffers to the
2165 * extent to map because we cannot extend it anymore. It can also return value
2166 * < 0 in case of error during IO submission.
2167 */
2168static int mpage_process_page_bufs(struct mpage_da_data *mpd,
2169 struct buffer_head *head,
2170 struct buffer_head *bh,
2171 ext4_lblk_t lblk)
Jan Kara4e7ea812013-06-04 13:17:40 -04002172{
2173 struct inode *inode = mpd->inode;
Jan Kara5f1132b2013-08-17 10:02:33 -04002174 int err;
Fabian Frederick93407472017-02-27 14:28:32 -08002175 ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(inode) - 1)
Jan Kara4e7ea812013-06-04 13:17:40 -04002176 >> inode->i_blkbits;
2177
Eric Biggersc93d8f82019-07-22 09:26:24 -07002178 if (ext4_verity_in_progress(inode))
2179 blocks = EXT_MAX_BLOCKS;
2180
Jan Kara4e7ea812013-06-04 13:17:40 -04002181 do {
2182 BUG_ON(buffer_locked(bh));
2183
Jan Kara09930042013-08-17 09:57:56 -04002184 if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002185 /* Found extent to map? */
2186 if (mpd->map.m_len)
Jan Kara5f1132b2013-08-17 10:02:33 -04002187 return 0;
Jan Karadddbd6a2017-04-30 18:29:10 -04002188 /* Buffer needs mapping and handle is not started? */
2189 if (!mpd->do_map)
2190 return 0;
Jan Kara09930042013-08-17 09:57:56 -04002191 /* Everything mapped so far and we hit EOF */
Jan Kara5f1132b2013-08-17 10:02:33 -04002192 break;
Jan Kara4e7ea812013-06-04 13:17:40 -04002193 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002194 } while (lblk++, (bh = bh->b_this_page) != head);
Jan Kara5f1132b2013-08-17 10:02:33 -04002195 /* So far everything mapped? Submit the page for IO. */
2196 if (mpd->map.m_len == 0) {
2197 err = mpage_submit_page(mpd, head->b_page);
2198 if (err < 0)
2199 return err;
2200 }
2201 return lblk < blocks;
Jan Kara4e7ea812013-06-04 13:17:40 -04002202}
2203
2204/*
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302205 * mpage_process_page - update page buffers corresponding to changed extent and
2206 * may submit fully mapped page for IO
2207 *
2208 * @mpd - description of extent to map, on return next extent to map
2209 * @m_lblk - logical block mapping.
2210 * @m_pblk - corresponding physical mapping.
2211 * @map_bh - determines on return whether this page requires any further
2212 * mapping or not.
2213 * Scan given page buffers corresponding to changed extent and update buffer
2214 * state according to new extent state.
2215 * We map delalloc buffers to their physical location, clear unwritten bits.
2216 * If the given page is not fully mapped, we update @map to the next extent in
2217 * the given page that needs mapping & return @map_bh as true.
2218 */
2219static int mpage_process_page(struct mpage_da_data *mpd, struct page *page,
2220 ext4_lblk_t *m_lblk, ext4_fsblk_t *m_pblk,
2221 bool *map_bh)
2222{
2223 struct buffer_head *head, *bh;
2224 ext4_io_end_t *io_end = mpd->io_submit.io_end;
2225 ext4_lblk_t lblk = *m_lblk;
2226 ext4_fsblk_t pblock = *m_pblk;
2227 int err = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302228 int blkbits = mpd->inode->i_blkbits;
2229 ssize_t io_end_size = 0;
2230 struct ext4_io_end_vec *io_end_vec = ext4_last_io_end_vec(io_end);
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302231
2232 bh = head = page_buffers(page);
2233 do {
2234 if (lblk < mpd->map.m_lblk)
2235 continue;
2236 if (lblk >= mpd->map.m_lblk + mpd->map.m_len) {
2237 /*
2238 * Buffer after end of mapped extent.
2239 * Find next buffer in the page to map.
2240 */
2241 mpd->map.m_len = 0;
2242 mpd->map.m_flags = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302243 io_end_vec->size += io_end_size;
2244 io_end_size = 0;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302245
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302246 err = mpage_process_page_bufs(mpd, head, bh, lblk);
2247 if (err > 0)
2248 err = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302249 if (!err && mpd->map.m_len && mpd->map.m_lblk > lblk) {
2250 io_end_vec = ext4_alloc_io_end_vec(io_end);
Ritesh Harjani4d06bfb2019-11-06 15:08:09 +05302251 if (IS_ERR(io_end_vec)) {
2252 err = PTR_ERR(io_end_vec);
2253 goto out;
2254 }
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302255 io_end_vec->offset = mpd->map.m_lblk << blkbits;
2256 }
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302257 *map_bh = true;
2258 goto out;
2259 }
2260 if (buffer_delay(bh)) {
2261 clear_buffer_delay(bh);
2262 bh->b_blocknr = pblock++;
2263 }
2264 clear_buffer_unwritten(bh);
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302265 io_end_size += (1 << blkbits);
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302266 } while (lblk++, (bh = bh->b_this_page) != head);
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302267
2268 io_end_vec->size += io_end_size;
2269 io_end_size = 0;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302270 *map_bh = false;
2271out:
2272 *m_lblk = lblk;
2273 *m_pblk = pblock;
2274 return err;
2275}
2276
2277/*
Jan Kara4e7ea812013-06-04 13:17:40 -04002278 * mpage_map_buffers - update buffers corresponding to changed extent and
2279 * submit fully mapped pages for IO
2280 *
2281 * @mpd - description of extent to map, on return next extent to map
2282 *
2283 * Scan buffers corresponding to changed extent (we expect corresponding pages
2284 * to be already locked) and update buffer state according to new extent state.
2285 * We map delalloc buffers to their physical location, clear unwritten bits,
Lukas Czerner556615d2014-04-20 23:45:47 -04002286 * and mark buffers as uninit when we perform writes to unwritten extents
Jan Kara4e7ea812013-06-04 13:17:40 -04002287 * and do extent conversion after IO is finished. If the last page is not fully
2288 * mapped, we update @map to the next extent in the last page that needs
2289 * mapping. Otherwise we submit the page for IO.
2290 */
2291static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd)
2292{
2293 struct pagevec pvec;
2294 int nr_pages, i;
2295 struct inode *inode = mpd->inode;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002296 int bpp_bits = PAGE_SHIFT - inode->i_blkbits;
Jan Kara4e7ea812013-06-04 13:17:40 -04002297 pgoff_t start, end;
2298 ext4_lblk_t lblk;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302299 ext4_fsblk_t pblock;
Jan Kara4e7ea812013-06-04 13:17:40 -04002300 int err;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302301 bool map_bh = false;
Jan Kara4e7ea812013-06-04 13:17:40 -04002302
2303 start = mpd->map.m_lblk >> bpp_bits;
2304 end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits;
2305 lblk = start << bpp_bits;
2306 pblock = mpd->map.m_pblk;
2307
Mel Gorman86679822017-11-15 17:37:52 -08002308 pagevec_init(&pvec);
Jan Kara4e7ea812013-06-04 13:17:40 -04002309 while (start <= end) {
Jan Kara2b85a612017-09-06 16:21:30 -07002310 nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping,
Jan Kara397162f2017-09-06 16:21:43 -07002311 &start, end);
Jan Kara4e7ea812013-06-04 13:17:40 -04002312 if (nr_pages == 0)
2313 break;
2314 for (i = 0; i < nr_pages; i++) {
2315 struct page *page = pvec.pages[i];
2316
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302317 err = mpage_process_page(mpd, page, &lblk, &pblock,
2318 &map_bh);
Jan Kara4e7ea812013-06-04 13:17:40 -04002319 /*
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302320 * If map_bh is true, means page may require further bh
2321 * mapping, or maybe the page was submitted for IO.
2322 * So we return to call further extent mapping.
Jan Kara4e7ea812013-06-04 13:17:40 -04002323 */
Jason Yan39c0ae12020-04-20 12:29:18 +08002324 if (err < 0 || map_bh)
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302325 goto out;
Jan Kara4e7ea812013-06-04 13:17:40 -04002326 /* Page fully mapped - let IO run! */
2327 err = mpage_submit_page(mpd, page);
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302328 if (err < 0)
2329 goto out;
Jan Kara4e7ea812013-06-04 13:17:40 -04002330 }
2331 pagevec_release(&pvec);
2332 }
2333 /* Extent fully mapped and matches with page boundary. We are done. */
2334 mpd->map.m_len = 0;
2335 mpd->map.m_flags = 0;
2336 return 0;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302337out:
2338 pagevec_release(&pvec);
2339 return err;
Jan Kara4e7ea812013-06-04 13:17:40 -04002340}
2341
2342static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd)
2343{
2344 struct inode *inode = mpd->inode;
2345 struct ext4_map_blocks *map = &mpd->map;
2346 int get_blocks_flags;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002347 int err, dioread_nolock;
Jan Kara4e7ea812013-06-04 13:17:40 -04002348
2349 trace_ext4_da_write_pages_extent(inode, map);
2350 /*
2351 * Call ext4_map_blocks() to allocate any delayed allocation blocks, or
Lukas Czerner556615d2014-04-20 23:45:47 -04002352 * to convert an unwritten extent to be initialized (in the case
Jan Kara4e7ea812013-06-04 13:17:40 -04002353 * where we have written into one or more preallocated blocks). It is
2354 * possible that we're going to need more metadata blocks than
2355 * previously reserved. However we must not fail because we're in
2356 * writeback and there is nothing we can do about it so it might result
2357 * in data loss. So use reserved blocks to allocate metadata if
2358 * possible.
2359 *
Theodore Ts'o754cfed2014-09-04 18:08:22 -04002360 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if
2361 * the blocks in question are delalloc blocks. This indicates
2362 * that the blocks and quotas has already been checked when
2363 * the data was copied into the page cache.
Jan Kara4e7ea812013-06-04 13:17:40 -04002364 */
2365 get_blocks_flags = EXT4_GET_BLOCKS_CREATE |
Jan Karaee0876b2016-04-24 00:56:08 -04002366 EXT4_GET_BLOCKS_METADATA_NOFAIL |
2367 EXT4_GET_BLOCKS_IO_SUBMIT;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002368 dioread_nolock = ext4_should_dioread_nolock(inode);
2369 if (dioread_nolock)
Jan Kara4e7ea812013-06-04 13:17:40 -04002370 get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
Ritesh Harjani6db07462020-05-10 11:54:51 +05302371 if (map->m_flags & BIT(BH_Delay))
Jan Kara4e7ea812013-06-04 13:17:40 -04002372 get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
2373
2374 err = ext4_map_blocks(handle, inode, map, get_blocks_flags);
2375 if (err < 0)
2376 return err;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002377 if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) {
Jan Kara6b523df2013-06-04 13:21:11 -04002378 if (!mpd->io_submit.io_end->handle &&
2379 ext4_handle_valid(handle)) {
2380 mpd->io_submit.io_end->handle = handle->h_rsv_handle;
2381 handle->h_rsv_handle = NULL;
2382 }
Jan Kara3613d222013-06-04 13:19:34 -04002383 ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end);
Jan Kara6b523df2013-06-04 13:21:11 -04002384 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002385
2386 BUG_ON(map->m_len == 0);
Jan Kara4e7ea812013-06-04 13:17:40 -04002387 return 0;
2388}
2389
2390/*
2391 * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length
2392 * mpd->len and submit pages underlying it for IO
2393 *
2394 * @handle - handle for journal operations
2395 * @mpd - extent to map
Jan Kara7534e852013-10-16 08:26:08 -04002396 * @give_up_on_write - we set this to true iff there is a fatal error and there
2397 * is no hope of writing the data. The caller should discard
2398 * dirty pages to avoid infinite loops.
Jan Kara4e7ea812013-06-04 13:17:40 -04002399 *
2400 * The function maps extent starting at mpd->lblk of length mpd->len. If it is
2401 * delayed, blocks are allocated, if it is unwritten, we may need to convert
2402 * them to initialized or split the described range from larger unwritten
2403 * extent. Note that we need not map all the described range since allocation
2404 * can return less blocks or the range is covered by more unwritten extents. We
2405 * cannot map more because we are limited by reserved transaction credits. On
2406 * the other hand we always make sure that the last touched page is fully
2407 * mapped so that it can be written out (and thus forward progress is
2408 * guaranteed). After mapping we submit all mapped pages for IO.
2409 */
2410static int mpage_map_and_submit_extent(handle_t *handle,
Theodore Ts'ocb530542013-07-01 08:12:40 -04002411 struct mpage_da_data *mpd,
2412 bool *give_up_on_write)
Jan Kara4e7ea812013-06-04 13:17:40 -04002413{
2414 struct inode *inode = mpd->inode;
2415 struct ext4_map_blocks *map = &mpd->map;
2416 int err;
2417 loff_t disksize;
Dmitry Monakhov66031202014-08-27 18:40:03 -04002418 int progress = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302419 ext4_io_end_t *io_end = mpd->io_submit.io_end;
Ritesh Harjani4d06bfb2019-11-06 15:08:09 +05302420 struct ext4_io_end_vec *io_end_vec;
Jan Kara4e7ea812013-06-04 13:17:40 -04002421
Ritesh Harjani4d06bfb2019-11-06 15:08:09 +05302422 io_end_vec = ext4_alloc_io_end_vec(io_end);
2423 if (IS_ERR(io_end_vec))
2424 return PTR_ERR(io_end_vec);
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302425 io_end_vec->offset = ((loff_t)map->m_lblk) << inode->i_blkbits;
Jan Kara27d7c4e2013-07-05 21:57:22 -04002426 do {
Jan Kara4e7ea812013-06-04 13:17:40 -04002427 err = mpage_map_one_extent(handle, mpd);
2428 if (err < 0) {
2429 struct super_block *sb = inode->i_sb;
2430
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002431 if (ext4_forced_shutdown(EXT4_SB(sb)) ||
2432 EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)
Theodore Ts'ocb530542013-07-01 08:12:40 -04002433 goto invalidate_dirty_pages;
Jan Kara4e7ea812013-06-04 13:17:40 -04002434 /*
Theodore Ts'ocb530542013-07-01 08:12:40 -04002435 * Let the uper layers retry transient errors.
2436 * In the case of ENOSPC, if ext4_count_free_blocks()
2437 * is non-zero, a commit should free up blocks.
Jan Kara4e7ea812013-06-04 13:17:40 -04002438 */
Theodore Ts'ocb530542013-07-01 08:12:40 -04002439 if ((err == -ENOMEM) ||
Dmitry Monakhov66031202014-08-27 18:40:03 -04002440 (err == -ENOSPC && ext4_count_free_clusters(sb))) {
2441 if (progress)
2442 goto update_disksize;
Theodore Ts'ocb530542013-07-01 08:12:40 -04002443 return err;
Dmitry Monakhov66031202014-08-27 18:40:03 -04002444 }
Theodore Ts'ocb530542013-07-01 08:12:40 -04002445 ext4_msg(sb, KERN_CRIT,
2446 "Delayed block allocation failed for "
2447 "inode %lu at logical offset %llu with"
2448 " max blocks %u with error %d",
2449 inode->i_ino,
2450 (unsigned long long)map->m_lblk,
2451 (unsigned)map->m_len, -err);
2452 ext4_msg(sb, KERN_CRIT,
2453 "This should not happen!! Data will "
2454 "be lost\n");
2455 if (err == -ENOSPC)
2456 ext4_print_free_blocks(inode);
2457 invalidate_dirty_pages:
2458 *give_up_on_write = true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002459 return err;
2460 }
Dmitry Monakhov66031202014-08-27 18:40:03 -04002461 progress = 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04002462 /*
2463 * Update buffer state, submit mapped pages, and get us new
2464 * extent to map
2465 */
2466 err = mpage_map_and_submit_buffers(mpd);
2467 if (err < 0)
Dmitry Monakhov66031202014-08-27 18:40:03 -04002468 goto update_disksize;
Jan Kara27d7c4e2013-07-05 21:57:22 -04002469 } while (map->m_len);
Jan Kara4e7ea812013-06-04 13:17:40 -04002470
Dmitry Monakhov66031202014-08-27 18:40:03 -04002471update_disksize:
Theodore Ts'o622cad12014-04-11 10:35:17 -04002472 /*
2473 * Update on-disk size after IO is submitted. Races with
2474 * truncate are avoided by checking i_size under i_data_sem.
2475 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002476 disksize = ((loff_t)mpd->first_page) << PAGE_SHIFT;
Qian Cai35df4292020-02-07 09:29:11 -05002477 if (disksize > READ_ONCE(EXT4_I(inode)->i_disksize)) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002478 int err2;
Theodore Ts'o622cad12014-04-11 10:35:17 -04002479 loff_t i_size;
Jan Kara4e7ea812013-06-04 13:17:40 -04002480
Theodore Ts'o622cad12014-04-11 10:35:17 -04002481 down_write(&EXT4_I(inode)->i_data_sem);
2482 i_size = i_size_read(inode);
2483 if (disksize > i_size)
2484 disksize = i_size;
2485 if (disksize > EXT4_I(inode)->i_disksize)
2486 EXT4_I(inode)->i_disksize = disksize;
Theodore Ts'o622cad12014-04-11 10:35:17 -04002487 up_write(&EXT4_I(inode)->i_data_sem);
Theodore Ts'ob907f2d2017-01-11 22:14:49 -05002488 err2 = ext4_mark_inode_dirty(handle, inode);
Theodore Ts'o878520a2019-11-19 21:54:15 -05002489 if (err2) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04002490 ext4_error_err(inode->i_sb, -err2,
2491 "Failed to mark inode %lu dirty",
2492 inode->i_ino);
Theodore Ts'o878520a2019-11-19 21:54:15 -05002493 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002494 if (!err)
2495 err = err2;
2496 }
2497 return err;
2498}
2499
2500/*
Jan Karafffb2732013-06-04 13:01:11 -04002501 * Calculate the total number of credits to reserve for one writepages
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002502 * iteration. This is called from ext4_writepages(). We map an extent of
Anatol Pomozov70261f52013-08-28 14:40:12 -04002503 * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping
Jan Karafffb2732013-06-04 13:01:11 -04002504 * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN +
2505 * bpp - 1 blocks in bpp different extents.
2506 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002507static int ext4_da_writepages_trans_blocks(struct inode *inode)
2508{
Jan Karafffb2732013-06-04 13:01:11 -04002509 int bpp = ext4_journal_blocks_per_page(inode);
Mingming Cao525f4ed2008-08-19 22:15:58 -04002510
Jan Karafffb2732013-06-04 13:01:11 -04002511 return ext4_meta_trans_blocks(inode,
2512 MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp);
Mingming Cao525f4ed2008-08-19 22:15:58 -04002513}
Mingming Cao61628a32008-07-11 19:27:31 -04002514
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002515/*
Jan Kara4e7ea812013-06-04 13:17:40 -04002516 * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages
2517 * and underlying extent to map
2518 *
2519 * @mpd - where to look for pages
2520 *
2521 * Walk dirty pages in the mapping. If they are fully mapped, submit them for
2522 * IO immediately. When we find a page which isn't mapped we start accumulating
2523 * extent of buffers underlying these pages that needs mapping (formed by
2524 * either delayed or unwritten buffers). We also lock the pages containing
2525 * these buffers. The extent found is returned in @mpd structure (starting at
2526 * mpd->lblk with length mpd->len blocks).
2527 *
2528 * Note that this function can attach bios to one io_end structure which are
2529 * neither logically nor physically contiguous. Although it may seem as an
2530 * unnecessary complication, it is actually inevitable in blocksize < pagesize
2531 * 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 -04002532 */
Jan Kara4e7ea812013-06-04 13:17:40 -04002533static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd)
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002534{
Jan Kara4e7ea812013-06-04 13:17:40 -04002535 struct address_space *mapping = mpd->inode->i_mapping;
2536 struct pagevec pvec;
2537 unsigned int nr_pages;
Ming Leiaeac5892013-10-17 18:56:16 -04002538 long left = mpd->wbc->nr_to_write;
Jan Kara4e7ea812013-06-04 13:17:40 -04002539 pgoff_t index = mpd->first_page;
2540 pgoff_t end = mpd->last_page;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05002541 xa_mark_t tag;
Jan Kara4e7ea812013-06-04 13:17:40 -04002542 int i, err = 0;
2543 int blkbits = mpd->inode->i_blkbits;
2544 ext4_lblk_t lblk;
2545 struct buffer_head *head;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002546
Jan Kara4e7ea812013-06-04 13:17:40 -04002547 if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages)
Eric Sandeen5b41d922010-10-27 21:30:13 -04002548 tag = PAGECACHE_TAG_TOWRITE;
2549 else
2550 tag = PAGECACHE_TAG_DIRTY;
2551
Mel Gorman86679822017-11-15 17:37:52 -08002552 pagevec_init(&pvec);
Jan Kara4e7ea812013-06-04 13:17:40 -04002553 mpd->map.m_len = 0;
2554 mpd->next_page = index;
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002555 while (index <= end) {
Jan Karadc7f3e82017-11-15 17:34:44 -08002556 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08002557 tag);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002558 if (nr_pages == 0)
Jan Kara4e7ea812013-06-04 13:17:40 -04002559 goto out;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002560
2561 for (i = 0; i < nr_pages; i++) {
2562 struct page *page = pvec.pages[i];
2563
2564 /*
Ming Leiaeac5892013-10-17 18:56:16 -04002565 * Accumulated enough dirty pages? This doesn't apply
2566 * to WB_SYNC_ALL mode. For integrity sync we have to
2567 * keep going because someone may be concurrently
2568 * dirtying pages, and we might have synced a lot of
2569 * newly appeared dirty pages, but have not synced all
2570 * of the old dirty pages.
2571 */
2572 if (mpd->wbc->sync_mode == WB_SYNC_NONE && left <= 0)
2573 goto out;
2574
Jan Kara4e7ea812013-06-04 13:17:40 -04002575 /* If we can't merge this page, we are done. */
2576 if (mpd->map.m_len > 0 && mpd->next_page != page->index)
2577 goto out;
Theodore Ts'o78aaced2011-02-26 14:09:14 -05002578
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002579 lock_page(page);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002580 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002581 * If the page is no longer dirty, or its mapping no
2582 * longer corresponds to inode we are writing (which
2583 * means it has been truncated or invalidated), or the
2584 * page is already under writeback and we are not doing
2585 * a data integrity writeback, skip the page
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002586 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002587 if (!PageDirty(page) ||
2588 (PageWriteback(page) &&
Jan Kara4e7ea812013-06-04 13:17:40 -04002589 (mpd->wbc->sync_mode == WB_SYNC_NONE)) ||
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002590 unlikely(page->mapping != mapping)) {
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002591 unlock_page(page);
2592 continue;
2593 }
2594
Darrick J. Wong7cb1a532011-05-18 13:53:20 -04002595 wait_on_page_writeback(page);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002596 BUG_ON(PageWriteback(page));
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002597
Jan Kara4e7ea812013-06-04 13:17:40 -04002598 if (mpd->map.m_len == 0)
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002599 mpd->first_page = page->index;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002600 mpd->next_page = page->index + 1;
Jan Karaf8bec372013-01-28 12:55:08 -05002601 /* Add all dirty buffers to mpd */
Jan Kara4e7ea812013-06-04 13:17:40 -04002602 lblk = ((ext4_lblk_t)page->index) <<
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002603 (PAGE_SHIFT - blkbits);
Jan Karaf8bec372013-01-28 12:55:08 -05002604 head = page_buffers(page);
Jan Kara5f1132b2013-08-17 10:02:33 -04002605 err = mpage_process_page_bufs(mpd, head, head, lblk);
2606 if (err <= 0)
Jan Kara4e7ea812013-06-04 13:17:40 -04002607 goto out;
Jan Kara5f1132b2013-08-17 10:02:33 -04002608 err = 0;
Ming Leiaeac5892013-10-17 18:56:16 -04002609 left--;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002610 }
2611 pagevec_release(&pvec);
2612 cond_resched();
2613 }
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002614 return 0;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002615out:
2616 pagevec_release(&pvec);
Jan Kara4e7ea812013-06-04 13:17:40 -04002617 return err;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002618}
2619
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002620static int ext4_writepages(struct address_space *mapping,
2621 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002622{
Jan Kara4e7ea812013-06-04 13:17:40 -04002623 pgoff_t writeback_index = 0;
2624 long nr_to_write = wbc->nr_to_write;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002625 int range_whole = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002626 int cycled = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002627 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002628 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002629 struct inode *inode = mapping->host;
Jan Kara6b523df2013-06-04 13:21:11 -04002630 int needed_blocks, rsv_blocks = 0, ret = 0;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002631 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Jan Kara4e7ea812013-06-04 13:17:40 -04002632 bool done;
Shaohua Li1bce63d12011-10-18 10:55:51 -04002633 struct blk_plug plug;
Theodore Ts'ocb530542013-07-01 08:12:40 -04002634 bool give_up_on_write = false;
Mingming Cao61628a32008-07-11 19:27:31 -04002635
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002636 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
2637 return -EIO;
2638
Eric Biggersbbd55932020-02-19 10:30:46 -08002639 percpu_down_read(&sbi->s_writepages_rwsem);
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002640 trace_ext4_writepages(inode, wbc);
Theodore Ts'oba80b102009-01-03 20:03:21 -05002641
Mingming Cao61628a32008-07-11 19:27:31 -04002642 /*
2643 * No pages to write? This is mainly a kludge to avoid starting
2644 * a transaction for special inodes like journal inode on last iput()
2645 * because that could violate lock ordering on umount
2646 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002647 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Ming Leibbf023c72013-10-30 07:27:16 -04002648 goto out_writepages;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002649
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002650 if (ext4_should_journal_data(inode)) {
Goldwyn Rodrigues043d20d2018-03-26 01:32:50 -04002651 ret = generic_writepages(mapping, wbc);
Ming Leibbf023c72013-10-30 07:27:16 -04002652 goto out_writepages;
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002653 }
2654
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002655 /*
2656 * If the filesystem has aborted, it is read-only, so return
2657 * right away instead of dumping stack traces later on that
2658 * will obscure the real source of the problem. We test
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002659 * EXT4_MF_FS_ABORTED instead of sb->s_flag's SB_RDONLY because
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002660 * the latter could be true if the filesystem is mounted
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002661 * read-only, and in that case, ext4_writepages should
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002662 * *never* be called, so if that ever happens, we would want
2663 * the stack trace.
2664 */
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002665 if (unlikely(ext4_forced_shutdown(EXT4_SB(mapping->host->i_sb)) ||
2666 sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) {
Ming Leibbf023c72013-10-30 07:27:16 -04002667 ret = -EROFS;
2668 goto out_writepages;
2669 }
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002670
Jan Kara4e7ea812013-06-04 13:17:40 -04002671 /*
2672 * If we have inline data and arrive here, it means that
2673 * we will soon create the block for the 1st page, so
2674 * we'd better clear the inline data here.
2675 */
2676 if (ext4_has_inline_data(inode)) {
2677 /* Just inode will be modified... */
2678 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
2679 if (IS_ERR(handle)) {
2680 ret = PTR_ERR(handle);
2681 goto out_writepages;
2682 }
2683 BUG_ON(ext4_test_inode_state(inode,
2684 EXT4_STATE_MAY_INLINE_DATA));
2685 ext4_destroy_inline_data(handle, inode);
2686 ext4_journal_stop(handle);
2687 }
2688
yangerkun4e343232019-08-11 16:27:41 -04002689 if (ext4_should_dioread_nolock(inode)) {
2690 /*
2691 * We may need to convert up to one extent per block in
2692 * the page and we may dirty the inode.
2693 */
2694 rsv_blocks = 1 + ext4_chunk_trans_blocks(inode,
2695 PAGE_SIZE >> inode->i_blkbits);
2696 }
2697
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002698 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2699 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002700
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002701 if (wbc->range_cyclic) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002702 writeback_index = mapping->writeback_index;
2703 if (writeback_index)
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002704 cycled = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002705 mpd.first_page = writeback_index;
2706 mpd.last_page = -1;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002707 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002708 mpd.first_page = wbc->range_start >> PAGE_SHIFT;
2709 mpd.last_page = wbc->range_end >> PAGE_SHIFT;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002710 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002711
Jan Kara4e7ea812013-06-04 13:17:40 -04002712 mpd.inode = inode;
2713 mpd.wbc = wbc;
2714 ext4_io_submit_init(&mpd.io_submit, wbc);
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002715retry:
Wu Fengguang6e6938b2010-06-06 10:38:15 -06002716 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Jan Kara4e7ea812013-06-04 13:17:40 -04002717 tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page);
2718 done = false;
Shaohua Li1bce63d12011-10-18 10:55:51 -04002719 blk_start_plug(&plug);
Jan Karadddbd6a2017-04-30 18:29:10 -04002720
2721 /*
2722 * First writeback pages that don't need mapping - we can avoid
2723 * starting a transaction unnecessarily and also avoid being blocked
2724 * in the block layer on device congestion while having transaction
2725 * started.
2726 */
2727 mpd.do_map = 0;
2728 mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
2729 if (!mpd.io_submit.io_end) {
2730 ret = -ENOMEM;
2731 goto unplug;
2732 }
2733 ret = mpage_prepare_extent_to_map(&mpd);
Xiaoguang Wanga297b2f2019-02-10 23:53:21 -05002734 /* Unlock pages we didn't use */
2735 mpage_release_unused_pages(&mpd, false);
Jan Karadddbd6a2017-04-30 18:29:10 -04002736 /* Submit prepared bio */
2737 ext4_io_submit(&mpd.io_submit);
2738 ext4_put_io_end_defer(mpd.io_submit.io_end);
2739 mpd.io_submit.io_end = NULL;
Jan Karadddbd6a2017-04-30 18:29:10 -04002740 if (ret < 0)
2741 goto unplug;
2742
Jan Kara4e7ea812013-06-04 13:17:40 -04002743 while (!done && mpd.first_page <= mpd.last_page) {
2744 /* For each extent of pages we use new io_end */
2745 mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
2746 if (!mpd.io_submit.io_end) {
2747 ret = -ENOMEM;
2748 break;
2749 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002750
2751 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002752 * We have two constraints: We find one extent to map and we
2753 * must always write out whole page (makes a difference when
2754 * blocksize < pagesize) so that we don't block on IO when we
2755 * try to write out the rest of the page. Journalled mode is
2756 * not supported by delalloc.
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002757 */
2758 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04002759 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002760
Jan Kara4e7ea812013-06-04 13:17:40 -04002761 /* start a new transaction */
Jan Kara6b523df2013-06-04 13:21:11 -04002762 handle = ext4_journal_start_with_reserve(inode,
2763 EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks);
Mingming Cao61628a32008-07-11 19:27:31 -04002764 if (IS_ERR(handle)) {
2765 ret = PTR_ERR(handle);
Theodore Ts'o16939182009-09-26 17:43:59 -04002766 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
Curt Wohlgemuthfbe845d2010-05-16 13:00:00 -04002767 "%ld pages, ino %lu; err %d", __func__,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002768 wbc->nr_to_write, inode->i_ino, ret);
Jan Kara4e7ea812013-06-04 13:17:40 -04002769 /* Release allocated io_end */
2770 ext4_put_io_end(mpd.io_submit.io_end);
Jan Karadddbd6a2017-04-30 18:29:10 -04002771 mpd.io_submit.io_end = NULL;
Jan Kara4e7ea812013-06-04 13:17:40 -04002772 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002773 }
Jan Karadddbd6a2017-04-30 18:29:10 -04002774 mpd.do_map = 1;
Theodore Ts'of63e60052009-02-23 16:42:39 -05002775
Jan Kara4e7ea812013-06-04 13:17:40 -04002776 trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc);
2777 ret = mpage_prepare_extent_to_map(&mpd);
2778 if (!ret) {
2779 if (mpd.map.m_len)
Theodore Ts'ocb530542013-07-01 08:12:40 -04002780 ret = mpage_map_and_submit_extent(handle, &mpd,
2781 &give_up_on_write);
Jan Kara4e7ea812013-06-04 13:17:40 -04002782 else {
2783 /*
2784 * We scanned the whole range (or exhausted
2785 * nr_to_write), submitted what was mapped and
2786 * didn't find anything needing mapping. We are
2787 * done.
2788 */
2789 done = true;
2790 }
Theodore Ts'of63e60052009-02-23 16:42:39 -05002791 }
Jan Kara646caa92016-07-04 10:14:01 -04002792 /*
2793 * Caution: If the handle is synchronous,
2794 * ext4_journal_stop() can wait for transaction commit
2795 * to finish which may depend on writeback of pages to
2796 * complete or on page lock to be released. In that
2797 * case, we have to wait until after after we have
2798 * submitted all the IO, released page locks we hold,
2799 * and dropped io_end reference (for extent conversion
2800 * to be able to complete) before stopping the handle.
2801 */
2802 if (!ext4_handle_valid(handle) || handle->h_sync == 0) {
2803 ext4_journal_stop(handle);
2804 handle = NULL;
Jan Karadddbd6a2017-04-30 18:29:10 -04002805 mpd.do_map = 0;
Jan Kara646caa92016-07-04 10:14:01 -04002806 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002807 /* Unlock pages we didn't use */
Theodore Ts'ocb530542013-07-01 08:12:40 -04002808 mpage_release_unused_pages(&mpd, give_up_on_write);
Xiaoguang Wanga297b2f2019-02-10 23:53:21 -05002809 /* Submit prepared bio */
2810 ext4_io_submit(&mpd.io_submit);
2811
Jan Kara646caa92016-07-04 10:14:01 -04002812 /*
2813 * Drop our io_end reference we got from init. We have
2814 * to be careful and use deferred io_end finishing if
2815 * we are still holding the transaction as we can
2816 * release the last reference to io_end which may end
2817 * up doing unwritten extent conversion.
2818 */
2819 if (handle) {
2820 ext4_put_io_end_defer(mpd.io_submit.io_end);
2821 ext4_journal_stop(handle);
2822 } else
2823 ext4_put_io_end(mpd.io_submit.io_end);
Jan Karadddbd6a2017-04-30 18:29:10 -04002824 mpd.io_submit.io_end = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002825
Jan Kara4e7ea812013-06-04 13:17:40 -04002826 if (ret == -ENOSPC && sbi->s_journal) {
2827 /*
2828 * Commit the transaction which would
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002829 * free blocks released in the transaction
2830 * and try again
2831 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002832 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002833 ret = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002834 continue;
2835 }
2836 /* Fatal error - ENOMEM, EIO... */
2837 if (ret)
Mingming Cao61628a32008-07-11 19:27:31 -04002838 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002839 }
Jan Karadddbd6a2017-04-30 18:29:10 -04002840unplug:
Shaohua Li1bce63d12011-10-18 10:55:51 -04002841 blk_finish_plug(&plug);
Jan Kara9c12a832013-09-16 08:24:26 -04002842 if (!ret && !cycled && wbc->nr_to_write > 0) {
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002843 cycled = 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04002844 mpd.last_page = writeback_index - 1;
2845 mpd.first_page = 0;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002846 goto retry;
2847 }
Mingming Cao61628a32008-07-11 19:27:31 -04002848
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002849 /* Update index */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002850 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2851 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002852 * Set the writeback_index so that range_cyclic
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002853 * mode will write it back later
2854 */
Jan Kara4e7ea812013-06-04 13:17:40 -04002855 mapping->writeback_index = mpd.first_page;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002856
Mingming Cao61628a32008-07-11 19:27:31 -04002857out_writepages:
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002858 trace_ext4_writepages_result(inode, wbc, ret,
2859 nr_to_write - wbc->nr_to_write);
Eric Biggersbbd55932020-02-19 10:30:46 -08002860 percpu_up_read(&sbi->s_writepages_rwsem);
Mingming Cao61628a32008-07-11 19:27:31 -04002861 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002862}
2863
Dan Williams5f0663b2017-12-21 12:25:11 -08002864static int ext4_dax_writepages(struct address_space *mapping,
2865 struct writeback_control *wbc)
2866{
2867 int ret;
2868 long nr_to_write = wbc->nr_to_write;
2869 struct inode *inode = mapping->host;
2870 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
2871
2872 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
2873 return -EIO;
2874
Eric Biggersbbd55932020-02-19 10:30:46 -08002875 percpu_down_read(&sbi->s_writepages_rwsem);
Dan Williams5f0663b2017-12-21 12:25:11 -08002876 trace_ext4_writepages(inode, wbc);
2877
Vivek Goyal3f666c52020-01-03 13:33:07 -05002878 ret = dax_writeback_mapping_range(mapping, sbi->s_daxdev, wbc);
Dan Williams5f0663b2017-12-21 12:25:11 -08002879 trace_ext4_writepages_result(inode, wbc, ret,
2880 nr_to_write - wbc->nr_to_write);
Eric Biggersbbd55932020-02-19 10:30:46 -08002881 percpu_up_read(&sbi->s_writepages_rwsem);
Dan Williams5f0663b2017-12-21 12:25:11 -08002882 return ret;
2883}
2884
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002885static int ext4_nonda_switch(struct super_block *sb)
2886{
Eric Whitney5c1ff332013-04-09 09:27:31 -04002887 s64 free_clusters, dirty_clusters;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002888 struct ext4_sb_info *sbi = EXT4_SB(sb);
2889
2890 /*
2891 * switch to non delalloc mode if we are running low
2892 * on free block. The free block accounting via percpu
Eric Dumazet179f7eb2009-01-06 14:41:04 -08002893 * counters can get slightly wrong with percpu_counter_batch getting
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002894 * accumulated on each CPU without updating global counters
2895 * Delalloc need an accurate free block accounting. So switch
2896 * to non delalloc when we are near to error range.
2897 */
Eric Whitney5c1ff332013-04-09 09:27:31 -04002898 free_clusters =
2899 percpu_counter_read_positive(&sbi->s_freeclusters_counter);
2900 dirty_clusters =
2901 percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
Theodore Ts'o00d4e732012-09-19 22:42:36 -04002902 /*
2903 * Start pushing delalloc when 1/2 of free blocks are dirty.
2904 */
Eric Whitney5c1ff332013-04-09 09:27:31 -04002905 if (dirty_clusters && (free_clusters < 2 * dirty_clusters))
Miao Xie10ee27a2013-01-10 13:47:57 +08002906 try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE);
Theodore Ts'o00d4e732012-09-19 22:42:36 -04002907
Eric Whitney5c1ff332013-04-09 09:27:31 -04002908 if (2 * free_clusters < 3 * dirty_clusters ||
2909 free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) {
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002910 /*
Eric Sandeenc8afb442009-12-23 07:58:12 -05002911 * free block count is less than 150% of dirty blocks
2912 * or free blocks is less than watermark
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002913 */
2914 return 1;
2915 }
2916 return 0;
2917}
2918
Eric Sandeen0ff89472014-10-11 19:51:17 -04002919/* We always reserve for an inode update; the superblock could be there too */
2920static int ext4_da_write_credits(struct inode *inode, loff_t pos, unsigned len)
2921{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002922 if (likely(ext4_has_feature_large_file(inode->i_sb)))
Eric Sandeen0ff89472014-10-11 19:51:17 -04002923 return 1;
2924
2925 if (pos + len <= 0x7fffffffULL)
2926 return 1;
2927
2928 /* We might need to update the superblock to set LARGE_FILE */
2929 return 2;
2930}
2931
Alex Tomas64769242008-07-11 19:27:31 -04002932static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002933 loff_t pos, unsigned len, unsigned flags,
2934 struct page **pagep, void **fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04002935{
Eric Sandeen72b8ab92010-05-16 11:00:00 -04002936 int ret, retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002937 struct page *page;
2938 pgoff_t index;
Alex Tomas64769242008-07-11 19:27:31 -04002939 struct inode *inode = mapping->host;
2940 handle_t *handle;
2941
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002942 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
2943 return -EIO;
2944
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002945 index = pos >> PAGE_SHIFT;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002946
Eric Biggersc93d8f82019-07-22 09:26:24 -07002947 if (ext4_nonda_switch(inode->i_sb) || S_ISLNK(inode->i_mode) ||
2948 ext4_verity_in_progress(inode)) {
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002949 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
2950 return ext4_write_begin(file, mapping, pos,
2951 len, flags, pagep, fsdata);
2952 }
2953 *fsdata = (void *)0;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002954 trace_ext4_da_write_begin(inode, pos, len, flags);
Tao Ma9c3569b2012-12-10 14:05:57 -05002955
2956 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2957 ret = ext4_da_write_inline_data_begin(mapping, inode,
2958 pos, len, flags,
2959 pagep, fsdata);
2960 if (ret < 0)
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002961 return ret;
2962 if (ret == 1)
2963 return 0;
Tao Ma9c3569b2012-12-10 14:05:57 -05002964 }
2965
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002966 /*
2967 * grab_cache_page_write_begin() can take a long time if the
2968 * system is thrashing due to memory pressure, or if the page
2969 * is being written back. So grab it first before we start
2970 * the transaction handle. This also allows us to allocate
2971 * the page (if needed) without using GFP_NOFS.
2972 */
2973retry_grab:
2974 page = grab_cache_page_write_begin(mapping, index, flags);
2975 if (!page)
2976 return -ENOMEM;
2977 unlock_page(page);
2978
Alex Tomas64769242008-07-11 19:27:31 -04002979 /*
2980 * With delayed allocation, we don't log the i_disksize update
2981 * if there is delayed block allocation. But we still need
2982 * to journalling the i_disksize update if writes to the end
2983 * of file which has an already mapped buffer.
2984 */
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002985retry_journal:
Eric Sandeen0ff89472014-10-11 19:51:17 -04002986 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
2987 ext4_da_write_credits(inode, pos, len));
Alex Tomas64769242008-07-11 19:27:31 -04002988 if (IS_ERR(handle)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002989 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002990 return PTR_ERR(handle);
Alex Tomas64769242008-07-11 19:27:31 -04002991 }
2992
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002993 lock_page(page);
2994 if (page->mapping != mapping) {
2995 /* The page got truncated from under us */
2996 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002997 put_page(page);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002998 ext4_journal_stop(handle);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002999 goto retry_grab;
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04003000 }
Theodore Ts'o47564bf2013-02-09 09:24:14 -05003001 /* In case writeback began while the page was unlocked */
Dmitry Monakhov7afe5aa2013-08-28 14:30:47 -04003002 wait_for_stable_page(page);
Alex Tomas64769242008-07-11 19:27:31 -04003003
Chandan Rajendra643fa962018-12-12 15:20:12 +05303004#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrow2058f832015-04-12 00:55:10 -04003005 ret = ext4_block_write_begin(page, pos, len,
3006 ext4_da_get_block_prep);
3007#else
Christoph Hellwig6e1db882010-06-04 11:29:57 +02003008 ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
Michael Halcrow2058f832015-04-12 00:55:10 -04003009#endif
Alex Tomas64769242008-07-11 19:27:31 -04003010 if (ret < 0) {
3011 unlock_page(page);
3012 ext4_journal_stop(handle);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04003013 /*
3014 * block_write_begin may have instantiated a few blocks
3015 * outside i_size. Trim these off again. Don't need
3016 * i_size_read because we hold i_mutex.
3017 */
3018 if (pos + len > inode->i_size)
Jan Karab9a42072009-12-08 21:24:33 -05003019 ext4_truncate_failed_write(inode);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05003020
3021 if (ret == -ENOSPC &&
3022 ext4_should_retry_alloc(inode->i_sb, &retries))
3023 goto retry_journal;
3024
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003025 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05003026 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04003027 }
3028
Theodore Ts'o47564bf2013-02-09 09:24:14 -05003029 *pagep = page;
Alex Tomas64769242008-07-11 19:27:31 -04003030 return ret;
3031}
3032
Mingming Cao632eaea2008-07-11 19:27:31 -04003033/*
3034 * Check if we should update i_disksize
3035 * when write to the end of file but not require block allocation
3036 */
3037static int ext4_da_should_update_i_disksize(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003038 unsigned long offset)
Mingming Cao632eaea2008-07-11 19:27:31 -04003039{
3040 struct buffer_head *bh;
3041 struct inode *inode = page->mapping->host;
3042 unsigned int idx;
3043 int i;
3044
3045 bh = page_buffers(page);
3046 idx = offset >> inode->i_blkbits;
3047
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003048 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04003049 bh = bh->b_this_page;
3050
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04003051 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
Mingming Cao632eaea2008-07-11 19:27:31 -04003052 return 0;
3053 return 1;
3054}
3055
Alex Tomas64769242008-07-11 19:27:31 -04003056static int ext4_da_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003057 struct address_space *mapping,
3058 loff_t pos, unsigned len, unsigned copied,
3059 struct page *page, void *fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04003060{
3061 struct inode *inode = mapping->host;
3062 int ret = 0, ret2;
3063 handle_t *handle = ext4_journal_current_handle();
3064 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04003065 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003066 int write_mode = (int)(unsigned long)fsdata;
3067
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003068 if (write_mode == FALL_BACK_TO_NONDELALLOC)
3069 return ext4_write_end(file, mapping, pos,
3070 len, copied, page, fsdata);
Mingming Cao632eaea2008-07-11 19:27:31 -04003071
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003072 trace_ext4_da_write_end(inode, pos, len, copied);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003073 start = pos & (PAGE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003074 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04003075
3076 /*
3077 * generic_write_end() will run mark_inode_dirty() if i_size
3078 * changes. So let's piggyback the i_disksize mark_inode_dirty
3079 * into that.
3080 */
Alex Tomas64769242008-07-11 19:27:31 -04003081 new_i_size = pos + copied;
Andrea Arcangeliea51d132011-12-13 21:41:15 -05003082 if (copied && new_i_size > EXT4_I(inode)->i_disksize) {
Tao Ma9c3569b2012-12-10 14:05:57 -05003083 if (ext4_has_inline_data(inode) ||
3084 ext4_da_should_update_i_disksize(page, end)) {
Dmitry Monakhovee124d22014-08-30 23:34:06 -04003085 ext4_update_i_disksize(inode, new_i_size);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003086 /* We need to mark inode dirty even if
3087 * new_i_size is less that inode->i_size
3088 * bu greater than i_disksize.(hint delalloc)
3089 */
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003090 ret = ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04003091 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003092 }
Tao Ma9c3569b2012-12-10 14:05:57 -05003093
3094 if (write_mode != CONVERT_INLINE_DATA &&
3095 ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
3096 ext4_has_inline_data(inode))
3097 ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied,
3098 page);
3099 else
3100 ret2 = generic_write_end(file, mapping, pos, len, copied,
Alex Tomas64769242008-07-11 19:27:31 -04003101 page, fsdata);
Tao Ma9c3569b2012-12-10 14:05:57 -05003102
Alex Tomas64769242008-07-11 19:27:31 -04003103 copied = ret2;
3104 if (ret2 < 0)
3105 ret = ret2;
3106 ret2 = ext4_journal_stop(handle);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003107 if (unlikely(ret2 && !ret))
Alex Tomas64769242008-07-11 19:27:31 -04003108 ret = ret2;
3109
3110 return ret ? ret : copied;
3111}
3112
Theodore Ts'occd25062009-02-26 01:04:07 -05003113/*
3114 * Force all delayed allocation blocks to be allocated for a given inode.
3115 */
3116int ext4_alloc_da_blocks(struct inode *inode)
3117{
Theodore Ts'ofb40ba02009-09-16 19:30:40 -04003118 trace_ext4_alloc_da_blocks(inode);
3119
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04003120 if (!EXT4_I(inode)->i_reserved_data_blocks)
Theodore Ts'occd25062009-02-26 01:04:07 -05003121 return 0;
3122
3123 /*
3124 * We do something simple for now. The filemap_flush() will
3125 * also start triggering a write of the data blocks, which is
3126 * not strictly speaking necessary (and for users of
3127 * laptop_mode, not even desirable). However, to do otherwise
3128 * would require replicating code paths in:
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003129 *
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003130 * ext4_writepages() ->
Theodore Ts'occd25062009-02-26 01:04:07 -05003131 * write_cache_pages() ---> (via passed in callback function)
3132 * __mpage_da_writepage() -->
3133 * mpage_add_bh_to_extent()
3134 * mpage_da_map_blocks()
3135 *
3136 * The problem is that write_cache_pages(), located in
3137 * mm/page-writeback.c, marks pages clean in preparation for
3138 * doing I/O, which is not desirable if we're not planning on
3139 * doing I/O at all.
3140 *
3141 * We could call write_cache_pages(), and then redirty all of
Wu Fengguang380cf092010-11-11 19:23:29 +08003142 * the pages by calling redirty_page_for_writepage() but that
Theodore Ts'occd25062009-02-26 01:04:07 -05003143 * would be ugly in the extreme. So instead we would need to
3144 * replicate parts of the code in the above functions,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003145 * simplifying them because we wouldn't actually intend to
Theodore Ts'occd25062009-02-26 01:04:07 -05003146 * write out the pages, but rather only collect contiguous
3147 * logical block extents, call the multi-block allocator, and
3148 * then update the buffer heads with the block allocations.
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003149 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003150 * For now, though, we'll cheat by calling filemap_flush(),
3151 * which will map the blocks, and start the I/O, but not
3152 * actually wait for the I/O to complete.
3153 */
3154 return filemap_flush(inode->i_mapping);
3155}
Alex Tomas64769242008-07-11 19:27:31 -04003156
3157/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003158 * bmap() is special. It gets used by applications such as lilo and by
3159 * the swapper to find the on-disk block of a specific piece of data.
3160 *
3161 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07003162 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003163 * filesystem and enables swap, then they may get a nasty shock when the
3164 * data getting swapped to that swapfile suddenly gets overwritten by
3165 * the original zero's written out previously to the journal and
3166 * awaiting writeback in the kernel's buffer cache.
3167 *
3168 * So, if we see any bmap calls here on a modified, data-journaled file,
3169 * take extra steps to flush any blocks which might be in the cache.
3170 */
Mingming Cao617ba132006-10-11 01:20:53 -07003171static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003172{
3173 struct inode *inode = mapping->host;
3174 journal_t *journal;
3175 int err;
3176
Tao Ma46c7f252012-12-10 14:04:52 -05003177 /*
3178 * We can get here for an inline file via the FIBMAP ioctl
3179 */
3180 if (ext4_has_inline_data(inode))
3181 return 0;
3182
Alex Tomas64769242008-07-11 19:27:31 -04003183 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
3184 test_opt(inode->i_sb, DELALLOC)) {
3185 /*
3186 * With delalloc we want to sync the file
3187 * so that we can make sure we allocate
3188 * blocks for file
3189 */
3190 filemap_write_and_wait(mapping);
3191 }
3192
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003193 if (EXT4_JOURNAL(inode) &&
3194 ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003195 /*
3196 * This is a REALLY heavyweight approach, but the use of
3197 * bmap on dirty files is expected to be extremely rare:
3198 * only if we run lilo or swapon on a freshly made file
3199 * do we expect this to happen.
3200 *
3201 * (bmap requires CAP_SYS_RAWIO so this does not
3202 * represent an unprivileged user DOS attack --- we'd be
3203 * in trouble if mortal users could trigger this path at
3204 * will.)
3205 *
Mingming Cao617ba132006-10-11 01:20:53 -07003206 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003207 * regular files. If somebody wants to bmap a directory
3208 * or symlink and gets confused because the buffer
3209 * hasn't yet been flushed to disk, they deserve
3210 * everything they get.
3211 */
3212
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003213 ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
Mingming Cao617ba132006-10-11 01:20:53 -07003214 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07003215 jbd2_journal_lock_updates(journal);
3216 err = jbd2_journal_flush(journal);
3217 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003218
3219 if (err)
3220 return 0;
3221 }
3222
Ritesh Harjaniac58e4f2020-02-28 14:56:56 +05303223 return iomap_bmap(mapping, block, &ext4_iomap_ops);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003224}
3225
Mingming Cao617ba132006-10-11 01:20:53 -07003226static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003227{
Tao Ma46c7f252012-12-10 14:04:52 -05003228 int ret = -EAGAIN;
3229 struct inode *inode = page->mapping->host;
3230
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003231 trace_ext4_readpage(page);
Tao Ma46c7f252012-12-10 14:04:52 -05003232
3233 if (ext4_has_inline_data(inode))
3234 ret = ext4_readpage_inline(inode, page);
3235
3236 if (ret == -EAGAIN)
Jens Axboeac22b462018-08-17 15:45:42 -07003237 return ext4_mpage_readpages(page->mapping, NULL, page, 1,
3238 false);
Tao Ma46c7f252012-12-10 14:04:52 -05003239
3240 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003241}
3242
3243static int
Mingming Cao617ba132006-10-11 01:20:53 -07003244ext4_readpages(struct file *file, struct address_space *mapping,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003245 struct list_head *pages, unsigned nr_pages)
3246{
Tao Ma46c7f252012-12-10 14:04:52 -05003247 struct inode *inode = mapping->host;
3248
3249 /* If the file has inline data, no need to do readpages. */
3250 if (ext4_has_inline_data(inode))
3251 return 0;
3252
Jens Axboeac22b462018-08-17 15:45:42 -07003253 return ext4_mpage_readpages(mapping, pages, NULL, nr_pages, true);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003254}
3255
Lukas Czernerd47992f2013-05-21 23:17:23 -04003256static void ext4_invalidatepage(struct page *page, unsigned int offset,
3257 unsigned int length)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003258{
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003259 trace_ext4_invalidatepage(page, offset, length);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003260
Jan Kara4520fb32012-12-25 13:28:54 -05003261 /* No journalling happens on data buffers when this function is used */
3262 WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page)));
3263
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003264 block_invalidatepage(page, offset, length);
Jan Kara4520fb32012-12-25 13:28:54 -05003265}
3266
Jan Kara53e87262012-12-25 13:29:52 -05003267static int __ext4_journalled_invalidatepage(struct page *page,
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003268 unsigned int offset,
3269 unsigned int length)
Jan Kara4520fb32012-12-25 13:28:54 -05003270{
3271 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
3272
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003273 trace_ext4_journalled_invalidatepage(page, offset, length);
Jan Kara4520fb32012-12-25 13:28:54 -05003274
Jiaying Zhang744692d2010-03-04 16:14:02 -05003275 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003276 * If it's a full truncate we just forget about the pending dirtying
3277 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003278 if (offset == 0 && length == PAGE_SIZE)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003279 ClearPageChecked(page);
3280
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003281 return jbd2_journal_invalidatepage(journal, page, offset, length);
Jan Kara53e87262012-12-25 13:29:52 -05003282}
3283
3284/* Wrapper for aops... */
3285static void ext4_journalled_invalidatepage(struct page *page,
Lukas Czernerd47992f2013-05-21 23:17:23 -04003286 unsigned int offset,
3287 unsigned int length)
Jan Kara53e87262012-12-25 13:29:52 -05003288{
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003289 WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003290}
3291
Mingming Cao617ba132006-10-11 01:20:53 -07003292static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003293{
Mingming Cao617ba132006-10-11 01:20:53 -07003294 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003295
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003296 trace_ext4_releasepage(page);
3297
Jan Karae1c36592013-03-10 22:19:00 -04003298 /* Page has dirty journalled data -> cannot release */
3299 if (PageChecked(page))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003300 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05003301 if (journal)
3302 return jbd2_journal_try_to_free_buffers(journal, page, wait);
3303 else
3304 return try_to_free_buffers(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003305}
3306
Jan Karab8a61762017-11-01 16:36:45 +01003307static bool ext4_inode_datasync_dirty(struct inode *inode)
3308{
3309 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
3310
3311 if (journal)
3312 return !jbd2_transaction_committed(journal,
3313 EXT4_I(inode)->i_datasync_tid);
3314 /* Any metadata buffers to write? */
3315 if (!list_empty(&inode->i_mapping->private_list))
3316 return true;
3317 return inode->i_state & I_DIRTY_DATASYNC;
3318}
3319
Matthew Bobrowskic8fdfe292019-11-05 22:59:56 +11003320static void ext4_set_iomap(struct inode *inode, struct iomap *iomap,
3321 struct ext4_map_blocks *map, loff_t offset,
3322 loff_t length)
Jan Kara364443c2016-11-20 17:36:06 -05003323{
Matthew Bobrowskic8fdfe292019-11-05 22:59:56 +11003324 u8 blkbits = inode->i_blkbits;
3325
3326 /*
3327 * Writes that span EOF might trigger an I/O size update on completion,
3328 * so consider them to be dirty for the purpose of O_DSYNC, even if
3329 * there is no other metadata changes being made or are pending.
3330 */
3331 iomap->flags = 0;
3332 if (ext4_inode_datasync_dirty(inode) ||
3333 offset + length > i_size_read(inode))
3334 iomap->flags |= IOMAP_F_DIRTY;
3335
3336 if (map->m_flags & EXT4_MAP_NEW)
3337 iomap->flags |= IOMAP_F_NEW;
3338
3339 iomap->bdev = inode->i_sb->s_bdev;
3340 iomap->dax_dev = EXT4_SB(inode->i_sb)->s_daxdev;
3341 iomap->offset = (u64) map->m_lblk << blkbits;
3342 iomap->length = (u64) map->m_len << blkbits;
3343
Ritesh Harjani63867222020-02-28 14:56:54 +05303344 if ((map->m_flags & EXT4_MAP_MAPPED) &&
3345 !ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3346 iomap->flags |= IOMAP_F_MERGED;
3347
Matthew Bobrowskic8fdfe292019-11-05 22:59:56 +11003348 /*
3349 * Flags passed to ext4_map_blocks() for direct I/O writes can result
3350 * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits
3351 * set. In order for any allocated unwritten extents to be converted
3352 * into written extents correctly within the ->end_io() handler, we
3353 * need to ensure that the iomap->type is set appropriately. Hence, the
3354 * reason why we need to check whether the EXT4_MAP_UNWRITTEN bit has
3355 * been set first.
3356 */
3357 if (map->m_flags & EXT4_MAP_UNWRITTEN) {
3358 iomap->type = IOMAP_UNWRITTEN;
3359 iomap->addr = (u64) map->m_pblk << blkbits;
3360 } else if (map->m_flags & EXT4_MAP_MAPPED) {
3361 iomap->type = IOMAP_MAPPED;
3362 iomap->addr = (u64) map->m_pblk << blkbits;
3363 } else {
3364 iomap->type = IOMAP_HOLE;
3365 iomap->addr = IOMAP_NULL_ADDR;
3366 }
3367}
3368
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003369static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map,
3370 unsigned int flags)
3371{
3372 handle_t *handle;
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003373 u8 blkbits = inode->i_blkbits;
3374 int ret, dio_credits, m_flags = 0, retries = 0;
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003375
3376 /*
3377 * Trim the mapping request to the maximum value that we can map at
3378 * once for direct I/O.
3379 */
3380 if (map->m_len > DIO_MAX_BLOCKS)
3381 map->m_len = DIO_MAX_BLOCKS;
3382 dio_credits = ext4_chunk_trans_blocks(inode, map->m_len);
3383
3384retry:
3385 /*
3386 * Either we allocate blocks and then don't get an unwritten extent, so
3387 * in that case we have reserved enough credits. Or, the blocks are
3388 * already allocated and unwritten. In that case, the extent conversion
3389 * fits into the credits as well.
3390 */
3391 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits);
3392 if (IS_ERR(handle))
3393 return PTR_ERR(handle);
3394
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003395 /*
3396 * DAX and direct I/O are the only two operations that are currently
3397 * supported with IOMAP_WRITE.
3398 */
3399 WARN_ON(!IS_DAX(inode) && !(flags & IOMAP_DIRECT));
3400 if (IS_DAX(inode))
3401 m_flags = EXT4_GET_BLOCKS_CREATE_ZERO;
3402 /*
3403 * We use i_size instead of i_disksize here because delalloc writeback
3404 * can complete at any point during the I/O and subsequently push the
3405 * i_disksize out to i_size. This could be beyond where direct I/O is
3406 * happening and thus expose allocated blocks to direct I/O reads.
3407 */
3408 else if ((map->m_lblk * (1 << blkbits)) >= i_size_read(inode))
3409 m_flags = EXT4_GET_BLOCKS_CREATE;
3410 else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3411 m_flags = EXT4_GET_BLOCKS_IO_CREATE_EXT;
3412
3413 ret = ext4_map_blocks(handle, inode, map, m_flags);
3414
3415 /*
3416 * We cannot fill holes in indirect tree based inodes as that could
3417 * expose stale data in the case of a crash. Use the magic error code
3418 * to fallback to buffered I/O.
3419 */
3420 if (!m_flags && !ret)
3421 ret = -ENOTBLK;
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003422
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003423 ext4_journal_stop(handle);
3424 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3425 goto retry;
3426
3427 return ret;
3428}
3429
3430
Jan Kara364443c2016-11-20 17:36:06 -05003431static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -07003432 unsigned flags, struct iomap *iomap, struct iomap *srcmap)
Jan Kara364443c2016-11-20 17:36:06 -05003433{
Jan Kara364443c2016-11-20 17:36:06 -05003434 int ret;
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003435 struct ext4_map_blocks map;
3436 u8 blkbits = inode->i_blkbits;
Jan Kara364443c2016-11-20 17:36:06 -05003437
Theodore Ts'obcd8e912018-09-01 12:45:04 -04003438 if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
3439 return -EINVAL;
Andreas Gruenbacher7046ae32017-10-01 17:57:54 -04003440
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003441 if (WARN_ON_ONCE(ext4_has_inline_data(inode)))
3442 return -ERANGE;
Jan Kara364443c2016-11-20 17:36:06 -05003443
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003444 /*
3445 * Calculate the first and last logical blocks respectively.
3446 */
3447 map.m_lblk = offset >> blkbits;
3448 map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits,
3449 EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1;
Jan Kara364443c2016-11-20 17:36:06 -05003450
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003451 if (flags & IOMAP_WRITE)
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003452 ret = ext4_iomap_alloc(inode, &map, flags);
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003453 else
Jan Kara776722e2016-11-20 18:09:11 -05003454 ret = ext4_map_blocks(NULL, inode, &map, 0);
Christoph Hellwig545052e2017-10-01 17:58:54 -04003455
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003456 if (ret < 0)
3457 return ret;
Christoph Hellwig545052e2017-10-01 17:58:54 -04003458
Matthew Bobrowskic8fdfe292019-11-05 22:59:56 +11003459 ext4_set_iomap(inode, iomap, &map, offset, length);
Christoph Hellwig545052e2017-10-01 17:58:54 -04003460
Jan Kara364443c2016-11-20 17:36:06 -05003461 return 0;
3462}
3463
Jan Kara8cd115b2019-12-18 18:44:33 +01003464static int ext4_iomap_overwrite_begin(struct inode *inode, loff_t offset,
3465 loff_t length, unsigned flags, struct iomap *iomap,
3466 struct iomap *srcmap)
3467{
3468 int ret;
3469
3470 /*
3471 * Even for writes we don't need to allocate blocks, so just pretend
3472 * we are reading to save overhead of starting a transaction.
3473 */
3474 flags &= ~IOMAP_WRITE;
3475 ret = ext4_iomap_begin(inode, offset, length, flags, iomap, srcmap);
3476 WARN_ON_ONCE(iomap->type != IOMAP_MAPPED);
3477 return ret;
3478}
3479
Jan Kara776722e2016-11-20 18:09:11 -05003480static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length,
3481 ssize_t written, unsigned flags, struct iomap *iomap)
3482{
Jan Kara776722e2016-11-20 18:09:11 -05003483 /*
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003484 * Check to see whether an error occurred while writing out the data to
3485 * the allocated blocks. If so, return the magic error code so that we
3486 * fallback to buffered I/O and attempt to complete the remainder of
3487 * the I/O. Any blocks that may have been allocated in preparation for
3488 * the direct I/O will be reused during buffered I/O.
Jan Kara776722e2016-11-20 18:09:11 -05003489 */
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003490 if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0)
3491 return -ENOTBLK;
Jan Kara776722e2016-11-20 18:09:11 -05003492
Matthew Bobrowski569342d2019-11-05 23:01:51 +11003493 return 0;
Jan Kara776722e2016-11-20 18:09:11 -05003494}
3495
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08003496const struct iomap_ops ext4_iomap_ops = {
Jan Kara364443c2016-11-20 17:36:06 -05003497 .iomap_begin = ext4_iomap_begin,
Jan Kara776722e2016-11-20 18:09:11 -05003498 .iomap_end = ext4_iomap_end,
Jan Kara364443c2016-11-20 17:36:06 -05003499};
3500
Jan Kara8cd115b2019-12-18 18:44:33 +01003501const struct iomap_ops ext4_iomap_overwrite_ops = {
3502 .iomap_begin = ext4_iomap_overwrite_begin,
3503 .iomap_end = ext4_iomap_end,
3504};
3505
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003506static bool ext4_iomap_is_delalloc(struct inode *inode,
3507 struct ext4_map_blocks *map)
Mingming Cao4c0425f2009-09-28 15:48:41 -04003508{
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003509 struct extent_status es;
3510 ext4_lblk_t offset = 0, end = map->m_lblk + map->m_len - 1;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003511
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003512 ext4_es_find_extent_range(inode, &ext4_es_is_delayed,
3513 map->m_lblk, end, &es);
Mingming4b70df12009-11-03 14:44:54 -05003514
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003515 if (!es.es_len || es.es_lblk > end)
3516 return false;
3517
3518 if (es.es_lblk > map->m_lblk) {
3519 map->m_len = es.es_lblk - map->m_lblk;
3520 return false;
3521 }
3522
3523 offset = map->m_lblk - es.es_lblk;
3524 map->m_len = es.es_len - offset;
3525
3526 return true;
3527}
3528
3529static int ext4_iomap_begin_report(struct inode *inode, loff_t offset,
3530 loff_t length, unsigned int flags,
3531 struct iomap *iomap, struct iomap *srcmap)
3532{
3533 int ret;
3534 bool delalloc = false;
3535 struct ext4_map_blocks map;
3536 u8 blkbits = inode->i_blkbits;
3537
3538 if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
3539 return -EINVAL;
3540
3541 if (ext4_has_inline_data(inode)) {
3542 ret = ext4_inline_data_iomap(inode, iomap);
3543 if (ret != -EAGAIN) {
3544 if (ret == 0 && offset >= iomap->length)
3545 ret = -ENOENT;
3546 return ret;
3547 }
3548 }
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003549
Jan Kara74c66bc2016-02-29 08:36:38 +11003550 /*
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003551 * Calculate the first and last logical block respectively.
Jan Kara74c66bc2016-02-29 08:36:38 +11003552 */
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003553 map.m_lblk = offset >> blkbits;
3554 map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits,
3555 EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1;
3556
Ritesh Harjanib2c57642020-02-28 14:56:57 +05303557 /*
3558 * Fiemap callers may call for offset beyond s_bitmap_maxbytes.
3559 * So handle it here itself instead of querying ext4_map_blocks().
3560 * Since ext4_map_blocks() will warn about it and will return
3561 * -EIO error.
3562 */
3563 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
3564 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3565
3566 if (offset >= sbi->s_bitmap_maxbytes) {
3567 map.m_flags = 0;
3568 goto set_iomap;
3569 }
3570 }
3571
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003572 ret = ext4_map_blocks(NULL, inode, &map, 0);
3573 if (ret < 0)
3574 return ret;
3575 if (ret == 0)
3576 delalloc = ext4_iomap_is_delalloc(inode, &map);
3577
Ritesh Harjanib2c57642020-02-28 14:56:57 +05303578set_iomap:
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003579 ext4_set_iomap(inode, iomap, &map, offset, length);
3580 if (delalloc && iomap->type == IOMAP_HOLE)
3581 iomap->type = IOMAP_DELALLOC;
Christoph Hellwig187372a2016-02-08 14:40:51 +11003582
3583 return 0;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003584}
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003585
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003586const struct iomap_ops ext4_iomap_report_ops = {
3587 .iomap_begin = ext4_iomap_begin_report,
3588};
Mingming Cao4c0425f2009-09-28 15:48:41 -04003589
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003590/*
Mingming Cao617ba132006-10-11 01:20:53 -07003591 * Pages can be marked dirty completely asynchronously from ext4's journalling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003592 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3593 * much here because ->set_page_dirty is called under VFS locks. The page is
3594 * not necessarily locked.
3595 *
3596 * We cannot just dirty the page and leave attached buffers clean, because the
3597 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3598 * or jbddirty because all the journalling code will explode.
3599 *
3600 * So what we do is to mark the page "pending dirty" and next time writepage
3601 * is called, propagate that into the buffers appropriately.
3602 */
Mingming Cao617ba132006-10-11 01:20:53 -07003603static int ext4_journalled_set_page_dirty(struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003604{
3605 SetPageChecked(page);
3606 return __set_page_dirty_nobuffers(page);
3607}
3608
Jan Kara6dcc6932016-12-01 11:46:40 -05003609static int ext4_set_page_dirty(struct page *page)
3610{
3611 WARN_ON_ONCE(!PageLocked(page) && !PageDirty(page));
3612 WARN_ON_ONCE(!page_has_buffers(page));
3613 return __set_page_dirty_buffers(page);
3614}
3615
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003616static const struct address_space_operations ext4_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003617 .readpage = ext4_readpage,
3618 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003619 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003620 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003621 .write_begin = ext4_write_begin,
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003622 .write_end = ext4_write_end,
Jan Kara6dcc6932016-12-01 11:46:40 -05003623 .set_page_dirty = ext4_set_page_dirty,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003624 .bmap = ext4_bmap,
3625 .invalidatepage = ext4_invalidatepage,
3626 .releasepage = ext4_releasepage,
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003627 .direct_IO = noop_direct_IO,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003628 .migratepage = buffer_migrate_page,
3629 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003630 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003631};
3632
Mingming Cao617ba132006-10-11 01:20:53 -07003633static const struct address_space_operations ext4_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003634 .readpage = ext4_readpage,
3635 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003636 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003637 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003638 .write_begin = ext4_write_begin,
3639 .write_end = ext4_journalled_write_end,
3640 .set_page_dirty = ext4_journalled_set_page_dirty,
3641 .bmap = ext4_bmap,
Jan Kara4520fb32012-12-25 13:28:54 -05003642 .invalidatepage = ext4_journalled_invalidatepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003643 .releasepage = ext4_releasepage,
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003644 .direct_IO = noop_direct_IO,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003645 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003646 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003647};
3648
Alex Tomas64769242008-07-11 19:27:31 -04003649static const struct address_space_operations ext4_da_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003650 .readpage = ext4_readpage,
3651 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003652 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003653 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003654 .write_begin = ext4_da_write_begin,
3655 .write_end = ext4_da_write_end,
Jan Kara6dcc6932016-12-01 11:46:40 -05003656 .set_page_dirty = ext4_set_page_dirty,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003657 .bmap = ext4_bmap,
Eric Whitney8fcc3a52019-08-22 23:22:14 -04003658 .invalidatepage = ext4_invalidatepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003659 .releasepage = ext4_releasepage,
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003660 .direct_IO = noop_direct_IO,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003661 .migratepage = buffer_migrate_page,
3662 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003663 .error_remove_page = generic_error_remove_page,
Alex Tomas64769242008-07-11 19:27:31 -04003664};
3665
Dan Williams5f0663b2017-12-21 12:25:11 -08003666static const struct address_space_operations ext4_dax_aops = {
3667 .writepages = ext4_dax_writepages,
3668 .direct_IO = noop_direct_IO,
3669 .set_page_dirty = noop_set_page_dirty,
Toshi Kani94dbb632018-09-15 21:23:41 -04003670 .bmap = ext4_bmap,
Dan Williams5f0663b2017-12-21 12:25:11 -08003671 .invalidatepage = noop_invalidatepage,
3672};
3673
Mingming Cao617ba132006-10-11 01:20:53 -07003674void ext4_set_aops(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003675{
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003676 switch (ext4_inode_journal_mode(inode)) {
3677 case EXT4_INODE_ORDERED_DATA_MODE:
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003678 case EXT4_INODE_WRITEBACK_DATA_MODE:
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003679 break;
3680 case EXT4_INODE_JOURNAL_DATA_MODE:
Mingming Cao617ba132006-10-11 01:20:53 -07003681 inode->i_mapping->a_ops = &ext4_journalled_aops;
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003682 return;
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003683 default:
3684 BUG();
3685 }
Dan Williams5f0663b2017-12-21 12:25:11 -08003686 if (IS_DAX(inode))
3687 inode->i_mapping->a_ops = &ext4_dax_aops;
3688 else if (test_opt(inode->i_sb, DELALLOC))
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003689 inode->i_mapping->a_ops = &ext4_da_aops;
3690 else
3691 inode->i_mapping->a_ops = &ext4_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003692}
3693
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003694static int __ext4_block_zero_page_range(handle_t *handle,
Lukas Czernerd863dc32013-05-27 23:32:35 -04003695 struct address_space *mapping, loff_t from, loff_t length)
3696{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003697 ext4_fsblk_t index = from >> PAGE_SHIFT;
3698 unsigned offset = from & (PAGE_SIZE-1);
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003699 unsigned blocksize, pos;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003700 ext4_lblk_t iblock;
3701 struct inode *inode = mapping->host;
3702 struct buffer_head *bh;
3703 struct page *page;
3704 int err = 0;
3705
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003706 page = find_or_create_page(mapping, from >> PAGE_SHIFT,
Michal Hockoc62d2552015-11-06 16:28:49 -08003707 mapping_gfp_constraint(mapping, ~__GFP_FS));
Lukas Czernerd863dc32013-05-27 23:32:35 -04003708 if (!page)
3709 return -ENOMEM;
3710
3711 blocksize = inode->i_sb->s_blocksize;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003712
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003713 iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003714
3715 if (!page_has_buffers(page))
3716 create_empty_buffers(page, blocksize, 0);
3717
3718 /* Find the buffer that contains "offset" */
3719 bh = page_buffers(page);
3720 pos = blocksize;
3721 while (offset >= pos) {
3722 bh = bh->b_this_page;
3723 iblock++;
3724 pos += blocksize;
3725 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003726 if (buffer_freed(bh)) {
3727 BUFFER_TRACE(bh, "freed: skip");
3728 goto unlock;
3729 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003730 if (!buffer_mapped(bh)) {
3731 BUFFER_TRACE(bh, "unmapped");
3732 ext4_get_block(inode, iblock, bh, 0);
3733 /* unmapped? It's a hole - nothing to do */
3734 if (!buffer_mapped(bh)) {
3735 BUFFER_TRACE(bh, "still unmapped");
3736 goto unlock;
3737 }
3738 }
3739
3740 /* Ok, it's mapped. Make sure it's up-to-date */
3741 if (PageUptodate(page))
3742 set_buffer_uptodate(bh);
3743
3744 if (!buffer_uptodate(bh)) {
3745 err = -EIO;
Mike Christiedfec8a12016-06-05 14:31:44 -05003746 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003747 wait_on_buffer(bh);
3748 /* Uhhuh. Read error. Complain and punt. */
3749 if (!buffer_uptodate(bh))
3750 goto unlock;
Chandan Rajendra592ddec2018-12-12 15:20:10 +05303751 if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode)) {
Michael Halcrowc9c74292015-04-12 00:56:10 -04003752 /* We expect the key to be set. */
Jaegeuk Kima7550b32016-07-10 14:01:03 -04003753 BUG_ON(!fscrypt_has_encryption_key(inode));
Eric Biggers834f1562019-12-26 09:41:05 -06003754 err = fscrypt_decrypt_pagecache_blocks(page, blocksize,
3755 bh_offset(bh));
3756 if (err) {
3757 clear_buffer_uptodate(bh);
3758 goto unlock;
3759 }
Michael Halcrowc9c74292015-04-12 00:56:10 -04003760 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003761 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003762 if (ext4_should_journal_data(inode)) {
3763 BUFFER_TRACE(bh, "get write access");
3764 err = ext4_journal_get_write_access(handle, bh);
3765 if (err)
3766 goto unlock;
3767 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003768 zero_user(page, offset, length);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003769 BUFFER_TRACE(bh, "zeroed end of block");
3770
Lukas Czernerd863dc32013-05-27 23:32:35 -04003771 if (ext4_should_journal_data(inode)) {
3772 err = ext4_handle_dirty_metadata(handle, inode, bh);
Lukas Czerner0713ed02013-05-27 23:32:35 -04003773 } else {
jon ernst353eefd2013-07-01 08:12:39 -04003774 err = 0;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003775 mark_buffer_dirty(bh);
Jan Kara3957ef52016-04-24 00:56:05 -04003776 if (ext4_should_order_data(inode))
Ross Zwisler73131fb2019-06-20 17:26:26 -04003777 err = ext4_jbd2_inode_add_write(handle, inode, from,
3778 length);
Lukas Czerner0713ed02013-05-27 23:32:35 -04003779 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003780
3781unlock:
3782 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003783 put_page(page);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003784 return err;
3785}
3786
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003787/*
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003788 * ext4_block_zero_page_range() zeros out a mapping of length 'length'
3789 * starting from file offset 'from'. The range to be zero'd must
3790 * be contained with in one block. If the specified range exceeds
3791 * the end of the block it will be shortened to end of the block
3792 * that cooresponds to 'from'
3793 */
3794static int ext4_block_zero_page_range(handle_t *handle,
3795 struct address_space *mapping, loff_t from, loff_t length)
3796{
3797 struct inode *inode = mapping->host;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003798 unsigned offset = from & (PAGE_SIZE-1);
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003799 unsigned blocksize = inode->i_sb->s_blocksize;
3800 unsigned max = blocksize - (offset & (blocksize - 1));
3801
3802 /*
3803 * correct length if it does not fall between
3804 * 'from' and the end of the block
3805 */
3806 if (length > max || length < 0)
3807 length = max;
3808
Jan Kara47e69352016-11-20 18:08:05 -05003809 if (IS_DAX(inode)) {
3810 return iomap_zero_range(inode, from, length, NULL,
3811 &ext4_iomap_ops);
3812 }
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003813 return __ext4_block_zero_page_range(handle, mapping, from, length);
3814}
3815
3816/*
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003817 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
3818 * up to the end of the block which corresponds to `from'.
3819 * This required during truncate. We need to physically zero the tail end
3820 * of that block so it doesn't yield old data if the file is later grown.
3821 */
Stephen Hemmingerc1978552014-05-12 10:50:23 -04003822static int ext4_block_truncate_page(handle_t *handle,
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003823 struct address_space *mapping, loff_t from)
3824{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003825 unsigned offset = from & (PAGE_SIZE-1);
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003826 unsigned length;
3827 unsigned blocksize;
3828 struct inode *inode = mapping->host;
3829
Theodore Ts'o0d068632017-02-14 11:31:15 -05003830 /* If we are processing an encrypted inode during orphan list handling */
Chandan Rajendra592ddec2018-12-12 15:20:10 +05303831 if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode))
Theodore Ts'o0d068632017-02-14 11:31:15 -05003832 return 0;
3833
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003834 blocksize = inode->i_sb->s_blocksize;
3835 length = blocksize - (offset & (blocksize - 1));
3836
3837 return ext4_block_zero_page_range(handle, mapping, from, length);
3838}
3839
Lukas Czernera87dd182013-05-27 23:32:35 -04003840int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
3841 loff_t lstart, loff_t length)
3842{
3843 struct super_block *sb = inode->i_sb;
3844 struct address_space *mapping = inode->i_mapping;
Lukas Czernere1be3a92013-07-01 08:12:39 -04003845 unsigned partial_start, partial_end;
Lukas Czernera87dd182013-05-27 23:32:35 -04003846 ext4_fsblk_t start, end;
3847 loff_t byte_end = (lstart + length - 1);
3848 int err = 0;
3849
Lukas Czernere1be3a92013-07-01 08:12:39 -04003850 partial_start = lstart & (sb->s_blocksize - 1);
3851 partial_end = byte_end & (sb->s_blocksize - 1);
3852
Lukas Czernera87dd182013-05-27 23:32:35 -04003853 start = lstart >> sb->s_blocksize_bits;
3854 end = byte_end >> sb->s_blocksize_bits;
3855
3856 /* Handle partial zero within the single block */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003857 if (start == end &&
3858 (partial_start || (partial_end != sb->s_blocksize - 1))) {
Lukas Czernera87dd182013-05-27 23:32:35 -04003859 err = ext4_block_zero_page_range(handle, mapping,
3860 lstart, length);
3861 return err;
3862 }
3863 /* Handle partial zero out on the start of the range */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003864 if (partial_start) {
Lukas Czernera87dd182013-05-27 23:32:35 -04003865 err = ext4_block_zero_page_range(handle, mapping,
3866 lstart, sb->s_blocksize);
3867 if (err)
3868 return err;
3869 }
3870 /* Handle partial zero out on the end of the range */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003871 if (partial_end != sb->s_blocksize - 1)
Lukas Czernera87dd182013-05-27 23:32:35 -04003872 err = ext4_block_zero_page_range(handle, mapping,
Lukas Czernere1be3a92013-07-01 08:12:39 -04003873 byte_end - partial_end,
3874 partial_end + 1);
Lukas Czernera87dd182013-05-27 23:32:35 -04003875 return err;
3876}
3877
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003878int ext4_can_truncate(struct inode *inode)
3879{
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003880 if (S_ISREG(inode->i_mode))
3881 return 1;
3882 if (S_ISDIR(inode->i_mode))
3883 return 1;
3884 if (S_ISLNK(inode->i_mode))
3885 return !ext4_inode_is_fast_symlink(inode);
3886 return 0;
3887}
3888
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003889/*
Jan Kara01127842015-12-07 14:34:49 -05003890 * We have to make sure i_disksize gets properly updated before we truncate
3891 * page cache due to hole punching or zero range. Otherwise i_disksize update
3892 * can get lost as it may have been postponed to submission of writeback but
3893 * that will never happen after we truncate page cache.
3894 */
3895int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset,
3896 loff_t len)
3897{
3898 handle_t *handle;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003899 int ret;
3900
Jan Kara01127842015-12-07 14:34:49 -05003901 loff_t size = i_size_read(inode);
3902
Al Viro59551022016-01-22 15:40:57 -05003903 WARN_ON(!inode_is_locked(inode));
Jan Kara01127842015-12-07 14:34:49 -05003904 if (offset > size || offset + len < size)
3905 return 0;
3906
3907 if (EXT4_I(inode)->i_disksize >= size)
3908 return 0;
3909
3910 handle = ext4_journal_start(inode, EXT4_HT_MISC, 1);
3911 if (IS_ERR(handle))
3912 return PTR_ERR(handle);
3913 ext4_update_i_disksize(inode, size);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003914 ret = ext4_mark_inode_dirty(handle, inode);
Jan Kara01127842015-12-07 14:34:49 -05003915 ext4_journal_stop(handle);
3916
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003917 return ret;
Jan Kara01127842015-12-07 14:34:49 -05003918}
3919
Ross Zwislerb1f38212018-09-11 13:31:16 -04003920static void ext4_wait_dax_page(struct ext4_inode_info *ei)
Ross Zwisler430657b2018-07-29 17:00:22 -04003921{
Ross Zwisler430657b2018-07-29 17:00:22 -04003922 up_write(&ei->i_mmap_sem);
3923 schedule();
3924 down_write(&ei->i_mmap_sem);
3925}
3926
3927int ext4_break_layouts(struct inode *inode)
3928{
3929 struct ext4_inode_info *ei = EXT4_I(inode);
3930 struct page *page;
Ross Zwisler430657b2018-07-29 17:00:22 -04003931 int error;
3932
3933 if (WARN_ON_ONCE(!rwsem_is_locked(&ei->i_mmap_sem)))
3934 return -EINVAL;
3935
3936 do {
Ross Zwisler430657b2018-07-29 17:00:22 -04003937 page = dax_layout_busy_page(inode->i_mapping);
3938 if (!page)
3939 return 0;
3940
3941 error = ___wait_var_event(&page->_refcount,
3942 atomic_read(&page->_refcount) == 1,
3943 TASK_INTERRUPTIBLE, 0, 0,
Ross Zwislerb1f38212018-09-11 13:31:16 -04003944 ext4_wait_dax_page(ei));
3945 } while (error == 0);
Ross Zwisler430657b2018-07-29 17:00:22 -04003946
3947 return error;
3948}
3949
Jan Kara01127842015-12-07 14:34:49 -05003950/*
Ross Zwislercca32b72016-09-22 11:49:38 -04003951 * ext4_punch_hole: punches a hole in a file by releasing the blocks
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003952 * associated with the given offset and length
3953 *
3954 * @inode: File inode
3955 * @offset: The offset where the hole will begin
3956 * @len: The length of the hole
3957 *
Anatol Pomozov4907cb72012-09-01 10:31:09 -07003958 * Returns: 0 on success or negative on failure
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003959 */
3960
Ashish Sangwanaeb28172013-07-01 08:12:38 -04003961int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length)
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003962{
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003963 struct super_block *sb = inode->i_sb;
3964 ext4_lblk_t first_block, stop_block;
3965 struct address_space *mapping = inode->i_mapping;
Lukas Czernera87dd182013-05-27 23:32:35 -04003966 loff_t first_block_offset, last_block_offset;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003967 handle_t *handle;
3968 unsigned int credits;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07003969 int ret = 0, ret2 = 0;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003970
Lukas Czernerb8a86842014-03-18 18:05:35 -04003971 trace_ext4_punch_hole(inode, offset, length, 0);
Zheng Liuaaddea82013-01-16 20:21:26 -05003972
Theodore Ts'oc1e82202019-08-23 22:38:00 -04003973 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
3974 if (ext4_has_inline_data(inode)) {
3975 down_write(&EXT4_I(inode)->i_mmap_sem);
3976 ret = ext4_convert_inline_data(inode);
3977 up_write(&EXT4_I(inode)->i_mmap_sem);
3978 if (ret)
3979 return ret;
3980 }
3981
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003982 /*
3983 * Write out all dirty pages to avoid race conditions
3984 * Then release them.
3985 */
Ross Zwislercca32b72016-09-22 11:49:38 -04003986 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003987 ret = filemap_write_and_wait_range(mapping, offset,
3988 offset + length - 1);
3989 if (ret)
3990 return ret;
3991 }
3992
Al Viro59551022016-01-22 15:40:57 -05003993 inode_lock(inode);
Lukas Czerner9ef06ce2014-04-12 09:47:00 -04003994
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003995 /* No need to punch hole beyond i_size */
3996 if (offset >= inode->i_size)
3997 goto out_mutex;
3998
3999 /*
4000 * If the hole extends beyond i_size, set the hole
4001 * to end after the page that contains i_size
4002 */
4003 if (offset + length > inode->i_size) {
4004 length = inode->i_size +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004005 PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) -
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004006 offset;
4007 }
4008
Jan Karaa3612932013-08-16 21:19:41 -04004009 if (offset & (sb->s_blocksize - 1) ||
4010 (offset + length) & (sb->s_blocksize - 1)) {
4011 /*
4012 * Attach jinode to inode for jbd2 if we do any zeroing of
4013 * partial block
4014 */
4015 ret = ext4_inode_attach_jinode(inode);
4016 if (ret < 0)
4017 goto out_mutex;
4018
4019 }
4020
Jan Karaea3d7202015-12-07 14:28:03 -05004021 /* Wait all existing dio workers, newcomers will block on i_mutex */
Jan Karaea3d7202015-12-07 14:28:03 -05004022 inode_dio_wait(inode);
4023
4024 /*
4025 * Prevent page faults from reinstantiating pages we have released from
4026 * page cache.
4027 */
4028 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04004029
4030 ret = ext4_break_layouts(inode);
4031 if (ret)
4032 goto out_dio;
4033
Lukas Czernera87dd182013-05-27 23:32:35 -04004034 first_block_offset = round_up(offset, sb->s_blocksize);
4035 last_block_offset = round_down((offset + length), sb->s_blocksize) - 1;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004036
Lukas Czernera87dd182013-05-27 23:32:35 -04004037 /* Now release the pages and zero block aligned part of pages*/
Jan Kara01127842015-12-07 14:34:49 -05004038 if (last_block_offset > first_block_offset) {
4039 ret = ext4_update_disksize_before_punch(inode, offset, length);
4040 if (ret)
4041 goto out_dio;
Lukas Czernera87dd182013-05-27 23:32:35 -04004042 truncate_pagecache_range(inode, first_block_offset,
4043 last_block_offset);
Jan Kara01127842015-12-07 14:34:49 -05004044 }
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004045
4046 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4047 credits = ext4_writepage_trans_blocks(inode);
4048 else
4049 credits = ext4_blocks_for_truncate(inode);
4050 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
4051 if (IS_ERR(handle)) {
4052 ret = PTR_ERR(handle);
4053 ext4_std_error(sb, ret);
4054 goto out_dio;
4055 }
4056
Lukas Czernera87dd182013-05-27 23:32:35 -04004057 ret = ext4_zero_partial_blocks(handle, inode, offset,
4058 length);
4059 if (ret)
4060 goto out_stop;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004061
4062 first_block = (offset + sb->s_blocksize - 1) >>
4063 EXT4_BLOCK_SIZE_BITS(sb);
4064 stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
4065
Lukas Czernereee597a2018-05-13 19:28:35 -04004066 /* If there are blocks to remove, do it */
4067 if (stop_block > first_block) {
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004068
Lukas Czernereee597a2018-05-13 19:28:35 -04004069 down_write(&EXT4_I(inode)->i_data_sem);
4070 ext4_discard_preallocations(inode);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004071
Lukas Czernereee597a2018-05-13 19:28:35 -04004072 ret = ext4_es_remove_extent(inode, first_block,
4073 stop_block - first_block);
4074 if (ret) {
4075 up_write(&EXT4_I(inode)->i_data_sem);
4076 goto out_stop;
4077 }
4078
4079 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4080 ret = ext4_ext_remove_space(inode, first_block,
4081 stop_block - 1);
4082 else
4083 ret = ext4_ind_remove_space(handle, inode, first_block,
4084 stop_block);
4085
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004086 up_write(&EXT4_I(inode)->i_data_sem);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004087 }
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004088 if (IS_SYNC(inode))
4089 ext4_handle_sync(handle);
Maxim Patlasove251f9b2014-02-20 16:58:05 -05004090
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004091 inode->i_mtime = inode->i_ctime = current_time(inode);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004092 ret2 = ext4_mark_inode_dirty(handle, inode);
4093 if (unlikely(ret2))
4094 ret = ret2;
Jan Kara67a7d5f2017-05-29 13:24:55 -04004095 if (ret >= 0)
4096 ext4_update_inode_fsync_trans(handle, inode, 1);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004097out_stop:
4098 ext4_journal_stop(handle);
4099out_dio:
Jan Karaea3d7202015-12-07 14:28:03 -05004100 up_write(&EXT4_I(inode)->i_mmap_sem);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004101out_mutex:
Al Viro59551022016-01-22 15:40:57 -05004102 inode_unlock(inode);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004103 return ret;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004104}
4105
Jan Karaa3612932013-08-16 21:19:41 -04004106int ext4_inode_attach_jinode(struct inode *inode)
4107{
4108 struct ext4_inode_info *ei = EXT4_I(inode);
4109 struct jbd2_inode *jinode;
4110
4111 if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal)
4112 return 0;
4113
4114 jinode = jbd2_alloc_inode(GFP_KERNEL);
4115 spin_lock(&inode->i_lock);
4116 if (!ei->jinode) {
4117 if (!jinode) {
4118 spin_unlock(&inode->i_lock);
4119 return -ENOMEM;
4120 }
4121 ei->jinode = jinode;
4122 jbd2_journal_init_jbd_inode(ei->jinode, inode);
4123 jinode = NULL;
4124 }
4125 spin_unlock(&inode->i_lock);
4126 if (unlikely(jinode != NULL))
4127 jbd2_free_inode(jinode);
4128 return 0;
4129}
4130
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004131/*
Mingming Cao617ba132006-10-11 01:20:53 -07004132 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004133 *
Mingming Cao617ba132006-10-11 01:20:53 -07004134 * We block out ext4_get_block() block instantiations across the entire
4135 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004136 * simultaneously on behalf of the same inode.
4137 *
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08004138 * As we work through the truncate and commit bits of it to the journal there
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004139 * is one core, guiding principle: the file's tree must always be consistent on
4140 * disk. We must be able to restart the truncate after a crash.
4141 *
4142 * The file's tree may be transiently inconsistent in memory (although it
4143 * probably isn't), but whenever we close off and commit a journal transaction,
4144 * the contents of (the filesystem + the journal) must be consistent and
4145 * restartable. It's pretty simple, really: bottom up, right to left (although
4146 * left-to-right works OK too).
4147 *
4148 * Note that at recovery time, journal replay occurs *before* the restart of
4149 * truncate against the orphan inode list.
4150 *
4151 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07004152 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004153 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07004154 * ext4_truncate() to have another go. So there will be instantiated blocks
4155 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004156 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07004157 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004158 */
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004159int ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004160{
Theodore Ts'o819c4922013-04-03 12:47:17 -04004161 struct ext4_inode_info *ei = EXT4_I(inode);
4162 unsigned int credits;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004163 int err = 0, err2;
Theodore Ts'o819c4922013-04-03 12:47:17 -04004164 handle_t *handle;
4165 struct address_space *mapping = inode->i_mapping;
Theodore Ts'o819c4922013-04-03 12:47:17 -04004166
Theodore Ts'o19b5ef62013-04-03 21:58:52 -04004167 /*
4168 * There is a possibility that we're either freeing the inode
Matthew Wilcoxe04027e2014-03-24 15:15:07 -04004169 * or it's a completely new inode. In those cases we might not
Theodore Ts'o19b5ef62013-04-03 21:58:52 -04004170 * have i_mutex locked because it's not necessary.
4171 */
4172 if (!(inode->i_state & (I_NEW|I_FREEING)))
Al Viro59551022016-01-22 15:40:57 -05004173 WARN_ON(!inode_is_locked(inode));
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004174 trace_ext4_truncate_enter(inode);
4175
Duane Griffin91ef4ca2008-07-11 19:27:31 -04004176 if (!ext4_can_truncate(inode))
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004177 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004178
Theodore Ts'o5534fb52009-09-17 09:34:16 -04004179 if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004180 ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
Theodore Ts'o7d8f9f72009-02-24 08:21:14 -05004181
Tao Maaef1c852012-12-10 14:06:02 -05004182 if (ext4_has_inline_data(inode)) {
4183 int has_inline = 1;
4184
Theodore Ts'o01daf942017-01-22 19:35:49 -05004185 err = ext4_inline_data_truncate(inode, &has_inline);
4186 if (err)
4187 return err;
Tao Maaef1c852012-12-10 14:06:02 -05004188 if (has_inline)
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004189 return 0;
Tao Maaef1c852012-12-10 14:06:02 -05004190 }
4191
Jan Karaa3612932013-08-16 21:19:41 -04004192 /* If we zero-out tail of the page, we have to create jinode for jbd2 */
4193 if (inode->i_size & (inode->i_sb->s_blocksize - 1)) {
4194 if (ext4_inode_attach_jinode(inode) < 0)
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004195 return 0;
Jan Karaa3612932013-08-16 21:19:41 -04004196 }
4197
Amir Goldsteinff9893d2011-06-27 16:36:31 -04004198 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Theodore Ts'o819c4922013-04-03 12:47:17 -04004199 credits = ext4_writepage_trans_blocks(inode);
Amir Goldsteinff9893d2011-06-27 16:36:31 -04004200 else
Theodore Ts'o819c4922013-04-03 12:47:17 -04004201 credits = ext4_blocks_for_truncate(inode);
4202
4203 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004204 if (IS_ERR(handle))
4205 return PTR_ERR(handle);
Theodore Ts'o819c4922013-04-03 12:47:17 -04004206
Lukas Czernereb3544c2013-05-27 23:32:35 -04004207 if (inode->i_size & (inode->i_sb->s_blocksize - 1))
4208 ext4_block_truncate_page(handle, mapping, inode->i_size);
Theodore Ts'o819c4922013-04-03 12:47:17 -04004209
4210 /*
4211 * We add the inode to the orphan list, so that if this
4212 * truncate spans multiple transactions, and we crash, we will
4213 * resume the truncate when the filesystem recovers. It also
4214 * marks the inode dirty, to catch the new size.
4215 *
4216 * Implication: the file must always be in a sane, consistent
4217 * truncatable state while each transaction commits.
4218 */
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004219 err = ext4_orphan_add(handle, inode);
4220 if (err)
Theodore Ts'o819c4922013-04-03 12:47:17 -04004221 goto out_stop;
4222
4223 down_write(&EXT4_I(inode)->i_data_sem);
4224
4225 ext4_discard_preallocations(inode);
4226
4227 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Theodore Ts'od0abb362016-11-13 22:02:28 -05004228 err = ext4_ext_truncate(handle, inode);
Theodore Ts'o819c4922013-04-03 12:47:17 -04004229 else
4230 ext4_ind_truncate(handle, inode);
4231
4232 up_write(&ei->i_data_sem);
Theodore Ts'od0abb362016-11-13 22:02:28 -05004233 if (err)
4234 goto out_stop;
Theodore Ts'o819c4922013-04-03 12:47:17 -04004235
4236 if (IS_SYNC(inode))
4237 ext4_handle_sync(handle);
4238
4239out_stop:
4240 /*
4241 * If this was a simple ftruncate() and the file will remain alive,
4242 * then we need to clear up the orphan record which we created above.
4243 * However, if this was a real unlink then we were called by
Wang Shilong58d86a52014-11-25 16:17:29 -05004244 * ext4_evict_inode(), and we allow that function to clean up the
Theodore Ts'o819c4922013-04-03 12:47:17 -04004245 * orphan info for us.
4246 */
4247 if (inode->i_nlink)
4248 ext4_orphan_del(handle, inode);
4249
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004250 inode->i_mtime = inode->i_ctime = current_time(inode);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07004251 err2 = ext4_mark_inode_dirty(handle, inode);
4252 if (unlikely(err2 && !err))
4253 err = err2;
Theodore Ts'o819c4922013-04-03 12:47:17 -04004254 ext4_journal_stop(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07004255
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004256 trace_ext4_truncate_exit(inode);
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004257 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004258}
4259
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004260/*
Mingming Cao617ba132006-10-11 01:20:53 -07004261 * ext4_get_inode_loc returns with an extra refcount against the inode's
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004262 * underlying buffer_head on success. If 'in_mem' is true, we have all
4263 * data in memory that is needed to recreate the on-disk version of this
4264 * inode.
4265 */
Mingming Cao617ba132006-10-11 01:20:53 -07004266static int __ext4_get_inode_loc(struct inode *inode,
4267 struct ext4_iloc *iloc, int in_mem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004268{
Theodore Ts'o240799c2008-10-09 23:53:47 -04004269 struct ext4_group_desc *gdp;
4270 struct buffer_head *bh;
4271 struct super_block *sb = inode->i_sb;
4272 ext4_fsblk_t block;
Linus Torvalds02f03c42019-09-29 17:59:23 -07004273 struct blk_plug plug;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004274 int inodes_per_block, inode_offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004275
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004276 iloc->bh = NULL;
Theodore Ts'oc37e9e02018-06-17 00:41:14 -04004277 if (inode->i_ino < EXT4_ROOT_INO ||
4278 inode->i_ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004279 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004280
Theodore Ts'o240799c2008-10-09 23:53:47 -04004281 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
4282 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
4283 if (!gdp)
4284 return -EIO;
4285
4286 /*
4287 * Figure out the offset within the block group inode table
4288 */
Tao Ma00d09882011-05-09 10:26:41 -04004289 inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004290 inode_offset = ((inode->i_ino - 1) %
4291 EXT4_INODES_PER_GROUP(sb));
4292 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
4293 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
4294
4295 bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05004296 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -05004297 return -ENOMEM;
Theodore Ts'o46f870d62019-11-21 13:09:43 -05004298 if (ext4_simulate_fail(sb, EXT4_SIM_INODE_EIO))
4299 goto simulate_eio;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004300 if (!buffer_uptodate(bh)) {
4301 lock_buffer(bh);
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04004302
4303 /*
4304 * If the buffer has the write error flag, we have failed
4305 * to write out another inode in the same block. In this
4306 * case, we don't have to read the block because we may
4307 * read the old inode data successfully.
4308 */
4309 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
4310 set_buffer_uptodate(bh);
4311
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004312 if (buffer_uptodate(bh)) {
4313 /* someone brought it uptodate while we waited */
4314 unlock_buffer(bh);
4315 goto has_buffer;
4316 }
4317
4318 /*
4319 * If we have all information of the inode in memory and this
4320 * is the only valid inode in the block, we need not read the
4321 * block.
4322 */
4323 if (in_mem) {
4324 struct buffer_head *bitmap_bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004325 int i, start;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004326
Theodore Ts'o240799c2008-10-09 23:53:47 -04004327 start = inode_offset & ~(inodes_per_block - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004328
4329 /* Is the inode bitmap in cache? */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004330 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
Wang Shilongaebf0242013-01-12 16:28:47 -05004331 if (unlikely(!bitmap_bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004332 goto make_io;
4333
4334 /*
4335 * If the inode bitmap isn't in cache then the
4336 * optimisation may end up performing two reads instead
4337 * of one, so skip it.
4338 */
4339 if (!buffer_uptodate(bitmap_bh)) {
4340 brelse(bitmap_bh);
4341 goto make_io;
4342 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04004343 for (i = start; i < start + inodes_per_block; i++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004344 if (i == inode_offset)
4345 continue;
Mingming Cao617ba132006-10-11 01:20:53 -07004346 if (ext4_test_bit(i, bitmap_bh->b_data))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004347 break;
4348 }
4349 brelse(bitmap_bh);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004350 if (i == start + inodes_per_block) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004351 /* all other inodes are free, so skip I/O */
4352 memset(bh->b_data, 0, bh->b_size);
4353 set_buffer_uptodate(bh);
4354 unlock_buffer(bh);
4355 goto has_buffer;
4356 }
4357 }
4358
4359make_io:
4360 /*
Theodore Ts'o240799c2008-10-09 23:53:47 -04004361 * If we need to do any I/O, try to pre-readahead extra
4362 * blocks from the inode table.
4363 */
Linus Torvalds02f03c42019-09-29 17:59:23 -07004364 blk_start_plug(&plug);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004365 if (EXT4_SB(sb)->s_inode_readahead_blks) {
4366 ext4_fsblk_t b, end, table;
4367 unsigned num;
Theodore Ts'o0d606e2c2013-04-23 08:59:35 -04004368 __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004369
4370 table = ext4_inode_table(sb, gdp);
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004371 /* s_inode_readahead_blks is always a power of 2 */
Theodore Ts'o0d606e2c2013-04-23 08:59:35 -04004372 b = block & ~((ext4_fsblk_t) ra_blks - 1);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004373 if (table > b)
4374 b = table;
Theodore Ts'o0d606e2c2013-04-23 08:59:35 -04004375 end = b + ra_blks;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004376 num = EXT4_INODES_PER_GROUP(sb);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04004377 if (ext4_has_group_desc_csum(sb))
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004378 num -= ext4_itable_unused_count(sb, gdp);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004379 table += num / inodes_per_block;
4380 if (end > table)
4381 end = table;
4382 while (b <= end)
Roman Gushchind87f6392020-02-28 16:14:11 -08004383 sb_breadahead_unmovable(sb, b++);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004384 }
4385
4386 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004387 * There are other valid inodes in the buffer, this inode
4388 * has in-inode xattrs, or we don't have this inode in memory.
4389 * Read the block from disk.
4390 */
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004391 trace_ext4_load_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004392 get_bh(bh);
4393 bh->b_end_io = end_buffer_read_sync;
Mike Christie2a222ca2016-06-05 14:31:43 -05004394 submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh);
Linus Torvalds02f03c42019-09-29 17:59:23 -07004395 blk_finish_plug(&plug);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004396 wait_on_buffer(bh);
4397 if (!buffer_uptodate(bh)) {
Theodore Ts'o46f870d62019-11-21 13:09:43 -05004398 simulate_eio:
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04004399 ext4_error_inode_block(inode, block, EIO,
Theodore Ts'oc398eda2010-07-27 11:56:40 -04004400 "unable to read itable block");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004401 brelse(bh);
4402 return -EIO;
4403 }
4404 }
4405has_buffer:
4406 iloc->bh = bh;
4407 return 0;
4408}
4409
Mingming Cao617ba132006-10-11 01:20:53 -07004410int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004411{
4412 /* We have all inode data except xattrs in memory here. */
Mingming Cao617ba132006-10-11 01:20:53 -07004413 return __ext4_get_inode_loc(inode, iloc,
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004414 !ext4_test_inode_state(inode, EXT4_STATE_XATTR));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004415}
4416
Ross Zwisler66425862017-10-12 12:00:59 -04004417static bool ext4_should_use_dax(struct inode *inode)
4418{
4419 if (!test_opt(inode->i_sb, DAX))
4420 return false;
4421 if (!S_ISREG(inode->i_mode))
4422 return false;
4423 if (ext4_should_journal_data(inode))
4424 return false;
4425 if (ext4_has_inline_data(inode))
4426 return false;
Chandan Rajendra592ddec2018-12-12 15:20:10 +05304427 if (ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT))
Ross Zwisler66425862017-10-12 12:00:59 -04004428 return false;
Eric Biggersc93d8f82019-07-22 09:26:24 -07004429 if (ext4_test_inode_flag(inode, EXT4_INODE_VERITY))
4430 return false;
Ross Zwisler66425862017-10-12 12:00:59 -04004431 return true;
4432}
4433
Mingming Cao617ba132006-10-11 01:20:53 -07004434void ext4_set_inode_flags(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004435{
Mingming Cao617ba132006-10-11 01:20:53 -07004436 unsigned int flags = EXT4_I(inode)->i_flags;
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004437 unsigned int new_fl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004438
Mingming Cao617ba132006-10-11 01:20:53 -07004439 if (flags & EXT4_SYNC_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004440 new_fl |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07004441 if (flags & EXT4_APPEND_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004442 new_fl |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07004443 if (flags & EXT4_IMMUTABLE_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004444 new_fl |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07004445 if (flags & EXT4_NOATIME_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004446 new_fl |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07004447 if (flags & EXT4_DIRSYNC_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004448 new_fl |= S_DIRSYNC;
Ross Zwisler66425862017-10-12 12:00:59 -04004449 if (ext4_should_use_dax(inode))
Ross Zwisler923ae0f2015-02-16 15:59:38 -08004450 new_fl |= S_DAX;
Eric Biggers2ee6a572017-10-09 12:15:35 -07004451 if (flags & EXT4_ENCRYPT_FL)
4452 new_fl |= S_ENCRYPTED;
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04004453 if (flags & EXT4_CASEFOLD_FL)
4454 new_fl |= S_CASEFOLD;
Eric Biggersc93d8f82019-07-22 09:26:24 -07004455 if (flags & EXT4_VERITY_FL)
4456 new_fl |= S_VERITY;
Theodore Ts'o5f16f322014-03-24 14:43:12 -04004457 inode_set_flags(inode, new_fl,
Eric Biggers2ee6a572017-10-09 12:15:35 -07004458 S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX|
Eric Biggersc93d8f82019-07-22 09:26:24 -07004459 S_ENCRYPTED|S_CASEFOLD|S_VERITY);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004460}
4461
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004462static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004463 struct ext4_inode_info *ei)
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004464{
4465 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004466 struct inode *inode = &(ei->vfs_inode);
4467 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004468
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004469 if (ext4_has_feature_huge_file(sb)) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004470 /* we are using combined 48 bit field */
4471 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
4472 le32_to_cpu(raw_inode->i_blocks_lo);
Theodore Ts'o07a03822010-06-14 09:54:48 -04004473 if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004474 /* i_blocks represent file system block size */
4475 return i_blocks << (inode->i_blkbits - 9);
4476 } else {
4477 return i_blocks;
4478 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004479 } else {
4480 return le32_to_cpu(raw_inode->i_blocks_lo);
4481 }
4482}
Jan Karaff9ddf72007-07-18 09:24:20 -04004483
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004484static inline int ext4_iget_extra_inode(struct inode *inode,
Tao Ma152a7b02012-12-02 11:13:24 -05004485 struct ext4_inode *raw_inode,
4486 struct ext4_inode_info *ei)
4487{
4488 __le32 *magic = (void *)raw_inode +
4489 EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004490
Eric Biggers290ab232016-12-01 14:51:58 -05004491 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize + sizeof(__le32) <=
4492 EXT4_INODE_SIZE(inode->i_sb) &&
4493 *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
Tao Ma152a7b02012-12-02 11:13:24 -05004494 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004495 return ext4_find_inline_data_nolock(inode);
Tao Maf19d5872012-12-10 14:05:51 -05004496 } else
4497 EXT4_I(inode)->i_inline_off = 0;
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004498 return 0;
Tao Ma152a7b02012-12-02 11:13:24 -05004499}
4500
Li Xi040cb372016-01-08 16:01:21 -05004501int ext4_get_projid(struct inode *inode, kprojid_t *projid)
4502{
Kaho Ng0b7b7772016-09-05 23:11:58 -04004503 if (!ext4_has_feature_project(inode->i_sb))
Li Xi040cb372016-01-08 16:01:21 -05004504 return -EOPNOTSUPP;
4505 *projid = EXT4_I(inode)->i_projid;
4506 return 0;
4507}
4508
Eryu Guane254d1a2018-05-10 11:55:31 -04004509/*
4510 * ext4 has self-managed i_version for ea inodes, it stores the lower 32bit of
4511 * refcount in i_version, so use raw values if inode has EXT4_EA_INODE_FL flag
4512 * set.
4513 */
4514static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val)
4515{
4516 if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
4517 inode_set_iversion_raw(inode, val);
4518 else
4519 inode_set_iversion_queried(inode, val);
4520}
4521static inline u64 ext4_inode_peek_iversion(const struct inode *inode)
4522{
4523 if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
4524 return inode_peek_iversion_raw(inode);
4525 else
4526 return inode_peek_iversion(inode);
4527}
4528
Theodore Ts'o8a363972018-12-19 12:29:13 -05004529struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
4530 ext4_iget_flags flags, const char *function,
4531 unsigned int line)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004532{
Mingming Cao617ba132006-10-11 01:20:53 -07004533 struct ext4_iloc iloc;
4534 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08004535 struct ext4_inode_info *ei;
David Howells1d1fe1e2008-02-07 00:15:37 -08004536 struct inode *inode;
Jan Karab436b9b2009-12-08 23:51:10 -05004537 journal_t *journal = EXT4_SB(sb)->s_journal;
David Howells1d1fe1e2008-02-07 00:15:37 -08004538 long ret;
Darrick J. Wong7e6e1ef2016-12-10 09:55:01 -05004539 loff_t size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004540 int block;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004541 uid_t i_uid;
4542 gid_t i_gid;
Li Xi040cb372016-01-08 16:01:21 -05004543 projid_t i_projid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004544
Theodore Ts'o191ce172018-12-31 22:34:31 -05004545 if ((!(flags & EXT4_IGET_SPECIAL) &&
Theodore Ts'o8a363972018-12-19 12:29:13 -05004546 (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)) ||
4547 (ino < EXT4_ROOT_INO) ||
4548 (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))) {
4549 if (flags & EXT4_IGET_HANDLE)
4550 return ERR_PTR(-ESTALE);
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04004551 __ext4_error(sb, function, line, EFSCORRUPTED, 0,
Theodore Ts'o8a363972018-12-19 12:29:13 -05004552 "inode #%lu: comm %s: iget: illegal inode #",
4553 ino, current->comm);
4554 return ERR_PTR(-EFSCORRUPTED);
4555 }
4556
David Howells1d1fe1e2008-02-07 00:15:37 -08004557 inode = iget_locked(sb, ino);
4558 if (!inode)
4559 return ERR_PTR(-ENOMEM);
4560 if (!(inode->i_state & I_NEW))
4561 return inode;
4562
4563 ei = EXT4_I(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05004564 iloc.bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004565
David Howells1d1fe1e2008-02-07 00:15:37 -08004566 ret = __ext4_get_inode_loc(inode, &iloc, 0);
4567 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004568 goto bad_inode;
Mingming Cao617ba132006-10-11 01:20:53 -07004569 raw_inode = ext4_raw_inode(&iloc);
Darrick J. Wong814525f2012-04-29 18:31:10 -04004570
Theodore Ts'o8e4b5ea2018-03-29 21:56:09 -04004571 if ((ino == EXT4_ROOT_INO) && (raw_inode->i_links_count == 0)) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004572 ext4_error_inode(inode, function, line, 0,
4573 "iget: root inode unallocated");
Theodore Ts'o8e4b5ea2018-03-29 21:56:09 -04004574 ret = -EFSCORRUPTED;
4575 goto bad_inode;
4576 }
4577
Theodore Ts'o8a363972018-12-19 12:29:13 -05004578 if ((flags & EXT4_IGET_HANDLE) &&
4579 (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) {
4580 ret = -ESTALE;
4581 goto bad_inode;
4582 }
4583
Darrick J. Wong814525f2012-04-29 18:31:10 -04004584 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4585 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
4586 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
Eric Biggers2dc8d9e2016-12-01 14:43:33 -05004587 EXT4_INODE_SIZE(inode->i_sb) ||
4588 (ei->i_extra_isize & 3)) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004589 ext4_error_inode(inode, function, line, 0,
4590 "iget: bad extra_isize %u "
4591 "(inode size %u)",
Eric Biggers2dc8d9e2016-12-01 14:43:33 -05004592 ei->i_extra_isize,
4593 EXT4_INODE_SIZE(inode->i_sb));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004594 ret = -EFSCORRUPTED;
Darrick J. Wong814525f2012-04-29 18:31:10 -04004595 goto bad_inode;
4596 }
4597 } else
4598 ei->i_extra_isize = 0;
4599
4600 /* Precompute checksum seed for inode metadata */
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04004601 if (ext4_has_metadata_csum(sb)) {
Darrick J. Wong814525f2012-04-29 18:31:10 -04004602 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4603 __u32 csum;
4604 __le32 inum = cpu_to_le32(inode->i_ino);
4605 __le32 gen = raw_inode->i_generation;
4606 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
4607 sizeof(inum));
4608 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
4609 sizeof(gen));
4610 }
4611
Theodore Ts'o46f870d62019-11-21 13:09:43 -05004612 if (!ext4_inode_csum_verify(inode, raw_inode, ei) ||
4613 ext4_simulate_fail(sb, EXT4_SIM_INODE_CRC)) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04004614 ext4_error_inode_err(inode, function, line, 0, EFSBADCRC,
4615 "iget: checksum invalid");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004616 ret = -EFSBADCRC;
Darrick J. Wong814525f2012-04-29 18:31:10 -04004617 goto bad_inode;
4618 }
4619
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004620 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004621 i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4622 i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Kaho Ng0b7b7772016-09-05 23:11:58 -04004623 if (ext4_has_feature_project(sb) &&
Li Xi040cb372016-01-08 16:01:21 -05004624 EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE &&
4625 EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
4626 i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid);
4627 else
4628 i_projid = EXT4_DEF_PROJID;
4629
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004630 if (!(test_opt(inode->i_sb, NO_UID32))) {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004631 i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4632 i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004633 }
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004634 i_uid_write(inode, i_uid);
4635 i_gid_write(inode, i_gid);
Li Xi040cb372016-01-08 16:01:21 -05004636 ei->i_projid = make_kprojid(&init_user_ns, i_projid);
Miklos Szeredibfe86842011-10-28 14:13:29 +02004637 set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004638
Theodore Ts'o353eb832011-01-10 12:18:25 -05004639 ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
Tao Ma67cf5b02012-12-10 14:04:46 -05004640 ei->i_inline_off = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004641 ei->i_dir_start_lookup = 0;
4642 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4643 /* We now have enough fields to check if the inode was active or not.
4644 * This is needed because nfsd might try to access dead inodes
4645 * the test is that same one that e2fsck uses
4646 * NeilBrown 1999oct15
4647 */
4648 if (inode->i_nlink == 0) {
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004649 if ((inode->i_mode == 0 ||
4650 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) &&
4651 ino != EXT4_BOOT_LOADER_INO) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004652 /* this inode is deleted */
David Howells1d1fe1e2008-02-07 00:15:37 -08004653 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004654 goto bad_inode;
4655 }
4656 /* The only unlinked inodes we let through here have
4657 * valid i_mode and are being read by the orphan
4658 * recovery code: that's fine, we're about to complete
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004659 * the process of deleting those.
4660 * OR it is the EXT4_BOOT_LOADER_INO which is
4661 * not initialized on a new filesystem. */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004662 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004663 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Toshi Kanicce6c9f2018-09-15 21:37:59 -04004664 ext4_set_inode_flags(inode);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004665 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004666 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004667 if (ext4_has_feature_64bit(sb))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004668 ei->i_file_acl |=
4669 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04004670 inode->i_size = ext4_isize(sb, raw_inode);
Darrick J. Wong7e6e1ef2016-12-10 09:55:01 -05004671 if ((size = i_size_read(inode)) < 0) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004672 ext4_error_inode(inode, function, line, 0,
4673 "iget: bad i_size value: %lld", size);
Darrick J. Wong7e6e1ef2016-12-10 09:55:01 -05004674 ret = -EFSCORRUPTED;
4675 goto bad_inode;
4676 }
Jan Kara48a34312020-02-10 15:43:16 +01004677 /*
4678 * If dir_index is not enabled but there's dir with INDEX flag set,
4679 * we'd normally treat htree data as empty space. But with metadata
4680 * checksumming that corrupts checksums so forbid that.
4681 */
4682 if (!ext4_has_feature_dir_index(sb) && ext4_has_metadata_csum(sb) &&
4683 ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) {
4684 ext4_error_inode(inode, function, line, 0,
4685 "iget: Dir with htree data on filesystem without dir_index feature.");
4686 ret = -EFSCORRUPTED;
4687 goto bad_inode;
4688 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004689 ei->i_disksize = inode->i_size;
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03004690#ifdef CONFIG_QUOTA
4691 ei->i_reserved_quota = 0;
4692#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004693 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4694 ei->i_block_group = iloc.block_group;
Theodore Ts'oa4912122009-03-12 12:18:34 -04004695 ei->i_last_alloc_group = ~0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004696 /*
4697 * NOTE! The in-memory inode i_data array is in little-endian order
4698 * even on big-endian machines: we do NOT byteswap the block numbers!
4699 */
Mingming Cao617ba132006-10-11 01:20:53 -07004700 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004701 ei->i_data[block] = raw_inode->i_block[block];
4702 INIT_LIST_HEAD(&ei->i_orphan);
4703
Jan Karab436b9b2009-12-08 23:51:10 -05004704 /*
4705 * Set transaction id's of transactions that have to be committed
4706 * to finish f[data]sync. We set them to currently running transaction
4707 * as we cannot be sure that the inode or some of its metadata isn't
4708 * part of the transaction - the inode could have been reclaimed and
4709 * now it is reread from disk.
4710 */
4711 if (journal) {
4712 transaction_t *transaction;
4713 tid_t tid;
4714
Theodore Ts'oa931da62010-08-03 21:35:12 -04004715 read_lock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05004716 if (journal->j_running_transaction)
4717 transaction = journal->j_running_transaction;
4718 else
4719 transaction = journal->j_committing_transaction;
4720 if (transaction)
4721 tid = transaction->t_tid;
4722 else
4723 tid = journal->j_commit_sequence;
Theodore Ts'oa931da62010-08-03 21:35:12 -04004724 read_unlock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05004725 ei->i_sync_tid = tid;
4726 ei->i_datasync_tid = tid;
4727 }
4728
Eric Sandeen0040d982008-02-05 22:36:43 -05004729 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004730 if (ei->i_extra_isize == 0) {
4731 /* The extra space is currently unused. Use it. */
Eric Biggers2dc8d9e2016-12-01 14:43:33 -05004732 BUILD_BUG_ON(sizeof(struct ext4_inode) & 3);
Mingming Cao617ba132006-10-11 01:20:53 -07004733 ei->i_extra_isize = sizeof(struct ext4_inode) -
4734 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004735 } else {
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004736 ret = ext4_iget_extra_inode(inode, raw_inode, ei);
4737 if (ret)
4738 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004739 }
Darrick J. Wong814525f2012-04-29 18:31:10 -04004740 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004741
Kalpak Shahef7f3832007-07-18 09:15:20 -04004742 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4743 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4744 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4745 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4746
Theodore Ts'oed3654e2014-03-24 14:09:06 -04004747 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
Jeff Laytonee73f9a2018-01-09 08:21:39 -05004748 u64 ivers = le32_to_cpu(raw_inode->i_disk_version);
4749
Theodore Ts'oc4f65702014-03-20 00:32:57 -04004750 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4751 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
Jeff Laytonee73f9a2018-01-09 08:21:39 -05004752 ivers |=
Theodore Ts'oc4f65702014-03-20 00:32:57 -04004753 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
4754 }
Eryu Guane254d1a2018-05-10 11:55:31 -04004755 ext4_inode_set_iversion_queried(inode, ivers);
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004756 }
4757
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004758 ret = 0;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004759 if (ei->i_file_acl &&
Theodore Ts'o10329882009-11-15 15:29:56 -05004760 !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004761 ext4_error_inode(inode, function, line, 0,
4762 "iget: bad extended attribute block %llu",
Theodore Ts'o24676da2010-05-16 21:00:00 -04004763 ei->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004764 ret = -EFSCORRUPTED;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004765 goto bad_inode;
Tao Maf19d5872012-12-10 14:05:51 -05004766 } else if (!ext4_has_inline_data(inode)) {
Liu Songbc716522018-08-02 00:11:16 -04004767 /* validate the block references in the inode */
4768 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4769 (S_ISLNK(inode->i_mode) &&
4770 !ext4_inode_is_fast_symlink(inode))) {
4771 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Tao Maf19d5872012-12-10 14:05:51 -05004772 ret = ext4_ext_check_inode(inode);
Liu Songbc716522018-08-02 00:11:16 -04004773 else
4774 ret = ext4_ind_check_inode(inode);
Tao Maf19d5872012-12-10 14:05:51 -05004775 }
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004776 }
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004777 if (ret)
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004778 goto bad_inode;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -04004779
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004780 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004781 inode->i_op = &ext4_file_inode_operations;
Boaz Harroshbe64f882015-04-15 16:15:17 -07004782 inode->i_fop = &ext4_file_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07004783 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004784 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004785 inode->i_op = &ext4_dir_inode_operations;
4786 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004787 } else if (S_ISLNK(inode->i_mode)) {
Luis R. Rodriguez6390d332018-05-13 16:45:56 -04004788 /* VFS does not allow setting these so must be corruption */
4789 if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004790 ext4_error_inode(inode, function, line, 0,
4791 "iget: immutable or append flags "
4792 "not allowed on symlinks");
Luis R. Rodriguez6390d332018-05-13 16:45:56 -04004793 ret = -EFSCORRUPTED;
4794 goto bad_inode;
4795 }
Chandan Rajendra592ddec2018-12-12 15:20:10 +05304796 if (IS_ENCRYPTED(inode)) {
Al Viroa7a67e82015-04-27 17:51:30 -04004797 inode->i_op = &ext4_encrypted_symlink_inode_operations;
4798 ext4_set_aops(inode);
4799 } else if (ext4_inode_is_fast_symlink(inode)) {
Al Viro75e75662015-05-02 10:13:58 -04004800 inode->i_link = (char *)ei->i_data;
Mingming Cao617ba132006-10-11 01:20:53 -07004801 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00004802 nd_terminate_link(ei->i_data, inode->i_size,
4803 sizeof(ei->i_data) - 1);
4804 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004805 inode->i_op = &ext4_symlink_inode_operations;
4806 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004807 }
Al Viro21fc61c2015-11-17 01:07:57 -05004808 inode_nohighmem(inode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004809 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
4810 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004811 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004812 if (raw_inode->i_block[0])
4813 init_special_inode(inode, inode->i_mode,
4814 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4815 else
4816 init_special_inode(inode, inode->i_mode,
4817 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004818 } else if (ino == EXT4_BOOT_LOADER_INO) {
4819 make_bad_inode(inode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004820 } else {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004821 ret = -EFSCORRUPTED;
Theodore Ts'o8a363972018-12-19 12:29:13 -05004822 ext4_error_inode(inode, function, line, 0,
4823 "iget: bogus i_mode (%o)", inode->i_mode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004824 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004825 }
Theodore Ts'o6456ca62019-09-03 01:43:17 -04004826 if (IS_CASEFOLDED(inode) && !ext4_has_feature_casefold(inode->i_sb))
4827 ext4_error_inode(inode, function, line, 0,
4828 "casefold flag without casefold feature");
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004829 brelse(iloc.bh);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04004830
David Howells1d1fe1e2008-02-07 00:15:37 -08004831 unlock_new_inode(inode);
4832 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004833
4834bad_inode:
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004835 brelse(iloc.bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004836 iget_failed(inode);
4837 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004838}
4839
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004840static int ext4_inode_blocks_set(handle_t *handle,
4841 struct ext4_inode *raw_inode,
4842 struct ext4_inode_info *ei)
4843{
4844 struct inode *inode = &(ei->vfs_inode);
Qian Cai28936b62020-02-21 23:32:58 -05004845 u64 i_blocks = READ_ONCE(inode->i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004846 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004847
4848 if (i_blocks <= ~0U) {
4849 /*
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004850 * i_blocks can be represented in a 32 bit variable
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004851 * as multiple of 512 bytes
4852 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004853 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004854 raw_inode->i_blocks_high = 0;
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004855 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004856 return 0;
4857 }
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004858 if (!ext4_has_feature_huge_file(sb))
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004859 return -EFBIG;
4860
4861 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004862 /*
4863 * i_blocks can be represented in a 48 bit variable
4864 * as multiple of 512 bytes
4865 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004866 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004867 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004868 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004869 } else {
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004870 ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004871 /* i_block is stored in file system block size */
4872 i_blocks = i_blocks >> (inode->i_blkbits - 9);
4873 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4874 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004875 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004876 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004877}
4878
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004879struct other_inode {
4880 unsigned long orig_ino;
4881 struct ext4_inode *raw_inode;
4882};
4883
4884static int other_inode_match(struct inode * inode, unsigned long ino,
4885 void *data)
4886{
4887 struct other_inode *oi = (struct other_inode *) data;
4888
4889 if ((inode->i_ino != ino) ||
4890 (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW |
Christoph Hellwig0e11f642018-02-21 07:54:49 -08004891 I_DIRTY_INODE)) ||
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004892 ((inode->i_state & I_DIRTY_TIME) == 0))
4893 return 0;
4894 spin_lock(&inode->i_lock);
4895 if (((inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW |
Christoph Hellwig0e11f642018-02-21 07:54:49 -08004896 I_DIRTY_INODE)) == 0) &&
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004897 (inode->i_state & I_DIRTY_TIME)) {
4898 struct ext4_inode_info *ei = EXT4_I(inode);
4899
4900 inode->i_state &= ~(I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED);
4901 spin_unlock(&inode->i_lock);
4902
4903 spin_lock(&ei->i_raw_lock);
4904 EXT4_INODE_SET_XTIME(i_ctime, inode, oi->raw_inode);
4905 EXT4_INODE_SET_XTIME(i_mtime, inode, oi->raw_inode);
4906 EXT4_INODE_SET_XTIME(i_atime, inode, oi->raw_inode);
4907 ext4_inode_csum_set(inode, oi->raw_inode, ei);
4908 spin_unlock(&ei->i_raw_lock);
4909 trace_ext4_other_inode_update_time(inode, oi->orig_ino);
4910 return -1;
4911 }
4912 spin_unlock(&inode->i_lock);
4913 return -1;
4914}
4915
4916/*
4917 * Opportunistically update the other time fields for other inodes in
4918 * the same inode table block.
4919 */
4920static void ext4_update_other_inodes_time(struct super_block *sb,
4921 unsigned long orig_ino, char *buf)
4922{
4923 struct other_inode oi;
4924 unsigned long ino;
4925 int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
4926 int inode_size = EXT4_INODE_SIZE(sb);
4927
4928 oi.orig_ino = orig_ino;
Theodore Ts'o0f0ff9a2015-07-01 23:37:46 -04004929 /*
4930 * Calculate the first inode in the inode table block. Inode
4931 * numbers are one-based. That is, the first inode in a block
4932 * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1).
4933 */
4934 ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1;
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004935 for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) {
4936 if (ino == orig_ino)
4937 continue;
4938 oi.raw_inode = (struct ext4_inode *) buf;
4939 (void) find_inode_nowait(sb, ino, other_inode_match, &oi);
4940 }
4941}
4942
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004943/*
4944 * Post the struct inode info into an on-disk inode location in the
4945 * buffer-cache. This gobbles the caller's reference to the
4946 * buffer_head in the inode location struct.
4947 *
4948 * The caller must have write access to iloc->bh.
4949 */
Mingming Cao617ba132006-10-11 01:20:53 -07004950static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004951 struct inode *inode,
Frank Mayhar830156c2009-09-29 10:07:47 -04004952 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004953{
Mingming Cao617ba132006-10-11 01:20:53 -07004954 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
4955 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004956 struct buffer_head *bh = iloc->bh;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004957 struct super_block *sb = inode->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004958 int err = 0, rc, block;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004959 int need_datasync = 0, set_large_file = 0;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004960 uid_t i_uid;
4961 gid_t i_gid;
Li Xi040cb372016-01-08 16:01:21 -05004962 projid_t i_projid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004963
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004964 spin_lock(&ei->i_raw_lock);
4965
4966 /* For fields not tracked in the in-memory inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004967 * initialise them to zero for new inodes. */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004968 if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
Mingming Cao617ba132006-10-11 01:20:53 -07004969 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004970
4971 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004972 i_uid = i_uid_read(inode);
4973 i_gid = i_gid_read(inode);
Li Xi040cb372016-01-08 16:01:21 -05004974 i_projid = from_kprojid(&init_user_ns, ei->i_projid);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004975 if (!(test_opt(inode->i_sb, NO_UID32))) {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004976 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid));
4977 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004978/*
4979 * Fix up interoperability with old kernels. Otherwise, old inodes get
4980 * re-used with the upper 16 bits of the uid/gid intact
4981 */
Daeho Jeong93e3b4e2016-09-05 22:56:10 -04004982 if (ei->i_dtime && list_empty(&ei->i_orphan)) {
4983 raw_inode->i_uid_high = 0;
4984 raw_inode->i_gid_high = 0;
4985 } else {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004986 raw_inode->i_uid_high =
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004987 cpu_to_le16(high_16_bits(i_uid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004988 raw_inode->i_gid_high =
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004989 cpu_to_le16(high_16_bits(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004990 }
4991 } else {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004992 raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid));
4993 raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004994 raw_inode->i_uid_high = 0;
4995 raw_inode->i_gid_high = 0;
4996 }
4997 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004998
4999 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
5000 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
5001 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
5002 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
5003
Li Xibce92d52014-10-01 22:11:06 -04005004 err = ext4_inode_blocks_set(handle, raw_inode, ei);
5005 if (err) {
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005006 spin_unlock(&ei->i_raw_lock);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05005007 goto out_brelse;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005008 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005009 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Theodore Ts'o353eb832011-01-10 12:18:25 -05005010 raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
Theodore Ts'oed3654e2014-03-24 14:09:06 -04005011 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT)))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07005012 raw_inode->i_file_acl_high =
5013 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05005014 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Qiujun Huangdce8e232020-02-24 23:02:46 +08005015 if (READ_ONCE(ei->i_disksize) != ext4_isize(inode->i_sb, raw_inode)) {
Jan Karab71fc072012-09-26 21:52:20 -04005016 ext4_isize_set(raw_inode, ei->i_disksize);
5017 need_datasync = 1;
5018 }
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05005019 if (ei->i_disksize > 0x7fffffffULL) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005020 if (!ext4_has_feature_large_file(sb) ||
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05005021 EXT4_SB(sb)->s_es->s_rev_level ==
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005022 cpu_to_le32(EXT4_GOOD_OLD_REV))
5023 set_large_file = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005024 }
5025 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
5026 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
5027 if (old_valid_dev(inode->i_rdev)) {
5028 raw_inode->i_block[0] =
5029 cpu_to_le32(old_encode_dev(inode->i_rdev));
5030 raw_inode->i_block[1] = 0;
5031 } else {
5032 raw_inode->i_block[0] = 0;
5033 raw_inode->i_block[1] =
5034 cpu_to_le32(new_encode_dev(inode->i_rdev));
5035 raw_inode->i_block[2] = 0;
5036 }
Tao Maf19d5872012-12-10 14:05:51 -05005037 } else if (!ext4_has_inline_data(inode)) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04005038 for (block = 0; block < EXT4_N_BLOCKS; block++)
5039 raw_inode->i_block[block] = ei->i_data[block];
Tao Maf19d5872012-12-10 14:05:51 -05005040 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005041
Theodore Ts'oed3654e2014-03-24 14:09:06 -04005042 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
Eryu Guane254d1a2018-05-10 11:55:31 -04005043 u64 ivers = ext4_inode_peek_iversion(inode);
Jeff Laytonee73f9a2018-01-09 08:21:39 -05005044
5045 raw_inode->i_disk_version = cpu_to_le32(ivers);
Theodore Ts'oc4f65702014-03-20 00:32:57 -04005046 if (ei->i_extra_isize) {
5047 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
5048 raw_inode->i_version_hi =
Jeff Laytonee73f9a2018-01-09 08:21:39 -05005049 cpu_to_le32(ivers >> 32);
Theodore Ts'oc4f65702014-03-20 00:32:57 -04005050 raw_inode->i_extra_isize =
5051 cpu_to_le16(ei->i_extra_isize);
5052 }
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005053 }
Li Xi040cb372016-01-08 16:01:21 -05005054
Kaho Ng0b7b7772016-09-05 23:11:58 -04005055 BUG_ON(!ext4_has_feature_project(inode->i_sb) &&
Li Xi040cb372016-01-08 16:01:21 -05005056 i_projid != EXT4_DEF_PROJID);
5057
5058 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
5059 EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
5060 raw_inode->i_projid = cpu_to_le32(i_projid);
5061
Darrick J. Wong814525f2012-04-29 18:31:10 -04005062 ext4_inode_csum_set(inode, raw_inode, ei);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005063 spin_unlock(&ei->i_raw_lock);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08005064 if (inode->i_sb->s_flags & SB_LAZYTIME)
Theodore Ts'oa26f4992015-02-02 00:37:02 -05005065 ext4_update_other_inodes_time(inode->i_sb, inode->i_ino,
5066 bh->b_data);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005067
Frank Mayhar830156c2009-09-29 10:07:47 -04005068 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05005069 rc = ext4_handle_dirty_metadata(handle, NULL, bh);
Frank Mayhar830156c2009-09-29 10:07:47 -04005070 if (!err)
5071 err = rc;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005072 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005073 if (set_large_file) {
liang xie5d601252014-05-12 22:06:43 -04005074 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access");
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005075 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
5076 if (err)
5077 goto out_brelse;
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005078 ext4_set_feature_large_file(sb);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005079 ext4_handle_sync(handle);
5080 err = ext4_handle_dirty_super(handle, sb);
5081 }
Jan Karab71fc072012-09-26 21:52:20 -04005082 ext4_update_inode_fsync_trans(handle, inode, need_datasync);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005083out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04005084 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07005085 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005086 return err;
5087}
5088
5089/*
Mingming Cao617ba132006-10-11 01:20:53 -07005090 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005091 *
5092 * We are called from a few places:
5093 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005094 * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005095 * Here, there will be no transaction running. We wait for any running
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005096 * transaction to commit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005097 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005098 * - Within flush work (sys_sync(), kupdate and such).
5099 * We wait on commit, if told to.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005100 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005101 * - Within iput_final() -> write_inode_now()
5102 * We wait on commit, if told to.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005103 *
5104 * In all cases it is actually safe for us to return without doing anything,
5105 * because the inode has been copied into a raw inode buffer in
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005106 * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL
5107 * writeback.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005108 *
5109 * Note that we are absolutely dependent upon all inode dirtiers doing the
5110 * right thing: they *must* call mark_inode_dirty() after dirtying info in
5111 * which we are interested.
5112 *
5113 * It would be a bug for them to not do this. The code:
5114 *
5115 * mark_inode_dirty(inode)
5116 * stuff();
5117 * inode->i_size = expr;
5118 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005119 * is in error because write_inode() could occur while `stuff()' is running,
5120 * and the new i_size will be lost. Plus the inode will no longer be on the
5121 * superblock's dirty inode list.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005122 */
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005123int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005124{
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005125 int err;
5126
Theodore Ts'o18f2c4f2018-12-19 14:36:58 -05005127 if (WARN_ON_ONCE(current->flags & PF_MEMALLOC) ||
5128 sb_rdonly(inode->i_sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005129 return 0;
5130
Theodore Ts'o18f2c4f2018-12-19 14:36:58 -05005131 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5132 return -EIO;
5133
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005134 if (EXT4_SB(inode->i_sb)->s_journal) {
5135 if (ext4_journal_current_handle()) {
5136 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
5137 dump_stack();
5138 return -EIO;
5139 }
5140
Jan Kara10542c22014-03-04 10:50:50 -05005141 /*
5142 * No need to force transaction in WB_SYNC_NONE mode. Also
5143 * ext4_sync_fs() will force the commit after everything is
5144 * written.
5145 */
5146 if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync)
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005147 return 0;
5148
Theodore Ts'o18f2c4f2018-12-19 14:36:58 -05005149 err = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
5150 EXT4_I(inode)->i_sync_tid);
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005151 } else {
5152 struct ext4_iloc iloc;
5153
Curt Wohlgemuth8b472d72010-04-03 16:45:06 -04005154 err = __ext4_get_inode_loc(inode, &iloc, 0);
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005155 if (err)
5156 return err;
Jan Kara10542c22014-03-04 10:50:50 -05005157 /*
5158 * sync(2) will flush the whole buffer cache. No need to do
5159 * it here separately for each inode.
5160 */
5161 if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
Frank Mayhar830156c2009-09-29 10:07:47 -04005162 sync_dirty_buffer(iloc.bh);
5163 if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04005164 ext4_error_inode_block(inode, iloc.bh->b_blocknr, EIO,
5165 "IO error syncing inode");
Frank Mayhar830156c2009-09-29 10:07:47 -04005166 err = -EIO;
5167 }
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04005168 brelse(iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005169 }
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005170 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005171}
5172
5173/*
Jan Kara53e87262012-12-25 13:29:52 -05005174 * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
5175 * buffers that are attached to a page stradding i_size and are undergoing
5176 * commit. In that case we have to wait for commit to finish and try again.
5177 */
5178static void ext4_wait_for_tail_page_commit(struct inode *inode)
5179{
5180 struct page *page;
5181 unsigned offset;
5182 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
5183 tid_t commit_tid = 0;
5184 int ret;
5185
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005186 offset = inode->i_size & (PAGE_SIZE - 1);
Jan Kara53e87262012-12-25 13:29:52 -05005187 /*
yangerkun565333a2019-09-19 14:35:08 +08005188 * If the page is fully truncated, we don't need to wait for any commit
5189 * (and we even should not as __ext4_journalled_invalidatepage() may
5190 * strip all buffers from the page but keep the page dirty which can then
5191 * confuse e.g. concurrent ext4_writepage() seeing dirty page without
5192 * buffers). Also we don't need to wait for any commit if all buffers in
5193 * the page remain valid. This is most beneficial for the common case of
5194 * blocksize == PAGESIZE.
Jan Kara53e87262012-12-25 13:29:52 -05005195 */
yangerkun565333a2019-09-19 14:35:08 +08005196 if (!offset || offset > (PAGE_SIZE - i_blocksize(inode)))
Jan Kara53e87262012-12-25 13:29:52 -05005197 return;
5198 while (1) {
5199 page = find_lock_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005200 inode->i_size >> PAGE_SHIFT);
Jan Kara53e87262012-12-25 13:29:52 -05005201 if (!page)
5202 return;
Lukas Czernerca99fdd2013-05-21 23:25:01 -04005203 ret = __ext4_journalled_invalidatepage(page, offset,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005204 PAGE_SIZE - offset);
Jan Kara53e87262012-12-25 13:29:52 -05005205 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005206 put_page(page);
Jan Kara53e87262012-12-25 13:29:52 -05005207 if (ret != -EBUSY)
5208 return;
5209 commit_tid = 0;
5210 read_lock(&journal->j_state_lock);
5211 if (journal->j_committing_transaction)
5212 commit_tid = journal->j_committing_transaction->t_tid;
5213 read_unlock(&journal->j_state_lock);
5214 if (commit_tid)
5215 jbd2_log_wait_commit(journal, commit_tid);
5216 }
5217}
5218
5219/*
Mingming Cao617ba132006-10-11 01:20:53 -07005220 * ext4_setattr()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005221 *
5222 * Called from notify_change.
5223 *
5224 * We want to trap VFS attempts to truncate the file as soon as
5225 * possible. In particular, we want to make sure that when the VFS
5226 * shrinks i_size, we put the inode on the orphan list and modify
5227 * i_disksize immediately, so that during the subsequent flushing of
5228 * dirty pages and freeing of disk blocks, we can guarantee that any
5229 * commit will leave the blocks being flushed in an unused state on
5230 * disk. (On recovery, the inode will get truncated and the blocks will
5231 * be freed, so we have a strong guarantee that no future commit will
5232 * leave these blocks visible to the user.)
5233 *
Jan Kara678aaf42008-07-11 19:27:31 -04005234 * Another thing we have to assure is that if we are in ordered mode
5235 * and inode is still attached to the committing transaction, we must
5236 * we start writeout of all the dirty pages which are being truncated.
5237 * This way we are sure that all the data written in the previous
5238 * transaction are already on disk (truncate waits for pages under
5239 * writeback).
5240 *
5241 * Called with inode->i_mutex down.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005242 */
Mingming Cao617ba132006-10-11 01:20:53 -07005243int ext4_setattr(struct dentry *dentry, struct iattr *attr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005244{
David Howells2b0143b2015-03-17 22:25:59 +00005245 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005246 int error, rc = 0;
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04005247 int orphan = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005248 const unsigned int ia_valid = attr->ia_valid;
5249
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05005250 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5251 return -EIO;
5252
Theodore Ts'o02b016c2019-06-09 22:04:33 -04005253 if (unlikely(IS_IMMUTABLE(inode)))
5254 return -EPERM;
5255
5256 if (unlikely(IS_APPEND(inode) &&
5257 (ia_valid & (ATTR_MODE | ATTR_UID |
5258 ATTR_GID | ATTR_TIMES_SET))))
5259 return -EPERM;
5260
Jan Kara31051c82016-05-26 16:55:18 +02005261 error = setattr_prepare(dentry, attr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005262 if (error)
5263 return error;
5264
Eric Biggers3ce2b8d2017-10-18 20:21:58 -04005265 error = fscrypt_prepare_setattr(dentry, attr);
5266 if (error)
5267 return error;
5268
Eric Biggersc93d8f82019-07-22 09:26:24 -07005269 error = fsverity_prepare_setattr(dentry, attr);
5270 if (error)
5271 return error;
5272
Jan Karaa7cdade2015-06-29 16:22:54 +02005273 if (is_quota_modification(inode, attr)) {
5274 error = dquot_initialize(inode);
5275 if (error)
5276 return error;
5277 }
Eric W. Biederman08cefc72012-02-07 15:41:49 -08005278 if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
5279 (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005280 handle_t *handle;
5281
5282 /* (user+group)*(old+new) structure, inode write (sb,
5283 * inode block, ? - but truncate inode update has it) */
Theodore Ts'o9924a922013-02-08 21:59:22 -05005284 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
5285 (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) +
5286 EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005287 if (IS_ERR(handle)) {
5288 error = PTR_ERR(handle);
5289 goto err_out;
5290 }
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -04005291
5292 /* dquot_transfer() calls back ext4_get_inode_usage() which
5293 * counts xattr inode references.
5294 */
5295 down_read(&EXT4_I(inode)->xattr_sem);
Christoph Hellwigb43fa822010-03-03 09:05:03 -05005296 error = dquot_transfer(inode, attr);
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -04005297 up_read(&EXT4_I(inode)->xattr_sem);
5298
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005299 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07005300 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005301 return error;
5302 }
5303 /* Update corresponding info in inode so that everything is in
5304 * one transaction */
5305 if (attr->ia_valid & ATTR_UID)
5306 inode->i_uid = attr->ia_uid;
5307 if (attr->ia_valid & ATTR_GID)
5308 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07005309 error = ext4_mark_inode_dirty(handle, inode);
5310 ext4_journal_stop(handle);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07005311 if (unlikely(error))
5312 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005313 }
5314
Josef Bacik3da40c72015-06-22 00:31:26 -04005315 if (attr->ia_valid & ATTR_SIZE) {
Jan Kara52083862013-08-17 10:07:17 -04005316 handle_t *handle;
Josef Bacik3da40c72015-06-22 00:31:26 -04005317 loff_t oldsize = inode->i_size;
Jan Karab9c1c262019-05-30 11:56:23 -04005318 int shrink = (attr->ia_size < inode->i_size);
Christoph Hellwig562c72aa52011-06-24 14:29:45 -04005319
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005320 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
Eric Sandeene2b46572008-01-28 23:58:27 -05005321 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5322
Theodore Ts'o0c095c72010-07-27 11:56:06 -04005323 if (attr->ia_size > sbi->s_bitmap_maxbytes)
5324 return -EFBIG;
Eric Sandeene2b46572008-01-28 23:58:27 -05005325 }
Josef Bacik3da40c72015-06-22 00:31:26 -04005326 if (!S_ISREG(inode->i_mode))
5327 return -EINVAL;
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005328
5329 if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size)
5330 inode_inc_iversion(inode);
5331
Jan Karab9c1c262019-05-30 11:56:23 -04005332 if (shrink) {
5333 if (ext4_should_order_data(inode)) {
5334 error = ext4_begin_ordered_truncate(inode,
Jan Kara678aaf42008-07-11 19:27:31 -04005335 attr->ia_size);
Jan Karab9c1c262019-05-30 11:56:23 -04005336 if (error)
5337 goto err_out;
5338 }
5339 /*
5340 * Blocks are going to be removed from the inode. Wait
5341 * for dio in flight.
5342 */
5343 inode_dio_wait(inode);
Josef Bacik3da40c72015-06-22 00:31:26 -04005344 }
Jan Karab9c1c262019-05-30 11:56:23 -04005345
5346 down_write(&EXT4_I(inode)->i_mmap_sem);
5347
5348 rc = ext4_break_layouts(inode);
5349 if (rc) {
5350 up_write(&EXT4_I(inode)->i_mmap_sem);
5351 return rc;
5352 }
5353
Josef Bacik3da40c72015-06-22 00:31:26 -04005354 if (attr->ia_size != inode->i_size) {
Jan Kara52083862013-08-17 10:07:17 -04005355 handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
5356 if (IS_ERR(handle)) {
5357 error = PTR_ERR(handle);
Jan Karab9c1c262019-05-30 11:56:23 -04005358 goto out_mmap_sem;
Jan Kara52083862013-08-17 10:07:17 -04005359 }
Josef Bacik3da40c72015-06-22 00:31:26 -04005360 if (ext4_handle_valid(handle) && shrink) {
Jan Kara52083862013-08-17 10:07:17 -04005361 error = ext4_orphan_add(handle, inode);
5362 orphan = 1;
5363 }
Eryu Guan911af572015-07-28 15:08:41 -04005364 /*
5365 * Update c/mtime on truncate up, ext4_truncate() will
5366 * update c/mtime in shrink case below
5367 */
5368 if (!shrink) {
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005369 inode->i_mtime = current_time(inode);
Eryu Guan911af572015-07-28 15:08:41 -04005370 inode->i_ctime = inode->i_mtime;
5371 }
Jan Kara90e775b2013-08-17 10:09:31 -04005372 down_write(&EXT4_I(inode)->i_data_sem);
Jan Kara52083862013-08-17 10:07:17 -04005373 EXT4_I(inode)->i_disksize = attr->ia_size;
5374 rc = ext4_mark_inode_dirty(handle, inode);
5375 if (!error)
5376 error = rc;
Jan Kara90e775b2013-08-17 10:09:31 -04005377 /*
5378 * We have to update i_size under i_data_sem together
5379 * with i_disksize to avoid races with writeback code
5380 * running ext4_wb_update_i_disksize().
5381 */
5382 if (!error)
5383 i_size_write(inode, attr->ia_size);
5384 up_write(&EXT4_I(inode)->i_data_sem);
Jan Kara52083862013-08-17 10:07:17 -04005385 ext4_journal_stop(handle);
Jan Karab9c1c262019-05-30 11:56:23 -04005386 if (error)
5387 goto out_mmap_sem;
5388 if (!shrink) {
5389 pagecache_isize_extended(inode, oldsize,
5390 inode->i_size);
5391 } else if (ext4_should_journal_data(inode)) {
5392 ext4_wait_for_tail_page_commit(inode);
Jan Kara678aaf42008-07-11 19:27:31 -04005393 }
Jan Karad6320cb2014-10-01 21:49:46 -04005394 }
Ross Zwisler430657b2018-07-29 17:00:22 -04005395
Jan Kara52083862013-08-17 10:07:17 -04005396 /*
5397 * Truncate pagecache after we've waited for commit
5398 * in data=journal mode to make pages freeable.
5399 */
Ross Zwisler923ae0f2015-02-16 15:59:38 -08005400 truncate_pagecache(inode, inode->i_size);
Jan Karab9c1c262019-05-30 11:56:23 -04005401 /*
5402 * Call ext4_truncate() even if i_size didn't change to
5403 * truncate possible preallocated blocks.
5404 */
5405 if (attr->ia_size <= oldsize) {
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05005406 rc = ext4_truncate(inode);
5407 if (rc)
5408 error = rc;
5409 }
Jan Karab9c1c262019-05-30 11:56:23 -04005410out_mmap_sem:
Jan Karaea3d7202015-12-07 14:28:03 -05005411 up_write(&EXT4_I(inode)->i_mmap_sem);
Theodore Ts'o072bd7e2011-05-23 15:13:02 -04005412 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005413
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05005414 if (!error) {
Christoph Hellwig10257742010-06-04 11:30:02 +02005415 setattr_copy(inode, attr);
5416 mark_inode_dirty(inode);
5417 }
5418
5419 /*
5420 * If the call to ext4_truncate failed to get a transaction handle at
5421 * all, we need to clean up the in-core orphan list manually.
5422 */
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04005423 if (orphan && inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07005424 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005425
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05005426 if (!error && (ia_valid & ATTR_MODE))
Christoph Hellwig64e178a2013-12-20 05:16:44 -08005427 rc = posix_acl_chmod(inode, inode->i_mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005428
5429err_out:
Mingming Cao617ba132006-10-11 01:20:53 -07005430 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005431 if (!error)
5432 error = rc;
5433 return error;
5434}
5435
David Howellsa528d352017-01-31 16:46:22 +00005436int ext4_getattr(const struct path *path, struct kstat *stat,
5437 u32 request_mask, unsigned int query_flags)
Mingming Cao3e3398a2008-07-11 19:27:31 -04005438{
David Howells99652ea2017-03-31 18:31:56 +01005439 struct inode *inode = d_inode(path->dentry);
5440 struct ext4_inode *raw_inode;
5441 struct ext4_inode_info *ei = EXT4_I(inode);
5442 unsigned int flags;
Mingming Cao3e3398a2008-07-11 19:27:31 -04005443
Theodore Ts'od4c5e962019-11-28 22:26:51 -05005444 if ((request_mask & STATX_BTIME) &&
5445 EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime)) {
David Howells99652ea2017-03-31 18:31:56 +01005446 stat->result_mask |= STATX_BTIME;
5447 stat->btime.tv_sec = ei->i_crtime.tv_sec;
5448 stat->btime.tv_nsec = ei->i_crtime.tv_nsec;
5449 }
5450
5451 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
5452 if (flags & EXT4_APPEND_FL)
5453 stat->attributes |= STATX_ATTR_APPEND;
5454 if (flags & EXT4_COMPR_FL)
5455 stat->attributes |= STATX_ATTR_COMPRESSED;
5456 if (flags & EXT4_ENCRYPT_FL)
5457 stat->attributes |= STATX_ATTR_ENCRYPTED;
5458 if (flags & EXT4_IMMUTABLE_FL)
5459 stat->attributes |= STATX_ATTR_IMMUTABLE;
5460 if (flags & EXT4_NODUMP_FL)
5461 stat->attributes |= STATX_ATTR_NODUMP;
Eric Biggers1f607192019-10-29 13:41:39 -07005462 if (flags & EXT4_VERITY_FL)
5463 stat->attributes |= STATX_ATTR_VERITY;
David Howells99652ea2017-03-31 18:31:56 +01005464
David Howells3209f682017-03-31 18:32:17 +01005465 stat->attributes_mask |= (STATX_ATTR_APPEND |
5466 STATX_ATTR_COMPRESSED |
5467 STATX_ATTR_ENCRYPTED |
5468 STATX_ATTR_IMMUTABLE |
Eric Biggers1f607192019-10-29 13:41:39 -07005469 STATX_ATTR_NODUMP |
5470 STATX_ATTR_VERITY);
David Howells3209f682017-03-31 18:32:17 +01005471
Mingming Cao3e3398a2008-07-11 19:27:31 -04005472 generic_fillattr(inode, stat);
David Howells99652ea2017-03-31 18:31:56 +01005473 return 0;
5474}
5475
5476int ext4_file_getattr(const struct path *path, struct kstat *stat,
5477 u32 request_mask, unsigned int query_flags)
5478{
5479 struct inode *inode = d_inode(path->dentry);
5480 u64 delalloc_blocks;
5481
5482 ext4_getattr(path, stat, request_mask, query_flags);
Mingming Cao3e3398a2008-07-11 19:27:31 -04005483
5484 /*
Andreas Dilger9206c562013-11-11 22:38:12 -05005485 * If there is inline data in the inode, the inode will normally not
5486 * have data blocks allocated (it may have an external xattr block).
5487 * Report at least one sector for such files, so tools like tar, rsync,
Theodore Ts'od67d64f2017-03-25 17:33:31 -04005488 * others don't incorrectly think the file is completely sparse.
Andreas Dilger9206c562013-11-11 22:38:12 -05005489 */
5490 if (unlikely(ext4_has_inline_data(inode)))
5491 stat->blocks += (stat->size + 511) >> 9;
5492
5493 /*
Mingming Cao3e3398a2008-07-11 19:27:31 -04005494 * We can't update i_blocks if the block allocation is delayed
5495 * otherwise in the case of system crash before the real block
5496 * allocation is done, we will have i_blocks inconsistent with
5497 * on-disk file blocks.
5498 * We always keep i_blocks updated together with real
5499 * allocation. But to not confuse with user, stat
5500 * will return the blocks that include the delayed allocation
5501 * blocks for this file.
5502 */
Tao Ma96607552012-05-31 22:54:16 -04005503 delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
Andreas Dilger9206c562013-11-11 22:38:12 -05005504 EXT4_I(inode)->i_reserved_data_blocks);
5505 stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9);
Mingming Cao3e3398a2008-07-11 19:27:31 -04005506 return 0;
5507}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005508
Jan Karafffb2732013-06-04 13:01:11 -04005509static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
5510 int pextents)
Mingming Caoa02908f2008-08-19 22:16:07 -04005511{
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005512 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Jan Karafffb2732013-06-04 13:01:11 -04005513 return ext4_ind_trans_blocks(inode, lblocks);
5514 return ext4_ext_index_trans_blocks(inode, pextents);
Mingming Caoa02908f2008-08-19 22:16:07 -04005515}
Theodore Ts'oac51d832008-11-06 16:49:36 -05005516
Mingming Caoa02908f2008-08-19 22:16:07 -04005517/*
5518 * Account for index blocks, block groups bitmaps and block group
5519 * descriptor blocks if modify datablocks and index blocks
5520 * worse case, the indexs blocks spread over different block groups
5521 *
5522 * If datablocks are discontiguous, they are possible to spread over
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005523 * different block groups too. If they are contiguous, with flexbg,
Mingming Caoa02908f2008-08-19 22:16:07 -04005524 * they could still across block group boundary.
5525 *
5526 * Also account for superblock, inode, quota and xattr blocks
5527 */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04005528static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
Jan Karafffb2732013-06-04 13:01:11 -04005529 int pextents)
Mingming Caoa02908f2008-08-19 22:16:07 -04005530{
Theodore Ts'o8df96752009-05-01 08:50:38 -04005531 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
5532 int gdpblocks;
Mingming Caoa02908f2008-08-19 22:16:07 -04005533 int idxblocks;
5534 int ret = 0;
5535
5536 /*
Jan Karafffb2732013-06-04 13:01:11 -04005537 * How many index blocks need to touch to map @lblocks logical blocks
5538 * to @pextents physical extents?
Mingming Caoa02908f2008-08-19 22:16:07 -04005539 */
Jan Karafffb2732013-06-04 13:01:11 -04005540 idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents);
Mingming Caoa02908f2008-08-19 22:16:07 -04005541
5542 ret = idxblocks;
5543
5544 /*
5545 * Now let's see how many group bitmaps and group descriptors need
5546 * to account
5547 */
Jan Karafffb2732013-06-04 13:01:11 -04005548 groups = idxblocks + pextents;
Mingming Caoa02908f2008-08-19 22:16:07 -04005549 gdpblocks = groups;
Theodore Ts'o8df96752009-05-01 08:50:38 -04005550 if (groups > ngroups)
5551 groups = ngroups;
Mingming Caoa02908f2008-08-19 22:16:07 -04005552 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
5553 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
5554
5555 /* bitmaps and block group descriptor blocks */
5556 ret += groups + gdpblocks;
5557
5558 /* Blocks for super block, inode, quota and xattr blocks */
5559 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005560
5561 return ret;
5562}
5563
5564/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005565 * Calculate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04005566 * the modification of a single pages into a single transaction,
5567 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04005568 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04005569 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04005570 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04005571 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04005572 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04005573 */
5574int ext4_writepage_trans_blocks(struct inode *inode)
5575{
5576 int bpp = ext4_journal_blocks_per_page(inode);
5577 int ret;
5578
Jan Karafffb2732013-06-04 13:01:11 -04005579 ret = ext4_meta_trans_blocks(inode, bpp, bpp);
Mingming Caoa02908f2008-08-19 22:16:07 -04005580
5581 /* Account for data blocks for journalled mode */
5582 if (ext4_should_journal_data(inode))
5583 ret += bpp;
5584 return ret;
5585}
Mingming Caof3bd1f32008-08-19 22:16:03 -04005586
5587/*
5588 * Calculate the journal credits for a chunk of data modification.
5589 *
5590 * This is called from DIO, fallocate or whoever calling
Eric Sandeen79e83032010-07-27 11:56:07 -04005591 * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
Mingming Caof3bd1f32008-08-19 22:16:03 -04005592 *
5593 * journal buffers for data blocks are not included here, as DIO
5594 * and fallocate do no need to journal data buffers.
5595 */
5596int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
5597{
5598 return ext4_meta_trans_blocks(inode, nrblocks, 1);
5599}
5600
Mingming Caoa02908f2008-08-19 22:16:07 -04005601/*
Mingming Cao617ba132006-10-11 01:20:53 -07005602 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005603 * Give this, we know that the caller already has write access to iloc->bh.
5604 */
Mingming Cao617ba132006-10-11 01:20:53 -07005605int ext4_mark_iloc_dirty(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04005606 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005607{
5608 int err = 0;
5609
Vasily Averina6758302018-11-06 16:49:50 -05005610 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) {
5611 put_bh(iloc->bh);
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05005612 return -EIO;
Vasily Averina6758302018-11-06 16:49:50 -05005613 }
Theodore Ts'oc64db502012-03-02 12:23:11 -05005614 if (IS_I_VERSION(inode))
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005615 inode_inc_iversion(inode);
5616
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005617 /* the do_update_inode consumes one bh->b_count */
5618 get_bh(iloc->bh);
5619
Mingming Caodab291a2006-10-11 01:21:01 -07005620 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Frank Mayhar830156c2009-09-29 10:07:47 -04005621 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005622 put_bh(iloc->bh);
5623 return err;
5624}
5625
5626/*
5627 * On success, We end up with an outstanding reference count against
5628 * iloc->bh. This _must_ be cleaned up later.
5629 */
5630
5631int
Mingming Cao617ba132006-10-11 01:20:53 -07005632ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
5633 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005634{
Frank Mayhar03901312009-01-07 00:06:22 -05005635 int err;
5636
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05005637 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5638 return -EIO;
5639
Frank Mayhar03901312009-01-07 00:06:22 -05005640 err = ext4_get_inode_loc(inode, iloc);
5641 if (!err) {
5642 BUFFER_TRACE(iloc->bh, "get_write_access");
5643 err = ext4_journal_get_write_access(handle, iloc->bh);
5644 if (err) {
5645 brelse(iloc->bh);
5646 iloc->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005647 }
5648 }
Mingming Cao617ba132006-10-11 01:20:53 -07005649 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005650 return err;
5651}
5652
Miao Xiec03b45b2017-08-06 01:00:49 -04005653static int __ext4_expand_extra_isize(struct inode *inode,
5654 unsigned int new_extra_isize,
5655 struct ext4_iloc *iloc,
5656 handle_t *handle, int *no_expand)
5657{
5658 struct ext4_inode *raw_inode;
5659 struct ext4_xattr_ibody_header *header;
Theodore Ts'o4ea99932019-11-07 21:43:41 -05005660 unsigned int inode_size = EXT4_INODE_SIZE(inode->i_sb);
5661 struct ext4_inode_info *ei = EXT4_I(inode);
Miao Xiec03b45b2017-08-06 01:00:49 -04005662 int error;
5663
Theodore Ts'o4ea99932019-11-07 21:43:41 -05005664 /* this was checked at iget time, but double check for good measure */
5665 if ((EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > inode_size) ||
5666 (ei->i_extra_isize & 3)) {
5667 EXT4_ERROR_INODE(inode, "bad extra_isize %u (inode size %u)",
5668 ei->i_extra_isize,
5669 EXT4_INODE_SIZE(inode->i_sb));
5670 return -EFSCORRUPTED;
5671 }
5672 if ((new_extra_isize < ei->i_extra_isize) ||
5673 (new_extra_isize < 4) ||
5674 (new_extra_isize > inode_size - EXT4_GOOD_OLD_INODE_SIZE))
5675 return -EINVAL; /* Should never happen */
5676
Miao Xiec03b45b2017-08-06 01:00:49 -04005677 raw_inode = ext4_raw_inode(iloc);
5678
5679 header = IHDR(inode, raw_inode);
5680
5681 /* No extended attributes present */
5682 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
5683 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
5684 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE +
5685 EXT4_I(inode)->i_extra_isize, 0,
5686 new_extra_isize - EXT4_I(inode)->i_extra_isize);
5687 EXT4_I(inode)->i_extra_isize = new_extra_isize;
5688 return 0;
5689 }
5690
5691 /* try to expand with EAs present */
5692 error = ext4_expand_extra_isize_ea(inode, new_extra_isize,
5693 raw_inode, handle);
5694 if (error) {
5695 /*
5696 * Inode size expansion failed; don't try again
5697 */
5698 *no_expand = 1;
5699 }
5700
5701 return error;
5702}
5703
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005704/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005705 * Expand an inode by new_extra_isize bytes.
5706 * Returns 0 on success or negative error number on failure.
5707 */
Miao Xiecf0a5e82017-08-06 00:40:01 -04005708static int ext4_try_to_expand_extra_isize(struct inode *inode,
5709 unsigned int new_extra_isize,
5710 struct ext4_iloc iloc,
5711 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005712{
Miao Xie3b10fdc2017-08-06 00:27:38 -04005713 int no_expand;
5714 int error;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005715
Miao Xiecf0a5e82017-08-06 00:40:01 -04005716 if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND))
5717 return -EOVERFLOW;
5718
5719 /*
5720 * In nojournal mode, we can immediately attempt to expand
5721 * the inode. When journaled, we first need to obtain extra
5722 * buffer credits since we may write into the EA block
5723 * with this same handle. If journal_extend fails, then it will
5724 * only result in a minor loss of functionality for that inode.
5725 * If this is felt to be critical, then e2fsck should be run to
5726 * force a large enough s_min_extra_isize.
5727 */
Jan Kara6cb367c2019-11-05 17:44:14 +01005728 if (ext4_journal_extend(handle,
Jan Kara83448bd2019-11-05 17:44:29 +01005729 EXT4_DATA_TRANS_BLOCKS(inode->i_sb), 0) != 0)
Miao Xiecf0a5e82017-08-06 00:40:01 -04005730 return -ENOSPC;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005731
Miao Xie3b10fdc2017-08-06 00:27:38 -04005732 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
Miao Xiecf0a5e82017-08-06 00:40:01 -04005733 return -EBUSY;
Miao Xie3b10fdc2017-08-06 00:27:38 -04005734
Miao Xiec03b45b2017-08-06 01:00:49 -04005735 error = __ext4_expand_extra_isize(inode, new_extra_isize, &iloc,
5736 handle, &no_expand);
Miao Xie3b10fdc2017-08-06 00:27:38 -04005737 ext4_write_unlock_xattr(inode, &no_expand);
Miao Xiecf0a5e82017-08-06 00:40:01 -04005738
Miao Xie3b10fdc2017-08-06 00:27:38 -04005739 return error;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005740}
5741
Miao Xiec03b45b2017-08-06 01:00:49 -04005742int ext4_expand_extra_isize(struct inode *inode,
5743 unsigned int new_extra_isize,
5744 struct ext4_iloc *iloc)
5745{
5746 handle_t *handle;
5747 int no_expand;
5748 int error, rc;
5749
5750 if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
5751 brelse(iloc->bh);
5752 return -EOVERFLOW;
5753 }
5754
5755 handle = ext4_journal_start(inode, EXT4_HT_INODE,
5756 EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
5757 if (IS_ERR(handle)) {
5758 error = PTR_ERR(handle);
5759 brelse(iloc->bh);
5760 return error;
5761 }
5762
5763 ext4_write_lock_xattr(inode, &no_expand);
5764
zhangyi (F)ddccb6d2019-02-21 11:29:10 -05005765 BUFFER_TRACE(iloc->bh, "get_write_access");
Miao Xiec03b45b2017-08-06 01:00:49 -04005766 error = ext4_journal_get_write_access(handle, iloc->bh);
5767 if (error) {
5768 brelse(iloc->bh);
Dan Carpenter7f420d642019-12-13 21:50:11 +03005769 goto out_unlock;
Miao Xiec03b45b2017-08-06 01:00:49 -04005770 }
5771
5772 error = __ext4_expand_extra_isize(inode, new_extra_isize, iloc,
5773 handle, &no_expand);
5774
5775 rc = ext4_mark_iloc_dirty(handle, inode, iloc);
5776 if (!error)
5777 error = rc;
5778
Dan Carpenter7f420d642019-12-13 21:50:11 +03005779out_unlock:
Miao Xiec03b45b2017-08-06 01:00:49 -04005780 ext4_write_unlock_xattr(inode, &no_expand);
Miao Xiec03b45b2017-08-06 01:00:49 -04005781 ext4_journal_stop(handle);
5782 return error;
5783}
5784
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005785/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005786 * What we do here is to mark the in-core inode as clean with respect to inode
5787 * dirtiness (it may still be data-dirty).
5788 * This means that the in-core inode may be reaped by prune_icache
5789 * without having to perform any I/O. This is a very good thing,
5790 * because *any* task may call prune_icache - even ones which
5791 * have a transaction open against a different journal.
5792 *
5793 * Is this cheating? Not really. Sure, we haven't written the
5794 * inode out, but prune_icache isn't a user-visible syncing function.
5795 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5796 * we start and wait on commits.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005797 */
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07005798int __ext4_mark_inode_dirty(handle_t *handle, struct inode *inode,
5799 const char *func, unsigned int line)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005800{
Mingming Cao617ba132006-10-11 01:20:53 -07005801 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005802 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Miao Xiecf0a5e82017-08-06 00:40:01 -04005803 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005804
5805 might_sleep();
Theodore Ts'o7ff9c072010-11-08 13:51:33 -05005806 trace_ext4_mark_inode_dirty(inode, _RET_IP_);
Mingming Cao617ba132006-10-11 01:20:53 -07005807 err = ext4_reserve_inode_write(handle, inode, &iloc);
Eryu Guan5e1021f2016-03-12 21:40:32 -05005808 if (err)
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07005809 goto out;
Miao Xiecf0a5e82017-08-06 00:40:01 -04005810
5811 if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize)
5812 ext4_try_to_expand_extra_isize(inode, sbi->s_want_extra_isize,
5813 iloc, handle);
5814
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07005815 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
5816out:
5817 if (unlikely(err))
5818 ext4_error_inode_err(inode, func, line, 0, err,
5819 "mark_inode_dirty error");
5820 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005821}
5822
5823/*
Mingming Cao617ba132006-10-11 01:20:53 -07005824 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005825 *
5826 * We're really interested in the case where a file is being extended.
5827 * i_size has been changed by generic_commit_write() and we thus need
5828 * to include the updated inode in the current transaction.
5829 *
Christoph Hellwig5dd40562010-03-03 09:05:00 -05005830 * Also, dquot_alloc_block() will always dirty the inode when blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005831 * are allocated to the file.
5832 *
5833 * If the inode is marked synchronous, we don't honour that here - doing
5834 * so would cause a commit on atime updates, which we don't bother doing.
5835 * We handle synchronous inodes at the highest possible level.
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05005836 *
5837 * If only the I_DIRTY_TIME flag is set, we can skip everything. If
5838 * I_DIRTY_TIME and I_DIRTY_SYNC is set, the only inode fields we need
5839 * to copy into the on-disk inode structure are the timestamp files.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005840 */
Christoph Hellwigaa385722011-05-27 06:53:02 -04005841void ext4_dirty_inode(struct inode *inode, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005842{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005843 handle_t *handle;
5844
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05005845 if (flags == I_DIRTY_TIME)
5846 return;
Theodore Ts'o9924a922013-02-08 21:59:22 -05005847 handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005848 if (IS_ERR(handle))
5849 goto out;
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005850
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005851 ext4_mark_inode_dirty(handle, inode);
5852
Mingming Cao617ba132006-10-11 01:20:53 -07005853 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005854out:
5855 return;
5856}
5857
Mingming Cao617ba132006-10-11 01:20:53 -07005858int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005859{
5860 journal_t *journal;
5861 handle_t *handle;
5862 int err;
Daeho Jeongc8585c62016-04-25 23:22:35 -04005863 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005864
5865 /*
5866 * We have to be very careful here: changing a data block's
5867 * journaling status dynamically is dangerous. If we write a
5868 * data block to the journal, change the status and then delete
5869 * that block, we risk forgetting to revoke the old log record
5870 * from the journal and so a subsequent replay can corrupt data.
5871 * So, first we make sure that the journal is empty and that
5872 * nobody is changing anything.
5873 */
5874
Mingming Cao617ba132006-10-11 01:20:53 -07005875 journal = EXT4_JOURNAL(inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005876 if (!journal)
5877 return 0;
Dave Hansend6995942007-07-18 08:33:51 -04005878 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005879 return -EROFS;
5880
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04005881 /* Wait for all existing dio workers */
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04005882 inode_dio_wait(inode);
5883
Daeho Jeong4c546592016-04-25 23:21:00 -04005884 /*
5885 * Before flushing the journal and switching inode's aops, we have
5886 * to flush all dirty data the inode has. There can be outstanding
5887 * delayed allocations, there can be unwritten extents created by
5888 * fallocate or buffered writes in dioread_nolock mode covered by
5889 * dirty data which can be converted only after flushing the dirty
5890 * data (and journalled aops don't know how to handle these cases).
5891 */
5892 if (val) {
5893 down_write(&EXT4_I(inode)->i_mmap_sem);
5894 err = filemap_write_and_wait(inode->i_mapping);
5895 if (err < 0) {
5896 up_write(&EXT4_I(inode)->i_mmap_sem);
Daeho Jeong4c546592016-04-25 23:21:00 -04005897 return err;
5898 }
5899 }
5900
Eric Biggersbbd55932020-02-19 10:30:46 -08005901 percpu_down_write(&sbi->s_writepages_rwsem);
Mingming Caodab291a2006-10-11 01:21:01 -07005902 jbd2_journal_lock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005903
5904 /*
5905 * OK, there are no updates running now, and all cached data is
5906 * synced to disk. We are now in a completely consistent state
5907 * which doesn't have anything in the journal, and we know that
5908 * no filesystem updates are running, so it is safe to modify
5909 * the inode's in-core data-journaling state flag now.
5910 */
5911
5912 if (val)
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005913 ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Yongqiang Yang5872dda2011-12-28 13:55:51 -05005914 else {
Jan Kara4f879ca2014-10-30 10:53:17 -04005915 err = jbd2_journal_flush(journal);
5916 if (err < 0) {
5917 jbd2_journal_unlock_updates(journal);
Eric Biggersbbd55932020-02-19 10:30:46 -08005918 percpu_up_write(&sbi->s_writepages_rwsem);
Jan Kara4f879ca2014-10-30 10:53:17 -04005919 return err;
5920 }
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005921 ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Yongqiang Yang5872dda2011-12-28 13:55:51 -05005922 }
Mingming Cao617ba132006-10-11 01:20:53 -07005923 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005924
Mingming Caodab291a2006-10-11 01:21:01 -07005925 jbd2_journal_unlock_updates(journal);
Eric Biggersbbd55932020-02-19 10:30:46 -08005926 percpu_up_write(&sbi->s_writepages_rwsem);
Daeho Jeongc8585c62016-04-25 23:22:35 -04005927
Daeho Jeong4c546592016-04-25 23:21:00 -04005928 if (val)
5929 up_write(&EXT4_I(inode)->i_mmap_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005930
5931 /* Finally we can mark the inode as dirty. */
5932
Theodore Ts'o9924a922013-02-08 21:59:22 -05005933 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005934 if (IS_ERR(handle))
5935 return PTR_ERR(handle);
5936
Mingming Cao617ba132006-10-11 01:20:53 -07005937 err = ext4_mark_inode_dirty(handle, inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005938 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005939 ext4_journal_stop(handle);
5940 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005941
5942 return err;
5943}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005944
5945static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
5946{
5947 return !buffer_mapped(bh);
5948}
5949
Souptick Joarder401b25a2018-10-02 22:20:50 -04005950vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf)
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005951{
Dave Jiang11bac802017-02-24 14:56:41 -08005952 struct vm_area_struct *vma = vmf->vma;
Nick Pigginc2ec1752009-03-31 15:23:21 -07005953 struct page *page = vmf->page;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005954 loff_t size;
5955 unsigned long len;
Souptick Joarder401b25a2018-10-02 22:20:50 -04005956 int err;
5957 vm_fault_t ret;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005958 struct file *file = vma->vm_file;
Al Viro496ad9a2013-01-23 17:07:38 -05005959 struct inode *inode = file_inode(file);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005960 struct address_space *mapping = inode->i_mapping;
Jan Kara9ea7df52011-06-24 14:29:41 -04005961 handle_t *handle;
5962 get_block_t *get_block;
5963 int retries = 0;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005964
Theodore Ts'o02b016c2019-06-09 22:04:33 -04005965 if (unlikely(IS_IMMUTABLE(inode)))
5966 return VM_FAULT_SIGBUS;
5967
Jan Kara8e8ad8a2012-06-12 16:20:38 +02005968 sb_start_pagefault(inode->i_sb);
Theodore Ts'o041bbb6d2012-09-30 23:04:56 -04005969 file_update_time(vma->vm_file);
Jan Karaea3d7202015-12-07 14:28:03 -05005970
5971 down_read(&EXT4_I(inode)->i_mmap_sem);
Eric Biggers7b4cc972017-04-30 00:10:50 -04005972
Souptick Joarder401b25a2018-10-02 22:20:50 -04005973 err = ext4_convert_inline_data(inode);
5974 if (err)
Eric Biggers7b4cc972017-04-30 00:10:50 -04005975 goto out_ret;
5976
Jan Kara9ea7df52011-06-24 14:29:41 -04005977 /* Delalloc case is easy... */
5978 if (test_opt(inode->i_sb, DELALLOC) &&
5979 !ext4_should_journal_data(inode) &&
5980 !ext4_nonda_switch(inode->i_sb)) {
5981 do {
Souptick Joarder401b25a2018-10-02 22:20:50 -04005982 err = block_page_mkwrite(vma, vmf,
Jan Kara9ea7df52011-06-24 14:29:41 -04005983 ext4_da_get_block_prep);
Souptick Joarder401b25a2018-10-02 22:20:50 -04005984 } while (err == -ENOSPC &&
Jan Kara9ea7df52011-06-24 14:29:41 -04005985 ext4_should_retry_alloc(inode->i_sb, &retries));
5986 goto out_ret;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005987 }
Darrick J. Wong0e499892011-05-18 13:55:20 -04005988
5989 lock_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04005990 size = i_size_read(inode);
5991 /* Page got truncated from under us? */
5992 if (page->mapping != mapping || page_offset(page) > size) {
5993 unlock_page(page);
5994 ret = VM_FAULT_NOPAGE;
5995 goto out;
Darrick J. Wong0e499892011-05-18 13:55:20 -04005996 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005997
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005998 if (page->index == size >> PAGE_SHIFT)
5999 len = size & ~PAGE_MASK;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04006000 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006001 len = PAGE_SIZE;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04006002 /*
Jan Kara9ea7df52011-06-24 14:29:41 -04006003 * Return if we have all the buffers mapped. This avoids the need to do
6004 * journal_start/journal_stop which can block and take a long time
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04006005 */
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04006006 if (page_has_buffers(page)) {
Tao Maf19d5872012-12-10 14:05:51 -05006007 if (!ext4_walk_page_buffers(NULL, page_buffers(page),
6008 0, len, NULL,
6009 ext4_bh_unmapped)) {
Jan Kara9ea7df52011-06-24 14:29:41 -04006010 /* Wait so that we don't change page under IO */
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08006011 wait_for_stable_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04006012 ret = VM_FAULT_LOCKED;
6013 goto out;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04006014 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04006015 }
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04006016 unlock_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04006017 /* OK, we need to fill the hole... */
6018 if (ext4_should_dioread_nolock(inode))
Jan Kara705965b2016-03-08 23:08:10 -05006019 get_block = ext4_get_block_unwritten;
Jan Kara9ea7df52011-06-24 14:29:41 -04006020 else
6021 get_block = ext4_get_block;
6022retry_alloc:
Theodore Ts'o9924a922013-02-08 21:59:22 -05006023 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
6024 ext4_writepage_trans_blocks(inode));
Jan Kara9ea7df52011-06-24 14:29:41 -04006025 if (IS_ERR(handle)) {
Nick Pigginc2ec1752009-03-31 15:23:21 -07006026 ret = VM_FAULT_SIGBUS;
Jan Kara9ea7df52011-06-24 14:29:41 -04006027 goto out;
6028 }
Souptick Joarder401b25a2018-10-02 22:20:50 -04006029 err = block_page_mkwrite(vma, vmf, get_block);
6030 if (!err && ext4_should_journal_data(inode)) {
Tao Maf19d5872012-12-10 14:05:51 -05006031 if (ext4_walk_page_buffers(handle, page_buffers(page), 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006032 PAGE_SIZE, NULL, do_journal_get_write_access)) {
Jan Kara9ea7df52011-06-24 14:29:41 -04006033 unlock_page(page);
6034 ret = VM_FAULT_SIGBUS;
Yongqiang Yangfcbb5512011-10-26 05:00:19 -04006035 ext4_journal_stop(handle);
Jan Kara9ea7df52011-06-24 14:29:41 -04006036 goto out;
6037 }
6038 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
6039 }
6040 ext4_journal_stop(handle);
Souptick Joarder401b25a2018-10-02 22:20:50 -04006041 if (err == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
Jan Kara9ea7df52011-06-24 14:29:41 -04006042 goto retry_alloc;
6043out_ret:
Souptick Joarder401b25a2018-10-02 22:20:50 -04006044 ret = block_page_mkwrite_return(err);
Jan Kara9ea7df52011-06-24 14:29:41 -04006045out:
Jan Karaea3d7202015-12-07 14:28:03 -05006046 up_read(&EXT4_I(inode)->i_mmap_sem);
Jan Kara8e8ad8a2012-06-12 16:20:38 +02006047 sb_end_pagefault(inode->i_sb);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04006048 return ret;
6049}
Jan Karaea3d7202015-12-07 14:28:03 -05006050
Souptick Joarder401b25a2018-10-02 22:20:50 -04006051vm_fault_t ext4_filemap_fault(struct vm_fault *vmf)
Jan Karaea3d7202015-12-07 14:28:03 -05006052{
Dave Jiang11bac802017-02-24 14:56:41 -08006053 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarder401b25a2018-10-02 22:20:50 -04006054 vm_fault_t ret;
Jan Karaea3d7202015-12-07 14:28:03 -05006055
6056 down_read(&EXT4_I(inode)->i_mmap_sem);
Souptick Joarder401b25a2018-10-02 22:20:50 -04006057 ret = filemap_fault(vmf);
Jan Karaea3d7202015-12-07 14:28:03 -05006058 up_read(&EXT4_I(inode)->i_mmap_sem);
6059
Souptick Joarder401b25a2018-10-02 22:20:50 -04006060 return ret;
Jan Karaea3d7202015-12-07 14:28:03 -05006061}