blob: 576b586b61aa76b28ca90aaa0292a1411f047356 [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>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040040
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040041#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070042#include "xattr.h"
43#include "acl.h"
Theodore Ts'o9f125d62011-06-27 19:16:04 -040044#include "truncate.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070045
Theodore Ts'o9bffad12009-06-17 11:48:11 -040046#include <trace/events/ext4.h>
47
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -040048#define MPAGE_DA_EXTENT_TAIL 0x01
49
Darrick J. Wong814525f2012-04-29 18:31:10 -040050static __u32 ext4_inode_csum(struct inode *inode, struct ext4_inode *raw,
51 struct ext4_inode_info *ei)
52{
53 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
54 __u16 csum_lo;
55 __u16 csum_hi = 0;
56 __u32 csum;
57
58 csum_lo = raw->i_checksum_lo;
59 raw->i_checksum_lo = 0;
60 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
61 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi)) {
62 csum_hi = raw->i_checksum_hi;
63 raw->i_checksum_hi = 0;
64 }
65
66 csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)raw,
67 EXT4_INODE_SIZE(inode->i_sb));
68
69 raw->i_checksum_lo = csum_lo;
70 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
71 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
72 raw->i_checksum_hi = csum_hi;
73
74 return csum;
75}
76
77static int ext4_inode_csum_verify(struct inode *inode, struct ext4_inode *raw,
78 struct ext4_inode_info *ei)
79{
80 __u32 provided, calculated;
81
82 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
83 cpu_to_le32(EXT4_OS_LINUX) ||
84 !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
85 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
86 return 1;
87
88 provided = le16_to_cpu(raw->i_checksum_lo);
89 calculated = ext4_inode_csum(inode, raw, ei);
90 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
91 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
92 provided |= ((__u32)le16_to_cpu(raw->i_checksum_hi)) << 16;
93 else
94 calculated &= 0xFFFF;
95
96 return provided == calculated;
97}
98
99static void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
100 struct ext4_inode_info *ei)
101{
102 __u32 csum;
103
104 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
105 cpu_to_le32(EXT4_OS_LINUX) ||
106 !EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
107 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
108 return;
109
110 csum = ext4_inode_csum(inode, raw, ei);
111 raw->i_checksum_lo = cpu_to_le16(csum & 0xFFFF);
112 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE &&
113 EXT4_FITS_IN_INODE(raw, ei, i_checksum_hi))
114 raw->i_checksum_hi = cpu_to_le16(csum >> 16);
115}
116
Jan Kara678aaf42008-07-11 19:27:31 -0400117static inline int ext4_begin_ordered_truncate(struct inode *inode,
118 loff_t new_size)
119{
Theodore Ts'o7ff9c072010-11-08 13:51:33 -0500120 trace_ext4_begin_ordered_truncate(inode, new_size);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -0500121 /*
122 * If jinode is zero, then we never opened the file for
123 * writing, so there's no need to call
124 * jbd2_journal_begin_ordered_truncate() since there's no
125 * outstanding writes we need to flush.
126 */
127 if (!EXT4_I(inode)->jinode)
128 return 0;
129 return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
130 EXT4_I(inode)->jinode,
131 new_size);
Jan Kara678aaf42008-07-11 19:27:31 -0400132}
133
Alex Tomas64769242008-07-11 19:27:31 -0400134static void ext4_invalidatepage(struct page *page, unsigned long offset);
Theodore Ts'ocb20d512010-10-27 21:30:09 -0400135static int __ext4_journalled_writepage(struct page *page, unsigned int len);
136static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
Eric Sandeen5f163cc2012-01-04 22:33:28 -0500137static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle,
138 struct inode *inode, struct page *page, loff_t from,
139 loff_t length, int flags);
Alex Tomas64769242008-07-11 19:27:31 -0400140
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700141/*
142 * Test whether an inode is a fast symlink.
143 */
Mingming Cao617ba132006-10-11 01:20:53 -0700144static int ext4_inode_is_fast_symlink(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700145{
Mingming Cao617ba132006-10-11 01:20:53 -0700146 int ea_blocks = EXT4_I(inode)->i_file_acl ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700147 (inode->i_sb->s_blocksize >> 9) : 0;
148
149 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
150}
151
152/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700153 * Restart the transaction associated with *handle. This does a commit,
154 * so before we call here everything must be consistently dirtied against
155 * this transaction.
156 */
Aneesh Kumar K.Vfa5d1112009-11-02 18:50:49 -0500157int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode,
Jan Kara487caee2009-08-17 22:17:20 -0400158 int nblocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700159{
Jan Kara487caee2009-08-17 22:17:20 -0400160 int ret;
161
162 /*
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400163 * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
Jan Kara487caee2009-08-17 22:17:20 -0400164 * moment, get_block can be called only for blocks inside i_size since
165 * page cache has been already dropped and writes are blocked by
166 * i_mutex. So we can safely drop the i_data_sem here.
167 */
Frank Mayhar03901312009-01-07 00:06:22 -0500168 BUG_ON(EXT4_JOURNAL(inode) == NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700169 jbd_debug(2, "restarting handle %p\n", handle);
Jan Kara487caee2009-08-17 22:17:20 -0400170 up_write(&EXT4_I(inode)->i_data_sem);
Amir Goldstein8e8eaab2011-02-27 23:32:12 -0500171 ret = ext4_journal_restart(handle, nblocks);
Jan Kara487caee2009-08-17 22:17:20 -0400172 down_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vfa5d1112009-11-02 18:50:49 -0500173 ext4_discard_preallocations(inode);
Jan Kara487caee2009-08-17 22:17:20 -0400174
175 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700176}
177
178/*
179 * Called at the last iput() if i_nlink is zero.
180 */
Al Viro0930fcc2010-06-07 13:16:22 -0400181void ext4_evict_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700182{
183 handle_t *handle;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400184 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700185
Theodore Ts'o7ff9c072010-11-08 13:51:33 -0500186 trace_ext4_evict_inode(inode);
Jiaying Zhang2581fdc2011-08-13 12:17:13 -0400187
Jiaying Zhang2581fdc2011-08-13 12:17:13 -0400188 ext4_ioend_wait(inode);
189
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) &&
210 (S_ISLNK(inode->i_mode) || S_ISREG(inode->i_mode))) {
211 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
212 tid_t commit_tid = EXT4_I(inode)->i_datasync_tid;
213
214 jbd2_log_start_commit(journal, commit_tid);
215 jbd2_log_wait_commit(journal, commit_tid);
216 filemap_write_and_wait(&inode->i_data);
217 }
Al Viro0930fcc2010-06-07 13:16:22 -0400218 truncate_inode_pages(&inode->i_data, 0);
219 goto no_delete;
220 }
221
Christoph Hellwig907f4552010-03-03 09:05:06 -0500222 if (!is_bad_inode(inode))
Christoph Hellwig871a2932010-03-03 09:05:07 -0500223 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500224
Jan Kara678aaf42008-07-11 19:27:31 -0400225 if (ext4_should_order_data(inode))
226 ext4_begin_ordered_truncate(inode, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700227 truncate_inode_pages(&inode->i_data, 0);
228
229 if (is_bad_inode(inode))
230 goto no_delete;
231
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200232 /*
233 * Protect us against freezing - iput() caller didn't have to have any
234 * protection against it
235 */
236 sb_start_intwrite(inode->i_sb);
Theodore Ts'o9924a922013-02-08 21:59:22 -0500237 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
238 ext4_blocks_for_truncate(inode)+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700239 if (IS_ERR(handle)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400240 ext4_std_error(inode->i_sb, PTR_ERR(handle));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700241 /*
242 * If we're going to skip the normal cleanup, we still need to
243 * make sure that the in-core orphan linked list is properly
244 * cleaned up.
245 */
Mingming Cao617ba132006-10-11 01:20:53 -0700246 ext4_orphan_del(NULL, inode);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200247 sb_end_intwrite(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700248 goto no_delete;
249 }
250
251 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500252 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700253 inode->i_size = 0;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400254 err = ext4_mark_inode_dirty(handle, inode);
255 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500256 ext4_warning(inode->i_sb,
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400257 "couldn't mark inode dirty (err %d)", err);
258 goto stop_handle;
259 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700260 if (inode->i_blocks)
Mingming Cao617ba132006-10-11 01:20:53 -0700261 ext4_truncate(inode);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400262
263 /*
264 * ext4_ext_truncate() doesn't reserve any slop when it
265 * restarts journal transactions; therefore there may not be
266 * enough credits left in the handle to remove the inode from
267 * the orphan list and set the dtime field.
268 */
Frank Mayhar03901312009-01-07 00:06:22 -0500269 if (!ext4_handle_has_enough_credits(handle, 3)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400270 err = ext4_journal_extend(handle, 3);
271 if (err > 0)
272 err = ext4_journal_restart(handle, 3);
273 if (err != 0) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500274 ext4_warning(inode->i_sb,
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400275 "couldn't extend journal (err %d)", err);
276 stop_handle:
277 ext4_journal_stop(handle);
Theodore Ts'o45388212010-07-29 15:06:10 -0400278 ext4_orphan_del(NULL, inode);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200279 sb_end_intwrite(inode->i_sb);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400280 goto no_delete;
281 }
282 }
283
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700284 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700285 * Kill off the orphan record which ext4_truncate created.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700286 * AKPM: I think this can be inside the above `if'.
Mingming Cao617ba132006-10-11 01:20:53 -0700287 * Note that ext4_orphan_del() has to be able to cope with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700288 * deletion of a non-existent orphan - this is because we don't
Mingming Cao617ba132006-10-11 01:20:53 -0700289 * know if ext4_truncate() actually created an orphan record.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700290 * (Well, we could do this if we need to, but heck - it works)
291 */
Mingming Cao617ba132006-10-11 01:20:53 -0700292 ext4_orphan_del(handle, inode);
293 EXT4_I(inode)->i_dtime = get_seconds();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700294
295 /*
296 * One subtle ordering requirement: if anything has gone wrong
297 * (transaction abort, IO errors, whatever), then we can still
298 * do these next steps (the fs will already have been marked as
299 * having errors), but we can't free the inode if the mark_dirty
300 * fails.
301 */
Mingming Cao617ba132006-10-11 01:20:53 -0700302 if (ext4_mark_inode_dirty(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700303 /* If that failed, just do the required in-core inode clear. */
Al Viro0930fcc2010-06-07 13:16:22 -0400304 ext4_clear_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700305 else
Mingming Cao617ba132006-10-11 01:20:53 -0700306 ext4_free_inode(handle, inode);
307 ext4_journal_stop(handle);
Jan Kara8e8ad8a2012-06-12 16:20:38 +0200308 sb_end_intwrite(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700309 return;
310no_delete:
Al Viro0930fcc2010-06-07 13:16:22 -0400311 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700312}
313
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300314#ifdef CONFIG_QUOTA
315qsize_t *ext4_get_reserved_space(struct inode *inode)
Mingming Cao60e58e02009-01-22 18:13:05 +0100316{
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300317 return &EXT4_I(inode)->i_reserved_quota;
Mingming Cao60e58e02009-01-22 18:13:05 +0100318}
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300319#endif
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500320
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400321/*
322 * Calculate the number of metadata blocks need to reserve
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500323 * to allocate a block located at @lblock
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400324 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -0500325static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400326{
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400327 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500328 return ext4_ext_calc_metadata_amount(inode, lblock);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400329
Amir Goldstein8bb2b242011-06-27 17:10:28 -0400330 return ext4_ind_calc_metadata_amount(inode, lblock);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400331}
332
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500333/*
334 * Called with i_data_sem down, which is important since we can call
335 * ext4_discard_preallocations() from here.
336 */
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500337void ext4_da_update_reserve_space(struct inode *inode,
338 int used, int quota_claim)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400339{
340 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500341 struct ext4_inode_info *ei = EXT4_I(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400342
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500343 spin_lock(&ei->i_block_reservation_lock);
Aditya Kalid8990242011-09-09 19:18:51 -0400344 trace_ext4_da_update_reserve_space(inode, used, quota_claim);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500345 if (unlikely(used > ei->i_reserved_data_blocks)) {
Theodore Ts'o8de5c322013-02-14 15:11:41 -0500346 ext4_warning(inode->i_sb, "%s: ino %lu, used %d "
Theodore Ts'o1084f252012-03-19 23:13:43 -0400347 "with only %d reserved data blocks",
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500348 __func__, inode->i_ino, used,
349 ei->i_reserved_data_blocks);
350 WARN_ON(1);
351 used = ei->i_reserved_data_blocks;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400352 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400353
Brian Foster97795d22012-07-22 23:59:40 -0400354 if (unlikely(ei->i_allocated_meta_blocks > ei->i_reserved_meta_blocks)) {
Theodore Ts'o01a523e2013-02-14 15:51:58 -0500355 ext4_warning(inode->i_sb, "ino %lu, allocated %d "
356 "with only %d reserved metadata blocks "
357 "(releasing %d blocks with reserved %d data blocks)",
358 inode->i_ino, ei->i_allocated_meta_blocks,
359 ei->i_reserved_meta_blocks, used,
360 ei->i_reserved_data_blocks);
Brian Foster97795d22012-07-22 23:59:40 -0400361 WARN_ON(1);
362 ei->i_allocated_meta_blocks = ei->i_reserved_meta_blocks;
363 }
364
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500365 /* Update per-inode reservations */
366 ei->i_reserved_data_blocks -= used;
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500367 ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks;
Theodore Ts'o57042652011-09-09 18:56:51 -0400368 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400369 used + ei->i_allocated_meta_blocks);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500370 ei->i_allocated_meta_blocks = 0;
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500371
372 if (ei->i_reserved_data_blocks == 0) {
373 /*
374 * We can release all of the reserved metadata blocks
375 * only when we have written all of the delayed
376 * allocation blocks.
377 */
Theodore Ts'o57042652011-09-09 18:56:51 -0400378 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400379 ei->i_reserved_meta_blocks);
Theodore Ts'oee5f4d92010-01-01 02:36:15 -0500380 ei->i_reserved_meta_blocks = 0;
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500381 ei->i_da_metadata_calc_len = 0;
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500382 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400383 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +0100384
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400385 /* Update quota subsystem for data blocks */
386 if (quota_claim)
Aditya Kali7b415bf2011-09-09 19:04:51 -0400387 dquot_claim_block(inode, EXT4_C2B(sbi, used));
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400388 else {
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500389 /*
390 * We did fallocate with an offset that is already delayed
391 * allocated. So on delayed allocated writeback we should
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400392 * not re-claim the quota for fallocated blocks.
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500393 */
Aditya Kali7b415bf2011-09-09 19:04:51 -0400394 dquot_release_reservation_block(inode, EXT4_C2B(sbi, used));
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500395 }
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -0400396
397 /*
398 * If we have done all the pending block allocations and if
399 * there aren't any writers on the inode, we can discard the
400 * inode's preallocations.
401 */
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500402 if ((ei->i_reserved_data_blocks == 0) &&
403 (atomic_read(&inode->i_writecount) == 0))
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -0400404 ext4_discard_preallocations(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400405}
406
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400407static int __check_block_validity(struct inode *inode, const char *func,
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400408 unsigned int line,
409 struct ext4_map_blocks *map)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400410{
Theodore Ts'o24676da2010-05-16 21:00:00 -0400411 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
412 map->m_len)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400413 ext4_error_inode(inode, func, line, map->m_pblk,
414 "lblock %lu mapped to illegal pblock "
415 "(length %d)", (unsigned long) map->m_lblk,
416 map->m_len);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400417 return -EIO;
418 }
419 return 0;
420}
421
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400422#define check_block_validity(inode, map) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400423 __check_block_validity((inode), __func__, __LINE__, (map))
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400424
Mingming Caof5ab0d12008-02-25 15:29:55 -0500425/*
Theodore Ts'o1f945332009-09-30 22:57:41 -0400426 * Return the number of contiguous dirty pages in a given inode
427 * starting at page frame idx.
Theodore Ts'o55138e02009-09-29 13:31:31 -0400428 */
429static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx,
430 unsigned int max_pages)
431{
432 struct address_space *mapping = inode->i_mapping;
433 pgoff_t index;
434 struct pagevec pvec;
435 pgoff_t num = 0;
436 int i, nr_pages, done = 0;
437
438 if (max_pages == 0)
439 return 0;
440 pagevec_init(&pvec, 0);
441 while (!done) {
442 index = idx;
443 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
444 PAGECACHE_TAG_DIRTY,
445 (pgoff_t)PAGEVEC_SIZE);
446 if (nr_pages == 0)
447 break;
448 for (i = 0; i < nr_pages; i++) {
449 struct page *page = pvec.pages[i];
450 struct buffer_head *bh, *head;
451
452 lock_page(page);
453 if (unlikely(page->mapping != mapping) ||
454 !PageDirty(page) ||
455 PageWriteback(page) ||
456 page->index != idx) {
457 done = 1;
458 unlock_page(page);
459 break;
460 }
Theodore Ts'o1f945332009-09-30 22:57:41 -0400461 if (page_has_buffers(page)) {
462 bh = head = page_buffers(page);
463 do {
464 if (!buffer_delay(bh) &&
465 !buffer_unwritten(bh))
466 done = 1;
467 bh = bh->b_this_page;
468 } while (!done && (bh != head));
469 }
Theodore Ts'o55138e02009-09-29 13:31:31 -0400470 unlock_page(page);
471 if (done)
472 break;
473 idx++;
474 num++;
Eric Sandeen659c6002010-10-27 21:30:03 -0400475 if (num >= max_pages) {
476 done = 1;
Theodore Ts'o55138e02009-09-29 13:31:31 -0400477 break;
Eric Sandeen659c6002010-10-27 21:30:03 -0400478 }
Theodore Ts'o55138e02009-09-29 13:31:31 -0400479 }
480 pagevec_release(&pvec);
481 }
482 return num;
483}
484
485/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400486 * The ext4_map_blocks() function tries to look up the requested blocks,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400487 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500488 *
Mingming Caof5ab0d12008-02-25 15:29:55 -0500489 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
490 * and store the allocated blocks in the result buffer head and mark it
491 * mapped.
492 *
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400493 * If file type is extents based, it will call ext4_ext_map_blocks(),
494 * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
Mingming Caof5ab0d12008-02-25 15:29:55 -0500495 * based files
496 *
497 * On success, it returns the number of blocks being mapped or allocate.
498 * if create==0 and the blocks are pre-allocated and uninitialized block,
499 * the result buffer head is unmapped. If the create ==1, it will make sure
500 * the buffer head is mapped.
501 *
502 * It returns 0 if plain look up failed (blocks have not been allocated), in
Tao Madf3ab172011-10-08 15:53:49 -0400503 * that case, buffer head is unmapped
Mingming Caof5ab0d12008-02-25 15:29:55 -0500504 *
505 * It returns the error in case of allocation failure.
506 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400507int ext4_map_blocks(handle_t *handle, struct inode *inode,
508 struct ext4_map_blocks *map, int flags)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500509{
510 int retval;
Mingming Caof5ab0d12008-02-25 15:29:55 -0500511
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400512 map->m_flags = 0;
513 ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
514 "logical block %lu\n", inode->i_ino, flags, map->m_len,
515 (unsigned long) map->m_lblk);
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500516 /*
Theodore Ts'ob920c752009-05-14 00:54:29 -0400517 * Try to see if we can get the block without requesting a new
518 * file system block.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500519 */
Zheng Liu729f52c2012-07-09 16:29:29 -0400520 if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
521 down_read((&EXT4_I(inode)->i_data_sem));
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400522 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Dmitry Monakhova4e5d882011-10-25 08:15:12 -0400523 retval = ext4_ext_map_blocks(handle, inode, map, flags &
524 EXT4_GET_BLOCKS_KEEP_SIZE);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500525 } else {
Dmitry Monakhova4e5d882011-10-25 08:15:12 -0400526 retval = ext4_ind_map_blocks(handle, inode, map, flags &
527 EXT4_GET_BLOCKS_KEEP_SIZE);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500528 }
Zheng Liuf7fec032013-02-18 00:28:47 -0500529 if (retval > 0) {
530 int ret;
531 unsigned long long status;
532
533 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
534 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
535 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
536 ext4_find_delalloc_range(inode, map->m_lblk,
537 map->m_lblk + map->m_len - 1))
538 status |= EXTENT_STATUS_DELAYED;
539 ret = ext4_es_insert_extent(inode, map->m_lblk,
540 map->m_len, map->m_pblk, status);
541 if (ret < 0)
542 retval = ret;
543 }
Zheng Liu729f52c2012-07-09 16:29:29 -0400544 if (!(flags & EXT4_GET_BLOCKS_NO_LOCK))
545 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -0500546
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400547 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Zheng Liuf7fec032013-02-18 00:28:47 -0500548 int ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400549 if (ret != 0)
550 return ret;
551 }
552
Mingming Caof5ab0d12008-02-25 15:29:55 -0500553 /* If it is only a block(s) look up */
Theodore Ts'oc2177052009-05-14 00:58:52 -0400554 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500555 return retval;
556
557 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -0500558 * Returns if the blocks have already allocated
559 *
560 * Note that if blocks have been preallocated
Tao Madf3ab172011-10-08 15:53:49 -0400561 * ext4_ext_get_block() returns the create = 0
Mingming Caof5ab0d12008-02-25 15:29:55 -0500562 * with buffer head unmapped.
563 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400564 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
Mingming Caof5ab0d12008-02-25 15:29:55 -0500565 return retval;
566
567 /*
Zheng Liua25a4e12013-02-18 00:28:04 -0500568 * Here we clear m_flags because after allocating an new extent,
569 * it will be set again.
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -0400570 */
Zheng Liua25a4e12013-02-18 00:28:04 -0500571 map->m_flags &= ~EXT4_MAP_FLAGS;
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -0400572
573 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -0500574 * New blocks allocate and/or writing to uninitialized extent
575 * will possibly result in updating i_data, so we take
576 * the write lock of i_data_sem, and call get_blocks()
577 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500578 */
579 down_write((&EXT4_I(inode)->i_data_sem));
Mingming Caod2a17632008-07-14 17:52:37 -0400580
581 /*
582 * if the caller is from delayed allocation writeout path
583 * we have already reserved fs blocks for allocation
584 * let the underlying get_block() function know to
585 * avoid double accounting
586 */
Theodore Ts'oc2177052009-05-14 00:58:52 -0400587 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Theodore Ts'of2321092011-01-10 12:12:36 -0500588 ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500589 /*
590 * We need to check for EXT4 here because migrate
591 * could have changed the inode type in between
592 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400593 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400594 retval = ext4_ext_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500595 } else {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400596 retval = ext4_ind_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400597
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400598 if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400599 /*
600 * We allocated new blocks which will result in
601 * i_data's format changing. Force the migrate
602 * to fail by clearing migrate flags
603 */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500604 ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400605 }
Mingming Caod2a17632008-07-14 17:52:37 -0400606
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500607 /*
608 * Update reserved blocks/metadata blocks after successful
609 * block allocation which had been deferred till now. We don't
610 * support fallocate for non extent files. So we can update
611 * reserve space here.
612 */
613 if ((retval > 0) &&
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -0500614 (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500615 ext4_da_update_reserve_space(inode, retval, 1);
616 }
Zheng Liuf7fec032013-02-18 00:28:47 -0500617 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Theodore Ts'of2321092011-01-10 12:12:36 -0500618 ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -0400619
Zheng Liuf7fec032013-02-18 00:28:47 -0500620 if (retval > 0) {
621 int ret;
622 unsigned long long status;
623
624 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
625 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
626 if (!(flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) &&
627 ext4_find_delalloc_range(inode, map->m_lblk,
628 map->m_lblk + map->m_len - 1))
629 status |= EXTENT_STATUS_DELAYED;
630 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
631 map->m_pblk, status);
632 if (ret < 0)
633 retval = ret;
Aditya Kali5356f262011-09-09 19:20:51 -0400634 }
635
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500636 up_write((&EXT4_I(inode)->i_data_sem));
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400637 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400638 int ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400639 if (ret != 0)
640 return ret;
641 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500642 return retval;
643}
644
Mingming Caof3bd1f32008-08-19 22:16:03 -0400645/* Maximum number of blocks we map for direct IO at once. */
646#define DIO_MAX_BLOCKS 4096
647
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400648static int _ext4_get_block(struct inode *inode, sector_t iblock,
649 struct buffer_head *bh, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700650{
Dmitriy Monakhov3e4fdaf2007-02-10 01:46:35 -0800651 handle_t *handle = ext4_journal_current_handle();
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400652 struct ext4_map_blocks map;
Jan Kara7fb54092008-02-10 01:08:38 -0500653 int ret = 0, started = 0;
Mingming Caof3bd1f32008-08-19 22:16:03 -0400654 int dio_credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700655
Tao Ma46c7f252012-12-10 14:04:52 -0500656 if (ext4_has_inline_data(inode))
657 return -ERANGE;
658
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400659 map.m_lblk = iblock;
660 map.m_len = bh->b_size >> inode->i_blkbits;
661
Anatol Pomozov8b0f1652012-11-08 15:07:16 -0500662 if (flags && !(flags & EXT4_GET_BLOCKS_NO_LOCK) && !handle) {
Jan Kara7fb54092008-02-10 01:08:38 -0500663 /* Direct IO write... */
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400664 if (map.m_len > DIO_MAX_BLOCKS)
665 map.m_len = DIO_MAX_BLOCKS;
666 dio_credits = ext4_chunk_trans_blocks(inode, map.m_len);
Theodore Ts'o9924a922013-02-08 21:59:22 -0500667 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
668 dio_credits);
Jan Kara7fb54092008-02-10 01:08:38 -0500669 if (IS_ERR(handle)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700670 ret = PTR_ERR(handle);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400671 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700672 }
Jan Kara7fb54092008-02-10 01:08:38 -0500673 started = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700674 }
675
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400676 ret = ext4_map_blocks(handle, inode, &map, flags);
Jan Kara7fb54092008-02-10 01:08:38 -0500677 if (ret > 0) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400678 map_bh(bh, inode->i_sb, map.m_pblk);
679 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
680 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
Jan Kara7fb54092008-02-10 01:08:38 -0500681 ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700682 }
Jan Kara7fb54092008-02-10 01:08:38 -0500683 if (started)
684 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700685 return ret;
686}
687
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400688int ext4_get_block(struct inode *inode, sector_t iblock,
689 struct buffer_head *bh, int create)
690{
691 return _ext4_get_block(inode, iblock, bh,
692 create ? EXT4_GET_BLOCKS_CREATE : 0);
693}
694
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700695/*
696 * `handle' can be NULL if create is zero
697 */
Mingming Cao617ba132006-10-11 01:20:53 -0700698struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500699 ext4_lblk_t block, int create, int *errp)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700700{
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400701 struct ext4_map_blocks map;
702 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700703 int fatal = 0, err;
704
705 J_ASSERT(handle != NULL || create == 0);
706
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400707 map.m_lblk = block;
708 map.m_len = 1;
709 err = ext4_map_blocks(handle, inode, &map,
710 create ? EXT4_GET_BLOCKS_CREATE : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700711
Carlos Maiolino90b0a972012-09-17 23:39:12 -0400712 /* ensure we send some value back into *errp */
713 *errp = 0;
714
Theodore Ts'o0f70b402013-02-15 03:35:57 -0500715 if (create && err == 0)
716 err = -ENOSPC; /* should never happen */
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400717 if (err < 0)
718 *errp = err;
719 if (err <= 0)
720 return NULL;
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400721
722 bh = sb_getblk(inode->i_sb, map.m_pblk);
Wang Shilongaebf0242013-01-12 16:28:47 -0500723 if (unlikely(!bh)) {
Theodore Ts'o860d21e2013-01-12 16:19:36 -0500724 *errp = -ENOMEM;
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400725 return NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700726 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400727 if (map.m_flags & EXT4_MAP_NEW) {
728 J_ASSERT(create != 0);
729 J_ASSERT(handle != NULL);
730
731 /*
732 * Now that we do not always journal data, we should
733 * keep in mind whether this should always journal the
734 * new buffer as metadata. For now, regular file
735 * writes use ext4_get_block instead, so it's not a
736 * problem.
737 */
738 lock_buffer(bh);
739 BUFFER_TRACE(bh, "call get_create_access");
740 fatal = ext4_journal_get_create_access(handle, bh);
741 if (!fatal && !buffer_uptodate(bh)) {
742 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
743 set_buffer_uptodate(bh);
744 }
745 unlock_buffer(bh);
746 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
747 err = ext4_handle_dirty_metadata(handle, inode, bh);
748 if (!fatal)
749 fatal = err;
750 } else {
751 BUFFER_TRACE(bh, "not a new buffer");
752 }
753 if (fatal) {
754 *errp = fatal;
755 brelse(bh);
756 bh = NULL;
757 }
758 return bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700759}
760
Mingming Cao617ba132006-10-11 01:20:53 -0700761struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500762 ext4_lblk_t block, int create, int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700763{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400764 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700765
Mingming Cao617ba132006-10-11 01:20:53 -0700766 bh = ext4_getblk(handle, inode, block, create, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700767 if (!bh)
768 return bh;
769 if (buffer_uptodate(bh))
770 return bh;
Christoph Hellwig65299a32011-08-23 14:50:29 +0200771 ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700772 wait_on_buffer(bh);
773 if (buffer_uptodate(bh))
774 return bh;
775 put_bh(bh);
776 *err = -EIO;
777 return NULL;
778}
779
Tao Maf19d5872012-12-10 14:05:51 -0500780int ext4_walk_page_buffers(handle_t *handle,
781 struct buffer_head *head,
782 unsigned from,
783 unsigned to,
784 int *partial,
785 int (*fn)(handle_t *handle,
786 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700787{
788 struct buffer_head *bh;
789 unsigned block_start, block_end;
790 unsigned blocksize = head->b_size;
791 int err, ret = 0;
792 struct buffer_head *next;
793
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400794 for (bh = head, block_start = 0;
795 ret == 0 && (bh != head || !block_start);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400796 block_start = block_end, bh = next) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700797 next = bh->b_this_page;
798 block_end = block_start + blocksize;
799 if (block_end <= from || block_start >= to) {
800 if (partial && !buffer_uptodate(bh))
801 *partial = 1;
802 continue;
803 }
804 err = (*fn)(handle, bh);
805 if (!ret)
806 ret = err;
807 }
808 return ret;
809}
810
811/*
812 * To preserve ordering, it is essential that the hole instantiation and
813 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -0700814 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -0700815 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700816 * prepare_write() is the right place.
817 *
Jan Kara36ade452013-01-28 09:30:52 -0500818 * Also, this function can nest inside ext4_writepage(). In that case, we
819 * *know* that ext4_writepage() has generated enough buffer credits to do the
820 * whole page. So we won't block on the journal in that case, which is good,
821 * because the caller may be PF_MEMALLOC.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700822 *
Mingming Cao617ba132006-10-11 01:20:53 -0700823 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700824 * quota file writes. If we were to commit the transaction while thus
825 * reentered, there can be a deadlock - we would be holding a quota
826 * lock, and the commit would never complete if another thread had a
827 * transaction open and was blocking on the quota lock - a ranking
828 * violation.
829 *
Mingming Caodab291a2006-10-11 01:21:01 -0700830 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700831 * will _not_ run commit under these circumstances because handle->h_ref
832 * is elevated. We'll still have enough credits for the tiny quotafile
833 * write.
834 */
Tao Maf19d5872012-12-10 14:05:51 -0500835int do_journal_get_write_access(handle_t *handle,
836 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700837{
Jan Kara56d35a42010-08-05 14:41:42 -0400838 int dirty = buffer_dirty(bh);
839 int ret;
840
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700841 if (!buffer_mapped(bh) || buffer_freed(bh))
842 return 0;
Jan Kara56d35a42010-08-05 14:41:42 -0400843 /*
Christoph Hellwigebdec242010-10-06 10:47:23 +0200844 * __block_write_begin() could have dirtied some buffers. Clean
Jan Kara56d35a42010-08-05 14:41:42 -0400845 * the dirty bit as jbd2_journal_get_write_access() could complain
846 * otherwise about fs integrity issues. Setting of the dirty bit
Christoph Hellwigebdec242010-10-06 10:47:23 +0200847 * by __block_write_begin() isn't a real problem here as we clear
Jan Kara56d35a42010-08-05 14:41:42 -0400848 * the bit before releasing a page lock and thus writeback cannot
849 * ever write the buffer.
850 */
851 if (dirty)
852 clear_buffer_dirty(bh);
853 ret = ext4_journal_get_write_access(handle, bh);
854 if (!ret && dirty)
855 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
856 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700857}
858
Anatol Pomozov8b0f1652012-11-08 15:07:16 -0500859static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock,
860 struct buffer_head *bh_result, int create);
Nick Pigginbfc1af62007-10-16 01:25:05 -0700861static int ext4_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400862 loff_t pos, unsigned len, unsigned flags,
863 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700864{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400865 struct inode *inode = mapping->host;
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -0400866 int ret, needed_blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700867 handle_t *handle;
868 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400869 struct page *page;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400870 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400871 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -0700872
Theodore Ts'o9bffad12009-06-17 11:48:11 -0400873 trace_ext4_write_begin(inode, pos, len, flags);
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -0400874 /*
875 * Reserve one block more for addition to orphan list in case
876 * we allocate blocks but write fails for some reason
877 */
878 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400879 index = pos >> PAGE_CACHE_SHIFT;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400880 from = pos & (PAGE_CACHE_SIZE - 1);
881 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700882
Tao Maf19d5872012-12-10 14:05:51 -0500883 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
884 ret = ext4_try_to_write_inline_data(mapping, inode, pos, len,
885 flags, pagep);
886 if (ret < 0)
Theodore Ts'o47564bf2013-02-09 09:24:14 -0500887 return ret;
888 if (ret == 1)
889 return 0;
Tao Maf19d5872012-12-10 14:05:51 -0500890 }
891
Theodore Ts'o47564bf2013-02-09 09:24:14 -0500892 /*
893 * grab_cache_page_write_begin() can take a long time if the
894 * system is thrashing due to memory pressure, or if the page
895 * is being written back. So grab it first before we start
896 * the transaction handle. This also allows us to allocate
897 * the page (if needed) without using GFP_NOFS.
898 */
899retry_grab:
900 page = grab_cache_page_write_begin(mapping, index, flags);
901 if (!page)
902 return -ENOMEM;
903 unlock_page(page);
904
905retry_journal:
Theodore Ts'o9924a922013-02-08 21:59:22 -0500906 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400907 if (IS_ERR(handle)) {
Theodore Ts'o47564bf2013-02-09 09:24:14 -0500908 page_cache_release(page);
909 return PTR_ERR(handle);
Nick Pigginbfc1af62007-10-16 01:25:05 -0700910 }
911
Theodore Ts'o47564bf2013-02-09 09:24:14 -0500912 lock_page(page);
913 if (page->mapping != mapping) {
914 /* The page got truncated from under us */
915 unlock_page(page);
916 page_cache_release(page);
Jan Karacf108bc2008-07-11 19:27:31 -0400917 ext4_journal_stop(handle);
Theodore Ts'o47564bf2013-02-09 09:24:14 -0500918 goto retry_grab;
Jan Karacf108bc2008-07-11 19:27:31 -0400919 }
Theodore Ts'o47564bf2013-02-09 09:24:14 -0500920 wait_on_page_writeback(page);
Jan Karacf108bc2008-07-11 19:27:31 -0400921
Jiaying Zhang744692d2010-03-04 16:14:02 -0500922 if (ext4_should_dioread_nolock(inode))
Christoph Hellwig6e1db882010-06-04 11:29:57 +0200923 ret = __block_write_begin(page, pos, len, ext4_get_block_write);
Jiaying Zhang744692d2010-03-04 16:14:02 -0500924 else
Christoph Hellwig6e1db882010-06-04 11:29:57 +0200925 ret = __block_write_begin(page, pos, len, ext4_get_block);
Nick Pigginbfc1af62007-10-16 01:25:05 -0700926
927 if (!ret && ext4_should_journal_data(inode)) {
Tao Maf19d5872012-12-10 14:05:51 -0500928 ret = ext4_walk_page_buffers(handle, page_buffers(page),
929 from, to, NULL,
930 do_journal_get_write_access);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700931 }
Nick Pigginbfc1af62007-10-16 01:25:05 -0700932
933 if (ret) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400934 unlock_page(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -0400935 /*
Christoph Hellwig6e1db882010-06-04 11:29:57 +0200936 * __block_write_begin may have instantiated a few blocks
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -0400937 * outside i_size. Trim these off again. Don't need
938 * i_size_read because we hold i_mutex.
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -0400939 *
940 * Add inode to orphan list in case we crash before
941 * truncate finishes
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -0400942 */
Jan Karaffacfa72009-07-13 16:22:22 -0400943 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -0400944 ext4_orphan_add(handle, inode);
945
946 ext4_journal_stop(handle);
947 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -0500948 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400949 /*
Jan Karaffacfa72009-07-13 16:22:22 -0400950 * If truncate failed early the inode might
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -0400951 * still be on the orphan list; we need to
952 * make sure the inode is removed from the
953 * orphan list in that case.
954 */
955 if (inode->i_nlink)
956 ext4_orphan_del(NULL, inode);
957 }
Nick Pigginbfc1af62007-10-16 01:25:05 -0700958
Theodore Ts'o47564bf2013-02-09 09:24:14 -0500959 if (ret == -ENOSPC &&
960 ext4_should_retry_alloc(inode->i_sb, &retries))
961 goto retry_journal;
962 page_cache_release(page);
963 return ret;
964 }
965 *pagep = page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700966 return ret;
967}
968
Nick Pigginbfc1af62007-10-16 01:25:05 -0700969/* For write_end() in data=journal mode */
970static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700971{
972 if (!buffer_mapped(bh) || buffer_freed(bh))
973 return 0;
974 set_buffer_uptodate(bh);
Frank Mayhar03901312009-01-07 00:06:22 -0500975 return ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700976}
977
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -0400978static int ext4_generic_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400979 struct address_space *mapping,
980 loff_t pos, unsigned len, unsigned copied,
981 struct page *page, void *fsdata)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -0400982{
983 int i_size_changed = 0;
984 struct inode *inode = mapping->host;
985 handle_t *handle = ext4_journal_current_handle();
986
Tao Maf19d5872012-12-10 14:05:51 -0500987 if (ext4_has_inline_data(inode))
988 copied = ext4_write_inline_data_end(inode, pos, len,
989 copied, page);
990 else
991 copied = block_write_end(file, mapping, pos,
992 len, copied, page, fsdata);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -0400993
994 /*
995 * No need to use i_size_read() here, the i_size
996 * cannot change under us because we hold i_mutex.
997 *
998 * But it's important to update i_size while still holding page lock:
999 * page writeout could otherwise come in and zero beyond i_size.
1000 */
1001 if (pos + copied > inode->i_size) {
1002 i_size_write(inode, pos + copied);
1003 i_size_changed = 1;
1004 }
1005
1006 if (pos + copied > EXT4_I(inode)->i_disksize) {
1007 /* We need to mark inode dirty even if
1008 * new_i_size is less that inode->i_size
1009 * bu greater than i_disksize.(hint delalloc)
1010 */
1011 ext4_update_i_disksize(inode, (pos + copied));
1012 i_size_changed = 1;
1013 }
1014 unlock_page(page);
1015 page_cache_release(page);
1016
1017 /*
1018 * Don't mark the inode dirty under page lock. First, it unnecessarily
1019 * makes the holding time of page lock longer. Second, it forces lock
1020 * ordering of page lock and transaction start for journaling
1021 * filesystems.
1022 */
1023 if (i_size_changed)
1024 ext4_mark_inode_dirty(handle, inode);
1025
1026 return copied;
1027}
1028
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001029/*
1030 * We need to pick up the new inode size which generic_commit_write gave us
1031 * `file' can be NULL - eg, when called from page_symlink().
1032 *
Mingming Cao617ba132006-10-11 01:20:53 -07001033 * ext4 never places buffers on inode->i_mapping->private_list. metadata
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001034 * buffers are managed internally.
1035 */
Nick Pigginbfc1af62007-10-16 01:25:05 -07001036static int ext4_ordered_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001037 struct address_space *mapping,
1038 loff_t pos, unsigned len, unsigned copied,
1039 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001040{
Mingming Cao617ba132006-10-11 01:20:53 -07001041 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001042 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001043 int ret = 0, ret2;
1044
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001045 trace_ext4_ordered_write_end(inode, pos, len, copied);
Jan Kara678aaf42008-07-11 19:27:31 -04001046 ret = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001047
1048 if (ret == 0) {
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001049 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001050 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001051 copied = ret2;
Jan Karaffacfa72009-07-13 16:22:22 -04001052 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001053 /* if we have allocated more blocks and copied
1054 * less. We will have blocks allocated outside
1055 * inode->i_size. So truncate them
1056 */
1057 ext4_orphan_add(handle, inode);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001058 if (ret2 < 0)
1059 ret = ret2;
Akira Fujita09e08342011-10-20 18:56:10 -04001060 } else {
1061 unlock_page(page);
1062 page_cache_release(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001063 }
Akira Fujita09e08342011-10-20 18:56:10 -04001064
Mingming Cao617ba132006-10-11 01:20:53 -07001065 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001066 if (!ret)
1067 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001068
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001069 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001070 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001071 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001072 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001073 * on the orphan list; we need to make sure the inode
1074 * is removed from the orphan list in that case.
1075 */
1076 if (inode->i_nlink)
1077 ext4_orphan_del(NULL, inode);
1078 }
1079
1080
Nick Pigginbfc1af62007-10-16 01:25:05 -07001081 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001082}
1083
Nick Pigginbfc1af62007-10-16 01:25:05 -07001084static int ext4_writeback_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001085 struct address_space *mapping,
1086 loff_t pos, unsigned len, unsigned copied,
1087 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001088{
Mingming Cao617ba132006-10-11 01:20:53 -07001089 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -04001090 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001091 int ret = 0, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001092
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001093 trace_ext4_writeback_write_end(inode, pos, len, copied);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001094 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -07001095 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -04001096 copied = ret2;
Jan Karaffacfa72009-07-13 16:22:22 -04001097 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001098 /* if we have allocated more blocks and copied
1099 * less. We will have blocks allocated outside
1100 * inode->i_size. So truncate them
1101 */
1102 ext4_orphan_add(handle, inode);
1103
Roel Kluinf8a87d82008-04-29 22:01:18 -04001104 if (ret2 < 0)
1105 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001106
Mingming Cao617ba132006-10-11 01:20:53 -07001107 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001108 if (!ret)
1109 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001110
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001111 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001112 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001113 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001114 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001115 * on the orphan list; we need to make sure the inode
1116 * is removed from the orphan list in that case.
1117 */
1118 if (inode->i_nlink)
1119 ext4_orphan_del(NULL, inode);
1120 }
1121
Nick Pigginbfc1af62007-10-16 01:25:05 -07001122 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001123}
1124
Nick Pigginbfc1af62007-10-16 01:25:05 -07001125static int ext4_journalled_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001126 struct address_space *mapping,
1127 loff_t pos, unsigned len, unsigned copied,
1128 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001129{
Mingming Cao617ba132006-10-11 01:20:53 -07001130 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -07001131 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001132 int ret = 0, ret2;
1133 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -07001134 unsigned from, to;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001135 loff_t new_i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001136
Theodore Ts'o9bffad12009-06-17 11:48:11 -04001137 trace_ext4_journalled_write_end(inode, pos, len, copied);
Nick Pigginbfc1af62007-10-16 01:25:05 -07001138 from = pos & (PAGE_CACHE_SIZE - 1);
1139 to = from + len;
1140
Curt Wohlgemuth441c8502011-08-13 11:25:18 -04001141 BUG_ON(!ext4_handle_valid(handle));
1142
Tao Ma3fdcfb62012-12-10 14:05:57 -05001143 if (ext4_has_inline_data(inode))
1144 copied = ext4_write_inline_data_end(inode, pos, len,
1145 copied, page);
1146 else {
1147 if (copied < len) {
1148 if (!PageUptodate(page))
1149 copied = 0;
1150 page_zero_new_buffers(page, from+copied, to);
1151 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001152
Tao Ma3fdcfb62012-12-10 14:05:57 -05001153 ret = ext4_walk_page_buffers(handle, page_buffers(page), from,
1154 to, &partial, write_end_fn);
1155 if (!partial)
1156 SetPageUptodate(page);
1157 }
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001158 new_i_size = pos + copied;
1159 if (new_i_size > inode->i_size)
Nick Pigginbfc1af62007-10-16 01:25:05 -07001160 i_size_write(inode, pos+copied);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001161 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Jan Kara2d859db2011-07-26 09:07:11 -04001162 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04001163 if (new_i_size > EXT4_I(inode)->i_disksize) {
1164 ext4_update_i_disksize(inode, new_i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001165 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001166 if (!ret)
1167 ret = ret2;
1168 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001169
Jan Karacf108bc2008-07-11 19:27:31 -04001170 unlock_page(page);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001171 page_cache_release(page);
Jan Karaffacfa72009-07-13 16:22:22 -04001172 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001173 /* if we have allocated more blocks and copied
1174 * less. We will have blocks allocated outside
1175 * inode->i_size. So truncate them
1176 */
1177 ext4_orphan_add(handle, inode);
1178
Mingming Cao617ba132006-10-11 01:20:53 -07001179 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001180 if (!ret)
1181 ret = ret2;
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001182 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -05001183 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001184 /*
Jan Karaffacfa72009-07-13 16:22:22 -04001185 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -04001186 * on the orphan list; we need to make sure the inode
1187 * is removed from the orphan list in that case.
1188 */
1189 if (inode->i_nlink)
1190 ext4_orphan_del(NULL, inode);
1191 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001192
1193 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001194}
Mingming Caod2a17632008-07-14 17:52:37 -04001195
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001196/*
Aditya Kali7b415bf2011-09-09 19:04:51 -04001197 * Reserve a single cluster located at lblock
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001198 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -05001199static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -04001200{
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001201 int retries = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01001202 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001203 struct ext4_inode_info *ei = EXT4_I(inode);
Aditya Kali7b415bf2011-09-09 19:04:51 -04001204 unsigned int md_needed;
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001205 int ret;
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001206 ext4_lblk_t save_last_lblock;
1207 int save_len;
Mingming Caod2a17632008-07-14 17:52:37 -04001208
Mingming Cao60e58e02009-01-22 18:13:05 +01001209 /*
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001210 * We will charge metadata quota at writeout time; this saves
1211 * us from metadata over-estimation, though we may go over by
1212 * a small amount in the end. Here we just reserve for data.
Mingming Cao60e58e02009-01-22 18:13:05 +01001213 */
Aditya Kali7b415bf2011-09-09 19:04:51 -04001214 ret = dquot_reserve_block(inode, EXT4_C2B(sbi, 1));
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001215 if (ret)
1216 return ret;
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001217
1218 /*
1219 * recalculate the amount of metadata blocks to reserve
1220 * in order to allocate nrblocks
1221 * worse case is one extent per block
1222 */
1223repeat:
1224 spin_lock(&ei->i_block_reservation_lock);
1225 /*
1226 * ext4_calc_metadata_amount() has side effects, which we have
1227 * to be prepared undo if we fail to claim space.
1228 */
1229 save_len = ei->i_da_metadata_calc_len;
1230 save_last_lblock = ei->i_da_metadata_calc_last_lblock;
1231 md_needed = EXT4_NUM_B2C(sbi,
1232 ext4_calc_metadata_amount(inode, lblock));
1233 trace_ext4_da_reserve_space(inode, md_needed);
1234
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001235 /*
1236 * We do still charge estimated metadata to the sb though;
1237 * we cannot afford to run out of free blocks.
1238 */
Theodore Ts'oe7d5f312011-09-09 19:14:51 -04001239 if (ext4_claim_free_clusters(sbi, md_needed + 1, 0)) {
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001240 ei->i_da_metadata_calc_len = save_len;
1241 ei->i_da_metadata_calc_last_lblock = save_last_lblock;
1242 spin_unlock(&ei->i_block_reservation_lock);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001243 if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1244 yield();
1245 goto repeat;
1246 }
Theodore Ts'o03179fe2012-07-23 00:00:20 -04001247 dquot_release_reservation_block(inode, EXT4_C2B(sbi, 1));
Mingming Caod2a17632008-07-14 17:52:37 -04001248 return -ENOSPC;
1249 }
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001250 ei->i_reserved_data_blocks++;
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001251 ei->i_reserved_meta_blocks += md_needed;
1252 spin_unlock(&ei->i_block_reservation_lock);
Dmitry Monakhov39bc6802009-12-10 16:36:27 +00001253
Mingming Caod2a17632008-07-14 17:52:37 -04001254 return 0; /* success */
1255}
1256
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001257static void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001258{
1259 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001260 struct ext4_inode_info *ei = EXT4_I(inode);
Mingming Caod2a17632008-07-14 17:52:37 -04001261
Mingming Caocd213222008-08-19 22:16:59 -04001262 if (!to_free)
1263 return; /* Nothing to release, exit */
1264
Mingming Caod2a17632008-07-14 17:52:37 -04001265 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001266
Li Zefan5a58ec82010-05-17 02:00:00 -04001267 trace_ext4_da_release_space(inode, to_free);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001268 if (unlikely(to_free > ei->i_reserved_data_blocks)) {
Mingming Caocd213222008-08-19 22:16:59 -04001269 /*
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001270 * if there aren't enough reserved blocks, then the
1271 * counter is messed up somewhere. Since this
1272 * function is called from invalidate page, it's
1273 * harmless to return without any action.
Mingming Caocd213222008-08-19 22:16:59 -04001274 */
Theodore Ts'o8de5c322013-02-14 15:11:41 -05001275 ext4_warning(inode->i_sb, "ext4_da_release_space: "
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001276 "ino %lu, to_free %d with only %d reserved "
Theodore Ts'o1084f252012-03-19 23:13:43 -04001277 "data blocks", inode->i_ino, to_free,
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001278 ei->i_reserved_data_blocks);
1279 WARN_ON(1);
1280 to_free = ei->i_reserved_data_blocks;
1281 }
1282 ei->i_reserved_data_blocks -= to_free;
1283
1284 if (ei->i_reserved_data_blocks == 0) {
1285 /*
1286 * We can release all of the reserved metadata blocks
1287 * only when we have written all of the delayed
1288 * allocation blocks.
Aditya Kali7b415bf2011-09-09 19:04:51 -04001289 * Note that in case of bigalloc, i_reserved_meta_blocks,
1290 * i_reserved_data_blocks, etc. refer to number of clusters.
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001291 */
Theodore Ts'o57042652011-09-09 18:56:51 -04001292 percpu_counter_sub(&sbi->s_dirtyclusters_counter,
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001293 ei->i_reserved_meta_blocks);
Theodore Ts'oee5f4d92010-01-01 02:36:15 -05001294 ei->i_reserved_meta_blocks = 0;
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001295 ei->i_da_metadata_calc_len = 0;
Mingming Caocd213222008-08-19 22:16:59 -04001296 }
1297
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001298 /* update fs dirty data blocks counter */
Theodore Ts'o57042652011-09-09 18:56:51 -04001299 percpu_counter_sub(&sbi->s_dirtyclusters_counter, to_free);
Mingming Caod2a17632008-07-14 17:52:37 -04001300
Mingming Caod2a17632008-07-14 17:52:37 -04001301 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001302
Aditya Kali7b415bf2011-09-09 19:04:51 -04001303 dquot_release_reservation_block(inode, EXT4_C2B(sbi, to_free));
Mingming Caod2a17632008-07-14 17:52:37 -04001304}
1305
1306static void ext4_da_page_release_reservation(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001307 unsigned long offset)
Mingming Caod2a17632008-07-14 17:52:37 -04001308{
1309 int to_release = 0;
1310 struct buffer_head *head, *bh;
1311 unsigned int curr_off = 0;
Aditya Kali7b415bf2011-09-09 19:04:51 -04001312 struct inode *inode = page->mapping->host;
1313 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1314 int num_clusters;
Zheng Liu51865fd2012-11-08 21:57:32 -05001315 ext4_fsblk_t lblk;
Mingming Caod2a17632008-07-14 17:52:37 -04001316
1317 head = page_buffers(page);
1318 bh = head;
1319 do {
1320 unsigned int next_off = curr_off + bh->b_size;
1321
1322 if ((offset <= curr_off) && (buffer_delay(bh))) {
1323 to_release++;
1324 clear_buffer_delay(bh);
1325 }
1326 curr_off = next_off;
1327 } while ((bh = bh->b_this_page) != head);
Aditya Kali7b415bf2011-09-09 19:04:51 -04001328
Zheng Liu51865fd2012-11-08 21:57:32 -05001329 if (to_release) {
1330 lblk = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1331 ext4_es_remove_extent(inode, lblk, to_release);
1332 }
1333
Aditya Kali7b415bf2011-09-09 19:04:51 -04001334 /* If we have released all the blocks belonging to a cluster, then we
1335 * need to release the reserved space for that cluster. */
1336 num_clusters = EXT4_NUM_B2C(sbi, to_release);
1337 while (num_clusters > 0) {
Aditya Kali7b415bf2011-09-09 19:04:51 -04001338 lblk = (page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits)) +
1339 ((num_clusters - 1) << sbi->s_cluster_bits);
1340 if (sbi->s_cluster_ratio == 1 ||
Zheng Liu7d1b1fb2012-11-08 21:57:35 -05001341 !ext4_find_delalloc_cluster(inode, lblk))
Aditya Kali7b415bf2011-09-09 19:04:51 -04001342 ext4_da_release_space(inode, 1);
1343
1344 num_clusters--;
1345 }
Mingming Caod2a17632008-07-14 17:52:37 -04001346}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001347
1348/*
Alex Tomas64769242008-07-11 19:27:31 -04001349 * Delayed allocation stuff
1350 */
1351
Alex Tomas64769242008-07-11 19:27:31 -04001352/*
1353 * mpage_da_submit_io - walks through extent of pages and try to write
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001354 * them with writepage() call back
Alex Tomas64769242008-07-11 19:27:31 -04001355 *
1356 * @mpd->inode: inode
1357 * @mpd->first_page: first page of the extent
1358 * @mpd->next_page: page after the last page of the extent
Alex Tomas64769242008-07-11 19:27:31 -04001359 *
1360 * By the time mpage_da_submit_io() is called we expect all blocks
1361 * to be allocated. this may be wrong if allocation failed.
1362 *
1363 * As pages are already locked by write_cache_pages(), we can't use it
1364 */
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04001365static int mpage_da_submit_io(struct mpage_da_data *mpd,
1366 struct ext4_map_blocks *map)
Alex Tomas64769242008-07-11 19:27:31 -04001367{
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001368 struct pagevec pvec;
1369 unsigned long index, end;
1370 int ret = 0, err, nr_pages, i;
1371 struct inode *inode = mpd->inode;
1372 struct address_space *mapping = inode->i_mapping;
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001373 loff_t size = i_size_read(inode);
Theodore Ts'o3ecdb3a2010-10-27 21:30:10 -04001374 unsigned int len, block_start;
1375 struct buffer_head *bh, *page_bufs = NULL;
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04001376 sector_t pblock = 0, cur_logical = 0;
Theodore Ts'obd2d0212010-10-27 21:30:10 -04001377 struct ext4_io_submit io_submit;
Alex Tomas64769242008-07-11 19:27:31 -04001378
1379 BUG_ON(mpd->next_page <= mpd->first_page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -04001380 memset(&io_submit, 0, sizeof(io_submit));
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001381 /*
1382 * We need to start from the first_page to the next_page - 1
1383 * to make sure we also write the mapped dirty buffer_heads.
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001384 * If we look at mpd->b_blocknr we would only be looking
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001385 * at the currently mapped buffer_heads.
1386 */
Alex Tomas64769242008-07-11 19:27:31 -04001387 index = mpd->first_page;
1388 end = mpd->next_page - 1;
1389
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001390 pagevec_init(&pvec, 0);
Alex Tomas64769242008-07-11 19:27:31 -04001391 while (index <= end) {
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001392 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
Alex Tomas64769242008-07-11 19:27:31 -04001393 if (nr_pages == 0)
1394 break;
1395 for (i = 0; i < nr_pages; i++) {
Jan Karaf8bec372013-01-28 12:55:08 -05001396 int skip_page = 0;
Alex Tomas64769242008-07-11 19:27:31 -04001397 struct page *page = pvec.pages[i];
1398
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001399 index = page->index;
1400 if (index > end)
1401 break;
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001402
1403 if (index == size >> PAGE_CACHE_SHIFT)
1404 len = size & ~PAGE_CACHE_MASK;
1405 else
1406 len = PAGE_CACHE_SIZE;
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04001407 if (map) {
1408 cur_logical = index << (PAGE_CACHE_SHIFT -
1409 inode->i_blkbits);
1410 pblock = map->m_pblk + (cur_logical -
1411 map->m_lblk);
1412 }
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001413 index++;
1414
1415 BUG_ON(!PageLocked(page));
1416 BUG_ON(PageWriteback(page));
1417
Theodore Ts'o3ecdb3a2010-10-27 21:30:10 -04001418 bh = page_bufs = page_buffers(page);
1419 block_start = 0;
1420 do {
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04001421 if (map && (cur_logical >= map->m_lblk) &&
1422 (cur_logical <= (map->m_lblk +
1423 (map->m_len - 1)))) {
1424 if (buffer_delay(bh)) {
1425 clear_buffer_delay(bh);
1426 bh->b_blocknr = pblock;
1427 }
1428 if (buffer_unwritten(bh) ||
1429 buffer_mapped(bh))
1430 BUG_ON(bh->b_blocknr != pblock);
1431 if (map->m_flags & EXT4_MAP_UNINIT)
1432 set_buffer_uninit(bh);
1433 clear_buffer_unwritten(bh);
1434 }
1435
Yongqiang Yang13a79a42011-12-13 21:51:55 -05001436 /*
1437 * skip page if block allocation undone and
1438 * block is dirty
1439 */
1440 if (ext4_bh_delay_or_unwritten(NULL, bh))
Theodore Ts'o97498952011-02-26 14:08:01 -05001441 skip_page = 1;
Theodore Ts'o3ecdb3a2010-10-27 21:30:10 -04001442 bh = bh->b_this_page;
1443 block_start += bh->b_size;
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04001444 cur_logical++;
1445 pblock++;
1446 } while (bh != page_bufs);
1447
Jan Karaf8bec372013-01-28 12:55:08 -05001448 if (skip_page) {
1449 unlock_page(page);
1450 continue;
1451 }
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001452
Theodore Ts'o97498952011-02-26 14:08:01 -05001453 clear_page_dirty_for_io(page);
Jan Karafe089c72013-01-28 09:38:49 -05001454 err = ext4_bio_write_page(&io_submit, page, len,
1455 mpd->wbc);
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001456 if (!err)
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001457 mpd->pages_written++;
Alex Tomas64769242008-07-11 19:27:31 -04001458 /*
1459 * In error case, we have to continue because
1460 * remaining pages are still locked
Alex Tomas64769242008-07-11 19:27:31 -04001461 */
1462 if (ret == 0)
1463 ret = err;
1464 }
1465 pagevec_release(&pvec);
1466 }
Theodore Ts'obd2d0212010-10-27 21:30:10 -04001467 ext4_io_submit(&io_submit);
Alex Tomas64769242008-07-11 19:27:31 -04001468 return ret;
1469}
1470
Curt Wohlgemuthc7f59382011-02-26 12:27:52 -05001471static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001472{
1473 int nr_pages, i;
1474 pgoff_t index, end;
1475 struct pagevec pvec;
1476 struct inode *inode = mpd->inode;
1477 struct address_space *mapping = inode->i_mapping;
Zheng Liu51865fd2012-11-08 21:57:32 -05001478 ext4_lblk_t start, last;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001479
Curt Wohlgemuthc7f59382011-02-26 12:27:52 -05001480 index = mpd->first_page;
1481 end = mpd->next_page - 1;
Zheng Liu51865fd2012-11-08 21:57:32 -05001482
1483 start = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1484 last = end << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1485 ext4_es_remove_extent(inode, start, last - start + 1);
1486
Eric Sandeen66bea922012-11-14 22:22:05 -05001487 pagevec_init(&pvec, 0);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001488 while (index <= end) {
1489 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1490 if (nr_pages == 0)
1491 break;
1492 for (i = 0; i < nr_pages; i++) {
1493 struct page *page = pvec.pages[i];
Jan Kara9b1d09982010-03-03 16:19:32 -05001494 if (page->index > end)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001495 break;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001496 BUG_ON(!PageLocked(page));
1497 BUG_ON(PageWriteback(page));
1498 block_invalidatepage(page, 0);
1499 ClearPageUptodate(page);
1500 unlock_page(page);
1501 }
Jan Kara9b1d09982010-03-03 16:19:32 -05001502 index = pvec.pages[nr_pages - 1]->index + 1;
1503 pagevec_release(&pvec);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001504 }
1505 return;
1506}
1507
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001508static void ext4_print_free_blocks(struct inode *inode)
1509{
1510 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o92b97812012-03-19 23:41:49 -04001511 struct super_block *sb = inode->i_sb;
1512
1513 ext4_msg(sb, KERN_CRIT, "Total free blocks count %lld",
Theodore Ts'o5dee5432011-09-09 19:10:51 -04001514 EXT4_C2B(EXT4_SB(inode->i_sb),
1515 ext4_count_free_clusters(inode->i_sb)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001516 ext4_msg(sb, KERN_CRIT, "Free/Dirty block details");
1517 ext4_msg(sb, KERN_CRIT, "free_blocks=%lld",
Theodore Ts'o57042652011-09-09 18:56:51 -04001518 (long long) EXT4_C2B(EXT4_SB(inode->i_sb),
1519 percpu_counter_sum(&sbi->s_freeclusters_counter)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001520 ext4_msg(sb, KERN_CRIT, "dirty_blocks=%lld",
Aditya Kali7b415bf2011-09-09 19:04:51 -04001521 (long long) EXT4_C2B(EXT4_SB(inode->i_sb),
1522 percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
Theodore Ts'o92b97812012-03-19 23:41:49 -04001523 ext4_msg(sb, KERN_CRIT, "Block reservation details");
1524 ext4_msg(sb, KERN_CRIT, "i_reserved_data_blocks=%u",
1525 EXT4_I(inode)->i_reserved_data_blocks);
1526 ext4_msg(sb, KERN_CRIT, "i_reserved_meta_blocks=%u",
Theodore Ts'o16939182009-09-26 17:43:59 -04001527 EXT4_I(inode)->i_reserved_meta_blocks);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001528 return;
1529}
1530
Theodore Ts'ob920c752009-05-14 00:54:29 -04001531/*
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001532 * mpage_da_map_and_submit - go through given space, map them
1533 * if necessary, and then submit them for I/O
Alex Tomas64769242008-07-11 19:27:31 -04001534 *
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001535 * @mpd - bh describing space
Alex Tomas64769242008-07-11 19:27:31 -04001536 *
1537 * The function skips space we know is already mapped to disk blocks.
1538 *
Alex Tomas64769242008-07-11 19:27:31 -04001539 */
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001540static void mpage_da_map_and_submit(struct mpage_da_data *mpd)
Alex Tomas64769242008-07-11 19:27:31 -04001541{
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001542 int err, blks, get_blocks_flags;
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04001543 struct ext4_map_blocks map, *mapp = NULL;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04001544 sector_t next = mpd->b_blocknr;
1545 unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits;
1546 loff_t disksize = EXT4_I(mpd->inode)->i_disksize;
1547 handle_t *handle = NULL;
Alex Tomas64769242008-07-11 19:27:31 -04001548
1549 /*
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001550 * If the blocks are mapped already, or we couldn't accumulate
1551 * any blocks, then proceed immediately to the submission stage.
Alex Tomas64769242008-07-11 19:27:31 -04001552 */
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001553 if ((mpd->b_size == 0) ||
1554 ((mpd->b_state & (1 << BH_Mapped)) &&
1555 !(mpd->b_state & (1 << BH_Delay)) &&
1556 !(mpd->b_state & (1 << BH_Unwritten))))
1557 goto submit_io;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04001558
1559 handle = ext4_journal_current_handle();
1560 BUG_ON(!handle);
1561
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04001562 /*
Eric Sandeen79e83032010-07-27 11:56:07 -04001563 * Call ext4_map_blocks() to allocate any delayed allocation
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001564 * blocks, or to convert an uninitialized extent to be
1565 * initialized (in the case where we have written into
1566 * one or more preallocated blocks).
1567 *
1568 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
1569 * indicate that we are on the delayed allocation path. This
1570 * affects functions in many different parts of the allocation
1571 * call path. This flag exists primarily because we don't
Eric Sandeen79e83032010-07-27 11:56:07 -04001572 * want to change *many* call functions, so ext4_map_blocks()
Theodore Ts'of2321092011-01-10 12:12:36 -05001573 * will set the EXT4_STATE_DELALLOC_RESERVED flag once the
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001574 * inode's allocation semaphore is taken.
1575 *
1576 * If the blocks in questions were delalloc blocks, set
1577 * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
1578 * variables are updated after the blocks have been allocated.
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04001579 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001580 map.m_lblk = next;
1581 map.m_len = max_blocks;
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05001582 get_blocks_flags = EXT4_GET_BLOCKS_CREATE;
Jiaying Zhang744692d2010-03-04 16:14:02 -05001583 if (ext4_should_dioread_nolock(mpd->inode))
1584 get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001585 if (mpd->b_state & (1 << BH_Delay))
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05001586 get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
1587
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001588 blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags);
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04001589 if (blks < 0) {
Eric Sandeene3570632010-07-27 11:56:08 -04001590 struct super_block *sb = mpd->inode->i_sb;
1591
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04001592 err = blks;
Theodore Ts'oed5bde02009-02-23 10:48:07 -05001593 /*
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001594 * If get block returns EAGAIN or ENOSPC and there
Theodore Ts'o97498952011-02-26 14:08:01 -05001595 * appears to be free blocks we will just let
1596 * mpage_da_submit_io() unlock all of the pages.
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001597 */
1598 if (err == -EAGAIN)
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001599 goto submit_io;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001600
Theodore Ts'o5dee5432011-09-09 19:10:51 -04001601 if (err == -ENOSPC && ext4_count_free_clusters(sb)) {
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001602 mpd->retval = err;
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001603 goto submit_io;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001604 }
1605
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001606 /*
Theodore Ts'oed5bde02009-02-23 10:48:07 -05001607 * get block failure will cause us to loop in
1608 * writepages, because a_ops->writepage won't be able
1609 * to make progress. The page will be redirtied by
1610 * writepage and writepages will again try to write
1611 * the same.
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001612 */
Eric Sandeene3570632010-07-27 11:56:08 -04001613 if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) {
1614 ext4_msg(sb, KERN_CRIT,
1615 "delayed block allocation failed for inode %lu "
1616 "at logical offset %llu with max blocks %zd "
1617 "with error %d", mpd->inode->i_ino,
1618 (unsigned long long) next,
1619 mpd->b_size >> mpd->inode->i_blkbits, err);
1620 ext4_msg(sb, KERN_CRIT,
Theodore Ts'o01a523e2013-02-14 15:51:58 -05001621 "This should not happen!! Data will be lost");
Eric Sandeene3570632010-07-27 11:56:08 -04001622 if (err == -ENOSPC)
1623 ext4_print_free_blocks(mpd->inode);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001624 }
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04001625 /* invalidate all the pages */
Curt Wohlgemuthc7f59382011-02-26 12:27:52 -05001626 ext4_da_block_invalidatepages(mpd);
Curt Wohlgemuthe0fd9b92011-02-26 12:25:52 -05001627
1628 /* Mark this page range as having been completed */
1629 mpd->io_done = 1;
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001630 return;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001631 }
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04001632 BUG_ON(blks == 0);
1633
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04001634 mapp = &map;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001635 if (map.m_flags & EXT4_MAP_NEW) {
1636 struct block_device *bdev = mpd->inode->i_sb->s_bdev;
1637 int i;
Alex Tomas64769242008-07-11 19:27:31 -04001638
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001639 for (i = 0; i < map.m_len; i++)
1640 unmap_underlying_metadata(bdev, map.m_pblk + i);
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04001641 }
1642
1643 /*
Jan Kara03f5d8b2009-06-09 00:17:05 -04001644 * Update on-disk size along with block allocation.
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04001645 */
1646 disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits;
1647 if (disksize > i_size_read(mpd->inode))
1648 disksize = i_size_read(mpd->inode);
1649 if (disksize > EXT4_I(mpd->inode)->i_disksize) {
1650 ext4_update_i_disksize(mpd->inode, disksize);
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001651 err = ext4_mark_inode_dirty(handle, mpd->inode);
1652 if (err)
1653 ext4_error(mpd->inode->i_sb,
1654 "Failed to mark inode %lu dirty",
1655 mpd->inode->i_ino);
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04001656 }
1657
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001658submit_io:
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04001659 mpage_da_submit_io(mpd, mapp);
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001660 mpd->io_done = 1;
Alex Tomas64769242008-07-11 19:27:31 -04001661}
1662
Aneesh Kumar K.Vbf068ee2008-08-19 22:16:43 -04001663#define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
1664 (1 << BH_Delay) | (1 << BH_Unwritten))
Alex Tomas64769242008-07-11 19:27:31 -04001665
1666/*
1667 * mpage_add_bh_to_extent - try to add one more block to extent of blocks
1668 *
1669 * @mpd->lbh - extent of blocks
1670 * @logical - logical number of the block in the file
Jan Karab6a8e622013-01-28 13:06:48 -05001671 * @b_state - b_state of the buffer head added
Alex Tomas64769242008-07-11 19:27:31 -04001672 *
1673 * the function is used to collect contig. blocks in same state
1674 */
Jan Karab6a8e622013-01-28 13:06:48 -05001675static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, sector_t logical,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001676 unsigned long b_state)
Alex Tomas64769242008-07-11 19:27:31 -04001677{
Alex Tomas64769242008-07-11 19:27:31 -04001678 sector_t next;
Jan Karab6a8e622013-01-28 13:06:48 -05001679 int blkbits = mpd->inode->i_blkbits;
1680 int nrblocks = mpd->b_size >> blkbits;
Alex Tomas64769242008-07-11 19:27:31 -04001681
Eric Sandeenc445e3e2010-05-16 04:00:00 -04001682 /*
1683 * XXX Don't go larger than mballoc is willing to allocate
1684 * This is a stopgap solution. We eventually need to fold
1685 * mpage_da_submit_io() into this function and then call
Eric Sandeen79e83032010-07-27 11:56:07 -04001686 * ext4_map_blocks() multiple times in a loop
Eric Sandeenc445e3e2010-05-16 04:00:00 -04001687 */
Jan Karab6a8e622013-01-28 13:06:48 -05001688 if (nrblocks >= (8*1024*1024 >> blkbits))
Eric Sandeenc445e3e2010-05-16 04:00:00 -04001689 goto flush_it;
1690
Jan Karab6a8e622013-01-28 13:06:48 -05001691 /* check if the reserved journal credits might overflow */
1692 if (!ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS)) {
Mingming Cao525f4ed2008-08-19 22:15:58 -04001693 if (nrblocks >= EXT4_MAX_TRANS_DATA) {
1694 /*
1695 * With non-extent format we are limited by the journal
1696 * credit available. Total credit needed to insert
1697 * nrblocks contiguous blocks is dependent on the
1698 * nrblocks. So limit nrblocks.
1699 */
1700 goto flush_it;
Mingming Cao525f4ed2008-08-19 22:15:58 -04001701 }
1702 }
Alex Tomas64769242008-07-11 19:27:31 -04001703 /*
1704 * First block in the extent
1705 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001706 if (mpd->b_size == 0) {
1707 mpd->b_blocknr = logical;
Jan Karab6a8e622013-01-28 13:06:48 -05001708 mpd->b_size = 1 << blkbits;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001709 mpd->b_state = b_state & BH_FLAGS;
Alex Tomas64769242008-07-11 19:27:31 -04001710 return;
1711 }
1712
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001713 next = mpd->b_blocknr + nrblocks;
Alex Tomas64769242008-07-11 19:27:31 -04001714 /*
1715 * Can we merge the block to our big extent?
1716 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001717 if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) {
Jan Karab6a8e622013-01-28 13:06:48 -05001718 mpd->b_size += 1 << blkbits;
Alex Tomas64769242008-07-11 19:27:31 -04001719 return;
1720 }
1721
Mingming Cao525f4ed2008-08-19 22:15:58 -04001722flush_it:
Alex Tomas64769242008-07-11 19:27:31 -04001723 /*
1724 * We couldn't merge the block to our extent, so we
1725 * need to flush current extent and start new one
1726 */
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001727 mpage_da_map_and_submit(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001728 return;
Alex Tomas64769242008-07-11 19:27:31 -04001729}
1730
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001731static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001732{
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001733 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001734}
1735
Alex Tomas64769242008-07-11 19:27:31 -04001736/*
Aditya Kali5356f262011-09-09 19:20:51 -04001737 * This function is grabs code from the very beginning of
1738 * ext4_map_blocks, but assumes that the caller is from delayed write
1739 * time. This function looks up the requested blocks and sets the
1740 * buffer delay bit under the protection of i_data_sem.
1741 */
1742static int ext4_da_map_blocks(struct inode *inode, sector_t iblock,
1743 struct ext4_map_blocks *map,
1744 struct buffer_head *bh)
1745{
1746 int retval;
1747 sector_t invalid_block = ~((sector_t) 0xffff);
1748
1749 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
1750 invalid_block = ~0;
1751
1752 map->m_flags = 0;
1753 ext_debug("ext4_da_map_blocks(): inode %lu, max_blocks %u,"
1754 "logical block %lu\n", inode->i_ino, map->m_len,
1755 (unsigned long) map->m_lblk);
1756 /*
1757 * Try to see if we can get the block without requesting a new
1758 * file system block.
1759 */
1760 down_read((&EXT4_I(inode)->i_data_sem));
Tao Ma9c3569b2012-12-10 14:05:57 -05001761 if (ext4_has_inline_data(inode)) {
1762 /*
1763 * We will soon create blocks for this page, and let
1764 * us pretend as if the blocks aren't allocated yet.
1765 * In case of clusters, we have to handle the work
1766 * of mapping from cluster so that the reserved space
1767 * is calculated properly.
1768 */
1769 if ((EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) &&
1770 ext4_find_delalloc_cluster(inode, map->m_lblk))
1771 map->m_flags |= EXT4_MAP_FROM_CLUSTER;
1772 retval = 0;
1773 } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Aditya Kali5356f262011-09-09 19:20:51 -04001774 retval = ext4_ext_map_blocks(NULL, inode, map, 0);
1775 else
1776 retval = ext4_ind_map_blocks(NULL, inode, map, 0);
1777
1778 if (retval == 0) {
Zheng Liuf7fec032013-02-18 00:28:47 -05001779 int ret;
Aditya Kali5356f262011-09-09 19:20:51 -04001780 /*
1781 * XXX: __block_prepare_write() unmaps passed block,
1782 * is it OK?
1783 */
1784 /* If the block was allocated from previously allocated cluster,
1785 * then we dont need to reserve it again. */
1786 if (!(map->m_flags & EXT4_MAP_FROM_CLUSTER)) {
Zheng Liuf7fec032013-02-18 00:28:47 -05001787 ret = ext4_da_reserve_space(inode, iblock);
1788 if (ret) {
Aditya Kali5356f262011-09-09 19:20:51 -04001789 /* not enough space to reserve */
Zheng Liuf7fec032013-02-18 00:28:47 -05001790 retval = ret;
Aditya Kali5356f262011-09-09 19:20:51 -04001791 goto out_unlock;
Zheng Liuf7fec032013-02-18 00:28:47 -05001792 }
Aditya Kali5356f262011-09-09 19:20:51 -04001793 }
1794
Zheng Liuf7fec032013-02-18 00:28:47 -05001795 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
1796 ~0, EXTENT_STATUS_DELAYED);
1797 if (ret) {
1798 retval = ret;
Zheng Liu51865fd2012-11-08 21:57:32 -05001799 goto out_unlock;
Zheng Liuf7fec032013-02-18 00:28:47 -05001800 }
Zheng Liu51865fd2012-11-08 21:57:32 -05001801
Aditya Kali5356f262011-09-09 19:20:51 -04001802 /* Clear EXT4_MAP_FROM_CLUSTER flag since its purpose is served
1803 * and it should not appear on the bh->b_state.
1804 */
1805 map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
1806
1807 map_bh(bh, inode->i_sb, invalid_block);
1808 set_buffer_new(bh);
1809 set_buffer_delay(bh);
Zheng Liuf7fec032013-02-18 00:28:47 -05001810 } else if (retval > 0) {
1811 int ret;
1812 unsigned long long status;
1813
1814 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
1815 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
1816 ret = ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
1817 map->m_pblk, status);
1818 if (ret != 0)
1819 retval = ret;
Aditya Kali5356f262011-09-09 19:20:51 -04001820 }
1821
1822out_unlock:
1823 up_read((&EXT4_I(inode)->i_data_sem));
1824
1825 return retval;
1826}
1827
1828/*
Theodore Ts'ob920c752009-05-14 00:54:29 -04001829 * This is a special get_blocks_t callback which is used by
1830 * ext4_da_write_begin(). It will either return mapped block or
1831 * reserve space for a single block.
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001832 *
1833 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
1834 * We also have b_blocknr = -1 and b_bdev initialized properly
1835 *
1836 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
1837 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
1838 * initialized properly.
Alex Tomas64769242008-07-11 19:27:31 -04001839 */
Tao Ma9c3569b2012-12-10 14:05:57 -05001840int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
1841 struct buffer_head *bh, int create)
Alex Tomas64769242008-07-11 19:27:31 -04001842{
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001843 struct ext4_map_blocks map;
Alex Tomas64769242008-07-11 19:27:31 -04001844 int ret = 0;
1845
1846 BUG_ON(create == 0);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001847 BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
1848
1849 map.m_lblk = iblock;
1850 map.m_len = 1;
Alex Tomas64769242008-07-11 19:27:31 -04001851
1852 /*
1853 * first, we need to know whether the block is allocated already
1854 * preallocated blocks are unmapped but should treated
1855 * the same as allocated blocks.
1856 */
Aditya Kali5356f262011-09-09 19:20:51 -04001857 ret = ext4_da_map_blocks(inode, iblock, &map, bh);
1858 if (ret <= 0)
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001859 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04001860
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001861 map_bh(bh, inode->i_sb, map.m_pblk);
1862 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
1863
1864 if (buffer_unwritten(bh)) {
1865 /* A delayed write to unwritten bh should be marked
1866 * new and mapped. Mapped ensures that we don't do
1867 * get_block multiple times when we write to the same
1868 * offset and new ensures that we do proper zero out
1869 * for partial write.
1870 */
1871 set_buffer_new(bh);
Theodore Ts'oc8205632011-04-10 22:30:07 -04001872 set_buffer_mapped(bh);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001873 }
1874 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04001875}
Mingming Cao61628a32008-07-11 19:27:31 -04001876
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001877static int bget_one(handle_t *handle, struct buffer_head *bh)
1878{
1879 get_bh(bh);
1880 return 0;
1881}
1882
1883static int bput_one(handle_t *handle, struct buffer_head *bh)
1884{
1885 put_bh(bh);
1886 return 0;
1887}
1888
1889static int __ext4_journalled_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001890 unsigned int len)
1891{
1892 struct address_space *mapping = page->mapping;
1893 struct inode *inode = mapping->host;
Tao Ma3fdcfb62012-12-10 14:05:57 -05001894 struct buffer_head *page_bufs = NULL;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001895 handle_t *handle = NULL;
Tao Ma3fdcfb62012-12-10 14:05:57 -05001896 int ret = 0, err = 0;
1897 int inline_data = ext4_has_inline_data(inode);
1898 struct buffer_head *inode_bh = NULL;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001899
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001900 ClearPageChecked(page);
Tao Ma3fdcfb62012-12-10 14:05:57 -05001901
1902 if (inline_data) {
1903 BUG_ON(page->index != 0);
1904 BUG_ON(len > ext4_get_max_inline_size(inode));
1905 inode_bh = ext4_journalled_write_inline_data(inode, len, page);
1906 if (inode_bh == NULL)
1907 goto out;
1908 } else {
1909 page_bufs = page_buffers(page);
1910 if (!page_bufs) {
1911 BUG();
1912 goto out;
1913 }
1914 ext4_walk_page_buffers(handle, page_bufs, 0, len,
1915 NULL, bget_one);
1916 }
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001917 /* As soon as we unlock the page, it can go away, but we have
1918 * references to buffers so we are safe */
1919 unlock_page(page);
1920
Theodore Ts'o9924a922013-02-08 21:59:22 -05001921 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
1922 ext4_writepage_trans_blocks(inode));
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001923 if (IS_ERR(handle)) {
1924 ret = PTR_ERR(handle);
1925 goto out;
1926 }
1927
Curt Wohlgemuth441c8502011-08-13 11:25:18 -04001928 BUG_ON(!ext4_handle_valid(handle));
1929
Tao Ma3fdcfb62012-12-10 14:05:57 -05001930 if (inline_data) {
1931 ret = ext4_journal_get_write_access(handle, inode_bh);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001932
Tao Ma3fdcfb62012-12-10 14:05:57 -05001933 err = ext4_handle_dirty_metadata(handle, inode, inode_bh);
1934
1935 } else {
1936 ret = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
1937 do_journal_get_write_access);
1938
1939 err = ext4_walk_page_buffers(handle, page_bufs, 0, len, NULL,
1940 write_end_fn);
1941 }
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001942 if (ret == 0)
1943 ret = err;
Jan Kara2d859db2011-07-26 09:07:11 -04001944 EXT4_I(inode)->i_datasync_tid = handle->h_transaction->t_tid;
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001945 err = ext4_journal_stop(handle);
1946 if (!ret)
1947 ret = err;
1948
Tao Ma3fdcfb62012-12-10 14:05:57 -05001949 if (!ext4_has_inline_data(inode))
1950 ext4_walk_page_buffers(handle, page_bufs, 0, len,
1951 NULL, bput_one);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001952 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001953out:
Tao Ma3fdcfb62012-12-10 14:05:57 -05001954 brelse(inode_bh);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001955 return ret;
1956}
1957
Mingming Cao61628a32008-07-11 19:27:31 -04001958/*
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001959 * Note that we don't need to start a transaction unless we're journaling data
1960 * because we should have holes filled from ext4_page_mkwrite(). We even don't
1961 * need to file the inode to the transaction's list in ordered mode because if
1962 * we are writing back data added by write(), the inode is already there and if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001963 * we are writing back data modified via mmap(), no one guarantees in which
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001964 * transaction the data will hit the disk. In case we are journaling data, we
1965 * cannot start transaction directly because transaction start ranks above page
1966 * lock so we have to do some magic.
1967 *
Theodore Ts'ob920c752009-05-14 00:54:29 -04001968 * This function can get called via...
1969 * - ext4_da_writepages after taking page lock (have journal handle)
1970 * - journal_submit_inode_data_buffers (no journal handle)
Artem Bityutskiyf6463b02012-07-25 18:12:04 +03001971 * - shrink_page_list via the kswapd/direct reclaim (no journal handle)
Theodore Ts'ob920c752009-05-14 00:54:29 -04001972 * - grab_page_cache when doing write_begin (have journal handle)
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001973 *
1974 * We don't do any block allocation in this function. If we have page with
1975 * multiple blocks we need to write those buffer_heads that are mapped. This
1976 * is important for mmaped based write. So if we do with blocksize 1K
1977 * truncate(f, 1024);
1978 * a = mmap(f, 0, 4096);
1979 * a[0] = 'a';
1980 * truncate(f, 4096);
1981 * we have in the page first buffer_head mapped via page_mkwrite call back
Paul Bolle90802ed2011-12-05 13:00:34 +01001982 * but other buffer_heads would be unmapped but dirty (dirty done via the
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001983 * do_wp_page). So writepage should write the first block. If we modify
1984 * the mmap area beyond 1024 we will again get a page_fault and the
1985 * page_mkwrite callback will do the block allocation and mark the
1986 * buffer_heads mapped.
1987 *
1988 * We redirty the page if we have any buffer_heads that is either delay or
1989 * unwritten in the page.
1990 *
1991 * We can get recursively called as show below.
1992 *
1993 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
1994 * ext4_writepage()
1995 *
1996 * But since we don't do any block allocation we should not deadlock.
1997 * Page also have the dirty flag cleared so we don't get recurive page_lock.
Mingming Cao61628a32008-07-11 19:27:31 -04001998 */
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001999static int ext4_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04002000 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002001{
Jan Karaf8bec372013-01-28 12:55:08 -05002002 int ret = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002003 loff_t size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002004 unsigned int len;
Jiaying Zhang744692d2010-03-04 16:14:02 -05002005 struct buffer_head *page_bufs = NULL;
Mingming Cao61628a32008-07-11 19:27:31 -04002006 struct inode *inode = page->mapping->host;
Jan Kara36ade452013-01-28 09:30:52 -05002007 struct ext4_io_submit io_submit;
Alex Tomas64769242008-07-11 19:27:31 -04002008
Lukas Czernera9c667f2011-06-06 09:51:52 -04002009 trace_ext4_writepage(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04002010 size = i_size_read(inode);
2011 if (page->index == size >> PAGE_CACHE_SHIFT)
2012 len = size & ~PAGE_CACHE_MASK;
2013 else
2014 len = PAGE_CACHE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04002015
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002016 page_bufs = page_buffers(page);
Jan Karafe386132013-01-28 21:06:42 -05002017 /*
2018 * We cannot do block allocation or other extent handling in this
2019 * function. If there are buffers needing that, we have to redirty
2020 * the page. But we may reach here when we do a journal commit via
2021 * journal_submit_inode_data_buffers() and in that case we must write
2022 * allocated buffers to achieve data=ordered mode guarantees.
2023 */
Tao Maf19d5872012-12-10 14:05:51 -05002024 if (ext4_walk_page_buffers(NULL, page_bufs, 0, len, NULL,
2025 ext4_bh_delay_or_unwritten)) {
Jan Karaf8bec372013-01-28 12:55:08 -05002026 redirty_page_for_writepage(wbc, page);
Jan Karafe386132013-01-28 21:06:42 -05002027 if (current->flags & PF_MEMALLOC) {
2028 /*
2029 * For memory cleaning there's no point in writing only
2030 * some buffers. So just bail out. Warn if we came here
2031 * from direct reclaim.
2032 */
2033 WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD))
2034 == PF_MEMALLOC);
2035 unlock_page(page);
2036 return 0;
2037 }
Theodore Ts'oa42afc52010-10-27 21:30:09 -04002038 }
Alex Tomas64769242008-07-11 19:27:31 -04002039
Theodore Ts'ocb20d512010-10-27 21:30:09 -04002040 if (PageChecked(page) && ext4_should_journal_data(inode))
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002041 /*
2042 * It's mmapped pagecache. Add buffers and journal it. There
2043 * doesn't seem much point in redirtying the page here.
2044 */
Wu Fengguang3f0ca302009-11-24 11:15:44 -05002045 return __ext4_journalled_writepage(page, len);
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002046
Jan Kara36ade452013-01-28 09:30:52 -05002047 memset(&io_submit, 0, sizeof(io_submit));
2048 ret = ext4_bio_write_page(&io_submit, page, len, wbc);
2049 ext4_io_submit(&io_submit);
Alex Tomas64769242008-07-11 19:27:31 -04002050 return ret;
2051}
2052
Mingming Cao61628a32008-07-11 19:27:31 -04002053/*
Mingming Cao525f4ed2008-08-19 22:15:58 -04002054 * This is called via ext4_da_writepages() to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002055 * calculate the total number of credits to reserve to fit
Mingming Cao525f4ed2008-08-19 22:15:58 -04002056 * a single extent allocation into a single transaction,
2057 * ext4_da_writpeages() will loop calling this before
2058 * the block allocation.
Mingming Cao61628a32008-07-11 19:27:31 -04002059 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04002060
2061static int ext4_da_writepages_trans_blocks(struct inode *inode)
2062{
2063 int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
2064
2065 /*
2066 * With non-extent format the journal credit needed to
2067 * insert nrblocks contiguous block is dependent on
2068 * number of contiguous block. So we will limit
2069 * number of contiguous block to a sane value
2070 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002071 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) &&
Mingming Cao525f4ed2008-08-19 22:15:58 -04002072 (max_blocks > EXT4_MAX_TRANS_DATA))
2073 max_blocks = EXT4_MAX_TRANS_DATA;
2074
2075 return ext4_chunk_trans_blocks(inode, max_blocks);
2076}
Mingming Cao61628a32008-07-11 19:27:31 -04002077
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002078/*
2079 * write_cache_pages_da - walk the list of dirty pages of the given
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002080 * address space and accumulate pages that need writing, and call
Theodore Ts'o168fc022011-02-26 14:09:20 -05002081 * mpage_da_map_and_submit to map a single contiguous memory region
2082 * and then write them.
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002083 */
Tao Ma9c3569b2012-12-10 14:05:57 -05002084static int write_cache_pages_da(handle_t *handle,
2085 struct address_space *mapping,
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002086 struct writeback_control *wbc,
Eric Sandeen72f84e62010-10-27 21:30:13 -04002087 struct mpage_da_data *mpd,
2088 pgoff_t *done_index)
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002089{
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002090 struct buffer_head *bh, *head;
Theodore Ts'o168fc022011-02-26 14:09:20 -05002091 struct inode *inode = mapping->host;
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002092 struct pagevec pvec;
2093 unsigned int nr_pages;
2094 sector_t logical;
2095 pgoff_t index, end;
2096 long nr_to_write = wbc->nr_to_write;
2097 int i, tag, ret = 0;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002098
Theodore Ts'o168fc022011-02-26 14:09:20 -05002099 memset(mpd, 0, sizeof(struct mpage_da_data));
2100 mpd->wbc = wbc;
2101 mpd->inode = inode;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002102 pagevec_init(&pvec, 0);
2103 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2104 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2105
Wu Fengguang6e6938b2010-06-06 10:38:15 -06002106 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Eric Sandeen5b41d922010-10-27 21:30:13 -04002107 tag = PAGECACHE_TAG_TOWRITE;
2108 else
2109 tag = PAGECACHE_TAG_DIRTY;
2110
Eric Sandeen72f84e62010-10-27 21:30:13 -04002111 *done_index = index;
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002112 while (index <= end) {
Eric Sandeen5b41d922010-10-27 21:30:13 -04002113 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002114 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
2115 if (nr_pages == 0)
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002116 return 0;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002117
2118 for (i = 0; i < nr_pages; i++) {
2119 struct page *page = pvec.pages[i];
2120
2121 /*
2122 * At this point, the page may be truncated or
2123 * invalidated (changing page->mapping to NULL), or
2124 * even swizzled back from swapper_space to tmpfs file
2125 * mapping. However, page->index will not change
2126 * because we have a reference on the page.
2127 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002128 if (page->index > end)
2129 goto out;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002130
Eric Sandeen72f84e62010-10-27 21:30:13 -04002131 *done_index = page->index + 1;
2132
Theodore Ts'o78aaced2011-02-26 14:09:14 -05002133 /*
2134 * If we can't merge this page, and we have
2135 * accumulated an contiguous region, write it
2136 */
2137 if ((mpd->next_page != page->index) &&
2138 (mpd->next_page != mpd->first_page)) {
2139 mpage_da_map_and_submit(mpd);
2140 goto ret_extent_tail;
2141 }
2142
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002143 lock_page(page);
2144
2145 /*
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002146 * If the page is no longer dirty, or its
2147 * mapping no longer corresponds to inode we
2148 * are writing (which means it has been
2149 * truncated or invalidated), or the page is
2150 * already under writeback and we are not
2151 * doing a data integrity writeback, skip the page
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002152 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002153 if (!PageDirty(page) ||
2154 (PageWriteback(page) &&
2155 (wbc->sync_mode == WB_SYNC_NONE)) ||
2156 unlikely(page->mapping != mapping)) {
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002157 unlock_page(page);
2158 continue;
2159 }
2160
Darrick J. Wong7cb1a532011-05-18 13:53:20 -04002161 wait_on_page_writeback(page);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002162 BUG_ON(PageWriteback(page));
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002163
Tao Ma9c3569b2012-12-10 14:05:57 -05002164 /*
2165 * If we have inline data and arrive here, it means that
2166 * we will soon create the block for the 1st page, so
2167 * we'd better clear the inline data here.
2168 */
2169 if (ext4_has_inline_data(inode)) {
2170 BUG_ON(ext4_test_inode_state(inode,
2171 EXT4_STATE_MAY_INLINE_DATA));
2172 ext4_destroy_inline_data(handle, inode);
2173 }
2174
Theodore Ts'o168fc022011-02-26 14:09:20 -05002175 if (mpd->next_page != page->index)
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002176 mpd->first_page = page->index;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002177 mpd->next_page = page->index + 1;
2178 logical = (sector_t) page->index <<
2179 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2180
Jan Karaf8bec372013-01-28 12:55:08 -05002181 /* Add all dirty buffers to mpd */
2182 head = page_buffers(page);
2183 bh = head;
2184 do {
2185 BUG_ON(buffer_locked(bh));
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002186 /*
Jan Karaf8bec372013-01-28 12:55:08 -05002187 * We need to try to allocate unmapped blocks
2188 * in the same page. Otherwise we won't make
2189 * progress with the page in ext4_writepage
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002190 */
Jan Karaf8bec372013-01-28 12:55:08 -05002191 if (ext4_bh_delay_or_unwritten(NULL, bh)) {
2192 mpage_add_bh_to_extent(mpd, logical,
Jan Karaf8bec372013-01-28 12:55:08 -05002193 bh->b_state);
2194 if (mpd->io_done)
2195 goto ret_extent_tail;
2196 } else if (buffer_dirty(bh) &&
2197 buffer_mapped(bh)) {
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002198 /*
Jan Karaf8bec372013-01-28 12:55:08 -05002199 * mapped dirty buffer. We need to
2200 * update the b_state because we look
2201 * at b_state in mpage_da_map_blocks.
2202 * We don't update b_size because if we
2203 * find an unmapped buffer_head later
2204 * we need to use the b_state flag of
2205 * that buffer_head.
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002206 */
Jan Karaf8bec372013-01-28 12:55:08 -05002207 if (mpd->b_size == 0)
2208 mpd->b_state =
2209 bh->b_state & BH_FLAGS;
2210 }
2211 logical++;
2212 } while ((bh = bh->b_this_page) != head);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002213
2214 if (nr_to_write > 0) {
2215 nr_to_write--;
2216 if (nr_to_write == 0 &&
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002217 wbc->sync_mode == WB_SYNC_NONE)
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002218 /*
2219 * We stop writing back only if we are
2220 * not doing integrity sync. In case of
2221 * integrity sync we have to keep going
2222 * because someone may be concurrently
2223 * dirtying pages, and we might have
2224 * synced a lot of newly appeared dirty
2225 * pages, but have not synced all of the
2226 * old dirty pages.
2227 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002228 goto out;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002229 }
2230 }
2231 pagevec_release(&pvec);
2232 cond_resched();
2233 }
Theodore Ts'o4f01b022011-02-26 14:07:37 -05002234 return 0;
2235ret_extent_tail:
2236 ret = MPAGE_DA_EXTENT_TAIL;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002237out:
2238 pagevec_release(&pvec);
2239 cond_resched();
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04002240 return ret;
2241}
2242
2243
Alex Tomas64769242008-07-11 19:27:31 -04002244static int ext4_da_writepages(struct address_space *mapping,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002245 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04002246{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002247 pgoff_t index;
2248 int range_whole = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04002249 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002250 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002251 struct inode *inode = mapping->host;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05002252 int pages_written = 0;
Theodore Ts'o55138e02009-09-29 13:31:31 -04002253 unsigned int max_pages;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002254 int range_cyclic, cycled = 1, io_done = 0;
Theodore Ts'o55138e02009-09-29 13:31:31 -04002255 int needed_blocks, ret = 0;
2256 long desired_nr_to_write, nr_to_writebump = 0;
Theodore Ts'ode89de62009-08-31 17:00:59 -04002257 loff_t range_start = wbc->range_start;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002258 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Eric Sandeen72f84e62010-10-27 21:30:13 -04002259 pgoff_t done_index = 0;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002260 pgoff_t end;
Shaohua Li1bce63d2011-10-18 10:55:51 -04002261 struct blk_plug plug;
Mingming Cao61628a32008-07-11 19:27:31 -04002262
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002263 trace_ext4_da_writepages(inode, wbc);
Theodore Ts'oba80b102009-01-03 20:03:21 -05002264
Mingming Cao61628a32008-07-11 19:27:31 -04002265 /*
2266 * No pages to write? This is mainly a kludge to avoid starting
2267 * a transaction for special inodes like journal inode on last iput()
2268 * because that could violate lock ordering on umount
2269 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002270 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Mingming Cao61628a32008-07-11 19:27:31 -04002271 return 0;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002272
2273 /*
2274 * If the filesystem has aborted, it is read-only, so return
2275 * right away instead of dumping stack traces later on that
2276 * will obscure the real source of the problem. We test
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002277 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002278 * the latter could be true if the filesystem is mounted
2279 * read-only, and in that case, ext4_da_writepages should
2280 * *never* be called, so if that ever happens, we would want
2281 * the stack trace.
2282 */
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002283 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002284 return -EROFS;
2285
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002286 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2287 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002288
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002289 range_cyclic = wbc->range_cyclic;
2290 if (wbc->range_cyclic) {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002291 index = mapping->writeback_index;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002292 if (index)
2293 cycled = 0;
2294 wbc->range_start = index << PAGE_CACHE_SHIFT;
2295 wbc->range_end = LLONG_MAX;
2296 wbc->range_cyclic = 0;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002297 end = -1;
2298 } else {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002299 index = wbc->range_start >> PAGE_CACHE_SHIFT;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002300 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2301 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002302
Theodore Ts'o55138e02009-09-29 13:31:31 -04002303 /*
2304 * This works around two forms of stupidity. The first is in
2305 * the writeback code, which caps the maximum number of pages
2306 * written to be 1024 pages. This is wrong on multiple
2307 * levels; different architectues have a different page size,
2308 * which changes the maximum amount of data which gets
2309 * written. Secondly, 4 megabytes is way too small. XFS
2310 * forces this value to be 16 megabytes by multiplying
2311 * nr_to_write parameter by four, and then relies on its
2312 * allocator to allocate larger extents to make them
2313 * contiguous. Unfortunately this brings us to the second
2314 * stupidity, which is that ext4's mballoc code only allocates
2315 * at most 2048 blocks. So we force contiguous writes up to
2316 * the number of dirty blocks in the inode, or
2317 * sbi->max_writeback_mb_bump whichever is smaller.
2318 */
2319 max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT);
Eric Sandeenb443e732010-10-27 21:30:03 -04002320 if (!range_cyclic && range_whole) {
2321 if (wbc->nr_to_write == LONG_MAX)
2322 desired_nr_to_write = wbc->nr_to_write;
2323 else
2324 desired_nr_to_write = wbc->nr_to_write * 8;
2325 } else
Theodore Ts'o55138e02009-09-29 13:31:31 -04002326 desired_nr_to_write = ext4_num_dirty_pages(inode, index,
2327 max_pages);
2328 if (desired_nr_to_write > max_pages)
2329 desired_nr_to_write = max_pages;
2330
2331 if (wbc->nr_to_write < desired_nr_to_write) {
2332 nr_to_writebump = desired_nr_to_write - wbc->nr_to_write;
2333 wbc->nr_to_write = desired_nr_to_write;
2334 }
2335
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002336retry:
Wu Fengguang6e6938b2010-06-06 10:38:15 -06002337 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
Eric Sandeen5b41d922010-10-27 21:30:13 -04002338 tag_pages_for_writeback(mapping, index, end);
2339
Shaohua Li1bce63d2011-10-18 10:55:51 -04002340 blk_start_plug(&plug);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002341 while (!ret && wbc->nr_to_write > 0) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002342
2343 /*
2344 * we insert one extent at a time. So we need
2345 * credit needed for single extent allocation.
2346 * journalled mode is currently not supported
2347 * by delalloc
2348 */
2349 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04002350 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002351
Mingming Cao61628a32008-07-11 19:27:31 -04002352 /* start a new transaction*/
Theodore Ts'o9924a922013-02-08 21:59:22 -05002353 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
2354 needed_blocks);
Mingming Cao61628a32008-07-11 19:27:31 -04002355 if (IS_ERR(handle)) {
2356 ret = PTR_ERR(handle);
Theodore Ts'o16939182009-09-26 17:43:59 -04002357 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
Curt Wohlgemuthfbe845d2010-05-16 13:00:00 -04002358 "%ld pages, ino %lu; err %d", __func__,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002359 wbc->nr_to_write, inode->i_ino, ret);
Namjae Jeon3c1fcb22011-11-07 11:01:13 -05002360 blk_finish_plug(&plug);
Mingming Cao61628a32008-07-11 19:27:31 -04002361 goto out_writepages;
2362 }
Theodore Ts'of63e6002009-02-23 16:42:39 -05002363
2364 /*
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002365 * Now call write_cache_pages_da() to find the next
Theodore Ts'of63e6002009-02-23 16:42:39 -05002366 * contiguous region of logical blocks that need
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002367 * blocks to be allocated by ext4 and submit them.
Theodore Ts'of63e6002009-02-23 16:42:39 -05002368 */
Tao Ma9c3569b2012-12-10 14:05:57 -05002369 ret = write_cache_pages_da(handle, mapping,
2370 wbc, &mpd, &done_index);
Theodore Ts'of63e6002009-02-23 16:42:39 -05002371 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002372 * If we have a contiguous extent of pages and we
Theodore Ts'of63e6002009-02-23 16:42:39 -05002373 * haven't done the I/O yet, map the blocks and submit
2374 * them for I/O.
2375 */
2376 if (!mpd.io_done && mpd.next_page != mpd.first_page) {
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002377 mpage_da_map_and_submit(&mpd);
Theodore Ts'of63e6002009-02-23 16:42:39 -05002378 ret = MPAGE_DA_EXTENT_TAIL;
2379 }
Theodore Ts'ob3a3ca82009-08-31 23:13:11 -04002380 trace_ext4_da_write_pages(inode, &mpd);
Theodore Ts'of63e6002009-02-23 16:42:39 -05002381 wbc->nr_to_write -= mpd.pages_written;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002382
Mingming Cao61628a32008-07-11 19:27:31 -04002383 ext4_journal_stop(handle);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002384
Eric Sandeen8f64b322009-02-26 00:57:35 -05002385 if ((mpd.retval == -ENOSPC) && sbi->s_journal) {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002386 /* commit the transaction which would
2387 * free blocks released in the transaction
2388 * and try again
2389 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002390 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002391 ret = 0;
2392 } else if (ret == MPAGE_DA_EXTENT_TAIL) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002393 /*
Kazuya Mio8de49e62011-10-20 19:23:08 -04002394 * Got one extent now try with rest of the pages.
2395 * If mpd.retval is set -EIO, journal is aborted.
2396 * So we don't need to write any more.
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002397 */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002398 pages_written += mpd.pages_written;
Kazuya Mio8de49e62011-10-20 19:23:08 -04002399 ret = mpd.retval;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002400 io_done = 1;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002401 } else if (wbc->nr_to_write)
Mingming Cao61628a32008-07-11 19:27:31 -04002402 /*
2403 * There is no more writeout needed
2404 * or we requested for a noblocking writeout
2405 * and we found the device congested
2406 */
Mingming Cao61628a32008-07-11 19:27:31 -04002407 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002408 }
Shaohua Li1bce63d2011-10-18 10:55:51 -04002409 blk_finish_plug(&plug);
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002410 if (!io_done && !cycled) {
2411 cycled = 1;
2412 index = 0;
2413 wbc->range_start = index << PAGE_CACHE_SHIFT;
2414 wbc->range_end = mapping->writeback_index - 1;
2415 goto retry;
2416 }
Mingming Cao61628a32008-07-11 19:27:31 -04002417
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002418 /* Update index */
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002419 wbc->range_cyclic = range_cyclic;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002420 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2421 /*
2422 * set the writeback_index so that range_cyclic
2423 * mode will write it back later
2424 */
Eric Sandeen72f84e62010-10-27 21:30:13 -04002425 mapping->writeback_index = done_index;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002426
Mingming Cao61628a32008-07-11 19:27:31 -04002427out_writepages:
Richard Kennedy2faf2e12009-12-25 15:46:07 -05002428 wbc->nr_to_write -= nr_to_writebump;
Theodore Ts'ode89de62009-08-31 17:00:59 -04002429 wbc->range_start = range_start;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002430 trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
Mingming Cao61628a32008-07-11 19:27:31 -04002431 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002432}
2433
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002434static int ext4_nonda_switch(struct super_block *sb)
2435{
2436 s64 free_blocks, dirty_blocks;
2437 struct ext4_sb_info *sbi = EXT4_SB(sb);
2438
2439 /*
2440 * switch to non delalloc mode if we are running low
2441 * on free block. The free block accounting via percpu
Eric Dumazet179f7eb2009-01-06 14:41:04 -08002442 * counters can get slightly wrong with percpu_counter_batch getting
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002443 * accumulated on each CPU without updating global counters
2444 * Delalloc need an accurate free block accounting. So switch
2445 * to non delalloc when we are near to error range.
2446 */
Theodore Ts'o57042652011-09-09 18:56:51 -04002447 free_blocks = EXT4_C2B(sbi,
2448 percpu_counter_read_positive(&sbi->s_freeclusters_counter));
2449 dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyclusters_counter);
Theodore Ts'o00d4e732012-09-19 22:42:36 -04002450 /*
2451 * Start pushing delalloc when 1/2 of free blocks are dirty.
2452 */
2453 if (dirty_blocks && (free_blocks < 2 * dirty_blocks) &&
2454 !writeback_in_progress(sb->s_bdi) &&
2455 down_read_trylock(&sb->s_umount)) {
2456 writeback_inodes_sb(sb, WB_REASON_FS_FREE_SPACE);
2457 up_read(&sb->s_umount);
2458 }
2459
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002460 if (2 * free_blocks < 3 * dirty_blocks ||
Theodore Ts'odf55c992011-09-09 19:16:51 -04002461 free_blocks < (dirty_blocks + EXT4_FREECLUSTERS_WATERMARK)) {
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002462 /*
Eric Sandeenc8afb442009-12-23 07:58:12 -05002463 * free block count is less than 150% of dirty blocks
2464 * or free blocks is less than watermark
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002465 */
2466 return 1;
2467 }
2468 return 0;
2469}
2470
Alex Tomas64769242008-07-11 19:27:31 -04002471static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002472 loff_t pos, unsigned len, unsigned flags,
2473 struct page **pagep, void **fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04002474{
Eric Sandeen72b8ab92010-05-16 11:00:00 -04002475 int ret, retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002476 struct page *page;
2477 pgoff_t index;
Alex Tomas64769242008-07-11 19:27:31 -04002478 struct inode *inode = mapping->host;
2479 handle_t *handle;
2480
2481 index = pos >> PAGE_CACHE_SHIFT;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002482
2483 if (ext4_nonda_switch(inode->i_sb)) {
2484 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
2485 return ext4_write_begin(file, mapping, pos,
2486 len, flags, pagep, fsdata);
2487 }
2488 *fsdata = (void *)0;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002489 trace_ext4_da_write_begin(inode, pos, len, flags);
Tao Ma9c3569b2012-12-10 14:05:57 -05002490
2491 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) {
2492 ret = ext4_da_write_inline_data_begin(mapping, inode,
2493 pos, len, flags,
2494 pagep, fsdata);
2495 if (ret < 0)
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002496 return ret;
2497 if (ret == 1)
2498 return 0;
Tao Ma9c3569b2012-12-10 14:05:57 -05002499 }
2500
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002501 /*
2502 * grab_cache_page_write_begin() can take a long time if the
2503 * system is thrashing due to memory pressure, or if the page
2504 * is being written back. So grab it first before we start
2505 * the transaction handle. This also allows us to allocate
2506 * the page (if needed) without using GFP_NOFS.
2507 */
2508retry_grab:
2509 page = grab_cache_page_write_begin(mapping, index, flags);
2510 if (!page)
2511 return -ENOMEM;
2512 unlock_page(page);
2513
Alex Tomas64769242008-07-11 19:27:31 -04002514 /*
2515 * With delayed allocation, we don't log the i_disksize update
2516 * if there is delayed block allocation. But we still need
2517 * to journalling the i_disksize update if writes to the end
2518 * of file which has an already mapped buffer.
2519 */
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002520retry_journal:
Theodore Ts'o9924a922013-02-08 21:59:22 -05002521 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 1);
Alex Tomas64769242008-07-11 19:27:31 -04002522 if (IS_ERR(handle)) {
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002523 page_cache_release(page);
2524 return PTR_ERR(handle);
Alex Tomas64769242008-07-11 19:27:31 -04002525 }
2526
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002527 lock_page(page);
2528 if (page->mapping != mapping) {
2529 /* The page got truncated from under us */
2530 unlock_page(page);
2531 page_cache_release(page);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002532 ext4_journal_stop(handle);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002533 goto retry_grab;
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002534 }
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002535 /* In case writeback began while the page was unlocked */
2536 wait_on_page_writeback(page);
Alex Tomas64769242008-07-11 19:27:31 -04002537
Christoph Hellwig6e1db882010-06-04 11:29:57 +02002538 ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
Alex Tomas64769242008-07-11 19:27:31 -04002539 if (ret < 0) {
2540 unlock_page(page);
2541 ext4_journal_stop(handle);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04002542 /*
2543 * block_write_begin may have instantiated a few blocks
2544 * outside i_size. Trim these off again. Don't need
2545 * i_size_read because we hold i_mutex.
2546 */
2547 if (pos + len > inode->i_size)
Jan Karab9a42072009-12-08 21:24:33 -05002548 ext4_truncate_failed_write(inode);
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002549
2550 if (ret == -ENOSPC &&
2551 ext4_should_retry_alloc(inode->i_sb, &retries))
2552 goto retry_journal;
2553
2554 page_cache_release(page);
2555 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002556 }
2557
Theodore Ts'o47564bf2013-02-09 09:24:14 -05002558 *pagep = page;
Alex Tomas64769242008-07-11 19:27:31 -04002559 return ret;
2560}
2561
Mingming Cao632eaea2008-07-11 19:27:31 -04002562/*
2563 * Check if we should update i_disksize
2564 * when write to the end of file but not require block allocation
2565 */
2566static int ext4_da_should_update_i_disksize(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002567 unsigned long offset)
Mingming Cao632eaea2008-07-11 19:27:31 -04002568{
2569 struct buffer_head *bh;
2570 struct inode *inode = page->mapping->host;
2571 unsigned int idx;
2572 int i;
2573
2574 bh = page_buffers(page);
2575 idx = offset >> inode->i_blkbits;
2576
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002577 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04002578 bh = bh->b_this_page;
2579
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002580 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
Mingming Cao632eaea2008-07-11 19:27:31 -04002581 return 0;
2582 return 1;
2583}
2584
Alex Tomas64769242008-07-11 19:27:31 -04002585static int ext4_da_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002586 struct address_space *mapping,
2587 loff_t pos, unsigned len, unsigned copied,
2588 struct page *page, void *fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04002589{
2590 struct inode *inode = mapping->host;
2591 int ret = 0, ret2;
2592 handle_t *handle = ext4_journal_current_handle();
2593 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04002594 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002595 int write_mode = (int)(unsigned long)fsdata;
2596
2597 if (write_mode == FALL_BACK_TO_NONDELALLOC) {
Lukas Czerner3d2b1582012-02-20 17:53:00 -05002598 switch (ext4_inode_journal_mode(inode)) {
2599 case EXT4_INODE_ORDERED_DATA_MODE:
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002600 return ext4_ordered_write_end(file, mapping, pos,
2601 len, copied, page, fsdata);
Lukas Czerner3d2b1582012-02-20 17:53:00 -05002602 case EXT4_INODE_WRITEBACK_DATA_MODE:
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002603 return ext4_writeback_write_end(file, mapping, pos,
2604 len, copied, page, fsdata);
Lukas Czerner3d2b1582012-02-20 17:53:00 -05002605 default:
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002606 BUG();
2607 }
2608 }
Mingming Cao632eaea2008-07-11 19:27:31 -04002609
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002610 trace_ext4_da_write_end(inode, pos, len, copied);
Mingming Cao632eaea2008-07-11 19:27:31 -04002611 start = pos & (PAGE_CACHE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002612 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04002613
2614 /*
2615 * generic_write_end() will run mark_inode_dirty() if i_size
2616 * changes. So let's piggyback the i_disksize mark_inode_dirty
2617 * into that.
2618 */
Alex Tomas64769242008-07-11 19:27:31 -04002619 new_i_size = pos + copied;
Andrea Arcangeliea51d132011-12-13 21:41:15 -05002620 if (copied && new_i_size > EXT4_I(inode)->i_disksize) {
Tao Ma9c3569b2012-12-10 14:05:57 -05002621 if (ext4_has_inline_data(inode) ||
2622 ext4_da_should_update_i_disksize(page, end)) {
Mingming Cao632eaea2008-07-11 19:27:31 -04002623 down_write(&EXT4_I(inode)->i_data_sem);
Theodore Ts'of3b59292012-11-15 23:08:57 -05002624 if (new_i_size > EXT4_I(inode)->i_disksize)
Mingming Cao632eaea2008-07-11 19:27:31 -04002625 EXT4_I(inode)->i_disksize = new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04002626 up_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04002627 /* We need to mark inode dirty even if
2628 * new_i_size is less that inode->i_size
2629 * bu greater than i_disksize.(hint delalloc)
2630 */
2631 ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04002632 }
Mingming Cao632eaea2008-07-11 19:27:31 -04002633 }
Tao Ma9c3569b2012-12-10 14:05:57 -05002634
2635 if (write_mode != CONVERT_INLINE_DATA &&
2636 ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) &&
2637 ext4_has_inline_data(inode))
2638 ret2 = ext4_da_write_inline_data_end(inode, pos, len, copied,
2639 page);
2640 else
2641 ret2 = generic_write_end(file, mapping, pos, len, copied,
Alex Tomas64769242008-07-11 19:27:31 -04002642 page, fsdata);
Tao Ma9c3569b2012-12-10 14:05:57 -05002643
Alex Tomas64769242008-07-11 19:27:31 -04002644 copied = ret2;
2645 if (ret2 < 0)
2646 ret = ret2;
2647 ret2 = ext4_journal_stop(handle);
2648 if (!ret)
2649 ret = ret2;
2650
2651 return ret ? ret : copied;
2652}
2653
2654static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
2655{
Alex Tomas64769242008-07-11 19:27:31 -04002656 /*
2657 * Drop reserved blocks
2658 */
2659 BUG_ON(!PageLocked(page));
2660 if (!page_has_buffers(page))
2661 goto out;
2662
Mingming Caod2a17632008-07-14 17:52:37 -04002663 ext4_da_page_release_reservation(page, offset);
Alex Tomas64769242008-07-11 19:27:31 -04002664
2665out:
2666 ext4_invalidatepage(page, offset);
2667
2668 return;
2669}
2670
Theodore Ts'occd25062009-02-26 01:04:07 -05002671/*
2672 * Force all delayed allocation blocks to be allocated for a given inode.
2673 */
2674int ext4_alloc_da_blocks(struct inode *inode)
2675{
Theodore Ts'ofb40ba02009-09-16 19:30:40 -04002676 trace_ext4_alloc_da_blocks(inode);
2677
Theodore Ts'occd25062009-02-26 01:04:07 -05002678 if (!EXT4_I(inode)->i_reserved_data_blocks &&
2679 !EXT4_I(inode)->i_reserved_meta_blocks)
2680 return 0;
2681
2682 /*
2683 * We do something simple for now. The filemap_flush() will
2684 * also start triggering a write of the data blocks, which is
2685 * not strictly speaking necessary (and for users of
2686 * laptop_mode, not even desirable). However, to do otherwise
2687 * would require replicating code paths in:
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002688 *
Theodore Ts'occd25062009-02-26 01:04:07 -05002689 * ext4_da_writepages() ->
2690 * write_cache_pages() ---> (via passed in callback function)
2691 * __mpage_da_writepage() -->
2692 * mpage_add_bh_to_extent()
2693 * mpage_da_map_blocks()
2694 *
2695 * The problem is that write_cache_pages(), located in
2696 * mm/page-writeback.c, marks pages clean in preparation for
2697 * doing I/O, which is not desirable if we're not planning on
2698 * doing I/O at all.
2699 *
2700 * We could call write_cache_pages(), and then redirty all of
Wu Fengguang380cf092010-11-11 19:23:29 +08002701 * the pages by calling redirty_page_for_writepage() but that
Theodore Ts'occd25062009-02-26 01:04:07 -05002702 * would be ugly in the extreme. So instead we would need to
2703 * replicate parts of the code in the above functions,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002704 * simplifying them because we wouldn't actually intend to
Theodore Ts'occd25062009-02-26 01:04:07 -05002705 * write out the pages, but rather only collect contiguous
2706 * logical block extents, call the multi-block allocator, and
2707 * then update the buffer heads with the block allocations.
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002708 *
Theodore Ts'occd25062009-02-26 01:04:07 -05002709 * For now, though, we'll cheat by calling filemap_flush(),
2710 * which will map the blocks, and start the I/O, but not
2711 * actually wait for the I/O to complete.
2712 */
2713 return filemap_flush(inode->i_mapping);
2714}
Alex Tomas64769242008-07-11 19:27:31 -04002715
2716/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002717 * bmap() is special. It gets used by applications such as lilo and by
2718 * the swapper to find the on-disk block of a specific piece of data.
2719 *
2720 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07002721 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002722 * filesystem and enables swap, then they may get a nasty shock when the
2723 * data getting swapped to that swapfile suddenly gets overwritten by
2724 * the original zero's written out previously to the journal and
2725 * awaiting writeback in the kernel's buffer cache.
2726 *
2727 * So, if we see any bmap calls here on a modified, data-journaled file,
2728 * take extra steps to flush any blocks which might be in the cache.
2729 */
Mingming Cao617ba132006-10-11 01:20:53 -07002730static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002731{
2732 struct inode *inode = mapping->host;
2733 journal_t *journal;
2734 int err;
2735
Tao Ma46c7f252012-12-10 14:04:52 -05002736 /*
2737 * We can get here for an inline file via the FIBMAP ioctl
2738 */
2739 if (ext4_has_inline_data(inode))
2740 return 0;
2741
Alex Tomas64769242008-07-11 19:27:31 -04002742 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
2743 test_opt(inode->i_sb, DELALLOC)) {
2744 /*
2745 * With delalloc we want to sync the file
2746 * so that we can make sure we allocate
2747 * blocks for file
2748 */
2749 filemap_write_and_wait(mapping);
2750 }
2751
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002752 if (EXT4_JOURNAL(inode) &&
2753 ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002754 /*
2755 * This is a REALLY heavyweight approach, but the use of
2756 * bmap on dirty files is expected to be extremely rare:
2757 * only if we run lilo or swapon on a freshly made file
2758 * do we expect this to happen.
2759 *
2760 * (bmap requires CAP_SYS_RAWIO so this does not
2761 * represent an unprivileged user DOS attack --- we'd be
2762 * in trouble if mortal users could trigger this path at
2763 * will.)
2764 *
Mingming Cao617ba132006-10-11 01:20:53 -07002765 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002766 * regular files. If somebody wants to bmap a directory
2767 * or symlink and gets confused because the buffer
2768 * hasn't yet been flushed to disk, they deserve
2769 * everything they get.
2770 */
2771
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002772 ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
Mingming Cao617ba132006-10-11 01:20:53 -07002773 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07002774 jbd2_journal_lock_updates(journal);
2775 err = jbd2_journal_flush(journal);
2776 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002777
2778 if (err)
2779 return 0;
2780 }
2781
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002782 return generic_block_bmap(mapping, block, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002783}
2784
Mingming Cao617ba132006-10-11 01:20:53 -07002785static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002786{
Tao Ma46c7f252012-12-10 14:04:52 -05002787 int ret = -EAGAIN;
2788 struct inode *inode = page->mapping->host;
2789
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04002790 trace_ext4_readpage(page);
Tao Ma46c7f252012-12-10 14:04:52 -05002791
2792 if (ext4_has_inline_data(inode))
2793 ret = ext4_readpage_inline(inode, page);
2794
2795 if (ret == -EAGAIN)
2796 return mpage_readpage(page, ext4_get_block);
2797
2798 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002799}
2800
2801static int
Mingming Cao617ba132006-10-11 01:20:53 -07002802ext4_readpages(struct file *file, struct address_space *mapping,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002803 struct list_head *pages, unsigned nr_pages)
2804{
Tao Ma46c7f252012-12-10 14:04:52 -05002805 struct inode *inode = mapping->host;
2806
2807 /* If the file has inline data, no need to do readpages. */
2808 if (ext4_has_inline_data(inode))
2809 return 0;
2810
Mingming Cao617ba132006-10-11 01:20:53 -07002811 return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002812}
2813
Mingming Cao617ba132006-10-11 01:20:53 -07002814static void ext4_invalidatepage(struct page *page, unsigned long offset)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002815{
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04002816 trace_ext4_invalidatepage(page, offset);
2817
Jan Kara4520fb32012-12-25 13:28:54 -05002818 /* No journalling happens on data buffers when this function is used */
2819 WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page)));
2820
2821 block_invalidatepage(page, offset);
2822}
2823
Jan Kara53e87262012-12-25 13:29:52 -05002824static int __ext4_journalled_invalidatepage(struct page *page,
2825 unsigned long offset)
Jan Kara4520fb32012-12-25 13:28:54 -05002826{
2827 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
2828
2829 trace_ext4_journalled_invalidatepage(page, offset);
2830
Jiaying Zhang744692d2010-03-04 16:14:02 -05002831 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002832 * If it's a full truncate we just forget about the pending dirtying
2833 */
2834 if (offset == 0)
2835 ClearPageChecked(page);
2836
Jan Kara53e87262012-12-25 13:29:52 -05002837 return jbd2_journal_invalidatepage(journal, page, offset);
2838}
2839
2840/* Wrapper for aops... */
2841static void ext4_journalled_invalidatepage(struct page *page,
2842 unsigned long offset)
2843{
2844 WARN_ON(__ext4_journalled_invalidatepage(page, offset) < 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002845}
2846
Mingming Cao617ba132006-10-11 01:20:53 -07002847static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002848{
Mingming Cao617ba132006-10-11 01:20:53 -07002849 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002850
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04002851 trace_ext4_releasepage(page);
2852
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002853 WARN_ON(PageChecked(page));
2854 if (!page_has_buffers(page))
2855 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05002856 if (journal)
2857 return jbd2_journal_try_to_free_buffers(journal, page, wait);
2858 else
2859 return try_to_free_buffers(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002860}
2861
2862/*
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002863 * ext4_get_block used when preparing for a DIO write or buffer write.
2864 * We allocate an uinitialized extent if blocks haven't been allocated.
2865 * The extent will be converted to initialized after the IO is complete.
2866 */
Tao Maf19d5872012-12-10 14:05:51 -05002867int ext4_get_block_write(struct inode *inode, sector_t iblock,
Mingming Cao4c0425f2009-09-28 15:48:41 -04002868 struct buffer_head *bh_result, int create)
2869{
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05002870 ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n",
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002871 inode->i_ino, create);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002872 return _ext4_get_block(inode, iblock, bh_result,
2873 EXT4_GET_BLOCKS_IO_CREATE_EXT);
Mingming Cao4c0425f2009-09-28 15:48:41 -04002874}
2875
Zheng Liu729f52c2012-07-09 16:29:29 -04002876static int ext4_get_block_write_nolock(struct inode *inode, sector_t iblock,
Anatol Pomozov8b0f1652012-11-08 15:07:16 -05002877 struct buffer_head *bh_result, int create)
Zheng Liu729f52c2012-07-09 16:29:29 -04002878{
Anatol Pomozov8b0f1652012-11-08 15:07:16 -05002879 ext4_debug("ext4_get_block_write_nolock: inode %lu, create flag %d\n",
2880 inode->i_ino, create);
2881 return _ext4_get_block(inode, iblock, bh_result,
2882 EXT4_GET_BLOCKS_NO_LOCK);
Zheng Liu729f52c2012-07-09 16:29:29 -04002883}
2884
Mingming Cao4c0425f2009-09-28 15:48:41 -04002885static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
Christoph Hellwig552ef8022010-07-27 11:56:06 -04002886 ssize_t size, void *private, int ret,
2887 bool is_async)
Mingming Cao4c0425f2009-09-28 15:48:41 -04002888{
Christoph Hellwig72c50522011-06-24 14:29:48 -04002889 struct inode *inode = iocb->ki_filp->f_path.dentry->d_inode;
Mingming Cao4c0425f2009-09-28 15:48:41 -04002890 ext4_io_end_t *io_end = iocb->private;
Mingming Cao4c0425f2009-09-28 15:48:41 -04002891
Mingming4b70df12009-11-03 14:44:54 -05002892 /* if not async direct IO or dio with 0 bytes write, just return */
2893 if (!io_end || !size)
Christoph Hellwig552ef8022010-07-27 11:56:06 -04002894 goto out;
Mingming4b70df12009-11-03 14:44:54 -05002895
Zheng Liu88635ca2011-12-28 19:00:25 -05002896 ext_debug("ext4_end_io_dio(): io_end 0x%p "
Joe Perchesace36ad2012-03-19 23:11:43 -04002897 "for inode %lu, iocb 0x%p, offset %llu, size %zd\n",
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002898 iocb->private, io_end->inode->i_ino, iocb, offset,
2899 size);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002900
Theodore Ts'ob5a7e972011-12-12 10:53:02 -05002901 iocb->private = NULL;
2902
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002903 /* if not aio dio with unwritten extents, just free io and return */
Theodore Ts'obd2d0212010-10-27 21:30:10 -04002904 if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002905 ext4_free_io_end(io_end);
jiayingz@google.com (Jiaying Zhang)5b3ff232010-07-27 11:56:06 -04002906out:
Jan Kara091e26d2013-01-29 22:48:17 -05002907 inode_dio_done(inode);
jiayingz@google.com (Jiaying Zhang)5b3ff232010-07-27 11:56:06 -04002908 if (is_async)
2909 aio_complete(iocb, ret, 0);
2910 return;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002911 }
2912
Mingming Cao4c0425f2009-09-28 15:48:41 -04002913 io_end->offset = offset;
2914 io_end->size = size;
jiayingz@google.com (Jiaying Zhang)5b3ff232010-07-27 11:56:06 -04002915 if (is_async) {
2916 io_end->iocb = iocb;
2917 io_end->result = ret;
2918 }
Mingming Cao4c0425f2009-09-28 15:48:41 -04002919
Dmitry Monakhov28a535f2012-09-29 00:14:55 -04002920 ext4_add_complete_io(io_end);
Mingming Cao4c0425f2009-09-28 15:48:41 -04002921}
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05002922
Mingming Cao4c0425f2009-09-28 15:48:41 -04002923/*
2924 * For ext4 extent files, ext4 will do direct-io write to holes,
2925 * preallocated extents, and those write extend the file, no need to
2926 * fall back to buffered IO.
2927 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002928 * For holes, we fallocate those blocks, mark them as uninitialized
Theodore Ts'o69c499d2012-11-29 21:13:48 -05002929 * If those blocks were preallocated, we mark sure they are split, but
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002930 * still keep the range to write as uninitialized.
Mingming Cao4c0425f2009-09-28 15:48:41 -04002931 *
Theodore Ts'o69c499d2012-11-29 21:13:48 -05002932 * The unwritten extents will be converted to written when DIO is completed.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002933 * For async direct IO, since the IO may still pending when return, we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002934 * set up an end_io call back function, which will do the conversion
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002935 * when async direct IO completed.
Mingming Cao4c0425f2009-09-28 15:48:41 -04002936 *
2937 * If the O_DIRECT write will extend the file then add this inode to the
2938 * orphan list. So recovery will truncate it back to the original size
2939 * if the machine crashes during the write.
2940 *
2941 */
2942static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
2943 const struct iovec *iov, loff_t offset,
2944 unsigned long nr_segs)
2945{
2946 struct file *file = iocb->ki_filp;
2947 struct inode *inode = file->f_mapping->host;
2948 ssize_t ret;
2949 size_t count = iov_length(iov, nr_segs);
Theodore Ts'o69c499d2012-11-29 21:13:48 -05002950 int overwrite = 0;
2951 get_block_t *get_block_func = NULL;
2952 int dio_flags = 0;
Mingming Cao4c0425f2009-09-28 15:48:41 -04002953 loff_t final_size = offset + count;
Zheng Liu729f52c2012-07-09 16:29:29 -04002954
Theodore Ts'o69c499d2012-11-29 21:13:48 -05002955 /* Use the old path for reads and writes beyond i_size. */
2956 if (rw != WRITE || final_size > inode->i_size)
2957 return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
Zheng Liu4bd809d2012-07-22 20:19:31 -04002958
Theodore Ts'o69c499d2012-11-29 21:13:48 -05002959 BUG_ON(iocb->private == NULL);
Zheng Liu4bd809d2012-07-22 20:19:31 -04002960
Theodore Ts'o69c499d2012-11-29 21:13:48 -05002961 /* If we do a overwrite dio, i_mutex locking can be released */
2962 overwrite = *((int *)iocb->private);
Zheng Liu4bd809d2012-07-22 20:19:31 -04002963
Theodore Ts'o69c499d2012-11-29 21:13:48 -05002964 if (overwrite) {
2965 atomic_inc(&inode->i_dio_count);
2966 down_read(&EXT4_I(inode)->i_data_sem);
2967 mutex_unlock(&inode->i_mutex);
Mingming Cao4c0425f2009-09-28 15:48:41 -04002968 }
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002969
Theodore Ts'o69c499d2012-11-29 21:13:48 -05002970 /*
2971 * We could direct write to holes and fallocate.
2972 *
2973 * Allocated blocks to fill the hole are marked as
2974 * uninitialized to prevent parallel buffered read to expose
2975 * the stale data before DIO complete the data IO.
2976 *
2977 * As to previously fallocated extents, ext4 get_block will
2978 * just simply mark the buffer mapped but still keep the
2979 * extents uninitialized.
2980 *
2981 * For non AIO case, we will convert those unwritten extents
2982 * to written after return back from blockdev_direct_IO.
2983 *
2984 * For async DIO, the conversion needs to be deferred when the
2985 * IO is completed. The ext4 end_io callback function will be
2986 * called to take care of the conversion work. Here for async
2987 * case, we allocate an io_end structure to hook to the iocb.
2988 */
2989 iocb->private = NULL;
2990 ext4_inode_aio_set(inode, NULL);
2991 if (!is_sync_kiocb(iocb)) {
2992 ext4_io_end_t *io_end = ext4_init_io_end(inode, GFP_NOFS);
2993 if (!io_end) {
2994 ret = -ENOMEM;
2995 goto retake_lock;
2996 }
2997 io_end->flag |= EXT4_IO_END_DIRECT;
2998 iocb->private = io_end;
2999 /*
3000 * we save the io structure for current async direct
3001 * IO, so that later ext4_map_blocks() could flag the
3002 * io structure whether there is a unwritten extents
3003 * needs to be converted when IO is completed.
3004 */
3005 ext4_inode_aio_set(inode, io_end);
3006 }
3007
3008 if (overwrite) {
3009 get_block_func = ext4_get_block_write_nolock;
3010 } else {
3011 get_block_func = ext4_get_block_write;
3012 dio_flags = DIO_LOCKING;
3013 }
3014 ret = __blockdev_direct_IO(rw, iocb, inode,
3015 inode->i_sb->s_bdev, iov,
3016 offset, nr_segs,
3017 get_block_func,
3018 ext4_end_io_dio,
3019 NULL,
3020 dio_flags);
3021
3022 if (iocb->private)
3023 ext4_inode_aio_set(inode, NULL);
3024 /*
3025 * The io_end structure takes a reference to the inode, that
3026 * structure needs to be destroyed and the reference to the
3027 * inode need to be dropped, when IO is complete, even with 0
3028 * byte write, or failed.
3029 *
3030 * In the successful AIO DIO case, the io_end structure will
3031 * be destroyed and the reference to the inode will be dropped
3032 * after the end_io call back function is called.
3033 *
3034 * In the case there is 0 byte write, or error case, since VFS
3035 * direct IO won't invoke the end_io call back function, we
3036 * need to free the end_io structure here.
3037 */
3038 if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) {
3039 ext4_free_io_end(iocb->private);
3040 iocb->private = NULL;
3041 } else if (ret > 0 && !overwrite && ext4_test_inode_state(inode,
3042 EXT4_STATE_DIO_UNWRITTEN)) {
3043 int err;
3044 /*
3045 * for non AIO case, since the IO is already
3046 * completed, we could do the conversion right here
3047 */
3048 err = ext4_convert_unwritten_extents(inode,
3049 offset, ret);
3050 if (err < 0)
3051 ret = err;
3052 ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
3053 }
3054
3055retake_lock:
3056 /* take i_mutex locking again if we do a ovewrite dio */
3057 if (overwrite) {
3058 inode_dio_done(inode);
3059 up_read(&EXT4_I(inode)->i_data_sem);
3060 mutex_lock(&inode->i_mutex);
3061 }
3062
3063 return ret;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003064}
3065
3066static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
3067 const struct iovec *iov, loff_t offset,
3068 unsigned long nr_segs)
3069{
3070 struct file *file = iocb->ki_filp;
3071 struct inode *inode = file->f_mapping->host;
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003072 ssize_t ret;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003073
Theodore Ts'o84ebd792011-08-31 11:56:51 -04003074 /*
3075 * If we are doing data journalling we don't support O_DIRECT
3076 */
3077 if (ext4_should_journal_data(inode))
3078 return 0;
3079
Tao Ma46c7f252012-12-10 14:04:52 -05003080 /* Let buffer I/O handle the inline data case. */
3081 if (ext4_has_inline_data(inode))
3082 return 0;
3083
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003084 trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003085 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003086 ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs);
3087 else
3088 ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
3089 trace_ext4_direct_IO_exit(inode, offset,
3090 iov_length(iov, nr_segs), rw, ret);
3091 return ret;
Mingming Cao4c0425f2009-09-28 15:48:41 -04003092}
3093
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003094/*
Mingming Cao617ba132006-10-11 01:20:53 -07003095 * Pages can be marked dirty completely asynchronously from ext4's journalling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003096 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
3097 * much here because ->set_page_dirty is called under VFS locks. The page is
3098 * not necessarily locked.
3099 *
3100 * We cannot just dirty the page and leave attached buffers clean, because the
3101 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
3102 * or jbddirty because all the journalling code will explode.
3103 *
3104 * So what we do is to mark the page "pending dirty" and next time writepage
3105 * is called, propagate that into the buffers appropriately.
3106 */
Mingming Cao617ba132006-10-11 01:20:53 -07003107static int ext4_journalled_set_page_dirty(struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003108{
3109 SetPageChecked(page);
3110 return __set_page_dirty_nobuffers(page);
3111}
3112
Mingming Cao617ba132006-10-11 01:20:53 -07003113static const struct address_space_operations ext4_ordered_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003114 .readpage = ext4_readpage,
3115 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003116 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003117 .write_begin = ext4_write_begin,
3118 .write_end = ext4_ordered_write_end,
3119 .bmap = ext4_bmap,
3120 .invalidatepage = ext4_invalidatepage,
3121 .releasepage = ext4_releasepage,
3122 .direct_IO = ext4_direct_IO,
3123 .migratepage = buffer_migrate_page,
3124 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003125 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003126};
3127
Mingming Cao617ba132006-10-11 01:20:53 -07003128static const struct address_space_operations ext4_writeback_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003129 .readpage = ext4_readpage,
3130 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003131 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003132 .write_begin = ext4_write_begin,
3133 .write_end = ext4_writeback_write_end,
3134 .bmap = ext4_bmap,
3135 .invalidatepage = ext4_invalidatepage,
3136 .releasepage = ext4_releasepage,
3137 .direct_IO = ext4_direct_IO,
3138 .migratepage = buffer_migrate_page,
3139 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003140 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003141};
3142
Mingming Cao617ba132006-10-11 01:20:53 -07003143static const struct address_space_operations ext4_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003144 .readpage = ext4_readpage,
3145 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003146 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003147 .write_begin = ext4_write_begin,
3148 .write_end = ext4_journalled_write_end,
3149 .set_page_dirty = ext4_journalled_set_page_dirty,
3150 .bmap = ext4_bmap,
Jan Kara4520fb32012-12-25 13:28:54 -05003151 .invalidatepage = ext4_journalled_invalidatepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003152 .releasepage = ext4_releasepage,
Theodore Ts'o84ebd792011-08-31 11:56:51 -04003153 .direct_IO = ext4_direct_IO,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003154 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003155 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003156};
3157
Alex Tomas64769242008-07-11 19:27:31 -04003158static const struct address_space_operations ext4_da_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003159 .readpage = ext4_readpage,
3160 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04003161 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003162 .writepages = ext4_da_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07003163 .write_begin = ext4_da_write_begin,
3164 .write_end = ext4_da_write_end,
3165 .bmap = ext4_bmap,
3166 .invalidatepage = ext4_da_invalidatepage,
3167 .releasepage = ext4_releasepage,
3168 .direct_IO = ext4_direct_IO,
3169 .migratepage = buffer_migrate_page,
3170 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02003171 .error_remove_page = generic_error_remove_page,
Alex Tomas64769242008-07-11 19:27:31 -04003172};
3173
Mingming Cao617ba132006-10-11 01:20:53 -07003174void ext4_set_aops(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003175{
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003176 switch (ext4_inode_journal_mode(inode)) {
3177 case EXT4_INODE_ORDERED_DATA_MODE:
3178 if (test_opt(inode->i_sb, DELALLOC))
3179 inode->i_mapping->a_ops = &ext4_da_aops;
3180 else
3181 inode->i_mapping->a_ops = &ext4_ordered_aops;
3182 break;
3183 case EXT4_INODE_WRITEBACK_DATA_MODE:
3184 if (test_opt(inode->i_sb, DELALLOC))
3185 inode->i_mapping->a_ops = &ext4_da_aops;
3186 else
3187 inode->i_mapping->a_ops = &ext4_writeback_aops;
3188 break;
3189 case EXT4_INODE_JOURNAL_DATA_MODE:
Mingming Cao617ba132006-10-11 01:20:53 -07003190 inode->i_mapping->a_ops = &ext4_journalled_aops;
Lukas Czerner3d2b1582012-02-20 17:53:00 -05003191 break;
3192 default:
3193 BUG();
3194 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003195}
3196
Allison Henderson4e96b2d2011-09-03 11:51:09 -04003197
3198/*
3199 * ext4_discard_partial_page_buffers()
3200 * Wrapper function for ext4_discard_partial_page_buffers_no_lock.
3201 * This function finds and locks the page containing the offset
3202 * "from" and passes it to ext4_discard_partial_page_buffers_no_lock.
3203 * Calling functions that already have the page locked should call
3204 * ext4_discard_partial_page_buffers_no_lock directly.
3205 */
3206int ext4_discard_partial_page_buffers(handle_t *handle,
3207 struct address_space *mapping, loff_t from,
3208 loff_t length, int flags)
3209{
3210 struct inode *inode = mapping->host;
3211 struct page *page;
3212 int err = 0;
3213
3214 page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
3215 mapping_gfp_mask(mapping) & ~__GFP_FS);
3216 if (!page)
Yongqiang Yang5129d052011-10-31 17:56:10 -04003217 return -ENOMEM;
Allison Henderson4e96b2d2011-09-03 11:51:09 -04003218
3219 err = ext4_discard_partial_page_buffers_no_lock(handle, inode, page,
3220 from, length, flags);
3221
3222 unlock_page(page);
3223 page_cache_release(page);
3224 return err;
3225}
3226
3227/*
3228 * ext4_discard_partial_page_buffers_no_lock()
3229 * Zeros a page range of length 'length' starting from offset 'from'.
3230 * Buffer heads that correspond to the block aligned regions of the
3231 * zeroed range will be unmapped. Unblock aligned regions
3232 * will have the corresponding buffer head mapped if needed so that
3233 * that region of the page can be updated with the partial zero out.
3234 *
3235 * This function assumes that the page has already been locked. The
3236 * The range to be discarded must be contained with in the given page.
3237 * If the specified range exceeds the end of the page it will be shortened
3238 * to the end of the page that corresponds to 'from'. This function is
3239 * appropriate for updating a page and it buffer heads to be unmapped and
3240 * zeroed for blocks that have been either released, or are going to be
3241 * released.
3242 *
3243 * handle: The journal handle
3244 * inode: The files inode
3245 * page: A locked page that contains the offset "from"
Anatol Pomozov4907cb72012-09-01 10:31:09 -07003246 * from: The starting byte offset (from the beginning of the file)
Allison Henderson4e96b2d2011-09-03 11:51:09 -04003247 * to begin discarding
3248 * len: The length of bytes to discard
3249 * flags: Optional flags that may be used:
3250 *
3251 * EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED
3252 * Only zero the regions of the page whose buffer heads
3253 * have already been unmapped. This flag is appropriate
Anatol Pomozov4907cb72012-09-01 10:31:09 -07003254 * for updating the contents of a page whose blocks may
Allison Henderson4e96b2d2011-09-03 11:51:09 -04003255 * have already been released, and we only want to zero
3256 * out the regions that correspond to those released blocks.
3257 *
Anatol Pomozov4907cb72012-09-01 10:31:09 -07003258 * Returns zero on success or negative on failure.
Allison Henderson4e96b2d2011-09-03 11:51:09 -04003259 */
Eric Sandeen5f163cc2012-01-04 22:33:28 -05003260static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle,
Allison Henderson4e96b2d2011-09-03 11:51:09 -04003261 struct inode *inode, struct page *page, loff_t from,
3262 loff_t length, int flags)
3263{
3264 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
3265 unsigned int offset = from & (PAGE_CACHE_SIZE-1);
3266 unsigned int blocksize, max, pos;
Allison Henderson4e96b2d2011-09-03 11:51:09 -04003267 ext4_lblk_t iblock;
3268 struct buffer_head *bh;
3269 int err = 0;
3270
3271 blocksize = inode->i_sb->s_blocksize;
3272 max = PAGE_CACHE_SIZE - offset;
3273
3274 if (index != page->index)
3275 return -EINVAL;
3276
3277 /*
3278 * correct length if it does not fall between
3279 * 'from' and the end of the page
3280 */
3281 if (length > max || length < 0)
3282 length = max;
3283
3284 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
3285
Yongqiang Yang093e6e32011-12-13 22:05:05 -05003286 if (!page_has_buffers(page))
3287 create_empty_buffers(page, blocksize, 0);
Allison Henderson4e96b2d2011-09-03 11:51:09 -04003288
3289 /* Find the buffer that contains "offset" */
3290 bh = page_buffers(page);
3291 pos = blocksize;
3292 while (offset >= pos) {
3293 bh = bh->b_this_page;
3294 iblock++;
3295 pos += blocksize;
3296 }
3297
3298 pos = offset;
3299 while (pos < offset + length) {
Yongqiang Yange260daf2011-10-31 17:54:36 -04003300 unsigned int end_of_block, range_to_discard;
3301
Allison Henderson4e96b2d2011-09-03 11:51:09 -04003302 err = 0;
3303
3304 /* The length of space left to zero and unmap */
3305 range_to_discard = offset + length - pos;
3306
3307 /* The length of space until the end of the block */
3308 end_of_block = blocksize - (pos & (blocksize-1));
3309
3310 /*
3311 * Do not unmap or zero past end of block
3312 * for this buffer head
3313 */
3314 if (range_to_discard > end_of_block)
3315 range_to_discard = end_of_block;
3316
3317
3318 /*
3319 * Skip this buffer head if we are only zeroing unampped
3320 * regions of the page
3321 */
3322 if (flags & EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED &&
3323 buffer_mapped(bh))
3324 goto next;
3325
3326 /* If the range is block aligned, unmap */
3327 if (range_to_discard == blocksize) {
3328 clear_buffer_dirty(bh);
3329 bh->b_bdev = NULL;
3330 clear_buffer_mapped(bh);
3331 clear_buffer_req(bh);
3332 clear_buffer_new(bh);
3333 clear_buffer_delay(bh);
3334 clear_buffer_unwritten(bh);
3335 clear_buffer_uptodate(bh);
3336 zero_user(page, pos, range_to_discard);
3337 BUFFER_TRACE(bh, "Buffer discarded");
3338 goto next;
3339 }
3340
3341 /*
3342 * If this block is not completely contained in the range
3343 * to be discarded, then it is not going to be released. Because
3344 * we need to keep this block, we need to make sure this part
3345 * of the page is uptodate before we modify it by writeing
3346 * partial zeros on it.
3347 */
3348 if (!buffer_mapped(bh)) {
3349 /*
3350 * Buffer head must be mapped before we can read
3351 * from the block
3352 */
3353 BUFFER_TRACE(bh, "unmapped");
3354 ext4_get_block(inode, iblock, bh, 0);
3355 /* unmapped? It's a hole - nothing to do */
3356 if (!buffer_mapped(bh)) {
3357 BUFFER_TRACE(bh, "still unmapped");
3358 goto next;
3359 }
3360 }
3361
3362 /* Ok, it's mapped. Make sure it's up-to-date */
3363 if (PageUptodate(page))
3364 set_buffer_uptodate(bh);
3365
3366 if (!buffer_uptodate(bh)) {
3367 err = -EIO;
3368 ll_rw_block(READ, 1, &bh);
3369 wait_on_buffer(bh);
3370 /* Uhhuh. Read error. Complain and punt.*/
3371 if (!buffer_uptodate(bh))
3372 goto next;
3373 }
3374
3375 if (ext4_should_journal_data(inode)) {
3376 BUFFER_TRACE(bh, "get write access");
3377 err = ext4_journal_get_write_access(handle, bh);
3378 if (err)
3379 goto next;
3380 }
3381
3382 zero_user(page, pos, range_to_discard);
3383
3384 err = 0;
3385 if (ext4_should_journal_data(inode)) {
3386 err = ext4_handle_dirty_metadata(handle, inode, bh);
Theodore Ts'odecbd912011-09-06 02:37:06 -04003387 } else
Allison Henderson4e96b2d2011-09-03 11:51:09 -04003388 mark_buffer_dirty(bh);
Allison Henderson4e96b2d2011-09-03 11:51:09 -04003389
3390 BUFFER_TRACE(bh, "Partial buffer zeroed");
3391next:
3392 bh = bh->b_this_page;
3393 iblock++;
3394 pos += range_to_discard;
3395 }
3396
3397 return err;
3398}
3399
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003400int ext4_can_truncate(struct inode *inode)
3401{
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003402 if (S_ISREG(inode->i_mode))
3403 return 1;
3404 if (S_ISDIR(inode->i_mode))
3405 return 1;
3406 if (S_ISLNK(inode->i_mode))
3407 return !ext4_inode_is_fast_symlink(inode);
3408 return 0;
3409}
3410
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003411/*
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003412 * ext4_punch_hole: punches a hole in a file by releaseing the blocks
3413 * associated with the given offset and length
3414 *
3415 * @inode: File inode
3416 * @offset: The offset where the hole will begin
3417 * @len: The length of the hole
3418 *
Anatol Pomozov4907cb72012-09-01 10:31:09 -07003419 * Returns: 0 on success or negative on failure
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003420 */
3421
3422int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
3423{
3424 struct inode *inode = file->f_path.dentry->d_inode;
3425 if (!S_ISREG(inode->i_mode))
Allison Henderson73355192012-03-21 22:23:31 -04003426 return -EOPNOTSUPP;
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003427
Zheng Liu8bad6fc2013-01-28 09:21:37 -05003428 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3429 return ext4_ind_punch_hole(file, offset, length);
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003430
Theodore Ts'obab08ab2011-09-09 18:36:51 -04003431 if (EXT4_SB(inode->i_sb)->s_cluster_ratio > 1) {
3432 /* TODO: Add support for bigalloc file systems */
Allison Henderson73355192012-03-21 22:23:31 -04003433 return -EOPNOTSUPP;
Theodore Ts'obab08ab2011-09-09 18:36:51 -04003434 }
3435
Zheng Liuaaddea82013-01-16 20:21:26 -05003436 trace_ext4_punch_hole(inode, offset, length);
3437
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003438 return ext4_ext_punch_hole(file, offset, length);
3439}
3440
3441/*
Mingming Cao617ba132006-10-11 01:20:53 -07003442 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003443 *
Mingming Cao617ba132006-10-11 01:20:53 -07003444 * We block out ext4_get_block() block instantiations across the entire
3445 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003446 * simultaneously on behalf of the same inode.
3447 *
Justin P. Mattock42b2aa82011-11-28 20:31:00 -08003448 * As we work through the truncate and commit bits of it to the journal there
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003449 * is one core, guiding principle: the file's tree must always be consistent on
3450 * disk. We must be able to restart the truncate after a crash.
3451 *
3452 * The file's tree may be transiently inconsistent in memory (although it
3453 * probably isn't), but whenever we close off and commit a journal transaction,
3454 * the contents of (the filesystem + the journal) must be consistent and
3455 * restartable. It's pretty simple, really: bottom up, right to left (although
3456 * left-to-right works OK too).
3457 *
3458 * Note that at recovery time, journal replay occurs *before* the restart of
3459 * truncate against the orphan inode list.
3460 *
3461 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07003462 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003463 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07003464 * ext4_truncate() to have another go. So there will be instantiated blocks
3465 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003466 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07003467 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003468 */
Mingming Cao617ba132006-10-11 01:20:53 -07003469void ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003470{
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003471 trace_ext4_truncate_enter(inode);
3472
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003473 if (!ext4_can_truncate(inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003474 return;
3475
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003476 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003477
Theodore Ts'o5534fb52009-09-17 09:34:16 -04003478 if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003479 ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
Theodore Ts'o7d8f9f72009-02-24 08:21:14 -05003480
Tao Maaef1c852012-12-10 14:06:02 -05003481 if (ext4_has_inline_data(inode)) {
3482 int has_inline = 1;
3483
3484 ext4_inline_data_truncate(inode, &has_inline);
3485 if (has_inline)
3486 return;
3487 }
3488
Amir Goldsteinff9893d2011-06-27 16:36:31 -04003489 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Jan Karacf108bc2008-07-11 19:27:31 -04003490 ext4_ext_truncate(inode);
Amir Goldsteinff9893d2011-06-27 16:36:31 -04003491 else
3492 ext4_ind_truncate(inode);
Alex Tomasa86c6182006-10-11 01:21:03 -07003493
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003494 trace_ext4_truncate_exit(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003495}
3496
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003497/*
Mingming Cao617ba132006-10-11 01:20:53 -07003498 * ext4_get_inode_loc returns with an extra refcount against the inode's
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003499 * underlying buffer_head on success. If 'in_mem' is true, we have all
3500 * data in memory that is needed to recreate the on-disk version of this
3501 * inode.
3502 */
Mingming Cao617ba132006-10-11 01:20:53 -07003503static int __ext4_get_inode_loc(struct inode *inode,
3504 struct ext4_iloc *iloc, int in_mem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003505{
Theodore Ts'o240799c2008-10-09 23:53:47 -04003506 struct ext4_group_desc *gdp;
3507 struct buffer_head *bh;
3508 struct super_block *sb = inode->i_sb;
3509 ext4_fsblk_t block;
3510 int inodes_per_block, inode_offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003511
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003512 iloc->bh = NULL;
Theodore Ts'o240799c2008-10-09 23:53:47 -04003513 if (!ext4_valid_inum(sb, inode->i_ino))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003514 return -EIO;
3515
Theodore Ts'o240799c2008-10-09 23:53:47 -04003516 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
3517 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
3518 if (!gdp)
3519 return -EIO;
3520
3521 /*
3522 * Figure out the offset within the block group inode table
3523 */
Tao Ma00d09882011-05-09 10:26:41 -04003524 inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
Theodore Ts'o240799c2008-10-09 23:53:47 -04003525 inode_offset = ((inode->i_ino - 1) %
3526 EXT4_INODES_PER_GROUP(sb));
3527 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
3528 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
3529
3530 bh = sb_getblk(sb, block);
Wang Shilongaebf0242013-01-12 16:28:47 -05003531 if (unlikely(!bh))
Theodore Ts'o860d21e2013-01-12 16:19:36 -05003532 return -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003533 if (!buffer_uptodate(bh)) {
3534 lock_buffer(bh);
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04003535
3536 /*
3537 * If the buffer has the write error flag, we have failed
3538 * to write out another inode in the same block. In this
3539 * case, we don't have to read the block because we may
3540 * read the old inode data successfully.
3541 */
3542 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
3543 set_buffer_uptodate(bh);
3544
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003545 if (buffer_uptodate(bh)) {
3546 /* someone brought it uptodate while we waited */
3547 unlock_buffer(bh);
3548 goto has_buffer;
3549 }
3550
3551 /*
3552 * If we have all information of the inode in memory and this
3553 * is the only valid inode in the block, we need not read the
3554 * block.
3555 */
3556 if (in_mem) {
3557 struct buffer_head *bitmap_bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04003558 int i, start;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003559
Theodore Ts'o240799c2008-10-09 23:53:47 -04003560 start = inode_offset & ~(inodes_per_block - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003561
3562 /* Is the inode bitmap in cache? */
Theodore Ts'o240799c2008-10-09 23:53:47 -04003563 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
Wang Shilongaebf0242013-01-12 16:28:47 -05003564 if (unlikely(!bitmap_bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003565 goto make_io;
3566
3567 /*
3568 * If the inode bitmap isn't in cache then the
3569 * optimisation may end up performing two reads instead
3570 * of one, so skip it.
3571 */
3572 if (!buffer_uptodate(bitmap_bh)) {
3573 brelse(bitmap_bh);
3574 goto make_io;
3575 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04003576 for (i = start; i < start + inodes_per_block; i++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003577 if (i == inode_offset)
3578 continue;
Mingming Cao617ba132006-10-11 01:20:53 -07003579 if (ext4_test_bit(i, bitmap_bh->b_data))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003580 break;
3581 }
3582 brelse(bitmap_bh);
Theodore Ts'o240799c2008-10-09 23:53:47 -04003583 if (i == start + inodes_per_block) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003584 /* all other inodes are free, so skip I/O */
3585 memset(bh->b_data, 0, bh->b_size);
3586 set_buffer_uptodate(bh);
3587 unlock_buffer(bh);
3588 goto has_buffer;
3589 }
3590 }
3591
3592make_io:
3593 /*
Theodore Ts'o240799c2008-10-09 23:53:47 -04003594 * If we need to do any I/O, try to pre-readahead extra
3595 * blocks from the inode table.
3596 */
3597 if (EXT4_SB(sb)->s_inode_readahead_blks) {
3598 ext4_fsblk_t b, end, table;
3599 unsigned num;
3600
3601 table = ext4_inode_table(sb, gdp);
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003602 /* s_inode_readahead_blks is always a power of 2 */
Theodore Ts'o240799c2008-10-09 23:53:47 -04003603 b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1);
3604 if (table > b)
3605 b = table;
3606 end = b + EXT4_SB(sb)->s_inode_readahead_blks;
3607 num = EXT4_INODES_PER_GROUP(sb);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04003608 if (ext4_has_group_desc_csum(sb))
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05003609 num -= ext4_itable_unused_count(sb, gdp);
Theodore Ts'o240799c2008-10-09 23:53:47 -04003610 table += num / inodes_per_block;
3611 if (end > table)
3612 end = table;
3613 while (b <= end)
3614 sb_breadahead(sb, b++);
3615 }
3616
3617 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003618 * There are other valid inodes in the buffer, this inode
3619 * has in-inode xattrs, or we don't have this inode in memory.
3620 * Read the block from disk.
3621 */
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003622 trace_ext4_load_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003623 get_bh(bh);
3624 bh->b_end_io = end_buffer_read_sync;
Christoph Hellwig65299a32011-08-23 14:50:29 +02003625 submit_bh(READ | REQ_META | REQ_PRIO, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003626 wait_on_buffer(bh);
3627 if (!buffer_uptodate(bh)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -04003628 EXT4_ERROR_INODE_BLOCK(inode, block,
3629 "unable to read itable block");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003630 brelse(bh);
3631 return -EIO;
3632 }
3633 }
3634has_buffer:
3635 iloc->bh = bh;
3636 return 0;
3637}
3638
Mingming Cao617ba132006-10-11 01:20:53 -07003639int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003640{
3641 /* We have all inode data except xattrs in memory here. */
Mingming Cao617ba132006-10-11 01:20:53 -07003642 return __ext4_get_inode_loc(inode, iloc,
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003643 !ext4_test_inode_state(inode, EXT4_STATE_XATTR));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003644}
3645
Mingming Cao617ba132006-10-11 01:20:53 -07003646void ext4_set_inode_flags(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003647{
Mingming Cao617ba132006-10-11 01:20:53 -07003648 unsigned int flags = EXT4_I(inode)->i_flags;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003649
3650 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
Mingming Cao617ba132006-10-11 01:20:53 -07003651 if (flags & EXT4_SYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003652 inode->i_flags |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07003653 if (flags & EXT4_APPEND_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003654 inode->i_flags |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07003655 if (flags & EXT4_IMMUTABLE_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003656 inode->i_flags |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07003657 if (flags & EXT4_NOATIME_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003658 inode->i_flags |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07003659 if (flags & EXT4_DIRSYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003660 inode->i_flags |= S_DIRSYNC;
3661}
3662
Jan Karaff9ddf72007-07-18 09:24:20 -04003663/* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
3664void ext4_get_inode_flags(struct ext4_inode_info *ei)
3665{
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04003666 unsigned int vfs_fl;
3667 unsigned long old_fl, new_fl;
Jan Karaff9ddf72007-07-18 09:24:20 -04003668
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04003669 do {
3670 vfs_fl = ei->vfs_inode.i_flags;
3671 old_fl = ei->i_flags;
3672 new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
3673 EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|
3674 EXT4_DIRSYNC_FL);
3675 if (vfs_fl & S_SYNC)
3676 new_fl |= EXT4_SYNC_FL;
3677 if (vfs_fl & S_APPEND)
3678 new_fl |= EXT4_APPEND_FL;
3679 if (vfs_fl & S_IMMUTABLE)
3680 new_fl |= EXT4_IMMUTABLE_FL;
3681 if (vfs_fl & S_NOATIME)
3682 new_fl |= EXT4_NOATIME_FL;
3683 if (vfs_fl & S_DIRSYNC)
3684 new_fl |= EXT4_DIRSYNC_FL;
3685 } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl);
Jan Karaff9ddf72007-07-18 09:24:20 -04003686}
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003687
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003688static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003689 struct ext4_inode_info *ei)
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003690{
3691 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05003692 struct inode *inode = &(ei->vfs_inode);
3693 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003694
3695 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3696 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
3697 /* we are using combined 48 bit field */
3698 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
3699 le32_to_cpu(raw_inode->i_blocks_lo);
Theodore Ts'o07a03822010-06-14 09:54:48 -04003700 if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05003701 /* i_blocks represent file system block size */
3702 return i_blocks << (inode->i_blkbits - 9);
3703 } else {
3704 return i_blocks;
3705 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003706 } else {
3707 return le32_to_cpu(raw_inode->i_blocks_lo);
3708 }
3709}
Jan Karaff9ddf72007-07-18 09:24:20 -04003710
Tao Ma152a7b02012-12-02 11:13:24 -05003711static inline void ext4_iget_extra_inode(struct inode *inode,
3712 struct ext4_inode *raw_inode,
3713 struct ext4_inode_info *ei)
3714{
3715 __le32 *magic = (void *)raw_inode +
3716 EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize;
Tao Ma67cf5b02012-12-10 14:04:46 -05003717 if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC)) {
Tao Ma152a7b02012-12-02 11:13:24 -05003718 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Tao Ma67cf5b02012-12-10 14:04:46 -05003719 ext4_find_inline_data_nolock(inode);
Tao Maf19d5872012-12-10 14:05:51 -05003720 } else
3721 EXT4_I(inode)->i_inline_off = 0;
Tao Ma152a7b02012-12-02 11:13:24 -05003722}
3723
David Howells1d1fe1e2008-02-07 00:15:37 -08003724struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003725{
Mingming Cao617ba132006-10-11 01:20:53 -07003726 struct ext4_iloc iloc;
3727 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08003728 struct ext4_inode_info *ei;
David Howells1d1fe1e2008-02-07 00:15:37 -08003729 struct inode *inode;
Jan Karab436b9b2009-12-08 23:51:10 -05003730 journal_t *journal = EXT4_SB(sb)->s_journal;
David Howells1d1fe1e2008-02-07 00:15:37 -08003731 long ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003732 int block;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08003733 uid_t i_uid;
3734 gid_t i_gid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003735
David Howells1d1fe1e2008-02-07 00:15:37 -08003736 inode = iget_locked(sb, ino);
3737 if (!inode)
3738 return ERR_PTR(-ENOMEM);
3739 if (!(inode->i_state & I_NEW))
3740 return inode;
3741
3742 ei = EXT4_I(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05003743 iloc.bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003744
David Howells1d1fe1e2008-02-07 00:15:37 -08003745 ret = __ext4_get_inode_loc(inode, &iloc, 0);
3746 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003747 goto bad_inode;
Mingming Cao617ba132006-10-11 01:20:53 -07003748 raw_inode = ext4_raw_inode(&iloc);
Darrick J. Wong814525f2012-04-29 18:31:10 -04003749
3750 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
3751 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
3752 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
3753 EXT4_INODE_SIZE(inode->i_sb)) {
3754 EXT4_ERROR_INODE(inode, "bad extra_isize (%u != %u)",
3755 EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize,
3756 EXT4_INODE_SIZE(inode->i_sb));
3757 ret = -EIO;
3758 goto bad_inode;
3759 }
3760 } else
3761 ei->i_extra_isize = 0;
3762
3763 /* Precompute checksum seed for inode metadata */
3764 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3765 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
3766 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3767 __u32 csum;
3768 __le32 inum = cpu_to_le32(inode->i_ino);
3769 __le32 gen = raw_inode->i_generation;
3770 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&inum,
3771 sizeof(inum));
3772 ei->i_csum_seed = ext4_chksum(sbi, csum, (__u8 *)&gen,
3773 sizeof(gen));
3774 }
3775
3776 if (!ext4_inode_csum_verify(inode, raw_inode, ei)) {
3777 EXT4_ERROR_INODE(inode, "checksum invalid");
3778 ret = -EIO;
3779 goto bad_inode;
3780 }
3781
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003782 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08003783 i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
3784 i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003785 if (!(test_opt(inode->i_sb, NO_UID32))) {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08003786 i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
3787 i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003788 }
Eric W. Biederman08cefc72012-02-07 15:41:49 -08003789 i_uid_write(inode, i_uid);
3790 i_gid_write(inode, i_gid);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003791 set_nlink(inode, le16_to_cpu(raw_inode->i_links_count));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003792
Theodore Ts'o353eb832011-01-10 12:18:25 -05003793 ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
Tao Ma67cf5b02012-12-10 14:04:46 -05003794 ei->i_inline_off = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003795 ei->i_dir_start_lookup = 0;
3796 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
3797 /* We now have enough fields to check if the inode was active or not.
3798 * This is needed because nfsd might try to access dead inodes
3799 * the test is that same one that e2fsck uses
3800 * NeilBrown 1999oct15
3801 */
3802 if (inode->i_nlink == 0) {
3803 if (inode->i_mode == 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07003804 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003805 /* this inode is deleted */
David Howells1d1fe1e2008-02-07 00:15:37 -08003806 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003807 goto bad_inode;
3808 }
3809 /* The only unlinked inodes we let through here have
3810 * valid i_mode and are being read by the orphan
3811 * recovery code: that's fine, we're about to complete
3812 * the process of deleting those. */
3813 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003814 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003815 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05003816 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Theodore Ts'oa9e81742009-04-24 16:11:18 -04003817 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07003818 ei->i_file_acl |=
3819 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05003820 inode->i_size = ext4_isize(raw_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003821 ei->i_disksize = inode->i_size;
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03003822#ifdef CONFIG_QUOTA
3823 ei->i_reserved_quota = 0;
3824#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003825 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
3826 ei->i_block_group = iloc.block_group;
Theodore Ts'oa4912122009-03-12 12:18:34 -04003827 ei->i_last_alloc_group = ~0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003828 /*
3829 * NOTE! The in-memory inode i_data array is in little-endian order
3830 * even on big-endian machines: we do NOT byteswap the block numbers!
3831 */
Mingming Cao617ba132006-10-11 01:20:53 -07003832 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003833 ei->i_data[block] = raw_inode->i_block[block];
3834 INIT_LIST_HEAD(&ei->i_orphan);
3835
Jan Karab436b9b2009-12-08 23:51:10 -05003836 /*
3837 * Set transaction id's of transactions that have to be committed
3838 * to finish f[data]sync. We set them to currently running transaction
3839 * as we cannot be sure that the inode or some of its metadata isn't
3840 * part of the transaction - the inode could have been reclaimed and
3841 * now it is reread from disk.
3842 */
3843 if (journal) {
3844 transaction_t *transaction;
3845 tid_t tid;
3846
Theodore Ts'oa931da62010-08-03 21:35:12 -04003847 read_lock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05003848 if (journal->j_running_transaction)
3849 transaction = journal->j_running_transaction;
3850 else
3851 transaction = journal->j_committing_transaction;
3852 if (transaction)
3853 tid = transaction->t_tid;
3854 else
3855 tid = journal->j_commit_sequence;
Theodore Ts'oa931da62010-08-03 21:35:12 -04003856 read_unlock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05003857 ei->i_sync_tid = tid;
3858 ei->i_datasync_tid = tid;
3859 }
3860
Eric Sandeen0040d982008-02-05 22:36:43 -05003861 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003862 if (ei->i_extra_isize == 0) {
3863 /* The extra space is currently unused. Use it. */
Mingming Cao617ba132006-10-11 01:20:53 -07003864 ei->i_extra_isize = sizeof(struct ext4_inode) -
3865 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003866 } else {
Tao Ma152a7b02012-12-02 11:13:24 -05003867 ext4_iget_extra_inode(inode, raw_inode, ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003868 }
Darrick J. Wong814525f2012-04-29 18:31:10 -04003869 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003870
Kalpak Shahef7f3832007-07-18 09:15:20 -04003871 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
3872 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
3873 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
3874 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
3875
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05003876 inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
3877 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
3878 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
3879 inode->i_version |=
3880 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
3881 }
3882
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04003883 ret = 0;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04003884 if (ei->i_file_acl &&
Theodore Ts'o10329882009-11-15 15:29:56 -05003885 !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04003886 EXT4_ERROR_INODE(inode, "bad extended attribute block %llu",
3887 ei->i_file_acl);
Theodore Ts'o485c26e2009-04-24 13:43:20 -04003888 ret = -EIO;
3889 goto bad_inode;
Tao Maf19d5872012-12-10 14:05:51 -05003890 } else if (!ext4_has_inline_data(inode)) {
3891 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
3892 if ((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
3893 (S_ISLNK(inode->i_mode) &&
3894 !ext4_inode_is_fast_symlink(inode))))
3895 /* Validate extent which is part of inode */
3896 ret = ext4_ext_check_inode(inode);
3897 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
3898 (S_ISLNK(inode->i_mode) &&
3899 !ext4_inode_is_fast_symlink(inode))) {
3900 /* Validate block references which are part of inode */
3901 ret = ext4_ind_check_inode(inode);
3902 }
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04003903 }
Theodore Ts'o567f3e92009-11-14 08:19:05 -05003904 if (ret)
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003905 goto bad_inode;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -04003906
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003907 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003908 inode->i_op = &ext4_file_inode_operations;
3909 inode->i_fop = &ext4_file_operations;
3910 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003911 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003912 inode->i_op = &ext4_dir_inode_operations;
3913 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003914 } else if (S_ISLNK(inode->i_mode)) {
Duane Griffine83c1392008-12-19 20:47:15 +00003915 if (ext4_inode_is_fast_symlink(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003916 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00003917 nd_terminate_link(ei->i_data, inode->i_size,
3918 sizeof(ei->i_data) - 1);
3919 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003920 inode->i_op = &ext4_symlink_inode_operations;
3921 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003922 }
Theodore Ts'o563bdd62009-03-26 00:06:19 -04003923 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
3924 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003925 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003926 if (raw_inode->i_block[0])
3927 init_special_inode(inode, inode->i_mode,
3928 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
3929 else
3930 init_special_inode(inode, inode->i_mode,
3931 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
Theodore Ts'o563bdd62009-03-26 00:06:19 -04003932 } else {
Theodore Ts'o563bdd62009-03-26 00:06:19 -04003933 ret = -EIO;
Theodore Ts'o24676da2010-05-16 21:00:00 -04003934 EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04003935 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003936 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003937 brelse(iloc.bh);
Mingming Cao617ba132006-10-11 01:20:53 -07003938 ext4_set_inode_flags(inode);
David Howells1d1fe1e2008-02-07 00:15:37 -08003939 unlock_new_inode(inode);
3940 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003941
3942bad_inode:
Theodore Ts'o567f3e92009-11-14 08:19:05 -05003943 brelse(iloc.bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08003944 iget_failed(inode);
3945 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003946}
3947
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003948static int ext4_inode_blocks_set(handle_t *handle,
3949 struct ext4_inode *raw_inode,
3950 struct ext4_inode_info *ei)
3951{
3952 struct inode *inode = &(ei->vfs_inode);
3953 u64 i_blocks = inode->i_blocks;
3954 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003955
3956 if (i_blocks <= ~0U) {
3957 /*
Anatol Pomozov4907cb72012-09-01 10:31:09 -07003958 * i_blocks can be represented in a 32 bit variable
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003959 * as multiple of 512 bytes
3960 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05003961 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003962 raw_inode->i_blocks_high = 0;
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04003963 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Theodore Ts'of287a1a2008-10-16 22:50:48 -04003964 return 0;
3965 }
3966 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
3967 return -EFBIG;
3968
3969 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003970 /*
3971 * i_blocks can be represented in a 48 bit variable
3972 * as multiple of 512 bytes
3973 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05003974 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003975 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04003976 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003977 } else {
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04003978 ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05003979 /* i_block is stored in file system block size */
3980 i_blocks = i_blocks >> (inode->i_blkbits - 9);
3981 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
3982 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003983 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04003984 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003985}
3986
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003987/*
3988 * Post the struct inode info into an on-disk inode location in the
3989 * buffer-cache. This gobbles the caller's reference to the
3990 * buffer_head in the inode location struct.
3991 *
3992 * The caller must have write access to iloc->bh.
3993 */
Mingming Cao617ba132006-10-11 01:20:53 -07003994static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003995 struct inode *inode,
Frank Mayhar830156c2009-09-29 10:07:47 -04003996 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003997{
Mingming Cao617ba132006-10-11 01:20:53 -07003998 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
3999 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004000 struct buffer_head *bh = iloc->bh;
4001 int err = 0, rc, block;
Jan Karab71fc072012-09-26 21:52:20 -04004002 int need_datasync = 0;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004003 uid_t i_uid;
4004 gid_t i_gid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004005
4006 /* For fields not not tracking in the in-memory inode,
4007 * initialise them to zero for new inodes. */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004008 if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
Mingming Cao617ba132006-10-11 01:20:53 -07004009 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004010
Jan Karaff9ddf72007-07-18 09:24:20 -04004011 ext4_get_inode_flags(ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004012 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004013 i_uid = i_uid_read(inode);
4014 i_gid = i_gid_read(inode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004015 if (!(test_opt(inode->i_sb, NO_UID32))) {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004016 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(i_uid));
4017 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004018/*
4019 * Fix up interoperability with old kernels. Otherwise, old inodes get
4020 * re-used with the upper 16 bits of the uid/gid intact
4021 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004022 if (!ei->i_dtime) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004023 raw_inode->i_uid_high =
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004024 cpu_to_le16(high_16_bits(i_uid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004025 raw_inode->i_gid_high =
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004026 cpu_to_le16(high_16_bits(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004027 } else {
4028 raw_inode->i_uid_high = 0;
4029 raw_inode->i_gid_high = 0;
4030 }
4031 } else {
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004032 raw_inode->i_uid_low = cpu_to_le16(fs_high2lowuid(i_uid));
4033 raw_inode->i_gid_low = cpu_to_le16(fs_high2lowgid(i_gid));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004034 raw_inode->i_uid_high = 0;
4035 raw_inode->i_gid_high = 0;
4036 }
4037 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04004038
4039 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
4040 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
4041 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
4042 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
4043
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05004044 if (ext4_inode_blocks_set(handle, raw_inode, ei))
4045 goto out_brelse;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004046 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Theodore Ts'o353eb832011-01-10 12:18:25 -05004047 raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
Mingming Cao9b8f1f02006-10-11 01:21:13 -07004048 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
4049 cpu_to_le32(EXT4_OS_HURD))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07004050 raw_inode->i_file_acl_high =
4051 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05004052 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Jan Karab71fc072012-09-26 21:52:20 -04004053 if (ei->i_disksize != ext4_isize(raw_inode)) {
4054 ext4_isize_set(raw_inode, ei->i_disksize);
4055 need_datasync = 1;
4056 }
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05004057 if (ei->i_disksize > 0x7fffffffULL) {
4058 struct super_block *sb = inode->i_sb;
4059 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
4060 EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
4061 EXT4_SB(sb)->s_es->s_rev_level ==
4062 cpu_to_le32(EXT4_GOOD_OLD_REV)) {
4063 /* If this is the first large file
4064 * created, add a flag to the superblock.
4065 */
4066 err = ext4_journal_get_write_access(handle,
4067 EXT4_SB(sb)->s_sbh);
4068 if (err)
4069 goto out_brelse;
4070 ext4_update_dynamic_rev(sb);
4071 EXT4_SET_RO_COMPAT_FEATURE(sb,
Mingming Cao617ba132006-10-11 01:20:53 -07004072 EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
Frank Mayhar03901312009-01-07 00:06:22 -05004073 ext4_handle_sync(handle);
Artem Bityutskiyb50924c2012-07-22 20:37:31 -04004074 err = ext4_handle_dirty_super(handle, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004075 }
4076 }
4077 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
4078 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
4079 if (old_valid_dev(inode->i_rdev)) {
4080 raw_inode->i_block[0] =
4081 cpu_to_le32(old_encode_dev(inode->i_rdev));
4082 raw_inode->i_block[1] = 0;
4083 } else {
4084 raw_inode->i_block[0] = 0;
4085 raw_inode->i_block[1] =
4086 cpu_to_le32(new_encode_dev(inode->i_rdev));
4087 raw_inode->i_block[2] = 0;
4088 }
Tao Maf19d5872012-12-10 14:05:51 -05004089 } else if (!ext4_has_inline_data(inode)) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004090 for (block = 0; block < EXT4_N_BLOCKS; block++)
4091 raw_inode->i_block[block] = ei->i_data[block];
Tao Maf19d5872012-12-10 14:05:51 -05004092 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004093
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004094 raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
4095 if (ei->i_extra_isize) {
4096 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
4097 raw_inode->i_version_hi =
4098 cpu_to_le32(inode->i_version >> 32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004099 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004100 }
4101
Darrick J. Wong814525f2012-04-29 18:31:10 -04004102 ext4_inode_csum_set(inode, raw_inode, ei);
4103
Frank Mayhar830156c2009-09-29 10:07:47 -04004104 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05004105 rc = ext4_handle_dirty_metadata(handle, NULL, bh);
Frank Mayhar830156c2009-09-29 10:07:47 -04004106 if (!err)
4107 err = rc;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004108 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004109
Jan Karab71fc072012-09-26 21:52:20 -04004110 ext4_update_inode_fsync_trans(handle, inode, need_datasync);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004111out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04004112 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07004113 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004114 return err;
4115}
4116
4117/*
Mingming Cao617ba132006-10-11 01:20:53 -07004118 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004119 *
4120 * We are called from a few places:
4121 *
4122 * - Within generic_file_write() for O_SYNC files.
4123 * Here, there will be no transaction running. We wait for any running
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004124 * transaction to commit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004125 *
4126 * - Within sys_sync(), kupdate and such.
4127 * We wait on commit, if tol to.
4128 *
4129 * - Within prune_icache() (PF_MEMALLOC == true)
4130 * Here we simply return. We can't afford to block kswapd on the
4131 * journal commit.
4132 *
4133 * In all cases it is actually safe for us to return without doing anything,
4134 * because the inode has been copied into a raw inode buffer in
Mingming Cao617ba132006-10-11 01:20:53 -07004135 * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004136 * knfsd.
4137 *
4138 * Note that we are absolutely dependent upon all inode dirtiers doing the
4139 * right thing: they *must* call mark_inode_dirty() after dirtying info in
4140 * which we are interested.
4141 *
4142 * It would be a bug for them to not do this. The code:
4143 *
4144 * mark_inode_dirty(inode)
4145 * stuff();
4146 * inode->i_size = expr;
4147 *
4148 * is in error because a kswapd-driven write_inode() could occur while
4149 * `stuff()' is running, and the new i_size will be lost. Plus the inode
4150 * will no longer be on the superblock's dirty inode list.
4151 */
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004152int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004153{
Frank Mayhar91ac6f42009-09-09 22:33:47 -04004154 int err;
4155
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004156 if (current->flags & PF_MEMALLOC)
4157 return 0;
4158
Frank Mayhar91ac6f42009-09-09 22:33:47 -04004159 if (EXT4_SB(inode->i_sb)->s_journal) {
4160 if (ext4_journal_current_handle()) {
4161 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
4162 dump_stack();
4163 return -EIO;
4164 }
4165
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004166 if (wbc->sync_mode != WB_SYNC_ALL)
Frank Mayhar91ac6f42009-09-09 22:33:47 -04004167 return 0;
4168
4169 err = ext4_force_commit(inode->i_sb);
4170 } else {
4171 struct ext4_iloc iloc;
4172
Curt Wohlgemuth8b472d72010-04-03 16:45:06 -04004173 err = __ext4_get_inode_loc(inode, &iloc, 0);
Frank Mayhar91ac6f42009-09-09 22:33:47 -04004174 if (err)
4175 return err;
Christoph Hellwiga9185b42010-03-05 09:21:37 +01004176 if (wbc->sync_mode == WB_SYNC_ALL)
Frank Mayhar830156c2009-09-29 10:07:47 -04004177 sync_dirty_buffer(iloc.bh);
4178 if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -04004179 EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr,
4180 "IO error syncing inode");
Frank Mayhar830156c2009-09-29 10:07:47 -04004181 err = -EIO;
4182 }
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04004183 brelse(iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004184 }
Frank Mayhar91ac6f42009-09-09 22:33:47 -04004185 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004186}
4187
4188/*
Jan Kara53e87262012-12-25 13:29:52 -05004189 * In data=journal mode ext4_journalled_invalidatepage() may fail to invalidate
4190 * buffers that are attached to a page stradding i_size and are undergoing
4191 * commit. In that case we have to wait for commit to finish and try again.
4192 */
4193static void ext4_wait_for_tail_page_commit(struct inode *inode)
4194{
4195 struct page *page;
4196 unsigned offset;
4197 journal_t *journal = EXT4_SB(inode->i_sb)->s_journal;
4198 tid_t commit_tid = 0;
4199 int ret;
4200
4201 offset = inode->i_size & (PAGE_CACHE_SIZE - 1);
4202 /*
4203 * All buffers in the last page remain valid? Then there's nothing to
4204 * do. We do the check mainly to optimize the common PAGE_CACHE_SIZE ==
4205 * blocksize case
4206 */
4207 if (offset > PAGE_CACHE_SIZE - (1 << inode->i_blkbits))
4208 return;
4209 while (1) {
4210 page = find_lock_page(inode->i_mapping,
4211 inode->i_size >> PAGE_CACHE_SHIFT);
4212 if (!page)
4213 return;
4214 ret = __ext4_journalled_invalidatepage(page, offset);
4215 unlock_page(page);
4216 page_cache_release(page);
4217 if (ret != -EBUSY)
4218 return;
4219 commit_tid = 0;
4220 read_lock(&journal->j_state_lock);
4221 if (journal->j_committing_transaction)
4222 commit_tid = journal->j_committing_transaction->t_tid;
4223 read_unlock(&journal->j_state_lock);
4224 if (commit_tid)
4225 jbd2_log_wait_commit(journal, commit_tid);
4226 }
4227}
4228
4229/*
Mingming Cao617ba132006-10-11 01:20:53 -07004230 * ext4_setattr()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004231 *
4232 * Called from notify_change.
4233 *
4234 * We want to trap VFS attempts to truncate the file as soon as
4235 * possible. In particular, we want to make sure that when the VFS
4236 * shrinks i_size, we put the inode on the orphan list and modify
4237 * i_disksize immediately, so that during the subsequent flushing of
4238 * dirty pages and freeing of disk blocks, we can guarantee that any
4239 * commit will leave the blocks being flushed in an unused state on
4240 * disk. (On recovery, the inode will get truncated and the blocks will
4241 * be freed, so we have a strong guarantee that no future commit will
4242 * leave these blocks visible to the user.)
4243 *
Jan Kara678aaf42008-07-11 19:27:31 -04004244 * Another thing we have to assure is that if we are in ordered mode
4245 * and inode is still attached to the committing transaction, we must
4246 * we start writeout of all the dirty pages which are being truncated.
4247 * This way we are sure that all the data written in the previous
4248 * transaction are already on disk (truncate waits for pages under
4249 * writeback).
4250 *
4251 * Called with inode->i_mutex down.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004252 */
Mingming Cao617ba132006-10-11 01:20:53 -07004253int ext4_setattr(struct dentry *dentry, struct iattr *attr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004254{
4255 struct inode *inode = dentry->d_inode;
4256 int error, rc = 0;
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04004257 int orphan = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004258 const unsigned int ia_valid = attr->ia_valid;
4259
4260 error = inode_change_ok(inode, attr);
4261 if (error)
4262 return error;
4263
Dmitry Monakhov12755622010-04-08 22:04:20 +04004264 if (is_quota_modification(inode, attr))
Christoph Hellwig871a2932010-03-03 09:05:07 -05004265 dquot_initialize(inode);
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004266 if ((ia_valid & ATTR_UID && !uid_eq(attr->ia_uid, inode->i_uid)) ||
4267 (ia_valid & ATTR_GID && !gid_eq(attr->ia_gid, inode->i_gid))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004268 handle_t *handle;
4269
4270 /* (user+group)*(old+new) structure, inode write (sb,
4271 * inode block, ? - but truncate inode update has it) */
Theodore Ts'o9924a922013-02-08 21:59:22 -05004272 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
4273 (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb) +
4274 EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb)) + 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004275 if (IS_ERR(handle)) {
4276 error = PTR_ERR(handle);
4277 goto err_out;
4278 }
Christoph Hellwigb43fa822010-03-03 09:05:03 -05004279 error = dquot_transfer(inode, attr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004280 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07004281 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004282 return error;
4283 }
4284 /* Update corresponding info in inode so that everything is in
4285 * one transaction */
4286 if (attr->ia_valid & ATTR_UID)
4287 inode->i_uid = attr->ia_uid;
4288 if (attr->ia_valid & ATTR_GID)
4289 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07004290 error = ext4_mark_inode_dirty(handle, inode);
4291 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004292 }
4293
Eric Sandeene2b46572008-01-28 23:58:27 -05004294 if (attr->ia_valid & ATTR_SIZE) {
Christoph Hellwig562c72a2011-06-24 14:29:45 -04004295
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004296 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
Eric Sandeene2b46572008-01-28 23:58:27 -05004297 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4298
Theodore Ts'o0c095c72010-07-27 11:56:06 -04004299 if (attr->ia_size > sbi->s_bitmap_maxbytes)
4300 return -EFBIG;
Eric Sandeene2b46572008-01-28 23:58:27 -05004301 }
4302 }
4303
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004304 if (S_ISREG(inode->i_mode) &&
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05004305 attr->ia_valid & ATTR_SIZE &&
Theodore Ts'o072bd7e2011-05-23 15:13:02 -04004306 (attr->ia_size < inode->i_size)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004307 handle_t *handle;
4308
Theodore Ts'o9924a922013-02-08 21:59:22 -05004309 handle = ext4_journal_start(inode, EXT4_HT_INODE, 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004310 if (IS_ERR(handle)) {
4311 error = PTR_ERR(handle);
4312 goto err_out;
4313 }
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04004314 if (ext4_handle_valid(handle)) {
4315 error = ext4_orphan_add(handle, inode);
4316 orphan = 1;
4317 }
Mingming Cao617ba132006-10-11 01:20:53 -07004318 EXT4_I(inode)->i_disksize = attr->ia_size;
4319 rc = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004320 if (!error)
4321 error = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07004322 ext4_journal_stop(handle);
Jan Kara678aaf42008-07-11 19:27:31 -04004323
4324 if (ext4_should_order_data(inode)) {
4325 error = ext4_begin_ordered_truncate(inode,
4326 attr->ia_size);
4327 if (error) {
4328 /* Do as much error cleanup as possible */
Theodore Ts'o9924a922013-02-08 21:59:22 -05004329 handle = ext4_journal_start(inode,
4330 EXT4_HT_INODE, 3);
Jan Kara678aaf42008-07-11 19:27:31 -04004331 if (IS_ERR(handle)) {
4332 ext4_orphan_del(NULL, inode);
4333 goto err_out;
4334 }
4335 ext4_orphan_del(handle, inode);
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04004336 orphan = 0;
Jan Kara678aaf42008-07-11 19:27:31 -04004337 ext4_journal_stop(handle);
4338 goto err_out;
4339 }
4340 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004341 }
4342
Theodore Ts'o072bd7e2011-05-23 15:13:02 -04004343 if (attr->ia_valid & ATTR_SIZE) {
Jan Kara53e87262012-12-25 13:29:52 -05004344 if (attr->ia_size != inode->i_size) {
4345 loff_t oldsize = inode->i_size;
4346
4347 i_size_write(inode, attr->ia_size);
4348 /*
4349 * Blocks are going to be removed from the inode. Wait
4350 * for dio in flight. Temporarily disable
4351 * dioread_nolock to prevent livelock.
4352 */
Dmitry Monakhov1b650072012-09-29 00:56:15 -04004353 if (orphan) {
Jan Kara53e87262012-12-25 13:29:52 -05004354 if (!ext4_should_journal_data(inode)) {
4355 ext4_inode_block_unlocked_dio(inode);
4356 inode_dio_wait(inode);
4357 ext4_inode_resume_unlocked_dio(inode);
4358 } else
4359 ext4_wait_for_tail_page_commit(inode);
Dmitry Monakhov1b650072012-09-29 00:56:15 -04004360 }
Jan Kara53e87262012-12-25 13:29:52 -05004361 /*
4362 * Truncate pagecache after we've waited for commit
4363 * in data=journal mode to make pages freeable.
4364 */
4365 truncate_pagecache(inode, oldsize, inode->i_size);
Dmitry Monakhov1c9114f2012-09-29 00:55:23 -04004366 }
Lukas Czernerafcff5d2012-03-21 21:47:55 -04004367 ext4_truncate(inode);
Theodore Ts'o072bd7e2011-05-23 15:13:02 -04004368 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004369
Christoph Hellwig10257742010-06-04 11:30:02 +02004370 if (!rc) {
4371 setattr_copy(inode, attr);
4372 mark_inode_dirty(inode);
4373 }
4374
4375 /*
4376 * If the call to ext4_truncate failed to get a transaction handle at
4377 * all, we need to clean up the in-core orphan list manually.
4378 */
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04004379 if (orphan && inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07004380 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004381
4382 if (!rc && (ia_valid & ATTR_MODE))
Mingming Cao617ba132006-10-11 01:20:53 -07004383 rc = ext4_acl_chmod(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004384
4385err_out:
Mingming Cao617ba132006-10-11 01:20:53 -07004386 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004387 if (!error)
4388 error = rc;
4389 return error;
4390}
4391
Mingming Cao3e3398a2008-07-11 19:27:31 -04004392int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
4393 struct kstat *stat)
4394{
4395 struct inode *inode;
4396 unsigned long delalloc_blocks;
4397
4398 inode = dentry->d_inode;
4399 generic_fillattr(inode, stat);
4400
4401 /*
4402 * We can't update i_blocks if the block allocation is delayed
4403 * otherwise in the case of system crash before the real block
4404 * allocation is done, we will have i_blocks inconsistent with
4405 * on-disk file blocks.
4406 * We always keep i_blocks updated together with real
4407 * allocation. But to not confuse with user, stat
4408 * will return the blocks that include the delayed allocation
4409 * blocks for this file.
4410 */
Tao Ma96607552012-05-31 22:54:16 -04004411 delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
4412 EXT4_I(inode)->i_reserved_data_blocks);
Mingming Cao3e3398a2008-07-11 19:27:31 -04004413
4414 stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
4415 return 0;
4416}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004417
Mingming Caoa02908f2008-08-19 22:16:07 -04004418static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4419{
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004420 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Amir Goldstein8bb2b242011-06-27 17:10:28 -04004421 return ext4_ind_trans_blocks(inode, nrblocks, chunk);
Theodore Ts'oac51d832008-11-06 16:49:36 -05004422 return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
Mingming Caoa02908f2008-08-19 22:16:07 -04004423}
Theodore Ts'oac51d832008-11-06 16:49:36 -05004424
Mingming Caoa02908f2008-08-19 22:16:07 -04004425/*
4426 * Account for index blocks, block groups bitmaps and block group
4427 * descriptor blocks if modify datablocks and index blocks
4428 * worse case, the indexs blocks spread over different block groups
4429 *
4430 * If datablocks are discontiguous, they are possible to spread over
Anatol Pomozov4907cb72012-09-01 10:31:09 -07004431 * different block groups too. If they are contiguous, with flexbg,
Mingming Caoa02908f2008-08-19 22:16:07 -04004432 * they could still across block group boundary.
4433 *
4434 * Also account for superblock, inode, quota and xattr blocks
4435 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04004436static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoa02908f2008-08-19 22:16:07 -04004437{
Theodore Ts'o8df96752009-05-01 08:50:38 -04004438 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
4439 int gdpblocks;
Mingming Caoa02908f2008-08-19 22:16:07 -04004440 int idxblocks;
4441 int ret = 0;
4442
4443 /*
4444 * How many index blocks need to touch to modify nrblocks?
4445 * The "Chunk" flag indicating whether the nrblocks is
4446 * physically contiguous on disk
4447 *
4448 * For Direct IO and fallocate, they calls get_block to allocate
4449 * one single extent at a time, so they could set the "Chunk" flag
4450 */
4451 idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
4452
4453 ret = idxblocks;
4454
4455 /*
4456 * Now let's see how many group bitmaps and group descriptors need
4457 * to account
4458 */
4459 groups = idxblocks;
4460 if (chunk)
4461 groups += 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004462 else
Mingming Caoa02908f2008-08-19 22:16:07 -04004463 groups += nrblocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004464
Mingming Caoa02908f2008-08-19 22:16:07 -04004465 gdpblocks = groups;
Theodore Ts'o8df96752009-05-01 08:50:38 -04004466 if (groups > ngroups)
4467 groups = ngroups;
Mingming Caoa02908f2008-08-19 22:16:07 -04004468 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
4469 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
4470
4471 /* bitmaps and block group descriptor blocks */
4472 ret += groups + gdpblocks;
4473
4474 /* Blocks for super block, inode, quota and xattr blocks */
4475 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004476
4477 return ret;
4478}
4479
4480/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004481 * Calculate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04004482 * the modification of a single pages into a single transaction,
4483 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04004484 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04004485 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04004486 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04004487 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04004488 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04004489 */
4490int ext4_writepage_trans_blocks(struct inode *inode)
4491{
4492 int bpp = ext4_journal_blocks_per_page(inode);
4493 int ret;
4494
4495 ret = ext4_meta_trans_blocks(inode, bpp, 0);
4496
4497 /* Account for data blocks for journalled mode */
4498 if (ext4_should_journal_data(inode))
4499 ret += bpp;
4500 return ret;
4501}
Mingming Caof3bd1f32008-08-19 22:16:03 -04004502
4503/*
4504 * Calculate the journal credits for a chunk of data modification.
4505 *
4506 * This is called from DIO, fallocate or whoever calling
Eric Sandeen79e83032010-07-27 11:56:07 -04004507 * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
Mingming Caof3bd1f32008-08-19 22:16:03 -04004508 *
4509 * journal buffers for data blocks are not included here, as DIO
4510 * and fallocate do no need to journal data buffers.
4511 */
4512int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
4513{
4514 return ext4_meta_trans_blocks(inode, nrblocks, 1);
4515}
4516
Mingming Caoa02908f2008-08-19 22:16:07 -04004517/*
Mingming Cao617ba132006-10-11 01:20:53 -07004518 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004519 * Give this, we know that the caller already has write access to iloc->bh.
4520 */
Mingming Cao617ba132006-10-11 01:20:53 -07004521int ext4_mark_iloc_dirty(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004522 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004523{
4524 int err = 0;
4525
Theodore Ts'oc64db502012-03-02 12:23:11 -05004526 if (IS_I_VERSION(inode))
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004527 inode_inc_iversion(inode);
4528
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004529 /* the do_update_inode consumes one bh->b_count */
4530 get_bh(iloc->bh);
4531
Mingming Caodab291a2006-10-11 01:21:01 -07004532 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Frank Mayhar830156c2009-09-29 10:07:47 -04004533 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004534 put_bh(iloc->bh);
4535 return err;
4536}
4537
4538/*
4539 * On success, We end up with an outstanding reference count against
4540 * iloc->bh. This _must_ be cleaned up later.
4541 */
4542
4543int
Mingming Cao617ba132006-10-11 01:20:53 -07004544ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
4545 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004546{
Frank Mayhar03901312009-01-07 00:06:22 -05004547 int err;
4548
4549 err = ext4_get_inode_loc(inode, iloc);
4550 if (!err) {
4551 BUFFER_TRACE(iloc->bh, "get_write_access");
4552 err = ext4_journal_get_write_access(handle, iloc->bh);
4553 if (err) {
4554 brelse(iloc->bh);
4555 iloc->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004556 }
4557 }
Mingming Cao617ba132006-10-11 01:20:53 -07004558 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004559 return err;
4560}
4561
4562/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004563 * Expand an inode by new_extra_isize bytes.
4564 * Returns 0 on success or negative error number on failure.
4565 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05004566static int ext4_expand_extra_isize(struct inode *inode,
4567 unsigned int new_extra_isize,
4568 struct ext4_iloc iloc,
4569 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004570{
4571 struct ext4_inode *raw_inode;
4572 struct ext4_xattr_ibody_header *header;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004573
4574 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
4575 return 0;
4576
4577 raw_inode = ext4_raw_inode(&iloc);
4578
4579 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004580
4581 /* No extended attributes present */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004582 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
4583 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004584 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
4585 new_extra_isize);
4586 EXT4_I(inode)->i_extra_isize = new_extra_isize;
4587 return 0;
4588 }
4589
4590 /* try to expand with EAs present */
4591 return ext4_expand_extra_isize_ea(inode, new_extra_isize,
4592 raw_inode, handle);
4593}
4594
4595/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004596 * What we do here is to mark the in-core inode as clean with respect to inode
4597 * dirtiness (it may still be data-dirty).
4598 * This means that the in-core inode may be reaped by prune_icache
4599 * without having to perform any I/O. This is a very good thing,
4600 * because *any* task may call prune_icache - even ones which
4601 * have a transaction open against a different journal.
4602 *
4603 * Is this cheating? Not really. Sure, we haven't written the
4604 * inode out, but prune_icache isn't a user-visible syncing function.
4605 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
4606 * we start and wait on commits.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004607 */
Mingming Cao617ba132006-10-11 01:20:53 -07004608int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004609{
Mingming Cao617ba132006-10-11 01:20:53 -07004610 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004611 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4612 static unsigned int mnt_count;
4613 int err, ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004614
4615 might_sleep();
Theodore Ts'o7ff9c072010-11-08 13:51:33 -05004616 trace_ext4_mark_inode_dirty(inode, _RET_IP_);
Mingming Cao617ba132006-10-11 01:20:53 -07004617 err = ext4_reserve_inode_write(handle, inode, &iloc);
Frank Mayhar03901312009-01-07 00:06:22 -05004618 if (ext4_handle_valid(handle) &&
4619 EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004620 !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004621 /*
4622 * We need extra buffer credits since we may write into EA block
4623 * with this same handle. If journal_extend fails, then it will
4624 * only result in a minor loss of functionality for that inode.
4625 * If this is felt to be critical, then e2fsck should be run to
4626 * force a large enough s_min_extra_isize.
4627 */
4628 if ((jbd2_journal_extend(handle,
4629 EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
4630 ret = ext4_expand_extra_isize(inode,
4631 sbi->s_want_extra_isize,
4632 iloc, handle);
4633 if (ret) {
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004634 ext4_set_inode_state(inode,
4635 EXT4_STATE_NO_EXPAND);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04004636 if (mnt_count !=
4637 le16_to_cpu(sbi->s_es->s_mnt_count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004638 ext4_warning(inode->i_sb,
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004639 "Unable to expand inode %lu. Delete"
4640 " some EAs or run e2fsck.",
4641 inode->i_ino);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04004642 mnt_count =
4643 le16_to_cpu(sbi->s_es->s_mnt_count);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004644 }
4645 }
4646 }
4647 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004648 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07004649 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004650 return err;
4651}
4652
4653/*
Mingming Cao617ba132006-10-11 01:20:53 -07004654 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004655 *
4656 * We're really interested in the case where a file is being extended.
4657 * i_size has been changed by generic_commit_write() and we thus need
4658 * to include the updated inode in the current transaction.
4659 *
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004660 * Also, dquot_alloc_block() will always dirty the inode when blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004661 * are allocated to the file.
4662 *
4663 * If the inode is marked synchronous, we don't honour that here - doing
4664 * so would cause a commit on atime updates, which we don't bother doing.
4665 * We handle synchronous inodes at the highest possible level.
4666 */
Christoph Hellwigaa385722011-05-27 06:53:02 -04004667void ext4_dirty_inode(struct inode *inode, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004668{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004669 handle_t *handle;
4670
Theodore Ts'o9924a922013-02-08 21:59:22 -05004671 handle = ext4_journal_start(inode, EXT4_HT_INODE, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004672 if (IS_ERR(handle))
4673 goto out;
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04004674
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04004675 ext4_mark_inode_dirty(handle, inode);
4676
Mingming Cao617ba132006-10-11 01:20:53 -07004677 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004678out:
4679 return;
4680}
4681
4682#if 0
4683/*
4684 * Bind an inode's backing buffer_head into this transaction, to prevent
4685 * it from being flushed to disk early. Unlike
Mingming Cao617ba132006-10-11 01:20:53 -07004686 * ext4_reserve_inode_write, this leaves behind no bh reference and
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004687 * returns no iloc structure, so the caller needs to repeat the iloc
4688 * lookup to mark the inode dirty later.
4689 */
Mingming Cao617ba132006-10-11 01:20:53 -07004690static int ext4_pin_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004691{
Mingming Cao617ba132006-10-11 01:20:53 -07004692 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004693
4694 int err = 0;
4695 if (handle) {
Mingming Cao617ba132006-10-11 01:20:53 -07004696 err = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004697 if (!err) {
4698 BUFFER_TRACE(iloc.bh, "get_write_access");
Mingming Caodab291a2006-10-11 01:21:01 -07004699 err = jbd2_journal_get_write_access(handle, iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004700 if (!err)
Frank Mayhar03901312009-01-07 00:06:22 -05004701 err = ext4_handle_dirty_metadata(handle,
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05004702 NULL,
Frank Mayhar03901312009-01-07 00:06:22 -05004703 iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004704 brelse(iloc.bh);
4705 }
4706 }
Mingming Cao617ba132006-10-11 01:20:53 -07004707 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004708 return err;
4709}
4710#endif
4711
Mingming Cao617ba132006-10-11 01:20:53 -07004712int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004713{
4714 journal_t *journal;
4715 handle_t *handle;
4716 int err;
4717
4718 /*
4719 * We have to be very careful here: changing a data block's
4720 * journaling status dynamically is dangerous. If we write a
4721 * data block to the journal, change the status and then delete
4722 * that block, we risk forgetting to revoke the old log record
4723 * from the journal and so a subsequent replay can corrupt data.
4724 * So, first we make sure that the journal is empty and that
4725 * nobody is changing anything.
4726 */
4727
Mingming Cao617ba132006-10-11 01:20:53 -07004728 journal = EXT4_JOURNAL(inode);
Frank Mayhar03901312009-01-07 00:06:22 -05004729 if (!journal)
4730 return 0;
Dave Hansend6995942007-07-18 08:33:51 -04004731 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004732 return -EROFS;
Yongqiang Yang2aff57b2011-12-28 12:02:13 -05004733 /* We have to allocate physical blocks for delalloc blocks
4734 * before flushing journal. otherwise delalloc blocks can not
4735 * be allocated any more. even more truncate on delalloc blocks
4736 * could trigger BUG by flushing delalloc blocks in journal.
4737 * There is no delalloc block in non-journal data mode.
4738 */
4739 if (val && test_opt(inode->i_sb, DELALLOC)) {
4740 err = ext4_alloc_da_blocks(inode);
4741 if (err < 0)
4742 return err;
4743 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004744
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04004745 /* Wait for all existing dio workers */
4746 ext4_inode_block_unlocked_dio(inode);
4747 inode_dio_wait(inode);
4748
Mingming Caodab291a2006-10-11 01:21:01 -07004749 jbd2_journal_lock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004750
4751 /*
4752 * OK, there are no updates running now, and all cached data is
4753 * synced to disk. We are now in a completely consistent state
4754 * which doesn't have anything in the journal, and we know that
4755 * no filesystem updates are running, so it is safe to modify
4756 * the inode's in-core data-journaling state flag now.
4757 */
4758
4759 if (val)
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004760 ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Yongqiang Yang5872dda2011-12-28 13:55:51 -05004761 else {
4762 jbd2_journal_flush(journal);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004763 ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Yongqiang Yang5872dda2011-12-28 13:55:51 -05004764 }
Mingming Cao617ba132006-10-11 01:20:53 -07004765 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004766
Mingming Caodab291a2006-10-11 01:21:01 -07004767 jbd2_journal_unlock_updates(journal);
Dmitry Monakhov17335dc2012-09-29 00:41:21 -04004768 ext4_inode_resume_unlocked_dio(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004769
4770 /* Finally we can mark the inode as dirty. */
4771
Theodore Ts'o9924a922013-02-08 21:59:22 -05004772 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004773 if (IS_ERR(handle))
4774 return PTR_ERR(handle);
4775
Mingming Cao617ba132006-10-11 01:20:53 -07004776 err = ext4_mark_inode_dirty(handle, inode);
Frank Mayhar03901312009-01-07 00:06:22 -05004777 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07004778 ext4_journal_stop(handle);
4779 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004780
4781 return err;
4782}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004783
4784static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
4785{
4786 return !buffer_mapped(bh);
4787}
4788
Nick Pigginc2ec1752009-03-31 15:23:21 -07004789int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004790{
Nick Pigginc2ec1752009-03-31 15:23:21 -07004791 struct page *page = vmf->page;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004792 loff_t size;
4793 unsigned long len;
Jan Kara9ea7df52011-06-24 14:29:41 -04004794 int ret;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004795 struct file *file = vma->vm_file;
4796 struct inode *inode = file->f_path.dentry->d_inode;
4797 struct address_space *mapping = inode->i_mapping;
Jan Kara9ea7df52011-06-24 14:29:41 -04004798 handle_t *handle;
4799 get_block_t *get_block;
4800 int retries = 0;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004801
Jan Kara8e8ad8a2012-06-12 16:20:38 +02004802 sb_start_pagefault(inode->i_sb);
Theodore Ts'o041bbb6d2012-09-30 23:04:56 -04004803 file_update_time(vma->vm_file);
Jan Kara9ea7df52011-06-24 14:29:41 -04004804 /* Delalloc case is easy... */
4805 if (test_opt(inode->i_sb, DELALLOC) &&
4806 !ext4_should_journal_data(inode) &&
4807 !ext4_nonda_switch(inode->i_sb)) {
4808 do {
4809 ret = __block_page_mkwrite(vma, vmf,
4810 ext4_da_get_block_prep);
4811 } while (ret == -ENOSPC &&
4812 ext4_should_retry_alloc(inode->i_sb, &retries));
4813 goto out_ret;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004814 }
Darrick J. Wong0e499892011-05-18 13:55:20 -04004815
4816 lock_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04004817 size = i_size_read(inode);
4818 /* Page got truncated from under us? */
4819 if (page->mapping != mapping || page_offset(page) > size) {
4820 unlock_page(page);
4821 ret = VM_FAULT_NOPAGE;
4822 goto out;
Darrick J. Wong0e499892011-05-18 13:55:20 -04004823 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004824
4825 if (page->index == size >> PAGE_CACHE_SHIFT)
4826 len = size & ~PAGE_CACHE_MASK;
4827 else
4828 len = PAGE_CACHE_SIZE;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04004829 /*
Jan Kara9ea7df52011-06-24 14:29:41 -04004830 * Return if we have all the buffers mapped. This avoids the need to do
4831 * journal_start/journal_stop which can block and take a long time
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04004832 */
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004833 if (page_has_buffers(page)) {
Tao Maf19d5872012-12-10 14:05:51 -05004834 if (!ext4_walk_page_buffers(NULL, page_buffers(page),
4835 0, len, NULL,
4836 ext4_bh_unmapped)) {
Jan Kara9ea7df52011-06-24 14:29:41 -04004837 /* Wait so that we don't change page under IO */
4838 wait_on_page_writeback(page);
4839 ret = VM_FAULT_LOCKED;
4840 goto out;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04004841 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004842 }
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04004843 unlock_page(page);
Jan Kara9ea7df52011-06-24 14:29:41 -04004844 /* OK, we need to fill the hole... */
4845 if (ext4_should_dioread_nolock(inode))
4846 get_block = ext4_get_block_write;
4847 else
4848 get_block = ext4_get_block;
4849retry_alloc:
Theodore Ts'o9924a922013-02-08 21:59:22 -05004850 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE,
4851 ext4_writepage_trans_blocks(inode));
Jan Kara9ea7df52011-06-24 14:29:41 -04004852 if (IS_ERR(handle)) {
Nick Pigginc2ec1752009-03-31 15:23:21 -07004853 ret = VM_FAULT_SIGBUS;
Jan Kara9ea7df52011-06-24 14:29:41 -04004854 goto out;
4855 }
4856 ret = __block_page_mkwrite(vma, vmf, get_block);
4857 if (!ret && ext4_should_journal_data(inode)) {
Tao Maf19d5872012-12-10 14:05:51 -05004858 if (ext4_walk_page_buffers(handle, page_buffers(page), 0,
Jan Kara9ea7df52011-06-24 14:29:41 -04004859 PAGE_CACHE_SIZE, NULL, do_journal_get_write_access)) {
4860 unlock_page(page);
4861 ret = VM_FAULT_SIGBUS;
Yongqiang Yangfcbb5512011-10-26 05:00:19 -04004862 ext4_journal_stop(handle);
Jan Kara9ea7df52011-06-24 14:29:41 -04004863 goto out;
4864 }
4865 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
4866 }
4867 ext4_journal_stop(handle);
4868 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
4869 goto retry_alloc;
4870out_ret:
4871 ret = block_page_mkwrite_return(ret);
4872out:
Jan Kara8e8ad8a2012-06-12 16:20:38 +02004873 sb_end_pagefault(inode->i_sb);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004874 return ret;
4875}