blob: 9100460d92e5f5ec02e0dccf40cbae7a2f61b9da [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002/*
Mingming Cao617ba132006-10-11 01:20:53 -07003 * linux/fs/ext4/inode.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004 *
5 * Copyright (C) 1992, 1993, 1994, 1995
6 * Remy Card (card@masi.ibp.fr)
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
9 *
10 * from
11 *
12 * linux/fs/minix/inode.c
13 *
14 * Copyright (C) 1991, 1992 Linus Torvalds
15 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -070016 * 64-bit file support on 64-bit platforms by Jakub Jelinek
17 * (jj@sunsite.ms.mff.cuni.cz)
18 *
Mingming Cao617ba132006-10-11 01:20:53 -070019 * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000
Dave Kleikampac27a0e2006-10-11 01:20:50 -070020 */
21
Dave Kleikampac27a0e2006-10-11 01:20:50 -070022#include <linux/fs.h>
23#include <linux/time.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070024#include <linux/highuid.h>
25#include <linux/pagemap.h>
Matthew Wilcoxc94c2ac2015-09-08 14:58:40 -070026#include <linux/dax.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070027#include <linux/quotaops.h>
28#include <linux/string.h>
29#include <linux/buffer_head.h>
30#include <linux/writeback.h>
Alex Tomas64769242008-07-11 19:27:31 -040031#include <linux/pagevec.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070032#include <linux/mpage.h>
Duane Griffine83c1392008-12-19 20:47:15 +000033#include <linux/namei.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070034#include <linux/uio.h>
35#include <linux/bio.h>
Mingming Cao4c0425f2009-09-28 15:48:41 -040036#include <linux/workqueue.h>
Jiaying Zhang744692d2010-03-04 16:14:02 -050037#include <linux/kernel.h>
Andrew Morton6db26ff2011-01-12 16:59:13 -080038#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Theodore Ts'o00a1a052014-03-30 10:20:01 -040040#include <linux/bitops.h>
Jan Kara364443c2016-11-20 17:36:06 -050041#include <linux/iomap.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050042#include <linux/iversion.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040043
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040044#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070045#include "xattr.h"
46#include "acl.h"
Theodore Ts'o9f125d62011-06-27 19:16:04 -040047#include "truncate.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070048
Theodore Ts'o9bffad12009-06-17 11:48:11 -040049#include <trace/events/ext4.h>
50
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -040051#define MPAGE_DA_EXTENT_TAIL 0x01
52
Darrick J. Wong814525f2012-04-29 18:31:10 -040053static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw,
54 struct ext4_inode_info *ei)
55{
56 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Darrick J. Wong814525f2012-04-29 18:31:10 -040057 __u32 csum;
Daeho Jeongb47820e2016-07-03 17:51:39 -040058 __u16 dummy_csum = 0;
59 int offset = offsetof(struct ext4_inode, i_checksum_lo);
60 unsigned int csum_size = sizeof(dummy_csum);
Darrick J. Wong814525f2012-04-29 18:31:10 -040061
Daeho Jeongb47820e2016-07-03 17:51:39 -040062 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, offset);
63 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, csum_size);
64 offset += csum_size;
65 csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset,
66 EXT4_GOOD_OLD_INODE_SIZE - offset);
67
68 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
69 offset = offsetof(struct ext4_inode, i_checksum_hi);
70 csum = ext4_chksum(sbi, csum, (__u8 *)raw +
71 EXT4_GOOD_OLD_INODE_SIZE,
72 offset - EXT4_GOOD_OLD_INODE_SIZE);
73 if (EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) {
74 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum,
75 csum_size);
76 offset += csum_size;
Daeho Jeongb47820e2016-07-03 17:51:39 -040077 }
Daeho Jeong05ac5aa2016-12-01 11:49:12 -050078 csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset,
79 EXT4_INODE_SIZE(inode->i_sb) - offset);
Darrick J. Wong814525f2012-04-29 18:31:10 -040080 }
81
Darrick J. Wong814525f2012-04-29 18:31:10 -040082 return csum;
83}
84
85static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw,
86 struct ext4_inode_info *ei)
87{
88 __u32 provided, calculated;
89
90 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
91 cpu_to_le32(EXT4_OS_LINUX) ||
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -040092 !ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong814525f2012-04-29 18:31:10 -040093 return 1;
94
95 provided = le16_to_cpu(raw->i_checksum_lo);
96 calculated = ext4_inode_csum(inode, raw, ei);
97 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
98 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
99 provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16;
100 else
101 calculated &= 0xFFFF;
102
103 return provided == calculated;
104}
105
106static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
107 struct ext4_inode_info *ei)
108{
109 __u32 csum;
110
111 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
112 cpu_to_le32(EXT4_OS_LINUX) ||
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400113 !ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong814525f2012-04-29 18:31:10 -0400114 return;
115
116 csum = ext4_inode_csum(inode, raw, ei);
117 raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF);
118 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
119 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
120 raw->i_checksum_hi = cpu_to_le16(csum >> 16);
121}
122
Jan Kara678aaf42008-07-11 19:27:31 -0400123static inline int ext4_begin_ordered_truncate(struct inode *inode,
124 loff_t new_size)
125{
Theodore Ts'o7ff9c072010-11-08 13:51:33 -0500126 trace_ext4_begin_ordered_truncate(inode, new_size);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -0500127 /*
128 * If jinode is zero, then we never opened the file for
129 * writing, so there's no need to call
130 * jbd2_journal_begin_ordered_truncate() since there's no
131 * outstanding writes we need to flush.
132 */
133 if (!EXT4_I(inode)->jinode)
134 return 0;
135 return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
136 EXT4_I(inode)->jinode,
137 new_size);
Jan Kara678aaf42008-07-11 19:27:31 -0400138}
139
Lukas Czernerd47992f2013-05-21 23:17:23 -0400140static void ext4_invalidatepage(struct page *page, unsigned int offset,
141 unsigned int length);
Theodore Ts'ocb20d512010-10-27 21:30:09 -0400142static int __ext4_journalled_writepage(struct page *page, unsigned int len);
143static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
Tahsin Erdogandec214d2017-06-22 11:44:55 -0400144static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
145 int pextents);
Alex Tomas64769242008-07-11 19:27:31 -0400146
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700147/*
148 * Test whether an inode is a fast symlink.
Tahsin Erdogan407cd7f2017-07-04 00:11:21 -0400149 * A fast symlink has its symlink data stored in ext4_inode_info->i_data.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700150 */
Theodore Ts'of348c252015-04-16 01:55:00 -0400151int ext4_inode_is_fast_symlink(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700152{
Andi Kleenfc822282017-12-03 20:38:01 -0500153 if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
154 int ea_blocks = EXT4_I(inode)->i_file_acl ?
155 EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0;
156
157 if (ext4_has_inline_data(inode))
158 return 0;
159
160 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
161 }
Tahsin Erdogan407cd7f2017-07-04 00:11:21 -0400162 return S_ISLNK(inode->i_mode) && inode->i_size &&
163 (inode->i_size < EXT4_N_BLOCKS * 4);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700164}
165
166/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700167 * Called at the last iput() if i_nlink is zero.
168 */
Al Viro0930fcc2010-06-07 13:16:22 -0400169void ext4_evict_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700170{
171 handle_t *handle;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400172 int err;
Jan Kara65db8692019-11-05 17:44:12 +0100173 /*
174 * Credits for final inode cleanup and freeing:
175 * sb + inode (ext4_orphan_del()), block bitmap, group descriptor
176 * (xattr block freeing), bitmap, group descriptor (inode freeing)
177 */
178 int extra_credits = 6;
Tahsin Erdogan0421a182017-06-22 10:26:31 -0400179 struct ext4_xattr_inode_array *ea_inode_array = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700180
Theodore Ts'o7ff9c072010-11-08 13:51:33 -0500181 trace_ext4_evict_inode(inode);
Jiaying Zhang2581fdc2011-08-13 12:17:13 -0400182
Al Viro0930fcc2010-06-07 13:16:22 -0400183 if (inode->i_nlink) {
Jan Kara2d859db2011-07-26 09:07:11 -0400184 /*
185 * When journalling data dirty buffers are tracked only in the
186 * journal. So although mm thinks everything is clean and
187 * ready for reaping the inode might still have some pages to
188 * write in the running transaction or waiting to be
189 * checkpointed. Thus calling jbd2_journal_invalidatepage()
190 * (via truncate_inode_pages()) to discard these buffers can
191 * cause data loss. Also even if we did not discard these
192 * buffers, we would have no way to find them after the inode
193 * is reaped and thus user could see stale data if he tries to
194 * read them before the transaction is checkpointed. So be
195 * careful and force everything to disk here... We use
196 * ei->i_datasync_tid to store the newest transaction
197 * containing inode's data.
198 *
199 * Note that directories do not have this problem because they
200 * don't use page cache.
201 */
Vegard Nossum6a7fd522016-07-04 11:03:00 -0400202 if (inode->i_ino != EXT4_JOURNAL_INO &&
203 ext4_should_journal_data(inode) &&
Jan Kara3abb1a02017-06-22 23:49:46 -0400204 (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode)) &&
205 inode->i_data.nrpages) {
Jan Kara2d859db2011-07-26 09:07:11 -0400206 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
207 tid_t commit_tid = EXT4_I(inode)->i_datasync_tid;
208
Theodore Ts'od76a3a772013-04-03 22:02:52 -0400209 jbd2_complete_transaction(journal, commit_tid);
Jan Kara2d859db2011-07-26 09:07:11 -0400210 filemap_write_and_wait(&inode->i_data);
211 }
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700212 truncate_inode_pages_final(&inode->i_data);
Jan Kara5dc23bd2013-06-04 14:46:12 -0400213
Al Viro0930fcc2010-06-07 13:16:22 -0400214 goto no_delete;
215 }
216
Theodore Ts'oe2bfb082014-10-05 22:47:07 -0400217 if (is_bad_inode(inode))
218 goto no_delete;
219 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500220
Jan Kara678aaf42008-07-11 19:27:31 -0400221 if (ext4_should_order_data(inode))
222 ext4_begin_ordered_truncate(inode, 0);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700223 truncate_inode_pages_final(&inode->i_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700224
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200225 /*
226 * Protect us against freezing - iput() caller didn't have to have any
227 * protection against it
228 */
229 sb_start_intwrite(inode->i_sb);
Andreas Dilgere50e5122017-06-21 21:10:32 -0400230
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400231 if (!IS_NOQUOTA(inode))
232 extra_credits += EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb);
233
Jan Kara65db8692019-11-05 17:44:12 +0100234 /*
235 * Block bitmap, group descriptor, and inode are accounted in both
236 * ext4_blocks_for_truncate() and extra_credits. So subtract 3.
237 */
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400238 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
Jan Kara65db8692019-11-05 17:44:12 +0100239 ext4_blocks_for_truncate(inode) + extra_credits - 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700240 if (IS_ERR(handle)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400241 ext4_std_error(inode->i_sb, PTR_ERR(handle));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700242 /*
243 * If we're going to skip the normal cleanup, we still need to
244 * make sure that the in-core orphan linked list is properly
245 * cleaned up.
246 */
Mingming Cao617ba132006-10-11 01:20:53 -0700247 ext4_orphan_del(NULL, inode);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200248 sb_end_intwrite(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700249 goto no_delete;
250 }
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400251
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700252 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500253 ext4_handle_sync(handle);
Tahsin Erdogan407cd7f2017-07-04 00:11:21 -0400254
255 /*
256 * Set inode->i_size to 0 before calling ext4_truncate(). We need
257 * special handling of symlinks here because i_size is used to
258 * determine whether ext4_inode_info->i_data contains symlink data or
259 * block mappings. Setting i_size to 0 will remove its fast symlink
260 * status. Erase i_data so that it becomes a valid empty block map.
261 */
262 if (ext4_inode_is_fast_symlink(inode))
263 memset(EXT4_I(inode)->i_data, 0, sizeof(EXT4_I(inode)->i_data));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700264 inode->i_size = 0;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400265 err = ext4_mark_inode_dirty(handle, inode);
266 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500267 ext4_warning(inode->i_sb,
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400268 "couldn't mark inode dirty (err %d)", err);
269 goto stop_handle;
270 }
Theodore Ts'o2c98eb52016-11-13 22:02:26 -0500271 if (inode->i_blocks) {
272 err = ext4_truncate(inode);
273 if (err) {
Theodore Ts'o878520a2019-11-19 21:54:15 -0500274 ext4_set_errno(inode->i_sb, -err);
Theodore Ts'o2c98eb52016-11-13 22:02:26 -0500275 ext4_error(inode->i_sb,
276 "couldn't truncate inode %lu (err %d)",
277 inode->i_ino, err);
278 goto stop_handle;
279 }
280 }
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400281
Tahsin Erdogan30a7eb92017-06-22 11:42:09 -0400282 /* Remove xattr references. */
283 err = ext4_xattr_delete_inode(handle, inode, &ea_inode_array,
284 extra_credits);
285 if (err) {
286 ext4_warning(inode->i_sb, "xattr delete (err %d)", err);
287stop_handle:
288 ext4_journal_stop(handle);
289 ext4_orphan_del(NULL, inode);
290 sb_end_intwrite(inode->i_sb);
291 ext4_xattr_inode_array_free(ea_inode_array);
292 goto no_delete;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400293 }
294
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700295 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700296 * Kill off the orphan record which ext4_truncate created.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700297 * AKPM: I think this can be inside the above `if'.
Mingming Cao617ba132006-10-11 01:20:53 -0700298 * Note that ext4_orphan_del() has to be able to cope with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700299 * deletion of a non-existent orphan - this is because we don't
Mingming Cao617ba132006-10-11 01:20:53 -0700300 * know if ext4_truncate() actually created an orphan record.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700301 * (Well, we could do this if we need to, but heck - it works)
302 */
Mingming Cao617ba132006-10-11 01:20:53 -0700303 ext4_orphan_del(handle, inode);
Arnd Bergmann5ffff832018-07-29 15:50:00 -0400304 EXT4_I(inode)->i_dtime = (__u32)ktime_get_real_seconds();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700305
306 /*
307 * One subtle ordering requirement: if anything has gone wrong
308 * (transaction abort, IO errors, whatever), then we can still
309 * do these next steps (the fs will already have been marked as
310 * having errors), but we can't free the inode if the mark_dirty
311 * fails.
312 */
Mingming Cao617ba132006-10-11 01:20:53 -0700313 if (ext4_mark_inode_dirty(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700314 /* If that failed, just do the required in-core inode clear. */
Al Viro0930fcc2010-06-07 13:16:22 -0400315 ext4_clear_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700316 else
Mingming Cao617ba132006-10-11 01:20:53 -0700317 ext4_free_inode(handle, inode);
318 ext4_journal_stop(handle);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200319 sb_end_intwrite(inode->i_sb);
Tahsin Erdogan0421a182017-06-22 10:26:31 -0400320 ext4_xattr_inode_array_free(ea_inode_array);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700321 return;
322no_delete:
Al Viro0930fcc2010-06-07 13:16:22 -0400323 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700324}
325
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300326#ifdef CONFIG_QUOTA
327qsize_t *ext4_get_reserved_space(struct inode *inode)
Mingming Cao60e58e02009-01-22 18:13:05 +0100328{
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300329 return &EXT4_I(inode)->i_reserved_quota;
Mingming Cao60e58e02009-01-22 18:13:05 +0100330}
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300331#endif
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500332
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400333/*
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500334 * Called with i_data_sem down, which is important since we can call
335 * ext4_discard_preallocations() from here.
336 */
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500337void ext4_da_update_reserve_space(struct inode *inode,
338 int used, int quota_claim)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400339{
340 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500341 struct ext4_inode_info *ei = EXT4_I(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400342
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500343 spin_lock(&ei->i_block_reservation_lock);
Aditya Kalid8990242011-09-09 19:18:51 -0400344 trace_ext4_da_update_reserve_space(inode, used, quota_claim);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500345 if (unlikely(used > ei->i_reserved_data_blocks)) {
Theodore Ts'o8de5c322013-02-14 15:11:41 -0500346 ext4_warning(inode->i_sb, "%s: ino %lu, used %d "
Theodore Ts'o1084f252012-03-19 23:13:43 -0400347 "with only %d reserved data blocks",
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500348 __func__, inode->i_ino, used,
349 ei->i_reserved_data_blocks);
350 WARN_ON(1);
351 used = ei->i_reserved_data_blocks;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400352 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400353
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500354 /* Update per-inode reservations */
355 ei->i_reserved_data_blocks -= used;
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -0400356 percpu_counter_sub(&sbi->s_dirtyclusters_counter, used);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500357
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400358 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +0100359
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400360 /* Update quota subsystem for data blocks */
361 if (quota_claim)
Aditya Kali7b415bf2011-09-09 19:04:51 -0400362 dquot_claim_block(inode, EXT4_C2B(sbi, used));
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400363 else {
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500364 /*
365 * We did fallocate with an offset that is already delayed
366 * allocated. So on delayed allocated writeback we should
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400367 * not re-claim the quota for fallocated blocks.
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500368 */
Aditya Kali7b415bf2011-09-09 19:04:51 -0400369 dquot_release_reservation_block(inode, EXT4_C2B(sbi, used));
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500370 }
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -0400371
372 /*
373 * If we have done all the pending block allocations and if
374 * there aren't any writers on the inode, we can discard the
375 * inode's preallocations.
376 */
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500377 if ((ei->i_reserved_data_blocks == 0) &&
Nikolay Borisov82dd1242019-02-10 23:04:16 -0500378 !inode_is_open_for_write(inode))
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -0400379 ext4_discard_preallocations(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400380}
381
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400382static int __check_block_validity(struct inode *inode, const char *func,
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400383 unsigned int line,
384 struct ext4_map_blocks *map)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400385{
Theodore Ts'o345c0db2019-04-09 23:37:08 -0400386 if (ext4_has_feature_journal(inode->i_sb) &&
387 (inode->i_ino ==
388 le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_journal_inum)))
389 return 0;
Theodore Ts'o24676da2010-05-16 21:00:00 -0400390 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
391 map->m_len)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400392 ext4_error_inode(inode, func, line, map->m_pblk,
Theodore Ts'obdbd6ce2018-06-15 12:27:16 -0400393 "lblock %lu mapped to illegal pblock %llu "
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400394 "(length %d)", (unsigned long) map->m_lblk,
Theodore Ts'obdbd6ce2018-06-15 12:27:16 -0400395 map->m_pblk, map->m_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400396 return -EFSCORRUPTED;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400397 }
398 return 0;
399}
400
Jan Kara53085fa2015-12-07 15:09:35 -0500401int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk,
402 ext4_lblk_t len)
403{
404 int ret;
405
Chandan Rajendra592ddec2018-12-12 15:20:10 +0530406 if (IS_ENCRYPTED(inode))
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400407 return fscrypt_zeroout_range(inode, lblk, pblk, len);
Jan Kara53085fa2015-12-07 15:09:35 -0500408
409 ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS);
410 if (ret > 0)
411 ret = 0;
412
413 return ret;
414}
415
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400416#define check_block_validity(inode, map) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400417 __check_block_validity((inode), __func__, __LINE__, (map))
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400418
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400419#ifdef ES_AGGRESSIVE_TEST
420static void ext4_map_blocks_es_recheck(handle_t *handle,
421 struct inode *inode,
422 struct ext4_map_blocks *es_map,
423 struct ext4_map_blocks *map,
424 int flags)
425{
426 int retval;
427
428 map->m_flags = 0;
429 /*
430 * There is a race window that the result is not the same.
431 * e.g. xfstests #223 when dioread_nolock enables. The reason
432 * is that we lookup a block mapping in extent status tree with
433 * out taking i_data_sem. So at the time the unwritten extent
434 * could be converted.
435 */
Jan Kara2dcba472015-12-07 15:04:57 -0500436 down_read(&EXT4_I(inode)->i_data_sem);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400437 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
438 retval = ext4_ext_map_blocks(handle, inode, map, flags &
439 EXT4_GET_BLOCKS_KEEP_SIZE);
440 } else {
441 retval = ext4_ind_map_blocks(handle, inode, map, flags &
442 EXT4_GET_BLOCKS_KEEP_SIZE);
443 }
Jan Kara2dcba472015-12-07 15:04:57 -0500444 up_read((&EXT4_I(inode)->i_data_sem));
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400445
446 /*
447 * We don't check m_len because extent will be collpased in status
448 * tree. So the m_len might not equal.
449 */
450 if (es_map->m_lblk != map->m_lblk ||
451 es_map->m_flags != map->m_flags ||
452 es_map->m_pblk != map->m_pblk) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400453 printk("ES cache assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400454 "es_cached ex [%d/%d/%llu/%x] != "
455 "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
456 inode->i_ino, es_map->m_lblk, es_map->m_len,
457 es_map->m_pblk, es_map->m_flags, map->m_lblk,
458 map->m_len, map->m_pblk, map->m_flags,
459 retval, flags);
460 }
461}
462#endif /* ES_AGGRESSIVE_TEST */
463
Theodore Ts'o55138e0b2009-09-29 13:31:31 -0400464/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400465 * The ext4_map_blocks() function tries to look up the requested blocks,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400466 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500467 *
Mingming Caof5ab0d12008-02-25 15:29:55 -0500468 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
469 * and store the allocated blocks in the result buffer head and mark it
470 * mapped.
471 *
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400472 * If file type is extents based, it will call ext4_ext_map_blocks(),
473 * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
Mingming Caof5ab0d12008-02-25 15:29:55 -0500474 * based files
475 *
Jan Karafacab4d2016-03-09 22:54:00 -0500476 * On success, it returns the number of blocks being mapped or allocated. if
477 * create==0 and the blocks are pre-allocated and unwritten, the resulting @map
478 * is marked as unwritten. If the create == 1, it will mark @map as mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500479 *
480 * It returns 0 if plain look up failed (blocks have not been allocated), in
Jan Karafacab4d2016-03-09 22:54:00 -0500481 * that case, @map is returned as unmapped but we still do fill map->m_len to
482 * indicate the length of a hole starting at map->m_lblk.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500483 *
484 * It returns the error in case of allocation failure.
485 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400486int ext4_map_blocks(handle_t *handle, struct inode *inode,
487 struct ext4_map_blocks *map, int flags)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500488{
Zheng Liud100eef2013-02-18 00:29:59 -0500489 struct extent_status es;
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500490 int retval;
Lukas Czernerb8a86842014-03-18 18:05:35 -0400491 int ret = 0;
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400492#ifdef ES_AGGRESSIVE_TEST
493 struct ext4_map_blocks orig_map;
494
495 memcpy(&orig_map, map, sizeof(*map));
496#endif
Mingming Caof5ab0d12008-02-25 15:29:55 -0500497
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400498 map->m_flags = 0;
499 ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
500 "logical block %lu\n", inode->i_ino, flags, map->m_len,
501 (unsigned long) map->m_lblk);
Zheng Liud100eef2013-02-18 00:29:59 -0500502
Theodore Ts'oe861b5e2014-02-20 12:54:05 -0500503 /*
504 * ext4_map_blocks returns an int, and m_len is an unsigned int
505 */
506 if (unlikely(map->m_len > INT_MAX))
507 map->m_len = INT_MAX;
508
Kazuya Mio4adb6ab2014-04-07 10:53:28 -0400509 /* We can handle the block number less than EXT_MAX_BLOCKS */
510 if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400511 return -EFSCORRUPTED;
Kazuya Mio4adb6ab2014-04-07 10:53:28 -0400512
Zheng Liud100eef2013-02-18 00:29:59 -0500513 /* Lookup extent status tree firstly */
Theodore Ts'obb5835e2019-08-11 16:32:41 -0400514 if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) {
Zheng Liud100eef2013-02-18 00:29:59 -0500515 if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
516 map->m_pblk = ext4_es_pblock(&es) +
517 map->m_lblk - es.es_lblk;
518 map->m_flags |= ext4_es_is_written(&es) ?
519 EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN;
520 retval = es.es_len - (map->m_lblk - es.es_lblk);
521 if (retval > map->m_len)
522 retval = map->m_len;
523 map->m_len = retval;
524 } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) {
Jan Karafacab4d2016-03-09 22:54:00 -0500525 map->m_pblk = 0;
526 retval = es.es_len - (map->m_lblk - es.es_lblk);
527 if (retval > map->m_len)
528 retval = map->m_len;
529 map->m_len = retval;
Zheng Liud100eef2013-02-18 00:29:59 -0500530 retval = 0;
531 } else {
Arnd Bergmann1e83bc82019-04-07 12:24:43 -0400532 BUG();
Zheng Liud100eef2013-02-18 00:29:59 -0500533 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400534#ifdef ES_AGGRESSIVE_TEST
535 ext4_map_blocks_es_recheck(handle, inode, map,
536 &orig_map, flags);
537#endif
Zheng Liud100eef2013-02-18 00:29:59 -0500538 goto found;
539 }
540
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500541 /*
Theodore Ts'ob920c752009-05-14 00:54:29 -0400542 * Try to see if we can get the block without requesting a new
543 * file system block.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500544 */
Jan Kara2dcba472015-12-07 15:04:57 -0500545 down_read(&EXT4_I(inode)->i_data_sem);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400546 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Dmitry Monakhova4e5d882011-10-25 08:15:12 -0400547 retval = ext4_ext_map_blocks(handle, inode, map, flags &
548 EXT4_GET_BLOCKS_KEEP_SIZE);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500549 } else {
Dmitry Monakhova4e5d882011-10-25 08:15:12 -0400550 retval = ext4_ind_map_blocks(handle, inode, map, flags &
551 EXT4_GET_BLOCKS_KEEP_SIZE);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500552 }
Zheng Liuf7fec032013-02-18 00:28:47 -0500553 if (retval > 0) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400554 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -0500555
Zheng Liu44fb851d2013-07-29 12:51:42 -0400556 if (unlikely(retval != map->m_len)) {
557 ext4_warning(inode->i_sb,
558 "ES len assertion failed for inode "
559 "%lu: retval %d != map->m_len %d",
560 inode->i_ino, retval, map->m_len);
561 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400562 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400563
Zheng Liuf7fec032013-02-18 00:28:47 -0500564 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
565 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
566 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400567 !(status & EXTENT_STATUS_WRITTEN) &&
Eric Whitneyad431022018-10-01 14:10:39 -0400568 ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk,
569 map->m_lblk + map->m_len - 1))
Zheng Liuf7fec032013-02-18 00:28:47 -0500570 status |= EXTENT_STATUS_DELAYED;
571 ret = ext4_es_insert_extent(inode, map->m_lblk,
572 map->m_len, map->m_pblk, status);
573 if (ret < 0)
574 retval = ret;
575 }
Jan Kara2dcba472015-12-07 15:04:57 -0500576 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -0500577
Zheng Liud100eef2013-02-18 00:29:59 -0500578found:
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400579 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Lukas Czernerb8a86842014-03-18 18:05:35 -0400580 ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400581 if (ret != 0)
582 return ret;
583 }
584
Mingming Caof5ab0d12008-02-25 15:29:55 -0500585 /* If it is only a block(s) look up */
Theodore Ts'oc2177052009-05-14 00:58:52 -0400586 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500587 return retval;
588
589 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -0500590 * Returns if the blocks have already allocated
591 *
592 * Note that if blocks have been preallocated
Tao Madf3ab172011-10-08 15:53:49 -0400593 * ext4_ext_get_block() returns the create = 0
Mingming Caof5ab0d12008-02-25 15:29:55 -0500594 * with buffer head unmapped.
595 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400596 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
Lukas Czernerb8a86842014-03-18 18:05:35 -0400597 /*
598 * If we need to convert extent to unwritten
599 * we continue and do the actual work in
600 * ext4_ext_map_blocks()
601 */
602 if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN))
603 return retval;
Mingming Caof5ab0d12008-02-25 15:29:55 -0500604
605 /*
Zheng Liua25a4e12013-02-18 00:28:04 -0500606 * Here we clear m_flags because after allocating an new extent,
607 * it will be set again.
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -0400608 */
Zheng Liua25a4e12013-02-18 00:28:04 -0500609 map->m_flags &= ~EXT4_MAP_FLAGS;
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -0400610
611 /*
Lukas Czerner556615d2014-04-20 23:45:47 -0400612 * New blocks allocate and/or writing to unwritten extent
Mingming Caof5ab0d12008-02-25 15:29:55 -0500613 * will possibly result in updating i_data, so we take
Seunghun Leed91bd2c2014-09-01 22:15:30 -0400614 * the write lock of i_data_sem, and call get_block()
Mingming Caof5ab0d12008-02-25 15:29:55 -0500615 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500616 */
Lukas Czernerc8b459f2014-05-12 12:55:07 -0400617 down_write(&EXT4_I(inode)->i_data_sem);
Mingming Caod2a17632008-07-14 17:52:37 -0400618
619 /*
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500620 * We need to check for EXT4 here because migrate
621 * could have changed the inode type in between
622 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400623 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400624 retval = ext4_ext_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500625 } else {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400626 retval = ext4_ind_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400627
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400628 if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400629 /*
630 * We allocated new blocks which will result in
631 * i_data's format changing. Force the migrate
632 * to fail by clearing migrate flags
633 */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500634 ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400635 }
Mingming Caod2a17632008-07-14 17:52:37 -0400636
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500637 /*
638 * Update reserved blocks/metadata blocks after successful
639 * block allocation which had been deferred till now. We don't
640 * support fallocate for non extent files. So we can update
641 * reserve space here.
642 */
643 if ((retval > 0) &&
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -0500644 (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500645 ext4_da_update_reserve_space(inode, retval, 1);
646 }
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -0400647
Zheng Liuf7fec032013-02-18 00:28:47 -0500648 if (retval > 0) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400649 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -0500650
Zheng Liu44fb851d2013-07-29 12:51:42 -0400651 if (unlikely(retval != map->m_len)) {
652 ext4_warning(inode->i_sb,
653 "ES len assertion failed for inode "
654 "%lu: retval %d != map->m_len %d",
655 inode->i_ino, retval, map->m_len);
656 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400657 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400658
Zheng Liuadb23552013-03-10 21:13:05 -0400659 /*
Jan Karac86d8db2015-12-07 15:10:26 -0500660 * We have to zeroout blocks before inserting them into extent
661 * status tree. Otherwise someone could look them up there and
Jan Kara9b623df2016-09-30 02:02:29 -0400662 * use them before they are really zeroed. We also have to
663 * unmap metadata before zeroing as otherwise writeback can
664 * overwrite zeros with stale data from block device.
Jan Karac86d8db2015-12-07 15:10:26 -0500665 */
666 if (flags & EXT4_GET_BLOCKS_ZERO &&
667 map->m_flags & EXT4_MAP_MAPPED &&
668 map->m_flags & EXT4_MAP_NEW) {
669 ret = ext4_issue_zeroout(inode, map->m_lblk,
670 map->m_pblk, map->m_len);
671 if (ret) {
672 retval = ret;
673 goto out_sem;
674 }
675 }
676
677 /*
Zheng Liuadb23552013-03-10 21:13:05 -0400678 * If the extent has been zeroed out, we don't need to update
679 * extent status tree.
680 */
681 if ((flags & EXT4_GET_BLOCKS_PRE_IO) &&
Theodore Ts'obb5835e2019-08-11 16:32:41 -0400682 ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es)) {
Zheng Liuadb23552013-03-10 21:13:05 -0400683 if (ext4_es_is_written(&es))
Jan Karac86d8db2015-12-07 15:10:26 -0500684 goto out_sem;
Zheng Liuadb23552013-03-10 21:13:05 -0400685 }
Zheng Liuf7fec032013-02-18 00:28:47 -0500686 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
687 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
688 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400689 !(status & EXTENT_STATUS_WRITTEN) &&
Eric Whitneyad431022018-10-01 14:10:39 -0400690 ext4_es_scan_range(inode, &ext4_es_is_delayed, map->m_lblk,
691 map->m_lblk + map->m_len - 1))
Zheng Liuf7fec032013-02-18 00:28:47 -0500692 status |= EXTENT_STATUS_DELAYED;
693 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
694 map->m_pblk, status);
Jan Karac86d8db2015-12-07 15:10:26 -0500695 if (ret < 0) {
Zheng Liuf7fec032013-02-18 00:28:47 -0500696 retval = ret;
Jan Karac86d8db2015-12-07 15:10:26 -0500697 goto out_sem;
698 }
Aditya Kali5356f2612011-09-09 19:20:51 -0400699 }
700
Jan Karac86d8db2015-12-07 15:10:26 -0500701out_sem:
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500702 up_write((&EXT4_I(inode)->i_data_sem));
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400703 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Lukas Czernerb8a86842014-03-18 18:05:35 -0400704 ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400705 if (ret != 0)
706 return ret;
Jan Kara06bd3c32016-04-24 00:56:03 -0400707
708 /*
709 * Inodes with freshly allocated blocks where contents will be
710 * visible after transaction commit must be on transaction's
711 * ordered data list.
712 */
713 if (map->m_flags & EXT4_MAP_NEW &&
714 !(map->m_flags & EXT4_MAP_UNWRITTEN) &&
715 !(flags & EXT4_GET_BLOCKS_ZERO) &&
Tahsin Erdogan02749a42017-06-22 11:31:25 -0400716 !ext4_is_quota_file(inode) &&
Jan Kara06bd3c32016-04-24 00:56:03 -0400717 ext4_should_order_data(inode)) {
Ross Zwisler73131fb2019-06-20 17:26:26 -0400718 loff_t start_byte =
719 (loff_t)map->m_lblk << inode->i_blkbits;
720 loff_t length = (loff_t)map->m_len << inode->i_blkbits;
721
Jan Karaee0876b2016-04-24 00:56:08 -0400722 if (flags & EXT4_GET_BLOCKS_IO_SUBMIT)
Ross Zwisler73131fb2019-06-20 17:26:26 -0400723 ret = ext4_jbd2_inode_add_wait(handle, inode,
724 start_byte, length);
Jan Karaee0876b2016-04-24 00:56:08 -0400725 else
Ross Zwisler73131fb2019-06-20 17:26:26 -0400726 ret = ext4_jbd2_inode_add_write(handle, inode,
727 start_byte, length);
Jan Kara06bd3c32016-04-24 00:56:03 -0400728 if (ret)
729 return ret;
730 }
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400731 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500732 return retval;
733}
734
Jan Karaed8ad832016-02-19 00:18:25 -0500735/*
736 * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages
737 * we have to be careful as someone else may be manipulating b_state as well.
738 */
739static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags)
740{
741 unsigned long old_state;
742 unsigned long new_state;
743
744 flags &= EXT4_MAP_FLAGS;
745
746 /* Dummy buffer_head? Set non-atomically. */
747 if (!bh->b_page) {
748 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags;
749 return;
750 }
751 /*
752 * Someone else may be modifying b_state. Be careful! This is ugly but
753 * once we get rid of using bh as a container for mapping information
754 * to pass to / from get_block functions, this can go away.
755 */
756 do {
757 old_state = READ_ONCE(bh->b_state);
758 new_state = (old_state & ~EXT4_MAP_FLAGS) | flags;
759 } while (unlikely(
760 cmpxchg(&bh->b_state, old_state, new_state) != old_state));
761}
762
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400763static int _ext4_get_block(struct inode *inode, sector_t iblock,
764 struct buffer_head *bh, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700765{
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400766 struct ext4_map_blocks map;
Jan Karaefe70c22016-03-08 23:35:46 -0500767 int ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700768
Tao Ma46c7f252012-12-10 14:04:52 -0500769 if (ext4_has_inline_data(inode))
770 return -ERANGE;
771
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400772 map.m_lblk = iblock;
773 map.m_len = bh->b_size >> inode->i_blkbits;
774
Jan Karaefe70c22016-03-08 23:35:46 -0500775 ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map,
776 flags);
Jan Kara7fb54092008-02-10 01:08:38 -0500777 if (ret > 0) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400778 map_bh(bh, inode->i_sb, map.m_pblk);
Jan Karaed8ad832016-02-19 00:18:25 -0500779 ext4_update_bh_state(bh, map.m_flags);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400780 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
Jan Kara7fb54092008-02-10 01:08:38 -0500781 ret = 0;
Ross Zwisler547edce2016-11-08 11:30:58 +1100782 } else if (ret == 0) {
783 /* hole case, need to fill in bh->b_size */
784 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700785 }
786 return ret;
787}
788
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400789int ext4_get_block(struct inode *inode, sector_t iblock,
790 struct buffer_head *bh, int create)
791{
792 return _ext4_get_block(inode, iblock, bh,
793 create ? EXT4_GET_BLOCKS_CREATE : 0);
794}
795
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700796/*
Jan Kara705965b2016-03-08 23:08:10 -0500797 * Get block function used when preparing for buffered write if we require
798 * creating an unwritten extent if blocks haven't been allocated. The extent
799 * will be converted to written after the IO is complete.
800 */
801int ext4_get_block_unwritten(struct inode *inode, sector_t iblock,
802 struct buffer_head *bh_result, int create)
803{
804 ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n",
805 inode->i_ino, create);
806 return _ext4_get_block(inode, iblock, bh_result,
807 EXT4_GET_BLOCKS_IO_CREATE_EXT);
808}
809
Jan Karaefe70c22016-03-08 23:35:46 -0500810/* Maximum number of blocks we map for direct IO at once. */
811#define DIO_MAX_BLOCKS 4096
812
Jan Karae84dfbe2016-04-01 02:07:22 -0400813/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700814 * `handle' can be NULL if create is zero
815 */
Mingming Cao617ba132006-10-11 01:20:53 -0700816struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400817 ext4_lblk_t block, int map_flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700818{
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400819 struct ext4_map_blocks map;
820 struct buffer_head *bh;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400821 int create = map_flags & EXT4_GET_BLOCKS_CREATE;
Theodore Ts'o10560082014-08-29 20:51:32 -0400822 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700823
824 J_ASSERT(handle != NULL || create == 0);
825
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400826 map.m_lblk = block;
827 map.m_len = 1;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400828 err = ext4_map_blocks(handle, inode, &map, map_flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700829
Theodore Ts'o10560082014-08-29 20:51:32 -0400830 if (err == 0)
831 return create ? ERR_PTR(-ENOSPC) : NULL;
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400832 if (err < 0)
Theodore Ts'o10560082014-08-29 20:51:32 -0400833 return ERR_PTR(err);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400834
835 bh = sb_getblk(inode->i_sb, map.m_pblk);
Theodore Ts'o10560082014-08-29 20:51:32 -0400836 if (unlikely(!bh))
837 return ERR_PTR(-ENOMEM);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400838 if (map.m_flags & EXT4_MAP_NEW) {
839 J_ASSERT(create != 0);
840 J_ASSERT(handle != NULL);
841
842 /*
843 * Now that we do not always journal data, we should
844 * keep in mind whether this should always journal the
845 * new buffer as metadata. For now, regular file
846 * writes use ext4_get_block instead, so it's not a
847 * problem.
848 */
849 lock_buffer(bh);
850 BUFFER_TRACE(bh, "call get_create_access");
Theodore Ts'o10560082014-08-29 20:51:32 -0400851 err = ext4_journal_get_create_access(handle, bh);
852 if (unlikely(err)) {
853 unlock_buffer(bh);
854 goto errout;
855 }
856 if (!buffer_uptodate(bh)) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400857 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
858 set_buffer_uptodate(bh);
859 }
860 unlock_buffer(bh);
861 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
862 err = ext4_handle_dirty_metadata(handle, inode, bh);
Theodore Ts'o10560082014-08-29 20:51:32 -0400863 if (unlikely(err))
864 goto errout;
865 } else
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400866 BUFFER_TRACE(bh, "not a new buffer");
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400867 return bh;
Theodore Ts'o10560082014-08-29 20:51:32 -0400868errout:
869 brelse(bh);
870 return ERR_PTR(err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700871}
872
Mingming Cao617ba132006-10-11 01:20:53 -0700873struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400874 ext4_lblk_t block, int map_flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700875{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400876 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700877
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400878 bh = ext4_getblk(handle, inode, block, map_flags);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400879 if (IS_ERR(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700880 return bh;
ZhangXiaoxu7963e5a2019-08-22 23:00:32 -0400881 if (!bh || ext4_buffer_uptodate(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700882 return bh;
Mike Christiedfec8a12016-06-05 14:31:44 -0500883 ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700884 wait_on_buffer(bh);
885 if (buffer_uptodate(bh))
886 return bh;
887 put_bh(bh);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400888 return ERR_PTR(-EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700889}
890
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400891/* Read a contiguous batch of blocks. */
892int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count,
893 bool wait, struct buffer_head **bhs)
894{
895 int i, err;
896
897 for (i = 0; i < bh_count; i++) {
898 bhs[i] = ext4_getblk(NULL, inode, block + i, 0 /* map_flags */);
899 if (IS_ERR(bhs[i])) {
900 err = PTR_ERR(bhs[i]);
901 bh_count = i;
902 goto out_brelse;
903 }
904 }
905
906 for (i = 0; i < bh_count; i++)
907 /* Note that NULL bhs[i] is valid because of holes. */
ZhangXiaoxu7963e5a2019-08-22 23:00:32 -0400908 if (bhs[i] && !ext4_buffer_uptodate(bhs[i]))
Tahsin Erdogan9699d4f2017-08-06 00:07:01 -0400909 ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1,
910 &bhs[i]);
911
912 if (!wait)
913 return 0;
914
915 for (i = 0; i < bh_count; i++)
916 if (bhs[i])
917 wait_on_buffer(bhs[i]);
918
919 for (i = 0; i < bh_count; i++) {
920 if (bhs[i] && !buffer_uptodate(bhs[i])) {
921 err = -EIO;
922 goto out_brelse;
923 }
924 }
925 return 0;
926
927out_brelse:
928 for (i = 0; i < bh_count; i++) {
929 brelse(bhs[i]);
930 bhs[i] = NULL;
931 }
932 return err;
933}
934
Tao Maf19d5872012-12-10 14:05:51 -0500935int ext4_walk_page_buffers(handle_t *handle,
936 struct buffer_head *head,
937 unsigned from,
938 unsigned to,
939 int *partial,
940 int (*fn)(handle_t *handle,
941 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700942{
943 struct buffer_head *bh;
944 unsigned block_start, block_end;
945 unsigned blocksize = head->b_size;
946 int err, ret = 0;
947 struct buffer_head *next;
948
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400949 for (bh = head, block_start = 0;
950 ret == 0 && (bh != head || !block_start);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400951 block_start = block_end, bh = next) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700952 next = bh->b_this_page;
953 block_end = block_start + blocksize;
954 if (block_end <= from || block_start >= to) {
955 if (partial && !buffer_uptodate(bh))
956 *partial = 1;
957 continue;
958 }
959 err = (*fn)(handle, bh);
960 if (!ret)
961 ret = err;
962 }
963 return ret;
964}
965
966/*
967 * To preserve ordering, it is essential that the hole instantiation and
968 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -0700969 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -0700970 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700971 * prepare_write() is the right place.
972 *
Jan Kara36ade452013-01-28 09:30:52 -0500973 * Also, this function can nest inside ext4_writepage(). In that case, we
974 * *know* that ext4_writepage() has generated enough buffer credits to do the
975 * whole page. So we won't block on the journal in that case, which is good,
976 * because the caller may be PF_MEMALLOC.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700977 *
Mingming Cao617ba132006-10-11 01:20:53 -0700978 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700979 * quota file writes. If we were to commit the transaction while thus
980 * reentered, there can be a deadlock - we would be holding a quota
981 * lock, and the commit would never complete if another thread had a
982 * transaction open and was blocking on the quota lock - a ranking
983 * violation.
984 *
Mingming Caodab291a2006-10-11 01:21:01 -0700985 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700986 * will _not_ run commit under these circumstances because handle->h_ref
987 * is elevated. We'll still have enough credits for the tiny quotafile
988 * write.
989 */
Tao Maf19d5872012-12-10 14:05:51 -0500990int do_journal_get_write_access(handle_t *handle,
991 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700992{
Jan Kara56d35a42010-08-05 14:41:42 -0400993 int dirty = buffer_dirty(bh);
994 int ret;
995
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700996 if (!buffer_mapped(bh) || buffer_freed(bh))
997 return 0;
Jan Kara56d35a42010-08-05 14:41:42 -0400998 /*
Christoph Hellwigebdec242010-10-06 10:47:23 +0200999 * __block_write_begin() could have dirtied some buffers. Clean
Jan Kara56d35a42010-08-05 14:41:42 -04001000 * the dirty bit as jbd2_journal_get_write_access() could complain
1001 * otherwise about fs integrity issues. Setting of the dirty bit
Christoph Hellwigebdec242010-10-06 10:47:23 +02001002 * by __block_write_begin() isn't a real problem here as we clear
Jan Kara56d35a42010-08-05 14:41:42 -04001003 * the bit before releasing a page lock and thus writeback cannot
1004 * ever write the buffer.
1005 */
1006 if (dirty)
1007 clear_buffer_dirty(bh);
liang xie5d601252014-05-12 22:06:43 -04001008 BUFFER_TRACE(bh, "get write access");
Jan Kara56d35a42010-08-05 14:41:42 -04001009 ret = ext4_journal_get_write_access(handle, bh);
1010 if (!ret && dirty)
1011 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
1012 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001013}
1014
Chandan Rajendra643fa962018-12-12 15:20:12 +05301015#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrow2058f832015-04-12 00:55:10 -04001016static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len,
1017 get_block_t *get_block)
1018{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001019 unsigned from = pos & (PAGE_SIZE - 1);
Michael Halcrow2058f832015-04-12 00:55:10 -04001020 unsigned to = from + len;
1021 struct inode *inode = page->mapping->host;
1022 unsigned block_start, block_end;
1023 sector_t block;
1024 int err = 0;
1025 unsigned blocksize = inode->i_sb->s_blocksize;
1026 unsigned bbits;
Chandan Rajendra0b578f32019-05-20 09:29:50 -07001027 struct buffer_head *bh, *head, *wait[2];
1028 int nr_wait = 0;
1029 int i;
Michael Halcrow2058f832015-04-12 00:55:10 -04001030
1031 BUG_ON(!PageLocked(page));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001032 BUG_ON(from > PAGE_SIZE);
1033 BUG_ON(to > PAGE_SIZE);
Michael Halcrow2058f832015-04-12 00:55:10 -04001034 BUG_ON(from > to);
1035
1036 if (!page_has_buffers(page))
1037 create_empty_buffers(page, blocksize, 0);
1038 head = page_buffers(page);
1039 bbits = ilog2(blocksize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001040 block = (sector_t)page->index << (PAGE_SHIFT - bbits);
Michael Halcrow2058f832015-04-12 00:55:10 -04001041
1042 for (bh = head, block_start = 0; bh != head || !block_start;
1043 block++, block_start = block_end, bh = bh->b_this_page) {
1044 block_end = block_start + blocksize;
1045 if (block_end <= from || block_start >= to) {
1046 if (PageUptodate(page)) {
1047 if (!buffer_uptodate(bh))
1048 set_buffer_uptodate(bh);
1049 }
1050 continue;
1051 }
1052 if (buffer_new(bh))
1053 clear_buffer_new(bh);
1054 if (!buffer_mapped(bh)) {
1055 WARN_ON(bh->b_size != blocksize);
1056 err = get_block(inode, block, bh, 1);
1057 if (err)
1058 break;
1059 if (buffer_new(bh)) {
Michael Halcrow2058f832015-04-12 00:55:10 -04001060 if (PageUptodate(page)) {
1061 clear_buffer_new(bh);
1062 set_buffer_uptodate(bh);
1063 mark_buffer_dirty(bh);
1064 continue;
1065 }
1066 if (block_end > to || block_start < from)
1067 zero_user_segments(page, to, block_end,
1068 block_start, from);
1069 continue;
1070 }
1071 }
1072 if (PageUptodate(page)) {
1073 if (!buffer_uptodate(bh))
1074 set_buffer_uptodate(bh);
1075 continue;
1076 }
1077 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
1078 !buffer_unwritten(bh) &&
1079 (block_start < from || block_end > to)) {
Mike Christiedfec8a12016-06-05 14:31:44 -05001080 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
Chandan Rajendra0b578f32019-05-20 09:29:50 -07001081 wait[nr_wait++] = bh;
Michael Halcrow2058f832015-04-12 00:55:10 -04001082 }
1083 }
1084 /*
1085 * If we issued read requests, let them complete.
1086 */
Chandan Rajendra0b578f32019-05-20 09:29:50 -07001087 for (i = 0; i < nr_wait; i++) {
1088 wait_on_buffer(wait[i]);
1089 if (!buffer_uptodate(wait[i]))
Michael Halcrow2058f832015-04-12 00:55:10 -04001090 err = -EIO;
1091 }
Chandan Rajendra7e0785f2019-05-20 09:29:49 -07001092 if (unlikely(err)) {
Michael Halcrow2058f832015-04-12 00:55:10 -04001093 page_zero_new_buffers(page, from, to);
Chandan Rajendra0b578f32019-05-20 09:29:50 -07001094 } else if (IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode)) {
1095 for (i = 0; i < nr_wait; i++) {
1096 int err2;
1097
1098 err2 = fscrypt_decrypt_pagecache_blocks(page, blocksize,
1099 bh_offset(wait[i]));
1100 if (err2) {
1101 clear_buffer_uptodate(wait[i]);
1102 err = err2;
1103 }
1104 }
Chandan Rajendra7e0785f2019-05-20 09:29:49 -07001105 }
1106
Michael Halcrow2058f832015-04-12 00:55:10 -04001107 return err;
1108}
1109#endif
1110
Nick Pigginbfc1af62007-10-16 01:25:05 -07001111static int ext4_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001112 loff_t pos, unsigned len, unsigned flags,
1113 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001114{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001115 struct inode *inode = mapping->host;
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001116 int ret, needed_blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001117 handle_t *handle;
1118 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001119 struct page *page;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001120 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001121 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001122
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05001123 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
1124 return -EIO;
1125
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001126 trace_ext4_write_begin(inode, pos, len, flags);
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001127 /*
1128 * Reserve one block more for addition to orphan list in case
1129 * we allocate blocks but write fails for some reason
1130 */
1131 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001132 index = pos >> PAGE_SHIFT;
1133 from = pos & (PAGE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001134 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001135
Tao Maf19d5872012-12-10 14:05:51 -05001136 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
1137 ret = ext4_try_to_write_inline_data(mapping, inode, pos, len,
1138 flags, pagep);
1139 if (ret < 0)
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001140 return ret;
1141 if (ret == 1)
1142 return 0;
Tao Maf19d5872012-12-10 14:05:51 -05001143 }
1144
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001145 /*
1146 * grab_cache_page_write_begin() can take a long time if the
1147 * system is thrashing due to memory pressure, or if the page
1148 * is being written back. So grab it first before we start
1149 * the transaction handle. This also allows us to allocate
1150 * the page (if needed) without using GFP_NOFS.
1151 */
1152retry_grab:
Nick Piggin54566b22009-01-04 12:00:53 -08001153 page = grab_cache_page_write_begin(mapping, index, flags);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001154 if (!page)
1155 return -ENOMEM;
1156 unlock_page(page);
1157
1158retry_journal:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001159 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001160 if (IS_ERR(handle)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001161 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001162 return PTR_ERR(handle);
Jan Karacf108bc2008-07-11 19:27:31 -04001163 }
Tao Maf19d5872012-12-10 14:05:51 -05001164
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001165 lock_page(page);
1166 if (page->mapping != mapping) {
1167 /* The page got truncated from under us */
1168 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001169 put_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04001170 ext4_journal_stop(handle);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001171 goto retry_grab;
Jan Karacf108bc2008-07-11 19:27:31 -04001172 }
Dmitry Monakhov7afe5aa2013-08-28 14:30:47 -04001173 /* In case writeback began while the page was unlocked */
1174 wait_for_stable_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04001175
Chandan Rajendra643fa962018-12-12 15:20:12 +05301176#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrow2058f832015-04-12 00:55:10 -04001177 if (ext4_should_dioread_nolock(inode))
1178 ret = ext4_block_write_begin(page, pos, len,
Jan Kara705965b2016-03-08 23:08:10 -05001179 ext4_get_block_unwritten);
Michael Halcrow2058f832015-04-12 00:55:10 -04001180 else
1181 ret = ext4_block_write_begin(page, pos, len,
1182 ext4_get_block);
1183#else
Jiaying Zhang744692d2010-03-04 16:14:02 -05001184 if (ext4_should_dioread_nolock(inode))
Jan Kara705965b2016-03-08 23:08:10 -05001185 ret = __block_write_begin(page, pos, len,
1186 ext4_get_block_unwritten);
Jiaying Zhang744692d2010-03-04 16:14:02 -05001187 else
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001188 ret = __block_write_begin(page, pos, len, ext4_get_block);
Michael Halcrow2058f832015-04-12 00:55:10 -04001189#endif
Nick Pigginbfc1af62007-10-16 01:25:05 -07001190 if (!ret && ext4_should_journal_data(inode)) {
Tao Maf19d5872012-12-10 14:05:51 -05001191 ret = ext4_walk_page_buffers(handle, page_buffers(page),
1192 from, to, NULL,
1193 do_journal_get_write_access);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001194 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001195
1196 if (ret) {
Eric Biggersc93d8f82019-07-22 09:26:24 -07001197 bool extended = (pos + len > inode->i_size) &&
1198 !ext4_verity_in_progress(inode);
1199
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001200 unlock_page(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001201 /*
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001202 * __block_write_begin may have instantiated a few blocks
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001203 * outside i_size. Trim these off again. Don't need
1204 * i_size_read because we hold i_mutex.
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001205 *
1206 * Add inode to orphan list in case we crash before
1207 * truncate finishes
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001208 */
Eric Biggersc93d8f82019-07-22 09:26:24 -07001209 if (extended && ext4_can_truncate(inode))
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001210 ext4_orphan_add(handle, inode);
1211
1212 ext4_journal_stop(handle);
Eric Biggersc93d8f82019-07-22 09:26:24 -07001213 if (extended) {
Jan Karab9a42072009-12-08 21:24:33 -05001214 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001215 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001216 * If truncate failed early the inode might
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001217 * still be on the orphan list; we need to
1218 * make sure the inode is removed from the
1219 * orphan list in that case.
1220 */
1221 if (inode->i_nlink)
1222 ext4_orphan_del(NULL, inode);
1223 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001224
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001225 if (ret == -ENOSPC &&
1226 ext4_should_retry_alloc(inode->i_sb, &retries))
1227 goto retry_journal;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001228 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001229 return ret;
1230 }
1231 *pagep = page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001232 return ret;
1233}
1234
Nick Pigginbfc1af62007-10-16 01:25:05 -07001235/* For write_end() in data=journal mode */
1236static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001237{
Theodore Ts'o13fca322013-04-21 16:45:54 -04001238 int ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001239 if (!buffer_mapped(bh) || buffer_freed(bh))
1240 return 0;
1241 set_buffer_uptodate(bh);
Theodore Ts'o13fca322013-04-21 16:45:54 -04001242 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
1243 clear_buffer_meta(bh);
1244 clear_buffer_prio(bh);
1245 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001246}
1247
Zheng Liueed43332013-04-03 12:41:17 -04001248/*
1249 * We need to pick up the new inode size which generic_commit_write gave us
1250 * `file' can be NULL - eg, when called from page_symlink().
1251 *
1252 * ext4 never places buffers on inode->i_mapping->private_list. metadata
1253 * buffers are managed internally.
1254 */
1255static int ext4_write_end(struct file *file,
1256 struct address_space *mapping,
1257 loff_t pos, unsigned len, unsigned copied,
1258 struct page *page, void *fsdata)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001259{
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001260 handle_t *handle = ext4_journal_current_handle();
Zheng Liueed43332013-04-03 12:41:17 -04001261 struct inode *inode = mapping->host;
Xiaoguang Wang05726392015-02-12 23:00:17 -05001262 loff_t old_size = inode->i_size;
Zheng Liueed43332013-04-03 12:41:17 -04001263 int ret = 0, ret2;
1264 int i_size_changed = 0;
Theodore Ts'o362eca72018-07-10 01:07:43 -04001265 int inline_data = ext4_has_inline_data(inode);
Eric Biggersc93d8f82019-07-22 09:26:24 -07001266 bool verity = ext4_verity_in_progress(inode);
Zheng Liueed43332013-04-03 12:41:17 -04001267
1268 trace_ext4_write_end(inode, pos, len, copied);
Theodore Ts'o362eca72018-07-10 01:07:43 -04001269 if (inline_data) {
Theodore Ts'o42c832d2013-07-01 08:12:39 -04001270 ret = ext4_write_inline_data_end(inode, pos, len,
1271 copied, page);
Theodore Ts'oeb5efbc2017-02-04 23:04:00 -05001272 if (ret < 0) {
1273 unlock_page(page);
1274 put_page(page);
Theodore Ts'o42c832d2013-07-01 08:12:39 -04001275 goto errout;
Theodore Ts'oeb5efbc2017-02-04 23:04:00 -05001276 }
Theodore Ts'o42c832d2013-07-01 08:12:39 -04001277 copied = ret;
1278 } else
Tao Maf19d5872012-12-10 14:05:51 -05001279 copied = block_write_end(file, mapping, pos,
1280 len, copied, page, fsdata);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001281 /*
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001282 * it's important to update i_size while still holding page lock:
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001283 * page writeout could otherwise come in and zero beyond i_size.
Eric Biggersc93d8f82019-07-22 09:26:24 -07001284 *
1285 * If FS_IOC_ENABLE_VERITY is running on this inode, then Merkle tree
1286 * blocks are being written past EOF, so skip the i_size update.
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001287 */
Eric Biggersc93d8f82019-07-22 09:26:24 -07001288 if (!verity)
1289 i_size_changed = ext4_update_inode_size(inode, pos + copied);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001290 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001291 put_page(page);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001292
Eric Biggersc93d8f82019-07-22 09:26:24 -07001293 if (old_size < pos && !verity)
Xiaoguang Wang05726392015-02-12 23:00:17 -05001294 pagecache_isize_extended(inode, old_size, pos);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001295 /*
1296 * Don't mark the inode dirty under page lock. First, it unnecessarily
1297 * makes the holding time of page lock longer. Second, it forces lock
1298 * ordering of page lock and transaction start for journaling
1299 * filesystems.
1300 */
Theodore Ts'o362eca72018-07-10 01:07:43 -04001301 if (i_size_changed || inline_data)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001302 ext4_mark_inode_dirty(handle, inode);
1303
Eric Biggersc93d8f82019-07-22 09:26:24 -07001304 if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001305 /* if we have allocated more blocks and copied
1306 * less. We will have blocks allocated outside
1307 * inode->i_size. So truncate them
1308 */
1309 ext4_orphan_add(handle, inode);
Theodore Ts'o74d553a2013-04-03 12:39:17 -04001310errout:
Mingming Cao617ba132006-10-11 01:20:53 -07001311 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001312 if (!ret)
1313 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001314
Eric Biggersc93d8f82019-07-22 09:26:24 -07001315 if (pos + len > inode->i_size && !verity) {
Jan Karab9a42072009-12-08 21:24:33 -05001316 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001317 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001318 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001319 * on the orphan list; we need to make sure the inode
1320 * is removed from the orphan list in that case.
1321 */
1322 if (inode->i_nlink)
1323 ext4_orphan_del(NULL, inode);
1324 }
1325
Nick Pigginbfc1af62007-10-16 01:25:05 -07001326 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001327}
1328
Theodore Ts'ob90197b2015-10-15 10:29:05 -04001329/*
1330 * This is a private version of page_zero_new_buffers() which doesn't
1331 * set the buffer to be dirty, since in data=journalled mode we need
1332 * to call ext4_handle_dirty_metadata() instead.
1333 */
Jan Kara3b136492017-01-27 14:35:38 -05001334static void ext4_journalled_zero_new_buffers(handle_t *handle,
1335 struct page *page,
1336 unsigned from, unsigned to)
Theodore Ts'ob90197b2015-10-15 10:29:05 -04001337{
1338 unsigned int block_start = 0, block_end;
1339 struct buffer_head *head, *bh;
1340
1341 bh = head = page_buffers(page);
1342 do {
1343 block_end = block_start + bh->b_size;
1344 if (buffer_new(bh)) {
1345 if (block_end > from && block_start < to) {
1346 if (!PageUptodate(page)) {
1347 unsigned start, size;
1348
1349 start = max(from, block_start);
1350 size = min(to, block_end) - start;
1351
1352 zero_user(page, start, size);
Jan Kara3b136492017-01-27 14:35:38 -05001353 write_end_fn(handle, bh);
Theodore Ts'ob90197b2015-10-15 10:29:05 -04001354 }
1355 clear_buffer_new(bh);
1356 }
1357 }
1358 block_start = block_end;
1359 bh = bh->b_this_page;
1360 } while (bh != head);
1361}
1362
Nick Pigginbfc1af62007-10-16 01:25:05 -07001363static int ext4_journalled_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001364 struct address_space *mapping,
1365 loff_t pos, unsigned len, unsigned copied,
1366 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001367{
Mingming Cao617ba132006-10-11 01:20:53 -07001368 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -07001369 struct inode *inode = mapping->host;
Xiaoguang Wang05726392015-02-12 23:00:17 -05001370 loff_t old_size = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001371 int ret = 0, ret2;
1372 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001373 unsigned from, to;
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001374 int size_changed = 0;
Theodore Ts'o362eca72018-07-10 01:07:43 -04001375 int inline_data = ext4_has_inline_data(inode);
Eric Biggersc93d8f82019-07-22 09:26:24 -07001376 bool verity = ext4_verity_in_progress(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001377
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001378 trace_ext4_journalled_write_end(inode, pos, len, copied);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001379 from = pos & (PAGE_SIZE - 1);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001380 to = from + len;
1381
Curt Wohlgemuth441c8502011-08-13 11:25:18 -04001382 BUG_ON(!ext4_handle_valid(handle));
1383
Theodore Ts'o362eca72018-07-10 01:07:43 -04001384 if (inline_data) {
Theodore Ts'oeb5efbc2017-02-04 23:04:00 -05001385 ret = ext4_write_inline_data_end(inode, pos, len,
1386 copied, page);
1387 if (ret < 0) {
1388 unlock_page(page);
1389 put_page(page);
1390 goto errout;
1391 }
1392 copied = ret;
1393 } else if (unlikely(copied < len) && !PageUptodate(page)) {
Jan Kara3b136492017-01-27 14:35:38 -05001394 copied = 0;
1395 ext4_journalled_zero_new_buffers(handle, page, from, to);
1396 } else {
1397 if (unlikely(copied < len))
1398 ext4_journalled_zero_new_buffers(handle, page,
1399 from + copied, to);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001400 ret = ext4_walk_page_buffers(handle, page_buffers(page), from,
Jan Kara3b136492017-01-27 14:35:38 -05001401 from + copied, &partial,
1402 write_end_fn);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001403 if (!partial)
1404 SetPageUptodate(page);
1405 }
Eric Biggersc93d8f82019-07-22 09:26:24 -07001406 if (!verity)
1407 size_changed = ext4_update_inode_size(inode, pos + copied);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001408 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Jan Kara2d859db2011-07-26 09:07:11 -04001409 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001410 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001411 put_page(page);
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001412
Eric Biggersc93d8f82019-07-22 09:26:24 -07001413 if (old_size < pos && !verity)
Xiaoguang Wang05726392015-02-12 23:00:17 -05001414 pagecache_isize_extended(inode, old_size, pos);
1415
Theodore Ts'o362eca72018-07-10 01:07:43 -04001416 if (size_changed || inline_data) {
Mingming Cao617ba132006-10-11 01:20:53 -07001417 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001418 if (!ret)
1419 ret = ret2;
1420 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001421
Eric Biggersc93d8f82019-07-22 09:26:24 -07001422 if (pos + len > inode->i_size && !verity && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001423 /* if we have allocated more blocks and copied
1424 * less. We will have blocks allocated outside
1425 * inode->i_size. So truncate them
1426 */
1427 ext4_orphan_add(handle, inode);
1428
Theodore Ts'oeb5efbc2017-02-04 23:04:00 -05001429errout:
Mingming Cao617ba132006-10-11 01:20:53 -07001430 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001431 if (!ret)
1432 ret = ret2;
Eric Biggersc93d8f82019-07-22 09:26:24 -07001433 if (pos + len > inode->i_size && !verity) {
Jan Karab9a42072009-12-08 21:24:33 -05001434 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001435 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001436 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001437 * on the orphan list; we need to make sure the inode
1438 * is removed from the orphan list in that case.
1439 */
1440 if (inode->i_nlink)
1441 ext4_orphan_del(NULL, inode);
1442 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001443
1444 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001445}
Mingming Caod2a17632008-07-14 17:52:37 -04001446
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001447/*
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001448 * Reserve space for a single cluster
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001449 */
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001450static int ext4_da_reserve_space(struct inode *inode)
Mingming Caod2a17632008-07-14 17:52:37 -04001451{
Mingming Cao60e58e02009-01-22 18:13:05 +01001452 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001453 struct ext4_inode_info *ei = EXT4_I(inode);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001454 int ret;
Mingming Caod2a17632008-07-14 17:52:37 -04001455
Mingming Cao60e58e02009-01-22 18:13:05 +01001456 /*
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001457 * We will charge metadata quota at writeout time; this saves
1458 * us from metadata over-estimation, though we may go over by
1459 * a small amount in the end. Here we just reserve for data.
Mingming Cao60e58e02009-01-22 18:13:05 +01001460 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04001461 ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001462 if (ret)
1463 return ret;
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001464
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001465 spin_lock(&ei->i_block_reservation_lock);
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04001466 if (ext4_claim_free_clusters(sbi, 1, 0)) {
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001467 spin_unlock(&ei->i_block_reservation_lock);
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001468 dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
Mingming Caod2a17632008-07-14 17:52:37 -04001469 return -ENOSPC;
1470 }
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001471 ei->i_reserved_data_blocks++;
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001472 trace_ext4_da_reserve_space(inode);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001473 spin_unlock(&ei->i_block_reservation_lock);
Dmitry Monakhov39bc6802009-12-10 16:36:27 +00001474
Mingming Caod2a17632008-07-14 17:52:37 -04001475 return 0; /* success */
1476}
1477
Eric Whitneyf4567672018-10-01 14:33:24 -04001478void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001479{
1480 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001481 struct ext4_inode_info *ei = EXT4_I(inode);
Mingming Caod2a17632008-07-14 17:52:37 -04001482
Mingming Caocd213222008-08-19 22:16:59 -04001483 if (!to_free)
1484 return; /* Nothing to release, exit */
1485
Mingming Caod2a17632008-07-14 17:52:37 -04001486 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001487
Li Zefan5a58ec872010-05-17 02:00:00 -04001488 trace_ext4_da_release_space(inode, to_free);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001489 if (unlikely(to_free > ei->i_reserved_data_blocks)) {
Mingming Caocd213222008-08-19 22:16:59 -04001490 /*
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001491 * if there aren't enough reserved blocks, then the
1492 * counter is messed up somewhere. Since this
1493 * function is called from invalidate page, it's
1494 * harmless to return without any action.
Mingming Caocd213222008-08-19 22:16:59 -04001495 */
Theodore Ts'o8de5c322013-02-14 15:11:41 -05001496 ext4_warning(inode->i_sb, "ext4_da_release_space: "
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001497 "ino %lu, to_free %d with only %d reserved "
Theodore Ts'o1084f252012-03-19 23:13:43 -04001498 "data blocks", inode->i_ino, to_free,
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001499 ei->i_reserved_data_blocks);
1500 WARN_ON(1);
1501 to_free = ei->i_reserved_data_blocks;
1502 }
1503 ei->i_reserved_data_blocks -= to_free;
1504
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001505 /* update fs dirty data blocks counter */
Theodore Ts'o57042652011-09-09 18:56:51 -04001506 percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free);
Mingming Caod2a17632008-07-14 17:52:37 -04001507
Mingming Caod2a17632008-07-14 17:52:37 -04001508 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001509
Aditya Kali7b415bf2011-09-09 19:04:51 -04001510 dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free));
Mingming Caod2a17632008-07-14 17:52:37 -04001511}
1512
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001513/*
Alex Tomas64769242008-07-11 19:27:31 -04001514 * Delayed allocation stuff
1515 */
1516
Jan Kara4e7ea812013-06-04 13:17:40 -04001517struct mpage_da_data {
1518 struct inode *inode;
1519 struct writeback_control *wbc;
Jan Kara6b523df2013-06-04 13:21:11 -04001520
Jan Kara4e7ea812013-06-04 13:17:40 -04001521 pgoff_t first_page; /* The first page to write */
1522 pgoff_t next_page; /* Current page to examine */
1523 pgoff_t last_page; /* Last page to examine */
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001524 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04001525 * Extent to map - this can be after first_page because that can be
1526 * fully mapped. We somewhat abuse m_flags to store whether the extent
1527 * is delalloc or unwritten.
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001528 */
Jan Kara4e7ea812013-06-04 13:17:40 -04001529 struct ext4_map_blocks map;
1530 struct ext4_io_submit io_submit; /* IO submission data */
Jan Karadddbd6a2017-04-30 18:29:10 -04001531 unsigned int do_map:1;
Jan Kara4e7ea812013-06-04 13:17:40 -04001532};
Alex Tomas64769242008-07-11 19:27:31 -04001533
Jan Kara4e7ea812013-06-04 13:17:40 -04001534static void mpage_release_unused_pages(struct mpage_da_data *mpd,
1535 bool invalidate)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001536{
1537 int nr_pages, i;
1538 pgoff_t index, end;
1539 struct pagevec pvec;
1540 struct inode *inode = mpd->inode;
1541 struct address_space *mapping = inode->i_mapping;
Jan Kara4e7ea812013-06-04 13:17:40 -04001542
1543 /* This is necessary when next_page == 0. */
1544 if (mpd->first_page >= mpd->next_page)
1545 return;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001546
Curt Wohlgemuthc7f59382011-02-26 12:27:52 -05001547 index = mpd->first_page;
1548 end = mpd->next_page - 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04001549 if (invalidate) {
1550 ext4_lblk_t start, last;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001551 start = index << (PAGE_SHIFT - inode->i_blkbits);
1552 last = end << (PAGE_SHIFT - inode->i_blkbits);
Jan Kara4e7ea812013-06-04 13:17:40 -04001553 ext4_es_remove_extent(inode, start, last - start + 1);
1554 }
Zheng Liu51865fd2012-11-08 21:57:32 -05001555
Mel Gorman86679822017-11-15 17:37:52 -08001556 pagevec_init(&pvec);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001557 while (index <= end) {
Jan Kara397162f2017-09-06 16:21:43 -07001558 nr_pages = pagevec_lookup_range(&pvec, mapping, &index, end);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001559 if (nr_pages == 0)
1560 break;
1561 for (i = 0; i < nr_pages; i++) {
1562 struct page *page = pvec.pages[i];
Jan Kara2b85a612017-09-06 16:21:30 -07001563
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001564 BUG_ON(!PageLocked(page));
1565 BUG_ON(PageWriteback(page));
Jan Kara4e7ea812013-06-04 13:17:40 -04001566 if (invalidate) {
wangguang4e800c02016-09-15 11:32:46 -04001567 if (page_mapped(page))
1568 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001569 block_invalidatepage(page, 0, PAGE_SIZE);
Jan Kara4e7ea812013-06-04 13:17:40 -04001570 ClearPageUptodate(page);
1571 }
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001572 unlock_page(page);
1573 }
Jan Kara9b1d09982010-03-03 16:19:32 -05001574 pagevec_release(&pvec);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001575 }
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001576}
1577
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001578static void ext4_print_free_blocks(struct inode *inode)
1579{
1580 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o92b97812012-03-19 23:41:49 -04001581 struct super_block *sb = inode->i_sb;
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001582 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'o92b97812012-03-19 23:41:49 -04001583
1584 ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld",
Theodore Ts'o5dee5432011-09-09 19:10:51 -04001585 EXT4_C2B(EXT4_SB(inode->i_sb),
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001586 ext4_count_free_clusters(sb)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001587 ext4_msg(sb, KERN_CRIT, "Free/Dirty block details");
1588 ext4_msg(sb, KERN_CRIT, "free_blocks=%lld",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001589 (long long) EXT4_C2B(EXT4_SB(sb),
Theodore Ts'o57042652011-09-09 18:56:51 -04001590 percpu_counter_sum(&sbi->s_freeclusters_counter)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001591 ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001592 (long long) EXT4_C2B(EXT4_SB(sb),
Aditya Kali7b415bf2011-09-09 19:04:51 -04001593 percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001594 ext4_msg(sb, KERN_CRIT, "Block reservation details");
1595 ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001596 ei->i_reserved_data_blocks);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001597 return;
1598}
1599
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001600static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001601{
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001602 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001603}
1604
Alex Tomas64769242008-07-11 19:27:31 -04001605/*
Eric Whitney0b02f4c2018-10-01 14:19:37 -04001606 * ext4_insert_delayed_block - adds a delayed block to the extents status
1607 * tree, incrementing the reserved cluster/block
1608 * count or making a pending reservation
1609 * where needed
1610 *
1611 * @inode - file containing the newly added block
1612 * @lblk - logical block to be added
1613 *
1614 * Returns 0 on success, negative error code on failure.
1615 */
1616static int ext4_insert_delayed_block(struct inode *inode, ext4_lblk_t lblk)
1617{
1618 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1619 int ret;
1620 bool allocated = false;
1621
1622 /*
1623 * If the cluster containing lblk is shared with a delayed,
1624 * written, or unwritten extent in a bigalloc file system, it's
1625 * already been accounted for and does not need to be reserved.
1626 * A pending reservation must be made for the cluster if it's
1627 * shared with a written or unwritten extent and doesn't already
1628 * have one. Written and unwritten extents can be purged from the
1629 * extents status tree if the system is under memory pressure, so
1630 * it's necessary to examine the extent tree if a search of the
1631 * extents status tree doesn't get a match.
1632 */
1633 if (sbi->s_cluster_ratio == 1) {
1634 ret = ext4_da_reserve_space(inode);
1635 if (ret != 0) /* ENOSPC */
1636 goto errout;
1637 } else { /* bigalloc */
1638 if (!ext4_es_scan_clu(inode, &ext4_es_is_delonly, lblk)) {
1639 if (!ext4_es_scan_clu(inode,
1640 &ext4_es_is_mapped, lblk)) {
1641 ret = ext4_clu_mapped(inode,
1642 EXT4_B2C(sbi, lblk));
1643 if (ret < 0)
1644 goto errout;
1645 if (ret == 0) {
1646 ret = ext4_da_reserve_space(inode);
1647 if (ret != 0) /* ENOSPC */
1648 goto errout;
1649 } else {
1650 allocated = true;
1651 }
1652 } else {
1653 allocated = true;
1654 }
1655 }
1656 }
1657
1658 ret = ext4_es_insert_delayed_block(inode, lblk, allocated);
1659
1660errout:
1661 return ret;
1662}
1663
1664/*
Aditya Kali5356f2612011-09-09 19:20:51 -04001665 * This function is grabs code from the very beginning of
1666 * ext4_map_blocks, but assumes that the caller is from delayed write
1667 * time. This function looks up the requested blocks and sets the
1668 * buffer delay bit under the protection of i_data_sem.
1669 */
1670static int ext4_da_map_blocks(struct inode *inode, sector_t iblock,
1671 struct ext4_map_blocks *map,
1672 struct buffer_head *bh)
1673{
Zheng Liud100eef2013-02-18 00:29:59 -05001674 struct extent_status es;
Aditya Kali5356f2612011-09-09 19:20:51 -04001675 int retval;
1676 sector_t invalid_block = ~((sector_t) 0xffff);
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001677#ifdef ES_AGGRESSIVE_TEST
1678 struct ext4_map_blocks orig_map;
1679
1680 memcpy(&orig_map, map, sizeof(*map));
1681#endif
Aditya Kali5356f2612011-09-09 19:20:51 -04001682
1683 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
1684 invalid_block = ~0;
1685
1686 map->m_flags = 0;
1687 ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
1688 "logical block %lu\n", inode->i_ino, map->m_len,
1689 (unsigned long) map->m_lblk);
Zheng Liud100eef2013-02-18 00:29:59 -05001690
1691 /* Lookup extent status tree firstly */
Theodore Ts'obb5835e2019-08-11 16:32:41 -04001692 if (ext4_es_lookup_extent(inode, iblock, NULL, &es)) {
Zheng Liud100eef2013-02-18 00:29:59 -05001693 if (ext4_es_is_hole(&es)) {
1694 retval = 0;
Lukas Czernerc8b459f2014-05-12 12:55:07 -04001695 down_read(&EXT4_I(inode)->i_data_sem);
Zheng Liud100eef2013-02-18 00:29:59 -05001696 goto add_delayed;
1697 }
1698
1699 /*
1700 * Delayed extent could be allocated by fallocate.
1701 * So we need to check it.
1702 */
1703 if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) {
1704 map_bh(bh, inode->i_sb, invalid_block);
1705 set_buffer_new(bh);
1706 set_buffer_delay(bh);
1707 return 0;
1708 }
1709
1710 map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk;
1711 retval = es.es_len - (iblock - es.es_lblk);
1712 if (retval > map->m_len)
1713 retval = map->m_len;
1714 map->m_len = retval;
1715 if (ext4_es_is_written(&es))
1716 map->m_flags |= EXT4_MAP_MAPPED;
1717 else if (ext4_es_is_unwritten(&es))
1718 map->m_flags |= EXT4_MAP_UNWRITTEN;
1719 else
Arnd Bergmann1e83bc82019-04-07 12:24:43 -04001720 BUG();
Zheng Liud100eef2013-02-18 00:29:59 -05001721
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001722#ifdef ES_AGGRESSIVE_TEST
1723 ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0);
1724#endif
Zheng Liud100eef2013-02-18 00:29:59 -05001725 return retval;
1726 }
1727
Aditya Kali5356f2612011-09-09 19:20:51 -04001728 /*
1729 * Try to see if we can get the block without requesting a new
1730 * file system block.
1731 */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04001732 down_read(&EXT4_I(inode)->i_data_sem);
Jan Karacbd75842014-11-25 11:41:49 -05001733 if (ext4_has_inline_data(inode))
Tao Ma9c3569b2012-12-10 14:05:57 -05001734 retval = 0;
Jan Karacbd75842014-11-25 11:41:49 -05001735 else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Zheng Liu2f8e0a72014-11-25 11:44:37 -05001736 retval = ext4_ext_map_blocks(NULL, inode, map, 0);
Aditya Kali5356f2612011-09-09 19:20:51 -04001737 else
Zheng Liu2f8e0a72014-11-25 11:44:37 -05001738 retval = ext4_ind_map_blocks(NULL, inode, map, 0);
Aditya Kali5356f2612011-09-09 19:20:51 -04001739
Zheng Liud100eef2013-02-18 00:29:59 -05001740add_delayed:
Aditya Kali5356f2612011-09-09 19:20:51 -04001741 if (retval == 0) {
Zheng Liuf7fec032013-02-18 00:28:47 -05001742 int ret;
Eric Whitneyad431022018-10-01 14:10:39 -04001743
Aditya Kali5356f2612011-09-09 19:20:51 -04001744 /*
1745 * XXX: __block_prepare_write() unmaps passed block,
1746 * is it OK?
1747 */
Aditya Kali5356f2612011-09-09 19:20:51 -04001748
Eric Whitney0b02f4c2018-10-01 14:19:37 -04001749 ret = ext4_insert_delayed_block(inode, map->m_lblk);
1750 if (ret != 0) {
Zheng Liuf7fec032013-02-18 00:28:47 -05001751 retval = ret;
Zheng Liu51865fd2012-11-08 21:57:32 -05001752 goto out_unlock;
Zheng Liuf7fec032013-02-18 00:28:47 -05001753 }
Zheng Liu51865fd2012-11-08 21:57:32 -05001754
Aditya Kali5356f2612011-09-09 19:20:51 -04001755 map_bh(bh, inode->i_sb, invalid_block);
1756 set_buffer_new(bh);
1757 set_buffer_delay(bh);
Zheng Liuf7fec032013-02-18 00:28:47 -05001758 } else if (retval > 0) {
1759 int ret;
Theodore Ts'o3be78c72013-08-16 21:22:41 -04001760 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -05001761
Zheng Liu44fb851d2013-07-29 12:51:42 -04001762 if (unlikely(retval != map->m_len)) {
1763 ext4_warning(inode->i_sb,
1764 "ES len assertion failed for inode "
1765 "%lu: retval %d != map->m_len %d",
1766 inode->i_ino, retval, map->m_len);
1767 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001768 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001769
Zheng Liuf7fec032013-02-18 00:28:47 -05001770 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
1771 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
1772 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
1773 map->m_pblk, status);
1774 if (ret != 0)
1775 retval = ret;
Aditya Kali5356f2612011-09-09 19:20:51 -04001776 }
1777
1778out_unlock:
1779 up_read((&EXT4_I(inode)->i_data_sem));
1780
1781 return retval;
1782}
1783
1784/*
Seunghun Leed91bd2c2014-09-01 22:15:30 -04001785 * This is a special get_block_t callback which is used by
Theodore Ts'ob920c752009-05-14 00:54:29 -04001786 * ext4_da_write_begin(). It will either return mapped block or
1787 * reserve space for a single block.
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001788 *
1789 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
1790 * We also have b_blocknr = -1 and b_bdev initialized properly
1791 *
1792 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
1793 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
1794 * initialized properly.
Alex Tomas64769242008-07-11 19:27:31 -04001795 */
Tao Ma9c3569b2012-12-10 14:05:57 -05001796int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
1797 struct buffer_head *bh, int create)
Alex Tomas64769242008-07-11 19:27:31 -04001798{
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001799 struct ext4_map_blocks map;
Alex Tomas64769242008-07-11 19:27:31 -04001800 int ret = 0;
1801
1802 BUG_ON(create == 0);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001803 BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
1804
1805 map.m_lblk = iblock;
1806 map.m_len = 1;
Alex Tomas64769242008-07-11 19:27:31 -04001807
1808 /*
1809 * first, we need to know whether the block is allocated already
1810 * preallocated blocks are unmapped but should treated
1811 * the same as allocated blocks.
1812 */
Aditya Kali5356f2612011-09-09 19:20:51 -04001813 ret = ext4_da_map_blocks(inode, iblock, &map, bh);
1814 if (ret <= 0)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001815 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04001816
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001817 map_bh(bh, inode->i_sb, map.m_pblk);
Jan Karaed8ad832016-02-19 00:18:25 -05001818 ext4_update_bh_state(bh, map.m_flags);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001819
1820 if (buffer_unwritten(bh)) {
1821 /* A delayed write to unwritten bh should be marked
1822 * new and mapped. Mapped ensures that we don't do
1823 * get_block multiple times when we write to the same
1824 * offset and new ensures that we do proper zero out
1825 * for partial write.
1826 */
1827 set_buffer_new(bh);
Theodore Ts'oc8205632011-04-10 22:30:07 -04001828 set_buffer_mapped(bh);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001829 }
1830 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04001831}
Mingming Cao61628a32008-07-11 19:27:31 -04001832
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001833static int bget_one(handle_t *handle, struct buffer_head *bh)
1834{
1835 get_bh(bh);
1836 return 0;
1837}
1838
1839static int bput_one(handle_t *handle, struct buffer_head *bh)
1840{
1841 put_bh(bh);
1842 return 0;
1843}
1844
1845static int __ext4_journalled_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001846 unsigned int len)
1847{
1848 struct address_space *mapping = page->mapping;
1849 struct inode *inode = mapping->host;
Tao Ma3fdcfb62012-12-10 14:05:57 -05001850 struct buffer_head *page_bufs = NULL;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001851 handle_t *handle = NULL;
Tao Ma3fdcfb62012-12-10 14:05:57 -05001852 int ret = 0, err = 0;
1853 int inline_data = ext4_has_inline_data(inode);
1854 struct buffer_head *inode_bh = NULL;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001855
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001856 ClearPageChecked(page);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001857
1858 if (inline_data) {
1859 BUG_ON(page->index != 0);
1860 BUG_ON(len > ext4_get_max_inline_size(inode));
1861 inode_bh = ext4_journalled_write_inline_data(inode, len, page);
1862 if (inode_bh == NULL)
1863 goto out;
1864 } else {
1865 page_bufs = page_buffers(page);
1866 if (!page_bufs) {
1867 BUG();
1868 goto out;
1869 }
1870 ext4_walk_page_buffers(handle, page_bufs, 0, len,
1871 NULL, bget_one);
1872 }
Theodore Ts'obdf96832015-06-12 23:45:33 -04001873 /*
1874 * We need to release the page lock before we start the
1875 * journal, so grab a reference so the page won't disappear
1876 * out from under us.
1877 */
1878 get_page(page);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001879 unlock_page(page);
1880
Theodore Ts'o9924a922013-02-08 21:59:22 -05001881 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
1882 ext4_writepage_trans_blocks(inode));
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001883 if (IS_ERR(handle)) {
1884 ret = PTR_ERR(handle);
Theodore Ts'obdf96832015-06-12 23:45:33 -04001885 put_page(page);
1886 goto out_no_pagelock;
1887 }
1888 BUG_ON(!ext4_handle_valid(handle));
1889
1890 lock_page(page);
1891 put_page(page);
1892 if (page->mapping != mapping) {
1893 /* The page got truncated from under us */
1894 ext4_journal_stop(handle);
1895 ret = 0;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001896 goto out;
1897 }
1898
Tao Ma3fdcfb62012-12-10 14:05:57 -05001899 if (inline_data) {
Theodore Ts'o362eca72018-07-10 01:07:43 -04001900 ret = ext4_mark_inode_dirty(handle, inode);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001901 } else {
1902 ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
1903 do_journal_get_write_access);
1904
1905 err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
1906 write_end_fn);
1907 }
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001908 if (ret == 0)
1909 ret = err;
Jan Kara2d859db2011-07-26 09:07:11 -04001910 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001911 err = ext4_journal_stop(handle);
1912 if (!ret)
1913 ret = err;
1914
Tao Ma3fdcfb62012-12-10 14:05:57 -05001915 if (!ext4_has_inline_data(inode))
Theodore Ts'o8c9367f2014-01-07 13:08:03 -05001916 ext4_walk_page_buffers(NULL, page_bufs, 0, len,
Tao Ma3fdcfb62012-12-10 14:05:57 -05001917 NULL, bput_one);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001918 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001919out:
Theodore Ts'obdf96832015-06-12 23:45:33 -04001920 unlock_page(page);
1921out_no_pagelock:
Tao Ma3fdcfb62012-12-10 14:05:57 -05001922 brelse(inode_bh);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001923 return ret;
1924}
1925
Mingming Cao61628a32008-07-11 19:27:31 -04001926/*
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001927 * Note that we don't need to start a transaction unless we're journaling data
1928 * because we should have holes filled from ext4_page_mkwrite(). We even don't
1929 * need to file the inode to the transaction's list in ordered mode because if
1930 * we are writing back data added by write(), the inode is already there and if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001931 * we are writing back data modified via mmap(), no one guarantees in which
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001932 * transaction the data will hit the disk. In case we are journaling data, we
1933 * cannot start transaction directly because transaction start ranks above page
1934 * lock so we have to do some magic.
1935 *
Theodore Ts'ob920c752009-05-14 00:54:29 -04001936 * This function can get called via...
Theodore Ts'o20970ba2013-06-06 14:00:46 -04001937 * - ext4_writepages after taking page lock (have journal handle)
Theodore Ts'ob920c752009-05-14 00:54:29 -04001938 * - journal_submit_inode_data_buffers (no journal handle)
Artem Bityutskiyf6463b02012-07-25 18:12:04 +03001939 * - shrink_page_list via the kswapd/direct reclaim (no journal handle)
Theodore Ts'ob920c752009-05-14 00:54:29 -04001940 * - grab_page_cache when doing write_begin (have journal handle)
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001941 *
1942 * We don't do any block allocation in this function. If we have page with
1943 * multiple blocks we need to write those buffer_heads that are mapped. This
1944 * is important for mmaped based write. So if we do with blocksize 1K
1945 * truncate(f, 1024);
1946 * a = mmap(f, 0, 4096);
1947 * a[0] = 'a';
1948 * truncate(f, 4096);
1949 * we have in the page first buffer_head mapped via page_mkwrite call back
Paul Bolle90802ed2011-12-05 13:00:34 +01001950 * but other buffer_heads would be unmapped but dirty (dirty done via the
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001951 * do_wp_page). So writepage should write the first block. If we modify
1952 * the mmap area beyond 1024 we will again get a page_fault and the
1953 * page_mkwrite callback will do the block allocation and mark the
1954 * buffer_heads mapped.
1955 *
1956 * We redirty the page if we have any buffer_heads that is either delay or
1957 * unwritten in the page.
1958 *
1959 * We can get recursively called as show below.
1960 *
1961 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
1962 * ext4_writepage()
1963 *
1964 * But since we don't do any block allocation we should not deadlock.
1965 * Page also have the dirty flag cleared so we don't get recurive page_lock.
Mingming Cao61628a32008-07-11 19:27:31 -04001966 */
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001967static int ext4_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001968 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04001969{
Jan Karaf8bec372013-01-28 12:55:08 -05001970 int ret = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04001971 loff_t size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001972 unsigned int len;
Jiaying Zhang744692d2010-03-04 16:14:02 -05001973 struct buffer_head *page_bufs = NULL;
Mingming Cao61628a32008-07-11 19:27:31 -04001974 struct inode *inode = page->mapping->host;
Jan Kara36ade452013-01-28 09:30:52 -05001975 struct ext4_io_submit io_submit;
Namjae Jeon1c8349a2014-05-12 08:12:25 -04001976 bool keep_towrite = false;
Alex Tomas64769242008-07-11 19:27:31 -04001977
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05001978 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) {
1979 ext4_invalidatepage(page, 0, PAGE_SIZE);
1980 unlock_page(page);
1981 return -EIO;
1982 }
1983
Lukas Czernera9c667f2011-06-06 09:51:52 -04001984 trace_ext4_writepage(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04001985 size = i_size_read(inode);
Eric Biggersc93d8f82019-07-22 09:26:24 -07001986 if (page->index == size >> PAGE_SHIFT &&
1987 !ext4_verity_in_progress(inode))
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001988 len = size & ~PAGE_MASK;
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04001989 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001990 len = PAGE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04001991
Theodore Ts'oa42afc52010-10-27 21:30:09 -04001992 page_bufs = page_buffers(page);
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001993 /*
Jan Karafe386132013-01-28 21:06:42 -05001994 * We cannot do block allocation or other extent handling in this
1995 * function. If there are buffers needing that, we have to redirty
1996 * the page. But we may reach here when we do a journal commit via
1997 * journal_submit_inode_data_buffers() and in that case we must write
1998 * allocated buffers to achieve data=ordered mode guarantees.
Theodore Ts'occcd1472015-10-03 10:49:23 -04001999 *
2000 * Also, if there is only one buffer per page (the fs block
2001 * size == the page size), if one buffer needs block
2002 * allocation or needs to modify the extent tree to clear the
2003 * unwritten flag, we know that the page can't be written at
2004 * all, so we might as well refuse the write immediately.
2005 * Unfortunately if the block size != page size, we can't as
2006 * easily detect this case using ext4_walk_page_buffers(), but
2007 * for the extremely common case, this is an optimization that
2008 * skips a useless round trip through ext4_bio_write_page().
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04002009 */
Tao Maf19d5872012-12-10 14:05:51 -05002010 if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL,
2011 ext4_bh_delay_or_unwritten)) {
Jan Karaf8bec372013-01-28 12:55:08 -05002012 redirty_page_for_writepage(wbc, page);
Theodore Ts'occcd1472015-10-03 10:49:23 -04002013 if ((current->flags & PF_MEMALLOC) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002014 (inode->i_sb->s_blocksize == PAGE_SIZE)) {
Jan Karafe386132013-01-28 21:06:42 -05002015 /*
2016 * For memory cleaning there's no point in writing only
2017 * some buffers. So just bail out. Warn if we came here
2018 * from direct reclaim.
2019 */
2020 WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD))
2021 == PF_MEMALLOC);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002022 unlock_page(page);
2023 return 0;
2024 }
Namjae Jeon1c8349a2014-05-12 08:12:25 -04002025 keep_towrite = true;
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002026 }
Alex Tomas64769242008-07-11 19:27:31 -04002027
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002028 if (PageChecked(page) && ext4_should_journal_data(inode))
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002029 /*
2030 * It's mmapped pagecache. Add buffers and journal it. There
2031 * doesn't seem much point in redirtying the page here.
2032 */
Wu Fengguang3f0ca302009-11-24 11:15:44 -05002033 return __ext4_journalled_writepage(page, len);
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002034
Jan Kara97a851e2013-06-04 11:58:58 -04002035 ext4_io_submit_init(&io_submit, wbc);
2036 io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS);
2037 if (!io_submit.io_end) {
2038 redirty_page_for_writepage(wbc, page);
2039 unlock_page(page);
2040 return -ENOMEM;
2041 }
Namjae Jeon1c8349a2014-05-12 08:12:25 -04002042 ret = ext4_bio_write_page(&io_submit, page, len, wbc, keep_towrite);
Jan Kara36ade452013-01-28 09:30:52 -05002043 ext4_io_submit(&io_submit);
Jan Kara97a851e2013-06-04 11:58:58 -04002044 /* Drop io_end reference we got from init */
2045 ext4_put_io_end_defer(io_submit.io_end);
Alex Tomas64769242008-07-11 19:27:31 -04002046 return ret;
2047}
2048
Jan Kara5f1132b2013-08-17 10:02:33 -04002049static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page)
2050{
2051 int len;
Jan Karaa056bda2017-05-26 17:45:45 -04002052 loff_t size;
Jan Kara5f1132b2013-08-17 10:02:33 -04002053 int err;
2054
2055 BUG_ON(page->index != mpd->first_page);
Jan Karaa056bda2017-05-26 17:45:45 -04002056 clear_page_dirty_for_io(page);
2057 /*
2058 * We have to be very careful here! Nothing protects writeback path
2059 * against i_size changes and the page can be writeably mapped into
2060 * page tables. So an application can be growing i_size and writing
2061 * data through mmap while writeback runs. clear_page_dirty_for_io()
2062 * write-protects our page in page tables and the page cannot get
2063 * written to again until we release page lock. So only after
2064 * clear_page_dirty_for_io() we are safe to sample i_size for
2065 * ext4_bio_write_page() to zero-out tail of the written page. We rely
2066 * on the barrier provided by TestClearPageDirty in
2067 * clear_page_dirty_for_io() to make sure i_size is really sampled only
2068 * after page tables are updated.
2069 */
2070 size = i_size_read(mpd->inode);
Eric Biggersc93d8f82019-07-22 09:26:24 -07002071 if (page->index == size >> PAGE_SHIFT &&
2072 !ext4_verity_in_progress(mpd->inode))
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002073 len = size & ~PAGE_MASK;
Jan Kara5f1132b2013-08-17 10:02:33 -04002074 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002075 len = PAGE_SIZE;
Namjae Jeon1c8349a2014-05-12 08:12:25 -04002076 err = ext4_bio_write_page(&mpd->io_submit, page, len, mpd->wbc, false);
Jan Kara5f1132b2013-08-17 10:02:33 -04002077 if (!err)
2078 mpd->wbc->nr_to_write--;
2079 mpd->first_page++;
2080
2081 return err;
2082}
2083
Jan Kara4e7ea812013-06-04 13:17:40 -04002084#define BH_FLAGS ((1 << BH_Unwritten) | (1 << BH_Delay))
2085
Mingming Cao61628a32008-07-11 19:27:31 -04002086/*
Jan Karafffb2732013-06-04 13:01:11 -04002087 * mballoc gives us at most this number of blocks...
2088 * XXX: That seems to be only a limitation of ext4_mb_normalize_request().
Anatol Pomozov70261f52013-08-28 14:40:12 -04002089 * The rest of mballoc seems to handle chunks up to full group size.
Mingming Cao61628a32008-07-11 19:27:31 -04002090 */
Jan Karafffb2732013-06-04 13:01:11 -04002091#define MAX_WRITEPAGES_EXTENT_LEN 2048
Mingming Cao525f4ed2008-08-19 22:15:58 -04002092
Jan Karafffb2732013-06-04 13:01:11 -04002093/*
Jan Kara4e7ea812013-06-04 13:17:40 -04002094 * mpage_add_bh_to_extent - try to add bh to extent of blocks to map
2095 *
2096 * @mpd - extent of blocks
2097 * @lblk - logical number of the block in the file
Jan Kara09930042013-08-17 09:57:56 -04002098 * @bh - buffer head we want to add to the extent
Jan Kara4e7ea812013-06-04 13:17:40 -04002099 *
Jan Kara09930042013-08-17 09:57:56 -04002100 * The function is used to collect contig. blocks in the same state. If the
2101 * buffer doesn't require mapping for writeback and we haven't started the
2102 * extent of buffers to map yet, the function returns 'true' immediately - the
2103 * caller can write the buffer right away. Otherwise the function returns true
2104 * if the block has been added to the extent, false if the block couldn't be
2105 * added.
Jan Kara4e7ea812013-06-04 13:17:40 -04002106 */
Jan Kara09930042013-08-17 09:57:56 -04002107static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk,
2108 struct buffer_head *bh)
Jan Kara4e7ea812013-06-04 13:17:40 -04002109{
2110 struct ext4_map_blocks *map = &mpd->map;
2111
Jan Kara09930042013-08-17 09:57:56 -04002112 /* Buffer that doesn't need mapping for writeback? */
2113 if (!buffer_dirty(bh) || !buffer_mapped(bh) ||
2114 (!buffer_delay(bh) && !buffer_unwritten(bh))) {
2115 /* So far no extent to map => we write the buffer right away */
2116 if (map->m_len == 0)
2117 return true;
2118 return false;
2119 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002120
2121 /* First block in the extent? */
2122 if (map->m_len == 0) {
Jan Karadddbd6a2017-04-30 18:29:10 -04002123 /* We cannot map unless handle is started... */
2124 if (!mpd->do_map)
2125 return false;
Jan Kara4e7ea812013-06-04 13:17:40 -04002126 map->m_lblk = lblk;
2127 map->m_len = 1;
Jan Kara09930042013-08-17 09:57:56 -04002128 map->m_flags = bh->b_state & BH_FLAGS;
2129 return true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002130 }
2131
Jan Kara09930042013-08-17 09:57:56 -04002132 /* Don't go larger than mballoc is willing to allocate */
2133 if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN)
2134 return false;
2135
Jan Kara4e7ea812013-06-04 13:17:40 -04002136 /* Can we merge the block to our big extent? */
2137 if (lblk == map->m_lblk + map->m_len &&
Jan Kara09930042013-08-17 09:57:56 -04002138 (bh->b_state & BH_FLAGS) == map->m_flags) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002139 map->m_len++;
Jan Kara09930042013-08-17 09:57:56 -04002140 return true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002141 }
Jan Kara09930042013-08-17 09:57:56 -04002142 return false;
Jan Kara4e7ea812013-06-04 13:17:40 -04002143}
2144
Jan Kara5f1132b2013-08-17 10:02:33 -04002145/*
2146 * mpage_process_page_bufs - submit page buffers for IO or add them to extent
2147 *
2148 * @mpd - extent of blocks for mapping
2149 * @head - the first buffer in the page
2150 * @bh - buffer we should start processing from
2151 * @lblk - logical number of the block in the file corresponding to @bh
2152 *
2153 * Walk through page buffers from @bh upto @head (exclusive) and either submit
2154 * the page for IO if all buffers in this page were mapped and there's no
2155 * accumulated extent of buffers to map or add buffers in the page to the
2156 * extent of buffers to map. The function returns 1 if the caller can continue
2157 * by processing the next page, 0 if it should stop adding buffers to the
2158 * extent to map because we cannot extend it anymore. It can also return value
2159 * < 0 in case of error during IO submission.
2160 */
2161static int mpage_process_page_bufs(struct mpage_da_data *mpd,
2162 struct buffer_head *head,
2163 struct buffer_head *bh,
2164 ext4_lblk_t lblk)
Jan Kara4e7ea812013-06-04 13:17:40 -04002165{
2166 struct inode *inode = mpd->inode;
Jan Kara5f1132b2013-08-17 10:02:33 -04002167 int err;
Fabian Frederick93407472017-02-27 14:28:32 -08002168 ext4_lblk_t blocks = (i_size_read(inode) + i_blocksize(inode) - 1)
Jan Kara4e7ea812013-06-04 13:17:40 -04002169 >> inode->i_blkbits;
2170
Eric Biggersc93d8f82019-07-22 09:26:24 -07002171 if (ext4_verity_in_progress(inode))
2172 blocks = EXT_MAX_BLOCKS;
2173
Jan Kara4e7ea812013-06-04 13:17:40 -04002174 do {
2175 BUG_ON(buffer_locked(bh));
2176
Jan Kara09930042013-08-17 09:57:56 -04002177 if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002178 /* Found extent to map? */
2179 if (mpd->map.m_len)
Jan Kara5f1132b2013-08-17 10:02:33 -04002180 return 0;
Jan Karadddbd6a2017-04-30 18:29:10 -04002181 /* Buffer needs mapping and handle is not started? */
2182 if (!mpd->do_map)
2183 return 0;
Jan Kara09930042013-08-17 09:57:56 -04002184 /* Everything mapped so far and we hit EOF */
Jan Kara5f1132b2013-08-17 10:02:33 -04002185 break;
Jan Kara4e7ea812013-06-04 13:17:40 -04002186 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002187 } while (lblk++, (bh = bh->b_this_page) != head);
Jan Kara5f1132b2013-08-17 10:02:33 -04002188 /* So far everything mapped? Submit the page for IO. */
2189 if (mpd->map.m_len == 0) {
2190 err = mpage_submit_page(mpd, head->b_page);
2191 if (err < 0)
2192 return err;
2193 }
2194 return lblk < blocks;
Jan Kara4e7ea812013-06-04 13:17:40 -04002195}
2196
2197/*
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302198 * mpage_process_page - update page buffers corresponding to changed extent and
2199 * may submit fully mapped page for IO
2200 *
2201 * @mpd - description of extent to map, on return next extent to map
2202 * @m_lblk - logical block mapping.
2203 * @m_pblk - corresponding physical mapping.
2204 * @map_bh - determines on return whether this page requires any further
2205 * mapping or not.
2206 * Scan given page buffers corresponding to changed extent and update buffer
2207 * state according to new extent state.
2208 * We map delalloc buffers to their physical location, clear unwritten bits.
2209 * If the given page is not fully mapped, we update @map to the next extent in
2210 * the given page that needs mapping & return @map_bh as true.
2211 */
2212static int mpage_process_page(struct mpage_da_data *mpd, struct page *page,
2213 ext4_lblk_t *m_lblk, ext4_fsblk_t *m_pblk,
2214 bool *map_bh)
2215{
2216 struct buffer_head *head, *bh;
2217 ext4_io_end_t *io_end = mpd->io_submit.io_end;
2218 ext4_lblk_t lblk = *m_lblk;
2219 ext4_fsblk_t pblock = *m_pblk;
2220 int err = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302221 int blkbits = mpd->inode->i_blkbits;
2222 ssize_t io_end_size = 0;
2223 struct ext4_io_end_vec *io_end_vec = ext4_last_io_end_vec(io_end);
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302224
2225 bh = head = page_buffers(page);
2226 do {
2227 if (lblk < mpd->map.m_lblk)
2228 continue;
2229 if (lblk >= mpd->map.m_lblk + mpd->map.m_len) {
2230 /*
2231 * Buffer after end of mapped extent.
2232 * Find next buffer in the page to map.
2233 */
2234 mpd->map.m_len = 0;
2235 mpd->map.m_flags = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302236 io_end_vec->size += io_end_size;
2237 io_end_size = 0;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302238
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302239 err = mpage_process_page_bufs(mpd, head, bh, lblk);
2240 if (err > 0)
2241 err = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302242 if (!err && mpd->map.m_len && mpd->map.m_lblk > lblk) {
2243 io_end_vec = ext4_alloc_io_end_vec(io_end);
Ritesh Harjani4d06bfb2019-11-06 15:08:09 +05302244 if (IS_ERR(io_end_vec)) {
2245 err = PTR_ERR(io_end_vec);
2246 goto out;
2247 }
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302248 io_end_vec->offset = mpd->map.m_lblk << blkbits;
2249 }
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302250 *map_bh = true;
2251 goto out;
2252 }
2253 if (buffer_delay(bh)) {
2254 clear_buffer_delay(bh);
2255 bh->b_blocknr = pblock++;
2256 }
2257 clear_buffer_unwritten(bh);
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302258 io_end_size += (1 << blkbits);
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302259 } while (lblk++, (bh = bh->b_this_page) != head);
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302260
2261 io_end_vec->size += io_end_size;
2262 io_end_size = 0;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302263 *map_bh = false;
2264out:
2265 *m_lblk = lblk;
2266 *m_pblk = pblock;
2267 return err;
2268}
2269
2270/*
Jan Kara4e7ea812013-06-04 13:17:40 -04002271 * mpage_map_buffers - update buffers corresponding to changed extent and
2272 * submit fully mapped pages for IO
2273 *
2274 * @mpd - description of extent to map, on return next extent to map
2275 *
2276 * Scan buffers corresponding to changed extent (we expect corresponding pages
2277 * to be already locked) and update buffer state according to new extent state.
2278 * We map delalloc buffers to their physical location, clear unwritten bits,
Lukas Czerner556615d2014-04-20 23:45:47 -04002279 * and mark buffers as uninit when we perform writes to unwritten extents
Jan Kara4e7ea812013-06-04 13:17:40 -04002280 * and do extent conversion after IO is finished. If the last page is not fully
2281 * mapped, we update @map to the next extent in the last page that needs
2282 * mapping. Otherwise we submit the page for IO.
2283 */
2284static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd)
2285{
2286 struct pagevec pvec;
2287 int nr_pages, i;
2288 struct inode *inode = mpd->inode;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002289 int bpp_bits = PAGE_SHIFT - inode->i_blkbits;
Jan Kara4e7ea812013-06-04 13:17:40 -04002290 pgoff_t start, end;
2291 ext4_lblk_t lblk;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302292 ext4_fsblk_t pblock;
Jan Kara4e7ea812013-06-04 13:17:40 -04002293 int err;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302294 bool map_bh = false;
Jan Kara4e7ea812013-06-04 13:17:40 -04002295
2296 start = mpd->map.m_lblk >> bpp_bits;
2297 end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits;
2298 lblk = start << bpp_bits;
2299 pblock = mpd->map.m_pblk;
2300
Mel Gorman86679822017-11-15 17:37:52 -08002301 pagevec_init(&pvec);
Jan Kara4e7ea812013-06-04 13:17:40 -04002302 while (start <= end) {
Jan Kara2b85a612017-09-06 16:21:30 -07002303 nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping,
Jan Kara397162f2017-09-06 16:21:43 -07002304 &start, end);
Jan Kara4e7ea812013-06-04 13:17:40 -04002305 if (nr_pages == 0)
2306 break;
2307 for (i = 0; i < nr_pages; i++) {
2308 struct page *page = pvec.pages[i];
2309
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302310 err = mpage_process_page(mpd, page, &lblk, &pblock,
2311 &map_bh);
Jan Kara4e7ea812013-06-04 13:17:40 -04002312 /*
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302313 * If map_bh is true, means page may require further bh
2314 * mapping, or maybe the page was submitted for IO.
2315 * So we return to call further extent mapping.
Jan Kara4e7ea812013-06-04 13:17:40 -04002316 */
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302317 if (err < 0 || map_bh == true)
2318 goto out;
Jan Kara4e7ea812013-06-04 13:17:40 -04002319 /* Page fully mapped - let IO run! */
2320 err = mpage_submit_page(mpd, page);
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302321 if (err < 0)
2322 goto out;
Jan Kara4e7ea812013-06-04 13:17:40 -04002323 }
2324 pagevec_release(&pvec);
2325 }
2326 /* Extent fully mapped and matches with page boundary. We are done. */
2327 mpd->map.m_len = 0;
2328 mpd->map.m_flags = 0;
2329 return 0;
Ritesh Harjani2943fdb2019-10-16 13:07:09 +05302330out:
2331 pagevec_release(&pvec);
2332 return err;
Jan Kara4e7ea812013-06-04 13:17:40 -04002333}
2334
2335static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd)
2336{
2337 struct inode *inode = mpd->inode;
2338 struct ext4_map_blocks *map = &mpd->map;
2339 int get_blocks_flags;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002340 int err, dioread_nolock;
Jan Kara4e7ea812013-06-04 13:17:40 -04002341
2342 trace_ext4_da_write_pages_extent(inode, map);
2343 /*
2344 * Call ext4_map_blocks() to allocate any delayed allocation blocks, or
Lukas Czerner556615d2014-04-20 23:45:47 -04002345 * to convert an unwritten extent to be initialized (in the case
Jan Kara4e7ea812013-06-04 13:17:40 -04002346 * where we have written into one or more preallocated blocks). It is
2347 * possible that we're going to need more metadata blocks than
2348 * previously reserved. However we must not fail because we're in
2349 * writeback and there is nothing we can do about it so it might result
2350 * in data loss. So use reserved blocks to allocate metadata if
2351 * possible.
2352 *
Theodore Ts'o754cfed2014-09-04 18:08:22 -04002353 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if
2354 * the blocks in question are delalloc blocks. This indicates
2355 * that the blocks and quotas has already been checked when
2356 * the data was copied into the page cache.
Jan Kara4e7ea812013-06-04 13:17:40 -04002357 */
2358 get_blocks_flags = EXT4_GET_BLOCKS_CREATE |
Jan Karaee0876b2016-04-24 00:56:08 -04002359 EXT4_GET_BLOCKS_METADATA_NOFAIL |
2360 EXT4_GET_BLOCKS_IO_SUBMIT;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002361 dioread_nolock = ext4_should_dioread_nolock(inode);
2362 if (dioread_nolock)
Jan Kara4e7ea812013-06-04 13:17:40 -04002363 get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
2364 if (map->m_flags & (1 << BH_Delay))
2365 get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
2366
2367 err = ext4_map_blocks(handle, inode, map, get_blocks_flags);
2368 if (err < 0)
2369 return err;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002370 if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) {
Jan Kara6b523df2013-06-04 13:21:11 -04002371 if (!mpd->io_submit.io_end->handle &&
2372 ext4_handle_valid(handle)) {
2373 mpd->io_submit.io_end->handle = handle->h_rsv_handle;
2374 handle->h_rsv_handle = NULL;
2375 }
Jan Kara3613d222013-06-04 13:19:34 -04002376 ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end);
Jan Kara6b523df2013-06-04 13:21:11 -04002377 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002378
2379 BUG_ON(map->m_len == 0);
Jan Kara4e7ea812013-06-04 13:17:40 -04002380 return 0;
2381}
2382
2383/*
2384 * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length
2385 * mpd->len and submit pages underlying it for IO
2386 *
2387 * @handle - handle for journal operations
2388 * @mpd - extent to map
Jan Kara7534e852013-10-16 08:26:08 -04002389 * @give_up_on_write - we set this to true iff there is a fatal error and there
2390 * is no hope of writing the data. The caller should discard
2391 * dirty pages to avoid infinite loops.
Jan Kara4e7ea812013-06-04 13:17:40 -04002392 *
2393 * The function maps extent starting at mpd->lblk of length mpd->len. If it is
2394 * delayed, blocks are allocated, if it is unwritten, we may need to convert
2395 * them to initialized or split the described range from larger unwritten
2396 * extent. Note that we need not map all the described range since allocation
2397 * can return less blocks or the range is covered by more unwritten extents. We
2398 * cannot map more because we are limited by reserved transaction credits. On
2399 * the other hand we always make sure that the last touched page is fully
2400 * mapped so that it can be written out (and thus forward progress is
2401 * guaranteed). After mapping we submit all mapped pages for IO.
2402 */
2403static int mpage_map_and_submit_extent(handle_t *handle,
Theodore Ts'ocb530542013-07-01 08:12:40 -04002404 struct mpage_da_data *mpd,
2405 bool *give_up_on_write)
Jan Kara4e7ea812013-06-04 13:17:40 -04002406{
2407 struct inode *inode = mpd->inode;
2408 struct ext4_map_blocks *map = &mpd->map;
2409 int err;
2410 loff_t disksize;
Dmitry Monakhov66031202014-08-27 18:40:03 -04002411 int progress = 0;
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302412 ext4_io_end_t *io_end = mpd->io_submit.io_end;
Ritesh Harjani4d06bfb2019-11-06 15:08:09 +05302413 struct ext4_io_end_vec *io_end_vec;
Jan Kara4e7ea812013-06-04 13:17:40 -04002414
Ritesh Harjani4d06bfb2019-11-06 15:08:09 +05302415 io_end_vec = ext4_alloc_io_end_vec(io_end);
2416 if (IS_ERR(io_end_vec))
2417 return PTR_ERR(io_end_vec);
Ritesh Harjanic8cc8812019-10-16 13:07:10 +05302418 io_end_vec->offset = ((loff_t)map->m_lblk) << inode->i_blkbits;
Jan Kara27d7c4e2013-07-05 21:57:22 -04002419 do {
Jan Kara4e7ea812013-06-04 13:17:40 -04002420 err = mpage_map_one_extent(handle, mpd);
2421 if (err < 0) {
2422 struct super_block *sb = inode->i_sb;
2423
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002424 if (ext4_forced_shutdown(EXT4_SB(sb)) ||
2425 EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)
Theodore Ts'ocb530542013-07-01 08:12:40 -04002426 goto invalidate_dirty_pages;
Jan Kara4e7ea812013-06-04 13:17:40 -04002427 /*
Theodore Ts'ocb530542013-07-01 08:12:40 -04002428 * Let the uper layers retry transient errors.
2429 * In the case of ENOSPC, if ext4_count_free_blocks()
2430 * is non-zero, a commit should free up blocks.
Jan Kara4e7ea812013-06-04 13:17:40 -04002431 */
Theodore Ts'ocb530542013-07-01 08:12:40 -04002432 if ((err == -ENOMEM) ||
Dmitry Monakhov66031202014-08-27 18:40:03 -04002433 (err == -ENOSPC && ext4_count_free_clusters(sb))) {
2434 if (progress)
2435 goto update_disksize;
Theodore Ts'ocb530542013-07-01 08:12:40 -04002436 return err;
Dmitry Monakhov66031202014-08-27 18:40:03 -04002437 }
Theodore Ts'ocb530542013-07-01 08:12:40 -04002438 ext4_msg(sb, KERN_CRIT,
2439 "Delayed block allocation failed for "
2440 "inode %lu at logical offset %llu with"
2441 " max blocks %u with error %d",
2442 inode->i_ino,
2443 (unsigned long long)map->m_lblk,
2444 (unsigned)map->m_len, -err);
2445 ext4_msg(sb, KERN_CRIT,
2446 "This should not happen!! Data will "
2447 "be lost\n");
2448 if (err == -ENOSPC)
2449 ext4_print_free_blocks(inode);
2450 invalidate_dirty_pages:
2451 *give_up_on_write = true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002452 return err;
2453 }
Dmitry Monakhov66031202014-08-27 18:40:03 -04002454 progress = 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04002455 /*
2456 * Update buffer state, submit mapped pages, and get us new
2457 * extent to map
2458 */
2459 err = mpage_map_and_submit_buffers(mpd);
2460 if (err < 0)
Dmitry Monakhov66031202014-08-27 18:40:03 -04002461 goto update_disksize;
Jan Kara27d7c4e2013-07-05 21:57:22 -04002462 } while (map->m_len);
Jan Kara4e7ea812013-06-04 13:17:40 -04002463
Dmitry Monakhov66031202014-08-27 18:40:03 -04002464update_disksize:
Theodore Ts'o622cad12014-04-11 10:35:17 -04002465 /*
2466 * Update on-disk size after IO is submitted. Races with
2467 * truncate are avoided by checking i_size under i_data_sem.
2468 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002469 disksize = ((loff_t)mpd->first_page) << PAGE_SHIFT;
Jan Kara4e7ea812013-06-04 13:17:40 -04002470 if (disksize > EXT4_I(inode)->i_disksize) {
2471 int err2;
Theodore Ts'o622cad12014-04-11 10:35:17 -04002472 loff_t i_size;
Jan Kara4e7ea812013-06-04 13:17:40 -04002473
Theodore Ts'o622cad12014-04-11 10:35:17 -04002474 down_write(&EXT4_I(inode)->i_data_sem);
2475 i_size = i_size_read(inode);
2476 if (disksize > i_size)
2477 disksize = i_size;
2478 if (disksize > EXT4_I(inode)->i_disksize)
2479 EXT4_I(inode)->i_disksize = disksize;
Theodore Ts'o622cad12014-04-11 10:35:17 -04002480 up_write(&EXT4_I(inode)->i_data_sem);
Theodore Ts'ob907f2d2017-01-11 22:14:49 -05002481 err2 = ext4_mark_inode_dirty(handle, inode);
Theodore Ts'o878520a2019-11-19 21:54:15 -05002482 if (err2) {
2483 ext4_set_errno(inode->i_sb, -err2);
Jan Kara4e7ea812013-06-04 13:17:40 -04002484 ext4_error(inode->i_sb,
2485 "Failed to mark inode %lu dirty",
2486 inode->i_ino);
Theodore Ts'o878520a2019-11-19 21:54:15 -05002487 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002488 if (!err)
2489 err = err2;
2490 }
2491 return err;
2492}
2493
2494/*
Jan Karafffb2732013-06-04 13:01:11 -04002495 * Calculate the total number of credits to reserve for one writepages
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002496 * iteration. This is called from ext4_writepages(). We map an extent of
Anatol Pomozov70261f52013-08-28 14:40:12 -04002497 * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping
Jan Karafffb2732013-06-04 13:01:11 -04002498 * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN +
2499 * bpp - 1 blocks in bpp different extents.
2500 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002501static int ext4_da_writepages_trans_blocks(struct inode *inode)
2502{
Jan Karafffb2732013-06-04 13:01:11 -04002503 int bpp = ext4_journal_blocks_per_page(inode);
Mingming Cao525f4ed2008-08-19 22:15:58 -04002504
Jan Karafffb2732013-06-04 13:01:11 -04002505 return ext4_meta_trans_blocks(inode,
2506 MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp);
Mingming Cao525f4ed2008-08-19 22:15:58 -04002507}
Mingming Cao61628a32008-07-11 19:27:31 -04002508
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002509/*
Jan Kara4e7ea812013-06-04 13:17:40 -04002510 * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages
2511 * and underlying extent to map
2512 *
2513 * @mpd - where to look for pages
2514 *
2515 * Walk dirty pages in the mapping. If they are fully mapped, submit them for
2516 * IO immediately. When we find a page which isn't mapped we start accumulating
2517 * extent of buffers underlying these pages that needs mapping (formed by
2518 * either delayed or unwritten buffers). We also lock the pages containing
2519 * these buffers. The extent found is returned in @mpd structure (starting at
2520 * mpd->lblk with length mpd->len blocks).
2521 *
2522 * Note that this function can attach bios to one io_end structure which are
2523 * neither logically nor physically contiguous. Although it may seem as an
2524 * unnecessary complication, it is actually inevitable in blocksize < pagesize
2525 * case as we need to track IO to all buffers underlying a page in one io_end.
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002526 */
Jan Kara4e7ea812013-06-04 13:17:40 -04002527static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd)
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002528{
Jan Kara4e7ea812013-06-04 13:17:40 -04002529 struct address_space *mapping = mpd->inode->i_mapping;
2530 struct pagevec pvec;
2531 unsigned int nr_pages;
Ming Leiaeac5892013-10-17 18:56:16 -04002532 long left = mpd->wbc->nr_to_write;
Jan Kara4e7ea812013-06-04 13:17:40 -04002533 pgoff_t index = mpd->first_page;
2534 pgoff_t end = mpd->last_page;
Matthew Wilcox10bbd232017-12-05 17:30:38 -05002535 xa_mark_t tag;
Jan Kara4e7ea812013-06-04 13:17:40 -04002536 int i, err = 0;
2537 int blkbits = mpd->inode->i_blkbits;
2538 ext4_lblk_t lblk;
2539 struct buffer_head *head;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002540
Jan Kara4e7ea812013-06-04 13:17:40 -04002541 if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages)
Eric Sandeen5b41d922010-10-27 21:30:13 -04002542 tag = PAGECACHE_TAG_TOWRITE;
2543 else
2544 tag = PAGECACHE_TAG_DIRTY;
2545
Mel Gorman86679822017-11-15 17:37:52 -08002546 pagevec_init(&pvec);
Jan Kara4e7ea812013-06-04 13:17:40 -04002547 mpd->map.m_len = 0;
2548 mpd->next_page = index;
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002549 while (index <= end) {
Jan Karadc7f3e82017-11-15 17:34:44 -08002550 nr_pages = pagevec_lookup_range_tag(&pvec, mapping, &index, end,
Jan Kara67fd7072017-11-15 17:35:19 -08002551 tag);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002552 if (nr_pages == 0)
Jan Kara4e7ea812013-06-04 13:17:40 -04002553 goto out;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002554
2555 for (i = 0; i < nr_pages; i++) {
2556 struct page *page = pvec.pages[i];
2557
2558 /*
Ming Leiaeac5892013-10-17 18:56:16 -04002559 * Accumulated enough dirty pages? This doesn't apply
2560 * to WB_SYNC_ALL mode. For integrity sync we have to
2561 * keep going because someone may be concurrently
2562 * dirtying pages, and we might have synced a lot of
2563 * newly appeared dirty pages, but have not synced all
2564 * of the old dirty pages.
2565 */
2566 if (mpd->wbc->sync_mode == WB_SYNC_NONE && left <= 0)
2567 goto out;
2568
Jan Kara4e7ea812013-06-04 13:17:40 -04002569 /* If we can't merge this page, we are done. */
2570 if (mpd->map.m_len > 0 && mpd->next_page != page->index)
2571 goto out;
Theodore Ts'o78aaced2011-02-26 14:09:14 -05002572
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002573 lock_page(page);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002574 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002575 * If the page is no longer dirty, or its mapping no
2576 * longer corresponds to inode we are writing (which
2577 * means it has been truncated or invalidated), or the
2578 * page is already under writeback and we are not doing
2579 * a data integrity writeback, skip the page
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002580 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002581 if (!PageDirty(page) ||
2582 (PageWriteback(page) &&
Jan Kara4e7ea812013-06-04 13:17:40 -04002583 (mpd->wbc->sync_mode == WB_SYNC_NONE)) ||
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002584 unlikely(page->mapping != mapping)) {
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002585 unlock_page(page);
2586 continue;
2587 }
2588
Darrick J. Wong7cb1a532011-05-18 13:53:20 -04002589 wait_on_page_writeback(page);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002590 BUG_ON(PageWriteback(page));
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002591
Jan Kara4e7ea812013-06-04 13:17:40 -04002592 if (mpd->map.m_len == 0)
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002593 mpd->first_page = page->index;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002594 mpd->next_page = page->index + 1;
Jan Karaf8bec372013-01-28 12:55:08 -05002595 /* Add all dirty buffers to mpd */
Jan Kara4e7ea812013-06-04 13:17:40 -04002596 lblk = ((ext4_lblk_t)page->index) <<
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002597 (PAGE_SHIFT - blkbits);
Jan Karaf8bec372013-01-28 12:55:08 -05002598 head = page_buffers(page);
Jan Kara5f1132b2013-08-17 10:02:33 -04002599 err = mpage_process_page_bufs(mpd, head, head, lblk);
2600 if (err <= 0)
Jan Kara4e7ea812013-06-04 13:17:40 -04002601 goto out;
Jan Kara5f1132b2013-08-17 10:02:33 -04002602 err = 0;
Ming Leiaeac5892013-10-17 18:56:16 -04002603 left--;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002604 }
2605 pagevec_release(&pvec);
2606 cond_resched();
2607 }
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002608 return 0;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002609out:
2610 pagevec_release(&pvec);
Jan Kara4e7ea812013-06-04 13:17:40 -04002611 return err;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002612}
2613
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002614static int ext4_writepages(struct address_space *mapping,
2615 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002616{
Jan Kara4e7ea812013-06-04 13:17:40 -04002617 pgoff_t writeback_index = 0;
2618 long nr_to_write = wbc->nr_to_write;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002619 int range_whole = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002620 int cycled = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002621 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002622 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002623 struct inode *inode = mapping->host;
Jan Kara6b523df2013-06-04 13:21:11 -04002624 int needed_blocks, rsv_blocks = 0, ret = 0;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002625 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Jan Kara4e7ea812013-06-04 13:17:40 -04002626 bool done;
Shaohua Li1bce63d12011-10-18 10:55:51 -04002627 struct blk_plug plug;
Theodore Ts'ocb530542013-07-01 08:12:40 -04002628 bool give_up_on_write = false;
Mingming Cao61628a32008-07-11 19:27:31 -04002629
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002630 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
2631 return -EIO;
2632
Daeho Jeongc8585c62016-04-25 23:22:35 -04002633 percpu_down_read(&sbi->s_journal_flag_rwsem);
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002634 trace_ext4_writepages(inode, wbc);
Theodore Ts'oba80b102009-01-03 20:03:21 -05002635
Mingming Cao61628a32008-07-11 19:27:31 -04002636 /*
2637 * No pages to write? This is mainly a kludge to avoid starting
2638 * a transaction for special inodes like journal inode on last iput()
2639 * because that could violate lock ordering on umount
2640 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002641 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Ming Leibbf023c72013-10-30 07:27:16 -04002642 goto out_writepages;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002643
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002644 if (ext4_should_journal_data(inode)) {
Goldwyn Rodrigues043d20d2018-03-26 01:32:50 -04002645 ret = generic_writepages(mapping, wbc);
Ming Leibbf023c72013-10-30 07:27:16 -04002646 goto out_writepages;
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002647 }
2648
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002649 /*
2650 * If the filesystem has aborted, it is read-only, so return
2651 * right away instead of dumping stack traces later on that
2652 * will obscure the real source of the problem. We test
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002653 * EXT4_MF_FS_ABORTED instead of sb->s_flag's SB_RDONLY because
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002654 * the latter could be true if the filesystem is mounted
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002655 * read-only, and in that case, ext4_writepages should
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002656 * *never* be called, so if that ever happens, we would want
2657 * the stack trace.
2658 */
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002659 if (unlikely(ext4_forced_shutdown(EXT4_SB(mapping->host->i_sb)) ||
2660 sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) {
Ming Leibbf023c72013-10-30 07:27:16 -04002661 ret = -EROFS;
2662 goto out_writepages;
2663 }
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002664
Jan Kara4e7ea812013-06-04 13:17:40 -04002665 /*
2666 * If we have inline data and arrive here, it means that
2667 * we will soon create the block for the 1st page, so
2668 * we'd better clear the inline data here.
2669 */
2670 if (ext4_has_inline_data(inode)) {
2671 /* Just inode will be modified... */
2672 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
2673 if (IS_ERR(handle)) {
2674 ret = PTR_ERR(handle);
2675 goto out_writepages;
2676 }
2677 BUG_ON(ext4_test_inode_state(inode,
2678 EXT4_STATE_MAY_INLINE_DATA));
2679 ext4_destroy_inline_data(handle, inode);
2680 ext4_journal_stop(handle);
2681 }
2682
yangerkun4e343232019-08-11 16:27:41 -04002683 if (ext4_should_dioread_nolock(inode)) {
2684 /*
2685 * We may need to convert up to one extent per block in
2686 * the page and we may dirty the inode.
2687 */
2688 rsv_blocks = 1 + ext4_chunk_trans_blocks(inode,
2689 PAGE_SIZE >> inode->i_blkbits);
2690 }
2691
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002692 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2693 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002694
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002695 if (wbc->range_cyclic) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002696 writeback_index = mapping->writeback_index;
2697 if (writeback_index)
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002698 cycled = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002699 mpd.first_page = writeback_index;
2700 mpd.last_page = -1;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002701 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002702 mpd.first_page = wbc->range_start >> PAGE_SHIFT;
2703 mpd.last_page = wbc->range_end >> PAGE_SHIFT;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002704 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002705
Jan Kara4e7ea812013-06-04 13:17:40 -04002706 mpd.inode = inode;
2707 mpd.wbc = wbc;
2708 ext4_io_submit_init(&mpd.io_submit, wbc);
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002709retry:
Wu Fengguang6e6938b2010-06-06 10:38:15 -06002710 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Jan Kara4e7ea812013-06-04 13:17:40 -04002711 tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page);
2712 done = false;
Shaohua Li1bce63d12011-10-18 10:55:51 -04002713 blk_start_plug(&plug);
Jan Karadddbd6a2017-04-30 18:29:10 -04002714
2715 /*
2716 * First writeback pages that don't need mapping - we can avoid
2717 * starting a transaction unnecessarily and also avoid being blocked
2718 * in the block layer on device congestion while having transaction
2719 * started.
2720 */
2721 mpd.do_map = 0;
2722 mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
2723 if (!mpd.io_submit.io_end) {
2724 ret = -ENOMEM;
2725 goto unplug;
2726 }
2727 ret = mpage_prepare_extent_to_map(&mpd);
Xiaoguang Wanga297b2f2019-02-10 23:53:21 -05002728 /* Unlock pages we didn't use */
2729 mpage_release_unused_pages(&mpd, false);
Jan Karadddbd6a2017-04-30 18:29:10 -04002730 /* Submit prepared bio */
2731 ext4_io_submit(&mpd.io_submit);
2732 ext4_put_io_end_defer(mpd.io_submit.io_end);
2733 mpd.io_submit.io_end = NULL;
Jan Karadddbd6a2017-04-30 18:29:10 -04002734 if (ret < 0)
2735 goto unplug;
2736
Jan Kara4e7ea812013-06-04 13:17:40 -04002737 while (!done && mpd.first_page <= mpd.last_page) {
2738 /* For each extent of pages we use new io_end */
2739 mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
2740 if (!mpd.io_submit.io_end) {
2741 ret = -ENOMEM;
2742 break;
2743 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002744
2745 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002746 * We have two constraints: We find one extent to map and we
2747 * must always write out whole page (makes a difference when
2748 * blocksize < pagesize) so that we don't block on IO when we
2749 * try to write out the rest of the page. Journalled mode is
2750 * not supported by delalloc.
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002751 */
2752 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04002753 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002754
Jan Kara4e7ea812013-06-04 13:17:40 -04002755 /* start a new transaction */
Jan Kara6b523df2013-06-04 13:21:11 -04002756 handle = ext4_journal_start_with_reserve(inode,
2757 EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks);
Mingming Cao61628a32008-07-11 19:27:31 -04002758 if (IS_ERR(handle)) {
2759 ret = PTR_ERR(handle);
Theodore Ts'o16939182009-09-26 17:43:59 -04002760 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
Curt Wohlgemuthfbe845d2010-05-16 13:00:00 -04002761 "%ld pages, ino %lu; err %d", __func__,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002762 wbc->nr_to_write, inode->i_ino, ret);
Jan Kara4e7ea812013-06-04 13:17:40 -04002763 /* Release allocated io_end */
2764 ext4_put_io_end(mpd.io_submit.io_end);
Jan Karadddbd6a2017-04-30 18:29:10 -04002765 mpd.io_submit.io_end = NULL;
Jan Kara4e7ea812013-06-04 13:17:40 -04002766 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002767 }
Jan Karadddbd6a2017-04-30 18:29:10 -04002768 mpd.do_map = 1;
Theodore Ts'of63e60052009-02-23 16:42:39 -05002769
Jan Kara4e7ea812013-06-04 13:17:40 -04002770 trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc);
2771 ret = mpage_prepare_extent_to_map(&mpd);
2772 if (!ret) {
2773 if (mpd.map.m_len)
Theodore Ts'ocb530542013-07-01 08:12:40 -04002774 ret = mpage_map_and_submit_extent(handle, &mpd,
2775 &give_up_on_write);
Jan Kara4e7ea812013-06-04 13:17:40 -04002776 else {
2777 /*
2778 * We scanned the whole range (or exhausted
2779 * nr_to_write), submitted what was mapped and
2780 * didn't find anything needing mapping. We are
2781 * done.
2782 */
2783 done = true;
2784 }
Theodore Ts'of63e60052009-02-23 16:42:39 -05002785 }
Jan Kara646caa92016-07-04 10:14:01 -04002786 /*
2787 * Caution: If the handle is synchronous,
2788 * ext4_journal_stop() can wait for transaction commit
2789 * to finish which may depend on writeback of pages to
2790 * complete or on page lock to be released. In that
2791 * case, we have to wait until after after we have
2792 * submitted all the IO, released page locks we hold,
2793 * and dropped io_end reference (for extent conversion
2794 * to be able to complete) before stopping the handle.
2795 */
2796 if (!ext4_handle_valid(handle) || handle->h_sync == 0) {
2797 ext4_journal_stop(handle);
2798 handle = NULL;
Jan Karadddbd6a2017-04-30 18:29:10 -04002799 mpd.do_map = 0;
Jan Kara646caa92016-07-04 10:14:01 -04002800 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002801 /* Unlock pages we didn't use */
Theodore Ts'ocb530542013-07-01 08:12:40 -04002802 mpage_release_unused_pages(&mpd, give_up_on_write);
Xiaoguang Wanga297b2f2019-02-10 23:53:21 -05002803 /* Submit prepared bio */
2804 ext4_io_submit(&mpd.io_submit);
2805
Jan Kara646caa92016-07-04 10:14:01 -04002806 /*
2807 * Drop our io_end reference we got from init. We have
2808 * to be careful and use deferred io_end finishing if
2809 * we are still holding the transaction as we can
2810 * release the last reference to io_end which may end
2811 * up doing unwritten extent conversion.
2812 */
2813 if (handle) {
2814 ext4_put_io_end_defer(mpd.io_submit.io_end);
2815 ext4_journal_stop(handle);
2816 } else
2817 ext4_put_io_end(mpd.io_submit.io_end);
Jan Karadddbd6a2017-04-30 18:29:10 -04002818 mpd.io_submit.io_end = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002819
Jan Kara4e7ea812013-06-04 13:17:40 -04002820 if (ret == -ENOSPC && sbi->s_journal) {
2821 /*
2822 * Commit the transaction which would
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002823 * free blocks released in the transaction
2824 * and try again
2825 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002826 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002827 ret = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002828 continue;
2829 }
2830 /* Fatal error - ENOMEM, EIO... */
2831 if (ret)
Mingming Cao61628a32008-07-11 19:27:31 -04002832 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002833 }
Jan Karadddbd6a2017-04-30 18:29:10 -04002834unplug:
Shaohua Li1bce63d12011-10-18 10:55:51 -04002835 blk_finish_plug(&plug);
Jan Kara9c12a832013-09-16 08:24:26 -04002836 if (!ret && !cycled && wbc->nr_to_write > 0) {
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002837 cycled = 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04002838 mpd.last_page = writeback_index - 1;
2839 mpd.first_page = 0;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002840 goto retry;
2841 }
Mingming Cao61628a32008-07-11 19:27:31 -04002842
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002843 /* Update index */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002844 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2845 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002846 * Set the writeback_index so that range_cyclic
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002847 * mode will write it back later
2848 */
Jan Kara4e7ea812013-06-04 13:17:40 -04002849 mapping->writeback_index = mpd.first_page;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002850
Mingming Cao61628a32008-07-11 19:27:31 -04002851out_writepages:
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002852 trace_ext4_writepages_result(inode, wbc, ret,
2853 nr_to_write - wbc->nr_to_write);
Daeho Jeongc8585c62016-04-25 23:22:35 -04002854 percpu_up_read(&sbi->s_journal_flag_rwsem);
Mingming Cao61628a32008-07-11 19:27:31 -04002855 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002856}
2857
Dan Williams5f0663b2017-12-21 12:25:11 -08002858static int ext4_dax_writepages(struct address_space *mapping,
2859 struct writeback_control *wbc)
2860{
2861 int ret;
2862 long nr_to_write = wbc->nr_to_write;
2863 struct inode *inode = mapping->host;
2864 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
2865
2866 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
2867 return -EIO;
2868
2869 percpu_down_read(&sbi->s_journal_flag_rwsem);
2870 trace_ext4_writepages(inode, wbc);
2871
2872 ret = dax_writeback_mapping_range(mapping, inode->i_sb->s_bdev, wbc);
2873 trace_ext4_writepages_result(inode, wbc, ret,
2874 nr_to_write - wbc->nr_to_write);
2875 percpu_up_read(&sbi->s_journal_flag_rwsem);
2876 return ret;
2877}
2878
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002879static int ext4_nonda_switch(struct super_block *sb)
2880{
Eric Whitney5c1ff332013-04-09 09:27:31 -04002881 s64 free_clusters, dirty_clusters;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002882 struct ext4_sb_info *sbi = EXT4_SB(sb);
2883
2884 /*
2885 * switch to non delalloc mode if we are running low
2886 * on free block. The free block accounting via percpu
Eric Dumazet179f7eb2009-01-06 14:41:04 -08002887 * counters can get slightly wrong with percpu_counter_batch getting
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002888 * accumulated on each CPU without updating global counters
2889 * Delalloc need an accurate free block accounting. So switch
2890 * to non delalloc when we are near to error range.
2891 */
Eric Whitney5c1ff332013-04-09 09:27:31 -04002892 free_clusters =
2893 percpu_counter_read_positive(&sbi->s_freeclusters_counter);
2894 dirty_clusters =
2895 percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
Theodore Ts'o00d4e732012-09-19 22:42:36 -04002896 /*
2897 * Start pushing delalloc when 1/2 of free blocks are dirty.
2898 */
Eric Whitney5c1ff332013-04-09 09:27:31 -04002899 if (dirty_clusters && (free_clusters < 2 * dirty_clusters))
Miao Xie10ee27a2013-01-10 13:47:57 +08002900 try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE);
Theodore Ts'o00d4e732012-09-19 22:42:36 -04002901
Eric Whitney5c1ff332013-04-09 09:27:31 -04002902 if (2 * free_clusters < 3 * dirty_clusters ||
2903 free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) {
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002904 /*
Eric Sandeenc8afb442009-12-23 07:58:12 -05002905 * free block count is less than 150% of dirty blocks
2906 * or free blocks is less than watermark
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002907 */
2908 return 1;
2909 }
2910 return 0;
2911}
2912
Eric Sandeen0ff89472014-10-11 19:51:17 -04002913/* We always reserve for an inode update; the superblock could be there too */
2914static int ext4_da_write_credits(struct inode *inode, loff_t pos, unsigned len)
2915{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002916 if (likely(ext4_has_feature_large_file(inode->i_sb)))
Eric Sandeen0ff89472014-10-11 19:51:17 -04002917 return 1;
2918
2919 if (pos + len <= 0x7fffffffULL)
2920 return 1;
2921
2922 /* We might need to update the superblock to set LARGE_FILE */
2923 return 2;
2924}
2925
Alex Tomas64769242008-07-11 19:27:31 -04002926static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002927 loff_t pos, unsigned len, unsigned flags,
2928 struct page **pagep, void **fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04002929{
Eric Sandeen72b8ab92010-05-16 11:00:00 -04002930 int ret, retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002931 struct page *page;
2932 pgoff_t index;
Alex Tomas64769242008-07-11 19:27:31 -04002933 struct inode *inode = mapping->host;
2934 handle_t *handle;
2935
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05002936 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
2937 return -EIO;
2938
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002939 index = pos >> PAGE_SHIFT;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002940
Eric Biggersc93d8f82019-07-22 09:26:24 -07002941 if (ext4_nonda_switch(inode->i_sb) || S_ISLNK(inode->i_mode) ||
2942 ext4_verity_in_progress(inode)) {
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002943 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
2944 return ext4_write_begin(file, mapping, pos,
2945 len, flags, pagep, fsdata);
2946 }
2947 *fsdata = (void *)0;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002948 trace_ext4_da_write_begin(inode, pos, len, flags);
Tao Ma9c3569b2012-12-10 14:05:57 -05002949
2950 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2951 ret = ext4_da_write_inline_data_begin(mapping, inode,
2952 pos, len, flags,
2953 pagep, fsdata);
2954 if (ret < 0)
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002955 return ret;
2956 if (ret == 1)
2957 return 0;
Tao Ma9c3569b2012-12-10 14:05:57 -05002958 }
2959
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002960 /*
2961 * grab_cache_page_write_begin() can take a long time if the
2962 * system is thrashing due to memory pressure, or if the page
2963 * is being written back. So grab it first before we start
2964 * the transaction handle. This also allows us to allocate
2965 * the page (if needed) without using GFP_NOFS.
2966 */
2967retry_grab:
2968 page = grab_cache_page_write_begin(mapping, index, flags);
2969 if (!page)
2970 return -ENOMEM;
2971 unlock_page(page);
2972
Alex Tomas64769242008-07-11 19:27:31 -04002973 /*
2974 * With delayed allocation, we don't log the i_disksize update
2975 * if there is delayed block allocation. But we still need
2976 * to journalling the i_disksize update if writes to the end
2977 * of file which has an already mapped buffer.
2978 */
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002979retry_journal:
Eric Sandeen0ff89472014-10-11 19:51:17 -04002980 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
2981 ext4_da_write_credits(inode, pos, len));
Alex Tomas64769242008-07-11 19:27:31 -04002982 if (IS_ERR(handle)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002983 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002984 return PTR_ERR(handle);
Alex Tomas64769242008-07-11 19:27:31 -04002985 }
2986
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002987 lock_page(page);
2988 if (page->mapping != mapping) {
2989 /* The page got truncated from under us */
2990 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002991 put_page(page);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002992 ext4_journal_stop(handle);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002993 goto retry_grab;
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002994 }
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002995 /* In case writeback began while the page was unlocked */
Dmitry Monakhov7afe5aa2013-08-28 14:30:47 -04002996 wait_for_stable_page(page);
Alex Tomas64769242008-07-11 19:27:31 -04002997
Chandan Rajendra643fa962018-12-12 15:20:12 +05302998#ifdef CONFIG_FS_ENCRYPTION
Michael Halcrow2058f832015-04-12 00:55:10 -04002999 ret = ext4_block_write_begin(page, pos, len,
3000 ext4_da_get_block_prep);
3001#else
Christoph Hellwig6e1db882010-06-04 11:29:57 +02003002 ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
Michael Halcrow2058f832015-04-12 00:55:10 -04003003#endif
Alex Tomas64769242008-07-11 19:27:31 -04003004 if (ret < 0) {
3005 unlock_page(page);
3006 ext4_journal_stop(handle);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04003007 /*
3008 * block_write_begin may have instantiated a few blocks
3009 * outside i_size. Trim these off again. Don't need
3010 * i_size_read because we hold i_mutex.
3011 */
3012 if (pos + len > inode->i_size)
Jan Karab9a42072009-12-08 21:24:33 -05003013 ext4_truncate_failed_write(inode);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05003014
3015 if (ret == -ENOSPC &&
3016 ext4_should_retry_alloc(inode->i_sb, &retries))
3017 goto retry_journal;
3018
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003019 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05003020 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04003021 }
3022
Theodore Ts'o47564bf2013-02-09 09:24:14 -05003023 *pagep = page;
Alex Tomas64769242008-07-11 19:27:31 -04003024 return ret;
3025}
3026
Mingming Cao632eaea2008-07-11 19:27:31 -04003027/*
3028 * Check if we should update i_disksize
3029 * when write to the end of file but not require block allocation
3030 */
3031static int ext4_da_should_update_i_disksize(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003032 unsigned long offset)
Mingming Cao632eaea2008-07-11 19:27:31 -04003033{
3034 struct buffer_head *bh;
3035 struct inode *inode = page->mapping->host;
3036 unsigned int idx;
3037 int i;
3038
3039 bh = page_buffers(page);
3040 idx = offset >> inode->i_blkbits;
3041
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003042 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04003043 bh = bh->b_this_page;
3044
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04003045 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
Mingming Cao632eaea2008-07-11 19:27:31 -04003046 return 0;
3047 return 1;
3048}
3049
Alex Tomas64769242008-07-11 19:27:31 -04003050static int ext4_da_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003051 struct address_space *mapping,
3052 loff_t pos, unsigned len, unsigned copied,
3053 struct page *page, void *fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04003054{
3055 struct inode *inode = mapping->host;
3056 int ret = 0, ret2;
3057 handle_t *handle = ext4_journal_current_handle();
3058 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04003059 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003060 int write_mode = (int)(unsigned long)fsdata;
3061
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003062 if (write_mode == FALL_BACK_TO_NONDELALLOC)
3063 return ext4_write_end(file, mapping, pos,
3064 len, copied, page, fsdata);
Mingming Cao632eaea2008-07-11 19:27:31 -04003065
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003066 trace_ext4_da_write_end(inode, pos, len, copied);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003067 start = pos & (PAGE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003068 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04003069
3070 /*
3071 * generic_write_end() will run mark_inode_dirty() if i_size
3072 * changes. So let's piggyback the i_disksize mark_inode_dirty
3073 * into that.
3074 */
Alex Tomas64769242008-07-11 19:27:31 -04003075 new_i_size = pos + copied;
Andrea Arcangeliea51d132011-12-13 21:41:15 -05003076 if (copied && new_i_size > EXT4_I(inode)->i_disksize) {
Tao Ma9c3569b2012-12-10 14:05:57 -05003077 if (ext4_has_inline_data(inode) ||
3078 ext4_da_should_update_i_disksize(page, end)) {
Dmitry Monakhovee124d22014-08-30 23:34:06 -04003079 ext4_update_i_disksize(inode, new_i_size);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003080 /* We need to mark inode dirty even if
3081 * new_i_size is less that inode->i_size
3082 * bu greater than i_disksize.(hint delalloc)
3083 */
3084 ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04003085 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003086 }
Tao Ma9c3569b2012-12-10 14:05:57 -05003087
3088 if (write_mode != CONVERT_INLINE_DATA &&
3089 ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
3090 ext4_has_inline_data(inode))
3091 ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied,
3092 page);
3093 else
3094 ret2 = generic_write_end(file, mapping, pos, len, copied,
Alex Tomas64769242008-07-11 19:27:31 -04003095 page, fsdata);
Tao Ma9c3569b2012-12-10 14:05:57 -05003096
Alex Tomas64769242008-07-11 19:27:31 -04003097 copied = ret2;
3098 if (ret2 < 0)
3099 ret = ret2;
3100 ret2 = ext4_journal_stop(handle);
3101 if (!ret)
3102 ret = ret2;
3103
3104 return ret ? ret : copied;
3105}
3106
Theodore Ts'occd25062009-02-26 01:04:07 -05003107/*
3108 * Force all delayed allocation blocks to be allocated for a given inode.
3109 */
3110int ext4_alloc_da_blocks(struct inode *inode)
3111{
Theodore Ts'ofb40ba02009-09-16 19:30:40 -04003112 trace_ext4_alloc_da_blocks(inode);
3113
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04003114 if (!EXT4_I(inode)->i_reserved_data_blocks)
Theodore Ts'occd25062009-02-26 01:04:07 -05003115 return 0;
3116
3117 /*
3118 * We do something simple for now. The filemap_flush() will
3119 * also start triggering a write of the data blocks, which is
3120 * not strictly speaking necessary (and for users of
3121 * laptop_mode, not even desirable). However, to do otherwise
3122 * would require replicating code paths in:
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003123 *
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003124 * ext4_writepages() ->
Theodore Ts'occd25062009-02-26 01:04:07 -05003125 * write_cache_pages() ---> (via passed in callback function)
3126 * __mpage_da_writepage() -->
3127 * mpage_add_bh_to_extent()
3128 * mpage_da_map_blocks()
3129 *
3130 * The problem is that write_cache_pages(), located in
3131 * mm/page-writeback.c, marks pages clean in preparation for
3132 * doing I/O, which is not desirable if we're not planning on
3133 * doing I/O at all.
3134 *
3135 * We could call write_cache_pages(), and then redirty all of
Wu Fengguang380cf092010-11-11 19:23:29 +08003136 * the pages by calling redirty_page_for_writepage() but that
Theodore Ts'occd25062009-02-26 01:04:07 -05003137 * would be ugly in the extreme. So instead we would need to
3138 * replicate parts of the code in the above functions,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003139 * simplifying them because we wouldn't actually intend to
Theodore Ts'occd25062009-02-26 01:04:07 -05003140 * write out the pages, but rather only collect contiguous
3141 * logical block extents, call the multi-block allocator, and
3142 * then update the buffer heads with the block allocations.
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003143 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003144 * For now, though, we'll cheat by calling filemap_flush(),
3145 * which will map the blocks, and start the I/O, but not
3146 * actually wait for the I/O to complete.
3147 */
3148 return filemap_flush(inode->i_mapping);
3149}
Alex Tomas64769242008-07-11 19:27:31 -04003150
3151/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003152 * bmap() is special. It gets used by applications such as lilo and by
3153 * the swapper to find the on-disk block of a specific piece of data.
3154 *
3155 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07003156 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003157 * filesystem and enables swap, then they may get a nasty shock when the
3158 * data getting swapped to that swapfile suddenly gets overwritten by
3159 * the original zero's written out previously to the journal and
3160 * awaiting writeback in the kernel's buffer cache.
3161 *
3162 * So, if we see any bmap calls here on a modified, data-journaled file,
3163 * take extra steps to flush any blocks which might be in the cache.
3164 */
Mingming Cao617ba132006-10-11 01:20:53 -07003165static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003166{
3167 struct inode *inode = mapping->host;
3168 journal_t *journal;
3169 int err;
3170
Tao Ma46c7f252012-12-10 14:04:52 -05003171 /*
3172 * We can get here for an inline file via the FIBMAP ioctl
3173 */
3174 if (ext4_has_inline_data(inode))
3175 return 0;
3176
Alex Tomas64769242008-07-11 19:27:31 -04003177 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
3178 test_opt(inode->i_sb, DELALLOC)) {
3179 /*
3180 * With delalloc we want to sync the file
3181 * so that we can make sure we allocate
3182 * blocks for file
3183 */
3184 filemap_write_and_wait(mapping);
3185 }
3186
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003187 if (EXT4_JOURNAL(inode) &&
3188 ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003189 /*
3190 * This is a REALLY heavyweight approach, but the use of
3191 * bmap on dirty files is expected to be extremely rare:
3192 * only if we run lilo or swapon on a freshly made file
3193 * do we expect this to happen.
3194 *
3195 * (bmap requires CAP_SYS_RAWIO so this does not
3196 * represent an unprivileged user DOS attack --- we'd be
3197 * in trouble if mortal users could trigger this path at
3198 * will.)
3199 *
Mingming Cao617ba132006-10-11 01:20:53 -07003200 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003201 * regular files. If somebody wants to bmap a directory
3202 * or symlink and gets confused because the buffer
3203 * hasn't yet been flushed to disk, they deserve
3204 * everything they get.
3205 */
3206
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003207 ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
Mingming Cao617ba132006-10-11 01:20:53 -07003208 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07003209 jbd2_journal_lock_updates(journal);
3210 err = jbd2_journal_flush(journal);
3211 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003212
3213 if (err)
3214 return 0;
3215 }
3216
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003217 return generic_block_bmap(mapping, block, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003218}
3219
Mingming Cao617ba132006-10-11 01:20:53 -07003220static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003221{
Tao Ma46c7f252012-12-10 14:04:52 -05003222 int ret = -EAGAIN;
3223 struct inode *inode = page->mapping->host;
3224
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003225 trace_ext4_readpage(page);
Tao Ma46c7f252012-12-10 14:04:52 -05003226
3227 if (ext4_has_inline_data(inode))
3228 ret = ext4_readpage_inline(inode, page);
3229
3230 if (ret == -EAGAIN)
Jens Axboeac22b462018-08-17 15:45:42 -07003231 return ext4_mpage_readpages(page->mapping, NULL, page, 1,
3232 false);
Tao Ma46c7f252012-12-10 14:04:52 -05003233
3234 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003235}
3236
3237static int
Mingming Cao617ba132006-10-11 01:20:53 -07003238ext4_readpages(struct file *file, struct address_space *mapping,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003239 struct list_head *pages, unsigned nr_pages)
3240{
Tao Ma46c7f252012-12-10 14:04:52 -05003241 struct inode *inode = mapping->host;
3242
3243 /* If the file has inline data, no need to do readpages. */
3244 if (ext4_has_inline_data(inode))
3245 return 0;
3246
Jens Axboeac22b462018-08-17 15:45:42 -07003247 return ext4_mpage_readpages(mapping, pages, NULL, nr_pages, true);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003248}
3249
Lukas Czernerd47992f2013-05-21 23:17:23 -04003250static void ext4_invalidatepage(struct page *page, unsigned int offset,
3251 unsigned int length)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003252{
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003253 trace_ext4_invalidatepage(page, offset, length);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003254
Jan Kara4520fb32012-12-25 13:28:54 -05003255 /* No journalling happens on data buffers when this function is used */
3256 WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page)));
3257
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003258 block_invalidatepage(page, offset, length);
Jan Kara4520fb32012-12-25 13:28:54 -05003259}
3260
Jan Kara53e87262012-12-25 13:29:52 -05003261static int __ext4_journalled_invalidatepage(struct page *page,
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003262 unsigned int offset,
3263 unsigned int length)
Jan Kara4520fb32012-12-25 13:28:54 -05003264{
3265 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
3266
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003267 trace_ext4_journalled_invalidatepage(page, offset, length);
Jan Kara4520fb32012-12-25 13:28:54 -05003268
Jiaying Zhang744692d2010-03-04 16:14:02 -05003269 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003270 * If it's a full truncate we just forget about the pending dirtying
3271 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003272 if (offset == 0 && length == PAGE_SIZE)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003273 ClearPageChecked(page);
3274
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003275 return jbd2_journal_invalidatepage(journal, page, offset, length);
Jan Kara53e87262012-12-25 13:29:52 -05003276}
3277
3278/* Wrapper for aops... */
3279static void ext4_journalled_invalidatepage(struct page *page,
Lukas Czernerd47992f2013-05-21 23:17:23 -04003280 unsigned int offset,
3281 unsigned int length)
Jan Kara53e87262012-12-25 13:29:52 -05003282{
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003283 WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003284}
3285
Mingming Cao617ba132006-10-11 01:20:53 -07003286static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003287{
Mingming Cao617ba132006-10-11 01:20:53 -07003288 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003289
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003290 trace_ext4_releasepage(page);
3291
Jan Karae1c36592013-03-10 22:19:00 -04003292 /* Page has dirty journalled data -> cannot release */
3293 if (PageChecked(page))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003294 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05003295 if (journal)
3296 return jbd2_journal_try_to_free_buffers(journal, page, wait);
3297 else
3298 return try_to_free_buffers(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003299}
3300
Jan Karab8a61762017-11-01 16:36:45 +01003301static bool ext4_inode_datasync_dirty(struct inode *inode)
3302{
3303 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
3304
3305 if (journal)
3306 return !jbd2_transaction_committed(journal,
3307 EXT4_I(inode)->i_datasync_tid);
3308 /* Any metadata buffers to write? */
3309 if (!list_empty(&inode->i_mapping->private_list))
3310 return true;
3311 return inode->i_state & I_DIRTY_DATASYNC;
3312}
3313
Matthew Bobrowskic8fdfe292019-11-05 22:59:56 +11003314static void ext4_set_iomap(struct inode *inode, struct iomap *iomap,
3315 struct ext4_map_blocks *map, loff_t offset,
3316 loff_t length)
Jan Kara364443c2016-11-20 17:36:06 -05003317{
Matthew Bobrowskic8fdfe292019-11-05 22:59:56 +11003318 u8 blkbits = inode->i_blkbits;
3319
3320 /*
3321 * Writes that span EOF might trigger an I/O size update on completion,
3322 * so consider them to be dirty for the purpose of O_DSYNC, even if
3323 * there is no other metadata changes being made or are pending.
3324 */
3325 iomap->flags = 0;
3326 if (ext4_inode_datasync_dirty(inode) ||
3327 offset + length > i_size_read(inode))
3328 iomap->flags |= IOMAP_F_DIRTY;
3329
3330 if (map->m_flags & EXT4_MAP_NEW)
3331 iomap->flags |= IOMAP_F_NEW;
3332
3333 iomap->bdev = inode->i_sb->s_bdev;
3334 iomap->dax_dev = EXT4_SB(inode->i_sb)->s_daxdev;
3335 iomap->offset = (u64) map->m_lblk << blkbits;
3336 iomap->length = (u64) map->m_len << blkbits;
3337
3338 /*
3339 * Flags passed to ext4_map_blocks() for direct I/O writes can result
3340 * in m_flags having both EXT4_MAP_MAPPED and EXT4_MAP_UNWRITTEN bits
3341 * set. In order for any allocated unwritten extents to be converted
3342 * into written extents correctly within the ->end_io() handler, we
3343 * need to ensure that the iomap->type is set appropriately. Hence, the
3344 * reason why we need to check whether the EXT4_MAP_UNWRITTEN bit has
3345 * been set first.
3346 */
3347 if (map->m_flags & EXT4_MAP_UNWRITTEN) {
3348 iomap->type = IOMAP_UNWRITTEN;
3349 iomap->addr = (u64) map->m_pblk << blkbits;
3350 } else if (map->m_flags & EXT4_MAP_MAPPED) {
3351 iomap->type = IOMAP_MAPPED;
3352 iomap->addr = (u64) map->m_pblk << blkbits;
3353 } else {
3354 iomap->type = IOMAP_HOLE;
3355 iomap->addr = IOMAP_NULL_ADDR;
3356 }
3357}
3358
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003359static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map,
3360 unsigned int flags)
3361{
3362 handle_t *handle;
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003363 u8 blkbits = inode->i_blkbits;
3364 int ret, dio_credits, m_flags = 0, retries = 0;
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003365
3366 /*
3367 * Trim the mapping request to the maximum value that we can map at
3368 * once for direct I/O.
3369 */
3370 if (map->m_len > DIO_MAX_BLOCKS)
3371 map->m_len = DIO_MAX_BLOCKS;
3372 dio_credits = ext4_chunk_trans_blocks(inode, map->m_len);
3373
3374retry:
3375 /*
3376 * Either we allocate blocks and then don't get an unwritten extent, so
3377 * in that case we have reserved enough credits. Or, the blocks are
3378 * already allocated and unwritten. In that case, the extent conversion
3379 * fits into the credits as well.
3380 */
3381 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits);
3382 if (IS_ERR(handle))
3383 return PTR_ERR(handle);
3384
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003385 /*
3386 * DAX and direct I/O are the only two operations that are currently
3387 * supported with IOMAP_WRITE.
3388 */
3389 WARN_ON(!IS_DAX(inode) && !(flags & IOMAP_DIRECT));
3390 if (IS_DAX(inode))
3391 m_flags = EXT4_GET_BLOCKS_CREATE_ZERO;
3392 /*
3393 * We use i_size instead of i_disksize here because delalloc writeback
3394 * can complete at any point during the I/O and subsequently push the
3395 * i_disksize out to i_size. This could be beyond where direct I/O is
3396 * happening and thus expose allocated blocks to direct I/O reads.
3397 */
3398 else if ((map->m_lblk * (1 << blkbits)) >= i_size_read(inode))
3399 m_flags = EXT4_GET_BLOCKS_CREATE;
3400 else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3401 m_flags = EXT4_GET_BLOCKS_IO_CREATE_EXT;
3402
3403 ret = ext4_map_blocks(handle, inode, map, m_flags);
3404
3405 /*
3406 * We cannot fill holes in indirect tree based inodes as that could
3407 * expose stale data in the case of a crash. Use the magic error code
3408 * to fallback to buffered I/O.
3409 */
3410 if (!m_flags && !ret)
3411 ret = -ENOTBLK;
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003412
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003413 ext4_journal_stop(handle);
3414 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
3415 goto retry;
3416
3417 return ret;
3418}
3419
3420
Jan Kara364443c2016-11-20 17:36:06 -05003421static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -07003422 unsigned flags, struct iomap *iomap, struct iomap *srcmap)
Jan Kara364443c2016-11-20 17:36:06 -05003423{
Jan Kara364443c2016-11-20 17:36:06 -05003424 int ret;
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003425 struct ext4_map_blocks map;
3426 u8 blkbits = inode->i_blkbits;
Jan Kara364443c2016-11-20 17:36:06 -05003427
Theodore Ts'obcd8e912018-09-01 12:45:04 -04003428 if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
3429 return -EINVAL;
Andreas Gruenbacher7046ae32017-10-01 17:57:54 -04003430
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003431 if (WARN_ON_ONCE(ext4_has_inline_data(inode)))
3432 return -ERANGE;
Jan Kara364443c2016-11-20 17:36:06 -05003433
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003434 /*
3435 * Calculate the first and last logical blocks respectively.
3436 */
3437 map.m_lblk = offset >> blkbits;
3438 map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits,
3439 EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1;
Jan Kara364443c2016-11-20 17:36:06 -05003440
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003441 if (flags & IOMAP_WRITE)
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003442 ret = ext4_iomap_alloc(inode, &map, flags);
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003443 else
Jan Kara776722e2016-11-20 18:09:11 -05003444 ret = ext4_map_blocks(NULL, inode, &map, 0);
Christoph Hellwig545052e2017-10-01 17:58:54 -04003445
Matthew Bobrowskif063db52019-11-05 23:00:14 +11003446 if (ret < 0)
3447 return ret;
Christoph Hellwig545052e2017-10-01 17:58:54 -04003448
Matthew Bobrowskic8fdfe292019-11-05 22:59:56 +11003449 ext4_set_iomap(inode, iomap, &map, offset, length);
Christoph Hellwig545052e2017-10-01 17:58:54 -04003450
Jan Kara364443c2016-11-20 17:36:06 -05003451 return 0;
3452}
3453
Jan Kara8cd115b2019-12-18 18:44:33 +01003454static int ext4_iomap_overwrite_begin(struct inode *inode, loff_t offset,
3455 loff_t length, unsigned flags, struct iomap *iomap,
3456 struct iomap *srcmap)
3457{
3458 int ret;
3459
3460 /*
3461 * Even for writes we don't need to allocate blocks, so just pretend
3462 * we are reading to save overhead of starting a transaction.
3463 */
3464 flags &= ~IOMAP_WRITE;
3465 ret = ext4_iomap_begin(inode, offset, length, flags, iomap, srcmap);
3466 WARN_ON_ONCE(iomap->type != IOMAP_MAPPED);
3467 return ret;
3468}
3469
Jan Kara776722e2016-11-20 18:09:11 -05003470static int ext4_iomap_end(struct inode *inode, loff_t offset, loff_t length,
3471 ssize_t written, unsigned flags, struct iomap *iomap)
3472{
Jan Kara776722e2016-11-20 18:09:11 -05003473 /*
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003474 * Check to see whether an error occurred while writing out the data to
3475 * the allocated blocks. If so, return the magic error code so that we
3476 * fallback to buffered I/O and attempt to complete the remainder of
3477 * the I/O. Any blocks that may have been allocated in preparation for
3478 * the direct I/O will be reused during buffered I/O.
Jan Kara776722e2016-11-20 18:09:11 -05003479 */
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003480 if (flags & (IOMAP_WRITE | IOMAP_DIRECT) && written == 0)
3481 return -ENOTBLK;
Jan Kara776722e2016-11-20 18:09:11 -05003482
Matthew Bobrowski569342d2019-11-05 23:01:51 +11003483 return 0;
Jan Kara776722e2016-11-20 18:09:11 -05003484}
3485
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -08003486const struct iomap_ops ext4_iomap_ops = {
Jan Kara364443c2016-11-20 17:36:06 -05003487 .iomap_begin = ext4_iomap_begin,
Jan Kara776722e2016-11-20 18:09:11 -05003488 .iomap_end = ext4_iomap_end,
Jan Kara364443c2016-11-20 17:36:06 -05003489};
3490
Jan Kara8cd115b2019-12-18 18:44:33 +01003491const struct iomap_ops ext4_iomap_overwrite_ops = {
3492 .iomap_begin = ext4_iomap_overwrite_begin,
3493 .iomap_end = ext4_iomap_end,
3494};
3495
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003496static bool ext4_iomap_is_delalloc(struct inode *inode,
3497 struct ext4_map_blocks *map)
Mingming Cao4c0425f2009-09-28 15:48:41 -04003498{
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003499 struct extent_status es;
3500 ext4_lblk_t offset = 0, end = map->m_lblk + map->m_len - 1;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003501
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003502 ext4_es_find_extent_range(inode, &ext4_es_is_delayed,
3503 map->m_lblk, end, &es);
Mingming4b70df12009-11-03 14:44:54 -05003504
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003505 if (!es.es_len || es.es_lblk > end)
3506 return false;
3507
3508 if (es.es_lblk > map->m_lblk) {
3509 map->m_len = es.es_lblk - map->m_lblk;
3510 return false;
3511 }
3512
3513 offset = map->m_lblk - es.es_lblk;
3514 map->m_len = es.es_len - offset;
3515
3516 return true;
3517}
3518
3519static int ext4_iomap_begin_report(struct inode *inode, loff_t offset,
3520 loff_t length, unsigned int flags,
3521 struct iomap *iomap, struct iomap *srcmap)
3522{
3523 int ret;
3524 bool delalloc = false;
3525 struct ext4_map_blocks map;
3526 u8 blkbits = inode->i_blkbits;
3527
3528 if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
3529 return -EINVAL;
3530
3531 if (ext4_has_inline_data(inode)) {
3532 ret = ext4_inline_data_iomap(inode, iomap);
3533 if (ret != -EAGAIN) {
3534 if (ret == 0 && offset >= iomap->length)
3535 ret = -ENOENT;
3536 return ret;
3537 }
3538 }
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003539
Jan Kara74c66bc2016-02-29 08:36:38 +11003540 /*
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003541 * Calculate the first and last logical block respectively.
Jan Kara74c66bc2016-02-29 08:36:38 +11003542 */
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003543 map.m_lblk = offset >> blkbits;
3544 map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits,
3545 EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1;
3546
3547 ret = ext4_map_blocks(NULL, inode, &map, 0);
3548 if (ret < 0)
3549 return ret;
3550 if (ret == 0)
3551 delalloc = ext4_iomap_is_delalloc(inode, &map);
3552
3553 ext4_set_iomap(inode, iomap, &map, offset, length);
3554 if (delalloc && iomap->type == IOMAP_HOLE)
3555 iomap->type = IOMAP_DELALLOC;
Christoph Hellwig187372a2016-02-08 14:40:51 +11003556
3557 return 0;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003558}
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003559
Matthew Bobrowski09edf4d2019-11-05 23:03:31 +11003560const struct iomap_ops ext4_iomap_report_ops = {
3561 .iomap_begin = ext4_iomap_begin_report,
3562};
Mingming Cao4c0425f2009-09-28 15:48:41 -04003563
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003564/*
Mingming Cao617ba132006-10-11 01:20:53 -07003565 * Pages can be marked dirty completely asynchronously from ext4's journalling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003566 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3567 * much here because ->set_page_dirty is called under VFS locks. The page is
3568 * not necessarily locked.
3569 *
3570 * We cannot just dirty the page and leave attached buffers clean, because the
3571 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3572 * or jbddirty because all the journalling code will explode.
3573 *
3574 * So what we do is to mark the page "pending dirty" and next time writepage
3575 * is called, propagate that into the buffers appropriately.
3576 */
Mingming Cao617ba132006-10-11 01:20:53 -07003577static int ext4_journalled_set_page_dirty(struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003578{
3579 SetPageChecked(page);
3580 return __set_page_dirty_nobuffers(page);
3581}
3582
Jan Kara6dcc6932016-12-01 11:46:40 -05003583static int ext4_set_page_dirty(struct page *page)
3584{
3585 WARN_ON_ONCE(!PageLocked(page) && !PageDirty(page));
3586 WARN_ON_ONCE(!page_has_buffers(page));
3587 return __set_page_dirty_buffers(page);
3588}
3589
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003590static const struct address_space_operations ext4_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003591 .readpage = ext4_readpage,
3592 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003593 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003594 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003595 .write_begin = ext4_write_begin,
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003596 .write_end = ext4_write_end,
Jan Kara6dcc6932016-12-01 11:46:40 -05003597 .set_page_dirty = ext4_set_page_dirty,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003598 .bmap = ext4_bmap,
3599 .invalidatepage = ext4_invalidatepage,
3600 .releasepage = ext4_releasepage,
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003601 .direct_IO = noop_direct_IO,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003602 .migratepage = buffer_migrate_page,
3603 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003604 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003605};
3606
Mingming Cao617ba132006-10-11 01:20:53 -07003607static const struct address_space_operations ext4_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003608 .readpage = ext4_readpage,
3609 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003610 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003611 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003612 .write_begin = ext4_write_begin,
3613 .write_end = ext4_journalled_write_end,
3614 .set_page_dirty = ext4_journalled_set_page_dirty,
3615 .bmap = ext4_bmap,
Jan Kara4520fb32012-12-25 13:28:54 -05003616 .invalidatepage = ext4_journalled_invalidatepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003617 .releasepage = ext4_releasepage,
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003618 .direct_IO = noop_direct_IO,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003619 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003620 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003621};
3622
Alex Tomas64769242008-07-11 19:27:31 -04003623static const struct address_space_operations ext4_da_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003624 .readpage = ext4_readpage,
3625 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003626 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003627 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003628 .write_begin = ext4_da_write_begin,
3629 .write_end = ext4_da_write_end,
Jan Kara6dcc6932016-12-01 11:46:40 -05003630 .set_page_dirty = ext4_set_page_dirty,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003631 .bmap = ext4_bmap,
Eric Whitney8fcc3a52019-08-22 23:22:14 -04003632 .invalidatepage = ext4_invalidatepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003633 .releasepage = ext4_releasepage,
Matthew Bobrowski378f32b2019-11-05 23:02:39 +11003634 .direct_IO = noop_direct_IO,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003635 .migratepage = buffer_migrate_page,
3636 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003637 .error_remove_page = generic_error_remove_page,
Alex Tomas64769242008-07-11 19:27:31 -04003638};
3639
Dan Williams5f0663b2017-12-21 12:25:11 -08003640static const struct address_space_operations ext4_dax_aops = {
3641 .writepages = ext4_dax_writepages,
3642 .direct_IO = noop_direct_IO,
3643 .set_page_dirty = noop_set_page_dirty,
Toshi Kani94dbb632018-09-15 21:23:41 -04003644 .bmap = ext4_bmap,
Dan Williams5f0663b2017-12-21 12:25:11 -08003645 .invalidatepage = noop_invalidatepage,
3646};
3647
Mingming Cao617ba132006-10-11 01:20:53 -07003648void ext4_set_aops(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003649{
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003650 switch (ext4_inode_journal_mode(inode)) {
3651 case EXT4_INODE_ORDERED_DATA_MODE:
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003652 case EXT4_INODE_WRITEBACK_DATA_MODE:
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003653 break;
3654 case EXT4_INODE_JOURNAL_DATA_MODE:
Mingming Cao617ba132006-10-11 01:20:53 -07003655 inode->i_mapping->a_ops = &ext4_journalled_aops;
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003656 return;
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003657 default:
3658 BUG();
3659 }
Dan Williams5f0663b2017-12-21 12:25:11 -08003660 if (IS_DAX(inode))
3661 inode->i_mapping->a_ops = &ext4_dax_aops;
3662 else if (test_opt(inode->i_sb, DELALLOC))
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003663 inode->i_mapping->a_ops = &ext4_da_aops;
3664 else
3665 inode->i_mapping->a_ops = &ext4_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003666}
3667
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003668static int __ext4_block_zero_page_range(handle_t *handle,
Lukas Czernerd863dc32013-05-27 23:32:35 -04003669 struct address_space *mapping, loff_t from, loff_t length)
3670{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003671 ext4_fsblk_t index = from >> PAGE_SHIFT;
3672 unsigned offset = from & (PAGE_SIZE-1);
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003673 unsigned blocksize, pos;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003674 ext4_lblk_t iblock;
3675 struct inode *inode = mapping->host;
3676 struct buffer_head *bh;
3677 struct page *page;
3678 int err = 0;
3679
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003680 page = find_or_create_page(mapping, from >> PAGE_SHIFT,
Michal Hockoc62d2552015-11-06 16:28:49 -08003681 mapping_gfp_constraint(mapping, ~__GFP_FS));
Lukas Czernerd863dc32013-05-27 23:32:35 -04003682 if (!page)
3683 return -ENOMEM;
3684
3685 blocksize = inode->i_sb->s_blocksize;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003686
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003687 iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003688
3689 if (!page_has_buffers(page))
3690 create_empty_buffers(page, blocksize, 0);
3691
3692 /* Find the buffer that contains "offset" */
3693 bh = page_buffers(page);
3694 pos = blocksize;
3695 while (offset >= pos) {
3696 bh = bh->b_this_page;
3697 iblock++;
3698 pos += blocksize;
3699 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003700 if (buffer_freed(bh)) {
3701 BUFFER_TRACE(bh, "freed: skip");
3702 goto unlock;
3703 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003704 if (!buffer_mapped(bh)) {
3705 BUFFER_TRACE(bh, "unmapped");
3706 ext4_get_block(inode, iblock, bh, 0);
3707 /* unmapped? It's a hole - nothing to do */
3708 if (!buffer_mapped(bh)) {
3709 BUFFER_TRACE(bh, "still unmapped");
3710 goto unlock;
3711 }
3712 }
3713
3714 /* Ok, it's mapped. Make sure it's up-to-date */
3715 if (PageUptodate(page))
3716 set_buffer_uptodate(bh);
3717
3718 if (!buffer_uptodate(bh)) {
3719 err = -EIO;
Mike Christiedfec8a12016-06-05 14:31:44 -05003720 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003721 wait_on_buffer(bh);
3722 /* Uhhuh. Read error. Complain and punt. */
3723 if (!buffer_uptodate(bh))
3724 goto unlock;
Chandan Rajendra592ddec2018-12-12 15:20:10 +05303725 if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode)) {
Michael Halcrowc9c74292015-04-12 00:56:10 -04003726 /* We expect the key to be set. */
Jaegeuk Kima7550b32016-07-10 14:01:03 -04003727 BUG_ON(!fscrypt_has_encryption_key(inode));
Eric Biggersaa8bc1a2019-05-20 09:29:47 -07003728 WARN_ON_ONCE(fscrypt_decrypt_pagecache_blocks(
Chandan Rajendraec39a362019-05-20 09:29:51 -07003729 page, blocksize, bh_offset(bh)));
Michael Halcrowc9c74292015-04-12 00:56:10 -04003730 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003731 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003732 if (ext4_should_journal_data(inode)) {
3733 BUFFER_TRACE(bh, "get write access");
3734 err = ext4_journal_get_write_access(handle, bh);
3735 if (err)
3736 goto unlock;
3737 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003738 zero_user(page, offset, length);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003739 BUFFER_TRACE(bh, "zeroed end of block");
3740
Lukas Czernerd863dc32013-05-27 23:32:35 -04003741 if (ext4_should_journal_data(inode)) {
3742 err = ext4_handle_dirty_metadata(handle, inode, bh);
Lukas Czerner0713ed02013-05-27 23:32:35 -04003743 } else {
jon ernst353eefd2013-07-01 08:12:39 -04003744 err = 0;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003745 mark_buffer_dirty(bh);
Jan Kara3957ef52016-04-24 00:56:05 -04003746 if (ext4_should_order_data(inode))
Ross Zwisler73131fb2019-06-20 17:26:26 -04003747 err = ext4_jbd2_inode_add_write(handle, inode, from,
3748 length);
Lukas Czerner0713ed02013-05-27 23:32:35 -04003749 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003750
3751unlock:
3752 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003753 put_page(page);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003754 return err;
3755}
3756
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003757/*
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003758 * ext4_block_zero_page_range() zeros out a mapping of length 'length'
3759 * starting from file offset 'from'. The range to be zero'd must
3760 * be contained with in one block. If the specified range exceeds
3761 * the end of the block it will be shortened to end of the block
3762 * that cooresponds to 'from'
3763 */
3764static int ext4_block_zero_page_range(handle_t *handle,
3765 struct address_space *mapping, loff_t from, loff_t length)
3766{
3767 struct inode *inode = mapping->host;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003768 unsigned offset = from & (PAGE_SIZE-1);
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003769 unsigned blocksize = inode->i_sb->s_blocksize;
3770 unsigned max = blocksize - (offset & (blocksize - 1));
3771
3772 /*
3773 * correct length if it does not fall between
3774 * 'from' and the end of the block
3775 */
3776 if (length > max || length < 0)
3777 length = max;
3778
Jan Kara47e69352016-11-20 18:08:05 -05003779 if (IS_DAX(inode)) {
3780 return iomap_zero_range(inode, from, length, NULL,
3781 &ext4_iomap_ops);
3782 }
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003783 return __ext4_block_zero_page_range(handle, mapping, from, length);
3784}
3785
3786/*
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003787 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
3788 * up to the end of the block which corresponds to `from'.
3789 * This required during truncate. We need to physically zero the tail end
3790 * of that block so it doesn't yield old data if the file is later grown.
3791 */
Stephen Hemmingerc1978552014-05-12 10:50:23 -04003792static int ext4_block_truncate_page(handle_t *handle,
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003793 struct address_space *mapping, loff_t from)
3794{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003795 unsigned offset = from & (PAGE_SIZE-1);
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003796 unsigned length;
3797 unsigned blocksize;
3798 struct inode *inode = mapping->host;
3799
Theodore Ts'o0d068632017-02-14 11:31:15 -05003800 /* If we are processing an encrypted inode during orphan list handling */
Chandan Rajendra592ddec2018-12-12 15:20:10 +05303801 if (IS_ENCRYPTED(inode) && !fscrypt_has_encryption_key(inode))
Theodore Ts'o0d068632017-02-14 11:31:15 -05003802 return 0;
3803
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003804 blocksize = inode->i_sb->s_blocksize;
3805 length = blocksize - (offset & (blocksize - 1));
3806
3807 return ext4_block_zero_page_range(handle, mapping, from, length);
3808}
3809
Lukas Czernera87dd182013-05-27 23:32:35 -04003810int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
3811 loff_t lstart, loff_t length)
3812{
3813 struct super_block *sb = inode->i_sb;
3814 struct address_space *mapping = inode->i_mapping;
Lukas Czernere1be3a92013-07-01 08:12:39 -04003815 unsigned partial_start, partial_end;
Lukas Czernera87dd182013-05-27 23:32:35 -04003816 ext4_fsblk_t start, end;
3817 loff_t byte_end = (lstart + length - 1);
3818 int err = 0;
3819
Lukas Czernere1be3a92013-07-01 08:12:39 -04003820 partial_start = lstart & (sb->s_blocksize - 1);
3821 partial_end = byte_end & (sb->s_blocksize - 1);
3822
Lukas Czernera87dd182013-05-27 23:32:35 -04003823 start = lstart >> sb->s_blocksize_bits;
3824 end = byte_end >> sb->s_blocksize_bits;
3825
3826 /* Handle partial zero within the single block */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003827 if (start == end &&
3828 (partial_start || (partial_end != sb->s_blocksize - 1))) {
Lukas Czernera87dd182013-05-27 23:32:35 -04003829 err = ext4_block_zero_page_range(handle, mapping,
3830 lstart, length);
3831 return err;
3832 }
3833 /* Handle partial zero out on the start of the range */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003834 if (partial_start) {
Lukas Czernera87dd182013-05-27 23:32:35 -04003835 err = ext4_block_zero_page_range(handle, mapping,
3836 lstart, sb->s_blocksize);
3837 if (err)
3838 return err;
3839 }
3840 /* Handle partial zero out on the end of the range */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003841 if (partial_end != sb->s_blocksize - 1)
Lukas Czernera87dd182013-05-27 23:32:35 -04003842 err = ext4_block_zero_page_range(handle, mapping,
Lukas Czernere1be3a92013-07-01 08:12:39 -04003843 byte_end - partial_end,
3844 partial_end + 1);
Lukas Czernera87dd182013-05-27 23:32:35 -04003845 return err;
3846}
3847
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003848int ext4_can_truncate(struct inode *inode)
3849{
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003850 if (S_ISREG(inode->i_mode))
3851 return 1;
3852 if (S_ISDIR(inode->i_mode))
3853 return 1;
3854 if (S_ISLNK(inode->i_mode))
3855 return !ext4_inode_is_fast_symlink(inode);
3856 return 0;
3857}
3858
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003859/*
Jan Kara01127842015-12-07 14:34:49 -05003860 * We have to make sure i_disksize gets properly updated before we truncate
3861 * page cache due to hole punching or zero range. Otherwise i_disksize update
3862 * can get lost as it may have been postponed to submission of writeback but
3863 * that will never happen after we truncate page cache.
3864 */
3865int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset,
3866 loff_t len)
3867{
3868 handle_t *handle;
3869 loff_t size = i_size_read(inode);
3870
Al Viro59551022016-01-22 15:40:57 -05003871 WARN_ON(!inode_is_locked(inode));
Jan Kara01127842015-12-07 14:34:49 -05003872 if (offset > size || offset + len < size)
3873 return 0;
3874
3875 if (EXT4_I(inode)->i_disksize >= size)
3876 return 0;
3877
3878 handle = ext4_journal_start(inode, EXT4_HT_MISC, 1);
3879 if (IS_ERR(handle))
3880 return PTR_ERR(handle);
3881 ext4_update_i_disksize(inode, size);
3882 ext4_mark_inode_dirty(handle, inode);
3883 ext4_journal_stop(handle);
3884
3885 return 0;
3886}
3887
Ross Zwislerb1f38212018-09-11 13:31:16 -04003888static void ext4_wait_dax_page(struct ext4_inode_info *ei)
Ross Zwisler430657b2018-07-29 17:00:22 -04003889{
Ross Zwisler430657b2018-07-29 17:00:22 -04003890 up_write(&ei->i_mmap_sem);
3891 schedule();
3892 down_write(&ei->i_mmap_sem);
3893}
3894
3895int ext4_break_layouts(struct inode *inode)
3896{
3897 struct ext4_inode_info *ei = EXT4_I(inode);
3898 struct page *page;
Ross Zwisler430657b2018-07-29 17:00:22 -04003899 int error;
3900
3901 if (WARN_ON_ONCE(!rwsem_is_locked(&ei->i_mmap_sem)))
3902 return -EINVAL;
3903
3904 do {
Ross Zwisler430657b2018-07-29 17:00:22 -04003905 page = dax_layout_busy_page(inode->i_mapping);
3906 if (!page)
3907 return 0;
3908
3909 error = ___wait_var_event(&page->_refcount,
3910 atomic_read(&page->_refcount) == 1,
3911 TASK_INTERRUPTIBLE, 0, 0,
Ross Zwislerb1f38212018-09-11 13:31:16 -04003912 ext4_wait_dax_page(ei));
3913 } while (error == 0);
Ross Zwisler430657b2018-07-29 17:00:22 -04003914
3915 return error;
3916}
3917
Jan Kara01127842015-12-07 14:34:49 -05003918/*
Ross Zwislercca32b72016-09-22 11:49:38 -04003919 * ext4_punch_hole: punches a hole in a file by releasing the blocks
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003920 * associated with the given offset and length
3921 *
3922 * @inode: File inode
3923 * @offset: The offset where the hole will begin
3924 * @len: The length of the hole
3925 *
Anatol Pomozov4907cb72012-09-01 10:31:09 -07003926 * Returns: 0 on success or negative on failure
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003927 */
3928
Ashish Sangwanaeb28172013-07-01 08:12:38 -04003929int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length)
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003930{
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003931 struct super_block *sb = inode->i_sb;
3932 ext4_lblk_t first_block, stop_block;
3933 struct address_space *mapping = inode->i_mapping;
Lukas Czernera87dd182013-05-27 23:32:35 -04003934 loff_t first_block_offset, last_block_offset;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003935 handle_t *handle;
3936 unsigned int credits;
3937 int ret = 0;
3938
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003939 if (!S_ISREG(inode->i_mode))
Allison Henderson73355192012-03-21 22:23:31 -04003940 return -EOPNOTSUPP;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003941
Lukas Czernerb8a86842014-03-18 18:05:35 -04003942 trace_ext4_punch_hole(inode, offset, length, 0);
Zheng Liuaaddea82013-01-16 20:21:26 -05003943
Theodore Ts'oc1e82202019-08-23 22:38:00 -04003944 ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA);
3945 if (ext4_has_inline_data(inode)) {
3946 down_write(&EXT4_I(inode)->i_mmap_sem);
3947 ret = ext4_convert_inline_data(inode);
3948 up_write(&EXT4_I(inode)->i_mmap_sem);
3949 if (ret)
3950 return ret;
3951 }
3952
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003953 /*
3954 * Write out all dirty pages to avoid race conditions
3955 * Then release them.
3956 */
Ross Zwislercca32b72016-09-22 11:49:38 -04003957 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003958 ret = filemap_write_and_wait_range(mapping, offset,
3959 offset + length - 1);
3960 if (ret)
3961 return ret;
3962 }
3963
Al Viro59551022016-01-22 15:40:57 -05003964 inode_lock(inode);
Lukas Czerner9ef06ce2014-04-12 09:47:00 -04003965
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003966 /* No need to punch hole beyond i_size */
3967 if (offset >= inode->i_size)
3968 goto out_mutex;
3969
3970 /*
3971 * If the hole extends beyond i_size, set the hole
3972 * to end after the page that contains i_size
3973 */
3974 if (offset + length > inode->i_size) {
3975 length = inode->i_size +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003976 PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) -
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003977 offset;
3978 }
3979
Jan Karaa3612932013-08-16 21:19:41 -04003980 if (offset & (sb->s_blocksize - 1) ||
3981 (offset + length) & (sb->s_blocksize - 1)) {
3982 /*
3983 * Attach jinode to inode for jbd2 if we do any zeroing of
3984 * partial block
3985 */
3986 ret = ext4_inode_attach_jinode(inode);
3987 if (ret < 0)
3988 goto out_mutex;
3989
3990 }
3991
Jan Karaea3d7202015-12-07 14:28:03 -05003992 /* Wait all existing dio workers, newcomers will block on i_mutex */
Jan Karaea3d7202015-12-07 14:28:03 -05003993 inode_dio_wait(inode);
3994
3995 /*
3996 * Prevent page faults from reinstantiating pages we have released from
3997 * page cache.
3998 */
3999 down_write(&EXT4_I(inode)->i_mmap_sem);
Ross Zwisler430657b2018-07-29 17:00:22 -04004000
4001 ret = ext4_break_layouts(inode);
4002 if (ret)
4003 goto out_dio;
4004
Lukas Czernera87dd182013-05-27 23:32:35 -04004005 first_block_offset = round_up(offset, sb->s_blocksize);
4006 last_block_offset = round_down((offset + length), sb->s_blocksize) - 1;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004007
Lukas Czernera87dd182013-05-27 23:32:35 -04004008 /* Now release the pages and zero block aligned part of pages*/
Jan Kara01127842015-12-07 14:34:49 -05004009 if (last_block_offset > first_block_offset) {
4010 ret = ext4_update_disksize_before_punch(inode, offset, length);
4011 if (ret)
4012 goto out_dio;
Lukas Czernera87dd182013-05-27 23:32:35 -04004013 truncate_pagecache_range(inode, first_block_offset,
4014 last_block_offset);
Jan Kara01127842015-12-07 14:34:49 -05004015 }
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004016
4017 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4018 credits = ext4_writepage_trans_blocks(inode);
4019 else
4020 credits = ext4_blocks_for_truncate(inode);
4021 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
4022 if (IS_ERR(handle)) {
4023 ret = PTR_ERR(handle);
4024 ext4_std_error(sb, ret);
4025 goto out_dio;
4026 }
4027
Lukas Czernera87dd182013-05-27 23:32:35 -04004028 ret = ext4_zero_partial_blocks(handle, inode, offset,
4029 length);
4030 if (ret)
4031 goto out_stop;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004032
4033 first_block = (offset + sb->s_blocksize - 1) >>
4034 EXT4_BLOCK_SIZE_BITS(sb);
4035 stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
4036
Lukas Czernereee597a2018-05-13 19:28:35 -04004037 /* If there are blocks to remove, do it */
4038 if (stop_block > first_block) {
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004039
Lukas Czernereee597a2018-05-13 19:28:35 -04004040 down_write(&EXT4_I(inode)->i_data_sem);
4041 ext4_discard_preallocations(inode);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004042
Lukas Czernereee597a2018-05-13 19:28:35 -04004043 ret = ext4_es_remove_extent(inode, first_block,
4044 stop_block - first_block);
4045 if (ret) {
4046 up_write(&EXT4_I(inode)->i_data_sem);
4047 goto out_stop;
4048 }
4049
4050 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4051 ret = ext4_ext_remove_space(inode, first_block,
4052 stop_block - 1);
4053 else
4054 ret = ext4_ind_remove_space(handle, inode, first_block,
4055 stop_block);
4056
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004057 up_write(&EXT4_I(inode)->i_data_sem);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004058 }
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004059 if (IS_SYNC(inode))
4060 ext4_handle_sync(handle);
Maxim Patlasove251f9b2014-02-20 16:58:05 -05004061
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004062 inode->i_mtime = inode->i_ctime = current_time(inode);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004063 ext4_mark_inode_dirty(handle, inode);
Jan Kara67a7d5f2017-05-29 13:24:55 -04004064 if (ret >= 0)
4065 ext4_update_inode_fsync_trans(handle, inode, 1);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004066out_stop:
4067 ext4_journal_stop(handle);
4068out_dio:
Jan Karaea3d7202015-12-07 14:28:03 -05004069 up_write(&EXT4_I(inode)->i_mmap_sem);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004070out_mutex:
Al Viro59551022016-01-22 15:40:57 -05004071 inode_unlock(inode);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004072 return ret;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004073}
4074
Jan Karaa3612932013-08-16 21:19:41 -04004075int ext4_inode_attach_jinode(struct inode *inode)
4076{
4077 struct ext4_inode_info *ei = EXT4_I(inode);
4078 struct jbd2_inode *jinode;
4079
4080 if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal)
4081 return 0;
4082
4083 jinode = jbd2_alloc_inode(GFP_KERNEL);
4084 spin_lock(&inode->i_lock);
4085 if (!ei->jinode) {
4086 if (!jinode) {
4087 spin_unlock(&inode->i_lock);
4088 return -ENOMEM;
4089 }
4090 ei->jinode = jinode;
4091 jbd2_journal_init_jbd_inode(ei->jinode, inode);
4092 jinode = NULL;
4093 }
4094 spin_unlock(&inode->i_lock);
4095 if (unlikely(jinode != NULL))
4096 jbd2_free_inode(jinode);
4097 return 0;
4098}
4099
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004100/*
Mingming Cao617ba132006-10-11 01:20:53 -07004101 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004102 *
Mingming Cao617ba132006-10-11 01:20:53 -07004103 * We block out ext4_get_block() block instantiations across the entire
4104 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004105 * simultaneously on behalf of the same inode.
4106 *
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08004107 * As we work through the truncate and commit bits of it to the journal there
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004108 * is one core, guiding principle: the file's tree must always be consistent on
4109 * disk. We must be able to restart the truncate after a crash.
4110 *
4111 * The file's tree may be transiently inconsistent in memory (although it
4112 * probably isn't), but whenever we close off and commit a journal transaction,
4113 * the contents of (the filesystem + the journal) must be consistent and
4114 * restartable. It's pretty simple, really: bottom up, right to left (although
4115 * left-to-right works OK too).
4116 *
4117 * Note that at recovery time, journal replay occurs *before* the restart of
4118 * truncate against the orphan inode list.
4119 *
4120 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07004121 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004122 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07004123 * ext4_truncate() to have another go. So there will be instantiated blocks
4124 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004125 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07004126 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004127 */
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004128int ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004129{
Theodore Ts'o819c4922013-04-03 12:47:17 -04004130 struct ext4_inode_info *ei = EXT4_I(inode);
4131 unsigned int credits;
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004132 int err = 0;
Theodore Ts'o819c4922013-04-03 12:47:17 -04004133 handle_t *handle;
4134 struct address_space *mapping = inode->i_mapping;
Theodore Ts'o819c4922013-04-03 12:47:17 -04004135
Theodore Ts'o19b5ef62013-04-03 21:58:52 -04004136 /*
4137 * There is a possibility that we're either freeing the inode
Matthew Wilcoxe04027e2014-03-24 15:15:07 -04004138 * or it's a completely new inode. In those cases we might not
Theodore Ts'o19b5ef62013-04-03 21:58:52 -04004139 * have i_mutex locked because it's not necessary.
4140 */
4141 if (!(inode->i_state & (I_NEW|I_FREEING)))
Al Viro59551022016-01-22 15:40:57 -05004142 WARN_ON(!inode_is_locked(inode));
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004143 trace_ext4_truncate_enter(inode);
4144
Duane Griffin91ef4ca2008-07-11 19:27:31 -04004145 if (!ext4_can_truncate(inode))
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004146 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004147
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004148 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004149
Theodore Ts'o5534fb52009-09-17 09:34:16 -04004150 if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004151 ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
Theodore Ts'o7d8f9f72009-02-24 08:21:14 -05004152
Tao Maaef1c852012-12-10 14:06:02 -05004153 if (ext4_has_inline_data(inode)) {
4154 int has_inline = 1;
4155
Theodore Ts'o01daf942017-01-22 19:35:49 -05004156 err = ext4_inline_data_truncate(inode, &has_inline);
4157 if (err)
4158 return err;
Tao Maaef1c852012-12-10 14:06:02 -05004159 if (has_inline)
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004160 return 0;
Tao Maaef1c852012-12-10 14:06:02 -05004161 }
4162
Jan Karaa3612932013-08-16 21:19:41 -04004163 /* If we zero-out tail of the page, we have to create jinode for jbd2 */
4164 if (inode->i_size & (inode->i_sb->s_blocksize - 1)) {
4165 if (ext4_inode_attach_jinode(inode) < 0)
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004166 return 0;
Jan Karaa3612932013-08-16 21:19:41 -04004167 }
4168
Amir Goldsteinff9893d2011-06-27 16:36:31 -04004169 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Theodore Ts'o819c4922013-04-03 12:47:17 -04004170 credits = ext4_writepage_trans_blocks(inode);
Amir Goldsteinff9893d2011-06-27 16:36:31 -04004171 else
Theodore Ts'o819c4922013-04-03 12:47:17 -04004172 credits = ext4_blocks_for_truncate(inode);
4173
4174 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004175 if (IS_ERR(handle))
4176 return PTR_ERR(handle);
Theodore Ts'o819c4922013-04-03 12:47:17 -04004177
Lukas Czernereb3544c2013-05-27 23:32:35 -04004178 if (inode->i_size & (inode->i_sb->s_blocksize - 1))
4179 ext4_block_truncate_page(handle, mapping, inode->i_size);
Theodore Ts'o819c4922013-04-03 12:47:17 -04004180
4181 /*
4182 * We add the inode to the orphan list, so that if this
4183 * truncate spans multiple transactions, and we crash, we will
4184 * resume the truncate when the filesystem recovers. It also
4185 * marks the inode dirty, to catch the new size.
4186 *
4187 * Implication: the file must always be in a sane, consistent
4188 * truncatable state while each transaction commits.
4189 */
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004190 err = ext4_orphan_add(handle, inode);
4191 if (err)
Theodore Ts'o819c4922013-04-03 12:47:17 -04004192 goto out_stop;
4193
4194 down_write(&EXT4_I(inode)->i_data_sem);
4195
4196 ext4_discard_preallocations(inode);
4197
4198 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Theodore Ts'od0abb362016-11-13 22:02:28 -05004199 err = ext4_ext_truncate(handle, inode);
Theodore Ts'o819c4922013-04-03 12:47:17 -04004200 else
4201 ext4_ind_truncate(handle, inode);
4202
4203 up_write(&ei->i_data_sem);
Theodore Ts'od0abb362016-11-13 22:02:28 -05004204 if (err)
4205 goto out_stop;
Theodore Ts'o819c4922013-04-03 12:47:17 -04004206
4207 if (IS_SYNC(inode))
4208 ext4_handle_sync(handle);
4209
4210out_stop:
4211 /*
4212 * If this was a simple ftruncate() and the file will remain alive,
4213 * then we need to clear up the orphan record which we created above.
4214 * However, if this was a real unlink then we were called by
Wang Shilong58d86a52014-11-25 16:17:29 -05004215 * ext4_evict_inode(), and we allow that function to clean up the
Theodore Ts'o819c4922013-04-03 12:47:17 -04004216 * orphan info for us.
4217 */
4218 if (inode->i_nlink)
4219 ext4_orphan_del(handle, inode);
4220
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05004221 inode->i_mtime = inode->i_ctime = current_time(inode);
Theodore Ts'o819c4922013-04-03 12:47:17 -04004222 ext4_mark_inode_dirty(handle, inode);
4223 ext4_journal_stop(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07004224
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004225 trace_ext4_truncate_exit(inode);
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05004226 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004227}
4228
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004229/*
Mingming Cao617ba132006-10-11 01:20:53 -07004230 * ext4_get_inode_loc returns with an extra refcount against the inode's
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004231 * underlying buffer_head on success. If 'in_mem' is true, we have all
4232 * data in memory that is needed to recreate the on-disk version of this
4233 * inode.
4234 */
Mingming Cao617ba132006-10-11 01:20:53 -07004235static int __ext4_get_inode_loc(struct inode *inode,
4236 struct ext4_iloc *iloc, int in_mem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004237{
Theodore Ts'o240799c2008-10-09 23:53:47 -04004238 struct ext4_group_desc *gdp;
4239 struct buffer_head *bh;
4240 struct super_block *sb = inode->i_sb;
4241 ext4_fsblk_t block;
Linus Torvalds02f03c42019-09-29 17:59:23 -07004242 struct blk_plug plug;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004243 int inodes_per_block, inode_offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004244
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004245 iloc->bh = NULL;
Theodore Ts'oc37e9e02018-06-17 00:41:14 -04004246 if (inode->i_ino < EXT4_ROOT_INO ||
4247 inode->i_ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004248 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004249
Theodore Ts'o240799c2008-10-09 23:53:47 -04004250 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
4251 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
4252 if (!gdp)
4253 return -EIO;
4254
4255 /*
4256 * Figure out the offset within the block group inode table
4257 */
Tao Ma00d09882011-05-09 10:26:41 -04004258 inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004259 inode_offset = ((inode->i_ino - 1) %
4260 EXT4_INODES_PER_GROUP(sb));
4261 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
4262 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
4263
4264 bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05004265 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -05004266 return -ENOMEM;
Theodore Ts'o46f870d62019-11-21 13:09:43 -05004267 if (ext4_simulate_fail(sb, EXT4_SIM_INODE_EIO))
4268 goto simulate_eio;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004269 if (!buffer_uptodate(bh)) {
4270 lock_buffer(bh);
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04004271
4272 /*
4273 * If the buffer has the write error flag, we have failed
4274 * to write out another inode in the same block. In this
4275 * case, we don't have to read the block because we may
4276 * read the old inode data successfully.
4277 */
4278 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
4279 set_buffer_uptodate(bh);
4280
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004281 if (buffer_uptodate(bh)) {
4282 /* someone brought it uptodate while we waited */
4283 unlock_buffer(bh);
4284 goto has_buffer;
4285 }
4286
4287 /*
4288 * If we have all information of the inode in memory and this
4289 * is the only valid inode in the block, we need not read the
4290 * block.
4291 */
4292 if (in_mem) {
4293 struct buffer_head *bitmap_bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004294 int i, start;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004295
Theodore Ts'o240799c2008-10-09 23:53:47 -04004296 start = inode_offset & ~(inodes_per_block - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004297
4298 /* Is the inode bitmap in cache? */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004299 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
Wang Shilongaebf0242013-01-12 16:28:47 -05004300 if (unlikely(!bitmap_bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004301 goto make_io;
4302
4303 /*
4304 * If the inode bitmap isn't in cache then the
4305 * optimisation may end up performing two reads instead
4306 * of one, so skip it.
4307 */
4308 if (!buffer_uptodate(bitmap_bh)) {
4309 brelse(bitmap_bh);
4310 goto make_io;
4311 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04004312 for (i = start; i < start + inodes_per_block; i++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004313 if (i == inode_offset)
4314 continue;
Mingming Cao617ba132006-10-11 01:20:53 -07004315 if (ext4_test_bit(i, bitmap_bh->b_data))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004316 break;
4317 }
4318 brelse(bitmap_bh);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004319 if (i == start + inodes_per_block) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004320 /* all other inodes are free, so skip I/O */
4321 memset(bh->b_data, 0, bh->b_size);
4322 set_buffer_uptodate(bh);
4323 unlock_buffer(bh);
4324 goto has_buffer;
4325 }
4326 }
4327
4328make_io:
4329 /*
Theodore Ts'o240799c2008-10-09 23:53:47 -04004330 * If we need to do any I/O, try to pre-readahead extra
4331 * blocks from the inode table.
4332 */
Linus Torvalds02f03c42019-09-29 17:59:23 -07004333 blk_start_plug(&plug);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004334 if (EXT4_SB(sb)->s_inode_readahead_blks) {
4335 ext4_fsblk_t b, end, table;
4336 unsigned num;
Theodore Ts'o0d606e2c2013-04-23 08:59:35 -04004337 __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004338
4339 table = ext4_inode_table(sb, gdp);
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004340 /* s_inode_readahead_blks is always a power of 2 */
Theodore Ts'o0d606e2c2013-04-23 08:59:35 -04004341 b = block & ~((ext4_fsblk_t) ra_blks - 1);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004342 if (table > b)
4343 b = table;
Theodore Ts'o0d606e2c2013-04-23 08:59:35 -04004344 end = b + ra_blks;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004345 num = EXT4_INODES_PER_GROUP(sb);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04004346 if (ext4_has_group_desc_csum(sb))
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004347 num -= ext4_itable_unused_count(sb, gdp);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004348 table += num / inodes_per_block;
4349 if (end > table)
4350 end = table;
4351 while (b <= end)
4352 sb_breadahead(sb, b++);
4353 }
4354
4355 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004356 * There are other valid inodes in the buffer, this inode
4357 * has in-inode xattrs, or we don't have this inode in memory.
4358 * Read the block from disk.
4359 */
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004360 trace_ext4_load_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004361 get_bh(bh);
4362 bh->b_end_io = end_buffer_read_sync;
Mike Christie2a222ca2016-06-05 14:31:43 -05004363 submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh);
Linus Torvalds02f03c42019-09-29 17:59:23 -07004364 blk_finish_plug(&plug);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004365 wait_on_buffer(bh);
4366 if (!buffer_uptodate(bh)) {
Theodore Ts'o46f870d62019-11-21 13:09:43 -05004367 simulate_eio:
Theodore Ts'o878520a2019-11-19 21:54:15 -05004368 ext4_set_errno(inode->i_sb, EIO);
Theodore Ts'oc398eda2010-07-27 11:56:40 -04004369 EXT4_ERROR_INODE_BLOCK(inode, block,
4370 "unable to read itable block");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004371 brelse(bh);
4372 return -EIO;
4373 }
4374 }
4375has_buffer:
4376 iloc->bh = bh;
4377 return 0;
4378}
4379
Mingming Cao617ba132006-10-11 01:20:53 -07004380int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004381{
4382 /* We have all inode data except xattrs in memory here. */
Mingming Cao617ba132006-10-11 01:20:53 -07004383 return __ext4_get_inode_loc(inode, iloc,
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004384 !ext4_test_inode_state(inode, EXT4_STATE_XATTR));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004385}
4386
Ross Zwisler66425862017-10-12 12:00:59 -04004387static bool ext4_should_use_dax(struct inode *inode)
4388{
4389 if (!test_opt(inode->i_sb, DAX))
4390 return false;
4391 if (!S_ISREG(inode->i_mode))
4392 return false;
4393 if (ext4_should_journal_data(inode))
4394 return false;
4395 if (ext4_has_inline_data(inode))
4396 return false;
Chandan Rajendra592ddec2018-12-12 15:20:10 +05304397 if (ext4_test_inode_flag(inode, EXT4_INODE_ENCRYPT))
Ross Zwisler66425862017-10-12 12:00:59 -04004398 return false;
Eric Biggersc93d8f82019-07-22 09:26:24 -07004399 if (ext4_test_inode_flag(inode, EXT4_INODE_VERITY))
4400 return false;
Ross Zwisler66425862017-10-12 12:00:59 -04004401 return true;
4402}
4403
Mingming Cao617ba132006-10-11 01:20:53 -07004404void ext4_set_inode_flags(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004405{
Mingming Cao617ba132006-10-11 01:20:53 -07004406 unsigned int flags = EXT4_I(inode)->i_flags;
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004407 unsigned int new_fl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004408
Mingming Cao617ba132006-10-11 01:20:53 -07004409 if (flags & EXT4_SYNC_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004410 new_fl |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07004411 if (flags & EXT4_APPEND_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004412 new_fl |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07004413 if (flags & EXT4_IMMUTABLE_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004414 new_fl |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07004415 if (flags & EXT4_NOATIME_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004416 new_fl |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07004417 if (flags & EXT4_DIRSYNC_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004418 new_fl |= S_DIRSYNC;
Ross Zwisler66425862017-10-12 12:00:59 -04004419 if (ext4_should_use_dax(inode))
Ross Zwisler923ae0f2015-02-16 15:59:38 -08004420 new_fl |= S_DAX;
Eric Biggers2ee6a572017-10-09 12:15:35 -07004421 if (flags & EXT4_ENCRYPT_FL)
4422 new_fl |= S_ENCRYPTED;
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04004423 if (flags & EXT4_CASEFOLD_FL)
4424 new_fl |= S_CASEFOLD;
Eric Biggersc93d8f82019-07-22 09:26:24 -07004425 if (flags & EXT4_VERITY_FL)
4426 new_fl |= S_VERITY;
Theodore Ts'o5f16f322014-03-24 14:43:12 -04004427 inode_set_flags(inode, new_fl,
Eric Biggers2ee6a572017-10-09 12:15:35 -07004428 S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX|
Eric Biggersc93d8f82019-07-22 09:26:24 -07004429 S_ENCRYPTED|S_CASEFOLD|S_VERITY);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004430}
4431
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004432static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004433 struct ext4_inode_info *ei)
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004434{
4435 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004436 struct inode *inode = &(ei->vfs_inode);
4437 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004438
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004439 if (ext4_has_feature_huge_file(sb)) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004440 /* we are using combined 48 bit field */
4441 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
4442 le32_to_cpu(raw_inode->i_blocks_lo);
Theodore Ts'o07a03822010-06-14 09:54:48 -04004443 if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004444 /* i_blocks represent file system block size */
4445 return i_blocks << (inode->i_blkbits - 9);
4446 } else {
4447 return i_blocks;
4448 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004449 } else {
4450 return le32_to_cpu(raw_inode->i_blocks_lo);
4451 }
4452}
Jan Karaff9ddf72007-07-18 09:24:20 -04004453
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004454static inline int ext4_iget_extra_inode(struct inode *inode,
Tao Ma152a7b02012-12-02 11:13:24 -05004455 struct ext4_inode *raw_inode,
4456 struct ext4_inode_info *ei)
4457{
4458 __le32 *magic = (void *)raw_inode +
4459 EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004460
Eric Biggers290ab232016-12-01 14:51:58 -05004461 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize + sizeof(__le32) <=
4462 EXT4_INODE_SIZE(inode->i_sb) &&
4463 *magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
Tao Ma152a7b02012-12-02 11:13:24 -05004464 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004465 return ext4_find_inline_data_nolock(inode);
Tao Maf19d5872012-12-10 14:05:51 -05004466 } else
4467 EXT4_I(inode)->i_inline_off = 0;
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004468 return 0;
Tao Ma152a7b02012-12-02 11:13:24 -05004469}
4470
Li Xi040cb372016-01-08 16:01:21 -05004471int ext4_get_projid(struct inode *inode, kprojid_t *projid)
4472{
Kaho Ng0b7b7772016-09-05 23:11:58 -04004473 if (!ext4_has_feature_project(inode->i_sb))
Li Xi040cb372016-01-08 16:01:21 -05004474 return -EOPNOTSUPP;
4475 *projid = EXT4_I(inode)->i_projid;
4476 return 0;
4477}
4478
Eryu Guane254d1a2018-05-10 11:55:31 -04004479/*
4480 * ext4 has self-managed i_version for ea inodes, it stores the lower 32bit of
4481 * refcount in i_version, so use raw values if inode has EXT4_EA_INODE_FL flag
4482 * set.
4483 */
4484static inline void ext4_inode_set_iversion_queried(struct inode *inode, u64 val)
4485{
4486 if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
4487 inode_set_iversion_raw(inode, val);
4488 else
4489 inode_set_iversion_queried(inode, val);
4490}
4491static inline u64 ext4_inode_peek_iversion(const struct inode *inode)
4492{
4493 if (unlikely(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
4494 return inode_peek_iversion_raw(inode);
4495 else
4496 return inode_peek_iversion(inode);
4497}
4498
Theodore Ts'o8a363972018-12-19 12:29:13 -05004499struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
4500 ext4_iget_flags flags, const char *function,
4501 unsigned int line)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004502{
Mingming Cao617ba132006-10-11 01:20:53 -07004503 struct ext4_iloc iloc;
4504 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08004505 struct ext4_inode_info *ei;
David Howells1d1fe1e2008-02-07 00:15:37 -08004506 struct inode *inode;
Jan Karab436b9b2009-12-08 23:51:10 -05004507 journal_t *journal = EXT4_SB(sb)->s_journal;
David Howells1d1fe1e2008-02-07 00:15:37 -08004508 long ret;
Darrick J. Wong7e6e1ef2016-12-10 09:55:01 -05004509 loff_t size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004510 int block;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004511 uid_t i_uid;
4512 gid_t i_gid;
Li Xi040cb372016-01-08 16:01:21 -05004513 projid_t i_projid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004514
Theodore Ts'o191ce172018-12-31 22:34:31 -05004515 if ((!(flags & EXT4_IGET_SPECIAL) &&
Theodore Ts'o8a363972018-12-19 12:29:13 -05004516 (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)) ||
4517 (ino < EXT4_ROOT_INO) ||
4518 (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))) {
4519 if (flags & EXT4_IGET_HANDLE)
4520 return ERR_PTR(-ESTALE);
4521 __ext4_error(sb, function, line,
4522 "inode #%lu: comm %s: iget: illegal inode #",
4523 ino, current->comm);
4524 return ERR_PTR(-EFSCORRUPTED);
4525 }
4526
David Howells1d1fe1e2008-02-07 00:15:37 -08004527 inode = iget_locked(sb, ino);
4528 if (!inode)
4529 return ERR_PTR(-ENOMEM);
4530 if (!(inode->i_state & I_NEW))
4531 return inode;
4532
4533 ei = EXT4_I(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05004534 iloc.bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004535
David Howells1d1fe1e2008-02-07 00:15:37 -08004536 ret = __ext4_get_inode_loc(inode, &iloc, 0);
4537 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004538 goto bad_inode;
Mingming Cao617ba132006-10-11 01:20:53 -07004539 raw_inode = ext4_raw_inode(&iloc);
Darrick J. Wong814525f2012-04-29 18:31:10 -04004540
Theodore Ts'o8e4b5ea2018-03-29 21:56:09 -04004541 if ((ino == EXT4_ROOT_INO) && (raw_inode->i_links_count == 0)) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004542 ext4_error_inode(inode, function, line, 0,
4543 "iget: root inode unallocated");
Theodore Ts'o8e4b5ea2018-03-29 21:56:09 -04004544 ret = -EFSCORRUPTED;
4545 goto bad_inode;
4546 }
4547
Theodore Ts'o8a363972018-12-19 12:29:13 -05004548 if ((flags & EXT4_IGET_HANDLE) &&
4549 (raw_inode->i_links_count == 0) && (raw_inode->i_mode == 0)) {
4550 ret = -ESTALE;
4551 goto bad_inode;
4552 }
4553
Darrick J. Wong814525f2012-04-29 18:31:10 -04004554 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4555 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
4556 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
Eric Biggers2dc8d9e2016-12-01 14:43:33 -05004557 EXT4_INODE_SIZE(inode->i_sb) ||
4558 (ei->i_extra_isize & 3)) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004559 ext4_error_inode(inode, function, line, 0,
4560 "iget: bad extra_isize %u "
4561 "(inode size %u)",
Eric Biggers2dc8d9e2016-12-01 14:43:33 -05004562 ei->i_extra_isize,
4563 EXT4_INODE_SIZE(inode->i_sb));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004564 ret = -EFSCORRUPTED;
Darrick J. Wong814525f2012-04-29 18:31:10 -04004565 goto bad_inode;
4566 }
4567 } else
4568 ei->i_extra_isize = 0;
4569
4570 /* Precompute checksum seed for inode metadata */
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04004571 if (ext4_has_metadata_csum(sb)) {
Darrick J. Wong814525f2012-04-29 18:31:10 -04004572 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4573 __u32 csum;
4574 __le32 inum = cpu_to_le32(inode->i_ino);
4575 __le32 gen = raw_inode->i_generation;
4576 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
4577 sizeof(inum));
4578 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
4579 sizeof(gen));
4580 }
4581
Theodore Ts'o46f870d62019-11-21 13:09:43 -05004582 if (!ext4_inode_csum_verify(inode, raw_inode, ei) ||
4583 ext4_simulate_fail(sb, EXT4_SIM_INODE_CRC)) {
Theodore Ts'o878520a2019-11-19 21:54:15 -05004584 ext4_set_errno(inode->i_sb, EFSBADCRC);
Theodore Ts'o8a363972018-12-19 12:29:13 -05004585 ext4_error_inode(inode, function, line, 0,
4586 "iget: checksum invalid");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004587 ret = -EFSBADCRC;
Darrick J. Wong814525f2012-04-29 18:31:10 -04004588 goto bad_inode;
4589 }
4590
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004591 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004592 i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4593 i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Kaho Ng0b7b7772016-09-05 23:11:58 -04004594 if (ext4_has_feature_project(sb) &&
Li Xi040cb372016-01-08 16:01:21 -05004595 EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE &&
4596 EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
4597 i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid);
4598 else
4599 i_projid = EXT4_DEF_PROJID;
4600
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004601 if (!(test_opt(inode->i_sb, NO_UID32))) {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004602 i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4603 i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004604 }
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004605 i_uid_write(inode, i_uid);
4606 i_gid_write(inode, i_gid);
Li Xi040cb372016-01-08 16:01:21 -05004607 ei->i_projid = make_kprojid(&init_user_ns, i_projid);
Miklos Szeredibfe86842011-10-28 14:13:29 +02004608 set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004609
Theodore Ts'o353eb832011-01-10 12:18:25 -05004610 ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
Tao Ma67cf5b02012-12-10 14:04:46 -05004611 ei->i_inline_off = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004612 ei->i_dir_start_lookup = 0;
4613 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4614 /* We now have enough fields to check if the inode was active or not.
4615 * This is needed because nfsd might try to access dead inodes
4616 * the test is that same one that e2fsck uses
4617 * NeilBrown 1999oct15
4618 */
4619 if (inode->i_nlink == 0) {
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004620 if ((inode->i_mode == 0 ||
4621 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) &&
4622 ino != EXT4_BOOT_LOADER_INO) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004623 /* this inode is deleted */
David Howells1d1fe1e2008-02-07 00:15:37 -08004624 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004625 goto bad_inode;
4626 }
4627 /* The only unlinked inodes we let through here have
4628 * valid i_mode and are being read by the orphan
4629 * recovery code: that's fine, we're about to complete
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004630 * the process of deleting those.
4631 * OR it is the EXT4_BOOT_LOADER_INO which is
4632 * not initialized on a new filesystem. */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004633 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004634 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Toshi Kanicce6c9f2018-09-15 21:37:59 -04004635 ext4_set_inode_flags(inode);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004636 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004637 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004638 if (ext4_has_feature_64bit(sb))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004639 ei->i_file_acl |=
4640 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04004641 inode->i_size = ext4_isize(sb, raw_inode);
Darrick J. Wong7e6e1ef2016-12-10 09:55:01 -05004642 if ((size = i_size_read(inode)) < 0) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004643 ext4_error_inode(inode, function, line, 0,
4644 "iget: bad i_size value: %lld", size);
Darrick J. Wong7e6e1ef2016-12-10 09:55:01 -05004645 ret = -EFSCORRUPTED;
4646 goto bad_inode;
4647 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004648 ei->i_disksize = inode->i_size;
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03004649#ifdef CONFIG_QUOTA
4650 ei->i_reserved_quota = 0;
4651#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004652 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4653 ei->i_block_group = iloc.block_group;
Theodore Ts'oa4912122009-03-12 12:18:34 -04004654 ei->i_last_alloc_group = ~0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004655 /*
4656 * NOTE! The in-memory inode i_data array is in little-endian order
4657 * even on big-endian machines: we do NOT byteswap the block numbers!
4658 */
Mingming Cao617ba132006-10-11 01:20:53 -07004659 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004660 ei->i_data[block] = raw_inode->i_block[block];
4661 INIT_LIST_HEAD(&ei->i_orphan);
4662
Jan Karab436b9b2009-12-08 23:51:10 -05004663 /*
4664 * Set transaction id's of transactions that have to be committed
4665 * to finish f[data]sync. We set them to currently running transaction
4666 * as we cannot be sure that the inode or some of its metadata isn't
4667 * part of the transaction - the inode could have been reclaimed and
4668 * now it is reread from disk.
4669 */
4670 if (journal) {
4671 transaction_t *transaction;
4672 tid_t tid;
4673
Theodore Ts'oa931da62010-08-03 21:35:12 -04004674 read_lock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05004675 if (journal->j_running_transaction)
4676 transaction = journal->j_running_transaction;
4677 else
4678 transaction = journal->j_committing_transaction;
4679 if (transaction)
4680 tid = transaction->t_tid;
4681 else
4682 tid = journal->j_commit_sequence;
Theodore Ts'oa931da62010-08-03 21:35:12 -04004683 read_unlock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05004684 ei->i_sync_tid = tid;
4685 ei->i_datasync_tid = tid;
4686 }
4687
Eric Sandeen0040d982008-02-05 22:36:43 -05004688 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004689 if (ei->i_extra_isize == 0) {
4690 /* The extra space is currently unused. Use it. */
Eric Biggers2dc8d9e2016-12-01 14:43:33 -05004691 BUILD_BUG_ON(sizeof(struct ext4_inode) & 3);
Mingming Cao617ba132006-10-11 01:20:53 -07004692 ei->i_extra_isize = sizeof(struct ext4_inode) -
4693 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004694 } else {
Theodore Ts'oeb9b5f02018-05-22 17:14:07 -04004695 ret = ext4_iget_extra_inode(inode, raw_inode, ei);
4696 if (ret)
4697 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004698 }
Darrick J. Wong814525f2012-04-29 18:31:10 -04004699 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004700
Kalpak Shahef7f3832007-07-18 09:15:20 -04004701 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4702 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4703 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4704 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4705
Theodore Ts'oed3654e2014-03-24 14:09:06 -04004706 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
Jeff Laytonee73f9a2018-01-09 08:21:39 -05004707 u64 ivers = le32_to_cpu(raw_inode->i_disk_version);
4708
Theodore Ts'oc4f65702014-03-20 00:32:57 -04004709 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4710 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
Jeff Laytonee73f9a2018-01-09 08:21:39 -05004711 ivers |=
Theodore Ts'oc4f65702014-03-20 00:32:57 -04004712 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
4713 }
Eryu Guane254d1a2018-05-10 11:55:31 -04004714 ext4_inode_set_iversion_queried(inode, ivers);
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004715 }
4716
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004717 ret = 0;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004718 if (ei->i_file_acl &&
Theodore Ts'o10329882009-11-15 15:29:56 -05004719 !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004720 ext4_error_inode(inode, function, line, 0,
4721 "iget: bad extended attribute block %llu",
Theodore Ts'o24676da2010-05-16 21:00:00 -04004722 ei->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004723 ret = -EFSCORRUPTED;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004724 goto bad_inode;
Tao Maf19d5872012-12-10 14:05:51 -05004725 } else if (!ext4_has_inline_data(inode)) {
Liu Songbc716522018-08-02 00:11:16 -04004726 /* validate the block references in the inode */
4727 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4728 (S_ISLNK(inode->i_mode) &&
4729 !ext4_inode_is_fast_symlink(inode))) {
4730 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Tao Maf19d5872012-12-10 14:05:51 -05004731 ret = ext4_ext_check_inode(inode);
Liu Songbc716522018-08-02 00:11:16 -04004732 else
4733 ret = ext4_ind_check_inode(inode);
Tao Maf19d5872012-12-10 14:05:51 -05004734 }
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004735 }
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004736 if (ret)
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004737 goto bad_inode;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -04004738
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004739 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004740 inode->i_op = &ext4_file_inode_operations;
Boaz Harroshbe64f882015-04-15 16:15:17 -07004741 inode->i_fop = &ext4_file_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07004742 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004743 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004744 inode->i_op = &ext4_dir_inode_operations;
4745 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004746 } else if (S_ISLNK(inode->i_mode)) {
Luis R. Rodriguez6390d332018-05-13 16:45:56 -04004747 /* VFS does not allow setting these so must be corruption */
4748 if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) {
Theodore Ts'o8a363972018-12-19 12:29:13 -05004749 ext4_error_inode(inode, function, line, 0,
4750 "iget: immutable or append flags "
4751 "not allowed on symlinks");
Luis R. Rodriguez6390d332018-05-13 16:45:56 -04004752 ret = -EFSCORRUPTED;
4753 goto bad_inode;
4754 }
Chandan Rajendra592ddec2018-12-12 15:20:10 +05304755 if (IS_ENCRYPTED(inode)) {
Al Viroa7a67e82015-04-27 17:51:30 -04004756 inode->i_op = &ext4_encrypted_symlink_inode_operations;
4757 ext4_set_aops(inode);
4758 } else if (ext4_inode_is_fast_symlink(inode)) {
Al Viro75e75662015-05-02 10:13:58 -04004759 inode->i_link = (char *)ei->i_data;
Mingming Cao617ba132006-10-11 01:20:53 -07004760 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00004761 nd_terminate_link(ei->i_data, inode->i_size,
4762 sizeof(ei->i_data) - 1);
4763 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004764 inode->i_op = &ext4_symlink_inode_operations;
4765 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004766 }
Al Viro21fc61c2015-11-17 01:07:57 -05004767 inode_nohighmem(inode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004768 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
4769 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004770 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004771 if (raw_inode->i_block[0])
4772 init_special_inode(inode, inode->i_mode,
4773 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4774 else
4775 init_special_inode(inode, inode->i_mode,
4776 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004777 } else if (ino == EXT4_BOOT_LOADER_INO) {
4778 make_bad_inode(inode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004779 } else {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004780 ret = -EFSCORRUPTED;
Theodore Ts'o8a363972018-12-19 12:29:13 -05004781 ext4_error_inode(inode, function, line, 0,
4782 "iget: bogus i_mode (%o)", inode->i_mode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004783 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004784 }
Theodore Ts'o6456ca62019-09-03 01:43:17 -04004785 if (IS_CASEFOLDED(inode) && !ext4_has_feature_casefold(inode->i_sb))
4786 ext4_error_inode(inode, function, line, 0,
4787 "casefold flag without casefold feature");
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004788 brelse(iloc.bh);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04004789
David Howells1d1fe1e2008-02-07 00:15:37 -08004790 unlock_new_inode(inode);
4791 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004792
4793bad_inode:
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004794 brelse(iloc.bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004795 iget_failed(inode);
4796 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004797}
4798
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004799static int ext4_inode_blocks_set(handle_t *handle,
4800 struct ext4_inode *raw_inode,
4801 struct ext4_inode_info *ei)
4802{
4803 struct inode *inode = &(ei->vfs_inode);
4804 u64 i_blocks = inode->i_blocks;
4805 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004806
4807 if (i_blocks <= ~0U) {
4808 /*
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004809 * i_blocks can be represented in a 32 bit variable
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004810 * as multiple of 512 bytes
4811 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004812 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004813 raw_inode->i_blocks_high = 0;
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004814 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004815 return 0;
4816 }
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004817 if (!ext4_has_feature_huge_file(sb))
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004818 return -EFBIG;
4819
4820 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004821 /*
4822 * i_blocks can be represented in a 48 bit variable
4823 * as multiple of 512 bytes
4824 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004825 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004826 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004827 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004828 } else {
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004829 ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004830 /* i_block is stored in file system block size */
4831 i_blocks = i_blocks >> (inode->i_blkbits - 9);
4832 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4833 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004834 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004835 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004836}
4837
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004838struct other_inode {
4839 unsigned long orig_ino;
4840 struct ext4_inode *raw_inode;
4841};
4842
4843static int other_inode_match(struct inode * inode, unsigned long ino,
4844 void *data)
4845{
4846 struct other_inode *oi = (struct other_inode *) data;
4847
4848 if ((inode->i_ino != ino) ||
4849 (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW |
Christoph Hellwig0e11f642018-02-21 07:54:49 -08004850 I_DIRTY_INODE)) ||
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004851 ((inode->i_state & I_DIRTY_TIME) == 0))
4852 return 0;
4853 spin_lock(&inode->i_lock);
4854 if (((inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW |
Christoph Hellwig0e11f642018-02-21 07:54:49 -08004855 I_DIRTY_INODE)) == 0) &&
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004856 (inode->i_state & I_DIRTY_TIME)) {
4857 struct ext4_inode_info *ei = EXT4_I(inode);
4858
4859 inode->i_state &= ~(I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED);
4860 spin_unlock(&inode->i_lock);
4861
4862 spin_lock(&ei->i_raw_lock);
4863 EXT4_INODE_SET_XTIME(i_ctime, inode, oi->raw_inode);
4864 EXT4_INODE_SET_XTIME(i_mtime, inode, oi->raw_inode);
4865 EXT4_INODE_SET_XTIME(i_atime, inode, oi->raw_inode);
4866 ext4_inode_csum_set(inode, oi->raw_inode, ei);
4867 spin_unlock(&ei->i_raw_lock);
4868 trace_ext4_other_inode_update_time(inode, oi->orig_ino);
4869 return -1;
4870 }
4871 spin_unlock(&inode->i_lock);
4872 return -1;
4873}
4874
4875/*
4876 * Opportunistically update the other time fields for other inodes in
4877 * the same inode table block.
4878 */
4879static void ext4_update_other_inodes_time(struct super_block *sb,
4880 unsigned long orig_ino, char *buf)
4881{
4882 struct other_inode oi;
4883 unsigned long ino;
4884 int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
4885 int inode_size = EXT4_INODE_SIZE(sb);
4886
4887 oi.orig_ino = orig_ino;
Theodore Ts'o0f0ff9a2015-07-01 23:37:46 -04004888 /*
4889 * Calculate the first inode in the inode table block. Inode
4890 * numbers are one-based. That is, the first inode in a block
4891 * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1).
4892 */
4893 ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1;
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004894 for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) {
4895 if (ino == orig_ino)
4896 continue;
4897 oi.raw_inode = (struct ext4_inode *) buf;
4898 (void) find_inode_nowait(sb, ino, other_inode_match, &oi);
4899 }
4900}
4901
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004902/*
4903 * Post the struct inode info into an on-disk inode location in the
4904 * buffer-cache. This gobbles the caller's reference to the
4905 * buffer_head in the inode location struct.
4906 *
4907 * The caller must have write access to iloc->bh.
4908 */
Mingming Cao617ba132006-10-11 01:20:53 -07004909static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004910 struct inode *inode,
Frank Mayhar830156c2009-09-29 10:07:47 -04004911 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004912{
Mingming Cao617ba132006-10-11 01:20:53 -07004913 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
4914 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004915 struct buffer_head *bh = iloc->bh;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004916 struct super_block *sb = inode->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004917 int err = 0, rc, block;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004918 int need_datasync = 0, set_large_file = 0;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004919 uid_t i_uid;
4920 gid_t i_gid;
Li Xi040cb372016-01-08 16:01:21 -05004921 projid_t i_projid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004922
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004923 spin_lock(&ei->i_raw_lock);
4924
4925 /* For fields not tracked in the in-memory inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004926 * initialise them to zero for new inodes. */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004927 if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
Mingming Cao617ba132006-10-11 01:20:53 -07004928 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004929
4930 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004931 i_uid = i_uid_read(inode);
4932 i_gid = i_gid_read(inode);
Li Xi040cb372016-01-08 16:01:21 -05004933 i_projid = from_kprojid(&init_user_ns, ei->i_projid);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004934 if (!(test_opt(inode->i_sb, NO_UID32))) {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004935 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid));
4936 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004937/*
4938 * Fix up interoperability with old kernels. Otherwise, old inodes get
4939 * re-used with the upper 16 bits of the uid/gid intact
4940 */
Daeho Jeong93e3b4e2016-09-05 22:56:10 -04004941 if (ei->i_dtime && list_empty(&ei->i_orphan)) {
4942 raw_inode->i_uid_high = 0;
4943 raw_inode->i_gid_high = 0;
4944 } else {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004945 raw_inode->i_uid_high =
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004946 cpu_to_le16(high_16_bits(i_uid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004947 raw_inode->i_gid_high =
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004948 cpu_to_le16(high_16_bits(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004949 }
4950 } else {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004951 raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid));
4952 raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004953 raw_inode->i_uid_high = 0;
4954 raw_inode->i_gid_high = 0;
4955 }
4956 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004957
4958 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
4959 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
4960 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
4961 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
4962
Li Xibce92d52014-10-01 22:11:06 -04004963 err = ext4_inode_blocks_set(handle, raw_inode, ei);
4964 if (err) {
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004965 spin_unlock(&ei->i_raw_lock);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004966 goto out_brelse;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004967 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004968 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Theodore Ts'o353eb832011-01-10 12:18:25 -05004969 raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
Theodore Ts'oed3654e2014-03-24 14:09:06 -04004970 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT)))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004971 raw_inode->i_file_acl_high =
4972 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004973 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Artem Blagodarenkoe08ac992017-06-21 21:09:57 -04004974 if (ei->i_disksize != ext4_isize(inode->i_sb, raw_inode)) {
Jan Karab71fc072012-09-26 21:52:20 -04004975 ext4_isize_set(raw_inode, ei->i_disksize);
4976 need_datasync = 1;
4977 }
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004978 if (ei->i_disksize > 0x7fffffffULL) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004979 if (!ext4_has_feature_large_file(sb) ||
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004980 EXT4_SB(sb)->s_es->s_rev_level ==
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004981 cpu_to_le32(EXT4_GOOD_OLD_REV))
4982 set_large_file = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004983 }
4984 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
4985 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
4986 if (old_valid_dev(inode->i_rdev)) {
4987 raw_inode->i_block[0] =
4988 cpu_to_le32(old_encode_dev(inode->i_rdev));
4989 raw_inode->i_block[1] = 0;
4990 } else {
4991 raw_inode->i_block[0] = 0;
4992 raw_inode->i_block[1] =
4993 cpu_to_le32(new_encode_dev(inode->i_rdev));
4994 raw_inode->i_block[2] = 0;
4995 }
Tao Maf19d5872012-12-10 14:05:51 -05004996 } else if (!ext4_has_inline_data(inode)) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004997 for (block = 0; block < EXT4_N_BLOCKS; block++)
4998 raw_inode->i_block[block] = ei->i_data[block];
Tao Maf19d5872012-12-10 14:05:51 -05004999 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005000
Theodore Ts'oed3654e2014-03-24 14:09:06 -04005001 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
Eryu Guane254d1a2018-05-10 11:55:31 -04005002 u64 ivers = ext4_inode_peek_iversion(inode);
Jeff Laytonee73f9a2018-01-09 08:21:39 -05005003
5004 raw_inode->i_disk_version = cpu_to_le32(ivers);
Theodore Ts'oc4f65702014-03-20 00:32:57 -04005005 if (ei->i_extra_isize) {
5006 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
5007 raw_inode->i_version_hi =
Jeff Laytonee73f9a2018-01-09 08:21:39 -05005008 cpu_to_le32(ivers >> 32);
Theodore Ts'oc4f65702014-03-20 00:32:57 -04005009 raw_inode->i_extra_isize =
5010 cpu_to_le16(ei->i_extra_isize);
5011 }
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005012 }
Li Xi040cb372016-01-08 16:01:21 -05005013
Kaho Ng0b7b7772016-09-05 23:11:58 -04005014 BUG_ON(!ext4_has_feature_project(inode->i_sb) &&
Li Xi040cb372016-01-08 16:01:21 -05005015 i_projid != EXT4_DEF_PROJID);
5016
5017 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
5018 EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
5019 raw_inode->i_projid = cpu_to_le32(i_projid);
5020
Darrick J. Wong814525f2012-04-29 18:31:10 -04005021 ext4_inode_csum_set(inode, raw_inode, ei);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005022 spin_unlock(&ei->i_raw_lock);
Linus Torvalds1751e8a2017-11-27 13:05:09 -08005023 if (inode->i_sb->s_flags & SB_LAZYTIME)
Theodore Ts'oa26f4992015-02-02 00:37:02 -05005024 ext4_update_other_inodes_time(inode->i_sb, inode->i_ino,
5025 bh->b_data);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005026
Frank Mayhar830156c2009-09-29 10:07:47 -04005027 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05005028 rc = ext4_handle_dirty_metadata(handle, NULL, bh);
Frank Mayhar830156c2009-09-29 10:07:47 -04005029 if (!err)
5030 err = rc;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005031 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005032 if (set_large_file) {
liang xie5d601252014-05-12 22:06:43 -04005033 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access");
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005034 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
5035 if (err)
5036 goto out_brelse;
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005037 ext4_set_feature_large_file(sb);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04005038 ext4_handle_sync(handle);
5039 err = ext4_handle_dirty_super(handle, sb);
5040 }
Jan Karab71fc072012-09-26 21:52:20 -04005041 ext4_update_inode_fsync_trans(handle, inode, need_datasync);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005042out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04005043 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07005044 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005045 return err;
5046}
5047
5048/*
Mingming Cao617ba132006-10-11 01:20:53 -07005049 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005050 *
5051 * We are called from a few places:
5052 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005053 * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005054 * Here, there will be no transaction running. We wait for any running
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005055 * transaction to commit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005056 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005057 * - Within flush work (sys_sync(), kupdate and such).
5058 * We wait on commit, if told to.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005059 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005060 * - Within iput_final() -> write_inode_now()
5061 * We wait on commit, if told to.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005062 *
5063 * In all cases it is actually safe for us to return without doing anything,
5064 * because the inode has been copied into a raw inode buffer in
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005065 * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL
5066 * writeback.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005067 *
5068 * Note that we are absolutely dependent upon all inode dirtiers doing the
5069 * right thing: they *must* call mark_inode_dirty() after dirtying info in
5070 * which we are interested.
5071 *
5072 * It would be a bug for them to not do this. The code:
5073 *
5074 * mark_inode_dirty(inode)
5075 * stuff();
5076 * inode->i_size = expr;
5077 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005078 * is in error because write_inode() could occur while `stuff()' is running,
5079 * and the new i_size will be lost. Plus the inode will no longer be on the
5080 * superblock's dirty inode list.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005081 */
Christoph Hellwiga9185b42010-03-05 09:21:37 +01005082int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005083{
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005084 int err;
5085
Theodore Ts'o18f2c4f2018-12-19 14:36:58 -05005086 if (WARN_ON_ONCE(current->flags & PF_MEMALLOC) ||
5087 sb_rdonly(inode->i_sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005088 return 0;
5089
Theodore Ts'o18f2c4f2018-12-19 14:36:58 -05005090 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5091 return -EIO;
5092
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005093 if (EXT4_SB(inode->i_sb)->s_journal) {
5094 if (ext4_journal_current_handle()) {
5095 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
5096 dump_stack();
5097 return -EIO;
5098 }
5099
Jan Kara10542c22014-03-04 10:50:50 -05005100 /*
5101 * No need to force transaction in WB_SYNC_NONE mode. Also
5102 * ext4_sync_fs() will force the commit after everything is
5103 * written.
5104 */
5105 if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync)
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005106 return 0;
5107
Theodore Ts'o18f2c4f2018-12-19 14:36:58 -05005108 err = jbd2_complete_transaction(EXT4_SB(inode->i_sb)->s_journal,
5109 EXT4_I(inode)->i_sync_tid);
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005110 } else {
5111 struct ext4_iloc iloc;
5112
Curt Wohlgemuth8b472d72010-04-03 16:45:06 -04005113 err = __ext4_get_inode_loc(inode, &iloc, 0);
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005114 if (err)
5115 return err;
Jan Kara10542c22014-03-04 10:50:50 -05005116 /*
5117 * sync(2) will flush the whole buffer cache. No need to do
5118 * it here separately for each inode.
5119 */
5120 if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
Frank Mayhar830156c2009-09-29 10:07:47 -04005121 sync_dirty_buffer(iloc.bh);
5122 if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
Theodore Ts'o878520a2019-11-19 21:54:15 -05005123 ext4_set_errno(inode->i_sb, EIO);
Theodore Ts'oc398eda2010-07-27 11:56:40 -04005124 EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr,
5125 "IO error syncing inode");
Frank Mayhar830156c2009-09-29 10:07:47 -04005126 err = -EIO;
5127 }
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04005128 brelse(iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005129 }
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005130 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005131}
5132
5133/*
Jan Kara53e87262012-12-25 13:29:52 -05005134 * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
5135 * buffers that are attached to a page stradding i_size and are undergoing
5136 * commit. In that case we have to wait for commit to finish and try again.
5137 */
5138static void ext4_wait_for_tail_page_commit(struct inode *inode)
5139{
5140 struct page *page;
5141 unsigned offset;
5142 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
5143 tid_t commit_tid = 0;
5144 int ret;
5145
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005146 offset = inode->i_size & (PAGE_SIZE - 1);
Jan Kara53e87262012-12-25 13:29:52 -05005147 /*
yangerkun565333a2019-09-19 14:35:08 +08005148 * If the page is fully truncated, we don't need to wait for any commit
5149 * (and we even should not as __ext4_journalled_invalidatepage() may
5150 * strip all buffers from the page but keep the page dirty which can then
5151 * confuse e.g. concurrent ext4_writepage() seeing dirty page without
5152 * buffers). Also we don't need to wait for any commit if all buffers in
5153 * the page remain valid. This is most beneficial for the common case of
5154 * blocksize == PAGESIZE.
Jan Kara53e87262012-12-25 13:29:52 -05005155 */
yangerkun565333a2019-09-19 14:35:08 +08005156 if (!offset || offset > (PAGE_SIZE - i_blocksize(inode)))
Jan Kara53e87262012-12-25 13:29:52 -05005157 return;
5158 while (1) {
5159 page = find_lock_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005160 inode->i_size >> PAGE_SHIFT);
Jan Kara53e87262012-12-25 13:29:52 -05005161 if (!page)
5162 return;
Lukas Czernerca99fdd2013-05-21 23:25:01 -04005163 ret = __ext4_journalled_invalidatepage(page, offset,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005164 PAGE_SIZE - offset);
Jan Kara53e87262012-12-25 13:29:52 -05005165 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005166 put_page(page);
Jan Kara53e87262012-12-25 13:29:52 -05005167 if (ret != -EBUSY)
5168 return;
5169 commit_tid = 0;
5170 read_lock(&journal->j_state_lock);
5171 if (journal->j_committing_transaction)
5172 commit_tid = journal->j_committing_transaction->t_tid;
5173 read_unlock(&journal->j_state_lock);
5174 if (commit_tid)
5175 jbd2_log_wait_commit(journal, commit_tid);
5176 }
5177}
5178
5179/*
Mingming Cao617ba132006-10-11 01:20:53 -07005180 * ext4_setattr()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005181 *
5182 * Called from notify_change.
5183 *
5184 * We want to trap VFS attempts to truncate the file as soon as
5185 * possible. In particular, we want to make sure that when the VFS
5186 * shrinks i_size, we put the inode on the orphan list and modify
5187 * i_disksize immediately, so that during the subsequent flushing of
5188 * dirty pages and freeing of disk blocks, we can guarantee that any
5189 * commit will leave the blocks being flushed in an unused state on
5190 * disk. (On recovery, the inode will get truncated and the blocks will
5191 * be freed, so we have a strong guarantee that no future commit will
5192 * leave these blocks visible to the user.)
5193 *
Jan Kara678aaf42008-07-11 19:27:31 -04005194 * Another thing we have to assure is that if we are in ordered mode
5195 * and inode is still attached to the committing transaction, we must
5196 * we start writeout of all the dirty pages which are being truncated.
5197 * This way we are sure that all the data written in the previous
5198 * transaction are already on disk (truncate waits for pages under
5199 * writeback).
5200 *
5201 * Called with inode->i_mutex down.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005202 */
Mingming Cao617ba132006-10-11 01:20:53 -07005203int ext4_setattr(struct dentry *dentry, struct iattr *attr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005204{
David Howells2b0143b2015-03-17 22:25:59 +00005205 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005206 int error, rc = 0;
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04005207 int orphan = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005208 const unsigned int ia_valid = attr->ia_valid;
5209
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05005210 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5211 return -EIO;
5212
Theodore Ts'o02b016c2019-06-09 22:04:33 -04005213 if (unlikely(IS_IMMUTABLE(inode)))
5214 return -EPERM;
5215
5216 if (unlikely(IS_APPEND(inode) &&
5217 (ia_valid & (ATTR_MODE | ATTR_UID |
5218 ATTR_GID | ATTR_TIMES_SET))))
5219 return -EPERM;
5220
Jan Kara31051c82016-05-26 16:55:18 +02005221 error = setattr_prepare(dentry, attr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005222 if (error)
5223 return error;
5224
Eric Biggers3ce2b8d2017-10-18 20:21:58 -04005225 error = fscrypt_prepare_setattr(dentry, attr);
5226 if (error)
5227 return error;
5228
Eric Biggersc93d8f82019-07-22 09:26:24 -07005229 error = fsverity_prepare_setattr(dentry, attr);
5230 if (error)
5231 return error;
5232
Jan Karaa7cdade2015-06-29 16:22:54 +02005233 if (is_quota_modification(inode, attr)) {
5234 error = dquot_initialize(inode);
5235 if (error)
5236 return error;
5237 }
Eric W. Biederman08cefc72012-02-07 15:41:49 -08005238 if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
5239 (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005240 handle_t *handle;
5241
5242 /* (user+group)*(old+new) structure, inode write (sb,
5243 * inode block, ? - but truncate inode update has it) */
Theodore Ts'o9924a922013-02-08 21:59:22 -05005244 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
5245 (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) +
5246 EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005247 if (IS_ERR(handle)) {
5248 error = PTR_ERR(handle);
5249 goto err_out;
5250 }
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -04005251
5252 /* dquot_transfer() calls back ext4_get_inode_usage() which
5253 * counts xattr inode references.
5254 */
5255 down_read(&EXT4_I(inode)->xattr_sem);
Christoph Hellwigb43fa822010-03-03 09:05:03 -05005256 error = dquot_transfer(inode, attr);
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -04005257 up_read(&EXT4_I(inode)->xattr_sem);
5258
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005259 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07005260 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005261 return error;
5262 }
5263 /* Update corresponding info in inode so that everything is in
5264 * one transaction */
5265 if (attr->ia_valid & ATTR_UID)
5266 inode->i_uid = attr->ia_uid;
5267 if (attr->ia_valid & ATTR_GID)
5268 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07005269 error = ext4_mark_inode_dirty(handle, inode);
5270 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005271 }
5272
Josef Bacik3da40c72015-06-22 00:31:26 -04005273 if (attr->ia_valid & ATTR_SIZE) {
Jan Kara52083862013-08-17 10:07:17 -04005274 handle_t *handle;
Josef Bacik3da40c72015-06-22 00:31:26 -04005275 loff_t oldsize = inode->i_size;
Jan Karab9c1c262019-05-30 11:56:23 -04005276 int shrink = (attr->ia_size < inode->i_size);
Christoph Hellwig562c72aa52011-06-24 14:29:45 -04005277
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005278 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
Eric Sandeene2b46572008-01-28 23:58:27 -05005279 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5280
Theodore Ts'o0c095c72010-07-27 11:56:06 -04005281 if (attr->ia_size > sbi->s_bitmap_maxbytes)
5282 return -EFBIG;
Eric Sandeene2b46572008-01-28 23:58:27 -05005283 }
Josef Bacik3da40c72015-06-22 00:31:26 -04005284 if (!S_ISREG(inode->i_mode))
5285 return -EINVAL;
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005286
5287 if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size)
5288 inode_inc_iversion(inode);
5289
Jan Karab9c1c262019-05-30 11:56:23 -04005290 if (shrink) {
5291 if (ext4_should_order_data(inode)) {
5292 error = ext4_begin_ordered_truncate(inode,
Jan Kara678aaf42008-07-11 19:27:31 -04005293 attr->ia_size);
Jan Karab9c1c262019-05-30 11:56:23 -04005294 if (error)
5295 goto err_out;
5296 }
5297 /*
5298 * Blocks are going to be removed from the inode. Wait
5299 * for dio in flight.
5300 */
5301 inode_dio_wait(inode);
Josef Bacik3da40c72015-06-22 00:31:26 -04005302 }
Jan Karab9c1c262019-05-30 11:56:23 -04005303
5304 down_write(&EXT4_I(inode)->i_mmap_sem);
5305
5306 rc = ext4_break_layouts(inode);
5307 if (rc) {
5308 up_write(&EXT4_I(inode)->i_mmap_sem);
5309 return rc;
5310 }
5311
Josef Bacik3da40c72015-06-22 00:31:26 -04005312 if (attr->ia_size != inode->i_size) {
Jan Kara52083862013-08-17 10:07:17 -04005313 handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
5314 if (IS_ERR(handle)) {
5315 error = PTR_ERR(handle);
Jan Karab9c1c262019-05-30 11:56:23 -04005316 goto out_mmap_sem;
Jan Kara52083862013-08-17 10:07:17 -04005317 }
Josef Bacik3da40c72015-06-22 00:31:26 -04005318 if (ext4_handle_valid(handle) && shrink) {
Jan Kara52083862013-08-17 10:07:17 -04005319 error = ext4_orphan_add(handle, inode);
5320 orphan = 1;
5321 }
Eryu Guan911af572015-07-28 15:08:41 -04005322 /*
5323 * Update c/mtime on truncate up, ext4_truncate() will
5324 * update c/mtime in shrink case below
5325 */
5326 if (!shrink) {
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05005327 inode->i_mtime = current_time(inode);
Eryu Guan911af572015-07-28 15:08:41 -04005328 inode->i_ctime = inode->i_mtime;
5329 }
Jan Kara90e775b2013-08-17 10:09:31 -04005330 down_write(&EXT4_I(inode)->i_data_sem);
Jan Kara52083862013-08-17 10:07:17 -04005331 EXT4_I(inode)->i_disksize = attr->ia_size;
5332 rc = ext4_mark_inode_dirty(handle, inode);
5333 if (!error)
5334 error = rc;
Jan Kara90e775b2013-08-17 10:09:31 -04005335 /*
5336 * We have to update i_size under i_data_sem together
5337 * with i_disksize to avoid races with writeback code
5338 * running ext4_wb_update_i_disksize().
5339 */
5340 if (!error)
5341 i_size_write(inode, attr->ia_size);
5342 up_write(&EXT4_I(inode)->i_data_sem);
Jan Kara52083862013-08-17 10:07:17 -04005343 ext4_journal_stop(handle);
Jan Karab9c1c262019-05-30 11:56:23 -04005344 if (error)
5345 goto out_mmap_sem;
5346 if (!shrink) {
5347 pagecache_isize_extended(inode, oldsize,
5348 inode->i_size);
5349 } else if (ext4_should_journal_data(inode)) {
5350 ext4_wait_for_tail_page_commit(inode);
Jan Kara678aaf42008-07-11 19:27:31 -04005351 }
Jan Karad6320cb2014-10-01 21:49:46 -04005352 }
Ross Zwisler430657b2018-07-29 17:00:22 -04005353
Jan Kara52083862013-08-17 10:07:17 -04005354 /*
5355 * Truncate pagecache after we've waited for commit
5356 * in data=journal mode to make pages freeable.
5357 */
Ross Zwisler923ae0f2015-02-16 15:59:38 -08005358 truncate_pagecache(inode, inode->i_size);
Jan Karab9c1c262019-05-30 11:56:23 -04005359 /*
5360 * Call ext4_truncate() even if i_size didn't change to
5361 * truncate possible preallocated blocks.
5362 */
5363 if (attr->ia_size <= oldsize) {
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05005364 rc = ext4_truncate(inode);
5365 if (rc)
5366 error = rc;
5367 }
Jan Karab9c1c262019-05-30 11:56:23 -04005368out_mmap_sem:
Jan Karaea3d7202015-12-07 14:28:03 -05005369 up_write(&EXT4_I(inode)->i_mmap_sem);
Theodore Ts'o072bd7e2011-05-23 15:13:02 -04005370 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005371
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05005372 if (!error) {
Christoph Hellwig10257742010-06-04 11:30:02 +02005373 setattr_copy(inode, attr);
5374 mark_inode_dirty(inode);
5375 }
5376
5377 /*
5378 * If the call to ext4_truncate failed to get a transaction handle at
5379 * all, we need to clean up the in-core orphan list manually.
5380 */
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04005381 if (orphan && inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07005382 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005383
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05005384 if (!error && (ia_valid & ATTR_MODE))
Christoph Hellwig64e178a2013-12-20 05:16:44 -08005385 rc = posix_acl_chmod(inode, inode->i_mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005386
5387err_out:
Mingming Cao617ba132006-10-11 01:20:53 -07005388 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005389 if (!error)
5390 error = rc;
5391 return error;
5392}
5393
David Howellsa528d352017-01-31 16:46:22 +00005394int ext4_getattr(const struct path *path, struct kstat *stat,
5395 u32 request_mask, unsigned int query_flags)
Mingming Cao3e3398a2008-07-11 19:27:31 -04005396{
David Howells99652ea2017-03-31 18:31:56 +01005397 struct inode *inode = d_inode(path->dentry);
5398 struct ext4_inode *raw_inode;
5399 struct ext4_inode_info *ei = EXT4_I(inode);
5400 unsigned int flags;
Mingming Cao3e3398a2008-07-11 19:27:31 -04005401
Theodore Ts'od4c5e962019-11-28 22:26:51 -05005402 if ((request_mask & STATX_BTIME) &&
5403 EXT4_FITS_IN_INODE(raw_inode, ei, i_crtime)) {
David Howells99652ea2017-03-31 18:31:56 +01005404 stat->result_mask |= STATX_BTIME;
5405 stat->btime.tv_sec = ei->i_crtime.tv_sec;
5406 stat->btime.tv_nsec = ei->i_crtime.tv_nsec;
5407 }
5408
5409 flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
5410 if (flags & EXT4_APPEND_FL)
5411 stat->attributes |= STATX_ATTR_APPEND;
5412 if (flags & EXT4_COMPR_FL)
5413 stat->attributes |= STATX_ATTR_COMPRESSED;
5414 if (flags & EXT4_ENCRYPT_FL)
5415 stat->attributes |= STATX_ATTR_ENCRYPTED;
5416 if (flags & EXT4_IMMUTABLE_FL)
5417 stat->attributes |= STATX_ATTR_IMMUTABLE;
5418 if (flags & EXT4_NODUMP_FL)
5419 stat->attributes |= STATX_ATTR_NODUMP;
Eric Biggers1f607192019-10-29 13:41:39 -07005420 if (flags & EXT4_VERITY_FL)
5421 stat->attributes |= STATX_ATTR_VERITY;
David Howells99652ea2017-03-31 18:31:56 +01005422
David Howells3209f682017-03-31 18:32:17 +01005423 stat->attributes_mask |= (STATX_ATTR_APPEND |
5424 STATX_ATTR_COMPRESSED |
5425 STATX_ATTR_ENCRYPTED |
5426 STATX_ATTR_IMMUTABLE |
Eric Biggers1f607192019-10-29 13:41:39 -07005427 STATX_ATTR_NODUMP |
5428 STATX_ATTR_VERITY);
David Howells3209f682017-03-31 18:32:17 +01005429
Mingming Cao3e3398a2008-07-11 19:27:31 -04005430 generic_fillattr(inode, stat);
David Howells99652ea2017-03-31 18:31:56 +01005431 return 0;
5432}
5433
5434int ext4_file_getattr(const struct path *path, struct kstat *stat,
5435 u32 request_mask, unsigned int query_flags)
5436{
5437 struct inode *inode = d_inode(path->dentry);
5438 u64 delalloc_blocks;
5439
5440 ext4_getattr(path, stat, request_mask, query_flags);
Mingming Cao3e3398a2008-07-11 19:27:31 -04005441
5442 /*
Andreas Dilger9206c562013-11-11 22:38:12 -05005443 * If there is inline data in the inode, the inode will normally not
5444 * have data blocks allocated (it may have an external xattr block).
5445 * Report at least one sector for such files, so tools like tar, rsync,
Theodore Ts'od67d64f2017-03-25 17:33:31 -04005446 * others don't incorrectly think the file is completely sparse.
Andreas Dilger9206c562013-11-11 22:38:12 -05005447 */
5448 if (unlikely(ext4_has_inline_data(inode)))
5449 stat->blocks += (stat->size + 511) >> 9;
5450
5451 /*
Mingming Cao3e3398a2008-07-11 19:27:31 -04005452 * We can't update i_blocks if the block allocation is delayed
5453 * otherwise in the case of system crash before the real block
5454 * allocation is done, we will have i_blocks inconsistent with
5455 * on-disk file blocks.
5456 * We always keep i_blocks updated together with real
5457 * allocation. But to not confuse with user, stat
5458 * will return the blocks that include the delayed allocation
5459 * blocks for this file.
5460 */
Tao Ma96607552012-05-31 22:54:16 -04005461 delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
Andreas Dilger9206c562013-11-11 22:38:12 -05005462 EXT4_I(inode)->i_reserved_data_blocks);
5463 stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9);
Mingming Cao3e3398a2008-07-11 19:27:31 -04005464 return 0;
5465}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005466
Jan Karafffb2732013-06-04 13:01:11 -04005467static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
5468 int pextents)
Mingming Caoa02908f2008-08-19 22:16:07 -04005469{
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005470 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Jan Karafffb2732013-06-04 13:01:11 -04005471 return ext4_ind_trans_blocks(inode, lblocks);
5472 return ext4_ext_index_trans_blocks(inode, pextents);
Mingming Caoa02908f2008-08-19 22:16:07 -04005473}
Theodore Ts'oac51d832008-11-06 16:49:36 -05005474
Mingming Caoa02908f2008-08-19 22:16:07 -04005475/*
5476 * Account for index blocks, block groups bitmaps and block group
5477 * descriptor blocks if modify datablocks and index blocks
5478 * worse case, the indexs blocks spread over different block groups
5479 *
5480 * If datablocks are discontiguous, they are possible to spread over
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005481 * different block groups too. If they are contiguous, with flexbg,
Mingming Caoa02908f2008-08-19 22:16:07 -04005482 * they could still across block group boundary.
5483 *
5484 * Also account for superblock, inode, quota and xattr blocks
5485 */
Tahsin Erdogandec214d2017-06-22 11:44:55 -04005486static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
Jan Karafffb2732013-06-04 13:01:11 -04005487 int pextents)
Mingming Caoa02908f2008-08-19 22:16:07 -04005488{
Theodore Ts'o8df96752009-05-01 08:50:38 -04005489 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
5490 int gdpblocks;
Mingming Caoa02908f2008-08-19 22:16:07 -04005491 int idxblocks;
5492 int ret = 0;
5493
5494 /*
Jan Karafffb2732013-06-04 13:01:11 -04005495 * How many index blocks need to touch to map @lblocks logical blocks
5496 * to @pextents physical extents?
Mingming Caoa02908f2008-08-19 22:16:07 -04005497 */
Jan Karafffb2732013-06-04 13:01:11 -04005498 idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents);
Mingming Caoa02908f2008-08-19 22:16:07 -04005499
5500 ret = idxblocks;
5501
5502 /*
5503 * Now let's see how many group bitmaps and group descriptors need
5504 * to account
5505 */
Jan Karafffb2732013-06-04 13:01:11 -04005506 groups = idxblocks + pextents;
Mingming Caoa02908f2008-08-19 22:16:07 -04005507 gdpblocks = groups;
Theodore Ts'o8df96752009-05-01 08:50:38 -04005508 if (groups > ngroups)
5509 groups = ngroups;
Mingming Caoa02908f2008-08-19 22:16:07 -04005510 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
5511 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
5512
5513 /* bitmaps and block group descriptor blocks */
5514 ret += groups + gdpblocks;
5515
5516 /* Blocks for super block, inode, quota and xattr blocks */
5517 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005518
5519 return ret;
5520}
5521
5522/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005523 * Calculate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04005524 * the modification of a single pages into a single transaction,
5525 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04005526 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04005527 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04005528 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04005529 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04005530 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04005531 */
5532int ext4_writepage_trans_blocks(struct inode *inode)
5533{
5534 int bpp = ext4_journal_blocks_per_page(inode);
5535 int ret;
5536
Jan Karafffb2732013-06-04 13:01:11 -04005537 ret = ext4_meta_trans_blocks(inode, bpp, bpp);
Mingming Caoa02908f2008-08-19 22:16:07 -04005538
5539 /* Account for data blocks for journalled mode */
5540 if (ext4_should_journal_data(inode))
5541 ret += bpp;
5542 return ret;
5543}
Mingming Caof3bd1f32008-08-19 22:16:03 -04005544
5545/*
5546 * Calculate the journal credits for a chunk of data modification.
5547 *
5548 * This is called from DIO, fallocate or whoever calling
Eric Sandeen79e83032010-07-27 11:56:07 -04005549 * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
Mingming Caof3bd1f32008-08-19 22:16:03 -04005550 *
5551 * journal buffers for data blocks are not included here, as DIO
5552 * and fallocate do no need to journal data buffers.
5553 */
5554int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
5555{
5556 return ext4_meta_trans_blocks(inode, nrblocks, 1);
5557}
5558
Mingming Caoa02908f2008-08-19 22:16:07 -04005559/*
Mingming Cao617ba132006-10-11 01:20:53 -07005560 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005561 * Give this, we know that the caller already has write access to iloc->bh.
5562 */
Mingming Cao617ba132006-10-11 01:20:53 -07005563int ext4_mark_iloc_dirty(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04005564 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005565{
5566 int err = 0;
5567
Vasily Averina6758302018-11-06 16:49:50 -05005568 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) {
5569 put_bh(iloc->bh);
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05005570 return -EIO;
Vasily Averina6758302018-11-06 16:49:50 -05005571 }
Theodore Ts'oc64db502012-03-02 12:23:11 -05005572 if (IS_I_VERSION(inode))
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005573 inode_inc_iversion(inode);
5574
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005575 /* the do_update_inode consumes one bh->b_count */
5576 get_bh(iloc->bh);
5577
Mingming Caodab291a2006-10-11 01:21:01 -07005578 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Frank Mayhar830156c2009-09-29 10:07:47 -04005579 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005580 put_bh(iloc->bh);
5581 return err;
5582}
5583
5584/*
5585 * On success, We end up with an outstanding reference count against
5586 * iloc->bh. This _must_ be cleaned up later.
5587 */
5588
5589int
Mingming Cao617ba132006-10-11 01:20:53 -07005590ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
5591 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005592{
Frank Mayhar03901312009-01-07 00:06:22 -05005593 int err;
5594
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05005595 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
5596 return -EIO;
5597
Frank Mayhar03901312009-01-07 00:06:22 -05005598 err = ext4_get_inode_loc(inode, iloc);
5599 if (!err) {
5600 BUFFER_TRACE(iloc->bh, "get_write_access");
5601 err = ext4_journal_get_write_access(handle, iloc->bh);
5602 if (err) {
5603 brelse(iloc->bh);
5604 iloc->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005605 }
5606 }
Mingming Cao617ba132006-10-11 01:20:53 -07005607 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005608 return err;
5609}
5610
Miao Xiec03b45b2017-08-06 01:00:49 -04005611static int __ext4_expand_extra_isize(struct inode *inode,
5612 unsigned int new_extra_isize,
5613 struct ext4_iloc *iloc,
5614 handle_t *handle, int *no_expand)
5615{
5616 struct ext4_inode *raw_inode;
5617 struct ext4_xattr_ibody_header *header;
Theodore Ts'o4ea99932019-11-07 21:43:41 -05005618 unsigned int inode_size = EXT4_INODE_SIZE(inode->i_sb);
5619 struct ext4_inode_info *ei = EXT4_I(inode);
Miao Xiec03b45b2017-08-06 01:00:49 -04005620 int error;
5621
Theodore Ts'o4ea99932019-11-07 21:43:41 -05005622 /* this was checked at iget time, but double check for good measure */
5623 if ((EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize > inode_size) ||
5624 (ei->i_extra_isize & 3)) {
5625 EXT4_ERROR_INODE(inode, "bad extra_isize %u (inode size %u)",
5626 ei->i_extra_isize,
5627 EXT4_INODE_SIZE(inode->i_sb));
5628 return -EFSCORRUPTED;
5629 }
5630 if ((new_extra_isize < ei->i_extra_isize) ||
5631 (new_extra_isize < 4) ||
5632 (new_extra_isize > inode_size - EXT4_GOOD_OLD_INODE_SIZE))
5633 return -EINVAL; /* Should never happen */
5634
Miao Xiec03b45b2017-08-06 01:00:49 -04005635 raw_inode = ext4_raw_inode(iloc);
5636
5637 header = IHDR(inode, raw_inode);
5638
5639 /* No extended attributes present */
5640 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
5641 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
5642 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE +
5643 EXT4_I(inode)->i_extra_isize, 0,
5644 new_extra_isize - EXT4_I(inode)->i_extra_isize);
5645 EXT4_I(inode)->i_extra_isize = new_extra_isize;
5646 return 0;
5647 }
5648
5649 /* try to expand with EAs present */
5650 error = ext4_expand_extra_isize_ea(inode, new_extra_isize,
5651 raw_inode, handle);
5652 if (error) {
5653 /*
5654 * Inode size expansion failed; don't try again
5655 */
5656 *no_expand = 1;
5657 }
5658
5659 return error;
5660}
5661
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005662/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005663 * Expand an inode by new_extra_isize bytes.
5664 * Returns 0 on success or negative error number on failure.
5665 */
Miao Xiecf0a5e82017-08-06 00:40:01 -04005666static int ext4_try_to_expand_extra_isize(struct inode *inode,
5667 unsigned int new_extra_isize,
5668 struct ext4_iloc iloc,
5669 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005670{
Miao Xie3b10fdc2017-08-06 00:27:38 -04005671 int no_expand;
5672 int error;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005673
Miao Xiecf0a5e82017-08-06 00:40:01 -04005674 if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND))
5675 return -EOVERFLOW;
5676
5677 /*
5678 * In nojournal mode, we can immediately attempt to expand
5679 * the inode. When journaled, we first need to obtain extra
5680 * buffer credits since we may write into the EA block
5681 * with this same handle. If journal_extend fails, then it will
5682 * only result in a minor loss of functionality for that inode.
5683 * If this is felt to be critical, then e2fsck should be run to
5684 * force a large enough s_min_extra_isize.
5685 */
Jan Kara6cb367c2019-11-05 17:44:14 +01005686 if (ext4_journal_extend(handle,
Jan Kara83448bd2019-11-05 17:44:29 +01005687 EXT4_DATA_TRANS_BLOCKS(inode->i_sb), 0) != 0)
Miao Xiecf0a5e82017-08-06 00:40:01 -04005688 return -ENOSPC;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005689
Miao Xie3b10fdc2017-08-06 00:27:38 -04005690 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
Miao Xiecf0a5e82017-08-06 00:40:01 -04005691 return -EBUSY;
Miao Xie3b10fdc2017-08-06 00:27:38 -04005692
Miao Xiec03b45b2017-08-06 01:00:49 -04005693 error = __ext4_expand_extra_isize(inode, new_extra_isize, &iloc,
5694 handle, &no_expand);
Miao Xie3b10fdc2017-08-06 00:27:38 -04005695 ext4_write_unlock_xattr(inode, &no_expand);
Miao Xiecf0a5e82017-08-06 00:40:01 -04005696
Miao Xie3b10fdc2017-08-06 00:27:38 -04005697 return error;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005698}
5699
Miao Xiec03b45b2017-08-06 01:00:49 -04005700int ext4_expand_extra_isize(struct inode *inode,
5701 unsigned int new_extra_isize,
5702 struct ext4_iloc *iloc)
5703{
5704 handle_t *handle;
5705 int no_expand;
5706 int error, rc;
5707
5708 if (ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
5709 brelse(iloc->bh);
5710 return -EOVERFLOW;
5711 }
5712
5713 handle = ext4_journal_start(inode, EXT4_HT_INODE,
5714 EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
5715 if (IS_ERR(handle)) {
5716 error = PTR_ERR(handle);
5717 brelse(iloc->bh);
5718 return error;
5719 }
5720
5721 ext4_write_lock_xattr(inode, &no_expand);
5722
zhangyi (F)ddccb6d2019-02-21 11:29:10 -05005723 BUFFER_TRACE(iloc->bh, "get_write_access");
Miao Xiec03b45b2017-08-06 01:00:49 -04005724 error = ext4_journal_get_write_access(handle, iloc->bh);
5725 if (error) {
5726 brelse(iloc->bh);
Dan Carpenter7f420d642019-12-13 21:50:11 +03005727 goto out_unlock;
Miao Xiec03b45b2017-08-06 01:00:49 -04005728 }
5729
5730 error = __ext4_expand_extra_isize(inode, new_extra_isize, iloc,
5731 handle, &no_expand);
5732
5733 rc = ext4_mark_iloc_dirty(handle, inode, iloc);
5734 if (!error)
5735 error = rc;
5736
Dan Carpenter7f420d642019-12-13 21:50:11 +03005737out_unlock:
Miao Xiec03b45b2017-08-06 01:00:49 -04005738 ext4_write_unlock_xattr(inode, &no_expand);
Miao Xiec03b45b2017-08-06 01:00:49 -04005739 ext4_journal_stop(handle);
5740 return error;
5741}
5742
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005743/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005744 * What we do here is to mark the in-core inode as clean with respect to inode
5745 * dirtiness (it may still be data-dirty).
5746 * This means that the in-core inode may be reaped by prune_icache
5747 * without having to perform any I/O. This is a very good thing,
5748 * because *any* task may call prune_icache - even ones which
5749 * have a transaction open against a different journal.
5750 *
5751 * Is this cheating? Not really. Sure, we haven't written the
5752 * inode out, but prune_icache isn't a user-visible syncing function.
5753 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5754 * we start and wait on commits.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005755 */
Mingming Cao617ba132006-10-11 01:20:53 -07005756int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005757{
Mingming Cao617ba132006-10-11 01:20:53 -07005758 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005759 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Miao Xiecf0a5e82017-08-06 00:40:01 -04005760 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005761
5762 might_sleep();
Theodore Ts'o7ff9c072010-11-08 13:51:33 -05005763 trace_ext4_mark_inode_dirty(inode, _RET_IP_);
Mingming Cao617ba132006-10-11 01:20:53 -07005764 err = ext4_reserve_inode_write(handle, inode, &iloc);
Eryu Guan5e1021f2016-03-12 21:40:32 -05005765 if (err)
5766 return err;
Miao Xiecf0a5e82017-08-06 00:40:01 -04005767
5768 if (EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize)
5769 ext4_try_to_expand_extra_isize(inode, sbi->s_want_extra_isize,
5770 iloc, handle);
5771
Eryu Guan5e1021f2016-03-12 21:40:32 -05005772 return ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005773}
5774
5775/*
Mingming Cao617ba132006-10-11 01:20:53 -07005776 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005777 *
5778 * We're really interested in the case where a file is being extended.
5779 * i_size has been changed by generic_commit_write() and we thus need
5780 * to include the updated inode in the current transaction.
5781 *
Christoph Hellwig5dd40562010-03-03 09:05:00 -05005782 * Also, dquot_alloc_block() will always dirty the inode when blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005783 * are allocated to the file.
5784 *
5785 * If the inode is marked synchronous, we don't honour that here - doing
5786 * so would cause a commit on atime updates, which we don't bother doing.
5787 * We handle synchronous inodes at the highest possible level.
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05005788 *
5789 * If only the I_DIRTY_TIME flag is set, we can skip everything. If
5790 * I_DIRTY_TIME and I_DIRTY_SYNC is set, the only inode fields we need
5791 * to copy into the on-disk inode structure are the timestamp files.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005792 */
Christoph Hellwigaa385722011-05-27 06:53:02 -04005793void ext4_dirty_inode(struct inode *inode, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005794{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005795 handle_t *handle;
5796
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05005797 if (flags == I_DIRTY_TIME)
5798 return;
Theodore Ts'o9924a922013-02-08 21:59:22 -05005799 handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005800 if (IS_ERR(handle))
5801 goto out;
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005802
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005803 ext4_mark_inode_dirty(handle, inode);
5804
Mingming Cao617ba132006-10-11 01:20:53 -07005805 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005806out:
5807 return;
5808}
5809
Mingming Cao617ba132006-10-11 01:20:53 -07005810int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005811{
5812 journal_t *journal;
5813 handle_t *handle;
5814 int err;
Daeho Jeongc8585c62016-04-25 23:22:35 -04005815 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005816
5817 /*
5818 * We have to be very careful here: changing a data block's
5819 * journaling status dynamically is dangerous. If we write a
5820 * data block to the journal, change the status and then delete
5821 * that block, we risk forgetting to revoke the old log record
5822 * from the journal and so a subsequent replay can corrupt data.
5823 * So, first we make sure that the journal is empty and that
5824 * nobody is changing anything.
5825 */
5826
Mingming Cao617ba132006-10-11 01:20:53 -07005827 journal = EXT4_JOURNAL(inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005828 if (!journal)
5829 return 0;
Dave Hansend6995942007-07-18 08:33:51 -04005830 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005831 return -EROFS;
5832
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04005833 /* Wait for all existing dio workers */
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04005834 inode_dio_wait(inode);
5835
Daeho Jeong4c546592016-04-25 23:21:00 -04005836 /*
5837 * Before flushing the journal and switching inode's aops, we have
5838 * to flush all dirty data the inode has. There can be outstanding
5839 * delayed allocations, there can be unwritten extents created by
5840 * fallocate or buffered writes in dioread_nolock mode covered by
5841 * dirty data which can be converted only after flushing the dirty
5842 * data (and journalled aops don't know how to handle these cases).
5843 */
5844 if (val) {
5845 down_write(&EXT4_I(inode)->i_mmap_sem);
5846 err = filemap_write_and_wait(inode->i_mapping);
5847 if (err < 0) {
5848 up_write(&EXT4_I(inode)->i_mmap_sem);
Daeho Jeong4c546592016-04-25 23:21:00 -04005849 return err;
5850 }
5851 }
5852
Daeho Jeongc8585c62016-04-25 23:22:35 -04005853 percpu_down_write(&sbi->s_journal_flag_rwsem);
Mingming Caodab291a2006-10-11 01:21:01 -07005854 jbd2_journal_lock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005855
5856 /*
5857 * OK, there are no updates running now, and all cached data is
5858 * synced to disk. We are now in a completely consistent state
5859 * which doesn't have anything in the journal, and we know that
5860 * no filesystem updates are running, so it is safe to modify
5861 * the inode's in-core data-journaling state flag now.
5862 */
5863
5864 if (val)
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005865 ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Yongqiang Yang5872dda2011-12-28 13:55:51 -05005866 else {
Jan Kara4f879ca2014-10-30 10:53:17 -04005867 err = jbd2_journal_flush(journal);
5868 if (err < 0) {
5869 jbd2_journal_unlock_updates(journal);
Daeho Jeongc8585c62016-04-25 23:22:35 -04005870 percpu_up_write(&sbi->s_journal_flag_rwsem);
Jan Kara4f879ca2014-10-30 10:53:17 -04005871 return err;
5872 }
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005873 ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Yongqiang Yang5872dda2011-12-28 13:55:51 -05005874 }
Mingming Cao617ba132006-10-11 01:20:53 -07005875 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005876
Mingming Caodab291a2006-10-11 01:21:01 -07005877 jbd2_journal_unlock_updates(journal);
Daeho Jeongc8585c62016-04-25 23:22:35 -04005878 percpu_up_write(&sbi->s_journal_flag_rwsem);
5879
Daeho Jeong4c546592016-04-25 23:21:00 -04005880 if (val)
5881 up_write(&EXT4_I(inode)->i_mmap_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005882
5883 /* Finally we can mark the inode as dirty. */
5884
Theodore Ts'o9924a922013-02-08 21:59:22 -05005885 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005886 if (IS_ERR(handle))
5887 return PTR_ERR(handle);
5888
Mingming Cao617ba132006-10-11 01:20:53 -07005889 err = ext4_mark_inode_dirty(handle, inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005890 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005891 ext4_journal_stop(handle);
5892 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005893
5894 return err;
5895}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005896
5897static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
5898{
5899 return !buffer_mapped(bh);
5900}
5901
Souptick Joarder401b25a2018-10-02 22:20:50 -04005902vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf)
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005903{
Dave Jiang11bac802017-02-24 14:56:41 -08005904 struct vm_area_struct *vma = vmf->vma;
Nick Pigginc2ec1752009-03-31 15:23:21 -07005905 struct page *page = vmf->page;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005906 loff_t size;
5907 unsigned long len;
Souptick Joarder401b25a2018-10-02 22:20:50 -04005908 int err;
5909 vm_fault_t ret;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005910 struct file *file = vma->vm_file;
Al Viro496ad9a2013-01-23 17:07:38 -05005911 struct inode *inode = file_inode(file);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005912 struct address_space *mapping = inode->i_mapping;
Jan Kara9ea7df52011-06-24 14:29:41 -04005913 handle_t *handle;
5914 get_block_t *get_block;
5915 int retries = 0;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005916
Theodore Ts'o02b016c2019-06-09 22:04:33 -04005917 if (unlikely(IS_IMMUTABLE(inode)))
5918 return VM_FAULT_SIGBUS;
5919
Jan Kara8e8ad8a2012-06-12 16:20:38 +02005920 sb_start_pagefault(inode->i_sb);
Theodore Ts'o041bbb6d2012-09-30 23:04:56 -04005921 file_update_time(vma->vm_file);
Jan Karaea3d7202015-12-07 14:28:03 -05005922
5923 down_read(&EXT4_I(inode)->i_mmap_sem);
Eric Biggers7b4cc972017-04-30 00:10:50 -04005924
Souptick Joarder401b25a2018-10-02 22:20:50 -04005925 err = ext4_convert_inline_data(inode);
5926 if (err)
Eric Biggers7b4cc972017-04-30 00:10:50 -04005927 goto out_ret;
5928
Jan Kara9ea7df52011-06-24 14:29:41 -04005929 /* Delalloc case is easy... */
5930 if (test_opt(inode->i_sb, DELALLOC) &&
5931 !ext4_should_journal_data(inode) &&
5932 !ext4_nonda_switch(inode->i_sb)) {
5933 do {
Souptick Joarder401b25a2018-10-02 22:20:50 -04005934 err = block_page_mkwrite(vma, vmf,
Jan Kara9ea7df52011-06-24 14:29:41 -04005935 ext4_da_get_block_prep);
Souptick Joarder401b25a2018-10-02 22:20:50 -04005936 } while (err == -ENOSPC &&
Jan Kara9ea7df52011-06-24 14:29:41 -04005937 ext4_should_retry_alloc(inode->i_sb, &retries));
5938 goto out_ret;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005939 }
Darrick J. Wong0e499892011-05-18 13:55:20 -04005940
5941 lock_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04005942 size = i_size_read(inode);
5943 /* Page got truncated from under us? */
5944 if (page->mapping != mapping || page_offset(page) > size) {
5945 unlock_page(page);
5946 ret = VM_FAULT_NOPAGE;
5947 goto out;
Darrick J. Wong0e499892011-05-18 13:55:20 -04005948 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005949
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005950 if (page->index == size >> PAGE_SHIFT)
5951 len = size & ~PAGE_MASK;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005952 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005953 len = PAGE_SIZE;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005954 /*
Jan Kara9ea7df52011-06-24 14:29:41 -04005955 * Return if we have all the buffers mapped. This avoids the need to do
5956 * journal_start/journal_stop which can block and take a long time
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005957 */
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005958 if (page_has_buffers(page)) {
Tao Maf19d5872012-12-10 14:05:51 -05005959 if (!ext4_walk_page_buffers(NULL, page_buffers(page),
5960 0, len, NULL,
5961 ext4_bh_unmapped)) {
Jan Kara9ea7df52011-06-24 14:29:41 -04005962 /* Wait so that we don't change page under IO */
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08005963 wait_for_stable_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04005964 ret = VM_FAULT_LOCKED;
5965 goto out;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005966 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005967 }
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005968 unlock_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04005969 /* OK, we need to fill the hole... */
5970 if (ext4_should_dioread_nolock(inode))
Jan Kara705965b2016-03-08 23:08:10 -05005971 get_block = ext4_get_block_unwritten;
Jan Kara9ea7df52011-06-24 14:29:41 -04005972 else
5973 get_block = ext4_get_block;
5974retry_alloc:
Theodore Ts'o9924a922013-02-08 21:59:22 -05005975 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
5976 ext4_writepage_trans_blocks(inode));
Jan Kara9ea7df52011-06-24 14:29:41 -04005977 if (IS_ERR(handle)) {
Nick Pigginc2ec1752009-03-31 15:23:21 -07005978 ret = VM_FAULT_SIGBUS;
Jan Kara9ea7df52011-06-24 14:29:41 -04005979 goto out;
5980 }
Souptick Joarder401b25a2018-10-02 22:20:50 -04005981 err = block_page_mkwrite(vma, vmf, get_block);
5982 if (!err && ext4_should_journal_data(inode)) {
Tao Maf19d5872012-12-10 14:05:51 -05005983 if (ext4_walk_page_buffers(handle, page_buffers(page), 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005984 PAGE_SIZE, NULL, do_journal_get_write_access)) {
Jan Kara9ea7df52011-06-24 14:29:41 -04005985 unlock_page(page);
5986 ret = VM_FAULT_SIGBUS;
Yongqiang Yangfcbb5512011-10-26 05:00:19 -04005987 ext4_journal_stop(handle);
Jan Kara9ea7df52011-06-24 14:29:41 -04005988 goto out;
5989 }
5990 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
5991 }
5992 ext4_journal_stop(handle);
Souptick Joarder401b25a2018-10-02 22:20:50 -04005993 if (err == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
Jan Kara9ea7df52011-06-24 14:29:41 -04005994 goto retry_alloc;
5995out_ret:
Souptick Joarder401b25a2018-10-02 22:20:50 -04005996 ret = block_page_mkwrite_return(err);
Jan Kara9ea7df52011-06-24 14:29:41 -04005997out:
Jan Karaea3d7202015-12-07 14:28:03 -05005998 up_read(&EXT4_I(inode)->i_mmap_sem);
Jan Kara8e8ad8a2012-06-12 16:20:38 +02005999 sb_end_pagefault(inode->i_sb);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04006000 return ret;
6001}
Jan Karaea3d7202015-12-07 14:28:03 -05006002
Souptick Joarder401b25a2018-10-02 22:20:50 -04006003vm_fault_t ext4_filemap_fault(struct vm_fault *vmf)
Jan Karaea3d7202015-12-07 14:28:03 -05006004{
Dave Jiang11bac802017-02-24 14:56:41 -08006005 struct inode *inode = file_inode(vmf->vma->vm_file);
Souptick Joarder401b25a2018-10-02 22:20:50 -04006006 vm_fault_t ret;
Jan Karaea3d7202015-12-07 14:28:03 -05006007
6008 down_read(&EXT4_I(inode)->i_mmap_sem);
Souptick Joarder401b25a2018-10-02 22:20:50 -04006009 ret = filemap_fault(vmf);
Jan Karaea3d7202015-12-07 14:28:03 -05006010 up_read(&EXT4_I(inode)->i_mmap_sem);
6011
Souptick Joarder401b25a2018-10-02 22:20:50 -04006012 return ret;
Jan Karaea3d7202015-12-07 14:28:03 -05006013}