blob: cbd0654a26750e8827f1e40b5b1ae07192bd066b [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>
25#include <linux/quotaops.h>
26#include <linux/string.h>
27#include <linux/buffer_head.h>
28#include <linux/writeback.h>
Alex Tomas64769242008-07-11 19:27:31 -040029#include <linux/pagevec.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070030#include <linux/mpage.h>
Duane Griffine83c1392008-12-19 20:47:15 +000031#include <linux/namei.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070032#include <linux/uio.h>
33#include <linux/bio.h>
Mingming Cao4c0425f2009-09-28 15:48:41 -040034#include <linux/workqueue.h>
Jiaying Zhang744692d2010-03-04 16:14:02 -050035#include <linux/kernel.h>
Andrew Morton6db26ff2011-01-12 16:59:13 -080036#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Theodore Ts'o00a1a052014-03-30 10:20:01 -040038#include <linux/bitops.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040039
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040040#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070041#include "xattr.h"
42#include "acl.h"
Theodore Ts'o9f125d62011-06-27 19:16:04 -040043#include "truncate.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070044
Theodore Ts'o9bffad12009-06-17 11:48:11 -040045#include <trace/events/ext4.h>
46
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -040047#define MPAGE_DA_EXTENT_TAIL 0x01
48
Darrick J. Wong814525f2012-04-29 18:31:10 -040049static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw,
50 struct ext4_inode_info *ei)
51{
52 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
53 __u16 csum_lo;
54 __u16 csum_hi = 0;
55 __u32 csum;
56
Dmitry Monakhov171a7f22013-04-09 23:56:48 -040057 csum_lo = le16_to_cpu(raw->i_checksum_lo);
Darrick J. Wong814525f2012-04-29 18:31:10 -040058 raw->i_checksum_lo = 0;
59 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
60 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) {
Dmitry Monakhov171a7f22013-04-09 23:56:48 -040061 csum_hi = le16_to_cpu(raw->i_checksum_hi);
Darrick J. Wong814525f2012-04-29 18:31:10 -040062 raw->i_checksum_hi = 0;
63 }
64
65 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw,
66 EXT4_INODE_SIZE(inode->i_sb));
67
Dmitry Monakhov171a7f22013-04-09 23:56:48 -040068 raw->i_checksum_lo = cpu_to_le16(csum_lo);
Darrick J. Wong814525f2012-04-29 18:31:10 -040069 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
70 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
Dmitry Monakhov171a7f22013-04-09 23:56:48 -040071 raw->i_checksum_hi = cpu_to_le16(csum_hi);
Darrick J. Wong814525f2012-04-29 18:31:10 -040072
73 return csum;
74}
75
76static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw,
77 struct ext4_inode_info *ei)
78{
79 __u32 provided, calculated;
80
81 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
82 cpu_to_le32(EXT4_OS_LINUX) ||
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -040083 !ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong814525f2012-04-29 18:31:10 -040084 return 1;
85
86 provided = le16_to_cpu(raw->i_checksum_lo);
87 calculated = ext4_inode_csum(inode, raw, ei);
88 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
89 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
90 provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16;
91 else
92 calculated &= 0xFFFF;
93
94 return provided == calculated;
95}
96
97static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
98 struct ext4_inode_info *ei)
99{
100 __u32 csum;
101
102 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
103 cpu_to_le32(EXT4_OS_LINUX) ||
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -0400104 !ext4_has_metadata_csum(inode->i_sb))
Darrick J. Wong814525f2012-04-29 18:31:10 -0400105 return;
106
107 csum = ext4_inode_csum(inode, raw, ei);
108 raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF);
109 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
110 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
111 raw->i_checksum_hi = cpu_to_le16(csum >> 16);
112}
113
Jan Kara678aaf42008-07-11 19:27:31 -0400114static inline int ext4_begin_ordered_truncate(struct inode *inode,
115 loff_t new_size)
116{
Theodore Ts'o7ff9c072010-11-08 13:51:33 -0500117 trace_ext4_begin_ordered_truncate(inode, new_size);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -0500118 /*
119 * If jinode is zero, then we never opened the file for
120 * writing, so there's no need to call
121 * jbd2_journal_begin_ordered_truncate() since there's no
122 * outstanding writes we need to flush.
123 */
124 if (!EXT4_I(inode)->jinode)
125 return 0;
126 return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
127 EXT4_I(inode)->jinode,
128 new_size);
Jan Kara678aaf42008-07-11 19:27:31 -0400129}
130
Lukas Czernerd47992f2013-05-21 23:17:23 -0400131static void ext4_invalidatepage(struct page *page, unsigned int offset,
132 unsigned int length);
Theodore Ts'ocb20d512010-10-27 21:30:09 -0400133static int __ext4_journalled_writepage(struct page *page, unsigned int len);
134static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
Jan Karafffb2732013-06-04 13:01:11 -0400135static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
136 int pextents);
Alex Tomas64769242008-07-11 19:27:31 -0400137
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700138/*
139 * Test whether an inode is a fast symlink.
140 */
Theodore Ts'of348c252015-04-16 01:55:00 -0400141int ext4_inode_is_fast_symlink(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700142{
Yongqiang Yang65eddb52014-01-06 14:06:18 -0500143 int ea_blocks = EXT4_I(inode)->i_file_acl ?
144 EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700145
Zheng Liubd9db172014-06-02 10:48:22 -0400146 if (ext4_has_inline_data(inode))
147 return 0;
148
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700149 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
150}
151
152/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700153 * Restart the transaction associated with *handle. This does a commit,
154 * so before we call here everything must be consistently dirtied against
155 * this transaction.
156 */
Aneesh Kumar K.Vfa5d1112009-11-02 18:50:49 -0500157int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode,
Jan Kara487caee2009-08-17 22:17:20 -0400158 int nblocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700159{
Jan Kara487caee2009-08-17 22:17:20 -0400160 int ret;
161
162 /*
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400163 * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
Jan Kara487caee2009-08-17 22:17:20 -0400164 * moment, get_block can be called only for blocks inside i_size since
165 * page cache has been already dropped and writes are blocked by
166 * i_mutex. So we can safely drop the i_data_sem here.
167 */
Frank Mayhar03901312009-01-07 00:06:22 -0500168 BUG_ON(EXT4_JOURNAL(inode) == NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700169 jbd_debug(2, "restarting handle %p\n", handle);
Jan Kara487caee2009-08-17 22:17:20 -0400170 up_write(&EXT4_I(inode)->i_data_sem);
Amir Goldstein8e8eaab2011-02-27 23:32:12 -0500171 ret = ext4_journal_restart(handle, nblocks);
Jan Kara487caee2009-08-17 22:17:20 -0400172 down_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vfa5d1112009-11-02 18:50:49 -0500173 ext4_discard_preallocations(inode);
Jan Kara487caee2009-08-17 22:17:20 -0400174
175 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700176}
177
178/*
179 * Called at the last iput() if i_nlink is zero.
180 */
Al Viro0930fcc2010-06-07 13:16:22 -0400181void ext4_evict_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700182{
183 handle_t *handle;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400184 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700185
Theodore Ts'o7ff9c072010-11-08 13:51:33 -0500186 trace_ext4_evict_inode(inode);
Jiaying Zhang2581fdc2011-08-13 12:17:13 -0400187
Al Viro0930fcc2010-06-07 13:16:22 -0400188 if (inode->i_nlink) {
Jan Kara2d859db2011-07-26 09:07:11 -0400189 /*
190 * When journalling data dirty buffers are tracked only in the
191 * journal. So although mm thinks everything is clean and
192 * ready for reaping the inode might still have some pages to
193 * write in the running transaction or waiting to be
194 * checkpointed. Thus calling jbd2_journal_invalidatepage()
195 * (via truncate_inode_pages()) to discard these buffers can
196 * cause data loss. Also even if we did not discard these
197 * buffers, we would have no way to find them after the inode
198 * is reaped and thus user could see stale data if he tries to
199 * read them before the transaction is checkpointed. So be
200 * careful and force everything to disk here... We use
201 * ei->i_datasync_tid to store the newest transaction
202 * containing inode's data.
203 *
204 * Note that directories do not have this problem because they
205 * don't use page cache.
206 */
207 if (ext4_should_journal_data(inode) &&
Theodore Ts'o2b405bf2013-03-20 09:42:11 -0400208 (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode)) &&
209 inode->i_ino != EXT4_JOURNAL_INO) {
Jan Kara2d859db2011-07-26 09:07:11 -0400210 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
211 tid_t commit_tid = EXT4_I(inode)->i_datasync_tid;
212
Theodore Ts'od76a3a772013-04-03 22:02:52 -0400213 jbd2_complete_transaction(journal, commit_tid);
Jan Kara2d859db2011-07-26 09:07:11 -0400214 filemap_write_and_wait(&inode->i_data);
215 }
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700216 truncate_inode_pages_final(&inode->i_data);
Jan Kara5dc23bd2013-06-04 14:46:12 -0400217
218 WARN_ON(atomic_read(&EXT4_I(inode)->i_ioend_count));
Al Viro0930fcc2010-06-07 13:16:22 -0400219 goto no_delete;
220 }
221
Theodore Ts'oe2bfb082014-10-05 22:47:07 -0400222 if (is_bad_inode(inode))
223 goto no_delete;
224 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500225
Jan Kara678aaf42008-07-11 19:27:31 -0400226 if (ext4_should_order_data(inode))
227 ext4_begin_ordered_truncate(inode, 0);
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700228 truncate_inode_pages_final(&inode->i_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700229
Jan Kara5dc23bd2013-06-04 14:46:12 -0400230 WARN_ON(atomic_read(&EXT4_I(inode)->i_ioend_count));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700231
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200232 /*
233 * Protect us against freezing - iput() caller didn't have to have any
234 * protection against it
235 */
236 sb_start_intwrite(inode->i_sb);
Theodore Ts'o9924a922013-02-08 21:59:22 -0500237 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
238 ext4_blocks_for_truncate(inode)+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700239 if (IS_ERR(handle)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400240 ext4_std_error(inode->i_sb, PTR_ERR(handle));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700241 /*
242 * If we're going to skip the normal cleanup, we still need to
243 * make sure that the in-core orphan linked list is properly
244 * cleaned up.
245 */
Mingming Cao617ba132006-10-11 01:20:53 -0700246 ext4_orphan_del(NULL, inode);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200247 sb_end_intwrite(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700248 goto no_delete;
249 }
250
251 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500252 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700253 inode->i_size = 0;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400254 err = ext4_mark_inode_dirty(handle, inode);
255 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500256 ext4_warning(inode->i_sb,
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400257 "couldn't mark inode dirty (err %d)", err);
258 goto stop_handle;
259 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700260 if (inode->i_blocks)
Mingming Cao617ba132006-10-11 01:20:53 -0700261 ext4_truncate(inode);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400262
263 /*
264 * ext4_ext_truncate() doesn't reserve any slop when it
265 * restarts journal transactions; therefore there may not be
266 * enough credits left in the handle to remove the inode from
267 * the orphan list and set the dtime field.
268 */
Frank Mayhar03901312009-01-07 00:06:22 -0500269 if (!ext4_handle_has_enough_credits(handle, 3)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400270 err = ext4_journal_extend(handle, 3);
271 if (err > 0)
272 err = ext4_journal_restart(handle, 3);
273 if (err != 0) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500274 ext4_warning(inode->i_sb,
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400275 "couldn't extend journal (err %d)", err);
276 stop_handle:
277 ext4_journal_stop(handle);
Theodore Ts'o45388212010-07-29 15:06:10 -0400278 ext4_orphan_del(NULL, inode);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200279 sb_end_intwrite(inode->i_sb);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400280 goto no_delete;
281 }
282 }
283
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700284 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700285 * Kill off the orphan record which ext4_truncate created.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700286 * AKPM: I think this can be inside the above `if'.
Mingming Cao617ba132006-10-11 01:20:53 -0700287 * Note that ext4_orphan_del() has to be able to cope with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700288 * deletion of a non-existent orphan - this is because we don't
Mingming Cao617ba132006-10-11 01:20:53 -0700289 * know if ext4_truncate() actually created an orphan record.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700290 * (Well, we could do this if we need to, but heck - it works)
291 */
Mingming Cao617ba132006-10-11 01:20:53 -0700292 ext4_orphan_del(handle, inode);
293 EXT4_I(inode)->i_dtime = get_seconds();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700294
295 /*
296 * One subtle ordering requirement: if anything has gone wrong
297 * (transaction abort, IO errors, whatever), then we can still
298 * do these next steps (the fs will already have been marked as
299 * having errors), but we can't free the inode if the mark_dirty
300 * fails.
301 */
Mingming Cao617ba132006-10-11 01:20:53 -0700302 if (ext4_mark_inode_dirty(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700303 /* If that failed, just do the required in-core inode clear. */
Al Viro0930fcc2010-06-07 13:16:22 -0400304 ext4_clear_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700305 else
Mingming Cao617ba132006-10-11 01:20:53 -0700306 ext4_free_inode(handle, inode);
307 ext4_journal_stop(handle);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200308 sb_end_intwrite(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700309 return;
310no_delete:
Al Viro0930fcc2010-06-07 13:16:22 -0400311 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700312}
313
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300314#ifdef CONFIG_QUOTA
315qsize_t *ext4_get_reserved_space(struct inode *inode)
Mingming Cao60e58e02009-01-22 18:13:05 +0100316{
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300317 return &EXT4_I(inode)->i_reserved_quota;
Mingming Cao60e58e02009-01-22 18:13:05 +0100318}
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300319#endif
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500320
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400321/*
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500322 * Called with i_data_sem down, which is important since we can call
323 * ext4_discard_preallocations() from here.
324 */
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500325void ext4_da_update_reserve_space(struct inode *inode,
326 int used, int quota_claim)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400327{
328 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500329 struct ext4_inode_info *ei = EXT4_I(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400330
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500331 spin_lock(&ei->i_block_reservation_lock);
Aditya Kalid8990242011-09-09 19:18:51 -0400332 trace_ext4_da_update_reserve_space(inode, used, quota_claim);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500333 if (unlikely(used > ei->i_reserved_data_blocks)) {
Theodore Ts'o8de5c322013-02-14 15:11:41 -0500334 ext4_warning(inode->i_sb, "%s: ino %lu, used %d "
Theodore Ts'o1084f252012-03-19 23:13:43 -0400335 "with only %d reserved data blocks",
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500336 __func__, inode->i_ino, used,
337 ei->i_reserved_data_blocks);
338 WARN_ON(1);
339 used = ei->i_reserved_data_blocks;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400340 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400341
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500342 /* Update per-inode reservations */
343 ei->i_reserved_data_blocks -= used;
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -0400344 percpu_counter_sub(&sbi->s_dirtyclusters_counter, used);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500345
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400346 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +0100347
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400348 /* Update quota subsystem for data blocks */
349 if (quota_claim)
Aditya Kali7b415bf2011-09-09 19:04:51 -0400350 dquot_claim_block(inode, EXT4_C2B(sbi, used));
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400351 else {
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500352 /*
353 * We did fallocate with an offset that is already delayed
354 * allocated. So on delayed allocated writeback we should
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400355 * not re-claim the quota for fallocated blocks.
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500356 */
Aditya Kali7b415bf2011-09-09 19:04:51 -0400357 dquot_release_reservation_block(inode, EXT4_C2B(sbi, used));
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500358 }
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -0400359
360 /*
361 * If we have done all the pending block allocations and if
362 * there aren't any writers on the inode, we can discard the
363 * inode's preallocations.
364 */
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500365 if ((ei->i_reserved_data_blocks == 0) &&
366 (atomic_read(&inode->i_writecount) == 0))
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -0400367 ext4_discard_preallocations(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400368}
369
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400370static int __check_block_validity(struct inode *inode, const char *func,
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400371 unsigned int line,
372 struct ext4_map_blocks *map)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400373{
Theodore Ts'o24676da2010-05-16 21:00:00 -0400374 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
375 map->m_len)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400376 ext4_error_inode(inode, func, line, map->m_pblk,
377 "lblock %lu mapped to illegal pblock "
378 "(length %d)", (unsigned long) map->m_lblk,
379 map->m_len);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400380 return -EIO;
381 }
382 return 0;
383}
384
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400385#define check_block_validity(inode, map) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400386 __check_block_validity((inode), __func__, __LINE__, (map))
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400387
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400388#ifdef ES_AGGRESSIVE_TEST
389static void ext4_map_blocks_es_recheck(handle_t *handle,
390 struct inode *inode,
391 struct ext4_map_blocks *es_map,
392 struct ext4_map_blocks *map,
393 int flags)
394{
395 int retval;
396
397 map->m_flags = 0;
398 /*
399 * There is a race window that the result is not the same.
400 * e.g. xfstests #223 when dioread_nolock enables. The reason
401 * is that we lookup a block mapping in extent status tree with
402 * out taking i_data_sem. So at the time the unwritten extent
403 * could be converted.
404 */
405 if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
Lukas Czernerc8b459f2014-05-12 12:55:07 -0400406 down_read(&EXT4_I(inode)->i_data_sem);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400407 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
408 retval = ext4_ext_map_blocks(handle, inode, map, flags &
409 EXT4_GET_BLOCKS_KEEP_SIZE);
410 } else {
411 retval = ext4_ind_map_blocks(handle, inode, map, flags &
412 EXT4_GET_BLOCKS_KEEP_SIZE);
413 }
414 if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
415 up_read((&EXT4_I(inode)->i_data_sem));
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400416
417 /*
418 * We don't check m_len because extent will be collpased in status
419 * tree. So the m_len might not equal.
420 */
421 if (es_map->m_lblk != map->m_lblk ||
422 es_map->m_flags != map->m_flags ||
423 es_map->m_pblk != map->m_pblk) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400424 printk("ES cache assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400425 "es_cached ex [%d/%d/%llu/%x] != "
426 "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
427 inode->i_ino, es_map->m_lblk, es_map->m_len,
428 es_map->m_pblk, es_map->m_flags, map->m_lblk,
429 map->m_len, map->m_pblk, map->m_flags,
430 retval, flags);
431 }
432}
433#endif /* ES_AGGRESSIVE_TEST */
434
Theodore Ts'o55138e02009-09-29 13:31:31 -0400435/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400436 * The ext4_map_blocks() function tries to look up the requested blocks,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400437 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500438 *
Mingming Caof5ab0d12008-02-25 15:29:55 -0500439 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
440 * and store the allocated blocks in the result buffer head and mark it
441 * mapped.
442 *
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400443 * If file type is extents based, it will call ext4_ext_map_blocks(),
444 * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
Mingming Caof5ab0d12008-02-25 15:29:55 -0500445 * based files
446 *
Lukas Czerner556615d2014-04-20 23:45:47 -0400447 * On success, it returns the number of blocks being mapped or allocated.
448 * if create==0 and the blocks are pre-allocated and unwritten block,
Mingming Caof5ab0d12008-02-25 15:29:55 -0500449 * the result buffer head is unmapped. If the create ==1, it will make sure
450 * the buffer head is mapped.
451 *
452 * It returns 0 if plain look up failed (blocks have not been allocated), in
Tao Madf3ab172011-10-08 15:53:49 -0400453 * that case, buffer head is unmapped
Mingming Caof5ab0d12008-02-25 15:29:55 -0500454 *
455 * It returns the error in case of allocation failure.
456 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400457int ext4_map_blocks(handle_t *handle, struct inode *inode,
458 struct ext4_map_blocks *map, int flags)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500459{
Zheng Liud100eef2013-02-18 00:29:59 -0500460 struct extent_status es;
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500461 int retval;
Lukas Czernerb8a86842014-03-18 18:05:35 -0400462 int ret = 0;
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400463#ifdef ES_AGGRESSIVE_TEST
464 struct ext4_map_blocks orig_map;
465
466 memcpy(&orig_map, map, sizeof(*map));
467#endif
Mingming Caof5ab0d12008-02-25 15:29:55 -0500468
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400469 map->m_flags = 0;
470 ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
471 "logical block %lu\n", inode->i_ino, flags, map->m_len,
472 (unsigned long) map->m_lblk);
Zheng Liud100eef2013-02-18 00:29:59 -0500473
Theodore Ts'oe861b5e2014-02-20 12:54:05 -0500474 /*
475 * ext4_map_blocks returns an int, and m_len is an unsigned int
476 */
477 if (unlikely(map->m_len > INT_MAX))
478 map->m_len = INT_MAX;
479
Kazuya Mio4adb6ab2014-04-07 10:53:28 -0400480 /* We can handle the block number less than EXT_MAX_BLOCKS */
481 if (unlikely(map->m_lblk >= EXT_MAX_BLOCKS))
482 return -EIO;
483
Zheng Liud100eef2013-02-18 00:29:59 -0500484 /* Lookup extent status tree firstly */
485 if (ext4_es_lookup_extent(inode, map->m_lblk, &es)) {
486 if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
487 map->m_pblk = ext4_es_pblock(&es) +
488 map->m_lblk - es.es_lblk;
489 map->m_flags |= ext4_es_is_written(&es) ?
490 EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN;
491 retval = es.es_len - (map->m_lblk - es.es_lblk);
492 if (retval > map->m_len)
493 retval = map->m_len;
494 map->m_len = retval;
495 } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) {
496 retval = 0;
497 } else {
498 BUG_ON(1);
499 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400500#ifdef ES_AGGRESSIVE_TEST
501 ext4_map_blocks_es_recheck(handle, inode, map,
502 &orig_map, flags);
503#endif
Zheng Liud100eef2013-02-18 00:29:59 -0500504 goto found;
505 }
506
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500507 /*
Theodore Ts'ob920c752009-05-14 00:54:29 -0400508 * Try to see if we can get the block without requesting a new
509 * file system block.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500510 */
Zheng Liu729f52c2012-07-09 16:29:29 -0400511 if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
Lukas Czernerc8b459f2014-05-12 12:55:07 -0400512 down_read(&EXT4_I(inode)->i_data_sem);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400513 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Dmitry Monakhova4e5d882011-10-25 08:15:12 -0400514 retval = ext4_ext_map_blocks(handle, inode, map, flags &
515 EXT4_GET_BLOCKS_KEEP_SIZE);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500516 } else {
Dmitry Monakhova4e5d882011-10-25 08:15:12 -0400517 retval = ext4_ind_map_blocks(handle, inode, map, flags &
518 EXT4_GET_BLOCKS_KEEP_SIZE);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500519 }
Zheng Liuf7fec032013-02-18 00:28:47 -0500520 if (retval > 0) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400521 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -0500522
Zheng Liu44fb851d2013-07-29 12:51:42 -0400523 if (unlikely(retval != map->m_len)) {
524 ext4_warning(inode->i_sb,
525 "ES len assertion failed for inode "
526 "%lu: retval %d != map->m_len %d",
527 inode->i_ino, retval, map->m_len);
528 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400529 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400530
Zheng Liuf7fec032013-02-18 00:28:47 -0500531 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
532 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
533 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
534 ext4_find_delalloc_range(inode, map->m_lblk,
535 map->m_lblk + map->m_len - 1))
536 status |= EXTENT_STATUS_DELAYED;
537 ret = ext4_es_insert_extent(inode, map->m_lblk,
538 map->m_len, map->m_pblk, status);
539 if (ret < 0)
540 retval = ret;
541 }
Zheng Liu729f52c2012-07-09 16:29:29 -0400542 if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
543 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -0500544
Zheng Liud100eef2013-02-18 00:29:59 -0500545found:
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400546 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Lukas Czernerb8a86842014-03-18 18:05:35 -0400547 ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400548 if (ret != 0)
549 return ret;
550 }
551
Mingming Caof5ab0d12008-02-25 15:29:55 -0500552 /* If it is only a block(s) look up */
Theodore Ts'oc2177052009-05-14 00:58:52 -0400553 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500554 return retval;
555
556 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -0500557 * Returns if the blocks have already allocated
558 *
559 * Note that if blocks have been preallocated
Tao Madf3ab172011-10-08 15:53:49 -0400560 * ext4_ext_get_block() returns the create = 0
Mingming Caof5ab0d12008-02-25 15:29:55 -0500561 * with buffer head unmapped.
562 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400563 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
Lukas Czernerb8a86842014-03-18 18:05:35 -0400564 /*
565 * If we need to convert extent to unwritten
566 * we continue and do the actual work in
567 * ext4_ext_map_blocks()
568 */
569 if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN))
570 return retval;
Mingming Caof5ab0d12008-02-25 15:29:55 -0500571
572 /*
Zheng Liua25a4e12013-02-18 00:28:04 -0500573 * Here we clear m_flags because after allocating an new extent,
574 * it will be set again.
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -0400575 */
Zheng Liua25a4e12013-02-18 00:28:04 -0500576 map->m_flags &= ~EXT4_MAP_FLAGS;
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -0400577
578 /*
Lukas Czerner556615d2014-04-20 23:45:47 -0400579 * New blocks allocate and/or writing to unwritten extent
Mingming Caof5ab0d12008-02-25 15:29:55 -0500580 * will possibly result in updating i_data, so we take
Seunghun Leed91bd2c2014-09-01 22:15:30 -0400581 * the write lock of i_data_sem, and call get_block()
Mingming Caof5ab0d12008-02-25 15:29:55 -0500582 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500583 */
Lukas Czernerc8b459f2014-05-12 12:55:07 -0400584 down_write(&EXT4_I(inode)->i_data_sem);
Mingming Caod2a17632008-07-14 17:52:37 -0400585
586 /*
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500587 * We need to check for EXT4 here because migrate
588 * could have changed the inode type in between
589 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400590 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400591 retval = ext4_ext_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500592 } else {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400593 retval = ext4_ind_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400594
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400595 if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400596 /*
597 * We allocated new blocks which will result in
598 * i_data's format changing. Force the migrate
599 * to fail by clearing migrate flags
600 */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500601 ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400602 }
Mingming Caod2a17632008-07-14 17:52:37 -0400603
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500604 /*
605 * Update reserved blocks/metadata blocks after successful
606 * block allocation which had been deferred till now. We don't
607 * support fallocate for non extent files. So we can update
608 * reserve space here.
609 */
610 if ((retval > 0) &&
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -0500611 (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500612 ext4_da_update_reserve_space(inode, retval, 1);
613 }
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -0400614
Zheng Liuf7fec032013-02-18 00:28:47 -0500615 if (retval > 0) {
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400616 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -0500617
Zheng Liu44fb851d2013-07-29 12:51:42 -0400618 if (unlikely(retval != map->m_len)) {
619 ext4_warning(inode->i_sb,
620 "ES len assertion failed for inode "
621 "%lu: retval %d != map->m_len %d",
622 inode->i_ino, retval, map->m_len);
623 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400624 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400625
Zheng Liuadb23552013-03-10 21:13:05 -0400626 /*
627 * If the extent has been zeroed out, we don't need to update
628 * extent status tree.
629 */
630 if ((flags & EXT4_GET_BLOCKS_PRE_IO) &&
631 ext4_es_lookup_extent(inode, map->m_lblk, &es)) {
632 if (ext4_es_is_written(&es))
633 goto has_zeroout;
634 }
Zheng Liuf7fec032013-02-18 00:28:47 -0500635 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
636 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
637 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
638 ext4_find_delalloc_range(inode, map->m_lblk,
639 map->m_lblk + map->m_len - 1))
640 status |= EXTENT_STATUS_DELAYED;
641 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
642 map->m_pblk, status);
643 if (ret < 0)
644 retval = ret;
Aditya Kali5356f262011-09-09 19:20:51 -0400645 }
646
Zheng Liuadb23552013-03-10 21:13:05 -0400647has_zeroout:
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500648 up_write((&EXT4_I(inode)->i_data_sem));
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400649 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Lukas Czernerb8a86842014-03-18 18:05:35 -0400650 ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400651 if (ret != 0)
652 return ret;
653 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500654 return retval;
655}
656
Ross Zwisler923ae0f2015-02-16 15:59:38 -0800657static void ext4_end_io_unwritten(struct buffer_head *bh, int uptodate)
658{
659 struct inode *inode = bh->b_assoc_map->host;
660 /* XXX: breaks on 32-bit > 16GB. Is that even supported? */
661 loff_t offset = (loff_t)(uintptr_t)bh->b_private << inode->i_blkbits;
662 int err;
663 if (!uptodate)
664 return;
665 WARN_ON(!buffer_unwritten(bh));
666 err = ext4_convert_unwritten_extents(NULL, inode, offset, bh->b_size);
667}
668
Mingming Caof3bd1f32008-08-19 22:16:03 -0400669/* Maximum number of blocks we map for direct IO at once. */
670#define DIO_MAX_BLOCKS 4096
671
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400672static int _ext4_get_block(struct inode *inode, sector_t iblock,
673 struct buffer_head *bh, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700674{
Dmitriy Monakhov3e4fdaf2007-02-10 01:46:35 -0800675 handle_t *handle = ext4_journal_current_handle();
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400676 struct ext4_map_blocks map;
Jan Kara7fb54092008-02-10 01:08:38 -0500677 int ret = 0, started = 0;
Mingming Caof3bd1f32008-08-19 22:16:03 -0400678 int dio_credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700679
Tao Ma46c7f252012-12-10 14:04:52 -0500680 if (ext4_has_inline_data(inode))
681 return -ERANGE;
682
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400683 map.m_lblk = iblock;
684 map.m_len = bh->b_size >> inode->i_blkbits;
685
Anatol Pomozov8b0f1652012-11-08 15:07:16 -0500686 if (flags && !(flags & EXT4_GET_BLOCKS_NO_LOCK) && !handle) {
Jan Kara7fb54092008-02-10 01:08:38 -0500687 /* Direct IO write... */
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400688 if (map.m_len > DIO_MAX_BLOCKS)
689 map.m_len = DIO_MAX_BLOCKS;
690 dio_credits = ext4_chunk_trans_blocks(inode, map.m_len);
Theodore Ts'o9924a922013-02-08 21:59:22 -0500691 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
692 dio_credits);
Jan Kara7fb54092008-02-10 01:08:38 -0500693 if (IS_ERR(handle)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700694 ret = PTR_ERR(handle);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400695 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700696 }
Jan Kara7fb54092008-02-10 01:08:38 -0500697 started = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700698 }
699
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400700 ret = ext4_map_blocks(handle, inode, &map, flags);
Jan Kara7fb54092008-02-10 01:08:38 -0500701 if (ret > 0) {
Christoph Hellwig7b7a8662013-09-04 15:04:39 +0200702 ext4_io_end_t *io_end = ext4_inode_aio(inode);
703
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400704 map_bh(bh, inode->i_sb, map.m_pblk);
705 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
Ross Zwisler923ae0f2015-02-16 15:59:38 -0800706 if (IS_DAX(inode) && buffer_unwritten(bh) && !io_end) {
707 bh->b_assoc_map = inode->i_mapping;
708 bh->b_private = (void *)(unsigned long)iblock;
709 bh->b_end_io = ext4_end_io_unwritten;
710 }
Christoph Hellwig7b7a8662013-09-04 15:04:39 +0200711 if (io_end && io_end->flag & EXT4_IO_END_UNWRITTEN)
712 set_buffer_defer_completion(bh);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400713 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
Jan Kara7fb54092008-02-10 01:08:38 -0500714 ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700715 }
Jan Kara7fb54092008-02-10 01:08:38 -0500716 if (started)
717 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700718 return ret;
719}
720
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400721int ext4_get_block(struct inode *inode, sector_t iblock,
722 struct buffer_head *bh, int create)
723{
724 return _ext4_get_block(inode, iblock, bh,
725 create ? EXT4_GET_BLOCKS_CREATE : 0);
726}
727
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700728/*
729 * `handle' can be NULL if create is zero
730 */
Mingming Cao617ba132006-10-11 01:20:53 -0700731struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Theodore Ts'o10560082014-08-29 20:51:32 -0400732 ext4_lblk_t block, int create)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700733{
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400734 struct ext4_map_blocks map;
735 struct buffer_head *bh;
Theodore Ts'o10560082014-08-29 20:51:32 -0400736 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700737
738 J_ASSERT(handle != NULL || create == 0);
739
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400740 map.m_lblk = block;
741 map.m_len = 1;
742 err = ext4_map_blocks(handle, inode, &map,
743 create ? EXT4_GET_BLOCKS_CREATE : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700744
Theodore Ts'o10560082014-08-29 20:51:32 -0400745 if (err == 0)
746 return create ? ERR_PTR(-ENOSPC) : NULL;
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400747 if (err < 0)
Theodore Ts'o10560082014-08-29 20:51:32 -0400748 return ERR_PTR(err);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400749
750 bh = sb_getblk(inode->i_sb, map.m_pblk);
Theodore Ts'o10560082014-08-29 20:51:32 -0400751 if (unlikely(!bh))
752 return ERR_PTR(-ENOMEM);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400753 if (map.m_flags & EXT4_MAP_NEW) {
754 J_ASSERT(create != 0);
755 J_ASSERT(handle != NULL);
756
757 /*
758 * Now that we do not always journal data, we should
759 * keep in mind whether this should always journal the
760 * new buffer as metadata. For now, regular file
761 * writes use ext4_get_block instead, so it's not a
762 * problem.
763 */
764 lock_buffer(bh);
765 BUFFER_TRACE(bh, "call get_create_access");
Theodore Ts'o10560082014-08-29 20:51:32 -0400766 err = ext4_journal_get_create_access(handle, bh);
767 if (unlikely(err)) {
768 unlock_buffer(bh);
769 goto errout;
770 }
771 if (!buffer_uptodate(bh)) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400772 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
773 set_buffer_uptodate(bh);
774 }
775 unlock_buffer(bh);
776 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
777 err = ext4_handle_dirty_metadata(handle, inode, bh);
Theodore Ts'o10560082014-08-29 20:51:32 -0400778 if (unlikely(err))
779 goto errout;
780 } else
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400781 BUFFER_TRACE(bh, "not a new buffer");
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400782 return bh;
Theodore Ts'o10560082014-08-29 20:51:32 -0400783errout:
784 brelse(bh);
785 return ERR_PTR(err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700786}
787
Mingming Cao617ba132006-10-11 01:20:53 -0700788struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Theodore Ts'o1c215022014-08-29 20:52:15 -0400789 ext4_lblk_t block, int create)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700790{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400791 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700792
Theodore Ts'o10560082014-08-29 20:51:32 -0400793 bh = ext4_getblk(handle, inode, block, create);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400794 if (IS_ERR(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700795 return bh;
Theodore Ts'o1c215022014-08-29 20:52:15 -0400796 if (!bh || buffer_uptodate(bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700797 return bh;
Christoph Hellwig65299a32011-08-23 14:50:29 +0200798 ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700799 wait_on_buffer(bh);
800 if (buffer_uptodate(bh))
801 return bh;
802 put_bh(bh);
Theodore Ts'o1c215022014-08-29 20:52:15 -0400803 return ERR_PTR(-EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700804}
805
Tao Maf19d5872012-12-10 14:05:51 -0500806int ext4_walk_page_buffers(handle_t *handle,
807 struct buffer_head *head,
808 unsigned from,
809 unsigned to,
810 int *partial,
811 int (*fn)(handle_t *handle,
812 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700813{
814 struct buffer_head *bh;
815 unsigned block_start, block_end;
816 unsigned blocksize = head->b_size;
817 int err, ret = 0;
818 struct buffer_head *next;
819
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400820 for (bh = head, block_start = 0;
821 ret == 0 && (bh != head || !block_start);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400822 block_start = block_end, bh = next) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700823 next = bh->b_this_page;
824 block_end = block_start + blocksize;
825 if (block_end <= from || block_start >= to) {
826 if (partial && !buffer_uptodate(bh))
827 *partial = 1;
828 continue;
829 }
830 err = (*fn)(handle, bh);
831 if (!ret)
832 ret = err;
833 }
834 return ret;
835}
836
837/*
838 * To preserve ordering, it is essential that the hole instantiation and
839 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -0700840 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -0700841 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700842 * prepare_write() is the right place.
843 *
Jan Kara36ade452013-01-28 09:30:52 -0500844 * Also, this function can nest inside ext4_writepage(). In that case, we
845 * *know* that ext4_writepage() has generated enough buffer credits to do the
846 * whole page. So we won't block on the journal in that case, which is good,
847 * because the caller may be PF_MEMALLOC.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700848 *
Mingming Cao617ba132006-10-11 01:20:53 -0700849 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700850 * quota file writes. If we were to commit the transaction while thus
851 * reentered, there can be a deadlock - we would be holding a quota
852 * lock, and the commit would never complete if another thread had a
853 * transaction open and was blocking on the quota lock - a ranking
854 * violation.
855 *
Mingming Caodab291a2006-10-11 01:21:01 -0700856 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700857 * will _not_ run commit under these circumstances because handle->h_ref
858 * is elevated. We'll still have enough credits for the tiny quotafile
859 * write.
860 */
Tao Maf19d5872012-12-10 14:05:51 -0500861int do_journal_get_write_access(handle_t *handle,
862 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700863{
Jan Kara56d35a42010-08-05 14:41:42 -0400864 int dirty = buffer_dirty(bh);
865 int ret;
866
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700867 if (!buffer_mapped(bh) || buffer_freed(bh))
868 return 0;
Jan Kara56d35a42010-08-05 14:41:42 -0400869 /*
Christoph Hellwigebdec242010-10-06 10:47:23 +0200870 * __block_write_begin() could have dirtied some buffers. Clean
Jan Kara56d35a42010-08-05 14:41:42 -0400871 * the dirty bit as jbd2_journal_get_write_access() could complain
872 * otherwise about fs integrity issues. Setting of the dirty bit
Christoph Hellwigebdec242010-10-06 10:47:23 +0200873 * by __block_write_begin() isn't a real problem here as we clear
Jan Kara56d35a42010-08-05 14:41:42 -0400874 * the bit before releasing a page lock and thus writeback cannot
875 * ever write the buffer.
876 */
877 if (dirty)
878 clear_buffer_dirty(bh);
liang xie5d601252014-05-12 22:06:43 -0400879 BUFFER_TRACE(bh, "get write access");
Jan Kara56d35a42010-08-05 14:41:42 -0400880 ret = ext4_journal_get_write_access(handle, bh);
881 if (!ret && dirty)
882 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
883 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700884}
885
Anatol Pomozov8b0f1652012-11-08 15:07:16 -0500886static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock,
887 struct buffer_head *bh_result, int create);
Michael Halcrow2058f832015-04-12 00:55:10 -0400888
889#ifdef CONFIG_EXT4_FS_ENCRYPTION
890static int ext4_block_write_begin(struct page *page, loff_t pos, unsigned len,
891 get_block_t *get_block)
892{
893 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
894 unsigned to = from + len;
895 struct inode *inode = page->mapping->host;
896 unsigned block_start, block_end;
897 sector_t block;
898 int err = 0;
899 unsigned blocksize = inode->i_sb->s_blocksize;
900 unsigned bbits;
901 struct buffer_head *bh, *head, *wait[2], **wait_bh = wait;
902 bool decrypt = false;
903
904 BUG_ON(!PageLocked(page));
905 BUG_ON(from > PAGE_CACHE_SIZE);
906 BUG_ON(to > PAGE_CACHE_SIZE);
907 BUG_ON(from > to);
908
909 if (!page_has_buffers(page))
910 create_empty_buffers(page, blocksize, 0);
911 head = page_buffers(page);
912 bbits = ilog2(blocksize);
913 block = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits);
914
915 for (bh = head, block_start = 0; bh != head || !block_start;
916 block++, block_start = block_end, bh = bh->b_this_page) {
917 block_end = block_start + blocksize;
918 if (block_end <= from || block_start >= to) {
919 if (PageUptodate(page)) {
920 if (!buffer_uptodate(bh))
921 set_buffer_uptodate(bh);
922 }
923 continue;
924 }
925 if (buffer_new(bh))
926 clear_buffer_new(bh);
927 if (!buffer_mapped(bh)) {
928 WARN_ON(bh->b_size != blocksize);
929 err = get_block(inode, block, bh, 1);
930 if (err)
931 break;
932 if (buffer_new(bh)) {
933 unmap_underlying_metadata(bh->b_bdev,
934 bh->b_blocknr);
935 if (PageUptodate(page)) {
936 clear_buffer_new(bh);
937 set_buffer_uptodate(bh);
938 mark_buffer_dirty(bh);
939 continue;
940 }
941 if (block_end > to || block_start < from)
942 zero_user_segments(page, to, block_end,
943 block_start, from);
944 continue;
945 }
946 }
947 if (PageUptodate(page)) {
948 if (!buffer_uptodate(bh))
949 set_buffer_uptodate(bh);
950 continue;
951 }
952 if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
953 !buffer_unwritten(bh) &&
954 (block_start < from || block_end > to)) {
955 ll_rw_block(READ, 1, &bh);
956 *wait_bh++ = bh;
957 decrypt = ext4_encrypted_inode(inode) &&
958 S_ISREG(inode->i_mode);
959 }
960 }
961 /*
962 * If we issued read requests, let them complete.
963 */
964 while (wait_bh > wait) {
965 wait_on_buffer(*--wait_bh);
966 if (!buffer_uptodate(*wait_bh))
967 err = -EIO;
968 }
969 if (unlikely(err))
970 page_zero_new_buffers(page, from, to);
971 else if (decrypt)
972 err = ext4_decrypt_one(inode, page);
973 return err;
974}
975#endif
976
Nick Pigginbfc1af62007-10-16 01:25:05 -0700977static int ext4_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400978 loff_t pos, unsigned len, unsigned flags,
979 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700980{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400981 struct inode *inode = mapping->host;
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -0400982 int ret, needed_blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700983 handle_t *handle;
984 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400985 struct page *page;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400986 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400987 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -0700988
Theodore Ts'o9bffad12009-06-17 11:48:11 -0400989 trace_ext4_write_begin(inode, pos, len, flags);
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -0400990 /*
991 * Reserve one block more for addition to orphan list in case
992 * we allocate blocks but write fails for some reason
993 */
994 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400995 index = pos >> PAGE_CACHE_SHIFT;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400996 from = pos & (PAGE_CACHE_SIZE - 1);
997 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700998
Tao Maf19d5872012-12-10 14:05:51 -0500999 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
1000 ret = ext4_try_to_write_inline_data(mapping, inode, pos, len,
1001 flags, pagep);
1002 if (ret < 0)
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001003 return ret;
1004 if (ret == 1)
1005 return 0;
Tao Maf19d5872012-12-10 14:05:51 -05001006 }
1007
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001008 /*
1009 * grab_cache_page_write_begin() can take a long time if the
1010 * system is thrashing due to memory pressure, or if the page
1011 * is being written back. So grab it first before we start
1012 * the transaction handle. This also allows us to allocate
1013 * the page (if needed) without using GFP_NOFS.
1014 */
1015retry_grab:
Nick Piggin54566b22009-01-04 12:00:53 -08001016 page = grab_cache_page_write_begin(mapping, index, flags);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001017 if (!page)
1018 return -ENOMEM;
1019 unlock_page(page);
1020
1021retry_journal:
Theodore Ts'o9924a922013-02-08 21:59:22 -05001022 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001023 if (IS_ERR(handle)) {
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001024 page_cache_release(page);
1025 return PTR_ERR(handle);
Jan Karacf108bc2008-07-11 19:27:31 -04001026 }
Tao Maf19d5872012-12-10 14:05:51 -05001027
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001028 lock_page(page);
1029 if (page->mapping != mapping) {
1030 /* The page got truncated from under us */
1031 unlock_page(page);
1032 page_cache_release(page);
Jan Karacf108bc2008-07-11 19:27:31 -04001033 ext4_journal_stop(handle);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001034 goto retry_grab;
Jan Karacf108bc2008-07-11 19:27:31 -04001035 }
Dmitry Monakhov7afe5aa2013-08-28 14:30:47 -04001036 /* In case writeback began while the page was unlocked */
1037 wait_for_stable_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -04001038
Michael Halcrow2058f832015-04-12 00:55:10 -04001039#ifdef CONFIG_EXT4_FS_ENCRYPTION
1040 if (ext4_should_dioread_nolock(inode))
1041 ret = ext4_block_write_begin(page, pos, len,
1042 ext4_get_block_write);
1043 else
1044 ret = ext4_block_write_begin(page, pos, len,
1045 ext4_get_block);
1046#else
Jiaying Zhang744692d2010-03-04 16:14:02 -05001047 if (ext4_should_dioread_nolock(inode))
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001048 ret = __block_write_begin(page, pos, len, ext4_get_block_write);
Jiaying Zhang744692d2010-03-04 16:14:02 -05001049 else
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001050 ret = __block_write_begin(page, pos, len, ext4_get_block);
Michael Halcrow2058f832015-04-12 00:55:10 -04001051#endif
Nick Pigginbfc1af62007-10-16 01:25:05 -07001052 if (!ret && ext4_should_journal_data(inode)) {
Tao Maf19d5872012-12-10 14:05:51 -05001053 ret = ext4_walk_page_buffers(handle, page_buffers(page),
1054 from, to, NULL,
1055 do_journal_get_write_access);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001056 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001057
1058 if (ret) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001059 unlock_page(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001060 /*
Christoph Hellwig6e1db882010-06-04 11:29:57 +02001061 * __block_write_begin may have instantiated a few blocks
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001062 * outside i_size. Trim these off again. Don't need
1063 * i_size_read because we hold i_mutex.
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001064 *
1065 * Add inode to orphan list in case we crash before
1066 * truncate finishes
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001067 */
Jan Karaffacfa72009-07-13 16:22:22 -04001068 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001069 ext4_orphan_add(handle, inode);
1070
1071 ext4_journal_stop(handle);
1072 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001073 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001074 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001075 * If truncate failed early the inode might
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001076 * still be on the orphan list; we need to
1077 * make sure the inode is removed from the
1078 * orphan list in that case.
1079 */
1080 if (inode->i_nlink)
1081 ext4_orphan_del(NULL, inode);
1082 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001083
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001084 if (ret == -ENOSPC &&
1085 ext4_should_retry_alloc(inode->i_sb, &retries))
1086 goto retry_journal;
1087 page_cache_release(page);
1088 return ret;
1089 }
1090 *pagep = page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001091 return ret;
1092}
1093
Nick Pigginbfc1af62007-10-16 01:25:05 -07001094/* For write_end() in data=journal mode */
1095static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001096{
Theodore Ts'o13fca322013-04-21 16:45:54 -04001097 int ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001098 if (!buffer_mapped(bh) || buffer_freed(bh))
1099 return 0;
1100 set_buffer_uptodate(bh);
Theodore Ts'o13fca322013-04-21 16:45:54 -04001101 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
1102 clear_buffer_meta(bh);
1103 clear_buffer_prio(bh);
1104 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001105}
1106
Zheng Liueed43332013-04-03 12:41:17 -04001107/*
1108 * We need to pick up the new inode size which generic_commit_write gave us
1109 * `file' can be NULL - eg, when called from page_symlink().
1110 *
1111 * ext4 never places buffers on inode->i_mapping->private_list. metadata
1112 * buffers are managed internally.
1113 */
1114static int ext4_write_end(struct file *file,
1115 struct address_space *mapping,
1116 loff_t pos, unsigned len, unsigned copied,
1117 struct page *page, void *fsdata)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001118{
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001119 handle_t *handle = ext4_journal_current_handle();
Zheng Liueed43332013-04-03 12:41:17 -04001120 struct inode *inode = mapping->host;
Xiaoguang Wang05726392015-02-12 23:00:17 -05001121 loff_t old_size = inode->i_size;
Zheng Liueed43332013-04-03 12:41:17 -04001122 int ret = 0, ret2;
1123 int i_size_changed = 0;
1124
1125 trace_ext4_write_end(inode, pos, len, copied);
1126 if (ext4_test_inode_state(inode, EXT4_STATE_ORDERED_MODE)) {
1127 ret = ext4_jbd2_file_inode(handle, inode);
1128 if (ret) {
1129 unlock_page(page);
1130 page_cache_release(page);
1131 goto errout;
1132 }
1133 }
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001134
Theodore Ts'o42c832d2013-07-01 08:12:39 -04001135 if (ext4_has_inline_data(inode)) {
1136 ret = ext4_write_inline_data_end(inode, pos, len,
1137 copied, page);
1138 if (ret < 0)
1139 goto errout;
1140 copied = ret;
1141 } else
Tao Maf19d5872012-12-10 14:05:51 -05001142 copied = block_write_end(file, mapping, pos,
1143 len, copied, page, fsdata);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001144 /*
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001145 * it's important to update i_size while still holding page lock:
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001146 * page writeout could otherwise come in and zero beyond i_size.
1147 */
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001148 i_size_changed = ext4_update_inode_size(inode, pos + copied);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001149 unlock_page(page);
1150 page_cache_release(page);
1151
Xiaoguang Wang05726392015-02-12 23:00:17 -05001152 if (old_size < pos)
1153 pagecache_isize_extended(inode, old_size, pos);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001154 /*
1155 * Don't mark the inode dirty under page lock. First, it unnecessarily
1156 * makes the holding time of page lock longer. Second, it forces lock
1157 * ordering of page lock and transaction start for journaling
1158 * filesystems.
1159 */
1160 if (i_size_changed)
1161 ext4_mark_inode_dirty(handle, inode);
1162
Jan Karaffacfa72009-07-13 16:22:22 -04001163 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001164 /* if we have allocated more blocks and copied
1165 * less. We will have blocks allocated outside
1166 * inode->i_size. So truncate them
1167 */
1168 ext4_orphan_add(handle, inode);
Theodore Ts'o74d553a2013-04-03 12:39:17 -04001169errout:
Mingming Cao617ba132006-10-11 01:20:53 -07001170 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001171 if (!ret)
1172 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001173
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001174 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001175 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001176 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001177 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001178 * on the orphan list; we need to make sure the inode
1179 * is removed from the orphan list in that case.
1180 */
1181 if (inode->i_nlink)
1182 ext4_orphan_del(NULL, inode);
1183 }
1184
Nick Pigginbfc1af62007-10-16 01:25:05 -07001185 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001186}
1187
Nick Pigginbfc1af62007-10-16 01:25:05 -07001188static int ext4_journalled_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001189 struct address_space *mapping,
1190 loff_t pos, unsigned len, unsigned copied,
1191 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001192{
Mingming Cao617ba132006-10-11 01:20:53 -07001193 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -07001194 struct inode *inode = mapping->host;
Xiaoguang Wang05726392015-02-12 23:00:17 -05001195 loff_t old_size = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001196 int ret = 0, ret2;
1197 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001198 unsigned from, to;
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001199 int size_changed = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001200
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001201 trace_ext4_journalled_write_end(inode, pos, len, copied);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001202 from = pos & (PAGE_CACHE_SIZE - 1);
1203 to = from + len;
1204
Curt Wohlgemuth441c8502011-08-13 11:25:18 -04001205 BUG_ON(!ext4_handle_valid(handle));
1206
Tao Ma3fdcfb62012-12-10 14:05:57 -05001207 if (ext4_has_inline_data(inode))
1208 copied = ext4_write_inline_data_end(inode, pos, len,
1209 copied, page);
1210 else {
1211 if (copied < len) {
1212 if (!PageUptodate(page))
1213 copied = 0;
1214 page_zero_new_buffers(page, from+copied, to);
1215 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001216
Tao Ma3fdcfb62012-12-10 14:05:57 -05001217 ret = ext4_walk_page_buffers(handle, page_buffers(page), from,
1218 to, &partial, write_end_fn);
1219 if (!partial)
1220 SetPageUptodate(page);
1221 }
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001222 size_changed = ext4_update_inode_size(inode, pos + copied);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001223 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Jan Kara2d859db2011-07-26 09:07:11 -04001224 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001225 unlock_page(page);
1226 page_cache_release(page);
1227
Xiaoguang Wang05726392015-02-12 23:00:17 -05001228 if (old_size < pos)
1229 pagecache_isize_extended(inode, old_size, pos);
1230
Dmitry Monakhov4631dbf2014-08-23 17:48:28 -04001231 if (size_changed) {
Mingming Cao617ba132006-10-11 01:20:53 -07001232 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001233 if (!ret)
1234 ret = ret2;
1235 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001236
Jan Karaffacfa72009-07-13 16:22:22 -04001237 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001238 /* if we have allocated more blocks and copied
1239 * less. We will have blocks allocated outside
1240 * inode->i_size. So truncate them
1241 */
1242 ext4_orphan_add(handle, inode);
1243
Mingming Cao617ba132006-10-11 01:20:53 -07001244 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001245 if (!ret)
1246 ret = ret2;
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001247 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001248 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001249 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001250 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001251 * on the orphan list; we need to make sure the inode
1252 * is removed from the orphan list in that case.
1253 */
1254 if (inode->i_nlink)
1255 ext4_orphan_del(NULL, inode);
1256 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001257
1258 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001259}
Mingming Caod2a17632008-07-14 17:52:37 -04001260
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001261/*
Aditya Kali7b415bf2011-09-09 19:04:51 -04001262 * Reserve a single cluster located at lblock
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001263 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -05001264static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -04001265{
Mingming Cao60e58e02009-01-22 18:13:05 +01001266 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001267 struct ext4_inode_info *ei = EXT4_I(inode);
Aditya Kali7b415bf2011-09-09 19:04:51 -04001268 unsigned int md_needed;
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001269 int ret;
Mingming Caod2a17632008-07-14 17:52:37 -04001270
Mingming Cao60e58e02009-01-22 18:13:05 +01001271 /*
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001272 * We will charge metadata quota at writeout time; this saves
1273 * us from metadata over-estimation, though we may go over by
1274 * a small amount in the end. Here we just reserve for data.
Mingming Cao60e58e02009-01-22 18:13:05 +01001275 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04001276 ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001277 if (ret)
1278 return ret;
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001279
1280 /*
1281 * recalculate the amount of metadata blocks to reserve
1282 * in order to allocate nrblocks
1283 * worse case is one extent per block
1284 */
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001285 spin_lock(&ei->i_block_reservation_lock);
1286 /*
1287 * ext4_calc_metadata_amount() has side effects, which we have
1288 * to be prepared undo if we fail to claim space.
1289 */
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04001290 md_needed = 0;
1291 trace_ext4_da_reserve_space(inode, 0);
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001292
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04001293 if (ext4_claim_free_clusters(sbi, 1, 0)) {
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001294 spin_unlock(&ei->i_block_reservation_lock);
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001295 dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
Mingming Caod2a17632008-07-14 17:52:37 -04001296 return -ENOSPC;
1297 }
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001298 ei->i_reserved_data_blocks++;
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001299 spin_unlock(&ei->i_block_reservation_lock);
Dmitry Monakhov39bc6802009-12-10 16:36:27 +00001300
Mingming Caod2a17632008-07-14 17:52:37 -04001301 return 0; /* success */
1302}
1303
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001304static void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001305{
1306 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001307 struct ext4_inode_info *ei = EXT4_I(inode);
Mingming Caod2a17632008-07-14 17:52:37 -04001308
Mingming Caocd213222008-08-19 22:16:59 -04001309 if (!to_free)
1310 return; /* Nothing to release, exit */
1311
Mingming Caod2a17632008-07-14 17:52:37 -04001312 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001313
Li Zefan5a58ec82010-05-17 02:00:00 -04001314 trace_ext4_da_release_space(inode, to_free);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001315 if (unlikely(to_free > ei->i_reserved_data_blocks)) {
Mingming Caocd213222008-08-19 22:16:59 -04001316 /*
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001317 * if there aren't enough reserved blocks, then the
1318 * counter is messed up somewhere. Since this
1319 * function is called from invalidate page, it's
1320 * harmless to return without any action.
Mingming Caocd213222008-08-19 22:16:59 -04001321 */
Theodore Ts'o8de5c322013-02-14 15:11:41 -05001322 ext4_warning(inode->i_sb, "ext4_da_release_space: "
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001323 "ino %lu, to_free %d with only %d reserved "
Theodore Ts'o1084f252012-03-19 23:13:43 -04001324 "data blocks", inode->i_ino, to_free,
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001325 ei->i_reserved_data_blocks);
1326 WARN_ON(1);
1327 to_free = ei->i_reserved_data_blocks;
1328 }
1329 ei->i_reserved_data_blocks -= to_free;
1330
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001331 /* update fs dirty data blocks counter */
Theodore Ts'o57042652011-09-09 18:56:51 -04001332 percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free);
Mingming Caod2a17632008-07-14 17:52:37 -04001333
Mingming Caod2a17632008-07-14 17:52:37 -04001334 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001335
Aditya Kali7b415bf2011-09-09 19:04:51 -04001336 dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free));
Mingming Caod2a17632008-07-14 17:52:37 -04001337}
1338
1339static void ext4_da_page_release_reservation(struct page *page,
Lukas Czernerca99fdd2013-05-21 23:25:01 -04001340 unsigned int offset,
1341 unsigned int length)
Mingming Caod2a17632008-07-14 17:52:37 -04001342{
1343 int to_release = 0;
1344 struct buffer_head *head, *bh;
1345 unsigned int curr_off = 0;
Aditya Kali7b415bf2011-09-09 19:04:51 -04001346 struct inode *inode = page->mapping->host;
1347 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Lukas Czernerca99fdd2013-05-21 23:25:01 -04001348 unsigned int stop = offset + length;
Aditya Kali7b415bf2011-09-09 19:04:51 -04001349 int num_clusters;
Zheng Liu51865fd2012-11-08 21:57:32 -05001350 ext4_fsblk_t lblk;
Mingming Caod2a17632008-07-14 17:52:37 -04001351
Lukas Czernerca99fdd2013-05-21 23:25:01 -04001352 BUG_ON(stop > PAGE_CACHE_SIZE || stop < length);
1353
Mingming Caod2a17632008-07-14 17:52:37 -04001354 head = page_buffers(page);
1355 bh = head;
1356 do {
1357 unsigned int next_off = curr_off + bh->b_size;
1358
Lukas Czernerca99fdd2013-05-21 23:25:01 -04001359 if (next_off > stop)
1360 break;
1361
Mingming Caod2a17632008-07-14 17:52:37 -04001362 if ((offset <= curr_off) && (buffer_delay(bh))) {
1363 to_release++;
1364 clear_buffer_delay(bh);
1365 }
1366 curr_off = next_off;
1367 } while ((bh = bh->b_this_page) != head);
Aditya Kali7b415bf2011-09-09 19:04:51 -04001368
Zheng Liu51865fd2012-11-08 21:57:32 -05001369 if (to_release) {
1370 lblk = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1371 ext4_es_remove_extent(inode, lblk, to_release);
1372 }
1373
Aditya Kali7b415bf2011-09-09 19:04:51 -04001374 /* If we have released all the blocks belonging to a cluster, then we
1375 * need to release the reserved space for that cluster. */
1376 num_clusters = EXT4_NUM_B2C(sbi, to_release);
1377 while (num_clusters > 0) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04001378 lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) +
1379 ((num_clusters - 1) << sbi->s_cluster_bits);
1380 if (sbi->s_cluster_ratio == 1 ||
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05001381 !ext4_find_delalloc_cluster(inode, lblk))
Aditya Kali7b415bf2011-09-09 19:04:51 -04001382 ext4_da_release_space(inode, 1);
1383
1384 num_clusters--;
1385 }
Mingming Caod2a17632008-07-14 17:52:37 -04001386}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001387
1388/*
Alex Tomas64769242008-07-11 19:27:31 -04001389 * Delayed allocation stuff
1390 */
1391
Jan Kara4e7ea812013-06-04 13:17:40 -04001392struct mpage_da_data {
1393 struct inode *inode;
1394 struct writeback_control *wbc;
Jan Kara6b523df2013-06-04 13:21:11 -04001395
Jan Kara4e7ea812013-06-04 13:17:40 -04001396 pgoff_t first_page; /* The first page to write */
1397 pgoff_t next_page; /* Current page to examine */
1398 pgoff_t last_page; /* Last page to examine */
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001399 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04001400 * Extent to map - this can be after first_page because that can be
1401 * fully mapped. We somewhat abuse m_flags to store whether the extent
1402 * is delalloc or unwritten.
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001403 */
Jan Kara4e7ea812013-06-04 13:17:40 -04001404 struct ext4_map_blocks map;
1405 struct ext4_io_submit io_submit; /* IO submission data */
1406};
Alex Tomas64769242008-07-11 19:27:31 -04001407
Jan Kara4e7ea812013-06-04 13:17:40 -04001408static void mpage_release_unused_pages(struct mpage_da_data *mpd,
1409 bool invalidate)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001410{
1411 int nr_pages, i;
1412 pgoff_t index, end;
1413 struct pagevec pvec;
1414 struct inode *inode = mpd->inode;
1415 struct address_space *mapping = inode->i_mapping;
Jan Kara4e7ea812013-06-04 13:17:40 -04001416
1417 /* This is necessary when next_page == 0. */
1418 if (mpd->first_page >= mpd->next_page)
1419 return;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001420
Curt Wohlgemuthc7f59382011-02-26 12:27:52 -05001421 index = mpd->first_page;
1422 end = mpd->next_page - 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04001423 if (invalidate) {
1424 ext4_lblk_t start, last;
1425 start = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1426 last = end << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1427 ext4_es_remove_extent(inode, start, last - start + 1);
1428 }
Zheng Liu51865fd2012-11-08 21:57:32 -05001429
Eric Sandeen66bea922012-11-14 22:22:05 -05001430 pagevec_init(&pvec, 0);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001431 while (index <= end) {
1432 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1433 if (nr_pages == 0)
1434 break;
1435 for (i = 0; i < nr_pages; i++) {
1436 struct page *page = pvec.pages[i];
Jan Kara9b1d09982010-03-03 16:19:32 -05001437 if (page->index > end)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001438 break;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001439 BUG_ON(!PageLocked(page));
1440 BUG_ON(PageWriteback(page));
Jan Kara4e7ea812013-06-04 13:17:40 -04001441 if (invalidate) {
1442 block_invalidatepage(page, 0, PAGE_CACHE_SIZE);
1443 ClearPageUptodate(page);
1444 }
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001445 unlock_page(page);
1446 }
Jan Kara9b1d09982010-03-03 16:19:32 -05001447 index = pvec.pages[nr_pages - 1]->index + 1;
1448 pagevec_release(&pvec);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001449 }
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001450}
1451
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001452static void ext4_print_free_blocks(struct inode *inode)
1453{
1454 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o92b97812012-03-19 23:41:49 -04001455 struct super_block *sb = inode->i_sb;
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001456 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'o92b97812012-03-19 23:41:49 -04001457
1458 ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld",
Theodore Ts'o5dee5432011-09-09 19:10:51 -04001459 EXT4_C2B(EXT4_SB(inode->i_sb),
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001460 ext4_count_free_clusters(sb)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001461 ext4_msg(sb, KERN_CRIT, "Free/Dirty block details");
1462 ext4_msg(sb, KERN_CRIT, "free_blocks=%lld",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001463 (long long) EXT4_C2B(EXT4_SB(sb),
Theodore Ts'o57042652011-09-09 18:56:51 -04001464 percpu_counter_sum(&sbi->s_freeclusters_counter)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001465 ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001466 (long long) EXT4_C2B(EXT4_SB(sb),
Aditya Kali7b415bf2011-09-09 19:04:51 -04001467 percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001468 ext4_msg(sb, KERN_CRIT, "Block reservation details");
1469 ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001470 ei->i_reserved_data_blocks);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001471 return;
1472}
1473
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001474static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001475{
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001476 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001477}
1478
Alex Tomas64769242008-07-11 19:27:31 -04001479/*
Aditya Kali5356f262011-09-09 19:20:51 -04001480 * This function is grabs code from the very beginning of
1481 * ext4_map_blocks, but assumes that the caller is from delayed write
1482 * time. This function looks up the requested blocks and sets the
1483 * buffer delay bit under the protection of i_data_sem.
1484 */
1485static int ext4_da_map_blocks(struct inode *inode, sector_t iblock,
1486 struct ext4_map_blocks *map,
1487 struct buffer_head *bh)
1488{
Zheng Liud100eef2013-02-18 00:29:59 -05001489 struct extent_status es;
Aditya Kali5356f262011-09-09 19:20:51 -04001490 int retval;
1491 sector_t invalid_block = ~((sector_t) 0xffff);
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001492#ifdef ES_AGGRESSIVE_TEST
1493 struct ext4_map_blocks orig_map;
1494
1495 memcpy(&orig_map, map, sizeof(*map));
1496#endif
Aditya Kali5356f262011-09-09 19:20:51 -04001497
1498 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
1499 invalid_block = ~0;
1500
1501 map->m_flags = 0;
1502 ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
1503 "logical block %lu\n", inode->i_ino, map->m_len,
1504 (unsigned long) map->m_lblk);
Zheng Liud100eef2013-02-18 00:29:59 -05001505
1506 /* Lookup extent status tree firstly */
1507 if (ext4_es_lookup_extent(inode, iblock, &es)) {
Zheng Liud100eef2013-02-18 00:29:59 -05001508 if (ext4_es_is_hole(&es)) {
1509 retval = 0;
Lukas Czernerc8b459f2014-05-12 12:55:07 -04001510 down_read(&EXT4_I(inode)->i_data_sem);
Zheng Liud100eef2013-02-18 00:29:59 -05001511 goto add_delayed;
1512 }
1513
1514 /*
1515 * Delayed extent could be allocated by fallocate.
1516 * So we need to check it.
1517 */
1518 if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) {
1519 map_bh(bh, inode->i_sb, invalid_block);
1520 set_buffer_new(bh);
1521 set_buffer_delay(bh);
1522 return 0;
1523 }
1524
1525 map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk;
1526 retval = es.es_len - (iblock - es.es_lblk);
1527 if (retval > map->m_len)
1528 retval = map->m_len;
1529 map->m_len = retval;
1530 if (ext4_es_is_written(&es))
1531 map->m_flags |= EXT4_MAP_MAPPED;
1532 else if (ext4_es_is_unwritten(&es))
1533 map->m_flags |= EXT4_MAP_UNWRITTEN;
1534 else
1535 BUG_ON(1);
1536
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001537#ifdef ES_AGGRESSIVE_TEST
1538 ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0);
1539#endif
Zheng Liud100eef2013-02-18 00:29:59 -05001540 return retval;
1541 }
1542
Aditya Kali5356f262011-09-09 19:20:51 -04001543 /*
1544 * Try to see if we can get the block without requesting a new
1545 * file system block.
1546 */
Lukas Czernerc8b459f2014-05-12 12:55:07 -04001547 down_read(&EXT4_I(inode)->i_data_sem);
Jan Karacbd75842014-11-25 11:41:49 -05001548 if (ext4_has_inline_data(inode))
Tao Ma9c3569b2012-12-10 14:05:57 -05001549 retval = 0;
Jan Karacbd75842014-11-25 11:41:49 -05001550 else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Zheng Liu2f8e0a72014-11-25 11:44:37 -05001551 retval = ext4_ext_map_blocks(NULL, inode, map, 0);
Aditya Kali5356f262011-09-09 19:20:51 -04001552 else
Zheng Liu2f8e0a72014-11-25 11:44:37 -05001553 retval = ext4_ind_map_blocks(NULL, inode, map, 0);
Aditya Kali5356f262011-09-09 19:20:51 -04001554
Zheng Liud100eef2013-02-18 00:29:59 -05001555add_delayed:
Aditya Kali5356f262011-09-09 19:20:51 -04001556 if (retval == 0) {
Zheng Liuf7fec032013-02-18 00:28:47 -05001557 int ret;
Aditya Kali5356f262011-09-09 19:20:51 -04001558 /*
1559 * XXX: __block_prepare_write() unmaps passed block,
1560 * is it OK?
1561 */
Lukas Czerner386ad672013-03-10 22:50:00 -04001562 /*
1563 * If the block was allocated from previously allocated cluster,
1564 * then we don't need to reserve it again. However we still need
1565 * to reserve metadata for every block we're going to write.
1566 */
Jan Karacbd75842014-11-25 11:41:49 -05001567 if (EXT4_SB(inode->i_sb)->s_cluster_ratio <= 1 ||
1568 !ext4_find_delalloc_cluster(inode, map->m_lblk)) {
Zheng Liuf7fec032013-02-18 00:28:47 -05001569 ret = ext4_da_reserve_space(inode, iblock);
1570 if (ret) {
Aditya Kali5356f262011-09-09 19:20:51 -04001571 /* not enough space to reserve */
Zheng Liuf7fec032013-02-18 00:28:47 -05001572 retval = ret;
Aditya Kali5356f262011-09-09 19:20:51 -04001573 goto out_unlock;
Zheng Liuf7fec032013-02-18 00:28:47 -05001574 }
Aditya Kali5356f262011-09-09 19:20:51 -04001575 }
1576
Zheng Liuf7fec032013-02-18 00:28:47 -05001577 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
1578 ~0, EXTENT_STATUS_DELAYED);
1579 if (ret) {
1580 retval = ret;
Zheng Liu51865fd2012-11-08 21:57:32 -05001581 goto out_unlock;
Zheng Liuf7fec032013-02-18 00:28:47 -05001582 }
Zheng Liu51865fd2012-11-08 21:57:32 -05001583
Aditya Kali5356f262011-09-09 19:20:51 -04001584 map_bh(bh, inode->i_sb, invalid_block);
1585 set_buffer_new(bh);
1586 set_buffer_delay(bh);
Zheng Liuf7fec032013-02-18 00:28:47 -05001587 } else if (retval > 0) {
1588 int ret;
Theodore Ts'o3be78c72013-08-16 21:22:41 -04001589 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -05001590
Zheng Liu44fb851d2013-07-29 12:51:42 -04001591 if (unlikely(retval != map->m_len)) {
1592 ext4_warning(inode->i_sb,
1593 "ES len assertion failed for inode "
1594 "%lu: retval %d != map->m_len %d",
1595 inode->i_ino, retval, map->m_len);
1596 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001597 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001598
Zheng Liuf7fec032013-02-18 00:28:47 -05001599 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
1600 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
1601 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
1602 map->m_pblk, status);
1603 if (ret != 0)
1604 retval = ret;
Aditya Kali5356f262011-09-09 19:20:51 -04001605 }
1606
1607out_unlock:
1608 up_read((&EXT4_I(inode)->i_data_sem));
1609
1610 return retval;
1611}
1612
1613/*
Seunghun Leed91bd2c2014-09-01 22:15:30 -04001614 * This is a special get_block_t callback which is used by
Theodore Ts'ob920c752009-05-14 00:54:29 -04001615 * ext4_da_write_begin(). It will either return mapped block or
1616 * reserve space for a single block.
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001617 *
1618 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
1619 * We also have b_blocknr = -1 and b_bdev initialized properly
1620 *
1621 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
1622 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
1623 * initialized properly.
Alex Tomas64769242008-07-11 19:27:31 -04001624 */
Tao Ma9c3569b2012-12-10 14:05:57 -05001625int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
1626 struct buffer_head *bh, int create)
Alex Tomas64769242008-07-11 19:27:31 -04001627{
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001628 struct ext4_map_blocks map;
Alex Tomas64769242008-07-11 19:27:31 -04001629 int ret = 0;
1630
1631 BUG_ON(create == 0);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001632 BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
1633
1634 map.m_lblk = iblock;
1635 map.m_len = 1;
Alex Tomas64769242008-07-11 19:27:31 -04001636
1637 /*
1638 * first, we need to know whether the block is allocated already
1639 * preallocated blocks are unmapped but should treated
1640 * the same as allocated blocks.
1641 */
Aditya Kali5356f262011-09-09 19:20:51 -04001642 ret = ext4_da_map_blocks(inode, iblock, &map, bh);
1643 if (ret <= 0)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001644 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04001645
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001646 map_bh(bh, inode->i_sb, map.m_pblk);
1647 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
1648
1649 if (buffer_unwritten(bh)) {
1650 /* A delayed write to unwritten bh should be marked
1651 * new and mapped. Mapped ensures that we don't do
1652 * get_block multiple times when we write to the same
1653 * offset and new ensures that we do proper zero out
1654 * for partial write.
1655 */
1656 set_buffer_new(bh);
Theodore Ts'oc8205632011-04-10 22:30:07 -04001657 set_buffer_mapped(bh);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001658 }
1659 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04001660}
Mingming Cao61628a32008-07-11 19:27:31 -04001661
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001662static int bget_one(handle_t *handle, struct buffer_head *bh)
1663{
1664 get_bh(bh);
1665 return 0;
1666}
1667
1668static int bput_one(handle_t *handle, struct buffer_head *bh)
1669{
1670 put_bh(bh);
1671 return 0;
1672}
1673
1674static int __ext4_journalled_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001675 unsigned int len)
1676{
1677 struct address_space *mapping = page->mapping;
1678 struct inode *inode = mapping->host;
Tao Ma3fdcfb62012-12-10 14:05:57 -05001679 struct buffer_head *page_bufs = NULL;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001680 handle_t *handle = NULL;
Tao Ma3fdcfb62012-12-10 14:05:57 -05001681 int ret = 0, err = 0;
1682 int inline_data = ext4_has_inline_data(inode);
1683 struct buffer_head *inode_bh = NULL;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001684
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001685 ClearPageChecked(page);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001686
1687 if (inline_data) {
1688 BUG_ON(page->index != 0);
1689 BUG_ON(len > ext4_get_max_inline_size(inode));
1690 inode_bh = ext4_journalled_write_inline_data(inode, len, page);
1691 if (inode_bh == NULL)
1692 goto out;
1693 } else {
1694 page_bufs = page_buffers(page);
1695 if (!page_bufs) {
1696 BUG();
1697 goto out;
1698 }
1699 ext4_walk_page_buffers(handle, page_bufs, 0, len,
1700 NULL, bget_one);
1701 }
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001702 /* As soon as we unlock the page, it can go away, but we have
1703 * references to buffers so we are safe */
1704 unlock_page(page);
1705
Theodore Ts'o9924a922013-02-08 21:59:22 -05001706 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
1707 ext4_writepage_trans_blocks(inode));
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001708 if (IS_ERR(handle)) {
1709 ret = PTR_ERR(handle);
1710 goto out;
1711 }
1712
Curt Wohlgemuth441c8502011-08-13 11:25:18 -04001713 BUG_ON(!ext4_handle_valid(handle));
1714
Tao Ma3fdcfb62012-12-10 14:05:57 -05001715 if (inline_data) {
liang xie5d601252014-05-12 22:06:43 -04001716 BUFFER_TRACE(inode_bh, "get write access");
Tao Ma3fdcfb62012-12-10 14:05:57 -05001717 ret = ext4_journal_get_write_access(handle, inode_bh);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001718
Tao Ma3fdcfb62012-12-10 14:05:57 -05001719 err = ext4_handle_dirty_metadata(handle, inode, inode_bh);
1720
1721 } else {
1722 ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
1723 do_journal_get_write_access);
1724
1725 err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
1726 write_end_fn);
1727 }
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001728 if (ret == 0)
1729 ret = err;
Jan Kara2d859db2011-07-26 09:07:11 -04001730 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001731 err = ext4_journal_stop(handle);
1732 if (!ret)
1733 ret = err;
1734
Tao Ma3fdcfb62012-12-10 14:05:57 -05001735 if (!ext4_has_inline_data(inode))
Theodore Ts'o8c9367f2014-01-07 13:08:03 -05001736 ext4_walk_page_buffers(NULL, page_bufs, 0, len,
Tao Ma3fdcfb62012-12-10 14:05:57 -05001737 NULL, bput_one);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001738 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001739out:
Tao Ma3fdcfb62012-12-10 14:05:57 -05001740 brelse(inode_bh);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001741 return ret;
1742}
1743
Mingming Cao61628a32008-07-11 19:27:31 -04001744/*
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001745 * Note that we don't need to start a transaction unless we're journaling data
1746 * because we should have holes filled from ext4_page_mkwrite(). We even don't
1747 * need to file the inode to the transaction's list in ordered mode because if
1748 * we are writing back data added by write(), the inode is already there and if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001749 * we are writing back data modified via mmap(), no one guarantees in which
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001750 * transaction the data will hit the disk. In case we are journaling data, we
1751 * cannot start transaction directly because transaction start ranks above page
1752 * lock so we have to do some magic.
1753 *
Theodore Ts'ob920c752009-05-14 00:54:29 -04001754 * This function can get called via...
Theodore Ts'o20970ba2013-06-06 14:00:46 -04001755 * - ext4_writepages after taking page lock (have journal handle)
Theodore Ts'ob920c752009-05-14 00:54:29 -04001756 * - journal_submit_inode_data_buffers (no journal handle)
Artem Bityutskiyf6463b02012-07-25 18:12:04 +03001757 * - shrink_page_list via the kswapd/direct reclaim (no journal handle)
Theodore Ts'ob920c752009-05-14 00:54:29 -04001758 * - grab_page_cache when doing write_begin (have journal handle)
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001759 *
1760 * We don't do any block allocation in this function. If we have page with
1761 * multiple blocks we need to write those buffer_heads that are mapped. This
1762 * is important for mmaped based write. So if we do with blocksize 1K
1763 * truncate(f, 1024);
1764 * a = mmap(f, 0, 4096);
1765 * a[0] = 'a';
1766 * truncate(f, 4096);
1767 * we have in the page first buffer_head mapped via page_mkwrite call back
Paul Bolle90802ed2011-12-05 13:00:34 +01001768 * but other buffer_heads would be unmapped but dirty (dirty done via the
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001769 * do_wp_page). So writepage should write the first block. If we modify
1770 * the mmap area beyond 1024 we will again get a page_fault and the
1771 * page_mkwrite callback will do the block allocation and mark the
1772 * buffer_heads mapped.
1773 *
1774 * We redirty the page if we have any buffer_heads that is either delay or
1775 * unwritten in the page.
1776 *
1777 * We can get recursively called as show below.
1778 *
1779 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
1780 * ext4_writepage()
1781 *
1782 * But since we don't do any block allocation we should not deadlock.
1783 * Page also have the dirty flag cleared so we don't get recurive page_lock.
Mingming Cao61628a32008-07-11 19:27:31 -04001784 */
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001785static int ext4_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001786 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04001787{
Jan Karaf8bec372013-01-28 12:55:08 -05001788 int ret = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04001789 loff_t size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001790 unsigned int len;
Jiaying Zhang744692d2010-03-04 16:14:02 -05001791 struct buffer_head *page_bufs = NULL;
Mingming Cao61628a32008-07-11 19:27:31 -04001792 struct inode *inode = page->mapping->host;
Jan Kara36ade452013-01-28 09:30:52 -05001793 struct ext4_io_submit io_submit;
Namjae Jeon1c8349a2014-05-12 08:12:25 -04001794 bool keep_towrite = false;
Alex Tomas64769242008-07-11 19:27:31 -04001795
Lukas Czernera9c667f2011-06-06 09:51:52 -04001796 trace_ext4_writepage(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04001797 size = i_size_read(inode);
1798 if (page->index == size >> PAGE_CACHE_SHIFT)
1799 len = size & ~PAGE_CACHE_MASK;
1800 else
1801 len = PAGE_CACHE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04001802
Theodore Ts'oa42afc52010-10-27 21:30:09 -04001803 page_bufs = page_buffers(page);
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001804 /*
Jan Karafe386132013-01-28 21:06:42 -05001805 * We cannot do block allocation or other extent handling in this
1806 * function. If there are buffers needing that, we have to redirty
1807 * the page. But we may reach here when we do a journal commit via
1808 * journal_submit_inode_data_buffers() and in that case we must write
1809 * allocated buffers to achieve data=ordered mode guarantees.
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04001810 */
Tao Maf19d5872012-12-10 14:05:51 -05001811 if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL,
1812 ext4_bh_delay_or_unwritten)) {
Jan Karaf8bec372013-01-28 12:55:08 -05001813 redirty_page_for_writepage(wbc, page);
Jan Karafe386132013-01-28 21:06:42 -05001814 if (current->flags & PF_MEMALLOC) {
1815 /*
1816 * For memory cleaning there's no point in writing only
1817 * some buffers. So just bail out. Warn if we came here
1818 * from direct reclaim.
1819 */
1820 WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD))
1821 == PF_MEMALLOC);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04001822 unlock_page(page);
1823 return 0;
1824 }
Namjae Jeon1c8349a2014-05-12 08:12:25 -04001825 keep_towrite = true;
Theodore Ts'oa42afc52010-10-27 21:30:09 -04001826 }
Alex Tomas64769242008-07-11 19:27:31 -04001827
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001828 if (PageChecked(page) && ext4_should_journal_data(inode))
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001829 /*
1830 * It's mmapped pagecache. Add buffers and journal it. There
1831 * doesn't seem much point in redirtying the page here.
1832 */
Wu Fengguang3f0ca302009-11-24 11:15:44 -05001833 return __ext4_journalled_writepage(page, len);
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001834
Jan Kara97a851e2013-06-04 11:58:58 -04001835 ext4_io_submit_init(&io_submit, wbc);
1836 io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS);
1837 if (!io_submit.io_end) {
1838 redirty_page_for_writepage(wbc, page);
1839 unlock_page(page);
1840 return -ENOMEM;
1841 }
Namjae Jeon1c8349a2014-05-12 08:12:25 -04001842 ret = ext4_bio_write_page(&io_submit, page, len, wbc, keep_towrite);
Jan Kara36ade452013-01-28 09:30:52 -05001843 ext4_io_submit(&io_submit);
Jan Kara97a851e2013-06-04 11:58:58 -04001844 /* Drop io_end reference we got from init */
1845 ext4_put_io_end_defer(io_submit.io_end);
Alex Tomas64769242008-07-11 19:27:31 -04001846 return ret;
1847}
1848
Jan Kara5f1132b2013-08-17 10:02:33 -04001849static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page)
1850{
1851 int len;
1852 loff_t size = i_size_read(mpd->inode);
1853 int err;
1854
1855 BUG_ON(page->index != mpd->first_page);
1856 if (page->index == size >> PAGE_CACHE_SHIFT)
1857 len = size & ~PAGE_CACHE_MASK;
1858 else
1859 len = PAGE_CACHE_SIZE;
1860 clear_page_dirty_for_io(page);
Namjae Jeon1c8349a2014-05-12 08:12:25 -04001861 err = ext4_bio_write_page(&mpd->io_submit, page, len, mpd->wbc, false);
Jan Kara5f1132b2013-08-17 10:02:33 -04001862 if (!err)
1863 mpd->wbc->nr_to_write--;
1864 mpd->first_page++;
1865
1866 return err;
1867}
1868
Jan Kara4e7ea812013-06-04 13:17:40 -04001869#define BH_FLAGS ((1 << BH_Unwritten) | (1 << BH_Delay))
1870
Mingming Cao61628a32008-07-11 19:27:31 -04001871/*
Jan Karafffb2732013-06-04 13:01:11 -04001872 * mballoc gives us at most this number of blocks...
1873 * XXX: That seems to be only a limitation of ext4_mb_normalize_request().
Anatol Pomozov70261f52013-08-28 14:40:12 -04001874 * The rest of mballoc seems to handle chunks up to full group size.
Mingming Cao61628a32008-07-11 19:27:31 -04001875 */
Jan Karafffb2732013-06-04 13:01:11 -04001876#define MAX_WRITEPAGES_EXTENT_LEN 2048
Mingming Cao525f4ed2008-08-19 22:15:58 -04001877
Jan Karafffb2732013-06-04 13:01:11 -04001878/*
Jan Kara4e7ea812013-06-04 13:17:40 -04001879 * mpage_add_bh_to_extent - try to add bh to extent of blocks to map
1880 *
1881 * @mpd - extent of blocks
1882 * @lblk - logical number of the block in the file
Jan Kara09930042013-08-17 09:57:56 -04001883 * @bh - buffer head we want to add to the extent
Jan Kara4e7ea812013-06-04 13:17:40 -04001884 *
Jan Kara09930042013-08-17 09:57:56 -04001885 * The function is used to collect contig. blocks in the same state. If the
1886 * buffer doesn't require mapping for writeback and we haven't started the
1887 * extent of buffers to map yet, the function returns 'true' immediately - the
1888 * caller can write the buffer right away. Otherwise the function returns true
1889 * if the block has been added to the extent, false if the block couldn't be
1890 * added.
Jan Kara4e7ea812013-06-04 13:17:40 -04001891 */
Jan Kara09930042013-08-17 09:57:56 -04001892static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk,
1893 struct buffer_head *bh)
Jan Kara4e7ea812013-06-04 13:17:40 -04001894{
1895 struct ext4_map_blocks *map = &mpd->map;
1896
Jan Kara09930042013-08-17 09:57:56 -04001897 /* Buffer that doesn't need mapping for writeback? */
1898 if (!buffer_dirty(bh) || !buffer_mapped(bh) ||
1899 (!buffer_delay(bh) && !buffer_unwritten(bh))) {
1900 /* So far no extent to map => we write the buffer right away */
1901 if (map->m_len == 0)
1902 return true;
1903 return false;
1904 }
Jan Kara4e7ea812013-06-04 13:17:40 -04001905
1906 /* First block in the extent? */
1907 if (map->m_len == 0) {
1908 map->m_lblk = lblk;
1909 map->m_len = 1;
Jan Kara09930042013-08-17 09:57:56 -04001910 map->m_flags = bh->b_state & BH_FLAGS;
1911 return true;
Jan Kara4e7ea812013-06-04 13:17:40 -04001912 }
1913
Jan Kara09930042013-08-17 09:57:56 -04001914 /* Don't go larger than mballoc is willing to allocate */
1915 if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN)
1916 return false;
1917
Jan Kara4e7ea812013-06-04 13:17:40 -04001918 /* Can we merge the block to our big extent? */
1919 if (lblk == map->m_lblk + map->m_len &&
Jan Kara09930042013-08-17 09:57:56 -04001920 (bh->b_state & BH_FLAGS) == map->m_flags) {
Jan Kara4e7ea812013-06-04 13:17:40 -04001921 map->m_len++;
Jan Kara09930042013-08-17 09:57:56 -04001922 return true;
Jan Kara4e7ea812013-06-04 13:17:40 -04001923 }
Jan Kara09930042013-08-17 09:57:56 -04001924 return false;
Jan Kara4e7ea812013-06-04 13:17:40 -04001925}
1926
Jan Kara5f1132b2013-08-17 10:02:33 -04001927/*
1928 * mpage_process_page_bufs - submit page buffers for IO or add them to extent
1929 *
1930 * @mpd - extent of blocks for mapping
1931 * @head - the first buffer in the page
1932 * @bh - buffer we should start processing from
1933 * @lblk - logical number of the block in the file corresponding to @bh
1934 *
1935 * Walk through page buffers from @bh upto @head (exclusive) and either submit
1936 * the page for IO if all buffers in this page were mapped and there's no
1937 * accumulated extent of buffers to map or add buffers in the page to the
1938 * extent of buffers to map. The function returns 1 if the caller can continue
1939 * by processing the next page, 0 if it should stop adding buffers to the
1940 * extent to map because we cannot extend it anymore. It can also return value
1941 * < 0 in case of error during IO submission.
1942 */
1943static int mpage_process_page_bufs(struct mpage_da_data *mpd,
1944 struct buffer_head *head,
1945 struct buffer_head *bh,
1946 ext4_lblk_t lblk)
Jan Kara4e7ea812013-06-04 13:17:40 -04001947{
1948 struct inode *inode = mpd->inode;
Jan Kara5f1132b2013-08-17 10:02:33 -04001949 int err;
Jan Kara4e7ea812013-06-04 13:17:40 -04001950 ext4_lblk_t blocks = (i_size_read(inode) + (1 << inode->i_blkbits) - 1)
1951 >> inode->i_blkbits;
1952
1953 do {
1954 BUG_ON(buffer_locked(bh));
1955
Jan Kara09930042013-08-17 09:57:56 -04001956 if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) {
Jan Kara4e7ea812013-06-04 13:17:40 -04001957 /* Found extent to map? */
1958 if (mpd->map.m_len)
Jan Kara5f1132b2013-08-17 10:02:33 -04001959 return 0;
Jan Kara09930042013-08-17 09:57:56 -04001960 /* Everything mapped so far and we hit EOF */
Jan Kara5f1132b2013-08-17 10:02:33 -04001961 break;
Jan Kara4e7ea812013-06-04 13:17:40 -04001962 }
Jan Kara4e7ea812013-06-04 13:17:40 -04001963 } while (lblk++, (bh = bh->b_this_page) != head);
Jan Kara5f1132b2013-08-17 10:02:33 -04001964 /* So far everything mapped? Submit the page for IO. */
1965 if (mpd->map.m_len == 0) {
1966 err = mpage_submit_page(mpd, head->b_page);
1967 if (err < 0)
1968 return err;
1969 }
1970 return lblk < blocks;
Jan Kara4e7ea812013-06-04 13:17:40 -04001971}
1972
1973/*
1974 * mpage_map_buffers - update buffers corresponding to changed extent and
1975 * submit fully mapped pages for IO
1976 *
1977 * @mpd - description of extent to map, on return next extent to map
1978 *
1979 * Scan buffers corresponding to changed extent (we expect corresponding pages
1980 * to be already locked) and update buffer state according to new extent state.
1981 * We map delalloc buffers to their physical location, clear unwritten bits,
Lukas Czerner556615d2014-04-20 23:45:47 -04001982 * and mark buffers as uninit when we perform writes to unwritten extents
Jan Kara4e7ea812013-06-04 13:17:40 -04001983 * and do extent conversion after IO is finished. If the last page is not fully
1984 * mapped, we update @map to the next extent in the last page that needs
1985 * mapping. Otherwise we submit the page for IO.
1986 */
1987static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd)
1988{
1989 struct pagevec pvec;
1990 int nr_pages, i;
1991 struct inode *inode = mpd->inode;
1992 struct buffer_head *head, *bh;
1993 int bpp_bits = PAGE_CACHE_SHIFT - inode->i_blkbits;
Jan Kara4e7ea812013-06-04 13:17:40 -04001994 pgoff_t start, end;
1995 ext4_lblk_t lblk;
1996 sector_t pblock;
1997 int err;
1998
1999 start = mpd->map.m_lblk >> bpp_bits;
2000 end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits;
2001 lblk = start << bpp_bits;
2002 pblock = mpd->map.m_pblk;
2003
2004 pagevec_init(&pvec, 0);
2005 while (start <= end) {
2006 nr_pages = pagevec_lookup(&pvec, inode->i_mapping, start,
2007 PAGEVEC_SIZE);
2008 if (nr_pages == 0)
2009 break;
2010 for (i = 0; i < nr_pages; i++) {
2011 struct page *page = pvec.pages[i];
2012
2013 if (page->index > end)
2014 break;
Anatol Pomozov70261f52013-08-28 14:40:12 -04002015 /* Up to 'end' pages must be contiguous */
Jan Kara4e7ea812013-06-04 13:17:40 -04002016 BUG_ON(page->index != start);
2017 bh = head = page_buffers(page);
2018 do {
2019 if (lblk < mpd->map.m_lblk)
2020 continue;
2021 if (lblk >= mpd->map.m_lblk + mpd->map.m_len) {
2022 /*
2023 * Buffer after end of mapped extent.
2024 * Find next buffer in the page to map.
2025 */
2026 mpd->map.m_len = 0;
2027 mpd->map.m_flags = 0;
Jan Kara5f1132b2013-08-17 10:02:33 -04002028 /*
2029 * FIXME: If dioread_nolock supports
2030 * blocksize < pagesize, we need to make
2031 * sure we add size mapped so far to
2032 * io_end->size as the following call
2033 * can submit the page for IO.
2034 */
2035 err = mpage_process_page_bufs(mpd, head,
2036 bh, lblk);
Jan Kara4e7ea812013-06-04 13:17:40 -04002037 pagevec_release(&pvec);
Jan Kara5f1132b2013-08-17 10:02:33 -04002038 if (err > 0)
2039 err = 0;
2040 return err;
Jan Kara4e7ea812013-06-04 13:17:40 -04002041 }
2042 if (buffer_delay(bh)) {
2043 clear_buffer_delay(bh);
2044 bh->b_blocknr = pblock++;
2045 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002046 clear_buffer_unwritten(bh);
Jan Kara5f1132b2013-08-17 10:02:33 -04002047 } while (lblk++, (bh = bh->b_this_page) != head);
Jan Kara4e7ea812013-06-04 13:17:40 -04002048
2049 /*
2050 * FIXME: This is going to break if dioread_nolock
2051 * supports blocksize < pagesize as we will try to
2052 * convert potentially unmapped parts of inode.
2053 */
2054 mpd->io_submit.io_end->size += PAGE_CACHE_SIZE;
2055 /* Page fully mapped - let IO run! */
2056 err = mpage_submit_page(mpd, page);
2057 if (err < 0) {
2058 pagevec_release(&pvec);
2059 return err;
2060 }
2061 start++;
2062 }
2063 pagevec_release(&pvec);
2064 }
2065 /* Extent fully mapped and matches with page boundary. We are done. */
2066 mpd->map.m_len = 0;
2067 mpd->map.m_flags = 0;
2068 return 0;
2069}
2070
2071static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd)
2072{
2073 struct inode *inode = mpd->inode;
2074 struct ext4_map_blocks *map = &mpd->map;
2075 int get_blocks_flags;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002076 int err, dioread_nolock;
Jan Kara4e7ea812013-06-04 13:17:40 -04002077
2078 trace_ext4_da_write_pages_extent(inode, map);
2079 /*
2080 * Call ext4_map_blocks() to allocate any delayed allocation blocks, or
Lukas Czerner556615d2014-04-20 23:45:47 -04002081 * to convert an unwritten extent to be initialized (in the case
Jan Kara4e7ea812013-06-04 13:17:40 -04002082 * where we have written into one or more preallocated blocks). It is
2083 * possible that we're going to need more metadata blocks than
2084 * previously reserved. However we must not fail because we're in
2085 * writeback and there is nothing we can do about it so it might result
2086 * in data loss. So use reserved blocks to allocate metadata if
2087 * possible.
2088 *
Theodore Ts'o754cfed2014-09-04 18:08:22 -04002089 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if
2090 * the blocks in question are delalloc blocks. This indicates
2091 * that the blocks and quotas has already been checked when
2092 * the data was copied into the page cache.
Jan Kara4e7ea812013-06-04 13:17:40 -04002093 */
2094 get_blocks_flags = EXT4_GET_BLOCKS_CREATE |
2095 EXT4_GET_BLOCKS_METADATA_NOFAIL;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002096 dioread_nolock = ext4_should_dioread_nolock(inode);
2097 if (dioread_nolock)
Jan Kara4e7ea812013-06-04 13:17:40 -04002098 get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
2099 if (map->m_flags & (1 << BH_Delay))
2100 get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
2101
2102 err = ext4_map_blocks(handle, inode, map, get_blocks_flags);
2103 if (err < 0)
2104 return err;
Lukas Czerner090f32e2014-04-20 23:44:47 -04002105 if (dioread_nolock && (map->m_flags & EXT4_MAP_UNWRITTEN)) {
Jan Kara6b523df2013-06-04 13:21:11 -04002106 if (!mpd->io_submit.io_end->handle &&
2107 ext4_handle_valid(handle)) {
2108 mpd->io_submit.io_end->handle = handle->h_rsv_handle;
2109 handle->h_rsv_handle = NULL;
2110 }
Jan Kara3613d222013-06-04 13:19:34 -04002111 ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end);
Jan Kara6b523df2013-06-04 13:21:11 -04002112 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002113
2114 BUG_ON(map->m_len == 0);
2115 if (map->m_flags & EXT4_MAP_NEW) {
2116 struct block_device *bdev = inode->i_sb->s_bdev;
2117 int i;
2118
2119 for (i = 0; i < map->m_len; i++)
2120 unmap_underlying_metadata(bdev, map->m_pblk + i);
2121 }
2122 return 0;
2123}
2124
2125/*
2126 * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length
2127 * mpd->len and submit pages underlying it for IO
2128 *
2129 * @handle - handle for journal operations
2130 * @mpd - extent to map
Jan Kara7534e852013-10-16 08:26:08 -04002131 * @give_up_on_write - we set this to true iff there is a fatal error and there
2132 * is no hope of writing the data. The caller should discard
2133 * dirty pages to avoid infinite loops.
Jan Kara4e7ea812013-06-04 13:17:40 -04002134 *
2135 * The function maps extent starting at mpd->lblk of length mpd->len. If it is
2136 * delayed, blocks are allocated, if it is unwritten, we may need to convert
2137 * them to initialized or split the described range from larger unwritten
2138 * extent. Note that we need not map all the described range since allocation
2139 * can return less blocks or the range is covered by more unwritten extents. We
2140 * cannot map more because we are limited by reserved transaction credits. On
2141 * the other hand we always make sure that the last touched page is fully
2142 * mapped so that it can be written out (and thus forward progress is
2143 * guaranteed). After mapping we submit all mapped pages for IO.
2144 */
2145static int mpage_map_and_submit_extent(handle_t *handle,
Theodore Ts'ocb530542013-07-01 08:12:40 -04002146 struct mpage_da_data *mpd,
2147 bool *give_up_on_write)
Jan Kara4e7ea812013-06-04 13:17:40 -04002148{
2149 struct inode *inode = mpd->inode;
2150 struct ext4_map_blocks *map = &mpd->map;
2151 int err;
2152 loff_t disksize;
Dmitry Monakhov66031202014-08-27 18:40:03 -04002153 int progress = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002154
2155 mpd->io_submit.io_end->offset =
2156 ((loff_t)map->m_lblk) << inode->i_blkbits;
Jan Kara27d7c4e2013-07-05 21:57:22 -04002157 do {
Jan Kara4e7ea812013-06-04 13:17:40 -04002158 err = mpage_map_one_extent(handle, mpd);
2159 if (err < 0) {
2160 struct super_block *sb = inode->i_sb;
2161
Theodore Ts'ocb530542013-07-01 08:12:40 -04002162 if (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)
2163 goto invalidate_dirty_pages;
Jan Kara4e7ea812013-06-04 13:17:40 -04002164 /*
Theodore Ts'ocb530542013-07-01 08:12:40 -04002165 * Let the uper layers retry transient errors.
2166 * In the case of ENOSPC, if ext4_count_free_blocks()
2167 * is non-zero, a commit should free up blocks.
Jan Kara4e7ea812013-06-04 13:17:40 -04002168 */
Theodore Ts'ocb530542013-07-01 08:12:40 -04002169 if ((err == -ENOMEM) ||
Dmitry Monakhov66031202014-08-27 18:40:03 -04002170 (err == -ENOSPC && ext4_count_free_clusters(sb))) {
2171 if (progress)
2172 goto update_disksize;
Theodore Ts'ocb530542013-07-01 08:12:40 -04002173 return err;
Dmitry Monakhov66031202014-08-27 18:40:03 -04002174 }
Theodore Ts'ocb530542013-07-01 08:12:40 -04002175 ext4_msg(sb, KERN_CRIT,
2176 "Delayed block allocation failed for "
2177 "inode %lu at logical offset %llu with"
2178 " max blocks %u with error %d",
2179 inode->i_ino,
2180 (unsigned long long)map->m_lblk,
2181 (unsigned)map->m_len, -err);
2182 ext4_msg(sb, KERN_CRIT,
2183 "This should not happen!! Data will "
2184 "be lost\n");
2185 if (err == -ENOSPC)
2186 ext4_print_free_blocks(inode);
2187 invalidate_dirty_pages:
2188 *give_up_on_write = true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002189 return err;
2190 }
Dmitry Monakhov66031202014-08-27 18:40:03 -04002191 progress = 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04002192 /*
2193 * Update buffer state, submit mapped pages, and get us new
2194 * extent to map
2195 */
2196 err = mpage_map_and_submit_buffers(mpd);
2197 if (err < 0)
Dmitry Monakhov66031202014-08-27 18:40:03 -04002198 goto update_disksize;
Jan Kara27d7c4e2013-07-05 21:57:22 -04002199 } while (map->m_len);
Jan Kara4e7ea812013-06-04 13:17:40 -04002200
Dmitry Monakhov66031202014-08-27 18:40:03 -04002201update_disksize:
Theodore Ts'o622cad12014-04-11 10:35:17 -04002202 /*
2203 * Update on-disk size after IO is submitted. Races with
2204 * truncate are avoided by checking i_size under i_data_sem.
2205 */
Jan Kara4e7ea812013-06-04 13:17:40 -04002206 disksize = ((loff_t)mpd->first_page) << PAGE_CACHE_SHIFT;
Jan Kara4e7ea812013-06-04 13:17:40 -04002207 if (disksize > EXT4_I(inode)->i_disksize) {
2208 int err2;
Theodore Ts'o622cad12014-04-11 10:35:17 -04002209 loff_t i_size;
Jan Kara4e7ea812013-06-04 13:17:40 -04002210
Theodore Ts'o622cad12014-04-11 10:35:17 -04002211 down_write(&EXT4_I(inode)->i_data_sem);
2212 i_size = i_size_read(inode);
2213 if (disksize > i_size)
2214 disksize = i_size;
2215 if (disksize > EXT4_I(inode)->i_disksize)
2216 EXT4_I(inode)->i_disksize = disksize;
Jan Kara4e7ea812013-06-04 13:17:40 -04002217 err2 = ext4_mark_inode_dirty(handle, inode);
Theodore Ts'o622cad12014-04-11 10:35:17 -04002218 up_write(&EXT4_I(inode)->i_data_sem);
Jan Kara4e7ea812013-06-04 13:17:40 -04002219 if (err2)
2220 ext4_error(inode->i_sb,
2221 "Failed to mark inode %lu dirty",
2222 inode->i_ino);
2223 if (!err)
2224 err = err2;
2225 }
2226 return err;
2227}
2228
2229/*
Jan Karafffb2732013-06-04 13:01:11 -04002230 * Calculate the total number of credits to reserve for one writepages
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002231 * iteration. This is called from ext4_writepages(). We map an extent of
Anatol Pomozov70261f52013-08-28 14:40:12 -04002232 * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping
Jan Karafffb2732013-06-04 13:01:11 -04002233 * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN +
2234 * bpp - 1 blocks in bpp different extents.
2235 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002236static int ext4_da_writepages_trans_blocks(struct inode *inode)
2237{
Jan Karafffb2732013-06-04 13:01:11 -04002238 int bpp = ext4_journal_blocks_per_page(inode);
Mingming Cao525f4ed2008-08-19 22:15:58 -04002239
Jan Karafffb2732013-06-04 13:01:11 -04002240 return ext4_meta_trans_blocks(inode,
2241 MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp);
Mingming Cao525f4ed2008-08-19 22:15:58 -04002242}
Mingming Cao61628a32008-07-11 19:27:31 -04002243
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002244/*
Jan Kara4e7ea812013-06-04 13:17:40 -04002245 * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages
2246 * and underlying extent to map
2247 *
2248 * @mpd - where to look for pages
2249 *
2250 * Walk dirty pages in the mapping. If they are fully mapped, submit them for
2251 * IO immediately. When we find a page which isn't mapped we start accumulating
2252 * extent of buffers underlying these pages that needs mapping (formed by
2253 * either delayed or unwritten buffers). We also lock the pages containing
2254 * these buffers. The extent found is returned in @mpd structure (starting at
2255 * mpd->lblk with length mpd->len blocks).
2256 *
2257 * Note that this function can attach bios to one io_end structure which are
2258 * neither logically nor physically contiguous. Although it may seem as an
2259 * unnecessary complication, it is actually inevitable in blocksize < pagesize
2260 * 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 -04002261 */
Jan Kara4e7ea812013-06-04 13:17:40 -04002262static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd)
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002263{
Jan Kara4e7ea812013-06-04 13:17:40 -04002264 struct address_space *mapping = mpd->inode->i_mapping;
2265 struct pagevec pvec;
2266 unsigned int nr_pages;
Ming Leiaeac5892013-10-17 18:56:16 -04002267 long left = mpd->wbc->nr_to_write;
Jan Kara4e7ea812013-06-04 13:17:40 -04002268 pgoff_t index = mpd->first_page;
2269 pgoff_t end = mpd->last_page;
2270 int tag;
2271 int i, err = 0;
2272 int blkbits = mpd->inode->i_blkbits;
2273 ext4_lblk_t lblk;
2274 struct buffer_head *head;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002275
Jan Kara4e7ea812013-06-04 13:17:40 -04002276 if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages)
Eric Sandeen5b41d922010-10-27 21:30:13 -04002277 tag = PAGECACHE_TAG_TOWRITE;
2278 else
2279 tag = PAGECACHE_TAG_DIRTY;
2280
Jan Kara4e7ea812013-06-04 13:17:40 -04002281 pagevec_init(&pvec, 0);
2282 mpd->map.m_len = 0;
2283 mpd->next_page = index;
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002284 while (index <= end) {
Eric Sandeen5b41d922010-10-27 21:30:13 -04002285 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002286 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
2287 if (nr_pages == 0)
Jan Kara4e7ea812013-06-04 13:17:40 -04002288 goto out;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002289
2290 for (i = 0; i < nr_pages; i++) {
2291 struct page *page = pvec.pages[i];
2292
2293 /*
2294 * At this point, the page may be truncated or
2295 * invalidated (changing page->mapping to NULL), or
2296 * even swizzled back from swapper_space to tmpfs file
2297 * mapping. However, page->index will not change
2298 * because we have a reference on the page.
2299 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002300 if (page->index > end)
2301 goto out;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002302
Ming Leiaeac5892013-10-17 18:56:16 -04002303 /*
2304 * Accumulated enough dirty pages? This doesn't apply
2305 * to WB_SYNC_ALL mode. For integrity sync we have to
2306 * keep going because someone may be concurrently
2307 * dirtying pages, and we might have synced a lot of
2308 * newly appeared dirty pages, but have not synced all
2309 * of the old dirty pages.
2310 */
2311 if (mpd->wbc->sync_mode == WB_SYNC_NONE && left <= 0)
2312 goto out;
2313
Jan Kara4e7ea812013-06-04 13:17:40 -04002314 /* If we can't merge this page, we are done. */
2315 if (mpd->map.m_len > 0 && mpd->next_page != page->index)
2316 goto out;
Theodore Ts'o78aaced2011-02-26 14:09:14 -05002317
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002318 lock_page(page);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002319 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002320 * If the page is no longer dirty, or its mapping no
2321 * longer corresponds to inode we are writing (which
2322 * means it has been truncated or invalidated), or the
2323 * page is already under writeback and we are not doing
2324 * a data integrity writeback, skip the page
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002325 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002326 if (!PageDirty(page) ||
2327 (PageWriteback(page) &&
Jan Kara4e7ea812013-06-04 13:17:40 -04002328 (mpd->wbc->sync_mode == WB_SYNC_NONE)) ||
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002329 unlikely(page->mapping != mapping)) {
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002330 unlock_page(page);
2331 continue;
2332 }
2333
Darrick J. Wong7cb1a532011-05-18 13:53:20 -04002334 wait_on_page_writeback(page);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002335 BUG_ON(PageWriteback(page));
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002336
Jan Kara4e7ea812013-06-04 13:17:40 -04002337 if (mpd->map.m_len == 0)
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002338 mpd->first_page = page->index;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002339 mpd->next_page = page->index + 1;
Jan Karaf8bec372013-01-28 12:55:08 -05002340 /* Add all dirty buffers to mpd */
Jan Kara4e7ea812013-06-04 13:17:40 -04002341 lblk = ((ext4_lblk_t)page->index) <<
2342 (PAGE_CACHE_SHIFT - blkbits);
Jan Karaf8bec372013-01-28 12:55:08 -05002343 head = page_buffers(page);
Jan Kara5f1132b2013-08-17 10:02:33 -04002344 err = mpage_process_page_bufs(mpd, head, head, lblk);
2345 if (err <= 0)
Jan Kara4e7ea812013-06-04 13:17:40 -04002346 goto out;
Jan Kara5f1132b2013-08-17 10:02:33 -04002347 err = 0;
Ming Leiaeac5892013-10-17 18:56:16 -04002348 left--;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002349 }
2350 pagevec_release(&pvec);
2351 cond_resched();
2352 }
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002353 return 0;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002354out:
2355 pagevec_release(&pvec);
Jan Kara4e7ea812013-06-04 13:17:40 -04002356 return err;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002357}
2358
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002359static int __writepage(struct page *page, struct writeback_control *wbc,
2360 void *data)
2361{
2362 struct address_space *mapping = data;
2363 int ret = ext4_writepage(page, wbc);
2364 mapping_set_error(mapping, ret);
2365 return ret;
2366}
2367
2368static int ext4_writepages(struct address_space *mapping,
2369 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002370{
Jan Kara4e7ea812013-06-04 13:17:40 -04002371 pgoff_t writeback_index = 0;
2372 long nr_to_write = wbc->nr_to_write;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002373 int range_whole = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002374 int cycled = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002375 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002376 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002377 struct inode *inode = mapping->host;
Jan Kara6b523df2013-06-04 13:21:11 -04002378 int needed_blocks, rsv_blocks = 0, ret = 0;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002379 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Jan Kara4e7ea812013-06-04 13:17:40 -04002380 bool done;
Shaohua Li1bce63d2011-10-18 10:55:51 -04002381 struct blk_plug plug;
Theodore Ts'ocb530542013-07-01 08:12:40 -04002382 bool give_up_on_write = false;
Mingming Cao61628a32008-07-11 19:27:31 -04002383
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002384 trace_ext4_writepages(inode, wbc);
Theodore Ts'oba80b102009-01-03 20:03:21 -05002385
Mingming Cao61628a32008-07-11 19:27:31 -04002386 /*
2387 * No pages to write? This is mainly a kludge to avoid starting
2388 * a transaction for special inodes like journal inode on last iput()
2389 * because that could violate lock ordering on umount
2390 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002391 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Ming Leibbf023c2013-10-30 07:27:16 -04002392 goto out_writepages;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002393
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002394 if (ext4_should_journal_data(inode)) {
2395 struct blk_plug plug;
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002396
2397 blk_start_plug(&plug);
2398 ret = write_cache_pages(mapping, wbc, __writepage, mapping);
2399 blk_finish_plug(&plug);
Ming Leibbf023c2013-10-30 07:27:16 -04002400 goto out_writepages;
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002401 }
2402
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002403 /*
2404 * If the filesystem has aborted, it is read-only, so return
2405 * right away instead of dumping stack traces later on that
2406 * will obscure the real source of the problem. We test
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002407 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002408 * the latter could be true if the filesystem is mounted
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002409 * read-only, and in that case, ext4_writepages should
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002410 * *never* be called, so if that ever happens, we would want
2411 * the stack trace.
2412 */
Ming Leibbf023c2013-10-30 07:27:16 -04002413 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) {
2414 ret = -EROFS;
2415 goto out_writepages;
2416 }
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002417
Jan Kara6b523df2013-06-04 13:21:11 -04002418 if (ext4_should_dioread_nolock(inode)) {
2419 /*
Anatol Pomozov70261f52013-08-28 14:40:12 -04002420 * We may need to convert up to one extent per block in
Jan Kara6b523df2013-06-04 13:21:11 -04002421 * the page and we may dirty the inode.
2422 */
2423 rsv_blocks = 1 + (PAGE_CACHE_SIZE >> inode->i_blkbits);
2424 }
2425
Jan Kara4e7ea812013-06-04 13:17:40 -04002426 /*
2427 * If we have inline data and arrive here, it means that
2428 * we will soon create the block for the 1st page, so
2429 * we'd better clear the inline data here.
2430 */
2431 if (ext4_has_inline_data(inode)) {
2432 /* Just inode will be modified... */
2433 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
2434 if (IS_ERR(handle)) {
2435 ret = PTR_ERR(handle);
2436 goto out_writepages;
2437 }
2438 BUG_ON(ext4_test_inode_state(inode,
2439 EXT4_STATE_MAY_INLINE_DATA));
2440 ext4_destroy_inline_data(handle, inode);
2441 ext4_journal_stop(handle);
2442 }
2443
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002444 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2445 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002446
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002447 if (wbc->range_cyclic) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002448 writeback_index = mapping->writeback_index;
2449 if (writeback_index)
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002450 cycled = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002451 mpd.first_page = writeback_index;
2452 mpd.last_page = -1;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002453 } else {
Jan Kara4e7ea812013-06-04 13:17:40 -04002454 mpd.first_page = wbc->range_start >> PAGE_CACHE_SHIFT;
2455 mpd.last_page = wbc->range_end >> PAGE_CACHE_SHIFT;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002456 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002457
Jan Kara4e7ea812013-06-04 13:17:40 -04002458 mpd.inode = inode;
2459 mpd.wbc = wbc;
2460 ext4_io_submit_init(&mpd.io_submit, wbc);
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002461retry:
Wu Fengguang6e6938b2010-06-06 10:38:15 -06002462 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Jan Kara4e7ea812013-06-04 13:17:40 -04002463 tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page);
2464 done = false;
Shaohua Li1bce63d2011-10-18 10:55:51 -04002465 blk_start_plug(&plug);
Jan Kara4e7ea812013-06-04 13:17:40 -04002466 while (!done && mpd.first_page <= mpd.last_page) {
2467 /* For each extent of pages we use new io_end */
2468 mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
2469 if (!mpd.io_submit.io_end) {
2470 ret = -ENOMEM;
2471 break;
2472 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002473
2474 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002475 * We have two constraints: We find one extent to map and we
2476 * must always write out whole page (makes a difference when
2477 * blocksize < pagesize) so that we don't block on IO when we
2478 * try to write out the rest of the page. Journalled mode is
2479 * not supported by delalloc.
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002480 */
2481 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04002482 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002483
Jan Kara4e7ea812013-06-04 13:17:40 -04002484 /* start a new transaction */
Jan Kara6b523df2013-06-04 13:21:11 -04002485 handle = ext4_journal_start_with_reserve(inode,
2486 EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks);
Mingming Cao61628a32008-07-11 19:27:31 -04002487 if (IS_ERR(handle)) {
2488 ret = PTR_ERR(handle);
Theodore Ts'o16939182009-09-26 17:43:59 -04002489 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
Curt Wohlgemuthfbe845d2010-05-16 13:00:00 -04002490 "%ld pages, ino %lu; err %d", __func__,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002491 wbc->nr_to_write, inode->i_ino, ret);
Jan Kara4e7ea812013-06-04 13:17:40 -04002492 /* Release allocated io_end */
2493 ext4_put_io_end(mpd.io_submit.io_end);
2494 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002495 }
Theodore Ts'of63e6002009-02-23 16:42:39 -05002496
Jan Kara4e7ea812013-06-04 13:17:40 -04002497 trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc);
2498 ret = mpage_prepare_extent_to_map(&mpd);
2499 if (!ret) {
2500 if (mpd.map.m_len)
Theodore Ts'ocb530542013-07-01 08:12:40 -04002501 ret = mpage_map_and_submit_extent(handle, &mpd,
2502 &give_up_on_write);
Jan Kara4e7ea812013-06-04 13:17:40 -04002503 else {
2504 /*
2505 * We scanned the whole range (or exhausted
2506 * nr_to_write), submitted what was mapped and
2507 * didn't find anything needing mapping. We are
2508 * done.
2509 */
2510 done = true;
2511 }
Theodore Ts'of63e6002009-02-23 16:42:39 -05002512 }
Mingming Cao61628a32008-07-11 19:27:31 -04002513 ext4_journal_stop(handle);
Jan Kara4e7ea812013-06-04 13:17:40 -04002514 /* Submit prepared bio */
2515 ext4_io_submit(&mpd.io_submit);
2516 /* Unlock pages we didn't use */
Theodore Ts'ocb530542013-07-01 08:12:40 -04002517 mpage_release_unused_pages(&mpd, give_up_on_write);
Jan Kara4e7ea812013-06-04 13:17:40 -04002518 /* Drop our io_end reference we got from init */
2519 ext4_put_io_end(mpd.io_submit.io_end);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002520
Jan Kara4e7ea812013-06-04 13:17:40 -04002521 if (ret == -ENOSPC && sbi->s_journal) {
2522 /*
2523 * Commit the transaction which would
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002524 * free blocks released in the transaction
2525 * and try again
2526 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002527 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002528 ret = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002529 continue;
2530 }
2531 /* Fatal error - ENOMEM, EIO... */
2532 if (ret)
Mingming Cao61628a32008-07-11 19:27:31 -04002533 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002534 }
Shaohua Li1bce63d2011-10-18 10:55:51 -04002535 blk_finish_plug(&plug);
Jan Kara9c12a832013-09-16 08:24:26 -04002536 if (!ret && !cycled && wbc->nr_to_write > 0) {
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002537 cycled = 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04002538 mpd.last_page = writeback_index - 1;
2539 mpd.first_page = 0;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002540 goto retry;
2541 }
Mingming Cao61628a32008-07-11 19:27:31 -04002542
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002543 /* Update index */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002544 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2545 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002546 * Set the writeback_index so that range_cyclic
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002547 * mode will write it back later
2548 */
Jan Kara4e7ea812013-06-04 13:17:40 -04002549 mapping->writeback_index = mpd.first_page;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002550
Mingming Cao61628a32008-07-11 19:27:31 -04002551out_writepages:
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002552 trace_ext4_writepages_result(inode, wbc, ret,
2553 nr_to_write - wbc->nr_to_write);
Mingming Cao61628a32008-07-11 19:27:31 -04002554 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002555}
2556
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002557static int ext4_nonda_switch(struct super_block *sb)
2558{
Eric Whitney5c1ff332013-04-09 09:27:31 -04002559 s64 free_clusters, dirty_clusters;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002560 struct ext4_sb_info *sbi = EXT4_SB(sb);
2561
2562 /*
2563 * switch to non delalloc mode if we are running low
2564 * on free block. The free block accounting via percpu
Eric Dumazet179f7eb2009-01-06 14:41:04 -08002565 * counters can get slightly wrong with percpu_counter_batch getting
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002566 * accumulated on each CPU without updating global counters
2567 * Delalloc need an accurate free block accounting. So switch
2568 * to non delalloc when we are near to error range.
2569 */
Eric Whitney5c1ff332013-04-09 09:27:31 -04002570 free_clusters =
2571 percpu_counter_read_positive(&sbi->s_freeclusters_counter);
2572 dirty_clusters =
2573 percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
Theodore Ts'o00d4e732012-09-19 22:42:36 -04002574 /*
2575 * Start pushing delalloc when 1/2 of free blocks are dirty.
2576 */
Eric Whitney5c1ff332013-04-09 09:27:31 -04002577 if (dirty_clusters && (free_clusters < 2 * dirty_clusters))
Miao Xie10ee27a2013-01-10 13:47:57 +08002578 try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE);
Theodore Ts'o00d4e732012-09-19 22:42:36 -04002579
Eric Whitney5c1ff332013-04-09 09:27:31 -04002580 if (2 * free_clusters < 3 * dirty_clusters ||
2581 free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) {
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002582 /*
Eric Sandeenc8afb442009-12-23 07:58:12 -05002583 * free block count is less than 150% of dirty blocks
2584 * or free blocks is less than watermark
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002585 */
2586 return 1;
2587 }
2588 return 0;
2589}
2590
Eric Sandeen0ff89472014-10-11 19:51:17 -04002591/* We always reserve for an inode update; the superblock could be there too */
2592static int ext4_da_write_credits(struct inode *inode, loff_t pos, unsigned len)
2593{
2594 if (likely(EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
2595 EXT4_FEATURE_RO_COMPAT_LARGE_FILE)))
2596 return 1;
2597
2598 if (pos + len <= 0x7fffffffULL)
2599 return 1;
2600
2601 /* We might need to update the superblock to set LARGE_FILE */
2602 return 2;
2603}
2604
Alex Tomas64769242008-07-11 19:27:31 -04002605static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002606 loff_t pos, unsigned len, unsigned flags,
2607 struct page **pagep, void **fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04002608{
Eric Sandeen72b8ab92010-05-16 11:00:00 -04002609 int ret, retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002610 struct page *page;
2611 pgoff_t index;
Alex Tomas64769242008-07-11 19:27:31 -04002612 struct inode *inode = mapping->host;
2613 handle_t *handle;
2614
2615 index = pos >> PAGE_CACHE_SHIFT;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002616
2617 if (ext4_nonda_switch(inode->i_sb)) {
2618 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
2619 return ext4_write_begin(file, mapping, pos,
2620 len, flags, pagep, fsdata);
2621 }
2622 *fsdata = (void *)0;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002623 trace_ext4_da_write_begin(inode, pos, len, flags);
Tao Ma9c3569b2012-12-10 14:05:57 -05002624
2625 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2626 ret = ext4_da_write_inline_data_begin(mapping, inode,
2627 pos, len, flags,
2628 pagep, fsdata);
2629 if (ret < 0)
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002630 return ret;
2631 if (ret == 1)
2632 return 0;
Tao Ma9c3569b2012-12-10 14:05:57 -05002633 }
2634
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002635 /*
2636 * grab_cache_page_write_begin() can take a long time if the
2637 * system is thrashing due to memory pressure, or if the page
2638 * is being written back. So grab it first before we start
2639 * the transaction handle. This also allows us to allocate
2640 * the page (if needed) without using GFP_NOFS.
2641 */
2642retry_grab:
2643 page = grab_cache_page_write_begin(mapping, index, flags);
2644 if (!page)
2645 return -ENOMEM;
2646 unlock_page(page);
2647
Alex Tomas64769242008-07-11 19:27:31 -04002648 /*
2649 * With delayed allocation, we don't log the i_disksize update
2650 * if there is delayed block allocation. But we still need
2651 * to journalling the i_disksize update if writes to the end
2652 * of file which has an already mapped buffer.
2653 */
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002654retry_journal:
Eric Sandeen0ff89472014-10-11 19:51:17 -04002655 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
2656 ext4_da_write_credits(inode, pos, len));
Alex Tomas64769242008-07-11 19:27:31 -04002657 if (IS_ERR(handle)) {
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002658 page_cache_release(page);
2659 return PTR_ERR(handle);
Alex Tomas64769242008-07-11 19:27:31 -04002660 }
2661
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002662 lock_page(page);
2663 if (page->mapping != mapping) {
2664 /* The page got truncated from under us */
2665 unlock_page(page);
2666 page_cache_release(page);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002667 ext4_journal_stop(handle);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002668 goto retry_grab;
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002669 }
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002670 /* In case writeback began while the page was unlocked */
Dmitry Monakhov7afe5aa2013-08-28 14:30:47 -04002671 wait_for_stable_page(page);
Alex Tomas64769242008-07-11 19:27:31 -04002672
Michael Halcrow2058f832015-04-12 00:55:10 -04002673#ifdef CONFIG_EXT4_FS_ENCRYPTION
2674 ret = ext4_block_write_begin(page, pos, len,
2675 ext4_da_get_block_prep);
2676#else
Christoph Hellwig6e1db882010-06-04 11:29:57 +02002677 ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
Michael Halcrow2058f832015-04-12 00:55:10 -04002678#endif
Alex Tomas64769242008-07-11 19:27:31 -04002679 if (ret < 0) {
2680 unlock_page(page);
2681 ext4_journal_stop(handle);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04002682 /*
2683 * block_write_begin may have instantiated a few blocks
2684 * outside i_size. Trim these off again. Don't need
2685 * i_size_read because we hold i_mutex.
2686 */
2687 if (pos + len > inode->i_size)
Jan Karab9a42072009-12-08 21:24:33 -05002688 ext4_truncate_failed_write(inode);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002689
2690 if (ret == -ENOSPC &&
2691 ext4_should_retry_alloc(inode->i_sb, &retries))
2692 goto retry_journal;
2693
2694 page_cache_release(page);
2695 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002696 }
2697
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002698 *pagep = page;
Alex Tomas64769242008-07-11 19:27:31 -04002699 return ret;
2700}
2701
Mingming Cao632eaea2008-07-11 19:27:31 -04002702/*
2703 * Check if we should update i_disksize
2704 * when write to the end of file but not require block allocation
2705 */
2706static int ext4_da_should_update_i_disksize(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002707 unsigned long offset)
Mingming Cao632eaea2008-07-11 19:27:31 -04002708{
2709 struct buffer_head *bh;
2710 struct inode *inode = page->mapping->host;
2711 unsigned int idx;
2712 int i;
2713
2714 bh = page_buffers(page);
2715 idx = offset >> inode->i_blkbits;
2716
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002717 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04002718 bh = bh->b_this_page;
2719
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002720 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
Mingming Cao632eaea2008-07-11 19:27:31 -04002721 return 0;
2722 return 1;
2723}
2724
Alex Tomas64769242008-07-11 19:27:31 -04002725static int ext4_da_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002726 struct address_space *mapping,
2727 loff_t pos, unsigned len, unsigned copied,
2728 struct page *page, void *fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04002729{
2730 struct inode *inode = mapping->host;
2731 int ret = 0, ret2;
2732 handle_t *handle = ext4_journal_current_handle();
2733 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04002734 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002735 int write_mode = (int)(unsigned long)fsdata;
2736
Theodore Ts'o74d553a2013-04-03 12:39:17 -04002737 if (write_mode == FALL_BACK_TO_NONDELALLOC)
2738 return ext4_write_end(file, mapping, pos,
2739 len, copied, page, fsdata);
Mingming Cao632eaea2008-07-11 19:27:31 -04002740
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002741 trace_ext4_da_write_end(inode, pos, len, copied);
Mingming Cao632eaea2008-07-11 19:27:31 -04002742 start = pos & (PAGE_CACHE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002743 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04002744
2745 /*
2746 * generic_write_end() will run mark_inode_dirty() if i_size
2747 * changes. So let's piggyback the i_disksize mark_inode_dirty
2748 * into that.
2749 */
Alex Tomas64769242008-07-11 19:27:31 -04002750 new_i_size = pos + copied;
Andrea Arcangeliea51d132011-12-13 21:41:15 -05002751 if (copied && new_i_size > EXT4_I(inode)->i_disksize) {
Tao Ma9c3569b2012-12-10 14:05:57 -05002752 if (ext4_has_inline_data(inode) ||
2753 ext4_da_should_update_i_disksize(page, end)) {
Dmitry Monakhovee124d22014-08-30 23:34:06 -04002754 ext4_update_i_disksize(inode, new_i_size);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04002755 /* We need to mark inode dirty even if
2756 * new_i_size is less that inode->i_size
2757 * bu greater than i_disksize.(hint delalloc)
2758 */
2759 ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04002760 }
Mingming Cao632eaea2008-07-11 19:27:31 -04002761 }
Tao Ma9c3569b2012-12-10 14:05:57 -05002762
2763 if (write_mode != CONVERT_INLINE_DATA &&
2764 ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
2765 ext4_has_inline_data(inode))
2766 ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied,
2767 page);
2768 else
2769 ret2 = generic_write_end(file, mapping, pos, len, copied,
Alex Tomas64769242008-07-11 19:27:31 -04002770 page, fsdata);
Tao Ma9c3569b2012-12-10 14:05:57 -05002771
Alex Tomas64769242008-07-11 19:27:31 -04002772 copied = ret2;
2773 if (ret2 < 0)
2774 ret = ret2;
2775 ret2 = ext4_journal_stop(handle);
2776 if (!ret)
2777 ret = ret2;
2778
2779 return ret ? ret : copied;
2780}
2781
Lukas Czernerd47992f2013-05-21 23:17:23 -04002782static void ext4_da_invalidatepage(struct page *page, unsigned int offset,
2783 unsigned int length)
Alex Tomas64769242008-07-11 19:27:31 -04002784{
Alex Tomas64769242008-07-11 19:27:31 -04002785 /*
2786 * Drop reserved blocks
2787 */
2788 BUG_ON(!PageLocked(page));
2789 if (!page_has_buffers(page))
2790 goto out;
2791
Lukas Czernerca99fdd2013-05-21 23:25:01 -04002792 ext4_da_page_release_reservation(page, offset, length);
Alex Tomas64769242008-07-11 19:27:31 -04002793
2794out:
Lukas Czernerd47992f2013-05-21 23:17:23 -04002795 ext4_invalidatepage(page, offset, length);
Alex Tomas64769242008-07-11 19:27:31 -04002796
2797 return;
2798}
2799
Theodore Ts'occd25062009-02-26 01:04:07 -05002800/*
2801 * Force all delayed allocation blocks to be allocated for a given inode.
2802 */
2803int ext4_alloc_da_blocks(struct inode *inode)
2804{
Theodore Ts'ofb40ba02009-09-16 19:30:40 -04002805 trace_ext4_alloc_da_blocks(inode);
2806
Theodore Ts'o71d4f7d2014-07-15 06:02:38 -04002807 if (!EXT4_I(inode)->i_reserved_data_blocks)
Theodore Ts'occd25062009-02-26 01:04:07 -05002808 return 0;
2809
2810 /*
2811 * We do something simple for now. The filemap_flush() will
2812 * also start triggering a write of the data blocks, which is
2813 * not strictly speaking necessary (and for users of
2814 * laptop_mode, not even desirable). However, to do otherwise
2815 * would require replicating code paths in:
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002816 *
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002817 * ext4_writepages() ->
Theodore Ts'occd25062009-02-26 01:04:07 -05002818 * write_cache_pages() ---> (via passed in callback function)
2819 * __mpage_da_writepage() -->
2820 * mpage_add_bh_to_extent()
2821 * mpage_da_map_blocks()
2822 *
2823 * The problem is that write_cache_pages(), located in
2824 * mm/page-writeback.c, marks pages clean in preparation for
2825 * doing I/O, which is not desirable if we're not planning on
2826 * doing I/O at all.
2827 *
2828 * We could call write_cache_pages(), and then redirty all of
Wu Fengguang380cf092010-11-11 19:23:29 +08002829 * the pages by calling redirty_page_for_writepage() but that
Theodore Ts'occd25062009-02-26 01:04:07 -05002830 * would be ugly in the extreme. So instead we would need to
2831 * replicate parts of the code in the above functions,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002832 * simplifying them because we wouldn't actually intend to
Theodore Ts'occd25062009-02-26 01:04:07 -05002833 * write out the pages, but rather only collect contiguous
2834 * logical block extents, call the multi-block allocator, and
2835 * then update the buffer heads with the block allocations.
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002836 *
Theodore Ts'occd25062009-02-26 01:04:07 -05002837 * For now, though, we'll cheat by calling filemap_flush(),
2838 * which will map the blocks, and start the I/O, but not
2839 * actually wait for the I/O to complete.
2840 */
2841 return filemap_flush(inode->i_mapping);
2842}
Alex Tomas64769242008-07-11 19:27:31 -04002843
2844/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002845 * bmap() is special. It gets used by applications such as lilo and by
2846 * the swapper to find the on-disk block of a specific piece of data.
2847 *
2848 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07002849 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002850 * filesystem and enables swap, then they may get a nasty shock when the
2851 * data getting swapped to that swapfile suddenly gets overwritten by
2852 * the original zero's written out previously to the journal and
2853 * awaiting writeback in the kernel's buffer cache.
2854 *
2855 * So, if we see any bmap calls here on a modified, data-journaled file,
2856 * take extra steps to flush any blocks which might be in the cache.
2857 */
Mingming Cao617ba132006-10-11 01:20:53 -07002858static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002859{
2860 struct inode *inode = mapping->host;
2861 journal_t *journal;
2862 int err;
2863
Tao Ma46c7f252012-12-10 14:04:52 -05002864 /*
2865 * We can get here for an inline file via the FIBMAP ioctl
2866 */
2867 if (ext4_has_inline_data(inode))
2868 return 0;
2869
Alex Tomas64769242008-07-11 19:27:31 -04002870 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
2871 test_opt(inode->i_sb, DELALLOC)) {
2872 /*
2873 * With delalloc we want to sync the file
2874 * so that we can make sure we allocate
2875 * blocks for file
2876 */
2877 filemap_write_and_wait(mapping);
2878 }
2879
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002880 if (EXT4_JOURNAL(inode) &&
2881 ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002882 /*
2883 * This is a REALLY heavyweight approach, but the use of
2884 * bmap on dirty files is expected to be extremely rare:
2885 * only if we run lilo or swapon on a freshly made file
2886 * do we expect this to happen.
2887 *
2888 * (bmap requires CAP_SYS_RAWIO so this does not
2889 * represent an unprivileged user DOS attack --- we'd be
2890 * in trouble if mortal users could trigger this path at
2891 * will.)
2892 *
Mingming Cao617ba132006-10-11 01:20:53 -07002893 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002894 * regular files. If somebody wants to bmap a directory
2895 * or symlink and gets confused because the buffer
2896 * hasn't yet been flushed to disk, they deserve
2897 * everything they get.
2898 */
2899
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002900 ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
Mingming Cao617ba132006-10-11 01:20:53 -07002901 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07002902 jbd2_journal_lock_updates(journal);
2903 err = jbd2_journal_flush(journal);
2904 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002905
2906 if (err)
2907 return 0;
2908 }
2909
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002910 return generic_block_bmap(mapping, block, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002911}
2912
Mingming Cao617ba132006-10-11 01:20:53 -07002913static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002914{
Tao Ma46c7f252012-12-10 14:04:52 -05002915 int ret = -EAGAIN;
2916 struct inode *inode = page->mapping->host;
2917
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04002918 trace_ext4_readpage(page);
Tao Ma46c7f252012-12-10 14:04:52 -05002919
2920 if (ext4_has_inline_data(inode))
2921 ret = ext4_readpage_inline(inode, page);
2922
2923 if (ret == -EAGAIN)
Theodore Ts'of64e02f2015-04-08 00:00:32 -04002924 return ext4_mpage_readpages(page->mapping, NULL, page, 1);
Tao Ma46c7f252012-12-10 14:04:52 -05002925
2926 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002927}
2928
2929static int
Mingming Cao617ba132006-10-11 01:20:53 -07002930ext4_readpages(struct file *file, struct address_space *mapping,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002931 struct list_head *pages, unsigned nr_pages)
2932{
Tao Ma46c7f252012-12-10 14:04:52 -05002933 struct inode *inode = mapping->host;
2934
2935 /* If the file has inline data, no need to do readpages. */
2936 if (ext4_has_inline_data(inode))
2937 return 0;
2938
Theodore Ts'of64e02f2015-04-08 00:00:32 -04002939 return ext4_mpage_readpages(mapping, pages, NULL, nr_pages);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002940}
2941
Lukas Czernerd47992f2013-05-21 23:17:23 -04002942static void ext4_invalidatepage(struct page *page, unsigned int offset,
2943 unsigned int length)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002944{
Lukas Czernerca99fdd2013-05-21 23:25:01 -04002945 trace_ext4_invalidatepage(page, offset, length);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04002946
Jan Kara4520fb32012-12-25 13:28:54 -05002947 /* No journalling happens on data buffers when this function is used */
2948 WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page)));
2949
Lukas Czernerca99fdd2013-05-21 23:25:01 -04002950 block_invalidatepage(page, offset, length);
Jan Kara4520fb32012-12-25 13:28:54 -05002951}
2952
Jan Kara53e87262012-12-25 13:29:52 -05002953static int __ext4_journalled_invalidatepage(struct page *page,
Lukas Czernerca99fdd2013-05-21 23:25:01 -04002954 unsigned int offset,
2955 unsigned int length)
Jan Kara4520fb32012-12-25 13:28:54 -05002956{
2957 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
2958
Lukas Czernerca99fdd2013-05-21 23:25:01 -04002959 trace_ext4_journalled_invalidatepage(page, offset, length);
Jan Kara4520fb32012-12-25 13:28:54 -05002960
Jiaying Zhang744692d2010-03-04 16:14:02 -05002961 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002962 * If it's a full truncate we just forget about the pending dirtying
2963 */
Lukas Czernerca99fdd2013-05-21 23:25:01 -04002964 if (offset == 0 && length == PAGE_CACHE_SIZE)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002965 ClearPageChecked(page);
2966
Lukas Czernerca99fdd2013-05-21 23:25:01 -04002967 return jbd2_journal_invalidatepage(journal, page, offset, length);
Jan Kara53e87262012-12-25 13:29:52 -05002968}
2969
2970/* Wrapper for aops... */
2971static void ext4_journalled_invalidatepage(struct page *page,
Lukas Czernerd47992f2013-05-21 23:17:23 -04002972 unsigned int offset,
2973 unsigned int length)
Jan Kara53e87262012-12-25 13:29:52 -05002974{
Lukas Czernerca99fdd2013-05-21 23:25:01 -04002975 WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002976}
2977
Mingming Cao617ba132006-10-11 01:20:53 -07002978static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002979{
Mingming Cao617ba132006-10-11 01:20:53 -07002980 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002981
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04002982 trace_ext4_releasepage(page);
2983
Jan Karae1c36592013-03-10 22:19:00 -04002984 /* Page has dirty journalled data -> cannot release */
2985 if (PageChecked(page))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002986 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05002987 if (journal)
2988 return jbd2_journal_try_to_free_buffers(journal, page, wait);
2989 else
2990 return try_to_free_buffers(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002991}
2992
2993/*
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002994 * ext4_get_block used when preparing for a DIO write or buffer write.
2995 * We allocate an uinitialized extent if blocks haven't been allocated.
2996 * The extent will be converted to initialized after the IO is complete.
2997 */
Tao Maf19d5872012-12-10 14:05:51 -05002998int ext4_get_block_write(struct inode *inode, sector_t iblock,
Mingming Cao4c0425f2009-09-28 15:48:41 -04002999 struct buffer_head *bh_result, int create)
3000{
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003001 ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n",
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003002 inode->i_ino, create);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003003 return _ext4_get_block(inode, iblock, bh_result,
3004 EXT4_GET_BLOCKS_IO_CREATE_EXT);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003005}
3006
Zheng Liu729f52c2012-07-09 16:29:29 -04003007static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock,
Anatol Pomozov8b0f1652012-11-08 15:07:16 -05003008 struct buffer_head *bh_result, int create)
Zheng Liu729f52c2012-07-09 16:29:29 -04003009{
Anatol Pomozov8b0f1652012-11-08 15:07:16 -05003010 ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n",
3011 inode->i_ino, create);
3012 return _ext4_get_block(inode, iblock, bh_result,
3013 EXT4_GET_BLOCKS_NO_LOCK);
Zheng Liu729f52c2012-07-09 16:29:29 -04003014}
3015
Mingming Cao4c0425f2009-09-28 15:48:41 -04003016static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
Christoph Hellwig7b7a8662013-09-04 15:04:39 +02003017 ssize_t size, void *private)
Mingming Cao4c0425f2009-09-28 15:48:41 -04003018{
3019 ext4_io_end_t *io_end = iocb->private;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003020
Jan Kara97a851e2013-06-04 11:58:58 -04003021 /* if not async direct IO just return */
Christoph Hellwig7b7a8662013-09-04 15:04:39 +02003022 if (!io_end)
Jan Kara97a851e2013-06-04 11:58:58 -04003023 return;
Mingming4b70df12009-11-03 14:44:54 -05003024
Zheng Liu88635ca2011-12-28 19:00:25 -05003025 ext_debug("ext4_end_io_dio(): io_end 0x%p "
Joe Perchesace36ad2012-03-19 23:11:43 -04003026 "for inode %lu, iocb 0x%p, offset %llu, size %zd\n",
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003027 iocb->private, io_end->inode->i_ino, iocb, offset,
3028 size);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003029
Theodore Ts'ob5a7e972011-12-12 10:53:02 -05003030 iocb->private = NULL;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003031 io_end->offset = offset;
3032 io_end->size = size;
Christoph Hellwig7b7a8662013-09-04 15:04:39 +02003033 ext4_put_io_end(io_end);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003034}
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003035
Mingming Cao4c0425f2009-09-28 15:48:41 -04003036/*
3037 * For ext4 extent files, ext4 will do direct-io write to holes,
3038 * preallocated extents, and those write extend the file, no need to
3039 * fall back to buffered IO.
3040 *
Lukas Czerner556615d2014-04-20 23:45:47 -04003041 * For holes, we fallocate those blocks, mark them as unwritten
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003042 * If those blocks were preallocated, we mark sure they are split, but
Lukas Czerner556615d2014-04-20 23:45:47 -04003043 * still keep the range to write as unwritten.
Mingming Cao4c0425f2009-09-28 15:48:41 -04003044 *
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003045 * The unwritten extents will be converted to written when DIO is completed.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003046 * For async direct IO, since the IO may still pending when return, we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003047 * set up an end_io call back function, which will do the conversion
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003048 * when async direct IO completed.
Mingming Cao4c0425f2009-09-28 15:48:41 -04003049 *
3050 * If the O_DIRECT write will extend the file then add this inode to the
3051 * orphan list. So recovery will truncate it back to the original size
3052 * if the machine crashes during the write.
3053 *
3054 */
Omar Sandoval6f673762015-03-16 04:33:52 -07003055static ssize_t ext4_ext_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
3056 loff_t offset)
Mingming Cao4c0425f2009-09-28 15:48:41 -04003057{
3058 struct file *file = iocb->ki_filp;
3059 struct inode *inode = file->f_mapping->host;
3060 ssize_t ret;
Al Viroa6cbcd42014-03-04 22:38:00 -05003061 size_t count = iov_iter_count(iter);
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003062 int overwrite = 0;
3063 get_block_t *get_block_func = NULL;
3064 int dio_flags = 0;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003065 loff_t final_size = offset + count;
Jan Kara97a851e2013-06-04 11:58:58 -04003066 ext4_io_end_t *io_end = NULL;
Zheng Liu729f52c2012-07-09 16:29:29 -04003067
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003068 /* Use the old path for reads and writes beyond i_size. */
Omar Sandoval6f673762015-03-16 04:33:52 -07003069 if (iov_iter_rw(iter) != WRITE || final_size > inode->i_size)
3070 return ext4_ind_direct_IO(iocb, iter, offset);
Zheng Liu4bd809d2012-07-22 20:19:31 -04003071
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003072 BUG_ON(iocb->private == NULL);
Zheng Liu4bd809d2012-07-22 20:19:31 -04003073
Jan Karae8340392013-06-04 14:27:38 -04003074 /*
3075 * Make all waiters for direct IO properly wait also for extent
3076 * conversion. This also disallows race between truncate() and
3077 * overwrite DIO as i_dio_count needs to be incremented under i_mutex.
3078 */
Omar Sandoval6f673762015-03-16 04:33:52 -07003079 if (iov_iter_rw(iter) == WRITE)
Jens Axboefe0f07d2015-04-15 17:05:48 -06003080 inode_dio_begin(inode);
Jan Karae8340392013-06-04 14:27:38 -04003081
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003082 /* If we do a overwrite dio, i_mutex locking can be released */
3083 overwrite = *((int *)iocb->private);
Zheng Liu4bd809d2012-07-22 20:19:31 -04003084
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003085 if (overwrite) {
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003086 down_read(&EXT4_I(inode)->i_data_sem);
3087 mutex_unlock(&inode->i_mutex);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003088 }
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003089
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003090 /*
3091 * We could direct write to holes and fallocate.
3092 *
3093 * Allocated blocks to fill the hole are marked as
Lukas Czerner556615d2014-04-20 23:45:47 -04003094 * unwritten to prevent parallel buffered read to expose
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003095 * the stale data before DIO complete the data IO.
3096 *
3097 * As to previously fallocated extents, ext4 get_block will
3098 * just simply mark the buffer mapped but still keep the
Lukas Czerner556615d2014-04-20 23:45:47 -04003099 * extents unwritten.
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003100 *
3101 * For non AIO case, we will convert those unwritten extents
3102 * to written after return back from blockdev_direct_IO.
3103 *
3104 * For async DIO, the conversion needs to be deferred when the
3105 * IO is completed. The ext4 end_io callback function will be
3106 * called to take care of the conversion work. Here for async
3107 * case, we allocate an io_end structure to hook to the iocb.
3108 */
3109 iocb->private = NULL;
3110 ext4_inode_aio_set(inode, NULL);
3111 if (!is_sync_kiocb(iocb)) {
Jan Kara97a851e2013-06-04 11:58:58 -04003112 io_end = ext4_init_io_end(inode, GFP_NOFS);
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003113 if (!io_end) {
3114 ret = -ENOMEM;
3115 goto retake_lock;
3116 }
Jan Kara97a851e2013-06-04 11:58:58 -04003117 /*
3118 * Grab reference for DIO. Will be dropped in ext4_end_io_dio()
3119 */
3120 iocb->private = ext4_get_io_end(io_end);
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003121 /*
3122 * we save the io structure for current async direct
3123 * IO, so that later ext4_map_blocks() could flag the
3124 * io structure whether there is a unwritten extents
3125 * needs to be converted when IO is completed.
3126 */
3127 ext4_inode_aio_set(inode, io_end);
3128 }
3129
3130 if (overwrite) {
3131 get_block_func = ext4_get_block_write_nolock;
3132 } else {
3133 get_block_func = ext4_get_block_write;
3134 dio_flags = DIO_LOCKING;
3135 }
Michael Halcrow2058f832015-04-12 00:55:10 -04003136#ifdef CONFIG_EXT4_FS_ENCRYPTION
3137 BUG_ON(ext4_encrypted_inode(inode) && S_ISREG(inode->i_mode));
3138#endif
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003139 if (IS_DAX(inode))
Omar Sandovala95cd632015-03-16 04:33:51 -07003140 ret = dax_do_io(iocb, inode, iter, offset, get_block_func,
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003141 ext4_end_io_dio, dio_flags);
3142 else
Omar Sandoval17f8c842015-03-16 04:33:50 -07003143 ret = __blockdev_direct_IO(iocb, inode,
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003144 inode->i_sb->s_bdev, iter, offset,
3145 get_block_func,
3146 ext4_end_io_dio, NULL, dio_flags);
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003147
Theodore Ts'oa5499842013-05-11 19:07:42 -04003148 /*
Jan Kara97a851e2013-06-04 11:58:58 -04003149 * Put our reference to io_end. This can free the io_end structure e.g.
3150 * in sync IO case or in case of error. It can even perform extent
3151 * conversion if all bios we submitted finished before we got here.
3152 * Note that in that case iocb->private can be already set to NULL
3153 * here.
Theodore Ts'oa5499842013-05-11 19:07:42 -04003154 */
Jan Kara97a851e2013-06-04 11:58:58 -04003155 if (io_end) {
3156 ext4_inode_aio_set(inode, NULL);
3157 ext4_put_io_end(io_end);
3158 /*
3159 * When no IO was submitted ext4_end_io_dio() was not
3160 * called so we have to put iocb's reference.
3161 */
3162 if (ret <= 0 && ret != -EIOCBQUEUED && iocb->private) {
3163 WARN_ON(iocb->private != io_end);
3164 WARN_ON(io_end->flag & EXT4_IO_END_UNWRITTEN);
Jan Kara97a851e2013-06-04 11:58:58 -04003165 ext4_put_io_end(io_end);
3166 iocb->private = NULL;
3167 }
3168 }
3169 if (ret > 0 && !overwrite && ext4_test_inode_state(inode,
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003170 EXT4_STATE_DIO_UNWRITTEN)) {
3171 int err;
3172 /*
3173 * for non AIO case, since the IO is already
3174 * completed, we could do the conversion right here
3175 */
Jan Kara6b523df2013-06-04 13:21:11 -04003176 err = ext4_convert_unwritten_extents(NULL, inode,
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003177 offset, ret);
3178 if (err < 0)
3179 ret = err;
3180 ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
3181 }
3182
3183retake_lock:
Omar Sandoval6f673762015-03-16 04:33:52 -07003184 if (iov_iter_rw(iter) == WRITE)
Jens Axboefe0f07d2015-04-15 17:05:48 -06003185 inode_dio_end(inode);
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003186 /* take i_mutex locking again if we do a ovewrite dio */
3187 if (overwrite) {
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003188 up_read(&EXT4_I(inode)->i_data_sem);
3189 mutex_lock(&inode->i_mutex);
3190 }
3191
3192 return ret;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003193}
3194
Omar Sandoval22c61862015-03-16 04:33:53 -07003195static ssize_t ext4_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
3196 loff_t offset)
Mingming Cao4c0425f2009-09-28 15:48:41 -04003197{
3198 struct file *file = iocb->ki_filp;
3199 struct inode *inode = file->f_mapping->host;
Al Viroa6cbcd42014-03-04 22:38:00 -05003200 size_t count = iov_iter_count(iter);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003201 ssize_t ret;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003202
Michael Halcrow2058f832015-04-12 00:55:10 -04003203#ifdef CONFIG_EXT4_FS_ENCRYPTION
3204 if (ext4_encrypted_inode(inode) && S_ISREG(inode->i_mode))
3205 return 0;
3206#endif
3207
Theodore Ts'o84ebd792011-08-31 11:56:51 -04003208 /*
3209 * If we are doing data journalling we don't support O_DIRECT
3210 */
3211 if (ext4_should_journal_data(inode))
3212 return 0;
3213
Tao Ma46c7f252012-12-10 14:04:52 -05003214 /* Let buffer I/O handle the inline data case. */
3215 if (ext4_has_inline_data(inode))
3216 return 0;
3217
Omar Sandoval6f673762015-03-16 04:33:52 -07003218 trace_ext4_direct_IO_enter(inode, offset, count, iov_iter_rw(iter));
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003219 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Omar Sandoval6f673762015-03-16 04:33:52 -07003220 ret = ext4_ext_direct_IO(iocb, iter, offset);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003221 else
Omar Sandoval6f673762015-03-16 04:33:52 -07003222 ret = ext4_ind_direct_IO(iocb, iter, offset);
3223 trace_ext4_direct_IO_exit(inode, offset, count, iov_iter_rw(iter), ret);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003224 return ret;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003225}
3226
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003227/*
Mingming Cao617ba132006-10-11 01:20:53 -07003228 * Pages can be marked dirty completely asynchronously from ext4's journalling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003229 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3230 * much here because ->set_page_dirty is called under VFS locks. The page is
3231 * not necessarily locked.
3232 *
3233 * We cannot just dirty the page and leave attached buffers clean, because the
3234 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3235 * or jbddirty because all the journalling code will explode.
3236 *
3237 * So what we do is to mark the page "pending dirty" and next time writepage
3238 * is called, propagate that into the buffers appropriately.
3239 */
Mingming Cao617ba132006-10-11 01:20:53 -07003240static int ext4_journalled_set_page_dirty(struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003241{
3242 SetPageChecked(page);
3243 return __set_page_dirty_nobuffers(page);
3244}
3245
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003246static const struct address_space_operations ext4_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003247 .readpage = ext4_readpage,
3248 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003249 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003250 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003251 .write_begin = ext4_write_begin,
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003252 .write_end = ext4_write_end,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003253 .bmap = ext4_bmap,
3254 .invalidatepage = ext4_invalidatepage,
3255 .releasepage = ext4_releasepage,
3256 .direct_IO = ext4_direct_IO,
3257 .migratepage = buffer_migrate_page,
3258 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003259 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003260};
3261
Mingming Cao617ba132006-10-11 01:20:53 -07003262static const struct address_space_operations ext4_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003263 .readpage = ext4_readpage,
3264 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003265 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003266 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003267 .write_begin = ext4_write_begin,
3268 .write_end = ext4_journalled_write_end,
3269 .set_page_dirty = ext4_journalled_set_page_dirty,
3270 .bmap = ext4_bmap,
Jan Kara4520fb32012-12-25 13:28:54 -05003271 .invalidatepage = ext4_journalled_invalidatepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003272 .releasepage = ext4_releasepage,
Theodore Ts'o84ebd792011-08-31 11:56:51 -04003273 .direct_IO = ext4_direct_IO,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003274 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003275 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003276};
3277
Alex Tomas64769242008-07-11 19:27:31 -04003278static const struct address_space_operations ext4_da_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003279 .readpage = ext4_readpage,
3280 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003281 .writepage = ext4_writepage,
Theodore Ts'o20970ba2013-06-06 14:00:46 -04003282 .writepages = ext4_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003283 .write_begin = ext4_da_write_begin,
3284 .write_end = ext4_da_write_end,
3285 .bmap = ext4_bmap,
3286 .invalidatepage = ext4_da_invalidatepage,
3287 .releasepage = ext4_releasepage,
3288 .direct_IO = ext4_direct_IO,
3289 .migratepage = buffer_migrate_page,
3290 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003291 .error_remove_page = generic_error_remove_page,
Alex Tomas64769242008-07-11 19:27:31 -04003292};
3293
Mingming Cao617ba132006-10-11 01:20:53 -07003294void ext4_set_aops(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003295{
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003296 switch (ext4_inode_journal_mode(inode)) {
3297 case EXT4_INODE_ORDERED_DATA_MODE:
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003298 ext4_set_inode_state(inode, EXT4_STATE_ORDERED_MODE);
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003299 break;
3300 case EXT4_INODE_WRITEBACK_DATA_MODE:
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003301 ext4_clear_inode_state(inode, EXT4_STATE_ORDERED_MODE);
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003302 break;
3303 case EXT4_INODE_JOURNAL_DATA_MODE:
Mingming Cao617ba132006-10-11 01:20:53 -07003304 inode->i_mapping->a_ops = &ext4_journalled_aops;
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003305 return;
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003306 default:
3307 BUG();
3308 }
Theodore Ts'o74d553a2013-04-03 12:39:17 -04003309 if (test_opt(inode->i_sb, DELALLOC))
3310 inode->i_mapping->a_ops = &ext4_da_aops;
3311 else
3312 inode->i_mapping->a_ops = &ext4_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003313}
3314
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003315static int __ext4_block_zero_page_range(handle_t *handle,
Lukas Czernerd863dc32013-05-27 23:32:35 -04003316 struct address_space *mapping, loff_t from, loff_t length)
3317{
3318 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
3319 unsigned offset = from & (PAGE_CACHE_SIZE-1);
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003320 unsigned blocksize, pos;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003321 ext4_lblk_t iblock;
3322 struct inode *inode = mapping->host;
3323 struct buffer_head *bh;
3324 struct page *page;
3325 int err = 0;
3326
3327 page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
3328 mapping_gfp_mask(mapping) & ~__GFP_FS);
3329 if (!page)
3330 return -ENOMEM;
3331
3332 blocksize = inode->i_sb->s_blocksize;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003333
3334 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
3335
3336 if (!page_has_buffers(page))
3337 create_empty_buffers(page, blocksize, 0);
3338
3339 /* Find the buffer that contains "offset" */
3340 bh = page_buffers(page);
3341 pos = blocksize;
3342 while (offset >= pos) {
3343 bh = bh->b_this_page;
3344 iblock++;
3345 pos += blocksize;
3346 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003347 if (buffer_freed(bh)) {
3348 BUFFER_TRACE(bh, "freed: skip");
3349 goto unlock;
3350 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003351 if (!buffer_mapped(bh)) {
3352 BUFFER_TRACE(bh, "unmapped");
3353 ext4_get_block(inode, iblock, bh, 0);
3354 /* unmapped? It's a hole - nothing to do */
3355 if (!buffer_mapped(bh)) {
3356 BUFFER_TRACE(bh, "still unmapped");
3357 goto unlock;
3358 }
3359 }
3360
3361 /* Ok, it's mapped. Make sure it's up-to-date */
3362 if (PageUptodate(page))
3363 set_buffer_uptodate(bh);
3364
3365 if (!buffer_uptodate(bh)) {
3366 err = -EIO;
3367 ll_rw_block(READ, 1, &bh);
3368 wait_on_buffer(bh);
3369 /* Uhhuh. Read error. Complain and punt. */
3370 if (!buffer_uptodate(bh))
3371 goto unlock;
Michael Halcrowc9c74292015-04-12 00:56:10 -04003372 if (S_ISREG(inode->i_mode) &&
3373 ext4_encrypted_inode(inode)) {
3374 /* We expect the key to be set. */
3375 BUG_ON(!ext4_has_encryption_key(inode));
3376 BUG_ON(blocksize != PAGE_CACHE_SIZE);
3377 WARN_ON_ONCE(ext4_decrypt_one(inode, page));
3378 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003379 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003380 if (ext4_should_journal_data(inode)) {
3381 BUFFER_TRACE(bh, "get write access");
3382 err = ext4_journal_get_write_access(handle, bh);
3383 if (err)
3384 goto unlock;
3385 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003386 zero_user(page, offset, length);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003387 BUFFER_TRACE(bh, "zeroed end of block");
3388
Lukas Czernerd863dc32013-05-27 23:32:35 -04003389 if (ext4_should_journal_data(inode)) {
3390 err = ext4_handle_dirty_metadata(handle, inode, bh);
Lukas Czerner0713ed02013-05-27 23:32:35 -04003391 } else {
jon ernst353eefd2013-07-01 08:12:39 -04003392 err = 0;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003393 mark_buffer_dirty(bh);
Lukas Czerner0713ed02013-05-27 23:32:35 -04003394 if (ext4_test_inode_state(inode, EXT4_STATE_ORDERED_MODE))
3395 err = ext4_jbd2_file_inode(handle, inode);
3396 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003397
3398unlock:
3399 unlock_page(page);
3400 page_cache_release(page);
3401 return err;
3402}
3403
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003404/*
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003405 * ext4_block_zero_page_range() zeros out a mapping of length 'length'
3406 * starting from file offset 'from'. The range to be zero'd must
3407 * be contained with in one block. If the specified range exceeds
3408 * the end of the block it will be shortened to end of the block
3409 * that cooresponds to 'from'
3410 */
3411static int ext4_block_zero_page_range(handle_t *handle,
3412 struct address_space *mapping, loff_t from, loff_t length)
3413{
3414 struct inode *inode = mapping->host;
3415 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3416 unsigned blocksize = inode->i_sb->s_blocksize;
3417 unsigned max = blocksize - (offset & (blocksize - 1));
3418
3419 /*
3420 * correct length if it does not fall between
3421 * 'from' and the end of the block
3422 */
3423 if (length > max || length < 0)
3424 length = max;
3425
3426 if (IS_DAX(inode))
3427 return dax_zero_page_range(inode, from, length, ext4_get_block);
3428 return __ext4_block_zero_page_range(handle, mapping, from, length);
3429}
3430
3431/*
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003432 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
3433 * up to the end of the block which corresponds to `from'.
3434 * This required during truncate. We need to physically zero the tail end
3435 * of that block so it doesn't yield old data if the file is later grown.
3436 */
Stephen Hemmingerc1978552014-05-12 10:50:23 -04003437static int ext4_block_truncate_page(handle_t *handle,
Matthew Wilcox94350ab2014-03-24 15:09:16 -04003438 struct address_space *mapping, loff_t from)
3439{
3440 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3441 unsigned length;
3442 unsigned blocksize;
3443 struct inode *inode = mapping->host;
3444
3445 blocksize = inode->i_sb->s_blocksize;
3446 length = blocksize - (offset & (blocksize - 1));
3447
3448 return ext4_block_zero_page_range(handle, mapping, from, length);
3449}
3450
Lukas Czernera87dd182013-05-27 23:32:35 -04003451int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
3452 loff_t lstart, loff_t length)
3453{
3454 struct super_block *sb = inode->i_sb;
3455 struct address_space *mapping = inode->i_mapping;
Lukas Czernere1be3a92013-07-01 08:12:39 -04003456 unsigned partial_start, partial_end;
Lukas Czernera87dd182013-05-27 23:32:35 -04003457 ext4_fsblk_t start, end;
3458 loff_t byte_end = (lstart + length - 1);
3459 int err = 0;
3460
Lukas Czernere1be3a92013-07-01 08:12:39 -04003461 partial_start = lstart & (sb->s_blocksize - 1);
3462 partial_end = byte_end & (sb->s_blocksize - 1);
3463
Lukas Czernera87dd182013-05-27 23:32:35 -04003464 start = lstart >> sb->s_blocksize_bits;
3465 end = byte_end >> sb->s_blocksize_bits;
3466
3467 /* Handle partial zero within the single block */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003468 if (start == end &&
3469 (partial_start || (partial_end != sb->s_blocksize - 1))) {
Lukas Czernera87dd182013-05-27 23:32:35 -04003470 err = ext4_block_zero_page_range(handle, mapping,
3471 lstart, length);
3472 return err;
3473 }
3474 /* Handle partial zero out on the start of the range */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003475 if (partial_start) {
Lukas Czernera87dd182013-05-27 23:32:35 -04003476 err = ext4_block_zero_page_range(handle, mapping,
3477 lstart, sb->s_blocksize);
3478 if (err)
3479 return err;
3480 }
3481 /* Handle partial zero out on the end of the range */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003482 if (partial_end != sb->s_blocksize - 1)
Lukas Czernera87dd182013-05-27 23:32:35 -04003483 err = ext4_block_zero_page_range(handle, mapping,
Lukas Czernere1be3a92013-07-01 08:12:39 -04003484 byte_end - partial_end,
3485 partial_end + 1);
Lukas Czernera87dd182013-05-27 23:32:35 -04003486 return err;
3487}
3488
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003489int ext4_can_truncate(struct inode *inode)
3490{
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003491 if (S_ISREG(inode->i_mode))
3492 return 1;
3493 if (S_ISDIR(inode->i_mode))
3494 return 1;
3495 if (S_ISLNK(inode->i_mode))
3496 return !ext4_inode_is_fast_symlink(inode);
3497 return 0;
3498}
3499
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003500/*
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003501 * ext4_punch_hole: punches a hole in a file by releaseing the blocks
3502 * associated with the given offset and length
3503 *
3504 * @inode: File inode
3505 * @offset: The offset where the hole will begin
3506 * @len: The length of the hole
3507 *
Anatol Pomozov4907cb72012-09-01 10:31:09 -07003508 * Returns: 0 on success or negative on failure
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003509 */
3510
Ashish Sangwanaeb28172013-07-01 08:12:38 -04003511int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length)
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003512{
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003513 struct super_block *sb = inode->i_sb;
3514 ext4_lblk_t first_block, stop_block;
3515 struct address_space *mapping = inode->i_mapping;
Lukas Czernera87dd182013-05-27 23:32:35 -04003516 loff_t first_block_offset, last_block_offset;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003517 handle_t *handle;
3518 unsigned int credits;
3519 int ret = 0;
3520
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003521 if (!S_ISREG(inode->i_mode))
Allison Henderson73355192012-03-21 22:23:31 -04003522 return -EOPNOTSUPP;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003523
Lukas Czernerb8a86842014-03-18 18:05:35 -04003524 trace_ext4_punch_hole(inode, offset, length, 0);
Zheng Liuaaddea82013-01-16 20:21:26 -05003525
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003526 /*
3527 * Write out all dirty pages to avoid race conditions
3528 * Then release them.
3529 */
3530 if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
3531 ret = filemap_write_and_wait_range(mapping, offset,
3532 offset + length - 1);
3533 if (ret)
3534 return ret;
3535 }
3536
3537 mutex_lock(&inode->i_mutex);
Lukas Czerner9ef06ce2014-04-12 09:47:00 -04003538
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003539 /* No need to punch hole beyond i_size */
3540 if (offset >= inode->i_size)
3541 goto out_mutex;
3542
3543 /*
3544 * If the hole extends beyond i_size, set the hole
3545 * to end after the page that contains i_size
3546 */
3547 if (offset + length > inode->i_size) {
3548 length = inode->i_size +
3549 PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) -
3550 offset;
3551 }
3552
Jan Karaa3612932013-08-16 21:19:41 -04003553 if (offset & (sb->s_blocksize - 1) ||
3554 (offset + length) & (sb->s_blocksize - 1)) {
3555 /*
3556 * Attach jinode to inode for jbd2 if we do any zeroing of
3557 * partial block
3558 */
3559 ret = ext4_inode_attach_jinode(inode);
3560 if (ret < 0)
3561 goto out_mutex;
3562
3563 }
3564
Lukas Czernera87dd182013-05-27 23:32:35 -04003565 first_block_offset = round_up(offset, sb->s_blocksize);
3566 last_block_offset = round_down((offset + length), sb->s_blocksize) - 1;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003567
Lukas Czernera87dd182013-05-27 23:32:35 -04003568 /* Now release the pages and zero block aligned part of pages*/
3569 if (last_block_offset > first_block_offset)
3570 truncate_pagecache_range(inode, first_block_offset,
3571 last_block_offset);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003572
3573 /* Wait all existing dio workers, newcomers will block on i_mutex */
3574 ext4_inode_block_unlocked_dio(inode);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003575 inode_dio_wait(inode);
3576
3577 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3578 credits = ext4_writepage_trans_blocks(inode);
3579 else
3580 credits = ext4_blocks_for_truncate(inode);
3581 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
3582 if (IS_ERR(handle)) {
3583 ret = PTR_ERR(handle);
3584 ext4_std_error(sb, ret);
3585 goto out_dio;
3586 }
3587
Lukas Czernera87dd182013-05-27 23:32:35 -04003588 ret = ext4_zero_partial_blocks(handle, inode, offset,
3589 length);
3590 if (ret)
3591 goto out_stop;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003592
3593 first_block = (offset + sb->s_blocksize - 1) >>
3594 EXT4_BLOCK_SIZE_BITS(sb);
3595 stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
3596
3597 /* If there are no blocks to remove, return now */
3598 if (first_block >= stop_block)
3599 goto out_stop;
3600
3601 down_write(&EXT4_I(inode)->i_data_sem);
3602 ext4_discard_preallocations(inode);
3603
3604 ret = ext4_es_remove_extent(inode, first_block,
3605 stop_block - first_block);
3606 if (ret) {
3607 up_write(&EXT4_I(inode)->i_data_sem);
3608 goto out_stop;
3609 }
3610
3611 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3612 ret = ext4_ext_remove_space(inode, first_block,
3613 stop_block - 1);
3614 else
Lukas Czerner4f579ae2014-07-15 06:03:38 -04003615 ret = ext4_ind_remove_space(handle, inode, first_block,
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003616 stop_block);
3617
Theodore Ts'o819c4922013-04-03 12:47:17 -04003618 up_write(&EXT4_I(inode)->i_data_sem);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003619 if (IS_SYNC(inode))
3620 ext4_handle_sync(handle);
Maxim Patlasove251f9b2014-02-20 16:58:05 -05003621
3622 /* Now release the pages again to reduce race window */
3623 if (last_block_offset > first_block_offset)
3624 truncate_pagecache_range(inode, first_block_offset,
3625 last_block_offset);
3626
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003627 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3628 ext4_mark_inode_dirty(handle, inode);
3629out_stop:
3630 ext4_journal_stop(handle);
3631out_dio:
3632 ext4_inode_resume_unlocked_dio(inode);
3633out_mutex:
3634 mutex_unlock(&inode->i_mutex);
3635 return ret;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003636}
3637
Jan Karaa3612932013-08-16 21:19:41 -04003638int ext4_inode_attach_jinode(struct inode *inode)
3639{
3640 struct ext4_inode_info *ei = EXT4_I(inode);
3641 struct jbd2_inode *jinode;
3642
3643 if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal)
3644 return 0;
3645
3646 jinode = jbd2_alloc_inode(GFP_KERNEL);
3647 spin_lock(&inode->i_lock);
3648 if (!ei->jinode) {
3649 if (!jinode) {
3650 spin_unlock(&inode->i_lock);
3651 return -ENOMEM;
3652 }
3653 ei->jinode = jinode;
3654 jbd2_journal_init_jbd_inode(ei->jinode, inode);
3655 jinode = NULL;
3656 }
3657 spin_unlock(&inode->i_lock);
3658 if (unlikely(jinode != NULL))
3659 jbd2_free_inode(jinode);
3660 return 0;
3661}
3662
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003663/*
Mingming Cao617ba132006-10-11 01:20:53 -07003664 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003665 *
Mingming Cao617ba132006-10-11 01:20:53 -07003666 * We block out ext4_get_block() block instantiations across the entire
3667 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003668 * simultaneously on behalf of the same inode.
3669 *
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003670 * As we work through the truncate and commit bits of it to the journal there
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003671 * is one core, guiding principle: the file's tree must always be consistent on
3672 * disk. We must be able to restart the truncate after a crash.
3673 *
3674 * The file's tree may be transiently inconsistent in memory (although it
3675 * probably isn't), but whenever we close off and commit a journal transaction,
3676 * the contents of (the filesystem + the journal) must be consistent and
3677 * restartable. It's pretty simple, really: bottom up, right to left (although
3678 * left-to-right works OK too).
3679 *
3680 * Note that at recovery time, journal replay occurs *before* the restart of
3681 * truncate against the orphan inode list.
3682 *
3683 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07003684 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003685 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07003686 * ext4_truncate() to have another go. So there will be instantiated blocks
3687 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003688 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07003689 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003690 */
Mingming Cao617ba132006-10-11 01:20:53 -07003691void ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003692{
Theodore Ts'o819c4922013-04-03 12:47:17 -04003693 struct ext4_inode_info *ei = EXT4_I(inode);
3694 unsigned int credits;
3695 handle_t *handle;
3696 struct address_space *mapping = inode->i_mapping;
Theodore Ts'o819c4922013-04-03 12:47:17 -04003697
Theodore Ts'o19b5ef62013-04-03 21:58:52 -04003698 /*
3699 * There is a possibility that we're either freeing the inode
Matthew Wilcoxe04027e2014-03-24 15:15:07 -04003700 * or it's a completely new inode. In those cases we might not
Theodore Ts'o19b5ef62013-04-03 21:58:52 -04003701 * have i_mutex locked because it's not necessary.
3702 */
3703 if (!(inode->i_state & (I_NEW|I_FREEING)))
3704 WARN_ON(!mutex_is_locked(&inode->i_mutex));
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003705 trace_ext4_truncate_enter(inode);
3706
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003707 if (!ext4_can_truncate(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003708 return;
3709
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003710 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003711
Theodore Ts'o5534fb52009-09-17 09:34:16 -04003712 if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003713 ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
Theodore Ts'o7d8f9f72009-02-24 08:21:14 -05003714
Tao Maaef1c852012-12-10 14:06:02 -05003715 if (ext4_has_inline_data(inode)) {
3716 int has_inline = 1;
3717
3718 ext4_inline_data_truncate(inode, &has_inline);
3719 if (has_inline)
3720 return;
3721 }
3722
Jan Karaa3612932013-08-16 21:19:41 -04003723 /* If we zero-out tail of the page, we have to create jinode for jbd2 */
3724 if (inode->i_size & (inode->i_sb->s_blocksize - 1)) {
3725 if (ext4_inode_attach_jinode(inode) < 0)
3726 return;
3727 }
3728
Amir Goldsteinff9893dc2011-06-27 16:36:31 -04003729 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Theodore Ts'o819c4922013-04-03 12:47:17 -04003730 credits = ext4_writepage_trans_blocks(inode);
Amir Goldsteinff9893dc2011-06-27 16:36:31 -04003731 else
Theodore Ts'o819c4922013-04-03 12:47:17 -04003732 credits = ext4_blocks_for_truncate(inode);
3733
3734 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
3735 if (IS_ERR(handle)) {
3736 ext4_std_error(inode->i_sb, PTR_ERR(handle));
3737 return;
3738 }
3739
Lukas Czernereb3544c2013-05-27 23:32:35 -04003740 if (inode->i_size & (inode->i_sb->s_blocksize - 1))
3741 ext4_block_truncate_page(handle, mapping, inode->i_size);
Theodore Ts'o819c4922013-04-03 12:47:17 -04003742
3743 /*
3744 * We add the inode to the orphan list, so that if this
3745 * truncate spans multiple transactions, and we crash, we will
3746 * resume the truncate when the filesystem recovers. It also
3747 * marks the inode dirty, to catch the new size.
3748 *
3749 * Implication: the file must always be in a sane, consistent
3750 * truncatable state while each transaction commits.
3751 */
3752 if (ext4_orphan_add(handle, inode))
3753 goto out_stop;
3754
3755 down_write(&EXT4_I(inode)->i_data_sem);
3756
3757 ext4_discard_preallocations(inode);
3758
3759 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3760 ext4_ext_truncate(handle, inode);
3761 else
3762 ext4_ind_truncate(handle, inode);
3763
3764 up_write(&ei->i_data_sem);
3765
3766 if (IS_SYNC(inode))
3767 ext4_handle_sync(handle);
3768
3769out_stop:
3770 /*
3771 * If this was a simple ftruncate() and the file will remain alive,
3772 * then we need to clear up the orphan record which we created above.
3773 * However, if this was a real unlink then we were called by
Wang Shilong58d86a52014-11-25 16:17:29 -05003774 * ext4_evict_inode(), and we allow that function to clean up the
Theodore Ts'o819c4922013-04-03 12:47:17 -04003775 * orphan info for us.
3776 */
3777 if (inode->i_nlink)
3778 ext4_orphan_del(handle, inode);
3779
3780 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3781 ext4_mark_inode_dirty(handle, inode);
3782 ext4_journal_stop(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07003783
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003784 trace_ext4_truncate_exit(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003785}
3786
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003787/*
Mingming Cao617ba132006-10-11 01:20:53 -07003788 * ext4_get_inode_loc returns with an extra refcount against the inode's
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003789 * underlying buffer_head on success. If 'in_mem' is true, we have all
3790 * data in memory that is needed to recreate the on-disk version of this
3791 * inode.
3792 */
Mingming Cao617ba132006-10-11 01:20:53 -07003793static int __ext4_get_inode_loc(struct inode *inode,
3794 struct ext4_iloc *iloc, int in_mem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003795{
Theodore Ts'o240799c2008-10-09 23:53:47 -04003796 struct ext4_group_desc *gdp;
3797 struct buffer_head *bh;
3798 struct super_block *sb = inode->i_sb;
3799 ext4_fsblk_t block;
3800 int inodes_per_block, inode_offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003801
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003802 iloc->bh = NULL;
Theodore Ts'o240799c2008-10-09 23:53:47 -04003803 if (!ext4_valid_inum(sb, inode->i_ino))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003804 return -EIO;
3805
Theodore Ts'o240799c2008-10-09 23:53:47 -04003806 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
3807 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
3808 if (!gdp)
3809 return -EIO;
3810
3811 /*
3812 * Figure out the offset within the block group inode table
3813 */
Tao Ma00d09882011-05-09 10:26:41 -04003814 inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
Theodore Ts'o240799c2008-10-09 23:53:47 -04003815 inode_offset = ((inode->i_ino - 1) %
3816 EXT4_INODES_PER_GROUP(sb));
3817 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
3818 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
3819
3820 bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05003821 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -05003822 return -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003823 if (!buffer_uptodate(bh)) {
3824 lock_buffer(bh);
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04003825
3826 /*
3827 * If the buffer has the write error flag, we have failed
3828 * to write out another inode in the same block. In this
3829 * case, we don't have to read the block because we may
3830 * read the old inode data successfully.
3831 */
3832 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
3833 set_buffer_uptodate(bh);
3834
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003835 if (buffer_uptodate(bh)) {
3836 /* someone brought it uptodate while we waited */
3837 unlock_buffer(bh);
3838 goto has_buffer;
3839 }
3840
3841 /*
3842 * If we have all information of the inode in memory and this
3843 * is the only valid inode in the block, we need not read the
3844 * block.
3845 */
3846 if (in_mem) {
3847 struct buffer_head *bitmap_bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04003848 int i, start;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003849
Theodore Ts'o240799c2008-10-09 23:53:47 -04003850 start = inode_offset & ~(inodes_per_block - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003851
3852 /* Is the inode bitmap in cache? */
Theodore Ts'o240799c2008-10-09 23:53:47 -04003853 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
Wang Shilongaebf0242013-01-12 16:28:47 -05003854 if (unlikely(!bitmap_bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003855 goto make_io;
3856
3857 /*
3858 * If the inode bitmap isn't in cache then the
3859 * optimisation may end up performing two reads instead
3860 * of one, so skip it.
3861 */
3862 if (!buffer_uptodate(bitmap_bh)) {
3863 brelse(bitmap_bh);
3864 goto make_io;
3865 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04003866 for (i = start; i < start + inodes_per_block; i++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003867 if (i == inode_offset)
3868 continue;
Mingming Cao617ba132006-10-11 01:20:53 -07003869 if (ext4_test_bit(i, bitmap_bh->b_data))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003870 break;
3871 }
3872 brelse(bitmap_bh);
Theodore Ts'o240799c2008-10-09 23:53:47 -04003873 if (i == start + inodes_per_block) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003874 /* all other inodes are free, so skip I/O */
3875 memset(bh->b_data, 0, bh->b_size);
3876 set_buffer_uptodate(bh);
3877 unlock_buffer(bh);
3878 goto has_buffer;
3879 }
3880 }
3881
3882make_io:
3883 /*
Theodore Ts'o240799c2008-10-09 23:53:47 -04003884 * If we need to do any I/O, try to pre-readahead extra
3885 * blocks from the inode table.
3886 */
3887 if (EXT4_SB(sb)->s_inode_readahead_blks) {
3888 ext4_fsblk_t b, end, table;
3889 unsigned num;
Theodore Ts'o0d606e22013-04-23 08:59:35 -04003890 __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks;
Theodore Ts'o240799c2008-10-09 23:53:47 -04003891
3892 table = ext4_inode_table(sb, gdp);
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003893 /* s_inode_readahead_blks is always a power of 2 */
Theodore Ts'o0d606e22013-04-23 08:59:35 -04003894 b = block & ~((ext4_fsblk_t) ra_blks - 1);
Theodore Ts'o240799c2008-10-09 23:53:47 -04003895 if (table > b)
3896 b = table;
Theodore Ts'o0d606e22013-04-23 08:59:35 -04003897 end = b + ra_blks;
Theodore Ts'o240799c2008-10-09 23:53:47 -04003898 num = EXT4_INODES_PER_GROUP(sb);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04003899 if (ext4_has_group_desc_csum(sb))
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05003900 num -= ext4_itable_unused_count(sb, gdp);
Theodore Ts'o240799c2008-10-09 23:53:47 -04003901 table += num / inodes_per_block;
3902 if (end > table)
3903 end = table;
3904 while (b <= end)
3905 sb_breadahead(sb, b++);
3906 }
3907
3908 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003909 * There are other valid inodes in the buffer, this inode
3910 * has in-inode xattrs, or we don't have this inode in memory.
3911 * Read the block from disk.
3912 */
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003913 trace_ext4_load_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003914 get_bh(bh);
3915 bh->b_end_io = end_buffer_read_sync;
Christoph Hellwig65299a32011-08-23 14:50:29 +02003916 submit_bh(READ | REQ_META | REQ_PRIO, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003917 wait_on_buffer(bh);
3918 if (!buffer_uptodate(bh)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -04003919 EXT4_ERROR_INODE_BLOCK(inode, block,
3920 "unable to read itable block");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003921 brelse(bh);
3922 return -EIO;
3923 }
3924 }
3925has_buffer:
3926 iloc->bh = bh;
3927 return 0;
3928}
3929
Mingming Cao617ba132006-10-11 01:20:53 -07003930int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003931{
3932 /* We have all inode data except xattrs in memory here. */
Mingming Cao617ba132006-10-11 01:20:53 -07003933 return __ext4_get_inode_loc(inode, iloc,
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003934 !ext4_test_inode_state(inode, EXT4_STATE_XATTR));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003935}
3936
Mingming Cao617ba132006-10-11 01:20:53 -07003937void ext4_set_inode_flags(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003938{
Mingming Cao617ba132006-10-11 01:20:53 -07003939 unsigned int flags = EXT4_I(inode)->i_flags;
Theodore Ts'o00a1a052014-03-30 10:20:01 -04003940 unsigned int new_fl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003941
Mingming Cao617ba132006-10-11 01:20:53 -07003942 if (flags & EXT4_SYNC_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04003943 new_fl |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07003944 if (flags & EXT4_APPEND_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04003945 new_fl |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07003946 if (flags & EXT4_IMMUTABLE_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04003947 new_fl |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07003948 if (flags & EXT4_NOATIME_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04003949 new_fl |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07003950 if (flags & EXT4_DIRSYNC_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04003951 new_fl |= S_DIRSYNC;
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003952 if (test_opt(inode->i_sb, DAX))
3953 new_fl |= S_DAX;
Theodore Ts'o5f16f322014-03-24 14:43:12 -04003954 inode_set_flags(inode, new_fl,
Ross Zwisler923ae0f2015-02-16 15:59:38 -08003955 S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|S_DAX);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003956}
3957
Jan Karaff9ddf72007-07-18 09:24:20 -04003958/* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
3959void ext4_get_inode_flags(struct ext4_inode_info *ei)
3960{
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04003961 unsigned int vfs_fl;
3962 unsigned long old_fl, new_fl;
Jan Karaff9ddf72007-07-18 09:24:20 -04003963
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04003964 do {
3965 vfs_fl = ei->vfs_inode.i_flags;
3966 old_fl = ei->i_flags;
3967 new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
3968 EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|
3969 EXT4_DIRSYNC_FL);
3970 if (vfs_fl & S_SYNC)
3971 new_fl |= EXT4_SYNC_FL;
3972 if (vfs_fl & S_APPEND)
3973 new_fl |= EXT4_APPEND_FL;
3974 if (vfs_fl & S_IMMUTABLE)
3975 new_fl |= EXT4_IMMUTABLE_FL;
3976 if (vfs_fl & S_NOATIME)
3977 new_fl |= EXT4_NOATIME_FL;
3978 if (vfs_fl & S_DIRSYNC)
3979 new_fl |= EXT4_DIRSYNC_FL;
3980 } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl);
Jan Karaff9ddf72007-07-18 09:24:20 -04003981}
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003982
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003983static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003984 struct ext4_inode_info *ei)
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003985{
3986 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05003987 struct inode *inode = &(ei->vfs_inode);
3988 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003989
3990 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3991 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
3992 /* we are using combined 48 bit field */
3993 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
3994 le32_to_cpu(raw_inode->i_blocks_lo);
Theodore Ts'o07a03822010-06-14 09:54:48 -04003995 if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05003996 /* i_blocks represent file system block size */
3997 return i_blocks << (inode->i_blkbits - 9);
3998 } else {
3999 return i_blocks;
4000 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004001 } else {
4002 return le32_to_cpu(raw_inode->i_blocks_lo);
4003 }
4004}
Jan Karaff9ddf72007-07-18 09:24:20 -04004005
Tao Ma152a7b02012-12-02 11:13:24 -05004006static inline void ext4_iget_extra_inode(struct inode *inode,
4007 struct ext4_inode *raw_inode,
4008 struct ext4_inode_info *ei)
4009{
4010 __le32 *magic = (void *)raw_inode +
4011 EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
Tao Ma67cf5b02012-12-10 14:04:46 -05004012 if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
Tao Ma152a7b02012-12-02 11:13:24 -05004013 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Tao Ma67cf5b02012-12-10 14:04:46 -05004014 ext4_find_inline_data_nolock(inode);
Tao Maf19d5872012-12-10 14:05:51 -05004015 } else
4016 EXT4_I(inode)->i_inline_off = 0;
Tao Ma152a7b02012-12-02 11:13:24 -05004017}
4018
David Howells1d1fe1e2008-02-07 00:15:37 -08004019struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004020{
Mingming Cao617ba132006-10-11 01:20:53 -07004021 struct ext4_iloc iloc;
4022 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08004023 struct ext4_inode_info *ei;
David Howells1d1fe1e2008-02-07 00:15:37 -08004024 struct inode *inode;
Jan Karab436b9b2009-12-08 23:51:10 -05004025 journal_t *journal = EXT4_SB(sb)->s_journal;
David Howells1d1fe1e2008-02-07 00:15:37 -08004026 long ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004027 int block;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004028 uid_t i_uid;
4029 gid_t i_gid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004030
David Howells1d1fe1e2008-02-07 00:15:37 -08004031 inode = iget_locked(sb, ino);
4032 if (!inode)
4033 return ERR_PTR(-ENOMEM);
4034 if (!(inode->i_state & I_NEW))
4035 return inode;
4036
4037 ei = EXT4_I(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05004038 iloc.bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004039
David Howells1d1fe1e2008-02-07 00:15:37 -08004040 ret = __ext4_get_inode_loc(inode, &iloc, 0);
4041 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004042 goto bad_inode;
Mingming Cao617ba132006-10-11 01:20:53 -07004043 raw_inode = ext4_raw_inode(&iloc);
Darrick J. Wong814525f2012-04-29 18:31:10 -04004044
4045 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4046 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
4047 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
4048 EXT4_INODE_SIZE(inode->i_sb)) {
4049 EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)",
4050 EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize,
4051 EXT4_INODE_SIZE(inode->i_sb));
4052 ret = -EIO;
4053 goto bad_inode;
4054 }
4055 } else
4056 ei->i_extra_isize = 0;
4057
4058 /* Precompute checksum seed for inode metadata */
Dmitry Monakhov9aa5d322014-10-13 03:36:16 -04004059 if (ext4_has_metadata_csum(sb)) {
Darrick J. Wong814525f2012-04-29 18:31:10 -04004060 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4061 __u32 csum;
4062 __le32 inum = cpu_to_le32(inode->i_ino);
4063 __le32 gen = raw_inode->i_generation;
4064 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
4065 sizeof(inum));
4066 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
4067 sizeof(gen));
4068 }
4069
4070 if (!ext4_inode_csum_verify(inode, raw_inode, ei)) {
4071 EXT4_ERROR_INODE(inode, "checksum invalid");
4072 ret = -EIO;
4073 goto bad_inode;
4074 }
4075
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004076 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004077 i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4078 i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004079 if (!(test_opt(inode->i_sb, NO_UID32))) {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004080 i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4081 i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004082 }
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004083 i_uid_write(inode, i_uid);
4084 i_gid_write(inode, i_gid);
Miklos Szeredibfe86842011-10-28 14:13:29 +02004085 set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004086
Theodore Ts'o353eb832011-01-10 12:18:25 -05004087 ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
Tao Ma67cf5b02012-12-10 14:04:46 -05004088 ei->i_inline_off = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004089 ei->i_dir_start_lookup = 0;
4090 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4091 /* We now have enough fields to check if the inode was active or not.
4092 * This is needed because nfsd might try to access dead inodes
4093 * the test is that same one that e2fsck uses
4094 * NeilBrown 1999oct15
4095 */
4096 if (inode->i_nlink == 0) {
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004097 if ((inode->i_mode == 0 ||
4098 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) &&
4099 ino != EXT4_BOOT_LOADER_INO) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004100 /* this inode is deleted */
David Howells1d1fe1e2008-02-07 00:15:37 -08004101 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004102 goto bad_inode;
4103 }
4104 /* The only unlinked inodes we let through here have
4105 * valid i_mode and are being read by the orphan
4106 * recovery code: that's fine, we're about to complete
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004107 * the process of deleting those.
4108 * OR it is the EXT4_BOOT_LOADER_INO which is
4109 * not initialized on a new filesystem. */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004110 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004111 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004112 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004113 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Theodore Ts'oa9e81742009-04-24 16:11:18 -04004114 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004115 ei->i_file_acl |=
4116 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004117 inode->i_size = ext4_isize(raw_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004118 ei->i_disksize = inode->i_size;
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03004119#ifdef CONFIG_QUOTA
4120 ei->i_reserved_quota = 0;
4121#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004122 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4123 ei->i_block_group = iloc.block_group;
Theodore Ts'oa4912122009-03-12 12:18:34 -04004124 ei->i_last_alloc_group = ~0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004125 /*
4126 * NOTE! The in-memory inode i_data array is in little-endian order
4127 * even on big-endian machines: we do NOT byteswap the block numbers!
4128 */
Mingming Cao617ba132006-10-11 01:20:53 -07004129 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004130 ei->i_data[block] = raw_inode->i_block[block];
4131 INIT_LIST_HEAD(&ei->i_orphan);
4132
Jan Karab436b9b2009-12-08 23:51:10 -05004133 /*
4134 * Set transaction id's of transactions that have to be committed
4135 * to finish f[data]sync. We set them to currently running transaction
4136 * as we cannot be sure that the inode or some of its metadata isn't
4137 * part of the transaction - the inode could have been reclaimed and
4138 * now it is reread from disk.
4139 */
4140 if (journal) {
4141 transaction_t *transaction;
4142 tid_t tid;
4143
Theodore Ts'oa931da62010-08-03 21:35:12 -04004144 read_lock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05004145 if (journal->j_running_transaction)
4146 transaction = journal->j_running_transaction;
4147 else
4148 transaction = journal->j_committing_transaction;
4149 if (transaction)
4150 tid = transaction->t_tid;
4151 else
4152 tid = journal->j_commit_sequence;
Theodore Ts'oa931da62010-08-03 21:35:12 -04004153 read_unlock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05004154 ei->i_sync_tid = tid;
4155 ei->i_datasync_tid = tid;
4156 }
4157
Eric Sandeen0040d982008-02-05 22:36:43 -05004158 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004159 if (ei->i_extra_isize == 0) {
4160 /* The extra space is currently unused. Use it. */
Mingming Cao617ba132006-10-11 01:20:53 -07004161 ei->i_extra_isize = sizeof(struct ext4_inode) -
4162 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004163 } else {
Tao Ma152a7b02012-12-02 11:13:24 -05004164 ext4_iget_extra_inode(inode, raw_inode, ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004165 }
Darrick J. Wong814525f2012-04-29 18:31:10 -04004166 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004167
Kalpak Shahef7f3832007-07-18 09:15:20 -04004168 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4169 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4170 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4171 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4172
Theodore Ts'oed3654e2014-03-24 14:09:06 -04004173 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
Theodore Ts'oc4f65702014-03-20 00:32:57 -04004174 inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
4175 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4176 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4177 inode->i_version |=
4178 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
4179 }
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004180 }
4181
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004182 ret = 0;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004183 if (ei->i_file_acl &&
Theodore Ts'o10329882009-11-15 15:29:56 -05004184 !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04004185 EXT4_ERROR_INODE(inode, "bad extended attribute block %llu",
4186 ei->i_file_acl);
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004187 ret = -EIO;
4188 goto bad_inode;
Tao Maf19d5872012-12-10 14:05:51 -05004189 } else if (!ext4_has_inline_data(inode)) {
4190 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
4191 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4192 (S_ISLNK(inode->i_mode) &&
4193 !ext4_inode_is_fast_symlink(inode))))
4194 /* Validate extent which is part of inode */
4195 ret = ext4_ext_check_inode(inode);
4196 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4197 (S_ISLNK(inode->i_mode) &&
4198 !ext4_inode_is_fast_symlink(inode))) {
4199 /* Validate block references which are part of inode */
4200 ret = ext4_ind_check_inode(inode);
4201 }
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004202 }
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004203 if (ret)
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004204 goto bad_inode;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -04004205
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004206 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004207 inode->i_op = &ext4_file_inode_operations;
Boaz Harroshbe64f882015-04-15 16:15:17 -07004208 inode->i_fop = &ext4_file_operations;
Mingming Cao617ba132006-10-11 01:20:53 -07004209 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004210 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004211 inode->i_op = &ext4_dir_inode_operations;
4212 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004213 } else if (S_ISLNK(inode->i_mode)) {
Theodore Ts'of348c252015-04-16 01:55:00 -04004214 if (ext4_inode_is_fast_symlink(inode) &&
4215 !ext4_encrypted_inode(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004216 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00004217 nd_terminate_link(ei->i_data, inode->i_size,
4218 sizeof(ei->i_data) - 1);
4219 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004220 inode->i_op = &ext4_symlink_inode_operations;
4221 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004222 }
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004223 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
4224 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004225 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004226 if (raw_inode->i_block[0])
4227 init_special_inode(inode, inode->i_mode,
4228 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4229 else
4230 init_special_inode(inode, inode->i_mode,
4231 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004232 } else if (ino == EXT4_BOOT_LOADER_INO) {
4233 make_bad_inode(inode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004234 } else {
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004235 ret = -EIO;
Theodore Ts'o24676da2010-05-16 21:00:00 -04004236 EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004237 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004238 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004239 brelse(iloc.bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004240 ext4_set_inode_flags(inode);
David Howells1d1fe1e2008-02-07 00:15:37 -08004241 unlock_new_inode(inode);
4242 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004243
4244bad_inode:
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004245 brelse(iloc.bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004246 iget_failed(inode);
4247 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004248}
4249
Theodore Ts'of4bb2982014-10-05 22:56:00 -04004250struct inode *ext4_iget_normal(struct super_block *sb, unsigned long ino)
4251{
4252 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
4253 return ERR_PTR(-EIO);
4254 return ext4_iget(sb, ino);
4255}
4256
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004257static int ext4_inode_blocks_set(handle_t *handle,
4258 struct ext4_inode *raw_inode,
4259 struct ext4_inode_info *ei)
4260{
4261 struct inode *inode = &(ei->vfs_inode);
4262 u64 i_blocks = inode->i_blocks;
4263 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004264
4265 if (i_blocks <= ~0U) {
4266 /*
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004267 * i_blocks can be represented in a 32 bit variable
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004268 * as multiple of 512 bytes
4269 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004270 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004271 raw_inode->i_blocks_high = 0;
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004272 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004273 return 0;
4274 }
4275 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
4276 return -EFBIG;
4277
4278 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004279 /*
4280 * i_blocks can be represented in a 48 bit variable
4281 * as multiple of 512 bytes
4282 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004283 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004284 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004285 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004286 } else {
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004287 ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004288 /* i_block is stored in file system block size */
4289 i_blocks = i_blocks >> (inode->i_blkbits - 9);
4290 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4291 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004292 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004293 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004294}
4295
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004296struct other_inode {
4297 unsigned long orig_ino;
4298 struct ext4_inode *raw_inode;
4299};
4300
4301static int other_inode_match(struct inode * inode, unsigned long ino,
4302 void *data)
4303{
4304 struct other_inode *oi = (struct other_inode *) data;
4305
4306 if ((inode->i_ino != ino) ||
4307 (inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW |
4308 I_DIRTY_SYNC | I_DIRTY_DATASYNC)) ||
4309 ((inode->i_state & I_DIRTY_TIME) == 0))
4310 return 0;
4311 spin_lock(&inode->i_lock);
4312 if (((inode->i_state & (I_FREEING | I_WILL_FREE | I_NEW |
4313 I_DIRTY_SYNC | I_DIRTY_DATASYNC)) == 0) &&
4314 (inode->i_state & I_DIRTY_TIME)) {
4315 struct ext4_inode_info *ei = EXT4_I(inode);
4316
4317 inode->i_state &= ~(I_DIRTY_TIME | I_DIRTY_TIME_EXPIRED);
4318 spin_unlock(&inode->i_lock);
4319
4320 spin_lock(&ei->i_raw_lock);
4321 EXT4_INODE_SET_XTIME(i_ctime, inode, oi->raw_inode);
4322 EXT4_INODE_SET_XTIME(i_mtime, inode, oi->raw_inode);
4323 EXT4_INODE_SET_XTIME(i_atime, inode, oi->raw_inode);
4324 ext4_inode_csum_set(inode, oi->raw_inode, ei);
4325 spin_unlock(&ei->i_raw_lock);
4326 trace_ext4_other_inode_update_time(inode, oi->orig_ino);
4327 return -1;
4328 }
4329 spin_unlock(&inode->i_lock);
4330 return -1;
4331}
4332
4333/*
4334 * Opportunistically update the other time fields for other inodes in
4335 * the same inode table block.
4336 */
4337static void ext4_update_other_inodes_time(struct super_block *sb,
4338 unsigned long orig_ino, char *buf)
4339{
4340 struct other_inode oi;
4341 unsigned long ino;
4342 int i, inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
4343 int inode_size = EXT4_INODE_SIZE(sb);
4344
4345 oi.orig_ino = orig_ino;
4346 ino = orig_ino & ~(inodes_per_block - 1);
4347 for (i = 0; i < inodes_per_block; i++, ino++, buf += inode_size) {
4348 if (ino == orig_ino)
4349 continue;
4350 oi.raw_inode = (struct ext4_inode *) buf;
4351 (void) find_inode_nowait(sb, ino, other_inode_match, &oi);
4352 }
4353}
4354
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004355/*
4356 * Post the struct inode info into an on-disk inode location in the
4357 * buffer-cache. This gobbles the caller's reference to the
4358 * buffer_head in the inode location struct.
4359 *
4360 * The caller must have write access to iloc->bh.
4361 */
Mingming Cao617ba132006-10-11 01:20:53 -07004362static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004363 struct inode *inode,
Frank Mayhar830156c2009-09-29 10:07:47 -04004364 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004365{
Mingming Cao617ba132006-10-11 01:20:53 -07004366 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
4367 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004368 struct buffer_head *bh = iloc->bh;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004369 struct super_block *sb = inode->i_sb;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004370 int err = 0, rc, block;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004371 int need_datasync = 0, set_large_file = 0;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004372 uid_t i_uid;
4373 gid_t i_gid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004374
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004375 spin_lock(&ei->i_raw_lock);
4376
4377 /* For fields not tracked in the in-memory inode,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004378 * initialise them to zero for new inodes. */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004379 if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
Mingming Cao617ba132006-10-11 01:20:53 -07004380 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004381
Jan Karaff9ddf72007-07-18 09:24:20 -04004382 ext4_get_inode_flags(ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004383 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004384 i_uid = i_uid_read(inode);
4385 i_gid = i_gid_read(inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004386 if (!(test_opt(inode->i_sb, NO_UID32))) {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004387 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid));
4388 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004389/*
4390 * Fix up interoperability with old kernels. Otherwise, old inodes get
4391 * re-used with the upper 16 bits of the uid/gid intact
4392 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004393 if (!ei->i_dtime) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004394 raw_inode->i_uid_high =
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004395 cpu_to_le16(high_16_bits(i_uid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004396 raw_inode->i_gid_high =
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004397 cpu_to_le16(high_16_bits(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004398 } else {
4399 raw_inode->i_uid_high = 0;
4400 raw_inode->i_gid_high = 0;
4401 }
4402 } else {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004403 raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid));
4404 raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004405 raw_inode->i_uid_high = 0;
4406 raw_inode->i_gid_high = 0;
4407 }
4408 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004409
4410 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
4411 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
4412 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
4413 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
4414
Li Xibce92d52014-10-01 22:11:06 -04004415 err = ext4_inode_blocks_set(handle, raw_inode, ei);
4416 if (err) {
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004417 spin_unlock(&ei->i_raw_lock);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004418 goto out_brelse;
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004419 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004420 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Theodore Ts'o353eb832011-01-10 12:18:25 -05004421 raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
Theodore Ts'oed3654e2014-03-24 14:09:06 -04004422 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT)))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004423 raw_inode->i_file_acl_high =
4424 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004425 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Jan Karab71fc072012-09-26 21:52:20 -04004426 if (ei->i_disksize != ext4_isize(raw_inode)) {
4427 ext4_isize_set(raw_inode, ei->i_disksize);
4428 need_datasync = 1;
4429 }
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004430 if (ei->i_disksize > 0x7fffffffULL) {
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004431 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
4432 EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
4433 EXT4_SB(sb)->s_es->s_rev_level ==
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004434 cpu_to_le32(EXT4_GOOD_OLD_REV))
4435 set_large_file = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004436 }
4437 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
4438 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
4439 if (old_valid_dev(inode->i_rdev)) {
4440 raw_inode->i_block[0] =
4441 cpu_to_le32(old_encode_dev(inode->i_rdev));
4442 raw_inode->i_block[1] = 0;
4443 } else {
4444 raw_inode->i_block[0] = 0;
4445 raw_inode->i_block[1] =
4446 cpu_to_le32(new_encode_dev(inode->i_rdev));
4447 raw_inode->i_block[2] = 0;
4448 }
Tao Maf19d5872012-12-10 14:05:51 -05004449 } else if (!ext4_has_inline_data(inode)) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004450 for (block = 0; block < EXT4_N_BLOCKS; block++)
4451 raw_inode->i_block[block] = ei->i_data[block];
Tao Maf19d5872012-12-10 14:05:51 -05004452 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004453
Theodore Ts'oed3654e2014-03-24 14:09:06 -04004454 if (likely(!test_opt2(inode->i_sb, HURD_COMPAT))) {
Theodore Ts'oc4f65702014-03-20 00:32:57 -04004455 raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
4456 if (ei->i_extra_isize) {
4457 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4458 raw_inode->i_version_hi =
4459 cpu_to_le32(inode->i_version >> 32);
4460 raw_inode->i_extra_isize =
4461 cpu_to_le16(ei->i_extra_isize);
4462 }
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004463 }
Darrick J. Wong814525f2012-04-29 18:31:10 -04004464 ext4_inode_csum_set(inode, raw_inode, ei);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004465 spin_unlock(&ei->i_raw_lock);
Theodore Ts'oa26f4992015-02-02 00:37:02 -05004466 if (inode->i_sb->s_flags & MS_LAZYTIME)
4467 ext4_update_other_inodes_time(inode->i_sb, inode->i_ino,
4468 bh->b_data);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004469
Frank Mayhar830156c2009-09-29 10:07:47 -04004470 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05004471 rc = ext4_handle_dirty_metadata(handle, NULL, bh);
Frank Mayhar830156c2009-09-29 10:07:47 -04004472 if (!err)
4473 err = rc;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004474 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004475 if (set_large_file) {
liang xie5d601252014-05-12 22:06:43 -04004476 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get write access");
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04004477 err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh);
4478 if (err)
4479 goto out_brelse;
4480 ext4_update_dynamic_rev(sb);
4481 EXT4_SET_RO_COMPAT_FEATURE(sb,
4482 EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
4483 ext4_handle_sync(handle);
4484 err = ext4_handle_dirty_super(handle, sb);
4485 }
Jan Karab71fc072012-09-26 21:52:20 -04004486 ext4_update_inode_fsync_trans(handle, inode, need_datasync);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004487out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004488 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004489 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004490 return err;
4491}
4492
4493/*
Mingming Cao617ba132006-10-11 01:20:53 -07004494 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004495 *
4496 * We are called from a few places:
4497 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04004498 * - Within generic_file_aio_write() -> generic_write_sync() for O_SYNC files.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004499 * Here, there will be no transaction running. We wait for any running
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004500 * transaction to commit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004501 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04004502 * - Within flush work (sys_sync(), kupdate and such).
4503 * We wait on commit, if told to.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004504 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04004505 * - Within iput_final() -> write_inode_now()
4506 * We wait on commit, if told to.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004507 *
4508 * In all cases it is actually safe for us to return without doing anything,
4509 * because the inode has been copied into a raw inode buffer in
Theodore Ts'o87f7e412014-04-08 11:38:28 -04004510 * ext4_mark_inode_dirty(). This is a correctness thing for WB_SYNC_ALL
4511 * writeback.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004512 *
4513 * Note that we are absolutely dependent upon all inode dirtiers doing the
4514 * right thing: they *must* call mark_inode_dirty() after dirtying info in
4515 * which we are interested.
4516 *
4517 * It would be a bug for them to not do this. The code:
4518 *
4519 * mark_inode_dirty(inode)
4520 * stuff();
4521 * inode->i_size = expr;
4522 *
Theodore Ts'o87f7e412014-04-08 11:38:28 -04004523 * is in error because write_inode() could occur while `stuff()' is running,
4524 * and the new i_size will be lost. Plus the inode will no longer be on the
4525 * superblock's dirty inode list.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004526 */
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004527int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004528{
Frank Mayhar91ac6f42009-09-09 22:33:47 -04004529 int err;
4530
Theodore Ts'o87f7e412014-04-08 11:38:28 -04004531 if (WARN_ON_ONCE(current->flags & PF_MEMALLOC))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004532 return 0;
4533
Frank Mayhar91ac6f42009-09-09 22:33:47 -04004534 if (EXT4_SB(inode->i_sb)->s_journal) {
4535 if (ext4_journal_current_handle()) {
4536 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
4537 dump_stack();
4538 return -EIO;
4539 }
4540
Jan Kara10542c22014-03-04 10:50:50 -05004541 /*
4542 * No need to force transaction in WB_SYNC_NONE mode. Also
4543 * ext4_sync_fs() will force the commit after everything is
4544 * written.
4545 */
4546 if (wbc->sync_mode != WB_SYNC_ALL || wbc->for_sync)
Frank Mayhar91ac6f42009-09-09 22:33:47 -04004547 return 0;
4548
4549 err = ext4_force_commit(inode->i_sb);
4550 } else {
4551 struct ext4_iloc iloc;
4552
Curt Wohlgemuth8b472d72010-04-03 16:45:06 -04004553 err = __ext4_get_inode_loc(inode, &iloc, 0);
Frank Mayhar91ac6f42009-09-09 22:33:47 -04004554 if (err)
4555 return err;
Jan Kara10542c22014-03-04 10:50:50 -05004556 /*
4557 * sync(2) will flush the whole buffer cache. No need to do
4558 * it here separately for each inode.
4559 */
4560 if (wbc->sync_mode == WB_SYNC_ALL && !wbc->for_sync)
Frank Mayhar830156c2009-09-29 10:07:47 -04004561 sync_dirty_buffer(iloc.bh);
4562 if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -04004563 EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr,
4564 "IO error syncing inode");
Frank Mayhar830156c2009-09-29 10:07:47 -04004565 err = -EIO;
4566 }
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004567 brelse(iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004568 }
Frank Mayhar91ac6f42009-09-09 22:33:47 -04004569 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004570}
4571
4572/*
Jan Kara53e87262012-12-25 13:29:52 -05004573 * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
4574 * buffers that are attached to a page stradding i_size and are undergoing
4575 * commit. In that case we have to wait for commit to finish and try again.
4576 */
4577static void ext4_wait_for_tail_page_commit(struct inode *inode)
4578{
4579 struct page *page;
4580 unsigned offset;
4581 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
4582 tid_t commit_tid = 0;
4583 int ret;
4584
4585 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
4586 /*
4587 * All buffers in the last page remain valid? Then there's nothing to
4588 * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE ==
4589 * blocksize case
4590 */
4591 if (offset > PAGE_CACHE_SIZE - (1 << inode->i_blkbits))
4592 return;
4593 while (1) {
4594 page = find_lock_page(inode->i_mapping,
4595 inode->i_size >> PAGE_CACHE_SHIFT);
4596 if (!page)
4597 return;
Lukas Czernerca99fdd2013-05-21 23:25:01 -04004598 ret = __ext4_journalled_invalidatepage(page, offset,
4599 PAGE_CACHE_SIZE - offset);
Jan Kara53e87262012-12-25 13:29:52 -05004600 unlock_page(page);
4601 page_cache_release(page);
4602 if (ret != -EBUSY)
4603 return;
4604 commit_tid = 0;
4605 read_lock(&journal->j_state_lock);
4606 if (journal->j_committing_transaction)
4607 commit_tid = journal->j_committing_transaction->t_tid;
4608 read_unlock(&journal->j_state_lock);
4609 if (commit_tid)
4610 jbd2_log_wait_commit(journal, commit_tid);
4611 }
4612}
4613
4614/*
Mingming Cao617ba132006-10-11 01:20:53 -07004615 * ext4_setattr()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004616 *
4617 * Called from notify_change.
4618 *
4619 * We want to trap VFS attempts to truncate the file as soon as
4620 * possible. In particular, we want to make sure that when the VFS
4621 * shrinks i_size, we put the inode on the orphan list and modify
4622 * i_disksize immediately, so that during the subsequent flushing of
4623 * dirty pages and freeing of disk blocks, we can guarantee that any
4624 * commit will leave the blocks being flushed in an unused state on
4625 * disk. (On recovery, the inode will get truncated and the blocks will
4626 * be freed, so we have a strong guarantee that no future commit will
4627 * leave these blocks visible to the user.)
4628 *
Jan Kara678aaf42008-07-11 19:27:31 -04004629 * Another thing we have to assure is that if we are in ordered mode
4630 * and inode is still attached to the committing transaction, we must
4631 * we start writeout of all the dirty pages which are being truncated.
4632 * This way we are sure that all the data written in the previous
4633 * transaction are already on disk (truncate waits for pages under
4634 * writeback).
4635 *
4636 * Called with inode->i_mutex down.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004637 */
Mingming Cao617ba132006-10-11 01:20:53 -07004638int ext4_setattr(struct dentry *dentry, struct iattr *attr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004639{
David Howells2b0143b2015-03-17 22:25:59 +00004640 struct inode *inode = d_inode(dentry);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004641 int error, rc = 0;
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04004642 int orphan = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004643 const unsigned int ia_valid = attr->ia_valid;
4644
4645 error = inode_change_ok(inode, attr);
4646 if (error)
4647 return error;
4648
Dmitry Monakhov12755622010-04-08 22:04:20 +04004649 if (is_quota_modification(inode, attr))
Christoph Hellwig871a2932010-03-03 09:05:07 -05004650 dquot_initialize(inode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004651 if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
4652 (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004653 handle_t *handle;
4654
4655 /* (user+group)*(old+new) structure, inode write (sb,
4656 * inode block, ? - but truncate inode update has it) */
Theodore Ts'o9924a922013-02-08 21:59:22 -05004657 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
4658 (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) +
4659 EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004660 if (IS_ERR(handle)) {
4661 error = PTR_ERR(handle);
4662 goto err_out;
4663 }
Christoph Hellwigb43fa822010-03-03 09:05:03 -05004664 error = dquot_transfer(inode, attr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004665 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07004666 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004667 return error;
4668 }
4669 /* Update corresponding info in inode so that everything is in
4670 * one transaction */
4671 if (attr->ia_valid & ATTR_UID)
4672 inode->i_uid = attr->ia_uid;
4673 if (attr->ia_valid & ATTR_GID)
4674 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07004675 error = ext4_mark_inode_dirty(handle, inode);
4676 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004677 }
4678
Jan Kara52083862013-08-17 10:07:17 -04004679 if (attr->ia_valid & ATTR_SIZE && attr->ia_size != inode->i_size) {
4680 handle_t *handle;
Christoph Hellwig562c72a2011-06-24 14:29:45 -04004681
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004682 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
Eric Sandeene2b46572008-01-28 23:58:27 -05004683 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4684
Theodore Ts'o0c095c72010-07-27 11:56:06 -04004685 if (attr->ia_size > sbi->s_bitmap_maxbytes)
4686 return -EFBIG;
Eric Sandeene2b46572008-01-28 23:58:27 -05004687 }
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004688
4689 if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size)
4690 inode_inc_iversion(inode);
4691
Jan Kara52083862013-08-17 10:07:17 -04004692 if (S_ISREG(inode->i_mode) &&
4693 (attr->ia_size < inode->i_size)) {
4694 if (ext4_should_order_data(inode)) {
4695 error = ext4_begin_ordered_truncate(inode,
Jan Kara678aaf42008-07-11 19:27:31 -04004696 attr->ia_size);
Jan Kara52083862013-08-17 10:07:17 -04004697 if (error)
Jan Kara678aaf42008-07-11 19:27:31 -04004698 goto err_out;
Jan Kara52083862013-08-17 10:07:17 -04004699 }
4700 handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
4701 if (IS_ERR(handle)) {
4702 error = PTR_ERR(handle);
4703 goto err_out;
4704 }
4705 if (ext4_handle_valid(handle)) {
4706 error = ext4_orphan_add(handle, inode);
4707 orphan = 1;
4708 }
Jan Kara90e775b2013-08-17 10:09:31 -04004709 down_write(&EXT4_I(inode)->i_data_sem);
Jan Kara52083862013-08-17 10:07:17 -04004710 EXT4_I(inode)->i_disksize = attr->ia_size;
4711 rc = ext4_mark_inode_dirty(handle, inode);
4712 if (!error)
4713 error = rc;
Jan Kara90e775b2013-08-17 10:09:31 -04004714 /*
4715 * We have to update i_size under i_data_sem together
4716 * with i_disksize to avoid races with writeback code
4717 * running ext4_wb_update_i_disksize().
4718 */
4719 if (!error)
4720 i_size_write(inode, attr->ia_size);
4721 up_write(&EXT4_I(inode)->i_data_sem);
Jan Kara52083862013-08-17 10:07:17 -04004722 ext4_journal_stop(handle);
4723 if (error) {
4724 ext4_orphan_del(NULL, inode);
Jan Kara678aaf42008-07-11 19:27:31 -04004725 goto err_out;
4726 }
Jan Karad6320cb2014-10-01 21:49:46 -04004727 } else {
4728 loff_t oldsize = inode->i_size;
4729
Jan Kara90e775b2013-08-17 10:09:31 -04004730 i_size_write(inode, attr->ia_size);
Jan Karad6320cb2014-10-01 21:49:46 -04004731 pagecache_isize_extended(inode, oldsize, inode->i_size);
4732 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004733
Jan Kara52083862013-08-17 10:07:17 -04004734 /*
4735 * Blocks are going to be removed from the inode. Wait
4736 * for dio in flight. Temporarily disable
4737 * dioread_nolock to prevent livelock.
4738 */
4739 if (orphan) {
4740 if (!ext4_should_journal_data(inode)) {
4741 ext4_inode_block_unlocked_dio(inode);
4742 inode_dio_wait(inode);
4743 ext4_inode_resume_unlocked_dio(inode);
4744 } else
4745 ext4_wait_for_tail_page_commit(inode);
Dmitry Monakhov1c9114f2012-09-29 00:55:23 -04004746 }
Jan Kara52083862013-08-17 10:07:17 -04004747 /*
4748 * Truncate pagecache after we've waited for commit
4749 * in data=journal mode to make pages freeable.
4750 */
Ross Zwisler923ae0f2015-02-16 15:59:38 -08004751 truncate_pagecache(inode, inode->i_size);
Theodore Ts'o072bd7e2011-05-23 15:13:02 -04004752 }
Jan Kara52083862013-08-17 10:07:17 -04004753 /*
4754 * We want to call ext4_truncate() even if attr->ia_size ==
4755 * inode->i_size for cases like truncation of fallocated space
4756 */
4757 if (attr->ia_valid & ATTR_SIZE)
4758 ext4_truncate(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004759
Christoph Hellwig10257742010-06-04 11:30:02 +02004760 if (!rc) {
4761 setattr_copy(inode, attr);
4762 mark_inode_dirty(inode);
4763 }
4764
4765 /*
4766 * If the call to ext4_truncate failed to get a transaction handle at
4767 * all, we need to clean up the in-core orphan list manually.
4768 */
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04004769 if (orphan && inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07004770 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004771
4772 if (!rc && (ia_valid & ATTR_MODE))
Christoph Hellwig64e178a2013-12-20 05:16:44 -08004773 rc = posix_acl_chmod(inode, inode->i_mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004774
4775err_out:
Mingming Cao617ba132006-10-11 01:20:53 -07004776 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004777 if (!error)
4778 error = rc;
4779 return error;
4780}
4781
Mingming Cao3e3398a2008-07-11 19:27:31 -04004782int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
4783 struct kstat *stat)
4784{
4785 struct inode *inode;
Jan Kara8af8eec2013-05-31 19:39:56 -04004786 unsigned long long delalloc_blocks;
Mingming Cao3e3398a2008-07-11 19:27:31 -04004787
David Howells2b0143b2015-03-17 22:25:59 +00004788 inode = d_inode(dentry);
Mingming Cao3e3398a2008-07-11 19:27:31 -04004789 generic_fillattr(inode, stat);
4790
4791 /*
Andreas Dilger9206c562013-11-11 22:38:12 -05004792 * If there is inline data in the inode, the inode will normally not
4793 * have data blocks allocated (it may have an external xattr block).
4794 * Report at least one sector for such files, so tools like tar, rsync,
4795 * others doen't incorrectly think the file is completely sparse.
4796 */
4797 if (unlikely(ext4_has_inline_data(inode)))
4798 stat->blocks += (stat->size + 511) >> 9;
4799
4800 /*
Mingming Cao3e3398a2008-07-11 19:27:31 -04004801 * We can't update i_blocks if the block allocation is delayed
4802 * otherwise in the case of system crash before the real block
4803 * allocation is done, we will have i_blocks inconsistent with
4804 * on-disk file blocks.
4805 * We always keep i_blocks updated together with real
4806 * allocation. But to not confuse with user, stat
4807 * will return the blocks that include the delayed allocation
4808 * blocks for this file.
4809 */
Tao Ma96607552012-05-31 22:54:16 -04004810 delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
Andreas Dilger9206c562013-11-11 22:38:12 -05004811 EXT4_I(inode)->i_reserved_data_blocks);
4812 stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9);
Mingming Cao3e3398a2008-07-11 19:27:31 -04004813 return 0;
4814}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004815
Jan Karafffb2732013-06-04 13:01:11 -04004816static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
4817 int pextents)
Mingming Caoa02908f2008-08-19 22:16:07 -04004818{
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004819 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Jan Karafffb2732013-06-04 13:01:11 -04004820 return ext4_ind_trans_blocks(inode, lblocks);
4821 return ext4_ext_index_trans_blocks(inode, pextents);
Mingming Caoa02908f2008-08-19 22:16:07 -04004822}
Theodore Ts'oac51d832008-11-06 16:49:36 -05004823
Mingming Caoa02908f2008-08-19 22:16:07 -04004824/*
4825 * Account for index blocks, block groups bitmaps and block group
4826 * descriptor blocks if modify datablocks and index blocks
4827 * worse case, the indexs blocks spread over different block groups
4828 *
4829 * If datablocks are discontiguous, they are possible to spread over
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004830 * different block groups too. If they are contiguous, with flexbg,
Mingming Caoa02908f2008-08-19 22:16:07 -04004831 * they could still across block group boundary.
4832 *
4833 * Also account for superblock, inode, quota and xattr blocks
4834 */
Jan Karafffb2732013-06-04 13:01:11 -04004835static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
4836 int pextents)
Mingming Caoa02908f2008-08-19 22:16:07 -04004837{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004838 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
4839 int gdpblocks;
Mingming Caoa02908f2008-08-19 22:16:07 -04004840 int idxblocks;
4841 int ret = 0;
4842
4843 /*
Jan Karafffb2732013-06-04 13:01:11 -04004844 * How many index blocks need to touch to map @lblocks logical blocks
4845 * to @pextents physical extents?
Mingming Caoa02908f2008-08-19 22:16:07 -04004846 */
Jan Karafffb2732013-06-04 13:01:11 -04004847 idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents);
Mingming Caoa02908f2008-08-19 22:16:07 -04004848
4849 ret = idxblocks;
4850
4851 /*
4852 * Now let's see how many group bitmaps and group descriptors need
4853 * to account
4854 */
Jan Karafffb2732013-06-04 13:01:11 -04004855 groups = idxblocks + pextents;
Mingming Caoa02908f2008-08-19 22:16:07 -04004856 gdpblocks = groups;
Theodore Ts'o8df96752009-05-01 08:50:38 -04004857 if (groups > ngroups)
4858 groups = ngroups;
Mingming Caoa02908f2008-08-19 22:16:07 -04004859 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
4860 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
4861
4862 /* bitmaps and block group descriptor blocks */
4863 ret += groups + gdpblocks;
4864
4865 /* Blocks for super block, inode, quota and xattr blocks */
4866 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004867
4868 return ret;
4869}
4870
4871/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004872 * Calculate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04004873 * the modification of a single pages into a single transaction,
4874 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04004875 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04004876 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04004877 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04004878 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04004879 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04004880 */
4881int ext4_writepage_trans_blocks(struct inode *inode)
4882{
4883 int bpp = ext4_journal_blocks_per_page(inode);
4884 int ret;
4885
Jan Karafffb2732013-06-04 13:01:11 -04004886 ret = ext4_meta_trans_blocks(inode, bpp, bpp);
Mingming Caoa02908f2008-08-19 22:16:07 -04004887
4888 /* Account for data blocks for journalled mode */
4889 if (ext4_should_journal_data(inode))
4890 ret += bpp;
4891 return ret;
4892}
Mingming Caof3bd1f32008-08-19 22:16:03 -04004893
4894/*
4895 * Calculate the journal credits for a chunk of data modification.
4896 *
4897 * This is called from DIO, fallocate or whoever calling
Eric Sandeen79e83032010-07-27 11:56:07 -04004898 * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
Mingming Caof3bd1f32008-08-19 22:16:03 -04004899 *
4900 * journal buffers for data blocks are not included here, as DIO
4901 * and fallocate do no need to journal data buffers.
4902 */
4903int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
4904{
4905 return ext4_meta_trans_blocks(inode, nrblocks, 1);
4906}
4907
Mingming Caoa02908f2008-08-19 22:16:07 -04004908/*
Mingming Cao617ba132006-10-11 01:20:53 -07004909 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004910 * Give this, we know that the caller already has write access to iloc->bh.
4911 */
Mingming Cao617ba132006-10-11 01:20:53 -07004912int ext4_mark_iloc_dirty(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004913 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004914{
4915 int err = 0;
4916
Theodore Ts'oc64db502012-03-02 12:23:11 -05004917 if (IS_I_VERSION(inode))
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004918 inode_inc_iversion(inode);
4919
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004920 /* the do_update_inode consumes one bh->b_count */
4921 get_bh(iloc->bh);
4922
Mingming Caodab291a2006-10-11 01:21:01 -07004923 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Frank Mayhar830156c2009-09-29 10:07:47 -04004924 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004925 put_bh(iloc->bh);
4926 return err;
4927}
4928
4929/*
4930 * On success, We end up with an outstanding reference count against
4931 * iloc->bh. This _must_ be cleaned up later.
4932 */
4933
4934int
Mingming Cao617ba132006-10-11 01:20:53 -07004935ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
4936 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004937{
Frank Mayhar03901312009-01-07 00:06:22 -05004938 int err;
4939
4940 err = ext4_get_inode_loc(inode, iloc);
4941 if (!err) {
4942 BUFFER_TRACE(iloc->bh, "get_write_access");
4943 err = ext4_journal_get_write_access(handle, iloc->bh);
4944 if (err) {
4945 brelse(iloc->bh);
4946 iloc->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004947 }
4948 }
Mingming Cao617ba132006-10-11 01:20:53 -07004949 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004950 return err;
4951}
4952
4953/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004954 * Expand an inode by new_extra_isize bytes.
4955 * Returns 0 on success or negative error number on failure.
4956 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05004957static int ext4_expand_extra_isize(struct inode *inode,
4958 unsigned int new_extra_isize,
4959 struct ext4_iloc iloc,
4960 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004961{
4962 struct ext4_inode *raw_inode;
4963 struct ext4_xattr_ibody_header *header;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004964
4965 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
4966 return 0;
4967
4968 raw_inode = ext4_raw_inode(&iloc);
4969
4970 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004971
4972 /* No extended attributes present */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004973 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
4974 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004975 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
4976 new_extra_isize);
4977 EXT4_I(inode)->i_extra_isize = new_extra_isize;
4978 return 0;
4979 }
4980
4981 /* try to expand with EAs present */
4982 return ext4_expand_extra_isize_ea(inode, new_extra_isize,
4983 raw_inode, handle);
4984}
4985
4986/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004987 * What we do here is to mark the in-core inode as clean with respect to inode
4988 * dirtiness (it may still be data-dirty).
4989 * This means that the in-core inode may be reaped by prune_icache
4990 * without having to perform any I/O. This is a very good thing,
4991 * because *any* task may call prune_icache - even ones which
4992 * have a transaction open against a different journal.
4993 *
4994 * Is this cheating? Not really. Sure, we haven't written the
4995 * inode out, but prune_icache isn't a user-visible syncing function.
4996 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
4997 * we start and wait on commits.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004998 */
Mingming Cao617ba132006-10-11 01:20:53 -07004999int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005000{
Mingming Cao617ba132006-10-11 01:20:53 -07005001 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005002 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
5003 static unsigned int mnt_count;
5004 int err, ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005005
5006 might_sleep();
Theodore Ts'o7ff9c072010-11-08 13:51:33 -05005007 trace_ext4_mark_inode_dirty(inode, _RET_IP_);
Mingming Cao617ba132006-10-11 01:20:53 -07005008 err = ext4_reserve_inode_write(handle, inode, &iloc);
Frank Mayhar03901312009-01-07 00:06:22 -05005009 if (ext4_handle_valid(handle) &&
5010 EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005011 !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005012 /*
5013 * We need extra buffer credits since we may write into EA block
5014 * with this same handle. If journal_extend fails, then it will
5015 * only result in a minor loss of functionality for that inode.
5016 * If this is felt to be critical, then e2fsck should be run to
5017 * force a large enough s_min_extra_isize.
5018 */
5019 if ((jbd2_journal_extend(handle,
5020 EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
5021 ret = ext4_expand_extra_isize(inode,
5022 sbi->s_want_extra_isize,
5023 iloc, handle);
5024 if (ret) {
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05005025 ext4_set_inode_state(inode,
5026 EXT4_STATE_NO_EXPAND);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005027 if (mnt_count !=
5028 le16_to_cpu(sbi->s_es->s_mnt_count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05005029 ext4_warning(inode->i_sb,
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005030 "Unable to expand inode %lu. Delete"
5031 " some EAs or run e2fsck.",
5032 inode->i_ino);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04005033 mnt_count =
5034 le16_to_cpu(sbi->s_es->s_mnt_count);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04005035 }
5036 }
5037 }
5038 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005039 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07005040 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005041 return err;
5042}
5043
5044/*
Mingming Cao617ba132006-10-11 01:20:53 -07005045 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005046 *
5047 * We're really interested in the case where a file is being extended.
5048 * i_size has been changed by generic_commit_write() and we thus need
5049 * to include the updated inode in the current transaction.
5050 *
Christoph Hellwig5dd40562010-03-03 09:05:00 -05005051 * Also, dquot_alloc_block() will always dirty the inode when blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005052 * are allocated to the file.
5053 *
5054 * If the inode is marked synchronous, we don't honour that here - doing
5055 * so would cause a commit on atime updates, which we don't bother doing.
5056 * We handle synchronous inodes at the highest possible level.
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05005057 *
5058 * If only the I_DIRTY_TIME flag is set, we can skip everything. If
5059 * I_DIRTY_TIME and I_DIRTY_SYNC is set, the only inode fields we need
5060 * to copy into the on-disk inode structure are the timestamp files.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005061 */
Christoph Hellwigaa385722011-05-27 06:53:02 -04005062void ext4_dirty_inode(struct inode *inode, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005063{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005064 handle_t *handle;
5065
Theodore Ts'o0ae45f62015-02-02 00:37:00 -05005066 if (flags == I_DIRTY_TIME)
5067 return;
Theodore Ts'o9924a922013-02-08 21:59:22 -05005068 handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005069 if (IS_ERR(handle))
5070 goto out;
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005071
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04005072 ext4_mark_inode_dirty(handle, inode);
5073
Mingming Cao617ba132006-10-11 01:20:53 -07005074 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005075out:
5076 return;
5077}
5078
5079#if 0
5080/*
5081 * Bind an inode's backing buffer_head into this transaction, to prevent
5082 * it from being flushed to disk early. Unlike
Mingming Cao617ba132006-10-11 01:20:53 -07005083 * ext4_reserve_inode_write, this leaves behind no bh reference and
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005084 * returns no iloc structure, so the caller needs to repeat the iloc
5085 * lookup to mark the inode dirty later.
5086 */
Mingming Cao617ba132006-10-11 01:20:53 -07005087static int ext4_pin_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005088{
Mingming Cao617ba132006-10-11 01:20:53 -07005089 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005090
5091 int err = 0;
5092 if (handle) {
Mingming Cao617ba132006-10-11 01:20:53 -07005093 err = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005094 if (!err) {
5095 BUFFER_TRACE(iloc.bh, "get_write_access");
Mingming Caodab291a2006-10-11 01:21:01 -07005096 err = jbd2_journal_get_write_access(handle, iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005097 if (!err)
Frank Mayhar03901312009-01-07 00:06:22 -05005098 err = ext4_handle_dirty_metadata(handle,
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05005099 NULL,
Frank Mayhar03901312009-01-07 00:06:22 -05005100 iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005101 brelse(iloc.bh);
5102 }
5103 }
Mingming Cao617ba132006-10-11 01:20:53 -07005104 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005105 return err;
5106}
5107#endif
5108
Mingming Cao617ba132006-10-11 01:20:53 -07005109int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005110{
5111 journal_t *journal;
5112 handle_t *handle;
5113 int err;
5114
5115 /*
5116 * We have to be very careful here: changing a data block's
5117 * journaling status dynamically is dangerous. If we write a
5118 * data block to the journal, change the status and then delete
5119 * that block, we risk forgetting to revoke the old log record
5120 * from the journal and so a subsequent replay can corrupt data.
5121 * So, first we make sure that the journal is empty and that
5122 * nobody is changing anything.
5123 */
5124
Mingming Cao617ba132006-10-11 01:20:53 -07005125 journal = EXT4_JOURNAL(inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005126 if (!journal)
5127 return 0;
Dave Hansend6995942007-07-18 08:33:51 -04005128 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005129 return -EROFS;
Yongqiang Yang2aff57b2011-12-28 12:02:13 -05005130 /* We have to allocate physical blocks for delalloc blocks
5131 * before flushing journal. otherwise delalloc blocks can not
5132 * be allocated any more. even more truncate on delalloc blocks
5133 * could trigger BUG by flushing delalloc blocks in journal.
5134 * There is no delalloc block in non-journal data mode.
5135 */
5136 if (val && test_opt(inode->i_sb, DELALLOC)) {
5137 err = ext4_alloc_da_blocks(inode);
5138 if (err < 0)
5139 return err;
5140 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005141
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04005142 /* Wait for all existing dio workers */
5143 ext4_inode_block_unlocked_dio(inode);
5144 inode_dio_wait(inode);
5145
Mingming Caodab291a2006-10-11 01:21:01 -07005146 jbd2_journal_lock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005147
5148 /*
5149 * OK, there are no updates running now, and all cached data is
5150 * synced to disk. We are now in a completely consistent state
5151 * which doesn't have anything in the journal, and we know that
5152 * no filesystem updates are running, so it is safe to modify
5153 * the inode's in-core data-journaling state flag now.
5154 */
5155
5156 if (val)
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005157 ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Yongqiang Yang5872dda2011-12-28 13:55:51 -05005158 else {
Jan Kara4f879ca2014-10-30 10:53:17 -04005159 err = jbd2_journal_flush(journal);
5160 if (err < 0) {
5161 jbd2_journal_unlock_updates(journal);
5162 ext4_inode_resume_unlocked_dio(inode);
5163 return err;
5164 }
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005165 ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Yongqiang Yang5872dda2011-12-28 13:55:51 -05005166 }
Mingming Cao617ba132006-10-11 01:20:53 -07005167 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005168
Mingming Caodab291a2006-10-11 01:21:01 -07005169 jbd2_journal_unlock_updates(journal);
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04005170 ext4_inode_resume_unlocked_dio(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005171
5172 /* Finally we can mark the inode as dirty. */
5173
Theodore Ts'o9924a922013-02-08 21:59:22 -05005174 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005175 if (IS_ERR(handle))
5176 return PTR_ERR(handle);
5177
Mingming Cao617ba132006-10-11 01:20:53 -07005178 err = ext4_mark_inode_dirty(handle, inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005179 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005180 ext4_journal_stop(handle);
5181 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005182
5183 return err;
5184}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005185
5186static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
5187{
5188 return !buffer_mapped(bh);
5189}
5190
Nick Pigginc2ec1752009-03-31 15:23:21 -07005191int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005192{
Nick Pigginc2ec1752009-03-31 15:23:21 -07005193 struct page *page = vmf->page;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005194 loff_t size;
5195 unsigned long len;
Jan Kara9ea7df52011-06-24 14:29:41 -04005196 int ret;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005197 struct file *file = vma->vm_file;
Al Viro496ad9a2013-01-23 17:07:38 -05005198 struct inode *inode = file_inode(file);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005199 struct address_space *mapping = inode->i_mapping;
Jan Kara9ea7df52011-06-24 14:29:41 -04005200 handle_t *handle;
5201 get_block_t *get_block;
5202 int retries = 0;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005203
Jan Kara8e8ad8a2012-06-12 16:20:38 +02005204 sb_start_pagefault(inode->i_sb);
Theodore Ts'o041bbb6d2012-09-30 23:04:56 -04005205 file_update_time(vma->vm_file);
Jan Kara9ea7df52011-06-24 14:29:41 -04005206 /* Delalloc case is easy... */
5207 if (test_opt(inode->i_sb, DELALLOC) &&
5208 !ext4_should_journal_data(inode) &&
5209 !ext4_nonda_switch(inode->i_sb)) {
5210 do {
5211 ret = __block_page_mkwrite(vma, vmf,
5212 ext4_da_get_block_prep);
5213 } while (ret == -ENOSPC &&
5214 ext4_should_retry_alloc(inode->i_sb, &retries));
5215 goto out_ret;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005216 }
Darrick J. Wong0e499892011-05-18 13:55:20 -04005217
5218 lock_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04005219 size = i_size_read(inode);
5220 /* Page got truncated from under us? */
5221 if (page->mapping != mapping || page_offset(page) > size) {
5222 unlock_page(page);
5223 ret = VM_FAULT_NOPAGE;
5224 goto out;
Darrick J. Wong0e499892011-05-18 13:55:20 -04005225 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005226
5227 if (page->index == size >> PAGE_CACHE_SHIFT)
5228 len = size & ~PAGE_CACHE_MASK;
5229 else
5230 len = PAGE_CACHE_SIZE;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005231 /*
Jan Kara9ea7df52011-06-24 14:29:41 -04005232 * Return if we have all the buffers mapped. This avoids the need to do
5233 * journal_start/journal_stop which can block and take a long time
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005234 */
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005235 if (page_has_buffers(page)) {
Tao Maf19d5872012-12-10 14:05:51 -05005236 if (!ext4_walk_page_buffers(NULL, page_buffers(page),
5237 0, len, NULL,
5238 ext4_bh_unmapped)) {
Jan Kara9ea7df52011-06-24 14:29:41 -04005239 /* Wait so that we don't change page under IO */
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08005240 wait_for_stable_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04005241 ret = VM_FAULT_LOCKED;
5242 goto out;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005243 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005244 }
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005245 unlock_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04005246 /* OK, we need to fill the hole... */
5247 if (ext4_should_dioread_nolock(inode))
5248 get_block = ext4_get_block_write;
5249 else
5250 get_block = ext4_get_block;
5251retry_alloc:
Theodore Ts'o9924a922013-02-08 21:59:22 -05005252 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
5253 ext4_writepage_trans_blocks(inode));
Jan Kara9ea7df52011-06-24 14:29:41 -04005254 if (IS_ERR(handle)) {
Nick Pigginc2ec1752009-03-31 15:23:21 -07005255 ret = VM_FAULT_SIGBUS;
Jan Kara9ea7df52011-06-24 14:29:41 -04005256 goto out;
5257 }
5258 ret = __block_page_mkwrite(vma, vmf, get_block);
5259 if (!ret && ext4_should_journal_data(inode)) {
Tao Maf19d5872012-12-10 14:05:51 -05005260 if (ext4_walk_page_buffers(handle, page_buffers(page), 0,
Jan Kara9ea7df52011-06-24 14:29:41 -04005261 PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) {
5262 unlock_page(page);
5263 ret = VM_FAULT_SIGBUS;
Yongqiang Yangfcbb5512011-10-26 05:00:19 -04005264 ext4_journal_stop(handle);
Jan Kara9ea7df52011-06-24 14:29:41 -04005265 goto out;
5266 }
5267 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
5268 }
5269 ext4_journal_stop(handle);
5270 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
5271 goto retry_alloc;
5272out_ret:
5273 ret = block_page_mkwrite_return(ret);
5274out:
Jan Kara8e8ad8a2012-06-12 16:20:38 +02005275 sb_end_pagefault(inode->i_sb);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005276 return ret;
5277}