blob: de50b16a8f674f479e880d03add25ebc07b9124b [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
21#include <linux/module.h>
22#include <linux/fs.h>
23#include <linux/time.h>
Mingming Caodab291a2006-10-11 01:21:01 -070024#include <linux/jbd2.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070025#include <linux/highuid.h>
26#include <linux/pagemap.h>
27#include <linux/quotaops.h>
28#include <linux/string.h>
29#include <linux/buffer_head.h>
30#include <linux/writeback.h>
Alex Tomas64769242008-07-11 19:27:31 -040031#include <linux/pagevec.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070032#include <linux/mpage.h>
Duane Griffine83c1392008-12-19 20:47:15 +000033#include <linux/namei.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070034#include <linux/uio.h>
35#include <linux/bio.h>
Mingming Cao4c0425f2009-09-28 15:48:41 -040036#include <linux/workqueue.h>
Jiaying Zhang744692d2010-03-04 16:14:02 -050037#include <linux/kernel.h>
Andrew Morton6db26ff2011-01-12 16:59:13 -080038#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Theodore Ts'oa8901d32010-12-17 10:40:47 -050040#include <linux/ratelimit.h>
Theodore Ts'o9bffad12009-06-17 11:48:11 -040041
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040042#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070043#include "xattr.h"
44#include "acl.h"
Mingming Caod2a17632008-07-14 17:52:37 -040045#include "ext4_extents.h"
Theodore Ts'o9f125d62011-06-27 19:16:04 -040046#include "truncate.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070047
Theodore Ts'o9bffad12009-06-17 11:48:11 -040048#include <trace/events/ext4.h>
49
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -040050#define MPAGE_DA_EXTENT_TAIL 0x01
51
Jan Kara678aaf42008-07-11 19:27:31 -040052static inline int ext4_begin_ordered_truncate(struct inode *inode,
53 loff_t new_size)
54{
Theodore Ts'o7ff9c072010-11-08 13:51:33 -050055 trace_ext4_begin_ordered_truncate(inode, new_size);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -050056 /*
57 * If jinode is zero, then we never opened the file for
58 * writing, so there's no need to call
59 * jbd2_journal_begin_ordered_truncate() since there's no
60 * outstanding writes we need to flush.
61 */
62 if (!EXT4_I(inode)->jinode)
63 return 0;
64 return jbd2_journal_begin_ordered_truncate(EXT4_JOURNAL(inode),
65 EXT4_I(inode)->jinode,
66 new_size);
Jan Kara678aaf42008-07-11 19:27:31 -040067}
68
Alex Tomas64769242008-07-11 19:27:31 -040069static void ext4_invalidatepage(struct page *page, unsigned long offset);
Theodore Ts'ocb20d512010-10-27 21:30:09 -040070static int noalloc_get_block_write(struct inode *inode, sector_t iblock,
71 struct buffer_head *bh_result, int create);
72static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode);
73static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate);
74static int __ext4_journalled_writepage(struct page *page, unsigned int len);
75static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
Alex Tomas64769242008-07-11 19:27:31 -040076
Dave Kleikampac27a0e2006-10-11 01:20:50 -070077/*
78 * Test whether an inode is a fast symlink.
79 */
Mingming Cao617ba132006-10-11 01:20:53 -070080static int ext4_inode_is_fast_symlink(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070081{
Mingming Cao617ba132006-10-11 01:20:53 -070082 int ea_blocks = EXT4_I(inode)->i_file_acl ?
Dave Kleikampac27a0e2006-10-11 01:20:50 -070083 (inode->i_sb->s_blocksize >> 9) : 0;
84
85 return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
86}
87
88/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -070089 * Restart the transaction associated with *handle. This does a commit,
90 * so before we call here everything must be consistently dirtied against
91 * this transaction.
92 */
Aneesh Kumar K.Vfa5d1112009-11-02 18:50:49 -050093int ext4_truncate_restart_trans(handle_t *handle, struct inode *inode,
Jan Kara487caee2009-08-17 22:17:20 -040094 int nblocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -070095{
Jan Kara487caee2009-08-17 22:17:20 -040096 int ret;
97
98 /*
Theodore Ts'oe35fd662010-05-16 19:00:00 -040099 * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this
Jan Kara487caee2009-08-17 22:17:20 -0400100 * moment, get_block can be called only for blocks inside i_size since
101 * page cache has been already dropped and writes are blocked by
102 * i_mutex. So we can safely drop the i_data_sem here.
103 */
Frank Mayhar03901312009-01-07 00:06:22 -0500104 BUG_ON(EXT4_JOURNAL(inode) == NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700105 jbd_debug(2, "restarting handle %p\n", handle);
Jan Kara487caee2009-08-17 22:17:20 -0400106 up_write(&EXT4_I(inode)->i_data_sem);
Amir Goldstein8e8eaab2011-02-27 23:32:12 -0500107 ret = ext4_journal_restart(handle, nblocks);
Jan Kara487caee2009-08-17 22:17:20 -0400108 down_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vfa5d1112009-11-02 18:50:49 -0500109 ext4_discard_preallocations(inode);
Jan Kara487caee2009-08-17 22:17:20 -0400110
111 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700112}
113
114/*
115 * Called at the last iput() if i_nlink is zero.
116 */
Al Viro0930fcc2010-06-07 13:16:22 -0400117void ext4_evict_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700118{
119 handle_t *handle;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400120 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700121
Theodore Ts'o7ff9c072010-11-08 13:51:33 -0500122 trace_ext4_evict_inode(inode);
Al Viro0930fcc2010-06-07 13:16:22 -0400123 if (inode->i_nlink) {
124 truncate_inode_pages(&inode->i_data, 0);
125 goto no_delete;
126 }
127
Christoph Hellwig907f4552010-03-03 09:05:06 -0500128 if (!is_bad_inode(inode))
Christoph Hellwig871a2932010-03-03 09:05:07 -0500129 dquot_initialize(inode);
Christoph Hellwig907f4552010-03-03 09:05:06 -0500130
Jan Kara678aaf42008-07-11 19:27:31 -0400131 if (ext4_should_order_data(inode))
132 ext4_begin_ordered_truncate(inode, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700133 truncate_inode_pages(&inode->i_data, 0);
134
135 if (is_bad_inode(inode))
136 goto no_delete;
137
Theodore Ts'o9f125d62011-06-27 19:16:04 -0400138 handle = ext4_journal_start(inode, ext4_blocks_for_truncate(inode)+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700139 if (IS_ERR(handle)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400140 ext4_std_error(inode->i_sb, PTR_ERR(handle));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700141 /*
142 * If we're going to skip the normal cleanup, we still need to
143 * make sure that the in-core orphan linked list is properly
144 * cleaned up.
145 */
Mingming Cao617ba132006-10-11 01:20:53 -0700146 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700147 goto no_delete;
148 }
149
150 if (IS_SYNC(inode))
Frank Mayhar03901312009-01-07 00:06:22 -0500151 ext4_handle_sync(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700152 inode->i_size = 0;
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400153 err = ext4_mark_inode_dirty(handle, inode);
154 if (err) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500155 ext4_warning(inode->i_sb,
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400156 "couldn't mark inode dirty (err %d)", err);
157 goto stop_handle;
158 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700159 if (inode->i_blocks)
Mingming Cao617ba132006-10-11 01:20:53 -0700160 ext4_truncate(inode);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400161
162 /*
163 * ext4_ext_truncate() doesn't reserve any slop when it
164 * restarts journal transactions; therefore there may not be
165 * enough credits left in the handle to remove the inode from
166 * the orphan list and set the dtime field.
167 */
Frank Mayhar03901312009-01-07 00:06:22 -0500168 if (!ext4_handle_has_enough_credits(handle, 3)) {
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400169 err = ext4_journal_extend(handle, 3);
170 if (err > 0)
171 err = ext4_journal_restart(handle, 3);
172 if (err != 0) {
Eric Sandeen12062dd2010-02-15 14:19:27 -0500173 ext4_warning(inode->i_sb,
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400174 "couldn't extend journal (err %d)", err);
175 stop_handle:
176 ext4_journal_stop(handle);
Theodore Ts'o45388212010-07-29 15:06:10 -0400177 ext4_orphan_del(NULL, inode);
Theodore Ts'obc965ab2008-08-02 21:10:38 -0400178 goto no_delete;
179 }
180 }
181
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700182 /*
Mingming Cao617ba132006-10-11 01:20:53 -0700183 * Kill off the orphan record which ext4_truncate created.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700184 * AKPM: I think this can be inside the above `if'.
Mingming Cao617ba132006-10-11 01:20:53 -0700185 * Note that ext4_orphan_del() has to be able to cope with the
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700186 * deletion of a non-existent orphan - this is because we don't
Mingming Cao617ba132006-10-11 01:20:53 -0700187 * know if ext4_truncate() actually created an orphan record.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700188 * (Well, we could do this if we need to, but heck - it works)
189 */
Mingming Cao617ba132006-10-11 01:20:53 -0700190 ext4_orphan_del(handle, inode);
191 EXT4_I(inode)->i_dtime = get_seconds();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700192
193 /*
194 * One subtle ordering requirement: if anything has gone wrong
195 * (transaction abort, IO errors, whatever), then we can still
196 * do these next steps (the fs will already have been marked as
197 * having errors), but we can't free the inode if the mark_dirty
198 * fails.
199 */
Mingming Cao617ba132006-10-11 01:20:53 -0700200 if (ext4_mark_inode_dirty(handle, inode))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700201 /* If that failed, just do the required in-core inode clear. */
Al Viro0930fcc2010-06-07 13:16:22 -0400202 ext4_clear_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700203 else
Mingming Cao617ba132006-10-11 01:20:53 -0700204 ext4_free_inode(handle, inode);
205 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700206 return;
207no_delete:
Al Viro0930fcc2010-06-07 13:16:22 -0400208 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700209}
210
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300211#ifdef CONFIG_QUOTA
212qsize_t *ext4_get_reserved_space(struct inode *inode)
Mingming Cao60e58e02009-01-22 18:13:05 +0100213{
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300214 return &EXT4_I(inode)->i_reserved_quota;
Mingming Cao60e58e02009-01-22 18:13:05 +0100215}
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300216#endif
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500217
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400218/*
219 * Calculate the number of metadata blocks need to reserve
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500220 * to allocate a block located at @lblock
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400221 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -0500222static int ext4_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400223{
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400224 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500225 return ext4_ext_calc_metadata_amount(inode, lblock);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400226
Amir Goldstein8bb2b242011-06-27 17:10:28 -0400227 return ext4_ind_calc_metadata_amount(inode, lblock);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400228}
229
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500230/*
231 * Called with i_data_sem down, which is important since we can call
232 * ext4_discard_preallocations() from here.
233 */
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500234void ext4_da_update_reserve_space(struct inode *inode,
235 int used, int quota_claim)
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400236{
237 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500238 struct ext4_inode_info *ei = EXT4_I(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400239
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500240 spin_lock(&ei->i_block_reservation_lock);
Theodore Ts'of8ec9d62010-01-01 01:00:21 -0500241 trace_ext4_da_update_reserve_space(inode, used);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500242 if (unlikely(used > ei->i_reserved_data_blocks)) {
243 ext4_msg(inode->i_sb, KERN_NOTICE, "%s: ino %lu, used %d "
244 "with only %d reserved data blocks\n",
245 __func__, inode->i_ino, used,
246 ei->i_reserved_data_blocks);
247 WARN_ON(1);
248 used = ei->i_reserved_data_blocks;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400249 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400250
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500251 /* Update per-inode reservations */
252 ei->i_reserved_data_blocks -= used;
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500253 ei->i_reserved_meta_blocks -= ei->i_allocated_meta_blocks;
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400254 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
255 used + ei->i_allocated_meta_blocks);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500256 ei->i_allocated_meta_blocks = 0;
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500257
258 if (ei->i_reserved_data_blocks == 0) {
259 /*
260 * We can release all of the reserved metadata blocks
261 * only when we have written all of the delayed
262 * allocation blocks.
263 */
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400264 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
265 ei->i_reserved_meta_blocks);
Theodore Ts'oee5f4d92010-01-01 02:36:15 -0500266 ei->i_reserved_meta_blocks = 0;
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500267 ei->i_da_metadata_calc_len = 0;
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500268 }
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400269 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +0100270
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400271 /* Update quota subsystem for data blocks */
272 if (quota_claim)
Christoph Hellwig5dd40562010-03-03 09:05:00 -0500273 dquot_claim_block(inode, used);
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400274 else {
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500275 /*
276 * We did fallocate with an offset that is already delayed
277 * allocated. So on delayed allocated writeback we should
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400278 * not re-claim the quota for fallocated blocks.
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500279 */
Eric Sandeen72b8ab92010-05-16 11:00:00 -0400280 dquot_release_reservation_block(inode, used);
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500281 }
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -0400282
283 /*
284 * If we have done all the pending block allocations and if
285 * there aren't any writers on the inode, we can discard the
286 * inode's preallocations.
287 */
Theodore Ts'o0637c6f2009-12-30 14:20:45 -0500288 if ((ei->i_reserved_data_blocks == 0) &&
289 (atomic_read(&inode->i_writecount) == 0))
Aneesh Kumar K.Vd6014302009-03-27 22:36:43 -0400290 ext4_discard_preallocations(inode);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -0400291}
292
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400293static int __check_block_validity(struct inode *inode, const char *func,
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400294 unsigned int line,
295 struct ext4_map_blocks *map)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400296{
Theodore Ts'o24676da2010-05-16 21:00:00 -0400297 if (!ext4_data_block_valid(EXT4_SB(inode->i_sb), map->m_pblk,
298 map->m_len)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400299 ext4_error_inode(inode, func, line, map->m_pblk,
300 "lblock %lu mapped to illegal pblock "
301 "(length %d)", (unsigned long) map->m_lblk,
302 map->m_len);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400303 return -EIO;
304 }
305 return 0;
306}
307
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400308#define check_block_validity(inode, map) \
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400309 __check_block_validity((inode), __func__, __LINE__, (map))
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400310
Mingming Caof5ab0d12008-02-25 15:29:55 -0500311/*
Theodore Ts'o1f945332009-09-30 22:57:41 -0400312 * Return the number of contiguous dirty pages in a given inode
313 * starting at page frame idx.
Theodore Ts'o55138e02009-09-29 13:31:31 -0400314 */
315static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx,
316 unsigned int max_pages)
317{
318 struct address_space *mapping = inode->i_mapping;
319 pgoff_t index;
320 struct pagevec pvec;
321 pgoff_t num = 0;
322 int i, nr_pages, done = 0;
323
324 if (max_pages == 0)
325 return 0;
326 pagevec_init(&pvec, 0);
327 while (!done) {
328 index = idx;
329 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
330 PAGECACHE_TAG_DIRTY,
331 (pgoff_t)PAGEVEC_SIZE);
332 if (nr_pages == 0)
333 break;
334 for (i = 0; i < nr_pages; i++) {
335 struct page *page = pvec.pages[i];
336 struct buffer_head *bh, *head;
337
338 lock_page(page);
339 if (unlikely(page->mapping != mapping) ||
340 !PageDirty(page) ||
341 PageWriteback(page) ||
342 page->index != idx) {
343 done = 1;
344 unlock_page(page);
345 break;
346 }
Theodore Ts'o1f945332009-09-30 22:57:41 -0400347 if (page_has_buffers(page)) {
348 bh = head = page_buffers(page);
349 do {
350 if (!buffer_delay(bh) &&
351 !buffer_unwritten(bh))
352 done = 1;
353 bh = bh->b_this_page;
354 } while (!done && (bh != head));
355 }
Theodore Ts'o55138e02009-09-29 13:31:31 -0400356 unlock_page(page);
357 if (done)
358 break;
359 idx++;
360 num++;
Eric Sandeen659c6002010-10-27 21:30:03 -0400361 if (num >= max_pages) {
362 done = 1;
Theodore Ts'o55138e02009-09-29 13:31:31 -0400363 break;
Eric Sandeen659c6002010-10-27 21:30:03 -0400364 }
Theodore Ts'o55138e02009-09-29 13:31:31 -0400365 }
366 pagevec_release(&pvec);
367 }
368 return num;
369}
370
371/*
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400372 * The ext4_map_blocks() function tries to look up the requested blocks,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400373 * and returns if the blocks are already mapped.
Mingming Caof5ab0d12008-02-25 15:29:55 -0500374 *
Mingming Caof5ab0d12008-02-25 15:29:55 -0500375 * Otherwise it takes the write lock of the i_data_sem and allocate blocks
376 * and store the allocated blocks in the result buffer head and mark it
377 * mapped.
378 *
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400379 * If file type is extents based, it will call ext4_ext_map_blocks(),
380 * Otherwise, call with ext4_ind_map_blocks() to handle indirect mapping
Mingming Caof5ab0d12008-02-25 15:29:55 -0500381 * based files
382 *
383 * On success, it returns the number of blocks being mapped or allocate.
384 * if create==0 and the blocks are pre-allocated and uninitialized block,
385 * the result buffer head is unmapped. If the create ==1, it will make sure
386 * the buffer head is mapped.
387 *
388 * It returns 0 if plain look up failed (blocks have not been allocated), in
389 * that casem, buffer head is unmapped
390 *
391 * It returns the error in case of allocation failure.
392 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400393int ext4_map_blocks(handle_t *handle, struct inode *inode,
394 struct ext4_map_blocks *map, int flags)
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500395{
396 int retval;
Mingming Caof5ab0d12008-02-25 15:29:55 -0500397
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400398 map->m_flags = 0;
399 ext_debug("ext4_map_blocks(): inode %lu, flag %d, max_blocks %u,"
400 "logical block %lu\n", inode->i_ino, flags, map->m_len,
401 (unsigned long) map->m_lblk);
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500402 /*
Theodore Ts'ob920c752009-05-14 00:54:29 -0400403 * Try to see if we can get the block without requesting a new
404 * file system block.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500405 */
406 down_read((&EXT4_I(inode)->i_data_sem));
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400407 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400408 retval = ext4_ext_map_blocks(handle, inode, map, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500409 } else {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400410 retval = ext4_ind_map_blocks(handle, inode, map, 0);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500411 }
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500412 up_read((&EXT4_I(inode)->i_data_sem));
Mingming Caof5ab0d12008-02-25 15:29:55 -0500413
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400414 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400415 int ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400416 if (ret != 0)
417 return ret;
418 }
419
Mingming Caof5ab0d12008-02-25 15:29:55 -0500420 /* If it is only a block(s) look up */
Theodore Ts'oc2177052009-05-14 00:58:52 -0400421 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500422 return retval;
423
424 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -0500425 * Returns if the blocks have already allocated
426 *
427 * Note that if blocks have been preallocated
428 * ext4_ext_get_block() returns th create = 0
429 * with buffer head unmapped.
430 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400431 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED)
Mingming Caof5ab0d12008-02-25 15:29:55 -0500432 return retval;
433
434 /*
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -0400435 * When we call get_blocks without the create flag, the
436 * BH_Unwritten flag could have gotten set if the blocks
437 * requested were part of a uninitialized extent. We need to
438 * clear this flag now that we are committed to convert all or
439 * part of the uninitialized extent to be an initialized
440 * extent. This is because we need to avoid the combination
441 * of BH_Unwritten and BH_Mapped flags being simultaneously
442 * set on the buffer_head.
443 */
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400444 map->m_flags &= ~EXT4_MAP_UNWRITTEN;
Aneesh Kumar K.V2a8964d2009-05-14 17:05:39 -0400445
446 /*
Mingming Caof5ab0d12008-02-25 15:29:55 -0500447 * New blocks allocate and/or writing to uninitialized extent
448 * will possibly result in updating i_data, so we take
449 * the write lock of i_data_sem, and call get_blocks()
450 * with create == 1 flag.
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500451 */
452 down_write((&EXT4_I(inode)->i_data_sem));
Mingming Caod2a17632008-07-14 17:52:37 -0400453
454 /*
455 * if the caller is from delayed allocation writeout path
456 * we have already reserved fs blocks for allocation
457 * let the underlying get_block() function know to
458 * avoid double accounting
459 */
Theodore Ts'oc2177052009-05-14 00:58:52 -0400460 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Theodore Ts'of2321092011-01-10 12:12:36 -0500461 ext4_set_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500462 /*
463 * We need to check for EXT4 here because migrate
464 * could have changed the inode type in between
465 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -0400466 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400467 retval = ext4_ext_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500468 } else {
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400469 retval = ext4_ind_map_blocks(handle, inode, map, flags);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400470
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400471 if (retval > 0 && map->m_flags & EXT4_MAP_NEW) {
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400472 /*
473 * We allocated new blocks which will result in
474 * i_data's format changing. Force the migrate
475 * to fail by clearing migrate flags
476 */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500477 ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE);
Aneesh Kumar K.V267e4db2008-04-29 08:11:12 -0400478 }
Mingming Caod2a17632008-07-14 17:52:37 -0400479
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500480 /*
481 * Update reserved blocks/metadata blocks after successful
482 * block allocation which had been deferred till now. We don't
483 * support fallocate for non extent files. So we can update
484 * reserve space here.
485 */
486 if ((retval > 0) &&
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -0500487 (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE))
Aneesh Kumar K.V5f634d02010-01-25 04:00:31 -0500488 ext4_da_update_reserve_space(inode, retval, 1);
489 }
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -0400490 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE)
Theodore Ts'of2321092011-01-10 12:12:36 -0500491 ext4_clear_inode_state(inode, EXT4_STATE_DELALLOC_RESERVED);
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -0400492
Aneesh Kumar K.V4df3d262008-01-28 23:58:29 -0500493 up_write((&EXT4_I(inode)->i_data_sem));
Theodore Ts'oe35fd662010-05-16 19:00:00 -0400494 if (retval > 0 && map->m_flags & EXT4_MAP_MAPPED) {
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400495 int ret = check_block_validity(inode, map);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -0400496 if (ret != 0)
497 return ret;
498 }
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500499 return retval;
500}
501
Mingming Caof3bd1f32008-08-19 22:16:03 -0400502/* Maximum number of blocks we map for direct IO at once. */
503#define DIO_MAX_BLOCKS 4096
504
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400505static int _ext4_get_block(struct inode *inode, sector_t iblock,
506 struct buffer_head *bh, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700507{
Dmitriy Monakhov3e4fdaf2007-02-10 01:46:35 -0800508 handle_t *handle = ext4_journal_current_handle();
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400509 struct ext4_map_blocks map;
Jan Kara7fb54092008-02-10 01:08:38 -0500510 int ret = 0, started = 0;
Mingming Caof3bd1f32008-08-19 22:16:03 -0400511 int dio_credits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700512
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400513 map.m_lblk = iblock;
514 map.m_len = bh->b_size >> inode->i_blkbits;
515
516 if (flags && !handle) {
Jan Kara7fb54092008-02-10 01:08:38 -0500517 /* Direct IO write... */
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400518 if (map.m_len > DIO_MAX_BLOCKS)
519 map.m_len = DIO_MAX_BLOCKS;
520 dio_credits = ext4_chunk_trans_blocks(inode, map.m_len);
Mingming Caof3bd1f32008-08-19 22:16:03 -0400521 handle = ext4_journal_start(inode, dio_credits);
Jan Kara7fb54092008-02-10 01:08:38 -0500522 if (IS_ERR(handle)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700523 ret = PTR_ERR(handle);
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400524 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700525 }
Jan Kara7fb54092008-02-10 01:08:38 -0500526 started = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700527 }
528
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400529 ret = ext4_map_blocks(handle, inode, &map, flags);
Jan Kara7fb54092008-02-10 01:08:38 -0500530 if (ret > 0) {
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400531 map_bh(bh, inode->i_sb, map.m_pblk);
532 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
533 bh->b_size = inode->i_sb->s_blocksize * map.m_len;
Jan Kara7fb54092008-02-10 01:08:38 -0500534 ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700535 }
Jan Kara7fb54092008-02-10 01:08:38 -0500536 if (started)
537 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700538 return ret;
539}
540
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400541int ext4_get_block(struct inode *inode, sector_t iblock,
542 struct buffer_head *bh, int create)
543{
544 return _ext4_get_block(inode, iblock, bh,
545 create ? EXT4_GET_BLOCKS_CREATE : 0);
546}
547
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700548/*
549 * `handle' can be NULL if create is zero
550 */
Mingming Cao617ba132006-10-11 01:20:53 -0700551struct buffer_head *ext4_getblk(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500552 ext4_lblk_t block, int create, int *errp)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700553{
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400554 struct ext4_map_blocks map;
555 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700556 int fatal = 0, err;
557
558 J_ASSERT(handle != NULL || create == 0);
559
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400560 map.m_lblk = block;
561 map.m_len = 1;
562 err = ext4_map_blocks(handle, inode, &map,
563 create ? EXT4_GET_BLOCKS_CREATE : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700564
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400565 if (err < 0)
566 *errp = err;
567 if (err <= 0)
568 return NULL;
569 *errp = 0;
570
571 bh = sb_getblk(inode->i_sb, map.m_pblk);
572 if (!bh) {
573 *errp = -EIO;
574 return NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700575 }
Theodore Ts'o2ed88682010-05-16 20:00:00 -0400576 if (map.m_flags & EXT4_MAP_NEW) {
577 J_ASSERT(create != 0);
578 J_ASSERT(handle != NULL);
579
580 /*
581 * Now that we do not always journal data, we should
582 * keep in mind whether this should always journal the
583 * new buffer as metadata. For now, regular file
584 * writes use ext4_get_block instead, so it's not a
585 * problem.
586 */
587 lock_buffer(bh);
588 BUFFER_TRACE(bh, "call get_create_access");
589 fatal = ext4_journal_get_create_access(handle, bh);
590 if (!fatal && !buffer_uptodate(bh)) {
591 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
592 set_buffer_uptodate(bh);
593 }
594 unlock_buffer(bh);
595 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
596 err = ext4_handle_dirty_metadata(handle, inode, bh);
597 if (!fatal)
598 fatal = err;
599 } else {
600 BUFFER_TRACE(bh, "not a new buffer");
601 }
602 if (fatal) {
603 *errp = fatal;
604 brelse(bh);
605 bh = NULL;
606 }
607 return bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700608}
609
Mingming Cao617ba132006-10-11 01:20:53 -0700610struct buffer_head *ext4_bread(handle_t *handle, struct inode *inode,
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -0500611 ext4_lblk_t block, int create, int *err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700612{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400613 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700614
Mingming Cao617ba132006-10-11 01:20:53 -0700615 bh = ext4_getblk(handle, inode, block, create, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700616 if (!bh)
617 return bh;
618 if (buffer_uptodate(bh))
619 return bh;
620 ll_rw_block(READ_META, 1, &bh);
621 wait_on_buffer(bh);
622 if (buffer_uptodate(bh))
623 return bh;
624 put_bh(bh);
625 *err = -EIO;
626 return NULL;
627}
628
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400629static int walk_page_buffers(handle_t *handle,
630 struct buffer_head *head,
631 unsigned from,
632 unsigned to,
633 int *partial,
634 int (*fn)(handle_t *handle,
635 struct buffer_head *bh))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700636{
637 struct buffer_head *bh;
638 unsigned block_start, block_end;
639 unsigned blocksize = head->b_size;
640 int err, ret = 0;
641 struct buffer_head *next;
642
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400643 for (bh = head, block_start = 0;
644 ret == 0 && (bh != head || !block_start);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400645 block_start = block_end, bh = next) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700646 next = bh->b_this_page;
647 block_end = block_start + blocksize;
648 if (block_end <= from || block_start >= to) {
649 if (partial && !buffer_uptodate(bh))
650 *partial = 1;
651 continue;
652 }
653 err = (*fn)(handle, bh);
654 if (!ret)
655 ret = err;
656 }
657 return ret;
658}
659
660/*
661 * To preserve ordering, it is essential that the hole instantiation and
662 * the data write be encapsulated in a single transaction. We cannot
Mingming Cao617ba132006-10-11 01:20:53 -0700663 * close off a transaction and start a new one between the ext4_get_block()
Mingming Caodab291a2006-10-11 01:21:01 -0700664 * and the commit_write(). So doing the jbd2_journal_start at the start of
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700665 * prepare_write() is the right place.
666 *
Mingming Cao617ba132006-10-11 01:20:53 -0700667 * Also, this function can nest inside ext4_writepage() ->
668 * block_write_full_page(). In that case, we *know* that ext4_writepage()
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700669 * has generated enough buffer credits to do the whole page. So we won't
670 * block on the journal in that case, which is good, because the caller may
671 * be PF_MEMALLOC.
672 *
Mingming Cao617ba132006-10-11 01:20:53 -0700673 * By accident, ext4 can be reentered when a transaction is open via
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700674 * quota file writes. If we were to commit the transaction while thus
675 * reentered, there can be a deadlock - we would be holding a quota
676 * lock, and the commit would never complete if another thread had a
677 * transaction open and was blocking on the quota lock - a ranking
678 * violation.
679 *
Mingming Caodab291a2006-10-11 01:21:01 -0700680 * So what we do is to rely on the fact that jbd2_journal_stop/journal_start
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700681 * will _not_ run commit under these circumstances because handle->h_ref
682 * is elevated. We'll still have enough credits for the tiny quotafile
683 * write.
684 */
685static int do_journal_get_write_access(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400686 struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700687{
Jan Kara56d35a42010-08-05 14:41:42 -0400688 int dirty = buffer_dirty(bh);
689 int ret;
690
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700691 if (!buffer_mapped(bh) || buffer_freed(bh))
692 return 0;
Jan Kara56d35a42010-08-05 14:41:42 -0400693 /*
Christoph Hellwigebdec242010-10-06 10:47:23 +0200694 * __block_write_begin() could have dirtied some buffers. Clean
Jan Kara56d35a42010-08-05 14:41:42 -0400695 * the dirty bit as jbd2_journal_get_write_access() could complain
696 * otherwise about fs integrity issues. Setting of the dirty bit
Christoph Hellwigebdec242010-10-06 10:47:23 +0200697 * by __block_write_begin() isn't a real problem here as we clear
Jan Kara56d35a42010-08-05 14:41:42 -0400698 * the bit before releasing a page lock and thus writeback cannot
699 * ever write the buffer.
700 */
701 if (dirty)
702 clear_buffer_dirty(bh);
703 ret = ext4_journal_get_write_access(handle, bh);
704 if (!ret && dirty)
705 ret = ext4_handle_dirty_metadata(handle, NULL, bh);
706 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700707}
708
Jiaying Zhang744692d2010-03-04 16:14:02 -0500709static int ext4_get_block_write(struct inode *inode, sector_t iblock,
710 struct buffer_head *bh_result, int create);
Nick Pigginbfc1af62007-10-16 01:25:05 -0700711static int ext4_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400712 loff_t pos, unsigned len, unsigned flags,
713 struct page **pagep, void **fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700714{
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400715 struct inode *inode = mapping->host;
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -0400716 int ret, needed_blocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700717 handle_t *handle;
718 int retries = 0;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400719 struct page *page;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400720 pgoff_t index;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400721 unsigned from, to;
Nick Pigginbfc1af62007-10-16 01:25:05 -0700722
Theodore Ts'o9bffad12009-06-17 11:48:11 -0400723 trace_ext4_write_begin(inode, pos, len, flags);
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -0400724 /*
725 * Reserve one block more for addition to orphan list in case
726 * we allocate blocks but write fails for some reason
727 */
728 needed_blocks = ext4_writepage_trans_blocks(inode) + 1;
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400729 index = pos >> PAGE_CACHE_SHIFT;
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400730 from = pos & (PAGE_CACHE_SIZE - 1);
731 to = from + len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700732
733retry:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400734 handle = ext4_journal_start(inode, needed_blocks);
735 if (IS_ERR(handle)) {
736 ret = PTR_ERR(handle);
737 goto out;
Nick Pigginbfc1af62007-10-16 01:25:05 -0700738 }
739
Jan Karaebd36102009-02-22 21:09:59 -0500740 /* We cannot recurse into the filesystem as the transaction is already
741 * started */
742 flags |= AOP_FLAG_NOFS;
743
Nick Piggin54566b22009-01-04 12:00:53 -0800744 page = grab_cache_page_write_begin(mapping, index, flags);
Jan Karacf108bc2008-07-11 19:27:31 -0400745 if (!page) {
746 ext4_journal_stop(handle);
747 ret = -ENOMEM;
748 goto out;
749 }
750 *pagep = page;
751
Jiaying Zhang744692d2010-03-04 16:14:02 -0500752 if (ext4_should_dioread_nolock(inode))
Christoph Hellwig6e1db882010-06-04 11:29:57 +0200753 ret = __block_write_begin(page, pos, len, ext4_get_block_write);
Jiaying Zhang744692d2010-03-04 16:14:02 -0500754 else
Christoph Hellwig6e1db882010-06-04 11:29:57 +0200755 ret = __block_write_begin(page, pos, len, ext4_get_block);
Nick Pigginbfc1af62007-10-16 01:25:05 -0700756
757 if (!ret && ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700758 ret = walk_page_buffers(handle, page_buffers(page),
759 from, to, NULL, do_journal_get_write_access);
760 }
Nick Pigginbfc1af62007-10-16 01:25:05 -0700761
762 if (ret) {
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400763 unlock_page(page);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400764 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -0400765 /*
Christoph Hellwig6e1db882010-06-04 11:29:57 +0200766 * __block_write_begin may have instantiated a few blocks
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -0400767 * outside i_size. Trim these off again. Don't need
768 * i_size_read because we hold i_mutex.
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -0400769 *
770 * Add inode to orphan list in case we crash before
771 * truncate finishes
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -0400772 */
Jan Karaffacfa72009-07-13 16:22:22 -0400773 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -0400774 ext4_orphan_add(handle, inode);
775
776 ext4_journal_stop(handle);
777 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -0500778 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400779 /*
Jan Karaffacfa72009-07-13 16:22:22 -0400780 * If truncate failed early the inode might
Aneesh Kumar K.V1938a152009-06-05 01:00:26 -0400781 * still be on the orphan list; we need to
782 * make sure the inode is removed from the
783 * orphan list in that case.
784 */
785 if (inode->i_nlink)
786 ext4_orphan_del(NULL, inode);
787 }
Nick Pigginbfc1af62007-10-16 01:25:05 -0700788 }
789
Mingming Cao617ba132006-10-11 01:20:53 -0700790 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700791 goto retry;
Andrew Morton7479d2b2007-04-01 23:49:44 -0700792out:
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700793 return ret;
794}
795
Nick Pigginbfc1af62007-10-16 01:25:05 -0700796/* For write_end() in data=journal mode */
797static int write_end_fn(handle_t *handle, struct buffer_head *bh)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700798{
799 if (!buffer_mapped(bh) || buffer_freed(bh))
800 return 0;
801 set_buffer_uptodate(bh);
Frank Mayhar03901312009-01-07 00:06:22 -0500802 return ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700803}
804
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -0400805static int ext4_generic_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400806 struct address_space *mapping,
807 loff_t pos, unsigned len, unsigned copied,
808 struct page *page, void *fsdata)
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -0400809{
810 int i_size_changed = 0;
811 struct inode *inode = mapping->host;
812 handle_t *handle = ext4_journal_current_handle();
813
814 copied = block_write_end(file, mapping, pos, len, copied, page, fsdata);
815
816 /*
817 * No need to use i_size_read() here, the i_size
818 * cannot change under us because we hold i_mutex.
819 *
820 * But it's important to update i_size while still holding page lock:
821 * page writeout could otherwise come in and zero beyond i_size.
822 */
823 if (pos + copied > inode->i_size) {
824 i_size_write(inode, pos + copied);
825 i_size_changed = 1;
826 }
827
828 if (pos + copied > EXT4_I(inode)->i_disksize) {
829 /* We need to mark inode dirty even if
830 * new_i_size is less that inode->i_size
831 * bu greater than i_disksize.(hint delalloc)
832 */
833 ext4_update_i_disksize(inode, (pos + copied));
834 i_size_changed = 1;
835 }
836 unlock_page(page);
837 page_cache_release(page);
838
839 /*
840 * Don't mark the inode dirty under page lock. First, it unnecessarily
841 * makes the holding time of page lock longer. Second, it forces lock
842 * ordering of page lock and transaction start for journaling
843 * filesystems.
844 */
845 if (i_size_changed)
846 ext4_mark_inode_dirty(handle, inode);
847
848 return copied;
849}
850
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700851/*
852 * We need to pick up the new inode size which generic_commit_write gave us
853 * `file' can be NULL - eg, when called from page_symlink().
854 *
Mingming Cao617ba132006-10-11 01:20:53 -0700855 * ext4 never places buffers on inode->i_mapping->private_list. metadata
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700856 * buffers are managed internally.
857 */
Nick Pigginbfc1af62007-10-16 01:25:05 -0700858static int ext4_ordered_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400859 struct address_space *mapping,
860 loff_t pos, unsigned len, unsigned copied,
861 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700862{
Mingming Cao617ba132006-10-11 01:20:53 -0700863 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -0400864 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700865 int ret = 0, ret2;
866
Theodore Ts'o9bffad12009-06-17 11:48:11 -0400867 trace_ext4_ordered_write_end(inode, pos, len, copied);
Jan Kara678aaf42008-07-11 19:27:31 -0400868 ret = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700869
870 if (ret == 0) {
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -0400871 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -0700872 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -0400873 copied = ret2;
Jan Karaffacfa72009-07-13 16:22:22 -0400874 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -0400875 /* if we have allocated more blocks and copied
876 * less. We will have blocks allocated outside
877 * inode->i_size. So truncate them
878 */
879 ext4_orphan_add(handle, inode);
Roel Kluinf8a87d82008-04-29 22:01:18 -0400880 if (ret2 < 0)
881 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700882 }
Mingming Cao617ba132006-10-11 01:20:53 -0700883 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700884 if (!ret)
885 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -0700886
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -0400887 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -0500888 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400889 /*
Jan Karaffacfa72009-07-13 16:22:22 -0400890 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -0400891 * on the orphan list; we need to make sure the inode
892 * is removed from the orphan list in that case.
893 */
894 if (inode->i_nlink)
895 ext4_orphan_del(NULL, inode);
896 }
897
898
Nick Pigginbfc1af62007-10-16 01:25:05 -0700899 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700900}
901
Nick Pigginbfc1af62007-10-16 01:25:05 -0700902static int ext4_writeback_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400903 struct address_space *mapping,
904 loff_t pos, unsigned len, unsigned copied,
905 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700906{
Mingming Cao617ba132006-10-11 01:20:53 -0700907 handle_t *handle = ext4_journal_current_handle();
Jan Karacf108bc2008-07-11 19:27:31 -0400908 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700909 int ret = 0, ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700910
Theodore Ts'o9bffad12009-06-17 11:48:11 -0400911 trace_ext4_writeback_write_end(inode, pos, len, copied);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -0400912 ret2 = ext4_generic_write_end(file, mapping, pos, len, copied,
Nick Pigginbfc1af62007-10-16 01:25:05 -0700913 page, fsdata);
Roel Kluinf8a87d82008-04-29 22:01:18 -0400914 copied = ret2;
Jan Karaffacfa72009-07-13 16:22:22 -0400915 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -0400916 /* if we have allocated more blocks and copied
917 * less. We will have blocks allocated outside
918 * inode->i_size. So truncate them
919 */
920 ext4_orphan_add(handle, inode);
921
Roel Kluinf8a87d82008-04-29 22:01:18 -0400922 if (ret2 < 0)
923 ret = ret2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700924
Mingming Cao617ba132006-10-11 01:20:53 -0700925 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700926 if (!ret)
927 ret = ret2;
Nick Pigginbfc1af62007-10-16 01:25:05 -0700928
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -0400929 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -0500930 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400931 /*
Jan Karaffacfa72009-07-13 16:22:22 -0400932 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -0400933 * on the orphan list; we need to make sure the inode
934 * is removed from the orphan list in that case.
935 */
936 if (inode->i_nlink)
937 ext4_orphan_del(NULL, inode);
938 }
939
Nick Pigginbfc1af62007-10-16 01:25:05 -0700940 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700941}
942
Nick Pigginbfc1af62007-10-16 01:25:05 -0700943static int ext4_journalled_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400944 struct address_space *mapping,
945 loff_t pos, unsigned len, unsigned copied,
946 struct page *page, void *fsdata)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700947{
Mingming Cao617ba132006-10-11 01:20:53 -0700948 handle_t *handle = ext4_journal_current_handle();
Nick Pigginbfc1af62007-10-16 01:25:05 -0700949 struct inode *inode = mapping->host;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700950 int ret = 0, ret2;
951 int partial = 0;
Nick Pigginbfc1af62007-10-16 01:25:05 -0700952 unsigned from, to;
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -0400953 loff_t new_i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700954
Theodore Ts'o9bffad12009-06-17 11:48:11 -0400955 trace_ext4_journalled_write_end(inode, pos, len, copied);
Nick Pigginbfc1af62007-10-16 01:25:05 -0700956 from = pos & (PAGE_CACHE_SIZE - 1);
957 to = from + len;
958
959 if (copied < len) {
960 if (!PageUptodate(page))
961 copied = 0;
962 page_zero_new_buffers(page, from+copied, to);
963 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700964
965 ret = walk_page_buffers(handle, page_buffers(page), from,
Nick Pigginbfc1af62007-10-16 01:25:05 -0700966 to, &partial, write_end_fn);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700967 if (!partial)
968 SetPageUptodate(page);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -0400969 new_i_size = pos + copied;
970 if (new_i_size > inode->i_size)
Nick Pigginbfc1af62007-10-16 01:25:05 -0700971 i_size_write(inode, pos+copied);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -0500972 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -0400973 if (new_i_size > EXT4_I(inode)->i_disksize) {
974 ext4_update_i_disksize(inode, new_i_size);
Mingming Cao617ba132006-10-11 01:20:53 -0700975 ret2 = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700976 if (!ret)
977 ret = ret2;
978 }
Nick Pigginbfc1af62007-10-16 01:25:05 -0700979
Jan Karacf108bc2008-07-11 19:27:31 -0400980 unlock_page(page);
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -0400981 page_cache_release(page);
Jan Karaffacfa72009-07-13 16:22:22 -0400982 if (pos + len > inode->i_size && ext4_can_truncate(inode))
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -0400983 /* if we have allocated more blocks and copied
984 * less. We will have blocks allocated outside
985 * inode->i_size. So truncate them
986 */
987 ext4_orphan_add(handle, inode);
988
Mingming Cao617ba132006-10-11 01:20:53 -0700989 ret2 = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700990 if (!ret)
991 ret = ret2;
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -0400992 if (pos + len > inode->i_size) {
Jan Karab9a42072009-12-08 21:24:33 -0500993 ext4_truncate_failed_write(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -0400994 /*
Jan Karaffacfa72009-07-13 16:22:22 -0400995 * If truncate failed early the inode might still be
Aneesh Kumar K.Vf8514082009-06-05 00:56:49 -0400996 * on the orphan list; we need to make sure the inode
997 * is removed from the orphan list in that case.
998 */
999 if (inode->i_nlink)
1000 ext4_orphan_del(NULL, inode);
1001 }
Nick Pigginbfc1af62007-10-16 01:25:05 -07001002
1003 return ret ? ret : copied;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001004}
Mingming Caod2a17632008-07-14 17:52:37 -04001005
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001006/*
1007 * Reserve a single block located at lblock
1008 */
Theodore Ts'o01f49d02011-01-10 12:13:03 -05001009static int ext4_da_reserve_space(struct inode *inode, ext4_lblk_t lblock)
Mingming Caod2a17632008-07-14 17:52:37 -04001010{
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001011 int retries = 0;
Mingming Cao60e58e02009-01-22 18:13:05 +01001012 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001013 struct ext4_inode_info *ei = EXT4_I(inode);
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001014 unsigned long md_needed;
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001015 int ret;
Mingming Caod2a17632008-07-14 17:52:37 -04001016
1017 /*
1018 * recalculate the amount of metadata blocks to reserve
1019 * in order to allocate nrblocks
1020 * worse case is one extent per block
1021 */
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001022repeat:
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001023 spin_lock(&ei->i_block_reservation_lock);
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001024 md_needed = ext4_calc_metadata_amount(inode, lblock);
Theodore Ts'of8ec9d62010-01-01 01:00:21 -05001025 trace_ext4_da_reserve_space(inode, md_needed);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001026 spin_unlock(&ei->i_block_reservation_lock);
Mingming Caod2a17632008-07-14 17:52:37 -04001027
Mingming Cao60e58e02009-01-22 18:13:05 +01001028 /*
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001029 * We will charge metadata quota at writeout time; this saves
1030 * us from metadata over-estimation, though we may go over by
1031 * a small amount in the end. Here we just reserve for data.
Mingming Cao60e58e02009-01-22 18:13:05 +01001032 */
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001033 ret = dquot_reserve_block(inode, 1);
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001034 if (ret)
1035 return ret;
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001036 /*
1037 * We do still charge estimated metadata to the sb though;
1038 * we cannot afford to run out of free blocks.
1039 */
Allison Henderson55f020d2011-05-25 07:41:26 -04001040 if (ext4_claim_free_blocks(sbi, md_needed + 1, 0)) {
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001041 dquot_release_reservation_block(inode, 1);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001042 if (ext4_should_retry_alloc(inode->i_sb, &retries)) {
1043 yield();
1044 goto repeat;
1045 }
Mingming Caod2a17632008-07-14 17:52:37 -04001046 return -ENOSPC;
1047 }
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001048 spin_lock(&ei->i_block_reservation_lock);
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001049 ei->i_reserved_data_blocks++;
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001050 ei->i_reserved_meta_blocks += md_needed;
1051 spin_unlock(&ei->i_block_reservation_lock);
Dmitry Monakhov39bc6802009-12-10 16:36:27 +00001052
Mingming Caod2a17632008-07-14 17:52:37 -04001053 return 0; /* success */
1054}
1055
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001056static void ext4_da_release_space(struct inode *inode, int to_free)
Mingming Caod2a17632008-07-14 17:52:37 -04001057{
1058 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001059 struct ext4_inode_info *ei = EXT4_I(inode);
Mingming Caod2a17632008-07-14 17:52:37 -04001060
Mingming Caocd213222008-08-19 22:16:59 -04001061 if (!to_free)
1062 return; /* Nothing to release, exit */
1063
Mingming Caod2a17632008-07-14 17:52:37 -04001064 spin_lock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Caocd213222008-08-19 22:16:59 -04001065
Li Zefan5a58ec82010-05-17 02:00:00 -04001066 trace_ext4_da_release_space(inode, to_free);
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001067 if (unlikely(to_free > ei->i_reserved_data_blocks)) {
Mingming Caocd213222008-08-19 22:16:59 -04001068 /*
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001069 * if there aren't enough reserved blocks, then the
1070 * counter is messed up somewhere. Since this
1071 * function is called from invalidate page, it's
1072 * harmless to return without any action.
Mingming Caocd213222008-08-19 22:16:59 -04001073 */
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001074 ext4_msg(inode->i_sb, KERN_NOTICE, "ext4_da_release_space: "
1075 "ino %lu, to_free %d with only %d reserved "
1076 "data blocks\n", inode->i_ino, to_free,
1077 ei->i_reserved_data_blocks);
1078 WARN_ON(1);
1079 to_free = ei->i_reserved_data_blocks;
1080 }
1081 ei->i_reserved_data_blocks -= to_free;
1082
1083 if (ei->i_reserved_data_blocks == 0) {
1084 /*
1085 * We can release all of the reserved metadata blocks
1086 * only when we have written all of the delayed
1087 * allocation blocks.
1088 */
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001089 percpu_counter_sub(&sbi->s_dirtyblocks_counter,
1090 ei->i_reserved_meta_blocks);
Theodore Ts'oee5f4d92010-01-01 02:36:15 -05001091 ei->i_reserved_meta_blocks = 0;
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001092 ei->i_da_metadata_calc_len = 0;
Mingming Caocd213222008-08-19 22:16:59 -04001093 }
1094
Eric Sandeen72b8ab92010-05-16 11:00:00 -04001095 /* update fs dirty data blocks counter */
Theodore Ts'o0637c6f2009-12-30 14:20:45 -05001096 percpu_counter_sub(&sbi->s_dirtyblocks_counter, to_free);
Mingming Caod2a17632008-07-14 17:52:37 -04001097
Mingming Caod2a17632008-07-14 17:52:37 -04001098 spin_unlock(&EXT4_I(inode)->i_block_reservation_lock);
Mingming Cao60e58e02009-01-22 18:13:05 +01001099
Christoph Hellwig5dd40562010-03-03 09:05:00 -05001100 dquot_release_reservation_block(inode, to_free);
Mingming Caod2a17632008-07-14 17:52:37 -04001101}
1102
1103static void ext4_da_page_release_reservation(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04001104 unsigned long offset)
Mingming Caod2a17632008-07-14 17:52:37 -04001105{
1106 int to_release = 0;
1107 struct buffer_head *head, *bh;
1108 unsigned int curr_off = 0;
1109
1110 head = page_buffers(page);
1111 bh = head;
1112 do {
1113 unsigned int next_off = curr_off + bh->b_size;
1114
1115 if ((offset <= curr_off) && (buffer_delay(bh))) {
1116 to_release++;
1117 clear_buffer_delay(bh);
1118 }
1119 curr_off = next_off;
1120 } while ((bh = bh->b_this_page) != head);
Aneesh Kumar K.V12219ae2008-07-17 16:12:08 -04001121 ext4_da_release_space(page->mapping->host, to_release);
Mingming Caod2a17632008-07-14 17:52:37 -04001122}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001123
1124/*
Alex Tomas64769242008-07-11 19:27:31 -04001125 * Delayed allocation stuff
1126 */
1127
Alex Tomas64769242008-07-11 19:27:31 -04001128/*
1129 * mpage_da_submit_io - walks through extent of pages and try to write
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001130 * them with writepage() call back
Alex Tomas64769242008-07-11 19:27:31 -04001131 *
1132 * @mpd->inode: inode
1133 * @mpd->first_page: first page of the extent
1134 * @mpd->next_page: page after the last page of the extent
Alex Tomas64769242008-07-11 19:27:31 -04001135 *
1136 * By the time mpage_da_submit_io() is called we expect all blocks
1137 * to be allocated. this may be wrong if allocation failed.
1138 *
1139 * As pages are already locked by write_cache_pages(), we can't use it
1140 */
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04001141static int mpage_da_submit_io(struct mpage_da_data *mpd,
1142 struct ext4_map_blocks *map)
Alex Tomas64769242008-07-11 19:27:31 -04001143{
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001144 struct pagevec pvec;
1145 unsigned long index, end;
1146 int ret = 0, err, nr_pages, i;
1147 struct inode *inode = mpd->inode;
1148 struct address_space *mapping = inode->i_mapping;
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001149 loff_t size = i_size_read(inode);
Theodore Ts'o3ecdb3a2010-10-27 21:30:10 -04001150 unsigned int len, block_start;
1151 struct buffer_head *bh, *page_bufs = NULL;
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001152 int journal_data = ext4_should_journal_data(inode);
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04001153 sector_t pblock = 0, cur_logical = 0;
Theodore Ts'obd2d0212010-10-27 21:30:10 -04001154 struct ext4_io_submit io_submit;
Alex Tomas64769242008-07-11 19:27:31 -04001155
1156 BUG_ON(mpd->next_page <= mpd->first_page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -04001157 memset(&io_submit, 0, sizeof(io_submit));
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001158 /*
1159 * We need to start from the first_page to the next_page - 1
1160 * to make sure we also write the mapped dirty buffer_heads.
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001161 * If we look at mpd->b_blocknr we would only be looking
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001162 * at the currently mapped buffer_heads.
1163 */
Alex Tomas64769242008-07-11 19:27:31 -04001164 index = mpd->first_page;
1165 end = mpd->next_page - 1;
1166
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001167 pagevec_init(&pvec, 0);
Alex Tomas64769242008-07-11 19:27:31 -04001168 while (index <= end) {
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001169 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
Alex Tomas64769242008-07-11 19:27:31 -04001170 if (nr_pages == 0)
1171 break;
1172 for (i = 0; i < nr_pages; i++) {
Theodore Ts'o97498952011-02-26 14:08:01 -05001173 int commit_write = 0, skip_page = 0;
Alex Tomas64769242008-07-11 19:27:31 -04001174 struct page *page = pvec.pages[i];
1175
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001176 index = page->index;
1177 if (index > end)
1178 break;
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001179
1180 if (index == size >> PAGE_CACHE_SHIFT)
1181 len = size & ~PAGE_CACHE_MASK;
1182 else
1183 len = PAGE_CACHE_SIZE;
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04001184 if (map) {
1185 cur_logical = index << (PAGE_CACHE_SHIFT -
1186 inode->i_blkbits);
1187 pblock = map->m_pblk + (cur_logical -
1188 map->m_lblk);
1189 }
Aneesh Kumar K.V791b7f02009-01-05 21:50:43 -05001190 index++;
1191
1192 BUG_ON(!PageLocked(page));
1193 BUG_ON(PageWriteback(page));
1194
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001195 /*
1196 * If the page does not have buffers (for
1197 * whatever reason), try to create them using
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04001198 * __block_write_begin. If this fails,
Theodore Ts'o97498952011-02-26 14:08:01 -05001199 * skip the page and move on.
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001200 */
1201 if (!page_has_buffers(page)) {
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04001202 if (__block_write_begin(page, 0, len,
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001203 noalloc_get_block_write)) {
Theodore Ts'o97498952011-02-26 14:08:01 -05001204 skip_page:
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001205 unlock_page(page);
1206 continue;
1207 }
1208 commit_write = 1;
1209 }
Theodore Ts'o3ecdb3a2010-10-27 21:30:10 -04001210
1211 bh = page_bufs = page_buffers(page);
1212 block_start = 0;
1213 do {
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04001214 if (!bh)
Theodore Ts'o97498952011-02-26 14:08:01 -05001215 goto skip_page;
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04001216 if (map && (cur_logical >= map->m_lblk) &&
1217 (cur_logical <= (map->m_lblk +
1218 (map->m_len - 1)))) {
1219 if (buffer_delay(bh)) {
1220 clear_buffer_delay(bh);
1221 bh->b_blocknr = pblock;
1222 }
1223 if (buffer_unwritten(bh) ||
1224 buffer_mapped(bh))
1225 BUG_ON(bh->b_blocknr != pblock);
1226 if (map->m_flags & EXT4_MAP_UNINIT)
1227 set_buffer_uninit(bh);
1228 clear_buffer_unwritten(bh);
1229 }
1230
Theodore Ts'o97498952011-02-26 14:08:01 -05001231 /* skip page if block allocation undone */
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04001232 if (buffer_delay(bh) || buffer_unwritten(bh))
Theodore Ts'o97498952011-02-26 14:08:01 -05001233 skip_page = 1;
Theodore Ts'o3ecdb3a2010-10-27 21:30:10 -04001234 bh = bh->b_this_page;
1235 block_start += bh->b_size;
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04001236 cur_logical++;
1237 pblock++;
1238 } while (bh != page_bufs);
1239
Theodore Ts'o97498952011-02-26 14:08:01 -05001240 if (skip_page)
1241 goto skip_page;
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001242
1243 if (commit_write)
1244 /* mark the buffer_heads as dirty & uptodate */
1245 block_commit_write(page, 0, len);
1246
Theodore Ts'o97498952011-02-26 14:08:01 -05001247 clear_page_dirty_for_io(page);
Theodore Ts'obd2d0212010-10-27 21:30:10 -04001248 /*
1249 * Delalloc doesn't support data journalling,
1250 * but eventually maybe we'll lift this
1251 * restriction.
1252 */
1253 if (unlikely(journal_data && PageChecked(page)))
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001254 err = __ext4_journalled_writepage(page, len);
Theodore Ts'o14490322010-12-14 15:27:50 -05001255 else if (test_opt(inode->i_sb, MBLK_IO_SUBMIT))
Theodore Ts'obd2d0212010-10-27 21:30:10 -04001256 err = ext4_bio_write_page(&io_submit, page,
1257 len, mpd->wbc);
Theodore Ts'o14490322010-12-14 15:27:50 -05001258 else
1259 err = block_write_full_page(page,
1260 noalloc_get_block_write, mpd->wbc);
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001261
1262 if (!err)
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001263 mpd->pages_written++;
Alex Tomas64769242008-07-11 19:27:31 -04001264 /*
1265 * In error case, we have to continue because
1266 * remaining pages are still locked
Alex Tomas64769242008-07-11 19:27:31 -04001267 */
1268 if (ret == 0)
1269 ret = err;
1270 }
1271 pagevec_release(&pvec);
1272 }
Theodore Ts'obd2d0212010-10-27 21:30:10 -04001273 ext4_io_submit(&io_submit);
Alex Tomas64769242008-07-11 19:27:31 -04001274 return ret;
1275}
1276
Curt Wohlgemuthc7f59382011-02-26 12:27:52 -05001277static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001278{
1279 int nr_pages, i;
1280 pgoff_t index, end;
1281 struct pagevec pvec;
1282 struct inode *inode = mpd->inode;
1283 struct address_space *mapping = inode->i_mapping;
1284
Curt Wohlgemuthc7f59382011-02-26 12:27:52 -05001285 index = mpd->first_page;
1286 end = mpd->next_page - 1;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001287 while (index <= end) {
1288 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1289 if (nr_pages == 0)
1290 break;
1291 for (i = 0; i < nr_pages; i++) {
1292 struct page *page = pvec.pages[i];
Jan Kara9b1d09982010-03-03 16:19:32 -05001293 if (page->index > end)
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001294 break;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001295 BUG_ON(!PageLocked(page));
1296 BUG_ON(PageWriteback(page));
1297 block_invalidatepage(page, 0);
1298 ClearPageUptodate(page);
1299 unlock_page(page);
1300 }
Jan Kara9b1d09982010-03-03 16:19:32 -05001301 index = pvec.pages[nr_pages - 1]->index + 1;
1302 pagevec_release(&pvec);
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001303 }
1304 return;
1305}
1306
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001307static void ext4_print_free_blocks(struct inode *inode)
1308{
1309 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
Theodore Ts'o16939182009-09-26 17:43:59 -04001310 printk(KERN_CRIT "Total free blocks count %lld\n",
1311 ext4_count_free_blocks(inode->i_sb));
1312 printk(KERN_CRIT "Free/Dirty block details\n");
1313 printk(KERN_CRIT "free_blocks=%lld\n",
1314 (long long) percpu_counter_sum(&sbi->s_freeblocks_counter));
1315 printk(KERN_CRIT "dirty_blocks=%lld\n",
1316 (long long) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
1317 printk(KERN_CRIT "Block reservation details\n");
1318 printk(KERN_CRIT "i_reserved_data_blocks=%u\n",
1319 EXT4_I(inode)->i_reserved_data_blocks);
1320 printk(KERN_CRIT "i_reserved_meta_blocks=%u\n",
1321 EXT4_I(inode)->i_reserved_meta_blocks);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001322 return;
1323}
1324
Theodore Ts'ob920c752009-05-14 00:54:29 -04001325/*
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001326 * mpage_da_map_and_submit - go through given space, map them
1327 * if necessary, and then submit them for I/O
Alex Tomas64769242008-07-11 19:27:31 -04001328 *
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001329 * @mpd - bh describing space
Alex Tomas64769242008-07-11 19:27:31 -04001330 *
1331 * The function skips space we know is already mapped to disk blocks.
1332 *
Alex Tomas64769242008-07-11 19:27:31 -04001333 */
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001334static void mpage_da_map_and_submit(struct mpage_da_data *mpd)
Alex Tomas64769242008-07-11 19:27:31 -04001335{
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001336 int err, blks, get_blocks_flags;
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04001337 struct ext4_map_blocks map, *mapp = NULL;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04001338 sector_t next = mpd->b_blocknr;
1339 unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits;
1340 loff_t disksize = EXT4_I(mpd->inode)->i_disksize;
1341 handle_t *handle = NULL;
Alex Tomas64769242008-07-11 19:27:31 -04001342
1343 /*
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001344 * If the blocks are mapped already, or we couldn't accumulate
1345 * any blocks, then proceed immediately to the submission stage.
Alex Tomas64769242008-07-11 19:27:31 -04001346 */
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001347 if ((mpd->b_size == 0) ||
1348 ((mpd->b_state & (1 << BH_Mapped)) &&
1349 !(mpd->b_state & (1 << BH_Delay)) &&
1350 !(mpd->b_state & (1 << BH_Unwritten))))
1351 goto submit_io;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04001352
1353 handle = ext4_journal_current_handle();
1354 BUG_ON(!handle);
1355
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04001356 /*
Eric Sandeen79e83032010-07-27 11:56:07 -04001357 * Call ext4_map_blocks() to allocate any delayed allocation
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001358 * blocks, or to convert an uninitialized extent to be
1359 * initialized (in the case where we have written into
1360 * one or more preallocated blocks).
1361 *
1362 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
1363 * indicate that we are on the delayed allocation path. This
1364 * affects functions in many different parts of the allocation
1365 * call path. This flag exists primarily because we don't
Eric Sandeen79e83032010-07-27 11:56:07 -04001366 * want to change *many* call functions, so ext4_map_blocks()
Theodore Ts'of2321092011-01-10 12:12:36 -05001367 * will set the EXT4_STATE_DELALLOC_RESERVED flag once the
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001368 * inode's allocation semaphore is taken.
1369 *
1370 * If the blocks in questions were delalloc blocks, set
1371 * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
1372 * variables are updated after the blocks have been allocated.
Aneesh Kumar K.V79ffab32009-05-13 15:13:42 -04001373 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001374 map.m_lblk = next;
1375 map.m_len = max_blocks;
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05001376 get_blocks_flags = EXT4_GET_BLOCKS_CREATE;
Jiaying Zhang744692d2010-03-04 16:14:02 -05001377 if (ext4_should_dioread_nolock(mpd->inode))
1378 get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
Theodore Ts'o2ac3b6e2009-05-14 13:57:08 -04001379 if (mpd->b_state & (1 << BH_Delay))
Aneesh Kumar K.V1296cc82010-01-15 01:27:59 -05001380 get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
1381
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001382 blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags);
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04001383 if (blks < 0) {
Eric Sandeene3570632010-07-27 11:56:08 -04001384 struct super_block *sb = mpd->inode->i_sb;
1385
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04001386 err = blks;
Theodore Ts'oed5bde02009-02-23 10:48:07 -05001387 /*
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001388 * If get block returns EAGAIN or ENOSPC and there
Theodore Ts'o97498952011-02-26 14:08:01 -05001389 * appears to be free blocks we will just let
1390 * mpage_da_submit_io() unlock all of the pages.
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001391 */
1392 if (err == -EAGAIN)
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001393 goto submit_io;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001394
1395 if (err == -ENOSPC &&
Eric Sandeene3570632010-07-27 11:56:08 -04001396 ext4_count_free_blocks(sb)) {
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001397 mpd->retval = err;
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001398 goto submit_io;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001399 }
1400
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001401 /*
Theodore Ts'oed5bde02009-02-23 10:48:07 -05001402 * get block failure will cause us to loop in
1403 * writepages, because a_ops->writepage won't be able
1404 * to make progress. The page will be redirtied by
1405 * writepage and writepages will again try to write
1406 * the same.
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001407 */
Eric Sandeene3570632010-07-27 11:56:08 -04001408 if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) {
1409 ext4_msg(sb, KERN_CRIT,
1410 "delayed block allocation failed for inode %lu "
1411 "at logical offset %llu with max blocks %zd "
1412 "with error %d", mpd->inode->i_ino,
1413 (unsigned long long) next,
1414 mpd->b_size >> mpd->inode->i_blkbits, err);
1415 ext4_msg(sb, KERN_CRIT,
1416 "This should not happen!! Data will be lost\n");
1417 if (err == -ENOSPC)
1418 ext4_print_free_blocks(mpd->inode);
Aneesh Kumar K.V030ba6b2008-09-08 23:14:50 -04001419 }
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04001420 /* invalidate all the pages */
Curt Wohlgemuthc7f59382011-02-26 12:27:52 -05001421 ext4_da_block_invalidatepages(mpd);
Curt Wohlgemuthe0fd9b92011-02-26 12:25:52 -05001422
1423 /* Mark this page range as having been completed */
1424 mpd->io_done = 1;
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001425 return;
Aneesh Kumar K.Vc4a0c462008-08-19 21:08:18 -04001426 }
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04001427 BUG_ON(blks == 0);
1428
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04001429 mapp = &map;
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001430 if (map.m_flags & EXT4_MAP_NEW) {
1431 struct block_device *bdev = mpd->inode->i_sb->s_bdev;
1432 int i;
Alex Tomas64769242008-07-11 19:27:31 -04001433
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001434 for (i = 0; i < map.m_len; i++)
1435 unmap_underlying_metadata(bdev, map.m_pblk + i);
1436 }
Alex Tomas64769242008-07-11 19:27:31 -04001437
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04001438 if (ext4_should_order_data(mpd->inode)) {
1439 err = ext4_jbd2_file_inode(handle, mpd->inode);
1440 if (err)
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001441 /* This only happens if the journal is aborted */
1442 return;
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04001443 }
1444
1445 /*
Jan Kara03f5d8b2009-06-09 00:17:05 -04001446 * Update on-disk size along with block allocation.
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04001447 */
1448 disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits;
1449 if (disksize > i_size_read(mpd->inode))
1450 disksize = i_size_read(mpd->inode);
1451 if (disksize > EXT4_I(mpd->inode)->i_disksize) {
1452 ext4_update_i_disksize(mpd->inode, disksize);
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001453 err = ext4_mark_inode_dirty(handle, mpd->inode);
1454 if (err)
1455 ext4_error(mpd->inode->i_sb,
1456 "Failed to mark inode %lu dirty",
1457 mpd->inode->i_ino);
Theodore Ts'o2fa3cdf2009-05-14 09:29:45 -04001458 }
1459
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001460submit_io:
Theodore Ts'o1de3e3d2010-10-27 21:30:10 -04001461 mpage_da_submit_io(mpd, mapp);
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001462 mpd->io_done = 1;
Alex Tomas64769242008-07-11 19:27:31 -04001463}
1464
Aneesh Kumar K.Vbf068ee2008-08-19 22:16:43 -04001465#define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
1466 (1 << BH_Delay) | (1 << BH_Unwritten))
Alex Tomas64769242008-07-11 19:27:31 -04001467
1468/*
1469 * mpage_add_bh_to_extent - try to add one more block to extent of blocks
1470 *
1471 * @mpd->lbh - extent of blocks
1472 * @logical - logical number of the block in the file
1473 * @bh - bh of the block (used to access block's state)
1474 *
1475 * the function is used to collect contig. blocks in same state
1476 */
1477static void mpage_add_bh_to_extent(struct mpage_da_data *mpd,
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001478 sector_t logical, size_t b_size,
1479 unsigned long b_state)
Alex Tomas64769242008-07-11 19:27:31 -04001480{
Alex Tomas64769242008-07-11 19:27:31 -04001481 sector_t next;
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001482 int nrblocks = mpd->b_size >> mpd->inode->i_blkbits;
Alex Tomas64769242008-07-11 19:27:31 -04001483
Eric Sandeenc445e3e2010-05-16 04:00:00 -04001484 /*
1485 * XXX Don't go larger than mballoc is willing to allocate
1486 * This is a stopgap solution. We eventually need to fold
1487 * mpage_da_submit_io() into this function and then call
Eric Sandeen79e83032010-07-27 11:56:07 -04001488 * ext4_map_blocks() multiple times in a loop
Eric Sandeenc445e3e2010-05-16 04:00:00 -04001489 */
1490 if (nrblocks >= 8*1024*1024/mpd->inode->i_sb->s_blocksize)
1491 goto flush_it;
1492
Mingming Cao525f4ed2008-08-19 22:15:58 -04001493 /* check if thereserved journal credits might overflow */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001494 if (!(ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS))) {
Mingming Cao525f4ed2008-08-19 22:15:58 -04001495 if (nrblocks >= EXT4_MAX_TRANS_DATA) {
1496 /*
1497 * With non-extent format we are limited by the journal
1498 * credit available. Total credit needed to insert
1499 * nrblocks contiguous blocks is dependent on the
1500 * nrblocks. So limit nrblocks.
1501 */
1502 goto flush_it;
1503 } else if ((nrblocks + (b_size >> mpd->inode->i_blkbits)) >
1504 EXT4_MAX_TRANS_DATA) {
1505 /*
1506 * Adding the new buffer_head would make it cross the
1507 * allowed limit for which we have journal credit
1508 * reserved. So limit the new bh->b_size
1509 */
1510 b_size = (EXT4_MAX_TRANS_DATA - nrblocks) <<
1511 mpd->inode->i_blkbits;
1512 /* we will do mpage_da_submit_io in the next loop */
1513 }
1514 }
Alex Tomas64769242008-07-11 19:27:31 -04001515 /*
1516 * First block in the extent
1517 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001518 if (mpd->b_size == 0) {
1519 mpd->b_blocknr = logical;
1520 mpd->b_size = b_size;
1521 mpd->b_state = b_state & BH_FLAGS;
Alex Tomas64769242008-07-11 19:27:31 -04001522 return;
1523 }
1524
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001525 next = mpd->b_blocknr + nrblocks;
Alex Tomas64769242008-07-11 19:27:31 -04001526 /*
1527 * Can we merge the block to our big extent?
1528 */
Theodore Ts'o8dc207c2009-02-23 06:46:01 -05001529 if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) {
1530 mpd->b_size += b_size;
Alex Tomas64769242008-07-11 19:27:31 -04001531 return;
1532 }
1533
Mingming Cao525f4ed2008-08-19 22:15:58 -04001534flush_it:
Alex Tomas64769242008-07-11 19:27:31 -04001535 /*
1536 * We couldn't merge the block to our extent, so we
1537 * need to flush current extent and start new one
1538 */
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04001539 mpage_da_map_and_submit(mpd);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001540 return;
Alex Tomas64769242008-07-11 19:27:31 -04001541}
1542
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001543static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001544{
Aneesh Kumar K.Vc364b222009-06-14 17:57:10 -04001545 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001546}
1547
Alex Tomas64769242008-07-11 19:27:31 -04001548/*
Theodore Ts'ob920c752009-05-14 00:54:29 -04001549 * This is a special get_blocks_t callback which is used by
1550 * ext4_da_write_begin(). It will either return mapped block or
1551 * reserve space for a single block.
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04001552 *
1553 * For delayed buffer_head we have BH_Mapped, BH_New, BH_Delay set.
1554 * We also have b_blocknr = -1 and b_bdev initialized properly
1555 *
1556 * For unwritten buffer_head we have BH_Mapped, BH_New, BH_Unwritten set.
1557 * We also have b_blocknr = physicalblock mapping unwritten extent and b_bdev
1558 * initialized properly.
Alex Tomas64769242008-07-11 19:27:31 -04001559 */
1560static int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001561 struct buffer_head *bh, int create)
Alex Tomas64769242008-07-11 19:27:31 -04001562{
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001563 struct ext4_map_blocks map;
Alex Tomas64769242008-07-11 19:27:31 -04001564 int ret = 0;
Aneesh Kumar K.V33b98172009-05-12 14:40:37 -04001565 sector_t invalid_block = ~((sector_t) 0xffff);
1566
1567 if (invalid_block < ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es))
1568 invalid_block = ~0;
Alex Tomas64769242008-07-11 19:27:31 -04001569
1570 BUG_ON(create == 0);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001571 BUG_ON(bh->b_size != inode->i_sb->s_blocksize);
1572
1573 map.m_lblk = iblock;
1574 map.m_len = 1;
Alex Tomas64769242008-07-11 19:27:31 -04001575
1576 /*
1577 * first, we need to know whether the block is allocated already
1578 * preallocated blocks are unmapped but should treated
1579 * the same as allocated blocks.
1580 */
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001581 ret = ext4_map_blocks(NULL, inode, &map, 0);
1582 if (ret < 0)
1583 return ret;
1584 if (ret == 0) {
1585 if (buffer_delay(bh))
1586 return 0; /* Not sure this could or should happen */
Alex Tomas64769242008-07-11 19:27:31 -04001587 /*
Christoph Hellwigebdec242010-10-06 10:47:23 +02001588 * XXX: __block_write_begin() unmaps passed block, is it OK?
Alex Tomas64769242008-07-11 19:27:31 -04001589 */
Theodore Ts'o9d0be502010-01-01 02:41:30 -05001590 ret = ext4_da_reserve_space(inode, iblock);
Mingming Caod2a17632008-07-14 17:52:37 -04001591 if (ret)
1592 /* not enough space to reserve */
1593 return ret;
1594
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001595 map_bh(bh, inode->i_sb, invalid_block);
1596 set_buffer_new(bh);
1597 set_buffer_delay(bh);
1598 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04001599 }
1600
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001601 map_bh(bh, inode->i_sb, map.m_pblk);
1602 bh->b_state = (bh->b_state & ~EXT4_MAP_FLAGS) | map.m_flags;
1603
1604 if (buffer_unwritten(bh)) {
1605 /* A delayed write to unwritten bh should be marked
1606 * new and mapped. Mapped ensures that we don't do
1607 * get_block multiple times when we write to the same
1608 * offset and new ensures that we do proper zero out
1609 * for partial write.
1610 */
1611 set_buffer_new(bh);
Theodore Ts'oc8205632011-04-10 22:30:07 -04001612 set_buffer_mapped(bh);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001613 }
1614 return 0;
Alex Tomas64769242008-07-11 19:27:31 -04001615}
Mingming Cao61628a32008-07-11 19:27:31 -04001616
Theodore Ts'ob920c752009-05-14 00:54:29 -04001617/*
1618 * This function is used as a standard get_block_t calback function
1619 * when there is no desire to allocate any blocks. It is used as a
Christoph Hellwigebdec242010-10-06 10:47:23 +02001620 * callback function for block_write_begin() and block_write_full_page().
Christoph Hellwig206f7ab2010-06-14 14:42:49 -04001621 * These functions should only try to map a single block at a time.
Theodore Ts'ob920c752009-05-14 00:54:29 -04001622 *
1623 * Since this function doesn't do block allocations even if the caller
1624 * requests it by passing in create=1, it is critically important that
1625 * any caller checks to make sure that any buffer heads are returned
1626 * by this function are either all already mapped or marked for
Christoph Hellwig206f7ab2010-06-14 14:42:49 -04001627 * delayed allocation before calling block_write_full_page(). Otherwise,
1628 * b_blocknr could be left unitialized, and the page write functions will
1629 * be taken by surprise.
Theodore Ts'ob920c752009-05-14 00:54:29 -04001630 */
1631static int noalloc_get_block_write(struct inode *inode, sector_t iblock,
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04001632 struct buffer_head *bh_result, int create)
1633{
Theodore Ts'oa2dc52b2009-05-12 13:51:29 -04001634 BUG_ON(bh_result->b_size != inode->i_sb->s_blocksize);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04001635 return _ext4_get_block(inode, iblock, bh_result, 0);
Mingming Cao61628a32008-07-11 19:27:31 -04001636}
1637
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001638static int bget_one(handle_t *handle, struct buffer_head *bh)
1639{
1640 get_bh(bh);
1641 return 0;
1642}
1643
1644static int bput_one(handle_t *handle, struct buffer_head *bh)
1645{
1646 put_bh(bh);
1647 return 0;
1648}
1649
1650static int __ext4_journalled_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001651 unsigned int len)
1652{
1653 struct address_space *mapping = page->mapping;
1654 struct inode *inode = mapping->host;
1655 struct buffer_head *page_bufs;
1656 handle_t *handle = NULL;
1657 int ret = 0;
1658 int err;
1659
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001660 ClearPageChecked(page);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001661 page_bufs = page_buffers(page);
1662 BUG_ON(!page_bufs);
1663 walk_page_buffers(handle, page_bufs, 0, len, NULL, bget_one);
1664 /* As soon as we unlock the page, it can go away, but we have
1665 * references to buffers so we are safe */
1666 unlock_page(page);
1667
1668 handle = ext4_journal_start(inode, ext4_writepage_trans_blocks(inode));
1669 if (IS_ERR(handle)) {
1670 ret = PTR_ERR(handle);
1671 goto out;
1672 }
1673
1674 ret = walk_page_buffers(handle, page_bufs, 0, len, NULL,
1675 do_journal_get_write_access);
1676
1677 err = walk_page_buffers(handle, page_bufs, 0, len, NULL,
1678 write_end_fn);
1679 if (ret == 0)
1680 ret = err;
1681 err = ext4_journal_stop(handle);
1682 if (!ret)
1683 ret = err;
1684
1685 walk_page_buffers(handle, page_bufs, 0, len, NULL, bput_one);
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05001686 ext4_set_inode_state(inode, EXT4_STATE_JDATA);
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001687out:
1688 return ret;
1689}
1690
Jiaying Zhang744692d2010-03-04 16:14:02 -05001691static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode);
1692static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate);
1693
Mingming Cao61628a32008-07-11 19:27:31 -04001694/*
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001695 * Note that we don't need to start a transaction unless we're journaling data
1696 * because we should have holes filled from ext4_page_mkwrite(). We even don't
1697 * need to file the inode to the transaction's list in ordered mode because if
1698 * we are writing back data added by write(), the inode is already there and if
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001699 * we are writing back data modified via mmap(), no one guarantees in which
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001700 * transaction the data will hit the disk. In case we are journaling data, we
1701 * cannot start transaction directly because transaction start ranks above page
1702 * lock so we have to do some magic.
1703 *
Theodore Ts'ob920c752009-05-14 00:54:29 -04001704 * This function can get called via...
1705 * - ext4_da_writepages after taking page lock (have journal handle)
1706 * - journal_submit_inode_data_buffers (no journal handle)
1707 * - shrink_page_list via pdflush (no journal handle)
1708 * - grab_page_cache when doing write_begin (have journal handle)
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001709 *
1710 * We don't do any block allocation in this function. If we have page with
1711 * multiple blocks we need to write those buffer_heads that are mapped. This
1712 * is important for mmaped based write. So if we do with blocksize 1K
1713 * truncate(f, 1024);
1714 * a = mmap(f, 0, 4096);
1715 * a[0] = 'a';
1716 * truncate(f, 4096);
1717 * we have in the page first buffer_head mapped via page_mkwrite call back
1718 * but other bufer_heads would be unmapped but dirty(dirty done via the
1719 * do_wp_page). So writepage should write the first block. If we modify
1720 * the mmap area beyond 1024 we will again get a page_fault and the
1721 * page_mkwrite callback will do the block allocation and mark the
1722 * buffer_heads mapped.
1723 *
1724 * We redirty the page if we have any buffer_heads that is either delay or
1725 * unwritten in the page.
1726 *
1727 * We can get recursively called as show below.
1728 *
1729 * ext4_writepage() -> kmalloc() -> __alloc_pages() -> page_launder() ->
1730 * ext4_writepage()
1731 *
1732 * But since we don't do any block allocation we should not deadlock.
1733 * Page also have the dirty flag cleared so we don't get recurive page_lock.
Mingming Cao61628a32008-07-11 19:27:31 -04001734 */
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001735static int ext4_writepage(struct page *page,
Aneesh Kumar K.V62e086b2009-06-14 17:59:34 -04001736 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04001737{
Theodore Ts'oa42afc52010-10-27 21:30:09 -04001738 int ret = 0, commit_write = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04001739 loff_t size;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001740 unsigned int len;
Jiaying Zhang744692d2010-03-04 16:14:02 -05001741 struct buffer_head *page_bufs = NULL;
Mingming Cao61628a32008-07-11 19:27:31 -04001742 struct inode *inode = page->mapping->host;
Alex Tomas64769242008-07-11 19:27:31 -04001743
Lukas Czernera9c667f2011-06-06 09:51:52 -04001744 trace_ext4_writepage(page);
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04001745 size = i_size_read(inode);
1746 if (page->index == size >> PAGE_CACHE_SHIFT)
1747 len = size & ~PAGE_CACHE_MASK;
1748 else
1749 len = PAGE_CACHE_SIZE;
Alex Tomas64769242008-07-11 19:27:31 -04001750
Theodore Ts'oa42afc52010-10-27 21:30:09 -04001751 /*
1752 * If the page does not have buffers (for whatever reason),
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04001753 * try to create them using __block_write_begin. If this
Theodore Ts'oa42afc52010-10-27 21:30:09 -04001754 * fails, redirty the page and move on.
1755 */
Theodore Ts'ob1142e82010-10-28 17:33:57 -04001756 if (!page_has_buffers(page)) {
Theodore Ts'oa107e5a2010-10-27 23:44:47 -04001757 if (__block_write_begin(page, 0, len,
Theodore Ts'oa42afc52010-10-27 21:30:09 -04001758 noalloc_get_block_write)) {
1759 redirty_page:
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04001760 redirty_page_for_writepage(wbc, page);
1761 unlock_page(page);
1762 return 0;
1763 }
Theodore Ts'oa42afc52010-10-27 21:30:09 -04001764 commit_write = 1;
1765 }
1766 page_bufs = page_buffers(page);
1767 if (walk_page_buffers(NULL, page_bufs, 0, len, NULL,
1768 ext4_bh_delay_or_unwritten)) {
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04001769 /*
Theodore Ts'ob1142e82010-10-28 17:33:57 -04001770 * We don't want to do block allocation, so redirty
1771 * the page and return. We may reach here when we do
1772 * a journal commit via journal_submit_inode_data_buffers.
1773 * We can also reach here via shrink_page_list
Aneesh Kumar K.Vf0e6c982008-07-11 19:27:31 -04001774 */
Theodore Ts'oa42afc52010-10-27 21:30:09 -04001775 goto redirty_page;
1776 }
1777 if (commit_write)
Aneesh Kumar K.Ved9b3e32008-11-07 09:06:45 -05001778 /* now mark the buffer_heads as dirty and uptodate */
Aneesh Kumar K.Vb767e782009-06-04 08:06:06 -04001779 block_commit_write(page, 0, len);
Alex Tomas64769242008-07-11 19:27:31 -04001780
Theodore Ts'ocb20d512010-10-27 21:30:09 -04001781 if (PageChecked(page) && ext4_should_journal_data(inode))
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001782 /*
1783 * It's mmapped pagecache. Add buffers and journal it. There
1784 * doesn't seem much point in redirtying the page here.
1785 */
Wu Fengguang3f0ca302009-11-24 11:15:44 -05001786 return __ext4_journalled_writepage(page, len);
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04001787
Theodore Ts'oa42afc52010-10-27 21:30:09 -04001788 if (buffer_uninit(page_bufs)) {
Jiaying Zhang744692d2010-03-04 16:14:02 -05001789 ext4_set_bh_endio(page_bufs, inode);
1790 ret = block_write_full_page_endio(page, noalloc_get_block_write,
1791 wbc, ext4_end_io_buffer_write);
1792 } else
Theodore Ts'ob920c752009-05-14 00:54:29 -04001793 ret = block_write_full_page(page, noalloc_get_block_write,
1794 wbc);
Alex Tomas64769242008-07-11 19:27:31 -04001795
Alex Tomas64769242008-07-11 19:27:31 -04001796 return ret;
1797}
1798
Mingming Cao61628a32008-07-11 19:27:31 -04001799/*
Mingming Cao525f4ed2008-08-19 22:15:58 -04001800 * This is called via ext4_da_writepages() to
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001801 * calculate the total number of credits to reserve to fit
Mingming Cao525f4ed2008-08-19 22:15:58 -04001802 * a single extent allocation into a single transaction,
1803 * ext4_da_writpeages() will loop calling this before
1804 * the block allocation.
Mingming Cao61628a32008-07-11 19:27:31 -04001805 */
Mingming Cao525f4ed2008-08-19 22:15:58 -04001806
1807static int ext4_da_writepages_trans_blocks(struct inode *inode)
1808{
1809 int max_blocks = EXT4_I(inode)->i_reserved_data_blocks;
1810
1811 /*
1812 * With non-extent format the journal credit needed to
1813 * insert nrblocks contiguous block is dependent on
1814 * number of contiguous block. So we will limit
1815 * number of contiguous block to a sane value
1816 */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04001817 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) &&
Mingming Cao525f4ed2008-08-19 22:15:58 -04001818 (max_blocks > EXT4_MAX_TRANS_DATA))
1819 max_blocks = EXT4_MAX_TRANS_DATA;
1820
1821 return ext4_chunk_trans_blocks(inode, max_blocks);
1822}
Mingming Cao61628a32008-07-11 19:27:31 -04001823
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04001824/*
1825 * write_cache_pages_da - walk the list of dirty pages of the given
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05001826 * address space and accumulate pages that need writing, and call
Theodore Ts'o168fc022011-02-26 14:09:20 -05001827 * mpage_da_map_and_submit to map a single contiguous memory region
1828 * and then write them.
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04001829 */
1830static int write_cache_pages_da(struct address_space *mapping,
1831 struct writeback_control *wbc,
Eric Sandeen72f84e62010-10-27 21:30:13 -04001832 struct mpage_da_data *mpd,
1833 pgoff_t *done_index)
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04001834{
Theodore Ts'o4f01b022011-02-26 14:07:37 -05001835 struct buffer_head *bh, *head;
Theodore Ts'o168fc022011-02-26 14:09:20 -05001836 struct inode *inode = mapping->host;
Theodore Ts'o4f01b022011-02-26 14:07:37 -05001837 struct pagevec pvec;
1838 unsigned int nr_pages;
1839 sector_t logical;
1840 pgoff_t index, end;
1841 long nr_to_write = wbc->nr_to_write;
1842 int i, tag, ret = 0;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04001843
Theodore Ts'o168fc022011-02-26 14:09:20 -05001844 memset(mpd, 0, sizeof(struct mpage_da_data));
1845 mpd->wbc = wbc;
1846 mpd->inode = inode;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04001847 pagevec_init(&pvec, 0);
1848 index = wbc->range_start >> PAGE_CACHE_SHIFT;
1849 end = wbc->range_end >> PAGE_CACHE_SHIFT;
1850
Eric Sandeen5b41d922010-10-27 21:30:13 -04001851 if (wbc->sync_mode == WB_SYNC_ALL)
1852 tag = PAGECACHE_TAG_TOWRITE;
1853 else
1854 tag = PAGECACHE_TAG_DIRTY;
1855
Eric Sandeen72f84e62010-10-27 21:30:13 -04001856 *done_index = index;
Theodore Ts'o4f01b022011-02-26 14:07:37 -05001857 while (index <= end) {
Eric Sandeen5b41d922010-10-27 21:30:13 -04001858 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04001859 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
1860 if (nr_pages == 0)
Theodore Ts'o4f01b022011-02-26 14:07:37 -05001861 return 0;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04001862
1863 for (i = 0; i < nr_pages; i++) {
1864 struct page *page = pvec.pages[i];
1865
1866 /*
1867 * At this point, the page may be truncated or
1868 * invalidated (changing page->mapping to NULL), or
1869 * even swizzled back from swapper_space to tmpfs file
1870 * mapping. However, page->index will not change
1871 * because we have a reference on the page.
1872 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05001873 if (page->index > end)
1874 goto out;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04001875
Eric Sandeen72f84e62010-10-27 21:30:13 -04001876 *done_index = page->index + 1;
1877
Theodore Ts'o78aaced2011-02-26 14:09:14 -05001878 /*
1879 * If we can't merge this page, and we have
1880 * accumulated an contiguous region, write it
1881 */
1882 if ((mpd->next_page != page->index) &&
1883 (mpd->next_page != mpd->first_page)) {
1884 mpage_da_map_and_submit(mpd);
1885 goto ret_extent_tail;
1886 }
1887
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04001888 lock_page(page);
1889
1890 /*
Theodore Ts'o4f01b022011-02-26 14:07:37 -05001891 * If the page is no longer dirty, or its
1892 * mapping no longer corresponds to inode we
1893 * are writing (which means it has been
1894 * truncated or invalidated), or the page is
1895 * already under writeback and we are not
1896 * doing a data integrity writeback, skip the page
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04001897 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05001898 if (!PageDirty(page) ||
1899 (PageWriteback(page) &&
1900 (wbc->sync_mode == WB_SYNC_NONE)) ||
1901 unlikely(page->mapping != mapping)) {
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04001902 unlock_page(page);
1903 continue;
1904 }
1905
Darrick J. Wong7cb1a532011-05-18 13:53:20 -04001906 wait_on_page_writeback(page);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04001907 BUG_ON(PageWriteback(page));
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04001908
Theodore Ts'o168fc022011-02-26 14:09:20 -05001909 if (mpd->next_page != page->index)
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05001910 mpd->first_page = page->index;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05001911 mpd->next_page = page->index + 1;
1912 logical = (sector_t) page->index <<
1913 (PAGE_CACHE_SHIFT - inode->i_blkbits);
1914
1915 if (!page_has_buffers(page)) {
Theodore Ts'o4f01b022011-02-26 14:07:37 -05001916 mpage_add_bh_to_extent(mpd, logical,
1917 PAGE_CACHE_SIZE,
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05001918 (1 << BH_Dirty) | (1 << BH_Uptodate));
Theodore Ts'o4f01b022011-02-26 14:07:37 -05001919 if (mpd->io_done)
1920 goto ret_extent_tail;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05001921 } else {
1922 /*
Theodore Ts'o4f01b022011-02-26 14:07:37 -05001923 * Page with regular buffer heads,
1924 * just add all dirty ones
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05001925 */
1926 head = page_buffers(page);
1927 bh = head;
1928 do {
1929 BUG_ON(buffer_locked(bh));
1930 /*
1931 * We need to try to allocate
1932 * unmapped blocks in the same page.
1933 * Otherwise we won't make progress
1934 * with the page in ext4_writepage
1935 */
1936 if (ext4_bh_delay_or_unwritten(NULL, bh)) {
1937 mpage_add_bh_to_extent(mpd, logical,
1938 bh->b_size,
1939 bh->b_state);
Theodore Ts'o4f01b022011-02-26 14:07:37 -05001940 if (mpd->io_done)
1941 goto ret_extent_tail;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05001942 } else if (buffer_dirty(bh) && (buffer_mapped(bh))) {
1943 /*
Theodore Ts'o4f01b022011-02-26 14:07:37 -05001944 * mapped dirty buffer. We need
1945 * to update the b_state
1946 * because we look at b_state
1947 * in mpage_da_map_blocks. We
1948 * don't update b_size because
1949 * if we find an unmapped
1950 * buffer_head later we need to
1951 * use the b_state flag of that
1952 * buffer_head.
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05001953 */
1954 if (mpd->b_size == 0)
1955 mpd->b_state = bh->b_state & BH_FLAGS;
1956 }
1957 logical++;
1958 } while ((bh = bh->b_this_page) != head);
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04001959 }
1960
1961 if (nr_to_write > 0) {
1962 nr_to_write--;
1963 if (nr_to_write == 0 &&
Theodore Ts'o4f01b022011-02-26 14:07:37 -05001964 wbc->sync_mode == WB_SYNC_NONE)
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04001965 /*
1966 * We stop writing back only if we are
1967 * not doing integrity sync. In case of
1968 * integrity sync we have to keep going
1969 * because someone may be concurrently
1970 * dirtying pages, and we might have
1971 * synced a lot of newly appeared dirty
1972 * pages, but have not synced all of the
1973 * old dirty pages.
1974 */
Theodore Ts'o4f01b022011-02-26 14:07:37 -05001975 goto out;
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04001976 }
1977 }
1978 pagevec_release(&pvec);
1979 cond_resched();
1980 }
Theodore Ts'o4f01b022011-02-26 14:07:37 -05001981 return 0;
1982ret_extent_tail:
1983 ret = MPAGE_DA_EXTENT_TAIL;
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05001984out:
1985 pagevec_release(&pvec);
1986 cond_resched();
Theodore Ts'o8e48dcf2010-05-16 18:00:00 -04001987 return ret;
1988}
1989
1990
Alex Tomas64769242008-07-11 19:27:31 -04001991static int ext4_da_writepages(struct address_space *mapping,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04001992 struct writeback_control *wbc)
Alex Tomas64769242008-07-11 19:27:31 -04001993{
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04001994 pgoff_t index;
1995 int range_whole = 0;
Mingming Cao61628a32008-07-11 19:27:31 -04001996 handle_t *handle = NULL;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04001997 struct mpage_da_data mpd;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04001998 struct inode *inode = mapping->host;
Theodore Ts'o498e5f22008-11-05 00:14:04 -05001999 int pages_written = 0;
Theodore Ts'o55138e02009-09-29 13:31:31 -04002000 unsigned int max_pages;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002001 int range_cyclic, cycled = 1, io_done = 0;
Theodore Ts'o55138e02009-09-29 13:31:31 -04002002 int needed_blocks, ret = 0;
2003 long desired_nr_to_write, nr_to_writebump = 0;
Theodore Ts'ode89de62009-08-31 17:00:59 -04002004 loff_t range_start = wbc->range_start;
Aneesh Kumar K.V5e745b02008-08-18 18:00:57 -04002005 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
Eric Sandeen72f84e62010-10-27 21:30:13 -04002006 pgoff_t done_index = 0;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002007 pgoff_t end;
Mingming Cao61628a32008-07-11 19:27:31 -04002008
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002009 trace_ext4_da_writepages(inode, wbc);
Theodore Ts'oba80b102009-01-03 20:03:21 -05002010
Mingming Cao61628a32008-07-11 19:27:31 -04002011 /*
2012 * No pages to write? This is mainly a kludge to avoid starting
2013 * a transaction for special inodes like journal inode on last iput()
2014 * because that could violate lock ordering on umount
2015 */
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002016 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
Mingming Cao61628a32008-07-11 19:27:31 -04002017 return 0;
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002018
2019 /*
2020 * If the filesystem has aborted, it is read-only, so return
2021 * right away instead of dumping stack traces later on that
2022 * will obscure the real source of the problem. We test
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002023 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002024 * the latter could be true if the filesystem is mounted
2025 * read-only, and in that case, ext4_da_writepages should
2026 * *never* be called, so if that ever happens, we would want
2027 * the stack trace.
2028 */
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04002029 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
Theodore Ts'o2a21e372008-11-05 09:22:24 -05002030 return -EROFS;
2031
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002032 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2033 range_whole = 1;
Mingming Cao61628a32008-07-11 19:27:31 -04002034
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002035 range_cyclic = wbc->range_cyclic;
2036 if (wbc->range_cyclic) {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002037 index = mapping->writeback_index;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002038 if (index)
2039 cycled = 0;
2040 wbc->range_start = index << PAGE_CACHE_SHIFT;
2041 wbc->range_end = LLONG_MAX;
2042 wbc->range_cyclic = 0;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002043 end = -1;
2044 } else {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002045 index = wbc->range_start >> PAGE_CACHE_SHIFT;
Eric Sandeen5b41d922010-10-27 21:30:13 -04002046 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2047 }
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002048
Theodore Ts'o55138e02009-09-29 13:31:31 -04002049 /*
2050 * This works around two forms of stupidity. The first is in
2051 * the writeback code, which caps the maximum number of pages
2052 * written to be 1024 pages. This is wrong on multiple
2053 * levels; different architectues have a different page size,
2054 * which changes the maximum amount of data which gets
2055 * written. Secondly, 4 megabytes is way too small. XFS
2056 * forces this value to be 16 megabytes by multiplying
2057 * nr_to_write parameter by four, and then relies on its
2058 * allocator to allocate larger extents to make them
2059 * contiguous. Unfortunately this brings us to the second
2060 * stupidity, which is that ext4's mballoc code only allocates
2061 * at most 2048 blocks. So we force contiguous writes up to
2062 * the number of dirty blocks in the inode, or
2063 * sbi->max_writeback_mb_bump whichever is smaller.
2064 */
2065 max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT);
Eric Sandeenb443e732010-10-27 21:30:03 -04002066 if (!range_cyclic && range_whole) {
2067 if (wbc->nr_to_write == LONG_MAX)
2068 desired_nr_to_write = wbc->nr_to_write;
2069 else
2070 desired_nr_to_write = wbc->nr_to_write * 8;
2071 } else
Theodore Ts'o55138e02009-09-29 13:31:31 -04002072 desired_nr_to_write = ext4_num_dirty_pages(inode, index,
2073 max_pages);
2074 if (desired_nr_to_write > max_pages)
2075 desired_nr_to_write = max_pages;
2076
2077 if (wbc->nr_to_write < desired_nr_to_write) {
2078 nr_to_writebump = desired_nr_to_write - wbc->nr_to_write;
2079 wbc->nr_to_write = desired_nr_to_write;
2080 }
2081
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002082retry:
Eric Sandeen5b41d922010-10-27 21:30:13 -04002083 if (wbc->sync_mode == WB_SYNC_ALL)
2084 tag_pages_for_writeback(mapping, index, end);
2085
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002086 while (!ret && wbc->nr_to_write > 0) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002087
2088 /*
2089 * we insert one extent at a time. So we need
2090 * credit needed for single extent allocation.
2091 * journalled mode is currently not supported
2092 * by delalloc
2093 */
2094 BUG_ON(ext4_should_journal_data(inode));
Mingming Cao525f4ed2008-08-19 22:15:58 -04002095 needed_blocks = ext4_da_writepages_trans_blocks(inode);
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002096
Mingming Cao61628a32008-07-11 19:27:31 -04002097 /* start a new transaction*/
2098 handle = ext4_journal_start(inode, needed_blocks);
2099 if (IS_ERR(handle)) {
2100 ret = PTR_ERR(handle);
Theodore Ts'o16939182009-09-26 17:43:59 -04002101 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
Curt Wohlgemuthfbe845d2010-05-16 13:00:00 -04002102 "%ld pages, ino %lu; err %d", __func__,
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002103 wbc->nr_to_write, inode->i_ino, ret);
Mingming Cao61628a32008-07-11 19:27:31 -04002104 goto out_writepages;
2105 }
Theodore Ts'of63e6002009-02-23 16:42:39 -05002106
2107 /*
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002108 * Now call write_cache_pages_da() to find the next
Theodore Ts'of63e6002009-02-23 16:42:39 -05002109 * contiguous region of logical blocks that need
Theodore Ts'o8eb9e5c2011-02-26 14:07:31 -05002110 * blocks to be allocated by ext4 and submit them.
Theodore Ts'of63e6002009-02-23 16:42:39 -05002111 */
Eric Sandeen72f84e62010-10-27 21:30:13 -04002112 ret = write_cache_pages_da(mapping, wbc, &mpd, &done_index);
Theodore Ts'of63e6002009-02-23 16:42:39 -05002113 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002114 * If we have a contiguous extent of pages and we
Theodore Ts'of63e6002009-02-23 16:42:39 -05002115 * haven't done the I/O yet, map the blocks and submit
2116 * them for I/O.
2117 */
2118 if (!mpd.io_done && mpd.next_page != mpd.first_page) {
Theodore Ts'o5a87b7a2010-10-27 21:30:09 -04002119 mpage_da_map_and_submit(&mpd);
Theodore Ts'of63e6002009-02-23 16:42:39 -05002120 ret = MPAGE_DA_EXTENT_TAIL;
2121 }
Theodore Ts'ob3a3ca82009-08-31 23:13:11 -04002122 trace_ext4_da_write_pages(inode, &mpd);
Theodore Ts'of63e6002009-02-23 16:42:39 -05002123 wbc->nr_to_write -= mpd.pages_written;
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002124
Mingming Cao61628a32008-07-11 19:27:31 -04002125 ext4_journal_stop(handle);
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002126
Eric Sandeen8f64b322009-02-26 00:57:35 -05002127 if ((mpd.retval == -ENOSPC) && sbi->s_journal) {
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002128 /* commit the transaction which would
2129 * free blocks released in the transaction
2130 * and try again
2131 */
Aneesh Kumar K.Vdf222912008-09-08 23:05:34 -04002132 jbd2_journal_force_commit_nested(sbi->s_journal);
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002133 ret = 0;
2134 } else if (ret == MPAGE_DA_EXTENT_TAIL) {
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002135 /*
2136 * got one extent now try with
2137 * rest of the pages
2138 */
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002139 pages_written += mpd.pages_written;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002140 ret = 0;
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002141 io_done = 1;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002142 } else if (wbc->nr_to_write)
Mingming Cao61628a32008-07-11 19:27:31 -04002143 /*
2144 * There is no more writeout needed
2145 * or we requested for a noblocking writeout
2146 * and we found the device congested
2147 */
Mingming Cao61628a32008-07-11 19:27:31 -04002148 break;
Mingming Cao61628a32008-07-11 19:27:31 -04002149 }
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002150 if (!io_done && !cycled) {
2151 cycled = 1;
2152 index = 0;
2153 wbc->range_start = index << PAGE_CACHE_SHIFT;
2154 wbc->range_end = mapping->writeback_index - 1;
2155 goto retry;
2156 }
Mingming Cao61628a32008-07-11 19:27:31 -04002157
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002158 /* Update index */
Aneesh Kumar K.V2acf2c22009-02-14 10:42:58 -05002159 wbc->range_cyclic = range_cyclic;
Aneesh Kumar K.V22208de2008-10-16 10:10:36 -04002160 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2161 /*
2162 * set the writeback_index so that range_cyclic
2163 * mode will write it back later
2164 */
Eric Sandeen72f84e62010-10-27 21:30:13 -04002165 mapping->writeback_index = done_index;
Aneesh Kumar K.Va1d6cc52008-08-19 21:55:02 -04002166
Mingming Cao61628a32008-07-11 19:27:31 -04002167out_writepages:
Richard Kennedy2faf2e12009-12-25 15:46:07 -05002168 wbc->nr_to_write -= nr_to_writebump;
Theodore Ts'ode89de62009-08-31 17:00:59 -04002169 wbc->range_start = range_start;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002170 trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
Mingming Cao61628a32008-07-11 19:27:31 -04002171 return ret;
Alex Tomas64769242008-07-11 19:27:31 -04002172}
2173
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002174#define FALL_BACK_TO_NONDELALLOC 1
2175static int ext4_nonda_switch(struct super_block *sb)
2176{
2177 s64 free_blocks, dirty_blocks;
2178 struct ext4_sb_info *sbi = EXT4_SB(sb);
2179
2180 /*
2181 * switch to non delalloc mode if we are running low
2182 * on free block. The free block accounting via percpu
Eric Dumazet179f7eb2009-01-06 14:41:04 -08002183 * counters can get slightly wrong with percpu_counter_batch getting
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002184 * accumulated on each CPU without updating global counters
2185 * Delalloc need an accurate free block accounting. So switch
2186 * to non delalloc when we are near to error range.
2187 */
2188 free_blocks = percpu_counter_read_positive(&sbi->s_freeblocks_counter);
2189 dirty_blocks = percpu_counter_read_positive(&sbi->s_dirtyblocks_counter);
2190 if (2 * free_blocks < 3 * dirty_blocks ||
2191 free_blocks < (dirty_blocks + EXT4_FREEBLOCKS_WATERMARK)) {
2192 /*
Eric Sandeenc8afb442009-12-23 07:58:12 -05002193 * free block count is less than 150% of dirty blocks
2194 * or free blocks is less than watermark
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002195 */
2196 return 1;
2197 }
Eric Sandeenc8afb442009-12-23 07:58:12 -05002198 /*
2199 * Even if we don't switch but are nearing capacity,
2200 * start pushing delalloc when 1/2 of free blocks are dirty.
2201 */
2202 if (free_blocks < 2 * dirty_blocks)
2203 writeback_inodes_sb_if_idle(sb);
2204
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002205 return 0;
2206}
2207
Alex Tomas64769242008-07-11 19:27:31 -04002208static int ext4_da_write_begin(struct file *file, struct address_space *mapping,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002209 loff_t pos, unsigned len, unsigned flags,
2210 struct page **pagep, void **fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04002211{
Eric Sandeen72b8ab92010-05-16 11:00:00 -04002212 int ret, retries = 0;
Alex Tomas64769242008-07-11 19:27:31 -04002213 struct page *page;
2214 pgoff_t index;
Alex Tomas64769242008-07-11 19:27:31 -04002215 struct inode *inode = mapping->host;
2216 handle_t *handle;
2217
2218 index = pos >> PAGE_CACHE_SHIFT;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002219
2220 if (ext4_nonda_switch(inode->i_sb)) {
2221 *fsdata = (void *)FALL_BACK_TO_NONDELALLOC;
2222 return ext4_write_begin(file, mapping, pos,
2223 len, flags, pagep, fsdata);
2224 }
2225 *fsdata = (void *)0;
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002226 trace_ext4_da_write_begin(inode, pos, len, flags);
Mingming Caod2a17632008-07-14 17:52:37 -04002227retry:
Alex Tomas64769242008-07-11 19:27:31 -04002228 /*
2229 * With delayed allocation, we don't log the i_disksize update
2230 * if there is delayed block allocation. But we still need
2231 * to journalling the i_disksize update if writes to the end
2232 * of file which has an already mapped buffer.
2233 */
2234 handle = ext4_journal_start(inode, 1);
2235 if (IS_ERR(handle)) {
2236 ret = PTR_ERR(handle);
2237 goto out;
2238 }
Jan Karaebd36102009-02-22 21:09:59 -05002239 /* We cannot recurse into the filesystem as the transaction is already
2240 * started */
2241 flags |= AOP_FLAG_NOFS;
Alex Tomas64769242008-07-11 19:27:31 -04002242
Nick Piggin54566b22009-01-04 12:00:53 -08002243 page = grab_cache_page_write_begin(mapping, index, flags);
Eric Sandeend5a0d4f2008-08-02 18:51:06 -04002244 if (!page) {
2245 ext4_journal_stop(handle);
2246 ret = -ENOMEM;
2247 goto out;
2248 }
Alex Tomas64769242008-07-11 19:27:31 -04002249 *pagep = page;
2250
Christoph Hellwig6e1db882010-06-04 11:29:57 +02002251 ret = __block_write_begin(page, pos, len, ext4_da_get_block_prep);
Alex Tomas64769242008-07-11 19:27:31 -04002252 if (ret < 0) {
2253 unlock_page(page);
2254 ext4_journal_stop(handle);
2255 page_cache_release(page);
Aneesh Kumar K.Vae4d5372008-09-13 13:10:25 -04002256 /*
2257 * block_write_begin may have instantiated a few blocks
2258 * outside i_size. Trim these off again. Don't need
2259 * i_size_read because we hold i_mutex.
2260 */
2261 if (pos + len > inode->i_size)
Jan Karab9a42072009-12-08 21:24:33 -05002262 ext4_truncate_failed_write(inode);
Alex Tomas64769242008-07-11 19:27:31 -04002263 }
2264
Mingming Caod2a17632008-07-14 17:52:37 -04002265 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
2266 goto retry;
Alex Tomas64769242008-07-11 19:27:31 -04002267out:
2268 return ret;
2269}
2270
Mingming Cao632eaea2008-07-11 19:27:31 -04002271/*
2272 * Check if we should update i_disksize
2273 * when write to the end of file but not require block allocation
2274 */
2275static int ext4_da_should_update_i_disksize(struct page *page,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002276 unsigned long offset)
Mingming Cao632eaea2008-07-11 19:27:31 -04002277{
2278 struct buffer_head *bh;
2279 struct inode *inode = page->mapping->host;
2280 unsigned int idx;
2281 int i;
2282
2283 bh = page_buffers(page);
2284 idx = offset >> inode->i_blkbits;
2285
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002286 for (i = 0; i < idx; i++)
Mingming Cao632eaea2008-07-11 19:27:31 -04002287 bh = bh->b_this_page;
2288
Aneesh Kumar K.V29fa89d2009-05-12 16:30:27 -04002289 if (!buffer_mapped(bh) || (buffer_delay(bh)) || buffer_unwritten(bh))
Mingming Cao632eaea2008-07-11 19:27:31 -04002290 return 0;
2291 return 1;
2292}
2293
Alex Tomas64769242008-07-11 19:27:31 -04002294static int ext4_da_write_end(struct file *file,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002295 struct address_space *mapping,
2296 loff_t pos, unsigned len, unsigned copied,
2297 struct page *page, void *fsdata)
Alex Tomas64769242008-07-11 19:27:31 -04002298{
2299 struct inode *inode = mapping->host;
2300 int ret = 0, ret2;
2301 handle_t *handle = ext4_journal_current_handle();
2302 loff_t new_i_size;
Mingming Cao632eaea2008-07-11 19:27:31 -04002303 unsigned long start, end;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04002304 int write_mode = (int)(unsigned long)fsdata;
2305
2306 if (write_mode == FALL_BACK_TO_NONDELALLOC) {
2307 if (ext4_should_order_data(inode)) {
2308 return ext4_ordered_write_end(file, mapping, pos,
2309 len, copied, page, fsdata);
2310 } else if (ext4_should_writeback_data(inode)) {
2311 return ext4_writeback_write_end(file, mapping, pos,
2312 len, copied, page, fsdata);
2313 } else {
2314 BUG();
2315 }
2316 }
Mingming Cao632eaea2008-07-11 19:27:31 -04002317
Theodore Ts'o9bffad12009-06-17 11:48:11 -04002318 trace_ext4_da_write_end(inode, pos, len, copied);
Mingming Cao632eaea2008-07-11 19:27:31 -04002319 start = pos & (PAGE_CACHE_SIZE - 1);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002320 end = start + copied - 1;
Alex Tomas64769242008-07-11 19:27:31 -04002321
2322 /*
2323 * generic_write_end() will run mark_inode_dirty() if i_size
2324 * changes. So let's piggyback the i_disksize mark_inode_dirty
2325 * into that.
2326 */
2327
2328 new_i_size = pos + copied;
Mingming Cao632eaea2008-07-11 19:27:31 -04002329 if (new_i_size > EXT4_I(inode)->i_disksize) {
2330 if (ext4_da_should_update_i_disksize(page, end)) {
2331 down_write(&EXT4_I(inode)->i_data_sem);
2332 if (new_i_size > EXT4_I(inode)->i_disksize) {
2333 /*
2334 * Updating i_disksize when extending file
2335 * without needing block allocation
2336 */
2337 if (ext4_should_order_data(inode))
2338 ret = ext4_jbd2_file_inode(handle,
2339 inode);
Alex Tomas64769242008-07-11 19:27:31 -04002340
Mingming Cao632eaea2008-07-11 19:27:31 -04002341 EXT4_I(inode)->i_disksize = new_i_size;
2342 }
2343 up_write(&EXT4_I(inode)->i_data_sem);
Aneesh Kumar K.Vcf17fea2008-09-13 13:06:18 -04002344 /* We need to mark inode dirty even if
2345 * new_i_size is less that inode->i_size
2346 * bu greater than i_disksize.(hint delalloc)
2347 */
2348 ext4_mark_inode_dirty(handle, inode);
Alex Tomas64769242008-07-11 19:27:31 -04002349 }
Mingming Cao632eaea2008-07-11 19:27:31 -04002350 }
Alex Tomas64769242008-07-11 19:27:31 -04002351 ret2 = generic_write_end(file, mapping, pos, len, copied,
2352 page, fsdata);
2353 copied = ret2;
2354 if (ret2 < 0)
2355 ret = ret2;
2356 ret2 = ext4_journal_stop(handle);
2357 if (!ret)
2358 ret = ret2;
2359
2360 return ret ? ret : copied;
2361}
2362
2363static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
2364{
Alex Tomas64769242008-07-11 19:27:31 -04002365 /*
2366 * Drop reserved blocks
2367 */
2368 BUG_ON(!PageLocked(page));
2369 if (!page_has_buffers(page))
2370 goto out;
2371
Mingming Caod2a17632008-07-14 17:52:37 -04002372 ext4_da_page_release_reservation(page, offset);
Alex Tomas64769242008-07-11 19:27:31 -04002373
2374out:
2375 ext4_invalidatepage(page, offset);
2376
2377 return;
2378}
2379
Theodore Ts'occd25062009-02-26 01:04:07 -05002380/*
2381 * Force all delayed allocation blocks to be allocated for a given inode.
2382 */
2383int ext4_alloc_da_blocks(struct inode *inode)
2384{
Theodore Ts'ofb40ba02009-09-16 19:30:40 -04002385 trace_ext4_alloc_da_blocks(inode);
2386
Theodore Ts'occd25062009-02-26 01:04:07 -05002387 if (!EXT4_I(inode)->i_reserved_data_blocks &&
2388 !EXT4_I(inode)->i_reserved_meta_blocks)
2389 return 0;
2390
2391 /*
2392 * We do something simple for now. The filemap_flush() will
2393 * also start triggering a write of the data blocks, which is
2394 * not strictly speaking necessary (and for users of
2395 * laptop_mode, not even desirable). However, to do otherwise
2396 * would require replicating code paths in:
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002397 *
Theodore Ts'occd25062009-02-26 01:04:07 -05002398 * ext4_da_writepages() ->
2399 * write_cache_pages() ---> (via passed in callback function)
2400 * __mpage_da_writepage() -->
2401 * mpage_add_bh_to_extent()
2402 * mpage_da_map_blocks()
2403 *
2404 * The problem is that write_cache_pages(), located in
2405 * mm/page-writeback.c, marks pages clean in preparation for
2406 * doing I/O, which is not desirable if we're not planning on
2407 * doing I/O at all.
2408 *
2409 * We could call write_cache_pages(), and then redirty all of
Wu Fengguang380cf092010-11-11 19:23:29 +08002410 * the pages by calling redirty_page_for_writepage() but that
Theodore Ts'occd25062009-02-26 01:04:07 -05002411 * would be ugly in the extreme. So instead we would need to
2412 * replicate parts of the code in the above functions,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002413 * simplifying them because we wouldn't actually intend to
Theodore Ts'occd25062009-02-26 01:04:07 -05002414 * write out the pages, but rather only collect contiguous
2415 * logical block extents, call the multi-block allocator, and
2416 * then update the buffer heads with the block allocations.
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04002417 *
Theodore Ts'occd25062009-02-26 01:04:07 -05002418 * For now, though, we'll cheat by calling filemap_flush(),
2419 * which will map the blocks, and start the I/O, but not
2420 * actually wait for the I/O to complete.
2421 */
2422 return filemap_flush(inode->i_mapping);
2423}
Alex Tomas64769242008-07-11 19:27:31 -04002424
2425/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002426 * bmap() is special. It gets used by applications such as lilo and by
2427 * the swapper to find the on-disk block of a specific piece of data.
2428 *
2429 * Naturally, this is dangerous if the block concerned is still in the
Mingming Cao617ba132006-10-11 01:20:53 -07002430 * journal. If somebody makes a swapfile on an ext4 data-journaling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002431 * filesystem and enables swap, then they may get a nasty shock when the
2432 * data getting swapped to that swapfile suddenly gets overwritten by
2433 * the original zero's written out previously to the journal and
2434 * awaiting writeback in the kernel's buffer cache.
2435 *
2436 * So, if we see any bmap calls here on a modified, data-journaled file,
2437 * take extra steps to flush any blocks which might be in the cache.
2438 */
Mingming Cao617ba132006-10-11 01:20:53 -07002439static sector_t ext4_bmap(struct address_space *mapping, sector_t block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002440{
2441 struct inode *inode = mapping->host;
2442 journal_t *journal;
2443 int err;
2444
Alex Tomas64769242008-07-11 19:27:31 -04002445 if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) &&
2446 test_opt(inode->i_sb, DELALLOC)) {
2447 /*
2448 * With delalloc we want to sync the file
2449 * so that we can make sure we allocate
2450 * blocks for file
2451 */
2452 filemap_write_and_wait(mapping);
2453 }
2454
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002455 if (EXT4_JOURNAL(inode) &&
2456 ext4_test_inode_state(inode, EXT4_STATE_JDATA)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002457 /*
2458 * This is a REALLY heavyweight approach, but the use of
2459 * bmap on dirty files is expected to be extremely rare:
2460 * only if we run lilo or swapon on a freshly made file
2461 * do we expect this to happen.
2462 *
2463 * (bmap requires CAP_SYS_RAWIO so this does not
2464 * represent an unprivileged user DOS attack --- we'd be
2465 * in trouble if mortal users could trigger this path at
2466 * will.)
2467 *
Mingming Cao617ba132006-10-11 01:20:53 -07002468 * NB. EXT4_STATE_JDATA is not set on files other than
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002469 * regular files. If somebody wants to bmap a directory
2470 * or symlink and gets confused because the buffer
2471 * hasn't yet been flushed to disk, they deserve
2472 * everything they get.
2473 */
2474
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002475 ext4_clear_inode_state(inode, EXT4_STATE_JDATA);
Mingming Cao617ba132006-10-11 01:20:53 -07002476 journal = EXT4_JOURNAL(inode);
Mingming Caodab291a2006-10-11 01:21:01 -07002477 jbd2_journal_lock_updates(journal);
2478 err = jbd2_journal_flush(journal);
2479 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002480
2481 if (err)
2482 return 0;
2483 }
2484
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002485 return generic_block_bmap(mapping, block, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002486}
2487
Mingming Cao617ba132006-10-11 01:20:53 -07002488static int ext4_readpage(struct file *file, struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002489{
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04002490 trace_ext4_readpage(page);
Mingming Cao617ba132006-10-11 01:20:53 -07002491 return mpage_readpage(page, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002492}
2493
2494static int
Mingming Cao617ba132006-10-11 01:20:53 -07002495ext4_readpages(struct file *file, struct address_space *mapping,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002496 struct list_head *pages, unsigned nr_pages)
2497{
Mingming Cao617ba132006-10-11 01:20:53 -07002498 return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002499}
2500
Jiaying Zhang744692d2010-03-04 16:14:02 -05002501static void ext4_invalidatepage_free_endio(struct page *page, unsigned long offset)
2502{
2503 struct buffer_head *head, *bh;
2504 unsigned int curr_off = 0;
2505
2506 if (!page_has_buffers(page))
2507 return;
2508 head = bh = page_buffers(page);
2509 do {
2510 if (offset <= curr_off && test_clear_buffer_uninit(bh)
2511 && bh->b_private) {
2512 ext4_free_io_end(bh->b_private);
2513 bh->b_private = NULL;
2514 bh->b_end_io = NULL;
2515 }
2516 curr_off = curr_off + bh->b_size;
2517 bh = bh->b_this_page;
2518 } while (bh != head);
2519}
2520
Mingming Cao617ba132006-10-11 01:20:53 -07002521static void ext4_invalidatepage(struct page *page, unsigned long offset)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002522{
Mingming Cao617ba132006-10-11 01:20:53 -07002523 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002524
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04002525 trace_ext4_invalidatepage(page, offset);
2526
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002527 /*
Jiaying Zhang744692d2010-03-04 16:14:02 -05002528 * free any io_end structure allocated for buffers to be discarded
2529 */
2530 if (ext4_should_dioread_nolock(page->mapping->host))
2531 ext4_invalidatepage_free_endio(page, offset);
2532 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002533 * If it's a full truncate we just forget about the pending dirtying
2534 */
2535 if (offset == 0)
2536 ClearPageChecked(page);
2537
Frank Mayhar03901312009-01-07 00:06:22 -05002538 if (journal)
2539 jbd2_journal_invalidatepage(journal, page, offset);
2540 else
2541 block_invalidatepage(page, offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002542}
2543
Mingming Cao617ba132006-10-11 01:20:53 -07002544static int ext4_releasepage(struct page *page, gfp_t wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002545{
Mingming Cao617ba132006-10-11 01:20:53 -07002546 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002547
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04002548 trace_ext4_releasepage(page);
2549
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002550 WARN_ON(PageChecked(page));
2551 if (!page_has_buffers(page))
2552 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05002553 if (journal)
2554 return jbd2_journal_try_to_free_buffers(journal, page, wait);
2555 else
2556 return try_to_free_buffers(page);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002557}
2558
2559/*
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002560 * ext4_get_block used when preparing for a DIO write or buffer write.
2561 * We allocate an uinitialized extent if blocks haven't been allocated.
2562 * The extent will be converted to initialized after the IO is complete.
2563 */
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05002564static int ext4_get_block_write(struct inode *inode, sector_t iblock,
Mingming Cao4c0425f2009-09-28 15:48:41 -04002565 struct buffer_head *bh_result, int create)
2566{
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05002567 ext4_debug("ext4_get_block_write: inode %lu, create flag %d\n",
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002568 inode->i_ino, create);
Theodore Ts'o2ed88682010-05-16 20:00:00 -04002569 return _ext4_get_block(inode, iblock, bh_result,
2570 EXT4_GET_BLOCKS_IO_CREATE_EXT);
Mingming Cao4c0425f2009-09-28 15:48:41 -04002571}
2572
Mingming Cao4c0425f2009-09-28 15:48:41 -04002573static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
Christoph Hellwig552ef8022010-07-27 11:56:06 -04002574 ssize_t size, void *private, int ret,
2575 bool is_async)
Mingming Cao4c0425f2009-09-28 15:48:41 -04002576{
2577 ext4_io_end_t *io_end = iocb->private;
2578 struct workqueue_struct *wq;
Jiaying Zhang744692d2010-03-04 16:14:02 -05002579 unsigned long flags;
2580 struct ext4_inode_info *ei;
Mingming Cao4c0425f2009-09-28 15:48:41 -04002581
Mingming4b70df12009-11-03 14:44:54 -05002582 /* if not async direct IO or dio with 0 bytes write, just return */
2583 if (!io_end || !size)
Christoph Hellwig552ef8022010-07-27 11:56:06 -04002584 goto out;
Mingming4b70df12009-11-03 14:44:54 -05002585
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002586 ext_debug("ext4_end_io_dio(): io_end 0x%p"
2587 "for inode %lu, iocb 0x%p, offset %llu, size %llu\n",
2588 iocb->private, io_end->inode->i_ino, iocb, offset,
2589 size);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002590
2591 /* if not aio dio with unwritten extents, just free io and return */
Theodore Ts'obd2d0212010-10-27 21:30:10 -04002592 if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002593 ext4_free_io_end(io_end);
2594 iocb->private = NULL;
jiayingz@google.com (Jiaying Zhang)5b3ff232010-07-27 11:56:06 -04002595out:
2596 if (is_async)
2597 aio_complete(iocb, ret, 0);
2598 return;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002599 }
2600
Mingming Cao4c0425f2009-09-28 15:48:41 -04002601 io_end->offset = offset;
2602 io_end->size = size;
jiayingz@google.com (Jiaying Zhang)5b3ff232010-07-27 11:56:06 -04002603 if (is_async) {
2604 io_end->iocb = iocb;
2605 io_end->result = ret;
2606 }
Mingming Cao4c0425f2009-09-28 15:48:41 -04002607 wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq;
2608
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002609 /* Add the io_end to per-inode completed aio dio list*/
Jiaying Zhang744692d2010-03-04 16:14:02 -05002610 ei = EXT4_I(io_end->inode);
2611 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
2612 list_add_tail(&io_end->list, &ei->i_completed_io_list);
2613 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
Eric Sandeenc999af22010-10-27 21:30:07 -04002614
2615 /* queue the work to convert unwritten extents to written */
2616 queue_work(wq, &io_end->work);
Mingming Cao4c0425f2009-09-28 15:48:41 -04002617 iocb->private = NULL;
2618}
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05002619
Jiaying Zhang744692d2010-03-04 16:14:02 -05002620static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate)
2621{
2622 ext4_io_end_t *io_end = bh->b_private;
2623 struct workqueue_struct *wq;
2624 struct inode *inode;
2625 unsigned long flags;
2626
2627 if (!test_clear_buffer_uninit(bh) || !io_end)
2628 goto out;
2629
2630 if (!(io_end->inode->i_sb->s_flags & MS_ACTIVE)) {
2631 printk("sb umounted, discard end_io request for inode %lu\n",
2632 io_end->inode->i_ino);
2633 ext4_free_io_end(io_end);
2634 goto out;
2635 }
2636
Theodore Ts'obd2d0212010-10-27 21:30:10 -04002637 io_end->flag = EXT4_IO_END_UNWRITTEN;
Jiaying Zhang744692d2010-03-04 16:14:02 -05002638 inode = io_end->inode;
2639
2640 /* Add the io_end to per-inode completed io list*/
2641 spin_lock_irqsave(&EXT4_I(inode)->i_completed_io_lock, flags);
2642 list_add_tail(&io_end->list, &EXT4_I(inode)->i_completed_io_list);
2643 spin_unlock_irqrestore(&EXT4_I(inode)->i_completed_io_lock, flags);
2644
2645 wq = EXT4_SB(inode->i_sb)->dio_unwritten_wq;
2646 /* queue the work to convert unwritten extents to written */
2647 queue_work(wq, &io_end->work);
2648out:
2649 bh->b_private = NULL;
2650 bh->b_end_io = NULL;
2651 clear_buffer_uninit(bh);
2652 end_buffer_async_write(bh, uptodate);
2653}
2654
2655static int ext4_set_bh_endio(struct buffer_head *bh, struct inode *inode)
2656{
2657 ext4_io_end_t *io_end;
2658 struct page *page = bh->b_page;
2659 loff_t offset = (sector_t)page->index << PAGE_CACHE_SHIFT;
2660 size_t size = bh->b_size;
2661
2662retry:
2663 io_end = ext4_init_io_end(inode, GFP_ATOMIC);
2664 if (!io_end) {
Andrew Morton6db26ff2011-01-12 16:59:13 -08002665 pr_warn_ratelimited("%s: allocation fail\n", __func__);
Jiaying Zhang744692d2010-03-04 16:14:02 -05002666 schedule();
2667 goto retry;
2668 }
2669 io_end->offset = offset;
2670 io_end->size = size;
2671 /*
2672 * We need to hold a reference to the page to make sure it
2673 * doesn't get evicted before ext4_end_io_work() has a chance
2674 * to convert the extent from written to unwritten.
2675 */
2676 io_end->page = page;
2677 get_page(io_end->page);
2678
2679 bh->b_private = io_end;
2680 bh->b_end_io = ext4_end_io_buffer_write;
2681 return 0;
2682}
2683
Mingming Cao4c0425f2009-09-28 15:48:41 -04002684/*
2685 * For ext4 extent files, ext4 will do direct-io write to holes,
2686 * preallocated extents, and those write extend the file, no need to
2687 * fall back to buffered IO.
2688 *
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002689 * For holes, we fallocate those blocks, mark them as uninitialized
Mingming Cao4c0425f2009-09-28 15:48:41 -04002690 * If those blocks were preallocated, we mark sure they are splited, but
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04002691 * still keep the range to write as uninitialized.
Mingming Cao4c0425f2009-09-28 15:48:41 -04002692 *
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002693 * The unwrritten extents will be converted to written when DIO is completed.
2694 * For async direct IO, since the IO may still pending when return, we
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002695 * set up an end_io call back function, which will do the conversion
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002696 * when async direct IO completed.
Mingming Cao4c0425f2009-09-28 15:48:41 -04002697 *
2698 * If the O_DIRECT write will extend the file then add this inode to the
2699 * orphan list. So recovery will truncate it back to the original size
2700 * if the machine crashes during the write.
2701 *
2702 */
2703static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
2704 const struct iovec *iov, loff_t offset,
2705 unsigned long nr_segs)
2706{
2707 struct file *file = iocb->ki_filp;
2708 struct inode *inode = file->f_mapping->host;
2709 ssize_t ret;
2710 size_t count = iov_length(iov, nr_segs);
2711
2712 loff_t final_size = offset + count;
2713 if (rw == WRITE && final_size <= inode->i_size) {
2714 /*
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002715 * We could direct write to holes and fallocate.
2716 *
2717 * Allocated blocks to fill the hole are marked as uninitialized
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002718 * to prevent parallel buffered read to expose the stale data
Mingming Cao4c0425f2009-09-28 15:48:41 -04002719 * before DIO complete the data IO.
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002720 *
2721 * As to previously fallocated extents, ext4 get_block
Mingming Cao4c0425f2009-09-28 15:48:41 -04002722 * will just simply mark the buffer mapped but still
2723 * keep the extents uninitialized.
2724 *
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002725 * for non AIO case, we will convert those unwritten extents
2726 * to written after return back from blockdev_direct_IO.
2727 *
2728 * for async DIO, the conversion needs to be defered when
2729 * the IO is completed. The ext4 end_io callback function
2730 * will be called to take care of the conversion work.
2731 * Here for async case, we allocate an io_end structure to
2732 * hook to the iocb.
Mingming Cao4c0425f2009-09-28 15:48:41 -04002733 */
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002734 iocb->private = NULL;
2735 EXT4_I(inode)->cur_aio_dio = NULL;
2736 if (!is_sync_kiocb(iocb)) {
Jiaying Zhang744692d2010-03-04 16:14:02 -05002737 iocb->private = ext4_init_io_end(inode, GFP_NOFS);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002738 if (!iocb->private)
2739 return -ENOMEM;
2740 /*
2741 * we save the io structure for current async
Eric Sandeen79e83032010-07-27 11:56:07 -04002742 * direct IO, so that later ext4_map_blocks()
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002743 * could flag the io structure whether there
2744 * is a unwritten extents needs to be converted
2745 * when IO is completed.
2746 */
2747 EXT4_I(inode)->cur_aio_dio = iocb->private;
2748 }
2749
Mingming Cao4c0425f2009-09-28 15:48:41 -04002750 ret = blockdev_direct_IO(rw, iocb, inode,
2751 inode->i_sb->s_bdev, iov,
2752 offset, nr_segs,
Jiaying Zhangc7064ef2010-03-02 13:28:44 -05002753 ext4_get_block_write,
Mingming Cao4c0425f2009-09-28 15:48:41 -04002754 ext4_end_io_dio);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002755 if (iocb->private)
2756 EXT4_I(inode)->cur_aio_dio = NULL;
2757 /*
2758 * The io_end structure takes a reference to the inode,
2759 * that structure needs to be destroyed and the
2760 * reference to the inode need to be dropped, when IO is
2761 * complete, even with 0 byte write, or failed.
2762 *
2763 * In the successful AIO DIO case, the io_end structure will be
2764 * desctroyed and the reference to the inode will be dropped
2765 * after the end_io call back function is called.
2766 *
2767 * In the case there is 0 byte write, or error case, since
2768 * VFS direct IO won't invoke the end_io call back function,
2769 * we need to free the end_io structure here.
2770 */
2771 if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) {
2772 ext4_free_io_end(iocb->private);
2773 iocb->private = NULL;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002774 } else if (ret > 0 && ext4_test_inode_state(inode,
2775 EXT4_STATE_DIO_UNWRITTEN)) {
Mingming109f5562009-11-10 10:48:08 -05002776 int err;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002777 /*
2778 * for non AIO case, since the IO is already
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002779 * completed, we could do the conversion right here
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002780 */
Mingming109f5562009-11-10 10:48:08 -05002781 err = ext4_convert_unwritten_extents(inode,
2782 offset, ret);
2783 if (err < 0)
2784 ret = err;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05002785 ext4_clear_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
Mingming109f5562009-11-10 10:48:08 -05002786 }
Mingming Cao4c0425f2009-09-28 15:48:41 -04002787 return ret;
2788 }
Mingming Cao8d5d02e2009-09-28 15:48:29 -04002789
2790 /* for write the the end of file case, we fall back to old way */
Mingming Cao4c0425f2009-09-28 15:48:41 -04002791 return ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
2792}
2793
2794static ssize_t ext4_direct_IO(int rw, struct kiocb *iocb,
2795 const struct iovec *iov, loff_t offset,
2796 unsigned long nr_segs)
2797{
2798 struct file *file = iocb->ki_filp;
2799 struct inode *inode = file->f_mapping->host;
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04002800 ssize_t ret;
Mingming Cao4c0425f2009-09-28 15:48:41 -04002801
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04002802 trace_ext4_direct_IO_enter(inode, offset, iov_length(iov, nr_segs), rw);
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04002803 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04002804 ret = ext4_ext_direct_IO(rw, iocb, iov, offset, nr_segs);
2805 else
2806 ret = ext4_ind_direct_IO(rw, iocb, iov, offset, nr_segs);
2807 trace_ext4_direct_IO_exit(inode, offset,
2808 iov_length(iov, nr_segs), rw, ret);
2809 return ret;
Mingming Cao4c0425f2009-09-28 15:48:41 -04002810}
2811
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002812/*
Mingming Cao617ba132006-10-11 01:20:53 -07002813 * Pages can be marked dirty completely asynchronously from ext4's journalling
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002814 * activity. By filemap_sync_pte(), try_to_unmap_one(), etc. We cannot do
2815 * much here because ->set_page_dirty is called under VFS locks. The page is
2816 * not necessarily locked.
2817 *
2818 * We cannot just dirty the page and leave attached buffers clean, because the
2819 * buffers' dirty state is "definitive". We cannot just set the buffers dirty
2820 * or jbddirty because all the journalling code will explode.
2821 *
2822 * So what we do is to mark the page "pending dirty" and next time writepage
2823 * is called, propagate that into the buffers appropriately.
2824 */
Mingming Cao617ba132006-10-11 01:20:53 -07002825static int ext4_journalled_set_page_dirty(struct page *page)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002826{
2827 SetPageChecked(page);
2828 return __set_page_dirty_nobuffers(page);
2829}
2830
Mingming Cao617ba132006-10-11 01:20:53 -07002831static const struct address_space_operations ext4_ordered_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002832 .readpage = ext4_readpage,
2833 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002834 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002835 .write_begin = ext4_write_begin,
2836 .write_end = ext4_ordered_write_end,
2837 .bmap = ext4_bmap,
2838 .invalidatepage = ext4_invalidatepage,
2839 .releasepage = ext4_releasepage,
2840 .direct_IO = ext4_direct_IO,
2841 .migratepage = buffer_migrate_page,
2842 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02002843 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002844};
2845
Mingming Cao617ba132006-10-11 01:20:53 -07002846static const struct address_space_operations ext4_writeback_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002847 .readpage = ext4_readpage,
2848 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002849 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002850 .write_begin = ext4_write_begin,
2851 .write_end = ext4_writeback_write_end,
2852 .bmap = ext4_bmap,
2853 .invalidatepage = ext4_invalidatepage,
2854 .releasepage = ext4_releasepage,
2855 .direct_IO = ext4_direct_IO,
2856 .migratepage = buffer_migrate_page,
2857 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02002858 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002859};
2860
Mingming Cao617ba132006-10-11 01:20:53 -07002861static const struct address_space_operations ext4_journalled_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002862 .readpage = ext4_readpage,
2863 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002864 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002865 .write_begin = ext4_write_begin,
2866 .write_end = ext4_journalled_write_end,
2867 .set_page_dirty = ext4_journalled_set_page_dirty,
2868 .bmap = ext4_bmap,
2869 .invalidatepage = ext4_invalidatepage,
2870 .releasepage = ext4_releasepage,
2871 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02002872 .error_remove_page = generic_error_remove_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002873};
2874
Alex Tomas64769242008-07-11 19:27:31 -04002875static const struct address_space_operations ext4_da_aops = {
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002876 .readpage = ext4_readpage,
2877 .readpages = ext4_readpages,
Aneesh Kumar K.V43ce1d22009-06-14 17:58:45 -04002878 .writepage = ext4_writepage,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002879 .writepages = ext4_da_writepages,
Hisashi Hifumi8ab22b92008-07-28 15:46:36 -07002880 .write_begin = ext4_da_write_begin,
2881 .write_end = ext4_da_write_end,
2882 .bmap = ext4_bmap,
2883 .invalidatepage = ext4_da_invalidatepage,
2884 .releasepage = ext4_releasepage,
2885 .direct_IO = ext4_direct_IO,
2886 .migratepage = buffer_migrate_page,
2887 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02002888 .error_remove_page = generic_error_remove_page,
Alex Tomas64769242008-07-11 19:27:31 -04002889};
2890
Mingming Cao617ba132006-10-11 01:20:53 -07002891void ext4_set_aops(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002892{
Aneesh Kumar K.Vcd1aac32008-07-11 19:27:31 -04002893 if (ext4_should_order_data(inode) &&
2894 test_opt(inode->i_sb, DELALLOC))
2895 inode->i_mapping->a_ops = &ext4_da_aops;
2896 else if (ext4_should_order_data(inode))
Mingming Cao617ba132006-10-11 01:20:53 -07002897 inode->i_mapping->a_ops = &ext4_ordered_aops;
Alex Tomas64769242008-07-11 19:27:31 -04002898 else if (ext4_should_writeback_data(inode) &&
2899 test_opt(inode->i_sb, DELALLOC))
2900 inode->i_mapping->a_ops = &ext4_da_aops;
Mingming Cao617ba132006-10-11 01:20:53 -07002901 else if (ext4_should_writeback_data(inode))
2902 inode->i_mapping->a_ops = &ext4_writeback_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002903 else
Mingming Cao617ba132006-10-11 01:20:53 -07002904 inode->i_mapping->a_ops = &ext4_journalled_aops;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002905}
2906
2907/*
Mingming Cao617ba132006-10-11 01:20:53 -07002908 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002909 * up to the end of the block which corresponds to `from'.
2910 * This required during truncate. We need to physically zero the tail end
2911 * of that block so it doesn't yield old data if the file is later grown.
2912 */
Jan Karacf108bc2008-07-11 19:27:31 -04002913int ext4_block_truncate_page(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002914 struct address_space *mapping, loff_t from)
2915{
Allison Henderson30848852011-05-25 07:41:32 -04002916 unsigned offset = from & (PAGE_CACHE_SIZE-1);
2917 unsigned length;
2918 unsigned blocksize;
2919 struct inode *inode = mapping->host;
2920
2921 blocksize = inode->i_sb->s_blocksize;
2922 length = blocksize - (offset & (blocksize - 1));
2923
2924 return ext4_block_zero_page_range(handle, mapping, from, length);
2925}
2926
2927/*
2928 * ext4_block_zero_page_range() zeros out a mapping of length 'length'
2929 * starting from file offset 'from'. The range to be zero'd must
2930 * be contained with in one block. If the specified range exceeds
2931 * the end of the block it will be shortened to end of the block
2932 * that cooresponds to 'from'
2933 */
2934int ext4_block_zero_page_range(handle_t *handle,
2935 struct address_space *mapping, loff_t from, loff_t length)
2936{
Mingming Cao617ba132006-10-11 01:20:53 -07002937 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002938 unsigned offset = from & (PAGE_CACHE_SIZE-1);
Allison Henderson30848852011-05-25 07:41:32 -04002939 unsigned blocksize, max, pos;
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05002940 ext4_lblk_t iblock;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002941 struct inode *inode = mapping->host;
2942 struct buffer_head *bh;
Jan Karacf108bc2008-07-11 19:27:31 -04002943 struct page *page;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002944 int err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002945
Theodore Ts'of4a01012009-07-05 22:08:16 -04002946 page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
2947 mapping_gfp_mask(mapping) & ~__GFP_FS);
Jan Karacf108bc2008-07-11 19:27:31 -04002948 if (!page)
2949 return -EINVAL;
2950
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002951 blocksize = inode->i_sb->s_blocksize;
Allison Henderson30848852011-05-25 07:41:32 -04002952 max = blocksize - (offset & (blocksize - 1));
2953
2954 /*
2955 * correct length if it does not fall between
2956 * 'from' and the end of the block
2957 */
2958 if (length > max || length < 0)
2959 length = max;
2960
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002961 iblock = index << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
2962
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002963 if (!page_has_buffers(page))
2964 create_empty_buffers(page, blocksize, 0);
2965
2966 /* Find the buffer that contains "offset" */
2967 bh = page_buffers(page);
2968 pos = blocksize;
2969 while (offset >= pos) {
2970 bh = bh->b_this_page;
2971 iblock++;
2972 pos += blocksize;
2973 }
2974
2975 err = 0;
2976 if (buffer_freed(bh)) {
2977 BUFFER_TRACE(bh, "freed: skip");
2978 goto unlock;
2979 }
2980
2981 if (!buffer_mapped(bh)) {
2982 BUFFER_TRACE(bh, "unmapped");
Mingming Cao617ba132006-10-11 01:20:53 -07002983 ext4_get_block(inode, iblock, bh, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002984 /* unmapped? It's a hole - nothing to do */
2985 if (!buffer_mapped(bh)) {
2986 BUFFER_TRACE(bh, "still unmapped");
2987 goto unlock;
2988 }
2989 }
2990
2991 /* Ok, it's mapped. Make sure it's up-to-date */
2992 if (PageUptodate(page))
2993 set_buffer_uptodate(bh);
2994
2995 if (!buffer_uptodate(bh)) {
2996 err = -EIO;
2997 ll_rw_block(READ, 1, &bh);
2998 wait_on_buffer(bh);
2999 /* Uhhuh. Read error. Complain and punt. */
3000 if (!buffer_uptodate(bh))
3001 goto unlock;
3002 }
3003
Mingming Cao617ba132006-10-11 01:20:53 -07003004 if (ext4_should_journal_data(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003005 BUFFER_TRACE(bh, "get write access");
Mingming Cao617ba132006-10-11 01:20:53 -07003006 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003007 if (err)
3008 goto unlock;
3009 }
3010
Christoph Lametereebd2aa2008-02-04 22:28:29 -08003011 zero_user(page, offset, length);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003012
3013 BUFFER_TRACE(bh, "zeroed end of block");
3014
3015 err = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07003016 if (ext4_should_journal_data(inode)) {
Frank Mayhar03901312009-01-07 00:06:22 -05003017 err = ext4_handle_dirty_metadata(handle, inode, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003018 } else {
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05003019 if (ext4_should_order_data(inode) && EXT4_I(inode)->jinode)
Jan Kara678aaf42008-07-11 19:27:31 -04003020 err = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003021 mark_buffer_dirty(bh);
3022 }
3023
3024unlock:
3025 unlock_page(page);
3026 page_cache_release(page);
3027 return err;
3028}
3029
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003030int ext4_can_truncate(struct inode *inode)
3031{
Duane Griffin91ef4ca2008-07-11 19:27:31 -04003032 if (S_ISREG(inode->i_mode))
3033 return 1;
3034 if (S_ISDIR(inode->i_mode))
3035 return 1;
3036 if (S_ISLNK(inode->i_mode))
3037 return !ext4_inode_is_fast_symlink(inode);
3038 return 0;
3039}
3040
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003041/*
Allison Hendersona4bb6b62011-05-25 07:41:50 -04003042 * ext4_punch_hole: punches a hole in a file by releaseing the blocks
3043 * associated with the given offset and length
3044 *
3045 * @inode: File inode
3046 * @offset: The offset where the hole will begin
3047 * @len: The length of the hole
3048 *
3049 * Returns: 0 on sucess or negative on failure
3050 */
3051
3052int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
3053{
3054 struct inode *inode = file->f_path.dentry->d_inode;
3055 if (!S_ISREG(inode->i_mode))
3056 return -ENOTSUPP;
3057
3058 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
3059 /* TODO: Add support for non extent hole punching */
3060 return -ENOTSUPP;
3061 }
3062
3063 return ext4_ext_punch_hole(file, offset, length);
3064}
3065
3066/*
Mingming Cao617ba132006-10-11 01:20:53 -07003067 * ext4_truncate()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003068 *
Mingming Cao617ba132006-10-11 01:20:53 -07003069 * We block out ext4_get_block() block instantiations across the entire
3070 * transaction, and VFS/VM ensures that ext4_truncate() cannot run
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003071 * simultaneously on behalf of the same inode.
3072 *
3073 * As we work through the truncate and commmit bits of it to the journal there
3074 * is one core, guiding principle: the file's tree must always be consistent on
3075 * disk. We must be able to restart the truncate after a crash.
3076 *
3077 * The file's tree may be transiently inconsistent in memory (although it
3078 * probably isn't), but whenever we close off and commit a journal transaction,
3079 * the contents of (the filesystem + the journal) must be consistent and
3080 * restartable. It's pretty simple, really: bottom up, right to left (although
3081 * left-to-right works OK too).
3082 *
3083 * Note that at recovery time, journal replay occurs *before* the restart of
3084 * truncate against the orphan inode list.
3085 *
3086 * The committed inode has the new, desired i_size (which is the same as
Mingming Cao617ba132006-10-11 01:20:53 -07003087 * i_disksize in this case). After a crash, ext4_orphan_cleanup() will see
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003088 * that this inode's truncate did not complete and it will again call
Mingming Cao617ba132006-10-11 01:20:53 -07003089 * ext4_truncate() to have another go. So there will be instantiated blocks
3090 * to the right of the truncation point in a crashed ext4 filesystem. But
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003091 * that's fine - as long as they are linked from the inode, the post-crash
Mingming Cao617ba132006-10-11 01:20:53 -07003092 * ext4_truncate() run will find them and release them.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003093 */
Mingming Cao617ba132006-10-11 01:20:53 -07003094void ext4_truncate(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003095{
Amir Goldsteinff9893d2011-06-27 16:36:31 -04003096 trace_ext4_truncate_enter(inode);
3097
3098 if (!ext4_can_truncate(inode))
3099 return;
3100
3101 ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
3102
3103 if (inode->i_size == 0 && !test_opt(inode->i_sb, NO_AUTO_DA_ALLOC))
3104 ext4_set_inode_state(inode, EXT4_STATE_DA_ALLOC_CLOSE);
3105
3106 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3107 ext4_ext_truncate(inode);
3108 else
3109 ext4_ind_truncate(inode);
3110
3111 trace_ext4_truncate_exit(inode);
3112}
3113
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003114/*
Mingming Cao617ba132006-10-11 01:20:53 -07003115 * ext4_get_inode_loc returns with an extra refcount against the inode's
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003116 * underlying buffer_head on success. If 'in_mem' is true, we have all
3117 * data in memory that is needed to recreate the on-disk version of this
3118 * inode.
3119 */
Mingming Cao617ba132006-10-11 01:20:53 -07003120static int __ext4_get_inode_loc(struct inode *inode,
3121 struct ext4_iloc *iloc, int in_mem)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003122{
Theodore Ts'o240799c2008-10-09 23:53:47 -04003123 struct ext4_group_desc *gdp;
3124 struct buffer_head *bh;
3125 struct super_block *sb = inode->i_sb;
3126 ext4_fsblk_t block;
3127 int inodes_per_block, inode_offset;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003128
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003129 iloc->bh = NULL;
Theodore Ts'o240799c2008-10-09 23:53:47 -04003130 if (!ext4_valid_inum(sb, inode->i_ino))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003131 return -EIO;
3132
Theodore Ts'o240799c2008-10-09 23:53:47 -04003133 iloc->block_group = (inode->i_ino - 1) / EXT4_INODES_PER_GROUP(sb);
3134 gdp = ext4_get_group_desc(sb, iloc->block_group, NULL);
3135 if (!gdp)
3136 return -EIO;
3137
3138 /*
3139 * Figure out the offset within the block group inode table
3140 */
Tao Ma00d09882011-05-09 10:26:41 -04003141 inodes_per_block = EXT4_SB(sb)->s_inodes_per_block;
Theodore Ts'o240799c2008-10-09 23:53:47 -04003142 inode_offset = ((inode->i_ino - 1) %
3143 EXT4_INODES_PER_GROUP(sb));
3144 block = ext4_inode_table(sb, gdp) + (inode_offset / inodes_per_block);
3145 iloc->offset = (inode_offset % inodes_per_block) * EXT4_INODE_SIZE(sb);
3146
3147 bh = sb_getblk(sb, block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003148 if (!bh) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -04003149 EXT4_ERROR_INODE_BLOCK(inode, block,
3150 "unable to read itable block");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003151 return -EIO;
3152 }
3153 if (!buffer_uptodate(bh)) {
3154 lock_buffer(bh);
Hidehiro Kawai9c83a922008-07-26 16:39:26 -04003155
3156 /*
3157 * If the buffer has the write error flag, we have failed
3158 * to write out another inode in the same block. In this
3159 * case, we don't have to read the block because we may
3160 * read the old inode data successfully.
3161 */
3162 if (buffer_write_io_error(bh) && !buffer_uptodate(bh))
3163 set_buffer_uptodate(bh);
3164
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003165 if (buffer_uptodate(bh)) {
3166 /* someone brought it uptodate while we waited */
3167 unlock_buffer(bh);
3168 goto has_buffer;
3169 }
3170
3171 /*
3172 * If we have all information of the inode in memory and this
3173 * is the only valid inode in the block, we need not read the
3174 * block.
3175 */
3176 if (in_mem) {
3177 struct buffer_head *bitmap_bh;
Theodore Ts'o240799c2008-10-09 23:53:47 -04003178 int i, start;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003179
Theodore Ts'o240799c2008-10-09 23:53:47 -04003180 start = inode_offset & ~(inodes_per_block - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003181
3182 /* Is the inode bitmap in cache? */
Theodore Ts'o240799c2008-10-09 23:53:47 -04003183 bitmap_bh = sb_getblk(sb, ext4_inode_bitmap(sb, gdp));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003184 if (!bitmap_bh)
3185 goto make_io;
3186
3187 /*
3188 * If the inode bitmap isn't in cache then the
3189 * optimisation may end up performing two reads instead
3190 * of one, so skip it.
3191 */
3192 if (!buffer_uptodate(bitmap_bh)) {
3193 brelse(bitmap_bh);
3194 goto make_io;
3195 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04003196 for (i = start; i < start + inodes_per_block; i++) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003197 if (i == inode_offset)
3198 continue;
Mingming Cao617ba132006-10-11 01:20:53 -07003199 if (ext4_test_bit(i, bitmap_bh->b_data))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003200 break;
3201 }
3202 brelse(bitmap_bh);
Theodore Ts'o240799c2008-10-09 23:53:47 -04003203 if (i == start + inodes_per_block) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003204 /* all other inodes are free, so skip I/O */
3205 memset(bh->b_data, 0, bh->b_size);
3206 set_buffer_uptodate(bh);
3207 unlock_buffer(bh);
3208 goto has_buffer;
3209 }
3210 }
3211
3212make_io:
3213 /*
Theodore Ts'o240799c2008-10-09 23:53:47 -04003214 * If we need to do any I/O, try to pre-readahead extra
3215 * blocks from the inode table.
3216 */
3217 if (EXT4_SB(sb)->s_inode_readahead_blks) {
3218 ext4_fsblk_t b, end, table;
3219 unsigned num;
3220
3221 table = ext4_inode_table(sb, gdp);
Theodore Ts'ob713a5e2009-03-31 09:11:14 -04003222 /* s_inode_readahead_blks is always a power of 2 */
Theodore Ts'o240799c2008-10-09 23:53:47 -04003223 b = block & ~(EXT4_SB(sb)->s_inode_readahead_blks-1);
3224 if (table > b)
3225 b = table;
3226 end = b + EXT4_SB(sb)->s_inode_readahead_blks;
3227 num = EXT4_INODES_PER_GROUP(sb);
3228 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3229 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05003230 num -= ext4_itable_unused_count(sb, gdp);
Theodore Ts'o240799c2008-10-09 23:53:47 -04003231 table += num / inodes_per_block;
3232 if (end > table)
3233 end = table;
3234 while (b <= end)
3235 sb_breadahead(sb, b++);
3236 }
3237
3238 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003239 * There are other valid inodes in the buffer, this inode
3240 * has in-inode xattrs, or we don't have this inode in memory.
3241 * Read the block from disk.
3242 */
Jiaying Zhang0562e0b2011-03-21 21:38:05 -04003243 trace_ext4_load_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003244 get_bh(bh);
3245 bh->b_end_io = end_buffer_read_sync;
3246 submit_bh(READ_META, bh);
3247 wait_on_buffer(bh);
3248 if (!buffer_uptodate(bh)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -04003249 EXT4_ERROR_INODE_BLOCK(inode, block,
3250 "unable to read itable block");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003251 brelse(bh);
3252 return -EIO;
3253 }
3254 }
3255has_buffer:
3256 iloc->bh = bh;
3257 return 0;
3258}
3259
Mingming Cao617ba132006-10-11 01:20:53 -07003260int ext4_get_inode_loc(struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003261{
3262 /* We have all inode data except xattrs in memory here. */
Mingming Cao617ba132006-10-11 01:20:53 -07003263 return __ext4_get_inode_loc(inode, iloc,
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003264 !ext4_test_inode_state(inode, EXT4_STATE_XATTR));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003265}
3266
Mingming Cao617ba132006-10-11 01:20:53 -07003267void ext4_set_inode_flags(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003268{
Mingming Cao617ba132006-10-11 01:20:53 -07003269 unsigned int flags = EXT4_I(inode)->i_flags;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003270
3271 inode->i_flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC);
Mingming Cao617ba132006-10-11 01:20:53 -07003272 if (flags & EXT4_SYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003273 inode->i_flags |= S_SYNC;
Mingming Cao617ba132006-10-11 01:20:53 -07003274 if (flags & EXT4_APPEND_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003275 inode->i_flags |= S_APPEND;
Mingming Cao617ba132006-10-11 01:20:53 -07003276 if (flags & EXT4_IMMUTABLE_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003277 inode->i_flags |= S_IMMUTABLE;
Mingming Cao617ba132006-10-11 01:20:53 -07003278 if (flags & EXT4_NOATIME_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003279 inode->i_flags |= S_NOATIME;
Mingming Cao617ba132006-10-11 01:20:53 -07003280 if (flags & EXT4_DIRSYNC_FL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003281 inode->i_flags |= S_DIRSYNC;
3282}
3283
Jan Karaff9ddf72007-07-18 09:24:20 -04003284/* Propagate flags from i_flags to EXT4_I(inode)->i_flags */
3285void ext4_get_inode_flags(struct ext4_inode_info *ei)
3286{
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04003287 unsigned int vfs_fl;
3288 unsigned long old_fl, new_fl;
Jan Karaff9ddf72007-07-18 09:24:20 -04003289
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04003290 do {
3291 vfs_fl = ei->vfs_inode.i_flags;
3292 old_fl = ei->i_flags;
3293 new_fl = old_fl & ~(EXT4_SYNC_FL|EXT4_APPEND_FL|
3294 EXT4_IMMUTABLE_FL|EXT4_NOATIME_FL|
3295 EXT4_DIRSYNC_FL);
3296 if (vfs_fl & S_SYNC)
3297 new_fl |= EXT4_SYNC_FL;
3298 if (vfs_fl & S_APPEND)
3299 new_fl |= EXT4_APPEND_FL;
3300 if (vfs_fl & S_IMMUTABLE)
3301 new_fl |= EXT4_IMMUTABLE_FL;
3302 if (vfs_fl & S_NOATIME)
3303 new_fl |= EXT4_NOATIME_FL;
3304 if (vfs_fl & S_DIRSYNC)
3305 new_fl |= EXT4_DIRSYNC_FL;
3306 } while (cmpxchg(&ei->i_flags, old_fl, new_fl) != old_fl);
Jan Karaff9ddf72007-07-18 09:24:20 -04003307}
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003308
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003309static blkcnt_t ext4_inode_blocks(struct ext4_inode *raw_inode,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003310 struct ext4_inode_info *ei)
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003311{
3312 blkcnt_t i_blocks ;
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05003313 struct inode *inode = &(ei->vfs_inode);
3314 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003315
3316 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3317 EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
3318 /* we are using combined 48 bit field */
3319 i_blocks = ((u64)le16_to_cpu(raw_inode->i_blocks_high)) << 32 |
3320 le32_to_cpu(raw_inode->i_blocks_lo);
Theodore Ts'o07a03822010-06-14 09:54:48 -04003321 if (ext4_test_inode_flag(inode, EXT4_INODE_HUGE_FILE)) {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05003322 /* i_blocks represent file system block size */
3323 return i_blocks << (inode->i_blkbits - 9);
3324 } else {
3325 return i_blocks;
3326 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003327 } else {
3328 return le32_to_cpu(raw_inode->i_blocks_lo);
3329 }
3330}
Jan Karaff9ddf72007-07-18 09:24:20 -04003331
David Howells1d1fe1e2008-02-07 00:15:37 -08003332struct inode *ext4_iget(struct super_block *sb, unsigned long ino)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003333{
Mingming Cao617ba132006-10-11 01:20:53 -07003334 struct ext4_iloc iloc;
3335 struct ext4_inode *raw_inode;
David Howells1d1fe1e2008-02-07 00:15:37 -08003336 struct ext4_inode_info *ei;
David Howells1d1fe1e2008-02-07 00:15:37 -08003337 struct inode *inode;
Jan Karab436b9b2009-12-08 23:51:10 -05003338 journal_t *journal = EXT4_SB(sb)->s_journal;
David Howells1d1fe1e2008-02-07 00:15:37 -08003339 long ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003340 int block;
3341
David Howells1d1fe1e2008-02-07 00:15:37 -08003342 inode = iget_locked(sb, ino);
3343 if (!inode)
3344 return ERR_PTR(-ENOMEM);
3345 if (!(inode->i_state & I_NEW))
3346 return inode;
3347
3348 ei = EXT4_I(inode);
Peter Huewe7dc57612011-02-21 21:01:42 -05003349 iloc.bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003350
David Howells1d1fe1e2008-02-07 00:15:37 -08003351 ret = __ext4_get_inode_loc(inode, &iloc, 0);
3352 if (ret < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003353 goto bad_inode;
Mingming Cao617ba132006-10-11 01:20:53 -07003354 raw_inode = ext4_raw_inode(&iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003355 inode->i_mode = le16_to_cpu(raw_inode->i_mode);
3356 inode->i_uid = (uid_t)le16_to_cpu(raw_inode->i_uid_low);
3357 inode->i_gid = (gid_t)le16_to_cpu(raw_inode->i_gid_low);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003358 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003359 inode->i_uid |= le16_to_cpu(raw_inode->i_uid_high) << 16;
3360 inode->i_gid |= le16_to_cpu(raw_inode->i_gid_high) << 16;
3361 }
3362 inode->i_nlink = le16_to_cpu(raw_inode->i_links_count);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003363
Theodore Ts'o353eb832011-01-10 12:18:25 -05003364 ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003365 ei->i_dir_start_lookup = 0;
3366 ei->i_dtime = le32_to_cpu(raw_inode->i_dtime);
3367 /* We now have enough fields to check if the inode was active or not.
3368 * This is needed because nfsd might try to access dead inodes
3369 * the test is that same one that e2fsck uses
3370 * NeilBrown 1999oct15
3371 */
3372 if (inode->i_nlink == 0) {
3373 if (inode->i_mode == 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07003374 !(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ORPHAN_FS)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003375 /* this inode is deleted */
David Howells1d1fe1e2008-02-07 00:15:37 -08003376 ret = -ESTALE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003377 goto bad_inode;
3378 }
3379 /* The only unlinked inodes we let through here have
3380 * valid i_mode and are being read by the orphan
3381 * recovery code: that's fine, we're about to complete
3382 * the process of deleting those. */
3383 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003384 ei->i_flags = le32_to_cpu(raw_inode->i_flags);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003385 inode->i_blocks = ext4_inode_blocks(raw_inode, ei);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05003386 ei->i_file_acl = le32_to_cpu(raw_inode->i_file_acl_lo);
Theodore Ts'oa9e81742009-04-24 16:11:18 -04003387 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07003388 ei->i_file_acl |=
3389 ((__u64)le16_to_cpu(raw_inode->i_file_acl_high)) << 32;
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05003390 inode->i_size = ext4_isize(raw_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003391 ei->i_disksize = inode->i_size;
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03003392#ifdef CONFIG_QUOTA
3393 ei->i_reserved_quota = 0;
3394#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003395 inode->i_generation = le32_to_cpu(raw_inode->i_generation);
3396 ei->i_block_group = iloc.block_group;
Theodore Ts'oa4912122009-03-12 12:18:34 -04003397 ei->i_last_alloc_group = ~0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003398 /*
3399 * NOTE! The in-memory inode i_data array is in little-endian order
3400 * even on big-endian machines: we do NOT byteswap the block numbers!
3401 */
Mingming Cao617ba132006-10-11 01:20:53 -07003402 for (block = 0; block < EXT4_N_BLOCKS; block++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003403 ei->i_data[block] = raw_inode->i_block[block];
3404 INIT_LIST_HEAD(&ei->i_orphan);
3405
Jan Karab436b9b2009-12-08 23:51:10 -05003406 /*
3407 * Set transaction id's of transactions that have to be committed
3408 * to finish f[data]sync. We set them to currently running transaction
3409 * as we cannot be sure that the inode or some of its metadata isn't
3410 * part of the transaction - the inode could have been reclaimed and
3411 * now it is reread from disk.
3412 */
3413 if (journal) {
3414 transaction_t *transaction;
3415 tid_t tid;
3416
Theodore Ts'oa931da62010-08-03 21:35:12 -04003417 read_lock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05003418 if (journal->j_running_transaction)
3419 transaction = journal->j_running_transaction;
3420 else
3421 transaction = journal->j_committing_transaction;
3422 if (transaction)
3423 tid = transaction->t_tid;
3424 else
3425 tid = journal->j_commit_sequence;
Theodore Ts'oa931da62010-08-03 21:35:12 -04003426 read_unlock(&journal->j_state_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05003427 ei->i_sync_tid = tid;
3428 ei->i_datasync_tid = tid;
3429 }
3430
Eric Sandeen0040d982008-02-05 22:36:43 -05003431 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003432 ei->i_extra_isize = le16_to_cpu(raw_inode->i_extra_isize);
Mingming Cao617ba132006-10-11 01:20:53 -07003433 if (EXT4_GOOD_OLD_INODE_SIZE + ei->i_extra_isize >
Kirill Korotaeve5d28612007-06-23 17:16:51 -07003434 EXT4_INODE_SIZE(inode->i_sb)) {
David Howells1d1fe1e2008-02-07 00:15:37 -08003435 ret = -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003436 goto bad_inode;
Kirill Korotaeve5d28612007-06-23 17:16:51 -07003437 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003438 if (ei->i_extra_isize == 0) {
3439 /* The extra space is currently unused. Use it. */
Mingming Cao617ba132006-10-11 01:20:53 -07003440 ei->i_extra_isize = sizeof(struct ext4_inode) -
3441 EXT4_GOOD_OLD_INODE_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003442 } else {
3443 __le32 *magic = (void *)raw_inode +
Mingming Cao617ba132006-10-11 01:20:53 -07003444 EXT4_GOOD_OLD_INODE_SIZE +
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003445 ei->i_extra_isize;
Mingming Cao617ba132006-10-11 01:20:53 -07003446 if (*magic == cpu_to_le32(EXT4_XATTR_MAGIC))
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003447 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003448 }
3449 } else
3450 ei->i_extra_isize = 0;
3451
Kalpak Shahef7f3832007-07-18 09:15:20 -04003452 EXT4_INODE_GET_XTIME(i_ctime, inode, raw_inode);
3453 EXT4_INODE_GET_XTIME(i_mtime, inode, raw_inode);
3454 EXT4_INODE_GET_XTIME(i_atime, inode, raw_inode);
3455 EXT4_EINODE_GET_XTIME(i_crtime, ei, raw_inode);
3456
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05003457 inode->i_version = le32_to_cpu(raw_inode->i_disk_version);
3458 if (EXT4_INODE_SIZE(inode->i_sb) > EXT4_GOOD_OLD_INODE_SIZE) {
3459 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
3460 inode->i_version |=
3461 (__u64)(le32_to_cpu(raw_inode->i_version_hi)) << 32;
3462 }
3463
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04003464 ret = 0;
Theodore Ts'o485c26e2009-04-24 13:43:20 -04003465 if (ei->i_file_acl &&
Theodore Ts'o10329882009-11-15 15:29:56 -05003466 !ext4_data_block_valid(EXT4_SB(sb), ei->i_file_acl, 1)) {
Theodore Ts'o24676da2010-05-16 21:00:00 -04003467 EXT4_ERROR_INODE(inode, "bad extended attribute block %llu",
3468 ei->i_file_acl);
Theodore Ts'o485c26e2009-04-24 13:43:20 -04003469 ret = -EIO;
3470 goto bad_inode;
Theodore Ts'o07a03822010-06-14 09:54:48 -04003471 } else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) {
Theodore Ts'oc4b5a612009-04-24 18:45:35 -04003472 if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
3473 (S_ISLNK(inode->i_mode) &&
3474 !ext4_inode_is_fast_symlink(inode)))
3475 /* Validate extent which is part of inode */
3476 ret = ext4_ext_check_inode(inode);
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003477 } else if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04003478 (S_ISLNK(inode->i_mode) &&
3479 !ext4_inode_is_fast_symlink(inode))) {
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003480 /* Validate block references which are part of inode */
Theodore Ts'o1f7d1e72011-06-27 19:16:02 -04003481 ret = ext4_ind_check_inode(inode);
Thiemo Nagelfe2c8192009-03-31 08:36:10 -04003482 }
Theodore Ts'o567f3e92009-11-14 08:19:05 -05003483 if (ret)
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003484 goto bad_inode;
Aneesh Kumar K.V7a262f72009-03-27 16:39:58 -04003485
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003486 if (S_ISREG(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003487 inode->i_op = &ext4_file_inode_operations;
3488 inode->i_fop = &ext4_file_operations;
3489 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003490 } else if (S_ISDIR(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003491 inode->i_op = &ext4_dir_inode_operations;
3492 inode->i_fop = &ext4_dir_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003493 } else if (S_ISLNK(inode->i_mode)) {
Duane Griffine83c1392008-12-19 20:47:15 +00003494 if (ext4_inode_is_fast_symlink(inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003495 inode->i_op = &ext4_fast_symlink_inode_operations;
Duane Griffine83c1392008-12-19 20:47:15 +00003496 nd_terminate_link(ei->i_data, inode->i_size,
3497 sizeof(ei->i_data) - 1);
3498 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003499 inode->i_op = &ext4_symlink_inode_operations;
3500 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003501 }
Theodore Ts'o563bdd62009-03-26 00:06:19 -04003502 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
3503 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003504 inode->i_op = &ext4_special_inode_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003505 if (raw_inode->i_block[0])
3506 init_special_inode(inode, inode->i_mode,
3507 old_decode_dev(le32_to_cpu(raw_inode->i_block[0])));
3508 else
3509 init_special_inode(inode, inode->i_mode,
3510 new_decode_dev(le32_to_cpu(raw_inode->i_block[1])));
Theodore Ts'o563bdd62009-03-26 00:06:19 -04003511 } else {
Theodore Ts'o563bdd62009-03-26 00:06:19 -04003512 ret = -EIO;
Theodore Ts'o24676da2010-05-16 21:00:00 -04003513 EXT4_ERROR_INODE(inode, "bogus i_mode (%o)", inode->i_mode);
Theodore Ts'o563bdd62009-03-26 00:06:19 -04003514 goto bad_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003515 }
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003516 brelse(iloc.bh);
Mingming Cao617ba132006-10-11 01:20:53 -07003517 ext4_set_inode_flags(inode);
David Howells1d1fe1e2008-02-07 00:15:37 -08003518 unlock_new_inode(inode);
3519 return inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003520
3521bad_inode:
Theodore Ts'o567f3e92009-11-14 08:19:05 -05003522 brelse(iloc.bh);
David Howells1d1fe1e2008-02-07 00:15:37 -08003523 iget_failed(inode);
3524 return ERR_PTR(ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003525}
3526
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003527static int ext4_inode_blocks_set(handle_t *handle,
3528 struct ext4_inode *raw_inode,
3529 struct ext4_inode_info *ei)
3530{
3531 struct inode *inode = &(ei->vfs_inode);
3532 u64 i_blocks = inode->i_blocks;
3533 struct super_block *sb = inode->i_sb;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003534
3535 if (i_blocks <= ~0U) {
3536 /*
3537 * i_blocks can be represnted in a 32 bit variable
3538 * as multiple of 512 bytes
3539 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05003540 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003541 raw_inode->i_blocks_high = 0;
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04003542 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Theodore Ts'of287a1a2008-10-16 22:50:48 -04003543 return 0;
3544 }
3545 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE))
3546 return -EFBIG;
3547
3548 if (i_blocks <= 0xffffffffffffULL) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003549 /*
3550 * i_blocks can be represented in a 48 bit variable
3551 * as multiple of 512 bytes
3552 */
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05003553 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003554 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04003555 ext4_clear_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003556 } else {
Dmitry Monakhov84a8dce2010-06-05 11:51:27 -04003557 ext4_set_inode_flag(inode, EXT4_INODE_HUGE_FILE);
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05003558 /* i_block is stored in file system block size */
3559 i_blocks = i_blocks >> (inode->i_blkbits - 9);
3560 raw_inode->i_blocks_lo = cpu_to_le32(i_blocks);
3561 raw_inode->i_blocks_high = cpu_to_le16(i_blocks >> 32);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003562 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04003563 return 0;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003564}
3565
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003566/*
3567 * Post the struct inode info into an on-disk inode location in the
3568 * buffer-cache. This gobbles the caller's reference to the
3569 * buffer_head in the inode location struct.
3570 *
3571 * The caller must have write access to iloc->bh.
3572 */
Mingming Cao617ba132006-10-11 01:20:53 -07003573static int ext4_do_update_inode(handle_t *handle,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003574 struct inode *inode,
Frank Mayhar830156c2009-09-29 10:07:47 -04003575 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003576{
Mingming Cao617ba132006-10-11 01:20:53 -07003577 struct ext4_inode *raw_inode = ext4_raw_inode(iloc);
3578 struct ext4_inode_info *ei = EXT4_I(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003579 struct buffer_head *bh = iloc->bh;
3580 int err = 0, rc, block;
3581
3582 /* For fields not not tracking in the in-memory inode,
3583 * initialise them to zero for new inodes. */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003584 if (ext4_test_inode_state(inode, EXT4_STATE_NEW))
Mingming Cao617ba132006-10-11 01:20:53 -07003585 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003586
Jan Karaff9ddf72007-07-18 09:24:20 -04003587 ext4_get_inode_flags(ei);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003588 raw_inode->i_mode = cpu_to_le16(inode->i_mode);
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003589 if (!(test_opt(inode->i_sb, NO_UID32))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003590 raw_inode->i_uid_low = cpu_to_le16(low_16_bits(inode->i_uid));
3591 raw_inode->i_gid_low = cpu_to_le16(low_16_bits(inode->i_gid));
3592/*
3593 * Fix up interoperability with old kernels. Otherwise, old inodes get
3594 * re-used with the upper 16 bits of the uid/gid intact
3595 */
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003596 if (!ei->i_dtime) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003597 raw_inode->i_uid_high =
3598 cpu_to_le16(high_16_bits(inode->i_uid));
3599 raw_inode->i_gid_high =
3600 cpu_to_le16(high_16_bits(inode->i_gid));
3601 } else {
3602 raw_inode->i_uid_high = 0;
3603 raw_inode->i_gid_high = 0;
3604 }
3605 } else {
3606 raw_inode->i_uid_low =
3607 cpu_to_le16(fs_high2lowuid(inode->i_uid));
3608 raw_inode->i_gid_low =
3609 cpu_to_le16(fs_high2lowgid(inode->i_gid));
3610 raw_inode->i_uid_high = 0;
3611 raw_inode->i_gid_high = 0;
3612 }
3613 raw_inode->i_links_count = cpu_to_le16(inode->i_nlink);
Kalpak Shahef7f3832007-07-18 09:15:20 -04003614
3615 EXT4_INODE_SET_XTIME(i_ctime, inode, raw_inode);
3616 EXT4_INODE_SET_XTIME(i_mtime, inode, raw_inode);
3617 EXT4_INODE_SET_XTIME(i_atime, inode, raw_inode);
3618 EXT4_EINODE_SET_XTIME(i_crtime, ei, raw_inode);
3619
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003620 if (ext4_inode_blocks_set(handle, raw_inode, ei))
3621 goto out_brelse;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003622 raw_inode->i_dtime = cpu_to_le32(ei->i_dtime);
Theodore Ts'o353eb832011-01-10 12:18:25 -05003623 raw_inode->i_flags = cpu_to_le32(ei->i_flags & 0xFFFFFFFF);
Mingming Cao9b8f1f02006-10-11 01:21:13 -07003624 if (EXT4_SB(inode->i_sb)->s_es->s_creator_os !=
3625 cpu_to_le32(EXT4_OS_HURD))
Badari Pulavartya1ddeb72006-10-11 01:21:09 -07003626 raw_inode->i_file_acl_high =
3627 cpu_to_le16(ei->i_file_acl >> 32);
Aneesh Kumar K.V7973c0c2008-01-28 23:58:27 -05003628 raw_inode->i_file_acl_lo = cpu_to_le32(ei->i_file_acl);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05003629 ext4_isize_set(raw_inode, ei->i_disksize);
3630 if (ei->i_disksize > 0x7fffffffULL) {
3631 struct super_block *sb = inode->i_sb;
3632 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
3633 EXT4_FEATURE_RO_COMPAT_LARGE_FILE) ||
3634 EXT4_SB(sb)->s_es->s_rev_level ==
3635 cpu_to_le32(EXT4_GOOD_OLD_REV)) {
3636 /* If this is the first large file
3637 * created, add a flag to the superblock.
3638 */
3639 err = ext4_journal_get_write_access(handle,
3640 EXT4_SB(sb)->s_sbh);
3641 if (err)
3642 goto out_brelse;
3643 ext4_update_dynamic_rev(sb);
3644 EXT4_SET_RO_COMPAT_FEATURE(sb,
Mingming Cao617ba132006-10-11 01:20:53 -07003645 EXT4_FEATURE_RO_COMPAT_LARGE_FILE);
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05003646 sb->s_dirt = 1;
Frank Mayhar03901312009-01-07 00:06:22 -05003647 ext4_handle_sync(handle);
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05003648 err = ext4_handle_dirty_metadata(handle, NULL,
Aneesh Kumar K.Va48380f2008-01-28 23:58:27 -05003649 EXT4_SB(sb)->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003650 }
3651 }
3652 raw_inode->i_generation = cpu_to_le32(inode->i_generation);
3653 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
3654 if (old_valid_dev(inode->i_rdev)) {
3655 raw_inode->i_block[0] =
3656 cpu_to_le32(old_encode_dev(inode->i_rdev));
3657 raw_inode->i_block[1] = 0;
3658 } else {
3659 raw_inode->i_block[0] = 0;
3660 raw_inode->i_block[1] =
3661 cpu_to_le32(new_encode_dev(inode->i_rdev));
3662 raw_inode->i_block[2] = 0;
3663 }
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04003664 } else
3665 for (block = 0; block < EXT4_N_BLOCKS; block++)
3666 raw_inode->i_block[block] = ei->i_data[block];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003667
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05003668 raw_inode->i_disk_version = cpu_to_le32(inode->i_version);
3669 if (ei->i_extra_isize) {
3670 if (EXT4_FITS_IN_INODE(raw_inode, ei, i_version_hi))
3671 raw_inode->i_version_hi =
3672 cpu_to_le32(inode->i_version >> 32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003673 raw_inode->i_extra_isize = cpu_to_le16(ei->i_extra_isize);
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05003674 }
3675
Frank Mayhar830156c2009-09-29 10:07:47 -04003676 BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata");
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05003677 rc = ext4_handle_dirty_metadata(handle, NULL, bh);
Frank Mayhar830156c2009-09-29 10:07:47 -04003678 if (!err)
3679 err = rc;
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05003680 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003681
Jan Karab436b9b2009-12-08 23:51:10 -05003682 ext4_update_inode_fsync_trans(handle, inode, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003683out_brelse:
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04003684 brelse(bh);
Mingming Cao617ba132006-10-11 01:20:53 -07003685 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003686 return err;
3687}
3688
3689/*
Mingming Cao617ba132006-10-11 01:20:53 -07003690 * ext4_write_inode()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003691 *
3692 * We are called from a few places:
3693 *
3694 * - Within generic_file_write() for O_SYNC files.
3695 * Here, there will be no transaction running. We wait for any running
3696 * trasnaction to commit.
3697 *
3698 * - Within sys_sync(), kupdate and such.
3699 * We wait on commit, if tol to.
3700 *
3701 * - Within prune_icache() (PF_MEMALLOC == true)
3702 * Here we simply return. We can't afford to block kswapd on the
3703 * journal commit.
3704 *
3705 * In all cases it is actually safe for us to return without doing anything,
3706 * because the inode has been copied into a raw inode buffer in
Mingming Cao617ba132006-10-11 01:20:53 -07003707 * ext4_mark_inode_dirty(). This is a correctness thing for O_SYNC and for
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003708 * knfsd.
3709 *
3710 * Note that we are absolutely dependent upon all inode dirtiers doing the
3711 * right thing: they *must* call mark_inode_dirty() after dirtying info in
3712 * which we are interested.
3713 *
3714 * It would be a bug for them to not do this. The code:
3715 *
3716 * mark_inode_dirty(inode)
3717 * stuff();
3718 * inode->i_size = expr;
3719 *
3720 * is in error because a kswapd-driven write_inode() could occur while
3721 * `stuff()' is running, and the new i_size will be lost. Plus the inode
3722 * will no longer be on the superblock's dirty inode list.
3723 */
Christoph Hellwiga9185b42010-03-05 09:21:37 +01003724int ext4_write_inode(struct inode *inode, struct writeback_control *wbc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003725{
Frank Mayhar91ac6f42009-09-09 22:33:47 -04003726 int err;
3727
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003728 if (current->flags & PF_MEMALLOC)
3729 return 0;
3730
Frank Mayhar91ac6f42009-09-09 22:33:47 -04003731 if (EXT4_SB(inode->i_sb)->s_journal) {
3732 if (ext4_journal_current_handle()) {
3733 jbd_debug(1, "called recursively, non-PF_MEMALLOC!\n");
3734 dump_stack();
3735 return -EIO;
3736 }
3737
Christoph Hellwiga9185b42010-03-05 09:21:37 +01003738 if (wbc->sync_mode != WB_SYNC_ALL)
Frank Mayhar91ac6f42009-09-09 22:33:47 -04003739 return 0;
3740
3741 err = ext4_force_commit(inode->i_sb);
3742 } else {
3743 struct ext4_iloc iloc;
3744
Curt Wohlgemuth8b472d72010-04-03 16:45:06 -04003745 err = __ext4_get_inode_loc(inode, &iloc, 0);
Frank Mayhar91ac6f42009-09-09 22:33:47 -04003746 if (err)
3747 return err;
Christoph Hellwiga9185b42010-03-05 09:21:37 +01003748 if (wbc->sync_mode == WB_SYNC_ALL)
Frank Mayhar830156c2009-09-29 10:07:47 -04003749 sync_dirty_buffer(iloc.bh);
3750 if (buffer_req(iloc.bh) && !buffer_uptodate(iloc.bh)) {
Theodore Ts'oc398eda2010-07-27 11:56:40 -04003751 EXT4_ERROR_INODE_BLOCK(inode, iloc.bh->b_blocknr,
3752 "IO error syncing inode");
Frank Mayhar830156c2009-09-29 10:07:47 -04003753 err = -EIO;
3754 }
Curt Wohlgemuthfd2dd9f2010-04-03 17:44:16 -04003755 brelse(iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003756 }
Frank Mayhar91ac6f42009-09-09 22:33:47 -04003757 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003758}
3759
3760/*
Mingming Cao617ba132006-10-11 01:20:53 -07003761 * ext4_setattr()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003762 *
3763 * Called from notify_change.
3764 *
3765 * We want to trap VFS attempts to truncate the file as soon as
3766 * possible. In particular, we want to make sure that when the VFS
3767 * shrinks i_size, we put the inode on the orphan list and modify
3768 * i_disksize immediately, so that during the subsequent flushing of
3769 * dirty pages and freeing of disk blocks, we can guarantee that any
3770 * commit will leave the blocks being flushed in an unused state on
3771 * disk. (On recovery, the inode will get truncated and the blocks will
3772 * be freed, so we have a strong guarantee that no future commit will
3773 * leave these blocks visible to the user.)
3774 *
Jan Kara678aaf42008-07-11 19:27:31 -04003775 * Another thing we have to assure is that if we are in ordered mode
3776 * and inode is still attached to the committing transaction, we must
3777 * we start writeout of all the dirty pages which are being truncated.
3778 * This way we are sure that all the data written in the previous
3779 * transaction are already on disk (truncate waits for pages under
3780 * writeback).
3781 *
3782 * Called with inode->i_mutex down.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003783 */
Mingming Cao617ba132006-10-11 01:20:53 -07003784int ext4_setattr(struct dentry *dentry, struct iattr *attr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003785{
3786 struct inode *inode = dentry->d_inode;
3787 int error, rc = 0;
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04003788 int orphan = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003789 const unsigned int ia_valid = attr->ia_valid;
3790
3791 error = inode_change_ok(inode, attr);
3792 if (error)
3793 return error;
3794
Dmitry Monakhov12755622010-04-08 22:04:20 +04003795 if (is_quota_modification(inode, attr))
Christoph Hellwig871a2932010-03-03 09:05:07 -05003796 dquot_initialize(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003797 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
3798 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) {
3799 handle_t *handle;
3800
3801 /* (user+group)*(old+new) structure, inode write (sb,
3802 * inode block, ? - but truncate inode update has it) */
Dmitry Monakhov5aca07e2009-12-08 22:42:15 -05003803 handle = ext4_journal_start(inode, (EXT4_MAXQUOTAS_INIT_BLOCKS(inode->i_sb)+
Dmitry Monakhov194074a2009-12-08 22:42:28 -05003804 EXT4_MAXQUOTAS_DEL_BLOCKS(inode->i_sb))+3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003805 if (IS_ERR(handle)) {
3806 error = PTR_ERR(handle);
3807 goto err_out;
3808 }
Christoph Hellwigb43fa822010-03-03 09:05:03 -05003809 error = dquot_transfer(inode, attr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003810 if (error) {
Mingming Cao617ba132006-10-11 01:20:53 -07003811 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003812 return error;
3813 }
3814 /* Update corresponding info in inode so that everything is in
3815 * one transaction */
3816 if (attr->ia_valid & ATTR_UID)
3817 inode->i_uid = attr->ia_uid;
3818 if (attr->ia_valid & ATTR_GID)
3819 inode->i_gid = attr->ia_gid;
Mingming Cao617ba132006-10-11 01:20:53 -07003820 error = ext4_mark_inode_dirty(handle, inode);
3821 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003822 }
3823
Eric Sandeene2b46572008-01-28 23:58:27 -05003824 if (attr->ia_valid & ATTR_SIZE) {
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003825 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) {
Eric Sandeene2b46572008-01-28 23:58:27 -05003826 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
3827
Theodore Ts'o0c095c72010-07-27 11:56:06 -04003828 if (attr->ia_size > sbi->s_bitmap_maxbytes)
3829 return -EFBIG;
Eric Sandeene2b46572008-01-28 23:58:27 -05003830 }
3831 }
3832
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003833 if (S_ISREG(inode->i_mode) &&
Jiaying Zhangc8d46e42010-02-24 09:52:53 -05003834 attr->ia_valid & ATTR_SIZE &&
Theodore Ts'o072bd7e2011-05-23 15:13:02 -04003835 (attr->ia_size < inode->i_size)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003836 handle_t *handle;
3837
Mingming Cao617ba132006-10-11 01:20:53 -07003838 handle = ext4_journal_start(inode, 3);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003839 if (IS_ERR(handle)) {
3840 error = PTR_ERR(handle);
3841 goto err_out;
3842 }
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04003843 if (ext4_handle_valid(handle)) {
3844 error = ext4_orphan_add(handle, inode);
3845 orphan = 1;
3846 }
Mingming Cao617ba132006-10-11 01:20:53 -07003847 EXT4_I(inode)->i_disksize = attr->ia_size;
3848 rc = ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003849 if (!error)
3850 error = rc;
Mingming Cao617ba132006-10-11 01:20:53 -07003851 ext4_journal_stop(handle);
Jan Kara678aaf42008-07-11 19:27:31 -04003852
3853 if (ext4_should_order_data(inode)) {
3854 error = ext4_begin_ordered_truncate(inode,
3855 attr->ia_size);
3856 if (error) {
3857 /* Do as much error cleanup as possible */
3858 handle = ext4_journal_start(inode, 3);
3859 if (IS_ERR(handle)) {
3860 ext4_orphan_del(NULL, inode);
3861 goto err_out;
3862 }
3863 ext4_orphan_del(handle, inode);
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04003864 orphan = 0;
Jan Kara678aaf42008-07-11 19:27:31 -04003865 ext4_journal_stop(handle);
3866 goto err_out;
3867 }
3868 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003869 }
3870
Theodore Ts'o072bd7e2011-05-23 15:13:02 -04003871 if (attr->ia_valid & ATTR_SIZE) {
3872 if (attr->ia_size != i_size_read(inode)) {
3873 truncate_setsize(inode, attr->ia_size);
3874 ext4_truncate(inode);
3875 } else if (ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
3876 ext4_truncate(inode);
3877 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003878
Christoph Hellwig10257742010-06-04 11:30:02 +02003879 if (!rc) {
3880 setattr_copy(inode, attr);
3881 mark_inode_dirty(inode);
3882 }
3883
3884 /*
3885 * If the call to ext4_truncate failed to get a transaction handle at
3886 * all, we need to clean up the in-core orphan list manually.
3887 */
Dmitry Monakhov3d287de2010-10-27 22:08:46 -04003888 if (orphan && inode->i_nlink)
Mingming Cao617ba132006-10-11 01:20:53 -07003889 ext4_orphan_del(NULL, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003890
3891 if (!rc && (ia_valid & ATTR_MODE))
Mingming Cao617ba132006-10-11 01:20:53 -07003892 rc = ext4_acl_chmod(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003893
3894err_out:
Mingming Cao617ba132006-10-11 01:20:53 -07003895 ext4_std_error(inode->i_sb, error);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003896 if (!error)
3897 error = rc;
3898 return error;
3899}
3900
Mingming Cao3e3398a2008-07-11 19:27:31 -04003901int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
3902 struct kstat *stat)
3903{
3904 struct inode *inode;
3905 unsigned long delalloc_blocks;
3906
3907 inode = dentry->d_inode;
3908 generic_fillattr(inode, stat);
3909
3910 /*
3911 * We can't update i_blocks if the block allocation is delayed
3912 * otherwise in the case of system crash before the real block
3913 * allocation is done, we will have i_blocks inconsistent with
3914 * on-disk file blocks.
3915 * We always keep i_blocks updated together with real
3916 * allocation. But to not confuse with user, stat
3917 * will return the blocks that include the delayed allocation
3918 * blocks for this file.
3919 */
Mingming Cao3e3398a2008-07-11 19:27:31 -04003920 delalloc_blocks = EXT4_I(inode)->i_reserved_data_blocks;
Mingming Cao3e3398a2008-07-11 19:27:31 -04003921
3922 stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9;
3923 return 0;
3924}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003925
Mingming Caoa02908f2008-08-19 22:16:07 -04003926static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
3927{
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04003928 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
Amir Goldstein8bb2b242011-06-27 17:10:28 -04003929 return ext4_ind_trans_blocks(inode, nrblocks, chunk);
Theodore Ts'oac51d832008-11-06 16:49:36 -05003930 return ext4_ext_index_trans_blocks(inode, nrblocks, chunk);
Mingming Caoa02908f2008-08-19 22:16:07 -04003931}
Theodore Ts'oac51d832008-11-06 16:49:36 -05003932
Mingming Caoa02908f2008-08-19 22:16:07 -04003933/*
3934 * Account for index blocks, block groups bitmaps and block group
3935 * descriptor blocks if modify datablocks and index blocks
3936 * worse case, the indexs blocks spread over different block groups
3937 *
3938 * If datablocks are discontiguous, they are possible to spread over
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003939 * different block groups too. If they are contiuguous, with flexbg,
Mingming Caoa02908f2008-08-19 22:16:07 -04003940 * they could still across block group boundary.
3941 *
3942 * Also account for superblock, inode, quota and xattr blocks
3943 */
Theodore Ts'o1f109d52010-10-27 21:30:14 -04003944static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
Mingming Caoa02908f2008-08-19 22:16:07 -04003945{
Theodore Ts'o8df96752009-05-01 08:50:38 -04003946 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
3947 int gdpblocks;
Mingming Caoa02908f2008-08-19 22:16:07 -04003948 int idxblocks;
3949 int ret = 0;
3950
3951 /*
3952 * How many index blocks need to touch to modify nrblocks?
3953 * The "Chunk" flag indicating whether the nrblocks is
3954 * physically contiguous on disk
3955 *
3956 * For Direct IO and fallocate, they calls get_block to allocate
3957 * one single extent at a time, so they could set the "Chunk" flag
3958 */
3959 idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk);
3960
3961 ret = idxblocks;
3962
3963 /*
3964 * Now let's see how many group bitmaps and group descriptors need
3965 * to account
3966 */
3967 groups = idxblocks;
3968 if (chunk)
3969 groups += 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003970 else
Mingming Caoa02908f2008-08-19 22:16:07 -04003971 groups += nrblocks;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003972
Mingming Caoa02908f2008-08-19 22:16:07 -04003973 gdpblocks = groups;
Theodore Ts'o8df96752009-05-01 08:50:38 -04003974 if (groups > ngroups)
3975 groups = ngroups;
Mingming Caoa02908f2008-08-19 22:16:07 -04003976 if (groups > EXT4_SB(inode->i_sb)->s_gdb_count)
3977 gdpblocks = EXT4_SB(inode->i_sb)->s_gdb_count;
3978
3979 /* bitmaps and block group descriptor blocks */
3980 ret += groups + gdpblocks;
3981
3982 /* Blocks for super block, inode, quota and xattr blocks */
3983 ret += EXT4_META_TRANS_BLOCKS(inode->i_sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003984
3985 return ret;
3986}
3987
3988/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003989 * Calculate the total number of credits to reserve to fit
Mingming Caof3bd1f32008-08-19 22:16:03 -04003990 * the modification of a single pages into a single transaction,
3991 * which may include multiple chunks of block allocations.
Mingming Caoa02908f2008-08-19 22:16:07 -04003992 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04003993 * This could be called via ext4_write_begin()
Mingming Caoa02908f2008-08-19 22:16:07 -04003994 *
Mingming Cao525f4ed2008-08-19 22:15:58 -04003995 * We need to consider the worse case, when
Mingming Caoa02908f2008-08-19 22:16:07 -04003996 * one new block per extent.
Mingming Caoa02908f2008-08-19 22:16:07 -04003997 */
3998int ext4_writepage_trans_blocks(struct inode *inode)
3999{
4000 int bpp = ext4_journal_blocks_per_page(inode);
4001 int ret;
4002
4003 ret = ext4_meta_trans_blocks(inode, bpp, 0);
4004
4005 /* Account for data blocks for journalled mode */
4006 if (ext4_should_journal_data(inode))
4007 ret += bpp;
4008 return ret;
4009}
Mingming Caof3bd1f32008-08-19 22:16:03 -04004010
4011/*
4012 * Calculate the journal credits for a chunk of data modification.
4013 *
4014 * This is called from DIO, fallocate or whoever calling
Eric Sandeen79e83032010-07-27 11:56:07 -04004015 * ext4_map_blocks() to map/allocate a chunk of contiguous disk blocks.
Mingming Caof3bd1f32008-08-19 22:16:03 -04004016 *
4017 * journal buffers for data blocks are not included here, as DIO
4018 * and fallocate do no need to journal data buffers.
4019 */
4020int ext4_chunk_trans_blocks(struct inode *inode, int nrblocks)
4021{
4022 return ext4_meta_trans_blocks(inode, nrblocks, 1);
4023}
4024
Mingming Caoa02908f2008-08-19 22:16:07 -04004025/*
Mingming Cao617ba132006-10-11 01:20:53 -07004026 * The caller must have previously called ext4_reserve_inode_write().
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004027 * Give this, we know that the caller already has write access to iloc->bh.
4028 */
Mingming Cao617ba132006-10-11 01:20:53 -07004029int ext4_mark_iloc_dirty(handle_t *handle,
Theodore Ts'ode9a55b2009-06-14 17:45:34 -04004030 struct inode *inode, struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004031{
4032 int err = 0;
4033
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05004034 if (test_opt(inode->i_sb, I_VERSION))
4035 inode_inc_iversion(inode);
4036
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004037 /* the do_update_inode consumes one bh->b_count */
4038 get_bh(iloc->bh);
4039
Mingming Caodab291a2006-10-11 01:21:01 -07004040 /* ext4_do_update_inode() does jbd2_journal_dirty_metadata */
Frank Mayhar830156c2009-09-29 10:07:47 -04004041 err = ext4_do_update_inode(handle, inode, iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004042 put_bh(iloc->bh);
4043 return err;
4044}
4045
4046/*
4047 * On success, We end up with an outstanding reference count against
4048 * iloc->bh. This _must_ be cleaned up later.
4049 */
4050
4051int
Mingming Cao617ba132006-10-11 01:20:53 -07004052ext4_reserve_inode_write(handle_t *handle, struct inode *inode,
4053 struct ext4_iloc *iloc)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004054{
Frank Mayhar03901312009-01-07 00:06:22 -05004055 int err;
4056
4057 err = ext4_get_inode_loc(inode, iloc);
4058 if (!err) {
4059 BUFFER_TRACE(iloc->bh, "get_write_access");
4060 err = ext4_journal_get_write_access(handle, iloc->bh);
4061 if (err) {
4062 brelse(iloc->bh);
4063 iloc->bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004064 }
4065 }
Mingming Cao617ba132006-10-11 01:20:53 -07004066 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004067 return err;
4068}
4069
4070/*
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004071 * Expand an inode by new_extra_isize bytes.
4072 * Returns 0 on success or negative error number on failure.
4073 */
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05004074static int ext4_expand_extra_isize(struct inode *inode,
4075 unsigned int new_extra_isize,
4076 struct ext4_iloc iloc,
4077 handle_t *handle)
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004078{
4079 struct ext4_inode *raw_inode;
4080 struct ext4_xattr_ibody_header *header;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004081
4082 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
4083 return 0;
4084
4085 raw_inode = ext4_raw_inode(&iloc);
4086
4087 header = IHDR(inode, raw_inode);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004088
4089 /* No extended attributes present */
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004090 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR) ||
4091 header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004092 memset((void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE, 0,
4093 new_extra_isize);
4094 EXT4_I(inode)->i_extra_isize = new_extra_isize;
4095 return 0;
4096 }
4097
4098 /* try to expand with EAs present */
4099 return ext4_expand_extra_isize_ea(inode, new_extra_isize,
4100 raw_inode, handle);
4101}
4102
4103/*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004104 * What we do here is to mark the in-core inode as clean with respect to inode
4105 * dirtiness (it may still be data-dirty).
4106 * This means that the in-core inode may be reaped by prune_icache
4107 * without having to perform any I/O. This is a very good thing,
4108 * because *any* task may call prune_icache - even ones which
4109 * have a transaction open against a different journal.
4110 *
4111 * Is this cheating? Not really. Sure, we haven't written the
4112 * inode out, but prune_icache isn't a user-visible syncing function.
4113 * Whenever the user wants stuff synced (sys_sync, sys_msync, sys_fsync)
4114 * we start and wait on commits.
4115 *
4116 * Is this efficient/effective? Well, we're being nice to the system
4117 * by cleaning up our inodes proactively so they can be reaped
4118 * without I/O. But we are potentially leaving up to five seconds'
4119 * worth of inodes floating about which prune_icache wants us to
4120 * write out. One way to fix that would be to get prune_icache()
4121 * to do a write_super() to free up some memory. It has the desired
4122 * effect.
4123 */
Mingming Cao617ba132006-10-11 01:20:53 -07004124int ext4_mark_inode_dirty(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004125{
Mingming Cao617ba132006-10-11 01:20:53 -07004126 struct ext4_iloc iloc;
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004127 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
4128 static unsigned int mnt_count;
4129 int err, ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004130
4131 might_sleep();
Theodore Ts'o7ff9c072010-11-08 13:51:33 -05004132 trace_ext4_mark_inode_dirty(inode, _RET_IP_);
Mingming Cao617ba132006-10-11 01:20:53 -07004133 err = ext4_reserve_inode_write(handle, inode, &iloc);
Frank Mayhar03901312009-01-07 00:06:22 -05004134 if (ext4_handle_valid(handle) &&
4135 EXT4_I(inode)->i_extra_isize < sbi->s_want_extra_isize &&
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004136 !ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND)) {
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004137 /*
4138 * We need extra buffer credits since we may write into EA block
4139 * with this same handle. If journal_extend fails, then it will
4140 * only result in a minor loss of functionality for that inode.
4141 * If this is felt to be critical, then e2fsck should be run to
4142 * force a large enough s_min_extra_isize.
4143 */
4144 if ((jbd2_journal_extend(handle,
4145 EXT4_DATA_TRANS_BLOCKS(inode->i_sb))) == 0) {
4146 ret = ext4_expand_extra_isize(inode,
4147 sbi->s_want_extra_isize,
4148 iloc, handle);
4149 if (ret) {
Theodore Ts'o19f5fb72010-01-24 14:34:07 -05004150 ext4_set_inode_state(inode,
4151 EXT4_STATE_NO_EXPAND);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04004152 if (mnt_count !=
4153 le16_to_cpu(sbi->s_es->s_mnt_count)) {
Eric Sandeen12062dd2010-02-15 14:19:27 -05004154 ext4_warning(inode->i_sb,
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004155 "Unable to expand inode %lu. Delete"
4156 " some EAs or run e2fsck.",
4157 inode->i_ino);
Aneesh Kumar K.Vc1bddad2007-10-16 18:38:25 -04004158 mnt_count =
4159 le16_to_cpu(sbi->s_es->s_mnt_count);
Kalpak Shah6dd4ee72007-07-18 09:19:57 -04004160 }
4161 }
4162 }
4163 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004164 if (!err)
Mingming Cao617ba132006-10-11 01:20:53 -07004165 err = ext4_mark_iloc_dirty(handle, inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004166 return err;
4167}
4168
4169/*
Mingming Cao617ba132006-10-11 01:20:53 -07004170 * ext4_dirty_inode() is called from __mark_inode_dirty()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004171 *
4172 * We're really interested in the case where a file is being extended.
4173 * i_size has been changed by generic_commit_write() and we thus need
4174 * to include the updated inode in the current transaction.
4175 *
Christoph Hellwig5dd40562010-03-03 09:05:00 -05004176 * Also, dquot_alloc_block() will always dirty the inode when blocks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004177 * are allocated to the file.
4178 *
4179 * If the inode is marked synchronous, we don't honour that here - doing
4180 * so would cause a commit on atime updates, which we don't bother doing.
4181 * We handle synchronous inodes at the highest possible level.
4182 */
Christoph Hellwigaa385722011-05-27 06:53:02 -04004183void ext4_dirty_inode(struct inode *inode, int flags)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004184{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004185 handle_t *handle;
4186
Mingming Cao617ba132006-10-11 01:20:53 -07004187 handle = ext4_journal_start(inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004188 if (IS_ERR(handle))
4189 goto out;
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04004190
Curt Wohlgemuthf3dc2722009-09-29 16:06:01 -04004191 ext4_mark_inode_dirty(handle, inode);
4192
Mingming Cao617ba132006-10-11 01:20:53 -07004193 ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004194out:
4195 return;
4196}
4197
4198#if 0
4199/*
4200 * Bind an inode's backing buffer_head into this transaction, to prevent
4201 * it from being flushed to disk early. Unlike
Mingming Cao617ba132006-10-11 01:20:53 -07004202 * ext4_reserve_inode_write, this leaves behind no bh reference and
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004203 * returns no iloc structure, so the caller needs to repeat the iloc
4204 * lookup to mark the inode dirty later.
4205 */
Mingming Cao617ba132006-10-11 01:20:53 -07004206static int ext4_pin_inode(handle_t *handle, struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004207{
Mingming Cao617ba132006-10-11 01:20:53 -07004208 struct ext4_iloc iloc;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004209
4210 int err = 0;
4211 if (handle) {
Mingming Cao617ba132006-10-11 01:20:53 -07004212 err = ext4_get_inode_loc(inode, &iloc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004213 if (!err) {
4214 BUFFER_TRACE(iloc.bh, "get_write_access");
Mingming Caodab291a2006-10-11 01:21:01 -07004215 err = jbd2_journal_get_write_access(handle, iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004216 if (!err)
Frank Mayhar03901312009-01-07 00:06:22 -05004217 err = ext4_handle_dirty_metadata(handle,
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -05004218 NULL,
Frank Mayhar03901312009-01-07 00:06:22 -05004219 iloc.bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004220 brelse(iloc.bh);
4221 }
4222 }
Mingming Cao617ba132006-10-11 01:20:53 -07004223 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004224 return err;
4225}
4226#endif
4227
Mingming Cao617ba132006-10-11 01:20:53 -07004228int ext4_change_inode_journal_flag(struct inode *inode, int val)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004229{
4230 journal_t *journal;
4231 handle_t *handle;
4232 int err;
4233
4234 /*
4235 * We have to be very careful here: changing a data block's
4236 * journaling status dynamically is dangerous. If we write a
4237 * data block to the journal, change the status and then delete
4238 * that block, we risk forgetting to revoke the old log record
4239 * from the journal and so a subsequent replay can corrupt data.
4240 * So, first we make sure that the journal is empty and that
4241 * nobody is changing anything.
4242 */
4243
Mingming Cao617ba132006-10-11 01:20:53 -07004244 journal = EXT4_JOURNAL(inode);
Frank Mayhar03901312009-01-07 00:06:22 -05004245 if (!journal)
4246 return 0;
Dave Hansend6995942007-07-18 08:33:51 -04004247 if (is_journal_aborted(journal))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004248 return -EROFS;
4249
Mingming Caodab291a2006-10-11 01:21:01 -07004250 jbd2_journal_lock_updates(journal);
4251 jbd2_journal_flush(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004252
4253 /*
4254 * OK, there are no updates running now, and all cached data is
4255 * synced to disk. We are now in a completely consistent state
4256 * which doesn't have anything in the journal, and we know that
4257 * no filesystem updates are running, so it is safe to modify
4258 * the inode's in-core data-journaling state flag now.
4259 */
4260
4261 if (val)
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004262 ext4_set_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004263 else
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004264 ext4_clear_inode_flag(inode, EXT4_INODE_JOURNAL_DATA);
Mingming Cao617ba132006-10-11 01:20:53 -07004265 ext4_set_aops(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004266
Mingming Caodab291a2006-10-11 01:21:01 -07004267 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004268
4269 /* Finally we can mark the inode as dirty. */
4270
Mingming Cao617ba132006-10-11 01:20:53 -07004271 handle = ext4_journal_start(inode, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004272 if (IS_ERR(handle))
4273 return PTR_ERR(handle);
4274
Mingming Cao617ba132006-10-11 01:20:53 -07004275 err = ext4_mark_inode_dirty(handle, inode);
Frank Mayhar03901312009-01-07 00:06:22 -05004276 ext4_handle_sync(handle);
Mingming Cao617ba132006-10-11 01:20:53 -07004277 ext4_journal_stop(handle);
4278 ext4_std_error(inode->i_sb, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004279
4280 return err;
4281}
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004282
4283static int ext4_bh_unmapped(handle_t *handle, struct buffer_head *bh)
4284{
4285 return !buffer_mapped(bh);
4286}
4287
Nick Pigginc2ec1752009-03-31 15:23:21 -07004288int ext4_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004289{
Nick Pigginc2ec1752009-03-31 15:23:21 -07004290 struct page *page = vmf->page;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004291 loff_t size;
4292 unsigned long len;
4293 int ret = -EINVAL;
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04004294 void *fsdata;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004295 struct file *file = vma->vm_file;
4296 struct inode *inode = file->f_path.dentry->d_inode;
4297 struct address_space *mapping = inode->i_mapping;
4298
4299 /*
4300 * Get i_alloc_sem to stop truncates messing with the inode. We cannot
4301 * get i_mutex because we are already holding mmap_sem.
4302 */
4303 down_read(&inode->i_alloc_sem);
4304 size = i_size_read(inode);
4305 if (page->mapping != mapping || size <= page_offset(page)
4306 || !PageUptodate(page)) {
4307 /* page got truncated from under us? */
4308 goto out_unlock;
4309 }
4310 ret = 0;
Darrick J. Wong0e499892011-05-18 13:55:20 -04004311
4312 lock_page(page);
4313 wait_on_page_writeback(page);
4314 if (PageMappedToDisk(page)) {
4315 up_read(&inode->i_alloc_sem);
4316 return VM_FAULT_LOCKED;
4317 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004318
4319 if (page->index == size >> PAGE_CACHE_SHIFT)
4320 len = size & ~PAGE_CACHE_MASK;
4321 else
4322 len = PAGE_CACHE_SIZE;
4323
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04004324 /*
4325 * return if we have all the buffers mapped. This avoid
4326 * the need to call write_begin/write_end which does a
4327 * journal_start/journal_stop which can block and take
4328 * long time
4329 */
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004330 if (page_has_buffers(page)) {
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004331 if (!walk_page_buffers(NULL, page_buffers(page), 0, len, NULL,
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04004332 ext4_bh_unmapped)) {
Darrick J. Wong0e499892011-05-18 13:55:20 -04004333 up_read(&inode->i_alloc_sem);
4334 return VM_FAULT_LOCKED;
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04004335 }
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004336 }
Aneesh Kumar K.Va827eaf2009-09-09 22:36:03 -04004337 unlock_page(page);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004338 /*
4339 * OK, we need to fill the hole... Do write_begin write_end
4340 * to do block allocation/reservation.We are not holding
4341 * inode.i__mutex here. That allow * parallel write_begin,
4342 * write_end call. lock_page prevent this from happening
4343 * on the same page though
4344 */
4345 ret = mapping->a_ops->write_begin(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04004346 len, AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004347 if (ret < 0)
4348 goto out_unlock;
4349 ret = mapping->a_ops->write_end(file, mapping, page_offset(page),
Aneesh Kumar K.V79f0be82008-10-08 23:13:30 -04004350 len, len, page, fsdata);
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004351 if (ret < 0)
4352 goto out_unlock;
4353 ret = 0;
Darrick J. Wong0e499892011-05-18 13:55:20 -04004354
4355 /*
4356 * write_begin/end might have created a dirty page and someone
4357 * could wander in and start the IO. Make sure that hasn't
4358 * happened.
4359 */
4360 lock_page(page);
4361 wait_on_page_writeback(page);
4362 up_read(&inode->i_alloc_sem);
4363 return VM_FAULT_LOCKED;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004364out_unlock:
Nick Pigginc2ec1752009-03-31 15:23:21 -07004365 if (ret)
4366 ret = VM_FAULT_SIGBUS;
Aneesh Kumar K.V2e9ee852008-07-11 19:27:31 -04004367 up_read(&inode->i_alloc_sem);
4368 return ret;
4369}