blob: 6532870f6a0bc3b12e25a86b2e9414ba93c68da3 [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 /*
224 * Protect us against freezing - iput() caller didn't have to have any
225 * protection against it
226 */
227 sb_start_intwrite(inode->i_sb);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400228
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400229 if (!IS_NOQUOTA(inode))
230 extra_credits += EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb);
231
Jan Kara65db8692019-11-05 17:44:12 +0100232 /*
233 * Block bitmap, group descriptor, and inode are accounted in both
234 * ext4_blocks_for_truncate() and extra_credits. So subtract 3.
235 */
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400236 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
Jan Kara65db8692019-11-05 17:44:12 +0100237 ext4_blocks_for_truncate(inode) + extra_credits - 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700238 if (IS_ERR(handle)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400239 ext4_std_error(inode->i_sb, PTR_ERR(handle));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700240 /*
241 * If we're going to skip the normal cleanup, we still need to
242 * make sure that the in-core orphan linked list is properly
243 * cleaned up.
244 */
Mingming Cao617ba132006-10-11 01:20:53 -0700245 ext4_orphan_del(NULL, inode);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200246 sb_end_intwrite(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700247 goto no_delete;
248 }
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400249
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700250 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500251 ext4_handle_sync(handle);
Tahsin Erdogan407cd7f2017-07-04 00:11:21 -0400252
253 /*
254 * Set inode->i_size to 0 before calling ext4_truncate(). We need
255 * special handling of symlinks here because i_size is used to
256 * determine whether ext4_inode_info->i_data contains symlink data or
257 * block mappings. Setting i_size to 0 will remove its fast symlink
258 * status. Erase i_data so that it becomes a valid empty block map.
259 */
260 if (ext4_inode_is_fast_symlink(inode))
261 memset(EXT4_I(inode)->i_data, 0, sizeof(EXT4_I(inode)->i_data));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700262 inode->i_size = 0;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400263 err = ext4_mark_inode_dirty(handle, inode);
264 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500265 ext4_warning(inode->i_sb,
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400266 "couldn't mark inode dirty (err %d)", err);
267 goto stop_handle;
268 }
Theodore Ts'o2c98eb52016-11-13 22:02:26 -0500269 if (inode->i_blocks) {
270 err = ext4_truncate(inode);
271 if (err) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400272 ext4_error_err(inode->i_sb, -err,
273 "couldn't truncate inode %lu (err %d)",
274 inode->i_ino, err);
Theodore Ts'o2c98eb52016-11-13 22:02:26 -0500275 goto stop_handle;
276 }
277 }
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400278
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400279 /* Remove xattr references. */
280 err = ext4_xattr_delete_inode(handle, inode, &ea_inode_array,
281 extra_credits);
282 if (err) {
283 ext4_warning(inode->i_sb, "xattr delete (err %d)", err);
284stop_handle:
285 ext4_journal_stop(handle);
286 ext4_orphan_del(NULL, inode);
287 sb_end_intwrite(inode->i_sb);
288 ext4_xattr_inode_array_free(ea_inode_array);
289 goto no_delete;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400290 }
291
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700292 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700293 * Kill off the orphan record which ext4_truncate created.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700294 * AKPM: I think this can be inside the above `if'.
Mingming Cao617ba132006-10-11 01:20:53 -0700295 * Note that ext4_orphan_del() has to be able to cope with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700296 * deletion of a non-existent orphan - this is because we don't
Mingming Cao617ba132006-10-11 01:20:53 -0700297 * know if ext4_truncate() actually created an orphan record.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700298 * (Well, we could do this if we need to, but heck - it works)
299 */
Mingming Cao617ba132006-10-11 01:20:53 -0700300 ext4_orphan_del(handle, inode);
Arnd Bergmann5ffff832018-07-29 15:50:00 -0400301 EXT4_I(inode)->i_dtime = (__u32)ktime_get_real_seconds();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700302
303 /*
304 * One subtle ordering requirement: if anything has gone wrong
305 * (transaction abort, IO errors, whatever), then we can still
306 * do these next steps (the fs will already have been marked as
307 * having errors), but we can't free the inode if the mark_dirty
308 * fails.
309 */
Mingming Cao617ba132006-10-11 01:20:53 -0700310 if (ext4_mark_inode_dirty(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700311 /* If that failed, just do the required in-core inode clear. */
Al Viro0930fcc2010-06-07 13:16:22 -0400312 ext4_clear_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700313 else
Mingming Cao617ba132006-10-11 01:20:53 -0700314 ext4_free_inode(handle, inode);
315 ext4_journal_stop(handle);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200316 sb_end_intwrite(inode->i_sb);
Tahsin Erdogan0421a182017-06-22 10:26:31 -0400317 ext4_xattr_inode_array_free(ea_inode_array);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700318 return;
319no_delete:
Al Viro0930fcc2010-06-07 13:16:22 -0400320 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700321}
322
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300323#ifdef CONFIG_QUOTA
324qsize_t *ext4_get_reserved_space(struct inode *inode)
Mingming Cao60e58e02009-01-22 18:13:05 +0100325{
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300326 return &EXT4_I(inode)->i_reserved_quota;
Mingming Cao60e58e02009-01-22 18:13:05 +0100327}
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300328#endif
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500329
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400330/*
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500331 * Called with i_data_sem down, which is important since we can call
332 * ext4_discard_preallocations() from here.
333 */
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500334void ext4_da_update_reserve_space(struct inode *inode,
335 int used, int quota_claim)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400336{
337 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500338 struct ext4_inode_info *ei = EXT4_I(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400339
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500340 spin_lock(&ei->i_block_reservation_lock);
Aditya Kalid8990242011-09-09 19:18:51 -0400341 trace_ext4_da_update_reserve_space(inode, used, quota_claim);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500342 if (unlikely(used > ei->i_reserved_data_blocks)) {
Theodore Ts'o8de5c322013-02-14 15:11:41 -0500343 ext4_warning(inode->i_sb, "%s: ino %lu, used %d "
Theodore Ts'o1084f252012-03-19 23:13:43 -0400344 "with only %d reserved data blocks",
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500345 __func__, inode->i_ino, used,
346 ei->i_reserved_data_blocks);
347 WARN_ON(1);
348 used = ei->i_reserved_data_blocks;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400349 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400350
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500351 /* Update per-inode reservations */
352 ei->i_reserved_data_blocks -= used;
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -0400353 percpu_counter_sub(&sbi->s_dirtyclusters_counter, used);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500354
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400355 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +0100356
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400357 /* Update quota subsystem for data blocks */
358 if (quota_claim)
Aditya Kali7b415bf2011-09-09 19:04:51 -0400359 dquot_claim_block(inode, EXT4_C2B(sbi, used));
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400360 else {
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500361 /*
362 * We did fallocate with an offset that is already delayed
363 * allocated. So on delayed allocated writeback we should
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400364 * not re-claim the quota for fallocated blocks.
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500365 */
Aditya Kali7b415bf2011-09-09 19:04:51 -0400366 dquot_release_reservation_block(inode, EXT4_C2B(sbi, used));
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500367 }
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -0400368
369 /*
370 * If we have done all the pending block allocations and if
371 * there aren't any writers on the inode, we can discard the
372 * inode's preallocations.
373 */
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500374 if ((ei->i_reserved_data_blocks == 0) &&
Nikolay Borisov82dd1242019-02-10 23:04:16 -0500375 !inode_is_open_for_write(inode))
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -0400376 ext4_discard_preallocations(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400377}
378
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400379static int __check_block_validity(struct inode *inode, const char *func,
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400380 unsigned int line,
381 struct ext4_map_blocks *map)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400382{
Theodore Ts'o345c0db2019-04-09 23:37:08 -0400383 if (ext4_has_feature_journal(inode->i_sb) &&
384 (inode->i_ino ==
385 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum)))
386 return 0;
Theodore Ts'o24676da2010-05-16 21:00:00 -0400387 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
388 map->m_len)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400389 ext4_error_inode(inode, func, line, map->m_pblk,
Theodore Ts'obdbd6ce2018-06-15 12:27:16 -0400390 "lblock %lu mapped to illegal pblock %llu "
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400391 "(length %d)", (unsigned long) map->m_lblk,
Theodore Ts'obdbd6ce2018-06-15 12:27:16 -0400392 map->m_pblk, map->m_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400393 return -EFSCORRUPTED;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400394 }
395 return 0;
396}
397
Jan Kara53085fa2015-12-07 15:09:35 -0500398int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk,
399 ext4_lblk_t len)
400{
401 int ret;
402
Eric Biggers33b4cc22019-12-26 10:10:22 -0600403 if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode))
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400404 return fscrypt_zeroout_range(inode, lblk, pblk, len);
Jan Kara53085fa2015-12-07 15:09:35 -0500405
406 ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS);
407 if (ret > 0)
408 ret = 0;
409
410 return ret;
411}
412
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400413#define check_block_validity(inode, map) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400414 __check_block_validity((inode), __func__, __LINE__, (map))
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400415
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400416#ifdef ES_AGGRESSIVE_TEST
417static void ext4_map_blocks_es_recheck(handle_t *handle,
418 struct inode *inode,
419 struct ext4_map_blocks *es_map,
420 struct ext4_map_blocks *map,
421 int flags)
422{
423 int retval;
424
425 map->m_flags = 0;
426 /*
427 * There is a race window that the result is not the same.
428 * e.g. xfstests #223 when dioread_nolock enables. The reason
429 * is that we lookup a block mapping in extent status tree with
430 * out taking i_data_sem. So at the time the unwritten extent
431 * could be converted.
432 */
Jan Kara2dcba472015-12-07 15:04:57 -0500433 down_read(&EXT4_I(inode)->i_data_sem);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400434 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
435 retval = ext4_ext_map_blocks(handle, inode, map, flags &
436 EXT4_GET_BLOCKS_KEEP_SIZE);
437 } else {
438 retval = ext4_ind_map_blocks(handle, inode, map, flags &
439 EXT4_GET_BLOCKS_KEEP_SIZE);
440 }
Jan Kara2dcba472015-12-07 15:04:57 -0500441 up_read((&EXT4_I(inode)->i_data_sem));
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400442
443 /*
444 * We don't check m_len because extent will be collpased in status
445 * tree. So the m_len might not equal.
446 */
447 if (es_map->m_lblk != map->m_lblk ||
448 es_map->m_flags != map->m_flags ||
449 es_map->m_pblk != map->m_pblk) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400450 printk("ES cache assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400451 "es_cached ex [%d/%d/%llu/%x] != "
452 "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
453 inode->i_ino, es_map->m_lblk, es_map->m_len,
454 es_map->m_pblk, es_map->m_flags, map->m_lblk,
455 map->m_len, map->m_pblk, map->m_flags,
456 retval, flags);
457 }
458}
459#endif /* ES_AGGRESSIVE_TEST */
460
Theodore Ts'o55138e0b2009-09-29 13:31:31 -0400461/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400462 * The ext4_map_blocks() function tries to look up the requested blocks,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400463 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500464 *
Mingming Caof5ab0d12008-02-25 15:29:55 -0500465 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
466 * and store the allocated blocks in the result buffer head and mark it
467 * mapped.
468 *
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400469 * If file type is extents based, it will call ext4_ext_map_blocks(),
470 * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
Mingming Caof5ab0d12008-02-25 15:29:55 -0500471 * based files
472 *
Jan Karafacab4d2016-03-09 22:54:00 -0500473 * On success, it returns the number of blocks being mapped or allocated. if
474 * create==0 and the blocks are pre-allocated and unwritten, the resulting @map
475 * is marked as unwritten. If the create == 1, it will mark @map as mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500476 *
477 * It returns 0 if plain look up failed (blocks have not been allocated), in
Jan Karafacab4d2016-03-09 22:54:00 -0500478 * that case, @map is returned as unmapped but we still do fill map->m_len to
479 * indicate the length of a hole starting at map->m_lblk.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500480 *
481 * It returns the error in case of allocation failure.
482 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400483int ext4_map_blocks(handle_t *handle, struct inode *inode,
484 struct ext4_map_blocks *map, int flags)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500485{
Zheng Liud100eef2013-02-18 00:29:59 -0500486 struct extent_status es;
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500487 int retval;
Lukas Czernerb8a86842014-03-18 18:05:35 -0400488 int ret = 0;
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400489#ifdef ES_AGGRESSIVE_TEST
490 struct ext4_map_blocks orig_map;
491
492 memcpy(&orig_map, map, sizeof(*map));
493#endif
Mingming Caof5ab0d12008-02-25 15:29:55 -0500494
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400495 map->m_flags = 0;
496 ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
497 "logical block %lu\n", inode->i_ino, flags, map->m_len,
498 (unsigned long) map->m_lblk);
Zheng Liud100eef2013-02-18 00:29:59 -0500499
Theodore Ts'oe861b5e2014-02-20 12:54:05 -0500500 /*
501 * ext4_map_blocks returns an int, and m_len is an unsigned int
502 */
503 if (unlikely(map->m_len > INT_MAX))
504 map->m_len = INT_MAX;
505
Kazuya Mio4adb6ab2014-04-07 10:53:28 -0400506 /* We can handle the block number less than EXT_MAX_BLOCKS */
507 if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400508 return -EFSCORRUPTED;
Kazuya Mio4adb6ab2014-04-07 10:53:28 -0400509
Zheng Liud100eef2013-02-18 00:29:59 -0500510 /* Lookup extent status tree firstly */
Theodore Ts'obb5835e2019-08-11 16:32:41 -0400511 if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) {
Zheng Liud100eef2013-02-18 00:29:59 -0500512 if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
513 map->m_pblk = ext4_es_pblock(&es) +
514 map->m_lblk - es.es_lblk;
515 map->m_flags |= ext4_es_is_written(&es) ?
516 EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN;
517 retval = es.es_len - (map->m_lblk - es.es_lblk);
518 if (retval > map->m_len)
519 retval = map->m_len;
520 map->m_len = retval;
521 } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) {
Jan Karafacab4d2016-03-09 22:54:00 -0500522 map->m_pblk = 0;
523 retval = es.es_len - (map->m_lblk - es.es_lblk);
524 if (retval > map->m_len)
525 retval = map->m_len;
526 map->m_len = retval;
Zheng Liud100eef2013-02-18 00:29:59 -0500527 retval = 0;
528 } else {
Arnd Bergmann1e83bc82019-04-07 12:24:43 -0400529 BUG();
Zheng Liud100eef2013-02-18 00:29:59 -0500530 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400531#ifdef ES_AGGRESSIVE_TEST
532 ext4_map_blocks_es_recheck(handle, inode, map,
533 &orig_map, flags);
534#endif
Zheng Liud100eef2013-02-18 00:29:59 -0500535 goto found;
536 }
537
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500538 /*
Theodore Ts'ob920c752009-05-14 00:54:29 -0400539 * Try to see if we can get the block without requesting a new
540 * file system block.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500541 */
Jan Kara2dcba472015-12-07 15:04:57 -0500542 down_read(&EXT4_I(inode)->i_data_sem);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400543 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Dmitry Monakhova4e5d882011-10-25 08:15:12 -0400544 retval = ext4_ext_map_blocks(handle, inode, map, flags &
545 EXT4_GET_BLOCKS_KEEP_SIZE);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500546 } else {
Dmitry Monakhova4e5d882011-10-25 08:15:12 -0400547 retval = ext4_ind_map_blocks(handle, inode, map, flags &
548 EXT4_GET_BLOCKS_KEEP_SIZE);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500549 }
Zheng Liuf7fec032013-02-18 00:28:47 -0500550 if (retval > 0) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400551 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -0500552
Zheng Liu44fb851d2013-07-29 12:51:42 -0400553 if (unlikely(retval != map->m_len)) {
554 ext4_warning(inode->i_sb,
555 "ES len assertion failed for inode "
556 "%lu: retval %d != map->m_len %d",
557 inode->i_ino, retval, map->m_len);
558 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400559 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400560
Zheng Liuf7fec032013-02-18 00:28:47 -0500561 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
562 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
563 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400564 !(status & EXTENT_STATUS_WRITTEN) &&
Eric Whitneyad431022018-10-01 14:10:39 -0400565 ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk,
566 map->m_lblk + map->m_len - 1))
Zheng Liuf7fec032013-02-18 00:28:47 -0500567 status |= EXTENT_STATUS_DELAYED;
568 ret = ext4_es_insert_extent(inode, map->m_lblk,
569 map->m_len, map->m_pblk, status);
570 if (ret < 0)
571 retval = ret;
572 }
Jan Kara2dcba472015-12-07 15:04:57 -0500573 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -0500574
Zheng Liud100eef2013-02-18 00:29:59 -0500575found:
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400576 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Lukas Czernerb8a86842014-03-18 18:05:35 -0400577 ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400578 if (ret != 0)
579 return ret;
580 }
581
Mingming Caof5ab0d12008-02-25 15:29:55 -0500582 /* If it is only a block(s) look up */
Theodore Ts'oc2177052009-05-14 00:58:52 -0400583 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500584 return retval;
585
586 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -0500587 * Returns if the blocks have already allocated
588 *
589 * Note that if blocks have been preallocated
Tao Madf3ab172011-10-08 15:53:49 -0400590 * ext4_ext_get_block() returns the create = 0
Mingming Caof5ab0d12008-02-25 15:29:55 -0500591 * with buffer head unmapped.
592 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400593 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
Lukas Czernerb8a86842014-03-18 18:05:35 -0400594 /*
595 * If we need to convert extent to unwritten
596 * we continue and do the actual work in
597 * ext4_ext_map_blocks()
598 */
599 if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN))
600 return retval;
Mingming Caof5ab0d12008-02-25 15:29:55 -0500601
602 /*
Zheng Liua25a4e12013-02-18 00:28:04 -0500603 * Here we clear m_flags because after allocating an new extent,
604 * it will be set again.
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -0400605 */
Zheng Liua25a4e12013-02-18 00:28:04 -0500606 map->m_flags &= ~EXT4_MAP_FLAGS;
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -0400607
608 /*
Lukas Czerner556615d2014-04-20 23:45:47 -0400609 * New blocks allocate and/or writing to unwritten extent
Mingming Caof5ab0d12008-02-25 15:29:55 -0500610 * will possibly result in updating i_data, so we take
Seunghun Leed91bd2c2014-09-01 22:15:30 -0400611 * the write lock of i_data_sem, and call get_block()
Mingming Caof5ab0d12008-02-25 15:29:55 -0500612 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500613 */
Lukas Czernerc8b459f2014-05-12 12:55:07 -0400614 down_write(&EXT4_I(inode)->i_data_sem);
Mingming Caod2a17632008-07-14 17:52:37 -0400615
616 /*
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500617 * We need to check for EXT4 here because migrate
618 * could have changed the inode type in between
619 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400620 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400621 retval = ext4_ext_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500622 } else {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400623 retval = ext4_ind_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400624
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400625 if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400626 /*
627 * We allocated new blocks which will result in
628 * i_data's format changing. Force the migrate
629 * to fail by clearing migrate flags
630 */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500631 ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400632 }
Mingming Caod2a17632008-07-14 17:52:37 -0400633
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500634 /*
635 * Update reserved blocks/metadata blocks after successful
636 * block allocation which had been deferred till now. We don't
637 * support fallocate for non extent files. So we can update
638 * reserve space here.
639 */
640 if ((retval > 0) &&
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -0500641 (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500642 ext4_da_update_reserve_space(inode, retval, 1);
643 }
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -0400644
Zheng Liuf7fec032013-02-18 00:28:47 -0500645 if (retval > 0) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400646 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -0500647
Zheng Liu44fb851d2013-07-29 12:51:42 -0400648 if (unlikely(retval != map->m_len)) {
649 ext4_warning(inode->i_sb,
650 "ES len assertion failed for inode "
651 "%lu: retval %d != map->m_len %d",
652 inode->i_ino, retval, map->m_len);
653 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400654 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400655
Zheng Liuadb23552013-03-10 21:13:05 -0400656 /*
Jan Karac86d8db2015-12-07 15:10:26 -0500657 * We have to zeroout blocks before inserting them into extent
658 * status tree. Otherwise someone could look them up there and
Jan Kara9b623df2016-09-30 02:02:29 -0400659 * use them before they are really zeroed. We also have to
660 * unmap metadata before zeroing as otherwise writeback can
661 * overwrite zeros with stale data from block device.
Jan Karac86d8db2015-12-07 15:10:26 -0500662 */
663 if (flags & EXT4_GET_BLOCKS_ZERO &&
664 map->m_flags & EXT4_MAP_MAPPED &&
665 map->m_flags & EXT4_MAP_NEW) {
666 ret = ext4_issue_zeroout(inode, map->m_lblk,
667 map->m_pblk, map->m_len);
668 if (ret) {
669 retval = ret;
670 goto out_sem;
671 }
672 }
673
674 /*
Zheng Liuadb23552013-03-10 21:13:05 -0400675 * If the extent has been zeroed out, we don't need to update
676 * extent status tree.
677 */
678 if ((flags & EXT4_GET_BLOCKS_PRE_IO) &&
Theodore Ts'obb5835e2019-08-11 16:32:41 -0400679 ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) {
Zheng Liuadb23552013-03-10 21:13:05 -0400680 if (ext4_es_is_written(&es))
Jan Karac86d8db2015-12-07 15:10:26 -0500681 goto out_sem;
Zheng Liuadb23552013-03-10 21:13:05 -0400682 }
Zheng Liuf7fec032013-02-18 00:28:47 -0500683 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
684 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
685 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400686 !(status & EXTENT_STATUS_WRITTEN) &&
Eric Whitneyad431022018-10-01 14:10:39 -0400687 ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk,
688 map->m_lblk + map->m_len - 1))
Zheng Liuf7fec032013-02-18 00:28:47 -0500689 status |= EXTENT_STATUS_DELAYED;
690 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
691 map->m_pblk, status);
Jan Karac86d8db2015-12-07 15:10:26 -0500692 if (ret < 0) {
Zheng Liuf7fec032013-02-18 00:28:47 -0500693 retval = ret;
Jan Karac86d8db2015-12-07 15:10:26 -0500694 goto out_sem;
695 }
Aditya Kali5356f2612011-09-09 19:20:51 -0400696 }
697
Jan Karac86d8db2015-12-07 15:10:26 -0500698out_sem:
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500699 up_write((&EXT4_I(inode)->i_data_sem));
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400700 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Lukas Czernerb8a86842014-03-18 18:05:35 -0400701 ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400702 if (ret != 0)
703 return ret;
Jan Kara06bd3c32016-04-24 00:56:03 -0400704
705 /*
706 * Inodes with freshly allocated blocks where contents will be
707 * visible after transaction commit must be on transaction's
708 * ordered data list.
709 */
710 if (map->m_flags & EXT4_MAP_NEW &&
711 !(map->m_flags & EXT4_MAP_UNWRITTEN) &&
712 !(flags & EXT4_GET_BLOCKS_ZERO) &&
Tahsin Erdogan02749a42017-06-22 11:31:25 -0400713 !ext4_is_quota_file(inode) &&
Jan Kara06bd3c32016-04-24 00:56:03 -0400714 ext4_should_order_data(inode)) {
Ross Zwisler73131fb2019-06-20 17:26:26 -0400715 loff_t start_byte =
716 (loff_t)map->m_lblk << inode->i_blkbits;
717 loff_t length = (loff_t)map->m_len << inode->i_blkbits;
718
Jan Karaee0876b2016-04-24 00:56:08 -0400719 if (flags & EXT4_GET_BLOCKS_IO_SUBMIT)
Ross Zwisler73131fb2019-06-20 17:26:26 -0400720 ret = ext4_jbd2_inode_add_wait(handle, inode,
721 start_byte, length);
Jan Karaee0876b2016-04-24 00:56:08 -0400722 else
Ross Zwisler73131fb2019-06-20 17:26:26 -0400723 ret = ext4_jbd2_inode_add_write(handle, inode,
724 start_byte, length);
Jan Kara06bd3c32016-04-24 00:56:03 -0400725 if (ret)
726 return ret;
727 }
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400728 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500729 return retval;
730}
731
Jan Karaed8ad832016-02-19 00:18:25 -0500732/*
733 * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages
734 * we have to be careful as someone else may be manipulating b_state as well.
735 */
736static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags)
737{
738 unsigned long old_state;
739 unsigned long new_state;
740
741 flags &= EXT4_MAP_FLAGS;
742
743 /* Dummy buffer_head? Set non-atomically. */
744 if (!bh->b_page) {
745 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags;
746 return;
747 }
748 /*
749 * Someone else may be modifying b_state. Be careful! This is ugly but
750 * once we get rid of using bh as a container for mapping information
751 * to pass to / from get_block functions, this can go away.
752 */
753 do {
754 old_state = READ_ONCE(bh->b_state);
755 new_state = (old_state & ~EXT4_MAP_FLAGS) | flags;
756 } while (unlikely(
757 cmpxchg(&bh->b_state, old_state, new_state) != old_state));
758}
759
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400760static int _ext4_get_block(struct inode *inode, sector_t iblock,
761 struct buffer_head *bh, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700762{
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400763 struct ext4_map_blocks map;
Jan Karaefe70c22016-03-08 23:35:46 -0500764 int ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700765
Tao Ma46c7f252012-12-10 14:04:52 -0500766 if (ext4_has_inline_data(inode))
767 return -ERANGE;
768
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400769 map.m_lblk = iblock;
770 map.m_len = bh->b_size >> inode->i_blkbits;
771
Jan Karaefe70c22016-03-08 23:35:46 -0500772 ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map,
773 flags);
Jan Kara7fb54092008-02-10 01:08:38 -0500774 if (ret > 0) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400775 map_bh(bh, inode->i_sb, map.m_pblk);
Jan Karaed8ad832016-02-19 00:18:25 -0500776 ext4_update_bh_state(bh, map.m_flags);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400777 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
Jan Kara7fb54092008-02-10 01:08:38 -0500778 ret = 0;
Ross Zwisler547edce2016-11-08 11:30:58 +1100779 } else if (ret == 0) {
780 /* hole case, need to fill in bh->b_size */
781 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700782 }
783 return ret;
784}
785
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400786int ext4_get_block(struct inode *inode, sector_t iblock,
787 struct buffer_head *bh, int create)
788{
789 return _ext4_get_block(inode, iblock, bh,
790 create ? EXT4_GET_BLOCKS_CREATE : 0);
791}
792
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700793/*
Jan Kara705965b2016-03-08 23:08:10 -0500794 * Get block function used when preparing for buffered write if we require
795 * creating an unwritten extent if blocks haven't been allocated. The extent
796 * will be converted to written after the IO is complete.
797 */
798int ext4_get_block_unwritten(struct inode *inode, sector_t iblock,
799 struct buffer_head *bh_result, int create)
800{
801 ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n",
802 inode->i_ino, create);
803 return _ext4_get_block(inode, iblock, bh_result,
804 EXT4_GET_BLOCKS_IO_CREATE_EXT);
805}
806
Jan Karaefe70c22016-03-08 23:35:46 -0500807/* Maximum number of blocks we map for direct IO at once. */
808#define DIO_MAX_BLOCKS 4096
809
Jan Karae84dfbe2016-04-01 02:07:22 -0400810/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700811 * `handle' can be NULL if create is zero
812 */
Mingming Cao617ba132006-10-11 01:20:53 -0700813struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400814 ext4_lblk_t block, int map_flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700815{
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400816 struct ext4_map_blocks map;
817 struct buffer_head *bh;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400818 int create = map_flags & EXT4_GET_BLOCKS_CREATE;
Theodore Ts'o10560082014-08-29 20:51:32 -0400819 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700820
821 J_ASSERT(handle != NULL || create == 0);
822
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400823 map.m_lblk = block;
824 map.m_len = 1;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400825 err = ext4_map_blocks(handle, inode, &map, map_flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700826
Theodore Ts'o10560082014-08-29 20:51:32 -0400827 if (err == 0)
828 return create ? ERR_PTR(-ENOSPC) : NULL;
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400829 if (err < 0)
Theodore Ts'o10560082014-08-29 20:51:32 -0400830 return ERR_PTR(err);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400831
832 bh = sb_getblk(inode->i_sb, map.m_pblk);
Theodore Ts'o10560082014-08-29 20:51:32 -0400833 if (unlikely(!bh))
834 return ERR_PTR(-ENOMEM);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400835 if (map.m_flags & EXT4_MAP_NEW) {
836 J_ASSERT(create != 0);
837 J_ASSERT(handle != NULL);
838
839 /*
840 * Now that we do not always journal data, we should
841 * keep in mind whether this should always journal the
842 * new buffer as metadata. For now, regular file
843 * writes use ext4_get_block instead, so it's not a
844 * problem.
845 */
846 lock_buffer(bh);
847 BUFFER_TRACE(bh, "call get_create_access");
Theodore Ts'o10560082014-08-29 20:51:32 -0400848 err = ext4_journal_get_create_access(handle, bh);
849 if (unlikely(err)) {
850 unlock_buffer(bh);
851 goto errout;
852 }
853 if (!buffer_uptodate(bh)) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400854 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
855 set_buffer_uptodate(bh);
856 }
857 unlock_buffer(bh);
858 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
859 err = ext4_handle_dirty_metadata(handle, inode, bh);
Theodore Ts'o10560082014-08-29 20:51:32 -0400860 if (unlikely(err))
861 goto errout;
862 } else
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400863 BUFFER_TRACE(bh, "not a new buffer");
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400864 return bh;
Theodore Ts'o10560082014-08-29 20:51:32 -0400865errout:
866 brelse(bh);
867 return ERR_PTR(err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700868}
869
Mingming Cao617ba132006-10-11 01:20:53 -0700870struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400871 ext4_lblk_t block, int map_flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700872{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400873 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700874
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400875 bh = ext4_getblk(handle, inode, block, map_flags);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400876 if (IS_ERR(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700877 return bh;
ZhangXiaoxu7963e5a2019-08-22 23:00:32 -0400878 if (!bh || ext4_buffer_uptodate(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700879 return bh;
Mike Christiedfec8a12016-06-05 14:31:44 -0500880 ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700881 wait_on_buffer(bh);
882 if (buffer_uptodate(bh))
883 return bh;
884 put_bh(bh);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400885 return ERR_PTR(-EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700886}
887
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400888/* Read a contiguous batch of blocks. */
889int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count,
890 bool wait, struct buffer_head **bhs)
891{
892 int i, err;
893
894 for (i = 0; i < bh_count; i++) {
895 bhs[i] = ext4_getblk(NULL, inode, block + i, 0 /* map_flags */);
896 if (IS_ERR(bhs[i])) {
897 err = PTR_ERR(bhs[i]);
898 bh_count = i;
899 goto out_brelse;
900 }
901 }
902
903 for (i = 0; i < bh_count; i++)
904 /* Note that NULL bhs[i] is valid because of holes. */
ZhangXiaoxu7963e5a2019-08-22 23:00:32 -0400905 if (bhs[i] && !ext4_buffer_uptodate(bhs[i]))
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400906 ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1,
907 &bhs[i]);
908
909 if (!wait)
910 return 0;
911
912 for (i = 0; i < bh_count; i++)
913 if (bhs[i])
914 wait_on_buffer(bhs[i]);
915
916 for (i = 0; i < bh_count; i++) {
917 if (bhs[i] && !buffer_uptodate(bhs[i])) {
918 err = -EIO;
919 goto out_brelse;
920 }
921 }
922 return 0;
923
924out_brelse:
925 for (i = 0; i < bh_count; i++) {
926 brelse(bhs[i]);
927 bhs[i] = NULL;
928 }
929 return err;
930}
931
Tao Maf19d5872012-12-10 14:05:51 -0500932int ext4_walk_page_buffers(handle_t *handle,
933 struct buffer_head *head,
934 unsigned from,
935 unsigned to,
936 int *partial,
937 int (*fn)(handle_t *handle,
938 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700939{
940 struct buffer_head *bh;
941 unsigned block_start, block_end;
942 unsigned blocksize = head->b_size;
943 int err, ret = 0;
944 struct buffer_head *next;
945
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400946 for (bh = head, block_start = 0;
947 ret == 0 && (bh != head || !block_start);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400948 block_start = block_end, bh = next) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700949 next = bh->b_this_page;
950 block_end = block_start + blocksize;
951 if (block_end <= from || block_start >= to) {
952 if (partial && !buffer_uptodate(bh))
953 *partial = 1;
954 continue;
955 }
956 err = (*fn)(handle, bh);
957 if (!ret)
958 ret = err;
959 }
960 return ret;
961}
962
963/*
964 * To preserve ordering, it is essential that the hole instantiation and
965 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -0700966 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -0700967 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700968 * prepare_write() is the right place.
969 *
Jan Kara36ade452013-01-28 09:30:52 -0500970 * Also, this function can nest inside ext4_writepage(). In that case, we
971 * *know* that ext4_writepage() has generated enough buffer credits to do the
972 * whole page. So we won't block on the journal in that case, which is good,
973 * because the caller may be PF_MEMALLOC.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700974 *
Mingming Cao617ba132006-10-11 01:20:53 -0700975 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700976 * quota file writes. If we were to commit the transaction while thus
977 * reentered, there can be a deadlock - we would be holding a quota
978 * lock, and the commit would never complete if another thread had a
979 * transaction open and was blocking on the quota lock - a ranking
980 * violation.
981 *
Mingming Caodab291a2006-10-11 01:21:01 -0700982 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700983 * will _not_ run commit under these circumstances because handle->h_ref
984 * is elevated. We'll still have enough credits for the tiny quotafile
985 * write.
986 */
Tao Maf19d5872012-12-10 14:05:51 -0500987int do_journal_get_write_access(handle_t *handle,
988 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700989{
Jan Kara56d35a42010-08-05 14:41:42 -0400990 int dirty = buffer_dirty(bh);
991 int ret;
992
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700993 if (!buffer_mapped(bh) || buffer_freed(bh))
994 return 0;
Jan Kara56d35a42010-08-05 14:41:42 -0400995 /*
Christoph Hellwigebdec242010-10-06 10:47:23 +0200996 * __block_write_begin() could have dirtied some buffers. Clean
Jan Kara56d35a42010-08-05 14:41:42 -0400997 * the dirty bit as jbd2_journal_get_write_access() could complain
998 * otherwise about fs integrity issues. Setting of the dirty bit
Christoph Hellwigebdec242010-10-06 10:47:23 +0200999 * by __block_write_begin() isn't a real problem here as we clear
Jan Kara56d35a42010-08-05 14:41:42 -04001000 * the bit before releasing a page lock and thus writeback cannot
1001 * ever write the buffer.
1002 */
1003 if (dirty)
1004 clear_buffer_dirty(bh);
liang xie5d601252014-05-12 22:06:43 -04001005 BUFFER_TRACE(bh, "get write access");
Jan Kara56d35a42010-08-05 14:41:42 -04001006 ret = ext4_journal_get_write_access(handle, bh);
1007 if (!ret && dirty)
1008 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
1009 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001010}
1011
Chandan Rajendra643fa962018-12-12 15:20:12 +05301012#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrow2058f832015-04-12 00:55:10 -04001013static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len,
1014 get_block_t *get_block)
1015{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001016 unsigned from = pos & (PAGE_SIZE - 1);
Michael Halcrow2058f832015-04-12 00:55:10 -04001017 unsigned to = from + len;
1018 struct inode *inode = page->mapping->host;
1019 unsigned block_start, block_end;
1020 sector_t block;
1021 int err = 0;
1022 unsigned blocksize = inode->i_sb->s_blocksize;
1023 unsigned bbits;
Chandan Rajendra0b578f32019-05-20 09:29:50 -07001024 struct buffer_head *bh, *head, *wait[2];
1025 int nr_wait = 0;
1026 int i;
Michael Halcrow2058f832015-04-12 00:55:10 -04001027
1028 BUG_ON(!PageLocked(page));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001029 BUG_ON(from > PAGE_SIZE);
1030 BUG_ON(to > PAGE_SIZE);
Michael Halcrow2058f832015-04-12 00:55:10 -04001031 BUG_ON(from > to);
1032
1033 if (!page_has_buffers(page))
1034 create_empty_buffers(page, blocksize, 0);
1035 head = page_buffers(page);
1036 bbits = ilog2(blocksize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001037 block = (sector_t)page->index << (PAGE_SHIFT - bbits);
Michael Halcrow2058f832015-04-12 00:55:10 -04001038
1039 for (bh = head, block_start = 0; bh != head || !block_start;
1040 block++, block_start = block_end, bh = bh->b_this_page) {
1041 block_end = block_start + blocksize;
1042 if (block_end <= from || block_start >= to) {
1043 if (PageUptodate(page)) {
1044 if (!buffer_uptodate(bh))
1045 set_buffer_uptodate(bh);
1046 }
1047 continue;
1048 }
1049 if (buffer_new(bh))
1050 clear_buffer_new(bh);
1051 if (!buffer_mapped(bh)) {
1052 WARN_ON(bh->b_size != blocksize);
1053 err = get_block(inode, block, bh, 1);
1054 if (err)
1055 break;
1056 if (buffer_new(bh)) {
Michael Halcrow2058f832015-04-12 00:55:10 -04001057 if (PageUptodate(page)) {
1058 clear_buffer_new(bh);
1059 set_buffer_uptodate(bh);
1060 mark_buffer_dirty(bh);
1061 continue;
1062 }
1063 if (block_end > to || block_start < from)
1064 zero_user_segments(page, to, block_end,
1065 block_start, from);
1066 continue;
1067 }
1068 }
1069 if (PageUptodate(page)) {
1070 if (!buffer_uptodate(bh))
1071 set_buffer_uptodate(bh);
1072 continue;
1073 }
1074 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
1075 !buffer_unwritten(bh) &&
1076 (block_start < from || block_end > to)) {
Mike Christiedfec8a12016-06-05 14:31:44 -05001077 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
Chandan Rajendra0b578f32019-05-20 09:29:50 -07001078 wait[nr_wait++] = bh;
Michael Halcrow2058f832015-04-12 00:55:10 -04001079 }
1080 }
1081 /*
1082 * If we issued read requests, let them complete.
1083 */
Chandan Rajendra0b578f32019-05-20 09:29:50 -07001084 for (i = 0; i < nr_wait; i++) {
1085 wait_on_buffer(wait[i]);
1086 if (!buffer_uptodate(wait[i]))
Michael Halcrow2058f832015-04-12 00:55:10 -04001087 err = -EIO;
1088 }
Chandan Rajendra7e0785f2019-05-20 09:29:49 -07001089 if (unlikely(err)) {
Michael Halcrow2058f832015-04-12 00:55:10 -04001090 page_zero_new_buffers(page, from, to);
Chandan Rajendra0b578f32019-05-20 09:29:50 -07001091 } else if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) {
1092 for (i = 0; i < nr_wait; i++) {
1093 int err2;
1094
1095 err2 = fscrypt_decrypt_pagecache_blocks(page, blocksize,
1096 bh_offset(wait[i]));
1097 if (err2) {
1098 clear_buffer_uptodate(wait[i]);
1099 err = err2;
1100 }
1101 }
Chandan Rajendra7e0785f2019-05-20 09:29:49 -07001102 }
1103
Michael Halcrow2058f832015-04-12 00:55:10 -04001104 return err;
1105}
1106#endif
1107
Nick Pigginbfc1af62007-10-16 01:25:05 -07001108static int ext4_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001109 loff_t pos, unsigned len, unsigned flags,
1110 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001111{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001112 struct inode *inode = mapping->host;
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001113 int ret, needed_blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001114 handle_t *handle;
1115 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001116 struct page *page;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001117 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001118 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001119
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05001120 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
1121 return -EIO;
1122
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001123 trace_ext4_write_begin(inode, pos, len, flags);
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001124 /*
1125 * Reserve one block more for addition to orphan list in case
1126 * we allocate blocks but write fails for some reason
1127 */
1128 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001129 index = pos >> PAGE_SHIFT;
1130 from = pos & (PAGE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001131 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001132
Tao Maf19d5872012-12-10 14:05:51 -05001133 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
1134 ret = ext4_try_to_write_inline_data(mapping, inode, pos, len,
1135 flags, pagep);
1136 if (ret < 0)
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001137 return ret;
1138 if (ret == 1)
1139 return 0;
Tao Maf19d5872012-12-10 14:05:51 -05001140 }
1141
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001142 /*
1143 * grab_cache_page_write_begin() can take a long time if the
1144 * system is thrashing due to memory pressure, or if the page
1145 * is being written back. So grab it first before we start
1146 * the transaction handle. This also allows us to allocate
1147 * the page (if needed) without using GFP_NOFS.
1148 */
1149retry_grab:
Nick Piggin54566b22009-01-04 12:00:53 -08001150 page = grab_cache_page_write_begin(mapping, index, flags);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001151 if (!page)
1152 return -ENOMEM;
1153 unlock_page(page);
1154
1155retry_journal:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001156 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001157 if (IS_ERR(handle)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001158 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001159 return PTR_ERR(handle);
Jan Karacf108bc2008-07-11 19:27:31 -04001160 }
Tao Maf19d5872012-12-10 14:05:51 -05001161
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001162 lock_page(page);
1163 if (page->mapping != mapping) {
1164 /* The page got truncated from under us */
1165 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001166 put_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04001167 ext4_journal_stop(handle);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001168 goto retry_grab;
Jan Karacf108bc2008-07-11 19:27:31 -04001169 }
Dmitry Monakhov7afe5aa2013-08-28 14:30:47 -04001170 /* In case writeback began while the page was unlocked */
1171 wait_for_stable_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04001172
Chandan Rajendra643fa962018-12-12 15:20:12 +05301173#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrow2058f832015-04-12 00:55:10 -04001174 if (ext4_should_dioread_nolock(inode))
1175 ret = ext4_block_write_begin(page, pos, len,
Jan Kara705965b2016-03-08 23:08:10 -05001176 ext4_get_block_unwritten);
Michael Halcrow2058f832015-04-12 00:55:10 -04001177 else
1178 ret = ext4_block_write_begin(page, pos, len,
1179 ext4_get_block);
1180#else
Jiaying Zhang744692d2010-03-04 16:14:02 -05001181 if (ext4_should_dioread_nolock(inode))
Jan Kara705965b2016-03-08 23:08:10 -05001182 ret = __block_write_begin(page, pos, len,
1183 ext4_get_block_unwritten);
Jiaying Zhang744692d2010-03-04 16:14:02 -05001184 else
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001185 ret = __block_write_begin(page, pos, len, ext4_get_block);
Michael Halcrow2058f832015-04-12 00:55:10 -04001186#endif
Nick Pigginbfc1af62007-10-16 01:25:05 -07001187 if (!ret && ext4_should_journal_data(inode)) {
Tao Maf19d5872012-12-10 14:05:51 -05001188 ret = ext4_walk_page_buffers(handle, page_buffers(page),
1189 from, to, NULL,
1190 do_journal_get_write_access);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001191 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001192
1193 if (ret) {
Eric Biggersc93d8f82019-07-22 09:26:24 -07001194 bool extended = (pos + len > inode->i_size) &&
1195 !ext4_verity_in_progress(inode);
1196
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001197 unlock_page(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001198 /*
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001199 * __block_write_begin may have instantiated a few blocks
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001200 * outside i_size. Trim these off again. Don't need
1201 * i_size_read because we hold i_mutex.
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001202 *
1203 * Add inode to orphan list in case we crash before
1204 * truncate finishes
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001205 */
Eric Biggersc93d8f82019-07-22 09:26:24 -07001206 if (extended && ext4_can_truncate(inode))
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001207 ext4_orphan_add(handle, inode);
1208
1209 ext4_journal_stop(handle);
Eric Biggersc93d8f82019-07-22 09:26:24 -07001210 if (extended) {
Jan Karab9a42072009-12-08 21:24:33 -05001211 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001212 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001213 * If truncate failed early the inode might
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001214 * still be on the orphan list; we need to
1215 * make sure the inode is removed from the
1216 * orphan list in that case.
1217 */
1218 if (inode->i_nlink)
1219 ext4_orphan_del(NULL, inode);
1220 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001221
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001222 if (ret == -ENOSPC &&
1223 ext4_should_retry_alloc(inode->i_sb, &retries))
1224 goto retry_journal;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001225 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001226 return ret;
1227 }
1228 *pagep = page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001229 return ret;
1230}
1231
Nick Pigginbfc1af62007-10-16 01:25:05 -07001232/* For write_end() in data=journal mode */
1233static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001234{
Theodore Ts'o13fca322013-04-21 16:45:54 -04001235 int ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001236 if (!buffer_mapped(bh) || buffer_freed(bh))
1237 return 0;
1238 set_buffer_uptodate(bh);
Theodore Ts'o13fca322013-04-21 16:45:54 -04001239 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
1240 clear_buffer_meta(bh);
1241 clear_buffer_prio(bh);
1242 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001243}
1244
Zheng Liueed43332013-04-03 12:41:17 -04001245/*
1246 * We need to pick up the new inode size which generic_commit_write gave us
1247 * `file' can be NULL - eg, when called from page_symlink().
1248 *
1249 * ext4 never places buffers on inode->i_mapping->private_list. metadata
1250 * buffers are managed internally.
1251 */
1252static int ext4_write_end(struct file *file,
1253 struct address_space *mapping,
1254 loff_t pos, unsigned len, unsigned copied,
1255 struct page *page, void *fsdata)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001256{
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001257 handle_t *handle = ext4_journal_current_handle();
Zheng Liueed43332013-04-03 12:41:17 -04001258 struct inode *inode = mapping->host;
Xiaoguang Wang05726392015-02-12 23:00:17 -05001259 loff_t old_size = inode->i_size;
Zheng Liueed43332013-04-03 12:41:17 -04001260 int ret = 0, ret2;
1261 int i_size_changed = 0;
Theodore Ts'o362eca72018-07-10 01:07:43 -04001262 int inline_data = ext4_has_inline_data(inode);
Eric Biggersc93d8f82019-07-22 09:26:24 -07001263 bool verity = ext4_verity_in_progress(inode);
Zheng Liueed43332013-04-03 12:41:17 -04001264
1265 trace_ext4_write_end(inode, pos, len, copied);
Theodore Ts'o362eca72018-07-10 01:07:43 -04001266 if (inline_data) {
Theodore Ts'o42c832d2013-07-01 08:12:39 -04001267 ret = ext4_write_inline_data_end(inode, pos, len,
1268 copied, page);
Theodore Ts'oeb5efbc2017-02-04 23:04:00 -05001269 if (ret < 0) {
1270 unlock_page(page);
1271 put_page(page);
Theodore Ts'o42c832d2013-07-01 08:12:39 -04001272 goto errout;
Theodore Ts'oeb5efbc2017-02-04 23:04:00 -05001273 }
Theodore Ts'o42c832d2013-07-01 08:12:39 -04001274 copied = ret;
1275 } else
Tao Maf19d5872012-12-10 14:05:51 -05001276 copied = block_write_end(file, mapping, pos,
1277 len, copied, page, fsdata);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001278 /*
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001279 * it's important to update i_size while still holding page lock:
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001280 * page writeout could otherwise come in and zero beyond i_size.
Eric Biggersc93d8f82019-07-22 09:26:24 -07001281 *
1282 * If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree
1283 * blocks are being written past EOF, so skip the i_size update.
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001284 */
Eric Biggersc93d8f82019-07-22 09:26:24 -07001285 if (!verity)
1286 i_size_changed = ext4_update_inode_size(inode, pos + copied);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001287 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001288 put_page(page);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001289
Eric Biggersc93d8f82019-07-22 09:26:24 -07001290 if (old_size < pos && !verity)
Xiaoguang Wang05726392015-02-12 23:00:17 -05001291 pagecache_isize_extended(inode, old_size, pos);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001292 /*
1293 * Don't mark the inode dirty under page lock. First, it unnecessarily
1294 * makes the holding time of page lock longer. Second, it forces lock
1295 * ordering of page lock and transaction start for journaling
1296 * filesystems.
1297 */
Theodore Ts'o362eca72018-07-10 01:07:43 -04001298 if (i_size_changed || inline_data)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001299 ext4_mark_inode_dirty(handle, inode);
1300
Eric Biggersc93d8f82019-07-22 09:26:24 -07001301 if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001302 /* if we have allocated more blocks and copied
1303 * less. We will have blocks allocated outside
1304 * inode->i_size. So truncate them
1305 */
1306 ext4_orphan_add(handle, inode);
Theodore Ts'o74d553a2013-04-03 12:39:17 -04001307errout:
Mingming Cao617ba132006-10-11 01:20:53 -07001308 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001309 if (!ret)
1310 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001311
Eric Biggersc93d8f82019-07-22 09:26:24 -07001312 if (pos + len > inode->i_size && !verity) {
Jan Karab9a42072009-12-08 21:24:33 -05001313 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001314 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001315 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001316 * on the orphan list; we need to make sure the inode
1317 * is removed from the orphan list in that case.
1318 */
1319 if (inode->i_nlink)
1320 ext4_orphan_del(NULL, inode);
1321 }
1322
Nick Pigginbfc1af62007-10-16 01:25:05 -07001323 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001324}
1325
Theodore Ts'ob90197b2015-10-15 10:29:05 -04001326/*
1327 * This is a private version of page_zero_new_buffers() which doesn't
1328 * set the buffer to be dirty, since in data=journalled mode we need
1329 * to call ext4_handle_dirty_metadata() instead.
1330 */
Jan Kara3b136492017-01-27 14:35:38 -05001331static void ext4_journalled_zero_new_buffers(handle_t *handle,
1332 struct page *page,
1333 unsigned from, unsigned to)
Theodore Ts'ob90197b2015-10-15 10:29:05 -04001334{
1335 unsigned int block_start = 0, block_end;
1336 struct buffer_head *head, *bh;
1337
1338 bh = head = page_buffers(page);
1339 do {
1340 block_end = block_start + bh->b_size;
1341 if (buffer_new(bh)) {
1342 if (block_end > from && block_start < to) {
1343 if (!PageUptodate(page)) {
1344 unsigned start, size;
1345
1346 start = max(from, block_start);
1347 size = min(to, block_end) - start;
1348
1349 zero_user(page, start, size);
Jan Kara3b136492017-01-27 14:35:38 -05001350 write_end_fn(handle, bh);
Theodore Ts'ob90197b2015-10-15 10:29:05 -04001351 }
1352 clear_buffer_new(bh);
1353 }
1354 }
1355 block_start = block_end;
1356 bh = bh->b_this_page;
1357 } while (bh != head);
1358}
1359
Nick Pigginbfc1af62007-10-16 01:25:05 -07001360static int ext4_journalled_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001361 struct address_space *mapping,
1362 loff_t pos, unsigned len, unsigned copied,
1363 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001364{
Mingming Cao617ba132006-10-11 01:20:53 -07001365 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -07001366 struct inode *inode = mapping->host;
Xiaoguang Wang05726392015-02-12 23:00:17 -05001367 loff_t old_size = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001368 int ret = 0, ret2;
1369 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001370 unsigned from, to;
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001371 int size_changed = 0;
Theodore Ts'o362eca72018-07-10 01:07:43 -04001372 int inline_data = ext4_has_inline_data(inode);
Eric Biggersc93d8f82019-07-22 09:26:24 -07001373 bool verity = ext4_verity_in_progress(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001374
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001375 trace_ext4_journalled_write_end(inode, pos, len, copied);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001376 from = pos & (PAGE_SIZE - 1);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001377 to = from + len;
1378
Curt Wohlgemuth441c8502011-08-13 11:25:18 -04001379 BUG_ON(!ext4_handle_valid(handle));
1380
Theodore Ts'o362eca72018-07-10 01:07:43 -04001381 if (inline_data) {
Theodore Ts'oeb5efbc2017-02-04 23:04:00 -05001382 ret = ext4_write_inline_data_end(inode, pos, len,
1383 copied, page);
1384 if (ret < 0) {
1385 unlock_page(page);
1386 put_page(page);
1387 goto errout;
1388 }
1389 copied = ret;
1390 } else if (unlikely(copied < len) && !PageUptodate(page)) {
Jan Kara3b136492017-01-27 14:35:38 -05001391 copied = 0;
1392 ext4_journalled_zero_new_buffers(handle, page, from, to);
1393 } else {
1394 if (unlikely(copied < len))
1395 ext4_journalled_zero_new_buffers(handle, page,
1396 from + copied, to);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001397 ret = ext4_walk_page_buffers(handle, page_buffers(page), from,
Jan Kara3b136492017-01-27 14:35:38 -05001398 from + copied, &partial,
1399 write_end_fn);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001400 if (!partial)
1401 SetPageUptodate(page);
1402 }
Eric Biggersc93d8f82019-07-22 09:26:24 -07001403 if (!verity)
1404 size_changed = ext4_update_inode_size(inode, pos + copied);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001405 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Jan Kara2d859db2011-07-26 09:07:11 -04001406 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001407 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001408 put_page(page);
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001409
Eric Biggersc93d8f82019-07-22 09:26:24 -07001410 if (old_size < pos && !verity)
Xiaoguang Wang05726392015-02-12 23:00:17 -05001411 pagecache_isize_extended(inode, old_size, pos);
1412
Theodore Ts'o362eca72018-07-10 01:07:43 -04001413 if (size_changed || inline_data) {
Mingming Cao617ba132006-10-11 01:20:53 -07001414 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001415 if (!ret)
1416 ret = ret2;
1417 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001418
Eric Biggersc93d8f82019-07-22 09:26:24 -07001419 if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001420 /* if we have allocated more blocks and copied
1421 * less. We will have blocks allocated outside
1422 * inode->i_size. So truncate them
1423 */
1424 ext4_orphan_add(handle, inode);
1425
Theodore Ts'oeb5efbc2017-02-04 23:04:00 -05001426errout:
Mingming Cao617ba132006-10-11 01:20:53 -07001427 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001428 if (!ret)
1429 ret = ret2;
Eric Biggersc93d8f82019-07-22 09:26:24 -07001430 if (pos + len > inode->i_size && !verity) {
Jan Karab9a42072009-12-08 21:24:33 -05001431 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001432 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001433 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001434 * on the orphan list; we need to make sure the inode
1435 * is removed from the orphan list in that case.
1436 */
1437 if (inode->i_nlink)
1438 ext4_orphan_del(NULL, inode);
1439 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001440
1441 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001442}
Mingming Caod2a17632008-07-14 17:52:37 -04001443
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001444/*
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001445 * Reserve space for a single cluster
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001446 */
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001447static int ext4_da_reserve_space(struct inode *inode)
Mingming Caod2a17632008-07-14 17:52:37 -04001448{
Mingming Cao60e58e02009-01-22 18:13:05 +01001449 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001450 struct ext4_inode_info *ei = EXT4_I(inode);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001451 int ret;
Mingming Caod2a17632008-07-14 17:52:37 -04001452
Mingming Cao60e58e02009-01-22 18:13:05 +01001453 /*
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001454 * We will charge metadata quota at writeout time; this saves
1455 * us from metadata over-estimation, though we may go over by
1456 * a small amount in the end. Here we just reserve for data.
Mingming Cao60e58e02009-01-22 18:13:05 +01001457 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04001458 ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001459 if (ret)
1460 return ret;
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001461
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001462 spin_lock(&ei->i_block_reservation_lock);
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04001463 if (ext4_claim_free_clusters(sbi, 1, 0)) {
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001464 spin_unlock(&ei->i_block_reservation_lock);
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001465 dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
Mingming Caod2a17632008-07-14 17:52:37 -04001466 return -ENOSPC;
1467 }
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001468 ei->i_reserved_data_blocks++;
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001469 trace_ext4_da_reserve_space(inode);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001470 spin_unlock(&ei->i_block_reservation_lock);
Dmitry Monakhov39bc6802009-12-10 16:36:27 +00001471
Mingming Caod2a17632008-07-14 17:52:37 -04001472 return 0; /* success */
1473}
1474
Eric Whitneyf4567672018-10-01 14:33:24 -04001475void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001476{
1477 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001478 struct ext4_inode_info *ei = EXT4_I(inode);
Mingming Caod2a17632008-07-14 17:52:37 -04001479
Mingming Caocd213222008-08-19 22:16:59 -04001480 if (!to_free)
1481 return; /* Nothing to release, exit */
1482
Mingming Caod2a17632008-07-14 17:52:37 -04001483 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001484
Li Zefan5a58ec872010-05-17 02:00:00 -04001485 trace_ext4_da_release_space(inode, to_free);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001486 if (unlikely(to_free > ei->i_reserved_data_blocks)) {
Mingming Caocd213222008-08-19 22:16:59 -04001487 /*
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001488 * if there aren't enough reserved blocks, then the
1489 * counter is messed up somewhere. Since this
1490 * function is called from invalidate page, it's
1491 * harmless to return without any action.
Mingming Caocd213222008-08-19 22:16:59 -04001492 */
Theodore Ts'o8de5c322013-02-14 15:11:41 -05001493 ext4_warning(inode->i_sb, "ext4_da_release_space: "
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001494 "ino %lu, to_free %d with only %d reserved "
Theodore Ts'o1084f252012-03-19 23:13:43 -04001495 "data blocks", inode->i_ino, to_free,
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001496 ei->i_reserved_data_blocks);
1497 WARN_ON(1);
1498 to_free = ei->i_reserved_data_blocks;
1499 }
1500 ei->i_reserved_data_blocks -= to_free;
1501
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001502 /* update fs dirty data blocks counter */
Theodore Ts'o57042652011-09-09 18:56:51 -04001503 percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free);
Mingming Caod2a17632008-07-14 17:52:37 -04001504
Mingming Caod2a17632008-07-14 17:52:37 -04001505 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001506
Aditya Kali7b415bf2011-09-09 19:04:51 -04001507 dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free));
Mingming Caod2a17632008-07-14 17:52:37 -04001508}
1509
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001510/*
Alex Tomas64769242008-07-11 19:27:31 -04001511 * Delayed allocation stuff
1512 */
1513
Jan Kara4e7ea812013-06-04 13:17:40 -04001514struct mpage_da_data {
1515 struct inode *inode;
1516 struct writeback_control *wbc;
Jan Kara6b523df2013-06-04 13:21:11 -04001517
Jan Kara4e7ea812013-06-04 13:17:40 -04001518 pgoff_t first_page; /* The first page to write */
1519 pgoff_t next_page; /* Current page to examine */
1520 pgoff_t last_page; /* Last page to examine */
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001521 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04001522 * Extent to map - this can be after first_page because that can be
1523 * fully mapped. We somewhat abuse m_flags to store whether the extent
1524 * is delalloc or unwritten.
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001525 */
Jan Kara4e7ea812013-06-04 13:17:40 -04001526 struct ext4_map_blocks map;
1527 struct ext4_io_submit io_submit; /* IO submission data */
Jan Karadddbd6a2017-04-30 18:29:10 -04001528 unsigned int do_map:1;
Jan Kara4e7ea812013-06-04 13:17:40 -04001529};
Alex Tomas64769242008-07-11 19:27:31 -04001530
Jan Kara4e7ea812013-06-04 13:17:40 -04001531static void mpage_release_unused_pages(struct mpage_da_data *mpd,
1532 bool invalidate)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001533{
1534 int nr_pages, i;
1535 pgoff_t index, end;
1536 struct pagevec pvec;
1537 struct inode *inode = mpd->inode;
1538 struct address_space *mapping = inode->i_mapping;
Jan Kara4e7ea812013-06-04 13:17:40 -04001539
1540 /* This is necessary when next_page == 0. */
1541 if (mpd->first_page >= mpd->next_page)
1542 return;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001543
Curt Wohlgemuthc7f59382011-02-26 12:27:52 -05001544 index = mpd->first_page;
1545 end = mpd->next_page - 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04001546 if (invalidate) {
1547 ext4_lblk_t start, last;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001548 start = index << (PAGE_SHIFT - inode->i_blkbits);
1549 last = end << (PAGE_SHIFT - inode->i_blkbits);
Jan Kara4e7ea812013-06-04 13:17:40 -04001550 ext4_es_remove_extent(inode, start, last - start + 1);
1551 }
Zheng Liu51865fd2012-11-08 21:57:32 -05001552
Mel Gorman86679822017-11-15 17:37:52 -08001553 pagevec_init(&pvec);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001554 while (index <= end) {
Jan Kara397162f2017-09-06 16:21:43 -07001555 nr_pages = pagevec_lookup_range(&pvec, mapping, &index, end);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001556 if (nr_pages == 0)
1557 break;
1558 for (i = 0; i < nr_pages; i++) {
1559 struct page *page = pvec.pages[i];
Jan Kara2b85a612017-09-06 16:21:30 -07001560
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001561 BUG_ON(!PageLocked(page));
1562 BUG_ON(PageWriteback(page));
Jan Kara4e7ea812013-06-04 13:17:40 -04001563 if (invalidate) {
wangguang4e800c02016-09-15 11:32:46 -04001564 if (page_mapped(page))
1565 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001566 block_invalidatepage(page, 0, PAGE_SIZE);
Jan Kara4e7ea812013-06-04 13:17:40 -04001567 ClearPageUptodate(page);
1568 }
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001569 unlock_page(page);
1570 }
Jan Kara9b1d09982010-03-03 16:19:32 -05001571 pagevec_release(&pvec);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001572 }
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001573}
1574
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001575static void ext4_print_free_blocks(struct inode *inode)
1576{
1577 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o92b97812012-03-19 23:41:49 -04001578 struct super_block *sb = inode->i_sb;
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001579 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'o92b97812012-03-19 23:41:49 -04001580
1581 ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld",
Theodore Ts'o5dee5432011-09-09 19:10:51 -04001582 EXT4_C2B(EXT4_SB(inode->i_sb),
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001583 ext4_count_free_clusters(sb)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001584 ext4_msg(sb, KERN_CRIT, "Free/Dirty block details");
1585 ext4_msg(sb, KERN_CRIT, "free_blocks=%lld",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001586 (long long) EXT4_C2B(EXT4_SB(sb),
Theodore Ts'o57042652011-09-09 18:56:51 -04001587 percpu_counter_sum(&sbi->s_freeclusters_counter)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001588 ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001589 (long long) EXT4_C2B(EXT4_SB(sb),
Aditya Kali7b415bf2011-09-09 19:04:51 -04001590 percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001591 ext4_msg(sb, KERN_CRIT, "Block reservation details");
1592 ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001593 ei->i_reserved_data_blocks);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001594 return;
1595}
1596
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001597static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001598{
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001599 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001600}
1601
Alex Tomas64769242008-07-11 19:27:31 -04001602/*
Eric Whitney0b02f4c2018-10-01 14:19:37 -04001603 * ext4_insert_delayed_block - adds a delayed block to the extents status
1604 * tree, incrementing the reserved cluster/block
1605 * count or making a pending reservation
1606 * where needed
1607 *
1608 * @inode - file containing the newly added block
1609 * @lblk - logical block to be added
1610 *
1611 * Returns 0 on success, negative error code on failure.
1612 */
1613static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk)
1614{
1615 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1616 int ret;
1617 bool allocated = false;
1618
1619 /*
1620 * If the cluster containing lblk is shared with a delayed,
1621 * written, or unwritten extent in a bigalloc file system, it's
1622 * already been accounted for and does not need to be reserved.
1623 * A pending reservation must be made for the cluster if it's
1624 * shared with a written or unwritten extent and doesn't already
1625 * have one. Written and unwritten extents can be purged from the
1626 * extents status tree if the system is under memory pressure, so
1627 * it's necessary to examine the extent tree if a search of the
1628 * extents status tree doesn't get a match.
1629 */
1630 if (sbi->s_cluster_ratio == 1) {
1631 ret = ext4_da_reserve_space(inode);
1632 if (ret != 0) /* ENOSPC */
1633 goto errout;
1634 } else { /* bigalloc */
1635 if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) {
1636 if (!ext4_es_scan_clu(inode,
1637 &ext4_es_is_mapped, lblk)) {
1638 ret = ext4_clu_mapped(inode,
1639 EXT4_B2C(sbi, lblk));
1640 if (ret < 0)
1641 goto errout;
1642 if (ret == 0) {
1643 ret = ext4_da_reserve_space(inode);
1644 if (ret != 0) /* ENOSPC */
1645 goto errout;
1646 } else {
1647 allocated = true;
1648 }
1649 } else {
1650 allocated = true;
1651 }
1652 }
1653 }
1654
1655 ret = ext4_es_insert_delayed_block(inode, lblk, allocated);
1656
1657errout:
1658 return ret;
1659}
1660
1661/*
Aditya Kali5356f2612011-09-09 19:20:51 -04001662 * This function is grabs code from the very beginning of
1663 * ext4_map_blocks, but assumes that the caller is from delayed write
1664 * time. This function looks up the requested blocks and sets the
1665 * buffer delay bit under the protection of i_data_sem.
1666 */
1667static int ext4_da_map_blocks(struct inode *inode, sector_t iblock,
1668 struct ext4_map_blocks *map,
1669 struct buffer_head *bh)
1670{
Zheng Liud100eef2013-02-18 00:29:59 -05001671 struct extent_status es;
Aditya Kali5356f2612011-09-09 19:20:51 -04001672 int retval;
1673 sector_t invalid_block = ~((sector_t) 0xffff);
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001674#ifdef ES_AGGRESSIVE_TEST
1675 struct ext4_map_blocks orig_map;
1676
1677 memcpy(&orig_map, map, sizeof(*map));
1678#endif
Aditya Kali5356f2612011-09-09 19:20:51 -04001679
1680 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
1681 invalid_block = ~0;
1682
1683 map->m_flags = 0;
1684 ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
1685 "logical block %lu\n", inode->i_ino, map->m_len,
1686 (unsigned long) map->m_lblk);
Zheng Liud100eef2013-02-18 00:29:59 -05001687
1688 /* Lookup extent status tree firstly */
Theodore Ts'obb5835e2019-08-11 16:32:41 -04001689 if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) {
Zheng Liud100eef2013-02-18 00:29:59 -05001690 if (ext4_es_is_hole(&es)) {
1691 retval = 0;
Lukas Czernerc8b459f2014-05-12 12:55:07 -04001692 down_read(&EXT4_I(inode)->i_data_sem);
Zheng Liud100eef2013-02-18 00:29:59 -05001693 goto add_delayed;
1694 }
1695
1696 /*
1697 * Delayed extent could be allocated by fallocate.
1698 * So we need to check it.
1699 */
1700 if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) {
1701 map_bh(bh, inode->i_sb, invalid_block);
1702 set_buffer_new(bh);
1703 set_buffer_delay(bh);
1704 return 0;
1705 }
1706
1707 map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk;
1708 retval = es.es_len - (iblock - es.es_lblk);
1709 if (retval > map->m_len)
1710 retval = map->m_len;
1711 map->m_len = retval;
1712 if (ext4_es_is_written(&es))
1713 map->m_flags |= EXT4_MAP_MAPPED;
1714 else if (ext4_es_is_unwritten(&es))
1715 map->m_flags |= EXT4_MAP_UNWRITTEN;
1716 else
Arnd Bergmann1e83bc82019-04-07 12:24:43 -04001717 BUG();
Zheng Liud100eef2013-02-18 00:29:59 -05001718
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001719#ifdef ES_AGGRESSIVE_TEST
1720 ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0);
1721#endif
Zheng Liud100eef2013-02-18 00:29:59 -05001722 return retval;
1723 }
1724
Aditya Kali5356f2612011-09-09 19:20:51 -04001725 /*
1726 * Try to see if we can get the block without requesting a new
1727 * file system block.
1728 */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04001729 down_read(&EXT4_I(inode)->i_data_sem);
Jan Karacbd75842014-11-25 11:41:49 -05001730 if (ext4_has_inline_data(inode))
Tao Ma9c3569b2012-12-10 14:05:57 -05001731 retval = 0;
Jan Karacbd75842014-11-25 11:41:49 -05001732 else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Zheng Liu2f8e0a72014-11-25 11:44:37 -05001733 retval = ext4_ext_map_blocks(NULL, inode, map, 0);
Aditya Kali5356f2612011-09-09 19:20:51 -04001734 else
Zheng Liu2f8e0a72014-11-25 11:44:37 -05001735 retval = ext4_ind_map_blocks(NULL, inode, map, 0);
Aditya Kali5356f2612011-09-09 19:20:51 -04001736
Zheng Liud100eef2013-02-18 00:29:59 -05001737add_delayed:
Aditya Kali5356f2612011-09-09 19:20:51 -04001738 if (retval == 0) {
Zheng Liuf7fec032013-02-18 00:28:47 -05001739 int ret;
Eric Whitneyad431022018-10-01 14:10:39 -04001740
Aditya Kali5356f2612011-09-09 19:20:51 -04001741 /*
1742 * XXX: __block_prepare_write() unmaps passed block,
1743 * is it OK?
1744 */
Aditya Kali5356f2612011-09-09 19:20:51 -04001745
Eric Whitney0b02f4c2018-10-01 14:19:37 -04001746 ret = ext4_insert_delayed_block(inode, map->m_lblk);
1747 if (ret != 0) {
Zheng Liuf7fec032013-02-18 00:28:47 -05001748 retval = ret;
Zheng Liu51865fd2012-11-08 21:57:32 -05001749 goto out_unlock;
Zheng Liuf7fec032013-02-18 00:28:47 -05001750 }
Zheng Liu51865fd2012-11-08 21:57:32 -05001751
Aditya Kali5356f2612011-09-09 19:20:51 -04001752 map_bh(bh, inode->i_sb, invalid_block);
1753 set_buffer_new(bh);
1754 set_buffer_delay(bh);
Zheng Liuf7fec032013-02-18 00:28:47 -05001755 } else if (retval > 0) {
1756 int ret;
Theodore Ts'o3be78c72013-08-16 21:22:41 -04001757 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -05001758
Zheng Liu44fb851d2013-07-29 12:51:42 -04001759 if (unlikely(retval != map->m_len)) {
1760 ext4_warning(inode->i_sb,
1761 "ES len assertion failed for inode "
1762 "%lu: retval %d != map->m_len %d",
1763 inode->i_ino, retval, map->m_len);
1764 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001765 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001766
Zheng Liuf7fec032013-02-18 00:28:47 -05001767 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
1768 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
1769 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
1770 map->m_pblk, status);
1771 if (ret != 0)
1772 retval = ret;
Aditya Kali5356f2612011-09-09 19:20:51 -04001773 }
1774
1775out_unlock:
1776 up_read((&EXT4_I(inode)->i_data_sem));
1777
1778 return retval;
1779}
1780
1781/*
Seunghun Leed91bd2c2014-09-01 22:15:30 -04001782 * This is a special get_block_t callback which is used by
Theodore Ts'ob920c752009-05-14 00:54:29 -04001783 * ext4_da_write_begin(). It will either return mapped block or
1784 * reserve space for a single block.
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001785 *
1786 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
1787 * We also have b_blocknr = -1 and b_bdev initialized properly
1788 *
1789 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
1790 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
1791 * initialized properly.
Alex Tomas64769242008-07-11 19:27:31 -04001792 */
Tao Ma9c3569b2012-12-10 14:05:57 -05001793int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
1794 struct buffer_head *bh, int create)
Alex Tomas64769242008-07-11 19:27:31 -04001795{
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001796 struct ext4_map_blocks map;
Alex Tomas64769242008-07-11 19:27:31 -04001797 int ret = 0;
1798
1799 BUG_ON(create == 0);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001800 BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
1801
1802 map.m_lblk = iblock;
1803 map.m_len = 1;
Alex Tomas64769242008-07-11 19:27:31 -04001804
1805 /*
1806 * first, we need to know whether the block is allocated already
1807 * preallocated blocks are unmapped but should treated
1808 * the same as allocated blocks.
1809 */
Aditya Kali5356f2612011-09-09 19:20:51 -04001810 ret = ext4_da_map_blocks(inode, iblock, &map, bh);
1811 if (ret <= 0)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001812 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04001813
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001814 map_bh(bh, inode->i_sb, map.m_pblk);
Jan Karaed8ad832016-02-19 00:18:25 -05001815 ext4_update_bh_state(bh, map.m_flags);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001816
1817 if (buffer_unwritten(bh)) {
1818 /* A delayed write to unwritten bh should be marked
1819 * new and mapped. Mapped ensures that we don't do
1820 * get_block multiple times when we write to the same
1821 * offset and new ensures that we do proper zero out
1822 * for partial write.
1823 */
1824 set_buffer_new(bh);
Theodore Ts'oc8205632011-04-10 22:30:07 -04001825 set_buffer_mapped(bh);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001826 }
1827 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04001828}
Mingming Cao61628a32008-07-11 19:27:31 -04001829
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001830static int bget_one(handle_t *handle, struct buffer_head *bh)
1831{
1832 get_bh(bh);
1833 return 0;
1834}
1835
1836static int bput_one(handle_t *handle, struct buffer_head *bh)
1837{
1838 put_bh(bh);
1839 return 0;
1840}
1841
1842static int __ext4_journalled_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001843 unsigned int len)
1844{
1845 struct address_space *mapping = page->mapping;
1846 struct inode *inode = mapping->host;
Tao Ma3fdcfb62012-12-10 14:05:57 -05001847 struct buffer_head *page_bufs = NULL;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001848 handle_t *handle = NULL;
Tao Ma3fdcfb62012-12-10 14:05:57 -05001849 int ret = 0, err = 0;
1850 int inline_data = ext4_has_inline_data(inode);
1851 struct buffer_head *inode_bh = NULL;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001852
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001853 ClearPageChecked(page);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001854
1855 if (inline_data) {
1856 BUG_ON(page->index != 0);
1857 BUG_ON(len > ext4_get_max_inline_size(inode));
1858 inode_bh = ext4_journalled_write_inline_data(inode, len, page);
1859 if (inode_bh == NULL)
1860 goto out;
1861 } else {
1862 page_bufs = page_buffers(page);
1863 if (!page_bufs) {
1864 BUG();
1865 goto out;
1866 }
1867 ext4_walk_page_buffers(handle, page_bufs, 0, len,
1868 NULL, bget_one);
1869 }
Theodore Ts'obdf96832015-06-12 23:45:33 -04001870 /*
1871 * We need to release the page lock before we start the
1872 * journal, so grab a reference so the page won't disappear
1873 * out from under us.
1874 */
1875 get_page(page);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001876 unlock_page(page);
1877
Theodore Ts'o9924a922013-02-08 21:59:22 -05001878 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
1879 ext4_writepage_trans_blocks(inode));
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001880 if (IS_ERR(handle)) {
1881 ret = PTR_ERR(handle);
Theodore Ts'obdf96832015-06-12 23:45:33 -04001882 put_page(page);
1883 goto out_no_pagelock;
1884 }
1885 BUG_ON(!ext4_handle_valid(handle));
1886
1887 lock_page(page);
1888 put_page(page);
1889 if (page->mapping != mapping) {
1890 /* The page got truncated from under us */
1891 ext4_journal_stop(handle);
1892 ret = 0;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001893 goto out;
1894 }
1895
Tao Ma3fdcfb62012-12-10 14:05:57 -05001896 if (inline_data) {
Theodore Ts'o362eca72018-07-10 01:07:43 -04001897 ret = ext4_mark_inode_dirty(handle, inode);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001898 } else {
1899 ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
1900 do_journal_get_write_access);
1901
1902 err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
1903 write_end_fn);
1904 }
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001905 if (ret == 0)
1906 ret = err;
Jan Kara2d859db2011-07-26 09:07:11 -04001907 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001908 err = ext4_journal_stop(handle);
1909 if (!ret)
1910 ret = err;
1911
Tao Ma3fdcfb62012-12-10 14:05:57 -05001912 if (!ext4_has_inline_data(inode))
Theodore Ts'o8c9367f2014-01-07 13:08:03 -05001913 ext4_walk_page_buffers(NULL, page_bufs, 0, len,
Tao Ma3fdcfb62012-12-10 14:05:57 -05001914 NULL, bput_one);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001915 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001916out:
Theodore Ts'obdf96832015-06-12 23:45:33 -04001917 unlock_page(page);
1918out_no_pagelock:
Tao Ma3fdcfb62012-12-10 14:05:57 -05001919 brelse(inode_bh);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001920 return ret;
1921}
1922
Mingming Cao61628a32008-07-11 19:27:31 -04001923/*
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001924 * Note that we don't need to start a transaction unless we're journaling data
1925 * because we should have holes filled from ext4_page_mkwrite(). We even don't
1926 * need to file the inode to the transaction's list in ordered mode because if
1927 * we are writing back data added by write(), the inode is already there and if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001928 * we are writing back data modified via mmap(), no one guarantees in which
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001929 * transaction the data will hit the disk. In case we are journaling data, we
1930 * cannot start transaction directly because transaction start ranks above page
1931 * lock so we have to do some magic.
1932 *
Theodore Ts'ob920c752009-05-14 00:54:29 -04001933 * This function can get called via...
Theodore Ts'o20970ba2013-06-06 14:00:46 -04001934 * - ext4_writepages after taking page lock (have journal handle)
Theodore Ts'ob920c752009-05-14 00:54:29 -04001935 * - journal_submit_inode_data_buffers (no journal handle)
Artem Bityutskiyf6463b02012-07-25 18:12:04 +03001936 * - shrink_page_list via the kswapd/direct reclaim (no journal handle)
Theodore Ts'ob920c752009-05-14 00:54:29 -04001937 * - grab_page_cache when doing write_begin (have journal handle)
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001938 *
1939 * We don't do any block allocation in this function. If we have page with
1940 * multiple blocks we need to write those buffer_heads that are mapped. This
1941 * is important for mmaped based write. So if we do with blocksize 1K
1942 * truncate(f, 1024);
1943 * a = mmap(f, 0, 4096);
1944 * a[0] = 'a';
1945 * truncate(f, 4096);
1946 * we have in the page first buffer_head mapped via page_mkwrite call back
Paul Bolle90802ed2011-12-05 13:00:34 +01001947 * but other buffer_heads would be unmapped but dirty (dirty done via the
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001948 * do_wp_page). So writepage should write the first block. If we modify
1949 * the mmap area beyond 1024 we will again get a page_fault and the
1950 * page_mkwrite callback will do the block allocation and mark the
1951 * buffer_heads mapped.
1952 *
1953 * We redirty the page if we have any buffer_heads that is either delay or
1954 * unwritten in the page.
1955 *
1956 * We can get recursively called as show below.
1957 *
1958 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
1959 * ext4_writepage()
1960 *
1961 * But since we don't do any block allocation we should not deadlock.
1962 * Page also have the dirty flag cleared so we don't get recurive page_lock.
Mingming Cao61628a32008-07-11 19:27:31 -04001963 */
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001964static int ext4_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001965 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04001966{
Jan Karaf8bec372013-01-28 12:55:08 -05001967 int ret = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04001968 loff_t size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001969 unsigned int len;
Jiaying Zhang744692d2010-03-04 16:14:02 -05001970 struct buffer_head *page_bufs = NULL;
Mingming Cao61628a32008-07-11 19:27:31 -04001971 struct inode *inode = page->mapping->host;
Jan Kara36ade452013-01-28 09:30:52 -05001972 struct ext4_io_submit io_submit;
Namjae Jeon1c8349a2014-05-12 08:12:25 -04001973 bool keep_towrite = false;
Alex Tomas64769242008-07-11 19:27:31 -04001974
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05001975 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) {
yangerkunc2a559b2020-02-26 12:10:02 +08001976 inode->i_mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05001977 unlock_page(page);
1978 return -EIO;
1979 }
1980
Lukas Czernera9c667f2011-06-06 09:51:52 -04001981 trace_ext4_writepage(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04001982 size = i_size_read(inode);
Eric Biggersc93d8f82019-07-22 09:26:24 -07001983 if (page->index == size >> PAGE_SHIFT &&
1984 !ext4_verity_in_progress(inode))
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001985 len = size & ~PAGE_MASK;
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04001986 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001987 len = PAGE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04001988
Theodore Ts'oa42afc52010-10-27 21:30:09 -04001989 page_bufs = page_buffers(page);
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001990 /*
Jan Karafe386132013-01-28 21:06:42 -05001991 * We cannot do block allocation or other extent handling in this
1992 * function. If there are buffers needing that, we have to redirty
1993 * the page. But we may reach here when we do a journal commit via
1994 * journal_submit_inode_data_buffers() and in that case we must write
1995 * allocated buffers to achieve data=ordered mode guarantees.
Theodore Ts'occcd1472015-10-03 10:49:23 -04001996 *
1997 * Also, if there is only one buffer per page (the fs block
1998 * size == the page size), if one buffer needs block
1999 * allocation or needs to modify the extent tree to clear the
2000 * unwritten flag, we know that the page can't be written at
2001 * all, so we might as well refuse the write immediately.
2002 * Unfortunately if the block size != page size, we can't as
2003 * easily detect this case using ext4_walk_page_buffers(), but
2004 * for the extremely common case, this is an optimization that
2005 * skips a useless round trip through ext4_bio_write_page().
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04002006 */
Tao Maf19d5872012-12-10 14:05:51 -05002007 if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL,
2008 ext4_bh_delay_or_unwritten)) {
Jan Karaf8bec372013-01-28 12:55:08 -05002009 redirty_page_for_writepage(wbc, page);
Theodore Ts'occcd1472015-10-03 10:49:23 -04002010 if ((current->flags & PF_MEMALLOC) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002011 (inode->i_sb->s_blocksize == PAGE_SIZE)) {
Jan Karafe386132013-01-28 21:06:42 -05002012 /*
2013 * For memory cleaning there's no point in writing only
2014 * some buffers. So just bail out. Warn if we came here
2015 * from direct reclaim.
2016 */
2017 WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD))
2018 == PF_MEMALLOC);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002019 unlock_page(page);
2020 return 0;
2021 }
Namjae Jeon1c8349a2014-05-12 08:12:25 -04002022 keep_towrite = true;
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002023 }
Alex Tomas64769242008-07-11 19:27:31 -04002024
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002025 if (PageChecked(page) && ext4_should_journal_data(inode))
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002026 /*
2027 * It's mmapped pagecache. Add buffers and journal it. There
2028 * doesn't seem much point in redirtying the page here.
2029 */
Wu Fengguang3f0ca302009-11-24 11:15:44 -05002030 return __ext4_journalled_writepage(page, len);
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002031
Jan Kara97a851e2013-06-04 11:58:58 -04002032 ext4_io_submit_init(&io_submit, wbc);
2033 io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS);
2034 if (!io_submit.io_end) {
2035 redirty_page_for_writepage(wbc, page);
2036 unlock_page(page);
2037 return -ENOMEM;
2038 }
Namjae Jeon1c8349a2014-05-12 08:12:25 -04002039 ret = ext4_bio_write_page(&io_submit, page, len, wbc, keep_towrite);
Jan Kara36ade452013-01-28 09:30:52 -05002040 ext4_io_submit(&io_submit);
Jan Kara97a851e2013-06-04 11:58:58 -04002041 /* Drop io_end reference we got from init */
2042 ext4_put_io_end_defer(io_submit.io_end);
Alex Tomas64769242008-07-11 19:27:31 -04002043 return ret;
2044}
2045
Jan Kara5f1132b2013-08-17 10:02:33 -04002046static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page)
2047{
2048 int len;
Jan Karaa056bda2017-05-26 17:45:45 -04002049 loff_t size;
Jan Kara5f1132b2013-08-17 10:02:33 -04002050 int err;
2051
2052 BUG_ON(page->index != mpd->first_page);
Jan Karaa056bda2017-05-26 17:45:45 -04002053 clear_page_dirty_for_io(page);
2054 /*
2055 * We have to be very careful here! Nothing protects writeback path
2056 * against i_size changes and the page can be writeably mapped into
2057 * page tables. So an application can be growing i_size and writing
2058 * data through mmap while writeback runs. clear_page_dirty_for_io()
2059 * write-protects our page in page tables and the page cannot get
2060 * written to again until we release page lock. So only after
2061 * clear_page_dirty_for_io() we are safe to sample i_size for
2062 * ext4_bio_write_page() to zero-out tail of the written page. We rely
2063 * on the barrier provided by TestClearPageDirty in
2064 * clear_page_dirty_for_io() to make sure i_size is really sampled only
2065 * after page tables are updated.
2066 */
2067 size = i_size_read(mpd->inode);
Eric Biggersc93d8f82019-07-22 09:26:24 -07002068 if (page->index == size >> PAGE_SHIFT &&
2069 !ext4_verity_in_progress(mpd->inode))
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002070 len = size & ~PAGE_MASK;
Jan Kara5f1132b2013-08-17 10:02:33 -04002071 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002072 len = PAGE_SIZE;
Namjae Jeon1c8349a2014-05-12 08:12:25 -04002073 err = ext4_bio_write_page(&mpd->io_submit, page, len, mpd->wbc, false);
Jan Kara5f1132b2013-08-17 10:02:33 -04002074 if (!err)
2075 mpd->wbc->nr_to_write--;
2076 mpd->first_page++;
2077
2078 return err;
2079}
2080
Jan Kara4e7ea812013-06-04 13:17:40 -04002081#define BH_FLAGS ((1 << BH_Unwritten) | (1 << BH_Delay))
2082
Mingming Cao61628a32008-07-11 19:27:31 -04002083/*
Jan Karafffb2732013-06-04 13:01:11 -04002084 * mballoc gives us at most this number of blocks...
2085 * XXX: That seems to be only a limitation of ext4_mb_normalize_request().
Anatol Pomozov70261f52013-08-28 14:40:12 -04002086 * The rest of mballoc seems to handle chunks up to full group size.
Mingming Cao61628a32008-07-11 19:27:31 -04002087 */
Jan Karafffb2732013-06-04 13:01:11 -04002088#define MAX_WRITEPAGES_EXTENT_LEN 2048
Mingming Cao525f4ed2008-08-19 22:15:58 -04002089
Jan Karafffb2732013-06-04 13:01:11 -04002090/*
Jan Kara4e7ea812013-06-04 13:17:40 -04002091 * mpage_add_bh_to_extent - try to add bh to extent of blocks to map
2092 *
2093 * @mpd - extent of blocks
2094 * @lblk - logical number of the block in the file
Jan Kara09930042013-08-17 09:57:56 -04002095 * @bh - buffer head we want to add to the extent
Jan Kara4e7ea812013-06-04 13:17:40 -04002096 *
Jan Kara09930042013-08-17 09:57:56 -04002097 * The function is used to collect contig. blocks in the same state. If the
2098 * buffer doesn't require mapping for writeback and we haven't started the
2099 * extent of buffers to map yet, the function returns 'true' immediately - the
2100 * caller can write the buffer right away. Otherwise the function returns true
2101 * if the block has been added to the extent, false if the block couldn't be
2102 * added.
Jan Kara4e7ea812013-06-04 13:17:40 -04002103 */
Jan Kara09930042013-08-17 09:57:56 -04002104static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk,
2105 struct buffer_head *bh)
Jan Kara4e7ea812013-06-04 13:17:40 -04002106{
2107 struct ext4_map_blocks *map = &mpd->map;
2108
Jan Kara09930042013-08-17 09:57:56 -04002109 /* Buffer that doesn't need mapping for writeback? */
2110 if (!buffer_dirty(bh) || !buffer_mapped(bh) ||
2111 (!buffer_delay(bh) && !buffer_unwritten(bh))) {
2112 /* So far no extent to map => we write the buffer right away */
2113 if (map->m_len == 0)
2114 return true;
2115 return false;
2116 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002117
2118 /* First block in the extent? */
2119 if (map->m_len == 0) {
Jan Karadddbd6a2017-04-30 18:29:10 -04002120 /* We cannot map unless handle is started... */
2121 if (!mpd->do_map)
2122 return false;
Jan Kara4e7ea812013-06-04 13:17:40 -04002123 map->m_lblk = lblk;
2124 map->m_len = 1;
Jan Kara09930042013-08-17 09:57:56 -04002125 map->m_flags = bh->b_state & BH_FLAGS;
2126 return true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002127 }
2128
Jan Kara09930042013-08-17 09:57:56 -04002129 /* Don't go larger than mballoc is willing to allocate */
2130 if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN)
2131 return false;
2132
Jan Kara4e7ea812013-06-04 13:17:40 -04002133 /* Can we merge the block to our big extent? */
2134 if (lblk == map->m_lblk + map->m_len &&
Jan Kara09930042013-08-17 09:57:56 -04002135 (bh->b_state & BH_FLAGS) == map->m_flags) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002136 map->m_len++;
Jan Kara09930042013-08-17 09:57:56 -04002137 return true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002138 }
Jan Kara09930042013-08-17 09:57:56 -04002139 return false;
Jan Kara4e7ea812013-06-04 13:17:40 -04002140}
2141
Jan Kara5f1132b2013-08-17 10:02:33 -04002142/*
2143 * mpage_process_page_bufs - submit page buffers for IO or add them to extent
2144 *
2145 * @mpd - extent of blocks for mapping
2146 * @head - the first buffer in the page
2147 * @bh - buffer we should start processing from
2148 * @lblk - logical number of the block in the file corresponding to @bh
2149 *
2150 * Walk through page buffers from @bh upto @head (exclusive) and either submit
2151 * the page for IO if all buffers in this page were mapped and there's no
2152 * accumulated extent of buffers to map or add buffers in the page to the
2153 * extent of buffers to map. The function returns 1 if the caller can continue
2154 * by processing the next page, 0 if it should stop adding buffers to the
2155 * extent to map because we cannot extend it anymore. It can also return value
2156 * < 0 in case of error during IO submission.
2157 */
2158static int mpage_process_page_bufs(struct mpage_da_data *mpd,
2159 struct buffer_head *head,
2160 struct buffer_head *bh,
2161 ext4_lblk_t lblk)
Jan Kara4e7ea812013-06-04 13:17:40 -04002162{
2163 struct inode *inode = mpd->inode;
Jan Kara5f1132b2013-08-17 10:02:33 -04002164 int err;
Fabian Frederick93407472017-02-27 14:28:32 -08002165 ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(inode) - 1)
Jan Kara4e7ea812013-06-04 13:17:40 -04002166 >> inode->i_blkbits;
2167
Eric Biggersc93d8f82019-07-22 09:26:24 -07002168 if (ext4_verity_in_progress(inode))
2169 blocks = EXT_MAX_BLOCKS;
2170
Jan Kara4e7ea812013-06-04 13:17:40 -04002171 do {
2172 BUG_ON(buffer_locked(bh));
2173
Jan Kara09930042013-08-17 09:57:56 -04002174 if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002175 /* Found extent to map? */
2176 if (mpd->map.m_len)
Jan Kara5f1132b2013-08-17 10:02:33 -04002177 return 0;
Jan Karadddbd6a2017-04-30 18:29:10 -04002178 /* Buffer needs mapping and handle is not started? */
2179 if (!mpd->do_map)
2180 return 0;
Jan Kara09930042013-08-17 09:57:56 -04002181 /* Everything mapped so far and we hit EOF */
Jan Kara5f1132b2013-08-17 10:02:33 -04002182 break;
Jan Kara4e7ea812013-06-04 13:17:40 -04002183 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002184 } while (lblk++, (bh = bh->b_this_page) != head);
Jan Kara5f1132b2013-08-17 10:02:33 -04002185 /* So far everything mapped? Submit the page for IO. */
2186 if (mpd->map.m_len == 0) {
2187 err = mpage_submit_page(mpd, head->b_page);
2188 if (err < 0)
2189 return err;
2190 }
2191 return lblk < blocks;
Jan Kara4e7ea812013-06-04 13:17:40 -04002192}
2193
2194/*
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302195 * mpage_process_page - update page buffers corresponding to changed extent and
2196 * may submit fully mapped page for IO
2197 *
2198 * @mpd - description of extent to map, on return next extent to map
2199 * @m_lblk - logical block mapping.
2200 * @m_pblk - corresponding physical mapping.
2201 * @map_bh - determines on return whether this page requires any further
2202 * mapping or not.
2203 * Scan given page buffers corresponding to changed extent and update buffer
2204 * state according to new extent state.
2205 * We map delalloc buffers to their physical location, clear unwritten bits.
2206 * If the given page is not fully mapped, we update @map to the next extent in
2207 * the given page that needs mapping & return @map_bh as true.
2208 */
2209static int mpage_process_page(struct mpage_da_data *mpd, struct page *page,
2210 ext4_lblk_t *m_lblk, ext4_fsblk_t *m_pblk,
2211 bool *map_bh)
2212{
2213 struct buffer_head *head, *bh;
2214 ext4_io_end_t *io_end = mpd->io_submit.io_end;
2215 ext4_lblk_t lblk = *m_lblk;
2216 ext4_fsblk_t pblock = *m_pblk;
2217 int err = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302218 int blkbits = mpd->inode->i_blkbits;
2219 ssize_t io_end_size = 0;
2220 struct ext4_io_end_vec *io_end_vec = ext4_last_io_end_vec(io_end);
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302221
2222 bh = head = page_buffers(page);
2223 do {
2224 if (lblk < mpd->map.m_lblk)
2225 continue;
2226 if (lblk >= mpd->map.m_lblk + mpd->map.m_len) {
2227 /*
2228 * Buffer after end of mapped extent.
2229 * Find next buffer in the page to map.
2230 */
2231 mpd->map.m_len = 0;
2232 mpd->map.m_flags = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302233 io_end_vec->size += io_end_size;
2234 io_end_size = 0;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302235
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302236 err = mpage_process_page_bufs(mpd, head, bh, lblk);
2237 if (err > 0)
2238 err = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302239 if (!err && mpd->map.m_len && mpd->map.m_lblk > lblk) {
2240 io_end_vec = ext4_alloc_io_end_vec(io_end);
Ritesh Harjani4d06bfb2019-11-06 15:08:09 +05302241 if (IS_ERR(io_end_vec)) {
2242 err = PTR_ERR(io_end_vec);
2243 goto out;
2244 }
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302245 io_end_vec->offset = mpd->map.m_lblk << blkbits;
2246 }
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302247 *map_bh = true;
2248 goto out;
2249 }
2250 if (buffer_delay(bh)) {
2251 clear_buffer_delay(bh);
2252 bh->b_blocknr = pblock++;
2253 }
2254 clear_buffer_unwritten(bh);
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302255 io_end_size += (1 << blkbits);
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302256 } while (lblk++, (bh = bh->b_this_page) != head);
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302257
2258 io_end_vec->size += io_end_size;
2259 io_end_size = 0;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302260 *map_bh = false;
2261out:
2262 *m_lblk = lblk;
2263 *m_pblk = pblock;
2264 return err;
2265}
2266
2267/*
Jan Kara4e7ea812013-06-04 13:17:40 -04002268 * mpage_map_buffers - update buffers corresponding to changed extent and
2269 * submit fully mapped pages for IO
2270 *
2271 * @mpd - description of extent to map, on return next extent to map
2272 *
2273 * Scan buffers corresponding to changed extent (we expect corresponding pages
2274 * to be already locked) and update buffer state according to new extent state.
2275 * We map delalloc buffers to their physical location, clear unwritten bits,
Lukas Czerner556615d2014-04-20 23:45:47 -04002276 * and mark buffers as uninit when we perform writes to unwritten extents
Jan Kara4e7ea812013-06-04 13:17:40 -04002277 * and do extent conversion after IO is finished. If the last page is not fully
2278 * mapped, we update @map to the next extent in the last page that needs
2279 * mapping. Otherwise we submit the page for IO.
2280 */
2281static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd)
2282{
2283 struct pagevec pvec;
2284 int nr_pages, i;
2285 struct inode *inode = mpd->inode;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002286 int bpp_bits = PAGE_SHIFT - inode->i_blkbits;
Jan Kara4e7ea812013-06-04 13:17:40 -04002287 pgoff_t start, end;
2288 ext4_lblk_t lblk;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302289 ext4_fsblk_t pblock;
Jan Kara4e7ea812013-06-04 13:17:40 -04002290 int err;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302291 bool map_bh = false;
Jan Kara4e7ea812013-06-04 13:17:40 -04002292
2293 start = mpd->map.m_lblk >> bpp_bits;
2294 end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits;
2295 lblk = start << bpp_bits;
2296 pblock = mpd->map.m_pblk;
2297
Mel Gorman86679822017-11-15 17:37:52 -08002298 pagevec_init(&pvec);
Jan Kara4e7ea812013-06-04 13:17:40 -04002299 while (start <= end) {
Jan Kara2b85a612017-09-06 16:21:30 -07002300 nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping,
Jan Kara397162f2017-09-06 16:21:43 -07002301 &start, end);
Jan Kara4e7ea812013-06-04 13:17:40 -04002302 if (nr_pages == 0)
2303 break;
2304 for (i = 0; i < nr_pages; i++) {
2305 struct page *page = pvec.pages[i];
2306
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302307 err = mpage_process_page(mpd, page, &lblk, &pblock,
2308 &map_bh);
Jan Kara4e7ea812013-06-04 13:17:40 -04002309 /*
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302310 * If map_bh is true, means page may require further bh
2311 * mapping, or maybe the page was submitted for IO.
2312 * So we return to call further extent mapping.
Jan Kara4e7ea812013-06-04 13:17:40 -04002313 */
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302314 if (err < 0 || map_bh == true)
2315 goto out;
Jan Kara4e7ea812013-06-04 13:17:40 -04002316 /* Page fully mapped - let IO run! */
2317 err = mpage_submit_page(mpd, page);
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302318 if (err < 0)
2319 goto out;
Jan Kara4e7ea812013-06-04 13:17:40 -04002320 }
2321 pagevec_release(&pvec);
2322 }
2323 /* Extent fully mapped and matches with page boundary. We are done. */
2324 mpd->map.m_len = 0;
2325 mpd->map.m_flags = 0;
2326 return 0;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302327out:
2328 pagevec_release(&pvec);
2329 return err;
Jan Kara4e7ea812013-06-04 13:17:40 -04002330}
2331
2332static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd)
2333{
2334 struct inode *inode = mpd->inode;
2335 struct ext4_map_blocks *map = &mpd->map;
2336 int get_blocks_flags;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002337 int err, dioread_nolock;
Jan Kara4e7ea812013-06-04 13:17:40 -04002338
2339 trace_ext4_da_write_pages_extent(inode, map);
2340 /*
2341 * Call ext4_map_blocks() to allocate any delayed allocation blocks, or
Lukas Czerner556615d2014-04-20 23:45:47 -04002342 * to convert an unwritten extent to be initialized (in the case
Jan Kara4e7ea812013-06-04 13:17:40 -04002343 * where we have written into one or more preallocated blocks). It is
2344 * possible that we're going to need more metadata blocks than
2345 * previously reserved. However we must not fail because we're in
2346 * writeback and there is nothing we can do about it so it might result
2347 * in data loss. So use reserved blocks to allocate metadata if
2348 * possible.
2349 *
Theodore Ts'o754cfed2014-09-04 18:08:22 -04002350 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if
2351 * the blocks in question are delalloc blocks. This indicates
2352 * that the blocks and quotas has already been checked when
2353 * the data was copied into the page cache.
Jan Kara4e7ea812013-06-04 13:17:40 -04002354 */
2355 get_blocks_flags = EXT4_GET_BLOCKS_CREATE |
Jan Karaee0876b2016-04-24 00:56:08 -04002356 EXT4_GET_BLOCKS_METADATA_NOFAIL |
2357 EXT4_GET_BLOCKS_IO_SUBMIT;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002358 dioread_nolock = ext4_should_dioread_nolock(inode);
2359 if (dioread_nolock)
Jan Kara4e7ea812013-06-04 13:17:40 -04002360 get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
2361 if (map->m_flags & (1 << BH_Delay))
2362 get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
2363
2364 err = ext4_map_blocks(handle, inode, map, get_blocks_flags);
2365 if (err < 0)
2366 return err;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002367 if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) {
Jan Kara6b523df2013-06-04 13:21:11 -04002368 if (!mpd->io_submit.io_end->handle &&
2369 ext4_handle_valid(handle)) {
2370 mpd->io_submit.io_end->handle = handle->h_rsv_handle;
2371 handle->h_rsv_handle = NULL;
2372 }
Jan Kara3613d222013-06-04 13:19:34 -04002373 ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end);
Jan Kara6b523df2013-06-04 13:21:11 -04002374 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002375
2376 BUG_ON(map->m_len == 0);
Jan Kara4e7ea812013-06-04 13:17:40 -04002377 return 0;
2378}
2379
2380/*
2381 * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length
2382 * mpd->len and submit pages underlying it for IO
2383 *
2384 * @handle - handle for journal operations
2385 * @mpd - extent to map
Jan Kara7534e852013-10-16 08:26:08 -04002386 * @give_up_on_write - we set this to true iff there is a fatal error and there
2387 * is no hope of writing the data. The caller should discard
2388 * dirty pages to avoid infinite loops.
Jan Kara4e7ea812013-06-04 13:17:40 -04002389 *
2390 * The function maps extent starting at mpd->lblk of length mpd->len. If it is
2391 * delayed, blocks are allocated, if it is unwritten, we may need to convert
2392 * them to initialized or split the described range from larger unwritten
2393 * extent. Note that we need not map all the described range since allocation
2394 * can return less blocks or the range is covered by more unwritten extents. We
2395 * cannot map more because we are limited by reserved transaction credits. On
2396 * the other hand we always make sure that the last touched page is fully
2397 * mapped so that it can be written out (and thus forward progress is
2398 * guaranteed). After mapping we submit all mapped pages for IO.
2399 */
2400static int mpage_map_and_submit_extent(handle_t *handle,
Theodore Ts'ocb530542013-07-01 08:12:40 -04002401 struct mpage_da_data *mpd,
2402 bool *give_up_on_write)
Jan Kara4e7ea812013-06-04 13:17:40 -04002403{
2404 struct inode *inode = mpd->inode;
2405 struct ext4_map_blocks *map = &mpd->map;
2406 int err;
2407 loff_t disksize;
Dmitry Monakhov66031202014-08-27 18:40:03 -04002408 int progress = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302409 ext4_io_end_t *io_end = mpd->io_submit.io_end;
Ritesh Harjani4d06bfb2019-11-06 15:08:09 +05302410 struct ext4_io_end_vec *io_end_vec;
Jan Kara4e7ea812013-06-04 13:17:40 -04002411
Ritesh Harjani4d06bfb2019-11-06 15:08:09 +05302412 io_end_vec = ext4_alloc_io_end_vec(io_end);
2413 if (IS_ERR(io_end_vec))
2414 return PTR_ERR(io_end_vec);
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302415 io_end_vec->offset = ((loff_t)map->m_lblk) << inode->i_blkbits;
Jan Kara27d7c4e2013-07-05 21:57:22 -04002416 do {
Jan Kara4e7ea812013-06-04 13:17:40 -04002417 err = mpage_map_one_extent(handle, mpd);
2418 if (err < 0) {
2419 struct super_block *sb = inode->i_sb;
2420
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002421 if (ext4_forced_shutdown(EXT4_SB(sb)) ||
2422 EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)
Theodore Ts'ocb530542013-07-01 08:12:40 -04002423 goto invalidate_dirty_pages;
Jan Kara4e7ea812013-06-04 13:17:40 -04002424 /*
Theodore Ts'ocb530542013-07-01 08:12:40 -04002425 * Let the uper layers retry transient errors.
2426 * In the case of ENOSPC, if ext4_count_free_blocks()
2427 * is non-zero, a commit should free up blocks.
Jan Kara4e7ea812013-06-04 13:17:40 -04002428 */
Theodore Ts'ocb530542013-07-01 08:12:40 -04002429 if ((err == -ENOMEM) ||
Dmitry Monakhov66031202014-08-27 18:40:03 -04002430 (err == -ENOSPC && ext4_count_free_clusters(sb))) {
2431 if (progress)
2432 goto update_disksize;
Theodore Ts'ocb530542013-07-01 08:12:40 -04002433 return err;
Dmitry Monakhov66031202014-08-27 18:40:03 -04002434 }
Theodore Ts'ocb530542013-07-01 08:12:40 -04002435 ext4_msg(sb, KERN_CRIT,
2436 "Delayed block allocation failed for "
2437 "inode %lu at logical offset %llu with"
2438 " max blocks %u with error %d",
2439 inode->i_ino,
2440 (unsigned long long)map->m_lblk,
2441 (unsigned)map->m_len, -err);
2442 ext4_msg(sb, KERN_CRIT,
2443 "This should not happen!! Data will "
2444 "be lost\n");
2445 if (err == -ENOSPC)
2446 ext4_print_free_blocks(inode);
2447 invalidate_dirty_pages:
2448 *give_up_on_write = true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002449 return err;
2450 }
Dmitry Monakhov66031202014-08-27 18:40:03 -04002451 progress = 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04002452 /*
2453 * Update buffer state, submit mapped pages, and get us new
2454 * extent to map
2455 */
2456 err = mpage_map_and_submit_buffers(mpd);
2457 if (err < 0)
Dmitry Monakhov66031202014-08-27 18:40:03 -04002458 goto update_disksize;
Jan Kara27d7c4e2013-07-05 21:57:22 -04002459 } while (map->m_len);
Jan Kara4e7ea812013-06-04 13:17:40 -04002460
Dmitry Monakhov66031202014-08-27 18:40:03 -04002461update_disksize:
Theodore Ts'o622cad12014-04-11 10:35:17 -04002462 /*
2463 * Update on-disk size after IO is submitted. Races with
2464 * truncate are avoided by checking i_size under i_data_sem.
2465 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002466 disksize = ((loff_t)mpd->first_page) << PAGE_SHIFT;
Qian Cai35df4292020-02-07 09:29:11 -05002467 if (disksize > READ_ONCE(EXT4_I(inode)->i_disksize)) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002468 int err2;
Theodore Ts'o622cad12014-04-11 10:35:17 -04002469 loff_t i_size;
Jan Kara4e7ea812013-06-04 13:17:40 -04002470
Theodore Ts'o622cad12014-04-11 10:35:17 -04002471 down_write(&EXT4_I(inode)->i_data_sem);
2472 i_size = i_size_read(inode);
2473 if (disksize > i_size)
2474 disksize = i_size;
2475 if (disksize > EXT4_I(inode)->i_disksize)
2476 EXT4_I(inode)->i_disksize = disksize;
Theodore Ts'o622cad12014-04-11 10:35:17 -04002477 up_write(&EXT4_I(inode)->i_data_sem);
Theodore Ts'ob907f2d2017-01-11 22:14:49 -05002478 err2 = ext4_mark_inode_dirty(handle, inode);
Theodore Ts'o878520a2019-11-19 21:54:15 -05002479 if (err2) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04002480 ext4_error_err(inode->i_sb, -err2,
2481 "Failed to mark inode %lu dirty",
2482 inode->i_ino);
Theodore Ts'o878520a2019-11-19 21:54:15 -05002483 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002484 if (!err)
2485 err = err2;
2486 }
2487 return err;
2488}
2489
2490/*
Jan Karafffb2732013-06-04 13:01:11 -04002491 * Calculate the total number of credits to reserve for one writepages
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002492 * iteration. This is called from ext4_writepages(). We map an extent of
Anatol Pomozov70261f52013-08-28 14:40:12 -04002493 * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping
Jan Karafffb2732013-06-04 13:01:11 -04002494 * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN +
2495 * bpp - 1 blocks in bpp different extents.
2496 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002497static int ext4_da_writepages_trans_blocks(struct inode *inode)
2498{
Jan Karafffb2732013-06-04 13:01:11 -04002499 int bpp = ext4_journal_blocks_per_page(inode);
Mingming Cao525f4ed2008-08-19 22:15:58 -04002500
Jan Karafffb2732013-06-04 13:01:11 -04002501 return ext4_meta_trans_blocks(inode,
2502 MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp);
Mingming Cao525f4ed2008-08-19 22:15:58 -04002503}
Mingming Cao61628a32008-07-11 19:27:31 -04002504
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002505/*
Jan Kara4e7ea812013-06-04 13:17:40 -04002506 * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages
2507 * and underlying extent to map
2508 *
2509 * @mpd - where to look for pages
2510 *
2511 * Walk dirty pages in the mapping. If they are fully mapped, submit them for
2512 * IO immediately. When we find a page which isn't mapped we start accumulating
2513 * extent of buffers underlying these pages that needs mapping (formed by
2514 * either delayed or unwritten buffers). We also lock the pages containing
2515 * these buffers. The extent found is returned in @mpd structure (starting at
2516 * mpd->lblk with length mpd->len blocks).
2517 *
2518 * Note that this function can attach bios to one io_end structure which are
2519 * neither logically nor physically contiguous. Although it may seem as an
2520 * unnecessary complication, it is actually inevitable in blocksize < pagesize
2521 * 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 -04002522 */
Jan Kara4e7ea812013-06-04 13:17:40 -04002523static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd)
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002524{
Jan Kara4e7ea812013-06-04 13:17:40 -04002525 struct address_space *mapping = mpd->inode->i_mapping;
2526 struct pagevec pvec;
2527 unsigned int nr_pages;
Ming Leiaeac5892013-10-17 18:56:16 -04002528 long left = mpd->wbc->nr_to_write;
Jan Kara4e7ea812013-06-04 13:17:40 -04002529 pgoff_t index = mpd->first_page;
2530 pgoff_t end = mpd->last_page;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05002531 xa_mark_t tag;
Jan Kara4e7ea812013-06-04 13:17:40 -04002532 int i, err = 0;
2533 int blkbits = mpd->inode->i_blkbits;
2534 ext4_lblk_t lblk;
2535 struct buffer_head *head;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002536
Jan Kara4e7ea812013-06-04 13:17:40 -04002537 if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages)
Eric Sandeen5b41d922010-10-27 21:30:13 -04002538 tag = PAGECACHE_TAG_TOWRITE;
2539 else
2540 tag = PAGECACHE_TAG_DIRTY;
2541
Mel Gorman86679822017-11-15 17:37:52 -08002542 pagevec_init(&pvec);
Jan Kara4e7ea812013-06-04 13:17:40 -04002543 mpd->map.m_len = 0;
2544 mpd->next_page = index;
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002545 while (index <= end) {
Jan Karadc7f3e82017-11-15 17:34:44 -08002546 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08002547 tag);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002548 if (nr_pages == 0)
Jan Kara4e7ea812013-06-04 13:17:40 -04002549 goto out;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002550
2551 for (i = 0; i < nr_pages; i++) {
2552 struct page *page = pvec.pages[i];
2553
2554 /*
Ming Leiaeac5892013-10-17 18:56:16 -04002555 * Accumulated enough dirty pages? This doesn't apply
2556 * to WB_SYNC_ALL mode. For integrity sync we have to
2557 * keep going because someone may be concurrently
2558 * dirtying pages, and we might have synced a lot of
2559 * newly appeared dirty pages, but have not synced all
2560 * of the old dirty pages.
2561 */
2562 if (mpd->wbc->sync_mode == WB_SYNC_NONE && left <= 0)
2563 goto out;
2564
Jan Kara4e7ea812013-06-04 13:17:40 -04002565 /* If we can't merge this page, we are done. */
2566 if (mpd->map.m_len > 0 && mpd->next_page != page->index)
2567 goto out;
Theodore Ts'o78aaced2011-02-26 14:09:14 -05002568
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002569 lock_page(page);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002570 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002571 * If the page is no longer dirty, or its mapping no
2572 * longer corresponds to inode we are writing (which
2573 * means it has been truncated or invalidated), or the
2574 * page is already under writeback and we are not doing
2575 * a data integrity writeback, skip the page
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002576 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002577 if (!PageDirty(page) ||
2578 (PageWriteback(page) &&
Jan Kara4e7ea812013-06-04 13:17:40 -04002579 (mpd->wbc->sync_mode == WB_SYNC_NONE)) ||
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002580 unlikely(page->mapping != mapping)) {
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002581 unlock_page(page);
2582 continue;
2583 }
2584
Darrick J. Wong7cb1a532011-05-18 13:53:20 -04002585 wait_on_page_writeback(page);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002586 BUG_ON(PageWriteback(page));
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002587
Jan Kara4e7ea812013-06-04 13:17:40 -04002588 if (mpd->map.m_len == 0)
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002589 mpd->first_page = page->index;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002590 mpd->next_page = page->index + 1;
Jan Karaf8bec372013-01-28 12:55:08 -05002591 /* Add all dirty buffers to mpd */
Jan Kara4e7ea812013-06-04 13:17:40 -04002592 lblk = ((ext4_lblk_t)page->index) <<
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002593 (PAGE_SHIFT - blkbits);
Jan Karaf8bec372013-01-28 12:55:08 -05002594 head = page_buffers(page);
Jan Kara5f1132b2013-08-17 10:02:33 -04002595 err = mpage_process_page_bufs(mpd, head, head, lblk);
2596 if (err <= 0)
Jan Kara4e7ea812013-06-04 13:17:40 -04002597 goto out;
Jan Kara5f1132b2013-08-17 10:02:33 -04002598 err = 0;
Ming Leiaeac5892013-10-17 18:56:16 -04002599 left--;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002600 }
2601 pagevec_release(&pvec);
2602 cond_resched();
2603 }
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002604 return 0;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002605out:
2606 pagevec_release(&pvec);
Jan Kara4e7ea812013-06-04 13:17:40 -04002607 return err;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002608}
2609
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002610static int ext4_writepages(struct address_space *mapping,
2611 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002612{
Jan Kara4e7ea812013-06-04 13:17:40 -04002613 pgoff_t writeback_index = 0;
2614 long nr_to_write = wbc->nr_to_write;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002615 int range_whole = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002616 int cycled = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002617 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002618 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002619 struct inode *inode = mapping->host;
Jan Kara6b523df2013-06-04 13:21:11 -04002620 int needed_blocks, rsv_blocks = 0, ret = 0;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002621 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Jan Kara4e7ea812013-06-04 13:17:40 -04002622 bool done;
Shaohua Li1bce63d12011-10-18 10:55:51 -04002623 struct blk_plug plug;
Theodore Ts'ocb530542013-07-01 08:12:40 -04002624 bool give_up_on_write = false;
Mingming Cao61628a32008-07-11 19:27:31 -04002625
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002626 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
2627 return -EIO;
2628
Eric Biggersbbd55932020-02-19 10:30:46 -08002629 percpu_down_read(&sbi->s_writepages_rwsem);
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002630 trace_ext4_writepages(inode, wbc);
Theodore Ts'oba80b102009-01-03 20:03:21 -05002631
Mingming Cao61628a32008-07-11 19:27:31 -04002632 /*
2633 * No pages to write? This is mainly a kludge to avoid starting
2634 * a transaction for special inodes like journal inode on last iput()
2635 * because that could violate lock ordering on umount
2636 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002637 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Ming Leibbf023c72013-10-30 07:27:16 -04002638 goto out_writepages;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002639
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002640 if (ext4_should_journal_data(inode)) {
Goldwyn Rodrigues043d20d2018-03-26 01:32:50 -04002641 ret = generic_writepages(mapping, wbc);
Ming Leibbf023c72013-10-30 07:27:16 -04002642 goto out_writepages;
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002643 }
2644
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002645 /*
2646 * If the filesystem has aborted, it is read-only, so return
2647 * right away instead of dumping stack traces later on that
2648 * will obscure the real source of the problem. We test
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002649 * EXT4_MF_FS_ABORTED instead of sb->s_flag's SB_RDONLY because
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002650 * the latter could be true if the filesystem is mounted
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002651 * read-only, and in that case, ext4_writepages should
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002652 * *never* be called, so if that ever happens, we would want
2653 * the stack trace.
2654 */
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002655 if (unlikely(ext4_forced_shutdown(EXT4_SB(mapping->host->i_sb)) ||
2656 sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) {
Ming Leibbf023c72013-10-30 07:27:16 -04002657 ret = -EROFS;
2658 goto out_writepages;
2659 }
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002660
Jan Kara4e7ea812013-06-04 13:17:40 -04002661 /*
2662 * If we have inline data and arrive here, it means that
2663 * we will soon create the block for the 1st page, so
2664 * we'd better clear the inline data here.
2665 */
2666 if (ext4_has_inline_data(inode)) {
2667 /* Just inode will be modified... */
2668 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
2669 if (IS_ERR(handle)) {
2670 ret = PTR_ERR(handle);
2671 goto out_writepages;
2672 }
2673 BUG_ON(ext4_test_inode_state(inode,
2674 EXT4_STATE_MAY_INLINE_DATA));
2675 ext4_destroy_inline_data(handle, inode);
2676 ext4_journal_stop(handle);
2677 }
2678
yangerkun4e343232019-08-11 16:27:41 -04002679 if (ext4_should_dioread_nolock(inode)) {
2680 /*
2681 * We may need to convert up to one extent per block in
2682 * the page and we may dirty the inode.
2683 */
2684 rsv_blocks = 1 + ext4_chunk_trans_blocks(inode,
2685 PAGE_SIZE >> inode->i_blkbits);
2686 }
2687
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002688 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2689 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002690
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002691 if (wbc->range_cyclic) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002692 writeback_index = mapping->writeback_index;
2693 if (writeback_index)
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002694 cycled = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002695 mpd.first_page = writeback_index;
2696 mpd.last_page = -1;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002697 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002698 mpd.first_page = wbc->range_start >> PAGE_SHIFT;
2699 mpd.last_page = wbc->range_end >> PAGE_SHIFT;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002700 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002701
Jan Kara4e7ea812013-06-04 13:17:40 -04002702 mpd.inode = inode;
2703 mpd.wbc = wbc;
2704 ext4_io_submit_init(&mpd.io_submit, wbc);
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002705retry:
Wu Fengguang6e6938b2010-06-06 10:38:15 -06002706 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Jan Kara4e7ea812013-06-04 13:17:40 -04002707 tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page);
2708 done = false;
Shaohua Li1bce63d12011-10-18 10:55:51 -04002709 blk_start_plug(&plug);
Jan Karadddbd6a2017-04-30 18:29:10 -04002710
2711 /*
2712 * First writeback pages that don't need mapping - we can avoid
2713 * starting a transaction unnecessarily and also avoid being blocked
2714 * in the block layer on device congestion while having transaction
2715 * started.
2716 */
2717 mpd.do_map = 0;
2718 mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
2719 if (!mpd.io_submit.io_end) {
2720 ret = -ENOMEM;
2721 goto unplug;
2722 }
2723 ret = mpage_prepare_extent_to_map(&mpd);
Xiaoguang Wanga297b2f2019-02-10 23:53:21 -05002724 /* Unlock pages we didn't use */
2725 mpage_release_unused_pages(&mpd, false);
Jan Karadddbd6a2017-04-30 18:29:10 -04002726 /* Submit prepared bio */
2727 ext4_io_submit(&mpd.io_submit);
2728 ext4_put_io_end_defer(mpd.io_submit.io_end);
2729 mpd.io_submit.io_end = NULL;
Jan Karadddbd6a2017-04-30 18:29:10 -04002730 if (ret < 0)
2731 goto unplug;
2732
Jan Kara4e7ea812013-06-04 13:17:40 -04002733 while (!done && mpd.first_page <= mpd.last_page) {
2734 /* For each extent of pages we use new io_end */
2735 mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
2736 if (!mpd.io_submit.io_end) {
2737 ret = -ENOMEM;
2738 break;
2739 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002740
2741 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002742 * We have two constraints: We find one extent to map and we
2743 * must always write out whole page (makes a difference when
2744 * blocksize < pagesize) so that we don't block on IO when we
2745 * try to write out the rest of the page. Journalled mode is
2746 * not supported by delalloc.
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002747 */
2748 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04002749 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002750
Jan Kara4e7ea812013-06-04 13:17:40 -04002751 /* start a new transaction */
Jan Kara6b523df2013-06-04 13:21:11 -04002752 handle = ext4_journal_start_with_reserve(inode,
2753 EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks);
Mingming Cao61628a32008-07-11 19:27:31 -04002754 if (IS_ERR(handle)) {
2755 ret = PTR_ERR(handle);
Theodore Ts'o16939182009-09-26 17:43:59 -04002756 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
Curt Wohlgemuthfbe845d2010-05-16 13:00:00 -04002757 "%ld pages, ino %lu; err %d", __func__,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002758 wbc->nr_to_write, inode->i_ino, ret);
Jan Kara4e7ea812013-06-04 13:17:40 -04002759 /* Release allocated io_end */
2760 ext4_put_io_end(mpd.io_submit.io_end);
Jan Karadddbd6a2017-04-30 18:29:10 -04002761 mpd.io_submit.io_end = NULL;
Jan Kara4e7ea812013-06-04 13:17:40 -04002762 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002763 }
Jan Karadddbd6a2017-04-30 18:29:10 -04002764 mpd.do_map = 1;
Theodore Ts'of63e60052009-02-23 16:42:39 -05002765
Jan Kara4e7ea812013-06-04 13:17:40 -04002766 trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc);
2767 ret = mpage_prepare_extent_to_map(&mpd);
2768 if (!ret) {
2769 if (mpd.map.m_len)
Theodore Ts'ocb530542013-07-01 08:12:40 -04002770 ret = mpage_map_and_submit_extent(handle, &mpd,
2771 &give_up_on_write);
Jan Kara4e7ea812013-06-04 13:17:40 -04002772 else {
2773 /*
2774 * We scanned the whole range (or exhausted
2775 * nr_to_write), submitted what was mapped and
2776 * didn't find anything needing mapping. We are
2777 * done.
2778 */
2779 done = true;
2780 }
Theodore Ts'of63e60052009-02-23 16:42:39 -05002781 }
Jan Kara646caa92016-07-04 10:14:01 -04002782 /*
2783 * Caution: If the handle is synchronous,
2784 * ext4_journal_stop() can wait for transaction commit
2785 * to finish which may depend on writeback of pages to
2786 * complete or on page lock to be released. In that
2787 * case, we have to wait until after after we have
2788 * submitted all the IO, released page locks we hold,
2789 * and dropped io_end reference (for extent conversion
2790 * to be able to complete) before stopping the handle.
2791 */
2792 if (!ext4_handle_valid(handle) || handle->h_sync == 0) {
2793 ext4_journal_stop(handle);
2794 handle = NULL;
Jan Karadddbd6a2017-04-30 18:29:10 -04002795 mpd.do_map = 0;
Jan Kara646caa92016-07-04 10:14:01 -04002796 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002797 /* Unlock pages we didn't use */
Theodore Ts'ocb530542013-07-01 08:12:40 -04002798 mpage_release_unused_pages(&mpd, give_up_on_write);
Xiaoguang Wanga297b2f2019-02-10 23:53:21 -05002799 /* Submit prepared bio */
2800 ext4_io_submit(&mpd.io_submit);
2801
Jan Kara646caa92016-07-04 10:14:01 -04002802 /*
2803 * Drop our io_end reference we got from init. We have
2804 * to be careful and use deferred io_end finishing if
2805 * we are still holding the transaction as we can
2806 * release the last reference to io_end which may end
2807 * up doing unwritten extent conversion.
2808 */
2809 if (handle) {
2810 ext4_put_io_end_defer(mpd.io_submit.io_end);
2811 ext4_journal_stop(handle);
2812 } else
2813 ext4_put_io_end(mpd.io_submit.io_end);
Jan Karadddbd6a2017-04-30 18:29:10 -04002814 mpd.io_submit.io_end = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002815
Jan Kara4e7ea812013-06-04 13:17:40 -04002816 if (ret == -ENOSPC && sbi->s_journal) {
2817 /*
2818 * Commit the transaction which would
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002819 * free blocks released in the transaction
2820 * and try again
2821 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002822 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002823 ret = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002824 continue;
2825 }
2826 /* Fatal error - ENOMEM, EIO... */
2827 if (ret)
Mingming Cao61628a32008-07-11 19:27:31 -04002828 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002829 }
Jan Karadddbd6a2017-04-30 18:29:10 -04002830unplug:
Shaohua Li1bce63d12011-10-18 10:55:51 -04002831 blk_finish_plug(&plug);
Jan Kara9c12a832013-09-16 08:24:26 -04002832 if (!ret && !cycled && wbc->nr_to_write > 0) {
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002833 cycled = 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04002834 mpd.last_page = writeback_index - 1;
2835 mpd.first_page = 0;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002836 goto retry;
2837 }
Mingming Cao61628a32008-07-11 19:27:31 -04002838
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002839 /* Update index */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002840 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2841 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002842 * Set the writeback_index so that range_cyclic
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002843 * mode will write it back later
2844 */
Jan Kara4e7ea812013-06-04 13:17:40 -04002845 mapping->writeback_index = mpd.first_page;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002846
Mingming Cao61628a32008-07-11 19:27:31 -04002847out_writepages:
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002848 trace_ext4_writepages_result(inode, wbc, ret,
2849 nr_to_write - wbc->nr_to_write);
Eric Biggersbbd55932020-02-19 10:30:46 -08002850 percpu_up_read(&sbi->s_writepages_rwsem);
Mingming Cao61628a32008-07-11 19:27:31 -04002851 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002852}
2853
Dan Williams5f0663b2017-12-21 12:25:11 -08002854static int ext4_dax_writepages(struct address_space *mapping,
2855 struct writeback_control *wbc)
2856{
2857 int ret;
2858 long nr_to_write = wbc->nr_to_write;
2859 struct inode *inode = mapping->host;
2860 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
2861
2862 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
2863 return -EIO;
2864
Eric Biggersbbd55932020-02-19 10:30:46 -08002865 percpu_down_read(&sbi->s_writepages_rwsem);
Dan Williams5f0663b2017-12-21 12:25:11 -08002866 trace_ext4_writepages(inode, wbc);
2867
Vivek Goyal3f666c52020-01-03 13:33:07 -05002868 ret = dax_writeback_mapping_range(mapping, sbi->s_daxdev, wbc);
Dan Williams5f0663b2017-12-21 12:25:11 -08002869 trace_ext4_writepages_result(inode, wbc, ret,
2870 nr_to_write - wbc->nr_to_write);
Eric Biggersbbd55932020-02-19 10:30:46 -08002871 percpu_up_read(&sbi->s_writepages_rwsem);
Dan Williams5f0663b2017-12-21 12:25:11 -08002872 return ret;
2873}
2874
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002875static int ext4_nonda_switch(struct super_block *sb)
2876{
Eric Whitney5c1ff332013-04-09 09:27:31 -04002877 s64 free_clusters, dirty_clusters;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002878 struct ext4_sb_info *sbi = EXT4_SB(sb);
2879
2880 /*
2881 * switch to non delalloc mode if we are running low
2882 * on free block. The free block accounting via percpu
Eric Dumazet179f7eb2009-01-06 14:41:04 -08002883 * counters can get slightly wrong with percpu_counter_batch getting
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002884 * accumulated on each CPU without updating global counters
2885 * Delalloc need an accurate free block accounting. So switch
2886 * to non delalloc when we are near to error range.
2887 */
Eric Whitney5c1ff332013-04-09 09:27:31 -04002888 free_clusters =
2889 percpu_counter_read_positive(&sbi->s_freeclusters_counter);
2890 dirty_clusters =
2891 percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
Theodore Ts'o00d4e732012-09-19 22:42:36 -04002892 /*
2893 * Start pushing delalloc when 1/2 of free blocks are dirty.
2894 */
Eric Whitney5c1ff332013-04-09 09:27:31 -04002895 if (dirty_clusters && (free_clusters < 2 * dirty_clusters))
Miao Xie10ee27a2013-01-10 13:47:57 +08002896 try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE);
Theodore Ts'o00d4e732012-09-19 22:42:36 -04002897
Eric Whitney5c1ff332013-04-09 09:27:31 -04002898 if (2 * free_clusters < 3 * dirty_clusters ||
2899 free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) {
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002900 /*
Eric Sandeenc8afb442009-12-23 07:58:12 -05002901 * free block count is less than 150% of dirty blocks
2902 * or free blocks is less than watermark
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002903 */
2904 return 1;
2905 }
2906 return 0;
2907}
2908
Eric Sandeen0ff89472014-10-11 19:51:17 -04002909/* We always reserve for an inode update; the superblock could be there too */
2910static int ext4_da_write_credits(struct inode *inode, loff_t pos, unsigned len)
2911{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002912 if (likely(ext4_has_feature_large_file(inode->i_sb)))
Eric Sandeen0ff89472014-10-11 19:51:17 -04002913 return 1;
2914
2915 if (pos + len <= 0x7fffffffULL)
2916 return 1;
2917
2918 /* We might need to update the superblock to set LARGE_FILE */
2919 return 2;
2920}
2921
Alex Tomas64769242008-07-11 19:27:31 -04002922static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002923 loff_t pos, unsigned len, unsigned flags,
2924 struct page **pagep, void **fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04002925{
Eric Sandeen72b8ab92010-05-16 11:00:00 -04002926 int ret, retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002927 struct page *page;
2928 pgoff_t index;
Alex Tomas64769242008-07-11 19:27:31 -04002929 struct inode *inode = mapping->host;
2930 handle_t *handle;
2931
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002932 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
2933 return -EIO;
2934
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002935 index = pos >> PAGE_SHIFT;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002936
Eric Biggersc93d8f82019-07-22 09:26:24 -07002937 if (ext4_nonda_switch(inode->i_sb) || S_ISLNK(inode->i_mode) ||
2938 ext4_verity_in_progress(inode)) {
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002939 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
2940 return ext4_write_begin(file, mapping, pos,
2941 len, flags, pagep, fsdata);
2942 }
2943 *fsdata = (void *)0;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002944 trace_ext4_da_write_begin(inode, pos, len, flags);
Tao Ma9c3569b2012-12-10 14:05:57 -05002945
2946 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2947 ret = ext4_da_write_inline_data_begin(mapping, inode,
2948 pos, len, flags,
2949 pagep, fsdata);
2950 if (ret < 0)
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002951 return ret;
2952 if (ret == 1)
2953 return 0;
Tao Ma9c3569b2012-12-10 14:05:57 -05002954 }
2955
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002956 /*
2957 * grab_cache_page_write_begin() can take a long time if the
2958 * system is thrashing due to memory pressure, or if the page
2959 * is being written back. So grab it first before we start
2960 * the transaction handle. This also allows us to allocate
2961 * the page (if needed) without using GFP_NOFS.
2962 */
2963retry_grab:
2964 page = grab_cache_page_write_begin(mapping, index, flags);
2965 if (!page)
2966 return -ENOMEM;
2967 unlock_page(page);
2968
Alex Tomas64769242008-07-11 19:27:31 -04002969 /*
2970 * With delayed allocation, we don't log the i_disksize update
2971 * if there is delayed block allocation. But we still need
2972 * to journalling the i_disksize update if writes to the end
2973 * of file which has an already mapped buffer.
2974 */
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002975retry_journal:
Eric Sandeen0ff89472014-10-11 19:51:17 -04002976 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
2977 ext4_da_write_credits(inode, pos, len));
Alex Tomas64769242008-07-11 19:27:31 -04002978 if (IS_ERR(handle)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002979 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002980 return PTR_ERR(handle);
Alex Tomas64769242008-07-11 19:27:31 -04002981 }
2982
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002983 lock_page(page);
2984 if (page->mapping != mapping) {
2985 /* The page got truncated from under us */
2986 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002987 put_page(page);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002988 ext4_journal_stop(handle);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002989 goto retry_grab;
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002990 }
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002991 /* In case writeback began while the page was unlocked */
Dmitry Monakhov7afe5aa2013-08-28 14:30:47 -04002992 wait_for_stable_page(page);
Alex Tomas64769242008-07-11 19:27:31 -04002993
Chandan Rajendra643fa962018-12-12 15:20:12 +05302994#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrow2058f832015-04-12 00:55:10 -04002995 ret = ext4_block_write_begin(page, pos, len,
2996 ext4_da_get_block_prep);
2997#else
Christoph Hellwig6e1db882010-06-04 11:29:57 +02002998 ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
Michael Halcrow2058f832015-04-12 00:55:10 -04002999#endif
Alex Tomas64769242008-07-11 19:27:31 -04003000 if (ret < 0) {
3001 unlock_page(page);
3002 ext4_journal_stop(handle);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04003003 /*
3004 * block_write_begin may have instantiated a few blocks
3005 * outside i_size. Trim these off again. Don't need
3006 * i_size_read because we hold i_mutex.
3007 */
3008 if (pos + len > inode->i_size)
Jan Karab9a42072009-12-08 21:24:33 -05003009 ext4_truncate_failed_write(inode);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05003010
3011 if (ret == -ENOSPC &&
3012 ext4_should_retry_alloc(inode->i_sb, &retries))
3013 goto retry_journal;
3014
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003015 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05003016 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04003017 }
3018
Theodore Ts'o47564bf2013-02-09 09:24:14 -05003019 *pagep = page;
Alex Tomas64769242008-07-11 19:27:31 -04003020 return ret;
3021}
3022
Mingming Cao632eaea2008-07-11 19:27:31 -04003023/*
3024 * Check if we should update i_disksize
3025 * when write to the end of file but not require block allocation
3026 */
3027static int ext4_da_should_update_i_disksize(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003028 unsigned long offset)
Mingming Cao632eaea2008-07-11 19:27:31 -04003029{
3030 struct buffer_head *bh;
3031 struct inode *inode = page->mapping->host;
3032 unsigned int idx;
3033 int i;
3034
3035 bh = page_buffers(page);
3036 idx = offset >> inode->i_blkbits;
3037
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003038 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04003039 bh = bh->b_this_page;
3040
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04003041 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
Mingming Cao632eaea2008-07-11 19:27:31 -04003042 return 0;
3043 return 1;
3044}
3045
Alex Tomas64769242008-07-11 19:27:31 -04003046static int ext4_da_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003047 struct address_space *mapping,
3048 loff_t pos, unsigned len, unsigned copied,
3049 struct page *page, void *fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04003050{
3051 struct inode *inode = mapping->host;
3052 int ret = 0, ret2;
3053 handle_t *handle = ext4_journal_current_handle();
3054 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04003055 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003056 int write_mode = (int)(unsigned long)fsdata;
3057
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003058 if (write_mode == FALL_BACK_TO_NONDELALLOC)
3059 return ext4_write_end(file, mapping, pos,
3060 len, copied, page, fsdata);
Mingming Cao632eaea2008-07-11 19:27:31 -04003061
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003062 trace_ext4_da_write_end(inode, pos, len, copied);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003063 start = pos & (PAGE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003064 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04003065
3066 /*
3067 * generic_write_end() will run mark_inode_dirty() if i_size
3068 * changes. So let's piggyback the i_disksize mark_inode_dirty
3069 * into that.
3070 */
Alex Tomas64769242008-07-11 19:27:31 -04003071 new_i_size = pos + copied;
Andrea Arcangeliea51d132011-12-13 21:41:15 -05003072 if (copied && new_i_size > EXT4_I(inode)->i_disksize) {
Tao Ma9c3569b2012-12-10 14:05:57 -05003073 if (ext4_has_inline_data(inode) ||
3074 ext4_da_should_update_i_disksize(page, end)) {
Dmitry Monakhovee124d22014-08-30 23:34:06 -04003075 ext4_update_i_disksize(inode, new_i_size);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003076 /* We need to mark inode dirty even if
3077 * new_i_size is less that inode->i_size
3078 * bu greater than i_disksize.(hint delalloc)
3079 */
3080 ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04003081 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003082 }
Tao Ma9c3569b2012-12-10 14:05:57 -05003083
3084 if (write_mode != CONVERT_INLINE_DATA &&
3085 ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
3086 ext4_has_inline_data(inode))
3087 ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied,
3088 page);
3089 else
3090 ret2 = generic_write_end(file, mapping, pos, len, copied,
Alex Tomas64769242008-07-11 19:27:31 -04003091 page, fsdata);
Tao Ma9c3569b2012-12-10 14:05:57 -05003092
Alex Tomas64769242008-07-11 19:27:31 -04003093 copied = ret2;
3094 if (ret2 < 0)
3095 ret = ret2;
3096 ret2 = ext4_journal_stop(handle);
3097 if (!ret)
3098 ret = ret2;
3099
3100 return ret ? ret : copied;
3101}
3102
Theodore Ts'occd25062009-02-26 01:04:07 -05003103/*
3104 * Force all delayed allocation blocks to be allocated for a given inode.
3105 */
3106int ext4_alloc_da_blocks(struct inode *inode)
3107{
Theodore Ts'ofb40ba02009-09-16 19:30:40 -04003108 trace_ext4_alloc_da_blocks(inode);
3109
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04003110 if (!EXT4_I(inode)->i_reserved_data_blocks)
Theodore Ts'occd25062009-02-26 01:04:07 -05003111 return 0;
3112
3113 /*
3114 * We do something simple for now. The filemap_flush() will
3115 * also start triggering a write of the data blocks, which is
3116 * not strictly speaking necessary (and for users of
3117 * laptop_mode, not even desirable). However, to do otherwise
3118 * would require replicating code paths in:
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003119 *
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003120 * ext4_writepages() ->
Theodore Ts'occd25062009-02-26 01:04:07 -05003121 * write_cache_pages() ---> (via passed in callback function)
3122 * __mpage_da_writepage() -->
3123 * mpage_add_bh_to_extent()
3124 * mpage_da_map_blocks()
3125 *
3126 * The problem is that write_cache_pages(), located in
3127 * mm/page-writeback.c, marks pages clean in preparation for
3128 * doing I/O, which is not desirable if we're not planning on
3129 * doing I/O at all.
3130 *
3131 * We could call write_cache_pages(), and then redirty all of
Wu Fengguang380cf092010-11-11 19:23:29 +08003132 * the pages by calling redirty_page_for_writepage() but that
Theodore Ts'occd25062009-02-26 01:04:07 -05003133 * would be ugly in the extreme. So instead we would need to
3134 * replicate parts of the code in the above functions,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003135 * simplifying them because we wouldn't actually intend to
Theodore Ts'occd25062009-02-26 01:04:07 -05003136 * write out the pages, but rather only collect contiguous
3137 * logical block extents, call the multi-block allocator, and
3138 * then update the buffer heads with the block allocations.
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003139 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003140 * For now, though, we'll cheat by calling filemap_flush(),
3141 * which will map the blocks, and start the I/O, but not
3142 * actually wait for the I/O to complete.
3143 */
3144 return filemap_flush(inode->i_mapping);
3145}
Alex Tomas64769242008-07-11 19:27:31 -04003146
3147/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003148 * bmap() is special. It gets used by applications such as lilo and by
3149 * the swapper to find the on-disk block of a specific piece of data.
3150 *
3151 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07003152 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003153 * filesystem and enables swap, then they may get a nasty shock when the
3154 * data getting swapped to that swapfile suddenly gets overwritten by
3155 * the original zero's written out previously to the journal and
3156 * awaiting writeback in the kernel's buffer cache.
3157 *
3158 * So, if we see any bmap calls here on a modified, data-journaled file,
3159 * take extra steps to flush any blocks which might be in the cache.
3160 */
Mingming Cao617ba132006-10-11 01:20:53 -07003161static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003162{
3163 struct inode *inode = mapping->host;
3164 journal_t *journal;
3165 int err;
3166
Tao Ma46c7f252012-12-10 14:04:52 -05003167 /*
3168 * We can get here for an inline file via the FIBMAP ioctl
3169 */
3170 if (ext4_has_inline_data(inode))
3171 return 0;
3172
Alex Tomas64769242008-07-11 19:27:31 -04003173 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
3174 test_opt(inode->i_sb, DELALLOC)) {
3175 /*
3176 * With delalloc we want to sync the file
3177 * so that we can make sure we allocate
3178 * blocks for file
3179 */
3180 filemap_write_and_wait(mapping);
3181 }
3182
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003183 if (EXT4_JOURNAL(inode) &&
3184 ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003185 /*
3186 * This is a REALLY heavyweight approach, but the use of
3187 * bmap on dirty files is expected to be extremely rare:
3188 * only if we run lilo or swapon on a freshly made file
3189 * do we expect this to happen.
3190 *
3191 * (bmap requires CAP_SYS_RAWIO so this does not
3192 * represent an unprivileged user DOS attack --- we'd be
3193 * in trouble if mortal users could trigger this path at
3194 * will.)
3195 *
Mingming Cao617ba132006-10-11 01:20:53 -07003196 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003197 * regular files. If somebody wants to bmap a directory
3198 * or symlink and gets confused because the buffer
3199 * hasn't yet been flushed to disk, they deserve
3200 * everything they get.
3201 */
3202
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003203 ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
Mingming Cao617ba132006-10-11 01:20:53 -07003204 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07003205 jbd2_journal_lock_updates(journal);
3206 err = jbd2_journal_flush(journal);
3207 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003208
3209 if (err)
3210 return 0;
3211 }
3212
Ritesh Harjaniac58e4f2020-02-28 14:56:56 +05303213 return iomap_bmap(mapping, block, &ext4_iomap_ops);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003214}
3215
Mingming Cao617ba132006-10-11 01:20:53 -07003216static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003217{
Tao Ma46c7f252012-12-10 14:04:52 -05003218 int ret = -EAGAIN;
3219 struct inode *inode = page->mapping->host;
3220
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003221 trace_ext4_readpage(page);
Tao Ma46c7f252012-12-10 14:04:52 -05003222
3223 if (ext4_has_inline_data(inode))
3224 ret = ext4_readpage_inline(inode, page);
3225
3226 if (ret == -EAGAIN)
Jens Axboeac22b462018-08-17 15:45:42 -07003227 return ext4_mpage_readpages(page->mapping, NULL, page, 1,
3228 false);
Tao Ma46c7f252012-12-10 14:04:52 -05003229
3230 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003231}
3232
3233static int
Mingming Cao617ba132006-10-11 01:20:53 -07003234ext4_readpages(struct file *file, struct address_space *mapping,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003235 struct list_head *pages, unsigned nr_pages)
3236{
Tao Ma46c7f252012-12-10 14:04:52 -05003237 struct inode *inode = mapping->host;
3238
3239 /* If the file has inline data, no need to do readpages. */
3240 if (ext4_has_inline_data(inode))
3241 return 0;
3242
Jens Axboeac22b462018-08-17 15:45:42 -07003243 return ext4_mpage_readpages(mapping, pages, NULL, nr_pages, true);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003244}
3245
Lukas Czernerd47992f2013-05-21 23:17:23 -04003246static void ext4_invalidatepage(struct page *page, unsigned int offset,
3247 unsigned int length)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003248{
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003249 trace_ext4_invalidatepage(page, offset, length);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003250
Jan Kara4520fb32012-12-25 13:28:54 -05003251 /* No journalling happens on data buffers when this function is used */
3252 WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page)));
3253
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003254 block_invalidatepage(page, offset, length);
Jan Kara4520fb32012-12-25 13:28:54 -05003255}
3256
Jan Kara53e87262012-12-25 13:29:52 -05003257static int __ext4_journalled_invalidatepage(struct page *page,
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003258 unsigned int offset,
3259 unsigned int length)
Jan Kara4520fb32012-12-25 13:28:54 -05003260{
3261 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
3262
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003263 trace_ext4_journalled_invalidatepage(page, offset, length);
Jan Kara4520fb32012-12-25 13:28:54 -05003264
Jiaying Zhang744692d2010-03-04 16:14:02 -05003265 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003266 * If it's a full truncate we just forget about the pending dirtying
3267 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003268 if (offset == 0 && length == PAGE_SIZE)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003269 ClearPageChecked(page);
3270
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003271 return jbd2_journal_invalidatepage(journal, page, offset, length);
Jan Kara53e87262012-12-25 13:29:52 -05003272}
3273
3274/* Wrapper for aops... */
3275static void ext4_journalled_invalidatepage(struct page *page,
Lukas Czernerd47992f2013-05-21 23:17:23 -04003276 unsigned int offset,
3277 unsigned int length)
Jan Kara53e87262012-12-25 13:29:52 -05003278{
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003279 WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003280}
3281
Mingming Cao617ba132006-10-11 01:20:53 -07003282static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003283{
Mingming Cao617ba132006-10-11 01:20:53 -07003284 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003285
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003286 trace_ext4_releasepage(page);
3287
Jan Karae1c36592013-03-10 22:19:00 -04003288 /* Page has dirty journalled data -> cannot release */
3289 if (PageChecked(page))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003290 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05003291 if (journal)
3292 return jbd2_journal_try_to_free_buffers(journal, page, wait);
3293 else
3294 return try_to_free_buffers(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003295}
3296
Jan Karab8a61762017-11-01 16:36:45 +01003297static bool ext4_inode_datasync_dirty(struct inode *inode)
3298{
3299 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
3300
3301 if (journal)
3302 return !jbd2_transaction_committed(journal,
3303 EXT4_I(inode)->i_datasync_tid);
3304 /* Any metadata buffers to write? */
3305 if (!list_empty(&inode->i_mapping->private_list))
3306 return true;
3307 return inode->i_state & I_DIRTY_DATASYNC;
3308}
3309
Matthew Bobrowskic8fdfe292019-11-05 22:59:56 +11003310static void ext4_set_iomap(struct inode *inode, struct iomap *iomap,
3311 struct ext4_map_blocks *map, loff_t offset,
3312 loff_t length)
Jan Kara364443c2016-11-20 17:36:06 -05003313{
Matthew Bobrowskic8fdfe292019-11-05 22:59:56 +11003314 u8 blkbits = inode->i_blkbits;
3315
3316 /*
3317 * Writes that span EOF might trigger an I/O size update on completion,
3318 * so consider them to be dirty for the purpose of O_DSYNC, even if
3319 * there is no other metadata changes being made or are pending.
3320 */
3321 iomap->flags = 0;
3322 if (ext4_inode_datasync_dirty(inode) ||
3323 offset + length > i_size_read(inode))
3324 iomap->flags |= IOMAP_F_DIRTY;
3325
3326 if (map->m_flags & EXT4_MAP_NEW)
3327 iomap->flags |= IOMAP_F_NEW;
3328
3329 iomap->bdev = inode->i_sb->s_bdev;
3330 iomap->dax_dev = EXT4_SB(inode->i_sb)->s_daxdev;
3331 iomap->offset = (u64) map->m_lblk << blkbits;
3332 iomap->length = (u64) map->m_len << blkbits;
3333
Ritesh Harjani63867222020-02-28 14:56:54 +05303334 if ((map->m_flags & EXT4_MAP_MAPPED) &&
3335 !ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3336 iomap->flags |= IOMAP_F_MERGED;
3337
Matthew Bobrowskic8fdfe292019-11-05 22:59:56 +11003338 /*
3339 * Flags passed to ext4_map_blocks() for direct I/O writes can result
3340 * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits
3341 * set. In order for any allocated unwritten extents to be converted
3342 * into written extents correctly within the ->end_io() handler, we
3343 * need to ensure that the iomap->type is set appropriately. Hence, the
3344 * reason why we need to check whether the EXT4_MAP_UNWRITTEN bit has
3345 * been set first.
3346 */
3347 if (map->m_flags & EXT4_MAP_UNWRITTEN) {
3348 iomap->type = IOMAP_UNWRITTEN;
3349 iomap->addr = (u64) map->m_pblk << blkbits;
3350 } else if (map->m_flags & EXT4_MAP_MAPPED) {
3351 iomap->type = IOMAP_MAPPED;
3352 iomap->addr = (u64) map->m_pblk << blkbits;
3353 } else {
3354 iomap->type = IOMAP_HOLE;
3355 iomap->addr = IOMAP_NULL_ADDR;
3356 }
3357}
3358
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003359static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map,
3360 unsigned int flags)
3361{
3362 handle_t *handle;
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003363 u8 blkbits = inode->i_blkbits;
3364 int ret, dio_credits, m_flags = 0, retries = 0;
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003365
3366 /*
3367 * Trim the mapping request to the maximum value that we can map at
3368 * once for direct I/O.
3369 */
3370 if (map->m_len > DIO_MAX_BLOCKS)
3371 map->m_len = DIO_MAX_BLOCKS;
3372 dio_credits = ext4_chunk_trans_blocks(inode, map->m_len);
3373
3374retry:
3375 /*
3376 * Either we allocate blocks and then don't get an unwritten extent, so
3377 * in that case we have reserved enough credits. Or, the blocks are
3378 * already allocated and unwritten. In that case, the extent conversion
3379 * fits into the credits as well.
3380 */
3381 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits);
3382 if (IS_ERR(handle))
3383 return PTR_ERR(handle);
3384
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003385 /*
3386 * DAX and direct I/O are the only two operations that are currently
3387 * supported with IOMAP_WRITE.
3388 */
3389 WARN_ON(!IS_DAX(inode) && !(flags & IOMAP_DIRECT));
3390 if (IS_DAX(inode))
3391 m_flags = EXT4_GET_BLOCKS_CREATE_ZERO;
3392 /*
3393 * We use i_size instead of i_disksize here because delalloc writeback
3394 * can complete at any point during the I/O and subsequently push the
3395 * i_disksize out to i_size. This could be beyond where direct I/O is
3396 * happening and thus expose allocated blocks to direct I/O reads.
3397 */
3398 else if ((map->m_lblk * (1 << blkbits)) >= i_size_read(inode))
3399 m_flags = EXT4_GET_BLOCKS_CREATE;
3400 else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3401 m_flags = EXT4_GET_BLOCKS_IO_CREATE_EXT;
3402
3403 ret = ext4_map_blocks(handle, inode, map, m_flags);
3404
3405 /*
3406 * We cannot fill holes in indirect tree based inodes as that could
3407 * expose stale data in the case of a crash. Use the magic error code
3408 * to fallback to buffered I/O.
3409 */
3410 if (!m_flags && !ret)
3411 ret = -ENOTBLK;
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003412
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003413 ext4_journal_stop(handle);
3414 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3415 goto retry;
3416
3417 return ret;
3418}
3419
3420
Jan Kara364443c2016-11-20 17:36:06 -05003421static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -07003422 unsigned flags, struct iomap *iomap, struct iomap *srcmap)
Jan Kara364443c2016-11-20 17:36:06 -05003423{
Jan Kara364443c2016-11-20 17:36:06 -05003424 int ret;
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003425 struct ext4_map_blocks map;
3426 u8 blkbits = inode->i_blkbits;
Jan Kara364443c2016-11-20 17:36:06 -05003427
Theodore Ts'obcd8e912018-09-01 12:45:04 -04003428 if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
3429 return -EINVAL;
Andreas Gruenbacher7046ae32017-10-01 17:57:54 -04003430
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003431 if (WARN_ON_ONCE(ext4_has_inline_data(inode)))
3432 return -ERANGE;
Jan Kara364443c2016-11-20 17:36:06 -05003433
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003434 /*
3435 * Calculate the first and last logical blocks respectively.
3436 */
3437 map.m_lblk = offset >> blkbits;
3438 map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits,
3439 EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1;
Jan Kara364443c2016-11-20 17:36:06 -05003440
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003441 if (flags & IOMAP_WRITE)
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003442 ret = ext4_iomap_alloc(inode, &map, flags);
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003443 else
Jan Kara776722e2016-11-20 18:09:11 -05003444 ret = ext4_map_blocks(NULL, inode, &map, 0);
Christoph Hellwig545052e2017-10-01 17:58:54 -04003445
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003446 if (ret < 0)
3447 return ret;
Christoph Hellwig545052e2017-10-01 17:58:54 -04003448
Matthew Bobrowskic8fdfe292019-11-05 22:59:56 +11003449 ext4_set_iomap(inode, iomap, &map, offset, length);
Christoph Hellwig545052e2017-10-01 17:58:54 -04003450
Jan Kara364443c2016-11-20 17:36:06 -05003451 return 0;
3452}
3453
Jan Kara8cd115b2019-12-18 18:44:33 +01003454static int ext4_iomap_overwrite_begin(struct inode *inode, loff_t offset,
3455 loff_t length, unsigned flags, struct iomap *iomap,
3456 struct iomap *srcmap)
3457{
3458 int ret;
3459
3460 /*
3461 * Even for writes we don't need to allocate blocks, so just pretend
3462 * we are reading to save overhead of starting a transaction.
3463 */
3464 flags &= ~IOMAP_WRITE;
3465 ret = ext4_iomap_begin(inode, offset, length, flags, iomap, srcmap);
3466 WARN_ON_ONCE(iomap->type != IOMAP_MAPPED);
3467 return ret;
3468}
3469
Jan Kara776722e2016-11-20 18:09:11 -05003470static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length,
3471 ssize_t written, unsigned flags, struct iomap *iomap)
3472{
Jan Kara776722e2016-11-20 18:09:11 -05003473 /*
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003474 * Check to see whether an error occurred while writing out the data to
3475 * the allocated blocks. If so, return the magic error code so that we
3476 * fallback to buffered I/O and attempt to complete the remainder of
3477 * the I/O. Any blocks that may have been allocated in preparation for
3478 * the direct I/O will be reused during buffered I/O.
Jan Kara776722e2016-11-20 18:09:11 -05003479 */
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003480 if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0)
3481 return -ENOTBLK;
Jan Kara776722e2016-11-20 18:09:11 -05003482
Matthew Bobrowski569342d2019-11-05 23:01:51 +11003483 return 0;
Jan Kara776722e2016-11-20 18:09:11 -05003484}
3485
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08003486const struct iomap_ops ext4_iomap_ops = {
Jan Kara364443c2016-11-20 17:36:06 -05003487 .iomap_begin = ext4_iomap_begin,
Jan Kara776722e2016-11-20 18:09:11 -05003488 .iomap_end = ext4_iomap_end,
Jan Kara364443c2016-11-20 17:36:06 -05003489};
3490
Jan Kara8cd115b2019-12-18 18:44:33 +01003491const struct iomap_ops ext4_iomap_overwrite_ops = {
3492 .iomap_begin = ext4_iomap_overwrite_begin,
3493 .iomap_end = ext4_iomap_end,
3494};
3495
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003496static bool ext4_iomap_is_delalloc(struct inode *inode,
3497 struct ext4_map_blocks *map)
Mingming Cao4c0425f2009-09-28 15:48:41 -04003498{
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003499 struct extent_status es;
3500 ext4_lblk_t offset = 0, end = map->m_lblk + map->m_len - 1;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003501
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003502 ext4_es_find_extent_range(inode, &ext4_es_is_delayed,
3503 map->m_lblk, end, &es);
Mingming4b70df12009-11-03 14:44:54 -05003504
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003505 if (!es.es_len || es.es_lblk > end)
3506 return false;
3507
3508 if (es.es_lblk > map->m_lblk) {
3509 map->m_len = es.es_lblk - map->m_lblk;
3510 return false;
3511 }
3512
3513 offset = map->m_lblk - es.es_lblk;
3514 map->m_len = es.es_len - offset;
3515
3516 return true;
3517}
3518
3519static int ext4_iomap_begin_report(struct inode *inode, loff_t offset,
3520 loff_t length, unsigned int flags,
3521 struct iomap *iomap, struct iomap *srcmap)
3522{
3523 int ret;
3524 bool delalloc = false;
3525 struct ext4_map_blocks map;
3526 u8 blkbits = inode->i_blkbits;
3527
3528 if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
3529 return -EINVAL;
3530
3531 if (ext4_has_inline_data(inode)) {
3532 ret = ext4_inline_data_iomap(inode, iomap);
3533 if (ret != -EAGAIN) {
3534 if (ret == 0 && offset >= iomap->length)
3535 ret = -ENOENT;
3536 return ret;
3537 }
3538 }
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003539
Jan Kara74c66bc2016-02-29 08:36:38 +11003540 /*
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003541 * Calculate the first and last logical block respectively.
Jan Kara74c66bc2016-02-29 08:36:38 +11003542 */
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003543 map.m_lblk = offset >> blkbits;
3544 map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits,
3545 EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1;
3546
Ritesh Harjanib2c57642020-02-28 14:56:57 +05303547 /*
3548 * Fiemap callers may call for offset beyond s_bitmap_maxbytes.
3549 * So handle it here itself instead of querying ext4_map_blocks().
3550 * Since ext4_map_blocks() will warn about it and will return
3551 * -EIO error.
3552 */
3553 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
3554 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3555
3556 if (offset >= sbi->s_bitmap_maxbytes) {
3557 map.m_flags = 0;
3558 goto set_iomap;
3559 }
3560 }
3561
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003562 ret = ext4_map_blocks(NULL, inode, &map, 0);
3563 if (ret < 0)
3564 return ret;
3565 if (ret == 0)
3566 delalloc = ext4_iomap_is_delalloc(inode, &map);
3567
Ritesh Harjanib2c57642020-02-28 14:56:57 +05303568set_iomap:
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003569 ext4_set_iomap(inode, iomap, &map, offset, length);
3570 if (delalloc && iomap->type == IOMAP_HOLE)
3571 iomap->type = IOMAP_DELALLOC;
Christoph Hellwig187372a2016-02-08 14:40:51 +11003572
3573 return 0;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003574}
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003575
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003576const struct iomap_ops ext4_iomap_report_ops = {
3577 .iomap_begin = ext4_iomap_begin_report,
3578};
Mingming Cao4c0425f2009-09-28 15:48:41 -04003579
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003580/*
Mingming Cao617ba132006-10-11 01:20:53 -07003581 * Pages can be marked dirty completely asynchronously from ext4's journalling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003582 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3583 * much here because ->set_page_dirty is called under VFS locks. The page is
3584 * not necessarily locked.
3585 *
3586 * We cannot just dirty the page and leave attached buffers clean, because the
3587 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3588 * or jbddirty because all the journalling code will explode.
3589 *
3590 * So what we do is to mark the page "pending dirty" and next time writepage
3591 * is called, propagate that into the buffers appropriately.
3592 */
Mingming Cao617ba132006-10-11 01:20:53 -07003593static int ext4_journalled_set_page_dirty(struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003594{
3595 SetPageChecked(page);
3596 return __set_page_dirty_nobuffers(page);
3597}
3598
Jan Kara6dcc6932016-12-01 11:46:40 -05003599static int ext4_set_page_dirty(struct page *page)
3600{
3601 WARN_ON_ONCE(!PageLocked(page) && !PageDirty(page));
3602 WARN_ON_ONCE(!page_has_buffers(page));
3603 return __set_page_dirty_buffers(page);
3604}
3605
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003606static const struct address_space_operations ext4_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003607 .readpage = ext4_readpage,
3608 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003609 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003610 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003611 .write_begin = ext4_write_begin,
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003612 .write_end = ext4_write_end,
Jan Kara6dcc6932016-12-01 11:46:40 -05003613 .set_page_dirty = ext4_set_page_dirty,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003614 .bmap = ext4_bmap,
3615 .invalidatepage = ext4_invalidatepage,
3616 .releasepage = ext4_releasepage,
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003617 .direct_IO = noop_direct_IO,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003618 .migratepage = buffer_migrate_page,
3619 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003620 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003621};
3622
Mingming Cao617ba132006-10-11 01:20:53 -07003623static const struct address_space_operations ext4_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003624 .readpage = ext4_readpage,
3625 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003626 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003627 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003628 .write_begin = ext4_write_begin,
3629 .write_end = ext4_journalled_write_end,
3630 .set_page_dirty = ext4_journalled_set_page_dirty,
3631 .bmap = ext4_bmap,
Jan Kara4520fb32012-12-25 13:28:54 -05003632 .invalidatepage = ext4_journalled_invalidatepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003633 .releasepage = ext4_releasepage,
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003634 .direct_IO = noop_direct_IO,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003635 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003636 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003637};
3638
Alex Tomas64769242008-07-11 19:27:31 -04003639static const struct address_space_operations ext4_da_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003640 .readpage = ext4_readpage,
3641 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003642 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003643 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003644 .write_begin = ext4_da_write_begin,
3645 .write_end = ext4_da_write_end,
Jan Kara6dcc6932016-12-01 11:46:40 -05003646 .set_page_dirty = ext4_set_page_dirty,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003647 .bmap = ext4_bmap,
Eric Whitney8fcc3a52019-08-22 23:22:14 -04003648 .invalidatepage = ext4_invalidatepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003649 .releasepage = ext4_releasepage,
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003650 .direct_IO = noop_direct_IO,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003651 .migratepage = buffer_migrate_page,
3652 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003653 .error_remove_page = generic_error_remove_page,
Alex Tomas64769242008-07-11 19:27:31 -04003654};
3655
Dan Williams5f0663b2017-12-21 12:25:11 -08003656static const struct address_space_operations ext4_dax_aops = {
3657 .writepages = ext4_dax_writepages,
3658 .direct_IO = noop_direct_IO,
3659 .set_page_dirty = noop_set_page_dirty,
Toshi Kani94dbb632018-09-15 21:23:41 -04003660 .bmap = ext4_bmap,
Dan Williams5f0663b2017-12-21 12:25:11 -08003661 .invalidatepage = noop_invalidatepage,
3662};
3663
Mingming Cao617ba132006-10-11 01:20:53 -07003664void ext4_set_aops(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003665{
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003666 switch (ext4_inode_journal_mode(inode)) {
3667 case EXT4_INODE_ORDERED_DATA_MODE:
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003668 case EXT4_INODE_WRITEBACK_DATA_MODE:
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003669 break;
3670 case EXT4_INODE_JOURNAL_DATA_MODE:
Mingming Cao617ba132006-10-11 01:20:53 -07003671 inode->i_mapping->a_ops = &ext4_journalled_aops;
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003672 return;
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003673 default:
3674 BUG();
3675 }
Dan Williams5f0663b2017-12-21 12:25:11 -08003676 if (IS_DAX(inode))
3677 inode->i_mapping->a_ops = &ext4_dax_aops;
3678 else if (test_opt(inode->i_sb, DELALLOC))
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003679 inode->i_mapping->a_ops = &ext4_da_aops;
3680 else
3681 inode->i_mapping->a_ops = &ext4_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003682}
3683
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003684static int __ext4_block_zero_page_range(handle_t *handle,
Lukas Czernerd863dc32013-05-27 23:32:35 -04003685 struct address_space *mapping, loff_t from, loff_t length)
3686{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003687 ext4_fsblk_t index = from >> PAGE_SHIFT;
3688 unsigned offset = from & (PAGE_SIZE-1);
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003689 unsigned blocksize, pos;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003690 ext4_lblk_t iblock;
3691 struct inode *inode = mapping->host;
3692 struct buffer_head *bh;
3693 struct page *page;
3694 int err = 0;
3695
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003696 page = find_or_create_page(mapping, from >> PAGE_SHIFT,
Michal Hockoc62d2552015-11-06 16:28:49 -08003697 mapping_gfp_constraint(mapping, ~__GFP_FS));
Lukas Czernerd863dc32013-05-27 23:32:35 -04003698 if (!page)
3699 return -ENOMEM;
3700
3701 blocksize = inode->i_sb->s_blocksize;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003702
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003703 iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003704
3705 if (!page_has_buffers(page))
3706 create_empty_buffers(page, blocksize, 0);
3707
3708 /* Find the buffer that contains "offset" */
3709 bh = page_buffers(page);
3710 pos = blocksize;
3711 while (offset >= pos) {
3712 bh = bh->b_this_page;
3713 iblock++;
3714 pos += blocksize;
3715 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003716 if (buffer_freed(bh)) {
3717 BUFFER_TRACE(bh, "freed: skip");
3718 goto unlock;
3719 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003720 if (!buffer_mapped(bh)) {
3721 BUFFER_TRACE(bh, "unmapped");
3722 ext4_get_block(inode, iblock, bh, 0);
3723 /* unmapped? It's a hole - nothing to do */
3724 if (!buffer_mapped(bh)) {
3725 BUFFER_TRACE(bh, "still unmapped");
3726 goto unlock;
3727 }
3728 }
3729
3730 /* Ok, it's mapped. Make sure it's up-to-date */
3731 if (PageUptodate(page))
3732 set_buffer_uptodate(bh);
3733
3734 if (!buffer_uptodate(bh)) {
3735 err = -EIO;
Mike Christiedfec8a12016-06-05 14:31:44 -05003736 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003737 wait_on_buffer(bh);
3738 /* Uhhuh. Read error. Complain and punt. */
3739 if (!buffer_uptodate(bh))
3740 goto unlock;
Chandan Rajendra592ddec2018-12-12 15:20:10 +05303741 if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode)) {
Michael Halcrowc9c74292015-04-12 00:56:10 -04003742 /* We expect the key to be set. */
Jaegeuk Kima7550b32016-07-10 14:01:03 -04003743 BUG_ON(!fscrypt_has_encryption_key(inode));
Eric Biggers834f1562019-12-26 09:41:05 -06003744 err = fscrypt_decrypt_pagecache_blocks(page, blocksize,
3745 bh_offset(bh));
3746 if (err) {
3747 clear_buffer_uptodate(bh);
3748 goto unlock;
3749 }
Michael Halcrowc9c74292015-04-12 00:56:10 -04003750 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003751 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003752 if (ext4_should_journal_data(inode)) {
3753 BUFFER_TRACE(bh, "get write access");
3754 err = ext4_journal_get_write_access(handle, bh);
3755 if (err)
3756 goto unlock;
3757 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003758 zero_user(page, offset, length);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003759 BUFFER_TRACE(bh, "zeroed end of block");
3760
Lukas Czernerd863dc32013-05-27 23:32:35 -04003761 if (ext4_should_journal_data(inode)) {
3762 err = ext4_handle_dirty_metadata(handle, inode, bh);
Lukas Czerner0713ed02013-05-27 23:32:35 -04003763 } else {
jon ernst353eefd2013-07-01 08:12:39 -04003764 err = 0;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003765 mark_buffer_dirty(bh);
Jan Kara3957ef52016-04-24 00:56:05 -04003766 if (ext4_should_order_data(inode))
Ross Zwisler73131fb2019-06-20 17:26:26 -04003767 err = ext4_jbd2_inode_add_write(handle, inode, from,
3768 length);
Lukas Czerner0713ed02013-05-27 23:32:35 -04003769 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003770
3771unlock:
3772 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003773 put_page(page);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003774 return err;
3775}
3776
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003777/*
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003778 * ext4_block_zero_page_range() zeros out a mapping of length 'length'
3779 * starting from file offset 'from'. The range to be zero'd must
3780 * be contained with in one block. If the specified range exceeds
3781 * the end of the block it will be shortened to end of the block
3782 * that cooresponds to 'from'
3783 */
3784static int ext4_block_zero_page_range(handle_t *handle,
3785 struct address_space *mapping, loff_t from, loff_t length)
3786{
3787 struct inode *inode = mapping->host;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003788 unsigned offset = from & (PAGE_SIZE-1);
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003789 unsigned blocksize = inode->i_sb->s_blocksize;
3790 unsigned max = blocksize - (offset & (blocksize - 1));
3791
3792 /*
3793 * correct length if it does not fall between
3794 * 'from' and the end of the block
3795 */
3796 if (length > max || length < 0)
3797 length = max;
3798
Jan Kara47e69352016-11-20 18:08:05 -05003799 if (IS_DAX(inode)) {
3800 return iomap_zero_range(inode, from, length, NULL,
3801 &ext4_iomap_ops);
3802 }
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003803 return __ext4_block_zero_page_range(handle, mapping, from, length);
3804}
3805
3806/*
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003807 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
3808 * up to the end of the block which corresponds to `from'.
3809 * This required during truncate. We need to physically zero the tail end
3810 * of that block so it doesn't yield old data if the file is later grown.
3811 */
Stephen Hemmingerc1978552014-05-12 10:50:23 -04003812static int ext4_block_truncate_page(handle_t *handle,
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003813 struct address_space *mapping, loff_t from)
3814{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003815 unsigned offset = from & (PAGE_SIZE-1);
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003816 unsigned length;
3817 unsigned blocksize;
3818 struct inode *inode = mapping->host;
3819
Theodore Ts'o0d068632017-02-14 11:31:15 -05003820 /* If we are processing an encrypted inode during orphan list handling */
Chandan Rajendra592ddec2018-12-12 15:20:10 +05303821 if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode))
Theodore Ts'o0d068632017-02-14 11:31:15 -05003822 return 0;
3823
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003824 blocksize = inode->i_sb->s_blocksize;
3825 length = blocksize - (offset & (blocksize - 1));
3826
3827 return ext4_block_zero_page_range(handle, mapping, from, length);
3828}
3829
Lukas Czernera87dd182013-05-27 23:32:35 -04003830int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
3831 loff_t lstart, loff_t length)
3832{
3833 struct super_block *sb = inode->i_sb;
3834 struct address_space *mapping = inode->i_mapping;
Lukas Czernere1be3a92013-07-01 08:12:39 -04003835 unsigned partial_start, partial_end;
Lukas Czernera87dd182013-05-27 23:32:35 -04003836 ext4_fsblk_t start, end;
3837 loff_t byte_end = (lstart + length - 1);
3838 int err = 0;
3839
Lukas Czernere1be3a92013-07-01 08:12:39 -04003840 partial_start = lstart & (sb->s_blocksize - 1);
3841 partial_end = byte_end & (sb->s_blocksize - 1);
3842
Lukas Czernera87dd182013-05-27 23:32:35 -04003843 start = lstart >> sb->s_blocksize_bits;
3844 end = byte_end >> sb->s_blocksize_bits;
3845
3846 /* Handle partial zero within the single block */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003847 if (start == end &&
3848 (partial_start || (partial_end != sb->s_blocksize - 1))) {
Lukas Czernera87dd182013-05-27 23:32:35 -04003849 err = ext4_block_zero_page_range(handle, mapping,
3850 lstart, length);
3851 return err;
3852 }
3853 /* Handle partial zero out on the start of the range */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003854 if (partial_start) {
Lukas Czernera87dd182013-05-27 23:32:35 -04003855 err = ext4_block_zero_page_range(handle, mapping,
3856 lstart, sb->s_blocksize);
3857 if (err)
3858 return err;
3859 }
3860 /* Handle partial zero out on the end of the range */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003861 if (partial_end != sb->s_blocksize - 1)
Lukas Czernera87dd182013-05-27 23:32:35 -04003862 err = ext4_block_zero_page_range(handle, mapping,
Lukas Czernere1be3a92013-07-01 08:12:39 -04003863 byte_end - partial_end,
3864 partial_end + 1);
Lukas Czernera87dd182013-05-27 23:32:35 -04003865 return err;
3866}
3867
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003868int ext4_can_truncate(struct inode *inode)
3869{
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003870 if (S_ISREG(inode->i_mode))
3871 return 1;
3872 if (S_ISDIR(inode->i_mode))
3873 return 1;
3874 if (S_ISLNK(inode->i_mode))
3875 return !ext4_inode_is_fast_symlink(inode);
3876 return 0;
3877}
3878
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003879/*
Jan Kara01127842015-12-07 14:34:49 -05003880 * We have to make sure i_disksize gets properly updated before we truncate
3881 * page cache due to hole punching or zero range. Otherwise i_disksize update
3882 * can get lost as it may have been postponed to submission of writeback but
3883 * that will never happen after we truncate page cache.
3884 */
3885int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset,
3886 loff_t len)
3887{
3888 handle_t *handle;
3889 loff_t size = i_size_read(inode);
3890
Al Viro59551022016-01-22 15:40:57 -05003891 WARN_ON(!inode_is_locked(inode));
Jan Kara01127842015-12-07 14:34:49 -05003892 if (offset > size || offset + len < size)
3893 return 0;
3894
3895 if (EXT4_I(inode)->i_disksize >= size)
3896 return 0;
3897
3898 handle = ext4_journal_start(inode, EXT4_HT_MISC, 1);
3899 if (IS_ERR(handle))
3900 return PTR_ERR(handle);
3901 ext4_update_i_disksize(inode, size);
3902 ext4_mark_inode_dirty(handle, inode);
3903 ext4_journal_stop(handle);
3904
3905 return 0;
3906}
3907
Ross Zwislerb1f38212018-09-11 13:31:16 -04003908static void ext4_wait_dax_page(struct ext4_inode_info *ei)
Ross Zwisler430657b2018-07-29 17:00:22 -04003909{
Ross Zwisler430657b2018-07-29 17:00:22 -04003910 up_write(&ei->i_mmap_sem);
3911 schedule();
3912 down_write(&ei->i_mmap_sem);
3913}
3914
3915int ext4_break_layouts(struct inode *inode)
3916{
3917 struct ext4_inode_info *ei = EXT4_I(inode);
3918 struct page *page;
Ross Zwisler430657b2018-07-29 17:00:22 -04003919 int error;
3920
3921 if (WARN_ON_ONCE(!rwsem_is_locked(&ei->i_mmap_sem)))
3922 return -EINVAL;
3923
3924 do {
Ross Zwisler430657b2018-07-29 17:00:22 -04003925 page = dax_layout_busy_page(inode->i_mapping);
3926 if (!page)
3927 return 0;
3928
3929 error = ___wait_var_event(&page->_refcount,
3930 atomic_read(&page->_refcount) == 1,
3931 TASK_INTERRUPTIBLE, 0, 0,
Ross Zwislerb1f38212018-09-11 13:31:16 -04003932 ext4_wait_dax_page(ei));
3933 } while (error == 0);
Ross Zwisler430657b2018-07-29 17:00:22 -04003934
3935 return error;
3936}
3937
Jan Kara01127842015-12-07 14:34:49 -05003938/*
Ross Zwislercca32b72016-09-22 11:49:38 -04003939 * ext4_punch_hole: punches a hole in a file by releasing the blocks
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003940 * associated with the given offset and length
3941 *
3942 * @inode: File inode
3943 * @offset: The offset where the hole will begin
3944 * @len: The length of the hole
3945 *
Anatol Pomozov4907cb72012-09-01 10:31:09 -07003946 * Returns: 0 on success or negative on failure
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003947 */
3948
Ashish Sangwanaeb28172013-07-01 08:12:38 -04003949int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length)
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003950{
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003951 struct super_block *sb = inode->i_sb;
3952 ext4_lblk_t first_block, stop_block;
3953 struct address_space *mapping = inode->i_mapping;
Lukas Czernera87dd182013-05-27 23:32:35 -04003954 loff_t first_block_offset, last_block_offset;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003955 handle_t *handle;
3956 unsigned int credits;
3957 int ret = 0;
3958
Lukas Czernerb8a86842014-03-18 18:05:35 -04003959 trace_ext4_punch_hole(inode, offset, length, 0);
Zheng Liuaaddea82013-01-16 20:21:26 -05003960
Theodore Ts'oc1e82202019-08-23 22:38:00 -04003961 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
3962 if (ext4_has_inline_data(inode)) {
3963 down_write(&EXT4_I(inode)->i_mmap_sem);
3964 ret = ext4_convert_inline_data(inode);
3965 up_write(&EXT4_I(inode)->i_mmap_sem);
3966 if (ret)
3967 return ret;
3968 }
3969
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003970 /*
3971 * Write out all dirty pages to avoid race conditions
3972 * Then release them.
3973 */
Ross Zwislercca32b72016-09-22 11:49:38 -04003974 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003975 ret = filemap_write_and_wait_range(mapping, offset,
3976 offset + length - 1);
3977 if (ret)
3978 return ret;
3979 }
3980
Al Viro59551022016-01-22 15:40:57 -05003981 inode_lock(inode);
Lukas Czerner9ef06ce2014-04-12 09:47:00 -04003982
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003983 /* No need to punch hole beyond i_size */
3984 if (offset >= inode->i_size)
3985 goto out_mutex;
3986
3987 /*
3988 * If the hole extends beyond i_size, set the hole
3989 * to end after the page that contains i_size
3990 */
3991 if (offset + length > inode->i_size) {
3992 length = inode->i_size +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003993 PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) -
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003994 offset;
3995 }
3996
Jan Karaa3612932013-08-16 21:19:41 -04003997 if (offset & (sb->s_blocksize - 1) ||
3998 (offset + length) & (sb->s_blocksize - 1)) {
3999 /*
4000 * Attach jinode to inode for jbd2 if we do any zeroing of
4001 * partial block
4002 */
4003 ret = ext4_inode_attach_jinode(inode);
4004 if (ret < 0)
4005 goto out_mutex;
4006
4007 }
4008
Jan Karaea3d7202015-12-07 14:28:03 -05004009 /* Wait all existing dio workers, newcomers will block on i_mutex */
Jan Karaea3d7202015-12-07 14:28:03 -05004010 inode_dio_wait(inode);
4011
4012 /*
4013 * Prevent page faults from reinstantiating pages we have released from
4014 * page cache.
4015 */
4016 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04004017
4018 ret = ext4_break_layouts(inode);
4019 if (ret)
4020 goto out_dio;
4021
Lukas Czernera87dd182013-05-27 23:32:35 -04004022 first_block_offset = round_up(offset, sb->s_blocksize);
4023 last_block_offset = round_down((offset + length), sb->s_blocksize) - 1;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004024
Lukas Czernera87dd182013-05-27 23:32:35 -04004025 /* Now release the pages and zero block aligned part of pages*/
Jan Kara01127842015-12-07 14:34:49 -05004026 if (last_block_offset > first_block_offset) {
4027 ret = ext4_update_disksize_before_punch(inode, offset, length);
4028 if (ret)
4029 goto out_dio;
Lukas Czernera87dd182013-05-27 23:32:35 -04004030 truncate_pagecache_range(inode, first_block_offset,
4031 last_block_offset);
Jan Kara01127842015-12-07 14:34:49 -05004032 }
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004033
4034 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4035 credits = ext4_writepage_trans_blocks(inode);
4036 else
4037 credits = ext4_blocks_for_truncate(inode);
4038 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
4039 if (IS_ERR(handle)) {
4040 ret = PTR_ERR(handle);
4041 ext4_std_error(sb, ret);
4042 goto out_dio;
4043 }
4044
Lukas Czernera87dd182013-05-27 23:32:35 -04004045 ret = ext4_zero_partial_blocks(handle, inode, offset,
4046 length);
4047 if (ret)
4048 goto out_stop;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004049
4050 first_block = (offset + sb->s_blocksize - 1) >>
4051 EXT4_BLOCK_SIZE_BITS(sb);
4052 stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
4053
Lukas Czernereee597a2018-05-13 19:28:35 -04004054 /* If there are blocks to remove, do it */
4055 if (stop_block > first_block) {
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004056
Lukas Czernereee597a2018-05-13 19:28:35 -04004057 down_write(&EXT4_I(inode)->i_data_sem);
4058 ext4_discard_preallocations(inode);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004059
Lukas Czernereee597a2018-05-13 19:28:35 -04004060 ret = ext4_es_remove_extent(inode, first_block,
4061 stop_block - first_block);
4062 if (ret) {
4063 up_write(&EXT4_I(inode)->i_data_sem);
4064 goto out_stop;
4065 }
4066
4067 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4068 ret = ext4_ext_remove_space(inode, first_block,
4069 stop_block - 1);
4070 else
4071 ret = ext4_ind_remove_space(handle, inode, first_block,
4072 stop_block);
4073
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004074 up_write(&EXT4_I(inode)->i_data_sem);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004075 }
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004076 if (IS_SYNC(inode))
4077 ext4_handle_sync(handle);
Maxim Patlasove251f9b2014-02-20 16:58:05 -05004078
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004079 inode->i_mtime = inode->i_ctime = current_time(inode);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004080 ext4_mark_inode_dirty(handle, inode);
Jan Kara67a7d5f2017-05-29 13:24:55 -04004081 if (ret >= 0)
4082 ext4_update_inode_fsync_trans(handle, inode, 1);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004083out_stop:
4084 ext4_journal_stop(handle);
4085out_dio:
Jan Karaea3d7202015-12-07 14:28:03 -05004086 up_write(&EXT4_I(inode)->i_mmap_sem);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004087out_mutex:
Al Viro59551022016-01-22 15:40:57 -05004088 inode_unlock(inode);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004089 return ret;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004090}
4091
Jan Karaa3612932013-08-16 21:19:41 -04004092int ext4_inode_attach_jinode(struct inode *inode)
4093{
4094 struct ext4_inode_info *ei = EXT4_I(inode);
4095 struct jbd2_inode *jinode;
4096
4097 if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal)
4098 return 0;
4099
4100 jinode = jbd2_alloc_inode(GFP_KERNEL);
4101 spin_lock(&inode->i_lock);
4102 if (!ei->jinode) {
4103 if (!jinode) {
4104 spin_unlock(&inode->i_lock);
4105 return -ENOMEM;
4106 }
4107 ei->jinode = jinode;
4108 jbd2_journal_init_jbd_inode(ei->jinode, inode);
4109 jinode = NULL;
4110 }
4111 spin_unlock(&inode->i_lock);
4112 if (unlikely(jinode != NULL))
4113 jbd2_free_inode(jinode);
4114 return 0;
4115}
4116
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004117/*
Mingming Cao617ba132006-10-11 01:20:53 -07004118 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004119 *
Mingming Cao617ba132006-10-11 01:20:53 -07004120 * We block out ext4_get_block() block instantiations across the entire
4121 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004122 * simultaneously on behalf of the same inode.
4123 *
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08004124 * As we work through the truncate and commit bits of it to the journal there
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004125 * is one core, guiding principle: the file's tree must always be consistent on
4126 * disk. We must be able to restart the truncate after a crash.
4127 *
4128 * The file's tree may be transiently inconsistent in memory (although it
4129 * probably isn't), but whenever we close off and commit a journal transaction,
4130 * the contents of (the filesystem + the journal) must be consistent and
4131 * restartable. It's pretty simple, really: bottom up, right to left (although
4132 * left-to-right works OK too).
4133 *
4134 * Note that at recovery time, journal replay occurs *before* the restart of
4135 * truncate against the orphan inode list.
4136 *
4137 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07004138 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004139 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07004140 * ext4_truncate() to have another go. So there will be instantiated blocks
4141 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004142 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07004143 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004144 */
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004145int ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004146{
Theodore Ts'o819c4922013-04-03 12:47:17 -04004147 struct ext4_inode_info *ei = EXT4_I(inode);
4148 unsigned int credits;
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004149 int err = 0;
Theodore Ts'o819c4922013-04-03 12:47:17 -04004150 handle_t *handle;
4151 struct address_space *mapping = inode->i_mapping;
Theodore Ts'o819c4922013-04-03 12:47:17 -04004152
Theodore Ts'o19b5ef62013-04-03 21:58:52 -04004153 /*
4154 * There is a possibility that we're either freeing the inode
Matthew Wilcoxe04027e2014-03-24 15:15:07 -04004155 * or it's a completely new inode. In those cases we might not
Theodore Ts'o19b5ef62013-04-03 21:58:52 -04004156 * have i_mutex locked because it's not necessary.
4157 */
4158 if (!(inode->i_state & (I_NEW|I_FREEING)))
Al Viro59551022016-01-22 15:40:57 -05004159 WARN_ON(!inode_is_locked(inode));
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004160 trace_ext4_truncate_enter(inode);
4161
Duane Griffin91ef4ca2008-07-11 19:27:31 -04004162 if (!ext4_can_truncate(inode))
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004163 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004164
Theodore Ts'o5534fb52009-09-17 09:34:16 -04004165 if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004166 ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
Theodore Ts'o7d8f9f72009-02-24 08:21:14 -05004167
Tao Maaef1c852012-12-10 14:06:02 -05004168 if (ext4_has_inline_data(inode)) {
4169 int has_inline = 1;
4170
Theodore Ts'o01daf942017-01-22 19:35:49 -05004171 err = ext4_inline_data_truncate(inode, &has_inline);
4172 if (err)
4173 return err;
Tao Maaef1c852012-12-10 14:06:02 -05004174 if (has_inline)
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004175 return 0;
Tao Maaef1c852012-12-10 14:06:02 -05004176 }
4177
Jan Karaa3612932013-08-16 21:19:41 -04004178 /* If we zero-out tail of the page, we have to create jinode for jbd2 */
4179 if (inode->i_size & (inode->i_sb->s_blocksize - 1)) {
4180 if (ext4_inode_attach_jinode(inode) < 0)
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004181 return 0;
Jan Karaa3612932013-08-16 21:19:41 -04004182 }
4183
Amir Goldsteinff9893d2011-06-27 16:36:31 -04004184 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Theodore Ts'o819c4922013-04-03 12:47:17 -04004185 credits = ext4_writepage_trans_blocks(inode);
Amir Goldsteinff9893d2011-06-27 16:36:31 -04004186 else
Theodore Ts'o819c4922013-04-03 12:47:17 -04004187 credits = ext4_blocks_for_truncate(inode);
4188
4189 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004190 if (IS_ERR(handle))
4191 return PTR_ERR(handle);
Theodore Ts'o819c4922013-04-03 12:47:17 -04004192
Lukas Czernereb3544c2013-05-27 23:32:35 -04004193 if (inode->i_size & (inode->i_sb->s_blocksize - 1))
4194 ext4_block_truncate_page(handle, mapping, inode->i_size);
Theodore Ts'o819c4922013-04-03 12:47:17 -04004195
4196 /*
4197 * We add the inode to the orphan list, so that if this
4198 * truncate spans multiple transactions, and we crash, we will
4199 * resume the truncate when the filesystem recovers. It also
4200 * marks the inode dirty, to catch the new size.
4201 *
4202 * Implication: the file must always be in a sane, consistent
4203 * truncatable state while each transaction commits.
4204 */
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004205 err = ext4_orphan_add(handle, inode);
4206 if (err)
Theodore Ts'o819c4922013-04-03 12:47:17 -04004207 goto out_stop;
4208
4209 down_write(&EXT4_I(inode)->i_data_sem);
4210
4211 ext4_discard_preallocations(inode);
4212
4213 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Theodore Ts'od0abb362016-11-13 22:02:28 -05004214 err = ext4_ext_truncate(handle, inode);
Theodore Ts'o819c4922013-04-03 12:47:17 -04004215 else
4216 ext4_ind_truncate(handle, inode);
4217
4218 up_write(&ei->i_data_sem);
Theodore Ts'od0abb362016-11-13 22:02:28 -05004219 if (err)
4220 goto out_stop;
Theodore Ts'o819c4922013-04-03 12:47:17 -04004221
4222 if (IS_SYNC(inode))
4223 ext4_handle_sync(handle);
4224
4225out_stop:
4226 /*
4227 * If this was a simple ftruncate() and the file will remain alive,
4228 * then we need to clear up the orphan record which we created above.
4229 * However, if this was a real unlink then we were called by
Wang Shilong58d86a52014-11-25 16:17:29 -05004230 * ext4_evict_inode(), and we allow that function to clean up the
Theodore Ts'o819c4922013-04-03 12:47:17 -04004231 * orphan info for us.
4232 */
4233 if (inode->i_nlink)
4234 ext4_orphan_del(handle, inode);
4235
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004236 inode->i_mtime = inode->i_ctime = current_time(inode);
Theodore Ts'o819c4922013-04-03 12:47:17 -04004237 ext4_mark_inode_dirty(handle, inode);
4238 ext4_journal_stop(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07004239
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004240 trace_ext4_truncate_exit(inode);
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004241 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004242}
4243
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004244/*
Mingming Cao617ba132006-10-11 01:20:53 -07004245 * ext4_get_inode_loc returns with an extra refcount against the inode's
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004246 * underlying buffer_head on success. If 'in_mem' is true, we have all
4247 * data in memory that is needed to recreate the on-disk version of this
4248 * inode.
4249 */
Mingming Cao617ba132006-10-11 01:20:53 -07004250static int __ext4_get_inode_loc(struct inode *inode,
4251 struct ext4_iloc *iloc, int in_mem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004252{
Theodore Ts'o240799c2008-10-09 23:53:47 -04004253 struct ext4_group_desc *gdp;
4254 struct buffer_head *bh;
4255 struct super_block *sb = inode->i_sb;
4256 ext4_fsblk_t block;
Linus Torvalds02f03c42019-09-29 17:59:23 -07004257 struct blk_plug plug;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004258 int inodes_per_block, inode_offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004259
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004260 iloc->bh = NULL;
Theodore Ts'oc37e9e02018-06-17 00:41:14 -04004261 if (inode->i_ino < EXT4_ROOT_INO ||
4262 inode->i_ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004263 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004264
Theodore Ts'o240799c2008-10-09 23:53:47 -04004265 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
4266 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
4267 if (!gdp)
4268 return -EIO;
4269
4270 /*
4271 * Figure out the offset within the block group inode table
4272 */
Tao Ma00d09882011-05-09 10:26:41 -04004273 inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004274 inode_offset = ((inode->i_ino - 1) %
4275 EXT4_INODES_PER_GROUP(sb));
4276 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
4277 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
4278
4279 bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05004280 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -05004281 return -ENOMEM;
Theodore Ts'o46f870d62019-11-21 13:09:43 -05004282 if (ext4_simulate_fail(sb, EXT4_SIM_INODE_EIO))
4283 goto simulate_eio;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004284 if (!buffer_uptodate(bh)) {
4285 lock_buffer(bh);
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04004286
4287 /*
4288 * If the buffer has the write error flag, we have failed
4289 * to write out another inode in the same block. In this
4290 * case, we don't have to read the block because we may
4291 * read the old inode data successfully.
4292 */
4293 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
4294 set_buffer_uptodate(bh);
4295
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004296 if (buffer_uptodate(bh)) {
4297 /* someone brought it uptodate while we waited */
4298 unlock_buffer(bh);
4299 goto has_buffer;
4300 }
4301
4302 /*
4303 * If we have all information of the inode in memory and this
4304 * is the only valid inode in the block, we need not read the
4305 * block.
4306 */
4307 if (in_mem) {
4308 struct buffer_head *bitmap_bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004309 int i, start;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004310
Theodore Ts'o240799c2008-10-09 23:53:47 -04004311 start = inode_offset & ~(inodes_per_block - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004312
4313 /* Is the inode bitmap in cache? */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004314 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
Wang Shilongaebf0242013-01-12 16:28:47 -05004315 if (unlikely(!bitmap_bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004316 goto make_io;
4317
4318 /*
4319 * If the inode bitmap isn't in cache then the
4320 * optimisation may end up performing two reads instead
4321 * of one, so skip it.
4322 */
4323 if (!buffer_uptodate(bitmap_bh)) {
4324 brelse(bitmap_bh);
4325 goto make_io;
4326 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04004327 for (i = start; i < start + inodes_per_block; i++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004328 if (i == inode_offset)
4329 continue;
Mingming Cao617ba132006-10-11 01:20:53 -07004330 if (ext4_test_bit(i, bitmap_bh->b_data))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004331 break;
4332 }
4333 brelse(bitmap_bh);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004334 if (i == start + inodes_per_block) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004335 /* all other inodes are free, so skip I/O */
4336 memset(bh->b_data, 0, bh->b_size);
4337 set_buffer_uptodate(bh);
4338 unlock_buffer(bh);
4339 goto has_buffer;
4340 }
4341 }
4342
4343make_io:
4344 /*
Theodore Ts'o240799c2008-10-09 23:53:47 -04004345 * If we need to do any I/O, try to pre-readahead extra
4346 * blocks from the inode table.
4347 */
Linus Torvalds02f03c42019-09-29 17:59:23 -07004348 blk_start_plug(&plug);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004349 if (EXT4_SB(sb)->s_inode_readahead_blks) {
4350 ext4_fsblk_t b, end, table;
4351 unsigned num;
Theodore Ts'o0d606e2c2013-04-23 08:59:35 -04004352 __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004353
4354 table = ext4_inode_table(sb, gdp);
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004355 /* s_inode_readahead_blks is always a power of 2 */
Theodore Ts'o0d606e2c2013-04-23 08:59:35 -04004356 b = block & ~((ext4_fsblk_t) ra_blks - 1);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004357 if (table > b)
4358 b = table;
Theodore Ts'o0d606e2c2013-04-23 08:59:35 -04004359 end = b + ra_blks;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004360 num = EXT4_INODES_PER_GROUP(sb);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04004361 if (ext4_has_group_desc_csum(sb))
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004362 num -= ext4_itable_unused_count(sb, gdp);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004363 table += num / inodes_per_block;
4364 if (end > table)
4365 end = table;
4366 while (b <= end)
Roman Gushchind87f6392020-02-28 16:14:11 -08004367 sb_breadahead_unmovable(sb, b++);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004368 }
4369
4370 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004371 * There are other valid inodes in the buffer, this inode
4372 * has in-inode xattrs, or we don't have this inode in memory.
4373 * Read the block from disk.
4374 */
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004375 trace_ext4_load_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004376 get_bh(bh);
4377 bh->b_end_io = end_buffer_read_sync;
Mike Christie2a222ca2016-06-05 14:31:43 -05004378 submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh);
Linus Torvalds02f03c42019-09-29 17:59:23 -07004379 blk_finish_plug(&plug);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004380 wait_on_buffer(bh);
4381 if (!buffer_uptodate(bh)) {
Theodore Ts'o46f870d62019-11-21 13:09:43 -05004382 simulate_eio:
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04004383 ext4_error_inode_block(inode, block, EIO,
Theodore Ts'oc398eda2010-07-27 11:56:40 -04004384 "unable to read itable block");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004385 brelse(bh);
4386 return -EIO;
4387 }
4388 }
4389has_buffer:
4390 iloc->bh = bh;
4391 return 0;
4392}
4393
Mingming Cao617ba132006-10-11 01:20:53 -07004394int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004395{
4396 /* We have all inode data except xattrs in memory here. */
Mingming Cao617ba132006-10-11 01:20:53 -07004397 return __ext4_get_inode_loc(inode, iloc,
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004398 !ext4_test_inode_state(inode, EXT4_STATE_XATTR));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004399}
4400
Ira Weinya8ab6d32020-05-28 07:59:58 -07004401static bool ext4_should_enable_dax(struct inode *inode)
Ross Zwisler66425862017-10-12 12:00:59 -04004402{
Ira Weinya8ab6d32020-05-28 07:59:58 -07004403 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4404
Ross Zwisler66425862017-10-12 12:00:59 -04004405 if (!S_ISREG(inode->i_mode))
4406 return false;
4407 if (ext4_should_journal_data(inode))
4408 return false;
4409 if (ext4_has_inline_data(inode))
4410 return false;
Chandan Rajendra592ddec2018-12-12 15:20:10 +05304411 if (ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT))
Ross Zwisler66425862017-10-12 12:00:59 -04004412 return false;
Eric Biggersc93d8f82019-07-22 09:26:24 -07004413 if (ext4_test_inode_flag(inode, EXT4_INODE_VERITY))
4414 return false;
Ira Weinya8ab6d32020-05-28 07:59:58 -07004415 if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags))
4416 return false;
4417 if (test_opt(inode->i_sb, DAX_ALWAYS))
4418 return true;
4419
4420 return false;
Ross Zwisler66425862017-10-12 12:00:59 -04004421}
4422
Mingming Cao617ba132006-10-11 01:20:53 -07004423void ext4_set_inode_flags(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004424{
Mingming Cao617ba132006-10-11 01:20:53 -07004425 unsigned int flags = EXT4_I(inode)->i_flags;
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004426 unsigned int new_fl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004427
Mingming Cao617ba132006-10-11 01:20:53 -07004428 if (flags & EXT4_SYNC_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004429 new_fl |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07004430 if (flags & EXT4_APPEND_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004431 new_fl |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07004432 if (flags & EXT4_IMMUTABLE_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004433 new_fl |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07004434 if (flags & EXT4_NOATIME_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004435 new_fl |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07004436 if (flags & EXT4_DIRSYNC_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004437 new_fl |= S_DIRSYNC;
Ira Weinya8ab6d32020-05-28 07:59:58 -07004438 if (ext4_should_enable_dax(inode))
Ross Zwisler923ae0f2015-02-16 15:59:38 -08004439 new_fl |= S_DAX;
Eric Biggers2ee6a572017-10-09 12:15:35 -07004440 if (flags & EXT4_ENCRYPT_FL)
4441 new_fl |= S_ENCRYPTED;
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04004442 if (flags & EXT4_CASEFOLD_FL)
4443 new_fl |= S_CASEFOLD;
Eric Biggersc93d8f82019-07-22 09:26:24 -07004444 if (flags & EXT4_VERITY_FL)
4445 new_fl |= S_VERITY;
Theodore Ts'o5f16f322014-03-24 14:43:12 -04004446 inode_set_flags(inode, new_fl,
Eric Biggers2ee6a572017-10-09 12:15:35 -07004447 S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX|
Eric Biggersc93d8f82019-07-22 09:26:24 -07004448 S_ENCRYPTED|S_CASEFOLD|S_VERITY);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004449}
4450
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004451static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004452 struct ext4_inode_info *ei)
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004453{
4454 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004455 struct inode *inode = &(ei->vfs_inode);
4456 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004457
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004458 if (ext4_has_feature_huge_file(sb)) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004459 /* we are using combined 48 bit field */
4460 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
4461 le32_to_cpu(raw_inode->i_blocks_lo);
Theodore Ts'o07a03822010-06-14 09:54:48 -04004462 if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004463 /* i_blocks represent file system block size */
4464 return i_blocks << (inode->i_blkbits - 9);
4465 } else {
4466 return i_blocks;
4467 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004468 } else {
4469 return le32_to_cpu(raw_inode->i_blocks_lo);
4470 }
4471}
Jan Karaff9ddf72007-07-18 09:24:20 -04004472
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004473static inline int ext4_iget_extra_inode(struct inode *inode,
Tao Ma152a7b02012-12-02 11:13:24 -05004474 struct ext4_inode *raw_inode,
4475 struct ext4_inode_info *ei)
4476{
4477 __le32 *magic = (void *)raw_inode +
4478 EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004479
Eric Biggers290ab232016-12-01 14:51:58 -05004480 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize + sizeof(__le32) <=
4481 EXT4_INODE_SIZE(inode->i_sb) &&
4482 *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
Tao Ma152a7b02012-12-02 11:13:24 -05004483 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004484 return ext4_find_inline_data_nolock(inode);
Tao Maf19d5872012-12-10 14:05:51 -05004485 } else
4486 EXT4_I(inode)->i_inline_off = 0;
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004487 return 0;
Tao Ma152a7b02012-12-02 11:13:24 -05004488}
4489
Li Xi040cb372016-01-08 16:01:21 -05004490int ext4_get_projid(struct inode *inode, kprojid_t *projid)
4491{
Kaho Ng0b7b7772016-09-05 23:11:58 -04004492 if (!ext4_has_feature_project(inode->i_sb))
Li Xi040cb372016-01-08 16:01:21 -05004493 return -EOPNOTSUPP;
4494 *projid = EXT4_I(inode)->i_projid;
4495 return 0;
4496}
4497
Eryu Guane254d1a2018-05-10 11:55:31 -04004498/*
4499 * ext4 has self-managed i_version for ea inodes, it stores the lower 32bit of
4500 * refcount in i_version, so use raw values if inode has EXT4_EA_INODE_FL flag
4501 * set.
4502 */
4503static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val)
4504{
4505 if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
4506 inode_set_iversion_raw(inode, val);
4507 else
4508 inode_set_iversion_queried(inode, val);
4509}
4510static inline u64 ext4_inode_peek_iversion(const struct inode *inode)
4511{
4512 if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
4513 return inode_peek_iversion_raw(inode);
4514 else
4515 return inode_peek_iversion(inode);
4516}
4517
Theodore Ts'o8a363972018-12-19 12:29:13 -05004518struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
4519 ext4_iget_flags flags, const char *function,
4520 unsigned int line)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004521{
Mingming Cao617ba132006-10-11 01:20:53 -07004522 struct ext4_iloc iloc;
4523 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08004524 struct ext4_inode_info *ei;
David Howells1d1fe1e2008-02-07 00:15:37 -08004525 struct inode *inode;
Jan Karab436b9b2009-12-08 23:51:10 -05004526 journal_t *journal = EXT4_SB(sb)->s_journal;
David Howells1d1fe1e2008-02-07 00:15:37 -08004527 long ret;
Darrick J. Wong7e6e1ef2016-12-10 09:55:01 -05004528 loff_t size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004529 int block;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004530 uid_t i_uid;
4531 gid_t i_gid;
Li Xi040cb372016-01-08 16:01:21 -05004532 projid_t i_projid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004533
Theodore Ts'o191ce172018-12-31 22:34:31 -05004534 if ((!(flags & EXT4_IGET_SPECIAL) &&
Theodore Ts'o8a363972018-12-19 12:29:13 -05004535 (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)) ||
4536 (ino < EXT4_ROOT_INO) ||
4537 (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))) {
4538 if (flags & EXT4_IGET_HANDLE)
4539 return ERR_PTR(-ESTALE);
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04004540 __ext4_error(sb, function, line, EFSCORRUPTED, 0,
Theodore Ts'o8a363972018-12-19 12:29:13 -05004541 "inode #%lu: comm %s: iget: illegal inode #",
4542 ino, current->comm);
4543 return ERR_PTR(-EFSCORRUPTED);
4544 }
4545
David Howells1d1fe1e2008-02-07 00:15:37 -08004546 inode = iget_locked(sb, ino);
4547 if (!inode)
4548 return ERR_PTR(-ENOMEM);
4549 if (!(inode->i_state & I_NEW))
4550 return inode;
4551
4552 ei = EXT4_I(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05004553 iloc.bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004554
David Howells1d1fe1e2008-02-07 00:15:37 -08004555 ret = __ext4_get_inode_loc(inode, &iloc, 0);
4556 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004557 goto bad_inode;
Mingming Cao617ba132006-10-11 01:20:53 -07004558 raw_inode = ext4_raw_inode(&iloc);
Darrick J. Wong814525f2012-04-29 18:31:10 -04004559
Theodore Ts'o8e4b5ea2018-03-29 21:56:09 -04004560 if ((ino == EXT4_ROOT_INO) && (raw_inode->i_links_count == 0)) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004561 ext4_error_inode(inode, function, line, 0,
4562 "iget: root inode unallocated");
Theodore Ts'o8e4b5ea2018-03-29 21:56:09 -04004563 ret = -EFSCORRUPTED;
4564 goto bad_inode;
4565 }
4566
Theodore Ts'o8a363972018-12-19 12:29:13 -05004567 if ((flags & EXT4_IGET_HANDLE) &&
4568 (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) {
4569 ret = -ESTALE;
4570 goto bad_inode;
4571 }
4572
Darrick J. Wong814525f2012-04-29 18:31:10 -04004573 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4574 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
4575 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
Eric Biggers2dc8d9e2016-12-01 14:43:33 -05004576 EXT4_INODE_SIZE(inode->i_sb) ||
4577 (ei->i_extra_isize & 3)) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004578 ext4_error_inode(inode, function, line, 0,
4579 "iget: bad extra_isize %u "
4580 "(inode size %u)",
Eric Biggers2dc8d9e2016-12-01 14:43:33 -05004581 ei->i_extra_isize,
4582 EXT4_INODE_SIZE(inode->i_sb));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004583 ret = -EFSCORRUPTED;
Darrick J. Wong814525f2012-04-29 18:31:10 -04004584 goto bad_inode;
4585 }
4586 } else
4587 ei->i_extra_isize = 0;
4588
4589 /* Precompute checksum seed for inode metadata */
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04004590 if (ext4_has_metadata_csum(sb)) {
Darrick J. Wong814525f2012-04-29 18:31:10 -04004591 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4592 __u32 csum;
4593 __le32 inum = cpu_to_le32(inode->i_ino);
4594 __le32 gen = raw_inode->i_generation;
4595 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
4596 sizeof(inum));
4597 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
4598 sizeof(gen));
4599 }
4600
Theodore Ts'o46f870d62019-11-21 13:09:43 -05004601 if (!ext4_inode_csum_verify(inode, raw_inode, ei) ||
4602 ext4_simulate_fail(sb, EXT4_SIM_INODE_CRC)) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04004603 ext4_error_inode_err(inode, function, line, 0, EFSBADCRC,
4604 "iget: checksum invalid");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004605 ret = -EFSBADCRC;
Darrick J. Wong814525f2012-04-29 18:31:10 -04004606 goto bad_inode;
4607 }
4608
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004609 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004610 i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4611 i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Kaho Ng0b7b7772016-09-05 23:11:58 -04004612 if (ext4_has_feature_project(sb) &&
Li Xi040cb372016-01-08 16:01:21 -05004613 EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE &&
4614 EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
4615 i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid);
4616 else
4617 i_projid = EXT4_DEF_PROJID;
4618
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004619 if (!(test_opt(inode->i_sb, NO_UID32))) {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004620 i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4621 i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004622 }
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004623 i_uid_write(inode, i_uid);
4624 i_gid_write(inode, i_gid);
Li Xi040cb372016-01-08 16:01:21 -05004625 ei->i_projid = make_kprojid(&init_user_ns, i_projid);
Miklos Szeredibfe86842011-10-28 14:13:29 +02004626 set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004627
Theodore Ts'o353eb832011-01-10 12:18:25 -05004628 ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
Tao Ma67cf5b02012-12-10 14:04:46 -05004629 ei->i_inline_off = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004630 ei->i_dir_start_lookup = 0;
4631 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4632 /* We now have enough fields to check if the inode was active or not.
4633 * This is needed because nfsd might try to access dead inodes
4634 * the test is that same one that e2fsck uses
4635 * NeilBrown 1999oct15
4636 */
4637 if (inode->i_nlink == 0) {
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004638 if ((inode->i_mode == 0 ||
4639 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) &&
4640 ino != EXT4_BOOT_LOADER_INO) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004641 /* this inode is deleted */
David Howells1d1fe1e2008-02-07 00:15:37 -08004642 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004643 goto bad_inode;
4644 }
4645 /* The only unlinked inodes we let through here have
4646 * valid i_mode and are being read by the orphan
4647 * recovery code: that's fine, we're about to complete
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004648 * the process of deleting those.
4649 * OR it is the EXT4_BOOT_LOADER_INO which is
4650 * not initialized on a new filesystem. */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004651 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004652 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Toshi Kanicce6c9f2018-09-15 21:37:59 -04004653 ext4_set_inode_flags(inode);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004654 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004655 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004656 if (ext4_has_feature_64bit(sb))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004657 ei->i_file_acl |=
4658 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04004659 inode->i_size = ext4_isize(sb, raw_inode);
Darrick J. Wong7e6e1ef2016-12-10 09:55:01 -05004660 if ((size = i_size_read(inode)) < 0) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004661 ext4_error_inode(inode, function, line, 0,
4662 "iget: bad i_size value: %lld", size);
Darrick J. Wong7e6e1ef2016-12-10 09:55:01 -05004663 ret = -EFSCORRUPTED;
4664 goto bad_inode;
4665 }
Jan Kara48a34312020-02-10 15:43:16 +01004666 /*
4667 * If dir_index is not enabled but there's dir with INDEX flag set,
4668 * we'd normally treat htree data as empty space. But with metadata
4669 * checksumming that corrupts checksums so forbid that.
4670 */
4671 if (!ext4_has_feature_dir_index(sb) && ext4_has_metadata_csum(sb) &&
4672 ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) {
4673 ext4_error_inode(inode, function, line, 0,
4674 "iget: Dir with htree data on filesystem without dir_index feature.");
4675 ret = -EFSCORRUPTED;
4676 goto bad_inode;
4677 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004678 ei->i_disksize = inode->i_size;
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03004679#ifdef CONFIG_QUOTA
4680 ei->i_reserved_quota = 0;
4681#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004682 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4683 ei->i_block_group = iloc.block_group;
Theodore Ts'oa4912122009-03-12 12:18:34 -04004684 ei->i_last_alloc_group = ~0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004685 /*
4686 * NOTE! The in-memory inode i_data array is in little-endian order
4687 * even on big-endian machines: we do NOT byteswap the block numbers!
4688 */
Mingming Cao617ba132006-10-11 01:20:53 -07004689 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004690 ei->i_data[block] = raw_inode->i_block[block];
4691 INIT_LIST_HEAD(&ei->i_orphan);
4692
Jan Karab436b9b2009-12-08 23:51:10 -05004693 /*
4694 * Set transaction id's of transactions that have to be committed
4695 * to finish f[data]sync. We set them to currently running transaction
4696 * as we cannot be sure that the inode or some of its metadata isn't
4697 * part of the transaction - the inode could have been reclaimed and
4698 * now it is reread from disk.
4699 */
4700 if (journal) {
4701 transaction_t *transaction;
4702 tid_t tid;
4703
Theodore Ts'oa931da62010-08-03 21:35:12 -04004704 read_lock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05004705 if (journal->j_running_transaction)
4706 transaction = journal->j_running_transaction;
4707 else
4708 transaction = journal->j_committing_transaction;
4709 if (transaction)
4710 tid = transaction->t_tid;
4711 else
4712 tid = journal->j_commit_sequence;
Theodore Ts'oa931da62010-08-03 21:35:12 -04004713 read_unlock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05004714 ei->i_sync_tid = tid;
4715 ei->i_datasync_tid = tid;
4716 }
4717
Eric Sandeen0040d982008-02-05 22:36:43 -05004718 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004719 if (ei->i_extra_isize == 0) {
4720 /* The extra space is currently unused. Use it. */
Eric Biggers2dc8d9e2016-12-01 14:43:33 -05004721 BUILD_BUG_ON(sizeof(struct ext4_inode) & 3);
Mingming Cao617ba132006-10-11 01:20:53 -07004722 ei->i_extra_isize = sizeof(struct ext4_inode) -
4723 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004724 } else {
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004725 ret = ext4_iget_extra_inode(inode, raw_inode, ei);
4726 if (ret)
4727 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004728 }
Darrick J. Wong814525f2012-04-29 18:31:10 -04004729 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004730
Kalpak Shahef7f3832007-07-18 09:15:20 -04004731 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4732 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4733 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4734 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4735
Theodore Ts'oed3654e2014-03-24 14:09:06 -04004736 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
Jeff Laytonee73f9a2018-01-09 08:21:39 -05004737 u64 ivers = le32_to_cpu(raw_inode->i_disk_version);
4738
Theodore Ts'oc4f65702014-03-20 00:32:57 -04004739 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4740 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
Jeff Laytonee73f9a2018-01-09 08:21:39 -05004741 ivers |=
Theodore Ts'oc4f65702014-03-20 00:32:57 -04004742 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
4743 }
Eryu Guane254d1a2018-05-10 11:55:31 -04004744 ext4_inode_set_iversion_queried(inode, ivers);
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004745 }
4746
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004747 ret = 0;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004748 if (ei->i_file_acl &&
Theodore Ts'o10329882009-11-15 15:29:56 -05004749 !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004750 ext4_error_inode(inode, function, line, 0,
4751 "iget: bad extended attribute block %llu",
Theodore Ts'o24676da2010-05-16 21:00:00 -04004752 ei->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004753 ret = -EFSCORRUPTED;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004754 goto bad_inode;
Tao Maf19d5872012-12-10 14:05:51 -05004755 } else if (!ext4_has_inline_data(inode)) {
Liu Songbc716522018-08-02 00:11:16 -04004756 /* validate the block references in the inode */
4757 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4758 (S_ISLNK(inode->i_mode) &&
4759 !ext4_inode_is_fast_symlink(inode))) {
4760 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Tao Maf19d5872012-12-10 14:05:51 -05004761 ret = ext4_ext_check_inode(inode);
Liu Songbc716522018-08-02 00:11:16 -04004762 else
4763 ret = ext4_ind_check_inode(inode);
Tao Maf19d5872012-12-10 14:05:51 -05004764 }
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004765 }
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004766 if (ret)
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004767 goto bad_inode;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -04004768
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004769 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004770 inode->i_op = &ext4_file_inode_operations;
Boaz Harroshbe64f882015-04-15 16:15:17 -07004771 inode->i_fop = &ext4_file_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07004772 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004773 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004774 inode->i_op = &ext4_dir_inode_operations;
4775 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004776 } else if (S_ISLNK(inode->i_mode)) {
Luis R. Rodriguez6390d332018-05-13 16:45:56 -04004777 /* VFS does not allow setting these so must be corruption */
4778 if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004779 ext4_error_inode(inode, function, line, 0,
4780 "iget: immutable or append flags "
4781 "not allowed on symlinks");
Luis R. Rodriguez6390d332018-05-13 16:45:56 -04004782 ret = -EFSCORRUPTED;
4783 goto bad_inode;
4784 }
Chandan Rajendra592ddec2018-12-12 15:20:10 +05304785 if (IS_ENCRYPTED(inode)) {
Al Viroa7a67e82015-04-27 17:51:30 -04004786 inode->i_op = &ext4_encrypted_symlink_inode_operations;
4787 ext4_set_aops(inode);
4788 } else if (ext4_inode_is_fast_symlink(inode)) {
Al Viro75e75662015-05-02 10:13:58 -04004789 inode->i_link = (char *)ei->i_data;
Mingming Cao617ba132006-10-11 01:20:53 -07004790 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00004791 nd_terminate_link(ei->i_data, inode->i_size,
4792 sizeof(ei->i_data) - 1);
4793 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004794 inode->i_op = &ext4_symlink_inode_operations;
4795 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004796 }
Al Viro21fc61c2015-11-17 01:07:57 -05004797 inode_nohighmem(inode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004798 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
4799 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004800 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004801 if (raw_inode->i_block[0])
4802 init_special_inode(inode, inode->i_mode,
4803 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4804 else
4805 init_special_inode(inode, inode->i_mode,
4806 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004807 } else if (ino == EXT4_BOOT_LOADER_INO) {
4808 make_bad_inode(inode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004809 } else {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004810 ret = -EFSCORRUPTED;
Theodore Ts'o8a363972018-12-19 12:29:13 -05004811 ext4_error_inode(inode, function, line, 0,
4812 "iget: bogus i_mode (%o)", inode->i_mode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004813 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004814 }
Theodore Ts'o6456ca62019-09-03 01:43:17 -04004815 if (IS_CASEFOLDED(inode) && !ext4_has_feature_casefold(inode->i_sb))
4816 ext4_error_inode(inode, function, line, 0,
4817 "casefold flag without casefold feature");
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004818 brelse(iloc.bh);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04004819
David Howells1d1fe1e2008-02-07 00:15:37 -08004820 unlock_new_inode(inode);
4821 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004822
4823bad_inode:
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004824 brelse(iloc.bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004825 iget_failed(inode);
4826 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004827}
4828
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004829static int ext4_inode_blocks_set(handle_t *handle,
4830 struct ext4_inode *raw_inode,
4831 struct ext4_inode_info *ei)
4832{
4833 struct inode *inode = &(ei->vfs_inode);
Qian Cai28936b62020-02-21 23:32:58 -05004834 u64 i_blocks = READ_ONCE(inode->i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004835 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004836
4837 if (i_blocks <= ~0U) {
4838 /*
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004839 * i_blocks can be represented in a 32 bit variable
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004840 * as multiple of 512 bytes
4841 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004842 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004843 raw_inode->i_blocks_high = 0;
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004844 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004845 return 0;
4846 }
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004847 if (!ext4_has_feature_huge_file(sb))
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004848 return -EFBIG;
4849
4850 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004851 /*
4852 * i_blocks can be represented in a 48 bit variable
4853 * as multiple of 512 bytes
4854 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004855 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004856 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004857 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004858 } else {
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004859 ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004860 /* i_block is stored in file system block size */
4861 i_blocks = i_blocks >> (inode->i_blkbits - 9);
4862 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4863 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004864 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004865 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004866}
4867
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004868struct other_inode {
4869 unsigned long orig_ino;
4870 struct ext4_inode *raw_inode;
4871};
4872
4873static int other_inode_match(struct inode * inode, unsigned long ino,
4874 void *data)
4875{
4876 struct other_inode *oi = (struct other_inode *) data;
4877
4878 if ((inode->i_ino != ino) ||
4879 (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW |
Christoph Hellwig0e11f642018-02-21 07:54:49 -08004880 I_DIRTY_INODE)) ||
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004881 ((inode->i_state & I_DIRTY_TIME) == 0))
4882 return 0;
4883 spin_lock(&inode->i_lock);
4884 if (((inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW |
Christoph Hellwig0e11f642018-02-21 07:54:49 -08004885 I_DIRTY_INODE)) == 0) &&
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004886 (inode->i_state & I_DIRTY_TIME)) {
4887 struct ext4_inode_info *ei = EXT4_I(inode);
4888
4889 inode->i_state &= ~(I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED);
4890 spin_unlock(&inode->i_lock);
4891
4892 spin_lock(&ei->i_raw_lock);
4893 EXT4_INODE_SET_XTIME(i_ctime, inode, oi->raw_inode);
4894 EXT4_INODE_SET_XTIME(i_mtime, inode, oi->raw_inode);
4895 EXT4_INODE_SET_XTIME(i_atime, inode, oi->raw_inode);
4896 ext4_inode_csum_set(inode, oi->raw_inode, ei);
4897 spin_unlock(&ei->i_raw_lock);
4898 trace_ext4_other_inode_update_time(inode, oi->orig_ino);
4899 return -1;
4900 }
4901 spin_unlock(&inode->i_lock);
4902 return -1;
4903}
4904
4905/*
4906 * Opportunistically update the other time fields for other inodes in
4907 * the same inode table block.
4908 */
4909static void ext4_update_other_inodes_time(struct super_block *sb,
4910 unsigned long orig_ino, char *buf)
4911{
4912 struct other_inode oi;
4913 unsigned long ino;
4914 int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
4915 int inode_size = EXT4_INODE_SIZE(sb);
4916
4917 oi.orig_ino = orig_ino;
Theodore Ts'o0f0ff9a2015-07-01 23:37:46 -04004918 /*
4919 * Calculate the first inode in the inode table block. Inode
4920 * numbers are one-based. That is, the first inode in a block
4921 * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1).
4922 */
4923 ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1;
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004924 for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) {
4925 if (ino == orig_ino)
4926 continue;
4927 oi.raw_inode = (struct ext4_inode *) buf;
4928 (void) find_inode_nowait(sb, ino, other_inode_match, &oi);
4929 }
4930}
4931
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004932/*
4933 * Post the struct inode info into an on-disk inode location in the
4934 * buffer-cache. This gobbles the caller's reference to the
4935 * buffer_head in the inode location struct.
4936 *
4937 * The caller must have write access to iloc->bh.
4938 */
Mingming Cao617ba132006-10-11 01:20:53 -07004939static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004940 struct inode *inode,
Frank Mayhar830156c2009-09-29 10:07:47 -04004941 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004942{
Mingming Cao617ba132006-10-11 01:20:53 -07004943 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
4944 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004945 struct buffer_head *bh = iloc->bh;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004946 struct super_block *sb = inode->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004947 int err = 0, rc, block;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004948 int need_datasync = 0, set_large_file = 0;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004949 uid_t i_uid;
4950 gid_t i_gid;
Li Xi040cb372016-01-08 16:01:21 -05004951 projid_t i_projid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004952
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004953 spin_lock(&ei->i_raw_lock);
4954
4955 /* For fields not tracked in the in-memory inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004956 * initialise them to zero for new inodes. */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004957 if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
Mingming Cao617ba132006-10-11 01:20:53 -07004958 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004959
4960 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004961 i_uid = i_uid_read(inode);
4962 i_gid = i_gid_read(inode);
Li Xi040cb372016-01-08 16:01:21 -05004963 i_projid = from_kprojid(&init_user_ns, ei->i_projid);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004964 if (!(test_opt(inode->i_sb, NO_UID32))) {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004965 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid));
4966 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004967/*
4968 * Fix up interoperability with old kernels. Otherwise, old inodes get
4969 * re-used with the upper 16 bits of the uid/gid intact
4970 */
Daeho Jeong93e3b4e2016-09-05 22:56:10 -04004971 if (ei->i_dtime && list_empty(&ei->i_orphan)) {
4972 raw_inode->i_uid_high = 0;
4973 raw_inode->i_gid_high = 0;
4974 } else {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004975 raw_inode->i_uid_high =
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004976 cpu_to_le16(high_16_bits(i_uid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004977 raw_inode->i_gid_high =
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004978 cpu_to_le16(high_16_bits(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004979 }
4980 } else {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004981 raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid));
4982 raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004983 raw_inode->i_uid_high = 0;
4984 raw_inode->i_gid_high = 0;
4985 }
4986 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004987
4988 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
4989 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
4990 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
4991 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
4992
Li Xibce92d52014-10-01 22:11:06 -04004993 err = ext4_inode_blocks_set(handle, raw_inode, ei);
4994 if (err) {
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004995 spin_unlock(&ei->i_raw_lock);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004996 goto out_brelse;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004997 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004998 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Theodore Ts'o353eb832011-01-10 12:18:25 -05004999 raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
Theodore Ts'oed3654e2014-03-24 14:09:06 -04005000 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT)))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07005001 raw_inode->i_file_acl_high =
5002 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05005003 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Qiujun Huangdce8e232020-02-24 23:02:46 +08005004 if (READ_ONCE(ei->i_disksize) != ext4_isize(inode->i_sb, raw_inode)) {
Jan Karab71fc072012-09-26 21:52:20 -04005005 ext4_isize_set(raw_inode, ei->i_disksize);
5006 need_datasync = 1;
5007 }
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05005008 if (ei->i_disksize > 0x7fffffffULL) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005009 if (!ext4_has_feature_large_file(sb) ||
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05005010 EXT4_SB(sb)->s_es->s_rev_level ==
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005011 cpu_to_le32(EXT4_GOOD_OLD_REV))
5012 set_large_file = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005013 }
5014 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
5015 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
5016 if (old_valid_dev(inode->i_rdev)) {
5017 raw_inode->i_block[0] =
5018 cpu_to_le32(old_encode_dev(inode->i_rdev));
5019 raw_inode->i_block[1] = 0;
5020 } else {
5021 raw_inode->i_block[0] = 0;
5022 raw_inode->i_block[1] =
5023 cpu_to_le32(new_encode_dev(inode->i_rdev));
5024 raw_inode->i_block[2] = 0;
5025 }
Tao Maf19d5872012-12-10 14:05:51 -05005026 } else if (!ext4_has_inline_data(inode)) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04005027 for (block = 0; block < EXT4_N_BLOCKS; block++)
5028 raw_inode->i_block[block] = ei->i_data[block];
Tao Maf19d5872012-12-10 14:05:51 -05005029 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005030
Theodore Ts'oed3654e2014-03-24 14:09:06 -04005031 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
Eryu Guane254d1a2018-05-10 11:55:31 -04005032 u64 ivers = ext4_inode_peek_iversion(inode);
Jeff Laytonee73f9a2018-01-09 08:21:39 -05005033
5034 raw_inode->i_disk_version = cpu_to_le32(ivers);
Theodore Ts'oc4f65702014-03-20 00:32:57 -04005035 if (ei->i_extra_isize) {
5036 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
5037 raw_inode->i_version_hi =
Jeff Laytonee73f9a2018-01-09 08:21:39 -05005038 cpu_to_le32(ivers >> 32);
Theodore Ts'oc4f65702014-03-20 00:32:57 -04005039 raw_inode->i_extra_isize =
5040 cpu_to_le16(ei->i_extra_isize);
5041 }
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005042 }
Li Xi040cb372016-01-08 16:01:21 -05005043
Kaho Ng0b7b7772016-09-05 23:11:58 -04005044 BUG_ON(!ext4_has_feature_project(inode->i_sb) &&
Li Xi040cb372016-01-08 16:01:21 -05005045 i_projid != EXT4_DEF_PROJID);
5046
5047 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
5048 EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
5049 raw_inode->i_projid = cpu_to_le32(i_projid);
5050
Darrick J. Wong814525f2012-04-29 18:31:10 -04005051 ext4_inode_csum_set(inode, raw_inode, ei);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005052 spin_unlock(&ei->i_raw_lock);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08005053 if (inode->i_sb->s_flags & SB_LAZYTIME)
Theodore Ts'oa26f4992015-02-02 00:37:02 -05005054 ext4_update_other_inodes_time(inode->i_sb, inode->i_ino,
5055 bh->b_data);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005056
Frank Mayhar830156c2009-09-29 10:07:47 -04005057 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05005058 rc = ext4_handle_dirty_metadata(handle, NULL, bh);
Frank Mayhar830156c2009-09-29 10:07:47 -04005059 if (!err)
5060 err = rc;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005061 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005062 if (set_large_file) {
liang xie5d601252014-05-12 22:06:43 -04005063 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access");
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005064 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
5065 if (err)
5066 goto out_brelse;
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005067 ext4_set_feature_large_file(sb);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005068 ext4_handle_sync(handle);
5069 err = ext4_handle_dirty_super(handle, sb);
5070 }
Jan Karab71fc072012-09-26 21:52:20 -04005071 ext4_update_inode_fsync_trans(handle, inode, need_datasync);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005072out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04005073 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07005074 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005075 return err;
5076}
5077
5078/*
Mingming Cao617ba132006-10-11 01:20:53 -07005079 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005080 *
5081 * We are called from a few places:
5082 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005083 * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005084 * Here, there will be no transaction running. We wait for any running
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005085 * transaction to commit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005086 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005087 * - Within flush work (sys_sync(), kupdate and such).
5088 * We wait on commit, if told to.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005089 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005090 * - Within iput_final() -> write_inode_now()
5091 * We wait on commit, if told to.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005092 *
5093 * In all cases it is actually safe for us to return without doing anything,
5094 * because the inode has been copied into a raw inode buffer in
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005095 * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL
5096 * writeback.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005097 *
5098 * Note that we are absolutely dependent upon all inode dirtiers doing the
5099 * right thing: they *must* call mark_inode_dirty() after dirtying info in
5100 * which we are interested.
5101 *
5102 * It would be a bug for them to not do this. The code:
5103 *
5104 * mark_inode_dirty(inode)
5105 * stuff();
5106 * inode->i_size = expr;
5107 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005108 * is in error because write_inode() could occur while `stuff()' is running,
5109 * and the new i_size will be lost. Plus the inode will no longer be on the
5110 * superblock's dirty inode list.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005111 */
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005112int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005113{
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005114 int err;
5115
Theodore Ts'o18f2c4f2018-12-19 14:36:58 -05005116 if (WARN_ON_ONCE(current->flags & PF_MEMALLOC) ||
5117 sb_rdonly(inode->i_sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005118 return 0;
5119
Theodore Ts'o18f2c4f2018-12-19 14:36:58 -05005120 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5121 return -EIO;
5122
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005123 if (EXT4_SB(inode->i_sb)->s_journal) {
5124 if (ext4_journal_current_handle()) {
5125 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
5126 dump_stack();
5127 return -EIO;
5128 }
5129
Jan Kara10542c22014-03-04 10:50:50 -05005130 /*
5131 * No need to force transaction in WB_SYNC_NONE mode. Also
5132 * ext4_sync_fs() will force the commit after everything is
5133 * written.
5134 */
5135 if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync)
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005136 return 0;
5137
Theodore Ts'o18f2c4f2018-12-19 14:36:58 -05005138 err = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
5139 EXT4_I(inode)->i_sync_tid);
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005140 } else {
5141 struct ext4_iloc iloc;
5142
Curt Wohlgemuth8b472d72010-04-03 16:45:06 -04005143 err = __ext4_get_inode_loc(inode, &iloc, 0);
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005144 if (err)
5145 return err;
Jan Kara10542c22014-03-04 10:50:50 -05005146 /*
5147 * sync(2) will flush the whole buffer cache. No need to do
5148 * it here separately for each inode.
5149 */
5150 if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
Frank Mayhar830156c2009-09-29 10:07:47 -04005151 sync_dirty_buffer(iloc.bh);
5152 if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04005153 ext4_error_inode_block(inode, iloc.bh->b_blocknr, EIO,
5154 "IO error syncing inode");
Frank Mayhar830156c2009-09-29 10:07:47 -04005155 err = -EIO;
5156 }
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04005157 brelse(iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005158 }
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005159 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005160}
5161
5162/*
Jan Kara53e87262012-12-25 13:29:52 -05005163 * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
5164 * buffers that are attached to a page stradding i_size and are undergoing
5165 * commit. In that case we have to wait for commit to finish and try again.
5166 */
5167static void ext4_wait_for_tail_page_commit(struct inode *inode)
5168{
5169 struct page *page;
5170 unsigned offset;
5171 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
5172 tid_t commit_tid = 0;
5173 int ret;
5174
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005175 offset = inode->i_size & (PAGE_SIZE - 1);
Jan Kara53e87262012-12-25 13:29:52 -05005176 /*
yangerkun565333a2019-09-19 14:35:08 +08005177 * If the page is fully truncated, we don't need to wait for any commit
5178 * (and we even should not as __ext4_journalled_invalidatepage() may
5179 * strip all buffers from the page but keep the page dirty which can then
5180 * confuse e.g. concurrent ext4_writepage() seeing dirty page without
5181 * buffers). Also we don't need to wait for any commit if all buffers in
5182 * the page remain valid. This is most beneficial for the common case of
5183 * blocksize == PAGESIZE.
Jan Kara53e87262012-12-25 13:29:52 -05005184 */
yangerkun565333a2019-09-19 14:35:08 +08005185 if (!offset || offset > (PAGE_SIZE - i_blocksize(inode)))
Jan Kara53e87262012-12-25 13:29:52 -05005186 return;
5187 while (1) {
5188 page = find_lock_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005189 inode->i_size >> PAGE_SHIFT);
Jan Kara53e87262012-12-25 13:29:52 -05005190 if (!page)
5191 return;
Lukas Czernerca99fdd2013-05-21 23:25:01 -04005192 ret = __ext4_journalled_invalidatepage(page, offset,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005193 PAGE_SIZE - offset);
Jan Kara53e87262012-12-25 13:29:52 -05005194 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005195 put_page(page);
Jan Kara53e87262012-12-25 13:29:52 -05005196 if (ret != -EBUSY)
5197 return;
5198 commit_tid = 0;
5199 read_lock(&journal->j_state_lock);
5200 if (journal->j_committing_transaction)
5201 commit_tid = journal->j_committing_transaction->t_tid;
5202 read_unlock(&journal->j_state_lock);
5203 if (commit_tid)
5204 jbd2_log_wait_commit(journal, commit_tid);
5205 }
5206}
5207
5208/*
Mingming Cao617ba132006-10-11 01:20:53 -07005209 * ext4_setattr()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005210 *
5211 * Called from notify_change.
5212 *
5213 * We want to trap VFS attempts to truncate the file as soon as
5214 * possible. In particular, we want to make sure that when the VFS
5215 * shrinks i_size, we put the inode on the orphan list and modify
5216 * i_disksize immediately, so that during the subsequent flushing of
5217 * dirty pages and freeing of disk blocks, we can guarantee that any
5218 * commit will leave the blocks being flushed in an unused state on
5219 * disk. (On recovery, the inode will get truncated and the blocks will
5220 * be freed, so we have a strong guarantee that no future commit will
5221 * leave these blocks visible to the user.)
5222 *
Jan Kara678aaf42008-07-11 19:27:31 -04005223 * Another thing we have to assure is that if we are in ordered mode
5224 * and inode is still attached to the committing transaction, we must
5225 * we start writeout of all the dirty pages which are being truncated.
5226 * This way we are sure that all the data written in the previous
5227 * transaction are already on disk (truncate waits for pages under
5228 * writeback).
5229 *
5230 * Called with inode->i_mutex down.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005231 */
Mingming Cao617ba132006-10-11 01:20:53 -07005232int ext4_setattr(struct dentry *dentry, struct iattr *attr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005233{
David Howells2b0143b2015-03-17 22:25:59 +00005234 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005235 int error, rc = 0;
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04005236 int orphan = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005237 const unsigned int ia_valid = attr->ia_valid;
5238
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05005239 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5240 return -EIO;
5241
Theodore Ts'o02b016c2019-06-09 22:04:33 -04005242 if (unlikely(IS_IMMUTABLE(inode)))
5243 return -EPERM;
5244
5245 if (unlikely(IS_APPEND(inode) &&
5246 (ia_valid & (ATTR_MODE | ATTR_UID |
5247 ATTR_GID | ATTR_TIMES_SET))))
5248 return -EPERM;
5249
Jan Kara31051c82016-05-26 16:55:18 +02005250 error = setattr_prepare(dentry, attr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005251 if (error)
5252 return error;
5253
Eric Biggers3ce2b8d2017-10-18 20:21:58 -04005254 error = fscrypt_prepare_setattr(dentry, attr);
5255 if (error)
5256 return error;
5257
Eric Biggersc93d8f82019-07-22 09:26:24 -07005258 error = fsverity_prepare_setattr(dentry, attr);
5259 if (error)
5260 return error;
5261
Jan Karaa7cdade2015-06-29 16:22:54 +02005262 if (is_quota_modification(inode, attr)) {
5263 error = dquot_initialize(inode);
5264 if (error)
5265 return error;
5266 }
Eric W. Biederman08cefc72012-02-07 15:41:49 -08005267 if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
5268 (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005269 handle_t *handle;
5270
5271 /* (user+group)*(old+new) structure, inode write (sb,
5272 * inode block, ? - but truncate inode update has it) */
Theodore Ts'o9924a922013-02-08 21:59:22 -05005273 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
5274 (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) +
5275 EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005276 if (IS_ERR(handle)) {
5277 error = PTR_ERR(handle);
5278 goto err_out;
5279 }
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -04005280
5281 /* dquot_transfer() calls back ext4_get_inode_usage() which
5282 * counts xattr inode references.
5283 */
5284 down_read(&EXT4_I(inode)->xattr_sem);
Christoph Hellwigb43fa822010-03-03 09:05:03 -05005285 error = dquot_transfer(inode, attr);
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -04005286 up_read(&EXT4_I(inode)->xattr_sem);
5287
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005288 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07005289 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005290 return error;
5291 }
5292 /* Update corresponding info in inode so that everything is in
5293 * one transaction */
5294 if (attr->ia_valid & ATTR_UID)
5295 inode->i_uid = attr->ia_uid;
5296 if (attr->ia_valid & ATTR_GID)
5297 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07005298 error = ext4_mark_inode_dirty(handle, inode);
5299 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005300 }
5301
Josef Bacik3da40c72015-06-22 00:31:26 -04005302 if (attr->ia_valid & ATTR_SIZE) {
Jan Kara52083862013-08-17 10:07:17 -04005303 handle_t *handle;
Josef Bacik3da40c72015-06-22 00:31:26 -04005304 loff_t oldsize = inode->i_size;
Jan Karab9c1c262019-05-30 11:56:23 -04005305 int shrink = (attr->ia_size < inode->i_size);
Christoph Hellwig562c72aa52011-06-24 14:29:45 -04005306
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005307 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
Eric Sandeene2b46572008-01-28 23:58:27 -05005308 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5309
Theodore Ts'o0c095c72010-07-27 11:56:06 -04005310 if (attr->ia_size > sbi->s_bitmap_maxbytes)
5311 return -EFBIG;
Eric Sandeene2b46572008-01-28 23:58:27 -05005312 }
Josef Bacik3da40c72015-06-22 00:31:26 -04005313 if (!S_ISREG(inode->i_mode))
5314 return -EINVAL;
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005315
5316 if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size)
5317 inode_inc_iversion(inode);
5318
Jan Karab9c1c262019-05-30 11:56:23 -04005319 if (shrink) {
5320 if (ext4_should_order_data(inode)) {
5321 error = ext4_begin_ordered_truncate(inode,
Jan Kara678aaf42008-07-11 19:27:31 -04005322 attr->ia_size);
Jan Karab9c1c262019-05-30 11:56:23 -04005323 if (error)
5324 goto err_out;
5325 }
5326 /*
5327 * Blocks are going to be removed from the inode. Wait
5328 * for dio in flight.
5329 */
5330 inode_dio_wait(inode);
Josef Bacik3da40c72015-06-22 00:31:26 -04005331 }
Jan Karab9c1c262019-05-30 11:56:23 -04005332
5333 down_write(&EXT4_I(inode)->i_mmap_sem);
5334
5335 rc = ext4_break_layouts(inode);
5336 if (rc) {
5337 up_write(&EXT4_I(inode)->i_mmap_sem);
5338 return rc;
5339 }
5340
Josef Bacik3da40c72015-06-22 00:31:26 -04005341 if (attr->ia_size != inode->i_size) {
Jan Kara52083862013-08-17 10:07:17 -04005342 handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
5343 if (IS_ERR(handle)) {
5344 error = PTR_ERR(handle);
Jan Karab9c1c262019-05-30 11:56:23 -04005345 goto out_mmap_sem;
Jan Kara52083862013-08-17 10:07:17 -04005346 }
Josef Bacik3da40c72015-06-22 00:31:26 -04005347 if (ext4_handle_valid(handle) && shrink) {
Jan Kara52083862013-08-17 10:07:17 -04005348 error = ext4_orphan_add(handle, inode);
5349 orphan = 1;
5350 }
Eryu Guan911af572015-07-28 15:08:41 -04005351 /*
5352 * Update c/mtime on truncate up, ext4_truncate() will
5353 * update c/mtime in shrink case below
5354 */
5355 if (!shrink) {
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005356 inode->i_mtime = current_time(inode);
Eryu Guan911af572015-07-28 15:08:41 -04005357 inode->i_ctime = inode->i_mtime;
5358 }
Jan Kara90e775b2013-08-17 10:09:31 -04005359 down_write(&EXT4_I(inode)->i_data_sem);
Jan Kara52083862013-08-17 10:07:17 -04005360 EXT4_I(inode)->i_disksize = attr->ia_size;
5361 rc = ext4_mark_inode_dirty(handle, inode);
5362 if (!error)
5363 error = rc;
Jan Kara90e775b2013-08-17 10:09:31 -04005364 /*
5365 * We have to update i_size under i_data_sem together
5366 * with i_disksize to avoid races with writeback code
5367 * running ext4_wb_update_i_disksize().
5368 */
5369 if (!error)
5370 i_size_write(inode, attr->ia_size);
5371 up_write(&EXT4_I(inode)->i_data_sem);
Jan Kara52083862013-08-17 10:07:17 -04005372 ext4_journal_stop(handle);
Jan Karab9c1c262019-05-30 11:56:23 -04005373 if (error)
5374 goto out_mmap_sem;
5375 if (!shrink) {
5376 pagecache_isize_extended(inode, oldsize,
5377 inode->i_size);
5378 } else if (ext4_should_journal_data(inode)) {
5379 ext4_wait_for_tail_page_commit(inode);
Jan Kara678aaf42008-07-11 19:27:31 -04005380 }
Jan Karad6320cb2014-10-01 21:49:46 -04005381 }
Ross Zwisler430657b2018-07-29 17:00:22 -04005382
Jan Kara52083862013-08-17 10:07:17 -04005383 /*
5384 * Truncate pagecache after we've waited for commit
5385 * in data=journal mode to make pages freeable.
5386 */
Ross Zwisler923ae0f2015-02-16 15:59:38 -08005387 truncate_pagecache(inode, inode->i_size);
Jan Karab9c1c262019-05-30 11:56:23 -04005388 /*
5389 * Call ext4_truncate() even if i_size didn't change to
5390 * truncate possible preallocated blocks.
5391 */
5392 if (attr->ia_size <= oldsize) {
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05005393 rc = ext4_truncate(inode);
5394 if (rc)
5395 error = rc;
5396 }
Jan Karab9c1c262019-05-30 11:56:23 -04005397out_mmap_sem:
Jan Karaea3d7202015-12-07 14:28:03 -05005398 up_write(&EXT4_I(inode)->i_mmap_sem);
Theodore Ts'o072bd7e2011-05-23 15:13:02 -04005399 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005400
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05005401 if (!error) {
Christoph Hellwig10257742010-06-04 11:30:02 +02005402 setattr_copy(inode, attr);
5403 mark_inode_dirty(inode);
5404 }
5405
5406 /*
5407 * If the call to ext4_truncate failed to get a transaction handle at
5408 * all, we need to clean up the in-core orphan list manually.
5409 */
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04005410 if (orphan && inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07005411 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005412
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05005413 if (!error && (ia_valid & ATTR_MODE))
Christoph Hellwig64e178a2013-12-20 05:16:44 -08005414 rc = posix_acl_chmod(inode, inode->i_mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005415
5416err_out:
Mingming Cao617ba132006-10-11 01:20:53 -07005417 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005418 if (!error)
5419 error = rc;
5420 return error;
5421}
5422
David Howellsa528d352017-01-31 16:46:22 +00005423int ext4_getattr(const struct path *path, struct kstat *stat,
5424 u32 request_mask, unsigned int query_flags)
Mingming Cao3e3398a2008-07-11 19:27:31 -04005425{
David Howells99652ea2017-03-31 18:31:56 +01005426 struct inode *inode = d_inode(path->dentry);
5427 struct ext4_inode *raw_inode;
5428 struct ext4_inode_info *ei = EXT4_I(inode);
5429 unsigned int flags;
Mingming Cao3e3398a2008-07-11 19:27:31 -04005430
Theodore Ts'od4c5e962019-11-28 22:26:51 -05005431 if ((request_mask & STATX_BTIME) &&
5432 EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime)) {
David Howells99652ea2017-03-31 18:31:56 +01005433 stat->result_mask |= STATX_BTIME;
5434 stat->btime.tv_sec = ei->i_crtime.tv_sec;
5435 stat->btime.tv_nsec = ei->i_crtime.tv_nsec;
5436 }
5437
5438 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
5439 if (flags & EXT4_APPEND_FL)
5440 stat->attributes |= STATX_ATTR_APPEND;
5441 if (flags & EXT4_COMPR_FL)
5442 stat->attributes |= STATX_ATTR_COMPRESSED;
5443 if (flags & EXT4_ENCRYPT_FL)
5444 stat->attributes |= STATX_ATTR_ENCRYPTED;
5445 if (flags & EXT4_IMMUTABLE_FL)
5446 stat->attributes |= STATX_ATTR_IMMUTABLE;
5447 if (flags & EXT4_NODUMP_FL)
5448 stat->attributes |= STATX_ATTR_NODUMP;
Eric Biggers1f607192019-10-29 13:41:39 -07005449 if (flags & EXT4_VERITY_FL)
5450 stat->attributes |= STATX_ATTR_VERITY;
David Howells99652ea2017-03-31 18:31:56 +01005451
David Howells3209f682017-03-31 18:32:17 +01005452 stat->attributes_mask |= (STATX_ATTR_APPEND |
5453 STATX_ATTR_COMPRESSED |
5454 STATX_ATTR_ENCRYPTED |
5455 STATX_ATTR_IMMUTABLE |
Eric Biggers1f607192019-10-29 13:41:39 -07005456 STATX_ATTR_NODUMP |
5457 STATX_ATTR_VERITY);
David Howells3209f682017-03-31 18:32:17 +01005458
Mingming Cao3e3398a2008-07-11 19:27:31 -04005459 generic_fillattr(inode, stat);
David Howells99652ea2017-03-31 18:31:56 +01005460 return 0;
5461}
5462
5463int ext4_file_getattr(const struct path *path, struct kstat *stat,
5464 u32 request_mask, unsigned int query_flags)
5465{
5466 struct inode *inode = d_inode(path->dentry);
5467 u64 delalloc_blocks;
5468
5469 ext4_getattr(path, stat, request_mask, query_flags);
Mingming Cao3e3398a2008-07-11 19:27:31 -04005470
5471 /*
Andreas Dilger9206c562013-11-11 22:38:12 -05005472 * If there is inline data in the inode, the inode will normally not
5473 * have data blocks allocated (it may have an external xattr block).
5474 * Report at least one sector for such files, so tools like tar, rsync,
Theodore Ts'od67d64f2017-03-25 17:33:31 -04005475 * others don't incorrectly think the file is completely sparse.
Andreas Dilger9206c562013-11-11 22:38:12 -05005476 */
5477 if (unlikely(ext4_has_inline_data(inode)))
5478 stat->blocks += (stat->size + 511) >> 9;
5479
5480 /*
Mingming Cao3e3398a2008-07-11 19:27:31 -04005481 * We can't update i_blocks if the block allocation is delayed
5482 * otherwise in the case of system crash before the real block
5483 * allocation is done, we will have i_blocks inconsistent with
5484 * on-disk file blocks.
5485 * We always keep i_blocks updated together with real
5486 * allocation. But to not confuse with user, stat
5487 * will return the blocks that include the delayed allocation
5488 * blocks for this file.
5489 */
Tao Ma96607552012-05-31 22:54:16 -04005490 delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
Andreas Dilger9206c562013-11-11 22:38:12 -05005491 EXT4_I(inode)->i_reserved_data_blocks);
5492 stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9);
Mingming Cao3e3398a2008-07-11 19:27:31 -04005493 return 0;
5494}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005495
Jan Karafffb2732013-06-04 13:01:11 -04005496static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
5497 int pextents)
Mingming Caoa02908f2008-08-19 22:16:07 -04005498{
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005499 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Jan Karafffb2732013-06-04 13:01:11 -04005500 return ext4_ind_trans_blocks(inode, lblocks);
5501 return ext4_ext_index_trans_blocks(inode, pextents);
Mingming Caoa02908f2008-08-19 22:16:07 -04005502}
Theodore Ts'oac51d832008-11-06 16:49:36 -05005503
Mingming Caoa02908f2008-08-19 22:16:07 -04005504/*
5505 * Account for index blocks, block groups bitmaps and block group
5506 * descriptor blocks if modify datablocks and index blocks
5507 * worse case, the indexs blocks spread over different block groups
5508 *
5509 * If datablocks are discontiguous, they are possible to spread over
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005510 * different block groups too. If they are contiguous, with flexbg,
Mingming Caoa02908f2008-08-19 22:16:07 -04005511 * they could still across block group boundary.
5512 *
5513 * Also account for superblock, inode, quota and xattr blocks
5514 */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04005515static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
Jan Karafffb2732013-06-04 13:01:11 -04005516 int pextents)
Mingming Caoa02908f2008-08-19 22:16:07 -04005517{
Theodore Ts'o8df96752009-05-01 08:50:38 -04005518 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
5519 int gdpblocks;
Mingming Caoa02908f2008-08-19 22:16:07 -04005520 int idxblocks;
5521 int ret = 0;
5522
5523 /*
Jan Karafffb2732013-06-04 13:01:11 -04005524 * How many index blocks need to touch to map @lblocks logical blocks
5525 * to @pextents physical extents?
Mingming Caoa02908f2008-08-19 22:16:07 -04005526 */
Jan Karafffb2732013-06-04 13:01:11 -04005527 idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents);
Mingming Caoa02908f2008-08-19 22:16:07 -04005528
5529 ret = idxblocks;
5530
5531 /*
5532 * Now let's see how many group bitmaps and group descriptors need
5533 * to account
5534 */
Jan Karafffb2732013-06-04 13:01:11 -04005535 groups = idxblocks + pextents;
Mingming Caoa02908f2008-08-19 22:16:07 -04005536 gdpblocks = groups;
Theodore Ts'o8df96752009-05-01 08:50:38 -04005537 if (groups > ngroups)
5538 groups = ngroups;
Mingming Caoa02908f2008-08-19 22:16:07 -04005539 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
5540 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
5541
5542 /* bitmaps and block group descriptor blocks */
5543 ret += groups + gdpblocks;
5544
5545 /* Blocks for super block, inode, quota and xattr blocks */
5546 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005547
5548 return ret;
5549}
5550
5551/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005552 * Calculate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04005553 * the modification of a single pages into a single transaction,
5554 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04005555 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04005556 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04005557 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04005558 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04005559 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04005560 */
5561int ext4_writepage_trans_blocks(struct inode *inode)
5562{
5563 int bpp = ext4_journal_blocks_per_page(inode);
5564 int ret;
5565
Jan Karafffb2732013-06-04 13:01:11 -04005566 ret = ext4_meta_trans_blocks(inode, bpp, bpp);
Mingming Caoa02908f2008-08-19 22:16:07 -04005567
5568 /* Account for data blocks for journalled mode */
5569 if (ext4_should_journal_data(inode))
5570 ret += bpp;
5571 return ret;
5572}
Mingming Caof3bd1f32008-08-19 22:16:03 -04005573
5574/*
5575 * Calculate the journal credits for a chunk of data modification.
5576 *
5577 * This is called from DIO, fallocate or whoever calling
Eric Sandeen79e83032010-07-27 11:56:07 -04005578 * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
Mingming Caof3bd1f32008-08-19 22:16:03 -04005579 *
5580 * journal buffers for data blocks are not included here, as DIO
5581 * and fallocate do no need to journal data buffers.
5582 */
5583int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
5584{
5585 return ext4_meta_trans_blocks(inode, nrblocks, 1);
5586}
5587
Mingming Caoa02908f2008-08-19 22:16:07 -04005588/*
Mingming Cao617ba132006-10-11 01:20:53 -07005589 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005590 * Give this, we know that the caller already has write access to iloc->bh.
5591 */
Mingming Cao617ba132006-10-11 01:20:53 -07005592int ext4_mark_iloc_dirty(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04005593 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005594{
5595 int err = 0;
5596
Vasily Averina6758302018-11-06 16:49:50 -05005597 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) {
5598 put_bh(iloc->bh);
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05005599 return -EIO;
Vasily Averina6758302018-11-06 16:49:50 -05005600 }
Theodore Ts'oc64db502012-03-02 12:23:11 -05005601 if (IS_I_VERSION(inode))
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005602 inode_inc_iversion(inode);
5603
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005604 /* the do_update_inode consumes one bh->b_count */
5605 get_bh(iloc->bh);
5606
Mingming Caodab291a2006-10-11 01:21:01 -07005607 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Frank Mayhar830156c2009-09-29 10:07:47 -04005608 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005609 put_bh(iloc->bh);
5610 return err;
5611}
5612
5613/*
5614 * On success, We end up with an outstanding reference count against
5615 * iloc->bh. This _must_ be cleaned up later.
5616 */
5617
5618int
Mingming Cao617ba132006-10-11 01:20:53 -07005619ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
5620 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005621{
Frank Mayhar03901312009-01-07 00:06:22 -05005622 int err;
5623
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05005624 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5625 return -EIO;
5626
Frank Mayhar03901312009-01-07 00:06:22 -05005627 err = ext4_get_inode_loc(inode, iloc);
5628 if (!err) {
5629 BUFFER_TRACE(iloc->bh, "get_write_access");
5630 err = ext4_journal_get_write_access(handle, iloc->bh);
5631 if (err) {
5632 brelse(iloc->bh);
5633 iloc->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005634 }
5635 }
Mingming Cao617ba132006-10-11 01:20:53 -07005636 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005637 return err;
5638}
5639
Miao Xiec03b45b2017-08-06 01:00:49 -04005640static int __ext4_expand_extra_isize(struct inode *inode,
5641 unsigned int new_extra_isize,
5642 struct ext4_iloc *iloc,
5643 handle_t *handle, int *no_expand)
5644{
5645 struct ext4_inode *raw_inode;
5646 struct ext4_xattr_ibody_header *header;
Theodore Ts'o4ea99932019-11-07 21:43:41 -05005647 unsigned int inode_size = EXT4_INODE_SIZE(inode->i_sb);
5648 struct ext4_inode_info *ei = EXT4_I(inode);
Miao Xiec03b45b2017-08-06 01:00:49 -04005649 int error;
5650
Theodore Ts'o4ea99932019-11-07 21:43:41 -05005651 /* this was checked at iget time, but double check for good measure */
5652 if ((EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > inode_size) ||
5653 (ei->i_extra_isize & 3)) {
5654 EXT4_ERROR_INODE(inode, "bad extra_isize %u (inode size %u)",
5655 ei->i_extra_isize,
5656 EXT4_INODE_SIZE(inode->i_sb));
5657 return -EFSCORRUPTED;
5658 }
5659 if ((new_extra_isize < ei->i_extra_isize) ||
5660 (new_extra_isize < 4) ||
5661 (new_extra_isize > inode_size - EXT4_GOOD_OLD_INODE_SIZE))
5662 return -EINVAL; /* Should never happen */
5663
Miao Xiec03b45b2017-08-06 01:00:49 -04005664 raw_inode = ext4_raw_inode(iloc);
5665
5666 header = IHDR(inode, raw_inode);
5667
5668 /* No extended attributes present */
5669 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
5670 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
5671 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE +
5672 EXT4_I(inode)->i_extra_isize, 0,
5673 new_extra_isize - EXT4_I(inode)->i_extra_isize);
5674 EXT4_I(inode)->i_extra_isize = new_extra_isize;
5675 return 0;
5676 }
5677
5678 /* try to expand with EAs present */
5679 error = ext4_expand_extra_isize_ea(inode, new_extra_isize,
5680 raw_inode, handle);
5681 if (error) {
5682 /*
5683 * Inode size expansion failed; don't try again
5684 */
5685 *no_expand = 1;
5686 }
5687
5688 return error;
5689}
5690
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005691/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005692 * Expand an inode by new_extra_isize bytes.
5693 * Returns 0 on success or negative error number on failure.
5694 */
Miao Xiecf0a5e82017-08-06 00:40:01 -04005695static int ext4_try_to_expand_extra_isize(struct inode *inode,
5696 unsigned int new_extra_isize,
5697 struct ext4_iloc iloc,
5698 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005699{
Miao Xie3b10fdc2017-08-06 00:27:38 -04005700 int no_expand;
5701 int error;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005702
Miao Xiecf0a5e82017-08-06 00:40:01 -04005703 if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND))
5704 return -EOVERFLOW;
5705
5706 /*
5707 * In nojournal mode, we can immediately attempt to expand
5708 * the inode. When journaled, we first need to obtain extra
5709 * buffer credits since we may write into the EA block
5710 * with this same handle. If journal_extend fails, then it will
5711 * only result in a minor loss of functionality for that inode.
5712 * If this is felt to be critical, then e2fsck should be run to
5713 * force a large enough s_min_extra_isize.
5714 */
Jan Kara6cb367c2019-11-05 17:44:14 +01005715 if (ext4_journal_extend(handle,
Jan Kara83448bd2019-11-05 17:44:29 +01005716 EXT4_DATA_TRANS_BLOCKS(inode->i_sb), 0) != 0)
Miao Xiecf0a5e82017-08-06 00:40:01 -04005717 return -ENOSPC;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005718
Miao Xie3b10fdc2017-08-06 00:27:38 -04005719 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
Miao Xiecf0a5e82017-08-06 00:40:01 -04005720 return -EBUSY;
Miao Xie3b10fdc2017-08-06 00:27:38 -04005721
Miao Xiec03b45b2017-08-06 01:00:49 -04005722 error = __ext4_expand_extra_isize(inode, new_extra_isize, &iloc,
5723 handle, &no_expand);
Miao Xie3b10fdc2017-08-06 00:27:38 -04005724 ext4_write_unlock_xattr(inode, &no_expand);
Miao Xiecf0a5e82017-08-06 00:40:01 -04005725
Miao Xie3b10fdc2017-08-06 00:27:38 -04005726 return error;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005727}
5728
Miao Xiec03b45b2017-08-06 01:00:49 -04005729int ext4_expand_extra_isize(struct inode *inode,
5730 unsigned int new_extra_isize,
5731 struct ext4_iloc *iloc)
5732{
5733 handle_t *handle;
5734 int no_expand;
5735 int error, rc;
5736
5737 if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
5738 brelse(iloc->bh);
5739 return -EOVERFLOW;
5740 }
5741
5742 handle = ext4_journal_start(inode, EXT4_HT_INODE,
5743 EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
5744 if (IS_ERR(handle)) {
5745 error = PTR_ERR(handle);
5746 brelse(iloc->bh);
5747 return error;
5748 }
5749
5750 ext4_write_lock_xattr(inode, &no_expand);
5751
zhangyi (F)ddccb6d2019-02-21 11:29:10 -05005752 BUFFER_TRACE(iloc->bh, "get_write_access");
Miao Xiec03b45b2017-08-06 01:00:49 -04005753 error = ext4_journal_get_write_access(handle, iloc->bh);
5754 if (error) {
5755 brelse(iloc->bh);
Dan Carpenter7f420d642019-12-13 21:50:11 +03005756 goto out_unlock;
Miao Xiec03b45b2017-08-06 01:00:49 -04005757 }
5758
5759 error = __ext4_expand_extra_isize(inode, new_extra_isize, iloc,
5760 handle, &no_expand);
5761
5762 rc = ext4_mark_iloc_dirty(handle, inode, iloc);
5763 if (!error)
5764 error = rc;
5765
Dan Carpenter7f420d642019-12-13 21:50:11 +03005766out_unlock:
Miao Xiec03b45b2017-08-06 01:00:49 -04005767 ext4_write_unlock_xattr(inode, &no_expand);
Miao Xiec03b45b2017-08-06 01:00:49 -04005768 ext4_journal_stop(handle);
5769 return error;
5770}
5771
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005772/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005773 * What we do here is to mark the in-core inode as clean with respect to inode
5774 * dirtiness (it may still be data-dirty).
5775 * This means that the in-core inode may be reaped by prune_icache
5776 * without having to perform any I/O. This is a very good thing,
5777 * because *any* task may call prune_icache - even ones which
5778 * have a transaction open against a different journal.
5779 *
5780 * Is this cheating? Not really. Sure, we haven't written the
5781 * inode out, but prune_icache isn't a user-visible syncing function.
5782 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5783 * we start and wait on commits.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005784 */
Mingming Cao617ba132006-10-11 01:20:53 -07005785int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005786{
Mingming Cao617ba132006-10-11 01:20:53 -07005787 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005788 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Miao Xiecf0a5e82017-08-06 00:40:01 -04005789 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005790
5791 might_sleep();
Theodore Ts'o7ff9c072010-11-08 13:51:33 -05005792 trace_ext4_mark_inode_dirty(inode, _RET_IP_);
Mingming Cao617ba132006-10-11 01:20:53 -07005793 err = ext4_reserve_inode_write(handle, inode, &iloc);
Eryu Guan5e1021f2016-03-12 21:40:32 -05005794 if (err)
5795 return err;
Miao Xiecf0a5e82017-08-06 00:40:01 -04005796
5797 if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize)
5798 ext4_try_to_expand_extra_isize(inode, sbi->s_want_extra_isize,
5799 iloc, handle);
5800
Eryu Guan5e1021f2016-03-12 21:40:32 -05005801 return ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005802}
5803
5804/*
Mingming Cao617ba132006-10-11 01:20:53 -07005805 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005806 *
5807 * We're really interested in the case where a file is being extended.
5808 * i_size has been changed by generic_commit_write() and we thus need
5809 * to include the updated inode in the current transaction.
5810 *
Christoph Hellwig5dd40562010-03-03 09:05:00 -05005811 * Also, dquot_alloc_block() will always dirty the inode when blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005812 * are allocated to the file.
5813 *
5814 * If the inode is marked synchronous, we don't honour that here - doing
5815 * so would cause a commit on atime updates, which we don't bother doing.
5816 * We handle synchronous inodes at the highest possible level.
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05005817 *
5818 * If only the I_DIRTY_TIME flag is set, we can skip everything. If
5819 * I_DIRTY_TIME and I_DIRTY_SYNC is set, the only inode fields we need
5820 * to copy into the on-disk inode structure are the timestamp files.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005821 */
Christoph Hellwigaa385722011-05-27 06:53:02 -04005822void ext4_dirty_inode(struct inode *inode, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005823{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005824 handle_t *handle;
5825
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05005826 if (flags == I_DIRTY_TIME)
5827 return;
Theodore Ts'o9924a922013-02-08 21:59:22 -05005828 handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005829 if (IS_ERR(handle))
5830 goto out;
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005831
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005832 ext4_mark_inode_dirty(handle, inode);
5833
Mingming Cao617ba132006-10-11 01:20:53 -07005834 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005835out:
5836 return;
5837}
5838
Mingming Cao617ba132006-10-11 01:20:53 -07005839int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005840{
5841 journal_t *journal;
5842 handle_t *handle;
5843 int err;
Daeho Jeongc8585c62016-04-25 23:22:35 -04005844 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005845
5846 /*
5847 * We have to be very careful here: changing a data block's
5848 * journaling status dynamically is dangerous. If we write a
5849 * data block to the journal, change the status and then delete
5850 * that block, we risk forgetting to revoke the old log record
5851 * from the journal and so a subsequent replay can corrupt data.
5852 * So, first we make sure that the journal is empty and that
5853 * nobody is changing anything.
5854 */
5855
Mingming Cao617ba132006-10-11 01:20:53 -07005856 journal = EXT4_JOURNAL(inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005857 if (!journal)
5858 return 0;
Dave Hansend6995942007-07-18 08:33:51 -04005859 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005860 return -EROFS;
5861
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04005862 /* Wait for all existing dio workers */
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04005863 inode_dio_wait(inode);
5864
Daeho Jeong4c546592016-04-25 23:21:00 -04005865 /*
5866 * Before flushing the journal and switching inode's aops, we have
5867 * to flush all dirty data the inode has. There can be outstanding
5868 * delayed allocations, there can be unwritten extents created by
5869 * fallocate or buffered writes in dioread_nolock mode covered by
5870 * dirty data which can be converted only after flushing the dirty
5871 * data (and journalled aops don't know how to handle these cases).
5872 */
5873 if (val) {
5874 down_write(&EXT4_I(inode)->i_mmap_sem);
5875 err = filemap_write_and_wait(inode->i_mapping);
5876 if (err < 0) {
5877 up_write(&EXT4_I(inode)->i_mmap_sem);
Daeho Jeong4c546592016-04-25 23:21:00 -04005878 return err;
5879 }
5880 }
5881
Eric Biggersbbd55932020-02-19 10:30:46 -08005882 percpu_down_write(&sbi->s_writepages_rwsem);
Mingming Caodab291a2006-10-11 01:21:01 -07005883 jbd2_journal_lock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005884
5885 /*
5886 * OK, there are no updates running now, and all cached data is
5887 * synced to disk. We are now in a completely consistent state
5888 * which doesn't have anything in the journal, and we know that
5889 * no filesystem updates are running, so it is safe to modify
5890 * the inode's in-core data-journaling state flag now.
5891 */
5892
5893 if (val)
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005894 ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Yongqiang Yang5872dda2011-12-28 13:55:51 -05005895 else {
Jan Kara4f879ca2014-10-30 10:53:17 -04005896 err = jbd2_journal_flush(journal);
5897 if (err < 0) {
5898 jbd2_journal_unlock_updates(journal);
Eric Biggersbbd55932020-02-19 10:30:46 -08005899 percpu_up_write(&sbi->s_writepages_rwsem);
Jan Kara4f879ca2014-10-30 10:53:17 -04005900 return err;
5901 }
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005902 ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Yongqiang Yang5872dda2011-12-28 13:55:51 -05005903 }
Mingming Cao617ba132006-10-11 01:20:53 -07005904 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005905
Mingming Caodab291a2006-10-11 01:21:01 -07005906 jbd2_journal_unlock_updates(journal);
Eric Biggersbbd55932020-02-19 10:30:46 -08005907 percpu_up_write(&sbi->s_writepages_rwsem);
Daeho Jeongc8585c62016-04-25 23:22:35 -04005908
Daeho Jeong4c546592016-04-25 23:21:00 -04005909 if (val)
5910 up_write(&EXT4_I(inode)->i_mmap_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005911
5912 /* Finally we can mark the inode as dirty. */
5913
Theodore Ts'o9924a922013-02-08 21:59:22 -05005914 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005915 if (IS_ERR(handle))
5916 return PTR_ERR(handle);
5917
Mingming Cao617ba132006-10-11 01:20:53 -07005918 err = ext4_mark_inode_dirty(handle, inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005919 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005920 ext4_journal_stop(handle);
5921 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005922
5923 return err;
5924}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005925
5926static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
5927{
5928 return !buffer_mapped(bh);
5929}
5930
Souptick Joarder401b25a2018-10-02 22:20:50 -04005931vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf)
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005932{
Dave Jiang11bac802017-02-24 14:56:41 -08005933 struct vm_area_struct *vma = vmf->vma;
Nick Pigginc2ec1752009-03-31 15:23:21 -07005934 struct page *page = vmf->page;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005935 loff_t size;
5936 unsigned long len;
Souptick Joarder401b25a2018-10-02 22:20:50 -04005937 int err;
5938 vm_fault_t ret;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005939 struct file *file = vma->vm_file;
Al Viro496ad9a2013-01-23 17:07:38 -05005940 struct inode *inode = file_inode(file);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005941 struct address_space *mapping = inode->i_mapping;
Jan Kara9ea7df52011-06-24 14:29:41 -04005942 handle_t *handle;
5943 get_block_t *get_block;
5944 int retries = 0;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005945
Theodore Ts'o02b016c2019-06-09 22:04:33 -04005946 if (unlikely(IS_IMMUTABLE(inode)))
5947 return VM_FAULT_SIGBUS;
5948
Jan Kara8e8ad8a2012-06-12 16:20:38 +02005949 sb_start_pagefault(inode->i_sb);
Theodore Ts'o041bbb6d2012-09-30 23:04:56 -04005950 file_update_time(vma->vm_file);
Jan Karaea3d7202015-12-07 14:28:03 -05005951
5952 down_read(&EXT4_I(inode)->i_mmap_sem);
Eric Biggers7b4cc972017-04-30 00:10:50 -04005953
Souptick Joarder401b25a2018-10-02 22:20:50 -04005954 err = ext4_convert_inline_data(inode);
5955 if (err)
Eric Biggers7b4cc972017-04-30 00:10:50 -04005956 goto out_ret;
5957
Jan Kara9ea7df52011-06-24 14:29:41 -04005958 /* Delalloc case is easy... */
5959 if (test_opt(inode->i_sb, DELALLOC) &&
5960 !ext4_should_journal_data(inode) &&
5961 !ext4_nonda_switch(inode->i_sb)) {
5962 do {
Souptick Joarder401b25a2018-10-02 22:20:50 -04005963 err = block_page_mkwrite(vma, vmf,
Jan Kara9ea7df52011-06-24 14:29:41 -04005964 ext4_da_get_block_prep);
Souptick Joarder401b25a2018-10-02 22:20:50 -04005965 } while (err == -ENOSPC &&
Jan Kara9ea7df52011-06-24 14:29:41 -04005966 ext4_should_retry_alloc(inode->i_sb, &retries));
5967 goto out_ret;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005968 }
Darrick J. Wong0e499892011-05-18 13:55:20 -04005969
5970 lock_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04005971 size = i_size_read(inode);
5972 /* Page got truncated from under us? */
5973 if (page->mapping != mapping || page_offset(page) > size) {
5974 unlock_page(page);
5975 ret = VM_FAULT_NOPAGE;
5976 goto out;
Darrick J. Wong0e499892011-05-18 13:55:20 -04005977 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005978
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005979 if (page->index == size >> PAGE_SHIFT)
5980 len = size & ~PAGE_MASK;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005981 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005982 len = PAGE_SIZE;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005983 /*
Jan Kara9ea7df52011-06-24 14:29:41 -04005984 * Return if we have all the buffers mapped. This avoids the need to do
5985 * journal_start/journal_stop which can block and take a long time
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005986 */
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005987 if (page_has_buffers(page)) {
Tao Maf19d5872012-12-10 14:05:51 -05005988 if (!ext4_walk_page_buffers(NULL, page_buffers(page),
5989 0, len, NULL,
5990 ext4_bh_unmapped)) {
Jan Kara9ea7df52011-06-24 14:29:41 -04005991 /* Wait so that we don't change page under IO */
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08005992 wait_for_stable_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04005993 ret = VM_FAULT_LOCKED;
5994 goto out;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005995 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005996 }
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005997 unlock_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04005998 /* OK, we need to fill the hole... */
5999 if (ext4_should_dioread_nolock(inode))
Jan Kara705965b2016-03-08 23:08:10 -05006000 get_block = ext4_get_block_unwritten;
Jan Kara9ea7df52011-06-24 14:29:41 -04006001 else
6002 get_block = ext4_get_block;
6003retry_alloc:
Theodore Ts'o9924a922013-02-08 21:59:22 -05006004 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
6005 ext4_writepage_trans_blocks(inode));
Jan Kara9ea7df52011-06-24 14:29:41 -04006006 if (IS_ERR(handle)) {
Nick Pigginc2ec1752009-03-31 15:23:21 -07006007 ret = VM_FAULT_SIGBUS;
Jan Kara9ea7df52011-06-24 14:29:41 -04006008 goto out;
6009 }
Souptick Joarder401b25a2018-10-02 22:20:50 -04006010 err = block_page_mkwrite(vma, vmf, get_block);
6011 if (!err && ext4_should_journal_data(inode)) {
Tao Maf19d5872012-12-10 14:05:51 -05006012 if (ext4_walk_page_buffers(handle, page_buffers(page), 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006013 PAGE_SIZE, NULL, do_journal_get_write_access)) {
Jan Kara9ea7df52011-06-24 14:29:41 -04006014 unlock_page(page);
6015 ret = VM_FAULT_SIGBUS;
Yongqiang Yangfcbb5512011-10-26 05:00:19 -04006016 ext4_journal_stop(handle);
Jan Kara9ea7df52011-06-24 14:29:41 -04006017 goto out;
6018 }
6019 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
6020 }
6021 ext4_journal_stop(handle);
Souptick Joarder401b25a2018-10-02 22:20:50 -04006022 if (err == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
Jan Kara9ea7df52011-06-24 14:29:41 -04006023 goto retry_alloc;
6024out_ret:
Souptick Joarder401b25a2018-10-02 22:20:50 -04006025 ret = block_page_mkwrite_return(err);
Jan Kara9ea7df52011-06-24 14:29:41 -04006026out:
Jan Karaea3d7202015-12-07 14:28:03 -05006027 up_read(&EXT4_I(inode)->i_mmap_sem);
Jan Kara8e8ad8a2012-06-12 16:20:38 +02006028 sb_end_pagefault(inode->i_sb);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04006029 return ret;
6030}
Jan Karaea3d7202015-12-07 14:28:03 -05006031
Souptick Joarder401b25a2018-10-02 22:20:50 -04006032vm_fault_t ext4_filemap_fault(struct vm_fault *vmf)
Jan Karaea3d7202015-12-07 14:28:03 -05006033{
Dave Jiang11bac802017-02-24 14:56:41 -08006034 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarder401b25a2018-10-02 22:20:50 -04006035 vm_fault_t ret;
Jan Karaea3d7202015-12-07 14:28:03 -05006036
6037 down_read(&EXT4_I(inode)->i_mmap_sem);
Souptick Joarder401b25a2018-10-02 22:20:50 -04006038 ret = filemap_fault(vmf);
Jan Karaea3d7202015-12-07 14:28:03 -05006039 up_read(&EXT4_I(inode)->i_mmap_sem);
6040
Souptick Joarder401b25a2018-10-02 22:20:50 -04006041 return ret;
Jan Karaea3d7202015-12-07 14:28:03 -05006042}