blob: 24bfd7ff30491457283b22821eb5a9b93b2adec4 [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>
Mingming Caodab291a2006-10-11 01:21:01 -070023#include <linux/jbd2.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070024#include <linux/highuid.h>
25#include <linux/pagemap.h>
26#include <linux/quotaops.h>
27#include <linux/string.h>
28#include <linux/buffer_head.h>
29#include <linux/writeback.h>
Alex Tomas64769242008-07-11 19:27:31 -040030#include <linux/pagevec.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070031#include <linux/mpage.h>
Duane Griffine83c1392008-12-19 20:47:15 +000032#include <linux/namei.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070033#include <linux/uio.h>
34#include <linux/bio.h>
Mingming Cao4c0425f2009-09-28 15:48:41 -040035#include <linux/workqueue.h>
Jiaying Zhang744692d2010-03-04 16:14:02 -050036#include <linux/kernel.h>
Andrew Morton6db26ff2011-01-12 16:59:13 -080037#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Theodore Ts'oa8901d32010-12-17 10:40:47 -050039#include <linux/ratelimit.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070040#include <linux/aio.h>
Theodore Ts'o00a1a052014-03-30 10:20:01 -040041#include <linux/bitops.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040042
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040043#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070044#include "xattr.h"
45#include "acl.h"
Theodore Ts'o9f125d62011-06-27 19:16:04 -040046#include "truncate.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070047
Theodore Ts'o9bffad12009-06-17 11:48:11 -040048#include <trace/events/ext4.h>
49
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -040050#define MPAGE_DA_EXTENT_TAIL 0x01
51
Darrick J. Wong814525f2012-04-29 18:31:10 -040052static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw,
53 struct ext4_inode_info *ei)
54{
55 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
56 __u16 csum_lo;
57 __u16 csum_hi = 0;
58 __u32 csum;
59
Dmitry Monakhov171a7f22013-04-09 23:56:48 -040060 csum_lo = le16_to_cpu(raw->i_checksum_lo);
Darrick J. Wong814525f2012-04-29 18:31:10 -040061 raw->i_checksum_lo = 0;
62 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
63 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) {
Dmitry Monakhov171a7f22013-04-09 23:56:48 -040064 csum_hi = le16_to_cpu(raw->i_checksum_hi);
Darrick J. Wong814525f2012-04-29 18:31:10 -040065 raw->i_checksum_hi = 0;
66 }
67
68 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw,
69 EXT4_INODE_SIZE(inode->i_sb));
70
Dmitry Monakhov171a7f22013-04-09 23:56:48 -040071 raw->i_checksum_lo = cpu_to_le16(csum_lo);
Darrick J. Wong814525f2012-04-29 18:31:10 -040072 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
73 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
Dmitry Monakhov171a7f22013-04-09 23:56:48 -040074 raw->i_checksum_hi = cpu_to_le16(csum_hi);
Darrick J. Wong814525f2012-04-29 18:31:10 -040075
76 return csum;
77}
78
79static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw,
80 struct ext4_inode_info *ei)
81{
82 __u32 provided, calculated;
83
84 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
85 cpu_to_le32(EXT4_OS_LINUX) ||
86 !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
87 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
88 return 1;
89
90 provided = le16_to_cpu(raw->i_checksum_lo);
91 calculated = ext4_inode_csum(inode, raw, ei);
92 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
93 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
94 provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16;
95 else
96 calculated &= 0xFFFF;
97
98 return provided == calculated;
99}
100
101static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
102 struct ext4_inode_info *ei)
103{
104 __u32 csum;
105
106 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
107 cpu_to_le32(EXT4_OS_LINUX) ||
108 !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
109 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
110 return;
111
112 csum = ext4_inode_csum(inode, raw, ei);
113 raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF);
114 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
115 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
116 raw->i_checksum_hi = cpu_to_le16(csum >> 16);
117}
118
Jan Kara678aaf42008-07-11 19:27:31 -0400119static inline int ext4_begin_ordered_truncate(struct inode *inode,
120 loff_t new_size)
121{
Theodore Ts'o7ff9c072010-11-08 13:51:33 -0500122 trace_ext4_begin_ordered_truncate(inode, new_size);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -0500123 /*
124 * If jinode is zero, then we never opened the file for
125 * writing, so there's no need to call
126 * jbd2_journal_begin_ordered_truncate() since there's no
127 * outstanding writes we need to flush.
128 */
129 if (!EXT4_I(inode)->jinode)
130 return 0;
131 return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
132 EXT4_I(inode)->jinode,
133 new_size);
Jan Kara678aaf42008-07-11 19:27:31 -0400134}
135
Lukas Czernerd47992f2013-05-21 23:17:23 -0400136static void ext4_invalidatepage(struct page *page, unsigned int offset,
137 unsigned int length);
Theodore Ts'ocb20d512010-10-27 21:30:09 -0400138static int __ext4_journalled_writepage(struct page *page, unsigned int len);
139static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
Jan Karafffb2732013-06-04 13:01:11 -0400140static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
141 int pextents);
Alex Tomas64769242008-07-11 19:27:31 -0400142
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700143/*
144 * Test whether an inode is a fast symlink.
145 */
Mingming Cao617ba132006-10-11 01:20:53 -0700146static int ext4_inode_is_fast_symlink(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700147{
Yongqiang Yang65eddb52014-01-06 14:06:18 -0500148 int ea_blocks = EXT4_I(inode)->i_file_acl ?
149 EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700150
151 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
152}
153
154/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700155 * Restart the transaction associated with *handle. This does a commit,
156 * so before we call here everything must be consistently dirtied against
157 * this transaction.
158 */
Aneesh Kumar K.Vfa5d1112009-11-02 18:50:49 -0500159int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode,
Jan Kara487caee2009-08-17 22:17:20 -0400160 int nblocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700161{
Jan Kara487caee2009-08-17 22:17:20 -0400162 int ret;
163
164 /*
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400165 * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
Jan Kara487caee2009-08-17 22:17:20 -0400166 * moment, get_block can be called only for blocks inside i_size since
167 * page cache has been already dropped and writes are blocked by
168 * i_mutex. So we can safely drop the i_data_sem here.
169 */
Frank Mayhar03901312009-01-07 00:06:22 -0500170 BUG_ON(EXT4_JOURNAL(inode) == NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700171 jbd_debug(2, "restarting handle %p\n", handle);
Jan Kara487caee2009-08-17 22:17:20 -0400172 up_write(&EXT4_I(inode)->i_data_sem);
Amir Goldstein8e8eaab2011-02-27 23:32:12 -0500173 ret = ext4_journal_restart(handle, nblocks);
Jan Kara487caee2009-08-17 22:17:20 -0400174 down_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vfa5d1112009-11-02 18:50:49 -0500175 ext4_discard_preallocations(inode);
Jan Kara487caee2009-08-17 22:17:20 -0400176
177 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700178}
179
180/*
181 * Called at the last iput() if i_nlink is zero.
182 */
Al Viro0930fcc2010-06-07 13:16:22 -0400183void ext4_evict_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700184{
185 handle_t *handle;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400186 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700187
Theodore Ts'o7ff9c072010-11-08 13:51:33 -0500188 trace_ext4_evict_inode(inode);
Jiaying Zhang2581fdc2011-08-13 12:17:13 -0400189
Al Viro0930fcc2010-06-07 13:16:22 -0400190 if (inode->i_nlink) {
Jan Kara2d859db2011-07-26 09:07:11 -0400191 /*
192 * When journalling data dirty buffers are tracked only in the
193 * journal. So although mm thinks everything is clean and
194 * ready for reaping the inode might still have some pages to
195 * write in the running transaction or waiting to be
196 * checkpointed. Thus calling jbd2_journal_invalidatepage()
197 * (via truncate_inode_pages()) to discard these buffers can
198 * cause data loss. Also even if we did not discard these
199 * buffers, we would have no way to find them after the inode
200 * is reaped and thus user could see stale data if he tries to
201 * read them before the transaction is checkpointed. So be
202 * careful and force everything to disk here... We use
203 * ei->i_datasync_tid to store the newest transaction
204 * containing inode's data.
205 *
206 * Note that directories do not have this problem because they
207 * don't use page cache.
208 */
209 if (ext4_should_journal_data(inode) &&
Theodore Ts'o2b405bf2013-03-20 09:42:11 -0400210 (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode)) &&
211 inode->i_ino != EXT4_JOURNAL_INO) {
Jan Kara2d859db2011-07-26 09:07:11 -0400212 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
213 tid_t commit_tid = EXT4_I(inode)->i_datasync_tid;
214
Theodore Ts'od76a3a772013-04-03 22:02:52 -0400215 jbd2_complete_transaction(journal, commit_tid);
Jan Kara2d859db2011-07-26 09:07:11 -0400216 filemap_write_and_wait(&inode->i_data);
217 }
Al Viro0930fcc2010-06-07 13:16:22 -0400218 truncate_inode_pages(&inode->i_data, 0);
Jan Kara5dc23bd2013-06-04 14:46:12 -0400219
220 WARN_ON(atomic_read(&EXT4_I(inode)->i_ioend_count));
Al Viro0930fcc2010-06-07 13:16:22 -0400221 goto no_delete;
222 }
223
Christoph Hellwig907f4552010-03-03 09:05:06 -0500224 if (!is_bad_inode(inode))
Christoph Hellwig871a2932010-03-03 09:05:07 -0500225 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500226
Jan Kara678aaf42008-07-11 19:27:31 -0400227 if (ext4_should_order_data(inode))
228 ext4_begin_ordered_truncate(inode, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700229 truncate_inode_pages(&inode->i_data, 0);
230
Jan Kara5dc23bd2013-06-04 14:46:12 -0400231 WARN_ON(atomic_read(&EXT4_I(inode)->i_ioend_count));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700232 if (is_bad_inode(inode))
233 goto no_delete;
234
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200235 /*
236 * Protect us against freezing - iput() caller didn't have to have any
237 * protection against it
238 */
239 sb_start_intwrite(inode->i_sb);
Theodore Ts'o9924a922013-02-08 21:59:22 -0500240 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
241 ext4_blocks_for_truncate(inode)+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700242 if (IS_ERR(handle)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400243 ext4_std_error(inode->i_sb, PTR_ERR(handle));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700244 /*
245 * If we're going to skip the normal cleanup, we still need to
246 * make sure that the in-core orphan linked list is properly
247 * cleaned up.
248 */
Mingming Cao617ba132006-10-11 01:20:53 -0700249 ext4_orphan_del(NULL, inode);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200250 sb_end_intwrite(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700251 goto no_delete;
252 }
253
254 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500255 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700256 inode->i_size = 0;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400257 err = ext4_mark_inode_dirty(handle, inode);
258 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500259 ext4_warning(inode->i_sb,
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400260 "couldn't mark inode dirty (err %d)", err);
261 goto stop_handle;
262 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700263 if (inode->i_blocks)
Mingming Cao617ba132006-10-11 01:20:53 -0700264 ext4_truncate(inode);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400265
266 /*
267 * ext4_ext_truncate() doesn't reserve any slop when it
268 * restarts journal transactions; therefore there may not be
269 * enough credits left in the handle to remove the inode from
270 * the orphan list and set the dtime field.
271 */
Frank Mayhar03901312009-01-07 00:06:22 -0500272 if (!ext4_handle_has_enough_credits(handle, 3)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400273 err = ext4_journal_extend(handle, 3);
274 if (err > 0)
275 err = ext4_journal_restart(handle, 3);
276 if (err != 0) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500277 ext4_warning(inode->i_sb,
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400278 "couldn't extend journal (err %d)", err);
279 stop_handle:
280 ext4_journal_stop(handle);
Theodore Ts'o45388212010-07-29 15:06:10 -0400281 ext4_orphan_del(NULL, inode);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200282 sb_end_intwrite(inode->i_sb);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400283 goto no_delete;
284 }
285 }
286
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700287 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700288 * Kill off the orphan record which ext4_truncate created.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700289 * AKPM: I think this can be inside the above `if'.
Mingming Cao617ba132006-10-11 01:20:53 -0700290 * Note that ext4_orphan_del() has to be able to cope with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700291 * deletion of a non-existent orphan - this is because we don't
Mingming Cao617ba132006-10-11 01:20:53 -0700292 * know if ext4_truncate() actually created an orphan record.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700293 * (Well, we could do this if we need to, but heck - it works)
294 */
Mingming Cao617ba132006-10-11 01:20:53 -0700295 ext4_orphan_del(handle, inode);
296 EXT4_I(inode)->i_dtime = get_seconds();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700297
298 /*
299 * One subtle ordering requirement: if anything has gone wrong
300 * (transaction abort, IO errors, whatever), then we can still
301 * do these next steps (the fs will already have been marked as
302 * having errors), but we can't free the inode if the mark_dirty
303 * fails.
304 */
Mingming Cao617ba132006-10-11 01:20:53 -0700305 if (ext4_mark_inode_dirty(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700306 /* If that failed, just do the required in-core inode clear. */
Al Viro0930fcc2010-06-07 13:16:22 -0400307 ext4_clear_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700308 else
Mingming Cao617ba132006-10-11 01:20:53 -0700309 ext4_free_inode(handle, inode);
310 ext4_journal_stop(handle);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200311 sb_end_intwrite(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700312 return;
313no_delete:
Al Viro0930fcc2010-06-07 13:16:22 -0400314 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700315}
316
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300317#ifdef CONFIG_QUOTA
318qsize_t *ext4_get_reserved_space(struct inode *inode)
Mingming Cao60e58e02009-01-22 18:13:05 +0100319{
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300320 return &EXT4_I(inode)->i_reserved_quota;
Mingming Cao60e58e02009-01-22 18:13:05 +0100321}
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300322#endif
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500323
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400324/*
325 * Calculate the number of metadata blocks need to reserve
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500326 * to allocate a block located at @lblock
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400327 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -0500328static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400329{
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400330 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500331 return ext4_ext_calc_metadata_amount(inode, lblock);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400332
Amir Goldstein8bb2b242011-06-27 17:10:28 -0400333 return ext4_ind_calc_metadata_amount(inode, lblock);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400334}
335
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500336/*
337 * Called with i_data_sem down, which is important since we can call
338 * ext4_discard_preallocations() from here.
339 */
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500340void ext4_da_update_reserve_space(struct inode *inode,
341 int used, int quota_claim)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400342{
343 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500344 struct ext4_inode_info *ei = EXT4_I(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400345
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500346 spin_lock(&ei->i_block_reservation_lock);
Aditya Kalid8990242011-09-09 19:18:51 -0400347 trace_ext4_da_update_reserve_space(inode, used, quota_claim);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500348 if (unlikely(used > ei->i_reserved_data_blocks)) {
Theodore Ts'o8de5c322013-02-14 15:11:41 -0500349 ext4_warning(inode->i_sb, "%s: ino %lu, used %d "
Theodore Ts'o1084f252012-03-19 23:13:43 -0400350 "with only %d reserved data blocks",
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500351 __func__, inode->i_ino, used,
352 ei->i_reserved_data_blocks);
353 WARN_ON(1);
354 used = ei->i_reserved_data_blocks;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400355 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400356
Brian Foster97795d22012-07-22 23:59:40 -0400357 if (unlikely(ei->i_allocated_meta_blocks > ei->i_reserved_meta_blocks)) {
Theodore Ts'o01a523e2013-02-14 15:51:58 -0500358 ext4_warning(inode->i_sb, "ino %lu, allocated %d "
359 "with only %d reserved metadata blocks "
360 "(releasing %d blocks with reserved %d data blocks)",
361 inode->i_ino, ei->i_allocated_meta_blocks,
362 ei->i_reserved_meta_blocks, used,
363 ei->i_reserved_data_blocks);
Brian Foster97795d22012-07-22 23:59:40 -0400364 WARN_ON(1);
365 ei->i_allocated_meta_blocks = ei->i_reserved_meta_blocks;
366 }
367
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500368 /* Update per-inode reservations */
369 ei->i_reserved_data_blocks -= used;
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500370 ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks;
Theodore Ts'o57042652011-09-09 18:56:51 -0400371 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400372 used + ei->i_allocated_meta_blocks);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500373 ei->i_allocated_meta_blocks = 0;
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500374
375 if (ei->i_reserved_data_blocks == 0) {
376 /*
377 * We can release all of the reserved metadata blocks
378 * only when we have written all of the delayed
379 * allocation blocks.
380 */
Theodore Ts'o57042652011-09-09 18:56:51 -0400381 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400382 ei->i_reserved_meta_blocks);
Theodore Ts'oee5f4d92010-01-01 02:36:15 -0500383 ei->i_reserved_meta_blocks = 0;
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500384 ei->i_da_metadata_calc_len = 0;
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500385 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400386 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +0100387
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400388 /* Update quota subsystem for data blocks */
389 if (quota_claim)
Aditya Kali7b415bf2011-09-09 19:04:51 -0400390 dquot_claim_block(inode, EXT4_C2B(sbi, used));
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400391 else {
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500392 /*
393 * We did fallocate with an offset that is already delayed
394 * allocated. So on delayed allocated writeback we should
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400395 * not re-claim the quota for fallocated blocks.
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500396 */
Aditya Kali7b415bf2011-09-09 19:04:51 -0400397 dquot_release_reservation_block(inode, EXT4_C2B(sbi, used));
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500398 }
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -0400399
400 /*
401 * If we have done all the pending block allocations and if
402 * there aren't any writers on the inode, we can discard the
403 * inode's preallocations.
404 */
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500405 if ((ei->i_reserved_data_blocks == 0) &&
406 (atomic_read(&inode->i_writecount) == 0))
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -0400407 ext4_discard_preallocations(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400408}
409
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400410static int __check_block_validity(struct inode *inode, const char *func,
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400411 unsigned int line,
412 struct ext4_map_blocks *map)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400413{
Theodore Ts'o24676da2010-05-16 21:00:00 -0400414 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
415 map->m_len)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400416 ext4_error_inode(inode, func, line, map->m_pblk,
417 "lblock %lu mapped to illegal pblock "
418 "(length %d)", (unsigned long) map->m_lblk,
419 map->m_len);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400420 return -EIO;
421 }
422 return 0;
423}
424
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400425#define check_block_validity(inode, map) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400426 __check_block_validity((inode), __func__, __LINE__, (map))
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400427
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400428#ifdef ES_AGGRESSIVE_TEST
429static void ext4_map_blocks_es_recheck(handle_t *handle,
430 struct inode *inode,
431 struct ext4_map_blocks *es_map,
432 struct ext4_map_blocks *map,
433 int flags)
434{
435 int retval;
436
437 map->m_flags = 0;
438 /*
439 * There is a race window that the result is not the same.
440 * e.g. xfstests #223 when dioread_nolock enables. The reason
441 * is that we lookup a block mapping in extent status tree with
442 * out taking i_data_sem. So at the time the unwritten extent
443 * could be converted.
444 */
445 if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
446 down_read((&EXT4_I(inode)->i_data_sem));
447 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
448 retval = ext4_ext_map_blocks(handle, inode, map, flags &
449 EXT4_GET_BLOCKS_KEEP_SIZE);
450 } else {
451 retval = ext4_ind_map_blocks(handle, inode, map, flags &
452 EXT4_GET_BLOCKS_KEEP_SIZE);
453 }
454 if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
455 up_read((&EXT4_I(inode)->i_data_sem));
456 /*
457 * Clear EXT4_MAP_FROM_CLUSTER and EXT4_MAP_BOUNDARY flag
458 * because it shouldn't be marked in es_map->m_flags.
459 */
460 map->m_flags &= ~(EXT4_MAP_FROM_CLUSTER | EXT4_MAP_BOUNDARY);
461
462 /*
463 * We don't check m_len because extent will be collpased in status
464 * tree. So the m_len might not equal.
465 */
466 if (es_map->m_lblk != map->m_lblk ||
467 es_map->m_flags != map->m_flags ||
468 es_map->m_pblk != map->m_pblk) {
Theodore Ts'obdafe422013-07-13 00:40:31 -0400469 printk("ES cache assertion failed for inode: %lu "
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400470 "es_cached ex [%d/%d/%llu/%x] != "
471 "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
472 inode->i_ino, es_map->m_lblk, es_map->m_len,
473 es_map->m_pblk, es_map->m_flags, map->m_lblk,
474 map->m_len, map->m_pblk, map->m_flags,
475 retval, flags);
476 }
477}
478#endif /* ES_AGGRESSIVE_TEST */
479
Theodore Ts'o55138e02009-09-29 13:31:31 -0400480/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400481 * The ext4_map_blocks() function tries to look up the requested blocks,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400482 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500483 *
Mingming Caof5ab0d12008-02-25 15:29:55 -0500484 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
485 * and store the allocated blocks in the result buffer head and mark it
486 * mapped.
487 *
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400488 * If file type is extents based, it will call ext4_ext_map_blocks(),
489 * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
Mingming Caof5ab0d12008-02-25 15:29:55 -0500490 * based files
491 *
492 * On success, it returns the number of blocks being mapped or allocate.
493 * if create==0 and the blocks are pre-allocated and uninitialized block,
494 * the result buffer head is unmapped. If the create ==1, it will make sure
495 * the buffer head is mapped.
496 *
497 * It returns 0 if plain look up failed (blocks have not been allocated), in
Tao Madf3ab172011-10-08 15:53:49 -0400498 * that case, buffer head is unmapped
Mingming Caof5ab0d12008-02-25 15:29:55 -0500499 *
500 * It returns the error in case of allocation failure.
501 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400502int ext4_map_blocks(handle_t *handle, struct inode *inode,
503 struct ext4_map_blocks *map, int flags)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500504{
Zheng Liud100eef2013-02-18 00:29:59 -0500505 struct extent_status es;
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500506 int retval;
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400507#ifdef ES_AGGRESSIVE_TEST
508 struct ext4_map_blocks orig_map;
509
510 memcpy(&orig_map, map, sizeof(*map));
511#endif
Mingming Caof5ab0d12008-02-25 15:29:55 -0500512
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400513 map->m_flags = 0;
514 ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
515 "logical block %lu\n", inode->i_ino, flags, map->m_len,
516 (unsigned long) map->m_lblk);
Zheng Liud100eef2013-02-18 00:29:59 -0500517
518 /* Lookup extent status tree firstly */
519 if (ext4_es_lookup_extent(inode, map->m_lblk, &es)) {
Theodore Ts'o63b99962013-07-16 10:28:47 -0400520 ext4_es_lru_add(inode);
Zheng Liud100eef2013-02-18 00:29:59 -0500521 if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
522 map->m_pblk = ext4_es_pblock(&es) +
523 map->m_lblk - es.es_lblk;
524 map->m_flags |= ext4_es_is_written(&es) ?
525 EXT4_MAP_MAPPED : EXT4_MAP_UNWRITTEN;
526 retval = es.es_len - (map->m_lblk - es.es_lblk);
527 if (retval > map->m_len)
528 retval = map->m_len;
529 map->m_len = retval;
530 } else if (ext4_es_is_delayed(&es) || ext4_es_is_hole(&es)) {
531 retval = 0;
532 } else {
533 BUG_ON(1);
534 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400535#ifdef ES_AGGRESSIVE_TEST
536 ext4_map_blocks_es_recheck(handle, inode, map,
537 &orig_map, flags);
538#endif
Zheng Liud100eef2013-02-18 00:29:59 -0500539 goto found;
540 }
541
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500542 /*
Theodore Ts'ob920c752009-05-14 00:54:29 -0400543 * Try to see if we can get the block without requesting a new
544 * file system block.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500545 */
Zheng Liu729f52c2012-07-09 16:29:29 -0400546 if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
547 down_read((&EXT4_I(inode)->i_data_sem));
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400548 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Dmitry Monakhova4e5d882011-10-25 08:15:12 -0400549 retval = ext4_ext_map_blocks(handle, inode, map, flags &
550 EXT4_GET_BLOCKS_KEEP_SIZE);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500551 } else {
Dmitry Monakhova4e5d882011-10-25 08:15:12 -0400552 retval = ext4_ind_map_blocks(handle, inode, map, flags &
553 EXT4_GET_BLOCKS_KEEP_SIZE);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500554 }
Zheng Liuf7fec032013-02-18 00:28:47 -0500555 if (retval > 0) {
556 int ret;
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400557 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -0500558
Zheng Liu44fb851d2013-07-29 12:51:42 -0400559 if (unlikely(retval != map->m_len)) {
560 ext4_warning(inode->i_sb,
561 "ES len assertion failed for inode "
562 "%lu: retval %d != map->m_len %d",
563 inode->i_ino, retval, map->m_len);
564 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400565 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400566
Zheng Liuf7fec032013-02-18 00:28:47 -0500567 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
568 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
569 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
570 ext4_find_delalloc_range(inode, map->m_lblk,
571 map->m_lblk + map->m_len - 1))
572 status |= EXTENT_STATUS_DELAYED;
573 ret = ext4_es_insert_extent(inode, map->m_lblk,
574 map->m_len, map->m_pblk, status);
575 if (ret < 0)
576 retval = ret;
577 }
Zheng Liu729f52c2012-07-09 16:29:29 -0400578 if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
579 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -0500580
Zheng Liud100eef2013-02-18 00:29:59 -0500581found:
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400582 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Zheng Liuf7fec032013-02-18 00:28:47 -0500583 int ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400584 if (ret != 0)
585 return ret;
586 }
587
Mingming Caof5ab0d12008-02-25 15:29:55 -0500588 /* If it is only a block(s) look up */
Theodore Ts'oc2177052009-05-14 00:58:52 -0400589 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500590 return retval;
591
592 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -0500593 * Returns if the blocks have already allocated
594 *
595 * Note that if blocks have been preallocated
Tao Madf3ab172011-10-08 15:53:49 -0400596 * ext4_ext_get_block() returns the create = 0
Mingming Caof5ab0d12008-02-25 15:29:55 -0500597 * with buffer head unmapped.
598 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400599 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
Mingming Caof5ab0d12008-02-25 15:29:55 -0500600 return retval;
601
602 /*
Zheng Liua25a4e12013-02-18 00:28:04 -0500603 * Here we clear m_flags because after allocating an new extent,
604 * it will be set again.
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -0400605 */
Zheng Liua25a4e12013-02-18 00:28:04 -0500606 map->m_flags &= ~EXT4_MAP_FLAGS;
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -0400607
608 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -0500609 * New blocks allocate and/or writing to uninitialized extent
610 * will possibly result in updating i_data, so we take
611 * the write lock of i_data_sem, and call get_blocks()
612 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500613 */
614 down_write((&EXT4_I(inode)->i_data_sem));
Mingming Caod2a17632008-07-14 17:52:37 -0400615
616 /*
617 * if the caller is from delayed allocation writeout path
618 * we have already reserved fs blocks for allocation
619 * let the underlying get_block() function know to
620 * avoid double accounting
621 */
Theodore Ts'oc2177052009-05-14 00:58:52 -0400622 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Theodore Ts'of2321092011-01-10 12:12:36 -0500623 ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500624 /*
625 * We need to check for EXT4 here because migrate
626 * could have changed the inode type in between
627 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400628 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400629 retval = ext4_ext_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500630 } else {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400631 retval = ext4_ind_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400632
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400633 if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400634 /*
635 * We allocated new blocks which will result in
636 * i_data's format changing. Force the migrate
637 * to fail by clearing migrate flags
638 */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500639 ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400640 }
Mingming Caod2a17632008-07-14 17:52:37 -0400641
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500642 /*
643 * Update reserved blocks/metadata blocks after successful
644 * block allocation which had been deferred till now. We don't
645 * support fallocate for non extent files. So we can update
646 * reserve space here.
647 */
648 if ((retval > 0) &&
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -0500649 (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500650 ext4_da_update_reserve_space(inode, retval, 1);
651 }
Zheng Liuf7fec032013-02-18 00:28:47 -0500652 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Theodore Ts'of2321092011-01-10 12:12:36 -0500653 ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -0400654
Zheng Liuf7fec032013-02-18 00:28:47 -0500655 if (retval > 0) {
656 int ret;
Theodore Ts'o3be78c72013-08-16 21:22:41 -0400657 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -0500658
Zheng Liu44fb851d2013-07-29 12:51:42 -0400659 if (unlikely(retval != map->m_len)) {
660 ext4_warning(inode->i_sb,
661 "ES len assertion failed for inode "
662 "%lu: retval %d != map->m_len %d",
663 inode->i_ino, retval, map->m_len);
664 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400665 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -0400666
Zheng Liuadb23552013-03-10 21:13:05 -0400667 /*
668 * If the extent has been zeroed out, we don't need to update
669 * extent status tree.
670 */
671 if ((flags & EXT4_GET_BLOCKS_PRE_IO) &&
672 ext4_es_lookup_extent(inode, map->m_lblk, &es)) {
673 if (ext4_es_is_written(&es))
674 goto has_zeroout;
675 }
Zheng Liuf7fec032013-02-18 00:28:47 -0500676 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
677 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
678 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
679 ext4_find_delalloc_range(inode, map->m_lblk,
680 map->m_lblk + map->m_len - 1))
681 status |= EXTENT_STATUS_DELAYED;
682 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
683 map->m_pblk, status);
684 if (ret < 0)
685 retval = ret;
Aditya Kali5356f262011-09-09 19:20:51 -0400686 }
687
Zheng Liuadb23552013-03-10 21:13:05 -0400688has_zeroout:
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500689 up_write((&EXT4_I(inode)->i_data_sem));
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400690 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400691 int ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400692 if (ret != 0)
693 return ret;
694 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500695 return retval;
696}
697
Mingming Caof3bd1f32008-08-19 22:16:03 -0400698/* Maximum number of blocks we map for direct IO at once. */
699#define DIO_MAX_BLOCKS 4096
700
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400701static int _ext4_get_block(struct inode *inode, sector_t iblock,
702 struct buffer_head *bh, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700703{
Dmitriy Monakhov3e4fdaf2007-02-10 01:46:35 -0800704 handle_t *handle = ext4_journal_current_handle();
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400705 struct ext4_map_blocks map;
Jan Kara7fb54092008-02-10 01:08:38 -0500706 int ret = 0, started = 0;
Mingming Caof3bd1f32008-08-19 22:16:03 -0400707 int dio_credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700708
Tao Ma46c7f252012-12-10 14:04:52 -0500709 if (ext4_has_inline_data(inode))
710 return -ERANGE;
711
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400712 map.m_lblk = iblock;
713 map.m_len = bh->b_size >> inode->i_blkbits;
714
Anatol Pomozov8b0f1652012-11-08 15:07:16 -0500715 if (flags && !(flags & EXT4_GET_BLOCKS_NO_LOCK) && !handle) {
Jan Kara7fb54092008-02-10 01:08:38 -0500716 /* Direct IO write... */
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400717 if (map.m_len > DIO_MAX_BLOCKS)
718 map.m_len = DIO_MAX_BLOCKS;
719 dio_credits = ext4_chunk_trans_blocks(inode, map.m_len);
Theodore Ts'o9924a922013-02-08 21:59:22 -0500720 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
721 dio_credits);
Jan Kara7fb54092008-02-10 01:08:38 -0500722 if (IS_ERR(handle)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700723 ret = PTR_ERR(handle);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400724 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700725 }
Jan Kara7fb54092008-02-10 01:08:38 -0500726 started = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700727 }
728
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400729 ret = ext4_map_blocks(handle, inode, &map, flags);
Jan Kara7fb54092008-02-10 01:08:38 -0500730 if (ret > 0) {
Christoph Hellwig7b7a8662013-09-04 15:04:39 +0200731 ext4_io_end_t *io_end = ext4_inode_aio(inode);
732
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400733 map_bh(bh, inode->i_sb, map.m_pblk);
734 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
Christoph Hellwig7b7a8662013-09-04 15:04:39 +0200735 if (io_end && io_end->flag & EXT4_IO_END_UNWRITTEN)
736 set_buffer_defer_completion(bh);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400737 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
Jan Kara7fb54092008-02-10 01:08:38 -0500738 ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700739 }
Jan Kara7fb54092008-02-10 01:08:38 -0500740 if (started)
741 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700742 return ret;
743}
744
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400745int ext4_get_block(struct inode *inode, sector_t iblock,
746 struct buffer_head *bh, int create)
747{
748 return _ext4_get_block(inode, iblock, bh,
749 create ? EXT4_GET_BLOCKS_CREATE : 0);
750}
751
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700752/*
753 * `handle' can be NULL if create is zero
754 */
Mingming Cao617ba132006-10-11 01:20:53 -0700755struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500756 ext4_lblk_t block, int create, int *errp)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700757{
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400758 struct ext4_map_blocks map;
759 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700760 int fatal = 0, err;
761
762 J_ASSERT(handle != NULL || create == 0);
763
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400764 map.m_lblk = block;
765 map.m_len = 1;
766 err = ext4_map_blocks(handle, inode, &map,
767 create ? EXT4_GET_BLOCKS_CREATE : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700768
Carlos Maiolino90b0a972012-09-17 23:39:12 -0400769 /* ensure we send some value back into *errp */
770 *errp = 0;
771
Theodore Ts'o0f70b402013-02-15 03:35:57 -0500772 if (create && err == 0)
773 err = -ENOSPC; /* should never happen */
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400774 if (err < 0)
775 *errp = err;
776 if (err <= 0)
777 return NULL;
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400778
779 bh = sb_getblk(inode->i_sb, map.m_pblk);
Wang Shilongaebf0242013-01-12 16:28:47 -0500780 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500781 *errp = -ENOMEM;
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400782 return NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700783 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400784 if (map.m_flags & EXT4_MAP_NEW) {
785 J_ASSERT(create != 0);
786 J_ASSERT(handle != NULL);
787
788 /*
789 * Now that we do not always journal data, we should
790 * keep in mind whether this should always journal the
791 * new buffer as metadata. For now, regular file
792 * writes use ext4_get_block instead, so it's not a
793 * problem.
794 */
795 lock_buffer(bh);
796 BUFFER_TRACE(bh, "call get_create_access");
797 fatal = ext4_journal_get_create_access(handle, bh);
798 if (!fatal && !buffer_uptodate(bh)) {
799 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
800 set_buffer_uptodate(bh);
801 }
802 unlock_buffer(bh);
803 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
804 err = ext4_handle_dirty_metadata(handle, inode, bh);
805 if (!fatal)
806 fatal = err;
807 } else {
808 BUFFER_TRACE(bh, "not a new buffer");
809 }
810 if (fatal) {
811 *errp = fatal;
812 brelse(bh);
813 bh = NULL;
814 }
815 return bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700816}
817
Mingming Cao617ba132006-10-11 01:20:53 -0700818struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500819 ext4_lblk_t block, int create, int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700820{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400821 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700822
Mingming Cao617ba132006-10-11 01:20:53 -0700823 bh = ext4_getblk(handle, inode, block, create, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700824 if (!bh)
825 return bh;
826 if (buffer_uptodate(bh))
827 return bh;
Christoph Hellwig65299a32011-08-23 14:50:29 +0200828 ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700829 wait_on_buffer(bh);
830 if (buffer_uptodate(bh))
831 return bh;
832 put_bh(bh);
833 *err = -EIO;
834 return NULL;
835}
836
Tao Maf19d5872012-12-10 14:05:51 -0500837int ext4_walk_page_buffers(handle_t *handle,
838 struct buffer_head *head,
839 unsigned from,
840 unsigned to,
841 int *partial,
842 int (*fn)(handle_t *handle,
843 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700844{
845 struct buffer_head *bh;
846 unsigned block_start, block_end;
847 unsigned blocksize = head->b_size;
848 int err, ret = 0;
849 struct buffer_head *next;
850
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400851 for (bh = head, block_start = 0;
852 ret == 0 && (bh != head || !block_start);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400853 block_start = block_end, bh = next) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700854 next = bh->b_this_page;
855 block_end = block_start + blocksize;
856 if (block_end <= from || block_start >= to) {
857 if (partial && !buffer_uptodate(bh))
858 *partial = 1;
859 continue;
860 }
861 err = (*fn)(handle, bh);
862 if (!ret)
863 ret = err;
864 }
865 return ret;
866}
867
868/*
869 * To preserve ordering, it is essential that the hole instantiation and
870 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -0700871 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -0700872 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700873 * prepare_write() is the right place.
874 *
Jan Kara36ade452013-01-28 09:30:52 -0500875 * Also, this function can nest inside ext4_writepage(). In that case, we
876 * *know* that ext4_writepage() has generated enough buffer credits to do the
877 * whole page. So we won't block on the journal in that case, which is good,
878 * because the caller may be PF_MEMALLOC.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700879 *
Mingming Cao617ba132006-10-11 01:20:53 -0700880 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700881 * quota file writes. If we were to commit the transaction while thus
882 * reentered, there can be a deadlock - we would be holding a quota
883 * lock, and the commit would never complete if another thread had a
884 * transaction open and was blocking on the quota lock - a ranking
885 * violation.
886 *
Mingming Caodab291a2006-10-11 01:21:01 -0700887 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700888 * will _not_ run commit under these circumstances because handle->h_ref
889 * is elevated. We'll still have enough credits for the tiny quotafile
890 * write.
891 */
Tao Maf19d5872012-12-10 14:05:51 -0500892int do_journal_get_write_access(handle_t *handle,
893 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700894{
Jan Kara56d35a42010-08-05 14:41:42 -0400895 int dirty = buffer_dirty(bh);
896 int ret;
897
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700898 if (!buffer_mapped(bh) || buffer_freed(bh))
899 return 0;
Jan Kara56d35a42010-08-05 14:41:42 -0400900 /*
Christoph Hellwigebdec242010-10-06 10:47:23 +0200901 * __block_write_begin() could have dirtied some buffers. Clean
Jan Kara56d35a42010-08-05 14:41:42 -0400902 * the dirty bit as jbd2_journal_get_write_access() could complain
903 * otherwise about fs integrity issues. Setting of the dirty bit
Christoph Hellwigebdec242010-10-06 10:47:23 +0200904 * by __block_write_begin() isn't a real problem here as we clear
Jan Kara56d35a42010-08-05 14:41:42 -0400905 * the bit before releasing a page lock and thus writeback cannot
906 * ever write the buffer.
907 */
908 if (dirty)
909 clear_buffer_dirty(bh);
910 ret = ext4_journal_get_write_access(handle, bh);
911 if (!ret && dirty)
912 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
913 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700914}
915
Anatol Pomozov8b0f1652012-11-08 15:07:16 -0500916static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock,
917 struct buffer_head *bh_result, int create);
Nick Pigginbfc1af62007-10-16 01:25:05 -0700918static int ext4_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400919 loff_t pos, unsigned len, unsigned flags,
920 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700921{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400922 struct inode *inode = mapping->host;
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -0400923 int ret, needed_blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700924 handle_t *handle;
925 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400926 struct page *page;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400927 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400928 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -0700929
Theodore Ts'o9bffad12009-06-17 11:48:11 -0400930 trace_ext4_write_begin(inode, pos, len, flags);
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -0400931 /*
932 * Reserve one block more for addition to orphan list in case
933 * we allocate blocks but write fails for some reason
934 */
935 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400936 index = pos >> PAGE_CACHE_SHIFT;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400937 from = pos & (PAGE_CACHE_SIZE - 1);
938 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700939
Tao Maf19d5872012-12-10 14:05:51 -0500940 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
941 ret = ext4_try_to_write_inline_data(mapping, inode, pos, len,
942 flags, pagep);
943 if (ret < 0)
Theodore Ts'o47564bf2013-02-09 09:24:14 -0500944 return ret;
945 if (ret == 1)
946 return 0;
Tao Maf19d5872012-12-10 14:05:51 -0500947 }
948
Theodore Ts'o47564bf2013-02-09 09:24:14 -0500949 /*
950 * grab_cache_page_write_begin() can take a long time if the
951 * system is thrashing due to memory pressure, or if the page
952 * is being written back. So grab it first before we start
953 * the transaction handle. This also allows us to allocate
954 * the page (if needed) without using GFP_NOFS.
955 */
956retry_grab:
Nick Piggin54566b22009-01-04 12:00:53 -0800957 page = grab_cache_page_write_begin(mapping, index, flags);
Theodore Ts'o47564bf2013-02-09 09:24:14 -0500958 if (!page)
959 return -ENOMEM;
960 unlock_page(page);
961
962retry_journal:
Theodore Ts'o9924a922013-02-08 21:59:22 -0500963 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700964 if (IS_ERR(handle)) {
Theodore Ts'o47564bf2013-02-09 09:24:14 -0500965 page_cache_release(page);
966 return PTR_ERR(handle);
Jan Karacf108bc2008-07-11 19:27:31 -0400967 }
Tao Maf19d5872012-12-10 14:05:51 -0500968
Theodore Ts'o47564bf2013-02-09 09:24:14 -0500969 lock_page(page);
970 if (page->mapping != mapping) {
971 /* The page got truncated from under us */
972 unlock_page(page);
973 page_cache_release(page);
Jan Karacf108bc2008-07-11 19:27:31 -0400974 ext4_journal_stop(handle);
Theodore Ts'o47564bf2013-02-09 09:24:14 -0500975 goto retry_grab;
Jan Karacf108bc2008-07-11 19:27:31 -0400976 }
Dmitry Monakhov7afe5aa2013-08-28 14:30:47 -0400977 /* In case writeback began while the page was unlocked */
978 wait_for_stable_page(page);
Jan Karacf108bc2008-07-11 19:27:31 -0400979
Jiaying Zhang744692d2010-03-04 16:14:02 -0500980 if (ext4_should_dioread_nolock(inode))
Christoph Hellwig6e1db882010-06-04 11:29:57 +0200981 ret = __block_write_begin(page, pos, len, ext4_get_block_write);
Jiaying Zhang744692d2010-03-04 16:14:02 -0500982 else
Christoph Hellwig6e1db882010-06-04 11:29:57 +0200983 ret = __block_write_begin(page, pos, len, ext4_get_block);
Nick Pigginbfc1af62007-10-16 01:25:05 -0700984
985 if (!ret && ext4_should_journal_data(inode)) {
Tao Maf19d5872012-12-10 14:05:51 -0500986 ret = ext4_walk_page_buffers(handle, page_buffers(page),
987 from, to, NULL,
988 do_journal_get_write_access);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700989 }
Nick Pigginbfc1af62007-10-16 01:25:05 -0700990
991 if (ret) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400992 unlock_page(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -0400993 /*
Christoph Hellwig6e1db882010-06-04 11:29:57 +0200994 * __block_write_begin may have instantiated a few blocks
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -0400995 * outside i_size. Trim these off again. Don't need
996 * i_size_read because we hold i_mutex.
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -0400997 *
998 * Add inode to orphan list in case we crash before
999 * truncate finishes
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04001000 */
Jan Karaffacfa72009-07-13 16:22:22 -04001001 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001002 ext4_orphan_add(handle, inode);
1003
1004 ext4_journal_stop(handle);
1005 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001006 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001007 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001008 * If truncate failed early the inode might
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -04001009 * still be on the orphan list; we need to
1010 * make sure the inode is removed from the
1011 * orphan list in that case.
1012 */
1013 if (inode->i_nlink)
1014 ext4_orphan_del(NULL, inode);
1015 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001016
Theodore Ts'o47564bf2013-02-09 09:24:14 -05001017 if (ret == -ENOSPC &&
1018 ext4_should_retry_alloc(inode->i_sb, &retries))
1019 goto retry_journal;
1020 page_cache_release(page);
1021 return ret;
1022 }
1023 *pagep = page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001024 return ret;
1025}
1026
Nick Pigginbfc1af62007-10-16 01:25:05 -07001027/* For write_end() in data=journal mode */
1028static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001029{
Theodore Ts'o13fca322013-04-21 16:45:54 -04001030 int ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001031 if (!buffer_mapped(bh) || buffer_freed(bh))
1032 return 0;
1033 set_buffer_uptodate(bh);
Theodore Ts'o13fca322013-04-21 16:45:54 -04001034 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
1035 clear_buffer_meta(bh);
1036 clear_buffer_prio(bh);
1037 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001038}
1039
Zheng Liueed43332013-04-03 12:41:17 -04001040/*
1041 * We need to pick up the new inode size which generic_commit_write gave us
1042 * `file' can be NULL - eg, when called from page_symlink().
1043 *
1044 * ext4 never places buffers on inode->i_mapping->private_list. metadata
1045 * buffers are managed internally.
1046 */
1047static int ext4_write_end(struct file *file,
1048 struct address_space *mapping,
1049 loff_t pos, unsigned len, unsigned copied,
1050 struct page *page, void *fsdata)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001051{
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001052 handle_t *handle = ext4_journal_current_handle();
Zheng Liueed43332013-04-03 12:41:17 -04001053 struct inode *inode = mapping->host;
1054 int ret = 0, ret2;
1055 int i_size_changed = 0;
1056
1057 trace_ext4_write_end(inode, pos, len, copied);
1058 if (ext4_test_inode_state(inode, EXT4_STATE_ORDERED_MODE)) {
1059 ret = ext4_jbd2_file_inode(handle, inode);
1060 if (ret) {
1061 unlock_page(page);
1062 page_cache_release(page);
1063 goto errout;
1064 }
1065 }
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001066
Theodore Ts'o42c832d2013-07-01 08:12:39 -04001067 if (ext4_has_inline_data(inode)) {
1068 ret = ext4_write_inline_data_end(inode, pos, len,
1069 copied, page);
1070 if (ret < 0)
1071 goto errout;
1072 copied = ret;
1073 } else
Tao Maf19d5872012-12-10 14:05:51 -05001074 copied = block_write_end(file, mapping, pos,
1075 len, copied, page, fsdata);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001076
1077 /*
1078 * No need to use i_size_read() here, the i_size
Zheng Liueed43332013-04-03 12:41:17 -04001079 * cannot change under us because we hole i_mutex.
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001080 *
1081 * But it's important to update i_size while still holding page lock:
1082 * page writeout could otherwise come in and zero beyond i_size.
1083 */
1084 if (pos + copied > inode->i_size) {
1085 i_size_write(inode, pos + copied);
1086 i_size_changed = 1;
1087 }
1088
Zheng Liueed43332013-04-03 12:41:17 -04001089 if (pos + copied > EXT4_I(inode)->i_disksize) {
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001090 /* We need to mark inode dirty even if
1091 * new_i_size is less that inode->i_size
Zheng Liueed43332013-04-03 12:41:17 -04001092 * but greater than i_disksize. (hint delalloc)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001093 */
1094 ext4_update_i_disksize(inode, (pos + copied));
1095 i_size_changed = 1;
1096 }
1097 unlock_page(page);
1098 page_cache_release(page);
1099
1100 /*
1101 * Don't mark the inode dirty under page lock. First, it unnecessarily
1102 * makes the holding time of page lock longer. Second, it forces lock
1103 * ordering of page lock and transaction start for journaling
1104 * filesystems.
1105 */
1106 if (i_size_changed)
1107 ext4_mark_inode_dirty(handle, inode);
1108
Jan Karaffacfa72009-07-13 16:22:22 -04001109 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001110 /* if we have allocated more blocks and copied
1111 * less. We will have blocks allocated outside
1112 * inode->i_size. So truncate them
1113 */
1114 ext4_orphan_add(handle, inode);
Theodore Ts'o74d553a2013-04-03 12:39:17 -04001115errout:
Mingming Cao617ba132006-10-11 01:20:53 -07001116 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001117 if (!ret)
1118 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001119
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001120 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001121 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001122 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001123 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001124 * on the orphan list; we need to make sure the inode
1125 * is removed from the orphan list in that case.
1126 */
1127 if (inode->i_nlink)
1128 ext4_orphan_del(NULL, inode);
1129 }
1130
Nick Pigginbfc1af62007-10-16 01:25:05 -07001131 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001132}
1133
Nick Pigginbfc1af62007-10-16 01:25:05 -07001134static int ext4_journalled_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001135 struct address_space *mapping,
1136 loff_t pos, unsigned len, unsigned copied,
1137 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001138{
Mingming Cao617ba132006-10-11 01:20:53 -07001139 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -07001140 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001141 int ret = 0, ret2;
1142 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001143 unsigned from, to;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001144 loff_t new_i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001145
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001146 trace_ext4_journalled_write_end(inode, pos, len, copied);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001147 from = pos & (PAGE_CACHE_SIZE - 1);
1148 to = from + len;
1149
Curt Wohlgemuth441c8502011-08-13 11:25:18 -04001150 BUG_ON(!ext4_handle_valid(handle));
1151
Tao Ma3fdcfb62012-12-10 14:05:57 -05001152 if (ext4_has_inline_data(inode))
1153 copied = ext4_write_inline_data_end(inode, pos, len,
1154 copied, page);
1155 else {
1156 if (copied < len) {
1157 if (!PageUptodate(page))
1158 copied = 0;
1159 page_zero_new_buffers(page, from+copied, to);
1160 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001161
Tao Ma3fdcfb62012-12-10 14:05:57 -05001162 ret = ext4_walk_page_buffers(handle, page_buffers(page), from,
1163 to, &partial, write_end_fn);
1164 if (!partial)
1165 SetPageUptodate(page);
1166 }
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001167 new_i_size = pos + copied;
1168 if (new_i_size > inode->i_size)
Nick Pigginbfc1af62007-10-16 01:25:05 -07001169 i_size_write(inode, pos+copied);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001170 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Jan Kara2d859db2011-07-26 09:07:11 -04001171 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001172 if (new_i_size > EXT4_I(inode)->i_disksize) {
1173 ext4_update_i_disksize(inode, new_i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001174 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001175 if (!ret)
1176 ret = ret2;
1177 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001178
Jan Karacf108bc2008-07-11 19:27:31 -04001179 unlock_page(page);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001180 page_cache_release(page);
Jan Karaffacfa72009-07-13 16:22:22 -04001181 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001182 /* if we have allocated more blocks and copied
1183 * less. We will have blocks allocated outside
1184 * inode->i_size. So truncate them
1185 */
1186 ext4_orphan_add(handle, inode);
1187
Mingming Cao617ba132006-10-11 01:20:53 -07001188 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001189 if (!ret)
1190 ret = ret2;
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001191 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001192 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001193 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001194 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001195 * on the orphan list; we need to make sure the inode
1196 * is removed from the orphan list in that case.
1197 */
1198 if (inode->i_nlink)
1199 ext4_orphan_del(NULL, inode);
1200 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001201
1202 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001203}
Mingming Caod2a17632008-07-14 17:52:37 -04001204
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001205/*
Lukas Czerner386ad672013-03-10 22:50:00 -04001206 * Reserve a metadata for a single block located at lblock
1207 */
1208static int ext4_da_reserve_metadata(struct inode *inode, ext4_lblk_t lblock)
1209{
Lukas Czerner386ad672013-03-10 22:50:00 -04001210 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1211 struct ext4_inode_info *ei = EXT4_I(inode);
1212 unsigned int md_needed;
1213 ext4_lblk_t save_last_lblock;
1214 int save_len;
1215
1216 /*
1217 * recalculate the amount of metadata blocks to reserve
1218 * in order to allocate nrblocks
1219 * worse case is one extent per block
1220 */
Lukas Czerner386ad672013-03-10 22:50:00 -04001221 spin_lock(&ei->i_block_reservation_lock);
1222 /*
1223 * ext4_calc_metadata_amount() has side effects, which we have
1224 * to be prepared undo if we fail to claim space.
1225 */
1226 save_len = ei->i_da_metadata_calc_len;
1227 save_last_lblock = ei->i_da_metadata_calc_last_lblock;
1228 md_needed = EXT4_NUM_B2C(sbi,
1229 ext4_calc_metadata_amount(inode, lblock));
1230 trace_ext4_da_reserve_space(inode, md_needed);
1231
1232 /*
1233 * We do still charge estimated metadata to the sb though;
1234 * we cannot afford to run out of free blocks.
1235 */
1236 if (ext4_claim_free_clusters(sbi, md_needed, 0)) {
1237 ei->i_da_metadata_calc_len = save_len;
1238 ei->i_da_metadata_calc_last_lblock = save_last_lblock;
1239 spin_unlock(&ei->i_block_reservation_lock);
Lukas Czerner386ad672013-03-10 22:50:00 -04001240 return -ENOSPC;
1241 }
1242 ei->i_reserved_meta_blocks += md_needed;
1243 spin_unlock(&ei->i_block_reservation_lock);
1244
1245 return 0; /* success */
1246}
1247
1248/*
Aditya Kali7b415bf2011-09-09 19:04:51 -04001249 * Reserve a single cluster located at lblock
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001250 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -05001251static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -04001252{
Mingming Cao60e58e02009-01-22 18:13:05 +01001253 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001254 struct ext4_inode_info *ei = EXT4_I(inode);
Aditya Kali7b415bf2011-09-09 19:04:51 -04001255 unsigned int md_needed;
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001256 int ret;
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001257 ext4_lblk_t save_last_lblock;
1258 int save_len;
Mingming Caod2a17632008-07-14 17:52:37 -04001259
Mingming Cao60e58e02009-01-22 18:13:05 +01001260 /*
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001261 * We will charge metadata quota at writeout time; this saves
1262 * us from metadata over-estimation, though we may go over by
1263 * a small amount in the end. Here we just reserve for data.
Mingming Cao60e58e02009-01-22 18:13:05 +01001264 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04001265 ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001266 if (ret)
1267 return ret;
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001268
1269 /*
1270 * recalculate the amount of metadata blocks to reserve
1271 * in order to allocate nrblocks
1272 * worse case is one extent per block
1273 */
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001274 spin_lock(&ei->i_block_reservation_lock);
1275 /*
1276 * ext4_calc_metadata_amount() has side effects, which we have
1277 * to be prepared undo if we fail to claim space.
1278 */
1279 save_len = ei->i_da_metadata_calc_len;
1280 save_last_lblock = ei->i_da_metadata_calc_last_lblock;
1281 md_needed = EXT4_NUM_B2C(sbi,
1282 ext4_calc_metadata_amount(inode, lblock));
1283 trace_ext4_da_reserve_space(inode, md_needed);
1284
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001285 /*
1286 * We do still charge estimated metadata to the sb though;
1287 * we cannot afford to run out of free blocks.
1288 */
Theodore Ts'oe7d5f312011-09-09 19:14:51 -04001289 if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) {
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001290 ei->i_da_metadata_calc_len = save_len;
1291 ei->i_da_metadata_calc_last_lblock = save_last_lblock;
1292 spin_unlock(&ei->i_block_reservation_lock);
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001293 dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
Mingming Caod2a17632008-07-14 17:52:37 -04001294 return -ENOSPC;
1295 }
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001296 ei->i_reserved_data_blocks++;
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001297 ei->i_reserved_meta_blocks += md_needed;
1298 spin_unlock(&ei->i_block_reservation_lock);
Dmitry Monakhov39bc6802009-12-10 16:36:27 +00001299
Mingming Caod2a17632008-07-14 17:52:37 -04001300 return 0; /* success */
1301}
1302
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001303static void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001304{
1305 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001306 struct ext4_inode_info *ei = EXT4_I(inode);
Mingming Caod2a17632008-07-14 17:52:37 -04001307
Mingming Caocd213222008-08-19 22:16:59 -04001308 if (!to_free)
1309 return; /* Nothing to release, exit */
1310
Mingming Caod2a17632008-07-14 17:52:37 -04001311 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001312
Li Zefan5a58ec82010-05-17 02:00:00 -04001313 trace_ext4_da_release_space(inode, to_free);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001314 if (unlikely(to_free > ei->i_reserved_data_blocks)) {
Mingming Caocd213222008-08-19 22:16:59 -04001315 /*
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001316 * if there aren't enough reserved blocks, then the
1317 * counter is messed up somewhere. Since this
1318 * function is called from invalidate page, it's
1319 * harmless to return without any action.
Mingming Caocd213222008-08-19 22:16:59 -04001320 */
Theodore Ts'o8de5c322013-02-14 15:11:41 -05001321 ext4_warning(inode->i_sb, "ext4_da_release_space: "
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001322 "ino %lu, to_free %d with only %d reserved "
Theodore Ts'o1084f252012-03-19 23:13:43 -04001323 "data blocks", inode->i_ino, to_free,
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001324 ei->i_reserved_data_blocks);
1325 WARN_ON(1);
1326 to_free = ei->i_reserved_data_blocks;
1327 }
1328 ei->i_reserved_data_blocks -= to_free;
1329
1330 if (ei->i_reserved_data_blocks == 0) {
1331 /*
1332 * We can release all of the reserved metadata blocks
1333 * only when we have written all of the delayed
1334 * allocation blocks.
Aditya Kali7b415bf2011-09-09 19:04:51 -04001335 * Note that in case of bigalloc, i_reserved_meta_blocks,
1336 * i_reserved_data_blocks, etc. refer to number of clusters.
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001337 */
Theodore Ts'o57042652011-09-09 18:56:51 -04001338 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001339 ei->i_reserved_meta_blocks);
Theodore Ts'oee5f4d92010-01-01 02:36:15 -05001340 ei->i_reserved_meta_blocks = 0;
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001341 ei->i_da_metadata_calc_len = 0;
Mingming Caocd213222008-08-19 22:16:59 -04001342 }
1343
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001344 /* update fs dirty data blocks counter */
Theodore Ts'o57042652011-09-09 18:56:51 -04001345 percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free);
Mingming Caod2a17632008-07-14 17:52:37 -04001346
Mingming Caod2a17632008-07-14 17:52:37 -04001347 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001348
Aditya Kali7b415bf2011-09-09 19:04:51 -04001349 dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free));
Mingming Caod2a17632008-07-14 17:52:37 -04001350}
1351
1352static void ext4_da_page_release_reservation(struct page *page,
Lukas Czernerca99fdd2013-05-21 23:25:01 -04001353 unsigned int offset,
1354 unsigned int length)
Mingming Caod2a17632008-07-14 17:52:37 -04001355{
1356 int to_release = 0;
1357 struct buffer_head *head, *bh;
1358 unsigned int curr_off = 0;
Aditya Kali7b415bf2011-09-09 19:04:51 -04001359 struct inode *inode = page->mapping->host;
1360 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Lukas Czernerca99fdd2013-05-21 23:25:01 -04001361 unsigned int stop = offset + length;
Aditya Kali7b415bf2011-09-09 19:04:51 -04001362 int num_clusters;
Zheng Liu51865fd2012-11-08 21:57:32 -05001363 ext4_fsblk_t lblk;
Mingming Caod2a17632008-07-14 17:52:37 -04001364
Lukas Czernerca99fdd2013-05-21 23:25:01 -04001365 BUG_ON(stop > PAGE_CACHE_SIZE || stop < length);
1366
Mingming Caod2a17632008-07-14 17:52:37 -04001367 head = page_buffers(page);
1368 bh = head;
1369 do {
1370 unsigned int next_off = curr_off + bh->b_size;
1371
Lukas Czernerca99fdd2013-05-21 23:25:01 -04001372 if (next_off > stop)
1373 break;
1374
Mingming Caod2a17632008-07-14 17:52:37 -04001375 if ((offset <= curr_off) && (buffer_delay(bh))) {
1376 to_release++;
1377 clear_buffer_delay(bh);
1378 }
1379 curr_off = next_off;
1380 } while ((bh = bh->b_this_page) != head);
Aditya Kali7b415bf2011-09-09 19:04:51 -04001381
Zheng Liu51865fd2012-11-08 21:57:32 -05001382 if (to_release) {
1383 lblk = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1384 ext4_es_remove_extent(inode, lblk, to_release);
1385 }
1386
Aditya Kali7b415bf2011-09-09 19:04:51 -04001387 /* If we have released all the blocks belonging to a cluster, then we
1388 * need to release the reserved space for that cluster. */
1389 num_clusters = EXT4_NUM_B2C(sbi, to_release);
1390 while (num_clusters > 0) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04001391 lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) +
1392 ((num_clusters - 1) << sbi->s_cluster_bits);
1393 if (sbi->s_cluster_ratio == 1 ||
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05001394 !ext4_find_delalloc_cluster(inode, lblk))
Aditya Kali7b415bf2011-09-09 19:04:51 -04001395 ext4_da_release_space(inode, 1);
1396
1397 num_clusters--;
1398 }
Mingming Caod2a17632008-07-14 17:52:37 -04001399}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001400
1401/*
Alex Tomas64769242008-07-11 19:27:31 -04001402 * Delayed allocation stuff
1403 */
1404
Jan Kara4e7ea812013-06-04 13:17:40 -04001405struct mpage_da_data {
1406 struct inode *inode;
1407 struct writeback_control *wbc;
Jan Kara6b523df2013-06-04 13:21:11 -04001408
Jan Kara4e7ea812013-06-04 13:17:40 -04001409 pgoff_t first_page; /* The first page to write */
1410 pgoff_t next_page; /* Current page to examine */
1411 pgoff_t last_page; /* Last page to examine */
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001412 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04001413 * Extent to map - this can be after first_page because that can be
1414 * fully mapped. We somewhat abuse m_flags to store whether the extent
1415 * is delalloc or unwritten.
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001416 */
Jan Kara4e7ea812013-06-04 13:17:40 -04001417 struct ext4_map_blocks map;
1418 struct ext4_io_submit io_submit; /* IO submission data */
1419};
Alex Tomas64769242008-07-11 19:27:31 -04001420
Jan Kara4e7ea812013-06-04 13:17:40 -04001421static void mpage_release_unused_pages(struct mpage_da_data *mpd,
1422 bool invalidate)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001423{
1424 int nr_pages, i;
1425 pgoff_t index, end;
1426 struct pagevec pvec;
1427 struct inode *inode = mpd->inode;
1428 struct address_space *mapping = inode->i_mapping;
Jan Kara4e7ea812013-06-04 13:17:40 -04001429
1430 /* This is necessary when next_page == 0. */
1431 if (mpd->first_page >= mpd->next_page)
1432 return;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001433
Curt Wohlgemuthc7f59382011-02-26 12:27:52 -05001434 index = mpd->first_page;
1435 end = mpd->next_page - 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04001436 if (invalidate) {
1437 ext4_lblk_t start, last;
1438 start = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1439 last = end << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1440 ext4_es_remove_extent(inode, start, last - start + 1);
1441 }
Zheng Liu51865fd2012-11-08 21:57:32 -05001442
Eric Sandeen66bea922012-11-14 22:22:05 -05001443 pagevec_init(&pvec, 0);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001444 while (index <= end) {
1445 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1446 if (nr_pages == 0)
1447 break;
1448 for (i = 0; i < nr_pages; i++) {
1449 struct page *page = pvec.pages[i];
Jan Kara9b1d09982010-03-03 16:19:32 -05001450 if (page->index > end)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001451 break;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001452 BUG_ON(!PageLocked(page));
1453 BUG_ON(PageWriteback(page));
Jan Kara4e7ea812013-06-04 13:17:40 -04001454 if (invalidate) {
1455 block_invalidatepage(page, 0, PAGE_CACHE_SIZE);
1456 ClearPageUptodate(page);
1457 }
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001458 unlock_page(page);
1459 }
Jan Kara9b1d09982010-03-03 16:19:32 -05001460 index = pvec.pages[nr_pages - 1]->index + 1;
1461 pagevec_release(&pvec);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001462 }
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001463}
1464
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001465static void ext4_print_free_blocks(struct inode *inode)
1466{
1467 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o92b97812012-03-19 23:41:49 -04001468 struct super_block *sb = inode->i_sb;
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001469 struct ext4_inode_info *ei = EXT4_I(inode);
Theodore Ts'o92b97812012-03-19 23:41:49 -04001470
1471 ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld",
Theodore Ts'o5dee5432011-09-09 19:10:51 -04001472 EXT4_C2B(EXT4_SB(inode->i_sb),
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001473 ext4_count_free_clusters(sb)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001474 ext4_msg(sb, KERN_CRIT, "Free/Dirty block details");
1475 ext4_msg(sb, KERN_CRIT, "free_blocks=%lld",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001476 (long long) EXT4_C2B(EXT4_SB(sb),
Theodore Ts'o57042652011-09-09 18:56:51 -04001477 percpu_counter_sum(&sbi->s_freeclusters_counter)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001478 ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001479 (long long) EXT4_C2B(EXT4_SB(sb),
Aditya Kali7b415bf2011-09-09 19:04:51 -04001480 percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001481 ext4_msg(sb, KERN_CRIT, "Block reservation details");
1482 ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001483 ei->i_reserved_data_blocks);
Theodore Ts'o92b97812012-03-19 23:41:49 -04001484 ext4_msg(sb, KERN_CRIT, "i_reserved_meta_blocks=%u",
Lukas Czernerf78ee70d2013-04-03 23:33:30 -04001485 ei->i_reserved_meta_blocks);
1486 ext4_msg(sb, KERN_CRIT, "i_allocated_meta_blocks=%u",
1487 ei->i_allocated_meta_blocks);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001488 return;
1489}
1490
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001491static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001492{
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001493 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001494}
1495
Alex Tomas64769242008-07-11 19:27:31 -04001496/*
Aditya Kali5356f262011-09-09 19:20:51 -04001497 * This function is grabs code from the very beginning of
1498 * ext4_map_blocks, but assumes that the caller is from delayed write
1499 * time. This function looks up the requested blocks and sets the
1500 * buffer delay bit under the protection of i_data_sem.
1501 */
1502static int ext4_da_map_blocks(struct inode *inode, sector_t iblock,
1503 struct ext4_map_blocks *map,
1504 struct buffer_head *bh)
1505{
Zheng Liud100eef2013-02-18 00:29:59 -05001506 struct extent_status es;
Aditya Kali5356f262011-09-09 19:20:51 -04001507 int retval;
1508 sector_t invalid_block = ~((sector_t) 0xffff);
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001509#ifdef ES_AGGRESSIVE_TEST
1510 struct ext4_map_blocks orig_map;
1511
1512 memcpy(&orig_map, map, sizeof(*map));
1513#endif
Aditya Kali5356f262011-09-09 19:20:51 -04001514
1515 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
1516 invalid_block = ~0;
1517
1518 map->m_flags = 0;
1519 ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
1520 "logical block %lu\n", inode->i_ino, map->m_len,
1521 (unsigned long) map->m_lblk);
Zheng Liud100eef2013-02-18 00:29:59 -05001522
1523 /* Lookup extent status tree firstly */
1524 if (ext4_es_lookup_extent(inode, iblock, &es)) {
Theodore Ts'o63b99962013-07-16 10:28:47 -04001525 ext4_es_lru_add(inode);
Zheng Liud100eef2013-02-18 00:29:59 -05001526 if (ext4_es_is_hole(&es)) {
1527 retval = 0;
1528 down_read((&EXT4_I(inode)->i_data_sem));
1529 goto add_delayed;
1530 }
1531
1532 /*
1533 * Delayed extent could be allocated by fallocate.
1534 * So we need to check it.
1535 */
1536 if (ext4_es_is_delayed(&es) && !ext4_es_is_unwritten(&es)) {
1537 map_bh(bh, inode->i_sb, invalid_block);
1538 set_buffer_new(bh);
1539 set_buffer_delay(bh);
1540 return 0;
1541 }
1542
1543 map->m_pblk = ext4_es_pblock(&es) + iblock - es.es_lblk;
1544 retval = es.es_len - (iblock - es.es_lblk);
1545 if (retval > map->m_len)
1546 retval = map->m_len;
1547 map->m_len = retval;
1548 if (ext4_es_is_written(&es))
1549 map->m_flags |= EXT4_MAP_MAPPED;
1550 else if (ext4_es_is_unwritten(&es))
1551 map->m_flags |= EXT4_MAP_UNWRITTEN;
1552 else
1553 BUG_ON(1);
1554
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001555#ifdef ES_AGGRESSIVE_TEST
1556 ext4_map_blocks_es_recheck(NULL, inode, map, &orig_map, 0);
1557#endif
Zheng Liud100eef2013-02-18 00:29:59 -05001558 return retval;
1559 }
1560
Aditya Kali5356f262011-09-09 19:20:51 -04001561 /*
1562 * Try to see if we can get the block without requesting a new
1563 * file system block.
1564 */
1565 down_read((&EXT4_I(inode)->i_data_sem));
Tao Ma9c3569b2012-12-10 14:05:57 -05001566 if (ext4_has_inline_data(inode)) {
1567 /*
1568 * We will soon create blocks for this page, and let
1569 * us pretend as if the blocks aren't allocated yet.
1570 * In case of clusters, we have to handle the work
1571 * of mapping from cluster so that the reserved space
1572 * is calculated properly.
1573 */
1574 if ((EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) &&
1575 ext4_find_delalloc_cluster(inode, map->m_lblk))
1576 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
1577 retval = 0;
1578 } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Zheng Liud100eef2013-02-18 00:29:59 -05001579 retval = ext4_ext_map_blocks(NULL, inode, map,
1580 EXT4_GET_BLOCKS_NO_PUT_HOLE);
Aditya Kali5356f262011-09-09 19:20:51 -04001581 else
Zheng Liud100eef2013-02-18 00:29:59 -05001582 retval = ext4_ind_map_blocks(NULL, inode, map,
1583 EXT4_GET_BLOCKS_NO_PUT_HOLE);
Aditya Kali5356f262011-09-09 19:20:51 -04001584
Zheng Liud100eef2013-02-18 00:29:59 -05001585add_delayed:
Aditya Kali5356f262011-09-09 19:20:51 -04001586 if (retval == 0) {
Zheng Liuf7fec032013-02-18 00:28:47 -05001587 int ret;
Aditya Kali5356f262011-09-09 19:20:51 -04001588 /*
1589 * XXX: __block_prepare_write() unmaps passed block,
1590 * is it OK?
1591 */
Lukas Czerner386ad672013-03-10 22:50:00 -04001592 /*
1593 * If the block was allocated from previously allocated cluster,
1594 * then we don't need to reserve it again. However we still need
1595 * to reserve metadata for every block we're going to write.
1596 */
Aditya Kali5356f262011-09-09 19:20:51 -04001597 if (!(map->m_flags & EXT4_MAP_FROM_CLUSTER)) {
Zheng Liuf7fec032013-02-18 00:28:47 -05001598 ret = ext4_da_reserve_space(inode, iblock);
1599 if (ret) {
Aditya Kali5356f262011-09-09 19:20:51 -04001600 /* not enough space to reserve */
Zheng Liuf7fec032013-02-18 00:28:47 -05001601 retval = ret;
Aditya Kali5356f262011-09-09 19:20:51 -04001602 goto out_unlock;
Zheng Liuf7fec032013-02-18 00:28:47 -05001603 }
Lukas Czerner386ad672013-03-10 22:50:00 -04001604 } else {
1605 ret = ext4_da_reserve_metadata(inode, iblock);
1606 if (ret) {
1607 /* not enough space to reserve */
1608 retval = ret;
1609 goto out_unlock;
1610 }
Aditya Kali5356f262011-09-09 19:20:51 -04001611 }
1612
Zheng Liuf7fec032013-02-18 00:28:47 -05001613 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
1614 ~0, EXTENT_STATUS_DELAYED);
1615 if (ret) {
1616 retval = ret;
Zheng Liu51865fd2012-11-08 21:57:32 -05001617 goto out_unlock;
Zheng Liuf7fec032013-02-18 00:28:47 -05001618 }
Zheng Liu51865fd2012-11-08 21:57:32 -05001619
Aditya Kali5356f262011-09-09 19:20:51 -04001620 /* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served
1621 * and it should not appear on the bh->b_state.
1622 */
1623 map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
1624
1625 map_bh(bh, inode->i_sb, invalid_block);
1626 set_buffer_new(bh);
1627 set_buffer_delay(bh);
Zheng Liuf7fec032013-02-18 00:28:47 -05001628 } else if (retval > 0) {
1629 int ret;
Theodore Ts'o3be78c72013-08-16 21:22:41 -04001630 unsigned int status;
Zheng Liuf7fec032013-02-18 00:28:47 -05001631
Zheng Liu44fb851d2013-07-29 12:51:42 -04001632 if (unlikely(retval != map->m_len)) {
1633 ext4_warning(inode->i_sb,
1634 "ES len assertion failed for inode "
1635 "%lu: retval %d != map->m_len %d",
1636 inode->i_ino, retval, map->m_len);
1637 WARN_ON(1);
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001638 }
Dmitry Monakhov921f2662013-03-10 21:01:03 -04001639
Zheng Liuf7fec032013-02-18 00:28:47 -05001640 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
1641 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
1642 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
1643 map->m_pblk, status);
1644 if (ret != 0)
1645 retval = ret;
Aditya Kali5356f262011-09-09 19:20:51 -04001646 }
1647
1648out_unlock:
1649 up_read((&EXT4_I(inode)->i_data_sem));
1650
1651 return retval;
1652}
1653
1654/*
Theodore Ts'ob920c752009-05-14 00:54:29 -04001655 * This is a special get_blocks_t callback which is used by
1656 * ext4_da_write_begin(). It will either return mapped block or
1657 * reserve space for a single block.
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001658 *
1659 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
1660 * We also have b_blocknr = -1 and b_bdev initialized properly
1661 *
1662 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
1663 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
1664 * initialized properly.
Alex Tomas64769242008-07-11 19:27:31 -04001665 */
Tao Ma9c3569b2012-12-10 14:05:57 -05001666int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
1667 struct buffer_head *bh, int create)
Alex Tomas64769242008-07-11 19:27:31 -04001668{
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001669 struct ext4_map_blocks map;
Alex Tomas64769242008-07-11 19:27:31 -04001670 int ret = 0;
1671
1672 BUG_ON(create == 0);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001673 BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
1674
1675 map.m_lblk = iblock;
1676 map.m_len = 1;
Alex Tomas64769242008-07-11 19:27:31 -04001677
1678 /*
1679 * first, we need to know whether the block is allocated already
1680 * preallocated blocks are unmapped but should treated
1681 * the same as allocated blocks.
1682 */
Aditya Kali5356f262011-09-09 19:20:51 -04001683 ret = ext4_da_map_blocks(inode, iblock, &map, bh);
1684 if (ret <= 0)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001685 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04001686
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001687 map_bh(bh, inode->i_sb, map.m_pblk);
1688 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
1689
1690 if (buffer_unwritten(bh)) {
1691 /* A delayed write to unwritten bh should be marked
1692 * new and mapped. Mapped ensures that we don't do
1693 * get_block multiple times when we write to the same
1694 * offset and new ensures that we do proper zero out
1695 * for partial write.
1696 */
1697 set_buffer_new(bh);
Theodore Ts'oc8205632011-04-10 22:30:07 -04001698 set_buffer_mapped(bh);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001699 }
1700 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04001701}
Mingming Cao61628a32008-07-11 19:27:31 -04001702
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001703static int bget_one(handle_t *handle, struct buffer_head *bh)
1704{
1705 get_bh(bh);
1706 return 0;
1707}
1708
1709static int bput_one(handle_t *handle, struct buffer_head *bh)
1710{
1711 put_bh(bh);
1712 return 0;
1713}
1714
1715static int __ext4_journalled_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001716 unsigned int len)
1717{
1718 struct address_space *mapping = page->mapping;
1719 struct inode *inode = mapping->host;
Tao Ma3fdcfb62012-12-10 14:05:57 -05001720 struct buffer_head *page_bufs = NULL;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001721 handle_t *handle = NULL;
Tao Ma3fdcfb62012-12-10 14:05:57 -05001722 int ret = 0, err = 0;
1723 int inline_data = ext4_has_inline_data(inode);
1724 struct buffer_head *inode_bh = NULL;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001725
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001726 ClearPageChecked(page);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001727
1728 if (inline_data) {
1729 BUG_ON(page->index != 0);
1730 BUG_ON(len > ext4_get_max_inline_size(inode));
1731 inode_bh = ext4_journalled_write_inline_data(inode, len, page);
1732 if (inode_bh == NULL)
1733 goto out;
1734 } else {
1735 page_bufs = page_buffers(page);
1736 if (!page_bufs) {
1737 BUG();
1738 goto out;
1739 }
1740 ext4_walk_page_buffers(handle, page_bufs, 0, len,
1741 NULL, bget_one);
1742 }
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001743 /* As soon as we unlock the page, it can go away, but we have
1744 * references to buffers so we are safe */
1745 unlock_page(page);
1746
Theodore Ts'o9924a922013-02-08 21:59:22 -05001747 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
1748 ext4_writepage_trans_blocks(inode));
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001749 if (IS_ERR(handle)) {
1750 ret = PTR_ERR(handle);
1751 goto out;
1752 }
1753
Curt Wohlgemuth441c8502011-08-13 11:25:18 -04001754 BUG_ON(!ext4_handle_valid(handle));
1755
Tao Ma3fdcfb62012-12-10 14:05:57 -05001756 if (inline_data) {
1757 ret = ext4_journal_get_write_access(handle, inode_bh);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001758
Tao Ma3fdcfb62012-12-10 14:05:57 -05001759 err = ext4_handle_dirty_metadata(handle, inode, inode_bh);
1760
1761 } else {
1762 ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
1763 do_journal_get_write_access);
1764
1765 err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
1766 write_end_fn);
1767 }
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001768 if (ret == 0)
1769 ret = err;
Jan Kara2d859db2011-07-26 09:07:11 -04001770 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001771 err = ext4_journal_stop(handle);
1772 if (!ret)
1773 ret = err;
1774
Tao Ma3fdcfb62012-12-10 14:05:57 -05001775 if (!ext4_has_inline_data(inode))
Theodore Ts'o8c9367f2014-01-07 13:08:03 -05001776 ext4_walk_page_buffers(NULL, page_bufs, 0, len,
Tao Ma3fdcfb62012-12-10 14:05:57 -05001777 NULL, bput_one);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001778 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001779out:
Tao Ma3fdcfb62012-12-10 14:05:57 -05001780 brelse(inode_bh);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001781 return ret;
1782}
1783
Mingming Cao61628a32008-07-11 19:27:31 -04001784/*
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001785 * Note that we don't need to start a transaction unless we're journaling data
1786 * because we should have holes filled from ext4_page_mkwrite(). We even don't
1787 * need to file the inode to the transaction's list in ordered mode because if
1788 * we are writing back data added by write(), the inode is already there and if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001789 * we are writing back data modified via mmap(), no one guarantees in which
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001790 * transaction the data will hit the disk. In case we are journaling data, we
1791 * cannot start transaction directly because transaction start ranks above page
1792 * lock so we have to do some magic.
1793 *
Theodore Ts'ob920c752009-05-14 00:54:29 -04001794 * This function can get called via...
Theodore Ts'o20970ba2013-06-06 14:00:46 -04001795 * - ext4_writepages after taking page lock (have journal handle)
Theodore Ts'ob920c752009-05-14 00:54:29 -04001796 * - journal_submit_inode_data_buffers (no journal handle)
Artem Bityutskiyf6463b02012-07-25 18:12:04 +03001797 * - shrink_page_list via the kswapd/direct reclaim (no journal handle)
Theodore Ts'ob920c752009-05-14 00:54:29 -04001798 * - grab_page_cache when doing write_begin (have journal handle)
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001799 *
1800 * We don't do any block allocation in this function. If we have page with
1801 * multiple blocks we need to write those buffer_heads that are mapped. This
1802 * is important for mmaped based write. So if we do with blocksize 1K
1803 * truncate(f, 1024);
1804 * a = mmap(f, 0, 4096);
1805 * a[0] = 'a';
1806 * truncate(f, 4096);
1807 * we have in the page first buffer_head mapped via page_mkwrite call back
Paul Bolle90802ed2011-12-05 13:00:34 +01001808 * but other buffer_heads would be unmapped but dirty (dirty done via the
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001809 * do_wp_page). So writepage should write the first block. If we modify
1810 * the mmap area beyond 1024 we will again get a page_fault and the
1811 * page_mkwrite callback will do the block allocation and mark the
1812 * buffer_heads mapped.
1813 *
1814 * We redirty the page if we have any buffer_heads that is either delay or
1815 * unwritten in the page.
1816 *
1817 * We can get recursively called as show below.
1818 *
1819 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
1820 * ext4_writepage()
1821 *
1822 * But since we don't do any block allocation we should not deadlock.
1823 * Page also have the dirty flag cleared so we don't get recurive page_lock.
Mingming Cao61628a32008-07-11 19:27:31 -04001824 */
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001825static int ext4_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001826 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04001827{
Jan Karaf8bec372013-01-28 12:55:08 -05001828 int ret = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04001829 loff_t size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001830 unsigned int len;
Jiaying Zhang744692d2010-03-04 16:14:02 -05001831 struct buffer_head *page_bufs = NULL;
Mingming Cao61628a32008-07-11 19:27:31 -04001832 struct inode *inode = page->mapping->host;
Jan Kara36ade452013-01-28 09:30:52 -05001833 struct ext4_io_submit io_submit;
Alex Tomas64769242008-07-11 19:27:31 -04001834
Lukas Czernera9c667f2011-06-06 09:51:52 -04001835 trace_ext4_writepage(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04001836 size = i_size_read(inode);
1837 if (page->index == size >> PAGE_CACHE_SHIFT)
1838 len = size & ~PAGE_CACHE_MASK;
1839 else
1840 len = PAGE_CACHE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04001841
Theodore Ts'oa42afc52010-10-27 21:30:09 -04001842 page_bufs = page_buffers(page);
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001843 /*
Jan Karafe386132013-01-28 21:06:42 -05001844 * We cannot do block allocation or other extent handling in this
1845 * function. If there are buffers needing that, we have to redirty
1846 * the page. But we may reach here when we do a journal commit via
1847 * journal_submit_inode_data_buffers() and in that case we must write
1848 * allocated buffers to achieve data=ordered mode guarantees.
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04001849 */
Tao Maf19d5872012-12-10 14:05:51 -05001850 if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL,
1851 ext4_bh_delay_or_unwritten)) {
Jan Karaf8bec372013-01-28 12:55:08 -05001852 redirty_page_for_writepage(wbc, page);
Jan Karafe386132013-01-28 21:06:42 -05001853 if (current->flags & PF_MEMALLOC) {
1854 /*
1855 * For memory cleaning there's no point in writing only
1856 * some buffers. So just bail out. Warn if we came here
1857 * from direct reclaim.
1858 */
1859 WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD))
1860 == PF_MEMALLOC);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04001861 unlock_page(page);
1862 return 0;
1863 }
Theodore Ts'oa42afc52010-10-27 21:30:09 -04001864 }
Alex Tomas64769242008-07-11 19:27:31 -04001865
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001866 if (PageChecked(page) && ext4_should_journal_data(inode))
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001867 /*
1868 * It's mmapped pagecache. Add buffers and journal it. There
1869 * doesn't seem much point in redirtying the page here.
1870 */
Wu Fengguang3f0ca302009-11-24 11:15:44 -05001871 return __ext4_journalled_writepage(page, len);
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001872
Jan Kara97a851e2013-06-04 11:58:58 -04001873 ext4_io_submit_init(&io_submit, wbc);
1874 io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS);
1875 if (!io_submit.io_end) {
1876 redirty_page_for_writepage(wbc, page);
1877 unlock_page(page);
1878 return -ENOMEM;
1879 }
Jan Kara36ade452013-01-28 09:30:52 -05001880 ret = ext4_bio_write_page(&io_submit, page, len, wbc);
1881 ext4_io_submit(&io_submit);
Jan Kara97a851e2013-06-04 11:58:58 -04001882 /* Drop io_end reference we got from init */
1883 ext4_put_io_end_defer(io_submit.io_end);
Alex Tomas64769242008-07-11 19:27:31 -04001884 return ret;
1885}
1886
Jan Kara5f1132b2013-08-17 10:02:33 -04001887static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page)
1888{
1889 int len;
1890 loff_t size = i_size_read(mpd->inode);
1891 int err;
1892
1893 BUG_ON(page->index != mpd->first_page);
1894 if (page->index == size >> PAGE_CACHE_SHIFT)
1895 len = size & ~PAGE_CACHE_MASK;
1896 else
1897 len = PAGE_CACHE_SIZE;
1898 clear_page_dirty_for_io(page);
1899 err = ext4_bio_write_page(&mpd->io_submit, page, len, mpd->wbc);
1900 if (!err)
1901 mpd->wbc->nr_to_write--;
1902 mpd->first_page++;
1903
1904 return err;
1905}
1906
Jan Kara4e7ea812013-06-04 13:17:40 -04001907#define BH_FLAGS ((1 << BH_Unwritten) | (1 << BH_Delay))
1908
Mingming Cao61628a32008-07-11 19:27:31 -04001909/*
Jan Karafffb2732013-06-04 13:01:11 -04001910 * mballoc gives us at most this number of blocks...
1911 * XXX: That seems to be only a limitation of ext4_mb_normalize_request().
Anatol Pomozov70261f52013-08-28 14:40:12 -04001912 * The rest of mballoc seems to handle chunks up to full group size.
Mingming Cao61628a32008-07-11 19:27:31 -04001913 */
Jan Karafffb2732013-06-04 13:01:11 -04001914#define MAX_WRITEPAGES_EXTENT_LEN 2048
Mingming Cao525f4ed2008-08-19 22:15:58 -04001915
Jan Karafffb2732013-06-04 13:01:11 -04001916/*
Jan Kara4e7ea812013-06-04 13:17:40 -04001917 * mpage_add_bh_to_extent - try to add bh to extent of blocks to map
1918 *
1919 * @mpd - extent of blocks
1920 * @lblk - logical number of the block in the file
Jan Kara09930042013-08-17 09:57:56 -04001921 * @bh - buffer head we want to add to the extent
Jan Kara4e7ea812013-06-04 13:17:40 -04001922 *
Jan Kara09930042013-08-17 09:57:56 -04001923 * The function is used to collect contig. blocks in the same state. If the
1924 * buffer doesn't require mapping for writeback and we haven't started the
1925 * extent of buffers to map yet, the function returns 'true' immediately - the
1926 * caller can write the buffer right away. Otherwise the function returns true
1927 * if the block has been added to the extent, false if the block couldn't be
1928 * added.
Jan Kara4e7ea812013-06-04 13:17:40 -04001929 */
Jan Kara09930042013-08-17 09:57:56 -04001930static bool mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk,
1931 struct buffer_head *bh)
Jan Kara4e7ea812013-06-04 13:17:40 -04001932{
1933 struct ext4_map_blocks *map = &mpd->map;
1934
Jan Kara09930042013-08-17 09:57:56 -04001935 /* Buffer that doesn't need mapping for writeback? */
1936 if (!buffer_dirty(bh) || !buffer_mapped(bh) ||
1937 (!buffer_delay(bh) && !buffer_unwritten(bh))) {
1938 /* So far no extent to map => we write the buffer right away */
1939 if (map->m_len == 0)
1940 return true;
1941 return false;
1942 }
Jan Kara4e7ea812013-06-04 13:17:40 -04001943
1944 /* First block in the extent? */
1945 if (map->m_len == 0) {
1946 map->m_lblk = lblk;
1947 map->m_len = 1;
Jan Kara09930042013-08-17 09:57:56 -04001948 map->m_flags = bh->b_state & BH_FLAGS;
1949 return true;
Jan Kara4e7ea812013-06-04 13:17:40 -04001950 }
1951
Jan Kara09930042013-08-17 09:57:56 -04001952 /* Don't go larger than mballoc is willing to allocate */
1953 if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN)
1954 return false;
1955
Jan Kara4e7ea812013-06-04 13:17:40 -04001956 /* Can we merge the block to our big extent? */
1957 if (lblk == map->m_lblk + map->m_len &&
Jan Kara09930042013-08-17 09:57:56 -04001958 (bh->b_state & BH_FLAGS) == map->m_flags) {
Jan Kara4e7ea812013-06-04 13:17:40 -04001959 map->m_len++;
Jan Kara09930042013-08-17 09:57:56 -04001960 return true;
Jan Kara4e7ea812013-06-04 13:17:40 -04001961 }
Jan Kara09930042013-08-17 09:57:56 -04001962 return false;
Jan Kara4e7ea812013-06-04 13:17:40 -04001963}
1964
Jan Kara5f1132b2013-08-17 10:02:33 -04001965/*
1966 * mpage_process_page_bufs - submit page buffers for IO or add them to extent
1967 *
1968 * @mpd - extent of blocks for mapping
1969 * @head - the first buffer in the page
1970 * @bh - buffer we should start processing from
1971 * @lblk - logical number of the block in the file corresponding to @bh
1972 *
1973 * Walk through page buffers from @bh upto @head (exclusive) and either submit
1974 * the page for IO if all buffers in this page were mapped and there's no
1975 * accumulated extent of buffers to map or add buffers in the page to the
1976 * extent of buffers to map. The function returns 1 if the caller can continue
1977 * by processing the next page, 0 if it should stop adding buffers to the
1978 * extent to map because we cannot extend it anymore. It can also return value
1979 * < 0 in case of error during IO submission.
1980 */
1981static int mpage_process_page_bufs(struct mpage_da_data *mpd,
1982 struct buffer_head *head,
1983 struct buffer_head *bh,
1984 ext4_lblk_t lblk)
Jan Kara4e7ea812013-06-04 13:17:40 -04001985{
1986 struct inode *inode = mpd->inode;
Jan Kara5f1132b2013-08-17 10:02:33 -04001987 int err;
Jan Kara4e7ea812013-06-04 13:17:40 -04001988 ext4_lblk_t blocks = (i_size_read(inode) + (1 << inode->i_blkbits) - 1)
1989 >> inode->i_blkbits;
1990
1991 do {
1992 BUG_ON(buffer_locked(bh));
1993
Jan Kara09930042013-08-17 09:57:56 -04001994 if (lblk >= blocks || !mpage_add_bh_to_extent(mpd, lblk, bh)) {
Jan Kara4e7ea812013-06-04 13:17:40 -04001995 /* Found extent to map? */
1996 if (mpd->map.m_len)
Jan Kara5f1132b2013-08-17 10:02:33 -04001997 return 0;
Jan Kara09930042013-08-17 09:57:56 -04001998 /* Everything mapped so far and we hit EOF */
Jan Kara5f1132b2013-08-17 10:02:33 -04001999 break;
Jan Kara4e7ea812013-06-04 13:17:40 -04002000 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002001 } while (lblk++, (bh = bh->b_this_page) != head);
Jan Kara5f1132b2013-08-17 10:02:33 -04002002 /* So far everything mapped? Submit the page for IO. */
2003 if (mpd->map.m_len == 0) {
2004 err = mpage_submit_page(mpd, head->b_page);
2005 if (err < 0)
2006 return err;
2007 }
2008 return lblk < blocks;
Jan Kara4e7ea812013-06-04 13:17:40 -04002009}
2010
2011/*
2012 * mpage_map_buffers - update buffers corresponding to changed extent and
2013 * submit fully mapped pages for IO
2014 *
2015 * @mpd - description of extent to map, on return next extent to map
2016 *
2017 * Scan buffers corresponding to changed extent (we expect corresponding pages
2018 * to be already locked) and update buffer state according to new extent state.
2019 * We map delalloc buffers to their physical location, clear unwritten bits,
2020 * and mark buffers as uninit when we perform writes to uninitialized extents
2021 * and do extent conversion after IO is finished. If the last page is not fully
2022 * mapped, we update @map to the next extent in the last page that needs
2023 * mapping. Otherwise we submit the page for IO.
2024 */
2025static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd)
2026{
2027 struct pagevec pvec;
2028 int nr_pages, i;
2029 struct inode *inode = mpd->inode;
2030 struct buffer_head *head, *bh;
2031 int bpp_bits = PAGE_CACHE_SHIFT - inode->i_blkbits;
Jan Kara4e7ea812013-06-04 13:17:40 -04002032 pgoff_t start, end;
2033 ext4_lblk_t lblk;
2034 sector_t pblock;
2035 int err;
2036
2037 start = mpd->map.m_lblk >> bpp_bits;
2038 end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits;
2039 lblk = start << bpp_bits;
2040 pblock = mpd->map.m_pblk;
2041
2042 pagevec_init(&pvec, 0);
2043 while (start <= end) {
2044 nr_pages = pagevec_lookup(&pvec, inode->i_mapping, start,
2045 PAGEVEC_SIZE);
2046 if (nr_pages == 0)
2047 break;
2048 for (i = 0; i < nr_pages; i++) {
2049 struct page *page = pvec.pages[i];
2050
2051 if (page->index > end)
2052 break;
Anatol Pomozov70261f52013-08-28 14:40:12 -04002053 /* Up to 'end' pages must be contiguous */
Jan Kara4e7ea812013-06-04 13:17:40 -04002054 BUG_ON(page->index != start);
2055 bh = head = page_buffers(page);
2056 do {
2057 if (lblk < mpd->map.m_lblk)
2058 continue;
2059 if (lblk >= mpd->map.m_lblk + mpd->map.m_len) {
2060 /*
2061 * Buffer after end of mapped extent.
2062 * Find next buffer in the page to map.
2063 */
2064 mpd->map.m_len = 0;
2065 mpd->map.m_flags = 0;
Jan Kara5f1132b2013-08-17 10:02:33 -04002066 /*
2067 * FIXME: If dioread_nolock supports
2068 * blocksize < pagesize, we need to make
2069 * sure we add size mapped so far to
2070 * io_end->size as the following call
2071 * can submit the page for IO.
2072 */
2073 err = mpage_process_page_bufs(mpd, head,
2074 bh, lblk);
Jan Kara4e7ea812013-06-04 13:17:40 -04002075 pagevec_release(&pvec);
Jan Kara5f1132b2013-08-17 10:02:33 -04002076 if (err > 0)
2077 err = 0;
2078 return err;
Jan Kara4e7ea812013-06-04 13:17:40 -04002079 }
2080 if (buffer_delay(bh)) {
2081 clear_buffer_delay(bh);
2082 bh->b_blocknr = pblock++;
2083 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002084 clear_buffer_unwritten(bh);
Jan Kara5f1132b2013-08-17 10:02:33 -04002085 } while (lblk++, (bh = bh->b_this_page) != head);
Jan Kara4e7ea812013-06-04 13:17:40 -04002086
2087 /*
2088 * FIXME: This is going to break if dioread_nolock
2089 * supports blocksize < pagesize as we will try to
2090 * convert potentially unmapped parts of inode.
2091 */
2092 mpd->io_submit.io_end->size += PAGE_CACHE_SIZE;
2093 /* Page fully mapped - let IO run! */
2094 err = mpage_submit_page(mpd, page);
2095 if (err < 0) {
2096 pagevec_release(&pvec);
2097 return err;
2098 }
2099 start++;
2100 }
2101 pagevec_release(&pvec);
2102 }
2103 /* Extent fully mapped and matches with page boundary. We are done. */
2104 mpd->map.m_len = 0;
2105 mpd->map.m_flags = 0;
2106 return 0;
2107}
2108
2109static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd)
2110{
2111 struct inode *inode = mpd->inode;
2112 struct ext4_map_blocks *map = &mpd->map;
2113 int get_blocks_flags;
2114 int err;
2115
2116 trace_ext4_da_write_pages_extent(inode, map);
2117 /*
2118 * Call ext4_map_blocks() to allocate any delayed allocation blocks, or
2119 * to convert an uninitialized extent to be initialized (in the case
2120 * where we have written into one or more preallocated blocks). It is
2121 * possible that we're going to need more metadata blocks than
2122 * previously reserved. However we must not fail because we're in
2123 * writeback and there is nothing we can do about it so it might result
2124 * in data loss. So use reserved blocks to allocate metadata if
2125 * possible.
2126 *
2127 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if the blocks
2128 * in question are delalloc blocks. This affects functions in many
2129 * different parts of the allocation call path. This flag exists
2130 * primarily because we don't want to change *many* call functions, so
2131 * ext4_map_blocks() will set the EXT4_STATE_DELALLOC_RESERVED flag
2132 * once the inode's allocation semaphore is taken.
2133 */
2134 get_blocks_flags = EXT4_GET_BLOCKS_CREATE |
2135 EXT4_GET_BLOCKS_METADATA_NOFAIL;
2136 if (ext4_should_dioread_nolock(inode))
2137 get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
2138 if (map->m_flags & (1 << BH_Delay))
2139 get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
2140
2141 err = ext4_map_blocks(handle, inode, map, get_blocks_flags);
2142 if (err < 0)
2143 return err;
Jan Kara6b523df2013-06-04 13:21:11 -04002144 if (map->m_flags & EXT4_MAP_UNINIT) {
2145 if (!mpd->io_submit.io_end->handle &&
2146 ext4_handle_valid(handle)) {
2147 mpd->io_submit.io_end->handle = handle->h_rsv_handle;
2148 handle->h_rsv_handle = NULL;
2149 }
Jan Kara3613d222013-06-04 13:19:34 -04002150 ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end);
Jan Kara6b523df2013-06-04 13:21:11 -04002151 }
Jan Kara4e7ea812013-06-04 13:17:40 -04002152
2153 BUG_ON(map->m_len == 0);
2154 if (map->m_flags & EXT4_MAP_NEW) {
2155 struct block_device *bdev = inode->i_sb->s_bdev;
2156 int i;
2157
2158 for (i = 0; i < map->m_len; i++)
2159 unmap_underlying_metadata(bdev, map->m_pblk + i);
2160 }
2161 return 0;
2162}
2163
2164/*
2165 * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length
2166 * mpd->len and submit pages underlying it for IO
2167 *
2168 * @handle - handle for journal operations
2169 * @mpd - extent to map
Jan Kara7534e852013-10-16 08:26:08 -04002170 * @give_up_on_write - we set this to true iff there is a fatal error and there
2171 * is no hope of writing the data. The caller should discard
2172 * dirty pages to avoid infinite loops.
Jan Kara4e7ea812013-06-04 13:17:40 -04002173 *
2174 * The function maps extent starting at mpd->lblk of length mpd->len. If it is
2175 * delayed, blocks are allocated, if it is unwritten, we may need to convert
2176 * them to initialized or split the described range from larger unwritten
2177 * extent. Note that we need not map all the described range since allocation
2178 * can return less blocks or the range is covered by more unwritten extents. We
2179 * cannot map more because we are limited by reserved transaction credits. On
2180 * the other hand we always make sure that the last touched page is fully
2181 * mapped so that it can be written out (and thus forward progress is
2182 * guaranteed). After mapping we submit all mapped pages for IO.
2183 */
2184static int mpage_map_and_submit_extent(handle_t *handle,
Theodore Ts'ocb530542013-07-01 08:12:40 -04002185 struct mpage_da_data *mpd,
2186 bool *give_up_on_write)
Jan Kara4e7ea812013-06-04 13:17:40 -04002187{
2188 struct inode *inode = mpd->inode;
2189 struct ext4_map_blocks *map = &mpd->map;
2190 int err;
2191 loff_t disksize;
2192
2193 mpd->io_submit.io_end->offset =
2194 ((loff_t)map->m_lblk) << inode->i_blkbits;
Jan Kara27d7c4e2013-07-05 21:57:22 -04002195 do {
Jan Kara4e7ea812013-06-04 13:17:40 -04002196 err = mpage_map_one_extent(handle, mpd);
2197 if (err < 0) {
2198 struct super_block *sb = inode->i_sb;
2199
Theodore Ts'ocb530542013-07-01 08:12:40 -04002200 if (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)
2201 goto invalidate_dirty_pages;
Jan Kara4e7ea812013-06-04 13:17:40 -04002202 /*
Theodore Ts'ocb530542013-07-01 08:12:40 -04002203 * Let the uper layers retry transient errors.
2204 * In the case of ENOSPC, if ext4_count_free_blocks()
2205 * is non-zero, a commit should free up blocks.
Jan Kara4e7ea812013-06-04 13:17:40 -04002206 */
Theodore Ts'ocb530542013-07-01 08:12:40 -04002207 if ((err == -ENOMEM) ||
2208 (err == -ENOSPC && ext4_count_free_clusters(sb)))
2209 return err;
2210 ext4_msg(sb, KERN_CRIT,
2211 "Delayed block allocation failed for "
2212 "inode %lu at logical offset %llu with"
2213 " max blocks %u with error %d",
2214 inode->i_ino,
2215 (unsigned long long)map->m_lblk,
2216 (unsigned)map->m_len, -err);
2217 ext4_msg(sb, KERN_CRIT,
2218 "This should not happen!! Data will "
2219 "be lost\n");
2220 if (err == -ENOSPC)
2221 ext4_print_free_blocks(inode);
2222 invalidate_dirty_pages:
2223 *give_up_on_write = true;
Jan Kara4e7ea812013-06-04 13:17:40 -04002224 return err;
2225 }
2226 /*
2227 * Update buffer state, submit mapped pages, and get us new
2228 * extent to map
2229 */
2230 err = mpage_map_and_submit_buffers(mpd);
2231 if (err < 0)
2232 return err;
Jan Kara27d7c4e2013-07-05 21:57:22 -04002233 } while (map->m_len);
Jan Kara4e7ea812013-06-04 13:17:40 -04002234
2235 /* Update on-disk size after IO is submitted */
2236 disksize = ((loff_t)mpd->first_page) << PAGE_CACHE_SHIFT;
Jan Kara4e7ea812013-06-04 13:17:40 -04002237 if (disksize > EXT4_I(inode)->i_disksize) {
2238 int err2;
2239
Jan Kara90e775b2013-08-17 10:09:31 -04002240 ext4_wb_update_i_disksize(inode, disksize);
Jan Kara4e7ea812013-06-04 13:17:40 -04002241 err2 = ext4_mark_inode_dirty(handle, inode);
2242 if (err2)
2243 ext4_error(inode->i_sb,
2244 "Failed to mark inode %lu dirty",
2245 inode->i_ino);
2246 if (!err)
2247 err = err2;
2248 }
2249 return err;
2250}
2251
2252/*
Jan Karafffb2732013-06-04 13:01:11 -04002253 * Calculate the total number of credits to reserve for one writepages
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002254 * iteration. This is called from ext4_writepages(). We map an extent of
Anatol Pomozov70261f52013-08-28 14:40:12 -04002255 * up to MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping
Jan Karafffb2732013-06-04 13:01:11 -04002256 * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN +
2257 * bpp - 1 blocks in bpp different extents.
2258 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002259static int ext4_da_writepages_trans_blocks(struct inode *inode)
2260{
Jan Karafffb2732013-06-04 13:01:11 -04002261 int bpp = ext4_journal_blocks_per_page(inode);
Mingming Cao525f4ed2008-08-19 22:15:58 -04002262
Jan Karafffb2732013-06-04 13:01:11 -04002263 return ext4_meta_trans_blocks(inode,
2264 MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp);
Mingming Cao525f4ed2008-08-19 22:15:58 -04002265}
Mingming Cao61628a32008-07-11 19:27:31 -04002266
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002267/*
Jan Kara4e7ea812013-06-04 13:17:40 -04002268 * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages
2269 * and underlying extent to map
2270 *
2271 * @mpd - where to look for pages
2272 *
2273 * Walk dirty pages in the mapping. If they are fully mapped, submit them for
2274 * IO immediately. When we find a page which isn't mapped we start accumulating
2275 * extent of buffers underlying these pages that needs mapping (formed by
2276 * either delayed or unwritten buffers). We also lock the pages containing
2277 * these buffers. The extent found is returned in @mpd structure (starting at
2278 * mpd->lblk with length mpd->len blocks).
2279 *
2280 * Note that this function can attach bios to one io_end structure which are
2281 * neither logically nor physically contiguous. Although it may seem as an
2282 * unnecessary complication, it is actually inevitable in blocksize < pagesize
2283 * 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 -04002284 */
Jan Kara4e7ea812013-06-04 13:17:40 -04002285static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd)
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002286{
Jan Kara4e7ea812013-06-04 13:17:40 -04002287 struct address_space *mapping = mpd->inode->i_mapping;
2288 struct pagevec pvec;
2289 unsigned int nr_pages;
Ming Leiaeac5892013-10-17 18:56:16 -04002290 long left = mpd->wbc->nr_to_write;
Jan Kara4e7ea812013-06-04 13:17:40 -04002291 pgoff_t index = mpd->first_page;
2292 pgoff_t end = mpd->last_page;
2293 int tag;
2294 int i, err = 0;
2295 int blkbits = mpd->inode->i_blkbits;
2296 ext4_lblk_t lblk;
2297 struct buffer_head *head;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002298
Jan Kara4e7ea812013-06-04 13:17:40 -04002299 if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages)
Eric Sandeen5b41d922010-10-27 21:30:13 -04002300 tag = PAGECACHE_TAG_TOWRITE;
2301 else
2302 tag = PAGECACHE_TAG_DIRTY;
2303
Jan Kara4e7ea812013-06-04 13:17:40 -04002304 pagevec_init(&pvec, 0);
2305 mpd->map.m_len = 0;
2306 mpd->next_page = index;
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002307 while (index <= end) {
Eric Sandeen5b41d922010-10-27 21:30:13 -04002308 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002309 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
2310 if (nr_pages == 0)
Jan Kara4e7ea812013-06-04 13:17:40 -04002311 goto out;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002312
2313 for (i = 0; i < nr_pages; i++) {
2314 struct page *page = pvec.pages[i];
2315
2316 /*
2317 * At this point, the page may be truncated or
2318 * invalidated (changing page->mapping to NULL), or
2319 * even swizzled back from swapper_space to tmpfs file
2320 * mapping. However, page->index will not change
2321 * because we have a reference on the page.
2322 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002323 if (page->index > end)
2324 goto out;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002325
Ming Leiaeac5892013-10-17 18:56:16 -04002326 /*
2327 * Accumulated enough dirty pages? This doesn't apply
2328 * to WB_SYNC_ALL mode. For integrity sync we have to
2329 * keep going because someone may be concurrently
2330 * dirtying pages, and we might have synced a lot of
2331 * newly appeared dirty pages, but have not synced all
2332 * of the old dirty pages.
2333 */
2334 if (mpd->wbc->sync_mode == WB_SYNC_NONE && left <= 0)
2335 goto out;
2336
Jan Kara4e7ea812013-06-04 13:17:40 -04002337 /* If we can't merge this page, we are done. */
2338 if (mpd->map.m_len > 0 && mpd->next_page != page->index)
2339 goto out;
Theodore Ts'o78aaced2011-02-26 14:09:14 -05002340
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002341 lock_page(page);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002342 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002343 * If the page is no longer dirty, or its mapping no
2344 * longer corresponds to inode we are writing (which
2345 * means it has been truncated or invalidated), or the
2346 * page is already under writeback and we are not doing
2347 * a data integrity writeback, skip the page
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002348 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002349 if (!PageDirty(page) ||
2350 (PageWriteback(page) &&
Jan Kara4e7ea812013-06-04 13:17:40 -04002351 (mpd->wbc->sync_mode == WB_SYNC_NONE)) ||
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002352 unlikely(page->mapping != mapping)) {
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002353 unlock_page(page);
2354 continue;
2355 }
2356
Darrick J. Wong7cb1a532011-05-18 13:53:20 -04002357 wait_on_page_writeback(page);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002358 BUG_ON(PageWriteback(page));
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002359
Jan Kara4e7ea812013-06-04 13:17:40 -04002360 if (mpd->map.m_len == 0)
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002361 mpd->first_page = page->index;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002362 mpd->next_page = page->index + 1;
Jan Karaf8bec372013-01-28 12:55:08 -05002363 /* Add all dirty buffers to mpd */
Jan Kara4e7ea812013-06-04 13:17:40 -04002364 lblk = ((ext4_lblk_t)page->index) <<
2365 (PAGE_CACHE_SHIFT - blkbits);
Jan Karaf8bec372013-01-28 12:55:08 -05002366 head = page_buffers(page);
Jan Kara5f1132b2013-08-17 10:02:33 -04002367 err = mpage_process_page_bufs(mpd, head, head, lblk);
2368 if (err <= 0)
Jan Kara4e7ea812013-06-04 13:17:40 -04002369 goto out;
Jan Kara5f1132b2013-08-17 10:02:33 -04002370 err = 0;
Ming Leiaeac5892013-10-17 18:56:16 -04002371 left--;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002372 }
2373 pagevec_release(&pvec);
2374 cond_resched();
2375 }
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002376 return 0;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002377out:
2378 pagevec_release(&pvec);
Jan Kara4e7ea812013-06-04 13:17:40 -04002379 return err;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002380}
2381
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002382static int __writepage(struct page *page, struct writeback_control *wbc,
2383 void *data)
2384{
2385 struct address_space *mapping = data;
2386 int ret = ext4_writepage(page, wbc);
2387 mapping_set_error(mapping, ret);
2388 return ret;
2389}
2390
2391static int ext4_writepages(struct address_space *mapping,
2392 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002393{
Jan Kara4e7ea812013-06-04 13:17:40 -04002394 pgoff_t writeback_index = 0;
2395 long nr_to_write = wbc->nr_to_write;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002396 int range_whole = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002397 int cycled = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002398 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002399 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002400 struct inode *inode = mapping->host;
Jan Kara6b523df2013-06-04 13:21:11 -04002401 int needed_blocks, rsv_blocks = 0, ret = 0;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002402 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Jan Kara4e7ea812013-06-04 13:17:40 -04002403 bool done;
Shaohua Li1bce63d2011-10-18 10:55:51 -04002404 struct blk_plug plug;
Theodore Ts'ocb530542013-07-01 08:12:40 -04002405 bool give_up_on_write = false;
Mingming Cao61628a32008-07-11 19:27:31 -04002406
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002407 trace_ext4_writepages(inode, wbc);
Theodore Ts'oba80b102009-01-03 20:03:21 -05002408
Mingming Cao61628a32008-07-11 19:27:31 -04002409 /*
2410 * No pages to write? This is mainly a kludge to avoid starting
2411 * a transaction for special inodes like journal inode on last iput()
2412 * because that could violate lock ordering on umount
2413 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002414 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Ming Leibbf023c2013-10-30 07:27:16 -04002415 goto out_writepages;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002416
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002417 if (ext4_should_journal_data(inode)) {
2418 struct blk_plug plug;
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002419
2420 blk_start_plug(&plug);
2421 ret = write_cache_pages(mapping, wbc, __writepage, mapping);
2422 blk_finish_plug(&plug);
Ming Leibbf023c2013-10-30 07:27:16 -04002423 goto out_writepages;
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002424 }
2425
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002426 /*
2427 * If the filesystem has aborted, it is read-only, so return
2428 * right away instead of dumping stack traces later on that
2429 * will obscure the real source of the problem. We test
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002430 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002431 * the latter could be true if the filesystem is mounted
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002432 * read-only, and in that case, ext4_writepages should
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002433 * *never* be called, so if that ever happens, we would want
2434 * the stack trace.
2435 */
Ming Leibbf023c2013-10-30 07:27:16 -04002436 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) {
2437 ret = -EROFS;
2438 goto out_writepages;
2439 }
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002440
Jan Kara6b523df2013-06-04 13:21:11 -04002441 if (ext4_should_dioread_nolock(inode)) {
2442 /*
Anatol Pomozov70261f52013-08-28 14:40:12 -04002443 * We may need to convert up to one extent per block in
Jan Kara6b523df2013-06-04 13:21:11 -04002444 * the page and we may dirty the inode.
2445 */
2446 rsv_blocks = 1 + (PAGE_CACHE_SIZE >> inode->i_blkbits);
2447 }
2448
Jan Kara4e7ea812013-06-04 13:17:40 -04002449 /*
2450 * If we have inline data and arrive here, it means that
2451 * we will soon create the block for the 1st page, so
2452 * we'd better clear the inline data here.
2453 */
2454 if (ext4_has_inline_data(inode)) {
2455 /* Just inode will be modified... */
2456 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
2457 if (IS_ERR(handle)) {
2458 ret = PTR_ERR(handle);
2459 goto out_writepages;
2460 }
2461 BUG_ON(ext4_test_inode_state(inode,
2462 EXT4_STATE_MAY_INLINE_DATA));
2463 ext4_destroy_inline_data(handle, inode);
2464 ext4_journal_stop(handle);
2465 }
2466
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002467 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2468 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002469
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002470 if (wbc->range_cyclic) {
Jan Kara4e7ea812013-06-04 13:17:40 -04002471 writeback_index = mapping->writeback_index;
2472 if (writeback_index)
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002473 cycled = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002474 mpd.first_page = writeback_index;
2475 mpd.last_page = -1;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002476 } else {
Jan Kara4e7ea812013-06-04 13:17:40 -04002477 mpd.first_page = wbc->range_start >> PAGE_CACHE_SHIFT;
2478 mpd.last_page = wbc->range_end >> PAGE_CACHE_SHIFT;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002479 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002480
Jan Kara4e7ea812013-06-04 13:17:40 -04002481 mpd.inode = inode;
2482 mpd.wbc = wbc;
2483 ext4_io_submit_init(&mpd.io_submit, wbc);
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002484retry:
Wu Fengguang6e6938b2010-06-06 10:38:15 -06002485 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Jan Kara4e7ea812013-06-04 13:17:40 -04002486 tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page);
2487 done = false;
Shaohua Li1bce63d2011-10-18 10:55:51 -04002488 blk_start_plug(&plug);
Jan Kara4e7ea812013-06-04 13:17:40 -04002489 while (!done && mpd.first_page <= mpd.last_page) {
2490 /* For each extent of pages we use new io_end */
2491 mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
2492 if (!mpd.io_submit.io_end) {
2493 ret = -ENOMEM;
2494 break;
2495 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002496
2497 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002498 * We have two constraints: We find one extent to map and we
2499 * must always write out whole page (makes a difference when
2500 * blocksize < pagesize) so that we don't block on IO when we
2501 * try to write out the rest of the page. Journalled mode is
2502 * not supported by delalloc.
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002503 */
2504 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04002505 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002506
Jan Kara4e7ea812013-06-04 13:17:40 -04002507 /* start a new transaction */
Jan Kara6b523df2013-06-04 13:21:11 -04002508 handle = ext4_journal_start_with_reserve(inode,
2509 EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks);
Mingming Cao61628a32008-07-11 19:27:31 -04002510 if (IS_ERR(handle)) {
2511 ret = PTR_ERR(handle);
Theodore Ts'o16939182009-09-26 17:43:59 -04002512 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
Curt Wohlgemuthfbe845d2010-05-16 13:00:00 -04002513 "%ld pages, ino %lu; err %d", __func__,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002514 wbc->nr_to_write, inode->i_ino, ret);
Jan Kara4e7ea812013-06-04 13:17:40 -04002515 /* Release allocated io_end */
2516 ext4_put_io_end(mpd.io_submit.io_end);
2517 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002518 }
Theodore Ts'of63e6002009-02-23 16:42:39 -05002519
Jan Kara4e7ea812013-06-04 13:17:40 -04002520 trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc);
2521 ret = mpage_prepare_extent_to_map(&mpd);
2522 if (!ret) {
2523 if (mpd.map.m_len)
Theodore Ts'ocb530542013-07-01 08:12:40 -04002524 ret = mpage_map_and_submit_extent(handle, &mpd,
2525 &give_up_on_write);
Jan Kara4e7ea812013-06-04 13:17:40 -04002526 else {
2527 /*
2528 * We scanned the whole range (or exhausted
2529 * nr_to_write), submitted what was mapped and
2530 * didn't find anything needing mapping. We are
2531 * done.
2532 */
2533 done = true;
2534 }
Theodore Ts'of63e6002009-02-23 16:42:39 -05002535 }
Mingming Cao61628a32008-07-11 19:27:31 -04002536 ext4_journal_stop(handle);
Jan Kara4e7ea812013-06-04 13:17:40 -04002537 /* Submit prepared bio */
2538 ext4_io_submit(&mpd.io_submit);
2539 /* Unlock pages we didn't use */
Theodore Ts'ocb530542013-07-01 08:12:40 -04002540 mpage_release_unused_pages(&mpd, give_up_on_write);
Jan Kara4e7ea812013-06-04 13:17:40 -04002541 /* Drop our io_end reference we got from init */
2542 ext4_put_io_end(mpd.io_submit.io_end);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002543
Jan Kara4e7ea812013-06-04 13:17:40 -04002544 if (ret == -ENOSPC && sbi->s_journal) {
2545 /*
2546 * Commit the transaction which would
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002547 * free blocks released in the transaction
2548 * and try again
2549 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002550 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002551 ret = 0;
Jan Kara4e7ea812013-06-04 13:17:40 -04002552 continue;
2553 }
2554 /* Fatal error - ENOMEM, EIO... */
2555 if (ret)
Mingming Cao61628a32008-07-11 19:27:31 -04002556 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002557 }
Shaohua Li1bce63d2011-10-18 10:55:51 -04002558 blk_finish_plug(&plug);
Jan Kara9c12a832013-09-16 08:24:26 -04002559 if (!ret && !cycled && wbc->nr_to_write > 0) {
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002560 cycled = 1;
Jan Kara4e7ea812013-06-04 13:17:40 -04002561 mpd.last_page = writeback_index - 1;
2562 mpd.first_page = 0;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002563 goto retry;
2564 }
Mingming Cao61628a32008-07-11 19:27:31 -04002565
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002566 /* Update index */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002567 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2568 /*
Jan Kara4e7ea812013-06-04 13:17:40 -04002569 * Set the writeback_index so that range_cyclic
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002570 * mode will write it back later
2571 */
Jan Kara4e7ea812013-06-04 13:17:40 -04002572 mapping->writeback_index = mpd.first_page;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002573
Mingming Cao61628a32008-07-11 19:27:31 -04002574out_writepages:
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002575 trace_ext4_writepages_result(inode, wbc, ret,
2576 nr_to_write - wbc->nr_to_write);
Mingming Cao61628a32008-07-11 19:27:31 -04002577 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002578}
2579
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002580static int ext4_nonda_switch(struct super_block *sb)
2581{
Eric Whitney5c1ff332013-04-09 09:27:31 -04002582 s64 free_clusters, dirty_clusters;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002583 struct ext4_sb_info *sbi = EXT4_SB(sb);
2584
2585 /*
2586 * switch to non delalloc mode if we are running low
2587 * on free block. The free block accounting via percpu
Eric Dumazet179f7eb2009-01-06 14:41:04 -08002588 * counters can get slightly wrong with percpu_counter_batch getting
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002589 * accumulated on each CPU without updating global counters
2590 * Delalloc need an accurate free block accounting. So switch
2591 * to non delalloc when we are near to error range.
2592 */
Eric Whitney5c1ff332013-04-09 09:27:31 -04002593 free_clusters =
2594 percpu_counter_read_positive(&sbi->s_freeclusters_counter);
2595 dirty_clusters =
2596 percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
Theodore Ts'o00d4e732012-09-19 22:42:36 -04002597 /*
2598 * Start pushing delalloc when 1/2 of free blocks are dirty.
2599 */
Eric Whitney5c1ff332013-04-09 09:27:31 -04002600 if (dirty_clusters && (free_clusters < 2 * dirty_clusters))
Miao Xie10ee27a2013-01-10 13:47:57 +08002601 try_to_writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE);
Theodore Ts'o00d4e732012-09-19 22:42:36 -04002602
Eric Whitney5c1ff332013-04-09 09:27:31 -04002603 if (2 * free_clusters < 3 * dirty_clusters ||
2604 free_clusters < (dirty_clusters + EXT4_FREECLUSTERS_WATERMARK)) {
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002605 /*
Eric Sandeenc8afb442009-12-23 07:58:12 -05002606 * free block count is less than 150% of dirty blocks
2607 * or free blocks is less than watermark
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002608 */
2609 return 1;
2610 }
2611 return 0;
2612}
2613
Alex Tomas64769242008-07-11 19:27:31 -04002614static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002615 loff_t pos, unsigned len, unsigned flags,
2616 struct page **pagep, void **fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04002617{
Eric Sandeen72b8ab92010-05-16 11:00:00 -04002618 int ret, retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002619 struct page *page;
2620 pgoff_t index;
Alex Tomas64769242008-07-11 19:27:31 -04002621 struct inode *inode = mapping->host;
2622 handle_t *handle;
2623
2624 index = pos >> PAGE_CACHE_SHIFT;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002625
2626 if (ext4_nonda_switch(inode->i_sb)) {
2627 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
2628 return ext4_write_begin(file, mapping, pos,
2629 len, flags, pagep, fsdata);
2630 }
2631 *fsdata = (void *)0;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002632 trace_ext4_da_write_begin(inode, pos, len, flags);
Tao Ma9c3569b2012-12-10 14:05:57 -05002633
2634 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2635 ret = ext4_da_write_inline_data_begin(mapping, inode,
2636 pos, len, flags,
2637 pagep, fsdata);
2638 if (ret < 0)
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002639 return ret;
2640 if (ret == 1)
2641 return 0;
Tao Ma9c3569b2012-12-10 14:05:57 -05002642 }
2643
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002644 /*
2645 * grab_cache_page_write_begin() can take a long time if the
2646 * system is thrashing due to memory pressure, or if the page
2647 * is being written back. So grab it first before we start
2648 * the transaction handle. This also allows us to allocate
2649 * the page (if needed) without using GFP_NOFS.
2650 */
2651retry_grab:
2652 page = grab_cache_page_write_begin(mapping, index, flags);
2653 if (!page)
2654 return -ENOMEM;
2655 unlock_page(page);
2656
Alex Tomas64769242008-07-11 19:27:31 -04002657 /*
2658 * With delayed allocation, we don't log the i_disksize update
2659 * if there is delayed block allocation. But we still need
2660 * to journalling the i_disksize update if writes to the end
2661 * of file which has an already mapped buffer.
2662 */
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002663retry_journal:
Theodore Ts'o9924a922013-02-08 21:59:22 -05002664 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 1);
Alex Tomas64769242008-07-11 19:27:31 -04002665 if (IS_ERR(handle)) {
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002666 page_cache_release(page);
2667 return PTR_ERR(handle);
Alex Tomas64769242008-07-11 19:27:31 -04002668 }
2669
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002670 lock_page(page);
2671 if (page->mapping != mapping) {
2672 /* The page got truncated from under us */
2673 unlock_page(page);
2674 page_cache_release(page);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002675 ext4_journal_stop(handle);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002676 goto retry_grab;
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002677 }
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002678 /* In case writeback began while the page was unlocked */
Dmitry Monakhov7afe5aa2013-08-28 14:30:47 -04002679 wait_for_stable_page(page);
Alex Tomas64769242008-07-11 19:27:31 -04002680
Christoph Hellwig6e1db882010-06-04 11:29:57 +02002681 ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
Alex Tomas64769242008-07-11 19:27:31 -04002682 if (ret < 0) {
2683 unlock_page(page);
2684 ext4_journal_stop(handle);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04002685 /*
2686 * block_write_begin may have instantiated a few blocks
2687 * outside i_size. Trim these off again. Don't need
2688 * i_size_read because we hold i_mutex.
2689 */
2690 if (pos + len > inode->i_size)
Jan Karab9a42072009-12-08 21:24:33 -05002691 ext4_truncate_failed_write(inode);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002692
2693 if (ret == -ENOSPC &&
2694 ext4_should_retry_alloc(inode->i_sb, &retries))
2695 goto retry_journal;
2696
2697 page_cache_release(page);
2698 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002699 }
2700
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002701 *pagep = page;
Alex Tomas64769242008-07-11 19:27:31 -04002702 return ret;
2703}
2704
Mingming Cao632eaea2008-07-11 19:27:31 -04002705/*
2706 * Check if we should update i_disksize
2707 * when write to the end of file but not require block allocation
2708 */
2709static int ext4_da_should_update_i_disksize(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002710 unsigned long offset)
Mingming Cao632eaea2008-07-11 19:27:31 -04002711{
2712 struct buffer_head *bh;
2713 struct inode *inode = page->mapping->host;
2714 unsigned int idx;
2715 int i;
2716
2717 bh = page_buffers(page);
2718 idx = offset >> inode->i_blkbits;
2719
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002720 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04002721 bh = bh->b_this_page;
2722
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002723 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
Mingming Cao632eaea2008-07-11 19:27:31 -04002724 return 0;
2725 return 1;
2726}
2727
Alex Tomas64769242008-07-11 19:27:31 -04002728static int ext4_da_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002729 struct address_space *mapping,
2730 loff_t pos, unsigned len, unsigned copied,
2731 struct page *page, void *fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04002732{
2733 struct inode *inode = mapping->host;
2734 int ret = 0, ret2;
2735 handle_t *handle = ext4_journal_current_handle();
2736 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04002737 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002738 int write_mode = (int)(unsigned long)fsdata;
2739
Theodore Ts'o74d553a2013-04-03 12:39:17 -04002740 if (write_mode == FALL_BACK_TO_NONDELALLOC)
2741 return ext4_write_end(file, mapping, pos,
2742 len, copied, page, fsdata);
Mingming Cao632eaea2008-07-11 19:27:31 -04002743
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002744 trace_ext4_da_write_end(inode, pos, len, copied);
Mingming Cao632eaea2008-07-11 19:27:31 -04002745 start = pos & (PAGE_CACHE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002746 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04002747
2748 /*
2749 * generic_write_end() will run mark_inode_dirty() if i_size
2750 * changes. So let's piggyback the i_disksize mark_inode_dirty
2751 * into that.
2752 */
Alex Tomas64769242008-07-11 19:27:31 -04002753 new_i_size = pos + copied;
Andrea Arcangeliea51d132011-12-13 21:41:15 -05002754 if (copied && new_i_size > EXT4_I(inode)->i_disksize) {
Tao Ma9c3569b2012-12-10 14:05:57 -05002755 if (ext4_has_inline_data(inode) ||
2756 ext4_da_should_update_i_disksize(page, end)) {
Mingming Cao632eaea2008-07-11 19:27:31 -04002757 down_write(&EXT4_I(inode)->i_data_sem);
Theodore Ts'of3b59292012-11-15 23:08:57 -05002758 if (new_i_size > EXT4_I(inode)->i_disksize)
Mingming Cao632eaea2008-07-11 19:27:31 -04002759 EXT4_I(inode)->i_disksize = new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04002760 up_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04002761 /* We need to mark inode dirty even if
2762 * new_i_size is less that inode->i_size
2763 * bu greater than i_disksize.(hint delalloc)
2764 */
2765 ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04002766 }
Mingming Cao632eaea2008-07-11 19:27:31 -04002767 }
Tao Ma9c3569b2012-12-10 14:05:57 -05002768
2769 if (write_mode != CONVERT_INLINE_DATA &&
2770 ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
2771 ext4_has_inline_data(inode))
2772 ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied,
2773 page);
2774 else
2775 ret2 = generic_write_end(file, mapping, pos, len, copied,
Alex Tomas64769242008-07-11 19:27:31 -04002776 page, fsdata);
Tao Ma9c3569b2012-12-10 14:05:57 -05002777
Alex Tomas64769242008-07-11 19:27:31 -04002778 copied = ret2;
2779 if (ret2 < 0)
2780 ret = ret2;
2781 ret2 = ext4_journal_stop(handle);
2782 if (!ret)
2783 ret = ret2;
2784
2785 return ret ? ret : copied;
2786}
2787
Lukas Czernerd47992f2013-05-21 23:17:23 -04002788static void ext4_da_invalidatepage(struct page *page, unsigned int offset,
2789 unsigned int length)
Alex Tomas64769242008-07-11 19:27:31 -04002790{
Alex Tomas64769242008-07-11 19:27:31 -04002791 /*
2792 * Drop reserved blocks
2793 */
2794 BUG_ON(!PageLocked(page));
2795 if (!page_has_buffers(page))
2796 goto out;
2797
Lukas Czernerca99fdd2013-05-21 23:25:01 -04002798 ext4_da_page_release_reservation(page, offset, length);
Alex Tomas64769242008-07-11 19:27:31 -04002799
2800out:
Lukas Czernerd47992f2013-05-21 23:17:23 -04002801 ext4_invalidatepage(page, offset, length);
Alex Tomas64769242008-07-11 19:27:31 -04002802
2803 return;
2804}
2805
Theodore Ts'occd25062009-02-26 01:04:07 -05002806/*
2807 * Force all delayed allocation blocks to be allocated for a given inode.
2808 */
2809int ext4_alloc_da_blocks(struct inode *inode)
2810{
Theodore Ts'ofb40ba02009-09-16 19:30:40 -04002811 trace_ext4_alloc_da_blocks(inode);
2812
Theodore Ts'occd25062009-02-26 01:04:07 -05002813 if (!EXT4_I(inode)->i_reserved_data_blocks &&
2814 !EXT4_I(inode)->i_reserved_meta_blocks)
2815 return 0;
2816
2817 /*
2818 * We do something simple for now. The filemap_flush() will
2819 * also start triggering a write of the data blocks, which is
2820 * not strictly speaking necessary (and for users of
2821 * laptop_mode, not even desirable). However, to do otherwise
2822 * would require replicating code paths in:
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002823 *
Theodore Ts'o20970ba2013-06-06 14:00:46 -04002824 * ext4_writepages() ->
Theodore Ts'occd25062009-02-26 01:04:07 -05002825 * write_cache_pages() ---> (via passed in callback function)
2826 * __mpage_da_writepage() -->
2827 * mpage_add_bh_to_extent()
2828 * mpage_da_map_blocks()
2829 *
2830 * The problem is that write_cache_pages(), located in
2831 * mm/page-writeback.c, marks pages clean in preparation for
2832 * doing I/O, which is not desirable if we're not planning on
2833 * doing I/O at all.
2834 *
2835 * We could call write_cache_pages(), and then redirty all of
Wu Fengguang380cf092010-11-11 19:23:29 +08002836 * the pages by calling redirty_page_for_writepage() but that
Theodore Ts'occd25062009-02-26 01:04:07 -05002837 * would be ugly in the extreme. So instead we would need to
2838 * replicate parts of the code in the above functions,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002839 * simplifying them because we wouldn't actually intend to
Theodore Ts'occd25062009-02-26 01:04:07 -05002840 * write out the pages, but rather only collect contiguous
2841 * logical block extents, call the multi-block allocator, and
2842 * then update the buffer heads with the block allocations.
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002843 *
Theodore Ts'occd25062009-02-26 01:04:07 -05002844 * For now, though, we'll cheat by calling filemap_flush(),
2845 * which will map the blocks, and start the I/O, but not
2846 * actually wait for the I/O to complete.
2847 */
2848 return filemap_flush(inode->i_mapping);
2849}
Alex Tomas64769242008-07-11 19:27:31 -04002850
2851/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002852 * bmap() is special. It gets used by applications such as lilo and by
2853 * the swapper to find the on-disk block of a specific piece of data.
2854 *
2855 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07002856 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002857 * filesystem and enables swap, then they may get a nasty shock when the
2858 * data getting swapped to that swapfile suddenly gets overwritten by
2859 * the original zero's written out previously to the journal and
2860 * awaiting writeback in the kernel's buffer cache.
2861 *
2862 * So, if we see any bmap calls here on a modified, data-journaled file,
2863 * take extra steps to flush any blocks which might be in the cache.
2864 */
Mingming Cao617ba132006-10-11 01:20:53 -07002865static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002866{
2867 struct inode *inode = mapping->host;
2868 journal_t *journal;
2869 int err;
2870
Tao Ma46c7f252012-12-10 14:04:52 -05002871 /*
2872 * We can get here for an inline file via the FIBMAP ioctl
2873 */
2874 if (ext4_has_inline_data(inode))
2875 return 0;
2876
Alex Tomas64769242008-07-11 19:27:31 -04002877 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
2878 test_opt(inode->i_sb, DELALLOC)) {
2879 /*
2880 * With delalloc we want to sync the file
2881 * so that we can make sure we allocate
2882 * blocks for file
2883 */
2884 filemap_write_and_wait(mapping);
2885 }
2886
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002887 if (EXT4_JOURNAL(inode) &&
2888 ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002889 /*
2890 * This is a REALLY heavyweight approach, but the use of
2891 * bmap on dirty files is expected to be extremely rare:
2892 * only if we run lilo or swapon on a freshly made file
2893 * do we expect this to happen.
2894 *
2895 * (bmap requires CAP_SYS_RAWIO so this does not
2896 * represent an unprivileged user DOS attack --- we'd be
2897 * in trouble if mortal users could trigger this path at
2898 * will.)
2899 *
Mingming Cao617ba132006-10-11 01:20:53 -07002900 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002901 * regular files. If somebody wants to bmap a directory
2902 * or symlink and gets confused because the buffer
2903 * hasn't yet been flushed to disk, they deserve
2904 * everything they get.
2905 */
2906
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002907 ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
Mingming Cao617ba132006-10-11 01:20:53 -07002908 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07002909 jbd2_journal_lock_updates(journal);
2910 err = jbd2_journal_flush(journal);
2911 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002912
2913 if (err)
2914 return 0;
2915 }
2916
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002917 return generic_block_bmap(mapping, block, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002918}
2919
Mingming Cao617ba132006-10-11 01:20:53 -07002920static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002921{
Tao Ma46c7f252012-12-10 14:04:52 -05002922 int ret = -EAGAIN;
2923 struct inode *inode = page->mapping->host;
2924
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04002925 trace_ext4_readpage(page);
Tao Ma46c7f252012-12-10 14:04:52 -05002926
2927 if (ext4_has_inline_data(inode))
2928 ret = ext4_readpage_inline(inode, page);
2929
2930 if (ret == -EAGAIN)
2931 return mpage_readpage(page, ext4_get_block);
2932
2933 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002934}
2935
2936static int
Mingming Cao617ba132006-10-11 01:20:53 -07002937ext4_readpages(struct file *file, struct address_space *mapping,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002938 struct list_head *pages, unsigned nr_pages)
2939{
Tao Ma46c7f252012-12-10 14:04:52 -05002940 struct inode *inode = mapping->host;
2941
2942 /* If the file has inline data, no need to do readpages. */
2943 if (ext4_has_inline_data(inode))
2944 return 0;
2945
Mingming Cao617ba132006-10-11 01:20:53 -07002946 return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002947}
2948
Lukas Czernerd47992f2013-05-21 23:17:23 -04002949static void ext4_invalidatepage(struct page *page, unsigned int offset,
2950 unsigned int length)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002951{
Lukas Czernerca99fdd2013-05-21 23:25:01 -04002952 trace_ext4_invalidatepage(page, offset, length);
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04002953
Jan Kara4520fb32012-12-25 13:28:54 -05002954 /* No journalling happens on data buffers when this function is used */
2955 WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page)));
2956
Lukas Czernerca99fdd2013-05-21 23:25:01 -04002957 block_invalidatepage(page, offset, length);
Jan Kara4520fb32012-12-25 13:28:54 -05002958}
2959
Jan Kara53e87262012-12-25 13:29:52 -05002960static int __ext4_journalled_invalidatepage(struct page *page,
Lukas Czernerca99fdd2013-05-21 23:25:01 -04002961 unsigned int offset,
2962 unsigned int length)
Jan Kara4520fb32012-12-25 13:28:54 -05002963{
2964 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
2965
Lukas Czernerca99fdd2013-05-21 23:25:01 -04002966 trace_ext4_journalled_invalidatepage(page, offset, length);
Jan Kara4520fb32012-12-25 13:28:54 -05002967
Jiaying Zhang744692d2010-03-04 16:14:02 -05002968 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002969 * If it's a full truncate we just forget about the pending dirtying
2970 */
Lukas Czernerca99fdd2013-05-21 23:25:01 -04002971 if (offset == 0 && length == PAGE_CACHE_SIZE)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002972 ClearPageChecked(page);
2973
Lukas Czernerca99fdd2013-05-21 23:25:01 -04002974 return jbd2_journal_invalidatepage(journal, page, offset, length);
Jan Kara53e87262012-12-25 13:29:52 -05002975}
2976
2977/* Wrapper for aops... */
2978static void ext4_journalled_invalidatepage(struct page *page,
Lukas Czernerd47992f2013-05-21 23:17:23 -04002979 unsigned int offset,
2980 unsigned int length)
Jan Kara53e87262012-12-25 13:29:52 -05002981{
Lukas Czernerca99fdd2013-05-21 23:25:01 -04002982 WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002983}
2984
Mingming Cao617ba132006-10-11 01:20:53 -07002985static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002986{
Mingming Cao617ba132006-10-11 01:20:53 -07002987 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002988
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04002989 trace_ext4_releasepage(page);
2990
Jan Karae1c36592013-03-10 22:19:00 -04002991 /* Page has dirty journalled data -> cannot release */
2992 if (PageChecked(page))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002993 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05002994 if (journal)
2995 return jbd2_journal_try_to_free_buffers(journal, page, wait);
2996 else
2997 return try_to_free_buffers(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002998}
2999
3000/*
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003001 * ext4_get_block used when preparing for a DIO write or buffer write.
3002 * We allocate an uinitialized extent if blocks haven't been allocated.
3003 * The extent will be converted to initialized after the IO is complete.
3004 */
Tao Maf19d5872012-12-10 14:05:51 -05003005int ext4_get_block_write(struct inode *inode, sector_t iblock,
Mingming Cao4c0425f2009-09-28 15:48:41 -04003006 struct buffer_head *bh_result, int create)
3007{
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003008 ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n",
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003009 inode->i_ino, create);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04003010 return _ext4_get_block(inode, iblock, bh_result,
3011 EXT4_GET_BLOCKS_IO_CREATE_EXT);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003012}
3013
Zheng Liu729f52c2012-07-09 16:29:29 -04003014static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock,
Anatol Pomozov8b0f1652012-11-08 15:07:16 -05003015 struct buffer_head *bh_result, int create)
Zheng Liu729f52c2012-07-09 16:29:29 -04003016{
Anatol Pomozov8b0f1652012-11-08 15:07:16 -05003017 ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n",
3018 inode->i_ino, create);
3019 return _ext4_get_block(inode, iblock, bh_result,
3020 EXT4_GET_BLOCKS_NO_LOCK);
Zheng Liu729f52c2012-07-09 16:29:29 -04003021}
3022
Mingming Cao4c0425f2009-09-28 15:48:41 -04003023static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
Christoph Hellwig7b7a8662013-09-04 15:04:39 +02003024 ssize_t size, void *private)
Mingming Cao4c0425f2009-09-28 15:48:41 -04003025{
3026 ext4_io_end_t *io_end = iocb->private;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003027
Jan Kara97a851e2013-06-04 11:58:58 -04003028 /* if not async direct IO just return */
Christoph Hellwig7b7a8662013-09-04 15:04:39 +02003029 if (!io_end)
Jan Kara97a851e2013-06-04 11:58:58 -04003030 return;
Mingming4b70df12009-11-03 14:44:54 -05003031
Zheng Liu88635ca2011-12-28 19:00:25 -05003032 ext_debug("ext4_end_io_dio(): io_end 0x%p "
Joe Perchesace36ad2012-03-19 23:11:43 -04003033 "for inode %lu, iocb 0x%p, offset %llu, size %zd\n",
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003034 iocb->private, io_end->inode->i_ino, iocb, offset,
3035 size);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003036
Theodore Ts'ob5a7e972011-12-12 10:53:02 -05003037 iocb->private = NULL;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003038 io_end->offset = offset;
3039 io_end->size = size;
Christoph Hellwig7b7a8662013-09-04 15:04:39 +02003040 ext4_put_io_end(io_end);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003041}
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05003042
Mingming Cao4c0425f2009-09-28 15:48:41 -04003043/*
3044 * For ext4 extent files, ext4 will do direct-io write to holes,
3045 * preallocated extents, and those write extend the file, no need to
3046 * fall back to buffered IO.
3047 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003048 * For holes, we fallocate those blocks, mark them as uninitialized
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003049 * If those blocks were preallocated, we mark sure they are split, but
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04003050 * still keep the range to write as uninitialized.
Mingming Cao4c0425f2009-09-28 15:48:41 -04003051 *
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003052 * The unwritten extents will be converted to written when DIO is completed.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003053 * For async direct IO, since the IO may still pending when return, we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003054 * set up an end_io call back function, which will do the conversion
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003055 * when async direct IO completed.
Mingming Cao4c0425f2009-09-28 15:48:41 -04003056 *
3057 * If the O_DIRECT write will extend the file then add this inode to the
3058 * orphan list. So recovery will truncate it back to the original size
3059 * if the machine crashes during the write.
3060 *
3061 */
3062static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
3063 const struct iovec *iov, loff_t offset,
3064 unsigned long nr_segs)
3065{
3066 struct file *file = iocb->ki_filp;
3067 struct inode *inode = file->f_mapping->host;
3068 ssize_t ret;
3069 size_t count = iov_length(iov, nr_segs);
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003070 int overwrite = 0;
3071 get_block_t *get_block_func = NULL;
3072 int dio_flags = 0;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003073 loff_t final_size = offset + count;
Jan Kara97a851e2013-06-04 11:58:58 -04003074 ext4_io_end_t *io_end = NULL;
Zheng Liu729f52c2012-07-09 16:29:29 -04003075
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003076 /* Use the old path for reads and writes beyond i_size. */
3077 if (rw != WRITE || final_size > inode->i_size)
3078 return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
Zheng Liu4bd809d2012-07-22 20:19:31 -04003079
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003080 BUG_ON(iocb->private == NULL);
Zheng Liu4bd809d2012-07-22 20:19:31 -04003081
Jan Karae8340392013-06-04 14:27:38 -04003082 /*
3083 * Make all waiters for direct IO properly wait also for extent
3084 * conversion. This also disallows race between truncate() and
3085 * overwrite DIO as i_dio_count needs to be incremented under i_mutex.
3086 */
3087 if (rw == WRITE)
3088 atomic_inc(&inode->i_dio_count);
3089
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003090 /* If we do a overwrite dio, i_mutex locking can be released */
3091 overwrite = *((int *)iocb->private);
Zheng Liu4bd809d2012-07-22 20:19:31 -04003092
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003093 if (overwrite) {
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003094 down_read(&EXT4_I(inode)->i_data_sem);
3095 mutex_unlock(&inode->i_mutex);
Mingming Cao4c0425f2009-09-28 15:48:41 -04003096 }
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003097
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003098 /*
3099 * We could direct write to holes and fallocate.
3100 *
3101 * Allocated blocks to fill the hole are marked as
3102 * uninitialized to prevent parallel buffered read to expose
3103 * the stale data before DIO complete the data IO.
3104 *
3105 * As to previously fallocated extents, ext4 get_block will
3106 * just simply mark the buffer mapped but still keep the
3107 * extents uninitialized.
3108 *
3109 * For non AIO case, we will convert those unwritten extents
3110 * to written after return back from blockdev_direct_IO.
3111 *
3112 * For async DIO, the conversion needs to be deferred when the
3113 * IO is completed. The ext4 end_io callback function will be
3114 * called to take care of the conversion work. Here for async
3115 * case, we allocate an io_end structure to hook to the iocb.
3116 */
3117 iocb->private = NULL;
3118 ext4_inode_aio_set(inode, NULL);
3119 if (!is_sync_kiocb(iocb)) {
Jan Kara97a851e2013-06-04 11:58:58 -04003120 io_end = ext4_init_io_end(inode, GFP_NOFS);
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003121 if (!io_end) {
3122 ret = -ENOMEM;
3123 goto retake_lock;
3124 }
Jan Kara97a851e2013-06-04 11:58:58 -04003125 /*
3126 * Grab reference for DIO. Will be dropped in ext4_end_io_dio()
3127 */
3128 iocb->private = ext4_get_io_end(io_end);
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003129 /*
3130 * we save the io structure for current async direct
3131 * IO, so that later ext4_map_blocks() could flag the
3132 * io structure whether there is a unwritten extents
3133 * needs to be converted when IO is completed.
3134 */
3135 ext4_inode_aio_set(inode, io_end);
3136 }
3137
3138 if (overwrite) {
3139 get_block_func = ext4_get_block_write_nolock;
3140 } else {
3141 get_block_func = ext4_get_block_write;
3142 dio_flags = DIO_LOCKING;
3143 }
3144 ret = __blockdev_direct_IO(rw, iocb, inode,
3145 inode->i_sb->s_bdev, iov,
3146 offset, nr_segs,
3147 get_block_func,
3148 ext4_end_io_dio,
3149 NULL,
3150 dio_flags);
3151
Theodore Ts'oa5499842013-05-11 19:07:42 -04003152 /*
Jan Kara97a851e2013-06-04 11:58:58 -04003153 * Put our reference to io_end. This can free the io_end structure e.g.
3154 * in sync IO case or in case of error. It can even perform extent
3155 * conversion if all bios we submitted finished before we got here.
3156 * Note that in that case iocb->private can be already set to NULL
3157 * here.
Theodore Ts'oa5499842013-05-11 19:07:42 -04003158 */
Jan Kara97a851e2013-06-04 11:58:58 -04003159 if (io_end) {
3160 ext4_inode_aio_set(inode, NULL);
3161 ext4_put_io_end(io_end);
3162 /*
3163 * When no IO was submitted ext4_end_io_dio() was not
3164 * called so we have to put iocb's reference.
3165 */
3166 if (ret <= 0 && ret != -EIOCBQUEUED && iocb->private) {
3167 WARN_ON(iocb->private != io_end);
3168 WARN_ON(io_end->flag & EXT4_IO_END_UNWRITTEN);
Jan Kara97a851e2013-06-04 11:58:58 -04003169 ext4_put_io_end(io_end);
3170 iocb->private = NULL;
3171 }
3172 }
3173 if (ret > 0 && !overwrite && ext4_test_inode_state(inode,
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003174 EXT4_STATE_DIO_UNWRITTEN)) {
3175 int err;
3176 /*
3177 * for non AIO case, since the IO is already
3178 * completed, we could do the conversion right here
3179 */
Jan Kara6b523df2013-06-04 13:21:11 -04003180 err = ext4_convert_unwritten_extents(NULL, inode,
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003181 offset, ret);
3182 if (err < 0)
3183 ret = err;
3184 ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
3185 }
3186
3187retake_lock:
Jan Karae8340392013-06-04 14:27:38 -04003188 if (rw == WRITE)
3189 inode_dio_done(inode);
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003190 /* take i_mutex locking again if we do a ovewrite dio */
3191 if (overwrite) {
Theodore Ts'o69c499d2012-11-29 21:13:48 -05003192 up_read(&EXT4_I(inode)->i_data_sem);
3193 mutex_lock(&inode->i_mutex);
3194 }
3195
3196 return ret;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003197}
3198
3199static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
3200 const struct iovec *iov, loff_t offset,
3201 unsigned long nr_segs)
3202{
3203 struct file *file = iocb->ki_filp;
3204 struct inode *inode = file->f_mapping->host;
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003205 ssize_t ret;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003206
Theodore Ts'o84ebd792011-08-31 11:56:51 -04003207 /*
3208 * If we are doing data journalling we don't support O_DIRECT
3209 */
3210 if (ext4_should_journal_data(inode))
3211 return 0;
3212
Tao Ma46c7f252012-12-10 14:04:52 -05003213 /* Let buffer I/O handle the inline data case. */
3214 if (ext4_has_inline_data(inode))
3215 return 0;
3216
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003217 trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003218 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003219 ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs);
3220 else
3221 ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
3222 trace_ext4_direct_IO_exit(inode, offset,
3223 iov_length(iov, nr_segs), rw, ret);
3224 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
Lukas Czernerd863dc32013-05-27 23:32:35 -04003315/*
3316 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
3317 * up to the end of the block which corresponds to `from'.
3318 * This required during truncate. We need to physically zero the tail end
3319 * of that block so it doesn't yield old data if the file is later grown.
3320 */
3321int ext4_block_truncate_page(handle_t *handle,
3322 struct address_space *mapping, loff_t from)
3323{
3324 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3325 unsigned length;
3326 unsigned blocksize;
3327 struct inode *inode = mapping->host;
3328
3329 blocksize = inode->i_sb->s_blocksize;
3330 length = blocksize - (offset & (blocksize - 1));
3331
3332 return ext4_block_zero_page_range(handle, mapping, from, length);
3333}
3334
3335/*
3336 * ext4_block_zero_page_range() zeros out a mapping of length 'length'
3337 * starting from file offset 'from'. The range to be zero'd must
3338 * be contained with in one block. If the specified range exceeds
3339 * the end of the block it will be shortened to end of the block
3340 * that cooresponds to 'from'
3341 */
3342int ext4_block_zero_page_range(handle_t *handle,
3343 struct address_space *mapping, loff_t from, loff_t length)
3344{
3345 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
3346 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3347 unsigned blocksize, max, pos;
3348 ext4_lblk_t iblock;
3349 struct inode *inode = mapping->host;
3350 struct buffer_head *bh;
3351 struct page *page;
3352 int err = 0;
3353
3354 page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
3355 mapping_gfp_mask(mapping) & ~__GFP_FS);
3356 if (!page)
3357 return -ENOMEM;
3358
3359 blocksize = inode->i_sb->s_blocksize;
3360 max = blocksize - (offset & (blocksize - 1));
3361
3362 /*
3363 * correct length if it does not fall between
3364 * 'from' and the end of the block
3365 */
3366 if (length > max || length < 0)
3367 length = max;
3368
3369 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
3370
3371 if (!page_has_buffers(page))
3372 create_empty_buffers(page, blocksize, 0);
3373
3374 /* Find the buffer that contains "offset" */
3375 bh = page_buffers(page);
3376 pos = blocksize;
3377 while (offset >= pos) {
3378 bh = bh->b_this_page;
3379 iblock++;
3380 pos += blocksize;
3381 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003382 if (buffer_freed(bh)) {
3383 BUFFER_TRACE(bh, "freed: skip");
3384 goto unlock;
3385 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003386 if (!buffer_mapped(bh)) {
3387 BUFFER_TRACE(bh, "unmapped");
3388 ext4_get_block(inode, iblock, bh, 0);
3389 /* unmapped? It's a hole - nothing to do */
3390 if (!buffer_mapped(bh)) {
3391 BUFFER_TRACE(bh, "still unmapped");
3392 goto unlock;
3393 }
3394 }
3395
3396 /* Ok, it's mapped. Make sure it's up-to-date */
3397 if (PageUptodate(page))
3398 set_buffer_uptodate(bh);
3399
3400 if (!buffer_uptodate(bh)) {
3401 err = -EIO;
3402 ll_rw_block(READ, 1, &bh);
3403 wait_on_buffer(bh);
3404 /* Uhhuh. Read error. Complain and punt. */
3405 if (!buffer_uptodate(bh))
3406 goto unlock;
3407 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003408 if (ext4_should_journal_data(inode)) {
3409 BUFFER_TRACE(bh, "get write access");
3410 err = ext4_journal_get_write_access(handle, bh);
3411 if (err)
3412 goto unlock;
3413 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003414 zero_user(page, offset, length);
Lukas Czernerd863dc32013-05-27 23:32:35 -04003415 BUFFER_TRACE(bh, "zeroed end of block");
3416
Lukas Czernerd863dc32013-05-27 23:32:35 -04003417 if (ext4_should_journal_data(inode)) {
3418 err = ext4_handle_dirty_metadata(handle, inode, bh);
Lukas Czerner0713ed02013-05-27 23:32:35 -04003419 } else {
jon ernst353eefd2013-07-01 08:12:39 -04003420 err = 0;
Lukas Czernerd863dc32013-05-27 23:32:35 -04003421 mark_buffer_dirty(bh);
Lukas Czerner0713ed02013-05-27 23:32:35 -04003422 if (ext4_test_inode_state(inode, EXT4_STATE_ORDERED_MODE))
3423 err = ext4_jbd2_file_inode(handle, inode);
3424 }
Lukas Czernerd863dc32013-05-27 23:32:35 -04003425
3426unlock:
3427 unlock_page(page);
3428 page_cache_release(page);
3429 return err;
3430}
3431
Lukas Czernera87dd182013-05-27 23:32:35 -04003432int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
3433 loff_t lstart, loff_t length)
3434{
3435 struct super_block *sb = inode->i_sb;
3436 struct address_space *mapping = inode->i_mapping;
Lukas Czernere1be3a92013-07-01 08:12:39 -04003437 unsigned partial_start, partial_end;
Lukas Czernera87dd182013-05-27 23:32:35 -04003438 ext4_fsblk_t start, end;
3439 loff_t byte_end = (lstart + length - 1);
3440 int err = 0;
3441
Lukas Czernere1be3a92013-07-01 08:12:39 -04003442 partial_start = lstart & (sb->s_blocksize - 1);
3443 partial_end = byte_end & (sb->s_blocksize - 1);
3444
Lukas Czernera87dd182013-05-27 23:32:35 -04003445 start = lstart >> sb->s_blocksize_bits;
3446 end = byte_end >> sb->s_blocksize_bits;
3447
3448 /* Handle partial zero within the single block */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003449 if (start == end &&
3450 (partial_start || (partial_end != sb->s_blocksize - 1))) {
Lukas Czernera87dd182013-05-27 23:32:35 -04003451 err = ext4_block_zero_page_range(handle, mapping,
3452 lstart, length);
3453 return err;
3454 }
3455 /* Handle partial zero out on the start of the range */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003456 if (partial_start) {
Lukas Czernera87dd182013-05-27 23:32:35 -04003457 err = ext4_block_zero_page_range(handle, mapping,
3458 lstart, sb->s_blocksize);
3459 if (err)
3460 return err;
3461 }
3462 /* Handle partial zero out on the end of the range */
Lukas Czernere1be3a92013-07-01 08:12:39 -04003463 if (partial_end != sb->s_blocksize - 1)
Lukas Czernera87dd182013-05-27 23:32:35 -04003464 err = ext4_block_zero_page_range(handle, mapping,
Lukas Czernere1be3a92013-07-01 08:12:39 -04003465 byte_end - partial_end,
3466 partial_end + 1);
Lukas Czernera87dd182013-05-27 23:32:35 -04003467 return err;
3468}
3469
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003470int ext4_can_truncate(struct inode *inode)
3471{
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003472 if (S_ISREG(inode->i_mode))
3473 return 1;
3474 if (S_ISDIR(inode->i_mode))
3475 return 1;
3476 if (S_ISLNK(inode->i_mode))
3477 return !ext4_inode_is_fast_symlink(inode);
3478 return 0;
3479}
3480
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003481/*
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003482 * ext4_punch_hole: punches a hole in a file by releaseing the blocks
3483 * associated with the given offset and length
3484 *
3485 * @inode: File inode
3486 * @offset: The offset where the hole will begin
3487 * @len: The length of the hole
3488 *
Anatol Pomozov4907cb72012-09-01 10:31:09 -07003489 * Returns: 0 on success or negative on failure
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003490 */
3491
Ashish Sangwanaeb28172013-07-01 08:12:38 -04003492int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length)
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003493{
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003494 struct super_block *sb = inode->i_sb;
3495 ext4_lblk_t first_block, stop_block;
3496 struct address_space *mapping = inode->i_mapping;
Lukas Czernera87dd182013-05-27 23:32:35 -04003497 loff_t first_block_offset, last_block_offset;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003498 handle_t *handle;
3499 unsigned int credits;
3500 int ret = 0;
3501
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003502 if (!S_ISREG(inode->i_mode))
Allison Henderson73355192012-03-21 22:23:31 -04003503 return -EOPNOTSUPP;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003504
Zheng Liuaaddea82013-01-16 20:21:26 -05003505 trace_ext4_punch_hole(inode, offset, length);
3506
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003507 /*
3508 * Write out all dirty pages to avoid race conditions
3509 * Then release them.
3510 */
3511 if (mapping->nrpages && mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) {
3512 ret = filemap_write_and_wait_range(mapping, offset,
3513 offset + length - 1);
3514 if (ret)
3515 return ret;
3516 }
3517
3518 mutex_lock(&inode->i_mutex);
3519 /* It's not possible punch hole on append only file */
3520 if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) {
3521 ret = -EPERM;
3522 goto out_mutex;
3523 }
3524 if (IS_SWAPFILE(inode)) {
3525 ret = -ETXTBSY;
3526 goto out_mutex;
3527 }
3528
3529 /* No need to punch hole beyond i_size */
3530 if (offset >= inode->i_size)
3531 goto out_mutex;
3532
3533 /*
3534 * If the hole extends beyond i_size, set the hole
3535 * to end after the page that contains i_size
3536 */
3537 if (offset + length > inode->i_size) {
3538 length = inode->i_size +
3539 PAGE_CACHE_SIZE - (inode->i_size & (PAGE_CACHE_SIZE - 1)) -
3540 offset;
3541 }
3542
Jan Karaa3612932013-08-16 21:19:41 -04003543 if (offset & (sb->s_blocksize - 1) ||
3544 (offset + length) & (sb->s_blocksize - 1)) {
3545 /*
3546 * Attach jinode to inode for jbd2 if we do any zeroing of
3547 * partial block
3548 */
3549 ret = ext4_inode_attach_jinode(inode);
3550 if (ret < 0)
3551 goto out_mutex;
3552
3553 }
3554
Lukas Czernera87dd182013-05-27 23:32:35 -04003555 first_block_offset = round_up(offset, sb->s_blocksize);
3556 last_block_offset = round_down((offset + length), sb->s_blocksize) - 1;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003557
Lukas Czernera87dd182013-05-27 23:32:35 -04003558 /* Now release the pages and zero block aligned part of pages*/
3559 if (last_block_offset > first_block_offset)
3560 truncate_pagecache_range(inode, first_block_offset,
3561 last_block_offset);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003562
3563 /* Wait all existing dio workers, newcomers will block on i_mutex */
3564 ext4_inode_block_unlocked_dio(inode);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003565 inode_dio_wait(inode);
3566
3567 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3568 credits = ext4_writepage_trans_blocks(inode);
3569 else
3570 credits = ext4_blocks_for_truncate(inode);
3571 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
3572 if (IS_ERR(handle)) {
3573 ret = PTR_ERR(handle);
3574 ext4_std_error(sb, ret);
3575 goto out_dio;
3576 }
3577
Lukas Czernera87dd182013-05-27 23:32:35 -04003578 ret = ext4_zero_partial_blocks(handle, inode, offset,
3579 length);
3580 if (ret)
3581 goto out_stop;
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003582
3583 first_block = (offset + sb->s_blocksize - 1) >>
3584 EXT4_BLOCK_SIZE_BITS(sb);
3585 stop_block = (offset + length) >> EXT4_BLOCK_SIZE_BITS(sb);
3586
3587 /* If there are no blocks to remove, return now */
3588 if (first_block >= stop_block)
3589 goto out_stop;
3590
3591 down_write(&EXT4_I(inode)->i_data_sem);
3592 ext4_discard_preallocations(inode);
3593
3594 ret = ext4_es_remove_extent(inode, first_block,
3595 stop_block - first_block);
3596 if (ret) {
3597 up_write(&EXT4_I(inode)->i_data_sem);
3598 goto out_stop;
3599 }
3600
3601 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3602 ret = ext4_ext_remove_space(inode, first_block,
3603 stop_block - 1);
3604 else
3605 ret = ext4_free_hole_blocks(handle, inode, first_block,
3606 stop_block);
3607
3608 ext4_discard_preallocations(inode);
Theodore Ts'o819c4922013-04-03 12:47:17 -04003609 up_write(&EXT4_I(inode)->i_data_sem);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003610 if (IS_SYNC(inode))
3611 ext4_handle_sync(handle);
Theodore Ts'o26a4c0c2013-04-03 12:45:17 -04003612 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3613 ext4_mark_inode_dirty(handle, inode);
3614out_stop:
3615 ext4_journal_stop(handle);
3616out_dio:
3617 ext4_inode_resume_unlocked_dio(inode);
3618out_mutex:
3619 mutex_unlock(&inode->i_mutex);
3620 return ret;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003621}
3622
Jan Karaa3612932013-08-16 21:19:41 -04003623int ext4_inode_attach_jinode(struct inode *inode)
3624{
3625 struct ext4_inode_info *ei = EXT4_I(inode);
3626 struct jbd2_inode *jinode;
3627
3628 if (ei->jinode || !EXT4_SB(inode->i_sb)->s_journal)
3629 return 0;
3630
3631 jinode = jbd2_alloc_inode(GFP_KERNEL);
3632 spin_lock(&inode->i_lock);
3633 if (!ei->jinode) {
3634 if (!jinode) {
3635 spin_unlock(&inode->i_lock);
3636 return -ENOMEM;
3637 }
3638 ei->jinode = jinode;
3639 jbd2_journal_init_jbd_inode(ei->jinode, inode);
3640 jinode = NULL;
3641 }
3642 spin_unlock(&inode->i_lock);
3643 if (unlikely(jinode != NULL))
3644 jbd2_free_inode(jinode);
3645 return 0;
3646}
3647
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003648/*
Mingming Cao617ba132006-10-11 01:20:53 -07003649 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003650 *
Mingming Cao617ba132006-10-11 01:20:53 -07003651 * We block out ext4_get_block() block instantiations across the entire
3652 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003653 * simultaneously on behalf of the same inode.
3654 *
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003655 * As we work through the truncate and commit bits of it to the journal there
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003656 * is one core, guiding principle: the file's tree must always be consistent on
3657 * disk. We must be able to restart the truncate after a crash.
3658 *
3659 * The file's tree may be transiently inconsistent in memory (although it
3660 * probably isn't), but whenever we close off and commit a journal transaction,
3661 * the contents of (the filesystem + the journal) must be consistent and
3662 * restartable. It's pretty simple, really: bottom up, right to left (although
3663 * left-to-right works OK too).
3664 *
3665 * Note that at recovery time, journal replay occurs *before* the restart of
3666 * truncate against the orphan inode list.
3667 *
3668 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07003669 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003670 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07003671 * ext4_truncate() to have another go. So there will be instantiated blocks
3672 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003673 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07003674 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003675 */
Mingming Cao617ba132006-10-11 01:20:53 -07003676void ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003677{
Theodore Ts'o819c4922013-04-03 12:47:17 -04003678 struct ext4_inode_info *ei = EXT4_I(inode);
3679 unsigned int credits;
3680 handle_t *handle;
3681 struct address_space *mapping = inode->i_mapping;
Theodore Ts'o819c4922013-04-03 12:47:17 -04003682
Theodore Ts'o19b5ef62013-04-03 21:58:52 -04003683 /*
3684 * There is a possibility that we're either freeing the inode
3685 * or it completely new indode. In those cases we might not
3686 * have i_mutex locked because it's not necessary.
3687 */
3688 if (!(inode->i_state & (I_NEW|I_FREEING)))
3689 WARN_ON(!mutex_is_locked(&inode->i_mutex));
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003690 trace_ext4_truncate_enter(inode);
3691
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003692 if (!ext4_can_truncate(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003693 return;
3694
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003695 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003696
Theodore Ts'o5534fb52009-09-17 09:34:16 -04003697 if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003698 ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
Theodore Ts'o7d8f9f72009-02-24 08:21:14 -05003699
Tao Maaef1c852012-12-10 14:06:02 -05003700 if (ext4_has_inline_data(inode)) {
3701 int has_inline = 1;
3702
3703 ext4_inline_data_truncate(inode, &has_inline);
3704 if (has_inline)
3705 return;
3706 }
3707
Jan Karaa3612932013-08-16 21:19:41 -04003708 /* If we zero-out tail of the page, we have to create jinode for jbd2 */
3709 if (inode->i_size & (inode->i_sb->s_blocksize - 1)) {
3710 if (ext4_inode_attach_jinode(inode) < 0)
3711 return;
3712 }
3713
Amir Goldsteinff9893d2011-06-27 16:36:31 -04003714 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Theodore Ts'o819c4922013-04-03 12:47:17 -04003715 credits = ext4_writepage_trans_blocks(inode);
Amir Goldsteinff9893d2011-06-27 16:36:31 -04003716 else
Theodore Ts'o819c4922013-04-03 12:47:17 -04003717 credits = ext4_blocks_for_truncate(inode);
3718
3719 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits);
3720 if (IS_ERR(handle)) {
3721 ext4_std_error(inode->i_sb, PTR_ERR(handle));
3722 return;
3723 }
3724
Lukas Czernereb3544c2013-05-27 23:32:35 -04003725 if (inode->i_size & (inode->i_sb->s_blocksize - 1))
3726 ext4_block_truncate_page(handle, mapping, inode->i_size);
Theodore Ts'o819c4922013-04-03 12:47:17 -04003727
3728 /*
3729 * We add the inode to the orphan list, so that if this
3730 * truncate spans multiple transactions, and we crash, we will
3731 * resume the truncate when the filesystem recovers. It also
3732 * marks the inode dirty, to catch the new size.
3733 *
3734 * Implication: the file must always be in a sane, consistent
3735 * truncatable state while each transaction commits.
3736 */
3737 if (ext4_orphan_add(handle, inode))
3738 goto out_stop;
3739
3740 down_write(&EXT4_I(inode)->i_data_sem);
3741
3742 ext4_discard_preallocations(inode);
3743
3744 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3745 ext4_ext_truncate(handle, inode);
3746 else
3747 ext4_ind_truncate(handle, inode);
3748
3749 up_write(&ei->i_data_sem);
3750
3751 if (IS_SYNC(inode))
3752 ext4_handle_sync(handle);
3753
3754out_stop:
3755 /*
3756 * If this was a simple ftruncate() and the file will remain alive,
3757 * then we need to clear up the orphan record which we created above.
3758 * However, if this was a real unlink then we were called by
3759 * ext4_delete_inode(), and we allow that function to clean up the
3760 * orphan info for us.
3761 */
3762 if (inode->i_nlink)
3763 ext4_orphan_del(handle, inode);
3764
3765 inode->i_mtime = inode->i_ctime = ext4_current_time(inode);
3766 ext4_mark_inode_dirty(handle, inode);
3767 ext4_journal_stop(handle);
Alex Tomasa86c6182006-10-11 01:21:03 -07003768
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003769 trace_ext4_truncate_exit(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003770}
3771
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003772/*
Mingming Cao617ba132006-10-11 01:20:53 -07003773 * ext4_get_inode_loc returns with an extra refcount against the inode's
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003774 * underlying buffer_head on success. If 'in_mem' is true, we have all
3775 * data in memory that is needed to recreate the on-disk version of this
3776 * inode.
3777 */
Mingming Cao617ba132006-10-11 01:20:53 -07003778static int __ext4_get_inode_loc(struct inode *inode,
3779 struct ext4_iloc *iloc, int in_mem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003780{
Theodore Ts'o240799c2008-10-09 23:53:47 -04003781 struct ext4_group_desc *gdp;
3782 struct buffer_head *bh;
3783 struct super_block *sb = inode->i_sb;
3784 ext4_fsblk_t block;
3785 int inodes_per_block, inode_offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003786
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003787 iloc->bh = NULL;
Theodore Ts'o240799c2008-10-09 23:53:47 -04003788 if (!ext4_valid_inum(sb, inode->i_ino))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003789 return -EIO;
3790
Theodore Ts'o240799c2008-10-09 23:53:47 -04003791 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
3792 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
3793 if (!gdp)
3794 return -EIO;
3795
3796 /*
3797 * Figure out the offset within the block group inode table
3798 */
Tao Ma00d09882011-05-09 10:26:41 -04003799 inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
Theodore Ts'o240799c2008-10-09 23:53:47 -04003800 inode_offset = ((inode->i_ino - 1) %
3801 EXT4_INODES_PER_GROUP(sb));
3802 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
3803 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
3804
3805 bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05003806 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -05003807 return -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003808 if (!buffer_uptodate(bh)) {
3809 lock_buffer(bh);
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04003810
3811 /*
3812 * If the buffer has the write error flag, we have failed
3813 * to write out another inode in the same block. In this
3814 * case, we don't have to read the block because we may
3815 * read the old inode data successfully.
3816 */
3817 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
3818 set_buffer_uptodate(bh);
3819
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003820 if (buffer_uptodate(bh)) {
3821 /* someone brought it uptodate while we waited */
3822 unlock_buffer(bh);
3823 goto has_buffer;
3824 }
3825
3826 /*
3827 * If we have all information of the inode in memory and this
3828 * is the only valid inode in the block, we need not read the
3829 * block.
3830 */
3831 if (in_mem) {
3832 struct buffer_head *bitmap_bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04003833 int i, start;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003834
Theodore Ts'o240799c2008-10-09 23:53:47 -04003835 start = inode_offset & ~(inodes_per_block - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003836
3837 /* Is the inode bitmap in cache? */
Theodore Ts'o240799c2008-10-09 23:53:47 -04003838 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
Wang Shilongaebf0242013-01-12 16:28:47 -05003839 if (unlikely(!bitmap_bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003840 goto make_io;
3841
3842 /*
3843 * If the inode bitmap isn't in cache then the
3844 * optimisation may end up performing two reads instead
3845 * of one, so skip it.
3846 */
3847 if (!buffer_uptodate(bitmap_bh)) {
3848 brelse(bitmap_bh);
3849 goto make_io;
3850 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04003851 for (i = start; i < start + inodes_per_block; i++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003852 if (i == inode_offset)
3853 continue;
Mingming Cao617ba132006-10-11 01:20:53 -07003854 if (ext4_test_bit(i, bitmap_bh->b_data))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003855 break;
3856 }
3857 brelse(bitmap_bh);
Theodore Ts'o240799c2008-10-09 23:53:47 -04003858 if (i == start + inodes_per_block) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003859 /* all other inodes are free, so skip I/O */
3860 memset(bh->b_data, 0, bh->b_size);
3861 set_buffer_uptodate(bh);
3862 unlock_buffer(bh);
3863 goto has_buffer;
3864 }
3865 }
3866
3867make_io:
3868 /*
Theodore Ts'o240799c2008-10-09 23:53:47 -04003869 * If we need to do any I/O, try to pre-readahead extra
3870 * blocks from the inode table.
3871 */
3872 if (EXT4_SB(sb)->s_inode_readahead_blks) {
3873 ext4_fsblk_t b, end, table;
3874 unsigned num;
Theodore Ts'o0d606e22013-04-23 08:59:35 -04003875 __u32 ra_blks = EXT4_SB(sb)->s_inode_readahead_blks;
Theodore Ts'o240799c2008-10-09 23:53:47 -04003876
3877 table = ext4_inode_table(sb, gdp);
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003878 /* s_inode_readahead_blks is always a power of 2 */
Theodore Ts'o0d606e22013-04-23 08:59:35 -04003879 b = block & ~((ext4_fsblk_t) ra_blks - 1);
Theodore Ts'o240799c2008-10-09 23:53:47 -04003880 if (table > b)
3881 b = table;
Theodore Ts'o0d606e22013-04-23 08:59:35 -04003882 end = b + ra_blks;
Theodore Ts'o240799c2008-10-09 23:53:47 -04003883 num = EXT4_INODES_PER_GROUP(sb);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04003884 if (ext4_has_group_desc_csum(sb))
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05003885 num -= ext4_itable_unused_count(sb, gdp);
Theodore Ts'o240799c2008-10-09 23:53:47 -04003886 table += num / inodes_per_block;
3887 if (end > table)
3888 end = table;
3889 while (b <= end)
3890 sb_breadahead(sb, b++);
3891 }
3892
3893 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003894 * There are other valid inodes in the buffer, this inode
3895 * has in-inode xattrs, or we don't have this inode in memory.
3896 * Read the block from disk.
3897 */
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003898 trace_ext4_load_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003899 get_bh(bh);
3900 bh->b_end_io = end_buffer_read_sync;
Christoph Hellwig65299a32011-08-23 14:50:29 +02003901 submit_bh(READ | REQ_META | REQ_PRIO, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003902 wait_on_buffer(bh);
3903 if (!buffer_uptodate(bh)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -04003904 EXT4_ERROR_INODE_BLOCK(inode, block,
3905 "unable to read itable block");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003906 brelse(bh);
3907 return -EIO;
3908 }
3909 }
3910has_buffer:
3911 iloc->bh = bh;
3912 return 0;
3913}
3914
Mingming Cao617ba132006-10-11 01:20:53 -07003915int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003916{
3917 /* We have all inode data except xattrs in memory here. */
Mingming Cao617ba132006-10-11 01:20:53 -07003918 return __ext4_get_inode_loc(inode, iloc,
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003919 !ext4_test_inode_state(inode, EXT4_STATE_XATTR));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003920}
3921
Mingming Cao617ba132006-10-11 01:20:53 -07003922void ext4_set_inode_flags(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003923{
Mingming Cao617ba132006-10-11 01:20:53 -07003924 unsigned int flags = EXT4_I(inode)->i_flags;
Theodore Ts'o00a1a052014-03-30 10:20:01 -04003925 unsigned int new_fl = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003926
Mingming Cao617ba132006-10-11 01:20:53 -07003927 if (flags & EXT4_SYNC_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04003928 new_fl |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07003929 if (flags & EXT4_APPEND_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04003930 new_fl |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07003931 if (flags & EXT4_IMMUTABLE_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04003932 new_fl |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07003933 if (flags & EXT4_NOATIME_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04003934 new_fl |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07003935 if (flags & EXT4_DIRSYNC_FL)
Theodore Ts'o00a1a052014-03-30 10:20:01 -04003936 new_fl |= S_DIRSYNC;
3937 set_mask_bits(&inode->i_flags,
3938 S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC, new_fl);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003939}
3940
Jan Karaff9ddf72007-07-18 09:24:20 -04003941/* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
3942void ext4_get_inode_flags(struct ext4_inode_info *ei)
3943{
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04003944 unsigned int vfs_fl;
3945 unsigned long old_fl, new_fl;
Jan Karaff9ddf72007-07-18 09:24:20 -04003946
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04003947 do {
3948 vfs_fl = ei->vfs_inode.i_flags;
3949 old_fl = ei->i_flags;
3950 new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
3951 EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|
3952 EXT4_DIRSYNC_FL);
3953 if (vfs_fl & S_SYNC)
3954 new_fl |= EXT4_SYNC_FL;
3955 if (vfs_fl & S_APPEND)
3956 new_fl |= EXT4_APPEND_FL;
3957 if (vfs_fl & S_IMMUTABLE)
3958 new_fl |= EXT4_IMMUTABLE_FL;
3959 if (vfs_fl & S_NOATIME)
3960 new_fl |= EXT4_NOATIME_FL;
3961 if (vfs_fl & S_DIRSYNC)
3962 new_fl |= EXT4_DIRSYNC_FL;
3963 } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl);
Jan Karaff9ddf72007-07-18 09:24:20 -04003964}
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003965
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003966static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003967 struct ext4_inode_info *ei)
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003968{
3969 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05003970 struct inode *inode = &(ei->vfs_inode);
3971 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003972
3973 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3974 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
3975 /* we are using combined 48 bit field */
3976 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
3977 le32_to_cpu(raw_inode->i_blocks_lo);
Theodore Ts'o07a03822010-06-14 09:54:48 -04003978 if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05003979 /* i_blocks represent file system block size */
3980 return i_blocks << (inode->i_blkbits - 9);
3981 } else {
3982 return i_blocks;
3983 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003984 } else {
3985 return le32_to_cpu(raw_inode->i_blocks_lo);
3986 }
3987}
Jan Karaff9ddf72007-07-18 09:24:20 -04003988
Tao Ma152a7b02012-12-02 11:13:24 -05003989static inline void ext4_iget_extra_inode(struct inode *inode,
3990 struct ext4_inode *raw_inode,
3991 struct ext4_inode_info *ei)
3992{
3993 __le32 *magic = (void *)raw_inode +
3994 EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
Tao Ma67cf5b02012-12-10 14:04:46 -05003995 if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
Tao Ma152a7b02012-12-02 11:13:24 -05003996 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Tao Ma67cf5b02012-12-10 14:04:46 -05003997 ext4_find_inline_data_nolock(inode);
Tao Maf19d5872012-12-10 14:05:51 -05003998 } else
3999 EXT4_I(inode)->i_inline_off = 0;
Tao Ma152a7b02012-12-02 11:13:24 -05004000}
4001
David Howells1d1fe1e2008-02-07 00:15:37 -08004002struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004003{
Mingming Cao617ba132006-10-11 01:20:53 -07004004 struct ext4_iloc iloc;
4005 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08004006 struct ext4_inode_info *ei;
David Howells1d1fe1e2008-02-07 00:15:37 -08004007 struct inode *inode;
Jan Karab436b9b2009-12-08 23:51:10 -05004008 journal_t *journal = EXT4_SB(sb)->s_journal;
David Howells1d1fe1e2008-02-07 00:15:37 -08004009 long ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004010 int block;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004011 uid_t i_uid;
4012 gid_t i_gid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004013
David Howells1d1fe1e2008-02-07 00:15:37 -08004014 inode = iget_locked(sb, ino);
4015 if (!inode)
4016 return ERR_PTR(-ENOMEM);
4017 if (!(inode->i_state & I_NEW))
4018 return inode;
4019
4020 ei = EXT4_I(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05004021 iloc.bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004022
David Howells1d1fe1e2008-02-07 00:15:37 -08004023 ret = __ext4_get_inode_loc(inode, &iloc, 0);
4024 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004025 goto bad_inode;
Mingming Cao617ba132006-10-11 01:20:53 -07004026 raw_inode = ext4_raw_inode(&iloc);
Darrick J. Wong814525f2012-04-29 18:31:10 -04004027
4028 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4029 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
4030 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
4031 EXT4_INODE_SIZE(inode->i_sb)) {
4032 EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)",
4033 EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize,
4034 EXT4_INODE_SIZE(inode->i_sb));
4035 ret = -EIO;
4036 goto bad_inode;
4037 }
4038 } else
4039 ei->i_extra_isize = 0;
4040
4041 /* Precompute checksum seed for inode metadata */
4042 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4043 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
4044 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4045 __u32 csum;
4046 __le32 inum = cpu_to_le32(inode->i_ino);
4047 __le32 gen = raw_inode->i_generation;
4048 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
4049 sizeof(inum));
4050 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
4051 sizeof(gen));
4052 }
4053
4054 if (!ext4_inode_csum_verify(inode, raw_inode, ei)) {
4055 EXT4_ERROR_INODE(inode, "checksum invalid");
4056 ret = -EIO;
4057 goto bad_inode;
4058 }
4059
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004060 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004061 i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
4062 i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004063 if (!(test_opt(inode->i_sb, NO_UID32))) {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004064 i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
4065 i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004066 }
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004067 i_uid_write(inode, i_uid);
4068 i_gid_write(inode, i_gid);
Miklos Szeredibfe86842011-10-28 14:13:29 +02004069 set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004070
Theodore Ts'o353eb832011-01-10 12:18:25 -05004071 ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
Tao Ma67cf5b02012-12-10 14:04:46 -05004072 ei->i_inline_off = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004073 ei->i_dir_start_lookup = 0;
4074 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
4075 /* We now have enough fields to check if the inode was active or not.
4076 * This is needed because nfsd might try to access dead inodes
4077 * the test is that same one that e2fsck uses
4078 * NeilBrown 1999oct15
4079 */
4080 if (inode->i_nlink == 0) {
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004081 if ((inode->i_mode == 0 ||
4082 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) &&
4083 ino != EXT4_BOOT_LOADER_INO) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004084 /* this inode is deleted */
David Howells1d1fe1e2008-02-07 00:15:37 -08004085 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004086 goto bad_inode;
4087 }
4088 /* The only unlinked inodes we let through here have
4089 * valid i_mode and are being read by the orphan
4090 * recovery code: that's fine, we're about to complete
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004091 * the process of deleting those.
4092 * OR it is the EXT4_BOOT_LOADER_INO which is
4093 * not initialized on a new filesystem. */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004094 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004095 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004096 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004097 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Theodore Ts'oa9e81742009-04-24 16:11:18 -04004098 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004099 ei->i_file_acl |=
4100 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004101 inode->i_size = ext4_isize(raw_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004102 ei->i_disksize = inode->i_size;
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03004103#ifdef CONFIG_QUOTA
4104 ei->i_reserved_quota = 0;
4105#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004106 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
4107 ei->i_block_group = iloc.block_group;
Theodore Ts'oa4912122009-03-12 12:18:34 -04004108 ei->i_last_alloc_group = ~0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004109 /*
4110 * NOTE! The in-memory inode i_data array is in little-endian order
4111 * even on big-endian machines: we do NOT byteswap the block numbers!
4112 */
Mingming Cao617ba132006-10-11 01:20:53 -07004113 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004114 ei->i_data[block] = raw_inode->i_block[block];
4115 INIT_LIST_HEAD(&ei->i_orphan);
4116
Jan Karab436b9b2009-12-08 23:51:10 -05004117 /*
4118 * Set transaction id's of transactions that have to be committed
4119 * to finish f[data]sync. We set them to currently running transaction
4120 * as we cannot be sure that the inode or some of its metadata isn't
4121 * part of the transaction - the inode could have been reclaimed and
4122 * now it is reread from disk.
4123 */
4124 if (journal) {
4125 transaction_t *transaction;
4126 tid_t tid;
4127
Theodore Ts'oa931da62010-08-03 21:35:12 -04004128 read_lock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05004129 if (journal->j_running_transaction)
4130 transaction = journal->j_running_transaction;
4131 else
4132 transaction = journal->j_committing_transaction;
4133 if (transaction)
4134 tid = transaction->t_tid;
4135 else
4136 tid = journal->j_commit_sequence;
Theodore Ts'oa931da62010-08-03 21:35:12 -04004137 read_unlock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05004138 ei->i_sync_tid = tid;
4139 ei->i_datasync_tid = tid;
4140 }
4141
Eric Sandeen0040d982008-02-05 22:36:43 -05004142 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004143 if (ei->i_extra_isize == 0) {
4144 /* The extra space is currently unused. Use it. */
Mingming Cao617ba132006-10-11 01:20:53 -07004145 ei->i_extra_isize = sizeof(struct ext4_inode) -
4146 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004147 } else {
Tao Ma152a7b02012-12-02 11:13:24 -05004148 ext4_iget_extra_inode(inode, raw_inode, ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004149 }
Darrick J. Wong814525f2012-04-29 18:31:10 -04004150 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004151
Kalpak Shahef7f3832007-07-18 09:15:20 -04004152 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
4153 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
4154 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
4155 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
4156
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004157 inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
4158 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
4159 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4160 inode->i_version |=
4161 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
4162 }
4163
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04004164 ret = 0;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004165 if (ei->i_file_acl &&
Theodore Ts'o10329882009-11-15 15:29:56 -05004166 !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04004167 EXT4_ERROR_INODE(inode, "bad extended attribute block %llu",
4168 ei->i_file_acl);
Theodore Ts'o485c26e2009-04-24 13:43:20 -04004169 ret = -EIO;
4170 goto bad_inode;
Tao Maf19d5872012-12-10 14:05:51 -05004171 } else if (!ext4_has_inline_data(inode)) {
4172 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
4173 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4174 (S_ISLNK(inode->i_mode) &&
4175 !ext4_inode_is_fast_symlink(inode))))
4176 /* Validate extent which is part of inode */
4177 ret = ext4_ext_check_inode(inode);
4178 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
4179 (S_ISLNK(inode->i_mode) &&
4180 !ext4_inode_is_fast_symlink(inode))) {
4181 /* Validate block references which are part of inode */
4182 ret = ext4_ind_check_inode(inode);
4183 }
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04004184 }
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004185 if (ret)
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004186 goto bad_inode;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -04004187
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004188 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004189 inode->i_op = &ext4_file_inode_operations;
4190 inode->i_fop = &ext4_file_operations;
4191 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004192 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004193 inode->i_op = &ext4_dir_inode_operations;
4194 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004195 } else if (S_ISLNK(inode->i_mode)) {
Duane Griffine83c1392008-12-19 20:47:15 +00004196 if (ext4_inode_is_fast_symlink(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004197 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00004198 nd_terminate_link(ei->i_data, inode->i_size,
4199 sizeof(ei->i_data) - 1);
4200 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07004201 inode->i_op = &ext4_symlink_inode_operations;
4202 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004203 }
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004204 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
4205 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07004206 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004207 if (raw_inode->i_block[0])
4208 init_special_inode(inode, inode->i_mode,
4209 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
4210 else
4211 init_special_inode(inode, inode->i_mode,
4212 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
Dr. Tilmann Bubeck393d1d12013-04-08 12:54:05 -04004213 } else if (ino == EXT4_BOOT_LOADER_INO) {
4214 make_bad_inode(inode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004215 } else {
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004216 ret = -EIO;
Theodore Ts'o24676da2010-05-16 21:00:00 -04004217 EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04004218 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004219 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004220 brelse(iloc.bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004221 ext4_set_inode_flags(inode);
David Howells1d1fe1e2008-02-07 00:15:37 -08004222 unlock_new_inode(inode);
4223 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004224
4225bad_inode:
Theodore Ts'o567f3e92009-11-14 08:19:05 -05004226 brelse(iloc.bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08004227 iget_failed(inode);
4228 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004229}
4230
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004231static int ext4_inode_blocks_set(handle_t *handle,
4232 struct ext4_inode *raw_inode,
4233 struct ext4_inode_info *ei)
4234{
4235 struct inode *inode = &(ei->vfs_inode);
4236 u64 i_blocks = inode->i_blocks;
4237 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004238
4239 if (i_blocks <= ~0U) {
4240 /*
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004241 * i_blocks can be represented in a 32 bit variable
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004242 * as multiple of 512 bytes
4243 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004244 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004245 raw_inode->i_blocks_high = 0;
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004246 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004247 return 0;
4248 }
4249 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
4250 return -EFBIG;
4251
4252 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004253 /*
4254 * i_blocks can be represented in a 48 bit variable
4255 * as multiple of 512 bytes
4256 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004257 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004258 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004259 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004260 } else {
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04004261 ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05004262 /* i_block is stored in file system block size */
4263 i_blocks = i_blocks >> (inode->i_blkbits - 9);
4264 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
4265 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004266 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004267 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004268}
4269
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004270/*
4271 * Post the struct inode info into an on-disk inode location in the
4272 * buffer-cache. This gobbles the caller's reference to the
4273 * buffer_head in the inode location struct.
4274 *
4275 * The caller must have write access to iloc->bh.
4276 */
Mingming Cao617ba132006-10-11 01:20:53 -07004277static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004278 struct inode *inode,
Frank Mayhar830156c2009-09-29 10:07:47 -04004279 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004280{
Mingming Cao617ba132006-10-11 01:20:53 -07004281 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
4282 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004283 struct buffer_head *bh = iloc->bh;
4284 int err = 0, rc, block;
Jan Karab71fc072012-09-26 21:52:20 -04004285 int need_datasync = 0;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004286 uid_t i_uid;
4287 gid_t i_gid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004288
4289 /* For fields not not tracking in the in-memory inode,
4290 * initialise them to zero for new inodes. */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004291 if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
Mingming Cao617ba132006-10-11 01:20:53 -07004292 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004293
Jan Karaff9ddf72007-07-18 09:24:20 -04004294 ext4_get_inode_flags(ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004295 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004296 i_uid = i_uid_read(inode);
4297 i_gid = i_gid_read(inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004298 if (!(test_opt(inode->i_sb, NO_UID32))) {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004299 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid));
4300 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004301/*
4302 * Fix up interoperability with old kernels. Otherwise, old inodes get
4303 * re-used with the upper 16 bits of the uid/gid intact
4304 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004305 if (!ei->i_dtime) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004306 raw_inode->i_uid_high =
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004307 cpu_to_le16(high_16_bits(i_uid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004308 raw_inode->i_gid_high =
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004309 cpu_to_le16(high_16_bits(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004310 } else {
4311 raw_inode->i_uid_high = 0;
4312 raw_inode->i_gid_high = 0;
4313 }
4314 } else {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004315 raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid));
4316 raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004317 raw_inode->i_uid_high = 0;
4318 raw_inode->i_gid_high = 0;
4319 }
4320 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004321
4322 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
4323 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
4324 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
4325 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
4326
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004327 if (ext4_inode_blocks_set(handle, raw_inode, ei))
4328 goto out_brelse;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004329 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Theodore Ts'o353eb832011-01-10 12:18:25 -05004330 raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
Mingming Cao9b8f1f02006-10-11 01:21:13 -07004331 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
4332 cpu_to_le32(EXT4_OS_HURD))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004333 raw_inode->i_file_acl_high =
4334 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004335 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Jan Karab71fc072012-09-26 21:52:20 -04004336 if (ei->i_disksize != ext4_isize(raw_inode)) {
4337 ext4_isize_set(raw_inode, ei->i_disksize);
4338 need_datasync = 1;
4339 }
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004340 if (ei->i_disksize > 0x7fffffffULL) {
4341 struct super_block *sb = inode->i_sb;
4342 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
4343 EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
4344 EXT4_SB(sb)->s_es->s_rev_level ==
4345 cpu_to_le32(EXT4_GOOD_OLD_REV)) {
4346 /* If this is the first large file
4347 * created, add a flag to the superblock.
4348 */
4349 err = ext4_journal_get_write_access(handle,
4350 EXT4_SB(sb)->s_sbh);
4351 if (err)
4352 goto out_brelse;
4353 ext4_update_dynamic_rev(sb);
4354 EXT4_SET_RO_COMPAT_FEATURE(sb,
Mingming Cao617ba132006-10-11 01:20:53 -07004355 EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
Frank Mayhar03901312009-01-07 00:06:22 -05004356 ext4_handle_sync(handle);
Artem Bityutskiyb50924c2012-07-22 20:37:31 -04004357 err = ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004358 }
4359 }
4360 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
4361 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
4362 if (old_valid_dev(inode->i_rdev)) {
4363 raw_inode->i_block[0] =
4364 cpu_to_le32(old_encode_dev(inode->i_rdev));
4365 raw_inode->i_block[1] = 0;
4366 } else {
4367 raw_inode->i_block[0] = 0;
4368 raw_inode->i_block[1] =
4369 cpu_to_le32(new_encode_dev(inode->i_rdev));
4370 raw_inode->i_block[2] = 0;
4371 }
Tao Maf19d5872012-12-10 14:05:51 -05004372 } else if (!ext4_has_inline_data(inode)) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004373 for (block = 0; block < EXT4_N_BLOCKS; block++)
4374 raw_inode->i_block[block] = ei->i_data[block];
Tao Maf19d5872012-12-10 14:05:51 -05004375 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004376
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004377 raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
4378 if (ei->i_extra_isize) {
4379 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4380 raw_inode->i_version_hi =
4381 cpu_to_le32(inode->i_version >> 32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004382 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004383 }
4384
Darrick J. Wong814525f2012-04-29 18:31:10 -04004385 ext4_inode_csum_set(inode, raw_inode, ei);
4386
Frank Mayhar830156c2009-09-29 10:07:47 -04004387 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05004388 rc = ext4_handle_dirty_metadata(handle, NULL, bh);
Frank Mayhar830156c2009-09-29 10:07:47 -04004389 if (!err)
4390 err = rc;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004391 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004392
Jan Karab71fc072012-09-26 21:52:20 -04004393 ext4_update_inode_fsync_trans(handle, inode, need_datasync);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004394out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004395 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004396 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004397 return err;
4398}
4399
4400/*
Mingming Cao617ba132006-10-11 01:20:53 -07004401 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004402 *
4403 * We are called from a few places:
4404 *
4405 * - Within generic_file_write() for O_SYNC files.
4406 * Here, there will be no transaction running. We wait for any running
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004407 * transaction to commit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004408 *
4409 * - Within sys_sync(), kupdate and such.
4410 * We wait on commit, if tol to.
4411 *
4412 * - Within prune_icache() (PF_MEMALLOC == true)
4413 * Here we simply return. We can't afford to block kswapd on the
4414 * journal commit.
4415 *
4416 * In all cases it is actually safe for us to return without doing anything,
4417 * because the inode has been copied into a raw inode buffer in
Mingming Cao617ba132006-10-11 01:20:53 -07004418 * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004419 * knfsd.
4420 *
4421 * Note that we are absolutely dependent upon all inode dirtiers doing the
4422 * right thing: they *must* call mark_inode_dirty() after dirtying info in
4423 * which we are interested.
4424 *
4425 * It would be a bug for them to not do this. The code:
4426 *
4427 * mark_inode_dirty(inode)
4428 * stuff();
4429 * inode->i_size = expr;
4430 *
4431 * is in error because a kswapd-driven write_inode() could occur while
4432 * `stuff()' is running, and the new i_size will be lost. Plus the inode
4433 * will no longer be on the superblock's dirty inode list.
4434 */
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004435int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004436{
Frank Mayhar91ac6f42009-09-09 22:33:47 -04004437 int err;
4438
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004439 if (current->flags & PF_MEMALLOC)
4440 return 0;
4441
Frank Mayhar91ac6f42009-09-09 22:33:47 -04004442 if (EXT4_SB(inode->i_sb)->s_journal) {
4443 if (ext4_journal_current_handle()) {
4444 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
4445 dump_stack();
4446 return -EIO;
4447 }
4448
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004449 if (wbc->sync_mode != WB_SYNC_ALL)
Frank Mayhar91ac6f42009-09-09 22:33:47 -04004450 return 0;
4451
4452 err = ext4_force_commit(inode->i_sb);
4453 } else {
4454 struct ext4_iloc iloc;
4455
Curt Wohlgemuth8b472d72010-04-03 16:45:06 -04004456 err = __ext4_get_inode_loc(inode, &iloc, 0);
Frank Mayhar91ac6f42009-09-09 22:33:47 -04004457 if (err)
4458 return err;
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004459 if (wbc->sync_mode == WB_SYNC_ALL)
Frank Mayhar830156c2009-09-29 10:07:47 -04004460 sync_dirty_buffer(iloc.bh);
4461 if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -04004462 EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr,
4463 "IO error syncing inode");
Frank Mayhar830156c2009-09-29 10:07:47 -04004464 err = -EIO;
4465 }
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004466 brelse(iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004467 }
Frank Mayhar91ac6f42009-09-09 22:33:47 -04004468 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004469}
4470
4471/*
Jan Kara53e87262012-12-25 13:29:52 -05004472 * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
4473 * buffers that are attached to a page stradding i_size and are undergoing
4474 * commit. In that case we have to wait for commit to finish and try again.
4475 */
4476static void ext4_wait_for_tail_page_commit(struct inode *inode)
4477{
4478 struct page *page;
4479 unsigned offset;
4480 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
4481 tid_t commit_tid = 0;
4482 int ret;
4483
4484 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
4485 /*
4486 * All buffers in the last page remain valid? Then there's nothing to
4487 * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE ==
4488 * blocksize case
4489 */
4490 if (offset > PAGE_CACHE_SIZE - (1 << inode->i_blkbits))
4491 return;
4492 while (1) {
4493 page = find_lock_page(inode->i_mapping,
4494 inode->i_size >> PAGE_CACHE_SHIFT);
4495 if (!page)
4496 return;
Lukas Czernerca99fdd2013-05-21 23:25:01 -04004497 ret = __ext4_journalled_invalidatepage(page, offset,
4498 PAGE_CACHE_SIZE - offset);
Jan Kara53e87262012-12-25 13:29:52 -05004499 unlock_page(page);
4500 page_cache_release(page);
4501 if (ret != -EBUSY)
4502 return;
4503 commit_tid = 0;
4504 read_lock(&journal->j_state_lock);
4505 if (journal->j_committing_transaction)
4506 commit_tid = journal->j_committing_transaction->t_tid;
4507 read_unlock(&journal->j_state_lock);
4508 if (commit_tid)
4509 jbd2_log_wait_commit(journal, commit_tid);
4510 }
4511}
4512
4513/*
Mingming Cao617ba132006-10-11 01:20:53 -07004514 * ext4_setattr()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004515 *
4516 * Called from notify_change.
4517 *
4518 * We want to trap VFS attempts to truncate the file as soon as
4519 * possible. In particular, we want to make sure that when the VFS
4520 * shrinks i_size, we put the inode on the orphan list and modify
4521 * i_disksize immediately, so that during the subsequent flushing of
4522 * dirty pages and freeing of disk blocks, we can guarantee that any
4523 * commit will leave the blocks being flushed in an unused state on
4524 * disk. (On recovery, the inode will get truncated and the blocks will
4525 * be freed, so we have a strong guarantee that no future commit will
4526 * leave these blocks visible to the user.)
4527 *
Jan Kara678aaf42008-07-11 19:27:31 -04004528 * Another thing we have to assure is that if we are in ordered mode
4529 * and inode is still attached to the committing transaction, we must
4530 * we start writeout of all the dirty pages which are being truncated.
4531 * This way we are sure that all the data written in the previous
4532 * transaction are already on disk (truncate waits for pages under
4533 * writeback).
4534 *
4535 * Called with inode->i_mutex down.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004536 */
Mingming Cao617ba132006-10-11 01:20:53 -07004537int ext4_setattr(struct dentry *dentry, struct iattr *attr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004538{
4539 struct inode *inode = dentry->d_inode;
4540 int error, rc = 0;
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04004541 int orphan = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004542 const unsigned int ia_valid = attr->ia_valid;
4543
4544 error = inode_change_ok(inode, attr);
4545 if (error)
4546 return error;
4547
Dmitry Monakhov12755622010-04-08 22:04:20 +04004548 if (is_quota_modification(inode, attr))
Christoph Hellwig871a2932010-03-03 09:05:07 -05004549 dquot_initialize(inode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004550 if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
4551 (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004552 handle_t *handle;
4553
4554 /* (user+group)*(old+new) structure, inode write (sb,
4555 * inode block, ? - but truncate inode update has it) */
Theodore Ts'o9924a922013-02-08 21:59:22 -05004556 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
4557 (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) +
4558 EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004559 if (IS_ERR(handle)) {
4560 error = PTR_ERR(handle);
4561 goto err_out;
4562 }
Christoph Hellwigb43fa822010-03-03 09:05:03 -05004563 error = dquot_transfer(inode, attr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004564 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07004565 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004566 return error;
4567 }
4568 /* Update corresponding info in inode so that everything is in
4569 * one transaction */
4570 if (attr->ia_valid & ATTR_UID)
4571 inode->i_uid = attr->ia_uid;
4572 if (attr->ia_valid & ATTR_GID)
4573 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07004574 error = ext4_mark_inode_dirty(handle, inode);
4575 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004576 }
4577
Jan Kara52083862013-08-17 10:07:17 -04004578 if (attr->ia_valid & ATTR_SIZE && attr->ia_size != inode->i_size) {
4579 handle_t *handle;
Christoph Hellwig562c72a2011-06-24 14:29:45 -04004580
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004581 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
Eric Sandeene2b46572008-01-28 23:58:27 -05004582 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4583
Theodore Ts'o0c095c72010-07-27 11:56:06 -04004584 if (attr->ia_size > sbi->s_bitmap_maxbytes)
4585 return -EFBIG;
Eric Sandeene2b46572008-01-28 23:58:27 -05004586 }
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004587
4588 if (IS_I_VERSION(inode) && attr->ia_size != inode->i_size)
4589 inode_inc_iversion(inode);
4590
Jan Kara52083862013-08-17 10:07:17 -04004591 if (S_ISREG(inode->i_mode) &&
4592 (attr->ia_size < inode->i_size)) {
4593 if (ext4_should_order_data(inode)) {
4594 error = ext4_begin_ordered_truncate(inode,
Jan Kara678aaf42008-07-11 19:27:31 -04004595 attr->ia_size);
Jan Kara52083862013-08-17 10:07:17 -04004596 if (error)
Jan Kara678aaf42008-07-11 19:27:31 -04004597 goto err_out;
Jan Kara52083862013-08-17 10:07:17 -04004598 }
4599 handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
4600 if (IS_ERR(handle)) {
4601 error = PTR_ERR(handle);
4602 goto err_out;
4603 }
4604 if (ext4_handle_valid(handle)) {
4605 error = ext4_orphan_add(handle, inode);
4606 orphan = 1;
4607 }
Jan Kara90e775b2013-08-17 10:09:31 -04004608 down_write(&EXT4_I(inode)->i_data_sem);
Jan Kara52083862013-08-17 10:07:17 -04004609 EXT4_I(inode)->i_disksize = attr->ia_size;
4610 rc = ext4_mark_inode_dirty(handle, inode);
4611 if (!error)
4612 error = rc;
Jan Kara90e775b2013-08-17 10:09:31 -04004613 /*
4614 * We have to update i_size under i_data_sem together
4615 * with i_disksize to avoid races with writeback code
4616 * running ext4_wb_update_i_disksize().
4617 */
4618 if (!error)
4619 i_size_write(inode, attr->ia_size);
4620 up_write(&EXT4_I(inode)->i_data_sem);
Jan Kara52083862013-08-17 10:07:17 -04004621 ext4_journal_stop(handle);
4622 if (error) {
4623 ext4_orphan_del(NULL, inode);
Jan Kara678aaf42008-07-11 19:27:31 -04004624 goto err_out;
4625 }
Jan Kara90e775b2013-08-17 10:09:31 -04004626 } else
4627 i_size_write(inode, attr->ia_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004628
Jan Kara52083862013-08-17 10:07:17 -04004629 /*
4630 * Blocks are going to be removed from the inode. Wait
4631 * for dio in flight. Temporarily disable
4632 * dioread_nolock to prevent livelock.
4633 */
4634 if (orphan) {
4635 if (!ext4_should_journal_data(inode)) {
4636 ext4_inode_block_unlocked_dio(inode);
4637 inode_dio_wait(inode);
4638 ext4_inode_resume_unlocked_dio(inode);
4639 } else
4640 ext4_wait_for_tail_page_commit(inode);
Dmitry Monakhov1c9114f2012-09-29 00:55:23 -04004641 }
Jan Kara52083862013-08-17 10:07:17 -04004642 /*
4643 * Truncate pagecache after we've waited for commit
4644 * in data=journal mode to make pages freeable.
4645 */
Kirill A. Shutemov7caef262013-09-12 15:13:56 -07004646 truncate_pagecache(inode, inode->i_size);
Theodore Ts'o072bd7e2011-05-23 15:13:02 -04004647 }
Jan Kara52083862013-08-17 10:07:17 -04004648 /*
4649 * We want to call ext4_truncate() even if attr->ia_size ==
4650 * inode->i_size for cases like truncation of fallocated space
4651 */
4652 if (attr->ia_valid & ATTR_SIZE)
4653 ext4_truncate(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004654
Christoph Hellwig10257742010-06-04 11:30:02 +02004655 if (!rc) {
4656 setattr_copy(inode, attr);
4657 mark_inode_dirty(inode);
4658 }
4659
4660 /*
4661 * If the call to ext4_truncate failed to get a transaction handle at
4662 * all, we need to clean up the in-core orphan list manually.
4663 */
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04004664 if (orphan && inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07004665 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004666
4667 if (!rc && (ia_valid & ATTR_MODE))
Christoph Hellwig64e178a2013-12-20 05:16:44 -08004668 rc = posix_acl_chmod(inode, inode->i_mode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004669
4670err_out:
Mingming Cao617ba132006-10-11 01:20:53 -07004671 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004672 if (!error)
4673 error = rc;
4674 return error;
4675}
4676
Mingming Cao3e3398a2008-07-11 19:27:31 -04004677int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
4678 struct kstat *stat)
4679{
4680 struct inode *inode;
Jan Kara8af8eec2013-05-31 19:39:56 -04004681 unsigned long long delalloc_blocks;
Mingming Cao3e3398a2008-07-11 19:27:31 -04004682
4683 inode = dentry->d_inode;
4684 generic_fillattr(inode, stat);
4685
4686 /*
Andreas Dilger9206c562013-11-11 22:38:12 -05004687 * If there is inline data in the inode, the inode will normally not
4688 * have data blocks allocated (it may have an external xattr block).
4689 * Report at least one sector for such files, so tools like tar, rsync,
4690 * others doen't incorrectly think the file is completely sparse.
4691 */
4692 if (unlikely(ext4_has_inline_data(inode)))
4693 stat->blocks += (stat->size + 511) >> 9;
4694
4695 /*
Mingming Cao3e3398a2008-07-11 19:27:31 -04004696 * We can't update i_blocks if the block allocation is delayed
4697 * otherwise in the case of system crash before the real block
4698 * allocation is done, we will have i_blocks inconsistent with
4699 * on-disk file blocks.
4700 * We always keep i_blocks updated together with real
4701 * allocation. But to not confuse with user, stat
4702 * will return the blocks that include the delayed allocation
4703 * blocks for this file.
4704 */
Tao Ma96607552012-05-31 22:54:16 -04004705 delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
Andreas Dilger9206c562013-11-11 22:38:12 -05004706 EXT4_I(inode)->i_reserved_data_blocks);
4707 stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits - 9);
Mingming Cao3e3398a2008-07-11 19:27:31 -04004708 return 0;
4709}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004710
Jan Karafffb2732013-06-04 13:01:11 -04004711static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
4712 int pextents)
Mingming Caoa02908f2008-08-19 22:16:07 -04004713{
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004714 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Jan Karafffb2732013-06-04 13:01:11 -04004715 return ext4_ind_trans_blocks(inode, lblocks);
4716 return ext4_ext_index_trans_blocks(inode, pextents);
Mingming Caoa02908f2008-08-19 22:16:07 -04004717}
Theodore Ts'oac51d832008-11-06 16:49:36 -05004718
Mingming Caoa02908f2008-08-19 22:16:07 -04004719/*
4720 * Account for index blocks, block groups bitmaps and block group
4721 * descriptor blocks if modify datablocks and index blocks
4722 * worse case, the indexs blocks spread over different block groups
4723 *
4724 * If datablocks are discontiguous, they are possible to spread over
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004725 * different block groups too. If they are contiguous, with flexbg,
Mingming Caoa02908f2008-08-19 22:16:07 -04004726 * they could still across block group boundary.
4727 *
4728 * Also account for superblock, inode, quota and xattr blocks
4729 */
Jan Karafffb2732013-06-04 13:01:11 -04004730static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
4731 int pextents)
Mingming Caoa02908f2008-08-19 22:16:07 -04004732{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004733 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
4734 int gdpblocks;
Mingming Caoa02908f2008-08-19 22:16:07 -04004735 int idxblocks;
4736 int ret = 0;
4737
4738 /*
Jan Karafffb2732013-06-04 13:01:11 -04004739 * How many index blocks need to touch to map @lblocks logical blocks
4740 * to @pextents physical extents?
Mingming Caoa02908f2008-08-19 22:16:07 -04004741 */
Jan Karafffb2732013-06-04 13:01:11 -04004742 idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents);
Mingming Caoa02908f2008-08-19 22:16:07 -04004743
4744 ret = idxblocks;
4745
4746 /*
4747 * Now let's see how many group bitmaps and group descriptors need
4748 * to account
4749 */
Jan Karafffb2732013-06-04 13:01:11 -04004750 groups = idxblocks + pextents;
Mingming Caoa02908f2008-08-19 22:16:07 -04004751 gdpblocks = groups;
Theodore Ts'o8df96752009-05-01 08:50:38 -04004752 if (groups > ngroups)
4753 groups = ngroups;
Mingming Caoa02908f2008-08-19 22:16:07 -04004754 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
4755 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
4756
4757 /* bitmaps and block group descriptor blocks */
4758 ret += groups + gdpblocks;
4759
4760 /* Blocks for super block, inode, quota and xattr blocks */
4761 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004762
4763 return ret;
4764}
4765
4766/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004767 * Calculate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04004768 * the modification of a single pages into a single transaction,
4769 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04004770 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04004771 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04004772 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04004773 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04004774 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04004775 */
4776int ext4_writepage_trans_blocks(struct inode *inode)
4777{
4778 int bpp = ext4_journal_blocks_per_page(inode);
4779 int ret;
4780
Jan Karafffb2732013-06-04 13:01:11 -04004781 ret = ext4_meta_trans_blocks(inode, bpp, bpp);
Mingming Caoa02908f2008-08-19 22:16:07 -04004782
4783 /* Account for data blocks for journalled mode */
4784 if (ext4_should_journal_data(inode))
4785 ret += bpp;
4786 return ret;
4787}
Mingming Caof3bd1f32008-08-19 22:16:03 -04004788
4789/*
4790 * Calculate the journal credits for a chunk of data modification.
4791 *
4792 * This is called from DIO, fallocate or whoever calling
Eric Sandeen79e83032010-07-27 11:56:07 -04004793 * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
Mingming Caof3bd1f32008-08-19 22:16:03 -04004794 *
4795 * journal buffers for data blocks are not included here, as DIO
4796 * and fallocate do no need to journal data buffers.
4797 */
4798int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
4799{
4800 return ext4_meta_trans_blocks(inode, nrblocks, 1);
4801}
4802
Mingming Caoa02908f2008-08-19 22:16:07 -04004803/*
Mingming Cao617ba132006-10-11 01:20:53 -07004804 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004805 * Give this, we know that the caller already has write access to iloc->bh.
4806 */
Mingming Cao617ba132006-10-11 01:20:53 -07004807int ext4_mark_iloc_dirty(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004808 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004809{
4810 int err = 0;
4811
Theodore Ts'oc64db502012-03-02 12:23:11 -05004812 if (IS_I_VERSION(inode))
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004813 inode_inc_iversion(inode);
4814
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004815 /* the do_update_inode consumes one bh->b_count */
4816 get_bh(iloc->bh);
4817
Mingming Caodab291a2006-10-11 01:21:01 -07004818 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Frank Mayhar830156c2009-09-29 10:07:47 -04004819 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004820 put_bh(iloc->bh);
4821 return err;
4822}
4823
4824/*
4825 * On success, We end up with an outstanding reference count against
4826 * iloc->bh. This _must_ be cleaned up later.
4827 */
4828
4829int
Mingming Cao617ba132006-10-11 01:20:53 -07004830ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
4831 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004832{
Frank Mayhar03901312009-01-07 00:06:22 -05004833 int err;
4834
4835 err = ext4_get_inode_loc(inode, iloc);
4836 if (!err) {
4837 BUFFER_TRACE(iloc->bh, "get_write_access");
4838 err = ext4_journal_get_write_access(handle, iloc->bh);
4839 if (err) {
4840 brelse(iloc->bh);
4841 iloc->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004842 }
4843 }
Mingming Cao617ba132006-10-11 01:20:53 -07004844 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004845 return err;
4846}
4847
4848/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004849 * Expand an inode by new_extra_isize bytes.
4850 * Returns 0 on success or negative error number on failure.
4851 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05004852static int ext4_expand_extra_isize(struct inode *inode,
4853 unsigned int new_extra_isize,
4854 struct ext4_iloc iloc,
4855 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004856{
4857 struct ext4_inode *raw_inode;
4858 struct ext4_xattr_ibody_header *header;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004859
4860 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
4861 return 0;
4862
4863 raw_inode = ext4_raw_inode(&iloc);
4864
4865 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004866
4867 /* No extended attributes present */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004868 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
4869 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004870 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
4871 new_extra_isize);
4872 EXT4_I(inode)->i_extra_isize = new_extra_isize;
4873 return 0;
4874 }
4875
4876 /* try to expand with EAs present */
4877 return ext4_expand_extra_isize_ea(inode, new_extra_isize,
4878 raw_inode, handle);
4879}
4880
4881/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004882 * What we do here is to mark the in-core inode as clean with respect to inode
4883 * dirtiness (it may still be data-dirty).
4884 * This means that the in-core inode may be reaped by prune_icache
4885 * without having to perform any I/O. This is a very good thing,
4886 * because *any* task may call prune_icache - even ones which
4887 * have a transaction open against a different journal.
4888 *
4889 * Is this cheating? Not really. Sure, we haven't written the
4890 * inode out, but prune_icache isn't a user-visible syncing function.
4891 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
4892 * we start and wait on commits.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004893 */
Mingming Cao617ba132006-10-11 01:20:53 -07004894int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004895{
Mingming Cao617ba132006-10-11 01:20:53 -07004896 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004897 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4898 static unsigned int mnt_count;
4899 int err, ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004900
4901 might_sleep();
Theodore Ts'o7ff9c072010-11-08 13:51:33 -05004902 trace_ext4_mark_inode_dirty(inode, _RET_IP_);
Mingming Cao617ba132006-10-11 01:20:53 -07004903 err = ext4_reserve_inode_write(handle, inode, &iloc);
Frank Mayhar03901312009-01-07 00:06:22 -05004904 if (ext4_handle_valid(handle) &&
4905 EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004906 !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004907 /*
4908 * We need extra buffer credits since we may write into EA block
4909 * with this same handle. If journal_extend fails, then it will
4910 * only result in a minor loss of functionality for that inode.
4911 * If this is felt to be critical, then e2fsck should be run to
4912 * force a large enough s_min_extra_isize.
4913 */
4914 if ((jbd2_journal_extend(handle,
4915 EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
4916 ret = ext4_expand_extra_isize(inode,
4917 sbi->s_want_extra_isize,
4918 iloc, handle);
4919 if (ret) {
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004920 ext4_set_inode_state(inode,
4921 EXT4_STATE_NO_EXPAND);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04004922 if (mnt_count !=
4923 le16_to_cpu(sbi->s_es->s_mnt_count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004924 ext4_warning(inode->i_sb,
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004925 "Unable to expand inode %lu. Delete"
4926 " some EAs or run e2fsck.",
4927 inode->i_ino);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04004928 mnt_count =
4929 le16_to_cpu(sbi->s_es->s_mnt_count);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004930 }
4931 }
4932 }
4933 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004934 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07004935 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004936 return err;
4937}
4938
4939/*
Mingming Cao617ba132006-10-11 01:20:53 -07004940 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004941 *
4942 * We're really interested in the case where a file is being extended.
4943 * i_size has been changed by generic_commit_write() and we thus need
4944 * to include the updated inode in the current transaction.
4945 *
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004946 * Also, dquot_alloc_block() will always dirty the inode when blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004947 * are allocated to the file.
4948 *
4949 * If the inode is marked synchronous, we don't honour that here - doing
4950 * so would cause a commit on atime updates, which we don't bother doing.
4951 * We handle synchronous inodes at the highest possible level.
4952 */
Christoph Hellwigaa385722011-05-27 06:53:02 -04004953void ext4_dirty_inode(struct inode *inode, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004954{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004955 handle_t *handle;
4956
Theodore Ts'o9924a922013-02-08 21:59:22 -05004957 handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004958 if (IS_ERR(handle))
4959 goto out;
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04004960
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04004961 ext4_mark_inode_dirty(handle, inode);
4962
Mingming Cao617ba132006-10-11 01:20:53 -07004963 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004964out:
4965 return;
4966}
4967
4968#if 0
4969/*
4970 * Bind an inode's backing buffer_head into this transaction, to prevent
4971 * it from being flushed to disk early. Unlike
Mingming Cao617ba132006-10-11 01:20:53 -07004972 * ext4_reserve_inode_write, this leaves behind no bh reference and
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004973 * returns no iloc structure, so the caller needs to repeat the iloc
4974 * lookup to mark the inode dirty later.
4975 */
Mingming Cao617ba132006-10-11 01:20:53 -07004976static int ext4_pin_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004977{
Mingming Cao617ba132006-10-11 01:20:53 -07004978 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004979
4980 int err = 0;
4981 if (handle) {
Mingming Cao617ba132006-10-11 01:20:53 -07004982 err = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004983 if (!err) {
4984 BUFFER_TRACE(iloc.bh, "get_write_access");
Mingming Caodab291a2006-10-11 01:21:01 -07004985 err = jbd2_journal_get_write_access(handle, iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004986 if (!err)
Frank Mayhar03901312009-01-07 00:06:22 -05004987 err = ext4_handle_dirty_metadata(handle,
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05004988 NULL,
Frank Mayhar03901312009-01-07 00:06:22 -05004989 iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004990 brelse(iloc.bh);
4991 }
4992 }
Mingming Cao617ba132006-10-11 01:20:53 -07004993 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004994 return err;
4995}
4996#endif
4997
Mingming Cao617ba132006-10-11 01:20:53 -07004998int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004999{
5000 journal_t *journal;
5001 handle_t *handle;
5002 int err;
5003
5004 /*
5005 * We have to be very careful here: changing a data block's
5006 * journaling status dynamically is dangerous. If we write a
5007 * data block to the journal, change the status and then delete
5008 * that block, we risk forgetting to revoke the old log record
5009 * from the journal and so a subsequent replay can corrupt data.
5010 * So, first we make sure that the journal is empty and that
5011 * nobody is changing anything.
5012 */
5013
Mingming Cao617ba132006-10-11 01:20:53 -07005014 journal = EXT4_JOURNAL(inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005015 if (!journal)
5016 return 0;
Dave Hansend6995942007-07-18 08:33:51 -04005017 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005018 return -EROFS;
Yongqiang Yang2aff57b2011-12-28 12:02:13 -05005019 /* We have to allocate physical blocks for delalloc blocks
5020 * before flushing journal. otherwise delalloc blocks can not
5021 * be allocated any more. even more truncate on delalloc blocks
5022 * could trigger BUG by flushing delalloc blocks in journal.
5023 * There is no delalloc block in non-journal data mode.
5024 */
5025 if (val && test_opt(inode->i_sb, DELALLOC)) {
5026 err = ext4_alloc_da_blocks(inode);
5027 if (err < 0)
5028 return err;
5029 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005030
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04005031 /* Wait for all existing dio workers */
5032 ext4_inode_block_unlocked_dio(inode);
5033 inode_dio_wait(inode);
5034
Mingming Caodab291a2006-10-11 01:21:01 -07005035 jbd2_journal_lock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005036
5037 /*
5038 * OK, there are no updates running now, and all cached data is
5039 * synced to disk. We are now in a completely consistent state
5040 * which doesn't have anything in the journal, and we know that
5041 * no filesystem updates are running, so it is safe to modify
5042 * the inode's in-core data-journaling state flag now.
5043 */
5044
5045 if (val)
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005046 ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Yongqiang Yang5872dda2011-12-28 13:55:51 -05005047 else {
5048 jbd2_journal_flush(journal);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04005049 ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Yongqiang Yang5872dda2011-12-28 13:55:51 -05005050 }
Mingming Cao617ba132006-10-11 01:20:53 -07005051 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005052
Mingming Caodab291a2006-10-11 01:21:01 -07005053 jbd2_journal_unlock_updates(journal);
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04005054 ext4_inode_resume_unlocked_dio(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005055
5056 /* Finally we can mark the inode as dirty. */
5057
Theodore Ts'o9924a922013-02-08 21:59:22 -05005058 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005059 if (IS_ERR(handle))
5060 return PTR_ERR(handle);
5061
Mingming Cao617ba132006-10-11 01:20:53 -07005062 err = ext4_mark_inode_dirty(handle, inode);
Frank Mayhar03901312009-01-07 00:06:22 -05005063 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07005064 ext4_journal_stop(handle);
5065 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005066
5067 return err;
5068}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005069
5070static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
5071{
5072 return !buffer_mapped(bh);
5073}
5074
Nick Pigginc2ec1752009-03-31 15:23:21 -07005075int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005076{
Nick Pigginc2ec1752009-03-31 15:23:21 -07005077 struct page *page = vmf->page;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005078 loff_t size;
5079 unsigned long len;
Jan Kara9ea7df52011-06-24 14:29:41 -04005080 int ret;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005081 struct file *file = vma->vm_file;
Al Viro496ad9a2013-01-23 17:07:38 -05005082 struct inode *inode = file_inode(file);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005083 struct address_space *mapping = inode->i_mapping;
Jan Kara9ea7df52011-06-24 14:29:41 -04005084 handle_t *handle;
5085 get_block_t *get_block;
5086 int retries = 0;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005087
Jan Kara8e8ad8a2012-06-12 16:20:38 +02005088 sb_start_pagefault(inode->i_sb);
Theodore Ts'o041bbb6d2012-09-30 23:04:56 -04005089 file_update_time(vma->vm_file);
Jan Kara9ea7df52011-06-24 14:29:41 -04005090 /* Delalloc case is easy... */
5091 if (test_opt(inode->i_sb, DELALLOC) &&
5092 !ext4_should_journal_data(inode) &&
5093 !ext4_nonda_switch(inode->i_sb)) {
5094 do {
5095 ret = __block_page_mkwrite(vma, vmf,
5096 ext4_da_get_block_prep);
5097 } while (ret == -ENOSPC &&
5098 ext4_should_retry_alloc(inode->i_sb, &retries));
5099 goto out_ret;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005100 }
Darrick J. Wong0e499892011-05-18 13:55:20 -04005101
5102 lock_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04005103 size = i_size_read(inode);
5104 /* Page got truncated from under us? */
5105 if (page->mapping != mapping || page_offset(page) > size) {
5106 unlock_page(page);
5107 ret = VM_FAULT_NOPAGE;
5108 goto out;
Darrick J. Wong0e499892011-05-18 13:55:20 -04005109 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005110
5111 if (page->index == size >> PAGE_CACHE_SHIFT)
5112 len = size & ~PAGE_CACHE_MASK;
5113 else
5114 len = PAGE_CACHE_SIZE;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005115 /*
Jan Kara9ea7df52011-06-24 14:29:41 -04005116 * Return if we have all the buffers mapped. This avoids the need to do
5117 * journal_start/journal_stop which can block and take a long time
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005118 */
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005119 if (page_has_buffers(page)) {
Tao Maf19d5872012-12-10 14:05:51 -05005120 if (!ext4_walk_page_buffers(NULL, page_buffers(page),
5121 0, len, NULL,
5122 ext4_bh_unmapped)) {
Jan Kara9ea7df52011-06-24 14:29:41 -04005123 /* Wait so that we don't change page under IO */
Darrick J. Wong1d1d1a72013-02-21 16:42:51 -08005124 wait_for_stable_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04005125 ret = VM_FAULT_LOCKED;
5126 goto out;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005127 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005128 }
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04005129 unlock_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04005130 /* OK, we need to fill the hole... */
5131 if (ext4_should_dioread_nolock(inode))
5132 get_block = ext4_get_block_write;
5133 else
5134 get_block = ext4_get_block;
5135retry_alloc:
Theodore Ts'o9924a922013-02-08 21:59:22 -05005136 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
5137 ext4_writepage_trans_blocks(inode));
Jan Kara9ea7df52011-06-24 14:29:41 -04005138 if (IS_ERR(handle)) {
Nick Pigginc2ec1752009-03-31 15:23:21 -07005139 ret = VM_FAULT_SIGBUS;
Jan Kara9ea7df52011-06-24 14:29:41 -04005140 goto out;
5141 }
5142 ret = __block_page_mkwrite(vma, vmf, get_block);
5143 if (!ret && ext4_should_journal_data(inode)) {
Tao Maf19d5872012-12-10 14:05:51 -05005144 if (ext4_walk_page_buffers(handle, page_buffers(page), 0,
Jan Kara9ea7df52011-06-24 14:29:41 -04005145 PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) {
5146 unlock_page(page);
5147 ret = VM_FAULT_SIGBUS;
Yongqiang Yangfcbb5512011-10-26 05:00:19 -04005148 ext4_journal_stop(handle);
Jan Kara9ea7df52011-06-24 14:29:41 -04005149 goto out;
5150 }
5151 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
5152 }
5153 ext4_journal_stop(handle);
5154 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
5155 goto retry_alloc;
5156out_ret:
5157 ret = block_page_mkwrite_return(ret);
5158out:
Jan Kara8e8ad8a2012-06-12 16:20:38 +02005159 sb_end_pagefault(inode->i_sb);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04005160 return ret;
5161}