blob: 1db9080026f5b9ad07bf06c7f2da3e3df5bdfb9e [file] [log] [blame]
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001/*
Mingming Cao617ba132006-10-11 01:20:53 -07002 * linux/fs/ext4/inode.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003 *
4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 *
9 * from
10 *
11 * linux/fs/minix/inode.c
12 *
13 * Copyright (C) 1991, 1992 Linus Torvalds
14 *
Dave Kleikampac27a0e2006-10-11 01:20:50 -070015 * 64-bit file support on 64-bit platforms by Jakub Jelinek
16 * (jj@sunsite.ms.mff.cuni.cz)
17 *
Mingming Cao617ba132006-10-11 01:20:53 -070018 * Assorted race fixes, rewrite of ext4_get_block() by Al Viro, 2000
Dave Kleikampac27a0e2006-10-11 01:20:50 -070019 */
20
Dave Kleikampac27a0e2006-10-11 01:20:50 -070021#include <linux/fs.h>
22#include <linux/time.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070023#include <linux/highuid.h>
24#include <linux/pagemap.h>
Matthew Wilcoxc94c2ac2015-09-08 14:58:40 -070025#include <linux/dax.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070026#include <linux/quotaops.h>
27#include <linux/string.h>
28#include <linux/buffer_head.h>
29#include <linux/writeback.h>
Alex Tomas64769242008-07-11 19:27:31 -040030#include <linux/pagevec.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070031#include <linux/mpage.h>
Duane Griffine83c1392008-12-19 20:47:15 +000032#include <linux/namei.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070033#include <linux/uio.h>
34#include <linux/bio.h>
Mingming Cao4c0425f2009-09-28 15:48:41 -040035#include <linux/workqueue.h>
Jiaying Zhang744692d2010-03-04 16:14:02 -050036#include <linux/kernel.h>
Andrew Morton6db26ff2011-01-12 16:59:13 -080037#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Theodore Ts'o00a1a052014-03-30 10:20:01 -040039#include <linux/bitops.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040040
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040041#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070042#include "xattr.h"
43#include "acl.h"
Theodore Ts'o9f125d62011-06-27 19:16:04 -040044#include "truncate.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070045
Theodore Ts'o9bffad12009-06-17 11:48:11 -040046#include <trace/events/ext4.h>
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -080047#include <trace/events/android_fs.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040048
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -040049#define MPAGE_DA_EXTENT_TAIL 0x01
50
Darrick J. Wong814525f2012-04-29 18:31:10 -040051static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw,
52 struct ext4_inode_info *ei)
53{
54 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Darrick J. Wong814525f2012-04-29 18:31:10 -040055 __u32 csum;
Daeho Jeongb47820e2016-07-03 17:51:39 -040056 __u16 dummy_csum = 0;
57 int offset = offsetof(struct ext4_inode, i_checksum_lo);
58 unsigned int csum_size = sizeof(dummy_csum);
Darrick J. Wong814525f2012-04-29 18:31:10 -040059
Daeho Jeongb47820e2016-07-03 17:51:39 -040060 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw, offset);
61 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, csum_size);
62 offset += csum_size;
63 csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset,
64 EXT4_GOOD_OLD_INODE_SIZE - offset);
65
66 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
67 offset = offsetof(struct ext4_inode, i_checksum_hi);
68 csum = ext4_chksum(sbi, csum, (__u8 *)raw +
69 EXT4_GOOD_OLD_INODE_SIZE,
70 offset - EXT4_GOOD_OLD_INODE_SIZE);
71 if (EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) {
72 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum,
73 csum_size);
74 offset += csum_size;
75 csum = ext4_chksum(sbi, csum, (__u8 *)raw + offset,
76 EXT4_INODE_SIZE(inode->i_sb) -
77 offset);
78 }
Darrick J. Wong814525f2012-04-29 18:31:10 -040079 }
80
Darrick J. Wong814525f2012-04-29 18:31:10 -040081 return csum;
82}
83
84static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw,
85 struct ext4_inode_info *ei)
86{
87 __u32 provided, calculated;
88
89 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
90 cpu_to_le32(EXT4_OS_LINUX) ||
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -040091 !ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong814525f2012-04-29 18:31:10 -040092 return 1;
93
94 provided = le16_to_cpu(raw->i_checksum_lo);
95 calculated = ext4_inode_csum(inode, raw, ei);
96 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
97 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
98 provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16;
99 else
100 calculated &= 0xFFFF;
101
102 return provided == calculated;
103}
104
105static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
106 struct ext4_inode_info *ei)
107{
108 __u32 csum;
109
110 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
111 cpu_to_le32(EXT4_OS_LINUX) ||
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400112 !ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong814525f2012-04-29 18:31:10 -0400113 return;
114
115 csum = ext4_inode_csum(inode, raw, ei);
116 raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF);
117 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
118 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
119 raw->i_checksum_hi = cpu_to_le16(csum >> 16);
120}
121
Jan Kara678aaf42008-07-11 19:27:31 -0400122static inline int ext4_begin_ordered_truncate(struct inode *inode,
123 loff_t new_size)
124{
Theodore Ts'o7ff9c072010-11-08 13:51:33 -0500125 trace_ext4_begin_ordered_truncate(inode, new_size);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -0500126 /*
127 * If jinode is zero, then we never opened the file for
128 * writing, so there's no need to call
129 * jbd2_journal_begin_ordered_truncate() since there's no
130 * outstanding writes we need to flush.
131 */
132 if (!EXT4_I(inode)->jinode)
133 return 0;
134 return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
135 EXT4_I(inode)->jinode,
136 new_size);
Jan Kara678aaf42008-07-11 19:27:31 -0400137}
138
Lukas Czernerd47992f2013-05-21 23:17:23 -0400139static void ext4_invalidatepage(struct page *page, unsigned int offset,
140 unsigned int length);
Theodore Ts'ocb20d512010-10-27 21:30:09 -0400141static int __ext4_journalled_writepage(struct page *page, unsigned int len);
142static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
Jan Karafffb2732013-06-04 13:01:11 -0400143static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
144 int pextents);
Alex Tomas64769242008-07-11 19:27:31 -0400145
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700146/*
147 * Test whether an inode is a fast symlink.
148 */
Theodore Ts'of348c252015-04-16 01:55:00 -0400149int ext4_inode_is_fast_symlink(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700150{
Yongqiang Yang65eddb52014-01-06 14:06:18 -0500151 int ea_blocks = EXT4_I(inode)->i_file_acl ?
152 EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700153
Zheng Liubd9db172014-06-02 10:48:22 -0400154 if (ext4_has_inline_data(inode))
155 return 0;
156
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700157 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
158}
159
160/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700161 * Restart the transaction associated with *handle. This does a commit,
162 * so before we call here everything must be consistently dirtied against
163 * this transaction.
164 */
Aneesh Kumar K.Vfa5d1112009-11-02 18:50:49 -0500165int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode,
Jan Kara487caee2009-08-17 22:17:20 -0400166 int nblocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700167{
Jan Kara487caee2009-08-17 22:17:20 -0400168 int ret;
169
170 /*
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400171 * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
Jan Kara487caee2009-08-17 22:17:20 -0400172 * moment, get_block can be called only for blocks inside i_size since
173 * page cache has been already dropped and writes are blocked by
174 * i_mutex. So we can safely drop the i_data_sem here.
175 */
Frank Mayhar03901312009-01-07 00:06:22 -0500176 BUG_ON(EXT4_JOURNAL(inode) == NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700177 jbd_debug(2, "restarting handle %p\n", handle);
Jan Kara487caee2009-08-17 22:17:20 -0400178 up_write(&EXT4_I(inode)->i_data_sem);
Amir Goldstein8e8eaab2011-02-27 23:32:12 -0500179 ret = ext4_journal_restart(handle, nblocks);
Jan Kara487caee2009-08-17 22:17:20 -0400180 down_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vfa5d1112009-11-02 18:50:49 -0500181 ext4_discard_preallocations(inode);
Jan Kara487caee2009-08-17 22:17:20 -0400182
183 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700184}
185
186/*
187 * Called at the last iput() if i_nlink is zero.
188 */
Al Viro0930fcc2010-06-07 13:16:22 -0400189void ext4_evict_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700190{
191 handle_t *handle;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400192 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700193
Theodore Ts'o7ff9c072010-11-08 13:51:33 -0500194 trace_ext4_evict_inode(inode);
Jiaying Zhang2581fdc2011-08-13 12:17:13 -0400195
Al Viro0930fcc2010-06-07 13:16:22 -0400196 if (inode->i_nlink) {
Jan Kara2d859db2011-07-26 09:07:11 -0400197 /*
198 * When journalling data dirty buffers are tracked only in the
199 * journal. So although mm thinks everything is clean and
200 * ready for reaping the inode might still have some pages to
201 * write in the running transaction or waiting to be
202 * checkpointed. Thus calling jbd2_journal_invalidatepage()
203 * (via truncate_inode_pages()) to discard these buffers can
204 * cause data loss. Also even if we did not discard these
205 * buffers, we would have no way to find them after the inode
206 * is reaped and thus user could see stale data if he tries to
207 * read them before the transaction is checkpointed. So be
208 * careful and force everything to disk here... We use
209 * ei->i_datasync_tid to store the newest transaction
210 * containing inode's data.
211 *
212 * Note that directories do not have this problem because they
213 * don't use page cache.
214 */
Vegard Nossum6a7fd522016-07-04 11:03:00 -0400215 if (inode->i_ino != EXT4_JOURNAL_INO &&
216 ext4_should_journal_data(inode) &&
217 (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode))) {
Jan Kara2d859db2011-07-26 09:07:11 -0400218 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
219 tid_t commit_tid = EXT4_I(inode)->i_datasync_tid;
220
Theodore Ts'od76a3a772013-04-03 22:02:52 -0400221 jbd2_complete_transaction(journal, commit_tid);
Jan Kara2d859db2011-07-26 09:07:11 -0400222 filemap_write_and_wait(&inode->i_data);
223 }
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700224 truncate_inode_pages_final(&inode->i_data);
Jan Kara5dc23bd2013-06-04 14:46:12 -0400225
Al Viro0930fcc2010-06-07 13:16:22 -0400226 goto no_delete;
227 }
228
Theodore Ts'oe2bfb082014-10-05 22:47:07 -0400229 if (is_bad_inode(inode))
230 goto no_delete;
231 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500232
Jan Kara678aaf42008-07-11 19:27:31 -0400233 if (ext4_should_order_data(inode))
234 ext4_begin_ordered_truncate(inode, 0);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700235 truncate_inode_pages_final(&inode->i_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700236
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200237 /*
238 * Protect us against freezing - iput() caller didn't have to have any
239 * protection against it
240 */
241 sb_start_intwrite(inode->i_sb);
Theodore Ts'o9924a922013-02-08 21:59:22 -0500242 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
243 ext4_blocks_for_truncate(inode)+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700244 if (IS_ERR(handle)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400245 ext4_std_error(inode->i_sb, PTR_ERR(handle));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700246 /*
247 * If we're going to skip the normal cleanup, we still need to
248 * make sure that the in-core orphan linked list is properly
249 * cleaned up.
250 */
Mingming Cao617ba132006-10-11 01:20:53 -0700251 ext4_orphan_del(NULL, inode);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200252 sb_end_intwrite(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700253 goto no_delete;
254 }
255
256 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500257 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700258 inode->i_size = 0;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400259 err = ext4_mark_inode_dirty(handle, inode);
260 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500261 ext4_warning(inode->i_sb,
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400262 "couldn't mark inode dirty (err %d)", err);
263 goto stop_handle;
264 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700265 if (inode->i_blocks)
Mingming Cao617ba132006-10-11 01:20:53 -0700266 ext4_truncate(inode);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400267
268 /*
269 * ext4_ext_truncate() doesn't reserve any slop when it
270 * restarts journal transactions; therefore there may not be
271 * enough credits left in the handle to remove the inode from
272 * the orphan list and set the dtime field.
273 */
Frank Mayhar03901312009-01-07 00:06:22 -0500274 if (!ext4_handle_has_enough_credits(handle, 3)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400275 err = ext4_journal_extend(handle, 3);
276 if (err > 0)
277 err = ext4_journal_restart(handle, 3);
278 if (err != 0) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500279 ext4_warning(inode->i_sb,
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400280 "couldn't extend journal (err %d)", err);
281 stop_handle:
282 ext4_journal_stop(handle);
Theodore Ts'o45388212010-07-29 15:06:10 -0400283 ext4_orphan_del(NULL, inode);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200284 sb_end_intwrite(inode->i_sb);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400285 goto no_delete;
286 }
287 }
288
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700289 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700290 * Kill off the orphan record which ext4_truncate created.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700291 * AKPM: I think this can be inside the above `if'.
Mingming Cao617ba132006-10-11 01:20:53 -0700292 * Note that ext4_orphan_del() has to be able to cope with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700293 * deletion of a non-existent orphan - this is because we don't
Mingming Cao617ba132006-10-11 01:20:53 -0700294 * know if ext4_truncate() actually created an orphan record.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700295 * (Well, we could do this if we need to, but heck - it works)
296 */
Mingming Cao617ba132006-10-11 01:20:53 -0700297 ext4_orphan_del(handle, inode);
298 EXT4_I(inode)->i_dtime = get_seconds();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700299
300 /*
301 * One subtle ordering requirement: if anything has gone wrong
302 * (transaction abort, IO errors, whatever), then we can still
303 * do these next steps (the fs will already have been marked as
304 * having errors), but we can't free the inode if the mark_dirty
305 * fails.
306 */
Mingming Cao617ba132006-10-11 01:20:53 -0700307 if (ext4_mark_inode_dirty(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700308 /* If that failed, just do the required in-core inode clear. */
Al Viro0930fcc2010-06-07 13:16:22 -0400309 ext4_clear_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700310 else
Mingming Cao617ba132006-10-11 01:20:53 -0700311 ext4_free_inode(handle, inode);
312 ext4_journal_stop(handle);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200313 sb_end_intwrite(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700314 return;
315no_delete:
Al Viro0930fcc2010-06-07 13:16:22 -0400316 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700317}
318
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300319#ifdef CONFIG_QUOTA
320qsize_t *ext4_get_reserved_space(struct inode *inode)
Mingming Cao60e58e02009-01-22 18:13:05 +0100321{
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300322 return &EXT4_I(inode)->i_reserved_quota;
Mingming Cao60e58e02009-01-22 18:13:05 +0100323}
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300324#endif
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500325
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400326/*
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500327 * Called with i_data_sem down, which is important since we can call
328 * ext4_discard_preallocations() from here.
329 */
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500330void ext4_da_update_reserve_space(struct inode *inode,
331 int used, int quota_claim)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400332{
333 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500334 struct ext4_inode_info *ei = EXT4_I(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400335
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500336 spin_lock(&ei->i_block_reservation_lock);
Aditya Kalid8990242011-09-09 19:18:51 -0400337 trace_ext4_da_update_reserve_space(inode, used, quota_claim);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500338 if (unlikely(used > ei->i_reserved_data_blocks)) {
Theodore Ts'o8de5c322013-02-14 15:11:41 -0500339 ext4_warning(inode->i_sb, "%s: ino %lu, used %d "
Theodore Ts'o1084f252012-03-19 23:13:43 -0400340 "with only %d reserved data blocks",
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500341 __func__, inode->i_ino, used,
342 ei->i_reserved_data_blocks);
343 WARN_ON(1);
344 used = ei->i_reserved_data_blocks;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400345 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400346
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500347 /* Update per-inode reservations */
348 ei->i_reserved_data_blocks -= used;
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -0400349 percpu_counter_sub(&sbi->s_dirtyclusters_counter, used);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500350
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400351 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +0100352
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400353 /* Update quota subsystem for data blocks */
354 if (quota_claim)
Aditya Kali7b415bf2011-09-09 19:04:51 -0400355 dquot_claim_block(inode, EXT4_C2B(sbi, used));
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400356 else {
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500357 /*
358 * We did fallocate with an offset that is already delayed
359 * allocated. So on delayed allocated writeback we should
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400360 * not re-claim the quota for fallocated blocks.
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500361 */
Aditya Kali7b415bf2011-09-09 19:04:51 -0400362 dquot_release_reservation_block(inode, EXT4_C2B(sbi, used));
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500363 }
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -0400364
365 /*
366 * If we have done all the pending block allocations and if
367 * there aren't any writers on the inode, we can discard the
368 * inode's preallocations.
369 */
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500370 if ((ei->i_reserved_data_blocks == 0) &&
371 (atomic_read(&inode->i_writecount) == 0))
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -0400372 ext4_discard_preallocations(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400373}
374
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400375static int __check_block_validity(struct inode *inode, const char *func,
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400376 unsigned int line,
377 struct ext4_map_blocks *map)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400378{
Theodore Ts'o24676da2010-05-16 21:00:00 -0400379 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
380 map->m_len)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400381 ext4_error_inode(inode, func, line, map->m_pblk,
382 "lblock %lu mapped to illegal pblock "
383 "(length %d)", (unsigned long) map->m_lblk,
384 map->m_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400385 return -EFSCORRUPTED;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400386 }
387 return 0;
388}
389
Jan Kara53085fa2015-12-07 15:09:35 -0500390int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk,
391 ext4_lblk_t len)
392{
393 int ret;
394
395 if (ext4_encrypted_inode(inode))
Jaegeuk Kima7550b32016-07-10 14:01:03 -0400396 return fscrypt_zeroout_range(inode, lblk, pblk, len);
Jan Kara53085fa2015-12-07 15:09:35 -0500397
398 ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS);
399 if (ret > 0)
400 ret = 0;
401
402 return ret;
403}
404
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400405#define check_block_validity(inode, map) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400406 __check_block_validity((inode), __func__, __LINE__, (map))
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400407
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400408#ifdef ES_AGGRESSIVE_TEST
409static void ext4_map_blocks_es_recheck(handle_t *handle,
410 struct inode *inode,
411 struct ext4_map_blocks *es_map,
412 struct ext4_map_blocks *map,
413 int flags)
414{
415 int retval;
416
417 map->m_flags = 0;
418 /*
419 * There is a race window that the result is not the same.
420 * e.g. xfstests #223 when dioread_nolock enables. The reason
421 * is that we lookup a block mapping in extent status tree with
422 * out taking i_data_sem. So at the time the unwritten extent
423 * could be converted.
424 */
Jan Kara2dcba472015-12-07 15:04:57 -0500425 down_read(&EXT4_I(inode)->i_data_sem);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400426 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
427 retval = ext4_ext_map_blocks(handle, inode, map, flags &
428 EXT4_GET_BLOCKS_KEEP_SIZE);
429 } else {
430 retval = ext4_ind_map_blocks(handle, inode, map, flags &
431 EXT4_GET_BLOCKS_KEEP_SIZE);
432 }
Jan Kara2dcba472015-12-07 15:04:57 -0500433 up_read((&EXT4_I(inode)->i_data_sem));
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400434
435 /*
436 * We don't check m_len because extent will be collpased in status
437 * tree. So the m_len might not equal.
438 */
439 if (es_map->m_lblk != map->m_lblk ||
440 es_map->m_flags != map->m_flags ||
441 es_map->m_pblk != map->m_pblk) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400442 printk("ES cache assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400443 "es_cached ex [%d/%d/%llu/%x] != "
444 "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
445 inode->i_ino, es_map->m_lblk, es_map->m_len,
446 es_map->m_pblk, es_map->m_flags, map->m_lblk,
447 map->m_len, map->m_pblk, map->m_flags,
448 retval, flags);
449 }
450}
451#endif /* ES_AGGRESSIVE_TEST */
452
Theodore Ts'o55138e02009-09-29 13:31:31 -0400453/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400454 * The ext4_map_blocks() function tries to look up the requested blocks,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400455 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500456 *
Mingming Caof5ab0d12008-02-25 15:29:55 -0500457 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
458 * and store the allocated blocks in the result buffer head and mark it
459 * mapped.
460 *
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400461 * If file type is extents based, it will call ext4_ext_map_blocks(),
462 * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
Mingming Caof5ab0d12008-02-25 15:29:55 -0500463 * based files
464 *
Jan Karafacab4d2016-03-09 22:54:00 -0500465 * On success, it returns the number of blocks being mapped or allocated. if
466 * create==0 and the blocks are pre-allocated and unwritten, the resulting @map
467 * is marked as unwritten. If the create == 1, it will mark @map as mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500468 *
469 * It returns 0 if plain look up failed (blocks have not been allocated), in
Jan Karafacab4d2016-03-09 22:54:00 -0500470 * that case, @map is returned as unmapped but we still do fill map->m_len to
471 * indicate the length of a hole starting at map->m_lblk.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500472 *
473 * It returns the error in case of allocation failure.
474 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400475int ext4_map_blocks(handle_t *handle, struct inode *inode,
476 struct ext4_map_blocks *map, int flags)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500477{
Zheng Liud100eef2013-02-18 00:29:59 -0500478 struct extent_status es;
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500479 int retval;
Lukas Czernerb8a86842014-03-18 18:05:35 -0400480 int ret = 0;
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400481#ifdef ES_AGGRESSIVE_TEST
482 struct ext4_map_blocks orig_map;
483
484 memcpy(&orig_map, map, sizeof(*map));
485#endif
Mingming Caof5ab0d12008-02-25 15:29:55 -0500486
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400487 map->m_flags = 0;
488 ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
489 "logical block %lu\n", inode->i_ino, flags, map->m_len,
490 (unsigned long) map->m_lblk);
Zheng Liud100eef2013-02-18 00:29:59 -0500491
Theodore Ts'oe861b5e2014-02-20 12:54:05 -0500492 /*
493 * ext4_map_blocks returns an int, and m_len is an unsigned int
494 */
495 if (unlikely(map->m_len > INT_MAX))
496 map->m_len = INT_MAX;
497
Kazuya Mio4adb6ab2014-04-07 10:53:28 -0400498 /* We can handle the block number less than EXT_MAX_BLOCKS */
499 if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400500 return -EFSCORRUPTED;
Kazuya Mio4adb6ab2014-04-07 10:53:28 -0400501
Zheng Liud100eef2013-02-18 00:29:59 -0500502 /* Lookup extent status tree firstly */
503 if (ext4_es_lookup_extent(inode, map->m_lblk, &es)) {
504 if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
505 map->m_pblk = ext4_es_pblock(&es) +
506 map->m_lblk - es.es_lblk;
507 map->m_flags |= ext4_es_is_written(&es) ?
508 EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN;
509 retval = es.es_len - (map->m_lblk - es.es_lblk);
510 if (retval > map->m_len)
511 retval = map->m_len;
512 map->m_len = retval;
513 } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) {
Jan Karafacab4d2016-03-09 22:54:00 -0500514 map->m_pblk = 0;
515 retval = es.es_len - (map->m_lblk - es.es_lblk);
516 if (retval > map->m_len)
517 retval = map->m_len;
518 map->m_len = retval;
Zheng Liud100eef2013-02-18 00:29:59 -0500519 retval = 0;
520 } else {
521 BUG_ON(1);
522 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400523#ifdef ES_AGGRESSIVE_TEST
524 ext4_map_blocks_es_recheck(handle, inode, map,
525 &orig_map, flags);
526#endif
Zheng Liud100eef2013-02-18 00:29:59 -0500527 goto found;
528 }
529
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500530 /*
Theodore Ts'ob920c752009-05-14 00:54:29 -0400531 * Try to see if we can get the block without requesting a new
532 * file system block.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500533 */
Jan Kara2dcba472015-12-07 15:04:57 -0500534 down_read(&EXT4_I(inode)->i_data_sem);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400535 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Dmitry Monakhova4e5d882011-10-25 08:15:12 -0400536 retval = ext4_ext_map_blocks(handle, inode, map, flags &
537 EXT4_GET_BLOCKS_KEEP_SIZE);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500538 } else {
Dmitry Monakhova4e5d882011-10-25 08:15:12 -0400539 retval = ext4_ind_map_blocks(handle, inode, map, flags &
540 EXT4_GET_BLOCKS_KEEP_SIZE);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500541 }
Zheng Liuf7fec032013-02-18 00:28:47 -0500542 if (retval > 0) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400543 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -0500544
Zheng Liu44fb851d2013-07-29 12:51:42 -0400545 if (unlikely(retval != map->m_len)) {
546 ext4_warning(inode->i_sb,
547 "ES len assertion failed for inode "
548 "%lu: retval %d != map->m_len %d",
549 inode->i_ino, retval, map->m_len);
550 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400551 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400552
Zheng Liuf7fec032013-02-18 00:28:47 -0500553 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
554 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
555 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400556 !(status & EXTENT_STATUS_WRITTEN) &&
Zheng Liuf7fec032013-02-18 00:28:47 -0500557 ext4_find_delalloc_range(inode, map->m_lblk,
558 map->m_lblk + map->m_len - 1))
559 status |= EXTENT_STATUS_DELAYED;
560 ret = ext4_es_insert_extent(inode, map->m_lblk,
561 map->m_len, map->m_pblk, status);
562 if (ret < 0)
563 retval = ret;
564 }
Jan Kara2dcba472015-12-07 15:04:57 -0500565 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -0500566
Zheng Liud100eef2013-02-18 00:29:59 -0500567found:
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400568 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Lukas Czernerb8a86842014-03-18 18:05:35 -0400569 ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400570 if (ret != 0)
571 return ret;
572 }
573
Mingming Caof5ab0d12008-02-25 15:29:55 -0500574 /* If it is only a block(s) look up */
Theodore Ts'oc2177052009-05-14 00:58:52 -0400575 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500576 return retval;
577
578 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -0500579 * Returns if the blocks have already allocated
580 *
581 * Note that if blocks have been preallocated
Tao Madf3ab172011-10-08 15:53:49 -0400582 * ext4_ext_get_block() returns the create = 0
Mingming Caof5ab0d12008-02-25 15:29:55 -0500583 * with buffer head unmapped.
584 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400585 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
Lukas Czernerb8a86842014-03-18 18:05:35 -0400586 /*
587 * If we need to convert extent to unwritten
588 * we continue and do the actual work in
589 * ext4_ext_map_blocks()
590 */
591 if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN))
592 return retval;
Mingming Caof5ab0d12008-02-25 15:29:55 -0500593
594 /*
Zheng Liua25a4e12013-02-18 00:28:04 -0500595 * Here we clear m_flags because after allocating an new extent,
596 * it will be set again.
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -0400597 */
Zheng Liua25a4e12013-02-18 00:28:04 -0500598 map->m_flags &= ~EXT4_MAP_FLAGS;
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -0400599
600 /*
Lukas Czerner556615d2014-04-20 23:45:47 -0400601 * New blocks allocate and/or writing to unwritten extent
Mingming Caof5ab0d12008-02-25 15:29:55 -0500602 * will possibly result in updating i_data, so we take
Seunghun Leed91bd2c2014-09-01 22:15:30 -0400603 * the write lock of i_data_sem, and call get_block()
Mingming Caof5ab0d12008-02-25 15:29:55 -0500604 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500605 */
Lukas Czernerc8b459f2014-05-12 12:55:07 -0400606 down_write(&EXT4_I(inode)->i_data_sem);
Mingming Caod2a17632008-07-14 17:52:37 -0400607
608 /*
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500609 * We need to check for EXT4 here because migrate
610 * could have changed the inode type in between
611 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400612 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400613 retval = ext4_ext_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500614 } else {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400615 retval = ext4_ind_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400616
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400617 if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400618 /*
619 * We allocated new blocks which will result in
620 * i_data's format changing. Force the migrate
621 * to fail by clearing migrate flags
622 */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500623 ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400624 }
Mingming Caod2a17632008-07-14 17:52:37 -0400625
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500626 /*
627 * Update reserved blocks/metadata blocks after successful
628 * block allocation which had been deferred till now. We don't
629 * support fallocate for non extent files. So we can update
630 * reserve space here.
631 */
632 if ((retval > 0) &&
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -0500633 (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500634 ext4_da_update_reserve_space(inode, retval, 1);
635 }
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -0400636
Zheng Liuf7fec032013-02-18 00:28:47 -0500637 if (retval > 0) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400638 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -0500639
Zheng Liu44fb851d2013-07-29 12:51:42 -0400640 if (unlikely(retval != map->m_len)) {
641 ext4_warning(inode->i_sb,
642 "ES len assertion failed for inode "
643 "%lu: retval %d != map->m_len %d",
644 inode->i_ino, retval, map->m_len);
645 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400646 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400647
Zheng Liuadb23552013-03-10 21:13:05 -0400648 /*
Jan Karac86d8db2015-12-07 15:10:26 -0500649 * We have to zeroout blocks before inserting them into extent
650 * status tree. Otherwise someone could look them up there and
Jan Kara9b623df2016-09-30 02:02:29 -0400651 * use them before they are really zeroed. We also have to
652 * unmap metadata before zeroing as otherwise writeback can
653 * overwrite zeros with stale data from block device.
Jan Karac86d8db2015-12-07 15:10:26 -0500654 */
655 if (flags & EXT4_GET_BLOCKS_ZERO &&
656 map->m_flags & EXT4_MAP_MAPPED &&
657 map->m_flags & EXT4_MAP_NEW) {
Jan Kara9b623df2016-09-30 02:02:29 -0400658 ext4_lblk_t i;
659
660 for (i = 0; i < map->m_len; i++) {
661 unmap_underlying_metadata(inode->i_sb->s_bdev,
662 map->m_pblk + i);
663 }
Jan Karac86d8db2015-12-07 15:10:26 -0500664 ret = ext4_issue_zeroout(inode, map->m_lblk,
665 map->m_pblk, map->m_len);
666 if (ret) {
667 retval = ret;
668 goto out_sem;
669 }
670 }
671
672 /*
Zheng Liuadb23552013-03-10 21:13:05 -0400673 * If the extent has been zeroed out, we don't need to update
674 * extent status tree.
675 */
676 if ((flags & EXT4_GET_BLOCKS_PRE_IO) &&
677 ext4_es_lookup_extent(inode, map->m_lblk, &es)) {
678 if (ext4_es_is_written(&es))
Jan Karac86d8db2015-12-07 15:10:26 -0500679 goto out_sem;
Zheng Liuadb23552013-03-10 21:13:05 -0400680 }
Zheng Liuf7fec032013-02-18 00:28:47 -0500681 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
682 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
683 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400684 !(status & EXTENT_STATUS_WRITTEN) &&
Zheng Liuf7fec032013-02-18 00:28:47 -0500685 ext4_find_delalloc_range(inode, map->m_lblk,
686 map->m_lblk + map->m_len - 1))
687 status |= EXTENT_STATUS_DELAYED;
688 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
689 map->m_pblk, status);
Jan Karac86d8db2015-12-07 15:10:26 -0500690 if (ret < 0) {
Zheng Liuf7fec032013-02-18 00:28:47 -0500691 retval = ret;
Jan Karac86d8db2015-12-07 15:10:26 -0500692 goto out_sem;
693 }
Aditya Kali5356f262011-09-09 19:20:51 -0400694 }
695
Jan Karac86d8db2015-12-07 15:10:26 -0500696out_sem:
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500697 up_write((&EXT4_I(inode)->i_data_sem));
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400698 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Lukas Czernerb8a86842014-03-18 18:05:35 -0400699 ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400700 if (ret != 0)
701 return ret;
Jan Kara06bd3c32016-04-24 00:56:03 -0400702
703 /*
704 * Inodes with freshly allocated blocks where contents will be
705 * visible after transaction commit must be on transaction's
706 * ordered data list.
707 */
708 if (map->m_flags & EXT4_MAP_NEW &&
709 !(map->m_flags & EXT4_MAP_UNWRITTEN) &&
710 !(flags & EXT4_GET_BLOCKS_ZERO) &&
711 !IS_NOQUOTA(inode) &&
712 ext4_should_order_data(inode)) {
Jan Karaee0876b2016-04-24 00:56:08 -0400713 if (flags & EXT4_GET_BLOCKS_IO_SUBMIT)
714 ret = ext4_jbd2_inode_add_wait(handle, inode);
715 else
716 ret = ext4_jbd2_inode_add_write(handle, inode);
Jan Kara06bd3c32016-04-24 00:56:03 -0400717 if (ret)
718 return ret;
719 }
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400720 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500721 return retval;
722}
723
Jan Karaed8ad832016-02-19 00:18:25 -0500724/*
725 * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages
726 * we have to be careful as someone else may be manipulating b_state as well.
727 */
728static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags)
729{
730 unsigned long old_state;
731 unsigned long new_state;
732
733 flags &= EXT4_MAP_FLAGS;
734
735 /* Dummy buffer_head? Set non-atomically. */
736 if (!bh->b_page) {
737 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags;
738 return;
739 }
740 /*
741 * Someone else may be modifying b_state. Be careful! This is ugly but
742 * once we get rid of using bh as a container for mapping information
743 * to pass to / from get_block functions, this can go away.
744 */
745 do {
746 old_state = READ_ONCE(bh->b_state);
747 new_state = (old_state & ~EXT4_MAP_FLAGS) | flags;
748 } while (unlikely(
749 cmpxchg(&bh->b_state, old_state, new_state) != old_state));
750}
751
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400752static int _ext4_get_block(struct inode *inode, sector_t iblock,
753 struct buffer_head *bh, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700754{
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400755 struct ext4_map_blocks map;
Jan Karaefe70c22016-03-08 23:35:46 -0500756 int ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700757
Tao Ma46c7f252012-12-10 14:04:52 -0500758 if (ext4_has_inline_data(inode))
759 return -ERANGE;
760
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400761 map.m_lblk = iblock;
762 map.m_len = bh->b_size >> inode->i_blkbits;
763
Jan Karaefe70c22016-03-08 23:35:46 -0500764 ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map,
765 flags);
Jan Kara7fb54092008-02-10 01:08:38 -0500766 if (ret > 0) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400767 map_bh(bh, inode->i_sb, map.m_pblk);
Jan Karaed8ad832016-02-19 00:18:25 -0500768 ext4_update_bh_state(bh, map.m_flags);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400769 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
Jan Kara7fb54092008-02-10 01:08:38 -0500770 ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700771 }
772 return ret;
773}
774
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400775int ext4_get_block(struct inode *inode, sector_t iblock,
776 struct buffer_head *bh, int create)
777{
778 return _ext4_get_block(inode, iblock, bh,
779 create ? EXT4_GET_BLOCKS_CREATE : 0);
780}
781
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700782/*
Jan Kara705965b2016-03-08 23:08:10 -0500783 * Get block function used when preparing for buffered write if we require
784 * creating an unwritten extent if blocks haven't been allocated. The extent
785 * will be converted to written after the IO is complete.
786 */
787int ext4_get_block_unwritten(struct inode *inode, sector_t iblock,
788 struct buffer_head *bh_result, int create)
789{
790 ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n",
791 inode->i_ino, create);
792 return _ext4_get_block(inode, iblock, bh_result,
793 EXT4_GET_BLOCKS_IO_CREATE_EXT);
794}
795
Jan Karaefe70c22016-03-08 23:35:46 -0500796/* Maximum number of blocks we map for direct IO at once. */
797#define DIO_MAX_BLOCKS 4096
798
Jan Karae84dfbe2016-04-01 02:07:22 -0400799/*
800 * Get blocks function for the cases that need to start a transaction -
801 * generally difference cases of direct IO and DAX IO. It also handles retries
802 * in case of ENOSPC.
803 */
804static int ext4_get_block_trans(struct inode *inode, sector_t iblock,
805 struct buffer_head *bh_result, int flags)
Jan Karaefe70c22016-03-08 23:35:46 -0500806{
807 int dio_credits;
Jan Karae84dfbe2016-04-01 02:07:22 -0400808 handle_t *handle;
809 int retries = 0;
810 int ret;
Jan Karaefe70c22016-03-08 23:35:46 -0500811
812 /* Trim mapping request to maximum we can map at once for DIO */
813 if (bh_result->b_size >> inode->i_blkbits > DIO_MAX_BLOCKS)
814 bh_result->b_size = DIO_MAX_BLOCKS << inode->i_blkbits;
815 dio_credits = ext4_chunk_trans_blocks(inode,
816 bh_result->b_size >> inode->i_blkbits);
Jan Karae84dfbe2016-04-01 02:07:22 -0400817retry:
818 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits);
819 if (IS_ERR(handle))
820 return PTR_ERR(handle);
821
822 ret = _ext4_get_block(inode, iblock, bh_result, flags);
823 ext4_journal_stop(handle);
824
825 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
826 goto retry;
827 return ret;
Jan Karaefe70c22016-03-08 23:35:46 -0500828}
829
Jan Kara705965b2016-03-08 23:08:10 -0500830/* Get block function for DIO reads and writes to inodes without extents */
831int ext4_dio_get_block(struct inode *inode, sector_t iblock,
832 struct buffer_head *bh, int create)
833{
Jan Karaefe70c22016-03-08 23:35:46 -0500834 /* We don't expect handle for direct IO */
835 WARN_ON_ONCE(ext4_journal_current_handle());
836
Jan Karae84dfbe2016-04-01 02:07:22 -0400837 if (!create)
838 return _ext4_get_block(inode, iblock, bh, 0);
839 return ext4_get_block_trans(inode, iblock, bh, EXT4_GET_BLOCKS_CREATE);
Jan Kara705965b2016-03-08 23:08:10 -0500840}
841
842/*
Jan Kara109811c2016-03-08 23:36:46 -0500843 * Get block function for AIO DIO writes when we create unwritten extent if
Jan Kara705965b2016-03-08 23:08:10 -0500844 * blocks are not allocated yet. The extent will be converted to written
845 * after IO is complete.
846 */
Jan Kara109811c2016-03-08 23:36:46 -0500847static int ext4_dio_get_block_unwritten_async(struct inode *inode,
848 sector_t iblock, struct buffer_head *bh_result, int create)
Jan Kara705965b2016-03-08 23:08:10 -0500849{
Jan Karaefe70c22016-03-08 23:35:46 -0500850 int ret;
851
Jan Karaefe70c22016-03-08 23:35:46 -0500852 /* We don't expect handle for direct IO */
853 WARN_ON_ONCE(ext4_journal_current_handle());
854
Jan Karae84dfbe2016-04-01 02:07:22 -0400855 ret = ext4_get_block_trans(inode, iblock, bh_result,
856 EXT4_GET_BLOCKS_IO_CREATE_EXT);
Jan Karaefe70c22016-03-08 23:35:46 -0500857
Jan Kara109811c2016-03-08 23:36:46 -0500858 /*
859 * When doing DIO using unwritten extents, we need io_end to convert
860 * unwritten extents to written on IO completion. We allocate io_end
861 * once we spot unwritten extent and store it in b_private. Generic
862 * DIO code keeps b_private set and furthermore passes the value to
863 * our completion callback in 'private' argument.
864 */
865 if (!ret && buffer_unwritten(bh_result)) {
866 if (!bh_result->b_private) {
867 ext4_io_end_t *io_end;
868
869 io_end = ext4_init_io_end(inode, GFP_KERNEL);
870 if (!io_end)
871 return -ENOMEM;
872 bh_result->b_private = io_end;
873 ext4_set_io_unwritten_flag(inode, io_end);
874 }
Jan Karaefe70c22016-03-08 23:35:46 -0500875 set_buffer_defer_completion(bh_result);
Jan Karaefe70c22016-03-08 23:35:46 -0500876 }
877
878 return ret;
Jan Kara705965b2016-03-08 23:08:10 -0500879}
880
Jan Kara109811c2016-03-08 23:36:46 -0500881/*
882 * Get block function for non-AIO DIO writes when we create unwritten extent if
883 * blocks are not allocated yet. The extent will be converted to written
884 * after IO is complete from ext4_ext_direct_IO() function.
885 */
886static int ext4_dio_get_block_unwritten_sync(struct inode *inode,
887 sector_t iblock, struct buffer_head *bh_result, int create)
888{
Jan Kara109811c2016-03-08 23:36:46 -0500889 int ret;
890
891 /* We don't expect handle for direct IO */
892 WARN_ON_ONCE(ext4_journal_current_handle());
893
Jan Karae84dfbe2016-04-01 02:07:22 -0400894 ret = ext4_get_block_trans(inode, iblock, bh_result,
895 EXT4_GET_BLOCKS_IO_CREATE_EXT);
Jan Kara109811c2016-03-08 23:36:46 -0500896
897 /*
898 * Mark inode as having pending DIO writes to unwritten extents.
899 * ext4_ext_direct_IO() checks this flag and converts extents to
900 * written.
901 */
902 if (!ret && buffer_unwritten(bh_result))
903 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
904
905 return ret;
906}
907
Jan Kara705965b2016-03-08 23:08:10 -0500908static int ext4_dio_get_block_overwrite(struct inode *inode, sector_t iblock,
909 struct buffer_head *bh_result, int create)
910{
911 int ret;
912
913 ext4_debug("ext4_dio_get_block_overwrite: inode %lu, create flag %d\n",
914 inode->i_ino, create);
Jan Karaefe70c22016-03-08 23:35:46 -0500915 /* We don't expect handle for direct IO */
916 WARN_ON_ONCE(ext4_journal_current_handle());
917
Jan Kara705965b2016-03-08 23:08:10 -0500918 ret = _ext4_get_block(inode, iblock, bh_result, 0);
919 /*
920 * Blocks should have been preallocated! ext4_file_write_iter() checks
921 * that.
922 */
Jan Karaefe70c22016-03-08 23:35:46 -0500923 WARN_ON_ONCE(!buffer_mapped(bh_result) || buffer_unwritten(bh_result));
Jan Kara705965b2016-03-08 23:08:10 -0500924
925 return ret;
926}
927
928
929/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700930 * `handle' can be NULL if create is zero
931 */
Mingming Cao617ba132006-10-11 01:20:53 -0700932struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400933 ext4_lblk_t block, int map_flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700934{
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400935 struct ext4_map_blocks map;
936 struct buffer_head *bh;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400937 int create = map_flags & EXT4_GET_BLOCKS_CREATE;
Theodore Ts'o10560082014-08-29 20:51:32 -0400938 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700939
940 J_ASSERT(handle != NULL || create == 0);
941
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400942 map.m_lblk = block;
943 map.m_len = 1;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400944 err = ext4_map_blocks(handle, inode, &map, map_flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700945
Theodore Ts'o10560082014-08-29 20:51:32 -0400946 if (err == 0)
947 return create ? ERR_PTR(-ENOSPC) : NULL;
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400948 if (err < 0)
Theodore Ts'o10560082014-08-29 20:51:32 -0400949 return ERR_PTR(err);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400950
951 bh = sb_getblk(inode->i_sb, map.m_pblk);
Theodore Ts'o10560082014-08-29 20:51:32 -0400952 if (unlikely(!bh))
953 return ERR_PTR(-ENOMEM);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400954 if (map.m_flags & EXT4_MAP_NEW) {
955 J_ASSERT(create != 0);
956 J_ASSERT(handle != NULL);
957
958 /*
959 * Now that we do not always journal data, we should
960 * keep in mind whether this should always journal the
961 * new buffer as metadata. For now, regular file
962 * writes use ext4_get_block instead, so it's not a
963 * problem.
964 */
965 lock_buffer(bh);
966 BUFFER_TRACE(bh, "call get_create_access");
Theodore Ts'o10560082014-08-29 20:51:32 -0400967 err = ext4_journal_get_create_access(handle, bh);
968 if (unlikely(err)) {
969 unlock_buffer(bh);
970 goto errout;
971 }
972 if (!buffer_uptodate(bh)) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400973 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
974 set_buffer_uptodate(bh);
975 }
976 unlock_buffer(bh);
977 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
978 err = ext4_handle_dirty_metadata(handle, inode, bh);
Theodore Ts'o10560082014-08-29 20:51:32 -0400979 if (unlikely(err))
980 goto errout;
981 } else
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400982 BUFFER_TRACE(bh, "not a new buffer");
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400983 return bh;
Theodore Ts'o10560082014-08-29 20:51:32 -0400984errout:
985 brelse(bh);
986 return ERR_PTR(err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700987}
988
Mingming Cao617ba132006-10-11 01:20:53 -0700989struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400990 ext4_lblk_t block, int map_flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700991{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400992 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700993
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400994 bh = ext4_getblk(handle, inode, block, map_flags);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400995 if (IS_ERR(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700996 return bh;
Theodore Ts'o1c215022014-08-29 20:52:15 -0400997 if (!bh || buffer_uptodate(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700998 return bh;
Mike Christiedfec8a12016-06-05 14:31:44 -0500999 ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001000 wait_on_buffer(bh);
1001 if (buffer_uptodate(bh))
1002 return bh;
1003 put_bh(bh);
Theodore Ts'o1c215022014-08-29 20:52:15 -04001004 return ERR_PTR(-EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001005}
1006
Tao Maf19d5872012-12-10 14:05:51 -05001007int ext4_walk_page_buffers(handle_t *handle,
1008 struct buffer_head *head,
1009 unsigned from,
1010 unsigned to,
1011 int *partial,
1012 int (*fn)(handle_t *handle,
1013 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001014{
1015 struct buffer_head *bh;
1016 unsigned block_start, block_end;
1017 unsigned blocksize = head->b_size;
1018 int err, ret = 0;
1019 struct buffer_head *next;
1020
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001021 for (bh = head, block_start = 0;
1022 ret == 0 && (bh != head || !block_start);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001023 block_start = block_end, bh = next) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001024 next = bh->b_this_page;
1025 block_end = block_start + blocksize;
1026 if (block_end <= from || block_start >= to) {
1027 if (partial && !buffer_uptodate(bh))
1028 *partial = 1;
1029 continue;
1030 }
1031 err = (*fn)(handle, bh);
1032 if (!ret)
1033 ret = err;
1034 }
1035 return ret;
1036}
1037
1038/*
1039 * To preserve ordering, it is essential that the hole instantiation and
1040 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -07001041 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -07001042 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001043 * prepare_write() is the right place.
1044 *
Jan Kara36ade452013-01-28 09:30:52 -05001045 * Also, this function can nest inside ext4_writepage(). In that case, we
1046 * *know* that ext4_writepage() has generated enough buffer credits to do the
1047 * whole page. So we won't block on the journal in that case, which is good,
1048 * because the caller may be PF_MEMALLOC.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001049 *
Mingming Cao617ba132006-10-11 01:20:53 -07001050 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001051 * quota file writes. If we were to commit the transaction while thus
1052 * reentered, there can be a deadlock - we would be holding a quota
1053 * lock, and the commit would never complete if another thread had a
1054 * transaction open and was blocking on the quota lock - a ranking
1055 * violation.
1056 *
Mingming Caodab291a2006-10-11 01:21:01 -07001057 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001058 * will _not_ run commit under these circumstances because handle->h_ref
1059 * is elevated. We'll still have enough credits for the tiny quotafile
1060 * write.
1061 */
Tao Maf19d5872012-12-10 14:05:51 -05001062int do_journal_get_write_access(handle_t *handle,
1063 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001064{
Jan Kara56d35a42010-08-05 14:41:42 -04001065 int dirty = buffer_dirty(bh);
1066 int ret;
1067
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001068 if (!buffer_mapped(bh) || buffer_freed(bh))
1069 return 0;
Jan Kara56d35a42010-08-05 14:41:42 -04001070 /*
Christoph Hellwigebdec242010-10-06 10:47:23 +02001071 * __block_write_begin() could have dirtied some buffers. Clean
Jan Kara56d35a42010-08-05 14:41:42 -04001072 * the dirty bit as jbd2_journal_get_write_access() could complain
1073 * otherwise about fs integrity issues. Setting of the dirty bit
Christoph Hellwigebdec242010-10-06 10:47:23 +02001074 * by __block_write_begin() isn't a real problem here as we clear
Jan Kara56d35a42010-08-05 14:41:42 -04001075 * the bit before releasing a page lock and thus writeback cannot
1076 * ever write the buffer.
1077 */
1078 if (dirty)
1079 clear_buffer_dirty(bh);
liang xie5d601252014-05-12 22:06:43 -04001080 BUFFER_TRACE(bh, "get write access");
Jan Kara56d35a42010-08-05 14:41:42 -04001081 ret = ext4_journal_get_write_access(handle, bh);
1082 if (!ret && dirty)
1083 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
1084 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001085}
1086
Michael Halcrow2058f832015-04-12 00:55:10 -04001087#ifdef CONFIG_EXT4_FS_ENCRYPTION
1088static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len,
1089 get_block_t *get_block)
1090{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001091 unsigned from = pos & (PAGE_SIZE - 1);
Michael Halcrow2058f832015-04-12 00:55:10 -04001092 unsigned to = from + len;
1093 struct inode *inode = page->mapping->host;
1094 unsigned block_start, block_end;
1095 sector_t block;
1096 int err = 0;
1097 unsigned blocksize = inode->i_sb->s_blocksize;
1098 unsigned bbits;
1099 struct buffer_head *bh, *head, *wait[2], **wait_bh = wait;
1100 bool decrypt = false;
1101
1102 BUG_ON(!PageLocked(page));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001103 BUG_ON(from > PAGE_SIZE);
1104 BUG_ON(to > PAGE_SIZE);
Michael Halcrow2058f832015-04-12 00:55:10 -04001105 BUG_ON(from > to);
1106
1107 if (!page_has_buffers(page))
1108 create_empty_buffers(page, blocksize, 0);
1109 head = page_buffers(page);
1110 bbits = ilog2(blocksize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001111 block = (sector_t)page->index << (PAGE_SHIFT - bbits);
Michael Halcrow2058f832015-04-12 00:55:10 -04001112
1113 for (bh = head, block_start = 0; bh != head || !block_start;
1114 block++, block_start = block_end, bh = bh->b_this_page) {
1115 block_end = block_start + blocksize;
1116 if (block_end <= from || block_start >= to) {
1117 if (PageUptodate(page)) {
1118 if (!buffer_uptodate(bh))
1119 set_buffer_uptodate(bh);
1120 }
1121 continue;
1122 }
1123 if (buffer_new(bh))
1124 clear_buffer_new(bh);
1125 if (!buffer_mapped(bh)) {
1126 WARN_ON(bh->b_size != blocksize);
1127 err = get_block(inode, block, bh, 1);
1128 if (err)
1129 break;
1130 if (buffer_new(bh)) {
1131 unmap_underlying_metadata(bh->b_bdev,
1132 bh->b_blocknr);
1133 if (PageUptodate(page)) {
1134 clear_buffer_new(bh);
1135 set_buffer_uptodate(bh);
1136 mark_buffer_dirty(bh);
1137 continue;
1138 }
1139 if (block_end > to || block_start < from)
1140 zero_user_segments(page, to, block_end,
1141 block_start, from);
1142 continue;
1143 }
1144 }
1145 if (PageUptodate(page)) {
1146 if (!buffer_uptodate(bh))
1147 set_buffer_uptodate(bh);
1148 continue;
1149 }
1150 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
1151 !buffer_unwritten(bh) &&
1152 (block_start < from || block_end > to)) {
Mike Christiedfec8a12016-06-05 14:31:44 -05001153 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
Michael Halcrow2058f832015-04-12 00:55:10 -04001154 *wait_bh++ = bh;
1155 decrypt = ext4_encrypted_inode(inode) &&
1156 S_ISREG(inode->i_mode);
1157 }
1158 }
1159 /*
1160 * If we issued read requests, let them complete.
1161 */
1162 while (wait_bh > wait) {
1163 wait_on_buffer(*--wait_bh);
1164 if (!buffer_uptodate(*wait_bh))
1165 err = -EIO;
1166 }
1167 if (unlikely(err))
1168 page_zero_new_buffers(page, from, to);
1169 else if (decrypt)
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001170 err = fscrypt_decrypt_page(page);
Michael Halcrow2058f832015-04-12 00:55:10 -04001171 return err;
1172}
1173#endif
1174
Nick Pigginbfc1af62007-10-16 01:25:05 -07001175static int ext4_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001176 loff_t pos, unsigned len, unsigned flags,
1177 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001178{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001179 struct inode *inode = mapping->host;
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001180 int ret, needed_blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001181 handle_t *handle;
1182 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001183 struct page *page;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001184 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001185 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001186
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -08001187 trace_android_fs_datawrite_start(inode, pos, len,
1188 current->pid, current->comm);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001189 trace_ext4_write_begin(inode, pos, len, flags);
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001190 /*
1191 * Reserve one block more for addition to orphan list in case
1192 * we allocate blocks but write fails for some reason
1193 */
1194 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001195 index = pos >> PAGE_SHIFT;
1196 from = pos & (PAGE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001197 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001198
Tao Maf19d5872012-12-10 14:05:51 -05001199 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
1200 ret = ext4_try_to_write_inline_data(mapping, inode, pos, len,
1201 flags, pagep);
1202 if (ret < 0)
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001203 return ret;
1204 if (ret == 1)
1205 return 0;
Tao Maf19d5872012-12-10 14:05:51 -05001206 }
1207
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001208 /*
1209 * grab_cache_page_write_begin() can take a long time if the
1210 * system is thrashing due to memory pressure, or if the page
1211 * is being written back. So grab it first before we start
1212 * the transaction handle. This also allows us to allocate
1213 * the page (if needed) without using GFP_NOFS.
1214 */
1215retry_grab:
Nick Piggin54566b22009-01-04 12:00:53 -08001216 page = grab_cache_page_write_begin(mapping, index, flags);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001217 if (!page)
1218 return -ENOMEM;
1219 unlock_page(page);
1220
1221retry_journal:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001222 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001223 if (IS_ERR(handle)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001224 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001225 return PTR_ERR(handle);
Jan Karacf108bc2008-07-11 19:27:31 -04001226 }
Tao Maf19d5872012-12-10 14:05:51 -05001227
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001228 lock_page(page);
1229 if (page->mapping != mapping) {
1230 /* The page got truncated from under us */
1231 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001232 put_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04001233 ext4_journal_stop(handle);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001234 goto retry_grab;
Jan Karacf108bc2008-07-11 19:27:31 -04001235 }
Dmitry Monakhov7afe5aa2013-08-28 14:30:47 -04001236 /* In case writeback began while the page was unlocked */
1237 wait_for_stable_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04001238
Michael Halcrow2058f832015-04-12 00:55:10 -04001239#ifdef CONFIG_EXT4_FS_ENCRYPTION
1240 if (ext4_should_dioread_nolock(inode))
1241 ret = ext4_block_write_begin(page, pos, len,
Jan Kara705965b2016-03-08 23:08:10 -05001242 ext4_get_block_unwritten);
Michael Halcrow2058f832015-04-12 00:55:10 -04001243 else
1244 ret = ext4_block_write_begin(page, pos, len,
1245 ext4_get_block);
1246#else
Jiaying Zhang744692d2010-03-04 16:14:02 -05001247 if (ext4_should_dioread_nolock(inode))
Jan Kara705965b2016-03-08 23:08:10 -05001248 ret = __block_write_begin(page, pos, len,
1249 ext4_get_block_unwritten);
Jiaying Zhang744692d2010-03-04 16:14:02 -05001250 else
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001251 ret = __block_write_begin(page, pos, len, ext4_get_block);
Michael Halcrow2058f832015-04-12 00:55:10 -04001252#endif
Nick Pigginbfc1af62007-10-16 01:25:05 -07001253 if (!ret && ext4_should_journal_data(inode)) {
Tao Maf19d5872012-12-10 14:05:51 -05001254 ret = ext4_walk_page_buffers(handle, page_buffers(page),
1255 from, to, NULL,
1256 do_journal_get_write_access);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001257 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001258
1259 if (ret) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001260 unlock_page(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001261 /*
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001262 * __block_write_begin may have instantiated a few blocks
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001263 * outside i_size. Trim these off again. Don't need
1264 * i_size_read because we hold i_mutex.
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001265 *
1266 * Add inode to orphan list in case we crash before
1267 * truncate finishes
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001268 */
Jan Karaffacfa72009-07-13 16:22:22 -04001269 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001270 ext4_orphan_add(handle, inode);
1271
1272 ext4_journal_stop(handle);
1273 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001274 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001275 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001276 * If truncate failed early the inode might
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001277 * still be on the orphan list; we need to
1278 * make sure the inode is removed from the
1279 * orphan list in that case.
1280 */
1281 if (inode->i_nlink)
1282 ext4_orphan_del(NULL, inode);
1283 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001284
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001285 if (ret == -ENOSPC &&
1286 ext4_should_retry_alloc(inode->i_sb, &retries))
1287 goto retry_journal;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001288 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001289 return ret;
1290 }
1291 *pagep = page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001292 return ret;
1293}
1294
Nick Pigginbfc1af62007-10-16 01:25:05 -07001295/* For write_end() in data=journal mode */
1296static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001297{
Theodore Ts'o13fca322013-04-21 16:45:54 -04001298 int ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001299 if (!buffer_mapped(bh) || buffer_freed(bh))
1300 return 0;
1301 set_buffer_uptodate(bh);
Theodore Ts'o13fca322013-04-21 16:45:54 -04001302 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
1303 clear_buffer_meta(bh);
1304 clear_buffer_prio(bh);
1305 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001306}
1307
Zheng Liueed43332013-04-03 12:41:17 -04001308/*
1309 * We need to pick up the new inode size which generic_commit_write gave us
1310 * `file' can be NULL - eg, when called from page_symlink().
1311 *
1312 * ext4 never places buffers on inode->i_mapping->private_list. metadata
1313 * buffers are managed internally.
1314 */
1315static int ext4_write_end(struct file *file,
1316 struct address_space *mapping,
1317 loff_t pos, unsigned len, unsigned copied,
1318 struct page *page, void *fsdata)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001319{
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001320 handle_t *handle = ext4_journal_current_handle();
Zheng Liueed43332013-04-03 12:41:17 -04001321 struct inode *inode = mapping->host;
Xiaoguang Wang05726392015-02-12 23:00:17 -05001322 loff_t old_size = inode->i_size;
Zheng Liueed43332013-04-03 12:41:17 -04001323 int ret = 0, ret2;
1324 int i_size_changed = 0;
1325
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -08001326 trace_android_fs_datawrite_end(inode, pos, len);
Zheng Liueed43332013-04-03 12:41:17 -04001327 trace_ext4_write_end(inode, pos, len, copied);
Theodore Ts'o42c832d2013-07-01 08:12:39 -04001328 if (ext4_has_inline_data(inode)) {
1329 ret = ext4_write_inline_data_end(inode, pos, len,
1330 copied, page);
1331 if (ret < 0)
1332 goto errout;
1333 copied = ret;
1334 } else
Tao Maf19d5872012-12-10 14:05:51 -05001335 copied = block_write_end(file, mapping, pos,
1336 len, copied, page, fsdata);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001337 /*
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001338 * it's important to update i_size while still holding page lock:
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001339 * page writeout could otherwise come in and zero beyond i_size.
1340 */
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001341 i_size_changed = ext4_update_inode_size(inode, pos + copied);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001342 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001343 put_page(page);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001344
Xiaoguang Wang05726392015-02-12 23:00:17 -05001345 if (old_size < pos)
1346 pagecache_isize_extended(inode, old_size, pos);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001347 /*
1348 * Don't mark the inode dirty under page lock. First, it unnecessarily
1349 * makes the holding time of page lock longer. Second, it forces lock
1350 * ordering of page lock and transaction start for journaling
1351 * filesystems.
1352 */
1353 if (i_size_changed)
1354 ext4_mark_inode_dirty(handle, inode);
1355
Jan Karaffacfa72009-07-13 16:22:22 -04001356 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001357 /* if we have allocated more blocks and copied
1358 * less. We will have blocks allocated outside
1359 * inode->i_size. So truncate them
1360 */
1361 ext4_orphan_add(handle, inode);
Theodore Ts'o74d553a2013-04-03 12:39:17 -04001362errout:
Mingming Cao617ba132006-10-11 01:20:53 -07001363 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001364 if (!ret)
1365 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001366
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001367 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001368 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001369 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001370 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001371 * on the orphan list; we need to make sure the inode
1372 * is removed from the orphan list in that case.
1373 */
1374 if (inode->i_nlink)
1375 ext4_orphan_del(NULL, inode);
1376 }
1377
Nick Pigginbfc1af62007-10-16 01:25:05 -07001378 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001379}
1380
Theodore Ts'ob90197b2015-10-15 10:29:05 -04001381/*
1382 * This is a private version of page_zero_new_buffers() which doesn't
1383 * set the buffer to be dirty, since in data=journalled mode we need
1384 * to call ext4_handle_dirty_metadata() instead.
1385 */
1386static void zero_new_buffers(struct page *page, unsigned from, unsigned to)
1387{
1388 unsigned int block_start = 0, block_end;
1389 struct buffer_head *head, *bh;
1390
1391 bh = head = page_buffers(page);
1392 do {
1393 block_end = block_start + bh->b_size;
1394 if (buffer_new(bh)) {
1395 if (block_end > from && block_start < to) {
1396 if (!PageUptodate(page)) {
1397 unsigned start, size;
1398
1399 start = max(from, block_start);
1400 size = min(to, block_end) - start;
1401
1402 zero_user(page, start, size);
1403 set_buffer_uptodate(bh);
1404 }
1405 clear_buffer_new(bh);
1406 }
1407 }
1408 block_start = block_end;
1409 bh = bh->b_this_page;
1410 } while (bh != head);
1411}
1412
Nick Pigginbfc1af62007-10-16 01:25:05 -07001413static int ext4_journalled_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001414 struct address_space *mapping,
1415 loff_t pos, unsigned len, unsigned copied,
1416 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001417{
Mingming Cao617ba132006-10-11 01:20:53 -07001418 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -07001419 struct inode *inode = mapping->host;
Xiaoguang Wang05726392015-02-12 23:00:17 -05001420 loff_t old_size = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001421 int ret = 0, ret2;
1422 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001423 unsigned from, to;
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001424 int size_changed = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001425
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -08001426 trace_android_fs_datawrite_end(inode, pos, len);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001427 trace_ext4_journalled_write_end(inode, pos, len, copied);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001428 from = pos & (PAGE_SIZE - 1);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001429 to = from + len;
1430
Curt Wohlgemuth441c8502011-08-13 11:25:18 -04001431 BUG_ON(!ext4_handle_valid(handle));
1432
Tao Ma3fdcfb62012-12-10 14:05:57 -05001433 if (ext4_has_inline_data(inode))
1434 copied = ext4_write_inline_data_end(inode, pos, len,
1435 copied, page);
1436 else {
1437 if (copied < len) {
1438 if (!PageUptodate(page))
1439 copied = 0;
Theodore Ts'ob90197b2015-10-15 10:29:05 -04001440 zero_new_buffers(page, from+copied, to);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001441 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001442
Tao Ma3fdcfb62012-12-10 14:05:57 -05001443 ret = ext4_walk_page_buffers(handle, page_buffers(page), from,
1444 to, &partial, write_end_fn);
1445 if (!partial)
1446 SetPageUptodate(page);
1447 }
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001448 size_changed = ext4_update_inode_size(inode, pos + copied);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001449 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Jan Kara2d859db2011-07-26 09:07:11 -04001450 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001451 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001452 put_page(page);
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001453
Xiaoguang Wang05726392015-02-12 23:00:17 -05001454 if (old_size < pos)
1455 pagecache_isize_extended(inode, old_size, pos);
1456
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001457 if (size_changed) {
Mingming Cao617ba132006-10-11 01:20:53 -07001458 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001459 if (!ret)
1460 ret = ret2;
1461 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001462
Jan Karaffacfa72009-07-13 16:22:22 -04001463 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001464 /* if we have allocated more blocks and copied
1465 * less. We will have blocks allocated outside
1466 * inode->i_size. So truncate them
1467 */
1468 ext4_orphan_add(handle, inode);
1469
Mingming Cao617ba132006-10-11 01:20:53 -07001470 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001471 if (!ret)
1472 ret = ret2;
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001473 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001474 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001475 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001476 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001477 * on the orphan list; we need to make sure the inode
1478 * is removed from the orphan list in that case.
1479 */
1480 if (inode->i_nlink)
1481 ext4_orphan_del(NULL, inode);
1482 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001483
1484 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001485}
Mingming Caod2a17632008-07-14 17:52:37 -04001486
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001487/*
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001488 * Reserve space for a single cluster
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001489 */
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001490static int ext4_da_reserve_space(struct inode *inode)
Mingming Caod2a17632008-07-14 17:52:37 -04001491{
Mingming Cao60e58e02009-01-22 18:13:05 +01001492 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001493 struct ext4_inode_info *ei = EXT4_I(inode);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001494 int ret;
Mingming Caod2a17632008-07-14 17:52:37 -04001495
Mingming Cao60e58e02009-01-22 18:13:05 +01001496 /*
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001497 * We will charge metadata quota at writeout time; this saves
1498 * us from metadata over-estimation, though we may go over by
1499 * a small amount in the end. Here we just reserve for data.
Mingming Cao60e58e02009-01-22 18:13:05 +01001500 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04001501 ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001502 if (ret)
1503 return ret;
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001504
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001505 spin_lock(&ei->i_block_reservation_lock);
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04001506 if (ext4_claim_free_clusters(sbi, 1, 0)) {
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001507 spin_unlock(&ei->i_block_reservation_lock);
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001508 dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
Mingming Caod2a17632008-07-14 17:52:37 -04001509 return -ENOSPC;
1510 }
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001511 ei->i_reserved_data_blocks++;
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001512 trace_ext4_da_reserve_space(inode);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001513 spin_unlock(&ei->i_block_reservation_lock);
Dmitry Monakhov39bc6802009-12-10 16:36:27 +00001514
Mingming Caod2a17632008-07-14 17:52:37 -04001515 return 0; /* success */
1516}
1517
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001518static void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001519{
1520 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001521 struct ext4_inode_info *ei = EXT4_I(inode);
Mingming Caod2a17632008-07-14 17:52:37 -04001522
Mingming Caocd213222008-08-19 22:16:59 -04001523 if (!to_free)
1524 return; /* Nothing to release, exit */
1525
Mingming Caod2a17632008-07-14 17:52:37 -04001526 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001527
Li Zefan5a58ec82010-05-17 02:00:00 -04001528 trace_ext4_da_release_space(inode, to_free);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001529 if (unlikely(to_free > ei->i_reserved_data_blocks)) {
Mingming Caocd213222008-08-19 22:16:59 -04001530 /*
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001531 * if there aren't enough reserved blocks, then the
1532 * counter is messed up somewhere. Since this
1533 * function is called from invalidate page, it's
1534 * harmless to return without any action.
Mingming Caocd213222008-08-19 22:16:59 -04001535 */
Theodore Ts'o8de5c322013-02-14 15:11:41 -05001536 ext4_warning(inode->i_sb, "ext4_da_release_space: "
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001537 "ino %lu, to_free %d with only %d reserved "
Theodore Ts'o1084f252012-03-19 23:13:43 -04001538 "data blocks", inode->i_ino, to_free,
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001539 ei->i_reserved_data_blocks);
1540 WARN_ON(1);
1541 to_free = ei->i_reserved_data_blocks;
1542 }
1543 ei->i_reserved_data_blocks -= to_free;
1544
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001545 /* update fs dirty data blocks counter */
Theodore Ts'o57042652011-09-09 18:56:51 -04001546 percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free);
Mingming Caod2a17632008-07-14 17:52:37 -04001547
Mingming Caod2a17632008-07-14 17:52:37 -04001548 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001549
Aditya Kali7b415bf2011-09-09 19:04:51 -04001550 dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free));
Mingming Caod2a17632008-07-14 17:52:37 -04001551}
1552
1553static void ext4_da_page_release_reservation(struct page *page,
Lukas Czernerca99fdd2013-05-21 23:25:01 -04001554 unsigned int offset,
1555 unsigned int length)
Mingming Caod2a17632008-07-14 17:52:37 -04001556{
Lukas Czerner9705acd2015-07-03 21:13:55 -04001557 int to_release = 0, contiguous_blks = 0;
Mingming Caod2a17632008-07-14 17:52:37 -04001558 struct buffer_head *head, *bh;
1559 unsigned int curr_off = 0;
Aditya Kali7b415bf2011-09-09 19:04:51 -04001560 struct inode *inode = page->mapping->host;
1561 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Lukas Czernerca99fdd2013-05-21 23:25:01 -04001562 unsigned int stop = offset + length;
Aditya Kali7b415bf2011-09-09 19:04:51 -04001563 int num_clusters;
Zheng Liu51865fd2012-11-08 21:57:32 -05001564 ext4_fsblk_t lblk;
Mingming Caod2a17632008-07-14 17:52:37 -04001565
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001566 BUG_ON(stop > PAGE_SIZE || stop < length);
Lukas Czernerca99fdd2013-05-21 23:25:01 -04001567
Mingming Caod2a17632008-07-14 17:52:37 -04001568 head = page_buffers(page);
1569 bh = head;
1570 do {
1571 unsigned int next_off = curr_off + bh->b_size;
1572
Lukas Czernerca99fdd2013-05-21 23:25:01 -04001573 if (next_off > stop)
1574 break;
1575
Mingming Caod2a17632008-07-14 17:52:37 -04001576 if ((offset <= curr_off) && (buffer_delay(bh))) {
1577 to_release++;
Lukas Czerner9705acd2015-07-03 21:13:55 -04001578 contiguous_blks++;
Mingming Caod2a17632008-07-14 17:52:37 -04001579 clear_buffer_delay(bh);
Lukas Czerner9705acd2015-07-03 21:13:55 -04001580 } else if (contiguous_blks) {
1581 lblk = page->index <<
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001582 (PAGE_SHIFT - inode->i_blkbits);
Lukas Czerner9705acd2015-07-03 21:13:55 -04001583 lblk += (curr_off >> inode->i_blkbits) -
1584 contiguous_blks;
1585 ext4_es_remove_extent(inode, lblk, contiguous_blks);
1586 contiguous_blks = 0;
Mingming Caod2a17632008-07-14 17:52:37 -04001587 }
1588 curr_off = next_off;
1589 } while ((bh = bh->b_this_page) != head);
Aditya Kali7b415bf2011-09-09 19:04:51 -04001590
Lukas Czerner9705acd2015-07-03 21:13:55 -04001591 if (contiguous_blks) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001592 lblk = page->index << (PAGE_SHIFT - inode->i_blkbits);
Lukas Czerner9705acd2015-07-03 21:13:55 -04001593 lblk += (curr_off >> inode->i_blkbits) - contiguous_blks;
1594 ext4_es_remove_extent(inode, lblk, contiguous_blks);
Zheng Liu51865fd2012-11-08 21:57:32 -05001595 }
1596
Aditya Kali7b415bf2011-09-09 19:04:51 -04001597 /* If we have released all the blocks belonging to a cluster, then we
1598 * need to release the reserved space for that cluster. */
1599 num_clusters = EXT4_NUM_B2C(sbi, to_release);
1600 while (num_clusters > 0) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001601 lblk = (page->index << (PAGE_SHIFT - inode->i_blkbits)) +
Aditya Kali7b415bf2011-09-09 19:04:51 -04001602 ((num_clusters - 1) << sbi->s_cluster_bits);
1603 if (sbi->s_cluster_ratio == 1 ||
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05001604 !ext4_find_delalloc_cluster(inode, lblk))
Aditya Kali7b415bf2011-09-09 19:04:51 -04001605 ext4_da_release_space(inode, 1);
1606
1607 num_clusters--;
1608 }
Mingming Caod2a17632008-07-14 17:52:37 -04001609}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001610
1611/*
Alex Tomas64769242008-07-11 19:27:31 -04001612 * Delayed allocation stuff
1613 */
1614
Jan Kara4e7ea812013-06-04 13:17:40 -04001615struct mpage_da_data {
1616 struct inode *inode;
1617 struct writeback_control *wbc;
Jan Kara6b523df2013-06-04 13:21:11 -04001618
Jan Kara4e7ea812013-06-04 13:17:40 -04001619 pgoff_t first_page; /* The first page to write */
1620 pgoff_t next_page; /* Current page to examine */
1621 pgoff_t last_page; /* Last page to examine */
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001622 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04001623 * Extent to map - this can be after first_page because that can be
1624 * fully mapped. We somewhat abuse m_flags to store whether the extent
1625 * is delalloc or unwritten.
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001626 */
Jan Kara4e7ea812013-06-04 13:17:40 -04001627 struct ext4_map_blocks map;
1628 struct ext4_io_submit io_submit; /* IO submission data */
1629};
Alex Tomas64769242008-07-11 19:27:31 -04001630
Jan Kara4e7ea812013-06-04 13:17:40 -04001631static void mpage_release_unused_pages(struct mpage_da_data *mpd,
1632 bool invalidate)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001633{
1634 int nr_pages, i;
1635 pgoff_t index, end;
1636 struct pagevec pvec;
1637 struct inode *inode = mpd->inode;
1638 struct address_space *mapping = inode->i_mapping;
Jan Kara4e7ea812013-06-04 13:17:40 -04001639
1640 /* This is necessary when next_page == 0. */
1641 if (mpd->first_page >= mpd->next_page)
1642 return;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001643
Curt Wohlgemuthc7f59382011-02-26 12:27:52 -05001644 index = mpd->first_page;
1645 end = mpd->next_page - 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04001646 if (invalidate) {
1647 ext4_lblk_t start, last;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001648 start = index << (PAGE_SHIFT - inode->i_blkbits);
1649 last = end << (PAGE_SHIFT - inode->i_blkbits);
Jan Kara4e7ea812013-06-04 13:17:40 -04001650 ext4_es_remove_extent(inode, start, last - start + 1);
1651 }
Zheng Liu51865fd2012-11-08 21:57:32 -05001652
Eric Sandeen66bea922012-11-14 22:22:05 -05001653 pagevec_init(&pvec, 0);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001654 while (index <= end) {
1655 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1656 if (nr_pages == 0)
1657 break;
1658 for (i = 0; i < nr_pages; i++) {
1659 struct page *page = pvec.pages[i];
Jan Kara9b1d09982010-03-03 16:19:32 -05001660 if (page->index > end)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001661 break;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001662 BUG_ON(!PageLocked(page));
1663 BUG_ON(PageWriteback(page));
Jan Kara4e7ea812013-06-04 13:17:40 -04001664 if (invalidate) {
wangguang4e800c02016-09-15 11:32:46 -04001665 if (page_mapped(page))
1666 clear_page_dirty_for_io(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001667 block_invalidatepage(page, 0, PAGE_SIZE);
Jan Kara4e7ea812013-06-04 13:17:40 -04001668 ClearPageUptodate(page);
1669 }
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001670 unlock_page(page);
1671 }
Jan Kara9b1d09982010-03-03 16:19:32 -05001672 index = pvec.pages[nr_pages - 1]->index + 1;
1673 pagevec_release(&pvec);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001674 }
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001675}
1676
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001677static void ext4_print_free_blocks(struct inode *inode)
1678{
1679 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o92b97812012-03-19 23:41:49 -04001680 struct super_block *sb = inode->i_sb;
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001681 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'o92b97812012-03-19 23:41:49 -04001682
1683 ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld",
Theodore Ts'o5dee5432011-09-09 19:10:51 -04001684 EXT4_C2B(EXT4_SB(inode->i_sb),
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001685 ext4_count_free_clusters(sb)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001686 ext4_msg(sb, KERN_CRIT, "Free/Dirty block details");
1687 ext4_msg(sb, KERN_CRIT, "free_blocks=%lld",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001688 (long long) EXT4_C2B(EXT4_SB(sb),
Theodore Ts'o57042652011-09-09 18:56:51 -04001689 percpu_counter_sum(&sbi->s_freeclusters_counter)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001690 ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001691 (long long) EXT4_C2B(EXT4_SB(sb),
Aditya Kali7b415bf2011-09-09 19:04:51 -04001692 percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001693 ext4_msg(sb, KERN_CRIT, "Block reservation details");
1694 ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001695 ei->i_reserved_data_blocks);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001696 return;
1697}
1698
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001699static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001700{
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001701 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001702}
1703
Alex Tomas64769242008-07-11 19:27:31 -04001704/*
Aditya Kali5356f262011-09-09 19:20:51 -04001705 * This function is grabs code from the very beginning of
1706 * ext4_map_blocks, but assumes that the caller is from delayed write
1707 * time. This function looks up the requested blocks and sets the
1708 * buffer delay bit under the protection of i_data_sem.
1709 */
1710static int ext4_da_map_blocks(struct inode *inode, sector_t iblock,
1711 struct ext4_map_blocks *map,
1712 struct buffer_head *bh)
1713{
Zheng Liud100eef2013-02-18 00:29:59 -05001714 struct extent_status es;
Aditya Kali5356f262011-09-09 19:20:51 -04001715 int retval;
1716 sector_t invalid_block = ~((sector_t) 0xffff);
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001717#ifdef ES_AGGRESSIVE_TEST
1718 struct ext4_map_blocks orig_map;
1719
1720 memcpy(&orig_map, map, sizeof(*map));
1721#endif
Aditya Kali5356f262011-09-09 19:20:51 -04001722
1723 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
1724 invalid_block = ~0;
1725
1726 map->m_flags = 0;
1727 ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
1728 "logical block %lu\n", inode->i_ino, map->m_len,
1729 (unsigned long) map->m_lblk);
Zheng Liud100eef2013-02-18 00:29:59 -05001730
1731 /* Lookup extent status tree firstly */
1732 if (ext4_es_lookup_extent(inode, iblock, &es)) {
Zheng Liud100eef2013-02-18 00:29:59 -05001733 if (ext4_es_is_hole(&es)) {
1734 retval = 0;
Lukas Czernerc8b459f2014-05-12 12:55:07 -04001735 down_read(&EXT4_I(inode)->i_data_sem);
Zheng Liud100eef2013-02-18 00:29:59 -05001736 goto add_delayed;
1737 }
1738
1739 /*
1740 * Delayed extent could be allocated by fallocate.
1741 * So we need to check it.
1742 */
1743 if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) {
1744 map_bh(bh, inode->i_sb, invalid_block);
1745 set_buffer_new(bh);
1746 set_buffer_delay(bh);
1747 return 0;
1748 }
1749
1750 map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk;
1751 retval = es.es_len - (iblock - es.es_lblk);
1752 if (retval > map->m_len)
1753 retval = map->m_len;
1754 map->m_len = retval;
1755 if (ext4_es_is_written(&es))
1756 map->m_flags |= EXT4_MAP_MAPPED;
1757 else if (ext4_es_is_unwritten(&es))
1758 map->m_flags |= EXT4_MAP_UNWRITTEN;
1759 else
1760 BUG_ON(1);
1761
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001762#ifdef ES_AGGRESSIVE_TEST
1763 ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0);
1764#endif
Zheng Liud100eef2013-02-18 00:29:59 -05001765 return retval;
1766 }
1767
Aditya Kali5356f262011-09-09 19:20:51 -04001768 /*
1769 * Try to see if we can get the block without requesting a new
1770 * file system block.
1771 */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04001772 down_read(&EXT4_I(inode)->i_data_sem);
Jan Karacbd75842014-11-25 11:41:49 -05001773 if (ext4_has_inline_data(inode))
Tao Ma9c3569b2012-12-10 14:05:57 -05001774 retval = 0;
Jan Karacbd75842014-11-25 11:41:49 -05001775 else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Zheng Liu2f8e0a72014-11-25 11:44:37 -05001776 retval = ext4_ext_map_blocks(NULL, inode, map, 0);
Aditya Kali5356f262011-09-09 19:20:51 -04001777 else
Zheng Liu2f8e0a72014-11-25 11:44:37 -05001778 retval = ext4_ind_map_blocks(NULL, inode, map, 0);
Aditya Kali5356f262011-09-09 19:20:51 -04001779
Zheng Liud100eef2013-02-18 00:29:59 -05001780add_delayed:
Aditya Kali5356f262011-09-09 19:20:51 -04001781 if (retval == 0) {
Zheng Liuf7fec032013-02-18 00:28:47 -05001782 int ret;
Aditya Kali5356f262011-09-09 19:20:51 -04001783 /*
1784 * XXX: __block_prepare_write() unmaps passed block,
1785 * is it OK?
1786 */
Lukas Czerner386ad672013-03-10 22:50:00 -04001787 /*
1788 * If the block was allocated from previously allocated cluster,
1789 * then we don't need to reserve it again. However we still need
1790 * to reserve metadata for every block we're going to write.
1791 */
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001792 if (EXT4_SB(inode->i_sb)->s_cluster_ratio == 1 ||
Jan Karacbd75842014-11-25 11:41:49 -05001793 !ext4_find_delalloc_cluster(inode, map->m_lblk)) {
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001794 ret = ext4_da_reserve_space(inode);
Zheng Liuf7fec032013-02-18 00:28:47 -05001795 if (ret) {
Aditya Kali5356f262011-09-09 19:20:51 -04001796 /* not enough space to reserve */
Zheng Liuf7fec032013-02-18 00:28:47 -05001797 retval = ret;
Aditya Kali5356f262011-09-09 19:20:51 -04001798 goto out_unlock;
Zheng Liuf7fec032013-02-18 00:28:47 -05001799 }
Aditya Kali5356f262011-09-09 19:20:51 -04001800 }
1801
Zheng Liuf7fec032013-02-18 00:28:47 -05001802 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
1803 ~0, EXTENT_STATUS_DELAYED);
1804 if (ret) {
1805 retval = ret;
Zheng Liu51865fd2012-11-08 21:57:32 -05001806 goto out_unlock;
Zheng Liuf7fec032013-02-18 00:28:47 -05001807 }
Zheng Liu51865fd2012-11-08 21:57:32 -05001808
Aditya Kali5356f262011-09-09 19:20:51 -04001809 map_bh(bh, inode->i_sb, invalid_block);
1810 set_buffer_new(bh);
1811 set_buffer_delay(bh);
Zheng Liuf7fec032013-02-18 00:28:47 -05001812 } else if (retval > 0) {
1813 int ret;
Theodore Ts'o3be78c72013-08-16 21:22:41 -04001814 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -05001815
Zheng Liu44fb851d2013-07-29 12:51:42 -04001816 if (unlikely(retval != map->m_len)) {
1817 ext4_warning(inode->i_sb,
1818 "ES len assertion failed for inode "
1819 "%lu: retval %d != map->m_len %d",
1820 inode->i_ino, retval, map->m_len);
1821 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001822 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001823
Zheng Liuf7fec032013-02-18 00:28:47 -05001824 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
1825 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
1826 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
1827 map->m_pblk, status);
1828 if (ret != 0)
1829 retval = ret;
Aditya Kali5356f262011-09-09 19:20:51 -04001830 }
1831
1832out_unlock:
1833 up_read((&EXT4_I(inode)->i_data_sem));
1834
1835 return retval;
1836}
1837
1838/*
Seunghun Leed91bd2c2014-09-01 22:15:30 -04001839 * This is a special get_block_t callback which is used by
Theodore Ts'ob920c752009-05-14 00:54:29 -04001840 * ext4_da_write_begin(). It will either return mapped block or
1841 * reserve space for a single block.
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001842 *
1843 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
1844 * We also have b_blocknr = -1 and b_bdev initialized properly
1845 *
1846 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
1847 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
1848 * initialized properly.
Alex Tomas64769242008-07-11 19:27:31 -04001849 */
Tao Ma9c3569b2012-12-10 14:05:57 -05001850int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
1851 struct buffer_head *bh, int create)
Alex Tomas64769242008-07-11 19:27:31 -04001852{
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001853 struct ext4_map_blocks map;
Alex Tomas64769242008-07-11 19:27:31 -04001854 int ret = 0;
1855
1856 BUG_ON(create == 0);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001857 BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
1858
1859 map.m_lblk = iblock;
1860 map.m_len = 1;
Alex Tomas64769242008-07-11 19:27:31 -04001861
1862 /*
1863 * first, we need to know whether the block is allocated already
1864 * preallocated blocks are unmapped but should treated
1865 * the same as allocated blocks.
1866 */
Aditya Kali5356f262011-09-09 19:20:51 -04001867 ret = ext4_da_map_blocks(inode, iblock, &map, bh);
1868 if (ret <= 0)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001869 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04001870
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001871 map_bh(bh, inode->i_sb, map.m_pblk);
Jan Karaed8ad832016-02-19 00:18:25 -05001872 ext4_update_bh_state(bh, map.m_flags);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001873
1874 if (buffer_unwritten(bh)) {
1875 /* A delayed write to unwritten bh should be marked
1876 * new and mapped. Mapped ensures that we don't do
1877 * get_block multiple times when we write to the same
1878 * offset and new ensures that we do proper zero out
1879 * for partial write.
1880 */
1881 set_buffer_new(bh);
Theodore Ts'oc8205632011-04-10 22:30:07 -04001882 set_buffer_mapped(bh);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001883 }
1884 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04001885}
Mingming Cao61628a32008-07-11 19:27:31 -04001886
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001887static int bget_one(handle_t *handle, struct buffer_head *bh)
1888{
1889 get_bh(bh);
1890 return 0;
1891}
1892
1893static int bput_one(handle_t *handle, struct buffer_head *bh)
1894{
1895 put_bh(bh);
1896 return 0;
1897}
1898
1899static int __ext4_journalled_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001900 unsigned int len)
1901{
1902 struct address_space *mapping = page->mapping;
1903 struct inode *inode = mapping->host;
Tao Ma3fdcfb62012-12-10 14:05:57 -05001904 struct buffer_head *page_bufs = NULL;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001905 handle_t *handle = NULL;
Tao Ma3fdcfb62012-12-10 14:05:57 -05001906 int ret = 0, err = 0;
1907 int inline_data = ext4_has_inline_data(inode);
1908 struct buffer_head *inode_bh = NULL;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001909
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001910 ClearPageChecked(page);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001911
1912 if (inline_data) {
1913 BUG_ON(page->index != 0);
1914 BUG_ON(len > ext4_get_max_inline_size(inode));
1915 inode_bh = ext4_journalled_write_inline_data(inode, len, page);
1916 if (inode_bh == NULL)
1917 goto out;
1918 } else {
1919 page_bufs = page_buffers(page);
1920 if (!page_bufs) {
1921 BUG();
1922 goto out;
1923 }
1924 ext4_walk_page_buffers(handle, page_bufs, 0, len,
1925 NULL, bget_one);
1926 }
Theodore Ts'obdf96832015-06-12 23:45:33 -04001927 /*
1928 * We need to release the page lock before we start the
1929 * journal, so grab a reference so the page won't disappear
1930 * out from under us.
1931 */
1932 get_page(page);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001933 unlock_page(page);
1934
Theodore Ts'o9924a922013-02-08 21:59:22 -05001935 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
1936 ext4_writepage_trans_blocks(inode));
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001937 if (IS_ERR(handle)) {
1938 ret = PTR_ERR(handle);
Theodore Ts'obdf96832015-06-12 23:45:33 -04001939 put_page(page);
1940 goto out_no_pagelock;
1941 }
1942 BUG_ON(!ext4_handle_valid(handle));
1943
1944 lock_page(page);
1945 put_page(page);
1946 if (page->mapping != mapping) {
1947 /* The page got truncated from under us */
1948 ext4_journal_stop(handle);
1949 ret = 0;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001950 goto out;
1951 }
1952
Tao Ma3fdcfb62012-12-10 14:05:57 -05001953 if (inline_data) {
liang xie5d601252014-05-12 22:06:43 -04001954 BUFFER_TRACE(inode_bh, "get write access");
Tao Ma3fdcfb62012-12-10 14:05:57 -05001955 ret = ext4_journal_get_write_access(handle, inode_bh);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001956
Tao Ma3fdcfb62012-12-10 14:05:57 -05001957 err = ext4_handle_dirty_metadata(handle, inode, inode_bh);
1958
1959 } else {
1960 ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
1961 do_journal_get_write_access);
1962
1963 err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
1964 write_end_fn);
1965 }
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001966 if (ret == 0)
1967 ret = err;
Jan Kara2d859db2011-07-26 09:07:11 -04001968 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001969 err = ext4_journal_stop(handle);
1970 if (!ret)
1971 ret = err;
1972
Tao Ma3fdcfb62012-12-10 14:05:57 -05001973 if (!ext4_has_inline_data(inode))
Theodore Ts'o8c9367f2014-01-07 13:08:03 -05001974 ext4_walk_page_buffers(NULL, page_bufs, 0, len,
Tao Ma3fdcfb62012-12-10 14:05:57 -05001975 NULL, bput_one);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001976 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001977out:
Theodore Ts'obdf96832015-06-12 23:45:33 -04001978 unlock_page(page);
1979out_no_pagelock:
Tao Ma3fdcfb62012-12-10 14:05:57 -05001980 brelse(inode_bh);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001981 return ret;
1982}
1983
Mingming Cao61628a32008-07-11 19:27:31 -04001984/*
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001985 * Note that we don't need to start a transaction unless we're journaling data
1986 * because we should have holes filled from ext4_page_mkwrite(). We even don't
1987 * need to file the inode to the transaction's list in ordered mode because if
1988 * we are writing back data added by write(), the inode is already there and if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001989 * we are writing back data modified via mmap(), no one guarantees in which
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001990 * transaction the data will hit the disk. In case we are journaling data, we
1991 * cannot start transaction directly because transaction start ranks above page
1992 * lock so we have to do some magic.
1993 *
Theodore Ts'ob920c752009-05-14 00:54:29 -04001994 * This function can get called via...
Theodore Ts'o20970ba2013-06-06 14:00:46 -04001995 * - ext4_writepages after taking page lock (have journal handle)
Theodore Ts'ob920c752009-05-14 00:54:29 -04001996 * - journal_submit_inode_data_buffers (no journal handle)
Artem Bityutskiyf6463b02012-07-25 18:12:04 +03001997 * - shrink_page_list via the kswapd/direct reclaim (no journal handle)
Theodore Ts'ob920c752009-05-14 00:54:29 -04001998 * - grab_page_cache when doing write_begin (have journal handle)
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001999 *
2000 * We don't do any block allocation in this function. If we have page with
2001 * multiple blocks we need to write those buffer_heads that are mapped. This
2002 * is important for mmaped based write. So if we do with blocksize 1K
2003 * truncate(f, 1024);
2004 * a = mmap(f, 0, 4096);
2005 * a[0] = 'a';
2006 * truncate(f, 4096);
2007 * we have in the page first buffer_head mapped via page_mkwrite call back
Paul Bolle90802ed2011-12-05 13:00:34 +01002008 * but other buffer_heads would be unmapped but dirty (dirty done via the
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002009 * do_wp_page). So writepage should write the first block. If we modify
2010 * the mmap area beyond 1024 we will again get a page_fault and the
2011 * page_mkwrite callback will do the block allocation and mark the
2012 * buffer_heads mapped.
2013 *
2014 * We redirty the page if we have any buffer_heads that is either delay or
2015 * unwritten in the page.
2016 *
2017 * We can get recursively called as show below.
2018 *
2019 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
2020 * ext4_writepage()
2021 *
2022 * But since we don't do any block allocation we should not deadlock.
2023 * Page also have the dirty flag cleared so we don't get recurive page_lock.
Mingming Cao61628a32008-07-11 19:27:31 -04002024 */
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002025static int ext4_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002026 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002027{
Jan Karaf8bec372013-01-28 12:55:08 -05002028 int ret = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002029 loff_t size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002030 unsigned int len;
Jiaying Zhang744692d2010-03-04 16:14:02 -05002031 struct buffer_head *page_bufs = NULL;
Mingming Cao61628a32008-07-11 19:27:31 -04002032 struct inode *inode = page->mapping->host;
Jan Kara36ade452013-01-28 09:30:52 -05002033 struct ext4_io_submit io_submit;
Namjae Jeon1c8349a2014-05-12 08:12:25 -04002034 bool keep_towrite = false;
Alex Tomas64769242008-07-11 19:27:31 -04002035
Lukas Czernera9c667f2011-06-06 09:51:52 -04002036 trace_ext4_writepage(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002037 size = i_size_read(inode);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002038 if (page->index == size >> PAGE_SHIFT)
2039 len = size & ~PAGE_MASK;
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002040 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002041 len = PAGE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04002042
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002043 page_bufs = page_buffers(page);
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04002044 /*
Jan Karafe386132013-01-28 21:06:42 -05002045 * We cannot do block allocation or other extent handling in this
2046 * function. If there are buffers needing that, we have to redirty
2047 * the page. But we may reach here when we do a journal commit via
2048 * journal_submit_inode_data_buffers() and in that case we must write
2049 * allocated buffers to achieve data=ordered mode guarantees.
Theodore Ts'occcd1472015-10-03 10:49:23 -04002050 *
2051 * Also, if there is only one buffer per page (the fs block
2052 * size == the page size), if one buffer needs block
2053 * allocation or needs to modify the extent tree to clear the
2054 * unwritten flag, we know that the page can't be written at
2055 * all, so we might as well refuse the write immediately.
2056 * Unfortunately if the block size != page size, we can't as
2057 * easily detect this case using ext4_walk_page_buffers(), but
2058 * for the extremely common case, this is an optimization that
2059 * skips a useless round trip through ext4_bio_write_page().
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04002060 */
Tao Maf19d5872012-12-10 14:05:51 -05002061 if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL,
2062 ext4_bh_delay_or_unwritten)) {
Jan Karaf8bec372013-01-28 12:55:08 -05002063 redirty_page_for_writepage(wbc, page);
Theodore Ts'occcd1472015-10-03 10:49:23 -04002064 if ((current->flags & PF_MEMALLOC) ||
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002065 (inode->i_sb->s_blocksize == PAGE_SIZE)) {
Jan Karafe386132013-01-28 21:06:42 -05002066 /*
2067 * For memory cleaning there's no point in writing only
2068 * some buffers. So just bail out. Warn if we came here
2069 * from direct reclaim.
2070 */
2071 WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD))
2072 == PF_MEMALLOC);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002073 unlock_page(page);
2074 return 0;
2075 }
Namjae Jeon1c8349a2014-05-12 08:12:25 -04002076 keep_towrite = true;
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002077 }
Alex Tomas64769242008-07-11 19:27:31 -04002078
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002079 if (PageChecked(page) && ext4_should_journal_data(inode))
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002080 /*
2081 * It's mmapped pagecache. Add buffers and journal it. There
2082 * doesn't seem much point in redirtying the page here.
2083 */
Wu Fengguang3f0ca302009-11-24 11:15:44 -05002084 return __ext4_journalled_writepage(page, len);
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002085
Jan Kara97a851e2013-06-04 11:58:58 -04002086 ext4_io_submit_init(&io_submit, wbc);
2087 io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS);
2088 if (!io_submit.io_end) {
2089 redirty_page_for_writepage(wbc, page);
2090 unlock_page(page);
2091 return -ENOMEM;
2092 }
Namjae Jeon1c8349a2014-05-12 08:12:25 -04002093 ret = ext4_bio_write_page(&io_submit, page, len, wbc, keep_towrite);
Jan Kara36ade452013-01-28 09:30:52 -05002094 ext4_io_submit(&io_submit);
Jan Kara97a851e2013-06-04 11:58:58 -04002095 /* Drop io_end reference we got from init */
2096 ext4_put_io_end_defer(io_submit.io_end);
Alex Tomas64769242008-07-11 19:27:31 -04002097 return ret;
2098}
2099
Jan Kara5f1132b2013-08-17 10:02:33 -04002100static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page)
2101{
2102 int len;
2103 loff_t size = i_size_read(mpd->inode);
2104 int err;
2105
2106 BUG_ON(page->index != mpd->first_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002107 if (page->index == size >> PAGE_SHIFT)
2108 len = size & ~PAGE_MASK;
Jan Kara5f1132b2013-08-17 10:02:33 -04002109 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002110 len = PAGE_SIZE;
Jan Kara5f1132b2013-08-17 10:02:33 -04002111 clear_page_dirty_for_io(page);
Namjae Jeon1c8349a2014-05-12 08:12:25 -04002112 err = ext4_bio_write_page(&mpd->io_submit, page, len, mpd->wbc, false);
Jan Kara5f1132b2013-08-17 10:02:33 -04002113 if (!err)
2114 mpd->wbc->nr_to_write--;
2115 mpd->first_page++;
2116
2117 return err;
2118}
2119
Jan Kara4e7ea812013-06-04 13:17:40 -04002120#define BH_FLAGS ((1 << BH_Unwritten) | (1 << BH_Delay))
2121
Mingming Cao61628a32008-07-11 19:27:31 -04002122/*
Jan Karafffb2732013-06-04 13:01:11 -04002123 * mballoc gives us at most this number of blocks...
2124 * XXX: That seems to be only a limitation of ext4_mb_normalize_request().
Anatol Pomozov70261f52013-08-28 14:40:12 -04002125 * The rest of mballoc seems to handle chunks up to full group size.
Mingming Cao61628a32008-07-11 19:27:31 -04002126 */
Jan Karafffb2732013-06-04 13:01:11 -04002127#define MAX_WRITEPAGES_EXTENT_LEN 2048
Mingming Cao525f4ed2008-08-19 22:15:58 -04002128
Jan Karafffb2732013-06-04 13:01:11 -04002129/*
Jan Kara4e7ea812013-06-04 13:17:40 -04002130 * mpage_add_bh_to_extent - try to add bh to extent of blocks to map
2131 *
2132 * @mpd - extent of blocks
2133 * @lblk - logical number of the block in the file
Jan Kara09930042013-08-17 09:57:56 -04002134 * @bh - buffer head we want to add to the extent
Jan Kara4e7ea812013-06-04 13:17:40 -04002135 *
Jan Kara09930042013-08-17 09:57:56 -04002136 * The function is used to collect contig. blocks in the same state. If the
2137 * buffer doesn't require mapping for writeback and we haven't started the
2138 * extent of buffers to map yet, the function returns 'true' immediately - the
2139 * caller can write the buffer right away. Otherwise the function returns true
2140 * if the block has been added to the extent, false if the block couldn't be
2141 * added.
Jan Kara4e7ea812013-06-04 13:17:40 -04002142 */
Jan Kara09930042013-08-17 09:57:56 -04002143static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk,
2144 struct buffer_head *bh)
Jan Kara4e7ea812013-06-04 13:17:40 -04002145{
2146 struct ext4_map_blocks *map = &mpd->map;
2147
Jan Kara09930042013-08-17 09:57:56 -04002148 /* Buffer that doesn't need mapping for writeback? */
2149 if (!buffer_dirty(bh) || !buffer_mapped(bh) ||
2150 (!buffer_delay(bh) && !buffer_unwritten(bh))) {
2151 /* So far no extent to map => we write the buffer right away */
2152 if (map->m_len == 0)
2153 return true;
2154 return false;
2155 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002156
2157 /* First block in the extent? */
2158 if (map->m_len == 0) {
2159 map->m_lblk = lblk;
2160 map->m_len = 1;
Jan Kara09930042013-08-17 09:57:56 -04002161 map->m_flags = bh->b_state & BH_FLAGS;
2162 return true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002163 }
2164
Jan Kara09930042013-08-17 09:57:56 -04002165 /* Don't go larger than mballoc is willing to allocate */
2166 if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN)
2167 return false;
2168
Jan Kara4e7ea812013-06-04 13:17:40 -04002169 /* Can we merge the block to our big extent? */
2170 if (lblk == map->m_lblk + map->m_len &&
Jan Kara09930042013-08-17 09:57:56 -04002171 (bh->b_state & BH_FLAGS) == map->m_flags) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002172 map->m_len++;
Jan Kara09930042013-08-17 09:57:56 -04002173 return true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002174 }
Jan Kara09930042013-08-17 09:57:56 -04002175 return false;
Jan Kara4e7ea812013-06-04 13:17:40 -04002176}
2177
Jan Kara5f1132b2013-08-17 10:02:33 -04002178/*
2179 * mpage_process_page_bufs - submit page buffers for IO or add them to extent
2180 *
2181 * @mpd - extent of blocks for mapping
2182 * @head - the first buffer in the page
2183 * @bh - buffer we should start processing from
2184 * @lblk - logical number of the block in the file corresponding to @bh
2185 *
2186 * Walk through page buffers from @bh upto @head (exclusive) and either submit
2187 * the page for IO if all buffers in this page were mapped and there's no
2188 * accumulated extent of buffers to map or add buffers in the page to the
2189 * extent of buffers to map. The function returns 1 if the caller can continue
2190 * by processing the next page, 0 if it should stop adding buffers to the
2191 * extent to map because we cannot extend it anymore. It can also return value
2192 * < 0 in case of error during IO submission.
2193 */
2194static int mpage_process_page_bufs(struct mpage_da_data *mpd,
2195 struct buffer_head *head,
2196 struct buffer_head *bh,
2197 ext4_lblk_t lblk)
Jan Kara4e7ea812013-06-04 13:17:40 -04002198{
2199 struct inode *inode = mpd->inode;
Jan Kara5f1132b2013-08-17 10:02:33 -04002200 int err;
Jan Kara4e7ea812013-06-04 13:17:40 -04002201 ext4_lblk_t blocks = (i_size_read(inode) + (1 << inode->i_blkbits) - 1)
2202 >> inode->i_blkbits;
2203
2204 do {
2205 BUG_ON(buffer_locked(bh));
2206
Jan Kara09930042013-08-17 09:57:56 -04002207 if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002208 /* Found extent to map? */
2209 if (mpd->map.m_len)
Jan Kara5f1132b2013-08-17 10:02:33 -04002210 return 0;
Jan Kara09930042013-08-17 09:57:56 -04002211 /* Everything mapped so far and we hit EOF */
Jan Kara5f1132b2013-08-17 10:02:33 -04002212 break;
Jan Kara4e7ea812013-06-04 13:17:40 -04002213 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002214 } while (lblk++, (bh = bh->b_this_page) != head);
Jan Kara5f1132b2013-08-17 10:02:33 -04002215 /* So far everything mapped? Submit the page for IO. */
2216 if (mpd->map.m_len == 0) {
2217 err = mpage_submit_page(mpd, head->b_page);
2218 if (err < 0)
2219 return err;
2220 }
2221 return lblk < blocks;
Jan Kara4e7ea812013-06-04 13:17:40 -04002222}
2223
2224/*
2225 * mpage_map_buffers - update buffers corresponding to changed extent and
2226 * submit fully mapped pages for IO
2227 *
2228 * @mpd - description of extent to map, on return next extent to map
2229 *
2230 * Scan buffers corresponding to changed extent (we expect corresponding pages
2231 * to be already locked) and update buffer state according to new extent state.
2232 * We map delalloc buffers to their physical location, clear unwritten bits,
Lukas Czerner556615d2014-04-20 23:45:47 -04002233 * and mark buffers as uninit when we perform writes to unwritten extents
Jan Kara4e7ea812013-06-04 13:17:40 -04002234 * and do extent conversion after IO is finished. If the last page is not fully
2235 * mapped, we update @map to the next extent in the last page that needs
2236 * mapping. Otherwise we submit the page for IO.
2237 */
2238static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd)
2239{
2240 struct pagevec pvec;
2241 int nr_pages, i;
2242 struct inode *inode = mpd->inode;
2243 struct buffer_head *head, *bh;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002244 int bpp_bits = PAGE_SHIFT - inode->i_blkbits;
Jan Kara4e7ea812013-06-04 13:17:40 -04002245 pgoff_t start, end;
2246 ext4_lblk_t lblk;
2247 sector_t pblock;
2248 int err;
2249
2250 start = mpd->map.m_lblk >> bpp_bits;
2251 end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits;
2252 lblk = start << bpp_bits;
2253 pblock = mpd->map.m_pblk;
2254
2255 pagevec_init(&pvec, 0);
2256 while (start <= end) {
2257 nr_pages = pagevec_lookup(&pvec, inode->i_mapping, start,
2258 PAGEVEC_SIZE);
2259 if (nr_pages == 0)
2260 break;
2261 for (i = 0; i < nr_pages; i++) {
2262 struct page *page = pvec.pages[i];
2263
2264 if (page->index > end)
2265 break;
Anatol Pomozov70261f52013-08-28 14:40:12 -04002266 /* Up to 'end' pages must be contiguous */
Jan Kara4e7ea812013-06-04 13:17:40 -04002267 BUG_ON(page->index != start);
2268 bh = head = page_buffers(page);
2269 do {
2270 if (lblk < mpd->map.m_lblk)
2271 continue;
2272 if (lblk >= mpd->map.m_lblk + mpd->map.m_len) {
2273 /*
2274 * Buffer after end of mapped extent.
2275 * Find next buffer in the page to map.
2276 */
2277 mpd->map.m_len = 0;
2278 mpd->map.m_flags = 0;
Jan Kara5f1132b2013-08-17 10:02:33 -04002279 /*
2280 * FIXME: If dioread_nolock supports
2281 * blocksize < pagesize, we need to make
2282 * sure we add size mapped so far to
2283 * io_end->size as the following call
2284 * can submit the page for IO.
2285 */
2286 err = mpage_process_page_bufs(mpd, head,
2287 bh, lblk);
Jan Kara4e7ea812013-06-04 13:17:40 -04002288 pagevec_release(&pvec);
Jan Kara5f1132b2013-08-17 10:02:33 -04002289 if (err > 0)
2290 err = 0;
2291 return err;
Jan Kara4e7ea812013-06-04 13:17:40 -04002292 }
2293 if (buffer_delay(bh)) {
2294 clear_buffer_delay(bh);
2295 bh->b_blocknr = pblock++;
2296 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002297 clear_buffer_unwritten(bh);
Jan Kara5f1132b2013-08-17 10:02:33 -04002298 } while (lblk++, (bh = bh->b_this_page) != head);
Jan Kara4e7ea812013-06-04 13:17:40 -04002299
2300 /*
2301 * FIXME: This is going to break if dioread_nolock
2302 * supports blocksize < pagesize as we will try to
2303 * convert potentially unmapped parts of inode.
2304 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002305 mpd->io_submit.io_end->size += PAGE_SIZE;
Jan Kara4e7ea812013-06-04 13:17:40 -04002306 /* Page fully mapped - let IO run! */
2307 err = mpage_submit_page(mpd, page);
2308 if (err < 0) {
2309 pagevec_release(&pvec);
2310 return err;
2311 }
2312 start++;
2313 }
2314 pagevec_release(&pvec);
2315 }
2316 /* Extent fully mapped and matches with page boundary. We are done. */
2317 mpd->map.m_len = 0;
2318 mpd->map.m_flags = 0;
2319 return 0;
2320}
2321
2322static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd)
2323{
2324 struct inode *inode = mpd->inode;
2325 struct ext4_map_blocks *map = &mpd->map;
2326 int get_blocks_flags;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002327 int err, dioread_nolock;
Jan Kara4e7ea812013-06-04 13:17:40 -04002328
2329 trace_ext4_da_write_pages_extent(inode, map);
2330 /*
2331 * Call ext4_map_blocks() to allocate any delayed allocation blocks, or
Lukas Czerner556615d2014-04-20 23:45:47 -04002332 * to convert an unwritten extent to be initialized (in the case
Jan Kara4e7ea812013-06-04 13:17:40 -04002333 * where we have written into one or more preallocated blocks). It is
2334 * possible that we're going to need more metadata blocks than
2335 * previously reserved. However we must not fail because we're in
2336 * writeback and there is nothing we can do about it so it might result
2337 * in data loss. So use reserved blocks to allocate metadata if
2338 * possible.
2339 *
Theodore Ts'o754cfed2014-09-04 18:08:22 -04002340 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if
2341 * the blocks in question are delalloc blocks. This indicates
2342 * that the blocks and quotas has already been checked when
2343 * the data was copied into the page cache.
Jan Kara4e7ea812013-06-04 13:17:40 -04002344 */
2345 get_blocks_flags = EXT4_GET_BLOCKS_CREATE |
Jan Karaee0876b2016-04-24 00:56:08 -04002346 EXT4_GET_BLOCKS_METADATA_NOFAIL |
2347 EXT4_GET_BLOCKS_IO_SUBMIT;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002348 dioread_nolock = ext4_should_dioread_nolock(inode);
2349 if (dioread_nolock)
Jan Kara4e7ea812013-06-04 13:17:40 -04002350 get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
2351 if (map->m_flags & (1 << BH_Delay))
2352 get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
2353
2354 err = ext4_map_blocks(handle, inode, map, get_blocks_flags);
2355 if (err < 0)
2356 return err;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002357 if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) {
Jan Kara6b523df2013-06-04 13:21:11 -04002358 if (!mpd->io_submit.io_end->handle &&
2359 ext4_handle_valid(handle)) {
2360 mpd->io_submit.io_end->handle = handle->h_rsv_handle;
2361 handle->h_rsv_handle = NULL;
2362 }
Jan Kara3613d222013-06-04 13:19:34 -04002363 ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end);
Jan Kara6b523df2013-06-04 13:21:11 -04002364 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002365
2366 BUG_ON(map->m_len == 0);
2367 if (map->m_flags & EXT4_MAP_NEW) {
2368 struct block_device *bdev = inode->i_sb->s_bdev;
2369 int i;
2370
2371 for (i = 0; i < map->m_len; i++)
2372 unmap_underlying_metadata(bdev, map->m_pblk + i);
2373 }
2374 return 0;
2375}
2376
2377/*
2378 * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length
2379 * mpd->len and submit pages underlying it for IO
2380 *
2381 * @handle - handle for journal operations
2382 * @mpd - extent to map
Jan Kara7534e852013-10-16 08:26:08 -04002383 * @give_up_on_write - we set this to true iff there is a fatal error and there
2384 * is no hope of writing the data. The caller should discard
2385 * dirty pages to avoid infinite loops.
Jan Kara4e7ea812013-06-04 13:17:40 -04002386 *
2387 * The function maps extent starting at mpd->lblk of length mpd->len. If it is
2388 * delayed, blocks are allocated, if it is unwritten, we may need to convert
2389 * them to initialized or split the described range from larger unwritten
2390 * extent. Note that we need not map all the described range since allocation
2391 * can return less blocks or the range is covered by more unwritten extents. We
2392 * cannot map more because we are limited by reserved transaction credits. On
2393 * the other hand we always make sure that the last touched page is fully
2394 * mapped so that it can be written out (and thus forward progress is
2395 * guaranteed). After mapping we submit all mapped pages for IO.
2396 */
2397static int mpage_map_and_submit_extent(handle_t *handle,
Theodore Ts'ocb530542013-07-01 08:12:40 -04002398 struct mpage_da_data *mpd,
2399 bool *give_up_on_write)
Jan Kara4e7ea812013-06-04 13:17:40 -04002400{
2401 struct inode *inode = mpd->inode;
2402 struct ext4_map_blocks *map = &mpd->map;
2403 int err;
2404 loff_t disksize;
Dmitry Monakhov66031202014-08-27 18:40:03 -04002405 int progress = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002406
2407 mpd->io_submit.io_end->offset =
2408 ((loff_t)map->m_lblk) << inode->i_blkbits;
Jan Kara27d7c4e2013-07-05 21:57:22 -04002409 do {
Jan Kara4e7ea812013-06-04 13:17:40 -04002410 err = mpage_map_one_extent(handle, mpd);
2411 if (err < 0) {
2412 struct super_block *sb = inode->i_sb;
2413
Theodore Ts'ocb530542013-07-01 08:12:40 -04002414 if (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)
2415 goto invalidate_dirty_pages;
Jan Kara4e7ea812013-06-04 13:17:40 -04002416 /*
Theodore Ts'ocb530542013-07-01 08:12:40 -04002417 * Let the uper layers retry transient errors.
2418 * In the case of ENOSPC, if ext4_count_free_blocks()
2419 * is non-zero, a commit should free up blocks.
Jan Kara4e7ea812013-06-04 13:17:40 -04002420 */
Theodore Ts'ocb530542013-07-01 08:12:40 -04002421 if ((err == -ENOMEM) ||
Dmitry Monakhov66031202014-08-27 18:40:03 -04002422 (err == -ENOSPC && ext4_count_free_clusters(sb))) {
2423 if (progress)
2424 goto update_disksize;
Theodore Ts'ocb530542013-07-01 08:12:40 -04002425 return err;
Dmitry Monakhov66031202014-08-27 18:40:03 -04002426 }
Theodore Ts'ocb530542013-07-01 08:12:40 -04002427 ext4_msg(sb, KERN_CRIT,
2428 "Delayed block allocation failed for "
2429 "inode %lu at logical offset %llu with"
2430 " max blocks %u with error %d",
2431 inode->i_ino,
2432 (unsigned long long)map->m_lblk,
2433 (unsigned)map->m_len, -err);
2434 ext4_msg(sb, KERN_CRIT,
2435 "This should not happen!! Data will "
2436 "be lost\n");
2437 if (err == -ENOSPC)
2438 ext4_print_free_blocks(inode);
2439 invalidate_dirty_pages:
2440 *give_up_on_write = true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002441 return err;
2442 }
Dmitry Monakhov66031202014-08-27 18:40:03 -04002443 progress = 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04002444 /*
2445 * Update buffer state, submit mapped pages, and get us new
2446 * extent to map
2447 */
2448 err = mpage_map_and_submit_buffers(mpd);
2449 if (err < 0)
Dmitry Monakhov66031202014-08-27 18:40:03 -04002450 goto update_disksize;
Jan Kara27d7c4e2013-07-05 21:57:22 -04002451 } while (map->m_len);
Jan Kara4e7ea812013-06-04 13:17:40 -04002452
Dmitry Monakhov66031202014-08-27 18:40:03 -04002453update_disksize:
Theodore Ts'o622cad12014-04-11 10:35:17 -04002454 /*
2455 * Update on-disk size after IO is submitted. Races with
2456 * truncate are avoided by checking i_size under i_data_sem.
2457 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002458 disksize = ((loff_t)mpd->first_page) << PAGE_SHIFT;
Jan Kara4e7ea812013-06-04 13:17:40 -04002459 if (disksize > EXT4_I(inode)->i_disksize) {
2460 int err2;
Theodore Ts'o622cad12014-04-11 10:35:17 -04002461 loff_t i_size;
Jan Kara4e7ea812013-06-04 13:17:40 -04002462
Theodore Ts'o622cad12014-04-11 10:35:17 -04002463 down_write(&EXT4_I(inode)->i_data_sem);
2464 i_size = i_size_read(inode);
2465 if (disksize > i_size)
2466 disksize = i_size;
2467 if (disksize > EXT4_I(inode)->i_disksize)
2468 EXT4_I(inode)->i_disksize = disksize;
Jan Kara4e7ea812013-06-04 13:17:40 -04002469 err2 = ext4_mark_inode_dirty(handle, inode);
Theodore Ts'o622cad12014-04-11 10:35:17 -04002470 up_write(&EXT4_I(inode)->i_data_sem);
Jan Kara4e7ea812013-06-04 13:17:40 -04002471 if (err2)
2472 ext4_error(inode->i_sb,
2473 "Failed to mark inode %lu dirty",
2474 inode->i_ino);
2475 if (!err)
2476 err = err2;
2477 }
2478 return err;
2479}
2480
2481/*
Jan Karafffb2732013-06-04 13:01:11 -04002482 * Calculate the total number of credits to reserve for one writepages
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002483 * iteration. This is called from ext4_writepages(). We map an extent of
Anatol Pomozov70261f52013-08-28 14:40:12 -04002484 * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping
Jan Karafffb2732013-06-04 13:01:11 -04002485 * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN +
2486 * bpp - 1 blocks in bpp different extents.
2487 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002488static int ext4_da_writepages_trans_blocks(struct inode *inode)
2489{
Jan Karafffb2732013-06-04 13:01:11 -04002490 int bpp = ext4_journal_blocks_per_page(inode);
Mingming Cao525f4ed2008-08-19 22:15:58 -04002491
Jan Karafffb2732013-06-04 13:01:11 -04002492 return ext4_meta_trans_blocks(inode,
2493 MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp);
Mingming Cao525f4ed2008-08-19 22:15:58 -04002494}
Mingming Cao61628a32008-07-11 19:27:31 -04002495
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002496/*
Jan Kara4e7ea812013-06-04 13:17:40 -04002497 * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages
2498 * and underlying extent to map
2499 *
2500 * @mpd - where to look for pages
2501 *
2502 * Walk dirty pages in the mapping. If they are fully mapped, submit them for
2503 * IO immediately. When we find a page which isn't mapped we start accumulating
2504 * extent of buffers underlying these pages that needs mapping (formed by
2505 * either delayed or unwritten buffers). We also lock the pages containing
2506 * these buffers. The extent found is returned in @mpd structure (starting at
2507 * mpd->lblk with length mpd->len blocks).
2508 *
2509 * Note that this function can attach bios to one io_end structure which are
2510 * neither logically nor physically contiguous. Although it may seem as an
2511 * unnecessary complication, it is actually inevitable in blocksize < pagesize
2512 * 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 -04002513 */
Jan Kara4e7ea812013-06-04 13:17:40 -04002514static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd)
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002515{
Jan Kara4e7ea812013-06-04 13:17:40 -04002516 struct address_space *mapping = mpd->inode->i_mapping;
2517 struct pagevec pvec;
2518 unsigned int nr_pages;
Ming Leiaeac5892013-10-17 18:56:16 -04002519 long left = mpd->wbc->nr_to_write;
Jan Kara4e7ea812013-06-04 13:17:40 -04002520 pgoff_t index = mpd->first_page;
2521 pgoff_t end = mpd->last_page;
2522 int tag;
2523 int i, err = 0;
2524 int blkbits = mpd->inode->i_blkbits;
2525 ext4_lblk_t lblk;
2526 struct buffer_head *head;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002527
Jan Kara4e7ea812013-06-04 13:17:40 -04002528 if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages)
Eric Sandeen5b41d922010-10-27 21:30:13 -04002529 tag = PAGECACHE_TAG_TOWRITE;
2530 else
2531 tag = PAGECACHE_TAG_DIRTY;
2532
Jan Kara4e7ea812013-06-04 13:17:40 -04002533 pagevec_init(&pvec, 0);
2534 mpd->map.m_len = 0;
2535 mpd->next_page = index;
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002536 while (index <= end) {
Eric Sandeen5b41d922010-10-27 21:30:13 -04002537 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002538 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
2539 if (nr_pages == 0)
Jan Kara4e7ea812013-06-04 13:17:40 -04002540 goto out;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002541
2542 for (i = 0; i < nr_pages; i++) {
2543 struct page *page = pvec.pages[i];
2544
2545 /*
2546 * At this point, the page may be truncated or
2547 * invalidated (changing page->mapping to NULL), or
2548 * even swizzled back from swapper_space to tmpfs file
2549 * mapping. However, page->index will not change
2550 * because we have a reference on the page.
2551 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002552 if (page->index > end)
2553 goto out;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002554
Ming Leiaeac5892013-10-17 18:56:16 -04002555 /*
2556 * Accumulated enough dirty pages? This doesn't apply
2557 * to WB_SYNC_ALL mode. For integrity sync we have to
2558 * keep going because someone may be concurrently
2559 * dirtying pages, and we might have synced a lot of
2560 * newly appeared dirty pages, but have not synced all
2561 * of the old dirty pages.
2562 */
2563 if (mpd->wbc->sync_mode == WB_SYNC_NONE && left <= 0)
2564 goto out;
2565
Jan Kara4e7ea812013-06-04 13:17:40 -04002566 /* If we can't merge this page, we are done. */
2567 if (mpd->map.m_len > 0 && mpd->next_page != page->index)
2568 goto out;
Theodore Ts'o78aaced2011-02-26 14:09:14 -05002569
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002570 lock_page(page);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002571 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002572 * If the page is no longer dirty, or its mapping no
2573 * longer corresponds to inode we are writing (which
2574 * means it has been truncated or invalidated), or the
2575 * page is already under writeback and we are not doing
2576 * a data integrity writeback, skip the page
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002577 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002578 if (!PageDirty(page) ||
2579 (PageWriteback(page) &&
Jan Kara4e7ea812013-06-04 13:17:40 -04002580 (mpd->wbc->sync_mode == WB_SYNC_NONE)) ||
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002581 unlikely(page->mapping != mapping)) {
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002582 unlock_page(page);
2583 continue;
2584 }
2585
Darrick J. Wong7cb1a532011-05-18 13:53:20 -04002586 wait_on_page_writeback(page);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002587 BUG_ON(PageWriteback(page));
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002588
Jan Kara4e7ea812013-06-04 13:17:40 -04002589 if (mpd->map.m_len == 0)
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002590 mpd->first_page = page->index;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002591 mpd->next_page = page->index + 1;
Jan Karaf8bec372013-01-28 12:55:08 -05002592 /* Add all dirty buffers to mpd */
Jan Kara4e7ea812013-06-04 13:17:40 -04002593 lblk = ((ext4_lblk_t)page->index) <<
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002594 (PAGE_SHIFT - blkbits);
Jan Karaf8bec372013-01-28 12:55:08 -05002595 head = page_buffers(page);
Jan Kara5f1132b2013-08-17 10:02:33 -04002596 err = mpage_process_page_bufs(mpd, head, head, lblk);
2597 if (err <= 0)
Jan Kara4e7ea812013-06-04 13:17:40 -04002598 goto out;
Jan Kara5f1132b2013-08-17 10:02:33 -04002599 err = 0;
Ming Leiaeac5892013-10-17 18:56:16 -04002600 left--;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002601 }
2602 pagevec_release(&pvec);
2603 cond_resched();
2604 }
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002605 return 0;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002606out:
2607 pagevec_release(&pvec);
Jan Kara4e7ea812013-06-04 13:17:40 -04002608 return err;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002609}
2610
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002611static int __writepage(struct page *page, struct writeback_control *wbc,
2612 void *data)
2613{
2614 struct address_space *mapping = data;
2615 int ret = ext4_writepage(page, wbc);
2616 mapping_set_error(mapping, ret);
2617 return ret;
2618}
2619
2620static int ext4_writepages(struct address_space *mapping,
2621 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002622{
Jan Kara4e7ea812013-06-04 13:17:40 -04002623 pgoff_t writeback_index = 0;
2624 long nr_to_write = wbc->nr_to_write;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002625 int range_whole = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002626 int cycled = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002627 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002628 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002629 struct inode *inode = mapping->host;
Jan Kara6b523df2013-06-04 13:21:11 -04002630 int needed_blocks, rsv_blocks = 0, ret = 0;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002631 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Jan Kara4e7ea812013-06-04 13:17:40 -04002632 bool done;
Shaohua Li1bce63d2011-10-18 10:55:51 -04002633 struct blk_plug plug;
Theodore Ts'ocb530542013-07-01 08:12:40 -04002634 bool give_up_on_write = false;
Mingming Cao61628a32008-07-11 19:27:31 -04002635
Daeho Jeongc8585c62016-04-25 23:22:35 -04002636 percpu_down_read(&sbi->s_journal_flag_rwsem);
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002637 trace_ext4_writepages(inode, wbc);
Theodore Ts'oba80b102009-01-03 20:03:21 -05002638
Daeho Jeongc8585c62016-04-25 23:22:35 -04002639 if (dax_mapping(mapping)) {
2640 ret = dax_writeback_mapping_range(mapping, inode->i_sb->s_bdev,
2641 wbc);
2642 goto out_writepages;
2643 }
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08002644
Mingming Cao61628a32008-07-11 19:27:31 -04002645 /*
2646 * No pages to write? This is mainly a kludge to avoid starting
2647 * a transaction for special inodes like journal inode on last iput()
2648 * because that could violate lock ordering on umount
2649 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002650 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Ming Leibbf023c2013-10-30 07:27:16 -04002651 goto out_writepages;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002652
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002653 if (ext4_should_journal_data(inode)) {
2654 struct blk_plug plug;
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002655
2656 blk_start_plug(&plug);
2657 ret = write_cache_pages(mapping, wbc, __writepage, mapping);
2658 blk_finish_plug(&plug);
Ming Leibbf023c2013-10-30 07:27:16 -04002659 goto out_writepages;
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002660 }
2661
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002662 /*
2663 * If the filesystem has aborted, it is read-only, so return
2664 * right away instead of dumping stack traces later on that
2665 * will obscure the real source of the problem. We test
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002666 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002667 * the latter could be true if the filesystem is mounted
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002668 * read-only, and in that case, ext4_writepages should
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002669 * *never* be called, so if that ever happens, we would want
2670 * the stack trace.
2671 */
Ming Leibbf023c2013-10-30 07:27:16 -04002672 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) {
2673 ret = -EROFS;
2674 goto out_writepages;
2675 }
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002676
Jan Kara6b523df2013-06-04 13:21:11 -04002677 if (ext4_should_dioread_nolock(inode)) {
2678 /*
Anatol Pomozov70261f52013-08-28 14:40:12 -04002679 * We may need to convert up to one extent per block in
Jan Kara6b523df2013-06-04 13:21:11 -04002680 * the page and we may dirty the inode.
2681 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002682 rsv_blocks = 1 + (PAGE_SIZE >> inode->i_blkbits);
Jan Kara6b523df2013-06-04 13:21:11 -04002683 }
2684
Jan Kara4e7ea812013-06-04 13:17:40 -04002685 /*
2686 * If we have inline data and arrive here, it means that
2687 * we will soon create the block for the 1st page, so
2688 * we'd better clear the inline data here.
2689 */
2690 if (ext4_has_inline_data(inode)) {
2691 /* Just inode will be modified... */
2692 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
2693 if (IS_ERR(handle)) {
2694 ret = PTR_ERR(handle);
2695 goto out_writepages;
2696 }
2697 BUG_ON(ext4_test_inode_state(inode,
2698 EXT4_STATE_MAY_INLINE_DATA));
2699 ext4_destroy_inline_data(handle, inode);
2700 ext4_journal_stop(handle);
2701 }
2702
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002703 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2704 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002705
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002706 if (wbc->range_cyclic) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002707 writeback_index = mapping->writeback_index;
2708 if (writeback_index)
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002709 cycled = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002710 mpd.first_page = writeback_index;
2711 mpd.last_page = -1;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002712 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002713 mpd.first_page = wbc->range_start >> PAGE_SHIFT;
2714 mpd.last_page = wbc->range_end >> PAGE_SHIFT;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002715 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002716
Jan Kara4e7ea812013-06-04 13:17:40 -04002717 mpd.inode = inode;
2718 mpd.wbc = wbc;
2719 ext4_io_submit_init(&mpd.io_submit, wbc);
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002720retry:
Wu Fengguang6e6938b2010-06-06 10:38:15 -06002721 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Jan Kara4e7ea812013-06-04 13:17:40 -04002722 tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page);
2723 done = false;
Shaohua Li1bce63d2011-10-18 10:55:51 -04002724 blk_start_plug(&plug);
Jan Kara4e7ea812013-06-04 13:17:40 -04002725 while (!done && mpd.first_page <= mpd.last_page) {
2726 /* For each extent of pages we use new io_end */
2727 mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
2728 if (!mpd.io_submit.io_end) {
2729 ret = -ENOMEM;
2730 break;
2731 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002732
2733 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002734 * We have two constraints: We find one extent to map and we
2735 * must always write out whole page (makes a difference when
2736 * blocksize < pagesize) so that we don't block on IO when we
2737 * try to write out the rest of the page. Journalled mode is
2738 * not supported by delalloc.
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002739 */
2740 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04002741 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002742
Jan Kara4e7ea812013-06-04 13:17:40 -04002743 /* start a new transaction */
Jan Kara6b523df2013-06-04 13:21:11 -04002744 handle = ext4_journal_start_with_reserve(inode,
2745 EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks);
Mingming Cao61628a32008-07-11 19:27:31 -04002746 if (IS_ERR(handle)) {
2747 ret = PTR_ERR(handle);
Theodore Ts'o16939182009-09-26 17:43:59 -04002748 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
Curt Wohlgemuthfbe845d2010-05-16 13:00:00 -04002749 "%ld pages, ino %lu; err %d", __func__,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002750 wbc->nr_to_write, inode->i_ino, ret);
Jan Kara4e7ea812013-06-04 13:17:40 -04002751 /* Release allocated io_end */
2752 ext4_put_io_end(mpd.io_submit.io_end);
2753 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002754 }
Theodore Ts'of63e6002009-02-23 16:42:39 -05002755
Jan Kara4e7ea812013-06-04 13:17:40 -04002756 trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc);
2757 ret = mpage_prepare_extent_to_map(&mpd);
2758 if (!ret) {
2759 if (mpd.map.m_len)
Theodore Ts'ocb530542013-07-01 08:12:40 -04002760 ret = mpage_map_and_submit_extent(handle, &mpd,
2761 &give_up_on_write);
Jan Kara4e7ea812013-06-04 13:17:40 -04002762 else {
2763 /*
2764 * We scanned the whole range (or exhausted
2765 * nr_to_write), submitted what was mapped and
2766 * didn't find anything needing mapping. We are
2767 * done.
2768 */
2769 done = true;
2770 }
Theodore Ts'of63e6002009-02-23 16:42:39 -05002771 }
Jan Kara646caa92016-07-04 10:14:01 -04002772 /*
2773 * Caution: If the handle is synchronous,
2774 * ext4_journal_stop() can wait for transaction commit
2775 * to finish which may depend on writeback of pages to
2776 * complete or on page lock to be released. In that
2777 * case, we have to wait until after after we have
2778 * submitted all the IO, released page locks we hold,
2779 * and dropped io_end reference (for extent conversion
2780 * to be able to complete) before stopping the handle.
2781 */
2782 if (!ext4_handle_valid(handle) || handle->h_sync == 0) {
2783 ext4_journal_stop(handle);
2784 handle = NULL;
2785 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002786 /* Submit prepared bio */
2787 ext4_io_submit(&mpd.io_submit);
2788 /* Unlock pages we didn't use */
Theodore Ts'ocb530542013-07-01 08:12:40 -04002789 mpage_release_unused_pages(&mpd, give_up_on_write);
Jan Kara646caa92016-07-04 10:14:01 -04002790 /*
2791 * Drop our io_end reference we got from init. We have
2792 * to be careful and use deferred io_end finishing if
2793 * we are still holding the transaction as we can
2794 * release the last reference to io_end which may end
2795 * up doing unwritten extent conversion.
2796 */
2797 if (handle) {
2798 ext4_put_io_end_defer(mpd.io_submit.io_end);
2799 ext4_journal_stop(handle);
2800 } else
2801 ext4_put_io_end(mpd.io_submit.io_end);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002802
Jan Kara4e7ea812013-06-04 13:17:40 -04002803 if (ret == -ENOSPC && sbi->s_journal) {
2804 /*
2805 * Commit the transaction which would
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002806 * free blocks released in the transaction
2807 * and try again
2808 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002809 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002810 ret = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002811 continue;
2812 }
2813 /* Fatal error - ENOMEM, EIO... */
2814 if (ret)
Mingming Cao61628a32008-07-11 19:27:31 -04002815 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002816 }
Shaohua Li1bce63d2011-10-18 10:55:51 -04002817 blk_finish_plug(&plug);
Jan Kara9c12a832013-09-16 08:24:26 -04002818 if (!ret && !cycled && wbc->nr_to_write > 0) {
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002819 cycled = 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04002820 mpd.last_page = writeback_index - 1;
2821 mpd.first_page = 0;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002822 goto retry;
2823 }
Mingming Cao61628a32008-07-11 19:27:31 -04002824
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002825 /* Update index */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002826 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2827 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002828 * Set the writeback_index so that range_cyclic
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002829 * mode will write it back later
2830 */
Jan Kara4e7ea812013-06-04 13:17:40 -04002831 mapping->writeback_index = mpd.first_page;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002832
Mingming Cao61628a32008-07-11 19:27:31 -04002833out_writepages:
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002834 trace_ext4_writepages_result(inode, wbc, ret,
2835 nr_to_write - wbc->nr_to_write);
Daeho Jeongc8585c62016-04-25 23:22:35 -04002836 percpu_up_read(&sbi->s_journal_flag_rwsem);
Mingming Cao61628a32008-07-11 19:27:31 -04002837 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002838}
2839
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002840static int ext4_nonda_switch(struct super_block *sb)
2841{
Eric Whitney5c1ff332013-04-09 09:27:31 -04002842 s64 free_clusters, dirty_clusters;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002843 struct ext4_sb_info *sbi = EXT4_SB(sb);
2844
2845 /*
2846 * switch to non delalloc mode if we are running low
2847 * on free block. The free block accounting via percpu
Eric Dumazet179f7eb2009-01-06 14:41:04 -08002848 * counters can get slightly wrong with percpu_counter_batch getting
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002849 * accumulated on each CPU without updating global counters
2850 * Delalloc need an accurate free block accounting. So switch
2851 * to non delalloc when we are near to error range.
2852 */
Eric Whitney5c1ff332013-04-09 09:27:31 -04002853 free_clusters =
2854 percpu_counter_read_positive(&sbi->s_freeclusters_counter);
2855 dirty_clusters =
2856 percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
Theodore Ts'o00d4e732012-09-19 22:42:36 -04002857 /*
2858 * Start pushing delalloc when 1/2 of free blocks are dirty.
2859 */
Eric Whitney5c1ff332013-04-09 09:27:31 -04002860 if (dirty_clusters && (free_clusters < 2 * dirty_clusters))
Miao Xie10ee27a2013-01-10 13:47:57 +08002861 try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE);
Theodore Ts'o00d4e732012-09-19 22:42:36 -04002862
Eric Whitney5c1ff332013-04-09 09:27:31 -04002863 if (2 * free_clusters < 3 * dirty_clusters ||
2864 free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) {
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002865 /*
Eric Sandeenc8afb442009-12-23 07:58:12 -05002866 * free block count is less than 150% of dirty blocks
2867 * or free blocks is less than watermark
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002868 */
2869 return 1;
2870 }
2871 return 0;
2872}
2873
Eric Sandeen0ff89472014-10-11 19:51:17 -04002874/* We always reserve for an inode update; the superblock could be there too */
2875static int ext4_da_write_credits(struct inode *inode, loff_t pos, unsigned len)
2876{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002877 if (likely(ext4_has_feature_large_file(inode->i_sb)))
Eric Sandeen0ff89472014-10-11 19:51:17 -04002878 return 1;
2879
2880 if (pos + len <= 0x7fffffffULL)
2881 return 1;
2882
2883 /* We might need to update the superblock to set LARGE_FILE */
2884 return 2;
2885}
2886
Alex Tomas64769242008-07-11 19:27:31 -04002887static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002888 loff_t pos, unsigned len, unsigned flags,
2889 struct page **pagep, void **fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04002890{
Eric Sandeen72b8ab92010-05-16 11:00:00 -04002891 int ret, retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002892 struct page *page;
2893 pgoff_t index;
Alex Tomas64769242008-07-11 19:27:31 -04002894 struct inode *inode = mapping->host;
2895 handle_t *handle;
2896
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002897 index = pos >> PAGE_SHIFT;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002898
2899 if (ext4_nonda_switch(inode->i_sb)) {
2900 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
2901 return ext4_write_begin(file, mapping, pos,
2902 len, flags, pagep, fsdata);
2903 }
2904 *fsdata = (void *)0;
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -08002905 trace_android_fs_datawrite_start(inode, pos, len,
2906 current->pid, current->comm);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002907 trace_ext4_da_write_begin(inode, pos, len, flags);
Tao Ma9c3569b2012-12-10 14:05:57 -05002908
2909 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2910 ret = ext4_da_write_inline_data_begin(mapping, inode,
2911 pos, len, flags,
2912 pagep, fsdata);
2913 if (ret < 0)
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002914 return ret;
2915 if (ret == 1)
2916 return 0;
Tao Ma9c3569b2012-12-10 14:05:57 -05002917 }
2918
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002919 /*
2920 * grab_cache_page_write_begin() can take a long time if the
2921 * system is thrashing due to memory pressure, or if the page
2922 * is being written back. So grab it first before we start
2923 * the transaction handle. This also allows us to allocate
2924 * the page (if needed) without using GFP_NOFS.
2925 */
2926retry_grab:
2927 page = grab_cache_page_write_begin(mapping, index, flags);
2928 if (!page)
2929 return -ENOMEM;
2930 unlock_page(page);
2931
Alex Tomas64769242008-07-11 19:27:31 -04002932 /*
2933 * With delayed allocation, we don't log the i_disksize update
2934 * if there is delayed block allocation. But we still need
2935 * to journalling the i_disksize update if writes to the end
2936 * of file which has an already mapped buffer.
2937 */
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002938retry_journal:
Eric Sandeen0ff89472014-10-11 19:51:17 -04002939 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
2940 ext4_da_write_credits(inode, pos, len));
Alex Tomas64769242008-07-11 19:27:31 -04002941 if (IS_ERR(handle)) {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002942 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002943 return PTR_ERR(handle);
Alex Tomas64769242008-07-11 19:27:31 -04002944 }
2945
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002946 lock_page(page);
2947 if (page->mapping != mapping) {
2948 /* The page got truncated from under us */
2949 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002950 put_page(page);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002951 ext4_journal_stop(handle);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002952 goto retry_grab;
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002953 }
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002954 /* In case writeback began while the page was unlocked */
Dmitry Monakhov7afe5aa2013-08-28 14:30:47 -04002955 wait_for_stable_page(page);
Alex Tomas64769242008-07-11 19:27:31 -04002956
Michael Halcrow2058f832015-04-12 00:55:10 -04002957#ifdef CONFIG_EXT4_FS_ENCRYPTION
2958 ret = ext4_block_write_begin(page, pos, len,
2959 ext4_da_get_block_prep);
2960#else
Christoph Hellwig6e1db882010-06-04 11:29:57 +02002961 ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
Michael Halcrow2058f832015-04-12 00:55:10 -04002962#endif
Alex Tomas64769242008-07-11 19:27:31 -04002963 if (ret < 0) {
2964 unlock_page(page);
2965 ext4_journal_stop(handle);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04002966 /*
2967 * block_write_begin may have instantiated a few blocks
2968 * outside i_size. Trim these off again. Don't need
2969 * i_size_read because we hold i_mutex.
2970 */
2971 if (pos + len > inode->i_size)
Jan Karab9a42072009-12-08 21:24:33 -05002972 ext4_truncate_failed_write(inode);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002973
2974 if (ret == -ENOSPC &&
2975 ext4_should_retry_alloc(inode->i_sb, &retries))
2976 goto retry_journal;
2977
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002978 put_page(page);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002979 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002980 }
2981
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002982 *pagep = page;
Alex Tomas64769242008-07-11 19:27:31 -04002983 return ret;
2984}
2985
Mingming Cao632eaea2008-07-11 19:27:31 -04002986/*
2987 * Check if we should update i_disksize
2988 * when write to the end of file but not require block allocation
2989 */
2990static int ext4_da_should_update_i_disksize(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002991 unsigned long offset)
Mingming Cao632eaea2008-07-11 19:27:31 -04002992{
2993 struct buffer_head *bh;
2994 struct inode *inode = page->mapping->host;
2995 unsigned int idx;
2996 int i;
2997
2998 bh = page_buffers(page);
2999 idx = offset >> inode->i_blkbits;
3000
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003001 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04003002 bh = bh->b_this_page;
3003
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04003004 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
Mingming Cao632eaea2008-07-11 19:27:31 -04003005 return 0;
3006 return 1;
3007}
3008
Alex Tomas64769242008-07-11 19:27:31 -04003009static int ext4_da_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003010 struct address_space *mapping,
3011 loff_t pos, unsigned len, unsigned copied,
3012 struct page *page, void *fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04003013{
3014 struct inode *inode = mapping->host;
3015 int ret = 0, ret2;
3016 handle_t *handle = ext4_journal_current_handle();
3017 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04003018 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04003019 int write_mode = (int)(unsigned long)fsdata;
3020
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003021 if (write_mode == FALL_BACK_TO_NONDELALLOC)
3022 return ext4_write_end(file, mapping, pos,
3023 len, copied, page, fsdata);
Mingming Cao632eaea2008-07-11 19:27:31 -04003024
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -08003025 trace_android_fs_datawrite_end(inode, pos, len);
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003026 trace_ext4_da_write_end(inode, pos, len, copied);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003027 start = pos & (PAGE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003028 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04003029
3030 /*
3031 * generic_write_end() will run mark_inode_dirty() if i_size
3032 * changes. So let's piggyback the i_disksize mark_inode_dirty
3033 * into that.
3034 */
Alex Tomas64769242008-07-11 19:27:31 -04003035 new_i_size = pos + copied;
Andrea Arcangeliea51d132011-12-13 21:41:15 -05003036 if (copied && new_i_size > EXT4_I(inode)->i_disksize) {
Tao Ma9c3569b2012-12-10 14:05:57 -05003037 if (ext4_has_inline_data(inode) ||
3038 ext4_da_should_update_i_disksize(page, end)) {
Dmitry Monakhovee124d22014-08-30 23:34:06 -04003039 ext4_update_i_disksize(inode, new_i_size);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04003040 /* We need to mark inode dirty even if
3041 * new_i_size is less that inode->i_size
3042 * bu greater than i_disksize.(hint delalloc)
3043 */
3044 ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04003045 }
Mingming Cao632eaea2008-07-11 19:27:31 -04003046 }
Tao Ma9c3569b2012-12-10 14:05:57 -05003047
3048 if (write_mode != CONVERT_INLINE_DATA &&
3049 ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
3050 ext4_has_inline_data(inode))
3051 ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied,
3052 page);
3053 else
3054 ret2 = generic_write_end(file, mapping, pos, len, copied,
Alex Tomas64769242008-07-11 19:27:31 -04003055 page, fsdata);
Tao Ma9c3569b2012-12-10 14:05:57 -05003056
Alex Tomas64769242008-07-11 19:27:31 -04003057 copied = ret2;
3058 if (ret2 < 0)
3059 ret = ret2;
3060 ret2 = ext4_journal_stop(handle);
3061 if (!ret)
3062 ret = ret2;
3063
3064 return ret ? ret : copied;
3065}
3066
Lukas Czernerd47992f2013-05-21 23:17:23 -04003067static void ext4_da_invalidatepage(struct page *page, unsigned int offset,
3068 unsigned int length)
Alex Tomas64769242008-07-11 19:27:31 -04003069{
Alex Tomas64769242008-07-11 19:27:31 -04003070 /*
3071 * Drop reserved blocks
3072 */
3073 BUG_ON(!PageLocked(page));
3074 if (!page_has_buffers(page))
3075 goto out;
3076
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003077 ext4_da_page_release_reservation(page, offset, length);
Alex Tomas64769242008-07-11 19:27:31 -04003078
3079out:
Lukas Czernerd47992f2013-05-21 23:17:23 -04003080 ext4_invalidatepage(page, offset, length);
Alex Tomas64769242008-07-11 19:27:31 -04003081
3082 return;
3083}
3084
Theodore Ts'occd25062009-02-26 01:04:07 -05003085/*
3086 * Force all delayed allocation blocks to be allocated for a given inode.
3087 */
3088int ext4_alloc_da_blocks(struct inode *inode)
3089{
Theodore Ts'ofb40ba02009-09-16 19:30:40 -04003090 trace_ext4_alloc_da_blocks(inode);
3091
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04003092 if (!EXT4_I(inode)->i_reserved_data_blocks)
Theodore Ts'occd25062009-02-26 01:04:07 -05003093 return 0;
3094
3095 /*
3096 * We do something simple for now. The filemap_flush() will
3097 * also start triggering a write of the data blocks, which is
3098 * not strictly speaking necessary (and for users of
3099 * laptop_mode, not even desirable). However, to do otherwise
3100 * would require replicating code paths in:
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003101 *
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003102 * ext4_writepages() ->
Theodore Ts'occd25062009-02-26 01:04:07 -05003103 * write_cache_pages() ---> (via passed in callback function)
3104 * __mpage_da_writepage() -->
3105 * mpage_add_bh_to_extent()
3106 * mpage_da_map_blocks()
3107 *
3108 * The problem is that write_cache_pages(), located in
3109 * mm/page-writeback.c, marks pages clean in preparation for
3110 * doing I/O, which is not desirable if we're not planning on
3111 * doing I/O at all.
3112 *
3113 * We could call write_cache_pages(), and then redirty all of
Wu Fengguang380cf092010-11-11 19:23:29 +08003114 * the pages by calling redirty_page_for_writepage() but that
Theodore Ts'occd25062009-02-26 01:04:07 -05003115 * would be ugly in the extreme. So instead we would need to
3116 * replicate parts of the code in the above functions,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003117 * simplifying them because we wouldn't actually intend to
Theodore Ts'occd25062009-02-26 01:04:07 -05003118 * write out the pages, but rather only collect contiguous
3119 * logical block extents, call the multi-block allocator, and
3120 * then update the buffer heads with the block allocations.
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003121 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003122 * For now, though, we'll cheat by calling filemap_flush(),
3123 * which will map the blocks, and start the I/O, but not
3124 * actually wait for the I/O to complete.
3125 */
3126 return filemap_flush(inode->i_mapping);
3127}
Alex Tomas64769242008-07-11 19:27:31 -04003128
3129/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003130 * bmap() is special. It gets used by applications such as lilo and by
3131 * the swapper to find the on-disk block of a specific piece of data.
3132 *
3133 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07003134 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003135 * filesystem and enables swap, then they may get a nasty shock when the
3136 * data getting swapped to that swapfile suddenly gets overwritten by
3137 * the original zero's written out previously to the journal and
3138 * awaiting writeback in the kernel's buffer cache.
3139 *
3140 * So, if we see any bmap calls here on a modified, data-journaled file,
3141 * take extra steps to flush any blocks which might be in the cache.
3142 */
Mingming Cao617ba132006-10-11 01:20:53 -07003143static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003144{
3145 struct inode *inode = mapping->host;
3146 journal_t *journal;
3147 int err;
3148
Tao Ma46c7f252012-12-10 14:04:52 -05003149 /*
3150 * We can get here for an inline file via the FIBMAP ioctl
3151 */
3152 if (ext4_has_inline_data(inode))
3153 return 0;
3154
Alex Tomas64769242008-07-11 19:27:31 -04003155 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
3156 test_opt(inode->i_sb, DELALLOC)) {
3157 /*
3158 * With delalloc we want to sync the file
3159 * so that we can make sure we allocate
3160 * blocks for file
3161 */
3162 filemap_write_and_wait(mapping);
3163 }
3164
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003165 if (EXT4_JOURNAL(inode) &&
3166 ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003167 /*
3168 * This is a REALLY heavyweight approach, but the use of
3169 * bmap on dirty files is expected to be extremely rare:
3170 * only if we run lilo or swapon on a freshly made file
3171 * do we expect this to happen.
3172 *
3173 * (bmap requires CAP_SYS_RAWIO so this does not
3174 * represent an unprivileged user DOS attack --- we'd be
3175 * in trouble if mortal users could trigger this path at
3176 * will.)
3177 *
Mingming Cao617ba132006-10-11 01:20:53 -07003178 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003179 * regular files. If somebody wants to bmap a directory
3180 * or symlink and gets confused because the buffer
3181 * hasn't yet been flushed to disk, they deserve
3182 * everything they get.
3183 */
3184
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003185 ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
Mingming Cao617ba132006-10-11 01:20:53 -07003186 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07003187 jbd2_journal_lock_updates(journal);
3188 err = jbd2_journal_flush(journal);
3189 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003190
3191 if (err)
3192 return 0;
3193 }
3194
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003195 return generic_block_bmap(mapping, block, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003196}
3197
Mingming Cao617ba132006-10-11 01:20:53 -07003198static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003199{
Tao Ma46c7f252012-12-10 14:04:52 -05003200 int ret = -EAGAIN;
3201 struct inode *inode = page->mapping->host;
3202
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003203 trace_ext4_readpage(page);
Tao Ma46c7f252012-12-10 14:04:52 -05003204
3205 if (ext4_has_inline_data(inode))
3206 ret = ext4_readpage_inline(inode, page);
3207
3208 if (ret == -EAGAIN)
Theodore Ts'of64e02f2015-04-08 00:00:32 -04003209 return ext4_mpage_readpages(page->mapping, NULL, page, 1);
Tao Ma46c7f252012-12-10 14:04:52 -05003210
3211 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003212}
3213
3214static int
Mingming Cao617ba132006-10-11 01:20:53 -07003215ext4_readpages(struct file *file, struct address_space *mapping,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003216 struct list_head *pages, unsigned nr_pages)
3217{
Tao Ma46c7f252012-12-10 14:04:52 -05003218 struct inode *inode = mapping->host;
3219
3220 /* If the file has inline data, no need to do readpages. */
3221 if (ext4_has_inline_data(inode))
3222 return 0;
3223
Theodore Ts'of64e02f2015-04-08 00:00:32 -04003224 return ext4_mpage_readpages(mapping, pages, NULL, nr_pages);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003225}
3226
Lukas Czernerd47992f2013-05-21 23:17:23 -04003227static void ext4_invalidatepage(struct page *page, unsigned int offset,
3228 unsigned int length)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003229{
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003230 trace_ext4_invalidatepage(page, offset, length);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003231
Jan Kara4520fb32012-12-25 13:28:54 -05003232 /* No journalling happens on data buffers when this function is used */
3233 WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page)));
3234
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003235 block_invalidatepage(page, offset, length);
Jan Kara4520fb32012-12-25 13:28:54 -05003236}
3237
Jan Kara53e87262012-12-25 13:29:52 -05003238static int __ext4_journalled_invalidatepage(struct page *page,
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003239 unsigned int offset,
3240 unsigned int length)
Jan Kara4520fb32012-12-25 13:28:54 -05003241{
3242 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
3243
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003244 trace_ext4_journalled_invalidatepage(page, offset, length);
Jan Kara4520fb32012-12-25 13:28:54 -05003245
Jiaying Zhang744692d2010-03-04 16:14:02 -05003246 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003247 * If it's a full truncate we just forget about the pending dirtying
3248 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003249 if (offset == 0 && length == PAGE_SIZE)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003250 ClearPageChecked(page);
3251
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003252 return jbd2_journal_invalidatepage(journal, page, offset, length);
Jan Kara53e87262012-12-25 13:29:52 -05003253}
3254
3255/* Wrapper for aops... */
3256static void ext4_journalled_invalidatepage(struct page *page,
Lukas Czernerd47992f2013-05-21 23:17:23 -04003257 unsigned int offset,
3258 unsigned int length)
Jan Kara53e87262012-12-25 13:29:52 -05003259{
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003260 WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003261}
3262
Mingming Cao617ba132006-10-11 01:20:53 -07003263static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003264{
Mingming Cao617ba132006-10-11 01:20:53 -07003265 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003266
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003267 trace_ext4_releasepage(page);
3268
Jan Karae1c36592013-03-10 22:19:00 -04003269 /* Page has dirty journalled data -> cannot release */
3270 if (PageChecked(page))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003271 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05003272 if (journal)
3273 return jbd2_journal_try_to_free_buffers(journal, page, wait);
3274 else
3275 return try_to_free_buffers(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003276}
3277
Jan Karaba5843f2015-12-07 15:10:44 -05003278#ifdef CONFIG_FS_DAX
Jan Kara12735f82016-05-13 00:51:15 -04003279/*
3280 * Get block function for DAX IO and mmap faults. It takes care of converting
3281 * unwritten extents to written ones and initializes new / converted blocks
3282 * to zeros.
3283 */
3284int ext4_dax_get_block(struct inode *inode, sector_t iblock,
3285 struct buffer_head *bh_result, int create)
Matthew Wilcoxed923b52015-09-08 14:59:20 -07003286{
Jan Kara7cb476f2016-05-13 00:38:16 -04003287 int ret;
Jan Karac86d8db2015-12-07 15:10:26 -05003288
Jan Kara12735f82016-05-13 00:51:15 -04003289 ext4_debug("inode %lu, create flag %d\n", inode->i_ino, create);
Jan Kara7cb476f2016-05-13 00:38:16 -04003290 if (!create)
3291 return _ext4_get_block(inode, iblock, bh_result, 0);
Jan Karaba5843f2015-12-07 15:10:44 -05003292
Jan Kara7cb476f2016-05-13 00:38:16 -04003293 ret = ext4_get_block_trans(inode, iblock, bh_result,
3294 EXT4_GET_BLOCKS_PRE_IO |
3295 EXT4_GET_BLOCKS_CREATE_ZERO);
3296 if (ret < 0)
3297 return ret;
Jan Karaba5843f2015-12-07 15:10:44 -05003298
Jan Kara7cb476f2016-05-13 00:38:16 -04003299 if (buffer_unwritten(bh_result)) {
Jan Karaba5843f2015-12-07 15:10:44 -05003300 /*
Jan Kara12735f82016-05-13 00:51:15 -04003301 * We are protected by i_mmap_sem or i_mutex so we know block
3302 * cannot go away from under us even though we dropped
3303 * i_data_sem. Convert extent to written and write zeros there.
Jan Karaba5843f2015-12-07 15:10:44 -05003304 */
Jan Kara7cb476f2016-05-13 00:38:16 -04003305 ret = ext4_get_block_trans(inode, iblock, bh_result,
3306 EXT4_GET_BLOCKS_CONVERT |
3307 EXT4_GET_BLOCKS_CREATE_ZERO);
3308 if (ret < 0)
Jan Karaba5843f2015-12-07 15:10:44 -05003309 return ret;
Jan Karaba5843f2015-12-07 15:10:44 -05003310 }
Jan Kara7cb476f2016-05-13 00:38:16 -04003311 /*
3312 * At least for now we have to clear BH_New so that DAX code
3313 * doesn't attempt to zero blocks again in a racy way.
3314 */
3315 clear_buffer_new(bh_result);
3316 return 0;
Matthew Wilcoxed923b52015-09-08 14:59:20 -07003317}
Jan Kara12735f82016-05-13 00:51:15 -04003318#else
3319/* Just define empty function, it will never get called. */
3320int ext4_dax_get_block(struct inode *inode, sector_t iblock,
3321 struct buffer_head *bh_result, int create)
3322{
3323 BUG();
3324 return 0;
Matthew Wilcoxed923b52015-09-08 14:59:20 -07003325}
Jan Karaba5843f2015-12-07 15:10:44 -05003326#endif
Matthew Wilcoxed923b52015-09-08 14:59:20 -07003327
Christoph Hellwig187372a2016-02-08 14:40:51 +11003328static int ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
Christoph Hellwig7b7a8662013-09-04 15:04:39 +02003329 ssize_t size, void *private)
Mingming Cao4c0425f2009-09-28 15:48:41 -04003330{
Jan Kara109811c2016-03-08 23:36:46 -05003331 ext4_io_end_t *io_end = private;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003332
Jan Kara97a851e2013-06-04 11:58:58 -04003333 /* if not async direct IO just return */
Christoph Hellwig7b7a8662013-09-04 15:04:39 +02003334 if (!io_end)
Christoph Hellwig187372a2016-02-08 14:40:51 +11003335 return 0;
Mingming4b70df12009-11-03 14:44:54 -05003336
Zheng Liu88635ca2011-12-28 19:00:25 -05003337 ext_debug("ext4_end_io_dio(): io_end 0x%p "
Joe Perchesace36ad2012-03-19 23:11:43 -04003338 "for inode %lu, iocb 0x%p, offset %llu, size %zd\n",
Jan Kara109811c2016-03-08 23:36:46 -05003339 io_end, io_end->inode->i_ino, iocb, offset, size);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003340
Jan Kara74c66bc2016-02-29 08:36:38 +11003341 /*
3342 * Error during AIO DIO. We cannot convert unwritten extents as the
3343 * data was not written. Just clear the unwritten flag and drop io_end.
3344 */
3345 if (size <= 0) {
3346 ext4_clear_io_unwritten_flag(io_end);
3347 size = 0;
3348 }
Mingming Cao4c0425f2009-09-28 15:48:41 -04003349 io_end->offset = offset;
3350 io_end->size = size;
Christoph Hellwig7b7a8662013-09-04 15:04:39 +02003351 ext4_put_io_end(io_end);
Christoph Hellwig187372a2016-02-08 14:40:51 +11003352
3353 return 0;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003354}
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003355
Mingming Cao4c0425f2009-09-28 15:48:41 -04003356/*
Jan Kara914f82a2016-05-13 00:44:16 -04003357 * Handling of direct IO writes.
3358 *
3359 * For ext4 extent files, ext4 will do direct-io write even to holes,
Mingming Cao4c0425f2009-09-28 15:48:41 -04003360 * preallocated extents, and those write extend the file, no need to
3361 * fall back to buffered IO.
3362 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003363 * For holes, we fallocate those blocks, mark them as unwritten
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003364 * If those blocks were preallocated, we mark sure they are split, but
Lukas Czerner556615d2014-04-20 23:45:47 -04003365 * still keep the range to write as unwritten.
Mingming Cao4c0425f2009-09-28 15:48:41 -04003366 *
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003367 * The unwritten extents will be converted to written when DIO is completed.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003368 * For async direct IO, since the IO may still pending when return, we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003369 * set up an end_io call back function, which will do the conversion
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003370 * when async direct IO completed.
Mingming Cao4c0425f2009-09-28 15:48:41 -04003371 *
3372 * If the O_DIRECT write will extend the file then add this inode to the
3373 * orphan list. So recovery will truncate it back to the original size
3374 * if the machine crashes during the write.
3375 *
3376 */
Linus Torvalds0e01df12016-05-24 12:55:26 -07003377static ssize_t ext4_direct_IO_write(struct kiocb *iocb, struct iov_iter *iter)
Mingming Cao4c0425f2009-09-28 15:48:41 -04003378{
3379 struct file *file = iocb->ki_filp;
3380 struct inode *inode = file->f_mapping->host;
Jan Kara914f82a2016-05-13 00:44:16 -04003381 struct ext4_inode_info *ei = EXT4_I(inode);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003382 ssize_t ret;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07003383 loff_t offset = iocb->ki_pos;
Al Viroa6cbcd42014-03-04 22:38:00 -05003384 size_t count = iov_iter_count(iter);
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003385 int overwrite = 0;
3386 get_block_t *get_block_func = NULL;
3387 int dio_flags = 0;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003388 loff_t final_size = offset + count;
Jan Kara914f82a2016-05-13 00:44:16 -04003389 int orphan = 0;
3390 handle_t *handle;
Zheng Liu729f52c2012-07-09 16:29:29 -04003391
Jan Kara914f82a2016-05-13 00:44:16 -04003392 if (final_size > inode->i_size) {
3393 /* Credits for sb + inode write */
3394 handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
3395 if (IS_ERR(handle)) {
3396 ret = PTR_ERR(handle);
3397 goto out;
3398 }
3399 ret = ext4_orphan_add(handle, inode);
3400 if (ret) {
3401 ext4_journal_stop(handle);
3402 goto out;
3403 }
3404 orphan = 1;
3405 ei->i_disksize = inode->i_size;
3406 ext4_journal_stop(handle);
3407 }
Zheng Liu4bd809d2012-07-22 20:19:31 -04003408
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003409 BUG_ON(iocb->private == NULL);
Zheng Liu4bd809d2012-07-22 20:19:31 -04003410
Jan Karae8340392013-06-04 14:27:38 -04003411 /*
3412 * Make all waiters for direct IO properly wait also for extent
3413 * conversion. This also disallows race between truncate() and
3414 * overwrite DIO as i_dio_count needs to be incremented under i_mutex.
3415 */
Jan Kara914f82a2016-05-13 00:44:16 -04003416 inode_dio_begin(inode);
Jan Karae8340392013-06-04 14:27:38 -04003417
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003418 /* If we do a overwrite dio, i_mutex locking can be released */
3419 overwrite = *((int *)iocb->private);
Zheng Liu4bd809d2012-07-22 20:19:31 -04003420
Jan Kara2dcba472015-12-07 15:04:57 -05003421 if (overwrite)
Al Viro59551022016-01-22 15:40:57 -05003422 inode_unlock(inode);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003423
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003424 /*
Jan Kara914f82a2016-05-13 00:44:16 -04003425 * For extent mapped files we could direct write to holes and fallocate.
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003426 *
Jan Kara109811c2016-03-08 23:36:46 -05003427 * Allocated blocks to fill the hole are marked as unwritten to prevent
3428 * parallel buffered read to expose the stale data before DIO complete
3429 * the data IO.
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003430 *
Jan Kara109811c2016-03-08 23:36:46 -05003431 * As to previously fallocated extents, ext4 get_block will just simply
3432 * mark the buffer mapped but still keep the extents unwritten.
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003433 *
Jan Kara109811c2016-03-08 23:36:46 -05003434 * For non AIO case, we will convert those unwritten extents to written
3435 * after return back from blockdev_direct_IO. That way we save us from
3436 * allocating io_end structure and also the overhead of offloading
3437 * the extent convertion to a workqueue.
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003438 *
3439 * For async DIO, the conversion needs to be deferred when the
3440 * IO is completed. The ext4 end_io callback function will be
3441 * called to take care of the conversion work. Here for async
3442 * case, we allocate an io_end structure to hook to the iocb.
3443 */
3444 iocb->private = NULL;
Jan Kara109811c2016-03-08 23:36:46 -05003445 if (overwrite)
Jan Kara705965b2016-03-08 23:08:10 -05003446 get_block_func = ext4_dio_get_block_overwrite;
Jan Kara12735f82016-05-13 00:51:15 -04003447 else if (IS_DAX(inode)) {
3448 /*
3449 * We can avoid zeroing for aligned DAX writes beyond EOF. Other
3450 * writes need zeroing either because they can race with page
3451 * faults or because they use partial blocks.
3452 */
3453 if (round_down(offset, 1<<inode->i_blkbits) >= inode->i_size &&
3454 ext4_aligned_io(inode, offset, count))
3455 get_block_func = ext4_dio_get_block;
3456 else
3457 get_block_func = ext4_dax_get_block;
3458 dio_flags = DIO_LOCKING;
3459 } else if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) ||
3460 round_down(offset, 1 << inode->i_blkbits) >= inode->i_size) {
Jan Kara914f82a2016-05-13 00:44:16 -04003461 get_block_func = ext4_dio_get_block;
3462 dio_flags = DIO_LOCKING | DIO_SKIP_HOLES;
3463 } else if (is_sync_kiocb(iocb)) {
Jan Kara109811c2016-03-08 23:36:46 -05003464 get_block_func = ext4_dio_get_block_unwritten_sync;
3465 dio_flags = DIO_LOCKING;
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003466 } else {
Jan Kara109811c2016-03-08 23:36:46 -05003467 get_block_func = ext4_dio_get_block_unwritten_async;
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003468 dio_flags = DIO_LOCKING;
3469 }
Michael Halcrow2058f832015-04-12 00:55:10 -04003470#ifdef CONFIG_EXT4_FS_ENCRYPTION
3471 BUG_ON(ext4_encrypted_inode(inode) && S_ISREG(inode->i_mode));
3472#endif
Jan Kara914f82a2016-05-13 00:44:16 -04003473 if (IS_DAX(inode)) {
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07003474 ret = dax_do_io(iocb, inode, iter, get_block_func,
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003475 ext4_end_io_dio, dio_flags);
Jan Kara914f82a2016-05-13 00:44:16 -04003476 } else
Omar Sandoval17f8c842015-03-16 04:33:50 -07003477 ret = __blockdev_direct_IO(iocb, inode,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07003478 inode->i_sb->s_bdev, iter,
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003479 get_block_func,
3480 ext4_end_io_dio, NULL, dio_flags);
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003481
Jan Kara97a851e2013-06-04 11:58:58 -04003482 if (ret > 0 && !overwrite && ext4_test_inode_state(inode,
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003483 EXT4_STATE_DIO_UNWRITTEN)) {
3484 int err;
3485 /*
3486 * for non AIO case, since the IO is already
3487 * completed, we could do the conversion right here
3488 */
Jan Kara6b523df2013-06-04 13:21:11 -04003489 err = ext4_convert_unwritten_extents(NULL, inode,
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003490 offset, ret);
3491 if (err < 0)
3492 ret = err;
3493 ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
3494 }
3495
Jan Kara914f82a2016-05-13 00:44:16 -04003496 inode_dio_end(inode);
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003497 /* take i_mutex locking again if we do a ovewrite dio */
Jan Kara2dcba472015-12-07 15:04:57 -05003498 if (overwrite)
Al Viro59551022016-01-22 15:40:57 -05003499 inode_lock(inode);
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003500
Jan Kara914f82a2016-05-13 00:44:16 -04003501 if (ret < 0 && final_size > inode->i_size)
3502 ext4_truncate_failed_write(inode);
3503
3504 /* Handle extending of i_size after direct IO write */
3505 if (orphan) {
3506 int err;
3507
3508 /* Credits for sb + inode write */
3509 handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
3510 if (IS_ERR(handle)) {
3511 /* This is really bad luck. We've written the data
3512 * but cannot extend i_size. Bail out and pretend
3513 * the write failed... */
3514 ret = PTR_ERR(handle);
3515 if (inode->i_nlink)
3516 ext4_orphan_del(NULL, inode);
3517
3518 goto out;
3519 }
3520 if (inode->i_nlink)
3521 ext4_orphan_del(handle, inode);
3522 if (ret > 0) {
3523 loff_t end = offset + ret;
3524 if (end > inode->i_size) {
3525 ei->i_disksize = end;
3526 i_size_write(inode, end);
3527 /*
3528 * We're going to return a positive `ret'
3529 * here due to non-zero-length I/O, so there's
3530 * no way of reporting error returns from
3531 * ext4_mark_inode_dirty() to userspace. So
3532 * ignore it.
3533 */
3534 ext4_mark_inode_dirty(handle, inode);
3535 }
3536 }
3537 err = ext4_journal_stop(handle);
3538 if (ret == 0)
3539 ret = err;
3540 }
3541out:
3542 return ret;
3543}
3544
Linus Torvalds0e01df12016-05-24 12:55:26 -07003545static ssize_t ext4_direct_IO_read(struct kiocb *iocb, struct iov_iter *iter)
Jan Kara914f82a2016-05-13 00:44:16 -04003546{
Jan Kara16c54682016-09-30 01:03:17 -04003547 struct address_space *mapping = iocb->ki_filp->f_mapping;
3548 struct inode *inode = mapping->host;
Jan Kara914f82a2016-05-13 00:44:16 -04003549 ssize_t ret;
3550
Jan Kara16c54682016-09-30 01:03:17 -04003551 /*
3552 * Shared inode_lock is enough for us - it protects against concurrent
3553 * writes & truncates and since we take care of writing back page cache,
3554 * we are protected against page writeback as well.
3555 */
3556 inode_lock_shared(inode);
Jan Kara914f82a2016-05-13 00:44:16 -04003557 if (IS_DAX(inode)) {
Jan Kara16c54682016-09-30 01:03:17 -04003558 ret = dax_do_io(iocb, inode, iter, ext4_dio_get_block, NULL, 0);
Jan Kara914f82a2016-05-13 00:44:16 -04003559 } else {
Jan Kara16c54682016-09-30 01:03:17 -04003560 size_t count = iov_iter_count(iter);
3561
3562 ret = filemap_write_and_wait_range(mapping, iocb->ki_pos,
3563 iocb->ki_pos + count);
3564 if (ret)
3565 goto out_unlock;
Jan Kara914f82a2016-05-13 00:44:16 -04003566 ret = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
Linus Torvalds0e01df12016-05-24 12:55:26 -07003567 iter, ext4_dio_get_block,
Jan Kara16c54682016-09-30 01:03:17 -04003568 NULL, NULL, 0);
Jan Kara914f82a2016-05-13 00:44:16 -04003569 }
Jan Kara16c54682016-09-30 01:03:17 -04003570out_unlock:
3571 inode_unlock_shared(inode);
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003572 return ret;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003573}
3574
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07003575static ssize_t ext4_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Mingming Cao4c0425f2009-09-28 15:48:41 -04003576{
3577 struct file *file = iocb->ki_filp;
3578 struct inode *inode = file->f_mapping->host;
Al Viroa6cbcd42014-03-04 22:38:00 -05003579 size_t count = iov_iter_count(iter);
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07003580 loff_t offset = iocb->ki_pos;
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003581 ssize_t ret;
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -08003582 int rw = iov_iter_rw(iter);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003583
Michael Halcrow2058f832015-04-12 00:55:10 -04003584#ifdef CONFIG_EXT4_FS_ENCRYPTION
3585 if (ext4_encrypted_inode(inode) && S_ISREG(inode->i_mode))
3586 return 0;
3587#endif
3588
Theodore Ts'o84ebd792011-08-31 11:56:51 -04003589 /*
3590 * If we are doing data journalling we don't support O_DIRECT
3591 */
3592 if (ext4_should_journal_data(inode))
3593 return 0;
3594
Tao Ma46c7f252012-12-10 14:04:52 -05003595 /* Let buffer I/O handle the inline data case. */
3596 if (ext4_has_inline_data(inode))
3597 return 0;
3598
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -08003599 if (trace_android_fs_dataread_start_enabled() &&
3600 (rw == READ))
3601 trace_android_fs_dataread_start(inode, offset, count,
3602 current->pid,
3603 current->comm);
3604 if (trace_android_fs_datawrite_start_enabled() &&
3605 (rw == WRITE))
3606 trace_android_fs_datawrite_start(inode, offset, count,
3607 current->pid,
3608 current->comm);
3609
Omar Sandoval6f673762015-03-16 04:33:52 -07003610 trace_ext4_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
Jan Kara914f82a2016-05-13 00:44:16 -04003611 if (iov_iter_rw(iter) == READ)
Linus Torvalds0e01df12016-05-24 12:55:26 -07003612 ret = ext4_direct_IO_read(iocb, iter);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003613 else
Linus Torvalds0e01df12016-05-24 12:55:26 -07003614 ret = ext4_direct_IO_write(iocb, iter);
Omar Sandoval6f673762015-03-16 04:33:52 -07003615 trace_ext4_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), ret);
Mohan Srinivasan25cc70f2016-12-14 16:39:51 -08003616
3617 if (trace_android_fs_dataread_start_enabled() &&
3618 (rw == READ))
3619 trace_android_fs_dataread_end(inode, offset, count);
3620 if (trace_android_fs_datawrite_start_enabled() &&
3621 (rw == WRITE))
3622 trace_android_fs_datawrite_end(inode, offset, count);
3623
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003624 return ret;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003625}
3626
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003627/*
Mingming Cao617ba132006-10-11 01:20:53 -07003628 * Pages can be marked dirty completely asynchronously from ext4's journalling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003629 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3630 * much here because ->set_page_dirty is called under VFS locks. The page is
3631 * not necessarily locked.
3632 *
3633 * We cannot just dirty the page and leave attached buffers clean, because the
3634 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3635 * or jbddirty because all the journalling code will explode.
3636 *
3637 * So what we do is to mark the page "pending dirty" and next time writepage
3638 * is called, propagate that into the buffers appropriately.
3639 */
Mingming Cao617ba132006-10-11 01:20:53 -07003640static int ext4_journalled_set_page_dirty(struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003641{
3642 SetPageChecked(page);
3643 return __set_page_dirty_nobuffers(page);
3644}
3645
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003646static const struct address_space_operations ext4_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003647 .readpage = ext4_readpage,
3648 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003649 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003650 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003651 .write_begin = ext4_write_begin,
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003652 .write_end = ext4_write_end,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003653 .bmap = ext4_bmap,
3654 .invalidatepage = ext4_invalidatepage,
3655 .releasepage = ext4_releasepage,
3656 .direct_IO = ext4_direct_IO,
3657 .migratepage = buffer_migrate_page,
3658 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003659 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003660};
3661
Mingming Cao617ba132006-10-11 01:20:53 -07003662static const struct address_space_operations ext4_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003663 .readpage = ext4_readpage,
3664 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003665 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003666 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003667 .write_begin = ext4_write_begin,
3668 .write_end = ext4_journalled_write_end,
3669 .set_page_dirty = ext4_journalled_set_page_dirty,
3670 .bmap = ext4_bmap,
Jan Kara4520fb32012-12-25 13:28:54 -05003671 .invalidatepage = ext4_journalled_invalidatepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003672 .releasepage = ext4_releasepage,
Theodore Ts'o84ebd792011-08-31 11:56:51 -04003673 .direct_IO = ext4_direct_IO,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003674 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003675 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003676};
3677
Alex Tomas64769242008-07-11 19:27:31 -04003678static const struct address_space_operations ext4_da_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003679 .readpage = ext4_readpage,
3680 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003681 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003682 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003683 .write_begin = ext4_da_write_begin,
3684 .write_end = ext4_da_write_end,
3685 .bmap = ext4_bmap,
3686 .invalidatepage = ext4_da_invalidatepage,
3687 .releasepage = ext4_releasepage,
3688 .direct_IO = ext4_direct_IO,
3689 .migratepage = buffer_migrate_page,
3690 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003691 .error_remove_page = generic_error_remove_page,
Alex Tomas64769242008-07-11 19:27:31 -04003692};
3693
Mingming Cao617ba132006-10-11 01:20:53 -07003694void ext4_set_aops(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003695{
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003696 switch (ext4_inode_journal_mode(inode)) {
3697 case EXT4_INODE_ORDERED_DATA_MODE:
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003698 case EXT4_INODE_WRITEBACK_DATA_MODE:
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003699 break;
3700 case EXT4_INODE_JOURNAL_DATA_MODE:
Mingming Cao617ba132006-10-11 01:20:53 -07003701 inode->i_mapping->a_ops = &ext4_journalled_aops;
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003702 return;
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003703 default:
3704 BUG();
3705 }
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003706 if (test_opt(inode->i_sb, DELALLOC))
3707 inode->i_mapping->a_ops = &ext4_da_aops;
3708 else
3709 inode->i_mapping->a_ops = &ext4_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003710}
3711
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003712static int __ext4_block_zero_page_range(handle_t *handle,
Lukas Czernerd863dc32013-05-27 23:32:35 -04003713 struct address_space *mapping, loff_t from, loff_t length)
3714{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003715 ext4_fsblk_t index = from >> PAGE_SHIFT;
3716 unsigned offset = from & (PAGE_SIZE-1);
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003717 unsigned blocksize, pos;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003718 ext4_lblk_t iblock;
3719 struct inode *inode = mapping->host;
3720 struct buffer_head *bh;
3721 struct page *page;
3722 int err = 0;
3723
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003724 page = find_or_create_page(mapping, from >> PAGE_SHIFT,
Michal Hockoc62d2552015-11-06 16:28:49 -08003725 mapping_gfp_constraint(mapping, ~__GFP_FS));
Lukas Czernerd863dc32013-05-27 23:32:35 -04003726 if (!page)
3727 return -ENOMEM;
3728
3729 blocksize = inode->i_sb->s_blocksize;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003730
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003731 iblock = index << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003732
3733 if (!page_has_buffers(page))
3734 create_empty_buffers(page, blocksize, 0);
3735
3736 /* Find the buffer that contains "offset" */
3737 bh = page_buffers(page);
3738 pos = blocksize;
3739 while (offset >= pos) {
3740 bh = bh->b_this_page;
3741 iblock++;
3742 pos += blocksize;
3743 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003744 if (buffer_freed(bh)) {
3745 BUFFER_TRACE(bh, "freed: skip");
3746 goto unlock;
3747 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003748 if (!buffer_mapped(bh)) {
3749 BUFFER_TRACE(bh, "unmapped");
3750 ext4_get_block(inode, iblock, bh, 0);
3751 /* unmapped? It's a hole - nothing to do */
3752 if (!buffer_mapped(bh)) {
3753 BUFFER_TRACE(bh, "still unmapped");
3754 goto unlock;
3755 }
3756 }
3757
3758 /* Ok, it's mapped. Make sure it's up-to-date */
3759 if (PageUptodate(page))
3760 set_buffer_uptodate(bh);
3761
3762 if (!buffer_uptodate(bh)) {
3763 err = -EIO;
Mike Christiedfec8a12016-06-05 14:31:44 -05003764 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003765 wait_on_buffer(bh);
3766 /* Uhhuh. Read error. Complain and punt. */
3767 if (!buffer_uptodate(bh))
3768 goto unlock;
Michael Halcrowc9c74292015-04-12 00:56:10 -04003769 if (S_ISREG(inode->i_mode) &&
3770 ext4_encrypted_inode(inode)) {
3771 /* We expect the key to be set. */
Jaegeuk Kima7550b32016-07-10 14:01:03 -04003772 BUG_ON(!fscrypt_has_encryption_key(inode));
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003773 BUG_ON(blocksize != PAGE_SIZE);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04003774 WARN_ON_ONCE(fscrypt_decrypt_page(page));
Michael Halcrowc9c74292015-04-12 00:56:10 -04003775 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003776 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003777 if (ext4_should_journal_data(inode)) {
3778 BUFFER_TRACE(bh, "get write access");
3779 err = ext4_journal_get_write_access(handle, bh);
3780 if (err)
3781 goto unlock;
3782 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003783 zero_user(page, offset, length);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003784 BUFFER_TRACE(bh, "zeroed end of block");
3785
Lukas Czernerd863dc32013-05-27 23:32:35 -04003786 if (ext4_should_journal_data(inode)) {
3787 err = ext4_handle_dirty_metadata(handle, inode, bh);
Lukas Czerner0713ed02013-05-27 23:32:35 -04003788 } else {
jon ernst353eefd2013-07-01 08:12:39 -04003789 err = 0;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003790 mark_buffer_dirty(bh);
Jan Kara3957ef52016-04-24 00:56:05 -04003791 if (ext4_should_order_data(inode))
Jan Karaee0876b2016-04-24 00:56:08 -04003792 err = ext4_jbd2_inode_add_write(handle, inode);
Lukas Czerner0713ed02013-05-27 23:32:35 -04003793 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003794
3795unlock:
3796 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003797 put_page(page);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003798 return err;
3799}
3800
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003801/*
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003802 * ext4_block_zero_page_range() zeros out a mapping of length 'length'
3803 * starting from file offset 'from'. The range to be zero'd must
3804 * be contained with in one block. If the specified range exceeds
3805 * the end of the block it will be shortened to end of the block
3806 * that cooresponds to 'from'
3807 */
3808static int ext4_block_zero_page_range(handle_t *handle,
3809 struct address_space *mapping, loff_t from, loff_t length)
3810{
3811 struct inode *inode = mapping->host;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003812 unsigned offset = from & (PAGE_SIZE-1);
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003813 unsigned blocksize = inode->i_sb->s_blocksize;
3814 unsigned max = blocksize - (offset & (blocksize - 1));
3815
3816 /*
3817 * correct length if it does not fall between
3818 * 'from' and the end of the block
3819 */
3820 if (length > max || length < 0)
3821 length = max;
3822
3823 if (IS_DAX(inode))
3824 return dax_zero_page_range(inode, from, length, ext4_get_block);
3825 return __ext4_block_zero_page_range(handle, mapping, from, length);
3826}
3827
3828/*
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003829 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
3830 * up to the end of the block which corresponds to `from'.
3831 * This required during truncate. We need to physically zero the tail end
3832 * of that block so it doesn't yield old data if the file is later grown.
3833 */
Stephen Hemmingerc1978552014-05-12 10:50:23 -04003834static int ext4_block_truncate_page(handle_t *handle,
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003835 struct address_space *mapping, loff_t from)
3836{
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003837 unsigned offset = from & (PAGE_SIZE-1);
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003838 unsigned length;
3839 unsigned blocksize;
3840 struct inode *inode = mapping->host;
3841
3842 blocksize = inode->i_sb->s_blocksize;
3843 length = blocksize - (offset & (blocksize - 1));
3844
3845 return ext4_block_zero_page_range(handle, mapping, from, length);
3846}
3847
Lukas Czernera87dd182013-05-27 23:32:35 -04003848int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
3849 loff_t lstart, loff_t length)
3850{
3851 struct super_block *sb = inode->i_sb;
3852 struct address_space *mapping = inode->i_mapping;
Lukas Czernere1be3a92013-07-01 08:12:39 -04003853 unsigned partial_start, partial_end;
Lukas Czernera87dd182013-05-27 23:32:35 -04003854 ext4_fsblk_t start, end;
3855 loff_t byte_end = (lstart + length - 1);
3856 int err = 0;
3857
Lukas Czernere1be3a92013-07-01 08:12:39 -04003858 partial_start = lstart & (sb->s_blocksize - 1);
3859 partial_end = byte_end & (sb->s_blocksize - 1);
3860
Lukas Czernera87dd182013-05-27 23:32:35 -04003861 start = lstart >> sb->s_blocksize_bits;
3862 end = byte_end >> sb->s_blocksize_bits;
3863
3864 /* Handle partial zero within the single block */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003865 if (start == end &&
3866 (partial_start || (partial_end != sb->s_blocksize - 1))) {
Lukas Czernera87dd182013-05-27 23:32:35 -04003867 err = ext4_block_zero_page_range(handle, mapping,
3868 lstart, length);
3869 return err;
3870 }
3871 /* Handle partial zero out on the start of the range */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003872 if (partial_start) {
Lukas Czernera87dd182013-05-27 23:32:35 -04003873 err = ext4_block_zero_page_range(handle, mapping,
3874 lstart, sb->s_blocksize);
3875 if (err)
3876 return err;
3877 }
3878 /* Handle partial zero out on the end of the range */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003879 if (partial_end != sb->s_blocksize - 1)
Lukas Czernera87dd182013-05-27 23:32:35 -04003880 err = ext4_block_zero_page_range(handle, mapping,
Lukas Czernere1be3a92013-07-01 08:12:39 -04003881 byte_end - partial_end,
3882 partial_end + 1);
Lukas Czernera87dd182013-05-27 23:32:35 -04003883 return err;
3884}
3885
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003886int ext4_can_truncate(struct inode *inode)
3887{
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003888 if (S_ISREG(inode->i_mode))
3889 return 1;
3890 if (S_ISDIR(inode->i_mode))
3891 return 1;
3892 if (S_ISLNK(inode->i_mode))
3893 return !ext4_inode_is_fast_symlink(inode);
3894 return 0;
3895}
3896
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003897/*
Jan Kara01127842015-12-07 14:34:49 -05003898 * We have to make sure i_disksize gets properly updated before we truncate
3899 * page cache due to hole punching or zero range. Otherwise i_disksize update
3900 * can get lost as it may have been postponed to submission of writeback but
3901 * that will never happen after we truncate page cache.
3902 */
3903int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset,
3904 loff_t len)
3905{
3906 handle_t *handle;
3907 loff_t size = i_size_read(inode);
3908
Al Viro59551022016-01-22 15:40:57 -05003909 WARN_ON(!inode_is_locked(inode));
Jan Kara01127842015-12-07 14:34:49 -05003910 if (offset > size || offset + len < size)
3911 return 0;
3912
3913 if (EXT4_I(inode)->i_disksize >= size)
3914 return 0;
3915
3916 handle = ext4_journal_start(inode, EXT4_HT_MISC, 1);
3917 if (IS_ERR(handle))
3918 return PTR_ERR(handle);
3919 ext4_update_i_disksize(inode, size);
3920 ext4_mark_inode_dirty(handle, inode);
3921 ext4_journal_stop(handle);
3922
3923 return 0;
3924}
3925
3926/*
Ross Zwislercca32b72016-09-22 11:49:38 -04003927 * ext4_punch_hole: punches a hole in a file by releasing the blocks
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003928 * associated with the given offset and length
3929 *
3930 * @inode: File inode
3931 * @offset: The offset where the hole will begin
3932 * @len: The length of the hole
3933 *
Anatol Pomozov4907cb72012-09-01 10:31:09 -07003934 * Returns: 0 on success or negative on failure
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003935 */
3936
Ashish Sangwanaeb28172013-07-01 08:12:38 -04003937int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length)
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003938{
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003939 struct super_block *sb = inode->i_sb;
3940 ext4_lblk_t first_block, stop_block;
3941 struct address_space *mapping = inode->i_mapping;
Lukas Czernera87dd182013-05-27 23:32:35 -04003942 loff_t first_block_offset, last_block_offset;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003943 handle_t *handle;
3944 unsigned int credits;
3945 int ret = 0;
3946
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003947 if (!S_ISREG(inode->i_mode))
Allison Henderson73355192012-03-21 22:23:31 -04003948 return -EOPNOTSUPP;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003949
Lukas Czernerb8a86842014-03-18 18:05:35 -04003950 trace_ext4_punch_hole(inode, offset, length, 0);
Zheng Liuaaddea82013-01-16 20:21:26 -05003951
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003952 /*
3953 * Write out all dirty pages to avoid race conditions
3954 * Then release them.
3955 */
Ross Zwislercca32b72016-09-22 11:49:38 -04003956 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003957 ret = filemap_write_and_wait_range(mapping, offset,
3958 offset + length - 1);
3959 if (ret)
3960 return ret;
3961 }
3962
Al Viro59551022016-01-22 15:40:57 -05003963 inode_lock(inode);
Lukas Czerner9ef06ce2014-04-12 09:47:00 -04003964
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003965 /* No need to punch hole beyond i_size */
3966 if (offset >= inode->i_size)
3967 goto out_mutex;
3968
3969 /*
3970 * If the hole extends beyond i_size, set the hole
3971 * to end after the page that contains i_size
3972 */
3973 if (offset + length > inode->i_size) {
3974 length = inode->i_size +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003975 PAGE_SIZE - (inode->i_size & (PAGE_SIZE - 1)) -
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003976 offset;
3977 }
3978
Jan Karaa3612932013-08-16 21:19:41 -04003979 if (offset & (sb->s_blocksize - 1) ||
3980 (offset + length) & (sb->s_blocksize - 1)) {
3981 /*
3982 * Attach jinode to inode for jbd2 if we do any zeroing of
3983 * partial block
3984 */
3985 ret = ext4_inode_attach_jinode(inode);
3986 if (ret < 0)
3987 goto out_mutex;
3988
3989 }
3990
Jan Karaea3d7202015-12-07 14:28:03 -05003991 /* Wait all existing dio workers, newcomers will block on i_mutex */
3992 ext4_inode_block_unlocked_dio(inode);
3993 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);
Lukas Czernera87dd182013-05-27 23:32:35 -04004000 first_block_offset = round_up(offset, sb->s_blocksize);
4001 last_block_offset = round_down((offset + length), sb->s_blocksize) - 1;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004002
Lukas Czernera87dd182013-05-27 23:32:35 -04004003 /* Now release the pages and zero block aligned part of pages*/
Jan Kara01127842015-12-07 14:34:49 -05004004 if (last_block_offset > first_block_offset) {
4005 ret = ext4_update_disksize_before_punch(inode, offset, length);
4006 if (ret)
4007 goto out_dio;
Lukas Czernera87dd182013-05-27 23:32:35 -04004008 truncate_pagecache_range(inode, first_block_offset,
4009 last_block_offset);
Jan Kara01127842015-12-07 14:34:49 -05004010 }
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004011
4012 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4013 credits = ext4_writepage_trans_blocks(inode);
4014 else
4015 credits = ext4_blocks_for_truncate(inode);
4016 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
4017 if (IS_ERR(handle)) {
4018 ret = PTR_ERR(handle);
4019 ext4_std_error(sb, ret);
4020 goto out_dio;
4021 }
4022
Lukas Czernera87dd182013-05-27 23:32:35 -04004023 ret = ext4_zero_partial_blocks(handle, inode, offset,
4024 length);
4025 if (ret)
4026 goto out_stop;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004027
4028 first_block = (offset + sb->s_blocksize - 1) >>
4029 EXT4_BLOCK_SIZE_BITS(sb);
4030 stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
4031
4032 /* If there are no blocks to remove, return now */
4033 if (first_block >= stop_block)
4034 goto out_stop;
4035
4036 down_write(&EXT4_I(inode)->i_data_sem);
4037 ext4_discard_preallocations(inode);
4038
4039 ret = ext4_es_remove_extent(inode, first_block,
4040 stop_block - first_block);
4041 if (ret) {
4042 up_write(&EXT4_I(inode)->i_data_sem);
4043 goto out_stop;
4044 }
4045
4046 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4047 ret = ext4_ext_remove_space(inode, first_block,
4048 stop_block - 1);
4049 else
Lukas Czerner4f579ae2014-07-15 06:03:38 -04004050 ret = ext4_ind_remove_space(handle, inode, first_block,
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004051 stop_block);
4052
Theodore Ts'o819c4922013-04-03 12:47:17 -04004053 up_write(&EXT4_I(inode)->i_data_sem);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004054 if (IS_SYNC(inode))
4055 ext4_handle_sync(handle);
Maxim Patlasove251f9b2014-02-20 16:58:05 -05004056
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004057 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4058 ext4_mark_inode_dirty(handle, inode);
4059out_stop:
4060 ext4_journal_stop(handle);
4061out_dio:
Jan Karaea3d7202015-12-07 14:28:03 -05004062 up_write(&EXT4_I(inode)->i_mmap_sem);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004063 ext4_inode_resume_unlocked_dio(inode);
4064out_mutex:
Al Viro59551022016-01-22 15:40:57 -05004065 inode_unlock(inode);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04004066 return ret;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004067}
4068
Jan Karaa3612932013-08-16 21:19:41 -04004069int ext4_inode_attach_jinode(struct inode *inode)
4070{
4071 struct ext4_inode_info *ei = EXT4_I(inode);
4072 struct jbd2_inode *jinode;
4073
4074 if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal)
4075 return 0;
4076
4077 jinode = jbd2_alloc_inode(GFP_KERNEL);
4078 spin_lock(&inode->i_lock);
4079 if (!ei->jinode) {
4080 if (!jinode) {
4081 spin_unlock(&inode->i_lock);
4082 return -ENOMEM;
4083 }
4084 ei->jinode = jinode;
4085 jbd2_journal_init_jbd_inode(ei->jinode, inode);
4086 jinode = NULL;
4087 }
4088 spin_unlock(&inode->i_lock);
4089 if (unlikely(jinode != NULL))
4090 jbd2_free_inode(jinode);
4091 return 0;
4092}
4093
Allison Hendersona4bb6b62011-05-25 07:41:50 -04004094/*
Mingming Cao617ba132006-10-11 01:20:53 -07004095 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004096 *
Mingming Cao617ba132006-10-11 01:20:53 -07004097 * We block out ext4_get_block() block instantiations across the entire
4098 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004099 * simultaneously on behalf of the same inode.
4100 *
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08004101 * As we work through the truncate and commit bits of it to the journal there
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004102 * is one core, guiding principle: the file's tree must always be consistent on
4103 * disk. We must be able to restart the truncate after a crash.
4104 *
4105 * The file's tree may be transiently inconsistent in memory (although it
4106 * probably isn't), but whenever we close off and commit a journal transaction,
4107 * the contents of (the filesystem + the journal) must be consistent and
4108 * restartable. It's pretty simple, really: bottom up, right to left (although
4109 * left-to-right works OK too).
4110 *
4111 * Note that at recovery time, journal replay occurs *before* the restart of
4112 * truncate against the orphan inode list.
4113 *
4114 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07004115 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004116 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07004117 * ext4_truncate() to have another go. So there will be instantiated blocks
4118 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004119 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07004120 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004121 */
Mingming Cao617ba132006-10-11 01:20:53 -07004122void ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004123{
Theodore Ts'o819c4922013-04-03 12:47:17 -04004124 struct ext4_inode_info *ei = EXT4_I(inode);
4125 unsigned int credits;
4126 handle_t *handle;
4127 struct address_space *mapping = inode->i_mapping;
Theodore Ts'o819c4922013-04-03 12:47:17 -04004128
Theodore Ts'o19b5ef62013-04-03 21:58:52 -04004129 /*
4130 * There is a possibility that we're either freeing the inode
Matthew Wilcoxe04027e2014-03-24 15:15:07 -04004131 * or it's a completely new inode. In those cases we might not
Theodore Ts'o19b5ef62013-04-03 21:58:52 -04004132 * have i_mutex locked because it's not necessary.
4133 */
4134 if (!(inode->i_state & (I_NEW|I_FREEING)))
Al Viro59551022016-01-22 15:40:57 -05004135 WARN_ON(!inode_is_locked(inode));
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004136 trace_ext4_truncate_enter(inode);
4137
Duane Griffin91ef4ca2008-07-11 19:27:31 -04004138 if (!ext4_can_truncate(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004139 return;
4140
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004141 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004142
Theodore Ts'o5534fb52009-09-17 09:34:16 -04004143 if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004144 ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
Theodore Ts'o7d8f9f72009-02-24 08:21:14 -05004145
Tao Maaef1c852012-12-10 14:06:02 -05004146 if (ext4_has_inline_data(inode)) {
4147 int has_inline = 1;
4148
4149 ext4_inline_data_truncate(inode, &has_inline);
4150 if (has_inline)
4151 return;
4152 }
4153
Jan Karaa3612932013-08-16 21:19:41 -04004154 /* If we zero-out tail of the page, we have to create jinode for jbd2 */
4155 if (inode->i_size & (inode->i_sb->s_blocksize - 1)) {
4156 if (ext4_inode_attach_jinode(inode) < 0)
4157 return;
4158 }
4159
Amir Goldsteinff9893d2011-06-27 16:36:31 -04004160 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Theodore Ts'o819c4922013-04-03 12:47:17 -04004161 credits = ext4_writepage_trans_blocks(inode);
Amir Goldsteinff9893d2011-06-27 16:36:31 -04004162 else
Theodore Ts'o819c4922013-04-03 12:47:17 -04004163 credits = ext4_blocks_for_truncate(inode);
4164
4165 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
4166 if (IS_ERR(handle)) {
4167 ext4_std_error(inode->i_sb, PTR_ERR(handle));
4168 return;
4169 }
4170
Lukas Czernereb3544c2013-05-27 23:32:35 -04004171 if (inode->i_size & (inode->i_sb->s_blocksize - 1))
4172 ext4_block_truncate_page(handle, mapping, inode->i_size);
Theodore Ts'o819c4922013-04-03 12:47:17 -04004173
4174 /*
4175 * We add the inode to the orphan list, so that if this
4176 * truncate spans multiple transactions, and we crash, we will
4177 * resume the truncate when the filesystem recovers. It also
4178 * marks the inode dirty, to catch the new size.
4179 *
4180 * Implication: the file must always be in a sane, consistent
4181 * truncatable state while each transaction commits.
4182 */
4183 if (ext4_orphan_add(handle, inode))
4184 goto out_stop;
4185
4186 down_write(&EXT4_I(inode)->i_data_sem);
4187
4188 ext4_discard_preallocations(inode);
4189
4190 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4191 ext4_ext_truncate(handle, inode);
4192 else
4193 ext4_ind_truncate(handle, inode);
4194
4195 up_write(&ei->i_data_sem);
4196
4197 if (IS_SYNC(inode))
4198 ext4_handle_sync(handle);
4199
4200out_stop:
4201 /*
4202 * If this was a simple ftruncate() and the file will remain alive,
4203 * then we need to clear up the orphan record which we created above.
4204 * However, if this was a real unlink then we were called by
Wang Shilong58d86a52014-11-25 16:17:29 -05004205 * ext4_evict_inode(), and we allow that function to clean up the
Theodore Ts'o819c4922013-04-03 12:47:17 -04004206 * orphan info for us.
4207 */
4208 if (inode->i_nlink)
4209 ext4_orphan_del(handle, inode);
4210
4211 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4212 ext4_mark_inode_dirty(handle, inode);
4213 ext4_journal_stop(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07004214
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004215 trace_ext4_truncate_exit(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004216}
4217
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004218/*
Mingming Cao617ba132006-10-11 01:20:53 -07004219 * ext4_get_inode_loc returns with an extra refcount against the inode's
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004220 * underlying buffer_head on success. If 'in_mem' is true, we have all
4221 * data in memory that is needed to recreate the on-disk version of this
4222 * inode.
4223 */
Mingming Cao617ba132006-10-11 01:20:53 -07004224static int __ext4_get_inode_loc(struct inode *inode,
4225 struct ext4_iloc *iloc, int in_mem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004226{
Theodore Ts'o240799c2008-10-09 23:53:47 -04004227 struct ext4_group_desc *gdp;
4228 struct buffer_head *bh;
4229 struct super_block *sb = inode->i_sb;
4230 ext4_fsblk_t block;
4231 int inodes_per_block, inode_offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004232
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004233 iloc->bh = NULL;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004234 if (!ext4_valid_inum(sb, inode->i_ino))
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004235 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004236
Theodore Ts'o240799c2008-10-09 23:53:47 -04004237 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
4238 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
4239 if (!gdp)
4240 return -EIO;
4241
4242 /*
4243 * Figure out the offset within the block group inode table
4244 */
Tao Ma00d09882011-05-09 10:26:41 -04004245 inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004246 inode_offset = ((inode->i_ino - 1) %
4247 EXT4_INODES_PER_GROUP(sb));
4248 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
4249 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
4250
4251 bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05004252 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -05004253 return -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004254 if (!buffer_uptodate(bh)) {
4255 lock_buffer(bh);
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04004256
4257 /*
4258 * If the buffer has the write error flag, we have failed
4259 * to write out another inode in the same block. In this
4260 * case, we don't have to read the block because we may
4261 * read the old inode data successfully.
4262 */
4263 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
4264 set_buffer_uptodate(bh);
4265
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004266 if (buffer_uptodate(bh)) {
4267 /* someone brought it uptodate while we waited */
4268 unlock_buffer(bh);
4269 goto has_buffer;
4270 }
4271
4272 /*
4273 * If we have all information of the inode in memory and this
4274 * is the only valid inode in the block, we need not read the
4275 * block.
4276 */
4277 if (in_mem) {
4278 struct buffer_head *bitmap_bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004279 int i, start;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004280
Theodore Ts'o240799c2008-10-09 23:53:47 -04004281 start = inode_offset & ~(inodes_per_block - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004282
4283 /* Is the inode bitmap in cache? */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004284 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
Wang Shilongaebf0242013-01-12 16:28:47 -05004285 if (unlikely(!bitmap_bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004286 goto make_io;
4287
4288 /*
4289 * If the inode bitmap isn't in cache then the
4290 * optimisation may end up performing two reads instead
4291 * of one, so skip it.
4292 */
4293 if (!buffer_uptodate(bitmap_bh)) {
4294 brelse(bitmap_bh);
4295 goto make_io;
4296 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04004297 for (i = start; i < start + inodes_per_block; i++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004298 if (i == inode_offset)
4299 continue;
Mingming Cao617ba132006-10-11 01:20:53 -07004300 if (ext4_test_bit(i, bitmap_bh->b_data))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004301 break;
4302 }
4303 brelse(bitmap_bh);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004304 if (i == start + inodes_per_block) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004305 /* all other inodes are free, so skip I/O */
4306 memset(bh->b_data, 0, bh->b_size);
4307 set_buffer_uptodate(bh);
4308 unlock_buffer(bh);
4309 goto has_buffer;
4310 }
4311 }
4312
4313make_io:
4314 /*
Theodore Ts'o240799c2008-10-09 23:53:47 -04004315 * If we need to do any I/O, try to pre-readahead extra
4316 * blocks from the inode table.
4317 */
4318 if (EXT4_SB(sb)->s_inode_readahead_blks) {
4319 ext4_fsblk_t b, end, table;
4320 unsigned num;
Theodore Ts'o0d606e22013-04-23 08:59:35 -04004321 __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004322
4323 table = ext4_inode_table(sb, gdp);
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004324 /* s_inode_readahead_blks is always a power of 2 */
Theodore Ts'o0d606e22013-04-23 08:59:35 -04004325 b = block & ~((ext4_fsblk_t) ra_blks - 1);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004326 if (table > b)
4327 b = table;
Theodore Ts'o0d606e22013-04-23 08:59:35 -04004328 end = b + ra_blks;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004329 num = EXT4_INODES_PER_GROUP(sb);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04004330 if (ext4_has_group_desc_csum(sb))
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004331 num -= ext4_itable_unused_count(sb, gdp);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004332 table += num / inodes_per_block;
4333 if (end > table)
4334 end = table;
4335 while (b <= end)
4336 sb_breadahead(sb, b++);
4337 }
4338
4339 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004340 * There are other valid inodes in the buffer, this inode
4341 * has in-inode xattrs, or we don't have this inode in memory.
4342 * Read the block from disk.
4343 */
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004344 trace_ext4_load_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004345 get_bh(bh);
4346 bh->b_end_io = end_buffer_read_sync;
Mike Christie2a222ca2016-06-05 14:31:43 -05004347 submit_bh(REQ_OP_READ, REQ_META | REQ_PRIO, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004348 wait_on_buffer(bh);
4349 if (!buffer_uptodate(bh)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -04004350 EXT4_ERROR_INODE_BLOCK(inode, block,
4351 "unable to read itable block");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004352 brelse(bh);
4353 return -EIO;
4354 }
4355 }
4356has_buffer:
4357 iloc->bh = bh;
4358 return 0;
4359}
4360
Mingming Cao617ba132006-10-11 01:20:53 -07004361int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004362{
4363 /* We have all inode data except xattrs in memory here. */
Mingming Cao617ba132006-10-11 01:20:53 -07004364 return __ext4_get_inode_loc(inode, iloc,
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004365 !ext4_test_inode_state(inode, EXT4_STATE_XATTR));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004366}
4367
Mingming Cao617ba132006-10-11 01:20:53 -07004368void ext4_set_inode_flags(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004369{
Mingming Cao617ba132006-10-11 01:20:53 -07004370 unsigned int flags = EXT4_I(inode)->i_flags;
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004371 unsigned int new_fl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004372
Mingming Cao617ba132006-10-11 01:20:53 -07004373 if (flags & EXT4_SYNC_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004374 new_fl |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07004375 if (flags & EXT4_APPEND_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004376 new_fl |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07004377 if (flags & EXT4_IMMUTABLE_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004378 new_fl |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07004379 if (flags & EXT4_NOATIME_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004380 new_fl |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07004381 if (flags & EXT4_DIRSYNC_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004382 new_fl |= S_DIRSYNC;
Ross Zwisler0a6cf912016-02-26 15:19:46 -08004383 if (test_opt(inode->i_sb, DAX) && S_ISREG(inode->i_mode))
Ross Zwisler923ae0f2015-02-16 15:59:38 -08004384 new_fl |= S_DAX;
Theodore Ts'o5f16f322014-03-24 14:43:12 -04004385 inode_set_flags(inode, new_fl,
Ross Zwisler923ae0f2015-02-16 15:59:38 -08004386 S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004387}
4388
Jan Karaff9ddf72007-07-18 09:24:20 -04004389/* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
4390void ext4_get_inode_flags(struct ext4_inode_info *ei)
4391{
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004392 unsigned int vfs_fl;
4393 unsigned long old_fl, new_fl;
Jan Karaff9ddf72007-07-18 09:24:20 -04004394
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004395 do {
4396 vfs_fl = ei->vfs_inode.i_flags;
4397 old_fl = ei->i_flags;
4398 new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
4399 EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|
4400 EXT4_DIRSYNC_FL);
4401 if (vfs_fl & S_SYNC)
4402 new_fl |= EXT4_SYNC_FL;
4403 if (vfs_fl & S_APPEND)
4404 new_fl |= EXT4_APPEND_FL;
4405 if (vfs_fl & S_IMMUTABLE)
4406 new_fl |= EXT4_IMMUTABLE_FL;
4407 if (vfs_fl & S_NOATIME)
4408 new_fl |= EXT4_NOATIME_FL;
4409 if (vfs_fl & S_DIRSYNC)
4410 new_fl |= EXT4_DIRSYNC_FL;
4411 } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl);
Jan Karaff9ddf72007-07-18 09:24:20 -04004412}
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004413
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004414static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004415 struct ext4_inode_info *ei)
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004416{
4417 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004418 struct inode *inode = &(ei->vfs_inode);
4419 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004420
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004421 if (ext4_has_feature_huge_file(sb)) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004422 /* we are using combined 48 bit field */
4423 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
4424 le32_to_cpu(raw_inode->i_blocks_lo);
Theodore Ts'o07a03822010-06-14 09:54:48 -04004425 if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004426 /* i_blocks represent file system block size */
4427 return i_blocks << (inode->i_blkbits - 9);
4428 } else {
4429 return i_blocks;
4430 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004431 } else {
4432 return le32_to_cpu(raw_inode->i_blocks_lo);
4433 }
4434}
Jan Karaff9ddf72007-07-18 09:24:20 -04004435
Tao Ma152a7b02012-12-02 11:13:24 -05004436static inline void ext4_iget_extra_inode(struct inode *inode,
4437 struct ext4_inode *raw_inode,
4438 struct ext4_inode_info *ei)
4439{
4440 __le32 *magic = (void *)raw_inode +
4441 EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
Tao Ma67cf5b02012-12-10 14:04:46 -05004442 if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
Tao Ma152a7b02012-12-02 11:13:24 -05004443 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Tao Ma67cf5b02012-12-10 14:04:46 -05004444 ext4_find_inline_data_nolock(inode);
Tao Maf19d5872012-12-10 14:05:51 -05004445 } else
4446 EXT4_I(inode)->i_inline_off = 0;
Tao Ma152a7b02012-12-02 11:13:24 -05004447}
4448
Li Xi040cb372016-01-08 16:01:21 -05004449int ext4_get_projid(struct inode *inode, kprojid_t *projid)
4450{
Kaho Ng0b7b7772016-09-05 23:11:58 -04004451 if (!ext4_has_feature_project(inode->i_sb))
Li Xi040cb372016-01-08 16:01:21 -05004452 return -EOPNOTSUPP;
4453 *projid = EXT4_I(inode)->i_projid;
4454 return 0;
4455}
4456
David Howells1d1fe1e2008-02-07 00:15:37 -08004457struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004458{
Mingming Cao617ba132006-10-11 01:20:53 -07004459 struct ext4_iloc iloc;
4460 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08004461 struct ext4_inode_info *ei;
David Howells1d1fe1e2008-02-07 00:15:37 -08004462 struct inode *inode;
Jan Karab436b9b2009-12-08 23:51:10 -05004463 journal_t *journal = EXT4_SB(sb)->s_journal;
David Howells1d1fe1e2008-02-07 00:15:37 -08004464 long ret;
Darrick J. Wong3e4f8da2016-12-10 09:55:01 -05004465 loff_t size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004466 int block;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004467 uid_t i_uid;
4468 gid_t i_gid;
Li Xi040cb372016-01-08 16:01:21 -05004469 projid_t i_projid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004470
David Howells1d1fe1e2008-02-07 00:15:37 -08004471 inode = iget_locked(sb, ino);
4472 if (!inode)
4473 return ERR_PTR(-ENOMEM);
4474 if (!(inode->i_state & I_NEW))
4475 return inode;
4476
4477 ei = EXT4_I(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05004478 iloc.bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004479
David Howells1d1fe1e2008-02-07 00:15:37 -08004480 ret = __ext4_get_inode_loc(inode, &iloc, 0);
4481 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004482 goto bad_inode;
Mingming Cao617ba132006-10-11 01:20:53 -07004483 raw_inode = ext4_raw_inode(&iloc);
Darrick J. Wong814525f2012-04-29 18:31:10 -04004484
4485 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4486 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
4487 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
4488 EXT4_INODE_SIZE(inode->i_sb)) {
4489 EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)",
4490 EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize,
4491 EXT4_INODE_SIZE(inode->i_sb));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004492 ret = -EFSCORRUPTED;
Darrick J. Wong814525f2012-04-29 18:31:10 -04004493 goto bad_inode;
4494 }
4495 } else
4496 ei->i_extra_isize = 0;
4497
4498 /* Precompute checksum seed for inode metadata */
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04004499 if (ext4_has_metadata_csum(sb)) {
Darrick J. Wong814525f2012-04-29 18:31:10 -04004500 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4501 __u32 csum;
4502 __le32 inum = cpu_to_le32(inode->i_ino);
4503 __le32 gen = raw_inode->i_generation;
4504 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
4505 sizeof(inum));
4506 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
4507 sizeof(gen));
4508 }
4509
4510 if (!ext4_inode_csum_verify(inode, raw_inode, ei)) {
4511 EXT4_ERROR_INODE(inode, "checksum invalid");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004512 ret = -EFSBADCRC;
Darrick J. Wong814525f2012-04-29 18:31:10 -04004513 goto bad_inode;
4514 }
4515
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004516 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004517 i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4518 i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Kaho Ng0b7b7772016-09-05 23:11:58 -04004519 if (ext4_has_feature_project(sb) &&
Li Xi040cb372016-01-08 16:01:21 -05004520 EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE &&
4521 EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
4522 i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid);
4523 else
4524 i_projid = EXT4_DEF_PROJID;
4525
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004526 if (!(test_opt(inode->i_sb, NO_UID32))) {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004527 i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4528 i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004529 }
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004530 i_uid_write(inode, i_uid);
4531 i_gid_write(inode, i_gid);
Li Xi040cb372016-01-08 16:01:21 -05004532 ei->i_projid = make_kprojid(&init_user_ns, i_projid);
Miklos Szeredibfe86842011-10-28 14:13:29 +02004533 set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004534
Theodore Ts'o353eb832011-01-10 12:18:25 -05004535 ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
Tao Ma67cf5b02012-12-10 14:04:46 -05004536 ei->i_inline_off = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004537 ei->i_dir_start_lookup = 0;
4538 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4539 /* We now have enough fields to check if the inode was active or not.
4540 * This is needed because nfsd might try to access dead inodes
4541 * the test is that same one that e2fsck uses
4542 * NeilBrown 1999oct15
4543 */
4544 if (inode->i_nlink == 0) {
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004545 if ((inode->i_mode == 0 ||
4546 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) &&
4547 ino != EXT4_BOOT_LOADER_INO) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004548 /* this inode is deleted */
David Howells1d1fe1e2008-02-07 00:15:37 -08004549 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004550 goto bad_inode;
4551 }
4552 /* The only unlinked inodes we let through here have
4553 * valid i_mode and are being read by the orphan
4554 * recovery code: that's fine, we're about to complete
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004555 * the process of deleting those.
4556 * OR it is the EXT4_BOOT_LOADER_INO which is
4557 * not initialized on a new filesystem. */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004558 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004559 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004560 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004561 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004562 if (ext4_has_feature_64bit(sb))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004563 ei->i_file_acl |=
4564 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004565 inode->i_size = ext4_isize(raw_inode);
Darrick J. Wong3e4f8da2016-12-10 09:55:01 -05004566 if ((size = i_size_read(inode)) < 0) {
4567 EXT4_ERROR_INODE(inode, "bad i_size value: %lld", size);
4568 ret = -EFSCORRUPTED;
4569 goto bad_inode;
4570 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004571 ei->i_disksize = inode->i_size;
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03004572#ifdef CONFIG_QUOTA
4573 ei->i_reserved_quota = 0;
4574#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004575 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4576 ei->i_block_group = iloc.block_group;
Theodore Ts'oa4912122009-03-12 12:18:34 -04004577 ei->i_last_alloc_group = ~0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004578 /*
4579 * NOTE! The in-memory inode i_data array is in little-endian order
4580 * even on big-endian machines: we do NOT byteswap the block numbers!
4581 */
Mingming Cao617ba132006-10-11 01:20:53 -07004582 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004583 ei->i_data[block] = raw_inode->i_block[block];
4584 INIT_LIST_HEAD(&ei->i_orphan);
4585
Jan Karab436b9b2009-12-08 23:51:10 -05004586 /*
4587 * Set transaction id's of transactions that have to be committed
4588 * to finish f[data]sync. We set them to currently running transaction
4589 * as we cannot be sure that the inode or some of its metadata isn't
4590 * part of the transaction - the inode could have been reclaimed and
4591 * now it is reread from disk.
4592 */
4593 if (journal) {
4594 transaction_t *transaction;
4595 tid_t tid;
4596
Theodore Ts'oa931da62010-08-03 21:35:12 -04004597 read_lock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05004598 if (journal->j_running_transaction)
4599 transaction = journal->j_running_transaction;
4600 else
4601 transaction = journal->j_committing_transaction;
4602 if (transaction)
4603 tid = transaction->t_tid;
4604 else
4605 tid = journal->j_commit_sequence;
Theodore Ts'oa931da62010-08-03 21:35:12 -04004606 read_unlock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05004607 ei->i_sync_tid = tid;
4608 ei->i_datasync_tid = tid;
4609 }
4610
Eric Sandeen0040d982008-02-05 22:36:43 -05004611 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004612 if (ei->i_extra_isize == 0) {
4613 /* The extra space is currently unused. Use it. */
Mingming Cao617ba132006-10-11 01:20:53 -07004614 ei->i_extra_isize = sizeof(struct ext4_inode) -
4615 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004616 } else {
Tao Ma152a7b02012-12-02 11:13:24 -05004617 ext4_iget_extra_inode(inode, raw_inode, ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004618 }
Darrick J. Wong814525f2012-04-29 18:31:10 -04004619 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004620
Kalpak Shahef7f3832007-07-18 09:15:20 -04004621 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4622 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4623 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4624 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4625
Theodore Ts'oed3654e2014-03-24 14:09:06 -04004626 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
Theodore Ts'oc4f65702014-03-20 00:32:57 -04004627 inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
4628 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4629 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4630 inode->i_version |=
4631 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
4632 }
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004633 }
4634
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004635 ret = 0;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004636 if (ei->i_file_acl &&
Theodore Ts'o10329882009-11-15 15:29:56 -05004637 !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04004638 EXT4_ERROR_INODE(inode, "bad extended attribute block %llu",
4639 ei->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004640 ret = -EFSCORRUPTED;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004641 goto bad_inode;
Tao Maf19d5872012-12-10 14:05:51 -05004642 } else if (!ext4_has_inline_data(inode)) {
4643 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
4644 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4645 (S_ISLNK(inode->i_mode) &&
4646 !ext4_inode_is_fast_symlink(inode))))
4647 /* Validate extent which is part of inode */
4648 ret = ext4_ext_check_inode(inode);
4649 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4650 (S_ISLNK(inode->i_mode) &&
4651 !ext4_inode_is_fast_symlink(inode))) {
4652 /* Validate block references which are part of inode */
4653 ret = ext4_ind_check_inode(inode);
4654 }
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004655 }
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004656 if (ret)
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004657 goto bad_inode;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -04004658
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004659 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004660 inode->i_op = &ext4_file_inode_operations;
Boaz Harroshbe64f882015-04-15 16:15:17 -07004661 inode->i_fop = &ext4_file_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07004662 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004663 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004664 inode->i_op = &ext4_dir_inode_operations;
4665 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004666 } else if (S_ISLNK(inode->i_mode)) {
Al Viroa7a67e82015-04-27 17:51:30 -04004667 if (ext4_encrypted_inode(inode)) {
4668 inode->i_op = &ext4_encrypted_symlink_inode_operations;
4669 ext4_set_aops(inode);
4670 } else if (ext4_inode_is_fast_symlink(inode)) {
Al Viro75e75662015-05-02 10:13:58 -04004671 inode->i_link = (char *)ei->i_data;
Mingming Cao617ba132006-10-11 01:20:53 -07004672 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00004673 nd_terminate_link(ei->i_data, inode->i_size,
4674 sizeof(ei->i_data) - 1);
4675 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004676 inode->i_op = &ext4_symlink_inode_operations;
4677 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004678 }
Al Viro21fc61c2015-11-17 01:07:57 -05004679 inode_nohighmem(inode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004680 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
4681 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004682 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004683 if (raw_inode->i_block[0])
4684 init_special_inode(inode, inode->i_mode,
4685 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4686 else
4687 init_special_inode(inode, inode->i_mode,
4688 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004689 } else if (ino == EXT4_BOOT_LOADER_INO) {
4690 make_bad_inode(inode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004691 } else {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004692 ret = -EFSCORRUPTED;
Theodore Ts'o24676da2010-05-16 21:00:00 -04004693 EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004694 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004695 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004696 brelse(iloc.bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004697 ext4_set_inode_flags(inode);
David Howells1d1fe1e2008-02-07 00:15:37 -08004698 unlock_new_inode(inode);
4699 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004700
4701bad_inode:
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004702 brelse(iloc.bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004703 iget_failed(inode);
4704 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004705}
4706
Theodore Ts'of4bb2982014-10-05 22:56:00 -04004707struct inode *ext4_iget_normal(struct super_block *sb, unsigned long ino)
4708{
4709 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004710 return ERR_PTR(-EFSCORRUPTED);
Theodore Ts'of4bb2982014-10-05 22:56:00 -04004711 return ext4_iget(sb, ino);
4712}
4713
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004714static int ext4_inode_blocks_set(handle_t *handle,
4715 struct ext4_inode *raw_inode,
4716 struct ext4_inode_info *ei)
4717{
4718 struct inode *inode = &(ei->vfs_inode);
4719 u64 i_blocks = inode->i_blocks;
4720 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004721
4722 if (i_blocks <= ~0U) {
4723 /*
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004724 * i_blocks can be represented in a 32 bit variable
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004725 * as multiple of 512 bytes
4726 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004727 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004728 raw_inode->i_blocks_high = 0;
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004729 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004730 return 0;
4731 }
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004732 if (!ext4_has_feature_huge_file(sb))
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004733 return -EFBIG;
4734
4735 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004736 /*
4737 * i_blocks can be represented in a 48 bit variable
4738 * as multiple of 512 bytes
4739 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004740 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004741 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004742 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004743 } else {
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004744 ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004745 /* i_block is stored in file system block size */
4746 i_blocks = i_blocks >> (inode->i_blkbits - 9);
4747 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4748 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004749 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004750 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004751}
4752
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004753struct other_inode {
4754 unsigned long orig_ino;
4755 struct ext4_inode *raw_inode;
4756};
4757
4758static int other_inode_match(struct inode * inode, unsigned long ino,
4759 void *data)
4760{
4761 struct other_inode *oi = (struct other_inode *) data;
4762
4763 if ((inode->i_ino != ino) ||
4764 (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW |
4765 I_DIRTY_SYNC | I_DIRTY_DATASYNC)) ||
4766 ((inode->i_state & I_DIRTY_TIME) == 0))
4767 return 0;
4768 spin_lock(&inode->i_lock);
4769 if (((inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW |
4770 I_DIRTY_SYNC | I_DIRTY_DATASYNC)) == 0) &&
4771 (inode->i_state & I_DIRTY_TIME)) {
4772 struct ext4_inode_info *ei = EXT4_I(inode);
4773
4774 inode->i_state &= ~(I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED);
4775 spin_unlock(&inode->i_lock);
4776
4777 spin_lock(&ei->i_raw_lock);
4778 EXT4_INODE_SET_XTIME(i_ctime, inode, oi->raw_inode);
4779 EXT4_INODE_SET_XTIME(i_mtime, inode, oi->raw_inode);
4780 EXT4_INODE_SET_XTIME(i_atime, inode, oi->raw_inode);
4781 ext4_inode_csum_set(inode, oi->raw_inode, ei);
4782 spin_unlock(&ei->i_raw_lock);
4783 trace_ext4_other_inode_update_time(inode, oi->orig_ino);
4784 return -1;
4785 }
4786 spin_unlock(&inode->i_lock);
4787 return -1;
4788}
4789
4790/*
4791 * Opportunistically update the other time fields for other inodes in
4792 * the same inode table block.
4793 */
4794static void ext4_update_other_inodes_time(struct super_block *sb,
4795 unsigned long orig_ino, char *buf)
4796{
4797 struct other_inode oi;
4798 unsigned long ino;
4799 int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
4800 int inode_size = EXT4_INODE_SIZE(sb);
4801
4802 oi.orig_ino = orig_ino;
Theodore Ts'o0f0ff9a2015-07-01 23:37:46 -04004803 /*
4804 * Calculate the first inode in the inode table block. Inode
4805 * numbers are one-based. That is, the first inode in a block
4806 * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1).
4807 */
4808 ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1;
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004809 for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) {
4810 if (ino == orig_ino)
4811 continue;
4812 oi.raw_inode = (struct ext4_inode *) buf;
4813 (void) find_inode_nowait(sb, ino, other_inode_match, &oi);
4814 }
4815}
4816
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004817/*
4818 * Post the struct inode info into an on-disk inode location in the
4819 * buffer-cache. This gobbles the caller's reference to the
4820 * buffer_head in the inode location struct.
4821 *
4822 * The caller must have write access to iloc->bh.
4823 */
Mingming Cao617ba132006-10-11 01:20:53 -07004824static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004825 struct inode *inode,
Frank Mayhar830156c2009-09-29 10:07:47 -04004826 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004827{
Mingming Cao617ba132006-10-11 01:20:53 -07004828 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
4829 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004830 struct buffer_head *bh = iloc->bh;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004831 struct super_block *sb = inode->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004832 int err = 0, rc, block;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004833 int need_datasync = 0, set_large_file = 0;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004834 uid_t i_uid;
4835 gid_t i_gid;
Li Xi040cb372016-01-08 16:01:21 -05004836 projid_t i_projid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004837
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004838 spin_lock(&ei->i_raw_lock);
4839
4840 /* For fields not tracked in the in-memory inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004841 * initialise them to zero for new inodes. */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004842 if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
Mingming Cao617ba132006-10-11 01:20:53 -07004843 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004844
Jan Karaff9ddf72007-07-18 09:24:20 -04004845 ext4_get_inode_flags(ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004846 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004847 i_uid = i_uid_read(inode);
4848 i_gid = i_gid_read(inode);
Li Xi040cb372016-01-08 16:01:21 -05004849 i_projid = from_kprojid(&init_user_ns, ei->i_projid);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004850 if (!(test_opt(inode->i_sb, NO_UID32))) {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004851 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid));
4852 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004853/*
4854 * Fix up interoperability with old kernels. Otherwise, old inodes get
4855 * re-used with the upper 16 bits of the uid/gid intact
4856 */
Daeho Jeong93e3b4e2016-09-05 22:56:10 -04004857 if (ei->i_dtime && list_empty(&ei->i_orphan)) {
4858 raw_inode->i_uid_high = 0;
4859 raw_inode->i_gid_high = 0;
4860 } else {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004861 raw_inode->i_uid_high =
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004862 cpu_to_le16(high_16_bits(i_uid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004863 raw_inode->i_gid_high =
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004864 cpu_to_le16(high_16_bits(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004865 }
4866 } else {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004867 raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid));
4868 raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004869 raw_inode->i_uid_high = 0;
4870 raw_inode->i_gid_high = 0;
4871 }
4872 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004873
4874 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
4875 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
4876 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
4877 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
4878
Li Xibce92d52014-10-01 22:11:06 -04004879 err = ext4_inode_blocks_set(handle, raw_inode, ei);
4880 if (err) {
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004881 spin_unlock(&ei->i_raw_lock);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004882 goto out_brelse;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004883 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004884 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Theodore Ts'o353eb832011-01-10 12:18:25 -05004885 raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
Theodore Ts'oed3654e2014-03-24 14:09:06 -04004886 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT)))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004887 raw_inode->i_file_acl_high =
4888 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004889 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Jan Karab71fc072012-09-26 21:52:20 -04004890 if (ei->i_disksize != ext4_isize(raw_inode)) {
4891 ext4_isize_set(raw_inode, ei->i_disksize);
4892 need_datasync = 1;
4893 }
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004894 if (ei->i_disksize > 0x7fffffffULL) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004895 if (!ext4_has_feature_large_file(sb) ||
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004896 EXT4_SB(sb)->s_es->s_rev_level ==
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004897 cpu_to_le32(EXT4_GOOD_OLD_REV))
4898 set_large_file = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004899 }
4900 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
4901 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
4902 if (old_valid_dev(inode->i_rdev)) {
4903 raw_inode->i_block[0] =
4904 cpu_to_le32(old_encode_dev(inode->i_rdev));
4905 raw_inode->i_block[1] = 0;
4906 } else {
4907 raw_inode->i_block[0] = 0;
4908 raw_inode->i_block[1] =
4909 cpu_to_le32(new_encode_dev(inode->i_rdev));
4910 raw_inode->i_block[2] = 0;
4911 }
Tao Maf19d5872012-12-10 14:05:51 -05004912 } else if (!ext4_has_inline_data(inode)) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004913 for (block = 0; block < EXT4_N_BLOCKS; block++)
4914 raw_inode->i_block[block] = ei->i_data[block];
Tao Maf19d5872012-12-10 14:05:51 -05004915 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004916
Theodore Ts'oed3654e2014-03-24 14:09:06 -04004917 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
Theodore Ts'oc4f65702014-03-20 00:32:57 -04004918 raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
4919 if (ei->i_extra_isize) {
4920 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4921 raw_inode->i_version_hi =
4922 cpu_to_le32(inode->i_version >> 32);
4923 raw_inode->i_extra_isize =
4924 cpu_to_le16(ei->i_extra_isize);
4925 }
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004926 }
Li Xi040cb372016-01-08 16:01:21 -05004927
Kaho Ng0b7b7772016-09-05 23:11:58 -04004928 BUG_ON(!ext4_has_feature_project(inode->i_sb) &&
Li Xi040cb372016-01-08 16:01:21 -05004929 i_projid != EXT4_DEF_PROJID);
4930
4931 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
4932 EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
4933 raw_inode->i_projid = cpu_to_le32(i_projid);
4934
Darrick J. Wong814525f2012-04-29 18:31:10 -04004935 ext4_inode_csum_set(inode, raw_inode, ei);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004936 spin_unlock(&ei->i_raw_lock);
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004937 if (inode->i_sb->s_flags & MS_LAZYTIME)
4938 ext4_update_other_inodes_time(inode->i_sb, inode->i_ino,
4939 bh->b_data);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004940
Frank Mayhar830156c2009-09-29 10:07:47 -04004941 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05004942 rc = ext4_handle_dirty_metadata(handle, NULL, bh);
Frank Mayhar830156c2009-09-29 10:07:47 -04004943 if (!err)
4944 err = rc;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004945 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004946 if (set_large_file) {
liang xie5d601252014-05-12 22:06:43 -04004947 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access");
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004948 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
4949 if (err)
4950 goto out_brelse;
4951 ext4_update_dynamic_rev(sb);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004952 ext4_set_feature_large_file(sb);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004953 ext4_handle_sync(handle);
4954 err = ext4_handle_dirty_super(handle, sb);
4955 }
Jan Karab71fc072012-09-26 21:52:20 -04004956 ext4_update_inode_fsync_trans(handle, inode, need_datasync);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004957out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004958 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004959 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004960 return err;
4961}
4962
4963/*
Mingming Cao617ba132006-10-11 01:20:53 -07004964 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004965 *
4966 * We are called from a few places:
4967 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04004968 * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004969 * Here, there will be no transaction running. We wait for any running
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004970 * transaction to commit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004971 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04004972 * - Within flush work (sys_sync(), kupdate and such).
4973 * We wait on commit, if told to.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004974 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04004975 * - Within iput_final() -> write_inode_now()
4976 * We wait on commit, if told to.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004977 *
4978 * In all cases it is actually safe for us to return without doing anything,
4979 * because the inode has been copied into a raw inode buffer in
Theodore Ts'o87f7e412014-04-08 11:38:28 -04004980 * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL
4981 * writeback.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004982 *
4983 * Note that we are absolutely dependent upon all inode dirtiers doing the
4984 * right thing: they *must* call mark_inode_dirty() after dirtying info in
4985 * which we are interested.
4986 *
4987 * It would be a bug for them to not do this. The code:
4988 *
4989 * mark_inode_dirty(inode)
4990 * stuff();
4991 * inode->i_size = expr;
4992 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04004993 * is in error because write_inode() could occur while `stuff()' is running,
4994 * and the new i_size will be lost. Plus the inode will no longer be on the
4995 * superblock's dirty inode list.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004996 */
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004997int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004998{
Frank Mayhar91ac6f42009-09-09 22:33:47 -04004999 int err;
5000
Theodore Ts'o87f7e412014-04-08 11:38:28 -04005001 if (WARN_ON_ONCE(current->flags & PF_MEMALLOC))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005002 return 0;
5003
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005004 if (EXT4_SB(inode->i_sb)->s_journal) {
5005 if (ext4_journal_current_handle()) {
5006 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
5007 dump_stack();
5008 return -EIO;
5009 }
5010
Jan Kara10542c22014-03-04 10:50:50 -05005011 /*
5012 * No need to force transaction in WB_SYNC_NONE mode. Also
5013 * ext4_sync_fs() will force the commit after everything is
5014 * written.
5015 */
5016 if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync)
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005017 return 0;
5018
5019 err = ext4_force_commit(inode->i_sb);
5020 } else {
5021 struct ext4_iloc iloc;
5022
Curt Wohlgemuth8b472d72010-04-03 16:45:06 -04005023 err = __ext4_get_inode_loc(inode, &iloc, 0);
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005024 if (err)
5025 return err;
Jan Kara10542c22014-03-04 10:50:50 -05005026 /*
5027 * sync(2) will flush the whole buffer cache. No need to do
5028 * it here separately for each inode.
5029 */
5030 if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
Frank Mayhar830156c2009-09-29 10:07:47 -04005031 sync_dirty_buffer(iloc.bh);
5032 if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -04005033 EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr,
5034 "IO error syncing inode");
Frank Mayhar830156c2009-09-29 10:07:47 -04005035 err = -EIO;
5036 }
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04005037 brelse(iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005038 }
Frank Mayhar91ac6f42009-09-09 22:33:47 -04005039 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005040}
5041
5042/*
Jan Kara53e87262012-12-25 13:29:52 -05005043 * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
5044 * buffers that are attached to a page stradding i_size and are undergoing
5045 * commit. In that case we have to wait for commit to finish and try again.
5046 */
5047static void ext4_wait_for_tail_page_commit(struct inode *inode)
5048{
5049 struct page *page;
5050 unsigned offset;
5051 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
5052 tid_t commit_tid = 0;
5053 int ret;
5054
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005055 offset = inode->i_size & (PAGE_SIZE - 1);
Jan Kara53e87262012-12-25 13:29:52 -05005056 /*
5057 * All buffers in the last page remain valid? Then there's nothing to
Kirill A. Shutemovea1754a2016-04-01 15:29:48 +03005058 * do. We do the check mainly to optimize the common PAGE_SIZE ==
Jan Kara53e87262012-12-25 13:29:52 -05005059 * blocksize case
5060 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005061 if (offset > PAGE_SIZE - (1 << inode->i_blkbits))
Jan Kara53e87262012-12-25 13:29:52 -05005062 return;
5063 while (1) {
5064 page = find_lock_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005065 inode->i_size >> PAGE_SHIFT);
Jan Kara53e87262012-12-25 13:29:52 -05005066 if (!page)
5067 return;
Lukas Czernerca99fdd2013-05-21 23:25:01 -04005068 ret = __ext4_journalled_invalidatepage(page, offset,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005069 PAGE_SIZE - offset);
Jan Kara53e87262012-12-25 13:29:52 -05005070 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005071 put_page(page);
Jan Kara53e87262012-12-25 13:29:52 -05005072 if (ret != -EBUSY)
5073 return;
5074 commit_tid = 0;
5075 read_lock(&journal->j_state_lock);
5076 if (journal->j_committing_transaction)
5077 commit_tid = journal->j_committing_transaction->t_tid;
5078 read_unlock(&journal->j_state_lock);
5079 if (commit_tid)
5080 jbd2_log_wait_commit(journal, commit_tid);
5081 }
5082}
5083
5084/*
Mingming Cao617ba132006-10-11 01:20:53 -07005085 * ext4_setattr()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005086 *
5087 * Called from notify_change.
5088 *
5089 * We want to trap VFS attempts to truncate the file as soon as
5090 * possible. In particular, we want to make sure that when the VFS
5091 * shrinks i_size, we put the inode on the orphan list and modify
5092 * i_disksize immediately, so that during the subsequent flushing of
5093 * dirty pages and freeing of disk blocks, we can guarantee that any
5094 * commit will leave the blocks being flushed in an unused state on
5095 * disk. (On recovery, the inode will get truncated and the blocks will
5096 * be freed, so we have a strong guarantee that no future commit will
5097 * leave these blocks visible to the user.)
5098 *
Jan Kara678aaf42008-07-11 19:27:31 -04005099 * Another thing we have to assure is that if we are in ordered mode
5100 * and inode is still attached to the committing transaction, we must
5101 * we start writeout of all the dirty pages which are being truncated.
5102 * This way we are sure that all the data written in the previous
5103 * transaction are already on disk (truncate waits for pages under
5104 * writeback).
5105 *
5106 * Called with inode->i_mutex down.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005107 */
Mingming Cao617ba132006-10-11 01:20:53 -07005108int ext4_setattr(struct dentry *dentry, struct iattr *attr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005109{
David Howells2b0143b2015-03-17 22:25:59 +00005110 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005111 int error, rc = 0;
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04005112 int orphan = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005113 const unsigned int ia_valid = attr->ia_valid;
5114
Jan Kara31051c82016-05-26 16:55:18 +02005115 error = setattr_prepare(dentry, attr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005116 if (error)
5117 return error;
5118
Jan Karaa7cdade2015-06-29 16:22:54 +02005119 if (is_quota_modification(inode, attr)) {
5120 error = dquot_initialize(inode);
5121 if (error)
5122 return error;
5123 }
Eric W. Biederman08cefc72012-02-07 15:41:49 -08005124 if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
5125 (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005126 handle_t *handle;
5127
5128 /* (user+group)*(old+new) structure, inode write (sb,
5129 * inode block, ? - but truncate inode update has it) */
Theodore Ts'o9924a922013-02-08 21:59:22 -05005130 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
5131 (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) +
5132 EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005133 if (IS_ERR(handle)) {
5134 error = PTR_ERR(handle);
5135 goto err_out;
5136 }
Christoph Hellwigb43fa822010-03-03 09:05:03 -05005137 error = dquot_transfer(inode, attr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005138 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07005139 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005140 return error;
5141 }
5142 /* Update corresponding info in inode so that everything is in
5143 * one transaction */
5144 if (attr->ia_valid & ATTR_UID)
5145 inode->i_uid = attr->ia_uid;
5146 if (attr->ia_valid & ATTR_GID)
5147 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07005148 error = ext4_mark_inode_dirty(handle, inode);
5149 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005150 }
5151
Josef Bacik3da40c72015-06-22 00:31:26 -04005152 if (attr->ia_valid & ATTR_SIZE) {
Jan Kara52083862013-08-17 10:07:17 -04005153 handle_t *handle;
Josef Bacik3da40c72015-06-22 00:31:26 -04005154 loff_t oldsize = inode->i_size;
5155 int shrink = (attr->ia_size <= inode->i_size);
Christoph Hellwig562c72a2011-06-24 14:29:45 -04005156
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005157 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
Eric Sandeene2b46572008-01-28 23:58:27 -05005158 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5159
Theodore Ts'o0c095c72010-07-27 11:56:06 -04005160 if (attr->ia_size > sbi->s_bitmap_maxbytes)
5161 return -EFBIG;
Eric Sandeene2b46572008-01-28 23:58:27 -05005162 }
Josef Bacik3da40c72015-06-22 00:31:26 -04005163 if (!S_ISREG(inode->i_mode))
5164 return -EINVAL;
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08005165
5166 if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size)
5167 inode_inc_iversion(inode);
5168
Josef Bacik3da40c72015-06-22 00:31:26 -04005169 if (ext4_should_order_data(inode) &&
Jan Kara52083862013-08-17 10:07:17 -04005170 (attr->ia_size < inode->i_size)) {
Josef Bacik3da40c72015-06-22 00:31:26 -04005171 error = ext4_begin_ordered_truncate(inode,
Jan Kara678aaf42008-07-11 19:27:31 -04005172 attr->ia_size);
Josef Bacik3da40c72015-06-22 00:31:26 -04005173 if (error)
5174 goto err_out;
5175 }
5176 if (attr->ia_size != inode->i_size) {
Jan Kara52083862013-08-17 10:07:17 -04005177 handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
5178 if (IS_ERR(handle)) {
5179 error = PTR_ERR(handle);
5180 goto err_out;
5181 }
Josef Bacik3da40c72015-06-22 00:31:26 -04005182 if (ext4_handle_valid(handle) && shrink) {
Jan Kara52083862013-08-17 10:07:17 -04005183 error = ext4_orphan_add(handle, inode);
5184 orphan = 1;
5185 }
Eryu Guan911af572015-07-28 15:08:41 -04005186 /*
5187 * Update c/mtime on truncate up, ext4_truncate() will
5188 * update c/mtime in shrink case below
5189 */
5190 if (!shrink) {
5191 inode->i_mtime = ext4_current_time(inode);
5192 inode->i_ctime = inode->i_mtime;
5193 }
Jan Kara90e775b2013-08-17 10:09:31 -04005194 down_write(&EXT4_I(inode)->i_data_sem);
Jan Kara52083862013-08-17 10:07:17 -04005195 EXT4_I(inode)->i_disksize = attr->ia_size;
5196 rc = ext4_mark_inode_dirty(handle, inode);
5197 if (!error)
5198 error = rc;
Jan Kara90e775b2013-08-17 10:09:31 -04005199 /*
5200 * We have to update i_size under i_data_sem together
5201 * with i_disksize to avoid races with writeback code
5202 * running ext4_wb_update_i_disksize().
5203 */
5204 if (!error)
5205 i_size_write(inode, attr->ia_size);
5206 up_write(&EXT4_I(inode)->i_data_sem);
Jan Kara52083862013-08-17 10:07:17 -04005207 ext4_journal_stop(handle);
5208 if (error) {
Josef Bacik3da40c72015-06-22 00:31:26 -04005209 if (orphan)
5210 ext4_orphan_del(NULL, inode);
Jan Kara678aaf42008-07-11 19:27:31 -04005211 goto err_out;
5212 }
Jan Karad6320cb2014-10-01 21:49:46 -04005213 }
Josef Bacik3da40c72015-06-22 00:31:26 -04005214 if (!shrink)
5215 pagecache_isize_extended(inode, oldsize, inode->i_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005216
Jan Kara52083862013-08-17 10:07:17 -04005217 /*
5218 * Blocks are going to be removed from the inode. Wait
5219 * for dio in flight. Temporarily disable
5220 * dioread_nolock to prevent livelock.
5221 */
5222 if (orphan) {
5223 if (!ext4_should_journal_data(inode)) {
5224 ext4_inode_block_unlocked_dio(inode);
5225 inode_dio_wait(inode);
5226 ext4_inode_resume_unlocked_dio(inode);
5227 } else
5228 ext4_wait_for_tail_page_commit(inode);
Dmitry Monakhov1c9114f2012-09-29 00:55:23 -04005229 }
Jan Karaea3d7202015-12-07 14:28:03 -05005230 down_write(&EXT4_I(inode)->i_mmap_sem);
Jan Kara52083862013-08-17 10:07:17 -04005231 /*
5232 * Truncate pagecache after we've waited for commit
5233 * in data=journal mode to make pages freeable.
5234 */
Ross Zwisler923ae0f2015-02-16 15:59:38 -08005235 truncate_pagecache(inode, inode->i_size);
Josef Bacik3da40c72015-06-22 00:31:26 -04005236 if (shrink)
5237 ext4_truncate(inode);
Jan Karaea3d7202015-12-07 14:28:03 -05005238 up_write(&EXT4_I(inode)->i_mmap_sem);
Theodore Ts'o072bd7e2011-05-23 15:13:02 -04005239 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005240
Christoph Hellwig10257742010-06-04 11:30:02 +02005241 if (!rc) {
5242 setattr_copy(inode, attr);
5243 mark_inode_dirty(inode);
5244 }
5245
5246 /*
5247 * If the call to ext4_truncate failed to get a transaction handle at
5248 * all, we need to clean up the in-core orphan list manually.
5249 */
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04005250 if (orphan && inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07005251 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005252
5253 if (!rc && (ia_valid & ATTR_MODE))
Christoph Hellwig64e178a2013-12-20 05:16:44 -08005254 rc = posix_acl_chmod(inode, inode->i_mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005255
5256err_out:
Mingming Cao617ba132006-10-11 01:20:53 -07005257 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005258 if (!error)
5259 error = rc;
5260 return error;
5261}
5262
Mingming Cao3e3398a2008-07-11 19:27:31 -04005263int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
5264 struct kstat *stat)
5265{
5266 struct inode *inode;
Jan Kara8af8eec2013-05-31 19:39:56 -04005267 unsigned long long delalloc_blocks;
Mingming Cao3e3398a2008-07-11 19:27:31 -04005268
David Howells2b0143b2015-03-17 22:25:59 +00005269 inode = d_inode(dentry);
Mingming Cao3e3398a2008-07-11 19:27:31 -04005270 generic_fillattr(inode, stat);
5271
5272 /*
Andreas Dilger9206c562013-11-11 22:38:12 -05005273 * If there is inline data in the inode, the inode will normally not
5274 * have data blocks allocated (it may have an external xattr block).
5275 * Report at least one sector for such files, so tools like tar, rsync,
5276 * others doen't incorrectly think the file is completely sparse.
5277 */
5278 if (unlikely(ext4_has_inline_data(inode)))
5279 stat->blocks += (stat->size + 511) >> 9;
5280
5281 /*
Mingming Cao3e3398a2008-07-11 19:27:31 -04005282 * We can't update i_blocks if the block allocation is delayed
5283 * otherwise in the case of system crash before the real block
5284 * allocation is done, we will have i_blocks inconsistent with
5285 * on-disk file blocks.
5286 * We always keep i_blocks updated together with real
5287 * allocation. But to not confuse with user, stat
5288 * will return the blocks that include the delayed allocation
5289 * blocks for this file.
5290 */
Tao Ma96607552012-05-31 22:54:16 -04005291 delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
Andreas Dilger9206c562013-11-11 22:38:12 -05005292 EXT4_I(inode)->i_reserved_data_blocks);
5293 stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9);
Mingming Cao3e3398a2008-07-11 19:27:31 -04005294 return 0;
5295}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005296
Jan Karafffb2732013-06-04 13:01:11 -04005297static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
5298 int pextents)
Mingming Caoa02908f2008-08-19 22:16:07 -04005299{
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005300 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Jan Karafffb2732013-06-04 13:01:11 -04005301 return ext4_ind_trans_blocks(inode, lblocks);
5302 return ext4_ext_index_trans_blocks(inode, pextents);
Mingming Caoa02908f2008-08-19 22:16:07 -04005303}
Theodore Ts'oac51d832008-11-06 16:49:36 -05005304
Mingming Caoa02908f2008-08-19 22:16:07 -04005305/*
5306 * Account for index blocks, block groups bitmaps and block group
5307 * descriptor blocks if modify datablocks and index blocks
5308 * worse case, the indexs blocks spread over different block groups
5309 *
5310 * If datablocks are discontiguous, they are possible to spread over
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005311 * different block groups too. If they are contiguous, with flexbg,
Mingming Caoa02908f2008-08-19 22:16:07 -04005312 * they could still across block group boundary.
5313 *
5314 * Also account for superblock, inode, quota and xattr blocks
5315 */
Jan Karafffb2732013-06-04 13:01:11 -04005316static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
5317 int pextents)
Mingming Caoa02908f2008-08-19 22:16:07 -04005318{
Theodore Ts'o8df96752009-05-01 08:50:38 -04005319 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
5320 int gdpblocks;
Mingming Caoa02908f2008-08-19 22:16:07 -04005321 int idxblocks;
5322 int ret = 0;
5323
5324 /*
Jan Karafffb2732013-06-04 13:01:11 -04005325 * How many index blocks need to touch to map @lblocks logical blocks
5326 * to @pextents physical extents?
Mingming Caoa02908f2008-08-19 22:16:07 -04005327 */
Jan Karafffb2732013-06-04 13:01:11 -04005328 idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents);
Mingming Caoa02908f2008-08-19 22:16:07 -04005329
5330 ret = idxblocks;
5331
5332 /*
5333 * Now let's see how many group bitmaps and group descriptors need
5334 * to account
5335 */
Jan Karafffb2732013-06-04 13:01:11 -04005336 groups = idxblocks + pextents;
Mingming Caoa02908f2008-08-19 22:16:07 -04005337 gdpblocks = groups;
Theodore Ts'o8df96752009-05-01 08:50:38 -04005338 if (groups > ngroups)
5339 groups = ngroups;
Mingming Caoa02908f2008-08-19 22:16:07 -04005340 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
5341 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
5342
5343 /* bitmaps and block group descriptor blocks */
5344 ret += groups + gdpblocks;
5345
5346 /* Blocks for super block, inode, quota and xattr blocks */
5347 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005348
5349 return ret;
5350}
5351
5352/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005353 * Calculate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04005354 * the modification of a single pages into a single transaction,
5355 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04005356 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04005357 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04005358 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04005359 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04005360 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04005361 */
5362int ext4_writepage_trans_blocks(struct inode *inode)
5363{
5364 int bpp = ext4_journal_blocks_per_page(inode);
5365 int ret;
5366
Jan Karafffb2732013-06-04 13:01:11 -04005367 ret = ext4_meta_trans_blocks(inode, bpp, bpp);
Mingming Caoa02908f2008-08-19 22:16:07 -04005368
5369 /* Account for data blocks for journalled mode */
5370 if (ext4_should_journal_data(inode))
5371 ret += bpp;
5372 return ret;
5373}
Mingming Caof3bd1f32008-08-19 22:16:03 -04005374
5375/*
5376 * Calculate the journal credits for a chunk of data modification.
5377 *
5378 * This is called from DIO, fallocate or whoever calling
Eric Sandeen79e83032010-07-27 11:56:07 -04005379 * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
Mingming Caof3bd1f32008-08-19 22:16:03 -04005380 *
5381 * journal buffers for data blocks are not included here, as DIO
5382 * and fallocate do no need to journal data buffers.
5383 */
5384int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
5385{
5386 return ext4_meta_trans_blocks(inode, nrblocks, 1);
5387}
5388
Mingming Caoa02908f2008-08-19 22:16:07 -04005389/*
Mingming Cao617ba132006-10-11 01:20:53 -07005390 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005391 * Give this, we know that the caller already has write access to iloc->bh.
5392 */
Mingming Cao617ba132006-10-11 01:20:53 -07005393int ext4_mark_iloc_dirty(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04005394 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005395{
5396 int err = 0;
5397
Theodore Ts'oc64db502012-03-02 12:23:11 -05005398 if (IS_I_VERSION(inode))
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005399 inode_inc_iversion(inode);
5400
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005401 /* the do_update_inode consumes one bh->b_count */
5402 get_bh(iloc->bh);
5403
Mingming Caodab291a2006-10-11 01:21:01 -07005404 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Frank Mayhar830156c2009-09-29 10:07:47 -04005405 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005406 put_bh(iloc->bh);
5407 return err;
5408}
5409
5410/*
5411 * On success, We end up with an outstanding reference count against
5412 * iloc->bh. This _must_ be cleaned up later.
5413 */
5414
5415int
Mingming Cao617ba132006-10-11 01:20:53 -07005416ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
5417 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005418{
Frank Mayhar03901312009-01-07 00:06:22 -05005419 int err;
5420
5421 err = ext4_get_inode_loc(inode, iloc);
5422 if (!err) {
5423 BUFFER_TRACE(iloc->bh, "get_write_access");
5424 err = ext4_journal_get_write_access(handle, iloc->bh);
5425 if (err) {
5426 brelse(iloc->bh);
5427 iloc->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005428 }
5429 }
Mingming Cao617ba132006-10-11 01:20:53 -07005430 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005431 return err;
5432}
5433
5434/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005435 * Expand an inode by new_extra_isize bytes.
5436 * Returns 0 on success or negative error number on failure.
5437 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05005438static int ext4_expand_extra_isize(struct inode *inode,
5439 unsigned int new_extra_isize,
5440 struct ext4_iloc iloc,
5441 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005442{
5443 struct ext4_inode *raw_inode;
5444 struct ext4_xattr_ibody_header *header;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005445
5446 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
5447 return 0;
5448
5449 raw_inode = ext4_raw_inode(&iloc);
5450
5451 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005452
5453 /* No extended attributes present */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005454 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
5455 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005456 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
5457 new_extra_isize);
5458 EXT4_I(inode)->i_extra_isize = new_extra_isize;
5459 return 0;
5460 }
5461
5462 /* try to expand with EAs present */
5463 return ext4_expand_extra_isize_ea(inode, new_extra_isize,
5464 raw_inode, handle);
5465}
5466
5467/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005468 * What we do here is to mark the in-core inode as clean with respect to inode
5469 * dirtiness (it may still be data-dirty).
5470 * This means that the in-core inode may be reaped by prune_icache
5471 * without having to perform any I/O. This is a very good thing,
5472 * because *any* task may call prune_icache - even ones which
5473 * have a transaction open against a different journal.
5474 *
5475 * Is this cheating? Not really. Sure, we haven't written the
5476 * inode out, but prune_icache isn't a user-visible syncing function.
5477 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5478 * we start and wait on commits.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005479 */
Mingming Cao617ba132006-10-11 01:20:53 -07005480int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005481{
Mingming Cao617ba132006-10-11 01:20:53 -07005482 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005483 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5484 static unsigned int mnt_count;
5485 int err, ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005486
5487 might_sleep();
Theodore Ts'o7ff9c072010-11-08 13:51:33 -05005488 trace_ext4_mark_inode_dirty(inode, _RET_IP_);
Mingming Cao617ba132006-10-11 01:20:53 -07005489 err = ext4_reserve_inode_write(handle, inode, &iloc);
Eryu Guan5e1021f2016-03-12 21:40:32 -05005490 if (err)
5491 return err;
Frank Mayhar03901312009-01-07 00:06:22 -05005492 if (ext4_handle_valid(handle) &&
5493 EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005494 !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005495 /*
5496 * We need extra buffer credits since we may write into EA block
5497 * with this same handle. If journal_extend fails, then it will
5498 * only result in a minor loss of functionality for that inode.
5499 * If this is felt to be critical, then e2fsck should be run to
5500 * force a large enough s_min_extra_isize.
5501 */
5502 if ((jbd2_journal_extend(handle,
5503 EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
5504 ret = ext4_expand_extra_isize(inode,
5505 sbi->s_want_extra_isize,
5506 iloc, handle);
5507 if (ret) {
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005508 if (mnt_count !=
5509 le16_to_cpu(sbi->s_es->s_mnt_count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05005510 ext4_warning(inode->i_sb,
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005511 "Unable to expand inode %lu. Delete"
5512 " some EAs or run e2fsck.",
5513 inode->i_ino);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005514 mnt_count =
5515 le16_to_cpu(sbi->s_es->s_mnt_count);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005516 }
5517 }
5518 }
5519 }
Eryu Guan5e1021f2016-03-12 21:40:32 -05005520 return ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005521}
5522
5523/*
Mingming Cao617ba132006-10-11 01:20:53 -07005524 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005525 *
5526 * We're really interested in the case where a file is being extended.
5527 * i_size has been changed by generic_commit_write() and we thus need
5528 * to include the updated inode in the current transaction.
5529 *
Christoph Hellwig5dd40562010-03-03 09:05:00 -05005530 * Also, dquot_alloc_block() will always dirty the inode when blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005531 * are allocated to the file.
5532 *
5533 * If the inode is marked synchronous, we don't honour that here - doing
5534 * so would cause a commit on atime updates, which we don't bother doing.
5535 * We handle synchronous inodes at the highest possible level.
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05005536 *
5537 * If only the I_DIRTY_TIME flag is set, we can skip everything. If
5538 * I_DIRTY_TIME and I_DIRTY_SYNC is set, the only inode fields we need
5539 * to copy into the on-disk inode structure are the timestamp files.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005540 */
Christoph Hellwigaa385722011-05-27 06:53:02 -04005541void ext4_dirty_inode(struct inode *inode, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005542{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005543 handle_t *handle;
5544
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05005545 if (flags == I_DIRTY_TIME)
5546 return;
Theodore Ts'o9924a922013-02-08 21:59:22 -05005547 handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005548 if (IS_ERR(handle))
5549 goto out;
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005550
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005551 ext4_mark_inode_dirty(handle, inode);
5552
Mingming Cao617ba132006-10-11 01:20:53 -07005553 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005554out:
5555 return;
5556}
5557
5558#if 0
5559/*
5560 * Bind an inode's backing buffer_head into this transaction, to prevent
5561 * it from being flushed to disk early. Unlike
Mingming Cao617ba132006-10-11 01:20:53 -07005562 * ext4_reserve_inode_write, this leaves behind no bh reference and
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005563 * returns no iloc structure, so the caller needs to repeat the iloc
5564 * lookup to mark the inode dirty later.
5565 */
Mingming Cao617ba132006-10-11 01:20:53 -07005566static int ext4_pin_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005567{
Mingming Cao617ba132006-10-11 01:20:53 -07005568 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005569
5570 int err = 0;
5571 if (handle) {
Mingming Cao617ba132006-10-11 01:20:53 -07005572 err = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005573 if (!err) {
5574 BUFFER_TRACE(iloc.bh, "get_write_access");
Mingming Caodab291a2006-10-11 01:21:01 -07005575 err = jbd2_journal_get_write_access(handle, iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005576 if (!err)
Frank Mayhar03901312009-01-07 00:06:22 -05005577 err = ext4_handle_dirty_metadata(handle,
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05005578 NULL,
Frank Mayhar03901312009-01-07 00:06:22 -05005579 iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005580 brelse(iloc.bh);
5581 }
5582 }
Mingming Cao617ba132006-10-11 01:20:53 -07005583 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005584 return err;
5585}
5586#endif
5587
Mingming Cao617ba132006-10-11 01:20:53 -07005588int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005589{
5590 journal_t *journal;
5591 handle_t *handle;
5592 int err;
Daeho Jeongc8585c62016-04-25 23:22:35 -04005593 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005594
5595 /*
5596 * We have to be very careful here: changing a data block's
5597 * journaling status dynamically is dangerous. If we write a
5598 * data block to the journal, change the status and then delete
5599 * that block, we risk forgetting to revoke the old log record
5600 * from the journal and so a subsequent replay can corrupt data.
5601 * So, first we make sure that the journal is empty and that
5602 * nobody is changing anything.
5603 */
5604
Mingming Cao617ba132006-10-11 01:20:53 -07005605 journal = EXT4_JOURNAL(inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005606 if (!journal)
5607 return 0;
Dave Hansend6995942007-07-18 08:33:51 -04005608 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005609 return -EROFS;
5610
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04005611 /* Wait for all existing dio workers */
5612 ext4_inode_block_unlocked_dio(inode);
5613 inode_dio_wait(inode);
5614
Daeho Jeong4c546592016-04-25 23:21:00 -04005615 /*
5616 * Before flushing the journal and switching inode's aops, we have
5617 * to flush all dirty data the inode has. There can be outstanding
5618 * delayed allocations, there can be unwritten extents created by
5619 * fallocate or buffered writes in dioread_nolock mode covered by
5620 * dirty data which can be converted only after flushing the dirty
5621 * data (and journalled aops don't know how to handle these cases).
5622 */
5623 if (val) {
5624 down_write(&EXT4_I(inode)->i_mmap_sem);
5625 err = filemap_write_and_wait(inode->i_mapping);
5626 if (err < 0) {
5627 up_write(&EXT4_I(inode)->i_mmap_sem);
5628 ext4_inode_resume_unlocked_dio(inode);
5629 return err;
5630 }
5631 }
5632
Daeho Jeongc8585c62016-04-25 23:22:35 -04005633 percpu_down_write(&sbi->s_journal_flag_rwsem);
Mingming Caodab291a2006-10-11 01:21:01 -07005634 jbd2_journal_lock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005635
5636 /*
5637 * OK, there are no updates running now, and all cached data is
5638 * synced to disk. We are now in a completely consistent state
5639 * which doesn't have anything in the journal, and we know that
5640 * no filesystem updates are running, so it is safe to modify
5641 * the inode's in-core data-journaling state flag now.
5642 */
5643
5644 if (val)
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005645 ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Yongqiang Yang5872dda2011-12-28 13:55:51 -05005646 else {
Jan Kara4f879ca2014-10-30 10:53:17 -04005647 err = jbd2_journal_flush(journal);
5648 if (err < 0) {
5649 jbd2_journal_unlock_updates(journal);
Daeho Jeongc8585c62016-04-25 23:22:35 -04005650 percpu_up_write(&sbi->s_journal_flag_rwsem);
Jan Kara4f879ca2014-10-30 10:53:17 -04005651 ext4_inode_resume_unlocked_dio(inode);
5652 return err;
5653 }
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005654 ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Yongqiang Yang5872dda2011-12-28 13:55:51 -05005655 }
Mingming Cao617ba132006-10-11 01:20:53 -07005656 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005657
Mingming Caodab291a2006-10-11 01:21:01 -07005658 jbd2_journal_unlock_updates(journal);
Daeho Jeongc8585c62016-04-25 23:22:35 -04005659 percpu_up_write(&sbi->s_journal_flag_rwsem);
5660
Daeho Jeong4c546592016-04-25 23:21:00 -04005661 if (val)
5662 up_write(&EXT4_I(inode)->i_mmap_sem);
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04005663 ext4_inode_resume_unlocked_dio(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005664
5665 /* Finally we can mark the inode as dirty. */
5666
Theodore Ts'o9924a922013-02-08 21:59:22 -05005667 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005668 if (IS_ERR(handle))
5669 return PTR_ERR(handle);
5670
Mingming Cao617ba132006-10-11 01:20:53 -07005671 err = ext4_mark_inode_dirty(handle, inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005672 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005673 ext4_journal_stop(handle);
5674 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005675
5676 return err;
5677}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005678
5679static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
5680{
5681 return !buffer_mapped(bh);
5682}
5683
Nick Pigginc2ec1752009-03-31 15:23:21 -07005684int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005685{
Nick Pigginc2ec1752009-03-31 15:23:21 -07005686 struct page *page = vmf->page;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005687 loff_t size;
5688 unsigned long len;
Jan Kara9ea7df52011-06-24 14:29:41 -04005689 int ret;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005690 struct file *file = vma->vm_file;
Al Viro496ad9a2013-01-23 17:07:38 -05005691 struct inode *inode = file_inode(file);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005692 struct address_space *mapping = inode->i_mapping;
Jan Kara9ea7df52011-06-24 14:29:41 -04005693 handle_t *handle;
5694 get_block_t *get_block;
5695 int retries = 0;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005696
Jan Kara8e8ad8a2012-06-12 16:20:38 +02005697 sb_start_pagefault(inode->i_sb);
Theodore Ts'o041bbb6d2012-09-30 23:04:56 -04005698 file_update_time(vma->vm_file);
Jan Karaea3d7202015-12-07 14:28:03 -05005699
5700 down_read(&EXT4_I(inode)->i_mmap_sem);
Jan Kara9ea7df52011-06-24 14:29:41 -04005701 /* Delalloc case is easy... */
5702 if (test_opt(inode->i_sb, DELALLOC) &&
5703 !ext4_should_journal_data(inode) &&
5704 !ext4_nonda_switch(inode->i_sb)) {
5705 do {
Ross Zwisler5c500022015-10-13 16:51:02 -06005706 ret = block_page_mkwrite(vma, vmf,
Jan Kara9ea7df52011-06-24 14:29:41 -04005707 ext4_da_get_block_prep);
5708 } while (ret == -ENOSPC &&
5709 ext4_should_retry_alloc(inode->i_sb, &retries));
5710 goto out_ret;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005711 }
Darrick J. Wong0e499892011-05-18 13:55:20 -04005712
5713 lock_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04005714 size = i_size_read(inode);
5715 /* Page got truncated from under us? */
5716 if (page->mapping != mapping || page_offset(page) > size) {
5717 unlock_page(page);
5718 ret = VM_FAULT_NOPAGE;
5719 goto out;
Darrick J. Wong0e499892011-05-18 13:55:20 -04005720 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005721
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005722 if (page->index == size >> PAGE_SHIFT)
5723 len = size & ~PAGE_MASK;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005724 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005725 len = PAGE_SIZE;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005726 /*
Jan Kara9ea7df52011-06-24 14:29:41 -04005727 * Return if we have all the buffers mapped. This avoids the need to do
5728 * journal_start/journal_stop which can block and take a long time
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005729 */
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005730 if (page_has_buffers(page)) {
Tao Maf19d5872012-12-10 14:05:51 -05005731 if (!ext4_walk_page_buffers(NULL, page_buffers(page),
5732 0, len, NULL,
5733 ext4_bh_unmapped)) {
Jan Kara9ea7df52011-06-24 14:29:41 -04005734 /* Wait so that we don't change page under IO */
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08005735 wait_for_stable_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04005736 ret = VM_FAULT_LOCKED;
5737 goto out;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005738 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005739 }
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005740 unlock_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04005741 /* OK, we need to fill the hole... */
5742 if (ext4_should_dioread_nolock(inode))
Jan Kara705965b2016-03-08 23:08:10 -05005743 get_block = ext4_get_block_unwritten;
Jan Kara9ea7df52011-06-24 14:29:41 -04005744 else
5745 get_block = ext4_get_block;
5746retry_alloc:
Theodore Ts'o9924a922013-02-08 21:59:22 -05005747 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
5748 ext4_writepage_trans_blocks(inode));
Jan Kara9ea7df52011-06-24 14:29:41 -04005749 if (IS_ERR(handle)) {
Nick Pigginc2ec1752009-03-31 15:23:21 -07005750 ret = VM_FAULT_SIGBUS;
Jan Kara9ea7df52011-06-24 14:29:41 -04005751 goto out;
5752 }
Ross Zwisler5c500022015-10-13 16:51:02 -06005753 ret = block_page_mkwrite(vma, vmf, get_block);
Jan Kara9ea7df52011-06-24 14:29:41 -04005754 if (!ret && ext4_should_journal_data(inode)) {
Tao Maf19d5872012-12-10 14:05:51 -05005755 if (ext4_walk_page_buffers(handle, page_buffers(page), 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03005756 PAGE_SIZE, NULL, do_journal_get_write_access)) {
Jan Kara9ea7df52011-06-24 14:29:41 -04005757 unlock_page(page);
5758 ret = VM_FAULT_SIGBUS;
Yongqiang Yangfcbb5512011-10-26 05:00:19 -04005759 ext4_journal_stop(handle);
Jan Kara9ea7df52011-06-24 14:29:41 -04005760 goto out;
5761 }
5762 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
5763 }
5764 ext4_journal_stop(handle);
5765 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
5766 goto retry_alloc;
5767out_ret:
5768 ret = block_page_mkwrite_return(ret);
5769out:
Jan Karaea3d7202015-12-07 14:28:03 -05005770 up_read(&EXT4_I(inode)->i_mmap_sem);
Jan Kara8e8ad8a2012-06-12 16:20:38 +02005771 sb_end_pagefault(inode->i_sb);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005772 return ret;
5773}
Jan Karaea3d7202015-12-07 14:28:03 -05005774
5775int ext4_filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
5776{
5777 struct inode *inode = file_inode(vma->vm_file);
5778 int err;
5779
5780 down_read(&EXT4_I(inode)->i_mmap_sem);
5781 err = filemap_fault(vma, vmf);
5782 up_read(&EXT4_I(inode)->i_mmap_sem);
5783
5784 return err;
5785}
Jan Kara2d90c162016-03-09 23:11:13 -05005786
5787/*
5788 * Find the first extent at or after @lblk in an inode that is not a hole.
5789 * Search for @map_len blocks at most. The extent is returned in @result.
5790 *
5791 * The function returns 1 if we found an extent. The function returns 0 in
5792 * case there is no extent at or after @lblk and in that case also sets
5793 * @result->es_len to 0. In case of error, the error code is returned.
5794 */
5795int ext4_get_next_extent(struct inode *inode, ext4_lblk_t lblk,
5796 unsigned int map_len, struct extent_status *result)
5797{
5798 struct ext4_map_blocks map;
5799 struct extent_status es = {};
5800 int ret;
5801
5802 map.m_lblk = lblk;
5803 map.m_len = map_len;
5804
5805 /*
5806 * For non-extent based files this loop may iterate several times since
5807 * we do not determine full hole size.
5808 */
5809 while (map.m_len > 0) {
5810 ret = ext4_map_blocks(NULL, inode, &map, 0);
5811 if (ret < 0)
5812 return ret;
5813 /* There's extent covering m_lblk? Just return it. */
5814 if (ret > 0) {
5815 int status;
5816
5817 ext4_es_store_pblock(result, map.m_pblk);
5818 result->es_lblk = map.m_lblk;
5819 result->es_len = map.m_len;
5820 if (map.m_flags & EXT4_MAP_UNWRITTEN)
5821 status = EXTENT_STATUS_UNWRITTEN;
5822 else
5823 status = EXTENT_STATUS_WRITTEN;
5824 ext4_es_store_status(result, status);
5825 return 1;
5826 }
5827 ext4_es_find_delayed_extent_range(inode, map.m_lblk,
5828 map.m_lblk + map.m_len - 1,
5829 &es);
5830 /* Is delalloc data before next block in extent tree? */
5831 if (es.es_len && es.es_lblk < map.m_lblk + map.m_len) {
5832 ext4_lblk_t offset = 0;
5833
5834 if (es.es_lblk < lblk)
5835 offset = lblk - es.es_lblk;
5836 result->es_lblk = es.es_lblk + offset;
5837 ext4_es_store_pblock(result,
5838 ext4_es_pblock(&es) + offset);
5839 result->es_len = es.es_len - offset;
5840 ext4_es_store_status(result, ext4_es_status(&es));
5841
5842 return 1;
5843 }
5844 /* There's a hole at m_lblk, advance us after it */
5845 map.m_lblk += map.m_len;
5846 map_len -= map.m_len;
5847 map.m_len = map_len;
5848 cond_resched();
5849 }
5850 result->es_len = 0;
5851 return 0;
5852}