blob: aea67d906cd8087b46afaf54ef29b8716addfffc [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>
47
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -040048#define MPAGE_DA_EXTENT_TAIL 0x01
49
Darrick J. Wong814525f2012-04-29 18:31:10 -040050static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw,
51 struct ext4_inode_info *ei)
52{
53 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
54 __u16 csum_lo;
55 __u16 csum_hi = 0;
56 __u32 csum;
57
Dmitry Monakhov171a7f22013-04-09 23:56:48 -040058 csum_lo = le16_to_cpu(raw->i_checksum_lo);
Darrick J. Wong814525f2012-04-29 18:31:10 -040059 raw->i_checksum_lo = 0;
60 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
61 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) {
Dmitry Monakhov171a7f22013-04-09 23:56:48 -040062 csum_hi = le16_to_cpu(raw->i_checksum_hi);
Darrick J. Wong814525f2012-04-29 18:31:10 -040063 raw->i_checksum_hi = 0;
64 }
65
66 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw,
67 EXT4_INODE_SIZE(inode->i_sb));
68
Dmitry Monakhov171a7f22013-04-09 23:56:48 -040069 raw->i_checksum_lo = cpu_to_le16(csum_lo);
Darrick J. Wong814525f2012-04-29 18:31:10 -040070 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
71 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
Dmitry Monakhov171a7f22013-04-09 23:56:48 -040072 raw->i_checksum_hi = cpu_to_le16(csum_hi);
Darrick J. Wong814525f2012-04-29 18:31:10 -040073
74 return csum;
75}
76
77static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw,
78 struct ext4_inode_info *ei)
79{
80 __u32 provided, calculated;
81
82 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
83 cpu_to_le32(EXT4_OS_LINUX) ||
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -040084 !ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong814525f2012-04-29 18:31:10 -040085 return 1;
86
87 provided = le16_to_cpu(raw->i_checksum_lo);
88 calculated = ext4_inode_csum(inode, raw, ei);
89 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
90 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
91 provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16;
92 else
93 calculated &= 0xFFFF;
94
95 return provided == calculated;
96}
97
98static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
99 struct ext4_inode_info *ei)
100{
101 __u32 csum;
102
103 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
104 cpu_to_le32(EXT4_OS_LINUX) ||
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400105 !ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong814525f2012-04-29 18:31:10 -0400106 return;
107
108 csum = ext4_inode_csum(inode, raw, ei);
109 raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF);
110 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
111 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
112 raw->i_checksum_hi = cpu_to_le16(csum >> 16);
113}
114
Jan Kara678aaf42008-07-11 19:27:31 -0400115static inline int ext4_begin_ordered_truncate(struct inode *inode,
116 loff_t new_size)
117{
Theodore Ts'o7ff9c072010-11-08 13:51:33 -0500118 trace_ext4_begin_ordered_truncate(inode, new_size);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -0500119 /*
120 * If jinode is zero, then we never opened the file for
121 * writing, so there's no need to call
122 * jbd2_journal_begin_ordered_truncate() since there's no
123 * outstanding writes we need to flush.
124 */
125 if (!EXT4_I(inode)->jinode)
126 return 0;
127 return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
128 EXT4_I(inode)->jinode,
129 new_size);
Jan Kara678aaf42008-07-11 19:27:31 -0400130}
131
Lukas Czernerd47992f2013-05-21 23:17:23 -0400132static void ext4_invalidatepage(struct page *page, unsigned int offset,
133 unsigned int length);
Theodore Ts'ocb20d512010-10-27 21:30:09 -0400134static int __ext4_journalled_writepage(struct page *page, unsigned int len);
135static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
Jan Karafffb2732013-06-04 13:01:11 -0400136static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
137 int pextents);
Alex Tomas64769242008-07-11 19:27:31 -0400138
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700139/*
140 * Test whether an inode is a fast symlink.
141 */
Theodore Ts'of348c252015-04-16 01:55:00 -0400142int ext4_inode_is_fast_symlink(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700143{
Yongqiang Yang65eddb52014-01-06 14:06:18 -0500144 int ea_blocks = EXT4_I(inode)->i_file_acl ?
145 EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700146
Zheng Liubd9db172014-06-02 10:48:22 -0400147 if (ext4_has_inline_data(inode))
148 return 0;
149
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700150 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
151}
152
153/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700154 * Restart the transaction associated with *handle. This does a commit,
155 * so before we call here everything must be consistently dirtied against
156 * this transaction.
157 */
Aneesh Kumar K.Vfa5d1112009-11-02 18:50:49 -0500158int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode,
Jan Kara487caee2009-08-17 22:17:20 -0400159 int nblocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700160{
Jan Kara487caee2009-08-17 22:17:20 -0400161 int ret;
162
163 /*
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400164 * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
Jan Kara487caee2009-08-17 22:17:20 -0400165 * moment, get_block can be called only for blocks inside i_size since
166 * page cache has been already dropped and writes are blocked by
167 * i_mutex. So we can safely drop the i_data_sem here.
168 */
Frank Mayhar03901312009-01-07 00:06:22 -0500169 BUG_ON(EXT4_JOURNAL(inode) == NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700170 jbd_debug(2, "restarting handle %p\n", handle);
Jan Kara487caee2009-08-17 22:17:20 -0400171 up_write(&EXT4_I(inode)->i_data_sem);
Amir Goldstein8e8eaab2011-02-27 23:32:12 -0500172 ret = ext4_journal_restart(handle, nblocks);
Jan Kara487caee2009-08-17 22:17:20 -0400173 down_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vfa5d1112009-11-02 18:50:49 -0500174 ext4_discard_preallocations(inode);
Jan Kara487caee2009-08-17 22:17:20 -0400175
176 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700177}
178
179/*
180 * Called at the last iput() if i_nlink is zero.
181 */
Al Viro0930fcc2010-06-07 13:16:22 -0400182void ext4_evict_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700183{
184 handle_t *handle;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400185 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700186
Theodore Ts'o7ff9c072010-11-08 13:51:33 -0500187 trace_ext4_evict_inode(inode);
Jiaying Zhang2581fdc2011-08-13 12:17:13 -0400188
Al Viro0930fcc2010-06-07 13:16:22 -0400189 if (inode->i_nlink) {
Jan Kara2d859db2011-07-26 09:07:11 -0400190 /*
191 * When journalling data dirty buffers are tracked only in the
192 * journal. So although mm thinks everything is clean and
193 * ready for reaping the inode might still have some pages to
194 * write in the running transaction or waiting to be
195 * checkpointed. Thus calling jbd2_journal_invalidatepage()
196 * (via truncate_inode_pages()) to discard these buffers can
197 * cause data loss. Also even if we did not discard these
198 * buffers, we would have no way to find them after the inode
199 * is reaped and thus user could see stale data if he tries to
200 * read them before the transaction is checkpointed. So be
201 * careful and force everything to disk here... We use
202 * ei->i_datasync_tid to store the newest transaction
203 * containing inode's data.
204 *
205 * Note that directories do not have this problem because they
206 * don't use page cache.
207 */
208 if (ext4_should_journal_data(inode) &&
Theodore Ts'o2b405bf2013-03-20 09:42:11 -0400209 (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode)) &&
210 inode->i_ino != EXT4_JOURNAL_INO) {
Jan Kara2d859db2011-07-26 09:07:11 -0400211 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
212 tid_t commit_tid = EXT4_I(inode)->i_datasync_tid;
213
Theodore Ts'od76a3a772013-04-03 22:02:52 -0400214 jbd2_complete_transaction(journal, commit_tid);
Jan Kara2d859db2011-07-26 09:07:11 -0400215 filemap_write_and_wait(&inode->i_data);
216 }
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700217 truncate_inode_pages_final(&inode->i_data);
Jan Kara5dc23bd2013-06-04 14:46:12 -0400218
219 WARN_ON(atomic_read(&EXT4_I(inode)->i_ioend_count));
Al Viro0930fcc2010-06-07 13:16:22 -0400220 goto no_delete;
221 }
222
Theodore Ts'oe2bfb082014-10-05 22:47:07 -0400223 if (is_bad_inode(inode))
224 goto no_delete;
225 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500226
Jan Kara678aaf42008-07-11 19:27:31 -0400227 if (ext4_should_order_data(inode))
228 ext4_begin_ordered_truncate(inode, 0);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700229 truncate_inode_pages_final(&inode->i_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700230
Jan Kara5dc23bd2013-06-04 14:46:12 -0400231 WARN_ON(atomic_read(&EXT4_I(inode)->i_ioend_count));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700232
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200233 /*
234 * Protect us against freezing - iput() caller didn't have to have any
235 * protection against it
236 */
237 sb_start_intwrite(inode->i_sb);
Theodore Ts'o9924a922013-02-08 21:59:22 -0500238 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
239 ext4_blocks_for_truncate(inode)+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700240 if (IS_ERR(handle)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400241 ext4_std_error(inode->i_sb, PTR_ERR(handle));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700242 /*
243 * If we're going to skip the normal cleanup, we still need to
244 * make sure that the in-core orphan linked list is properly
245 * cleaned up.
246 */
Mingming Cao617ba132006-10-11 01:20:53 -0700247 ext4_orphan_del(NULL, inode);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200248 sb_end_intwrite(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700249 goto no_delete;
250 }
251
252 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500253 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700254 inode->i_size = 0;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400255 err = ext4_mark_inode_dirty(handle, inode);
256 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500257 ext4_warning(inode->i_sb,
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400258 "couldn't mark inode dirty (err %d)", err);
259 goto stop_handle;
260 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700261 if (inode->i_blocks)
Mingming Cao617ba132006-10-11 01:20:53 -0700262 ext4_truncate(inode);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400263
264 /*
265 * ext4_ext_truncate() doesn't reserve any slop when it
266 * restarts journal transactions; therefore there may not be
267 * enough credits left in the handle to remove the inode from
268 * the orphan list and set the dtime field.
269 */
Frank Mayhar03901312009-01-07 00:06:22 -0500270 if (!ext4_handle_has_enough_credits(handle, 3)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400271 err = ext4_journal_extend(handle, 3);
272 if (err > 0)
273 err = ext4_journal_restart(handle, 3);
274 if (err != 0) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500275 ext4_warning(inode->i_sb,
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400276 "couldn't extend journal (err %d)", err);
277 stop_handle:
278 ext4_journal_stop(handle);
Theodore Ts'o45388212010-07-29 15:06:10 -0400279 ext4_orphan_del(NULL, inode);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200280 sb_end_intwrite(inode->i_sb);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400281 goto no_delete;
282 }
283 }
284
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700285 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700286 * Kill off the orphan record which ext4_truncate created.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700287 * AKPM: I think this can be inside the above `if'.
Mingming Cao617ba132006-10-11 01:20:53 -0700288 * Note that ext4_orphan_del() has to be able to cope with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700289 * deletion of a non-existent orphan - this is because we don't
Mingming Cao617ba132006-10-11 01:20:53 -0700290 * know if ext4_truncate() actually created an orphan record.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700291 * (Well, we could do this if we need to, but heck - it works)
292 */
Mingming Cao617ba132006-10-11 01:20:53 -0700293 ext4_orphan_del(handle, inode);
294 EXT4_I(inode)->i_dtime = get_seconds();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700295
296 /*
297 * One subtle ordering requirement: if anything has gone wrong
298 * (transaction abort, IO errors, whatever), then we can still
299 * do these next steps (the fs will already have been marked as
300 * having errors), but we can't free the inode if the mark_dirty
301 * fails.
302 */
Mingming Cao617ba132006-10-11 01:20:53 -0700303 if (ext4_mark_inode_dirty(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700304 /* If that failed, just do the required in-core inode clear. */
Al Viro0930fcc2010-06-07 13:16:22 -0400305 ext4_clear_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700306 else
Mingming Cao617ba132006-10-11 01:20:53 -0700307 ext4_free_inode(handle, inode);
308 ext4_journal_stop(handle);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200309 sb_end_intwrite(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700310 return;
311no_delete:
Al Viro0930fcc2010-06-07 13:16:22 -0400312 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700313}
314
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300315#ifdef CONFIG_QUOTA
316qsize_t *ext4_get_reserved_space(struct inode *inode)
Mingming Cao60e58e02009-01-22 18:13:05 +0100317{
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300318 return &EXT4_I(inode)->i_reserved_quota;
Mingming Cao60e58e02009-01-22 18:13:05 +0100319}
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300320#endif
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500321
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400322/*
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500323 * Called with i_data_sem down, which is important since we can call
324 * ext4_discard_preallocations() from here.
325 */
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500326void ext4_da_update_reserve_space(struct inode *inode,
327 int used, int quota_claim)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400328{
329 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500330 struct ext4_inode_info *ei = EXT4_I(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400331
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500332 spin_lock(&ei->i_block_reservation_lock);
Aditya Kalid8990242011-09-09 19:18:51 -0400333 trace_ext4_da_update_reserve_space(inode, used, quota_claim);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500334 if (unlikely(used > ei->i_reserved_data_blocks)) {
Theodore Ts'o8de5c322013-02-14 15:11:41 -0500335 ext4_warning(inode->i_sb, "%s: ino %lu, used %d "
Theodore Ts'o1084f252012-03-19 23:13:43 -0400336 "with only %d reserved data blocks",
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500337 __func__, inode->i_ino, used,
338 ei->i_reserved_data_blocks);
339 WARN_ON(1);
340 used = ei->i_reserved_data_blocks;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400341 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400342
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500343 /* Update per-inode reservations */
344 ei->i_reserved_data_blocks -= used;
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -0400345 percpu_counter_sub(&sbi->s_dirtyclusters_counter, used);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500346
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400347 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +0100348
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400349 /* Update quota subsystem for data blocks */
350 if (quota_claim)
Aditya Kali7b415bf2011-09-09 19:04:51 -0400351 dquot_claim_block(inode, EXT4_C2B(sbi, used));
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400352 else {
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500353 /*
354 * We did fallocate with an offset that is already delayed
355 * allocated. So on delayed allocated writeback we should
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400356 * not re-claim the quota for fallocated blocks.
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500357 */
Aditya Kali7b415bf2011-09-09 19:04:51 -0400358 dquot_release_reservation_block(inode, EXT4_C2B(sbi, used));
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500359 }
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -0400360
361 /*
362 * If we have done all the pending block allocations and if
363 * there aren't any writers on the inode, we can discard the
364 * inode's preallocations.
365 */
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500366 if ((ei->i_reserved_data_blocks == 0) &&
367 (atomic_read(&inode->i_writecount) == 0))
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -0400368 ext4_discard_preallocations(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400369}
370
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400371static int __check_block_validity(struct inode *inode, const char *func,
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400372 unsigned int line,
373 struct ext4_map_blocks *map)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400374{
Theodore Ts'o24676da2010-05-16 21:00:00 -0400375 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
376 map->m_len)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400377 ext4_error_inode(inode, func, line, map->m_pblk,
378 "lblock %lu mapped to illegal pblock "
379 "(length %d)", (unsigned long) map->m_lblk,
380 map->m_len);
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400381 return -EFSCORRUPTED;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400382 }
383 return 0;
384}
385
Jan Kara53085fa2015-12-07 15:09:35 -0500386int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk, ext4_fsblk_t pblk,
387 ext4_lblk_t len)
388{
389 int ret;
390
391 if (ext4_encrypted_inode(inode))
392 return ext4_encrypted_zeroout(inode, lblk, pblk, len);
393
394 ret = sb_issue_zeroout(inode->i_sb, pblk, len, GFP_NOFS);
395 if (ret > 0)
396 ret = 0;
397
398 return ret;
399}
400
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400401#define check_block_validity(inode, map) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400402 __check_block_validity((inode), __func__, __LINE__, (map))
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400403
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400404#ifdef ES_AGGRESSIVE_TEST
405static void ext4_map_blocks_es_recheck(handle_t *handle,
406 struct inode *inode,
407 struct ext4_map_blocks *es_map,
408 struct ext4_map_blocks *map,
409 int flags)
410{
411 int retval;
412
413 map->m_flags = 0;
414 /*
415 * There is a race window that the result is not the same.
416 * e.g. xfstests #223 when dioread_nolock enables. The reason
417 * is that we lookup a block mapping in extent status tree with
418 * out taking i_data_sem. So at the time the unwritten extent
419 * could be converted.
420 */
Jan Kara2dcba472015-12-07 15:04:57 -0500421 down_read(&EXT4_I(inode)->i_data_sem);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400422 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
423 retval = ext4_ext_map_blocks(handle, inode, map, flags &
424 EXT4_GET_BLOCKS_KEEP_SIZE);
425 } else {
426 retval = ext4_ind_map_blocks(handle, inode, map, flags &
427 EXT4_GET_BLOCKS_KEEP_SIZE);
428 }
Jan Kara2dcba472015-12-07 15:04:57 -0500429 up_read((&EXT4_I(inode)->i_data_sem));
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400430
431 /*
432 * We don't check m_len because extent will be collpased in status
433 * tree. So the m_len might not equal.
434 */
435 if (es_map->m_lblk != map->m_lblk ||
436 es_map->m_flags != map->m_flags ||
437 es_map->m_pblk != map->m_pblk) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400438 printk("ES cache assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400439 "es_cached ex [%d/%d/%llu/%x] != "
440 "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
441 inode->i_ino, es_map->m_lblk, es_map->m_len,
442 es_map->m_pblk, es_map->m_flags, map->m_lblk,
443 map->m_len, map->m_pblk, map->m_flags,
444 retval, flags);
445 }
446}
447#endif /* ES_AGGRESSIVE_TEST */
448
Theodore Ts'o55138e02009-09-29 13:31:31 -0400449/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400450 * The ext4_map_blocks() function tries to look up the requested blocks,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400451 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500452 *
Mingming Caof5ab0d12008-02-25 15:29:55 -0500453 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
454 * and store the allocated blocks in the result buffer head and mark it
455 * mapped.
456 *
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400457 * If file type is extents based, it will call ext4_ext_map_blocks(),
458 * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
Mingming Caof5ab0d12008-02-25 15:29:55 -0500459 * based files
460 *
Lukas Czerner556615d2014-04-20 23:45:47 -0400461 * On success, it returns the number of blocks being mapped or allocated.
462 * if create==0 and the blocks are pre-allocated and unwritten block,
Mingming Caof5ab0d12008-02-25 15:29:55 -0500463 * the result buffer head is unmapped. If the create ==1, it will make sure
464 * the buffer head is mapped.
465 *
466 * It returns 0 if plain look up failed (blocks have not been allocated), in
Tao Madf3ab172011-10-08 15:53:49 -0400467 * that case, buffer head is unmapped
Mingming Caof5ab0d12008-02-25 15:29:55 -0500468 *
469 * It returns the error in case of allocation failure.
470 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400471int ext4_map_blocks(handle_t *handle, struct inode *inode,
472 struct ext4_map_blocks *map, int flags)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500473{
Zheng Liud100eef2013-02-18 00:29:59 -0500474 struct extent_status es;
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500475 int retval;
Lukas Czernerb8a86842014-03-18 18:05:35 -0400476 int ret = 0;
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400477#ifdef ES_AGGRESSIVE_TEST
478 struct ext4_map_blocks orig_map;
479
480 memcpy(&orig_map, map, sizeof(*map));
481#endif
Mingming Caof5ab0d12008-02-25 15:29:55 -0500482
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400483 map->m_flags = 0;
484 ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
485 "logical block %lu\n", inode->i_ino, flags, map->m_len,
486 (unsigned long) map->m_lblk);
Zheng Liud100eef2013-02-18 00:29:59 -0500487
Theodore Ts'oe861b5e2014-02-20 12:54:05 -0500488 /*
489 * ext4_map_blocks returns an int, and m_len is an unsigned int
490 */
491 if (unlikely(map->m_len > INT_MAX))
492 map->m_len = INT_MAX;
493
Kazuya Mio4adb6ab2014-04-07 10:53:28 -0400494 /* We can handle the block number less than EXT_MAX_BLOCKS */
495 if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS))
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400496 return -EFSCORRUPTED;
Kazuya Mio4adb6ab2014-04-07 10:53:28 -0400497
Zheng Liud100eef2013-02-18 00:29:59 -0500498 /* Lookup extent status tree firstly */
499 if (ext4_es_lookup_extent(inode, map->m_lblk, &es)) {
500 if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
501 map->m_pblk = ext4_es_pblock(&es) +
502 map->m_lblk - es.es_lblk;
503 map->m_flags |= ext4_es_is_written(&es) ?
504 EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN;
505 retval = es.es_len - (map->m_lblk - es.es_lblk);
506 if (retval > map->m_len)
507 retval = map->m_len;
508 map->m_len = retval;
509 } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) {
510 retval = 0;
511 } else {
512 BUG_ON(1);
513 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400514#ifdef ES_AGGRESSIVE_TEST
515 ext4_map_blocks_es_recheck(handle, inode, map,
516 &orig_map, flags);
517#endif
Zheng Liud100eef2013-02-18 00:29:59 -0500518 goto found;
519 }
520
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500521 /*
Theodore Ts'ob920c752009-05-14 00:54:29 -0400522 * Try to see if we can get the block without requesting a new
523 * file system block.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500524 */
Jan Kara2dcba472015-12-07 15:04:57 -0500525 down_read(&EXT4_I(inode)->i_data_sem);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400526 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Dmitry Monakhova4e5d882011-10-25 08:15:12 -0400527 retval = ext4_ext_map_blocks(handle, inode, map, flags &
528 EXT4_GET_BLOCKS_KEEP_SIZE);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500529 } else {
Dmitry Monakhova4e5d882011-10-25 08:15:12 -0400530 retval = ext4_ind_map_blocks(handle, inode, map, flags &
531 EXT4_GET_BLOCKS_KEEP_SIZE);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500532 }
Zheng Liuf7fec032013-02-18 00:28:47 -0500533 if (retval > 0) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400534 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -0500535
Zheng Liu44fb851d2013-07-29 12:51:42 -0400536 if (unlikely(retval != map->m_len)) {
537 ext4_warning(inode->i_sb,
538 "ES len assertion failed for inode "
539 "%lu: retval %d != map->m_len %d",
540 inode->i_ino, retval, map->m_len);
541 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400542 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400543
Zheng Liuf7fec032013-02-18 00:28:47 -0500544 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
545 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
546 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400547 !(status & EXTENT_STATUS_WRITTEN) &&
Zheng Liuf7fec032013-02-18 00:28:47 -0500548 ext4_find_delalloc_range(inode, map->m_lblk,
549 map->m_lblk + map->m_len - 1))
550 status |= EXTENT_STATUS_DELAYED;
551 ret = ext4_es_insert_extent(inode, map->m_lblk,
552 map->m_len, map->m_pblk, status);
553 if (ret < 0)
554 retval = ret;
555 }
Jan Kara2dcba472015-12-07 15:04:57 -0500556 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -0500557
Zheng Liud100eef2013-02-18 00:29:59 -0500558found:
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400559 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Lukas Czernerb8a86842014-03-18 18:05:35 -0400560 ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400561 if (ret != 0)
562 return ret;
563 }
564
Mingming Caof5ab0d12008-02-25 15:29:55 -0500565 /* If it is only a block(s) look up */
Theodore Ts'oc2177052009-05-14 00:58:52 -0400566 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500567 return retval;
568
569 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -0500570 * Returns if the blocks have already allocated
571 *
572 * Note that if blocks have been preallocated
Tao Madf3ab172011-10-08 15:53:49 -0400573 * ext4_ext_get_block() returns the create = 0
Mingming Caof5ab0d12008-02-25 15:29:55 -0500574 * with buffer head unmapped.
575 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400576 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
Lukas Czernerb8a86842014-03-18 18:05:35 -0400577 /*
578 * If we need to convert extent to unwritten
579 * we continue and do the actual work in
580 * ext4_ext_map_blocks()
581 */
582 if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN))
583 return retval;
Mingming Caof5ab0d12008-02-25 15:29:55 -0500584
585 /*
Zheng Liua25a4e12013-02-18 00:28:04 -0500586 * Here we clear m_flags because after allocating an new extent,
587 * it will be set again.
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -0400588 */
Zheng Liua25a4e12013-02-18 00:28:04 -0500589 map->m_flags &= ~EXT4_MAP_FLAGS;
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -0400590
591 /*
Lukas Czerner556615d2014-04-20 23:45:47 -0400592 * New blocks allocate and/or writing to unwritten extent
Mingming Caof5ab0d12008-02-25 15:29:55 -0500593 * will possibly result in updating i_data, so we take
Seunghun Leed91bd2c2014-09-01 22:15:30 -0400594 * the write lock of i_data_sem, and call get_block()
Mingming Caof5ab0d12008-02-25 15:29:55 -0500595 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500596 */
Lukas Czernerc8b459f2014-05-12 12:55:07 -0400597 down_write(&EXT4_I(inode)->i_data_sem);
Mingming Caod2a17632008-07-14 17:52:37 -0400598
599 /*
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500600 * We need to check for EXT4 here because migrate
601 * could have changed the inode type in between
602 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400603 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400604 retval = ext4_ext_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500605 } else {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400606 retval = ext4_ind_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400607
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400608 if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400609 /*
610 * We allocated new blocks which will result in
611 * i_data's format changing. Force the migrate
612 * to fail by clearing migrate flags
613 */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500614 ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400615 }
Mingming Caod2a17632008-07-14 17:52:37 -0400616
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500617 /*
618 * Update reserved blocks/metadata blocks after successful
619 * block allocation which had been deferred till now. We don't
620 * support fallocate for non extent files. So we can update
621 * reserve space here.
622 */
623 if ((retval > 0) &&
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -0500624 (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500625 ext4_da_update_reserve_space(inode, retval, 1);
626 }
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -0400627
Zheng Liuf7fec032013-02-18 00:28:47 -0500628 if (retval > 0) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400629 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -0500630
Zheng Liu44fb851d2013-07-29 12:51:42 -0400631 if (unlikely(retval != map->m_len)) {
632 ext4_warning(inode->i_sb,
633 "ES len assertion failed for inode "
634 "%lu: retval %d != map->m_len %d",
635 inode->i_ino, retval, map->m_len);
636 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400637 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400638
Zheng Liuadb23552013-03-10 21:13:05 -0400639 /*
Jan Karac86d8db2015-12-07 15:10:26 -0500640 * We have to zeroout blocks before inserting them into extent
641 * status tree. Otherwise someone could look them up there and
642 * use them before they are really zeroed.
643 */
644 if (flags & EXT4_GET_BLOCKS_ZERO &&
645 map->m_flags & EXT4_MAP_MAPPED &&
646 map->m_flags & EXT4_MAP_NEW) {
647 ret = ext4_issue_zeroout(inode, map->m_lblk,
648 map->m_pblk, map->m_len);
649 if (ret) {
650 retval = ret;
651 goto out_sem;
652 }
653 }
654
655 /*
Zheng Liuadb23552013-03-10 21:13:05 -0400656 * If the extent has been zeroed out, we don't need to update
657 * extent status tree.
658 */
659 if ((flags & EXT4_GET_BLOCKS_PRE_IO) &&
660 ext4_es_lookup_extent(inode, map->m_lblk, &es)) {
661 if (ext4_es_is_written(&es))
Jan Karac86d8db2015-12-07 15:10:26 -0500662 goto out_sem;
Zheng Liuadb23552013-03-10 21:13:05 -0400663 }
Zheng Liuf7fec032013-02-18 00:28:47 -0500664 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
665 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
666 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
Lukas Czernerd2dc3172015-05-02 21:36:55 -0400667 !(status & EXTENT_STATUS_WRITTEN) &&
Zheng Liuf7fec032013-02-18 00:28:47 -0500668 ext4_find_delalloc_range(inode, map->m_lblk,
669 map->m_lblk + map->m_len - 1))
670 status |= EXTENT_STATUS_DELAYED;
671 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
672 map->m_pblk, status);
Jan Karac86d8db2015-12-07 15:10:26 -0500673 if (ret < 0) {
Zheng Liuf7fec032013-02-18 00:28:47 -0500674 retval = ret;
Jan Karac86d8db2015-12-07 15:10:26 -0500675 goto out_sem;
676 }
Aditya Kali5356f262011-09-09 19:20:51 -0400677 }
678
Jan Karac86d8db2015-12-07 15:10:26 -0500679out_sem:
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500680 up_write((&EXT4_I(inode)->i_data_sem));
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400681 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Lukas Czernerb8a86842014-03-18 18:05:35 -0400682 ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400683 if (ret != 0)
684 return ret;
685 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500686 return retval;
687}
688
Jan Karaed8ad832016-02-19 00:18:25 -0500689/*
690 * Update EXT4_MAP_FLAGS in bh->b_state. For buffer heads attached to pages
691 * we have to be careful as someone else may be manipulating b_state as well.
692 */
693static void ext4_update_bh_state(struct buffer_head *bh, unsigned long flags)
694{
695 unsigned long old_state;
696 unsigned long new_state;
697
698 flags &= EXT4_MAP_FLAGS;
699
700 /* Dummy buffer_head? Set non-atomically. */
701 if (!bh->b_page) {
702 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | flags;
703 return;
704 }
705 /*
706 * Someone else may be modifying b_state. Be careful! This is ugly but
707 * once we get rid of using bh as a container for mapping information
708 * to pass to / from get_block functions, this can go away.
709 */
710 do {
711 old_state = READ_ONCE(bh->b_state);
712 new_state = (old_state & ~EXT4_MAP_FLAGS) | flags;
713 } while (unlikely(
714 cmpxchg(&bh->b_state, old_state, new_state) != old_state));
715}
716
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400717static int _ext4_get_block(struct inode *inode, sector_t iblock,
718 struct buffer_head *bh, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700719{
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400720 struct ext4_map_blocks map;
Jan Karaefe70c22016-03-08 23:35:46 -0500721 int ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700722
Tao Ma46c7f252012-12-10 14:04:52 -0500723 if (ext4_has_inline_data(inode))
724 return -ERANGE;
725
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400726 map.m_lblk = iblock;
727 map.m_len = bh->b_size >> inode->i_blkbits;
728
Jan Karaefe70c22016-03-08 23:35:46 -0500729 ret = ext4_map_blocks(ext4_journal_current_handle(), inode, &map,
730 flags);
Jan Kara7fb54092008-02-10 01:08:38 -0500731 if (ret > 0) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400732 map_bh(bh, inode->i_sb, map.m_pblk);
Jan Karaed8ad832016-02-19 00:18:25 -0500733 ext4_update_bh_state(bh, map.m_flags);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400734 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
Jan Kara7fb54092008-02-10 01:08:38 -0500735 ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700736 }
737 return ret;
738}
739
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400740int ext4_get_block(struct inode *inode, sector_t iblock,
741 struct buffer_head *bh, int create)
742{
743 return _ext4_get_block(inode, iblock, bh,
744 create ? EXT4_GET_BLOCKS_CREATE : 0);
745}
746
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700747/*
Jan Kara705965b2016-03-08 23:08:10 -0500748 * Get block function used when preparing for buffered write if we require
749 * creating an unwritten extent if blocks haven't been allocated. The extent
750 * will be converted to written after the IO is complete.
751 */
752int ext4_get_block_unwritten(struct inode *inode, sector_t iblock,
753 struct buffer_head *bh_result, int create)
754{
755 ext4_debug("ext4_get_block_unwritten: inode %lu, create flag %d\n",
756 inode->i_ino, create);
757 return _ext4_get_block(inode, iblock, bh_result,
758 EXT4_GET_BLOCKS_IO_CREATE_EXT);
759}
760
Jan Karaefe70c22016-03-08 23:35:46 -0500761/* Maximum number of blocks we map for direct IO at once. */
762#define DIO_MAX_BLOCKS 4096
763
764static handle_t *start_dio_trans(struct inode *inode,
765 struct buffer_head *bh_result)
766{
767 int dio_credits;
768
769 /* Trim mapping request to maximum we can map at once for DIO */
770 if (bh_result->b_size >> inode->i_blkbits > DIO_MAX_BLOCKS)
771 bh_result->b_size = DIO_MAX_BLOCKS << inode->i_blkbits;
772 dio_credits = ext4_chunk_trans_blocks(inode,
773 bh_result->b_size >> inode->i_blkbits);
774 return ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, dio_credits);
775}
776
Jan Kara705965b2016-03-08 23:08:10 -0500777/* Get block function for DIO reads and writes to inodes without extents */
778int ext4_dio_get_block(struct inode *inode, sector_t iblock,
779 struct buffer_head *bh, int create)
780{
Jan Karaefe70c22016-03-08 23:35:46 -0500781 handle_t *handle;
782 int ret;
783
784 /* We don't expect handle for direct IO */
785 WARN_ON_ONCE(ext4_journal_current_handle());
786
787 if (create) {
788 handle = start_dio_trans(inode, bh);
789 if (IS_ERR(handle))
790 return PTR_ERR(handle);
791 }
792 ret = _ext4_get_block(inode, iblock, bh,
793 create ? EXT4_GET_BLOCKS_CREATE : 0);
794 if (create)
795 ext4_journal_stop(handle);
796 return ret;
Jan Kara705965b2016-03-08 23:08:10 -0500797}
798
799/*
800 * Get block function for DIO writes when we create unwritten extent if
801 * blocks are not allocated yet. The extent will be converted to written
802 * after IO is complete.
803 */
804static int ext4_dio_get_block_unwritten(struct inode *inode, sector_t iblock,
805 struct buffer_head *bh_result, int create)
806{
Jan Karaefe70c22016-03-08 23:35:46 -0500807 handle_t *handle;
808 int ret;
809
Jan Kara705965b2016-03-08 23:08:10 -0500810 ext4_debug("ext4_dio_get_block_unwritten: inode %lu, create flag %d\n",
811 inode->i_ino, create);
Jan Karaefe70c22016-03-08 23:35:46 -0500812 /* We don't expect handle for direct IO */
813 WARN_ON_ONCE(ext4_journal_current_handle());
814
815 handle = start_dio_trans(inode, bh_result);
816 if (IS_ERR(handle))
817 return PTR_ERR(handle);
818 ret = _ext4_get_block(inode, iblock, bh_result,
819 EXT4_GET_BLOCKS_IO_CREATE_EXT);
820 ext4_journal_stop(handle);
821 if (!ret && buffer_unwritten(bh_result)) {
822 ext4_io_end_t *io_end = ext4_inode_aio(inode);
823
824 set_buffer_defer_completion(bh_result);
825 WARN_ON_ONCE(io_end && !(io_end->flag & EXT4_IO_END_UNWRITTEN));
826 }
827
828 return ret;
Jan Kara705965b2016-03-08 23:08:10 -0500829}
830
831static int ext4_dio_get_block_overwrite(struct inode *inode, sector_t iblock,
832 struct buffer_head *bh_result, int create)
833{
834 int ret;
835
836 ext4_debug("ext4_dio_get_block_overwrite: inode %lu, create flag %d\n",
837 inode->i_ino, create);
Jan Karaefe70c22016-03-08 23:35:46 -0500838 /* We don't expect handle for direct IO */
839 WARN_ON_ONCE(ext4_journal_current_handle());
840
Jan Kara705965b2016-03-08 23:08:10 -0500841 ret = _ext4_get_block(inode, iblock, bh_result, 0);
842 /*
843 * Blocks should have been preallocated! ext4_file_write_iter() checks
844 * that.
845 */
Jan Karaefe70c22016-03-08 23:35:46 -0500846 WARN_ON_ONCE(!buffer_mapped(bh_result) || buffer_unwritten(bh_result));
Jan Kara705965b2016-03-08 23:08:10 -0500847
848 return ret;
849}
850
851
852/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700853 * `handle' can be NULL if create is zero
854 */
Mingming Cao617ba132006-10-11 01:20:53 -0700855struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400856 ext4_lblk_t block, int map_flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700857{
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400858 struct ext4_map_blocks map;
859 struct buffer_head *bh;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400860 int create = map_flags & EXT4_GET_BLOCKS_CREATE;
Theodore Ts'o10560082014-08-29 20:51:32 -0400861 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700862
863 J_ASSERT(handle != NULL || create == 0);
864
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400865 map.m_lblk = block;
866 map.m_len = 1;
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400867 err = ext4_map_blocks(handle, inode, &map, map_flags);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700868
Theodore Ts'o10560082014-08-29 20:51:32 -0400869 if (err == 0)
870 return create ? ERR_PTR(-ENOSPC) : NULL;
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400871 if (err < 0)
Theodore Ts'o10560082014-08-29 20:51:32 -0400872 return ERR_PTR(err);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400873
874 bh = sb_getblk(inode->i_sb, map.m_pblk);
Theodore Ts'o10560082014-08-29 20:51:32 -0400875 if (unlikely(!bh))
876 return ERR_PTR(-ENOMEM);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400877 if (map.m_flags & EXT4_MAP_NEW) {
878 J_ASSERT(create != 0);
879 J_ASSERT(handle != NULL);
880
881 /*
882 * Now that we do not always journal data, we should
883 * keep in mind whether this should always journal the
884 * new buffer as metadata. For now, regular file
885 * writes use ext4_get_block instead, so it's not a
886 * problem.
887 */
888 lock_buffer(bh);
889 BUFFER_TRACE(bh, "call get_create_access");
Theodore Ts'o10560082014-08-29 20:51:32 -0400890 err = ext4_journal_get_create_access(handle, bh);
891 if (unlikely(err)) {
892 unlock_buffer(bh);
893 goto errout;
894 }
895 if (!buffer_uptodate(bh)) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400896 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
897 set_buffer_uptodate(bh);
898 }
899 unlock_buffer(bh);
900 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
901 err = ext4_handle_dirty_metadata(handle, inode, bh);
Theodore Ts'o10560082014-08-29 20:51:32 -0400902 if (unlikely(err))
903 goto errout;
904 } else
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400905 BUFFER_TRACE(bh, "not a new buffer");
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400906 return bh;
Theodore Ts'o10560082014-08-29 20:51:32 -0400907errout:
908 brelse(bh);
909 return ERR_PTR(err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700910}
911
Mingming Cao617ba132006-10-11 01:20:53 -0700912struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400913 ext4_lblk_t block, int map_flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700914{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400915 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700916
Theodore Ts'oc5e298a2015-06-21 01:25:29 -0400917 bh = ext4_getblk(handle, inode, block, map_flags);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400918 if (IS_ERR(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700919 return bh;
Theodore Ts'o1c215022014-08-29 20:52:15 -0400920 if (!bh || buffer_uptodate(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700921 return bh;
Christoph Hellwig65299a32011-08-23 14:50:29 +0200922 ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700923 wait_on_buffer(bh);
924 if (buffer_uptodate(bh))
925 return bh;
926 put_bh(bh);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400927 return ERR_PTR(-EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700928}
929
Tao Maf19d5872012-12-10 14:05:51 -0500930int ext4_walk_page_buffers(handle_t *handle,
931 struct buffer_head *head,
932 unsigned from,
933 unsigned to,
934 int *partial,
935 int (*fn)(handle_t *handle,
936 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700937{
938 struct buffer_head *bh;
939 unsigned block_start, block_end;
940 unsigned blocksize = head->b_size;
941 int err, ret = 0;
942 struct buffer_head *next;
943
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400944 for (bh = head, block_start = 0;
945 ret == 0 && (bh != head || !block_start);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400946 block_start = block_end, bh = next) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700947 next = bh->b_this_page;
948 block_end = block_start + blocksize;
949 if (block_end <= from || block_start >= to) {
950 if (partial && !buffer_uptodate(bh))
951 *partial = 1;
952 continue;
953 }
954 err = (*fn)(handle, bh);
955 if (!ret)
956 ret = err;
957 }
958 return ret;
959}
960
961/*
962 * To preserve ordering, it is essential that the hole instantiation and
963 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -0700964 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -0700965 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700966 * prepare_write() is the right place.
967 *
Jan Kara36ade452013-01-28 09:30:52 -0500968 * Also, this function can nest inside ext4_writepage(). In that case, we
969 * *know* that ext4_writepage() has generated enough buffer credits to do the
970 * whole page. So we won't block on the journal in that case, which is good,
971 * because the caller may be PF_MEMALLOC.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700972 *
Mingming Cao617ba132006-10-11 01:20:53 -0700973 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700974 * quota file writes. If we were to commit the transaction while thus
975 * reentered, there can be a deadlock - we would be holding a quota
976 * lock, and the commit would never complete if another thread had a
977 * transaction open and was blocking on the quota lock - a ranking
978 * violation.
979 *
Mingming Caodab291a2006-10-11 01:21:01 -0700980 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700981 * will _not_ run commit under these circumstances because handle->h_ref
982 * is elevated. We'll still have enough credits for the tiny quotafile
983 * write.
984 */
Tao Maf19d5872012-12-10 14:05:51 -0500985int do_journal_get_write_access(handle_t *handle,
986 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700987{
Jan Kara56d35a42010-08-05 14:41:42 -0400988 int dirty = buffer_dirty(bh);
989 int ret;
990
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700991 if (!buffer_mapped(bh) || buffer_freed(bh))
992 return 0;
Jan Kara56d35a42010-08-05 14:41:42 -0400993 /*
Christoph Hellwigebdec242010-10-06 10:47:23 +0200994 * __block_write_begin() could have dirtied some buffers. Clean
Jan Kara56d35a42010-08-05 14:41:42 -0400995 * the dirty bit as jbd2_journal_get_write_access() could complain
996 * otherwise about fs integrity issues. Setting of the dirty bit
Christoph Hellwigebdec242010-10-06 10:47:23 +0200997 * by __block_write_begin() isn't a real problem here as we clear
Jan Kara56d35a42010-08-05 14:41:42 -0400998 * the bit before releasing a page lock and thus writeback cannot
999 * ever write the buffer.
1000 */
1001 if (dirty)
1002 clear_buffer_dirty(bh);
liang xie5d601252014-05-12 22:06:43 -04001003 BUFFER_TRACE(bh, "get write access");
Jan Kara56d35a42010-08-05 14:41:42 -04001004 ret = ext4_journal_get_write_access(handle, bh);
1005 if (!ret && dirty)
1006 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
1007 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001008}
1009
Michael Halcrow2058f832015-04-12 00:55:10 -04001010#ifdef CONFIG_EXT4_FS_ENCRYPTION
1011static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len,
1012 get_block_t *get_block)
1013{
1014 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
1015 unsigned to = from + len;
1016 struct inode *inode = page->mapping->host;
1017 unsigned block_start, block_end;
1018 sector_t block;
1019 int err = 0;
1020 unsigned blocksize = inode->i_sb->s_blocksize;
1021 unsigned bbits;
1022 struct buffer_head *bh, *head, *wait[2], **wait_bh = wait;
1023 bool decrypt = false;
1024
1025 BUG_ON(!PageLocked(page));
1026 BUG_ON(from > PAGE_CACHE_SIZE);
1027 BUG_ON(to > PAGE_CACHE_SIZE);
1028 BUG_ON(from > to);
1029
1030 if (!page_has_buffers(page))
1031 create_empty_buffers(page, blocksize, 0);
1032 head = page_buffers(page);
1033 bbits = ilog2(blocksize);
1034 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
1035
1036 for (bh = head, block_start = 0; bh != head || !block_start;
1037 block++, block_start = block_end, bh = bh->b_this_page) {
1038 block_end = block_start + blocksize;
1039 if (block_end <= from || block_start >= to) {
1040 if (PageUptodate(page)) {
1041 if (!buffer_uptodate(bh))
1042 set_buffer_uptodate(bh);
1043 }
1044 continue;
1045 }
1046 if (buffer_new(bh))
1047 clear_buffer_new(bh);
1048 if (!buffer_mapped(bh)) {
1049 WARN_ON(bh->b_size != blocksize);
1050 err = get_block(inode, block, bh, 1);
1051 if (err)
1052 break;
1053 if (buffer_new(bh)) {
1054 unmap_underlying_metadata(bh->b_bdev,
1055 bh->b_blocknr);
1056 if (PageUptodate(page)) {
1057 clear_buffer_new(bh);
1058 set_buffer_uptodate(bh);
1059 mark_buffer_dirty(bh);
1060 continue;
1061 }
1062 if (block_end > to || block_start < from)
1063 zero_user_segments(page, to, block_end,
1064 block_start, from);
1065 continue;
1066 }
1067 }
1068 if (PageUptodate(page)) {
1069 if (!buffer_uptodate(bh))
1070 set_buffer_uptodate(bh);
1071 continue;
1072 }
1073 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
1074 !buffer_unwritten(bh) &&
1075 (block_start < from || block_end > to)) {
1076 ll_rw_block(READ, 1, &bh);
1077 *wait_bh++ = bh;
1078 decrypt = ext4_encrypted_inode(inode) &&
1079 S_ISREG(inode->i_mode);
1080 }
1081 }
1082 /*
1083 * If we issued read requests, let them complete.
1084 */
1085 while (wait_bh > wait) {
1086 wait_on_buffer(*--wait_bh);
1087 if (!buffer_uptodate(*wait_bh))
1088 err = -EIO;
1089 }
1090 if (unlikely(err))
1091 page_zero_new_buffers(page, from, to);
1092 else if (decrypt)
Theodore Ts'o3684de82015-10-03 10:49:26 -04001093 err = ext4_decrypt(page);
Michael Halcrow2058f832015-04-12 00:55:10 -04001094 return err;
1095}
1096#endif
1097
Nick Pigginbfc1af62007-10-16 01:25:05 -07001098static int ext4_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001099 loff_t pos, unsigned len, unsigned flags,
1100 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001101{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001102 struct inode *inode = mapping->host;
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001103 int ret, needed_blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001104 handle_t *handle;
1105 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001106 struct page *page;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001107 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001108 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001109
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001110 trace_ext4_write_begin(inode, pos, len, flags);
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001111 /*
1112 * Reserve one block more for addition to orphan list in case
1113 * we allocate blocks but write fails for some reason
1114 */
1115 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001116 index = pos >> PAGE_CACHE_SHIFT;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001117 from = pos & (PAGE_CACHE_SIZE - 1);
1118 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001119
Tao Maf19d5872012-12-10 14:05:51 -05001120 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
1121 ret = ext4_try_to_write_inline_data(mapping, inode, pos, len,
1122 flags, pagep);
1123 if (ret < 0)
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001124 return ret;
1125 if (ret == 1)
1126 return 0;
Tao Maf19d5872012-12-10 14:05:51 -05001127 }
1128
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001129 /*
1130 * grab_cache_page_write_begin() can take a long time if the
1131 * system is thrashing due to memory pressure, or if the page
1132 * is being written back. So grab it first before we start
1133 * the transaction handle. This also allows us to allocate
1134 * the page (if needed) without using GFP_NOFS.
1135 */
1136retry_grab:
Nick Piggin54566b22009-01-04 12:00:53 -08001137 page = grab_cache_page_write_begin(mapping, index, flags);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001138 if (!page)
1139 return -ENOMEM;
1140 unlock_page(page);
1141
1142retry_journal:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001143 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001144 if (IS_ERR(handle)) {
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001145 page_cache_release(page);
1146 return PTR_ERR(handle);
Jan Karacf108bc2008-07-11 19:27:31 -04001147 }
Tao Maf19d5872012-12-10 14:05:51 -05001148
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001149 lock_page(page);
1150 if (page->mapping != mapping) {
1151 /* The page got truncated from under us */
1152 unlock_page(page);
1153 page_cache_release(page);
Jan Karacf108bc2008-07-11 19:27:31 -04001154 ext4_journal_stop(handle);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001155 goto retry_grab;
Jan Karacf108bc2008-07-11 19:27:31 -04001156 }
Dmitry Monakhov7afe5aa2013-08-28 14:30:47 -04001157 /* In case writeback began while the page was unlocked */
1158 wait_for_stable_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04001159
Michael Halcrow2058f832015-04-12 00:55:10 -04001160#ifdef CONFIG_EXT4_FS_ENCRYPTION
1161 if (ext4_should_dioread_nolock(inode))
1162 ret = ext4_block_write_begin(page, pos, len,
Jan Kara705965b2016-03-08 23:08:10 -05001163 ext4_get_block_unwritten);
Michael Halcrow2058f832015-04-12 00:55:10 -04001164 else
1165 ret = ext4_block_write_begin(page, pos, len,
1166 ext4_get_block);
1167#else
Jiaying Zhang744692d2010-03-04 16:14:02 -05001168 if (ext4_should_dioread_nolock(inode))
Jan Kara705965b2016-03-08 23:08:10 -05001169 ret = __block_write_begin(page, pos, len,
1170 ext4_get_block_unwritten);
Jiaying Zhang744692d2010-03-04 16:14:02 -05001171 else
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001172 ret = __block_write_begin(page, pos, len, ext4_get_block);
Michael Halcrow2058f832015-04-12 00:55:10 -04001173#endif
Nick Pigginbfc1af62007-10-16 01:25:05 -07001174 if (!ret && ext4_should_journal_data(inode)) {
Tao Maf19d5872012-12-10 14:05:51 -05001175 ret = ext4_walk_page_buffers(handle, page_buffers(page),
1176 from, to, NULL,
1177 do_journal_get_write_access);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001178 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001179
1180 if (ret) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001181 unlock_page(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001182 /*
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001183 * __block_write_begin may have instantiated a few blocks
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001184 * outside i_size. Trim these off again. Don't need
1185 * i_size_read because we hold i_mutex.
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001186 *
1187 * Add inode to orphan list in case we crash before
1188 * truncate finishes
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001189 */
Jan Karaffacfa72009-07-13 16:22:22 -04001190 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001191 ext4_orphan_add(handle, inode);
1192
1193 ext4_journal_stop(handle);
1194 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001195 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001196 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001197 * If truncate failed early the inode might
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001198 * still be on the orphan list; we need to
1199 * make sure the inode is removed from the
1200 * orphan list in that case.
1201 */
1202 if (inode->i_nlink)
1203 ext4_orphan_del(NULL, inode);
1204 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001205
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001206 if (ret == -ENOSPC &&
1207 ext4_should_retry_alloc(inode->i_sb, &retries))
1208 goto retry_journal;
1209 page_cache_release(page);
1210 return ret;
1211 }
1212 *pagep = page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001213 return ret;
1214}
1215
Nick Pigginbfc1af62007-10-16 01:25:05 -07001216/* For write_end() in data=journal mode */
1217static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001218{
Theodore Ts'o13fca322013-04-21 16:45:54 -04001219 int ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001220 if (!buffer_mapped(bh) || buffer_freed(bh))
1221 return 0;
1222 set_buffer_uptodate(bh);
Theodore Ts'o13fca322013-04-21 16:45:54 -04001223 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
1224 clear_buffer_meta(bh);
1225 clear_buffer_prio(bh);
1226 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001227}
1228
Zheng Liueed43332013-04-03 12:41:17 -04001229/*
1230 * We need to pick up the new inode size which generic_commit_write gave us
1231 * `file' can be NULL - eg, when called from page_symlink().
1232 *
1233 * ext4 never places buffers on inode->i_mapping->private_list. metadata
1234 * buffers are managed internally.
1235 */
1236static int ext4_write_end(struct file *file,
1237 struct address_space *mapping,
1238 loff_t pos, unsigned len, unsigned copied,
1239 struct page *page, void *fsdata)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001240{
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001241 handle_t *handle = ext4_journal_current_handle();
Zheng Liueed43332013-04-03 12:41:17 -04001242 struct inode *inode = mapping->host;
Xiaoguang Wang05726392015-02-12 23:00:17 -05001243 loff_t old_size = inode->i_size;
Zheng Liueed43332013-04-03 12:41:17 -04001244 int ret = 0, ret2;
1245 int i_size_changed = 0;
1246
1247 trace_ext4_write_end(inode, pos, len, copied);
1248 if (ext4_test_inode_state(inode, EXT4_STATE_ORDERED_MODE)) {
1249 ret = ext4_jbd2_file_inode(handle, inode);
1250 if (ret) {
1251 unlock_page(page);
1252 page_cache_release(page);
1253 goto errout;
1254 }
1255 }
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001256
Theodore Ts'o42c832d2013-07-01 08:12:39 -04001257 if (ext4_has_inline_data(inode)) {
1258 ret = ext4_write_inline_data_end(inode, pos, len,
1259 copied, page);
1260 if (ret < 0)
1261 goto errout;
1262 copied = ret;
1263 } else
Tao Maf19d5872012-12-10 14:05:51 -05001264 copied = block_write_end(file, mapping, pos,
1265 len, copied, page, fsdata);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001266 /*
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001267 * it's important to update i_size while still holding page lock:
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001268 * page writeout could otherwise come in and zero beyond i_size.
1269 */
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001270 i_size_changed = ext4_update_inode_size(inode, pos + copied);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001271 unlock_page(page);
1272 page_cache_release(page);
1273
Xiaoguang Wang05726392015-02-12 23:00:17 -05001274 if (old_size < pos)
1275 pagecache_isize_extended(inode, old_size, pos);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001276 /*
1277 * Don't mark the inode dirty under page lock. First, it unnecessarily
1278 * makes the holding time of page lock longer. Second, it forces lock
1279 * ordering of page lock and transaction start for journaling
1280 * filesystems.
1281 */
1282 if (i_size_changed)
1283 ext4_mark_inode_dirty(handle, inode);
1284
Jan Karaffacfa72009-07-13 16:22:22 -04001285 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001286 /* if we have allocated more blocks and copied
1287 * less. We will have blocks allocated outside
1288 * inode->i_size. So truncate them
1289 */
1290 ext4_orphan_add(handle, inode);
Theodore Ts'o74d553a2013-04-03 12:39:17 -04001291errout:
Mingming Cao617ba132006-10-11 01:20:53 -07001292 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001293 if (!ret)
1294 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001295
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001296 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001297 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001298 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001299 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001300 * on the orphan list; we need to make sure the inode
1301 * is removed from the orphan list in that case.
1302 */
1303 if (inode->i_nlink)
1304 ext4_orphan_del(NULL, inode);
1305 }
1306
Nick Pigginbfc1af62007-10-16 01:25:05 -07001307 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001308}
1309
Theodore Ts'ob90197b2015-10-15 10:29:05 -04001310/*
1311 * This is a private version of page_zero_new_buffers() which doesn't
1312 * set the buffer to be dirty, since in data=journalled mode we need
1313 * to call ext4_handle_dirty_metadata() instead.
1314 */
1315static void zero_new_buffers(struct page *page, unsigned from, unsigned to)
1316{
1317 unsigned int block_start = 0, block_end;
1318 struct buffer_head *head, *bh;
1319
1320 bh = head = page_buffers(page);
1321 do {
1322 block_end = block_start + bh->b_size;
1323 if (buffer_new(bh)) {
1324 if (block_end > from && block_start < to) {
1325 if (!PageUptodate(page)) {
1326 unsigned start, size;
1327
1328 start = max(from, block_start);
1329 size = min(to, block_end) - start;
1330
1331 zero_user(page, start, size);
1332 set_buffer_uptodate(bh);
1333 }
1334 clear_buffer_new(bh);
1335 }
1336 }
1337 block_start = block_end;
1338 bh = bh->b_this_page;
1339 } while (bh != head);
1340}
1341
Nick Pigginbfc1af62007-10-16 01:25:05 -07001342static int ext4_journalled_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001343 struct address_space *mapping,
1344 loff_t pos, unsigned len, unsigned copied,
1345 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001346{
Mingming Cao617ba132006-10-11 01:20:53 -07001347 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -07001348 struct inode *inode = mapping->host;
Xiaoguang Wang05726392015-02-12 23:00:17 -05001349 loff_t old_size = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001350 int ret = 0, ret2;
1351 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001352 unsigned from, to;
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001353 int size_changed = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001354
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001355 trace_ext4_journalled_write_end(inode, pos, len, copied);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001356 from = pos & (PAGE_CACHE_SIZE - 1);
1357 to = from + len;
1358
Curt Wohlgemuth441c8502011-08-13 11:25:18 -04001359 BUG_ON(!ext4_handle_valid(handle));
1360
Tao Ma3fdcfb62012-12-10 14:05:57 -05001361 if (ext4_has_inline_data(inode))
1362 copied = ext4_write_inline_data_end(inode, pos, len,
1363 copied, page);
1364 else {
1365 if (copied < len) {
1366 if (!PageUptodate(page))
1367 copied = 0;
Theodore Ts'ob90197b2015-10-15 10:29:05 -04001368 zero_new_buffers(page, from+copied, to);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001369 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001370
Tao Ma3fdcfb62012-12-10 14:05:57 -05001371 ret = ext4_walk_page_buffers(handle, page_buffers(page), from,
1372 to, &partial, write_end_fn);
1373 if (!partial)
1374 SetPageUptodate(page);
1375 }
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001376 size_changed = ext4_update_inode_size(inode, pos + copied);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001377 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Jan Kara2d859db2011-07-26 09:07:11 -04001378 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001379 unlock_page(page);
1380 page_cache_release(page);
1381
Xiaoguang Wang05726392015-02-12 23:00:17 -05001382 if (old_size < pos)
1383 pagecache_isize_extended(inode, old_size, pos);
1384
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001385 if (size_changed) {
Mingming Cao617ba132006-10-11 01:20:53 -07001386 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001387 if (!ret)
1388 ret = ret2;
1389 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001390
Jan Karaffacfa72009-07-13 16:22:22 -04001391 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001392 /* if we have allocated more blocks and copied
1393 * less. We will have blocks allocated outside
1394 * inode->i_size. So truncate them
1395 */
1396 ext4_orphan_add(handle, inode);
1397
Mingming Cao617ba132006-10-11 01:20:53 -07001398 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001399 if (!ret)
1400 ret = ret2;
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001401 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001402 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001403 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001404 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001405 * on the orphan list; we need to make sure the inode
1406 * is removed from the orphan list in that case.
1407 */
1408 if (inode->i_nlink)
1409 ext4_orphan_del(NULL, inode);
1410 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001411
1412 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001413}
Mingming Caod2a17632008-07-14 17:52:37 -04001414
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001415/*
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001416 * Reserve space for a single cluster
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001417 */
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001418static int ext4_da_reserve_space(struct inode *inode)
Mingming Caod2a17632008-07-14 17:52:37 -04001419{
Mingming Cao60e58e02009-01-22 18:13:05 +01001420 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001421 struct ext4_inode_info *ei = EXT4_I(inode);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001422 int ret;
Mingming Caod2a17632008-07-14 17:52:37 -04001423
Mingming Cao60e58e02009-01-22 18:13:05 +01001424 /*
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001425 * We will charge metadata quota at writeout time; this saves
1426 * us from metadata over-estimation, though we may go over by
1427 * a small amount in the end. Here we just reserve for data.
Mingming Cao60e58e02009-01-22 18:13:05 +01001428 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04001429 ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001430 if (ret)
1431 return ret;
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001432
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001433 spin_lock(&ei->i_block_reservation_lock);
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04001434 if (ext4_claim_free_clusters(sbi, 1, 0)) {
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001435 spin_unlock(&ei->i_block_reservation_lock);
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001436 dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
Mingming Caod2a17632008-07-14 17:52:37 -04001437 return -ENOSPC;
1438 }
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001439 ei->i_reserved_data_blocks++;
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001440 trace_ext4_da_reserve_space(inode);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001441 spin_unlock(&ei->i_block_reservation_lock);
Dmitry Monakhov39bc6802009-12-10 16:36:27 +00001442
Mingming Caod2a17632008-07-14 17:52:37 -04001443 return 0; /* success */
1444}
1445
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001446static void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001447{
1448 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001449 struct ext4_inode_info *ei = EXT4_I(inode);
Mingming Caod2a17632008-07-14 17:52:37 -04001450
Mingming Caocd213222008-08-19 22:16:59 -04001451 if (!to_free)
1452 return; /* Nothing to release, exit */
1453
Mingming Caod2a17632008-07-14 17:52:37 -04001454 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001455
Li Zefan5a58ec82010-05-17 02:00:00 -04001456 trace_ext4_da_release_space(inode, to_free);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001457 if (unlikely(to_free > ei->i_reserved_data_blocks)) {
Mingming Caocd213222008-08-19 22:16:59 -04001458 /*
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001459 * if there aren't enough reserved blocks, then the
1460 * counter is messed up somewhere. Since this
1461 * function is called from invalidate page, it's
1462 * harmless to return without any action.
Mingming Caocd213222008-08-19 22:16:59 -04001463 */
Theodore Ts'o8de5c322013-02-14 15:11:41 -05001464 ext4_warning(inode->i_sb, "ext4_da_release_space: "
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001465 "ino %lu, to_free %d with only %d reserved "
Theodore Ts'o1084f252012-03-19 23:13:43 -04001466 "data blocks", inode->i_ino, to_free,
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001467 ei->i_reserved_data_blocks);
1468 WARN_ON(1);
1469 to_free = ei->i_reserved_data_blocks;
1470 }
1471 ei->i_reserved_data_blocks -= to_free;
1472
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001473 /* update fs dirty data blocks counter */
Theodore Ts'o57042652011-09-09 18:56:51 -04001474 percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free);
Mingming Caod2a17632008-07-14 17:52:37 -04001475
Mingming Caod2a17632008-07-14 17:52:37 -04001476 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001477
Aditya Kali7b415bf2011-09-09 19:04:51 -04001478 dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free));
Mingming Caod2a17632008-07-14 17:52:37 -04001479}
1480
1481static void ext4_da_page_release_reservation(struct page *page,
Lukas Czernerca99fdd2013-05-21 23:25:01 -04001482 unsigned int offset,
1483 unsigned int length)
Mingming Caod2a17632008-07-14 17:52:37 -04001484{
Lukas Czerner9705acd2015-07-03 21:13:55 -04001485 int to_release = 0, contiguous_blks = 0;
Mingming Caod2a17632008-07-14 17:52:37 -04001486 struct buffer_head *head, *bh;
1487 unsigned int curr_off = 0;
Aditya Kali7b415bf2011-09-09 19:04:51 -04001488 struct inode *inode = page->mapping->host;
1489 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Lukas Czernerca99fdd2013-05-21 23:25:01 -04001490 unsigned int stop = offset + length;
Aditya Kali7b415bf2011-09-09 19:04:51 -04001491 int num_clusters;
Zheng Liu51865fd2012-11-08 21:57:32 -05001492 ext4_fsblk_t lblk;
Mingming Caod2a17632008-07-14 17:52:37 -04001493
Lukas Czernerca99fdd2013-05-21 23:25:01 -04001494 BUG_ON(stop > PAGE_CACHE_SIZE || stop < length);
1495
Mingming Caod2a17632008-07-14 17:52:37 -04001496 head = page_buffers(page);
1497 bh = head;
1498 do {
1499 unsigned int next_off = curr_off + bh->b_size;
1500
Lukas Czernerca99fdd2013-05-21 23:25:01 -04001501 if (next_off > stop)
1502 break;
1503
Mingming Caod2a17632008-07-14 17:52:37 -04001504 if ((offset <= curr_off) && (buffer_delay(bh))) {
1505 to_release++;
Lukas Czerner9705acd2015-07-03 21:13:55 -04001506 contiguous_blks++;
Mingming Caod2a17632008-07-14 17:52:37 -04001507 clear_buffer_delay(bh);
Lukas Czerner9705acd2015-07-03 21:13:55 -04001508 } else if (contiguous_blks) {
1509 lblk = page->index <<
1510 (PAGE_CACHE_SHIFT - inode->i_blkbits);
1511 lblk += (curr_off >> inode->i_blkbits) -
1512 contiguous_blks;
1513 ext4_es_remove_extent(inode, lblk, contiguous_blks);
1514 contiguous_blks = 0;
Mingming Caod2a17632008-07-14 17:52:37 -04001515 }
1516 curr_off = next_off;
1517 } while ((bh = bh->b_this_page) != head);
Aditya Kali7b415bf2011-09-09 19:04:51 -04001518
Lukas Czerner9705acd2015-07-03 21:13:55 -04001519 if (contiguous_blks) {
Zheng Liu51865fd2012-11-08 21:57:32 -05001520 lblk = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
Lukas Czerner9705acd2015-07-03 21:13:55 -04001521 lblk += (curr_off >> inode->i_blkbits) - contiguous_blks;
1522 ext4_es_remove_extent(inode, lblk, contiguous_blks);
Zheng Liu51865fd2012-11-08 21:57:32 -05001523 }
1524
Aditya Kali7b415bf2011-09-09 19:04:51 -04001525 /* If we have released all the blocks belonging to a cluster, then we
1526 * need to release the reserved space for that cluster. */
1527 num_clusters = EXT4_NUM_B2C(sbi, to_release);
1528 while (num_clusters > 0) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04001529 lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) +
1530 ((num_clusters - 1) << sbi->s_cluster_bits);
1531 if (sbi->s_cluster_ratio == 1 ||
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05001532 !ext4_find_delalloc_cluster(inode, lblk))
Aditya Kali7b415bf2011-09-09 19:04:51 -04001533 ext4_da_release_space(inode, 1);
1534
1535 num_clusters--;
1536 }
Mingming Caod2a17632008-07-14 17:52:37 -04001537}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001538
1539/*
Alex Tomas64769242008-07-11 19:27:31 -04001540 * Delayed allocation stuff
1541 */
1542
Jan Kara4e7ea812013-06-04 13:17:40 -04001543struct mpage_da_data {
1544 struct inode *inode;
1545 struct writeback_control *wbc;
Jan Kara6b523df2013-06-04 13:21:11 -04001546
Jan Kara4e7ea812013-06-04 13:17:40 -04001547 pgoff_t first_page; /* The first page to write */
1548 pgoff_t next_page; /* Current page to examine */
1549 pgoff_t last_page; /* Last page to examine */
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001550 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04001551 * Extent to map - this can be after first_page because that can be
1552 * fully mapped. We somewhat abuse m_flags to store whether the extent
1553 * is delalloc or unwritten.
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001554 */
Jan Kara4e7ea812013-06-04 13:17:40 -04001555 struct ext4_map_blocks map;
1556 struct ext4_io_submit io_submit; /* IO submission data */
1557};
Alex Tomas64769242008-07-11 19:27:31 -04001558
Jan Kara4e7ea812013-06-04 13:17:40 -04001559static void mpage_release_unused_pages(struct mpage_da_data *mpd,
1560 bool invalidate)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001561{
1562 int nr_pages, i;
1563 pgoff_t index, end;
1564 struct pagevec pvec;
1565 struct inode *inode = mpd->inode;
1566 struct address_space *mapping = inode->i_mapping;
Jan Kara4e7ea812013-06-04 13:17:40 -04001567
1568 /* This is necessary when next_page == 0. */
1569 if (mpd->first_page >= mpd->next_page)
1570 return;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001571
Curt Wohlgemuthc7f59382011-02-26 12:27:52 -05001572 index = mpd->first_page;
1573 end = mpd->next_page - 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04001574 if (invalidate) {
1575 ext4_lblk_t start, last;
1576 start = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1577 last = end << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1578 ext4_es_remove_extent(inode, start, last - start + 1);
1579 }
Zheng Liu51865fd2012-11-08 21:57:32 -05001580
Eric Sandeen66bea922012-11-14 22:22:05 -05001581 pagevec_init(&pvec, 0);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001582 while (index <= end) {
1583 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1584 if (nr_pages == 0)
1585 break;
1586 for (i = 0; i < nr_pages; i++) {
1587 struct page *page = pvec.pages[i];
Jan Kara9b1d09982010-03-03 16:19:32 -05001588 if (page->index > end)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001589 break;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001590 BUG_ON(!PageLocked(page));
1591 BUG_ON(PageWriteback(page));
Jan Kara4e7ea812013-06-04 13:17:40 -04001592 if (invalidate) {
1593 block_invalidatepage(page, 0, PAGE_CACHE_SIZE);
1594 ClearPageUptodate(page);
1595 }
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001596 unlock_page(page);
1597 }
Jan Kara9b1d09982010-03-03 16:19:32 -05001598 index = pvec.pages[nr_pages - 1]->index + 1;
1599 pagevec_release(&pvec);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001600 }
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001601}
1602
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001603static void ext4_print_free_blocks(struct inode *inode)
1604{
1605 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o92b97812012-03-19 23:41:49 -04001606 struct super_block *sb = inode->i_sb;
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001607 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'o92b97812012-03-19 23:41:49 -04001608
1609 ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld",
Theodore Ts'o5dee5432011-09-09 19:10:51 -04001610 EXT4_C2B(EXT4_SB(inode->i_sb),
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001611 ext4_count_free_clusters(sb)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001612 ext4_msg(sb, KERN_CRIT, "Free/Dirty block details");
1613 ext4_msg(sb, KERN_CRIT, "free_blocks=%lld",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001614 (long long) EXT4_C2B(EXT4_SB(sb),
Theodore Ts'o57042652011-09-09 18:56:51 -04001615 percpu_counter_sum(&sbi->s_freeclusters_counter)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001616 ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001617 (long long) EXT4_C2B(EXT4_SB(sb),
Aditya Kali7b415bf2011-09-09 19:04:51 -04001618 percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001619 ext4_msg(sb, KERN_CRIT, "Block reservation details");
1620 ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001621 ei->i_reserved_data_blocks);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001622 return;
1623}
1624
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001625static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001626{
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001627 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001628}
1629
Alex Tomas64769242008-07-11 19:27:31 -04001630/*
Aditya Kali5356f262011-09-09 19:20:51 -04001631 * This function is grabs code from the very beginning of
1632 * ext4_map_blocks, but assumes that the caller is from delayed write
1633 * time. This function looks up the requested blocks and sets the
1634 * buffer delay bit under the protection of i_data_sem.
1635 */
1636static int ext4_da_map_blocks(struct inode *inode, sector_t iblock,
1637 struct ext4_map_blocks *map,
1638 struct buffer_head *bh)
1639{
Zheng Liud100eef2013-02-18 00:29:59 -05001640 struct extent_status es;
Aditya Kali5356f262011-09-09 19:20:51 -04001641 int retval;
1642 sector_t invalid_block = ~((sector_t) 0xffff);
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001643#ifdef ES_AGGRESSIVE_TEST
1644 struct ext4_map_blocks orig_map;
1645
1646 memcpy(&orig_map, map, sizeof(*map));
1647#endif
Aditya Kali5356f262011-09-09 19:20:51 -04001648
1649 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
1650 invalid_block = ~0;
1651
1652 map->m_flags = 0;
1653 ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
1654 "logical block %lu\n", inode->i_ino, map->m_len,
1655 (unsigned long) map->m_lblk);
Zheng Liud100eef2013-02-18 00:29:59 -05001656
1657 /* Lookup extent status tree firstly */
1658 if (ext4_es_lookup_extent(inode, iblock, &es)) {
Zheng Liud100eef2013-02-18 00:29:59 -05001659 if (ext4_es_is_hole(&es)) {
1660 retval = 0;
Lukas Czernerc8b459f2014-05-12 12:55:07 -04001661 down_read(&EXT4_I(inode)->i_data_sem);
Zheng Liud100eef2013-02-18 00:29:59 -05001662 goto add_delayed;
1663 }
1664
1665 /*
1666 * Delayed extent could be allocated by fallocate.
1667 * So we need to check it.
1668 */
1669 if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) {
1670 map_bh(bh, inode->i_sb, invalid_block);
1671 set_buffer_new(bh);
1672 set_buffer_delay(bh);
1673 return 0;
1674 }
1675
1676 map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk;
1677 retval = es.es_len - (iblock - es.es_lblk);
1678 if (retval > map->m_len)
1679 retval = map->m_len;
1680 map->m_len = retval;
1681 if (ext4_es_is_written(&es))
1682 map->m_flags |= EXT4_MAP_MAPPED;
1683 else if (ext4_es_is_unwritten(&es))
1684 map->m_flags |= EXT4_MAP_UNWRITTEN;
1685 else
1686 BUG_ON(1);
1687
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001688#ifdef ES_AGGRESSIVE_TEST
1689 ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0);
1690#endif
Zheng Liud100eef2013-02-18 00:29:59 -05001691 return retval;
1692 }
1693
Aditya Kali5356f262011-09-09 19:20:51 -04001694 /*
1695 * Try to see if we can get the block without requesting a new
1696 * file system block.
1697 */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04001698 down_read(&EXT4_I(inode)->i_data_sem);
Jan Karacbd75842014-11-25 11:41:49 -05001699 if (ext4_has_inline_data(inode))
Tao Ma9c3569b2012-12-10 14:05:57 -05001700 retval = 0;
Jan Karacbd75842014-11-25 11:41:49 -05001701 else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Zheng Liu2f8e0a72014-11-25 11:44:37 -05001702 retval = ext4_ext_map_blocks(NULL, inode, map, 0);
Aditya Kali5356f262011-09-09 19:20:51 -04001703 else
Zheng Liu2f8e0a72014-11-25 11:44:37 -05001704 retval = ext4_ind_map_blocks(NULL, inode, map, 0);
Aditya Kali5356f262011-09-09 19:20:51 -04001705
Zheng Liud100eef2013-02-18 00:29:59 -05001706add_delayed:
Aditya Kali5356f262011-09-09 19:20:51 -04001707 if (retval == 0) {
Zheng Liuf7fec032013-02-18 00:28:47 -05001708 int ret;
Aditya Kali5356f262011-09-09 19:20:51 -04001709 /*
1710 * XXX: __block_prepare_write() unmaps passed block,
1711 * is it OK?
1712 */
Lukas Czerner386ad672013-03-10 22:50:00 -04001713 /*
1714 * If the block was allocated from previously allocated cluster,
1715 * then we don't need to reserve it again. However we still need
1716 * to reserve metadata for every block we're going to write.
1717 */
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001718 if (EXT4_SB(inode->i_sb)->s_cluster_ratio == 1 ||
Jan Karacbd75842014-11-25 11:41:49 -05001719 !ext4_find_delalloc_cluster(inode, map->m_lblk)) {
Eric Whitneyc27e43a2015-06-21 21:37:05 -04001720 ret = ext4_da_reserve_space(inode);
Zheng Liuf7fec032013-02-18 00:28:47 -05001721 if (ret) {
Aditya Kali5356f262011-09-09 19:20:51 -04001722 /* not enough space to reserve */
Zheng Liuf7fec032013-02-18 00:28:47 -05001723 retval = ret;
Aditya Kali5356f262011-09-09 19:20:51 -04001724 goto out_unlock;
Zheng Liuf7fec032013-02-18 00:28:47 -05001725 }
Aditya Kali5356f262011-09-09 19:20:51 -04001726 }
1727
Zheng Liuf7fec032013-02-18 00:28:47 -05001728 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
1729 ~0, EXTENT_STATUS_DELAYED);
1730 if (ret) {
1731 retval = ret;
Zheng Liu51865fd2012-11-08 21:57:32 -05001732 goto out_unlock;
Zheng Liuf7fec032013-02-18 00:28:47 -05001733 }
Zheng Liu51865fd2012-11-08 21:57:32 -05001734
Aditya Kali5356f262011-09-09 19:20:51 -04001735 map_bh(bh, inode->i_sb, invalid_block);
1736 set_buffer_new(bh);
1737 set_buffer_delay(bh);
Zheng Liuf7fec032013-02-18 00:28:47 -05001738 } else if (retval > 0) {
1739 int ret;
Theodore Ts'o3be78c72013-08-16 21:22:41 -04001740 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -05001741
Zheng Liu44fb851d2013-07-29 12:51:42 -04001742 if (unlikely(retval != map->m_len)) {
1743 ext4_warning(inode->i_sb,
1744 "ES len assertion failed for inode "
1745 "%lu: retval %d != map->m_len %d",
1746 inode->i_ino, retval, map->m_len);
1747 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001748 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001749
Zheng Liuf7fec032013-02-18 00:28:47 -05001750 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
1751 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
1752 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
1753 map->m_pblk, status);
1754 if (ret != 0)
1755 retval = ret;
Aditya Kali5356f262011-09-09 19:20:51 -04001756 }
1757
1758out_unlock:
1759 up_read((&EXT4_I(inode)->i_data_sem));
1760
1761 return retval;
1762}
1763
1764/*
Seunghun Leed91bd2c2014-09-01 22:15:30 -04001765 * This is a special get_block_t callback which is used by
Theodore Ts'ob920c752009-05-14 00:54:29 -04001766 * ext4_da_write_begin(). It will either return mapped block or
1767 * reserve space for a single block.
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001768 *
1769 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
1770 * We also have b_blocknr = -1 and b_bdev initialized properly
1771 *
1772 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
1773 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
1774 * initialized properly.
Alex Tomas64769242008-07-11 19:27:31 -04001775 */
Tao Ma9c3569b2012-12-10 14:05:57 -05001776int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
1777 struct buffer_head *bh, int create)
Alex Tomas64769242008-07-11 19:27:31 -04001778{
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001779 struct ext4_map_blocks map;
Alex Tomas64769242008-07-11 19:27:31 -04001780 int ret = 0;
1781
1782 BUG_ON(create == 0);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001783 BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
1784
1785 map.m_lblk = iblock;
1786 map.m_len = 1;
Alex Tomas64769242008-07-11 19:27:31 -04001787
1788 /*
1789 * first, we need to know whether the block is allocated already
1790 * preallocated blocks are unmapped but should treated
1791 * the same as allocated blocks.
1792 */
Aditya Kali5356f262011-09-09 19:20:51 -04001793 ret = ext4_da_map_blocks(inode, iblock, &map, bh);
1794 if (ret <= 0)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001795 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04001796
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001797 map_bh(bh, inode->i_sb, map.m_pblk);
Jan Karaed8ad832016-02-19 00:18:25 -05001798 ext4_update_bh_state(bh, map.m_flags);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001799
1800 if (buffer_unwritten(bh)) {
1801 /* A delayed write to unwritten bh should be marked
1802 * new and mapped. Mapped ensures that we don't do
1803 * get_block multiple times when we write to the same
1804 * offset and new ensures that we do proper zero out
1805 * for partial write.
1806 */
1807 set_buffer_new(bh);
Theodore Ts'oc8205632011-04-10 22:30:07 -04001808 set_buffer_mapped(bh);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001809 }
1810 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04001811}
Mingming Cao61628a32008-07-11 19:27:31 -04001812
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001813static int bget_one(handle_t *handle, struct buffer_head *bh)
1814{
1815 get_bh(bh);
1816 return 0;
1817}
1818
1819static int bput_one(handle_t *handle, struct buffer_head *bh)
1820{
1821 put_bh(bh);
1822 return 0;
1823}
1824
1825static int __ext4_journalled_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001826 unsigned int len)
1827{
1828 struct address_space *mapping = page->mapping;
1829 struct inode *inode = mapping->host;
Tao Ma3fdcfb62012-12-10 14:05:57 -05001830 struct buffer_head *page_bufs = NULL;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001831 handle_t *handle = NULL;
Tao Ma3fdcfb62012-12-10 14:05:57 -05001832 int ret = 0, err = 0;
1833 int inline_data = ext4_has_inline_data(inode);
1834 struct buffer_head *inode_bh = NULL;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001835
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001836 ClearPageChecked(page);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001837
1838 if (inline_data) {
1839 BUG_ON(page->index != 0);
1840 BUG_ON(len > ext4_get_max_inline_size(inode));
1841 inode_bh = ext4_journalled_write_inline_data(inode, len, page);
1842 if (inode_bh == NULL)
1843 goto out;
1844 } else {
1845 page_bufs = page_buffers(page);
1846 if (!page_bufs) {
1847 BUG();
1848 goto out;
1849 }
1850 ext4_walk_page_buffers(handle, page_bufs, 0, len,
1851 NULL, bget_one);
1852 }
Theodore Ts'obdf96832015-06-12 23:45:33 -04001853 /*
1854 * We need to release the page lock before we start the
1855 * journal, so grab a reference so the page won't disappear
1856 * out from under us.
1857 */
1858 get_page(page);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001859 unlock_page(page);
1860
Theodore Ts'o9924a922013-02-08 21:59:22 -05001861 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
1862 ext4_writepage_trans_blocks(inode));
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001863 if (IS_ERR(handle)) {
1864 ret = PTR_ERR(handle);
Theodore Ts'obdf96832015-06-12 23:45:33 -04001865 put_page(page);
1866 goto out_no_pagelock;
1867 }
1868 BUG_ON(!ext4_handle_valid(handle));
1869
1870 lock_page(page);
1871 put_page(page);
1872 if (page->mapping != mapping) {
1873 /* The page got truncated from under us */
1874 ext4_journal_stop(handle);
1875 ret = 0;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001876 goto out;
1877 }
1878
Tao Ma3fdcfb62012-12-10 14:05:57 -05001879 if (inline_data) {
liang xie5d601252014-05-12 22:06:43 -04001880 BUFFER_TRACE(inode_bh, "get write access");
Tao Ma3fdcfb62012-12-10 14:05:57 -05001881 ret = ext4_journal_get_write_access(handle, inode_bh);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001882
Tao Ma3fdcfb62012-12-10 14:05:57 -05001883 err = ext4_handle_dirty_metadata(handle, inode, inode_bh);
1884
1885 } else {
1886 ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
1887 do_journal_get_write_access);
1888
1889 err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
1890 write_end_fn);
1891 }
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001892 if (ret == 0)
1893 ret = err;
Jan Kara2d859db2011-07-26 09:07:11 -04001894 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001895 err = ext4_journal_stop(handle);
1896 if (!ret)
1897 ret = err;
1898
Tao Ma3fdcfb62012-12-10 14:05:57 -05001899 if (!ext4_has_inline_data(inode))
Theodore Ts'o8c9367f2014-01-07 13:08:03 -05001900 ext4_walk_page_buffers(NULL, page_bufs, 0, len,
Tao Ma3fdcfb62012-12-10 14:05:57 -05001901 NULL, bput_one);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001902 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001903out:
Theodore Ts'obdf96832015-06-12 23:45:33 -04001904 unlock_page(page);
1905out_no_pagelock:
Tao Ma3fdcfb62012-12-10 14:05:57 -05001906 brelse(inode_bh);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001907 return ret;
1908}
1909
Mingming Cao61628a32008-07-11 19:27:31 -04001910/*
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001911 * Note that we don't need to start a transaction unless we're journaling data
1912 * because we should have holes filled from ext4_page_mkwrite(). We even don't
1913 * need to file the inode to the transaction's list in ordered mode because if
1914 * we are writing back data added by write(), the inode is already there and if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001915 * we are writing back data modified via mmap(), no one guarantees in which
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001916 * transaction the data will hit the disk. In case we are journaling data, we
1917 * cannot start transaction directly because transaction start ranks above page
1918 * lock so we have to do some magic.
1919 *
Theodore Ts'ob920c752009-05-14 00:54:29 -04001920 * This function can get called via...
Theodore Ts'o20970ba2013-06-06 14:00:46 -04001921 * - ext4_writepages after taking page lock (have journal handle)
Theodore Ts'ob920c752009-05-14 00:54:29 -04001922 * - journal_submit_inode_data_buffers (no journal handle)
Artem Bityutskiyf6463b02012-07-25 18:12:04 +03001923 * - shrink_page_list via the kswapd/direct reclaim (no journal handle)
Theodore Ts'ob920c752009-05-14 00:54:29 -04001924 * - grab_page_cache when doing write_begin (have journal handle)
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001925 *
1926 * We don't do any block allocation in this function. If we have page with
1927 * multiple blocks we need to write those buffer_heads that are mapped. This
1928 * is important for mmaped based write. So if we do with blocksize 1K
1929 * truncate(f, 1024);
1930 * a = mmap(f, 0, 4096);
1931 * a[0] = 'a';
1932 * truncate(f, 4096);
1933 * we have in the page first buffer_head mapped via page_mkwrite call back
Paul Bolle90802ed2011-12-05 13:00:34 +01001934 * but other buffer_heads would be unmapped but dirty (dirty done via the
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001935 * do_wp_page). So writepage should write the first block. If we modify
1936 * the mmap area beyond 1024 we will again get a page_fault and the
1937 * page_mkwrite callback will do the block allocation and mark the
1938 * buffer_heads mapped.
1939 *
1940 * We redirty the page if we have any buffer_heads that is either delay or
1941 * unwritten in the page.
1942 *
1943 * We can get recursively called as show below.
1944 *
1945 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
1946 * ext4_writepage()
1947 *
1948 * But since we don't do any block allocation we should not deadlock.
1949 * Page also have the dirty flag cleared so we don't get recurive page_lock.
Mingming Cao61628a32008-07-11 19:27:31 -04001950 */
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001951static int ext4_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001952 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04001953{
Jan Karaf8bec372013-01-28 12:55:08 -05001954 int ret = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04001955 loff_t size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001956 unsigned int len;
Jiaying Zhang744692d2010-03-04 16:14:02 -05001957 struct buffer_head *page_bufs = NULL;
Mingming Cao61628a32008-07-11 19:27:31 -04001958 struct inode *inode = page->mapping->host;
Jan Kara36ade452013-01-28 09:30:52 -05001959 struct ext4_io_submit io_submit;
Namjae Jeon1c8349a2014-05-12 08:12:25 -04001960 bool keep_towrite = false;
Alex Tomas64769242008-07-11 19:27:31 -04001961
Lukas Czernera9c667f2011-06-06 09:51:52 -04001962 trace_ext4_writepage(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04001963 size = i_size_read(inode);
1964 if (page->index == size >> PAGE_CACHE_SHIFT)
1965 len = size & ~PAGE_CACHE_MASK;
1966 else
1967 len = PAGE_CACHE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04001968
Theodore Ts'oa42afc52010-10-27 21:30:09 -04001969 page_bufs = page_buffers(page);
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001970 /*
Jan Karafe386132013-01-28 21:06:42 -05001971 * We cannot do block allocation or other extent handling in this
1972 * function. If there are buffers needing that, we have to redirty
1973 * the page. But we may reach here when we do a journal commit via
1974 * journal_submit_inode_data_buffers() and in that case we must write
1975 * allocated buffers to achieve data=ordered mode guarantees.
Theodore Ts'occcd1472015-10-03 10:49:23 -04001976 *
1977 * Also, if there is only one buffer per page (the fs block
1978 * size == the page size), if one buffer needs block
1979 * allocation or needs to modify the extent tree to clear the
1980 * unwritten flag, we know that the page can't be written at
1981 * all, so we might as well refuse the write immediately.
1982 * Unfortunately if the block size != page size, we can't as
1983 * easily detect this case using ext4_walk_page_buffers(), but
1984 * for the extremely common case, this is an optimization that
1985 * skips a useless round trip through ext4_bio_write_page().
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04001986 */
Tao Maf19d5872012-12-10 14:05:51 -05001987 if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL,
1988 ext4_bh_delay_or_unwritten)) {
Jan Karaf8bec372013-01-28 12:55:08 -05001989 redirty_page_for_writepage(wbc, page);
Theodore Ts'occcd1472015-10-03 10:49:23 -04001990 if ((current->flags & PF_MEMALLOC) ||
1991 (inode->i_sb->s_blocksize == PAGE_CACHE_SIZE)) {
Jan Karafe386132013-01-28 21:06:42 -05001992 /*
1993 * For memory cleaning there's no point in writing only
1994 * some buffers. So just bail out. Warn if we came here
1995 * from direct reclaim.
1996 */
1997 WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD))
1998 == PF_MEMALLOC);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04001999 unlock_page(page);
2000 return 0;
2001 }
Namjae Jeon1c8349a2014-05-12 08:12:25 -04002002 keep_towrite = true;
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002003 }
Alex Tomas64769242008-07-11 19:27:31 -04002004
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002005 if (PageChecked(page) && ext4_should_journal_data(inode))
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002006 /*
2007 * It's mmapped pagecache. Add buffers and journal it. There
2008 * doesn't seem much point in redirtying the page here.
2009 */
Wu Fengguang3f0ca302009-11-24 11:15:44 -05002010 return __ext4_journalled_writepage(page, len);
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002011
Jan Kara97a851e2013-06-04 11:58:58 -04002012 ext4_io_submit_init(&io_submit, wbc);
2013 io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS);
2014 if (!io_submit.io_end) {
2015 redirty_page_for_writepage(wbc, page);
2016 unlock_page(page);
2017 return -ENOMEM;
2018 }
Namjae Jeon1c8349a2014-05-12 08:12:25 -04002019 ret = ext4_bio_write_page(&io_submit, page, len, wbc, keep_towrite);
Jan Kara36ade452013-01-28 09:30:52 -05002020 ext4_io_submit(&io_submit);
Jan Kara97a851e2013-06-04 11:58:58 -04002021 /* Drop io_end reference we got from init */
2022 ext4_put_io_end_defer(io_submit.io_end);
Alex Tomas64769242008-07-11 19:27:31 -04002023 return ret;
2024}
2025
Jan Kara5f1132b2013-08-17 10:02:33 -04002026static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page)
2027{
2028 int len;
2029 loff_t size = i_size_read(mpd->inode);
2030 int err;
2031
2032 BUG_ON(page->index != mpd->first_page);
2033 if (page->index == size >> PAGE_CACHE_SHIFT)
2034 len = size & ~PAGE_CACHE_MASK;
2035 else
2036 len = PAGE_CACHE_SIZE;
2037 clear_page_dirty_for_io(page);
Namjae Jeon1c8349a2014-05-12 08:12:25 -04002038 err = ext4_bio_write_page(&mpd->io_submit, page, len, mpd->wbc, false);
Jan Kara5f1132b2013-08-17 10:02:33 -04002039 if (!err)
2040 mpd->wbc->nr_to_write--;
2041 mpd->first_page++;
2042
2043 return err;
2044}
2045
Jan Kara4e7ea812013-06-04 13:17:40 -04002046#define BH_FLAGS ((1 << BH_Unwritten) | (1 << BH_Delay))
2047
Mingming Cao61628a32008-07-11 19:27:31 -04002048/*
Jan Karafffb2732013-06-04 13:01:11 -04002049 * mballoc gives us at most this number of blocks...
2050 * XXX: That seems to be only a limitation of ext4_mb_normalize_request().
Anatol Pomozov70261f52013-08-28 14:40:12 -04002051 * The rest of mballoc seems to handle chunks up to full group size.
Mingming Cao61628a32008-07-11 19:27:31 -04002052 */
Jan Karafffb2732013-06-04 13:01:11 -04002053#define MAX_WRITEPAGES_EXTENT_LEN 2048
Mingming Cao525f4ed2008-08-19 22:15:58 -04002054
Jan Karafffb2732013-06-04 13:01:11 -04002055/*
Jan Kara4e7ea812013-06-04 13:17:40 -04002056 * mpage_add_bh_to_extent - try to add bh to extent of blocks to map
2057 *
2058 * @mpd - extent of blocks
2059 * @lblk - logical number of the block in the file
Jan Kara09930042013-08-17 09:57:56 -04002060 * @bh - buffer head we want to add to the extent
Jan Kara4e7ea812013-06-04 13:17:40 -04002061 *
Jan Kara09930042013-08-17 09:57:56 -04002062 * The function is used to collect contig. blocks in the same state. If the
2063 * buffer doesn't require mapping for writeback and we haven't started the
2064 * extent of buffers to map yet, the function returns 'true' immediately - the
2065 * caller can write the buffer right away. Otherwise the function returns true
2066 * if the block has been added to the extent, false if the block couldn't be
2067 * added.
Jan Kara4e7ea812013-06-04 13:17:40 -04002068 */
Jan Kara09930042013-08-17 09:57:56 -04002069static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk,
2070 struct buffer_head *bh)
Jan Kara4e7ea812013-06-04 13:17:40 -04002071{
2072 struct ext4_map_blocks *map = &mpd->map;
2073
Jan Kara09930042013-08-17 09:57:56 -04002074 /* Buffer that doesn't need mapping for writeback? */
2075 if (!buffer_dirty(bh) || !buffer_mapped(bh) ||
2076 (!buffer_delay(bh) && !buffer_unwritten(bh))) {
2077 /* So far no extent to map => we write the buffer right away */
2078 if (map->m_len == 0)
2079 return true;
2080 return false;
2081 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002082
2083 /* First block in the extent? */
2084 if (map->m_len == 0) {
2085 map->m_lblk = lblk;
2086 map->m_len = 1;
Jan Kara09930042013-08-17 09:57:56 -04002087 map->m_flags = bh->b_state & BH_FLAGS;
2088 return true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002089 }
2090
Jan Kara09930042013-08-17 09:57:56 -04002091 /* Don't go larger than mballoc is willing to allocate */
2092 if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN)
2093 return false;
2094
Jan Kara4e7ea812013-06-04 13:17:40 -04002095 /* Can we merge the block to our big extent? */
2096 if (lblk == map->m_lblk + map->m_len &&
Jan Kara09930042013-08-17 09:57:56 -04002097 (bh->b_state & BH_FLAGS) == map->m_flags) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002098 map->m_len++;
Jan Kara09930042013-08-17 09:57:56 -04002099 return true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002100 }
Jan Kara09930042013-08-17 09:57:56 -04002101 return false;
Jan Kara4e7ea812013-06-04 13:17:40 -04002102}
2103
Jan Kara5f1132b2013-08-17 10:02:33 -04002104/*
2105 * mpage_process_page_bufs - submit page buffers for IO or add them to extent
2106 *
2107 * @mpd - extent of blocks for mapping
2108 * @head - the first buffer in the page
2109 * @bh - buffer we should start processing from
2110 * @lblk - logical number of the block in the file corresponding to @bh
2111 *
2112 * Walk through page buffers from @bh upto @head (exclusive) and either submit
2113 * the page for IO if all buffers in this page were mapped and there's no
2114 * accumulated extent of buffers to map or add buffers in the page to the
2115 * extent of buffers to map. The function returns 1 if the caller can continue
2116 * by processing the next page, 0 if it should stop adding buffers to the
2117 * extent to map because we cannot extend it anymore. It can also return value
2118 * < 0 in case of error during IO submission.
2119 */
2120static int mpage_process_page_bufs(struct mpage_da_data *mpd,
2121 struct buffer_head *head,
2122 struct buffer_head *bh,
2123 ext4_lblk_t lblk)
Jan Kara4e7ea812013-06-04 13:17:40 -04002124{
2125 struct inode *inode = mpd->inode;
Jan Kara5f1132b2013-08-17 10:02:33 -04002126 int err;
Jan Kara4e7ea812013-06-04 13:17:40 -04002127 ext4_lblk_t blocks = (i_size_read(inode) + (1 << inode->i_blkbits) - 1)
2128 >> inode->i_blkbits;
2129
2130 do {
2131 BUG_ON(buffer_locked(bh));
2132
Jan Kara09930042013-08-17 09:57:56 -04002133 if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002134 /* Found extent to map? */
2135 if (mpd->map.m_len)
Jan Kara5f1132b2013-08-17 10:02:33 -04002136 return 0;
Jan Kara09930042013-08-17 09:57:56 -04002137 /* Everything mapped so far and we hit EOF */
Jan Kara5f1132b2013-08-17 10:02:33 -04002138 break;
Jan Kara4e7ea812013-06-04 13:17:40 -04002139 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002140 } while (lblk++, (bh = bh->b_this_page) != head);
Jan Kara5f1132b2013-08-17 10:02:33 -04002141 /* So far everything mapped? Submit the page for IO. */
2142 if (mpd->map.m_len == 0) {
2143 err = mpage_submit_page(mpd, head->b_page);
2144 if (err < 0)
2145 return err;
2146 }
2147 return lblk < blocks;
Jan Kara4e7ea812013-06-04 13:17:40 -04002148}
2149
2150/*
2151 * mpage_map_buffers - update buffers corresponding to changed extent and
2152 * submit fully mapped pages for IO
2153 *
2154 * @mpd - description of extent to map, on return next extent to map
2155 *
2156 * Scan buffers corresponding to changed extent (we expect corresponding pages
2157 * to be already locked) and update buffer state according to new extent state.
2158 * We map delalloc buffers to their physical location, clear unwritten bits,
Lukas Czerner556615d2014-04-20 23:45:47 -04002159 * and mark buffers as uninit when we perform writes to unwritten extents
Jan Kara4e7ea812013-06-04 13:17:40 -04002160 * and do extent conversion after IO is finished. If the last page is not fully
2161 * mapped, we update @map to the next extent in the last page that needs
2162 * mapping. Otherwise we submit the page for IO.
2163 */
2164static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd)
2165{
2166 struct pagevec pvec;
2167 int nr_pages, i;
2168 struct inode *inode = mpd->inode;
2169 struct buffer_head *head, *bh;
2170 int bpp_bits = PAGE_CACHE_SHIFT - inode->i_blkbits;
Jan Kara4e7ea812013-06-04 13:17:40 -04002171 pgoff_t start, end;
2172 ext4_lblk_t lblk;
2173 sector_t pblock;
2174 int err;
2175
2176 start = mpd->map.m_lblk >> bpp_bits;
2177 end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits;
2178 lblk = start << bpp_bits;
2179 pblock = mpd->map.m_pblk;
2180
2181 pagevec_init(&pvec, 0);
2182 while (start <= end) {
2183 nr_pages = pagevec_lookup(&pvec, inode->i_mapping, start,
2184 PAGEVEC_SIZE);
2185 if (nr_pages == 0)
2186 break;
2187 for (i = 0; i < nr_pages; i++) {
2188 struct page *page = pvec.pages[i];
2189
2190 if (page->index > end)
2191 break;
Anatol Pomozov70261f52013-08-28 14:40:12 -04002192 /* Up to 'end' pages must be contiguous */
Jan Kara4e7ea812013-06-04 13:17:40 -04002193 BUG_ON(page->index != start);
2194 bh = head = page_buffers(page);
2195 do {
2196 if (lblk < mpd->map.m_lblk)
2197 continue;
2198 if (lblk >= mpd->map.m_lblk + mpd->map.m_len) {
2199 /*
2200 * Buffer after end of mapped extent.
2201 * Find next buffer in the page to map.
2202 */
2203 mpd->map.m_len = 0;
2204 mpd->map.m_flags = 0;
Jan Kara5f1132b2013-08-17 10:02:33 -04002205 /*
2206 * FIXME: If dioread_nolock supports
2207 * blocksize < pagesize, we need to make
2208 * sure we add size mapped so far to
2209 * io_end->size as the following call
2210 * can submit the page for IO.
2211 */
2212 err = mpage_process_page_bufs(mpd, head,
2213 bh, lblk);
Jan Kara4e7ea812013-06-04 13:17:40 -04002214 pagevec_release(&pvec);
Jan Kara5f1132b2013-08-17 10:02:33 -04002215 if (err > 0)
2216 err = 0;
2217 return err;
Jan Kara4e7ea812013-06-04 13:17:40 -04002218 }
2219 if (buffer_delay(bh)) {
2220 clear_buffer_delay(bh);
2221 bh->b_blocknr = pblock++;
2222 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002223 clear_buffer_unwritten(bh);
Jan Kara5f1132b2013-08-17 10:02:33 -04002224 } while (lblk++, (bh = bh->b_this_page) != head);
Jan Kara4e7ea812013-06-04 13:17:40 -04002225
2226 /*
2227 * FIXME: This is going to break if dioread_nolock
2228 * supports blocksize < pagesize as we will try to
2229 * convert potentially unmapped parts of inode.
2230 */
2231 mpd->io_submit.io_end->size += PAGE_CACHE_SIZE;
2232 /* Page fully mapped - let IO run! */
2233 err = mpage_submit_page(mpd, page);
2234 if (err < 0) {
2235 pagevec_release(&pvec);
2236 return err;
2237 }
2238 start++;
2239 }
2240 pagevec_release(&pvec);
2241 }
2242 /* Extent fully mapped and matches with page boundary. We are done. */
2243 mpd->map.m_len = 0;
2244 mpd->map.m_flags = 0;
2245 return 0;
2246}
2247
2248static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd)
2249{
2250 struct inode *inode = mpd->inode;
2251 struct ext4_map_blocks *map = &mpd->map;
2252 int get_blocks_flags;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002253 int err, dioread_nolock;
Jan Kara4e7ea812013-06-04 13:17:40 -04002254
2255 trace_ext4_da_write_pages_extent(inode, map);
2256 /*
2257 * Call ext4_map_blocks() to allocate any delayed allocation blocks, or
Lukas Czerner556615d2014-04-20 23:45:47 -04002258 * to convert an unwritten extent to be initialized (in the case
Jan Kara4e7ea812013-06-04 13:17:40 -04002259 * where we have written into one or more preallocated blocks). It is
2260 * possible that we're going to need more metadata blocks than
2261 * previously reserved. However we must not fail because we're in
2262 * writeback and there is nothing we can do about it so it might result
2263 * in data loss. So use reserved blocks to allocate metadata if
2264 * possible.
2265 *
Theodore Ts'o754cfed2014-09-04 18:08:22 -04002266 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if
2267 * the blocks in question are delalloc blocks. This indicates
2268 * that the blocks and quotas has already been checked when
2269 * the data was copied into the page cache.
Jan Kara4e7ea812013-06-04 13:17:40 -04002270 */
2271 get_blocks_flags = EXT4_GET_BLOCKS_CREATE |
2272 EXT4_GET_BLOCKS_METADATA_NOFAIL;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002273 dioread_nolock = ext4_should_dioread_nolock(inode);
2274 if (dioread_nolock)
Jan Kara4e7ea812013-06-04 13:17:40 -04002275 get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
2276 if (map->m_flags & (1 << BH_Delay))
2277 get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
2278
2279 err = ext4_map_blocks(handle, inode, map, get_blocks_flags);
2280 if (err < 0)
2281 return err;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002282 if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) {
Jan Kara6b523df2013-06-04 13:21:11 -04002283 if (!mpd->io_submit.io_end->handle &&
2284 ext4_handle_valid(handle)) {
2285 mpd->io_submit.io_end->handle = handle->h_rsv_handle;
2286 handle->h_rsv_handle = NULL;
2287 }
Jan Kara3613d222013-06-04 13:19:34 -04002288 ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end);
Jan Kara6b523df2013-06-04 13:21:11 -04002289 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002290
2291 BUG_ON(map->m_len == 0);
2292 if (map->m_flags & EXT4_MAP_NEW) {
2293 struct block_device *bdev = inode->i_sb->s_bdev;
2294 int i;
2295
2296 for (i = 0; i < map->m_len; i++)
2297 unmap_underlying_metadata(bdev, map->m_pblk + i);
2298 }
2299 return 0;
2300}
2301
2302/*
2303 * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length
2304 * mpd->len and submit pages underlying it for IO
2305 *
2306 * @handle - handle for journal operations
2307 * @mpd - extent to map
Jan Kara7534e852013-10-16 08:26:08 -04002308 * @give_up_on_write - we set this to true iff there is a fatal error and there
2309 * is no hope of writing the data. The caller should discard
2310 * dirty pages to avoid infinite loops.
Jan Kara4e7ea812013-06-04 13:17:40 -04002311 *
2312 * The function maps extent starting at mpd->lblk of length mpd->len. If it is
2313 * delayed, blocks are allocated, if it is unwritten, we may need to convert
2314 * them to initialized or split the described range from larger unwritten
2315 * extent. Note that we need not map all the described range since allocation
2316 * can return less blocks or the range is covered by more unwritten extents. We
2317 * cannot map more because we are limited by reserved transaction credits. On
2318 * the other hand we always make sure that the last touched page is fully
2319 * mapped so that it can be written out (and thus forward progress is
2320 * guaranteed). After mapping we submit all mapped pages for IO.
2321 */
2322static int mpage_map_and_submit_extent(handle_t *handle,
Theodore Ts'ocb530542013-07-01 08:12:40 -04002323 struct mpage_da_data *mpd,
2324 bool *give_up_on_write)
Jan Kara4e7ea812013-06-04 13:17:40 -04002325{
2326 struct inode *inode = mpd->inode;
2327 struct ext4_map_blocks *map = &mpd->map;
2328 int err;
2329 loff_t disksize;
Dmitry Monakhov66031202014-08-27 18:40:03 -04002330 int progress = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002331
2332 mpd->io_submit.io_end->offset =
2333 ((loff_t)map->m_lblk) << inode->i_blkbits;
Jan Kara27d7c4e2013-07-05 21:57:22 -04002334 do {
Jan Kara4e7ea812013-06-04 13:17:40 -04002335 err = mpage_map_one_extent(handle, mpd);
2336 if (err < 0) {
2337 struct super_block *sb = inode->i_sb;
2338
Theodore Ts'ocb530542013-07-01 08:12:40 -04002339 if (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)
2340 goto invalidate_dirty_pages;
Jan Kara4e7ea812013-06-04 13:17:40 -04002341 /*
Theodore Ts'ocb530542013-07-01 08:12:40 -04002342 * Let the uper layers retry transient errors.
2343 * In the case of ENOSPC, if ext4_count_free_blocks()
2344 * is non-zero, a commit should free up blocks.
Jan Kara4e7ea812013-06-04 13:17:40 -04002345 */
Theodore Ts'ocb530542013-07-01 08:12:40 -04002346 if ((err == -ENOMEM) ||
Dmitry Monakhov66031202014-08-27 18:40:03 -04002347 (err == -ENOSPC && ext4_count_free_clusters(sb))) {
2348 if (progress)
2349 goto update_disksize;
Theodore Ts'ocb530542013-07-01 08:12:40 -04002350 return err;
Dmitry Monakhov66031202014-08-27 18:40:03 -04002351 }
Theodore Ts'ocb530542013-07-01 08:12:40 -04002352 ext4_msg(sb, KERN_CRIT,
2353 "Delayed block allocation failed for "
2354 "inode %lu at logical offset %llu with"
2355 " max blocks %u with error %d",
2356 inode->i_ino,
2357 (unsigned long long)map->m_lblk,
2358 (unsigned)map->m_len, -err);
2359 ext4_msg(sb, KERN_CRIT,
2360 "This should not happen!! Data will "
2361 "be lost\n");
2362 if (err == -ENOSPC)
2363 ext4_print_free_blocks(inode);
2364 invalidate_dirty_pages:
2365 *give_up_on_write = true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002366 return err;
2367 }
Dmitry Monakhov66031202014-08-27 18:40:03 -04002368 progress = 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04002369 /*
2370 * Update buffer state, submit mapped pages, and get us new
2371 * extent to map
2372 */
2373 err = mpage_map_and_submit_buffers(mpd);
2374 if (err < 0)
Dmitry Monakhov66031202014-08-27 18:40:03 -04002375 goto update_disksize;
Jan Kara27d7c4e2013-07-05 21:57:22 -04002376 } while (map->m_len);
Jan Kara4e7ea812013-06-04 13:17:40 -04002377
Dmitry Monakhov66031202014-08-27 18:40:03 -04002378update_disksize:
Theodore Ts'o622cad12014-04-11 10:35:17 -04002379 /*
2380 * Update on-disk size after IO is submitted. Races with
2381 * truncate are avoided by checking i_size under i_data_sem.
2382 */
Jan Kara4e7ea812013-06-04 13:17:40 -04002383 disksize = ((loff_t)mpd->first_page) << PAGE_CACHE_SHIFT;
Jan Kara4e7ea812013-06-04 13:17:40 -04002384 if (disksize > EXT4_I(inode)->i_disksize) {
2385 int err2;
Theodore Ts'o622cad12014-04-11 10:35:17 -04002386 loff_t i_size;
Jan Kara4e7ea812013-06-04 13:17:40 -04002387
Theodore Ts'o622cad12014-04-11 10:35:17 -04002388 down_write(&EXT4_I(inode)->i_data_sem);
2389 i_size = i_size_read(inode);
2390 if (disksize > i_size)
2391 disksize = i_size;
2392 if (disksize > EXT4_I(inode)->i_disksize)
2393 EXT4_I(inode)->i_disksize = disksize;
Jan Kara4e7ea812013-06-04 13:17:40 -04002394 err2 = ext4_mark_inode_dirty(handle, inode);
Theodore Ts'o622cad12014-04-11 10:35:17 -04002395 up_write(&EXT4_I(inode)->i_data_sem);
Jan Kara4e7ea812013-06-04 13:17:40 -04002396 if (err2)
2397 ext4_error(inode->i_sb,
2398 "Failed to mark inode %lu dirty",
2399 inode->i_ino);
2400 if (!err)
2401 err = err2;
2402 }
2403 return err;
2404}
2405
2406/*
Jan Karafffb2732013-06-04 13:01:11 -04002407 * Calculate the total number of credits to reserve for one writepages
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002408 * iteration. This is called from ext4_writepages(). We map an extent of
Anatol Pomozov70261f52013-08-28 14:40:12 -04002409 * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping
Jan Karafffb2732013-06-04 13:01:11 -04002410 * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN +
2411 * bpp - 1 blocks in bpp different extents.
2412 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002413static int ext4_da_writepages_trans_blocks(struct inode *inode)
2414{
Jan Karafffb2732013-06-04 13:01:11 -04002415 int bpp = ext4_journal_blocks_per_page(inode);
Mingming Cao525f4ed2008-08-19 22:15:58 -04002416
Jan Karafffb2732013-06-04 13:01:11 -04002417 return ext4_meta_trans_blocks(inode,
2418 MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp);
Mingming Cao525f4ed2008-08-19 22:15:58 -04002419}
Mingming Cao61628a32008-07-11 19:27:31 -04002420
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002421/*
Jan Kara4e7ea812013-06-04 13:17:40 -04002422 * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages
2423 * and underlying extent to map
2424 *
2425 * @mpd - where to look for pages
2426 *
2427 * Walk dirty pages in the mapping. If they are fully mapped, submit them for
2428 * IO immediately. When we find a page which isn't mapped we start accumulating
2429 * extent of buffers underlying these pages that needs mapping (formed by
2430 * either delayed or unwritten buffers). We also lock the pages containing
2431 * these buffers. The extent found is returned in @mpd structure (starting at
2432 * mpd->lblk with length mpd->len blocks).
2433 *
2434 * Note that this function can attach bios to one io_end structure which are
2435 * neither logically nor physically contiguous. Although it may seem as an
2436 * unnecessary complication, it is actually inevitable in blocksize < pagesize
2437 * 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 -04002438 */
Jan Kara4e7ea812013-06-04 13:17:40 -04002439static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd)
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002440{
Jan Kara4e7ea812013-06-04 13:17:40 -04002441 struct address_space *mapping = mpd->inode->i_mapping;
2442 struct pagevec pvec;
2443 unsigned int nr_pages;
Ming Leiaeac5892013-10-17 18:56:16 -04002444 long left = mpd->wbc->nr_to_write;
Jan Kara4e7ea812013-06-04 13:17:40 -04002445 pgoff_t index = mpd->first_page;
2446 pgoff_t end = mpd->last_page;
2447 int tag;
2448 int i, err = 0;
2449 int blkbits = mpd->inode->i_blkbits;
2450 ext4_lblk_t lblk;
2451 struct buffer_head *head;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002452
Jan Kara4e7ea812013-06-04 13:17:40 -04002453 if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages)
Eric Sandeen5b41d922010-10-27 21:30:13 -04002454 tag = PAGECACHE_TAG_TOWRITE;
2455 else
2456 tag = PAGECACHE_TAG_DIRTY;
2457
Jan Kara4e7ea812013-06-04 13:17:40 -04002458 pagevec_init(&pvec, 0);
2459 mpd->map.m_len = 0;
2460 mpd->next_page = index;
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002461 while (index <= end) {
Eric Sandeen5b41d922010-10-27 21:30:13 -04002462 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002463 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
2464 if (nr_pages == 0)
Jan Kara4e7ea812013-06-04 13:17:40 -04002465 goto out;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002466
2467 for (i = 0; i < nr_pages; i++) {
2468 struct page *page = pvec.pages[i];
2469
2470 /*
2471 * At this point, the page may be truncated or
2472 * invalidated (changing page->mapping to NULL), or
2473 * even swizzled back from swapper_space to tmpfs file
2474 * mapping. However, page->index will not change
2475 * because we have a reference on the page.
2476 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002477 if (page->index > end)
2478 goto out;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002479
Ming Leiaeac5892013-10-17 18:56:16 -04002480 /*
2481 * Accumulated enough dirty pages? This doesn't apply
2482 * to WB_SYNC_ALL mode. For integrity sync we have to
2483 * keep going because someone may be concurrently
2484 * dirtying pages, and we might have synced a lot of
2485 * newly appeared dirty pages, but have not synced all
2486 * of the old dirty pages.
2487 */
2488 if (mpd->wbc->sync_mode == WB_SYNC_NONE && left <= 0)
2489 goto out;
2490
Jan Kara4e7ea812013-06-04 13:17:40 -04002491 /* If we can't merge this page, we are done. */
2492 if (mpd->map.m_len > 0 && mpd->next_page != page->index)
2493 goto out;
Theodore Ts'o78aaced2011-02-26 14:09:14 -05002494
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002495 lock_page(page);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002496 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002497 * If the page is no longer dirty, or its mapping no
2498 * longer corresponds to inode we are writing (which
2499 * means it has been truncated or invalidated), or the
2500 * page is already under writeback and we are not doing
2501 * a data integrity writeback, skip the page
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002502 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002503 if (!PageDirty(page) ||
2504 (PageWriteback(page) &&
Jan Kara4e7ea812013-06-04 13:17:40 -04002505 (mpd->wbc->sync_mode == WB_SYNC_NONE)) ||
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002506 unlikely(page->mapping != mapping)) {
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002507 unlock_page(page);
2508 continue;
2509 }
2510
Darrick J. Wong7cb1a532011-05-18 13:53:20 -04002511 wait_on_page_writeback(page);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002512 BUG_ON(PageWriteback(page));
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002513
Jan Kara4e7ea812013-06-04 13:17:40 -04002514 if (mpd->map.m_len == 0)
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002515 mpd->first_page = page->index;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002516 mpd->next_page = page->index + 1;
Jan Karaf8bec372013-01-28 12:55:08 -05002517 /* Add all dirty buffers to mpd */
Jan Kara4e7ea812013-06-04 13:17:40 -04002518 lblk = ((ext4_lblk_t)page->index) <<
2519 (PAGE_CACHE_SHIFT - blkbits);
Jan Karaf8bec372013-01-28 12:55:08 -05002520 head = page_buffers(page);
Jan Kara5f1132b2013-08-17 10:02:33 -04002521 err = mpage_process_page_bufs(mpd, head, head, lblk);
2522 if (err <= 0)
Jan Kara4e7ea812013-06-04 13:17:40 -04002523 goto out;
Jan Kara5f1132b2013-08-17 10:02:33 -04002524 err = 0;
Ming Leiaeac5892013-10-17 18:56:16 -04002525 left--;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002526 }
2527 pagevec_release(&pvec);
2528 cond_resched();
2529 }
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002530 return 0;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002531out:
2532 pagevec_release(&pvec);
Jan Kara4e7ea812013-06-04 13:17:40 -04002533 return err;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002534}
2535
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002536static int __writepage(struct page *page, struct writeback_control *wbc,
2537 void *data)
2538{
2539 struct address_space *mapping = data;
2540 int ret = ext4_writepage(page, wbc);
2541 mapping_set_error(mapping, ret);
2542 return ret;
2543}
2544
2545static int ext4_writepages(struct address_space *mapping,
2546 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002547{
Jan Kara4e7ea812013-06-04 13:17:40 -04002548 pgoff_t writeback_index = 0;
2549 long nr_to_write = wbc->nr_to_write;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002550 int range_whole = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002551 int cycled = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002552 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002553 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002554 struct inode *inode = mapping->host;
Jan Kara6b523df2013-06-04 13:21:11 -04002555 int needed_blocks, rsv_blocks = 0, ret = 0;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002556 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Jan Kara4e7ea812013-06-04 13:17:40 -04002557 bool done;
Shaohua Li1bce63d2011-10-18 10:55:51 -04002558 struct blk_plug plug;
Theodore Ts'ocb530542013-07-01 08:12:40 -04002559 bool give_up_on_write = false;
Mingming Cao61628a32008-07-11 19:27:31 -04002560
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002561 trace_ext4_writepages(inode, wbc);
Theodore Ts'oba80b102009-01-03 20:03:21 -05002562
Mingming Cao61628a32008-07-11 19:27:31 -04002563 /*
2564 * No pages to write? This is mainly a kludge to avoid starting
2565 * a transaction for special inodes like journal inode on last iput()
2566 * because that could violate lock ordering on umount
2567 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002568 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Ming Leibbf023c2013-10-30 07:27:16 -04002569 goto out_writepages;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002570
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002571 if (ext4_should_journal_data(inode)) {
2572 struct blk_plug plug;
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002573
2574 blk_start_plug(&plug);
2575 ret = write_cache_pages(mapping, wbc, __writepage, mapping);
2576 blk_finish_plug(&plug);
Ming Leibbf023c2013-10-30 07:27:16 -04002577 goto out_writepages;
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002578 }
2579
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002580 /*
2581 * If the filesystem has aborted, it is read-only, so return
2582 * right away instead of dumping stack traces later on that
2583 * will obscure the real source of the problem. We test
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002584 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002585 * the latter could be true if the filesystem is mounted
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002586 * read-only, and in that case, ext4_writepages should
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002587 * *never* be called, so if that ever happens, we would want
2588 * the stack trace.
2589 */
Ming Leibbf023c2013-10-30 07:27:16 -04002590 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) {
2591 ret = -EROFS;
2592 goto out_writepages;
2593 }
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002594
Jan Kara6b523df2013-06-04 13:21:11 -04002595 if (ext4_should_dioread_nolock(inode)) {
2596 /*
Anatol Pomozov70261f52013-08-28 14:40:12 -04002597 * We may need to convert up to one extent per block in
Jan Kara6b523df2013-06-04 13:21:11 -04002598 * the page and we may dirty the inode.
2599 */
2600 rsv_blocks = 1 + (PAGE_CACHE_SIZE >> inode->i_blkbits);
2601 }
2602
Jan Kara4e7ea812013-06-04 13:17:40 -04002603 /*
2604 * If we have inline data and arrive here, it means that
2605 * we will soon create the block for the 1st page, so
2606 * we'd better clear the inline data here.
2607 */
2608 if (ext4_has_inline_data(inode)) {
2609 /* Just inode will be modified... */
2610 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
2611 if (IS_ERR(handle)) {
2612 ret = PTR_ERR(handle);
2613 goto out_writepages;
2614 }
2615 BUG_ON(ext4_test_inode_state(inode,
2616 EXT4_STATE_MAY_INLINE_DATA));
2617 ext4_destroy_inline_data(handle, inode);
2618 ext4_journal_stop(handle);
2619 }
2620
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002621 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2622 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002623
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002624 if (wbc->range_cyclic) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002625 writeback_index = mapping->writeback_index;
2626 if (writeback_index)
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002627 cycled = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002628 mpd.first_page = writeback_index;
2629 mpd.last_page = -1;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002630 } else {
Jan Kara4e7ea812013-06-04 13:17:40 -04002631 mpd.first_page = wbc->range_start >> PAGE_CACHE_SHIFT;
2632 mpd.last_page = wbc->range_end >> PAGE_CACHE_SHIFT;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002633 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002634
Jan Kara4e7ea812013-06-04 13:17:40 -04002635 mpd.inode = inode;
2636 mpd.wbc = wbc;
2637 ext4_io_submit_init(&mpd.io_submit, wbc);
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002638retry:
Wu Fengguang6e6938b2010-06-06 10:38:15 -06002639 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Jan Kara4e7ea812013-06-04 13:17:40 -04002640 tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page);
2641 done = false;
Shaohua Li1bce63d2011-10-18 10:55:51 -04002642 blk_start_plug(&plug);
Jan Kara4e7ea812013-06-04 13:17:40 -04002643 while (!done && mpd.first_page <= mpd.last_page) {
2644 /* For each extent of pages we use new io_end */
2645 mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
2646 if (!mpd.io_submit.io_end) {
2647 ret = -ENOMEM;
2648 break;
2649 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002650
2651 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002652 * We have two constraints: We find one extent to map and we
2653 * must always write out whole page (makes a difference when
2654 * blocksize < pagesize) so that we don't block on IO when we
2655 * try to write out the rest of the page. Journalled mode is
2656 * not supported by delalloc.
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002657 */
2658 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04002659 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002660
Jan Kara4e7ea812013-06-04 13:17:40 -04002661 /* start a new transaction */
Jan Kara6b523df2013-06-04 13:21:11 -04002662 handle = ext4_journal_start_with_reserve(inode,
2663 EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks);
Mingming Cao61628a32008-07-11 19:27:31 -04002664 if (IS_ERR(handle)) {
2665 ret = PTR_ERR(handle);
Theodore Ts'o16939182009-09-26 17:43:59 -04002666 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
Curt Wohlgemuthfbe845d2010-05-16 13:00:00 -04002667 "%ld pages, ino %lu; err %d", __func__,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002668 wbc->nr_to_write, inode->i_ino, ret);
Jan Kara4e7ea812013-06-04 13:17:40 -04002669 /* Release allocated io_end */
2670 ext4_put_io_end(mpd.io_submit.io_end);
2671 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002672 }
Theodore Ts'of63e6002009-02-23 16:42:39 -05002673
Jan Kara4e7ea812013-06-04 13:17:40 -04002674 trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc);
2675 ret = mpage_prepare_extent_to_map(&mpd);
2676 if (!ret) {
2677 if (mpd.map.m_len)
Theodore Ts'ocb530542013-07-01 08:12:40 -04002678 ret = mpage_map_and_submit_extent(handle, &mpd,
2679 &give_up_on_write);
Jan Kara4e7ea812013-06-04 13:17:40 -04002680 else {
2681 /*
2682 * We scanned the whole range (or exhausted
2683 * nr_to_write), submitted what was mapped and
2684 * didn't find anything needing mapping. We are
2685 * done.
2686 */
2687 done = true;
2688 }
Theodore Ts'of63e6002009-02-23 16:42:39 -05002689 }
Mingming Cao61628a32008-07-11 19:27:31 -04002690 ext4_journal_stop(handle);
Jan Kara4e7ea812013-06-04 13:17:40 -04002691 /* Submit prepared bio */
2692 ext4_io_submit(&mpd.io_submit);
2693 /* Unlock pages we didn't use */
Theodore Ts'ocb530542013-07-01 08:12:40 -04002694 mpage_release_unused_pages(&mpd, give_up_on_write);
Jan Kara4e7ea812013-06-04 13:17:40 -04002695 /* Drop our io_end reference we got from init */
2696 ext4_put_io_end(mpd.io_submit.io_end);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002697
Jan Kara4e7ea812013-06-04 13:17:40 -04002698 if (ret == -ENOSPC && sbi->s_journal) {
2699 /*
2700 * Commit the transaction which would
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002701 * free blocks released in the transaction
2702 * and try again
2703 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002704 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002705 ret = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002706 continue;
2707 }
2708 /* Fatal error - ENOMEM, EIO... */
2709 if (ret)
Mingming Cao61628a32008-07-11 19:27:31 -04002710 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002711 }
Shaohua Li1bce63d2011-10-18 10:55:51 -04002712 blk_finish_plug(&plug);
Jan Kara9c12a832013-09-16 08:24:26 -04002713 if (!ret && !cycled && wbc->nr_to_write > 0) {
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002714 cycled = 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04002715 mpd.last_page = writeback_index - 1;
2716 mpd.first_page = 0;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002717 goto retry;
2718 }
Mingming Cao61628a32008-07-11 19:27:31 -04002719
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002720 /* Update index */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002721 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2722 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002723 * Set the writeback_index so that range_cyclic
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002724 * mode will write it back later
2725 */
Jan Kara4e7ea812013-06-04 13:17:40 -04002726 mapping->writeback_index = mpd.first_page;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002727
Mingming Cao61628a32008-07-11 19:27:31 -04002728out_writepages:
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002729 trace_ext4_writepages_result(inode, wbc, ret,
2730 nr_to_write - wbc->nr_to_write);
Mingming Cao61628a32008-07-11 19:27:31 -04002731 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002732}
2733
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002734static int ext4_nonda_switch(struct super_block *sb)
2735{
Eric Whitney5c1ff332013-04-09 09:27:31 -04002736 s64 free_clusters, dirty_clusters;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002737 struct ext4_sb_info *sbi = EXT4_SB(sb);
2738
2739 /*
2740 * switch to non delalloc mode if we are running low
2741 * on free block. The free block accounting via percpu
Eric Dumazet179f7eb2009-01-06 14:41:04 -08002742 * counters can get slightly wrong with percpu_counter_batch getting
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002743 * accumulated on each CPU without updating global counters
2744 * Delalloc need an accurate free block accounting. So switch
2745 * to non delalloc when we are near to error range.
2746 */
Eric Whitney5c1ff332013-04-09 09:27:31 -04002747 free_clusters =
2748 percpu_counter_read_positive(&sbi->s_freeclusters_counter);
2749 dirty_clusters =
2750 percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
Theodore Ts'o00d4e732012-09-19 22:42:36 -04002751 /*
2752 * Start pushing delalloc when 1/2 of free blocks are dirty.
2753 */
Eric Whitney5c1ff332013-04-09 09:27:31 -04002754 if (dirty_clusters && (free_clusters < 2 * dirty_clusters))
Miao Xie10ee27a2013-01-10 13:47:57 +08002755 try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE);
Theodore Ts'o00d4e732012-09-19 22:42:36 -04002756
Eric Whitney5c1ff332013-04-09 09:27:31 -04002757 if (2 * free_clusters < 3 * dirty_clusters ||
2758 free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) {
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002759 /*
Eric Sandeenc8afb442009-12-23 07:58:12 -05002760 * free block count is less than 150% of dirty blocks
2761 * or free blocks is less than watermark
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002762 */
2763 return 1;
2764 }
2765 return 0;
2766}
2767
Eric Sandeen0ff89472014-10-11 19:51:17 -04002768/* We always reserve for an inode update; the superblock could be there too */
2769static int ext4_da_write_credits(struct inode *inode, loff_t pos, unsigned len)
2770{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002771 if (likely(ext4_has_feature_large_file(inode->i_sb)))
Eric Sandeen0ff89472014-10-11 19:51:17 -04002772 return 1;
2773
2774 if (pos + len <= 0x7fffffffULL)
2775 return 1;
2776
2777 /* We might need to update the superblock to set LARGE_FILE */
2778 return 2;
2779}
2780
Alex Tomas64769242008-07-11 19:27:31 -04002781static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002782 loff_t pos, unsigned len, unsigned flags,
2783 struct page **pagep, void **fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04002784{
Eric Sandeen72b8ab92010-05-16 11:00:00 -04002785 int ret, retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002786 struct page *page;
2787 pgoff_t index;
Alex Tomas64769242008-07-11 19:27:31 -04002788 struct inode *inode = mapping->host;
2789 handle_t *handle;
2790
2791 index = pos >> PAGE_CACHE_SHIFT;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002792
2793 if (ext4_nonda_switch(inode->i_sb)) {
2794 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
2795 return ext4_write_begin(file, mapping, pos,
2796 len, flags, pagep, fsdata);
2797 }
2798 *fsdata = (void *)0;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002799 trace_ext4_da_write_begin(inode, pos, len, flags);
Tao Ma9c3569b2012-12-10 14:05:57 -05002800
2801 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2802 ret = ext4_da_write_inline_data_begin(mapping, inode,
2803 pos, len, flags,
2804 pagep, fsdata);
2805 if (ret < 0)
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002806 return ret;
2807 if (ret == 1)
2808 return 0;
Tao Ma9c3569b2012-12-10 14:05:57 -05002809 }
2810
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002811 /*
2812 * grab_cache_page_write_begin() can take a long time if the
2813 * system is thrashing due to memory pressure, or if the page
2814 * is being written back. So grab it first before we start
2815 * the transaction handle. This also allows us to allocate
2816 * the page (if needed) without using GFP_NOFS.
2817 */
2818retry_grab:
2819 page = grab_cache_page_write_begin(mapping, index, flags);
2820 if (!page)
2821 return -ENOMEM;
2822 unlock_page(page);
2823
Alex Tomas64769242008-07-11 19:27:31 -04002824 /*
2825 * With delayed allocation, we don't log the i_disksize update
2826 * if there is delayed block allocation. But we still need
2827 * to journalling the i_disksize update if writes to the end
2828 * of file which has an already mapped buffer.
2829 */
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002830retry_journal:
Eric Sandeen0ff89472014-10-11 19:51:17 -04002831 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
2832 ext4_da_write_credits(inode, pos, len));
Alex Tomas64769242008-07-11 19:27:31 -04002833 if (IS_ERR(handle)) {
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002834 page_cache_release(page);
2835 return PTR_ERR(handle);
Alex Tomas64769242008-07-11 19:27:31 -04002836 }
2837
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002838 lock_page(page);
2839 if (page->mapping != mapping) {
2840 /* The page got truncated from under us */
2841 unlock_page(page);
2842 page_cache_release(page);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002843 ext4_journal_stop(handle);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002844 goto retry_grab;
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002845 }
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002846 /* In case writeback began while the page was unlocked */
Dmitry Monakhov7afe5aa2013-08-28 14:30:47 -04002847 wait_for_stable_page(page);
Alex Tomas64769242008-07-11 19:27:31 -04002848
Michael Halcrow2058f832015-04-12 00:55:10 -04002849#ifdef CONFIG_EXT4_FS_ENCRYPTION
2850 ret = ext4_block_write_begin(page, pos, len,
2851 ext4_da_get_block_prep);
2852#else
Christoph Hellwig6e1db882010-06-04 11:29:57 +02002853 ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
Michael Halcrow2058f832015-04-12 00:55:10 -04002854#endif
Alex Tomas64769242008-07-11 19:27:31 -04002855 if (ret < 0) {
2856 unlock_page(page);
2857 ext4_journal_stop(handle);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04002858 /*
2859 * block_write_begin may have instantiated a few blocks
2860 * outside i_size. Trim these off again. Don't need
2861 * i_size_read because we hold i_mutex.
2862 */
2863 if (pos + len > inode->i_size)
Jan Karab9a42072009-12-08 21:24:33 -05002864 ext4_truncate_failed_write(inode);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002865
2866 if (ret == -ENOSPC &&
2867 ext4_should_retry_alloc(inode->i_sb, &retries))
2868 goto retry_journal;
2869
2870 page_cache_release(page);
2871 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002872 }
2873
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002874 *pagep = page;
Alex Tomas64769242008-07-11 19:27:31 -04002875 return ret;
2876}
2877
Mingming Cao632eaea2008-07-11 19:27:31 -04002878/*
2879 * Check if we should update i_disksize
2880 * when write to the end of file but not require block allocation
2881 */
2882static int ext4_da_should_update_i_disksize(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002883 unsigned long offset)
Mingming Cao632eaea2008-07-11 19:27:31 -04002884{
2885 struct buffer_head *bh;
2886 struct inode *inode = page->mapping->host;
2887 unsigned int idx;
2888 int i;
2889
2890 bh = page_buffers(page);
2891 idx = offset >> inode->i_blkbits;
2892
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002893 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04002894 bh = bh->b_this_page;
2895
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002896 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
Mingming Cao632eaea2008-07-11 19:27:31 -04002897 return 0;
2898 return 1;
2899}
2900
Alex Tomas64769242008-07-11 19:27:31 -04002901static int ext4_da_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002902 struct address_space *mapping,
2903 loff_t pos, unsigned len, unsigned copied,
2904 struct page *page, void *fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04002905{
2906 struct inode *inode = mapping->host;
2907 int ret = 0, ret2;
2908 handle_t *handle = ext4_journal_current_handle();
2909 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04002910 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002911 int write_mode = (int)(unsigned long)fsdata;
2912
Theodore Ts'o74d553a2013-04-03 12:39:17 -04002913 if (write_mode == FALL_BACK_TO_NONDELALLOC)
2914 return ext4_write_end(file, mapping, pos,
2915 len, copied, page, fsdata);
Mingming Cao632eaea2008-07-11 19:27:31 -04002916
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002917 trace_ext4_da_write_end(inode, pos, len, copied);
Mingming Cao632eaea2008-07-11 19:27:31 -04002918 start = pos & (PAGE_CACHE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002919 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04002920
2921 /*
2922 * generic_write_end() will run mark_inode_dirty() if i_size
2923 * changes. So let's piggyback the i_disksize mark_inode_dirty
2924 * into that.
2925 */
Alex Tomas64769242008-07-11 19:27:31 -04002926 new_i_size = pos + copied;
Andrea Arcangeliea51d132011-12-13 21:41:15 -05002927 if (copied && new_i_size > EXT4_I(inode)->i_disksize) {
Tao Ma9c3569b2012-12-10 14:05:57 -05002928 if (ext4_has_inline_data(inode) ||
2929 ext4_da_should_update_i_disksize(page, end)) {
Dmitry Monakhovee124d22014-08-30 23:34:06 -04002930 ext4_update_i_disksize(inode, new_i_size);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04002931 /* We need to mark inode dirty even if
2932 * new_i_size is less that inode->i_size
2933 * bu greater than i_disksize.(hint delalloc)
2934 */
2935 ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04002936 }
Mingming Cao632eaea2008-07-11 19:27:31 -04002937 }
Tao Ma9c3569b2012-12-10 14:05:57 -05002938
2939 if (write_mode != CONVERT_INLINE_DATA &&
2940 ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
2941 ext4_has_inline_data(inode))
2942 ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied,
2943 page);
2944 else
2945 ret2 = generic_write_end(file, mapping, pos, len, copied,
Alex Tomas64769242008-07-11 19:27:31 -04002946 page, fsdata);
Tao Ma9c3569b2012-12-10 14:05:57 -05002947
Alex Tomas64769242008-07-11 19:27:31 -04002948 copied = ret2;
2949 if (ret2 < 0)
2950 ret = ret2;
2951 ret2 = ext4_journal_stop(handle);
2952 if (!ret)
2953 ret = ret2;
2954
2955 return ret ? ret : copied;
2956}
2957
Lukas Czernerd47992f2013-05-21 23:17:23 -04002958static void ext4_da_invalidatepage(struct page *page, unsigned int offset,
2959 unsigned int length)
Alex Tomas64769242008-07-11 19:27:31 -04002960{
Alex Tomas64769242008-07-11 19:27:31 -04002961 /*
2962 * Drop reserved blocks
2963 */
2964 BUG_ON(!PageLocked(page));
2965 if (!page_has_buffers(page))
2966 goto out;
2967
Lukas Czernerca99fdd2013-05-21 23:25:01 -04002968 ext4_da_page_release_reservation(page, offset, length);
Alex Tomas64769242008-07-11 19:27:31 -04002969
2970out:
Lukas Czernerd47992f2013-05-21 23:17:23 -04002971 ext4_invalidatepage(page, offset, length);
Alex Tomas64769242008-07-11 19:27:31 -04002972
2973 return;
2974}
2975
Theodore Ts'occd25062009-02-26 01:04:07 -05002976/*
2977 * Force all delayed allocation blocks to be allocated for a given inode.
2978 */
2979int ext4_alloc_da_blocks(struct inode *inode)
2980{
Theodore Ts'ofb40ba02009-09-16 19:30:40 -04002981 trace_ext4_alloc_da_blocks(inode);
2982
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04002983 if (!EXT4_I(inode)->i_reserved_data_blocks)
Theodore Ts'occd25062009-02-26 01:04:07 -05002984 return 0;
2985
2986 /*
2987 * We do something simple for now. The filemap_flush() will
2988 * also start triggering a write of the data blocks, which is
2989 * not strictly speaking necessary (and for users of
2990 * laptop_mode, not even desirable). However, to do otherwise
2991 * would require replicating code paths in:
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002992 *
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002993 * ext4_writepages() ->
Theodore Ts'occd25062009-02-26 01:04:07 -05002994 * write_cache_pages() ---> (via passed in callback function)
2995 * __mpage_da_writepage() -->
2996 * mpage_add_bh_to_extent()
2997 * mpage_da_map_blocks()
2998 *
2999 * The problem is that write_cache_pages(), located in
3000 * mm/page-writeback.c, marks pages clean in preparation for
3001 * doing I/O, which is not desirable if we're not planning on
3002 * doing I/O at all.
3003 *
3004 * We could call write_cache_pages(), and then redirty all of
Wu Fengguang380cf092010-11-11 19:23:29 +08003005 * the pages by calling redirty_page_for_writepage() but that
Theodore Ts'occd25062009-02-26 01:04:07 -05003006 * would be ugly in the extreme. So instead we would need to
3007 * replicate parts of the code in the above functions,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003008 * simplifying them because we wouldn't actually intend to
Theodore Ts'occd25062009-02-26 01:04:07 -05003009 * write out the pages, but rather only collect contiguous
3010 * logical block extents, call the multi-block allocator, and
3011 * then update the buffer heads with the block allocations.
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003012 *
Theodore Ts'occd25062009-02-26 01:04:07 -05003013 * For now, though, we'll cheat by calling filemap_flush(),
3014 * which will map the blocks, and start the I/O, but not
3015 * actually wait for the I/O to complete.
3016 */
3017 return filemap_flush(inode->i_mapping);
3018}
Alex Tomas64769242008-07-11 19:27:31 -04003019
3020/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003021 * bmap() is special. It gets used by applications such as lilo and by
3022 * the swapper to find the on-disk block of a specific piece of data.
3023 *
3024 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07003025 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003026 * filesystem and enables swap, then they may get a nasty shock when the
3027 * data getting swapped to that swapfile suddenly gets overwritten by
3028 * the original zero's written out previously to the journal and
3029 * awaiting writeback in the kernel's buffer cache.
3030 *
3031 * So, if we see any bmap calls here on a modified, data-journaled file,
3032 * take extra steps to flush any blocks which might be in the cache.
3033 */
Mingming Cao617ba132006-10-11 01:20:53 -07003034static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003035{
3036 struct inode *inode = mapping->host;
3037 journal_t *journal;
3038 int err;
3039
Tao Ma46c7f252012-12-10 14:04:52 -05003040 /*
3041 * We can get here for an inline file via the FIBMAP ioctl
3042 */
3043 if (ext4_has_inline_data(inode))
3044 return 0;
3045
Alex Tomas64769242008-07-11 19:27:31 -04003046 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
3047 test_opt(inode->i_sb, DELALLOC)) {
3048 /*
3049 * With delalloc we want to sync the file
3050 * so that we can make sure we allocate
3051 * blocks for file
3052 */
3053 filemap_write_and_wait(mapping);
3054 }
3055
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003056 if (EXT4_JOURNAL(inode) &&
3057 ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003058 /*
3059 * This is a REALLY heavyweight approach, but the use of
3060 * bmap on dirty files is expected to be extremely rare:
3061 * only if we run lilo or swapon on a freshly made file
3062 * do we expect this to happen.
3063 *
3064 * (bmap requires CAP_SYS_RAWIO so this does not
3065 * represent an unprivileged user DOS attack --- we'd be
3066 * in trouble if mortal users could trigger this path at
3067 * will.)
3068 *
Mingming Cao617ba132006-10-11 01:20:53 -07003069 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003070 * regular files. If somebody wants to bmap a directory
3071 * or symlink and gets confused because the buffer
3072 * hasn't yet been flushed to disk, they deserve
3073 * everything they get.
3074 */
3075
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003076 ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
Mingming Cao617ba132006-10-11 01:20:53 -07003077 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07003078 jbd2_journal_lock_updates(journal);
3079 err = jbd2_journal_flush(journal);
3080 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003081
3082 if (err)
3083 return 0;
3084 }
3085
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003086 return generic_block_bmap(mapping, block, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003087}
3088
Mingming Cao617ba132006-10-11 01:20:53 -07003089static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003090{
Tao Ma46c7f252012-12-10 14:04:52 -05003091 int ret = -EAGAIN;
3092 struct inode *inode = page->mapping->host;
3093
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003094 trace_ext4_readpage(page);
Tao Ma46c7f252012-12-10 14:04:52 -05003095
3096 if (ext4_has_inline_data(inode))
3097 ret = ext4_readpage_inline(inode, page);
3098
3099 if (ret == -EAGAIN)
Theodore Ts'of64e02f2015-04-08 00:00:32 -04003100 return ext4_mpage_readpages(page->mapping, NULL, page, 1);
Tao Ma46c7f252012-12-10 14:04:52 -05003101
3102 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003103}
3104
3105static int
Mingming Cao617ba132006-10-11 01:20:53 -07003106ext4_readpages(struct file *file, struct address_space *mapping,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003107 struct list_head *pages, unsigned nr_pages)
3108{
Tao Ma46c7f252012-12-10 14:04:52 -05003109 struct inode *inode = mapping->host;
3110
3111 /* If the file has inline data, no need to do readpages. */
3112 if (ext4_has_inline_data(inode))
3113 return 0;
3114
Theodore Ts'of64e02f2015-04-08 00:00:32 -04003115 return ext4_mpage_readpages(mapping, pages, NULL, nr_pages);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003116}
3117
Lukas Czernerd47992f2013-05-21 23:17:23 -04003118static void ext4_invalidatepage(struct page *page, unsigned int offset,
3119 unsigned int length)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003120{
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003121 trace_ext4_invalidatepage(page, offset, length);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003122
Jan Kara4520fb32012-12-25 13:28:54 -05003123 /* No journalling happens on data buffers when this function is used */
3124 WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page)));
3125
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003126 block_invalidatepage(page, offset, length);
Jan Kara4520fb32012-12-25 13:28:54 -05003127}
3128
Jan Kara53e87262012-12-25 13:29:52 -05003129static int __ext4_journalled_invalidatepage(struct page *page,
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003130 unsigned int offset,
3131 unsigned int length)
Jan Kara4520fb32012-12-25 13:28:54 -05003132{
3133 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
3134
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003135 trace_ext4_journalled_invalidatepage(page, offset, length);
Jan Kara4520fb32012-12-25 13:28:54 -05003136
Jiaying Zhang744692d2010-03-04 16:14:02 -05003137 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003138 * If it's a full truncate we just forget about the pending dirtying
3139 */
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003140 if (offset == 0 && length == PAGE_CACHE_SIZE)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003141 ClearPageChecked(page);
3142
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003143 return jbd2_journal_invalidatepage(journal, page, offset, length);
Jan Kara53e87262012-12-25 13:29:52 -05003144}
3145
3146/* Wrapper for aops... */
3147static void ext4_journalled_invalidatepage(struct page *page,
Lukas Czernerd47992f2013-05-21 23:17:23 -04003148 unsigned int offset,
3149 unsigned int length)
Jan Kara53e87262012-12-25 13:29:52 -05003150{
Lukas Czernerca99fdd2013-05-21 23:25:01 -04003151 WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003152}
3153
Mingming Cao617ba132006-10-11 01:20:53 -07003154static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003155{
Mingming Cao617ba132006-10-11 01:20:53 -07003156 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003157
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003158 trace_ext4_releasepage(page);
3159
Jan Karae1c36592013-03-10 22:19:00 -04003160 /* Page has dirty journalled data -> cannot release */
3161 if (PageChecked(page))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003162 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05003163 if (journal)
3164 return jbd2_journal_try_to_free_buffers(journal, page, wait);
3165 else
3166 return try_to_free_buffers(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003167}
3168
Jan Karaba5843f2015-12-07 15:10:44 -05003169#ifdef CONFIG_FS_DAX
3170int ext4_dax_mmap_get_block(struct inode *inode, sector_t iblock,
3171 struct buffer_head *bh_result, int create)
Matthew Wilcoxed923b52015-09-08 14:59:20 -07003172{
Jan Karaba5843f2015-12-07 15:10:44 -05003173 int ret, err;
3174 int credits;
3175 struct ext4_map_blocks map;
3176 handle_t *handle = NULL;
3177 int flags = 0;
Jan Karac86d8db2015-12-07 15:10:26 -05003178
Jan Karaba5843f2015-12-07 15:10:44 -05003179 ext4_debug("ext4_dax_mmap_get_block: inode %lu, create flag %d\n",
Matthew Wilcoxed923b52015-09-08 14:59:20 -07003180 inode->i_ino, create);
Jan Karaba5843f2015-12-07 15:10:44 -05003181 map.m_lblk = iblock;
3182 map.m_len = bh_result->b_size >> inode->i_blkbits;
3183 credits = ext4_chunk_trans_blocks(inode, map.m_len);
3184 if (create) {
3185 flags |= EXT4_GET_BLOCKS_PRE_IO | EXT4_GET_BLOCKS_CREATE_ZERO;
3186 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, credits);
3187 if (IS_ERR(handle)) {
3188 ret = PTR_ERR(handle);
3189 return ret;
3190 }
3191 }
3192
3193 ret = ext4_map_blocks(handle, inode, &map, flags);
3194 if (create) {
3195 err = ext4_journal_stop(handle);
3196 if (ret >= 0 && err < 0)
3197 ret = err;
3198 }
3199 if (ret <= 0)
3200 goto out;
3201 if (map.m_flags & EXT4_MAP_UNWRITTEN) {
3202 int err2;
3203
3204 /*
3205 * We are protected by i_mmap_sem so we know block cannot go
3206 * away from under us even though we dropped i_data_sem.
3207 * Convert extent to written and write zeros there.
3208 *
3209 * Note: We may get here even when create == 0.
3210 */
3211 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, credits);
3212 if (IS_ERR(handle)) {
3213 ret = PTR_ERR(handle);
3214 goto out;
3215 }
3216
3217 err = ext4_map_blocks(handle, inode, &map,
3218 EXT4_GET_BLOCKS_CONVERT | EXT4_GET_BLOCKS_CREATE_ZERO);
3219 if (err < 0)
3220 ret = err;
3221 err2 = ext4_journal_stop(handle);
3222 if (err2 < 0 && ret > 0)
3223 ret = err2;
3224 }
3225out:
3226 WARN_ON_ONCE(ret == 0 && create);
3227 if (ret > 0) {
3228 map_bh(bh_result, inode->i_sb, map.m_pblk);
3229 bh_result->b_state = (bh_result->b_state & ~EXT4_MAP_FLAGS) |
3230 map.m_flags;
3231 /*
3232 * At least for now we have to clear BH_New so that DAX code
3233 * doesn't attempt to zero blocks again in a racy way.
3234 */
3235 bh_result->b_state &= ~(1 << BH_New);
3236 bh_result->b_size = map.m_len << inode->i_blkbits;
3237 ret = 0;
3238 }
3239 return ret;
Matthew Wilcoxed923b52015-09-08 14:59:20 -07003240}
Jan Karaba5843f2015-12-07 15:10:44 -05003241#endif
Matthew Wilcoxed923b52015-09-08 14:59:20 -07003242
Mingming Cao4c0425f2009-09-28 15:48:41 -04003243static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
Christoph Hellwig7b7a8662013-09-04 15:04:39 +02003244 ssize_t size, void *private)
Mingming Cao4c0425f2009-09-28 15:48:41 -04003245{
3246 ext4_io_end_t *io_end = iocb->private;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003247
Jan Kara97a851e2013-06-04 11:58:58 -04003248 /* if not async direct IO just return */
Christoph Hellwig7b7a8662013-09-04 15:04:39 +02003249 if (!io_end)
Jan Kara97a851e2013-06-04 11:58:58 -04003250 return;
Mingming4b70df12009-11-03 14:44:54 -05003251
Zheng Liu88635ca2011-12-28 19:00:25 -05003252 ext_debug("ext4_end_io_dio(): io_end 0x%p "
Joe Perchesace36ad2012-03-19 23:11:43 -04003253 "for inode %lu, iocb 0x%p, offset %llu, size %zd\n",
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003254 iocb->private, io_end->inode->i_ino, iocb, offset,
3255 size);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003256
Theodore Ts'ob5a7e972011-12-12 10:53:02 -05003257 iocb->private = NULL;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003258 io_end->offset = offset;
3259 io_end->size = size;
Christoph Hellwig7b7a8662013-09-04 15:04:39 +02003260 ext4_put_io_end(io_end);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003261}
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003262
Mingming Cao4c0425f2009-09-28 15:48:41 -04003263/*
3264 * For ext4 extent files, ext4 will do direct-io write to holes,
3265 * preallocated extents, and those write extend the file, no need to
3266 * fall back to buffered IO.
3267 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003268 * For holes, we fallocate those blocks, mark them as unwritten
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003269 * If those blocks were preallocated, we mark sure they are split, but
Lukas Czerner556615d2014-04-20 23:45:47 -04003270 * still keep the range to write as unwritten.
Mingming Cao4c0425f2009-09-28 15:48:41 -04003271 *
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003272 * The unwritten extents will be converted to written when DIO is completed.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003273 * For async direct IO, since the IO may still pending when return, we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003274 * set up an end_io call back function, which will do the conversion
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003275 * when async direct IO completed.
Mingming Cao4c0425f2009-09-28 15:48:41 -04003276 *
3277 * If the O_DIRECT write will extend the file then add this inode to the
3278 * orphan list. So recovery will truncate it back to the original size
3279 * if the machine crashes during the write.
3280 *
3281 */
Omar Sandoval6f673762015-03-16 04:33:52 -07003282static ssize_t ext4_ext_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
3283 loff_t offset)
Mingming Cao4c0425f2009-09-28 15:48:41 -04003284{
3285 struct file *file = iocb->ki_filp;
3286 struct inode *inode = file->f_mapping->host;
3287 ssize_t ret;
Al Viroa6cbcd42014-03-04 22:38:00 -05003288 size_t count = iov_iter_count(iter);
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003289 int overwrite = 0;
3290 get_block_t *get_block_func = NULL;
3291 int dio_flags = 0;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003292 loff_t final_size = offset + count;
Jan Kara97a851e2013-06-04 11:58:58 -04003293 ext4_io_end_t *io_end = NULL;
Zheng Liu729f52c2012-07-09 16:29:29 -04003294
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003295 /* Use the old path for reads and writes beyond i_size. */
Omar Sandoval6f673762015-03-16 04:33:52 -07003296 if (iov_iter_rw(iter) != WRITE || final_size > inode->i_size)
3297 return ext4_ind_direct_IO(iocb, iter, offset);
Zheng Liu4bd809d2012-07-22 20:19:31 -04003298
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003299 BUG_ON(iocb->private == NULL);
Zheng Liu4bd809d2012-07-22 20:19:31 -04003300
Jan Karae8340392013-06-04 14:27:38 -04003301 /*
3302 * Make all waiters for direct IO properly wait also for extent
3303 * conversion. This also disallows race between truncate() and
3304 * overwrite DIO as i_dio_count needs to be incremented under i_mutex.
3305 */
Omar Sandoval6f673762015-03-16 04:33:52 -07003306 if (iov_iter_rw(iter) == WRITE)
Jens Axboefe0f07d2015-04-15 17:05:48 -06003307 inode_dio_begin(inode);
Jan Karae8340392013-06-04 14:27:38 -04003308
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003309 /* If we do a overwrite dio, i_mutex locking can be released */
3310 overwrite = *((int *)iocb->private);
Zheng Liu4bd809d2012-07-22 20:19:31 -04003311
Jan Kara2dcba472015-12-07 15:04:57 -05003312 if (overwrite)
Al Viro59551022016-01-22 15:40:57 -05003313 inode_unlock(inode);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003314
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003315 /*
3316 * We could direct write to holes and fallocate.
3317 *
3318 * Allocated blocks to fill the hole are marked as
Lukas Czerner556615d2014-04-20 23:45:47 -04003319 * unwritten to prevent parallel buffered read to expose
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003320 * the stale data before DIO complete the data IO.
3321 *
3322 * As to previously fallocated extents, ext4 get_block will
3323 * just simply mark the buffer mapped but still keep the
Lukas Czerner556615d2014-04-20 23:45:47 -04003324 * extents unwritten.
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003325 *
3326 * For non AIO case, we will convert those unwritten extents
3327 * to written after return back from blockdev_direct_IO.
3328 *
3329 * For async DIO, the conversion needs to be deferred when the
3330 * IO is completed. The ext4 end_io callback function will be
3331 * called to take care of the conversion work. Here for async
3332 * case, we allocate an io_end structure to hook to the iocb.
3333 */
3334 iocb->private = NULL;
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003335 if (overwrite) {
Jan Kara705965b2016-03-08 23:08:10 -05003336 get_block_func = ext4_dio_get_block_overwrite;
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003337 } else {
Jan Kara74dae422016-02-19 00:33:21 -05003338 ext4_inode_aio_set(inode, NULL);
3339 if (!is_sync_kiocb(iocb)) {
3340 io_end = ext4_init_io_end(inode, GFP_NOFS);
3341 if (!io_end) {
3342 ret = -ENOMEM;
3343 goto retake_lock;
3344 }
3345 /*
3346 * Grab reference for DIO. Will be dropped in
3347 * ext4_end_io_dio()
3348 */
3349 iocb->private = ext4_get_io_end(io_end);
3350 /*
3351 * we save the io structure for current async direct
3352 * IO, so that later ext4_map_blocks() could flag the
3353 * io structure whether there is a unwritten extents
3354 * needs to be converted when IO is completed.
3355 */
3356 ext4_inode_aio_set(inode, io_end);
3357 }
Jan Kara705965b2016-03-08 23:08:10 -05003358 get_block_func = ext4_dio_get_block_unwritten;
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003359 dio_flags = DIO_LOCKING;
3360 }
Michael Halcrow2058f832015-04-12 00:55:10 -04003361#ifdef CONFIG_EXT4_FS_ENCRYPTION
3362 BUG_ON(ext4_encrypted_inode(inode) && S_ISREG(inode->i_mode));
3363#endif
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003364 if (IS_DAX(inode))
Omar Sandovala95cd632015-03-16 04:33:51 -07003365 ret = dax_do_io(iocb, inode, iter, offset, get_block_func,
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003366 ext4_end_io_dio, dio_flags);
3367 else
Omar Sandoval17f8c842015-03-16 04:33:50 -07003368 ret = __blockdev_direct_IO(iocb, inode,
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003369 inode->i_sb->s_bdev, iter, offset,
3370 get_block_func,
3371 ext4_end_io_dio, NULL, dio_flags);
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003372
Theodore Ts'oa5499842013-05-11 19:07:42 -04003373 /*
Jan Kara97a851e2013-06-04 11:58:58 -04003374 * Put our reference to io_end. This can free the io_end structure e.g.
3375 * in sync IO case or in case of error. It can even perform extent
3376 * conversion if all bios we submitted finished before we got here.
3377 * Note that in that case iocb->private can be already set to NULL
3378 * here.
Theodore Ts'oa5499842013-05-11 19:07:42 -04003379 */
Jan Kara97a851e2013-06-04 11:58:58 -04003380 if (io_end) {
3381 ext4_inode_aio_set(inode, NULL);
3382 ext4_put_io_end(io_end);
3383 /*
3384 * When no IO was submitted ext4_end_io_dio() was not
3385 * called so we have to put iocb's reference.
3386 */
3387 if (ret <= 0 && ret != -EIOCBQUEUED && iocb->private) {
3388 WARN_ON(iocb->private != io_end);
3389 WARN_ON(io_end->flag & EXT4_IO_END_UNWRITTEN);
Jan Kara97a851e2013-06-04 11:58:58 -04003390 ext4_put_io_end(io_end);
3391 iocb->private = NULL;
3392 }
3393 }
3394 if (ret > 0 && !overwrite && ext4_test_inode_state(inode,
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003395 EXT4_STATE_DIO_UNWRITTEN)) {
3396 int err;
3397 /*
3398 * for non AIO case, since the IO is already
3399 * completed, we could do the conversion right here
3400 */
Jan Kara6b523df2013-06-04 13:21:11 -04003401 err = ext4_convert_unwritten_extents(NULL, inode,
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003402 offset, ret);
3403 if (err < 0)
3404 ret = err;
3405 ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
3406 }
3407
3408retake_lock:
Omar Sandoval6f673762015-03-16 04:33:52 -07003409 if (iov_iter_rw(iter) == WRITE)
Jens Axboefe0f07d2015-04-15 17:05:48 -06003410 inode_dio_end(inode);
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003411 /* take i_mutex locking again if we do a ovewrite dio */
Jan Kara2dcba472015-12-07 15:04:57 -05003412 if (overwrite)
Al Viro59551022016-01-22 15:40:57 -05003413 inode_lock(inode);
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003414
3415 return ret;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003416}
3417
Omar Sandoval22c61862015-03-16 04:33:53 -07003418static ssize_t ext4_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
3419 loff_t offset)
Mingming Cao4c0425f2009-09-28 15:48:41 -04003420{
3421 struct file *file = iocb->ki_filp;
3422 struct inode *inode = file->f_mapping->host;
Al Viroa6cbcd42014-03-04 22:38:00 -05003423 size_t count = iov_iter_count(iter);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003424 ssize_t ret;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003425
Michael Halcrow2058f832015-04-12 00:55:10 -04003426#ifdef CONFIG_EXT4_FS_ENCRYPTION
3427 if (ext4_encrypted_inode(inode) && S_ISREG(inode->i_mode))
3428 return 0;
3429#endif
3430
Theodore Ts'o84ebd792011-08-31 11:56:51 -04003431 /*
3432 * If we are doing data journalling we don't support O_DIRECT
3433 */
3434 if (ext4_should_journal_data(inode))
3435 return 0;
3436
Tao Ma46c7f252012-12-10 14:04:52 -05003437 /* Let buffer I/O handle the inline data case. */
3438 if (ext4_has_inline_data(inode))
3439 return 0;
3440
Omar Sandoval6f673762015-03-16 04:33:52 -07003441 trace_ext4_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003442 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Omar Sandoval6f673762015-03-16 04:33:52 -07003443 ret = ext4_ext_direct_IO(iocb, iter, offset);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003444 else
Omar Sandoval6f673762015-03-16 04:33:52 -07003445 ret = ext4_ind_direct_IO(iocb, iter, offset);
3446 trace_ext4_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), ret);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003447 return ret;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003448}
3449
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003450/*
Mingming Cao617ba132006-10-11 01:20:53 -07003451 * Pages can be marked dirty completely asynchronously from ext4's journalling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003452 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3453 * much here because ->set_page_dirty is called under VFS locks. The page is
3454 * not necessarily locked.
3455 *
3456 * We cannot just dirty the page and leave attached buffers clean, because the
3457 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3458 * or jbddirty because all the journalling code will explode.
3459 *
3460 * So what we do is to mark the page "pending dirty" and next time writepage
3461 * is called, propagate that into the buffers appropriately.
3462 */
Mingming Cao617ba132006-10-11 01:20:53 -07003463static int ext4_journalled_set_page_dirty(struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003464{
3465 SetPageChecked(page);
3466 return __set_page_dirty_nobuffers(page);
3467}
3468
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003469static const struct address_space_operations ext4_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003470 .readpage = ext4_readpage,
3471 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003472 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003473 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003474 .write_begin = ext4_write_begin,
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003475 .write_end = ext4_write_end,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003476 .bmap = ext4_bmap,
3477 .invalidatepage = ext4_invalidatepage,
3478 .releasepage = ext4_releasepage,
3479 .direct_IO = ext4_direct_IO,
3480 .migratepage = buffer_migrate_page,
3481 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003482 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003483};
3484
Mingming Cao617ba132006-10-11 01:20:53 -07003485static const struct address_space_operations ext4_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003486 .readpage = ext4_readpage,
3487 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003488 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003489 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003490 .write_begin = ext4_write_begin,
3491 .write_end = ext4_journalled_write_end,
3492 .set_page_dirty = ext4_journalled_set_page_dirty,
3493 .bmap = ext4_bmap,
Jan Kara4520fb32012-12-25 13:28:54 -05003494 .invalidatepage = ext4_journalled_invalidatepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003495 .releasepage = ext4_releasepage,
Theodore Ts'o84ebd792011-08-31 11:56:51 -04003496 .direct_IO = ext4_direct_IO,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003497 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003498 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003499};
3500
Alex Tomas64769242008-07-11 19:27:31 -04003501static const struct address_space_operations ext4_da_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003502 .readpage = ext4_readpage,
3503 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003504 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003505 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003506 .write_begin = ext4_da_write_begin,
3507 .write_end = ext4_da_write_end,
3508 .bmap = ext4_bmap,
3509 .invalidatepage = ext4_da_invalidatepage,
3510 .releasepage = ext4_releasepage,
3511 .direct_IO = ext4_direct_IO,
3512 .migratepage = buffer_migrate_page,
3513 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003514 .error_remove_page = generic_error_remove_page,
Alex Tomas64769242008-07-11 19:27:31 -04003515};
3516
Mingming Cao617ba132006-10-11 01:20:53 -07003517void ext4_set_aops(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003518{
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003519 switch (ext4_inode_journal_mode(inode)) {
3520 case EXT4_INODE_ORDERED_DATA_MODE:
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003521 ext4_set_inode_state(inode, EXT4_STATE_ORDERED_MODE);
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003522 break;
3523 case EXT4_INODE_WRITEBACK_DATA_MODE:
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003524 ext4_clear_inode_state(inode, EXT4_STATE_ORDERED_MODE);
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003525 break;
3526 case EXT4_INODE_JOURNAL_DATA_MODE:
Mingming Cao617ba132006-10-11 01:20:53 -07003527 inode->i_mapping->a_ops = &ext4_journalled_aops;
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003528 return;
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003529 default:
3530 BUG();
3531 }
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003532 if (test_opt(inode->i_sb, DELALLOC))
3533 inode->i_mapping->a_ops = &ext4_da_aops;
3534 else
3535 inode->i_mapping->a_ops = &ext4_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003536}
3537
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003538static int __ext4_block_zero_page_range(handle_t *handle,
Lukas Czernerd863dc32013-05-27 23:32:35 -04003539 struct address_space *mapping, loff_t from, loff_t length)
3540{
3541 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
3542 unsigned offset = from & (PAGE_CACHE_SIZE-1);
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003543 unsigned blocksize, pos;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003544 ext4_lblk_t iblock;
3545 struct inode *inode = mapping->host;
3546 struct buffer_head *bh;
3547 struct page *page;
3548 int err = 0;
3549
3550 page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
Michal Hockoc62d2552015-11-06 16:28:49 -08003551 mapping_gfp_constraint(mapping, ~__GFP_FS));
Lukas Czernerd863dc32013-05-27 23:32:35 -04003552 if (!page)
3553 return -ENOMEM;
3554
3555 blocksize = inode->i_sb->s_blocksize;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003556
3557 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
3558
3559 if (!page_has_buffers(page))
3560 create_empty_buffers(page, blocksize, 0);
3561
3562 /* Find the buffer that contains "offset" */
3563 bh = page_buffers(page);
3564 pos = blocksize;
3565 while (offset >= pos) {
3566 bh = bh->b_this_page;
3567 iblock++;
3568 pos += blocksize;
3569 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003570 if (buffer_freed(bh)) {
3571 BUFFER_TRACE(bh, "freed: skip");
3572 goto unlock;
3573 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003574 if (!buffer_mapped(bh)) {
3575 BUFFER_TRACE(bh, "unmapped");
3576 ext4_get_block(inode, iblock, bh, 0);
3577 /* unmapped? It's a hole - nothing to do */
3578 if (!buffer_mapped(bh)) {
3579 BUFFER_TRACE(bh, "still unmapped");
3580 goto unlock;
3581 }
3582 }
3583
3584 /* Ok, it's mapped. Make sure it's up-to-date */
3585 if (PageUptodate(page))
3586 set_buffer_uptodate(bh);
3587
3588 if (!buffer_uptodate(bh)) {
3589 err = -EIO;
3590 ll_rw_block(READ, 1, &bh);
3591 wait_on_buffer(bh);
3592 /* Uhhuh. Read error. Complain and punt. */
3593 if (!buffer_uptodate(bh))
3594 goto unlock;
Michael Halcrowc9c74292015-04-12 00:56:10 -04003595 if (S_ISREG(inode->i_mode) &&
3596 ext4_encrypted_inode(inode)) {
3597 /* We expect the key to be set. */
3598 BUG_ON(!ext4_has_encryption_key(inode));
3599 BUG_ON(blocksize != PAGE_CACHE_SIZE);
Theodore Ts'o3684de82015-10-03 10:49:26 -04003600 WARN_ON_ONCE(ext4_decrypt(page));
Michael Halcrowc9c74292015-04-12 00:56:10 -04003601 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003602 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003603 if (ext4_should_journal_data(inode)) {
3604 BUFFER_TRACE(bh, "get write access");
3605 err = ext4_journal_get_write_access(handle, bh);
3606 if (err)
3607 goto unlock;
3608 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003609 zero_user(page, offset, length);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003610 BUFFER_TRACE(bh, "zeroed end of block");
3611
Lukas Czernerd863dc32013-05-27 23:32:35 -04003612 if (ext4_should_journal_data(inode)) {
3613 err = ext4_handle_dirty_metadata(handle, inode, bh);
Lukas Czerner0713ed02013-05-27 23:32:35 -04003614 } else {
jon ernst353eefd2013-07-01 08:12:39 -04003615 err = 0;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003616 mark_buffer_dirty(bh);
Lukas Czerner0713ed02013-05-27 23:32:35 -04003617 if (ext4_test_inode_state(inode, EXT4_STATE_ORDERED_MODE))
3618 err = ext4_jbd2_file_inode(handle, inode);
3619 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003620
3621unlock:
3622 unlock_page(page);
3623 page_cache_release(page);
3624 return err;
3625}
3626
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003627/*
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003628 * ext4_block_zero_page_range() zeros out a mapping of length 'length'
3629 * starting from file offset 'from'. The range to be zero'd must
3630 * be contained with in one block. If the specified range exceeds
3631 * the end of the block it will be shortened to end of the block
3632 * that cooresponds to 'from'
3633 */
3634static int ext4_block_zero_page_range(handle_t *handle,
3635 struct address_space *mapping, loff_t from, loff_t length)
3636{
3637 struct inode *inode = mapping->host;
3638 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3639 unsigned blocksize = inode->i_sb->s_blocksize;
3640 unsigned max = blocksize - (offset & (blocksize - 1));
3641
3642 /*
3643 * correct length if it does not fall between
3644 * 'from' and the end of the block
3645 */
3646 if (length > max || length < 0)
3647 length = max;
3648
3649 if (IS_DAX(inode))
3650 return dax_zero_page_range(inode, from, length, ext4_get_block);
3651 return __ext4_block_zero_page_range(handle, mapping, from, length);
3652}
3653
3654/*
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003655 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
3656 * up to the end of the block which corresponds to `from'.
3657 * This required during truncate. We need to physically zero the tail end
3658 * of that block so it doesn't yield old data if the file is later grown.
3659 */
Stephen Hemmingerc1978552014-05-12 10:50:23 -04003660static int ext4_block_truncate_page(handle_t *handle,
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003661 struct address_space *mapping, loff_t from)
3662{
3663 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3664 unsigned length;
3665 unsigned blocksize;
3666 struct inode *inode = mapping->host;
3667
3668 blocksize = inode->i_sb->s_blocksize;
3669 length = blocksize - (offset & (blocksize - 1));
3670
3671 return ext4_block_zero_page_range(handle, mapping, from, length);
3672}
3673
Lukas Czernera87dd182013-05-27 23:32:35 -04003674int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
3675 loff_t lstart, loff_t length)
3676{
3677 struct super_block *sb = inode->i_sb;
3678 struct address_space *mapping = inode->i_mapping;
Lukas Czernere1be3a92013-07-01 08:12:39 -04003679 unsigned partial_start, partial_end;
Lukas Czernera87dd182013-05-27 23:32:35 -04003680 ext4_fsblk_t start, end;
3681 loff_t byte_end = (lstart + length - 1);
3682 int err = 0;
3683
Lukas Czernere1be3a92013-07-01 08:12:39 -04003684 partial_start = lstart & (sb->s_blocksize - 1);
3685 partial_end = byte_end & (sb->s_blocksize - 1);
3686
Lukas Czernera87dd182013-05-27 23:32:35 -04003687 start = lstart >> sb->s_blocksize_bits;
3688 end = byte_end >> sb->s_blocksize_bits;
3689
3690 /* Handle partial zero within the single block */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003691 if (start == end &&
3692 (partial_start || (partial_end != sb->s_blocksize - 1))) {
Lukas Czernera87dd182013-05-27 23:32:35 -04003693 err = ext4_block_zero_page_range(handle, mapping,
3694 lstart, length);
3695 return err;
3696 }
3697 /* Handle partial zero out on the start of the range */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003698 if (partial_start) {
Lukas Czernera87dd182013-05-27 23:32:35 -04003699 err = ext4_block_zero_page_range(handle, mapping,
3700 lstart, sb->s_blocksize);
3701 if (err)
3702 return err;
3703 }
3704 /* Handle partial zero out on the end of the range */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003705 if (partial_end != sb->s_blocksize - 1)
Lukas Czernera87dd182013-05-27 23:32:35 -04003706 err = ext4_block_zero_page_range(handle, mapping,
Lukas Czernere1be3a92013-07-01 08:12:39 -04003707 byte_end - partial_end,
3708 partial_end + 1);
Lukas Czernera87dd182013-05-27 23:32:35 -04003709 return err;
3710}
3711
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003712int ext4_can_truncate(struct inode *inode)
3713{
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003714 if (S_ISREG(inode->i_mode))
3715 return 1;
3716 if (S_ISDIR(inode->i_mode))
3717 return 1;
3718 if (S_ISLNK(inode->i_mode))
3719 return !ext4_inode_is_fast_symlink(inode);
3720 return 0;
3721}
3722
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003723/*
Jan Kara01127842015-12-07 14:34:49 -05003724 * We have to make sure i_disksize gets properly updated before we truncate
3725 * page cache due to hole punching or zero range. Otherwise i_disksize update
3726 * can get lost as it may have been postponed to submission of writeback but
3727 * that will never happen after we truncate page cache.
3728 */
3729int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset,
3730 loff_t len)
3731{
3732 handle_t *handle;
3733 loff_t size = i_size_read(inode);
3734
Al Viro59551022016-01-22 15:40:57 -05003735 WARN_ON(!inode_is_locked(inode));
Jan Kara01127842015-12-07 14:34:49 -05003736 if (offset > size || offset + len < size)
3737 return 0;
3738
3739 if (EXT4_I(inode)->i_disksize >= size)
3740 return 0;
3741
3742 handle = ext4_journal_start(inode, EXT4_HT_MISC, 1);
3743 if (IS_ERR(handle))
3744 return PTR_ERR(handle);
3745 ext4_update_i_disksize(inode, size);
3746 ext4_mark_inode_dirty(handle, inode);
3747 ext4_journal_stop(handle);
3748
3749 return 0;
3750}
3751
3752/*
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003753 * ext4_punch_hole: punches a hole in a file by releaseing the blocks
3754 * associated with the given offset and length
3755 *
3756 * @inode: File inode
3757 * @offset: The offset where the hole will begin
3758 * @len: The length of the hole
3759 *
Anatol Pomozov4907cb72012-09-01 10:31:09 -07003760 * Returns: 0 on success or negative on failure
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003761 */
3762
Ashish Sangwanaeb28172013-07-01 08:12:38 -04003763int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length)
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003764{
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003765 struct super_block *sb = inode->i_sb;
3766 ext4_lblk_t first_block, stop_block;
3767 struct address_space *mapping = inode->i_mapping;
Lukas Czernera87dd182013-05-27 23:32:35 -04003768 loff_t first_block_offset, last_block_offset;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003769 handle_t *handle;
3770 unsigned int credits;
3771 int ret = 0;
3772
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003773 if (!S_ISREG(inode->i_mode))
Allison Henderson73355192012-03-21 22:23:31 -04003774 return -EOPNOTSUPP;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003775
Lukas Czernerb8a86842014-03-18 18:05:35 -04003776 trace_ext4_punch_hole(inode, offset, length, 0);
Zheng Liuaaddea82013-01-16 20:21:26 -05003777
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003778 /*
3779 * Write out all dirty pages to avoid race conditions
3780 * Then release them.
3781 */
3782 if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
3783 ret = filemap_write_and_wait_range(mapping, offset,
3784 offset + length - 1);
3785 if (ret)
3786 return ret;
3787 }
3788
Al Viro59551022016-01-22 15:40:57 -05003789 inode_lock(inode);
Lukas Czerner9ef06ce2014-04-12 09:47:00 -04003790
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003791 /* No need to punch hole beyond i_size */
3792 if (offset >= inode->i_size)
3793 goto out_mutex;
3794
3795 /*
3796 * If the hole extends beyond i_size, set the hole
3797 * to end after the page that contains i_size
3798 */
3799 if (offset + length > inode->i_size) {
3800 length = inode->i_size +
3801 PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) -
3802 offset;
3803 }
3804
Jan Karaa3612932013-08-16 21:19:41 -04003805 if (offset & (sb->s_blocksize - 1) ||
3806 (offset + length) & (sb->s_blocksize - 1)) {
3807 /*
3808 * Attach jinode to inode for jbd2 if we do any zeroing of
3809 * partial block
3810 */
3811 ret = ext4_inode_attach_jinode(inode);
3812 if (ret < 0)
3813 goto out_mutex;
3814
3815 }
3816
Jan Karaea3d7202015-12-07 14:28:03 -05003817 /* Wait all existing dio workers, newcomers will block on i_mutex */
3818 ext4_inode_block_unlocked_dio(inode);
3819 inode_dio_wait(inode);
3820
3821 /*
3822 * Prevent page faults from reinstantiating pages we have released from
3823 * page cache.
3824 */
3825 down_write(&EXT4_I(inode)->i_mmap_sem);
Lukas Czernera87dd182013-05-27 23:32:35 -04003826 first_block_offset = round_up(offset, sb->s_blocksize);
3827 last_block_offset = round_down((offset + length), sb->s_blocksize) - 1;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003828
Lukas Czernera87dd182013-05-27 23:32:35 -04003829 /* Now release the pages and zero block aligned part of pages*/
Jan Kara01127842015-12-07 14:34:49 -05003830 if (last_block_offset > first_block_offset) {
3831 ret = ext4_update_disksize_before_punch(inode, offset, length);
3832 if (ret)
3833 goto out_dio;
Lukas Czernera87dd182013-05-27 23:32:35 -04003834 truncate_pagecache_range(inode, first_block_offset,
3835 last_block_offset);
Jan Kara01127842015-12-07 14:34:49 -05003836 }
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003837
3838 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3839 credits = ext4_writepage_trans_blocks(inode);
3840 else
3841 credits = ext4_blocks_for_truncate(inode);
3842 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
3843 if (IS_ERR(handle)) {
3844 ret = PTR_ERR(handle);
3845 ext4_std_error(sb, ret);
3846 goto out_dio;
3847 }
3848
Lukas Czernera87dd182013-05-27 23:32:35 -04003849 ret = ext4_zero_partial_blocks(handle, inode, offset,
3850 length);
3851 if (ret)
3852 goto out_stop;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003853
3854 first_block = (offset + sb->s_blocksize - 1) >>
3855 EXT4_BLOCK_SIZE_BITS(sb);
3856 stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
3857
3858 /* If there are no blocks to remove, return now */
3859 if (first_block >= stop_block)
3860 goto out_stop;
3861
3862 down_write(&EXT4_I(inode)->i_data_sem);
3863 ext4_discard_preallocations(inode);
3864
3865 ret = ext4_es_remove_extent(inode, first_block,
3866 stop_block - first_block);
3867 if (ret) {
3868 up_write(&EXT4_I(inode)->i_data_sem);
3869 goto out_stop;
3870 }
3871
3872 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3873 ret = ext4_ext_remove_space(inode, first_block,
3874 stop_block - 1);
3875 else
Lukas Czerner4f579ae2014-07-15 06:03:38 -04003876 ret = ext4_ind_remove_space(handle, inode, first_block,
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003877 stop_block);
3878
Theodore Ts'o819c4922013-04-03 12:47:17 -04003879 up_write(&EXT4_I(inode)->i_data_sem);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003880 if (IS_SYNC(inode))
3881 ext4_handle_sync(handle);
Maxim Patlasove251f9b2014-02-20 16:58:05 -05003882
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003883 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3884 ext4_mark_inode_dirty(handle, inode);
3885out_stop:
3886 ext4_journal_stop(handle);
3887out_dio:
Jan Karaea3d7202015-12-07 14:28:03 -05003888 up_write(&EXT4_I(inode)->i_mmap_sem);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003889 ext4_inode_resume_unlocked_dio(inode);
3890out_mutex:
Al Viro59551022016-01-22 15:40:57 -05003891 inode_unlock(inode);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003892 return ret;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003893}
3894
Jan Karaa3612932013-08-16 21:19:41 -04003895int ext4_inode_attach_jinode(struct inode *inode)
3896{
3897 struct ext4_inode_info *ei = EXT4_I(inode);
3898 struct jbd2_inode *jinode;
3899
3900 if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal)
3901 return 0;
3902
3903 jinode = jbd2_alloc_inode(GFP_KERNEL);
3904 spin_lock(&inode->i_lock);
3905 if (!ei->jinode) {
3906 if (!jinode) {
3907 spin_unlock(&inode->i_lock);
3908 return -ENOMEM;
3909 }
3910 ei->jinode = jinode;
3911 jbd2_journal_init_jbd_inode(ei->jinode, inode);
3912 jinode = NULL;
3913 }
3914 spin_unlock(&inode->i_lock);
3915 if (unlikely(jinode != NULL))
3916 jbd2_free_inode(jinode);
3917 return 0;
3918}
3919
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003920/*
Mingming Cao617ba132006-10-11 01:20:53 -07003921 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003922 *
Mingming Cao617ba132006-10-11 01:20:53 -07003923 * We block out ext4_get_block() block instantiations across the entire
3924 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003925 * simultaneously on behalf of the same inode.
3926 *
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003927 * As we work through the truncate and commit bits of it to the journal there
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003928 * is one core, guiding principle: the file's tree must always be consistent on
3929 * disk. We must be able to restart the truncate after a crash.
3930 *
3931 * The file's tree may be transiently inconsistent in memory (although it
3932 * probably isn't), but whenever we close off and commit a journal transaction,
3933 * the contents of (the filesystem + the journal) must be consistent and
3934 * restartable. It's pretty simple, really: bottom up, right to left (although
3935 * left-to-right works OK too).
3936 *
3937 * Note that at recovery time, journal replay occurs *before* the restart of
3938 * truncate against the orphan inode list.
3939 *
3940 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07003941 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003942 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07003943 * ext4_truncate() to have another go. So there will be instantiated blocks
3944 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003945 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07003946 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003947 */
Mingming Cao617ba132006-10-11 01:20:53 -07003948void ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003949{
Theodore Ts'o819c4922013-04-03 12:47:17 -04003950 struct ext4_inode_info *ei = EXT4_I(inode);
3951 unsigned int credits;
3952 handle_t *handle;
3953 struct address_space *mapping = inode->i_mapping;
Theodore Ts'o819c4922013-04-03 12:47:17 -04003954
Theodore Ts'o19b5ef62013-04-03 21:58:52 -04003955 /*
3956 * There is a possibility that we're either freeing the inode
Matthew Wilcoxe04027e2014-03-24 15:15:07 -04003957 * or it's a completely new inode. In those cases we might not
Theodore Ts'o19b5ef62013-04-03 21:58:52 -04003958 * have i_mutex locked because it's not necessary.
3959 */
3960 if (!(inode->i_state & (I_NEW|I_FREEING)))
Al Viro59551022016-01-22 15:40:57 -05003961 WARN_ON(!inode_is_locked(inode));
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003962 trace_ext4_truncate_enter(inode);
3963
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003964 if (!ext4_can_truncate(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003965 return;
3966
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003967 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003968
Theodore Ts'o5534fb52009-09-17 09:34:16 -04003969 if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003970 ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
Theodore Ts'o7d8f9f72009-02-24 08:21:14 -05003971
Tao Maaef1c852012-12-10 14:06:02 -05003972 if (ext4_has_inline_data(inode)) {
3973 int has_inline = 1;
3974
3975 ext4_inline_data_truncate(inode, &has_inline);
3976 if (has_inline)
3977 return;
3978 }
3979
Jan Karaa3612932013-08-16 21:19:41 -04003980 /* If we zero-out tail of the page, we have to create jinode for jbd2 */
3981 if (inode->i_size & (inode->i_sb->s_blocksize - 1)) {
3982 if (ext4_inode_attach_jinode(inode) < 0)
3983 return;
3984 }
3985
Amir Goldsteinff9893d2011-06-27 16:36:31 -04003986 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Theodore Ts'o819c4922013-04-03 12:47:17 -04003987 credits = ext4_writepage_trans_blocks(inode);
Amir Goldsteinff9893d2011-06-27 16:36:31 -04003988 else
Theodore Ts'o819c4922013-04-03 12:47:17 -04003989 credits = ext4_blocks_for_truncate(inode);
3990
3991 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
3992 if (IS_ERR(handle)) {
3993 ext4_std_error(inode->i_sb, PTR_ERR(handle));
3994 return;
3995 }
3996
Lukas Czernereb3544c2013-05-27 23:32:35 -04003997 if (inode->i_size & (inode->i_sb->s_blocksize - 1))
3998 ext4_block_truncate_page(handle, mapping, inode->i_size);
Theodore Ts'o819c4922013-04-03 12:47:17 -04003999
4000 /*
4001 * We add the inode to the orphan list, so that if this
4002 * truncate spans multiple transactions, and we crash, we will
4003 * resume the truncate when the filesystem recovers. It also
4004 * marks the inode dirty, to catch the new size.
4005 *
4006 * Implication: the file must always be in a sane, consistent
4007 * truncatable state while each transaction commits.
4008 */
4009 if (ext4_orphan_add(handle, inode))
4010 goto out_stop;
4011
4012 down_write(&EXT4_I(inode)->i_data_sem);
4013
4014 ext4_discard_preallocations(inode);
4015
4016 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
4017 ext4_ext_truncate(handle, inode);
4018 else
4019 ext4_ind_truncate(handle, inode);
4020
4021 up_write(&ei->i_data_sem);
4022
4023 if (IS_SYNC(inode))
4024 ext4_handle_sync(handle);
4025
4026out_stop:
4027 /*
4028 * If this was a simple ftruncate() and the file will remain alive,
4029 * then we need to clear up the orphan record which we created above.
4030 * However, if this was a real unlink then we were called by
Wang Shilong58d86a52014-11-25 16:17:29 -05004031 * ext4_evict_inode(), and we allow that function to clean up the
Theodore Ts'o819c4922013-04-03 12:47:17 -04004032 * orphan info for us.
4033 */
4034 if (inode->i_nlink)
4035 ext4_orphan_del(handle, inode);
4036
4037 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
4038 ext4_mark_inode_dirty(handle, inode);
4039 ext4_journal_stop(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07004040
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004041 trace_ext4_truncate_exit(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004042}
4043
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004044/*
Mingming Cao617ba132006-10-11 01:20:53 -07004045 * ext4_get_inode_loc returns with an extra refcount against the inode's
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004046 * underlying buffer_head on success. If 'in_mem' is true, we have all
4047 * data in memory that is needed to recreate the on-disk version of this
4048 * inode.
4049 */
Mingming Cao617ba132006-10-11 01:20:53 -07004050static int __ext4_get_inode_loc(struct inode *inode,
4051 struct ext4_iloc *iloc, int in_mem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004052{
Theodore Ts'o240799c2008-10-09 23:53:47 -04004053 struct ext4_group_desc *gdp;
4054 struct buffer_head *bh;
4055 struct super_block *sb = inode->i_sb;
4056 ext4_fsblk_t block;
4057 int inodes_per_block, inode_offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004058
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05004059 iloc->bh = NULL;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004060 if (!ext4_valid_inum(sb, inode->i_ino))
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004061 return -EFSCORRUPTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004062
Theodore Ts'o240799c2008-10-09 23:53:47 -04004063 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
4064 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
4065 if (!gdp)
4066 return -EIO;
4067
4068 /*
4069 * Figure out the offset within the block group inode table
4070 */
Tao Ma00d09882011-05-09 10:26:41 -04004071 inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004072 inode_offset = ((inode->i_ino - 1) %
4073 EXT4_INODES_PER_GROUP(sb));
4074 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
4075 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
4076
4077 bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05004078 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -05004079 return -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004080 if (!buffer_uptodate(bh)) {
4081 lock_buffer(bh);
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04004082
4083 /*
4084 * If the buffer has the write error flag, we have failed
4085 * to write out another inode in the same block. In this
4086 * case, we don't have to read the block because we may
4087 * read the old inode data successfully.
4088 */
4089 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
4090 set_buffer_uptodate(bh);
4091
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004092 if (buffer_uptodate(bh)) {
4093 /* someone brought it uptodate while we waited */
4094 unlock_buffer(bh);
4095 goto has_buffer;
4096 }
4097
4098 /*
4099 * If we have all information of the inode in memory and this
4100 * is the only valid inode in the block, we need not read the
4101 * block.
4102 */
4103 if (in_mem) {
4104 struct buffer_head *bitmap_bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004105 int i, start;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004106
Theodore Ts'o240799c2008-10-09 23:53:47 -04004107 start = inode_offset & ~(inodes_per_block - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004108
4109 /* Is the inode bitmap in cache? */
Theodore Ts'o240799c2008-10-09 23:53:47 -04004110 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
Wang Shilongaebf0242013-01-12 16:28:47 -05004111 if (unlikely(!bitmap_bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004112 goto make_io;
4113
4114 /*
4115 * If the inode bitmap isn't in cache then the
4116 * optimisation may end up performing two reads instead
4117 * of one, so skip it.
4118 */
4119 if (!buffer_uptodate(bitmap_bh)) {
4120 brelse(bitmap_bh);
4121 goto make_io;
4122 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04004123 for (i = start; i < start + inodes_per_block; i++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004124 if (i == inode_offset)
4125 continue;
Mingming Cao617ba132006-10-11 01:20:53 -07004126 if (ext4_test_bit(i, bitmap_bh->b_data))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004127 break;
4128 }
4129 brelse(bitmap_bh);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004130 if (i == start + inodes_per_block) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004131 /* all other inodes are free, so skip I/O */
4132 memset(bh->b_data, 0, bh->b_size);
4133 set_buffer_uptodate(bh);
4134 unlock_buffer(bh);
4135 goto has_buffer;
4136 }
4137 }
4138
4139make_io:
4140 /*
Theodore Ts'o240799c2008-10-09 23:53:47 -04004141 * If we need to do any I/O, try to pre-readahead extra
4142 * blocks from the inode table.
4143 */
4144 if (EXT4_SB(sb)->s_inode_readahead_blks) {
4145 ext4_fsblk_t b, end, table;
4146 unsigned num;
Theodore Ts'o0d606e22013-04-23 08:59:35 -04004147 __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004148
4149 table = ext4_inode_table(sb, gdp);
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04004150 /* s_inode_readahead_blks is always a power of 2 */
Theodore Ts'o0d606e22013-04-23 08:59:35 -04004151 b = block & ~((ext4_fsblk_t) ra_blks - 1);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004152 if (table > b)
4153 b = table;
Theodore Ts'o0d606e22013-04-23 08:59:35 -04004154 end = b + ra_blks;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004155 num = EXT4_INODES_PER_GROUP(sb);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04004156 if (ext4_has_group_desc_csum(sb))
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05004157 num -= ext4_itable_unused_count(sb, gdp);
Theodore Ts'o240799c2008-10-09 23:53:47 -04004158 table += num / inodes_per_block;
4159 if (end > table)
4160 end = table;
4161 while (b <= end)
4162 sb_breadahead(sb, b++);
4163 }
4164
4165 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004166 * There are other valid inodes in the buffer, this inode
4167 * has in-inode xattrs, or we don't have this inode in memory.
4168 * Read the block from disk.
4169 */
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04004170 trace_ext4_load_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004171 get_bh(bh);
4172 bh->b_end_io = end_buffer_read_sync;
Christoph Hellwig65299a32011-08-23 14:50:29 +02004173 submit_bh(READ | REQ_META | REQ_PRIO, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004174 wait_on_buffer(bh);
4175 if (!buffer_uptodate(bh)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -04004176 EXT4_ERROR_INODE_BLOCK(inode, block,
4177 "unable to read itable block");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004178 brelse(bh);
4179 return -EIO;
4180 }
4181 }
4182has_buffer:
4183 iloc->bh = bh;
4184 return 0;
4185}
4186
Mingming Cao617ba132006-10-11 01:20:53 -07004187int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004188{
4189 /* We have all inode data except xattrs in memory here. */
Mingming Cao617ba132006-10-11 01:20:53 -07004190 return __ext4_get_inode_loc(inode, iloc,
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004191 !ext4_test_inode_state(inode, EXT4_STATE_XATTR));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004192}
4193
Mingming Cao617ba132006-10-11 01:20:53 -07004194void ext4_set_inode_flags(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004195{
Mingming Cao617ba132006-10-11 01:20:53 -07004196 unsigned int flags = EXT4_I(inode)->i_flags;
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004197 unsigned int new_fl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004198
Mingming Cao617ba132006-10-11 01:20:53 -07004199 if (flags & EXT4_SYNC_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004200 new_fl |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07004201 if (flags & EXT4_APPEND_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004202 new_fl |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07004203 if (flags & EXT4_IMMUTABLE_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004204 new_fl |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07004205 if (flags & EXT4_NOATIME_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004206 new_fl |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07004207 if (flags & EXT4_DIRSYNC_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04004208 new_fl |= S_DIRSYNC;
Ross Zwisler923ae0f2015-02-16 15:59:38 -08004209 if (test_opt(inode->i_sb, DAX))
4210 new_fl |= S_DAX;
Theodore Ts'o5f16f322014-03-24 14:43:12 -04004211 inode_set_flags(inode, new_fl,
Ross Zwisler923ae0f2015-02-16 15:59:38 -08004212 S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004213}
4214
Jan Karaff9ddf72007-07-18 09:24:20 -04004215/* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
4216void ext4_get_inode_flags(struct ext4_inode_info *ei)
4217{
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004218 unsigned int vfs_fl;
4219 unsigned long old_fl, new_fl;
Jan Karaff9ddf72007-07-18 09:24:20 -04004220
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004221 do {
4222 vfs_fl = ei->vfs_inode.i_flags;
4223 old_fl = ei->i_flags;
4224 new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
4225 EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|
4226 EXT4_DIRSYNC_FL);
4227 if (vfs_fl & S_SYNC)
4228 new_fl |= EXT4_SYNC_FL;
4229 if (vfs_fl & S_APPEND)
4230 new_fl |= EXT4_APPEND_FL;
4231 if (vfs_fl & S_IMMUTABLE)
4232 new_fl |= EXT4_IMMUTABLE_FL;
4233 if (vfs_fl & S_NOATIME)
4234 new_fl |= EXT4_NOATIME_FL;
4235 if (vfs_fl & S_DIRSYNC)
4236 new_fl |= EXT4_DIRSYNC_FL;
4237 } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl);
Jan Karaff9ddf72007-07-18 09:24:20 -04004238}
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004239
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004240static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004241 struct ext4_inode_info *ei)
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004242{
4243 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004244 struct inode *inode = &(ei->vfs_inode);
4245 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004246
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004247 if (ext4_has_feature_huge_file(sb)) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004248 /* we are using combined 48 bit field */
4249 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
4250 le32_to_cpu(raw_inode->i_blocks_lo);
Theodore Ts'o07a03822010-06-14 09:54:48 -04004251 if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004252 /* i_blocks represent file system block size */
4253 return i_blocks << (inode->i_blkbits - 9);
4254 } else {
4255 return i_blocks;
4256 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004257 } else {
4258 return le32_to_cpu(raw_inode->i_blocks_lo);
4259 }
4260}
Jan Karaff9ddf72007-07-18 09:24:20 -04004261
Tao Ma152a7b02012-12-02 11:13:24 -05004262static inline void ext4_iget_extra_inode(struct inode *inode,
4263 struct ext4_inode *raw_inode,
4264 struct ext4_inode_info *ei)
4265{
4266 __le32 *magic = (void *)raw_inode +
4267 EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
Tao Ma67cf5b02012-12-10 14:04:46 -05004268 if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
Tao Ma152a7b02012-12-02 11:13:24 -05004269 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Tao Ma67cf5b02012-12-10 14:04:46 -05004270 ext4_find_inline_data_nolock(inode);
Tao Maf19d5872012-12-10 14:05:51 -05004271 } else
4272 EXT4_I(inode)->i_inline_off = 0;
Tao Ma152a7b02012-12-02 11:13:24 -05004273}
4274
Li Xi040cb372016-01-08 16:01:21 -05004275int ext4_get_projid(struct inode *inode, kprojid_t *projid)
4276{
4277 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, EXT4_FEATURE_RO_COMPAT_PROJECT))
4278 return -EOPNOTSUPP;
4279 *projid = EXT4_I(inode)->i_projid;
4280 return 0;
4281}
4282
David Howells1d1fe1e2008-02-07 00:15:37 -08004283struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004284{
Mingming Cao617ba132006-10-11 01:20:53 -07004285 struct ext4_iloc iloc;
4286 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08004287 struct ext4_inode_info *ei;
David Howells1d1fe1e2008-02-07 00:15:37 -08004288 struct inode *inode;
Jan Karab436b9b2009-12-08 23:51:10 -05004289 journal_t *journal = EXT4_SB(sb)->s_journal;
David Howells1d1fe1e2008-02-07 00:15:37 -08004290 long ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004291 int block;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004292 uid_t i_uid;
4293 gid_t i_gid;
Li Xi040cb372016-01-08 16:01:21 -05004294 projid_t i_projid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004295
David Howells1d1fe1e2008-02-07 00:15:37 -08004296 inode = iget_locked(sb, ino);
4297 if (!inode)
4298 return ERR_PTR(-ENOMEM);
4299 if (!(inode->i_state & I_NEW))
4300 return inode;
4301
4302 ei = EXT4_I(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05004303 iloc.bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004304
David Howells1d1fe1e2008-02-07 00:15:37 -08004305 ret = __ext4_get_inode_loc(inode, &iloc, 0);
4306 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004307 goto bad_inode;
Mingming Cao617ba132006-10-11 01:20:53 -07004308 raw_inode = ext4_raw_inode(&iloc);
Darrick J. Wong814525f2012-04-29 18:31:10 -04004309
4310 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4311 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
4312 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
4313 EXT4_INODE_SIZE(inode->i_sb)) {
4314 EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)",
4315 EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize,
4316 EXT4_INODE_SIZE(inode->i_sb));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004317 ret = -EFSCORRUPTED;
Darrick J. Wong814525f2012-04-29 18:31:10 -04004318 goto bad_inode;
4319 }
4320 } else
4321 ei->i_extra_isize = 0;
4322
4323 /* Precompute checksum seed for inode metadata */
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04004324 if (ext4_has_metadata_csum(sb)) {
Darrick J. Wong814525f2012-04-29 18:31:10 -04004325 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4326 __u32 csum;
4327 __le32 inum = cpu_to_le32(inode->i_ino);
4328 __le32 gen = raw_inode->i_generation;
4329 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
4330 sizeof(inum));
4331 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
4332 sizeof(gen));
4333 }
4334
4335 if (!ext4_inode_csum_verify(inode, raw_inode, ei)) {
4336 EXT4_ERROR_INODE(inode, "checksum invalid");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004337 ret = -EFSBADCRC;
Darrick J. Wong814525f2012-04-29 18:31:10 -04004338 goto bad_inode;
4339 }
4340
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004341 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004342 i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4343 i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Li Xi040cb372016-01-08 16:01:21 -05004344 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_PROJECT) &&
4345 EXT4_INODE_SIZE(sb) > EXT4_GOOD_OLD_INODE_SIZE &&
4346 EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
4347 i_projid = (projid_t)le32_to_cpu(raw_inode->i_projid);
4348 else
4349 i_projid = EXT4_DEF_PROJID;
4350
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004351 if (!(test_opt(inode->i_sb, NO_UID32))) {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004352 i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4353 i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004354 }
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004355 i_uid_write(inode, i_uid);
4356 i_gid_write(inode, i_gid);
Li Xi040cb372016-01-08 16:01:21 -05004357 ei->i_projid = make_kprojid(&init_user_ns, i_projid);
Miklos Szeredibfe86842011-10-28 14:13:29 +02004358 set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004359
Theodore Ts'o353eb832011-01-10 12:18:25 -05004360 ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
Tao Ma67cf5b02012-12-10 14:04:46 -05004361 ei->i_inline_off = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004362 ei->i_dir_start_lookup = 0;
4363 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4364 /* We now have enough fields to check if the inode was active or not.
4365 * This is needed because nfsd might try to access dead inodes
4366 * the test is that same one that e2fsck uses
4367 * NeilBrown 1999oct15
4368 */
4369 if (inode->i_nlink == 0) {
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004370 if ((inode->i_mode == 0 ||
4371 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) &&
4372 ino != EXT4_BOOT_LOADER_INO) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004373 /* this inode is deleted */
David Howells1d1fe1e2008-02-07 00:15:37 -08004374 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004375 goto bad_inode;
4376 }
4377 /* The only unlinked inodes we let through here have
4378 * valid i_mode and are being read by the orphan
4379 * recovery code: that's fine, we're about to complete
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004380 * the process of deleting those.
4381 * OR it is the EXT4_BOOT_LOADER_INO which is
4382 * not initialized on a new filesystem. */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004383 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004384 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004385 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004386 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004387 if (ext4_has_feature_64bit(sb))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004388 ei->i_file_acl |=
4389 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004390 inode->i_size = ext4_isize(raw_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004391 ei->i_disksize = inode->i_size;
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03004392#ifdef CONFIG_QUOTA
4393 ei->i_reserved_quota = 0;
4394#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004395 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4396 ei->i_block_group = iloc.block_group;
Theodore Ts'oa4912122009-03-12 12:18:34 -04004397 ei->i_last_alloc_group = ~0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004398 /*
4399 * NOTE! The in-memory inode i_data array is in little-endian order
4400 * even on big-endian machines: we do NOT byteswap the block numbers!
4401 */
Mingming Cao617ba132006-10-11 01:20:53 -07004402 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004403 ei->i_data[block] = raw_inode->i_block[block];
4404 INIT_LIST_HEAD(&ei->i_orphan);
4405
Jan Karab436b9b2009-12-08 23:51:10 -05004406 /*
4407 * Set transaction id's of transactions that have to be committed
4408 * to finish f[data]sync. We set them to currently running transaction
4409 * as we cannot be sure that the inode or some of its metadata isn't
4410 * part of the transaction - the inode could have been reclaimed and
4411 * now it is reread from disk.
4412 */
4413 if (journal) {
4414 transaction_t *transaction;
4415 tid_t tid;
4416
Theodore Ts'oa931da62010-08-03 21:35:12 -04004417 read_lock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05004418 if (journal->j_running_transaction)
4419 transaction = journal->j_running_transaction;
4420 else
4421 transaction = journal->j_committing_transaction;
4422 if (transaction)
4423 tid = transaction->t_tid;
4424 else
4425 tid = journal->j_commit_sequence;
Theodore Ts'oa931da62010-08-03 21:35:12 -04004426 read_unlock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05004427 ei->i_sync_tid = tid;
4428 ei->i_datasync_tid = tid;
4429 }
4430
Eric Sandeen0040d982008-02-05 22:36:43 -05004431 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004432 if (ei->i_extra_isize == 0) {
4433 /* The extra space is currently unused. Use it. */
Mingming Cao617ba132006-10-11 01:20:53 -07004434 ei->i_extra_isize = sizeof(struct ext4_inode) -
4435 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004436 } else {
Tao Ma152a7b02012-12-02 11:13:24 -05004437 ext4_iget_extra_inode(inode, raw_inode, ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004438 }
Darrick J. Wong814525f2012-04-29 18:31:10 -04004439 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004440
Kalpak Shahef7f3832007-07-18 09:15:20 -04004441 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4442 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4443 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4444 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4445
Theodore Ts'oed3654e2014-03-24 14:09:06 -04004446 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
Theodore Ts'oc4f65702014-03-20 00:32:57 -04004447 inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
4448 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4449 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4450 inode->i_version |=
4451 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
4452 }
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004453 }
4454
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004455 ret = 0;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004456 if (ei->i_file_acl &&
Theodore Ts'o10329882009-11-15 15:29:56 -05004457 !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04004458 EXT4_ERROR_INODE(inode, "bad extended attribute block %llu",
4459 ei->i_file_acl);
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004460 ret = -EFSCORRUPTED;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004461 goto bad_inode;
Tao Maf19d5872012-12-10 14:05:51 -05004462 } else if (!ext4_has_inline_data(inode)) {
4463 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
4464 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4465 (S_ISLNK(inode->i_mode) &&
4466 !ext4_inode_is_fast_symlink(inode))))
4467 /* Validate extent which is part of inode */
4468 ret = ext4_ext_check_inode(inode);
4469 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4470 (S_ISLNK(inode->i_mode) &&
4471 !ext4_inode_is_fast_symlink(inode))) {
4472 /* Validate block references which are part of inode */
4473 ret = ext4_ind_check_inode(inode);
4474 }
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004475 }
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004476 if (ret)
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004477 goto bad_inode;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -04004478
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004479 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004480 inode->i_op = &ext4_file_inode_operations;
Boaz Harroshbe64f882015-04-15 16:15:17 -07004481 inode->i_fop = &ext4_file_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07004482 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004483 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004484 inode->i_op = &ext4_dir_inode_operations;
4485 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004486 } else if (S_ISLNK(inode->i_mode)) {
Al Viroa7a67e82015-04-27 17:51:30 -04004487 if (ext4_encrypted_inode(inode)) {
4488 inode->i_op = &ext4_encrypted_symlink_inode_operations;
4489 ext4_set_aops(inode);
4490 } else if (ext4_inode_is_fast_symlink(inode)) {
Al Viro75e75662015-05-02 10:13:58 -04004491 inode->i_link = (char *)ei->i_data;
Mingming Cao617ba132006-10-11 01:20:53 -07004492 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00004493 nd_terminate_link(ei->i_data, inode->i_size,
4494 sizeof(ei->i_data) - 1);
4495 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004496 inode->i_op = &ext4_symlink_inode_operations;
4497 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004498 }
Al Viro21fc61c2015-11-17 01:07:57 -05004499 inode_nohighmem(inode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004500 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
4501 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004502 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004503 if (raw_inode->i_block[0])
4504 init_special_inode(inode, inode->i_mode,
4505 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4506 else
4507 init_special_inode(inode, inode->i_mode,
4508 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004509 } else if (ino == EXT4_BOOT_LOADER_INO) {
4510 make_bad_inode(inode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004511 } else {
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004512 ret = -EFSCORRUPTED;
Theodore Ts'o24676da2010-05-16 21:00:00 -04004513 EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004514 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004515 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004516 brelse(iloc.bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004517 ext4_set_inode_flags(inode);
David Howells1d1fe1e2008-02-07 00:15:37 -08004518 unlock_new_inode(inode);
4519 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004520
4521bad_inode:
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004522 brelse(iloc.bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004523 iget_failed(inode);
4524 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004525}
4526
Theodore Ts'of4bb2982014-10-05 22:56:00 -04004527struct inode *ext4_iget_normal(struct super_block *sb, unsigned long ino)
4528{
4529 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004530 return ERR_PTR(-EFSCORRUPTED);
Theodore Ts'of4bb2982014-10-05 22:56:00 -04004531 return ext4_iget(sb, ino);
4532}
4533
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004534static int ext4_inode_blocks_set(handle_t *handle,
4535 struct ext4_inode *raw_inode,
4536 struct ext4_inode_info *ei)
4537{
4538 struct inode *inode = &(ei->vfs_inode);
4539 u64 i_blocks = inode->i_blocks;
4540 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004541
4542 if (i_blocks <= ~0U) {
4543 /*
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004544 * i_blocks can be represented in a 32 bit variable
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004545 * as multiple of 512 bytes
4546 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004547 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004548 raw_inode->i_blocks_high = 0;
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004549 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004550 return 0;
4551 }
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004552 if (!ext4_has_feature_huge_file(sb))
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004553 return -EFBIG;
4554
4555 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004556 /*
4557 * i_blocks can be represented in a 48 bit variable
4558 * as multiple of 512 bytes
4559 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004560 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004561 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004562 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004563 } else {
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004564 ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004565 /* i_block is stored in file system block size */
4566 i_blocks = i_blocks >> (inode->i_blkbits - 9);
4567 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4568 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004569 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004570 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004571}
4572
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004573struct other_inode {
4574 unsigned long orig_ino;
4575 struct ext4_inode *raw_inode;
4576};
4577
4578static int other_inode_match(struct inode * inode, unsigned long ino,
4579 void *data)
4580{
4581 struct other_inode *oi = (struct other_inode *) data;
4582
4583 if ((inode->i_ino != ino) ||
4584 (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW |
4585 I_DIRTY_SYNC | I_DIRTY_DATASYNC)) ||
4586 ((inode->i_state & I_DIRTY_TIME) == 0))
4587 return 0;
4588 spin_lock(&inode->i_lock);
4589 if (((inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW |
4590 I_DIRTY_SYNC | I_DIRTY_DATASYNC)) == 0) &&
4591 (inode->i_state & I_DIRTY_TIME)) {
4592 struct ext4_inode_info *ei = EXT4_I(inode);
4593
4594 inode->i_state &= ~(I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED);
4595 spin_unlock(&inode->i_lock);
4596
4597 spin_lock(&ei->i_raw_lock);
4598 EXT4_INODE_SET_XTIME(i_ctime, inode, oi->raw_inode);
4599 EXT4_INODE_SET_XTIME(i_mtime, inode, oi->raw_inode);
4600 EXT4_INODE_SET_XTIME(i_atime, inode, oi->raw_inode);
4601 ext4_inode_csum_set(inode, oi->raw_inode, ei);
4602 spin_unlock(&ei->i_raw_lock);
4603 trace_ext4_other_inode_update_time(inode, oi->orig_ino);
4604 return -1;
4605 }
4606 spin_unlock(&inode->i_lock);
4607 return -1;
4608}
4609
4610/*
4611 * Opportunistically update the other time fields for other inodes in
4612 * the same inode table block.
4613 */
4614static void ext4_update_other_inodes_time(struct super_block *sb,
4615 unsigned long orig_ino, char *buf)
4616{
4617 struct other_inode oi;
4618 unsigned long ino;
4619 int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
4620 int inode_size = EXT4_INODE_SIZE(sb);
4621
4622 oi.orig_ino = orig_ino;
Theodore Ts'o0f0ff9a2015-07-01 23:37:46 -04004623 /*
4624 * Calculate the first inode in the inode table block. Inode
4625 * numbers are one-based. That is, the first inode in a block
4626 * (assuming 4k blocks and 256 byte inodes) is (n*16 + 1).
4627 */
4628 ino = ((orig_ino - 1) & ~(inodes_per_block - 1)) + 1;
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004629 for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) {
4630 if (ino == orig_ino)
4631 continue;
4632 oi.raw_inode = (struct ext4_inode *) buf;
4633 (void) find_inode_nowait(sb, ino, other_inode_match, &oi);
4634 }
4635}
4636
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004637/*
4638 * Post the struct inode info into an on-disk inode location in the
4639 * buffer-cache. This gobbles the caller's reference to the
4640 * buffer_head in the inode location struct.
4641 *
4642 * The caller must have write access to iloc->bh.
4643 */
Mingming Cao617ba132006-10-11 01:20:53 -07004644static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004645 struct inode *inode,
Frank Mayhar830156c2009-09-29 10:07:47 -04004646 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004647{
Mingming Cao617ba132006-10-11 01:20:53 -07004648 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
4649 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004650 struct buffer_head *bh = iloc->bh;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004651 struct super_block *sb = inode->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004652 int err = 0, rc, block;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004653 int need_datasync = 0, set_large_file = 0;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004654 uid_t i_uid;
4655 gid_t i_gid;
Li Xi040cb372016-01-08 16:01:21 -05004656 projid_t i_projid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004657
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004658 spin_lock(&ei->i_raw_lock);
4659
4660 /* For fields not tracked in the in-memory inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004661 * initialise them to zero for new inodes. */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004662 if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
Mingming Cao617ba132006-10-11 01:20:53 -07004663 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004664
Jan Karaff9ddf72007-07-18 09:24:20 -04004665 ext4_get_inode_flags(ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004666 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004667 i_uid = i_uid_read(inode);
4668 i_gid = i_gid_read(inode);
Li Xi040cb372016-01-08 16:01:21 -05004669 i_projid = from_kprojid(&init_user_ns, ei->i_projid);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004670 if (!(test_opt(inode->i_sb, NO_UID32))) {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004671 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid));
4672 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004673/*
4674 * Fix up interoperability with old kernels. Otherwise, old inodes get
4675 * re-used with the upper 16 bits of the uid/gid intact
4676 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004677 if (!ei->i_dtime) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004678 raw_inode->i_uid_high =
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004679 cpu_to_le16(high_16_bits(i_uid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004680 raw_inode->i_gid_high =
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004681 cpu_to_le16(high_16_bits(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004682 } else {
4683 raw_inode->i_uid_high = 0;
4684 raw_inode->i_gid_high = 0;
4685 }
4686 } else {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004687 raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid));
4688 raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004689 raw_inode->i_uid_high = 0;
4690 raw_inode->i_gid_high = 0;
4691 }
4692 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004693
4694 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
4695 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
4696 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
4697 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
4698
Li Xibce92d52014-10-01 22:11:06 -04004699 err = ext4_inode_blocks_set(handle, raw_inode, ei);
4700 if (err) {
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004701 spin_unlock(&ei->i_raw_lock);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004702 goto out_brelse;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004703 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004704 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Theodore Ts'o353eb832011-01-10 12:18:25 -05004705 raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
Theodore Ts'oed3654e2014-03-24 14:09:06 -04004706 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT)))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004707 raw_inode->i_file_acl_high =
4708 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004709 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Jan Karab71fc072012-09-26 21:52:20 -04004710 if (ei->i_disksize != ext4_isize(raw_inode)) {
4711 ext4_isize_set(raw_inode, ei->i_disksize);
4712 need_datasync = 1;
4713 }
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004714 if (ei->i_disksize > 0x7fffffffULL) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004715 if (!ext4_has_feature_large_file(sb) ||
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004716 EXT4_SB(sb)->s_es->s_rev_level ==
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004717 cpu_to_le32(EXT4_GOOD_OLD_REV))
4718 set_large_file = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004719 }
4720 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
4721 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
4722 if (old_valid_dev(inode->i_rdev)) {
4723 raw_inode->i_block[0] =
4724 cpu_to_le32(old_encode_dev(inode->i_rdev));
4725 raw_inode->i_block[1] = 0;
4726 } else {
4727 raw_inode->i_block[0] = 0;
4728 raw_inode->i_block[1] =
4729 cpu_to_le32(new_encode_dev(inode->i_rdev));
4730 raw_inode->i_block[2] = 0;
4731 }
Tao Maf19d5872012-12-10 14:05:51 -05004732 } else if (!ext4_has_inline_data(inode)) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004733 for (block = 0; block < EXT4_N_BLOCKS; block++)
4734 raw_inode->i_block[block] = ei->i_data[block];
Tao Maf19d5872012-12-10 14:05:51 -05004735 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004736
Theodore Ts'oed3654e2014-03-24 14:09:06 -04004737 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
Theodore Ts'oc4f65702014-03-20 00:32:57 -04004738 raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
4739 if (ei->i_extra_isize) {
4740 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4741 raw_inode->i_version_hi =
4742 cpu_to_le32(inode->i_version >> 32);
4743 raw_inode->i_extra_isize =
4744 cpu_to_le16(ei->i_extra_isize);
4745 }
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004746 }
Li Xi040cb372016-01-08 16:01:21 -05004747
4748 BUG_ON(!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
4749 EXT4_FEATURE_RO_COMPAT_PROJECT) &&
4750 i_projid != EXT4_DEF_PROJID);
4751
4752 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
4753 EXT4_FITS_IN_INODE(raw_inode, ei, i_projid))
4754 raw_inode->i_projid = cpu_to_le32(i_projid);
4755
Darrick J. Wong814525f2012-04-29 18:31:10 -04004756 ext4_inode_csum_set(inode, raw_inode, ei);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004757 spin_unlock(&ei->i_raw_lock);
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004758 if (inode->i_sb->s_flags & MS_LAZYTIME)
4759 ext4_update_other_inodes_time(inode->i_sb, inode->i_ino,
4760 bh->b_data);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004761
Frank Mayhar830156c2009-09-29 10:07:47 -04004762 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05004763 rc = ext4_handle_dirty_metadata(handle, NULL, bh);
Frank Mayhar830156c2009-09-29 10:07:47 -04004764 if (!err)
4765 err = rc;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004766 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004767 if (set_large_file) {
liang xie5d601252014-05-12 22:06:43 -04004768 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access");
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004769 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
4770 if (err)
4771 goto out_brelse;
4772 ext4_update_dynamic_rev(sb);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004773 ext4_set_feature_large_file(sb);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004774 ext4_handle_sync(handle);
4775 err = ext4_handle_dirty_super(handle, sb);
4776 }
Jan Karab71fc072012-09-26 21:52:20 -04004777 ext4_update_inode_fsync_trans(handle, inode, need_datasync);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004778out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004779 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004780 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004781 return err;
4782}
4783
4784/*
Mingming Cao617ba132006-10-11 01:20:53 -07004785 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004786 *
4787 * We are called from a few places:
4788 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04004789 * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004790 * Here, there will be no transaction running. We wait for any running
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004791 * transaction to commit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004792 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04004793 * - Within flush work (sys_sync(), kupdate and such).
4794 * We wait on commit, if told to.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004795 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04004796 * - Within iput_final() -> write_inode_now()
4797 * We wait on commit, if told to.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004798 *
4799 * In all cases it is actually safe for us to return without doing anything,
4800 * because the inode has been copied into a raw inode buffer in
Theodore Ts'o87f7e412014-04-08 11:38:28 -04004801 * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL
4802 * writeback.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004803 *
4804 * Note that we are absolutely dependent upon all inode dirtiers doing the
4805 * right thing: they *must* call mark_inode_dirty() after dirtying info in
4806 * which we are interested.
4807 *
4808 * It would be a bug for them to not do this. The code:
4809 *
4810 * mark_inode_dirty(inode)
4811 * stuff();
4812 * inode->i_size = expr;
4813 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04004814 * is in error because write_inode() could occur while `stuff()' is running,
4815 * and the new i_size will be lost. Plus the inode will no longer be on the
4816 * superblock's dirty inode list.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004817 */
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004818int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004819{
Frank Mayhar91ac6f42009-09-09 22:33:47 -04004820 int err;
4821
Theodore Ts'o87f7e412014-04-08 11:38:28 -04004822 if (WARN_ON_ONCE(current->flags & PF_MEMALLOC))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004823 return 0;
4824
Frank Mayhar91ac6f42009-09-09 22:33:47 -04004825 if (EXT4_SB(inode->i_sb)->s_journal) {
4826 if (ext4_journal_current_handle()) {
4827 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
4828 dump_stack();
4829 return -EIO;
4830 }
4831
Jan Kara10542c22014-03-04 10:50:50 -05004832 /*
4833 * No need to force transaction in WB_SYNC_NONE mode. Also
4834 * ext4_sync_fs() will force the commit after everything is
4835 * written.
4836 */
4837 if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync)
Frank Mayhar91ac6f42009-09-09 22:33:47 -04004838 return 0;
4839
4840 err = ext4_force_commit(inode->i_sb);
4841 } else {
4842 struct ext4_iloc iloc;
4843
Curt Wohlgemuth8b472d72010-04-03 16:45:06 -04004844 err = __ext4_get_inode_loc(inode, &iloc, 0);
Frank Mayhar91ac6f42009-09-09 22:33:47 -04004845 if (err)
4846 return err;
Jan Kara10542c22014-03-04 10:50:50 -05004847 /*
4848 * sync(2) will flush the whole buffer cache. No need to do
4849 * it here separately for each inode.
4850 */
4851 if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
Frank Mayhar830156c2009-09-29 10:07:47 -04004852 sync_dirty_buffer(iloc.bh);
4853 if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -04004854 EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr,
4855 "IO error syncing inode");
Frank Mayhar830156c2009-09-29 10:07:47 -04004856 err = -EIO;
4857 }
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004858 brelse(iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004859 }
Frank Mayhar91ac6f42009-09-09 22:33:47 -04004860 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004861}
4862
4863/*
Jan Kara53e87262012-12-25 13:29:52 -05004864 * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
4865 * buffers that are attached to a page stradding i_size and are undergoing
4866 * commit. In that case we have to wait for commit to finish and try again.
4867 */
4868static void ext4_wait_for_tail_page_commit(struct inode *inode)
4869{
4870 struct page *page;
4871 unsigned offset;
4872 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
4873 tid_t commit_tid = 0;
4874 int ret;
4875
4876 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
4877 /*
4878 * All buffers in the last page remain valid? Then there's nothing to
4879 * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE ==
4880 * blocksize case
4881 */
4882 if (offset > PAGE_CACHE_SIZE - (1 << inode->i_blkbits))
4883 return;
4884 while (1) {
4885 page = find_lock_page(inode->i_mapping,
4886 inode->i_size >> PAGE_CACHE_SHIFT);
4887 if (!page)
4888 return;
Lukas Czernerca99fdd2013-05-21 23:25:01 -04004889 ret = __ext4_journalled_invalidatepage(page, offset,
4890 PAGE_CACHE_SIZE - offset);
Jan Kara53e87262012-12-25 13:29:52 -05004891 unlock_page(page);
4892 page_cache_release(page);
4893 if (ret != -EBUSY)
4894 return;
4895 commit_tid = 0;
4896 read_lock(&journal->j_state_lock);
4897 if (journal->j_committing_transaction)
4898 commit_tid = journal->j_committing_transaction->t_tid;
4899 read_unlock(&journal->j_state_lock);
4900 if (commit_tid)
4901 jbd2_log_wait_commit(journal, commit_tid);
4902 }
4903}
4904
4905/*
Mingming Cao617ba132006-10-11 01:20:53 -07004906 * ext4_setattr()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004907 *
4908 * Called from notify_change.
4909 *
4910 * We want to trap VFS attempts to truncate the file as soon as
4911 * possible. In particular, we want to make sure that when the VFS
4912 * shrinks i_size, we put the inode on the orphan list and modify
4913 * i_disksize immediately, so that during the subsequent flushing of
4914 * dirty pages and freeing of disk blocks, we can guarantee that any
4915 * commit will leave the blocks being flushed in an unused state on
4916 * disk. (On recovery, the inode will get truncated and the blocks will
4917 * be freed, so we have a strong guarantee that no future commit will
4918 * leave these blocks visible to the user.)
4919 *
Jan Kara678aaf42008-07-11 19:27:31 -04004920 * Another thing we have to assure is that if we are in ordered mode
4921 * and inode is still attached to the committing transaction, we must
4922 * we start writeout of all the dirty pages which are being truncated.
4923 * This way we are sure that all the data written in the previous
4924 * transaction are already on disk (truncate waits for pages under
4925 * writeback).
4926 *
4927 * Called with inode->i_mutex down.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004928 */
Mingming Cao617ba132006-10-11 01:20:53 -07004929int ext4_setattr(struct dentry *dentry, struct iattr *attr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004930{
David Howells2b0143b2015-03-17 22:25:59 +00004931 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004932 int error, rc = 0;
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04004933 int orphan = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004934 const unsigned int ia_valid = attr->ia_valid;
4935
4936 error = inode_change_ok(inode, attr);
4937 if (error)
4938 return error;
4939
Jan Karaa7cdade2015-06-29 16:22:54 +02004940 if (is_quota_modification(inode, attr)) {
4941 error = dquot_initialize(inode);
4942 if (error)
4943 return error;
4944 }
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004945 if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
4946 (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004947 handle_t *handle;
4948
4949 /* (user+group)*(old+new) structure, inode write (sb,
4950 * inode block, ? - but truncate inode update has it) */
Theodore Ts'o9924a922013-02-08 21:59:22 -05004951 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
4952 (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) +
4953 EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004954 if (IS_ERR(handle)) {
4955 error = PTR_ERR(handle);
4956 goto err_out;
4957 }
Christoph Hellwigb43fa822010-03-03 09:05:03 -05004958 error = dquot_transfer(inode, attr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004959 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07004960 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004961 return error;
4962 }
4963 /* Update corresponding info in inode so that everything is in
4964 * one transaction */
4965 if (attr->ia_valid & ATTR_UID)
4966 inode->i_uid = attr->ia_uid;
4967 if (attr->ia_valid & ATTR_GID)
4968 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07004969 error = ext4_mark_inode_dirty(handle, inode);
4970 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004971 }
4972
Josef Bacik3da40c72015-06-22 00:31:26 -04004973 if (attr->ia_valid & ATTR_SIZE) {
Jan Kara52083862013-08-17 10:07:17 -04004974 handle_t *handle;
Josef Bacik3da40c72015-06-22 00:31:26 -04004975 loff_t oldsize = inode->i_size;
4976 int shrink = (attr->ia_size <= inode->i_size);
Christoph Hellwig562c72a2011-06-24 14:29:45 -04004977
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004978 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
Eric Sandeene2b46572008-01-28 23:58:27 -05004979 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4980
Theodore Ts'o0c095c72010-07-27 11:56:06 -04004981 if (attr->ia_size > sbi->s_bitmap_maxbytes)
4982 return -EFBIG;
Eric Sandeene2b46572008-01-28 23:58:27 -05004983 }
Josef Bacik3da40c72015-06-22 00:31:26 -04004984 if (!S_ISREG(inode->i_mode))
4985 return -EINVAL;
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004986
4987 if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size)
4988 inode_inc_iversion(inode);
4989
Josef Bacik3da40c72015-06-22 00:31:26 -04004990 if (ext4_should_order_data(inode) &&
Jan Kara52083862013-08-17 10:07:17 -04004991 (attr->ia_size < inode->i_size)) {
Josef Bacik3da40c72015-06-22 00:31:26 -04004992 error = ext4_begin_ordered_truncate(inode,
Jan Kara678aaf42008-07-11 19:27:31 -04004993 attr->ia_size);
Josef Bacik3da40c72015-06-22 00:31:26 -04004994 if (error)
4995 goto err_out;
4996 }
4997 if (attr->ia_size != inode->i_size) {
Jan Kara52083862013-08-17 10:07:17 -04004998 handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
4999 if (IS_ERR(handle)) {
5000 error = PTR_ERR(handle);
5001 goto err_out;
5002 }
Josef Bacik3da40c72015-06-22 00:31:26 -04005003 if (ext4_handle_valid(handle) && shrink) {
Jan Kara52083862013-08-17 10:07:17 -04005004 error = ext4_orphan_add(handle, inode);
5005 orphan = 1;
5006 }
Eryu Guan911af572015-07-28 15:08:41 -04005007 /*
5008 * Update c/mtime on truncate up, ext4_truncate() will
5009 * update c/mtime in shrink case below
5010 */
5011 if (!shrink) {
5012 inode->i_mtime = ext4_current_time(inode);
5013 inode->i_ctime = inode->i_mtime;
5014 }
Jan Kara90e775b2013-08-17 10:09:31 -04005015 down_write(&EXT4_I(inode)->i_data_sem);
Jan Kara52083862013-08-17 10:07:17 -04005016 EXT4_I(inode)->i_disksize = attr->ia_size;
5017 rc = ext4_mark_inode_dirty(handle, inode);
5018 if (!error)
5019 error = rc;
Jan Kara90e775b2013-08-17 10:09:31 -04005020 /*
5021 * We have to update i_size under i_data_sem together
5022 * with i_disksize to avoid races with writeback code
5023 * running ext4_wb_update_i_disksize().
5024 */
5025 if (!error)
5026 i_size_write(inode, attr->ia_size);
5027 up_write(&EXT4_I(inode)->i_data_sem);
Jan Kara52083862013-08-17 10:07:17 -04005028 ext4_journal_stop(handle);
5029 if (error) {
Josef Bacik3da40c72015-06-22 00:31:26 -04005030 if (orphan)
5031 ext4_orphan_del(NULL, inode);
Jan Kara678aaf42008-07-11 19:27:31 -04005032 goto err_out;
5033 }
Jan Karad6320cb2014-10-01 21:49:46 -04005034 }
Josef Bacik3da40c72015-06-22 00:31:26 -04005035 if (!shrink)
5036 pagecache_isize_extended(inode, oldsize, inode->i_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005037
Jan Kara52083862013-08-17 10:07:17 -04005038 /*
5039 * Blocks are going to be removed from the inode. Wait
5040 * for dio in flight. Temporarily disable
5041 * dioread_nolock to prevent livelock.
5042 */
5043 if (orphan) {
5044 if (!ext4_should_journal_data(inode)) {
5045 ext4_inode_block_unlocked_dio(inode);
5046 inode_dio_wait(inode);
5047 ext4_inode_resume_unlocked_dio(inode);
5048 } else
5049 ext4_wait_for_tail_page_commit(inode);
Dmitry Monakhov1c9114f2012-09-29 00:55:23 -04005050 }
Jan Karaea3d7202015-12-07 14:28:03 -05005051 down_write(&EXT4_I(inode)->i_mmap_sem);
Jan Kara52083862013-08-17 10:07:17 -04005052 /*
5053 * Truncate pagecache after we've waited for commit
5054 * in data=journal mode to make pages freeable.
5055 */
Ross Zwisler923ae0f2015-02-16 15:59:38 -08005056 truncate_pagecache(inode, inode->i_size);
Josef Bacik3da40c72015-06-22 00:31:26 -04005057 if (shrink)
5058 ext4_truncate(inode);
Jan Karaea3d7202015-12-07 14:28:03 -05005059 up_write(&EXT4_I(inode)->i_mmap_sem);
Theodore Ts'o072bd7e2011-05-23 15:13:02 -04005060 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005061
Christoph Hellwig10257742010-06-04 11:30:02 +02005062 if (!rc) {
5063 setattr_copy(inode, attr);
5064 mark_inode_dirty(inode);
5065 }
5066
5067 /*
5068 * If the call to ext4_truncate failed to get a transaction handle at
5069 * all, we need to clean up the in-core orphan list manually.
5070 */
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04005071 if (orphan && inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07005072 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005073
5074 if (!rc && (ia_valid & ATTR_MODE))
Christoph Hellwig64e178a2013-12-20 05:16:44 -08005075 rc = posix_acl_chmod(inode, inode->i_mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005076
5077err_out:
Mingming Cao617ba132006-10-11 01:20:53 -07005078 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005079 if (!error)
5080 error = rc;
5081 return error;
5082}
5083
Mingming Cao3e3398a2008-07-11 19:27:31 -04005084int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
5085 struct kstat *stat)
5086{
5087 struct inode *inode;
Jan Kara8af8eec2013-05-31 19:39:56 -04005088 unsigned long long delalloc_blocks;
Mingming Cao3e3398a2008-07-11 19:27:31 -04005089
David Howells2b0143b2015-03-17 22:25:59 +00005090 inode = d_inode(dentry);
Mingming Cao3e3398a2008-07-11 19:27:31 -04005091 generic_fillattr(inode, stat);
5092
5093 /*
Andreas Dilger9206c562013-11-11 22:38:12 -05005094 * If there is inline data in the inode, the inode will normally not
5095 * have data blocks allocated (it may have an external xattr block).
5096 * Report at least one sector for such files, so tools like tar, rsync,
5097 * others doen't incorrectly think the file is completely sparse.
5098 */
5099 if (unlikely(ext4_has_inline_data(inode)))
5100 stat->blocks += (stat->size + 511) >> 9;
5101
5102 /*
Mingming Cao3e3398a2008-07-11 19:27:31 -04005103 * We can't update i_blocks if the block allocation is delayed
5104 * otherwise in the case of system crash before the real block
5105 * allocation is done, we will have i_blocks inconsistent with
5106 * on-disk file blocks.
5107 * We always keep i_blocks updated together with real
5108 * allocation. But to not confuse with user, stat
5109 * will return the blocks that include the delayed allocation
5110 * blocks for this file.
5111 */
Tao Ma96607552012-05-31 22:54:16 -04005112 delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
Andreas Dilger9206c562013-11-11 22:38:12 -05005113 EXT4_I(inode)->i_reserved_data_blocks);
5114 stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9);
Mingming Cao3e3398a2008-07-11 19:27:31 -04005115 return 0;
5116}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005117
Jan Karafffb2732013-06-04 13:01:11 -04005118static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
5119 int pextents)
Mingming Caoa02908f2008-08-19 22:16:07 -04005120{
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005121 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Jan Karafffb2732013-06-04 13:01:11 -04005122 return ext4_ind_trans_blocks(inode, lblocks);
5123 return ext4_ext_index_trans_blocks(inode, pextents);
Mingming Caoa02908f2008-08-19 22:16:07 -04005124}
Theodore Ts'oac51d832008-11-06 16:49:36 -05005125
Mingming Caoa02908f2008-08-19 22:16:07 -04005126/*
5127 * Account for index blocks, block groups bitmaps and block group
5128 * descriptor blocks if modify datablocks and index blocks
5129 * worse case, the indexs blocks spread over different block groups
5130 *
5131 * If datablocks are discontiguous, they are possible to spread over
Anatol Pomozov4907cb72012-09-01 10:31:09 -07005132 * different block groups too. If they are contiguous, with flexbg,
Mingming Caoa02908f2008-08-19 22:16:07 -04005133 * they could still across block group boundary.
5134 *
5135 * Also account for superblock, inode, quota and xattr blocks
5136 */
Jan Karafffb2732013-06-04 13:01:11 -04005137static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
5138 int pextents)
Mingming Caoa02908f2008-08-19 22:16:07 -04005139{
Theodore Ts'o8df96752009-05-01 08:50:38 -04005140 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
5141 int gdpblocks;
Mingming Caoa02908f2008-08-19 22:16:07 -04005142 int idxblocks;
5143 int ret = 0;
5144
5145 /*
Jan Karafffb2732013-06-04 13:01:11 -04005146 * How many index blocks need to touch to map @lblocks logical blocks
5147 * to @pextents physical extents?
Mingming Caoa02908f2008-08-19 22:16:07 -04005148 */
Jan Karafffb2732013-06-04 13:01:11 -04005149 idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents);
Mingming Caoa02908f2008-08-19 22:16:07 -04005150
5151 ret = idxblocks;
5152
5153 /*
5154 * Now let's see how many group bitmaps and group descriptors need
5155 * to account
5156 */
Jan Karafffb2732013-06-04 13:01:11 -04005157 groups = idxblocks + pextents;
Mingming Caoa02908f2008-08-19 22:16:07 -04005158 gdpblocks = groups;
Theodore Ts'o8df96752009-05-01 08:50:38 -04005159 if (groups > ngroups)
5160 groups = ngroups;
Mingming Caoa02908f2008-08-19 22:16:07 -04005161 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
5162 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
5163
5164 /* bitmaps and block group descriptor blocks */
5165 ret += groups + gdpblocks;
5166
5167 /* Blocks for super block, inode, quota and xattr blocks */
5168 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005169
5170 return ret;
5171}
5172
5173/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03005174 * Calculate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04005175 * the modification of a single pages into a single transaction,
5176 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04005177 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04005178 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04005179 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04005180 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04005181 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04005182 */
5183int ext4_writepage_trans_blocks(struct inode *inode)
5184{
5185 int bpp = ext4_journal_blocks_per_page(inode);
5186 int ret;
5187
Jan Karafffb2732013-06-04 13:01:11 -04005188 ret = ext4_meta_trans_blocks(inode, bpp, bpp);
Mingming Caoa02908f2008-08-19 22:16:07 -04005189
5190 /* Account for data blocks for journalled mode */
5191 if (ext4_should_journal_data(inode))
5192 ret += bpp;
5193 return ret;
5194}
Mingming Caof3bd1f32008-08-19 22:16:03 -04005195
5196/*
5197 * Calculate the journal credits for a chunk of data modification.
5198 *
5199 * This is called from DIO, fallocate or whoever calling
Eric Sandeen79e83032010-07-27 11:56:07 -04005200 * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
Mingming Caof3bd1f32008-08-19 22:16:03 -04005201 *
5202 * journal buffers for data blocks are not included here, as DIO
5203 * and fallocate do no need to journal data buffers.
5204 */
5205int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
5206{
5207 return ext4_meta_trans_blocks(inode, nrblocks, 1);
5208}
5209
Mingming Caoa02908f2008-08-19 22:16:07 -04005210/*
Mingming Cao617ba132006-10-11 01:20:53 -07005211 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005212 * Give this, we know that the caller already has write access to iloc->bh.
5213 */
Mingming Cao617ba132006-10-11 01:20:53 -07005214int ext4_mark_iloc_dirty(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04005215 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005216{
5217 int err = 0;
5218
Theodore Ts'oc64db502012-03-02 12:23:11 -05005219 if (IS_I_VERSION(inode))
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05005220 inode_inc_iversion(inode);
5221
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005222 /* the do_update_inode consumes one bh->b_count */
5223 get_bh(iloc->bh);
5224
Mingming Caodab291a2006-10-11 01:21:01 -07005225 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Frank Mayhar830156c2009-09-29 10:07:47 -04005226 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005227 put_bh(iloc->bh);
5228 return err;
5229}
5230
5231/*
5232 * On success, We end up with an outstanding reference count against
5233 * iloc->bh. This _must_ be cleaned up later.
5234 */
5235
5236int
Mingming Cao617ba132006-10-11 01:20:53 -07005237ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
5238 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005239{
Frank Mayhar03901312009-01-07 00:06:22 -05005240 int err;
5241
5242 err = ext4_get_inode_loc(inode, iloc);
5243 if (!err) {
5244 BUFFER_TRACE(iloc->bh, "get_write_access");
5245 err = ext4_journal_get_write_access(handle, iloc->bh);
5246 if (err) {
5247 brelse(iloc->bh);
5248 iloc->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005249 }
5250 }
Mingming Cao617ba132006-10-11 01:20:53 -07005251 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005252 return err;
5253}
5254
5255/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005256 * Expand an inode by new_extra_isize bytes.
5257 * Returns 0 on success or negative error number on failure.
5258 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05005259static int ext4_expand_extra_isize(struct inode *inode,
5260 unsigned int new_extra_isize,
5261 struct ext4_iloc iloc,
5262 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005263{
5264 struct ext4_inode *raw_inode;
5265 struct ext4_xattr_ibody_header *header;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005266
5267 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
5268 return 0;
5269
5270 raw_inode = ext4_raw_inode(&iloc);
5271
5272 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005273
5274 /* No extended attributes present */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005275 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
5276 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005277 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
5278 new_extra_isize);
5279 EXT4_I(inode)->i_extra_isize = new_extra_isize;
5280 return 0;
5281 }
5282
5283 /* try to expand with EAs present */
5284 return ext4_expand_extra_isize_ea(inode, new_extra_isize,
5285 raw_inode, handle);
5286}
5287
5288/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005289 * What we do here is to mark the in-core inode as clean with respect to inode
5290 * dirtiness (it may still be data-dirty).
5291 * This means that the in-core inode may be reaped by prune_icache
5292 * without having to perform any I/O. This is a very good thing,
5293 * because *any* task may call prune_icache - even ones which
5294 * have a transaction open against a different journal.
5295 *
5296 * Is this cheating? Not really. Sure, we haven't written the
5297 * inode out, but prune_icache isn't a user-visible syncing function.
5298 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
5299 * we start and wait on commits.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005300 */
Mingming Cao617ba132006-10-11 01:20:53 -07005301int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005302{
Mingming Cao617ba132006-10-11 01:20:53 -07005303 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005304 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5305 static unsigned int mnt_count;
5306 int err, ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005307
5308 might_sleep();
Theodore Ts'o7ff9c072010-11-08 13:51:33 -05005309 trace_ext4_mark_inode_dirty(inode, _RET_IP_);
Mingming Cao617ba132006-10-11 01:20:53 -07005310 err = ext4_reserve_inode_write(handle, inode, &iloc);
Frank Mayhar03901312009-01-07 00:06:22 -05005311 if (ext4_handle_valid(handle) &&
5312 EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005313 !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005314 /*
5315 * We need extra buffer credits since we may write into EA block
5316 * with this same handle. If journal_extend fails, then it will
5317 * only result in a minor loss of functionality for that inode.
5318 * If this is felt to be critical, then e2fsck should be run to
5319 * force a large enough s_min_extra_isize.
5320 */
5321 if ((jbd2_journal_extend(handle,
5322 EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
5323 ret = ext4_expand_extra_isize(inode,
5324 sbi->s_want_extra_isize,
5325 iloc, handle);
5326 if (ret) {
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005327 ext4_set_inode_state(inode,
5328 EXT4_STATE_NO_EXPAND);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005329 if (mnt_count !=
5330 le16_to_cpu(sbi->s_es->s_mnt_count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05005331 ext4_warning(inode->i_sb,
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005332 "Unable to expand inode %lu. Delete"
5333 " some EAs or run e2fsck.",
5334 inode->i_ino);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005335 mnt_count =
5336 le16_to_cpu(sbi->s_es->s_mnt_count);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005337 }
5338 }
5339 }
5340 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005341 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07005342 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005343 return err;
5344}
5345
5346/*
Mingming Cao617ba132006-10-11 01:20:53 -07005347 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005348 *
5349 * We're really interested in the case where a file is being extended.
5350 * i_size has been changed by generic_commit_write() and we thus need
5351 * to include the updated inode in the current transaction.
5352 *
Christoph Hellwig5dd40562010-03-03 09:05:00 -05005353 * Also, dquot_alloc_block() will always dirty the inode when blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005354 * are allocated to the file.
5355 *
5356 * If the inode is marked synchronous, we don't honour that here - doing
5357 * so would cause a commit on atime updates, which we don't bother doing.
5358 * We handle synchronous inodes at the highest possible level.
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05005359 *
5360 * If only the I_DIRTY_TIME flag is set, we can skip everything. If
5361 * I_DIRTY_TIME and I_DIRTY_SYNC is set, the only inode fields we need
5362 * to copy into the on-disk inode structure are the timestamp files.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005363 */
Christoph Hellwigaa385722011-05-27 06:53:02 -04005364void ext4_dirty_inode(struct inode *inode, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005365{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005366 handle_t *handle;
5367
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05005368 if (flags == I_DIRTY_TIME)
5369 return;
Theodore Ts'o9924a922013-02-08 21:59:22 -05005370 handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005371 if (IS_ERR(handle))
5372 goto out;
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005373
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005374 ext4_mark_inode_dirty(handle, inode);
5375
Mingming Cao617ba132006-10-11 01:20:53 -07005376 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005377out:
5378 return;
5379}
5380
5381#if 0
5382/*
5383 * Bind an inode's backing buffer_head into this transaction, to prevent
5384 * it from being flushed to disk early. Unlike
Mingming Cao617ba132006-10-11 01:20:53 -07005385 * ext4_reserve_inode_write, this leaves behind no bh reference and
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005386 * returns no iloc structure, so the caller needs to repeat the iloc
5387 * lookup to mark the inode dirty later.
5388 */
Mingming Cao617ba132006-10-11 01:20:53 -07005389static int ext4_pin_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005390{
Mingming Cao617ba132006-10-11 01:20:53 -07005391 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005392
5393 int err = 0;
5394 if (handle) {
Mingming Cao617ba132006-10-11 01:20:53 -07005395 err = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005396 if (!err) {
5397 BUFFER_TRACE(iloc.bh, "get_write_access");
Mingming Caodab291a2006-10-11 01:21:01 -07005398 err = jbd2_journal_get_write_access(handle, iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005399 if (!err)
Frank Mayhar03901312009-01-07 00:06:22 -05005400 err = ext4_handle_dirty_metadata(handle,
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05005401 NULL,
Frank Mayhar03901312009-01-07 00:06:22 -05005402 iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005403 brelse(iloc.bh);
5404 }
5405 }
Mingming Cao617ba132006-10-11 01:20:53 -07005406 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005407 return err;
5408}
5409#endif
5410
Mingming Cao617ba132006-10-11 01:20:53 -07005411int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005412{
5413 journal_t *journal;
5414 handle_t *handle;
5415 int err;
5416
5417 /*
5418 * We have to be very careful here: changing a data block's
5419 * journaling status dynamically is dangerous. If we write a
5420 * data block to the journal, change the status and then delete
5421 * that block, we risk forgetting to revoke the old log record
5422 * from the journal and so a subsequent replay can corrupt data.
5423 * So, first we make sure that the journal is empty and that
5424 * nobody is changing anything.
5425 */
5426
Mingming Cao617ba132006-10-11 01:20:53 -07005427 journal = EXT4_JOURNAL(inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005428 if (!journal)
5429 return 0;
Dave Hansend6995942007-07-18 08:33:51 -04005430 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005431 return -EROFS;
Yongqiang Yang2aff57b2011-12-28 12:02:13 -05005432 /* We have to allocate physical blocks for delalloc blocks
5433 * before flushing journal. otherwise delalloc blocks can not
5434 * be allocated any more. even more truncate on delalloc blocks
5435 * could trigger BUG by flushing delalloc blocks in journal.
5436 * There is no delalloc block in non-journal data mode.
5437 */
5438 if (val && test_opt(inode->i_sb, DELALLOC)) {
5439 err = ext4_alloc_da_blocks(inode);
5440 if (err < 0)
5441 return err;
5442 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005443
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04005444 /* Wait for all existing dio workers */
5445 ext4_inode_block_unlocked_dio(inode);
5446 inode_dio_wait(inode);
5447
Mingming Caodab291a2006-10-11 01:21:01 -07005448 jbd2_journal_lock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005449
5450 /*
5451 * OK, there are no updates running now, and all cached data is
5452 * synced to disk. We are now in a completely consistent state
5453 * which doesn't have anything in the journal, and we know that
5454 * no filesystem updates are running, so it is safe to modify
5455 * the inode's in-core data-journaling state flag now.
5456 */
5457
5458 if (val)
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005459 ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Yongqiang Yang5872dda2011-12-28 13:55:51 -05005460 else {
Jan Kara4f879ca2014-10-30 10:53:17 -04005461 err = jbd2_journal_flush(journal);
5462 if (err < 0) {
5463 jbd2_journal_unlock_updates(journal);
5464 ext4_inode_resume_unlocked_dio(inode);
5465 return err;
5466 }
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005467 ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Yongqiang Yang5872dda2011-12-28 13:55:51 -05005468 }
Mingming Cao617ba132006-10-11 01:20:53 -07005469 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005470
Mingming Caodab291a2006-10-11 01:21:01 -07005471 jbd2_journal_unlock_updates(journal);
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04005472 ext4_inode_resume_unlocked_dio(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005473
5474 /* Finally we can mark the inode as dirty. */
5475
Theodore Ts'o9924a922013-02-08 21:59:22 -05005476 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005477 if (IS_ERR(handle))
5478 return PTR_ERR(handle);
5479
Mingming Cao617ba132006-10-11 01:20:53 -07005480 err = ext4_mark_inode_dirty(handle, inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005481 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005482 ext4_journal_stop(handle);
5483 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005484
5485 return err;
5486}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005487
5488static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
5489{
5490 return !buffer_mapped(bh);
5491}
5492
Nick Pigginc2ec1752009-03-31 15:23:21 -07005493int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005494{
Nick Pigginc2ec1752009-03-31 15:23:21 -07005495 struct page *page = vmf->page;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005496 loff_t size;
5497 unsigned long len;
Jan Kara9ea7df52011-06-24 14:29:41 -04005498 int ret;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005499 struct file *file = vma->vm_file;
Al Viro496ad9a2013-01-23 17:07:38 -05005500 struct inode *inode = file_inode(file);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005501 struct address_space *mapping = inode->i_mapping;
Jan Kara9ea7df52011-06-24 14:29:41 -04005502 handle_t *handle;
5503 get_block_t *get_block;
5504 int retries = 0;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005505
Jan Kara8e8ad8a2012-06-12 16:20:38 +02005506 sb_start_pagefault(inode->i_sb);
Theodore Ts'o041bbb6d2012-09-30 23:04:56 -04005507 file_update_time(vma->vm_file);
Jan Karaea3d7202015-12-07 14:28:03 -05005508
5509 down_read(&EXT4_I(inode)->i_mmap_sem);
Jan Kara9ea7df52011-06-24 14:29:41 -04005510 /* Delalloc case is easy... */
5511 if (test_opt(inode->i_sb, DELALLOC) &&
5512 !ext4_should_journal_data(inode) &&
5513 !ext4_nonda_switch(inode->i_sb)) {
5514 do {
Ross Zwisler5c500022015-10-13 16:51:02 -06005515 ret = block_page_mkwrite(vma, vmf,
Jan Kara9ea7df52011-06-24 14:29:41 -04005516 ext4_da_get_block_prep);
5517 } while (ret == -ENOSPC &&
5518 ext4_should_retry_alloc(inode->i_sb, &retries));
5519 goto out_ret;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005520 }
Darrick J. Wong0e499892011-05-18 13:55:20 -04005521
5522 lock_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04005523 size = i_size_read(inode);
5524 /* Page got truncated from under us? */
5525 if (page->mapping != mapping || page_offset(page) > size) {
5526 unlock_page(page);
5527 ret = VM_FAULT_NOPAGE;
5528 goto out;
Darrick J. Wong0e499892011-05-18 13:55:20 -04005529 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005530
5531 if (page->index == size >> PAGE_CACHE_SHIFT)
5532 len = size & ~PAGE_CACHE_MASK;
5533 else
5534 len = PAGE_CACHE_SIZE;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005535 /*
Jan Kara9ea7df52011-06-24 14:29:41 -04005536 * Return if we have all the buffers mapped. This avoids the need to do
5537 * journal_start/journal_stop which can block and take a long time
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005538 */
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005539 if (page_has_buffers(page)) {
Tao Maf19d5872012-12-10 14:05:51 -05005540 if (!ext4_walk_page_buffers(NULL, page_buffers(page),
5541 0, len, NULL,
5542 ext4_bh_unmapped)) {
Jan Kara9ea7df52011-06-24 14:29:41 -04005543 /* Wait so that we don't change page under IO */
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08005544 wait_for_stable_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04005545 ret = VM_FAULT_LOCKED;
5546 goto out;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005547 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005548 }
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005549 unlock_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04005550 /* OK, we need to fill the hole... */
5551 if (ext4_should_dioread_nolock(inode))
Jan Kara705965b2016-03-08 23:08:10 -05005552 get_block = ext4_get_block_unwritten;
Jan Kara9ea7df52011-06-24 14:29:41 -04005553 else
5554 get_block = ext4_get_block;
5555retry_alloc:
Theodore Ts'o9924a922013-02-08 21:59:22 -05005556 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
5557 ext4_writepage_trans_blocks(inode));
Jan Kara9ea7df52011-06-24 14:29:41 -04005558 if (IS_ERR(handle)) {
Nick Pigginc2ec1752009-03-31 15:23:21 -07005559 ret = VM_FAULT_SIGBUS;
Jan Kara9ea7df52011-06-24 14:29:41 -04005560 goto out;
5561 }
Ross Zwisler5c500022015-10-13 16:51:02 -06005562 ret = block_page_mkwrite(vma, vmf, get_block);
Jan Kara9ea7df52011-06-24 14:29:41 -04005563 if (!ret && ext4_should_journal_data(inode)) {
Tao Maf19d5872012-12-10 14:05:51 -05005564 if (ext4_walk_page_buffers(handle, page_buffers(page), 0,
Jan Kara9ea7df52011-06-24 14:29:41 -04005565 PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) {
5566 unlock_page(page);
5567 ret = VM_FAULT_SIGBUS;
Yongqiang Yangfcbb5512011-10-26 05:00:19 -04005568 ext4_journal_stop(handle);
Jan Kara9ea7df52011-06-24 14:29:41 -04005569 goto out;
5570 }
5571 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
5572 }
5573 ext4_journal_stop(handle);
5574 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
5575 goto retry_alloc;
5576out_ret:
5577 ret = block_page_mkwrite_return(ret);
5578out:
Jan Karaea3d7202015-12-07 14:28:03 -05005579 up_read(&EXT4_I(inode)->i_mmap_sem);
Jan Kara8e8ad8a2012-06-12 16:20:38 +02005580 sb_end_pagefault(inode->i_sb);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005581 return ret;
5582}
Jan Karaea3d7202015-12-07 14:28:03 -05005583
5584int ext4_filemap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
5585{
5586 struct inode *inode = file_inode(vma->vm_file);
5587 int err;
5588
5589 down_read(&EXT4_I(inode)->i_mmap_sem);
5590 err = filemap_fault(vma, vmf);
5591 up_read(&EXT4_I(inode)->i_mmap_sem);
5592
5593 return err;
5594}